Adding view culling to non-depth culling variant

This commit is contained in:
Dynamitos
2024-06-25 11:21:56 +02:00
parent c352555b0b
commit 00bb6b3a96
10 changed files with 144 additions and 175 deletions
+4 -21
View File
@@ -24,27 +24,8 @@ bool isBoxVisible(AABB bounding)
// now we calculate what mip level we need to only sample up to 4 texels covering the entire meshlet
uint2 screenCornerMin = mipDimensions;
uint2 screenCornerMax = uint2(0, 0);
// we use reverse depth, so higher values are closer
float maxDepth = 0;
{
float4 corners[8];
corners[0] = float4(bounding.min.x, bounding.min.y, bounding.min.z, 1.0f);
corners[1] = float4(bounding.min.x, bounding.min.y, bounding.max.z, 1.0f);
corners[2] = float4(bounding.min.x, bounding.max.y, bounding.min.z, 1.0f);
corners[3] = float4(bounding.min.x, bounding.max.y, bounding.max.z, 1.0f);
corners[4] = float4(bounding.max.x, bounding.min.y, bounding.min.z, 1.0f);
corners[5] = float4(bounding.max.x, bounding.min.y, bounding.max.z, 1.0f);
corners[6] = float4(bounding.max.x, bounding.max.y, bounding.min.z, 1.0f);
corners[7] = float4(bounding.max.x, bounding.max.y, bounding.max.z, 1.0f);
for(uint i = 0; i < 8; ++i)
{
float4 clipCorner = mul(modelViewProjection, corners[i]);
float4 screenCorner = clipToScreen(clipCorner);
screenCornerMin = uint2(min(screenCornerMin.x, uint(screenCorner.x)), min(screenCornerMin.y, uint(screenCorner.y)));
screenCornerMax = uint2(max(screenCornerMax.x, uint(screenCorner.x)), max(screenCornerMax.y, uint(screenCorner.y)));
maxDepth = max(maxDepth, screenCorner.z);
}
}
// lower values are closer
float maxDepth = bounding.projectScreenDepth(modelViewProjection, screenCornerMin, screenCornerMax);
uint mipOffset = 0;
// in theory this wouldnt work if no corner was in screen, as min would be greater that max, however we verified that with view culling
while(screenCornerMax.x - screenCornerMin.x > 1 || screenCornerMax.y - screenCornerMin.y > 1)
@@ -118,8 +99,10 @@ void taskMain(
// if the meshlet is outside of the frustum, we skip it since we cant do depth culling anyways
if(meshlet.bounding.insideFrustum(viewFrustum))
{
#ifdef DEPTH_CULLING
// if the meshlet bounding box is behind the cached depth buffer, we skip
if(isBoxVisible(meshlet.bounding))
#endif
{
uint index;
InterlockedAdd(head, 1, index);
-1
View File
@@ -62,5 +62,4 @@ void initialReduce(
int2 texCoord = groupOffset + threadID.xy;
float fDepth = pDepthAttachment.texture.Load(int3(texCoord, 0)).r;
pDepthAttachment.buffer[texCoord.x + (texCoord.y * width)] = fDepth;
}
-3
View File
@@ -26,10 +26,7 @@ void taskMain(
uint cull = p.cullingOffset + i;
MeshletDescription meshlet = pScene.meshletInfos[m];
MeshletCullingInfo culling = pScene.cullingInfos[cull];
#ifdef DEPTH_CULLING
// if depth culling is disabled, we draw the whole scene as if cached
if(culling.wasVisible())
#endif
{
uint index;
InterlockedAdd(head, 1, index);
+27 -5
View File
@@ -16,7 +16,7 @@ struct BoundingSphere
bool result = true;
for(int i = 0; i < 4 && result; ++i)
{
if(dot(frustum.sides[i].n, centerRadius.xyz) - frustum.sides[i].d < -getRadius())
if(dot(frustum.sides[i].n, getCenter()) - frustum.sides[i].d < -getRadius())
{
result = false;
}
@@ -27,15 +27,15 @@ struct BoundingSphere
struct AABB
{
float3 min;
float3 minCorner;
float pad0;
float3 max;
float3 maxCorner;
float pad1;
// modified version from https://learnopengl.com/Guest-Articles/2021/Scene/Frustum-Culling
bool insideFrustum(Frustum frustum)
{
float3 center = (min + max) * 0.5;
float3 extents = float3(max.x - center.x, max.y - center.y, max.z - center.z);
float3 center = (minCorner + maxCorner) * 0.5;
float3 extents = float3(maxCorner.x - center.x, maxCorner.y - center.y, maxCorner.z - center.z);
bool result = true;
for(int i = 0; i < 4 && result; ++i)
{
@@ -51,4 +51,26 @@ struct AABB
}
return result;
}
float projectScreenDepth(float4x4 mvp, inout uint2 screenCornerMin, inout uint2 screenCornerMax)
{
float maxDepth = 0;
float4 corners[8];
corners[0] = float4(minCorner.x, minCorner.y, minCorner.z, 1.0f);
corners[1] = float4(minCorner.x, minCorner.y, maxCorner.z, 1.0f);
corners[2] = float4(minCorner.x, maxCorner.y, minCorner.z, 1.0f);
corners[3] = float4(minCorner.x, maxCorner.y, maxCorner.z, 1.0f);
corners[4] = float4(maxCorner.x, minCorner.y, minCorner.z, 1.0f);
corners[5] = float4(maxCorner.x, minCorner.y, maxCorner.z, 1.0f);
corners[6] = float4(maxCorner.x, maxCorner.y, minCorner.z, 1.0f);
corners[7] = float4(maxCorner.x, maxCorner.y, maxCorner.z, 1.0f);
for(uint i = 0; i < 8; ++i)
{
float4 clipCorner = mul(mvp, corners[i]);
float4 screenCorner = clipToScreen(clipCorner);
screenCornerMin = uint2(min(screenCornerMin.x, uint(screenCorner.x)), min(screenCornerMin.y, uint(screenCorner.y)));
screenCornerMax = uint2(max(screenCornerMax.x, uint(screenCorner.x)), max(screenCornerMax.y, uint(screenCorner.y)));
maxDepth = max(maxDepth, screenCorner.z);
}
return maxDepth;
}
};