diff --git a/texture.cc b/texture.cc index 337570e..9571ec7 100644 --- a/texture.cc +++ b/texture.cc @@ -283,3 +283,49 @@ void T_TextureBinding::bind( ) const glUniform1i( uniform_ , binding_ ); 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; + } + } +} diff --git a/texture.hh b/texture.hh index 3ef5595..bc74fab 100644 --- a/texture.hh +++ b/texture.hh @@ -87,6 +87,9 @@ struct T_TextureSampler // -------------------------------------------------------------------- // Usage + GLuint id( ) const noexcept { return id_; } + + // unused void bind( __rd__ const GLuint unit ) { glBindSampler( unit , id_ ); } @@ -120,3 +123,30 @@ struct T_TextureBinding uint32_t uniform_; 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 ]; +};