Skip to content

Commit c6a469a

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 d353d60 commit c6a469a

19 files changed

+232
-13
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
@@ -175,6 +175,7 @@ class GLBuffer {
175175

176176
void DelBuffer() {
177177
glDeleteBuffers( 1, &id );
178+
id = 0;
178179
mapped = false;
179180
}
180181

@@ -303,6 +304,8 @@ class GLStagingBuffer {
303304
void FlushStagingCopyQueue();
304305
void FlushAll();
305306

307+
bool Active() const;
308+
306309
void InitGLBuffer();
307310
void FreeGLBuffer();
308311

src/engine/renderer/Material.cpp

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1593,9 +1593,24 @@ void MaterialSystem::UpdateDynamicSurfaces() {
15931593
GL_CheckErrors();
15941594
}
15951595

1596+
void MaterialSystem::SetConstUniforms() {
1597+
globalUBOProxy->SetUniform_SurfaceDescriptorsCount( surfaceDescriptorsCount );
1598+
uint32_t globalWorkGroupX = surfaceDescriptorsCount % MAX_COMMAND_COUNTERS == 0 ?
1599+
surfaceDescriptorsCount / MAX_COMMAND_COUNTERS : surfaceDescriptorsCount / MAX_COMMAND_COUNTERS + 1;
1600+
1601+
globalUBOProxy->SetUniform_FirstPortalGroup( globalWorkGroupX );
1602+
globalUBOProxy->SetUniform_TotalPortals( totalPortals );
1603+
}
1604+
1605+
void MaterialSystem::SetFrameUniforms() {
1606+
globalUBOProxy->SetUniform_Frame( nextFrame );
1607+
1608+
globalUBOProxy->SetUniform_UseFrustumCulling( r_gpuFrustumCulling.Get() );
1609+
globalUBOProxy->SetUniform_UseOcclusionCulling( r_gpuOcclusionCulling.Get() );
1610+
}
1611+
15961612
void MaterialSystem::UpdateFrameData() {
15971613
gl_clearSurfacesShader->BindProgram( 0 );
1598-
gl_clearSurfacesShader->SetUniform_Frame( nextFrame );
15991614
gl_clearSurfacesShader->DispatchCompute( MAX_VIEWS, 1, 1 );
16001615

16011616
GL_CheckErrors();
@@ -1681,15 +1696,9 @@ void MaterialSystem::CullSurfaces() {
16811696
uint32_t globalWorkGroupX = surfaceDescriptorsCount % MAX_COMMAND_COUNTERS == 0 ?
16821697
surfaceDescriptorsCount / MAX_COMMAND_COUNTERS : surfaceDescriptorsCount / MAX_COMMAND_COUNTERS + 1;
16831698
GL_Bind( depthImage );
1684-
gl_cullShader->SetUniform_Frame( nextFrame );
16851699
gl_cullShader->SetUniform_ViewID( view );
1686-
gl_cullShader->SetUniform_SurfaceDescriptorsCount( surfaceDescriptorsCount );
1687-
gl_cullShader->SetUniform_UseFrustumCulling( r_gpuFrustumCulling.Get() );
1688-
gl_cullShader->SetUniform_UseOcclusionCulling( r_gpuOcclusionCulling.Get() );
16891700
gl_cullShader->SetUniform_CameraPosition( origin );
16901701
gl_cullShader->SetUniform_ModelViewMatrix( viewMatrix );
1691-
gl_cullShader->SetUniform_FirstPortalGroup( globalWorkGroupX );
1692-
gl_cullShader->SetUniform_TotalPortals( totalPortals );
16931702
gl_cullShader->SetUniform_ViewWidth( depthImage->width );
16941703
gl_cullShader->SetUniform_ViewHeight( depthImage->height );
16951704
gl_cullShader->SetUniform_SurfaceCommandsOffset( surfaceCommandsCount * ( MAX_VIEWS * nextFrame + view ) );
@@ -1721,7 +1730,6 @@ void MaterialSystem::CullSurfaces() {
17211730
gl_cullShader->DispatchCompute( globalWorkGroupX, 1, 1 );
17221731

17231732
gl_processSurfacesShader->BindProgram( 0 );
1724-
gl_processSurfacesShader->SetUniform_Frame( nextFrame );
17251733
gl_processSurfacesShader->SetUniform_ViewID( view );
17261734
gl_processSurfacesShader->SetUniform_SurfaceCommandsOffset( surfaceCommandsCount * ( MAX_VIEWS * nextFrame + view ) );
17271735

src/engine/renderer/Material.h

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

363363
void StartFrame();
364364
void EndFrame();
365+
void SetConstUniforms();
366+
void SetFrameUniforms();
365367

366368
void GenerateDepthImages( const int width, const int height, imageParams_t imageParms );
367369

src/engine/renderer/gl_shader.cpp

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

1335+
if ( desc && glConfig.pushBufferAvailable ) {
1336+
desc->shaderSource = RemoveUniformsFromShaderText( desc->shaderSource, shader->_pushUniforms );
1337+
1338+
desc->shaderSource.insert( shaderType.offset, globalUniformBlock );
1339+
}
1340+
13351341
if ( desc && glConfig.usingMaterialSystem && shader->_useMaterialSystem ) {
13361342
desc->shaderSource = ShaderPostProcess( shader, desc->shaderSource, shaderType.offset );
13371343
}
@@ -1585,7 +1591,7 @@ void GLShaderManager::PostProcessGlobalUniforms() {
15851591
GLuint padding;
15861592
std::vector<GLUniform*>* uniforms = &( ( GLShader* ) globalUBOProxy )->_uniforms;
15871593
std::vector<GLUniform*> constUniforms =
1588-
ProcessUniforms( GLUniform::CONST, GLUniform::CONST, false, *uniforms, size, padding );
1594+
ProcessUniforms( GLUniform::CONST, GLUniform::CONST, !glConfig.usingBindlessTextures, *uniforms, size, padding );
15891595

15901596
GenerateUniformStructDefinesText( constUniforms, padding, 0, "globalUniforms", uniformStruct, uniformDefines );
15911597

@@ -1594,7 +1600,7 @@ void GLShaderManager::PostProcessGlobalUniforms() {
15941600
pushBuffer.constUniformsSize = size + padding;
15951601

15961602
std::vector<GLUniform*> frameUniforms =
1597-
ProcessUniforms( GLUniform::FRAME, GLUniform::FRAME, false, *uniforms, size, padding );
1603+
ProcessUniforms( GLUniform::FRAME, GLUniform::FRAME, !glConfig.usingBindlessTextures, *uniforms, size, padding );
15981604

15991605
GenerateUniformStructDefinesText( frameUniforms, padding, paddingCount, "globalUniforms", uniformStruct, uniformDefines );
16001606

@@ -2191,6 +2197,29 @@ GLuint GLShaderManager::SortUniforms( std::vector<GLUniform*>& uniforms ) {
21912197
return structSize;
21922198
}
21932199

2200+
std::vector<GLUniform*> GLShaderManager::ProcessUniforms( const GLUniform::UpdateType minType, const GLUniform::UpdateType maxType,
2201+
const bool skipTextures,
2202+
std::vector<GLUniform*>& uniforms, GLuint& structSize, GLuint& padding ) {
2203+
std::vector<GLUniform*> tmp;
2204+
2205+
tmp.reserve( uniforms.size() );
2206+
for ( GLUniform* uniform : uniforms ) {
2207+
if ( uniform->_updateType >= minType && uniform->_updateType <= maxType
2208+
&& ( !uniform->_isTexture || !skipTextures ) ) {
2209+
tmp.emplace_back( uniform );
2210+
}
2211+
}
2212+
2213+
structSize = SortUniforms( tmp );
2214+
2215+
const GLuint structAlignment = 4; // Material buffer is now a UBO, so it uses std140 layout, which is aligned to vec4
2216+
if ( structSize > 0 ) {
2217+
padding = ( structAlignment - ( structSize % structAlignment ) ) % structAlignment;
2218+
}
2219+
2220+
return tmp;
2221+
}
2222+
21942223
void GLShader::PostProcessUniforms() {
21952224
if ( _useMaterialSystem ) {
21962225
_materialSystemUniforms = gl_shaderManager.ProcessUniforms( GLUniform::MATERIAL_OR_PUSH, GLUniform::MATERIAL_OR_PUSH,
@@ -2200,7 +2229,7 @@ void GLShader::PostProcessUniforms() {
22002229
if ( glConfig.pushBufferAvailable && !pushSkip ) {
22012230
GLuint unused;
22022231
_pushUniforms = gl_shaderManager.ProcessUniforms( GLUniform::CONST, GLUniform::FRAME,
2203-
false, _uniforms, unused, unused );
2232+
!glConfig.usingBindlessTextures, _uniforms, unused, unused );
22042233
}
22052234
}
22062235

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)
@@ -73,6 +75,8 @@ DECLARE_OUTPUT(vec4)
7375

7476
void main()
7577
{
78+
#insert material_fp
79+
7680
// calculate the screen texcoord in the 0.0 to 1.0 range
7781
vec2 st = gl_FragCoord.st / r_FBufSize;
7882

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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ DECLARE_OUTPUT(vec4)
3737

3838
void main()
3939
{
40+
#insert material_fp
41+
4042
// calculate the screen texcoord in the 0.0 to 1.0 range
4143
vec2 st = gl_FragCoord.st / r_FBufSize;
4244

0 commit comments

Comments
 (0)