Skip to content

Commit

Permalink
Use one shader for blur
Browse files Browse the repository at this point in the history
The only difference between the 2 shaders was switching 2 components of a vec2, which is better done by using a uniform instead of switching shaders. Simplifies the code as well.
  • Loading branch information
VReaperV committed Nov 20, 2024
1 parent b69bf47 commit 13e2d66
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 222 deletions.
6 changes: 2 additions & 4 deletions src.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,8 @@ set(GLSLSOURCELIST
${ENGINE_DIR}/renderer/glsl_source/vertexAnimation_vp.glsl
${ENGINE_DIR}/renderer/glsl_source/vertexSimple_vp.glsl
${ENGINE_DIR}/renderer/glsl_source/vertexSkinning_vp.glsl
${ENGINE_DIR}/renderer/glsl_source/blurX_fp.glsl
${ENGINE_DIR}/renderer/glsl_source/blurX_vp.glsl
${ENGINE_DIR}/renderer/glsl_source/blurY_fp.glsl
${ENGINE_DIR}/renderer/glsl_source/blurY_vp.glsl
${ENGINE_DIR}/renderer/glsl_source/blur_fp.glsl
${ENGINE_DIR}/renderer/glsl_source/blur_vp.glsl
${ENGINE_DIR}/renderer/glsl_source/cameraEffects_fp.glsl
${ENGINE_DIR}/renderer/glsl_source/cameraEffects_vp.glsl
${ENGINE_DIR}/renderer/glsl_source/computeLight_fp.glsl
Expand Down
26 changes: 6 additions & 20 deletions src/engine/renderer/gl_shader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ GLShader_screenMaterial *gl_screenShaderMaterial = nullptr;
GLShader_portal *gl_portalShader = nullptr;
GLShader_contrast *gl_contrastShader = nullptr;
GLShader_cameraEffects *gl_cameraEffectsShader = nullptr;
GLShader_blurX *gl_blurXShader = nullptr;
GLShader_blurY *gl_blurYShader = nullptr;
GLShader_blur *gl_blurShader = nullptr;
GLShader_debugShadowMap *gl_debugShadowMapShader = nullptr;
GLShader_liquid *gl_liquidShader = nullptr;
GLShader_liquidMaterial *gl_liquidShaderMaterial = nullptr;
Expand Down Expand Up @@ -2874,30 +2873,17 @@ void GLShader_cameraEffects::SetShaderProgramUniforms( shaderProgram_t *shaderPr
glUniform1i( glGetUniformLocation( shaderProgram->program, "u_ColorMap3D" ), 3 );
}

GLShader_blurX::GLShader_blurX( GLShaderManager *manager ) :
GLShader( "blurX", ATTR_POSITION, manager ),
GLShader_blur::GLShader_blur( GLShaderManager *manager ) :
GLShader( "blur", ATTR_POSITION, manager ),
u_ColorMap( this ),
u_ModelViewProjectionMatrix( this ),
u_DeformMagnitude( this ),
u_TexScale( this )
u_TexScale( this ),
u_Horizontal( this )
{
}

void GLShader_blurX::SetShaderProgramUniforms( shaderProgram_t *shaderProgram )
{
glUniform1i( glGetUniformLocation( shaderProgram->program, "u_ColorMap" ), 0 );
}

GLShader_blurY::GLShader_blurY( GLShaderManager *manager ) :
GLShader( "blurY", ATTR_POSITION, manager ),
u_ColorMap( this ),
u_ModelViewProjectionMatrix( this ),
u_DeformMagnitude( this ),
u_TexScale( this )
{
}

void GLShader_blurY::SetShaderProgramUniforms( shaderProgram_t *shaderProgram )
void GLShader_blur::SetShaderProgramUniforms( shaderProgram_t *shaderProgram )
{
glUniform1i( glGetUniformLocation( shaderProgram->program, "u_ColorMap" ), 0 );
}
Expand Down
34 changes: 17 additions & 17 deletions src/engine/renderer/gl_shader.h
Original file line number Diff line number Diff line change
Expand Up @@ -3754,6 +3754,18 @@ class u_blurVec :
}
};

class u_Horizontal :
GLUniform1Bool {
public:
u_Horizontal( GLShader* shader ) :
GLUniform1Bool( shader, "u_Horizontal", true ) {
}

void SetUniform_Horizontal( bool horizontal ) {
this->SetValue( horizontal );
}
};

class u_TexScale :
GLUniform2f
{
Expand Down Expand Up @@ -4486,27 +4498,16 @@ class GLShader_cameraEffects :
void SetShaderProgramUniforms( shaderProgram_t *shaderProgram ) override;
};

class GLShader_blurX :
public GLShader,
public u_ColorMap,
public u_ModelViewProjectionMatrix,
public u_DeformMagnitude,
public u_TexScale
{
public:
GLShader_blurX( GLShaderManager *manager );
void SetShaderProgramUniforms( shaderProgram_t *shaderProgram ) override;
};

class GLShader_blurY :
class GLShader_blur :
public GLShader,
public u_ColorMap,
public u_ModelViewProjectionMatrix,
public u_DeformMagnitude,
public u_TexScale
public u_TexScale,
public u_Horizontal
{
public:
GLShader_blurY( GLShaderManager *manager );
GLShader_blur( GLShaderManager *manager );
void SetShaderProgramUniforms( shaderProgram_t *shaderProgram ) override;
};

Expand Down Expand Up @@ -4740,8 +4741,7 @@ extern GLShader_screenMaterial *gl_screenShaderMaterial;
extern GLShader_portal *gl_portalShader;
extern GLShader_contrast *gl_contrastShader;
extern GLShader_cameraEffects *gl_cameraEffectsShader;
extern GLShader_blurX *gl_blurXShader;
extern GLShader_blurY *gl_blurYShader;
extern GLShader_blur *gl_blurShader;
extern GLShader_debugShadowMap *gl_debugShadowMapShader;
extern GLShader_liquid *gl_liquidShader;
extern GLShader_liquidMaterial *gl_liquidShaderMaterial;
Expand Down
63 changes: 0 additions & 63 deletions src/engine/renderer/glsl_source/blurY_fp.glsl

This file was deleted.

33 changes: 0 additions & 33 deletions src/engine/renderer/glsl_source/blurY_vp.glsl

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -20,44 +20,39 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
===========================================================================
*/

/* blurX_fp.glsl */
/* blur_fp.glsl */

uniform sampler2D u_ColorMap;
uniform float u_DeformMagnitude;
uniform vec2 u_TexScale;
uniform sampler2D u_ColorMap;
uniform float u_DeformMagnitude;
uniform vec2 u_TexScale;
uniform bool u_Horizontal;

#if __VERSION__ > 120
out vec4 outputColor;
#else
#define outputColor gl_FragColor
#endif
DECLARE_OUTPUT( vec4 )

void main()
{
void main() {
vec2 st = gl_FragCoord.st * u_TexScale;

#if 0
float gaussFact[3] = float[3](1.0, 2.0, 1.0);
float gaussFact[3] = float[3] ( 1.0, 2.0, 1.0 );
float gaussSum = 4;
const int tap = 1;
#elif 0
float gaussFact[5] = float[5](1.0, 4.0, 6.0, 4.0, 1.0);
float gaussFact[5] = float[5] ( 1.0, 4.0, 6.0, 4.0, 1.0 );
float gaussSum = 16.0;
const int tap = 2;
#elif 1
float gaussFact[7] = float[7](1.0, 6.0, 15.0, 20.0, 15.0, 6.0, 1.0);
float gaussFact[7] = float[7] ( 1.0, 6.0, 15.0, 20.0, 15.0, 6.0, 1.0 );
float gaussSum = 64.0;
const int tap = 3;
#endif

// do a full gaussian blur
vec4 sumColors = vec4(0.0);
vec4 sumColors = vec4( 0.0 );

for(int t = -tap; t <= tap; t++)
{
for( int t = -tap; t <= tap; t++ ) {
float weight = gaussFact[t + tap];
sumColors += texture2D(u_ColorMap, st + vec2(t, 0) * u_TexScale * u_DeformMagnitude) * weight;
sumColors += texture2D( u_ColorMap, st + vec2( u_Horizontal ? t : 0, u_Horizontal ? 0 : t ) * u_TexScale * u_DeformMagnitude ) * weight;
}

outputColor = sumColors * (1.0 / gaussSum);
outputColor = sumColors * ( 1.0 / gaussSum );
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,13 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
===========================================================================
*/

/* blurX_vp.glsl */
/* blur_vp.glsl */

IN vec3 attr_Position;
IN vec3 attr_Position;

uniform mat4 u_ModelViewProjectionMatrix;
uniform mat4 u_ModelViewProjectionMatrix;

void main()
{
void main() {
// transform vertex position into homogenous clip-space
gl_Position = u_ModelViewProjectionMatrix * vec4(attr_Position, 1.0);
gl_Position = u_ModelViewProjectionMatrix * vec4( attr_Position, 1.0 );
}
12 changes: 4 additions & 8 deletions src/engine/renderer/shaders.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@
#include "vertexAnimation_vp.glsl.h"
#include "vertexSimple_vp.glsl.h"
#include "vertexSkinning_vp.glsl.h"
#include "blurX_fp.glsl.h"
#include "blurX_vp.glsl.h"
#include "blurY_fp.glsl.h"
#include "blurY_vp.glsl.h"
#include "blur_fp.glsl.h"
#include "blur_vp.glsl.h"
#include "cameraEffects_fp.glsl.h"
#include "cameraEffects_vp.glsl.h"
#include "computeLight_fp.glsl.h"
Expand Down Expand Up @@ -68,10 +66,8 @@
#include "processSurfaces_cp.glsl.h"

std::unordered_map<std::string, std::string> shadermap({
{ "blurX_fp.glsl", std::string(reinterpret_cast<const char*>(blurX_fp_glsl), sizeof(blurX_fp_glsl)) },
{ "blurX_vp.glsl", std::string(reinterpret_cast<const char*>(blurX_vp_glsl), sizeof(blurX_vp_glsl)) },
{ "blurY_fp.glsl", std::string(reinterpret_cast<const char*>(blurY_fp_glsl), sizeof(blurY_fp_glsl)) },
{ "blurY_vp.glsl", std::string(reinterpret_cast<const char*>(blurY_vp_glsl), sizeof(blurY_vp_glsl)) },
{ "blur_fp.glsl", std::string(reinterpret_cast<const char*>(blur_fp_glsl), sizeof(blur_fp_glsl)) },
{ "blur_vp.glsl", std::string(reinterpret_cast<const char*>(blur_vp_glsl), sizeof(blur_vp_glsl)) },
{ "cameraEffects_fp.glsl", std::string(reinterpret_cast<const char*>(cameraEffects_fp_glsl), sizeof(cameraEffects_fp_glsl)) },
{ "cameraEffects_vp.glsl", std::string(reinterpret_cast<const char*>(cameraEffects_vp_glsl), sizeof(cameraEffects_vp_glsl)) },
{ "computeLight_fp.glsl", std::string(reinterpret_cast<const char*>(computeLight_fp_glsl), sizeof(computeLight_fp_glsl)) },
Expand Down
Loading

0 comments on commit 13e2d66

Please sign in to comment.