diff --git a/cmake/FindAssimp.cmake b/cmake/FindAssimp.cmake index 4639ce1..b7d2c68 100644 --- a/cmake/FindAssimp.cmake +++ b/cmake/FindAssimp.cmake @@ -89,7 +89,5 @@ if (ASSIMP_FOUND) set(ASSIMP_INCLUDE_DIRS ${ASSIMP_INCLUDE_DIR}) endif() -message(STATUS BINARY: ${ASSIMP_BINARY}) - # Hide some variables mark_as_advanced(ASSIMP_INCLUDE_DIR ASSIMP_LIBRARY) \ No newline at end of file diff --git a/cmake/FindGLFW.cmake b/cmake/FindGLFW.cmake index 03d9242..d8deebc 100644 --- a/cmake/FindGLFW.cmake +++ b/cmake/FindGLFW.cmake @@ -46,8 +46,6 @@ if (WIN32) ${GLFW_ROOT}/src PATH_SUFFIXES Debug Release) - message(STATUS cant find ${GLFW_LIBRARY_NAME} in ${GLFW_ROOT}/src) - unset(GLFW_LIBRARY_NAME) find_file( diff --git a/src/Engine/Asset/Asset.cpp b/src/Engine/Asset/Asset.cpp index d8c4e4d..25d650a 100644 --- a/src/Engine/Asset/Asset.cpp +++ b/src/Engine/Asset/Asset.cpp @@ -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)) diff --git a/src/Engine/Asset/Asset.h b/src/Engine/Asset/Asset.h index 31169d0..ed1f7fd 100644 --- a/src/Engine/Asset/Asset.h +++ b/src/Engine/Asset/Asset.h @@ -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; diff --git a/src/Engine/Asset/AssetRegistry.cpp b/src/Engine/Asset/AssetRegistry.cpp index e6f9feb..b082af9 100644 --- a/src/Engine/Asset/AssetRegistry.cpp +++ b/src/Engine/Asset/AssetRegistry.cpp @@ -7,6 +7,7 @@ #include "Material/Material.h" #include "Graphics/Graphics.h" #include "graphics/WindowManager.h" +#include 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 ® = 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); } \ No newline at end of file diff --git a/src/Engine/Asset/AssetRegistry.h b/src/Engine/Asset/AssetRegistry.h index 69a094a..9c1bf32 100644 --- a/src/Engine/Asset/AssetRegistry.h +++ b/src/Engine/Asset/AssetRegistry.h @@ -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 textures; Map meshes; Map materials; diff --git a/src/Engine/Asset/MaterialLoader.cpp b/src/Engine/Asset/MaterialLoader.cpp index b200cf6..551e7ae 100644 --- a/src/Engine/Asset/MaterialLoader.cpp +++ b/src/Engine/Asset/MaterialLoader.cpp @@ -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; } diff --git a/src/Engine/Asset/MaterialLoader.h b/src/Engine/Asset/MaterialLoader.h index 37aa30f..b655947 100644 --- a/src/Engine/Asset/MaterialLoader.h +++ b/src/Engine/Asset/MaterialLoader.h @@ -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> futures; PMaterial placeholderMaterial; diff --git a/src/Engine/Asset/MeshAsset.cpp b/src/Engine/Asset/MeshAsset.cpp index 80c6e29..e15fae7 100644 --- a/src/Engine/Asset/MeshAsset.cpp +++ b/src/Engine/Asset/MeshAsset.cpp @@ -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: } \ No newline at end of file diff --git a/src/Engine/Asset/MeshAsset.h b/src/Engine/Asset/MeshAsset.h index faf148f..262907a 100644 --- a/src/Engine/Asset/MeshAsset.h +++ b/src/Engine/Asset/MeshAsset.h @@ -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 getMeshes() const + { + return meshes; } private: - PMesh mesh; + Array meshes; + Array referencedMaterials; }; DEFINE_REF(MeshAsset); } // namespace Seele \ No newline at end of file diff --git a/src/Engine/Asset/MeshLoader.cpp b/src/Engine/Asset/MeshLoader.cpp index a478927..99161f0 100644 --- a/src/Engine/Asset/MeshLoader.cpp +++ b/src/Engine/Asset/MeshLoader.cpp @@ -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 +#include +#include +#include +#include #include #include #include +#include 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& meshNodes) +void findMeshRoots(aiNode *node, List &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& 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 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 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& 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 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 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 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 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 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; + } } diff --git a/src/Engine/Asset/MeshLoader.h b/src/Engine/Asset/MeshLoader.h index 4d6c62a..43eba68 100644 --- a/src/Engine/Asset/MeshLoader.h +++ b/src/Engine/Asset/MeshLoader.h @@ -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> futures; Gfx::PGraphics graphics; }; diff --git a/src/Engine/Asset/TextureAsset.cpp b/src/Engine/Asset/TextureAsset.cpp index 7c39ced..753ff46 100644 --- a/src/Engine/Asset/TextureAsset.cpp +++ b/src/Engine/Asset/TextureAsset.cpp @@ -1,5 +1,9 @@ #include "TextureAsset.h" #include "Graphics/GraphicsResources.h" +#include "Graphics/Graphics.h" +#include "Graphics/WindowManager.h" +#include +#include 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); } \ No newline at end of file diff --git a/src/Engine/Asset/TextureAsset.h b/src/Engine/Asset/TextureAsset.h index 4c41067..a26a271 100644 --- a/src/Engine/Asset/TextureAsset.h +++ b/src/Engine/Asset/TextureAsset.h @@ -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); diff --git a/src/Engine/Asset/TextureLoader.cpp b/src/Engine/Asset/TextureLoader.cpp index 1ad11e2..1e9c2ef 100644 --- a/src/Engine/Asset/TextureLoader.cpp +++ b/src/Engine/Asset/TextureLoader.cpp @@ -4,6 +4,8 @@ #include "AssetRegistry.h" #define STB_IMAGE_IMPLEMENTATION #include +#define STB_IMAGE_WRITE_IMPLEMENTATION +#include 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; } \ No newline at end of file diff --git a/src/Engine/Asset/TextureLoader.h b/src/Engine/Asset/TextureLoader.h index b84acab..5246977 100644 --- a/src/Engine/Asset/TextureLoader.h +++ b/src/Engine/Asset/TextureLoader.h @@ -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> futures; PTextureAsset placeholderTexture; diff --git a/src/Engine/Containers/Array.h b/src/Engine/Containers/Array.h index fe17291..cce6f8a 100644 --- a/src/Engine/Containers/Array.h +++ b/src/Engine/Containers/Array.h @@ -10,442 +10,479 @@ namespace Seele { -template -struct Array -{ -public: - Array() - : allocated(DEFAULT_ALLOC_SIZE), arraySize(0) + template + struct Array { - _data = new T[DEFAULT_ALLOC_SIZE]; - assert(_data != nullptr); - memset(_data, 0, sizeof(T) * DEFAULT_ALLOC_SIZE); - refreshIterators(); - } - Array(uint32 size, T value = T()) - : allocated(size), arraySize(size) - { - _data = new T[size]; - assert(_data != nullptr); - for (uint32 i = 0; i < size; ++i) + public: + Array() + : allocated(DEFAULT_ALLOC_SIZE), arraySize(0) { - assert(i < size); - _data[i] = value; + _data = new T[DEFAULT_ALLOC_SIZE]; + assert(_data != nullptr); + //std::memset(_data, 0, sizeof(T) * DEFAULT_ALLOC_SIZE); + refreshIterators(); } - refreshIterators(); - } - Array(std::initializer_list init) - : allocated((uint32)init.size()), arraySize((uint32)init.size()) - { - _data = new T[init.size()]; - auto it = init.begin(); - for (size_t i = 0; it != init.end(); i++, it++) + Array(uint32 size, T value = T()) + : allocated(size), arraySize(size) { - assert(i < init.size()); - _data[i] = *it; - } - refreshIterators(); - } - Array(const Array &other) - : allocated(other.allocated), arraySize(other.arraySize) - { - _data = new T[other.allocated]; - assert(_data != nullptr); - std::memcpy(_data, other._data, sizeof(T) * allocated); - refreshIterators(); - } - Array(Array &&other) noexcept - : allocated(std::move(other.allocated)), arraySize(std::move(other.arraySize)) - { - _data = other._data; - other._data = nullptr; - other.allocated = 0; - other.arraySize = 0; - refreshIterators(); - } - Array &operator=(const Array &other) noexcept - { - if (*this != other) - { - if (_data != nullptr) + _data = new T[size]; + assert(_data != nullptr); + for (uint32 i = 0; i < size; ++i) { - delete[] _data; + assert(i < size); + _data[i] = value; } - allocated = other.allocated; - arraySize = other.arraySize; + refreshIterators(); + } + Array(std::initializer_list init) + : allocated((uint32)init.size()), arraySize((uint32)init.size()) + { + _data = new T[init.size()]; + auto it = init.begin(); + for (size_t i = 0; it != init.end(); i++, it++) + { + assert(i < init.size()); + _data[i] = *it; + } + refreshIterators(); + } + Array(const Array &other) + : allocated(other.allocated), arraySize(other.arraySize) + { _data = new T[other.allocated]; + assert(_data != nullptr); std::memcpy(_data, other._data, sizeof(T) * allocated); refreshIterators(); } - return *this; - } - Array &operator=(Array &&other) noexcept - { - if (*this != other) + Array(Array &&other) noexcept + : allocated(std::move(other.allocated)), arraySize(std::move(other.arraySize)) { - if (_data != nullptr) - { - delete[] _data; - } - allocated = std::move(other.allocated); - arraySize = std::move(other.arraySize); _data = other._data; other._data = nullptr; + other.allocated = 0; + other.arraySize = 0; + refreshIterators(); } - return *this; - } - ~Array() - { - if (_data) + Array &operator=(const Array &other) noexcept + { + if (*this != other) + { + if (_data != nullptr) + { + delete[] _data; + } + allocated = other.allocated; + arraySize = other.arraySize; + _data = new T[other.allocated]; + std::memcpy(_data, other._data, sizeof(T) * allocated); + refreshIterators(); + } + return *this; + } + Array &operator=(Array &&other) noexcept + { + if (*this != other) + { + if (_data != nullptr) + { + delete[] _data; + } + allocated = std::move(other.allocated); + arraySize = std::move(other.arraySize); + _data = other._data; + other._data = nullptr; + } + return *this; + } + ~Array() + { + if (_data) + { + delete[] _data; + _data = nullptr; + } + } + template + class IteratorBase + { + public: + typedef std::forward_iterator_tag iterator_category; + typedef X value_type; + typedef std::ptrdiff_t difference_type; + typedef X &reference; + typedef X *pointer; + + IteratorBase(X *x = nullptr) + : p(x) + { + } + IteratorBase(const IteratorBase &i) + : p(i.p) + { + } + reference operator*() const + { + return *p; + } + pointer operator->() const + { + return p; + } + inline bool operator!=(const IteratorBase &other) + { + return p != other.p; + } + inline bool operator==(const IteratorBase &other) + { + return p == other.p; + } + inline int operator-(const IteratorBase &other) + { + return (int)(p - other.p); + } + IteratorBase &operator++() + { + p++; + return *this; + } + IteratorBase &operator--() + { + p--; + return *this; + } + IteratorBase operator++(int) + { + IteratorBase tmp(*this); + ++*this; + return tmp; + } + IteratorBase operator--(int) + { + IteratorBase tmp(*this); + --*this; + return tmp; + } + + private: + X *p; + }; + typedef IteratorBase Iterator; + typedef IteratorBase ConstIterator; + + bool operator==(const Array &other) + { + return _data == other._data; + } + + bool operator!=(const Array &other) + { + return !(*this == other); + } + + Iterator find(const T &item) + { + for (uint32 i = 0; i < arraySize; ++i) + { + if (_data[i] == item) + { + return Iterator(&_data[i]); + } + } + return endIt; + } + Iterator begin() const + { + return beginIt; + } + Iterator end() const + { + return endIt; + } + ConstIterator cbegin() const + { + return beginIt; + } + ConstIterator cend() const + { + return endIt; + } + + T &add(const T &item = T()) + { + if (arraySize == allocated) + { + uint32 newSize = arraySize + 1; + allocated = calculateGrowth(newSize); + T *tempArray = new T[allocated]; + assert(tempArray != nullptr); + for (uint32 i = 0; i < arraySize; ++i) + { + tempArray[i] = _data[i]; + } + delete[] _data; + _data = tempArray; + } + _data[arraySize++] = item; + refreshIterators(); + return _data[arraySize - 1]; + } + template + T &emplace(args...) + { + if (arraySize == allocated) + { + uint32 newSize = arraySize + 1; + allocated = calculateGrowth(newSize); + T *tempArray = new T[allocated]; + assert(tempArray != nullptr); + for (uint32 i = 0; i < arraySize; ++i) + { + tempArray[i] = _data[i]; + } + delete[] _data; + _data = tempArray; + } + _data[arraySize++] = T(args...); + refreshIterators(); + return _data[arraySize - 1]; + } + void remove(Iterator it, bool keepOrder = true) + { + remove(it - beginIt, keepOrder); + } + void remove(int index, bool keepOrder = true) + { + if (keepOrder) + { + std::memcpy(&_data[index], &_data[index + 1], sizeof(T) * (arraySize - index)); + } + else + { + _data[index] = _data[arraySize - 1]; + } + arraySize--; + } + void clear() { delete[] _data; _data = nullptr; + arraySize = 0; + allocated = 0; + refreshIterators(); } - } - template - class IteratorBase - { - public: - typedef std::forward_iterator_tag iterator_category; - typedef X value_type; - typedef std::ptrdiff_t difference_type; - typedef X &reference; - typedef X *pointer; - - IteratorBase(X *x = nullptr) - : p(x) + void resize(uint32 newSize) { + if (newSize < allocated) + { + arraySize = newSize; + } + else + { + T *newData = new T[newSize]; + assert(newData != nullptr); + allocated = newSize; + std::memcpy(newData, _data, sizeof(T) * arraySize); + arraySize = newSize; + delete _data; + _data = newData; + } + refreshIterators(); } - IteratorBase(const IteratorBase &i) - : p(i.p) + inline uint32 size() const { + return arraySize; } - reference operator*() const + inline uint32 capacity() const { - return *p; + return allocated; } - pointer operator->() const + inline T *data() const { - return p; + return _data; } - inline bool operator!=(const IteratorBase &other) + T &back() const { - return p != other.p; + return _data[arraySize - 1]; } - inline bool operator==(const IteratorBase &other) + void pop() { - return p == other.p; + arraySize--; + refreshIterators(); } - inline int operator-(const IteratorBase &other) + T &operator[](uint32 index) { - return (int)(p - other.p); + assert(index < arraySize); + return _data[index]; } - IteratorBase &operator++() + inline T &operator[](size_t index) { - p++; - return *this; + return this->operator[]((uint32)index); } - IteratorBase &operator--() + inline T &operator[](int32 index) { - p--; - return *this; + return this->operator[]((uint32)index); } - IteratorBase operator++(int) + const T &operator[](uint32 index) const { - IteratorBase tmp(*this); - ++*this; - return tmp; + assert(index < arraySize); + return _data[index]; } - IteratorBase operator--(int) + inline const T &operator[](size_t index) const { - IteratorBase tmp(*this); - --*this; - return tmp; + return this->operator[]((uint32)index); + } + inline const T &operator[](int32 index) const + { + return this->operator[]((uint32)index); } private: - X *p; + uint32 calculateGrowth(uint32 newSize) const + { + const uint32 oldCapacity = capacity(); + + if (oldCapacity > UINT32_MAX - oldCapacity / 2) + { + return newSize; // geometric growth would overflow + } + + const uint32 geometric = oldCapacity + oldCapacity / 2; + + if (geometric < newSize) + { + return newSize; // geometric growth would be insufficient + } + + return geometric; // geometric growth is sufficient + } + void refreshIterators() + { + beginIt = Iterator(_data); + endIt = Iterator(_data + arraySize); + } + uint32 arraySize; + uint32 allocated; + Iterator beginIt; + Iterator endIt; + T *_data; }; - typedef IteratorBase Iterator; - typedef IteratorBase ConstIterator; - bool operator==(const Array &other) - { - return _data == other._data; - } - - bool operator!=(const Array &other) - { - return !(*this == other); - } - - Iterator find(const T &item) - { - for (uint32 i = 0; i < arraySize; ++i) - { - if (_data[i] == item) - { - return Iterator(&_data[i]); - } - } - return endIt; - } - Iterator begin() - { - return beginIt; - } - Iterator begin() const - { - return beginIt; - } - Iterator end() - { - return endIt; - } - Iterator end() const - { - return endIt; - } - - T &add(const T &item = T()) - { - if (arraySize == allocated) - { - uint32 newSize = arraySize + 1; - allocated = calculateGrowth(newSize); - T *tempArray = new T[allocated]; - assert(tempArray != nullptr); - for (uint32 i = 0; i < arraySize; ++i) - { - tempArray[i] = _data[i]; - } - delete[] _data; - _data = tempArray; - } - _data[arraySize++] = item; - refreshIterators(); - return _data[arraySize - 1]; - } - void remove(Iterator it, bool keepOrder = true) - { - remove(it - beginIt, keepOrder); - } - void remove(int index, bool keepOrder = true) - { - if (keepOrder) - { - std::memcpy(&_data[index], &_data[index + 1], sizeof(T) * (arraySize - index)); - } - else - { - _data[index] = _data[arraySize - 1]; - } - arraySize--; - } - void clear() - { - delete[] _data; - _data = nullptr; - arraySize = 0; - allocated = 0; - refreshIterators(); - } - void resize(uint32 newSize) - { - if (newSize < allocated) - { - arraySize = newSize; - } - else - { - T *newData = new T[newSize]; - assert(newData != nullptr); - allocated = newSize; - std::memcpy(newData, _data, sizeof(T) * arraySize); - arraySize = newSize; - delete _data; - _data = newData; - } - refreshIterators(); - } - inline uint32 size() const - { - return arraySize; - } - inline uint32 capacity() const - { - return allocated; - } - inline T *data() const - { - return _data; - } - T &back() const - { - return _data[arraySize - 1]; - } - void pop() - { - arraySize--; - } - T &operator[](uint32 index) - { - assert(index < arraySize); - return _data[index]; - } - inline T &operator[](size_t index) - { - return this->operator[]((uint32)index); - } - inline T &operator[](int32 index) - { - return this->operator[]((uint32)index); - } - const T &operator[](uint32 index) const - { - assert(index < arraySize); - return _data[index]; - } - inline const T &operator[](size_t index) const - { - return this->operator[]((uint32)index); - } - inline const T &operator[](int32 index) const - { - return this->operator[]((uint32)index); - } - -private: - uint32 calculateGrowth(uint32 newSize) const - { - const uint32 oldCapacity = capacity(); - - if (oldCapacity > UINT32_MAX - oldCapacity / 2) - { - return newSize; // geometric growth would overflow - } - - const uint32 geometric = oldCapacity + oldCapacity / 2; - - if (geometric < newSize) - { - return newSize; // geometric growth would be insufficient - } - - return geometric; // geometric growth is sufficient - } - void refreshIterators() - { - beginIt = Iterator(_data); - endIt = Iterator(_data + arraySize); - } - uint32 arraySize; - uint32 allocated; - Iterator beginIt; - Iterator endIt; - T *_data; -}; - -template -struct StaticArray -{ -public: - StaticArray() - { - beginIt = Iterator(_data); - endIt = Iterator(_data + N); - } - StaticArray(T value) - { - for (int i = 0; i < N; ++i) - { - _data[i] = value; - } - beginIt = Iterator(_data); - endIt = Iterator(_data + N); - } - ~StaticArray() - { - } - - inline uint32 size() const - { - return N; - } - inline T *data() - { - return _data; - } - inline const T* data() const - { - return _data; - } - T &operator[](int index) - { - assert(index >= 0 && index < N); - return _data[index]; - } - const T &operator[](int index) const - { - assert(index >= 0 && index < N); - return _data[index]; - } - - template - class IteratorBase + template + struct StaticArray { public: - typedef std::forward_iterator_tag iterator_category; - typedef X value_type; - typedef std::ptrdiff_t difference_type; - typedef X &reference; - typedef X *pointer; - - IteratorBase(X *x = nullptr) - : p(x) + StaticArray() + { + beginIt = Iterator(_data); + endIt = Iterator(_data + N); + } + StaticArray(T value) + { + for (int i = 0; i < N; ++i) + { + _data[i] = value; + } + beginIt = Iterator(_data); + endIt = Iterator(_data + N); + } + ~StaticArray() { } - IteratorBase(const IteratorBase &i) - : p(i.p) + + inline uint32 size() const { + return N; } - reference operator*() const + inline T *data() { - return *p; + return _data; } - pointer operator->() const + inline const T *data() const { - return p; + return _data; } - inline bool operator!=(const IteratorBase &other) + T &operator[](int index) { - return p != other.p; + assert(index >= 0 && index < N); + return _data[index]; } - inline bool operator==(const IteratorBase &other) + const T &operator[](int index) const { - return p == other.p; - } - IteratorBase &operator++() - { - p++; - return *this; - } - IteratorBase operator++(int) - { - IteratorBase tmp(*this); - ++*this; - return tmp; + assert(index >= 0 && index < N); + return _data[index]; } + template + class IteratorBase + { + public: + typedef std::forward_iterator_tag iterator_category; + typedef X value_type; + typedef std::ptrdiff_t difference_type; + typedef X &reference; + typedef X *pointer; + + IteratorBase(X *x = nullptr) + : p(x) + { + } + IteratorBase(const IteratorBase &i) + : p(i.p) + { + } + reference operator*() const + { + return *p; + } + pointer operator->() const + { + return p; + } + inline bool operator!=(const IteratorBase &other) + { + return p != other.p; + } + inline bool operator==(const IteratorBase &other) + { + return p == other.p; + } + IteratorBase &operator++() + { + p++; + return *this; + } + IteratorBase operator++(int) + { + IteratorBase tmp(*this); + ++*this; + return tmp; + } + + private: + X *p; + }; + typedef IteratorBase Iterator; + typedef IteratorBase ConstIterator; + + Iterator begin() + { + return beginIt; + } + Iterator end() + { + return endIt; + } + ConstIterator begin() const + { + return beginIt; + } + ConstIterator end() const + { + return beginIt; + } private: - X *p; + T _data[N]; + Iterator beginIt; + Iterator endIt; }; - typedef IteratorBase Iterator; - typedef IteratorBase ConstIterator; - -private: - T _data[N]; - Iterator beginIt; - Iterator endIt; -}; } // namespace Seele \ No newline at end of file diff --git a/src/Engine/Graphics/Graphics.h b/src/Engine/Graphics/Graphics.h index 7a2b603..6eea7d0 100644 --- a/src/Engine/Graphics/Graphics.h +++ b/src/Engine/Graphics/Graphics.h @@ -2,6 +2,7 @@ #include "MinimalEngine.h" #include "GraphicsResources.h" #include "Containers/Array.h" +#include "RenderPass/VertexFactory.h" namespace Seele { diff --git a/src/Engine/Graphics/GraphicsEnums.h b/src/Engine/Graphics/GraphicsEnums.h index ef90de1..7f48f6c 100644 --- a/src/Engine/Graphics/GraphicsEnums.h +++ b/src/Engine/Graphics/GraphicsEnums.h @@ -5,7 +5,7 @@ namespace Seele namespace Gfx { static constexpr bool useAsyncCompute = true; -static constexpr bool waitIdleOnSubmit = false; +static constexpr bool waitIdleOnSubmit = true; static constexpr uint32 numFramesBuffered = 3; typedef uint32_t SeFlags; diff --git a/src/Engine/Graphics/GraphicsResources.cpp b/src/Engine/Graphics/GraphicsResources.cpp index 50e3983..e0d4546 100644 --- a/src/Engine/Graphics/GraphicsResources.cpp +++ b/src/Engine/Graphics/GraphicsResources.cpp @@ -126,8 +126,10 @@ IndexBuffer::IndexBuffer(PGraphics graphics, uint32 size, Gfx::SeIndexType index IndexBuffer::~IndexBuffer() { } -VertexStream::VertexStream(uint32 stride, uint8 instanced) - : stride(stride), instanced(instanced) +VertexStream::VertexStream() +{} +VertexStream::VertexStream(uint32 stride, uint32 offset, uint8 instanced, Gfx::PVertexBuffer vertexBuffer) + : stride(stride), instanced(instanced), offset(offset), vertexBuffer(vertexBuffer) { } VertexStream::~VertexStream() diff --git a/src/Engine/Graphics/GraphicsResources.h b/src/Engine/Graphics/GraphicsResources.h index 5648f4c..703ed22 100644 --- a/src/Engine/Graphics/GraphicsResources.h +++ b/src/Engine/Graphics/GraphicsResources.h @@ -13,7 +13,7 @@ namespace Seele { - +struct VertexInputStream; namespace Gfx { DECLARE_REF(Graphics); @@ -297,8 +297,8 @@ DEFINE_REF(StructuredBuffer); class VertexStream { public: - VertexStream() {} - VertexStream(uint32 stride, uint8 instanced); + VertexStream(); + VertexStream(uint32 stride, uint32 offset, uint8 instanced, Gfx::PVertexBuffer vertexBuffer); ~VertexStream(); void addVertexElement(VertexElement element); const Array getVertexDescriptions() const; @@ -363,7 +363,7 @@ public: virtual ~RenderCommand(); virtual void bindPipeline(Gfx::PGraphicsPipeline pipeline) = 0; virtual void bindDescriptor(Gfx::PDescriptorSet set) = 0; - virtual void bindVertexBuffer(Gfx::PVertexBuffer vertexBuffer) = 0; + virtual void bindVertexBuffer(const Array& streams) = 0; virtual void bindIndexBuffer(Gfx::PIndexBuffer indexBuffer) = 0; virtual void draw(const MeshBatchElement& data) = 0; }; diff --git a/src/Engine/Graphics/Mesh.cpp b/src/Engine/Graphics/Mesh.cpp index 36082dd..02a0a0e 100644 --- a/src/Engine/Graphics/Mesh.cpp +++ b/src/Engine/Graphics/Mesh.cpp @@ -2,8 +2,8 @@ using namespace Seele; -Mesh::Mesh(Gfx::PVertexBuffer vertexBuffer, Gfx::PIndexBuffer indexBuffer) - : vertexBuffer(vertexBuffer) +Mesh::Mesh(MeshDescription description, Gfx::PIndexBuffer indexBuffer) + : description(description) , indexBuffer(indexBuffer) { } diff --git a/src/Engine/Graphics/Mesh.h b/src/Engine/Graphics/Mesh.h index 14fa4ff..a036fac 100644 --- a/src/Engine/Graphics/Mesh.h +++ b/src/Engine/Graphics/Mesh.h @@ -3,15 +3,54 @@ namespace Seele { +#define MAX_TEX_CHANNELS 4 +enum class VertexAttribute +{ + POSITION, + TEXCOORD, + NORMAL, + TANGENT, + BITANGENT +}; +struct MeshDescription +{ + Array layout; + Gfx::PVertexDeclaration declaration; + uint32 getStride() const + { + return getNumFloats() * sizeof(float); + } + uint32 getNumFloats() const + { + uint32 vertexSize = 0; + for(auto a : layout) + { + switch (a) + { + case VertexAttribute::POSITION: + case VertexAttribute::NORMAL: + case VertexAttribute::TANGENT: + case VertexAttribute::BITANGENT: + vertexSize += 3; + break; + case VertexAttribute::TEXCOORD: + vertexSize += 2; + break; + } + } + return vertexSize; + } +}; +DECLARE_REF(MaterialAsset); class Mesh { public: - Mesh(Gfx::PVertexBuffer vertexBuffer, Gfx::PIndexBuffer indexBuffer); + Mesh(MeshDescription description, Gfx::PIndexBuffer indexBuffer); ~Mesh(); -private: - Gfx::PVertexBuffer vertexBuffer; Gfx::PIndexBuffer indexBuffer; + MeshDescription description; + PMaterialAsset referencedMaterial; }; DEFINE_REF(Mesh); } // namespace Seele \ No newline at end of file diff --git a/src/Engine/Graphics/MeshBatch.h b/src/Engine/Graphics/MeshBatch.h index 282f395..ad6674c 100644 --- a/src/Engine/Graphics/MeshBatch.h +++ b/src/Engine/Graphics/MeshBatch.h @@ -50,11 +50,11 @@ struct MeshBatch uint8 isCastingShadow : 1; uint8 useWireframe : 1; - const Gfx::SePrimitiveTopology topology; + Gfx::SePrimitiveTopology topology; - const PVertexFactory vertexFactory; + PVertexFactory vertexFactory; - const PMaterial material; + PMaterial material; inline int32 getNumPrimitives() const { diff --git a/src/Engine/Graphics/RenderCore.cpp b/src/Engine/Graphics/RenderCore.cpp index 42c9866..2896d37 100644 --- a/src/Engine/Graphics/RenderCore.cpp +++ b/src/Engine/Graphics/RenderCore.cpp @@ -24,6 +24,7 @@ void Seele::RenderCore::init() sceneViewInfo.offsetX = 0; sceneViewInfo.offsetY = 0; PSceneView sceneView = new SceneView(windowManager->getGraphics(), window, sceneViewInfo); + window->addView(sceneView); } void Seele::RenderCore::renderLoop() diff --git a/src/Engine/Graphics/RenderPass/MeshProcessor.cpp b/src/Engine/Graphics/RenderPass/MeshProcessor.cpp index 1a9e41c..f617b4a 100644 --- a/src/Engine/Graphics/RenderPass/MeshProcessor.cpp +++ b/src/Engine/Graphics/RenderPass/MeshProcessor.cpp @@ -15,9 +15,11 @@ MeshProcessor::~MeshProcessor() { } -void MeshProcessor::buildMeshDrawCommands( +void MeshProcessor::buildMeshDrawCommand( const MeshBatch& meshBatch, - const PPrimitiveComponent primitiveComponent, + const PPrimitiveComponent primitiveComponent, + const Gfx::PRenderPass renderPass, + Gfx::PRenderCommand drawCommand, PMaterial material, Gfx::PVertexShader vertexShader, Gfx::PControlShader controlShader, @@ -37,6 +39,7 @@ void MeshProcessor::buildMeshDrawCommands( pipelineInitializer.evalShader = evaluationShader; pipelineInitializer.geometryShader = geometryShader; pipelineInitializer.fragmentShader = fragmentShader; + pipelineInitializer.renderPass = renderPass; VertexInputStreamArray vertexStreams; if(positionOnly) @@ -47,8 +50,12 @@ void MeshProcessor::buildMeshDrawCommands( { vertexFactory->getStreams(vertexStreams); } - if(vertexShader != nullptr) + drawCommand->bindVertexBuffer(vertexStreams); + Gfx::PGraphicsPipeline pipeline = graphics->createGraphicsPipeline(pipelineInitializer); + drawCommand->bindPipeline(pipeline); + for(auto element : meshBatch.elements) { - - } + drawCommand->bindIndexBuffer(element.indexBuffer); + drawCommand->draw(element); + } } \ No newline at end of file diff --git a/src/Engine/Graphics/RenderPass/MeshProcessor.h b/src/Engine/Graphics/RenderPass/MeshProcessor.h index 4025071..1613370 100644 --- a/src/Engine/Graphics/RenderPass/MeshProcessor.h +++ b/src/Engine/Graphics/RenderPass/MeshProcessor.h @@ -18,9 +18,11 @@ private: const MeshBatch& meshBatch, const PPrimitiveComponent primitiveComponent, int32 staticMeshId = -1) = 0; - void buildMeshDrawCommands( + void buildMeshDrawCommand( const MeshBatch& meshBatch, - const PPrimitiveComponent primitiveComponent, + const PPrimitiveComponent primitiveComponent, + const Gfx::PRenderPass renderPass, + Gfx::PRenderCommand drawCommand, PMaterial material, Gfx::PVertexShader vertexShader, Gfx::PControlShader controlShader, diff --git a/src/Engine/Graphics/RenderPass/VertexFactory.cpp b/src/Engine/Graphics/RenderPass/VertexFactory.cpp index e143a7a..218c0e2 100644 --- a/src/Engine/Graphics/RenderPass/VertexFactory.cpp +++ b/src/Engine/Graphics/RenderPass/VertexFactory.cpp @@ -1,7 +1,44 @@ #include "VertexFactory.h" +#include "Graphics/Mesh.h" +#include using namespace Seele; +List VertexFactory::registeredVertexFactories; +std::mutex VertexFactory::registeredVertexFactoryLock; + +VertexFactory::VertexFactory() +{ + std::scoped_lock lock(registeredVertexFactoryLock); + registeredVertexFactories.add(this); +} + +VertexFactory::VertexFactory(MeshDescription description) + : declaration(description.declaration) +{ + auto declStreams = declaration->getVertexStreams(); + std::copy(declStreams.begin(), declStreams.end(), streams.begin()); + + uint32 positionStreamIndex = 0; + positionDeclaration = new Gfx::VertexDeclaration(); + for (uint32 i = 0; i < declStreams.size(); i++) + { + if(description.layout[i] == VertexAttribute::POSITION) + { + positionStream[positionStreamIndex++] = declStreams[i]; + positionDeclaration->addVertexStream(declStreams[i]); + } + } + + std::scoped_lock lock(registeredVertexFactoryLock); + registeredVertexFactories.add(this); +} + +VertexFactory::~VertexFactory() +{ + registeredVertexFactories.remove(registeredVertexFactories.find(this)); +} + void VertexFactory::getStreams(VertexInputStreamArray& outVertexStreams) const { for(uint32 i = 0; i < streams.size(); ++i) diff --git a/src/Engine/Graphics/RenderPass/VertexFactory.h b/src/Engine/Graphics/RenderPass/VertexFactory.h index d3f0393..f93559a 100644 --- a/src/Engine/Graphics/RenderPass/VertexFactory.h +++ b/src/Engine/Graphics/RenderPass/VertexFactory.h @@ -35,7 +35,7 @@ struct VertexInputStream } }; -struct VertexStreamComponent +/*struct VertexStreamComponent { const Gfx::PVertexBuffer vertexBuffer = nullptr; @@ -68,21 +68,25 @@ struct VertexStreamComponent , type(type) { } -}; +};*/ typedef Array VertexInputStreamArray; +struct MeshDescription; +DECLARE_REF(VertexFactory); class VertexFactory { public: - VertexFactory() - { - } + VertexFactory(); + VertexFactory(MeshDescription description); + ~VertexFactory(); void getStreams(VertexInputStreamArray& outVertexStreams) const; void getPositionOnlyStream(VertexInputStreamArray& outVertexStreams) const; virtual bool supportsTesselation() { return false; } Gfx::PVertexDeclaration getDeclaration() const {return declaration;} Gfx::PVertexDeclaration getPositionDeclaration() const {return positionDeclaration;} private: + static List registeredVertexFactories; + static std::mutex registeredVertexFactoryLock; StaticArray streams; StaticArray positionStream; Gfx::PVertexDeclaration declaration; diff --git a/src/Engine/Graphics/SceneRenderPath.cpp b/src/Engine/Graphics/SceneRenderPath.cpp index 849be00..51aed2e 100644 --- a/src/Engine/Graphics/SceneRenderPath.cpp +++ b/src/Engine/Graphics/SceneRenderPath.cpp @@ -1,12 +1,17 @@ #include "SceneRenderPath.h" #include "Scene/Scene.h" #include "Material/Material.h" +#include "Asset/AssetRegistry.h" using namespace Seele; SceneRenderPath::SceneRenderPath(Gfx::PGraphics graphics, Gfx::PViewport target) : RenderPath(graphics, target) { + scene = new Scene(); + PMeshAsset asset = AssetRegistry::findMesh("Unbenannt"); + PActor rootActor = new Actor(); + PPrimitiveComponent primitiveComponent = new PrimitiveComponent(); } SceneRenderPath::~SceneRenderPath() diff --git a/src/Engine/Graphics/SceneView.cpp b/src/Engine/Graphics/SceneView.cpp index e195c2f..f35ce44 100644 --- a/src/Engine/Graphics/SceneView.cpp +++ b/src/Engine/Graphics/SceneView.cpp @@ -1,5 +1,6 @@ #include "SceneView.h" #include "SceneRenderPath.h" +#include "Scene/Scene.h" #include "Window.h" Seele::SceneView::SceneView(Gfx::PGraphics graphics, PWindow owner, const ViewportCreateInfo &createInfo) diff --git a/src/Engine/Graphics/Vulkan/VulkanBuffer.cpp b/src/Engine/Graphics/Vulkan/VulkanBuffer.cpp index 9ab7c62..c972232 100644 --- a/src/Engine/Graphics/Vulkan/VulkanBuffer.cpp +++ b/src/Engine/Graphics/Vulkan/VulkanBuffer.cpp @@ -129,7 +129,7 @@ void Buffer::executeOwnershipBarrier(Gfx::QueueType newOwner) dynamicBarriers[i] = barrier; dynamicBarriers[i].buffer = buffers[i].buffer; dynamicBarriers[i].offset = 0; - dynamicBarriers[i].size = buffers[i].allocation->getSize(); + dynamicBarriers[i].size = size; } vkCmdPipelineBarrier(srcCommand, srcStage, dstStage, 0, 0, nullptr, numBuffers, dynamicBarriers, 0, nullptr); vkCmdPipelineBarrier(dstCommand, srcStage, dstStage, 0, 0, nullptr, numBuffers, dynamicBarriers, 0, nullptr); diff --git a/src/Engine/Graphics/Vulkan/VulkanCommandBuffer.cpp b/src/Engine/Graphics/Vulkan/VulkanCommandBuffer.cpp index 9a42812..fee8170 100644 --- a/src/Engine/Graphics/Vulkan/VulkanCommandBuffer.cpp +++ b/src/Engine/Graphics/Vulkan/VulkanCommandBuffer.cpp @@ -163,12 +163,17 @@ void SecondaryCmdBuffer::bindDescriptor(Gfx::PDescriptorSet descriptorSet) VkDescriptorSet setHandle = descriptorSet.cast()->getHandle(); vkCmdBindDescriptorSets(handle, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline->getLayout(), descriptorSet->getSetIndex(), 1, &setHandle, 0, nullptr); } -void SecondaryCmdBuffer::bindVertexBuffer(Gfx::PVertexBuffer vertexBuffer) +void SecondaryCmdBuffer::bindVertexBuffer(const Array& streams) { - PVertexBuffer buf = vertexBuffer.cast(); - const VkBuffer bufHandle[1] = {buf->getHandle()}; - const VkDeviceSize offsets[1] = {0}; - vkCmdBindVertexBuffers(handle, 0, 1, bufHandle, offsets); + Array buffers(streams.size()); + Array offsets(streams.size()); + for(uint32 i = 0; i < streams.size(); ++i) + { + PVertexBuffer buf = streams[i].vertexBuffer.cast(); + buffers[i] = buf->getHandle(); + offsets[i] = streams[i].offset; + }; + vkCmdBindVertexBuffers(handle, 0, streams.size(), buffers.data(), offsets.data()); } void SecondaryCmdBuffer::bindIndexBuffer(Gfx::PIndexBuffer indexBuffer) { @@ -181,7 +186,7 @@ void SecondaryCmdBuffer::draw(const MeshBatchElement& data) } CommandBufferManager::CommandBufferManager(PGraphics graphics, PQueue queue) - : graphics(graphics), queue(queue) + : graphics(graphics), queue(queue), queueFamilyIndex(queue->getFamilyIndex()) { VkCommandPoolCreateInfo info = init::CommandPoolCreateInfo(); diff --git a/src/Engine/Graphics/Vulkan/VulkanCommandBuffer.h b/src/Engine/Graphics/Vulkan/VulkanCommandBuffer.h index b56b997..c3b7dc4 100644 --- a/src/Engine/Graphics/Vulkan/VulkanCommandBuffer.h +++ b/src/Engine/Graphics/Vulkan/VulkanCommandBuffer.h @@ -4,6 +4,7 @@ namespace Seele { +struct VertexInputStream; namespace Vulkan { DECLARE_REF(RenderPass); @@ -75,7 +76,7 @@ public: void end(); virtual void bindPipeline(Gfx::PGraphicsPipeline pipeline) override; virtual void bindDescriptor(Gfx::PDescriptorSet descriptorSet) override; - virtual void bindVertexBuffer(Gfx::PVertexBuffer vertexBuffer) override; + virtual void bindVertexBuffer(const Array& streams) override; virtual void bindIndexBuffer(Gfx::PIndexBuffer indexBuffer) override; virtual void draw(const MeshBatchElement& data) override; diff --git a/src/Engine/Graphics/Vulkan/VulkanGraphics.cpp b/src/Engine/Graphics/Vulkan/VulkanGraphics.cpp index 5c20342..f9f773a 100644 --- a/src/Engine/Graphics/Vulkan/VulkanGraphics.cpp +++ b/src/Engine/Graphics/Vulkan/VulkanGraphics.cpp @@ -21,12 +21,6 @@ Graphics::Graphics() Graphics::~Graphics() { - allocator = nullptr; - stagingManager = nullptr; - graphicsCommands = nullptr; - computeCommands = nullptr; - transferCommands = nullptr; - dedicatedTransferCommands = nullptr; viewports.clear(); vkDestroyDevice(handle, nullptr); DestroyDebugReportCallbackEXT(instance, nullptr, callback); @@ -41,10 +35,6 @@ void Graphics::init(GraphicsInitializer initInfo) createDevice(initInfo); allocator = new Allocator(this); stagingManager = new StagingManager(this, allocator); - graphicsCommands = new CommandBufferManager(this, graphicsQueue); - computeCommands = new CommandBufferManager(this, computeQueue); - transferCommands = new CommandBufferManager(this, transferQueue); - dedicatedTransferCommands = new CommandBufferManager(this, dedicatedTransferQueue); pipelineCache = new PipelineCache(this, "pipeline.cache"); } @@ -79,12 +69,12 @@ void Graphics::beginRenderPass(Gfx::PRenderPass renderPass) { framebuffer = found->value; } - graphicsCommands->getCommands()->beginRenderPass(rp, framebuffer); + getGraphicsCommands()->getCommands()->beginRenderPass(rp, framebuffer); } void Graphics::endRenderPass() { - graphicsCommands->getCommands()->endRenderPass(); + getGraphicsCommands()->getCommands()->endRenderPass(); } void Graphics::executeCommands(Array commands) @@ -96,7 +86,7 @@ void Graphics::executeCommands(Array commands) buf->end(); cmdBuffers[i] = buf->getHandle(); } - vkCmdExecuteCommands(graphicsCommands->getCommands()->getHandle(), cmdBuffers.size(), cmdBuffers.data()); + vkCmdExecuteCommands(getGraphicsCommands()->getCommands()->getHandle(), cmdBuffers.size(), cmdBuffers.data()); } Gfx::PTexture2D Graphics::createTexture2D(const TextureCreateInfo &createInfo) @@ -129,7 +119,7 @@ Gfx::PIndexBuffer Graphics::createIndexBuffer(const IndexBufferCreateInfo &bulkD } Gfx::PRenderCommand Graphics::createRenderCommand() { - PSecondaryCmdBuffer cmdBuffer = graphicsCommands->createSecondaryCmdBuffer(); + PSecondaryCmdBuffer cmdBuffer = getGraphicsCommands()->createSecondaryCmdBuffer(); cmdBuffer->begin(getGraphicsCommands()->getCommands()); return cmdBuffer; } @@ -199,33 +189,60 @@ PCommandBufferManager Graphics::getQueueCommands(Gfx::QueueType queueType) switch (queueType) { case Gfx::QueueType::GRAPHICS: - return graphicsCommands; + return getGraphicsCommands(); case Gfx::QueueType::COMPUTE: - return computeCommands; + return getComputeCommands(); case Gfx::QueueType::TRANSFER: - return transferCommands; + return getTransferCommands(); case Gfx::QueueType::DEDICATED_TRANSFER: - return dedicatedTransferCommands; + return getDedicatedTransferCommands(); default: throw new std::logic_error("invalid queue type"); } } - +std::mutex graphicsCommandLock; PCommandBufferManager Graphics::getGraphicsCommands() { - return graphicsCommands; + std::scoped_lock lock(graphicsCommandLock); + auto id = std::this_thread::get_id(); + if(graphicsCommands.find(id) == graphicsCommands.end()) + { + graphicsCommands[id] = new CommandBufferManager(this, graphicsQueue); + } + return graphicsCommands[id]; } +std::mutex computeCommandLock; PCommandBufferManager Graphics::getComputeCommands() { - return computeCommands; + std::scoped_lock lock(computeCommandLock); + auto id = std::this_thread::get_id(); + if(computeCommands.find(id) == computeCommands.end()) + { + computeCommands[id] = new CommandBufferManager(this, computeQueue); + } + return computeCommands[id]; } +std::mutex transferCommandLock; PCommandBufferManager Graphics::getTransferCommands() { - return transferCommands; + std::scoped_lock lock(transferCommandLock); + auto id = std::this_thread::get_id(); + if(transferCommands.find(id) == transferCommands.end()) + { + transferCommands[id] = new CommandBufferManager(this, transferQueue); + } + return transferCommands[id]; } +std::mutex dedicatedCommandLock; PCommandBufferManager Graphics::getDedicatedTransferCommands() { - return dedicatedTransferCommands; + std::scoped_lock lock(dedicatedCommandLock); + auto id = std::this_thread::get_id(); + if(dedicatedTransferCommands.find(id) == dedicatedTransferCommands.end()) + { + dedicatedTransferCommands[id] = new CommandBufferManager(this, dedicatedTransferQueue); + } + return dedicatedTransferCommands[id]; } PAllocator Graphics::getAllocator() { diff --git a/src/Engine/Graphics/Vulkan/VulkanGraphics.h b/src/Engine/Graphics/Vulkan/VulkanGraphics.h index 6bded7a..ed5536a 100644 --- a/src/Engine/Graphics/Vulkan/VulkanGraphics.h +++ b/src/Engine/Graphics/Vulkan/VulkanGraphics.h @@ -80,10 +80,10 @@ protected: PQueue dedicatedTransferQueue; QueueOwnedResourceDeletion deletionQueue; PPipelineCache pipelineCache; - PCommandBufferManager graphicsCommands; - PCommandBufferManager computeCommands; - PCommandBufferManager transferCommands; - PCommandBufferManager dedicatedTransferCommands; + Map graphicsCommands; + Map computeCommands; + Map transferCommands; + Map dedicatedTransferCommands; VkPhysicalDeviceProperties props; VkPhysicalDeviceFeatures features; VkDebugReportCallbackEXT callback; diff --git a/src/Engine/Graphics/Vulkan/VulkanTexture.cpp b/src/Engine/Graphics/Vulkan/VulkanTexture.cpp index 1f37026..593ed43 100644 --- a/src/Engine/Graphics/Vulkan/VulkanTexture.cpp +++ b/src/Engine/Graphics/Vulkan/VulkanTexture.cpp @@ -78,6 +78,7 @@ TextureHandle::TextureHandle(PGraphics graphics, VkImageViewType viewType, default: break; } + info.initialLayout = layout; info.mipLevels = mipLevels; info.arrayLayers = arrayCount * layerCount; diff --git a/src/Engine/Material/Material.cpp b/src/Engine/Material/Material.cpp index b397194..113d1d5 100644 --- a/src/Engine/Material/Material.cpp +++ b/src/Engine/Material/Material.cpp @@ -15,7 +15,7 @@ Material::Material(const std::string& directory, const std::string& name) { } -Material::Material(const std::string& fullPath) +Material::Material(const std::filesystem::path& fullPath) : MaterialAsset(fullPath) { } diff --git a/src/Engine/Material/Material.h b/src/Engine/Material/Material.h index 0cc6a03..94e362a 100644 --- a/src/Engine/Material/Material.h +++ b/src/Engine/Material/Material.h @@ -10,12 +10,13 @@ class Material : public MaterialAsset public: Material(); Material(const std::string &directory, const std::string &name); - Material(const std::string &fullPath); + Material(const std::filesystem::path& fullPath); ~Material(); inline std::string getMaterialName() const {return materialName;} private: void compile(); std::string materialName; + Array materialCode; friend class MaterialLoader; }; DEFINE_REF(Material); diff --git a/src/Engine/Material/MaterialAsset.cpp b/src/Engine/Material/MaterialAsset.cpp index 792cb5f..24979b6 100644 --- a/src/Engine/Material/MaterialAsset.cpp +++ b/src/Engine/Material/MaterialAsset.cpp @@ -11,7 +11,7 @@ MaterialAsset::MaterialAsset(const std::string& directory, const std::string& na { } -MaterialAsset::MaterialAsset(const std::string& fullPath) +MaterialAsset::MaterialAsset(const std::filesystem::path& fullPath) : Asset(fullPath) { } diff --git a/src/Engine/Material/MaterialAsset.h b/src/Engine/Material/MaterialAsset.h index ccabd49..452ccbf 100644 --- a/src/Engine/Material/MaterialAsset.h +++ b/src/Engine/Material/MaterialAsset.h @@ -9,7 +9,7 @@ class MaterialAsset : public Asset public: MaterialAsset(); MaterialAsset(const std::string &directory, const std::string &name); - MaterialAsset(const std::string &fullPath); + MaterialAsset(const std::filesystem::path &fullPath); ~MaterialAsset(); protected: //For now its simply the collection of parameters, since there is no point for expressions diff --git a/src/Engine/Material/MaterialInstance.cpp b/src/Engine/Material/MaterialInstance.cpp index 34c2e98..c2b048d 100644 --- a/src/Engine/Material/MaterialInstance.cpp +++ b/src/Engine/Material/MaterialInstance.cpp @@ -17,6 +17,11 @@ MaterialInstance::MaterialInstance(const std::string& fullPath) { } +MaterialInstance::MaterialInstance(const std::filesystem::path& fullPath) + : Asset(fullPath) +{ +} + MaterialInstance::~MaterialInstance() { } diff --git a/src/Engine/Material/MaterialInstance.h b/src/Engine/Material/MaterialInstance.h index 59c71a2..1755686 100644 --- a/src/Engine/Material/MaterialInstance.h +++ b/src/Engine/Material/MaterialInstance.h @@ -11,6 +11,7 @@ public: MaterialInstance(); MaterialInstance(const std::string& directory, const std::string& name); MaterialInstance(const std::string& fullPath); + MaterialInstance(const std::filesystem::path& fullPath); ~MaterialInstance(); PMaterial getBaseMaterial() const; Gfx::PDescriptorSet getDescriptor(); diff --git a/src/Engine/MinimalEngine.cpp b/src/Engine/MinimalEngine.cpp index b028685..2f51672 100644 --- a/src/Engine/MinimalEngine.cpp +++ b/src/Engine/MinimalEngine.cpp @@ -1,3 +1,4 @@ #include "MinimalEngine.h" Seele::Map registeredObjects; +std::mutex registeredObjectsLock; diff --git a/src/Engine/MinimalEngine.h b/src/Engine/MinimalEngine.h index 2187007..4a3efc5 100644 --- a/src/Engine/MinimalEngine.h +++ b/src/Engine/MinimalEngine.h @@ -25,6 +25,7 @@ } extern Seele::Map registeredObjects; +extern std::mutex registeredObjectsLock; namespace Seele { template @@ -34,6 +35,7 @@ public: RefObject(T *ptr) : handle(ptr), refCount(1) { + std::scoped_lock lock(registeredObjectsLock); registeredObjects[ptr] = this; } RefObject(const RefObject &rhs) diff --git a/src/Engine/Scene/Components/PrimitiveComponent.cpp b/src/Engine/Scene/Components/PrimitiveComponent.cpp index 60ff942..7492070 100644 --- a/src/Engine/Scene/Components/PrimitiveComponent.cpp +++ b/src/Engine/Scene/Components/PrimitiveComponent.cpp @@ -1,6 +1,8 @@ #include "PrimitiveComponent.h" #include "Scene/Scene.h" #include "Material/MaterialInstance.h" +#include "Asset/MeshAsset.h" +#include "Graphics/RenderPass/VertexFactory.h" using namespace Seele; @@ -8,6 +10,10 @@ PrimitiveComponent::PrimitiveComponent() { } +PrimitiveComponent::PrimitiveComponent(PMeshAsset asset) +{ +} + PrimitiveComponent::~PrimitiveComponent() { } diff --git a/src/Engine/Scene/Components/PrimitiveComponent.h b/src/Engine/Scene/Components/PrimitiveComponent.h index 62ebffd..b9728c2 100644 --- a/src/Engine/Scene/Components/PrimitiveComponent.h +++ b/src/Engine/Scene/Components/PrimitiveComponent.h @@ -2,24 +2,25 @@ #include "Component.h" #include "Graphics/GraphicsResources.h" #include "Graphics/Mesh.h" -#include "Material/MaterialInstance.h" +#include "Material/MaterialAsset.h" #include "Graphics/MeshBatch.h" namespace Seele { +DECLARE_REF(MeshAsset); class PrimitiveComponent : public Component { public: PrimitiveComponent(); + PrimitiveComponent(PMeshAsset asset); ~PrimitiveComponent(); virtual void notifySceneAttach(PScene scene) override; Matrix4 getRenderMatrix(); private: - Array materials; + Array materials; Gfx::PUniformBuffer uniformBuffer; Array staticMeshes; - PMesh mesh; friend class Scene; }; DEFINE_REF(PrimitiveComponent); diff --git a/src/Engine/Scene/Scene.cpp b/src/Engine/Scene/Scene.cpp index ab0934c..fc33a02 100644 --- a/src/Engine/Scene/Scene.cpp +++ b/src/Engine/Scene/Scene.cpp @@ -32,31 +32,15 @@ void Scene::addPrimitiveComponent(PPrimitiveComponent comp) primitives.add(comp); } -Map Scene::getMeshBatches() +Array Scene::getMeshBatches() { meshBatches.clear(); for (auto primitive : primitives) { - /* Array materials = primitive->materials; - PMaterialInstance matInstance = primitive->; - PMaterial mat = matInstance->getBaseMaterial(); - MeshBatch inst; - inst.instance = primitive->instance; - inst.vertexBuffer = primitive->vertexBuffer; - inst.indexBuffer = primitive->indexBuffer; - inst.modelMatrix = primitive->getRenderMatrix(); - - if (meshBatches.find(mat) != meshBatches.end()) + for(auto batch : primitive->staticMeshes) { - MeshBatchElement &state = meshBatches[mat]; - state.instances.add(inst); + meshBatches.add(batch); } - else - { - MeshBatchElement state; - state.instances.add(inst); - meshBatches[mat] = state; - }*/ } return meshBatches; } \ No newline at end of file diff --git a/src/Engine/Scene/Scene.h b/src/Engine/Scene/Scene.h index 0bea77b..0efc29c 100644 --- a/src/Engine/Scene/Scene.h +++ b/src/Engine/Scene/Scene.h @@ -20,11 +20,11 @@ public: void addPrimitiveComponent(PPrimitiveComponent comp); private: - Map meshBatches; + Array meshBatches; Array rootActors; Array primitives; const static int constant = 10; public: - Map getMeshBatches(); + Array getMeshBatches(); }; } // namespace Seele \ No newline at end of file diff --git a/src/Engine/main.cpp b/src/Engine/main.cpp index ee8f030..f1e25d6 100644 --- a/src/Engine/main.cpp +++ b/src/Engine/main.cpp @@ -5,8 +5,8 @@ int main() { RenderCore core; core.init(); - AssetRegistry::init("./"); - AssetRegistry::importFile("D:\\Private\\Programming\\Unreal Engine\\Assets\\TestAssets\\Unbenannt.fbx"); + AssetRegistry::init("D:\\Private\\Programming\\C++\\TestSeeleProject"); + AssetRegistry::importFile("D:\\Private\\Programming\\Unreal Engine\\Assets\\Arissa\\Arissa.fbx"); core.renderLoop(); core.shutdown(); return 0;