Skip to content

Commit 53cf63c

Browse files
committed
Updated scene serialisation to use binary serialiser
1 parent be4a236 commit 53cf63c

19 files changed

+146
-98
lines changed

engine/core/scene/SceneFile.cpp

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

1212
#include <resources/PackFile.h>
1313
#include <resources/ResourceSystem.h>
14+
#include <resources/PackFileData.h>
1415
#include <resources/SceneData.h>
1516
#include <utils/Logging.h>
1617

@@ -125,15 +126,23 @@ bool SceneFile::Deserialise(std::vector<Entity*>& entities)
125126
{
126127
PackFile* packFile = ResourceSystem::GetInstance().GetPackFile();
127128

128-
std::shared_ptr<SceneData> sceneData = packFile->FindData<SceneData>(ScenePath);
129-
if (!sceneData)
129+
std::shared_ptr<PackFileData> sceneDataBuffer = packFile->FindData<PackFileData>(ScenePath);
130+
if (!sceneDataBuffer)
130131
{
131132
CC_LOG_WARNING("Failed to find scene \"{}\" in pack file", ScenePath)
132133
return false;
133134
}
134135

135-
String sceneString(sceneData->data);
136-
for (const String& entityData : sceneString.Split('|'))
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)
137146
{
138147
deserialiseEntityString(entityData.Str(), "");
139148
}

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

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

1212
#include <freetype/freetype.h>
13-
#include <resources/GenericFileData.h>
13+
#include <resources/PackFileData.h>
1414
#include <resources/PackFile.h>
1515
#include <resources/ResourceSystem.h>
1616
#include <utils/Defer.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<GenericFileData> fileData = packFile->FindData<GenericFileData>(filePath);
30+
std::shared_ptr<PackFileData> fileData = packFile->FindData<PackFileData>(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,7 +9,7 @@
99

1010
#include "Shader.h"
1111

12-
#include <resources/GenericFileData.h>
12+
#include <resources/PackFileData.h>
1313
#include <resources/PackFile.h>
1414
#include <resources/ResourceSystem.h>
1515
#include <utils/Logging.h>
@@ -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<GenericFileData> fileData = packFile->FindData<GenericFileData>(filePath);
177+
std::shared_ptr<PackFileData> fileData = packFile->FindData<PackFileData>(filePath);
178178
MHArray<char> buffer(fileData->data, fileData->dataSize);
179179
return buffer;
180180
}

engine/resources/GenericFileData.h renamed to engine/resources/PackFileData.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@
77
// https://opensource.org/licenses/Zlib
88
//
99

10-
#ifndef SIEGE_ENGINE_GENERICFILEDATA_H
11-
#define SIEGE_ENGINE_GENERICFILEDATA_H
10+
#ifndef SIEGE_ENGINE_PACKFILEDATA_H
11+
#define SIEGE_ENGINE_PACKFILEDATA_H
1212

1313
#include "PackFile.h"
1414

1515
namespace Siege
1616
{
1717

1818
#pragma pack(push, 1)
19-
struct GenericFileData
19+
struct PackFileData
2020
{
2121
uint32_t dataSize = 0;
2222
char data[];
@@ -26,12 +26,12 @@ struct GenericFileData
2626
return &data[0];
2727
}
2828

29-
static GenericFileData* Create(char* data, uint32_t dataSize)
29+
static PackFileData* Create(char* data, uint32_t dataSize)
3030
{
31-
uint32_t totalDataSize = sizeof(GenericFileData) + dataSize;
31+
uint32_t totalDataSize = sizeof(PackFileData) + dataSize;
3232

3333
void* mem = malloc(totalDataSize);
34-
GenericFileData* fileData = new (mem) GenericFileData();
34+
PackFileData* fileData = new (mem) PackFileData();
3535

3636
fileData->dataSize = dataSize;
3737
memcpy(&fileData->data[0], data, dataSize);
@@ -42,12 +42,12 @@ struct GenericFileData
4242
static uint32_t GetDataSize(void* objectData)
4343
{
4444
if (!objectData) return 0;
45-
GenericFileData* fileData = reinterpret_cast<GenericFileData*>(objectData);
46-
return sizeof(GenericFileData) + fileData->dataSize;
45+
PackFileData* fileData = reinterpret_cast<PackFileData*>(objectData);
46+
return sizeof(PackFileData) + fileData->dataSize;
4747
}
4848
};
4949
#pragma pack(pop)
5050

5151
} // namespace Siege
5252

53-
#endif // SIEGE_ENGINE_GENERICFILEDATA_H
53+
#endif // SIEGE_ENGINE_PACKFILEDATA_H

engine/resources/SceneData.h

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,43 +10,20 @@
1010
#ifndef SIEGE_ENGINE_SCENEDATA_H
1111
#define SIEGE_ENGINE_SCENEDATA_H
1212

13-
#include "PackFile.h"
13+
#include <utils/BinarySerialisation.h>
1414

1515
namespace Siege
1616
{
1717

18-
#pragma pack(push, 1)
19-
struct SceneData
18+
struct SceneData : BinarySerialisation::BinarySerialisable
2019
{
21-
uint32_t dataSize = 0;
22-
char data[];
20+
std::vector<String> entities;
2321

24-
const char* GetData() const
22+
void serialise(BinarySerialisation::Buffer& buffer, BinarySerialisation::SerialisationMode mode) override
2523
{
26-
return &data[0];
27-
}
28-
29-
static SceneData* Create(char* data, uint32_t dataSize)
30-
{
31-
uint32_t totalDataSize = sizeof(SceneData) + dataSize;
32-
33-
void* mem = malloc(totalDataSize);
34-
SceneData* sceneData = new (mem) SceneData();
35-
36-
sceneData->dataSize = dataSize;
37-
memcpy(&sceneData->data[0], data, dataSize);
38-
39-
return sceneData;
40-
}
41-
42-
static uint32_t GetDataSize(void* objectData)
43-
{
44-
if (!objectData) return 0;
45-
SceneData* sceneData = reinterpret_cast<SceneData*>(objectData);
46-
return sizeof(SceneData) + sceneData->dataSize;
24+
BinarySerialisation::serialise(buffer, entities, mode);
4725
}
4826
};
49-
#pragma pack(pop)
5027

5128
} // namespace Siege
5229

engine/utils/BinarySerialisation.h

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ enum SerialisationMode : u_int8_t
2626
};
2727

2828
#define SERIALISE_NATIVE(type) \
29-
void serialise(Buffer& buffer, type& value, SerialisationMode mode) \
29+
inline void serialise(Buffer& buffer, type& value, SerialisationMode mode) \
3030
{ \
3131
serialiseNative(buffer, value, mode); \
3232
} \
33-
void serialise(Buffer& buffer, const type& value, SerialisationMode mode) \
33+
inline void serialise(Buffer& buffer, const type& value, SerialisationMode mode) \
3434
{ \
3535
serialiseNative(buffer, value, mode); \
3636
}
@@ -83,6 +83,7 @@ void serialiseNative(Buffer& buffer, const T& value, SerialisationMode mode)
8383
}
8484

8585
SERIALISE_NATIVE(bool)
86+
SERIALISE_NATIVE(char)
8687
SERIALISE_NATIVE(uint32_t)
8788
SERIALISE_NATIVE(int32_t)
8889
SERIALISE_NATIVE(float)
@@ -106,6 +107,32 @@ void serialiseContainer(Buffer& buffer, T& value)
106107
}
107108
}
108109

110+
inline void serialise(Buffer& buffer, String& value, SerialisationMode mode)
111+
{
112+
113+
switch (mode)
114+
{
115+
case SERIALISE:
116+
{
117+
std::string temp(value.Str());
118+
serialiseContainer(buffer, temp);
119+
break;
120+
}
121+
case DESERIALISE:
122+
{
123+
value.Clear();
124+
125+
uint32_t size;
126+
serialise(buffer, size, mode);
127+
value.Reserve(size);
128+
129+
value.Append(reinterpret_cast<char*>(buffer.data.data() + buffer.cursor), size);
130+
buffer.cursor += size;
131+
break;
132+
}
133+
}
134+
}
135+
109136
template<typename T>
110137
void serialise(Buffer& buffer, std::vector<T>& value, SerialisationMode mode)
111138
{

engine/utils/String.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,19 @@ void String::Append(char character)
549549
Assign(cstr);
550550
}
551551

552+
void String::Append(const char *string, size_t size)
553+
{
554+
const char* data = Data();
555+
size_t lhsLength = Size();
556+
size_t fullLength = lhsLength + size;
557+
558+
char* cstr = static_cast<char*>(alloca(fullLength + 1));
559+
strcpy(cstr, data);
560+
strncpy(cstr + lhsLength, string, size);
561+
562+
Assign(cstr);
563+
}
564+
552565
void String::Prepend(const String& string)
553566
{
554567
// Mirrors c-string implementation

engine/utils/String.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,11 @@ class String
183183
*/
184184
String& operator+=(const char* rhs);
185185

186+
/**
187+
* Wide-char c-string addition compound assignment operator overload for appending wide char c-strings
188+
* @param rhs - the wide char c-string to append
189+
* @return a reference to the original String object
190+
*/
186191
String& operator+=(const wchar_t* rhs);
187192

188193
/**
@@ -439,6 +444,13 @@ class String
439444
*/
440445
void Append(char character);
441446

447+
/**
448+
* Appends the value of a c-string to the String for a number of characters
449+
* @param string - the c-string to append
450+
* @param length - the length of the string to append
451+
*/
452+
void Append(const char* string, size_t length);
453+
442454
/**
443455
* Prepends the value of another String
444456
* @param string - the String to prepend

packer/src/main.cpp

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,11 @@
77
// https://opensource.org/licenses/Zlib
88
//
99

10-
#include <resources/GenericFileData.h>
10+
#include <resources/PackFileData.h>
1111
#include <resources/PackFile.h>
1212
#include <resources/ResourceSystem.h>
1313
#include <resources/SceneData.h>
1414
#include <resources/StaticMeshData.h>
15-
#include <resources/Texture2DData.h>
1615
#include <utils/Logging.h>
1716

1817
#include <fstream>
@@ -55,28 +54,24 @@ int main(int argc, char* argv[])
5554
CC_LOG_INFO("Reading asset at path {}", file.c_str())
5655

5756
void* data = nullptr;
58-
uint32_t bodyDataSize = 0;
57+
uint32_t dataSize = 0;
5958
Siege::String fullPath = assetsDir + "/" + Siege::String(file.c_str());
6059
std::filesystem::path extension = file.extension();
6160
if (extension == ".sm")
6261
{
63-
data = PackStaticMeshFile(fullPath, assetsDir);
64-
bodyDataSize = Siege::StaticMeshData::GetDataSize(data);
62+
data = PackStaticMeshFile(fullPath, assetsDir, dataSize);
6563
}
6664
else if (extension == ".jpg" || extension == ".jpeg" || extension == ".png")
6765
{
68-
data = PackTexture2DFile(fullPath);
69-
bodyDataSize = Siege::Texture2DData::GetDataSize(data);
66+
data = PackTexture2DFile(fullPath, dataSize);
7067
}
7168
else if (extension == ".spv" || extension == ".ttf")
7269
{
73-
data = PackGenericFile(fullPath);
74-
bodyDataSize = Siege::GenericFileData::GetDataSize(data);
70+
data = PackGenericFile(fullPath, dataSize);
7571
}
7672
else if (extension == ".scene")
7773
{
78-
data = PackSceneFile(fullPath);
79-
bodyDataSize = Siege::SceneData::GetDataSize(data);
74+
data = PackSceneFile(fullPath, dataSize);
8075
}
8176

8277
if (!data)
@@ -86,12 +81,11 @@ int main(int argc, char* argv[])
8681
continue;
8782
}
8883

89-
PackFile::TocEntry* tocEntry =
90-
PackFile::TocEntry::Create(file.c_str(), entriesDataSize, bodyDataSize);
84+
PackFile::TocEntry* tocEntry = PackFile::TocEntry::Create(file.c_str(), entriesDataSize, dataSize);
9185

9286
entries.emplace_back(tocEntry, data);
9387

94-
entriesDataSize += bodyDataSize;
88+
entriesDataSize += dataSize;
9589
entriesTocSize += tocEntry->GetDataSize();
9690

9791
dynamicAllocations.push_back(data);
@@ -109,7 +103,6 @@ int main(int argc, char* argv[])
109103
header.bodySize,
110104
header.tocOffset)
111105
uint64_t writeTotal = 0;
112-
uint64_t dataOffset = 0;
113106

114107
std::ofstream outputFileStream;
115108
outputFileStream.open(outputFile, std::ios::out | std::ios::binary);
@@ -121,12 +114,15 @@ int main(int argc, char* argv[])
121114
writeTotal)
122115

123116
entriesDataSize = 0;
117+
Bytef* bodyDataBufferCompressed = nullptr;
124118
for (const std::pair<PackFile::TocEntry*, void*>& entry : entries)
125119
{
126120
uLongf bodyDataSizeUncompressed = entry.first->dataSize;
127121
uLongf bodyDataSizeCompressed = compressBound(bodyDataSizeUncompressed);
128122

129-
Bytef bodyDataBufferCompressed[bodyDataSizeCompressed];
123+
Bytef* tempBuffer = static_cast<Bytef*>(realloc(bodyDataBufferCompressed, bodyDataSizeCompressed));
124+
bodyDataBufferCompressed = tempBuffer;
125+
130126
int result = compress2(bodyDataBufferCompressed, &bodyDataSizeCompressed,
131127
static_cast<Bytef*>(entry.second), bodyDataSizeUncompressed, Z_BEST_COMPRESSION);
132128
CC_ASSERT(result == Z_OK, "Compression failed for entry: " + Siege::String(entry.first->name));
@@ -136,13 +132,14 @@ int main(int argc, char* argv[])
136132
entry.first->dataSizeCompressed = bodyDataSizeCompressed;
137133
writeTotal += bodyDataSizeCompressed;
138134
entriesDataSize += bodyDataSizeCompressed;
139-
CC_LOG_INFO("Adding DATA \"{}\" to pack file with size: {}, compressed to {}% (write total: {})",
135+
CC_LOG_INFO("Adding DATA \"{}\" to pack file with size: {} from {}, compressed to ~{}% (write total: {})",
140136
entry.first->name,
141137
bodyDataSizeCompressed,
142-
static_cast<uint8_t>(static_cast<float>(bodyDataSizeCompressed) / static_cast<float>(bodyDataSizeUncompressed) * 100.f),
138+
bodyDataSizeUncompressed,
139+
static_cast<uint8_t>(ceilf(static_cast<float>(bodyDataSizeCompressed) / static_cast<float>(bodyDataSizeUncompressed) * 100.f)),
143140
writeTotal)
144-
dataOffset += bodyDataSizeCompressed;
145141
}
142+
free(bodyDataBufferCompressed);
146143

147144
outputFileStream.write(PACKER_MAGIC_NUMBER_TOC, PACKER_MAGIC_NUMBER_SIZE);
148145
writeTotal += PACKER_MAGIC_NUMBER_SIZE;

0 commit comments

Comments
 (0)