// ============================================= // Aster: image.h // Copyright (c) 2020-2024 Anish Bhobe // ============================================= #pragma once #include "global.h" struct Device; [[nodiscard]] inline vk::Extent2D ToExtent2D(const vk::Extent3D &extent) { return {extent.width, extent.height}; } [[nodiscard]] inline vk::Extent3D ToExtent3D(const vk::Extent2D &extent, const u32 depth) { return {extent.width, extent.height, depth}; } [[nodiscard]] inline vk::Offset2D ToOffset2D(const vk::Extent3D &extent) { return {Cast(extent.width), Cast(extent.height)}; } [[nodiscard]] inline vk::Offset3D ToOffset3D(const vk::Extent3D &extent) { return {Cast(extent.width), Cast(extent.height), Cast(extent.depth)}; } struct Image { vk::Image m_Image = nullptr; vk::ImageView m_View = nullptr; VmaAllocation m_Allocation = nullptr; vk::Extent3D m_Extent; // Image.m_MipLevels_ is used for bookkeeping // If the image is Invalid, the remaining data in Image is used intrusively by `GpuResourceManager`. u32 m_MipLevels_ = 0; [[nodiscard]] bool IsValid() const; [[nodiscard]] bool IsOwned() const; [[nodiscard]] u32 GetMipLevels() const; void Destroy(const Device *device); constexpr static u32 VALID_BIT = 1u << 31; constexpr static u32 OWNED_BIT = 1u << 30; constexpr static u32 MIP_MASK = ~(VALID_BIT | OWNED_BIT); }; struct Texture : Image { void Init(const Device *device, vk::Extent2D extent, vk::Format imageFormat, bool isMipMapped, cstr name = nullptr); }; struct TextureCube : Texture { void Init(const Device *device, u32 cubeSide, vk::Format imageFormat, bool isMipMapped = false, cstr name = nullptr); }; struct AttachmentImage : Image { void Init(const Device *device, vk::Extent2D extent, vk::Format imageFormat, cstr name = nullptr); }; struct DepthImage : Image { void Init(const Device *device, vk::Extent2D extent, cstr name = nullptr); }; struct StorageTexture : Texture { void Init(const Device *device, vk::Extent2D extent, vk::Format imageFormat, bool isSampled, cstr name = nullptr); }; inline bool Image::IsValid() const { return m_MipLevels_ & VALID_BIT; } inline bool Image::IsOwned() const { return m_MipLevels_ & OWNED_BIT; } inline u32 Image::GetMipLevels() const { return m_MipLevels_ & MIP_MASK; }