From b35ca86ae91b2f0bd3212e46dafcca331b1e72fe Mon Sep 17 00:00:00 2001 From: Kah Wei Date: Tue, 1 Nov 2022 00:53:15 +0800 Subject: [PATCH 1/2] BatcherDispatcher should now account for mesh changes properly --- .../src/Graphics/MiddleEnd/Batching/SHBatch.cpp | 11 +++++------ .../src/Graphics/MiddleEnd/Batching/SHSuperBatch.cpp | 6 +++--- .../Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp | 4 ++-- 3 files changed, 10 insertions(+), 11 deletions(-) diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Batching/SHBatch.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/Batching/SHBatch.cpp index 1bda7c90..22d1875b 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Batching/SHBatch.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Batching/SHBatch.cpp @@ -74,11 +74,12 @@ namespace SHADE void SHBatch::Remove(const SHRenderable* renderable) { - // Check if we have a SubBatch with the same mesh yet + // Check if we have a SubBatch with the existing mesh yet (if changed, we use the old mesh) + Handle prevSubBatchMesh = renderable->HasMeshChanged() ? renderable->GetPrevMesh() : renderable->GetMesh(); auto subBatch = std::find_if(subBatches.begin(), subBatches.end(), [&](const SHSubBatch& batch) - { - return batch.Mesh == renderable->GetMesh(); - }); + { + return batch.Mesh == prevSubBatchMesh; + }); // Attempt to remove if it exists if (subBatch == subBatches.end()) @@ -88,9 +89,7 @@ namespace SHADE // Check if other renderables in subBatches contain the same material instance bool matUnused = true; - Handle matToCheck = renderable->HasMaterialChanged() ? renderable->GetPrevMaterial() : renderable->GetMaterial(); - for (const auto& sb : subBatches) { // Check material usage diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Batching/SHSuperBatch.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/Batching/SHSuperBatch.cpp index 434e7163..df389879 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Batching/SHSuperBatch.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Batching/SHSuperBatch.cpp @@ -37,9 +37,9 @@ namespace SHADE // Check if we have a batch with the same pipeline first auto batch = std::find_if(batches.begin(), batches.end(), [&](const SHBatch& batch) - { - return batch.GetPipeline() == PIPELINE; - }); + { + return batch.GetPipeline() == PIPELINE; + }); // Create one if not found diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp index 97380fa3..16e2b1e8 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp @@ -709,8 +709,8 @@ namespace SHADE if (!renderable.HasChanged()) continue; - // Remove from old material's SuperBatch - Handle prevMaterial = renderable.GetPrevMaterial(); + // Remove from the SuperBatch it is previously in (prevMat if mat has changed) + Handle prevMaterial = renderable.HasMaterialChanged() ? renderable.GetPrevMaterial() : renderable.GetMaterial(); if (prevMaterial) { Handle oldSuperBatch = prevMaterial->GetBaseMaterial()->GetPipeline()->GetPipelineState().GetSubpass()->GetSuperBatch(); -- 2.40.1 From e1587c7252c56f44258e4c8ba305064c77c6a8d4 Mon Sep 17 00:00:00 2001 From: Kah Wei Date: Tue, 1 Nov 2022 14:56:03 +0800 Subject: [PATCH 2/2] Fixed SetMesh() not working and loading of mesh and textures --- .../Inspector/SHEditorComponentView.hpp | 1 + .../MiddleEnd/Interface/SHGraphicsSystem.cpp | 17 ++++++-- .../src/Resource/SHResourceManager.cpp | 17 ++++++-- SHADE_Engine/src/Resource/SHResourceManager.h | 39 ++++++------------- .../src/Resource/SHResourceManager.hpp | 4 +- 5 files changed, 44 insertions(+), 34 deletions(-) diff --git a/SHADE_Engine/src/Editor/EditorWindow/Inspector/SHEditorComponentView.hpp b/SHADE_Engine/src/Editor/EditorWindow/Inspector/SHEditorComponentView.hpp index e3f93713..fdc94d1f 100644 --- a/SHADE_Engine/src/Editor/EditorWindow/Inspector/SHEditorComponentView.hpp +++ b/SHADE_Engine/src/Editor/EditorWindow/Inspector/SHEditorComponentView.hpp @@ -358,6 +358,7 @@ namespace SHADE [component](AssetID const& id) { component->SetMesh(SHResourceManager::LoadOrGet(id)); + SHResourceManager::FinaliseChanges(); }, SHDragDrop::DRAG_RESOURCE); } else diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp index 16e2b1e8..007c8639 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp @@ -106,10 +106,7 @@ namespace SHADE descPool = device->CreateDescriptorPools(); // Create generic command buffer - //transferCmdPool = device->CreateCommandPool(SH_QUEUE_FAMILY_ARRAY_INDEX::GRAPHICS, SH_CMD_POOL_RESET::POOL_BASED, true); graphicsCmdPool = device->CreateCommandPool(SH_QUEUE_FAMILY_ARRAY_INDEX::GRAPHICS, SH_CMD_POOL_RESET::POOL_BASED, true); - transferCmdBuffer = graphicsCmdPool->RequestCommandBuffer(SH_CMD_BUFFER_TYPE::PRIMARY); - graphicsTexCmdBuffer = graphicsCmdPool->RequestCommandBuffer(SH_CMD_BUFFER_TYPE::PRIMARY); // Load Built In Shaders static constexpr AssetID VS_DEFAULT = 39210065; defaultVertShader = SHResourceManager::LoadOrGet(VS_DEFAULT); @@ -621,10 +618,14 @@ namespace SHADE void SHGraphicsSystem::BuildMeshBuffers() { + transferCmdBuffer = graphicsCmdPool->RequestCommandBuffer(SH_CMD_BUFFER_TYPE::PRIMARY); + device->WaitIdle(); transferCmdBuffer->BeginRecording(); meshLibrary.BuildBuffers(device, transferCmdBuffer); transferCmdBuffer->EndRecording(); graphicsQueue->SubmitCommandBuffer({ transferCmdBuffer }); + device->WaitIdle(); + transferCmdBuffer.Free(); transferCmdBuffer = {}; } /*---------------------------------------------------------------------------------*/ @@ -649,10 +650,14 @@ namespace SHADE void SHGraphicsSystem::BuildTextures() { + graphicsTexCmdBuffer = graphicsCmdPool->RequestCommandBuffer(SH_CMD_BUFFER_TYPE::PRIMARY); + device->WaitIdle(); texLibrary.BuildTextures ( device, graphicsTexCmdBuffer, graphicsQueue, descPool ); + device->WaitIdle(); + graphicsTexCmdBuffer.Free(); graphicsTexCmdBuffer = {}; } #pragma endregion ADD_REMOVE @@ -692,6 +697,7 @@ namespace SHADE void SHGraphicsSystem::EndRoutine::Execute(double) noexcept { reinterpret_cast(system)->EndRender(); + SHResourceManager::FinaliseChanges(); } /*-----------------------------------------------------------------------------------*/ @@ -709,6 +715,11 @@ namespace SHADE if (!renderable.HasChanged()) continue; + if (!renderable.GetMesh()) + { + SHLOG_CRITICAL("NULL Mesh provided!"); + } + // Remove from the SuperBatch it is previously in (prevMat if mat has changed) Handle prevMaterial = renderable.HasMaterialChanged() ? renderable.GetPrevMaterial() : renderable.GetMaterial(); if (prevMaterial) diff --git a/SHADE_Engine/src/Resource/SHResourceManager.cpp b/SHADE_Engine/src/Resource/SHResourceManager.cpp index 156c31c7..dad9fd9f 100644 --- a/SHADE_Engine/src/Resource/SHResourceManager.cpp +++ b/SHADE_Engine/src/Resource/SHResourceManager.cpp @@ -21,9 +21,11 @@ namespace SHADE /*-----------------------------------------------------------------------------------*/ SHResourceHub SHResourceManager::resourceHub; std::unordered_map>> SHResourceManager::handlesMap; - std::unordered_map SHResourceManager::assetIdMap; + std::unordered_map SHResourceManager::assetIdMap; std::unordered_map> SHResourceManager::typedFreeFuncMap; std::vector SHResourceManager::loadedAssetData; + bool SHResourceManager::textureChanged = false; + bool SHResourceManager::meshChanged = false; /*-----------------------------------------------------------------------------------*/ /* Function Definitions */ @@ -63,8 +65,17 @@ namespace SHADE SHGraphicsSystem* gfxSystem = SHSystemManager::GetSystem(); if (gfxSystem == nullptr) throw std::runtime_error("[SHResourceManager] Attempted to load graphics resource without a SHGraphicsSystem installed."); - gfxSystem->BuildMeshBuffers(); - gfxSystem->BuildTextures(); + + if (meshChanged) + { + gfxSystem->BuildMeshBuffers(); + meshChanged = false; + } + if (textureChanged) + { + gfxSystem->BuildTextures(); + textureChanged = false; + } // Free CPU Resources for (auto assetId : loadedAssetData) diff --git a/SHADE_Engine/src/Resource/SHResourceManager.h b/SHADE_Engine/src/Resource/SHResourceManager.h index 61689420..e5e303e1 100644 --- a/SHADE_Engine/src/Resource/SHResourceManager.h +++ b/SHADE_Engine/src/Resource/SHResourceManager.h @@ -28,33 +28,15 @@ of DigiPen Institute of Technology is prohibited. namespace SHADE { + /// + /// Template structs that maps a resource to their loaded asset representation type. + /// template - struct SHResourceLoader - { - using AssetType = void; - }; - - template<> - struct SHResourceLoader - { - using AssetType = SHMeshAsset; - }; - template<> - struct SHResourceLoader - { - using AssetType = SHTextureAsset; - }; - template<> - struct SHResourceLoader - { - using AssetType = SHShaderAsset; - }; - template<> - struct SHResourceLoader - { - using AssetType = SHMaterialAsset; - }; - + struct SHResourceLoader { using AssetType = void; }; + template<> struct SHResourceLoader { using AssetType = SHMeshAsset; }; + template<> struct SHResourceLoader { using AssetType = SHTextureAsset; }; + template<> struct SHResourceLoader { using AssetType = SHShaderAsset; }; + template<> struct SHResourceLoader { using AssetType = SHMaterialAsset; }; /// /// Static class responsible for loading and caching runtime resources from their @@ -97,7 +79,7 @@ namespace SHADE /// Handle to the resource to unload. static void Unload(AssetID assetId); /// - /// Needs to be called to finalise all changes to loads. + /// Needs to be called to finalise all changes to loads, unless at runtime. /// static void FinaliseChanges(); @@ -147,6 +129,9 @@ namespace SHADE static std::unordered_map> typedFreeFuncMap; // Pointers to temp CPU resources static std::vector loadedAssetData; + // Dirty Flags + static bool meshChanged; + static bool textureChanged; /*---------------------------------------------------------------------------------*/ /* Helper Functions */ diff --git a/SHADE_Engine/src/Resource/SHResourceManager.hpp b/SHADE_Engine/src/Resource/SHResourceManager.hpp index 15834cdf..1623d70a 100644 --- a/SHADE_Engine/src/Resource/SHResourceManager.hpp +++ b/SHADE_Engine/src/Resource/SHResourceManager.hpp @@ -54,7 +54,7 @@ namespace SHADE } auto handle = load(assetId, *assetData); - Handle genericHandle = Handle(); + Handle genericHandle = Handle(handle); typedHandleMap.get().emplace(assetId, genericHandle); typedAssetIdMap.get().emplace(genericHandle, assetId); return handle; @@ -139,6 +139,7 @@ namespace SHADE if constexpr (std::is_same_v) { loadedAssetData.emplace_back(assetId); + meshChanged = true; return gfxSystem->AddMesh ( @@ -155,6 +156,7 @@ namespace SHADE else if constexpr (std::is_same_v) { loadedAssetData.emplace_back(assetId); + textureChanged = true; return gfxSystem->AddTexture ( -- 2.40.1