CULLING FINALLY WORKS

This commit is contained in:
Dynamitos
2024-05-05 08:31:40 +02:00
parent 87b72dcd84
commit 05bb1d0cee
7 changed files with 32 additions and 25 deletions
+2 -2
View File
@@ -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,
+13 -13
View File
@@ -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;
}
};
+7
View File
@@ -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);
+2 -2
View File
@@ -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;
+5 -5
View File
@@ -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<Vector, 8> 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<uint32> loadedIndices, Array<Meshlet>
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<uint32> loadedIndices, Array<Meshlet>
});
}
meshData[id].add(MeshData{
.bounding = meshAABB.toSphere(),
.bounding = meshAABB,//.toSphere(),
.numMeshlets = numMeshlets,
.meshletOffset = meshletOffset,
.indicesOffset = (uint32)meshOffsets[id],
+2 -2
View File
@@ -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;
+1 -1
View File
@@ -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();