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
use crate::z_ignore_test_common::*;
use flecs_ecs::prelude::*;
#[derive(Component)]
pub struct PositionSP {
x: f32,
y: f32,
}
#[derive(Component)]
pub struct VelocitySP {
x: f32,
y: f32,
}
fn main() {
let world = World::new();
// System that sets velocity using ecs_set for entities with PositionSP.
// While systems are progressing, operations like ecs_set are deferred until
// it is safe to merge. By default this merge happens at the end of the
// frame, but we can annotate systems to give the scheduler more information
// about what it's doing, which allows it to insert sync points earlier.
//
// Note that sync points are never necessary/inserted for systems that write
// components provided by their signature, as these writes directly happen
// in the ECS storage and are never deferred.
//
// .inout_none() for PositionSP tells the scheduler that while we
// want to match entities with PositionSP, we're not interested in reading or
// writing the component value.
world
.system_named::<()>("SetVelocitySP")
.with(&PositionSP::id())
.set_inout_none()
.with(&mut VelocitySP::id()) // VelocitySP is written, but shouldn't be matched
.each_entity(|e, ()| {
e.set(VelocitySP { x: 1.0, y: 2.0 });
});
// This system reads VelocitySP, which causes the insertion of a sync point.
world
.system_named::<(&mut PositionSP, &VelocitySP)>("Move")
.each(|(p, v)| {
p.x += v.x;
p.y += v.y;
});
// Print resulting PositionSP
world
.system_named::<&PositionSP>("PrintPositionSP")
.each_entity(|e, p| {
println!("{}: {{ {}, {} }}", e.name(), p.x, p.y);
});
// Create a few test entities for a PositionSP, VelocitySP query
world
.entity_named("e1")
.set(PositionSP { x: 10.0, y: 20.0 })
.set(VelocitySP { x: 1.0, y: 2.0 });
world
.entity_named("e2")
.set(PositionSP { x: 10.0, y: 20.0 })
.set(VelocitySP { x: 3.0, y: 4.0 });
// Run systems. Debug logging enables us to see the generated schedule.
// NOTE flecs C / flecs_ecs_sys needs to be build in debug mode to see the logging.
// use the feature flag "sys_build_debug" to enable debug build of flecs C.
set_log_level(1);
world.progress();
set_log_level(-1);
// Output:
// info: pipeline rebuild
// info: | schedule: threading: 0, staging: 1:
// info: | | system SetVelocitySP
// info: | | merge
// info: | schedule: threading: 0, staging: 1:
// info: | | system Move
// info: | | system PrintPositionSP
// info: | | merge
// e1: { 11, 22 }
// e2: { 11, 22 }
// The "merge" lines indicate sync points.
//
// Removing `.with(&mut VelocitySP::id())` from the system will remove the first
// sync point from the schedule.
}
#[cfg(feature = "flecs_nightly_tests")]
#[test]
#[ignore = "`set_log_level` is not safe in parallel tests"]
fn test() {
let output_capture = OutputCapture::capture().unwrap();
main();
output_capture.test("system_sync_point".to_string());
}