Skip to content

Commit 6416a75

Browse files
author
Jaedan
committed
Add a bloom example
1 parent 33cdf2a commit 6416a75

File tree

12 files changed

+735
-0
lines changed

12 files changed

+735
-0
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ add_executable(SDL_gpu_examples
2828
Examples/TriangleMSAA.c
2929
Examples/Cubemap.c
3030
Examples/WindowResize.c
31+
Examples/Bloom.c
3132
)
3233

3334
target_include_directories(SDL_gpu_examples PRIVATE . spirv)
1016 Bytes
Binary file not shown.
5.34 KB
Binary file not shown.
3.79 KB
Binary file not shown.
1.1 KB
Binary file not shown.

Content/Shaders/Source/Bloom.vert

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#version 450
2+
3+
layout (location = 0) in vec3 Position;
4+
layout (location = 1) in vec2 TexCoord;
5+
layout (location = 0) out vec2 outTexCoord;
6+
7+
void main()
8+
{
9+
outTexCoord = TexCoord;
10+
gl_Position = vec4(Position, 1);
11+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#version 450
2+
3+
// This shader performs downsampling on a texture,
4+
// as taken from the Call Of Duty method, presented at ACM Siggraph 2014.
5+
6+
layout (location = 0) in vec2 TexCoord;
7+
layout (location = 0) out vec4 FragColor;
8+
layout (set = 2, binding = 0) uniform sampler2D Sampler;
9+
10+
void main()
11+
{
12+
vec2 srcTexelSize = vec2(1.0, 1.0) / textureSize(Sampler, 0);
13+
float x = srcTexelSize.x;
14+
float y = srcTexelSize.y;
15+
16+
// Take 13 samples around current texel:
17+
// a - b - c
18+
// - j - k -
19+
// d - e - f
20+
// - l - m -
21+
// g - h - i
22+
// === ('e' is the current texel) ===
23+
vec3 a = texture(Sampler, vec2(TexCoord.x - 2*x, TexCoord.y + 2*y)).rgb;
24+
vec3 b = texture(Sampler, vec2(TexCoord.x, TexCoord.y + 2*y)).rgb;
25+
vec3 c = texture(Sampler, vec2(TexCoord.x + 2*x, TexCoord.y + 2*y)).rgb;
26+
27+
vec3 d = texture(Sampler, vec2(TexCoord.x - 2*x, TexCoord.y)).rgb;
28+
vec3 e = texture(Sampler, vec2(TexCoord.x, TexCoord.y)).rgb;
29+
vec3 f = texture(Sampler, vec2(TexCoord.x + 2*x, TexCoord.y)).rgb;
30+
31+
vec3 g = texture(Sampler, vec2(TexCoord.x - 2*x, TexCoord.y - 2*y)).rgb;
32+
vec3 h = texture(Sampler, vec2(TexCoord.x, TexCoord.y - 2*y)).rgb;
33+
vec3 i = texture(Sampler, vec2(TexCoord.x + 2*x, TexCoord.y - 2*y)).rgb;
34+
35+
vec3 j = texture(Sampler, vec2(TexCoord.x - x, TexCoord.y + y)).rgb;
36+
vec3 k = texture(Sampler, vec2(TexCoord.x + x, TexCoord.y + y)).rgb;
37+
vec3 l = texture(Sampler, vec2(TexCoord.x - x, TexCoord.y - y)).rgb;
38+
vec3 m = texture(Sampler, vec2(TexCoord.x + x, TexCoord.y - y)).rgb;
39+
40+
// Apply weighted distribution:
41+
// 0.5 + 0.125 + 0.125 + 0.125 + 0.125 = 1
42+
// a,b,d,e * 0.125
43+
// b,c,e,f * 0.125
44+
// d,e,g,h * 0.125
45+
// e,f,h,i * 0.125
46+
// j,k,l,m * 0.5
47+
// This shows 5 square areas that are being sampled. But some of them overlap,
48+
// so to have an energy preserving downsample we need to make some adjustments.
49+
// The weights are distributed, so that the sum of j,k,l,m (e.g.)
50+
// contribute 0.5 to the final color output. The code below is written
51+
// to effectively yield this sum. We get:
52+
// 0.125*5 + 0.03125*4 + 0.0625*4 = 1
53+
vec3 color = e * 0.125;
54+
color = e*0.125;
55+
color += (a+c+g+i)*0.03125;
56+
color += (b+d+f+h)*0.0625;
57+
color += (j+k+l+m)*0.125;
58+
59+
FragColor = vec4(color, 0.0);
60+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#version 450
2+
3+
// This shader performs upsampling with blur on a texture
4+
// as taken from the Call Of Duty method, presented at ACM Siggraph 2014.
5+
6+
layout (location = 0) in vec2 TexCoord;
7+
layout (location = 0) out vec4 FragColor;
8+
layout (set = 2, binding = 0) uniform sampler2D Sampler;
9+
layout (set = 3, binding = 0) uniform UniformBlock
10+
{
11+
float FilterRadius;
12+
};
13+
14+
void main()
15+
{
16+
// The filter kernel is applied with a radius, specified in texture
17+
// coordinates, so that the radius will vary across mip resolutions.
18+
float x = FilterRadius;
19+
float y = FilterRadius;
20+
21+
// Take 9 samples around current texel:
22+
// a - b - c
23+
// d - e - f
24+
// g - h - i
25+
// === ('e' is the current texel) ===
26+
vec3 a = texture(Sampler, vec2(TexCoord.x - x, TexCoord.y + y)).rgb;
27+
vec3 b = texture(Sampler, vec2(TexCoord.x, TexCoord.y + y)).rgb;
28+
vec3 c = texture(Sampler, vec2(TexCoord.x + x, TexCoord.y + y)).rgb;
29+
30+
vec3 d = texture(Sampler, vec2(TexCoord.x - x, TexCoord.y)).rgb;
31+
vec3 e = texture(Sampler, vec2(TexCoord.x, TexCoord.y)).rgb;
32+
vec3 f = texture(Sampler, vec2(TexCoord.x + x, TexCoord.y)).rgb;
33+
34+
vec3 g = texture(Sampler, vec2(TexCoord.x - x, TexCoord.y - y)).rgb;
35+
vec3 h = texture(Sampler, vec2(TexCoord.x, TexCoord.y - y)).rgb;
36+
vec3 i = texture(Sampler, vec2(TexCoord.x + x, TexCoord.y - y)).rgb;
37+
38+
// Apply weighted distribution, by using a 3x3 tent filter:
39+
// 1 | 1 2 1 |
40+
// -- * | 2 4 2 |
41+
// 16 | 1 2 1 |
42+
vec3 color = e*4.0;
43+
color += (b+d+f+h)*2.0;
44+
color += (a+c+g+i);
45+
color *= 1.0 / 16.0;
46+
47+
FragColor = vec4(color, 1.0);
48+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#version 450
2+
3+
layout (location = 0) in vec2 TexCoord;
4+
layout (location = 0) out vec4 FragColor;
5+
layout (set = 2, binding = 0) uniform sampler2D Primary;
6+
layout (set = 2, binding = 1) uniform sampler2D Secondary;
7+
layout (set = 3, binding = 0) uniform UniformBlock
8+
{
9+
float Weight;
10+
};
11+
12+
void main()
13+
{
14+
vec4 primary = texture(Primary, TexCoord);
15+
vec4 secondary = texture(Secondary, TexCoord);
16+
17+
FragColor = mix(primary, secondary, Weight);
18+
}

0 commit comments

Comments
 (0)