Skip to content

Commit 134cd4a

Browse files
committed
Updated texture2d serialisation to use binary serialiser
1 parent 53cf63c commit 134cd4a

File tree

5 files changed

+44
-51
lines changed

5 files changed

+44
-51
lines changed

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

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "Constants.h"
1515
#include "Context.h"
1616
#include "render/renderer/buffer/Buffer.h"
17+
#include "resources/PackFileData.h"
1718
#include "resources/ResourceSystem.h"
1819
#include "resources/Texture2DData.h"
1920
#include "utils/Descriptor.h"
@@ -93,14 +94,21 @@ Texture2D& Texture2D::operator=(Texture2D&& other)
9394
void Texture2D::LoadFromFile(const char* filePath)
9495
{
9596
PackFile* packFile = ResourceSystem::GetInstance().GetPackFile();
96-
std::shared_ptr<Texture2DData> texture2dData = packFile->FindData<Texture2DData>(filePath);
97-
uint64_t imageSize = texture2dData->GetImageSize();
98-
const uint8_t* pixelPtr = texture2dData->GetPixels();
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);
99107

100108
Buffer::Buffer stagingBuffer;
101109
defer([&stagingBuffer] { Buffer::DestroyBuffer(stagingBuffer); });
102110

103-
Buffer::CreateBuffer(imageSize,
111+
Buffer::CreateBuffer(texture2dData.GetImageSize(),
104112
VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
105113
// specifies that data is accessible on the CPU.
106114
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
@@ -109,13 +117,13 @@ void Texture2D::LoadFromFile(const char* filePath)
109117
OUT stagingBuffer.buffer,
110118
OUT stagingBuffer.bufferMemory);
111119

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

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

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

engine/resources/Texture2DData.h

Lines changed: 9 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -11,61 +11,34 @@
1111
#define SIEGE_ENGINE_TEXTURE2DDATA_H
1212

1313
#include "PackFile.h"
14+
#include <utils/BinarySerialisation.h>
1415

1516
// TODO - Remove this hard-coded value and support various texture channel sizes
1617
#define TEXTURE2D_TEXTURE_CHANNELS 4
1718

1819
namespace Siege
1920
{
2021

21-
#pragma pack(push, 1)
22-
struct Texture2DData
22+
struct Texture2DData : BinarySerialisation::BinarySerialisable
2323
{
2424
int32_t texWidth = 0;
2525
int32_t texHeight = 0;
26-
int32_t texChannels = 0;
27-
uint8_t pixels[];
28-
29-
const uint8_t* GetPixels() const
30-
{
31-
return &pixels[0];
32-
}
26+
int32_t texChannels = TEXTURE2D_TEXTURE_CHANNELS;
27+
std::vector<uint8_t> pixels;
3328

3429
uint64_t GetImageSize() const
3530
{
3631
return texWidth * texHeight * TEXTURE2D_TEXTURE_CHANNELS;
3732
}
3833

39-
static Texture2DData* Create(uint8_t* pixels,
40-
int32_t texWidth,
41-
int32_t texHeight,
42-
int32_t texChannels)
43-
{
44-
uint64_t imageSize = texWidth * texHeight * TEXTURE2D_TEXTURE_CHANNELS;
45-
uint32_t pixelDataSize = sizeof(uint8_t) * imageSize;
46-
uint32_t totalDataSize = sizeof(Texture2DData) + pixelDataSize;
47-
48-
void* mem = malloc(totalDataSize);
49-
Texture2DData* texture2dData = new (mem) Texture2DData();
50-
51-
texture2dData->texWidth = texWidth;
52-
texture2dData->texHeight = texHeight;
53-
texture2dData->texChannels = texChannels;
54-
// Ensure we get no garbage data in the pixel array
55-
memset(&texture2dData->pixels[0], 0, imageSize);
56-
memcpy(&texture2dData->pixels[0], pixels, sizeof(uint8_t) * imageSize);
57-
58-
return texture2dData;
59-
}
60-
61-
static uint32_t GetDataSize(void* objectData)
34+
void serialise(BinarySerialisation::Buffer& buffer, BinarySerialisation::SerialisationMode mode) override
6235
{
63-
if (!objectData) return 0;
64-
Texture2DData* textureData = reinterpret_cast<Texture2DData*>(objectData);
65-
return sizeof(Texture2DData) + sizeof(uint8_t) * textureData->GetImageSize();
36+
BinarySerialisation::serialise(buffer, texWidth, mode);
37+
BinarySerialisation::serialise(buffer, texHeight, mode);
38+
BinarySerialisation::serialise(buffer, texChannels, mode);
39+
BinarySerialisation::serialise(buffer, pixels, mode);
6640
}
6741
};
68-
#pragma pack(pop)
6942

7043
} // namespace Siege
7144

engine/utils/BinarySerialisation.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ void serialiseNative(Buffer& buffer, const T& value, SerialisationMode mode)
8484

8585
SERIALISE_NATIVE(bool)
8686
SERIALISE_NATIVE(char)
87+
SERIALISE_NATIVE(unsigned char)
8788
SERIALISE_NATIVE(uint32_t)
8889
SERIALISE_NATIVE(int32_t)
8990
SERIALISE_NATIVE(float)

packer/src/types/Texture2DDataPacker.cpp

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

1212
#define STB_IMAGE_IMPLEMENTATION
1313
#include <resources/Texture2DData.h>
14+
#include <resources/PackFileData.h>
15+
#include <utils/BinarySerialisation.h>
1416
#include <stb_image.h>
1517
#include <utils/Defer.h>
1618
#include <utils/Logging.h>
@@ -27,8 +29,18 @@ void* PackTexture2DFile(const Siege::String& filePath, uint32_t& fileSize)
2729
texHeight,
2830
texChannels)
2931

30-
Siege::Texture2DData* texture2dData =
31-
Siege::Texture2DData::Create(pixels, texWidth, texHeight, texChannels);
32-
fileSize = Siege::Texture2DData::GetDataSize(texture2dData);
33-
return texture2dData;
32+
Siege::Texture2DData texture2dData;
33+
texture2dData.texWidth = texWidth;
34+
texture2dData.texHeight = texHeight;
35+
texture2dData.texChannels = TEXTURE2D_TEXTURE_CHANNELS;
36+
std::copy_n(pixels, texture2dData.GetImageSize(), std::back_inserter(texture2dData.pixels));
37+
38+
Siege::BinarySerialisation::Buffer dataBuffer;
39+
texture2dData.serialise(dataBuffer, Siege::BinarySerialisation::SERIALISE);
40+
41+
fileSize = dataBuffer.data.size();
42+
char* data = reinterpret_cast<char*>(dataBuffer.data.data());
43+
44+
Siege::PackFileData* fileData = Siege::PackFileData::Create(data, fileSize);
45+
return fileData;
3446
}

tests/src/resources/test_ResourceSystem.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,7 @@ UTEST_F(test_ResourceSystem, LoadTextureData)
168168
ASSERT_TRUE(data);
169169
ASSERT_EQ(160000, data->GetImageSize());
170170

171-
const uint8_t* actualPixels = data->GetPixels();
172-
ASSERT_TRUE(actualPixels);
171+
ASSERT_EQ(160000, data->pixels.size());
173172
}
174173

175174
UTEST_F(test_ResourceSystem, LoadSceneData)

0 commit comments

Comments
 (0)