Improving Material shader code generation

This commit is contained in:
Dynamitos
2020-09-19 14:36:50 +02:00
parent 6814587b54
commit facbfed79c
72 changed files with 1049 additions and 329 deletions
+15 -5
View File
@@ -1,4 +1,5 @@
#include "Asset.h"
#include "AssetRegistry.h"
using namespace Seele;
@@ -11,9 +12,18 @@ Asset::Asset()
{
}
Asset::Asset(const std::filesystem::path& path)
: fullPath(std::filesystem::absolute(path))
, status(Status::Uninitialized)
: status(Status::Uninitialized)
{
if(path.is_absolute())
{
fullPath = path;
}
else
{
fullPath = AssetRegistry::getRootFolder();
fullPath.append(path.generic_string());
}
fullPath.make_preferred();
parentDir = fullPath.parent_path();
name = fullPath.stem();
@@ -49,15 +59,15 @@ std::ofstream &Asset::getWriteStream()
return outStream;
}
std::string Asset::getFileName()
std::string Asset::getFileName() const
{
return name.generic_string();
}
std::string Asset::getFullPath()
std::string Asset::getFullPath() const
{
return fullPath.generic_string();
}
std::string Asset::getExtension()
std::string Asset::getExtension() const
{
return extension.generic_string();
}
+4 -3
View File
@@ -23,11 +23,11 @@ public:
virtual void load() = 0;
// returns the name of the file, without extension
std::string getFileName();
std::string getFileName() const;
// returns the full absolute path, from root to extension
std::string getFullPath();
std::string getFullPath() const;
// returns the file extension, without preceding dot
std::string getExtension();
std::string getExtension() const;
inline Status getStatus()
{
std::scoped_lock lck(lock);
@@ -44,6 +44,7 @@ protected:
std::ofstream& getWriteStream();
private:
Status status;
// Path relative to the project root
std::filesystem::path fullPath;
std::filesystem::path parentDir;
std::filesystem::path name;
+49 -7
View File
@@ -28,16 +28,16 @@ void AssetRegistry::importFile(const std::string &filePath)
if (extension.compare(".fbx") == 0
|| extension.compare(".obj") == 0)
{
get().registerMesh(fsPath);
get().importMesh(fsPath);
}
if (extension.compare(".png") == 0
|| extension.compare(".jpg") == 0)
{
get().registerTexture(fsPath);
get().importTexture(fsPath);
}
if (extension.compare(".semat") == 0)
{
get().registerMaterial(fsPath);
get().importMaterial(fsPath);
}
}
@@ -51,6 +51,16 @@ PMaterialAsset AssetRegistry::findMaterial(const std::string &filePath)
return get().materials[filePath];
}
std::ofstream AssetRegistry::createWriteStream(const std::string& relativePath, std::ios_base::openmode openmode)
{
return get().internalCreateWriteStream(relativePath, openmode);
}
std::ifstream AssetRegistry::createReadStream(const std::string& relativePath, std::ios_base::openmode openmode)
{
return get().internalCreateReadStream(relativePath, openmode);
}
PTextureAsset AssetRegistry::findTexture(const std::string &filePath)
{
return get().textures[filePath];
@@ -75,17 +85,49 @@ void AssetRegistry::init(const std::filesystem::path &rootFolder, Gfx::PGraphics
reg.materialLoader = new MaterialLoader(graphics);
}
void AssetRegistry::registerMesh(const std::filesystem::path &filePath)
std::string AssetRegistry::getRootFolder()
{
return get().rootFolder.generic_string();
}
void AssetRegistry::importMesh(const std::filesystem::path &filePath)
{
meshLoader->importAsset(filePath);
}
void AssetRegistry::registerTexture(const std::filesystem::path &filePath)
void AssetRegistry::importTexture(const std::filesystem::path &filePath)
{
textureLoader->importAsset(filePath);
}
void AssetRegistry::registerMaterial(const std::filesystem::path &filePath)
void AssetRegistry::importMaterial(const std::filesystem::path &filePath)
{
materialLoader->queueAsset(filePath);
}
}
void AssetRegistry::registerMesh(PMeshAsset mesh)
{
PMeshAsset existingMesh = meshes[mesh->getFileName()];
if(existingMesh != nullptr)
{
auto newMeshes = mesh->getMeshes();
for(uint32 i = 0; i < newMeshes.size(); ++i)
{
existingMesh->addMesh(newMeshes[i]);
}
}
else
{
meshes[mesh->getFileName()] = mesh;
}
}
std::ofstream AssetRegistry::internalCreateWriteStream(const std::string& relativePath, std::ios_base::openmode openmode)
{
return std::ofstream(rootFolder.generic_string().append(relativePath), openmode);
}
std::ifstream AssetRegistry::internalCreateReadStream(const std::string& relativePath, std::ios_base::openmode openmode)
{
return std::ifstream(rootFolder.generic_string().append(relativePath), openmode);
}
+16 -4
View File
@@ -18,21 +18,33 @@ public:
~AssetRegistry();
static void init(const std::string& rootFolder);
static std::string getRootFolder();
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);
static std::ofstream createWriteStream(const std::string& relativePath, std::ios_base::openmode openmode = 0);
static std::ifstream createReadStream(const std::string& relativePath, std::ios_base::openmode openmode = 0);
private:
static AssetRegistry& get();
AssetRegistry();
void init(const std::filesystem::path& rootFolder, Gfx::PGraphics graphics);
void registerMesh(const std::filesystem::path& filePath);
void registerTexture(const std::filesystem::path& filePath);
void registerMaterial(const std::filesystem::path& filePath);
void importMesh(const std::filesystem::path& filePath);
void importTexture(const std::filesystem::path& filePath);
void importMaterial(const std::filesystem::path& filePath);
void registerMesh(PMeshAsset mesh);
void registerTexture(PTextureAsset texture);
void registerMaterial(PMaterialAsset material);
std::ofstream internalCreateWriteStream(const std::string& relativePath, std::ios_base::openmode openmode = 0);
std::ifstream internalCreateReadStream(const std::string& relaitvePath, std::ios_base::openmode openmode = 0);
std::filesystem::path rootFolder;
Map<std::string, PTextureAsset> textures;
Map<std::string, PMeshAsset> meshes;
+5 -1
View File
@@ -5,9 +5,11 @@
using namespace Seele;
MaterialLoader::MaterialLoader(Gfx::PGraphics graphics)
: graphics(graphics)
{
placeholderMaterial = new Material("shaders/Placeholder.semat");
placeholderMaterial = new Material(std::filesystem::absolute("./shaders/Placeholder.asset"));
placeholderMaterial->compile();
graphics->getShaderCompiler()->registerMaterial(placeholderMaterial);
}
MaterialLoader::~MaterialLoader()
@@ -17,6 +19,8 @@ MaterialLoader::~MaterialLoader()
PMaterial MaterialLoader::queueAsset(const std::filesystem::path& filePath)
{
PMaterial result = new Material(filePath);
result->compile();
graphics->getShaderCompiler()->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;
+1
View File
@@ -16,6 +16,7 @@ public:
~MaterialLoader();
PMaterial queueAsset(const std::filesystem::path& filePath);
private:
Gfx::PGraphics graphics;
List<std::future<void>> futures;
PMaterial placeholderMaterial;
};
+42 -37
View File
@@ -3,9 +3,11 @@
#include "Graphics/Graphics.h"
#include "MeshAsset.h"
#include "Graphics/Mesh.h"
#include "Graphics/StaticMeshVertexInput.h"
#include "AssetRegistry.h"
#include "Material/Material.h"
#include <fstream>
#include <iostream>
#include <nlohmann/json.hpp>
#include <stb_image_write.h>
#include <assimp/config.h>
@@ -27,7 +29,8 @@ MeshLoader::~MeshLoader()
void MeshLoader::importAsset(const std::filesystem::path &path)
{
futures.add(std::async(std::launch::async, &MeshLoader::import, this, path));
//futures.add(std::async(std::launch::async, &MeshLoader::import, this, path));
import(path);
}
void loadMaterials(const aiScene* scene, Array<PMaterialAsset>& globalMaterials, Gfx::PGraphics graphics)
@@ -52,7 +55,7 @@ void loadMaterials(const aiScene* scene, Array<PMaterialAsset>& globalMaterials,
matCode["params"]["diffuseTexture"] =
{
{"type", "Texture2D"},
{"default", texFilename}
{"default", texPath.C_Str()}
};
code.push_back("result.baseColor = diffuseTexture.Sample(textureSampler, geometry.texCoord).xyz;\n");
}
@@ -62,7 +65,7 @@ void loadMaterials(const aiScene* scene, Array<PMaterialAsset>& globalMaterials,
matCode["params"]["specularTexture"] =
{
{"type", "Texture2D"},
{"default", texFilename}
{"default", texPath.C_Str()}
};
code.push_back("result.specular = specularTexture.Sample(textureSampler, geometry.texCoord).xyz;\n");
}
@@ -72,7 +75,7 @@ void loadMaterials(const aiScene* scene, Array<PMaterialAsset>& globalMaterials,
matCode["params"]["normalTexture"] =
{
{"type", "Texture2D"},
{"default", texFilename}
{"default", texPath.C_Str()}
};
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");
@@ -80,10 +83,15 @@ void loadMaterials(const aiScene* scene, Array<PMaterialAsset>& globalMaterials,
}
code.push_back("return result;\n");
matCode["code"] = code;
std::ofstream outMatFile("testMat.asset");
std::string outMatFilename = matCode["name"].get<std::string>().append(".asset");
std::ofstream outMatFile = AssetRegistry::createWriteStream(outMatFilename);
outMatFile << std::setw(4) << matCode;
outMatFile.flush();
PMaterial asset = new Material("testMat.asset");
outMatFile.close();
//TODO: let the material loader handle this instead
std::cout << matCode["name"] << std::endl;
AssetRegistry::importFile(outMatFilename);
PMaterialAsset asset = AssetRegistry::findMaterial(outMatFilename);
globalMaterials[i] = asset;
}
}
@@ -108,7 +116,7 @@ void loadToBuffer(Array<Vector>& buffer, const aiVector3D* sourceData, uint32 si
buffer[i] = Vector(sourceData[i].x, sourceData[i].y, sourceData[i].z);
}
}
Gfx::VertexStream createVertexStream(uint32 size, aiVector3D* sourceData, Gfx::PGraphics graphics)
VertexStreamComponent createVertexStream(uint32 size, aiVector3D* sourceData, Gfx::PGraphics graphics)
{
Array<Vector> buffer(size);
for(uint32 i = 0; i < size; ++i)
@@ -121,12 +129,10 @@ Gfx::VertexStream createVertexStream(uint32 size, aiVector3D* sourceData, Gfx::P
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;
const Gfx::PVertexBuffer vertexBuffer = graphics->createVertexBuffer(vbInfo);
return VertexStreamComponent(vertexBuffer, 0, vbInfo.vertexSize, Gfx::SE_FORMAT_R32G32B32_SFLOAT);
}
Gfx::VertexStream createVertexStream(uint32 size, aiVector2D* sourceData, Gfx::PGraphics graphics)
VertexStreamComponent createVertexStream(uint32 size, aiVector2D* sourceData, Gfx::PGraphics graphics)
{
Array<Vector2> buffer(size);
for(uint32 i = 0; i < size; ++i)
@@ -140,9 +146,7 @@ Gfx::VertexStream createVertexStream(uint32 size, aiVector2D* sourceData, Gfx::P
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;
return VertexStreamComponent(vertexBuffer, 0, vbInfo.vertexSize, Gfx::SE_FORMAT_R32G32_SFLOAT);
}
void loadGlobalMeshes(const aiScene* scene, Array<PMesh>& globalMeshes, Array<PMaterialAsset> materials, Gfx::PGraphics graphics)
{
@@ -150,32 +154,29 @@ void loadGlobalMeshes(const aiScene* scene, Array<PMesh>& globalMeshes, Array<PM
{
aiMesh *mesh = scene->mMeshes[meshIndex];
MeshDescription description;
Gfx::PVertexDeclaration declaration = new Gfx::VertexDeclaration();
declaration->addVertexStream(createVertexStream(mesh->mNumVertices, mesh->mVertices, graphics));
description.layout.add(Gfx::VertexAttribute::POSITION);
PStaticMeshVertexInput vertexShaderInput = new StaticMeshVertexInput(std::string(mesh->mName.C_Str()));
VertexStreamComponent positionStream = createVertexStream(mesh->mNumVertices, mesh->mVertices, graphics);
vertexShaderInput->setPositionStream(positionStream);
for(uint32 i = 0; i < MAX_TEX_CHANNELS; ++i)
for(uint32 i = 0; i < MAX_TEXCOORDS; ++i)
{
if(mesh->HasTextureCoords(i))
{
declaration->addVertexStream(createVertexStream(mesh->mNumVertices, mesh->mTextureCoords[i], graphics));
description.layout.add(Gfx::VertexAttribute::TEXCOORD);
VertexStreamComponent texCoordStream = createVertexStream(mesh->mNumVertices, mesh->mTextureCoords[i], graphics);
vertexShaderInput->setTexCoordStream(i, texCoordStream);
}
}
if(mesh->HasNormals())
{
declaration->addVertexStream(createVertexStream(mesh->mNumVertices, mesh->mNormals, graphics));
description.layout.add(Gfx::VertexAttribute::NORMAL);
VertexStreamComponent normalStream = createVertexStream(mesh->mNumVertices, mesh->mNormals, graphics);
vertexShaderInput->setTangentXStream(normalStream);
}
if(mesh->HasTangentsAndBitangents())
{
declaration->addVertexStream(createVertexStream(mesh->mNumVertices, mesh->mVertices, graphics));
declaration->addVertexStream(createVertexStream(mesh->mNumVertices, mesh->mVertices, graphics));
description.layout.add(Gfx::VertexAttribute::TANGENT);
description.layout.add(Gfx::VertexAttribute::BITANGENT);
//TODO: use bitangent to calculate sign for 4th coordinate of tangentstream
VertexStreamComponent tangentStream = createVertexStream(mesh->mNumVertices, mesh->mTangents, graphics);
vertexShaderInput->setTangentZStream(tangentStream);
}
description.declaration = declaration;
Array<uint32> indices(mesh->mNumFaces * 3);
for (uint32 faceIndex = 0; faceIndex < mesh->mNumFaces; ++faceIndex)
@@ -192,7 +193,7 @@ void loadGlobalMeshes(const aiScene* scene, Array<PMesh>& globalMeshes, Array<PM
Gfx::PIndexBuffer indexBuffer = graphics->createIndexBuffer(idxInfo);
indexBuffer->transferOwnership(Gfx::QueueType::GRAPHICS);
globalMeshes[meshIndex] = new Mesh(description, indexBuffer);
globalMeshes[meshIndex] = new Mesh(vertexShaderInput, indexBuffer);
globalMeshes[meshIndex]->referencedMaterial = materials[mesh->mMaterialIndex];
}
}
@@ -247,23 +248,27 @@ void MeshLoader::import(const std::filesystem::path &path)
aiProcess_FindDegenerates |
aiProcess_EmbedTextures);
const aiScene *scene = importer.ApplyPostProcessing(aiProcess_CalcTangentSpace);
Array<PMesh> globalMeshes(scene->mNumMeshes);
Array<PMaterialAsset> globalMaterials(scene->mNumMaterials);
loadMaterials(scene, globalMaterials, graphics);
loadGlobalMeshes(scene, globalMeshes, globalMaterials, graphics);
loadTextures(scene, graphics, path);
loadMaterials(scene, globalMaterials, graphics);
Array<PMesh> globalMeshes(scene->mNumMeshes);
loadGlobalMeshes(scene, globalMeshes, globalMaterials, graphics);
List<aiNode *> meshNodes;
findMeshRoots(scene->mRootNode, meshNodes);
std::filesystem::path filePath = path.filename();
filePath.replace_extension("asset");
PMeshAsset meshAsset = new MeshAsset(filePath.generic_string());
for (auto meshNode : meshNodes)
{
std::string fileName = std::string("arissa").append(".asset");
PMeshAsset meshAsset = new MeshAsset(fileName);
for(uint32 i = 0; i < meshNode->mNumMeshes; ++i)
{
meshAsset->addMesh(globalMeshes[meshNode->mMeshes[i]]);
}
meshAsset->save();
AssetRegistry::get().meshes[meshAsset->getFileName()] = meshAsset;
}
meshAsset->save();
AssetRegistry::get().registerMesh(meshAsset);
}
+13 -11
View File
@@ -12,8 +12,7 @@ using namespace Seele;
TextureLoader::TextureLoader(Gfx::PGraphics graphics)
: graphics(graphics)
{
import("textures/placeholder.png");
placeholderTexture = AssetRegistry::findTexture("textures/placeholder.png");
placeholderTexture = import("./textures/placeholder.png");
}
TextureLoader::~TextureLoader()
@@ -22,15 +21,20 @@ TextureLoader::~TextureLoader()
void TextureLoader::importAsset(const std::filesystem::path& filePath)
{
futures.add(std::async(std::launch::async, &TextureLoader::import, this, filePath));
auto assetFileName = filePath;
PTextureAsset asset = new TextureAsset(assetFileName.replace_extension("asset").filename().generic_string());
asset->setStatus(Asset::Status::Loading);
asset->setTexture(placeholderTexture);
AssetRegistry::get().textures[filePath.string()] = asset;
futures.add(std::async(std::launch::async, [this, filePath, asset] () mutable {
Gfx::PTexture2D texture = import(filePath);
asset->setTexture(texture);
asset->setStatus(Asset::Status::Ready);
}));
}
void TextureLoader::import(const std::filesystem::path& path)
Gfx::PTexture2D TextureLoader::import(const std::filesystem::path& path)
{
auto assetFileName = path;
std::filesystem::path assetPath = AssetRegistry::get().rootFolder.append(assetFileName.replace_extension("asset").filename().string());
PTextureAsset asset = new TextureAsset(assetPath);
asset->setStatus(Asset::Status::Loading);
int x, y, n;
unsigned char* data = stbi_load(path.string().c_str(), &x, &y, &n, 4);
TextureCreateInfo createInfo;
@@ -43,7 +47,5 @@ void TextureLoader::import(const std::filesystem::path& path)
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[path.string()] = asset;
return texture;
}
+3 -2
View File
@@ -9,6 +9,7 @@ namespace Seele
{
DECLARE_REF(TextureAsset);
DECLARE_NAME_REF(Gfx, Graphics);
DECLARE_NAME_REF(Gfx, Texture2D);
class TextureLoader
{
public:
@@ -16,10 +17,10 @@ public:
~TextureLoader();
void importAsset(const std::filesystem::path& filePath);
private:
void import(const std::filesystem::path& path);
Gfx::PTexture2D import(const std::filesystem::path& path);
Gfx::PGraphics graphics;
List<std::future<void>> futures;
PTextureAsset placeholderTexture;
Gfx::PTexture2D placeholderTexture;
};
DEFINE_REF(TextureLoader);
} // namespace Seele