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
+1 -1
View File
@@ -94,7 +94,7 @@ target_link_libraries(AssetViewer PRIVATE Engine)
target_include_directories(AssetViewer PRIVATE src/AssetViewer) target_include_directories(AssetViewer PRIVATE src/AssetViewer)
if(MSVC) if(MSVC)
set(_CRT_SECURE_NO_WARNINGS) add_definitions(-D_CRT_SECURE_NO_WARNINGS)
target_compile_options(Engine PUBLIC /std:c++20 /Zi /MP14 /W4 /DEBUG /wd4505) target_compile_options(Engine PUBLIC /std:c++20 /Zi /MP14 /W4 /DEBUG /wd4505)
target_compile_options(Editor PUBLIC /std:c++20 /Zi /MP14 /W4 /DEBUG) target_compile_options(Editor PUBLIC /std:c++20 /Zi /MP14 /W4 /DEBUG)
target_sources(Engine INTERFACE target_sources(Engine INTERFACE
+5 -5
View File
@@ -15,13 +15,13 @@ void computeFrustums(ComputeShaderInput in)
{ {
float3 origin = float3(0, 0, 0); float3 origin = float3(0, 0, 0);
float3 corners[4] = { float3 corners[4] = {
screenToClip(float3(float2(in.dispatchThreadID.x + 0, in.dispatchThreadID.y + 0) * BLOCK_SIZE, -1.0f)), screenToView(float4(float2(in.dispatchThreadID.x + 0, in.dispatchThreadID.y + 0) * BLOCK_SIZE, -1.0f, 1.0f)).xyz,
screenToClip(float3(float2(in.dispatchThreadID.x + 1, in.dispatchThreadID.y + 0) * BLOCK_SIZE, -1.0f)), screenToView(float4(float2(in.dispatchThreadID.x + 1, in.dispatchThreadID.y + 0) * BLOCK_SIZE, -1.0f, 1.0f)).xyz,
screenToClip(float3(float2(in.dispatchThreadID.x + 0, in.dispatchThreadID.y + 1) * BLOCK_SIZE, -1.0f)), screenToView(float4(float2(in.dispatchThreadID.x + 0, in.dispatchThreadID.y + 1) * BLOCK_SIZE, -1.0f, 1.0f)).xyz,
screenToClip(float3(float2(in.dispatchThreadID.x + 1, in.dispatchThreadID.y + 1) * BLOCK_SIZE, -1.0f)) screenToView(float4(float2(in.dispatchThreadID.x + 1, in.dispatchThreadID.y + 1) * BLOCK_SIZE, -1.0f, 1.0f)).xyz
}; };
Frustum frustum; 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[1] = computePlane(origin, corners[1], corners[3]);
frustum.sides[2] = computePlane(origin, corners[0], corners[1]); frustum.sides[2] = computePlane(origin, corners[0], corners[1]);
frustum.sides[3] = computePlane(origin, corners[3], corners[2]); 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 fMinDepth = asfloat(uMinDepth);
float fMaxDepth = asfloat(uMaxDepth); float fMaxDepth = asfloat(uMaxDepth);
float minDepth = fMinDepth; float minDepthVS = clipToView(float4(0, 0, fMinDepth, 1)).z;
float maxDepth = fMaxDepth; float maxDepthVS = clipToView(float4(0, 0, fMaxDepth, 1)).z;
float nearClip = 0.1f; 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 ) for ( uint i = in.groupIndex; i < pLightEnv.numPointLights; i += BLOCK_SIZE * BLOCK_SIZE )
{ {
PointLight light = pLightEnv.pointLights[i]; PointLight light = pLightEnv.pointLights[i];
float3 lightClip = light.getClipPosition(); float3 light_VS = light.getViewPosition();
if(light.insideFrustum(groupFrustum, lightClip, nearClip, maxDepth)) if(light.insideFrustum(groupFrustum, light_VS, nearClipVS, maxDepthVS))
{ {
tAppendLight(i); tAppendLight(i);
if(!light.insidePlane(minPlane, lightClip)) if(!light.insidePlane(minPlane, light_VS))
{ {
oAppendLight(i); oAppendLight(i);
} }
+14 -2
View File
@@ -6,18 +6,30 @@ struct ViewParameter
{ {
float4x4 viewMatrix; float4x4 viewMatrix;
float4x4 projectionMatrix; float4x4 projectionMatrix;
float4x4 inverseProjection;
float4 cameraPos_WS; float4 cameraPos_WS;
float2 screenDimensions; float2 screenDimensions;
} }
layout(set = 0) layout(set = 0)
ParameterBlock<ViewParameter> pViewParams; 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; float2 texCoord = screen.xy / pViewParams.screenDimensions;
// Convert to clip space // 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 struct Plane
+8 -8
View File
@@ -32,27 +32,27 @@ struct PointLight : ILightEnv
float illuminance = max(1 - d / colorRange.w, 0); float illuminance = max(1 - d / colorRange.w, 0);
return illuminance * brdf.evaluate(params.tbn, params.viewDir_TS, -normalize(lightDir_TS), colorRange.xyz); 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)); float4 position_VS = mul(pViewParams.viewMatrix, position_WS);
return position_CS.xyz / position_CS.w; 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; 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; result = false;
} }
for(int i = 0; i < 4 && result; ++i) for(int i = 0; i < 4 && result; ++i)
{ {
if(insidePlane(frustum.sides[i], position_CS)) if(insidePlane(frustum.sides[i], position_VS))
{ {
result = false; result = false;
} }
@@ -169,7 +169,6 @@ void BasePass::render()
} }
graphics->executeCommands(commands); graphics->executeCommands(commands);
graphics->endRenderPass(); graphics->endRenderPass();
vkDeviceWaitIdle(((Vulkan::Graphics*)graphics.getHandle())->getDevice());
} }
void BasePass::endFrame() void BasePass::endFrame()
@@ -28,6 +28,7 @@ void RenderPass::beginFrame(const Component::Camera& cam)
viewParams = { viewParams = {
.viewMatrix = cam.getViewMatrix(), .viewMatrix = cam.getViewMatrix(),
.projectionMatrix = viewport->getProjectionMatrix(), .projectionMatrix = viewport->getProjectionMatrix(),
.inverseProjection = glm::inverse(viewport->getProjectionMatrix()),
.cameraPosition = Vector4(cam.getCameraPosition(), 1), .cameraPosition = Vector4(cam.getCameraPosition(), 1),
.screenDimensions = Vector2(static_cast<float>(viewport->getWidth()), static_cast<float>(viewport->getHeight())), .screenDimensions = Vector2(static_cast<float>(viewport->getWidth()), static_cast<float>(viewport->getHeight())),
}; };
@@ -31,6 +31,7 @@ protected:
{ {
Matrix4 viewMatrix; Matrix4 viewMatrix;
Matrix4 projectionMatrix; Matrix4 projectionMatrix;
Matrix4 inverseProjection;
Vector4 cameraPosition; Vector4 cameraPosition;
Vector2 screenDimensions; Vector2 screenDimensions;
} viewParams; } viewParams;