ssao_interleave.glsl 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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(rgba8, set = 0, binding = 0) uniform restrict writeonly image2D dest_image;
  24. layout(set = 1, binding = 0) uniform sampler2DArray source_texture;
  25. layout(push_constant, std430) uniform Params {
  26. float inv_sharpness;
  27. uint size_modifier;
  28. vec2 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.inv_sharpness, 0.0, 1.0);
  39. }
  40. void main() {
  41. ivec2 ssC = ivec2(gl_GlobalInvocationID.xy);
  42. if (any(greaterThanEqual(ssC, ivec2(1.0 / params.pixel_size)))) { //too large, do nothing
  43. return;
  44. }
  45. #ifdef MODE_SMART
  46. float ao;
  47. uvec2 pix_pos = uvec2(gl_GlobalInvocationID.xy);
  48. vec2 uv = (gl_GlobalInvocationID.xy + vec2(0.5)) * params.pixel_size;
  49. // calculate index in the four deinterleaved source array texture
  50. int mx = int(pix_pos.x % 2);
  51. int my = int(pix_pos.y % 2);
  52. int index_center = mx + my * 2; // center index
  53. int index_horizontal = (1 - mx) + my * 2; // neighboring, horizontal
  54. int index_vertical = mx + (1 - my) * 2; // neighboring, vertical
  55. int index_diagonal = (1 - mx) + (1 - my) * 2; // diagonal
  56. vec2 center_val = texelFetch(source_texture, ivec3(pix_pos / uvec2(params.size_modifier), index_center), 0).xy;
  57. ao = center_val.x;
  58. vec4 edgesLRTB = unpack_edges(center_val.y);
  59. // convert index shifts to sampling offsets
  60. float fmx = float(mx);
  61. float fmy = float(my);
  62. // in case of an edge, push sampling offsets away from the edge (towards pixel center)
  63. float fmxe = (edgesLRTB.y - edgesLRTB.x);
  64. float fmye = (edgesLRTB.w - edgesLRTB.z);
  65. // calculate final sampling offsets and sample using bilinear filter
  66. vec2 uv_horizontal = (gl_GlobalInvocationID.xy + vec2(0.5) + vec2(fmx + fmxe - 0.5, 0.5 - fmy)) * params.pixel_size;
  67. float ao_horizontal = textureLod(source_texture, vec3(uv_horizontal, index_horizontal), 0.0).x;
  68. vec2 uv_vertical = (gl_GlobalInvocationID.xy + vec2(0.5) + vec2(0.5 - fmx, fmy - 0.5 + fmye)) * params.pixel_size;
  69. float ao_vertical = textureLod(source_texture, vec3(uv_vertical, index_vertical), 0.0).x;
  70. vec2 uv_diagonal = (gl_GlobalInvocationID.xy + vec2(0.5) + vec2(fmx - 0.5 + fmxe, fmy - 0.5 + fmye)) * params.pixel_size;
  71. float ao_diagonal = textureLod(source_texture, vec3(uv_diagonal, index_diagonal), 0.0).x;
  72. // reduce weight for samples near edge - if the edge is on both sides, weight goes to 0
  73. vec4 blendWeights;
  74. blendWeights.x = 1.0;
  75. blendWeights.y = (edgesLRTB.x + edgesLRTB.y) * 0.5;
  76. blendWeights.z = (edgesLRTB.z + edgesLRTB.w) * 0.5;
  77. blendWeights.w = (blendWeights.y + blendWeights.z) * 0.5;
  78. // calculate weighted average
  79. float blendWeightsSum = dot(blendWeights, vec4(1.0, 1.0, 1.0, 1.0));
  80. ao = dot(vec4(ao, ao_horizontal, ao_vertical, ao_diagonal), blendWeights) / blendWeightsSum;
  81. imageStore(dest_image, ivec2(gl_GlobalInvocationID.xy), vec4(ao));
  82. #else // !MODE_SMART
  83. vec2 uv = (gl_GlobalInvocationID.xy + vec2(0.5)) * params.pixel_size;
  84. #ifdef MODE_HALF
  85. float a = textureLod(source_texture, vec3(uv, 0), 0.0).x;
  86. float d = textureLod(source_texture, vec3(uv, 3), 0.0).x;
  87. float avg = (a + d) * 0.5;
  88. #else
  89. float a = textureLod(source_texture, vec3(uv, 0), 0.0).x;
  90. float b = textureLod(source_texture, vec3(uv, 1), 0.0).x;
  91. float c = textureLod(source_texture, vec3(uv, 2), 0.0).x;
  92. float d = textureLod(source_texture, vec3(uv, 3), 0.0).x;
  93. float avg = (a + b + c + d) * 0.25;
  94. #endif
  95. imageStore(dest_image, ivec2(gl_GlobalInvocationID.xy), vec4(avg));
  96. #endif
  97. }