Debian Codegraph @

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

raw
1 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2016, Intel Corporation
3 // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
4 // documentation files (the "Software"), to deal in the Software without restriction, including without limitation
5 // the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to the following conditions:
7 // The above copyright notice and this permission notice shall be included in all copies or substantial portions of
8 // the Software.
9 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
10 // THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
11 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
12 // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
13 // SOFTWARE.
14 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
15 // File changes (yyyy-mm-dd)
16 // 2016-09-07: [email protected]: first commit
17 // 2020-12-05: clayjohn: convert to Vulkan and Godot
18 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
19
20 #[compute]
21
22 #version 450
23
24 #VERSION_DEFINES
25
26 layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in;
27
28 #ifdef GENERATE_MAP
29 layout(set = 0, binding = 0) uniform sampler2DArray source_texture;
30 #else
31 layout(set = 0, binding = 0) uniform sampler2D source_importance;
32 #endif
33 layout(r8, set = 1, binding = 0) uniform restrict writeonly image2D dest_image;
34
35 #ifdef PROCESS_MAPB
36 layout(set = 2, binding = 0, std430) buffer Counter {
37 uint sum;
38 }
39 counter;
40 #endif
41
42 layout(push_constant, std430) uniform Params {
43 vec2 half_screen_pixel_size;
44 float intensity;
45 float power;
46 }
47 params;
48
49 void main() {
50 // Pixel being shaded
51 ivec2 ssC = ivec2(gl_GlobalInvocationID.xy);
52
53 #ifdef GENERATE_MAP
54 // importance map stuff
55 uvec2 base_position = ssC * 2;
56
57 vec2 base_uv = (vec2(base_position) + vec2(0.5f, 0.5f)) * params.half_screen_pixel_size;
58
59 float minV = 1.0;
60 float maxV = 0.0;
61 for (int i = 0; i < 4; i++) {
62 vec4 vals = textureGather(source_texture, vec3(base_uv, i));
63
64 // apply the same modifications that would have been applied in the main shader
65 vals = params.intensity * vals;
66
67 vals = 1 - vals;
68
69 vals = pow(clamp(vals, 0.0, 1.0), vec4(params.power));
70
71 maxV = max(maxV, max(max(vals.x, vals.y), max(vals.z, vals.w)));
72 minV = min(minV, min(min(vals.x, vals.y), min(vals.z, vals.w)));
73 }
74
75 float min_max_diff = maxV - minV;
76
77 imageStore(dest_image, ssC, vec4(pow(clamp(min_max_diff * 2.0, 0.0, 1.0), 0.8)));
78 #endif
79
80 #ifdef PROCESS_MAPA
81 vec2 uv = (vec2(ssC) + 0.5f) * params.half_screen_pixel_size * 2.0;
82
83 float center = textureLod(source_importance, uv, 0.0).x;
84
85 vec2 half_pixel = params.half_screen_pixel_size;
86
87 vec4 vals;
88 vals.x = textureLod(source_importance, uv + vec2(-half_pixel.x * 3, -half_pixel.y), 0.0).x;
89 vals.y = textureLod(source_importance, uv + vec2(+half_pixel.x, -half_pixel.y * 3), 0.0).x;
90 vals.z = textureLod(source_importance, uv + vec2(+half_pixel.x * 3, +half_pixel.y), 0.0).x;
91 vals.w = textureLod(source_importance, uv + vec2(-half_pixel.x, +half_pixel.y * 3), 0.0).x;
92
93 float avg = dot(vals, vec4(0.25, 0.25, 0.25, 0.25));
94
95 imageStore(dest_image, ssC, vec4(avg));
96 #endif
97
98 #ifdef PROCESS_MAPB
99 vec2 uv = (vec2(ssC) + 0.5f) * params.half_screen_pixel_size * 2.0;
100
101 float center = textureLod(source_importance, uv, 0.0).x;
102
103 vec2 half_pixel = params.half_screen_pixel_size;
104
105 vec4 vals;
106 vals.x = textureLod(source_importance, uv + vec2(-half_pixel.x, -half_pixel.y * 3), 0.0).x;
107 vals.y = textureLod(source_importance, uv + vec2(+half_pixel.x * 3, -half_pixel.y), 0.0).x;
108 vals.z = textureLod(source_importance, uv + vec2(+half_pixel.x, +half_pixel.y * 3), 0.0).x;
109 vals.w = textureLod(source_importance, uv + vec2(-half_pixel.x * 3, +half_pixel.y), 0.0).x;
110
111 float avg = dot(vals, vec4(0.25, 0.25, 0.25, 0.25));
112
113 imageStore(dest_image, ssC, vec4(avg));
114
115 // sum the average; to avoid overflowing we assume max AO resolution is not bigger than 16384x16384; so quarter res (used here) will be 4096x4096, which leaves us with 8 bits per pixel
116 uint sum = uint(clamp(avg, 0.0, 1.0) * 255.0 + 0.5);
117
118 // save every 9th to avoid InterlockedAdd congestion - since we're blurring, this is good enough; compensated by multiplying load_counter_avg_div by 9
119 if (((ssC.x % 3) + (ssC.y % 3)) == 0) {
120 atomicAdd(counter.sum, sum);
121 }
122 #endif
123 }