project-aster/aster/image.h

119 lines
2.8 KiB
C++

// =============================================
// 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<i32>(extent.width), Cast<i32>(extent.height)};
}
[[nodiscard]] inline vk::Offset3D
ToOffset3D(const vk::Extent3D &extent)
{
return {Cast<i32>(extent.width), Cast<i32>(extent.height), Cast<i32>(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`.
u8 m_EmptyPadding_ = 0;
u8 m_Flags_ = 0;
u8 m_LayerCount = 0;
u8 m_MipLevels = 0;
[[nodiscard]] bool IsValid() const;
[[nodiscard]] bool IsOwned() const;
[[nodiscard]] u32 GetMipLevels() const;
void Destroy(const Device *device);
constexpr static u8 VALID_BIT = 1u << 7;
constexpr static u8 OWNED_BIT = 1u << 6;
};
struct Texture : Image
{
void Init(const Device *device, vk::Extent2D extent, vk::Format imageFormat, bool isMipMapped, cstr name = nullptr);
};
static_assert(sizeof(Texture) == sizeof(Image));
struct TextureCube : Texture
{
void
Init(const Device *device, u32 cubeSide, vk::Format imageFormat, bool isMipMapped = false, cstr name = nullptr);
};
static_assert(sizeof(TextureCube) == sizeof(Image));
struct AttachmentImage : Image
{
void Init(const Device *device, vk::Extent2D extent, vk::Format imageFormat, cstr name = nullptr);
};
static_assert(sizeof(AttachmentImage) == sizeof(Image));
struct DepthImage : Image
{
void Init(const Device *device, vk::Extent2D extent, cstr name = nullptr);
};
static_assert(sizeof(DepthImage) == sizeof(Image));
struct StorageTexture : Texture
{
void Init(const Device *device, vk::Extent2D extent, vk::Format imageFormat, bool isSampled, cstr name = nullptr);
};
static_assert(sizeof(StorageTexture) == sizeof(Image));
struct StorageTextureCube : StorageTexture
{
void Init(const Device *device, u32 cubeSide, vk::Format imageFormat, bool isSampled, bool isMipMapped = false,
cstr name = nullptr);
};
static_assert(sizeof(StorageTextureCube) == sizeof(Image));
inline bool
Image::IsValid() const
{
return m_Flags_ & VALID_BIT;
}
inline bool
Image::IsOwned() const
{
return m_Flags_ & OWNED_BIT;
}
inline u32
Image::GetMipLevels() const
{
return m_MipLevels;
}