From 05bb1d0ceefbe92aa1c1fc2710d8df562a13e931 Mon Sep 17 00:00:00 2001 From: Dynamitos Date: Sun, 5 May 2024 08:31:40 +0200 Subject: [PATCH] CULLING FINALLY WORKS --- res/shaders/MeshletBasePass.slang | 4 ++-- res/shaders/lib/Bounding.slang | 26 +++++++++++++------------- res/shaders/lib/Common.slang | 7 +++++++ res/shaders/lib/Scene.slang | 4 ++-- src/Engine/Graphics/VertexData.cpp | 10 +++++----- src/Engine/Graphics/VertexData.h | 4 ++-- src/Engine/Window/GameView.cpp | 2 +- 7 files changed, 32 insertions(+), 25 deletions(-) diff --git a/res/shaders/MeshletBasePass.slang b/res/shaders/MeshletBasePass.slang index 6c6fde2..1f79e90 100644 --- a/res/shaders/MeshletBasePass.slang +++ b/res/shaders/MeshletBasePass.slang @@ -25,8 +25,8 @@ void taskMain( if(threadID == 0) { head = 0; - float3 origin = float3(0, 0, 0); - const float offset = 600.0f; + float3 origin = viewToModel(instance.inverseTransformMatrix, float4(0, 0, 0, 1)).xyz; + const float offset = 0.0f; float3 corners[4] = { screenToModel(instance.inverseTransformMatrix, float4(offset, offset, -1.0f, 1.0f)).xyz, screenToModel(instance.inverseTransformMatrix, float4(pViewParams.screenDimensions.x - offset, offset, -1.0f, 1.0f)).xyz, diff --git a/res/shaders/lib/Bounding.slang b/res/shaders/lib/Bounding.slang index 303c517..08992c2 100644 --- a/res/shaders/lib/Bounding.slang +++ b/res/shaders/lib/Bounding.slang @@ -31,24 +31,24 @@ struct AABB float pad0; float3 max; float pad1; + // modified version from https://learnopengl.com/Guest-Articles/2021/Scene/Frustum-Culling bool insideFrustum(Frustum frustum) { - float3 corners[8]; - corners[0] = float3(min.x, min.y, min.z); - corners[1] = float3(min.x, min.y, max.z); - corners[2] = float3(min.x, max.y, min.z); - corners[3] = float3(min.x, max.y, max.z); - corners[4] = float3(max.x, min.y, min.z); - corners[5] = float3(max.x, min.y, max.z); - corners[6] = float3(max.x, max.y, min.z); - corners[7] = float3(max.x, max.y, max.z); - for(int i = 0; i < 8; ++i) + float3 center = (min + max) * 0.5; + float3 extents = float3(max.x - center.x, max.y - center.y, max.z - center.z); + bool result = true; + for(int i = 0; i < 4 && result; ++i) { - if(frustum.pointInside(corners[i])) + const float r = extents.x * abs(frustum.sides[i].n.x) + + extents.y * abs(frustum.sides[i].n.y) + + extents.z * abs(frustum.sides[i].n.z); + + const float signedDistance = dot(frustum.sides[i].n, center) - frustum.sides[i].d; + if(signedDistance < -r) { - return true; + result = false; } } - return false; + return result; } }; diff --git a/res/shaders/lib/Common.slang b/res/shaders/lib/Common.slang index 108b19b..2b4c7cf 100644 --- a/res/shaders/lib/Common.slang +++ b/res/shaders/lib/Common.slang @@ -31,6 +31,13 @@ float4 viewToWorld(float4 view) return world; } +float4 viewToModel(float4x4 inverseTransform, float4 view) +{ + float4 world = viewToWorld(view); + + return worldToModel(inverseTransform, world); +} + float4 clipToView(float4 clip) { float4 view = mul(pViewParams.inverseProjection, clip); diff --git a/res/shaders/lib/Scene.slang b/res/shaders/lib/Scene.slang index 6f06396..7d98e84 100644 --- a/res/shaders/lib/Scene.slang +++ b/res/shaders/lib/Scene.slang @@ -2,7 +2,7 @@ import Bounding; struct MeshletDescription { - BoundingSphere bounding; + AABB bounding; uint32_t vertexCount; uint32_t primitiveCount; uint32_t vertexOffset; @@ -13,7 +13,7 @@ struct MeshletDescription struct MeshData { - BoundingSphere bounding; + AABB bounding; uint32_t numMeshlets; uint32_t meshletOffset; uint32_t firstIndex; diff --git a/src/Engine/Graphics/VertexData.cpp b/src/Engine/Graphics/VertexData.cpp index f455e75..95c91f1 100644 --- a/src/Engine/Graphics/VertexData.cpp +++ b/src/Engine/Graphics/VertexData.cpp @@ -46,12 +46,12 @@ void VertexData::updateMesh(PMesh mesh, Component::Transform& transform) }, .data = data, }); - for (size_t i = 0; i < data.numMeshlets; ++i) + for (size_t i = 0; i < 0; ++i) { auto bounding = meshlets[data.meshletOffset + i].bounding; StaticArray corners; - Vector min = bounding.center - bounding.radius * Vector(1, 1, 1); - Vector max = bounding.center + bounding.radius * Vector(1, 1, 1); + Vector min = bounding.min;//bounding.center - bounding.radius * Vector(1, 1, 1); + Vector max = bounding.max;//bounding.center + bounding.radius * Vector(1, 1, 1); corners[0] = transformMatrix * Vector4(min.x, min.y, min.z, 1); corners[1] = transformMatrix * Vector4(min.x, min.y, max.z, 1); corners[2] = transformMatrix * Vector4(min.x, max.y, min.z, 1); @@ -171,7 +171,7 @@ void VertexData::loadMesh(MeshId id, Array loadedIndices, Array primitiveIndices.resize(primitiveOffset + (m.numPrimitives * 3)); std::memcpy(primitiveIndices.data() + primitiveOffset, m.primitiveLayout, m.numPrimitives * 3 * sizeof(uint8)); meshlets.add(MeshletDescription{ - .bounding = m.boundingBox.toSphere(), + .bounding = m.boundingBox,//.toSphere(), .vertexCount = m.numVertices, .primitiveCount = m.numPrimitives, .vertexOffset = vertexOffset, @@ -180,7 +180,7 @@ void VertexData::loadMesh(MeshId id, Array loadedIndices, Array }); } meshData[id].add(MeshData{ - .bounding = meshAABB.toSphere(), + .bounding = meshAABB,//.toSphere(), .numMeshlets = numMeshlets, .meshletOffset = meshletOffset, .indicesOffset = (uint32)meshOffsets[id], diff --git a/src/Engine/Graphics/VertexData.h b/src/Engine/Graphics/VertexData.h index 2acd2c7..9bb4432 100644 --- a/src/Engine/Graphics/VertexData.h +++ b/src/Engine/Graphics/VertexData.h @@ -33,7 +33,7 @@ public: }; struct MeshData { - BoundingSphere bounding; + AABB bounding; uint32 numMeshlets = 0; uint32 meshletOffset = 0; uint32 firstIndex = 0; @@ -86,7 +86,7 @@ protected: VertexData(); struct MeshletDescription { - BoundingSphere bounding; + AABB bounding; uint32_t vertexCount; uint32_t primitiveCount; uint32_t vertexOffset; diff --git a/src/Engine/Window/GameView.cpp b/src/Engine/Window/GameView.cpp index 757eab4..2257cc9 100644 --- a/src/Engine/Window/GameView.cpp +++ b/src/Engine/Window/GameView.cpp @@ -21,7 +21,7 @@ GameView::GameView(Gfx::PGraphics graphics, PWindow window, const ViewportCreate renderGraph.addPass(new DepthPrepass(graphics, scene)); renderGraph.addPass(new LightCullingPass(graphics, scene)); renderGraph.addPass(new BasePass(graphics, scene)); - renderGraph.addPass(new DebugPass(graphics, scene)); + //renderGraph.addPass(new DebugPass(graphics, scene)); //renderGraph.addPass(new SkyboxRenderPass(graphics, scene)); renderGraph.setViewport(viewport); renderGraph.createRenderPass();