Skip to content

Commit 94bd75a

Browse files
committed
Refactor: treat shader deform index like a GLSL macro
Instead of passing the deform index as a BindProgram argument, use a SetDeform function that stores the deform index in the shader object. This makes it work the same way as setting GLSL macro options (e.g. SetVertexSkining), which makes sense because the deform index is also an option that determines which variant of the GLSL shader gets compiled. SetDeform is a member of GLDeformStage, so you are no longer allowed and no longer required to pass a deform index to GLSL shaders that don't use deforms. This eliminates a fair amount of plumbing of the deform index through functions that shouldn't have to care about it.
1 parent 527e8d7 commit 94bd75a

File tree

8 files changed

+153
-120
lines changed

8 files changed

+153
-120
lines changed

src/engine/renderer/GeometryOptimiser.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -85,39 +85,39 @@ void MarkShaderBuildNOP( const shaderStage_t* ) {
8585

8686
void MarkShaderBuildGeneric3D( const shaderStage_t* pStage ) {
8787
ProcessShaderGeneric3D( pStage );
88-
gl_genericShader->MarkProgramForBuilding( pStage->deformIndex );
88+
gl_genericShader->MarkProgramForBuilding();
8989
}
9090

9191
void MarkShaderBuildLightMapping( const shaderStage_t* pStage ) {
9292
ProcessShaderLightMapping( pStage );
93-
gl_lightMappingShader->MarkProgramForBuilding( pStage->deformIndex );
93+
gl_lightMappingShader->MarkProgramForBuilding();
9494
}
9595

9696
void MarkShaderBuildReflection( const shaderStage_t* pStage ) {
9797
ProcessShaderReflection( pStage );
98-
gl_reflectionShader->MarkProgramForBuilding( pStage->deformIndex );
98+
gl_reflectionShader->MarkProgramForBuilding();
9999
}
100100

101-
void MarkShaderBuildSkybox( const shaderStage_t* pStage ) {
102-
gl_skyboxShader->MarkProgramForBuilding( pStage->deformIndex );
101+
void MarkShaderBuildSkybox( const shaderStage_t* ) {
102+
gl_skyboxShader->MarkProgramForBuilding();
103103
}
104104

105-
void MarkShaderBuildScreen( const shaderStage_t* pStage ) {
106-
gl_screenShader->MarkProgramForBuilding( pStage->deformIndex );
105+
void MarkShaderBuildScreen( const shaderStage_t* ) {
106+
gl_screenShader->MarkProgramForBuilding();
107107
}
108108

109-
void MarkShaderBuildPortal( const shaderStage_t* pStage ) {
110-
gl_portalShader->MarkProgramForBuilding( pStage->deformIndex );
109+
void MarkShaderBuildPortal( const shaderStage_t* ) {
110+
gl_portalShader->MarkProgramForBuilding();
111111
}
112112

113113
void MarkShaderBuildHeatHaze( const shaderStage_t* pStage ) {
114114
ProcessShaderHeatHaze( pStage );
115-
gl_heatHazeShader->MarkProgramForBuilding( pStage->deformIndex );
115+
gl_heatHazeShader->MarkProgramForBuilding();
116116
}
117117

118118
void MarkShaderBuildLiquid( const shaderStage_t* pStage ) {
119119
ProcessShaderLiquid( pStage );
120-
gl_liquidShader->MarkProgramForBuilding( pStage->deformIndex );
120+
gl_liquidShader->MarkProgramForBuilding();
121121
}
122122

123123
void MarkShaderBuild( shader_t* shader, const int lightMapNum, const bool bspSurface,

src/engine/renderer/Material.cpp

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -891,9 +891,10 @@ void BindShaderGeneric3D( Material* material ) {
891891
gl_genericShaderMaterial->SetTCGenEnvironment( material->tcGenEnvironment );
892892
gl_genericShaderMaterial->SetTCGenLightmap( material->tcGen_Lightmap );
893893
gl_genericShaderMaterial->SetDepthFade( material->hasDepthFade );
894+
gl_genericShaderMaterial->SetDeform( material->deformIndex );
894895

895896
// Bind shader program.
896-
gl_genericShaderMaterial->BindProgram( material->deformIndex );
897+
gl_genericShaderMaterial->BindProgram();
897898

898899
// Set shader uniforms.
899900
if ( material->tcGenEnvironment ) {
@@ -926,9 +927,10 @@ void BindShaderLightMapping( Material* material ) {
926927
because we don't have cubemaps built yet at this point, but for the purposes of the material ordering there's no difference */
927928
gl_lightMappingShaderMaterial->SetReflectiveSpecular( glConfig.reflectionMapping && material->enableSpecularMapping && !( tr.refdef.rdflags & RDF_NOCUBEMAP ) );
928929
gl_lightMappingShaderMaterial->SetPhysicalShading( material->enablePhysicalMapping );
930+
gl_lightMappingShaderMaterial->SetDeform( material->deformIndex );
929931

930932
// Bind shader program.
931-
gl_lightMappingShaderMaterial->BindProgram( material->deformIndex );
933+
gl_lightMappingShaderMaterial->BindProgram();
932934

933935
// Set shader uniforms.
934936
if ( tr.world ) {
@@ -1011,35 +1013,37 @@ void BindShaderReflection( Material* material ) {
10111013
// Select shader permutation.
10121014
gl_reflectionShaderMaterial->SetHeightMapInNormalMap( material->hasHeightMapInNormalMap );
10131015
gl_reflectionShaderMaterial->SetReliefMapping( material->enableReliefMapping );
1016+
gl_reflectionShaderMaterial->SetDeform( material->deformIndex );
10141017

10151018
// Bind shader program.
1016-
gl_reflectionShaderMaterial->BindProgram( material->deformIndex );
1019+
gl_reflectionShaderMaterial->BindProgram();
10171020

10181021
// Set shader uniforms.
10191022
gl_reflectionShaderMaterial->SetUniform_ViewOrigin( backEnd.viewParms.orientation.origin );
10201023
gl_reflectionShaderMaterial->SetUniform_ModelMatrix( backEnd.orientation.transformMatrix );
10211024
gl_reflectionShaderMaterial->SetUniform_ModelViewProjectionMatrix( glState.modelViewProjectionMatrix[glState.stackIndex] );
10221025
}
10231026

1024-
void BindShaderSkybox( Material* material ) {
1027+
void BindShaderSkybox( Material* ) {
10251028
// Bind shader program.
1026-
gl_skyboxShaderMaterial->BindProgram( material->deformIndex );
1029+
gl_skyboxShaderMaterial->BindProgram();
10271030

10281031
// Set shader uniforms.
10291032
gl_skyboxShaderMaterial->SetUniform_ModelViewProjectionMatrix( glState.modelViewProjectionMatrix[glState.stackIndex] );
10301033
}
10311034

1032-
void BindShaderScreen( Material* material ) {
1035+
void BindShaderScreen( Material* ) {
10331036
// Bind shader program.
1034-
gl_screenShaderMaterial->BindProgram( material->deformIndex );
1037+
gl_screenShaderMaterial->BindProgram();
10351038

10361039
// Set shader uniforms.
10371040
gl_screenShaderMaterial->SetUniform_ModelViewProjectionMatrix( glState.modelViewProjectionMatrix[glState.stackIndex] );
10381041
}
10391042

10401043
void BindShaderHeatHaze( Material* material ) {
10411044
// Bind shader program.
1042-
gl_heatHazeShaderMaterial->BindProgram( material->deformIndex );
1045+
gl_heatHazeShaderMaterial->SetDeform( material->deformIndex );
1046+
gl_heatHazeShaderMaterial->BindProgram();
10431047

10441048
// Set shader uniforms.
10451049
gl_heatHazeShaderMaterial->SetUniform_ModelViewProjectionMatrix( glState.modelViewProjectionMatrix[glState.stackIndex] );
@@ -1067,7 +1071,7 @@ void BindShaderLiquid( Material* material ) {
10671071
gl_liquidShaderMaterial->SetGridLighting( material->enableGridLighting );
10681072

10691073
// Bind shader program.
1070-
gl_liquidShaderMaterial->BindProgram( material->deformIndex );
1074+
gl_liquidShaderMaterial->BindProgram();
10711075

10721076
// Set shader uniforms.
10731077
gl_liquidShaderMaterial->SetUniform_ViewOrigin( backEnd.viewParms.orientation.origin );
@@ -1084,7 +1088,8 @@ void BindShaderLiquid( Material* material ) {
10841088

10851089
void BindShaderFog( Material* material ) {
10861090
// Bind shader program.
1087-
gl_fogQuake3ShaderMaterial->BindProgram( material->deformIndex );
1091+
gl_fogQuake3ShaderMaterial->SetDeform( material->deformIndex );
1092+
gl_fogQuake3ShaderMaterial->BindProgram();
10881093

10891094
// Set shader uniforms.
10901095
const fog_t* fog = tr.world->fogs + material->fog;
@@ -1140,7 +1145,9 @@ void ProcessMaterialGeneric3D( Material* material, shaderStage_t* pStage, Materi
11401145
material->hasDepthFade = hasDepthFade;
11411146
gl_genericShaderMaterial->SetDepthFade( hasDepthFade );
11421147

1143-
material->program = gl_genericShaderMaterial->GetProgram( pStage->deformIndex, materialSystem.buildOneShader );
1148+
gl_genericShaderMaterial->SetDeform( pStage->deformIndex );
1149+
1150+
material->program = gl_genericShaderMaterial->GetProgram( materialSystem.buildOneShader );
11441151
}
11451152

11461153
void ProcessMaterialLightMapping( Material* material, shaderStage_t* pStage, MaterialSurface* surface ) {
@@ -1181,7 +1188,9 @@ void ProcessMaterialLightMapping( Material* material, shaderStage_t* pStage, Mat
11811188

11821189
gl_lightMappingShaderMaterial->SetPhysicalShading( pStage->enablePhysicalMapping );
11831190

1184-
material->program = gl_lightMappingShaderMaterial->GetProgram( pStage->deformIndex, materialSystem.buildOneShader );
1191+
gl_lightMappingShaderMaterial->SetDeform( pStage->deformIndex );
1192+
1193+
material->program = gl_lightMappingShaderMaterial->GetProgram( materialSystem.buildOneShader );
11851194
}
11861195

11871196
void ProcessMaterialReflection( Material* material, shaderStage_t* pStage, MaterialSurface* /* surface */ ) {
@@ -1195,31 +1204,35 @@ void ProcessMaterialReflection( Material* material, shaderStage_t* pStage, Mater
11951204

11961205
gl_reflectionShaderMaterial->SetReliefMapping( pStage->enableReliefMapping );
11971206

1198-
material->program = gl_reflectionShaderMaterial->GetProgram( pStage->deformIndex, materialSystem.buildOneShader );
1207+
gl_reflectionShaderMaterial->SetDeform( pStage->deformIndex );
1208+
1209+
material->program = gl_reflectionShaderMaterial->GetProgram( materialSystem.buildOneShader );
11991210
}
12001211

12011212
void ProcessMaterialSkybox( Material* material, shaderStage_t* pStage, MaterialSurface* /* surface */ ) {
12021213
material->shader = gl_skyboxShaderMaterial;
12031214

12041215
material->deformIndex = pStage->deformIndex;
12051216

1206-
material->program = gl_skyboxShaderMaterial->GetProgram( pStage->deformIndex, materialSystem.buildOneShader );
1217+
material->program = gl_skyboxShaderMaterial->GetProgram( materialSystem.buildOneShader );
12071218
}
12081219

12091220
void ProcessMaterialScreen( Material* material, shaderStage_t* pStage, MaterialSurface* /* surface */ ) {
12101221
material->shader = gl_screenShaderMaterial;
12111222

12121223
material->deformIndex = pStage->deformIndex;
12131224

1214-
material->program = gl_screenShaderMaterial->GetProgram( pStage->deformIndex, materialSystem.buildOneShader );
1225+
material->program = gl_screenShaderMaterial->GetProgram( materialSystem.buildOneShader );
12151226
}
12161227

12171228
void ProcessMaterialHeatHaze( Material* material, shaderStage_t* pStage, MaterialSurface* ) {
12181229
material->shader = gl_heatHazeShaderMaterial;
12191230

12201231
material->deformIndex = pStage->deformIndex;
12211232

1222-
material->program = gl_heatHazeShaderMaterial->GetProgram( pStage->deformIndex, materialSystem.buildOneShader );
1233+
gl_heatHazeShaderMaterial->SetDeform( pStage->deformIndex );
1234+
1235+
material->program = gl_heatHazeShaderMaterial->GetProgram( materialSystem.buildOneShader );
12231236
}
12241237

12251238
void ProcessMaterialLiquid( Material* material, shaderStage_t* pStage, MaterialSurface* surface ) {
@@ -1243,14 +1256,16 @@ void ProcessMaterialLiquid( Material* material, shaderStage_t* pStage, MaterialS
12431256

12441257
gl_liquidShaderMaterial->SetGridLighting( lightMode == lightMode_t::GRID );
12451258

1246-
material->program = gl_liquidShaderMaterial->GetProgram( pStage->deformIndex, materialSystem.buildOneShader );
1259+
material->program = gl_liquidShaderMaterial->GetProgram( materialSystem.buildOneShader );
12471260
}
12481261

12491262
void ProcessMaterialFog( Material* material, shaderStage_t* pStage, MaterialSurface* surface ) {
12501263
material->shader = gl_fogQuake3ShaderMaterial;
12511264
material->fog = surface->fog;
12521265

1253-
material->program = gl_fogQuake3ShaderMaterial->GetProgram( pStage->deformIndex, materialSystem.buildOneShader );
1266+
gl_fogQuake3ShaderMaterial->SetDeform( pStage->deformIndex );
1267+
1268+
material->program = gl_fogQuake3ShaderMaterial->GetProgram( materialSystem.buildOneShader );
12541269
}
12551270

12561271
void MaterialSystem::AddStage( MaterialSurface* surface, shaderStage_t* pStage, uint32_t stage,
@@ -1607,7 +1622,7 @@ void MaterialSystem::UpdateDynamicSurfaces() {
16071622
}
16081623

16091624
void MaterialSystem::UpdateFrameData() {
1610-
gl_clearSurfacesShader->BindProgram( 0 );
1625+
gl_clearSurfacesShader->BindProgram();
16111626
gl_clearSurfacesShader->SetUniform_Frame( nextFrame );
16121627
gl_clearSurfacesShader->DispatchCompute( MAX_VIEWS, 1, 1 );
16131628

@@ -1632,7 +1647,7 @@ void MaterialSystem::DepthReduction() {
16321647
int width = depthImage->width;
16331648
int height = depthImage->height;
16341649

1635-
gl_depthReductionShader->BindProgram( 0 );
1650+
gl_depthReductionShader->BindProgram();
16361651

16371652
uint32_t globalWorkgroupX = ( width + 7 ) / 8;
16381653
uint32_t globalWorkgroupY = ( height + 7 ) / 8;
@@ -1690,7 +1705,7 @@ void MaterialSystem::CullSurfaces() {
16901705
MatrixCopy( backEnd.viewParms.world.modelViewMatrix, viewMatrix );
16911706
}
16921707

1693-
gl_cullShader->BindProgram( 0 );
1708+
gl_cullShader->BindProgram();
16941709
uint32_t globalWorkGroupX = surfaceDescriptorsCount % MAX_COMMAND_COUNTERS == 0 ?
16951710
surfaceDescriptorsCount / MAX_COMMAND_COUNTERS : surfaceDescriptorsCount / MAX_COMMAND_COUNTERS + 1;
16961711
GL_Bind( depthImage );
@@ -1733,7 +1748,7 @@ void MaterialSystem::CullSurfaces() {
17331748

17341749
gl_cullShader->DispatchCompute( globalWorkGroupX, 1, 1 );
17351750

1736-
gl_processSurfacesShader->BindProgram( 0 );
1751+
gl_processSurfacesShader->BindProgram();
17371752
gl_processSurfacesShader->SetUniform_Frame( nextFrame );
17381753
gl_processSurfacesShader->SetUniform_ViewID( view );
17391754
gl_processSurfacesShader->SetUniform_SurfaceCommandsOffset( surfaceCommandsCount * ( MAX_VIEWS * nextFrame + view ) );

src/engine/renderer/gl_shader.cpp

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,11 +1040,9 @@ ShaderProgramDescriptor* GLShaderManager::FindShaderProgram( std::vector<ShaderE
10401040
return &*it;
10411041
}
10421042

1043-
bool GLShaderManager::BuildPermutation( GLShader* shader, int macroIndex, int deformIndex, const bool buildOneShader ) {
1044-
size_t i = macroIndex + ( deformIndex << shader->_compileMacros.size() );
1045-
1043+
bool GLShaderManager::BuildPermutation( GLShader* shader, int index, const bool buildOneShader ) {
10461044
std::string compileMacros;
1047-
if ( !shader->GetCompileMacrosString( i, compileMacros, GLCompileMacro::VERTEX | GLCompileMacro::FRAGMENT ) ) {
1045+
if ( !shader->GetCompileMacrosString( index, compileMacros, GLCompileMacro::VERTEX | GLCompileMacro::FRAGMENT ) ) {
10481046
return false;
10491047
}
10501048

@@ -1053,8 +1051,8 @@ bool GLShaderManager::BuildPermutation( GLShader* shader, int macroIndex, int de
10531051
}
10541052

10551053
// Program already exists
1056-
if ( i < shader->shaderPrograms.size() &&
1057-
shader->shaderPrograms[i].id ) {
1054+
if ( index < shader->shaderPrograms.size() &&
1055+
shader->shaderPrograms[index].id ) {
10581056
return false;
10591057
}
10601058

@@ -1076,7 +1074,10 @@ bool GLShaderManager::BuildPermutation( GLShader* shader, int macroIndex, int de
10761074

10771075
const int start = Sys::Milliseconds();
10781076

1079-
if ( i >= shader->shaderPrograms.size() ) {
1077+
int macroIndex = index & ( ( 1 << shader->_compileMacros.size() ) - 1 );
1078+
int deformIndex = index >> shader->_compileMacros.size();
1079+
1080+
if ( index >= shader->shaderPrograms.size() ) {
10801081
shader->shaderPrograms.resize( ( deformIndex + 1 ) << shader->_compileMacros.size() );
10811082
}
10821083

@@ -1104,7 +1105,7 @@ bool GLShaderManager::BuildPermutation( GLShader* shader, int macroIndex, int de
11041105
GL_BindNullProgram();
11051106

11061107
// Copy this for a fast look-up, but the values held in program aren't supposed to change after
1107-
shader->shaderPrograms[i] = *program;
1108+
shader->shaderPrograms[index] = *program;
11081109

11091110
GL_CheckErrors();
11101111

@@ -1144,17 +1145,15 @@ void GLShaderManager::BuildAll( const bool buildOnlyMarked ) {
11441145
if ( buildOnlyMarked ) {
11451146
for ( size_t i = 0; i < shader->shaderProgramsToBuild.size(); i++ ) {
11461147
if ( shader->shaderProgramsToBuild[i] ) {
1147-
const int macroIndex = i & ( ( 1u << shader->GetNumOfCompiledMacros() ) - 1 );
1148-
const int deformIndex = i >> shader->GetNumOfCompiledMacros();
1149-
1150-
count += +BuildPermutation( shader, macroIndex, deformIndex, false );
1148+
count += +BuildPermutation( shader, i, false );
11511149
}
11521150
}
11531151
} else {
11541152
size_t numPermutations = static_cast<size_t>( 1 ) << shader->GetNumOfCompiledMacros();
11551153

11561154
for ( size_t i = 0; i < numPermutations; i++ ) {
1157-
count += +BuildPermutation( shader, i, 0, false );
1155+
// doesn't include deform vertex shaders, those are built elsewhere!
1156+
count += +BuildPermutation( shader, i, false );
11581157
}
11591158
}
11601159

@@ -2192,38 +2191,36 @@ int GLShader::SelectProgram()
21922191
{
21932192
int index = 0;
21942193

2195-
size_t numMacros = _compileMacros.size();
2194+
int numMacros = static_cast<int>( _compileMacros.size() );
21962195

2197-
for ( size_t i = 0; i < numMacros; i++ )
2196+
for ( int i = 0; i < numMacros; i++ )
21982197
{
21992198
if ( _activeMacros & BIT( i ) )
22002199
{
22012200
index += BIT( i );
22022201
}
22032202
}
22042203

2205-
return index;
2204+
return index | ( _deformIndex << numMacros );
22062205
}
22072206

2208-
void GLShader::MarkProgramForBuilding( int deformIndex ) {
2209-
int macroIndex = SelectProgram();
2210-
size_t index = macroIndex + ( size_t( deformIndex ) << _compileMacros.size() );
2207+
void GLShader::MarkProgramForBuilding() {
2208+
int index = SelectProgram();
22112209

2212-
if ( index >= shaderProgramsToBuild.size() ) {
2210+
if ( size_t(index) >= shaderProgramsToBuild.size() ) {
22132211
shaderProgramsToBuild.resize( index + 1 );
22142212
}
22152213

22162214
shaderProgramsToBuild[index] = true;
22172215
}
22182216

2219-
GLuint GLShader::GetProgram( int deformIndex, const bool buildOneShader ) {
2220-
int macroIndex = SelectProgram();
2221-
size_t index = macroIndex + ( size_t( deformIndex ) << _compileMacros.size() );
2217+
GLuint GLShader::GetProgram( const bool buildOneShader ) {
2218+
int index = SelectProgram();
22222219

22232220
// program may not be loaded yet because the shader manager hasn't yet gotten to it
22242221
// so try to load it now
22252222
if ( index >= shaderPrograms.size() || !shaderPrograms[index].id ) {
2226-
gl_shaderManager.BuildPermutation( this, macroIndex, deformIndex, buildOneShader );
2223+
gl_shaderManager.BuildPermutation( this, index, buildOneShader );
22272224
}
22282225

22292226
// program is still not loaded
@@ -2248,15 +2245,14 @@ GLuint GLShader::GetProgram( int deformIndex, const bool buildOneShader ) {
22482245
return shaderPrograms[index].id;
22492246
}
22502247

2251-
void GLShader::BindProgram( int deformIndex ) {
2252-
int macroIndex = SelectProgram();
2253-
size_t index = macroIndex + ( size_t(deformIndex) << _compileMacros.size() );
2248+
void GLShader::BindProgram() {
2249+
int index = SelectProgram();
22542250

22552251
// program may not be loaded yet because the shader manager hasn't yet gotten to it
22562252
// so try to load it now
22572253
if ( index >= shaderPrograms.size() || !shaderPrograms[index].id )
22582254
{
2259-
gl_shaderManager.BuildPermutation( this, macroIndex, deformIndex, true );
2255+
gl_shaderManager.BuildPermutation( this, index, true );
22602256
}
22612257

22622258
// program is still not loaded

0 commit comments

Comments
 (0)