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

157 lines
4.7 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"
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-23 18:26:54 +01:00
#include "Meshlet.h"
2024-06-07 09:19:47 +02:00
#include <entt/entt.hpp>
2023-08-28 21:23:13 +02:00
2024-05-06 18:36:16 +02:00
constexpr uint64 MAX_TEXCOORDS = 8;
2023-08-28 21:23:13 +02:00
namespace Seele
{
2024-05-30 16:56:22 +02:00
DECLARE_REF(Mesh)
struct MeshId
{
2023-10-24 15:01:09 +02:00
uint64 id;
2024-05-30 16:56:22 +02:00
std::strong_ordering operator<=>(const MeshId &other) const
2023-10-31 16:16:23 +01:00
{
2024-05-30 16:56:22 +02:00
return id <=> other.id;
2023-10-31 16:16:23 +01:00
}
2024-05-30 16:56:22 +02:00
bool operator==(const MeshId &other) const
2023-11-22 13:18:54 +01:00
{
2024-05-30 16:56:22 +02:00
return id == other.id;
2023-11-22 13:18:54 +01:00
}
2024-05-30 16:56:22 +02:00
};
class VertexData
{
public:
2023-10-24 15:01:09 +02:00
struct InstanceData
{
2024-05-30 16:56:22 +02:00
Matrix4 transformMatrix;
Matrix4 inverseTransformMatrix;
2023-10-24 15:01:09 +02:00
};
2023-11-26 11:27:39 +01:00
struct MeshData
{
2024-05-30 16:56:22 +02:00
AABB bounding;
uint32 numMeshlets = 0;
uint32 meshletOffset = 0;
uint32 firstIndex = 0;
uint32 numIndices = 0;
2023-11-26 11:27:39 +01:00
};
2024-05-12 19:36:32 +02:00
struct DrawCallOffsets
{
2024-05-30 16:56:22 +02:00
uint32 instanceOffset = 0;
};
struct MeshletCullingInfo
{
uint64_t cull[256 / 64];
2024-05-12 19:36:32 +02:00
};
struct BatchedDrawCall
2023-10-24 15:01:09 +02:00
{
2024-05-30 16:56:22 +02:00
PMaterialInstance materialInstance;
DrawCallOffsets offsets;
Array<InstanceData> instanceData;
Array<MeshData> instanceMeshData;
2024-06-07 09:19:47 +02:00
Array<uint32> cullingOffsets;
2023-10-24 15:01:09 +02:00
};
struct MaterialData
{
2024-05-30 16:56:22 +02:00
PMaterial material;
Array<BatchedDrawCall> instances;
2023-10-24 15:01:09 +02:00
};
void resetMeshData();
2024-06-07 09:19:47 +02:00
void updateMesh(entt::entity id, uint32 meshIndex, PMesh mesh, Component::Transform &transform);
2023-10-24 15:01:09 +02:00
void createDescriptors();
2024-04-07 16:33:32 +02: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);
2024-05-30 16:56:22 +02: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; }
Gfx::PDescriptorSet getInstanceDataSet() { return descriptorSet; }
2024-05-30 16:56:22 +02:00
const Array<MaterialData> &getMaterialData() const { return materialData; }
const MeshData &getMeshData(MeshId id) { return meshData[id]; }
2024-05-22 10:30:45 +02:00
uint64 getIndicesOffset(uint32 meshletIndex) { return meshlets[meshletIndex].indicesOffset; }
2024-05-30 16:56:22 +02:00
uint64 getNumInstances() const { return instanceData.size(); }
static List<VertexData *> getList();
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();
2024-05-30 16:56:22 +02:00
2024-06-07 09:19:47 +02:00
struct CullingMapping
{
uint64 instanceId;
uint32 cullingOffset;
};
static CullingMapping getCullingMapping(entt::entity id, uint32 meshIndex, uint32 numMeshlets);
static uint64 getInstanceCount() { return instanceCount; }
static uint64 getMeshletCount() { return meshletCount; }
2024-05-30 16:56:22 +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
{
2024-05-30 16:56:22 +02:00
AABB bounding;
uint32 vertexCount;
uint32 primitiveCount;
uint32 vertexOffset;
uint32 primitiveOffset;
Vector color;
uint32 indicesOffset = 0;
2023-10-24 15:01:09 +02:00
};
2023-12-17 16:16:46 +01:00
std::mutex materialDataLock;
2024-05-09 08:41:46 +02:00
Array<MaterialData> materialData;
2024-05-01 14:10:52 +02:00
std::mutex vertexDataLock;
2024-05-12 19:36:32 +02:00
Map<MeshId, 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;
2024-04-24 23:25:34 +02:00
Array<uint8> primitiveIndices;
2024-04-07 16:33:32 +02:00
Array<uint32> vertexIndices;
Array<uint32> indices;
2024-06-07 09:19:47 +02:00
struct MeshMapping
{
entt::entity id;
uint32 meshId;
auto operator<=>(const MeshMapping& other) const = default;
};
static Map<MeshMapping, CullingMapping> instanceIdMap;
static uint64 instanceCount;
static uint64 meshletCount;
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;
2024-05-30 16:56:22 +02:00
Gfx::OShaderBuffer cullingOffsetBuffer;
2023-10-24 15:01:09 +02:00
// for legacy pipeline
2023-11-01 23:12:30 +01:00
Gfx::OIndexBuffer indexBuffer;
// Material data
Array<InstanceData> instanceData;
Gfx::OShaderBuffer instanceBuffer;
Array<MeshData> instanceMeshData;
Gfx::OShaderBuffer instanceMeshDataBuffer;
Gfx::PDescriptorSet descriptorSet;
2023-10-29 09:20:23 +01:00
uint64 idCounter;
2024-05-30 16:56:22 +02:00
uint64 head;
uint64 verticesAllocated;
2023-10-31 16:16:23 +01:00
bool dirty;
2024-05-30 16:56:22 +02:00
};
2023-10-29 09:20:23 +01:00
}