Skip to content

Commit 9364a8f

Browse files
committed
Use one shader for blur
The only difference between the 2 shaders was switching the 2 components of a vec2, which is better done by using a uniform instead of switching shaders. Simplifies the code as well.
1 parent cea890f commit 9364a8f

File tree

10 files changed

+84
-222
lines changed

10 files changed

+84
-222
lines changed

src.cmake

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,8 @@ set(GLSLSOURCELIST
162162
${ENGINE_DIR}/renderer/glsl_source/vertexAnimation_vp.glsl
163163
${ENGINE_DIR}/renderer/glsl_source/vertexSimple_vp.glsl
164164
${ENGINE_DIR}/renderer/glsl_source/vertexSkinning_vp.glsl
165-
${ENGINE_DIR}/renderer/glsl_source/blurX_fp.glsl
166-
${ENGINE_DIR}/renderer/glsl_source/blurX_vp.glsl
167-
${ENGINE_DIR}/renderer/glsl_source/blurY_fp.glsl
168-
${ENGINE_DIR}/renderer/glsl_source/blurY_vp.glsl
165+
${ENGINE_DIR}/renderer/glsl_source/blur_fp.glsl
166+
${ENGINE_DIR}/renderer/glsl_source/blur_vp.glsl
169167
${ENGINE_DIR}/renderer/glsl_source/cameraEffects_fp.glsl
170168
${ENGINE_DIR}/renderer/glsl_source/cameraEffects_vp.glsl
171169
${ENGINE_DIR}/renderer/glsl_source/computeLight_fp.glsl

src/engine/renderer/gl_shader.cpp

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,7 @@ GLShader_screenMaterial *gl_screenShaderMaterial = nullptr;
7070
GLShader_portal *gl_portalShader = nullptr;
7171
GLShader_contrast *gl_contrastShader = nullptr;
7272
GLShader_cameraEffects *gl_cameraEffectsShader = nullptr;
73-
GLShader_blurX *gl_blurXShader = nullptr;
74-
GLShader_blurY *gl_blurYShader = nullptr;
73+
GLShader_blur *gl_blurShader = nullptr;
7574
GLShader_debugShadowMap *gl_debugShadowMapShader = nullptr;
7675
GLShader_liquid *gl_liquidShader = nullptr;
7776
GLShader_liquidMaterial *gl_liquidShaderMaterial = nullptr;
@@ -2874,30 +2873,17 @@ void GLShader_cameraEffects::SetShaderProgramUniforms( shaderProgram_t *shaderPr
28742873
glUniform1i( glGetUniformLocation( shaderProgram->program, "u_ColorMap3D" ), 3 );
28752874
}
28762875

2877-
GLShader_blurX::GLShader_blurX( GLShaderManager *manager ) :
2878-
GLShader( "blurX", ATTR_POSITION, manager ),
2876+
GLShader_blur::GLShader_blur( GLShaderManager *manager ) :
2877+
GLShader( "blur", ATTR_POSITION, manager ),
28792878
u_ColorMap( this ),
28802879
u_ModelViewProjectionMatrix( this ),
28812880
u_DeformMagnitude( this ),
2882-
u_TexScale( this )
2881+
u_TexScale( this ),
2882+
u_Horizontal( this )
28832883
{
28842884
}
28852885

2886-
void GLShader_blurX::SetShaderProgramUniforms( shaderProgram_t *shaderProgram )
2887-
{
2888-
glUniform1i( glGetUniformLocation( shaderProgram->program, "u_ColorMap" ), 0 );
2889-
}
2890-
2891-
GLShader_blurY::GLShader_blurY( GLShaderManager *manager ) :
2892-
GLShader( "blurY", ATTR_POSITION, manager ),
2893-
u_ColorMap( this ),
2894-
u_ModelViewProjectionMatrix( this ),
2895-
u_DeformMagnitude( this ),
2896-
u_TexScale( this )
2897-
{
2898-
}
2899-
2900-
void GLShader_blurY::SetShaderProgramUniforms( shaderProgram_t *shaderProgram )
2886+
void GLShader_blur::SetShaderProgramUniforms( shaderProgram_t *shaderProgram )
29012887
{
29022888
glUniform1i( glGetUniformLocation( shaderProgram->program, "u_ColorMap" ), 0 );
29032889
}

src/engine/renderer/gl_shader.h

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3754,6 +3754,18 @@ class u_blurVec :
37543754
}
37553755
};
37563756

3757+
class u_Horizontal :
3758+
GLUniform1Bool {
3759+
public:
3760+
u_Horizontal( GLShader* shader ) :
3761+
GLUniform1Bool( shader, "u_Horizontal", true ) {
3762+
}
3763+
3764+
void SetUniform_Horizontal( bool horizontal ) {
3765+
this->SetValue( horizontal );
3766+
}
3767+
};
3768+
37573769
class u_TexScale :
37583770
GLUniform2f
37593771
{
@@ -4486,27 +4498,16 @@ class GLShader_cameraEffects :
44864498
void SetShaderProgramUniforms( shaderProgram_t *shaderProgram ) override;
44874499
};
44884500

4489-
class GLShader_blurX :
4490-
public GLShader,
4491-
public u_ColorMap,
4492-
public u_ModelViewProjectionMatrix,
4493-
public u_DeformMagnitude,
4494-
public u_TexScale
4495-
{
4496-
public:
4497-
GLShader_blurX( GLShaderManager *manager );
4498-
void SetShaderProgramUniforms( shaderProgram_t *shaderProgram ) override;
4499-
};
4500-
4501-
class GLShader_blurY :
4501+
class GLShader_blur :
45024502
public GLShader,
45034503
public u_ColorMap,
45044504
public u_ModelViewProjectionMatrix,
45054505
public u_DeformMagnitude,
4506-
public u_TexScale
4506+
public u_TexScale,
4507+
public u_Horizontal
45074508
{
45084509
public:
4509-
GLShader_blurY( GLShaderManager *manager );
4510+
GLShader_blur( GLShaderManager *manager );
45104511
void SetShaderProgramUniforms( shaderProgram_t *shaderProgram ) override;
45114512
};
45124513

@@ -4740,8 +4741,7 @@ extern GLShader_screenMaterial *gl_screenShaderMaterial;
47404741
extern GLShader_portal *gl_portalShader;
47414742
extern GLShader_contrast *gl_contrastShader;
47424743
extern GLShader_cameraEffects *gl_cameraEffectsShader;
4743-
extern GLShader_blurX *gl_blurXShader;
4744-
extern GLShader_blurY *gl_blurYShader;
4744+
extern GLShader_blur *gl_blurShader;
47454745
extern GLShader_debugShadowMap *gl_debugShadowMapShader;
47464746
extern GLShader_liquid *gl_liquidShader;
47474747
extern GLShader_liquidMaterial *gl_liquidShaderMaterial;

src/engine/renderer/glsl_source/blurY_fp.glsl

Lines changed: 0 additions & 63 deletions
This file was deleted.

src/engine/renderer/glsl_source/blurY_vp.glsl

Lines changed: 0 additions & 33 deletions
This file was deleted.

src/engine/renderer/glsl_source/blurX_fp.glsl renamed to src/engine/renderer/glsl_source/blur_fp.glsl

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,44 +20,39 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
2020
===========================================================================
2121
*/
2222

23-
/* blurX_fp.glsl */
23+
/* blur_fp.glsl */
2424

25-
uniform sampler2D u_ColorMap;
26-
uniform float u_DeformMagnitude;
27-
uniform vec2 u_TexScale;
25+
uniform sampler2D u_ColorMap;
26+
uniform float u_DeformMagnitude;
27+
uniform vec2 u_TexScale;
28+
uniform bool u_Horizontal;
2829

29-
#if __VERSION__ > 120
30-
out vec4 outputColor;
31-
#else
32-
#define outputColor gl_FragColor
33-
#endif
30+
DECLARE_OUTPUT( vec4 )
3431

35-
void main()
36-
{
32+
void main() {
3733
vec2 st = gl_FragCoord.st * u_TexScale;
3834

3935
#if 0
40-
float gaussFact[3] = float[3](1.0, 2.0, 1.0);
36+
float gaussFact[3] = float[3] ( 1.0, 2.0, 1.0 );
4137
float gaussSum = 4;
4238
const int tap = 1;
4339
#elif 0
44-
float gaussFact[5] = float[5](1.0, 4.0, 6.0, 4.0, 1.0);
40+
float gaussFact[5] = float[5] ( 1.0, 4.0, 6.0, 4.0, 1.0 );
4541
float gaussSum = 16.0;
4642
const int tap = 2;
4743
#elif 1
48-
float gaussFact[7] = float[7](1.0, 6.0, 15.0, 20.0, 15.0, 6.0, 1.0);
44+
float gaussFact[7] = float[7] ( 1.0, 6.0, 15.0, 20.0, 15.0, 6.0, 1.0 );
4945
float gaussSum = 64.0;
5046
const int tap = 3;
5147
#endif
5248

5349
// do a full gaussian blur
54-
vec4 sumColors = vec4(0.0);
50+
vec4 sumColors = vec4( 0.0 );
5551

56-
for(int t = -tap; t <= tap; t++)
57-
{
52+
for( int t = -tap; t <= tap; t++ ) {
5853
float weight = gaussFact[t + tap];
59-
sumColors += texture2D(u_ColorMap, st + vec2(t, 0) * u_TexScale * u_DeformMagnitude) * weight;
54+
sumColors += texture2D( u_ColorMap, st + vec2( u_Horizontal ? t : 0, u_Horizontal ? 0 : t ) * u_TexScale * u_DeformMagnitude ) * weight;
6055
}
6156

62-
outputColor = sumColors * (1.0 / gaussSum);
57+
outputColor = sumColors * ( 1.0 / gaussSum );
6358
}

src/engine/renderer/glsl_source/blurX_vp.glsl renamed to src/engine/renderer/glsl_source/blur_vp.glsl

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,13 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
2020
===========================================================================
2121
*/
2222

23-
/* blurX_vp.glsl */
23+
/* blur_vp.glsl */
2424

25-
IN vec3 attr_Position;
25+
IN vec3 attr_Position;
2626

27-
uniform mat4 u_ModelViewProjectionMatrix;
27+
uniform mat4 u_ModelViewProjectionMatrix;
2828

29-
void main()
30-
{
29+
void main() {
3130
// transform vertex position into homogenous clip-space
32-
gl_Position = u_ModelViewProjectionMatrix * vec4(attr_Position, 1.0);
31+
gl_Position = u_ModelViewProjectionMatrix * vec4( attr_Position, 1.0 );
3332
}

src/engine/renderer/shaders.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,8 @@
66
#include "vertexAnimation_vp.glsl.h"
77
#include "vertexSimple_vp.glsl.h"
88
#include "vertexSkinning_vp.glsl.h"
9-
#include "blurX_fp.glsl.h"
10-
#include "blurX_vp.glsl.h"
11-
#include "blurY_fp.glsl.h"
12-
#include "blurY_vp.glsl.h"
9+
#include "blur_fp.glsl.h"
10+
#include "blur_vp.glsl.h"
1311
#include "cameraEffects_fp.glsl.h"
1412
#include "cameraEffects_vp.glsl.h"
1513
#include "computeLight_fp.glsl.h"
@@ -68,10 +66,8 @@
6866
#include "processSurfaces_cp.glsl.h"
6967

7068
std::unordered_map<std::string, std::string> shadermap({
71-
{ "blurX_fp.glsl", std::string(reinterpret_cast<const char*>(blurX_fp_glsl), sizeof(blurX_fp_glsl)) },
72-
{ "blurX_vp.glsl", std::string(reinterpret_cast<const char*>(blurX_vp_glsl), sizeof(blurX_vp_glsl)) },
73-
{ "blurY_fp.glsl", std::string(reinterpret_cast<const char*>(blurY_fp_glsl), sizeof(blurY_fp_glsl)) },
74-
{ "blurY_vp.glsl", std::string(reinterpret_cast<const char*>(blurY_vp_glsl), sizeof(blurY_vp_glsl)) },
69+
{ "blur_fp.glsl", std::string(reinterpret_cast<const char*>(blur_fp_glsl), sizeof(blur_fp_glsl)) },
70+
{ "blur_vp.glsl", std::string(reinterpret_cast<const char*>(blur_vp_glsl), sizeof(blur_vp_glsl)) },
7571
{ "cameraEffects_fp.glsl", std::string(reinterpret_cast<const char*>(cameraEffects_fp_glsl), sizeof(cameraEffects_fp_glsl)) },
7672
{ "cameraEffects_vp.glsl", std::string(reinterpret_cast<const char*>(cameraEffects_vp_glsl), sizeof(cameraEffects_vp_glsl)) },
7773
{ "computeLight_fp.glsl", std::string(reinterpret_cast<const char*>(computeLight_fp_glsl), sizeof(computeLight_fp_glsl)) },

0 commit comments

Comments
 (0)