Implementing basic asset serialization

This commit is contained in:
Dynamitos
2023-02-13 14:56:13 +01:00
parent 9e1e4076f0
commit 48fa098546
82 changed files with 1834 additions and 423 deletions
+103 -9
View File
@@ -1,5 +1,7 @@
#include "FontAsset.h"
#include "Graphics/Graphics.h"
#include "Graphics/Vulkan/VulkanGraphicsEnums.h"
#include <ktx.h>
using namespace Seele;
@@ -7,13 +9,8 @@ FontAsset::FontAsset()
{
}
FontAsset::FontAsset(const std::string& directory, const std::string& name)
: Asset(directory, name)
{
}
FontAsset::FontAsset(const std::filesystem::path& fullPath)
: Asset(fullPath)
FontAsset::FontAsset(std::string_view folderPath, std::string_view name)
: Asset(folderPath, name)
{
}
@@ -22,11 +19,108 @@ FontAsset::~FontAsset()
}
void FontAsset::save(Gfx::PGraphics graphics)
void FontAsset::save(ArchiveBuffer& buffer) const
{
uint64 numGlyphs = glyphs.size();
buffer.writeBytes(&numGlyphs, sizeof(uint64));
for (auto& [index, glyph] : glyphs)
{
Serialization::save(buffer, index);
Array<uint8> textureData;
ktxTexture2* kTexture;
ktxTextureCreateInfo createInfo = {
.vkFormat = (uint32_t)glyph.texture->getFormat(),
.baseWidth = glyph.texture->getSizeX(),
.baseHeight = glyph.texture->getSizeY(),
.baseDepth = glyph.texture->getSizeZ(),
.numDimensions = glyph.texture->getSizeZ() > 1 ? 3u : glyph.texture->getSizeY() > 1 ? 2u : 1u,
.numLevels = glyph.texture->getMipLevels(),
.numFaces = glyph.texture->getNumFaces(),
.isArray = false,
.generateMipmaps = false,
};
ktxTexture2_Create(&createInfo, KTX_TEXTURE_CREATE_ALLOC_STORAGE, &kTexture);
for (uint32 depth = 0; depth < glyph.texture->getSizeZ(); ++depth)
{
for (uint32 face = 0; face < glyph.texture->getNumFaces(); ++face)
{
// technically, downloading cant be const, because we have to allocate temporary buffers and change layouts
// but practically the texture stays the same
glyph.texture->download(0, depth, face, textureData);
ktxTexture_SetImageFromMemory(ktxTexture(kTexture), 0, depth, face, textureData.data(), textureData.size());
}
}
char writer[100];
snprintf(writer, sizeof(writer), "%s version %s", "SeeleEngine", "0.0.1");
ktxHashList_AddKVPair(&kTexture->kvDataHead, KTX_WRITER_KEY,
(ktx_uint32_t)strlen(writer) + 1,
writer);
ktxTexture2_TranscodeBasis(kTexture, KTX_TTF_ASTC_4x4_RGBA, 0);
ktx_uint8_t* texData;
ktx_size_t texSize;
ktxTexture_WriteToMemory(ktxTexture(kTexture), &texData, &texSize);
buffer.writeBytes(texData, texSize);
free(texData);
Serialization::save(buffer, glyph.size);
Serialization::save(buffer, glyph.bearing);
Serialization::save(buffer, glyph.advance);
}
}
void FontAsset::load(Gfx::PGraphics graphics)
void FontAsset::load(ArchiveBuffer& buffer)
{
uint64 numGlyphs = glyphs.size();
buffer.readBytes(&numGlyphs, sizeof(uint64));
for (uint64 x = 0; x < numGlyphs; ++x)
{
uint32 index;
Serialization::load(buffer, index);
Glyph& glyph = glyphs[index];
uint64 texSize;
buffer.readBytes(&texSize, sizeof(uint64));
Array<uint8> rawTex;
rawTex.resize(texSize);
buffer.readBytes(rawTex.data(), rawTex.size());
ktxTexture2* kTexture;
ktxTexture2_CreateFromMemory(rawTex.data(),
rawTex.size(),
KTX_TEXTURE_CREATE_LOAD_IMAGE_DATA_BIT,
&kTexture);
ktxTexture2_TranscodeBasis(kTexture, KTX_TTF_BC7_RGBA, 0);
TextureCreateInfo createInfo = {
.resourceData = {
.size = ktxTexture_GetDataSize(ktxTexture(kTexture)),
.data = ktxTexture_GetData(ktxTexture(kTexture)),
.owner = Gfx::QueueType::GRAPHICS,
},
.width = kTexture->baseWidth,
.height = kTexture->baseHeight,
.depth = kTexture->baseDepth,
.bArray = kTexture->isArray,
.arrayLayers = kTexture->isArray ? kTexture->numLayers : kTexture->numFaces,
.mipLevels = kTexture->numLevels,
.format = Vulkan::cast((VkFormat)kTexture->vkFormat),
.usage = Gfx::SE_IMAGE_USAGE_SAMPLED_BIT,
};
glyph.texture = buffer.getGraphics()->createTexture2D(createInfo);
ktxTexture_Destroy(ktxTexture(kTexture));
Serialization::load(buffer, glyph.size);
Serialization::load(buffer, glyph.bearing);
Serialization::load(buffer, glyph.advance);
}
}