coroutines truly are cursed

This commit is contained in:
2021-11-11 20:12:50 +01:00
parent 1848bb5139
commit 01049019fd
25 changed files with 202 additions and 92 deletions
+1 -1
View File
@@ -104,7 +104,7 @@ void AssetRegistry::importTexture(const std::filesystem::path &filePath)
void AssetRegistry::importMaterial(const std::filesystem::path &filePath)
{
materialLoader->queueAsset(filePath);
materialLoader->importAsset(filePath);
}
void AssetRegistry::registerMesh(PMeshAsset mesh)
+15 -8
View File
@@ -17,15 +17,22 @@ MaterialLoader::~MaterialLoader()
{
}
PMaterialAsset MaterialLoader::queueAsset(const std::filesystem::path& filePath)
void MaterialLoader::importAsset(const std::filesystem::path& name)
{
PMaterialAsset result = new MaterialAsset(filePath);
result->load();
graphics->getShaderCompiler()->registerMaterial(result);
AssetRegistry::get().registerMaterial(result);
// TODO: There is actually no real reason to import a standalone material,
// maybe in the future there could be a substance loader or something
return result;
std::filesystem::path assetPath = name.filename();
assetPath.replace_extension("asset");
PMaterialAsset asset = new MaterialAsset(assetPath.generic_string());
asset->setStatus(Asset::Status::Loading);
AssetRegistry::get().registerMaterial(asset);
import(name, asset);
}
Job MaterialLoader::import(std::filesystem::path, PMaterialAsset asset)
{
asset->load();
graphics->getShaderCompiler()->registerMaterial(asset);
AssetRegistry::get().registerMaterial(asset);
co_return;
}
PMaterialAsset MaterialLoader::getPlaceHolderMaterial()
+3 -1
View File
@@ -1,6 +1,7 @@
#pragma once
#include "MinimalEngine.h"
#include "Containers/List.h"
#include "ThreadPool.h"
#include <thread>
#include <future>
#include <filesystem>
@@ -14,9 +15,10 @@ class MaterialLoader
public:
MaterialLoader(Gfx::PGraphics graphic);
~MaterialLoader();
PMaterialAsset queueAsset(const std::filesystem::path& filePath);
void importAsset(const std::filesystem::path& name);
PMaterialAsset getPlaceHolderMaterial();
private:
Job import(std::filesystem::path filePath, PMaterialAsset asset);
Gfx::PGraphics graphics;
List<std::future<void>> futures;
PMaterialAsset placeholderMaterial;
+6 -4
View File
@@ -30,9 +30,10 @@ void MeshLoader::importAsset(const std::filesystem::path &path)
{
std::filesystem::path assetPath = path.filename();
assetPath.replace_extension("asset");
PMeshAsset meshAsset = new MeshAsset(assetPath.generic_string());
AssetRegistry::get().registerMesh(meshAsset);
futures.add(std::async(std::launch::async, &MeshLoader::import, this, meshAsset, path));
PMeshAsset asset = new MeshAsset(assetPath.generic_string());
asset->setStatus(Asset::Status::Loading);
AssetRegistry::get().registerMesh(asset);
import(path, asset);
}
void MeshLoader::loadMaterials(const aiScene* scene, Array<PMaterialAsset>& globalMaterials, Gfx::PGraphics graphics)
@@ -228,7 +229,7 @@ void MeshLoader::loadTextures(const aiScene* scene, const std::filesystem::path&
AssetRegistry::importFile(texPngPath.string());
}
}
void MeshLoader::import(PMeshAsset meshAsset, const std::filesystem::path &path)
Job MeshLoader::import(std::filesystem::path path, PMeshAsset meshAsset)
{
std::cout << "Starting to import "<<path << std::endl;
meshAsset->setStatus(Asset::Status::Loading);
@@ -263,4 +264,5 @@ void MeshLoader::import(PMeshAsset meshAsset, const std::filesystem::path &path)
meshAsset->setStatus(Asset::Status::Ready);
meshAsset->save();
std::cout << "Finished loading " << path << std::endl;
co_return;
}
+2 -1
View File
@@ -1,6 +1,7 @@
#pragma once
#include "MinimalEngine.h"
#include "Containers/List.h"
#include "ThreadPool.h"
#include <thread>
#include <future>
#include <filesystem>
@@ -25,7 +26,7 @@ private:
void loadGlobalMeshes(const aiScene* scene, Array<PMesh>& globalMeshes, const Array<PMaterialAsset>& materials, Gfx::PGraphics graphics);
void convertAssimpARGB(unsigned char* dst, aiTexel* src, uint32 numPixels);
void import(PMeshAsset meshAsset, const std::filesystem::path& path);
Job import(std::filesystem::path path, PMeshAsset meshAsset);
List<std::future<void>> futures;
Gfx::PGraphics graphics;
};
+10 -11
View File
@@ -22,20 +22,15 @@ TextureLoader::~TextureLoader()
{
}
void TextureLoader::importAsset(const std::filesystem::path& filePath)
void TextureLoader::importAsset(const std::filesystem::path& path)
{
auto assetFileName = filePath;
PTextureAsset asset = new TextureAsset(assetFileName.replace_extension("asset").filename().generic_string());
std::filesystem::path assetPath = path.filename();
assetPath.replace_extension("asset");
PTextureAsset asset = new TextureAsset(assetPath.generic_string());
asset->setStatus(Asset::Status::Loading);
asset->setTexture(placeholderAsset->getTexture());
AssetRegistry::get().registerTexture(asset);
futures.add(std::async(std::launch::async, [this, filePath, asset] () mutable {
using namespace std::chrono_literals;
//std::this_thread::sleep_for(5s);
import(filePath, asset);
asset->load();
asset->setStatus(Asset::Status::Ready);
}));
import(path, asset);
}
PTextureAsset TextureLoader::getPlaceholderTexture()
@@ -43,7 +38,7 @@ PTextureAsset TextureLoader::getPlaceholderTexture()
return placeholderAsset;
}
void TextureLoader::import(const std::filesystem::path& path, PTextureAsset textureAsset)
Job TextureLoader::import(std::filesystem::path path, PTextureAsset textureAsset)
{
int x, y, n;
unsigned char* data = stbi_load(path.string().c_str(), &x, &y, &n, 4);
@@ -72,4 +67,8 @@ void TextureLoader::import(const std::filesystem::path& path, PTextureAsset text
ktxTexture_WriteToNamedFile(ktxTexture(kTexture), textureAsset->getFullPath().c_str());
ktxTexture_Destroy(ktxTexture(kTexture));
textureAsset->load();
textureAsset->setStatus(Asset::Status::Ready);
co_return;
}
+2 -1
View File
@@ -1,6 +1,7 @@
#pragma once
#include "MinimalEngine.h"
#include "Containers/List.h"
#include "ThreadPool.h"
#include <thread>
#include <future>
#include <filesystem>
@@ -18,7 +19,7 @@ public:
void importAsset(const std::filesystem::path& filePath);
PTextureAsset getPlaceholderTexture();
private:
void import(const std::filesystem::path& path, PTextureAsset asset);
Job import(std::filesystem::path path, PTextureAsset asset);
Gfx::PGraphics graphics;
List<std::future<void>> futures;
PTextureAsset placeholderAsset;