Files
Seele/src/Engine/Asset/TextureAsset.cpp
T

101 lines
2.7 KiB
C++
Raw Normal View History

2020-06-02 11:46:18 +02:00
#include "TextureAsset.h"
2020-06-08 01:44:47 +02:00
#include "Graphics/Graphics.h"
2021-01-19 15:30:00 +01:00
#include "Window/WindowManager.h"
2023-10-26 18:37:29 +02:00
#include "Graphics/Vulkan/Enums.h"
2023-11-06 14:47:21 +01:00
#include "Graphics/Texture.h"
2022-04-15 11:19:30 +02:00
#include "ktx.h"
2020-06-02 11:46:18 +02:00
using namespace Seele;
2023-02-13 14:56:13 +01:00
#define KTX_CHECK(x) { ktx_error_code_e err = x; assert(err == KTX_SUCCESS); }
2020-06-02 11:46:18 +02:00
TextureAsset::TextureAsset()
{
}
2023-02-13 14:56:13 +01:00
TextureAsset::TextureAsset(std::string_view folderPath, std::string_view name)
: Asset(folderPath, name)
2020-06-02 11:46:18 +02:00
{
}
2020-06-08 01:44:47 +02:00
TextureAsset::~TextureAsset()
2020-06-02 11:46:18 +02:00
{
2024-05-01 14:10:52 +02:00
ktxTexture_Destroy(ktxTexture(ktxHandle));
2020-06-08 01:44:47 +02:00
}
2024-05-01 14:10:52 +02:00
void TextureAsset::save(ArchiveBuffer& buffer) const
2020-06-08 01:44:47 +02:00
{
2023-02-13 14:56:13 +01:00
char writer[100];
snprintf(writer, sizeof(writer), "%s version %s", "SeeleEngine", "0.0.1");
2024-05-01 14:10:52 +02:00
ktxHashList_AddKVPair(&ktxHandle->kvDataHead, KTX_WRITER_KEY,
2023-02-13 14:56:13 +01:00
(ktx_uint32_t)strlen(writer) + 1,
writer);
ktx_uint8_t* texData;
ktx_size_t texSize;
2024-05-01 14:10:52 +02:00
KTX_CHECK(ktxTexture_WriteToMemory(ktxTexture(ktxHandle), &texData, &texSize));
2023-02-13 14:56:13 +01:00
Array<uint8> rawData(texSize);
std::memcpy(rawData.data(), texData, texSize);
2023-07-31 21:43:20 +02:00
Serialization::save(buffer, rawData);
2024-05-01 14:10:52 +02:00
free(texData);
2020-06-08 01:44:47 +02:00
}
2023-02-13 14:56:13 +01:00
void TextureAsset::load(ArchiveBuffer& buffer)
2020-06-08 01:44:47 +02:00
{
2023-07-31 21:43:20 +02:00
Gfx::PGraphics graphics = buffer.getGraphics();
2023-02-13 14:56:13 +01:00
Array<uint8> rawData;
Serialization::load(buffer, rawData);
2023-07-31 21:43:20 +02:00
KTX_CHECK(ktxTexture_CreateFromMemory(rawData.data(),
rawData.size(),
2023-02-24 22:09:07 +01:00
KTX_TEXTURE_CREATE_LOAD_IMAGE_DATA_BIT,
2024-05-01 14:10:52 +02:00
(ktxTexture**)&ktxHandle));
2023-02-13 14:56:13 +01:00
2024-05-01 14:10:52 +02:00
//ktx_error_code_e e = ktxTexture2_TranscodeBasis(ktxHandle, KTX_TTF_BC7_RGBA, 0);
//assert(e == ktx_error_code_e::KTX_SUCCESS);
2023-02-13 14:56:13 +01:00
TextureCreateInfo createInfo = {
2023-11-01 23:12:30 +01:00
.sourceData = {
2024-05-01 14:10:52 +02:00
.size = ktxTexture_GetDataSize(ktxTexture(ktxHandle)),
.data = ktxTexture_GetData(ktxTexture(ktxHandle)),
2024-02-01 08:42:24 +01:00
.owner = Gfx::QueueType::TRANSFER,
2023-02-13 14:56:13 +01:00
},
2024-05-01 14:10:52 +02:00
.format = (Gfx::SeFormat)ktxHandle->vkFormat,
.width = ktxHandle->baseWidth,
.height = ktxHandle->baseHeight,
.depth = ktxHandle->baseDepth,
.mipLevels = ktxHandle->numLevels,
.layers = ktxHandle->numFaces,
.elements = ktxHandle->numLayers,
2023-02-13 14:56:13 +01:00
.usage = Gfx::SE_IMAGE_USAGE_SAMPLED_BIT,
};
2024-05-01 14:10:52 +02:00
if (ktxHandle->isCubemap)
2023-01-29 18:58:59 +01:00
{
2023-02-24 22:09:07 +01:00
texture = graphics->createTextureCube(createInfo);
2023-01-29 18:58:59 +01:00
}
2024-05-01 14:10:52 +02:00
else if (ktxHandle->isArray)
2023-01-29 18:58:59 +01:00
{
2023-02-24 22:09:07 +01:00
texture = graphics->createTexture3D(createInfo);
2023-01-29 18:58:59 +01:00
}
else
{
2023-02-24 22:09:07 +01:00
texture = graphics->createTexture2D(createInfo);
2023-01-29 18:58:59 +01:00
}
2023-02-24 22:09:07 +01:00
texture->transferOwnership(Gfx::QueueType::GRAPHICS);
2023-07-31 21:43:20 +02:00
}
2023-11-06 14:47:21 +01:00
void TextureAsset::setTexture(Gfx::OTexture _texture)
{
texture = std::move(_texture);
}
2023-11-11 13:56:12 +01:00
uint32 TextureAsset::getWidth()
{
2023-11-15 00:06:00 +01:00
return texture->getWidth();
2023-11-11 13:56:12 +01:00
}
uint32 TextureAsset::getHeight()
{
2023-11-15 00:06:00 +01:00
return texture->getHeight();
2023-11-11 13:56:12 +01:00
}