Permalink
Browse files

Add support for uniform buffer objects.

  • Loading branch information...
gimhael committed Feb 1, 2016
1 parent f1d1769 commit 6340f239a43a1764d39e18ca52f46b2d0be9cea7
@@ -244,18 +244,27 @@ void GLShaderManager::UpdateShaderProgramUniformLocations( GLShader *shader, sha
{
size_t uniformSize = shader->_uniformStorageSize;
size_t numUniforms = shader->_uniforms.size();
size_t numUniformBlocks = shader->_uniformBlocks.size();
// create buffer for storing uniform locations
shaderProgram->uniformLocations = ( GLint * ) ri.Z_Malloc( sizeof( GLint ) * numUniforms );
// create buffer for uniform firewall
shaderProgram->uniformFirewall = ( byte * ) ri.Z_Malloc( uniformSize );
// create buffer for storing uniform block indexes
shaderProgram->uniformBlockIndexes = ( GLuint * ) ri.Z_Malloc( sizeof( GLuint ) * numUniformBlocks );
// update uniforms
for (GLUniform *uniform : shader->_uniforms)
{
uniform->UpdateShaderProgramUniformLocation( shaderProgram );
}
// update uniform blocks
for (GLUniformBlock *uniformBlock : shader->_uniformBlocks)
{
uniformBlock->UpdateShaderProgramUniformBlockIndex( shaderProgram );
}
}
static inline void AddDefine( std::string& defines, const std::string& define, int value )
@@ -677,6 +686,12 @@ void GLShaderManager::InitShader( GLShader *shader )
shader->_uniformStorageSize += uniform->GetSize();
}
for ( std::size_t i = 0; i < shader->_uniformBlocks.size(); i++ )
{
GLUniformBlock *uniformBlock = shader->_uniformBlocks[ i ];
uniformBlock->SetLocationIndex( i );
}
std::string vertexInlines;
shader->BuildShaderVertexLibNames( vertexInlines );
@@ -59,6 +59,7 @@ struct GLShaderHeader
};
class GLUniform;
class GLUniformBlock;
class GLCompileMacro;
class GLShaderManager;
@@ -85,6 +86,7 @@ class GLShader
std::vector< GLUniform * > _uniforms;
std::vector< GLUniformBlock * > _uniformBlocks;
std::vector< GLCompileMacro * > _compileMacros;
@@ -147,6 +149,11 @@ class GLShader
{
ri.Free( p->uniformLocations );
}
if ( p->uniformBlockIndexes )
{
ri.Free( p->uniformBlockIndexes );
}
}
}
@@ -157,6 +164,11 @@ class GLShader
_uniforms.push_back( uniform );
}
void RegisterUniformBlock( GLUniformBlock *uniformBlock )
{
_uniformBlocks.push_back( uniformBlock );
}
void RegisterCompileMacro( GLCompileMacro *compileMacro )
{
if ( _compileMacros.size() >= MAX_SHADER_MACROS )
@@ -324,6 +336,46 @@ class GLUniform
}
};
class GLUniform1i : protected GLUniform
{
protected:
GLUniform1i( GLShader *shader, const char *name ) :
GLUniform( shader, name )
{
}
inline void SetValue( int value )
{
shaderProgram_t *p = _shader->GetProgram();
ASSERT_EQ(p, glState.currentProgram);
#if defined( LOG_GLSL_UNIFORMS )
if ( r_logFile->integer )
{
GLimp_LogComment( va( "GLSL_SetUniform1i( %s, shader: %s, value: %d ) ---\n",
this->GetName(), _shader->GetName().c_str(), value ) );
}
#endif
#if defined( USE_UNIFORM_FIREWALL )
int *firewall = ( int * ) &p->uniformFirewall[ _firewallIndex ];
if ( *firewall == value )
{
return;
}
*firewall = value;
#endif
glUniform1i( p->uniformLocations[ _locationIndex ], value );
}
public:
size_t GetSize()
{
return sizeof( int );
}
};
class GLUniform1f : protected GLUniform
{
protected:
@@ -626,6 +678,46 @@ class GLUniformMatrix34fv : protected GLUniform
}
};
class GLUniformBlock
{
protected:
GLShader *_shader;
std::string _name;
size_t _locationIndex;
GLUniformBlock( GLShader *shader, const char *name ) :
_shader( shader ),
_name( name ),
_locationIndex( 0 )
{
_shader->RegisterUniformBlock( this );
}
public:
void SetLocationIndex( size_t index )
{
_locationIndex = index;
}
const char *GetName()
{
return _name.c_str();
}
void UpdateShaderProgramUniformBlockIndex( shaderProgram_t *shaderProgram )
{
shaderProgram->uniformBlockIndexes[ _locationIndex ] = glGetUniformBlockIndex( shaderProgram->program, GetName() );
}
void SetBuffer( GLuint buffer ) {
shaderProgram_t *p = _shader->GetProgram();
ASSERT_EQ(p, glState.currentProgram);
glBindBufferBase( GL_UNIFORM_BUFFER, p->uniformBlockIndexes[ _locationIndex ], buffer );
}
};
class GLCompileMacro
{
private:
@@ -1322,6 +1322,7 @@ static inline void halfToFloat( const f16vec4_t in, vec4_t out )
GLuint VS, FS;
uint32_t attribs; // vertex array attributes
GLint *uniformLocations;
GLuint *uniformBlockIndexes;
byte *uniformFirewall;
};

0 comments on commit 6340f23

Please sign in to comment.