@@ -1550,9 +1550,9 @@ std::string GLShaderManager::RemoveUniformsFromShaderText( const std::string& sh
15501550 return shaderMain;
15511551}
15521552
1553- void GLShaderManager::GenerateUniformStructDefinesText ( const std::vector<GLUniform*>& uniforms,
1554- const std::string& definesName, std::string& uniformStruct, std::string& uniformDefines ) {
1555- int pad = 0 ;
1553+ uint32_t GLShaderManager::GenerateUniformStructDefinesText ( const std::vector<GLUniform*>& uniforms,
1554+ const std::string& definesName, const uint32_t offset, std::string& uniformStruct, std::string& uniformDefines ) {
1555+ int pad = offset ;
15561556 for ( GLUniform* uniform : uniforms ) {
15571557 uniformStruct += " " + ( uniform->_isTexture ? " uvec2" : uniform->_type ) + " " + uniform->_name ;
15581558
@@ -1579,6 +1579,8 @@ void GLShaderManager::GenerateUniformStructDefinesText( const std::vector<GLUnif
15791579 }
15801580
15811581 uniformDefines += " \n " ;
1582+
1583+ return pad;
15821584}
15831585
15841586void GLShaderManager::PostProcessGlobalUniforms () {
@@ -1602,23 +1604,20 @@ void GLShaderManager::PostProcessGlobalUniforms() {
16021604 std::string uniformDefines;
16031605
16041606 GLuint size;
1605- GLuint padding;
16061607 std::vector<GLUniform*>* uniforms = &( ( GLShader* ) globalUBOProxy )->_uniforms ;
16071608 std::vector<GLUniform*> constUniforms =
1608- ProcessUniforms ( GLUniform::CONST, GLUniform::CONST, !glConfig.usingBindlessTextures , *uniforms, size, padding );
1609-
1610- GenerateUniformStructDefinesText ( constUniforms, padding, 0 , " globalUniforms" , uniformStruct, uniformDefines );
1609+ ProcessUniforms ( GLUniform::CONST, GLUniform::CONST, !glConfig.usingBindlessTextures , *uniforms, size );
16111610
1612- uint32_t paddingCount = padding ;
1611+ const uint32_t padding = GenerateUniformStructDefinesText ( constUniforms, " globalUniforms " , 0 , uniformStruct, uniformDefines ) ;
16131612
1614- pushBuffer.constUniformsSize = size + padding ;
1613+ pushBuffer.constUniformsSize = size;
16151614
16161615 std::vector<GLUniform*> frameUniforms =
1617- ProcessUniforms ( GLUniform::FRAME, GLUniform::FRAME, !glConfig.usingBindlessTextures , *uniforms, size, padding );
1616+ ProcessUniforms ( GLUniform::FRAME, GLUniform::FRAME, !glConfig.usingBindlessTextures , *uniforms, size );
16181617
1619- GenerateUniformStructDefinesText ( frameUniforms, padding, paddingCount, " globalUniforms" , uniformStruct, uniformDefines );
1618+ GenerateUniformStructDefinesText ( frameUniforms, " globalUniforms" , padding, uniformStruct, uniformDefines );
16201619
1621- pushBuffer.frameUniformsSize = size + padding ;
1620+ pushBuffer.frameUniformsSize = size;
16221621
16231622 uniformStruct += " };\n\n " ;
16241623
@@ -1696,7 +1695,7 @@ std::string GLShaderManager::ShaderPostProcess( GLShader *shader, const std::str
16961695 std::string materialStruct = " \n struct Material {\n " ;
16971696 std::string materialDefines;
16981697 GenerateUniformStructDefinesText ( shader->_materialSystemUniforms ,
1699- " materials[baseInstance & 0xFFF]" , materialStruct, materialDefines );
1698+ " materials[baseInstance & 0xFFF]" , 0 , materialStruct, materialDefines );
17001699
17011700 materialStruct += " };\n\n " ;
17021701
@@ -2171,12 +2170,7 @@ static auto FindUniformForOffset( std::vector<GLUniform*>& uniforms, const GLuin
21712170// Note: using the std430 uniform size will give the wrong result for matrix types where
21722171// the number of rows is not 4
21732172GLuint GLShaderManager::SortUniforms ( std::vector<GLUniform*>& uniforms ) {
2174- std::vector<GLUniform*> uniformQueue;
2175- for ( GLUniform* uniform : _uniforms ) {
2176- if ( !uniform->_global ) {
2177- uniformQueue.emplace_back ( uniform );
2178- }
2179- }
2173+ std::vector<GLUniform*> uniformQueue = uniforms;
21802174
21812175 std::stable_sort ( uniformQueue.begin (), uniformQueue.end (),
21822176 []( const GLUniform* lhs, const GLUniform* rhs ) {
@@ -2188,32 +2182,32 @@ GLuint GLShaderManager::SortUniforms( std::vector<GLUniform*>& uniforms ) {
21882182 GLuint align = 4 ; // mininum alignment since this will be used as an std140 array element
21892183 GLuint structSize = 0 ;
21902184 uniforms.clear ();
2191- while ( !uniformQueue.empty () || std140Size & ( align - 1 ) ) {
2192- auto iterNext = FindUniformForOffset ( uniformQueue, std140Size );
2185+ while ( !uniformQueue.empty () || structSize & ( align - 1 ) ) {
2186+ auto iterNext = FindUniformForOffset ( uniformQueue, structSize );
21932187 if ( iterNext == uniformQueue.end () ) {
21942188 // add 1 unit of padding
2195- ASSERT ( !( *iterNext )->_components ); // array WriteToBuffer impls don't handle padding correctly
2196- ++std140Size;
2189+ ++structSize;
21972190 ++uniforms.back ()->_std430Size ;
21982191 } else {
21992192 ( *iterNext )->_std430Size = ( *iterNext )->_std430BaseSize ;
22002193 if ( ( *iterNext )->_components ) {
22012194 ASSERT_GE ( ( *iterNext )->_std430Alignment , 4 ); // these would need extra padding in a std130 array
2202- std140Size += ( *iterNext )->_std430Size * ( *iterNext )->_components ;
2195+ structSize += ( *iterNext )->_std430Size * ( *iterNext )->_components ;
22032196 } else {
2204- std140Size += ( *iterNext )->_std430Size ;
2197+ structSize += ( *iterNext )->_std430Size ;
22052198 }
22062199 align = std::max ( align, ( *iterNext )->_std430Alignment );
22072200 uniforms.push_back ( *iterNext );
22082201 uniformQueue.erase ( iterNext );
22092202 }
22102203 }
2204+
22112205 return structSize;
22122206}
22132207
22142208std::vector<GLUniform*> GLShaderManager::ProcessUniforms ( const GLUniform::UpdateType minType, const GLUniform::UpdateType maxType,
22152209 const bool skipTextures,
2216- std::vector<GLUniform*>& uniforms, GLuint& structSize, GLuint& padding ) {
2210+ std::vector<GLUniform*>& uniforms, GLuint& structSize ) {
22172211 std::vector<GLUniform*> tmp;
22182212
22192213 tmp.reserve ( uniforms.size () );
@@ -2226,24 +2220,19 @@ std::vector<GLUniform*> GLShaderManager::ProcessUniforms( const GLUniform::Updat
22262220
22272221 structSize = SortUniforms ( tmp );
22282222
2229- const GLuint structAlignment = 4 ; // Material buffer is now a UBO, so it uses std140 layout, which is aligned to vec4
2230- if ( structSize > 0 ) {
2231- padding = ( structAlignment - ( structSize % structAlignment ) ) % structAlignment;
2232- }
2233-
22342223 return tmp;
22352224}
22362225
22372226void GLShader::PostProcessUniforms () {
22382227 if ( _useMaterialSystem ) {
22392228 _materialSystemUniforms = gl_shaderManager.ProcessUniforms ( GLUniform::MATERIAL_OR_PUSH, GLUniform::MATERIAL_OR_PUSH,
2240- true , _uniforms, std430Size, padding );
2229+ true , _uniforms, std140Size );
22412230 }
22422231
22432232 if ( glConfig.pushBufferAvailable && !pushSkip ) {
22442233 GLuint unused;
22452234 _pushUniforms = gl_shaderManager.ProcessUniforms ( GLUniform::CONST, GLUniform::FRAME,
2246- !glConfig.usingBindlessTextures , _uniforms, unused, unused );
2235+ !glConfig.usingBindlessTextures , _uniforms, unused );
22472236 }
22482237}
22492238
0 commit comments