Debian Codegraph @

servers/rendering/renderer_rd/shaders/effects/ssao.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 #define INTELSSAO_MAIN_DISK_SAMPLE_COUNT (32)
27 const vec4 sample_pattern[INTELSSAO_MAIN_DISK_SAMPLE_COUNT] = {
28 vec4(0.78488064, 0.56661671, 1.500000, -0.126083), vec4(0.26022232, -0.29575172, 1.500000, -1.064030), vec4(0.10459357, 0.08372527, 1.110000, -2.730563), vec4(-0.68286800, 0.04963045, 1.090000, -0.498827),
29 vec4(-0.13570161, -0.64190155, 1.250000, -0.532765), vec4(-0.26193795, -0.08205118, 0.670000, -1.783245), vec4(-0.61177456, 0.66664219, 0.710000, -0.044234), vec4(0.43675563, 0.25119025, 0.610000, -1.167283),
30 vec4(0.07884444, 0.86618668, 0.640000, -0.459002), vec4(-0.12790935, -0.29869005, 0.600000, -1.729424), vec4(-0.04031125, 0.02413622, 0.600000, -4.792042), vec4(0.16201244, -0.52851415, 0.790000, -1.067055),
31 vec4(-0.70991218, 0.47301072, 0.640000, -0.335236), vec4(0.03277707, -0.22349690, 0.600000, -1.982384), vec4(0.68921727, 0.36800742, 0.630000, -0.266718), vec4(0.29251814, 0.37775412, 0.610000, -1.422520),
32 vec4(-0.12224089, 0.96582592, 0.600000, -0.426142), vec4(0.11071457, -0.16131058, 0.600000, -2.165947), vec4(0.46562141, -0.59747696, 0.600000, -0.189760), vec4(-0.51548797, 0.11804193, 0.600000, -1.246800),
33 vec4(0.89141309, -0.42090443, 0.600000, 0.028192), vec4(-0.32402530, -0.01591529, 0.600000, -1.543018), vec4(0.60771245, 0.41635221, 0.600000, -0.605411), vec4(0.02379565, -0.08239821, 0.600000, -3.809046),
34 vec4(0.48951152, -0.23657045, 0.600000, -1.189011), vec4(-0.17611565, -0.81696892, 0.600000, -0.513724), vec4(-0.33930185, -0.20732205, 0.600000, -1.698047), vec4(-0.91974425, 0.05403209, 0.600000, 0.062246),
35 vec4(-0.15064627, -0.14949332, 0.600000, -1.896062), vec4(0.53180975, -0.35210401, 0.600000, -0.758838), vec4(0.41487166, 0.81442589, 0.600000, -0.505648), vec4(-0.24106961, -0.32721516, 0.600000, -1.665244)
36 };
37
38 // these values can be changed (up to SSAO_MAX_TAPS) with no changes required elsewhere; values for 4th and 5th preset are ignored but array needed to avoid compilation errors
39 // the actual number of texture samples is two times this value (each "tap" has two symmetrical depth texture samples)
40 const int num_taps[5] = { 3, 5, 12, 0, 0 };
41
42 #define SSAO_TILT_SAMPLES_ENABLE_AT_QUALITY_PRESET (99) // to disable simply set to 99 or similar
43 #define SSAO_TILT_SAMPLES_AMOUNT (0.4)
44 //
45 #define SSAO_HALOING_REDUCTION_ENABLE_AT_QUALITY_PRESET (1) // to disable simply set to 99 or similar
46 #define SSAO_HALOING_REDUCTION_AMOUNT (0.6) // values from 0.0 - 1.0, 1.0 means max weighting (will cause artifacts, 0.8 is more reasonable)
47 //
48 #define SSAO_NORMAL_BASED_EDGES_ENABLE_AT_QUALITY_PRESET (2) // to disable simply set to 99 or similar
49 #define SSAO_NORMAL_BASED_EDGES_DOT_THRESHOLD (0.5) // use 0-0.1 for super-sharp normal-based edges
50 //
51 #define SSAO_DETAIL_AO_ENABLE_AT_QUALITY_PRESET (1) // whether to use detail; to disable simply set to 99 or similar
52 //
53 // WARNING: The MIP generation on the C++ side will be enabled on quality preset 2 regardless of
54 // this value, so if changing here, change the C++ side too.
55 #define SSAO_DEPTH_MIPS_ENABLE_AT_QUALITY_PRESET (2)
56 #define SSAO_DEPTH_MIPS_GLOBAL_OFFSET (-4.3) // best noise/quality/performance tradeoff, found empirically
57 //
58 // WARNING: The edge handling is hard-coded to 'disabled' on quality level 0, and enabled above,
59 // on the C++ side; while toggling it here will work for testing purposes, it will not yield
60 // performance gains (or correct results).
61 #define SSAO_DEPTH_BASED_EDGES_ENABLE_AT_QUALITY_PRESET (1)
62 //
63 #define SSAO_REDUCE_RADIUS_NEAR_SCREEN_BORDER_ENABLE_AT_QUALITY_PRESET (1)
64
65 #define SSAO_MAX_TAPS 32
66 #define SSAO_ADAPTIVE_TAP_BASE_COUNT 5
67 #define SSAO_ADAPTIVE_TAP_FLEXIBLE_COUNT (SSAO_MAX_TAPS - SSAO_ADAPTIVE_TAP_BASE_COUNT)
68 #define SSAO_DEPTH_MIP_LEVELS 4
69
70 layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in;
71
72 layout(set = 0, binding = 0) uniform sampler2DArray source_depth_mipmaps;
73 layout(rgba8, set = 0, binding = 1) uniform restrict readonly image2D source_normal;
74 layout(set = 0, binding = 2) uniform Constants { //get into a lower set
75 vec4 rotation_matrices[20];
76 }
77 constants;
78
79 #ifdef ADAPTIVE
80 layout(rg8, set = 1, binding = 0) uniform restrict readonly image2DArray source_ssao;
81 layout(set = 1, binding = 1) uniform sampler2D source_importance;
82 layout(set = 1, binding = 2, std430) buffer Counter {
83 uint sum;
84 }
85 counter;
86 #endif
87
88 layout(rg8, set = 2, binding = 0) uniform restrict writeonly image2D dest_image;
89
90 // This push_constant is full - 128 bytes - if you need to add more data, consider adding to the uniform buffer instead
91 layout(push_constant, std430) uniform Params {
92 ivec2 screen_size;
93 int pass;
94 int quality;
95
96 vec2 half_screen_pixel_size;
97 int size_multiplier;
98 float detail_intensity;
99
100 vec2 NDC_to_view_mul;
101 vec2 NDC_to_view_add;
102
103 vec2 pad2;
104 vec2 half_screen_pixel_size_x025;
105
106 float radius;
107 float intensity;
108 float shadow_power;
109 float shadow_clamp;
110
111 float fade_out_mul;
112 float fade_out_add;
113 float horizon_angle_threshold;
114 float inv_radius_near_limit;
115
116 bool is_orthogonal;
117 float neg_inv_radius;
118 float load_counter_avg_div;
119 float adaptive_sample_limit;
120
121 ivec2 pass_coord_offset;
122 vec2 pass_uv_offset;
123 }
124 params;
125
126 // packing/unpacking for edges; 2 bits per edge mean 4 gradient values (0, 0.33, 0.66, 1) for smoother transitions!
127 float pack_edges(vec4 p_edgesLRTB) {
128 p_edgesLRTB = round(clamp(p_edgesLRTB, 0.0, 1.0) * 3.05);
129 return dot(p_edgesLRTB, vec4(64.0 / 255.0, 16.0 / 255.0, 4.0 / 255.0, 1.0 / 255.0));
130 }
131
132 vec3 NDC_to_view_space(vec2 p_pos, float p_viewspace_depth) {
133 if (params.is_orthogonal) {
134 return vec3((params.NDC_to_view_mul * p_pos.xy + params.NDC_to_view_add), p_viewspace_depth);
135 } else {
136 return vec3((params.NDC_to_view_mul * p_pos.xy + params.NDC_to_view_add) * p_viewspace_depth, p_viewspace_depth);
137 }
138 }
139
140 // calculate effect radius and fit our screen sampling pattern inside it
141 void calculate_radius_parameters(const float p_pix_center_length, const vec2 p_pixel_size_at_center, out float r_lookup_radius, out float r_radius, out float r_fallof_sq) {
142 r_radius = params.radius;
143
144 // when too close, on-screen sampling disk will grow beyond screen size; limit this to avoid closeup temporal artifacts
145 const float too_close_limit = clamp(p_pix_center_length * params.inv_radius_near_limit, 0.0, 1.0) * 0.8 + 0.2;
146
147 r_radius *= too_close_limit;
148
149 // 0.85 is to reduce the radius to allow for more samples on a slope to still stay within influence
150 r_lookup_radius = (0.85 * r_radius) / p_pixel_size_at_center.x;
151
152 // used to calculate falloff (both for AO samples and per-sample weights)
153 r_fallof_sq = -1.0 / (r_radius * r_radius);
154 }
155
156 vec4 calculate_edges(const float p_center_z, const float p_left_z, const float p_right_z, const float p_top_z, const float p_bottom_z) {
157 // slope-sensitive depth-based edge detection
158 vec4 edgesLRTB = vec4(p_left_z, p_right_z, p_top_z, p_bottom_z) - p_center_z;
159 vec4 edgesLRTB_slope_adjusted = edgesLRTB + edgesLRTB.yxwz;
160 edgesLRTB = min(abs(edgesLRTB), abs(edgesLRTB_slope_adjusted));
161 return clamp((1.3 - edgesLRTB / (p_center_z * 0.040)), 0.0, 1.0);
162 }
163
164 vec3 load_normal(ivec2 p_pos) {
165 vec3 encoded_normal = normalize(imageLoad(source_normal, p_pos).xyz * 2.0 - 1.0);
166 encoded_normal.z = -encoded_normal.z;
167 return encoded_normal;
168 }
169
170 vec3 load_normal(ivec2 p_pos, ivec2 p_offset) {
171 vec3 encoded_normal = normalize(imageLoad(source_normal, p_pos + p_offset).xyz * 2.0 - 1.0);
172 encoded_normal.z = -encoded_normal.z;
173 return encoded_normal;
174 }
175
176 // all vectors in viewspace
177 float calculate_pixel_obscurance(vec3 p_pixel_normal, vec3 p_hit_delta, float p_fallof_sq) {
178 float length_sq = dot(p_hit_delta, p_hit_delta);
179 float NdotD = dot(p_pixel_normal, p_hit_delta) / sqrt(length_sq);
180
181 float falloff_mult = max(0.0, length_sq * p_fallof_sq + 1.0);
182
183 return max(0, NdotD - params.horizon_angle_threshold) * falloff_mult;
184 }
185
186 void SSAO_tap_inner(const int p_quality_level, inout float r_obscurance_sum, inout float r_weight_sum, const vec2 p_sampling_uv, const float p_mip_level, const vec3 p_pix_center_pos, vec3 p_pixel_normal, const float p_fallof_sq, const float p_weight_mod) {
187 // get depth at sample
188 float viewspace_sample_z = textureLod(source_depth_mipmaps, vec3(p_sampling_uv, params.pass), p_mip_level).x;
189
190 // convert to viewspace
191 vec3 hit_pos = NDC_to_view_space(p_sampling_uv.xy, viewspace_sample_z).xyz;
192 vec3 hit_delta = hit_pos - p_pix_center_pos;
193
194 float obscurance = calculate_pixel_obscurance(p_pixel_normal, hit_delta, p_fallof_sq);
195 float weight = 1.0;
196
197 if (p_quality_level >= SSAO_HALOING_REDUCTION_ENABLE_AT_QUALITY_PRESET) {
198 float reduce = max(0, -hit_delta.z);
199 reduce = clamp(reduce * params.neg_inv_radius + 2.0, 0.0, 1.0);
200 weight = SSAO_HALOING_REDUCTION_AMOUNT * reduce + (1.0 - SSAO_HALOING_REDUCTION_AMOUNT);
201 }
202 weight *= p_weight_mod;
203 r_obscurance_sum += obscurance * weight;
204 r_weight_sum += weight;
205 }
206
207 void SSAOTap(const int p_quality_level, inout float r_obscurance_sum, inout float r_weight_sum, const int p_tap_index, const mat2 p_rot_scale, const vec3 p_pix_center_pos, vec3 p_pixel_normal, const vec2 p_normalized_screen_pos, const float p_mip_offset, const float p_fallof_sq, float p_weight_mod, vec2 p_norm_xy, float p_norm_xy_length) {
208 vec2 sample_offset;
209 float sample_pow_2_len;
210
211 // patterns
212 {
213 vec4 new_sample = sample_pattern[p_tap_index];
214 sample_offset = new_sample.xy * p_rot_scale;
215 sample_pow_2_len = new_sample.w; // precalculated, same as: sample_pow_2_len = log2( length( new_sample.xy ) );
216 p_weight_mod *= new_sample.z;
217 }
218
219 // snap to pixel center (more correct obscurance math, avoids artifacts)
220 sample_offset = round(sample_offset);
221
222 // calculate MIP based on the sample distance from the center, similar to as described
223 // in http://graphics.cs.williams.edu/papers/SAOHPG12/.
224 float mip_level = (p_quality_level < SSAO_DEPTH_MIPS_ENABLE_AT_QUALITY_PRESET) ? (0) : (sample_pow_2_len + p_mip_offset);
225
226 vec2 sampling_uv = sample_offset * params.half_screen_pixel_size + p_normalized_screen_pos;
227
228 SSAO_tap_inner(p_quality_level, r_obscurance_sum, r_weight_sum, sampling_uv, mip_level, p_pix_center_pos, p_pixel_normal, p_fallof_sq, p_weight_mod);
229
230 // for the second tap, just use the mirrored offset
231 vec2 sample_offset_mirrored_uv = -sample_offset;
232
233 // tilt the second set of samples so that the disk is effectively rotated by the normal
234 // effective at removing one set of artifacts, but too expensive for lower quality settings
235 if (p_quality_level >= SSAO_TILT_SAMPLES_ENABLE_AT_QUALITY_PRESET) {
236 float dot_norm = dot(sample_offset_mirrored_uv, p_norm_xy);
237 sample_offset_mirrored_uv -= dot_norm * p_norm_xy_length * p_norm_xy;
238 sample_offset_mirrored_uv = round(sample_offset_mirrored_uv);
239 }
240
241 // snap to pixel center (more correct obscurance math, avoids artifacts)
242 vec2 sampling_mirrored_uv = sample_offset_mirrored_uv * params.half_screen_pixel_size + p_normalized_screen_pos;
243
244 SSAO_tap_inner(p_quality_level, r_obscurance_sum, r_weight_sum, sampling_mirrored_uv, mip_level, p_pix_center_pos, p_pixel_normal, p_fallof_sq, p_weight_mod);
245 }
246
247 void generate_SSAO_shadows_internal(out float r_shadow_term, out vec4 r_edges, out float r_weight, const vec2 p_pos, int p_quality_level, bool p_adaptive_base) {
248 vec2 pos_rounded = trunc(p_pos);
249 uvec2 upos = uvec2(pos_rounded);
250
251 const int number_of_taps = (p_adaptive_base) ? (SSAO_ADAPTIVE_TAP_BASE_COUNT) : (num_taps[p_quality_level]);
252 float pix_z, pix_left_z, pix_top_z, pix_right_z, pix_bottom_z;
253
254 vec4 valuesUL = textureGather(source_depth_mipmaps, vec3(pos_rounded * params.half_screen_pixel_size, params.pass));
255 vec4 valuesBR = textureGather(source_depth_mipmaps, vec3((pos_rounded + vec2(1.0)) * params.half_screen_pixel_size, params.pass));
256
257 // get this pixel's viewspace depth
258 pix_z = valuesUL.y;
259
260 // get left right top bottom neighboring pixels for edge detection (gets compiled out on quality_level == 0)
261 pix_left_z = valuesUL.x;
262 pix_top_z = valuesUL.z;
263 pix_right_z = valuesBR.z;
264 pix_bottom_z = valuesBR.x;
265
266 vec2 normalized_screen_pos = pos_rounded * params.half_screen_pixel_size + params.half_screen_pixel_size_x025;
267 vec3 pix_center_pos = NDC_to_view_space(normalized_screen_pos, pix_z);
268
269 // Load this pixel's viewspace normal
270 uvec2 full_res_coord = upos * 2 * params.size_multiplier + params.pass_coord_offset.xy;
271 vec3 pixel_normal = load_normal(ivec2(full_res_coord));
272
273 const vec2 pixel_size_at_center = NDC_to_view_space(normalized_screen_pos.xy + params.half_screen_pixel_size, pix_center_pos.z).xy - pix_center_pos.xy;
274
275 float pixel_lookup_radius;
276 float fallof_sq;
277
278 // calculate effect radius and fit our screen sampling pattern inside it
279 float viewspace_radius;
280 calculate_radius_parameters(length(pix_center_pos), pixel_size_at_center, pixel_lookup_radius, viewspace_radius, fallof_sq);
281
282 // calculate samples rotation/scaling
283 mat2 rot_scale_matrix;
284 uint pseudo_random_index;
285
286 {
287 vec4 rotation_scale;
288 // reduce effect radius near the screen edges slightly; ideally, one would render a larger depth buffer (5% on each side) instead
289 if (!p_adaptive_base && (p_quality_level >= SSAO_REDUCE_RADIUS_NEAR_SCREEN_BORDER_ENABLE_AT_QUALITY_PRESET)) {
290 float near_screen_border = min(min(normalized_screen_pos.x, 1.0 - normalized_screen_pos.x), min(normalized_screen_pos.y, 1.0 - normalized_screen_pos.y));
291 near_screen_border = clamp(10.0 * near_screen_border + 0.6, 0.0, 1.0);
292 pixel_lookup_radius *= near_screen_border;
293 }
294
295 // load & update pseudo-random rotation matrix
296 pseudo_random_index = uint(pos_rounded.y * 2 + pos_rounded.x) % 5;
297 rotation_scale = constants.rotation_matrices[params.pass * 5 + pseudo_random_index];
298 rot_scale_matrix = mat2(rotation_scale.x * pixel_lookup_radius, rotation_scale.y * pixel_lookup_radius, rotation_scale.z * pixel_lookup_radius, rotation_scale.w * pixel_lookup_radius);
299 }
300
301 // the main obscurance & sample weight storage
302 float obscurance_sum = 0.0;
303 float weight_sum = 0.0;
304
305 // edge mask for between this and left/right/top/bottom neighbor pixels - not used in quality level 0 so initialize to "no edge" (1 is no edge, 0 is edge)
306 vec4 edgesLRTB = vec4(1.0, 1.0, 1.0, 1.0);
307
308 // Move center pixel slightly towards camera to avoid imprecision artifacts due to using of 16bit depth buffer.
309 pix_center_pos *= 0.99;
310
311 if (!p_adaptive_base && (p_quality_level >= SSAO_DEPTH_BASED_EDGES_ENABLE_AT_QUALITY_PRESET)) {
312 edgesLRTB = calculate_edges(pix_z, pix_left_z, pix_right_z, pix_top_z, pix_bottom_z);
313 }
314
315 // adds a more high definition sharp effect, which gets blurred out (reuses left/right/top/bottom samples that we used for edge detection)
316 if (!p_adaptive_base && (p_quality_level >= SSAO_DETAIL_AO_ENABLE_AT_QUALITY_PRESET)) {
317 // disable in case of quality level 4 (reference)
318 if (p_quality_level != 4) {
319 //approximate neighboring pixels positions (actually just deltas or "positions - pix_center_pos" )
320 vec3 normalized_viewspace_dir = vec3(pix_center_pos.xy / pix_center_pos.zz, 1.0);
321 vec3 pixel_left_delta = vec3(-pixel_size_at_center.x, 0.0, 0.0) + normalized_viewspace_dir * (pix_left_z - pix_center_pos.z);
322 vec3 pixel_right_delta = vec3(+pixel_size_at_center.x, 0.0, 0.0) + normalized_viewspace_dir * (pix_right_z - pix_center_pos.z);
323 vec3 pixel_top_delta = vec3(0.0, -pixel_size_at_center.y, 0.0) + normalized_viewspace_dir * (pix_top_z - pix_center_pos.z);
324 vec3 pixel_bottom_delta = vec3(0.0, +pixel_size_at_center.y, 0.0) + normalized_viewspace_dir * (pix_bottom_z - pix_center_pos.z);
325
326 const float range_reduction = 4.0f; // this is to avoid various artifacts
327 const float modified_fallof_sq = range_reduction * fallof_sq;
328
329 vec4 additional_obscurance;
330 additional_obscurance.x = calculate_pixel_obscurance(pixel_normal, pixel_left_delta, modified_fallof_sq);
331 additional_obscurance.y = calculate_pixel_obscurance(pixel_normal, pixel_right_delta, modified_fallof_sq);
332 additional_obscurance.z = calculate_pixel_obscurance(pixel_normal, pixel_top_delta, modified_fallof_sq);
333 additional_obscurance.w = calculate_pixel_obscurance(pixel_normal, pixel_bottom_delta, modified_fallof_sq);
334
335 obscurance_sum += params.detail_intensity * dot(additional_obscurance, edgesLRTB);
336 }
337 }
338
339 // Sharp normals also create edges - but this adds to the cost as well
340 if (!p_adaptive_base && (p_quality_level >= SSAO_NORMAL_BASED_EDGES_ENABLE_AT_QUALITY_PRESET)) {
341 vec3 neighbour_normal_left = load_normal(ivec2(full_res_coord), ivec2(-2, 0));
342 vec3 neighbour_normal_right = load_normal(ivec2(full_res_coord), ivec2(2, 0));
343 vec3 neighbour_normal_top = load_normal(ivec2(full_res_coord), ivec2(0, -2));
344 vec3 neighbour_normal_bottom = load_normal(ivec2(full_res_coord), ivec2(0, 2));
345
346 const float dot_threshold = SSAO_NORMAL_BASED_EDGES_DOT_THRESHOLD;
347
348 vec4 normal_edgesLRTB;
349 normal_edgesLRTB.x = clamp((dot(pixel_normal, neighbour_normal_left) + dot_threshold), 0.0, 1.0);
350 normal_edgesLRTB.y = clamp((dot(pixel_normal, neighbour_normal_right) + dot_threshold), 0.0, 1.0);
351 normal_edgesLRTB.z = clamp((dot(pixel_normal, neighbour_normal_top) + dot_threshold), 0.0, 1.0);
352 normal_edgesLRTB.w = clamp((dot(pixel_normal, neighbour_normal_bottom) + dot_threshold), 0.0, 1.0);
353
354 edgesLRTB *= normal_edgesLRTB;
355 }
356
357 const float global_mip_offset = SSAO_DEPTH_MIPS_GLOBAL_OFFSET;
358 float mip_offset = (p_quality_level < SSAO_DEPTH_MIPS_ENABLE_AT_QUALITY_PRESET) ? (0) : (log2(pixel_lookup_radius) + global_mip_offset);
359
360 // Used to tilt the second set of samples so that the disk is effectively rotated by the normal
361 // effective at removing one set of artifacts, but too expensive for lower quality settings
362 vec2 norm_xy = vec2(pixel_normal.x, pixel_normal.y);
363 float norm_xy_length = length(norm_xy);
364 norm_xy /= vec2(norm_xy_length, -norm_xy_length);
365 norm_xy_length *= SSAO_TILT_SAMPLES_AMOUNT;
366
367 // standard, non-adaptive approach
368 if ((p_quality_level != 3) || p_adaptive_base) {
369 for (int i = 0; i < number_of_taps; i++) {
370 SSAOTap(p_quality_level, obscurance_sum, weight_sum, i, rot_scale_matrix, pix_center_pos, pixel_normal, normalized_screen_pos, mip_offset, fallof_sq, 1.0, norm_xy, norm_xy_length);
371 }
372 }
373 #ifdef ADAPTIVE
374 else {
375 // add new ones if needed
376 vec2 full_res_uv = normalized_screen_pos + params.pass_uv_offset.xy;
377 float importance = textureLod(source_importance, full_res_uv, 0.0).x;
378
379 // this is to normalize SSAO_DETAIL_AO_AMOUNT across all pixel regardless of importance
380 obscurance_sum *= (SSAO_ADAPTIVE_TAP_BASE_COUNT / float(SSAO_MAX_TAPS)) + (importance * SSAO_ADAPTIVE_TAP_FLEXIBLE_COUNT / float(SSAO_MAX_TAPS));
381
382 // load existing base values
383 vec2 base_values = imageLoad(source_ssao, ivec3(upos, params.pass)).xy;
384 weight_sum += base_values.y * float(SSAO_ADAPTIVE_TAP_BASE_COUNT * 4.0);
385 obscurance_sum += (base_values.x) * weight_sum;
386
387 // increase importance around edges
388 float edge_count = dot(1.0 - edgesLRTB, vec4(1.0, 1.0, 1.0, 1.0));
389
390 float avg_total_importance = float(counter.sum) * params.load_counter_avg_div;
391
392 float importance_limiter = clamp(params.adaptive_sample_limit / avg_total_importance, 0.0, 1.0);
393 importance *= importance_limiter;
394
395 float additional_sample_count = SSAO_ADAPTIVE_TAP_FLEXIBLE_COUNT * importance;
396
397 const float blend_range = 3.0;
398 const float blend_range_inv = 1.0 / blend_range;
399
400 additional_sample_count += 0.5;
401 uint additional_samples = uint(additional_sample_count);
402 uint additional_samples_to = min(SSAO_MAX_TAPS, additional_samples + SSAO_ADAPTIVE_TAP_BASE_COUNT);
403
404 for (uint i = SSAO_ADAPTIVE_TAP_BASE_COUNT; i < additional_samples_to; i++) {
405 additional_sample_count -= 1.0f;
406 float weight_mod = clamp(additional_sample_count * blend_range_inv, 0.0, 1.0);
407 SSAOTap(p_quality_level, obscurance_sum, weight_sum, int(i), rot_scale_matrix, pix_center_pos, pixel_normal, normalized_screen_pos, mip_offset, fallof_sq, weight_mod, norm_xy, norm_xy_length);
408 }
409 }
410 #endif
411
412 // early out for adaptive base - just output weight (used for the next pass)
413 if (p_adaptive_base) {
414 float obscurance = obscurance_sum / weight_sum;
415
416 r_shadow_term = obscurance;
417 r_edges = vec4(0.0);
418 r_weight = weight_sum;
419 return;
420 }
421
422 // calculate weighted average
423 float obscurance = obscurance_sum / weight_sum;
424
425 // calculate fadeout (1 close, gradient, 0 far)
426 float fade_out = clamp(pix_center_pos.z * params.fade_out_mul + params.fade_out_add, 0.0, 1.0);
427
428 // Reduce the SSAO shadowing if we're on the edge to remove artifacts on edges (we don't care for the lower quality one)
429 if (!p_adaptive_base && (p_quality_level >= SSAO_DEPTH_BASED_EDGES_ENABLE_AT_QUALITY_PRESET)) {
430 // when there's more than 2 opposite edges, start fading out the occlusion to reduce aliasing artifacts
431 float edge_fadeout_factor = clamp((1.0 - edgesLRTB.x - edgesLRTB.y) * 0.35, 0.0, 1.0) + clamp((1.0 - edgesLRTB.z - edgesLRTB.w) * 0.35, 0.0, 1.0);
432
433 fade_out *= clamp(1.0 - edge_fadeout_factor, 0.0, 1.0);
434 }
435
436 // strength
437 obscurance = params.intensity * obscurance;
438
439 // clamp
440 obscurance = min(obscurance, params.shadow_clamp);
441
442 // fadeout
443 obscurance *= fade_out;
444
445 // conceptually switch to occlusion with the meaning being visibility (grows with visibility, occlusion == 1 implies full visibility),
446 // to be in line with what is more commonly used.
447 float occlusion = 1.0 - obscurance;
448
449 // modify the gradient
450 // note: this cannot be moved to a later pass because of loss of precision after storing in the render target
451 occlusion = pow(clamp(occlusion, 0.0, 1.0), params.shadow_power);
452
453 // outputs!
454 r_shadow_term = occlusion; // Our final 'occlusion' term (0 means fully occluded, 1 means fully lit)
455 r_edges = edgesLRTB; // These are used to prevent blurring across edges, 1 means no edge, 0 means edge, 0.5 means half way there, etc.
456 r_weight = weight_sum;
457 }
458
459 void main() {
460 float out_shadow_term;
461 float out_weight;
462 vec4 out_edges;
463 ivec2 ssC = ivec2(gl_GlobalInvocationID.xy);
464 if (any(greaterThanEqual(ssC, params.screen_size))) { //too large, do nothing
465 return;
466 }
467
468 vec2 uv = vec2(gl_GlobalInvocationID) + vec2(0.5);
469 #ifdef SSAO_BASE
470 generate_SSAO_shadows_internal(out_shadow_term, out_edges, out_weight, uv, params.quality, true);
471
472 imageStore(dest_image, ivec2(gl_GlobalInvocationID.xy), vec4(out_shadow_term, out_weight / (float(SSAO_ADAPTIVE_TAP_BASE_COUNT) * 4.0), 0.0, 0.0));
473 #else
474 generate_SSAO_shadows_internal(out_shadow_term, out_edges, out_weight, uv, params.quality, false); // pass in quality levels
475 if (params.quality == 0) {
476 out_edges = vec4(1.0);
477 }
478
479 imageStore(dest_image, ivec2(gl_GlobalInvocationID.xy), vec4(out_shadow_term, pack_edges(out_edges), 0.0, 0.0));
480 #endif
481 }