ssil_interleave.glsl 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in;
  24. layout(rgba16, set = 0, binding = 0) uniform restrict writeonly image2D dest_image;
  25. layout(set = 1, binding = 0) uniform sampler2DArray source_texture;
  26. layout(r8, set = 2, binding = 0) uniform restrict readonly image2DArray source_edges;
  27. layout(push_constant, std430) uniform Params {
  28. float inv_sharpness;
  29. uint size_modifier;
  30. vec2 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.inv_sharpness, 0.0, 1.0);
  41. }
  42. void main() {
  43. ivec2 ssC = ivec2(gl_GlobalInvocationID.xy);
  44. if (any(greaterThanEqual(ssC, ivec2(1.0 / params.pixel_size)))) { //too large, do nothing
  45. return;
  46. }
  47. #ifdef MODE_SMART
  48. uvec2 pix_pos = uvec2(gl_GlobalInvocationID.xy);
  49. vec2 uv = (gl_GlobalInvocationID.xy + vec2(0.5)) * params.pixel_size;
  50. // calculate index in the four deinterleaved source array texture
  51. int mx = int(pix_pos.x % 2);
  52. int my = int(pix_pos.y % 2);
  53. int index_center = mx + my * 2; // center index
  54. int index_horizontal = (1 - mx) + my * 2; // neighboring, horizontal
  55. int index_vertical = mx + (1 - my) * 2; // neighboring, vertical
  56. int index_diagonal = (1 - mx) + (1 - my) * 2; // diagonal
  57. vec4 color = texelFetch(source_texture, ivec3(pix_pos / uvec2(params.size_modifier), index_center), 0);
  58. vec4 edgesLRTB = unpack_edges(imageLoad(source_edges, ivec3(pix_pos / uvec2(params.size_modifier), index_center)).r);
  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. vec4 color_horizontal = textureLod(source_texture, vec3(uv_horizontal, index_horizontal), 0.0);
  68. vec2 uv_vertical = (gl_GlobalInvocationID.xy + vec2(0.5) + vec2(0.5 - fmx, fmy - 0.5 + fmye)) * params.pixel_size;
  69. vec4 color_vertical = textureLod(source_texture, vec3(uv_vertical, index_vertical), 0.0);
  70. vec2 uv_diagonal = (gl_GlobalInvocationID.xy + vec2(0.5) + vec2(fmx - 0.5 + fmxe, fmy - 0.5 + fmye)) * params.pixel_size;
  71. vec4 color_diagonal = textureLod(source_texture, vec3(uv_diagonal, index_diagonal), 0.0);
  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. color += color_horizontal * blendWeights.y;
  81. color += color_vertical * blendWeights.z;
  82. color += color_diagonal * blendWeights.w;
  83. color /= blendWeightsSum;
  84. imageStore(dest_image, ivec2(gl_GlobalInvocationID.xy), color);
  85. #else // !MODE_SMART
  86. vec2 uv = (gl_GlobalInvocationID.xy + vec2(0.5)) * params.pixel_size;
  87. #ifdef MODE_HALF
  88. vec4 a = textureLod(source_texture, vec3(uv, 0), 0.0);
  89. vec4 d = textureLod(source_texture, vec3(uv, 3), 0.0);
  90. vec4 avg = (a + d) * 0.5;
  91. #else
  92. vec4 a = textureLod(source_texture, vec3(uv, 0), 0.0);
  93. vec4 b = textureLod(source_texture, vec3(uv, 1), 0.0);
  94. vec4 c = textureLod(source_texture, vec3(uv, 2), 0.0);
  95. vec4 d = textureLod(source_texture, vec3(uv, 3), 0.0);
  96. vec4 avg = (a + b + c + d) * 0.25;
  97. #endif
  98. imageStore(dest_image, ivec2(gl_GlobalInvocationID.xy), avg);
  99. #endif
  100. }