2022-04-13 13:01:35 +02:00
|
|
|
#include "FontAsset.h"
|
2023-01-29 18:58:59 +01:00
|
|
|
#include "Graphics/Graphics.h"
|
2023-10-26 18:37:29 +02:00
|
|
|
#include "Graphics/Vulkan/Enums.h"
|
|
|
|
|
#include "Graphics/Texture.h"
|
2023-02-13 14:56:13 +01:00
|
|
|
#include <ktx.h>
|
2022-04-13 13:01:35 +02:00
|
|
|
|
|
|
|
|
using namespace Seele;
|
|
|
|
|
|
|
|
|
|
FontAsset::FontAsset()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-13 14:56:13 +01:00
|
|
|
FontAsset::FontAsset(std::string_view folderPath, std::string_view name)
|
|
|
|
|
: Asset(folderPath, name)
|
2022-04-13 13:01:35 +02:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FontAsset::~FontAsset()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-13 14:56:13 +01:00
|
|
|
void FontAsset::save(ArchiveBuffer& buffer) const
|
2022-04-13 13:01:35 +02:00
|
|
|
{
|
2023-02-13 14:56:13 +01:00
|
|
|
uint64 numGlyphs = glyphs.size();
|
2023-02-24 22:09:07 +01:00
|
|
|
Serialization::save(buffer, numGlyphs);
|
2023-02-13 14:56:13 +01:00
|
|
|
for (auto& [index, glyph] : glyphs)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
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(),
|
2023-02-24 22:09:07 +01:00
|
|
|
.numLayers = glyph.texture->getSizeZ(),
|
2023-02-13 14:56:13 +01:00
|
|
|
.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);
|
|
|
|
|
|
2023-02-24 22:09:07 +01:00
|
|
|
Array<uint8> rawData(texSize);
|
|
|
|
|
std::memcpy(rawData.data(), texData, texSize);
|
|
|
|
|
|
2023-02-13 14:56:13 +01:00
|
|
|
free(texData);
|
|
|
|
|
|
2023-02-24 22:09:07 +01:00
|
|
|
Serialization::save(buffer, index);
|
|
|
|
|
Serialization::save(buffer, rawData);
|
2023-02-13 14:56:13 +01:00
|
|
|
Serialization::save(buffer, glyph.size);
|
|
|
|
|
Serialization::save(buffer, glyph.bearing);
|
|
|
|
|
Serialization::save(buffer, glyph.advance);
|
|
|
|
|
}
|
2022-04-13 13:01:35 +02:00
|
|
|
}
|
|
|
|
|
|
2022-04-15 23:45:44 +02:00
|
|
|
|
2023-02-13 14:56:13 +01:00
|
|
|
void FontAsset::load(ArchiveBuffer& buffer)
|
2022-04-13 13:01:35 +02:00
|
|
|
{
|
2023-02-13 14:56:13 +01:00
|
|
|
uint64 numGlyphs = glyphs.size();
|
2023-02-24 22:09:07 +01:00
|
|
|
Serialization::load(buffer, numGlyphs);
|
2023-02-13 14:56:13 +01:00
|
|
|
for (uint64 x = 0; x < numGlyphs; ++x)
|
|
|
|
|
{
|
|
|
|
|
uint32 index;
|
|
|
|
|
Serialization::load(buffer, index);
|
|
|
|
|
|
|
|
|
|
Glyph& glyph = glyphs[index];
|
|
|
|
|
|
|
|
|
|
Array<uint8> rawTex;
|
2023-02-24 22:09:07 +01:00
|
|
|
Serialization::load(buffer, rawTex);
|
2023-02-13 14:56:13 +01:00
|
|
|
|
|
|
|
|
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 = {
|
2023-11-01 23:12:30 +01:00
|
|
|
.sourceData = {
|
2023-02-13 14:56:13 +01:00
|
|
|
.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);
|
|
|
|
|
}
|
2022-04-13 13:01:35 +02:00
|
|
|
}
|