Debian Codegraph @

drivers/gles3/shaders/effects/glow.glsl

raw
1 /* clang-format off */
2 #[modes]
3
4 // Based on Dual filtering glow as explained in Marius Bjørge presentation at Siggraph 2015 "Bandwidth-Efficient Rendering"
5
6 mode_filter = #define MODE_FILTER
7 mode_downsample = #define MODE_DOWNSAMPLE
8 mode_upsample = #define MODE_UPSAMPLE
9
10 #[specializations]
11
12 USE_MULTIVIEW = false
13
14 #[vertex]
15 layout(location = 0) in vec2 vertex_attrib;
16
17 /* clang-format on */
18
19 out vec2 uv_interp;
20
21 void main() {
22 uv_interp = vertex_attrib * 0.5 + 0.5;
23 gl_Position = vec4(vertex_attrib, 1.0, 1.0);
24 }
25
26 /* clang-format off */
27 #[fragment]
28 /* clang-format on */
29
30 #ifdef MODE_FILTER
31 #ifdef USE_MULTIVIEW
32 uniform sampler2DArray source_color; // texunit:0
33 #else
34 uniform sampler2D source_color; // texunit:0
35 #endif // USE_MULTIVIEW
36 uniform float view;
37 uniform vec2 pixel_size;
38 uniform float luminance_multiplier;
39 uniform float glow_bloom;
40 uniform float glow_hdr_threshold;
41 uniform float glow_hdr_scale;
42 uniform float glow_luminance_cap;
43 #endif // MODE_FILTER
44
45 #ifdef MODE_DOWNSAMPLE
46 uniform sampler2D source_color; // texunit:0
47 uniform vec2 pixel_size;
48 #endif // MODE_DOWNSAMPLE
49
50 #ifdef MODE_UPSAMPLE
51 uniform sampler2D source_color; // texunit:0
52 uniform vec2 pixel_size;
53 #endif // MODE_UPSAMPLE
54
55 in vec2 uv_interp;
56
57 layout(location = 0) out vec4 frag_color;
58
59 void main() {
60 #ifdef MODE_FILTER
61 // Note, we read from an image with double resolution, so we average those out
62 #ifdef USE_MULTIVIEW
63 vec2 half_pixel = pixel_size * 0.5;
64 vec3 uv = vec3(uv_interp, view);
65 vec3 color = textureLod(source_color, uv, 0.0).rgb * 4.0;
66 color += textureLod(source_color, uv - vec3(half_pixel, 0.0), 0.0).rgb;
67 color += textureLod(source_color, uv + vec3(half_pixel, 0.0), 0.0).rgb;
68 color += textureLod(source_color, uv - vec3(half_pixel.x, -half_pixel.y, 0.0), 0.0).rgb;
69 color += textureLod(source_color, uv + vec3(half_pixel.x, -half_pixel.y, 0.0), 0.0).rgb;
70 #else
71 vec2 half_pixel = pixel_size * 0.5;
72 vec2 uv = uv_interp;
73 vec3 color = textureLod(source_color, uv, 0.0).rgb * 4.0;
74 color += textureLod(source_color, uv - half_pixel, 0.0).rgb;
75 color += textureLod(source_color, uv + half_pixel, 0.0).rgb;
76 color += textureLod(source_color, uv - vec2(half_pixel.x, -half_pixel.y), 0.0).rgb;
77 color += textureLod(source_color, uv + vec2(half_pixel.x, -half_pixel.y), 0.0).rgb;
78 #endif // USE_MULTIVIEW
79 color /= luminance_multiplier * 8.0;
80
81 float feedback_factor = max(color.r, max(color.g, color.b));
82 float feedback = max(smoothstep(glow_hdr_threshold, glow_hdr_threshold + glow_hdr_scale, feedback_factor), glow_bloom);
83
84 color = min(color * feedback, vec3(glow_luminance_cap));
85
86 frag_color = vec4(luminance_multiplier * color, 1.0);
87 #endif // MODE_FILTER
88
89 #ifdef MODE_DOWNSAMPLE
90 vec2 half_pixel = pixel_size * 0.5;
91 vec4 color = textureLod(source_color, uv_interp, 0.0) * 4.0;
92 color += textureLod(source_color, uv_interp - half_pixel, 0.0);
93 color += textureLod(source_color, uv_interp + half_pixel, 0.0);
94 color += textureLod(source_color, uv_interp - vec2(half_pixel.x, -half_pixel.y), 0.0);
95 color += textureLod(source_color, uv_interp + vec2(half_pixel.x, -half_pixel.y), 0.0);
96 frag_color = color / 8.0;
97 #endif // MODE_DOWNSAMPLE
98
99 #ifdef MODE_UPSAMPLE
100 vec2 half_pixel = pixel_size * 0.5;
101
102 vec4 color = textureLod(source_color, uv_interp + vec2(-half_pixel.x * 2.0, 0.0), 0.0);
103 color += textureLod(source_color, uv_interp + vec2(-half_pixel.x, half_pixel.y), 0.0) * 2.0;
104 color += textureLod(source_color, uv_interp + vec2(0.0, half_pixel.y * 2.0), 0.0);
105 color += textureLod(source_color, uv_interp + vec2(half_pixel.x, half_pixel.y), 0.0) * 2.0;
106 color += textureLod(source_color, uv_interp + vec2(half_pixel.x * 2.0, 0.0), 0.0);
107 color += textureLod(source_color, uv_interp + vec2(half_pixel.x, -half_pixel.y), 0.0) * 2.0;
108 color += textureLod(source_color, uv_interp + vec2(0.0, -half_pixel.y * 2.0), 0.0);
109 color += textureLod(source_color, uv_interp + vec2(-half_pixel.x, -half_pixel.y), 0.0) * 2.0;
110
111 frag_color = color / 12.0;
112 #endif // MODE_UPSAMPLE
113 }