Files

135 lines
3.6 KiB
Plaintext
Raw Permalink Normal View History

2020-06-02 11:46:18 +02:00
import Common;
2023-11-11 13:56:12 +01:00
import DispatchParams;
2023-11-05 10:36:01 +01:00
import LightEnv;
2026-03-15 22:37:08 +01:00
import Frustum;
2020-06-02 11:46:18 +02:00
struct ComputeShaderInput
{
2021-11-01 20:25:16 +01:00
uint3 groupID : SV_GroupID;
uint3 dispatchThreadID : SV_DispatchThreadID;
uint groupIndex : SV_GroupIndex;
2020-06-02 11:46:18 +02:00
};
2023-10-26 18:37:29 +02:00
struct CullingParams
2020-06-02 11:46:18 +02:00
{
2024-02-02 11:39:40 +01:00
Texture2D depthTexture;
2020-06-02 11:46:18 +02:00
2023-10-26 18:37:29 +02:00
globallycoherent RWStructuredBuffer<uint> oLightIndexCounter;
globallycoherent RWStructuredBuffer<uint> tLightIndexCounter;
2020-06-02 11:46:18 +02:00
2023-10-26 18:37:29 +02:00
RWStructuredBuffer<uint> oLightIndexList;
RWStructuredBuffer<uint> tLightIndexList;
2022-02-24 22:38:26 +01:00
2023-10-26 18:37:29 +02:00
RWTexture2D<uint2> oLightGrid;
RWTexture2D<uint2> tLightGrid;
};
2024-04-23 19:11:06 +02:00
2023-11-08 23:27:21 +01:00
ParameterBlock<CullingParams> pCullingParams;
2022-02-24 22:38:26 +01:00
2020-06-02 11:46:18 +02:00
groupshared uint uMinDepth;
groupshared uint uMaxDepth;
groupshared Frustum groupFrustum;
groupshared uint oLightCount;
groupshared uint oLightIndexStartOffset;
groupshared uint oLightList[1024];
groupshared uint tLightCount;
groupshared uint tLightIndexStartOffset;
groupshared uint tLightList[1024];
void oAppendLight(uint lightIndex)
{
2021-11-01 20:25:16 +01:00
uint index;
InterlockedAdd(oLightCount, 1, index);
if(index < 1024)
{
oLightList[index] = lightIndex;
}
2020-06-02 11:46:18 +02:00
}
void tAppendLight(uint lightIndex)
{
2021-11-01 20:25:16 +01:00
uint index;
InterlockedAdd(tLightCount, 1, index);
if(index < 1024)
{
tLightList[index] = lightIndex;
}
2020-06-02 11:46:18 +02:00
}
[numthreads(BLOCK_SIZE, BLOCK_SIZE, 1)]
2021-05-10 23:57:55 +02:00
[shader("compute")]
2020-06-02 11:46:18 +02:00
void cullLights(ComputeShaderInput in)
{
int2 texCoord = int2(in.dispatchThreadID.xy);
2024-02-02 11:39:40 +01:00
float fDepth = pCullingParams.depthTexture.Load(int3(texCoord, 0)).r;
2020-06-02 11:46:18 +02:00
2021-11-01 20:25:16 +01:00
uint uDepth = asuint(fDepth);
if(in.groupIndex == 0)
{
uMinDepth = 0xffffffff;
uMaxDepth = 0x0;
oLightCount = 0;
tLightCount = 0;
2024-10-01 16:56:04 +02:00
groupFrustum = pDispatchParams.frustums[in.groupID.x + (in.groupID.y * pDispatchParams.numThreadGroups.x)];
2021-11-01 20:25:16 +01:00
}
2020-06-02 11:46:18 +02:00
2021-11-01 20:25:16 +01:00
GroupMemoryBarrierWithGroupSync();
2022-02-24 22:38:26 +01:00
2021-11-01 20:25:16 +01:00
InterlockedMin(uMinDepth, uDepth);
InterlockedMax(uMaxDepth, uDepth);
2020-06-02 11:46:18 +02:00
2021-11-01 20:25:16 +01:00
GroupMemoryBarrierWithGroupSync();
2020-06-02 11:46:18 +02:00
2021-11-01 20:25:16 +01:00
float fMinDepth = asfloat(uMinDepth);
float fMaxDepth = asfloat(uMaxDepth);
2020-06-02 11:46:18 +02:00
2024-05-29 10:40:35 +02:00
float minDepthWS = clipToWorld(float4(0, 0, fMinDepth, 1)).z;
float maxDepthWS = clipToWorld(float4(0, 0, fMaxDepth, 1)).z;
float nearClipWS = clipToWorld(float4(0, 0, 0, 1)).z;
2020-06-02 11:46:18 +02:00
Plane maxPlane = {float4(0, 0, -1, -maxDepthWS)};
2020-06-02 11:46:18 +02:00
2023-11-08 23:27:21 +01:00
for ( uint i = in.groupIndex; i < pLightEnv.numPointLights; i += BLOCK_SIZE * BLOCK_SIZE )
2021-11-01 20:25:16 +01:00
{
2023-11-08 23:27:21 +01:00
PointLight light = pLightEnv.pointLights[i];
2024-05-29 10:40:35 +02:00
#ifdef LIGHT_CULLING
2024-06-17 16:21:41 +02:00
if(light.insideFrustum(groupFrustum, light.getPosition(), nearClipWS, minDepthWS))
2024-05-29 10:40:35 +02:00
#endif
2021-11-01 20:25:16 +01:00
{
tAppendLight(i);
2024-05-29 10:40:35 +02:00
#ifdef LIGHT_CULLING
2024-06-17 16:21:41 +02:00
if(!light.insidePlane(maxPlane, light.getPosition()))
2024-05-29 10:40:35 +02:00
#endif
2021-11-01 20:25:16 +01:00
{
oAppendLight(i);
}
}
2020-06-02 11:46:18 +02:00
}
2024-05-29 10:40:35 +02:00
GroupMemoryBarrierWithGroupSync();
2021-11-01 20:25:16 +01:00
if(in.groupIndex == 0)
{
2023-11-08 23:27:21 +01:00
InterlockedAdd(pCullingParams.oLightIndexCounter[0], oLightCount, oLightIndexStartOffset);
pCullingParams.oLightGrid[in.groupID.xy] = uint2(oLightIndexStartOffset, oLightCount);
2021-11-01 20:25:16 +01:00
2023-11-08 23:27:21 +01:00
InterlockedAdd(pCullingParams.tLightIndexCounter[0], tLightCount, tLightIndexStartOffset);
pCullingParams.tLightGrid[in.groupID.xy] = uint2(tLightIndexStartOffset, tLightCount);
2021-11-01 20:25:16 +01:00
}
GroupMemoryBarrierWithGroupSync();
2022-02-24 22:38:26 +01:00
for (uint j = in.groupIndex; j < oLightCount; j += BLOCK_SIZE * BLOCK_SIZE)
2021-11-01 20:25:16 +01:00
{
2023-11-08 23:27:21 +01:00
pCullingParams.oLightIndexList[oLightIndexStartOffset + j] = oLightList[j];
2021-11-01 20:25:16 +01:00
}
// For transparent geometry.
2022-02-24 22:38:26 +01:00
for ( uint k = in.groupIndex; k < tLightCount; k += BLOCK_SIZE * BLOCK_SIZE )
2021-11-01 20:25:16 +01:00
{
2023-11-08 23:27:21 +01:00
pCullingParams.tLightIndexList[tLightIndexStartOffset + k] = tLightList[k];
2021-11-01 20:25:16 +01:00
}
}