2020-06-02 11:46:18 +02:00
|
|
|
#include "TextureLoader.h"
|
|
|
|
|
#include "TextureAsset.h"
|
|
|
|
|
#include "Graphics/Graphics.h"
|
|
|
|
|
#include "AssetRegistry.h"
|
|
|
|
|
#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)
|
|
|
|
|
{
|
|
|
|
|
import("./textures/placeholder.png");
|
|
|
|
|
placeholderTexture = AssetRegistry::findTexture("./textures/placeholder.png");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
{
|
|
|
|
|
futures.add(std::async(std::launch::async, &TextureLoader::import, this, filePath));
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-08 01:44:47 +02:00
|
|
|
void TextureLoader::import(const std::filesystem::path& path)
|
2020-06-02 11:46:18 +02:00
|
|
|
{
|
2020-06-08 01:44:47 +02:00
|
|
|
std::filesystem::path assetPath = path.stem().append(".asset");
|
|
|
|
|
PTextureAsset asset = new TextureAsset(assetPath);
|
|
|
|
|
asset->setStatus(Asset::Status::Loading);
|
2020-06-02 11:46:18 +02:00
|
|
|
int x, y, n;
|
|
|
|
|
const std::string fullPath = std::string(asset->getFullPath());
|
2020-06-08 01:44:47 +02:00
|
|
|
unsigned char* data = stbi_load(path.string().c_str(), &x, &y, &n, 4);
|
2020-06-02 11:46:18 +02:00
|
|
|
TextureCreateInfo createInfo;
|
2020-06-08 01:44:47 +02:00
|
|
|
createInfo.format = Gfx::SE_FORMAT_R8G8B8A8_UINT;
|
2020-06-02 11:46:18 +02:00
|
|
|
createInfo.resourceData.data = data;
|
2020-06-08 01:44:47 +02:00
|
|
|
createInfo.resourceData.size = x * y * 4 * sizeof(unsigned char);
|
|
|
|
|
createInfo.resourceData.owner = Gfx::QueueType::DEDICATED_TRANSFER;
|
2020-06-02 11:46:18 +02:00
|
|
|
createInfo.width = x;
|
|
|
|
|
createInfo.height = y;
|
|
|
|
|
Gfx::PTexture2D texture = graphics->createTexture2D(createInfo);
|
2020-06-08 01:44:47 +02:00
|
|
|
stbi_image_free(data);
|
2020-06-02 11:46:18 +02:00
|
|
|
texture->transferOwnership(Gfx::QueueType::GRAPHICS);
|
|
|
|
|
asset->setTexture(texture);
|
|
|
|
|
asset->setStatus(Asset::Status::Ready);
|
2020-06-08 01:44:47 +02:00
|
|
|
AssetRegistry::get().textures[assetPath.string()] = asset;
|
2020-06-02 11:46:18 +02:00
|
|
|
}
|