| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- // Copyright (C) 2009-present, Panagiotis Christopoulos Charitos and contributors.
- // All rights reserved.
- // Code licensed under the BSD License.
- // http://www.anki3d.org/LICENSE
- #pragma anki technique comp
- #include <AnKi/Shaders/Common.hlsl>
- #define THREAD_COUNT_SQRT 8
- ANKI_FAST_CONSTANTS(Mat4, g_mvp)
- StructuredBuffer<Vec4> g_flarePositions : register(t0);
- RWStructuredBuffer<DrawIndirectArgs> g_indirectInfo : register(u0);
- SamplerState g_nearestAnyClampSampler : register(s0);
- Texture2D g_depthMap : register(t1);
- groupshared U32 s_maxDepth;
- [numthreads(THREAD_COUNT_SQRT, THREAD_COUNT_SQRT, 1)] void main(U32 svGroupIndex : SV_GROUPINDEX, UVec3 svGroupThreadId : SV_GROUPTHREADID,
- UVec3 svGroupId : SV_GROUPID)
- {
- // Init the s_maxDepth
- if(svGroupIndex == 0u)
- {
- s_maxDepth = 0u;
- }
- GroupMemoryBarrierWithGroupSync();
- // Project the flare
- const U32 flareIdx = svGroupId.x;
- const Vec4 posClip = mul(g_mvp, g_flarePositions[flareIdx]);
- const Vec3 posNdc = posClip.xyz / posClip.w;
- const F32 depth = posNdc.z;
- // Compute the UVs to sample the depth map
- // Belongs to [-THREAD_COUNT_SQRT, THREAD_COUNT_SQRT]
- Vec2 depthMapSize;
- g_depthMap.GetDimensions(depthMapSize.x, depthMapSize.y);
- const Vec2 displacement = Vec2(svGroupThreadId.xy) - (THREAD_COUNT_SQRT / 2u);
- const Vec2 texelSize = 1.0 / depthMapSize;
- const Vec2 uv = ndcToUv(posNdc.xy) + displacement * texelSize;
- // Sample and store depth
- const F32 refDepth = g_depthMap.SampleLevel(g_nearestAnyClampSampler, uv, 0.0).r;
- InterlockedMax(s_maxDepth, U32(refDepth * F32(kMaxU32)));
- // Sync
- GroupMemoryBarrierWithGroupSync();
- if(svGroupIndex == 0u)
- {
- const F32 refDepth2 = F32(s_maxDepth) / F32(kMaxU32);
- g_indirectInfo[flareIdx].m_vertexCount = (depth > refDepth2) ? 0u : 4u;
- g_indirectInfo[flareIdx].m_instanceCount = 1u;
- g_indirectInfo[flareIdx].m_firstVertex = 0u;
- g_indirectInfo[flareIdx].m_firstInstance = 0u;
- }
- }
|