From 7a713afdb4801f13b7eb908cf6028af365c44d5f Mon Sep 17 00:00:00 2001 From: Dynamitos Date: Sat, 6 Apr 2024 08:29:15 +0200 Subject: [PATCH] Trying to switch to 16 bit indices --- res/shaders/lib/Bounding.slang | 6 +- res/shaders/lib/Scene.slang | 6 +- src/Editor/Asset/MeshLoader.cpp | 5 +- src/Engine/Component/ShapeBase.cpp | 8 +-- src/Engine/Component/ShapeBase.h | 6 +- src/Engine/Graphics/Mesh.h | 2 +- src/Engine/Graphics/Meshlet.cpp | 39 ++++--------- src/Engine/Graphics/Meshlet.h | 4 +- src/Engine/Graphics/VertexData.cpp | 88 +++++++++++++++--------------- src/Engine/Graphics/VertexData.h | 10 ++-- src/Engine/Math/AABB.h | 4 +- src/Engine/Window/GameView.cpp | 2 +- 12 files changed, 81 insertions(+), 99 deletions(-) diff --git a/res/shaders/lib/Bounding.slang b/res/shaders/lib/Bounding.slang index 3d24bde..2867aa0 100644 --- a/res/shaders/lib/Bounding.slang +++ b/res/shaders/lib/Bounding.slang @@ -35,7 +35,7 @@ struct AABB float3 max; float pad1; bool insideFrustum(float4x4 transform, Frustum frustum) - { + { float4 corners[8]; corners[0] = mul(transform, float4(min.x, min.y, min.z, 1.0f)); corners[1] = mul(transform, float4(min.x, min.y, max.z, 1.0f)); @@ -47,12 +47,10 @@ struct AABB corners[7] = mul(transform, float4(max.x, max.y, max.z, 1.0f)); for(int i = 0; i < 8; ++i) { - float3 adjusted = corners[i].xyz / corners[i].w; - if(frustum.pointInside(adjusted)) + if(frustum.pointInside(corners[i].xyz)) { return true; } - } return false; } diff --git a/res/shaders/lib/Scene.slang b/res/shaders/lib/Scene.slang index 82cd316..0e37c04 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; @@ -39,7 +39,7 @@ struct Scene StructuredBuffer meshData; StructuredBuffer meshletInfos; StructuredBuffer primitiveIndices; - StructuredBuffer vertexIndices; + StructuredBuffer vertexIndices; }; ParameterBlock pScene; diff --git a/src/Editor/Asset/MeshLoader.cpp b/src/Editor/Asset/MeshLoader.cpp index c871ec8..2a00cb6 100644 --- a/src/Editor/Asset/MeshLoader.cpp +++ b/src/Editor/Asset/MeshLoader.cpp @@ -258,9 +258,12 @@ void MeshLoader::loadGlobalMeshes(const aiScene* scene, const ArrayloadBiTangents(id, biTangents); vertexData->loadColors(id, colors); - Array indices(mesh->mNumFaces * 3); + Array indices(mesh->mNumFaces * 3); for (size_t faceIndex = 0; faceIndex < mesh->mNumFaces; ++faceIndex) { + assert(mesh->mFaces[faceIndex].mIndices[0] < std::numeric_limits::max()); + assert(mesh->mFaces[faceIndex].mIndices[1] < std::numeric_limits::max()); + assert(mesh->mFaces[faceIndex].mIndices[2] < std::numeric_limits::max()); indices[faceIndex * 3 + 0] = mesh->mFaces[faceIndex].mIndices[0]; indices[faceIndex * 3 + 1] = mesh->mFaces[faceIndex].mIndices[1]; indices[faceIndex * 3 + 2] = mesh->mFaces[faceIndex].mIndices[2]; diff --git a/src/Engine/Component/ShapeBase.cpp b/src/Engine/Component/ShapeBase.cpp index e43fa66..8ccbe46 100644 --- a/src/Engine/Component/ShapeBase.cpp +++ b/src/Engine/Component/ShapeBase.cpp @@ -127,7 +127,7 @@ void computeFaceIntegrals(Face& f, ComputationState& state) + w * (2 * (n[state.A] * state.Paa + n[state.B] * state.Pab) + w * state.Pa)); } -void computeVolumeIntegrals(const Array vertices, const Array& indices, ComputationState& state) +void computeVolumeIntegrals(const Array vertices, const Array& indices, ComputationState& state) { std::memset(&state, 0, sizeof(ComputationState)); for (size_t i = 0; i < indices.size(); i+=3) @@ -172,7 +172,7 @@ void computeVolumeIntegrals(const Array vertices, const Array& i state.TP /= 2.0f; } -void computePhysicsParamsForMesh(Array& vertices, const Array& indices, Matrix3& bodyInertia, Vector& centerOfMass, float& mass) +void computePhysicsParamsForMesh(Array& vertices, const Array& indices, Matrix3& bodyInertia, Vector& centerOfMass, float& mass) { ComputationState state; computeVolumeIntegrals(vertices, indices, state); @@ -200,7 +200,7 @@ ShapeBase::ShapeBase() } -ShapeBase::ShapeBase(Array vertices, Array indices) +ShapeBase::ShapeBase(Array vertices, Array indices) : vertices(vertices) , indices(indices) { @@ -217,7 +217,7 @@ ShapeBase ShapeBase::transform(const Component::Transform& transform) const return result; } -void ShapeBase::addCollider(Array verts, Array inds, Matrix4 matrix) +void ShapeBase::addCollider(Array verts, Array inds, Matrix4 matrix) { size_t indOffset = vertices.size(); for(auto vert : verts) diff --git a/src/Engine/Component/ShapeBase.h b/src/Engine/Component/ShapeBase.h index a036c2a..5f3b748 100644 --- a/src/Engine/Component/ShapeBase.h +++ b/src/Engine/Component/ShapeBase.h @@ -10,15 +10,15 @@ namespace Component struct ShapeBase { ShapeBase(); - ShapeBase(Array vertices, Array indices); + ShapeBase(Array vertices, Array indices); ShapeBase transform(const Component::Transform& transform) const; - void addCollider(Array vertices, Array indices, Matrix4 matrix); + void addCollider(Array vertices, Array indices, Matrix4 matrix); void visualize() const; Vector centerOfMass; float mass; Matrix3 bodyInertia; Array vertices; - Array indices; + Array indices; }; } // namespace Component } // namespace Seele diff --git a/src/Engine/Graphics/Mesh.h b/src/Engine/Graphics/Mesh.h index 90a4c03..d49a09c 100644 --- a/src/Engine/Graphics/Mesh.h +++ b/src/Engine/Graphics/Mesh.h @@ -15,7 +15,7 @@ public: MeshId id; uint64 vertexCount; PMaterialInstanceAsset referencedMaterial; - Array indices; + Array indices; Array meshlets; void save(ArchiveBuffer& buffer) const; void load(ArchiveBuffer& buffer); diff --git a/src/Engine/Graphics/Meshlet.cpp b/src/Engine/Graphics/Meshlet.cpp index 7d00364..585e838 100644 --- a/src/Engine/Graphics/Meshlet.cpp +++ b/src/Engine/Graphics/Meshlet.cpp @@ -32,6 +32,7 @@ void completeMeshlet(Array& meshlets, Meshlet& current) { meshlets.add(current); current = { + .boundingBox = AABB(), .numVertices = 0, .numPrimitives = 0, }; @@ -60,40 +61,20 @@ void addTriangle(Array& meshlets, Meshlet& current, Triangle tri) } } -void Meshlet::build(const Array& positions, const Array& indices, Array& meshlets) +void Meshlet::build(const Array& positions, const Array& indices, Array& meshlets) { Meshlet current = { .numVertices = 0, .numPrimitives = 0, }; - Array triangles(indices.size() / 3); - for (size_t i = 0; i < triangles.size(); ++i) + for (size_t i = 0; i < indices.size() / 3; ++i) { - triangles[i].indices[0] = indices[i * 3 + 0]; - triangles[i].indices[1] = indices[i * 3 + 1]; - triangles[i].indices[2] = indices[i * 3 + 2]; - } - while (!triangles.empty()) - { - uint32 best = 0; - float lowestSurface = std::numeric_limits::max(); - AABB newAABB; - for (uint32 i = 0; i < triangles.size(); ++i) - { - AABB adjusted = current.boundingBox; - adjusted.adjust(positions[triangles[i].indices[0]]); - adjusted.adjust(positions[triangles[i].indices[1]]); - adjusted.adjust(positions[triangles[i].indices[2]]); - float surface = adjusted.surfaceArea(); - if (surface < lowestSurface) - { - lowestSurface = surface; - best = i; - newAABB = adjusted; - } - } - current.boundingBox = newAABB; - addTriangle(meshlets, current, triangles[best]); - triangles.removeAt(best); + addTriangle(meshlets, current, Triangle{ + .indices = { + indices[i * 3 + 0], + indices[i * 3 + 1], + indices[i * 3 + 2], + }, + }); } } diff --git a/src/Engine/Graphics/Meshlet.h b/src/Engine/Graphics/Meshlet.h index 88afea2..3acf913 100644 --- a/src/Engine/Graphics/Meshlet.h +++ b/src/Engine/Graphics/Meshlet.h @@ -7,10 +7,10 @@ namespace Seele struct Meshlet { AABB boundingBox; - uint32 uniqueVertices[Gfx::numVerticesPerMeshlet]; // unique vertiex indices in the vertex data + uint16 uniqueVertices[Gfx::numVerticesPerMeshlet]; // unique vertiex indices in the vertex data uint8 primitiveLayout[Gfx::numPrimitivesPerMeshlet * 3]; // indices into the uniqueVertices array, only uint8 needed uint32 numVertices; uint32 numPrimitives; - static void build(const Array& positions, const Array& indices, Array& meshlets); + static void build(const Array& positions, const Array& indices, Array& meshlets); }; } // namespace Seele \ No newline at end of file diff --git a/src/Engine/Graphics/VertexData.cpp b/src/Engine/Graphics/VertexData.cpp index b76e39e..87ac929 100644 --- a/src/Engine/Graphics/VertexData.cpp +++ b/src/Engine/Graphics/VertexData.cpp @@ -44,43 +44,43 @@ void VertexData::updateMesh(PMesh mesh, Component::Transform& transform) }, .data = data, }); - //for (size_t i = 0; i < data.numMeshlets; ++i) - //{ - // auto bounding = meshlets[data.meshletOffset + i].bounding; - // StaticArray corners; - // corners[0] = transform.toMatrix() * Vector4(bounding.min.x, bounding.min.y, bounding.min.z, 1); - // corners[1] = transform.toMatrix() * Vector4(bounding.min.x, bounding.min.y, bounding.max.z, 1); - // corners[2] = transform.toMatrix() * Vector4(bounding.min.x, bounding.max.y, bounding.min.z, 1); - // corners[3] = transform.toMatrix() * Vector4(bounding.min.x, bounding.max.y, bounding.max.z, 1); - // corners[4] = transform.toMatrix() * Vector4(bounding.max.x, bounding.min.y, bounding.min.z, 1); - // corners[5] = transform.toMatrix() * Vector4(bounding.max.x, bounding.min.y, bounding.max.z, 1); - // corners[6] = transform.toMatrix() * Vector4(bounding.max.x, bounding.max.y, bounding.min.z, 1); - // corners[7] = transform.toMatrix() * Vector4(bounding.max.x, bounding.max.y, bounding.max.z, 1); - // addDebugVertex(DebugVertex{ .position = corners[0], .color = meshlets[data.meshletOffset + i].color }); - // addDebugVertex(DebugVertex{ .position = corners[1], .color = meshlets[data.meshletOffset + i].color }); - // addDebugVertex(DebugVertex{ .position = corners[0], .color = meshlets[data.meshletOffset + i].color }); - // addDebugVertex(DebugVertex{ .position = corners[2], .color = meshlets[data.meshletOffset + i].color }); - // addDebugVertex(DebugVertex{ .position = corners[1], .color = meshlets[data.meshletOffset + i].color }); - // addDebugVertex(DebugVertex{ .position = corners[3], .color = meshlets[data.meshletOffset + i].color }); - // addDebugVertex(DebugVertex{ .position = corners[2], .color = meshlets[data.meshletOffset + i].color }); - // addDebugVertex(DebugVertex{ .position = corners[3], .color = meshlets[data.meshletOffset + i].color }); - // addDebugVertex(DebugVertex{ .position = corners[0], .color = meshlets[data.meshletOffset + i].color }); - // addDebugVertex(DebugVertex{ .position = corners[4], .color = meshlets[data.meshletOffset + i].color }); - // addDebugVertex(DebugVertex{ .position = corners[1], .color = meshlets[data.meshletOffset + i].color }); - // addDebugVertex(DebugVertex{ .position = corners[5], .color = meshlets[data.meshletOffset + i].color }); - // addDebugVertex(DebugVertex{ .position = corners[2], .color = meshlets[data.meshletOffset + i].color }); - // addDebugVertex(DebugVertex{ .position = corners[6], .color = meshlets[data.meshletOffset + i].color }); - // addDebugVertex(DebugVertex{ .position = corners[3], .color = meshlets[data.meshletOffset + i].color }); - // addDebugVertex(DebugVertex{ .position = corners[7], .color = meshlets[data.meshletOffset + i].color }); - // addDebugVertex(DebugVertex{ .position = corners[4], .color = meshlets[data.meshletOffset + i].color }); - // addDebugVertex(DebugVertex{ .position = corners[5], .color = meshlets[data.meshletOffset + i].color }); - // addDebugVertex(DebugVertex{ .position = corners[4], .color = meshlets[data.meshletOffset + i].color }); - // addDebugVertex(DebugVertex{ .position = corners[6], .color = meshlets[data.meshletOffset + i].color }); - // addDebugVertex(DebugVertex{ .position = corners[6], .color = meshlets[data.meshletOffset + i].color }); - // addDebugVertex(DebugVertex{ .position = corners[7], .color = meshlets[data.meshletOffset + i].color }); - // addDebugVertex(DebugVertex{ .position = corners[5], .color = meshlets[data.meshletOffset + i].color }); - // addDebugVertex(DebugVertex{ .position = corners[7], .color = meshlets[data.meshletOffset + i].color }); - //} + for (size_t i = 0; i < data.numMeshlets; ++i) + { + auto bounding = meshlets[data.meshletOffset + i].bounding; + StaticArray corners; + corners[0] = transform.toMatrix() * Vector4(bounding.min.x, bounding.min.y, bounding.min.z, 1); + corners[1] = transform.toMatrix() * Vector4(bounding.min.x, bounding.min.y, bounding.max.z, 1); + corners[2] = transform.toMatrix() * Vector4(bounding.min.x, bounding.max.y, bounding.min.z, 1); + corners[3] = transform.toMatrix() * Vector4(bounding.min.x, bounding.max.y, bounding.max.z, 1); + corners[4] = transform.toMatrix() * Vector4(bounding.max.x, bounding.min.y, bounding.min.z, 1); + corners[5] = transform.toMatrix() * Vector4(bounding.max.x, bounding.min.y, bounding.max.z, 1); + corners[6] = transform.toMatrix() * Vector4(bounding.max.x, bounding.max.y, bounding.min.z, 1); + corners[7] = transform.toMatrix() * Vector4(bounding.max.x, bounding.max.y, bounding.max.z, 1); + addDebugVertex(DebugVertex{ .position = corners[0], .color = meshlets[data.meshletOffset + i].color }); + addDebugVertex(DebugVertex{ .position = corners[1], .color = meshlets[data.meshletOffset + i].color }); + addDebugVertex(DebugVertex{ .position = corners[0], .color = meshlets[data.meshletOffset + i].color }); + addDebugVertex(DebugVertex{ .position = corners[2], .color = meshlets[data.meshletOffset + i].color }); + addDebugVertex(DebugVertex{ .position = corners[1], .color = meshlets[data.meshletOffset + i].color }); + addDebugVertex(DebugVertex{ .position = corners[3], .color = meshlets[data.meshletOffset + i].color }); + addDebugVertex(DebugVertex{ .position = corners[2], .color = meshlets[data.meshletOffset + i].color }); + addDebugVertex(DebugVertex{ .position = corners[3], .color = meshlets[data.meshletOffset + i].color }); + addDebugVertex(DebugVertex{ .position = corners[0], .color = meshlets[data.meshletOffset + i].color }); + addDebugVertex(DebugVertex{ .position = corners[4], .color = meshlets[data.meshletOffset + i].color }); + addDebugVertex(DebugVertex{ .position = corners[1], .color = meshlets[data.meshletOffset + i].color }); + addDebugVertex(DebugVertex{ .position = corners[5], .color = meshlets[data.meshletOffset + i].color }); + addDebugVertex(DebugVertex{ .position = corners[2], .color = meshlets[data.meshletOffset + i].color }); + addDebugVertex(DebugVertex{ .position = corners[6], .color = meshlets[data.meshletOffset + i].color }); + addDebugVertex(DebugVertex{ .position = corners[3], .color = meshlets[data.meshletOffset + i].color }); + addDebugVertex(DebugVertex{ .position = corners[7], .color = meshlets[data.meshletOffset + i].color }); + addDebugVertex(DebugVertex{ .position = corners[4], .color = meshlets[data.meshletOffset + i].color }); + addDebugVertex(DebugVertex{ .position = corners[5], .color = meshlets[data.meshletOffset + i].color }); + addDebugVertex(DebugVertex{ .position = corners[4], .color = meshlets[data.meshletOffset + i].color }); + addDebugVertex(DebugVertex{ .position = corners[6], .color = meshlets[data.meshletOffset + i].color }); + addDebugVertex(DebugVertex{ .position = corners[6], .color = meshlets[data.meshletOffset + i].color }); + addDebugVertex(DebugVertex{ .position = corners[7], .color = meshlets[data.meshletOffset + i].color }); + addDebugVertex(DebugVertex{ .position = corners[5], .color = meshlets[data.meshletOffset + i].color }); + addDebugVertex(DebugVertex{ .position = corners[7], .color = meshlets[data.meshletOffset + i].color }); + } } matInstanceData.materialInstance = referencedInstance; referencedInstance->updateDescriptor(); @@ -142,7 +142,7 @@ void VertexData::createDescriptors() } } -void VertexData::loadMesh(MeshId id, Array loadedIndices, Array loadedMeshlets) +void VertexData::loadMesh(MeshId id, Array loadedIndices, Array loadedMeshlets) { meshlets.reserve(meshlets.size() + loadedMeshlets.size()); vertexIndices.reserve(vertexIndices.size() + loadedMeshlets.size() * Gfx::numVerticesPerMeshlet); @@ -164,7 +164,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, @@ -173,7 +173,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], @@ -183,13 +183,13 @@ void VertexData::loadMesh(MeshId id, Array loadedIndices, Array meshData[id][0].firstIndex = indices.size(); meshData[id][0].numIndices = loadedIndices.size(); indices.resize(indices.size() + loadedIndices.size()); - std::memcpy(indices.data() + meshData[id][0].firstIndex, loadedIndices.data(), loadedIndices.size() * sizeof(uint32)); + std::memcpy(indices.data() + meshData[id][0].firstIndex, loadedIndices.data(), loadedIndices.size() * sizeof(uint16)); indexBuffer = graphics->createIndexBuffer(IndexBufferCreateInfo{ .sourceData = { - .size = sizeof(uint32) * indices.size(), + .size = sizeof(uint16) * indices.size(), .data = (uint8*)indices.data(), }, - .indexType = Gfx::SE_INDEX_TYPE_UINT32, + .indexType = Gfx::SE_INDEX_TYPE_UINT16, }); meshletBuffer = graphics->createShaderBuffer(ShaderBufferCreateInfo{ .sourceData = { @@ -201,7 +201,7 @@ void VertexData::loadMesh(MeshId id, Array loadedIndices, Array }); vertexIndicesBuffer = graphics->createShaderBuffer(ShaderBufferCreateInfo{ .sourceData = { - .size = sizeof(uint32) * vertexIndices.size(), + .size = sizeof(uint16) * vertexIndices.size(), .data = (uint8*)vertexIndices.data(), }, .numElements = vertexIndices.size(), diff --git a/src/Engine/Graphics/VertexData.h b/src/Engine/Graphics/VertexData.h index 28898a2..06ff082 100644 --- a/src/Engine/Graphics/VertexData.h +++ b/src/Engine/Graphics/VertexData.h @@ -32,7 +32,7 @@ public: }; struct MeshData { - BoundingSphere bounding; + AABB bounding; uint32 numMeshlets = 0; uint32 meshletOffset = 0; uint32 firstIndex = 0; @@ -61,7 +61,7 @@ public: void resetMeshData(); void updateMesh(PMesh mesh, Component::Transform& transform); void createDescriptors(); - void loadMesh(MeshId id, Array indices, Array meshlets); + void loadMesh(MeshId id, Array indices, Array meshlets); MeshId allocateVertexData(uint64 numVertices); uint64 getMeshOffset(MeshId id); uint64 getMeshVertexCount(MeshId id); @@ -85,7 +85,7 @@ protected: VertexData(); struct MeshletDescription { - BoundingSphere bounding; + AABB bounding; uint32_t vertexCount; uint32_t primitiveCount; uint32_t vertexOffset; @@ -100,8 +100,8 @@ protected: Map meshVertexCounts; Array meshlets; Array primitiveIndices; - Array vertexIndices; - Array indices; + Array vertexIndices; + Array indices; Gfx::PGraphics graphics; Gfx::ODescriptorLayout instanceDataLayout; // for mesh shading diff --git a/src/Engine/Math/AABB.h b/src/Engine/Math/AABB.h index 20d2a2f..7cb005d 100644 --- a/src/Engine/Math/AABB.h +++ b/src/Engine/Math/AABB.h @@ -140,8 +140,8 @@ struct AABB corners[5] = Vector(max.x, min.y, max.z); corners[6] = Vector(max.x, max.y, min.z); corners[7] = Vector(max.x, max.y, max.z); - Vector tmin = Vector(1, 1, 1) * std::numeric_limits::max(); - Vector tmax = Vector(1, 1, 1) * std::numeric_limits::lowest(); + Vector tmin = Vector(std::numeric_limits::max()); + Vector tmax = Vector(std::numeric_limits::lowest()); for(int i = 0; i < 8; ++i) { Vector transformed = matrix * Vector4(corners[i], 1.0f); diff --git a/src/Engine/Window/GameView.cpp b/src/Engine/Window/GameView.cpp index bb9a8fb..1c34a5c 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();