Skip to content

Commit 7dc61bd

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 bef7df0 commit 7dc61bd

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
@@ -2110,11 +2110,7 @@ static auto FindUniformForOffset( std::vector<GLUniform*>& uniforms, const GLuin
21102110
// Compute std140 size/alignment and sort uniforms from highest to lowest alignment
21112111
// Note: using the std430 uniform size will give the wrong result for matrix types where
21122112
// the number of rows is not 4
2113-
void GLShader::PostProcessUniforms() {
2114-
if ( !_useMaterialSystem ) {
2115-
return;
2116-
}
2117-
2113+
GLuint GLShaderManager::SortUniforms( std::vector<GLUniform*>& uniforms ) {
21182114
std::vector<GLUniform*> uniformQueue;
21192115
for ( GLUniform* uniform : _uniforms ) {
21202116
if ( !uniform->_global ) {
@@ -2130,15 +2126,15 @@ void GLShader::PostProcessUniforms() {
21302126

21312127
// Sort uniforms from highest to lowest alignment so we don't need to pad uniforms (other than vec3s)
21322128
GLuint align = 4; // mininum alignment since this will be used as an std140 array element
2133-
std140Size = 0;
2134-
_materialSystemUniforms.clear();
2129+
GLuint structSize = 0;
2130+
uniforms.clear();
21352131
while ( !uniformQueue.empty() || std140Size & ( align - 1 ) ) {
21362132
auto iterNext = FindUniformForOffset( uniformQueue, std140Size );
21372133
if ( iterNext == uniformQueue.end() ) {
21382134
// add 1 unit of padding
21392135
ASSERT( !( *iterNext )->_components ); // array WriteToBuffer impls don't handle padding correctly
21402136
++std140Size;
2141-
++_materialSystemUniforms.back()->_std430Size;
2137+
++uniforms.back()->_std430Size;
21422138
} else {
21432139
( *iterNext )->_std430Size = ( *iterNext )->_std430BaseSize;
21442140
if ( ( *iterNext )->_components ) {
@@ -2148,10 +2144,24 @@ void GLShader::PostProcessUniforms() {
21482144
std140Size += ( *iterNext )->_std430Size;
21492145
}
21502146
align = std::max( align, ( *iterNext )->_std430Alignment );
2151-
_materialSystemUniforms.push_back( *iterNext );
2147+
uniforms.push_back( *iterNext );
21522148
uniformQueue.erase( iterNext );
21532149
}
21542150
}
2151+
return structSize;
2152+
}
2153+
2154+
void GLShader::PostProcessUniforms() {
2155+
if ( _useMaterialSystem ) {
2156+
_materialSystemUniforms = gl_shaderManager.ProcessUniforms( GLUniform::MATERIAL_OR_PUSH, GLUniform::MATERIAL_OR_PUSH,
2157+
true, _uniforms, std430Size, padding );
2158+
}
2159+
2160+
if ( glConfig.pushBufferAvailable && !pushSkip ) {
2161+
GLuint unused;
2162+
_pushUniforms = gl_shaderManager.ProcessUniforms( GLUniform::CONST, GLUniform::FRAME,
2163+
false, _uniforms, unused, unused );
2164+
}
21552165
}
21562166

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