Skip to content

Commit ec1f27b

Browse files
committed
Folded system data deserialisation into packfile access operations, fixed all tests
1 parent 408c263 commit ec1f27b

26 files changed

+230
-196
lines changed

engine/core/scene/SceneFile.cpp

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
#include "SceneFile.h"
1111

1212
#include <resources/PackFile.h>
13-
#include <resources/ResourceSystem.h>
1413
#include <resources/PackFileData.h>
14+
#include <resources/ResourceSystem.h>
1515
#include <resources/SceneData.h>
1616
#include <utils/Logging.h>
1717

@@ -106,43 +106,36 @@ bool SceneFile::Deserialise(std::vector<Entity*>& entities)
106106
};
107107

108108
// TODO: Implement proper write-mode handling for scene system
109-
String ScenePath = MakeScenePath(sceneName);
109+
String scenePath = MakeScenePath(sceneName);
110110
if (!SceneSystem::GetBaseDirectory().IsEmpty())
111111
{
112112
bool result = FileSystem::ForEachFileInDir(
113-
ScenePath,
113+
scenePath,
114114
[&deserialiseEntityString](const std::filesystem::path& path) {
115115
if (path.extension() != ENTITY_FILE_EXT) return;
116116
String entityData = FileSystem::Read(path.c_str());
117117
deserialiseEntityString(entityData, path);
118118
});
119119
if (!result)
120120
{
121-
CC_LOG_ERROR("Failed to read scene file at path \"{}\"", ScenePath)
121+
CC_LOG_ERROR("Failed to read scene file at path \"{}\"", scenePath)
122122
return false;
123123
}
124124
}
125125
else
126126
{
127127
PackFile* packFile = ResourceSystem::GetInstance().GetPackFile();
128128

129-
std::shared_ptr<PackFileData> sceneDataBuffer = packFile->FindData<PackFileData>(ScenePath);
130-
if (!sceneDataBuffer)
129+
std::shared_ptr<SceneData> sceneData = packFile->FindDataDeserialised<SceneData>(scenePath);
130+
if (!sceneData)
131131
{
132-
CC_LOG_WARNING("Failed to find scene \"{}\" in pack file", ScenePath)
132+
CC_LOG_WARNING(
133+
"Failed to retrieve deserialised scene data for scene \"{}\" from pack file",
134+
scenePath)
133135
return false;
134136
}
135137

136-
BinarySerialisation::Buffer buffer;
137-
uint8_t* data = reinterpret_cast<uint8_t*>(sceneDataBuffer->data);
138-
size_t dataSize = sceneDataBuffer->dataSize;
139-
buffer.data.assign(data, dataSize);
140-
141-
// TODO - Fold into resource system
142-
SceneData sceneData;
143-
sceneData.serialise(buffer, BinarySerialisation::DESERIALISE);
144-
145-
for (const String& entityData : sceneData.entities)
138+
for (const String& entityData : sceneData->entities)
146139
{
147140
deserialiseEntityString(entityData.Str(), "");
148141
}

engine/render/renderer/platform/vulkan/Font.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
#include "Font.h"
1111

1212
#include <freetype/freetype.h>
13-
#include <resources/PackFileData.h>
1413
#include <resources/PackFile.h>
14+
#include <resources/PackFileData.h>
1515
#include <resources/ResourceSystem.h>
1616
#include <utils/Defer.h>
1717
#include <utils/Logging.h>
@@ -27,7 +27,7 @@ namespace Siege::Vulkan
2727
Font::Font(const char* filePath)
2828
{
2929
PackFile* packFile = ResourceSystem::GetInstance().GetPackFile();
30-
std::shared_ptr<PackFileData> fileData = packFile->FindData<PackFileData>(filePath);
30+
std::shared_ptr<PackFileData> fileData = packFile->FindData(filePath);
3131

3232
FT_Open_Args args;
3333
args.flags = FT_OPEN_MEMORY;

engine/render/renderer/platform/vulkan/Shader.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99

1010
#include "Shader.h"
1111

12-
#include <resources/PackFileData.h>
1312
#include <resources/PackFile.h>
13+
#include <resources/PackFileData.h>
1414
#include <resources/ResourceSystem.h>
1515
#include <utils/Logging.h>
1616

@@ -174,7 +174,7 @@ void Shader::Destroy()
174174
MHArray<char> Shader::ReadFileAsBinary(const String& filePath)
175175
{
176176
PackFile* packFile = ResourceSystem::GetInstance().GetPackFile();
177-
std::shared_ptr<PackFileData> fileData = packFile->FindData<PackFileData>(filePath);
177+
std::shared_ptr<PackFileData> fileData = packFile->FindData(filePath);
178178
MHArray<char> buffer(fileData->data, fileData->dataSize);
179179
return buffer;
180180
}

engine/render/renderer/platform/vulkan/StaticMesh.cpp

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
#include <resources/PackFile.h>
1313
#include <resources/ResourceSystem.h>
14-
#include <resources/PackFileData.h>
1514
#include <resources/StaticMeshData.h>
1615
#include <utils/Logging.h>
1716

@@ -44,31 +43,25 @@ StaticMesh::StaticMesh(const char* filePath, Material* material)
4443
{
4544
// TODO(Aryeh): How to extract material data from object files?
4645
PackFile* packFile = ResourceSystem::GetInstance().GetPackFile();
47-
std::shared_ptr<PackFileData> vertexDataDataBuffer = packFile->FindData<PackFileData>(filePath);
46+
std::shared_ptr<StaticMeshData> staticMeshData =
47+
packFile->FindDataDeserialised<StaticMeshData>(filePath);
4848

49-
BinarySerialisation::Buffer buffer;
50-
uint8_t* data = reinterpret_cast<uint8_t*>(vertexDataDataBuffer->data);
51-
size_t dataSize = vertexDataDataBuffer->dataSize;
52-
buffer.data.assign(data, dataSize);
49+
CC_ASSERT(!staticMeshData->vertices.empty(), "Cannot load in a file with no vertices!")
50+
CC_ASSERT(!staticMeshData->indices.empty(), "Cannot load in a file with no indices!")
5351

54-
// TODO - Fold into resource system
55-
StaticMeshData staticMeshData;
56-
staticMeshData.serialise(buffer, BinarySerialisation::DESERIALISE);
52+
CC_ASSERT(staticMeshData->vertices.size() < MAX_VERTICES,
53+
"The provided model has too many vertices!")
54+
CC_ASSERT(staticMeshData->indices.size() < MAX_INDICES,
55+
"The provided model has too many indices!")
5756

58-
CC_ASSERT(!staticMeshData.vertices.empty(), "Cannot load in a file with no vertices!")
59-
CC_ASSERT(!staticMeshData.indices.empty(), "Cannot load in a file with no indices!")
60-
61-
CC_ASSERT(staticMeshData.vertices.size() < MAX_VERTICES, "The provided model has too many vertices!")
62-
CC_ASSERT(staticMeshData.indices.size() < MAX_INDICES, "The provided model has too many indices!")
63-
64-
vertexCount = staticMeshData.vertices.size();
65-
indexCount = staticMeshData.indices.size();
57+
vertexCount = staticMeshData->vertices.size();
58+
indexCount = staticMeshData->indices.size();
6659

6760
vertexBuffer = VertexBuffer(sizeof(BaseVertex) * vertexCount);
68-
vertexBuffer.Copy(staticMeshData.vertices.data(), sizeof(BaseVertex) * vertexCount);
61+
vertexBuffer.Copy(staticMeshData->vertices.data(), sizeof(BaseVertex) * vertexCount);
6962

7063
indexBuffer = IndexBuffer(sizeof(unsigned int) * indexCount);
71-
indexBuffer.Copy(staticMeshData.indices.data(), sizeof(unsigned int) * indexCount);
64+
indexBuffer.Copy(staticMeshData->indices.data(), sizeof(unsigned int) * indexCount);
7265

7366
subMeshes = MHArray<SubMesh>(1);
7467
materials = MHArray<Material*>(1);

engine/render/renderer/platform/vulkan/Texture2D.cpp

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -94,21 +94,13 @@ Texture2D& Texture2D::operator=(Texture2D&& other)
9494
void Texture2D::LoadFromFile(const char* filePath)
9595
{
9696
PackFile* packFile = ResourceSystem::GetInstance().GetPackFile();
97-
std::shared_ptr<PackFileData> texture2dDataBuffer = packFile->FindData<PackFileData>(filePath);
98-
99-
BinarySerialisation::Buffer buffer;
100-
uint8_t* data = reinterpret_cast<uint8_t*>(texture2dDataBuffer->data);
101-
size_t dataSize = texture2dDataBuffer->dataSize;
102-
buffer.data.assign(data, dataSize);
103-
104-
// TODO - Fold into resource system
105-
Texture2DData texture2dData;
106-
texture2dData.serialise(buffer, BinarySerialisation::DESERIALISE);
97+
std::shared_ptr<Texture2DData> texture2dData =
98+
packFile->FindDataDeserialised<Texture2DData>(filePath);
10799

108100
Buffer::Buffer stagingBuffer;
109101
defer([&stagingBuffer] { Buffer::DestroyBuffer(stagingBuffer); });
110102

111-
Buffer::CreateBuffer(texture2dData.GetImageSize(),
103+
Buffer::CreateBuffer(texture2dData->GetImageSize(),
112104
VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
113105
// specifies that data is accessible on the CPU.
114106
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
@@ -117,13 +109,13 @@ void Texture2D::LoadFromFile(const char* filePath)
117109
OUT stagingBuffer.buffer,
118110
OUT stagingBuffer.bufferMemory);
119111

120-
Buffer::CopyData(stagingBuffer, texture2dData.GetImageSize(), texture2dData.pixels.data());
112+
Buffer::CopyData(stagingBuffer, texture2dData->GetImageSize(), texture2dData->pixels.data());
121113

122-
extent = {static_cast<uint32_t>(texture2dData.texWidth),
123-
static_cast<uint32_t>(texture2dData.texHeight)};
114+
extent = {static_cast<uint32_t>(texture2dData->texWidth),
115+
static_cast<uint32_t>(texture2dData->texHeight)};
124116

125-
Utils::Extent3D imageExtent {static_cast<uint32_t>(texture2dData.texWidth),
126-
static_cast<uint32_t>(texture2dData.texHeight),
117+
Utils::Extent3D imageExtent {static_cast<uint32_t>(texture2dData->texWidth),
118+
static_cast<uint32_t>(texture2dData->texHeight),
127119
1};
128120
image = Image({Utils::RGBASRGB, imageExtent, Vulkan::Utils::USAGE_TEXTURE, 1, 1});
129121

engine/resources/PackFile.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,27 @@ bool PackFile::LoadFromPath(const String& filepath)
5252
return true;
5353
}
5454

55+
std::shared_ptr<PackFileData> PackFile::FindData(const String& filepath)
56+
{
57+
const TocEntry* toc = entries[filepath];
58+
if (!toc)
59+
{
60+
return nullptr;
61+
}
62+
63+
uLongf bodyDataSizeUncompressed = toc->dataSize;
64+
uLongf bodyDataSizeCompressed = toc->dataSizeCompressed;
65+
66+
PackFileData* packFileData = new (malloc(bodyDataSizeUncompressed)) PackFileData();
67+
int result = uncompress(reinterpret_cast<Bytef*>(packFileData),
68+
&bodyDataSizeUncompressed,
69+
reinterpret_cast<Bytef*>(body + toc->dataOffset),
70+
bodyDataSizeCompressed);
71+
CC_ASSERT(result == Z_OK, "Decompression failed for filepath: " + filepath);
72+
73+
return std::shared_ptr<PackFileData>(packFileData);
74+
}
75+
5576
const std::map<String, PackFile::TocEntry*>& PackFile::GetEntries()
5677
{
5778
return entries;

engine/resources/PackFile.h

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@
1010
#ifndef SIEGE_ENGINE_PACKFILE_H
1111
#define SIEGE_ENGINE_PACKFILE_H
1212

13-
#include <utils/String.h>
13+
#include <utils/BinarySerialisation.h>
1414
#include <utils/Logging.h>
15+
#include <utils/String.h>
16+
#include <zlib.h>
1517

1618
#include <filesystem>
1719
#include <map>
1820

19-
#include <zlib.h>
21+
#include "PackFileData.h"
2022

2123
#define PACKER_MAGIC_NUMBER_FILE "pck"
2224
#define PACKER_MAGIC_NUMBER_TOC "toc!"
@@ -69,23 +71,25 @@ class PackFile
6971

7072
bool LoadFromPath(const String& filepath);
7173

74+
std::shared_ptr<PackFileData> FindData(const String& filepath);
75+
7276
template<typename T>
73-
std::shared_ptr<T> FindData(const String& filepath)
77+
std::shared_ptr<T> FindDataDeserialised(const String& filepath)
7478
{
75-
const TocEntry* toc = entries[filepath];
76-
if (!toc)
79+
std::shared_ptr<PackFileData> packFileData = FindData(filepath);
80+
if (!packFileData)
7781
{
82+
CC_LOG_WARNING("Failed to find data for filepath \"{}\"", filepath);
7883
return nullptr;
7984
}
8085

81-
uLongf bodyDataSizeUncompressed = toc->dataSize;
82-
uLongf bodyDataSizeCompressed = toc->dataSizeCompressed;
83-
void* mem = malloc(bodyDataSizeUncompressed);
86+
BinarySerialisation::Buffer buffer;
87+
buffer.Fill(reinterpret_cast<uint8_t*>(packFileData->data), packFileData->dataSize);
8488

85-
int result = uncompress(static_cast<Bytef*>(mem), &bodyDataSizeUncompressed, reinterpret_cast<Bytef*>(body + toc->dataOffset), bodyDataSizeCompressed);
86-
CC_ASSERT(result == Z_OK, "Decompression failed for filepath: " + filepath);
89+
T* typedData = new T();
90+
typedData->serialise(buffer, BinarySerialisation::DESERIALISE);
8791

88-
return std::shared_ptr<T>(static_cast<T*>(mem), free);
92+
return std::shared_ptr<T>(typedData);
8993
}
9094

9195
const std::map<String, TocEntry*>& GetEntries();

engine/resources/PackFileData.h

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@
1010
#ifndef SIEGE_ENGINE_PACKFILEDATA_H
1111
#define SIEGE_ENGINE_PACKFILEDATA_H
1212

13-
#include "PackFile.h"
13+
#include <cstdint>
14+
#include <cstdlib>
15+
#include <cstring>
16+
#include <new>
1417

1518
namespace Siege
1619
{
@@ -26,25 +29,19 @@ struct PackFileData
2629
return &data[0];
2730
}
2831

29-
static PackFileData* Create(char* data, uint32_t dataSize)
32+
uint32_t GetDataSize() const
3033
{
31-
uint32_t totalDataSize = sizeof(PackFileData) + dataSize;
34+
return sizeof(PackFileData) + dataSize;
35+
}
3236

33-
void* mem = malloc(totalDataSize);
37+
static PackFileData* Create(char* data, uint32_t dataSize)
38+
{
39+
void* mem = malloc(sizeof(PackFileData) + dataSize);
3440
PackFileData* fileData = new (mem) PackFileData();
35-
3641
fileData->dataSize = dataSize;
3742
memcpy(&fileData->data[0], data, dataSize);
38-
3943
return fileData;
4044
}
41-
42-
static uint32_t GetDataSize(void* objectData)
43-
{
44-
if (!objectData) return 0;
45-
PackFileData* fileData = reinterpret_cast<PackFileData*>(objectData);
46-
return sizeof(PackFileData) + fileData->dataSize;
47-
}
4845
};
4946
#pragma pack(pop)
5047

engine/resources/SceneData.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ struct SceneData : BinarySerialisation::BinarySerialisable
1919
{
2020
std::vector<String> entities;
2121

22-
void serialise(BinarySerialisation::Buffer& buffer, BinarySerialisation::SerialisationMode mode) override
22+
void serialise(BinarySerialisation::Buffer& buffer,
23+
BinarySerialisation::SerialisationMode mode) override
2324
{
2425
BinarySerialisation::serialise(buffer, entities, mode);
2526
}

engine/resources/StaticMeshData.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010
#ifndef SIEGE_ENGINE_STATICMESHDATA_H
1111
#define SIEGE_ENGINE_STATICMESHDATA_H
1212

13+
#include <utils/BinarySerialisation.h>
1314
#include <utils/Colour.h>
1415
#include <utils/math/vec/Hashing.h>
1516
#include <utils/math/vec/Vec2.h>
1617
#include <utils/math/vec/Vec3.h>
17-
#include <utils/BinarySerialisation.h>
1818

1919
#include "PackFile.h"
2020

@@ -40,7 +40,9 @@ inline bool operator!=(const BaseVertex& left, const BaseVertex& right)
4040
return !(left == right);
4141
}
4242

43-
inline void serialise(BinarySerialisation::Buffer& buffer, BaseVertex& value, BinarySerialisation::SerialisationMode mode)
43+
inline void serialise(BinarySerialisation::Buffer& buffer,
44+
BaseVertex& value,
45+
BinarySerialisation::SerialisationMode mode)
4446
{
4547
serialise(buffer, value.position, mode);
4648
serialise(buffer, value.color, mode);
@@ -53,7 +55,8 @@ struct StaticMeshData : BinarySerialisation::BinarySerialisable
5355
std::vector<uint32_t> indices;
5456
std::vector<BaseVertex> vertices;
5557

56-
void serialise(BinarySerialisation::Buffer& buffer, BinarySerialisation::SerialisationMode mode) override
58+
void serialise(BinarySerialisation::Buffer& buffer,
59+
BinarySerialisation::SerialisationMode mode) override
5760
{
5861
BinarySerialisation::serialise(buffer, indices, mode);
5962
BinarySerialisation::serialise(buffer, vertices, mode);

0 commit comments

Comments
 (0)