From 3feb3319273f756bc0a3c7f39e9205a381a3e7ee Mon Sep 17 00:00:00 2001 From: Dynamitos Date: Mon, 11 Dec 2023 14:45:37 +0100 Subject: [PATCH] Adding AABB to meshlets --- res/shaders/MeshletBasePass.slang | 18 ++++++++-------- res/shaders/lib/AABB.slang | 2 ++ res/shaders/lib/LightEnv.slang | 2 +- res/shaders/lib/Scene.slang | 1 + src/Editor/Asset/MeshLoader.cpp | 2 +- src/Editor/main.cpp | 2 +- src/Engine/Component/CMakeLists.txt | 2 -- src/Engine/Component/Collider.h | 2 +- src/Engine/Component/RigidBody.h | 2 +- src/Engine/Component/ShapeBase.cpp | 2 +- src/Engine/Containers/Array.h | 15 ++++++++++---- src/Engine/Graphics/Initializer.h | 30 +++++++++++++-------------- src/Engine/Graphics/VertexData.cpp | 10 ++++++--- src/Engine/Graphics/VertexData.h | 14 +++++-------- src/Engine/{Component => Math}/AABB.h | 6 ++++-- src/Engine/Math/CMakeLists.txt | 2 ++ src/Engine/Physics/BVH.h | 2 +- 17 files changed, 63 insertions(+), 51 deletions(-) rename src/Engine/{Component => Math}/AABB.h (97%) diff --git a/res/shaders/MeshletBasePass.slang b/res/shaders/MeshletBasePass.slang index 410264c..c67749d 100644 --- a/res/shaders/MeshletBasePass.slang +++ b/res/shaders/MeshletBasePass.slang @@ -29,16 +29,16 @@ 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 = -0.8; // Right viewFrustum.sides[1].n = float3(-1, 0, 0); - viewFrustum.sides[1].d = 1; + viewFrustum.sides[1].d = 0.8; // Top viewFrustum.sides[2].n = float3(0, -1, 0); - viewFrustum.sides[2].d = 1; + viewFrustum.sides[2].d = 0.8; // Bottom viewFrustum.sides[1].n = float3(0, 1, 0); - viewFrustum.sides[1].d = -1; + viewFrustum.sides[1].d = -0.8; // Base viewFrustum.basePlane.n = float3(0, 0, 1); viewFrustum.basePlane.d = 0; @@ -51,7 +51,7 @@ void taskMain( { uint m = mesh.meshletOffset + i; MeshletDescription meshlet = pScene.meshletInfos[m]; - //if(meshlet.boundingBox.insideFrustum(localToClip, viewFrustum)) + if(meshlet.boundingBox.insideFrustum(localToClip, viewFrustum)) { uint index; InterlockedAdd(head, 1, index); @@ -75,13 +75,13 @@ struct PrimitiveAttributes void meshMain( in uint threadID: SV_GroupIndex, in uint groupID: SV_GroupID, - //in payload MeshPayload meshPayload, + in payload MeshPayload meshPayload, out Vertices vertices, out Indices indices ){ - InstanceData inst = pScene.instances[0];//meshPayload.instanceId[groupID]]; - MeshData md = pScene.meshData[0];//meshPayload.instanceId[groupID]]; - MeshletDescription m = pScene.meshletInfos[0];//meshPayload.meshletId[groupID]]; + InstanceData inst = pScene.instances[meshPayload.instanceId[groupID]]; + MeshData md = pScene.meshData[meshPayload.instanceId[groupID]]; + MeshletDescription m = pScene.meshletInfos[meshPayload.meshletId[groupID]]; SetMeshOutputCounts(m.vertexCount, m.primitiveCount); uint p = min(threadID, m.primitiveCount - 1); diff --git a/res/shaders/lib/AABB.slang b/res/shaders/lib/AABB.slang index e43456c..ac565c4 100644 --- a/res/shaders/lib/AABB.slang +++ b/res/shaders/lib/AABB.slang @@ -3,7 +3,9 @@ import Common; struct AABB { float3 min; + float pad0; float3 max; + float3 pad1; bool insideFrustum(float4x4 transform, Frustum frustum) { float3 corners[8]; diff --git a/res/shaders/lib/LightEnv.slang b/res/shaders/lib/LightEnv.slang index 8e3cc68..2dc1fd2 100644 --- a/res/shaders/lib/LightEnv.slang +++ b/res/shaders/lib/LightEnv.slang @@ -30,7 +30,7 @@ struct PointLight : ILightEnv float3 lightDir_TS = mul(params.tbn, lightDir_WS); float d = length(lightDir_TS); float illuminance = max(1 - d / colorRange.w, 0); - return brdf.evaluate(params.tbn, params.viewDir_TS, normalize(lightDir_TS), colorRange.xyz); + return illuminance * brdf.evaluate(params.tbn, params.viewDir_TS, normalize(lightDir_TS), colorRange.xyz); } bool insidePlane(Plane plane) diff --git a/res/shaders/lib/Scene.slang b/res/shaders/lib/Scene.slang index b9bbd44..5eac612 100644 --- a/res/shaders/lib/Scene.slang +++ b/res/shaders/lib/Scene.slang @@ -11,6 +11,7 @@ struct MeshletDescription struct MeshData { + AABB boundingBox; uint numMeshlets; uint meshletOffset; uint firstIndex; diff --git a/src/Editor/Asset/MeshLoader.cpp b/src/Editor/Asset/MeshLoader.cpp index 475b606..f48b346 100644 --- a/src/Editor/Asset/MeshLoader.cpp +++ b/src/Editor/Asset/MeshLoader.cpp @@ -268,7 +268,7 @@ void MeshLoader::loadGlobalMeshes(const aiScene* scene, const Array meshlets; meshlets.reserve(indices.size() / (3ull * Gfx::numPrimitivesPerMeshlet)); - Meshlet::buildFromIndexBuffer(indices, meshlets); + Meshlet::build(positions, indices, meshlets); vertexData->loadMesh(id, indices, meshlets); collider.physicsMesh.addCollider(positions, indices, Matrix4(1.0f)); diff --git a/src/Editor/main.cpp b/src/Editor/main.cpp index bac03c6..0920765 100644 --- a/src/Editor/main.cpp +++ b/src/Editor/main.cpp @@ -229,7 +229,7 @@ int main() sceneViewInfo.dimensions.size.y = 1080; sceneViewInfo.dimensions.offset.x = 0; sceneViewInfo.dimensions.offset.y = 0; - sceneViewInfo.numSamples = Gfx::SE_SAMPLE_COUNT_1_BIT; + sceneViewInfo.numSamples = Gfx::SE_SAMPLE_COUNT_4_BIT; OGameView sceneView = new Editor::PlayView(graphics, window, sceneViewInfo, binaryPath.generic_string()); //ViewportCreateInfo inspectorViewInfo; diff --git a/src/Engine/Component/CMakeLists.txt b/src/Engine/Component/CMakeLists.txt index 4731b91..14f6240 100644 --- a/src/Engine/Component/CMakeLists.txt +++ b/src/Engine/Component/CMakeLists.txt @@ -1,6 +1,5 @@ target_sources(Engine PRIVATE - AABB.h BoxCollider.h Camera.h Camera.cpp @@ -24,7 +23,6 @@ target_sources(Engine target_sources(Engine PUBLIC FILE_SET HEADERS FILES - AABB.h BoxCollider.h Camera.h Collider.h diff --git a/src/Engine/Component/Collider.h b/src/Engine/Component/Collider.h index 7f055b0..3315947 100644 --- a/src/Engine/Component/Collider.h +++ b/src/Engine/Component/Collider.h @@ -1,5 +1,5 @@ #pragma once -#include "AABB.h" +#include "Math/AABB.h" #include "ShapeBase.h" namespace Seele diff --git a/src/Engine/Component/RigidBody.h b/src/Engine/Component/RigidBody.h index 11ab93c..b9599ea 100644 --- a/src/Engine/Component/RigidBody.h +++ b/src/Engine/Component/RigidBody.h @@ -1,5 +1,5 @@ #pragma once -#include "AABB.h" +#include "Math/AABB.h" namespace Seele { diff --git a/src/Engine/Component/ShapeBase.cpp b/src/Engine/Component/ShapeBase.cpp index d9450b5..e43fa66 100644 --- a/src/Engine/Component/ShapeBase.cpp +++ b/src/Engine/Component/ShapeBase.cpp @@ -1,5 +1,5 @@ #include "ShapeBase.h" -#include "AABB.h" +#include "Math/AABB.h" using namespace Seele; using namespace Seele::Component; diff --git a/src/Engine/Containers/Array.h b/src/Engine/Containers/Array.h index 7344ed1..9d65f41 100644 --- a/src/Engine/Containers/Array.h +++ b/src/Engine/Containers/Array.h @@ -456,9 +456,12 @@ public: { return; } - for(size_type i = 0; i < arraySize; ++i) + if constexpr (!std::is_trivially_destructible_v) { - std::allocator_traits::destroy(allocator, &_data[i]); + for (size_type i = 0; i < arraySize; ++i) + { + std::allocator_traits::destroy(allocator, &_data[i]); + } } deallocateArray(_data, allocated); _data = nullptr; @@ -596,9 +599,13 @@ private: if(newSize < arraySize) { // But since we are sizing down we destruct some of them - for(size_type i = newSize; i < arraySize; ++i) + + if constexpr (!std::is_trivially_destructible_v) { - std::allocator_traits::destroy(allocator, &_data[i]); + for (size_type i = newSize; i < arraySize; ++i) + { + std::allocator_traits::destroy(allocator, &_data[i]); + } } } else diff --git a/src/Engine/Graphics/Initializer.h b/src/Engine/Graphics/Initializer.h index 08bedeb..493aea6 100644 --- a/src/Engine/Graphics/Initializer.h +++ b/src/Engine/Graphics/Initializer.h @@ -70,21 +70,21 @@ struct TextureCreateInfo struct SamplerCreateInfo { Gfx::SeSamplerCreateFlags flags; - Gfx::SeFilter magFilter; - Gfx::SeFilter minFilter; - Gfx::SeSamplerMipmapMode mipmapMode; - Gfx::SeSamplerAddressMode addressModeU; - Gfx::SeSamplerAddressMode addressModeV; - Gfx::SeSamplerAddressMode addressModeW; - float mipLodBias; - uint32 anisotropyEnable; - float maxAnisotropy; - uint32 compareEnable; - Gfx::SeCompareOp compareOp; - float minLod; - float maxLod; - Gfx::SeBorderColor borderColor; - uint32 unnormalizedCoordinates; + Gfx::SeFilter magFilter = Gfx::SE_FILTER_LINEAR; + Gfx::SeFilter minFilter = Gfx::SE_FILTER_LINEAR; + Gfx::SeSamplerMipmapMode mipmapMode = Gfx::SE_SAMPLER_MIPMAP_MODE_LINEAR; + Gfx::SeSamplerAddressMode addressModeU = Gfx::SE_SAMPLER_ADDRESS_MODE_REPEAT; + Gfx::SeSamplerAddressMode addressModeV = Gfx::SE_SAMPLER_ADDRESS_MODE_REPEAT; + Gfx::SeSamplerAddressMode addressModeW = Gfx::SE_SAMPLER_ADDRESS_MODE_REPEAT; + float mipLodBias = 0.0f; + uint32 anisotropyEnable = 0; + float maxAnisotropy = 0.0f; + uint32 compareEnable = 0; + Gfx::SeCompareOp compareOp = Gfx::SE_COMPARE_OP_NEVER; + float minLod = 0.0f; + float maxLod = 0.0f; + Gfx::SeBorderColor borderColor = Gfx::SE_BORDER_COLOR_FLOAT_OPAQUE_BLACK; + uint32 unnormalizedCoordinates = 0; }; struct VertexBufferCreateInfo { diff --git a/src/Engine/Graphics/VertexData.cpp b/src/Engine/Graphics/VertexData.cpp index 1a09994..1a715b6 100644 --- a/src/Engine/Graphics/VertexData.cpp +++ b/src/Engine/Graphics/VertexData.cpp @@ -97,9 +97,11 @@ void VertexData::loadMesh(MeshId id, Array loadedIndices, Array { uint32 numMeshlets = std::min(512, loadedMeshlets.size() - currentMesh); uint32 meshletOffset = meshlets.size(); + AABB meshAABB; for (uint32 i = 0; i < numMeshlets; ++i) { Meshlet& m = loadedMeshlets[currentMesh + i]; + meshAABB = meshAABB.combine(m.boundingBox); uint32 vertexOffset = vertexIndices.size(); vertexIndices.resize(vertexOffset + m.numVertices); std::memcpy(vertexIndices.data() + vertexOffset, m.uniqueVertices, m.numVertices * sizeof(uint32)); @@ -107,7 +109,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{ - .boundingBox = MeshletAABB(), + .boundingBox = m.boundingBox, .vertexCount = m.numVertices, .primitiveCount = m.numPrimitives, .vertexOffset = vertexOffset, @@ -115,6 +117,7 @@ void VertexData::loadMesh(MeshId id, Array loadedIndices, Array }); } meshData[id].add(MeshData{ + .boundingBox = meshAABB, .numMeshlets = numMeshlets, .meshletOffset = meshletOffset, .indicesOffset = static_cast(meshOffsets[id]), @@ -230,14 +233,14 @@ VertexData::VertexData() { } -void Seele::Meshlet::buildFromIndexBuffer(const Array& indices, Array& meshlets) +void Meshlet::build(const Array& positions, const Array& indices, Array& meshlets) { std::set uniqueVertices; Meshlet current = { .numVertices = 0, .numPrimitives = 0, }; - auto insertAndGetIndex = [&uniqueVertices, ¤t](uint32 index) -> int8_t + auto insertAndGetIndex = [&positions, &uniqueVertices, ¤t](uint32 index) -> int8_t { auto [it, inserted] = uniqueVertices.insert(index); if (inserted) @@ -247,6 +250,7 @@ void Seele::Meshlet::buildFromIndexBuffer(const Array& indices, Array& indices, Array& meshlets); + static void build(const Array& positions, const Array& indices, Array& meshlets); }; class VertexData { @@ -39,6 +41,7 @@ public: }; struct MeshData { + AABB boundingBox; uint32 numMeshlets = 0; uint32 meshletOffset = 0; uint32 firstIndex = 0; @@ -87,16 +90,9 @@ protected: virtual void resizeBuffers() = 0; virtual void updateBuffers() = 0; VertexData(); - struct MeshletAABB - { - Vector min; - float pad0; - Vector max; - float pad1; - }; struct MeshletDescription { - MeshletAABB boundingBox; + AABB boundingBox; uint32_t vertexCount; uint32_t primitiveCount; uint32_t vertexOffset; diff --git a/src/Engine/Component/AABB.h b/src/Engine/Math/AABB.h similarity index 97% rename from src/Engine/Component/AABB.h rename to src/Engine/Math/AABB.h index 7977ad7..82f021e 100644 --- a/src/Engine/Component/AABB.h +++ b/src/Engine/Math/AABB.h @@ -1,6 +1,6 @@ #pragma once -#include "Math/Vector.h" -#include "Math/Matrix.h" +#include "Vector.h" +#include "Matrix.h" #include "Containers/Array.h" #include "Graphics/DebugVertex.h" namespace Seele @@ -8,7 +8,9 @@ namespace Seele struct AABB { Vector min = Vector(std::numeric_limits::max()); + float pad0; // So that it can be used directly in shaders Vector max = Vector(std::numeric_limits::lowest());// cause of reasons + float pad1; void visualize(Array& vertices) const { StaticArray corners; diff --git a/src/Engine/Math/CMakeLists.txt b/src/Engine/Math/CMakeLists.txt index 09c665c..a32f7b7 100644 --- a/src/Engine/Math/CMakeLists.txt +++ b/src/Engine/Math/CMakeLists.txt @@ -1,5 +1,6 @@ target_sources(Engine PRIVATE + AABB.h Math.h Matrix.h Transform.h @@ -10,6 +11,7 @@ target_sources(Engine target_sources(Engine PUBLIC FILE_SET HEADERS FILES + AABB.h Math.h Matrix.h Transform.h diff --git a/src/Engine/Physics/BVH.h b/src/Engine/Physics/BVH.h index d703add..ed7d78f 100644 --- a/src/Engine/Physics/BVH.h +++ b/src/Engine/Physics/BVH.h @@ -1,7 +1,7 @@ #pragma once #include #include "Containers/Array.h" -#include "Component/AABB.h" +#include "Math/AABB.h" #include "Component/Collider.h" #include "Containers/Pair.h"