Toast Engine
Loading...
Searching...
No Matches
tick_scheduler.hpp
Go to the documentation of this file.
1
8
9#pragma once
10#include "box.hpp"
11#include "node.hpp"
12#include "wave_executor.hpp"
13
14#include <mutex>
15#include <unordered_map>
16#include <variant>
17#include <vector>
18
19namespace toast {
20
21namespace _detail {
33struct NodeCluster {
34 explicit NodeCluster(std::vector<Box<Node>>& nodes) : nodes(std::move(nodes)) { }
35
36 std::vector<Box<Node>> nodes;
37
38 void earlyTick();
39 void tick();
40 void physicsTick();
41 void postPhysics();
42 void lateTick();
43
44 auto hasEarlyTick() -> bool;
45 auto hasTick() -> bool;
46 auto hasPhysicsTick() -> bool;
47 auto hasPostPhysics() -> bool;
48 auto hasLateTick() -> bool;
49};
50
52 using Wave = std::vector<std::variant<Box<Node>, NodeCluster>>;
53 std::vector<Wave> early_tick;
54 std::vector<Wave> tick;
55 std::vector<Wave> physics_tick;
56 std::vector<Wave> post_physics;
57 std::vector<Wave> late_tick;
58};
59
61using Interactions = std::unordered_map<Box<Node>, std::vector<Box<Node>>>;
62
70TOAST_API auto planWave(const TickSchedule::Wave& wave, TickFunctionList func, const Interactions* interactions = nullptr)
71 -> std::vector<WaveJob>;
72}
73
75public:
77 std::unordered_map<Box<Node>, std::vector<Box<Node>>> connections;
78 std::unordered_map<Box<Node>, std::vector<Box<Node>>> inverse_connections;
79 };
80
81 // clang-format off
86 auto registerDependency(Node& from, Node& to) -> bool;
87 auto unregisterDependency(Node& from, Node& to) -> bool;
88 // clang-format on
89
93 auto registerInteraction(Node& first, Node& second) -> bool;
94
98 void compute(const std::vector<Box<Node>>& all_nodes);
99
101 void run() const;
102
107 void runLifecycle(Node& root, TickFunctionList stage) const;
108
110 void runPhaseSerial(const std::vector<_detail::TickSchedule::Wave>& phase, TickFunctionList func, std::string_view name) const;
111
113 void runPhase(const std::vector<_detail::TickSchedule::Wave>& phase, TickFunctionList func, std::string_view name) const;
114
116 mutable std::mutex graph_mutex;
117 DependencyGraph graph;
118 _detail::TickSchedule schedule;
119
122
123private:
125 static void tickItem(const _detail::TickSchedule::Wave::value_type& item, TickFunctionList func);
126
128 static void runWave(
129 const _detail::TickSchedule::Wave& wave, const std::vector<_detail::WaveJob>& jobs, TickFunctionList func, int number
130 );
131
132 struct CachedPlan {
133 TickFunctionList func = TickFunctionList::none;
134 std::vector<_detail::WaveJob> jobs;
135 };
136
137 auto planFor(const _detail::TickSchedule::Wave& wave, TickFunctionList func) const -> const std::vector<_detail::WaveJob>&;
138
139 mutable std::unordered_map<const _detail::TickSchedule::Wave*, CachedPlan> m_plans;
140
142 auto subgraphSeparation(const std::vector<Box<Node>>& all_nodes) -> std::vector<std::vector<Box<Node>>>;
143
145 auto tarjanAlgorithm(const std::vector<std::vector<Box<Node>>>& input_subgraphs) -> std::vector<_detail::TickSchedule::Wave>;
146
148 auto assignWaves(const std::vector<_detail::TickSchedule::Wave>& subgraphs) -> std::vector<_detail::TickSchedule::Wave>;
149
151 auto optimizeWaves(const std::vector<_detail::TickSchedule::Wave>& waves) -> _detail::TickSchedule;
152};
153
154}
pseudo shared pointer for Nodes
Intrusive reference-counted handle to a Node.
Definition box.hpp:35
Definition node.hpp:81
Definition tick_scheduler.hpp:74
void run() const
Runs the four frame phases (early_tick → tick → post_physics → late_tick) of the schedule.
Definition tick_scheduler.cpp:555
void runPhase(const std::vector< _detail::TickSchedule::Wave > &phase, TickFunctionList func, std::string_view name) const
Dispatches a single phase of the tick schedule.
Definition tick_scheduler.cpp:373
auto registerDependency(Node &from, Node &to) -> bool
Records a tick ordering constraint between two nodes.
Definition tick_scheduler.cpp:243
void runPhaseSerial(const std::vector< _detail::TickSchedule::Wave > &phase, TickFunctionList func, std::string_view name) const
Runs a phase wave by wave on the calling thread; for phases that touch thread-bound state like the ph...
Definition tick_scheduler.cpp:535
void runLifecycle(Node &root, TickFunctionList stage) const
Runs a lifecycle stage on root and every node below it, parents before their children.
Definition tick_scheduler.cpp:408
std::mutex graph_mutex
Scripts register dependencies from init().
Definition tick_scheduler.hpp:116
auto registerInteraction(Node &first, Node &second) -> bool
Records that the scripts of two nodes call each other.
Definition tick_scheduler.cpp:263
void compute(const std::vector< Box< Node > > &all_nodes)
Rebuilds the tick schedule from the given node set.
Definition tick_scheduler.cpp:298
_detail::Interactions interactions
The nodes that declared an interaction.
Definition tick_scheduler.hpp:121
Member-robber pattern used by the generated reflection code.
Definition defer.hpp:16
std::unordered_map< Box< Node >, std::vector< Box< Node > > > Interactions
For a node, the nodes whose scripts it calls or is called by, see Node::interactsWith().
Definition tick_scheduler.hpp:61
auto planWave(const TickSchedule::Wave &wave, TickFunctionList func, const Interactions *interactions) -> std::vector< WaveJob >
Splits a wave into the jobs that run it in parallel.
Definition tick_scheduler.cpp:203
Base object type for the scene graph.
TickFunctionList
Bit-flag set describing which lifecycle phases a Node type implements.
Definition reflect_node.hpp:36
Definition tick_scheduler.hpp:76
std::unordered_map< Box< Node >, std::vector< Box< Node > > > connections
forward edges (from → to)
Definition tick_scheduler.hpp:77
std::unordered_map< Box< Node >, std::vector< Box< Node > > > inverse_connections
reverse edges
Definition tick_scheduler.hpp:78
Definition tick_scheduler.hpp:33
Definition tick_scheduler.hpp:51
Runs a plan of jobs on the thread pool, one interpreter per job.