From 2c65a71fcb3bd13d85f041af00a58ad257bd99a8 Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Sun, 22 Nov 2026 00:25:34 -0800 Subject: [PATCH] :racehorse: Fill no video planes at their make; their first decode takes it A video cache's planes took their fill at their make: a clear of each on the transfer queue, which the first decode op into each new surface then waited for on its thread before the hardware could write. A macOS guest's walk of 1080p H.264 and HEVC and 4K 10-bit HEVC measured that wait at every new 4K 10-bit surface's first op on a Radeon 780M over VA-API, 14 of 14, 1.9 ms at the median and 2.6 ms at most (26.1 ms in all), and on 12 of 14 on an RTX 3090 over NVDEC (0.23 ms, 0.32 ms at most); 4 of 21 and 7 of 28 of the 1080p surfaces' first ops waited, up to 0.9 ms. The planes now owe their fill as any new shape does. A decode that takes a new cache's first fill as met takes the planes' fills as met too, on every host (on Windows it did already), so its begin fills nothing and its op waits for no fill. The guest reads the same zeros wherever no picture lands: - a picture short of the planes has the host's video zero the rest (the commit before this one); - a picture that fails to land in planes whose fill its decode took has them cleared whole by the session, on its queue, before its value is signalled (SurfaceTarget::zeros); - a decode op that fails, at any step after its command buffer's begin, makes the cache's fill from its pages, as one that answers no picture does (Caches::decode_failed); - planes that a video command buffer landed no picture in owe a fill from the buffer, which a use makes by clearing them where the buffer holds no memory, from the surface's own clear passes, rather than making the buffer's memory to copy its zeros: SurfaceCaches::fill_shapes now clears wherever the fill at the planes' make did. What changes for the guest: a decode's first picture into a new surface starts without that wait. A picture that fails to land in a new surface now leaves its planes all zeros, where the hardware's partial write over the make's zeros showed before. The counts line names the plane fills a decode took ("taken as met by a decode over zeros"); its "filled at their make" and "owing theirs past their make" counts are gone. Tests: the tests that held the fill at the make now hold the planes owing it and the decode taking it, with no wait (surface_shapes); new tests hold the clears of planes no picture landed in, and a failed landing's clears (video_host). --- .../src/graphics/decode/surface_cache.rs | 68 +++--- .../graphics/decode/surface_cache/exported.rs | 160 ++++++------- .../graphics/decode/surface_cache/shapes.rs | 85 +++---- .../src/graphics/decode/surface_shapes.rs | 215 +++++++++++------- .../reims-vgpu/src/graphics/decode/video.rs | 72 ++++-- crates/reims-vgpu/src/graphics/video_host.rs | 28 ++- .../src/graphics/video_host/tests.rs | 83 +++++++ docs/video/devices.md | 4 + 8 files changed, 465 insertions(+), 250 deletions(-) diff --git a/crates/reims-vgpu/src/graphics/decode/surface_cache.rs b/crates/reims-vgpu/src/graphics/decode/surface_cache.rs index 7d93c60e4..6b4e04ad7 100644 --- a/crates/reims-vgpu/src/graphics/decode/surface_cache.rs +++ b/crates/reims-vgpu/src/graphics/decode/surface_cache.rs @@ -44,7 +44,9 @@ //! have left the pages' bytes: Metal leaves a failed decode's output undefined, and a new //! surface's texels are the kernel's zero-filled pages unless its client wrote them first, //! which a read-write lock does and the use's newer generation then fills (below); no other -//! memory's bytes reach the guest, the cache made as zeros. A command +//! memory's bytes reach the guest, the cache made as zeros, and its surface's planes, whose +//! fills the decode takes as met too, zeroed where its picture does not write them, by the +//! host's video or by the fill of a decode that answers none (`exported`). A command //! buffer names each cached surface it uses (`RGFX_OP_SURFACE_USE`), with the CPU's generation //! of the surface when it was committed. Where the cache holds an older generation, or none, //! the cache is filled from the pages before the buffer's work; a buffer of the same generation @@ -4367,9 +4369,9 @@ impl SurfaceCaches

{ /// generation `generation`: where the fill owed is of that generation or a newer one, and no /// device of the host wrote the pages since the make. The cache holds that generation, its /// zeros until the picture lands ([`SurfaceCache::zeros`]), and copies into the pages only - /// the planes' rows where the kext gave no layout ([`SurfaceCache::texels`]). On Windows, - /// where its surface's planes lie over Direct3D 11 textures, its plane shapes' fills are - /// taken as met too (`exported::take_drawn_fill`). + /// the planes' rows where the kext gave no layout ([`SurfaceCache::texels`]). Where the cache + /// has its surface, its plane shapes' fills are taken as met too, as the picture lands over + /// them (`exported::take_plane_fills`). fn take_decoded(&mut self, block: u64, generation: u64, planes: &[PagesPlane; 2]) { let host = self.watched.writes(block); let Some(cache) = self.by_block.get_mut(&block) else { @@ -4385,8 +4387,7 @@ impl SurfaceCaches

{ cache.first_fill = None; cache.owner = None; cache.zeros = true; - #[cfg(windows)] - exported::take_drawn_fill(&cache.surface, &mut cache.shapes, &mut self.counts.shapes); + exported::take_plane_fills(&cache.surface, &mut cache.shapes, &mut self.counts.shapes); if cache.texels.is_none() { cache.texels = Some(texel_spans(planes)); cache.planes = planes.len(); @@ -5212,24 +5213,14 @@ impl SurfaceCaches

{ /// the buffer first, so the new ones take its writes (rule 2; the module doc of `shapes`); /// signalled as [`Self::fill`] signals, so the uses counted after it wait for it on the GPU /// ([`Self::wait_last_fill`]). The shapes the cache's zeros fill take none first - /// ([`Self::take_zeros`]). The transfer event's value at which it is done; `None` where it - /// made none, as where nothing was left to fill. + /// ([`Self::take_zeros`]). A fill that would copy the cache's buffer's zeros + /// ([`SurfaceCache::fills_zeros`]) into plane shapes alone, each taking a clear + /// ([`shapes::clears_fit`]) and its surface holding that clear (`exported`'s + /// `CacheSurface::clear_of`), clears their images instead, with the buffer's memory left + /// unmade (the module doc of `shapes`, "Zeros"; [`shapes::fill_clears`]); a fill that owes + /// another shape copies through the buffer, its memory made. The transfer event's value at + /// which it is done; `None` where it made none, as where nothing was left to fill. fn fill_shapes(&mut self, device: &P::Device, block: u64) -> RaiseResult> { - self.fill_shapes_over(device, block, None) - } - - /// [`Self::fill_shapes`], given `clears`, the platform to make render passes with, where the - /// fill may clear: a fill that would copy the cache's buffer's zeros - /// ([`SurfaceCache::fills_zeros`]) into shapes that each take a clear - /// ([`shapes::clears_fit`]) clears their images instead, with the buffer's memory left unmade - /// (the module doc of `shapes`, "Zeros"; [`shapes::fill_clears`]). A fill that owes another - /// shape copies through the buffer, its memory made, as one not given `clears` does. - pub(super) fn fill_shapes_over( - &mut self, - device: &P::Device, - block: u64, - clears: Option<&P>, - ) -> RaiseResult> { self.touch_shapes(block); if self.take_zeros(block) { return Ok(None); @@ -5250,12 +5241,15 @@ impl SurfaceCaches

{ && cache.fills_straight(self.spares.piece) && !cache.shapes.iter().any(|s| s.dirty && !s.owed); // A buffer that holds no memory and would fill its shapes with its zeros clears them - // instead, where `clears` is given and each takes a clear ("Zeros"). - let clearing = clears.filter(|_| { - !straight && cache.fills_zeros() && shapes::clears_fit::

(&cache.shapes, |s| s.owed) - }); + // instead, where each is a plane of the surface's that takes a clear ("Zeros"). + let clearing = !straight + && cache.fills_zeros() + && shapes::clears_fit::

(&cache.shapes, |s| s.owed) + && (cache.shapes.iter()) + .filter(|s| s.owed) + .all(|s| cache.surface.clear_of(&s.key).is_some()); let Some(cb) = (transfer.queue.command_buffer(ON)?).filter(|_| { - if straight || clearing.is_some() { + if straight || clearing { return true; } let made = cache.copy_through(MemoryFor::FillShapes); @@ -5273,9 +5267,10 @@ impl SurfaceCaches

{ reserve.extend(self.left_over.remove(&block).unwrap_or_default()); let owed: &dyn Fn(&shapes::Shape) -> bool = &|s| s.owed; let mut filled = || -> RaiseResult> { - if let Some(platform) = clearing { - let counts = &mut self.counts.shapes; - shapes::fill_clears(platform, &cb, &mut cache.shapes, owed, counts)?; + if clearing { + let (surface, counts) = (&cache.surface, &mut self.counts.shapes); + let pass_of = |s: &shapes::Shape| Ok(surface.clear_of(&s.key)); + shapes::fill_clears::

(pass_of, &cb, &mut cache.shapes, owed, counts)?; return Ok(Vec::new()); } let staging = (timed, &self.spares, &mut reserve); @@ -8456,6 +8451,17 @@ impl Caches

{ state.decoded(device, block, picture) } + /// A decode into the block at `block`'s surface failed, answering no picture: where it took + /// the cache's first fill as met ([`SurfaceCache::zeros`]), the fill is made, on `device`, as + /// [`Self::decoded`] makes it for a decode that answers none; else nothing. + pub(crate) fn decode_failed(&self, device: Option<&P::Device>, block: u64) -> RaiseResult<()> { + let zeros = (self.lock().by_block.get(&block)).is_some_and(|cache| cache.zeros); + if !zeros { + return Ok(()); + } + self.decoded(device, block, false) + } + /// The deferred block at `block`'s pages taken copied ([`SurfaceCaches::attach_copied`]), /// which copies nothing, so waits for no command buffer. pub(crate) fn attach_copied( diff --git a/crates/reims-vgpu/src/graphics/decode/surface_cache/exported.rs b/crates/reims-vgpu/src/graphics/decode/surface_cache/exported.rs index a429ba784..5bd799c57 100644 --- a/crates/reims-vgpu/src/graphics/decode/surface_cache/exported.rs +++ b/crates/reims-vgpu/src/graphics/decode/surface_cache/exported.rs @@ -12,18 +12,30 @@ //! multiple of 64 luma rows, 32 chroma), for VA-API's decodes into it; the pitch and each plane's //! start are multiples of 256, as VA-API's imports and CUDA's copies take them. //! -//! The surface's memory holds undefined bytes at its make, so the planes take their fill from the -//! cache's buffer at once, on the transfer queue, as their texture is made -//! (`SurfaceCaches::fill_planes_at_make`), where no command buffer begun and not finished uses -//! the cache and no copy of it waits, as a placed image's fill is made (`shapes`, "Placed -//! images"): the thread that makes the texture encodes and commits the fill and waits for -//! nothing. Where the cache's buffer holds no memory yet (`super`'s "Deferred memory"), the -//! fill clears each plane to the zeros it would read, a render pass of its own with no source, -//! so the buffer's memory stays unmade (`shapes`, "Zeros"). A decode's begin then -//! owes the planes no fill. The host's video writes them outside the platform's queues, so the -//! decode op waits on its thread for the cache's last fill (`Caches::wait_filled`), which that -//! fill, made ahead of it, has as a rule done by then; every command buffer that uses the cache -//! after it waits for it on the GPU. A busy cache's planes owe their fill to its next use. +//! The surface's memory holds undefined bytes at its make, and the planes owe their fill from the +//! cache's buffer, as any new shape does. A decode that takes a new cache's first fill as met, +//! the buffer holding its make's zeros (`SurfaceCaches::take_decoded`), takes the plane shapes' +//! fills as met too ([`take_plane_fills`]): its picture lands over the planes whole, and the +//! host's video zeros what a picture short of them does not write (`video_host`'s module docs, +//! "A short picture"), so the planes read past the picture what the fill would have left them, +//! and the decode op, which waits on its thread for the cache's last fill as the host's video +//! writes the planes outside the platform's queues (`Caches::wait_filled`), finds none to wait +//! for. Where no picture lands, the zeros are made all the same: a decode that answers none, or +//! fails, makes the cache's fill, the planes' among it (`SurfaceCaches::decoded`), and a picture +//! that fails to land has the planes cleared by the session that failed it +//! (`SurfaceTarget::zeros`). Any other first use fills the planes as it fills any shape that +//! owes a fill, where the cache's buffer holds no memory yet (`super`'s "Deferred memory") by +//! clearing each plane to the zeros it would read, a render pass of its own with no source, so +//! the buffer's memory stays unmade (`shapes`, "Zeros"); every command buffer that uses the +//! cache after a fill waits for it on the GPU. +//! +//! **Measured** (2026-11-21, a macOS guest's video walk of 1080p H.264 and HEVC and 4K 10-bit +//! HEVC, m3-max-66323 on a Radeon 780M over VA-API and m3-max-66312 on an RTX 3090 over NVDEC), +//! while the planes took their fill at their make, a clear on the transfer queue that the first +//! decode op into each new surface waited for: each new 4K 10-bit surface's first op waited +//! for it, 14 of 14 on the 780M, 1.9 ms at the median and 2.6 ms at most (26.1 ms in all), and +//! 12 of 14 on the 3090, 0.23 ms and 0.32 ms; of the 1080p surfaces' first ops, 4 of 21 on the +//! 780M (0.78 ms at most) and 7 of 28 on the 3090 (0.89 ms). //! //! A decode whose luma and chroma textures show the planes whole is given the surface's planes //! (`Caches::surface_targets`), and its picture lands there, by the hardware or by a copy into @@ -64,7 +76,7 @@ //! The textures' bytes are undefined at their make, and the plane shapes owe a fill from the //! buffer, as any new shape does. A decode that takes a new cache's first fill as met, the //! buffer holding its make's zeros (`SurfaceCaches::take_decoded`), takes the plane shapes' -//! fills as met too (`take_drawn_fill`), and its command buffer waits for no fill: the +//! fills as met too ([`take_plane_fills`]), and its command buffer waits for no fill: the //! textures owe zeros instead (`graphics::drawn`), which the picture's draw writes wherever it //! does not write the picture, as that fill would have left them, so a picture smaller than its //! planes leaves the texels past it zero, as the module doc of `super` says of a picture that @@ -236,6 +248,24 @@ impl CacheSurface

{ self.made.is_some() } + /// The clear to zeros of the surface's plane that the plane shape keyed `key` is + /// ([`PlaneSurface::clears`]), for a fill of the plane by clears + /// (`SurfaceCaches::fill_shapes`); `None` where the cache has no surface, `key` is no plane of + /// it or the platform raised at its clears. + #[cfg(unix)] + pub(super) fn clear_of(&self, key: &ShapeKey) -> Option { + let made = self.made.as_ref()?; + let n = made.keys.iter().position(|k| k == key)?; + (made.clears.as_ref()).map(|clears| clears[n].clone_ref()) + } + + /// On Windows, `None`: the planes over Direct3D 11 textures take no clear here, their draw + /// clearing the textures (`graphics::drawn`). + #[cfg(windows)] + pub(super) fn clear_of(&self, _key: &ShapeKey) -> Option { + None + } + /// Whether the host's video writes the cache's memory itself /// ([`VideoMemory::written_outside`]); `false` for a cache not made for it. pub(super) fn written_outside(&self) -> bool { @@ -310,10 +340,13 @@ pub(super) struct PlaneSurface { life: MemoryLife, /// The rows each plane holds, its own and the room below them. rows: [u64; 2], + /// Each plane's shape's key, luma's then chroma's. + keys: [ShapeKey; 2], /// Each plane's clear to zeros, a render pass of its image alone /// ([`shapes::zeros_pass`](super::shapes::zeros_pass)), for the host's video to clear a - /// plane a short picture lands in (`video_host`'s `Landing::writes_only`); `None` where the - /// platform raised at one. + /// plane a short picture lands in or a picture failed to land in (`video_host`'s + /// `Landing::writes_only` and `land_pictures`), and for a fill of the planes by clears + /// (`SurfaceCaches::fill_shapes`); `None` where the platform raised at one. clears: Option<[P::RenderPassDescriptor; 2]>, } @@ -414,8 +447,9 @@ impl CacheSurface

{ } /// The targets of a decode into the surface's planes, where the cache has its surface: - /// counted as landed for the command buffer being walked. - fn targets(&mut self) -> Option<[SurfaceTarget

; 2]> { + /// counted as landed for the command buffer being walked. `zeros`: the decode took the + /// planes' fills as met ([`SurfaceTarget::zeros`]). + fn targets(&mut self, zeros: bool) -> Option<[SurfaceTarget

; 2]> { let made = self.made.as_ref()?; let layout = made.memory.layout(); let identity = made.life.identity(); @@ -429,6 +463,7 @@ impl CacheSurface

{ bytes_per_row: laid.row_pitch, rows: made.rows[n], clear: (made.clears.as_ref()).map(|clears| clears[n].clone_ref()), + zeros, }) }; let targets = [target(0)?, target(1)?]; @@ -479,24 +514,23 @@ pub(super) fn decodes_into(surface: &CacheSurface

, decoded: bool } /// The log's line of the video caches' planes and the fills a video write owed, from the shapes' -/// counts `sh`, and the plane shapes' fills taken as met where any were (`take_drawn_fill`); +/// counts `sh`, and the plane shapes' fills taken as met where any were ([`take_plane_fills`]); /// `None` where nothing of either was counted. pub(crate) fn planes_line(sh: &ShapeCounts) -> Option { let asked = sh.surfaces + sh.surfaces_refused + sh.surfaces_declined + sh.surfaces_unasked; let any = asked + sh.planes_landed + sh.video_fills > 0; any.then(|| { - let taken = match sh.drawn_fills_taken { + let taken = match sh.plane_fills_taken { 0 => String::new(), - n => format!("; {n} plane shapes' fills taken as met by a decode drawn over zeros"), + n => format!("; {n} plane shapes' fills taken as met by a decode over zeros"), }; format!( "IOSurface device caches' video planes: {} surfaces made ({} plane shapes), {} the \ platform made none of, {} it made in its copy form declined, {} declined unmade as \ it made their description so before, {} caches holding one at once at most; {} \ video command buffers landed in them; of the fills, {} owed by a video write ({} \ - MiB); plane shapes filled {} at their make, {} at a later use, {} surfaces' planes \ - owing theirs past their make as their cache was busy; {} video ops waited on the \ - CPU for a cache's fill not done, the longest {} us{taken}", + MiB); plane shapes filled {} times; {} video ops waited on the CPU for a cache's \ + fill not done, the longest {} us{taken}", sh.surfaces, sh.planes_made, sh.surfaces_refused, @@ -506,9 +540,7 @@ pub(crate) fn planes_line(sh: &ShapeCounts) -> Option { sh.planes_landed, sh.video_fills, sh.video_filled_bytes >> 20, - sh.planes_filled_at_make, - sh.plane_fills.saturating_sub(sh.planes_filled_at_make), - sh.planes_owed_busy, + sh.plane_fills, sh.video_fill_waits, sh.video_fill_wait_max_us ) @@ -517,22 +549,26 @@ pub(crate) fn planes_line(sh: &ShapeCounts) -> Option { /// A decode took the first fill of the cache whose surface is `surface` and shapes `shapes` as /// met, the buffer holding its make's zeros ([`super::SurfaceCaches::take_decoded`]): where the -/// surface's planes lie over Direct3D 11 textures, the fills its plane shapes owe are taken as -/// met too, and the textures owe zeros instead (module doc); counted in `counts`. -#[cfg(windows)] -pub(super) fn take_drawn_fill( +/// cache has its surface, the fills its plane shapes owe are taken as met too, as the picture +/// lands over them (module doc), and on Windows the Direct3D 11 textures under them owe zeros +/// instead; counted in `counts`. +pub(super) fn take_plane_fills( surface: &CacheSurface

, shapes: &mut ShapeTable, counts: &mut ShapeCounts, ) { - let Some(made) = &surface.made else { + if !surface.is_made() { return; - }; - let taken = shapes.take_plane_fills(); - if taken > 0 { - made.drawn.fill_taken(); - counts.drawn_fills_taken += taken; } + let taken = shapes.take_plane_fills(); + if taken == 0 { + return; + } + #[cfg(windows)] + if let Some(made) = &surface.made { + made.drawn.fill_taken(); + } + counts.plane_fills_taken += taken; } /// A fill of the plane shapes of the cache whose surface is `surface` from its buffer was made: @@ -597,6 +633,7 @@ impl SurfaceCaches

{ memory, life: MemoryLife::new(), rows, + keys: ask.keys, clears, }); let counts = &mut self.counts.shapes; @@ -616,36 +653,6 @@ impl SurfaceCaches

{ } } -#[cfg(unix)] -impl SurfaceCaches

{ - /// The planes just added to the block at `block`'s cache ([`Self::add_planes`]) take their - /// fill from its buffer at once (the module doc), with the cache's other shapes that owe one - /// and its dirty shapes written back first ([`Self::fill_shapes`]), or by clears of - /// `platform`'s while the buffer holds no memory ([`Self::fill_shapes_over`]), where no - /// command buffer begun and not finished uses the cache and no copy of it waits; else they - /// owe it to the cache's next use, counted ([`ShapeCounts::planes_owed_busy`]). A fill the - /// platform raises at is logged, and the planes owe theirs still. - fn fill_planes_at_make(&mut self, platform: &P, device: &P::Device, block: u64) { - if self.pending(block, false) > 0 || self.deferring(block) { - self.counts.shapes.planes_owed_busy += 1; - return; - } - let before = self.counts.shapes.plane_fills; - // A refusal of a buffer's memory is a begin's to fail its command buffer on, not this. - let refused = self.memory_refused; - if let Err(raise) = self.fill_shapes_over(device, block, Some(platform)) { - log::warn!( - target: "reims_vgpu::graphics", kind:% = raise; - "the IOSurface block at {block:#x}: the platform raised at its video planes' fill \ - at their make ({raise:?}); they owe it to the cache's next use" - ); - } - self.memory_refused = refused; - let filled = self.counts.shapes.plane_fills - before; - self.counts.shapes.planes_filled_at_make += filled; - } -} - /// The counts' code of a surface the platform made in its copy form that a cache declined /// ([`decline`]): in the process's counts line, which the boot reads at its end. #[cfg(unix)] @@ -746,9 +753,8 @@ impl Caches

{ /// its image. `None` where the cache takes no surface for `key`, or the platform made none /// (counted) or made it in its copy form, declined (counted, [`decline`]), or made its /// description so before, declined unmade (counted, [`unasked`]), for the caller to make the - /// shape's image of its own. Where the cache's buffer holds no memory yet, the planes' fill - /// at their make clears them by `platform`'s render passes - /// ([`SurfaceCaches::fill_planes_at_make`]), with the buffer's memory left unmade. + /// shape's image of its own. The planes owe their fill (module doc); each takes its clear + /// to zeros, a render pass of `platform`'s ([`PlaneSurface::clears`]). pub(super) fn plane_shape( &self, platform: &P, @@ -790,15 +796,7 @@ impl Caches

{ return None; } let clears = plane_clears(platform, block, &made.planes); - // The caches' lock, whose let-go commits the planes' fill (`Locked`). - let (found, gone) = { - let mut state = self.lock(); - let added = state.add_planes(block, key, &ask, made, clears); - if added.0.is_some() { - state.fill_planes_at_make(platform, device, block); - } - added - }; + let (found, gone) = self.lock().add_planes(block, key, &ask, made, clears); if let Err(refusal) = refusals::refusing(move || drop(gone)) { log::warn!( target: "reims_vgpu::graphics", kind:% = refusal; @@ -814,7 +812,9 @@ impl Caches

{ /// picture there ([`owe_video`]). `None` where it has none. pub(crate) fn surface_targets(&self, block: u64) -> Option<[SurfaceTarget

; 2]> { let mut state = self.state.lock(); - state.by_block.get_mut(&block)?.surface.targets() + let cache = state.by_block.get_mut(&block)?; + let zeros = cache.zeros; + cache.surface.targets(zeros) } } @@ -1416,7 +1416,7 @@ mod drawn_tests { ); let shapes = counts.shapes; assert_eq!( - (shapes.fills, shapes.fills_made, shapes.drawn_fills_taken), + (shapes.fills, shapes.fills_made, shapes.plane_fills_taken), (0, 0, 2) ); assert!(drawn.owes_zeros(), "the textures owe zeros"); diff --git a/crates/reims-vgpu/src/graphics/decode/surface_cache/shapes.rs b/crates/reims-vgpu/src/graphics/decode/surface_cache/shapes.rs index e69f1d0fb..40c821251 100644 --- a/crates/reims-vgpu/src/graphics/decode/surface_cache/shapes.rs +++ b/crates/reims-vgpu/src/graphics/decode/surface_cache/shapes.rs @@ -110,10 +110,11 @@ //! guest reads the same zeros either way. A placed image holds its memory's bytes as they were, //! so it is never taken so: its fill makes the buffer's memory and copies its zeros, as before; //! and so does a plane shape's, whose image is its surface's plane, which holds undefined bytes -//! at its make ("Plane shapes"), but where the fill may clear: the planes' fill at their make -//! (`super::exported`). Then each plane takes its zeros from a render pass that clears its image -//! to 0.0, its samples' bytes of zeros, with no source and the cache's buffer's memory left -//! unmade ([`fill_clears`], counted in [`ShapeCounts::zero_cleared`]). A fill that owes any other +//! at its make ("Plane shapes"), but where the fill may clear: a fill of the planes made while +//! the buffer holds no memory (`super::exported`). Then each plane takes its zeros from a render +//! pass that clears its image to 0.0, its samples' bytes of zeros, with no source and the +//! cache's buffer's memory left unmade ([`fill_clears`], counted in +//! [`ShapeCounts::zero_cleared`]). A fill that owes any other //! shape too, which may not take a clear ([`clears_fit`]), makes the buffer's memory and copies //! its zeros, as before. //! Where the cache would hold zeros so, a new shape is not placed: the platform's own image takes @@ -140,23 +141,20 @@ //! is a copy of its own, the plane shapes dirty, so the copies out and the fills of the others //! take the picture from them (rule 3). The bound never drops a plane shape, referenced or not, //! and rule 1 copies a write through another shape of the plane into it. The surface's memory -//! holds undefined bytes at its make, so the planes take their fill from the buffer then, at the -//! texture's make, as a placed image does, where no command buffer begun and not finished uses -//! the cache and no copy of it waits (`super::exported`, counted -//! [`ShapeCounts::planes_filled_at_make`]), by a clear to zeros where the buffer holds no -//! memory yet ("Zeros"); a decode's begin then owes none, and its wait for the cache's last fill -//! finds that fill made ahead of it. A busy cache's planes owe theirs to its -//! next use, as any new shape's ([`ShapeCounts::planes_owed_busy`]). A picture that lands over -//! part of the planes alone (a picture smaller than them, or one not written) leaves the rest -//! holding that fill. A CPU lock of the surface never reads the surface's memory beside the +//! holds undefined bytes at its make, and the planes owe their fill from the buffer, as any new +//! shape does: a decode that takes a new cache's first fill as met, the buffer holding its +//! make's zeros, takes their fills as met too ([`ShapeTable::take_plane_fills`], counted in +//! [`ShapeCounts::plane_fills_taken`]), as its picture lands over the planes whole, the host's +//! video zeroing what a picture short of them does not write (`super::exported`); any other use +//! fills them as it fills any shape, by a clear to zeros where the buffer holds no memory yet +//! ("Zeros"). A CPU lock of the surface never reads the surface's memory beside the //! planes' texels (each row's padding, and the rows of room below): a fill and a write-back move //! the shapes' texels alone, and a copy out of a cache of a surface the kext laid out moves its //! planes' texels alone (`super::SurfaceCache`'s `texels`), so those bytes of the pages stay as //! the guest's CPU wrote them, a new IOSurface's zeros. On Windows the planes lie over -//! Direct3D 11 textures that the pictures are drawn into, and take no fill at their make: a -//! decode that takes a new cache's first fill as met takes its plane shapes' fills as met too -//! (`ShapeTable::take_plane_fills`), as the buffer holds its make's zeros and the textures are -//! cleared to zeros wherever the picture's draw does not write them (`super::exported`). +//! Direct3D 11 textures that the pictures are drawn into, the textures cleared to zeros wherever +//! the picture's draw does not write them once a decode took the planes' fills as met +//! (`super::exported`). //! //! **References.** Each guest texture over a shape, and each guest view of one, holds a //! [`ShapeRef`] in its decoder entry; the count of those is the shape's reference count, so every @@ -491,9 +489,9 @@ pub(crate) struct ShapeCounts { /// command buffers' writes that landed in them. pub(crate) planes_made: u64, pub(crate) planes_landed: u64, - /// Plane shapes' fills taken as met by a decode drawn over a new cache's zeros - /// (`ShapeTable::take_plane_fills`, on Windows). - pub(crate) drawn_fills_taken: u64, + /// Plane shapes' fills taken as met by a decode over a new cache's zeros + /// ([`ShapeTable::take_plane_fills`]). + pub(crate) plane_fills_taken: u64, /// Surfaces made for video caches' planes, those the platform made none of, those it made in /// its copy form, declined, those declined unmade as it made their description so before, /// and the most caches that held one at once (`super::exported`). @@ -535,12 +533,8 @@ pub(crate) struct ShapeCounts { pub(crate) zero_cleared: u64, pub(crate) zero_cleared_bytes: u64, pub(crate) video_zeroed: u64, - /// Fills of plane shapes from their cache's buffer, and of them those made at the planes' - /// make (the module doc, "Plane shapes"); surfaces whose planes owed their fill past their - /// make, as a command buffer begun and not finished used the cache or a copy of it waited. + /// Fills of plane shapes from their cache's buffer (the module doc, "Plane shapes"). pub(crate) plane_fills: u64, - pub(crate) planes_filled_at_make: u64, - pub(crate) planes_owed_busy: u64, /// A video op's waits on the CPU for its surface's cache's last fill that found it not done /// (`super::Caches::wait_filled`), and the longest, in microseconds. pub(crate) video_fill_waits: u64, @@ -849,11 +843,10 @@ impl ShapeTable { } /// A decode that writes the planes whole took the cache's first fill as met, its buffer - /// holding its make's zeros, where the plane shapes' images lie over Direct3D 11 textures - /// that its picture is drawn into, cleared to zeros wherever it does not write them (the - /// module doc, "Plane shapes"): each plane shape that owes a fill owes none. How many owed - /// one. - #[cfg(any(windows, test))] + /// holding its make's zeros, and its picture lands over the plane shapes' images, which the + /// host's video, or on Windows the picture's draw, zeros wherever the picture does not write + /// them (the module doc, "Plane shapes"): each plane shape that owes a fill owes none. How + /// many owed one. pub(crate) fn take_plane_fills(&mut self) -> u64 { let mut taken = 0; for shape in self.shapes.iter_mut().filter(|s| s.plane && s.owed) { @@ -1003,7 +996,9 @@ pub(crate) fn fill( /// Metal's `MTLTextureUsageRenderTarget`, `MTLLoadActionClear` and `MTLStoreActionStore`: what /// a fill by clears asks of a shape's image and its pass ([`fill_clears`]). const USAGE_RENDER_TARGET: u64 = 0x4; +#[cfg_attr(windows, allow(dead_code))] const LOAD_ACTION_CLEAR: u64 = 2; +#[cfg_attr(windows, allow(dead_code))] const STORE_ACTION_STORE: u64 = 1; /// Whether `shape` takes a fill by a clear ([`fill_clears`]): a plane shape whose image declares @@ -1024,14 +1019,15 @@ pub(crate) fn clears_fit( } /// Encodes in `cb`, for each shape of `table` that `pick` picks, a render pass of its own whose -/// one colour attachment is the shape's image, loaded by a clear to 0.0 and stored, with no draw: -/// the zeros its cache's buffer, which holds no memory yet, reads (the module doc, "Zeros"), with -/// no copy and no source. Each shape then owes no fill and holds no write the buffer lacks, as -/// [`fill`] leaves it, counted in `counts` as a fill and as a clear. The caller made sure each -/// takes a clear ([`clears_fit`]): one that does not, or whose pass the platform made no encoder -/// for, is left owing its fill. +/// one colour attachment is the shape's image, loaded by a clear to 0.0 and stored, with no draw +/// (`pass_of`'s for the shape, a [`zeros_pass`]): the zeros its cache's buffer, which holds no +/// memory yet, reads (the module doc, "Zeros"), with no copy and no source. Each shape then owes +/// no fill and holds no write the buffer lacks, as [`fill`] leaves it, counted in `counts` as a +/// fill and as a clear. The caller made sure each takes a clear ([`clears_fit`]): one that does +/// not, or that `pass_of` gives no pass, or whose pass the platform made no encoder for, is left +/// owing its fill. pub(crate) fn fill_clears( - platform: &P, + pass_of: impl Fn(&Shape) -> RaiseResult>, cb: &P::CommandBuffer, table: &mut ShapeTable, pick: impl Fn(&Shape) -> bool, @@ -1039,7 +1035,9 @@ pub(crate) fn fill_clears( ) -> RaiseResult<()> { const ON: OnFault = OnFault::Return; for shape in (table.iter_mut()).filter(|s| pick(s) && takes_clear::

(s)) { - let pass = zeros_pass(platform, &shape.image)?; + let Some(pass) = pass_of(shape)? else { + continue; + }; let Some(encoder) = cb.render_encoder(ON, &pass)? else { continue; }; @@ -1055,6 +1053,9 @@ pub(crate) fn fill_clears( /// 0.0 and stored: with no draw, it writes the image's zeros, with no copy and no source /// ([`fill_clears`]; a video plane's clear before a short picture lands, `video_host`'s /// `Landing::writes_only`). `image` is a plane shape's image that takes a clear ([`takes_clear`]). +/// On Windows nothing makes one: a video plane's surface there lies over Direct3D 11 textures +/// (`graphics::drawn`). +#[cfg_attr(windows, allow(dead_code))] pub(crate) fn zeros_pass( platform: &P, image: &P::Texture, @@ -1697,7 +1698,7 @@ mod tests { /// 0.0 and stored, with no draw, no copy and no buffer, counted as a fill and as a clear. A /// storage shape, whose image declares the Unknown usage, and a plain shape take no clear and /// are left owing their fill, so a fill that owes either does not clear ([`clears_fit`]) but - /// copies through the cache's buffer (`SurfaceCaches::fill_shapes_over`) + /// copies through the cache's buffer (`SurfaceCaches::fill_shapes`) #[test] fn a_fill_by_clears_clears_the_plane_shapes_alone() { use reims_vgpu_vulkan::core::platform::Handle as _; @@ -1760,7 +1761,8 @@ mod tests { let cb = queue.command_buffer(ON).unwrap().unwrap(); let mark = platform.call_log().mark(); let mut counts = ShapeCounts::default(); - fill_clears(&platform, &cb, &mut table, |s| s.owed, &mut counts).unwrap(); + let pass_of = |s: &Shape<_>| zeros_pass(&platform, &s.image).map(Some); + fill_clears::(pass_of, &cb, &mut table, |s| s.owed, &mut counts).unwrap(); let calls = platform.call_log().since(mark); let sels: Vec<&str> = calls.iter().map(|c| c.sel).collect(); assert_eq!( @@ -1910,7 +1912,8 @@ mod tests { } let mut counts = ShapeCounts::default(); let cb = queue.command_buffer(ON).unwrap().unwrap(); - fill_clears(&*platform, &cb, &mut table, |s| s.owed, &mut counts).unwrap(); + let pass_of = |s: &Shape<_>| zeros_pass(&*platform, &s.image).map(Some); + fill_clears::(pass_of, &cb, &mut table, |s| s.owed, &mut counts).unwrap(); cb.commit(ON).unwrap(); cb.wait_until_completed(ON).unwrap(); assert_eq!(counts.zero_cleared, 4, "each cleared"); diff --git a/crates/reims-vgpu/src/graphics/decode/surface_shapes.rs b/crates/reims-vgpu/src/graphics/decode/surface_shapes.rs index 0fe3d4318..45ff95816 100644 --- a/crates/reims-vgpu/src/graphics/decode/surface_shapes.rs +++ b/crates/reims-vgpu/src/graphics/decode/surface_shapes.rs @@ -2448,17 +2448,19 @@ mod tests { .collect() } - /// A video cache's planes (`surface_cache::exported`) take their fill as the first texture - /// over either is made, which makes them: on the transfer queue, committed, with no wait on - /// the CPU, counted as filled at their make. The cache's buffer holds no memory, so the zeros - /// it would give them come from a render pass for each that clears its image to zeros and - /// stores it, with no copy and no buffer made, counted, the cache's buffer's memory left - /// unmade (`shapes`, "Zeros"). A texture over the other plane fills nothing, and the decode's - /// begin owes the planes no fill + /// A video cache's planes (`surface_cache::exported`) owe their fill at their make, as any new + /// shape does: the first texture over either, which makes them, encodes nothing, with no + /// pass, copy, buffer or commit. A decode that takes the new cache's first fill as met takes + /// the planes' fills as met too, counted, as its picture lands over them whole: its begin + /// fills nothing, its op's wait on its thread for the cache's last fill finds none, and its + /// planes' targets say the fills were taken (`SurfaceTarget::zeros`), for a picture that fails + /// to land; once its picture is answered, a later decode's targets say none were. The cache's + /// buffer's memory is never made #[cfg(unix)] #[test] - fn a_video_caches_planes_take_their_fill_at_their_make() { + fn a_decode_takes_a_new_video_caches_planes_fills_as_met() { use super::super::surface_cache::Reach; + use reims_vgpu_vulkan::core::platform::os::clock::MonoTime; use reims_vgpu_vulkan::core::testkit::impls::device::EXPORTED_SURFACE_PLANE; let mut r = video_shaped(); let cache = cache_id(&r); @@ -2472,48 +2474,30 @@ mod tests { .filter(|&&s| s == EXPORTED_SURFACE_PLANE) .count(); assert_eq!(surfaces, 2, "the surface's planes: {sels:?}"); - let cleared = passes(&calls, &kept); - assert_eq!(cleared.len(), 2, "both planes: {cleared:?}"); - assert_eq!(cleared[0], (luma, CLEARS), "luma's"); - let chroma = cleared[1].0; - assert!( - cleared[1].1 == CLEARS && chroma != luma, - "chroma's: {cleared:?}" - ); + assert!(passes(&calls, &kept).is_empty(), "no clear: {sels:?}"); assert!(fills(&calls).is_empty(), "no copy into them"); - assert_eq!( - zeros_into(&calls, cache), - 0, - "no zeros of the cache's buffer" - ); assert!( !sels.contains(&"newBufferWithLength:options:"), "no buffer made: {sels:?}" ); - let memory = (r.dec.caches().buffer(BLOCK)).is_some_and(|b| b.memory_made()); - assert!(!memory, "the cache's buffer holds no memory"); - assert!( - first(&calls, PASS) < first(&calls, "commit"), - "committed: {sels:?}" - ); - assert!(!sels.contains(&"waitUntilCompleted"), "no wait: {sels:?}"); + assert!(!sels.contains(&"commit"), "nothing committed: {sels:?}"); let counts = r.dec.caches().counts().shapes; - assert_eq!((counts.planes_filled_at_make, counts.plane_fills), (2, 2)); - assert_eq!(counts.zero_cleared, 2, "both cleared"); - assert_eq!(counts.planes_owed_busy, 0); + assert_eq!( + ( + counts.plane_fills, + counts.zero_cleared, + counts.plane_fills_taken + ), + (0, 0, 0) + ); assert_eq!( (counts.surfaces, counts.surfaces_declined), (1, 0), "a surface in place is kept" ); - assert!(r.dec.surface_caches.surface_targets(BLOCK).is_some()); - let mark = r.p.call_log().mark(); - assert_eq!( - plane_texture(&mut r, 22, 1), - chroma, - "the chroma plane's image" - ); + let chroma = plane_texture(&mut r, 22, 1); + assert_ne!(chroma, luma, "the chroma plane's image"); assert!(fills(&r.p.call_log().since(mark)).is_empty()); let queue = r.device.new_command_queue().unwrap(); @@ -2534,16 +2518,42 @@ mod tests { let calls = r.p.call_log().since(mark); assert!(fills(&calls).is_empty(), "none at the decode's begin"); assert!(copies(&calls).is_empty(), "the decode takes the first fill"); + assert!( + passes(&calls, &kept).is_empty(), + "no clear at the decode's begin" + ); + let counts = r.dec.caches().counts().shapes; + assert_eq!((counts.plane_fills_taken, counts.plane_fills), (2, 0)); + // The decode op, on its thread. + let mut short = || true; + let now = || MonoTime::from_nanos(0); + assert!( + r.dec.surface_caches.wait_filled(BLOCK, &mut short, &now), + "no fill to wait for" + ); + assert_eq!(r.dec.caches().counts().shapes.video_fill_waits, 0); + let targets = (r.dec.surface_caches.surface_targets(BLOCK)).expect("the planes"); + assert!(targets.iter().all(|t| t.zeros), "the fills taken"); + (r.dec.surface_caches) + .decoded(Some(&r.device), BLOCK, true) + .unwrap(); (r.dec.surface_caches) .finish(&r.device, 1, None, &cb, uses.unwrap(), || cb.commit(ON)) .unwrap(); r.p.world().complete_all(CompleteOn::Caller); - let counts = r.dec.caches().counts().shapes; + let targets = (r.dec.surface_caches.surface_targets(BLOCK)).expect("the planes"); + assert!(targets.iter().all(|t| !t.zeros), "a later decode's"); + let calls = r.p.call_log().since(0); assert_eq!( - (counts.planes_filled_at_make, counts.plane_fills), - (2, 2), - "none at a later use" + zeros_into(&calls, cache), + 0, + "no zeros of the cache's buffer" ); + let memory = (r.dec.caches().buffer(BLOCK)).is_some_and(|b| b.memory_made()); + assert!(!memory, "the cache's buffer holds no memory"); + let counts = r.dec.caches().counts(); + assert_eq!(counts.decode_firsts, 1, "the decode took the first fill"); + assert_eq!(counts.shapes.plane_fills, 0, "none at a later use"); } /// A video cache's surface the platform made in its copy form (`surface_cache::exported`), @@ -2591,7 +2601,7 @@ mod tests { ), (0, 1, 0) ); - assert_eq!((counts.planes_made, counts.planes_filled_at_make), (0, 0)); + assert_eq!(counts.planes_made, 0); assert!( r.dec.surface_caches.surface_targets(BLOCK).is_none(), "a decode lands in the cache's buffer" @@ -2658,23 +2668,22 @@ mod tests { ); } - /// A picture smaller than its planes leaves the rest of them holding zeros, as a new - /// IOSurface reads: the planes took zeros at their make, each from a render pass that clears - /// it, the cache's buffer holding no memory (`shapes`, "Zeros"), and nothing of the pages - /// since, as the decode takes the surface's first fill; the decode op's wait on its - /// thread for the cache's last fill lasts until that fill is done, so the hardware's picture - /// lands over it; and its command buffer leaves the planes owing nothing, so no use after it - /// fills them again. The cache's buffer's memory is never made. The fake holds no texels: the - /// order pins the bytes + /// A decode over a new video cache's planes waits for no fill of them: the decode takes the + /// surface's first fill as met, the planes' among it, so its op's wait on its thread for the + /// cache's last fill ends at once, and the hardware's picture lands over planes that took no + /// zeros of the platform's, no clear and nothing of the pages; the host's video zeros what a + /// picture smaller than them does not write (`video_host`'s module docs, "A short picture"). + /// Its command buffer leaves the planes owing nothing, so no use after it fills them. The + /// cache's buffer's memory is never made #[cfg(unix)] #[test] - fn a_picture_smaller_than_its_planes_leaves_the_rest_reading_zeros() { + fn a_decode_over_a_new_video_caches_planes_waits_for_no_fill() { use super::super::surface_cache::Reach; let mut r = video_shaped(); let cache = cache_id(&r); let kept = keep_passes(&r, 8); - let luma = plane_texture(&mut r, 21, 0); - let chroma = plane_texture(&mut r, 22, 1); + plane_texture(&mut r, 21, 0); + plane_texture(&mut r, 22, 1); let queue = r.device.new_command_queue().unwrap(); let cb = queue.command_buffer(ON).unwrap().unwrap(); let decode = Reach { @@ -2689,16 +2698,14 @@ mod tests { let (_, uses) = (r.dec.surface_caches) .before(&r.device, &cb, &writes, &decode) .unwrap(); - // The decode op, on its thread. + // The decode op, on its thread, with nothing completed. let mut short = || true; let now = || reims_vgpu_vulkan::core::platform::os::clock::MonoTime::from_nanos(0); assert!( - !r.dec.surface_caches.wait_filled(BLOCK, &mut short, &now), - "the planes' fill not done: the wait goes on" + r.dec.surface_caches.wait_filled(BLOCK, &mut short, &now), + "no fill of the planes: the wait ends at once" ); - assert_eq!(r.dec.caches().counts().shapes.video_fill_waits, 1); - r.p.world().complete_all(CompleteOn::Caller); - assert!(r.dec.surface_caches.wait_filled(BLOCK, &mut short, &now)); + assert_eq!(r.dec.caches().counts().shapes.video_fill_waits, 0); assert!( r.dec.surface_caches.surface_targets(BLOCK).is_some(), "the picture lands in the planes" @@ -2720,11 +2727,7 @@ mod tests { 0, "no zeros of the cache's buffer" ); - assert_eq!( - passes(&calls, &kept), - [(luma, CLEARS), (chroma, CLEARS)], - "each cleared" - ); + assert!(passes(&calls, &kept).is_empty(), "no clear"); let filled = filled_images(&calls); assert!(filled.is_empty(), "no copy into them: {filled:?}"); let memory = (r.dec.caches().buffer(BLOCK)).is_some_and(|b| b.memory_made()); @@ -2739,16 +2742,75 @@ mod tests { let counts = r.dec.caches().counts(); assert_eq!(counts.decode_firsts, 1, "the decode took the first fill"); assert_eq!( - (counts.shapes.planes_landed, counts.shapes.plane_fills), - (1, 2) + ( + counts.shapes.planes_landed, + counts.shapes.plane_fills, + counts.shapes.plane_fills_taken + ), + (1, 0, 2) ); } + /// A video command buffer that took a new video cache's planes' fills as met with the cache's + /// first and landed no picture in them (its walk faulted before its decode op asked for the + /// planes) leaves every shape owing a fill from the buffer, which holds its make's zeros and + /// no memory: the next use fills the planes by a render pass for each that clears its image + /// to zeros and stores it, the surface's own clears, with no copy and the buffer's memory left + /// unmade (`shapes`, "Zeros") + #[cfg(unix)] + #[test] + fn planes_no_picture_landed_in_take_their_zeros_from_clears() { + use super::super::surface_cache::Reach; + let mut r = video_shaped(); + let cache = cache_id(&r); + let kept = keep_passes(&r, 8); + let luma = plane_texture(&mut r, 21, 0); + let chroma = plane_texture(&mut r, 22, 1); + let queue = r.device.new_command_queue().unwrap(); + let cb = queue.command_buffer(ON).unwrap().unwrap(); + let decode = Reach { + video: true, + decoded: vec![(BLOCK, nv12_planes())], + ..Reach::default() + }; + let writes = [RgfxOpSurfaceUse { + flags: RGFX_SURFACE_USE_WRITES, + ..reading(BLOCK, 0)[0] + }]; + let (_, uses) = (r.dec.surface_caches) + .before(&r.device, &cb, &writes, &decode) + .unwrap(); + (r.dec.surface_caches) + .finish(&r.device, 1, None, &cb, uses.unwrap(), || cb.commit(ON)) + .unwrap(); + r.p.world().complete_all(CompleteOn::Caller); + assert_eq!(r.dec.caches().counts().shapes.plane_fills, 0); + let mark = r.p.call_log().mark(); + use_surfaces(&mut r, 1, 0x101, &[(BLOCK, 0, false)]); + r.p.world().complete_all(CompleteOn::Caller); + let calls = r.p.call_log().since(mark); + assert_eq!( + passes(&calls, &kept), + [(luma, CLEARS), (chroma, CLEARS)], + "each cleared" + ); + let filled = filled_images(&calls); + assert!(filled.is_empty(), "no copy into them: {filled:?}"); + assert_eq!( + zeros_into(&calls, cache), + 0, + "no zeros of the cache's buffer" + ); + let memory = (r.dec.caches().buffer(BLOCK)).is_some_and(|b| b.memory_made()); + assert!(!memory, "the cache's buffer holds no memory"); + let counts = r.dec.caches().counts().shapes; + assert_eq!((counts.plane_fills, counts.zero_cleared), (2, 2)); + } + /// A video cache's planes made while a command buffer begun and not finished uses the cache - /// owe their fill to its next use, counted, as a placed image's make finds the cache busy - /// (`shapes`, "Placed images"): that use fills them, counted as filled at a later use, from - /// the pages straight, as the cache's buffer holds no memory (the module doc of - /// `surface_cache`, "Straight fills") + /// owe their fill to its next use, as any planes do at their make: a use that is no decode + /// fills them from the pages straight, as the cache's buffer, which lacks them, holds no + /// memory (the module doc of `surface_cache`, "Straight fills") #[cfg(unix)] #[test] fn a_busy_video_caches_planes_owe_their_fill_to_its_next_use() { @@ -2766,11 +2828,7 @@ mod tests { fills(&r.p.call_log().since(mark)).is_empty(), "none while a command buffer uses the cache" ); - let counts = r.dec.caches().counts().shapes; - assert_eq!( - (counts.planes_filled_at_make, counts.planes_owed_busy), - (0, 1) - ); + assert_eq!(r.dec.caches().counts().shapes.plane_fills, 0); (r.dec.surface_caches) .finish(&r.device, 1, None, &reader, uses.unwrap(), || { reader.commit(ON) @@ -2790,7 +2848,7 @@ mod tests { let memory = (r.dec.caches().buffer(BLOCK)).is_some_and(|b| b.memory_made()); assert!(!memory, "the buffer's memory not made"); let counts = r.dec.caches().counts().shapes; - assert_eq!((counts.planes_filled_at_make, counts.plane_fills), (0, 2)); + assert_eq!((counts.plane_fills, counts.plane_fills_taken), (2, 0)); } /// A decode whose textures show a video cache's planes whole, where the cache has its @@ -2815,8 +2873,7 @@ mod tests { plane_texture_with(&mut r, 21, 10, 0, whole); plane_texture_with(&mut r, 22, 10, 1, whole); r.p.world().complete_all(CompleteOn::Caller); - // The clears' command buffer completes as it is committed, so the planes' wait for it - // ends. + // A fill's command buffer completes as it is committed, so a wait for it ends. r.p.world().set_auto_complete(Some(CompleteOn::Thread)); let mark = r.p.call_log().mark(); let (luma, chroma) = r.dec.video_planes(0, 21, 22, true).unwrap(); diff --git a/crates/reims-vgpu/src/graphics/decode/video.rs b/crates/reims-vgpu/src/graphics/decode/video.rs index 047b3f69f..8649f51aa 100644 --- a/crates/reims-vgpu/src/graphics/decode/video.rs +++ b/crates/reims-vgpu/src/graphics/decode/video.rs @@ -629,6 +629,11 @@ pub struct SurfaceTarget { /// clear and stored, for a picture shorter than the plane (`video_host`'s /// `Landing::writes_only`); `None` where the platform raised at its make. pub clear: Option, + /// The decode took the plane's fill as met (`surface_cache::exported`'s + /// `take_plane_fills`): its image holds undefined bytes until the picture lands, so a + /// picture that fails to land has it cleared to the zeros the fill would have left + /// (`video_host`'s `land_pictures`). + pub zeros: bool, } #[cfg(unix)] @@ -643,6 +648,7 @@ impl SurfaceTarget

{ bytes_per_row: self.bytes_per_row, rows: self.rows, clear: self.clear.as_ref().map(|clear| clear.clone_ref()), + zeros: self.zeros, } } } @@ -875,6 +881,53 @@ pub(super) fn decode_op( return Ok(false); }; let (m, rest) = read?; + let done = match decode_picture(tag, &m, rest, objs, session) { + Ok(done) => done, + Err(fault) => { + // A decode that fails answers no picture: a new output whose first fill the + // buffer's begin took as met, its planes' among it, takes the fill now, before + // anything reads the cache (`surface_cache`, "Fill"). + let device = cb.device(); + for block in objs.decode_blocks(m.luma_texture_obj_id, m.chroma_texture_obj_id) { + if let Err(raise) = (objs.surface_caches).decode_failed(device.as_ref(), block) { + log::warn!( + target: "reims_vgpu::graphics", kind:% = raise; + "video op {tag:#06x}: the fill of the IOSurface block at {block:#x}'s new \ + device cache after its decode failed: {raise}" + ); + } + } + return Err(fault); + } + }; + // The guest's decodes in flight, this one among them, as many as the spare surfaces of the + // caches over Direct3D 11 textures an ask keeps (`surface_cache`'s `exported`). + #[cfg(windows)] + if let EventRef::Shared(event) = session.event() { + let in_flight = done.value.saturating_sub(event.signaled_value()); + objs.surface_caches.saw_decodes_in_flight(in_flight); + } + // A decoder's new output whose first fill the buffer's begin took as met holds the cache's + // zeros: a picture lands over them, and none makes the fill, before anything reads the cache + // (`surface_cache`, "Fill"). + let device = done.skipped.then(|| cb.device()).flatten(); + for block in objs.decode_blocks(m.luma_texture_obj_id, m.chroma_texture_obj_id) { + (objs.surface_caches).decoded(device.as_ref(), block, !done.skipped)?; + } + cb.encode_wait(Some(session.event()), done.value)?; + landings.extend(done.landed); + Ok(true) +} + +/// The picture of decode op `tag`, `m` its body and `rest` the bytes after it, decoded on +/// `session` ([`decode_op`]), once the buffers it runs after on other queues are done. +fn decode_picture( + tag: u16, + m: &DecodeOp, + rest: &[u8], + objs: &GraphicsDecoder

, + session: &Arc>, +) -> RaiseResult { if m.codec != session.codec() { return Err(op_fault(format_args!( "video op {tag:#06x}: a decode of codec {} on a session of codec {}", @@ -916,24 +969,7 @@ pub(super) fn decode_op( // After the buffers of other queues its command buffer runs after: the reads of the // output it would land over among them. objs.wait_for_order_on_cpu(); - let done = session.decode(&decode)?; - // The guest's decodes in flight, this one among them, as many as the spare surfaces of the - // caches over Direct3D 11 textures an ask keeps (`surface_cache`'s `exported`). - #[cfg(windows)] - if let EventRef::Shared(event) = session.event() { - let in_flight = done.value.saturating_sub(event.signaled_value()); - objs.surface_caches.saw_decodes_in_flight(in_flight); - } - // A decoder's new output whose first fill the buffer's begin took as met holds the cache's - // zeros: a picture lands over them, and none makes the fill, before anything reads the cache - // (`surface_cache`, "Fill"). - let device = done.skipped.then(|| cb.device()).flatten(); - for block in objs.decode_blocks(m.luma_texture_obj_id, m.chroma_texture_obj_id) { - (objs.surface_caches).decoded(device.as_ref(), block, !done.skipped)?; - } - cb.encode_wait(Some(session.event()), done.value)?; - landings.extend(done.landed); - Ok(true) + session.decode(&decode) } /// Decodes one encode op, `tag` of `codec`, its body at the front of `payload` and its settings diff --git a/crates/reims-vgpu/src/graphics/video_host.rs b/crates/reims-vgpu/src/graphics/video_host.rs index 018e9581b..be2bf83fd 100644 --- a/crates/reims-vgpu/src/graphics/video_host.rs +++ b/crates/reims-vgpu/src/graphics/video_host.rs @@ -45,7 +45,10 @@ //! says first what it covers ([`Landing::writes_only`]), and the session zeros the rest of each //! plane in host bytes by the CPU, and clears a plane in a device cache whole on its queue, //! before the write. The rows past the crop that a plane holds for a coded picture -//! ([`OutputPlane::rows_held`]) are no texels of it, and keep what they held. +//! ([`OutputPlane::rows_held`]) are no texels of it, and keep what they held. A picture that +//! fails to land in a new output's planes, whose fill its decode took as met +//! (`SurfaceTarget::zeros`), has them cleared whole the same way, so they read the zeros the +//! fill would have left ([`land_pictures`]). //! //! A picture holds the hardware's surface until it has landed, so the session holds no more //! pictures unlanded than the decoder spares ([`HostDecoder::most_unlanded`]): a decode past @@ -1409,6 +1412,8 @@ fn land_pictures( } } Some(Err(error)) => { + #[cfg(unix)] + clear_unlanded(&output, &mut stage, name); // Before the signal: the command buffer waiting for it completes failed, or, // where it completed at the decode already, the next decode's does. carried = landed.fail(); @@ -1442,6 +1447,27 @@ fn land_pictures( } } +/// The planes of `output` whose fill the decode took as met (`SurfaceTarget::zeros`), as its +/// picture failed to land: cleared to zeros whole on the session's queue, as the fill would +/// have left them (the module docs, "A short picture"); a clear that fails is logged. +#[cfg(unix)] +fn clear_unlanded(output: &DecodeOutput

, stage: &mut Stage

, name: &str) { + let planes: Vec<&OutputPlane

> = [&output.luma, &output.chroma] + .into_iter() + .filter(|plane| (plane.surface.as_ref()).is_some_and(|target| target.zeros)) + .collect(); + if planes.is_empty() { + return; + } + if let Err(error) = stage.clear_planes(&planes) { + log::error!( + kind:% = error; + "hardware video: {name}: a new output's planes a picture did not land in were not \ + cleared: {error}" + ); + } +} + /// Lands `picture` in `output`, copying its staged planes into their device caches, and answers /// how, what the hardware answered where it failed the decode and the picture landed all the /// same ([`Landing::hardware_failed`]), and how long the CPU's copy took where the CPU copied it diff --git a/crates/reims-vgpu/src/graphics/video_host/tests.rs b/crates/reims-vgpu/src/graphics/video_host/tests.rs index dc05e41de..3e6996806 100644 --- a/crates/reims-vgpu/src/graphics/video_host/tests.rs +++ b/crates/reims-vgpu/src/graphics/video_host/tests.rs @@ -1164,6 +1164,7 @@ fn a_short_picture_in_a_cache_s_surface_has_its_images_cleared_first() { bytes_per_row: laid[n].row_pitch, rows: descs[n].height, clear: Some(clears[n].clone_ref()), + zeros: false, }; let mut stage = Stage { device: p.device(), @@ -1192,6 +1193,88 @@ fn a_short_picture_in_a_cache_s_surface_has_its_images_cleared_first() { assert!(p.call_log().calls_to(FILL).is_empty()); } +/// A picture that fails to land in its planes in their cache's surface, where its decode took +/// their fills as met (`SurfaceTarget::zeros`), has each plane's image cleared by its clear pass +/// on the session's queue before its value is signalled, as the fill would have left them; where +/// the decode took none, its planes are left as they were. +#[cfg(unix)] +#[test] +fn a_picture_that_fails_to_land_in_planes_owed_zeros_has_them_cleared() { + use reims_vgpu_vulkan::core::platform::resource::{ + SurfaceDesc, SurfaceHandle, SurfacePlaneDesc, + }; + let p = FakePlatform::new(); + p.world().set_auto_complete(Some(CompleteOn::Caller)); + p.world().give_exported_surfaces(true); + let plane = |pixel_format, width, height| SurfacePlaneDesc { + pixel_format, + width, + height, + usage: 0x15, + rows_below: 0, + }; + let descs = [plane(10, 96, 64), plane(30, 48, 32)]; + let desc = SurfaceDesc { + handle: SurfaceHandle::DmaBuf, + pitch_multiple: 128, + start_multiple: 256, + planes: &descs, + }; + let made = (p.device().new_exported_surface(OnFault::Return, &desc)) + .unwrap() + .unwrap(); + let laid = made.memory.layout().to_vec(); + let life = MemoryLife::new(); + let clears = [p.render_pass_descriptor(), p.render_pass_descriptor()]; + let target = |n: usize, zeros: bool| SurfaceTarget { + memory: Arc::clone(&made.memory), + identity: life.identity(), + texture: made.planes[n].clone_ref(), + offset: laid[n].offset, + bytes_per_row: laid[n].row_pitch, + rows: descs[n].height, + clear: Some(clears[n].clone_ref()), + zeros, + }; + let log = Log::default(); + let answers = vec![Ok(Some(Picture::Fail)), Ok(Some(Picture::Fail))]; + let s = session(&p, answers, &log); + let bitstream = buffer(&p, 4096, STORAGE_SHARED); + let cache = buffer(&p, 16384, PRIVATE); + for (value, zeros) in [(1, true), (2, false)] { + let (luma, chroma) = planes(&cache, &cache, 96); + let (luma, chroma) = ( + VideoPlane { + surface: Some(target(0, zeros)), + ..luma + }, + VideoPlane { + surface: Some(target(1, zeros)), + ..chroma + }, + ); + let mark = p.call_log().mark(); + let failed = s.decode(&decode(&bitstream, &[], (luma, chroma))).unwrap(); + assert!(event_of(&s).wait_until_signaled_value(value, 10_000)); + assert!(failed.landed.as_deref().is_some_and(Landed::failed)); + let calls = p.call_log().since(mark); + let passes: Vec<_> = (calls.iter()) + .filter(|c| c.sel == RENDER) + .map(|c| c.args.clone()) + .collect(); + let want = match zeros { + true => vec![vec![Arg::obj(&clears[0])], vec![Arg::obj(&clears[1])]], + false => Vec::new(), + }; + assert_eq!(passes, want, "zeros {zeros}"); + assert!( + !calls.iter().any(|c| c.sel == FILL), + "the cache's buffer takes no fill" + ); + } + assert_eq!(s.counts.lock().failed, 2); +} + /// A 4:2:0 picture's extent: the luma's samples and rows, the chroma's pairs and rows, each half /// as many rounded up. #[test] diff --git a/docs/video/devices.md b/docs/video/devices.md index e634d7489..f615d0648 100644 --- a/docs/video/devices.md +++ b/docs/video/devices.md @@ -155,6 +155,10 @@ command buffer of the session's process (`graphics/decode/video.rs`): plane's earlier bytes, another picture's or a staging buffer's. A VA-API decode straight into the output's cache takes only a coded picture that covers the planes; a shorter one is decoded elsewhere and lands as above (reims-vgpu's `graphics/video_host.rs`, "A short picture"). + A new output in a device cache takes no fill before its first decode, which so waits for + none: its planes read zeros wherever no picture lands in them, the session clearing them + where a picture fails to land, and the device filling the cache from its pages where the + decode fails or answers no picture. - **The command buffer's later work, and its completion, wait** for the session's event to reach the value the decode signals once its picture has landed. So a CPU lock of the output waits for the picture, as for any GPU write.