Files
Seele/src/Engine/Graphics/VertexData.h
T

118 lines
3.6 KiB
C++
Raw Normal View History

2023-08-28 21:23:13 +02:00
#pragma once
#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"
#include "Graphics/Resources.h"
#include "Graphics/Descriptor.h"
#include "Graphics/Buffer.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;
}
};
struct Meshlet
{
2023-11-01 13:38:49 +01:00
StaticArray<uint32, Gfx::numVerticesPerMeshlet> uniqueVertices; // unique vertiex indices in the vertex data
StaticArray<uint8, Gfx::numPrimitivesPerMeshlet * 3> primitiveLayout; // indices into the uniqueVertices array, only uint8 needed
2023-10-31 16:16:23 +01:00
uint32 numVertices;
uint32 numPrimitives;
2023-11-05 10:36:01 +01:00
void save(ArchiveBuffer& buffer) const;
2023-11-01 13:38:49 +01:00
void load(ArchiveBuffer& buffer);
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;
};
struct MeshInstanceData
{
MeshId id;
InstanceData instance;
Gfx::PIndexBuffer indexBuffer;
2023-10-24 15:01:09 +02:00
};
struct MaterialInstanceData
{
PMaterialInstance materialInstance;
Gfx::PDescriptorSet descriptorSet;
uint32 numMeshes; // not necessarily equal to meshes.size() if a MeshId has multiple meshes
2023-10-24 15:01:09 +02:00
Array<MeshInstanceData> meshes;
};
struct MaterialData
{
PMaterial material;
Map<uint64, MaterialInstanceData> instances;
};
struct MeshData
{
uint32 numMeshlets;
uint32 meshletOffset;
2023-10-31 17:55:30 +01:00
uint32 indicesOffset;
2023-10-24 15:01:09 +02:00
};
void resetMeshData();
2023-11-06 14:47:21 +01:00
void updateMesh(const Component::Transform& transform, PMesh mesh);
2023-10-31 16:16:23 +01:00
void loadMesh(MeshId id, Array<Meshlet> meshlets);
2023-10-24 15:01:09 +02:00
void createDescriptors();
2023-10-31 17:55:30 +01:00
MeshId allocateVertexData(uint64 numVertices);
uint32 getMeshOffset(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-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-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();
2023-10-24 15:01:09 +02:00
struct MeshletAABB
{
Vector min;
float pad0;
Vector max;
float pad1;
};
2023-10-31 16:16:23 +01:00
struct MeshletDescription
2023-10-24 15:01:09 +02:00
{
MeshletAABB boundingBox;
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-10-31 16:16:23 +01:00
Array<MeshletDescription> meshlets;
Array<uint8> primitiveIndices;
Array<uint32> vertexIndices;
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
}