2023-08-28 21:23:13 +02:00
|
|
|
#pragma once
|
2023-11-06 22:24:40 +01:00
|
|
|
#include "Graphics/Initializer.h"
|
2023-10-24 15:01:09 +02:00
|
|
|
#include "Material/MaterialInstance.h"
|
|
|
|
|
#include "Component/Transform.h"
|
2023-10-26 18:37:29 +02:00
|
|
|
#include "Containers/List.h"
|
2023-11-15 17:42:57 +01:00
|
|
|
#include "Graphics/Command.h"
|
2023-10-26 18:37:29 +02:00
|
|
|
#include "Graphics/Descriptor.h"
|
|
|
|
|
#include "Graphics/Buffer.h"
|
2023-12-11 14:45:37 +01:00
|
|
|
#include "Math/AABB.h"
|
2023-08-28 21:23:13 +02:00
|
|
|
|
|
|
|
|
namespace Seele
|
|
|
|
|
{
|
2023-11-06 14:47:21 +01:00
|
|
|
DECLARE_REF(Mesh)
|
2023-10-24 15:01:09 +02:00
|
|
|
struct MeshId
|
|
|
|
|
{
|
|
|
|
|
uint64 id;
|
2023-10-31 16:16:23 +01:00
|
|
|
std::strong_ordering operator<=>(const MeshId& other) const
|
|
|
|
|
{
|
|
|
|
|
return id <=> other.id;
|
|
|
|
|
}
|
2023-11-22 13:18:54 +01:00
|
|
|
bool operator==(const MeshId& other) const
|
|
|
|
|
{
|
|
|
|
|
return id == other.id;
|
|
|
|
|
}
|
2023-10-31 16:16:23 +01:00
|
|
|
};
|
|
|
|
|
struct Meshlet
|
|
|
|
|
{
|
2023-12-11 14:45:37 +01:00
|
|
|
AABB boundingBox;
|
2023-11-27 22:50:37 +01:00
|
|
|
uint32 uniqueVertices[Gfx::numVerticesPerMeshlet]; // unique vertiex indices in the vertex data
|
|
|
|
|
uint8 primitiveLayout[Gfx::numPrimitivesPerMeshlet * 3]; // indices into the uniqueVertices array, only uint8 needed
|
2023-10-31 16:16:23 +01:00
|
|
|
uint32 numVertices;
|
|
|
|
|
uint32 numPrimitives;
|
2023-12-15 18:33:47 +01:00
|
|
|
static void build(const Array<uint32>& indices, Array<Meshlet>& meshlets);
|
|
|
|
|
void calcBoundingBox(const Array<Vector>& positions);
|
2023-10-24 15:01:09 +02:00
|
|
|
};
|
2023-08-28 21:23:13 +02:00
|
|
|
class VertexData
|
|
|
|
|
{
|
|
|
|
|
public:
|
2023-10-24 15:01:09 +02:00
|
|
|
struct InstanceData
|
|
|
|
|
{
|
|
|
|
|
Matrix4 transformMatrix;
|
|
|
|
|
};
|
2023-11-26 11:27:39 +01:00
|
|
|
struct MeshData
|
|
|
|
|
{
|
2023-12-14 09:04:23 +01:00
|
|
|
uint32 numMeshlets = 0;
|
2023-11-26 11:27:39 +01:00
|
|
|
uint32 meshletOffset = 0;
|
|
|
|
|
uint32 firstIndex = 0;
|
|
|
|
|
uint32 numIndices = 0;
|
2023-12-14 09:04:23 +01:00
|
|
|
uint32 indicesOffset = 0;
|
|
|
|
|
uint32 pad0[3];
|
2023-11-26 11:27:39 +01:00
|
|
|
};
|
2023-10-24 15:01:09 +02:00
|
|
|
struct MeshInstanceData
|
|
|
|
|
{
|
|
|
|
|
InstanceData instance;
|
2023-11-26 11:27:39 +01:00
|
|
|
MeshData data;
|
2023-10-24 15:01:09 +02:00
|
|
|
};
|
|
|
|
|
struct MaterialInstanceData
|
|
|
|
|
{
|
|
|
|
|
PMaterialInstance materialInstance;
|
2023-11-08 23:27:21 +01:00
|
|
|
Gfx::OShaderBuffer instanceBuffer;
|
|
|
|
|
Gfx::OShaderBuffer meshDataBuffer;
|
2023-10-24 15:01:09 +02:00
|
|
|
Gfx::PDescriptorSet descriptorSet;
|
|
|
|
|
Array<MeshInstanceData> meshes;
|
|
|
|
|
};
|
|
|
|
|
struct MaterialData
|
|
|
|
|
{
|
|
|
|
|
PMaterial material;
|
|
|
|
|
Map<uint64, MaterialInstanceData> instances;
|
|
|
|
|
};
|
|
|
|
|
void resetMeshData();
|
2023-11-26 09:40:48 +01:00
|
|
|
void updateMesh(PMesh mesh, Component::Transform& transform);
|
2023-10-24 15:01:09 +02:00
|
|
|
void createDescriptors();
|
2023-11-26 11:27:39 +01:00
|
|
|
void loadMesh(MeshId id, Array<uint32> indices, Array<Meshlet> meshlets);
|
2023-10-31 17:55:30 +01:00
|
|
|
MeshId allocateVertexData(uint64 numVertices);
|
2023-11-09 22:15:51 +01:00
|
|
|
uint64 getMeshOffset(MeshId id);
|
|
|
|
|
uint64 getMeshVertexCount(MeshId id);
|
2023-11-01 13:38:49 +01:00
|
|
|
virtual void serializeMesh(MeshId id, uint64 numVertices, ArchiveBuffer& buffer) = 0;
|
|
|
|
|
virtual void deserializeMesh(MeshId id, ArchiveBuffer& buffer) = 0;
|
2023-10-24 15:01:09 +02:00
|
|
|
virtual void bindBuffers(Gfx::PRenderCommand command) = 0;
|
|
|
|
|
virtual Gfx::PDescriptorLayout getVertexDataLayout() = 0;
|
|
|
|
|
virtual Gfx::PDescriptorSet getVertexDataSet() = 0;
|
|
|
|
|
virtual std::string getTypeName() const = 0;
|
2023-11-26 11:27:39 +01:00
|
|
|
Gfx::PIndexBuffer getIndexBuffer() { return indexBuffer; }
|
2023-10-31 17:55:30 +01:00
|
|
|
Gfx::PDescriptorLayout getInstanceDataLayout() { return instanceDataLayout; }
|
2023-10-24 15:01:09 +02:00
|
|
|
const Map<std::string, MaterialData>& getMaterialData() const { return materialData; }
|
|
|
|
|
const Array<MeshData>& getMeshData(MeshId id) { return meshData[id]; }
|
|
|
|
|
static List<VertexData*> getList();
|
2023-11-01 13:38:49 +01:00
|
|
|
static VertexData* findByTypeName(std::string name);
|
2023-10-31 16:16:23 +01:00
|
|
|
virtual void init(Gfx::PGraphics graphics);
|
2023-12-12 11:37:00 +01:00
|
|
|
virtual void destroy();
|
2023-10-24 15:01:09 +02:00
|
|
|
protected:
|
2023-10-31 17:55:30 +01:00
|
|
|
virtual void resizeBuffers() = 0;
|
2023-10-31 16:16:23 +01:00
|
|
|
virtual void updateBuffers() = 0;
|
|
|
|
|
VertexData();
|
|
|
|
|
struct MeshletDescription
|
2023-10-24 15:01:09 +02:00
|
|
|
{
|
2023-12-14 09:04:23 +01:00
|
|
|
AABB boundingBox;
|
2023-10-24 15:01:09 +02:00
|
|
|
uint32_t vertexCount;
|
2023-10-31 16:16:23 +01:00
|
|
|
uint32_t primitiveCount;
|
2023-10-24 15:01:09 +02:00
|
|
|
uint32_t vertexOffset;
|
|
|
|
|
uint32_t primitiveOffset;
|
|
|
|
|
};
|
|
|
|
|
Map<std::string, MaterialData> materialData;
|
|
|
|
|
Map<MeshId, Array<MeshData>> meshData;
|
2023-11-01 13:38:49 +01:00
|
|
|
Map<MeshId, uint64> meshOffsets;
|
2023-11-09 22:15:51 +01:00
|
|
|
Map<MeshId, uint64> meshVertexCounts;
|
2023-10-31 16:16:23 +01:00
|
|
|
Array<MeshletDescription> meshlets;
|
|
|
|
|
Array<uint8> primitiveIndices;
|
|
|
|
|
Array<uint32> vertexIndices;
|
2023-11-26 11:27:39 +01:00
|
|
|
Array<uint32> indices;
|
2023-11-05 10:36:01 +01:00
|
|
|
Gfx::PGraphics graphics;
|
2023-11-01 23:12:30 +01:00
|
|
|
Gfx::ODescriptorLayout instanceDataLayout;
|
2023-10-24 15:01:09 +02:00
|
|
|
// for mesh shading
|
2023-11-01 23:12:30 +01:00
|
|
|
Gfx::OShaderBuffer meshletBuffer;
|
|
|
|
|
Gfx::OShaderBuffer vertexIndicesBuffer;
|
|
|
|
|
Gfx::OShaderBuffer primitiveIndicesBuffer;
|
2023-10-24 15:01:09 +02:00
|
|
|
// for legacy pipeline
|
2023-11-01 23:12:30 +01:00
|
|
|
Gfx::OIndexBuffer indexBuffer;
|
2023-10-29 09:20:23 +01:00
|
|
|
uint64 idCounter;
|
2023-10-31 17:55:30 +01:00
|
|
|
uint64 head;
|
|
|
|
|
uint64 verticesAllocated;
|
2023-10-31 16:16:23 +01:00
|
|
|
bool dirty;
|
2023-08-28 21:23:13 +02:00
|
|
|
};
|
2023-10-29 09:20:23 +01:00
|
|
|
}
|