Debian Codegraph @

servers/rendering/renderer_rd/shaders/effects/luminance_reduce_raster.glsl

raw
1 /* clang-format off */
2 #[vertex]
3
4 #version 450
5
6 #VERSION_DEFINES
7
8 #include "luminance_reduce_raster_inc.glsl"
9
10 layout(location = 0) out vec2 uv_interp;
11 /* clang-format on */
12
13 void main() {
14 vec2 base_arr[3] = vec2[](vec2(-1.0, -1.0), vec2(-1.0, 3.0), vec2(3.0, -1.0));
15 gl_Position = vec4(base_arr[gl_VertexIndex], 0.0, 1.0);
16 uv_interp = clamp(gl_Position.xy, vec2(0.0, 0.0), vec2(1.0, 1.0)) * 2.0; // saturate(x) * 2.0
17 }
18
19 /* clang-format off */
20 #[fragment]
21
22 #version 450
23
24 #VERSION_DEFINES
25
26 #include "luminance_reduce_raster_inc.glsl"
27
28 layout(location = 0) in vec2 uv_interp;
29 /* clang-format on */
30
31 layout(set = 0, binding = 0) uniform sampler2D source_exposure;
32
33 #ifdef FINAL_PASS
34 layout(set = 1, binding = 0) uniform sampler2D prev_luminance;
35 #endif
36
37 layout(location = 0) out highp float luminance;
38
39 void main() {
40 ivec2 dest_pos = ivec2(uv_interp * settings.dest_size);
41 ivec2 src_pos = ivec2(uv_interp * settings.source_size);
42
43 ivec2 next_pos = (dest_pos + ivec2(1)) * settings.source_size / settings.dest_size;
44 next_pos = max(next_pos, src_pos + ivec2(1)); //so it at least reads one pixel
45
46 highp vec3 source_color = vec3(0.0);
47 for (int i = src_pos.x; i < next_pos.x; i++) {
48 for (int j = src_pos.y; j < next_pos.y; j++) {
49 source_color += texelFetch(source_exposure, ivec2(i, j), 0).rgb;
50 }
51 }
52
53 source_color /= float((next_pos.x - src_pos.x) * (next_pos.y - src_pos.y));
54
55 #ifdef FIRST_PASS
56 luminance = max(source_color.r, max(source_color.g, source_color.b));
57
58 // This formula should be more "accurate" but gave an overexposed result when testing.
59 // Leaving it here so we can revisit it if we want.
60 // luminance = source_color.r * 0.21 + source_color.g * 0.71 + source_color.b * 0.07;
61 #else
62 luminance = source_color.r;
63 #endif
64
65 #ifdef FINAL_PASS
66 // Obtain our target luminance
67 luminance = clamp(luminance, settings.min_luminance, settings.max_luminance);
68
69 // Now smooth to our transition
70 highp float prev_lum = texelFetch(prev_luminance, ivec2(0, 0), 0).r; //1 pixel previous luminance
71 luminance = prev_lum + (luminance - prev_lum) * clamp(settings.exposure_adjust, 0.0, 1.0);
72 #endif
73 }