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

152 lines
6.1 KiB
C++
Raw Normal View History

2020-06-02 11:46:18 +02:00
#include "TextureLoader.h"
2024-06-09 12:20:04 +02:00
#include "Asset/AssetRegistry.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-11-05 10:36:01 +01:00
#include "Graphics/Vulkan/Enums.h"
2024-06-09 12:20:04 +02:00
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-07-18 11:45:56 +02:00
#include <ThreadPool.h>
2024-01-16 19:24:49 +01:00
#include <fstream>
2020-06-02 11:46:18 +02:00
using namespace Seele;
2024-06-09 12:20:04 +02:00
TextureLoader::TextureLoader(Gfx::PGraphics graphics) : graphics(graphics) {
2023-11-05 10:36:01 +01:00
OTextureAsset placeholder = new TextureAsset();
placeholderAsset = placeholder;
2024-06-09 12:20:04 +02:00
import(
TextureImportArgs{
.filePath = std::filesystem::absolute("textures/placeholder.png"),
.importPath = "",
},
placeholderAsset);
2024-07-19 10:42:59 +02:00
AssetRegistry::get().registerTexture(std::move(placeholder));
2020-06-02 11:46:18 +02:00
}
2024-06-09 12:20:04 +02:00
TextureLoader::~TextureLoader() {}
2020-06-02 11:46:18 +02:00
2024-06-09 12:20:04 +02:00
void TextureLoader::importAsset(TextureImportArgs args) {
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(), "");
2024-06-09 12:20:04 +02: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));
2024-08-26 22:35:22 +02:00
getThreadPool().runAsync([=](){import(args, ref);});
2020-10-03 11:00:10 +02:00
}
2024-06-09 12:20:04 +02:00
PTextureAsset TextureLoader::getPlaceholderTexture() { return placeholderAsset; }
2020-06-02 11:46:18 +02:00
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(); \
} \
}
2024-06-09 12:20:04 +02:00
void TextureLoader::import(TextureImportArgs args, PTextureAsset textureAsset) {
2024-07-05 12:02:46 +02:00
int totalWidth = 0, totalHeight = 0, n = 0;
2024-07-16 09:56:47 +02:00
unsigned char* data = stbi_load(args.filePath.string().c_str(), &totalWidth, &totalHeight, &n, 4);
2024-07-05 12:02:46 +02:00
ktxTexture2* kTexture = nullptr;
2024-07-16 09:56:47 +02:00
VkFormat format = VK_FORMAT_R8G8B8A8_UNORM;
2024-07-05 12:02:46 +02:00
ktxTextureCreateInfo createInfo = {
.vkFormat = (uint32)format,
.baseDepth = 1,
.numLevels = 1,
.numLayers = 1,
.isArray = false,
.generateMipmaps = false,
};
2023-07-31 21:43:20 +02:00
2024-07-05 12:02:46 +02:00
if (args.type == TextureImportType::TEXTURE_CUBEMAP) {
uint32 faceWidth = totalWidth / 4;
// uint32 faceHeight = totalHeight / 3;
// Cube map
createInfo.baseWidth = totalWidth / 4;
createInfo.baseHeight = totalHeight / 3;
createInfo.numFaces = 6;
createInfo.numDimensions = 2;
2023-07-31 21:43:20 +02:00
2024-07-05 12:02:46 +02:00
KTX_ASSERT(ktxTexture2_Create(&createInfo, KTX_TEXTURE_CREATE_ALLOC_STORAGE, &kTexture));
2023-02-24 22:09:07 +01:00
2024-07-16 09:56:47 +02:00
auto loadCubeFace = [&kTexture, faceWidth, totalWidth, &data](int xPos, int yPos, int faceName) {
std::vector<unsigned char> vec(faceWidth * faceWidth * 4);
2024-07-05 12:02:46 +02:00
for (uint32 y = 0; y < faceWidth; ++y) {
for (uint32 x = 0; x < faceWidth; ++x) {
int imgX = x + (xPos * faceWidth);
int imgY = y + (yPos * faceWidth);
2024-07-16 09:56:47 +02:00
std::memcpy(&vec[(x + (faceWidth * y)) * 4], &data[(imgX + (totalWidth * imgY)) * 4], 4);
2024-07-05 12:02:46 +02:00
}
}
ktxTexture_SetImageFromMemory(ktxTexture(kTexture), 0, 0, faceName, vec.data(), vec.size());
};
loadCubeFace(2, 1, 0); // +X
loadCubeFace(0, 1, 1); // -X
loadCubeFace(1, 0, 2); // +Y
loadCubeFace(1, 2, 3); // -Y
loadCubeFace(1, 1, 4); // +Z
loadCubeFace(3, 1, 5); // -Z
} else {
createInfo.baseWidth = totalWidth;
createInfo.baseHeight = totalHeight;
createInfo.numFaces = 1;
createInfo.numDimensions = 1 + (totalHeight > 1);
2024-07-05 12:02:46 +02:00
ktxTexture2_Create(&createInfo, KTX_TEXTURE_CREATE_ALLOC_STORAGE, &kTexture);
2024-07-16 09:56:47 +02:00
ktxTexture_SetImageFromMemory(ktxTexture(kTexture), 0, 0, 0, data, totalWidth * totalHeight * 4 * sizeof(unsigned char));
2024-07-05 12:02:46 +02:00
}
ktxBasisParams basisParams = {
.structSize = sizeof(ktxBasisParams),
.uastc = true,
2024-08-26 22:35:22 +02:00
.threadCount = 1,
2024-07-12 13:33:52 +02:00
.uastcFlags = KTX_PACK_UASTC_LEVEL_DEFAULT,
2024-07-05 12:02:46 +02:00
.uastcRDO = true,
};
2024-08-07 21:19:33 +02:00
KTX_ASSERT(ktxTexture2_CompressBasisEx(kTexture, &basisParams));
KTX_ASSERT(ktxTexture2_DeflateZstd(kTexture, 20));
2024-07-05 12:02:46 +02:00
char writer[100];
snprintf(writer, sizeof(writer), "%s version %s", "SeeleEngine", "0.0.1");
ktxHashList_AddKVPair(&kTexture->kvDataHead, KTX_WRITER_KEY, (ktx_uint32_t)strlen(writer) + 1, writer);
2024-07-05 12:02:46 +02:00
uint8* texData;
size_t texSize;
KTX_ASSERT(ktxTexture_WriteToMemory(ktxTexture(kTexture), &texData, &texSize));
2024-07-18 11:45:56 +02:00
// ktxTexture_WriteToNamedFile(ktxTexture(kTexture), args.filePath.filename().replace_extension(".ktx").generic_string().c_str());
2024-07-05 12:02:46 +02:00
Array<uint8> serialized(texSize);
std::memcpy(serialized.data(), texData, texSize);
stbi_image_free(data);
ktxTexture_Destroy(ktxTexture(kTexture));
2023-07-31 21:43:20 +02:00
2024-06-09 12:20:04 +02:00
if (textureAsset->getName().empty()) {
2023-07-31 21:43:20 +02:00
return;
}
ArchiveBuffer buffer(graphics);
2024-07-18 11:45:56 +02:00
Serialization::save(buffer, serialized);
buffer.rewind();
2024-07-19 10:42:59 +02:00
textureAsset->load(buffer);
2024-07-19 10:42:59 +02:00
textureAsset->setTexture(std::move(serialized));
AssetRegistry::saveAsset(textureAsset, TextureAsset::IDENTIFIER, textureAsset->getFolderPath(), textureAsset->getName());
2024-08-26 22:35:22 +02:00
2024-07-19 10:42:59 +02:00
textureAsset->setStatus(Asset::Status::Ready);
2024-04-19 18:23:36 +02:00
}