diff --git a/CMakeSettings.json b/CMakeSettings.json index de49079..6f5248a 100644 --- a/CMakeSettings.json +++ b/CMakeSettings.json @@ -10,7 +10,8 @@ "inheritEnvironments": [ "msvc_x64" ], "cmakeCommandArgs": "-DCMAKE_DEBUG_POSTFIX=\"\" -DCMAKE_PLATFORM=x64", "buildRoot": "C:/Users/Dynamitos/Seele/bin/", - "installRoot": "C:/Program Files/Seele" + "installRoot": "C:/Program Files/Seele", + "addressSanitizerEnabled": true }, { "name": "Release", diff --git a/res/shaders/MeshletBasePass.slang b/res/shaders/MeshletBasePass.slang index f59c353..7cf3122 100644 --- a/res/shaders/MeshletBasePass.slang +++ b/res/shaders/MeshletBasePass.slang @@ -29,18 +29,18 @@ void taskMain( localToClip = mul(pViewParams.projectionMatrix, mul(pViewParams.viewMatrix, instance.transformMatrix)); // Left viewFrustum.sides[0].n = float3(1, 0, 0); - viewFrustum.sides[0].d = 1; + viewFrustum.sides[0].d = -1; // Right viewFrustum.sides[1].n = float3(-1, 0, 0); - viewFrustum.sides[1].d = 1; + viewFrustum.sides[1].d = -1; // Top viewFrustum.sides[2].n = float3(0, -1, 0); - viewFrustum.sides[2].d = 1; + viewFrustum.sides[2].d = -1; // Bottom viewFrustum.sides[3].n = float3(0, 1, 0); - viewFrustum.sides[3].d = 1; + viewFrustum.sides[3].d = -1; // Base - viewFrustum.basePlane.n = float3(0, 0, 1); + viewFrustum.basePlane.n = float3(0, 0, -1); viewFrustum.basePlane.d = 0; } GroupMemoryBarrierWithGroupSync(); diff --git a/res/shaders/lib/Common.slang b/res/shaders/lib/Common.slang index 977c31d..b6f83fb 100644 --- a/res/shaders/lib/Common.slang +++ b/res/shaders/lib/Common.slang @@ -35,13 +35,13 @@ struct Frustum Plane basePlane; bool pointInside(float3 point) { - if (dot(basePlane.n, point) + basePlane.d < 0.0f) + if (dot(basePlane.n, point) + basePlane.d > 0.0f) { return false; } for(int p = 0; p < 4; ++p) { - if(dot(sides[p].n, point) + sides[p].d < 0.0f) + if(dot(sides[p].n, point) + sides[p].d > 0.0f) { return false; } diff --git a/res/shaders/lib/Scene.slang b/res/shaders/lib/Scene.slang index 6a8dc5a..c599217 100644 --- a/res/shaders/lib/Scene.slang +++ b/res/shaders/lib/Scene.slang @@ -7,8 +7,6 @@ struct MeshletDescription uint32_t primitiveCount; uint32_t vertexOffset; uint32_t primitiveOffset; - float3 color; - float pad; }; struct MeshData @@ -22,8 +20,8 @@ struct MeshData uint32_t pad0[3]; }; -static const uint MAX_VERTICES = 64; -static const uint MAX_PRIMITIVES = 126; +static const uint MAX_VERTICES = 256; +static const uint MAX_PRIMITIVES = 256; static const uint TASK_GROUP_SIZE = 128; static const uint MESH_GROUP_SIZE = 32; static const uint MAX_MESHLETS_PER_MESH = 512; diff --git a/src/Editor/Asset/MeshLoader.cpp b/src/Editor/Asset/MeshLoader.cpp index 92dad4b..5bc1a11 100644 --- a/src/Editor/Asset/MeshLoader.cpp +++ b/src/Editor/Asset/MeshLoader.cpp @@ -268,7 +268,11 @@ void MeshLoader::loadGlobalMeshes(const aiScene* scene, const Array meshlets; meshlets.reserve(indices.size() / (3ull * Gfx::numPrimitivesPerMeshlet)); - Meshlet::build(positions, indices, meshlets); + Meshlet::build(indices, meshlets); + for (auto& meshlet : meshlets) + { + meshlet.calcBoundingBox(positions); + } vertexData->loadMesh(id, indices, meshlets); collider.physicsMesh.addCollider(positions, indices, Matrix4(1.0f)); diff --git a/src/Engine/Containers/Array.h b/src/Engine/Containers/Array.h index 9d65f41..ef966c2 100644 --- a/src/Engine/Containers/Array.h +++ b/src/Engine/Containers/Array.h @@ -587,8 +587,8 @@ private: deallocateArray(_data, arraySize); _data = tempArray; } - std::allocator_traits::construct(allocator, &_data[arraySize++], std::forward(t)); - return _data[arraySize - 1]; + std::allocator_traits::construct(allocator, &_data[arraySize], std::forward(t)); + return _data[arraySize++]; } template void resizeInternal(size_type newSize, const Type& value) noexcept diff --git a/src/Engine/Graphics/VertexData.cpp b/src/Engine/Graphics/VertexData.cpp index bf61dc1..81dc59d 100644 --- a/src/Engine/Graphics/VertexData.cpp +++ b/src/Engine/Graphics/VertexData.cpp @@ -114,7 +114,6 @@ void VertexData::loadMesh(MeshId id, Array loadedIndices, Array .primitiveCount = m.numPrimitives, .vertexOffset = vertexOffset, .primitiveOffset = primitiveOffset, - .color = Vector((float)rand() / RAND_MAX, (float)rand() / RAND_MAX, (float)rand() / RAND_MAX), }); } meshData[id].add(MeshData{ @@ -244,70 +243,66 @@ VertexData::VertexData() { } -void Meshlet::build(const Array& positions, const Array& indices, Array& meshlets) +void Meshlet::build(const Array& indices, Array& meshlets) { - std::set uniqueVertices; Meshlet current = { .numVertices = 0, .numPrimitives = 0, }; - auto insertAndGetIndex = [&positions, &uniqueVertices, ¤t](uint32 index) -> int8_t + auto findIndex = [¤t](uint32 index) -> int { + for (uint32 i = 0; i < current.numVertices; ++i) { - auto [it, inserted] = uniqueVertices.insert(index); - if (inserted) + if (current.uniqueVertices[i] == index) { - if (current.numVertices == Gfx::numVerticesPerMeshlet) - { - return -1; - } - current.uniqueVertices[current.numVertices] = index; - current.boundingBox.adjust(positions[index]); - return current.numVertices++; - } - else - { - for (uint32 i = 0; i < current.numVertices; ++i) - { - if (current.uniqueVertices[i] == index) - { - return i; - } - } - // it could be in unique vertices but not in meshlet vertices - return -1; + return i; } + } + if (current.numVertices == Gfx::numVerticesPerMeshlet) + { + return -1; + } + current.uniqueVertices[current.numVertices] = index; + return current.numVertices++; }; - auto completeMeshlet = [&meshlets, ¤t, &uniqueVertices]() { + auto completeMeshlet = [&meshlets, ¤t]() { meshlets.add(current); current = { .numVertices = 0, .numPrimitives = 0, }; - uniqueVertices.clear(); }; for (size_t faceIndex = 0; faceIndex < indices.size() / 3; ++faceIndex) { - auto i1 = insertAndGetIndex(indices[faceIndex * 3 + 0]); - auto i2 = insertAndGetIndex(indices[faceIndex * 3 + 1]); - auto i3 = insertAndGetIndex(indices[faceIndex * 3 + 2]); - if (i1 == -1 || i2 == -1 || i3 == -1) + int f1 = findIndex(indices[faceIndex * 3 + 0]); + int f2 = findIndex(indices[faceIndex * 3 + 1]); + int f3 = findIndex(indices[faceIndex * 3 + 2]); + + if (f1 == -1 || f2 == -1 || f1 == -1) { completeMeshlet(); - i1 = insertAndGetIndex(indices[faceIndex * 3 + 0]); - i2 = insertAndGetIndex(indices[faceIndex * 3 + 1]); - i3 = insertAndGetIndex(indices[faceIndex * 3 + 2]); + f1 = findIndex(indices[faceIndex * 3 + 0]); + f2 = findIndex(indices[faceIndex * 3 + 1]); + f3 = findIndex(indices[faceIndex * 3 + 2]); } - current.primitiveLayout[current.numPrimitives * 3 + 0] = i1; - current.primitiveLayout[current.numPrimitives * 3 + 1] = i2; - current.primitiveLayout[current.numPrimitives * 3 + 2] = i3; + current.primitiveLayout[current.numPrimitives * 3 + 0] = uint8(f1); + current.primitiveLayout[current.numPrimitives * 3 + 1] = uint8(f2); + current.primitiveLayout[current.numPrimitives * 3 + 2] = uint8(f3); current.numPrimitives++; if (current.numPrimitives == Gfx::numPrimitivesPerMeshlet) { completeMeshlet(); } } - if (!uniqueVertices.empty()) + if (current.numVertices > 0) { completeMeshlet(); } } + +void Meshlet::calcBoundingBox(const Array& positions) +{ + for (uint32 i = 0; i < numVertices; ++i) + { + boundingBox.adjust(positions[uniqueVertices[i]]); + } +} \ No newline at end of file diff --git a/src/Engine/Graphics/VertexData.h b/src/Engine/Graphics/VertexData.h index 5be7026..fec4d54 100644 --- a/src/Engine/Graphics/VertexData.h +++ b/src/Engine/Graphics/VertexData.h @@ -30,7 +30,8 @@ struct Meshlet 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& indices, Array& meshlets); + void calcBoundingBox(const Array& positions); }; class VertexData { @@ -98,8 +99,6 @@ protected: uint32_t primitiveCount; uint32_t vertexOffset; uint32_t primitiveOffset; - Vector color; - float pad0; }; Map materialData; Map> meshData;