Skip to content

Commit

Permalink
Clean-up material system buffer bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
VReaperV committed Jan 29, 2025
1 parent 617e682 commit 8c10245
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 29 deletions.
24 changes: 12 additions & 12 deletions src/engine/renderer/Material.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,18 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "Material.h"
#include "ShadeCommon.h"

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

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

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

PortalView portalStack[MAX_VIEWS];

Expand Down Expand Up @@ -1593,11 +1593,11 @@ void MaterialSystem::UpdateDynamicSurfaces() {
}

void MaterialSystem::UpdateFrameData() {
atomicCommandCountersBuffer.BindBufferBase( GL_SHADER_STORAGE_BUFFER );
atomicCommandCountersBuffer.BindBufferBase( GL_SHADER_STORAGE_BUFFER, Util::ordinal( BufferBind::COMMAND_COUNTERS_STORAGE ) );
gl_clearSurfacesShader->BindProgram( 0 );
gl_clearSurfacesShader->SetUniform_Frame( nextFrame );
gl_clearSurfacesShader->DispatchCompute( MAX_VIEWS, 1, 1 );
atomicCommandCountersBuffer.UnBindBufferBase( GL_SHADER_STORAGE_BUFFER );
atomicCommandCountersBuffer.UnBindBufferBase( GL_SHADER_STORAGE_BUFFER, Util::ordinal( BufferBind::COMMAND_COUNTERS_STORAGE ) );

GL_CheckErrors();
}
Expand Down
14 changes: 14 additions & 0 deletions src/engine/renderer/Material.h
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,20 @@ struct SurfaceCommandBatch {
uint32_t materialIDs[2] { 0, 0 };
};

enum class BufferBind {
MATERIALS = 1, // LightTile UBO uses binding point 0, so avoid it here
TEX_DATA = 6,
LIGHTMAP_DATA = 2,
SURFACE_DESCRIPTORS = 0,
SURFACE_COMMANDS = 1,
CULLED_COMMANDS = 2,
SURFACE_BATCHES = 3,
COMMAND_COUNTERS_ATOMIC = 0,
COMMAND_COUNTERS_STORAGE = 4, // Avoid needlessly rebinding buffers
PORTAL_SURFACES = 5,
DEBUG = 10
};

class MaterialSystem {
public:
bool generatedWorldCommandBuffer = false;
Expand Down
46 changes: 38 additions & 8 deletions src/engine/renderer/gl_shader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,12 @@ static std::string GenVertexHeader() {
str += "#define baseInstance gl_BaseInstanceARB\n\n";
}

if ( glConfig2.usingMaterialSystem ) {
AddDefine( str, "BIND_MATERIALS", Util::ordinal( BufferBind::MATERIALS ) );
AddDefine( str, "BIND_TEX_DATA", Util::ordinal( BufferBind::TEX_DATA ) );
AddDefine( str, "BIND_LIGHTMAP_DATA", Util::ordinal( BufferBind::LIGHTMAP_DATA ) );
}

return str;
}

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

if ( glConfig2.usingMaterialSystem ) {
AddDefine( str, "BIND_MATERIALS", Util::ordinal( BufferBind::MATERIALS ) );
AddDefine( str, "BIND_TEX_DATA", Util::ordinal( BufferBind::TEX_DATA ) );
AddDefine( str, "BIND_LIGHTMAP_DATA", Util::ordinal( BufferBind::LIGHTMAP_DATA ) );
}

return str;
}

static std::string GenComputeHeader() {
std::string str;

// Compute shader compatibility defines
AddDefine( str, "MAX_VIEWS", MAX_VIEWS );
AddDefine( str, "MAX_FRAMES", MAX_FRAMES );
AddDefine( str, "MAX_VIEWFRAMES", MAX_VIEWFRAMES );
AddDefine( str, "MAX_SURFACE_COMMAND_BATCHES", MAX_SURFACE_COMMAND_BATCHES );
AddDefine( str, "MAX_COMMAND_COUNTERS", MAX_COMMAND_COUNTERS );
if ( glConfig2.usingMaterialSystem ) {
AddDefine( str, "MAX_VIEWS", MAX_VIEWS );
AddDefine( str, "MAX_FRAMES", MAX_FRAMES );
AddDefine( str, "MAX_VIEWFRAMES", MAX_VIEWFRAMES );
AddDefine( str, "MAX_SURFACE_COMMAND_BATCHES", MAX_SURFACE_COMMAND_BATCHES );
AddDefine( str, "MAX_COMMAND_COUNTERS", MAX_COMMAND_COUNTERS );

AddDefine( str, "BIND_SURFACE_DESCRIPTORS", Util::ordinal( BufferBind::SURFACE_DESCRIPTORS ) );
AddDefine( str, "BIND_SURFACE_COMMANDS", Util::ordinal( BufferBind::SURFACE_COMMANDS ) );
AddDefine( str, "BIND_CULLED_COMMANDS", Util::ordinal( BufferBind::CULLED_COMMANDS ) );
AddDefine( str, "BIND_SURFACE_BATCHES", Util::ordinal( BufferBind::SURFACE_BATCHES ) );
AddDefine( str, "BIND_COMMAND_COUNTERS_ATOMIC", Util::ordinal( BufferBind::COMMAND_COUNTERS_ATOMIC ) );
AddDefine( str, "BIND_COMMAND_COUNTERS_STORAGE", Util::ordinal( BufferBind::COMMAND_COUNTERS_STORAGE ) );
AddDefine( str, "BIND_PORTAL_SURFACES", Util::ordinal( BufferBind::PORTAL_SURFACES ) );

AddDefine( str, "BIND_DEBUG", Util::ordinal( BufferBind::DEBUG ) );
}

if ( glConfig2.usingBindlessTextures ) {
str += "layout(bindless_image) uniform;\n";
Expand Down Expand Up @@ -1361,15 +1385,21 @@ std::string GLShaderManager::ShaderPostProcess( GLShader *shader, const std::str
std::string materialStruct = "\nstruct Material {\n";
// 6 kb for materials
const uint32_t count = ( 4096 + 2048 ) / shader->GetPaddedSize();
std::string materialBlock = "layout(std140, binding = 6) uniform materialsUBO {\n"
std::string materialBlock = "layout(std140, binding = "
+ std::to_string( Util::ordinal( BufferBind::MATERIALS ) )
+ ") uniform materialsUBO {\n"
" Material materials[" + std::to_string( count ) + "]; \n"
"};\n\n";

std::string texBuf = glConfig2.maxUniformBlockSize >= MIN_MATERIAL_UBO_SIZE ?
"layout(std140, binding = 7) uniform texDataUBO {\n"
"layout(std140, binding = "
+ std::to_string( Util::ordinal( BufferBind::TEX_DATA ) )
+ ") uniform texDataUBO {\n"
" TexData texData[" + std::to_string( MAX_TEX_BUNDLES ) + "]; \n"
"};\n\n"
: "layout(std430, binding = 7) restrict readonly buffer texDataSSBO {\n"
: "layout(std430, binding = "
+ std::to_string( Util::ordinal( BufferBind::TEX_DATA ) )
+ ") restrict readonly buffer texDataSSBO {\n"
" TexData texData[];\n"
"};\n\n";
// We have to store u_TextureMatrix as vec4 + vec2 because otherwise it would be aligned to a vec4 under std140
Expand Down
2 changes: 1 addition & 1 deletion src/engine/renderer/glsl_source/clearSurfaces_cp.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

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

layout(std430, binding = 4) writeonly buffer atomicCommandCountersBuffer {
layout(std430, binding = BIND_COMMAND_COUNTERS_STORAGE) writeonly buffer atomicCommandCountersBuffer {
uint atomicCommandCounters[MAX_COMMAND_COUNTERS * MAX_VIEWFRAMES];
};

Expand Down
8 changes: 4 additions & 4 deletions src/engine/renderer/glsl_source/cull_cp.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -41,23 +41,23 @@ layout (local_size_x = 64, local_size_y = 1, local_size_z = 1) in;

layout(binding = 0) uniform sampler2D depthImage;

layout(std430, binding = 1) readonly restrict buffer surfaceDescriptorsSSBO {
layout(std430, binding = BIND_SURFACE_DESCRIPTORS) readonly restrict buffer surfaceDescriptorsSSBO {
SurfaceDescriptor surfaces[];
};

layout(std430, binding = 2) writeonly restrict buffer surfaceCommandsSSBO {
layout(std430, binding = BIND_SURFACE_COMMANDS) writeonly restrict buffer surfaceCommandsSSBO {
SurfaceCommand surfaceCommands[];
};

layout(std430, binding = 5) restrict buffer portalSurfacesSSBO {
layout(std430, binding = BIND_PORTAL_SURFACES) restrict buffer portalSurfacesSSBO {
PortalSurface portalSurfaces[];
};

#if defined( r_materialDebug )
#define DEBUG_INVOCATION_SIZE 5
#define DEBUG_ID( id ) ( id * DEBUG_INVOCATION_SIZE )

layout(std430, binding = 10) writeonly restrict buffer debugSSBO {
layout(std430, binding = BIND_DEBUG) writeonly restrict buffer debugSSBO {
uvec4 debug[];
};
#endif
Expand Down
8 changes: 4 additions & 4 deletions src/engine/renderer/glsl_source/processSurfaces_cp.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,19 @@ layout (local_size_x = 64, local_size_y = 1, local_size_z = 1) in;

#define SurfaceCommandBatch uvec4

layout(std430, binding = 2) readonly restrict buffer surfaceCommandsSSBO {
layout(std430, binding = BIND_SURFACE_COMMANDS) readonly restrict buffer surfaceCommandsSSBO {
SurfaceCommand surfaceCommands[];
};

layout(std430, binding = 3) writeonly restrict buffer culledCommandsSSBO {
layout(std430, binding = BIND_CULLED_COMMANDS) writeonly restrict buffer culledCommandsSSBO {
GLIndirectCommand culledCommands[];
};

layout(std140, binding = 0) uniform ub_SurfaceBatches {
layout(std140, binding = BIND_SURFACE_BATCHES) uniform ub_SurfaceBatches {
SurfaceCommandBatch surfaceBatches[MAX_SURFACE_COMMAND_BATCHES];
};

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

uniform uint u_Frame;
uniform uint u_ViewID;
Expand Down

0 comments on commit 8c10245

Please sign in to comment.