Skip to content

Commit 6dbbd9b

Browse files
committed
Use explicit bind for u_Lights UBO when possible
Get rid of the requirement to not use binding point 0 for UBOs explicitly.
1 parent d9e756c commit 6dbbd9b

File tree

5 files changed

+28
-8
lines changed

5 files changed

+28
-8
lines changed

src/engine/renderer/Material.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,9 +308,10 @@ struct SurfaceCommandBatch {
308308

309309
namespace BufferBind {
310310
enum : uint32_t {
311-
MATERIALS = 1, // LightTile UBO uses binding point 0, so avoid it here
311+
MATERIALS = 1,
312312
TEX_DATA = 6,
313313
LIGHTMAP_DATA = 2,
314+
LIGHTS = 0,
314315
SURFACE_DESCRIPTORS = 0,
315316
SURFACE_COMMANDS = 1,
316317
CULLED_COMMANDS = 2,

src/engine/renderer/gl_shader.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ void GLShaderManager::UpdateShaderProgramUniformLocations( GLShader* shader, Sha
286286
uniform->UpdateShaderProgramUniformLocation( shaderProgram );
287287
}
288288

289-
if( glConfig2.uniformBufferObjectAvailable ) {
289+
if( glConfig2.uniformBufferObjectAvailable && !glConfig2.shadingLanguage420PackAvailable ) {
290290
// create buffer for storing uniform block indexes
291291
shaderProgram->uniformBlockIndexes = ( GLuint* ) Z_Malloc( sizeof( GLuint ) * numUniformBlocks );
292292

@@ -416,6 +416,7 @@ struct addedExtension_t {
416416
static const std::vector<addedExtension_t> fragmentVertexAddedExtensions = {
417417
{ glConfig2.gpuShader4Available, 130, "EXT_gpu_shader4" },
418418
{ glConfig2.gpuShader5Available, 400, "ARB_gpu_shader5" },
419+
{ glConfig2.shadingLanguage420PackAvailable, 420, "ARB_shading_language_420pack" },
419420
{ glConfig2.textureFloatAvailable, 130, "ARB_texture_float" },
420421
{ glConfig2.textureGatherAvailable, 400, "ARB_texture_gather" },
421422
{ glConfig2.textureIntegerAvailable, 0, "EXT_texture_integer" },
@@ -620,6 +621,10 @@ static std::string GenFragmentHeader() {
620621
str += "#define baseInstance in_baseInstance\n\n";
621622
}
622623

624+
if ( glConfig2.shadingLanguage420PackAvailable ) {
625+
AddDefine( str, "BIND_LIGHTS", BufferBind::LIGHTS );
626+
}
627+
623628
if ( glConfig2.usingMaterialSystem ) {
624629
AddDefine( str, "BIND_MATERIALS", BufferBind::MATERIALS );
625630
AddDefine( str, "BIND_TEX_DATA", BufferBind::TEX_DATA );

src/engine/renderer/gl_shader.h

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1231,12 +1231,13 @@ class GLUniformBlock
12311231
protected:
12321232
GLShader *_shader;
12331233
std::string _name;
1234-
size_t _locationIndex;
1234+
size_t _locationIndex; // Only valid if GL_ARB_shading_language_420pack is not available
1235+
const GLuint _bindingPoint; // Only valid if GL_ARB_shading_language_420pack is available
12351236

1236-
GLUniformBlock( GLShader *shader, const char *name ) :
1237+
GLUniformBlock( GLShader *shader, const char *name, const GLuint bindingPoint ) :
12371238
_shader( shader ),
12381239
_name( name ),
1239-
_locationIndex( 0 )
1240+
_bindingPoint( bindingPoint )
12401241
{
12411242
_shader->RegisterUniformBlock( this );
12421243
}
@@ -1258,10 +1259,15 @@ class GLUniformBlock
12581259
}
12591260

12601261
void SetBuffer( GLuint buffer ) {
1262+
if ( glConfig2.shadingLanguage420PackAvailable ) {
1263+
glBindBufferBase( GL_UNIFORM_BUFFER, _bindingPoint, buffer );
1264+
return;
1265+
}
1266+
12611267
ShaderProgramDescriptor *p = _shader->GetProgram();
1262-
GLuint blockIndex = p->uniformBlockIndexes[ _locationIndex ];
1268+
GLuint blockIndex = p->uniformBlockIndexes[_locationIndex];
12631269

1264-
ASSERT_EQ(p, glState.currentProgram);
1270+
ASSERT_EQ( p, glState.currentProgram );
12651271

12661272
if( blockIndex != GL_INVALID_INDEX ) {
12671273
glBindBufferBase( GL_UNIFORM_BUFFER, blockIndex, buffer );
@@ -3599,7 +3605,7 @@ class u_Lights :
35993605
{
36003606
public:
36013607
u_Lights( GLShader *shader ) :
3602-
GLUniformBlock( shader, "u_Lights" )
3608+
GLUniformBlock( shader, "u_Lights", BufferBind::LIGHTS )
36033609
{
36043610
}
36053611

src/engine/renderer/glsl_source/computeLight_fp.glsl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,11 @@ struct Light {
153153
float angle;
154154
};
155155

156+
#if defined( HAVE_ARB_shading_language_420pack )
157+
layout(std140, binding = BIND_LIGHTS) uniform u_Lights {
158+
#else
156159
layout(std140) uniform u_Lights {
160+
#endif
157161
Light lights[MAX_REF_LIGHTS];
158162
};
159163

src/engine/renderer/glsl_source/lighttile_fp.glsl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,11 @@ struct Light {
4545
float angle;
4646
};
4747

48+
#if defined( HAVE_ARB_shading_language_420pack )
49+
layout(std140, binding = BIND_LIGHTS) uniform u_Lights {
50+
#else
4851
layout(std140) uniform u_Lights {
52+
#endif
4953
Light lights[MAX_REF_LIGHTS];
5054
};
5155

0 commit comments

Comments
 (0)