DepthDownscaleCompute.ankiprog 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Copyright (C) 2009-2022, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. // The 1st reduction is average depth. The next reductions are max depth
  6. #pragma anki mutator WAVE_OPERATIONS 0 1
  7. #pragma anki start comp
  8. #include <AnKi/Shaders/Common.glsl>
  9. #include <AnKi/Shaders/Include/MiscRendererTypes.h>
  10. layout(local_size_x = 256) in;
  11. layout(push_constant, std140) uniform b_pc
  12. {
  13. DepthDownscaleUniforms u_unis;
  14. };
  15. layout(set = 0, binding = 0) uniform image2D u_dstImages[12u];
  16. layout(set = 0, binding = 1) coherent uniform image2D u_dstImage5;
  17. layout(set = 0, binding = 2) coherent buffer b_atomic
  18. {
  19. U32 u_spdCounter;
  20. };
  21. layout(std430, set = 0, binding = 3) writeonly buffer b_cb
  22. {
  23. F32 u_clientBuf[];
  24. };
  25. layout(set = 0, binding = 4) uniform sampler u_linearAnyClampSampler;
  26. layout(set = 0, binding = 5) uniform texture2D u_srcTex;
  27. // Include SPD
  28. #define A_GPU 1
  29. #define A_GLSL 1
  30. #include <ThirdParty/FidelityFX/ffx_a.h>
  31. shared AU1 s_spdCounter;
  32. shared AF1 s_spdIntermediateR[16][16];
  33. AF4 SpdLoadSourceImage(AU2 p, AU1 slice)
  34. {
  35. const AF2 textureCoord = Vec2(p) * u_unis.m_srcTexSizeOverOne + u_unis.m_srcTexSizeOverOne;
  36. return AF4(textureLod(u_srcTex, u_linearAnyClampSampler, textureCoord, 0.0).r, 0.0, 0.0, 0.0);
  37. }
  38. AF4 SpdLoad(AU2 p, AU1 slice)
  39. {
  40. return AF4(imageLoad(u_dstImage5, IVec2(p)).r, 0.0, 0.0, 0.0);
  41. }
  42. void SpdStore(AU2 p, AF4 value, AU1 mip, AU1 slice)
  43. {
  44. if(mip == 5u)
  45. {
  46. imageStore(u_dstImage5, IVec2(p), Vec4(value.x, 0.0, 0.0, 0.0));
  47. }
  48. else
  49. {
  50. imageStore(u_dstImages[mip], IVec2(p), Vec4(value.x, 0.0, 0.0, 0.0));
  51. }
  52. // Store the last mip to the buffer as well
  53. if(mip == u_unis.m_mipmapCount - 1u)
  54. {
  55. const U32 idx = p.y * u_unis.m_lastMipWidth + p.x;
  56. u_clientBuf[idx] = value.x;
  57. }
  58. }
  59. void SpdIncreaseAtomicCounter(AU1 slice)
  60. {
  61. s_spdCounter = atomicAdd(u_spdCounter, 1u);
  62. }
  63. AU1 SpdGetAtomicCounter()
  64. {
  65. return s_spdCounter;
  66. }
  67. void SpdResetAtomicCounter(AU1 slice)
  68. {
  69. u_spdCounter = 0u;
  70. }
  71. AF4 SpdLoadIntermediate(AU1 x, AU1 y)
  72. {
  73. return AF4(s_spdIntermediateR[x][y], 0.0, 0.0, 0.0);
  74. }
  75. void SpdStoreIntermediate(AU1 x, AU1 y, AF4 value)
  76. {
  77. s_spdIntermediateR[x][y] = value.x;
  78. }
  79. AF4 SpdReduce4(AF4 v0, AF4 v1, AF4 v2, AF4 v3)
  80. {
  81. const F32 maxDepth = max(v0.x, max(v1.x, max(v2.x, v3.x)));
  82. return AF4(maxDepth, 0.0, 0.0, 0.0);
  83. }
  84. #define SPD_LINEAR_SAMPLER 1
  85. #if WAVE_OPERATIONS == 0
  86. # define SPD_NO_WAVE_OPERATIONS 1
  87. #endif
  88. #include <ThirdParty/FidelityFX/ffx_spd.h>
  89. void main()
  90. {
  91. const U32 slice = 0u;
  92. const UVec2 offset = UVec2(0u);
  93. SpdDownsample(AU2(gl_WorkGroupID.xy), AU1(gl_LocalInvocationIndex), AU1(u_unis.m_mipmapCount),
  94. AU1(u_unis.m_workgroupCount), slice, offset);
  95. }
  96. #pragma anki end