Skip to content

Commit 1e2423c

Browse files
committed
Add the remaining code for the global UBO in PushBuffer
Post-process shaders to actually add the `globalUniformBlock`, add `SetConstUniforms()` and `SetFrameUniforms()` functions to the core and material system renderers, and add the supporting glsl code.
1 parent e33ad90 commit 1e2423c

21 files changed

+210
-5
lines changed

src/engine/renderer/GLMemory.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,10 @@ void GLStagingBuffer::FlushAll() {
117117
FlushStagingCopyQueue();
118118
}
119119

120+
bool GLStagingBuffer::Active() const {
121+
return buffer.id;
122+
}
123+
120124
void GLStagingBuffer::InitGLBuffer() {
121125
buffer.GenBuffer();
122126

src/engine/renderer/GLMemory.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ class GLBuffer {
173173

174174
void DelBuffer() {
175175
glDeleteBuffers( 1, &id );
176+
id = 0;
176177
mapped = false;
177178
}
178179

@@ -301,6 +302,8 @@ class GLStagingBuffer {
301302
void FlushStagingCopyQueue();
302303
void FlushAll();
303304

305+
bool Active() const;
306+
304307
void InitGLBuffer();
305308
void FreeGLBuffer();
306309

src/engine/renderer/Material.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1495,6 +1495,22 @@ void MaterialSystem::UpdateDynamicSurfaces() {
14951495
GL_CheckErrors();
14961496
}
14971497

1498+
void MaterialSystem::SetConstUniforms() {
1499+
globalUBOProxy->SetUniform_SurfaceDescriptorsCount( surfaceDescriptorsCount );
1500+
uint32_t globalWorkGroupX = surfaceDescriptorsCount % MAX_COMMAND_COUNTERS == 0 ?
1501+
surfaceDescriptorsCount / MAX_COMMAND_COUNTERS : surfaceDescriptorsCount / MAX_COMMAND_COUNTERS + 1;
1502+
1503+
globalUBOProxy->SetUniform_FirstPortalGroup( globalWorkGroupX );
1504+
globalUBOProxy->SetUniform_TotalPortals( totalPortals );
1505+
}
1506+
1507+
void MaterialSystem::SetFrameUniforms() {
1508+
globalUBOProxy->SetUniform_Frame( nextFrame );
1509+
1510+
globalUBOProxy->SetUniform_UseFrustumCulling( r_gpuFrustumCulling.Get() );
1511+
globalUBOProxy->SetUniform_UseOcclusionCulling( r_gpuOcclusionCulling.Get() );
1512+
}
1513+
14981514
void MaterialSystem::UpdateFrameData() {
14991515
gl_clearSurfacesShader->BindProgram( 0 );
15001516
gl_clearSurfacesShader->SetUniform_Frame( nextFrame );

src/engine/renderer/Material.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,8 @@ class MaterialSystem {
359359

360360
void StartFrame();
361361
void EndFrame();
362+
void SetConstUniforms();
363+
void SetFrameUniforms();
362364

363365
void GenerateDepthImages( const int width, const int height, imageParams_t imageParms );
364366

src/engine/renderer/gl_shader.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1345,6 +1345,12 @@ void GLShaderManager::InitShader( GLShader* shader ) {
13451345
ShaderDescriptor* desc = FindShader( shader->_name, shaderType.mainText, shaderType.GLType, shaderType.headers,
13461346
uniqueMacros, compileMacros, true );
13471347

1348+
if ( desc && glConfig2.pushBufferAvailable ) {
1349+
desc->shaderSource = RemoveUniformsFromShaderText( desc->shaderSource, shader->_pushUniforms );
1350+
1351+
desc->shaderSource.insert( shaderType.offset, globalUniformBlock );
1352+
}
1353+
13481354
if ( desc && glConfig2.usingMaterialSystem && shader->_useMaterialSystem ) {
13491355
desc->shaderSource = ShaderPostProcess( shader, desc->shaderSource, shaderType.offset );
13501356
}
@@ -1600,7 +1606,7 @@ void GLShaderManager::PostProcessGlobalUniforms() {
16001606
GLuint padding;
16011607
std::vector<GLUniform*>* uniforms = &( ( GLShader* ) globalUBOProxy )->_uniforms;
16021608
std::vector<GLUniform*> constUniforms =
1603-
ProcessUniforms( GLUniform::CONST, GLUniform::CONST, false, *uniforms, size, padding );
1609+
ProcessUniforms( GLUniform::CONST, GLUniform::CONST, !glConfig2.usingBindlessTextures, *uniforms, size, padding );
16041610

16051611
GenerateUniformStructDefinesText( constUniforms, padding, 0, "globalUniforms", uniformStruct, uniformDefines );
16061612

@@ -1609,7 +1615,7 @@ void GLShaderManager::PostProcessGlobalUniforms() {
16091615
pushBuffer.constUniformsSize = size + padding;
16101616

16111617
std::vector<GLUniform*> frameUniforms =
1612-
ProcessUniforms( GLUniform::FRAME, GLUniform::FRAME, false, *uniforms, size, padding );
1618+
ProcessUniforms( GLUniform::FRAME, GLUniform::FRAME, !glConfig2.usingBindlessTextures, *uniforms, size, padding );
16131619

16141620
GenerateUniformStructDefinesText( frameUniforms, padding, paddingCount, "globalUniforms", uniformStruct, uniformDefines );
16151621

@@ -2245,7 +2251,7 @@ void GLShader::PostProcessUniforms() {
22452251
if ( glConfig2.pushBufferAvailable && !pushSkip ) {
22462252
GLuint unused;
22472253
_pushUniforms = gl_shaderManager.ProcessUniforms( GLUniform::CONST, GLUniform::FRAME,
2248-
false, _uniforms, unused, unused );
2254+
!glConfig2.usingBindlessTextures, _uniforms, unused, unused );
22492255
}
22502256
}
22512257

src/engine/renderer/gl_shader.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -554,9 +554,9 @@ class GLUniformSampler : protected GLUniform {
554554
if ( glConfig2.pushBufferAvailable && _updateType <= FRAME ) {
555555
return;
556556
}
557-
}
558557

559558
glUniformHandleui64ARB( GetLocation(), currentValueBindless );
559+
}
560560
}
561561

562562
uint32_t* WriteToBuffer( uint32_t* buffer ) override {

src/engine/renderer/glsl_source/cameraEffects_fp.glsl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
2222

2323
/* cameraEffects_fp.glsl */
2424

25+
#define CAMERAEFFECTS_GLSL
26+
2527
uniform sampler2D u_CurrentMap;
2628

2729
#if defined(r_colorGrading)
@@ -53,6 +55,8 @@ DECLARE_OUTPUT(vec4)
5355

5456
void main()
5557
{
58+
#insert material_fp
59+
5660
// calculate the screen texcoord in the 0.0 to 1.0 range
5761
vec2 st = gl_FragCoord.st / r_FBufSize;
5862

src/engine/renderer/glsl_source/depthReduction_cp.glsl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3434

3535
/* depthReduction_cp.glsl */
3636

37+
#define DEPTHMAP_GLSL
38+
3739
#insert common_cp
3840

3941
// Keep this to 8x8 because we don't want extra shared mem etc. to be allocated, and to minimize wasted lanes
@@ -48,6 +50,8 @@ uniform uint u_ViewHeight;
4850
uniform bool u_InitialDepthLevel;
4951

5052
void main() {
53+
#insert material_fp
54+
5155
const uint globalInvocationID = GLOBAL_INVOCATION_ID;
5256

5357
const ivec2 position = ivec2( gl_GlobalInvocationID.xy );

src/engine/renderer/glsl_source/depthtile1_fp.glsl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3434

3535
/* depthtile1_fp.glsl */
3636

37+
#define DEPTHMAP_GLSL
38+
3739
uniform sampler2D u_DepthMap;
3840
IN(flat) vec3 unprojectionParams;
3941

@@ -63,6 +65,8 @@ float min16(in vec4 data0, in vec4 data1, in vec4 data2, in vec4 data3)
6365
}
6466
void main()
6567
{
68+
#insert material_fp
69+
6670
vec2 st = gl_FragCoord.st * 4.0 * pixelScale;
6771
vec4 depth[4], mask[4];
6872

src/engine/renderer/glsl_source/depthtile2_fp.glsl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3434

3535
/* depthtile2_fp.glsl */
3636

37+
#define DEPTHTILE1_GLSL
38+
3739
uniform sampler2D u_DepthTile1;
3840

3941
#if __VERSION__ > 120
@@ -44,6 +46,8 @@ out vec4 outputColor;
4446

4547
void main()
4648
{
49+
#insert material_fp
50+
4751
vec2 st = gl_FragCoord.st * r_tileStep;
4852
float x, y;
4953
vec4 accum = vec4( 0.0, 99999.0, 0.0, 0.0 );

src/engine/renderer/glsl_source/fogGlobal_fp.glsl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
2424

2525
#insert common
2626

27+
#define DEPTHMAP_GLSL
28+
2729
uniform sampler2D u_ColorMap; // fog texture
2830
uniform sampler2D u_DepthMap;
2931

@@ -40,6 +42,8 @@ out vec4 outputColor;
4042

4143
void main()
4244
{
45+
#insert material_fp
46+
4347
// calculate the screen texcoord in the 0.0 to 1.0 range
4448
vec2 st = gl_FragCoord.st / r_FBufSize;
4549

src/engine/renderer/glsl_source/generic_fp.glsl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ IN(smooth) vec2 var_TexCoords;
3636
IN(smooth) vec4 var_Color;
3737

3838
#if defined(USE_DEPTH_FADE)
39+
#define DEPTHMAP_GLSL
40+
3941
IN(smooth) float var_FadeDepth;
4042
uniform sampler2D u_DepthMap;
4143
#endif

src/engine/renderer/glsl_source/lightMapping_fp.glsl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
2727
#insert reliefMapping_fp
2828

2929
#define LIGHTMAPPING_GLSL
30+
#define LIGHTGRID_GLSL
3031

3132
uniform sampler2D u_DiffuseMap;
3233
uniform sampler2D u_MaterialMap;

src/engine/renderer/glsl_source/lighttile_fp.glsl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3434

3535
/* lighttile_fp.glsl */
3636

37+
#define DEPTHTILE2_GLSL
38+
3739
IN(smooth) vec2 vPosition;
3840

3941
struct Light {
@@ -96,6 +98,8 @@ vec3 ProjToView( vec2 inp ) {
9698
}
9799

98100
void main() {
101+
#insert material_fp
102+
99103
vec2 minmax = texture2D( u_DepthTile2, 0.5 * vPosition + 0.5 ).xy;
100104

101105
float minx = vPosition.x - r_tileStep.x;

src/engine/renderer/glsl_source/liquid_fp.glsl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
2727
#insert reliefMapping_fp
2828

2929
#define LIQUID_GLSL
30+
#define DEPTHMAP_GLSL
31+
#define LIGHTGRID_GLSL
3032

3133
uniform sampler2D u_CurrentMap;
3234
uniform sampler2D u_PortalMap;

src/engine/renderer/glsl_source/material_fp.glsl

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,47 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
4646
in the beginning of main() once
4747
Any texture samplers should be passed to functions from main() or other functions */
4848

49+
#ifdef HAVE_ARB_bindless_texture
50+
51+
#if defined(CAMERAEFFECTS_GLSL)
52+
sampler3D u_ColorMap3D = sampler3D( u_ColorMap3D_initial );
53+
#endif // !CAMERAEFFECTS_GLSL
54+
55+
#if defined(FOGQUAKE3_GLSL)
56+
sampler2D u_FogMap = sampler2D( u_FogMap_initial );
57+
#endif // !FOGQUAKE3_GLSL
58+
59+
#if defined(LIQUID_GLSL)
60+
sampler2D u_PortalMap = sampler2D( u_PortalMap_initial );
61+
#endif // !LIQUID_GLSL
62+
63+
#if defined(DEPTHMAP_GLSL)
64+
sampler2D u_DepthMap = sampler2D( u_DepthMap_initial );
65+
#endif // !DEPTHMAP_GLSL
66+
67+
#if defined(DEPTHTILE1_GLSL)
68+
sampler2D u_DepthTile1 = sampler2D( u_DepthTile1_initial );
69+
#endif // !DEPTHTILE1_GLSL
70+
71+
#if defined(DEPTHTILE2_GLSL)
72+
sampler2D u_DepthTile2 = sampler2D( u_DepthTile2_initial );
73+
#endif // !DEPTHTILE2_GLSL
74+
75+
#if defined(DEPTHREDUCTION_GLSL)
76+
sampler2D depthTextureInitial = sampler2D( depthTextureInitial_initial );
77+
#endif // !DEPTHREDUCTION_GLSL
78+
79+
#if defined(COMPUTELIGHT_GLSL)
80+
usampler3D u_LightTiles = usampler3D( u_LightTiles_initial );
81+
#endif // !COMPUTELIGHT_GLSL
82+
83+
#if defined(LIGHTGRID_GLSL)
84+
sampler3D u_LightGrid1 = sampler3D( u_LightGrid1_initial );
85+
sampler3D u_LightGrid2 = sampler3D( u_LightGrid2_initial );
86+
#endif // !LIGHTGRID_GLSL
87+
88+
#endif // !HAVE_ARB_bindless_texture
89+
4990
#if defined(USE_MATERIAL_SYSTEM)
5091

5192
#ifdef HAVE_ARB_bindless_texture

src/engine/renderer/glsl_source/motionblur_fp.glsl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
2222

2323
/* motionblur_fp.glsl */
2424

25+
#define DEPTHMAP_GLSL
26+
2527
uniform sampler2D u_ColorMap;
2628
uniform sampler2D u_DepthMap;
2729

@@ -35,6 +37,8 @@ out vec4 outputColor;
3537

3638
void main()
3739
{
40+
#insert material_fp
41+
3842
vec4 color = vec4( 0.0 );
3943

4044
// calculate the screen texcoord in the 0.0 to 1.0 range

src/engine/renderer/glsl_source/ssao_fp.glsl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
2222

2323
/* ssao_fp.glsl */
2424

25+
#define DEPTHMAP_GLSL
26+
2527
uniform sampler2D u_DepthMap;
2628
uniform vec3 u_UnprojectionParams;
2729

@@ -85,6 +87,8 @@ vec4 offsets[] = vec4[6](
8587

8688
void main()
8789
{
90+
#insert material_fp
91+
8892
vec2 st = gl_FragCoord.st * pixelScale;
8993
vec4 occlusion = vec4( 0.0 );
9094
vec4 total = vec4( 0.0 );

src/engine/renderer/tr_backend.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2652,6 +2652,41 @@ static void RB_RenderPostProcess()
26522652
GL_CheckErrors();
26532653
}
26542654

2655+
static void SetFrameUniforms() {
2656+
// This can happen with glsl_restart/vid_restart in R_SyncRenderThread()
2657+
if ( !stagingBuffer.Active() ) {
2658+
return;
2659+
}
2660+
2661+
GLIMP_LOGCOMMENT( "--- SetFrameUniforms ---" );
2662+
2663+
uint32_t* data = pushBuffer.MapGlobalUniformData( GLUniform::FRAME );
2664+
2665+
globalUBOProxy->SetUniform_blurVec( backEnd.refdef.blurVec );
2666+
globalUBOProxy->SetUniform_numLights( backEnd.refdef.numLights );
2667+
2668+
globalUBOProxy->SetUniform_ColorModulate( backEnd.viewParms.gradingWeights );
2669+
globalUBOProxy->SetUniform_InverseGamma( 1.0f / r_gamma->value );
2670+
2671+
const bool tonemap = r_toneMapping.Get() && r_highPrecisionRendering.Get() && glConfig2.textureFloatAvailable;
2672+
if ( tonemap ) {
2673+
vec4_t tonemapParms{ r_toneMappingContrast.Get(), r_toneMappingHighlightsCompressionSpeed.Get() };
2674+
ComputeTonemapParams( tonemapParms[0], tonemapParms[1], r_toneMappingHDRMax.Get(),
2675+
r_toneMappingDarkAreaPointHDR.Get(), r_toneMappingDarkAreaPointLDR.Get(), tonemapParms[2], tonemapParms[3] );
2676+
globalUBOProxy->SetUniform_TonemapParms( tonemapParms );
2677+
globalUBOProxy->SetUniform_TonemapExposure( r_toneMappingExposure.Get() );
2678+
}
2679+
globalUBOProxy->SetUniform_Tonemap( tonemap );
2680+
2681+
if ( glConfig2.usingMaterialSystem ) {
2682+
materialSystem.SetFrameUniforms();
2683+
}
2684+
2685+
globalUBOProxy->WriteUniformsToBuffer( data, GLShader::PUSH, GLUniform::FRAME );
2686+
2687+
pushBuffer.PushGlobalUniforms();
2688+
}
2689+
26552690
/*
26562691
============================================================================
26572692
@@ -3630,6 +3665,11 @@ void RB_ExecuteRenderCommands( const void *data )
36303665

36313666

36323667
materialSystem.frameStart = true;
3668+
3669+
if ( glConfig2.pushBufferAvailable ) {
3670+
SetFrameUniforms();
3671+
}
3672+
36333673
while ( cmd != nullptr )
36343674
{
36353675
cmd = cmd->ExecuteSelf();

0 commit comments

Comments
 (0)