pebble-engine 0.13.0

A modular, ECS-style graphics/app framework for Rust.
Documentation
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
use crate::ecs::resources::Resources;
use crate::ecs::system::{RequiredResource, Res, SystemParam};
use crate::threading::{BackgroundTasks, SpawnableFuture};

/// A double-buffered queue of `T` events, stored as a singleton resource.
///
/// Write with an [`EventWriter<T>`] system param, read with an
/// [`EventReader<T>`] system param. An event sent during tick `N` stays
/// visible to readers for the rest of tick `N` and all of tick `N + 1`, then
/// is dropped — so a reader running anywhere in either tick sees it exactly
/// once, regardless of whether it runs before or after the writer that sent
/// it.
///
/// Not usable until registered with
/// [`App::add_event`](crate::app::App::add_event), which inserts this
/// resource and schedules the per-tick aging that gives the two-tick
/// guarantee above.
pub struct Events<T> {
    current: Vec<(usize, T)>,
    previous: Vec<(usize, T)>,
    next_id: usize,
}

impl<T> Default for Events<T> {
    fn default() -> Self {
        Self {
            current: Vec::new(),
            previous: Vec::new(),
            next_id: 0,
        }
    }
}

impl<T> Events<T> {
    /// Push a new event, tagged with the next monotonically increasing id.
    pub fn send(&mut self, event: T) {
        self.current.push((self.next_id, event));
        self.next_id += 1;
    }

    /// Age the buffers: last tick's `current` becomes `previous` (still
    /// visible to readers that haven't caught up), and a fresh `current`
    /// starts collecting this tick's sends.
    ///
    /// Called once per tick, before any user systems run, by the closure
    /// [`App::add_event`](crate::app::App::add_event) registers — not meant
    /// to be called directly.
    pub(crate) fn update(&mut self) {
        self.previous = std::mem::take(&mut self.current);
    }
}

/// Read-only handle to an [`Events<T>`] resource, obtained as a system
/// parameter.
///
/// Each system with an `EventReader<T>` parameter has its own private
/// read cursor (persisted like [`Local`](crate::ecs::system::Local)), so
/// multiple readers of the same event type don't interfere with each other.
pub struct EventReader<'a, T: 'static> {
    events: hecs::Ref<'a, Events<T>>,
    last_seen: &'a mut usize,
}

impl<'a, T: 'static> EventReader<'a, T> {
    /// Iterate events this reader hasn't seen yet, oldest first, then mark
    /// them as read.
    pub fn iter(&mut self) -> impl Iterator<Item = &T> + '_ {
        let seen = *self.last_seen;
        let unread: Vec<&T> = self
            .events
            .previous
            .iter()
            .chain(self.events.current.iter())
            .filter(|(id, _)| *id >= seen)
            .map(|(_, event)| event)
            .collect();
        *self.last_seen = self.events.next_id;
        unread.into_iter()
    }

    /// `true` if there are no unread events for this reader.
    pub fn is_empty(&self) -> bool {
        let seen = *self.last_seen;
        !self
            .events
            .previous
            .iter()
            .chain(self.events.current.iter())
            .any(|(id, _)| *id >= seen)
    }
}

impl<T> SystemParam for EventReader<'static, T>
where
    T: Send + Sync + 'static,
{
    type Item<'a> = EventReader<'a, T>;
    type State = usize;

    fn fetch<'a>(
        state: &'a mut Self::State,
        world: &'a hecs::World,
        resources: &'a Resources,
    ) -> Self::Item<'a> {
        EventReader {
            events: resources.get_resource(world),
            last_seen: state,
        }
    }

    fn requires() -> Vec<RequiredResource> {
        vec![RequiredResource {
            name: std::any::type_name::<Events<T>>(),
            type_id: std::any::TypeId::of::<Events<T>>(),
            present: |world, resources| resources.has_resource::<Events<T>>(world),
            hint: Some(
                "Register this event type with `app.add_event::<T>()` (or \
                 `app.add_async_event::<T>()` if it's delivered by a background task) \
                 before this system runs.",
            ),
        }]
    }
}

/// Soft-access variant of [`EventReader<T>`]: `None` instead of panicking
/// while `T` isn't registered (via [`App::add_event`](crate::app::App::add_event)),
/// so a system that should just skip when an event type it optionally
/// consumes hasn't been set up yet doesn't have to hard-depend on it. The
/// read cursor still persists correctly across ticks once the event type
/// does appear later — it lives in the same per-system `State` slot either way.
impl<T> SystemParam for Option<EventReader<'static, T>>
where
    T: Send + Sync + 'static,
{
    type Item<'a> = Option<EventReader<'a, T>>;
    type State = usize;

    fn fetch<'a>(
        state: &'a mut Self::State,
        world: &'a hecs::World,
        resources: &'a Resources,
    ) -> Self::Item<'a> {
        if resources.has_resource::<Events<T>>(world) {
            return Some(EventReader {
                events: resources.get_resource(world),
                last_seen: state,
            });
        }
        None
    }
}

/// Write handle to an [`Events<T>`] resource, obtained as a system
/// parameter.
pub struct EventWriter<'a, T: 'static> {
    events: hecs::RefMut<'a, Events<T>>,
}

impl<'a, T: 'static> EventWriter<'a, T> {
    /// Queue `event` for delivery to every [`EventReader<T>`] for the rest
    /// of this tick and all of the next.
    pub fn send(&mut self, event: T) {
        self.events.send(event);
    }
}

impl<T> SystemParam for EventWriter<'static, T>
where
    T: Send + Sync + 'static,
{
    type Item<'a> = EventWriter<'a, T>;
    type State = ();

    fn fetch<'a>(
        _state: &'a mut Self::State,
        world: &'a hecs::World,
        resources: &'a Resources,
    ) -> Self::Item<'a> {
        EventWriter {
            events: resources.get_resource_mut(world),
        }
    }

    fn requires() -> Vec<RequiredResource> {
        vec![RequiredResource {
            name: std::any::type_name::<Events<T>>(),
            type_id: std::any::TypeId::of::<Events<T>>(),
            present: |world, resources| resources.has_resource::<Events<T>>(world),
            hint: Some(
                "Register this event type with `app.add_event::<T>()` (or \
                 `app.add_async_event::<T>()` if it's delivered by a background task) \
                 before this system runs.",
            ),
        }]
    }
}

/// Soft-access variant of [`EventWriter<T>`]: `None` instead of panicking
/// while `T` isn't registered (via [`App::add_event`](crate::app::App::add_event)).
impl<T> SystemParam for Option<EventWriter<'static, T>>
where
    T: Send + Sync + 'static,
{
    type Item<'a> = Option<EventWriter<'a, T>>;
    type State = ();

    fn fetch<'a>(
        _state: &'a mut Self::State,
        world: &'a hecs::World,
        resources: &'a Resources,
    ) -> Self::Item<'a> {
        if resources.has_resource::<Events<T>>(world) {
            return Some(EventWriter {
                events: resources.get_resource_mut(world),
            });
        }
        None
    }
}

/// The channel [`AsyncEventWriter::spawn`] sends into and the drain system
/// (registered by [`App::add_async_event`](crate::app::App::add_async_event))
/// reads out of, turning finished background tasks into ordinary `T`
/// events. A resource, but not one to reach for directly — go through
/// [`AsyncEventWriter<T>`].
pub(crate) struct AsyncEventChannel<T> {
    tx: crossbeam_channel::Sender<T>,
    rx: crossbeam_channel::Receiver<T>,
}

impl<T> AsyncEventChannel<T> {
    pub(crate) fn new() -> Self {
        let (tx, rx) = crossbeam_channel::unbounded();
        Self { tx, rx }
    }
}

/// `PreUpdate` system registered by [`App::add_async_event`](crate::app::App::add_async_event):
/// drains every task that finished since last checked and re-delivers each
/// result as a `T` event. Two ticks of latency at most between a task
/// finishing and a reader seeing it — one to be drained here, one more it
/// stays visible for like any [`Events<T>`] send.
pub(crate) fn drain_async_events<T: Send + Sync + 'static>(
    channel: Res<AsyncEventChannel<T>>,
    mut writer: EventWriter<T>,
) {
    while let Ok(event) = channel.rx.try_recv() {
        writer.send(event);
    }
}

/// System parameter for spawning async work whose result is delivered as a
/// `T` event once it resolves. The friendly front door for combining
/// [`BackgroundTasks::spawn_async`] with the [`Events`] system, instead of
/// hand-rolling a pending-task resource and poll system per async data
/// source the way [`GraphicsPlugin`](crate::rendering::graphics_plugin::GraphicsPlugin)
/// does for the GPU backend.
///
/// Register the event type with [`App::add_async_event::<T>()`](crate::app::App::add_async_event)
/// (instead of [`App::add_event`](crate::app::App::add_event)) to make this usable as a system
/// parameter. Reading the result is completely ordinary — just an
/// [`EventReader<T>`], same as any other event; nothing about consuming it
/// differs from a synchronously-sent one. Requires
/// [`BackgroundTasksPlugin`](crate::threading::BackgroundTasksPlugin) to be
/// registered, since [`spawn`](Self::spawn) drives its future through
/// [`BackgroundTasks::spawn_async`].
///
/// ```ignore
/// app.add_async_event::<ReadbackDone>();
///
/// fn start_readback(events: AsyncEventWriter<ReadbackDone>, backend: Res<WGPUBackend>) {
///     let future = backend.readback_buffer(&buf);
///     events.spawn(async move { ReadbackDone(future.await) });
/// }
///
/// fn on_readback(mut reader: EventReader<ReadbackDone>) {
///     for event in reader.iter() {
///         // event.0
///     }
/// }
/// ```
pub struct AsyncEventWriter<'a, T: Send + 'static> {
    tasks: hecs::Ref<'a, BackgroundTasks>,
    channel: hecs::Ref<'a, AsyncEventChannel<T>>,
}

impl<'a, T: Send + 'static> AsyncEventWriter<'a, T> {
    /// Spawn `future`. Once it resolves, its output is delivered to every
    /// [`EventReader<T>`] — no matter how many ticks the future takes, and
    /// with no need to hold onto a handle or poll anything yourself.
    pub fn spawn(&self, future: impl SpawnableFuture<T>) {
        let tx = self.channel.tx.clone();
        let _ = self.tasks.spawn_async(async move {
            // Ignore send failure: it only happens if the receiving end
            // (owned by the App) was dropped, meaning the app has shut down
            // and there's nothing left to deliver this to.
            let _ = tx.send(future.await);
        });
    }
}

impl<T> SystemParam for AsyncEventWriter<'static, T>
where
    T: Send + Sync + 'static,
{
    type Item<'a> = AsyncEventWriter<'a, T>;
    type State = ();

    fn fetch<'a>(
        _state: &'a mut Self::State,
        world: &'a hecs::World,
        resources: &'a Resources,
    ) -> Self::Item<'a> {
        AsyncEventWriter {
            tasks: resources.get_resource(world),
            channel: resources.get_resource(world),
        }
    }

    fn requires() -> Vec<RequiredResource> {
        vec![
            RequiredResource {
                name: std::any::type_name::<BackgroundTasks>(),
                type_id: std::any::TypeId::of::<BackgroundTasks>(),
                present: |world, resources| resources.has_resource::<BackgroundTasks>(world),
                hint: Some(
                    "`AsyncEventWriter` drives its future through `BackgroundTasks` — register \
                     `app.add_plugin(BackgroundTasksPlugin::new(worker_count))` before this system runs.",
                ),
            },
            RequiredResource {
                name: std::any::type_name::<AsyncEventChannel<T>>(),
                type_id: std::any::TypeId::of::<AsyncEventChannel<T>>(),
                present: |world, resources| resources.has_resource::<AsyncEventChannel<T>>(world),
                hint: Some(
                    "Register this event type with `app.add_async_event::<T>()` (not \
                     `app.add_event`) before this system runs.",
                ),
            },
        ]
    }
}

/// Soft-access variant of [`AsyncEventWriter<T>`]: `None` instead of panicking
/// while `T` isn't registered via [`add_async_event`](crate::app::App::add_async_event)
/// (or [`BackgroundTasksPlugin`](crate::threading::BackgroundTasksPlugin)
/// isn't installed yet). Useful for systems that only opportunistically
/// spawn background work and shouldn't hard-fail while the app is still
/// assembling its plugins.
impl<T> SystemParam for Option<AsyncEventWriter<'static, T>>
where
    T: Send + Sync + 'static,
{
    type Item<'a> = Option<AsyncEventWriter<'a, T>>;
    type State = ();

    fn fetch<'a>(
        _state: &'a mut Self::State,
        world: &'a hecs::World,
        resources: &'a Resources,
    ) -> Self::Item<'a> {
        if resources.has_resource::<BackgroundTasks>(world)
            && resources.has_resource::<AsyncEventChannel<T>>(world)
        {
            return Some(AsyncEventWriter {
                tasks: resources.get_resource(world),
                channel: resources.get_resource(world),
            });
        }
        None
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::app::{App, SystemStage};
    use crate::ecs::system::{Local, ResMut};

    struct Damage(u32);

    struct Seen(Vec<u32>);

    fn send_once(mut writer: EventWriter<Damage>, mut sent: Local<bool>) {
        if !*sent {
            writer.send(Damage(5));
            *sent = true;
        }
    }

    fn record_damage(mut reader: EventReader<Damage>, mut seen: ResMut<Seen>) {
        for event in reader.iter() {
            seen.0.push(event.0);
        }
    }

    #[test]
    fn reader_in_an_earlier_stage_still_catches_a_same_tick_send_one_tick_later() {
        let mut app = App::new();
        app.add_event::<Damage>();
        app.add_resource(Seen(Vec::new()));

        // The reader runs in PreUpdate, before the writer's Update stage —
        // so within the tick the event is sent, this reader has already run
        // and can't see it yet. It should only pick it up on the *next*
        // tick, via the aged `previous` buffer, and exactly once.
        app.add_system(SystemStage::PreUpdate, record_damage);
        app.add_system(SystemStage::Update, send_once);
        app.build();

        app.update(); // tick 1: reader runs first (nothing sent yet), then writer sends
        assert_eq!(app.get_resource::<Seen>().0, Vec::<u32>::new());

        app.update(); // tick 2: reader now sees last tick's send
        assert_eq!(app.get_resource::<Seen>().0, vec![5]);

        app.update(); // tick 3: event has aged out, nothing new
        assert_eq!(app.get_resource::<Seen>().0, vec![5]);
    }

    struct Loaded(u32);

    fn kick_off(events: AsyncEventWriter<Loaded>, mut sent: Local<bool>) {
        if !*sent {
            events.spawn(async { Loaded(42) });
            *sent = true;
        }
    }

    fn record_loaded(mut reader: EventReader<Loaded>, mut seen: ResMut<Seen>) {
        for event in reader.iter() {
            seen.0.push(event.0);
        }
    }

    #[test]
    fn async_events_deliver_a_background_tasks_result_as_an_event() {
        use crate::ecs::plugin::Plugin;
        use crate::threading::BackgroundTasksPlugin;

        let mut app = App::new();
        BackgroundTasksPlugin::new(1).build(&mut app);
        app.add_async_event::<Loaded>();
        app.add_resource(Seen(Vec::new()));
        app.add_system(SystemStage::Update, kick_off);
        app.add_system(SystemStage::PostRender, record_loaded);
        app.build();

        // The task resolves on a worker thread, off the tick loop — poll a
        // bounded number of ticks (with a short sleep so the worker gets a
        // chance to run) instead of assuming it lands on a specific tick.
        for _ in 0..200 {
            app.update();
            if !app.get_resource::<Seen>().0.is_empty() {
                break;
            }
            std::thread::sleep(std::time::Duration::from_millis(5));
        }

        assert_eq!(app.get_resource::<Seen>().0, vec![42]);
    }
}