fixing light culling
This commit is contained in:
+1
-1
@@ -94,7 +94,7 @@ target_link_libraries(AssetViewer PRIVATE Engine)
|
||||
target_include_directories(AssetViewer PRIVATE src/AssetViewer)
|
||||
|
||||
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(Editor PUBLIC /std:c++20 /Zi /MP14 /W4 /DEBUG)
|
||||
target_sources(Engine INTERFACE
|
||||
|
||||
@@ -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]);
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -169,7 +169,6 @@ void BasePass::render()
|
||||
}
|
||||
graphics->executeCommands(commands);
|
||||
graphics->endRenderPass();
|
||||
vkDeviceWaitIdle(((Vulkan::Graphics*)graphics.getHandle())->getDevice());
|
||||
}
|
||||
|
||||
void BasePass::endFrame()
|
||||
|
||||
@@ -28,6 +28,7 @@ void RenderPass::beginFrame(const Component::Camera& cam)
|
||||
viewParams = {
|
||||
.viewMatrix = cam.getViewMatrix(),
|
||||
.projectionMatrix = viewport->getProjectionMatrix(),
|
||||
.inverseProjection = glm::inverse(viewport->getProjectionMatrix()),
|
||||
.cameraPosition = Vector4(cam.getCameraPosition(), 1),
|
||||
.screenDimensions = Vector2(static_cast<float>(viewport->getWidth()), static_cast<float>(viewport->getHeight())),
|
||||
};
|
||||
|
||||
@@ -31,6 +31,7 @@ protected:
|
||||
{
|
||||
Matrix4 viewMatrix;
|
||||
Matrix4 projectionMatrix;
|
||||
Matrix4 inverseProjection;
|
||||
Vector4 cameraPosition;
|
||||
Vector2 screenDimensions;
|
||||
} viewParams;
|
||||
|
||||
Reference in New Issue
Block a user