Files
Seele/src/Editor/Asset/TextureLoader.cpp
T

151 lines
4.7 KiB
C++
Raw Normal View History

2020-06-02 11:46:18 +02:00
#include "TextureLoader.h"
2023-02-24 22:09:07 +01:00
#include "Asset/TextureAsset.h"
2020-06-02 11:46:18 +02:00
#include "Graphics/Graphics.h"
2023-02-24 22:09:07 +01:00
#include "Asset/AssetRegistry.h"
2023-11-05 10:36:01 +01:00
#include "Graphics/Vulkan/Enums.h"
2023-11-06 14:47:21 +01:00
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wsign-compare"
#pragma GCC diagnostic ignored "-Wunused-but-set-variable"
#pragma GCC diagnostic ignored "-Wunused-variable"
2020-06-02 11:46:18 +02:00
#define STB_IMAGE_IMPLEMENTATION
#include <stb_image.h>
2020-06-08 01:44:47 +02:00
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include <stb_image_write.h>
2023-11-06 14:47:21 +01:00
#pragma GCC diagnostic pop
2022-04-15 11:19:30 +02:00
#include "ktx.h"
2024-01-16 19:24:49 +01:00
#include <fstream>
2020-06-02 11:46:18 +02:00
using namespace Seele;
TextureLoader::TextureLoader(Gfx::PGraphics graphics)
: graphics(graphics)
{
2023-11-05 10:36:01 +01:00
OTextureAsset placeholder = new TextureAsset();
placeholderAsset = placeholder;
2023-02-13 14:56:13 +01:00
import(TextureImportArgs{
2023-11-06 14:47:21 +01:00
.filePath = std::filesystem::absolute("textures/placeholder.png"),
2023-02-13 14:56:13 +01:00
.importPath = "",
}, placeholderAsset);
2023-11-05 10:36:01 +01:00
AssetRegistry::get().assetRoot->textures[""] = std::move(placeholder);
2020-06-02 11:46:18 +02:00
}
TextureLoader::~TextureLoader()
{
}
2023-02-01 22:13:04 +01:00
void TextureLoader::importAsset(TextureImportArgs args)
2020-06-02 11:46:18 +02:00
{
2024-05-01 14:10:52 +02:00
std::string str = args.filePath.filename().string();
auto pos = str.rfind(".");
str.replace(str.begin() + pos, str.end(), "");
2023-11-05 10:36:01 +01:00
2024-05-01 14:10:52 +02:00
OTextureAsset asset = new TextureAsset(args.importPath, str);
2023-11-05 10:36:01 +01:00
PTextureAsset ref = asset;
2020-09-19 14:36:50 +02:00
asset->setStatus(Asset::Status::Loading);
2023-11-05 10:36:01 +01:00
AssetRegistry::get().registerTexture(std::move(asset));
import(args, ref);
2020-10-03 11:00:10 +02:00
}
PTextureAsset TextureLoader::getPlaceholderTexture()
{
return placeholderAsset;
2020-06-02 11:46:18 +02:00
}
2023-02-01 22:13:04 +01:00
void TextureLoader::import(TextureImportArgs args, PTextureAsset textureAsset)
2020-06-02 11:46:18 +02:00
{
2023-11-06 14:47:21 +01:00
int totalWidth = 0, totalHeight = 0, n = 0;
2023-02-01 22:13:04 +01:00
unsigned char* data = stbi_load(args.filePath.string().c_str(), &totalWidth, &totalHeight, &n, 4);
2023-11-06 14:47:21 +01:00
ktxTexture2* kTexture = nullptr;
2024-05-01 14:10:52 +02:00
ktxTextureCreateInfo createInfo = {
.vkFormat = VK_FORMAT_R8G8B8A8_UNORM,
.baseDepth = 1,
.numLevels = 1,
.numLayers = 1,
.isArray = false,
.generateMipmaps = false,
};
2023-02-01 22:13:04 +01:00
if (args.type == TextureImportType::TEXTURE_CUBEMAP)
2023-01-29 18:58:59 +01:00
{
uint32 faceWidth = totalWidth / 4;
2023-11-06 14:47:21 +01:00
// uint32 faceHeight = totalHeight / 3;
2023-01-29 18:58:59 +01:00
// Cube map
createInfo.baseWidth = totalWidth / 4;
createInfo.baseHeight = totalHeight / 3;
createInfo.numFaces = 6;
2023-02-01 22:13:04 +01:00
createInfo.numDimensions = 2;
2021-08-22 23:07:38 +02:00
2023-01-29 18:58:59 +01:00
ktxTexture2_Create(&createInfo,
KTX_TEXTURE_CREATE_ALLOC_STORAGE,
&kTexture);
2021-08-22 23:07:38 +02:00
2023-02-01 22:13:04 +01:00
auto loadCubeFace = [&kTexture, &faceWidth, &totalWidth, &data](int xPos, int yPos, int faceName)
2023-01-29 18:58:59 +01:00
{
std::vector<unsigned char> vec(faceWidth * faceWidth * 4);
2023-11-06 14:47:21 +01:00
for (uint32 y = 0; y < faceWidth; ++y)
2023-01-29 18:58:59 +01:00
{
2023-11-06 14:47:21 +01:00
for (uint32 x = 0; x < faceWidth; ++x)
2023-01-29 18:58:59 +01:00
{
int imgX = x + (xPos * faceWidth);
int imgY = y + (yPos * faceWidth);
std::memcpy(&vec[(x + (faceWidth * y)) * 4], &data[(imgX + (totalWidth * imgY)) * 4], 4);
}
}
ktxTexture_SetImageFromMemory(ktxTexture(kTexture),
0, 0, faceName, vec.data(), vec.size());
};
2023-02-01 22:13:04 +01:00
loadCubeFace(2, 1, 0); // +X
loadCubeFace(0, 1, 1); // -X
loadCubeFace(1, 0, 2); // +Y
loadCubeFace(1, 2, 3); // -Y
2023-02-13 14:56:13 +01:00
loadCubeFace(1, 1, 4); // +Z
2023-02-01 22:13:04 +01:00
loadCubeFace(3, 1, 5); // -Z
2023-01-29 18:58:59 +01:00
}
else
{
createInfo.baseWidth = totalWidth;
createInfo.baseHeight = totalHeight;
createInfo.numFaces = 1;
2023-02-13 14:56:13 +01:00
createInfo.numDimensions = 1 + (totalHeight > 1);
2023-01-29 18:58:59 +01:00
ktxTexture2_Create(&createInfo,
KTX_TEXTURE_CREATE_ALLOC_STORAGE,
&kTexture);
ktxTexture_SetImageFromMemory(ktxTexture(kTexture),
0, 0, 0, data, totalWidth * totalHeight * 4 * sizeof(unsigned char));
}
2023-07-31 21:43:20 +02:00
ktxBasisParams params2 = {
2023-02-13 14:56:13 +01:00
.structSize = sizeof(ktxBasisParams),
2023-07-31 21:43:20 +02:00
.uastc = false,
2023-02-13 14:56:13 +01:00
.threadCount = std::thread::hardware_concurrency(),
2023-11-06 14:47:21 +01:00
.compressionLevel = 0,
2024-05-01 14:10:52 +02:00
.qualityLevel = 1,
2023-02-13 14:56:13 +01:00
};
2023-07-31 21:43:20 +02:00
2024-05-01 14:10:52 +02:00
//ktx_error_code_e error = ktxTexture2_CompressBasisEx(kTexture, &params2);
//assert(error == KTX_SUCCESS);
2021-08-22 23:07:38 +02:00
2023-02-24 22:09:07 +01:00
ktx_uint8_t* dest;
ktx_size_t size;
ktxTexture_WriteToMemory(ktxTexture(kTexture), &dest, &size);
Array<uint8> memory(size);
std::memcpy(memory.data(), dest, size);
free(dest);
2023-07-31 21:43:20 +02:00
2020-06-08 01:44:47 +02:00
stbi_image_free(data);
2023-07-31 21:43:20 +02:00
ArchiveBuffer temp(graphics);
Serialization::save(temp, memory);
temp.rewind();
textureAsset->load(temp);
if (textureAsset->getName().empty())
{
return;
}
2024-05-01 14:10:52 +02:00
AssetRegistry::get().saveAsset(textureAsset, TextureAsset::IDENTIFIER, textureAsset->getFolderPath(), textureAsset->getName());
2024-04-19 18:23:36 +02:00
}