Skip to content

Commit 3f52760

Browse files
committed
Generalise uniform post-processing
Also adds `GLShader.pushSkip` and `GLShader._pushUniforms`. Add `padding` back to the `GLShader` size calculations in material system.
1 parent 3010f75 commit 3f52760

File tree

2 files changed

+31
-12
lines changed

2 files changed

+31
-12
lines changed

src/engine/renderer/gl_shader.cpp

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2096,11 +2096,7 @@ static auto FindUniformForOffset( std::vector<GLUniform*>& uniforms, const GLuin
20962096
// Compute std140 size/alignment and sort uniforms from highest to lowest alignment
20972097
// Note: using the std430 uniform size will give the wrong result for matrix types where
20982098
// the number of rows is not 4
2099-
void GLShader::PostProcessUniforms() {
2100-
if ( !_useMaterialSystem ) {
2101-
return;
2102-
}
2103-
2099+
GLuint GLShaderManager::SortUniforms( std::vector<GLUniform*>& uniforms ) {
21042100
std::vector<GLUniform*> uniformQueue;
21052101
for ( GLUniform* uniform : _uniforms ) {
21062102
if ( !uniform->_global ) {
@@ -2116,15 +2112,15 @@ void GLShader::PostProcessUniforms() {
21162112

21172113
// Sort uniforms from highest to lowest alignment so we don't need to pad uniforms (other than vec3s)
21182114
GLuint align = 4; // mininum alignment since this will be used as an std140 array element
2119-
std140Size = 0;
2120-
_materialSystemUniforms.clear();
2115+
GLuint structSize = 0;
2116+
uniforms.clear();
21212117
while ( !uniformQueue.empty() || std140Size & ( align - 1 ) ) {
21222118
auto iterNext = FindUniformForOffset( uniformQueue, std140Size );
21232119
if ( iterNext == uniformQueue.end() ) {
21242120
// add 1 unit of padding
21252121
ASSERT( !_materialSystemUniforms.back()->_components); // array WriteToBuffer impls don't handle padding correctly
21262122
++std140Size;
2127-
++_materialSystemUniforms.back()->_std430Size;
2123+
++uniforms.back()->_std430Size;
21282124
} else {
21292125
( *iterNext )->_std430Size = ( *iterNext )->_std430BaseSize;
21302126
if ( ( *iterNext )->_components ) {
@@ -2134,10 +2130,24 @@ void GLShader::PostProcessUniforms() {
21342130
std140Size += ( *iterNext )->_std430Size;
21352131
}
21362132
align = std::max( align, ( *iterNext )->_std430Alignment );
2137-
_materialSystemUniforms.push_back( *iterNext );
2133+
uniforms.push_back( *iterNext );
21382134
uniformQueue.erase( iterNext );
21392135
}
21402136
}
2137+
return structSize;
2138+
}
2139+
2140+
void GLShader::PostProcessUniforms() {
2141+
if ( _useMaterialSystem ) {
2142+
_materialSystemUniforms = gl_shaderManager.ProcessUniforms( GLUniform::MATERIAL_OR_PUSH, GLUniform::MATERIAL_OR_PUSH,
2143+
true, _uniforms, std430Size, padding );
2144+
}
2145+
2146+
if ( glConfig.pushBufferAvailable && !pushSkip ) {
2147+
GLuint unused;
2148+
_pushUniforms = gl_shaderManager.ProcessUniforms( GLUniform::CONST, GLUniform::FRAME,
2149+
false, _uniforms, unused, unused );
2150+
}
21412151
}
21422152

21432153
uint32_t GLShader::GetUniqueCompileMacros( size_t permutation, const int type ) const {

src/engine/renderer/gl_shader.h

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ class GLShader {
188188
GLuint std140Size = 0;
189189

190190
const bool worldShader;
191+
const bool pushSkip;
191192
protected:
192193
int _activeMacros = 0;
193194
ShaderProgramDescriptor* currentProgram;
@@ -202,13 +203,15 @@ class GLShader {
202203

203204
size_t _uniformStorageSize;
204205
std::vector<GLUniform*> _uniforms;
206+
std::vector<GLUniform*> _pushUniforms;
205207
std::vector<GLUniform*> _materialSystemUniforms;
206208
std::vector<GLUniformBlock*> _uniformBlocks;
207209
std::vector<GLCompileMacro*> _compileMacros;
208210

209211
GLShader( const std::string& name, uint32_t vertexAttribsRequired,
210212
const bool useMaterialSystem,
211-
const std::string newVertexShaderName, const std::string newFragmentShaderName ) :
213+
const std::string newVertexShaderName, const std::string newFragmentShaderName,
214+
const bool newPushSkip = false ) :
212215
_name( name ),
213216
_vertexAttribsRequired( vertexAttribsRequired ),
214217
_useMaterialSystem( useMaterialSystem ),
@@ -217,7 +220,8 @@ class GLShader {
217220
hasVertexShader( true ),
218221
hasFragmentShader( true ),
219222
hasComputeShader( false ),
220-
worldShader( false ) {
223+
worldShader( false ),
224+
pushSkip( newPushSkip ) {
221225
}
222226

223227
GLShader( const std::string& name,
@@ -230,7 +234,8 @@ class GLShader {
230234
hasVertexShader( false ),
231235
hasFragmentShader( false ),
232236
hasComputeShader( true ),
233-
worldShader( newWorldShader ) {
237+
worldShader( newWorldShader ),
238+
pushSkip( false ) {
234239
}
235240

236241
public:
@@ -396,6 +401,10 @@ class GLShaderManager {
396401
void GenerateBuiltinHeaders();
397402
void GenerateWorldHeaders();
398403

404+
static GLuint SortUniforms( std::vector<GLUniform*>& uniforms );
405+
static std::vector<GLUniform*> ProcessUniforms( const GLUniform::UpdateType minType, const GLUniform::UpdateType maxType,
406+
const bool skipTextures, std::vector<GLUniform*>& uniforms, GLuint& structSize, GLuint& padding );
407+
399408
template<class T>
400409
void LoadShader( T*& shader ) {
401410
if ( !deformShaderCount ) {

0 commit comments

Comments
 (0)