Skip to content

Commit f5ffb7f

Browse files
committed
Clean-up material system buffer bindings
1 parent c319055 commit f5ffb7f

File tree

6 files changed

+73
-29
lines changed

6 files changed

+73
-29
lines changed

src/engine/renderer/Material.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,18 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3737
#include "Material.h"
3838
#include "ShadeCommon.h"
3939

40-
GLUBO materialsUBO( "materials", 6, GL_MAP_WRITE_BIT, GL_MAP_INVALIDATE_RANGE_BIT );
41-
GLBuffer texDataBuffer( "texData", 7, GL_MAP_WRITE_BIT, GL_MAP_FLUSH_EXPLICIT_BIT );
42-
GLUBO lightMapDataUBO( "lightMapData", 8, GL_MAP_WRITE_BIT, GL_MAP_FLUSH_EXPLICIT_BIT );
40+
GLUBO materialsUBO( "materials", Util::ordinal( BufferBind::MATERIALS ), GL_MAP_WRITE_BIT, GL_MAP_INVALIDATE_RANGE_BIT );
41+
GLBuffer texDataBuffer( "texData", Util::ordinal( BufferBind::TEX_DATA ), GL_MAP_WRITE_BIT, GL_MAP_FLUSH_EXPLICIT_BIT );
42+
GLUBO lightMapDataUBO( "lightMapData", Util::ordinal( BufferBind::LIGHTMAP_DATA ), GL_MAP_WRITE_BIT, GL_MAP_FLUSH_EXPLICIT_BIT );
4343

44-
GLSSBO surfaceDescriptorsSSBO( "surfaceDescriptors", 1, GL_MAP_WRITE_BIT, GL_MAP_INVALIDATE_RANGE_BIT );
45-
GLSSBO surfaceCommandsSSBO( "surfaceCommands", 2, GL_MAP_WRITE_BIT, GL_MAP_FLUSH_EXPLICIT_BIT );
46-
GLBuffer culledCommandsBuffer( "culledCommands", 3, GL_MAP_WRITE_BIT, GL_MAP_FLUSH_EXPLICIT_BIT );
47-
GLUBO surfaceBatchesUBO( "surfaceBatches", 0, GL_MAP_WRITE_BIT, GL_MAP_INVALIDATE_RANGE_BIT );
48-
GLBuffer atomicCommandCountersBuffer( "atomicCommandCounters", 4, GL_MAP_WRITE_BIT, GL_MAP_FLUSH_EXPLICIT_BIT );
49-
GLSSBO portalSurfacesSSBO( "portalSurfaces", 5, GL_MAP_READ_BIT | GL_MAP_PERSISTENT_BIT, 0 );
44+
GLSSBO surfaceDescriptorsSSBO( "surfaceDescriptors", Util::ordinal( BufferBind::SURFACE_DESCRIPTORS ), GL_MAP_WRITE_BIT, GL_MAP_INVALIDATE_RANGE_BIT );
45+
GLSSBO surfaceCommandsSSBO( "surfaceCommands", Util::ordinal( BufferBind::SURFACE_COMMANDS ), GL_MAP_WRITE_BIT, GL_MAP_FLUSH_EXPLICIT_BIT );
46+
GLBuffer culledCommandsBuffer( "culledCommands", Util::ordinal( BufferBind::CULLED_COMMANDS ), GL_MAP_WRITE_BIT, GL_MAP_FLUSH_EXPLICIT_BIT );
47+
GLUBO surfaceBatchesUBO( "surfaceBatches", Util::ordinal( BufferBind::SURFACE_BATCHES ), GL_MAP_WRITE_BIT, GL_MAP_INVALIDATE_RANGE_BIT );
48+
GLBuffer atomicCommandCountersBuffer( "atomicCommandCounters", Util::ordinal( BufferBind::COMMAND_COUNTERS_ATOMIC ), GL_MAP_WRITE_BIT, GL_MAP_FLUSH_EXPLICIT_BIT );
49+
GLSSBO portalSurfacesSSBO( "portalSurfaces", Util::ordinal( BufferBind::PORTAL_SURFACES ), GL_MAP_READ_BIT | GL_MAP_PERSISTENT_BIT, 0 );
5050

51-
GLSSBO debugSSBO( "debug", 10, GL_MAP_WRITE_BIT, GL_MAP_INVALIDATE_RANGE_BIT );
51+
GLSSBO debugSSBO( "debug", Util::ordinal( BufferBind::DEBUG ), GL_MAP_WRITE_BIT, GL_MAP_INVALIDATE_RANGE_BIT );
5252

5353
PortalView portalStack[MAX_VIEWS];
5454

@@ -1607,11 +1607,11 @@ void MaterialSystem::UpdateDynamicSurfaces() {
16071607
}
16081608

16091609
void MaterialSystem::UpdateFrameData() {
1610-
atomicCommandCountersBuffer.BindBufferBase( GL_SHADER_STORAGE_BUFFER );
1610+
atomicCommandCountersBuffer.BindBufferBase( GL_SHADER_STORAGE_BUFFER, Util::ordinal( BufferBind::COMMAND_COUNTERS_STORAGE ) );
16111611
gl_clearSurfacesShader->BindProgram( 0 );
16121612
gl_clearSurfacesShader->SetUniform_Frame( nextFrame );
16131613
gl_clearSurfacesShader->DispatchCompute( MAX_VIEWS, 1, 1 );
1614-
atomicCommandCountersBuffer.UnBindBufferBase( GL_SHADER_STORAGE_BUFFER );
1614+
atomicCommandCountersBuffer.UnBindBufferBase( GL_SHADER_STORAGE_BUFFER, Util::ordinal( BufferBind::COMMAND_COUNTERS_STORAGE ) );
16151615

16161616
GL_CheckErrors();
16171617
}

src/engine/renderer/Material.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,20 @@ struct SurfaceCommandBatch {
280280
uint32_t materialIDs[2] { 0, 0 };
281281
};
282282

283+
enum class BufferBind {
284+
MATERIALS = 1, // LightTile UBO uses binding point 0, so avoid it here
285+
TEX_DATA = 6,
286+
LIGHTMAP_DATA = 2,
287+
SURFACE_DESCRIPTORS = 0,
288+
SURFACE_COMMANDS = 1,
289+
CULLED_COMMANDS = 2,
290+
SURFACE_BATCHES = 3,
291+
COMMAND_COUNTERS_ATOMIC = 0,
292+
COMMAND_COUNTERS_STORAGE = 4, // Avoid needlessly rebinding buffers
293+
PORTAL_SURFACES = 5,
294+
DEBUG = 10
295+
};
296+
283297
class MaterialSystem {
284298
public:
285299
bool generatedWorldCommandBuffer = false;

src/engine/renderer/gl_shader.cpp

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,12 @@ static std::string GenVertexHeader() {
532532
str += "#define baseInstance gl_BaseInstanceARB\n\n";
533533
}
534534

535+
if ( glConfig2.usingMaterialSystem ) {
536+
AddDefine( str, "BIND_MATERIALS", Util::ordinal( BufferBind::MATERIALS ) );
537+
AddDefine( str, "BIND_TEX_DATA", Util::ordinal( BufferBind::TEX_DATA ) );
538+
AddDefine( str, "BIND_LIGHTMAP_DATA", Util::ordinal( BufferBind::LIGHTMAP_DATA ) );
539+
}
540+
535541
return str;
536542
}
537543

@@ -566,18 +572,36 @@ static std::string GenFragmentHeader() {
566572
str += "#define baseInstance in_baseInstance\n\n";
567573
}
568574

575+
if ( glConfig2.usingMaterialSystem ) {
576+
AddDefine( str, "BIND_MATERIALS", Util::ordinal( BufferBind::MATERIALS ) );
577+
AddDefine( str, "BIND_TEX_DATA", Util::ordinal( BufferBind::TEX_DATA ) );
578+
AddDefine( str, "BIND_LIGHTMAP_DATA", Util::ordinal( BufferBind::LIGHTMAP_DATA ) );
579+
}
580+
569581
return str;
570582
}
571583

572584
static std::string GenComputeHeader() {
573585
std::string str;
574586

575587
// Compute shader compatibility defines
576-
AddDefine( str, "MAX_VIEWS", MAX_VIEWS );
577-
AddDefine( str, "MAX_FRAMES", MAX_FRAMES );
578-
AddDefine( str, "MAX_VIEWFRAMES", MAX_VIEWFRAMES );
579-
AddDefine( str, "MAX_SURFACE_COMMAND_BATCHES", MAX_SURFACE_COMMAND_BATCHES );
580-
AddDefine( str, "MAX_COMMAND_COUNTERS", MAX_COMMAND_COUNTERS );
588+
if ( glConfig2.usingMaterialSystem ) {
589+
AddDefine( str, "MAX_VIEWS", MAX_VIEWS );
590+
AddDefine( str, "MAX_FRAMES", MAX_FRAMES );
591+
AddDefine( str, "MAX_VIEWFRAMES", MAX_VIEWFRAMES );
592+
AddDefine( str, "MAX_SURFACE_COMMAND_BATCHES", MAX_SURFACE_COMMAND_BATCHES );
593+
AddDefine( str, "MAX_COMMAND_COUNTERS", MAX_COMMAND_COUNTERS );
594+
595+
AddDefine( str, "BIND_SURFACE_DESCRIPTORS", Util::ordinal( BufferBind::SURFACE_DESCRIPTORS ) );
596+
AddDefine( str, "BIND_SURFACE_COMMANDS", Util::ordinal( BufferBind::SURFACE_COMMANDS ) );
597+
AddDefine( str, "BIND_CULLED_COMMANDS", Util::ordinal( BufferBind::CULLED_COMMANDS ) );
598+
AddDefine( str, "BIND_SURFACE_BATCHES", Util::ordinal( BufferBind::SURFACE_BATCHES ) );
599+
AddDefine( str, "BIND_COMMAND_COUNTERS_ATOMIC", Util::ordinal( BufferBind::COMMAND_COUNTERS_ATOMIC ) );
600+
AddDefine( str, "BIND_COMMAND_COUNTERS_STORAGE", Util::ordinal( BufferBind::COMMAND_COUNTERS_STORAGE ) );
601+
AddDefine( str, "BIND_PORTAL_SURFACES", Util::ordinal( BufferBind::PORTAL_SURFACES ) );
602+
603+
AddDefine( str, "BIND_DEBUG", Util::ordinal( BufferBind::DEBUG ) );
604+
}
581605

582606
if ( glConfig2.usingBindlessTextures ) {
583607
str += "layout(bindless_image) uniform;\n";
@@ -1361,15 +1385,21 @@ std::string GLShaderManager::ShaderPostProcess( GLShader *shader, const std::str
13611385
std::string materialStruct = "\nstruct Material {\n";
13621386
// 6 kb for materials
13631387
const uint32_t count = ( 4096 + 2048 ) / shader->GetPaddedSize();
1364-
std::string materialBlock = "layout(std140, binding = 6) uniform materialsUBO {\n"
1388+
std::string materialBlock = "layout(std140, binding = "
1389+
+ std::to_string( Util::ordinal( BufferBind::MATERIALS ) )
1390+
+ ") uniform materialsUBO {\n"
13651391
" Material materials[" + std::to_string( count ) + "]; \n"
13661392
"};\n\n";
13671393

13681394
std::string texBuf = glConfig2.maxUniformBlockSize >= MIN_MATERIAL_UBO_SIZE ?
1369-
"layout(std140, binding = 7) uniform texDataUBO {\n"
1395+
"layout(std140, binding = "
1396+
+ std::to_string( Util::ordinal( BufferBind::TEX_DATA ) )
1397+
+ ") uniform texDataUBO {\n"
13701398
" TexData texData[" + std::to_string( MAX_TEX_BUNDLES ) + "]; \n"
13711399
"};\n\n"
1372-
: "layout(std430, binding = 7) restrict readonly buffer texDataSSBO {\n"
1400+
: "layout(std430, binding = "
1401+
+ std::to_string( Util::ordinal( BufferBind::TEX_DATA ) )
1402+
+ ") restrict readonly buffer texDataSSBO {\n"
13731403
" TexData texData[];\n"
13741404
"};\n\n";
13751405
// We have to store u_TextureMatrix as vec4 + vec2 because otherwise it would be aligned to a vec4 under std140

src/engine/renderer/glsl_source/clearSurfaces_cp.glsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3838

3939
layout (local_size_x = 64, local_size_y = 1, local_size_z = 1) in;
4040

41-
layout(std430, binding = 4) writeonly buffer atomicCommandCountersBuffer {
41+
layout(std430, binding = BIND_COMMAND_COUNTERS_STORAGE) writeonly buffer atomicCommandCountersBuffer {
4242
uint atomicCommandCounters[MAX_COMMAND_COUNTERS * MAX_VIEWFRAMES];
4343
};
4444

src/engine/renderer/glsl_source/cull_cp.glsl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,23 +41,23 @@ layout (local_size_x = 64, local_size_y = 1, local_size_z = 1) in;
4141

4242
layout(binding = 0) uniform sampler2D depthImage;
4343

44-
layout(std430, binding = 1) readonly restrict buffer surfaceDescriptorsSSBO {
44+
layout(std430, binding = BIND_SURFACE_DESCRIPTORS) readonly restrict buffer surfaceDescriptorsSSBO {
4545
SurfaceDescriptor surfaces[];
4646
};
4747

48-
layout(std430, binding = 2) writeonly restrict buffer surfaceCommandsSSBO {
48+
layout(std430, binding = BIND_SURFACE_COMMANDS) writeonly restrict buffer surfaceCommandsSSBO {
4949
SurfaceCommand surfaceCommands[];
5050
};
5151

52-
layout(std430, binding = 5) restrict buffer portalSurfacesSSBO {
52+
layout(std430, binding = BIND_PORTAL_SURFACES) restrict buffer portalSurfacesSSBO {
5353
PortalSurface portalSurfaces[];
5454
};
5555

5656
#if defined( r_materialDebug )
5757
#define DEBUG_INVOCATION_SIZE 5
5858
#define DEBUG_ID( id ) ( id * DEBUG_INVOCATION_SIZE )
5959

60-
layout(std430, binding = 10) writeonly restrict buffer debugSSBO {
60+
layout(std430, binding = BIND_DEBUG) writeonly restrict buffer debugSSBO {
6161
uvec4 debug[];
6262
};
6363
#endif

src/engine/renderer/glsl_source/processSurfaces_cp.glsl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,19 @@ layout (local_size_x = 64, local_size_y = 1, local_size_z = 1) in;
4141

4242
#define SurfaceCommandBatch uvec4
4343

44-
layout(std430, binding = 2) readonly restrict buffer surfaceCommandsSSBO {
44+
layout(std430, binding = BIND_SURFACE_COMMANDS) readonly restrict buffer surfaceCommandsSSBO {
4545
SurfaceCommand surfaceCommands[];
4646
};
4747

48-
layout(std430, binding = 3) writeonly restrict buffer culledCommandsSSBO {
48+
layout(std430, binding = BIND_CULLED_COMMANDS) writeonly restrict buffer culledCommandsSSBO {
4949
GLIndirectCommand culledCommands[];
5050
};
5151

52-
layout(std140, binding = 0) uniform ub_SurfaceBatches {
52+
layout(std140, binding = BIND_SURFACE_BATCHES) uniform ub_SurfaceBatches {
5353
SurfaceCommandBatch surfaceBatches[MAX_SURFACE_COMMAND_BATCHES];
5454
};
5555

56-
layout (binding = 4) uniform atomic_uint atomicCommandCounters[MAX_COMMAND_COUNTERS * MAX_VIEWFRAMES];
56+
layout (binding = BIND_COMMAND_COUNTERS_ATOMIC) uniform atomic_uint atomicCommandCounters[MAX_COMMAND_COUNTERS * MAX_VIEWFRAMES];
5757

5858
uniform uint u_Frame;
5959
uniform uint u_ViewID;

0 commit comments

Comments
 (0)