LensFlareUpdateIndirectInfo.ankiprog 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. #pragma anki technique comp
  6. #include <AnKi/Shaders/Common.hlsl>
  7. #define THREAD_COUNT_SQRT 8
  8. ANKI_FAST_CONSTANTS(Mat4, g_mvp)
  9. StructuredBuffer<Vec4> g_flarePositions : register(t0);
  10. RWStructuredBuffer<DrawIndirectArgs> g_indirectInfo : register(u0);
  11. SamplerState g_nearestAnyClampSampler : register(s0);
  12. Texture2D g_depthMap : register(t1);
  13. groupshared U32 s_maxDepth;
  14. [numthreads(THREAD_COUNT_SQRT, THREAD_COUNT_SQRT, 1)] void main(U32 svGroupIndex : SV_GROUPINDEX, UVec3 svGroupThreadId : SV_GROUPTHREADID,
  15. UVec3 svGroupId : SV_GROUPID)
  16. {
  17. // Init the s_maxDepth
  18. if(svGroupIndex == 0u)
  19. {
  20. s_maxDepth = 0u;
  21. }
  22. GroupMemoryBarrierWithGroupSync();
  23. // Project the flare
  24. const U32 flareIdx = svGroupId.x;
  25. const Vec4 posClip = mul(g_mvp, g_flarePositions[flareIdx]);
  26. const Vec3 posNdc = posClip.xyz / posClip.w;
  27. const F32 depth = posNdc.z;
  28. // Compute the UVs to sample the depth map
  29. // Belongs to [-THREAD_COUNT_SQRT, THREAD_COUNT_SQRT]
  30. Vec2 depthMapSize;
  31. g_depthMap.GetDimensions(depthMapSize.x, depthMapSize.y);
  32. const Vec2 displacement = Vec2(svGroupThreadId.xy) - (THREAD_COUNT_SQRT / 2u);
  33. const Vec2 texelSize = 1.0 / depthMapSize;
  34. const Vec2 uv = ndcToUv(posNdc.xy) + displacement * texelSize;
  35. // Sample and store depth
  36. const F32 refDepth = g_depthMap.SampleLevel(g_nearestAnyClampSampler, uv, 0.0).r;
  37. InterlockedMax(s_maxDepth, U32(refDepth * F32(kMaxU32)));
  38. // Sync
  39. GroupMemoryBarrierWithGroupSync();
  40. if(svGroupIndex == 0u)
  41. {
  42. const F32 refDepth2 = F32(s_maxDepth) / F32(kMaxU32);
  43. g_indirectInfo[flareIdx].m_vertexCount = (depth > refDepth2) ? 0u : 4u;
  44. g_indirectInfo[flareIdx].m_instanceCount = 1u;
  45. g_indirectInfo[flareIdx].m_firstVertex = 0u;
  46. g_indirectInfo[flareIdx].m_firstInstance = 0u;
  47. }
  48. }