From 31af9bfa9a08ae9611ca49d8c66889f3a800395c Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 19 Nov 2026 02:13:09 -0800 Subject: [PATCH 1/3] chore(quest): claim quest/m1/readback.md Quest-Claim: bbc26dae2b778c4b From 8bb0bbafcfebe0b0d7976f2bfed74f69599ef21b Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 19 Nov 2026 02:21:14 -0800 Subject: [PATCH 2/3] refactor(scene): one readback for the compositor's small GPU reads The dark-capture probe and the keep-clear look each kept their own storage and mapped buffers, rate cap, map in flight, and stale-read discard. Both now share readback::Readback and keep only their pipeline and what they make of the tiles. Co-Authored-By: Claude Opus 5.5 --- quest/m1/readback.md | 32 ------- rs/moq-scene/src/compositor.rs | 4 +- rs/moq-scene/src/dark.rs | 126 +++++++-------------------- rs/moq-scene/src/lib.rs | 1 + rs/moq-scene/src/place.rs | 116 ++++++------------------- rs/moq-scene/src/readback.rs | 151 +++++++++++++++++++++++++++++++++ 6 files changed, 213 insertions(+), 217 deletions(-) delete mode 100644 quest/m1/readback.md create mode 100644 rs/moq-scene/src/readback.rs diff --git a/quest/m1/readback.md b/quest/m1/readback.md deleted file mode 100644 index 85c14a2b..00000000 --- a/quest/m1/readback.md +++ /dev/null @@ -1,32 +0,0 @@ -# [S] One readback for the compositor's small GPU reads - -## Goal - -The compositor reads a few small pictures back from the GPU, a few times a -second each, through one shared helper: the dark-capture probe -(`rs/moq-scene/src/dark.rs`) and the keep-clear look -(`rs/moq-scene/src/place.rs`) each keep only their shader and what they -make of the tiles, and neither stalls a frame. - -## Plan - -The two landed separately with the same pattern: a compute pass reducing a -picture to a grid of tiles (64x36 for the probe, 32x18 for the look), at -most every 250 ms, its result copied to a buffer -mapped with `map_async` and read on a later frame, a failed or late read -dropped. - -- **Shared:** the rate cap (`due`), the storage and readback buffers sized - to the largest grid, the copy after the pass, the map in flight and its - poll, and discarding a read whose source or feed changed meanwhile. -- **Kept apart:** each pipeline and its bindings. `dark.wgsl` reduces the - capture texture alone to mean color and brightest channel; `place.wgsl` - compares the items drawn under a layer with the last read, so it keeps - two pictures that ping-pong. -- Their tests (`dark::tests::`, `compositor::tests::a_camera_keeping_clear_glides_off_what_moves`, - `a_moment_of_dark_draws_the_last_good_picture`) pass unchanged under - `just render-sw`, and the frame time on the AMD iGPU does not grow. - -Decided 2026-11-17 (maintainer, on -[#194](https://github.com/moq-dev/studio/pull/194#issuecomment-6941200435)): -✅ one helper once both land (rejected: sharing in #194, before #192 lands). diff --git a/rs/moq-scene/src/compositor.rs b/rs/moq-scene/src/compositor.rs index b57d515c..0c6cc4df 100644 --- a/rs/moq-scene/src/compositor.rs +++ b/rs/moq-scene/src/compositor.rs @@ -1511,7 +1511,7 @@ impl Compositor { /// good picture in its place while it holds one over the dark. A picture /// cut out of its background is never held, since the good one is not. /// A picture is drawn before it is judged, so black shows until the next - /// read, up to [`INTERVAL`](crate::dark::INTERVAL) and its readback: + /// read, up to [`INTERVAL`](crate::readback::INTERVAL) and its readback: /// judging each one first would hold every capture's picture back by a /// readback, or read every picture, and the maintainer took this bounded /// flash over either on moq-dev/studio#192. @@ -4634,7 +4634,7 @@ mod tests { read(&mut compositor, &mut layer, RED, at(Duration::ZERO), Background::Keep); assert!(!layer.holding); - let dark = crate::dark::INTERVAL; + let dark = crate::readback::INTERVAL; read(&mut compositor, &mut layer, BLACK, at(dark), Background::Keep); assert!(layer.holding, "the last good picture is drawn"); assert_eq!(layer.probe.as_ref().unwrap().dark(at(dark)), None); diff --git a/rs/moq-scene/src/dark.rs b/rs/moq-scene/src/dark.rs index 624039b4..05ca6e35 100644 --- a/rs/moq-scene/src/dark.rs +++ b/rs/moq-scene/src/dark.rs @@ -4,9 +4,10 @@ //! //! A camera that stops delivering already reads as stalled, but a display or //! window that stands still is no fault, so a capture is judged by what it -//! shows. A [`Probe`] reads the newest picture at most every [`INTERVAL`], -//! on the GPU: a compute shader reduces it to a small grid of tiles, and only -//! the grid comes back, a frame or so later, to be judged on the CPU. +//! shows. A [`Probe`] reads the newest picture at most every +//! [`INTERVAL`](crate::readback::INTERVAL), on the GPU: a compute shader +//! reduces it to a small grid of tiles, and only the grid comes back, a +//! frame or so later, to be judged on the CPU. //! //! Until a capture has been dark for [`DARK`], the probe holds the last //! picture it judged not dark, for the compositor to draw in its place, so a @@ -14,18 +15,12 @@ //! After that the capture shows as it is, so a stale picture never passes for //! live. -use std::sync::{Arc, Mutex}; use std::time::Duration; use kio::clock::Instant; use crate::effect::{Effects, Picture}; - -/// How often a probe reads a capture's picture, at most, and only when one -/// arrives: a camera or window that stops delivering is not read, though a -/// Linux screen capture repeats its last picture while it stands still, and -/// is read each time. -pub const INTERVAL: Duration = Duration::from_millis(250); +use crate::readback::Readback; /// How long a capture's picture stays dark before it counts as gone dark, /// rather than a moment of black the last good picture covers. @@ -35,8 +30,8 @@ pub const DARK: Duration = Duration::from_secs(3); /// texels a side at 1080p. const TILES: (u32, u32) = (64, 36); -/// The bytes of the reduced grid, four a tile. -const BYTES: u64 = (TILES.0 * TILES.1 * 4) as u64; +/// How many tiles the grid has. +const COUNT: u32 = TILES.0 * TILES.1; /// The brightest a dark picture's brightest channel gets, out of 255: /// a camera's noise in the dark stays under it, and any lit pixel passes it. @@ -100,7 +95,7 @@ impl Prober { ty: wgpu::BindingType::Buffer { ty: wgpu::BufferBindingType::Storage { read_only: false }, has_dynamic_offset: false, - min_binding_size: wgpu::BufferSize::new(BYTES), + min_binding_size: wgpu::BufferSize::new(COUNT as u64 * 4), }, count: None, }, @@ -123,30 +118,17 @@ impl Prober { } } -/// A read under way: when its picture was taken, and the buffer's mapping -/// once the GPU is done. -struct Reading { - at: Instant, - mapped: Arc>>>, - /// Whether the capture stopped being live since, so what it read is - /// from before and judged neither dark nor good. - stale: bool, -} - /// Watches one capture's pictures for going dark. pub(crate) struct Probe { device: wgpu::Device, - queue: wgpu::Queue, - tiles: wgpu::Buffer, - readback: wgpu::Buffer, + /// The grid read back, a read under way dropped once the capture stops + /// being live, as it is from before and judged neither dark nor good. + readback: Readback<()>, /// A copy of the picture last read, since the capture's own texture is /// drawn over by its next picture before the read comes back. read: Option, /// The newest picture judged not dark, bound to draw. good: Option<(wgpu::Texture, Picture)>, - reading: Option, - /// When the last read was taken. - last: Option, /// Whether a picture arrived since the last read. changed: bool, /// When the first of the dark pictures read since the last good one was @@ -159,27 +141,11 @@ pub(crate) struct Probe { impl Probe { pub fn new(device: &wgpu::Device, queue: &wgpu::Queue) -> Self { - let tiles = device.create_buffer(&wgpu::BufferDescriptor { - label: Some("moq-scene dark tiles"), - size: BYTES, - usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC, - mapped_at_creation: false, - }); - let readback = device.create_buffer(&wgpu::BufferDescriptor { - label: Some("moq-scene dark tiles"), - size: BYTES, - usage: wgpu::BufferUsages::COPY_DST | wgpu::BufferUsages::MAP_READ, - mapped_at_creation: false, - }); Self { device: device.clone(), - queue: queue.clone(), - tiles, - readback, + readback: Readback::new(device, queue, COUNT), read: None, good: None, - reading: None, - last: None, changed: true, dark: None, feed: None, @@ -202,9 +168,7 @@ impl Probe { fn forget(&mut self) { self.dark = None; self.good = None; - if let Some(reading) = &mut self.reading { - reading.stale = true; - } + self.readback.discard(); } /// Note that the capture's texture holds a new picture. @@ -214,7 +178,9 @@ impl Probe { /// Judge the read that came back, if one did, and read `picture`, the /// capture's newest, if it is new and the last read was at least - /// [`INTERVAL`] ago. `picture` needs `COPY_SRC`. + /// [`INTERVAL`](crate::readback::INTERVAL) ago. `picture` needs `COPY_SRC`. A camera or window that + /// stops delivering is not read, though a Linux screen capture repeats + /// its last picture while it stands still, and is read each time. /// /// A capture that is not `live`, such as one reopening, is not dark, and /// forgets its last good picture and what it is reading, so nothing from @@ -226,10 +192,7 @@ impl Probe { self.forget(); return; } - let due = self - .last - .is_none_or(|last| now.saturating_duration_since(last) >= INTERVAL); - if self.reading.is_none() && self.changed && due { + if self.changed && self.readback.due(now) { self.start(prober, picture, now); } } @@ -275,7 +238,7 @@ impl Probe { }, wgpu::BindGroupEntry { binding: 1, - resource: self.tiles.as_entire_binding(), + resource: self.readback.binding(), }, ], }); @@ -290,46 +253,18 @@ impl Probe { pass.set_bind_group(0, &bind, &[]); pass.dispatch_workgroups(TILES.0, TILES.1, 1); } - encoder.copy_buffer_to_buffer(&self.tiles, 0, &self.readback, 0, BYTES); - - self.queue.submit([encoder.finish()]); - let mapped = Arc::new(Mutex::new(None)); - let done = mapped.clone(); - // Called from a later submit or poll, once the GPU is done. - self.readback.map_async(wgpu::MapMode::Read, .., move |result| { - *done.lock().unwrap() = Some(result); - }); + self.readback.submit(encoder, COUNT, now, ()); self.read = Some(read); - self.reading = Some(Reading { - at: now, - mapped, - stale: false, - }); - self.last = Some(now); self.changed = false; } /// Judge the read under way, if it came back: a dark picture starts or /// continues the dark, and any other ends it and becomes the good one. fn judge(&mut self, effects: &Effects) { - let Some(reading) = &self.reading else { return }; - let Some(mapped) = reading.mapped.lock().unwrap().take() else { - return; - }; - let (at, stale) = (reading.at, reading.stale); - self.reading = None; - let dark = mapped.map_err(|err| err.to_string()).and_then(|()| { - let dark = match self.readback.slice(..).get_mapped_range() { - Ok(view) => Ok(judge(&view)), - Err(err) => Err(format!("{err:?}")), - }; - self.readback.unmap(); - dark - }); - let dark = match dark { - Ok(_) if stale => return, - Ok(dark) => dark, - Err(err) => { + let (dark, at) = match self.readback.finish() { + None => return, + Some(Ok(read)) => (judge(read.tiles.as_flattened()), read.at), + Some(Err(err)) => { tracing::warn!(%err, "cannot read a capture back to see if it went dark"); return; } @@ -349,6 +284,7 @@ impl Probe { #[cfg(test)] mod tests { use super::*; + use crate::readback::INTERVAL; /// `tiles` tiles of one color, its brightest channel as given. fn plain(rgb: [u8; 3], brightest: u8) -> Vec { @@ -485,10 +421,10 @@ mod tests { ) { probe.changed(); probe.step(prober, effects, &capture.texture, true, at); - assert!(probe.reading.is_some(), "a read started"); + assert!(probe.readback.busy(), "a read started"); device.poll(wgpu::PollType::wait_indefinitely()).unwrap(); probe.step(prober, effects, &capture.texture, true, at); - assert!(probe.reading.is_none(), "the read came back"); + assert!(!probe.readback.busy(), "the read came back"); } /// The shader reads every texel: one line of light across a black 1080p @@ -551,7 +487,7 @@ mod tests { // Not read again without a new picture. probe.step(&prober, &effects, &capture.texture, true, at(dark + INTERVAL / 2)); - assert!(probe.reading.is_none()); + assert!(!probe.readback.busy()); // A capture that stops being live, such as one reopening, is not dark, // and forgets its last good picture: reopened dark, it holds nothing @@ -574,11 +510,11 @@ mod tests { capture.show([200, 40, 40], None); probe.changed(); probe.step(&prober, &effects, &capture.texture, true, at(before)); - assert!(probe.reading.is_some(), "a read started"); + assert!(probe.readback.busy(), "a read started"); probe.step(&prober, &effects, &capture.texture, false, at(before)); device.poll(wgpu::PollType::wait_indefinitely()).unwrap(); probe.step(&prober, &effects, &capture.texture, true, at(before)); - assert!(probe.reading.is_none(), "the read came back"); + assert!(!probe.readback.busy(), "the read came back"); let after = before + INTERVAL; capture.show([0, 0, 0], None); read(&mut probe, &prober, &effects, &device, &capture, at(after)); @@ -618,14 +554,14 @@ mod tests { // Live before its first picture, the new feed's layer still has the // one from before, which is not read for it. probe.step(&prober, &effects, &capture.texture, true, at(INTERVAL * 2)); - assert!(probe.reading.is_none(), "the picture from before is not read"); + assert!(!probe.readback.busy(), "the picture from before is not read"); // A read under way as the feed starts again is from before. let before = INTERVAL * 2; capture.show([200, 40, 40], None); probe.changed(); probe.step(&prober, &effects, &capture.texture, true, at(before)); - assert!(probe.reading.is_some(), "a read started"); + assert!(probe.readback.busy(), "a read started"); probe.follow(3); device.poll(wgpu::PollType::wait_indefinitely()).unwrap(); let after = before + INTERVAL; diff --git a/rs/moq-scene/src/lib.rs b/rs/moq-scene/src/lib.rs index 997f492f..7ef205a6 100644 --- a/rs/moq-scene/src/lib.rs +++ b/rs/moq-scene/src/lib.rs @@ -63,6 +63,7 @@ mod overlay; pub mod place; mod playout; pub mod publish; +mod readback; pub mod replay; mod scale; pub mod source; diff --git a/rs/moq-scene/src/place.rs b/rs/moq-scene/src/place.rs index 6e339334..8946f7f2 100644 --- a/rs/moq-scene/src/place.rs +++ b/rs/moq-scene/src/place.rs @@ -10,7 +10,6 @@ //! otherwise only once its place has been the worst for a while, gliding //! there. A move by hand holds until the content changes. -use std::sync::{Arc, Mutex}; use std::time::Duration; use kio::clock::Instant; @@ -19,9 +18,10 @@ use crate::collection::{Canvas, Item, KeepClear, Rect, mix}; use crate::compositor::Target; use crate::effect::Effects; use crate::glide::Glide; +use crate::readback::Readback; /// How often the picture under an item is read, at most. -pub const INTERVAL: Duration = Duration::from_millis(250); +pub const INTERVAL: Duration = crate::readback::INTERVAL; /// How long after the read before a read follows a gap, as when its scene /// was off or the item hidden: it compares with no picture, and a change of @@ -42,8 +42,8 @@ const SIDE: u32 = 512; /// A tile's side in pixels of the picture read, as `place.wgsl`'s workgroup. const TILE: u32 = 16; -/// The bytes of the largest grid, four a tile. -const BYTES: u64 = ((SIDE / TILE) * (SIDE / TILE) * 4) as u64; +/// How many tiles the largest grid has. +const COUNT: u32 = (SIDE / TILE) * (SIDE / TILE); /// How long what matters lingers once it stops, so one busy moment is never /// a reason to move. @@ -946,7 +946,7 @@ impl Reducer { ty: wgpu::BindingType::Buffer { ty: wgpu::BufferBindingType::Storage { read_only: false }, has_dynamic_offset: false, - min_binding_size: wgpu::BufferSize::new(BYTES), + min_binding_size: wgpu::BufferSize::new(COUNT as u64 * 4), }, count: None, }, @@ -969,76 +969,44 @@ impl Reducer { } } -/// A read under way: when it was taken, its picture's size, whether it had -/// one before to compare with, and the buffer's mapping once the GPU is done. -struct Reading { - at: Instant, +/// What a read is of: its picture's size, and whether it had one before to +/// compare with. +struct Of { size: (u32, u32), compared: bool, - mapped: Arc>>>, - /// Whether it is of what the scene showed before it went on again, so it - /// is dropped once it comes back. - dropped: bool, } /// One item's reads of the picture under it. pub(crate) struct Look { device: wgpu::Device, - queue: wgpu::Queue, /// The pictures read, by turns: the next read draws into the one the read /// before last did, and compares with the other. pictures: [Option; 2], /// Which of `pictures` the next read draws into. next: usize, - tiles: wgpu::Buffer, - readback: wgpu::Buffer, - reading: Option, - /// When the last read was taken. - last: Option, + readback: Readback, } impl Look { pub fn new(device: &wgpu::Device, queue: &wgpu::Queue) -> Self { - let tiles = device.create_buffer(&wgpu::BufferDescriptor { - label: Some("moq-scene place tiles"), - size: BYTES, - usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC, - mapped_at_creation: false, - }); - let readback = device.create_buffer(&wgpu::BufferDescriptor { - label: Some("moq-scene place tiles"), - size: BYTES, - usage: wgpu::BufferUsages::COPY_DST | wgpu::BufferUsages::MAP_READ, - mapped_at_creation: false, - }); Self { device: device.clone(), - queue: queue.clone(), pictures: [None, None], next: 0, - tiles, - readback, - reading: None, - last: None, + readback: Readback::new(device, queue, COUNT), } } /// Forget the reads so far, as when the scene goes on again: the next /// read compares with no picture, and one under way is dropped. pub fn restart(&mut self) { - self.last = None; - if let Some(reading) = &mut self.reading { - reading.dropped = true; - } + self.readback.restart(); } /// Whether a read may start at `now`: none is under way, and the last /// was at least [`INTERVAL`] ago. pub fn due(&self, now: Instant) -> bool { - self.reading.is_none() - && self - .last - .is_none_or(|last| now.saturating_duration_since(last) >= INTERVAL) + self.readback.due(now) } /// What the next read draws the picture under the item into, `canvas` @@ -1073,7 +1041,10 @@ impl Look { /// [`GAP`], and read the tiles back. pub fn start(&mut self, reducer: &Reducer, now: Instant) { let read = self.pictures[self.next].as_ref().expect("drawn into"); - let recent = self.last.is_some_and(|last| now.saturating_duration_since(last) <= GAP); + let recent = self + .readback + .last() + .is_some_and(|last| now.saturating_duration_since(last) <= GAP); let before = self.pictures[1 - self.next].as_ref().filter(|_| recent); let compared = before.is_some(); let view = |target: &Target| target.texture.create_view(&Default::default()); @@ -1091,7 +1062,7 @@ impl Look { }, wgpu::BindGroupEntry { binding: 2, - resource: self.tiles.as_entire_binding(), + resource: self.readback.binding(), }, ], }); @@ -1106,55 +1077,24 @@ impl Look { pass.set_bind_group(0, &bind, &[]); pass.dispatch_workgroups(size.0.div_ceil(TILE), size.1.div_ceil(TILE), 1); } - let bytes = u64::from(size.0.div_ceil(TILE) * size.1.div_ceil(TILE) * 4); - encoder.copy_buffer_to_buffer(&self.tiles, 0, &self.readback, 0, bytes); - self.queue.submit([encoder.finish()]); - - let mapped = Arc::new(Mutex::new(None)); - let done = mapped.clone(); - // Called from a later submit or poll, once the GPU is done. - self.readback.map_async(wgpu::MapMode::Read, .., move |result| { - *done.lock().unwrap() = Some(result); - }); - self.reading = Some(Reading { - at: now, - size, - compared, - mapped, - dropped: false, - }); - self.last = Some(now); + let count = size.0.div_ceil(TILE) * size.1.div_ceil(TILE); + self.readback.submit(encoder, count, now, Of { size, compared }); self.next = 1 - self.next; } /// The read under way, once it came back, and when it was taken; a /// dropped one comes back as nothing. pub fn finish(&mut self) -> Option> { - let reading = self.reading.as_ref()?; - let mapped = reading.mapped.lock().unwrap().take()?; - let Reading { - at, - size, - compared, - dropped, - .. - } = self.reading.take().expect("checked above"); - if dropped { - if mapped.is_ok() { - self.readback.unmap(); - } - return None; - } - let count = (size.0.div_ceil(TILE) * size.1.div_ceil(TILE)) as usize; - let read = mapped.map_err(|err| err.to_string()).and_then(|()| { - let tiles = match self.readback.slice(..).get_mapped_range() { - Ok(view) => Ok(view.as_chunks::<4>().0[..count].to_vec()), - Err(err) => Err(format!("{err:?}")), + let read = self.readback.finish()?; + Some(read.map(|read| { + let Of { size, compared } = read.of; + let tiles = Tiles { + size, + compared, + tiles: read.tiles, }; - self.readback.unmap(); - tiles - }); - Some(read.map(|tiles| (Tiles { size, compared, tiles }, at))) + (tiles, read.at) + })) } } diff --git a/rs/moq-scene/src/readback.rs b/rs/moq-scene/src/readback.rs new file mode 100644 index 00000000..682034a6 --- /dev/null +++ b/rs/moq-scene/src/readback.rs @@ -0,0 +1,151 @@ +//! A small picture read back from the GPU without stalling a frame. +//! +//! A compute pass reduces a picture to a grid of RGBA8 tiles in a storage +//! buffer, which is copied to a buffer mapped for reading and comes back a +//! frame or so later, at most every [`INTERVAL`]. A read that fails is +//! dropped, as is one whose picture stopped mattering while under way. + +use std::sync::{Arc, Mutex}; +use std::time::Duration; + +use kio::clock::Instant; + +/// How often a picture is read, at most. +pub const INTERVAL: Duration = Duration::from_millis(250); + +/// A read that came back: its tiles, row by row, when its picture was taken, +/// and what the reader said it was of. +pub(crate) struct Read { + pub tiles: Vec<[u8; 4]>, + pub at: Instant, + pub of: T, +} + +/// A read under way. +struct Reading { + at: Instant, + of: T, + count: usize, + mapped: Arc>>>, + /// Whether what it reads stopped mattering, so it is dropped once it + /// comes back. + stale: bool, +} + +/// One reader's tiles on the GPU and the read under way, if any. +pub(crate) struct Readback { + queue: wgpu::Queue, + tiles: wgpu::Buffer, + mapped: wgpu::Buffer, + reading: Option>, + /// When the last read was taken. + last: Option, +} + +impl Readback { + /// Room for a grid of up to `tiles` tiles. + pub fn new(device: &wgpu::Device, queue: &wgpu::Queue, tiles: u32) -> Self { + let size = u64::from(tiles) * 4; + let buffer = |usage| { + device.create_buffer(&wgpu::BufferDescriptor { + label: Some("moq-scene readback"), + size, + usage, + mapped_at_creation: false, + }) + }; + Self { + queue: queue.clone(), + tiles: buffer(wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC), + mapped: buffer(wgpu::BufferUsages::COPY_DST | wgpu::BufferUsages::MAP_READ), + reading: None, + last: None, + } + } + + /// The storage buffer the compute pass writes the tiles to. + pub fn binding(&self) -> wgpu::BindingResource<'_> { + self.tiles.as_entire_binding() + } + + /// When the last read was taken, if one was since the last + /// [`restart`](Self::restart). + pub fn last(&self) -> Option { + self.last + } + + /// Whether a read is under way. + #[cfg(test)] + pub fn busy(&self) -> bool { + self.reading.is_some() + } + + /// Whether a read may start at `now`: none is under way, and the last + /// was at least [`INTERVAL`] ago. + pub fn due(&self, now: Instant) -> bool { + self.reading.is_none() + && self + .last + .is_none_or(|last| now.saturating_duration_since(last) >= INTERVAL) + } + + /// Drop the read under way once it comes back. + pub fn discard(&mut self) { + if let Some(reading) = &mut self.reading { + reading.stale = true; + } + } + + /// Forget the reads so far: the read under way is dropped, and the next + /// is due at once. + pub fn restart(&mut self) { + self.discard(); + self.last = None; + } + + /// Submit `encoder`, whose compute pass wrote `count` tiles of a picture + /// taken at `at`, and read them back. + pub fn submit(&mut self, mut encoder: wgpu::CommandEncoder, count: u32, at: Instant, of: T) { + encoder.copy_buffer_to_buffer(&self.tiles, 0, &self.mapped, 0, u64::from(count) * 4); + self.queue.submit([encoder.finish()]); + let mapped = Arc::new(Mutex::new(None)); + let done = mapped.clone(); + // Called from a later submit or poll, once the GPU is done. + self.mapped.map_async(wgpu::MapMode::Read, .., move |result| { + *done.lock().unwrap() = Some(result); + }); + self.reading = Some(Reading { + at, + of, + count: count as usize, + mapped, + stale: false, + }); + self.last = Some(at); + } + + /// The read under way, once it came back; a stale one comes back as + /// nothing. + pub fn finish(&mut self) -> Option, String>> { + let reading = self.reading.as_ref()?; + let mapped = reading.mapped.lock().unwrap().take()?; + let Reading { + at, of, count, stale, .. + } = self.reading.take().expect("checked above"); + if stale { + if mapped.is_ok() { + self.mapped.unmap(); + } + return None; + } + let tiles = mapped.map_err(|err| err.to_string()).and_then(|()| { + let tiles = match self.mapped.slice(..).get_mapped_range() { + Ok(view) => Ok(view.as_chunks::<4>().0[..count].to_vec()), + Err(err) => Err(format!("{err:?}")), + }; + self.mapped.unmap(); + tiles + }); + Some(tiles.map(|tiles| Read { tiles, at, of })) + } +} From 3cc8a8703d68697c2dee4a4175fe4912386e96ce Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 19 Nov 2026 07:20:32 -0800 Subject: [PATCH 3/3] docs: rewrap the dark probe's step comment Co-Authored-By: Claude Opus 5.5 --- rs/moq-scene/src/dark.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/rs/moq-scene/src/dark.rs b/rs/moq-scene/src/dark.rs index 05ca6e35..40c7d20e 100644 --- a/rs/moq-scene/src/dark.rs +++ b/rs/moq-scene/src/dark.rs @@ -178,9 +178,10 @@ impl Probe { /// Judge the read that came back, if one did, and read `picture`, the /// capture's newest, if it is new and the last read was at least - /// [`INTERVAL`](crate::readback::INTERVAL) ago. `picture` needs `COPY_SRC`. A camera or window that - /// stops delivering is not read, though a Linux screen capture repeats - /// its last picture while it stands still, and is read each time. + /// [`INTERVAL`](crate::readback::INTERVAL) ago. `picture` needs + /// `COPY_SRC`. A camera or window that stops delivering is not read, + /// though a Linux screen capture repeats its last picture while it stands + /// still, and is read each time. /// /// A capture that is not `live`, such as one reopening, is not dark, and /// forgets its last good picture and what it is reading, so nothing from