forked from bevyengine/bevy
-
Notifications
You must be signed in to change notification settings - Fork 1
/
stage.rs
1650 lines (1520 loc) · 64 KB
/
stage.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
use crate::{
self as bevy_ecs,
change_detection::CHECK_TICK_THRESHOLD,
component::ComponentId,
prelude::IntoSystem,
schedule::{
graph_utils::{self, DependencyGraphError},
BoxedRunCriteria, DuplicateLabelStrategy, ExclusiveInsertionPoint, GraphNode,
ParallelExecutor, ParallelSystemExecutor, RunCriteriaContainer, RunCriteriaDescriptor,
RunCriteriaDescriptorOrLabel, RunCriteriaInner, RunCriteriaLabelId, ShouldRun,
SingleThreadedExecutor, SystemContainer, SystemDescriptor, SystemLabelId, SystemSet,
},
world::{World, WorldId},
};
use bevy_ecs_macros::Resource;
use bevy_utils::{tracing::warn, HashMap, HashSet};
use core::fmt::Debug;
use downcast_rs::{impl_downcast, Downcast};
use super::{IntoSystemDescriptor, Schedule};
/// A type that can run as a step of a [`Schedule`](super::Schedule).
pub trait Stage: Downcast + Send + Sync {
/// Runs the stage; this happens once per update.
/// Implementors must initialize all of their state and systems before running the first time.
fn run(&mut self, world: &mut World);
}
impl Debug for dyn Stage {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
if let Some(as_systemstage) = self.as_any().downcast_ref::<SystemStage>() {
write!(f, "{:?}", as_systemstage)
} else if let Some(as_schedule) = self.as_any().downcast_ref::<Schedule>() {
write!(f, "{:?}", as_schedule)
} else {
write!(f, "Unknown dyn Stage")
}
}
}
impl_downcast!(Stage);
/// When this resource is present in the `App`'s `Resources`,
/// each `SystemStage` will log a report containing
/// pairs of systems with ambiguous execution order.
///
/// Systems that access the same Component or Resource within the same stage
/// risk an ambiguous order that could result in logic bugs, unless they have an
/// explicit execution ordering constraint between them.
///
/// This occurs because, in the absence of explicit constraints, systems are executed in
/// an unstable, arbitrary order within each stage that may vary between runs and frames.
///
/// Some ambiguities reported by the ambiguity checker may be warranted (to allow two systems to run
/// without blocking each other) or spurious, as the exact combination of archetypes used may
/// prevent them from ever conflicting during actual gameplay. You can resolve the warnings produced
/// by the ambiguity checker by adding `.before` or `.after` to one of the conflicting systems
/// referencing the other system to force a specific ordering.
///
/// The checker may report a system more times than the amount of constraints it would actually need
/// to have unambiguous order with regards to a group of already-constrained systems.
#[derive(Resource, Default)]
pub struct ReportExecutionOrderAmbiguities;
/// Stores and executes systems. Execution order is not defined unless explicitly specified;
/// see `SystemDescriptor` documentation.
pub struct SystemStage {
/// The WorldId this stage was last run on.
world_id: Option<WorldId>,
/// Instance of a scheduling algorithm for running the systems.
executor: Box<dyn ParallelSystemExecutor>,
/// Determines whether the stage should run.
stage_run_criteria: BoxedRunCriteria,
/// Topologically sorted run criteria of systems.
run_criteria: Vec<RunCriteriaContainer>,
/// Topologically sorted exclusive systems that want to be run at the start of the stage.
pub(super) exclusive_at_start: Vec<SystemContainer>,
/// Topologically sorted exclusive systems that want to be run after parallel systems but
/// before the application of their command buffers.
pub(super) exclusive_before_commands: Vec<SystemContainer>,
/// Topologically sorted exclusive systems that want to be run at the end of the stage.
pub(super) exclusive_at_end: Vec<SystemContainer>,
/// Topologically sorted parallel systems.
pub(super) parallel: Vec<SystemContainer>,
/// Determines if the stage was modified and needs to rebuild its graphs and orders.
pub(super) systems_modified: bool,
/// Determines if the stage's executor was changed.
executor_modified: bool,
/// Newly inserted run criteria that will be initialized at the next opportunity.
uninitialized_run_criteria: Vec<(usize, DuplicateLabelStrategy)>,
/// Newly inserted systems that will be initialized at the next opportunity.
uninitialized_at_start: Vec<usize>,
/// Newly inserted systems that will be initialized at the next opportunity.
uninitialized_before_commands: Vec<usize>,
/// Newly inserted systems that will be initialized at the next opportunity.
uninitialized_at_end: Vec<usize>,
/// Newly inserted systems that will be initialized at the next opportunity.
uninitialized_parallel: Vec<usize>,
/// Saves the value of the World change_tick during the last tick check
last_tick_check: u32,
/// If true, buffers will be automatically applied at the end of the stage. If false, buffers must be manually applied.
apply_buffers: bool,
must_read_resource: Option<ComponentId>,
}
impl SystemStage {
pub fn new(executor: Box<dyn ParallelSystemExecutor>) -> Self {
SystemStage {
world_id: None,
executor,
stage_run_criteria: Default::default(),
run_criteria: vec![],
uninitialized_run_criteria: vec![],
exclusive_at_start: Default::default(),
exclusive_before_commands: Default::default(),
exclusive_at_end: Default::default(),
parallel: vec![],
systems_modified: true,
executor_modified: true,
uninitialized_parallel: vec![],
uninitialized_at_start: vec![],
uninitialized_before_commands: vec![],
uninitialized_at_end: vec![],
last_tick_check: Default::default(),
apply_buffers: true,
must_read_resource: None,
}
}
pub fn single<Params>(system: impl IntoSystemDescriptor<Params>) -> Self {
Self::single_threaded().with_system(system)
}
pub fn single_threaded() -> Self {
Self::new(Box::new(SingleThreadedExecutor::default()))
}
pub fn parallel() -> Self {
Self::new(Box::new(ParallelExecutor::default()))
}
pub fn get_executor<T: ParallelSystemExecutor>(&self) -> Option<&T> {
self.executor.downcast_ref()
}
pub fn get_executor_mut<T: ParallelSystemExecutor>(&mut self) -> Option<&mut T> {
self.executor_modified = true;
self.executor.downcast_mut()
}
pub fn set_executor(&mut self, executor: Box<dyn ParallelSystemExecutor>) {
self.executor_modified = true;
self.executor = executor;
}
pub fn set_must_read_resource(&mut self, resource_id: ComponentId) {
self.must_read_resource = Some(resource_id);
}
#[must_use]
pub fn with_system<Params>(mut self, system: impl IntoSystemDescriptor<Params>) -> Self {
self.add_system(system);
self
}
pub fn add_system<Params>(&mut self, system: impl IntoSystemDescriptor<Params>) -> &mut Self {
self.add_system_inner(system.into_descriptor(), None);
self
}
fn add_system_inner(
&mut self,
mut descriptor: SystemDescriptor,
default_run_criteria: Option<usize>,
) {
self.systems_modified = true;
if let Some(insertion_point) = descriptor.exclusive_insertion_point {
let criteria = descriptor.run_criteria.take();
let mut container = SystemContainer::from_descriptor(descriptor);
match criteria {
Some(RunCriteriaDescriptorOrLabel::Label(label)) => {
container.run_criteria_label = Some(label);
}
Some(RunCriteriaDescriptorOrLabel::Descriptor(criteria_descriptor)) => {
container.run_criteria_label = criteria_descriptor.label;
container.run_criteria_index =
Some(self.add_run_criteria_internal(criteria_descriptor));
}
None => {
container.run_criteria_index = default_run_criteria;
}
}
match insertion_point {
ExclusiveInsertionPoint::AtStart => {
let index = self.exclusive_at_start.len();
self.uninitialized_at_start.push(index);
self.exclusive_at_start.push(container);
}
ExclusiveInsertionPoint::BeforeCommands => {
let index = self.exclusive_before_commands.len();
self.uninitialized_before_commands.push(index);
self.exclusive_before_commands.push(container);
}
ExclusiveInsertionPoint::AtEnd => {
let index = self.exclusive_at_end.len();
self.uninitialized_at_end.push(index);
self.exclusive_at_end.push(container);
}
}
} else {
let criteria = descriptor.run_criteria.take();
let mut container = SystemContainer::from_descriptor(descriptor);
match criteria {
Some(RunCriteriaDescriptorOrLabel::Label(label)) => {
container.run_criteria_label = Some(label);
}
Some(RunCriteriaDescriptorOrLabel::Descriptor(criteria_descriptor)) => {
container.run_criteria_label = criteria_descriptor.label;
container.run_criteria_index =
Some(self.add_run_criteria_internal(criteria_descriptor));
}
None => {
container.run_criteria_index = default_run_criteria;
}
}
self.uninitialized_parallel.push(self.parallel.len());
self.parallel.push(container);
}
}
pub fn apply_buffers(&mut self, world: &mut World) {
for container in &mut self.parallel {
let system = container.system_mut();
#[cfg(feature = "trace")]
let _span = bevy_utils::tracing::info_span!("system_commands", name = &*system.name())
.entered();
system.apply_buffers(world);
}
}
pub fn set_apply_buffers(&mut self, apply_buffers: bool) {
self.apply_buffers = apply_buffers;
}
/// Topologically sorted parallel systems.
///
/// Note that systems won't be fully-formed until the stage has been run at least once.
pub fn parallel_systems(&self) -> &[SystemContainer] {
&self.parallel
}
/// Topologically sorted exclusive systems that want to be run at the start of the stage.
///
/// Note that systems won't be fully-formed until the stage has been run at least once.
pub fn exclusive_at_start_systems(&self) -> &[SystemContainer] {
&self.exclusive_at_start
}
/// Topologically sorted exclusive systems that want to be run at the end of the stage.
///
/// Note that systems won't be fully-formed until the stage has been run at least once.
pub fn exclusive_at_end_systems(&self) -> &[SystemContainer] {
&self.exclusive_at_end
}
/// Topologically sorted exclusive systems that want to be run after parallel systems but
/// before the application of their command buffers.
///
/// Note that systems won't be fully-formed until the stage has been run at least once.
pub fn exclusive_before_commands_systems(&self) -> &[SystemContainer] {
&self.exclusive_before_commands
}
#[must_use]
pub fn with_system_set(mut self, system_set: SystemSet) -> Self {
self.add_system_set(system_set);
self
}
pub fn add_system_set(&mut self, system_set: SystemSet) -> &mut Self {
self.systems_modified = true;
let (run_criteria, mut systems) = system_set.bake();
let set_run_criteria_index = run_criteria.and_then(|criteria| {
// validate that no systems have criteria
for descriptor in &mut systems {
if let Some(name) = descriptor
.run_criteria
.is_some()
.then(|| descriptor.system.name())
{
panic!(
"The system {} has a run criteria, but its `SystemSet` also has a run \
criteria. This is not supported. Consider moving the system into a \
different `SystemSet` or calling `add_system()` instead.",
name
)
}
}
match criteria {
RunCriteriaDescriptorOrLabel::Descriptor(descriptor) => {
Some(self.add_run_criteria_internal(descriptor))
}
RunCriteriaDescriptorOrLabel::Label(label) => {
for system in &mut systems {
system.run_criteria = Some(RunCriteriaDescriptorOrLabel::Label(label));
}
None
}
}
});
for system in systems.drain(..) {
self.add_system_inner(system, set_run_criteria_index);
}
self
}
#[must_use]
pub fn with_run_criteria<Param, S: IntoSystem<(), ShouldRun, Param>>(
mut self,
system: S,
) -> Self {
self.set_run_criteria(system);
self
}
pub fn set_run_criteria<Param, S: IntoSystem<(), ShouldRun, Param>>(
&mut self,
system: S,
) -> &mut Self {
self.stage_run_criteria
.set(Box::new(IntoSystem::into_system(system)));
self
}
#[must_use]
pub fn with_system_run_criteria(mut self, run_criteria: RunCriteriaDescriptor) -> Self {
self.add_system_run_criteria(run_criteria);
self
}
pub fn add_system_run_criteria(&mut self, run_criteria: RunCriteriaDescriptor) -> &mut Self {
self.add_run_criteria_internal(run_criteria);
self
}
pub(crate) fn add_run_criteria_internal(&mut self, descriptor: RunCriteriaDescriptor) -> usize {
let index = self.run_criteria.len();
self.uninitialized_run_criteria
.push((index, descriptor.duplicate_label_strategy));
self.run_criteria
.push(RunCriteriaContainer::from_descriptor(descriptor));
index
}
fn initialize_systems(&mut self, world: &mut World) {
let mut criteria_labels = HashMap::default();
let uninitialized_criteria: HashMap<_, _> =
self.uninitialized_run_criteria.drain(..).collect();
// track the number of filtered criteria to correct run criteria indices
let mut filtered_criteria = 0;
let mut new_indices = Vec::new();
self.run_criteria = self
.run_criteria
.drain(..)
.enumerate()
.filter_map(|(index, mut container)| {
let new_index = index - filtered_criteria;
let label = container.label;
if let Some(strategy) = uninitialized_criteria.get(&index) {
if let Some(ref label) = label {
if let Some(duplicate_index) = criteria_labels.get(label) {
match strategy {
DuplicateLabelStrategy::Panic => panic!(
"Run criteria {} is labelled with {:?}, which \
is already in use. Consider using \
`RunCriteriaDescriptorCoercion::label_discard_if_duplicate().",
container.name(),
container.label
),
DuplicateLabelStrategy::Discard => {
new_indices.push(*duplicate_index);
filtered_criteria += 1;
return None;
}
}
}
}
container.initialize(world);
}
if let Some(label) = label {
criteria_labels.insert(label, new_index);
}
new_indices.push(new_index);
Some(container)
})
.collect();
for index in self.uninitialized_at_start.drain(..) {
let container = &mut self.exclusive_at_start[index];
if let Some(index) = container.run_criteria() {
container.set_run_criteria(new_indices[index]);
}
container.system_mut().initialize(world);
}
for index in self.uninitialized_before_commands.drain(..) {
let container = &mut self.exclusive_before_commands[index];
if let Some(index) = container.run_criteria() {
container.set_run_criteria(new_indices[index]);
}
container.system_mut().initialize(world);
}
for index in self.uninitialized_at_end.drain(..) {
let container = &mut self.exclusive_at_end[index];
if let Some(index) = container.run_criteria() {
container.set_run_criteria(new_indices[index]);
}
container.system_mut().initialize(world);
}
for index in self.uninitialized_parallel.drain(..) {
let container = &mut self.parallel[index];
if let Some(index) = container.run_criteria() {
container.set_run_criteria(new_indices[index]);
}
container.system_mut().initialize(world);
}
}
/// Rearranges all systems in topological orders. Systems must be initialized.
fn rebuild_orders_and_dependencies(&mut self) {
// This assertion exists to document that the number of systems in a stage is limited
// to guarantee that change detection never yields false positives. However, it's possible
// (but still unlikely) to circumvent this by abusing exclusive or chained systems.
assert!(
self.exclusive_at_start.len()
+ self.exclusive_before_commands.len()
+ self.exclusive_at_end.len()
+ self.parallel.len()
< (CHECK_TICK_THRESHOLD as usize)
);
debug_assert!(
self.uninitialized_run_criteria.is_empty()
&& self.uninitialized_parallel.is_empty()
&& self.uninitialized_at_start.is_empty()
&& self.uninitialized_before_commands.is_empty()
&& self.uninitialized_at_end.is_empty()
);
fn unwrap_dependency_cycle_error<Node: GraphNode, Output, Labels: std::fmt::Debug>(
result: Result<Output, DependencyGraphError<Labels>>,
nodes: &[Node],
nodes_description: &'static str,
) -> Output {
match result {
Ok(output) => output,
Err(DependencyGraphError::GraphCycles(cycle)) => {
use std::fmt::Write;
let mut message = format!("Found a dependency cycle in {}:", nodes_description);
writeln!(message).unwrap();
for (index, labels) in &cycle {
writeln!(message, " - {}", nodes[*index].name()).unwrap();
writeln!(
message,
" wants to be after (because of labels: {:?})",
labels,
)
.unwrap();
}
writeln!(message, " - {}", cycle[0].0).unwrap();
panic!("{}", message);
}
}
}
let run_criteria_labels = unwrap_dependency_cycle_error(
self.process_run_criteria(),
&self.run_criteria,
"run criteria",
);
unwrap_dependency_cycle_error(
process_systems(&mut self.parallel, &run_criteria_labels),
&self.parallel,
"parallel systems",
);
unwrap_dependency_cycle_error(
process_systems(&mut self.exclusive_at_start, &run_criteria_labels),
&self.exclusive_at_start,
"exclusive systems at start of stage",
);
unwrap_dependency_cycle_error(
process_systems(&mut self.exclusive_before_commands, &run_criteria_labels),
&self.exclusive_before_commands,
"exclusive systems before commands of stage",
);
unwrap_dependency_cycle_error(
process_systems(&mut self.exclusive_at_end, &run_criteria_labels),
&self.exclusive_at_end,
"exclusive systems at end of stage",
);
}
fn check_uses_resource(&self, resource_id: ComponentId, world: &World) {
debug_assert!(!self.systems_modified);
for system in &self.parallel {
if !system.component_access().has_read(resource_id) {
let component_name = world.components().get_info(resource_id).unwrap().name();
warn!(
"System {} doesn't access resource {component_name}, despite being required to",
system.name()
);
}
}
}
/// All system and component change ticks are scanned once the world counter has incremented
/// at least [`CHECK_TICK_THRESHOLD`](crate::change_detection::CHECK_TICK_THRESHOLD)
/// times since the previous `check_tick` scan.
///
/// During each scan, any change ticks older than [`MAX_CHANGE_AGE`](crate::change_detection::MAX_CHANGE_AGE)
/// are clamped to that age. This prevents false positives from appearing due to overflow.
fn check_change_ticks(&mut self, world: &mut World) {
let change_tick = world.change_tick();
let ticks_since_last_check = change_tick.wrapping_sub(self.last_tick_check);
if ticks_since_last_check >= CHECK_TICK_THRESHOLD {
// Check all system change ticks.
for exclusive_system in &mut self.exclusive_at_start {
exclusive_system.system_mut().check_change_tick(change_tick);
}
for exclusive_system in &mut self.exclusive_before_commands {
exclusive_system.system_mut().check_change_tick(change_tick);
}
for exclusive_system in &mut self.exclusive_at_end {
exclusive_system.system_mut().check_change_tick(change_tick);
}
for parallel_system in &mut self.parallel {
parallel_system.system_mut().check_change_tick(change_tick);
}
// Check all component change ticks.
world.check_change_ticks();
self.last_tick_check = change_tick;
}
}
/// Sorts run criteria and populates resolved input-criteria for piping.
/// Returns a map of run criteria labels to their indices.
fn process_run_criteria(
&mut self,
) -> Result<HashMap<RunCriteriaLabelId, usize>, DependencyGraphError<HashSet<RunCriteriaLabelId>>>
{
let graph = graph_utils::build_dependency_graph(&self.run_criteria);
let order = graph_utils::topological_order(&graph)?;
let mut order_inverted = order.iter().enumerate().collect::<Vec<_>>();
order_inverted.sort_unstable_by_key(|(_, &key)| key);
let labels: HashMap<_, _> = self
.run_criteria
.iter()
.enumerate()
.filter_map(|(index, criteria)| {
criteria
.label
.as_ref()
.map(|&label| (label, order_inverted[index].0))
})
.collect();
for criteria in &mut self.run_criteria {
if let RunCriteriaInner::Piped { input: parent, .. } = &mut criteria.inner {
let label = &criteria.after[0];
*parent = *labels.get(label).unwrap_or_else(|| {
panic!(
"Couldn't find run criteria labelled {:?} to pipe from.",
label
)
});
}
}
fn update_run_criteria_indices(
systems: &mut [SystemContainer],
order_inverted: &[(usize, &usize)],
) {
for system in systems {
if let Some(index) = system.run_criteria() {
system.set_run_criteria(order_inverted[index].0);
}
}
}
update_run_criteria_indices(&mut self.exclusive_at_end, &order_inverted);
update_run_criteria_indices(&mut self.exclusive_at_start, &order_inverted);
update_run_criteria_indices(&mut self.exclusive_before_commands, &order_inverted);
update_run_criteria_indices(&mut self.parallel, &order_inverted);
let mut temp = self.run_criteria.drain(..).map(Some).collect::<Vec<_>>();
for index in order {
self.run_criteria.push(temp[index].take().unwrap());
}
Ok(labels)
}
pub fn vec_system_container_debug(
&self,
name: &str,
v: &Vec<SystemContainer>,
f: &mut std::fmt::Formatter<'_>,
) -> std::fmt::Result {
write!(f, "{}: ", name)?;
if v.len() > 1 {
writeln!(f, "[")?;
for sc in v.iter() {
writeln!(f, "{:?},", sc)?;
}
write!(f, "], ")
} else {
write!(f, "{:?}, ", v)
}
}
}
impl std::fmt::Debug for SystemStage {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "SystemStage: {{ ")?;
write!(
f,
"world_id: {:?}, executor: {:?}, stage_run_criteria: {:?}, run_criteria: {:?}, ",
self.world_id, self.executor, self.stage_run_criteria, self.run_criteria
)?;
self.vec_system_container_debug("exclusive_at_start", &self.exclusive_at_start, f)?;
self.vec_system_container_debug(
"exclusive_before_commands",
&self.exclusive_before_commands,
f,
)?;
self.vec_system_container_debug("exclusive_at_end", &self.exclusive_at_end, f)?;
self.vec_system_container_debug("parallel", &self.parallel, f)?;
write!(
f,
"systems_modified: {:?}, uninitialized_run_criteria: {:?}, ",
self.systems_modified, self.uninitialized_run_criteria
)?;
write!(
f,
"uninitialized_at_start: {:?}, uninitialized_before_commands: {:?}, ",
self.uninitialized_at_start, self.uninitialized_before_commands
)?;
write!(
f,
"uninitialized_at_end: {:?}, uninitialized_parallel: {:?}, ",
self.uninitialized_at_end, self.uninitialized_parallel
)?;
write!(
f,
"last_tick_check: {:?}, apply_buffers: {:?}, ",
self.last_tick_check, self.apply_buffers
)?;
write!(f, "must_read_resource: {:?}}}", self.must_read_resource)
}
}
/// Sorts given system containers topologically, populates their resolved dependencies
/// and run criteria.
fn process_systems(
systems: &mut Vec<SystemContainer>,
run_criteria_labels: &HashMap<RunCriteriaLabelId, usize>,
) -> Result<(), DependencyGraphError<HashSet<SystemLabelId>>> {
let mut graph = graph_utils::build_dependency_graph(systems);
let order = graph_utils::topological_order(&graph)?;
let mut order_inverted = order.iter().enumerate().collect::<Vec<_>>();
order_inverted.sort_unstable_by_key(|(_, &key)| key);
for (index, container) in systems.iter_mut().enumerate() {
if let Some(index) = container.run_criteria_label().map(|label| {
*run_criteria_labels
.get(label)
.unwrap_or_else(|| panic!("No run criteria with label {:?} found.", label))
}) {
container.set_run_criteria(index);
}
container.set_dependencies(
graph
.get_mut(&index)
.unwrap()
.drain()
.map(|(index, _)| order_inverted[index].0),
);
}
let mut temp = systems.drain(..).map(Some).collect::<Vec<_>>();
for index in order {
systems.push(temp[index].take().unwrap());
}
Ok(())
}
impl Stage for SystemStage {
fn run(&mut self, world: &mut World) {
if let Some(world_id) = self.world_id {
assert!(
world.id() == world_id,
"Cannot run SystemStage on two different Worlds"
);
} else {
self.world_id = Some(world.id());
}
if self.systems_modified {
self.initialize_systems(world);
self.rebuild_orders_and_dependencies();
self.systems_modified = false;
self.executor.rebuild_cached_data(&self.parallel);
self.executor_modified = false;
if world.contains_resource::<ReportExecutionOrderAmbiguities>() {
self.report_ambiguities(world);
}
if let Some(resource_id) = self.must_read_resource {
self.check_uses_resource(resource_id, world);
}
} else if self.executor_modified {
self.executor.rebuild_cached_data(&self.parallel);
self.executor_modified = false;
}
let mut run_stage_loop = true;
while run_stage_loop {
let should_run = self.stage_run_criteria.should_run(world);
match should_run {
ShouldRun::No => return,
ShouldRun::NoAndCheckAgain => continue,
ShouldRun::YesAndCheckAgain => (),
ShouldRun::Yes => {
run_stage_loop = false;
}
};
// Evaluate system run criteria.
for index in 0..self.run_criteria.len() {
let (run_criteria, tail) = self.run_criteria.split_at_mut(index);
let mut criteria = &mut tail[0];
#[cfg(feature = "trace")]
let _span =
bevy_utils::tracing::info_span!("run criteria", name = &*criteria.name())
.entered();
match &mut criteria.inner {
RunCriteriaInner::Single(system) => criteria.should_run = system.run((), world),
RunCriteriaInner::Piped {
input: parent,
system,
..
} => criteria.should_run = system.run(run_criteria[*parent].should_run, world),
}
}
let mut run_system_loop = true;
let mut default_should_run = ShouldRun::Yes;
while run_system_loop {
run_system_loop = false;
fn should_run(
container: &SystemContainer,
run_criteria: &[RunCriteriaContainer],
default: ShouldRun,
) -> bool {
matches!(
container
.run_criteria()
.map(|index| run_criteria[index].should_run)
.unwrap_or(default),
ShouldRun::Yes | ShouldRun::YesAndCheckAgain
)
}
// Run systems that want to be at the start of stage.
for container in &mut self.exclusive_at_start {
if should_run(container, &self.run_criteria, default_should_run) {
{
#[cfg(feature = "trace")]
let _system_span = bevy_utils::tracing::info_span!(
"exclusive_system",
name = &*container.name()
)
.entered();
container.system_mut().run((), world);
}
{
#[cfg(feature = "trace")]
let _system_span = bevy_utils::tracing::info_span!(
"system_commands",
name = &*container.name()
)
.entered();
container.system_mut().apply_buffers(world);
}
}
}
// Run parallel systems using the executor.
// TODO: hard dependencies, nested sets, whatever... should be evaluated here.
for container in &mut self.parallel {
container.should_run =
should_run(container, &self.run_criteria, default_should_run);
}
self.executor.run_systems(&mut self.parallel, world);
// Run systems that want to be between parallel systems and their command buffers.
for container in &mut self.exclusive_before_commands {
if should_run(container, &self.run_criteria, default_should_run) {
{
#[cfg(feature = "trace")]
let _system_span = bevy_utils::tracing::info_span!(
"exclusive_system",
name = &*container.name()
)
.entered();
container.system_mut().run((), world);
}
{
#[cfg(feature = "trace")]
let _system_span = bevy_utils::tracing::info_span!(
"system_commands",
name = &*container.name()
)
.entered();
container.system_mut().apply_buffers(world);
}
}
}
// Apply parallel systems' buffers.
if self.apply_buffers {
for container in &mut self.parallel {
if container.should_run {
#[cfg(feature = "trace")]
let _span = bevy_utils::tracing::info_span!(
"system_commands",
name = &*container.name()
)
.entered();
container.system_mut().apply_buffers(world);
}
}
}
// Run systems that want to be at the end of stage.
for container in &mut self.exclusive_at_end {
if should_run(container, &self.run_criteria, default_should_run) {
{
#[cfg(feature = "trace")]
let _system_span = bevy_utils::tracing::info_span!(
"exclusive_system",
name = &*container.name()
)
.entered();
container.system_mut().run((), world);
}
{
#[cfg(feature = "trace")]
let _system_span = bevy_utils::tracing::info_span!(
"system_commands",
name = &*container.name()
)
.entered();
container.system_mut().apply_buffers(world);
}
}
}
// Check for old component and system change ticks
self.check_change_ticks(world);
// Evaluate run criteria.
let run_criteria = &mut self.run_criteria;
for index in 0..run_criteria.len() {
let (run_criteria, tail) = run_criteria.split_at_mut(index);
let criteria = &mut tail[0];
match criteria.should_run {
ShouldRun::No => (),
ShouldRun::Yes => criteria.should_run = ShouldRun::No,
ShouldRun::YesAndCheckAgain | ShouldRun::NoAndCheckAgain => {
match &mut criteria.inner {
RunCriteriaInner::Single(system) => {
criteria.should_run = system.run((), world);
}
RunCriteriaInner::Piped {
input: parent,
system,
..
} => {
criteria.should_run =
system.run(run_criteria[*parent].should_run, world);
}
}
match criteria.should_run {
ShouldRun::Yes
| ShouldRun::YesAndCheckAgain
| ShouldRun::NoAndCheckAgain => {
run_system_loop = true;
}
ShouldRun::No => (),
}
}
}
}
// after the first loop, default to not running systems without run criteria
default_should_run = ShouldRun::No;
}
}
}
}
#[cfg(test)]
mod tests {
use bevy_ecs_macros::RunCriteriaLabel;
use crate::{
schedule::{
IntoSystemDescriptor, RunCriteria, RunCriteriaDescriptorCoercion, ShouldRun,
SingleThreadedExecutor, Stage, SystemLabel, SystemSet, SystemStage,
},
system::{In, Local, Query, ResMut},
world::World,
};
use crate as bevy_ecs;
use crate::component::Component;
use crate::system::Resource;
#[derive(Component)]
struct W<T>(T);
#[derive(Resource)]
struct R(usize);
#[derive(Resource, Default)]
struct EntityCount(Vec<usize>);
fn make_exclusive(tag: usize) -> impl FnMut(&mut World) {
move |world| world.resource_mut::<EntityCount>().0.push(tag)
}
fn make_parallel(tag: usize) -> impl FnMut(ResMut<EntityCount>) {
move |mut resource: ResMut<EntityCount>| resource.0.push(tag)
}
fn every_other_time(mut has_ran: Local<bool>) -> ShouldRun {
*has_ran = !*has_ran;
if *has_ran {
ShouldRun::Yes
} else {
ShouldRun::No
}
}
#[test]
fn insertion_points() {
let mut world = World::new();
world.init_resource::<EntityCount>();
let mut stage = SystemStage::parallel()
.with_system(make_exclusive(0).at_start())
.with_system(make_parallel(1))
.with_system(make_exclusive(2).before_commands())
.with_system(make_exclusive(3).at_end());
stage.run(&mut world);
assert_eq!(world.resource_mut::<EntityCount>().0, vec![0, 1, 2, 3]);
stage.set_executor(Box::new(SingleThreadedExecutor::default()));
stage.run(&mut world);
assert_eq!(
world.resource::<EntityCount>().0,
vec![0, 1, 2, 3, 0, 1, 2, 3]
);
world.resource_mut::<EntityCount>().0.clear();
let mut stage = SystemStage::parallel()
.with_system(make_exclusive(2).before_commands())
.with_system(make_exclusive(3).at_end())
.with_system(make_parallel(1))
.with_system(make_exclusive(0).at_start());
stage.run(&mut world);
assert_eq!(world.resource::<EntityCount>().0, vec![0, 1, 2, 3]);
stage.set_executor(Box::new(SingleThreadedExecutor::default()));
stage.run(&mut world);
assert_eq!(
world.resource::<EntityCount>().0,
vec![0, 1, 2, 3, 0, 1, 2, 3]
);
world.resource_mut::<EntityCount>().0.clear();
let mut stage = SystemStage::parallel()
.with_system(make_parallel(2).before_commands())
.with_system(make_parallel(3).at_end())
.with_system(make_parallel(1))
.with_system(make_parallel(0).at_start());
stage.run(&mut world);
assert_eq!(world.resource::<EntityCount>().0, vec![0, 1, 2, 3]);
stage.set_executor(Box::new(SingleThreadedExecutor::default()));
stage.run(&mut world);
assert_eq!(
world.resource::<EntityCount>().0,
vec![0, 1, 2, 3, 0, 1, 2, 3]
);
}