View culling is stupid

This commit is contained in:
Dynamitos
2024-10-22 13:31:58 +02:00
parent bbb774d9df
commit 72e60cfe30
11 changed files with 118 additions and 76 deletions
+3 -3
View File
@@ -90,7 +90,7 @@ void taskMain(
p.instanceId = pOffsets.instanceOffset + groupID; p.instanceId = pOffsets.instanceOffset + groupID;
p.meshletOffset = mesh.meshletOffset; p.meshletOffset = mesh.meshletOffset;
p.cullingOffset = pScene.cullingOffsets[p.instanceId]; p.cullingOffset = pScene.cullingOffsets[p.instanceId];
modelViewProjection = mul(mul(pViewParams.projectionMatrix, pViewParams.viewMatrix), instance.transformMatrix); modelViewProjection = mul(pViewParams.viewProjectionMatrix, instance.transformMatrix);
float3 origin = viewToModel(instance.inverseTransformMatrix, float4(0, 0, 0, 1)).xyz; float3 origin = viewToModel(instance.inverseTransformMatrix, float4(0, 0, 0, 1)).xyz;
const float offset = 0.0f; const float offset = 0.0f;
float3 corners[4] = { float3 corners[4] = {
@@ -103,8 +103,8 @@ void taskMain(
viewFrustum.sides[1] = computePlane(origin, corners[1], corners[3]); viewFrustum.sides[1] = computePlane(origin, corners[1], corners[3]);
viewFrustum.sides[2] = computePlane(origin, corners[0], corners[1]); viewFrustum.sides[2] = computePlane(origin, corners[0], corners[1]);
viewFrustum.sides[3] = computePlane(origin, corners[3], corners[2]); viewFrustum.sides[3] = computePlane(origin, corners[3], corners[2]);
//meshVisible = mesh.bounding.insideFrustum(viewFrustum) && isBoxVisible(mesh.bounding); meshVisible = mesh.bounding.insideFrustum(viewFrustum) && isBoxVisible(mesh.bounding);
meshVisible = true; //meshVisible = true;
} }
GroupMemoryBarrierWithGroupSync(); GroupMemoryBarrierWithGroupSync();
if(!meshVisible) if(!meshVisible)
+4 -4
View File
@@ -310,9 +310,9 @@ void EvaluateLEB(uint currentID : SV_DispatchThreadID, uint groupIndex: SV_Group
EvaluateElementPosition(cHeapID, 0, pParams.geometry.baseDepth, pParams.currentVertexBuffer, parentTri, childTri); EvaluateElementPosition(cHeapID, 0, pParams.geometry.baseDepth, pParams.currentVertexBuffer, parentTri, childTri);
// Export the child // Export the child
pParams.lebPositionBuffer[3 * currentID] = float4((childTri.p[0]), 1.0f); pParams.lebPositionBuffer[3 * currentID + 0] = float4(1000 * (childTri.p[0]), 1.0f);
pParams.lebPositionBuffer[3 * currentID + 1] = float4((childTri.p[1]), 1.0f); pParams.lebPositionBuffer[3 * currentID + 1] = float4(1000 * (childTri.p[1]), 1.0f);
pParams.lebPositionBuffer[3 * currentID + 2] = float4((childTri.p[2]), 1.0f); pParams.lebPositionBuffer[3 * currentID + 2] = float4(1000 * (childTri.p[2]), 1.0f);
// Export the fourth element // Export the fourth element
if (pParams.geometry.baseDepth < depth) if (pParams.geometry.baseDepth < depth)
@@ -321,6 +321,6 @@ void EvaluateLEB(uint currentID : SV_DispatchThreadID, uint groupIndex: SV_Group
const uint parentOffset = 3 * pParams.geometry.totalNumElements; const uint parentOffset = 3 * pParams.geometry.totalNumElements;
// Transform the coordinate to planet space // Transform the coordinate to planet space
pParams.lebPositionBuffer[parentOffset + currentID] = float4((cHeapID % 2 == 0 ? parentTri.p[0] : parentTri.p[2]), 1.0f); pParams.lebPositionBuffer[parentOffset + currentID] = float4(1000 * (cHeapID % 2 == 0 ? parentTri.p[0] : parentTri.p[2]), 1.0f);
} }
} }
+1 -1
View File
@@ -84,7 +84,7 @@ struct ComputeParams
// 21 // 21
RWStructuredBuffer<float4> lebPositionBuffer; RWStructuredBuffer<float4> lebPositionBuffer;
// 22 // 22
StructuredBuffer<float3x3> lebMatrixCache; StructuredBuffer<float> lebMatrixCache;
// 23 // 23
globallycoherent RWStructuredBuffer<DebugStruct> debugBuffer; globallycoherent RWStructuredBuffer<DebugStruct> debugBuffer;
}; };
+7 -5
View File
@@ -2,6 +2,7 @@ import Common;
import Bisector; import Bisector;
import CBT; import CBT;
import Parameters; import Parameters;
import Bounding;
// Needs to be defined before including update_utilities // Needs to be defined before including update_utilities
struct BisectorGeometry struct BisectorGeometry
@@ -43,10 +44,10 @@ bool FrustumAABBIntersect(in Frustum frustum, float3 aabbMin, float3 aabbMax)
int ClassifyBisector(in BisectorGeometry tri, uint depth) int ClassifyBisector(in BisectorGeometry tri, uint depth)
{ {
// Check the triangle's visibility // Check the triangle's visibility
float3 triNormal = normalize(cross(tri.p[2] - tri.p[1], tri.p[0] - tri.p[1])); float3 triNormal = normalize(cross(tri.p[2] - tri.p[0], tri.p[1] - tri.p[0]));
float3 triCenter = (tri.p[0] + tri.p[1] + tri.p[2]) / 3.0; float3 triCenter = (tri.p[0] + tri.p[1] + tri.p[2]) / 3.0;
float3 viewDir = normalize(-triCenter); float3 viewDir = normalize(-triCenter);
float FdotV = dot(viewDir, pViewParams.cameraForward_WS.xyz); float FdotV = dot(viewDir, -pViewParams.cameraForward_WS.xyz);
float VdotN = dot(viewDir, triNormal); float VdotN = dot(viewDir, triNormal);
// Here we don't use 0 as it introduces stability issues at grazing angles // Here we don't use 0 as it introduces stability issues at grazing angles
@@ -54,11 +55,12 @@ int ClassifyBisector(in BisectorGeometry tri, uint depth)
return BACK_FACE_CULLED; return BACK_FACE_CULLED;
// Compute the triangle's AABB // Compute the triangle's AABB
float3 aabbMin = float3(min(min(tri.p[0].x, tri.p[1].x), tri.p[2].x), min(min(tri.p[0].y, tri.p[1].y), tri.p[2].y), min(min(tri.p[0].z, tri.p[1].z), tri.p[2].z)); AABB aabb;
float3 aabbMax = float3(max(max(tri.p[0].x, tri.p[1].x), tri.p[2].x), max(max(tri.p[0].y, tri.p[1].y), tri.p[2].y), max(max(tri.p[0].z, tri.p[1].z), tri.p[2].z)); aabb.minCorner = float3(min(min(tri.p[0].x, tri.p[1].x), tri.p[2].x), min(min(tri.p[0].y, tri.p[1].y), tri.p[2].y), min(min(tri.p[0].z, tri.p[1].z), tri.p[2].z));
aabb.maxCorner = float3(max(max(tri.p[0].x, tri.p[1].x), tri.p[2].x), max(max(tri.p[0].y, tri.p[1].y), tri.p[2].y), max(max(tri.p[0].z, tri.p[1].z), tri.p[2].z));
// First we do a frustum culling pass // First we do a frustum culling pass
if (!FrustumAABBIntersect(pViewParams.viewFrustum, aabbMin, aabbMax)) if (!FrustumAABBIntersect(pViewParams.viewFrustum, aabb.minCorner, aabb.maxCorner))
return FRUSTUM_CULLED; return FRUSTUM_CULLED;
// Project the points on screen // Project the points on screen
+1 -1
View File
@@ -558,7 +558,7 @@ void MeshLoader::loadGlobalMeshes(const aiScene* scene, const Array<PMaterialIns
globalMeshes[meshIndex]->blas = graphics->createBottomLevelAccelerationStructure(Gfx::BottomLevelASCreateInfo{ globalMeshes[meshIndex]->blas = graphics->createBottomLevelAccelerationStructure(Gfx::BottomLevelASCreateInfo{
.mesh = globalMeshes[meshIndex], .mesh = globalMeshes[meshIndex],
}); });
vertexData->registerBottomLevelAccelerationStructure(globalMeshes[meshIndex]->blas); //vertexData->registerBottomLevelAccelerationStructure(globalMeshes[meshIndex]->blas);
}); });
} }
getThreadPool().runAndWait(std::move(work)); getThreadPool().runAndWait(std::move(work));
+4 -4
View File
@@ -109,10 +109,10 @@ int main() {
//AssetImporter::importTexture(TextureImportArgs{ //AssetImporter::importTexture(TextureImportArgs{
// .filePath = sourcePath / "import/textures/wgen.png", // .filePath = sourcePath / "import/textures/wgen.png",
//}); //});
// AssetImporter::importMesh(MeshImportArgs{ AssetImporter::importMesh(MeshImportArgs{
// .filePath = sourcePath / "import/models/after-the-rain-vr-sound/source/Whitechapel.glb", .filePath = sourcePath / "import/models/after-the-rain-vr-sound/source/Whitechapel.glb",
// .importPath = "Whitechapel", .importPath = "Whitechapel",
// }); });
// AssetImporter::importMesh(MeshImportArgs{521 // AssetImporter::importMesh(MeshImportArgs{521
// .filePath = sourcePath / "import/models/city-suburbs/source/city-suburbs.obj", // .filePath = sourcePath / "import/models/city-suburbs/source/city-suburbs.obj",
// .importPath = "suburbs", // .importPath = "suburbs",
+1 -1
View File
@@ -48,6 +48,6 @@ void Camera::buildViewMatrix() {
Vector lookAt = eyePos + getTransform().getForward(); Vector lookAt = eyePos + getTransform().getForward();
viewMatrix = glm::lookAt(eyePos, lookAt, Vector(0, 1, 0)); viewMatrix = glm::lookAt(eyePos, lookAt, Vector(0, 1, 0));
cameraPos = eyePos; cameraPos = eyePos;
cameraForward = -getTransform().getForward(); cameraForward = getTransform().getForward();
bNeedsViewBuild = false; bNeedsViewBuild = false;
} }
+59 -45
View File
@@ -23,59 +23,76 @@ struct Halfedge {
}; };
Array<Halfedge> edges = { Array<Halfedge> edges = {
Halfedge{0, 1, 2, 4294967295}, Halfedge{0, 1, 2, 4294967295},
Halfedge{1, 2, 0, 3}, Halfedge{4, 2, 0, 3},
Halfedge{4, 0, 1, 4294967295}, Halfedge{1, 0, 1, 4294967295},
Halfedge{4, 4, 5, 1}, Halfedge{4, 4, 5, 1},
Halfedge{1, 5, 3, 8}, Halfedge{5, 5, 3, 8},
Halfedge{5, 3, 4, 18}, Halfedge{1, 3, 4, 18},
Halfedge{1, 7, 8, 4294967295}, Halfedge{1, 7, 8, 4294967295},
Halfedge{2, 8, 6, 9}, Halfedge{5, 8, 6, 9},
Halfedge{5, 6, 7, 4}, Halfedge{2, 6, 7, 4},
Halfedge{5, 10, 11, 7}, Halfedge{5, 10, 11, 7},
Halfedge{2, 11, 9, 14}, Halfedge{6, 11, 9, 14},
Halfedge{6, 9, 10, 24}, Halfedge{2, 9, 10, 24},
Halfedge{2, 13, 14, 4294967295}, Halfedge{2, 13, 14, 4294967295},
Halfedge{3, 14, 12, 15}, Halfedge{6, 14, 12, 15},
Halfedge{6, 12, 13, 10}, Halfedge{3, 12, 13, 10},
Halfedge{6, 16, 17, 13}, Halfedge{6, 16, 17, 13},
Halfedge{3, 17, 15, 4294967295}, Halfedge{7, 17, 15, 4294967295},
Halfedge{7, 15, 16, 30}, Halfedge{3, 15, 16, 30},
Halfedge{4, 19, 20, 5}, Halfedge{4, 19, 20, 5},
Halfedge{5, 20, 18, 21}, Halfedge{8, 20, 18, 21},
Halfedge{8, 18, 19, 4294967295}, Halfedge{5, 18, 19, 4294967295},
Halfedge{8, 22, 23, 19}, Halfedge{8, 22, 23, 19},
Halfedge{5, 23, 21, 26}, Halfedge{9, 23, 21, 26},
Halfedge{9, 21, 22, 36}, Halfedge{5, 21, 22, 36},
Halfedge{5, 25, 26, 11}, Halfedge{5, 25, 26, 11},
Halfedge{6, 26, 24, 27}, Halfedge{9, 26, 24, 27},
Halfedge{9, 24, 25, 22}, Halfedge{6, 24, 25, 22},
Halfedge{9, 28, 29, 25}, Halfedge{9, 28, 29, 25},
Halfedge{6, 29, 27, 32}, Halfedge{10, 29, 27, 32},
Halfedge{10, 27, 28, 42}, Halfedge{6, 27, 28, 42},
Halfedge{6, 31, 32, 17}, Halfedge{6, 31, 32, 17},
Halfedge{7, 32, 30, 33}, Halfedge{10, 32, 30, 33},
Halfedge{10, 30, 31, 28}, Halfedge{7, 30, 31, 28},
Halfedge{10, 34, 35, 31}, Halfedge{10, 34, 35, 31},
Halfedge{7, 35, 33, 4294967295}, Halfedge{11, 35, 33, 4294967295},
Halfedge{11, 33, 34, 48}, Halfedge{7, 33, 34, 48},
Halfedge{8, 37, 38, 23}, Halfedge{8, 37, 38, 23},
Halfedge{9, 38, 36, 39}, Halfedge{12, 38, 36, 39},
Halfedge{12, 36, 37, 4294967295}, Halfedge{9, 36, 37, 4294967295},
Halfedge{12, 40, 41, 37}, Halfedge{12, 40, 41, 37},
Halfedge{9, 41, 39, 44}, Halfedge{13, 41, 39, 44},
Halfedge{13, 39, 40, 4294967295}, Halfedge{9, 39, 40, 4294967295},
Halfedge{9, 43, 44, 29}, Halfedge{9, 43, 44, 29},
Halfedge{10, 44, 42, 45}, Halfedge{13, 44, 42, 45},
Halfedge{13, 42, 43, 40}, Halfedge{10, 42, 43, 40},
Halfedge{13, 46, 47, 43}, Halfedge{13, 46, 47, 43},
Halfedge{10, 47, 45, 50}, Halfedge{14, 47, 45, 50},
Halfedge{14, 45, 46, 4294967295}, Halfedge{10, 45, 46, 4294967295},
Halfedge{10, 49, 50, 35}, Halfedge{10, 49, 50, 35},
Halfedge{11, 50, 48, 51}, Halfedge{14, 50, 48, 51},
Halfedge{14, 48, 49, 46}, Halfedge{11, 48, 49, 46},
Halfedge{14, 52, 53, 49}, Halfedge{14, 52, 53, 49},
Halfedge{11, 53, 51, 4294967295}, Halfedge{15, 53, 51, 4294967295},
Halfedge{15, 51, 52, 4294967295}, Halfedge{11, 51, 52, 4294967295},
// Halfedge{1, 1, 2, 5}, Halfedge{2, 2, 0, 36}, Halfedge{6, 0, 1, 39}, Halfedge{1, 4, 5, 51}, Halfedge{7, 5, 3, 48}, // Halfedge{1, 1, 2, 5}, Halfedge{2, 2, 0, 36}, Halfedge{6, 0, 1, 39}, Halfedge{1, 4, 5, 51}, Halfedge{7, 5, 3, 48},
// Halfedge{2, 3, 4, 0}, Halfedge{3, 7, 8, 9}, Halfedge{4, 8, 6, 45}, Halfedge{5, 6, 7, 42}, Halfedge{4, 10, 11, 6}, // Halfedge{2, 3, 4, 0}, Halfedge{3, 7, 8, 9}, Halfedge{4, 8, 6, 45}, Halfedge{5, 6, 7, 42}, Halfedge{4, 10, 11, 6},
@@ -150,7 +167,7 @@ Matrix3 splittingMatrix(uint32 bitValue) {
float b = float(bitValue); float b = float(bitValue);
float c = 1.0f - b; float c = 1.0f - b;
return Matrix3({ return glm::transpose(Matrix3({
0.0f, 0.0f,
b, b,
c, c,
@@ -160,7 +177,7 @@ Matrix3 splittingMatrix(uint32 bitValue) {
b, b,
c, c,
0.0f, 0.0f,
}); }));
} }
Matrix3 decodeSubdivisionMatrix(uint64 heapID) { Matrix3 decodeSubdivisionMatrix(uint64 heapID) {
@@ -175,19 +192,16 @@ Matrix3 decodeSubdivisionMatrix(uint64 heapID) {
void LebMatrixCache::init(Gfx::PGraphics graphics, uint32 depth) { void LebMatrixCache::init(Gfx::PGraphics graphics, uint32 depth) {
cacheDepth = depth; cacheDepth = depth;
uint32 matrixCount = 2ULL << cacheDepth; uint32 matrixCount = 2ULL << cacheDepth;
struct PaddedMatrix {
Matrix3 m; Matrix3 m;
uint8 pad[12]; std::vector<Matrix3> table(matrixCount);
}; table[0] = Matrix3({1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f});
std::vector<PaddedMatrix> table(matrixCount);
table[0].m = Matrix3({1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f});
for (uint64 heapID = 1ULL; heapID < (2ULL << cacheDepth); ++heapID) { for (uint64 heapID = 1ULL; heapID < (2ULL << cacheDepth); ++heapID) {
table[heapID].m = decodeSubdivisionMatrix(heapID); table[heapID] = decodeSubdivisionMatrix(heapID);
} }
lebMatrixBuffer = graphics->createShaderBuffer(ShaderBufferCreateInfo{ lebMatrixBuffer = graphics->createShaderBuffer(ShaderBufferCreateInfo{
.sourceData = .sourceData =
{ {
.size = sizeof(PaddedMatrix) * matrixCount, .size = sizeof(Matrix3) * matrixCount,
.data = (uint8*)table.data(), .data = (uint8*)table.data(),
}, },
.name = "LebMatrixCache", .name = "LebMatrixCache",
+32 -9
View File
@@ -81,15 +81,7 @@ void RenderPass::beginFrame(const Component::Camera& cam) {
corners[i] = world / world.w; corners[i] = world / world.w;
} }
//viewParams.viewFrustum = { extract_planes_from_view_projection_matrix(viewParams.viewProjectionMatrix, viewParams.viewFrustum);
// .planes =
// {
// computePlane(cam.getCameraPosition(), Vector(corners[2]), Vector(corners[0])),
// computePlane(cam.getCameraPosition(), Vector(corners[1]), Vector(corners[3])),
// computePlane(cam.getCameraPosition(), Vector(corners[0]), Vector(corners[1])),
// computePlane(cam.getCameraPosition(), Vector(corners[3]), Vector(corners[2])),
// },
//};
viewParamsBuffer->rotateBuffer(sizeof(ViewParameter)); viewParamsBuffer->rotateBuffer(sizeof(ViewParameter));
viewParamsBuffer->updateContents(0, sizeof(ViewParameter), &viewParams); viewParamsBuffer->updateContents(0, sizeof(ViewParameter), &viewParams);
@@ -107,3 +99,34 @@ void RenderPass::beginFrame(const Component::Camera& cam) {
void RenderPass::setResources(PRenderGraphResources _resources) { resources = _resources; } void RenderPass::setResources(PRenderGraphResources _resources) { resources = _resources; }
void RenderPass::setViewport(Gfx::PViewport _viewport) { viewport = _viewport; } void RenderPass::setViewport(Gfx::PViewport _viewport) { viewport = _viewport; }
void RenderPass::normalize_plane(Plane& plane) {
float l = sqrtf(plane.n.x * plane.n.x + plane.n.y * plane.n.y + plane.n.z * plane.n.z);
plane.n.x /= l;
plane.n.y /= l;
plane.n.z /= l;
plane.d /= l;
}
void RenderPass::extract_planes_from_view_projection_matrix(const Matrix4 viewProj, Frustum& frustum) {
// Compute all the planes
frustum.planes[0] = {
.n = Vector(viewProj[0][0] + viewProj[0][3], viewProj[1][0] + viewProj[1][3], viewProj[2][0] + viewProj[2][3]),
.d = viewProj[3][0] + viewProj[3][3],
};
frustum.planes[1] = {
.n = Vector(-viewProj[0][0] + viewProj[0][3], -viewProj[1][0] + viewProj[1][3], -viewProj[2][0] + viewProj[2][3]),
.d = -viewProj[3][0] + viewProj[3][3],
};
frustum.planes[2] = {
.n = Vector(viewProj[0][1] + viewProj[0][3], viewProj[1][1] + viewProj[1][3], viewProj[2][1] + viewProj[2][3]),
.d = viewProj[3][1] + viewProj[3][3],
};
frustum.planes[3] = {
.n = Vector(-viewProj[0][1] + viewProj[0][3], -viewProj[1][1] + viewProj[1][3], -viewProj[2][1] + viewProj[2][3]),
.d = -viewProj[3][1] + viewProj[3][3],
};
// Normalize all the planes
for (uint32_t planeIdx = 0; planeIdx < 4; ++planeIdx)
normalize_plane(frustum.planes[planeIdx]);
}
@@ -46,6 +46,8 @@ class RenderPass {
return plane; return plane;
} }
void normalize_plane(Plane& plane);
void extract_planes_from_view_projection_matrix(const Matrix4 viewProj, Frustum& frustum);
struct ViewParameter { struct ViewParameter {
Frustum viewFrustum; Frustum viewFrustum;
@@ -298,7 +298,7 @@ void TerrainRenderer::setViewport(Gfx::PViewport _viewport, Gfx::PRenderPass ren
.rasterizationState = .rasterizationState =
{ {
.polygonMode = Gfx::SE_POLYGON_MODE_LINE, .polygonMode = Gfx::SE_POLYGON_MODE_LINE,
.cullMode = Gfx::SE_CULL_MODE_NONE, .cullMode = Gfx::SE_CULL_MODE_BACK_BIT,
}, },
.colorBlend = .colorBlend =
{ {
@@ -310,6 +310,7 @@ void TerrainRenderer::setViewport(Gfx::PViewport _viewport, Gfx::PRenderPass ren
}, },
}, },
}, },
}; };
pipeline = graphics->createGraphicsPipeline(std::move(pipelineInfo)); pipeline = graphics->createGraphicsPipeline(std::move(pipelineInfo));
} }