Trying to switch to 16 bit indices

This commit is contained in:
Dynamitos
2024-04-06 08:29:15 +02:00
parent 505e7d6547
commit 7a713afdb4
12 changed files with 81 additions and 99 deletions
+1 -1
View File
@@ -15,7 +15,7 @@ public:
MeshId id;
uint64 vertexCount;
PMaterialInstanceAsset referencedMaterial;
Array<uint32> indices;
Array<uint16> indices;
Array<Meshlet> meshlets;
void save(ArchiveBuffer& buffer) const;
void load(ArchiveBuffer& buffer);
+10 -29
View File
@@ -32,6 +32,7 @@ void completeMeshlet(Array<Meshlet>& meshlets, Meshlet& current)
{
meshlets.add(current);
current = {
.boundingBox = AABB(),
.numVertices = 0,
.numPrimitives = 0,
};
@@ -60,40 +61,20 @@ void addTriangle(Array<Meshlet>& meshlets, Meshlet& current, Triangle tri)
}
}
void Meshlet::build(const Array<Vector>& positions, const Array<uint32>& indices, Array<Meshlet>& meshlets)
void Meshlet::build(const Array<Vector>& positions, const Array<uint16>& indices, Array<Meshlet>& meshlets)
{
Meshlet current = {
.numVertices = 0,
.numPrimitives = 0,
};
Array<Triangle> 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<float>::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],
},
});
}
}
+2 -2
View File
@@ -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<Vector>& positions, const Array<uint32>& indices, Array<Meshlet>& meshlets);
static void build(const Array<Vector>& positions, const Array<uint16>& indices, Array<Meshlet>& meshlets);
};
} // namespace Seele
+44 -44
View File
@@ -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<Vector, 8> 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<Vector, 8> 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<uint32> loadedIndices, Array<Meshlet> loadedMeshlets)
void VertexData::loadMesh(MeshId id, Array<uint16> loadedIndices, Array<Meshlet> 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<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,
@@ -173,7 +173,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],
@@ -183,13 +183,13 @@ void VertexData::loadMesh(MeshId id, Array<uint32> loadedIndices, Array<Meshlet>
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<uint32> loadedIndices, Array<Meshlet>
});
vertexIndicesBuffer = graphics->createShaderBuffer(ShaderBufferCreateInfo{
.sourceData = {
.size = sizeof(uint32) * vertexIndices.size(),
.size = sizeof(uint16) * vertexIndices.size(),
.data = (uint8*)vertexIndices.data(),
},
.numElements = vertexIndices.size(),
+5 -5
View File
@@ -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<uint32> indices, Array<Meshlet> meshlets);
void loadMesh(MeshId id, Array<uint16> indices, Array<Meshlet> 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<MeshId, uint64> meshVertexCounts;
Array<MeshletDescription> meshlets;
Array<uint8> primitiveIndices;
Array<uint32> vertexIndices;
Array<uint32> indices;
Array<uint16> vertexIndices;
Array<uint16> indices;
Gfx::PGraphics graphics;
Gfx::ODescriptorLayout instanceDataLayout;
// for mesh shading