2020-06-02 11:46:18 +02:00
|
|
|
#include "TextureAsset.h"
|
2020-06-08 01:44:47 +02:00
|
|
|
#include "Graphics/Graphics.h"
|
2023-11-06 14:47:21 +01:00
|
|
|
#include "Graphics/Texture.h"
|
2024-06-09 12:20:04 +02:00
|
|
|
#include "Graphics/Vulkan/Enums.h"
|
|
|
|
|
#include "Window/WindowManager.h"
|
2022-04-15 11:19:30 +02:00
|
|
|
#include "ktx.h"
|
|
|
|
|
|
2024-06-09 12:20:04 +02:00
|
|
|
|
2020-06-02 11:46:18 +02:00
|
|
|
using namespace Seele;
|
|
|
|
|
|
2024-06-09 12:20:04 +02:00
|
|
|
#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
|
|
|
|
2024-06-09 12:20:04 +02:00
|
|
|
TextureAsset::TextureAsset() {}
|
2023-02-13 14:56:13 +01:00
|
|
|
|
2024-06-09 12:20:04 +02:00
|
|
|
TextureAsset::TextureAsset(std::string_view folderPath, std::string_view name) : Asset(folderPath, name) {}
|
2020-06-08 01:44:47 +02:00
|
|
|
|
2024-06-09 12:20:04 +02:00
|
|
|
TextureAsset::~TextureAsset() {}
|
2020-06-08 01:44:47 +02:00
|
|
|
|
2024-06-09 12:20:04 +02:00
|
|
|
void TextureAsset::save(ArchiveBuffer& buffer) const {
|
|
|
|
|
// 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));
|
2024-05-04 09:25:13 +02:00
|
|
|
//
|
2024-06-09 12:20:04 +02:00
|
|
|
// Array<uint8> rawData(texSize);
|
|
|
|
|
// std::memcpy(rawData.data(), texData, texSize);
|
|
|
|
|
// Serialization::save(buffer, rawData);
|
|
|
|
|
// free(texData);
|
2020-06-08 01:44:47 +02:00
|
|
|
}
|
|
|
|
|
|
2024-06-09 12:20:04 +02:00
|
|
|
void TextureAsset::load(ArchiveBuffer& buffer) {
|
2024-05-04 09:25:13 +02:00
|
|
|
std::string ktxPath;
|
|
|
|
|
Serialization::load(buffer, ktxPath);
|
|
|
|
|
ktxTexture2* ktxHandle;
|
2024-06-09 12:20:04 +02:00
|
|
|
|
|
|
|
|
KTX_ASSERT(ktxTexture_CreateFromNamedFile(ktxPath.c_str(), KTX_TEXTURE_CREATE_NO_FLAGS, (ktxTexture**)&ktxHandle));
|
2023-02-13 14:56:13 +01:00
|
|
|
|
2024-05-04 09:25:13 +02:00
|
|
|
KTX_ASSERT(ktxTexture2_TranscodeBasis(ktxHandle, KTX_TTF_BC7_RGBA, 0));
|
2023-02-13 14:56:13 +01:00
|
|
|
|
2024-05-04 09:25:13 +02:00
|
|
|
Gfx::PGraphics graphics = buffer.getGraphics();
|
2023-02-13 14:56:13 +01:00
|
|
|
TextureCreateInfo createInfo = {
|
2024-06-09 12:20:04 +02:00
|
|
|
.sourceData =
|
|
|
|
|
{
|
|
|
|
|
.size = ktxTexture_GetDataSize(ktxTexture(ktxHandle)),
|
|
|
|
|
.data = ktxTexture_GetData(ktxTexture(ktxHandle)),
|
|
|
|
|
.owner = Gfx::QueueType::TRANSFER,
|
|
|
|
|
},
|
2024-05-04 09:25:13 +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,
|
|
|
|
|
.usage = Gfx::SE_IMAGE_USAGE_SAMPLED_BIT,
|
2023-02-13 14:56:13 +01:00
|
|
|
};
|
2024-06-09 12:20:04 +02:00
|
|
|
if (ktxHandle->isCubemap) {
|
2023-02-24 22:09:07 +01:00
|
|
|
texture = graphics->createTextureCube(createInfo);
|
2024-06-09 12:20:04 +02:00
|
|
|
} else if (ktxHandle->isArray) {
|
2023-02-24 22:09:07 +01:00
|
|
|
texture = graphics->createTexture3D(createInfo);
|
2024-06-09 12:20:04 +02:00
|
|
|
} else {
|
2023-02-24 22:09:07 +01:00
|
|
|
texture = graphics->createTexture2D(createInfo);
|
2023-01-29 18:58:59 +01:00
|
|
|
}
|
2024-06-09 12:20:04 +02:00
|
|
|
|
2023-02-24 22:09:07 +01:00
|
|
|
texture->transferOwnership(Gfx::QueueType::GRAPHICS);
|
2024-05-04 09:25:13 +02:00
|
|
|
ktxTexture_Destroy(ktxTexture(ktxHandle));
|
2023-07-31 21:43:20 +02:00
|
|
|
}
|
2023-11-06 14:47:21 +01:00
|
|
|
|
2024-06-09 12:20:04 +02:00
|
|
|
void TextureAsset::setTexture(Gfx::OTexture _texture) { texture = std::move(_texture); }
|
2023-11-11 13:56:12 +01:00
|
|
|
|
2024-06-09 12:20:04 +02:00
|
|
|
uint32 TextureAsset::getWidth() { return texture->getWidth(); }
|
2023-11-11 13:56:12 +01:00
|
|
|
|
2024-06-09 12:20:04 +02:00
|
|
|
uint32 TextureAsset::getHeight() { return texture->getHeight(); }
|