Files
Seele/res/shaders/LightCulling.slang
T

140 lines
3.5 KiB
Plaintext
Raw Normal View History

2020-06-02 11:46:18 +02:00
import LightEnv;
import Common;
struct ComputeShaderInput
{
uint3 groupID : SV_GroupID;
uint3 groupThreadID : SV_GroupThreadID;
uint3 dispatchThreadID : SV_DispatchThreadID;
uint groupIndex : SV_GroupIndex;
};
2021-05-10 23:57:55 +02:00
layout(set = INDEX_VIEW_PARAMS, binding = 1)
2020-06-02 11:46:18 +02:00
cbuffer DispatchParams
{
uint3 numThreadGroups;
uint pad0;
uint3 numThreads;
uint pad1;
}
2021-05-10 23:57:55 +02:00
layout(set = INDEX_VIEW_PARAMS, binding = 2)
Texture2D depthTextureVS;
layout(set = INDEX_VIEW_PARAMS, binding = 3)
2020-06-02 11:46:18 +02:00
StructuredBuffer<Frustum> frustums;
2021-05-10 23:57:55 +02:00
layout(set = INDEX_VIEW_PARAMS, binding = 4)
2020-06-02 11:46:18 +02:00
RWStructuredBuffer<uint> oLightIndexCounter;
2021-05-10 23:57:55 +02:00
layout(set = INDEX_VIEW_PARAMS, binding = 5)
2020-06-02 11:46:18 +02:00
RWStructuredBuffer<uint> tLightIndexCounter;
2021-05-10 23:57:55 +02:00
layout(set = INDEX_VIEW_PARAMS, binding = 6)
2020-06-02 11:46:18 +02:00
RWStructuredBuffer<uint> oLightIndexList;
2021-05-10 23:57:55 +02:00
layout(set = INDEX_VIEW_PARAMS, binding = 7)
2020-06-02 11:46:18 +02:00
RWStructuredBuffer<uint> tLightIndexList;
2021-05-10 23:57:55 +02:00
layout(set = INDEX_VIEW_PARAMS, binding = 8)
2020-06-02 11:46:18 +02:00
RWTexture2D<uint2> oLightGrid;
2021-05-10 23:57:55 +02:00
layout(set = INDEX_VIEW_PARAMS, binding = 9)
2020-06-02 11:46:18 +02:00
RWTexture2D<uint2> tLightGrid;
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)
{
uint index;
InterlockedAdd(oLightCount, 1, index);
if(index < 1024)
{
oLightList[index] = lightIndex;
}
}
void tAppendLight(uint lightIndex)
{
uint index;
InterlockedAdd(tLightCount, 1, index);
if(index < 1024)
{
tLightList[index] = lightIndex;
}
}
[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)
{
2021-05-10 23:57:55 +02:00
int3 texCoord = int3(in.dispatchThreadID.xy, 0);
2020-06-02 11:46:18 +02:00
float fDepth = depthTextureVS.Load(texCoord).r;
uint uDepth = asuint(fDepth);
if(in.groupIndex == 0)
{
uMinDepth = 0xffffffff;
uMaxDepth = 0x0;
oLightCount = 0;
tLightCount = 0;
groupFrustum = frustums[in.groupID.x + (in.groupID.y * numThreadGroups.x)];
}
GroupMemoryBarrierWithGroupSync();
InterlockedMin(uMinDepth, uDepth);
InterlockedMax(uMaxDepth, uDepth);
GroupMemoryBarrierWithGroupSync();
float fMinDepth = asfloat(uMinDepth);
float fMaxDepth = asfloat(uMaxDepth);
float minDepthVS = clipToView(float4(0, 0, fMinDepth, 1)).z;
float maxDepthVS = clipToView(float4(0, 0, fMaxDepth, 1)).z;
float nearClipVS = clipToView(float4(0, 0, 0, 1.0f)).z;
Plane minPlane = {float3(0, 0, -1), -minDepthVS};
2021-06-12 18:51:29 +02:00
for ( uint i = in.groupIndex; i < numPointLights; i += BLOCK_SIZE * BLOCK_SIZE )
2020-06-02 11:46:18 +02:00
{
2021-06-12 18:51:29 +02:00
PointLight light = pointLights[i];
2021-07-11 22:23:01 +02:00
if(light.insideFrustum(groupFrustum, nearClipVS, maxDepthVS))
2020-06-02 11:46:18 +02:00
{
2021-06-12 18:51:29 +02:00
tAppendLight(i);
2021-07-11 22:23:01 +02:00
if(!light.insidePlane(minPlane))
2021-06-12 18:51:29 +02:00
{
oAppendLight(i);
}
2020-06-02 11:46:18 +02:00
}
}
GroupMemoryBarrierWithGroupSync();
if(in.groupIndex == 0)
{
InterlockedAdd(oLightIndexCounter[0], (uint)oLightCount, oLightIndexStartOffset);
oLightGrid[in.groupID.xy] = uint2(oLightIndexStartOffset, oLightCount);
InterlockedAdd(tLightIndexCounter[0], (uint)tLightCount, tLightIndexStartOffset);
tLightGrid[in.groupID.xy] = uint2(tLightIndexStartOffset, tLightCount);
}
GroupMemoryBarrierWithGroupSync();
for (uint j = in.groupIndex; j < (uint)oLightCount; j += BLOCK_SIZE * BLOCK_SIZE)
{
oLightIndexList[oLightIndexStartOffset + j] = oLightList[j];
}
2020-06-02 11:46:18 +02:00
// For transparent geometry.
for ( uint k = in.groupIndex; k < (uint)tLightCount; k += BLOCK_SIZE * BLOCK_SIZE )
{
tLightIndexList[tLightIndexStartOffset + k] = tLightList[k];
}
}