Debian Codegraph @

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

raw
1 #[compute]
2
3 #version 450
4
5 #VERSION_DEFINES
6
7 #define BLOCK_SIZE 8
8
9 layout(local_size_x = BLOCK_SIZE, local_size_y = BLOCK_SIZE, local_size_z = 1) in;
10
11 shared float tmp_data[BLOCK_SIZE * BLOCK_SIZE];
12
13 #ifdef READ_TEXTURE
14
15 //use for main texture
16 layout(set = 0, binding = 0) uniform sampler2D source_texture;
17
18 #else
19
20 //use for intermediate textures
21 layout(r32f, set = 0, binding = 0) uniform restrict readonly image2D source_luminance;
22
23 #endif
24
25 layout(r32f, set = 1, binding = 0) uniform restrict writeonly image2D dest_luminance;
26
27 #ifdef WRITE_LUMINANCE
28 layout(set = 2, binding = 0) uniform sampler2D prev_luminance;
29 #endif
30
31 layout(push_constant, std430) uniform Params {
32 ivec2 source_size;
33 float max_luminance;
34 float min_luminance;
35 float exposure_adjust;
36 float pad[3];
37 }
38 params;
39
40 void main() {
41 uint t = gl_LocalInvocationID.y * BLOCK_SIZE + gl_LocalInvocationID.x;
42 ivec2 pos = ivec2(gl_GlobalInvocationID.xy);
43
44 if (any(lessThan(pos, params.source_size))) {
45 #ifdef READ_TEXTURE
46 vec3 v = texelFetch(source_texture, pos, 0).rgb;
47 tmp_data[t] = max(v.r, max(v.g, v.b));
48 #else
49 tmp_data[t] = imageLoad(source_luminance, pos).r;
50 #endif
51 } else {
52 tmp_data[t] = 0.0;
53 }
54
55 groupMemoryBarrier();
56 barrier();
57
58 uint size = (BLOCK_SIZE * BLOCK_SIZE) >> 1;
59
60 do {
61 if (t < size) {
62 tmp_data[t] += tmp_data[t + size];
63 }
64 groupMemoryBarrier();
65 barrier();
66
67 size >>= 1;
68 } while (size >= 1);
69
70 if (t == 0) {
71 //compute rect size
72 ivec2 rect_size = min(params.source_size - pos, ivec2(BLOCK_SIZE));
73 float avg = tmp_data[0] / float(rect_size.x * rect_size.y);
74 //float avg = tmp_data[0] / float(BLOCK_SIZE*BLOCK_SIZE);
75 pos /= ivec2(BLOCK_SIZE);
76 #ifdef WRITE_LUMINANCE
77 float prev_lum = texelFetch(prev_luminance, ivec2(0, 0), 0).r; //1 pixel previous exposure
78 avg = clamp(prev_lum + (avg - prev_lum) * params.exposure_adjust, params.min_luminance, params.max_luminance);
79 #endif
80 imageStore(dest_luminance, pos, vec4(avg));
81 }
82 }