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

162 lines
5.7 KiB
C++
Raw Normal View History

2023-08-28 21:23:13 +02:00
#pragma once
2023-10-24 15:01:09 +02:00
#include "Component/Transform.h"
2023-10-26 18:37:29 +02:00
#include "Containers/List.h"
2024-06-09 10:44:24 +02:00
#include "Graphics/Buffer.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"
2024-06-09 10:44:24 +02:00
#include "MeshData.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;
2024-06-09 10:44:24 +02:00
namespace Seele {
DECLARE_REF(MaterialInstance)
DECLARE_REF(Mesh)
struct MeshId {
2024-06-09 12:20:04 +02:00
uint64 id;
2024-06-25 08:59:09 +02:00
operator uint64() const { return id; }
2024-06-09 12:20:04 +02:00
std::strong_ordering operator<=>(const MeshId& other) const { return id <=> other.id; }
bool operator==(const MeshId& other) const { return id == other.id; }
2024-06-09 10:44:24 +02:00
};
class VertexData {
2024-06-09 12:20:04 +02:00
public:
struct DrawCallOffsets {
2024-07-08 13:46:49 +02:00
// offset into the instanceData and meshData arrays
2024-06-09 12:20:04 +02:00
uint32 instanceOffset = 0;
2024-06-18 19:19:05 +02:00
uint32 textureOffset = 0;
uint32 samplerOffset = 0;
uint32 floatOffset = 0;
2024-06-09 12:20:04 +02:00
};
2024-05-30 16:56:22 +02:00
2024-06-09 12:20:04 +02:00
struct MeshletCullingInfo {
2024-06-19 10:33:19 +02:00
uint32_t cull;
2024-06-09 12:20:04 +02:00
};
struct BatchedDrawCall {
PMaterialInstance materialInstance;
DrawCallOffsets offsets;
Array<InstanceData> instanceData;
Array<MeshData> instanceMeshData;
2024-07-10 21:07:10 +02:00
Array<Gfx::PBottomLevelAS> rayTracingData;
2024-06-09 12:20:04 +02:00
Array<uint32> cullingOffsets;
};
struct MaterialData {
PMaterial material;
Array<BatchedDrawCall> instances;
};
2024-06-20 21:57:26 +02:00
struct TransparentDraw {
PMaterialInstance matInst;
VertexData* vertexData;
DrawCallOffsets offsets;
Vector worldPosition;
};
2024-06-09 12:20:04 +02:00
void resetMeshData();
void updateMesh(entt::entity id, uint32 meshIndex, PMesh mesh, Component::Transform& transform);
void createDescriptors();
void loadMesh(MeshId id, Array<uint32> indices, Array<Meshlet> meshlets);
2024-06-20 21:57:26 +02:00
void commitMeshes();
2024-06-09 12:20:04 +02:00
MeshId allocateVertexData(uint64 numVertices);
uint64 getMeshOffset(MeshId id);
uint64 getMeshVertexCount(MeshId id);
virtual void serializeMesh(MeshId id, uint64 numVertices, ArchiveBuffer& buffer) = 0;
virtual void deserializeMesh(MeshId id, ArchiveBuffer& buffer) = 0;
virtual void bindBuffers(Gfx::PRenderCommand command) = 0;
virtual Gfx::PDescriptorLayout getVertexDataLayout() = 0;
virtual Gfx::PDescriptorSet getVertexDataSet() = 0;
virtual std::string getTypeName() const = 0;
virtual Gfx::PShaderBuffer getPositionBuffer() const = 0;
2024-06-13 15:43:03 +02:00
Gfx::PIndexBuffer getIndexBuffer() const { return indexBuffer; }
uint32* getIndexData() const { return indices.data(); }
2024-06-09 12:20:04 +02:00
Gfx::PDescriptorLayout getInstanceDataLayout() { return instanceDataLayout; }
Gfx::PDescriptorSet getInstanceDataSet() { return descriptorSet; }
const Array<MaterialData>& getMaterialData() const { return materialData; }
2024-06-20 21:57:26 +02:00
const Array<TransparentDraw>& getTransparentData() const { return transparentData; }
2024-07-10 21:07:10 +02:00
const Array<Gfx::PBottomLevelAS>& getRayTracingData() const { return rayTracingScene; }
2024-06-09 12:20:04 +02:00
const MeshData& getMeshData(MeshId id) { return meshData[id]; }
uint64 getIndicesOffset(uint32 meshletIndex) { return meshlets[meshletIndex].indicesOffset; }
uint64 getNumInstances() const { return instanceData.size(); }
static List<VertexData*> getList();
static VertexData* findByTypeName(std::string name);
virtual void init(Gfx::PGraphics graphics);
virtual void destroy();
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; }
protected:
virtual void resizeBuffers() = 0;
virtual void updateBuffers() = 0;
VertexData();
struct MeshletDescription {
AABB bounding;
2024-07-08 13:46:49 +02:00
// number of relevant entries in the vertexIndices array
2024-06-09 12:20:04 +02:00
uint32 vertexCount;
2024-07-08 13:46:49 +02:00
// number of relevant entries in the primitiveIndices array
2024-06-09 12:20:04 +02:00
uint32 primitiveCount;
2024-07-08 13:46:49 +02:00
// starting offset into the vertexIndices array
2024-06-09 12:20:04 +02:00
uint32 vertexOffset;
2024-07-08 13:46:49 +02:00
// starting offset into the primitiveIndices array
2024-06-09 12:20:04 +02:00
uint32 primitiveOffset;
Vector color;
2024-07-08 13:46:49 +02:00
// gets added to vertex indices so that they reference the global mesh bool
2024-06-09 12:20:04 +02:00
uint32 indicesOffset = 0;
};
std::mutex materialDataLock;
Array<MaterialData> materialData;
2024-06-20 21:57:26 +02:00
Array<TransparentDraw> transparentData;
2024-06-09 12:20:04 +02:00
std::mutex vertexDataLock;
2024-06-25 08:59:09 +02:00
Array<MeshData> meshData;
Array<uint64> meshOffsets;
Array<uint64> meshVertexCounts;
2024-06-20 21:57:26 +02:00
2024-06-09 12:20:04 +02:00
Array<MeshletDescription> meshlets;
Array<uint8> primitiveIndices;
Array<uint32> vertexIndices;
Array<uint32> indices;
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;
Gfx::PGraphics graphics;
Gfx::ODescriptorLayout instanceDataLayout;
// for mesh shading
Gfx::OShaderBuffer meshletBuffer;
Gfx::OShaderBuffer vertexIndicesBuffer;
Gfx::OShaderBuffer primitiveIndicesBuffer;
Gfx::OShaderBuffer cullingOffsetBuffer;
// for legacy pipeline
Gfx::OIndexBuffer indexBuffer;
// Material data
Array<InstanceData> instanceData;
2024-06-09 12:20:04 +02:00
Gfx::OShaderBuffer instanceBuffer;
2024-06-20 21:57:26 +02:00
Array<MeshData> instanceMeshData;
2024-06-09 12:20:04 +02:00
Gfx::OShaderBuffer instanceMeshDataBuffer;
2024-07-10 21:07:10 +02:00
Array<Gfx::PBottomLevelAS> rayTracingScene;
2024-06-20 21:57:26 +02:00
Array<InstanceData> transparentInstanceData;
Gfx::OShaderBuffer transparentInstanceDataBuffer;
Array<MeshData> transparentMeshData;
Gfx::OShaderBuffer transparentMeshDataBuffer;
2024-06-09 12:20:04 +02:00
Gfx::PDescriptorSet descriptorSet;
uint64 idCounter;
uint64 head;
uint64 verticesAllocated;
bool dirty;
2024-06-09 10:44:24 +02:00
};
} // namespace Seele