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
+7 -7
View File
@@ -33,10 +33,10 @@ public:
virtual ~Graphics();
virtual void init(GraphicsInitializer initializer) = 0;
const QueueFamilyMapping getFamilyMapping() const
{
return queueMapping;
}
const QueueFamilyMapping getFamilyMapping() const
{
return queueMapping;
}
PShaderCompiler getShaderCompiler() const
{
@@ -73,10 +73,10 @@ public:
virtual PComputePipeline createComputePipeline(const ComputePipelineCreateInfo& createInfo) = 0;
virtual PSamplerState createSamplerState(const SamplerCreateInfo& createInfo) = 0;
virtual PDescriptorLayout createDescriptorLayout(const std::string& name = "") = 0;
virtual PPipelineLayout createPipelineLayout(PPipelineLayout baseLayout = nullptr) = 0;
virtual PDescriptorLayout createDescriptorLayout(const std::string& name = "") = 0;
virtual PPipelineLayout createPipelineLayout(PPipelineLayout baseLayout = nullptr) = 0;
virtual void copyTexture(Gfx::PTexture srcTexture, Gfx::PTexture dstTexture) = 0;
virtual void copyTexture(Gfx::PTexture srcTexture, Gfx::PTexture dstTexture) = 0;
PVertexBuffer getNullVertexBuffer();
@@ -76,11 +76,10 @@ void DepthPrepass::render()
layout->addDescriptorLayout(INDEX_SCENE_DATA, vertexData->getInstanceDataLayout());
layout->create();
GraphicsPipelineCreateInfo pipelineInfo;
Gfx::MeshPipelineCreateInfo pipelineInfo;
Gfx::PGraphicsPipeline pipeline = graphics->createGraphicsPipeline(pipelineInfo);
command->bindPipeline(pipeline);
vertexData->bindBuffers(command);
descriptorSets[INDEX_VERTEX_DATA] = vertexData->getVertexDataSet();
for (const auto&[_, instance]: materialData.instances)
{
+46 -37
View File
@@ -1,15 +1,13 @@
#include "StaticMeshVertexData.h"
#include "Graphics.h"
#include "Graphics/Enums.h"
using namespace Seele;
extern List<VertexData*> vertexDataList;
constexpr static uint64 NUM_DEFAULT_ELEMENTS = 1024;
StaticMeshVertexData::StaticMeshVertexData()
: head(0)
, verticesAllocated(NUM_DEFAULT_ELEMENTS)
{
vertexDataList.add(this);
}
@@ -17,39 +15,10 @@ StaticMeshVertexData::StaticMeshVertexData()
StaticMeshVertexData::~StaticMeshVertexData()
{}
StaticMeshVertexData* Seele::StaticMeshVertexData::getInstance()
StaticMeshVertexData* StaticMeshVertexData::getInstance()
{
return ;
}
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;
static StaticMeshVertexData instance;
return &instance;
}
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)
{
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 = {
.resourceData = {
.size = NUM_DEFAULT_ELEMENTS * sizeof(Vector),
.size = verticesAllocated * sizeof(Vector),
},
.stride = sizeof(Vector),
.bDynamic = true,
@@ -106,9 +102,15 @@ void StaticMeshVertexData::init(Gfx::PGraphics graphics)
normals = graphics->createShaderBuffer(createInfo);
tangents = 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);
texCoords = graphics->createShaderBuffer(createInfo);
positionData.resize(verticesAllocated);
texCoordsData.resize(verticesAllocated);
normalData.resize(verticesAllocated);
tangentData.resize(verticesAllocated);
biTangentData.resize(verticesAllocated);
}
void StaticMeshVertexData::updateBuffers()
@@ -133,4 +135,11 @@ void StaticMeshVertexData::updateBuffers()
.size = biTangentData.size() * sizeof(Vector),
.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();
virtual ~StaticMeshVertexData();
static StaticMeshVertexData* getInstance();
virtual MeshId allocateVertexData(uint64 numVertices) override;
void loadPositions(MeshId id, const Array<Vector>& data);
void loadTexCoords(MeshId id, const Array<Vector2>& data);
void loadNormals(MeshId id, const Array<Vector>& data);
void loadTangents(MeshId id, const Array<Vector>& data);
void loadBiTangents(MeshId id, const Array<Vector>& data);
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:
virtual void resizeBuffers() override;
virtual void updateBuffers() override;
Gfx::PShaderBuffer positions;
Array<Vector> positionData;
@@ -28,8 +32,7 @@ private:
Array<Vector> tangentData;
Gfx::PShaderBuffer biTangents;
Array<Vector> biTangentData;
Map<MeshId, uint64_t> meshOffsets;
uint64 head;
uint64 verticesAllocated;
Gfx::PDescriptorLayout descriptorLayout;
Gfx::PDescriptorSet descriptorSet;
};
}
+39 -12
View File
@@ -6,6 +6,8 @@
using namespace Seele;
constexpr static uint64 NUM_DEFAULT_ELEMENTS = 1024;
void VertexData::resetMeshData()
{
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();
uint32 head = 0;
while (head < meshlets.size())
uint32 currentMesh = 0;
while (currentMesh < loadedMeshlets.size())
{
uint32 numMeshlets = std::min<uint32>(512, meshlets.size() - head);
Array<MeshletDescription> desc(numMeshlets);
uint32 numMeshlets = std::min<uint32>(512, loadedMeshlets.size() - currentMesh);
uint32 meshletOffset = meshlets.size();
for (uint32 i = 0; i < numMeshlets; ++i)
{
Meshlet& m = meshlets[head + i];
vertexIndices.resize()
desc.add(MeshletDescription{
Meshlet& m = loadedMeshlets[currentMesh + i];
uint32 vertexOffset = vertexIndices.size();
vertexIndices.resize(vertexOffset + m.numVertices);
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(),
.vertexCount = m.numVertices,
.primitiveCount = m.numPrimitives,
.vertexOffset = vertexOffset,
.primitiveOffset = primitiveOffset,
});
}
meshData[id].add();
head += numMeshlets;
meshData[id].add(MeshData{
.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*> VertexData::getList()
@@ -105,6 +130,7 @@ List<VertexData*> VertexData::getList()
void Seele::VertexData::init(Gfx::PGraphics graphics)
{
this->graphics = graphics;
verticesAllocated = NUM_DEFAULT_ELEMENTS;
instanceDataLayout = graphics->createDescriptorLayout("VertexDataInstanceLayout");
instanceDataLayout->addDescriptorBinding(0, Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER);
if (Gfx::useMeshShading)
@@ -115,13 +141,14 @@ void Seele::VertexData::init(Gfx::PGraphics graphics)
instanceDataLayout->addDescriptorBinding(2, Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER);
meshletBuffer = graphics->createShaderBuffer(ShaderBufferCreateInfo{
.resourceData = {
.size = sizeof(MeshletData),
.size = sizeof(MeshletDescription) * 512,
.data = nullptr,
},
.bDynamic = true,
});
}
instanceDataLayout->create();
resizeBuffers();
}
VertexData::VertexData()
+7 -3
View File
@@ -55,23 +55,24 @@ public:
{
uint32 numMeshlets;
uint32 meshletOffset;
uint32 vertexOffset;
uint32 indicesOffset;
};
void resetMeshData();
void updateMesh(const Component::Transform& transform, const Component::Mesh& mesh);
void loadMesh(MeshId id, Array<Meshlet> meshlets);
void createDescriptors();
virtual MeshId allocateVertexData(uint64 numVertices) = 0;
MeshId allocateVertexData(uint64 numVertices);
virtual void bindBuffers(Gfx::PRenderCommand command) = 0;
virtual Gfx::PDescriptorLayout getVertexDataLayout() = 0;
virtual Gfx::PDescriptorSet getVertexDataSet() = 0;
virtual Gfx::PDescriptorLayout getInstanceDataLayout() = 0;
virtual std::string getTypeName() const = 0;
Gfx::PDescriptorLayout getInstanceDataLayout() { return instanceDataLayout; }
const Map<std::string, MaterialData>& getMaterialData() const { return materialData; }
const Array<MeshData>& getMeshData(MeshId id) { return meshData[id]; }
static List<VertexData*> getList();
virtual void init(Gfx::PGraphics graphics);
protected:
virtual void resizeBuffers() = 0;
virtual void updateBuffers() = 0;
VertexData();
struct MeshletAABB
@@ -91,6 +92,7 @@ protected:
};
Map<std::string, MaterialData> materialData;
Map<MeshId, Array<MeshData>> meshData;
Map<MeshId, uint64_t> meshOffsets;
Array<MeshletDescription> meshlets;
Array<uint8> primitiveIndices;
Array<uint32> vertexIndices;
@@ -101,6 +103,8 @@ protected:
// for legacy pipeline
Gfx::PIndexBuffer indexBuffer;
uint64 idCounter;
uint64 head;
uint64 verticesAllocated;
bool dirty;
};
}