@@ -1647,7 +1647,7 @@ std::string GLShaderManager::ShaderPostProcess( GLShader *shader, const std::str
16471647 materialStruct += " };\n\n " ;
16481648
16491649 // 6 kb for materials
1650- const uint32_t count = ( 4096 + 2048 ) / shader->GetSTD430Size ();
1650+ const uint32_t count = ( 4096 + 2048 ) / shader->GetSTD430PaddedSize ();
16511651 std::string materialBlock = " layout(std140, binding = "
16521652 + std::to_string ( BufferBind::MATERIALS )
16531653 + " ) uniform materialsUBO {\n "
@@ -2113,59 +2113,91 @@ static int FindUniformForAlignment( std::vector<GLUniform*>& uniforms, const GLu
21132113 return -1 ;
21142114}
21152115
2116- // Compute std430 size/alignment and sort uniforms from highest to lowest alignment
2117- void GLShader::PostProcessUniforms () {
2118- if ( !_useMaterialSystem ) {
2119- return ;
2120- }
2121-
2122- for ( GLUniform* uniform : _uniforms ) {
2123- if ( !uniform->_global ) {
2124- _materialSystemUniforms.emplace_back ( uniform );
2125- }
2126- }
2127-
2128- std::sort ( _materialSystemUniforms.begin (), _materialSystemUniforms.end (),
2116+ GLuint GLShaderManager::SortUniforms ( std::vector<GLUniform*>& uniforms ) {
2117+ std::sort ( uniforms.begin (), uniforms.end (),
21292118 []( const GLUniform* lhs, const GLUniform* rhs ) {
21302119 return lhs->_std430Size > rhs->_std430Size ;
21312120 }
21322121 );
21332122
2134- // Sort uniforms from highest to lowest alignment so we don't need to pad uniforms (other than vec3s)
2135- const uint numUniforms = _materialSystemUniforms.size ();
2123+ const uint numUniforms = uniforms.size ();
21362124 std::vector<GLUniform*> tmp;
2125+ GLuint structSize = 0 ;
21372126 while ( tmp.size () < numUniforms ) {
21382127 // Higher-alignment uniforms first to avoid wasting memory
2139- GLuint size = _materialSystemUniforms [0 ]->_std430Size ;
2140- GLuint components = _materialSystemUniforms [0 ]->_components ;
2128+ GLuint size = uniforms [0 ]->_std430Size ;
2129+ GLuint components = uniforms [0 ]->_components ;
21412130 size = components ? PAD ( size, 4 ) * components : size;
21422131 GLuint alignmentConsume = 4 - size % 4 ;
2132+ GLuint usedSpace = size;
21432133
2144- GLUniform* tmpUniform = _materialSystemUniforms [0 ];
2145- tmp.emplace_back ( _materialSystemUniforms [0 ] );
2146- _materialSystemUniforms .erase ( _materialSystemUniforms .begin () );
2134+ GLUniform* tmpUniform = uniforms [0 ];
2135+ tmp.emplace_back ( uniforms [0 ] );
2136+ uniforms .erase ( uniforms .begin () );
21472137
21482138 int uniform;
2149- while ( ( alignmentConsume & 3 ) && _materialSystemUniforms.size ()
2150- && ( uniform = FindUniformForAlignment ( _materialSystemUniforms, alignmentConsume ) ) != -1 ) {
2151- alignmentConsume -= _materialSystemUniforms[uniform]->_std430Size ;
2139+ while ( ( alignmentConsume & 3 ) && uniforms.size ()
2140+ && ( uniform = FindUniformForAlignment ( uniforms, alignmentConsume ) ) != -1 ) {
2141+ alignmentConsume -= uniforms[uniform]->_std430Size ;
2142+ usedSpace += uniforms[uniform]->_std430Size ;
21522143
2153- tmpUniform = _materialSystemUniforms [uniform];
2144+ tmpUniform = uniforms [uniform];
21542145
2155- tmp.emplace_back ( _materialSystemUniforms [uniform] );
2156- _materialSystemUniforms .erase ( _materialSystemUniforms .begin () + uniform );
2146+ tmp.emplace_back ( uniforms [uniform] );
2147+ uniforms .erase ( uniforms .begin () + uniform );
21572148 }
21582149
2159- if ( alignmentConsume ) {
2150+ if ( alignmentConsume & 3 ) {
21602151 tmpUniform->_std430Size += alignmentConsume;
21612152 }
21622153
2163- size = PAD ( size, 4 );
2164- std430Size += size;
2165- padding = alignmentConsume;
2154+ if ( uniforms.size () ) {
2155+ structSize += PAD ( size, 4 );
2156+ } else {
2157+ structSize += usedSpace;
2158+ }
21662159 }
21672160
2168- _materialSystemUniforms = tmp;
2161+ uniforms = tmp;
2162+
2163+ return structSize;
2164+ }
2165+
2166+ std::vector<GLUniform*> GLShaderManager::ProcessUniforms ( const GLUniform::UpdateType minType, const GLUniform::UpdateType maxType,
2167+ const bool skipTextures,
2168+ std::vector<GLUniform*>& uniforms, GLuint& structSize, GLuint& padding ) {
2169+ std::vector<GLUniform*> tmp;
2170+
2171+ tmp.reserve ( uniforms.size () );
2172+ for ( GLUniform* uniform : uniforms ) {
2173+ if ( uniform->_updateType >= minType && uniform->_updateType <= maxType
2174+ && ( !uniform->_isTexture || !skipTextures ) ) {
2175+ tmp.emplace_back ( uniform );
2176+ }
2177+ }
2178+
2179+ structSize = SortUniforms ( tmp );
2180+
2181+ const GLuint structAlignment = 4 ; // Material buffer is now a UBO, so it uses std140 layout, which is aligned to vec4
2182+ if ( structSize > 0 ) {
2183+ padding = ( structAlignment - ( structSize % structAlignment ) ) % structAlignment;
2184+ }
2185+
2186+ return tmp;
2187+ }
2188+
2189+ // Compute std140 size/alignment and sort uniforms from highest to lowest alignment
2190+ void GLShader::PostProcessUniforms () {
2191+ if ( _useMaterialSystem ) {
2192+ _materialSystemUniforms = gl_shaderManager.ProcessUniforms ( GLUniform::MATERIAL_OR_PUSH, GLUniform::MATERIAL_OR_PUSH,
2193+ true , _uniforms, std430Size, padding );
2194+ }
2195+
2196+ if ( glConfig2.pushBufferAvailable && !pushSkip ) {
2197+ GLuint unused;
2198+ _pushUniforms = gl_shaderManager.ProcessUniforms ( GLUniform::CONST, GLUniform::FRAME,
2199+ false , _uniforms, unused, unused );
2200+ }
21692201}
21702202
21712203uint32_t GLShader::GetUniqueCompileMacros ( size_t permutation, const int type ) const {
0 commit comments