Redo of render paths

This commit is contained in:
Dynamitos
2020-06-02 11:46:18 +02:00
parent bb5b48698a
commit 356e6058fe
121 changed files with 3890 additions and 572 deletions
+56
View File
@@ -3,9 +3,65 @@
using namespace Seele;
Asset::Asset()
: fullPath("")
, name("")
, parentDir("")
, extension("")
, status(Status::Uninitialized)
{
}
Asset::Asset(const std::filesystem::path& path)
: fullPath(std::filesystem::absolute(path))
, status(Status::Uninitialized)
{
fullPath.make_preferred();
parentDir = fullPath.parent_path();
name = fullPath.stem();
extension = fullPath.extension();
}
Asset::Asset(const std::string &fullPath)
: Asset(std::filesystem::path(fullPath))
{
}
Asset::Asset(const std::string &directory, const std::string &fileName)
: Asset(std::filesystem::path(directory + fileName))
{
}
Asset::~Asset()
{
}
std::ifstream &Asset::getReadStream()
{
if(inStream.is_open())
{
return inStream;
}
inStream.open(fullPath);
return inStream;
}
std::ofstream &Asset::getWriteStream()
{
if(outStream.is_open())
{
return outStream;
}
outStream.open(fullPath);
return outStream;
}
std::string Asset::getFileName()
{
return name.generic_string();
}
std::string Asset::getFullPath()
{
return fullPath.generic_string();
}
std::string Asset::getExtension()
{
return extension.generic_string();
}
+38 -1
View File
@@ -6,10 +6,47 @@ namespace Seele
class Asset
{
public:
enum class Status
{
Uninitialized,
Loading,
Ready
};
Asset();
Asset(const std::filesystem::path& path);
Asset(const std::string& directory, const std::string& name);
Asset(const std::string& fullPath);
virtual ~Asset();
std::ifstream& getReadStream();
std::ofstream& getWriteStream();
// returns the name of the file, without extension
std::string getFileName();
// returns the full absolute path, from root to extension
std::string getFullPath();
// returns the file extension, without preceding dot
std::string getExtension();
inline Status getStatus()
{
std::scoped_lock lck(lock);
return status;
}
inline void setStatus(Status status)
{
std::scoped_lock lck(lock);
this->status = status;
}
protected:
std::mutex lock;
private:
Status status;
std::filesystem::path fullPath;
std::filesystem::path parentDir;
std::filesystem::path name;
std::filesystem::path extension;
uint32 byteSize;
std::ifstream inStream;
std::ofstream outStream;
};
DEFINE_REF(Asset);
} // namespace Seele
+89
View File
@@ -0,0 +1,89 @@
#include "AssetRegistry.h"
#include "MeshAsset.h"
#include "TextureAsset.h"
#include "TextureLoader.h"
#include "MaterialLoader.h"
#include "MeshLoader.h"
#include "Material/Material.h"
#include "Graphics/Graphics.h"
#include "graphics/WindowManager.h"
using namespace Seele;
AssetRegistry::~AssetRegistry()
{
}
void AssetRegistry::init(const std::string& rootFolder)
{
get().init(rootFolder, WindowManager::getGraphics());
}
void AssetRegistry::importFile(const std::string &filePath)
{
std::string extension = filePath.substr(filePath.find_last_of(".") + 1);
std::cout << extension << std::endl;
if (extension.compare("fbx") == 0
|| extension.compare("obj") == 0)
{
get().registerMesh(filePath);
}
if (extension.compare("png") == 0
|| extension.compare("jpg") == 0)
{
get().registerTexture(filePath);
}
if (extension.compare("semat") == 0)
{
get().registerMaterial(filePath);
}
}
PMeshAsset AssetRegistry::findMesh(const std::string &filePath)
{
return get().meshes[filePath];
}
PMaterialAsset AssetRegistry::findMaterial(const std::string &filePath)
{
return get().materials[filePath];
}
PTextureAsset AssetRegistry::findTexture(const std::string &filePath)
{
return get().textures[filePath];
}
AssetRegistry &AssetRegistry::get()
{
static AssetRegistry instance;
return instance;
}
AssetRegistry::AssetRegistry()
{
}
void AssetRegistry::init(const std::string &rootFolder, Gfx::PGraphics graphics)
{
AssetRegistry &reg = get();
reg.rootFolder = rootFolder;
reg.meshLoader = new MeshLoader(graphics);
reg.textureLoader = new TextureLoader(graphics);
reg.materialLoader = new MaterialLoader(graphics);
}
void AssetRegistry::registerMesh(const std::string &filePath)
{
meshLoader->importAsset(filePath);
}
void AssetRegistry::registerTexture(const std::string &filePath)
{
textureLoader->importAsset(filePath);
}
void AssetRegistry::registerMaterial(const std::string &filePath)
{
materialLoader->queueAsset(filePath);
}
+47
View File
@@ -0,0 +1,47 @@
#pragma once
#include "MinimalEngine.h"
#include "Asset.h"
#include <string>
namespace Seele
{
DECLARE_REF(TextureLoader);
DECLARE_REF(MeshLoader);
DECLARE_REF(MaterialLoader);
DECLARE_REF(TextureAsset);
DECLARE_REF(MeshAsset);
DECLARE_REF(MaterialAsset);
DECLARE_NAME_REF(Gfx, Graphics);
class AssetRegistry
{
public:
~AssetRegistry();
static void init(const std::string& rootFolder);
static void importFile(const std::string& filePath);
static PMeshAsset findMesh(const std::string& filePath);
static PTextureAsset findTexture(const std::string& filePath);
static PMaterialAsset findMaterial(const std::string& filePath);
private:
static AssetRegistry& get();
AssetRegistry();
void init(const std::string& rootFolder, Gfx::PGraphics graphics);
void registerMesh(const std::string& filePath);
void registerTexture(const std::string& filePath);
void registerMaterial(const std::string& filePath);
std::string rootFolder;
Map<std::string, PTextureAsset> textures;
Map<std::string, PMeshAsset> meshes;
Map<std::string, PMaterialAsset> materials;
UPTextureLoader textureLoader;
UPMeshLoader meshLoader;
UPMaterialLoader materialLoader;
friend class TextureLoader;
friend class MaterialLoader;
friend class MeshLoader;
};
}
+12 -2
View File
@@ -2,5 +2,15 @@ target_sources(SeeleEngine
PRIVATE
Asset.h
Asset.cpp
FileAsset.h
FileAsset.cpp)
AssetRegistry.h
AssetRegistry.cpp
MaterialLoader.h
MaterialLoader.cpp
MeshAsset.h
MeshAsset.cpp
MeshLoader.h
MeshLoader.cpp
TextureAsset.h
TextureAsset.cpp
TextureLoader.h
TextureLoader.cpp)
-62
View File
@@ -1,62 +0,0 @@
#include "FileAsset.h"
using namespace Seele;
FileAsset::FileAsset()
: path("")
, name("")
{
}
FileAsset::FileAsset(const std::string &fullPath)
: path(fullPath)
{
name = fullPath.substr(fullPath.find_last_of('\\')+1);
}
FileAsset::FileAsset(const std::string &directory, const std::string &name)
: path(directory+name)
, name(name)
{
}
FileAsset::~FileAsset()
{
}
std::ifstream &FileAsset::getReadStream()
{
if(inStream.is_open())
{
return inStream;
}
inStream.open(path);
return inStream;
}
std::ofstream &FileAsset::getWriteStream()
{
if(outStream.is_open())
{
return outStream;
}
outStream.open(path);
return outStream;
}
void FileAsset::rename(const std::string& newName)
{
if(inStream.is_open())
{
inStream.close();
}
if(outStream.is_open())
{
outStream.flush();
outStream.close();
}
path.replace(path.find_last_of(name), name.size(), newName);
name = newName;
inStream.open(path);
outStream.open(path);
}
-24
View File
@@ -1,24 +0,0 @@
#pragma once
#include "Asset.h"
#include <fstream>
namespace Seele
{
class FileAsset
{
public:
FileAsset();
FileAsset(const std::string& directory, const std::string& name);
FileAsset(const std::string& fullPath);
virtual ~FileAsset();
std::ifstream& getReadStream();
std::ofstream& getWriteStream();
void rename(const std::string& newName);
private:
std::string name;
std::string path;
uint32 byteSize;
std::ifstream inStream;
std::ofstream outStream;
};
}
+22
View File
@@ -0,0 +1,22 @@
#include "MaterialLoader.h"
#include "Material/Material.h"
#include "Graphics/Graphics.h"
using namespace Seele;
MaterialLoader::MaterialLoader(Gfx::PGraphics graphics)
{
placeholderMaterial = new Material("shaders/Placeholder.semat");
placeholderMaterial->compile();
}
MaterialLoader::~MaterialLoader()
{
}
PMaterial MaterialLoader::queueAsset(const std::string& filePath)
{
PMaterial result = new Material(filePath);
//TODO
return result;
}
+22
View File
@@ -0,0 +1,22 @@
#pragma once
#include "MinimalEngine.h"
#include "Containers/List.h"
#include <thread>
#include <future>
namespace Seele
{
DECLARE_REF(Material)
DECLARE_NAME_REF(Gfx, Graphics);
class MaterialLoader
{
public:
MaterialLoader(Gfx::PGraphics graphic);
~MaterialLoader();
PMaterial queueAsset(const std::string& filePath);
private:
List<std::future<void>> futures;
PMaterial placeholderMaterial;
};
DEFINE_REF(MaterialLoader);
} // namespace Seele
+17
View File
@@ -0,0 +1,17 @@
#include "MeshAsset.h"
#include "Graphics/Mesh.h"
using namespace Seele;
MeshAsset::MeshAsset()
{
}
MeshAsset::MeshAsset(const std::string& directory, const std::string& name)
: Asset(directory, name)
{
}
MeshAsset::MeshAsset(const std::string& fullPath)
: Asset(fullPath)
{
}
+22
View File
@@ -0,0 +1,22 @@
#pragma once
#include "Asset.h"
namespace Seele
{
DECLARE_REF(Mesh);
class MeshAsset : public Asset
{
public:
MeshAsset();
MeshAsset(const std::string& directory, const std::string& name);
MeshAsset(const std::string& fullPath);
void setMesh(PMesh mesh)
{
std::scoped_lock lck(lock);
this->mesh = mesh;
}
private:
PMesh mesh;
};
DEFINE_REF(MeshAsset);
} // namespace Seele
+61
View File
@@ -0,0 +1,61 @@
#include "MeshLoader.h"
#include "Graphics/Graphics.h"
#include "MeshAsset.h"
#include "Graphics/Mesh.h"
#include <assimp/Importer.hpp>
#include <assimp/scene.h>
#include <assimp/postprocess.h>
using namespace Seele;
MeshLoader::MeshLoader(Gfx::PGraphics graphics)
: graphics(graphics)
{
}
MeshLoader::~MeshLoader()
{
}
void MeshLoader::importAsset(const std::string& path)
{
futures.add(std::async(std::launch::async, &MeshLoader::import, this, path));
}
void findMeshRoots(aiNode* node, List<aiNode*>& meshNodes)
{
if(node->mNumMeshes > 0)
{
meshNodes.add(node);
return;
}
for(uint32 i = 0; i < node->mNumChildren; ++i)
{
findMeshRoots(node->mChildren[i], meshNodes);
}
}
void MeshLoader::import(const std::string& path)
{
PMeshAsset asset = new MeshAsset(path);
asset->setStatus(Asset::Status::Loading);
Assimp::Importer importer;
const aiScene* scene = importer.ReadFile(path.c_str(),
aiProcess_CalcTangentSpace |
aiProcess_FlipUVs |
aiProcess_ImproveCacheLocality |
aiProcess_OptimizeMeshes |
aiProcess_GenBoundingBoxes |
aiProcessPreset_TargetRealtime_Fast);
List<aiNode*> meshNodes;
findMeshRoots(scene->mRootNode, meshNodes);
for(auto meshNode : meshNodes)
{
std::cout << "Test:" << meshNode->mNumMeshes << std::endl;
}
PMesh mesh = new Mesh(nullptr, nullptr);
asset->setMesh(mesh);
asset->setStatus(Asset::Status::Ready);
}
+23
View File
@@ -0,0 +1,23 @@
#pragma once
#include "MinimalEngine.h"
#include "Containers/List.h"
#include <thread>
#include <future>
namespace Seele
{
DECLARE_REF(MeshAsset)
DECLARE_NAME_REF(Gfx, Graphics);
class MeshLoader
{
public:
MeshLoader(Gfx::PGraphics graphic);
~MeshLoader();
void importAsset(const std::string& filePath);
private:
void import(const std::string& path);
List<std::future<void>> futures;
Gfx::PGraphics graphics;
};
DEFINE_REF(MeshLoader);
} // namespace Seele
+17
View File
@@ -0,0 +1,17 @@
#include "TextureAsset.h"
#include "Graphics/GraphicsResources.h"
using namespace Seele;
TextureAsset::TextureAsset()
{
}
TextureAsset::TextureAsset(const std::string& directory, const std::string& name)
: Asset(directory, name)
{
}
TextureAsset::TextureAsset(const std::string& fullPath)
: Asset(fullPath)
{
}
+25
View File
@@ -0,0 +1,25 @@
#include "Asset.h"
namespace Seele
{
DECLARE_NAME_REF(Gfx, Texture);
class TextureAsset : public Asset
{
public:
TextureAsset();
TextureAsset(const std::string& directory, const std::string& name);
TextureAsset(const std::string& fullPath);
void setTexture(Gfx::PTexture texture)
{
std::scoped_lock lck(lock);
this->texture = texture;
}
Gfx::PTexture getTexture()
{
return texture;
}
private:
Gfx::PTexture texture;
};
DEFINE_REF(TextureAsset);
} // namespace Seele
+61
View File
@@ -0,0 +1,61 @@
#include "TextureLoader.h"
#include "TextureAsset.h"
#include "Graphics/Graphics.h"
#include "AssetRegistry.h"
#define STB_IMAGE_IMPLEMENTATION
#include <stb_image.h>
using namespace Seele;
TextureLoader::TextureLoader(Gfx::PGraphics graphics)
: graphics(graphics)
{
import("./textures/placeholder.png");
placeholderTexture = AssetRegistry::findTexture("./textures/placeholder.png");
}
TextureLoader::~TextureLoader()
{
}
void TextureLoader::importAsset(const std::string& filePath)
{
futures.add(std::async(std::launch::async, &TextureLoader::import, this, filePath));
}
void TextureLoader::import(const std::string& path)
{
PTextureAsset asset = new TextureAsset(path);
AssetRegistry::get().textures[path] = asset;
asset->setStatus(Asset::Status::Loading);
int x, y, n;
const std::string fullPath = std::string(asset->getFullPath());
unsigned char* data = stbi_load(fullPath.c_str(), &x, &y, &n, 0);
TextureCreateInfo createInfo;
createInfo.resourceData.data = data;
createInfo.resourceData.size = x * y * n * sizeof(unsigned char);
createInfo.width = x;
createInfo.height = y;
switch (n)
{
case 1:
createInfo.format = Gfx::SE_FORMAT_R8_UINT;
break;
case 2:
createInfo.format = Gfx::SE_FORMAT_R8G8_UINT;
break;
case 3:
createInfo.format = Gfx::SE_FORMAT_R8G8B8_UINT;
break;
case 4:
createInfo.format = Gfx::SE_FORMAT_R8G8B8A8_UINT;
break;
default:
break;
}
createInfo.resourceData.owner = Gfx::QueueType::DEDICATED_TRANSFER;
Gfx::PTexture2D texture = graphics->createTexture2D(createInfo);
texture->transferOwnership(Gfx::QueueType::GRAPHICS);
asset->setTexture(texture);
asset->setStatus(Asset::Status::Ready);
}
+24
View File
@@ -0,0 +1,24 @@
#pragma once
#include "MinimalEngine.h"
#include "Containers/List.h"
#include <thread>
#include <future>
namespace Seele
{
DECLARE_REF(TextureAsset);
DECLARE_NAME_REF(Gfx, Graphics);
class TextureLoader
{
public:
TextureLoader(Gfx::PGraphics graphic);
~TextureLoader();
void importAsset(const std::string& filePath);
private:
void import(const std::string& path);
Gfx::PGraphics graphics;
List<std::future<void>> futures;
PTextureAsset placeholderTexture;
};
DEFINE_REF(TextureLoader);
} // namespace Seele