2020-06-02 11:46:18 +02:00
|
|
|
#include "TextureLoader.h"
|
|
|
|
|
#include "TextureAsset.h"
|
|
|
|
|
#include "Graphics/Graphics.h"
|
|
|
|
|
#include "AssetRegistry.h"
|
2021-08-22 23:07:38 +02:00
|
|
|
#include "Graphics/Vulkan/VulkanGraphicsEnums.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>
|
2020-06-02 11:46:18 +02:00
|
|
|
|
|
|
|
|
using namespace Seele;
|
|
|
|
|
|
|
|
|
|
TextureLoader::TextureLoader(Gfx::PGraphics graphics)
|
|
|
|
|
: graphics(graphics)
|
|
|
|
|
{
|
2021-08-22 23:07:38 +02:00
|
|
|
placeholderAsset = new TextureAsset(std::filesystem::absolute("./textures/placeholder.ktx"));
|
|
|
|
|
placeholderAsset->load();
|
2021-04-25 23:43:40 +02:00
|
|
|
AssetRegistry::get().textures[""] = placeholderAsset;
|
2020-06-02 11:46:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TextureLoader::~TextureLoader()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-08 01:44:47 +02:00
|
|
|
void TextureLoader::importAsset(const std::filesystem::path& filePath)
|
2020-06-02 11:46:18 +02:00
|
|
|
{
|
2020-09-19 14:36:50 +02:00
|
|
|
auto assetFileName = filePath;
|
|
|
|
|
PTextureAsset asset = new TextureAsset(assetFileName.replace_extension("asset").filename().generic_string());
|
|
|
|
|
asset->setStatus(Asset::Status::Loading);
|
2021-08-22 23:07:38 +02:00
|
|
|
asset->setTexture(placeholderAsset->getTexture());
|
2021-10-15 23:12:29 +02:00
|
|
|
AssetRegistry::get().registerTexture(asset);
|
2021-04-13 23:09:16 +02:00
|
|
|
futures.add(std::async(std::launch::async, [this, filePath, asset] () mutable {
|
2021-04-25 23:43:40 +02:00
|
|
|
using namespace std::chrono_literals;
|
2021-05-10 23:57:55 +02:00
|
|
|
//std::this_thread::sleep_for(5s);
|
2021-08-22 23:07:38 +02:00
|
|
|
import(filePath, asset);
|
|
|
|
|
asset->load();
|
2020-09-19 14:36:50 +02:00
|
|
|
asset->setStatus(Asset::Status::Ready);
|
2021-04-13 23:09:16 +02:00
|
|
|
}));
|
2020-10-03 11:00:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
PTextureAsset TextureLoader::getPlaceholderTexture()
|
|
|
|
|
{
|
|
|
|
|
return placeholderAsset;
|
2020-06-02 11:46:18 +02:00
|
|
|
}
|
|
|
|
|
|
2021-08-22 23:07:38 +02:00
|
|
|
void TextureLoader::import(const std::filesystem::path& path, PTextureAsset textureAsset)
|
2020-06-02 11:46:18 +02:00
|
|
|
{
|
|
|
|
|
int x, y, n;
|
2020-06-08 01:44:47 +02:00
|
|
|
unsigned char* data = stbi_load(path.string().c_str(), &x, &y, &n, 4);
|
2021-08-22 23:07:38 +02:00
|
|
|
ktxTexture2* kTexture;
|
|
|
|
|
ktxTextureCreateInfo createInfo;
|
|
|
|
|
|
|
|
|
|
createInfo.vkFormat = VK_FORMAT_R8G8B8A8_SRGB;
|
|
|
|
|
createInfo.baseWidth = x;
|
|
|
|
|
createInfo.baseHeight = y;
|
|
|
|
|
createInfo.baseDepth = 1;
|
|
|
|
|
createInfo.numDimensions = 1 + (y > 1);
|
|
|
|
|
createInfo.numLevels = 1;
|
|
|
|
|
createInfo.numLayers = 1;
|
|
|
|
|
createInfo.numFaces = 1;
|
|
|
|
|
createInfo.isArray = false;
|
|
|
|
|
createInfo.generateMipmaps = true;
|
|
|
|
|
|
2021-10-15 23:12:29 +02:00
|
|
|
ktxTexture2_Create(&createInfo,
|
2021-08-22 23:07:38 +02:00
|
|
|
KTX_TEXTURE_CREATE_ALLOC_STORAGE,
|
|
|
|
|
&kTexture);
|
|
|
|
|
|
2021-10-15 23:12:29 +02:00
|
|
|
ktxTexture_SetImageFromMemory(ktxTexture(kTexture),
|
2021-08-22 23:07:38 +02:00
|
|
|
0, 0, 0, data, x * y * 4 * sizeof(unsigned char));
|
|
|
|
|
|
2020-06-08 01:44:47 +02:00
|
|
|
stbi_image_free(data);
|
2021-08-22 23:07:38 +02:00
|
|
|
|
2021-10-15 23:12:29 +02:00
|
|
|
ktxTexture_WriteToNamedFile(ktxTexture(kTexture), textureAsset->getFullPath().c_str());
|
2021-08-22 23:07:38 +02:00
|
|
|
ktxTexture_Destroy(ktxTexture(kTexture));
|
2020-06-02 11:46:18 +02:00
|
|
|
}
|