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

153 lines
4.9 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"
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>
2022-04-15 11:19:30 +02:00
#include "ktx.h"
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{
.filePath = std::filesystem::absolute("./textures/placeholder.png"),
.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
{
2023-02-01 22:13:04 +01:00
std::filesystem::path assetPath = args.filePath.filename();
2021-11-11 20:12:50 +01:00
assetPath.replace_extension("asset");
2023-11-05 10:36:01 +01:00
OTextureAsset asset = new TextureAsset(args.importPath, assetPath.stem().string());
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-07-31 21:43:20 +02:00
2023-01-29 18:58:59 +01:00
int totalWidth, totalHeight, n;
2023-02-01 22:13:04 +01:00
unsigned char* data = stbi_load(args.filePath.string().c_str(), &totalWidth, &totalHeight, &n, 4);
2021-08-22 23:07:38 +02:00
ktxTexture2* kTexture;
ktxTextureCreateInfo createInfo;
2023-02-13 14:56:13 +01:00
createInfo.vkFormat = VK_FORMAT_R8G8B8A8_UNORM;
createInfo.baseDepth = 1;
createInfo.numLevels = 1;
createInfo.numLayers = 1;
createInfo.isArray = false;
createInfo.generateMipmaps = false;
2021-08-22 23:07:38 +02:00
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;
uint32 faceHeight = totalHeight / 3;
// 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);
for (int y = 0; y < faceWidth; ++y)
{
for (int x = 0; x < faceWidth; ++x)
{
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-02-13 14:56:13 +01:00
std::cout << "starting compression of " << textureAsset->getAssetIdentifier() << std::endl;
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-07-31 21:43:20 +02:00
.compressionLevel = 5,
2023-02-13 14:56:13 +01:00
.qualityLevel = 0,
};
2023-07-31 21:43:20 +02:00
ktx_error_code_e error = ktxTexture2_CompressBasisEx(kTexture, &params2);
2023-02-24 22:09:07 +01:00
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;
}
auto stream = AssetRegistry::createWriteStream((std::filesystem::path(textureAsset->folderPath) / textureAsset->getName()).replace_extension("asset").string(), std::ios::binary);
ArchiveBuffer archive;
Serialization::save(archive, TextureAsset::IDENTIFIER);
Serialization::save(archive, textureAsset->getName());
Serialization::save(archive, textureAsset->getFolderPath());
Serialization::save(archive, memory);
archive.writeToStream(stream);
2022-04-15 23:45:44 +02:00
////co_return;
2020-06-02 11:46:18 +02:00
}