LensFlareUpdateIndirectInfo.ankiprog 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // Copyright (C) 2009-2021, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. ANKI_SPECIALIZATION_CONSTANT_UVEC2(IN_DEPTH_MAP_SIZE, 0u);
  6. #pragma anki start comp
  7. #include <AnKi/Shaders/Common.glsl>
  8. const U32 WORKGROUP_SIZE = 8u;
  9. layout(local_size_x = WORKGROUP_SIZE, local_size_y = WORKGROUP_SIZE, local_size_z = 1) in;
  10. struct DrawArraysIndirectInfo
  11. {
  12. U32 count;
  13. U32 instanceCount;
  14. U32 first;
  15. U32 baseInstance;
  16. };
  17. layout(set = 0, binding = 0, std430, row_major) readonly buffer ss0_
  18. {
  19. Mat4 u_mvp;
  20. Vec4 u_flarePositions[];
  21. };
  22. layout(set = 0, binding = 1, std430) writeonly buffer ss1_
  23. {
  24. DrawArraysIndirectInfo u_indirectInfo[];
  25. };
  26. layout(set = 0, binding = 2) uniform sampler u_nearestAnyClampSampler;
  27. layout(set = 0, binding = 3) uniform texture2D u_depthMap;
  28. shared U32 s_maxDepth;
  29. void main()
  30. {
  31. // Init the s_maxDepth
  32. if(gl_LocalInvocationIndex == 0u)
  33. {
  34. s_maxDepth = 0u;
  35. }
  36. memoryBarrierShared();
  37. barrier();
  38. // Project the flare
  39. const U32 flareIdx = gl_WorkGroupID.x;
  40. const Vec4 posClip = u_mvp * u_flarePositions[flareIdx];
  41. const Vec3 posNdc = posClip.xyz / posClip.w;
  42. const F32 depth = posNdc.z;
  43. // Compute the UVs to sample the depth map
  44. // Belongs to [-WORKGROUP_SIZE, WORKGROUP_SIZE]
  45. const Vec2 displacement = Vec2(gl_LocalInvocationID.xy) - Vec2(WORKGROUP_SIZE / 2u);
  46. const Vec2 TEXEL_SIZE = 1.0 / Vec2(IN_DEPTH_MAP_SIZE);
  47. const Vec2 uv = NDC_TO_UV(posNdc.xy) + displacement * TEXEL_SIZE;
  48. // Sample and store depth
  49. const F32 refDepth = textureLod(u_depthMap, u_nearestAnyClampSampler, uv, 0.0).r;
  50. atomicMax(s_maxDepth, U32(refDepth * F32(MAX_U32)));
  51. // Sync
  52. memoryBarrierShared();
  53. barrier();
  54. if(gl_LocalInvocationIndex == 0u)
  55. {
  56. const F32 refDepth2 = F32(s_maxDepth) / F32(MAX_U32);
  57. u_indirectInfo[flareIdx].count = (depth > refDepth2) ? 0u : 4u;
  58. u_indirectInfo[flareIdx].instanceCount = 1u;
  59. u_indirectInfo[flareIdx].first = 0u;
  60. u_indirectInfo[flareIdx].baseInstance = 0u;
  61. }
  62. }
  63. #pragma anki end