// Copyright (c) 2012-2022 Wojciech Figat. All rights reserved. #pragma once #include "Engine/Scripting/ScriptingObject.h" #include "Engine/Core/Collections/Array.h" class TaskGraph; /// /// System that can generate work into Task Graph for asynchronous execution. /// API_CLASS(Abstract) class FLAXENGINE_API TaskGraphSystem : public ScriptingObject { DECLARE_SCRIPTING_TYPE(TaskGraphSystem); friend TaskGraph; private: Array> _dependencies; public: /// /// The execution order of the system (systems with higher order are executed later, lower first). /// API_FIELD() int32 Order = 0; public: /// /// Adds the dependency on the system execution. Before this system can be executed the given dependant system has to be executed first. /// /// The system to depend on. API_FUNCTION() void AddDependency(TaskGraphSystem* system); /// /// Called before executing any systems of the graph. Can be used to initialize data (synchronous). /// /// The graph executing the system. API_FUNCTION() virtual void PreExecute(TaskGraph* graph); /// /// Executes the system logic and schedules the asynchronous work. /// /// The graph executing the system. API_FUNCTION() virtual void Execute(TaskGraph* graph); /// /// Called after executing all systems of the graph. Can be used to cleanup data (synchronous). /// /// The graph executing the system. API_FUNCTION() virtual void PostExecute(TaskGraph* graph); }; /// /// Graph-based asynchronous tasks scheduler for high-performance computing and processing. /// API_CLASS() class FLAXENGINE_API TaskGraph : public ScriptingObject { DECLARE_SCRIPTING_TYPE(TaskGraph); private: Array> _systems; Array> _remaining; Array> _queue; TaskGraphSystem* _currentSystem = nullptr; int64 _currentLabel = 0; public: /// /// Gets the list of systems. /// API_PROPERTY() const Array>& GetSystems() const; /// /// Adds the system to the graph for the execution. /// /// The system to add. API_FUNCTION() void AddSystem(TaskGraphSystem* system); /// /// Removes the system from the graph. /// /// The system to add. API_FUNCTION() void RemoveSystem(TaskGraphSystem* system); /// /// Schedules the asynchronous systems execution including ordering and dependencies handling. /// API_FUNCTION() void Execute(); /// /// Dispatches the job for the execution. /// /// Call only from system's Execute method to properly schedule job. /// The job. Argument is an index of the job execution. /// The job executions count. API_FUNCTION() void DispatchJob(const Function& job, int32 jobCount = 1); };