Skip to content

Commit ab59550

Browse files
committed
Improve shader building
Previously, the GLSL shader building was done in the following manner: 1. When the renderer is started, some of the shader source was initialised/processed. This included the main shader text, #insert's, and material system post-processing. 2. When `buildAll()` was called, all permutations with empty deform shaders were built. The deform shaders themselves are loaded either at the start (for the default, empty one), or when the map itself is being loaded. These permutations included adding macros to shaders in the form of `#ifndef #define #endif`, and attaching the default deform shader. When possible, shader program binaries were saved/loaded to/from disk. 3. UI shader and non-0 deform shaders were built on-demand. There were multiple issues with that system: 1. Shader caches were unreliable. This is likely because the checksum was calculated *before* macros and such were added to the shader text. 2. Every shader permutation was always compiled and linked every time. 3. Shader programs with non-0 deformVertexes were never cached. This commit changes the underlying system to use an `std::vector` of shader program descriptors, and another one for shader descriptors. The initialisation/building of shaders is now more generic for each type, and as a result each unique shader is only compiled once. The permutations are still linked each time (unless downloaded from cache), but this change lays the foundation to easily add shader pipelines, which would solve this problem. Shader cache binaries are now identified by each shader compiled into the corresponding shader program, which allows caching non-0 deformVertexes too. This also fixes the incorrect #line numbers emitted by #insert due to macros and such being added after #insert is processed. Also adds more information into how many shader/shader programs were built and how much time it took (there's currently a small discrepancy, because the output is shown at the end of `BuildAll()`, but is gathered every time `BuildPermutation()` or `InitShader()` is called).
1 parent e74b3ff commit ab59550

14 files changed

+1072
-836
lines changed

src/engine/renderer/gl_shader.cpp

Lines changed: 684 additions & 554 deletions
Large diffs are not rendered by default.

src/engine/renderer/gl_shader.h

Lines changed: 319 additions & 206 deletions
Large diffs are not rendered by default.

src/engine/renderer/glsl_source/cull_cp.glsl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ layout (local_size_x = 64, local_size_y = 1, local_size_z = 1) in;
4242

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

45+
struct SurfaceDescriptor {
46+
BoundingSphere boundingSphere;
47+
uint surfaceCommandIDs[MAX_SURFACE_COMMANDS];
48+
};
49+
4550
layout(std430, binding = BIND_SURFACE_DESCRIPTORS) readonly restrict buffer surfaceDescriptorsSSBO {
4651
SurfaceDescriptor surfaces[];
4752
};

src/engine/renderer/glsl_source/fogQuake3_vp.glsl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ void DeformVertex( inout vec4 pos,
4444
inout vec3 normal,
4545
inout vec2 st,
4646
inout vec4 color,
47-
in float time);
47+
in float time );
4848

49-
void main()
49+
void main()
5050
{
5151
#insert material_vp
5252

@@ -63,7 +63,7 @@ void main()
6363
LB.normal,
6464
texCoord,
6565
color,
66-
u_Time);
66+
u_Time );
6767

6868
// transform vertex position into homogenous clip-space
6969
gl_Position = u_ModelViewProjectionMatrix * position;

src/engine/renderer/glsl_source/generic_vp.glsl

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ void DeformVertex( inout vec4 pos,
5555
inout vec3 normal,
5656
inout vec2 st,
5757
inout vec4 color,
58-
in float time);
58+
in float time );
5959

60-
void main()
60+
void main()
6161
{
6262
#insert material_vp
6363

@@ -72,11 +72,7 @@ void main()
7272
color = color * ColorModulateToColor( u_ColorModulateColorGen, lightFactor )
7373
+ unpackUnorm4x8( u_Color ) * vec4( lightFactor, lightFactor, lightFactor, 1.0 );
7474

75-
DeformVertex( position,
76-
LB.normal,
77-
texCoord,
78-
color,
79-
u_Time);
75+
DeformVertex( position, LB.normal, texCoord, color, u_Time );
8076

8177
// transform vertex position into homogenous clip-space
8278
gl_Position = u_ModelViewProjectionMatrix * position;

src/engine/renderer/glsl_source/lightMapping_vp.glsl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ OUT(smooth) vec3 var_Normal;
6464

6565
OUT(smooth) vec4 var_Color;
6666

67-
void DeformVertex(inout vec4 pos, inout vec3 normal, inout vec2 st, inout vec4 color, in float time);
67+
void DeformVertex( inout vec4 pos, inout vec3 normal, inout vec2 st, inout vec4 color, in float time );
6868

6969
void main()
7070
{
@@ -78,7 +78,7 @@ void main()
7878

7979
color = color * ColorModulateToColor( u_ColorModulateColorGen ) + unpackUnorm4x8( u_Color );
8080

81-
DeformVertex(position, LB.normal, texCoord, color, u_Time);
81+
DeformVertex( position, LB.normal, texCoord, color, u_Time );
8282

8383
// transform vertex position into homogenous clip-space
8484
gl_Position = u_ModelViewProjectionMatrix * position;

src/engine/renderer/glsl_source/material_cp.glsl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ struct BoundingSphere {
3939
float radius;
4040
};
4141

42-
struct SurfaceDescriptor {
42+
/* struct SurfaceDescriptor {
4343
BoundingSphere boundingSphere;
4444
uint surfaceCommandIDs[MAX_SURFACE_COMMANDS];
45-
};
45+
}; */
4646

4747
struct PortalSurface {
4848
BoundingSphere boundingSphere;

src/engine/renderer/tr_backend.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ GLuint64 BindAnimatedImage( int unit, const textureBundle_t *bundle )
120120
return GL_BindToTMU( unit, bundle->image[ index ] );
121121
}
122122

123-
void GL_BindProgram( shaderProgram_t *program )
123+
void GL_BindProgram( ShaderProgramDescriptor* program )
124124
{
125125
if ( !program )
126126
{
@@ -130,7 +130,7 @@ void GL_BindProgram( shaderProgram_t *program )
130130

131131
if ( glState.currentProgram != program )
132132
{
133-
glUseProgram( program->program );
133+
glUseProgram( program->id );
134134
glState.currentProgram = program;
135135
}
136136
}
@@ -1378,7 +1378,7 @@ static void RB_SetupLightForShadowing( trRefLight_t *light, int index,
13781378
bool shadowClip )
13791379
{
13801380
// HACK: bring OpenGL into a safe state or strange FBO update problems will occur
1381-
GL_BindProgram( nullptr );
1381+
GL_BindNullProgram();
13821382
GL_State( GLS_DEFAULT );
13831383

13841384
GL_Bind( tr.whiteImage );

src/engine/renderer/tr_bsp.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5054,6 +5054,7 @@ void RE_LoadWorldMap( const char *name )
50545054

50555055
Q_strncpyz( s_worldData.baseName, COM_SkipPath( s_worldData.name ), sizeof( s_worldData.name ) );
50565056
COM_StripExtension3( s_worldData.baseName, s_worldData.baseName, sizeof( s_worldData.baseName ) );
5057+
tr.loadingMap = s_worldData.baseName;
50575058

50585059
startMarker = (byte*) ri.Hunk_Alloc( 0, ha_pref::h_low );
50595060

@@ -5222,6 +5223,7 @@ void RE_LoadWorldMap( const char *name )
52225223
}
52235224

52245225
tr.worldLoaded = true;
5226+
tr.loadingMap = "";
52255227
GLSL_InitWorldShaders();
52265228

52275229
if ( glConfig2.reflectionMappingAvailable ) {

src/engine/renderer/tr_init.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -878,7 +878,7 @@ ScreenshotCmd screenshotPNGRegistration("screenshotPNG", ssFormat_t::SSF_PNG, "p
878878
glState.vertexAttribsState = 0;
879879
glState.vertexAttribPointersSet = 0;
880880

881-
GL_BindProgram( nullptr );
881+
GL_BindNullProgram();
882882

883883
glBindBuffer( GL_ARRAY_BUFFER, 0 );
884884
glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, 0 );
@@ -1141,7 +1141,7 @@ ScreenshotCmd screenshotPNGRegistration("screenshotPNG", ssFormat_t::SSF_PNG, "p
11411141
}
11421142

11431143
int deformIndex =
1144-
gl_shaderManager.getDeformShaderIndex( shader.deforms, shader.numDeforms );
1144+
gl_shaderManager.GetDeformShaderIndex( shader.deforms, shader.numDeforms );
11451145

11461146
for ( shaderStage_t *stage = shader.stages; stage != shader.lastStage; stage++ )
11471147
{

src/engine/renderer/tr_local.h

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1415,18 +1415,7 @@ enum class shaderProfilerRenderSubGroupsMode {
14151415
};
14161416

14171417
// *INDENT-ON*
1418-
1419-
// Tr3B - shaderProgram_t represents a pair of one
1420-
// GLSL vertex and one GLSL fragment shader
1421-
struct shaderProgram_t
1422-
{
1423-
GLuint program;
1424-
GLuint VS, FS, CS;
1425-
uint32_t attribs; // vertex array attributes
1426-
GLint *uniformLocations;
1427-
GLuint *uniformBlockIndexes;
1428-
byte *uniformFirewall;
1429-
};
1418+
struct ShaderProgramDescriptor;
14301419

14311420
// trRefdef_t holds everything that comes in refdef_t,
14321421
// as well as the locally generated scene information
@@ -2439,7 +2428,7 @@ enum class shaderProfilerRenderSubGroupsMode {
24392428
float vertexAttribsInterpolation; // 0 = no interpolation, 1 = final position
24402429
uint32_t vertexAttribsNewFrame; // offset for VBO vertex animations
24412430
uint32_t vertexAttribsOldFrame; // offset for VBO vertex animations
2442-
shaderProgram_t *currentProgram;
2431+
ShaderProgramDescriptor* currentProgram;
24432432
FBO_t *currentFBO;
24442433
VBO_t *currentVBO;
24452434
IBO_t *currentIBO;
@@ -2723,6 +2712,7 @@ enum class shaderProfilerRenderSubGroupsMode {
27232712

27242713
bool worldLoaded;
27252714
world_t *world;
2715+
std::string loadingMap;
27262716

27272717
TextureManager textureManager;
27282718

@@ -3245,7 +3235,7 @@ inline bool checkGLErrors()
32453235
void GL_Unbind( image_t *image );
32463236
GLuint64 BindAnimatedImage( int unit, const textureBundle_t *bundle );
32473237
void GL_TextureFilter( image_t *image, filterType_t filterType );
3248-
void GL_BindProgram( shaderProgram_t *program );
3238+
void GL_BindProgram( ShaderProgramDescriptor* program );
32493239
GLuint64 GL_BindToTMU( int unit, image_t *image );
32503240
void GL_BindNullProgram();
32513241
void GL_SetDefaultState();

src/engine/renderer/tr_main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2469,7 +2469,7 @@ static void R_DebugGraphics()
24692469
// the render thread can't make callbacks to the main thread
24702470
R_SyncRenderThread();
24712471

2472-
GL_BindProgram( nullptr );
2472+
GL_BindNullProgram();
24732473

24742474
GL_Bind( tr.whiteImage );
24752475

0 commit comments

Comments
 (0)