@@ -1536,9 +1536,9 @@ std::string GLShaderManager::RemoveUniformsFromShaderText( const std::string& sh
15361536 return shaderMain;
15371537}
15381538
1539- void GLShaderManager::GenerateUniformStructDefinesText ( const std::vector<GLUniform*>& uniforms,
1540- const std::string& definesName, std::string& uniformStruct, std::string& uniformDefines ) {
1541- int pad = 0 ;
1539+ uint32_t GLShaderManager::GenerateUniformStructDefinesText ( const std::vector<GLUniform*>& uniforms,
1540+ const std::string& definesName, const uint32_t offset, std::string& uniformStruct, std::string& uniformDefines ) {
1541+ int pad = offset ;
15421542 for ( GLUniform* uniform : uniforms ) {
15431543 uniformStruct += " " + ( uniform->_isTexture ? " uvec2" : uniform->_type ) + " " + uniform->_name ;
15441544
@@ -1565,6 +1565,8 @@ void GLShaderManager::GenerateUniformStructDefinesText( const std::vector<GLUnif
15651565 }
15661566
15671567 uniformDefines += " \n " ;
1568+
1569+ return pad;
15681570}
15691571
15701572void GLShaderManager::PostProcessGlobalUniforms () {
@@ -1588,23 +1590,20 @@ void GLShaderManager::PostProcessGlobalUniforms() {
15881590 std::string uniformDefines;
15891591
15901592 GLuint size;
1591- GLuint padding;
15921593 std::vector<GLUniform*>* uniforms = &( ( GLShader* ) globalUBOProxy )->_uniforms ;
15931594 std::vector<GLUniform*> constUniforms =
1594- ProcessUniforms ( GLUniform::CONST, GLUniform::CONST, !glConfig.usingBindlessTextures , *uniforms, size, padding );
1595-
1596- GenerateUniformStructDefinesText ( constUniforms, padding, 0 , " globalUniforms" , uniformStruct, uniformDefines );
1595+ ProcessUniforms ( GLUniform::CONST, GLUniform::CONST, !glConfig.usingBindlessTextures , *uniforms, size );
15971596
1598- uint32_t paddingCount = padding ;
1597+ const uint32_t padding = GenerateUniformStructDefinesText ( constUniforms, " globalUniforms " , 0 , uniformStruct, uniformDefines ) ;
15991598
1600- pushBuffer.constUniformsSize = size + padding ;
1599+ pushBuffer.constUniformsSize = size;
16011600
16021601 std::vector<GLUniform*> frameUniforms =
1603- ProcessUniforms ( GLUniform::FRAME, GLUniform::FRAME, !glConfig.usingBindlessTextures , *uniforms, size, padding );
1602+ ProcessUniforms ( GLUniform::FRAME, GLUniform::FRAME, !glConfig.usingBindlessTextures , *uniforms, size );
16041603
1605- GenerateUniformStructDefinesText ( frameUniforms, padding, paddingCount, " globalUniforms" , uniformStruct, uniformDefines );
1604+ GenerateUniformStructDefinesText ( frameUniforms, " globalUniforms" , padding, uniformStruct, uniformDefines );
16061605
1607- pushBuffer.frameUniformsSize = size + padding ;
1606+ pushBuffer.frameUniformsSize = size;
16081607
16091608 uniformStruct += " };\n\n " ;
16101609
@@ -1682,7 +1681,7 @@ std::string GLShaderManager::ShaderPostProcess( GLShader *shader, const std::str
16821681 std::string materialStruct = " \n struct Material {\n " ;
16831682 std::string materialDefines;
16841683 GenerateUniformStructDefinesText ( shader->_materialSystemUniforms ,
1685- " materials[baseInstance & 0xFFF]" , materialStruct, materialDefines );
1684+ " materials[baseInstance & 0xFFF]" , 0 , materialStruct, materialDefines );
16861685
16871686 materialStruct += " };\n\n " ;
16881687
@@ -2157,12 +2156,7 @@ static auto FindUniformForOffset( std::vector<GLUniform*>& uniforms, const GLuin
21572156// Note: using the std430 uniform size will give the wrong result for matrix types where
21582157// the number of rows is not 4
21592158GLuint GLShaderManager::SortUniforms ( std::vector<GLUniform*>& uniforms ) {
2160- std::vector<GLUniform*> uniformQueue;
2161- for ( GLUniform* uniform : _uniforms ) {
2162- if ( !uniform->_global ) {
2163- uniformQueue.emplace_back ( uniform );
2164- }
2165- }
2159+ std::vector<GLUniform*> uniformQueue = uniforms;
21662160
21672161 std::stable_sort ( uniformQueue.begin (), uniformQueue.end (),
21682162 []( const GLUniform* lhs, const GLUniform* rhs ) {
@@ -2174,32 +2168,33 @@ GLuint GLShaderManager::SortUniforms( std::vector<GLUniform*>& uniforms ) {
21742168 GLuint align = 4 ; // mininum alignment since this will be used as an std140 array element
21752169 GLuint structSize = 0 ;
21762170 uniforms.clear ();
2177- while ( !uniformQueue.empty () || std140Size & ( align - 1 ) ) {
2178- auto iterNext = FindUniformForOffset ( uniformQueue, std140Size );
2171+ while ( !uniformQueue.empty () || structSize & ( align - 1 ) ) {
2172+ auto iterNext = FindUniformForOffset ( uniformQueue, structSize );
21792173 if ( iterNext == uniformQueue.end () ) {
21802174 // add 1 unit of padding
21812175 ASSERT ( !_materialSystemUniforms.back ()->_components ); // array WriteToBuffer impls don't handle padding correctly
2182- ++std140Size ;
2176+ ++structSize ;
21832177 ++uniforms.back ()->_std430Size ;
21842178 } else {
21852179 ( *iterNext )->_std430Size = ( *iterNext )->_std430BaseSize ;
21862180 if ( ( *iterNext )->_components ) {
21872181 ASSERT_GE ( ( *iterNext )->_std430Alignment , 4u ); // these would need extra padding in a std130 array
2188- std140Size += ( *iterNext )->_std430Size * ( *iterNext )->_components ;
2182+ structSize += ( *iterNext )->_std430Size * ( *iterNext )->_components ;
21892183 } else {
2190- std140Size += ( *iterNext )->_std430Size ;
2184+ structSize += ( *iterNext )->_std430Size ;
21912185 }
21922186 align = std::max ( align, ( *iterNext )->_std430Alignment );
21932187 uniforms.push_back ( *iterNext );
21942188 uniformQueue.erase ( iterNext );
21952189 }
21962190 }
2191+
21972192 return structSize;
21982193}
21992194
22002195std::vector<GLUniform*> GLShaderManager::ProcessUniforms ( const GLUniform::UpdateType minType, const GLUniform::UpdateType maxType,
22012196 const bool skipTextures,
2202- std::vector<GLUniform*>& uniforms, GLuint& structSize, GLuint& padding ) {
2197+ std::vector<GLUniform*>& uniforms, GLuint& structSize ) {
22032198 std::vector<GLUniform*> tmp;
22042199
22052200 tmp.reserve ( uniforms.size () );
@@ -2212,24 +2207,19 @@ std::vector<GLUniform*> GLShaderManager::ProcessUniforms( const GLUniform::Updat
22122207
22132208 structSize = SortUniforms ( tmp );
22142209
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-
22202210 return tmp;
22212211}
22222212
22232213void GLShader::PostProcessUniforms () {
22242214 if ( _useMaterialSystem ) {
22252215 _materialSystemUniforms = gl_shaderManager.ProcessUniforms ( GLUniform::MATERIAL_OR_PUSH, GLUniform::MATERIAL_OR_PUSH,
2226- true , _uniforms, std430Size, padding );
2216+ true , _uniforms, std140Size );
22272217 }
22282218
22292219 if ( glConfig.pushBufferAvailable && !pushSkip ) {
22302220 GLuint unused;
22312221 _pushUniforms = gl_shaderManager.ProcessUniforms ( GLUniform::CONST, GLUniform::FRAME,
2232- !glConfig.usingBindlessTextures , _uniforms, unused, unused );
2222+ !glConfig.usingBindlessTextures , _uniforms, unused );
22332223 }
22342224}
22352225
0 commit comments