Skip to content

Commit

Permalink
Improved serializer and implemented concept serializers
Browse files Browse the repository at this point in the history
  • Loading branch information
ChunkTreasure1 committed Mar 7, 2024
1 parent 7da344d commit b770625
Show file tree
Hide file tree
Showing 45 changed files with 1,024 additions and 149 deletions.
3 changes: 2 additions & 1 deletion Volt/Sandbox/src/Sandbox/NodeGraph/IONodeGraphEditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,8 @@ inline IONodeGraphEditor<graphType, EditorBackend>::IONodeGraphEditor(const std:
SetGraphTypeText("GRAPHKEY");
}

myHeaderTexture = Volt::TextureImporter::ImportTexture("Editor/Textures/Graph/Translucency.dds");
myHeaderTexture = CreateRef<Volt::Texture2D>();
Volt::TextureImporter::ImportTexture("Editor/Textures/Graph/Translucency.dds", *myHeaderTexture);

{
Ref<NodeGraph::EditorContext> editorContext = CreateRef<NodeGraph::EditorContext>();
Expand Down
4 changes: 3 additions & 1 deletion Volt/Sandbox/src/Sandbox/Utility/AnimatedIcon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ AnimatedIcon::AnimatedIcon(const std::filesystem::path& firstFrame, uint32_t fra
for (uint32_t frame = 1; frame <= frameCount; frame++)
{
const std::filesystem::path path = dirPath / (filename + std::to_string(frame) + firstFrame.extension().string());
myTextures.emplace_back(Volt::TextureImporter::ImportTexture(path));

myTextures.emplace_back() = CreateRef<Volt::Texture2D>();
Volt::TextureImporter::ImportTexture(path, *myTextures.back());
}

VT_CORE_ASSERT(!myTextures.empty(), "No frames found!");
Expand Down
5 changes: 3 additions & 2 deletions Volt/Sandbox/src/Sandbox/Utility/EditorResources.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,10 @@ Ref<Volt::Mesh> EditorResources::GetEditorMesh(EditorMesh mesh)

Ref<Volt::Texture2D> EditorResources::TryLoadIcon(const std::filesystem::path& path)
{
Ref<Volt::Texture2D> texture = Volt::TextureImporter::ImportTexture(path);
Ref<Volt::Texture2D> texture = CreateRef<Volt::Texture2D>();
Volt::TextureImporter::ImportTexture(path, *texture);

if (!texture)
if (!texture->IsValid())
{
texture = Volt::Renderer::GetDefaultData().whiteTexture;
}
Expand Down
1 change: 1 addition & 0 deletions Volt/Volt/src/Volt/Asset/Asset.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ namespace Volt
PostProcessingMaterial = BIT(25),
PostProcessingStack = BIT(26),
MotionWeave = BIT(27),
TextureSource = BIT(28)
};

inline AssetType operator|(AssetType aLhs, AssetType aRhs)
Expand Down
10 changes: 4 additions & 6 deletions Volt/Volt/src/Volt/Asset/Importers/AssetImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ namespace Volt
{
bool TextureSourceImporter::Load(const AssetMetadata& metadata, Ref<Asset>& asset) const
{
asset = CreateRef<Texture2D>();
Ref<Texture2D> texture = std::reinterpret_pointer_cast<Texture2D>(asset);

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

if (!std::filesystem::exists(filePath))
Expand All @@ -41,17 +42,14 @@ namespace Volt
asset->SetFlag(AssetFlag::Missing, true);
return false;
}
auto mesh = TextureImporter::ImportTexture(filePath);

if (!mesh)
if (!TextureImporter::ImportTexture(filePath, *texture))
{
asset->SetFlag(AssetFlag::Invalid, true);
return false;
}

asset = mesh;

Renderer::AddTexture(std::reinterpret_pointer_cast<Texture2D>(asset)->GetImage());
Renderer::AddTexture(texture->GetImage());
return true;
}

Expand Down
12 changes: 7 additions & 5 deletions Volt/Volt/src/Volt/Asset/Importers/DDSTextureImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ namespace Volt

case tdl::DDSFile::DXGIFormat::BC5_UNorm: return ImageFormat::BC5;

case tdl::DDSFile::DXGIFormat::BC6H_SF16: return ImageFormat::BC6H_SF16;
case tdl::DDSFile::DXGIFormat::BC6H_UF16: return ImageFormat::BC6H_UF16;

case tdl::DDSFile::DXGIFormat::BC7_UNorm: return ImageFormat::BC7;
case tdl::DDSFile::DXGIFormat::BC7_UNorm_SRGB: return ImageFormat::BC7SRGB;
}
Expand All @@ -67,7 +70,7 @@ namespace Volt
}
}

Ref<Texture2D> DDSTextureImporter::ImportTextureImpl(const std::filesystem::path& path)
bool DDSTextureImporter::ImportTextureImpl(const std::filesystem::path& path, Texture2D& outTexture)
{
tdl::DDSFile dds;
auto returnCode = dds.Load(path.string().c_str());
Expand All @@ -79,7 +82,7 @@ namespace Volt
if (dds.GetTextureDimension() != tdl::DDSFile::TextureDimension::Texture2D)
{
VT_CORE_ERROR("Texture {0} is not 2D!", path.string().c_str());
return nullptr;
return false;
}

auto imageData = dds.GetImageData();
Expand Down Expand Up @@ -171,9 +174,8 @@ namespace Volt
Utility::TransitionImageLayout(image->GetHandle(), VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
image->OverrideLayout(VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);

Ref<Texture2D> texture = CreateRef<Texture2D>();
texture->myImage = image;
outTexture.myImage = image;

return texture;
return true;
}
}
2 changes: 1 addition & 1 deletion Volt/Volt/src/Volt/Asset/Importers/DDSTextureImporter.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ namespace Volt
~DDSTextureImporter() override = default;

protected:
Ref<Texture2D> ImportTextureImpl(const std::filesystem::path& path) override;
bool ImportTextureImpl(const std::filesystem::path& path, Texture2D& outTexture) override;
};
}
15 changes: 12 additions & 3 deletions Volt/Volt/src/Volt/Asset/Importers/DefaultTextureImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
#include "DefaultTextureImporter.h"

#include "Volt/Rendering/Texture/Texture2D.h"
#include "Volt/Rendering/Texture/Image2D.h"

#include <stb/stb_image.h>

namespace Volt
{
Ref<Texture2D> DefaultTextureImporter::ImportTextureImpl(const std::filesystem::path& path)
bool DefaultTextureImporter::ImportTextureImpl(const std::filesystem::path& path, Texture2D& outTexture)
{
int32_t width;
int32_t height;
Expand All @@ -34,9 +35,17 @@ namespace Volt
format = ImageFormat::RGBA32F;
}

Ref<Texture2D> texture = CreateRef<Texture2D>(format, width, height, data);
ImageSpecification imageSpec{};
imageSpec.format = format;
imageSpec.usage = ImageUsage::Texture;
imageSpec.width = width;
imageSpec.height = height;

Ref<Image2D> image = Image2D::Create(imageSpec, data);
outTexture.myImage = image;

stbi_image_free(data);

return texture;
return true;
}
}
4 changes: 2 additions & 2 deletions Volt/Volt/src/Volt/Asset/Importers/DefaultTextureImporter.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ namespace Volt
~DefaultTextureImporter() override = default;

protected:
Ref<Texture2D> ImportTextureImpl(const std::filesystem::path& path) override;
bool ImportTextureImpl(const std::filesystem::path& path, Texture2D& outTexture) override;
};
}
}
4 changes: 2 additions & 2 deletions Volt/Volt/src/Volt/Asset/Importers/PrefabImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

#include "Volt/Asset/Importers/SceneImporter.h"

#include "Volt/Utility/FileIO/YAMLStreamWriter.h"
#include "Volt/Utility/FileIO/YAMLFileStreamWriter.h"
#include "Volt/Utility/FileIO/YAMLStreamReader.h"

namespace Volt
Expand Down Expand Up @@ -69,7 +69,7 @@ namespace Volt
{
const Ref<Prefab> prefab = std::reinterpret_pointer_cast<Prefab>(asset);

YAMLStreamWriter streamWriter{ AssetManager::GetFilesystemPath(metadata.filePath) };
YAMLFileStreamWriter streamWriter{ AssetManager::GetFilesystemPath(metadata.filePath) };

streamWriter.BeginMap();
streamWriter.BeginMapNamned("Prefab");
Expand Down
20 changes: 10 additions & 10 deletions Volt/Volt/src/Volt/Asset/Importers/SceneImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@
namespace Volt
{
template<typename T>
void RegisterSerializationFunction(std::unordered_map<std::type_index, std::function<void(YAMLStreamWriter&, const uint8_t*, const size_t)>>& outTypes)
void RegisterSerializationFunction(std::unordered_map<std::type_index, std::function<void(YAMLFileStreamWriter&, const uint8_t*, const size_t)>>& outTypes)
{
VT_PROFILE_FUNCTION();

outTypes[std::type_index{ typeid(T) }] = [](YAMLStreamWriter& streamWriter, const uint8_t* data, const size_t offset)
outTypes[std::type_index{ typeid(T) }] = [](YAMLFileStreamWriter& streamWriter, const uint8_t* data, const size_t offset)
{
const T& var = *reinterpret_cast<const T*>(&data[offset]);
streamWriter.SetKey("data", var);
Expand Down Expand Up @@ -188,7 +188,7 @@ namespace Volt

std::filesystem::path scenePath = directoryPath / (metadata.filePath.stem().string() + ".vtscene");

YAMLStreamWriter streamWriter{ scenePath };
YAMLFileStreamWriter streamWriter{ scenePath };
streamWriter.BeginMap();
streamWriter.BeginMapNamned("Scene");
streamWriter.SetKey("name", metadata.filePath.stem().string());
Expand Down Expand Up @@ -285,7 +285,7 @@ namespace Volt
{
const auto layerPath = layerFolderPath / ("layer_" + std::to_string(layer.id) + ".vtlayer");

YAMLStreamWriter streamWriter{ layerPath };
YAMLFileStreamWriter streamWriter{ layerPath };
streamWriter.BeginMap();
streamWriter.BeginMapNamned("Layer");
{
Expand Down Expand Up @@ -332,7 +332,7 @@ namespace Volt
Entity entity = entities.at(i);
const auto entityPath = entitiesDirectoryPath / (entity.ToString() + ".entity");

YAMLStreamWriter streamWriter{ entityPath };
YAMLFileStreamWriter streamWriter{ entityPath };
SerializeEntity(entity, metadata, scene, streamWriter);

streamWriter.WriteToDisk();
Expand Down Expand Up @@ -424,7 +424,7 @@ namespace Volt
}
}

void SceneImporter::SerializeWorldEngine(const Ref<Scene>& scene, YAMLStreamWriter& streamWriter) const
void SceneImporter::SerializeWorldEngine(const Ref<Scene>& scene, YAMLFileStreamWriter& streamWriter) const
{
auto& worldEngine = scene->m_worldEngine;

Expand Down Expand Up @@ -532,7 +532,7 @@ namespace Volt
return scene->CreateEntityWithUUID(entityId);
}

void SceneImporter::SerializeEntity(entt::entity id, const AssetMetadata& metadata, const Ref<Scene>& scene, YAMLStreamWriter& streamWriter) const
void SceneImporter::SerializeEntity(entt::entity id, const AssetMetadata& metadata, const Ref<Scene>& scene, YAMLFileStreamWriter& streamWriter) const
{
streamWriter.BeginMap();
streamWriter.BeginMapNamned("Entity");
Expand Down Expand Up @@ -594,7 +594,7 @@ namespace Volt
streamWriter.EndMap();
}

void SceneImporter::SerializeClass(const uint8_t* data, const size_t offset, const IComponentTypeDesc* compDesc, YAMLStreamWriter& streamWriter, bool isSubComponent) const
void SceneImporter::SerializeClass(const uint8_t* data, const size_t offset, const IComponentTypeDesc* compDesc, YAMLFileStreamWriter& streamWriter, bool isSubComponent) const
{
if (!isSubComponent)
{
Expand Down Expand Up @@ -655,7 +655,7 @@ namespace Volt
}
}

void SceneImporter::SerializeArray(const uint8_t* data, const size_t offset, const IArrayTypeDesc* arrayDesc, YAMLStreamWriter& streamWriter) const
void SceneImporter::SerializeArray(const uint8_t* data, const size_t offset, const IArrayTypeDesc* arrayDesc, YAMLFileStreamWriter& streamWriter) const
{
const void* arrayPtr = &data[offset];

Expand Down Expand Up @@ -704,7 +704,7 @@ namespace Volt
streamWriter.EndSequence();
}

void SceneImporter::SerializeMono(entt::entity id, const Ref<Scene>& scene, YAMLStreamWriter& streamWriter) const
void SceneImporter::SerializeMono(entt::entity id, const Ref<Scene>& scene, YAMLFileStreamWriter& streamWriter) const
{
const auto& scriptFieldCache = scene->GetScriptFieldCache();

Expand Down
14 changes: 7 additions & 7 deletions Volt/Volt/src/Volt/Asset/Importers/SceneImporter.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include "Volt/Asset/Importers/AssetImporter.h"

#include "Volt/Utility/FileIO/YAMLStreamWriter.h"
#include "Volt/Utility/FileIO/YAMLFileStreamWriter.h"
#include "Volt/Utility/FileIO/YAMLStreamReader.h"

#include "Volt/Scene/Entity.h"
Expand Down Expand Up @@ -32,7 +32,7 @@ namespace Volt
bool Load(const AssetMetadata& metadata, Ref<Asset>& asset) const override;
void Save(const AssetMetadata& metadata, const Ref<Asset>& asset) const override;

void SerializeEntity(entt::entity id, const AssetMetadata& metadata, const Ref<Scene>& scene, YAMLStreamWriter& streamWriter) const;
void SerializeEntity(entt::entity id, const AssetMetadata& metadata, const Ref<Scene>& scene, YAMLFileStreamWriter& streamWriter) const;
void DeserializeEntity(const Ref<Scene>& scene, const AssetMetadata& metadata, YAMLStreamReader& streamReader) const;
void DeserializeMono(entt::entity id, const Ref<Scene>& scene, YAMLStreamReader& streamReader) const;

Expand All @@ -50,21 +50,21 @@ namespace Volt
void SaveEntities(const AssetMetadata& metadata, const Ref<Scene>& scene, const std::filesystem::path& sceneDirectory) const;
void LoadEntities(const AssetMetadata& metadata, const Ref<Scene>& scene, const std::filesystem::path& sceneDirectory) const;

void SerializeWorldEngine(const Ref<Scene>& scene, YAMLStreamWriter& streamWriter) const;
void SerializeWorldEngine(const Ref<Scene>& scene, YAMLFileStreamWriter& streamWriter) const;
void DeserializeWorldEngine(const Ref<Scene>& scene, YAMLStreamReader& streamReader) const;

void LoadCellEntities(const AssetMetadata& metadata, const Ref<Scene>& scene, const std::filesystem::path& sceneDirectory) const;

Entity CreateEntityFromUUIDThreadSafe(EntityID entityId, const Ref<Scene>& scene) const;

void SerializeClass(const uint8_t* data, const size_t offset, const IComponentTypeDesc* compDesc, YAMLStreamWriter& streamWriter, bool isSubComponent) const;
void SerializeArray(const uint8_t* data, const size_t offset, const IArrayTypeDesc* arrayDesc, YAMLStreamWriter& streamWriter) const;
void SerializeMono(entt::entity id, const Ref<Scene>& scene, YAMLStreamWriter& streamWriter) const;
void SerializeClass(const uint8_t* data, const size_t offset, const IComponentTypeDesc* compDesc, YAMLFileStreamWriter& streamWriter, bool isSubComponent) const;
void SerializeArray(const uint8_t* data, const size_t offset, const IArrayTypeDesc* arrayDesc, YAMLFileStreamWriter& streamWriter) const;
void SerializeMono(entt::entity id, const Ref<Scene>& scene, YAMLFileStreamWriter& streamWriter) const;

void DeserializeClass(uint8_t* data, const size_t offset, const IComponentTypeDesc* compDesc, YAMLStreamReader& streamReader) const;
void DeserializeArray(uint8_t* data, const size_t offset, const IArrayTypeDesc* arrayDesc, YAMLStreamReader& streamReader) const;

inline static std::unordered_map<std::type_index, std::function<void(YAMLStreamWriter&, const uint8_t*, const size_t)>> s_typeSerializers;
inline static std::unordered_map<std::type_index, std::function<void(YAMLFileStreamWriter&, const uint8_t*, const size_t)>> s_typeSerializers;
inline static std::unordered_map<std::type_index, std::function<void(YAMLStreamReader&, uint8_t*, const size_t)>> s_typeDeserializers;
};
}
6 changes: 3 additions & 3 deletions Volt/Volt/src/Volt/Asset/Importers/TextureImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ namespace Volt
s_importers.clear();
}

Ref<Texture2D> TextureImporter::ImportTexture(const std::filesystem::path& path)
bool TextureImporter::ImportTexture(const std::filesystem::path& path, Texture2D& outTexture)
{
return s_importers[FormatFromExtension(path)]->ImportTextureImpl(path);
return s_importers[FormatFromExtension(path)]->ImportTextureImpl(path, outTexture);
}

TextureImporter::TextureFormat TextureImporter::FormatFromExtension(const std::filesystem::path& path)
Expand All @@ -34,4 +34,4 @@ namespace Volt
return TextureFormat::Other;
}

}
}
6 changes: 3 additions & 3 deletions Volt/Volt/src/Volt/Asset/Importers/TextureImporter.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ namespace Volt
static void Initialize();
static void Shutdown();

static Ref<Texture2D> ImportTexture(const std::filesystem::path& path);
static bool ImportTexture(const std::filesystem::path& path, Texture2D& outTexture);

protected:
virtual Ref<Texture2D> ImportTextureImpl(const std::filesystem::path& path) = 0;
virtual bool ImportTextureImpl(const std::filesystem::path& path, Texture2D& outTexture) = 0;

private:
enum class TextureFormat
Expand All @@ -31,4 +31,4 @@ namespace Volt
static TextureFormat FormatFromExtension(const std::filesystem::path& path);
inline static std::unordered_map<TextureFormat, Scope<TextureImporter>> s_importers;
};
}
}
13 changes: 2 additions & 11 deletions Volt/Volt/src/Volt/Asset/ImportersNew/AssetSerializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,19 @@

namespace Volt
{
void AssetSerializer::WriteMetadata(const AssetMetadata& metadata, const uint32_t version, BinaryStreamWriter& streamWriter)
size_t AssetSerializer::WriteMetadata(const AssetMetadata& metadata, const uint32_t version, BinaryStreamWriter& streamWriter)
{
SerializedAssetMetadataHeader metadataHeader{};
metadataHeader.assetMetadataSize = sizeof(SerializedAssetMetadata);

SerializedAssetMetadata serializedMetadata{};
serializedMetadata.handle = metadata.handle;
serializedMetadata.type = metadata.type;
serializedMetadata.version = version;

streamWriter.Write(metadataHeader);
streamWriter.Write(serializedMetadata);
return 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;
}
Expand Down
2 changes: 1 addition & 1 deletion Volt/Volt/src/Volt/Asset/ImportersNew/AssetSerializer.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace Volt
virtual void Serialize(const AssetMetadata& metadata, const Ref<Asset>& asset) const = 0;
virtual bool Deserialize(const AssetMetadata& metadata, Ref<Asset> destinationAsset) const = 0;

static void WriteMetadata(const AssetMetadata& metadata, const uint32_t version, BinaryStreamWriter& streamWriter);
[[nodiscard]] static size_t WriteMetadata(const AssetMetadata& metadata, const uint32_t version, BinaryStreamWriter& streamWriter);
static SerializedAssetMetadata ReadMetadata(BinaryStreamReader& streamReader);
};
}
Loading

0 comments on commit b770625

Please sign in to comment.