Skip to content

Commit

Permalink
Move vertex count allocation constants to rgCreate
Browse files Browse the repository at this point in the history
  • Loading branch information
sultim-t committed Nov 12, 2023
1 parent e4d250a commit 09d9662
Show file tree
Hide file tree
Showing 11 changed files with 69 additions and 60 deletions.
7 changes: 7 additions & 0 deletions Include/RTGL1/RTGL1.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
52 changes: 27 additions & 25 deletions Source/ASManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions Source/ASManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
4 changes: 0 additions & 4 deletions Source/Generated/GenerateShaderCommon.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Expand Down
3 changes: 0 additions & 3 deletions Source/Generated/ShaderCommonC.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ namespace RTGL1

#include <stdint.h>

#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)
Expand Down
3 changes: 0 additions & 3 deletions Source/Generated/ShaderCommonGLSL.h
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
4 changes: 4 additions & 0 deletions Source/Scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
Expand All @@ -154,6 +156,8 @@ RTGL1::Scene::Scene( VkDevice _device,
_allocator,
std::move( _cmdManager ),
geomInfoMgr,
_maxReplacementsVerts,
_maxDynamicVerts,
_enableTexCoordLayer1,
_enableTexCoordLayer2,
_enableTexCoordLayer3 );
Expand Down
2 changes: 2 additions & 0 deletions Source/Scene.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
25 changes: 9 additions & 16 deletions Source/VertexCollector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -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,
Expand Down Expand Up @@ -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
Expand Down
25 changes: 16 additions & 9 deletions Source/VertexCollector.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 );

Expand Down Expand Up @@ -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 );
}
}

Expand All @@ -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()
{
Expand Down
2 changes: 2 additions & 0 deletions Source/VulkanDevice_Init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,8 @@ RTGL1::VulkanDevice::VulkanDevice( const RgInstanceCreateInfo* info )
cmdManager,
*uniform,
*shaderManager,
info->replacementsMaxVertexCount,
info->dynamicMaxVertexCount,
info->allowTexCoordLayer1,
info->allowTexCoordLayer2,
info->allowTexCoordLayer3);
Expand Down

0 comments on commit 09d9662

Please sign in to comment.