clean: RID and Resource creation.

This commit is contained in:
Anish Bhobe 2025-07-02 13:11:32 +02:00
parent 751af977ac
commit 490d8b7f95
8 changed files with 65 additions and 114 deletions

View File

@ -61,12 +61,9 @@ bool BufferManager::isValidID( BufferID const& rid ) const
return m_aBuffers[innerIndex].index == index; return m_aBuffers[innerIndex].index == index;
} }
std::optional<BufferID> BufferManager::createVertexBuffer( size_t const size ) BufferID BufferManager::createVertexBuffer( size_t const size )
{ {
if ( m_freeList.empty() ) ASSERT( not m_freeList.empty() );
{
return std::nullopt;
}
Buffer* bufferSlot = reinterpret_cast<Buffer*>( m_freeList.popFront() ); Buffer* bufferSlot = reinterpret_cast<Buffer*>( m_freeList.popFront() );
++m_count; ++m_count;
@ -74,7 +71,6 @@ std::optional<BufferID> BufferManager::createVertexBuffer( size_t const size )
ASSERT( m_pRenderDevice ); ASSERT( m_pRenderDevice );
RenderDevice const& renderDevice = *m_pRenderDevice; RenderDevice const& renderDevice = *m_pRenderDevice;
VkBufferCreateInfo const bufferCreateInfo = { VkBufferCreateInfo const bufferCreateInfo = {
.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, .sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
.pNext = nullptr, .pNext = nullptr,
@ -121,15 +117,12 @@ std::optional<BufferID> BufferManager::createVertexBuffer( size_t const size )
}; };
// NOTE: Memory hackery to create TextureID; // NOTE: Memory hackery to create TextureID;
return std::move( *reinterpret_cast<BufferID*>( &index ) ); return *reinterpret_cast<BufferID*>( &index );
} }
std::optional<BufferID> BufferManager::createIndexBuffer( size_t size ) BufferID BufferManager::createIndexBuffer( size_t const size )
{ {
if ( m_freeList.empty() ) ASSERT( not m_freeList.empty() );
{
return std::nullopt;
}
Buffer* bufferSlot = reinterpret_cast<Buffer*>( m_freeList.popFront() ); Buffer* bufferSlot = reinterpret_cast<Buffer*>( m_freeList.popFront() );
++m_count; ++m_count;
@ -184,15 +177,12 @@ std::optional<BufferID> BufferManager::createIndexBuffer( size_t size )
}; };
// NOTE: Memory hackery to create BufferID; // NOTE: Memory hackery to create BufferID;
return std::move( *reinterpret_cast<BufferID*>( &index ) ); return *reinterpret_cast<BufferID*>( &index );
} }
std::optional<BufferID> BufferManager::createStorageBuffer( size_t size ) BufferID BufferManager::createStorageBuffer( size_t const size )
{ {
if ( m_freeList.empty() ) ASSERT( not m_freeList.empty() );
{
return std::nullopt;
}
Buffer* bufferSlot = reinterpret_cast<Buffer*>( m_freeList.popFront() ); Buffer* bufferSlot = reinterpret_cast<Buffer*>( m_freeList.popFront() );
++m_count; ++m_count;
@ -255,18 +245,18 @@ std::optional<BufferID> BufferManager::createStorageBuffer( size_t size )
}; };
// NOTE: Memory hackery to create BufferID; // NOTE: Memory hackery to create BufferID;
return std::move( *reinterpret_cast<BufferID*>( &index ) ); return *reinterpret_cast<BufferID*>( &index );
} }
void BufferManager::freeBuffer( BufferID&& rid ) void BufferManager::freeBuffer( BufferID* rid )
{ {
if ( not isValidID( rid ) ) return; if ( not isValidID( *rid ) ) return;
Buffer& buffer = fetchBufferUnchecked( rid ); Buffer& buffer = fetchBufferUnchecked( *rid );
destroyBuffer( buffer ); destroyBuffer( buffer );
auto _ = std::move( rid ); *rid = {};
} }
std::optional<VkBuffer> BufferManager::fetchBuffer( BufferID const& rid ) std::optional<VkBuffer> BufferManager::fetchBuffer( BufferID const& rid )

View File

@ -55,11 +55,11 @@ private:
public: public:
[[nodiscard]] bool isValidID( BufferID const& rid ) const; [[nodiscard]] bool isValidID( BufferID const& rid ) const;
std::optional<BufferID> createVertexBuffer( size_t size ); BufferID createVertexBuffer( size_t size );
std::optional<BufferID> createIndexBuffer( size_t size ); BufferID createIndexBuffer( size_t size );
std::optional<BufferID> createStorageBuffer( size_t size ); BufferID createStorageBuffer( size_t size );
void freeBuffer( BufferID&& rid ); void freeBuffer( BufferID* rid );
DEPRECATE_JULY_2025 DEPRECATE_JULY_2025
std::optional<VkBuffer> fetchBuffer( BufferID const& rid ); std::optional<VkBuffer> fetchBuffer( BufferID const& rid );

View File

@ -132,14 +132,14 @@ void EntityManager::destroyEntity( Entity* entity )
for ( auto& material : entity->model.materials ) for ( auto& material : entity->model.materials )
{ {
vkDestroySampler( device, Take( material.sampler ), nullptr ); vkDestroySampler( device, Take( material.sampler ), nullptr );
pRenderDevice->textureManager->freeTexture( std::move( material.albedoTextureID ) ); pRenderDevice->textureManager->freeTexture( &material.albedoTextureID );
pRenderDevice->textureManager->freeTexture( std::move( material.normalTextureID ) ); pRenderDevice->textureManager->freeTexture( &material.normalTextureID );
pRenderDevice->textureManager->freeTexture( std::move( material.metalRoughTextureID ) ); pRenderDevice->textureManager->freeTexture( &material.metalRoughTextureID );
pRenderDevice->textureManager->freeTexture( std::move( material.emissiveTextureID ) ); pRenderDevice->textureManager->freeTexture( &material.emissiveTextureID );
} }
pRenderDevice->bufferManager->freeBuffer( std::move( entity->model.vertexBuffer ) ); pRenderDevice->bufferManager->freeBuffer( &entity->model.vertexBuffer );
pRenderDevice->bufferManager->freeBuffer( std::move( entity->model.indexBuffer ) ); pRenderDevice->bufferManager->freeBuffer( &entity->model.indexBuffer );
entity->model.primitives.clear(); entity->model.primitives.clear();
entity->model.materials.clear(); entity->model.materials.clear();
} }

View File

@ -1,6 +1,7 @@
#include "MiscData.h" #include "MiscData.h"
#include <array> #include <array>
#include <iso646.h>
#include <SDL3/SDL_log.h> #include <SDL3/SDL_log.h>
@ -308,15 +309,11 @@ bool MiscData::init( RenderDevice const& renderDevice )
// Lights // Lights
{ {
auto pointLightsValue = renderDevice.bufferManager->createStorageBuffer( 10 * sizeof( PointLight ) ); pointLights = renderDevice.bufferManager->createStorageBuffer( 10 * sizeof( PointLight ) );
if ( !pointLightsValue ) return false; if ( not pointLights ) return false;
pointLights = std::move( pointLightsValue.value() ); directionalLights = renderDevice.bufferManager->createStorageBuffer( 10 * sizeof( DirectionalLight ) );
if ( not directionalLights ) return false;
auto dirLightsValue = renderDevice.bufferManager->createStorageBuffer( 10 * sizeof( DirectionalLight ) );
if ( !dirLightsValue ) return false;
directionalLights = std::move( dirLightsValue.value() );
lightData.pointLights = renderDevice.bufferManager->fetchDeviceAddress( pointLights ).value(); lightData.pointLights = renderDevice.bufferManager->fetchDeviceAddress( pointLights ).value();
lightData.directionalLights = renderDevice.bufferManager->fetchDeviceAddress( directionalLights ).value(); lightData.directionalLights = renderDevice.bufferManager->fetchDeviceAddress( directionalLights ).value();
@ -504,8 +501,8 @@ void MiscData::destroy( RenderDevice const& renderDevice )
vkDestroyDescriptorPool( device, Take( descriptorPool ), nullptr ); vkDestroyDescriptorPool( device, Take( descriptorPool ), nullptr );
vmaDestroyBuffer( renderDevice.gpuAllocator, Take( cameraUniformBuffer ), Take( cameraUniformBufferAllocation ) ); vmaDestroyBuffer( renderDevice.gpuAllocator, Take( cameraUniformBuffer ), Take( cameraUniformBufferAllocation ) );
renderDevice.bufferManager->freeBuffer( std::move( pointLights ) ); renderDevice.bufferManager->freeBuffer( &pointLights );
renderDevice.bufferManager->freeBuffer( std::move( directionalLights ) ); renderDevice.bufferManager->freeBuffer( &directionalLights );
vkDestroyPipeline( device, Take( meshPipeline ), nullptr ); vkDestroyPipeline( device, Take( meshPipeline ), nullptr );
vkDestroyPipelineLayout( device, Take( pipelineLayout ), nullptr ); vkDestroyPipelineLayout( device, Take( pipelineLayout ), nullptr );

View File

@ -53,14 +53,13 @@ std::optional<TextureID> LoadTexture(
height = static_cast<uint32_t>( h ); height = static_cast<uint32_t>( h );
} }
auto textureOpt = renderDevice->textureManager->createTexture( TextureID texture = renderDevice->textureManager->createTexture(
{ width, height, 1 }, sampler, linear ? VK_FORMAT_R8G8B8A8_UNORM : VK_FORMAT_R8G8B8A8_SRGB ); { width, height, 1 }, sampler, linear ? VK_FORMAT_R8G8B8A8_UNORM : VK_FORMAT_R8G8B8A8_SRGB );
if ( not textureOpt ) if ( not texture )
{ {
return std::nullopt; return std::nullopt;
} }
TextureID texture = std::move( textureOpt.value() );
VkImage textureImage = renderDevice->textureManager->fetchImage( texture ).value(); VkImage textureImage = renderDevice->textureManager->fetchImage( texture ).value();
// Staging Buffer Create // Staging Buffer Create
@ -441,58 +440,58 @@ uint32_t ProcessMaterial( RenderDevice* renderDevice, Model* model, cgltf_materi
{ {
cgltf_image const* baseColorImage = material.pbr_metallic_roughness.base_color_texture.texture->image; cgltf_image const* baseColorImage = material.pbr_metallic_roughness.base_color_texture.texture->image;
auto baseColorTextureOpt = LoadTexture( renderDevice, sampler, *baseColorImage, false ); auto const baseColorTextureOpt = LoadTexture( renderDevice, sampler, *baseColorImage, false );
if ( not baseColorTextureOpt ) if ( not baseColorTextureOpt )
{ {
vkDestroySampler( renderDevice->device, Take( sampler ), nullptr ); vkDestroySampler( renderDevice->device, Take( sampler ), nullptr );
return UINT32_MAX; return UINT32_MAX;
} }
baseColorTexture = std::move( baseColorTextureOpt.value() ); baseColorTexture = baseColorTextureOpt.value();
} }
if ( material.pbr_metallic_roughness.metallic_roughness_texture.texture ) if ( material.pbr_metallic_roughness.metallic_roughness_texture.texture )
{ {
cgltf_image const* metalRoughImage = material.pbr_metallic_roughness.metallic_roughness_texture.texture->image; cgltf_image const* metalRoughImage = material.pbr_metallic_roughness.metallic_roughness_texture.texture->image;
auto metalRoughTextureOpt = LoadTexture( renderDevice, sampler, *metalRoughImage, true ); auto const metalRoughTextureOpt = LoadTexture( renderDevice, sampler, *metalRoughImage, true );
if ( not metalRoughTextureOpt ) if ( not metalRoughTextureOpt )
{ {
vkDestroySampler( renderDevice->device, Take( sampler ), nullptr ); vkDestroySampler( renderDevice->device, Take( sampler ), nullptr );
renderDevice->textureManager->freeTexture( std::move( baseColorTexture ) ); renderDevice->textureManager->freeTexture( &baseColorTexture );
return UINT32_MAX; return UINT32_MAX;
} }
metalRoughTexture = std::move( metalRoughTextureOpt.value() ); metalRoughTexture = metalRoughTextureOpt.value();
} }
if ( material.normal_texture.texture ) if ( material.normal_texture.texture )
{ {
cgltf_image const* normalImage = material.normal_texture.texture->image; cgltf_image const* normalImage = material.normal_texture.texture->image;
auto normalTextureOpt = LoadTexture( renderDevice, sampler, *normalImage, true ); auto const normalTextureOpt = LoadTexture( renderDevice, sampler, *normalImage, true );
if ( not normalTextureOpt ) if ( not normalTextureOpt )
{ {
vkDestroySampler( renderDevice->device, Take( sampler ), nullptr ); vkDestroySampler( renderDevice->device, Take( sampler ), nullptr );
renderDevice->textureManager->freeTexture( std::move( metalRoughTexture ) ); renderDevice->textureManager->freeTexture( &metalRoughTexture );
renderDevice->textureManager->freeTexture( std::move( baseColorTexture ) ); renderDevice->textureManager->freeTexture( &baseColorTexture );
return UINT32_MAX; return UINT32_MAX;
} }
normalTexture = std::move( normalTextureOpt.value() ); normalTexture = normalTextureOpt.value();
} }
if ( material.emissive_texture.texture ) if ( material.emissive_texture.texture )
{ {
cgltf_image const* emissiveImage = material.emissive_texture.texture->image; cgltf_image const* emissiveImage = material.emissive_texture.texture->image;
auto emissiveTextureOpt = LoadTexture( renderDevice, sampler, *emissiveImage, true ); auto const emissiveTextureOpt = LoadTexture( renderDevice, sampler, *emissiveImage, true );
if ( not emissiveTextureOpt ) if ( not emissiveTextureOpt )
{ {
vkDestroySampler( renderDevice->device, Take( sampler ), nullptr ); vkDestroySampler( renderDevice->device, Take( sampler ), nullptr );
renderDevice->textureManager->freeTexture( std::move( baseColorTexture ) ); renderDevice->textureManager->freeTexture( &baseColorTexture );
renderDevice->textureManager->freeTexture( std::move( normalTexture ) ); renderDevice->textureManager->freeTexture( &normalTexture );
renderDevice->textureManager->freeTexture( std::move( metalRoughTexture ) ); renderDevice->textureManager->freeTexture( &metalRoughTexture );
return UINT32_MAX; return UINT32_MAX;
} }
emissiveTexture = std::move( emissiveTextureOpt.value() ); emissiveTexture = emissiveTextureOpt.value();
} }
float const metallic = material.pbr_metallic_roughness.metallic_factor; float const metallic = material.pbr_metallic_roughness.metallic_factor;
@ -503,10 +502,10 @@ uint32_t ProcessMaterial( RenderDevice* renderDevice, Model* model, cgltf_materi
sampler, sampler,
baseColorFactor, baseColorFactor,
emissiveFactor, emissiveFactor,
std::move( baseColorTexture ), baseColorTexture,
std::move( normalTexture ), normalTexture,
std::move( metalRoughTexture ), metalRoughTexture,
std::move( emissiveTexture ), emissiveTexture,
roughness, roughness,
metallic, metallic,
} ); } );
@ -783,21 +782,13 @@ Entity* LoadModel( RenderDevice* renderDevice, EntityManager* entityManager, con
renderDevice, entityManager, &entity->model, &vertices, &indices, *currentScene->nodes[nodeIdx] ) ); renderDevice, entityManager, &entity->model, &vertices, &indices, *currentScene->nodes[nodeIdx] ) );
} }
auto vertexBuffer = renderDevice->bufferManager->createVertexBuffer( vertices.size() * sizeof vertices[0] ); entity->model.vertexBuffer = renderDevice->bufferManager->createVertexBuffer( vertices.size() * sizeof vertices[0] );
if ( not vertexBuffer ) if ( not entity->model.vertexBuffer ) return nullptr;
{
return nullptr;
}
entity->model.vertexBuffer = std::move( vertexBuffer.value() );
renderDevice->bufferManager->writeToBuffer( entity->model.vertexBuffer, vertices ); renderDevice->bufferManager->writeToBuffer( entity->model.vertexBuffer, vertices );
auto indexBuffer = renderDevice->bufferManager->createIndexBuffer( indices.size() * sizeof indices[0] ); entity->model.indexBuffer = renderDevice->bufferManager->createIndexBuffer( indices.size() * sizeof indices[0] );
if ( not indexBuffer ) if ( not entity->model.indexBuffer ) return nullptr;
{
return nullptr;
}
entity->model.indexBuffer = std::move( indexBuffer.value() );
renderDevice->bufferManager->writeToBuffer( entity->model.indexBuffer, std::span{ indices } ); renderDevice->bufferManager->writeToBuffer( entity->model.indexBuffer, std::span{ indices } );

View File

@ -14,14 +14,6 @@ private:
public: public:
RID() = default; RID() = default;
// No copy
RID( RID const& ) = delete;
RID& operator=( RID const& ) = delete;
// Move allowed
RID( RID&& other ) noexcept;
RID& operator=( RID&& other ) noexcept;
[[nodiscard]] bool isNull() const [[nodiscard]] bool isNull() const
{ {
return m_index == 0; return m_index == 0;
@ -37,19 +29,3 @@ public:
return m_index != 0; return m_index != 0;
} }
}; };
template <typename T>
RID<T>::RID( RID&& other ) noexcept : m_index{ other.m_index }
{
other.m_index = 0;
}
template <typename T>
RID<T>& RID<T>::operator=( RID&& other ) noexcept
{
if ( this == &other ) return *this;
m_index = other.m_index;
other.m_index = 0;
return *this;
}

View File

@ -7,12 +7,9 @@
template struct RID<Texture>; template struct RID<Texture>;
std::optional<TextureID> TextureManager::createTexture( VkExtent3D const extent, VkSampler const sampler, VkFormat const format ) TextureID TextureManager::createTexture( VkExtent3D const extent, VkSampler const sampler, VkFormat const format )
{ {
if ( m_freeList.empty() ) ASSERT( not m_freeList.empty() );
{
return std::nullopt;
}
Texture* textureSlot = reinterpret_cast<Texture*>( m_freeList.popFront() ); Texture* textureSlot = reinterpret_cast<Texture*>( m_freeList.popFront() );
++m_count; ++m_count;
@ -122,7 +119,7 @@ std::optional<TextureID> TextureManager::createTexture( VkExtent3D const extent,
vkUpdateDescriptorSets( renderDevice.device, 1, &descriptorWrite, 0, nullptr ); vkUpdateDescriptorSets( renderDevice.device, 1, &descriptorWrite, 0, nullptr );
// NOTE: Memory hackery to create TextureID; // NOTE: Memory hackery to create TextureID;
return std::move( *reinterpret_cast<TextureID*>( &index ) ); return *reinterpret_cast<TextureID*>( &index );
} }
bool TextureManager::isValidID( TextureID const& rid ) const bool TextureManager::isValidID( TextureID const& rid ) const
@ -135,15 +132,15 @@ bool TextureManager::isValidID( TextureID const& rid ) const
return m_aTextures[innerIndex].index == index; return m_aTextures[innerIndex].index == index;
} }
void TextureManager::freeTexture( TextureID&& rid ) void TextureManager::freeTexture( TextureID* rid )
{ {
if ( not isValidID( rid ) ) return; if ( not isValidID( *rid ) ) return;
Texture& texture = fetchTextureUnchecked( rid ); Texture& texture = fetchTextureUnchecked( *rid );
destroyTexture( texture ); destroyTexture( texture );
auto _ = std::move( rid ); *rid = {};
} }
std::optional<VkImage> TextureManager::fetchImage( TextureID const& rid ) std::optional<VkImage> TextureManager::fetchImage( TextureID const& rid )

View File

@ -64,10 +64,10 @@ public:
// [[nodiscard]] std::optional<TextureID> createTexture( VkExtent3D extent ); // [[nodiscard]] std::optional<TextureID> createTexture( VkExtent3D extent );
void freeTexture( TextureID&& rid ); void freeTexture( TextureID* rid );
DEPRECATE_JULY_2025 DEPRECATE_JULY_2025
[[nodiscard]] std::optional<TextureID> createTexture( [[nodiscard]] TextureID createTexture(
VkExtent3D extent, VkSampler sampler, VkFormat format = VK_FORMAT_R8G8B8A8_SRGB ); VkExtent3D extent, VkSampler sampler, VkFormat format = VK_FORMAT_R8G8B8A8_SRGB );
DEPRECATE_JULY_2025 DEPRECATE_JULY_2025