Got rid of old texture binding code

Also this fixes the DoF texture wrapping bullshit
This commit is contained in:
Emmanuel BENOîT 2017-10-02 14:48:16 +02:00
parent 72acd42782
commit aabf8949a8
8 changed files with 121 additions and 109 deletions

View file

@ -46,17 +46,18 @@ void T_BloomPass::render( )
{ {
PSTART( ); PSTART( );
if ( spHighpass_.activate( ) ) { auto& tm( T_TextureManagement::TM( ) );
if ( spHighpass_.activate( ) && rtBlur0_[ 0 ].activate( ) ) {
enum { enum {
U_TEXTURE = 0 , U_TEXTURE = 0 ,
U_LOD = 1 , U_LOD = 1 ,
U_INPUT_SIZE = 2 , U_INPUT_SIZE = 2 ,
U_PARAMS = 3 , U_PARAMS = 3 ,
}; };
rtBlur0_[ 0 ].activate( );
T_TextureBinding tb( 0 ); tm.bind( 0 , input_ );
tb.set( U_TEXTURE , input_ );
tb.bind( ); glUniform1i( U_TEXTURE , 0 );
glUniform1i( U_LOD , 0 ); glUniform1i( U_LOD , 0 );
glUniform2f( U_INPUT_SIZE , glUniform2f( U_INPUT_SIZE ,
input_.width( ) , input_.width( ) ,
@ -64,7 +65,7 @@ void T_BloomPass::render( )
glUniform3fv( U_PARAMS , 1 , filterParams_ ); glUniform3fv( U_PARAMS , 1 , filterParams_ );
glRectf( -1, -1 , 1 , 1 ); glRectf( -1, -1 , 1 , 1 );
} }
assert( glGetError( ) == GL_NO_ERROR ); GL_ASSERT( );
enum { enum {
U_TEXTURE = 0 , U_TEXTURE = 0 ,
@ -74,34 +75,37 @@ void T_BloomPass::render( )
U_WEIGHTS = 4 , U_WEIGHTS = 4 ,
}; };
for ( auto i = 0u ; i < BloomLevels ; i ++ ) { for ( auto i = 0u ; i < BloomLevels ; i ++ ) {
T_TextureBinding tb( 0 );
if ( i > 0 ) { if ( i > 0 ) {
spDownsample_.activate( ); spDownsample_.activate( );
rtBlur0_[ i ].activate( ); rtBlur0_[ i ].activate( );
tb.set( 0 , txBlur0_ );
tb.bind( ); tm.bind( 0 , txBlur0_ );
glUniform1i( 0 , 0 );
glUniform2f( 1 , txBlur0_.width( ) >> i , glUniform2f( 1 , txBlur0_.width( ) >> i ,
txBlur0_.height( ) >> i ); txBlur0_.height( ) >> i );
glUniform1i( 2 , i - 1 ); glUniform1i( 2 , i - 1 );
glRectf( -1 , -1 , 1 , 1 ); glRectf( -1 , -1 , 1 , 1 );
} }
spBlur_.activate( ); if ( !spBlur_.activate( ) ) {
break;
}
glUniform4fv( U_WEIGHTS , 1 , blurWeights_ ); glUniform4fv( U_WEIGHTS , 1 , blurWeights_ );
glUniform2f( U_OUTPUT_SIZE , rtBlur0_[ i ].width( ) , glUniform2f( U_OUTPUT_SIZE , rtBlur0_[ i ].width( ) ,
rtBlur0_[ i ].height( ) ); rtBlur0_[ i ].height( ) );
glUniform1i( U_SOURCE_LOD , i ); glUniform1i( U_SOURCE_LOD , i );
glUniform1i( U_TEXTURE , 0 );
rtBlur1_[ i ].activate( ); rtBlur1_[ i ].activate( );
glUniform2f( U_DIRECTION , blurSize_ , 0 ); glUniform2f( U_DIRECTION , blurSize_ , 0 );
tb.set( U_TEXTURE , txBlur0_ ); tm.bind( 0 , txBlur0_ );
tb.bind( );
glRectf( -1 , -1 , 1 , 1 ); glRectf( -1 , -1 , 1 , 1 );
rtBlur0_[ i ].activate( ); rtBlur0_[ i ].activate( );
glUniform2f( U_DIRECTION , 0 , blurSize_ ); glUniform2f( U_DIRECTION , 0 , blurSize_ );
tb.set( U_TEXTURE , txBlur1_ ); tm.bind( 0 , txBlur1_ );
tb.bind( );
glRectf( -1 , -1 , 1 , 1 ); glRectf( -1 , -1 , 1 , 1 );
} }

View file

@ -30,6 +30,7 @@ struct T_BloomPass
T_ShaderProgram spDownsample_; T_ShaderProgram spDownsample_;
T_ShaderProgram spBlur_; T_ShaderProgram spBlur_;
T_TextureSampler sampler_;
T_Texture txBlur0_ , txBlur1_; T_Texture txBlur0_ , txBlur1_;
std::vector< T_Rendertarget > rtBlur0_ , rtBlur1_; std::vector< T_Rendertarget > rtBlur0_ , rtBlur1_;

View file

@ -31,11 +31,11 @@ void T_CombinePass::render( )
glClearColor( 1 , 0 , 1 , 1 ); glClearColor( 1 , 0 , 1 , 1 );
glClear( GL_COLOR_BUFFER_BIT ); glClear( GL_COLOR_BUFFER_BIT );
if ( program_.activate( ) ) { if ( program_.activate( ) ) {
T_TextureBinding main( 0 ) , blur( 1 ); auto& tm( T_TextureManagement::TM( ) );
main.set( 0 , txImage_ ); tm.bind( 0 , txImage_ );
main.bind( ); tm.bind( 1 , txBloom_ );
blur.set( 1 , txBloom_ ); glUniform1i( 0 , 0 );
blur.bind( ); glUniform1i( 1 , 1 );
glUniform2f( 2 , txImage_.width( ) , txImage_.height( ) ); glUniform2f( 2 , txImage_.width( ) , txImage_.height( ) );
glUniform2f( 3 , bloomStrength_ , bloomAttenuation_ ); glUniform2f( 3 , bloomStrength_ , bloomAttenuation_ );
glRectf( -1, -1 , 1 , 1 ); glRectf( -1, -1 , 1 , 1 );

View file

@ -41,8 +41,6 @@ struct T_Demo
const uint32_t width; const uint32_t width;
const uint32_t height; const uint32_t height;
// T_TextureManagement textures;
std::unique_ptr< T_Raymarcher > raymarcher; std::unique_ptr< T_Raymarcher > raymarcher;
std::unique_ptr< T_DoFPass > dof; std::unique_ptr< T_DoFPass > dof;
std::unique_ptr< T_BloomPass > bloom; std::unique_ptr< T_BloomPass > bloom;

22
dof.cc
View file

@ -47,29 +47,33 @@ void T_DoFPass::render( )
U_SAMPLES = 3 , U_SAMPLES = 3 ,
U_RES_TIME = 4 U_RES_TIME = 4
}; };
T_TextureBinding tb0( 0 ) , tb1( 1 );
auto& tm( T_TextureManagement::TM( ) );
if ( spPass1_.activate( ) && rtPass1_.activate( ) ) { if ( spPass1_.activate( ) && rtPass1_.activate( ) ) {
tb0.set( U_INPUT , imageInput_ ); glUniform1i( U_INPUT , 0 );
tb1.set( U_DEPTH , depthInput_ ); glUniform1i( U_DEPTH , 1 );
tb0.bind( );
tb1.bind( );
glUniform4fv( U_PARAMS , 1 , filterParams_ ); glUniform4fv( U_PARAMS , 1 , filterParams_ );
glUniform1f( U_SAMPLES , nSamples_ ); glUniform1f( U_SAMPLES , nSamples_ );
glUniform3f( U_RES_TIME , imageInput_.width( ) , glUniform3f( U_RES_TIME , imageInput_.width( ) ,
imageInput_.height( ) , imageInput_.height( ) ,
0 ); 0 );
tm.bind( 0 , imageInput_ , *tm.sampler( "linear-edge" ) );
tm.bind( 1 , depthInput_ , *tm.sampler( "linear-edge" ) );
glRectf( -1 , -1 , 1 , 1 ); glRectf( -1 , -1 , 1 , 1 );
} }
if ( spPass2_.activate( ) && rtPass2_.activate( ) ) { if ( spPass2_.activate( ) && rtPass2_.activate( ) ) {
tb0.set( U_INPUT , txPass1_ ); glUniform1i( U_INPUT , 0 );
tb1.set( U_DEPTH , depthInput_ ); glUniform1i( U_DEPTH , 1 );
tb0.bind( );
tb1.bind( );
glUniform4fv( U_PARAMS , 1 , filterParams_ ); glUniform4fv( U_PARAMS , 1 , filterParams_ );
glUniform1f( U_SAMPLES , nSamples_ ); glUniform1f( U_SAMPLES , nSamples_ );
glUniform3f( U_RES_TIME , imageInput_.width( ) , glUniform3f( U_RES_TIME , imageInput_.width( ) ,
imageInput_.height( ) , imageInput_.height( ) ,
0 ); 0 );
tm.bind( 0 , txPass1_ , *tm.sampler( "linear-edge" ) );
glRectf( -1 , -1 , 1 , 1 ); glRectf( -1 , -1 , 1 , 1 );
} }
PEND( ); PEND( );

View file

@ -96,6 +96,7 @@ void T_Main::mainLoop( )
T_Main::~T_Main( ) T_Main::~T_Main( )
{ {
demo.reset( ); demo.reset( );
T_TextureManagement::Shutdown( );
ImGui_ImplSdl_Shutdown( ); ImGui_ImplSdl_Shutdown( );
SDL_GL_DeleteContext( gl ); SDL_GL_DeleteContext( gl );
SDL_DestroyWindow( window ); SDL_DestroyWindow( window );
@ -203,6 +204,7 @@ void T_Main::render( )
glFinish( ); T_Profiler::Profiler.end( "Render" ); glFinish( ); T_Profiler::Profiler.end( "Render" );
} }
glUseProgram( 0 ); glUseProgram( 0 );
T_TextureManagement::TM( ).reset( );
ImGui::Render( ); ImGui::Render( );
} }

View file

@ -254,40 +254,56 @@ void T_TextureSampler::setSamplingMode( ) const
} }
/*============================================================================*/
T_TextureBinding::T_TextureBinding(
__rd__ const uint32_t binding )
: binding_( binding ) , uniform_( 0 ) , texture_( nullptr )
{ }
void T_TextureBinding::clear( )
{
uniform_ = 0;
texture_ = nullptr;
}
void T_TextureBinding::set(
__rd__ const uint32_t uniform ,
__rd__ T_Texture const& texture )
{
uniform_ = uniform;
texture_ = &texture;
}
void T_TextureBinding::bind( ) const
{
glBindTextureUnit( binding_ ,
texture_ ? texture_->id( ) : 0 );
glUniform1i( uniform_ , binding_ );
assert( glGetError( ) == GL_NO_ERROR );
}
/*============================================================================*/ /*============================================================================*/
constexpr uint32_t T_TextureManagement::MaxUnits; constexpr uint32_t T_TextureManagement::MaxUnits;
std::unique_ptr< T_TextureManagement > T_TextureManagement::instance_;
T_TextureManagement& T_TextureManagement::TM( )
{
if ( !instance_ ) {
instance_.reset( new T_TextureManagement( ) );
}
return *instance_;
}
void T_TextureManagement::Shutdown( )
{
instance_.reset( );
}
T_TextureManagement::T_TextureManagement( )
{
std::shared_ptr< T_TextureSampler > tsam;
tsam = std::make_shared< T_TextureSampler >( );
tsam->wrap( E_TexWrap::CLAMP_EDGE ).sampling( E_TexSampling::NEAREST );
samplers_[ "nearest-edge" ] = tsam;
tsam = std::make_shared< T_TextureSampler >( );
tsam->wrap( E_TexWrap::CLAMP_EDGE ).sampling( E_TexSampling::LINEAR );
samplers_[ "linear-edge" ] = tsam;
tsam = std::make_shared< T_TextureSampler >( );
tsam->wrap( E_TexWrap::CLAMP_BORDER ).sampling( E_TexSampling::NEAREST );
samplers_[ "nearest-border" ] = tsam;
tsam = std::make_shared< T_TextureSampler >( );
tsam->wrap( E_TexWrap::CLAMP_BORDER ).sampling( E_TexSampling::LINEAR );
samplers_[ "linear-border" ] = tsam;
}
T_TextureSampler const* T_TextureManagement::sampler(
__rd__ std::string const& name ) const
{
auto pos( samplers_.find( name ) );
if ( pos == samplers_.end( ) ) {
return nullptr;
}
return (*pos).second.get( );
}
void T_TextureManagement::bind( void T_TextureManagement::bind(
__rd__ const uint32_t unit , __rd__ const uint32_t unit ,
@ -295,37 +311,38 @@ void T_TextureManagement::bind(
{ {
assert( unit < MaxUnits ); assert( unit < MaxUnits );
auto& u( bindings_[ unit ] ); auto& u( bindings_[ unit ] );
if ( u.texture == &texture && !u.sampler ) { if ( u.texture == texture.id( ) && !u.sampler ) {
return; return;
} }
u.texture = &texture; u.texture = texture.id( );
u.sampler = nullptr; u.sampler = 0;
u.update = true; glBindTextureUnit( unit , texture.id( ) );
glBindSampler( unit , 0 );
} }
void T_TextureManagement::bind( void T_TextureManagement::bind(
__rd__ const uint32_t unit , __rd__ const uint32_t unit ,
__rd__ T_Texture& texture , __rd__ T_Texture const& texture ,
__rd__ T_TextureSampler& sampler ) __rd__ T_TextureSampler const& sampler )
{ {
assert( unit < MaxUnits ); assert( unit < MaxUnits );
auto& u( bindings_[ unit ] ); auto& u( bindings_[ unit ] );
if ( u.texture == &texture && u.sampler == &sampler ) { if ( u.texture == texture.id( ) && u.sampler == sampler.id( ) ) {
return; return;
} }
u.texture = &texture; u.texture = texture.id( );
u.sampler = &sampler; u.sampler = sampler.id( );
u.update = true; glBindTextureUnit( unit , texture.id( ) );
glBindSampler( unit , sampler.id( ) );
} }
void T_TextureManagement::update( ) void T_TextureManagement::reset( )
{ {
for ( auto i = 0u ; i < MaxUnits ; i ++ ) { for ( auto i = 0u ; i < MaxUnits ; i ++ ) {
auto& u( bindings_[ i ] ); auto& u( bindings_[ i ] );
if ( u.update ) { u.texture = 0;
glBindTextureUnit( i , u.texture ? u.texture->id( ) : 0 ); u.sampler = 0;
glBindSampler( i , u.sampler ? u.sampler->id( ) : 0 ); glBindTextureUnit( i , 0 );
u.update = false; glBindSampler( i , 0 );
}
} }
} }

View file

@ -103,51 +103,37 @@ struct T_TextureSampler
}; };
struct T_TextureBinding
{
T_TextureBinding( ) = delete;
T_TextureBinding( T_TextureBinding const& ) = delete;
T_TextureBinding( T_TextureBinding&& ) = delete;
explicit T_TextureBinding(
__rd__ const uint32_t binding );
void clear( );
void set( __rd__ const uint32_t uniform ,
__rd__ T_Texture const& texture );
void bind( ) const;
private:
const uint32_t binding_;
uint32_t uniform_;
T_Texture const* texture_;
};
struct T_TextureManagement struct T_TextureManagement
{ {
static constexpr uint32_t MaxUnits = 8; static constexpr uint32_t MaxUnits = 8;
T_TextureManagement( ) = default; NO_COPY( T_TextureManagement );
T_TextureManagement( T_TextureManagement const& ) = delete; NO_MOVE( T_TextureManagement );
T_TextureManagement( T_TextureManagement&& ) = delete;
static T_TextureManagement& TM( );
static void Shutdown( );
void reset( );
T_TextureSampler const* sampler(
__rd__ std::string const& name ) const;
void bind( __rd__ const uint32_t unit , void bind( __rd__ const uint32_t unit ,
__rd__ T_Texture const& texture ); __rd__ T_Texture const& texture );
void bind( __rd__ const uint32_t unit , void bind( __rd__ const uint32_t unit ,
__rd__ T_Texture& texture , __rd__ T_Texture const& texture ,
__rd__ T_TextureSampler& sampler ); __rd__ T_TextureSampler const& sampler );
void update( );
private: private:
static std::unique_ptr< T_TextureManagement > instance_;
T_TextureManagement( );
struct T_Binding_ struct T_Binding_
{ {
T_Texture const* texture = nullptr; GLuint texture = 0;
T_TextureSampler const* sampler = nullptr; GLuint sampler = 0;
bool update = false;
}; };
std::map< std::string , std::shared_ptr< T_TextureSampler > > samplers_;
T_Binding_ bindings_[ MaxUnits ]; T_Binding_ bindings_[ MaxUnits ];
}; };