Skip to content

Commit

Permalink
Implemented concept stream reader
Browse files Browse the repository at this point in the history
  • Loading branch information
ChunkTreasure1 committed Mar 1, 2024
1 parent 2baa32b commit 254ab32
Show file tree
Hide file tree
Showing 18 changed files with 491 additions and 76 deletions.
14 changes: 14 additions & 0 deletions Volt/Volt/src/Volt/Asset/ImportersNew/AssetSerializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace Volt
void AssetSerializer::WriteMetadata(const AssetMetadata& metadata, const uint32_t version, BinaryStreamWriter& streamWriter)
{
SerializedAssetMetadataHeader metadataHeader{};
metadataHeader.assetMetadataSize = sizeof(SerializedAssetMetadata);

SerializedAssetMetadata serializedMetadata{};
serializedMetadata.handle = metadata.handle;
Expand All @@ -15,4 +16,17 @@ namespace Volt
streamWriter.Write(metadataHeader);
streamWriter.Write(serializedMetadata);
}

SerializedAssetMetadata AssetSerializer::ReadMetadata(BinaryStreamReader& streamReader)
{
SerializedAssetMetadataHeader header{};
SerializedAssetMetadata result{};

streamReader.Read(header);
VT_CORE_ASSERT(sizeof(SerializedAssetMetadata) == header.assetMetadataSize, "Size mismatch!");

streamReader.Read(result);
return result;
}

}
2 changes: 2 additions & 0 deletions Volt/Volt/src/Volt/Asset/ImportersNew/AssetSerializer.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "Volt/Asset/Serialization/AssetSerializationCommon.h"

#include "Volt/Utility/FileIO/BinaryStreamWriter.h"
#include "Volt/Utility/FileIO/BinaryStreamReader.h"

namespace Volt
{
Expand All @@ -17,5 +18,6 @@ namespace Volt
virtual bool Deserialize(const AssetMetadata& metadata, Ref<Asset> destinationAsset) const = 0;

static void WriteMetadata(const AssetMetadata& metadata, const uint32_t version, BinaryStreamWriter& streamWriter);
static SerializedAssetMetadata ReadMetadata(BinaryStreamReader& streamReader);
};
}
60 changes: 0 additions & 60 deletions Volt/Volt/src/Volt/Asset/ImportersNew/MeshImporter.cpp

This file was deleted.

110 changes: 110 additions & 0 deletions Volt/Volt/src/Volt/Asset/ImportersNew/MeshSerializer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
#include "vtpch.h"
#include "MeshSerializer.h"

#include "Volt/Asset/Mesh/Mesh.h"
#include "Volt/Asset/Mesh/Material.h"
#include "Volt/Asset/Serialization/AssetSerializationCommon.h"

#include "Volt/Asset/AssetManager.h"

namespace Volt
{
constexpr uint32_t CURRENT_ASSET_VERSION = 1;

struct MeshSerializationData
{
AssetHandle materialHandle;

std::vector<Vertex> vertices;
std::vector<uint32_t> indices;

glm::vec3 boundingSphereCenter;
float boundingSphereRadius;

std::vector<SubMesh> subMeshes;

static void Serialize(BinaryStreamWriter& streamWriter, const MeshSerializationData& data)
{
streamWriter.Write(data.materialHandle);
streamWriter.WriteRaw(data.vertices);
streamWriter.WriteRaw(data.indices);
streamWriter.Write(data.boundingSphereCenter);
streamWriter.Write(data.boundingSphereRadius);
streamWriter.Write(data.subMeshes);
}

static void Deserialize(BinaryStreamReader& streamReader, MeshSerializationData& outData)
{
streamReader.Read(outData.materialHandle);
streamReader.ReadRaw(outData.vertices);
streamReader.ReadRaw(outData.indices);
streamReader.Read(outData.boundingSphereCenter);
streamReader.Read(outData.boundingSphereRadius);
streamReader.Read(outData.subMeshes);
}
};

void MeshSerializer::Serialize(const AssetMetadata& metadata, const Ref<Asset>& asset) const
{
Ref<Mesh> mesh = std::reinterpret_pointer_cast<Mesh>(asset);

BinaryStreamWriter streamWriter{};
AssetSerializer::WriteMetadata(metadata, CURRENT_ASSET_VERSION, streamWriter);

MeshSerializationData serializationData{};
serializationData.materialHandle = mesh->GetMaterial()->handle;
serializationData.vertices = mesh->GetVertices();
serializationData.indices = mesh->GetIndices();
serializationData.boundingSphereCenter = mesh->GetBoundingSphere().center;
serializationData.boundingSphereRadius = mesh->GetBoundingSphere().radius;
serializationData.subMeshes = mesh->GetSubMeshes();

streamWriter.Write(serializationData);

const auto filePath = AssetManager::GetFilesystemPath(metadata.filePath);
streamWriter.WriteToDisk(filePath);
}

bool MeshSerializer::Deserialize(const AssetMetadata& metadata, Ref<Asset> destinationAsset) const
{
const auto filePath = AssetManager::GetFilesystemPath(metadata.filePath);

if (!std::filesystem::exists(filePath))
{
VT_CORE_ERROR("File {0} not found!", metadata.filePath);
destinationAsset->SetFlag(AssetFlag::Missing, true);
return false;
}

BinaryStreamReader streamReader{ filePath };

if (!streamReader.IsStreamValid())
{
VT_CORE_ERROR("Failed to open file: {0}!", metadata.filePath);
destinationAsset->SetFlag(AssetFlag::Invalid, true);
return false;
}

SerializedAssetMetadata serializedMetadata = AssetSerializer::ReadMetadata(streamReader);

MeshSerializationData serializationData{};
streamReader.Read(serializationData);

Ref<Mesh> mesh = std::reinterpret_pointer_cast<Mesh>(destinationAsset);

mesh->myMaterial = AssetManager::QueueAsset<Material>(serializationData.materialHandle);
mesh->myVertices = serializationData.vertices;
mesh->myIndices = serializationData.indices;
mesh->myBoundingSphere.center = serializationData.boundingSphereCenter;
mesh->myBoundingSphere.radius = serializationData.boundingSphereRadius;
mesh->mySubMeshes = serializationData.subMeshes;

for (auto& subMesh : mesh->mySubMeshes)
{
subMesh.GenerateHash();
}

mesh->Construct();
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@

namespace Volt
{
class MeshImporter : public AssetSerializer
class MeshSerializer : public AssetSerializer
{
public:
void Serialize(const AssetMetadata& metadata, const Ref<Asset>& asset) const override;
bool Deserialize(const AssetMetadata& metadata, Ref<Asset> destinationAsset) const override;

private:
};
}
2 changes: 1 addition & 1 deletion Volt/Volt/src/Volt/Asset/Mesh/Mesh.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ namespace Volt

friend class FbxImporter;
friend class MeshCompiler;
friend class MeshImporter;
friend class MeshSerializer;
friend class MeshExporterUtilities;
friend class VTMeshImporter;
friend class GLTFImporter;
Expand Down
27 changes: 26 additions & 1 deletion Volt/Volt/src/Volt/Asset/Mesh/SubMesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
#include "Volt/Core/UUID.h"
#include "Volt/Rendering/Shader/ShaderUtility.h"

#include "Volt/Utility/FileIO/BinaryStreamWriter.h"
#include "Volt/Utility/FileIO/BinaryStreamReader.h"

namespace Volt
{
SubMesh::SubMesh(uint32_t aMaterialIndex, uint32_t aVertexCount, uint32_t aIndexCount, uint32_t aVertexStartOffset, uint32_t aIndexStartOffset)
Expand Down Expand Up @@ -38,6 +41,28 @@ namespace Volt
return m_hash != rhs.m_hash;
}

void SubMesh::Serialize(BinaryStreamWriter& streamWriter, const SubMesh& data)
{
streamWriter.Write(data.materialIndex);
streamWriter.Write(data.vertexCount);
streamWriter.Write(data.indexCount);
streamWriter.Write(data.vertexStartOffset);
streamWriter.Write(data.indexStartOffset);
streamWriter.Write(data.transform);
streamWriter.Write(data.name);
}

void SubMesh::Deserialize(BinaryStreamReader& streamReader, SubMesh& outData)
{
streamReader.Read(outData.materialIndex);
streamReader.Read(outData.vertexCount);
streamReader.Read(outData.indexCount);
streamReader.Read(outData.vertexStartOffset);
streamReader.Read(outData.indexStartOffset);
streamReader.Read(outData.transform);
streamReader.Read(outData.name);
}

bool operator>(const SubMesh& lhs, const SubMesh& rhs)
{
return lhs.m_hash > rhs.m_hash;
Expand All @@ -47,4 +72,4 @@ namespace Volt
{
return lhs.m_hash < rhs.m_hash;
}
}
}
6 changes: 6 additions & 0 deletions Volt/Volt/src/Volt/Asset/Mesh/SubMesh.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

namespace Volt
{
class BinaryStreamWriter;
class BinaryStreamReader;

struct SubMesh
{
SubMesh(uint32_t aMaterialIndex, uint32_t aVertexCount, uint32_t aIndexCount, uint32_t aVertexStartOffset, uint32_t aIndexStartOffset);
Expand All @@ -28,6 +31,9 @@ namespace Volt
glm::mat4 transform = { 1.f };
std::string name;

static void Serialize(BinaryStreamWriter& streamWriter, const SubMesh& data);
static void Deserialize(BinaryStreamReader& streamReader, SubMesh& outData);

private:
size_t m_hash = 0;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "AssetSerializationCommon.h"

#include "Volt/Utility/FileIO/BinaryStreamWriter.h"
#include "Volt/Utility/FileIO/BinaryStreamReader.h"

namespace Volt
{
Expand All @@ -11,4 +12,11 @@ namespace Volt
streamWriter.Write(data.type);
streamWriter.Write(data.version);
}

void SerializedAssetMetadata::Deserialize(BinaryStreamReader& streamReader, SerializedAssetMetadata& outData)
{
streamReader.Read(outData.handle);
streamReader.Read(outData.type);
streamReader.Read(outData.version);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Volt
{
class BinaryStreamWriter;
class BinaryStreamReader;

struct SerializedAssetMetadata
{
Expand All @@ -13,6 +14,7 @@ namespace Volt
uint32_t version;

static void Serialize(BinaryStreamWriter& streamWriter, const SerializedAssetMetadata& data);
static void Deserialize(BinaryStreamReader& streamReader, SerializedAssetMetadata& outData);
};

struct SerializedAssetMetadataHeader
Expand Down
6 changes: 6 additions & 0 deletions Volt/Volt/src/Volt/Core/UUID.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "UUID.h"

#include "Volt/Utility/FileIO/BinaryStreamWriter.h"
#include "Volt/Utility/FileIO/BinaryStreamReader.h"

#include <random>
#include <unordered_map>
Expand All @@ -24,6 +25,11 @@ namespace Volt
streamWriter.Write(data.myUUID);
}

void UUID::Deserialize(BinaryStreamReader& streamReader, UUID& outData)
{
streamReader.Read(outData.myUUID);
}

UUID32::UUID32()
: m_uuid(s_uniformDistribution32(s_engine32))
{
Expand Down
2 changes: 2 additions & 0 deletions Volt/Volt/src/Volt/Core/UUID.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Volt
{
class BinaryStreamWriter;
class BinaryStreamReader;

class UUID
{
Expand All @@ -20,6 +21,7 @@ namespace Volt
operator uint64_t() const { return myUUID; }

static void Serialize(BinaryStreamWriter& streamWriter, const UUID& data);
static void Deserialize(BinaryStreamReader& streamReader, UUID& outData);

private:
uint64_t myUUID;
Expand Down
1 change: 1 addition & 0 deletions Volt/Volt/src/Volt/Rendering/Vertex.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include "Volt/Rendering/Buffer/BufferLayout.h"
#include "Volt/Utility/FileIO/BinaryStreamWriter.h"

#include <glm/glm.hpp>
#include <half/half.hpp>
Expand Down
Loading

0 comments on commit 254ab32

Please sign in to comment.