2017-09-30 19:13:06 +02:00
|
|
|
#include "externals.hh"
|
|
|
|
#include "bloom.hh"
|
2017-10-01 11:37:04 +02:00
|
|
|
#include "profiling.hh"
|
2017-10-04 11:20:27 +02:00
|
|
|
#include "globals.hh"
|
2017-10-01 11:37:04 +02:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
static const std::string Name_( "BLOOOOM!" );
|
|
|
|
}
|
|
|
|
|
2017-10-04 18:08:37 +02:00
|
|
|
#define PSTART() Globals::Profiler( ).start( Name_ )
|
|
|
|
#define PEND() do { glFinish( ); Globals::Profiler( ).end( Name_ ); } while ( 0 )
|
2017-09-30 19:13:06 +02:00
|
|
|
|
|
|
|
|
|
|
|
T_BloomPass::T_BloomPass(
|
|
|
|
__rw__ T_Texture& input )
|
|
|
|
: input_( input ) ,
|
|
|
|
//
|
2017-09-30 21:30:31 +02:00
|
|
|
txBlur0_( input.width( ) , input.height( ) , E_TexType::RGB16F , BloomLevels ) ,
|
|
|
|
txBlur1_( input.width( ) , input.height( ) , E_TexType::RGB16F , BloomLevels ) ,
|
2017-09-30 19:41:45 +02:00
|
|
|
//
|
|
|
|
filterParams_{ 1.6 , 1.2 , .95 } ,
|
|
|
|
blurWeights_{ 0.324 , 0.232 , 0.0855 , 0.0205 } ,
|
2017-10-01 18:51:02 +02:00
|
|
|
blurSize_{ 1.3 }
|
2017-09-30 19:13:06 +02:00
|
|
|
{
|
|
|
|
txBlur0_.wrap( E_TexWrap::CLAMP_EDGE ).samplingMode( E_TexSampling::LINEAR );
|
|
|
|
txBlur1_.wrap( E_TexWrap::CLAMP_EDGE ).samplingMode( E_TexSampling::LINEAR );
|
|
|
|
|
2017-09-30 21:27:50 +02:00
|
|
|
for ( auto i = 0u ; i < BloomLevels ; i ++ ) {
|
2017-09-30 19:13:06 +02:00
|
|
|
rtBlur0_.push_back( T_RendertargetSetup( ).add( txBlur0_ , i ).create( ) );
|
|
|
|
rtBlur1_.push_back( T_RendertargetSetup( ).add( txBlur1_ , i ).create( ) );
|
|
|
|
}
|
|
|
|
|
2017-10-04 17:48:25 +02:00
|
|
|
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" });
|
2017-09-30 19:13:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void T_BloomPass::render( )
|
|
|
|
{
|
2017-10-04 17:48:25 +02:00
|
|
|
if ( !( spHighpass_.valid( ) && spBlur_.valid( ) && spDownsample_.valid( ) ) ) {
|
|
|
|
return;
|
|
|
|
}
|
2017-10-01 11:37:04 +02:00
|
|
|
|
2017-10-04 17:48:25 +02:00
|
|
|
PSTART( );
|
2017-10-04 11:20:27 +02:00
|
|
|
auto& tm( Globals::Textures( ) );
|
2017-10-04 17:48:25 +02:00
|
|
|
{
|
|
|
|
rtBlur0_[ 0 ].activate( );
|
|
|
|
const auto hpf( spHighpass_.program( E_ShaderType::FRAGMENT ) );
|
2017-09-30 19:13:06 +02:00
|
|
|
enum {
|
|
|
|
U_TEXTURE = 0 ,
|
|
|
|
U_LOD = 1 ,
|
|
|
|
U_INPUT_SIZE = 2 ,
|
|
|
|
U_PARAMS = 3 ,
|
|
|
|
};
|
2017-10-02 14:48:16 +02:00
|
|
|
|
|
|
|
tm.bind( 0 , input_ );
|
2017-10-04 17:48:25 +02:00
|
|
|
spHighpass_.enable( );
|
2017-10-02 14:48:16 +02:00
|
|
|
|
2017-10-04 17:48:25 +02:00
|
|
|
glProgramUniform1i( hpf , U_TEXTURE , 0 );
|
|
|
|
glProgramUniform1i( hpf , U_LOD , 0 );
|
|
|
|
glProgramUniform2f( hpf , U_INPUT_SIZE ,
|
2017-09-30 19:13:06 +02:00
|
|
|
input_.width( ) ,
|
|
|
|
input_.height( ) );
|
2017-10-04 17:48:25 +02:00
|
|
|
glProgramUniform3fv( hpf , U_PARAMS , 1 , filterParams_ );
|
|
|
|
glDrawArrays( GL_TRIANGLE_STRIP , 0 , 4 );
|
2017-09-30 19:13:06 +02:00
|
|
|
}
|
|
|
|
|
2017-09-30 21:22:38 +02:00
|
|
|
enum {
|
|
|
|
U_TEXTURE = 0 ,
|
|
|
|
U_OUTPUT_SIZE = 1 ,
|
|
|
|
U_SOURCE_LOD = 2 ,
|
|
|
|
U_DIRECTION = 3 ,
|
|
|
|
U_WEIGHTS = 4 ,
|
|
|
|
};
|
2017-10-04 17:48:25 +02:00
|
|
|
|
|
|
|
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 );
|
|
|
|
|
2017-09-30 21:22:38 +02:00
|
|
|
for ( auto i = 0u ; i < BloomLevels ; i ++ ) {
|
|
|
|
if ( i > 0 ) {
|
2017-10-04 17:48:25 +02:00
|
|
|
spDownsample_.enable( );
|
2017-09-30 19:13:06 +02:00
|
|
|
rtBlur0_[ i ].activate( );
|
2017-10-02 14:48:16 +02:00
|
|
|
|
|
|
|
tm.bind( 0 , txBlur0_ );
|
|
|
|
|
2017-10-04 17:48:25 +02:00
|
|
|
glProgramUniform2f( dsf , 1 , txBlur0_.width( ) >> i ,
|
2017-09-30 21:22:38 +02:00
|
|
|
txBlur0_.height( ) >> i );
|
2017-10-04 17:48:25 +02:00
|
|
|
glProgramUniform1i( dsf , 2 , i - 1 );
|
2017-10-02 14:48:16 +02:00
|
|
|
|
2017-10-04 17:48:25 +02:00
|
|
|
glDrawArrays( GL_TRIANGLE_STRIP , 0 , 4 );
|
2017-09-30 19:13:06 +02:00
|
|
|
}
|
2017-09-30 21:22:38 +02:00
|
|
|
|
2017-10-04 17:48:25 +02:00
|
|
|
spBlur_.enable( );
|
|
|
|
glProgramUniform2f( bpf , U_OUTPUT_SIZE , rtBlur0_[ i ].width( ) ,
|
2017-09-30 21:22:38 +02:00
|
|
|
rtBlur0_[ i ].height( ) );
|
2017-10-04 17:48:25 +02:00
|
|
|
glProgramUniform1i( bpf , U_SOURCE_LOD , i );
|
2017-09-30 21:22:38 +02:00
|
|
|
|
|
|
|
rtBlur1_[ i ].activate( );
|
2017-10-04 17:48:25 +02:00
|
|
|
glProgramUniform2f( bpf , U_DIRECTION , blurSize_ , 0 );
|
2017-10-02 14:48:16 +02:00
|
|
|
tm.bind( 0 , txBlur0_ );
|
2017-10-04 17:48:25 +02:00
|
|
|
glDrawArrays( GL_TRIANGLE_STRIP , 0 , 4 );
|
2017-09-30 21:22:38 +02:00
|
|
|
|
|
|
|
rtBlur0_[ i ].activate( );
|
2017-10-04 17:48:25 +02:00
|
|
|
glProgramUniform2f( bpf , U_DIRECTION , 0 , blurSize_ );
|
2017-10-02 14:48:16 +02:00
|
|
|
tm.bind( 0 , txBlur1_ );
|
2017-10-04 17:48:25 +02:00
|
|
|
glDrawArrays( GL_TRIANGLE_STRIP , 0 , 4 );
|
2017-09-30 19:13:06 +02:00
|
|
|
}
|
|
|
|
|
2017-10-01 11:37:04 +02:00
|
|
|
PEND( );
|
2017-09-30 19:13:06 +02:00
|
|
|
}
|
2017-09-30 19:41:45 +02:00
|
|
|
|
|
|
|
void T_BloomPass::makeUI( )
|
|
|
|
{
|
|
|
|
if ( !ImGui::CollapsingHeader( "Bloom" ) ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ImGui::DragFloat3( "Filter" , filterParams_ , .01f , 0.1 , 10 );
|
|
|
|
ImGui::DragFloat4( "Weights" , blurWeights_ , .001f , 0. , 10 );
|
|
|
|
ImGui::DragFloat( "Blur size" , &blurSize_ , .001f , 0. , 10 );
|
|
|
|
}
|