Texture binding manager

This commit is contained in:
Emmanuel BENOîT 2017-10-02 10:57:51 +02:00
parent 456affe784
commit 00f9187d74
2 changed files with 76 additions and 0 deletions

View file

@ -283,3 +283,49 @@ void T_TextureBinding::bind( ) const
glUniform1i( uniform_ , binding_ ); glUniform1i( uniform_ , binding_ );
assert( glGetError( ) == GL_NO_ERROR ); assert( glGetError( ) == GL_NO_ERROR );
} }
/*============================================================================*/
constexpr uint32_t T_TextureManagement::MaxUnits;
void T_TextureManagement::bind(
__rd__ const uint32_t unit ,
__rd__ T_Texture const& texture )
{
assert( unit < MaxUnits );
auto& u( bindings_[ unit ] );
if ( u.texture == &texture && !u.sampler ) {
return;
}
u.texture = &texture;
u.sampler = nullptr;
u.update = true;
}
void T_TextureManagement::bind(
__rd__ const uint32_t unit ,
__rd__ T_Texture& texture ,
__rd__ T_TextureSampler& sampler )
{
assert( unit < MaxUnits );
auto& u( bindings_[ unit ] );
if ( u.texture == &texture && u.sampler == &sampler ) {
return;
}
u.texture = &texture;
u.sampler = &sampler;
u.update = true;
}
void T_TextureManagement::update( )
{
for ( auto i = 0u ; i < MaxUnits ; i ++ ) {
auto& u( bindings_[ i ] );
if ( u.update ) {
glBindTextureUnit( i , u.texture ? u.texture->id( ) : 0 );
glBindSampler( i , u.sampler ? u.sampler->id( ) : 0 );
u.update = false;
}
}
}

View file

@ -87,6 +87,9 @@ struct T_TextureSampler
// -------------------------------------------------------------------- // --------------------------------------------------------------------
// Usage // Usage
GLuint id( ) const noexcept { return id_; }
// unused
void bind( __rd__ const GLuint unit ) void bind( __rd__ const GLuint unit )
{ glBindSampler( unit , id_ ); } { glBindSampler( unit , id_ ); }
@ -120,3 +123,30 @@ struct T_TextureBinding
uint32_t uniform_; uint32_t uniform_;
T_Texture const* texture_; T_Texture const* texture_;
}; };
struct T_TextureManagement
{
static constexpr uint32_t MaxUnits = 8;
T_TextureManagement( T_TextureManagement const& ) = delete;
T_TextureManagement( T_TextureManagement&& ) = delete;
void bind( __rd__ const uint32_t unit ,
__rd__ T_Texture const& texture );
void bind( __rd__ const uint32_t unit ,
__rd__ T_Texture& texture ,
__rd__ T_TextureSampler& sampler );
void update( );
private:
struct T_Binding_
{
T_Texture const* texture = nullptr;
T_TextureSampler const* sampler = nullptr;
bool update = false;
};
T_Binding_ bindings_[ MaxUnits ];
};