From 09d9662136b1650554a3aa9b5c0a295deaa9c8ea Mon Sep 17 00:00:00 2001 From: Sultim Tsyrendashiev Date: Sun, 12 Nov 2023 20:45:01 +0000 Subject: [PATCH] Move vertex count allocation constants to rgCreate --- Include/RTGL1/RTGL1.h | 7 ++++ Source/ASManager.cpp | 52 ++++++++++++------------ Source/ASManager.h | 2 + Source/Generated/GenerateShaderCommon.py | 4 -- Source/Generated/ShaderCommonC.h | 3 -- Source/Generated/ShaderCommonGLSL.h | 3 -- Source/Scene.cpp | 4 ++ Source/Scene.h | 2 + Source/VertexCollector.cpp | 25 ++++-------- Source/VertexCollector.h | 25 ++++++++---- Source/VulkanDevice_Init.cpp | 2 + 11 files changed, 69 insertions(+), 60 deletions(-) diff --git a/Include/RTGL1/RTGL1.h b/Include/RTGL1/RTGL1.h index 329d4c4c..4b2865b0 100644 --- a/Include/RTGL1/RTGL1.h +++ b/Include/RTGL1/RTGL1.h @@ -249,6 +249,13 @@ typedef struct RgInstanceCreateInfo uint32_t primaryRaysMaxAlbedoLayers; uint32_t indirectIlluminationMaxAlbedoLayers; + // How many vertices to allocate for static and replacements (load once) geometry. + // Bytes allocated in VRAM: 2 * replacementsMaxVertexCount * sizeof(RgPrimitiveVertex) + uint64_t replacementsMaxVertexCount; + // How many vertices to allocate for dynamic (load each frame) geometry. + // Bytes allocated in VRAM: 3 * dynamicMaxVertexCount * sizeof(RgPrimitiveVertex) + uint64_t dynamicMaxVertexCount; + RgBool32 rayCullBackFacingTriangles; // Allow RG_GEOMETRY_VISIBILITY_TYPE_SKY. // If true, RG_GEOMETRY_VISIBILITY_TYPE_WORLD_2 must not be used. diff --git a/Source/ASManager.cpp b/Source/ASManager.cpp index 179bb558..0b4d08d4 100644 --- a/Source/ASManager.cpp +++ b/Source/ASManager.cpp @@ -54,6 +54,8 @@ RTGL1::ASManager::ASManager( VkDevice _device, std::shared_ptr< MemoryAllocator > _allocator, std::shared_ptr< CommandBufferManager > _cmdManager, std::shared_ptr< GeomInfoManager > _geomInfoManager, + uint64_t _maxReplacementsVerts, + uint64_t _maxDynamicVerts, bool _enableTexCoordLayer1, bool _enableTexCoordLayer2, bool _enableTexCoordLayer3 ) @@ -102,48 +104,48 @@ RTGL1::ASManager::ASManager( VkDevice _device, allocator, usage, asAlignment, "TLAS common buffer" ); } + _maxReplacementsVerts = _maxReplacementsVerts > 0 ? _maxReplacementsVerts : 2097152; { - const uint32_t maxVertsPerLayer[] = { - MAX_STATIC_VERTEX_COUNT, - _enableTexCoordLayer1 ? uint32_t{ MAX_STATIC_VERTEX_COUNT } : 0, - _enableTexCoordLayer2 ? uint32_t{ MAX_STATIC_VERTEX_COUNT } : 0, - _enableTexCoordLayer3 ? uint32_t{ MAX_STATIC_VERTEX_COUNT } : 0, + const size_t maxVertsPerLayer[] = { + _maxReplacementsVerts, + _enableTexCoordLayer1 ? _maxReplacementsVerts : 0, + _enableTexCoordLayer2 ? _maxReplacementsVerts : 0, + _enableTexCoordLayer3 ? _maxReplacementsVerts : 0, }; + const size_t maxIndices = _maxReplacementsVerts * 3; collectorStatic = std::make_unique< VertexCollector >( - device, *allocator, maxVertsPerLayer, false, "Static" ); + device, *allocator, maxVertsPerLayer, maxIndices, false, "Static" ); } + + _maxDynamicVerts = _maxDynamicVerts > 0 ? _maxDynamicVerts : 2097152; { - const uint32_t maxVertsPerLayer[] = { - MAX_DYNAMIC_VERTEX_COUNT, - _enableTexCoordLayer1 ? uint32_t{ MAX_DYNAMIC_VERTEX_COUNT } : 0, - _enableTexCoordLayer2 ? uint32_t{ MAX_DYNAMIC_VERTEX_COUNT } : 0, - _enableTexCoordLayer3 ? uint32_t{ MAX_DYNAMIC_VERTEX_COUNT } : 0, + const size_t maxVertsPerLayer[] = { + _maxDynamicVerts, + _enableTexCoordLayer1 ? _maxDynamicVerts : 0, + _enableTexCoordLayer2 ? _maxDynamicVerts : 0, + _enableTexCoordLayer3 ? _maxDynamicVerts : 0, }; + const size_t maxIndices = _maxDynamicVerts * 3; - for( auto& c : collectorDynamic ) - { - if( !collectorDynamic[ 0 ] ) - { - c = std::make_unique< VertexCollector >( - device, *allocator, maxVertsPerLayer, true, "Dynamic" ); - continue; - } + collectorDynamic[ 0 ] = std::make_unique< VertexCollector >( + device, *allocator, maxVertsPerLayer, maxIndices, true, "Dynamic 0" ); - // share buffers with 0 - c = VertexCollector::CreateWithSameDeviceLocalBuffers( - *( collectorDynamic[ 0 ] ), *allocator, "Dynamic" ); - } + // share device-local buffer with 0 + collectorDynamic[ 1 ] = VertexCollector::CreateWithSameDeviceLocalBuffers( + *( collectorDynamic[ 0 ] ), *allocator, "Dynamic 1" ); + + assert( std::size( collectorDynamic ) == 2 ); } previousDynamicPositions.Init( *allocator, - MAX_DYNAMIC_VERTEX_COUNT * sizeof( ShVertex ), + _maxDynamicVerts * sizeof( ShVertex ), VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, "Previous frame's vertex data" ); previousDynamicIndices.Init( *allocator, - MAX_DYNAMIC_VERTEX_COUNT * sizeof( uint32_t ), + _maxDynamicVerts * sizeof( uint32_t ), VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, diff --git a/Source/ASManager.h b/Source/ASManager.h index 4384d798..c40b3eb1 100644 --- a/Source/ASManager.h +++ b/Source/ASManager.h @@ -41,6 +41,8 @@ class ASManager std::shared_ptr< MemoryAllocator > allocator, std::shared_ptr< CommandBufferManager > cmdManager, std::shared_ptr< GeomInfoManager > geomInfoManager, + uint64_t maxReplacementsVerts, + uint64_t maxDynamicVerts, bool enableTexCoordLayer1, bool enableTexCoordLayer2, bool enableTexCoordLayer3 ); diff --git a/Source/Generated/GenerateShaderCommon.py b/Source/Generated/GenerateShaderCommon.py index aaac6265..8b80a760 100644 --- a/Source/Generated/GenerateShaderCommon.py +++ b/Source/Generated/GenerateShaderCommon.py @@ -225,10 +225,6 @@ def BIT( i ): return "1 << " + str( i ) CONST = { - "MAX_STATIC_VERTEX_COUNT" : 1 << 20, - "MAX_DYNAMIC_VERTEX_COUNT" : 1 << 21, - "MAX_INDEXED_PRIMITIVE_COUNT" : 1 << 20, - "MAX_INSTANCE_COUNT" : 1 << 16, "MAX_GEOM_INFO_COUNT" : 1 << 17, diff --git a/Source/Generated/ShaderCommonC.h b/Source/Generated/ShaderCommonC.h index f66f9ffe..7a8d5f96 100644 --- a/Source/Generated/ShaderCommonC.h +++ b/Source/Generated/ShaderCommonC.h @@ -7,9 +7,6 @@ namespace RTGL1 #include -#define MAX_STATIC_VERTEX_COUNT (1048576) -#define MAX_DYNAMIC_VERTEX_COUNT (2097152) -#define MAX_INDEXED_PRIMITIVE_COUNT (1048576) #define MAX_INSTANCE_COUNT (65536) #define MAX_GEOM_INFO_COUNT (131072) #define BINDING_VERTEX_BUFFER_STATIC (0) diff --git a/Source/Generated/ShaderCommonGLSL.h b/Source/Generated/ShaderCommonGLSL.h index 628633bc..f1936e80 100644 --- a/Source/Generated/ShaderCommonGLSL.h +++ b/Source/Generated/ShaderCommonGLSL.h @@ -1,8 +1,5 @@ // This file was generated by GenerateShaderCommon.py -#define MAX_STATIC_VERTEX_COUNT (1048576) -#define MAX_DYNAMIC_VERTEX_COUNT (2097152) -#define MAX_INDEXED_PRIMITIVE_COUNT (1048576) #define MAX_INSTANCE_COUNT (65536) #define MAX_GEOM_INFO_COUNT (131072) #define BINDING_VERTEX_BUFFER_STATIC (0) diff --git a/Source/Scene.cpp b/Source/Scene.cpp index 55680416..1a22a71d 100644 --- a/Source/Scene.cpp +++ b/Source/Scene.cpp @@ -143,6 +143,8 @@ RTGL1::Scene::Scene( VkDevice _device, std::shared_ptr< CommandBufferManager > _cmdManager, const GlobalUniform& _uniform, const ShaderManager& _shaderManager, + uint64_t _maxReplacementsVerts, + uint64_t _maxDynamicVerts, bool _enableTexCoordLayer1, bool _enableTexCoordLayer2, bool _enableTexCoordLayer3 ) @@ -154,6 +156,8 @@ RTGL1::Scene::Scene( VkDevice _device, _allocator, std::move( _cmdManager ), geomInfoMgr, + _maxReplacementsVerts, + _maxDynamicVerts, _enableTexCoordLayer1, _enableTexCoordLayer2, _enableTexCoordLayer3 ); diff --git a/Source/Scene.h b/Source/Scene.h index ffe574f3..7fb6b299 100644 --- a/Source/Scene.h +++ b/Source/Scene.h @@ -49,6 +49,8 @@ class Scene std::shared_ptr< CommandBufferManager > cmdManager, const GlobalUniform& uniform, const ShaderManager& shaderManager, + uint64_t maxReplacementsVerts, + uint64_t maxDynamicVerts, bool enableTexCoordLayer1, bool enableTexCoordLayer2, bool enableTexCoordLayer3 ); diff --git a/Source/VertexCollector.cpp b/Source/VertexCollector.cpp index 87bae7c2..35344e40 100644 --- a/Source/VertexCollector.cpp +++ b/Source/VertexCollector.cpp @@ -65,7 +65,8 @@ auto MakeUsage( bool isDynamic, bool accelStructureRead ) RTGL1::VertexCollector::VertexCollector( VkDevice _device, MemoryAllocator& _allocator, - const uint32_t ( &_maxVertsPerLayer )[ 4 ], + const size_t ( &_maxVertsPerLayer )[ 4 ], + const size_t _maxIndices, bool _isDynamic, std::string_view _debugName ) : device{ _device } @@ -74,7 +75,7 @@ RTGL1::VertexCollector::VertexCollector( VkDevice _device, MakeUsage( _isDynamic, true ), MakeName( "Vertices", _debugName ) } , bufIndices{ _allocator, - MAX_INDEXED_PRIMITIVE_COUNT * 3, + _maxIndices, MakeUsage( _isDynamic, true ), MakeName( "Indices", _debugName ) } , bufTexcoordLayer1{ _allocator, @@ -168,29 +169,21 @@ auto RTGL1::VertexCollector::Upload( VertexCollectorFilterTypeFlags geomFlags, } - if( !( geomFlags & FT::CF_DYNAMIC ) ) { - if( curVertexCount >= MAX_STATIC_VERTEX_COUNT ) + if( curVertexCount >= bufVertices.ElementCount() ) { - debug::Error( "Too many static vertices: the limit is {}", MAX_STATIC_VERTEX_COUNT ); + debug::Error( geomFlags & FT::CF_DYNAMIC ? "Too many dynamic vertices: the limit is {}" + : "Too many static vertices: the limit is {}", + bufVertices.ElementCount() ); return {}; } - } - else - { - if( curVertexCount >= MAX_DYNAMIC_VERTEX_COUNT ) + if( curIndexCount >= bufIndices.ElementCount() ) { - debug::Error( "Too many dynamic vertices: the limit is {}", MAX_DYNAMIC_VERTEX_COUNT ); + debug::Error( "Too many indices: the limit is {}", bufIndices.ElementCount() ); return {}; } } - if( curIndexCount >= MAX_INDEXED_PRIMITIVE_COUNT * 3 ) - { - debug::Error( "Too many indices: the limit is {}", MAX_INDEXED_PRIMITIVE_COUNT * 3 ); - return {}; - } - // copy data to buffers diff --git a/Source/VertexCollector.h b/Source/VertexCollector.h index f7f1ef5d..f1211f61 100644 --- a/Source/VertexCollector.h +++ b/Source/VertexCollector.h @@ -47,7 +47,8 @@ class VertexCollector public: explicit VertexCollector( VkDevice device, MemoryAllocator& allocator, - const uint32_t ( &maxVertsPerLayer )[ 4 ], + const size_t ( &maxVertsPerLayer )[ 4 ], + const size_t maxIndices, bool isDynamic, std::string_view debugName ); @@ -164,19 +165,19 @@ class VertexCollector public: explicit SharedDeviceLocal( MemoryAllocator& allocator, - uint32_t maxElements, + size_t maxElements, VkBufferUsageFlags usage, std::string_view name ) { if( maxElements > 0 ) { - deviceLocal = std::make_shared< Buffer >(); - deviceLocal->Init( allocator, - sizeof( T ) * maxElements, - usage, - VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, - MakeName( name, false ).c_str() ); - Init( deviceLocal, allocator, name ); + auto newDeviceLocal = std::make_shared< Buffer >(); + newDeviceLocal->Init( allocator, + sizeof( T ) * maxElements, + usage, + VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, + MakeName( name, false ).c_str() ); + Init( newDeviceLocal, allocator, name ); } } @@ -191,6 +192,12 @@ class VertexCollector } [[nodiscard]] bool IsInitialized() const { return deviceLocal != nullptr; } + [[nodiscard]] auto ElementCount() const + { + assert( deviceLocal->GetSize() == staging.GetSize() ); + assert( deviceLocal->GetSize() % sizeof( T ) == 0 ); + return deviceLocal->GetSize() / sizeof( T ); + } ~SharedDeviceLocal() { diff --git a/Source/VulkanDevice_Init.cpp b/Source/VulkanDevice_Init.cpp index 45a9fee6..1ccf172f 100644 --- a/Source/VulkanDevice_Init.cpp +++ b/Source/VulkanDevice_Init.cpp @@ -305,6 +305,8 @@ RTGL1::VulkanDevice::VulkanDevice( const RgInstanceCreateInfo* info ) cmdManager, *uniform, *shaderManager, + info->replacementsMaxVertexCount, + info->dynamicMaxVertexCount, info->allowTexCoordLayer1, info->allowTexCoordLayer2, info->allowTexCoordLayer3);