Adding AABB to meshlets

This commit is contained in:
Dynamitos
2023-12-11 14:45:37 +01:00
parent eceb0f6bcc
commit 3feb331927
17 changed files with 63 additions and 51 deletions
+9 -9
View File
@@ -29,16 +29,16 @@ void taskMain(
localToClip = mul(pViewParams.projectionMatrix, mul(pViewParams.viewMatrix, instance.transformMatrix)); localToClip = mul(pViewParams.projectionMatrix, mul(pViewParams.viewMatrix, instance.transformMatrix));
// Left // Left
viewFrustum.sides[0].n = float3(1, 0, 0); viewFrustum.sides[0].n = float3(1, 0, 0);
viewFrustum.sides[0].d = -1; viewFrustum.sides[0].d = -0.8;
// Right // Right
viewFrustum.sides[1].n = float3(-1, 0, 0); viewFrustum.sides[1].n = float3(-1, 0, 0);
viewFrustum.sides[1].d = 1; viewFrustum.sides[1].d = 0.8;
// Top // Top
viewFrustum.sides[2].n = float3(0, -1, 0); viewFrustum.sides[2].n = float3(0, -1, 0);
viewFrustum.sides[2].d = 1; viewFrustum.sides[2].d = 0.8;
// Bottom // Bottom
viewFrustum.sides[1].n = float3(0, 1, 0); viewFrustum.sides[1].n = float3(0, 1, 0);
viewFrustum.sides[1].d = -1; viewFrustum.sides[1].d = -0.8;
// Base // Base
viewFrustum.basePlane.n = float3(0, 0, 1); viewFrustum.basePlane.n = float3(0, 0, 1);
viewFrustum.basePlane.d = 0; viewFrustum.basePlane.d = 0;
@@ -51,7 +51,7 @@ void taskMain(
{ {
uint m = mesh.meshletOffset + i; uint m = mesh.meshletOffset + i;
MeshletDescription meshlet = pScene.meshletInfos[m]; MeshletDescription meshlet = pScene.meshletInfos[m];
//if(meshlet.boundingBox.insideFrustum(localToClip, viewFrustum)) if(meshlet.boundingBox.insideFrustum(localToClip, viewFrustum))
{ {
uint index; uint index;
InterlockedAdd(head, 1, index); InterlockedAdd(head, 1, index);
@@ -75,13 +75,13 @@ struct PrimitiveAttributes
void meshMain( void meshMain(
in uint threadID: SV_GroupIndex, in uint threadID: SV_GroupIndex,
in uint groupID: SV_GroupID, in uint groupID: SV_GroupID,
//in payload MeshPayload meshPayload, in payload MeshPayload meshPayload,
out Vertices<FragmentParameter, MAX_VERTICES> vertices, out Vertices<FragmentParameter, MAX_VERTICES> vertices,
out Indices<uint3, MAX_PRIMITIVES> indices out Indices<uint3, MAX_PRIMITIVES> indices
){ ){
InstanceData inst = pScene.instances[0];//meshPayload.instanceId[groupID]]; InstanceData inst = pScene.instances[meshPayload.instanceId[groupID]];
MeshData md = pScene.meshData[0];//meshPayload.instanceId[groupID]]; MeshData md = pScene.meshData[meshPayload.instanceId[groupID]];
MeshletDescription m = pScene.meshletInfos[0];//meshPayload.meshletId[groupID]]; MeshletDescription m = pScene.meshletInfos[meshPayload.meshletId[groupID]];
SetMeshOutputCounts(m.vertexCount, m.primitiveCount); SetMeshOutputCounts(m.vertexCount, m.primitiveCount);
uint p = min(threadID, m.primitiveCount - 1); uint p = min(threadID, m.primitiveCount - 1);
+2
View File
@@ -3,7 +3,9 @@ import Common;
struct AABB struct AABB
{ {
float3 min; float3 min;
float pad0;
float3 max; float3 max;
float3 pad1;
bool insideFrustum(float4x4 transform, Frustum frustum) bool insideFrustum(float4x4 transform, Frustum frustum)
{ {
float3 corners[8]; float3 corners[8];
+1 -1
View File
@@ -30,7 +30,7 @@ struct PointLight : ILightEnv
float3 lightDir_TS = mul(params.tbn, lightDir_WS); float3 lightDir_TS = mul(params.tbn, lightDir_WS);
float d = length(lightDir_TS); float d = length(lightDir_TS);
float illuminance = max(1 - d / colorRange.w, 0); 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) bool insidePlane(Plane plane)
+1
View File
@@ -11,6 +11,7 @@ struct MeshletDescription
struct MeshData struct MeshData
{ {
AABB boundingBox;
uint numMeshlets; uint numMeshlets;
uint meshletOffset; uint meshletOffset;
uint firstIndex; uint firstIndex;
+1 -1
View File
@@ -268,7 +268,7 @@ void MeshLoader::loadGlobalMeshes(const aiScene* scene, const Array<PMaterialIns
Array<Meshlet> meshlets; Array<Meshlet> meshlets;
meshlets.reserve(indices.size() / (3ull * Gfx::numPrimitivesPerMeshlet)); meshlets.reserve(indices.size() / (3ull * Gfx::numPrimitivesPerMeshlet));
Meshlet::buildFromIndexBuffer(indices, meshlets); Meshlet::build(positions, indices, meshlets);
vertexData->loadMesh(id, indices, meshlets); vertexData->loadMesh(id, indices, meshlets);
collider.physicsMesh.addCollider(positions, indices, Matrix4(1.0f)); collider.physicsMesh.addCollider(positions, indices, Matrix4(1.0f));
+1 -1
View File
@@ -229,7 +229,7 @@ int main()
sceneViewInfo.dimensions.size.y = 1080; sceneViewInfo.dimensions.size.y = 1080;
sceneViewInfo.dimensions.offset.x = 0; sceneViewInfo.dimensions.offset.x = 0;
sceneViewInfo.dimensions.offset.y = 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()); OGameView sceneView = new Editor::PlayView(graphics, window, sceneViewInfo, binaryPath.generic_string());
//ViewportCreateInfo inspectorViewInfo; //ViewportCreateInfo inspectorViewInfo;
-2
View File
@@ -1,6 +1,5 @@
target_sources(Engine target_sources(Engine
PRIVATE PRIVATE
AABB.h
BoxCollider.h BoxCollider.h
Camera.h Camera.h
Camera.cpp Camera.cpp
@@ -24,7 +23,6 @@ target_sources(Engine
target_sources(Engine target_sources(Engine
PUBLIC FILE_SET HEADERS PUBLIC FILE_SET HEADERS
FILES FILES
AABB.h
BoxCollider.h BoxCollider.h
Camera.h Camera.h
Collider.h Collider.h
+1 -1
View File
@@ -1,5 +1,5 @@
#pragma once #pragma once
#include "AABB.h" #include "Math/AABB.h"
#include "ShapeBase.h" #include "ShapeBase.h"
namespace Seele namespace Seele
+1 -1
View File
@@ -1,5 +1,5 @@
#pragma once #pragma once
#include "AABB.h" #include "Math/AABB.h"
namespace Seele namespace Seele
{ {
+1 -1
View File
@@ -1,5 +1,5 @@
#include "ShapeBase.h" #include "ShapeBase.h"
#include "AABB.h" #include "Math/AABB.h"
using namespace Seele; using namespace Seele;
using namespace Seele::Component; using namespace Seele::Component;
+7
View File
@@ -456,10 +456,13 @@ public:
{ {
return; return;
} }
if constexpr (!std::is_trivially_destructible_v<T>)
{
for (size_type i = 0; i < arraySize; ++i) for (size_type i = 0; i < arraySize; ++i)
{ {
std::allocator_traits<allocator_type>::destroy(allocator, &_data[i]); std::allocator_traits<allocator_type>::destroy(allocator, &_data[i]);
} }
}
deallocateArray(_data, allocated); deallocateArray(_data, allocated);
_data = nullptr; _data = nullptr;
arraySize = 0; arraySize = 0;
@@ -596,11 +599,15 @@ private:
if(newSize < arraySize) if(newSize < arraySize)
{ {
// But since we are sizing down we destruct some of them // But since we are sizing down we destruct some of them
if constexpr (!std::is_trivially_destructible_v<T>)
{
for (size_type i = newSize; i < arraySize; ++i) for (size_type i = newSize; i < arraySize; ++i)
{ {
std::allocator_traits<allocator_type>::destroy(allocator, &_data[i]); std::allocator_traits<allocator_type>::destroy(allocator, &_data[i]);
} }
} }
}
else else
{ {
// Or construct the new elements by default // Or construct the new elements by default
+15 -15
View File
@@ -70,21 +70,21 @@ struct TextureCreateInfo
struct SamplerCreateInfo struct SamplerCreateInfo
{ {
Gfx::SeSamplerCreateFlags flags; Gfx::SeSamplerCreateFlags flags;
Gfx::SeFilter magFilter; Gfx::SeFilter magFilter = Gfx::SE_FILTER_LINEAR;
Gfx::SeFilter minFilter; Gfx::SeFilter minFilter = Gfx::SE_FILTER_LINEAR;
Gfx::SeSamplerMipmapMode mipmapMode; Gfx::SeSamplerMipmapMode mipmapMode = Gfx::SE_SAMPLER_MIPMAP_MODE_LINEAR;
Gfx::SeSamplerAddressMode addressModeU; Gfx::SeSamplerAddressMode addressModeU = Gfx::SE_SAMPLER_ADDRESS_MODE_REPEAT;
Gfx::SeSamplerAddressMode addressModeV; Gfx::SeSamplerAddressMode addressModeV = Gfx::SE_SAMPLER_ADDRESS_MODE_REPEAT;
Gfx::SeSamplerAddressMode addressModeW; Gfx::SeSamplerAddressMode addressModeW = Gfx::SE_SAMPLER_ADDRESS_MODE_REPEAT;
float mipLodBias; float mipLodBias = 0.0f;
uint32 anisotropyEnable; uint32 anisotropyEnable = 0;
float maxAnisotropy; float maxAnisotropy = 0.0f;
uint32 compareEnable; uint32 compareEnable = 0;
Gfx::SeCompareOp compareOp; Gfx::SeCompareOp compareOp = Gfx::SE_COMPARE_OP_NEVER;
float minLod; float minLod = 0.0f;
float maxLod; float maxLod = 0.0f;
Gfx::SeBorderColor borderColor; Gfx::SeBorderColor borderColor = Gfx::SE_BORDER_COLOR_FLOAT_OPAQUE_BLACK;
uint32 unnormalizedCoordinates; uint32 unnormalizedCoordinates = 0;
}; };
struct VertexBufferCreateInfo struct VertexBufferCreateInfo
{ {
+7 -3
View File
@@ -97,9 +97,11 @@ void VertexData::loadMesh(MeshId id, Array<uint32> loadedIndices, Array<Meshlet>
{ {
uint32 numMeshlets = std::min<uint32>(512, loadedMeshlets.size() - currentMesh); uint32 numMeshlets = std::min<uint32>(512, loadedMeshlets.size() - currentMesh);
uint32 meshletOffset = meshlets.size(); uint32 meshletOffset = meshlets.size();
AABB meshAABB;
for (uint32 i = 0; i < numMeshlets; ++i) for (uint32 i = 0; i < numMeshlets; ++i)
{ {
Meshlet& m = loadedMeshlets[currentMesh + i]; Meshlet& m = loadedMeshlets[currentMesh + i];
meshAABB = meshAABB.combine(m.boundingBox);
uint32 vertexOffset = vertexIndices.size(); uint32 vertexOffset = vertexIndices.size();
vertexIndices.resize(vertexOffset + m.numVertices); vertexIndices.resize(vertexOffset + m.numVertices);
std::memcpy(vertexIndices.data() + vertexOffset, m.uniqueVertices, m.numVertices * sizeof(uint32)); std::memcpy(vertexIndices.data() + vertexOffset, m.uniqueVertices, m.numVertices * sizeof(uint32));
@@ -107,7 +109,7 @@ void VertexData::loadMesh(MeshId id, Array<uint32> loadedIndices, Array<Meshlet>
primitiveIndices.resize(primitiveOffset + (m.numPrimitives * 3)); primitiveIndices.resize(primitiveOffset + (m.numPrimitives * 3));
std::memcpy(primitiveIndices.data() + primitiveOffset, m.primitiveLayout, m.numPrimitives * 3 * sizeof(uint8)); std::memcpy(primitiveIndices.data() + primitiveOffset, m.primitiveLayout, m.numPrimitives * 3 * sizeof(uint8));
meshlets.add(MeshletDescription{ meshlets.add(MeshletDescription{
.boundingBox = MeshletAABB(), .boundingBox = m.boundingBox,
.vertexCount = m.numVertices, .vertexCount = m.numVertices,
.primitiveCount = m.numPrimitives, .primitiveCount = m.numPrimitives,
.vertexOffset = vertexOffset, .vertexOffset = vertexOffset,
@@ -115,6 +117,7 @@ void VertexData::loadMesh(MeshId id, Array<uint32> loadedIndices, Array<Meshlet>
}); });
} }
meshData[id].add(MeshData{ meshData[id].add(MeshData{
.boundingBox = meshAABB,
.numMeshlets = numMeshlets, .numMeshlets = numMeshlets,
.meshletOffset = meshletOffset, .meshletOffset = meshletOffset,
.indicesOffset = static_cast<uint32>(meshOffsets[id]), .indicesOffset = static_cast<uint32>(meshOffsets[id]),
@@ -230,14 +233,14 @@ VertexData::VertexData()
{ {
} }
void Seele::Meshlet::buildFromIndexBuffer(const Array<uint32>& indices, Array<Meshlet>& meshlets) void Meshlet::build(const Array<Vector>& positions, const Array<uint32>& indices, Array<Meshlet>& meshlets)
{ {
std::set<uint32> uniqueVertices; std::set<uint32> uniqueVertices;
Meshlet current = { Meshlet current = {
.numVertices = 0, .numVertices = 0,
.numPrimitives = 0, .numPrimitives = 0,
}; };
auto insertAndGetIndex = [&uniqueVertices, &current](uint32 index) -> int8_t auto insertAndGetIndex = [&positions, &uniqueVertices, &current](uint32 index) -> int8_t
{ {
auto [it, inserted] = uniqueVertices.insert(index); auto [it, inserted] = uniqueVertices.insert(index);
if (inserted) if (inserted)
@@ -247,6 +250,7 @@ void Seele::Meshlet::buildFromIndexBuffer(const Array<uint32>& indices, Array<Me
return -1; return -1;
} }
current.uniqueVertices[current.numVertices] = index; current.uniqueVertices[current.numVertices] = index;
current.boundingBox.adjust(positions[index]);
return current.numVertices++; return current.numVertices++;
} }
else else
+5 -9
View File
@@ -6,6 +6,7 @@
#include "Graphics/Command.h" #include "Graphics/Command.h"
#include "Graphics/Descriptor.h" #include "Graphics/Descriptor.h"
#include "Graphics/Buffer.h" #include "Graphics/Buffer.h"
#include "Math/AABB.h"
namespace Seele namespace Seele
{ {
@@ -24,11 +25,12 @@ struct MeshId
}; };
struct Meshlet struct Meshlet
{ {
AABB boundingBox;
uint32 uniqueVertices[Gfx::numVerticesPerMeshlet]; // unique vertiex indices in the vertex data uint32 uniqueVertices[Gfx::numVerticesPerMeshlet]; // unique vertiex indices in the vertex data
uint8 primitiveLayout[Gfx::numPrimitivesPerMeshlet * 3]; // indices into the uniqueVertices array, only uint8 needed uint8 primitiveLayout[Gfx::numPrimitivesPerMeshlet * 3]; // indices into the uniqueVertices array, only uint8 needed
uint32 numVertices; uint32 numVertices;
uint32 numPrimitives; uint32 numPrimitives;
static void buildFromIndexBuffer(const Array<uint32>& indices, Array<Meshlet>& meshlets); static void build(const Array<Vector>& positions, const Array<uint32>& indices, Array<Meshlet>& meshlets);
}; };
class VertexData class VertexData
{ {
@@ -39,6 +41,7 @@ public:
}; };
struct MeshData struct MeshData
{ {
AABB boundingBox;
uint32 numMeshlets = 0; uint32 numMeshlets = 0;
uint32 meshletOffset = 0; uint32 meshletOffset = 0;
uint32 firstIndex = 0; uint32 firstIndex = 0;
@@ -87,16 +90,9 @@ protected:
virtual void resizeBuffers() = 0; virtual void resizeBuffers() = 0;
virtual void updateBuffers() = 0; virtual void updateBuffers() = 0;
VertexData(); VertexData();
struct MeshletAABB
{
Vector min;
float pad0;
Vector max;
float pad1;
};
struct MeshletDescription struct MeshletDescription
{ {
MeshletAABB boundingBox; AABB boundingBox;
uint32_t vertexCount; uint32_t vertexCount;
uint32_t primitiveCount; uint32_t primitiveCount;
uint32_t vertexOffset; uint32_t vertexOffset;
@@ -1,6 +1,6 @@
#pragma once #pragma once
#include "Math/Vector.h" #include "Vector.h"
#include "Math/Matrix.h" #include "Matrix.h"
#include "Containers/Array.h" #include "Containers/Array.h"
#include "Graphics/DebugVertex.h" #include "Graphics/DebugVertex.h"
namespace Seele namespace Seele
@@ -8,7 +8,9 @@ namespace Seele
struct AABB struct AABB
{ {
Vector min = Vector(std::numeric_limits<float>::max()); Vector min = Vector(std::numeric_limits<float>::max());
float pad0; // So that it can be used directly in shaders
Vector max = Vector(std::numeric_limits<float>::lowest());// cause of reasons Vector max = Vector(std::numeric_limits<float>::lowest());// cause of reasons
float pad1;
void visualize(Array<DebugVertex>& vertices) const void visualize(Array<DebugVertex>& vertices) const
{ {
StaticArray<DebugVertex, 8> corners; StaticArray<DebugVertex, 8> corners;
+2
View File
@@ -1,5 +1,6 @@
target_sources(Engine target_sources(Engine
PRIVATE PRIVATE
AABB.h
Math.h Math.h
Matrix.h Matrix.h
Transform.h Transform.h
@@ -10,6 +11,7 @@ target_sources(Engine
target_sources(Engine target_sources(Engine
PUBLIC FILE_SET HEADERS PUBLIC FILE_SET HEADERS
FILES FILES
AABB.h
Math.h Math.h
Matrix.h Matrix.h
Transform.h Transform.h
+1 -1
View File
@@ -1,7 +1,7 @@
#pragma once #pragma once
#include <entt/entt.hpp> #include <entt/entt.hpp>
#include "Containers/Array.h" #include "Containers/Array.h"
#include "Component/AABB.h" #include "Math/AABB.h"
#include "Component/Collider.h" #include "Component/Collider.h"
#include "Containers/Pair.h" #include "Containers/Pair.h"