From 7e6b558f27fef875db7718ecb92a8b73163ac089 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emmanuel=20Beno=C3=AEt?= Date: Wed, 4 Oct 2017 17:48:25 +0200 Subject: [PATCH] Bloom ported to new shader stuff --- bloom.cc | 69 ++++++++++--------- bloom.hh | 8 +-- ...om-highpass.glsl => bloom-highpass.f.glsl} | 2 + shaders/{blur-pass.glsl => blur-pass.f.glsl} | 2 + .../{downsample.glsl => downsample.f.glsl} | 2 + 5 files changed, 45 insertions(+), 38 deletions(-) rename shaders/{bloom-highpass.glsl => bloom-highpass.f.glsl} (96%) rename shaders/{blur-pass.glsl => blur-pass.f.glsl} (98%) rename shaders/{downsample.glsl => downsample.f.glsl} (97%) diff --git a/bloom.cc b/bloom.cc index 387361f..016fc79 100644 --- a/bloom.cc +++ b/bloom.cc @@ -14,9 +14,6 @@ namespace { T_BloomPass::T_BloomPass( __rw__ T_Texture& input ) : input_( input ) , - spHighpass_( GL_FRAGMENT_SHADER ) , - spDownsample_( GL_FRAGMENT_SHADER ) , - spBlur_( GL_FRAGMENT_SHADER ) , // txBlur0_( input.width( ) , input.height( ) , E_TexType::RGB16F , BloomLevels ) , txBlur1_( input.width( ) , input.height( ) , E_TexType::RGB16F , BloomLevels ) , @@ -33,21 +30,23 @@ T_BloomPass::T_BloomPass( rtBlur1_.push_back( T_RendertargetSetup( ).add( txBlur1_ , i ).create( ) ); } - spHighpass_.addFile( "bloom-highpass.glsl" ); - spDownsample_.addFile( "downsample.glsl" ); - spBlur_.addFile( "blur-pass.glsl" ); - - spHighpass_.load( ); - spDownsample_.load( ); - spBlur_.load( ); + auto& sm( Globals::Shaders( ) ); + spHighpass_ = sm.pipeline({ "fullscreen.v.glsl" , "bloom-highpass.f.glsl" }); + spDownsample_ = sm.pipeline({ "fullscreen.v.glsl" , "downsample.f.glsl" }); + spBlur_ = sm.pipeline({ "fullscreen.v.glsl" , "blur-pass.f.glsl" }); } void T_BloomPass::render( ) { - PSTART( ); + if ( !( spHighpass_.valid( ) && spBlur_.valid( ) && spDownsample_.valid( ) ) ) { + return; + } + PSTART( ); auto& tm( Globals::Textures( ) ); - if ( spHighpass_.activate( ) && rtBlur0_[ 0 ].activate( ) ) { + { + rtBlur0_[ 0 ].activate( ); + const auto hpf( spHighpass_.program( E_ShaderType::FRAGMENT ) ); enum { U_TEXTURE = 0 , U_LOD = 1 , @@ -56,16 +55,16 @@ void T_BloomPass::render( ) }; tm.bind( 0 , input_ ); + spHighpass_.enable( ); - glUniform1i( U_TEXTURE , 0 ); - glUniform1i( U_LOD , 0 ); - glUniform2f( U_INPUT_SIZE , + glProgramUniform1i( hpf , U_TEXTURE , 0 ); + glProgramUniform1i( hpf , U_LOD , 0 ); + glProgramUniform2f( hpf , U_INPUT_SIZE , input_.width( ) , input_.height( ) ); - glUniform3fv( U_PARAMS , 1 , filterParams_ ); - glRectf( -1, -1 , 1 , 1 ); + glProgramUniform3fv( hpf , U_PARAMS , 1 , filterParams_ ); + glDrawArrays( GL_TRIANGLE_STRIP , 0 , 4 ); } - GL_ASSERT( ); enum { U_TEXTURE = 0 , @@ -74,39 +73,41 @@ void T_BloomPass::render( ) U_DIRECTION = 3 , U_WEIGHTS = 4 , }; + + const auto dsf( spDownsample_.program( E_ShaderType::FRAGMENT ) ); + const auto bpf( spBlur_.program( E_ShaderType::FRAGMENT ) ); + glProgramUniform1i( dsf , 0 , 0 ); + glProgramUniform4fv( bpf , U_WEIGHTS , 1 , blurWeights_ ); + glProgramUniform1i( bpf , U_TEXTURE , 0 ); + for ( auto i = 0u ; i < BloomLevels ; i ++ ) { if ( i > 0 ) { - spDownsample_.activate( ); + spDownsample_.enable( ); rtBlur0_[ i ].activate( ); tm.bind( 0 , txBlur0_ ); - glUniform1i( 0 , 0 ); - glUniform2f( 1 , txBlur0_.width( ) >> i , + glProgramUniform2f( dsf , 1 , txBlur0_.width( ) >> i , txBlur0_.height( ) >> i ); - glUniform1i( 2 , i - 1 ); + glProgramUniform1i( dsf , 2 , i - 1 ); - glRectf( -1 , -1 , 1 , 1 ); + glDrawArrays( GL_TRIANGLE_STRIP , 0 , 4 ); } - if ( !spBlur_.activate( ) ) { - break; - } - glUniform4fv( U_WEIGHTS , 1 , blurWeights_ ); - glUniform2f( U_OUTPUT_SIZE , rtBlur0_[ i ].width( ) , + spBlur_.enable( ); + glProgramUniform2f( bpf , U_OUTPUT_SIZE , rtBlur0_[ i ].width( ) , rtBlur0_[ i ].height( ) ); - glUniform1i( U_SOURCE_LOD , i ); - glUniform1i( U_TEXTURE , 0 ); + glProgramUniform1i( bpf , U_SOURCE_LOD , i ); rtBlur1_[ i ].activate( ); - glUniform2f( U_DIRECTION , blurSize_ , 0 ); + glProgramUniform2f( bpf , U_DIRECTION , blurSize_ , 0 ); tm.bind( 0 , txBlur0_ ); - glRectf( -1 , -1 , 1 , 1 ); + glDrawArrays( GL_TRIANGLE_STRIP , 0 , 4 ); rtBlur0_[ i ].activate( ); - glUniform2f( U_DIRECTION , 0 , blurSize_ ); + glProgramUniform2f( bpf , U_DIRECTION , 0 , blurSize_ ); tm.bind( 0 , txBlur1_ ); - glRectf( -1 , -1 , 1 , 1 ); + glDrawArrays( GL_TRIANGLE_STRIP , 0 , 4 ); } PEND( ); diff --git a/bloom.hh b/bloom.hh index dfadf11..9038a83 100644 --- a/bloom.hh +++ b/bloom.hh @@ -4,7 +4,7 @@ #endif #include "rendertarget.hh" -#include "programs.hh" +#include "shaders.hh" struct T_BloomPass @@ -25,9 +25,9 @@ struct T_BloomPass private: T_Texture& input_; - T_ShaderProgram spHighpass_; - T_ShaderProgram spDownsample_; - T_ShaderProgram spBlur_; + T_ShaderPipeline spHighpass_; + T_ShaderPipeline spDownsample_; + T_ShaderPipeline spBlur_; T_TextureSampler sampler_; T_Texture txBlur0_ , txBlur1_; diff --git a/shaders/bloom-highpass.glsl b/shaders/bloom-highpass.f.glsl similarity index 96% rename from shaders/bloom-highpass.glsl rename to shaders/bloom-highpass.f.glsl index c8806d3..ec45636 100644 --- a/shaders/bloom-highpass.glsl +++ b/shaders/bloom-highpass.f.glsl @@ -1,5 +1,7 @@ #version 450 core +//! type fragment + layout( location = 0 ) uniform sampler2D u_Input; layout( location = 1 ) uniform int u_LOD; layout( location = 2 ) uniform vec2 u_InputSize; diff --git a/shaders/blur-pass.glsl b/shaders/blur-pass.f.glsl similarity index 98% rename from shaders/blur-pass.glsl rename to shaders/blur-pass.f.glsl index 81f0abe..8545829 100644 --- a/shaders/blur-pass.glsl +++ b/shaders/blur-pass.f.glsl @@ -1,5 +1,7 @@ #version 450 core +//! type fragment + layout( location = 0 ) uniform sampler2D u_InputTexture; layout( location = 1 ) uniform vec2 u_OutputSize; layout( location = 2 ) uniform int u_SourceLOD; diff --git a/shaders/downsample.glsl b/shaders/downsample.f.glsl similarity index 97% rename from shaders/downsample.glsl rename to shaders/downsample.f.glsl index 60e0298..2fb1614 100644 --- a/shaders/downsample.glsl +++ b/shaders/downsample.f.glsl @@ -1,5 +1,7 @@ #version 450 core +//! type fragment + layout( location = 0 ) uniform sampler2D u_InputTexture; layout( location = 1 ) uniform vec2 u_OutputSize; layout( location = 2 ) uniform int u_SourceLOD;