project-aster/aster/image.h

82 lines
1.9 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::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`.
u32 m_MipLevels_;
[[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 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);
};
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;
}