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

107 lines
2.9 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;
#define KTX_ASSERT(x) { auto error = x; if(error != KTX_SUCCESS) { std::cout << ktxErrorString(error) << std::endl; abort(); } }
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
{
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
{
//ktxBasisParams basisParams = {
// .structSize = sizeof(ktxBasisParams),
// .uastc = true,
// .threadCount = std::thread::hardware_concurrency(),
// .normalMap = normalMap,
// .uastcFlags = KTX_PACK_UASTC_LEVEL_VERYSLOW,
// .uastcRDO = true,
// .uastcRDOQualityScalar = 1,
//};
//KTX_ASSERT(ktxTexture2_CompressBasisEx(ktxHandle, &basisParams));
//KTX_ASSERT(ktxTexture2_DeflateZstd(ktxHandle, 20));
//ktx_uint8_t* texData;
//ktx_size_t texSize;
//KTX_ASSERT(ktxTexture_WriteToMemory(ktxTexture(ktxHandle), &texData, &texSize));
//
//Array<uint8> rawData(texSize);
//std::memcpy(rawData.data(), texData, texSize);
//Serialization::save(buffer, rawData);
//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
{
std::string ktxPath;
Serialization::load(buffer, ktxPath);
ktxTexture2* ktxHandle;
KTX_ASSERT(ktxTexture_CreateFromNamedFile(ktxPath.c_str(),
KTX_TEXTURE_CREATE_NO_FLAGS,
2024-05-01 14:10:52 +02:00
(ktxTexture**)&ktxHandle));
2023-02-13 14:56:13 +01:00
KTX_ASSERT(ktxTexture2_TranscodeBasis(ktxHandle, KTX_TTF_BC7_RGBA, 0));
2023-02-13 14:56:13 +01:00
Gfx::PGraphics graphics = buffer.getGraphics();
2023-02-13 14:56:13 +01:00
TextureCreateInfo createInfo = {
.sourceData = {
.size = ktxTexture_GetDataSize(ktxTexture(ktxHandle)),
.data = ktxTexture_GetData(ktxTexture(ktxHandle)),
.owner = Gfx::QueueType::TRANSFER,
},
.format = (Gfx::SeFormat)ktxHandle->vkFormat,
.width = ktxHandle->baseWidth,
.height = ktxHandle->baseHeight,
.depth = ktxHandle->baseDepth,
.mipLevels = ktxHandle->numLevels,
.layers = ktxHandle->numFaces,
.elements = ktxHandle->numLayers,
.usage = Gfx::SE_IMAGE_USAGE_SAMPLED_BIT,
2023-02-13 14:56:13 +01:00
};
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);
ktxTexture_Destroy(ktxTexture(ktxHandle));
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
}