demotool/bloom.cc

134 lines
3.6 KiB
C++
Raw Normal View History

2017-09-30 19:13:06 +02:00
#include "externals.hh"
#include "utilities.hh"
#include "texture.hh"
#include "rendertarget.hh"
#include "bloom.hh"
T_BloomPass::T_BloomPass(
__rw__ T_FilesWatcher& watcher ,
__rw__ T_Texture& input )
: input_( input ) ,
spHighpass_( GL_FRAGMENT_SHADER , watcher ) ,
2017-09-30 21:22:38 +02:00
spDownsample_( GL_FRAGMENT_SHADER , watcher ) ,
2017-09-30 19:13:06 +02:00
spBlur_( GL_FRAGMENT_SHADER , watcher ) ,
spCombine_( GL_FRAGMENT_SHADER , watcher ) ,
//
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-09-30 21:27:50 +02:00
blurSize_{ 1.3 } ,
combineStrength_( 0.45 ) ,
2017-09-30 19:41:45 +02:00
combineAttenuation_( 0.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( ) );
}
spHighpass_.addFile( "bloom-highpass.glsl" );
2017-09-30 21:22:38 +02:00
spDownsample_.addFile( "downsample.glsl" );
2017-09-30 19:13:06 +02:00
spBlur_.addFile( "blur-pass.glsl" );
spCombine_.addFile( "bloom-combine.glsl" );
spHighpass_.load( );
2017-09-30 21:22:38 +02:00
spDownsample_.load( );
2017-09-30 19:13:06 +02:00
spBlur_.load( );
spCombine_.load( );
}
void T_BloomPass::render( )
{
if ( spHighpass_.activate( ) ) {
enum {
U_TEXTURE = 0 ,
U_LOD = 1 ,
U_INPUT_SIZE = 2 ,
U_PARAMS = 3 ,
};
2017-09-30 21:22:38 +02:00
rtBlur0_[ 0 ].activate( );
2017-09-30 19:13:06 +02:00
T_TextureBinding tb( 0 );
tb.set( U_TEXTURE , input_ );
tb.bind( );
glUniform1i( U_LOD , 0 );
glUniform2f( U_INPUT_SIZE ,
input_.width( ) ,
input_.height( ) );
2017-09-30 19:41:45 +02:00
glUniform3fv( U_PARAMS , 1 , filterParams_ );
2017-09-30 19:13:06 +02:00
glRectf( -1, -1 , 1 , 1 );
}
2017-09-30 21:22:38 +02:00
assert( glGetError( ) == GL_NO_ERROR );
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 ,
};
for ( auto i = 0u ; i < BloomLevels ; i ++ ) {
T_TextureBinding tb( 0 );
if ( i > 0 ) {
spDownsample_.activate( );
2017-09-30 19:13:06 +02:00
rtBlur0_[ i ].activate( );
2017-09-30 21:22:38 +02:00
tb.set( 0 , txBlur0_ );
2017-09-30 19:13:06 +02:00
tb.bind( );
2017-09-30 21:22:38 +02:00
glUniform2f( 1 , txBlur0_.width( ) >> i ,
txBlur0_.height( ) >> i );
glUniform1i( 2 , i - 1 );
2017-09-30 19:13:06 +02:00
glRectf( -1 , -1 , 1 , 1 );
}
2017-09-30 21:22:38 +02:00
spBlur_.activate( );
glUniform4fv( U_WEIGHTS , 1 , blurWeights_ );
glUniform2f( U_OUTPUT_SIZE , rtBlur0_[ i ].width( ) ,
rtBlur0_[ i ].height( ) );
glUniform1i( U_SOURCE_LOD , i );
rtBlur1_[ i ].activate( );
glUniform2f( U_DIRECTION , blurSize_ , 0 );
tb.set( U_TEXTURE , txBlur0_ );
tb.bind( );
glRectf( -1 , -1 , 1 , 1 );
rtBlur0_[ i ].activate( );
glUniform2f( U_DIRECTION , 0 , blurSize_ );
tb.set( U_TEXTURE , txBlur1_ );
tb.bind( );
glRectf( -1 , -1 , 1 , 1 );
2017-09-30 19:13:06 +02:00
}
T_Rendertarget::MainOutput( );
glClearColor( 1 , 0 , 1 , 1 );
glClear( GL_COLOR_BUFFER_BIT );
if ( spCombine_.activate( ) ) {
T_TextureBinding main( 0 ) , blur( 1 );
main.set( 0 , input_ );
main.bind( );
2017-09-30 21:22:38 +02:00
blur.set( 1 , txBlur0_ );
2017-09-30 19:13:06 +02:00
blur.bind( );
glUniform2f( 2 , input_.width( ) , input_.height( ) );
2017-09-30 21:27:50 +02:00
glUniform2f( 3 , combineStrength_ , combineAttenuation_ );
2017-09-30 19:13:06 +02:00
glRectf( -1, -1 , 1 , 1 );
glBindTexture( GL_TEXTURE_2D , 0 );
}
}
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 );
ImGui::DragFloat( "Initial strength" , &combineStrength_ , .001f , 0. , 10 );
ImGui::DragFloat( "Attenuation" , &combineAttenuation_ , .001f , 0. , 1 );
}