🐎 Ask no surface of a description made in the copy form

A video cache declines a surface the platform made in its copy form, but each
new cache still asked for one: on the RX 550, whose driver holds no dma-buf
surface in place, a walk made and let go of 128 to 137 such surfaces
(m3-max-63574, m3-max-63824), each an exported block of a picture's planes,
about 25 MB at 3840 by 2160 10-bit, on the guest's ring-0 thread.

The platform decides the form from the device's answers for the surface's
description and the images it makes of it, so the caches keep the
descriptions it made in the copy form (the latest 16), and a cache whose
surface would be made of one asks for none: declined unmade, counted
(surfaces_unasked, video-surface-copy-form-unasked), its shapes made as a
declined cache's are.

Nothing the guest observes changes: such a cache's textures read the same
texels from the same fills as a declined one's. A test pins that a second
cache of the same description asks for no surface.
This commit is contained in:
Anees Iqbal committed 2026-11-21 09:44:38 -08:00
1 parent fd1316d5ac
commit 1a1c040be8
4 files changed
+194 -19

No files matched your search

@@ -2064,6 +2064,10 @@ pub(crate) struct SurfaceCaches<P: Platform> {
/// Whether laid-out caches of two planes are made with their memory a dma-buf, for the host's
/// video to decode into ([`Caches::set_dma_buf`]).
dma_buf: bool,
/// The descriptions of the video caches' surfaces the platform made in its copy form, which
/// no later cache asks for (`exported`).
#[cfg(unix)]
copy_forms: exported::CopyForms,
/// The draws that write the host's video's pictures into Direct3D 11 textures, where laid-out
/// caches of two planes take their planes over such textures ([`Caches::set_drawn`]).
#[cfg(windows)]
@@ -2321,6 +2325,8 @@ impl<P: Platform> SurfaceCaches<P> {
answering: HashSet::new(),
copy_gpu: Arc::default(),
dma_buf: false,
#[cfg(unix)]
copy_forms: exported::CopyForms::default(),
#[cfg(windows)]
drawn: None,
recoveries: 0,
@@ -44,6 +44,15 @@
//! of 12.44 MB each, a 3840 by 2160 10-bit picture's two planes' sizes; with it (m3-max-63574),
//! the walk declined 137 surfaces and rt made no such copy.
//!
//! The platform decides a surface's form from the device's answers for its description and the
//! images it makes of it, so the description of each surface declined is kept
//! ([`CopyForms`]), and a later cache whose surface would be made of the same one asks for none:
//! it is declined unmade, counted (`surfaces_unasked`, `video-surface-copy-form-unasked`), and
//! its shapes are made as a declined cache's are. Each new video cache otherwise made a surface
//! for the platform to let go of at once: 128 to 137 a walk on the RX 550 (m3-max-63574,
//! m3-max-63824), each an exported block of a picture's planes, about 25 MB at 3840 by 2160
//! 10-bit.
//!
//! On Windows the surface's planes lie over two Direct3D 11 textures that the host's video draws
//! its pictures into (`graphics::drawn`), made at the ask and imported by the platform
//! (`GpuDevice::import_d3d11_surface`), each plane the size of its plane shape with no rows of
@@ -303,8 +312,50 @@ pub(super) struct SurfaceAsk {
keys: [ShapeKey; 2],
}
/// What a surface's description is made of ([`SurfaceAsk::desc`]): its handle's kind, which
/// decides its pitch's and starts' multiples, and each plane's description.
#[cfg(unix)]
type SurfaceShape = (SurfaceHandle, [SurfacePlaneDesc; 2]);
/// The most descriptions [`CopyForms`] keeps: more than the picture sizes a few players decode
/// at once.
#[cfg(unix)]
const COPY_FORMS_KEPT: usize = 16;
/// The descriptions of the surfaces the platform made in its copy form (the module doc), the
/// latest [`COPY_FORMS_KEPT`]: a cache whose surface would be made of one of them asks for none
/// ([`Caches::plane_shape`]).
#[cfg(unix)]
#[derive(Default)]
pub(super) struct CopyForms(std::collections::VecDeque<SurfaceShape>);
#[cfg(unix)]
impl CopyForms {
/// Whether the platform made a surface of `ask`'s description in its copy form.
fn holds(&self, ask: &SurfaceAsk) -> bool {
self.0.contains(&ask.shape())
}
/// The platform made a surface of `ask`'s description in its copy form: kept, the oldest
/// let go of past [`COPY_FORMS_KEPT`].
fn note(&mut self, ask: &SurfaceAsk) {
if self.holds(ask) {
return;
}
if self.0.len() >= COPY_FORMS_KEPT {
self.0.pop_front();
}
self.0.push_back(ask.shape());
}
}
#[cfg(unix)]
impl SurfaceAsk {
/// What the description [`Self::desc`] is made of.
fn shape(&self) -> SurfaceShape {
(self.handle, self.planes)
}
/// The description the platform makes the surface of: a dma-buf's pitch on
/// [`PLANE_ALIGNMENT`], an exportable surface's on its texels alone.
fn desc(&self) -> SurfaceDesc<'_> {
@@ -416,7 +467,7 @@ pub(super) fn decodes_into<P: Platform>(surface: &CacheSurface<P>, decoded: bool
/// counts `sh`, and the plane shapes' fills taken as met where any were (`take_drawn_fill`);
/// `None` where nothing of either was counted.
pub(crate) fn planes_line(sh: &ShapeCounts) -> Option<String> {
let asked = sh.surfaces + sh.surfaces_refused + sh.surfaces_declined;
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 {
@@ -425,15 +476,17 @@ pub(crate) fn planes_line(sh: &ShapeCounts) -> Option<String> {
};
format!(
"IOSurface device caches' video planes: {} surfaces made ({} plane shapes), {} the \
platform made none of, {} it made in its copy form declined, {} 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}",
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}",
sh.surfaces,
sh.planes_made,
sh.surfaces_refused,
sh.surfaces_declined,
sh.surfaces_unasked,
sh.surfaces_live_most,
sh.planes_landed,
sh.video_fills,
@@ -609,6 +662,32 @@ fn decline<P: Platform>(block: u64, made: ExportedSurface<P>) {
}
}
/// The counts' code of a cache whose surface was declined unmade, the platform having made its
/// description in the copy form before ([`CopyForms`]).
#[cfg(unix)]
const SURFACE_UNASKED: &str = "video-surface-copy-form-unasked";
/// Whether the process has logged a surface declined unmade ([`unasked`]).
#[cfg(unix)]
static UNASKED_SAID: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);
/// The block at `block`'s cache asks for no surface, the platform having made its description in
/// the copy form before ([`CopyForms`]): counted ([`SURFACE_UNASKED`]), the process's first
/// logged.
#[cfg(unix)]
fn unasked(block: u64) {
reims_vgpu_vulkan::answer::count_kind(SURFACE_UNASKED);
if !UNASKED_SAID.swap(true, std::sync::atomic::Ordering::Relaxed) {
log::info!(
target: "reims_vgpu::graphics", kind:% = SURFACE_UNASKED;
"the IOSurface block at {block:#x}: the platform made its video planes' surface's \
description in its copy form before; none asked, the cache's shapes are made as \
any other's and the host's video writes its buffer ({SURFACE_UNASKED}; the rest are \
counted)"
);
}
}
/// What [`SurfaceCaches::add_planes`] lets go of: images, and a surface's memory not taken.
#[cfg(unix)]
type Gone<P> = (
@@ -622,15 +701,26 @@ impl<P: Platform> Caches<P> {
/// (module doc): the surface made of both planes with the lock let go of, its planes added as
/// the cache's plane shapes ([`SurfaceCaches::add_planes`]), and a reference to `key`'s and
/// 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`]), for the caller to
/// make the shape's image of its own.
/// (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.
pub(super) fn plane_shape(
&self,
device: &P::Device,
block: u64,
key: &ShapeKey,
) -> Option<Held<P::Texture>> {
let ask = self.state.lock().surface_ask(block, key)?;
let ask = {
let mut state = self.state.lock();
let ask = state.surface_ask(block, key)?;
if state.copy_forms.holds(&ask) {
state.counts.shapes.surfaces_unasked += 1;
drop(state);
unasked(block);
return None;
}
ask
};
let made = match device.new_exported_surface(ON, &ask.desc()) {
Ok(made) => made.filter(|m| m.planes.len() == 2),
Err(raise) => {
@@ -648,7 +738,9 @@ impl<P: Platform> Caches<P> {
};
if !made.memory.in_place() {
decline(block, made);
self.state.lock().counts.shapes.surfaces_declined += 1;
let mut state = self.state.lock();
state.counts.shapes.surfaces_declined += 1;
state.copy_forms.note(&ask);
return None;
}
// The caches' lock, whose let-go commits the planes' fill (`Locked`).
@@ -432,10 +432,12 @@ pub(crate) struct ShapeCounts {
/// (`ShapeTable::take_plane_fills`, on Windows).
pub(crate) drawn_fills_taken: u64,
/// Surfaces made for video caches' planes, those the platform made none of, those it made in
/// its copy form, declined, and the most caches that held one at once (`super::exported`).
/// 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`).
pub(crate) surfaces: u64,
pub(crate) surfaces_refused: u64,
pub(crate) surfaces_declined: u64,
pub(crate) surfaces_unasked: u64,
pub(crate) surfaces_live_most: u64,
/// Shapes made over a plane that held a shape of the other kind, plain or storage (the module
/// doc, "A storage shape serves every texture of its extent"); and of rule 1's copies, those
@@ -1696,11 +1696,10 @@ mod tests {
video_shaped_made().0
}
/// [`video_shaped`], and the images its decoder makes ([`take_shapes`]).
/// [`nv12_planes`] as a `SURFACE_PAGES` lays them out.
#[cfg(unix)]
fn video_shaped_made() -> (Rig, Made) {
fn nv12_layout() -> reims_vgpu_graphics::wire::RgfxSurfacePagesLayout {
use reims_vgpu_graphics::wire::{RgfxSurfacePagesLayout, RgfxSurfacePlaneLayout};
use reims_vgpu_vulkan::core::platform::values::PagesInPlace;
let mut layout = RgfxSurfacePagesLayout {
plane_count: 2,
..Default::default()
@@ -1713,6 +1712,14 @@ mod tests {
rows: plane.rows,
};
}
layout
}
/// [`video_shaped`], and the images its decoder makes ([`take_shapes`]).
#[cfg(unix)]
fn video_shaped_made() -> (Rig, Made) {
use reims_vgpu_vulkan::core::platform::values::PagesInPlace;
let layout = nv12_layout();
let mut r = rig(16);
let page = r.page;
let made = take_shapes(&r);
@@ -1735,12 +1742,35 @@ mod tests {
(r, made)
}
/// Texture `obj_id` over buffer 10 that shows plane `plane` of [`nv12_planes`] whole, as a
/// decode's luma (`R8Unorm`) or chroma (`RG8Unorm`) texture: the image of its shape. The
/// rig's device describes every pixel format with the plane's texel bytes from then on,
/// where the fake's own answer is four.
/// Another cache made for the host's video on [`video_shaped`]'s rig, of the same layout in
/// pages 5 to 8, at `block`: buffer `buffer` is the cache.
#[cfg(unix)]
fn other_video_cache(r: &mut Rig, block: u64, buffer: u64) {
let page = r.page;
assert_eq!(
r.pages_asking(block, &[(5, 4)], 4 * page, Some(nv12_layout())),
RGFX_STATUS_CACHE
);
assert_eq!(
r.pages_cached(block, &[(5, 4)], 4 * page, true),
RGFX_STATUS_OK
);
assert_eq!(r.buffer(buffer, block, 4 * page), Ok(()));
}
/// Texture `obj_id` over buffer 10 that shows plane `plane` of [`nv12_planes`] whole
/// ([`plane_texture_over`]).
#[cfg(unix)]
fn plane_texture(r: &mut Rig, obj_id: u64, plane: u32) -> u64 {
plane_texture_over(r, obj_id, 10, plane)
}
/// Texture `obj_id` over buffer `buffer` that shows plane `plane` of [`nv12_planes`] whole,
/// as a decode's luma (`R8Unorm`) or chroma (`RG8Unorm`) texture: the image of its shape.
/// The rig's device describes every pixel format with the plane's texel bytes from then on,
/// where the fake's own answer is four.
#[cfg(unix)]
fn plane_texture_over(r: &mut Rig, obj_id: u64, buffer: u64, plane: u32) -> u64 {
use reims_vgpu_graphics::wire::RgfxTextureFromIOSurface;
use reims_vgpu_vulkan::core::platform::types::PixelFormatInfo;
use zerocopy::{FromBytes as _, IntoBytes as _};
@@ -1759,7 +1789,7 @@ mod tests {
let width = laid.row_bytes / texel;
let format = [10, 30][plane as usize];
let size = (width as u32, laid.rows as u32);
let bytes = surface_texture_as(obj_id, 10, format, size, (1, 0));
let bytes = surface_texture_as(obj_id, buffer, format, size, (1, 0));
let mut m = RgfxTextureFromIOSurface::read_from_bytes(&bytes).unwrap();
m.offset = laid.offset;
m.bytes_per_row = laid.bytes_per_row;
@@ -1919,6 +1949,51 @@ mod tests {
assert_eq!(r.dec.caches().counts().shapes.surfaces_declined, 1);
}
/// A video cache whose surface would be made of a description the platform made in its copy
/// form before (`surface_cache::exported`) asks for none: declined unmade, counted, its
/// textures' shapes made as a declined cache's are, of the platform's own images, and a
/// decode into its planes given none
#[cfg(unix)]
#[test]
fn a_video_cache_asks_no_surface_of_a_description_made_in_the_copy_form() {
use reims_vgpu_vulkan::core::testkit::impls::device::EXPORTED_SURFACE_PLANE;
const OTHER: u64 = BLOCK + 0x4000;
let (mut r, made) = video_shaped_made();
r.p.world().copy_exported_surfaces(true);
let surface_planes = |calls: &[Call]| {
(calls.iter())
.filter(|c| c.sel == EXPORTED_SURFACE_PLANE)
.count()
};
let mark = r.p.call_log().mark();
plane_texture(&mut r, 21, 0);
let calls = r.p.call_log().since(mark);
assert_eq!(surface_planes(&calls), 2, "the first cache's surface was made");
other_video_cache(&mut r, OTHER, 11);
let mark = r.p.call_log().mark();
let luma = plane_texture_over(&mut r, 23, 11, 0);
let chroma = plane_texture_over(&mut r, 24, 11, 1);
assert_eq!(surface_planes(&r.p.call_log().since(mark)), 0, "none asked");
assert_ne!(chroma, luma);
assert_eq!(made.lock().len(), 3, "the shapes' own images");
let counts = r.dec.caches().counts().shapes;
assert_eq!(
(
counts.surfaces,
counts.surfaces_declined,
counts.surfaces_unasked,
counts.surfaces_refused
),
(0, 1, 1, 0),
"asked once a cache"
);
assert!(
r.dec.surface_caches.surface_targets(OTHER).is_none(),
"a decode lands in the cache's buffer"
);
}
/// A picture smaller than its planes leaves the rest of them holding the cache's zeros, as a
/// new IOSurface reads: the cache's buffer took its zeros at its make, and nothing of the
/// pages since, as the decode takes the surface's first fill; the planes took the buffer at