fixing light culling

This commit is contained in:
Dynamitos
2024-02-23 08:31:21 +01:00
parent 15813ba612
commit a3f5ad2841
8 changed files with 37 additions and 24 deletions
+5 -5
View File
@@ -15,13 +15,13 @@ void computeFrustums(ComputeShaderInput in)
{
float3 origin = float3(0, 0, 0);
float3 corners[4] = {
screenToClip(float3(float2(in.dispatchThreadID.x + 0, in.dispatchThreadID.y + 0) * BLOCK_SIZE, -1.0f)),
screenToClip(float3(float2(in.dispatchThreadID.x + 1, in.dispatchThreadID.y + 0) * BLOCK_SIZE, -1.0f)),
screenToClip(float3(float2(in.dispatchThreadID.x + 0, in.dispatchThreadID.y + 1) * BLOCK_SIZE, -1.0f)),
screenToClip(float3(float2(in.dispatchThreadID.x + 1, in.dispatchThreadID.y + 1) * BLOCK_SIZE, -1.0f))
screenToView(float4(float2(in.dispatchThreadID.x + 0, in.dispatchThreadID.y + 0) * BLOCK_SIZE, -1.0f, 1.0f)).xyz,
screenToView(float4(float2(in.dispatchThreadID.x + 1, in.dispatchThreadID.y + 0) * BLOCK_SIZE, -1.0f, 1.0f)).xyz,
screenToView(float4(float2(in.dispatchThreadID.x + 0, in.dispatchThreadID.y + 1) * BLOCK_SIZE, -1.0f, 1.0f)).xyz,
screenToView(float4(float2(in.dispatchThreadID.x + 1, in.dispatchThreadID.y + 1) * BLOCK_SIZE, -1.0f, 1.0f)).xyz
};
Frustum frustum;
frustum.sides[0] = computePlane(origin, corners[2], corners[1]);
frustum.sides[0] = computePlane(origin, corners[2], corners[0]);
frustum.sides[1] = computePlane(origin, corners[1], corners[3]);
frustum.sides[2] = computePlane(origin, corners[0], corners[1]);
frustum.sides[3] = computePlane(origin, corners[3], corners[2]);
+7 -7
View File
@@ -89,20 +89,20 @@ void cullLights(ComputeShaderInput in)
float fMinDepth = asfloat(uMinDepth);
float fMaxDepth = asfloat(uMaxDepth);
float minDepth = fMinDepth;
float maxDepth = fMaxDepth;
float nearClip = 0.1f;
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)).z;
Plane minPlane = {float3(0, 0, -1), -minDepth};
Plane minPlane = {float3(0, 0, -1), -minDepthVS};
for ( uint i = in.groupIndex; i < pLightEnv.numPointLights; i += BLOCK_SIZE * BLOCK_SIZE )
{
PointLight light = pLightEnv.pointLights[i];
float3 lightClip = light.getClipPosition();
if(light.insideFrustum(groupFrustum, lightClip, nearClip, maxDepth))
float3 light_VS = light.getViewPosition();
if(light.insideFrustum(groupFrustum, light_VS, nearClipVS, maxDepthVS))
{
tAppendLight(i);
if(!light.insidePlane(minPlane, lightClip))
if(!light.insidePlane(minPlane, light_VS))
{
oAppendLight(i);
}
+14 -2
View File
@@ -6,18 +6,30 @@ struct ViewParameter
{
float4x4 viewMatrix;
float4x4 projectionMatrix;
float4x4 inverseProjection;
float4 cameraPos_WS;
float2 screenDimensions;
}
layout(set = 0)
ParameterBlock<ViewParameter> pViewParams;
float3 screenToClip(float3 screen)
float4 clipToView(float4 clip)
{
float4 view = mul(pViewParams.inverseProjection, clip);
view = view / view.w;
return view;
}
float4 screenToView(float4 screen)
{
float2 texCoord = screen.xy / pViewParams.screenDimensions;
// Convert to clip space
return float3( float2( texCoord.x, 1.0f-texCoord.y ) * 2.0f - 1.0f, screen.z);
float4 clip = float4( float2( texCoord.x, 1.0f-texCoord.y ) * 2.0f - 1.0f, screen.z, screen.w);
return clipToView(clip);
}
struct Plane
+8 -8
View File
@@ -32,27 +32,27 @@ struct PointLight : ILightEnv
float illuminance = max(1 - d / colorRange.w, 0);
return illuminance * brdf.evaluate(params.tbn, params.viewDir_TS, -normalize(lightDir_TS), colorRange.xyz);
}
float3 getClipPosition()
float3 getViewPosition()
{
float4 position_CS = mul(pViewParams.projectionMatrix, mul(pViewParams.viewMatrix, position_WS));
return position_CS.xyz / position_CS.w;
float4 position_VS = mul(pViewParams.viewMatrix, position_WS);
return position_VS.xyz / position_VS.w;
}
bool insidePlane(Plane plane, float3 position_CS)
bool insidePlane(Plane plane, float3 position_VS)
{
return dot(plane.n, position_CS) - plane.d < -colorRange.w;
return dot(plane.n, position_VS) - plane.d < -colorRange.w;
}
bool insideFrustum(Frustum frustum, float3 position_CS, float minDepth, float maxDepth)
bool insideFrustum(Frustum frustum, float3 position_VS, float minDepth, float maxDepth)
{
bool result = true;
if(position_CS.z - colorRange.w > minDepth || position_CS.z + colorRange.w < maxDepth)
if(position_VS.z - colorRange.w > minDepth || position_VS.z + colorRange.w < maxDepth)
{
result = false;
}
for(int i = 0; i < 4 && result; ++i)
{
if(insidePlane(frustum.sides[i], position_CS))
if(insidePlane(frustum.sides[i], position_VS))
{
result = false;
}