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
+8 -3
View File
@@ -174,9 +174,9 @@ void MeshLoader::loadGlobalMeshes(const aiScene* scene, const Array<PMaterialAss
indices[faceIndex * 3 + 2] = mesh->mFaces[faceIndex].mIndices[2];
}
Array<Meshlet> meshlets;
if (Gfx::useMeshShading)
{
Array<Meshlet> meshlets;
meshlets.reserve(indices.size() / (3ull * Gfx::numPrimitivesPerMeshlet));
std::set<uint32> uniqueVertices;
Meshlet current = {
@@ -233,6 +233,7 @@ void MeshLoader::loadGlobalMeshes(const aiScene* scene, const Array<PMaterialAss
completeMeshlet();
}
}
vertexData->loadMesh(id, meshlets);
}
else
{
@@ -250,10 +251,14 @@ void MeshLoader::loadGlobalMeshes(const aiScene* scene, const Array<PMaterialAss
Gfx::PIndexBuffer indexBuffer = graphics->createIndexBuffer(idxInfo);
indexBuffer->transferOwnership(Gfx::QueueType::GRAPHICS);
globalMeshes[meshIndex] = new Mesh(vertexShaderInput, indexBuffer);
globalMeshes[meshIndex]->referencedMaterial = materials[mesh->mMaterialIndex];
globalMeshes[meshIndex] = new Mesh();
globalMeshes[meshIndex]->vertexData = vertexData;
globalMeshes[meshIndex]->id = id;
globalMeshes[meshIndex]->referencedMaterial = materials[mesh->mMaterialIndex]->getMaterial()->instantiate();
globalMeshes[meshIndex]->meshlets = std::move(meshlets);
}
}
void MeshLoader::convertAssimpARGB(unsigned char* dst, aiTexel* src, uint32 numPixels)
{
for(uint32 i = 0; i < numPixels; ++i)
+3 -45
View File
@@ -20,53 +20,11 @@ MeshAsset::~MeshAsset()
void MeshAsset::save(ArchiveBuffer& buffer) const
{
uint64 numMeshes = meshes.size();
Serialization::save(buffer, numMeshes);
for (auto mesh : meshes)
{
mesh->vertexInput->save(buffer);
Array<uint8> rawIndices;
mesh->indexBuffer->download(rawIndices);
Serialization::save(buffer, rawIndices);
Serialization::save(buffer, mesh->indexBuffer->getIndexType());
Serialization::save(buffer, mesh->referencedMaterial->getAssetIdentifier());
}
Serialization::save(buffer, meshes);
}
void MeshAsset::load(ArchiveBuffer& buffer)
{
uint64 numMeshes = 0;
Serialization::load(buffer, numMeshes);
meshes.resize(numMeshes);
for (auto& mesh : meshes)
{
PVertexShaderInput vertexInput = new StaticMeshVertexInput("");
vertexInput->load(buffer);
Array<uint8> rawIndices;
Serialization::load(buffer, rawIndices);
Gfx::SeIndexType indexType;
Serialization::load(buffer, indexType);
IndexBufferCreateInfo createInfo = {
.resourceData = {
.size = rawIndices.size(),
.data = rawIndices.data(),
},
.indexType = indexType,
};
Gfx::PIndexBuffer indexBuffer = buffer.getGraphics()->createIndexBuffer(createInfo);
mesh = new Mesh(vertexInput, indexBuffer);
std::string matname;
Serialization::load(buffer, matname);
mesh->referencedMaterial = AssetRegistry::findMaterial(matname);
}
Serialization::load(buffer, meshes);
}
void MeshAsset::addMesh(PMesh mesh)
{
meshes.add(mesh);
}
const Array<PMesh> MeshAsset::getMeshes()
{
return meshes;
}
+1
View File
@@ -5,6 +5,7 @@
namespace Seele
{
DECLARE_NAME_REF(Gfx, Viewport)
namespace Component
{
struct Camera
+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;
+16 -5
View File
@@ -5,6 +5,9 @@
using namespace Seele;
Material::Material()
{
}
Material::Material(Gfx::PGraphics graphics,
Array<PShaderParameter> parameter,
@@ -37,11 +40,15 @@ PMaterialInstance Seele::Material::instantiate()
void Material::save(ArchiveBuffer& buffer) const
{
MaterialInterface::save(buffer);
Serialization::save(buffer, brdfName);
Serialization::save(buffer, uniformDataSize);
Serialization::save(buffer, uniformBinding);
Serialization::save(buffer, instanceId);
Serialization::save(buffer, materialName);
Serialization::save(buffer, layout->getSetIndex());
Serialization::save(buffer, codeExpressions);
Serialization::save(buffer, parameters);
Serialization::save(buffer, brdf);
Serialization::save(buffer, layout->getSetIndex());
const auto& bindings = layout->getBindings();
Serialization::save(buffer, bindings.size());
for (const auto& binding : bindings)
@@ -57,12 +64,16 @@ void Material::save(ArchiveBuffer& buffer) const
void Material::load(ArchiveBuffer& buffer)
{
graphics = buffer.getGraphics();
MaterialInterface::load(buffer);
Serialization::load(buffer, brdfName);
Serialization::load(buffer, uniformDataSize);
Serialization::load(buffer, uniformBinding);
Serialization::load(buffer, instanceId);
Serialization::load(buffer, materialName);
Serialization::load(buffer, codeExpressions);
Serialization::load(buffer, parameters);
Serialization::load(buffer, brdf);
uint32 setIndex;
Serialization::load(buffer, setIndex);
Serialization::load(buffer, codeExpressions);
Serialization::load(buffer, brdf);
uint64 numBindings;
Serialization::load(buffer, numBindings);
layout = graphics->createDescriptorLayout();
+1
View File
@@ -8,6 +8,7 @@ DECLARE_REF(MaterialInstance)
class Material
{
public:
Material();
Material(Gfx::PGraphics graphics,
Array<PShaderParameter> parameter,
Gfx::PDescriptorLayout layout,
+36 -1
View File
@@ -22,6 +22,41 @@ MaterialInstance::~MaterialInstance()
}
void Seele::MaterialInstance::updateDescriptor()
void MaterialInstance::updateDescriptor()
{
descriptor = layout->allocateDescriptorSet();
for (auto param : parameters)
{
param->updateDescriptorSet(descriptor, uniformData.data());
}
descriptor->writeChanges();
}
Gfx::PDescriptorSet Seele::MaterialInstance::getDescriptorSet() const
{
return descriptor;
}
void MaterialInstance::save(ArchiveBuffer& buffer) const
{
Serialization::save(buffer, uniformData);
Serialization::save(buffer, uniformBinding);
Serialization::save(buffer, uniformBuffer);
Serialization::save(buffer, parameters);
Serialization::save(buffer, layout);
Serialization::save(buffer, descriptor);
Serialization::save(buffer, baseMaterial);
Serialization::save(buffer, id);
}
void MaterialInstance::load(ArchiveBuffer& buffer)
{
Serialization::load(buffer, uniformData);
Serialization::load(buffer, uniformBinding);
Serialization::load(buffer, uniformBuffer);
Serialization::load(buffer, parameters);
Serialization::load(buffer, layout);
Serialization::load(buffer, descriptor);
Serialization::load(buffer, baseMaterial);
Serialization::load(buffer, id);
}
+4
View File
@@ -13,6 +13,10 @@ public:
Gfx::PDescriptorSet getDescriptorSet() const;
PMaterial getBaseMaterial() const { return baseMaterial; }
uint64 getId() const { return id; }
void save(ArchiveBuffer& buffer) const;
void load(ArchiveBuffer& buffer);
private:
Gfx::PGraphics graphics;
Array<uint8> uniformData;