From b3380d157bc9aeb9eac043e8f09f84518af49e68 Mon Sep 17 00:00:00 2001 From: enricobuehler Date: Thu, 5 Nov 2026 21:02:30 +0100 Subject: [PATCH 1/6] perf(video): hold NVDEC at its top clock above 720p nvtegra rescales NVDEC every 20 pictures so that one takes about a frame interval, with a 180 MHz floor up to 1080p. That is playback's rule. In a stream it left a 1080p picture 7.6 ms of decode, and with the VIC transfer and the copy the lane filled the 60 Hz frame: docked play fell behind. Above 720p the shim asks the decoder's own clock request for the top rate again after each rescale, and closing the decoder releases it. Handheld keeps the scaling: 720p fits there, and the clock costs battery. The nvdec line carries NVDEC's clock, so a console run shows what each window decoded at. --- crates/pf-nx-platform/src/video.rs | 32 +++++++++++++++--------- crates/pf-nx-probe/src/nvdec.rs | 2 +- crates/pf-nx-sys/csrc/pf_av.c | 39 ++++++++++++++++++++++++++++-- crates/pf-nx-sys/src/av.rs | 25 ++++++++++++------- crates/pf-nx-sys/tests/layout.rs | 5 ++-- 5 files changed, 78 insertions(+), 25 deletions(-) diff --git a/crates/pf-nx-platform/src/video.rs b/crates/pf-nx-platform/src/video.rs index 73748ec..ce41596 100644 --- a/crates/pf-nx-platform/src/video.rs +++ b/crates/pf-nx-platform/src/video.rs @@ -6,10 +6,11 @@ //! picture straight into the presenter's next staging buffer, which `commit` copies into a //! plane set and publishes (D7). On the console the picture goes through libavutil's own frame //! and the shim copies it; with [`NxVideo::straight`] the VIC writes into that staging buffer -//! itself where it can map it (WP5). The `nvdec[10s]` line names which route ran. On the -//! framebuffer display the pictures land in scratch planes -//! and are dropped, so the emulator still runs the hwaccel. A decoder error fails this feed, -//! which has the session ask for a keyframe, and the next feed starts from a flushed decoder. +//! itself where it can map it (WP5). Above 720p NVDEC is held at its top clock. The +//! `nvdec[10s]` line names which route ran and NVDEC's clock. On the framebuffer display the +//! pictures land in scratch planes and are dropped, so the emulator still runs the hwaccel. A +//! decoder error fails this feed, which has the session ask for a keyframe, and the next feed +//! starts from a flushed decoder. use std::collections::VecDeque; use std::sync::{Arc, Mutex}; @@ -79,20 +80,24 @@ impl VideoDecoders for NxVideo { spec.bit_depth, if spec.chroma444 { " 4:4:4" } else { "" } ); - let decoder = av::Decoder::open(codec, spec.width, spec.height, self.straight)?; + // nvtegra scales NVDEC down until a picture takes most of a frame interval: about 8 ms at + // 1080p, which with the transfer and the copy leaves a 60 Hz frame no slack. 720p fits. + let top_clock = spec.width * spec.height > 1280 * 720; + let decoder = av::Decoder::open(codec, spec.width, spec.height, self.straight, top_clock)?; let out = match &self.gpu { Some((dev, slot)) => Out::Gpu(Nv12Upload::new(dev.clone(), slot.clone(), spec)?), None => Out::scratch(spec), }; tracing::info!( - "{DECODER}: {codec:?} {}x{}, {}", + "{DECODER}: {codec:?} {}x{}, {}{}", spec.width, spec.height, if self.straight { "straight VIC route asked for" } else { "VIC then copy" - } + }, + if top_clock { ", NVDEC at its top clock" } else { "" } ); let started = Instant::now(); Ok(Box::new(NxSink { @@ -237,9 +242,9 @@ impl Lane { } /// Where the lane's time went over one [`TIMING_WINDOW`]: the wait for a free staging buffer, -/// the decoder's own time, the transfer to NV12, the shim's copy out of libavutil's frame (none -/// when the transfer wrote the staging planes itself), the upload's submit, and the staging -/// copy's time on the GPU. +/// the decoder's own time and clock, the transfer to NV12, the shim's copy out of libavutil's +/// frame (none when the transfer wrote the staging planes itself), the upload's submit, and the +/// staging copy's time on the GPU. struct Timing { since: Instant, pictures: u32, @@ -258,6 +263,8 @@ struct Timing { in_flight_max: u32, /// How the last picture reached the staging planes; `None` before the first. route: Option, + /// NVDEC's clock after the last picture, kHz. + nvdec_khz: u32, /// Pictures whose host PTS found no due time: they are shown at once, whatever the budget. undue: u32, /// The due time less the moment the picture came out of the decoder, µs, over the pictures @@ -286,6 +293,7 @@ impl Timing { gpu_copies: 0, in_flight_max: 0, route: None, + nvdec_khz: 0, undue: 0, slack_us: 0, slack_min_us: i64::MAX, @@ -339,6 +347,7 @@ impl Timing { ); } self.route = Some(s.route); + self.nvdec_khz = s.nvdec_khz; self.copy_us += u64::from(s.last_copy_us); self.copy_max_us = self.copy_max_us.max(s.last_copy_us); if let Some(us) = gpu_copy_us { @@ -366,7 +375,7 @@ impl Timing { .checked_div(u64::from(self.gpu_copies)) .map_or_else(|| "-".to_string(), |us| format!("{:.2}", us as f64 / 1000.0)); tracing::info!( - "nvdec[{}s]: {} pictures ({} dropped, {} decoded in all); staging wait mean {:.1}ms max {:.1}ms; decode mean {:.1}ms max {:.1}ms; transfer mean {:.1}ms max {:.1}ms; copy mean {:.1}ms max {:.1}ms; upload mean {:.2}ms; gpu copy mean {gpu_copy}ms; in flight max {}; route {}; {}", + "nvdec[{}s]: {} pictures ({} dropped, {} decoded in all); staging wait mean {:.1}ms max {:.1}ms; decode mean {:.1}ms max {:.1}ms at {:.1}MHz; transfer mean {:.1}ms max {:.1}ms; copy mean {:.1}ms max {:.1}ms; upload mean {:.2}ms; gpu copy mean {gpu_copy}ms; in flight max {}; route {}; {}", TIMING_WINDOW.as_secs(), self.pictures, self.dropped, @@ -375,6 +384,7 @@ impl Timing { self.wait_max.as_secs_f64() * 1000.0, ms(self.decode_us / n), ms(u64::from(self.decode_max_us)), + f64::from(self.nvdec_khz) / 1000.0, ms(self.transfer_us / n), ms(u64::from(self.transfer_max_us)), ms(self.copy_us / n), diff --git a/crates/pf-nx-probe/src/nvdec.rs b/crates/pf-nx-probe/src/nvdec.rs index 85d9734..87ebd24 100644 --- a/crates/pf-nx-probe/src/nvdec.rs +++ b/crates/pf-nx-probe/src/nvdec.rs @@ -90,7 +90,7 @@ fn decode(step: &str, codec: Codec, data: &[u8], out: &mut Out) -> Result #include +#ifdef __SWITCH__ +// nvtegra.h names libnx's NvChannel and MmuRequest without including them. +#include +#endif + #include #include #include +#ifdef __SWITCH__ +#include +#endif // The shim's own errors, outside FFmpeg's AVERROR space; src/av.rs names them. #define PF_AV_NOT_NV12 (-0x70660001) @@ -33,6 +41,8 @@ typedef struct PfAvStats { uint32_t route; // Why transfers stopped going straight into the caller's planes; 0 while they do. int32_t direct_err; + // NVDEC's clock after the last picture, kHz; 0 off the console. + uint32_t nvdec_khz; } PfAvStats; typedef struct PfAvPictureMeta { @@ -51,6 +61,9 @@ typedef struct PfAvDecoder { int64_t sent_at; uint64_t taken; PfAvStats stats; + // Hold NVDEC at its top clock; the clock last asked for, Hz. + int pin_clock; + uint32_t pinned_hz; } PfAvDecoder; void pf_av_close(PfAvDecoder *d) { @@ -66,9 +79,9 @@ void pf_av_close(PfAvDecoder *d) { // codec: 0 H.264, 1 HEVC. The hwaccel is found by name, so no header needs its enum. // `direct` non-zero tries the transfer straight into the caller's planes first; zero always -// goes through libavutil's frame and a copy. +// goes through libavutil's frame and a copy. `pin_clock` non-zero holds NVDEC at its top clock. // TODO(verify): the device type's name is "nvtegra" in the pinned tree (WP0.6). -int pf_av_open(int codec, int width, int height, int direct, PfAvDecoder **out) { +int pf_av_open(int codec, int width, int height, int direct, int pin_clock, PfAvDecoder **out) { *out = NULL; const AVCodec *c = avcodec_find_decoder(codec == 1 ? AV_CODEC_ID_HEVC : AV_CODEC_ID_H264); if (!c) return AVERROR_DECODER_NOT_FOUND; @@ -93,8 +106,10 @@ int pf_av_open(int codec, int width, int height, int direct, PfAvDecoder **out) #ifdef __SWITCH__ if (!(d->ctx->hw_device_ctx = av_buffer_ref(d->device)) || (direct && !(d->direct = av_frame_alloc()))) goto fail; + d->pin_clock = pin_clock; #else (void)direct; + (void)pin_clock; #endif // The default get_format picks the device's hw format when hw_device_ctx is set. d->ctx->width = width; @@ -126,6 +141,25 @@ int pf_av_send(PfAvDecoder *d, const uint8_t *data, int len, uint64_t pts) { return 0; } +// nvtegra rescales NVDEC every 20 pictures so one takes about a frame interval: playback's +// rule, which puts a 1080p picture at about 8 ms. Asking the top clock again after each +// rescale holds it there; closing the decoder releases the request. +static void hold_clock(PfAvDecoder *d) { +#ifdef __SWITCH__ + AVHWDeviceContext *dev = (AVHWDeviceContext *)d->device->data; + AVNVTegraChannel *ch = &((AVNVTegraDeviceContext *)dev->hwctx)->nvdec_channel; + if (d->pin_clock && ch->clock != d->pinned_hz) { + // nvtegra's own top request; mm rounds it down to the fastest step. + if (R_SUCCEEDED(mmuRequestSetAndWait(&ch->mmu_request, UINT32_MAX - 999, -1))) + mmuRequestGet(&ch->mmu_request, &ch->clock); + d->pinned_hz = ch->clock; + } + d->stats.nvdec_khz = ch->clock / 1000; +#else + (void)d; +#endif +} + // The caller's memory as a buffer the frame refers to; the caller owns it. static void keep(void *opaque, uint8_t *data) { (void)opaque; @@ -195,6 +229,7 @@ int pf_av_receive(PfAvDecoder *d, uint8_t *y, size_t y_stride, size_t y_len, uin if (err < 0) return err; d->stats.decoded++; d->stats.last_decode_us = (uint32_t)(av_gettime_relative() - d->sent_at); + hold_clock(d); meta->width = (uint32_t)d->hw->width; meta->height = (uint32_t)d->hw->height; meta->pts = (uint64_t)d->hw->pts; diff --git a/crates/pf-nx-sys/src/av.rs b/crates/pf-nx-sys/src/av.rs index 5e9ac96..fd7af47 100644 --- a/crates/pf-nx-sys/src/av.rs +++ b/crates/pf-nx-sys/src/av.rs @@ -44,6 +44,8 @@ pub struct Stats { /// Why transfers stopped going straight into the caller's planes, an `AVERROR`; 0 while /// they do. pub direct_err: i32, + /// NVDEC's clock after the last picture, kHz; 0 off the console. + pub nvdec_khz: u32, } /// How the last picture reached the caller's planes (`PF_AV_ROUTE_*`). @@ -97,6 +99,7 @@ pub struct RawStats { pub last_copy_us: u32, pub route: u32, pub direct_err: i32, + pub nvdec_khz: u32, } /// The shim's own codes, outside libavcodec's `AVERROR` space (`csrc/pf_av.c`). @@ -120,6 +123,7 @@ mod ffi { width: c_int, height: c_int, direct: c_int, + pin_clock: c_int, out: *mut *mut PfAvDecoder, ) -> c_int; pub fn pf_av_send(d: *mut PfAvDecoder, data: *const u8, len: c_int, pts: u64) -> c_int; @@ -172,12 +176,13 @@ unsafe impl Send for Decoder {} impl Decoder { /// Opens `codec` for a `width`×`height` stream. `straight` has the VIC write into the /// caller's planes ([`Route::Vic`]) where it can; otherwise every picture goes through - /// libavutil's frame and a copy ([`Route::VicThenCopy`]). - pub fn open(codec: Codec, width: u32, height: u32, straight: bool) -> anyhow::Result { + /// libavutil's frame and a copy ([`Route::VicThenCopy`]). `top_clock` holds NVDEC at its + /// fastest clock, where nvtegra would scale it down to just keep pace. + pub fn open(codec: Codec, width: u32, height: u32, straight: bool, top_clock: bool) -> anyhow::Result { let (w, h) = (i32::try_from(width)?, i32::try_from(height)?); let mut raw = core::ptr::null_mut(); // SAFETY: `raw` is a valid out-pointer; the shim sets it only on success. - let rc = unsafe { ffi::pf_av_open(codec as i32, w, h, i32::from(straight), &mut raw) }; + let rc = unsafe { ffi::pf_av_open(codec as i32, w, h, i32::from(straight), i32::from(top_clock), &mut raw) }; if rc < 0 { return Err(av_error(&format!("open the {codec:?} decoder"), rc)); } @@ -255,6 +260,7 @@ impl Decoder { last_copy_us: s.last_copy_us, route: Route::from_raw(s.route), direct_err: s.direct_err, + nvdec_khz: s.nvdec_khz, } } } @@ -269,7 +275,7 @@ impl Drop for Decoder { #[cfg(not(pf_av))] impl Decoder { - pub fn open(codec: Codec, _width: u32, _height: u32, _straight: bool) -> anyhow::Result { + pub fn open(codec: Codec, _width: u32, _height: u32, _straight: bool, _top_clock: bool) -> anyhow::Result { anyhow::bail!("open the {codec:?} decoder: no decoder on this box (NVDEC on the console, libavcodec on Linux)") } @@ -303,7 +309,7 @@ mod tests { #[cfg(not(pf_av))] #[test] fn open_fails_off_the_console() { - let err = Decoder::open(Codec::Hevc, 1920, 1080, false).err().unwrap(); + let err = Decoder::open(Codec::Hevc, 1920, 1080, false, false).err().unwrap(); assert_eq!( err.to_string(), "open the Hevc decoder: no decoder on this box (NVDEC on the console, libavcodec on Linux)" @@ -314,7 +320,7 @@ mod tests { #[test] fn a_dev_box_opens_both_codecs_in_software() { for codec in [Codec::H264, Codec::Hevc] { - let mut d = Decoder::open(codec, 1920, 1080, false).unwrap(); + let mut d = Decoder::open(codec, 1920, 1080, false, false).unwrap(); assert_eq!(d.stats(), Stats::default()); d.flush(); } @@ -325,14 +331,15 @@ mod tests { use core::mem::{offset_of, size_of}; // PfAvPictureMeta: u32 width, u32 height, u64 pts → 16. assert_eq!((size_of::(), offset_of!(PictureMeta, pts)), (16, 8)); - // PfAvStats: u64 decoded, five u32 and an i32 → 32. + // PfAvStats: u64 decoded, five u32, an i32 and a u32 → 36, padded to 40. assert_eq!( ( size_of::(), offset_of!(RawStats, last_transfer_us), - offset_of!(RawStats, direct_err) + offset_of!(RawStats, direct_err), + offset_of!(RawStats, nvdec_khz) ), - (32, 16, 28) + (40, 16, 28, 32) ); assert_eq!(Codec::Hevc as i32, 1); } diff --git a/crates/pf-nx-sys/tests/layout.rs b/crates/pf-nx-sys/tests/layout.rs index 703e94f..7799e4a 100644 --- a/crates/pf-nx-sys/tests/layout.rs +++ b/crates/pf-nx-sys/tests/layout.rs @@ -117,8 +117,9 @@ fn firmware_version() { #[test] fn shim_structs() { - // PfAvPictureMeta: u32, u32, u64 → 16. PfAvStats: u64, 6 × 32-bit → 32. + // PfAvPictureMeta: u32, u32, u64 → 16. PfAvStats: u64, 7 × 32-bit → 36, padded to 40. assert_eq!(layout::(), (16, 8)); - assert_eq!(layout::(), (32, 8)); + assert_eq!(layout::(), (40, 8)); assert_eq!(offset_of!(RawStats, in_flight), 8); + assert_eq!(offset_of!(RawStats, nvdec_khz), 32); } From a2f4b4746fa5151a604ea573fbd8c1a802078776 Mon Sep 17 00:00:00 2001 From: enricobuehler Date: Thu, 5 Nov 2026 21:02:30 +0100 Subject: [PATCH 2/6] perf(presenter): skip the UI layer while the overlay is empty Every stream frame cleared the full-screen UI image, flushed Skia and blended the layer over the picture, though with stats off and the dial closed it held nothing. At 1080p that is three full-screen passes on the memory bus that NVDEC, the VIC and the copy share. The stream now tells the display when its overlay is empty, and the presenter skips the draw, the flush and the blend. Leaving the stream turns the layer back on. --- crates/pf-nx-app/src/platform/mod.rs | 11 ++- crates/pf-nx-app/src/runtime/stream.rs | 3 + .../pf-nx-platform/src/console/display_nvk.rs | 4 ++ crates/pf-nx-vk/src/presenter.rs | 68 +++++++++++-------- 4 files changed, 56 insertions(+), 30 deletions(-) diff --git a/crates/pf-nx-app/src/platform/mod.rs b/crates/pf-nx-app/src/platform/mod.rs index 1401bfa..0912386 100644 --- a/crates/pf-nx-app/src/platform/mod.rs +++ b/crates/pf-nx-app/src/platform/mod.rs @@ -99,13 +99,18 @@ pub trait Display { } /// Draws one frame and presents it at the next vblank: the newest due stream picture - /// (when [`Self::set_stream`] is on), then `ui` over it on a transparent layer. Blocks - /// until a swapchain image is free, so this paces the main loop. + /// (when [`Self::set_stream`] is on), then `ui` over it on a transparent layer while + /// [`Self::set_ui_layer`] has it on. Blocks until a swapchain image is free, so this paces + /// the main loop. fn frame(&mut self, ui: &mut dyn FnMut(&skia_safe::Canvas)) -> Result; - /// Whether the stream layer shows. Off, the UI draws over black. + /// Whether the stream layer shows. Off, the UI draws over black and the UI layer is on. fn set_stream(&mut self, on: bool, fit: Fit); + /// Whether [`Self::frame`] draws and blends the UI layer. A stream turns it off while its + /// overlay is empty, which spares a full-screen clear, flush and blend each frame. + fn set_ui_layer(&mut self, _on: bool) {} + /// The last presented frame as tightly packed RGBA, for test-run bundles. fn read_back(&mut self) -> Result>; } diff --git a/crates/pf-nx-app/src/runtime/stream.rs b/crates/pf-nx-app/src/runtime/stream.rs index 92f20e5..25bfc25 100644 --- a/crates/pf-nx-app/src/runtime/stream.rs +++ b/crates/pf-nx-app/src/runtime/stream.rs @@ -439,6 +439,9 @@ fn stream( )]; let dt = last_frame.elapsed().as_secs_f64(); last_frame = Instant::now(); + // Stats off and the dial out of sight draw nothing: the layer is skipped, not blended. + app.display + .set_ui_layer(hud.tier != StatsVerbosity::Off || ring.damage() != 0); let result = app.display.frame(&mut |canvas| { if let Some(fonts) = &fonts { hud.draw(canvas, fonts, scale); diff --git a/crates/pf-nx-platform/src/console/display_nvk.rs b/crates/pf-nx-platform/src/console/display_nvk.rs index 899d7b3..9c52bd0 100644 --- a/crates/pf-nx-platform/src/console/display_nvk.rs +++ b/crates/pf-nx-platform/src/console/display_nvk.rs @@ -49,6 +49,10 @@ impl Display for NvkDisplay { self.presenter.set_stream(on, fit); } + fn set_ui_layer(&mut self, on: bool) { + self.presenter.set_ui_layer(on); + } + fn read_back(&mut self) -> Result> { self.presenter.read_back() } diff --git a/crates/pf-nx-vk/src/presenter.rs b/crates/pf-nx-vk/src/presenter.rs index f2a4f4b..6f84012 100644 --- a/crates/pf-nx-vk/src/presenter.rs +++ b/crates/pf-nx-vk/src/presenter.rs @@ -1,6 +1,6 @@ //! The presenter: the [`Display`] seam on the one device. Each frame clears to black, draws the //! newest stream picture into its fitted rectangle (when the stream layer is on), blends the UI -//! layer over it, and presents at the next vblank. +//! layer over it (unless a stream switched that layer off), and presents at the next vblank. //! //! Pictures arrive from the decode lanes through a [`VideoSlot`]: newest wins, and the last one //! shown is drawn again until a newer one arrives. A picture stays alive until the frame that @@ -59,6 +59,8 @@ pub struct Presenter { frames: Vec, frame: usize, ui: UiLayer, + /// The UI layer is drawn and blended; a stream with an empty overlay turns it off. + ui_on: bool, slot: Arc, current: Option>, stream: Option, @@ -165,6 +167,7 @@ impl Presenter { frames, frame: 0, ui, + ui_on: true, slot, current: None, stream: None, @@ -277,7 +280,7 @@ impl Presenter { } } - /// Records the composition into `fb`: clear, picture, UI layer. + /// Records the composition into `fb`: clear, picture, and the UI layer while it is on. /// /// # Safety /// `cmd` is recording, `fb` belongs to `pass`, and `sets` are not read by work in flight. @@ -339,29 +342,31 @@ impl Presenter { ); dev.cmd_draw(cmd, 3, 1, 0, 0); } - self.write_set(sets[2], &[self.ui.view()], vk::ImageLayout::SHADER_READ_ONLY_OPTIMAL); - dev.cmd_bind_pipeline(cmd, vk::PipelineBindPoint::GRAPHICS, self.pipes.overlay.pipeline); - dev.cmd_set_viewport( - cmd, - 0, - &[vk::Viewport { - x: 0.0, - y: 0.0, - width: extent.width as f32, - height: extent.height as f32, - min_depth: 0.0, - max_depth: 1.0, - }], - ); - dev.cmd_bind_descriptor_sets( - cmd, - vk::PipelineBindPoint::GRAPHICS, - self.pipes.overlay.layout, - 0, - &[sets[2]], - &[], - ); - dev.cmd_draw(cmd, 3, 1, 0, 0); + if self.ui_on { + self.write_set(sets[2], &[self.ui.view()], vk::ImageLayout::SHADER_READ_ONLY_OPTIMAL); + dev.cmd_bind_pipeline(cmd, vk::PipelineBindPoint::GRAPHICS, self.pipes.overlay.pipeline); + dev.cmd_set_viewport( + cmd, + 0, + &[vk::Viewport { + x: 0.0, + y: 0.0, + width: extent.width as f32, + height: extent.height as f32, + min_depth: 0.0, + max_depth: 1.0, + }], + ); + dev.cmd_bind_descriptor_sets( + cmd, + vk::PipelineBindPoint::GRAPHICS, + self.pipes.overlay.layout, + 0, + &[sets[2]], + &[], + ); + dev.cmd_draw(cmd, 3, 1, 0, 0); + } dev.cmd_end_render_pass(cmd); } } @@ -469,7 +474,9 @@ impl Display for Presenter { self.rebuild()?; } - self.ui.draw(ui); + if self.ui_on { + self.ui.draw(ui); + } // The acquire blocks until the panel frees an image, just after a vblank: the picture is // taken after it, so one that lands during the wait still makes this frame. let index = self.acquire(ready)?; @@ -508,7 +515,9 @@ impl Display for Presenter { if let Some(t) = &mut self.timer { t.begin(&self.dev, cmd, f); } - self.ui.finish(&self.dev, cmd, f); + if self.ui_on { + self.ui.finish(&self.dev, cmd, f); + } self.compose(cmd, self.pipes.pass_out, self.framebuffers[index as usize], sets); if let Some(t) = &self.timer { t.end(&self.dev, cmd, f); @@ -569,9 +578,14 @@ impl Display for Presenter { if !on { self.slot.clear(); self.current = None; + self.ui_on = true; } } + fn set_ui_layer(&mut self, on: bool) { + self.ui_on = on; + } + fn read_back(&mut self) -> Result> { self.ensure_readback()?; self.wait_idle(); From c3ee5a17bd108be61eff237debaf5aef87760c77 Mon Sep 17 00:00:00 2001 From: enricobuehler Date: Thu, 5 Nov 2026 21:02:30 +0100 Subject: [PATCH 3/6] docs: list what the picture lane leaves for later [skip ci] The handheld clock needs the maintainer's yes; sampling the decoded surface in place needs a console probe first. --- docs/improvements.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/docs/improvements.md b/docs/improvements.md index c304c33..6896c3d 100644 --- a/docs/improvements.md +++ b/docs/improvements.md @@ -85,6 +85,24 @@ mode and can go in parallel. 3 is the busy one with the current build. If the remaining calls turn out to sit in `punktfunk-core`'s receive loop, that part moves to section 1. +### NVDEC's top clock in handheld + +**Why.** nvtegra rescales NVDEC every 20 pictures so that one takes about a frame interval +(`av_nvtegra_dfs_update`, floor 180 MHz up to 1080p). Above 720p this client now holds the top +clock; handheld keeps the scaling, at about 4 ms a 720p picture (NOTES, 2026-10-31). + +**The idea.** Hold it in handheld too, for a shorter decode. **Needs the maintainer's yes**: it +spends battery. It could go with Performance mode. + +### Pictures without the VIC and the copy + +**Why.** Each picture costs NVDEC, then a VIC transfer that maps and unmaps the destination +through nvdrv and flushes 3 MB of cache, then the 3 MB copy: about 3 and 3.7 ms at 1080p. + +**The idea.** NVK's fork imports an NvMap ID (`nvk_switch_allocate_shared_memory`). The decoded +surface's NvMap could be sampled in place, its 16Bx2 block-linear layout detiled in the CSC +shader. **Test first:** the import of a decoder surface, and the picture it samples. + ### The smoothness buffer's Automatic on the Switch The shared setting resolves Automatic to two frames on every client From 5db41e696c440e0a3e4d96e86c0e2a90264617d6 Mon Sep 17 00:00:00 2001 From: enricobuehler Date: Thu, 5 Nov 2026 21:51:10 +0100 Subject: [PATCH 4/6] fix(sys): link libavcodec after the shim pkg-config's link flags came out before the shim's own archive. With --as-needed, as on Debian's toolchain, the linker dropped libavcodec before anything used it, so the crate's own tests did not link alone. Naming the libraries after the shim keeps them. --- crates/pf-nx-sys/build.rs | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/crates/pf-nx-sys/build.rs b/crates/pf-nx-sys/build.rs index 83775f9..0dced81 100644 --- a/crates/pf-nx-sys/build.rs +++ b/crates/pf-nx-sys/build.rs @@ -97,13 +97,26 @@ fn build_shim(dkp: &Path, portlibs: &Path, name: &str) { fn build_dev_box_shim() { println!("cargo:rerun-if-changed=csrc/pf_av.c"); let mut build = cc::Build::new(); + let mut found = Vec::new(); for lib in ["libavcodec", "libavutil"] { - let found = pkg_config::probe_library(lib) + let lib = pkg_config::Config::new() + .cargo_metadata(false) + .probe(lib) .unwrap_or_else(|e| panic!("{lib} for the dev box's software decoder (libavcodec-dev): {e}")); - for dir in found.include_paths { + for dir in &lib.include_paths { build.include(dir); } + found.push(lib); } build.file("csrc/pf_av.c").warnings(true).compile("pf_av"); + // After the shim: with --as-needed a library named before its user is dropped. + for lib in found { + for dir in lib.link_paths { + println!("cargo:rustc-link-search=native={}", dir.display()); + } + for name in lib.libs { + println!("cargo:rustc-link-lib={name}"); + } + } println!("cargo:rustc-cfg=pf_av"); } From 08e8a015841616c87d04752179f73f2098ff2428 Mon Sep 17 00:00:00 2001 From: enricobuehler Date: Thu, 5 Nov 2026 21:53:48 +0100 Subject: [PATCH 5/6] perf(video): hold the top clock only while docked The stream's size was the wrong gate: a handheld 1080p stream took the top clock and its battery cost. Docked, the console is on mains and the 1080p picture needs the headroom. Handheld keeps nvtegra's own scaling, which already decodes 720p in about 4 ms. The lane asks at each access unit, so a dock change mid-stream moves the clock without a new decoder. Turning it off hands NVDEC back to nvtegra's scaling at its next rescale. --- crates/pf-nx-platform/src/console/mod.rs | 9 +++++++- crates/pf-nx-platform/src/video.rs | 25 +++++++++++++++-------- crates/pf-nx-probe/src/nvdec.rs | 2 +- crates/pf-nx-sys/csrc/pf_av.c | 16 +++++++++++---- crates/pf-nx-sys/src/av.rs | 26 ++++++++++++++++-------- docs/improvements.md | 10 ++++----- 6 files changed, 60 insertions(+), 28 deletions(-) diff --git a/crates/pf-nx-platform/src/console/mod.rs b/crates/pf-nx-platform/src/console/mod.rs index 066728a..01b8701 100644 --- a/crates/pf-nx-platform/src/console/mod.rs +++ b/crates/pf-nx-platform/src/console/mod.rs @@ -53,6 +53,11 @@ fn operation_mode() -> OperationMode { } } +/// On mains power: NVDEC may hold its top clock. Handheld keeps nvtegra's scaling. +fn docked() -> bool { + applet::operation_mode() == applet::OperationMode::Docked +} + /// The stream and swapchain size each mode calls for (plan D11). fn mode_size(mode: OperationMode) -> (u32, u32) { match mode { @@ -99,7 +104,9 @@ pub fn platform(kind: DisplayKind) -> Result { ( Box::new(display_nvk::NvkDisplay::new(presenter, mode)), Arc::new( - crate::video::NxVideo::gpu(dev, slot).straight(Path::new(DATA_DIR).join(STRAIGHT_MARKER).exists()), + crate::video::NxVideo::gpu(dev, slot) + .straight(Path::new(DATA_DIR).join(STRAIGHT_MARKER).exists()) + .top_clock(docked), ), gpu, ui, diff --git a/crates/pf-nx-platform/src/video.rs b/crates/pf-nx-platform/src/video.rs index ce41596..be36b3c 100644 --- a/crates/pf-nx-platform/src/video.rs +++ b/crates/pf-nx-platform/src/video.rs @@ -6,7 +6,7 @@ //! picture straight into the presenter's next staging buffer, which `commit` copies into a //! plane set and publishes (D7). On the console the picture goes through libavutil's own frame //! and the shim copies it; with [`NxVideo::straight`] the VIC writes into that staging buffer -//! itself where it can map it (WP5). Above 720p NVDEC is held at its top clock. The +//! itself where it can map it (WP5). Docked, NVDEC is held at its top clock. The //! `nvdec[10s]` line names which route ran and NVDEC's clock. On the framebuffer display the //! pictures land in scratch planes and are dropped, so the emulator still runs the hwaccel. A //! decoder error fails this feed, which has the session ask for a keyframe, and the next feed @@ -39,6 +39,8 @@ pub struct NxVideo { /// The VIC writes into the staging buffer itself. Off by default: on the console it neither /// lowered the transfer plus copy nor the end-to-end time (WP5, 2026-11-01). straight: bool, + /// Whether NVDEC holds its top clock, asked at each access unit. + top_clock: fn() -> bool, } impl NxVideo { @@ -46,6 +48,7 @@ impl NxVideo { Self { gpu: Some((dev, slot)), straight: false, + top_clock: || false, } } @@ -53,6 +56,7 @@ impl NxVideo { Self { gpu: None, straight: false, + top_clock: || false, } } @@ -61,6 +65,12 @@ impl NxVideo { self.straight = on; self } + + /// Holds NVDEC at its top clock while `when` says so, asked at each access unit. + pub fn top_clock(mut self, when: fn() -> bool) -> Self { + self.top_clock = when; + self + } } impl VideoDecoders for NxVideo { @@ -80,30 +90,27 @@ impl VideoDecoders for NxVideo { spec.bit_depth, if spec.chroma444 { " 4:4:4" } else { "" } ); - // nvtegra scales NVDEC down until a picture takes most of a frame interval: about 8 ms at - // 1080p, which with the transfer and the copy leaves a 60 Hz frame no slack. 720p fits. - let top_clock = spec.width * spec.height > 1280 * 720; - let decoder = av::Decoder::open(codec, spec.width, spec.height, self.straight, top_clock)?; + let decoder = av::Decoder::open(codec, spec.width, spec.height, self.straight)?; let out = match &self.gpu { Some((dev, slot)) => Out::Gpu(Nv12Upload::new(dev.clone(), slot.clone(), spec)?), None => Out::scratch(spec), }; tracing::info!( - "{DECODER}: {codec:?} {}x{}, {}{}", + "{DECODER}: {codec:?} {}x{}, {}", spec.width, spec.height, if self.straight { "straight VIC route asked for" } else { "VIC then copy" - }, - if top_clock { ", NVDEC at its top clock" } else { "" } + } ); let started = Instant::now(); Ok(Box::new(NxSink { lane: Mutex::new(Lane { decoder, out, + top_clock: self.top_clock, needs_flush: false, timing: Timing::new(), started, @@ -176,6 +183,7 @@ const DUES_KEPT: usize = 8; struct Lane { decoder: av::Decoder, out: Out, + top_clock: fn() -> bool, /// The last feed failed; flush before the next one. needs_flush: bool, timing: Timing, @@ -187,6 +195,7 @@ struct Lane { impl Lane { fn feed(&mut self, au: &[u8], pts: u64, due_ns: u64) -> Result<()> { + self.decoder.set_top_clock((self.top_clock)()); if std::mem::take(&mut self.needs_flush) { self.decoder.flush(); self.dues.clear(); diff --git a/crates/pf-nx-probe/src/nvdec.rs b/crates/pf-nx-probe/src/nvdec.rs index 87ebd24..85d9734 100644 --- a/crates/pf-nx-probe/src/nvdec.rs +++ b/crates/pf-nx-probe/src/nvdec.rs @@ -90,7 +90,7 @@ fn decode(step: &str, codec: Codec, data: &[u8], out: &mut Out) -> Resultctx->hw_device_ctx = av_buffer_ref(d->device)) || (direct && !(d->direct = av_frame_alloc()))) goto fail; - d->pin_clock = pin_clock; #else (void)direct; - (void)pin_clock; #endif // The default get_format picks the device's hw format when hw_device_ctx is set. d->ctx->width = width; @@ -141,6 +139,16 @@ int pf_av_send(PfAvDecoder *d, const uint8_t *data, int len, uint64_t pts) { return 0; } +// Non-zero holds NVDEC at its top clock from the next picture; zero hands it back to nvtegra's +// scaling, which takes over at its next rescale. +void pf_av_set_top_clock(PfAvDecoder *d, int on) { + on = on != 0; + if (d->pin_clock != on) { + d->pin_clock = on; + d->pinned_hz = 0; + } +} + // nvtegra rescales NVDEC every 20 pictures so one takes about a frame interval: playback's // rule, which puts a 1080p picture at about 8 ms. Asking the top clock again after each // rescale holds it there; closing the decoder releases the request. diff --git a/crates/pf-nx-sys/src/av.rs b/crates/pf-nx-sys/src/av.rs index fd7af47..e197283 100644 --- a/crates/pf-nx-sys/src/av.rs +++ b/crates/pf-nx-sys/src/av.rs @@ -123,9 +123,9 @@ mod ffi { width: c_int, height: c_int, direct: c_int, - pin_clock: c_int, out: *mut *mut PfAvDecoder, ) -> c_int; + pub fn pf_av_set_top_clock(d: *mut PfAvDecoder, on: c_int); pub fn pf_av_send(d: *mut PfAvDecoder, data: *const u8, len: c_int, pts: u64) -> c_int; pub fn pf_av_receive( d: *mut PfAvDecoder, @@ -176,13 +176,12 @@ unsafe impl Send for Decoder {} impl Decoder { /// Opens `codec` for a `width`×`height` stream. `straight` has the VIC write into the /// caller's planes ([`Route::Vic`]) where it can; otherwise every picture goes through - /// libavutil's frame and a copy ([`Route::VicThenCopy`]). `top_clock` holds NVDEC at its - /// fastest clock, where nvtegra would scale it down to just keep pace. - pub fn open(codec: Codec, width: u32, height: u32, straight: bool, top_clock: bool) -> anyhow::Result { + /// libavutil's frame and a copy ([`Route::VicThenCopy`]). + pub fn open(codec: Codec, width: u32, height: u32, straight: bool) -> anyhow::Result { let (w, h) = (i32::try_from(width)?, i32::try_from(height)?); let mut raw = core::ptr::null_mut(); // SAFETY: `raw` is a valid out-pointer; the shim sets it only on success. - let rc = unsafe { ffi::pf_av_open(codec as i32, w, h, i32::from(straight), i32::from(top_clock), &mut raw) }; + let rc = unsafe { ffi::pf_av_open(codec as i32, w, h, i32::from(straight), &mut raw) }; if rc < 0 { return Err(av_error(&format!("open the {codec:?} decoder"), rc)); } @@ -248,6 +247,13 @@ impl Decoder { unsafe { ffi::pf_av_flush(self.raw.as_ptr()) }; } + /// Holds NVDEC at its fastest clock from the next picture, where nvtegra would scale it + /// down to just keep pace; off hands it back to that scaling. + pub fn set_top_clock(&mut self, on: bool) { + // SAFETY: a live decoder. + unsafe { ffi::pf_av_set_top_clock(self.raw.as_ptr(), i32::from(on)) }; + } + pub fn stats(&self) -> Stats { let mut s = RawStats::default(); // SAFETY: a live decoder and a valid out-pointer. @@ -275,7 +281,7 @@ impl Drop for Decoder { #[cfg(not(pf_av))] impl Decoder { - pub fn open(codec: Codec, _width: u32, _height: u32, _straight: bool, _top_clock: bool) -> anyhow::Result { + pub fn open(codec: Codec, _width: u32, _height: u32, _straight: bool) -> anyhow::Result { anyhow::bail!("open the {codec:?} decoder: no decoder on this box (NVDEC on the console, libavcodec on Linux)") } @@ -297,6 +303,10 @@ impl Decoder { match self.never {} } + pub fn set_top_clock(&mut self, _on: bool) { + match self.never {} + } + pub fn stats(&self) -> Stats { match self.never {} } @@ -309,7 +319,7 @@ mod tests { #[cfg(not(pf_av))] #[test] fn open_fails_off_the_console() { - let err = Decoder::open(Codec::Hevc, 1920, 1080, false, false).err().unwrap(); + let err = Decoder::open(Codec::Hevc, 1920, 1080, false).err().unwrap(); assert_eq!( err.to_string(), "open the Hevc decoder: no decoder on this box (NVDEC on the console, libavcodec on Linux)" @@ -320,7 +330,7 @@ mod tests { #[test] fn a_dev_box_opens_both_codecs_in_software() { for codec in [Codec::H264, Codec::Hevc] { - let mut d = Decoder::open(codec, 1920, 1080, false, false).unwrap(); + let mut d = Decoder::open(codec, 1920, 1080, false).unwrap(); assert_eq!(d.stats(), Stats::default()); d.flush(); } diff --git a/docs/improvements.md b/docs/improvements.md index 6896c3d..e7581e6 100644 --- a/docs/improvements.md +++ b/docs/improvements.md @@ -87,12 +87,10 @@ mode and can go in parallel. ### NVDEC's top clock in handheld -**Why.** nvtegra rescales NVDEC every 20 pictures so that one takes about a frame interval -(`av_nvtegra_dfs_update`, floor 180 MHz up to 1080p). Above 720p this client now holds the top -clock; handheld keeps the scaling, at about 4 ms a 720p picture (NOTES, 2026-10-31). - -**The idea.** Hold it in handheld too, for a shorter decode. **Needs the maintainer's yes**: it -spends battery. It could go with Performance mode. +nvtegra rescales NVDEC every 20 pictures so that one takes about a frame interval +(`av_nvtegra_dfs_update`, floor 180 MHz up to 1080p). Docked, this client holds the top clock; +handheld keeps the scaling, at about 4 ms a 720p picture (NOTES, 2026-10-31). The maintainer +keeps it off by default in handheld (2026-11-05); an opt-in could go with Performance mode. ### Pictures without the VIC and the copy From c65be0af5eeb096ec01b682cafc7ba1c8e82cb43 Mon Sep 17 00:00:00 2001 From: enricobuehler Date: Thu, 5 Nov 2026 21:54:24 +0100 Subject: [PATCH 6/6] perf(video): sample NVDEC surfaces in place At docked 1080p each picture paid a VIC transfer (seven nvdrv calls and a 3 MB cache flush), then a 3 MB copy into uncached staging, then a staging copy on the GPU. That serial lane was most of why 1080p60 did not fit a frame. The presenter now imports each decoder surface once as host memory and detiles its block-linear planes as it samples them. A layout or import the console does not honour would show a wrong picture, so each decoder starts on the VIC. Its first detailed picture is read back through the GPU at the addresses the shader samples and compared with the VIC's copy; only a match moves the lane in place, and any later failure moves it back for good. A vic-copia file keeps the VIC for an A/B. Imports carry the decoder's pool generation: a replaced pool may free addresses a new one reuses. --- crates/pf-nx-platform/src/console/mod.rs | 6 +- crates/pf-nx-platform/src/video.rs | 181 ++++++- crates/pf-nx-sys/csrc/pf_av.c | 97 +++- crates/pf-nx-sys/src/av.rs | 185 ++++++- crates/pf-nx-sys/tests/layout.rs | 6 +- crates/pf-nx-vk/Cargo.toml | 4 + crates/pf-nx-vk/shaders/nv12_tiled.frag | 67 +++ crates/pf-nx-vk/shaders/nv12_tiled.frag.spv | Bin 0 -> 6720 bytes crates/pf-nx-vk/src/device.rs | 19 +- crates/pf-nx-vk/src/in_place.rs | 538 ++++++++++++++++++++ crates/pf-nx-vk/src/lib.rs | 2 + crates/pf-nx-vk/src/picture.rs | 20 +- crates/pf-nx-vk/src/pipelines.rs | 65 ++- crates/pf-nx-vk/src/presenter.rs | 108 +++- crates/pf-nx-vk/src/util.rs | 22 + crates/pf-nx-vk/tests/in_place.rs | 212 ++++++++ docs/improvements.md | 21 +- 17 files changed, 1460 insertions(+), 93 deletions(-) create mode 100644 crates/pf-nx-vk/shaders/nv12_tiled.frag create mode 100644 crates/pf-nx-vk/shaders/nv12_tiled.frag.spv create mode 100644 crates/pf-nx-vk/src/in_place.rs create mode 100644 crates/pf-nx-vk/tests/in_place.rs diff --git a/crates/pf-nx-platform/src/console/mod.rs b/crates/pf-nx-platform/src/console/mod.rs index 01b8701..a3fdc03 100644 --- a/crates/pf-nx-platform/src/console/mod.rs +++ b/crates/pf-nx-platform/src/console/mod.rs @@ -21,6 +21,8 @@ const DATA_DIR: &str = "sdmc:/switch/punktfunk"; /// A file of this name in [`DATA_DIR`], read at start, turns the straight VIC route on, so one /// build A/Bs both routes. const STRAIGHT_MARKER: &str = "vic-direto"; +/// A file of this name keeps pictures on the VIC route instead of in place, for the same A/B. +const VIC_MARKER: &str = "vic-copia"; /// The Mesa pin NVK was built from, for the log and the About screen. const MESA_PIN: &str = match option_env!("PF_MESA_PIN") { @@ -101,11 +103,13 @@ pub fn platform(kind: DisplayKind) -> Result { let presenter = Presenter::new(dev.clone(), slot.clone()).context("bring up the display")?; let gpu = format!("NVK {MESA_PIN} ({})", dev.name); let ui = presenter.ui(); + let marker = |name: &str| Path::new(DATA_DIR).join(name).exists(); ( Box::new(display_nvk::NvkDisplay::new(presenter, mode)), Arc::new( crate::video::NxVideo::gpu(dev, slot) - .straight(Path::new(DATA_DIR).join(STRAIGHT_MARKER).exists()) + .straight(marker(STRAIGHT_MARKER)) + .in_place(!marker(VIC_MARKER)) .top_clock(docked), ), gpu, diff --git a/crates/pf-nx-platform/src/video.rs b/crates/pf-nx-platform/src/video.rs index be36b3c..efc2aef 100644 --- a/crates/pf-nx-platform/src/video.rs +++ b/crates/pf-nx-platform/src/video.rs @@ -3,29 +3,33 @@ //! shows real pictures there. //! //! `feed` decodes on the session's video thread: send the access unit, then receive every -//! picture straight into the presenter's next staging buffer, which `commit` copies into a -//! plane set and publishes (D7). On the console the picture goes through libavutil's own frame -//! and the shim copies it; with [`NxVideo::straight`] the VIC writes into that staging buffer -//! itself where it can map it (WP5). Docked, NVDEC is held at its top clock. The -//! `nvdec[10s]` line names which route ran and NVDEC's clock. On the framebuffer display the -//! pictures land in scratch planes and are dropped, so the emulator still runs the hwaccel. A -//! decoder error fails this feed, which has the session ask for a keyframe, and the next feed -//! starts from a flushed decoder. +//! picture. By the VIC a picture lands in the presenter's next staging buffer, which `commit` +//! copies into a plane set and publishes (D7): through libavutil's own frame and the shim's copy, +//! or with [`NxVideo::straight`] written there by the VIC itself (WP5). In place +//! ([`NxVideo::in_place`]) NVDEC's own surface goes to the presenter, which samples it as it +//! lies; the lane takes that route once a picture's surface reads back as the VIC copied it, and +//! leaves it for good on any mismatch. The `nvdec[10s]` line names the route and NVDEC's clock. +//! On the framebuffer display the pictures land in scratch planes and are dropped, so the +//! emulator still runs the hwaccel. A decoder error fails this feed, which has the session ask +//! for a keyframe, and the next feed starts from a flushed decoder. use std::collections::VecDeque; use std::sync::{Arc, Mutex}; use std::time::{Duration, Instant}; -use anyhow::{bail, ensure, Result}; +use anyhow::{anyhow, bail, ensure, Result}; use pf_nx_app::core::caps::VideoCaps; use pf_nx_app::core::media::{FeedFlags, MediaClock, VideoSink, VideoSinkCaps}; use pf_nx_app::platform::{StreamSpec, VideoDecoders}; use pf_nx_sys::av; -use pf_nx_vk::{GpuDevice, Nv12Upload, StagingPlanes, VideoSlot}; +use pf_nx_vk::{Check, GpuDevice, InPlace, Nv12Upload, StagingPlanes, TiledSurface, VideoSlot}; use punktfunk_core::quic; /// How often the lane logs where a picture's time went: the `stats:` line's cadence. const TIMING_WINDOW: Duration = Duration::from_secs(10); +/// While the in-place route is checked, every this-many-th picture is checked: half a second at +/// 60 Hz, so a flat start costs little. +const CHECK_EVERY: u32 = 30; /// The decoder behind the lane, for the sink's name and the log. #[cfg(target_os = "horizon")] @@ -39,6 +43,8 @@ pub struct NxVideo { /// The VIC writes into the staging buffer itself. Off by default: on the console it neither /// lowered the transfer plus copy nor the end-to-end time (WP5, 2026-11-01). straight: bool, + /// Pictures go to the presenter as NVDEC left them, once one checks out. + in_place: bool, /// Whether NVDEC holds its top clock, asked at each access unit. top_clock: fn() -> bool, } @@ -48,6 +54,7 @@ impl NxVideo { Self { gpu: Some((dev, slot)), straight: false, + in_place: false, top_clock: || false, } } @@ -56,6 +63,7 @@ impl NxVideo { Self { gpu: None, straight: false, + in_place: false, top_clock: || false, } } @@ -66,6 +74,13 @@ impl NxVideo { self } + /// Has the presenter sample NVDEC's surfaces where they lie, once one reads back right. + /// The straight route keeps the VIC. + pub fn in_place(mut self, on: bool) -> Self { + self.in_place = on; + self + } + /// Holds NVDEC at its top clock while `when` says so, asked at each access unit. pub fn top_clock(mut self, when: fn() -> bool) -> Self { self.top_clock = when; @@ -95,21 +110,34 @@ impl VideoDecoders for NxVideo { Some((dev, slot)) => Out::Gpu(Nv12Upload::new(dev.clone(), slot.clone(), spec)?), None => Out::scratch(spec), }; + let route = match &self.gpu { + Some((dev, slot)) if self.in_place && !self.straight => { + match InPlace::new(dev.clone(), slot.clone(), spec) { + Ok(lane) => Route::Checking(lane, 0), + Err(e) => { + tracing::info!("in place: off for this stream: {e:#}"); + Route::Vic + } + } + } + _ => Route::Vic, + }; tracing::info!( "{DECODER}: {codec:?} {}x{}, {}", spec.width, spec.height, - if self.straight { - "straight VIC route asked for" - } else { - "VIC then copy" + match (&route, self.straight) { + (Route::Checking(..), _) => "VIC then copy until a picture checks out in place", + (_, true) => "straight VIC route asked for", + _ => "VIC then copy", } ); let started = Instant::now(); Ok(Box::new(NxSink { lane: Mutex::new(Lane { - decoder, + route, out, + decoder, top_clock: self.top_clock, needs_flush: false, timing: Timing::new(), @@ -180,9 +208,21 @@ impl Out { /// is the rule; the rest bound a decoder that swallowed access units. const DUES_KEPT: usize = 8; +/// How the lane's pictures reach the presenter. +enum Route { + /// By the VIC into the staging planes. + Vic, + /// By the VIC, while every [`CHECK_EVERY`]th picture is checked in place; pictures so far. + Checking(InPlace, u32), + /// As NVDEC left them. + InPlace(InPlace), +} + +/// Fields drop in order: the route's surface imports go before the decoder frees its surfaces. struct Lane { - decoder: av::Decoder, + route: Route, out: Out, + decoder: av::Decoder, top_clock: fn() -> bool, /// The last feed failed; flush before the next one. needs_flush: bool, @@ -219,15 +259,30 @@ impl Lane { Ok(()) } - /// Receives one picture into the next staging planes; `false` when none is ready. + /// Receives one picture by the lane's route; `false` when none is ready. fn receive(&mut self) -> Result { + if matches!(self.route, Route::InPlace(_)) { + return self.receive_in_place(); + } let waited = Instant::now(); let planes = self.out.stage()?; let wait = waited.elapsed(); - let got = self - .decoder - .receive_into(planes.y, planes.y_stride, planes.uv, planes.uv_stride)?; - let Some(meta) = got else { return Ok(false) }; + let check = matches!(self.route, Route::Checking(_, n) if n % CHECK_EVERY == 0); + let got = if check { + self.decoder + .receive_kept_into(planes.y, planes.y_stride, planes.uv, planes.uv_stride)? + } else { + self.decoder + .receive_into(planes.y, planes.y_stride, planes.uv, planes.uv_stride)? + .map(|meta| (meta, None)) + }; + let Some((meta, kept)) = got else { return Ok(false) }; + if let Route::Checking(_, n) = &mut self.route { + *n += 1; + } + if check { + settle(&mut self.route, kept.as_ref(), &planes); + } let due = self.due_of(meta.pts); self.timing.note_due(due); let started = Instant::now(); @@ -238,6 +293,36 @@ impl Lane { Ok(true) } + /// Receives one picture as NVDEC left it and publishes it; `false` when none is ready. A + /// surface the presenter cannot sample, or a failed import, puts the lane on the VIC. + fn receive_in_place(&mut self) -> Result { + let surface = match self.decoder.receive_surface() { + Ok(Some(surface)) => surface, + Ok(None) => return Ok(false), + Err(e) if e.is::() => { + tracing::warn!("in place: back to the VIC: {e:#}"); + self.route = Route::Vic; + return Ok(true); + } + Err(e) => return Err(e), + }; + let due = self.due_of(surface.pts()); + self.timing.note_due(due); + let Route::InPlace(lane) = &mut self.route else { + unreachable!("only the in-place route receives surfaces") + }; + let started = Instant::now(); + let published = lane.publish(&tiled(&surface), surface.pts(), due, Box::new(surface)); + let upload = started.elapsed(); + if let Err(e) = &published { + tracing::warn!("in place: back to the VIC: {e:#}"); + self.route = Route::Vic; + } + self.timing + .note(&self.decoder.stats(), Duration::ZERO, upload, None, published.is_ok()); + Ok(true) + } + /// The due time fed with the access unit at host PTS `pts`, dropping any older ones: their /// pictures never came out. `None` (shown at once) when it is not kept. fn due_of(&mut self, pts: u64) -> Option { @@ -250,6 +335,58 @@ impl Lane { } } +/// The surface as the presenter's in-place lane reads it. +fn tiled(s: &av::Surface) -> TiledSurface { + TiledSurface { + base: s.base(), + size: s.size(), + pitch: s.pitch(), + chroma_offset: s.chroma_offset(), + width: s.width(), + height: s.height(), + pool: s.pool(), + } +} + +/// Moves a checking route on from the check of `kept`, the surface of the picture the VIC has +/// just written into `planes`: in place on a match, back to the VIC for good on a mismatch or a +/// failure, unchanged on a picture too flat to tell. +fn settle(route: &mut Route, kept: Option<&av::Surface>, planes: &StagingPlanes<'_>) { + let Route::Checking(lane, n) = route else { return }; + let verdict = match kept { + Some(s) => lane.check( + &tiled(s), + &planes.y[..], + planes.y_stride, + &planes.uv[..], + planes.uv_stride, + ), + None => Err(anyhow!("{}", av::NotInPlace)), + }; + let n = *n; + match verdict { + Ok(Check::Flat) => {} + Ok(Check::Match { atoms, differ }) => { + tracing::info!( + "in place: picture {n} read back as the VIC wrote it ({differ} of {atoms} atoms off); pictures now go in place" + ); + if let Route::Checking(lane, _) = std::mem::replace(route, Route::Vic) { + *route = Route::InPlace(lane); + } + } + Ok(Check::Differ { atoms, differ }) => { + tracing::warn!( + "in place: {differ} of {atoms} atoms of picture {n} read back otherwise; staying on the VIC" + ); + *route = Route::Vic; + } + Err(e) => { + tracing::warn!("in place: the check of picture {n} failed, staying on the VIC: {e:#}"); + *route = Route::Vic; + } + } +} + /// Where the lane's time went over one [`TIMING_WINDOW`]: the wait for a free staging buffer, /// the decoder's own time and clock, the transfer to NV12, the shim's copy out of libavutil's /// frame (none when the transfer wrote the staging planes itself), the upload's submit, and the @@ -345,7 +482,7 @@ impl Timing { // falling back. if self.route != Some(s.route) { tracing::info!( - "nvdec: pictures reach the staging planes by {}{}{}", + "nvdec: route {}{}{}", s.route.name(), self.route.map_or_else(String::new, |r| format!(" (was {})", r.name())), if s.direct_err == 0 { diff --git a/crates/pf-nx-sys/csrc/pf_av.c b/crates/pf-nx-sys/csrc/pf_av.c index a5a6827..0d23f85 100644 --- a/crates/pf-nx-sys/csrc/pf_av.c +++ b/crates/pf-nx-sys/csrc/pf_av.c @@ -1,7 +1,7 @@ -// NVDEC through libavcodec's nvtegra hwaccel, as six calls the Rust side binds (src/av.rs). -// One thread uses a decoder at a time; pictures leave as pitch-linear NV12 via the VIC, -// through libavutil's own frame and a copy, or, when the caller opens with `direct`, straight -// into the caller's planes where the hwcontext can map them. +// NVDEC through libavcodec's nvtegra hwaccel, as the calls src/av.rs binds. One thread uses a +// decoder at a time; pictures leave as pitch-linear NV12 via the VIC, through libavutil's own +// frame and a copy, or, opened with `direct`, straight into the caller's planes where the +// hwcontext can map them. In place, a picture leaves as the decoder's own surface. // Built for a Linux dev box (no __SWITCH__), the same calls decode in software against the // system's libavcodec and interleave its 4:2:0 chroma into the same NV12 planes. #include @@ -23,6 +23,7 @@ // The shim's own errors, outside FFmpeg's AVERROR space; src/av.rs names them. #define PF_AV_NOT_NV12 (-0x70660001) #define PF_AV_TOO_SMALL (-0x70660002) +#define PF_AV_NOT_IN_PLACE (-0x70660003) // How the last picture reached the caller's planes (src/av.rs `Route`). enum { @@ -33,6 +34,8 @@ enum { // The hwcontext's transfer straight into the caller's planes. PF_AV_ROUTE_VIC = 3, PF_AV_ROUTE_CPU = 4, + // The decoder's surface itself, sampled where NVDEC wrote it. + PF_AV_ROUTE_IN_PLACE = 5, }; typedef struct PfAvStats { @@ -50,6 +53,17 @@ typedef struct PfAvPictureMeta { uint64_t pts; } PfAvPictureMeta; +// A decoded surface out of the decoder (src/av.rs `Surface`); NVDEC decodes into it again only +// after `frame` is freed. Both planes are 16Bx2 block-linear with two GOBs to a block, `pitch` +// bytes a row; the CbCr plane starts `chroma_offset` bytes in. `base` and `size` are 4 KiB +// aligned. `pool` changes when libavcodec replaces its surface pool: older addresses may be freed. +typedef struct PfAvSurface { + AVFrame *frame; + uint8_t *base; + uint32_t size, pitch, chroma_offset, width, height, pool; + uint64_t pts; +} PfAvSurface; + typedef struct PfAvDecoder { AVCodecContext *ctx; AVBufferRef *device; @@ -64,6 +78,9 @@ typedef struct PfAvDecoder { // Hold NVDEC at its top clock; the clock last asked for, Hz. int pin_clock; uint32_t pinned_hz; + // The surface pool last handed out from, held so its address names no later pool. + AVBufferRef *pool; + uint32_t pool_gen; } PfAvDecoder; void pf_av_close(PfAvDecoder *d) { @@ -74,6 +91,7 @@ void pf_av_close(PfAvDecoder *d) { av_frame_free(&d->hw); av_frame_free(&d->sw); av_frame_free(&d->direct); + av_buffer_unref(&d->pool); av_free(d); } @@ -204,6 +222,53 @@ static int transfer_into(PfAvDecoder *d, uint8_t *y, size_t y_stride, size_t y_l return err; } +// Moves the picture in `d->hw` into `out`. PF_AV_NOT_IN_PLACE off the console, or for a layout +// the in-place shader does not read; `d->hw` then keeps the picture. +static int hand_out(PfAvDecoder *d, PfAvSurface *out) { + out->frame = NULL; +#ifdef __SWITCH__ + AVFrame *hw = d->hw; + if (!hw->hw_frames_ctx || hw->format != AV_PIX_FMT_NVTEGRA) + return PF_AV_NOT_IN_PLACE; + AVNVTegraMap *map = av_nvtegra_frame_get_fbuf_map(hw); + uint8_t *base = av_nvtegra_map_get_addr(map); + uint32_t size = av_nvtegra_map_get_size(map); + int pitch = hw->linesize[0]; + size_t chroma = (size_t)(hw->data[1] - base); + if (map->is_linear || !base || hw->data[0] != base || hw->linesize[1] != pitch || pitch <= 0 || + (pitch & 63) || ((uintptr_t)base & 0xfff) || (size & 0xfff) || + chroma + (size_t)pitch * (((size_t)hw->height + 1) / 2) > size) + return PF_AV_NOT_IN_PLACE; + if (!d->pool || d->pool->data != hw->hw_frames_ctx->data) { + AVBufferRef *pool = av_buffer_ref(hw->hw_frames_ctx); + if (!pool) + return AVERROR(ENOMEM); + av_buffer_unref(&d->pool); + d->pool = pool; + d->pool_gen++; + } + if (!(out->frame = av_frame_alloc())) + return AVERROR(ENOMEM); + out->pool = d->pool_gen; + out->base = base; + out->size = size; + out->pitch = (uint32_t)pitch; + out->chroma_offset = (uint32_t)chroma; + out->width = (uint32_t)hw->width; + out->height = (uint32_t)hw->height; + out->pts = (uint64_t)hw->pts; + av_frame_move_ref(out->frame, hw); + return 1; +#else + (void)d; + return PF_AV_NOT_IN_PLACE; +#endif +} + +void pf_av_surface_free(AVFrame *frame) { + av_frame_free(&frame); +} + static int aligned_256(const void *p, size_t stride) { return (((uintptr_t)p | stride) & 0xff) == 0; } @@ -229,9 +294,10 @@ static void interleave_uv(uint8_t *dst, size_t dst_stride, const AVFrame *src, s } // 1 a picture written, 0 none ready, < 0 an AVERROR; the picture is dropped on error. `meta` -// is filled whenever a picture came out, so a too-small buffer reports the size it needs. +// is filled whenever a picture came out, so a too-small buffer reports the size it needs. A +// non-NULL `keep` takes the decoded surface too, its `frame` NULL where it cannot go in place. int pf_av_receive(PfAvDecoder *d, uint8_t *y, size_t y_stride, size_t y_len, uint8_t *uv, - size_t uv_stride, size_t uv_len, PfAvPictureMeta *meta) { + size_t uv_stride, size_t uv_len, PfAvPictureMeta *meta, PfAvSurface *keep) { int err = avcodec_receive_frame(d->ctx, d->hw); if (err == AVERROR(EAGAIN)) return 0; if (err < 0) return err; @@ -298,11 +364,30 @@ int pf_av_receive(PfAvDecoder *d, uint8_t *y, size_t y_stride, size_t y_len, uin } err = 1; out: + if (err == 1 && keep) + hand_out(d, keep); av_frame_unref(d->hw); av_frame_unref(d->sw); return err; } +// The next picture as the decoder's surface, with no transfer: 1 out, 0 none ready, < 0 an +// AVERROR or PF_AV_NOT_IN_PLACE, the picture dropped. +int pf_av_receive_surface(PfAvDecoder *d, PfAvSurface *out) { + int err = avcodec_receive_frame(d->ctx, d->hw); + if (err == AVERROR(EAGAIN)) return 0; + if (err < 0) return err; + d->stats.decoded++; + d->stats.last_decode_us = (uint32_t)(av_gettime_relative() - d->sent_at); + hold_clock(d); + d->stats.last_transfer_us = 0; + d->stats.last_copy_us = 0; + d->stats.route = PF_AV_ROUTE_IN_PLACE; + err = hand_out(d, out); + av_frame_unref(d->hw); + return err; +} + void pf_av_flush(PfAvDecoder *d) { avcodec_flush_buffers(d->ctx); d->taken = d->stats.decoded; diff --git a/crates/pf-nx-sys/src/av.rs b/crates/pf-nx-sys/src/av.rs index e197283..c01c524 100644 --- a/crates/pf-nx-sys/src/av.rs +++ b/crates/pf-nx-sys/src/av.rs @@ -1,6 +1,7 @@ //! NVDEC through libavcodec's `nvtegra` hwaccel, behind the C shim `csrc/pf_av.c`. The shim is -//! the only code that names libavcodec's structs; this side binds its six calls and `av_strerror` -//! (`libavutil/error.h`). Pictures come out as pitch-linear NV12 copied into the caller's planes. +//! the only code that names libavcodec's structs; this side binds its calls and `av_strerror` +//! (`libavutil/error.h`). Pictures come out as pitch-linear NV12 copied into the caller's planes, +//! or in place as the decoder's own block-linear [`Surface`]. //! //! On Linux the same shim decodes in software against the system's libavcodec, for the dev box; //! elsewhere [`Decoder::open`] returns an error, so the workspace builds anywhere. @@ -28,6 +29,96 @@ pub struct PictureMeta { pub pts: u64, } +/// `PfAvSurface`. +#[repr(C)] +#[derive(Debug)] +pub struct RawSurface { + pub frame: *mut core::ffi::c_void, + pub base: *mut u8, + pub size: u32, + pub pitch: u32, + pub chroma_offset: u32, + pub width: u32, + pub height: u32, + pub pool: u32, + pub pts: u64, +} + +impl RawSurface { + #[cfg_attr(not(pf_av), allow(dead_code))] + fn empty() -> Self { + Self { + frame: core::ptr::null_mut(), + base: core::ptr::null_mut(), + size: 0, + pitch: 0, + chroma_offset: 0, + width: 0, + height: 0, + pool: 0, + pts: 0, + } + } +} + +/// A decoded picture in the decoder's own surface: NVDEC decodes into that memory again only +/// after this drops. Both planes are 16Bx2 block-linear with two GOBs to a block, [`Self::pitch`] +/// bytes a row; the `CbCr` plane starts [`Self::chroma_offset`] bytes in. +pub struct Surface(RawSurface); + +// SAFETY: the surface is read, never written, through `base`, and dropping it only returns a +// buffer to libavutil's pool, which takes returns from any thread. +unsafe impl Send for Surface {} +// SAFETY: as above; nothing here mutates through a shared reference. +unsafe impl Sync for Surface {} + +impl Surface { + /// The surface's memory, 4 KiB aligned. + pub fn base(&self) -> *const u8 { + self.0.base + } + + /// Bytes from [`Self::base`], a 4 KiB multiple. + pub fn size(&self) -> usize { + self.0.size as usize + } + + pub fn pitch(&self) -> u32 { + self.0.pitch + } + + pub fn chroma_offset(&self) -> u32 { + self.0.chroma_offset + } + + pub fn width(&self) -> u32 { + self.0.width + } + + pub fn height(&self) -> u32 { + self.0.height + } + + /// The pts its access unit was sent with. + pub fn pts(&self) -> u64 { + self.0.pts + } + + /// The decoder's surface pool, which changes when libavcodec replaces it: memory of an + /// older pool may be freed once no surface of it is held. + pub fn pool(&self) -> u32 { + self.0.pool + } +} + +#[cfg(pf_av)] +impl Drop for Surface { + fn drop(&mut self) { + // SAFETY: `frame` came from the shim's hand-out and is freed once, here. + unsafe { ffi::pf_av_surface_free(self.0.frame) }; + } +} + #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] pub struct Stats { /// Pictures received since open. @@ -63,6 +154,8 @@ pub enum Route { Vic, /// Detiled on the CPU straight into the caller's planes. Cpu, + /// The decoder's surface itself, sampled where NVDEC wrote it. + InPlace, } impl Route { @@ -72,6 +165,7 @@ impl Route { 2 => Self::CpuThenCopy, 3 => Self::Vic, 4 => Self::Cpu, + 5 => Self::InPlace, _ => Self::Software, } } @@ -84,6 +178,7 @@ impl Route { Self::CpuThenCopy => "CPU detile, copied", Self::Vic => "VIC straight", Self::Cpu => "CPU detile straight", + Self::InPlace => "in place", } } } @@ -102,13 +197,27 @@ pub struct RawStats { pub nvdec_khz: u32, } +/// [`Decoder::receive_surface`]'s picture is in a surface the in-place shader does not read: +/// the route, not the stream, has failed. The picture is dropped. +#[derive(Clone, Copy, Debug)] +pub struct NotInPlace; + +impl core::fmt::Display for NotInPlace { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + f.write_str("the decoder's surface is not one the in-place shader reads") + } +} + +impl std::error::Error for NotInPlace {} + /// The shim's own codes, outside libavcodec's `AVERROR` space (`csrc/pf_av.c`). pub const PF_AV_NOT_NV12: i32 = -0x7066_0001; pub const PF_AV_TOO_SMALL: i32 = -0x7066_0002; +pub const PF_AV_NOT_IN_PLACE: i32 = -0x7066_0003; #[cfg(pf_av)] mod ffi { - use super::{PictureMeta, RawStats}; + use super::{PictureMeta, RawStats, RawSurface}; use core::ffi::{c_char, c_int}; /// `PfAvDecoder`, owned by the shim. @@ -136,7 +245,10 @@ mod ffi { uv_stride: usize, uv_len: usize, meta: *mut PictureMeta, + keep: *mut RawSurface, ) -> c_int; + pub fn pf_av_receive_surface(d: *mut PfAvDecoder, out: *mut RawSurface) -> c_int; + pub fn pf_av_surface_free(frame: *mut core::ffi::c_void); pub fn pf_av_flush(d: *mut PfAvDecoder); pub fn pf_av_close(d: *mut PfAvDecoder); pub fn pf_av_stats(d: *const PfAvDecoder, out: *mut RawStats); @@ -211,10 +323,50 @@ impl Decoder { y_stride: usize, uv: &mut [u8], uv_stride: usize, + ) -> anyhow::Result> { + self.receive(y, y_stride, uv, uv_stride, core::ptr::null_mut()) + } + + /// [`Self::receive_into`], and the decoder's surface of the same picture where it can go in + /// place, so the two can be compared. + pub fn receive_kept_into( + &mut self, + y: &mut [u8], + y_stride: usize, + uv: &mut [u8], + uv_stride: usize, + ) -> anyhow::Result)>> { + let mut keep = RawSurface::empty(); + let got = self.receive(y, y_stride, uv, uv_stride, &mut keep)?; + Ok(got.map(|meta| (meta, (!keep.frame.is_null()).then_some(Surface(keep))))) + } + + /// The next picture in place, with no transfer; `None` when none is ready. A surface the + /// in-place shader cannot read fails with [`NotInPlace`], the picture dropped. + pub fn receive_surface(&mut self) -> anyhow::Result> { + let mut out = RawSurface::empty(); + // SAFETY: `out` is a valid out-pointer; the shim sets `frame` only to a picture it hands over. + let rc = unsafe { ffi::pf_av_receive_surface(self.raw.as_ptr(), &mut out) }; + match rc { + 1 => Ok(Some(Surface(out))), + 0 => Ok(None), + PF_AV_NOT_IN_PLACE => Err(NotInPlace.into()), + _ => Err(av_error("receive a surface", rc)), + } + } + + /// `keep` null, or a surface the shim fills when the picture can go in place. + fn receive( + &mut self, + y: &mut [u8], + y_stride: usize, + uv: &mut [u8], + uv_stride: usize, + keep: *mut RawSurface, ) -> anyhow::Result> { let mut meta = PictureMeta::default(); // SAFETY: each plane pointer is writable for the length passed with it; the shim checks - // every row against those lengths before it copies. + // every row against those lengths before it copies. `keep` is null or a valid out-pointer. let rc = unsafe { ffi::pf_av_receive( self.raw.as_ptr(), @@ -225,6 +377,7 @@ impl Decoder { uv_stride, uv.len(), &mut meta, + keep, ) }; match rc { @@ -307,6 +460,20 @@ impl Decoder { match self.never {} } + pub fn receive_kept_into( + &mut self, + _y: &mut [u8], + _y_stride: usize, + _uv: &mut [u8], + _uv_stride: usize, + ) -> anyhow::Result)>> { + match self.never {} + } + + pub fn receive_surface(&mut self) -> anyhow::Result> { + match self.never {} + } + pub fn stats(&self) -> Stats { match self.never {} } @@ -351,6 +518,16 @@ mod tests { ), (40, 16, 28, 32) ); + // PfAvSurface: two pointers, six u32 → 40, then u64 pts → 48. + assert_eq!( + ( + size_of::(), + offset_of!(RawSurface, chroma_offset), + offset_of!(RawSurface, pool), + offset_of!(RawSurface, pts) + ), + (48, 24, 36, 40) + ); assert_eq!(Codec::Hevc as i32, 1); } } diff --git a/crates/pf-nx-sys/tests/layout.rs b/crates/pf-nx-sys/tests/layout.rs index 7799e4a..aefaef2 100644 --- a/crates/pf-nx-sys/tests/layout.rs +++ b/crates/pf-nx-sys/tests/layout.rs @@ -8,7 +8,7 @@ use std::mem::{align_of, offset_of, size_of}; use pf_nx_sys::applet::AppletHookCookie; use pf_nx_sys::audout::AudioOutBuffer; -use pf_nx_sys::av::{PictureMeta, RawStats}; +use pf_nx_sys::av::{PictureMeta, RawStats, RawSurface}; use pf_nx_sys::clk::ClkrstSession; use pf_nx_sys::fb::RawFramebuffer; use pf_nx_sys::hid::*; @@ -122,4 +122,8 @@ fn shim_structs() { assert_eq!(layout::(), (40, 8)); assert_eq!(offset_of!(RawStats, in_flight), 8); assert_eq!(offset_of!(RawStats, nvdec_khz), 32); + // PfAvSurface: frame*, base*, 6 × u32 → 40, u64 pts → 48. + assert_eq!(layout::(), (48, 8)); + assert_eq!(offset_of!(RawSurface, pool), 36); + assert_eq!(offset_of!(RawSurface, pts), 40); } diff --git a/crates/pf-nx-vk/Cargo.toml b/crates/pf-nx-vk/Cargo.toml index 109cd4e..9d03260 100644 --- a/crates/pf-nx-vk/Cargo.toml +++ b/crates/pf-nx-vk/Cargo.toml @@ -25,5 +25,9 @@ punktfunk-core.workspace = true pf-client-video.workspace = true pf-nx-app = { path = "../pf-nx-app" } +# The lavapipe tests load the system's Vulkan loader. +[target.'cfg(target_os = "linux")'.dev-dependencies] +ash = { workspace = true, features = ["loaded"] } + [lints] workspace = true diff --git a/crates/pf-nx-vk/shaders/nv12_tiled.frag b/crates/pf-nx-vk/shaders/nv12_tiled.frag new file mode 100644 index 0000000..01473b1 --- /dev/null +++ b/crates/pf-nx-vk/shaders/nv12_tiled.frag @@ -0,0 +1,67 @@ +// NV12 where NVDEC wrote it → RGBA. Both planes are uniform texel buffers over the decoder's +// surface, 16Bx2 block-linear with two GOBs to a block: 64 bytes by 16 rows, 1 KiB, blocks in +// row order, `pitch` bytes a row. `block_linear` is in_place.rs's twin. The colour rows and the +// chroma siting are nv12_csc.frag's; the lane sends only SDR here. A texel buffer has no +// sampler, so the bilinear filter is done by hand. +// +// Regenerate: glslc -O nv12_tiled.frag -o nv12_tiled.frag.spv (the .spv is committed). +#version 450 + +layout(location = 0) in vec2 v_uv; +layout(location = 0) out vec4 frag; + +layout(set = 0, binding = 0) uniform samplerBuffer u_y; // R8_UNORM: the luma plane +layout(set = 0, binding = 1) uniform samplerBuffer u_c; // R8G8_UNORM: the CbCr plane + +layout(push_constant) uniform Csc { + vec4 r0; + vec4 r1; + vec4 r2; + vec4 params; // nv12_csc.frag's; unused, the stream is SDR + uvec4 surface; // x: pitch in bytes, y: width, z: height in luma texels +} pc; + +uint block_linear(uint x, uint y, uint pitch) { + return (y >> 4) * (pitch << 4) + (x >> 6) * 1024u + ((y >> 3) & 1u) * 512u + + ((x >> 5) & 1u) * 256u + ((y >> 1) & 3u) * 64u + ((x >> 4) & 1u) * 32u + + (y & 1u) * 16u + (x & 15u); +} + +float luma(ivec2 p) { + return texelFetch(u_y, int(block_linear(uint(p.x), uint(p.y), pc.surface.x))).r; +} + +vec2 chroma(ivec2 p) { + return texelFetch(u_c, int(block_linear(uint(p.x) * 2u, uint(p.y), pc.surface.x) >> 1)).rg; +} + +// `pos` in texel space, centres on .5; edges clamp, as the sampled path's CLAMP_TO_EDGE. +float bilinear_y(vec2 pos, ivec2 size) { + vec2 p = pos - 0.5; + vec2 f = fract(p); + ivec2 a = clamp(ivec2(floor(p)), ivec2(0), size - 1); + ivec2 b = clamp(ivec2(floor(p)) + 1, ivec2(0), size - 1); + return mix(mix(luma(a), luma(ivec2(b.x, a.y)), f.x), mix(luma(ivec2(a.x, b.y)), luma(b), f.x), f.y); +} + +vec2 bilinear_c(vec2 pos, ivec2 size) { + vec2 p = pos - 0.5; + vec2 f = fract(p); + ivec2 a = clamp(ivec2(floor(p)), ivec2(0), size - 1); + ivec2 b = clamp(ivec2(floor(p)) + 1, ivec2(0), size - 1); + return mix(mix(chroma(a), chroma(ivec2(b.x, a.y)), f.x), mix(chroma(ivec2(a.x, b.y)), chroma(b), f.x), f.y); +} + +void main() { + ivec2 size = ivec2(pc.surface.yz); + ivec2 csize = (size + 1) / 2; + // Left-cosited 4:2:0 chroma: a quarter chroma texel right of the luma position. + vec2 cpos = v_uv * vec2(csize) + vec2(0.25, 0.0); + vec3 yuv = vec3(bilinear_y(v_uv * vec2(size), size), bilinear_c(cpos, csize)); + vec3 rgb = vec3( + dot(pc.r0.xyz, yuv) + pc.r0.w, + dot(pc.r1.xyz, yuv) + pc.r1.w, + dot(pc.r2.xyz, yuv) + pc.r2.w + ); + frag = vec4(clamp(rgb, 0.0, 1.0), 1.0); +} diff --git a/crates/pf-nx-vk/shaders/nv12_tiled.frag.spv b/crates/pf-nx-vk/shaders/nv12_tiled.frag.spv new file mode 100644 index 0000000000000000000000000000000000000000..c32481e50acdc5314c74570395376f388aeadc32 GIT binary patch literal 6720 zcmYkAYmA*$6^1`sD4n*@d(qaGQ)|l)%Ct&LZ_t***cKS*v^ZWW*pM`~h1e!gj3&fD zsWg$qR0B~=3tsp^ynweLNH25>5{ywI8Wlm~B|tPrF&b~6KF^u&#kF(xeCvJJ+H0-7 z_uBh>bH9*jvj6(3(W;gN}`ljaeo~&wt ze`w*1DL18Xj*;sW&J5*DIAhI4Zav-jAK_^}`V9VmKBx8OF%O+hy>kklPNv44Pu|8- zo{7yko*lYPd=}3R-56(sC-aokeikFA^6b#n&EhFmcV0X8Tx9bOJ|Ec_VxLRs8bp5q z(0rm_j%@7U703pN{TkruMZZ=(1ETMa!q>+SfUExv;Pv$QA4KjHyan04ihdZ`HF#6g z*KFhIe2rh5`P$|X>3ux4>oCb{Bx!PMT5L_(;(z-n?5*ccWc|B-uJ10Mu9h&-7Or##P+X`-)Ti0bykD=_s*ux!#GjF?`b$<-b^|3Ez*5-B`&V19upMW<` zdgk^xJh6`lpX}l0L}HA9wUNJ&xUQSEVAJ#;FxKKF{B^A-(B1QM=@(<^x=(ER&D4+k zGl?!|=hO3f3%2g(nGou4#h-X8vYcyju;pfeCyR5@LWO>&iV?GS#(>O+b#<(V)_fNxl*0&JXvvUvFI5yYYbMjfR zd~6Q4Hk!re2tfW4MtFwrMa~|3cNkA!_ra?hA)fdFWXDSTVQ>vi@%4F z&Bc2^{=N;Cul^nZ%lmy0e~*IosV@E=LpDFpcl;d%%MZXMzhhwe4aMJaus+qr-{Z*U z=bFafNw9p)Zv-sw_f7IU1=gp!jC~T>{OZ{6g5|fAu}^^=uP$SsMmCq>V)s4ubamF0 zwfK2t`5j=ttG4f}c7nf%ET^5d={pmb$FhACcv2ef$Zmzgi!E2Fuqqd<86D>*FtAeX2{0zaz`nJ^m-y z7VKi&OyoOapogUGq}vfq8=Quli@Si9`^Y_MFtbLN8O zy>qfA^TG1sssq%h%e!5iDQlcr#eO z*7+bO4GIH>O!cf#q@ zyHfijt)2Vjm_u;#bIm3IQeDV zF*y00{il)T>e>H3SiZh*`~Yl>`knAYuzdYacn0ivb&2sKWMkAj;#shKJ#Rk-%hx;N zCt!W5i@%>Dn@jyZ`59Qg`ujOpzJ8xP2iB*$_S=^7VfCC0M@RFTVoo zQ(gT18rl5nchPUa@-@HTg5~RX(eJ?eRF_;{L^i)V_9d`JJ{vO#}hM8OL zkUxO6RhOJkBFlH%8uJ}w1T3HPeG1vy%=vx-Sx!6uck$dki7aooc{WmypMkUXZK?HF zz}9-|^RHm-QrCY&mb0#tYaeG?zSiOdIQd$O6T#M^y3BnFvV5(lX<+@;dYTR=U)OvF zSiaWNOt3!HCB|%I`C1ci1{*SqQpus+pgpN5d->wU8oY>X~*$~nCnEI$C3+J7flzTP*(V124fjBUvB z_0G5kY>b-owP5*rXS^Tmcy;l29kP7AA69_n%{lM6E5Y)qnN`T{O=@N}vYd9QnKj7r zc3a)oopA1J^0*D`{?zk(2UxzY@tt7#dIk@HP&y+SfQxAdV?eX)q z*2XjC`O>d;o+fm;Cq(#>irFC8PDZ(f8X#oMbG3;o)@Yjpa}7OyJqP+W--}wE?{?yg!H$gf$|6)g`AZksU8T#2R;aUPX8P%vZfNBR@>%!*9ByHpi*G6ZF)_ syILF6uXFD%<6`%omU9k6yc4(&?}c-`_UgCN*K*Mh@l^L3kyq0H2Oy+8RsaA1 literal 0 HcmV?d00001 diff --git a/crates/pf-nx-vk/src/device.rs b/crates/pf-nx-vk/src/device.rs index 3aeba6f..8a4f929 100644 --- a/crates/pf-nx-vk/src/device.rs +++ b/crates/pf-nx-vk/src/device.rs @@ -2,7 +2,8 @@ //! [`VulkanDecodeDevice`] the shared `PyroWave` decoder builds on. //! //! NVK is not conformant on the X1, so the device asks for the least the presenter needs: -//! `synchronization2` for its barriers, nothing else. Robust buffer access stays off: on the +//! `synchronization2` for its barriers, and `VK_EXT_external_memory_host` where offered, for +//! pictures sampled in place. Robust buffer access stays off: on the //! X1's SM 5.3 NVK checks each storage access with a branch. The `PyroWave` conjuncts (int16, //! 8-bit storage, subgroup control, timeline semaphores) are asked for only with the //! `pyrowave` feature and only where the probe found them all. @@ -49,6 +50,8 @@ pub struct GpuDevice { pub name: String, /// The handoff the `PyroWave` decoder is built from; carries the shared queue lock. pub vkd: VulkanDecodeDevice, + /// `VK_EXT_external_memory_host` is on: host memory can be imported. + pub host_import: bool, pub target: Target, messenger: Option, } @@ -198,10 +201,18 @@ impl GpuDevice { .ok_or_else(|| anyhow!("no Vulkan 1.3 device with a graphics+compute queue"))?; let pyro = cfg!(feature = "pyrowave") && p.ok; - let device_exts: Vec<&CStr> = match target { + let mut device_exts: Vec<&CStr> = match target { Target::Vi { .. } => vec![ash::khr::swapchain::NAME], Target::Offscreen { .. } => Vec::new(), }; + // SAFETY: read-only query on a live physical device. + let offered = unsafe { instance.enumerate_device_extension_properties(pdev) }.unwrap_or_default(); + let host_import = offered + .iter() + .any(|e| e.extension_name_as_c_str() == Ok(ash::ext::external_memory_host::NAME)); + if host_import { + device_exts.push(ash::ext::external_memory_host::NAME); + } let ext_ptrs: Vec<*const std::ffi::c_char> = device_exts.iter().map(|n| n.as_ptr()).collect(); let prio = [1.0f32]; let queue_ci = [vk::DeviceQueueCreateInfo::default() @@ -284,6 +295,7 @@ impl GpuDevice { api_version: p.api_version, name: p.name, vkd, + host_import, target, messenger: None, }) @@ -292,7 +304,7 @@ impl GpuDevice { /// The device's feature line for the probe and the first bundle. pub fn feature_line(&self) -> String { format!( - "vulkan {}.{} {} pyrowave={} int16={} sb8={} subgroup_ctl={} full_subgroups={} f16={} messenger={}", + "vulkan {}.{} {} pyrowave={} int16={} sb8={} subgroup_ctl={} full_subgroups={} f16={} host_import={} messenger={}", vk::api_version_major(self.api_version), vk::api_version_minor(self.api_version), self.name, @@ -302,6 +314,7 @@ impl GpuDevice { self.vkd.f_subgroup_size_control, self.vkd.f_compute_full_subgroups, self.vkd.f_shader_float16, + self.host_import, self.messenger.is_some(), ) } diff --git a/crates/pf-nx-vk/src/in_place.rs b/crates/pf-nx-vk/src/in_place.rs new file mode 100644 index 0000000..7a3df44 --- /dev/null +++ b/crates/pf-nx-vk/src/in_place.rs @@ -0,0 +1,538 @@ +//! Pictures sampled where NVDEC wrote them. A decoder surface is imported once +//! (`VK_EXT_external_memory_host`) as a buffer, with an R8 view of its luma plane and an RG8 view +//! of its `CbCr` plane, and the tiled CSC pass detiles the 16Bx2 block-linear layout as it samples. +//! No VIC transfer, no copy, no upload: a picture is published once NVDEC is done with it. +//! +//! NVDEC cycles through a few surfaces, so imports are kept, by address, while the lane lives. +//! [`InPlace::check`] reads one picture's surface back through the GPU at the addresses the shader +//! samples and compares it with the VIC's copy of the same picture: a layout or an import the +//! console does not honour shows there, before any picture goes out in place. + +use std::any::Any; +use std::sync::Arc; +use std::time::Instant; + +use anyhow::{ensure, Context, Result}; +use ash::vk; +use pf_client_video::video_color::ColorDesc; +use pf_nx_app::platform::StreamSpec; + +use crate::device::GpuDevice; +use crate::picture::{Picture, Planes, VideoSlot}; + +/// Surfaces kept imported: NVDEC's references and the pictures in flight, a dozen at the deepest +/// smoothing budget. +const MAX_IMPORTS: usize = 32; +/// A check samples this many rows by this many 16-byte atoms in each plane. +const CHECK_GRID: u32 = 16; +/// A byte read back within this of the VIC's copy is the same sample. +const CHECK_TOLERANCE: u8 = 2; +/// Luma samples spread less than this cannot tell one layout from another. +const CHECK_MIN_SPREAD: u8 = 32; + +/// A decoder surface as the lane hands it over: NV12, both planes 16Bx2 block-linear. +#[derive(Clone, Copy, Debug)] +pub struct TiledSurface { + /// The surface's memory, 4 KiB aligned, and its length, a 4 KiB multiple. + pub base: *const u8, + pub size: usize, + /// Bytes a row of either plane, a multiple of 64. + pub pitch: u32, + pub chroma_offset: u32, + pub width: u32, + pub height: u32, + /// The decoder's surface pool. A new one may reuse an older one's freed addresses. + pub pool: u32, +} + +/// The byte at column `x`, row `y` of a 16Bx2 block-linear plane `pitch` bytes wide with two +/// GOBs (16 rows) to a block. `shaders/nv12_tiled.frag` has the same function. +pub fn block_linear(x: u32, y: u32, pitch: u32) -> u32 { + (y >> 4) * (pitch << 4) + + (x >> 6) * 1024 + + ((y >> 3) & 1) * 512 + + ((x >> 5) & 1) * 256 + + ((y >> 1) & 3) * 64 + + ((x >> 4) & 1) * 32 + + (y & 1) * 16 + + (x & 15) +} + +/// What a check found. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum Check { + /// The GPU read the sampled atoms as the VIC wrote them, but for `differ` of `atoms`. + Match { atoms: usize, differ: usize }, + /// Too many of the sampled atoms came back otherwise: not a layout to sample in place. + Differ { atoms: usize, differ: usize }, + /// Too little detail to tell layouts apart; a later picture decides. + Flat, +} + +/// One surface's import: the buffer over its memory and the plane views the shader reads. +struct Import { + dev: Arc, + base: usize, + size: usize, + pool: u32, + buffer: vk::Buffer, + memory: vk::DeviceMemory, + views: [vk::BufferView; 2], +} + +impl Drop for Import { + fn drop(&mut self) { + // SAFETY: pictures hold an import until the frame that sampled it has finished, and a + // check waits its copy, so nothing in flight reads it; null handles are no-ops. + unsafe { + for view in self.views { + self.dev.device.destroy_buffer_view(view, None); + } + self.dev.device.destroy_buffer(self.buffer, None); + self.dev.device.free_memory(self.memory, None); + } + } +} + +/// The check's copy out of a surface into memory the CPU reads. +struct Readback { + dev: Arc, + pool: vk::CommandPool, + cmd: vk::CommandBuffer, + fence: vk::Fence, + buffer: vk::Buffer, + memory: vk::DeviceMemory, + ptr: *const u8, +} + +impl Drop for Readback { + fn drop(&mut self) { + // SAFETY: every check waits its fence, so the copy is done; the handles are this one's. + unsafe { + self.dev.device.destroy_fence(self.fence, None); + self.dev.device.destroy_command_pool(self.pool, None); + self.dev.device.destroy_buffer(self.buffer, None); + self.dev.device.free_memory(self.memory, None); + } + } +} + +/// Decoder surfaces published to the presenter's slot as they are. +pub struct InPlace { + dev: Arc, + slot: Arc, + color: ColorDesc, + hud: Arc, + e2e: pf_nx_app::platform::VideoE2e, + host: ash::ext::external_memory_host::Device, + /// Oldest first. + imports: Vec>, + readback: Option, +} + +// SAFETY: the read-back pointer maps memory this value owns, read only by `check` under +// `&mut self`; the rest is handles and `Arc`s that are `Send`. +unsafe impl Send for InPlace {} + +impl InPlace { + /// For `spec`'s stream on `dev`, publishing to `slot`. Fails where the device cannot import + /// host memory or read the planes as texel buffers, or for an HDR stream. + pub fn new(dev: Arc, slot: Arc, spec: &StreamSpec) -> Result { + ensure!(dev.host_import, "the device cannot import host memory"); + let color = crate::picture::color_of(spec); + ensure!( + !color.is_pq(), + "an HDR stream needs the tonemap, which the tiled pass leaves out" + ); + for format in [vk::Format::R8_UNORM, vk::Format::R8G8_UNORM] { + // SAFETY: read-only query on a live physical device. + let props = unsafe { dev.instance.get_physical_device_format_properties(dev.pdev, format) }; + ensure!( + props + .buffer_features + .contains(vk::FormatFeatureFlags::UNIFORM_TEXEL_BUFFER), + "the device has no {format:?} texel buffers" + ); + } + let host = ash::ext::external_memory_host::Device::new(&dev.instance, &dev.device); + Ok(Self { + dev, + slot, + color, + hud: spec.hud.clone(), + e2e: spec.video_e2e.clone(), + host, + imports: Vec::new(), + readback: None, + }) + } + + /// Queues the picture in `s` on the slot, due on glass at `due`. `hold` keeps the surface + /// from being decoded into again; the picture keeps it until its last frame has sampled it. + pub fn publish( + &mut self, + s: &TiledSurface, + pts_ns: u64, + due: Option, + hold: Box, + ) -> Result<()> { + let import = self.import(s)?; + self.slot.publish(Picture { + planes: Planes::Tiled { + views: import.views, + pitch: s.pitch, + }, + layout: vk::ImageLayout::UNDEFINED, + width: s.width, + height: s.height, + color: self.color, + depth: 8, + msb_packed: false, + uv_scale: [1.0, 1.0], + decoded_at: Instant::now(), + due, + pts_ns, + decoded_ns: u64::try_from(punktfunk_core::client::now_realtime_ns()).unwrap_or(0), + turn: None, + hud: Some(self.hud.clone()), + e2e: Some(self.e2e.clone()), + // The import drops first: the GPU's mapping ends before NVDEC may reuse the surface. + hold: Box::new((import, hold)), + }); + Ok(()) + } + + /// Reads `s` back through the GPU at the addresses the tiled pass samples and compares it with + /// the same picture as the VIC wrote it, pitch-linear, into `y` and `uv`. + pub fn check(&mut self, s: &TiledSurface, y: &[u8], y_stride: usize, uv: &[u8], uv_stride: usize) -> Result { + ensure!( + s.width >= 32 && s.height >= 2, + "a {}x{} picture is too small to check", + s.width, + s.height + ); + let atoms = atoms(s, y_stride, uv_stride); + let import = self.import(s)?; + let bytes = atoms.len() * 16; + if self.readback.is_none() { + self.readback = Some(readback(&self.dev, bytes as u64)?); + } + let rb = self.readback.as_ref().expect("made just above"); + let regions: Vec = atoms + .iter() + .enumerate() + .map(|(i, a)| { + vk::BufferCopy::default() + .src_offset(a.surface) + .dst_offset(i as u64 * 16) + .size(16) + }) + .collect(); + let dev = &self.dev; + // SAFETY: the command buffer and fence are the read-back's and idle (each check waits + // them); the copy reads the import, which stays alive in the cache, into the read-back. + unsafe { + dev.device + .reset_command_buffer(rb.cmd, vk::CommandBufferResetFlags::empty())?; + dev.device.begin_command_buffer( + rb.cmd, + &vk::CommandBufferBeginInfo::default().flags(vk::CommandBufferUsageFlags::ONE_TIME_SUBMIT), + )?; + crate::util::memory_barrier( + dev, + rb.cmd, + ( + vk::PipelineStageFlags2::HOST | vk::PipelineStageFlags2::ALL_COMMANDS, + vk::AccessFlags2::HOST_WRITE | vk::AccessFlags2::MEMORY_WRITE, + ), + (vk::PipelineStageFlags2::COPY, vk::AccessFlags2::TRANSFER_READ), + ); + dev.device.cmd_copy_buffer(rb.cmd, import.buffer, rb.buffer, ®ions); + crate::util::memory_barrier( + dev, + rb.cmd, + (vk::PipelineStageFlags2::COPY, vk::AccessFlags2::TRANSFER_WRITE), + (vk::PipelineStageFlags2::HOST, vk::AccessFlags2::HOST_READ), + ); + dev.device.end_command_buffer(rb.cmd)?; + let cmds = [rb.cmd]; + { + let _q = dev.vkd.queue_lock.guard(); + dev.device + .queue_submit(dev.queue, &[vk::SubmitInfo::default().command_buffers(&cmds)], rb.fence)?; + } + let waited = dev.device.wait_for_fences(&[rb.fence], true, 1_000_000_000); + dev.device.reset_fences(&[rb.fence])?; + waited.context("check read-back")?; + } + // SAFETY: `ptr` maps `bytes` of host-coherent memory the copy above finished writing. + let got = unsafe { std::slice::from_raw_parts(rb.ptr, bytes) }; + let mut differ = 0; + let (mut lo, mut hi) = (u8::MAX, u8::MIN); + for (i, a) in atoms.iter().enumerate() { + let plane = if a.luma { y } else { uv }; + let want = plane + .get(a.linear..a.linear + 16) + .context("the VIC's copy is smaller than the picture")?; + if a.luma { + lo = want.iter().fold(lo, |m, &b| m.min(b)); + hi = want.iter().fold(hi, |m, &b| m.max(b)); + } + let read = &got[i * 16..i * 16 + 16]; + differ += usize::from(read.iter().zip(want).any(|(g, w)| g.abs_diff(*w) > CHECK_TOLERANCE)); + } + if hi.saturating_sub(lo) < CHECK_MIN_SPREAD { + return Ok(Check::Flat); + } + // A wrong layout or import reads back nearly every atom wrong; the VIC may round a few. + let atoms = atoms.len(); + Ok(if differ * 32 <= atoms { + Check::Match { atoms, differ } + } else { + Check::Differ { atoms, differ } + }) + } + + /// The import of `s`, made on first sight. Imports of an older pool go, those still on + /// screen with their pictures; past [`MAX_IMPORTS`] the oldest that no picture holds goes. + fn import(&mut self, s: &TiledSurface) -> Result> { + self.imports.retain(|i| i.pool == s.pool); + let base = s.base as usize; + if let Some(import) = self.imports.iter().find(|i| i.base == base && i.size == s.size) { + return Ok(import.clone()); + } + if self.imports.len() >= MAX_IMPORTS { + if let Some(at) = self.imports.iter().position(|i| Arc::strong_count(i) == 1) { + self.imports.remove(at); + } + } + let import = Arc::new(make_import(&self.dev, &self.host, s)?); + self.imports.push(import.clone()); + Ok(import) + } +} + +/// Imports `s`'s memory as a buffer with its two plane views. +fn make_import( + dev: &Arc, + host: &ash::ext::external_memory_host::Device, + s: &TiledSurface, +) -> Result { + // SAFETY: read-only query on a live physical device. + let limits = unsafe { dev.instance.get_physical_device_properties(dev.pdev) }.limits; + let (size, chroma) = (s.size as u64, u64::from(s.chroma_offset)); + ensure!( + chroma > 0 && chroma < size && chroma % limits.min_texel_buffer_offset_alignment.max(1) == 0, + "the CbCr plane at {chroma} of {size} bytes cannot start a texel buffer" + ); + ensure!( + size <= u64::from(limits.max_texel_buffer_elements), + "a {size}-byte surface outgrows the device's texel buffers" + ); + // Handles are filled in place: a failed step drops the import, which destroys what was made. + let mut import = Import { + dev: dev.clone(), + base: s.base as usize, + size: s.size, + pool: s.pool, + buffer: vk::Buffer::null(), + memory: vk::DeviceMemory::null(), + views: [vk::BufferView::null(); 2], + }; + let handle = vk::ExternalMemoryHandleTypeFlags::HOST_ALLOCATION_EXT; + let mut external = vk::ExternalMemoryBufferCreateInfo::default().handle_types(handle); + // SAFETY: the builders are locals that outlive the calls; `s.base` is 4 KiB aligned host + // memory the decoder keeps for longer than the import, which the picture holds before it. + unsafe { + import.buffer = dev.device.create_buffer( + &vk::BufferCreateInfo::default() + .size(size) + .usage(vk::BufferUsageFlags::UNIFORM_TEXEL_BUFFER | vk::BufferUsageFlags::TRANSFER_SRC) + .sharing_mode(vk::SharingMode::EXCLUSIVE) + .push_next(&mut external), + None, + )?; + let mut props = vk::MemoryHostPointerPropertiesEXT::default(); + (host.fp().get_memory_host_pointer_properties_ext)(dev.device.handle(), handle, s.base.cast(), &mut props) + .result() + .context("host pointer properties")?; + let req = dev.device.get_buffer_memory_requirements(import.buffer); + ensure!( + req.size <= size, + "the buffer wants {} bytes of a {size}-byte surface", + req.size + ); + let bits = props.memory_type_bits & req.memory_type_bits; + // Cached keeps the decoder's pages as they are; a coherent import would make them uncached. + let ty = dev + .memory_type(bits, vk::MemoryPropertyFlags::HOST_CACHED) + .or_else(|| dev.memory_type(bits, vk::MemoryPropertyFlags::empty())) + .context("no memory type takes the decoder's surface")?; + let mut from_host = vk::ImportMemoryHostPointerInfoEXT::default() + .handle_type(handle) + .host_pointer(s.base.cast_mut().cast()); + import.memory = dev + .device + .allocate_memory( + &vk::MemoryAllocateInfo::default() + .allocation_size(size) + .memory_type_index(ty) + .push_next(&mut from_host), + None, + ) + .context("import the decoder's surface")?; + dev.device.bind_buffer_memory(import.buffer, import.memory, 0)?; + for (i, (format, offset, range)) in [ + (vk::Format::R8_UNORM, 0, chroma), + (vk::Format::R8G8_UNORM, chroma, (size - chroma) & !1), + ] + .into_iter() + .enumerate() + { + import.views[i] = dev.device.create_buffer_view( + &vk::BufferViewCreateInfo::default() + .buffer(import.buffer) + .format(format) + .offset(offset) + .range(range), + None, + )?; + } + } + Ok(import) +} + +/// The read-back for a check of `bytes`, with its command buffer and fence. +fn readback(dev: &Arc, bytes: u64) -> Result { + let (buffer, memory, ptr) = crate::util::host_buffer(dev, bytes, vk::BufferUsageFlags::TRANSFER_DST)?; + // Filled in place: a failed step drops the read-back, which destroys what was made. + let mut rb = Readback { + dev: dev.clone(), + pool: vk::CommandPool::null(), + cmd: vk::CommandBuffer::null(), + fence: vk::Fence::null(), + buffer, + memory, + ptr: ptr.cast_const(), + }; + // SAFETY: the builders are locals; the objects are the read-back's, destroyed in its Drop. + unsafe { + rb.pool = dev.device.create_command_pool( + &vk::CommandPoolCreateInfo::default() + .queue_family_index(dev.qf) + .flags(vk::CommandPoolCreateFlags::RESET_COMMAND_BUFFER), + None, + )?; + rb.cmd = dev.device.allocate_command_buffers( + &vk::CommandBufferAllocateInfo::default() + .command_pool(rb.pool) + .level(vk::CommandBufferLevel::PRIMARY) + .command_buffer_count(1), + )?[0]; + rb.fence = dev.device.create_fence(&vk::FenceCreateInfo::default(), None)?; + } + Ok(rb) +} + +/// One 16-byte atom of a check: its offset in the surface, and where the same bytes sit in the +/// pitch-linear copy's luma or `CbCr` plane. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +struct Atom { + surface: u64, + luma: bool, + linear: usize, +} + +/// A [`CHECK_GRID`]-square grid of atoms over each plane, its rows and columns spread from edge +/// to edge, so every bit of the block-linear address takes both values. +fn atoms(s: &TiledSurface, y_stride: usize, uv_stride: usize) -> Vec { + let mut out = Vec::with_capacity(2 * (CHECK_GRID * CHECK_GRID) as usize); + let planes = [ + (true, s.width, s.height, 0, y_stride), + ( + false, + s.width.next_multiple_of(2), + s.height.div_ceil(2), + s.chroma_offset, + uv_stride, + ), + ]; + for (luma, row_bytes, rows, base, stride) in planes { + for i in 0..CHECK_GRID { + let y = i * (rows - 1) / (CHECK_GRID - 1); + for j in 0..CHECK_GRID { + let x = (j * (row_bytes - 16) / (CHECK_GRID - 1)) & !15; + out.push(Atom { + surface: u64::from(base + block_linear(x, y, s.pitch)), + luma, + linear: y as usize * stride + x as usize, + }); + } + } + } + out +} + +#[cfg(test)] +mod tests { + use super::*; + + /// The layout's own rules, apart from the formula: a block is 64 bytes by 16 rows, 1 KiB, + /// its first GOB the top 8 rows; inside a GOB a 16-byte row segment is contiguous, two rows + /// interleave per 16 bytes, and 32-byte halves follow the row pairs. + #[test] + fn block_linear_follows_the_16bx2_gob() { + let pitch = 256; + assert_eq!(block_linear(0, 0, pitch), 0); + assert_eq!(block_linear(15, 0, pitch), 15); + assert_eq!(block_linear(0, 1, pitch), 16); + assert_eq!(block_linear(16, 0, pitch), 32); + assert_eq!(block_linear(0, 2, pitch), 64); + assert_eq!(block_linear(32, 0, pitch), 256); + assert_eq!(block_linear(0, 8, pitch), 512); + assert_eq!(block_linear(64, 0, pitch), 1024); + assert_eq!(block_linear(0, 16, pitch), 16 * pitch); + assert_eq!(block_linear(255, 15, pitch), 4 * 1024 - 1); + } + + #[test] + fn block_linear_is_a_permutation_of_each_block_row() { + let (pitch, rows) = (192, 32); + let mut seen = vec![false; (pitch * rows) as usize]; + for y in 0..rows { + for x in 0..pitch { + let a = block_linear(x, y, pitch) as usize; + assert!(!seen[a], "{x},{y} lands on a taken byte"); + seen[a] = true; + } + } + assert!(seen.iter().all(|&s| s)); + } + + #[test] + fn check_atoms_stay_inside_both_layouts() { + let s = TiledSurface { + base: std::ptr::null(), + size: 0, + pitch: 1920, + chroma_offset: 1920 * 1088, + width: 1920, + height: 1080, + pool: 0, + }; + let atoms = atoms(&s, 2048, 2048); + assert_eq!(atoms.len(), 2 * 16 * 16); + for a in &atoms { + assert_eq!(a.surface % 16, 0, "an atom starts a 16-byte segment"); + if a.luma { + assert!(a.surface + 16 <= u64::from(s.chroma_offset)); + assert!(a.linear + 16 <= 2048 * 1079 + 1920); + } else { + assert!(a.surface + 16 <= u64::from(s.chroma_offset) + 1920 * 544); + assert!(a.linear + 16 <= 2048 * 539 + 1920); + } + } + let rows: std::collections::BTreeSet = atoms.iter().map(|a| a.linear as u64 / 2048).collect(); + assert!(rows.contains(&0) && rows.contains(&1079), "edge rows are sampled"); + } +} diff --git a/crates/pf-nx-vk/src/lib.rs b/crates/pf-nx-vk/src/lib.rs index 59a77eb..5efcbee 100644 --- a/crates/pf-nx-vk/src/lib.rs +++ b/crates/pf-nx-vk/src/lib.rs @@ -7,6 +7,7 @@ #![cfg(any(target_os = "linux", target_os = "horizon"))] pub mod device; +pub mod in_place; pub mod output; pub mod picture; pub mod pipelines; @@ -18,6 +19,7 @@ pub mod ui; pub mod util; pub use device::{GpuDevice, Target}; +pub use in_place::{Check, InPlace, TiledSurface}; pub use picture::{Drops, Nv12Upload, Picture, Planes, StagingPlanes, VideoSlot}; pub use presenter::Presenter; #[cfg(feature = "pyrowave")] diff --git a/crates/pf-nx-vk/src/picture.rs b/crates/pf-nx-vk/src/picture.rs index 4935734..75c4208 100644 --- a/crates/pf-nx-vk/src/picture.rs +++ b/crates/pf-nx-vk/src/picture.rs @@ -26,6 +26,9 @@ pub enum Planes { Three([vk::ImageView; 3]), /// Y and interleaved `CbCr`: the NV12 upload. Two([vk::ImageView; 2]), + /// Y and `CbCr` texel buffers over a decoder's block-linear surface, `pitch` bytes a row: + /// NV12 sampled in place (`in_place`). + Tiled { views: [vk::BufferView; 2], pitch: u32 }, } pub struct Picture { @@ -294,6 +297,16 @@ impl Queue { } } +/// The stream's colour, as the CSC passes read it. +pub(crate) fn color_of(spec: &StreamSpec) -> ColorDesc { + ColorDesc { + primaries: spec.color.primaries, + transfer: spec.color.transfer, + matrix: spec.color.matrix, + full_range: spec.color.full_range != 0, + } +} + /// Rows in a staging buffer start this many bytes apart, at least. const ROW_ALIGN: usize = 256; /// Staging buffers: the decoder fills one while the GPU copies out of the other. @@ -395,12 +408,7 @@ impl Nv12Upload { slot, extent, stride, - color: ColorDesc { - primaries: spec.color.primaries, - transfer: spec.color.transfer, - matrix: spec.color.matrix, - full_range: spec.color.full_range != 0, - }, + color: color_of(spec), hud: spec.hud.clone(), e2e: spec.video_e2e.clone(), planes: Vec::new(), diff --git a/crates/pf-nx-vk/src/pipelines.rs b/crates/pf-nx-vk/src/pipelines.rs index 2f222b9..a807ba4 100644 --- a/crates/pf-nx-vk/src/pipelines.rs +++ b/crates/pf-nx-vk/src/pipelines.rs @@ -1,7 +1,7 @@ -//! The render passes and the three pipelines a frame uses: planar CSC (`PyroWave`), two-plane -//! CSC (the NV12 upload) and the UI layer's premultiplied blit. The shaders are the desktop -//! presenter's committed SPIR-V (`pf_client_video::video_csc_spv`), so the colour maths lives -//! in one place. +//! The render passes and the pipelines a frame uses: planar CSC (`PyroWave`), two-plane CSC (the +//! NV12 upload), tiled CSC (NV12 sampled in place) and the UI layer's premultiplied blit. All but +//! the tiled one are the desktop presenter's committed SPIR-V (`pf_client_video::video_csc_spv`); +//! the tiled one is this crate's (`shaders/nv12_tiled.frag`), with the same colour rows. use anyhow::{anyhow, Result}; use ash::vk; @@ -11,6 +11,10 @@ use crate::device::GpuDevice; /// Three CSC rows plus the params vec4. pub const PUSH_BYTES: u32 = 16 * 4; +/// [`PUSH_BYTES`] and the tiled surface's pitch, width and height, with one word spare. +pub const TILED_PUSH_BYTES: u32 = PUSH_BYTES + 4 * 4; + +const TILED_FRAG: &[u8] = include_bytes!("../shaders/nv12_tiled.frag.spv"); pub struct Pipe { pub set_layout: vk::DescriptorSetLayout, @@ -26,6 +30,8 @@ pub struct Pipelines { pub sampler: vk::Sampler, pub planar: Pipe, pub two_plane: Pipe, + /// Two uniform texel buffers over a decoder's block-linear surface. + pub tiled: Pipe, pub overlay: Pipe, } @@ -35,10 +41,30 @@ impl Pipelines { let sampler = crate::util::sampler(dev)?; let pass_out = render_pass(dev, format, out_layout)?; let pass_read = render_pass(dev, format, vk::ImageLayout::TRANSFER_SRC_OPTIMAL)?; + let image = vk::DescriptorType::COMBINED_IMAGE_SAMPLER; Ok(Self { - planar: pipe(dev, pass_out, sampler, 3, true, spv::PLANAR_CSC_FRAG, false)?, - two_plane: pipe(dev, pass_out, sampler, 2, true, spv::NV12_CSC_FRAG, false)?, - overlay: pipe(dev, pass_out, sampler, 1, false, spv::OVERLAY_FRAG, true)?, + planar: pipe( + dev, + pass_out, + sampler, + 3, + image, + PUSH_BYTES, + spv::PLANAR_CSC_FRAG, + false, + )?, + two_plane: pipe(dev, pass_out, sampler, 2, image, PUSH_BYTES, spv::NV12_CSC_FRAG, false)?, + tiled: pipe( + dev, + pass_out, + sampler, + 2, + vk::DescriptorType::UNIFORM_TEXEL_BUFFER, + TILED_PUSH_BYTES, + TILED_FRAG, + false, + )?, + overlay: pipe(dev, pass_out, sampler, 1, image, 0, spv::OVERLAY_FRAG, true)?, pass_out, pass_read, sampler, @@ -50,7 +76,7 @@ impl Pipelines { pub unsafe fn destroy(&self, dev: &GpuDevice) { // SAFETY: the caller guarantees the device is idle; every handle is owned here. unsafe { - for p in [&self.planar, &self.two_plane, &self.overlay] { + for p in [&self.planar, &self.two_plane, &self.tiled, &self.overlay] { dev.device.destroy_pipeline(p.pipeline, None); dev.device.destroy_pipeline_layout(p.layout, None); dev.device.destroy_descriptor_set_layout(p.set_layout, None); @@ -103,25 +129,32 @@ fn render_pass(dev: &GpuDevice, format: vk::Format, final_layout: vk::ImageLayou }?) } -/// A bufferless fullscreen-triangle pipeline sampling `planes` images through immutable samplers. +/// A bufferless fullscreen-triangle pipeline reading `planes` descriptors of type `desc`: images +/// through immutable samplers, or texel buffers. `push` bytes of push constants, `0` for none. +#[allow(clippy::too_many_arguments)] fn pipe( dev: &GpuDevice, pass: vk::RenderPass, sampler: vk::Sampler, planes: u32, - push: bool, + desc: vk::DescriptorType, + push: u32, frag: &[u8], premultiplied_blend: bool, ) -> Result { let immutable = [sampler]; let bindings: Vec = (0..planes) .map(|i| { - vk::DescriptorSetLayoutBinding::default() + let binding = vk::DescriptorSetLayoutBinding::default() .binding(i) - .descriptor_type(vk::DescriptorType::COMBINED_IMAGE_SAMPLER) + .descriptor_type(desc) .descriptor_count(1) - .stage_flags(vk::ShaderStageFlags::FRAGMENT) - .immutable_samplers(&immutable) + .stage_flags(vk::ShaderStageFlags::FRAGMENT); + if desc == vk::DescriptorType::COMBINED_IMAGE_SAMPLER { + binding.immutable_samplers(&immutable) + } else { + binding + } }) .collect(); // SAFETY: builders are locals outliving the calls; the objects go to the returned `Pipe`. @@ -131,10 +164,10 @@ fn pipe( }?; let ranges = [vk::PushConstantRange::default() .stage_flags(vk::ShaderStageFlags::FRAGMENT) - .size(PUSH_BYTES)]; + .size(push)]; let set_layouts = [set_layout]; let mut layout_ci = vk::PipelineLayoutCreateInfo::default().set_layouts(&set_layouts); - if push { + if push > 0 { layout_ci = layout_ci.push_constant_ranges(&ranges); } // SAFETY: as above. diff --git a/crates/pf-nx-vk/src/presenter.rs b/crates/pf-nx-vk/src/presenter.rs index 6f84012..83fdc31 100644 --- a/crates/pf-nx-vk/src/presenter.rs +++ b/crates/pf-nx-vk/src/presenter.rs @@ -30,8 +30,8 @@ struct FrameRes { fence: vk::Fence, /// Signalled by the acquire; waited by this frame's submit. ready: vk::Semaphore, - /// planar, two-plane, overlay. - sets: [vk::DescriptorSet; 3], + /// planar, two-plane, overlay, tiled. + sets: [vk::DescriptorSet; 4], /// The picture this frame sampled, released once its fence signals. in_use: Option>, } @@ -66,7 +66,7 @@ pub struct Presenter { stream: Option, last_present: Option, readback: Option, - readback_sets: [vk::DescriptorSet; 3], + readback_sets: [vk::DescriptorSet; 4], /// The extent asked of a surface that leaves it to the caller. want: vk::Extent2D, /// The swapchain no longer matches the window: rebuild before the next acquire. @@ -108,10 +108,15 @@ impl Presenter { None, ) }?; - let sets_needed = (FRAMES as u32 + 1) * 3; - let sizes = [vk::DescriptorPoolSize::default() - .ty(vk::DescriptorType::COMBINED_IMAGE_SAMPLER) - .descriptor_count(sets_needed * 3)]; + let sets_needed = (FRAMES as u32 + 1) * 4; + let sizes = [ + vk::DescriptorPoolSize::default() + .ty(vk::DescriptorType::COMBINED_IMAGE_SAMPLER) + .descriptor_count(sets_needed * 3), + vk::DescriptorPoolSize::default() + .ty(vk::DescriptorType::UNIFORM_TEXEL_BUFFER) + .descriptor_count((FRAMES as u32 + 1) * 2), + ]; // SAFETY: as above. let desc_pool = unsafe { dev.device.create_descriptor_pool( @@ -280,7 +285,8 @@ impl Presenter { } } - /// Records the composition into `fb`: clear, picture, and the UI layer while it is on. + /// Records the composition into `fb`: clear, picture, and the UI layer while it is on. A + /// picture in place is preceded by a barrier: NVDEC wrote it outside Vulkan. /// /// # Safety /// `cmd` is recording, `fb` belongs to `pass`, and `sets` are not read by work in flight. @@ -289,7 +295,7 @@ impl Presenter { cmd: vk::CommandBuffer, pass: vk::RenderPass, fb: vk::Framebuffer, - sets: [vk::DescriptorSet; 3], + sets: [vk::DescriptorSet; 4], ) { let extent = self.output.extent; let full = vk::Rect2D { @@ -302,9 +308,21 @@ impl Presenter { }, }]; let dev = &self.dev.device; + let picture = self.stream.and(self.current.as_ref()); // SAFETY: forwarded from the caller; every handle is owned by this presenter or by the // picture it holds for the frame. unsafe { + if picture.is_some_and(|p| matches!(p.planes, Planes::Tiled { .. })) { + crate::util::memory_barrier( + &self.dev, + cmd, + ( + vk::PipelineStageFlags2::HOST | vk::PipelineStageFlags2::ALL_COMMANDS, + vk::AccessFlags2::HOST_WRITE | vk::AccessFlags2::MEMORY_WRITE, + ), + (vk::PipelineStageFlags2::FRAGMENT_SHADER, vk::AccessFlags2::SHADER_READ), + ); + } dev.cmd_begin_render_pass( cmd, &vk::RenderPassBeginInfo::default() @@ -315,21 +333,42 @@ impl Presenter { vk::SubpassContents::INLINE, ); dev.cmd_set_scissor(cmd, 0, &[full]); - if let (Some(fit), Some(picture)) = (self.stream, self.current.as_ref()) { - let (pipe, set, views): (_, _, &[vk::ImageView]) = match &picture.planes { - Planes::Three(v) => (&self.pipes.planar, sets[0], v), - Planes::Two(v) => (&self.pipes.two_plane, sets[1], v), + if let (Some(fit), Some(picture)) = (self.stream, picture) { + let (pipe, set, surface) = match &picture.planes { + Planes::Three(v) => { + self.write_set(sets[0], v, picture.layout); + (&self.pipes.planar, sets[0], None) + } + Planes::Two(v) => { + self.write_set(sets[1], v, picture.layout); + (&self.pipes.two_plane, sets[1], None) + } + Planes::Tiled { views, pitch } => { + self.write_texel_set(sets[3], views); + ( + &self.pipes.tiled, + sets[3], + Some([*pitch, picture.width, picture.height, 0]), + ) + } }; - self.write_set(set, views, picture.layout); let rows = csc_rows(picture.color, picture.depth, picture.msb_packed); - let mut pc = [0f32; 16]; - pc[..12].copy_from_slice(rows.as_flattened()); + let mut pc = [0u32; 20]; + for (w, r) in pc.iter_mut().zip(rows.as_flattened()) { + *w = r.to_bits(); + } // The output is SDR: a PQ picture is tonemapped. - pc[12] = if picture.color.is_pq() { 1.0 } else { 0.0 }; - pc[13] = TONEMAP_PEAK; - pc[14] = picture.uv_scale[0]; - pc[15] = picture.uv_scale[1]; - let words = pc.map(f32::to_ne_bytes); + pc[12] = if picture.color.is_pq() { 1f32 } else { 0f32 }.to_bits(); + pc[13] = TONEMAP_PEAK.to_bits(); + pc[14] = picture.uv_scale[0].to_bits(); + pc[15] = picture.uv_scale[1].to_bits(); + let len = if let Some(surface) = surface { + pc[16..].copy_from_slice(&surface); + 20 + } else { + 16 + }; + let words = pc.map(u32::to_ne_bytes); dev.cmd_bind_pipeline(cmd, vk::PipelineBindPoint::GRAPHICS, pipe.pipeline); dev.cmd_set_viewport(cmd, 0, &[Self::fitted(fit, picture.width, picture.height, extent)]); dev.cmd_bind_descriptor_sets(cmd, vk::PipelineBindPoint::GRAPHICS, pipe.layout, 0, &[set], &[]); @@ -338,7 +377,7 @@ impl Presenter { pipe.layout, vk::ShaderStageFlags::FRAGMENT, 0, - words.as_flattened(), + &words.as_flattened()[..len * 4], ); dev.cmd_draw(cmd, 3, 1, 0, 0); } @@ -397,6 +436,24 @@ impl Presenter { unsafe { self.dev.device.update_descriptor_sets(&writes, &[]) }; } + /// Points `set`'s two texel-buffer bindings at a tiled picture's plane views. + fn write_texel_set(&self, set: vk::DescriptorSet, views: &[vk::BufferView; 2]) { + let writes: Vec = views + .iter() + .enumerate() + .map(|(i, view)| { + vk::WriteDescriptorSet::default() + .dst_set(set) + .dst_binding(i as u32) + .descriptor_type(vk::DescriptorType::UNIFORM_TEXEL_BUFFER) + .texel_buffer_view(std::slice::from_ref(view)) + }) + .collect(); + // SAFETY: `writes` borrows `views`, both live for the call; the set is not in use by + // work in flight (its frame's fence was waited, or the device idled for read-back). + unsafe { self.dev.device.update_descriptor_sets(&writes, &[]) }; + } + fn ensure_readback(&mut self) -> Result<()> { if self.readback.is_some() { return Ok(()); @@ -715,12 +772,13 @@ unsafe fn destroy_readback(dev: &GpuDevice, rb: &Readback) { } } -/// One planar, one two-plane and one overlay set from `pool`, freed with it. -fn alloc_sets(dev: &GpuDevice, pool: vk::DescriptorPool, pipes: &Pipelines) -> Result<[vk::DescriptorSet; 3]> { +/// One planar, two-plane, overlay and tiled set from `pool`, freed with it. +fn alloc_sets(dev: &GpuDevice, pool: vk::DescriptorPool, pipes: &Pipelines) -> Result<[vk::DescriptorSet; 4]> { let layouts = [ pipes.planar.set_layout, pipes.two_plane.set_layout, pipes.overlay.set_layout, + pipes.tiled.set_layout, ]; // SAFETY: the pool and layouts are live; the sets are freed with the pool. let sets = unsafe { @@ -730,7 +788,7 @@ fn alloc_sets(dev: &GpuDevice, pool: vk::DescriptorPool, pipes: &Pipelines) -> R .set_layouts(&layouts), ) }?; - Ok([sets[0], sets[1], sets[2]]) + Ok([sets[0], sets[1], sets[2], sets[3]]) } fn semaphore(dev: &GpuDevice) -> Result { diff --git a/crates/pf-nx-vk/src/util.rs b/crates/pf-nx-vk/src/util.rs index a301eab..5c31d7b 100644 --- a/crates/pf-nx-vk/src/util.rs +++ b/crates/pf-nx-vk/src/util.rs @@ -144,6 +144,28 @@ pub unsafe fn barrier( } } +/// Records a global memory barrier from `src` to `dst`. +/// +/// # Safety +/// `cmd` is recording on `dev`. +pub unsafe fn memory_barrier( + dev: &GpuDevice, + cmd: vk::CommandBuffer, + src: (vk::PipelineStageFlags2, vk::AccessFlags2), + dst: (vk::PipelineStageFlags2, vk::AccessFlags2), +) { + let barrier = [vk::MemoryBarrier2::default() + .src_stage_mask(src.0) + .src_access_mask(src.1) + .dst_stage_mask(dst.0) + .dst_access_mask(dst.1)]; + // SAFETY: the caller guarantees `cmd` is recording. + unsafe { + dev.device + .cmd_pipeline_barrier2(cmd, &vk::DependencyInfo::default().memory_barriers(&barrier)); + } +} + /// Linear, clamped. Chroma is upsampled by the sampler and the UI layer is scaled by it. pub fn sampler(dev: &GpuDevice) -> Result { // SAFETY: the builder is a local; the sampler is returned to the caller. diff --git a/crates/pf-nx-vk/tests/in_place.rs b/crates/pf-nx-vk/tests/in_place.rs new file mode 100644 index 0000000..c90b295 --- /dev/null +++ b/crates/pf-nx-vk/tests/in_place.rs @@ -0,0 +1,212 @@ +//! The in-place route on lavapipe: a picture laid out as NVDEC lays it, 16Bx2 block-linear in +//! 4 KiB-aligned host memory, imported and drawn by the tiled pass, against the same picture +//! through the NV12 upload. Of the console's route only NVDEC is missing. +#![cfg(target_os = "linux")] + +use std::alloc::{alloc_zeroed, dealloc, Layout}; +use std::sync::atomic::AtomicI64; +use std::sync::Arc; + +use pf_nx_app::platform::{Display, Fit, StreamSpec, VideoE2e}; +use pf_nx_vk::in_place::block_linear; +use pf_nx_vk::{Check, GpuDevice, InPlace, Nv12Upload, Presenter, Target, TiledSurface, VideoSlot}; +use punktfunk_core::quic; + +const W: u32 = 200; +const H: u32 = 120; +/// NVDEC's pitch for `W` (64-byte aligned) and its plane height (32-row aligned). +const PITCH: u32 = 256; +const CHROMA_OFFSET: u32 = PITCH * 128; +const SIZE: usize = (CHROMA_OFFSET + PITCH * 64) as usize; + +fn luma(x: u32, y: u32) -> u8 { + ((x * 7 + y * 13 + (x * y) % 31) & 0xff) as u8 +} + +fn chroma(x: u32, y: u32) -> [u8; 2] { + [((x * 5 + y * 3 + 40) & 0xff) as u8, (((x * 11) ^ (y * 7)) & 0xff) as u8] +} + +fn spec() -> StreamSpec { + StreamSpec { + codec: quic::CODEC_HEVC, + width: W, + height: H, + refresh_hz: 60, + color: quic::ColorInfo { + primaries: 1, + transfer: 1, + matrix: 1, + full_range: 0, + }, + bit_depth: 8, + chroma444: false, + shard_payload: 1200, + smoothing: 0, + hud: Arc::new(punktfunk_core::hud::Stats::new(Arc::new(AtomicI64::new(0)))), + video_e2e: VideoE2e::default(), + } +} + +/// The picture pitch-linear, rows `stride` bytes apart: luma, then interleaved `CbCr`. +fn linear(stride: usize) -> (Vec, Vec) { + let mut y = vec![0; stride * H as usize]; + let mut uv = vec![0; stride * H.div_ceil(2) as usize]; + for r in 0..H { + for c in 0..W { + y[r as usize * stride + c as usize] = luma(c, r); + } + } + for r in 0..H.div_ceil(2) { + for c in 0..W / 2 { + let at = r as usize * stride + 2 * c as usize; + uv[at..at + 2].copy_from_slice(&chroma(c, r)); + } + } + (y, uv) +} + +/// 4 KiB-aligned host memory, as nvtegra allocates a surface. +struct Surface(*mut u8); + +impl Surface { + fn layout() -> Layout { + Layout::from_size_align(SIZE, 4096).unwrap() + } + + /// The picture block-linear, or written pitch-linear where `tiled` is false. + fn new(tiled: bool) -> Self { + // SAFETY: a non-zero layout. + let p = unsafe { alloc_zeroed(Self::layout()) }; + assert!(!p.is_null()); + // SAFETY: `p` holds SIZE bytes; every offset below stays inside them. + let mem = unsafe { std::slice::from_raw_parts_mut(p, SIZE) }; + let at = |x: u32, y: u32| if tiled { block_linear(x, y, PITCH) } else { y * PITCH + x } as usize; + for r in 0..H { + for c in 0..W { + mem[at(c, r)] = luma(c, r); + } + } + for r in 0..H.div_ceil(2) { + for c in 0..W / 2 { + let [u, v] = chroma(c, r); + mem[CHROMA_OFFSET as usize + at(2 * c, r)] = u; + mem[CHROMA_OFFSET as usize + at(2 * c + 1, r)] = v; + } + } + Self(p) + } + + fn tiled(&self) -> TiledSurface { + TiledSurface { + base: self.0, + size: SIZE, + pitch: PITCH, + chroma_offset: CHROMA_OFFSET, + width: W, + height: H, + pool: 0, + } + } +} + +impl Drop for Surface { + fn drop(&mut self) { + // SAFETY: allocated in `new` with this layout. + unsafe { dealloc(self.0, Self::layout()) }; + } +} + +fn frame(presenter: &mut Presenter) -> Vec { + presenter.frame(&mut |_| {}).expect("frame"); + presenter.read_back().expect("read back").expect("an image").pixels +} + +fn max_diff(a: &[u8], b: &[u8]) -> u8 { + assert_eq!(a.len(), b.len()); + a.iter().zip(b).map(|(x, y)| x.abs_diff(*y)).max().unwrap_or(0) +} + +/// The device, or `None` (said) on a box without Vulkan. +fn device(width: u32, height: u32) -> Option> { + // SAFETY: loads the system's Vulkan loader; nothing else in the test holds it. + let entry = unsafe { ash::Entry::load() } + .inspect_err(|e| eprintln!("no Vulkan loader: skipped ({e})")) + .ok()?; + let dev = GpuDevice::new(entry, Target::Offscreen { width, height }) + .inspect_err(|e| eprintln!("no Vulkan device: skipped ({e:#})")) + .ok()?; + assert!(dev.host_import, "lavapipe imports host memory"); + Some(Arc::new(dev)) +} + +/// The picture drawn onto a `width`×`height` output through the upload, in place, and in place +/// from a surface written pitch-linear. +fn draw_both(width: u32, height: u32) -> Option<(Vec, Vec, Vec)> { + let dev = device(width, height)?; + let slot = Arc::new(VideoSlot::default()); + let mut presenter = Presenter::new(dev.clone(), slot.clone()).expect("presenter"); + presenter.set_stream(true, Fit::Contain); + let spec = spec(); + let mut upload = Nv12Upload::new(dev.clone(), slot.clone(), &spec).expect("upload"); + let mut lane = InPlace::new(dev, slot, &spec).expect("in-place lane"); + let (surface, linear_surface) = (Surface::new(true), Surface::new(false)); + + let planes = upload.stage().expect("stage"); + let (y, uv) = linear(planes.y_stride); + planes.y[..y.len()].copy_from_slice(&y); + planes.uv[..uv.len()].copy_from_slice(&uv); + assert!(upload.commit(W, H, 0, None).expect("commit")); + let want = frame(&mut presenter); + lane.publish(&surface.tiled(), 0, None, Box::new(())).expect("publish"); + let got = frame(&mut presenter); + lane.publish(&linear_surface.tiled(), 0, None, Box::new(())) + .expect("publish"); + let wrong = frame(&mut presenter); + + // The imports go before the memory they wrap. + presenter.set_stream(false, Fit::Contain); + drop(presenter); + drop(lane); + Some((want, got, wrong)) +} + +// The upload's sampler filters in 8-bit fixed point, the tiled pass in float: about 1 LSB of +// chroma, which the CSC doubles. Neighbouring texels here differ by up to 255, so a wrong +// address or siting costs tens. +#[test] +fn tiled_pass_draws_what_the_upload_draws_at_one_to_one() { + let Some((want, got, wrong)) = draw_both(W, H) else { + return; + }; + assert!(max_diff(&want, &got) <= 3, "differs by {}", max_diff(&want, &got)); + assert!(max_diff(&want, &wrong) > 32, "a pitch-linear surface passes"); +} + +/// Scaled, the sampler also rounds its coordinates, so luma and chroma both carry its error. +#[test] +fn tiled_pass_draws_what_the_upload_draws_scaled() { + let Some((want, got, _)) = draw_both(W * 3 / 2, H * 3 / 2) else { + return; + }; + assert!(max_diff(&want, &got) <= 8, "differs by {}", max_diff(&want, &got)); +} + +/// The check reads a tiled surface back as the VIC would have copied it, and finds one written +/// pitch-linear wrong. +#[test] +fn check_tells_the_layouts_apart() { + let Some(dev) = device(W, H) else { return }; + let slot = Arc::new(VideoSlot::default()); + let mut lane = InPlace::new(dev, slot, &spec()).expect("in-place lane"); + let (tiled, linear_surface) = (Surface::new(true), Surface::new(false)); + let stride = PITCH as usize; + let (y, uv) = linear(stride); + let verdict = lane.check(&tiled.tiled(), &y, stride, &uv, stride).expect("check"); + assert_eq!(verdict, Check::Match { atoms: 512, differ: 0 }); + let verdict = lane + .check(&linear_surface.tiled(), &y, stride, &uv, stride) + .expect("check"); + assert!(matches!(verdict, Check::Differ { .. }), "{verdict:?}"); + drop(lane); +} diff --git a/docs/improvements.md b/docs/improvements.md index e7581e6..db44590 100644 --- a/docs/improvements.md +++ b/docs/improvements.md @@ -85,6 +85,18 @@ mode and can go in parallel. 3 is the busy one with the current build. If the remaining calls turn out to sit in `punktfunk-core`'s receive loop, that part moves to section 1. +### Pictures in place: the console run + +**What.** NVDEC's surfaces go to the presenter as they lie: imported once each through +`VK_EXT_external_memory_host` and detiled (16Bx2 block-linear) by the tiled CSC pass, with no +VIC transfer, no copy and no staging upload (`pf-nx-vk/src/in_place.rs`). Each decoder starts on +the VIC; its first detailed picture is read back through the GPU and compared with the VIC's +copy, and only a match moves the lane in place. A mismatch or a failed import keeps the VIC. + +**Test first.** A docked 1080p session: the log's `in place: picture … read back as the VIC +wrote it` line, then `nvdec[10s]` with `route in place`, transfer and copy at 0, and the picture +right. A file `vic-copia` in `/switch/punktfunk/` keeps the VIC route for the A/B. + ### NVDEC's top clock in handheld nvtegra rescales NVDEC every 20 pictures so that one takes about a frame interval @@ -92,15 +104,6 @@ nvtegra rescales NVDEC every 20 pictures so that one takes about a frame interva handheld keeps the scaling, at about 4 ms a 720p picture (NOTES, 2026-10-31). The maintainer keeps it off by default in handheld (2026-11-05); an opt-in could go with Performance mode. -### Pictures without the VIC and the copy - -**Why.** Each picture costs NVDEC, then a VIC transfer that maps and unmaps the destination -through nvdrv and flushes 3 MB of cache, then the 3 MB copy: about 3 and 3.7 ms at 1080p. - -**The idea.** NVK's fork imports an NvMap ID (`nvk_switch_allocate_shared_memory`). The decoded -surface's NvMap could be sampled in place, its 16Bx2 block-linear layout detiled in the CSC -shader. **Test first:** the import of a decoder surface, and the picture it samples. - ### The smoothness buffer's Automatic on the Switch The shared setting resolves Automatic to two frames on every client