Skip to content

Commit c5ff501

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 b4056ff commit c5ff501

22 files changed

Lines changed: 230 additions & 23 deletions

src/engine/renderer/GLMemory.cpp

Lines changed: 8 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

@@ -136,6 +140,10 @@ void GLStagingBuffer::FreeGLBuffer() {
136140

137141
void PushBuffer::InitGLBuffers() {
138142
globalUBO.GenBuffer();
143+
144+
globalUBO.BufferStorage( pushBuffer.constUniformsSize + pushBuffer.frameUniformsSize, 1, nullptr );
145+
146+
globalUBO.BindBufferBase();
139147
}
140148

141149
void PushBuffer::FreeGLBuffers() {

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: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -268,10 +268,6 @@ void GLShaderManager::FreeAll() {
268268
{
269269
_shaderBuildQueue.pop();
270270
}
271-
272-
if ( glConfig2.pushBufferAvailable ) {
273-
pushBuffer.FreeGLBuffers();
274-
}
275271
}
276272

277273
void GLShaderManager::UpdateShaderProgramUniformLocations( GLShader* shader, ShaderProgramDescriptor* shaderProgram ) const {
@@ -1349,6 +1345,12 @@ void GLShaderManager::InitShader( GLShader* shader ) {
13491345
ShaderDescriptor* desc = FindShader( shader->_name, shaderType.mainText, shaderType.GLType, shaderType.headers,
13501346
uniqueMacros, compileMacros, true );
13511347

1348+
if ( desc && glConfig2.pushBufferAvailable ) {
1349+
desc->shaderSource = RemoveUniformsFromShaderText( desc->shaderSource, shader->_pushUniforms );
1350+
1351+
desc->shaderSource.insert( shaderType.offset, globalUniformBlock );
1352+
}
1353+
13521354
if ( desc && glConfig2.usingMaterialSystem && shader->_useMaterialSystem ) {
13531355
desc->shaderSource = ShaderPostProcess( shader, desc->shaderSource, shaderType.offset );
13541356
}
@@ -1581,14 +1583,6 @@ void GLShaderManager::GenerateUniformStructDefinesText( const std::vector<GLUnif
15811583
}
15821584

15831585
void GLShaderManager::PostProcessGlobalUniforms() {
1584-
std::string uniformStruct = "\nstruct GlobalUniforms {\n";
1585-
std::string uniformBlock = "layout(std140, binding = "
1586-
+ std::to_string( BufferBind::GLOBAL_DATA )
1587-
+ ") uniform globalUBO {\n"
1588-
+ "GlobalUniforms globalUniforms;\n"
1589-
+ "};\n\n";
1590-
std::string uniformDefines;
1591-
15921586
/* Generate the struct and defines in the form of:
15931587
* struct GlobalUniforms {
15941588
* type uniform0;
@@ -1600,11 +1594,19 @@ void GLShaderManager::PostProcessGlobalUniforms() {
16001594
* #define uniformx globalUniforms.uniformx
16011595
*/
16021596

1597+
std::string uniformStruct = "\nstruct GlobalUniforms {\n";
1598+
std::string uniformBlock = "layout(std140, binding = "
1599+
+ std::to_string( BufferBind::GLOBAL_DATA )
1600+
+ ") uniform globalUBO {\n"
1601+
+ "GlobalUniforms globalUniforms;\n"
1602+
+ "};\n\n";
1603+
std::string uniformDefines;
1604+
16031605
GLuint size;
16041606
GLuint padding;
16051607
std::vector<GLUniform*>* uniforms = &( ( GLShader* ) globalUBOProxy )->_uniforms;
16061608
std::vector<GLUniform*> constUniforms =
1607-
ProcessUniforms( GLUniform::CONST, GLUniform::CONST, false, *uniforms, size, padding );
1609+
ProcessUniforms( GLUniform::CONST, GLUniform::CONST, !glConfig2.usingBindlessTextures, *uniforms, size, padding );
16081610

16091611
GenerateUniformStructDefinesText( constUniforms, padding, 0, "globalUniforms", uniformStruct, uniformDefines );
16101612

@@ -1613,7 +1615,7 @@ void GLShaderManager::PostProcessGlobalUniforms() {
16131615
pushBuffer.constUniformsSize = size + padding;
16141616

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

16181620
GenerateUniformStructDefinesText( frameUniforms, padding, paddingCount, "globalUniforms", uniformStruct, uniformDefines );
16191621

@@ -1633,12 +1635,6 @@ void GLShaderManager::PostProcessGlobalUniforms() {
16331635
for ( GLUniform* uniform : frameUniforms ) {
16341636
uniforms->push_back( uniform );
16351637
}
1636-
1637-
pushBuffer.InitGLBuffers();
1638-
1639-
pushBuffer.globalUBO.BufferStorage( pushBuffer.constUniformsSize + pushBuffer.frameUniformsSize, 1, nullptr );
1640-
1641-
pushBuffer.globalUBO.BindBufferBase();
16421638
}
16431639

16441640
// This will generate all the extra code for material system shaders
@@ -2255,7 +2251,7 @@ void GLShader::PostProcessUniforms() {
22552251
if ( glConfig2.pushBufferAvailable && !pushSkip ) {
22562252
GLuint unused;
22572253
_pushUniforms = gl_shaderManager.ProcessUniforms( GLUniform::CONST, GLUniform::FRAME,
2258-
false, _uniforms, unused, unused );
2254+
!glConfig2.usingBindlessTextures, _uniforms, unused, unused );
22592255
}
22602256
}
22612257

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 );

0 commit comments

Comments
 (0)