From 59b3893391c679fd2a40c7ab83d47a875e2cc736 Mon Sep 17 00:00:00 2001 From: Dynamitos Date: Tue, 31 Oct 2023 17:55:30 +0100 Subject: [PATCH] vertex data changes --- src/Engine/Graphics/Graphics.h | 14 ++-- .../Graphics/RenderPass/DepthPrepass.cpp | 3 +- src/Engine/Graphics/StaticMeshVertexData.cpp | 83 ++++++++++--------- src/Engine/Graphics/StaticMeshVertexData.h | 11 ++- src/Engine/Graphics/VertexData.cpp | 51 +++++++++--- src/Engine/Graphics/VertexData.h | 10 ++- 6 files changed, 107 insertions(+), 65 deletions(-) diff --git a/src/Engine/Graphics/Graphics.h b/src/Engine/Graphics/Graphics.h index 76ccec8..57c9764 100644 --- a/src/Engine/Graphics/Graphics.h +++ b/src/Engine/Graphics/Graphics.h @@ -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(); diff --git a/src/Engine/Graphics/RenderPass/DepthPrepass.cpp b/src/Engine/Graphics/RenderPass/DepthPrepass.cpp index 7256611..991e1c4 100644 --- a/src/Engine/Graphics/RenderPass/DepthPrepass.cpp +++ b/src/Engine/Graphics/RenderPass/DepthPrepass.cpp @@ -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) { diff --git a/src/Engine/Graphics/StaticMeshVertexData.cpp b/src/Engine/Graphics/StaticMeshVertexData.cpp index 5f3cbc1..dd486d7 100644 --- a/src/Engine/Graphics/StaticMeshVertexData.cpp +++ b/src/Engine/Graphics/StaticMeshVertexData.cpp @@ -1,15 +1,13 @@ #include "StaticMeshVertexData.h" #include "Graphics.h" +#include "Graphics/Enums.h" using namespace Seele; extern List 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& data) @@ -95,9 +64,36 @@ void StaticMeshVertexData::loadBiTangents(MeshId id, const Array& 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(); } diff --git a/src/Engine/Graphics/StaticMeshVertexData.h b/src/Engine/Graphics/StaticMeshVertexData.h index a1b7f95..5bea49f 100644 --- a/src/Engine/Graphics/StaticMeshVertexData.h +++ b/src/Engine/Graphics/StaticMeshVertexData.h @@ -9,14 +9,18 @@ public: StaticMeshVertexData(); virtual ~StaticMeshVertexData(); static StaticMeshVertexData* getInstance(); - virtual MeshId allocateVertexData(uint64 numVertices) override; void loadPositions(MeshId id, const Array& data); void loadTexCoords(MeshId id, const Array& data); void loadNormals(MeshId id, const Array& data); void loadTangents(MeshId id, const Array& data); void loadBiTangents(MeshId id, const Array& 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 positionData; @@ -28,8 +32,7 @@ private: Array tangentData; Gfx::PShaderBuffer biTangents; Array biTangentData; - Map meshOffsets; - uint64 head; - uint64 verticesAllocated; + Gfx::PDescriptorLayout descriptorLayout; + Gfx::PDescriptorSet descriptorSet; }; } diff --git a/src/Engine/Graphics/VertexData.cpp b/src/Engine/Graphics/VertexData.cpp index 9141a87..ce5200e 100644 --- a/src/Engine/Graphics/VertexData.cpp +++ b/src/Engine/Graphics/VertexData.cpp @@ -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 meshlets) +void VertexData::loadMesh(MeshId id, Array loadedMeshlets) { meshData[id].clear(); - uint32 head = 0; - while (head < meshlets.size()) + uint32 currentMesh = 0; + while (currentMesh < loadedMeshlets.size()) { - uint32 numMeshlets = std::min(512, meshlets.size() - head); - Array desc(numMeshlets); + uint32 numMeshlets = std::min(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(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 vertexDataList; List VertexData::getList() @@ -105,6 +130,7 @@ List 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() diff --git a/src/Engine/Graphics/VertexData.h b/src/Engine/Graphics/VertexData.h index b07b16e..4b8a3c3 100644 --- a/src/Engine/Graphics/VertexData.h +++ b/src/Engine/Graphics/VertexData.h @@ -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 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& getMaterialData() const { return materialData; } const Array& getMeshData(MeshId id) { return meshData[id]; } static List 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 materialData; Map> meshData; + Map meshOffsets; Array meshlets; Array primitiveIndices; Array vertexIndices; @@ -101,6 +103,8 @@ protected: // for legacy pipeline Gfx::PIndexBuffer indexBuffer; uint64 idCounter; + uint64 head; + uint64 verticesAllocated; bool dirty; }; }