Skip to content

Commit

Permalink
Better handling of surfaceCommand 0
Browse files Browse the repository at this point in the history
Don't actually add it, just do `-1` when adding the command. Simplify some code a bit, remove `culledCommandsOffset`.
  • Loading branch information
VReaperV committed Sep 15, 2024
1 parent 58c55f0 commit 326dfa2
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 34 deletions.
20 changes: 9 additions & 11 deletions src/engine/renderer/Material.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -900,8 +900,7 @@ void MaterialSystem::GenerateWorldCommandBuffer() {
nullptr, GL_STATIC_DRAW );
uint32_t* surfaceDescriptors = surfaceDescriptorsSSBO.MapBufferRange( surfaceDescriptorsCount * descriptorSize );

culledCommandsCount = totalBatchCount * SURFACE_COMMANDS_PER_BATCH;
surfaceCommandsCount = totalBatchCount * SURFACE_COMMANDS_PER_BATCH + 1;
surfaceCommandsCount = totalBatchCount * SURFACE_COMMANDS_PER_BATCH;

surfaceCommandsSSBO.BindBuffer();
surfaceCommandsSSBO.BufferStorage( surfaceCommandsCount * SURFACE_COMMAND_SIZE * MAX_VIEWFRAMES, 1, nullptr );
Expand All @@ -911,10 +910,10 @@ void MaterialSystem::GenerateWorldCommandBuffer() {

culledCommandsBuffer.BindBuffer( GL_SHADER_STORAGE_BUFFER );
culledCommandsBuffer.BufferStorage( GL_SHADER_STORAGE_BUFFER,
culledCommandsCount * INDIRECT_COMMAND_SIZE * MAX_VIEWFRAMES, 1, nullptr );
surfaceCommandsCount * INDIRECT_COMMAND_SIZE * MAX_VIEWFRAMES, 1, nullptr );
culledCommandsBuffer.MapAll( GL_SHADER_STORAGE_BUFFER );
GLIndirectBuffer::GLIndirectCommand* culledCommands = ( GLIndirectBuffer::GLIndirectCommand* ) culledCommandsBuffer.GetData();
memset( culledCommands, 0, culledCommandsCount * sizeof( GLIndirectBuffer::GLIndirectCommand ) * MAX_VIEWFRAMES );
memset( culledCommands, 0, surfaceCommandsCount * sizeof( GLIndirectBuffer::GLIndirectCommand ) * MAX_VIEWFRAMES );
culledCommandsBuffer.FlushAll( GL_SHADER_STORAGE_BUFFER );

surfaceBatchesUBO.BindBuffer();
Expand Down Expand Up @@ -984,8 +983,8 @@ void MaterialSystem::GenerateWorldCommandBuffer() {
const drawSurf_t* depthDrawSurf = drawSurf->depthSurface;
const Material* material = &materialPacks[depthDrawSurf->materialPackIDs[0]].materials[depthDrawSurf->materialIDs[0]];
uint cmdID = material->surfaceCommandBatchOffset * SURFACE_COMMANDS_PER_BATCH + depthDrawSurf->drawCommandIDs[0];
cmdID++; // Add 1 because the first surface command is always reserved as a fake command
surface.surfaceCommandIDs[0] = cmdID;
// Add 1 because cmd 0 == no-command
surface.surfaceCommandIDs[0] = cmdID + 1;

SurfaceCommand surfaceCommand;
surfaceCommand.enabled = 0;
Expand All @@ -997,8 +996,8 @@ void MaterialSystem::GenerateWorldCommandBuffer() {
for ( shaderStage_t* pStage = drawSurf->shader->stages; pStage < drawSurf->shader->lastStage; pStage++ ) {
const Material* material = &materialPacks[drawSurf->materialPackIDs[stage]].materials[drawSurf->materialIDs[stage]];
uint32_t cmdID = material->surfaceCommandBatchOffset * SURFACE_COMMANDS_PER_BATCH + drawSurf->drawCommandIDs[stage];
cmdID++; // Add 1 because the first surface command is always reserved as a fake command
surface.surfaceCommandIDs[stage + ( depthPrePass ? 1 : 0 )] = cmdID;
// Add 1 because cmd 0 == no-command
surface.surfaceCommandIDs[stage + ( depthPrePass ? 1 : 0 )] = cmdID + 1;

SurfaceCommand surfaceCommand;
surfaceCommand.enabled = 0;
Expand Down Expand Up @@ -1704,7 +1703,6 @@ void MaterialSystem::CullSurfaces() {
gl_processSurfacesShader->SetUniform_Frame( nextFrame );
gl_processSurfacesShader->SetUniform_ViewID( view );
gl_processSurfacesShader->SetUniform_SurfaceCommandsOffset( surfaceCommandsCount * ( MAX_VIEWS * nextFrame + view ) );
gl_processSurfacesShader->SetUniform_CulledCommandsOffset( culledCommandsCount * ( MAX_VIEWS * nextFrame + view ) );

glMemoryBarrier( GL_SHADER_STORAGE_BARRIER_BIT | GL_ATOMIC_COUNTER_BARRIER_BIT );
gl_processSurfacesShader->DispatchCompute( totalBatchCount, 1, 1 );
Expand Down Expand Up @@ -2049,7 +2047,7 @@ void MaterialSystem::RenderMaterial( Material& material, const uint32_t viewID )

glMultiDrawElementsIndirectCountARB( GL_TRIANGLES, GL_UNSIGNED_INT,
BUFFER_OFFSET( material.surfaceCommandBatchOffset * SURFACE_COMMANDS_PER_BATCH * sizeof( GLIndirectBuffer::GLIndirectCommand )
+ ( culledCommandsCount * ( MAX_VIEWS * currentFrame + viewID )
+ ( surfaceCommandsCount * ( MAX_VIEWS * currentFrame + viewID )
* sizeof( GLIndirectBuffer::GLIndirectCommand ) ) ),
material.globalID * sizeof( uint32_t )
+ ( MAX_COMMAND_COUNTERS * ( MAX_VIEWS * currentFrame + viewID ) ) * sizeof( uint32_t ),
Expand All @@ -2063,7 +2061,7 @@ void MaterialSystem::RenderMaterial( Material& material, const uint32_t viewID )
GL_State( GLS_DEPTHTEST_DISABLE );
glMultiDrawElementsIndirectCountARB( GL_LINES, GL_UNSIGNED_INT,
BUFFER_OFFSET( material.surfaceCommandBatchOffset * SURFACE_COMMANDS_PER_BATCH * sizeof( GLIndirectBuffer::GLIndirectCommand )
+ ( culledCommandsCount * ( MAX_VIEWS * currentFrame + viewID )
+ ( surfaceCommandsCount * ( MAX_VIEWS * currentFrame + viewID )
* sizeof( GLIndirectBuffer::GLIndirectCommand ) ) ),
material.globalID * sizeof( uint32_t )
+ ( MAX_COMMAND_COUNTERS * ( MAX_VIEWS * currentFrame + viewID ) ) * sizeof( uint32_t ),
Expand Down
1 change: 0 additions & 1 deletion src/engine/renderer/Material.h
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,6 @@ class MaterialSystem {
uint32_t totalBatchCount = 0;

uint32_t surfaceCommandsCount = 0;
uint32_t culledCommandsCount = 0;
uint32_t surfaceDescriptorsCount = 0;

std::vector<drawSurf_t> dynamicDrawSurfs;
Expand Down
3 changes: 1 addition & 2 deletions src/engine/renderer/gl_shader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2972,6 +2972,5 @@ GLShader_processSurfaces::GLShader_processSurfaces( GLShaderManager* manager ) :
GLShader( "processSurfaces", ATTR_POSITION, manager, false, false, true ),
u_Frame( this ),
u_ViewID( this ),
u_SurfaceCommandsOffset( this ),
u_CulledCommandsOffset( this ) {
u_SurfaceCommandsOffset( this ) {
}
15 changes: 1 addition & 14 deletions src/engine/renderer/gl_shader.h
Original file line number Diff line number Diff line change
Expand Up @@ -3359,18 +3359,6 @@ class u_SurfaceCommandsOffset :
}
};

class u_CulledCommandsOffset :
GLUniform1ui {
public:
u_CulledCommandsOffset( GLShader* shader ) :
GLUniform1ui( shader, "u_CulledCommandsOffset" ) {
}

void SetUniform_CulledCommandsOffset( const uint culledCommandsOffset ) {
this->SetValue( culledCommandsOffset );
}
};

class u_ModelMatrix :
GLUniformMatrix4f
{
Expand Down Expand Up @@ -4735,8 +4723,7 @@ class GLShader_processSurfaces :
public GLShader,
public u_Frame,
public u_ViewID,
public u_SurfaceCommandsOffset,
public u_CulledCommandsOffset {
public u_SurfaceCommandsOffset {
public:
GLShader_processSurfaces( GLShaderManager* manager );
};
Expand Down
3 changes: 2 additions & 1 deletion src/engine/renderer/glsl_source/cull_cp.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,8 @@ void ProcessSurfaceCommands( const in SurfaceDescriptor surface, const in bool e
if( commandID == 0 ) { // Reserved for no-command
return;
}
surfaceCommands[commandID + u_SurfaceCommandsOffset].enabled = enabled;
// Subtract 1 because of no-command
surfaceCommands[commandID + u_SurfaceCommandsOffset - 1].enabled = enabled;
}
}

Expand Down
8 changes: 3 additions & 5 deletions src/engine/renderer/glsl_source/processSurfaces_cp.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ layout (binding = 4) uniform atomic_uint atomicCommandCounters[MAX_COMMAND_COUNT
uniform uint u_Frame;
uniform uint u_ViewID;
uniform uint u_SurfaceCommandsOffset;
uniform uint u_CulledCommandsOffset;

#if defined(HAVE_KHR_shader_subgroup_basic) && defined(HAVE_KHR_shader_subgroup_arithmetic)\
&& defined(HAVE_KHR_shader_subgroup_ballot) && defined(HAVE_ARB_shader_atomic_counter_ops)
Expand Down Expand Up @@ -99,17 +98,16 @@ void AddDrawCommand( in uint commandID, in uvec2 materialID ) {
indirectCommand.baseInstance = command.drawCommand.baseInstance;

#if defined(HAVE_processSurfaces_subgroup)
culledCommands[atomicCmdID + subgroupOffset + materialID.y * MAX_COMMAND_COUNTERS + u_CulledCommandsOffset] = indirectCommand;
culledCommands[atomicCmdID + subgroupOffset + materialID.y * MAX_COMMAND_COUNTERS + u_SurfaceCommandsOffset] = indirectCommand;
#else
culledCommands[atomicCmdID + materialID.y * MAX_COMMAND_COUNTERS + u_CulledCommandsOffset] = indirectCommand;
culledCommands[atomicCmdID + materialID.y * MAX_COMMAND_COUNTERS + u_SurfaceCommandsOffset] = indirectCommand;
#endif
}
}

void main() {
const uint globalGroupID = GLOBAL_GROUP_ID;
// Add 1 because the first surface command is always reserved as a fake command
const uint globalInvocationID = GLOBAL_INVOCATION_ID + 1;
const uint globalInvocationID = GLOBAL_INVOCATION_ID;

// Each surfaceBatch encompasses 64 surfaceCommands with the same material, padded to 64 as necessary
const uvec2 materialID = UVEC2_FROM_UVEC4_ARRAY( surfaceBatches, globalGroupID );
Expand Down

0 comments on commit 326dfa2

Please sign in to comment.