unlit3d 0.1.0-dev.3

The high-level WebGPU rendering API over unlit_wgpu and unlit_ecs: ECS components, frame sources, input, UI and winit presentation.
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
//! The two shapes a value takes when it leaves a world: a slot that keeps
//! the newest one, and a queue that keeps every one.
//!
//! A world is owned by the thread that ticks it, so nothing inside it can be
//! read from another thread. What crosses is a handle: a producer keeps
//! publishing, and every reader sees the value on its own thread without a
//! job or a reply.
//!
//! - A [`Snapshot`](crate::relay::Snapshot) holds one value. Publishing
//!   replaces it, so a reader that misses a publish never learns what was
//!   there, and that is exactly right for state: poses, a playback position,
//!   the size a UI is laid out at. A reader is not a queue consumer, so a
//!   slow one cannot fall behind, and one that subscribes late still sees the
//!   newest publish.
//! - A [`Stream`](crate::relay::Stream) holds every value, once per reader.
//!   Publishing appends to each reader's queue, and a reader folds the queue
//!   into its tick with [`StreamReader::pump`](crate::relay::StreamReader::pump).
//!   That is right for events: input, collisions, a sequence ending. A reader
//!   that ticks slower than the producer still sees every value, in order. Its
//!   queue is unbounded and a publish never waits, so a producer that outruns
//!   its readers for a long time grows their queues for as long as it lives:
//!   that is the bargain, and an event source paced by a device rather than by
//!   a world is where it costs.
//!
//! Neither side blocks: a publish swaps a pointer or publishes to an unbounded
//! queue, and a reader takes what is there. Dropping a reader unsubscribes
//! it: a stream prunes it when nobody is left, and a snapshot keeps its value
//! for whoever subscribes next.

use std::fmt;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::{Arc, Mutex, PoisonError};

use arc_swap::ArcSwapOption;
use async_channel::{Receiver, Sender, unbounded};

/// The producing end of a [`Snapshot`]: keeps the newest value for however
/// many readers are subscribed.
///
/// Cloning shares the slot. It keeps the newest value whether or not anyone
/// is subscribed, so a reader that arrives after a publish still finds it
/// instead of waiting for the next one.
pub struct Snapshot<T> {
    slot: Arc<SnapshotSlot<T>>,
}

struct SnapshotSlot<T> {
    value: ArcSwapOption<T>,
    readers: AtomicUsize,
}

impl<T> Snapshot<T> {
    /// A slot nobody is subscribed to yet.
    pub fn new() -> Self {
        Self {
            slot: Arc::new(SnapshotSlot {
                value: ArcSwapOption::new(None),
                readers: AtomicUsize::new(0),
            }),
        }
    }

    /// Replace the value, and answer how many readers are subscribed.
    ///
    /// The value is kept even with none subscribed, so one that subscribes
    /// later still finds the newest publish. A collector uses the answer to
    /// prune its own subscription: at zero, nobody is left to read it.
    pub fn publish(&self, value: T) -> usize {
        self.slot.value.store(Some(Arc::new(value)));
        self.slot.readers.load(Ordering::Relaxed)
    }

    /// A reader of this slot. The snapshot stays a slot as long as any
    /// reader or the producing handle is alive.
    pub fn reader(&self) -> SnapshotReader<T> {
        self.slot.readers.fetch_add(1, Ordering::Relaxed);
        SnapshotReader {
            slot: Arc::clone(&self.slot),
        }
    }

    /// Whether a reader is subscribed.
    pub fn has_readers(&self) -> bool {
        self.slot.readers.load(Ordering::Relaxed) > 0
    }
}

impl<T> Clone for Snapshot<T> {
    fn clone(&self) -> Self {
        Self {
            slot: Arc::clone(&self.slot),
        }
    }
}

impl<T> Default for Snapshot<T> {
    fn default() -> Self {
        Self::new()
    }
}

impl<T> fmt::Debug for Snapshot<T> {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("Snapshot")
            .field("readers", &self.slot.readers.load(Ordering::Relaxed))
            .finish_non_exhaustive()
    }
}

/// The reading end of a [`Snapshot`]: always the newest published value.
pub struct SnapshotReader<T> {
    slot: Arc<SnapshotSlot<T>>,
}

impl<T> SnapshotReader<T> {
    /// The newest value, or `None` before the first publish.
    #[must_use]
    pub fn latest(&self) -> Option<Arc<T>> {
        self.slot.value.load_full()
    }
}

impl<T> Clone for SnapshotReader<T> {
    fn clone(&self) -> Self {
        self.slot.readers.fetch_add(1, Ordering::Relaxed);
        Self {
            slot: Arc::clone(&self.slot),
        }
    }
}

impl<T> Drop for SnapshotReader<T> {
    fn drop(&mut self) {
        self.slot.readers.fetch_sub(1, Ordering::Relaxed);
    }
}

impl<T> fmt::Debug for SnapshotReader<T> {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("SnapshotReader")
            .finish_non_exhaustive()
    }
}

/// The producing end of a [`Stream`]: publishes every value to every reader.
///
/// Cloning shares the readers. A publish to a stream nobody reads is
/// dropped, and the dead senders are pruned then. Publishing never blocks and
/// never drops a value for a reader that is there: each reader's queue is
/// unbounded, so a reader that falls behind falls behind in memory.
pub struct Stream<T> {
    inner: Arc<StreamInner<T>>,
}

struct StreamInner<T> {
    senders: Mutex<Vec<Sender<T>>>,
}

impl<T: Clone> Stream<T> {
    /// A stream nobody is subscribed to yet.
    pub fn new() -> Self {
        Self {
            inner: Arc::new(StreamInner {
                senders: Mutex::new(Vec::new()),
            }),
        }
    }

    /// A reader with its own queue, so one reader's pace cannot starve
    /// another's.
    ///
    /// The queue is unbounded: a reader that stops pumping holds every value
    /// published since, and a publish never waits for it. A producer whose
    /// pace comes from outside the worlds it serves - a device, say - is the
    /// one to publish at a pace its readers keep up with.
    pub fn subscribe(&self) -> StreamReader<T> {
        let (sender, receiver) = unbounded();
        self.inner
            .senders
            .lock()
            .unwrap_or_else(PoisonError::into_inner)
            .push(sender);
        StreamReader {
            inner: Arc::new(ReaderInner {
                queue: receiver,
                batch: Mutex::new(Vec::new()),
            }),
        }
    }

    /// Publish one value to every reader, and answer how many are left.
    ///
    /// A reader that dropped its queue is pruned here, so the producing
    /// world learns that nobody is left the next time it publishes.
    pub fn publish(&self, value: T) -> usize {
        self.publish_all([value])
    }

    /// Publish several values, in the order given.
    pub fn publish_all(&self, values: impl IntoIterator<Item = T>) -> usize {
        let mut senders = self
            .inner
            .senders
            .lock()
            .unwrap_or_else(PoisonError::into_inner);
        senders.retain(|sender| !sender.is_closed());
        for value in values {
            for sender in senders.iter() {
                // The queue is unbounded, so the only way this fails is a
                // reader that went away since the prune above.
                let _ = sender.try_send(value.clone());
            }
        }
        // A reader may have gone away while the values were being sent, so
        // the count is taken after the sends rather than before them.
        senders.retain(|sender| !sender.is_closed());
        senders.len()
    }

    /// Whether a reader is subscribed, pruning the ones that went away.
    pub fn has_readers(&self) -> bool {
        let mut senders = self
            .inner
            .senders
            .lock()
            .unwrap_or_else(PoisonError::into_inner);
        senders.retain(|sender| !sender.is_closed());
        !senders.is_empty()
    }
}

impl<T: Clone> Clone for Stream<T> {
    fn clone(&self) -> Self {
        Self {
            inner: Arc::clone(&self.inner),
        }
    }
}

impl<T: Clone> Default for Stream<T> {
    fn default() -> Self {
        Self::new()
    }
}

impl<T> fmt::Debug for Stream<T> {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.debug_struct("Stream").finish_non_exhaustive()
    }
}

/// The reading end of a [`Stream`]: one queue per reader, folded into the
/// reader's tick.
///
/// A reader is cheap to clone within the world that owns it; every clone
/// shares the same queue and batch, so the world's components read one
/// tick's events between them.
pub struct StreamReader<T> {
    inner: Arc<ReaderInner<T>>,
}

struct ReaderInner<T> {
    queue: Receiver<T>,
    batch: Mutex<Vec<T>>,
}

impl<T> StreamReader<T> {
    /// Start the reader's tick: whatever queued since the last pump becomes
    /// the batch, and the previous batch is dropped.
    ///
    /// Call it once per tick, before anything reads the batch. Calling it
    /// twice in one tick drops what the first call collected.
    pub fn pump(&self) {
        let mut batch = self
            .inner
            .batch
            .lock()
            .unwrap_or_else(PoisonError::into_inner);
        batch.clear();
        while let Ok(value) = self.inner.queue.try_recv() {
            batch.push(value);
        }
    }

    /// Run `f` over this tick's values, oldest first.
    ///
    /// Reading does not consume: every reader in the world sees the same
    /// batch, so no one can take a value away from the next.
    pub fn with_batch<R>(&self, f: impl FnOnce(&[T]) -> R) -> R {
        let batch = self
            .inner
            .batch
            .lock()
            .unwrap_or_else(PoisonError::into_inner);
        f(&batch)
    }

    /// Append this tick's values, oldest first, to `out`.
    pub fn copy_into(&self, out: &mut Vec<T>)
    where
        T: Clone,
    {
        self.with_batch(|batch| out.extend_from_slice(batch));
    }
}

impl<T> Clone for StreamReader<T> {
    fn clone(&self) -> Self {
        Self {
            inner: Arc::clone(&self.inner),
        }
    }
}

impl<T> fmt::Debug for StreamReader<T> {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("StreamReader")
            .finish_non_exhaustive()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn a_snapshot_keeps_the_newest_value() {
        let snapshot = Snapshot::new();
        let reader = snapshot.reader();
        assert!(reader.latest().is_none());
        assert_eq!(snapshot.publish(1), 1);
        assert_eq!(snapshot.publish(2), 1);
        assert_eq!(*reader.latest().unwrap(), 2);
    }

    #[test]
    fn a_late_reader_finds_the_value_published_before_it() {
        let snapshot = Snapshot::new();
        assert_eq!(snapshot.publish(1), 0);
        let reader = snapshot.reader();
        assert_eq!(*reader.latest().expect("the publish was kept"), 1);
        assert_eq!(snapshot.publish(2), 1);
        assert_eq!(*reader.latest().expect("the newest publish"), 2);
    }

    #[test]
    fn dropping_the_last_reader_unsubscribes() {
        let snapshot = Snapshot::new();
        let reader = snapshot.reader();
        assert_eq!(snapshot.publish(1), 1);
        drop(reader);
        assert_eq!(snapshot.publish(2), 0);
        assert_eq!(*snapshot.reader().latest().expect("the last publish"), 2);
    }

    #[test]
    fn a_stream_gives_every_reader_the_same_values() {
        let stream = Stream::new();
        let one = stream.subscribe();
        let other = stream.subscribe();
        assert_eq!(stream.publish(1), 2);
        assert_eq!(stream.publish(2), 2);
        one.pump();
        other.pump();
        assert_eq!(one.with_batch(|batch| batch.to_vec()), vec![1, 2]);
        assert_eq!(other.with_batch(|batch| batch.to_vec()), vec![1, 2]);
    }

    #[test]
    fn a_pump_replaces_the_previous_batch() {
        let stream = Stream::new();
        let reader = stream.subscribe();
        stream.publish(1);
        reader.pump();
        stream.publish(2);
        reader.pump();
        assert_eq!(reader.with_batch(|batch| batch.to_vec()), vec![2]);
    }

    #[test]
    fn a_stream_prunes_a_reader_that_went_away() {
        let stream = Stream::new();
        let reader = stream.subscribe();
        assert!(stream.has_readers());
        drop(reader);
        assert!(!stream.has_readers());
        assert_eq!(stream.publish(1), 0);
    }

    #[test]
    fn a_reader_clone_shares_the_queue() {
        let stream = Stream::new();
        let reader = stream.subscribe();
        let clone = reader.clone();
        stream.publish(1);
        reader.pump();
        assert_eq!(clone.with_batch(|batch| batch.to_vec()), vec![1]);
        drop(reader);
        assert!(stream.has_readers());
    }
}