🐎 Decline a video cache's surface made in the copy form

A video cache's planes take a surface of the platform's at the first texture
over a plane. Where rt makes that surface in its copy form, each plane is
rt's copy over a buffer in an exported block, and rt copies both planes in for
every command buffer that samples them, written or not. On the RX 550, whose
driver holds no dma-buf surface in place, a walk's desktop made 17,917 such
copies of 107.16 GB (m3-max-63386), 2 a present of 12.44 MB each.

Keep a made surface only where rt says its memory is in place. Otherwise let
it go at once, count it (surfaces_declined, video-surface-copy-form-declined),
and make the cache's shapes as any other cache's, the path every host takes
where the platform makes no surface: the host's video writes the cache's
buffer, and the shapes take one fill a picture written.

Nothing the guest or host observes changes but counts and cost: the guest's
textures read the same texels from the same fills, and no object id, index
or answer moves. The testkit's exported surfaces can now answer the copy
form (World::copy_exported_surfaces), and a test pins the decline.
This commit is contained in:
Anees Iqbal committed 2026-11-21 08:20:37 -08:00
1 parent 63056e4299
commit 738b781a85
5 files changed
+165 -18

No files matched your search

@@ -58,10 +58,14 @@ pub const EXPORTABLE_BUFFER: &str = "newBufferWithLength:options: (exportable)";
pub const EXPORTED_SURFACE_PLANE: &str = "newExportedSurface (a plane)";
/// What the fake's exported surface lies in ([`EXPORTED_SURFACE_PLANE`]): memory that exports
/// nothing, its planes in place, laid at the description's pitch and starts.
/// nothing, its planes in place unless its world makes the copy form
/// ([`World::copy_exported_surfaces`]), laid at the description's pitch and starts.
///
/// [`World::copy_exported_surfaces`]: crate::core::testkit::objects::World::copy_exported_surfaces
#[cfg(unix)]
struct FakeSurfaceMemory {
handle: crate::core::platform::resource::SurfaceHandle,
in_place: bool,
layout: Vec<crate::core::platform::resource::SurfacePlaneLayout>,
}
@@ -69,8 +73,8 @@ struct FakeSurfaceMemory {
impl FakeSurfaceMemory {
/// The memory of `desc`'s planes: one pitch, the widest plane's row rounded up to its
/// multiple, the first plane at 0 and each after it on its start multiple past the one
/// before and its rows of room.
fn of(desc: &crate::core::platform::resource::SurfaceDesc<'_>) -> Self {
/// before and its rows of room; in place as `in_place` says.
fn of(desc: &crate::core::platform::resource::SurfaceDesc<'_>, in_place: bool) -> Self {
use crate::core::platform::resource::SurfacePlaneLayout;
// `MTLPixelFormatR8Unorm`, `RG8Unorm`, `R16Unorm` and `RG16Unorm`'s texel bytes; 4 for
// any other.
@@ -96,6 +100,7 @@ impl FakeSurfaceMemory {
.collect();
FakeSurfaceMemory {
handle: desc.handle,
in_place,
layout,
}
}
@@ -116,7 +121,7 @@ impl crate::core::platform::resource::SurfaceMemory for FakeSurfaceMemory {
}
fn in_place(&self) -> bool {
true
self.in_place
}
fn layout(&self) -> &[crate::core::platform::resource::SurfacePlaneLayout] {
@@ -377,7 +382,8 @@ impl GpuDevice<FakePlatform> for Fk<kind::Device> {
/// `None`, unlogged, as the platform's default, unless the world makes such surfaces
/// ([`World::give_exported_surfaces`]): then each plane a Private texture of the plane's
/// format, size and usage, logged and scripted under [`EXPORTED_SURFACE_PLANE`], in memory
/// that exports nothing ([`FakeSurfaceMemory`]); `None` where a plane's make answers none.
/// that exports nothing, in place unless the world makes the copy form
/// ([`FakeSurfaceMemory`]); `None` where a plane's make answers none.
///
/// [`World::give_exported_surfaces`]: crate::core::testkit::objects::World::give_exported_surfaces
#[cfg(unix)]
@@ -411,7 +417,8 @@ impl GpuDevice<FakePlatform> for Fk<kind::Device> {
};
planes.push(made);
}
let memory = Arc::new(FakeSurfaceMemory::of(desc));
let in_place = !self.world().copies_exported_surfaces();
let memory = Arc::new(FakeSurfaceMemory::of(desc, in_place));
Ok(Some(crate::core::platform::resource::ExportedSurface {
planes,
memory,
@@ -995,6 +995,9 @@ pub struct World {
/// Whether the devices' `GpuDevice::new_exported_surface` makes a surface
/// ([`World::give_exported_surfaces`]).
exported_surfaces: AtomicBool,
/// Whether the surfaces the devices make answer their planes not in place
/// ([`World::copy_exported_surfaces`]).
exported_surfaces_copied: AtomicBool,
/// What the devices' [`GpuDevice::recoveries`] answers ([`World::recover`]).
///
/// [`GpuDevice::recoveries`]: crate::core::platform::device::GpuDevice::recoveries
@@ -1127,6 +1130,7 @@ impl World {
splits: AtomicBool::new(false),
guest_ram_copies: AtomicBool::new(false),
exported_surfaces: AtomicBool::new(false),
exported_surfaces_copied: AtomicBool::new(false),
recoveries: AtomicU64::new(0),
linear_rows: AtomicU64::new(0),
surface_pages: Mutex::new((PagesInPlace::Yes, Vec::new())),
@@ -1356,6 +1360,20 @@ impl World {
self.exported_surfaces.load(Ordering::SeqCst)
}
/// Has the exported surfaces the devices make ([`World::give_exported_surfaces`]) answer
/// their planes not in place (`SurfaceMemory::in_place`), as a platform that makes a surface
/// in its copy form, each plane its copy that it moves the bytes into and back; in place at
/// first.
pub fn copy_exported_surfaces(&self, on: bool) {
self.exported_surfaces_copied.store(on, Ordering::SeqCst);
}
/// Whether the devices' exported surfaces answer their planes not in place
/// ([`World::copy_exported_surfaces`]).
pub fn copies_exported_surfaces(&self) -> bool {
self.exported_surfaces_copied.load(Ordering::SeqCst)
}
/// Has the devices answer one recovery more ([`GpuDevice::recoveries`]), as a device whose
/// lost GPU the platform made again behind it does. Nothing else changes: the fake objects
/// made before it go on, their contents kept, so a test sees what the host does at the count
@@ -32,7 +32,16 @@
//! nothing back first, as it writes the planes whole.
//!
//! A platform that makes no such surface (the default, and one that exports no such memory)
//! answers `None`, counted, and the cache's shapes are made as any other's.
//! answers `None`, counted, and the cache's shapes are made as any other's. A surface it makes in
//! its copy form (`SurfaceMemory::in_place` false), each plane its copy that it moves the bytes
//! into and back, is declined: let go of at once, counted (`surfaces_declined`,
//! `video-surface-copy-form-declined`), and the cache's shapes are made as any other's, so the
//! host's video writes the cache's buffer and the shapes take a fill a picture written. Kept,
//! such a surface's planes are copied in for every command buffer that samples them, written or
//! not. **Measured** (2026-11-21, m3-max-63386, an RX 550, whose driver holds no dma-buf surface
//! in place, before the decline): a macOS guest's walk made 17,917 such copies of
//! 107.16 GB, 2 a present on a scrolling desktop, of 12.44 MB each, a 3840 by 2160 10-bit
//! picture's two planes' sizes.
//!
//! 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
@@ -406,7 +415,8 @@ 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 any = sh.surfaces + sh.surfaces_refused + sh.planes_landed + sh.video_fills > 0;
let asked = sh.surfaces + sh.surfaces_refused + sh.surfaces_declined;
let any = asked + sh.planes_landed + sh.video_fills > 0;
any.then(|| {
let taken = match sh.drawn_fills_taken {
0 => String::new(),
@@ -414,14 +424,15 @@ pub(crate) fn planes_line(sh: &ShapeCounts) -> Option<String> {
};
format!(
"IOSurface device caches' video planes: {} surfaces made ({} plane shapes), {} the \
platform made none of, {} 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, {} 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_live_most,
sh.planes_landed,
sh.video_fills,
@@ -564,6 +575,39 @@ impl<P: Platform> SurfaceCaches<P> {
}
}
/// 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)]
const SURFACE_DECLINED: &str = "video-surface-copy-form-declined";
/// Whether the process has logged a declined surface ([`decline`]).
#[cfg(unix)]
static DECLINED_SAID: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);
/// Lets go of `made`, a surface the platform made for the block at `block`'s cache in its copy
/// form (the module doc), with no lock held: counted ([`SURFACE_DECLINED`]), the process's first
/// logged, and a refusal of a release logged.
#[cfg(unix)]
fn decline<P: Platform>(block: u64, made: ExportedSurface<P>) {
reims_vgpu_vulkan::answer::count_kind(SURFACE_DECLINED);
if !DECLINED_SAID.swap(true, std::sync::atomic::Ordering::Relaxed) {
log::info!(
target: "reims_vgpu::graphics", kind:% = SURFACE_DECLINED;
"the IOSurface block at {block:#x}: the platform made its video planes' surface in its \
copy form, each plane its copy that it moves the bytes into and back; let go of, \
the cache's shapes are made as any other's and the host's video writes its buffer \
({SURFACE_DECLINED}; the rest are counted)"
);
}
if let Err(refusal) = refusals::refusing(move || drop(made)) {
log::warn!(
target: "reims_vgpu::graphics", kind:% = refusal;
"the IOSurface block at {block:#x}'s video planes' surface in the copy form was let \
go of, and a release was refused: {refusal}"
);
}
}
/// What [`SurfaceCaches::add_planes`] lets go of: images, and a surface's memory not taken.
#[cfg(unix)]
type Gone<P> = (
@@ -577,7 +621,8 @@ 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), for the caller to make the shape's image of its own.
/// (counted) or made it in its copy form, declined (counted, [`decline`]), for the caller to
/// make the shape's image of its own.
pub(super) fn plane_shape(
&self,
device: &P::Device,
@@ -600,6 +645,11 @@ impl<P: Platform> Caches<P> {
self.state.lock().counts.shapes.surfaces_refused += 1;
return None;
};
if !made.memory.in_place() {
decline(block, made);
self.state.lock().counts.shapes.surfaces_declined += 1;
return None;
}
// The caches' lock, whose let-go commits the planes' fill (`Locked`).
let (found, gone) = {
let mut state = self.lock();
@@ -431,10 +431,11 @@ pub(crate) struct ShapeCounts {
/// 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,
/// Surfaces made for video caches' planes, those the platform made none of, and the most
/// caches that held one at once (`super::exported`).
/// 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`).
pub(crate) surfaces: u64,
pub(crate) surfaces_refused: u64,
pub(crate) surfaces_declined: 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
@@ -1693,6 +1693,12 @@ mod tests {
/// 10 is the cache.
#[cfg(unix)]
fn video_shaped() -> Rig {
video_shaped_made().0
}
/// [`video_shaped`], and the images its decoder makes ([`take_shapes`]).
#[cfg(unix)]
fn video_shaped_made() -> (Rig, Made) {
use reims_vgpu_graphics::wire::{RgfxSurfacePagesLayout, RgfxSurfacePlaneLayout};
use reims_vgpu_vulkan::core::platform::values::PagesInPlace;
let mut layout = RgfxSurfacePagesLayout {
@@ -1709,7 +1715,7 @@ mod tests {
}
let mut r = rig(16);
let page = r.page;
take_shapes(&r);
let made = take_shapes(&r);
r.dec.set_unified_memory(true);
r.dec.set_dma_buf_caches();
let world = Arc::clone(r.p.world());
@@ -1726,7 +1732,7 @@ mod tests {
);
queues(&mut r);
assert_eq!(r.buffer(10, BLOCK, 4 * page), Ok(()));
r
(r, made)
}
/// Texture `obj_id` over buffer 10 that shows plane `plane` of [`nv12_planes`] whole, as a
@@ -1805,6 +1811,12 @@ mod tests {
let counts = r.dec.caches().counts().shapes;
assert_eq!((counts.planes_filled_at_make, counts.plane_fills), (2, 2));
assert_eq!(counts.planes_owed_busy, 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!(
@@ -1844,6 +1856,65 @@ mod tests {
);
}
/// A video cache's surface the platform made in its copy form (`surface_cache::exported`),
/// each plane its copy that it moves the bytes into and back, is declined as the first
/// texture over a plane is made: let go of, counted, and that texture's shape made as any
/// other cache's, of the platform's own image. The cache asks for no surface again, and a
/// decode into its planes is given none, so the host's video writes the cache's buffer
#[cfg(unix)]
#[test]
fn a_video_caches_surface_in_the_copy_form_is_declined() {
use reims_vgpu_vulkan::core::testkit::impls::device::EXPORTED_SURFACE_PLANE;
let (mut r, made) = video_shaped_made();
let world = Arc::clone(r.p.world());
world.copy_exported_surfaces(true);
let surface_planes = |calls: &[Call]| {
(calls.iter())
.filter(|c| c.sel == EXPORTED_SURFACE_PLANE)
.count()
};
let live = || {
let live = world.live("MTLTexture").into_iter();
live.map(|t| t.0).collect::<Vec<u64>>()
};
let before = live();
let mark = r.p.call_log().mark();
let luma = plane_texture(&mut r, 21, 0);
let calls = r.p.call_log().since(mark);
assert_eq!(surface_planes(&calls), 2, "the surface was made");
assert_eq!(made.lock().len(), 1, "luma's own image");
let new: Vec<u64> = (live().into_iter())
.filter(|t| !before.contains(t))
.collect();
assert_eq!(
new.len(),
2,
"the shape's image and the texture's view, the planes let go of: {new:?}"
);
assert!(new.contains(&luma), "{new:?}");
let counts = r.dec.caches().counts().shapes;
assert_eq!(
(counts.surfaces, counts.surfaces_declined, counts.surfaces_refused),
(0, 1, 0)
);
assert_eq!((counts.planes_made, counts.planes_filled_at_make), (0, 0));
assert!(
r.dec.surface_caches.surface_targets(BLOCK).is_none(),
"a decode lands in the cache's buffer"
);
let mark = r.p.call_log().mark();
let chroma = plane_texture(&mut r, 22, 1);
assert_eq!(
surface_planes(&r.p.call_log().since(mark)),
0,
"asked once a cache"
);
assert_ne!(chroma, luma);
assert_eq!(made.lock().len(), 2, "chroma's own image");
assert_eq!(r.dec.caches().counts().shapes.surfaces_declined, 1);
}
/// 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