Blaze/Blaze/Source/TextureManager.h

100 lines
2.9 KiB
C++

#pragma once
#include <optional>
#include <span>
#include "FreeList.h"
#include "MacroUtils.h"
#include "RID.h"
#include "RenderDevice.h"
#include "VulkanHeader.h"
struct GlobalMemory;
struct RenderDevice;
struct Texture
{
VkImage image;
VmaAllocation allocation;
VkImageView view;
VkExtent3D extent;
VkFormat format;
uint32_t index;
};
static_assert( sizeof( Texture ) > sizeof( FreeList::Node ) and "Texture is used intrusively by FreeList" );
static_assert(
offsetof( Texture, index ) >= sizeof( FreeList::Node ) and
"Index should not be overwritten even in invalid state" );
extern template struct RID<Texture>;
using TextureID = RID<Texture>;
struct TextureManager
{
private:
constexpr static uint32_t INDEX_MASK = 0x0007FFFF;
constexpr static uint32_t GENERATION_MASK = ~INDEX_MASK;
constexpr static uint32_t GENERATION_OFFSET = 19;
static_assert(
( ( GENERATION_MASK >> GENERATION_OFFSET & 0x1 ) == 0x1 ) and
( ( GENERATION_MASK >> ( GENERATION_OFFSET - 1 ) & 0x1 ) != 0x1 ) and "Checks boundary" );
RenderDevice* m_pRenderDevice;
// Texture Manager
Texture* m_aTextures;
uint32_t m_count;
uint32_t m_capacity;
FreeList m_freeList;
// Bindless Descriptor Info
VkDescriptorSetLayout m_descriptorSetLayout;
VkDescriptorPool m_descriptorPool;
VkDescriptorSet m_descriptorSet;
void destroyTexture( Texture& tex );
Texture& fetchTextureUnchecked( TextureID const& rid );
public:
static uint32_t calculateRequiredMipLevels( uint32_t w, uint32_t h, uint32_t d );
[[nodiscard]] bool isValidID( TextureID const& rid ) const;
// [[nodiscard]] std::optional<TextureID> createTexture( VkExtent3D extent );
void freeTexture( TextureID* rid );
DEPRECATE_JULY_2025
[[nodiscard]] TextureID createTexture(
VkExtent3D extent, VkSampler sampler, VkFormat format = VK_FORMAT_R8G8B8A8_SRGB );
DEPRECATE_JULY_2025
std::optional<VkImage> fetchImage( TextureID const& rid );
DEPRECATE_JULY_2025
std::optional<VkImageView> fetchImageView( TextureID const& rid );
[[nodiscard]] VkDescriptorSetLayout const& descriptorLayout() const;
[[nodiscard]] VkDescriptorSet const& descriptorSet() const;
//
TextureManager(
RenderDevice* pRenderDevice,
Texture* aTextures,
uint32_t capacity,
VkDescriptorSetLayout setLayout,
VkDescriptorPool pool,
VkDescriptorSet descriptorSet );
void destroy();
TextureManager( TextureManager const& other ) = delete;
TextureManager( TextureManager&& other ) noexcept = delete;
TextureManager& operator=( TextureManager const& other ) = delete;
TextureManager& operator=( TextureManager&& other ) noexcept = delete;
~TextureManager();
};
TextureManager* TextureManager_Create( GlobalMemory* mem, RenderDevice* renderDevice, uint32_t maxCount );