diff --git a/doc/bin/studio.md b/doc/bin/studio.md index a64b9830f4c..766c8f5b2df 100644 --- a/doc/bin/studio.md +++ b/doc/bin/studio.md @@ -100,6 +100,11 @@ with Opus stereo audio, under the `--broadcast` path. Timestamps come from the studio's own clock, so a source that stalls repeats its last picture instead of holding up the stream. -Video is only encoded while someone watches it. Each frame is read back from -the GPU to the CPU for the encoder, which the Output panel counts; frames the -encoder cannot keep up with are dropped there rather than delaying the stream. +Video is only encoded while someone watches it. A hardware encoder that reads +the GPU (NVENC on Linux, VideoToolbox on macOS, Media Foundation on Windows) +takes each frame there when it can read the GPU the studio draws on. When it +cannot, such as NVENC while the studio draws on a laptop's integrated GPU, the +studio passes over it for an encoder that reads each frame back to the CPU +(the opt-in `vaapi` or `openh264` features), which the Output panel counts. +Frames the encoder cannot keep up with are dropped rather than delaying the +stream. diff --git a/quest/m1/README.md b/quest/m1/README.md index 4dbd4eec8a1..57b7d4ff4f2 100644 --- a/quest/m1/README.md +++ b/quest/m1/README.md @@ -105,7 +105,7 @@ cut ships a relay that compiles no C. It follows moq-quic's crypto reshape, whose surface its QUIC adapter plugs into. Added 2026-10-25 (maintainer): the studio's -[GPU encode](/quest/m1/studio-gpu/encode.md) and +GPU encode and [source import](/quest/m1/studio-gpu/import.md) quests are in m1 so moq-video's GPU encode and import API changes, mainly encoders taking a GPU surface and zero-copy texture import, land before the cut. diff --git a/quest/m1/studio-gpu/README.md b/quest/m1/studio-gpu/README.md index 951a5708e6d..374b0587af2 100644 --- a/quest/m1/studio-gpu/README.md +++ b/quest/m1/studio-gpu/README.md @@ -23,7 +23,6 @@ test each platform's path on that platform's CI. ## Required -- [Zero-copy program encode](/quest/m1/studio-gpu/encode.md) - the program reaches the encoder as a GPU surface instead of a CPU readback - [Zero-copy source import](/quest/m1/studio-gpu/import.md) - captured and decoded frames reach the compositor as GPU textures, with uploads counted ## Related diff --git a/quest/m1/studio-gpu/encode.md b/quest/m1/studio-gpu/encode.md deleted file mode 100644 index dfd18de62d0..00000000000 --- a/quest/m1/studio-gpu/encode.md +++ /dev/null @@ -1,36 +0,0 @@ -# [L] Zero-copy program encode - -## Goal - -`moq-studio`'s program reaches moq-video's encoder as a GPU `Surface` on -NVENC, VideoToolbox, and Media Foundation, instead of the CPU readback every -frame takes today. Any other backend keeps the readback, which the Output -panel already counts. - -## Plan - -- The compositor's program texture is allocated as an exportable image on - the device the window and sources share, then handed to the encoder - without leaving the GPU. Each encoder below already accepts its surface; - the work is the export and the API that carries it: - - NVENC: a `moq_video::frame::vulkan` slot (opaque-fd memory and a timeline - semaphore), the path the NVDEC to NVENC transcode already uses. - - VideoToolbox: an IOSurface-backed `CVPixelBuffer` the Metal texture aliases. - - Media Foundation: a shared D3D11 texture. -- This reaches below `wgpu` (its `as_hal` escape hatch), so keep the export in - moq-video or moq-scene, never in the app. -- Shape the export so the Vulkan slot also serves VA-API and AMD Vulkan - Video later without an API change; wiring those backends is m2 work. -- **Tests:** extend `just rs render-sw`'s end-to-end test (a red canvas - published to a test relay and decoded) to assert the readback count, which - is nonzero there. VideoToolbox and Media Foundation assert no readback on - their platforms' CI, and NVENC in an ignored hardware test run by - `just rs gpu`. - -## Related - -- [Zero-copy source import](/quest/m1/studio-gpu/import.md) - the other half of keeping the studio's pictures on the GPU -- [VA-API encodes an external Vulkan image](/quest/m2/vaapi-vulkan-import.md) - the follow-up that gives Intel the same Vulkan slot -- [Vulkan Video encode on AMD](/quest/m2/vulkan-encode.md) - the follow-up that gives AMD the same Vulkan slot -- [NVENC reuses its input and output buffers](/quest/m2/nvenc-pool.md) - per-frame NVENC allocation the program encode also pays -- [Hardware validation](/quest/m3/video-hardware.md) - capture and zero-copy paths not yet run on real machines diff --git a/quest/m1/studio-gpu/import.md b/quest/m1/studio-gpu/import.md index aceb86e3ce7..3b0e3e9cb32 100644 --- a/quest/m1/studio-gpu/import.md +++ b/quest/m1/studio-gpu/import.md @@ -31,7 +31,6 @@ the studio counts uploads per source next to its readback stats. ## Related -- [Zero-copy program encode](/quest/m1/studio-gpu/encode.md) - the other half of keeping the studio's pictures on the GPU - [Validate PipeWire DMA-BUFs into the Vulkan renderer](/quest/m2/2819-moq-video-carry-pipewire-dma-bufs-safely-into-the-vulkan.md) - the hardware proof of the Linux import path - [Direct3D11 render import](/quest/m2/render-d3d11.md) - the follow-up that brings Windows sources onto the GPU - [VAAPI encode and decode](/quest/m2/video-vaapi.md) - decoded DMA-BUF surfaces the renderer imports diff --git a/quest/m3/studio/README.md b/quest/m3/studio/README.md index 4b7de4afbee..89f3dcac8b1 100644 --- a/quest/m3/studio/README.md +++ b/quest/m3/studio/README.md @@ -60,14 +60,13 @@ OBS multistream, and the hosted version is product work outside this repository. Added 2026-10-25 (maintainer): the studio's -[GPU encode](/quest/m1/studio-gpu/encode.md) and +GPU encode and [source import](/quest/m1/studio-gpu/import.md) quests are in m1 so moq-video's GPU encode and import API changes, mainly encoders taking a GPU surface and zero-copy texture import, land before the cut. ## Required -- [Zero-copy program encode](/quest/m1/studio-gpu/encode.md) - the program reaches the encoder as a GPU surface instead of a CPU readback - [Zero-copy source import](/quest/m1/studio-gpu/import.md) - captured and decoded frames reach the compositor as GPU textures instead of a CPU upload - [Studio on macOS and Windows](/quest/m3/studio/app.md) - the studio runs on macOS and Windows, not only Linux - [Destinations](/quest/m3/studio/destinations.md) - RTMP, RTMPS, and WHIP push with per-destination codecs and shared encoders, on a UI-free egress library a hosted service can reuse diff --git a/quest/m3/studio/app.md b/quest/m3/studio/app.md index a9f1da42464..dca65d82255 100644 --- a/quest/m3/studio/app.md +++ b/quest/m3/studio/app.md @@ -19,5 +19,4 @@ landed. ## Related -- [Zero-copy program encode](/quest/m1/studio-gpu/encode.md) - the GPU encode path, which lands without this run - [Hardware validation](/quest/m3/video-hardware.md) - capture paths not yet run on real machines diff --git a/quest/m3/video-hardware.md b/quest/m3/video-hardware.md index 814e4cf4705..7237514ac2e 100644 --- a/quest/m3/video-hardware.md +++ b/quest/m3/video-hardware.md @@ -21,6 +21,13 @@ by [VA-API external images](/quest/m2/vaapi-vulkan-import.md). - **Windows Media Foundation capture**: on-demand open and close, so the camera LED is off when nobody is watching, and NV12 delivery from MJPEG and YUY2 cameras. +- **The studio's program on a hardware encoder**: run the ignored + `render::export::dx12::tests::exports_an_nv12_texture` (the Platform runner + has only WARP, which has no Direct3D11 video device) and + `render::export::dx12::tests::media_foundation_encodes_the_export_in_place` + on a Windows GPU with a Media Foundation H.264 encoder, and `moq-studio` + publishing on that machine and a Mac, confirming the Output panel reports + no readback. - **A live camera run per platform**: capture needs device permission that a headless or agent process cannot grant itself. - **OBS follows its grant on a shaped uplink**: stream from a real OBS build diff --git a/rs/justfile b/rs/justfile index 9bdb0b4a52a..e44e76217d1 100644 --- a/rs/justfile +++ b/rs/justfile @@ -99,6 +99,7 @@ windows $BASE="": windows-test: just rs test --manifest-path rs/moq-auth/Cargo.toml just rs test -p hang -p moq-tokio -p moq-sock -p moq-relay + just rs test -p moq-video --no-default-features --features render -E 'test(/^render::export::/)' # Run io_uring tests when the branch affects the relay or its dependencies. [positional-arguments] @@ -141,6 +142,7 @@ macos-test: just rs test -p hang -p moq-tokio -p moq-sock just rs test -p moq-sock --features fast-apple-datapath just rs test -p moq-video --no-default-features -E 'test(encode::backend::videotoolbox::tests)' + just rs test -p moq-video --no-default-features --features render -E 'test(/^render::export::/)' # Compile and lint the capture and playback features, which the default check skips. [positional-arguments] @@ -368,6 +370,9 @@ render-sw: LD_LIBRARY_PATH="$loader/lib" VK_DRIVER_FILES="${icd[0]}" \ just rs test -p moq-video --features render --lib --run-ignored only \ -E 'test(/^render::renderer::tests::(the_declared_color_space_beats_the_size_guess|cpu_frames_survive_the_round_trip|config_size_overrides_the_frame_size)$/)' + # Mesa's software driver exports no memory, so this covers the readback route. + LD_LIBRARY_PATH="$loader/lib" VK_DRIVER_FILES="${icd[0]}" \ + just rs test -p moq-video --features render,openh264 --lib --run-ignored only -E 'test(/^render::export::(nv12::)?tests::/)' LD_LIBRARY_PATH="$loader/lib" VK_DRIVER_FILES="${icd[0]}" \ just rs test -p moq-scene --lib --run-ignored only -E 'test(/^compositor::tests::/)' LD_LIBRARY_PATH="$loader/lib" VK_DRIVER_FILES="${icd[0]}" \ diff --git a/rs/moq-scene/src/compositor.rs b/rs/moq-scene/src/compositor.rs index e509e8522db..be96f4b25b0 100644 --- a/rs/moq-scene/src/compositor.rs +++ b/rs/moq-scene/src/compositor.rs @@ -13,7 +13,7 @@ use crate::collection::{Canvas, Collection, Rect, Source}; use crate::source::{Picture, Sources}; /// The format of every canvas texture: gamma-encoded RGBA, what -/// `moq_video::render` produces and what an encoder reads back. +/// `moq_video::render` produces and what its exporter hands an encoder. pub const FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::Rgba8Unorm; /// One draw's uniform: where on the canvas, in clip space, and how opaque. @@ -277,7 +277,8 @@ impl Compositor { } /// The program output, as of the last [`render`](Self::render). Created - /// with `COPY_SRC` so an encoder can read it back. + /// with `COPY_SRC | TEXTURE_BINDING`, what + /// `moq_video::render::export::Exporter::copy` takes to hand it to an encoder. pub fn program(&self) -> &wgpu::Texture { &self.program.texture } @@ -632,11 +633,8 @@ impl Uniforms { #[cfg(test)] mod tests { - use moq_net::Timestamp; - use super::*; use crate::collection::{Item, Rgba, Scene, Source}; - use crate::readback::Readback; use crate::source::test::{context, local, start}; const RED: [u8; 4] = [255, 0, 0, 255]; @@ -701,7 +699,8 @@ mod tests { collection: Collection, sources: Sources, compositor: Compositor, - readback: Readback, + device: wgpu::Device, + queue: wgpu::Queue, _origin: moq_net::origin::Producer, _dir: tempfile::TempDir, } @@ -716,7 +715,8 @@ mod tests { start(&sources, drivers).await; Self { compositor: Compositor::new(&device, &queue, collection.canvas).unwrap(), - readback: Readback::new(&device, &queue, collection.canvas), + device, + queue, collection, sources, _origin: origin, @@ -728,8 +728,39 @@ mod tests { self.compositor.render(&self.collection, &self.sources, now).unwrap(); } + /// The canvas texture's exact RGBA rows: no 4:2:0 round trip, so a test + /// compares the stored values. fn read(&mut self, texture: &wgpu::Texture) -> Vec { - self.readback.copy(texture, Timestamp::ZERO).unwrap().rgba().unwrap() + let canvas = self.compositor.canvas(); + let row = (canvas.width * 4).next_multiple_of(wgpu::COPY_BYTES_PER_ROW_ALIGNMENT); + let buffer = self.device.create_buffer(&wgpu::BufferDescriptor { + label: None, + size: u64::from(row * canvas.height), + usage: wgpu::BufferUsages::COPY_DST | wgpu::BufferUsages::MAP_READ, + mapped_at_creation: false, + }); + let mut encoder = self.device.create_command_encoder(&Default::default()); + encoder.copy_texture_to_buffer( + texture.as_image_copy(), + wgpu::TexelCopyBufferInfo { + buffer: &buffer, + layout: wgpu::TexelCopyBufferLayout { + offset: 0, + bytes_per_row: Some(row), + rows_per_image: None, + }, + }, + texture.size(), + ); + self.queue.submit([encoder.finish()]); + buffer.map_async(wgpu::MapMode::Read, .., |result| result.unwrap()); + self.device.poll(wgpu::PollType::wait_indefinitely()).unwrap(); + let width = canvas.width as usize * 4; + let mapped = buffer.slice(..).get_mapped_range().unwrap(); + mapped + .chunks_exact(row as usize) + .flat_map(|row| row[..width].to_vec()) + .collect() } fn program(&mut self) -> Vec { @@ -911,26 +942,4 @@ mod tests { }) .await; } - - /// A slow encoder runs out of buffers and drops pictures rather than - /// queuing them, and a finished copy frees its buffer. - #[tokio::test] - #[ignore = "needs a Vulkan device; run by just rs render-sw"] - async fn readback_drops_past_its_depth() { - local(async { - let mut studio = Studio::new(collection()).await; - studio.render(Instant::now()); - let program = studio.compositor.program().clone(); - let mut held: Vec<_> = (0..Readback::DEPTH) - .map(|_| studio.readback.copy(&program, Timestamp::ZERO).unwrap()) - .collect(); - assert!(studio.readback.copy(&program, Timestamp::ZERO).is_none()); - - let frame = held.pop().unwrap().wait().unwrap(); - assert_eq!(frame.size(), moq_video::Size::new(8, 8)); - held.push(studio.readback.copy(&program, Timestamp::ZERO).unwrap()); - assert!(studio.readback.copy(&program, Timestamp::ZERO).is_none()); - }) - .await; - } } diff --git a/rs/moq-scene/src/lib.rs b/rs/moq-scene/src/lib.rs index 50e1a5338fc..79614db3ed9 100644 --- a/rs/moq-scene/src/lib.rs +++ b/rs/moq-scene/src/lib.rs @@ -12,8 +12,8 @@ //! compositor and sound goes to a `moq_audio::bus::Bus`, the program audio. //! - [`Compositor`] draws the scene in preview and the scene on program into //! textures on the caller's `wgpu` device, cutting or fading between them. -//! - [`Readback`] copies the program back to the CPU for an encoder that cannot -//! take a GPU surface. +//! `moq_video::render::export` hands the program to an encoder, on the GPU +//! where the platform allows. //! //! Every picture is drawn on the program clock the caller ticks //! [`Compositor::render`] with, from whatever each source delivered last: a @@ -22,12 +22,10 @@ mod collection; mod compositor; -mod readback; pub mod source; pub use collection::{Audio, Canvas, Collection, Item, Rect, Rgba, Scene, Source, Transition}; pub use compositor::{Compositor, FORMAT}; -pub use readback::{Pending, Readback}; /// The `wgpu` the compositor draws with: `moq_video`'s, so its textures and /// the decoder's are the same type. @@ -63,10 +61,6 @@ pub enum Error { #[error("unsupported source: {0}")] Unsupported(String), - /// The GPU failed while reading a picture back. - #[error("gpu: {0}")] - Gpu(String), - /// Capturing, decoding, or drawing video failed. #[error(transparent)] Video(#[from] moq_video::Error), diff --git a/rs/moq-scene/src/readback.rs b/rs/moq-scene/src/readback.rs deleted file mode 100644 index 0ba7c9a7202..00000000000 --- a/rs/moq-scene/src/readback.rs +++ /dev/null @@ -1,190 +0,0 @@ -//! Copy canvas pictures back to the CPU, for an encoder that cannot take a GPU -//! surface. - -use std::sync::{Arc, Mutex}; - -use moq_net::Timestamp; -use moq_video::render::wgpu; - -use crate::Error; -use crate::collection::Canvas; - -/// Copies of a canvas texture on their way back to the CPU, through a small -/// pool of staging buffers. -/// -/// [`copy`](Self::copy) records the copy and returns at once; the [`Pending`] -/// it hands back waits for the GPU and converts the pixels, on whatever thread -/// the encoder runs on, so the canvas keeps drawing meanwhile. A copy that -/// finds every buffer still in flight is refused rather than queued, so an -/// encoder that falls behind drops pictures instead of adding latency. -pub struct Readback { - device: wgpu::Device, - queue: wgpu::Queue, - canvas: Canvas, - /// Bytes per row in a staging buffer, padded to the copy alignment. - row: u32, - pool: Arc>, -} - -/// Staging buffers not held by a [`Pending`]. -#[derive(Default)] -struct Pool { - free: Vec, - /// Buffers in existence, up to [`Readback::DEPTH`]. - allocated: usize, -} - -impl Readback { - /// Copies in flight at once: one being converted, one being copied, and one - /// spare so a slow conversion does not drop the very next picture. - pub const DEPTH: usize = 3; - - /// Read back `canvas`-sized textures from `device`. - pub fn new(device: &wgpu::Device, queue: &wgpu::Queue, canvas: Canvas) -> Self { - Self { - device: device.clone(), - queue: queue.clone(), - canvas, - row: (canvas.width * 4).next_multiple_of(wgpu::COPY_BYTES_PER_ROW_ALIGNMENT), - pool: Arc::default(), - } - } - - /// Start copying `texture`, a canvas-sized [`FORMAT`](crate::compositor::FORMAT) - /// texture created with `COPY_SRC`, to be encoded at `timestamp`. - /// - /// `None` when every buffer is still held by an earlier [`Pending`]. - pub fn copy(&mut self, texture: &wgpu::Texture, timestamp: Timestamp) -> Option { - let mut pool = self.pool.lock().unwrap(); - let buffer = match pool.free.pop() { - Some(buffer) => buffer, - None if pool.allocated < Self::DEPTH => { - pool.allocated += 1; - self.device.create_buffer(&wgpu::BufferDescriptor { - label: Some("moq-scene readback"), - size: u64::from(self.row) * u64::from(self.canvas.height), - usage: wgpu::BufferUsages::COPY_DST | wgpu::BufferUsages::MAP_READ, - mapped_at_creation: false, - }) - } - None => return None, - }; - drop(pool); - - let mut encoder = self.device.create_command_encoder(&wgpu::CommandEncoderDescriptor { - label: Some("moq-scene readback"), - }); - encoder.copy_texture_to_buffer( - wgpu::TexelCopyTextureInfo { - texture, - mip_level: 0, - origin: wgpu::Origin3d::ZERO, - aspect: wgpu::TextureAspect::All, - }, - wgpu::TexelCopyBufferInfo { - buffer: &buffer, - layout: wgpu::TexelCopyBufferLayout { - offset: 0, - bytes_per_row: Some(self.row), - rows_per_image: Some(self.canvas.height), - }, - }, - wgpu::Extent3d { - width: self.canvas.width, - height: self.canvas.height, - depth_or_array_layers: 1, - }, - ); - let submission = self.queue.submit([encoder.finish()]); - - Some(Pending { - device: self.device.clone(), - buffer: Some(buffer), - submission, - canvas: self.canvas, - row: self.row, - timestamp, - pool: self.pool.clone(), - mapping: false, - }) - } -} - -/// One copy in flight. Dropping it returns its buffer to the pool. -pub struct Pending { - device: wgpu::Device, - buffer: Option, - submission: wgpu::SubmissionIndex, - canvas: Canvas, - row: u32, - timestamp: Timestamp, - pool: Arc>, - /// Set while a mapping was requested and not yet undone, so a wait that - /// failed partway does not hand a mapped buffer to the next copy. - mapping: bool, -} - -impl Pending { - /// Block until the copy lands, then hand back the picture as a frame an - /// encoder takes. - pub fn wait(mut self) -> Result { - let rgba = self.rgba()?; - let surface = moq_video::Surface::rgba(&rgba, self.canvas.size())?; - Ok(moq_video::Frame::new(surface, self.timestamp)) - } - - /// Block until the copy lands, then hand back tightly packed RGBA rows. - pub fn rgba(&mut self) -> Result, Error> { - let buffer = self.buffer.as_ref().expect("held until drop"); - let (send, recv) = std::sync::mpsc::channel(); - self.mapping = true; - buffer.map_async(wgpu::MapMode::Read, .., move |result| { - let _ = send.send(result); - }); - self.device - .poll(wgpu::PollType::Wait { - submission_index: Some(self.submission.clone()), - timeout: None, - }) - .map_err(|err| Error::Gpu(format!("waiting for the readback: {err}")))?; - recv.recv() - .map_err(|_| Error::Gpu("the readback was dropped".into()))? - .map_err(|err| Error::Gpu(format!("mapping the readback: {err}")))?; - - let width = self.canvas.width as usize * 4; - let mut rgba = Vec::with_capacity(width * self.canvas.height as usize); - { - let mapped = buffer - .slice(..) - .get_mapped_range() - .map_err(|err| Error::Gpu(format!("reading the readback: {err}")))?; - for row in mapped.chunks_exact(self.row as usize) { - rgba.extend_from_slice(&row[..width]); - } - } - buffer.unmap(); - self.mapping = false; - Ok(rgba) - } - - /// When the picture is shown. - pub fn timestamp(&self) -> Timestamp { - self.timestamp - } -} - -impl Drop for Pending { - fn drop(&mut self) { - let Some(buffer) = self.buffer.take() else { - return; - }; - let mut pool = self.pool.lock().unwrap(); - // The pool only holds buffers the next copy can write. One a failed wait - // left mapped, or waiting on a mapping, is let go and replaced. - if self.mapping { - pool.allocated -= 1; - } else { - pool.free.push(buffer); - } - } -} diff --git a/rs/moq-studio/src/engine.rs b/rs/moq-studio/src/engine.rs index 15a20e819cd..56eb84372ec 100644 --- a/rs/moq-studio/src/engine.rs +++ b/rs/moq-studio/src/engine.rs @@ -5,7 +5,7 @@ //! - **program** draws preview and program once per canvas frame and hands the //! program to the encoder, stamped with the program clock. //! - **audio** reads the program bus on the same clock and publishes it. -//! - **encode** reads the program back from the GPU and encodes it. +//! - **encode** waits for each program copy and encodes it. //! - **source-***: one per running source, since a capture stream may not //! leave the thread that opened it. //! - The UI runs on the main thread and edits [`State`] under its lock. @@ -19,10 +19,11 @@ use anyhow::Context; use bytes::Bytes; use moq_clock::Instant; use moq_scene::source::{self, Driver, Sources}; -use moq_scene::{Collection, Compositor, Pending, Readback, wgpu}; +use moq_scene::{Collection, Compositor, wgpu}; +use moq_video::render::export::{Exporter, Pending, Route}; /// The GPU everything shares: sources upload to it, the compositor draws on -/// it, the window presents from it, and the encoder reads back from it. +/// it, the window presents from it, and the encoder reads the program from it. #[derive(Clone)] pub struct Gpu { pub instance: wgpu::Instance, @@ -41,15 +42,13 @@ impl Gpu { }) .await .context("no GPU adapter")?; - // Lets a hardware-decoded or captured DMA-BUF reach the compositor - // without a copy, where the driver can import one. - let features = adapter.features() & wgpu::Features::VULKAN_EXTERNAL_MEMORY_DMA_BUF; - let (device, queue) = adapter - .request_device(&wgpu::DeviceDescriptor { - label: Some("moq-studio"), - required_features: features, - ..Default::default() - }) + // Lets a hardware-decoded or captured frame reach the compositor, and the + // program reach the encoder, without a copy where the driver allows. + let descriptor = wgpu::DeviceDescriptor { + label: Some("moq-studio"), + ..Default::default() + }; + let (device, queue) = moq_video::render::request_device(&adapter, &descriptor) .await .context("cannot open the GPU")?; Ok(Self { @@ -126,11 +125,17 @@ pub fn load(path: &Path) -> anyhow::Result { pub struct Stats { /// Canvas frames drawn. pub rendered: AtomicU64, - /// Program frames copied back to the CPU for the encoder. + /// Program frames handed to the encoder. + pub copied: AtomicU64, + /// Microseconds the last one waited on the GPU, and on a readback converted + /// pixels. + pub copy_us: AtomicU64, + /// Whether the encoder reads the program on the GPU, rather than each frame + /// being read back to the CPU. + pub zero_copy: AtomicBool, + /// Program frames read back to the CPU for the encoder. pub read_back: AtomicU64, - /// Microseconds the last readback waited on the GPU and converted pixels. - pub readback_us: AtomicU64, - /// Program frames not encoded because the encoder still held every buffer. + /// Program frames not encoded because the encoder still held every copy. pub dropped: AtomicU64, /// Program frames encoded. pub encoded: AtomicU64, @@ -168,7 +173,6 @@ impl Drop for Output { /// The running studio. pub struct Engine { - pub gpu: Gpu, pub state: Mutex, pub stats: Stats, pub bus: moq_audio::bus::Bus, @@ -208,7 +212,7 @@ impl Engine { let (output, origin, publish) = match connect { Some((client, url)) => { - let (output, publish) = publish(client, url, subscribe, broadcast, canvas).await?; + let (output, publish) = publish(&gpu, client, url, subscribe, broadcast, canvas).await?; (Some(output), None, Some(publish)) } None => (None, Some(subscribe), None), @@ -223,7 +227,6 @@ impl Engine { stale: true, unsaved: false, }), - gpu, stats: Stats::default(), bus, output, @@ -235,16 +238,19 @@ impl Engine { video, audio, config, + exporter, clock, }) => { - // One more than the readback pool, for a pause behind a full pool. - let (frames, pending) = mpsc::sync_channel(Readback::DEPTH + 1); + let zero_copy = exporter.route() == Route::Gpu; + engine.stats.zero_copy.store(zero_copy, Ordering::Relaxed); + // One more than the exporter's pool, for a pause behind a full pool. + let (frames, pending) = mpsc::sync_channel(Exporter::DEPTH + 1); tokio::spawn(watch(video.demand(), engine.clone())); let encoder = engine.clone(); std::thread::Builder::new() .name("encode".into()) - .spawn(move || encode(&encoder, pending, video, config))?; - (Some(frames), Some(audio), Some(clock)) + .spawn(move || encode(&encoder, pending, video, config, zero_copy))?; + (Some((frames, exporter)), Some(audio), Some(clock)) } None => (None, None, None), }; @@ -271,11 +277,15 @@ impl Engine { /// Draw a canvas frame per tick of the program clock, and hand the program /// to the encoder while anyone watches, stamped `epoch` past `start`. /// - /// Drawing outlives the encoder: a failed output stops reading the program - /// back, and the preview and program keep moving. - async fn run_program(&self, start: Instant, epoch: Duration, mut frames: Option>) { + /// Drawing outlives the encoder: a failed output stops copying the program, + /// and the preview and program keep moving. + async fn run_program( + &self, + start: Instant, + epoch: Duration, + mut video: Option<(mpsc::SyncSender