DepthDownscale.ankiprog 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // Copyright (C) 2009-2020, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #pragma anki mutator SAMPLE_RESOLVE_TYPE 0 1 2 // 0: average, 1: min, 2: max
  6. #define AVG 0
  7. #define MIN 1
  8. #define MAX 2
  9. #pragma anki start comp
  10. #include <shaders/Common.glsl>
  11. layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in;
  12. layout(push_constant, std430) uniform pc_
  13. {
  14. UVec2 u_level0WriteImgSize;
  15. UVec2 u_level1WriteImgSize;
  16. U32 u_copyToClientLevel;
  17. U32 u_writeLevel1;
  18. U32 u_padding0;
  19. U32 u_padding1;
  20. };
  21. layout(set = 0, binding = 0) uniform sampler u_nearestAnyClampSampler;
  22. layout(set = 0, binding = 1) uniform texture2D u_readTex;
  23. layout(set = 0, binding = 2) writeonly uniform image2D u_level0WriteImg;
  24. layout(set = 0, binding = 3) writeonly uniform image2D u_level1WriteImg;
  25. layout(std430, set = 0, binding = 4) writeonly buffer s1_
  26. {
  27. F32 u_clientBuf[];
  28. };
  29. shared F32 s_depths[gl_WorkGroupSize.y][gl_WorkGroupSize.x];
  30. // Resolve depths into one value
  31. F32 resolveDepths(Vec4 depths)
  32. {
  33. #if SAMPLE_RESOLVE_TYPE == MIN
  34. Vec2 mind2 = min(depths.xy, depths.zw);
  35. const F32 depth = min(mind2.x, mind2.y);
  36. #elif SAMPLE_RESOLVE_TYPE == MAX
  37. Vec2 max2 = max(depths.xy, depths.zw);
  38. const F32 depth = max(max2.x, max2.y);
  39. #elif SAMPLE_RESOLVE_TYPE == AVG
  40. const F32 depth = dot(depths, Vec4(1.0 / 4.0));
  41. #else
  42. # error See file
  43. #endif
  44. return depth;
  45. }
  46. void main()
  47. {
  48. // Read depth
  49. const Vec2 readUv = (Vec2(gl_GlobalInvocationID.xy) + 0.5) / Vec2(u_level0WriteImgSize);
  50. Vec4 depths = textureGather(sampler2D(u_readTex, u_nearestAnyClampSampler), readUv, 0);
  51. // Resolve & store the 1st level
  52. F32 depth = resolveDepths(depths);
  53. s_depths[gl_LocalInvocationID.y][gl_LocalInvocationID.x] = depth;
  54. if(all(lessThan(gl_GlobalInvocationID.xy, u_level0WriteImgSize)))
  55. {
  56. imageStore(u_level0WriteImg, IVec2(gl_GlobalInvocationID.xy), Vec4(depth));
  57. if(u_copyToClientLevel == 0u)
  58. {
  59. const U32 idx = gl_GlobalInvocationID.y * u_level0WriteImgSize.x + gl_GlobalInvocationID.x;
  60. u_clientBuf[idx] = depth;
  61. }
  62. }
  63. // Sync
  64. memoryBarrierShared();
  65. barrier();
  66. // Resolve 2nd level
  67. if(u_writeLevel1 == 1u && all(equal(gl_LocalInvocationID.xy & UVec2(1u), UVec2(0u))))
  68. {
  69. depths.x = depth;
  70. depths.y = s_depths[gl_LocalInvocationID.y + 0u][gl_LocalInvocationID.x + 1u];
  71. depths.z = s_depths[gl_LocalInvocationID.y + 1u][gl_LocalInvocationID.x + 1u];
  72. depths.w = s_depths[gl_LocalInvocationID.y + 1u][gl_LocalInvocationID.x + 0u];
  73. depth = resolveDepths(depths);
  74. const UVec2 writeUv = gl_GlobalInvocationID.xy >> 1u;
  75. if(all(lessThan(writeUv, u_level1WriteImgSize)))
  76. {
  77. imageStore(u_level1WriteImg, IVec2(writeUv), Vec4(depth));
  78. if(u_copyToClientLevel == 1u)
  79. {
  80. const U32 idx = writeUv.y * u_level1WriteImgSize.x + writeUv.x;
  81. u_clientBuf[idx] = depth;
  82. }
  83. }
  84. }
  85. }
  86. #pragma anki end