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"
|
|
|
|
|
|
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-07-19 10:42:59 +02:00
|
|
|
void TextureAsset::save(ArchiveBuffer& buffer) const {
|
|
|
|
|
Serialization::save(buffer, ktxData);
|
2024-07-18 11:45:56 +02:00
|
|
|
}
|
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
|
|
|
ktxTexture2* ktxHandle;
|
2024-07-05 12:02:46 +02:00
|
|
|
Serialization::load(buffer, ktxData);
|
|
|
|
|
KTX_ASSERT(
|
|
|
|
|
ktxTexture_CreateFromMemory(ktxData.data(), ktxData.size(), KTX_TEXTURE_CREATE_LOAD_IMAGE_DATA_BIT, (ktxTexture**)&ktxHandle));
|
2025-03-31 11:39:17 +02:00
|
|
|
ktxTexture2_TranscodeBasis(ktxHandle, KTX_TTF_RGBA32, 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)),
|
2024-07-05 12:02:46 +02:00
|
|
|
.owner = Gfx::QueueType::GRAPHICS,
|
2024-06-09 12:20:04 +02:00
|
|
|
},
|
2024-05-04 09:25:13 +02:00
|
|
|
.format = (Gfx::SeFormat)ktxHandle->vkFormat,
|
|
|
|
|
.width = ktxHandle->baseWidth,
|
|
|
|
|
.height = ktxHandle->baseHeight,
|
|
|
|
|
.depth = ktxHandle->baseDepth,
|
|
|
|
|
.elements = ktxHandle->numLayers,
|
2024-08-13 22:44:04 +02:00
|
|
|
.useMip = true,
|
2024-05-04 09:25:13 +02:00
|
|
|
.usage = Gfx::SE_IMAGE_USAGE_SAMPLED_BIT,
|
2024-07-15 17:55:22 +02:00
|
|
|
.name = name,
|
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
|
|
|
|
2024-05-04 09:25:13 +02:00
|
|
|
ktxTexture_Destroy(ktxTexture(ktxHandle));
|
2024-08-07 21:19:33 +02:00
|
|
|
|
|
|
|
|
byteSize = sizeof(TextureAsset) + ktxData.size();
|
2023-07-31 21:43:20 +02:00
|
|
|
}
|
2023-11-06 14:47:21 +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(); }
|