Skip to content

Commit 847a93f

Browse files
authored
Merge pull request #69 from CapsCollective/feature/asset-compression
Added compression and switched to recursive binary serialisation for asset packing
2 parents 3c96562 + 0e3f22c commit 847a93f

37 files changed

+867
-303
lines changed

.clang-format

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ AttributeMacros:
2929
BinPackArguments: false
3030
BinPackParameters: false
3131
BraceWrapping:
32-
AfterCaseLabel: false
32+
AfterCaseLabel: true
3333
AfterClass: true
3434
AfterControlStatement: Always
3535
AfterEnum: true

engine/core/Makefile

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,6 @@ coreSources := $(call rwildcard,$(coreSrcDir)/,*.cpp)
1515
coreObjects := $(call findobjs,$(coreSrcDir),$(coreBinDir),$(coreSources))
1616
coreDepends := $(patsubst %.o, %.d, $(call rwildcard,$(coreBinDir)/,*.o))
1717

18-
# Set build vars
19-
ifeq ($(platform), windows)
20-
libGenDir := src
21-
else ifeq ($(platform), macos)
22-
libGenDir := src
23-
endif
24-
2518
# Build the static library
2619
$(coreLib): $(coreObjects)
2720
$(call MKDIR,$(call platformpth,$(libDir)))

engine/core/scene/SceneFile.cpp

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

1212
#include <resources/PackFile.h>
13+
#include <resources/PackFileData.h>
1314
#include <resources/ResourceSystem.h>
1415
#include <resources/SceneData.h>
1516
#include <utils/Logging.h>
@@ -105,35 +106,36 @@ bool SceneFile::Deserialise(std::vector<Entity*>& entities)
105106
};
106107

107108
// TODO: Implement proper write-mode handling for scene system
108-
String ScenePath = MakeScenePath(sceneName);
109+
String scenePath = MakeScenePath(sceneName);
109110
if (!SceneSystem::GetBaseDirectory().IsEmpty())
110111
{
111112
bool result = FileSystem::ForEachFileInDir(
112-
ScenePath,
113+
scenePath,
113114
[&deserialiseEntityString](const std::filesystem::path& path) {
114115
if (path.extension() != ENTITY_FILE_EXT) return;
115116
String entityData = FileSystem::Read(path.c_str());
116117
deserialiseEntityString(entityData, path);
117118
});
118119
if (!result)
119120
{
120-
CC_LOG_ERROR("Failed to read scene file at path \"{}\"", ScenePath)
121+
CC_LOG_ERROR("Failed to read scene file at path \"{}\"", scenePath)
121122
return false;
122123
}
123124
}
124125
else
125126
{
126127
PackFile* packFile = ResourceSystem::GetInstance().GetPackFile();
127128

128-
SceneData* sceneData = packFile->FindData<SceneData>(ScenePath);
129+
std::shared_ptr<SceneData> sceneData = packFile->FindDataDeserialised<SceneData>(scenePath);
129130
if (!sceneData)
130131
{
131-
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)
132135
return false;
133136
}
134137

135-
String sceneString(sceneData->data);
136-
for (const String& entityData : sceneString.Split('|'))
138+
for (const String& entityData : sceneData->entities)
137139
{
138140
deserialiseEntityString(entityData.Str(), "");
139141
}

engine/render/Makefile

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,18 @@ renderSources := $(call rwildcard,$(renderSrcDir)/,*.cpp)
1515
renderObjects := $(call findobjs,$(renderSrcDir),$(renderBinDir),$(renderSources))
1616
renderDepends := $(patsubst %.o, %.d, $(call rwildcard,$(renderBinDir)/,*.o))
1717
renderBuildDir := $(renderBinDir)/build
18-
renderLibs := $(vendorDir)/zlib/build/lib/libz.a $(vendorDir)/libpng/build/libpng.a $(vendorDir)/freetype/build/libfreetype.a
18+
renderLibs := $(vendorDir)/libpng/build/libpng.a $(vendorDir)/freetype/build/libfreetype.a
1919

2020
# Set shader build vars
2121
vertSources := $(call rwildcard,assets/shaders,*.vert)
2222
fragSources := $(call rwildcard,assets/shaders,*.frag)
2323
vertObjects := $(patsubst %.vert,$(renderBuildDir)/%.vert.spv,$(vertSources))
2424
fragObjects := $(patsubst %.frag,$(renderBuildDir)/%.frag.spv,$(fragSources))
2525

26+
# Set build vars
2627
linkFlags += -l utils -l window
27-
28-
compileFlags += -I $(vendorDir)/vulkan/include -I $(vendorDir)/glfw/include -I $(vendorDir)/glm \
29-
-I $(vendorDir)/zlib/build/include -I $(vendorDir)/libpng -I $(vendorDir)/include/freetype
28+
compileFlags += -I $(vendorDir)/vulkan/include -I $(vendorDir)/glfw/include \
29+
-I $(vendorDir)/libpng -I $(vendorDir)/include/freetype
3030

3131
.PHONY: all
3232

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/GenericFileData.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-
GenericFileData* fileData = packFile->FindData<GenericFileData>(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/GenericFileData.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-
GenericFileData* fileData = packFile->FindData<GenericFileData>(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: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111

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

1617
#include "Swapchain.h"
17-
#include "resources/StaticMeshData.h"
1818

1919
namespace Siege::Vulkan
2020
{
@@ -43,22 +43,25 @@ StaticMesh::StaticMesh(const char* filePath, Material* material)
4343
{
4444
// TODO(Aryeh): How to extract material data from object files?
4545
PackFile* packFile = ResourceSystem::GetInstance().GetPackFile();
46-
StaticMeshData* vertexData = packFile->FindData<StaticMeshData>(filePath);
46+
std::shared_ptr<StaticMeshData> staticMeshData =
47+
packFile->FindDataDeserialised<StaticMeshData>(filePath);
4748

48-
CC_ASSERT(vertexData->verticesCount > 0, "Cannot load in a file with no vertices!")
49-
CC_ASSERT(vertexData->indicesCount > 0, "Cannot load in a file with no indices!")
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!")
5051

51-
CC_ASSERT(vertexData->verticesCount < MAX_VERTICES, "The provided model has too many vertices!")
52-
CC_ASSERT(vertexData->indicesCount < MAX_INDICES, "The provided model has too many indices!")
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!")
5356

54-
vertexBuffer = VertexBuffer(sizeof(BaseVertex) * vertexData->verticesCount);
55-
vertexBuffer.Copy(vertexData->GetVertices(), sizeof(BaseVertex) * vertexData->verticesCount);
57+
vertexCount = staticMeshData->vertices.size();
58+
indexCount = staticMeshData->indices.size();
5659

57-
indexBuffer = IndexBuffer(sizeof(unsigned int) * vertexData->indicesCount);
58-
indexBuffer.Copy(vertexData->GetIndices(), sizeof(unsigned int) * vertexData->indicesCount);
60+
vertexBuffer = VertexBuffer(sizeof(BaseVertex) * vertexCount);
61+
vertexBuffer.Copy(staticMeshData->vertices.data(), sizeof(BaseVertex) * vertexCount);
5962

60-
vertexCount = vertexData->verticesCount;
61-
indexCount = vertexData->indicesCount;
63+
indexBuffer = IndexBuffer(sizeof(unsigned int) * indexCount);
64+
indexBuffer.Copy(staticMeshData->indices.data(), sizeof(unsigned int) * indexCount);
6265

6366
subMeshes = MHArray<SubMesh>(1);
6467
materials = MHArray<Material*>(1);

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@
99

1010
#include "Texture2D.h"
1111

12+
#include <resources/PackFile.h>
13+
#include <resources/ResourceSystem.h>
14+
#include <resources/Texture2DData.h>
1215
#include <utils/Defer.h>
1316

1417
#include "Constants.h"
1518
#include "Context.h"
1619
#include "render/renderer/buffer/Buffer.h"
17-
#include "resources/ResourceSystem.h"
18-
#include "resources/Texture2DData.h"
1920
#include "utils/Descriptor.h"
2021
#include "utils/TypeAdaptor.h"
2122

@@ -93,14 +94,13 @@ Texture2D& Texture2D::operator=(Texture2D&& other)
9394
void Texture2D::LoadFromFile(const char* filePath)
9495
{
9596
PackFile* packFile = ResourceSystem::GetInstance().GetPackFile();
96-
Texture2DData* texture2dData = packFile->FindData<Texture2DData>(filePath);
97-
uint64_t imageSize = texture2dData->GetImageSize();
98-
const uint8_t* pixelPtr = texture2dData->GetPixels();
97+
std::shared_ptr<Texture2DData> texture2dData =
98+
packFile->FindDataDeserialised<Texture2DData>(filePath);
9999

100100
Buffer::Buffer stagingBuffer;
101101
defer([&stagingBuffer] { Buffer::DestroyBuffer(stagingBuffer); });
102102

103-
Buffer::CreateBuffer(imageSize,
103+
Buffer::CreateBuffer(texture2dData->GetImageSize(),
104104
VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
105105
// specifies that data is accessible on the CPU.
106106
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
@@ -109,7 +109,7 @@ void Texture2D::LoadFromFile(const char* filePath)
109109
OUT stagingBuffer.buffer,
110110
OUT stagingBuffer.bufferMemory);
111111

112-
Buffer::CopyData(stagingBuffer, imageSize, pixelPtr);
112+
Buffer::CopyData(stagingBuffer, texture2dData->GetImageSize(), texture2dData->pixels.data());
113113

114114
extent = {static_cast<uint32_t>(texture2dData->texWidth),
115115
static_cast<uint32_t>(texture2dData->texHeight)};

engine/resources/Makefile

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,16 @@ resourcesBinDir := $(binDir)/engine/resources
1414
resourcesSources := $(call rwildcard,$(resourcesSrcDir)/,*.cpp)
1515
resourcesObjects := $(call findobjs,$(resourcesSrcDir),$(resourcesBinDir),$(resourcesSources))
1616
resourcesDepends := $(patsubst %.o, %.d, $(call rwildcard,$(resourcesBinDir)/,*.o))
17+
resourcesLibs := $(vendorDir)/zlib/build/lib/libz.a
1718

18-
linkFlags += -l utils
19+
# Set build vars
20+
linkFlags += -l utils -l z
21+
compileFlags += -I $(vendorDir)/zlib/build/include
1922

2023
# Build the static library
2124
$(resourcesLib): $(resourcesObjects)
2225
$(call MKDIR,$(call platformpth,$(libDir)))
23-
ar -crs $(resourcesLib) $(resourcesObjects)
26+
$(call COMBINE_LIBS, $(resourcesLibs), $(resourcesObjects), $(libDir), resources)
2427

2528
# Add all rules from dependency files
2629
-include $(resourcesDepends)

engine/resources/PackFile.cpp

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ bool PackFile::LoadFromPath(const String& filepath)
2828
{
2929
std::ifstream inputFileStream;
3030
inputFileStream.open(filepath, std::ios::in | std::ios::binary);
31-
inputFileStream.read(reinterpret_cast<char*>(&header), sizeof(PackFile::Header));
31+
inputFileStream.read(reinterpret_cast<char*>(&header), sizeof(Header));
3232

3333
body = reinterpret_cast<char*>(malloc(header.bodySize));
3434
inputFileStream.read(body, (uint32_t) header.bodySize);
@@ -41,7 +41,7 @@ bool PackFile::LoadFromPath(const String& filepath)
4141
char* tocEnd = body + header.bodySize;
4242
while (tocCurr < tocEnd)
4343
{
44-
PackFile::TocEntry* toc = reinterpret_cast<PackFile::TocEntry*>(tocCurr);
44+
TocEntry* toc = reinterpret_cast<TocEntry*>(tocCurr);
4545
if (!toc)
4646
{
4747
break;
@@ -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 {packFileData, free};
74+
}
75+
5576
const std::map<String, PackFile::TocEntry*>& PackFile::GetEntries()
5677
{
5778
return entries;
@@ -69,7 +90,7 @@ PackFile::TocEntry* PackFile::TocEntry::Create(const String& name,
6990
uint32_t nameDataSize = name.Size() + 1;
7091
void* mem = malloc(sizeof(TocEntry) + nameDataSize);
7192

72-
PackFile::TocEntry* tocEntry = new (mem) TocEntry();
93+
TocEntry* tocEntry = new (mem) TocEntry();
7394
tocEntry->dataOffset = dataOffset;
7495
tocEntry->dataSize = dataSize;
7596
strcpy(&tocEntry->name[0], name.Str());

0 commit comments

Comments
 (0)