Debian Codegraph @

servers/rendering/renderer_rd/shaders/effects/ss_effects_downsample.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 layout(push_constant, std430) uniform Params {
29 vec2 pixel_size;
30 float z_far;
31 float z_near;
32 bool orthogonal;
33 float radius_sq;
34 uvec2 pad;
35 }
36 params;
37
38 layout(set = 0, binding = 0) uniform sampler2D source_depth;
39
40 layout(r16f, set = 1, binding = 0) uniform restrict writeonly image2DArray dest_image0; //rename
41 #ifdef GENERATE_MIPS
42 layout(r16f, set = 2, binding = 0) uniform restrict writeonly image2DArray dest_image1;
43 layout(r16f, set = 2, binding = 1) uniform restrict writeonly image2DArray dest_image2;
44 layout(r16f, set = 2, binding = 2) uniform restrict writeonly image2DArray dest_image3;
45 #ifdef GENERATE_FULL_MIPS
46 layout(r16f, set = 2, binding = 3) uniform restrict writeonly image2DArray dest_image4;
47 #endif
48 #endif
49
50 vec4 screen_space_to_view_space_depth(vec4 p_depth) {
51 if (params.orthogonal) {
52 vec4 depth = p_depth * 2.0 - 1.0;
53 return -(depth * (params.z_far - params.z_near) - (params.z_far + params.z_near)) / 2.0;
54 }
55
56 float depth_linearize_mul = params.z_near;
57 float depth_linearize_add = params.z_far;
58
59 // Optimized version of "-cameraClipNear / (cameraClipFar - projDepth * (cameraClipFar - cameraClipNear)) * cameraClipFar"
60
61 // Set your depth_linearize_mul and depth_linearize_add to:
62 // depth_linearize_mul = ( cameraClipFar * cameraClipNear) / ( cameraClipFar - cameraClipNear );
63 // depth_linearize_add = cameraClipFar / ( cameraClipFar - cameraClipNear );
64
65 return depth_linearize_mul / (depth_linearize_add - p_depth);
66 }
67
68 float screen_space_to_view_space_depth(float p_depth) {
69 if (params.orthogonal) {
70 float depth = p_depth * 2.0 - 1.0;
71 return -(depth * (params.z_far - params.z_near) - (params.z_far + params.z_near)) / 2.0;
72 }
73
74 float depth_linearize_mul = params.z_near;
75 float depth_linearize_add = params.z_far;
76
77 return depth_linearize_mul / (depth_linearize_add - p_depth);
78 }
79
80 #ifdef GENERATE_MIPS
81
82 shared float depth_buffer[4][8][8];
83
84 float mip_smart_average(vec4 p_depths) {
85 float closest = min(min(p_depths.x, p_depths.y), min(p_depths.z, p_depths.w));
86 float fallof_sq = -1.0f / params.radius_sq;
87 vec4 dists = p_depths - closest.xxxx;
88 vec4 weights = clamp(dists * dists * fallof_sq + 1.0, 0.0, 1.0);
89 return dot(weights, p_depths) / dot(weights, vec4(1.0, 1.0, 1.0, 1.0));
90 }
91
92 void prepare_depths_and_mips(vec4 p_samples, uvec2 p_output_coord, uvec2 p_gtid) {
93 p_samples = screen_space_to_view_space_depth(p_samples);
94
95 depth_buffer[0][p_gtid.x][p_gtid.y] = p_samples.w;
96 depth_buffer[1][p_gtid.x][p_gtid.y] = p_samples.z;
97 depth_buffer[2][p_gtid.x][p_gtid.y] = p_samples.x;
98 depth_buffer[3][p_gtid.x][p_gtid.y] = p_samples.y;
99
100 imageStore(dest_image0, ivec3(p_output_coord.x, p_output_coord.y, 0), vec4(p_samples.w));
101 imageStore(dest_image0, ivec3(p_output_coord.x, p_output_coord.y, 1), vec4(p_samples.z));
102 imageStore(dest_image0, ivec3(p_output_coord.x, p_output_coord.y, 2), vec4(p_samples.x));
103 imageStore(dest_image0, ivec3(p_output_coord.x, p_output_coord.y, 3), vec4(p_samples.y));
104
105 uint depth_array_index = 2 * (p_gtid.y % 2) + (p_gtid.x % 2);
106 uvec2 depth_array_offset = ivec2(p_gtid.x % 2, p_gtid.y % 2);
107 ivec2 buffer_coord = ivec2(p_gtid) - ivec2(depth_array_offset);
108
109 p_output_coord /= 2;
110 groupMemoryBarrier();
111 barrier();
112
113 // if (still_alive) <-- all threads alive here
114 {
115 float sample_00 = depth_buffer[depth_array_index][buffer_coord.x + 0][buffer_coord.y + 0];
116 float sample_01 = depth_buffer[depth_array_index][buffer_coord.x + 0][buffer_coord.y + 1];
117 float sample_10 = depth_buffer[depth_array_index][buffer_coord.x + 1][buffer_coord.y + 0];
118 float sample_11 = depth_buffer[depth_array_index][buffer_coord.x + 1][buffer_coord.y + 1];
119
120 float avg = mip_smart_average(vec4(sample_00, sample_01, sample_10, sample_11));
121 imageStore(dest_image1, ivec3(p_output_coord.x, p_output_coord.y, depth_array_index), vec4(avg));
122 depth_buffer[depth_array_index][buffer_coord.x][buffer_coord.y] = avg;
123 }
124
125 bool still_alive = p_gtid.x % 4 == depth_array_offset.x && p_gtid.y % 4 == depth_array_offset.y;
126
127 p_output_coord /= 2;
128 groupMemoryBarrier();
129 barrier();
130
131 if (still_alive) {
132 float sample_00 = depth_buffer[depth_array_index][buffer_coord.x + 0][buffer_coord.y + 0];
133 float sample_01 = depth_buffer[depth_array_index][buffer_coord.x + 0][buffer_coord.y + 2];
134 float sample_10 = depth_buffer[depth_array_index][buffer_coord.x + 2][buffer_coord.y + 0];
135 float sample_11 = depth_buffer[depth_array_index][buffer_coord.x + 2][buffer_coord.y + 2];
136
137 float avg = mip_smart_average(vec4(sample_00, sample_01, sample_10, sample_11));
138 imageStore(dest_image2, ivec3(p_output_coord.x, p_output_coord.y, depth_array_index), vec4(avg));
139 depth_buffer[depth_array_index][buffer_coord.x][buffer_coord.y] = avg;
140 }
141
142 still_alive = p_gtid.x % 8 == depth_array_offset.x && depth_array_offset.y % 8 == depth_array_offset.y;
143
144 p_output_coord /= 2;
145 groupMemoryBarrier();
146 barrier();
147
148 if (still_alive) {
149 float sample_00 = depth_buffer[depth_array_index][buffer_coord.x + 0][buffer_coord.y + 0];
150 float sample_01 = depth_buffer[depth_array_index][buffer_coord.x + 0][buffer_coord.y + 4];
151 float sample_10 = depth_buffer[depth_array_index][buffer_coord.x + 4][buffer_coord.y + 0];
152 float sample_11 = depth_buffer[depth_array_index][buffer_coord.x + 4][buffer_coord.y + 4];
153
154 float avg = mip_smart_average(vec4(sample_00, sample_01, sample_10, sample_11));
155 imageStore(dest_image3, ivec3(p_output_coord.x, p_output_coord.y, depth_array_index), vec4(avg));
156 #ifndef GENERATE_FULL_MIPS
157 }
158 #else
159 depth_buffer[depth_array_index][buffer_coord.x][buffer_coord.y] = avg;
160 }
161 still_alive = p_gtid.x % 16 == depth_array_offset.x && depth_array_offset.y % 16 == depth_array_offset.y;
162
163 p_output_coord /= 2;
164
165 if (still_alive) {
166 // Use the previous average, not ideal, but still not bad.
167 float sample_00 = depth_buffer[depth_array_index][buffer_coord.x + 0][buffer_coord.y + 0];
168 imageStore(dest_image4, ivec3(p_output_coord.x, p_output_coord.y, depth_array_index), vec4(sample_00));
169 }
170 #endif
171 }
172 #else
173 #ifndef USE_HALF_BUFFERS
174 void prepare_depths(vec4 p_samples, uvec2 p_tid) {
175 p_samples = screen_space_to_view_space_depth(p_samples);
176
177 imageStore(dest_image0, ivec3(p_tid, 0), vec4(p_samples.w));
178 imageStore(dest_image0, ivec3(p_tid, 1), vec4(p_samples.z));
179 imageStore(dest_image0, ivec3(p_tid, 2), vec4(p_samples.x));
180 imageStore(dest_image0, ivec3(p_tid, 3), vec4(p_samples.y));
181 }
182 #endif
183 #endif
184
185 void main() {
186 #ifdef USE_HALF_BUFFERS
187 // Half buffers means that we divide depth into two half res buffers (we only capture 1/4 of pixels).
188 #ifdef USE_HALF_SIZE
189 float sample_00 = texelFetch(source_depth, ivec2(4 * gl_GlobalInvocationID.x + 0, 4 * gl_GlobalInvocationID.y + 0), 0).x;
190 float sample_11 = texelFetch(source_depth, ivec2(4 * gl_GlobalInvocationID.x + 2, 4 * gl_GlobalInvocationID.y + 2), 0).x;
191 #else
192 float sample_00 = texelFetch(source_depth, ivec2(2 * gl_GlobalInvocationID.x + 0, 2 * gl_GlobalInvocationID.y + 0), 0).x;
193 float sample_11 = texelFetch(source_depth, ivec2(2 * gl_GlobalInvocationID.x + 1, 2 * gl_GlobalInvocationID.y + 1), 0).x;
194 #endif
195 sample_00 = screen_space_to_view_space_depth(sample_00);
196 sample_11 = screen_space_to_view_space_depth(sample_11);
197
198 imageStore(dest_image0, ivec3(gl_GlobalInvocationID.xy, 0), vec4(sample_00));
199 imageStore(dest_image0, ivec3(gl_GlobalInvocationID.xy, 3), vec4(sample_11));
200 #else //!USE_HALF_BUFFERS
201 #ifdef USE_HALF_SIZE
202 ivec2 depth_buffer_coord = 4 * ivec2(gl_GlobalInvocationID.xy);
203 ivec2 output_coord = ivec2(gl_GlobalInvocationID);
204
205 vec2 uv = (vec2(depth_buffer_coord) + 0.5f) * params.pixel_size;
206 vec4 samples;
207 samples.x = textureLodOffset(source_depth, uv, 0, ivec2(0, 2)).x;
208 samples.y = textureLodOffset(source_depth, uv, 0, ivec2(2, 2)).x;
209 samples.z = textureLodOffset(source_depth, uv, 0, ivec2(2, 0)).x;
210 samples.w = textureLodOffset(source_depth, uv, 0, ivec2(0, 0)).x;
211 #else
212 ivec2 depth_buffer_coord = 2 * ivec2(gl_GlobalInvocationID.xy);
213 ivec2 output_coord = ivec2(gl_GlobalInvocationID);
214
215 vec2 uv = (vec2(depth_buffer_coord) + 0.5f) * params.pixel_size;
216 vec4 samples = textureGather(source_depth, uv);
217 #endif //USE_HALF_SIZE
218 #ifdef GENERATE_MIPS
219 prepare_depths_and_mips(samples, output_coord, gl_LocalInvocationID.xy);
220 #else
221 prepare_depths(samples, gl_GlobalInvocationID.xy);
222 #endif
223 #endif //USE_HALF_BUFFERS
224 }