Add system graph (wip)

This commit is contained in:
SirLynix
2022-07-02 19:45:50 +02:00
parent 4d24be2ae9
commit 1b678defae
18 changed files with 524 additions and 286 deletions

View File

@@ -30,5 +30,6 @@
#define NAZARA_CORE_SYSTEMS_HPP
#include <Nazara/Core/Systems/LifetimeSystem.hpp>
#include <Nazara/Core/Systems/SystemGraph.hpp>
#endif // NAZARA_CORE_SYSTEMS_HPP

View File

@@ -0,0 +1,70 @@
// Copyright (C) 2022 Jérôme "Lynix" Leclercq (lynix680@gmail.com)
// This file is part of the "Nazara Engine - Core module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#pragma once
#ifndef NAZARA_CORE_SYSTEMS_SYSTEMGRAPH_HPP
#define NAZARA_CORE_SYSTEMS_SYSTEMGRAPH_HPP
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Core/Clock.hpp>
#include <Nazara/Core/Config.hpp>
#include <Nazara/Utils/MovablePtr.hpp>
#include <entt/entt.hpp>
#include <functional>
#include <unordered_map>
#include <vector>
namespace Nz
{
class NAZARA_CORE_API SystemGraph
{
public:
inline SystemGraph(entt::registry& registry);
SystemGraph(const SystemGraph&) = delete;
SystemGraph(SystemGraph&&) = delete;
~SystemGraph() = default;
template<typename T, typename... Args> T& AddSystem(Args&&... args);
template<typename T> T& GetSystem() const;
void Update();
void Update(float elapsedTime);
SystemGraph& operator=(const SystemGraph&) = delete;
SystemGraph& operator=(SystemGraph&&) = delete;
private:
struct NAZARA_CORE_API NodeBase
{
virtual ~NodeBase();
virtual void Update(float elapsedTime) = 0;
Int64 executionOrder;
};
template<typename T>
struct Node : NodeBase
{
template<typename... Args> Node(Args&&... args);
void Update(float elapsedTime) override;
T system;
};
std::unordered_map<entt::id_type, std::size_t /*nodeIndex*/> m_systemToNodes;
std::vector<NodeBase*> m_orderedNodes;
std::vector<std::unique_ptr<NodeBase>> m_nodes;
entt::registry& m_registry;
Nz::Clock m_clock;
bool m_systemOrderUpdated;
};
}
#include <Nazara/Core/Systems/SystemGraph.inl>
#endif // NAZARA_CORE_SYSTEMS_SYSTEMGRAPH_HPP

View File

@@ -0,0 +1,74 @@
// Copyright (C) 2022 Jérôme "Lynix" Leclercq (lynix680@gmail.com)
// This file is part of the "Nazara Engine - Core module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Core/Systems/SystemGraph.hpp>
#include <Nazara/Core/Error.hpp>
#include <stdexcept>
#include <Nazara/Core/Debug.hpp>
namespace Nz
{
namespace Detail
{
template<typename, typename = void>
struct SystemGraphAllowConcurrent : std::bool_constant<true> {};
template<typename T>
struct SystemGraphAllowConcurrent<T, std::void_t<decltype(T::AllowConcurrent)>> : std::bool_constant<T::AllowConcurrent> {};
template<typename, typename = void>
struct SystemGraphExecutionOrder : std::integral_constant<Int64, 0> {};
template<typename T>
struct SystemGraphExecutionOrder<T, std::void_t<decltype(T::ExecutionOrder)>> : std::integral_constant<Int64, T::ExecutionOrder> {};
}
template<typename T>
template<typename... Args>
SystemGraph::Node<T>::Node(Args&&... args) :
system(std::forward<Args>(args)...)
{
}
template<typename T>
void SystemGraph::Node<T>::Update(float elapsedTime)
{
system.Update(elapsedTime);
}
inline SystemGraph::SystemGraph(entt::registry& registry) :
m_registry(registry),
m_systemOrderUpdated(true)
{
}
template<typename T, typename... Args>
T& SystemGraph::AddSystem(Args&&... args)
{
NazaraAssert(m_systemToNodes.find(entt::type_hash<T>()) == m_systemToNodes.end(), "this system already exists");
auto nodePtr = std::make_unique<Node<T>>(m_registry, std::forward<Args>(args)...);
nodePtr->executionOrder = Detail::SystemGraphExecutionOrder<T>();
T& system = nodePtr->system;
m_nodes.emplace_back(std::move(nodePtr));
m_systemOrderUpdated = false;
return system;
}
template<typename T>
T& SystemGraph::GetSystem() const
{
auto it = m_systemToNodes.find(entt::type_hash<T>());
if (it == m_systemToNodes.end())
throw std::runtime_error("this system is not part of the graph");
auto& node = static_cast<Node<T>&>(*m_nodes[it->second]);
return node.system;
}
}
#include <Nazara/Core/DebugOff.hpp>

View File

@@ -8,11 +8,11 @@
#define NAZARA_GRAPHICS_GRAPHICALMESH_HPP
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Utils/Signal.hpp>
#include <Nazara/Graphics/Config.hpp>
#include <Nazara/Renderer/RenderBuffer.hpp>
#include <Nazara/Utility/Mesh.hpp>
#include <Nazara/Utility/VertexDeclaration.hpp>
#include <Nazara/Utils/Signal.hpp>
#include <memory>
namespace Nz

View File

@@ -24,17 +24,23 @@ namespace Nz
class CommandBufferBuilder;
class FramePipeline;
class RenderFrame;
class RenderWindow;
class UploadPool;
class NAZARA_GRAPHICS_API RenderSystem
{
public:
static constexpr bool AllowConcurrent = false;
static constexpr Int64 ExecutionOrder = 1'000;
RenderSystem(entt::registry& registry);
RenderSystem(const RenderSystem&) = delete;
RenderSystem(RenderSystem&&) = delete;
~RenderSystem();
void Render(entt::registry& registry, RenderFrame& renderFrame);
template<typename T = RenderWindow, typename... Args> T& CreateWindow(Args&&... args);
void Update(float elapsedTime);
RenderSystem& operator=(const RenderSystem&) = delete;
RenderSystem& operator=(RenderSystem&&) = delete;
@@ -44,8 +50,9 @@ namespace Nz
void OnGraphicsDestroy(entt::registry& registry, entt::entity entity);
void OnLightDestroy(entt::registry& registry, entt::entity entity);
void OnNodeDestroy(entt::registry& registry, entt::entity entity);
void UpdateInstances(entt::registry& registry);
void UpdateVisibility(entt::registry& registry);
void UpdateInstances();
void UpdateObservers();
void UpdateVisibility();
struct CameraEntity
{
@@ -89,6 +96,7 @@ namespace Nz
entt::observer m_cameraConstructObserver;
entt::observer m_graphicsConstructObserver;
entt::observer m_lightConstructObserver;
entt::registry& m_registry;
std::set<CameraEntity*> m_invalidatedCameraNode;
std::set<GraphicsEntity*> m_invalidatedGfxWorldNode;
std::set<LightEntity*> m_invalidatedLightWorldNode;
@@ -100,6 +108,7 @@ namespace Nz
std::unordered_set<GraphicsEntity*> m_newlyVisibleGfxEntities;
std::unordered_set<LightEntity*> m_newlyHiddenLightEntities;
std::unordered_set<LightEntity*> m_newlyVisibleLightEntities;
std::vector<std::unique_ptr<RenderWindow>> m_renderWindows;
MemoryPool<CameraEntity> m_cameraEntityPool;
MemoryPool<GraphicsEntity> m_graphicsEntityPool;
MemoryPool<LightEntity> m_lightEntityPool;

View File

@@ -3,10 +3,23 @@
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Graphics/Systems/RenderSystem.hpp>
#include <type_traits>
#include <Nazara/Graphics/Debug.hpp>
namespace Nz
{
template<typename T, typename ...Args>
T& RenderSystem::CreateWindow(Args&& ...args)
{
static_assert(std::is_base_of_v<RenderWindow, T>, "T must inherit RenderWindow");
auto windowPtr = std::make_unique<T>(std::forward<Args>(args)...);
T& windowRef = *windowPtr;
m_renderWindows.emplace_back(std::move(windowPtr));
return windowRef;
}
}
#include <Nazara/Graphics/DebugOff.hpp>

View File

@@ -10,6 +10,7 @@
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Physics2D/PhysWorld2D.hpp>
#include <Nazara/Physics2D/Components/RigidBody2DComponent.hpp>
#include <Nazara/Utils/TypeList.hpp>
#include <entt/entt.hpp>
namespace Nz
@@ -17,6 +18,9 @@ namespace Nz
class NAZARA_PHYSICS2D_API Physics2DSystem
{
public:
static constexpr Int64 ExecutionOrder = 0;
using Components = TypeList<RigidBody2DComponent, class NodeComponent>;
Physics2DSystem(entt::registry& registry);
Physics2DSystem(const Physics2DSystem&) = delete;
Physics2DSystem(Physics2DSystem&&) = delete;
@@ -27,7 +31,7 @@ namespace Nz
inline PhysWorld2D& GetPhysWorld();
inline const PhysWorld2D& GetPhysWorld() const;
void Update(entt::registry& registry, float elapsedTime);
void Update(float elapsedTime);
Physics2DSystem& operator=(const Physics2DSystem&) = delete;
Physics2DSystem& operator=(Physics2DSystem&&) = delete;

View File

@@ -10,6 +10,7 @@
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Physics3D/PhysWorld3D.hpp>
#include <Nazara/Physics3D/Components/RigidBody3DComponent.hpp>
#include <Nazara/Utils/TypeList.hpp>
#include <entt/entt.hpp>
namespace Nz
@@ -17,6 +18,9 @@ namespace Nz
class NAZARA_PHYSICS3D_API Physics3DSystem
{
public:
static constexpr Int64 ExecutionOrder = 0;
using Components = TypeList<RigidBody3DComponent, class NodeComponent>;
Physics3DSystem(entt::registry& registry);
Physics3DSystem(const Physics3DSystem&) = delete;
Physics3DSystem(Physics3DSystem&&) = delete;
@@ -27,7 +31,7 @@ namespace Nz
inline PhysWorld3D& GetPhysWorld();
inline const PhysWorld3D& GetPhysWorld() const;
void Update(entt::registry& registry, float elapsedTime);
void Update(float elapsedTime);
Physics3DSystem& operator=(const Physics3DSystem&) = delete;
Physics3DSystem& operator=(Physics3DSystem&&) = delete;