clean: RID and Resource creation.
This commit is contained in:
parent
751af977ac
commit
490d8b7f95
|
|
@ -61,12 +61,9 @@ bool BufferManager::isValidID( BufferID const& rid ) const
|
|||
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() )
|
||||
{
|
||||
return std::nullopt;
|
||||
}
|
||||
ASSERT( not m_freeList.empty() );
|
||||
|
||||
Buffer* bufferSlot = reinterpret_cast<Buffer*>( m_freeList.popFront() );
|
||||
++m_count;
|
||||
|
|
@ -74,7 +71,6 @@ std::optional<BufferID> BufferManager::createVertexBuffer( size_t const size )
|
|||
ASSERT( m_pRenderDevice );
|
||||
RenderDevice const& renderDevice = *m_pRenderDevice;
|
||||
|
||||
|
||||
VkBufferCreateInfo const bufferCreateInfo = {
|
||||
.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
|
||||
.pNext = nullptr,
|
||||
|
|
@ -121,15 +117,12 @@ std::optional<BufferID> BufferManager::createVertexBuffer( size_t const size )
|
|||
};
|
||||
|
||||
// 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() )
|
||||
{
|
||||
return std::nullopt;
|
||||
}
|
||||
ASSERT( not m_freeList.empty() );
|
||||
|
||||
Buffer* bufferSlot = reinterpret_cast<Buffer*>( m_freeList.popFront() );
|
||||
++m_count;
|
||||
|
|
@ -184,15 +177,12 @@ std::optional<BufferID> BufferManager::createIndexBuffer( size_t size )
|
|||
};
|
||||
|
||||
// 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() )
|
||||
{
|
||||
return std::nullopt;
|
||||
}
|
||||
ASSERT( not m_freeList.empty() );
|
||||
|
||||
Buffer* bufferSlot = reinterpret_cast<Buffer*>( m_freeList.popFront() );
|
||||
++m_count;
|
||||
|
|
@ -255,18 +245,18 @@ std::optional<BufferID> BufferManager::createStorageBuffer( size_t size )
|
|||
};
|
||||
|
||||
// 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 );
|
||||
|
||||
auto _ = std::move( rid );
|
||||
*rid = {};
|
||||
}
|
||||
|
||||
std::optional<VkBuffer> BufferManager::fetchBuffer( BufferID const& rid )
|
||||
|
|
|
|||
|
|
@ -55,11 +55,11 @@ private:
|
|||
public:
|
||||
[[nodiscard]] bool isValidID( BufferID const& rid ) const;
|
||||
|
||||
std::optional<BufferID> createVertexBuffer( size_t size );
|
||||
std::optional<BufferID> createIndexBuffer( size_t size );
|
||||
std::optional<BufferID> createStorageBuffer( size_t size );
|
||||
BufferID createVertexBuffer( size_t size );
|
||||
BufferID createIndexBuffer( size_t size );
|
||||
BufferID createStorageBuffer( size_t size );
|
||||
|
||||
void freeBuffer( BufferID&& rid );
|
||||
void freeBuffer( BufferID* rid );
|
||||
|
||||
DEPRECATE_JULY_2025
|
||||
std::optional<VkBuffer> fetchBuffer( BufferID const& rid );
|
||||
|
|
|
|||
|
|
@ -132,14 +132,14 @@ void EntityManager::destroyEntity( Entity* entity )
|
|||
for ( auto& material : entity->model.materials )
|
||||
{
|
||||
vkDestroySampler( device, Take( material.sampler ), nullptr );
|
||||
pRenderDevice->textureManager->freeTexture( std::move( material.albedoTextureID ) );
|
||||
pRenderDevice->textureManager->freeTexture( std::move( material.normalTextureID ) );
|
||||
pRenderDevice->textureManager->freeTexture( std::move( material.metalRoughTextureID ) );
|
||||
pRenderDevice->textureManager->freeTexture( std::move( material.emissiveTextureID ) );
|
||||
pRenderDevice->textureManager->freeTexture( &material.albedoTextureID );
|
||||
pRenderDevice->textureManager->freeTexture( &material.normalTextureID );
|
||||
pRenderDevice->textureManager->freeTexture( &material.metalRoughTextureID );
|
||||
pRenderDevice->textureManager->freeTexture( &material.emissiveTextureID );
|
||||
}
|
||||
|
||||
pRenderDevice->bufferManager->freeBuffer( std::move( entity->model.vertexBuffer ) );
|
||||
pRenderDevice->bufferManager->freeBuffer( std::move( entity->model.indexBuffer ) );
|
||||
pRenderDevice->bufferManager->freeBuffer( &entity->model.vertexBuffer );
|
||||
pRenderDevice->bufferManager->freeBuffer( &entity->model.indexBuffer );
|
||||
entity->model.primitives.clear();
|
||||
entity->model.materials.clear();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
#include "MiscData.h"
|
||||
|
||||
#include <array>
|
||||
#include <iso646.h>
|
||||
|
||||
#include <SDL3/SDL_log.h>
|
||||
|
||||
|
|
@ -308,15 +309,11 @@ bool MiscData::init( RenderDevice const& renderDevice )
|
|||
|
||||
// Lights
|
||||
{
|
||||
auto pointLightsValue = renderDevice.bufferManager->createStorageBuffer( 10 * sizeof( PointLight ) );
|
||||
if ( !pointLightsValue ) return false;
|
||||
pointLights = renderDevice.bufferManager->createStorageBuffer( 10 * sizeof( PointLight ) );
|
||||
if ( not pointLights ) return false;
|
||||
|
||||
pointLights = std::move( pointLightsValue.value() );
|
||||
|
||||
auto dirLightsValue = renderDevice.bufferManager->createStorageBuffer( 10 * sizeof( DirectionalLight ) );
|
||||
if ( !dirLightsValue ) return false;
|
||||
|
||||
directionalLights = std::move( dirLightsValue.value() );
|
||||
directionalLights = renderDevice.bufferManager->createStorageBuffer( 10 * sizeof( DirectionalLight ) );
|
||||
if ( not directionalLights ) return false;
|
||||
|
||||
lightData.pointLights = renderDevice.bufferManager->fetchDeviceAddress( pointLights ).value();
|
||||
lightData.directionalLights = renderDevice.bufferManager->fetchDeviceAddress( directionalLights ).value();
|
||||
|
|
@ -504,8 +501,8 @@ void MiscData::destroy( RenderDevice const& renderDevice )
|
|||
vkDestroyDescriptorPool( device, Take( descriptorPool ), nullptr );
|
||||
vmaDestroyBuffer( renderDevice.gpuAllocator, Take( cameraUniformBuffer ), Take( cameraUniformBufferAllocation ) );
|
||||
|
||||
renderDevice.bufferManager->freeBuffer( std::move( pointLights ) );
|
||||
renderDevice.bufferManager->freeBuffer( std::move( directionalLights ) );
|
||||
renderDevice.bufferManager->freeBuffer( &pointLights );
|
||||
renderDevice.bufferManager->freeBuffer( &directionalLights );
|
||||
|
||||
vkDestroyPipeline( device, Take( meshPipeline ), nullptr );
|
||||
vkDestroyPipelineLayout( device, Take( pipelineLayout ), nullptr );
|
||||
|
|
|
|||
|
|
@ -53,14 +53,13 @@ std::optional<TextureID> LoadTexture(
|
|||
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 );
|
||||
if ( not textureOpt )
|
||||
if ( not texture )
|
||||
{
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
TextureID texture = std::move( textureOpt.value() );
|
||||
VkImage textureImage = renderDevice->textureManager->fetchImage( texture ).value();
|
||||
|
||||
// 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;
|
||||
|
||||
auto baseColorTextureOpt = LoadTexture( renderDevice, sampler, *baseColorImage, false );
|
||||
auto const baseColorTextureOpt = LoadTexture( renderDevice, sampler, *baseColorImage, false );
|
||||
if ( not baseColorTextureOpt )
|
||||
{
|
||||
vkDestroySampler( renderDevice->device, Take( sampler ), nullptr );
|
||||
return UINT32_MAX;
|
||||
}
|
||||
baseColorTexture = std::move( baseColorTextureOpt.value() );
|
||||
baseColorTexture = baseColorTextureOpt.value();
|
||||
}
|
||||
|
||||
if ( material.pbr_metallic_roughness.metallic_roughness_texture.texture )
|
||||
{
|
||||
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 )
|
||||
{
|
||||
vkDestroySampler( renderDevice->device, Take( sampler ), nullptr );
|
||||
renderDevice->textureManager->freeTexture( std::move( baseColorTexture ) );
|
||||
renderDevice->textureManager->freeTexture( &baseColorTexture );
|
||||
return UINT32_MAX;
|
||||
}
|
||||
metalRoughTexture = std::move( metalRoughTextureOpt.value() );
|
||||
metalRoughTexture = metalRoughTextureOpt.value();
|
||||
}
|
||||
|
||||
if ( material.normal_texture.texture )
|
||||
{
|
||||
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 )
|
||||
{
|
||||
vkDestroySampler( renderDevice->device, Take( sampler ), nullptr );
|
||||
renderDevice->textureManager->freeTexture( std::move( metalRoughTexture ) );
|
||||
renderDevice->textureManager->freeTexture( std::move( baseColorTexture ) );
|
||||
renderDevice->textureManager->freeTexture( &metalRoughTexture );
|
||||
renderDevice->textureManager->freeTexture( &baseColorTexture );
|
||||
return UINT32_MAX;
|
||||
}
|
||||
normalTexture = std::move( normalTextureOpt.value() );
|
||||
normalTexture = normalTextureOpt.value();
|
||||
}
|
||||
|
||||
if ( material.emissive_texture.texture )
|
||||
{
|
||||
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 )
|
||||
{
|
||||
vkDestroySampler( renderDevice->device, Take( sampler ), nullptr );
|
||||
renderDevice->textureManager->freeTexture( std::move( baseColorTexture ) );
|
||||
renderDevice->textureManager->freeTexture( std::move( normalTexture ) );
|
||||
renderDevice->textureManager->freeTexture( std::move( metalRoughTexture ) );
|
||||
renderDevice->textureManager->freeTexture( &baseColorTexture );
|
||||
renderDevice->textureManager->freeTexture( &normalTexture );
|
||||
renderDevice->textureManager->freeTexture( &metalRoughTexture );
|
||||
return UINT32_MAX;
|
||||
}
|
||||
emissiveTexture = std::move( emissiveTextureOpt.value() );
|
||||
emissiveTexture = emissiveTextureOpt.value();
|
||||
}
|
||||
|
||||
float const metallic = material.pbr_metallic_roughness.metallic_factor;
|
||||
|
|
@ -503,10 +502,10 @@ uint32_t ProcessMaterial( RenderDevice* renderDevice, Model* model, cgltf_materi
|
|||
sampler,
|
||||
baseColorFactor,
|
||||
emissiveFactor,
|
||||
std::move( baseColorTexture ),
|
||||
std::move( normalTexture ),
|
||||
std::move( metalRoughTexture ),
|
||||
std::move( emissiveTexture ),
|
||||
baseColorTexture,
|
||||
normalTexture,
|
||||
metalRoughTexture,
|
||||
emissiveTexture,
|
||||
roughness,
|
||||
metallic,
|
||||
} );
|
||||
|
|
@ -783,21 +782,13 @@ Entity* LoadModel( RenderDevice* renderDevice, EntityManager* entityManager, con
|
|||
renderDevice, entityManager, &entity->model, &vertices, &indices, *currentScene->nodes[nodeIdx] ) );
|
||||
}
|
||||
|
||||
auto vertexBuffer = renderDevice->bufferManager->createVertexBuffer( vertices.size() * sizeof vertices[0] );
|
||||
if ( not vertexBuffer )
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
entity->model.vertexBuffer = std::move( vertexBuffer.value() );
|
||||
entity->model.vertexBuffer = renderDevice->bufferManager->createVertexBuffer( vertices.size() * sizeof vertices[0] );
|
||||
if ( not entity->model.vertexBuffer ) return nullptr;
|
||||
|
||||
renderDevice->bufferManager->writeToBuffer( entity->model.vertexBuffer, vertices );
|
||||
|
||||
auto indexBuffer = renderDevice->bufferManager->createIndexBuffer( indices.size() * sizeof indices[0] );
|
||||
if ( not indexBuffer )
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
entity->model.indexBuffer = std::move( indexBuffer.value() );
|
||||
entity->model.indexBuffer = renderDevice->bufferManager->createIndexBuffer( indices.size() * sizeof indices[0] );
|
||||
if ( not entity->model.indexBuffer ) return nullptr;
|
||||
|
||||
renderDevice->bufferManager->writeToBuffer( entity->model.indexBuffer, std::span{ indices } );
|
||||
|
||||
|
|
|
|||
|
|
@ -14,14 +14,6 @@ private:
|
|||
public:
|
||||
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
|
||||
{
|
||||
return m_index == 0;
|
||||
|
|
@ -37,19 +29,3 @@ public:
|
|||
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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,12 +7,9 @@
|
|||
|
||||
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() )
|
||||
{
|
||||
return std::nullopt;
|
||||
}
|
||||
ASSERT( not m_freeList.empty() );
|
||||
|
||||
Texture* textureSlot = reinterpret_cast<Texture*>( m_freeList.popFront() );
|
||||
++m_count;
|
||||
|
|
@ -122,7 +119,7 @@ std::optional<TextureID> TextureManager::createTexture( VkExtent3D const extent,
|
|||
vkUpdateDescriptorSets( renderDevice.device, 1, &descriptorWrite, 0, nullptr );
|
||||
|
||||
// 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
|
||||
|
|
@ -135,15 +132,15 @@ bool TextureManager::isValidID( TextureID const& rid ) const
|
|||
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 );
|
||||
|
||||
auto _ = std::move( rid );
|
||||
*rid = {};
|
||||
}
|
||||
|
||||
std::optional<VkImage> TextureManager::fetchImage( TextureID const& rid )
|
||||
|
|
|
|||
|
|
@ -64,10 +64,10 @@ public:
|
|||
|
||||
// [[nodiscard]] std::optional<TextureID> createTexture( VkExtent3D extent );
|
||||
|
||||
void freeTexture( TextureID&& rid );
|
||||
void freeTexture( TextureID* rid );
|
||||
|
||||
DEPRECATE_JULY_2025
|
||||
[[nodiscard]] std::optional<TextureID> createTexture(
|
||||
[[nodiscard]] TextureID createTexture(
|
||||
VkExtent3D extent, VkSampler sampler, VkFormat format = VK_FORMAT_R8G8B8A8_SRGB );
|
||||
|
||||
DEPRECATE_JULY_2025
|
||||
|
|
|
|||
Loading…
Reference in New Issue