ssil.glsl 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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. // 2021-05-27: clayjohn: convert SSAO to SSIL
  19. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  20. #[compute]
  21. #version 450
  22. #VERSION_DEFINES
  23. #define SSIL_MAIN_DISK_SAMPLE_COUNT (32)
  24. const vec4 sample_pattern[SSIL_MAIN_DISK_SAMPLE_COUNT] = {
  25. 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),
  26. 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),
  27. 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),
  28. 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),
  29. 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),
  30. 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),
  31. 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),
  32. 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)
  33. };
  34. // these values can be changed (up to SSIL_MAX_TAPS) with no changes required elsewhere; values for 4th and 5th preset are ignored but array needed to avoid compilation errors
  35. // the actual number of texture samples is two times this value (each "tap" has two symmetrical depth texture samples)
  36. const int num_taps[5] = { 3, 5, 12, 0, 0 };
  37. #define SSIL_TILT_SAMPLES_ENABLE_AT_QUALITY_PRESET (99) // to disable simply set to 99 or similar
  38. #define SSIL_TILT_SAMPLES_AMOUNT (0.4)
  39. //
  40. #define SSIL_HALOING_REDUCTION_ENABLE_AT_QUALITY_PRESET (1) // to disable simply set to 99 or similar
  41. #define SSIL_HALOING_REDUCTION_AMOUNT (0.8) // values from 0.0 - 1.0, 1.0 means max weighting (will cause artifacts, 0.8 is more reasonable)
  42. //
  43. #define SSIL_DEPTH_MIPS_ENABLE_AT_QUALITY_PRESET (2)
  44. #define SSIL_DEPTH_MIPS_GLOBAL_OFFSET (-4.3) // best noise/quality/performance tradeoff, found empirically
  45. //
  46. // !!warning!! the edge handling is hard-coded to 'disabled' on quality level 0, and enabled above, on the C++ side; while toggling it here will work for
  47. // testing purposes, it will not yield performance gains (or correct results)
  48. #define SSIL_DEPTH_BASED_EDGES_ENABLE_AT_QUALITY_PRESET (1)
  49. //
  50. #define SSIL_REDUCE_RADIUS_NEAR_SCREEN_BORDER_ENABLE_AT_QUALITY_PRESET (1)
  51. #define SSIL_MAX_TAPS 32
  52. #define SSIL_ADAPTIVE_TAP_BASE_COUNT 5
  53. #define SSIL_ADAPTIVE_TAP_FLEXIBLE_COUNT (SSIL_MAX_TAPS - SSIL_ADAPTIVE_TAP_BASE_COUNT)
  54. #define SSIL_DEPTH_MIP_LEVELS 4
  55. layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in;
  56. layout(set = 0, binding = 0) uniform sampler2DArray source_depth_mipmaps;
  57. layout(rgba8, set = 0, binding = 1) uniform restrict readonly image2D source_normal;
  58. layout(set = 0, binding = 2) uniform Constants { //get into a lower set
  59. vec4 rotation_matrices[20];
  60. }
  61. constants;
  62. #ifdef ADAPTIVE
  63. layout(rgba16, set = 1, binding = 0) uniform restrict readonly image2DArray source_ssil;
  64. layout(set = 1, binding = 1) uniform sampler2D source_importance;
  65. layout(set = 1, binding = 2, std430) buffer Counter {
  66. uint sum;
  67. }
  68. counter;
  69. #endif
  70. layout(rgba16, set = 2, binding = 0) uniform restrict writeonly image2D dest_image;
  71. layout(r8, set = 2, binding = 1) uniform image2D edges_weights_image;
  72. layout(set = 3, binding = 0) uniform sampler2D last_frame;
  73. layout(set = 3, binding = 1) uniform ProjectionConstants {
  74. mat4 reprojection;
  75. }
  76. projection_constants;
  77. layout(push_constant, std430) uniform Params {
  78. ivec2 screen_size;
  79. int pass;
  80. int quality;
  81. vec2 half_screen_pixel_size;
  82. vec2 half_screen_pixel_size_x025;
  83. vec2 NDC_to_view_mul;
  84. vec2 NDC_to_view_add;
  85. vec2 pad2;
  86. float z_near;
  87. float z_far;
  88. float radius;
  89. float intensity;
  90. int size_multiplier;
  91. int pad;
  92. float fade_out_mul;
  93. float fade_out_add;
  94. float normal_rejection_amount;
  95. float inv_radius_near_limit;
  96. bool is_orthogonal;
  97. float neg_inv_radius;
  98. float load_counter_avg_div;
  99. float adaptive_sample_limit;
  100. ivec2 pass_coord_offset;
  101. vec2 pass_uv_offset;
  102. }
  103. params;
  104. float pack_edges(vec4 p_edgesLRTB) {
  105. p_edgesLRTB = round(clamp(p_edgesLRTB, 0.0, 1.0) * 3.05);
  106. return dot(p_edgesLRTB, vec4(64.0 / 255.0, 16.0 / 255.0, 4.0 / 255.0, 1.0 / 255.0));
  107. }
  108. vec3 NDC_to_view_space(vec2 p_pos, float p_viewspace_depth) {
  109. if (params.is_orthogonal) {
  110. return vec3((params.NDC_to_view_mul * p_pos.xy + params.NDC_to_view_add), p_viewspace_depth);
  111. } else {
  112. return vec3((params.NDC_to_view_mul * p_pos.xy + params.NDC_to_view_add) * p_viewspace_depth, p_viewspace_depth);
  113. }
  114. }
  115. // calculate effect radius and fit our screen sampling pattern inside it
  116. 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) {
  117. r_radius = params.radius;
  118. // when too close, on-screen sampling disk will grow beyond screen size; limit this to avoid closeup temporal artifacts
  119. const float too_close_limit = clamp(p_pix_center_length * params.inv_radius_near_limit, 0.0, 1.0) * 0.8 + 0.2;
  120. r_radius *= too_close_limit;
  121. // 0.85 is to reduce the radius to allow for more samples on a slope to still stay within influence
  122. r_lookup_radius = (0.85 * r_radius) / p_pixel_size_at_center.x;
  123. // used to calculate falloff (both for AO samples and per-sample weights)
  124. r_fallof_sq = -1.0 / (r_radius * r_radius);
  125. }
  126. 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) {
  127. // slope-sensitive depth-based edge detection
  128. vec4 edgesLRTB = vec4(p_left_z, p_right_z, p_top_z, p_bottom_z) - p_center_z;
  129. vec4 edgesLRTB_slope_adjusted = edgesLRTB + edgesLRTB.yxwz;
  130. edgesLRTB = min(abs(edgesLRTB), abs(edgesLRTB_slope_adjusted));
  131. return clamp((1.3 - edgesLRTB / (p_center_z * 0.040)), 0.0, 1.0);
  132. }
  133. vec3 decode_normal(vec3 p_encoded_normal) {
  134. vec3 normal = p_encoded_normal * 2.0 - 1.0;
  135. return normal;
  136. }
  137. vec3 load_normal(ivec2 p_pos) {
  138. vec3 encoded_normal = imageLoad(source_normal, p_pos).xyz;
  139. encoded_normal.z = 1.0 - encoded_normal.z;
  140. return decode_normal(encoded_normal);
  141. }
  142. vec3 load_normal(ivec2 p_pos, ivec2 p_offset) {
  143. vec3 encoded_normal = imageLoad(source_normal, p_pos + p_offset).xyz;
  144. encoded_normal.z = 1.0 - encoded_normal.z;
  145. return decode_normal(encoded_normal);
  146. }
  147. // all vectors in viewspace
  148. float calculate_pixel_obscurance(vec3 p_pixel_normal, vec3 p_hit_delta, float p_fallof_sq) {
  149. float length_sq = dot(p_hit_delta, p_hit_delta);
  150. float NdotD = dot(p_pixel_normal, p_hit_delta) / sqrt(length_sq);
  151. float falloff_mult = max(0.0, length_sq * p_fallof_sq + 1.0);
  152. return max(0, NdotD - 0.05) * falloff_mult;
  153. }
  154. void SSIL_tap_inner(const int p_quality_level, inout vec3 r_color_sum, 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) {
  155. // get depth at sample
  156. float viewspace_sample_z = textureLod(source_depth_mipmaps, vec3(p_sampling_uv, params.pass), p_mip_level).x;
  157. vec3 sample_normal = load_normal(ivec2(p_sampling_uv * vec2(params.screen_size)));
  158. // convert to viewspace
  159. vec3 hit_pos = NDC_to_view_space(p_sampling_uv.xy, viewspace_sample_z);
  160. vec3 hit_delta = hit_pos - p_pix_center_pos;
  161. float obscurance = calculate_pixel_obscurance(p_pixel_normal, hit_delta, p_fallof_sq);
  162. float weight = 1.0;
  163. if (p_quality_level >= SSIL_HALOING_REDUCTION_ENABLE_AT_QUALITY_PRESET) {
  164. float reduct = max(0, -hit_delta.z);
  165. reduct = clamp(reduct * params.neg_inv_radius + 2.0, 0.0, 1.0);
  166. weight = SSIL_HALOING_REDUCTION_AMOUNT * reduct + (1.0 - SSIL_HALOING_REDUCTION_AMOUNT);
  167. }
  168. // Translate sampling_uv to last screen's coordinates
  169. const vec4 sample_pos = projection_constants.reprojection * vec4(p_sampling_uv * 2.0 - 1.0, (viewspace_sample_z - params.z_near) / (params.z_far - params.z_near) * 2.0 - 1.0, 1.0);
  170. vec2 reprojected_sampling_uv = (sample_pos.xy / sample_pos.w) * 0.5 + 0.5;
  171. weight *= p_weight_mod;
  172. r_obscurance_sum += obscurance * weight;
  173. vec3 sample_color = textureLod(last_frame, reprojected_sampling_uv, 5.0).rgb;
  174. // Reduce impact of fireflies by tonemapping before averaging: http://graphicrants.blogspot.com/2013/12/tone-mapping.html
  175. sample_color /= (1.0 + dot(sample_color, vec3(0.299, 0.587, 0.114)));
  176. r_color_sum += sample_color * obscurance * weight * mix(1.0, smoothstep(0.0, 0.1, -dot(sample_normal, normalize(hit_delta))), params.normal_rejection_amount);
  177. r_weight_sum += weight;
  178. }
  179. void SSILTap(const int p_quality_level, inout vec3 r_color_sum, 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) {
  180. vec2 sample_offset;
  181. float sample_pow_2_len;
  182. // patterns
  183. {
  184. vec4 new_sample = sample_pattern[p_tap_index];
  185. sample_offset = new_sample.xy * p_rot_scale;
  186. sample_pow_2_len = new_sample.w; // precalculated, same as: sample_pow_2_len = log2( length( new_sample.xy ) );
  187. p_weight_mod *= new_sample.z;
  188. }
  189. // snap to pixel center (more correct obscurance math, avoids artifacts)
  190. sample_offset = round(sample_offset);
  191. // calculate MIP based on the sample distance from the center, similar to as described
  192. // in http://graphics.cs.williams.edu/papers/SAOHPG12/.
  193. float mip_level = (p_quality_level < SSIL_DEPTH_MIPS_ENABLE_AT_QUALITY_PRESET) ? (0) : (sample_pow_2_len + p_mip_offset);
  194. vec2 sampling_uv = sample_offset * params.half_screen_pixel_size + p_normalized_screen_pos;
  195. SSIL_tap_inner(p_quality_level, r_color_sum, r_obscurance_sum, r_weight_sum, sampling_uv, mip_level, p_pix_center_pos, p_pixel_normal, p_fallof_sq, p_weight_mod);
  196. // for the second tap, just use the mirrored offset
  197. vec2 sample_offset_mirrored_uv = -sample_offset;
  198. // tilt the second set of samples so that the disk is effectively rotated by the normal
  199. // effective at removing one set of artifacts, but too expensive for lower quality settings
  200. if (p_quality_level >= SSIL_TILT_SAMPLES_ENABLE_AT_QUALITY_PRESET) {
  201. float dot_norm = dot(sample_offset_mirrored_uv, p_norm_xy);
  202. sample_offset_mirrored_uv -= dot_norm * p_norm_xy_length * p_norm_xy;
  203. sample_offset_mirrored_uv = round(sample_offset_mirrored_uv);
  204. }
  205. // snap to pixel center (more correct obscurance math, avoids artifacts)
  206. vec2 sampling_mirrored_uv = sample_offset_mirrored_uv * params.half_screen_pixel_size + p_normalized_screen_pos;
  207. SSIL_tap_inner(p_quality_level, r_color_sum, r_obscurance_sum, r_weight_sum, sampling_mirrored_uv, mip_level, p_pix_center_pos, p_pixel_normal, p_fallof_sq, p_weight_mod);
  208. }
  209. void generate_SSIL(out vec3 r_color, out vec4 r_edges, out float r_obscurance, out float r_weight, const vec2 p_pos, int p_quality_level, bool p_adaptive_base) {
  210. vec2 pos_rounded = trunc(p_pos);
  211. uvec2 upos = uvec2(pos_rounded);
  212. const int number_of_taps = (p_adaptive_base) ? (SSIL_ADAPTIVE_TAP_BASE_COUNT) : (num_taps[p_quality_level]);
  213. float pix_z, pix_left_z, pix_top_z, pix_right_z, pix_bottom_z;
  214. vec4 valuesUL = textureGather(source_depth_mipmaps, vec3(pos_rounded * params.half_screen_pixel_size, params.pass));
  215. vec4 valuesBR = textureGather(source_depth_mipmaps, vec3((pos_rounded + vec2(1.0)) * params.half_screen_pixel_size, params.pass));
  216. // get this pixel's viewspace depth
  217. pix_z = valuesUL.y;
  218. // get left right top bottom neighboring pixels for edge detection (gets compiled out on quality_level == 0)
  219. pix_left_z = valuesUL.x;
  220. pix_top_z = valuesUL.z;
  221. pix_right_z = valuesBR.z;
  222. pix_bottom_z = valuesBR.x;
  223. vec2 normalized_screen_pos = pos_rounded * params.half_screen_pixel_size + params.half_screen_pixel_size_x025;
  224. vec3 pix_center_pos = NDC_to_view_space(normalized_screen_pos, pix_z);
  225. // Load this pixel's viewspace normal
  226. uvec2 full_res_coord = upos * 2 * params.size_multiplier + params.pass_coord_offset.xy;
  227. vec3 pixel_normal = load_normal(ivec2(full_res_coord));
  228. 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;
  229. float pixel_lookup_radius;
  230. float fallof_sq;
  231. // calculate effect radius and fit our screen sampling pattern inside it
  232. float viewspace_radius;
  233. calculate_radius_parameters(length(pix_center_pos), pixel_size_at_center, pixel_lookup_radius, viewspace_radius, fallof_sq);
  234. // calculate samples rotation/scaling
  235. mat2 rot_scale_matrix;
  236. uint pseudo_random_index;
  237. {
  238. vec4 rotation_scale;
  239. // reduce effect radius near the screen edges slightly; ideally, one would render a larger depth buffer (5% on each side) instead
  240. if (!p_adaptive_base && (p_quality_level >= SSIL_REDUCE_RADIUS_NEAR_SCREEN_BORDER_ENABLE_AT_QUALITY_PRESET)) {
  241. 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));
  242. near_screen_border = clamp(10.0 * near_screen_border + 0.6, 0.0, 1.0);
  243. pixel_lookup_radius *= near_screen_border;
  244. }
  245. // load & update pseudo-random rotation matrix
  246. pseudo_random_index = uint(pos_rounded.y * 2 + pos_rounded.x) % 5;
  247. rotation_scale = constants.rotation_matrices[params.pass * 5 + pseudo_random_index];
  248. 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);
  249. }
  250. // the main obscurance & sample weight storage
  251. vec3 color_sum = vec3(0.0);
  252. float obscurance_sum = 0.0;
  253. float weight_sum = 0.0;
  254. // 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)
  255. vec4 edgesLRTB = vec4(1.0, 1.0, 1.0, 1.0);
  256. // Move center pixel slightly towards camera to avoid imprecision artifacts due to using of 16bit depth buffer.
  257. pix_center_pos *= 0.99;
  258. if (!p_adaptive_base && (p_quality_level >= SSIL_DEPTH_BASED_EDGES_ENABLE_AT_QUALITY_PRESET)) {
  259. edgesLRTB = calculate_edges(pix_z, pix_left_z, pix_right_z, pix_top_z, pix_bottom_z);
  260. }
  261. const float global_mip_offset = SSIL_DEPTH_MIPS_GLOBAL_OFFSET;
  262. float mip_offset = (p_quality_level < SSIL_DEPTH_MIPS_ENABLE_AT_QUALITY_PRESET) ? (0) : (log2(pixel_lookup_radius) + global_mip_offset);
  263. // Used to tilt the second set of samples so that the disk is effectively rotated by the normal
  264. // effective at removing one set of artifacts, but too expensive for lower quality settings
  265. vec2 norm_xy = vec2(pixel_normal.x, pixel_normal.y);
  266. float norm_xy_length = length(norm_xy);
  267. norm_xy /= vec2(norm_xy_length, -norm_xy_length);
  268. norm_xy_length *= SSIL_TILT_SAMPLES_AMOUNT;
  269. // standard, non-adaptive approach
  270. if ((p_quality_level != 3) || p_adaptive_base) {
  271. for (int i = 0; i < number_of_taps; i++) {
  272. SSILTap(p_quality_level, color_sum, 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);
  273. }
  274. }
  275. #ifdef ADAPTIVE
  276. else {
  277. // add new ones if needed
  278. vec2 full_res_uv = normalized_screen_pos + params.pass_uv_offset.xy;
  279. float importance = textureLod(source_importance, full_res_uv, 0.0).x;
  280. //Need to store obscurance from base pass
  281. // load existing base values
  282. vec4 base_values = imageLoad(source_ssil, ivec3(upos, params.pass));
  283. weight_sum += imageLoad(edges_weights_image, ivec2(upos)).r * float(SSIL_ADAPTIVE_TAP_BASE_COUNT * 4.0);
  284. color_sum += (base_values.rgb) * weight_sum;
  285. obscurance_sum += (base_values.a) * weight_sum;
  286. // increase importance around edges
  287. float edge_count = dot(1.0 - edgesLRTB, vec4(1.0, 1.0, 1.0, 1.0));
  288. float avg_total_importance = float(counter.sum) * params.load_counter_avg_div;
  289. float importance_limiter = clamp(params.adaptive_sample_limit / avg_total_importance, 0.0, 1.0);
  290. importance *= importance_limiter;
  291. float additional_sample_count = SSIL_ADAPTIVE_TAP_FLEXIBLE_COUNT * importance;
  292. const float blend_range = 3.0;
  293. const float blend_range_inv = 1.0 / blend_range;
  294. additional_sample_count += 0.5;
  295. uint additional_samples = uint(additional_sample_count);
  296. uint additional_samples_to = min(SSIL_MAX_TAPS, additional_samples + SSIL_ADAPTIVE_TAP_BASE_COUNT);
  297. for (uint i = SSIL_ADAPTIVE_TAP_BASE_COUNT; i < additional_samples_to; i++) {
  298. additional_sample_count -= 1.0f;
  299. float weight_mod = clamp(additional_sample_count * blend_range_inv, 0.0, 1.0);
  300. SSILTap(p_quality_level, color_sum, 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);
  301. }
  302. }
  303. #endif
  304. // Early out for adaptive base
  305. if (p_adaptive_base) {
  306. vec3 color = color_sum / weight_sum;
  307. r_color = color;
  308. r_edges = vec4(0.0);
  309. r_obscurance = obscurance_sum / weight_sum;
  310. r_weight = weight_sum;
  311. return;
  312. }
  313. // Calculate weighted average
  314. vec3 color = color_sum / weight_sum;
  315. color /= 1.0 - dot(color, vec3(0.299, 0.587, 0.114));
  316. // Calculate fadeout (1 close, gradient, 0 far)
  317. float fade_out = clamp(pix_center_pos.z * params.fade_out_mul + params.fade_out_add, 0.0, 1.0);
  318. // Reduce the SSIL if we're on the edge to remove artifacts on edges (we don't care for the lower quality one)
  319. if (!p_adaptive_base && (p_quality_level >= SSIL_DEPTH_BASED_EDGES_ENABLE_AT_QUALITY_PRESET)) {
  320. // when there's more than 2 opposite edges, start fading out the occlusion to reduce aliasing artifacts
  321. 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);
  322. fade_out *= clamp(1.0 - edge_fadeout_factor, 0.0, 1.0);
  323. }
  324. color = params.intensity * color;
  325. color *= fade_out;
  326. // outputs!
  327. r_color = color;
  328. 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.
  329. r_obscurance = clamp((obscurance_sum / weight_sum) * params.intensity, 0.0, 1.0);
  330. r_weight = weight_sum;
  331. }
  332. void main() {
  333. vec3 out_color;
  334. float out_obscurance;
  335. float out_weight;
  336. vec4 out_edges;
  337. ivec2 ssC = ivec2(gl_GlobalInvocationID.xy);
  338. if (any(greaterThanEqual(ssC, params.screen_size))) { //too large, do nothing
  339. return;
  340. }
  341. vec2 uv = vec2(gl_GlobalInvocationID) + vec2(0.5);
  342. #ifdef SSIL_BASE
  343. generate_SSIL(out_color, out_edges, out_obscurance, out_weight, uv, params.quality, true);
  344. imageStore(dest_image, ssC, vec4(out_color, out_obscurance));
  345. imageStore(edges_weights_image, ssC, vec4(out_weight / (float(SSIL_ADAPTIVE_TAP_BASE_COUNT) * 4.0)));
  346. #else
  347. generate_SSIL(out_color, out_edges, out_obscurance, out_weight, uv, params.quality, false); // pass in quality levels
  348. imageStore(dest_image, ssC, vec4(out_color, out_obscurance));
  349. imageStore(edges_weights_image, ssC, vec4(pack_edges(out_edges)));
  350. #endif
  351. }