vertex data changes

This commit is contained in:
2023-10-31 17:55:30 +01:00
parent d53492d07b
commit 59b3893391
6 changed files with 107 additions and 65 deletions
@@ -76,11 +76,10 @@ void DepthPrepass::render()
layout->addDescriptorLayout(INDEX_SCENE_DATA, vertexData->getInstanceDataLayout()); layout->addDescriptorLayout(INDEX_SCENE_DATA, vertexData->getInstanceDataLayout());
layout->create(); layout->create();
GraphicsPipelineCreateInfo pipelineInfo; Gfx::MeshPipelineCreateInfo pipelineInfo;
Gfx::PGraphicsPipeline pipeline = graphics->createGraphicsPipeline(pipelineInfo); Gfx::PGraphicsPipeline pipeline = graphics->createGraphicsPipeline(pipelineInfo);
command->bindPipeline(pipeline); command->bindPipeline(pipeline);
vertexData->bindBuffers(command);
descriptorSets[INDEX_VERTEX_DATA] = vertexData->getVertexDataSet(); descriptorSets[INDEX_VERTEX_DATA] = vertexData->getVertexDataSet();
for (const auto&[_, instance]: materialData.instances) for (const auto&[_, instance]: materialData.instances)
{ {
+46 -37
View File
@@ -1,15 +1,13 @@
#include "StaticMeshVertexData.h" #include "StaticMeshVertexData.h"
#include "Graphics.h" #include "Graphics.h"
#include "Graphics/Enums.h"
using namespace Seele; using namespace Seele;
extern List<VertexData*> vertexDataList; extern List<VertexData*> vertexDataList;
constexpr static uint64 NUM_DEFAULT_ELEMENTS = 1024;
StaticMeshVertexData::StaticMeshVertexData() StaticMeshVertexData::StaticMeshVertexData()
: head(0)
, verticesAllocated(NUM_DEFAULT_ELEMENTS)
{ {
vertexDataList.add(this); vertexDataList.add(this);
} }
@@ -17,39 +15,10 @@ StaticMeshVertexData::StaticMeshVertexData()
StaticMeshVertexData::~StaticMeshVertexData() StaticMeshVertexData::~StaticMeshVertexData()
{} {}
StaticMeshVertexData* Seele::StaticMeshVertexData::getInstance() StaticMeshVertexData* StaticMeshVertexData::getInstance()
{ {
return ; static StaticMeshVertexData instance;
} return &instance;
MeshId StaticMeshVertexData::allocateVertexData(uint64 numVertices)
{
MeshId res{ idCounter++ };
meshOffsets[res] = head;
head += numVertices;
if (head > verticesAllocated)
{
ShaderBufferCreateInfo createInfo = {
.resourceData = {
.size = head * sizeof(Vector),
},
.stride = sizeof(Vector),
.bDynamic = true,
};
positions = graphics->createShaderBuffer(createInfo);
normals = graphics->createShaderBuffer(createInfo);
tangents = graphics->createShaderBuffer(createInfo);
biTangents = graphics->createShaderBuffer(createInfo);
createInfo.resourceData.size = head * sizeof(Vector2);
createInfo.stride = sizeof(Vector2);
texCoords = graphics->createShaderBuffer(createInfo);
}
positionData.resize(head);
texCoordsData.resize(head);
normalData.resize(head);
tangentData.resize(head);
biTangentData.resize(head);
return res;
} }
void StaticMeshVertexData::loadPositions(MeshId id, const Array<Vector>& data) void StaticMeshVertexData::loadPositions(MeshId id, const Array<Vector>& data)
@@ -95,9 +64,36 @@ void StaticMeshVertexData::loadBiTangents(MeshId id, const Array<Vector>& data)
void StaticMeshVertexData::init(Gfx::PGraphics graphics) void StaticMeshVertexData::init(Gfx::PGraphics graphics)
{ {
VertexData::init(graphics); VertexData::init(graphics);
descriptorLayout = graphics->createDescriptorLayout("StaticMeshDescriptorLayout");
descriptorLayout->addDescriptorBinding(0, Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER);
descriptorLayout->addDescriptorBinding(1, Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER);
descriptorLayout->addDescriptorBinding(2, Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER);
descriptorLayout->addDescriptorBinding(3, Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER);
descriptorLayout->addDescriptorBinding(4, Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER);
descriptorLayout->create();
descriptorSet = descriptorLayout->allocateDescriptorSet();
}
void StaticMeshVertexData::bindBuffers(Gfx::PRenderCommand)
{
// TODO: for legacy vertex buffer binding
}
Gfx::PDescriptorLayout StaticMeshVertexData::getVertexDataLayout()
{
return descriptorLayout;
}
Gfx::PDescriptorSet StaticMeshVertexData::getVertexDataSet()
{
return descriptorSet;
}
void StaticMeshVertexData::resizeBuffers()
{
ShaderBufferCreateInfo createInfo = { ShaderBufferCreateInfo createInfo = {
.resourceData = { .resourceData = {
.size = NUM_DEFAULT_ELEMENTS * sizeof(Vector), .size = verticesAllocated * sizeof(Vector),
}, },
.stride = sizeof(Vector), .stride = sizeof(Vector),
.bDynamic = true, .bDynamic = true,
@@ -106,9 +102,15 @@ void StaticMeshVertexData::init(Gfx::PGraphics graphics)
normals = graphics->createShaderBuffer(createInfo); normals = graphics->createShaderBuffer(createInfo);
tangents = graphics->createShaderBuffer(createInfo); tangents = graphics->createShaderBuffer(createInfo);
biTangents = graphics->createShaderBuffer(createInfo); biTangents = graphics->createShaderBuffer(createInfo);
createInfo.resourceData.size = NUM_DEFAULT_ELEMENTS * sizeof(Vector2); createInfo.resourceData.size = verticesAllocated * sizeof(Vector2);
createInfo.stride = sizeof(Vector2); createInfo.stride = sizeof(Vector2);
texCoords = graphics->createShaderBuffer(createInfo); texCoords = graphics->createShaderBuffer(createInfo);
positionData.resize(verticesAllocated);
texCoordsData.resize(verticesAllocated);
normalData.resize(verticesAllocated);
tangentData.resize(verticesAllocated);
biTangentData.resize(verticesAllocated);
} }
void StaticMeshVertexData::updateBuffers() void StaticMeshVertexData::updateBuffers()
@@ -133,4 +135,11 @@ void StaticMeshVertexData::updateBuffers()
.size = biTangentData.size() * sizeof(Vector), .size = biTangentData.size() * sizeof(Vector),
.data = (uint8*)biTangentData.data(), .data = (uint8*)biTangentData.data(),
}); });
descriptorSet = descriptorLayout->allocateDescriptorSet();
descriptorSet->updateBuffer(0, positions);
descriptorSet->updateBuffer(1, texCoords);
descriptorSet->updateBuffer(2, normals);
descriptorSet->updateBuffer(3, tangents);
descriptorSet->updateBuffer(4, biTangents);
descriptorSet->writeChanges();
} }
+7 -4
View File
@@ -9,14 +9,18 @@ public:
StaticMeshVertexData(); StaticMeshVertexData();
virtual ~StaticMeshVertexData(); virtual ~StaticMeshVertexData();
static StaticMeshVertexData* getInstance(); static StaticMeshVertexData* getInstance();
virtual MeshId allocateVertexData(uint64 numVertices) override;
void loadPositions(MeshId id, const Array<Vector>& data); void loadPositions(MeshId id, const Array<Vector>& data);
void loadTexCoords(MeshId id, const Array<Vector2>& data); void loadTexCoords(MeshId id, const Array<Vector2>& data);
void loadNormals(MeshId id, const Array<Vector>& data); void loadNormals(MeshId id, const Array<Vector>& data);
void loadTangents(MeshId id, const Array<Vector>& data); void loadTangents(MeshId id, const Array<Vector>& data);
void loadBiTangents(MeshId id, const Array<Vector>& data); void loadBiTangents(MeshId id, const Array<Vector>& data);
virtual void init(Gfx::PGraphics graphics) override; virtual void init(Gfx::PGraphics graphics) override;
virtual void bindBuffers(Gfx::PRenderCommand command) override;
virtual Gfx::PDescriptorLayout getVertexDataLayout() override;
virtual Gfx::PDescriptorSet getVertexDataSet() override;
virtual std::string getTypeName() const override { return "StaticMeshVertexData"; }
private: private:
virtual void resizeBuffers() override;
virtual void updateBuffers() override; virtual void updateBuffers() override;
Gfx::PShaderBuffer positions; Gfx::PShaderBuffer positions;
Array<Vector> positionData; Array<Vector> positionData;
@@ -28,8 +32,7 @@ private:
Array<Vector> tangentData; Array<Vector> tangentData;
Gfx::PShaderBuffer biTangents; Gfx::PShaderBuffer biTangents;
Array<Vector> biTangentData; Array<Vector> biTangentData;
Map<MeshId, uint64_t> meshOffsets; Gfx::PDescriptorLayout descriptorLayout;
uint64 head; Gfx::PDescriptorSet descriptorSet;
uint64 verticesAllocated;
}; };
} }
+39 -12
View File
@@ -6,6 +6,8 @@
using namespace Seele; using namespace Seele;
constexpr static uint64 NUM_DEFAULT_ELEMENTS = 1024;
void VertexData::resetMeshData() void VertexData::resetMeshData()
{ {
materialData.clear(); materialData.clear();
@@ -28,27 +30,37 @@ void VertexData::updateMesh(const Component::Transform& transform, const Compone
}); });
} }
void VertexData::loadMesh(MeshId id, Array<Meshlet> meshlets) void VertexData::loadMesh(MeshId id, Array<Meshlet> loadedMeshlets)
{ {
meshData[id].clear(); meshData[id].clear();
uint32 head = 0; uint32 currentMesh = 0;
while (head < meshlets.size()) while (currentMesh < loadedMeshlets.size())
{ {
uint32 numMeshlets = std::min<uint32>(512, meshlets.size() - head); uint32 numMeshlets = std::min<uint32>(512, loadedMeshlets.size() - currentMesh);
Array<MeshletDescription> desc(numMeshlets); uint32 meshletOffset = meshlets.size();
for (uint32 i = 0; i < numMeshlets; ++i) for (uint32 i = 0; i < numMeshlets; ++i)
{ {
Meshlet& m = meshlets[head + i]; Meshlet& m = loadedMeshlets[currentMesh + i];
uint32 vertexOffset = vertexIndices.size();
vertexIndices.resize() vertexIndices.resize(vertexOffset + m.numVertices);
desc.add(MeshletDescription{ std::memcpy(vertexIndices.data() + vertexOffset, m.uniqueVertices, sizeof(m.uniqueVertices));
uint32 primitiveOffset = primitiveIndices.size();
primitiveIndices.resize(primitiveOffset + (m.numPrimitives * 3));
std::memcpy(primitiveIndices.data() + primitiveOffset, m.primitiveLayout, sizeof(m.primitiveLayout));
meshlets.add(MeshletDescription{
.boundingBox = MeshletAABB(), .boundingBox = MeshletAABB(),
.vertexCount = m.numVertices, .vertexCount = m.numVertices,
.primitiveCount = m.numPrimitives, .primitiveCount = m.numPrimitives,
.vertexOffset = vertexOffset,
.primitiveOffset = primitiveOffset,
}); });
} }
meshData[id].add(); meshData[id].add(MeshData{
head += numMeshlets; .numMeshlets = numMeshlets,
.meshletOffset = meshletOffset,
.indicesOffset = static_cast<uint32>(meshOffsets[id]),
});
currentMesh += numMeshlets;
} }
} }
@@ -95,6 +107,19 @@ void VertexData::createDescriptors()
} }
} }
MeshId VertexData::allocateVertexData(uint64 numVertices)
{
MeshId res{ idCounter++ };
meshOffsets[res] = head;
head += numVertices;
if (head > verticesAllocated)
{
verticesAllocated = head;
resizeBuffers();
}
return res;
}
List<VertexData*> vertexDataList; List<VertexData*> vertexDataList;
List<VertexData*> VertexData::getList() List<VertexData*> VertexData::getList()
@@ -105,6 +130,7 @@ List<VertexData*> VertexData::getList()
void Seele::VertexData::init(Gfx::PGraphics graphics) void Seele::VertexData::init(Gfx::PGraphics graphics)
{ {
this->graphics = graphics; this->graphics = graphics;
verticesAllocated = NUM_DEFAULT_ELEMENTS;
instanceDataLayout = graphics->createDescriptorLayout("VertexDataInstanceLayout"); instanceDataLayout = graphics->createDescriptorLayout("VertexDataInstanceLayout");
instanceDataLayout->addDescriptorBinding(0, Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER); instanceDataLayout->addDescriptorBinding(0, Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER);
if (Gfx::useMeshShading) if (Gfx::useMeshShading)
@@ -115,13 +141,14 @@ void Seele::VertexData::init(Gfx::PGraphics graphics)
instanceDataLayout->addDescriptorBinding(2, Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER); instanceDataLayout->addDescriptorBinding(2, Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER);
meshletBuffer = graphics->createShaderBuffer(ShaderBufferCreateInfo{ meshletBuffer = graphics->createShaderBuffer(ShaderBufferCreateInfo{
.resourceData = { .resourceData = {
.size = sizeof(MeshletData), .size = sizeof(MeshletDescription) * 512,
.data = nullptr, .data = nullptr,
}, },
.bDynamic = true, .bDynamic = true,
}); });
} }
instanceDataLayout->create(); instanceDataLayout->create();
resizeBuffers();
} }
VertexData::VertexData() VertexData::VertexData()
+7 -3
View File
@@ -55,23 +55,24 @@ public:
{ {
uint32 numMeshlets; uint32 numMeshlets;
uint32 meshletOffset; uint32 meshletOffset;
uint32 vertexOffset; uint32 indicesOffset;
}; };
void resetMeshData(); void resetMeshData();
void updateMesh(const Component::Transform& transform, const Component::Mesh& mesh); void updateMesh(const Component::Transform& transform, const Component::Mesh& mesh);
void loadMesh(MeshId id, Array<Meshlet> meshlets); void loadMesh(MeshId id, Array<Meshlet> meshlets);
void createDescriptors(); void createDescriptors();
virtual MeshId allocateVertexData(uint64 numVertices) = 0; MeshId allocateVertexData(uint64 numVertices);
virtual void bindBuffers(Gfx::PRenderCommand command) = 0; virtual void bindBuffers(Gfx::PRenderCommand command) = 0;
virtual Gfx::PDescriptorLayout getVertexDataLayout() = 0; virtual Gfx::PDescriptorLayout getVertexDataLayout() = 0;
virtual Gfx::PDescriptorSet getVertexDataSet() = 0; virtual Gfx::PDescriptorSet getVertexDataSet() = 0;
virtual Gfx::PDescriptorLayout getInstanceDataLayout() = 0;
virtual std::string getTypeName() const = 0; virtual std::string getTypeName() const = 0;
Gfx::PDescriptorLayout getInstanceDataLayout() { return instanceDataLayout; }
const Map<std::string, MaterialData>& getMaterialData() const { return materialData; } const Map<std::string, MaterialData>& getMaterialData() const { return materialData; }
const Array<MeshData>& getMeshData(MeshId id) { return meshData[id]; } const Array<MeshData>& getMeshData(MeshId id) { return meshData[id]; }
static List<VertexData*> getList(); static List<VertexData*> getList();
virtual void init(Gfx::PGraphics graphics); virtual void init(Gfx::PGraphics graphics);
protected: protected:
virtual void resizeBuffers() = 0;
virtual void updateBuffers() = 0; virtual void updateBuffers() = 0;
VertexData(); VertexData();
struct MeshletAABB struct MeshletAABB
@@ -91,6 +92,7 @@ protected:
}; };
Map<std::string, MaterialData> materialData; Map<std::string, MaterialData> materialData;
Map<MeshId, Array<MeshData>> meshData; Map<MeshId, Array<MeshData>> meshData;
Map<MeshId, uint64_t> meshOffsets;
Array<MeshletDescription> meshlets; Array<MeshletDescription> meshlets;
Array<uint8> primitiveIndices; Array<uint8> primitiveIndices;
Array<uint32> vertexIndices; Array<uint32> vertexIndices;
@@ -101,6 +103,8 @@ protected:
// for legacy pipeline // for legacy pipeline
Gfx::PIndexBuffer indexBuffer; Gfx::PIndexBuffer indexBuffer;
uint64 idCounter; uint64 idCounter;
uint64 head;
uint64 verticesAllocated;
bool dirty; bool dirty;
}; };
} }