From f36d79c1d1ad7cc42fee2e104f6176bd36ab2c54 Mon Sep 17 00:00:00 2001 From: JC Date: Sun, 8 Sep 2024 12:09:50 -0700 Subject: [PATCH] multi-threaded scene loading --- application/lucre/UI/UIControllerIcon.cpp | 4 +- application/lucre/UI/UIControllerIcon.h | 2 +- .../lucre/sceneDescriptions/terrain.json | 2 +- application/lucre/scenes/beachScene.cpp | 6 +- .../lucre/scenes/characterAnimation.cpp | 2 +- application/lucre/scenes/characterAnimation.h | 4 +- application/lucre/scenes/cutScene.cpp | 6 +- application/lucre/scenes/dessertScene.cpp | 6 +- application/lucre/scenes/mainScene.cpp | 2 +- application/lucre/scenes/mainSceneModels.cpp | 2 +- application/lucre/scenes/nightScene.cpp | 6 +- application/lucre/scenes/settingsScene.cpp | 4 +- application/lucre/scenes/splashScene.cpp | 4 +- application/lucre/scenes/terrainScene.cpp | 6 +- engine/platform/Vulkan/VKpool.cpp | 33 +++- engine/platform/Vulkan/VKpool.h | 9 + engine/platform/Vulkan/VKrenderer.cpp | 10 +- engine/platform/Vulkan/VKrenderer.h | 10 +- .../Vulkan/systems/VKcubemapRenderSys.cpp | 2 +- .../Vulkan/systems/VKcubemapRenderSys.h | 2 +- engine/platform/Vulkan/systems/VKgrassSys.cpp | 2 +- engine/platform/Vulkan/systems/VKgrassSys.h | 2 +- engine/platform/Vulkan/systems/VKlightSys.cpp | 4 +- engine/platform/Vulkan/systems/VKlightSys.h | 4 +- engine/platform/Vulkan/systems/VKpbrSASys.cpp | 2 +- engine/platform/Vulkan/systems/VKpbrSASys.h | 2 +- engine/platform/Vulkan/systems/VKpbrSys.cpp | 4 +- engine/platform/Vulkan/systems/VKpbrSys.h | 2 +- .../VKshadowAnimatedRenderSysInstanced.cpp | 2 +- .../VKshadowAnimatedRenderSysInstanced.h | 2 +- .../systems/VKshadowRenderSysInstanced.cpp | 6 +- .../systems/VKshadowRenderSysInstanced.h | 2 +- .../Vulkan/systems/VKspriteRenderSys.cpp | 2 +- .../Vulkan/systems/VKspriteRenderSys.h | 2 +- .../Vulkan/systems/VKspriteRenderSys2D.cpp | 2 +- .../Vulkan/systems/VKspriteRenderSys2D.h | 2 +- engine/renderer/builder/builder.cpp | 4 +- engine/renderer/builder/builder.h | 8 +- engine/renderer/builder/fastgltfBuilder.cpp | 13 +- engine/renderer/builder/fastgltfBuilder.h | 7 +- engine/renderer/builder/fbxBuilder.cpp | 6 +- engine/renderer/builder/fbxBuilder.h | 2 +- engine/renderer/builder/gltfBuilder.cpp | 6 +- engine/renderer/builder/gltfBuilder.h | 2 +- engine/renderer/builder/terrainBuilder.cpp | 6 +- engine/renderer/builder/ufbxBuilder.cpp | 4 +- engine/renderer/builder/ufbxBuilder.h | 2 +- engine/renderer/renderer.h | 10 +- engine/scene/nativeScript.h | 2 +- engine/scene/particleSystem.cpp | 4 +- engine/scene/particleSystem.h | 2 +- engine/scene/registry.cpp | 36 ++++ engine/scene/registry.h | 71 +++++++ engine/scene/scene.cpp | 8 +- engine/scene/scene.h | 5 +- engine/scene/sceneGraph.cpp | 77 +++----- engine/scene/sceneGraph.h | 32 ++-- engine/scene/sceneLoaderDeserializeJSON.cpp | 177 +++++++++++++----- engine/scene/sceneLoaderJSON.h | 12 +- 59 files changed, 441 insertions(+), 219 deletions(-) create mode 100644 engine/scene/registry.cpp create mode 100644 engine/scene/registry.h diff --git a/application/lucre/UI/UIControllerIcon.cpp b/application/lucre/UI/UIControllerIcon.cpp index e66af085..fd52fb23 100644 --- a/application/lucre/UI/UIControllerIcon.cpp +++ b/application/lucre/UI/UIControllerIcon.cpp @@ -231,7 +231,7 @@ namespace LucreApp MeshComponent mesh{"controller icon", model}; { - m_ID1 = m_Registry.create(); + m_ID1 = m_Registry.Create(); m_Registry.emplace(m_ID1, mesh); TransformComponent transform{}; @@ -241,7 +241,7 @@ namespace LucreApp m_Registry.emplace(m_ID1, spriteRendererComponent2D); } { - m_ID2 = m_Registry.create(); + m_ID2 = m_Registry.Create(); m_Registry.emplace(m_ID2, mesh); TransformComponent transform{}; diff --git a/application/lucre/UI/UIControllerIcon.h b/application/lucre/UI/UIControllerIcon.h index df9ac30b..ef1cbf47 100644 --- a/application/lucre/UI/UIControllerIcon.h +++ b/application/lucre/UI/UIControllerIcon.h @@ -47,7 +47,7 @@ namespace LucreApp void Init(); public: - entt::registry m_Registry; + Registry m_Registry; private: void LoadModels(); diff --git a/application/lucre/sceneDescriptions/terrain.json b/application/lucre/sceneDescriptions/terrain.json index 277e79ae..fcbe8afe 100644 --- a/application/lucre/sceneDescriptions/terrain.json +++ b/application/lucre/sceneDescriptions/terrain.json @@ -110,7 +110,7 @@ ], "translation": [ - -3.22584, 5.06019, 41.9383 + 1.0569, 2.24202, -0.363263 ] } } diff --git a/application/lucre/scenes/beachScene.cpp b/application/lucre/scenes/beachScene.cpp index 73decf5e..b9a927be 100644 --- a/application/lucre/scenes/beachScene.cpp +++ b/application/lucre/scenes/beachScene.cpp @@ -61,7 +61,7 @@ namespace LucreApp PerspectiveCameraComponent perspectiveCameraComponent(aspectRatio, yfov, zfar, znear); m_CameraController = std::make_shared(perspectiveCameraComponent); - m_Camera = m_Registry.create(); + m_Camera = m_Registry.Create(); TransformComponent cameraTransform{}; m_Registry.emplace(m_Camera, cameraTransform); uint cameraNode = m_SceneGraph.CreateNode(m_Camera, "defaultCamera", "defaultCamera", m_Dictionary); @@ -140,7 +140,7 @@ namespace LucreApp m_Dictionary.Retrieve("application/lucre/models/external_3D_files/lightBulb/lightBulb.gltf::0::root"); if (m_Lightbulb0 == entt::null) { - m_Lightbulb0 = m_Registry.create(); + m_Lightbulb0 = m_Registry.Create(); TransformComponent transform{}; transform.SetScale({0.00999978, 0.0100001, 0.0100001}); @@ -165,7 +165,7 @@ namespace LucreApp m_Dictionary.Retrieve("application/lucre/models/external_3D_files/lightBulb/lightBulb2.gltf::0::root"); if (m_Lightbulb1 == entt::null) { - m_Lightbulb1 = m_Registry.create(); + m_Lightbulb1 = m_Registry.Create(); TransformComponent transform{}; transform.SetScale({0.00999934, 0.00999997, 0.00999993}); diff --git a/application/lucre/scenes/characterAnimation.cpp b/application/lucre/scenes/characterAnimation.cpp index de20c961..d23f4961 100644 --- a/application/lucre/scenes/characterAnimation.cpp +++ b/application/lucre/scenes/characterAnimation.cpp @@ -27,7 +27,7 @@ namespace LucreApp { - CharacterAnimation::CharacterAnimation(entt::registry& registry, entt::entity gameObject, SkeletalAnimations& animations) + CharacterAnimation::CharacterAnimation(Registry& registry, entt::entity gameObject, SkeletalAnimations& animations) : m_Registry{registry}, m_GameObject{gameObject}, m_Animations{animations}, m_DirToTheRight{false}, m_Transform{glm::mat4(1.0f)}, m_PreviousPositionX{0.0f}, m_MotionState{MotionState::IDLE}, m_FramesPerRotation{FRAMES_PER_ROTATION}, m_FramesToRotate{0}, m_Speed{0.0f}, m_WaitStartWalk{0.0f}, diff --git a/application/lucre/scenes/characterAnimation.h b/application/lucre/scenes/characterAnimation.h index d3df8a33..584afc71 100644 --- a/application/lucre/scenes/characterAnimation.h +++ b/application/lucre/scenes/characterAnimation.h @@ -38,7 +38,7 @@ namespace LucreApp { public: - CharacterAnimation(entt::registry& registry, entt::entity gameObject, SkeletalAnimations& animations); + CharacterAnimation(Registry& registry, entt::entity gameObject, SkeletalAnimations& animations); ~CharacterAnimation() {} void Start(); @@ -82,7 +82,7 @@ namespace LucreApp float ToDegree(float rotation); private: - entt::registry& m_Registry; + Registry& m_Registry; std::unique_ptr m_GamepadInputController; entt::entity m_GameObject; SkeletalAnimations& m_Animations; diff --git a/application/lucre/scenes/cutScene.cpp b/application/lucre/scenes/cutScene.cpp index 5161e092..1b91a971 100644 --- a/application/lucre/scenes/cutScene.cpp +++ b/application/lucre/scenes/cutScene.cpp @@ -62,7 +62,7 @@ namespace LucreApp MeshComponent mesh{"walk animation", model}; mesh.m_Enabled = false; - m_Guybrush[i] = m_Registry.create(); + m_Guybrush[i] = m_Registry.Create(); m_Registry.emplace(m_Guybrush[i], mesh); TransformComponent transform{}; @@ -71,7 +71,7 @@ namespace LucreApp // beach { - m_Beach = m_Registry.create(); + m_Beach = m_Registry.Create(); TransformComponent transform{}; m_Registry.emplace(m_Beach, transform); @@ -81,7 +81,7 @@ namespace LucreApp { for (uint i = 0; i < 2; i++) { - m_Clouds[i] = m_Registry.create(); + m_Clouds[i] = m_Registry.Create(); TransformComponent transform{}; m_Registry.emplace(m_Clouds[i], transform); diff --git a/application/lucre/scenes/dessertScene.cpp b/application/lucre/scenes/dessertScene.cpp index b8afa020..451c20ac 100644 --- a/application/lucre/scenes/dessertScene.cpp +++ b/application/lucre/scenes/dessertScene.cpp @@ -64,7 +64,7 @@ namespace LucreApp PerspectiveCameraComponent perspectiveCameraComponent(aspectRatio, yfov, zfar, znear); m_CameraControllers[CameraTypes::DefaultCamera] = std::make_shared(perspectiveCameraComponent); m_CameraControllers[CameraTypes::DefaultCamera]->GetCamera().SetName("default camera"); - m_Camera[CameraTypes::DefaultCamera] = m_Registry.create(); + m_Camera[CameraTypes::DefaultCamera] = m_Registry.Create(); TransformComponent cameraTransform{}; m_Registry.emplace(m_Camera[CameraTypes::DefaultCamera], cameraTransform); uint cameraNode = m_SceneGraph.CreateNode(m_Camera[CameraTypes::DefaultCamera], "defaultCamera", "defaultCamera", @@ -293,7 +293,7 @@ namespace LucreApp if (m_Lightbulb0 == entt::null) { LOG_APP_INFO("m_Lightbulb0 not found"); - m_Lightbulb0 = m_Registry.create(); + m_Lightbulb0 = m_Registry.Create(); TransformComponent transform{}; transform.SetScale({0.01, 0.01, 0.01}); @@ -313,7 +313,7 @@ namespace LucreApp if (m_Lightbulb1 == entt::null) { LOG_APP_INFO("m_Lightbulb1 not found"); - m_Lightbulb1 = m_Registry.create(); + m_Lightbulb1 = m_Registry.Create(); TransformComponent transform{}; transform.SetScale({0.00999934, 0.00999997, 0.00999993}); diff --git a/application/lucre/scenes/mainScene.cpp b/application/lucre/scenes/mainScene.cpp index 3439354a..b05013fb 100644 --- a/application/lucre/scenes/mainScene.cpp +++ b/application/lucre/scenes/mainScene.cpp @@ -61,7 +61,7 @@ namespace LucreApp PerspectiveCameraComponent perspectiveCameraComponent(aspectRatio, yfov, zfar, znear); m_CameraController = std::make_shared(perspectiveCameraComponent); - m_Camera = m_Registry.create(); + m_Camera = m_Registry.Create(); TransformComponent cameraTransform{}; m_Registry.emplace(m_Camera, cameraTransform); uint cameraNode = m_SceneGraph.CreateNode(m_Camera, "defaultCamera", "defaultCamera", m_Dictionary); diff --git a/application/lucre/scenes/mainSceneModels.cpp b/application/lucre/scenes/mainSceneModels.cpp index 10374991..fcffdd19 100644 --- a/application/lucre/scenes/mainSceneModels.cpp +++ b/application/lucre/scenes/mainSceneModels.cpp @@ -64,7 +64,7 @@ namespace LucreApp MeshComponent mesh{"horn animation", model}; mesh.m_Enabled = false; - m_Guybrush[i] = m_Registry.create(); + m_Guybrush[i] = m_Registry.Create(); m_Registry.emplace(m_Guybrush[i], mesh); TransformComponent transform{}; diff --git a/application/lucre/scenes/nightScene.cpp b/application/lucre/scenes/nightScene.cpp index 79a5af01..33c11da4 100644 --- a/application/lucre/scenes/nightScene.cpp +++ b/application/lucre/scenes/nightScene.cpp @@ -62,7 +62,7 @@ namespace LucreApp PerspectiveCameraComponent perspectiveCameraComponent(aspectRatio, yfov, zfar, znear); m_CameraController = std::make_shared(perspectiveCameraComponent); - m_Camera = m_Registry.create(); + m_Camera = m_Registry.Create(); TransformComponent cameraTransform{}; m_Registry.emplace(m_Camera, cameraTransform); uint cameraNode = m_SceneGraph.CreateNode(m_Camera, "defaultCamera", "defaultCamera", m_Dictionary); @@ -256,7 +256,7 @@ namespace LucreApp if (m_Lightbulb0 == entt::null) { LOG_APP_INFO("m_Lightbulb0 not found"); - m_Lightbulb0 = m_Registry.create(); + m_Lightbulb0 = m_Registry.Create(); TransformComponent transform{}; transform.SetScale({0.00999978, 0.0100001, 0.0100001}); @@ -282,7 +282,7 @@ namespace LucreApp if (m_Lightbulb1 == entt::null) { LOG_APP_INFO("m_Lightbulb1 not found"); - m_Lightbulb1 = m_Registry.create(); + m_Lightbulb1 = m_Registry.Create(); TransformComponent transform{}; transform.SetScale({0.00999934, 0.00999997, 0.00999993}); diff --git a/application/lucre/scenes/settingsScene.cpp b/application/lucre/scenes/settingsScene.cpp index 1ac4255d..d8b31a46 100644 --- a/application/lucre/scenes/settingsScene.cpp +++ b/application/lucre/scenes/settingsScene.cpp @@ -56,7 +56,7 @@ namespace LucreApp m_BarrelTranslationSpeed[i] = 250.0f + (50.0f * EngineCore::RandomPlusMinusOne()); m_BarrelRotationSpeed[i] = 2.0f + (0.1f * EngineCore::RandomPlusMinusOne()); - m_Barrel[i] = m_Registry.create(); + m_Barrel[i] = m_Registry.Create(); m_Registry.emplace(m_Barrel[i], mesh); TransformComponent transform = TransformComponent(barrelSprite.GetMat4()); @@ -77,7 +77,7 @@ namespace LucreApp MeshComponent mesh{"background", model}; mesh.m_Enabled = true; - m_BackGround = m_Registry.create(); + m_BackGround = m_Registry.Create(); m_Registry.emplace(m_BackGround, mesh); TransformComponent transform = TransformComponent(backGroundSprite.GetMat4()); diff --git a/application/lucre/scenes/splashScene.cpp b/application/lucre/scenes/splashScene.cpp index 8e1aabfb..b2961a66 100644 --- a/application/lucre/scenes/splashScene.cpp +++ b/application/lucre/scenes/splashScene.cpp @@ -63,7 +63,7 @@ namespace LucreApp MeshComponent mesh{"walk animation", model}; mesh.m_Enabled = false; - m_Guybrush[i] = m_Registry.create(); + m_Guybrush[i] = m_Registry.Create(); m_Registry.emplace(m_Guybrush[i], mesh); TransformComponent transform{}; @@ -83,7 +83,7 @@ namespace LucreApp MeshComponent mesh{"logo", model}; mesh.m_Enabled = true; - m_Logo = m_Registry.create(); + m_Logo = m_Registry.Create(); m_Registry.emplace(m_Logo, mesh); TransformComponent transform{}; diff --git a/application/lucre/scenes/terrainScene.cpp b/application/lucre/scenes/terrainScene.cpp index c09e6411..79bf6c9b 100644 --- a/application/lucre/scenes/terrainScene.cpp +++ b/application/lucre/scenes/terrainScene.cpp @@ -61,7 +61,7 @@ namespace LucreApp PerspectiveCameraComponent perspectiveCameraComponent(aspectRatio, yfov, zfar, znear); m_CameraController = std::make_shared(perspectiveCameraComponent); - m_Camera = m_Registry.create(); + m_Camera = m_Registry.Create(); TransformComponent cameraTransform{}; m_Registry.emplace(m_Camera, cameraTransform); uint cameraNode = m_SceneGraph.CreateNode(m_Camera, "defaultCamera", "defaultCamera", m_Dictionary); @@ -179,7 +179,7 @@ namespace LucreApp if (m_Lightbulb0 == entt::null) { LOG_APP_INFO("m_Lightbulb0 not found"); - m_Lightbulb0 = m_Registry.create(); + m_Lightbulb0 = m_Registry.Create(); TransformComponent transform{}; transform.SetScale({0.01, 0.01, 0.01}); @@ -199,7 +199,7 @@ namespace LucreApp if (m_Lightbulb1 == entt::null) { LOG_APP_INFO("m_Lightbulb1 not found"); - m_Lightbulb1 = m_Registry.create(); + m_Lightbulb1 = m_Registry.Create(); TransformComponent transform{}; transform.SetScale({0.00999934, 0.00999997, 0.00999993}); diff --git a/engine/platform/Vulkan/VKpool.cpp b/engine/platform/Vulkan/VKpool.cpp index 46ef141e..885ded9e 100644 --- a/engine/platform/Vulkan/VKpool.cpp +++ b/engine/platform/Vulkan/VKpool.cpp @@ -49,7 +49,7 @@ namespace GfxRenderEngine auto createDescriptorPool = [this, device]() { - static constexpr uint POOL_SIZE = 500; + static constexpr uint POOL_SIZE = 5000; std::unique_ptr descriptorPool = VK_DescriptorPool::Builder(device) .SetMaxSets(VK_SwapChain::MAX_FRAMES_IN_FLIGHT * POOL_SIZE) @@ -114,9 +114,9 @@ namespace GfxRenderEngine CORE_ASSERT(pool != nullptr, "no command pool found!"); if (!pool) { + std::cout << "terminating because of thread id:" << threadID << "\n"; exit(1); } - return pool; } @@ -127,9 +127,38 @@ namespace GfxRenderEngine CORE_ASSERT(m_DescriptorPools[hash] != nullptr, "no command pool found!"); if (!m_DescriptorPools[hash]) { + std::cout << "thread id:" << threadID << "\n"; exit(1); } return *m_DescriptorPools[hash]; } + void VK_Pool::ResetCommandPool() + { + VkCommandPoolResetFlags flags{VK_COMMAND_POOL_RESET_RELEASE_RESOURCES_BIT}; + vkResetCommandPool(m_Device, GetCommandPool(), flags); + } + + void VK_Pool::ResetCommandPools(ThreadPool& threadpool) + { + VkCommandPoolResetFlags flags{VK_COMMAND_POOL_RESET_RELEASE_RESOURCES_BIT}; + for (auto& threadID : threadpool.GetThreadIDs()) + { + std::cout << "VK_Pool::ResetAllCommandPools: " << "):" << threadID << "\n"; + uint64 hash = std::hash()(threadID); + vkResetCommandPool(m_Device, m_CommandPools[hash], flags); + } + } + + void VK_Pool::ResetDescriptorPool() { GetDescriptorPool().ResetPool(); } + + void VK_Pool::ResetDescriptorPools(ThreadPool& threadpool) + { + for (auto& threadID : threadpool.GetThreadIDs()) + { + std::cout << "VK_Pool::ResetAllCommandPools: " << "):" << threadID << "\n"; + uint64 hash = std::hash()(threadID); + m_DescriptorPools[hash]->ResetPool(); + } + } } // namespace GfxRenderEngine diff --git a/engine/platform/Vulkan/VKpool.h b/engine/platform/Vulkan/VKpool.h index 4d8d5700..8045d87d 100644 --- a/engine/platform/Vulkan/VKpool.h +++ b/engine/platform/Vulkan/VKpool.h @@ -41,6 +41,12 @@ namespace GfxRenderEngine VkCommandPool& GetCommandPool(); VK_DescriptorPool& GetDescriptorPool(); + void ResetCommandPool(); + void ResetCommandPools(ThreadPool& threadpool); + + void ResetDescriptorPool(); + void ResetDescriptorPools(ThreadPool& threadpool); + // Not copyable or movable VK_Pool(const VK_Pool&) = delete; VK_Pool& operator=(const VK_Pool&) = delete; @@ -52,7 +58,10 @@ namespace GfxRenderEngine QueueFamilyIndices& m_QueueFamilyIndices; ThreadPool& m_PoolPrimary; ThreadPool& m_PoolSecondary; + + // pool of command pools std::unordered_map m_CommandPools; + // pool of descriptor pools std::unordered_map> m_DescriptorPools; }; } // namespace GfxRenderEngine diff --git a/engine/platform/Vulkan/VKrenderer.cpp b/engine/platform/Vulkan/VKrenderer.cpp index 905ab232..ac2dd268 100644 --- a/engine/platform/Vulkan/VKrenderer.cpp +++ b/engine/platform/Vulkan/VKrenderer.cpp @@ -611,7 +611,7 @@ namespace GfxRenderEngine vkCmdSetScissor(commandBuffer, 0, 1, &scissor); } - void VK_Renderer::SubmitShadows(entt::registry& registry, + void VK_Renderer::SubmitShadows(Registry& registry, const std::vector& directionalLights) { // this function supports one directional light @@ -785,7 +785,7 @@ namespace GfxRenderEngine } } - void VK_Renderer::Renderpass3D(entt::registry& registry) + void VK_Renderer::Renderpass3D(Registry& registry) { if (m_CurrentCommandBuffer) { @@ -844,7 +844,7 @@ namespace GfxRenderEngine } } - void VK_Renderer::TransparencyPass(entt::registry& registry, ParticleSystem* particleSystem) + void VK_Renderer::TransparencyPass(Registry& registry, ParticleSystem* particleSystem) { if (m_CurrentCommandBuffer) { @@ -889,7 +889,7 @@ namespace GfxRenderEngine } } - void VK_Renderer::Submit2D(Camera* camera, entt::registry& registry) + void VK_Renderer::Submit2D(Camera* camera, Registry& registry) { if (m_CurrentCommandBuffer) { @@ -917,7 +917,7 @@ namespace GfxRenderEngine return m_CurrentFrameIndex; } - void VK_Renderer::UpdateAnimations(entt::registry& registry, const Timestep& timestep) + void VK_Renderer::UpdateAnimations(Registry& registry, const Timestep& timestep) { auto view = registry.view(); for (auto entity : view) diff --git a/engine/platform/Vulkan/VKrenderer.h b/engine/platform/Vulkan/VKrenderer.h index e242cd4b..395e38da 100644 --- a/engine/platform/Vulkan/VKrenderer.h +++ b/engine/platform/Vulkan/VKrenderer.h @@ -86,15 +86,15 @@ namespace GfxRenderEngine virtual bool Init() override; virtual void BeginFrame(Camera* camera) override; - virtual void Renderpass3D(entt::registry& registry) override; - virtual void SubmitShadows(entt::registry& registry, + virtual void Renderpass3D(Registry& registry) override; + virtual void SubmitShadows(Registry& registry, const std::vector& directionalLights = {}) override; virtual void Submit(Scene& scene) override; virtual void NextSubpass() override; virtual void LightingPass() override; virtual void PostProcessingRenderpass() override; - virtual void TransparencyPass(entt::registry& registry, ParticleSystem* particleSystem) override; - virtual void Submit2D(Camera* camera, entt::registry& registry) override; + virtual void TransparencyPass(Registry& registry, ParticleSystem* particleSystem) override; + virtual void Submit2D(Camera* camera, Registry& registry) override; virtual void GUIRenderpass(Camera* camera) override; virtual void EndScene() override; virtual uint GetFrameCounter() override { return m_FrameCounter; } @@ -108,7 +108,7 @@ namespace GfxRenderEngine const float textureID = 1.0f) override; virtual void ShowDebugShadowMap(bool showDebugShadowMap) override { m_ShowDebugShadowMap = showDebugShadowMap; } - virtual void UpdateAnimations(entt::registry& registry, const Timestep& timestep) override; + virtual void UpdateAnimations(Registry& registry, const Timestep& timestep) override; void ToggleDebugWindow(const GenericCallback& callback = nullptr) { m_Imgui = Imgui::ToggleDebugWindow(callback); } diff --git a/engine/platform/Vulkan/systems/VKcubemapRenderSys.cpp b/engine/platform/Vulkan/systems/VKcubemapRenderSys.cpp index ad3f2c1c..cfe45923 100644 --- a/engine/platform/Vulkan/systems/VKcubemapRenderSys.cpp +++ b/engine/platform/Vulkan/systems/VKcubemapRenderSys.cpp @@ -79,7 +79,7 @@ namespace GfxRenderEngine pipelineConfig); } - void VK_RenderSystemCubemap::RenderEntities(const VK_FrameInfo& frameInfo, entt::registry& registry) + void VK_RenderSystemCubemap::RenderEntities(const VK_FrameInfo& frameInfo, Registry& registry) { m_Pipeline->Bind(frameInfo.m_CommandBuffer); diff --git a/engine/platform/Vulkan/systems/VKcubemapRenderSys.h b/engine/platform/Vulkan/systems/VKcubemapRenderSys.h index eb221687..42a20f92 100644 --- a/engine/platform/Vulkan/systems/VKcubemapRenderSys.h +++ b/engine/platform/Vulkan/systems/VKcubemapRenderSys.h @@ -47,7 +47,7 @@ namespace GfxRenderEngine VK_RenderSystemCubemap(const VK_RenderSystemCubemap&) = delete; VK_RenderSystemCubemap& operator=(const VK_RenderSystemCubemap&) = delete; - void RenderEntities(const VK_FrameInfo& frameInfo, entt::registry& registry); + void RenderEntities(const VK_FrameInfo& frameInfo, Registry& registry); private: void CreatePipelineLayout(std::vector& descriptorSetLayouts); diff --git a/engine/platform/Vulkan/systems/VKgrassSys.cpp b/engine/platform/Vulkan/systems/VKgrassSys.cpp index e48c61cf..a4d67b32 100644 --- a/engine/platform/Vulkan/systems/VKgrassSys.cpp +++ b/engine/platform/Vulkan/systems/VKgrassSys.cpp @@ -95,7 +95,7 @@ namespace GfxRenderEngine pipelineConfig); } - void VK_RenderSystemGrass::RenderEntities(const VK_FrameInfo& frameInfo, entt::registry& registry) + void VK_RenderSystemGrass::RenderEntities(const VK_FrameInfo& frameInfo, Registry& registry) { m_Pipeline->Bind(frameInfo.m_CommandBuffer); diff --git a/engine/platform/Vulkan/systems/VKgrassSys.h b/engine/platform/Vulkan/systems/VKgrassSys.h index f7cd6a0a..c98d7215 100644 --- a/engine/platform/Vulkan/systems/VKgrassSys.h +++ b/engine/platform/Vulkan/systems/VKgrassSys.h @@ -49,7 +49,7 @@ namespace GfxRenderEngine VK_RenderSystemGrass(const VK_RenderSystemGrass&) = delete; VK_RenderSystemGrass& operator=(const VK_RenderSystemGrass&) = delete; - void RenderEntities(const VK_FrameInfo& frameInfo, entt::registry& registry); + void RenderEntities(const VK_FrameInfo& frameInfo, Registry& registry); private: void CreatePipelineLayout(std::vector& descriptorSetLayouts); diff --git a/engine/platform/Vulkan/systems/VKlightSys.cpp b/engine/platform/Vulkan/systems/VKlightSys.cpp index 6ae18152..a274fdf2 100644 --- a/engine/platform/Vulkan/systems/VKlightSys.cpp +++ b/engine/platform/Vulkan/systems/VKlightSys.cpp @@ -88,7 +88,7 @@ namespace GfxRenderEngine pipelineConfig); } - void VK_LightSystem::Render(const VK_FrameInfo& frameInfo, entt::registry& registry) + void VK_LightSystem::Render(const VK_FrameInfo& frameInfo, Registry& registry) { vkCmdBindDescriptorSets(frameInfo.m_CommandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, m_PipelineLayout, 0, 1, &frameInfo.m_GlobalDescriptorSet, 0, nullptr); @@ -116,7 +116,7 @@ namespace GfxRenderEngine } } - void VK_LightSystem::Update(const VK_FrameInfo& frameInfo, GlobalUniformBuffer& ubo, entt::registry& registry) + void VK_LightSystem::Update(const VK_FrameInfo& frameInfo, GlobalUniformBuffer& ubo, Registry& registry) { PROFILE_SCOPE("VK_LightSystem::Update"); { diff --git a/engine/platform/Vulkan/systems/VKlightSys.h b/engine/platform/Vulkan/systems/VKlightSys.h index 3a3a8547..675543fc 100644 --- a/engine/platform/Vulkan/systems/VKlightSys.h +++ b/engine/platform/Vulkan/systems/VKlightSys.h @@ -49,8 +49,8 @@ namespace GfxRenderEngine VK_LightSystem(const VK_LightSystem&) = delete; VK_LightSystem& operator=(const VK_LightSystem&) = delete; - void Update(const VK_FrameInfo& frameInfo, GlobalUniformBuffer& ubo, entt::registry& registry); - void Render(const VK_FrameInfo& frameInfo, entt::registry& registry); + void Update(const VK_FrameInfo& frameInfo, GlobalUniformBuffer& ubo, Registry& registry); + void Render(const VK_FrameInfo& frameInfo, Registry& registry); private: void CreatePipelineLayout(VkDescriptorSetLayout globalDescriptorSetLayout); diff --git a/engine/platform/Vulkan/systems/VKpbrSASys.cpp b/engine/platform/Vulkan/systems/VKpbrSASys.cpp index 77159dfa..27def66b 100644 --- a/engine/platform/Vulkan/systems/VKpbrSASys.cpp +++ b/engine/platform/Vulkan/systems/VKpbrSASys.cpp @@ -94,7 +94,7 @@ namespace GfxRenderEngine pipelineConfig); } - void VK_RenderSystemPbrSA::RenderEntities(const VK_FrameInfo& frameInfo, entt::registry& registry) + void VK_RenderSystemPbrSA::RenderEntities(const VK_FrameInfo& frameInfo, Registry& registry) { m_Pipeline->Bind(frameInfo.m_CommandBuffer); diff --git a/engine/platform/Vulkan/systems/VKpbrSASys.h b/engine/platform/Vulkan/systems/VKpbrSASys.h index ca1ae3db..9ce9dd2d 100644 --- a/engine/platform/Vulkan/systems/VKpbrSASys.h +++ b/engine/platform/Vulkan/systems/VKpbrSASys.h @@ -48,7 +48,7 @@ namespace GfxRenderEngine VK_RenderSystemPbrSA(const VK_RenderSystemPbrSA&) = delete; VK_RenderSystemPbrSA& operator=(const VK_RenderSystemPbrSA&) = delete; - void RenderEntities(const VK_FrameInfo& frameInfo, entt::registry& registry); + void RenderEntities(const VK_FrameInfo& frameInfo, Registry& registry); private: void CreatePipelineLayout(std::vector& descriptorSetLayouts); diff --git a/engine/platform/Vulkan/systems/VKpbrSys.cpp b/engine/platform/Vulkan/systems/VKpbrSys.cpp index 61dca819..06d08ae2 100644 --- a/engine/platform/Vulkan/systems/VKpbrSys.cpp +++ b/engine/platform/Vulkan/systems/VKpbrSys.cpp @@ -91,11 +91,11 @@ namespace GfxRenderEngine std::make_unique(VK_Core::m_Device, "bin-int/pbr.vert.spv", "bin-int/pbr.frag.spv", pipelineConfig); } - void VK_RenderSystemPbr::RenderEntities(const VK_FrameInfo& frameInfo, entt::registry& registry) + void VK_RenderSystemPbr::RenderEntities(const VK_FrameInfo& frameInfo, Registry& registry) { m_Pipeline->Bind(frameInfo.m_CommandBuffer); - auto view = registry.view( + auto view = registry.Get().view( entt::exclude); for (auto mainInstance : view) { diff --git a/engine/platform/Vulkan/systems/VKpbrSys.h b/engine/platform/Vulkan/systems/VKpbrSys.h index 976f4dd1..f2cf4330 100644 --- a/engine/platform/Vulkan/systems/VKpbrSys.h +++ b/engine/platform/Vulkan/systems/VKpbrSys.h @@ -48,7 +48,7 @@ namespace GfxRenderEngine VK_RenderSystemPbr(const VK_RenderSystemPbr&) = delete; VK_RenderSystemPbr& operator=(const VK_RenderSystemPbr&) = delete; - void RenderEntities(const VK_FrameInfo& frameInfo, entt::registry& registry); + void RenderEntities(const VK_FrameInfo& frameInfo, Registry& registry); private: void CreatePipelineLayout(std::vector& descriptorSetLayouts); diff --git a/engine/platform/Vulkan/systems/VKshadowAnimatedRenderSysInstanced.cpp b/engine/platform/Vulkan/systems/VKshadowAnimatedRenderSysInstanced.cpp index e3f8358f..dfda5a9c 100644 --- a/engine/platform/Vulkan/systems/VKshadowAnimatedRenderSysInstanced.cpp +++ b/engine/platform/Vulkan/systems/VKshadowAnimatedRenderSysInstanced.cpp @@ -81,7 +81,7 @@ namespace GfxRenderEngine "bin-int/shadowShaderAnimatedInstanced.frag.spv", pipelineConfig); } - void VK_RenderSystemShadowAnimatedInstanced::RenderEntities(const VK_FrameInfo& frameInfo, entt::registry& registry, + void VK_RenderSystemShadowAnimatedInstanced::RenderEntities(const VK_FrameInfo& frameInfo, Registry& registry, DirectionalLightComponent* directionalLight, int renderpass, const VkDescriptorSet& shadowDescriptorSet) { diff --git a/engine/platform/Vulkan/systems/VKshadowAnimatedRenderSysInstanced.h b/engine/platform/Vulkan/systems/VKshadowAnimatedRenderSysInstanced.h index ef766e03..9ba83bd9 100644 --- a/engine/platform/Vulkan/systems/VKshadowAnimatedRenderSysInstanced.h +++ b/engine/platform/Vulkan/systems/VKshadowAnimatedRenderSysInstanced.h @@ -49,7 +49,7 @@ namespace GfxRenderEngine VK_RenderSystemShadowAnimatedInstanced(const VK_RenderSystemShadowAnimatedInstanced&) = delete; VK_RenderSystemShadowAnimatedInstanced& operator=(const VK_RenderSystemShadowAnimatedInstanced&) = delete; - void RenderEntities(const VK_FrameInfo& frameInfo, entt::registry& registry, + void RenderEntities(const VK_FrameInfo& frameInfo, Registry& registry, DirectionalLightComponent* directionalLight, int renderpass, const VkDescriptorSet& shadowDescriptorSet); diff --git a/engine/platform/Vulkan/systems/VKshadowRenderSysInstanced.cpp b/engine/platform/Vulkan/systems/VKshadowRenderSysInstanced.cpp index 1acc0b4d..cbbc2888 100644 --- a/engine/platform/Vulkan/systems/VKshadowRenderSysInstanced.cpp +++ b/engine/platform/Vulkan/systems/VKshadowRenderSysInstanced.cpp @@ -77,7 +77,7 @@ namespace GfxRenderEngine "bin-int/shadowShaderInstanced.frag.spv", pipelineConfig); } - void VK_RenderSystemShadowInstanced::RenderEntities(const VK_FrameInfo& frameInfo, entt::registry& registry, + void VK_RenderSystemShadowInstanced::RenderEntities(const VK_FrameInfo& frameInfo, Registry& registry, DirectionalLightComponent* directionalLight, int renderpass, const VkDescriptorSet& shadowDescriptorSet) { @@ -91,8 +91,8 @@ namespace GfxRenderEngine m_Pipeline1->Bind(frameInfo.m_CommandBuffer); } - auto meshView = - registry.view(entt::exclude); + auto meshView = registry.Get().view( + entt::exclude); for (auto entity : meshView) { auto& mesh = meshView.get(entity); diff --git a/engine/platform/Vulkan/systems/VKshadowRenderSysInstanced.h b/engine/platform/Vulkan/systems/VKshadowRenderSysInstanced.h index ec7ec400..b3c3f047 100644 --- a/engine/platform/Vulkan/systems/VKshadowRenderSysInstanced.h +++ b/engine/platform/Vulkan/systems/VKshadowRenderSysInstanced.h @@ -49,7 +49,7 @@ namespace GfxRenderEngine VK_RenderSystemShadowInstanced(const VK_RenderSystemShadowInstanced&) = delete; VK_RenderSystemShadowInstanced& operator=(const VK_RenderSystemShadowInstanced&) = delete; - void RenderEntities(const VK_FrameInfo& frameInfo, entt::registry& registry, + void RenderEntities(const VK_FrameInfo& frameInfo, Registry& registry, DirectionalLightComponent* directionalLight, int renderpass, const VkDescriptorSet& shadowDescriptorSet); diff --git a/engine/platform/Vulkan/systems/VKspriteRenderSys.cpp b/engine/platform/Vulkan/systems/VKspriteRenderSys.cpp index a9bd024b..f53b12af 100644 --- a/engine/platform/Vulkan/systems/VKspriteRenderSys.cpp +++ b/engine/platform/Vulkan/systems/VKspriteRenderSys.cpp @@ -78,7 +78,7 @@ namespace GfxRenderEngine "bin-int/spriteRenderer.frag.spv", pipelineConfig); } - void VK_RenderSystemSpriteRenderer::RenderEntities(const VK_FrameInfo& frameInfo, entt::registry& registry) + void VK_RenderSystemSpriteRenderer::RenderEntities(const VK_FrameInfo& frameInfo, Registry& registry) { vkCmdBindDescriptorSets(frameInfo.m_CommandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, m_PipelineLayout, 0, 1, &frameInfo.m_GlobalDescriptorSet, 0, nullptr); diff --git a/engine/platform/Vulkan/systems/VKspriteRenderSys.h b/engine/platform/Vulkan/systems/VKspriteRenderSys.h index d1614424..91bc4a91 100644 --- a/engine/platform/Vulkan/systems/VKspriteRenderSys.h +++ b/engine/platform/Vulkan/systems/VKspriteRenderSys.h @@ -49,7 +49,7 @@ namespace GfxRenderEngine VK_RenderSystemSpriteRenderer(const VK_RenderSystemSpriteRenderer&) = delete; VK_RenderSystemSpriteRenderer& operator=(const VK_RenderSystemSpriteRenderer&) = delete; - void RenderEntities(const VK_FrameInfo& frameInfo, entt::registry& registry); + void RenderEntities(const VK_FrameInfo& frameInfo, Registry& registry); void DrawParticles(const VK_FrameInfo& frameInfo, ParticleSystem* particleSystem); private: diff --git a/engine/platform/Vulkan/systems/VKspriteRenderSys2D.cpp b/engine/platform/Vulkan/systems/VKspriteRenderSys2D.cpp index 81b3ffbc..7bc141b5 100644 --- a/engine/platform/Vulkan/systems/VKspriteRenderSys2D.cpp +++ b/engine/platform/Vulkan/systems/VKspriteRenderSys2D.cpp @@ -79,7 +79,7 @@ namespace GfxRenderEngine "bin-int/spriteRenderer2D.frag.spv", pipelineConfig); } - void VK_RenderSystemSpriteRenderer2D::RenderEntities(const VK_FrameInfo& frameInfo, entt::registry& registry, + void VK_RenderSystemSpriteRenderer2D::RenderEntities(const VK_FrameInfo& frameInfo, Registry& registry, Camera* camera) { vkCmdBindDescriptorSets(frameInfo.m_CommandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, m_PipelineLayout, 0, 1, diff --git a/engine/platform/Vulkan/systems/VKspriteRenderSys2D.h b/engine/platform/Vulkan/systems/VKspriteRenderSys2D.h index 8e9ab634..614631b6 100644 --- a/engine/platform/Vulkan/systems/VKspriteRenderSys2D.h +++ b/engine/platform/Vulkan/systems/VKspriteRenderSys2D.h @@ -53,7 +53,7 @@ namespace GfxRenderEngine VK_RenderSystemSpriteRenderer2D(const VK_RenderSystemSpriteRenderer2D&) = delete; VK_RenderSystemSpriteRenderer2D& operator=(const VK_RenderSystemSpriteRenderer2D&) = delete; - void RenderEntities(const VK_FrameInfo& frameInfo, entt::registry& registry, Camera* camera); + void RenderEntities(const VK_FrameInfo& frameInfo, Registry& registry, Camera* camera); private: void CreatePipelineLayout(VkDescriptorSetLayout globalDescriptorSetLayout); diff --git a/engine/renderer/builder/builder.cpp b/engine/renderer/builder/builder.cpp index 0a8eabad..790b08bb 100644 --- a/engine/renderer/builder/builder.cpp +++ b/engine/renderer/builder/builder.cpp @@ -161,7 +161,7 @@ namespace GfxRenderEngine m_Indices.push_back(3); } - entt::entity Builder::LoadCubemap(std::vector const& faces, entt::registry& registry) + entt::entity Builder::LoadCubemap(std::vector const& faces, Registry& registry) { entt::entity entity; static constexpr uint VERTEX_COUNT = 36; @@ -234,7 +234,7 @@ namespace GfxRenderEngine // create game object { auto model = Engine::m_Engine->LoadModel(*this); - entity = registry.create(); + entity = registry.Create(); MeshComponent mesh{"cubemap", model}; registry.emplace(entity, mesh); TransformComponent transform{}; diff --git a/engine/renderer/builder/builder.h b/engine/renderer/builder/builder.h index a1c9dfd0..51dd28a7 100644 --- a/engine/renderer/builder/builder.h +++ b/engine/renderer/builder/builder.h @@ -22,14 +22,16 @@ #pragma once +#include "scene/registry.h" + namespace GfxRenderEngine { class Cubemap; class Model; class Scene; - class Submesh; - class Vertex; + struct Submesh; + struct Vertex; class Builder { @@ -40,7 +42,7 @@ namespace GfxRenderEngine void LoadParticle(glm::vec4 const& color); void LoadSprite(Sprite const& sprite, float const amplification = 0.0f, int const unlit = 0, glm::vec4 const& color = glm::vec4(1.0f)); - entt::entity LoadCubemap(std::vector const& faces, entt::registry& registry); + entt::entity LoadCubemap(std::vector const& faces, Registry& registry); private: void CalculateTangents(); diff --git a/engine/renderer/builder/fastgltfBuilder.cpp b/engine/renderer/builder/fastgltfBuilder.cpp index 5adf99ec..f6037b81 100644 --- a/engine/renderer/builder/fastgltfBuilder.cpp +++ b/engine/renderer/builder/fastgltfBuilder.cpp @@ -71,7 +71,12 @@ namespace GfxRenderEngine fastgltf::GltfDataBuffer dataBuffer; fastgltf::Parser parser(extensions); - + if (!EngineCore::FileExists(m_Filepath)) + { + LOG_CORE_CRITICAL("FastgltfBuilder::Load NOT FOUND {0}", m_Filepath); + std::cout << m_Filepath << "\n"; + exit(1); + } // load raw data of the file (can be gltf or glb) dataBuffer.loadFromFile(path); @@ -128,7 +133,7 @@ namespace GfxRenderEngine for (m_InstanceIndex = 0; m_InstanceIndex < m_InstanceCount; ++m_InstanceIndex) { // create group game object(s) for all instances to apply transform from JSON file to - auto entity = m_Registry.create(); + auto entity = m_Registry.Create(); std::string name = EngineCore::GetFilenameWithoutPathAndExtension(m_Filepath); auto shortName = name + "::" + std::to_string(m_InstanceIndex) + "::root"; @@ -213,7 +218,7 @@ namespace GfxRenderEngine else // one or more children have a mesh, but not this one --> create group node { // create game object and transform component - auto entity = m_Registry.create(); + auto entity = m_Registry.Create(); // create scene graph node and add to parent auto shortName = "::" + std::to_string(m_InstanceIndex) + "::" + std::string(scene.name) + "::" + nodeName; @@ -245,7 +250,7 @@ namespace GfxRenderEngine int lightIndex = node.lightIndex.has_value() ? node.lightIndex.value() : Gltf::GLTF_NOT_USED; int cameraIndex = node.cameraIndex.has_value() ? node.cameraIndex.value() : Gltf::GLTF_NOT_USED; - auto entity = m_Registry.create(); + auto entity = m_Registry.Create(); auto baseName = "::" + std::to_string(m_InstanceIndex) + "::" + std::string(scene.name) + "::" + nodeName; auto shortName = EngineCore::GetFilenameWithoutPathAndExtension(m_Filepath) + baseName; auto longName = m_Filepath + baseName; diff --git a/engine/renderer/builder/fastgltfBuilder.h b/engine/renderer/builder/fastgltfBuilder.h index be714a62..3792de96 100644 --- a/engine/renderer/builder/fastgltfBuilder.h +++ b/engine/renderer/builder/fastgltfBuilder.h @@ -30,6 +30,7 @@ #include "scene/gltf.h" #include "scene/material.h" +#include "scene/registry.h" #include "renderer/resourceDescriptor.h" namespace GfxRenderEngine @@ -46,10 +47,10 @@ namespace GfxRenderEngine class Scene; class SceneGraph; class SkeletalAnimations; - class Submesh; + struct Submesh; class Texture; class TransformComponent; - class Vertex; + struct Vertex; class FastgltfBuilder { @@ -131,7 +132,7 @@ namespace GfxRenderEngine Resources::ResourceBuffers m_ResourceBuffersPre; uint m_RenderObject; - entt::registry& m_Registry; + Registry& m_Registry; SceneGraph& m_SceneGraph; Dictionary& m_Dictionary; diff --git a/engine/renderer/builder/fbxBuilder.cpp b/engine/renderer/builder/fbxBuilder.cpp index 20e1f1c3..0a78019d 100644 --- a/engine/renderer/builder/fbxBuilder.cpp +++ b/engine/renderer/builder/fbxBuilder.cpp @@ -83,7 +83,7 @@ namespace GfxRenderEngine for (m_InstanceIndex = 0; m_InstanceIndex < m_InstanceCount; ++m_InstanceIndex) { // create group game object(s) for all instances to apply transform from JSON file to - auto entity = m_Registry.create(); + auto entity = m_Registry.Create(); std::string name = EngineCore::GetFilenameWithoutPathAndExtension(m_Filepath); auto shortName = name + "::" + std::to_string(m_InstanceIndex) + "::root"; @@ -153,7 +153,7 @@ namespace GfxRenderEngine else // one or more children have a mesh, but not this one --> create group node { // create game object and transform component - auto entity = m_Registry.create(); + auto entity = m_Registry.Create(); { TransformComponent transform(LoadTransformationMatrix(fbxNodePtr)); if (fbxNodePtr->mParent == m_FbxScene->mRootNode) @@ -186,7 +186,7 @@ namespace GfxRenderEngine { std::string nodeName = std::string(fbxNodePtr->mName.C_Str()); - auto entity = m_Registry.create(); + auto entity = m_Registry.Create(); auto shortName = EngineCore::GetFilenameWithoutPathAndExtension(m_Filepath) + "::" + std::to_string(m_InstanceIndex) + "::" + nodeName; auto longName = m_Filepath + "::" + std::to_string(m_InstanceIndex) + "::" + nodeName; diff --git a/engine/renderer/builder/fbxBuilder.h b/engine/renderer/builder/fbxBuilder.h index 132bd4ec..09f81c49 100644 --- a/engine/renderer/builder/fbxBuilder.h +++ b/engine/renderer/builder/fbxBuilder.h @@ -84,7 +84,7 @@ namespace GfxRenderEngine std::vector m_HasMesh; entt::entity m_GameObject; - entt::registry& m_Registry; + Registry& m_Registry; SceneGraph& m_SceneGraph; Dictionary& m_Dictionary; diff --git a/engine/renderer/builder/gltfBuilder.cpp b/engine/renderer/builder/gltfBuilder.cpp index a9301b24..c4ad4b59 100644 --- a/engine/renderer/builder/gltfBuilder.cpp +++ b/engine/renderer/builder/gltfBuilder.cpp @@ -122,7 +122,7 @@ namespace GfxRenderEngine for (m_InstanceIndex = 0; m_InstanceIndex < m_InstanceCount; ++m_InstanceIndex) { // create group game object(s) for all instances to apply transform from JSON file to - auto entity = m_Registry.create(); + auto entity = m_Registry.Create(); std::string name = EngineCore::GetFilenameWithoutPathAndExtension(m_Filepath); auto shortName = name + "::" + std::to_string(m_InstanceIndex) + "::root"; @@ -205,7 +205,7 @@ namespace GfxRenderEngine else // one or more children have a mesh, but not this one --> create group node { // create game object and transform component - auto entity = m_Registry.create(); + auto entity = m_Registry.Create(); // create scene graph node and add to parent auto shortName = "::" + std::to_string(m_InstanceIndex) + "::" + scene.name + "::" + nodeName; @@ -235,7 +235,7 @@ namespace GfxRenderEngine auto& nodeName = node.name; uint meshIndex = node.mesh; - auto entity = m_Registry.create(); + auto entity = m_Registry.Create(); auto shortName = EngineCore::GetFilenameWithoutPathAndExtension(m_Filepath) + "::" + std::to_string(m_InstanceIndex) + "::" + scene.name + "::" + nodeName; auto longName = m_Filepath + "::" + std::to_string(m_InstanceIndex) + "::" + scene.name + "::" + nodeName; diff --git a/engine/renderer/builder/gltfBuilder.h b/engine/renderer/builder/gltfBuilder.h index 22ba3a7d..1924b88f 100644 --- a/engine/renderer/builder/gltfBuilder.h +++ b/engine/renderer/builder/gltfBuilder.h @@ -99,7 +99,7 @@ namespace GfxRenderEngine std::shared_ptr m_InstanceBuffer; uint m_RenderObject; - entt::registry& m_Registry; + Registry& m_Registry; SceneGraph& m_SceneGraph; Dictionary& m_Dictionary; diff --git a/engine/renderer/builder/terrainBuilder.cpp b/engine/renderer/builder/terrainBuilder.cpp index 95e493ab..88b13557 100644 --- a/engine/renderer/builder/terrainBuilder.cpp +++ b/engine/renderer/builder/terrainBuilder.cpp @@ -34,6 +34,10 @@ namespace GfxRenderEngine { void TerrainBuilder::ColorTerrain(Terrain::TerrainSpec const& terrainSpec, Image const& heightMap) { + if (!EngineCore::FileExists(terrainSpec.m_FilepathColorMap)) + { + return; + } Image colorMap{terrainSpec.m_FilepathColorMap}; if (!colorMap.IsValid()) @@ -224,7 +228,7 @@ namespace GfxRenderEngine for (int instanceIndex = 0; instanceIndex < instanceCount; ++instanceIndex) { // create game object - entt::entity entity = registry.create(); + entt::entity entity = registry.Create(); std::shared_ptr model; TransformComponent transform{}; instanceTag.m_Instances.push_back(entity); diff --git a/engine/renderer/builder/ufbxBuilder.cpp b/engine/renderer/builder/ufbxBuilder.cpp index fa052ee4..ecfd9114 100644 --- a/engine/renderer/builder/ufbxBuilder.cpp +++ b/engine/renderer/builder/ufbxBuilder.cpp @@ -151,7 +151,7 @@ namespace GfxRenderEngine else // one or more children have a mesh, but not this one --> create group node { // create game object and transform component - auto entity = m_Registry.create(); + auto entity = m_Registry.Create(); { glm::vec3 scale; glm::quat rotation; @@ -198,7 +198,7 @@ namespace GfxRenderEngine { std::string nodeName(fbxNodePtr->name.data); - auto entity = m_Registry.create(); + auto entity = m_Registry.Create(); auto shortName = EngineCore::GetFilenameWithoutPathAndExtension(m_Filepath) + "::" + std::to_string(m_InstanceIndex) + "::" + nodeName; auto longName = m_Filepath + "::" + std::to_string(m_InstanceIndex) + "::" + nodeName; diff --git a/engine/renderer/builder/ufbxBuilder.h b/engine/renderer/builder/ufbxBuilder.h index ccc751ae..1bcc881e 100644 --- a/engine/renderer/builder/ufbxBuilder.h +++ b/engine/renderer/builder/ufbxBuilder.h @@ -85,7 +85,7 @@ namespace GfxRenderEngine std::vector m_HasMesh; entt::entity m_GameObject; - entt::registry& m_Registry; + Registry& m_Registry; SceneGraph& m_SceneGraph; Dictionary& m_Dictionary; diff --git a/engine/renderer/renderer.h b/engine/renderer/renderer.h index 723151f9..097f6536 100644 --- a/engine/renderer/renderer.h +++ b/engine/renderer/renderer.h @@ -42,18 +42,18 @@ namespace GfxRenderEngine virtual bool Init() =0; - virtual void SubmitShadows(entt::registry& registry, const std::vector& directionalLights = {}) = 0; + virtual void SubmitShadows(Registry& registry, const std::vector& directionalLights = {}) = 0; virtual void Submit(Scene& scene) = 0; virtual void NextSubpass() = 0; virtual void LightingPass() = 0; virtual void PostProcessingRenderpass() = 0; - virtual void TransparencyPass(entt::registry& registry, ParticleSystem* particleSystem = nullptr) = 0; - virtual void Submit2D(Camera* camera, entt::registry& registry) = 0; + virtual void TransparencyPass(Registry& registry, ParticleSystem* particleSystem = nullptr) = 0; + virtual void Submit2D(Camera* camera, Registry& registry) = 0; virtual void GUIRenderpass(Camera* camera) = 0; virtual uint GetFrameCounter() = 0; virtual void BeginFrame(Camera* camera) = 0; - virtual void Renderpass3D(entt::registry& registry) = 0; + virtual void Renderpass3D(Registry& registry) = 0; virtual void EndScene() = 0; virtual void DrawWithTransform(const Sprite& sprite, const glm::mat4& transform) = 0; @@ -63,7 +63,7 @@ namespace GfxRenderEngine virtual float GetAmbientLightIntensity() = 0; virtual void ShowDebugShadowMap(bool showDebugShadowMap) = 0; - virtual void UpdateAnimations(entt::registry& registry, const Timestep& timestep) = 0; + virtual void UpdateAnimations(Registry& registry, const Timestep& timestep) = 0; }; } diff --git a/engine/scene/nativeScript.h b/engine/scene/nativeScript.h index d2606b96..46e116a3 100644 --- a/engine/scene/nativeScript.h +++ b/engine/scene/nativeScript.h @@ -47,7 +47,7 @@ namespace GfxRenderEngine Scene* m_Scene; entt::entity m_GameObject; - entt::registry& m_Registry; + Registry& m_Registry; TransformComponent& m_Transform; const glm::vec3& m_Translation; diff --git a/engine/scene/particleSystem.cpp b/engine/scene/particleSystem.cpp index bc87b890..25afcf3d 100644 --- a/engine/scene/particleSystem.cpp +++ b/engine/scene/particleSystem.cpp @@ -43,7 +43,7 @@ namespace GfxRenderEngine MeshComponent mesh{"particle animation", model}; mesh.m_Enabled = false; - m_AnimationSprites[i] = m_Registry.create(); + m_AnimationSprites[i] = m_Registry.Create(); m_Registry.emplace(m_AnimationSprites[i], mesh); } } @@ -77,7 +77,7 @@ namespace GfxRenderEngine m_PoolIndex = (m_PoolIndex + 1) % m_ParticlePool.size(); Builder builder{}; - particle.m_Entity = m_Registry.create(); + particle.m_Entity = m_Registry.Create(); builder.LoadParticle(spec.m_StartColor); auto model = Engine::m_Engine->LoadModel(builder); diff --git a/engine/scene/particleSystem.h b/engine/scene/particleSystem.h index 8e77ff4c..a9198fb4 100644 --- a/engine/scene/particleSystem.h +++ b/engine/scene/particleSystem.h @@ -88,7 +88,7 @@ namespace GfxRenderEngine }; std::vector m_ParticlePool; - entt::registry m_Registry; + Registry m_Registry; private: diff --git a/engine/scene/registry.cpp b/engine/scene/registry.cpp new file mode 100644 index 00000000..3bafb7e0 --- /dev/null +++ b/engine/scene/registry.cpp @@ -0,0 +1,36 @@ +/* Engine Copyright (c) 2024 Engine Development Team + https://github.com/beaumanvienna/vulkan + + Permission is hereby granted, free of charge, to any person + obtaining a copy of this software and associated documentation files + (the "Software"), to deal in the Software without restriction, + including without limitation the rights to use, copy, modify, merge, + publish, distribute, sublicense, and/or sell copies of the Software, + and to permit persons to whom the Software is furnished to do so, + subject to the following conditions: + + The above copyright notice and this permission notice shall be + included in all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ + +#include "scene/registry.h" + +namespace GfxRenderEngine +{ + Registry::Registry() {} + + Registry::~Registry() {} + + [[nodiscard]] entt::entity Registry::Create() + { + std::lock_guard guard(m_Mutex); + return m_Registry.create(); + } +} // namespace GfxRenderEngine diff --git a/engine/scene/registry.h b/engine/scene/registry.h new file mode 100644 index 00000000..af0bb3a7 --- /dev/null +++ b/engine/scene/registry.h @@ -0,0 +1,71 @@ +/* Engine Copyright (c) 2024 Engine Development Team + https://github.com/beaumanvienna/vulkan + + Permission is hereby granted, free of charge, to any person + obtaining a copy of this software and associated documentation files + (the "Software"), to deal in the Software without restriction, + including without limitation the rights to use, copy, modify, merge, + publish, distribute, sublicense, and/or sell copies of the Software, + and to permit persons to whom the Software is furnished to do so, + subject to the following conditions: + + The above copyright notice and this permission notice shall be + included in all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ + +#pragma once + +#include "entt.hpp" + +#include "engine.h" + +namespace GfxRenderEngine +{ + + class Registry + { + + public: + Registry(); + ~Registry(); + entt::registry& Get() { return m_Registry; } + + [[nodiscard]] entt::entity Create(); + + template decltype(auto) emplace(const entt::entity entity, Args&&... args) + { + std::lock_guard guard(m_Mutex); + return m_Registry.emplace(entity, std::forward(args)...); + } + + template [[nodiscard]] decltype(auto) get([[maybe_unused]] const entt::entity entity) + { + std::lock_guard guard(m_Mutex); + return m_Registry.get(entity); + } + + template + [[nodiscard]] auto view(entt::exclude_t = {}) + { + std::lock_guard guard(m_Mutex); + return m_Registry.view(); + } + + template [[nodiscard]] bool all_of(const entt::entity entity) + { + std::lock_guard guard(m_Mutex); + return m_Registry.all_of(entity); + } + + private: + std::mutex m_Mutex; + entt::registry m_Registry; + }; +} // namespace GfxRenderEngine diff --git a/engine/scene/scene.cpp b/engine/scene/scene.cpp index 28b76dc2..d3d8fe2d 100644 --- a/engine/scene/scene.cpp +++ b/engine/scene/scene.cpp @@ -40,7 +40,7 @@ namespace GfxRenderEngine { { m_Name = EngineCore::GetFilenameWithoutExtension(filepath); - auto entity = m_Registry.create(); + auto entity = m_Registry.Create(); // The root node gets a transform so that each and every node // has a transform, however, it should never be used TransformComponent transform{}; @@ -50,7 +50,7 @@ namespace GfxRenderEngine } { // create lights group - auto entity = m_Registry.create(); + auto entity = m_Registry.Create(); TransformComponent lightGroupTransform{}; m_Registry.emplace(entity, lightGroupTransform); @@ -66,7 +66,7 @@ namespace GfxRenderEngine entt::entity Scene::CreatePointLight(const float intensity, const float radius, const glm::vec3& color) { - entt::entity pointLight = m_Registry.create(); + entt::entity pointLight = m_Registry.Create(); // transform TransformComponent lightTransform{}; @@ -88,7 +88,7 @@ namespace GfxRenderEngine entt::entity Scene::CreateDirectionalLight(const float intensity, const glm::vec3& color) { - entt::entity directionlLight = m_Registry.create(); + entt::entity directionlLight = m_Registry.Create(); DirectionalLightComponent directionlLightComponent{intensity, color}; m_Registry.emplace(directionlLight, directionlLightComponent); diff --git a/engine/scene/scene.h b/engine/scene/scene.h index 9283b561..36713ece 100644 --- a/engine/scene/scene.h +++ b/engine/scene/scene.h @@ -28,6 +28,7 @@ #include "engine.h" #include "entt.hpp" #include "events/event.h" +#include "scene/registry.h" #include "scene/sceneGraph.h" #include "scene/dictionary.h" #include "auxiliary/timestep.h" @@ -65,7 +66,7 @@ namespace GfxRenderEngine bool IsFinished() const { return !m_IsRunning; } void SetRunning() { m_IsRunning = true; } - entt::registry& GetRegistry() { return m_Registry; }; + Registry& GetRegistry() { return m_Registry; }; Dictionary& GetDictionary() { return m_Dictionary; }; SceneGraph& GetSceneGraph() { return m_SceneGraph; } TreeNode* GetTreeNode(entt::entity entity) { return &m_SceneGraph.GetNodeByGameObject(entity); } @@ -76,7 +77,7 @@ namespace GfxRenderEngine std::string m_Name; std::string m_Filepath; std::string m_AlternativeFilepath; - entt::registry m_Registry; + Registry m_Registry; Dictionary m_Dictionary; SceneGraph m_SceneGraph; bool m_IsRunning; diff --git a/engine/scene/sceneGraph.cpp b/engine/scene/sceneGraph.cpp index c3b74c0c..d5968b46 100644 --- a/engine/scene/sceneGraph.cpp +++ b/engine/scene/sceneGraph.cpp @@ -1,4 +1,4 @@ -/* Engine Copyright (c) 2022 Engine Development Team +/* Engine Copyright (c) 2024 Engine Development Team https://github.com/beaumanvienna/vulkan Permission is hereby granted, free of charge, to any person @@ -12,68 +12,54 @@ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS - OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. - IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY - CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, - TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include "scene/sceneGraph.h" namespace GfxRenderEngine { - TreeNode::TreeNode(entt::entity gameObject, - const std::string& name, - const std::string& longName) - : m_GameObject(gameObject), - m_LongName(longName), - m_Name(name) - {} - - TreeNode::~TreeNode() - {} - - entt::entity TreeNode::GetGameObject() const + TreeNode::TreeNode(entt::entity gameObject, const std::string& name, const std::string& longName) + : m_GameObject(gameObject), m_LongName(longName), m_Name(name) { - return m_GameObject; } - const std::string& TreeNode::GetName() const + TreeNode::TreeNode(GfxRenderEngine::TreeNode const& other) + : m_GameObject(other.m_GameObject), m_LongName(other.m_LongName), m_Name(other.m_Name), m_Children(other.m_Children) { - return m_Name; } - const std::string& TreeNode::GetLongName() const - { - return m_LongName; - } + TreeNode::~TreeNode() {} - uint TreeNode::Children() const - { - return m_Children.size(); - } + entt::entity TreeNode::GetGameObject() const { return m_GameObject; } - uint TreeNode::GetChild(uint const childIndex) - { - return m_Children[childIndex]; - } + const std::string& TreeNode::GetName() const { return m_Name; } + + const std::string& TreeNode::GetLongName() const { return m_LongName; } + + uint TreeNode::Children() const { return m_Children.size(); } + + uint TreeNode::GetChild(uint const childIndex) { return m_Children[childIndex]; } uint TreeNode::AddChild(uint const nodeIndex) { + std::lock_guard guard(m_Mutex); uint childIndex = m_Children.size(); m_Children.push_back(nodeIndex); return childIndex; } - void TreeNode::SetGameObject(entt::entity gameObject) - { - m_GameObject = gameObject; - } + void TreeNode::SetGameObject(entt::entity gameObject) { m_GameObject = gameObject; } - uint SceneGraph::CreateNode(entt::entity const gameObject, std::string const& name, std::string const& longName, Dictionary& dictionary) + uint SceneGraph::CreateNode(entt::entity const gameObject, std::string const& name, std::string const& longName, + Dictionary& dictionary) { + std::lock_guard guard(m_Mutex); uint nodeIndex = m_Nodes.size(); m_Nodes.push_back({gameObject, name, longName}); dictionary.InsertShort(name, gameObject); @@ -86,17 +72,15 @@ namespace GfxRenderEngine { std::string indentStr(indent, ' '); TreeNode& treeNode = m_Nodes[nodeIndex]; - LOG_CORE_INFO("{0}game object `{1}`, name: `{2}`", indentStr, static_cast(treeNode.GetGameObject()), treeNode.GetName()); + LOG_CORE_INFO("{0}game object `{1}`, name: `{2}`", indentStr, static_cast(treeNode.GetGameObject()), + treeNode.GetName()); for (uint childIndex = 0; childIndex < treeNode.Children(); ++childIndex) { TraverseLog(treeNode.GetChild(childIndex), indent + 4); } } - TreeNode& SceneGraph::GetNode(uint const nodeIndex) - { - return m_Nodes[nodeIndex]; - } + TreeNode& SceneGraph::GetNode(uint const nodeIndex) { return m_Nodes[nodeIndex]; } TreeNode& SceneGraph::GetNodeByGameObject(entt::entity const gameObject) { @@ -106,6 +90,7 @@ namespace GfxRenderEngine TreeNode& SceneGraph::GetRoot() { + std::lock_guard guard(m_Mutex); CORE_ASSERT(m_Nodes.size(), "SceneGraph::GetRoot(): scene graph is empty"); return m_Nodes[SceneGraph::ROOT_NODE]; } @@ -113,11 +98,11 @@ namespace GfxRenderEngine uint SceneGraph::GetTreeNodeIndex(entt::entity const gameObject) { uint returnValue = NODE_INVALID; - + if (m_MapFromGameObjectToNode.find(gameObject) != m_MapFromGameObjectToNode.end()) { returnValue = m_MapFromGameObjectToNode[gameObject]; } return returnValue; } -} +} // namespace GfxRenderEngine diff --git a/engine/scene/sceneGraph.h b/engine/scene/sceneGraph.h index fa8e6e6a..5acf6a02 100644 --- a/engine/scene/sceneGraph.h +++ b/engine/scene/sceneGraph.h @@ -1,4 +1,4 @@ -/* Engine Copyright (c) 2023 Engine Development Team +/* Engine Copyright (c) 2023 Engine Development Team https://github.com/beaumanvienna/vulkan Permission is hereby granted, free of charge, to any person @@ -12,12 +12,12 @@ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS - OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. - IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY - CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, - TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #pragma once @@ -31,13 +31,12 @@ namespace GfxRenderEngine { - class TreeNode { public: - TreeNode(entt::entity gameObject, const std::string& name, const std::string& longName); + TreeNode(GfxRenderEngine::TreeNode const& other); ~TreeNode(); entt::entity GetGameObject() const; @@ -50,25 +49,23 @@ namespace GfxRenderEngine std::vector& GetChildren() { return m_Children; } private: - entt::entity m_GameObject; std::string m_Name; std::string m_LongName; + std::mutex m_Mutex; std::vector m_Children; - }; class SceneGraph { public: - static constexpr uint ROOT_NODE = 0; static constexpr uint NODE_INVALID = -1; public: - - uint CreateNode(entt::entity const gameObject, std::string const& name, std::string const& longName, Dictionary& dictionary); + uint CreateNode(entt::entity const gameObject, std::string const& name, std::string const& longName, + Dictionary& dictionary); TreeNode& GetNode(uint const nodeIndex); TreeNode& GetNodeByGameObject(entt::entity const gameObject); TreeNode& GetRoot(); @@ -77,9 +74,8 @@ namespace GfxRenderEngine void TraverseLog(uint nodeIndex, uint indent = 0); private: - + std::mutex m_Mutex; std::vector m_Nodes; - std::map m_MapFromGameObjectToNode; - + std::map m_MapFromGameObjectToNode; }; -} +} // namespace GfxRenderEngine diff --git a/engine/scene/sceneLoaderDeserializeJSON.cpp b/engine/scene/sceneLoaderDeserializeJSON.cpp index 890f690b..ecd81242 100644 --- a/engine/scene/sceneLoaderDeserializeJSON.cpp +++ b/engine/scene/sceneLoaderDeserializeJSON.cpp @@ -20,8 +20,8 @@ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include "auxiliary/file.h" #include "core.h" +#include "auxiliary/file.h" #include "scene/sceneLoaderJSON.h" #include "scene/terrainLoaderJSON.h" @@ -123,31 +123,114 @@ namespace GfxRenderEngine } } - for (auto gltfFileJSON : gltfFiles) + std::vector gltfInfoVector; + gltfInfoVector.resize(gltfFiles.count_elements()); + { + uint fileCount{0}; + for (auto gltfFileJSON : gltfFiles) + { + ParseGltfFile(gltfFileJSON, false /*tinygltf loader*/, gltfInfoVector[fileCount]); + ++fileCount; + } + } + { - ParseGltfFile(gltfFileJSON, false /*tinygltf loader*/, - m_SceneDescriptionFile.m_GltfFiles.m_GltfFilesFromScene); + std::vector& gltfFilesFromScene = + m_SceneDescriptionFile.m_GltfFiles.m_GltfFilesFromScene; + uint fileCount{0}; + for (auto& gltfInfo : gltfInfoVector) + { + if (!gltfInfo.m_LoadFuture.get()) + { + LOG_CORE_CRITICAL("gltf file did not load properly: {0}", gltfInfo.m_GltfFile.m_Filename); + continue; + } + gltfFilesFromScene.push_back(gltfInfo.m_GltfFile); + + std::vector& gltfFileInstances = gltfFilesFromScene.back().m_Instances; + gltfFileInstances.resize(gltfInfo.m_InstanceCount); + uint instanceIndex = 0; + for (auto& gltfFileInstance : gltfFileInstances) + { + gltfFileInstance = gltfInfo.m_GltfFile.m_Instances[instanceIndex]; + std::string fullEntityName = gltfInfo.m_GltfFile.m_Filename + + std::string("::" + std::to_string(instanceIndex) + "::root"); + entt::entity entity = m_Scene.m_Dictionary.Retrieve(fullEntityName); + + gltfFileInstance.m_Entity = entity; + TransformComponent& transform = m_Scene.m_Registry.get(entity); + transform.SetScale(gltfInfo.m_InstanceTransforms[instanceIndex].GetScale()); + transform.SetRotation(gltfInfo.m_InstanceTransforms[instanceIndex].GetRotation()); + transform.SetTranslation(gltfInfo.m_InstanceTransforms[instanceIndex].GetTranslation()); + ++instanceIndex; + } + ++fileCount; + } } } else if (sceneObjectKey == "fastgltf files") { CORE_ASSERT((sceneObject.value().type() == ondemand::json_type::array), "type must be array"); ondemand::array gltfFiles = sceneObject.value().get_array(); + int gltfFileCount = gltfFiles.count_elements(); + if (gltfFileCount == 1) { - int gltfFileCount = gltfFiles.count_elements(); - if (gltfFileCount == 1) - { - LOG_CORE_INFO("loading 1 gltf file (fastgltf)"); - } - else + LOG_CORE_INFO("loading 1 gltf file (fastgltf)"); + } + else + { + LOG_CORE_INFO("loading {0} gltf files (fastgltf)", gltfFileCount); + } + + std::vector gltfInfoVector; + gltfInfoVector.resize(gltfFileCount); + + { + uint fileCount{0}; + for (auto gltfFileJSON : gltfFiles) { - LOG_CORE_INFO("loading {0} gltf files (fastgltf)", gltfFileCount); + ParseGltfFile(gltfFileJSON, true /*fast*/, gltfInfoVector[fileCount]); + ++fileCount; } } - for (auto gltfFileJSON : gltfFiles) { - ParseGltfFile(gltfFileJSON, true /*fast*/, m_SceneDescriptionFile.m_FastgltfFiles.m_GltfFilesFromScene); + std::vector& gltfFilesFromScene = + m_SceneDescriptionFile.m_FastgltfFiles.m_GltfFilesFromScene; + uint fileCount{0}; + for (auto& gltfInfo : gltfInfoVector) + { + if (!gltfInfo.m_LoadFuture.get()) + { + LOG_CORE_CRITICAL("gltf file did not load properly: {0}", gltfInfo.m_GltfFile.m_Filename); + continue; + } + gltfFilesFromScene.push_back(gltfInfo.m_GltfFile); + + std::vector& gltfFileInstances = gltfFilesFromScene.back().m_Instances; + { + Gltf::GltfFile& file = gltfFilesFromScene.back(); + LOG_CORE_INFO("file.m_Filename: {0}", file.m_Filename); + LOG_CORE_INFO("file.m_Instances.size(): {0}", file.m_Instances.size()); + } + gltfFileInstances.resize(gltfInfo.m_InstanceCount); + uint instanceIndex = 0; + for (auto& gltfFileInstance : gltfFileInstances) + { + gltfFileInstance = gltfInfo.m_GltfFile.m_Instances[instanceIndex]; + std::string fullEntityName = gltfInfo.m_GltfFile.m_Filename + + std::string("::" + std::to_string(instanceIndex) + "::root"); + entt::entity entity = m_Scene.m_Dictionary.Retrieve(fullEntityName); + + gltfFileInstance.m_Entity = entity; + TransformComponent& transform = m_Scene.m_Registry.get(entity); + transform.SetScale(gltfInfo.m_InstanceTransforms[instanceIndex].GetScale()); + transform.SetRotation(gltfInfo.m_InstanceTransforms[instanceIndex].GetRotation()); + transform.SetTranslation(gltfInfo.m_InstanceTransforms[instanceIndex].GetTranslation()); + ++instanceIndex; + } + ++fileCount; + } } } else if (sceneObjectKey == "fbx files") @@ -199,11 +282,9 @@ namespace GfxRenderEngine } } - void SceneLoaderJSON::ParseGltfFile(ondemand::object gltfFileJSON, bool fast, - std::vector& gltfFilesFromScene) + void SceneLoaderJSON::ParseGltfFile(ondemand::object gltfFileJSON, bool fast, SceneLoaderJSON::GltfInfo& gltfInfo) { std::string gltfFilename; - bool loadSuccessful = false; int sceneID = Gltf::GLTF_NOT_USED; bool instanceFieldFound = false; @@ -235,50 +316,44 @@ namespace GfxRenderEngine } else if (gltfFileObjectKey == "instances") { - instanceFieldFound = true; // get array of gltf file instances ondemand::array instances = gltfFileObject.value(); int instanceCount = instances.count_elements(); - - if (fast) - { - FastgltfBuilder builder(gltfFilename, m_Scene); - loadSuccessful = builder.Load(instanceCount, sceneID); - } - else + if (!instanceCount) { - GltfBuilder builder(gltfFilename, m_Scene); - loadSuccessful = builder.Load(instanceCount, sceneID); + LOG_CORE_ERROR("no instances found (json file broken): {0}", gltfFilename); + return; } - if (loadSuccessful) + gltfInfo.m_InstanceCount = instanceCount; + instanceFieldFound = true; + + if (fast) { - Gltf::GltfFile gltfFile(gltfFilename); - gltfFilesFromScene.push_back(gltfFile); + auto loadGltf = [this, gltfFilename, instanceCount, sceneID]() + { + FastgltfBuilder builder(gltfFilename, m_Scene); + return builder.Load(instanceCount, sceneID); + }; + gltfInfo.m_LoadFuture = Engine::m_Engine->m_PoolPrimary.SubmitTask(loadGltf); } else { - LOG_CORE_CRITICAL("gltf file did not load properly: {0}", gltfFilename); - return; - } - - if (!instanceCount) - { - LOG_CORE_ERROR("no instances found (json file broken): {0}", gltfFilename); - return; + auto loadGltf = [this, gltfFilename, instanceCount, sceneID]() + { + GltfBuilder builder(gltfFilename, m_Scene); + return builder.Load(instanceCount, sceneID); + }; + gltfInfo.m_LoadFuture = Engine::m_Engine->m_PoolPrimary.SubmitTask(loadGltf); } - std::vector& gltfFileInstances = gltfFilesFromScene.back().m_Instances; - gltfFileInstances.resize(instanceCount); - + gltfInfo.m_GltfFile = Gltf::GltfFile{gltfFilename}; { uint instanceIndex = 0; + gltfInfo.m_InstanceTransforms.resize(instances.count_elements()); + gltfInfo.m_GltfFile.m_Instances.resize(instances.count_elements()); for (auto instance : instances) { - std::string fullEntityName = - gltfFilename + std::string("::" + std::to_string(instanceIndex) + "::root"); - entt::entity entity = m_Scene.m_Dictionary.Retrieve(fullEntityName); - Gltf::Instance& gltfFileInstance = gltfFileInstances[instanceIndex]; - gltfFileInstance.m_Entity = entity; + Gltf::Instance& gltfFileInstance = gltfInfo.m_GltfFile.m_Instances[instanceIndex]; ondemand::object instanceObjects = instance.value(); for (auto instanceObject : instanceObjects) { @@ -288,7 +363,7 @@ namespace GfxRenderEngine { CORE_ASSERT((instanceObject.value().type() == ondemand::json_type::object), "type must be object"); - ParseTransform(instanceObject.value(), entity); + ParseTransform(instanceObject.value(), gltfInfo.m_InstanceTransforms[instanceIndex]); } else if (instanceObjectKey == "nodes") { @@ -392,7 +467,8 @@ namespace GfxRenderEngine { CORE_ASSERT((instanceObject.value().type() == ondemand::json_type::object), "type must be object"); - ParseTransform(instanceObject.value(), entity); + TransformComponent& transform = m_Scene.m_Registry.get(entity); + ParseTransform(instanceObject.value(), transform); } else { @@ -410,10 +486,9 @@ namespace GfxRenderEngine } } - void SceneLoaderJSON::ParseTransform(ondemand::object transformJSON, entt::entity entity) + void SceneLoaderJSON::ParseTransform(ondemand::object transformJSON, TransformComponent& transform) { // transform - TransformComponent& transform = m_Scene.m_Registry.get(entity); glm::vec3 scale{1.0f}; glm::vec3 rotation{0.0f}; glm::vec3 translation{0.0f}; @@ -441,7 +516,6 @@ namespace GfxRenderEngine LOG_CORE_CRITICAL("unrecognized transform component"); } } - transform.SetScale(scale); transform.SetRotation(rotation); transform.SetTranslation(translation); @@ -617,7 +691,8 @@ namespace GfxRenderEngine { CORE_ASSERT((instanceObject.value().type() == ondemand::json_type::object), "type must be object"); - ParseTransform(instanceObject.value(), entity); + TransformComponent& transform = m_Scene.m_Registry.get(entity); + ParseTransform(instanceObject.value(), transform); } else { diff --git a/engine/scene/sceneLoaderJSON.h b/engine/scene/sceneLoaderJSON.h index 84ff42c0..c06450ad 100644 --- a/engine/scene/sceneLoaderJSON.h +++ b/engine/scene/sceneLoaderJSON.h @@ -66,13 +66,21 @@ namespace GfxRenderEngine Obj::ObjFiles m_ObjFiles; }; + struct GltfInfo + { + std::future m_LoadFuture; + Gltf::GltfFile m_GltfFile; + int m_InstanceCount{0}; + std::vector m_InstanceTransforms; + }; + private: void Deserialize(std::string& filepath); - void ParseGltfFile(ondemand::object gltfFileJSON, bool fast, std::vector& gltfFilesFromScene); + void ParseGltfFile(ondemand::object gltfFileJSON, bool fast, SceneLoaderJSON::GltfInfo& gltfInfo); void ParseFbxFile(ondemand::object fbxFileJSON, bool ufbx); - void ParseTransform(ondemand::object transformJSON, entt::entity entity); + void ParseTransform(ondemand::object transformJSON, TransformComponent& transform); void ParseNodesGltf(ondemand::array nodesJSON, std::string const& gltfFilename, Gltf::Instance& gltfFileInstance, uint instanceIndex); void ParseTerrainDescription(ondemand::object terrainDescription,