Mesh and Material Serializations

This commit is contained in:
Dynamitos
2023-11-01 13:38:49 +01:00
parent 4e7f4a56b8
commit 5a9cb13e74
20 changed files with 233 additions and 111 deletions
+1
View File
@@ -10,6 +10,7 @@ target_sources(Engine
Graphics.h
Graphics.cpp
Initializer.h
Initializer.cpp
Mesh.h
Mesh.cpp
RenderTarget.h
+3 -3
View File
@@ -79,9 +79,9 @@ public:
{
descriptorBindings.resize(other.descriptorBindings.size());
for(uint32 i = 0; i < descriptorBindings.size(); ++i)
{
descriptorBindings[i] = other.descriptorBindings[i];
}
{
descriptorBindings[i] = other.descriptorBindings[i];
}
}
DescriptorLayout& operator=(const DescriptorLayout& other)
{
+48
View File
@@ -0,0 +1,48 @@
#include "Initializer.h"
using namespace Seele;
using namespace Seele::Graphics;
LegacyPipelineCreateInfo::LegacyPipelineCreateInfo()
: topology(Gfx::SE_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST)
, multisampleState(MultisampleState{
.samples = 1,
})
, rasterizationState(RasterizationState{
.polygonMode = Gfx::SE_POLYGON_MODE_FILL,
.cullMode = Gfx::SE_CULL_MODE_BACK_BIT,
.frontFace = Gfx::SE_FRONT_FACE_COUNTER_CLOCKWISE,
})
, depthStencilState(DepthStencilState{
.depthTestEnable = true,
.depthWriteEnable = true,
.stencilTestEnable = false,
.depthCompareOp = Gfx::SE_COMPARE_OP_LESS_OR_EQUAL,
.minDepthBounds = 0.0f,
.maxDepthBounds = 1.0f,
})
, colorBlend(ColorBlendState{
.logicOpEnable = false,
.attachmentCount = 0,
.blendAttachments = {
ColorBlendState::BlendAttachment{
.colorWriteMask =
Gfx::SE_COLOR_COMPONENT_R_BIT |
Gfx::SE_COLOR_COMPONENT_G_BIT |
Gfx::SE_COLOR_COMPONENT_B_BIT |
Gfx::SE_COLOR_COMPONENT_A_BIT,
}
},
.blendConstants = {
1.0f,
1.0f,
1.0f,
1.0f,
},
})
{
}
Seele::Gfx::LegacyPipelineCreateInfo::~LegacyPipelineCreateInfo()
{
}
+4 -46
View File
@@ -2,6 +2,7 @@
#include "Enums.h"
#include "Containers/Map.h"
#include "Math/Math.h"
#include "Shader.h"
namespace Seele
{
@@ -189,15 +190,9 @@ struct ColorBlendState
} blendAttachments[16];
float blendConstants[4];
};
DECLARE_REF(VertexDeclaration)
DECLARE_REF(VertexShader)
DECLARE_REF(FragmentShader)
DECLARE_REF(TaskShader)
DECLARE_REF(MeshShader)
DECLARE_REF(ComputeShader)
DECLARE_REF(PipelineLayout)
DECLARE_REF(RenderPass)
DECLARE_REF(PipelineLayout)
struct LegacyPipelineCreateInfo
{
PVertexDeclaration vertexDeclaration;
@@ -210,45 +205,8 @@ struct LegacyPipelineCreateInfo
RasterizationState rasterizationState;
DepthStencilState depthStencilState;
ColorBlendState colorBlend;
LegacyPipelineCreateInfo()
: topology(Gfx::SE_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST)
, multisampleState(MultisampleState{
.samples = 1,
})
, rasterizationState(RasterizationState{
.polygonMode = Gfx::SE_POLYGON_MODE_FILL,
.cullMode = Gfx::SE_CULL_MODE_BACK_BIT,
.frontFace = Gfx::SE_FRONT_FACE_COUNTER_CLOCKWISE,
})
, depthStencilState(DepthStencilState{
.depthTestEnable = true,
.depthWriteEnable = true,
.stencilTestEnable = false,
.depthCompareOp = Gfx::SE_COMPARE_OP_LESS_OR_EQUAL,
.minDepthBounds = 0.0f,
.maxDepthBounds = 1.0f,
})
, colorBlend(ColorBlendState{
.logicOpEnable = false,
.attachmentCount = 0,
.blendAttachments = {
ColorBlendState::BlendAttachment{
.colorWriteMask =
Gfx::SE_COLOR_COMPONENT_R_BIT |
Gfx::SE_COLOR_COMPONENT_G_BIT |
Gfx::SE_COLOR_COMPONENT_B_BIT |
Gfx::SE_COLOR_COMPONENT_A_BIT,
}
},
.blendConstants = {
1.0f,
1.0f,
1.0f,
1.0f,
},
})
{
}
LegacyPipelineCreateInfo();
~LegacyPipelineCreateInfo();
};
struct MeshPipelineCreateInfo
+20
View File
@@ -10,3 +10,23 @@ Mesh::Mesh()
Mesh::~Mesh()
{
}
void Mesh::save(ArchiveBuffer& buffer)
{
Serialization::save(buffer, vertexData->getTypeName());
Serialization::save(buffer, vertexCount);
Serialization::save(buffer, meshlets);
vertexData->serializeMesh(id, vertexCount, buffer);
}
void Mesh::load(ArchiveBuffer& buffer)
{
std::string typeName;
Serialization::load(buffer, typeName);
Serialization::load(buffer, vertexCount);
vertexData = VertexData::findByTypeName(typeName);
Serialization::load(buffer, meshlets);
id = vertexData->allocateVertexData(vertexCount);
vertexData->loadMesh(id, meshlets);
vertexData->deserializeMesh(id, buffer);
}
+5 -1
View File
@@ -12,7 +12,11 @@ public:
VertexData* vertexData;
MeshId id;
PMaterialInstanceAsset referencedMaterial;
uint64 vertexCount;
PMaterialInstance referencedMaterial;
Array<Meshlet> meshlets;
void save(ArchiveBuffer& buffer);
void load(ArchiveBuffer& buffer);
private:
};
DEFINE_REF(Mesh)
+1
View File
@@ -1,5 +1,6 @@
#pragma once
#include "Enums.h"
#include "CRC.h"
namespace Seele
{
@@ -61,6 +61,45 @@ void StaticMeshVertexData::loadBiTangents(MeshId id, const Array<Vector>& data)
dirty = true;
}
void StaticMeshVertexData::serializeMesh(MeshId id, uint64 numVertices, ArchiveBuffer& buffer)
{
uint64 offset = meshOffsets[id];
Array<Vector> pos(numVertices);
Array<Vector2> tex(numVertices);
Array<Vector> nor(numVertices);
Array<Vector> tan(numVertices);
Array<Vector> bit(numVertices);
std::copy(positionData.begin() + offset, positionData.begin() + offset + numVertices, pos.begin());
std::copy(texCoordsData.begin() + offset, texCoordsData.begin() + offset + numVertices, tex.begin());
std::copy(normalData.begin() + offset, normalData.begin() + offset + numVertices, nor.begin());
std::copy(tangentData.begin() + offset, tangentData.begin() + offset + numVertices, tan.begin());
std::copy(biTangentData.begin() + offset, biTangentData.begin() + offset + numVertices, bit.begin());
Serialization::save(buffer, pos);
Serialization::save(buffer, tex);
Serialization::save(buffer, nor);
Serialization::save(buffer, tan);
Serialization::save(buffer, bit);
}
void StaticMeshVertexData::deserializeMesh(MeshId id, ArchiveBuffer& buffer)
{
Array<Vector> pos;
Array<Vector2> tex;
Array<Vector> nor;
Array<Vector> tan;
Array<Vector> bit;
Serialization::load(buffer, pos);
Serialization::load(buffer, tex);
Serialization::load(buffer, nor);
Serialization::load(buffer, tan);
Serialization::load(buffer, bit);
loadPositions(id, pos);
loadTexCoords(id, tex);
loadNormals(id, nor);
loadTangents(id, tan);
loadBiTangents(id, bit);
}
void StaticMeshVertexData::init(Gfx::PGraphics graphics)
{
VertexData::init(graphics);
@@ -14,6 +14,8 @@ public:
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 serializeMesh(MeshId id, uint64 numVertices, ArchiveBuffer& buffer) override;
virtual void deserializeMesh(MeshId id, ArchiveBuffer& buffer) override;
virtual void init(Gfx::PGraphics graphics) override;
virtual void bindBuffers(Gfx::PRenderCommand command) override;
virtual Gfx::PDescriptorLayout getVertexDataLayout() override;
+30 -2
View File
@@ -45,10 +45,10 @@ void VertexData::loadMesh(MeshId id, Array<Meshlet> loadedMeshlets)
Meshlet& m = loadedMeshlets[currentMesh + i];
uint32 vertexOffset = vertexIndices.size();
vertexIndices.resize(vertexOffset + m.numVertices);
std::memcpy(vertexIndices.data() + vertexOffset, m.uniqueVertices, sizeof(m.uniqueVertices));
std::memcpy(vertexIndices.data() + vertexOffset, m.uniqueVertices.data(), sizeof(m.uniqueVertices));
uint32 primitiveOffset = primitiveIndices.size();
primitiveIndices.resize(primitiveOffset + (m.numPrimitives * 3));
std::memcpy(primitiveIndices.data() + primitiveOffset, m.primitiveLayout, sizeof(m.primitiveLayout));
std::memcpy(primitiveIndices.data() + primitiveOffset, m.primitiveLayout.data(), sizeof(m.primitiveLayout));
meshlets.add(MeshletDescription{
.boundingBox = MeshletAABB(),
.vertexCount = m.numVertices,
@@ -156,6 +156,18 @@ List<VertexData*> VertexData::getList()
return vertexDataList;
}
VertexData* Seele::VertexData::findByTypeName(std::string name)
{
for (auto vd : vertexDataList)
{
if (vd->getTypeName() == name)
{
return vd;
}
}
return nullptr;
}
void Seele::VertexData::init(Gfx::PGraphics graphics)
{
this->graphics = graphics;
@@ -182,3 +194,19 @@ VertexData::VertexData()
, dirty(false)
{
}
void Meshlet::save(ArchiveBuffer& buffer)
{
Serialization::save(buffer, uniqueVertices);
Serialization::save(buffer, primitiveLayout);
Serialization::save(buffer, numVertices);
Serialization::save(buffer, numPrimitives);
}
void Meshlet::load(ArchiveBuffer& buffer)
{
Serialization::load(buffer, uniqueVertices);
Serialization::load(buffer, primitiveLayout);
Serialization::load(buffer, numVertices);
Serialization::load(buffer, numPrimitives);
}
+8 -3
View File
@@ -22,10 +22,12 @@ struct MeshId
};
struct Meshlet
{
uint32 uniqueVertices[Gfx::numVerticesPerMeshlet]; // unique vertiex indices in the vertex data
uint8 primitiveLayout[Gfx::numPrimitivesPerMeshlet * 3]; // indices into the uniqueVertices array, only uint8 needed
StaticArray<uint32, Gfx::numVerticesPerMeshlet> uniqueVertices; // unique vertiex indices in the vertex data
StaticArray<uint8, Gfx::numPrimitivesPerMeshlet * 3> primitiveLayout; // indices into the uniqueVertices array, only uint8 needed
uint32 numVertices;
uint32 numPrimitives;
void save(ArchiveBuffer& buffer);
void load(ArchiveBuffer& buffer);
};
class VertexData
{
@@ -62,6 +64,8 @@ public:
void loadMesh(MeshId id, Array<Meshlet> meshlets);
void createDescriptors();
MeshId allocateVertexData(uint64 numVertices);
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;
@@ -70,6 +74,7 @@ public:
const Map<std::string, MaterialData>& getMaterialData() const { return materialData; }
const Array<MeshData>& getMeshData(MeshId id) { return meshData[id]; }
static List<VertexData*> getList();
static VertexData* findByTypeName(std::string name);
virtual void init(Gfx::PGraphics graphics);
protected:
virtual void resizeBuffers() = 0;
@@ -92,7 +97,7 @@ protected:
};
Map<std::string, MaterialData> materialData;
Map<MeshId, Array<MeshData>> meshData;
Map<MeshId, uint64_t> meshOffsets;
Map<MeshId, uint64> meshOffsets;
Array<MeshletDescription> meshlets;
Array<uint8> primitiveIndices;
Array<uint32> vertexIndices;
@@ -1,5 +1,6 @@
#include "Initializer.h"
#include <iostream>
#include "Initializer.h"
using namespace Seele::Vulkan;
+2 -2
View File
@@ -129,7 +129,7 @@ PGraphicsPipeline PipelineCache::createPipeline(const Gfx::LegacyPipelineCreateI
);
rasterizationState.depthBiasEnable = gfxInfo.rasterizationState.depthBiasEnable;
rasterizationState.depthBiasClamp = gfxInfo.rasterizationState.depthBiasClamp;
rasterizationState.depthBiasConstantFactor = gfxInfo.rasterizationState.depthBoasConstantFactor;
rasterizationState.depthBiasConstantFactor = gfxInfo.rasterizationState.depthBiasConstantFactor;
rasterizationState.depthBiasSlopeFactor = gfxInfo.rasterizationState.depthBiasSlopeFactor;
rasterizationState.depthClampEnable = gfxInfo.rasterizationState.depthClampEnable;
rasterizationState.lineWidth = gfxInfo.rasterizationState.lineWidth;
@@ -274,7 +274,7 @@ PGraphicsPipeline PipelineCache::createPipeline(const Gfx::MeshPipelineCreateInf
);
rasterizationState.depthBiasEnable = gfxInfo.rasterizationState.depthBiasEnable;
rasterizationState.depthBiasClamp = gfxInfo.rasterizationState.depthBiasClamp;
rasterizationState.depthBiasConstantFactor = gfxInfo.rasterizationState.depthBoasConstantFactor;
rasterizationState.depthBiasConstantFactor = gfxInfo.rasterizationState.depthBiasConstantFactor;
rasterizationState.depthBiasSlopeFactor = gfxInfo.rasterizationState.depthBiasSlopeFactor;
rasterizationState.depthClampEnable = gfxInfo.rasterizationState.depthClampEnable;
rasterizationState.lineWidth = gfxInfo.rasterizationState.lineWidth;