ssao_blur.glsl 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  19. #[compute]
  20. #version 450
  21. #VERSION_DEFINES
  22. layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in;
  23. layout(set = 0, binding = 0) uniform sampler2D source_ssao;
  24. layout(rg8, set = 1, binding = 0) uniform restrict writeonly image2D dest_image;
  25. layout(push_constant, std430) uniform Params {
  26. float edge_sharpness;
  27. float pad;
  28. vec2 half_screen_pixel_size;
  29. }
  30. params;
  31. vec4 unpack_edges(float p_packed_val) {
  32. uint packed_val = uint(p_packed_val * 255.5);
  33. vec4 edgesLRTB;
  34. edgesLRTB.x = float((packed_val >> 6) & 0x03) / 3.0;
  35. edgesLRTB.y = float((packed_val >> 4) & 0x03) / 3.0;
  36. edgesLRTB.z = float((packed_val >> 2) & 0x03) / 3.0;
  37. edgesLRTB.w = float((packed_val >> 0) & 0x03) / 3.0;
  38. return clamp(edgesLRTB + params.edge_sharpness, 0.0, 1.0);
  39. }
  40. void add_sample(float p_ssao_value, float p_edge_value, inout float r_sum, inout float r_sum_weight) {
  41. float weight = p_edge_value;
  42. r_sum += (weight * p_ssao_value);
  43. r_sum_weight += weight;
  44. }
  45. #ifdef MODE_WIDE
  46. vec2 sample_blurred_wide(vec2 p_coord) {
  47. vec2 vC = textureLodOffset(source_ssao, vec2(p_coord), 0.0, ivec2(0, 0)).xy;
  48. vec2 vL = textureLodOffset(source_ssao, vec2(p_coord), 0.0, ivec2(-2, 0)).xy;
  49. vec2 vT = textureLodOffset(source_ssao, vec2(p_coord), 0.0, ivec2(0, -2)).xy;
  50. vec2 vR = textureLodOffset(source_ssao, vec2(p_coord), 0.0, ivec2(2, 0)).xy;
  51. vec2 vB = textureLodOffset(source_ssao, vec2(p_coord), 0.0, ivec2(0, 2)).xy;
  52. float packed_edges = vC.y;
  53. vec4 edgesLRTB = unpack_edges(packed_edges);
  54. edgesLRTB.x *= unpack_edges(vL.y).y;
  55. edgesLRTB.z *= unpack_edges(vT.y).w;
  56. edgesLRTB.y *= unpack_edges(vR.y).x;
  57. edgesLRTB.w *= unpack_edges(vB.y).z;
  58. float ssao_value = vC.x;
  59. float ssao_valueL = vL.x;
  60. float ssao_valueT = vT.x;
  61. float ssao_valueR = vR.x;
  62. float ssao_valueB = vB.x;
  63. float sum_weight = 0.8f;
  64. float sum = ssao_value * sum_weight;
  65. add_sample(ssao_valueL, edgesLRTB.x, sum, sum_weight);
  66. add_sample(ssao_valueR, edgesLRTB.y, sum, sum_weight);
  67. add_sample(ssao_valueT, edgesLRTB.z, sum, sum_weight);
  68. add_sample(ssao_valueB, edgesLRTB.w, sum, sum_weight);
  69. float ssao_avg = sum / sum_weight;
  70. ssao_value = ssao_avg;
  71. return vec2(ssao_value, packed_edges);
  72. }
  73. #endif
  74. #ifdef MODE_SMART
  75. vec2 sample_blurred(vec3 p_pos, vec2 p_coord) {
  76. float packed_edges = texelFetch(source_ssao, ivec2(p_pos.xy), 0).y;
  77. vec4 edgesLRTB = unpack_edges(packed_edges);
  78. vec4 valuesUL = textureGather(source_ssao, vec2(p_coord - params.half_screen_pixel_size * 0.5));
  79. vec4 valuesBR = textureGather(source_ssao, vec2(p_coord + params.half_screen_pixel_size * 0.5));
  80. float ssao_value = valuesUL.y;
  81. float ssao_valueL = valuesUL.x;
  82. float ssao_valueT = valuesUL.z;
  83. float ssao_valueR = valuesBR.z;
  84. float ssao_valueB = valuesBR.x;
  85. float sum_weight = 0.5;
  86. float sum = ssao_value * sum_weight;
  87. add_sample(ssao_valueL, edgesLRTB.x, sum, sum_weight);
  88. add_sample(ssao_valueR, edgesLRTB.y, sum, sum_weight);
  89. add_sample(ssao_valueT, edgesLRTB.z, sum, sum_weight);
  90. add_sample(ssao_valueB, edgesLRTB.w, sum, sum_weight);
  91. float ssao_avg = sum / sum_weight;
  92. ssao_value = ssao_avg;
  93. return vec2(ssao_value, packed_edges);
  94. }
  95. #endif
  96. void main() {
  97. // Pixel being shaded
  98. ivec2 ssC = ivec2(gl_GlobalInvocationID.xy);
  99. #ifdef MODE_NON_SMART
  100. vec2 half_pixel = params.half_screen_pixel_size * 0.5;
  101. vec2 uv = (vec2(gl_GlobalInvocationID.xy) + vec2(0.5, 0.5)) * params.half_screen_pixel_size;
  102. vec2 center = textureLod(source_ssao, vec2(uv), 0.0).xy;
  103. vec4 vals;
  104. vals.x = textureLod(source_ssao, vec2(uv + vec2(-half_pixel.x * 3, -half_pixel.y)), 0.0).x;
  105. vals.y = textureLod(source_ssao, vec2(uv + vec2(+half_pixel.x, -half_pixel.y * 3)), 0.0).x;
  106. vals.z = textureLod(source_ssao, vec2(uv + vec2(-half_pixel.x, +half_pixel.y * 3)), 0.0).x;
  107. vals.w = textureLod(source_ssao, vec2(uv + vec2(+half_pixel.x * 3, +half_pixel.y)), 0.0).x;
  108. vec2 sampled = vec2(dot(vals, vec4(0.2)) + center.x * 0.2, center.y);
  109. #else
  110. #ifdef MODE_SMART
  111. vec2 sampled = sample_blurred(vec3(gl_GlobalInvocationID), (vec2(gl_GlobalInvocationID.xy) + vec2(0.5, 0.5)) * params.half_screen_pixel_size);
  112. #else // MODE_WIDE
  113. vec2 sampled = sample_blurred_wide((vec2(gl_GlobalInvocationID.xy) + vec2(0.5, 0.5)) * params.half_screen_pixel_size);
  114. #endif
  115. #endif
  116. imageStore(dest_image, ivec2(ssC), vec4(sampled, 0.0, 0.0));
  117. }