Skip to content

Commit bf91241

Browse files
committed
Material system clean-up
1 parent 7b73eac commit bf91241

File tree

5 files changed

+66
-71
lines changed

5 files changed

+66
-71
lines changed

src/engine/renderer/Material.cpp

Lines changed: 56 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -501,46 +501,7 @@ void MaterialSystem::GenerateTexturesBuffer( std::vector<TextureData>& textures,
501501
void MaterialSystem::GenerateWorldCommandBuffer( std::vector<MaterialSurface>& surfaces ) {
502502
Log::Debug( "Generating world command buffer" );
503503

504-
totalBatchCount = 0;
505-
506-
for ( MaterialSurface& surface : surfaces ) {
507-
if ( surface.skyBrush ) {
508-
continue;
509-
}
510-
511-
for ( uint8_t stage = 0; stage < surface.stages; stage++ ) {
512-
Material* material = &materialPacks[surface.materialPackIDs[stage]].materials[surface.materialIDs[stage]];
513-
material->drawCommandCount++;
514-
}
515-
}
516-
517-
uint32_t batchOffset = 0;
518-
uint32_t globalID = 0;
519-
for ( MaterialPack& pack : materialPacks ) {
520-
for ( Material& material : pack.materials ) {
521-
material.surfaceCommandBatchOffset = batchOffset;
522-
523-
const uint32_t cmdCount = material.drawCommandCount;
524-
const uint32_t batchCount = cmdCount % SURFACE_COMMANDS_PER_BATCH == 0 ? cmdCount / SURFACE_COMMANDS_PER_BATCH
525-
: cmdCount / SURFACE_COMMANDS_PER_BATCH + 1;
526-
527-
material.surfaceCommandBatchOffset = batchOffset;
528-
material.surfaceCommandBatchCount = batchCount;
529-
530-
batchOffset += batchCount;
531-
material.globalID = globalID;
532-
533-
material.drawCommandCount = 0;
534-
535-
totalBatchCount += batchCount;
536-
globalID++;
537-
}
538-
}
539-
540-
Log::Debug( "Total batch count: %u", totalBatchCount );
541-
542-
totalDrawSurfs = surfaces.size();
543-
504+
// TexBundles
544505
if ( glConfig2.maxUniformBlockSize >= MIN_MATERIAL_UBO_SIZE ) {
545506
texDataBufferType = GL_UNIFORM_BUFFER;
546507
texDataBindingPoint = BufferBind::TEX_DATA;
@@ -566,6 +527,7 @@ void MaterialSystem::GenerateWorldCommandBuffer( std::vector<MaterialSurface>& s
566527
dynamicTexDataOffset = texData.size() * TEX_BUNDLE_SIZE;
567528
dynamicTexDataSize = dynamicTexData.size() * TEX_BUNDLE_SIZE;
568529

530+
// Lightmaps/Deluxemaps
569531
lightMapDataUBO.BufferStorage( MAX_LIGHTMAPS * LIGHTMAP_SIZE, 1, nullptr );
570532

571533
uint64_t* lightMapData = ( uint64_t* ) stagingBuffer.MapBuffer( MAX_LIGHTMAPS * LIGHTMAP_SIZE );
@@ -605,14 +567,43 @@ void MaterialSystem::GenerateWorldCommandBuffer( std::vector<MaterialSurface>& s
605567

606568
stagingBuffer.QueueStagingCopy( &lightMapDataUBO, 0 );
607569

608-
surfaceCommandsCount = totalBatchCount * SURFACE_COMMANDS_PER_BATCH;
570+
// Surface batches
571+
for ( MaterialSurface& surface : surfaces ) {
572+
if ( surface.skyBrush ) {
573+
continue;
574+
}
609575

610-
culledCommandsBuffer.BufferStorage( surfaceCommandsCount * INDIRECT_COMMAND_SIZE * MAX_VIEWFRAMES, 1, nullptr );
611-
GLIndirectCommand* culledCommands =
612-
( GLIndirectCommand* ) stagingBuffer.MapBuffer( surfaceCommandsCount * INDIRECT_COMMAND_SIZE * MAX_VIEWFRAMES );
613-
memset( culledCommands, 0, surfaceCommandsCount * sizeof( GLIndirectCommand ) * MAX_VIEWFRAMES );
576+
for ( uint8_t stage = 0; stage < surface.stages; stage++ ) {
577+
Material* material = &materialPacks[surface.materialPackIDs[stage]].materials[surface.materialIDs[stage]];
578+
material->drawCommandCount++;
579+
}
580+
}
581+
582+
totalBatchCount = 0;
583+
uint32_t batchOffset = 0;
584+
uint32_t globalID = 0;
585+
for ( MaterialPack& pack : materialPacks ) {
586+
for ( Material& material : pack.materials ) {
587+
material.surfaceCommandBatchOffset = batchOffset;
588+
589+
const uint32_t cmdCount = material.drawCommandCount;
590+
const uint32_t batchCount = cmdCount % SURFACE_COMMANDS_PER_BATCH == 0 ? cmdCount / SURFACE_COMMANDS_PER_BATCH
591+
: cmdCount / SURFACE_COMMANDS_PER_BATCH + 1;
592+
593+
material.surfaceCommandBatchOffset = batchOffset;
594+
material.surfaceCommandBatchCount = batchCount;
595+
596+
batchOffset += batchCount;
597+
material.globalID = globalID;
598+
599+
material.drawCommandCount = 0;
614600

615-
stagingBuffer.QueueStagingCopy( &culledCommandsBuffer, 0 );
601+
totalBatchCount += batchCount;
602+
globalID++;
603+
}
604+
}
605+
606+
Log::Debug( "Total batch count: %u", totalBatchCount );
616607

617608
surfaceBatchesUBO.BufferStorage( MAX_SURFACE_COMMAND_BATCHES * SURFACE_COMMAND_BATCH_SIZE, 1, nullptr );
618609
SurfaceCommandBatch* surfaceCommandBatches =
@@ -639,12 +630,6 @@ void MaterialSystem::GenerateWorldCommandBuffer( std::vector<MaterialSurface>& s
639630

640631
stagingBuffer.QueueStagingCopy( &surfaceBatchesUBO, 0 );
641632

642-
atomicCommandCountersBuffer.BufferStorage( MAX_COMMAND_COUNTERS * MAX_VIEWS, MAX_FRAMES, nullptr );
643-
uint32_t* atomicCommandCounters = stagingBuffer.MapBuffer( MAX_COMMAND_COUNTERS * MAX_VIEWS );
644-
memset( atomicCommandCounters, 0, MAX_COMMAND_COUNTERS * MAX_VIEWFRAMES * sizeof( uint32_t ) );
645-
646-
stagingBuffer.QueueStagingCopy( &atomicCommandCountersBuffer, 0 );
647-
648633
/* For use in debugging compute shaders
649634
Intended for use with Nsight Graphics to format the output */
650635
if ( r_materialDebug.Get() ) {
@@ -656,11 +641,23 @@ void MaterialSystem::GenerateWorldCommandBuffer( std::vector<MaterialSurface>& s
656641
debugSSBO.UnmapBuffer();
657642
}
658643

644+
// Surfaces
645+
surfaceCommandsCount = totalBatchCount * SURFACE_COMMANDS_PER_BATCH;
646+
647+
culledCommandsBuffer.BufferStorage( surfaceCommandsCount * INDIRECT_COMMAND_SIZE * MAX_VIEWFRAMES, 1, nullptr );
648+
649+
atomicCommandCountersBuffer.BufferStorage( MAX_COMMAND_COUNTERS * MAX_VIEWS, MAX_FRAMES, nullptr );
650+
uint32_t* atomicCommandCounters = stagingBuffer.MapBuffer( MAX_COMMAND_COUNTERS * MAX_VIEWS );
651+
memset( atomicCommandCounters, 0, MAX_COMMAND_COUNTERS * MAX_VIEWFRAMES * sizeof( uint32_t ) );
652+
653+
stagingBuffer.QueueStagingCopy( &atomicCommandCountersBuffer, 0 );
654+
659655
stagingBuffer.FlushAll();
660656

661-
surfaceDescriptorsCount = totalDrawSurfs;
657+
surfaceDescriptorsCount = surfaces.size();
658+
662659
descriptorSize = BOUNDING_SPHERE_SIZE + maxStages;
663-
surfaceDescriptorsSSBO.BufferStorage( surfaceDescriptorsCount* descriptorSize, 1, nullptr );
660+
surfaceDescriptorsSSBO.BufferStorage( surfaceDescriptorsCount * descriptorSize, 1, nullptr );
664661
uint32_t* surfaceDescriptors = stagingBuffer.MapBuffer( surfaceDescriptorsCount * descriptorSize );
665662

666663
stagingBuffer.QueueStagingCopy( &surfaceDescriptorsSSBO, 0 );
@@ -722,7 +719,7 @@ void MaterialSystem::GenerateWorldCommandBuffer( std::vector<MaterialSurface>& s
722719
for ( MaterialPack& pack : materialPacks ) {
723720
totalCount += pack.materials.size();
724721
}
725-
Log::Notice( "Generated %u BSP materials from %u BSP surfaces", totalCount, totalDrawSurfs );
722+
Log::Notice( "Generated %u BSP materials from %u BSP surfaces", totalCount, surfaceDescriptorsCount );
726723
Log::Notice( "Materials UBO: total: %.2f kb, dynamic: %.2f kb, texData: %.2f kb",
727724
totalStageSize * 4 / 1024.0f, dynamicStagesSize * 4 / 1024.0f,
728725
( texData.size() + dynamicTexData.size() ) * TEX_BUNDLE_SIZE * 4 / 1024.0f );
@@ -1584,12 +1581,12 @@ void MaterialSystem::CullSurfaces() {
15841581
}
15851582

15861583
gl_cullShader->BindProgram( 0 );
1587-
uint32_t globalWorkGroupX = totalDrawSurfs % MAX_COMMAND_COUNTERS == 0 ?
1588-
totalDrawSurfs / MAX_COMMAND_COUNTERS : totalDrawSurfs / MAX_COMMAND_COUNTERS + 1;
1584+
uint32_t globalWorkGroupX = surfaceDescriptorsCount % MAX_COMMAND_COUNTERS == 0 ?
1585+
surfaceDescriptorsCount / MAX_COMMAND_COUNTERS : surfaceDescriptorsCount / MAX_COMMAND_COUNTERS + 1;
15891586
GL_Bind( depthImage );
15901587
gl_cullShader->SetUniform_Frame( nextFrame );
15911588
gl_cullShader->SetUniform_ViewID( view );
1592-
gl_cullShader->SetUniform_TotalDrawSurfs( totalDrawSurfs );
1589+
gl_cullShader->SetUniform_SurfaceDescriptorsCount( surfaceDescriptorsCount );
15931590
gl_cullShader->SetUniform_UseFrustumCulling( r_gpuFrustumCulling.Get() );
15941591
gl_cullShader->SetUniform_UseOcclusionCulling( r_gpuOcclusionCulling.Get() );
15951592
gl_cullShader->SetUniform_CameraPosition( origin );
@@ -1753,7 +1750,7 @@ void MaterialSystem::Free() {
17531750

17541751
buildOneShader = true;
17551752

1756-
totalDrawSurfs = 0;
1753+
surfaceDescriptorsCount = 0;
17571754

17581755
currentFrame = 0;
17591756
nextFrame = 1;

src/engine/renderer/Material.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -401,11 +401,9 @@ class MaterialSystem {
401401
image_t* depthImage;
402402
int depthImageLevels;
403403

404-
uint32_t totalDrawSurfs;
405-
uint32_t totalBatchCount = 0;
406-
407404
uint32_t surfaceCommandsCount = 0;
408405
uint32_t surfaceDescriptorsCount = 0;
406+
uint32_t totalBatchCount = 0;
409407

410408
uint32_t packIDs[3] = { 0, 0, 0 };
411409

src/engine/renderer/gl_shader.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3028,7 +3028,7 @@ GLShader_cull::GLShader_cull() :
30283028
false, "cull" ),
30293029
u_Frame( this ),
30303030
u_ViewID( this ),
3031-
u_TotalDrawSurfs( this ),
3031+
u_SurfaceDescriptorsCount( this ),
30323032
u_SurfaceCommandsOffset( this ),
30333033
u_Frustum( this ),
30343034
u_UseFrustumCulling( this ),

src/engine/renderer/gl_shader.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2764,15 +2764,15 @@ class u_P11 :
27642764
}
27652765
};
27662766

2767-
class u_TotalDrawSurfs :
2767+
class u_SurfaceDescriptorsCount :
27682768
GLUniform1ui {
27692769
public:
2770-
u_TotalDrawSurfs( GLShader* shader ) :
2771-
GLUniform1ui( shader, "u_TotalDrawSurfs", true ) {
2770+
u_SurfaceDescriptorsCount( GLShader* shader ) :
2771+
GLUniform1ui( shader, "u_SurfaceDescriptorsCount", true ) {
27722772
}
27732773

2774-
void SetUniform_TotalDrawSurfs( const uint totalDrawSurfs ) {
2775-
this->SetValue( totalDrawSurfs );
2774+
void SetUniform_SurfaceDescriptorsCount( const uint SurfaceDescriptorsCount ) {
2775+
this->SetValue( SurfaceDescriptorsCount );
27762776
}
27772777
};
27782778

@@ -4153,7 +4153,7 @@ class GLShader_cull :
41534153
public GLShader,
41544154
public u_Frame,
41554155
public u_ViewID,
4156-
public u_TotalDrawSurfs,
4156+
public u_SurfaceDescriptorsCount,
41574157
public u_SurfaceCommandsOffset,
41584158
public u_Frustum,
41594159
public u_UseFrustumCulling,

src/engine/renderer/glsl_source/cull_cp.glsl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ layout(std430, binding = BIND_PORTAL_SURFACES) restrict buffer portalSurfacesSSB
7070

7171
uniform uint u_Frame;
7272
uniform uint u_ViewID;
73-
uniform uint u_TotalDrawSurfs;
73+
uniform uint u_SurfaceDescriptorsCount;
7474
uniform uint u_SurfaceCommandsOffset;
7575
uniform vec4 u_Frustum[6]; // xyz - normal, w - distance
7676
uniform bool u_UseFrustumCulling;
@@ -198,7 +198,7 @@ void main() {
198198
}
199199

200200
// Regular surfaces
201-
if( globalInvocationID >= u_TotalDrawSurfs ) {
201+
if( globalInvocationID >= u_SurfaceDescriptorsCount ) {
202202
return;
203203
}
204204
SurfaceDescriptor surface = surfaces[globalInvocationID];

0 commit comments

Comments
 (0)