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

View file

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

View file

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

View file

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

22
dof.cc
View file

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

View file

@ -96,6 +96,7 @@ void T_Main::mainLoop( )
T_Main::~T_Main( )
{
demo.reset( );
T_TextureManagement::Shutdown( );
ImGui_ImplSdl_Shutdown( );
SDL_GL_DeleteContext( gl );
SDL_DestroyWindow( window );
@ -203,6 +204,7 @@ void T_Main::render( )
glFinish( ); T_Profiler::Profiler.end( "Render" );
}
glUseProgram( 0 );
T_TextureManagement::TM( ).reset( );
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;
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(
__rd__ const uint32_t unit ,
@ -295,37 +311,38 @@ void T_TextureManagement::bind(
{
assert( unit < MaxUnits );
auto& u( bindings_[ unit ] );
if ( u.texture == &texture && !u.sampler ) {
if ( u.texture == texture.id( ) && !u.sampler ) {
return;
}
u.texture = &texture;
u.sampler = nullptr;
u.update = true;
u.texture = texture.id( );
u.sampler = 0;
glBindTextureUnit( unit , texture.id( ) );
glBindSampler( unit , 0 );
}
void T_TextureManagement::bind(
__rd__ const uint32_t unit ,
__rd__ T_Texture& texture ,
__rd__ T_TextureSampler& sampler )
__rd__ T_Texture const& texture ,
__rd__ T_TextureSampler const& sampler )
{
assert( unit < MaxUnits );
auto& u( bindings_[ unit ] );
if ( u.texture == &texture && u.sampler == &sampler ) {
if ( u.texture == texture.id( ) && u.sampler == sampler.id( ) ) {
return;
}
u.texture = &texture;
u.sampler = &sampler;
u.update = true;
u.texture = texture.id( );
u.sampler = sampler.id( );
glBindTextureUnit( unit , texture.id( ) );
glBindSampler( unit , sampler.id( ) );
}
void T_TextureManagement::update( )
void T_TextureManagement::reset( )
{
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;
}
u.texture = 0;
u.sampler = 0;
glBindTextureUnit( i , 0 );
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
{
static constexpr uint32_t MaxUnits = 8;
T_TextureManagement( ) = default;
T_TextureManagement( T_TextureManagement const& ) = delete;
T_TextureManagement( T_TextureManagement&& ) = delete;
NO_COPY( T_TextureManagement );
NO_MOVE( T_TextureManagement );
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 ,
__rd__ T_Texture const& texture );
void bind( __rd__ const uint32_t unit ,
__rd__ T_Texture& texture ,
__rd__ T_TextureSampler& sampler );
void update( );
__rd__ T_Texture const& texture ,
__rd__ T_TextureSampler const& sampler );
private:
static std::unique_ptr< T_TextureManagement > instance_;
T_TextureManagement( );
struct T_Binding_
{
T_Texture const* texture = nullptr;
T_TextureSampler const* sampler = nullptr;
bool update = false;
GLuint texture = 0;
GLuint sampler = 0;
};
std::map< std::string , std::shared_ptr< T_TextureSampler > > samplers_;
T_Binding_ bindings_[ MaxUnits ];
};