Loading logic moved to Asset classes

This commit is contained in:
Dynamitos
2020-06-08 01:44:47 +02:00
parent 356e6058fe
commit ab4a3b5e23
50 changed files with 966 additions and 558 deletions
-4
View File
@@ -19,10 +19,6 @@ Asset::Asset(const std::filesystem::path& 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))
+6 -4
View File
@@ -13,12 +13,12 @@ public:
Ready
};
Asset();
Asset(const std::filesystem::path& path);
Asset(const std::string& directory, const std::string& name);
Asset(const std::string& fullPath);
Asset(const std::filesystem::path& path);
virtual ~Asset();
std::ifstream& getReadStream();
std::ofstream& getWriteStream();
virtual void save() = 0;
virtual void load() = 0;
// returns the name of the file, without extension
std::string getFileName();
@@ -38,6 +38,8 @@ public:
}
protected:
std::mutex lock;
std::ifstream& getReadStream();
std::ofstream& getWriteStream();
private:
Status status;
std::filesystem::path fullPath;
+15 -13
View File
@@ -7,6 +7,7 @@
#include "Material/Material.h"
#include "Graphics/Graphics.h"
#include "graphics/WindowManager.h"
#include <filesystem>
using namespace Seele;
@@ -21,21 +22,22 @@ void AssetRegistry::init(const std::string& rootFolder)
void AssetRegistry::importFile(const std::string &filePath)
{
std::string extension = filePath.substr(filePath.find_last_of(".") + 1);
std::filesystem::path fsPath = std::filesystem::path(filePath);
std::string extension = fsPath.extension().string();
std::cout << extension << std::endl;
if (extension.compare("fbx") == 0
|| extension.compare("obj") == 0)
if (extension.compare(".fbx") == 0
|| extension.compare(".obj") == 0)
{
get().registerMesh(filePath);
get().registerMesh(fsPath);
}
if (extension.compare("png") == 0
|| extension.compare("jpg") == 0)
if (extension.compare(".png") == 0
|| extension.compare(".jpg") == 0)
{
get().registerTexture(filePath);
get().registerTexture(fsPath);
}
if (extension.compare("semat") == 0)
if (extension.compare(".semat") == 0)
{
get().registerMaterial(filePath);
get().registerMaterial(fsPath);
}
}
@@ -64,7 +66,7 @@ AssetRegistry::AssetRegistry()
{
}
void AssetRegistry::init(const std::string &rootFolder, Gfx::PGraphics graphics)
void AssetRegistry::init(const std::filesystem::path &rootFolder, Gfx::PGraphics graphics)
{
AssetRegistry &reg = get();
reg.rootFolder = rootFolder;
@@ -73,17 +75,17 @@ void AssetRegistry::init(const std::string &rootFolder, Gfx::PGraphics graphics)
reg.materialLoader = new MaterialLoader(graphics);
}
void AssetRegistry::registerMesh(const std::string &filePath)
void AssetRegistry::registerMesh(const std::filesystem::path &filePath)
{
meshLoader->importAsset(filePath);
}
void AssetRegistry::registerTexture(const std::string &filePath)
void AssetRegistry::registerTexture(const std::filesystem::path &filePath)
{
textureLoader->importAsset(filePath);
}
void AssetRegistry::registerMaterial(const std::string &filePath)
void AssetRegistry::registerMaterial(const std::filesystem::path &filePath)
{
materialLoader->queueAsset(filePath);
}
+5 -5
View File
@@ -27,13 +27,13 @@ private:
static AssetRegistry& get();
AssetRegistry();
void init(const std::string& rootFolder, Gfx::PGraphics graphics);
void init(const std::filesystem::path& rootFolder, Gfx::PGraphics graphics);
void registerMesh(const std::string& filePath);
void registerTexture(const std::string& filePath);
void registerMaterial(const std::string& filePath);
void registerMesh(const std::filesystem::path& filePath);
void registerTexture(const std::filesystem::path& filePath);
void registerMaterial(const std::filesystem::path& filePath);
std::string rootFolder;
std::filesystem::path rootFolder;
Map<std::string, PTextureAsset> textures;
Map<std::string, PMeshAsset> meshes;
Map<std::string, PMaterialAsset> materials;
+3 -2
View File
@@ -14,9 +14,10 @@ MaterialLoader::~MaterialLoader()
{
}
PMaterial MaterialLoader::queueAsset(const std::string& filePath)
PMaterial MaterialLoader::queueAsset(const std::filesystem::path& filePath)
{
PMaterial result = new Material(filePath);
//TODO
// 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;
}
+1 -1
View File
@@ -13,7 +13,7 @@ class MaterialLoader
public:
MaterialLoader(Gfx::PGraphics graphic);
~MaterialLoader();
PMaterial queueAsset(const std::string& filePath);
PMaterial queueAsset(const std::filesystem::path& filePath);
private:
List<std::future<void>> futures;
PMaterial placeholderMaterial;
+14 -1
View File
@@ -7,11 +7,24 @@ using namespace Seele;
MeshAsset::MeshAsset()
{
}
MeshAsset::MeshAsset(const std::string& directory, const std::string& name)
: Asset(directory, name)
{
}
MeshAsset::MeshAsset(const std::string& fullPath)
MeshAsset::MeshAsset(const std::filesystem::path& fullPath)
: Asset(fullPath)
{
}
MeshAsset::~MeshAsset()
{
}
void MeshAsset::save()
{
//TODO:
}
void MeshAsset::load()
{
//TODO:
}
+13 -4
View File
@@ -4,19 +4,28 @@
namespace Seele
{
DECLARE_REF(Mesh);
DECLARE_REF(MaterialAsset);
class MeshAsset : public Asset
{
public:
MeshAsset();
MeshAsset(const std::string& directory, const std::string& name);
MeshAsset(const std::string& fullPath);
void setMesh(PMesh mesh)
MeshAsset(const std::filesystem::path& fullPath);
virtual ~MeshAsset();
virtual void save() override;
virtual void load() override;
void addMesh(PMesh mesh)
{
std::scoped_lock lck(lock);
this->mesh = mesh;
meshes.add(mesh);
}
const Array<PMesh> getMeshes() const
{
return meshes;
}
private:
PMesh mesh;
Array<PMesh> meshes;
Array<PMaterialAsset> referencedMaterials;
};
DEFINE_REF(MeshAsset);
} // namespace Seele
+222 -20
View File
@@ -1,10 +1,18 @@
#include "MeshLoader.h"
#include "Graphics/Graphics.h"
#include "Graphics/GraphicsResources.h"
#include "MeshAsset.h"
#include "Graphics/Mesh.h"
#include "AssetRegistry.h"
#include <filesystem>
#include <fstream>
#include <nlohmann/json.hpp>
#include <stb_image_write.h>
#include <assimp/config.h>
#include <assimp/Importer.hpp>
#include <assimp/scene.h>
#include <assimp/postprocess.h>
#include <assimp/material.h>
using namespace Seele;
@@ -17,45 +25,239 @@ MeshLoader::~MeshLoader()
{
}
void MeshLoader::importAsset(const std::string& path)
void MeshLoader::importAsset(const std::filesystem::path &path)
{
futures.add(std::async(std::launch::async, &MeshLoader::import, this, path));
}
void findMeshRoots(aiNode* node, List<aiNode*>& meshNodes)
void findMeshRoots(aiNode *node, List<aiNode *> &meshNodes)
{
if(node->mNumMeshes > 0)
if (node->mNumMeshes > 0)
{
meshNodes.add(node);
return;
}
for(uint32 i = 0; i < node->mNumChildren; ++i)
for (uint32 i = 0; i < node->mNumChildren; ++i)
{
findMeshRoots(node->mChildren[i], meshNodes);
}
}
void loadToBuffer(Array<Vector>& buffer, const aiVector3D* sourceData, uint32 size)
{
buffer.resize(size);
for(uint32 i = 0; i < size; ++i)
{
buffer[i] = Vector(sourceData[i].x, sourceData[i].y, sourceData[i].z);
}
}
Gfx::VertexStream createVertexStream(uint32 size, aiVector3D* sourceData, Gfx::PGraphics graphics)
{
Array<Vector> buffer(size);
for(uint32 i = 0; i < size; ++i)
{
buffer[i] = Vector(sourceData[i].x, sourceData[i].y, sourceData[i].z);
}
VertexBufferCreateInfo vbInfo;
vbInfo.numVertices = size;
vbInfo.vertexSize = sizeof(Vector);
vbInfo.resourceData.data = (uint8 *)buffer.data();
vbInfo.resourceData.owner = Gfx::QueueType::DEDICATED_TRANSFER;
vbInfo.resourceData.size = buffer.size();
Gfx::PVertexBuffer vertexBuffer = graphics->createVertexBuffer(vbInfo);
auto stream = Gfx::VertexStream(vbInfo.vertexSize, 0, false, vertexBuffer);
stream.addVertexElement(Gfx::VertexElement(0, Gfx::SE_FORMAT_R32G32_SFLOAT, 0));
return stream;
}
Gfx::VertexStream createVertexStream(uint32 size, aiVector2D* sourceData, Gfx::PGraphics graphics)
{
Array<Vector2> buffer(size);
for(uint32 i = 0; i < size; ++i)
{
buffer[i] = Vector2(sourceData[i].x, sourceData[i].y);
}
VertexBufferCreateInfo vbInfo;
vbInfo.numVertices = size;
vbInfo.vertexSize = sizeof(Vector2);
vbInfo.resourceData.data = (uint8 *)buffer.data();
vbInfo.resourceData.owner = Gfx::QueueType::DEDICATED_TRANSFER;
vbInfo.resourceData.size = buffer.size();
Gfx::PVertexBuffer vertexBuffer = graphics->createVertexBuffer(vbInfo);
auto stream = Gfx::VertexStream(vbInfo.vertexSize, 0, false, vertexBuffer);
stream.addVertexElement(Gfx::VertexElement(0, Gfx::SE_FORMAT_R32G32B32_SFLOAT, 0));
return stream;
}
void loadGlobalMeshes(const aiScene* scene, Array<PMesh>& globalMeshes, Gfx::PGraphics graphics)
{
for (uint32 meshIndex = 0; meshIndex < scene->mNumMeshes; ++meshIndex)
{
aiMesh *mesh = scene->mMeshes[meshIndex];
void MeshLoader::import(const std::string& path)
MeshDescription description;
Gfx::PVertexDeclaration declaration = new Gfx::VertexDeclaration();
declaration->addVertexStream(createVertexStream(mesh->mNumVertices, mesh->mVertices, graphics));
description.layout.add(VertexAttribute::POSITION);
for(uint32 i = 0; i < MAX_TEX_CHANNELS; ++i)
{
if(mesh->HasTextureCoords(i))
{
declaration->addVertexStream(createVertexStream(mesh->mNumVertices, mesh->mTextureCoords[i], graphics));
description.layout.add(VertexAttribute::TEXCOORD);
}
}
if(mesh->HasNormals())
{
declaration->addVertexStream(createVertexStream(mesh->mNumVertices, mesh->mNormals, graphics));
description.layout.add(VertexAttribute::NORMAL);
}
if(mesh->HasTangentsAndBitangents())
{
declaration->addVertexStream(createVertexStream(mesh->mNumVertices, mesh->mVertices, graphics));
declaration->addVertexStream(createVertexStream(mesh->mNumVertices, mesh->mVertices, graphics));
description.layout.add(VertexAttribute::TANGENT);
description.layout.add(VertexAttribute::BITANGENT);
}
description.declaration = declaration;
Array<uint32> indices(mesh->mNumFaces * 3);
for (uint32 faceIndex = 0; faceIndex < mesh->mNumFaces; ++faceIndex)
{
indices[faceIndex * 3 + 0] = mesh->mFaces[faceIndex].mIndices[0];
indices[faceIndex * 3 + 1] = mesh->mFaces[faceIndex].mIndices[1];
indices[faceIndex * 3 + 2] = mesh->mFaces[faceIndex].mIndices[2];
}
IndexBufferCreateInfo idxInfo;
idxInfo.indexType = Gfx::SE_INDEX_TYPE_UINT32;
idxInfo.resourceData.data = (uint8 *)indices.data();
idxInfo.resourceData.owner = Gfx::QueueType::DEDICATED_TRANSFER;
idxInfo.resourceData.size = indices.size();
Gfx::PIndexBuffer indexBuffer = graphics->createIndexBuffer(idxInfo);
indexBuffer->transferOwnership(Gfx::QueueType::GRAPHICS);
globalMeshes[meshIndex] = new Mesh(description, indexBuffer);
}
}
void convertAssimpARGB(unsigned char* dst, aiTexel* src, uint32 numPixels)
{
for(uint32 i = 0; i < numPixels; ++i)
{
dst[i * 4 + 0] = src[i].r;
dst[i * 4 + 1] = src[i].g;
dst[i * 4 + 2] = src[i].b;
dst[i * 4 + 3] = src[i].a;
}
}
void loadTextures(const aiScene* scene, Gfx::PGraphics graphics, const std::filesystem::path& meshPath)
{
for (uint32 i = 0; i < scene->mNumTextures; ++i)
{
aiTexture* tex = scene->mTextures[i];
auto texPath = std::filesystem::path(tex->mFilename.C_Str());
auto texPngPath = meshPath.parent_path().append(texPath.filename().string());
if(tex->mHeight == 0)
{
// already compressed, just dump it to the disk
std::ofstream file(texPngPath, std::ios::binary);
file.write((const char*)tex->pcData, tex->mWidth);
file.flush();
}
else
{
// recompress data so that the TextureLoader can read it
unsigned char* texData = new unsigned char[tex->mWidth * tex->mHeight * 4];
convertAssimpARGB(texData, tex->pcData, tex->mWidth * tex->mHeight);
stbi_write_png(texPngPath.string().c_str(), tex->mWidth, tex->mHeight, 4, tex->pcData, tex->mWidth * 32);
delete texData;
}
AssetRegistry::importFile(texPngPath.string());
}
}
void loadMaterials(const aiScene* scene, Gfx::PGraphics graphics)
{
using json = nlohmann::json;
for(uint32 i = 0; i < scene->mNumMaterials; ++i)
{
aiMaterial* material = scene->mMaterials[i];
json matCode;
matCode["name"] = material->GetName().C_Str();
matCode["profile"] = "BlinnPhong"; //TODO: other shading models
std::vector<std::string> code;
aiString texPath;
//TODO make samplers based on used textures
matCode["params"]["texSampler"] =
{
{"type", "SamplerState"}
};
if(material->GetTexture(aiTextureType_DIFFUSE, 0, &texPath) == AI_SUCCESS)
{
std::string texFilename = std::filesystem::path(texPath.C_Str()).stem().string();
matCode["params"]["diffuseTexture"] =
{
{"type", "Texture2D"},
{"default", texFilename}
};
code.push_back("result.baseColor = diffuseTexture.Sample(textureSampler, geometry.texCoord).xyz;\n");
}
if(material->GetTexture(aiTextureType_SPECULAR, 0, &texPath) == AI_SUCCESS)
{
std::string texFilename = std::filesystem::path(texPath.C_Str()).stem().string();
matCode["params"]["specularTexture"] =
{
{"type", "Texture2D"},
{"default", texFilename}
};
code.push_back("result.specular = specularTexture.Sample(textureSampler, geometry.texCoord).xyz;\n");
}
if(material->GetTexture(aiTextureType_NORMALS, 0, &texPath) == AI_SUCCESS)
{
std::string texFilename = std::filesystem::path(texPath.C_Str()).stem().string();
matCode["params"]["normalTexture"] =
{
{"type", "Texture2D"},
{"default", texFilename}
};
code.push_back("float3 bumpMapNormal = normalTexture.Sample(textureSampler, geometry.texCoord).xyz;\n");
code.push_back("bumpMapNormal = 2.0 * bumpMapNormal - float3(1.0f, 1.0f, 1.0f);\n");
code.push_back("result.normal = geometry.transformLocalToWorld(bumpMapNormal);\n");
}
code.push_back("return result;\n");
matCode["code"] = code;
std::ofstream outMatFile(material->GetName().C_Str());
outMatFile << std::setw(4) << matCode;
outMatFile.flush();
}
}
void MeshLoader::import(const std::filesystem::path &path)
{
PMeshAsset asset = new MeshAsset(path);
asset->setStatus(Asset::Status::Loading);
Assimp::Importer importer;
const aiScene* scene = importer.ReadFile(path.c_str(),
aiProcess_CalcTangentSpace |
importer.ReadFile(path.string().c_str(),
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;
}
aiProcess_Triangulate |
aiProcess_SortByPType |
aiProcess_GenSmoothNormals |
aiProcess_GenUVCoords |
aiProcess_FindDegenerates |
aiProcess_EmbedTextures);
const aiScene *scene = importer.ApplyPostProcessing(aiProcess_CalcTangentSpace);
Array<PMesh> globalMeshes(scene->mNumMeshes);
loadGlobalMeshes(scene, globalMeshes, graphics);
loadTextures(scene, graphics, path);
loadMaterials(scene, graphics);
PMesh mesh = new Mesh(nullptr, nullptr);
asset->setMesh(mesh);
asset->setStatus(Asset::Status::Ready);
List<aiNode *> meshNodes;
findMeshRoots(scene->mRootNode, meshNodes);
for (auto meshNode : meshNodes)
{
std::string fileName = std::string(meshNode->mName.C_Str()).append(".asset");
PMeshAsset meshAsset = new MeshAsset(fileName);
for(uint32 i = 0; i < meshNode->mNumMeshes; ++i)
{
meshAsset->addMesh(globalMeshes[meshNode->mMeshes[i]]);
}
AssetRegistry::get().meshes[meshAsset->getFileName()] = meshAsset;
}
}
+2 -2
View File
@@ -13,9 +13,9 @@ class MeshLoader
public:
MeshLoader(Gfx::PGraphics graphic);
~MeshLoader();
void importAsset(const std::string& filePath);
void importAsset(const std::filesystem::path& filePath);
private:
void import(const std::string& path);
void import(const std::filesystem::path& path);
List<std::future<void>> futures;
Gfx::PGraphics graphics;
};
+36 -1
View File
@@ -1,5 +1,9 @@
#include "TextureAsset.h"
#include "Graphics/GraphicsResources.h"
#include "Graphics/Graphics.h"
#include "Graphics/WindowManager.h"
#include <stb_image.h>
#include <stb_image_write.h>
using namespace Seele;
@@ -11,7 +15,38 @@ TextureAsset::TextureAsset(const std::string& directory, const std::string& name
: Asset(directory, name)
{
}
TextureAsset::TextureAsset(const std::string& fullPath)
TextureAsset::TextureAsset(const std::filesystem::path& fullPath)
: Asset(fullPath)
{
}
TextureAsset::~TextureAsset()
{
}
void TextureAsset::save()
{
//TODO: make this an actual file, not just a png wrapper
}
void TextureAsset::load()
{
setStatus(Status::Loading);
int x, y, n;
unsigned char* data = stbi_load(getFullPath().c_str(), &x, &y, &n, 4);
TextureCreateInfo createInfo;
//TODO: support other formats
createInfo.format = Gfx::SE_FORMAT_R8G8B8A8_UINT;
createInfo.resourceData.data = data;
createInfo.resourceData.size = x * y * 4 * sizeof(unsigned char);
createInfo.resourceData.owner = Gfx::QueueType::DEDICATED_TRANSFER;
createInfo.width = x;
createInfo.height = y;
Gfx::PTexture2D texture = WindowManager::getGraphics()->createTexture2D(createInfo);
stbi_image_free(data);
texture->transferOwnership(Gfx::QueueType::GRAPHICS);
setTexture(texture);
setStatus(Asset::Status::Ready);
}
+4 -1
View File
@@ -8,7 +8,10 @@ class TextureAsset : public Asset
public:
TextureAsset();
TextureAsset(const std::string& directory, const std::string& name);
TextureAsset(const std::string& fullPath);
TextureAsset(const std::filesystem::path& fullPath);
virtual ~TextureAsset();
virtual void save() override;
virtual void load() override;
void setTexture(Gfx::PTexture texture)
{
std::scoped_lock lck(lock);
+13 -25
View File
@@ -4,6 +4,8 @@
#include "AssetRegistry.h"
#define STB_IMAGE_IMPLEMENTATION
#include <stb_image.h>
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include <stb_image_write.h>
using namespace Seele;
@@ -18,44 +20,30 @@ TextureLoader::~TextureLoader()
{
}
void TextureLoader::importAsset(const std::string& filePath)
void TextureLoader::importAsset(const std::filesystem::path& filePath)
{
futures.add(std::async(std::launch::async, &TextureLoader::import, this, filePath));
}
void TextureLoader::import(const std::string& path)
void TextureLoader::import(const std::filesystem::path& path)
{
PTextureAsset asset = new TextureAsset(path);
AssetRegistry::get().textures[path] = asset;
asset->setStatus(Asset::Status::Loading);
std::filesystem::path assetPath = path.stem().append(".asset");
PTextureAsset asset = new TextureAsset(assetPath);
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);
unsigned char* data = stbi_load(path.string().c_str(), &x, &y, &n, 4);
TextureCreateInfo createInfo;
createInfo.format = Gfx::SE_FORMAT_R8G8B8A8_UINT;
createInfo.resourceData.data = data;
createInfo.resourceData.size = x * y * n * sizeof(unsigned char);
createInfo.resourceData.size = x * y * 4 * sizeof(unsigned char);
createInfo.resourceData.owner = Gfx::QueueType::DEDICATED_TRANSFER;
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);
stbi_image_free(data);
texture->transferOwnership(Gfx::QueueType::GRAPHICS);
asset->setTexture(texture);
asset->setStatus(Asset::Status::Ready);
AssetRegistry::get().textures[assetPath.string()] = asset;
}
+2 -2
View File
@@ -13,9 +13,9 @@ class TextureLoader
public:
TextureLoader(Gfx::PGraphics graphic);
~TextureLoader();
void importAsset(const std::string& filePath);
void importAsset(const std::filesystem::path& filePath);
private:
void import(const std::string& path);
void import(const std::filesystem::path& path);
Gfx::PGraphics graphics;
List<std::future<void>> futures;
PTextureAsset placeholderTexture;