Skip to content

Commit 90d15eb

Browse files
committed
Gaussian blur postprocessing
1 parent 967e6e2 commit 90d15eb

File tree

10 files changed

+297
-83
lines changed

10 files changed

+297
-83
lines changed

shaders/compile.bat

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,6 @@ glslc.exe -fshader-stage=frag texture_frag.glsl -o texture_frag.spv
77
glslc.exe -fshader-stage=vert model_vert.glsl -o model_vert.spv
88
glslc.exe -fshader-stage=frag model_frag.glsl -o model_frag.spv
99
glslc.exe -fshader-stage=vert postprocess_vert.glsl -o postprocess_vert.spv
10-
glslc.exe -fshader-stage=frag postprocess_frag.glsl -o postprocess_frag.spv
10+
glslc.exe -fshader-stage=frag postprocess_frag.glsl -o postprocess_frag.spv
11+
glslc.exe -fshader-stage=vert gaussian_vert.glsl -o gaussian_vert.spv
12+
glslc.exe -fshader-stage=frag gaussian_frag.glsl -o gaussian_frag.spv

shaders/compile.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,6 @@ glslc -fshader-stage=frag texture_frag.glsl -o texture_frag.spv
88
glslc -fshader-stage=vert model_vert.glsl -o model_vert.spv
99
glslc -fshader-stage=frag model_frag.glsl -o model_frag.spv
1010
glslc -fshader-stage=vert postprocess_vert.glsl -o postprocess_vert.spv
11-
glslc -fshader-stage=frag postprocess_frag.glsl -o postprocess_frag.spv
11+
glslc -fshader-stage=frag postprocess_frag.glsl -o postprocess_frag.spv
12+
glslc -fshader-stage=vert gaussian_vert.glsl -o gaussian_vert.spv
13+
glslc -fshader-stage=frag gaussian_frag.glsl -o gaussian_frag.spv

shaders/gaussian_frag.glsl

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#version 450 core
2+
3+
// Two pass gaussian blur
4+
// https://www.rastergrid.com/blog/2010/09/efficient-gaussian-blur-with-linear-sampling/
5+
6+
layout(binding = 0) uniform sampler2D colorTex;
7+
8+
layout(location = 0) in vec2 texCoord;
9+
layout (location = 0) out vec4 outColor;
10+
11+
layout(constant_id = 0) const bool VERTICAL = false;
12+
13+
layout(push_constant) uniform pushConstants {
14+
float pixelSize; // pixel size eg. 1 / screenWidth and 1 / screenHeight
15+
} u_pushConstants;
16+
17+
float offset[3] = float[](0.0, 1.3846153846, 3.2307692308);
18+
float weight[3] = float[](0.2270270270, 0.3162162162, 0.0702702703);
19+
20+
void main(void) {
21+
outColor = texture(colorTex, texCoord) * weight[0];
22+
for (int i=1; i<3; i++) {
23+
24+
if(VERTICAL) {
25+
outColor += texture(colorTex, (vec2(texCoord) + vec2(0.0, offset[i] * u_pushConstants.pixelSize))) * weight[i];
26+
outColor += texture(colorTex, (vec2(texCoord) - vec2(0.0, offset[i] * u_pushConstants.pixelSize))) * weight[i];
27+
} else {
28+
// Horizontal
29+
outColor += texture(colorTex, (vec2(texCoord) + vec2(offset[i] * u_pushConstants.pixelSize, 0.0))) * weight[i];
30+
outColor += texture(colorTex, (vec2(texCoord) - vec2(offset[i] * u_pushConstants.pixelSize, 0.0))) * weight[i];
31+
}
32+
}
33+
}

shaders/gaussian_frag.spv

3.05 KB
Binary file not shown.

shaders/gaussian_vert.glsl

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#version 450 core
2+
3+
//Basic Buffer/Layout-less fullscreen triangle vertex shader
4+
vec2 triangleVertex(in int vertID, out vec2 texcoord) {
5+
/*
6+
//See: https://web.archive.org/web/20140719063725/http://www.altdev.co/2011/08/08/interesting-vertex-shader-trick/
7+
1
8+
( 0, 2)
9+
[-1, 3] [ 3, 3]
10+
.
11+
|`.
12+
| `.
13+
| `.
14+
'------`
15+
0 2
16+
( 0, 0) ( 2, 0)
17+
[-1,-1] [ 3,-1]
18+
ID=0 -> Pos=[-1,-1], Tex=(0,0)
19+
ID=1 -> Pos=[-1, 3], Tex=(0,2)
20+
ID=2 -> Pos=[ 3,-1], Tex=(2,0)
21+
*/
22+
23+
texcoord.x = (vertID == 2) ? 2.0 : 0.0;
24+
texcoord.y = (vertID == 1) ? 2.0 : 0.0;
25+
26+
return texcoord * vec2(2.0, -2.0) + vec2(-1.0, 1.0);
27+
}
28+
29+
layout(location = 0) out vec2 texCoord;
30+
31+
void main(void) {
32+
vec2 pos = triangleVertex(gl_VertexIndex, texCoord);
33+
34+
gl_Position = vec4(pos, 1.0, 1.0);
35+
}

shaders/gaussian_vert.spv

1.7 KB
Binary file not shown.

0 commit comments

Comments
 (0)