ssil_blur.glsl 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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: filip.strugar@intel.com: 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. layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in;
  24. layout(set = 0, binding = 0) uniform sampler2D source_ssil;
  25. layout(rgba16, set = 1, binding = 0) uniform restrict writeonly image2D dest_image;
  26. layout(r8, set = 2, binding = 0) uniform restrict readonly image2D source_edges;
  27. layout(push_constant, std430) uniform Params {
  28. float edge_sharpness;
  29. float pad;
  30. vec2 half_screen_pixel_size;
  31. }
  32. params;
  33. vec4 unpack_edges(float p_packed_val) {
  34. uint packed_val = uint(p_packed_val * 255.5);
  35. vec4 edgesLRTB;
  36. edgesLRTB.x = float((packed_val >> 6) & 0x03) / 3.0;
  37. edgesLRTB.y = float((packed_val >> 4) & 0x03) / 3.0;
  38. edgesLRTB.z = float((packed_val >> 2) & 0x03) / 3.0;
  39. edgesLRTB.w = float((packed_val >> 0) & 0x03) / 3.0;
  40. return clamp(edgesLRTB + params.edge_sharpness, 0.0, 1.0);
  41. }
  42. void add_sample(vec4 p_ssil_value, float p_edge_value, inout vec4 r_sum, inout float r_sum_weight) {
  43. float weight = p_edge_value;
  44. r_sum += (weight * p_ssil_value);
  45. r_sum_weight += weight;
  46. }
  47. #ifdef MODE_WIDE
  48. vec4 sample_blurred_wide(ivec2 p_pos, vec2 p_coord) {
  49. vec4 ssil_value = textureLodOffset(source_ssil, vec2(p_coord), 0.0, ivec2(0, 0));
  50. vec4 ssil_valueL = textureLodOffset(source_ssil, vec2(p_coord), 0.0, ivec2(-2, 0));
  51. vec4 ssil_valueT = textureLodOffset(source_ssil, vec2(p_coord), 0.0, ivec2(0, -2));
  52. vec4 ssil_valueR = textureLodOffset(source_ssil, vec2(p_coord), 0.0, ivec2(2, 0));
  53. vec4 ssil_valueB = textureLodOffset(source_ssil, vec2(p_coord), 0.0, ivec2(0, 2));
  54. vec4 edgesLRTB = unpack_edges(imageLoad(source_edges, p_pos).r);
  55. edgesLRTB.x *= unpack_edges(imageLoad(source_edges, p_pos + ivec2(-2, 0)).r).y;
  56. edgesLRTB.z *= unpack_edges(imageLoad(source_edges, p_pos + ivec2(0, -2)).r).w;
  57. edgesLRTB.y *= unpack_edges(imageLoad(source_edges, p_pos + ivec2(2, 0)).r).x;
  58. edgesLRTB.w *= unpack_edges(imageLoad(source_edges, p_pos + ivec2(0, 2)).r).z;
  59. float sum_weight = 0.8;
  60. vec4 sum = ssil_value * sum_weight;
  61. add_sample(ssil_valueL, edgesLRTB.x, sum, sum_weight);
  62. add_sample(ssil_valueR, edgesLRTB.y, sum, sum_weight);
  63. add_sample(ssil_valueT, edgesLRTB.z, sum, sum_weight);
  64. add_sample(ssil_valueB, edgesLRTB.w, sum, sum_weight);
  65. vec4 ssil_avg = sum / sum_weight;
  66. ssil_value = ssil_avg;
  67. return ssil_value;
  68. }
  69. #endif
  70. #ifdef MODE_SMART
  71. vec4 sample_blurred(ivec2 p_pos, vec2 p_coord) {
  72. vec4 vC = textureLodOffset(source_ssil, vec2(p_coord), 0.0, ivec2(0, 0));
  73. vec4 vL = textureLodOffset(source_ssil, vec2(p_coord), 0.0, ivec2(-1, 0));
  74. vec4 vT = textureLodOffset(source_ssil, vec2(p_coord), 0.0, ivec2(0, -1));
  75. vec4 vR = textureLodOffset(source_ssil, vec2(p_coord), 0.0, ivec2(1, 0));
  76. vec4 vB = textureLodOffset(source_ssil, vec2(p_coord), 0.0, ivec2(0, 1));
  77. float packed_edges = imageLoad(source_edges, p_pos).r;
  78. vec4 edgesLRTB = unpack_edges(packed_edges);
  79. float sum_weight = 0.5;
  80. vec4 sum = vC * sum_weight;
  81. add_sample(vL, edgesLRTB.x, sum, sum_weight);
  82. add_sample(vR, edgesLRTB.y, sum, sum_weight);
  83. add_sample(vT, edgesLRTB.z, sum, sum_weight);
  84. add_sample(vB, edgesLRTB.w, sum, sum_weight);
  85. vec4 ssil_avg = sum / sum_weight;
  86. vec4 ssil_value = ssil_avg;
  87. return ssil_value;
  88. }
  89. #endif
  90. void main() {
  91. // Pixel being shaded
  92. ivec2 ssC = ivec2(gl_GlobalInvocationID.xy);
  93. #ifdef MODE_NON_SMART
  94. vec2 half_pixel = params.half_screen_pixel_size * 0.5;
  95. vec2 uv = (vec2(gl_GlobalInvocationID.xy) + vec2(0.5, 0.5)) * params.half_screen_pixel_size;
  96. vec4 center = textureLod(source_ssil, uv, 0.0);
  97. vec4 value = textureLod(source_ssil, vec2(uv + vec2(-half_pixel.x * 3, -half_pixel.y)), 0.0) * 0.2;
  98. value += textureLod(source_ssil, vec2(uv + vec2(+half_pixel.x, -half_pixel.y * 3)), 0.0) * 0.2;
  99. value += textureLod(source_ssil, vec2(uv + vec2(-half_pixel.x, +half_pixel.y * 3)), 0.0) * 0.2;
  100. value += textureLod(source_ssil, vec2(uv + vec2(+half_pixel.x * 3, +half_pixel.y)), 0.0) * 0.2;
  101. vec4 sampled = value + center * 0.2;
  102. #else
  103. #ifdef MODE_SMART
  104. vec4 sampled = sample_blurred(ssC, (vec2(gl_GlobalInvocationID.xy) + vec2(0.5, 0.5)) * params.half_screen_pixel_size);
  105. #else // MODE_WIDE
  106. vec4 sampled = sample_blurred_wide(ssC, (vec2(gl_GlobalInvocationID.xy) + vec2(0.5, 0.5)) * params.half_screen_pixel_size);
  107. #endif
  108. #endif // MODE_NON_SMART
  109. imageStore(dest_image, ssC, sampled);
  110. }