HzbMaxDepthProject.ankiprog 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // Copyright (C) 2009-present, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. // This shader draws tile aligned boxes in order to fill the HZB buffer for cascaded shadows
  6. #pragma anki technique vert pixel
  7. #include <AnKi/Shaders/Functions.hlsl>
  8. Texture2D<Vec4> g_maxDepthRt : register(t0);
  9. struct Constants
  10. {
  11. Mat4 m_reprojectionMat;
  12. F32 m_cascadeMinDepth;
  13. F32 m_cascadeMaxDepth;
  14. F32 m_padding0;
  15. F32 m_padding1;
  16. };
  17. ANKI_FAST_CONSTANTS(Constants, g_consts)
  18. #if ANKI_VERTEX_SHADER
  19. Vec4 main(U32 svVertexId : SV_VERTEXID, U32 svInstanceId : SV_INSTANCEID) : SV_POSITION
  20. {
  21. UVec2 maxDepthRtSize;
  22. g_maxDepthRt.GetDimensions(maxDepthRtSize.x, maxDepthRtSize.y);
  23. const U32 tileX = svInstanceId % maxDepthRtSize.x;
  24. const U32 tileY = svInstanceId / maxDepthRtSize.x;
  25. const F32 maxDepth = min(g_maxDepthRt[UVec2(tileX, tileY)].x, g_consts.m_cascadeMaxDepth);
  26. const F32 minDepth = g_consts.m_cascadeMinDepth;
  27. if(maxDepth <= minDepth)
  28. {
  29. // Tile's depth is outside the cascade, make the box digenerate
  30. return 0.0f;
  31. }
  32. // Z
  33. Vec3 ndc;
  34. ndc.z = (svVertexId <= 3) ? minDepth : maxDepth;
  35. // X
  36. ndc.x = F32(tileX);
  37. if(svVertexId == 1 || svVertexId == 2 || svVertexId == 5 || svVertexId == 6)
  38. {
  39. // Right side, move the point
  40. ndc.x += 1.0f;
  41. }
  42. // Y
  43. ndc.y = F32(tileY);
  44. if(svVertexId == 0 || svVertexId == 1 || svVertexId == 4 || svVertexId == 5)
  45. {
  46. // Top side, move the point
  47. ndc.y += 1.0f;
  48. }
  49. ndc.xy /= Vec2(maxDepthRtSize);
  50. ndc.xy = uvToNdc(saturate(ndc.xy));
  51. // Unproject and project
  52. return mul(g_consts.m_reprojectionMat, Vec4(ndc, 1.0));
  53. }
  54. #endif // ANKI_VERTEX_SHADER
  55. #if ANKI_PIXEL_SHADER
  56. void main()
  57. {
  58. }
  59. #endif // ANKI_PIXEL_SHADER