🐎 Lay a device cache out at the GPU's pitch

A device cache held its surface's bytes in the pages' layout, so a texture
over a plane whose rows lie at another pitch than the GPU's own linear images
lay them was the runtime's shadow: an image of its own that each command
buffer copies in before its use. CoreAnimation names its backing stores' rows
at multiples of 64 bytes, and RADV lays its images' at 256: on an RX 7900 XTX,
552 of the textures over a macOS 26.7.2 guest's caches in Apple's sample app
were such shadows (RGBA16Float, BGRA8Unorm, RGBA8Unorm and R32Uint).

Where the kext gives the surface's layout and a plane's rows lie off the
device's pitch (GpuDevice::linear_rows_pitch), the cache now lays each plane's
rows out at that pitch: the first plane from the cache's first byte, each next
on a 4 KiB boundary past the last's rows. A texture over a plane lies at the
same byte of the same row of the plane in the cache, at the cache's pitch, a
two-plane texture's chroma alike, so it is the device's image over the
cache's memory. Every copy between the cache and the pages moves each row's
texels between its place in the pages and its place in the cache: by the
pages' runs in one copy, each part at its own byte of the cache; through the
view or a copied cache's staging, a copy for each row. A host video op writes
its picture at the cache's place of its texture. Every plane format is laid
out alike, from the layout's row bytes and rows.

The guest sees nothing change: each texture reads and writes the same texels,
and the bytes between the pages' rows and outside the planes stay the guest's
CPU's, as a copy out already left them. A fill no longer copies those bytes
into the cache, where no texture's texel lies.
This commit is contained in:
Anees Iqbal committed 2026-11-12 10:30:41 -08:00
1 parent d066f8a1e2
commit aad69b90cd
6 files changed
+987 -100

No files matched your search

+79 -6
View File
@@ -1329,7 +1329,7 @@ impl<P: Platform> GraphicsDecoder<P> {
cache waited for no copy of it, {} for one; {} made exportable for the \
host's video, {} not; {} made a dma-buf for the host's video to decode \
into, {} not; {} made with their surface's first plane on 256 bytes of \
their memory",
their memory; {} laid out at the GPU's pitch",
d.made, d.fills, d.filled_bytes >> 20, d.made_fills, d.decode_firsts,
d.downloads,
d.downloaded_bytes >> 20, d.clean, d.host_writes, d.unread,
@@ -1340,7 +1340,8 @@ impl<P: Platform> GraphicsDecoder<P> {
d.by_runs, d.runs_refused,
self.shared.slow_answers.load(Ordering::Relaxed), SLOW_ANSWER.as_millis(),
d.fills_deferred, d.cpu_waits, d.doorbell_free, d.doorbell_waits,
d.exportable, d.unexportable, d.dma_buf, d.no_dma_buf, d.aligned
d.exportable, d.unexportable, d.dma_buf, d.no_dma_buf, d.aligned,
d.repitched
);
}
}
@@ -3513,6 +3514,20 @@ impl<P: Platform> GraphicsDecoder<P> {
_ => return Ok(Err(DecodeError::Malformed)),
}
};
let info = device.pixel_format_info(u64::from(m.desc.pixel_format));
let row_bytes = video::row_bytes(&info, m.desc.width, m.bytes_per_row);
// Over a device cache laid out at the GPU's pitch the texture lies at the same
// byte of the same row of its plane in the cache, its rows at the cache's pitch
// (`surface_cache::Repitched`).
let laid_out = self.entry(m.buffer_obj_id).and_then(|e| e.laid_out.clone());
let plane = match &laid_out {
Some(laid) => {
let rows = video::texel_rows(&info, m.desc.height);
laid_plane(laid, plane, row_bytes, rows, &m)
}
None => plane,
};
let at = (plane.offset, plane.bytes_per_row);
let made = {
let Some(ObjHandle::Buffer(buffer)) = self.named(m.buffer_obj_id) else {
return Ok(Err(DecodeError::BadReference));
@@ -3541,12 +3556,12 @@ impl<P: Platform> GraphicsDecoder<P> {
// An id made again names only its new texture's surface.
self.forget_surface_texture(m.obj_id);
let mut entry = Entry::of(ObjHandle::Texture(texture)).over(span);
let info = device.pixel_format_info(u64::from(m.desc.pixel_format));
entry.place = Some(video::PlanePlace {
buffer_obj_id: m.buffer_obj_id,
offset: m.offset,
bytes_per_row: m.bytes_per_row,
row_bytes: video::row_bytes(&info, m.desc.width, m.bytes_per_row),
offset: at.0,
bytes_per_row: at.1,
in_surface: (m.offset, m.bytes_per_row),
row_bytes,
width: u64::from(m.desc.width),
height: u64::from(m.desc.height),
whole: video::whole_plane(
@@ -5723,6 +5738,64 @@ fn surface_plane(
})
}
/// The plane `plane` of the texture `m` makes over a buffer of a device cache laid out at the
/// GPU's pitch, `laid`: its first byte, and its chroma plane's where its facts name a second
/// plane, at the same byte of the same row of the same plane of the cache, its rows at the
/// cache's pitch ([`surface_cache::Repitched::place`]). A texture whose texels, `row_bytes` by
/// `rows` from its first, pass its plane's, made over its plane all the same, reads the cache's
/// bytes past them, not the guest's beside the plane; one whose first byte lies in no plane of
/// the cache keeps the surface's place, over bytes of the cache that are not its own. Each says
/// so. The bundle places each texture over a plane of the surface where the layout the cache's
/// comes from places the plane, at the plane's pitch (`RgfxIOSurface.mm`), so its first byte lies
/// in the plane's texels, and checks that one made at an origin inside the plane fits it; one at
/// the plane's first byte it does not check.
fn laid_plane(
laid: &surface_cache::Repitched,
plane: SurfacePlane,
row_bytes: u64,
rows: u64,
m: &RgfxTextureFromIOSurface,
) -> SurfacePlane {
let chroma = (plane.facts)
.filter(|f| f.plane_count >= 2)
.map(|f| laid.place(f.chroma_offset, f.chroma_bytes_per_row));
let (Some(placed), None | Some(Some(_))) =
(laid.place(plane.offset, plane.bytes_per_row), chroma)
else {
log::warn!(
target: "reims_vgpu::graphics",
"the texture {:#x} over plane {} of the IOSurface {} at {} with rows {} apart lies in \
no plane of its device cache laid out at the GPU's pitch: made at the surface's place, \
over bytes of the cache that are not its texels",
m.obj_id, m.plane, m.iosurface_id, m.offset, m.bytes_per_row
);
return plane;
};
if row_bytes > placed.room.0 || rows > placed.room.1 {
log::warn!(
target: "reims_vgpu::graphics",
"the texture {:#x} over plane {} of the IOSurface {} ({} bytes by {} rows from {}) \
passes its plane: over its device cache laid out at the GPU's pitch it reads the \
cache's bytes past the plane's texels",
m.obj_id, m.plane, m.iosurface_id, row_bytes, rows, m.offset
);
}
let facts = plane.facts.map(|f| match chroma {
Some(Some(c)) => SurfaceFacts {
chroma_offset: c.offset,
chroma_bytes_per_row: c.bytes_per_row,
..f
},
_ => f,
});
SurfacePlane {
offset: placed.offset,
bytes_per_row: placed.bytes_per_row,
facts,
..plane
}
}
/// An OSType as its four characters, quoted, where each is printable ASCII; else its value in hex.
fn four_characters(code: u32) -> String {
let bytes = code.to_be_bytes();
File diff suppressed because it is too large. Load diff
@@ -1605,31 +1605,34 @@ impl<P: Platform> super::GraphicsDecoder<P> {
// A block with a device cache: the buffer is the cache, over the surface's bytes in the
// pages' layout (`surface_cache`); a block named by its runs has one, or took its view. A
// deferred block whose pages are not named has its cache, and neither.
// The buffer keeps the cache's layout, by which the textures over it are placed.
let cache = {
let caches = self.surface_caches.lock();
match names {
true => caches.buffer(m.block_offset),
false => caches.deferred_buffer(&m),
}
.map(|c| c.clone_ref())
.map(|c| (c.clone_ref(), caches.repitched(m.block_offset)))
};
let buffer = match (cache, view) {
(Some(cache), _) => {
let (buffer, laid_out) = match (cache, view) {
(Some((cache, laid_out)), _) => {
// The first buffer over a new cache names the context that owns its first fill;
// another context's makes it (`SurfaceCaches::buffer_made`).
(self.surface_caches).buffer_made(device, m.block_offset, self.context)?;
cache
(cache, laid_out)
}
(None, Some(view)) => {
match buffer_over(device, &view, 0, m.length, m.options.into(), None)? {
Some(buffer) => buffer,
Some(buffer) => (buffer, None),
None => return Ok(Err(super::DecodeError::Refused)),
}
}
(None, None) => return Ok(Err(super::DecodeError::Refused)),
};
let span = Some((m.block_offset, m.block_size));
let entry = super::tables::Entry::of(super::ObjHandle::Buffer(buffer)).over(span);
let entry = (super::tables::Entry::of(super::ObjHandle::Buffer(buffer)))
.over(span)
.laid_out(laid_out);
self.store(m.obj_id, entry.over_view());
Ok(Ok(()))
}
@@ -45,6 +45,7 @@ use reims_vgpu_vulkan::core::sync::Mutex;
use super::lane::Lane;
use super::managed::ManagedCopy;
use super::surface_cache::Repitched;
use super::video::PlanePlace;
use super::{Drain, ObjHandle, drop_refused};
@@ -143,6 +144,10 @@ pub(crate) struct Entry<P: Platform> {
/// An indirect command buffer's commands whose draws and dispatches are dropped
/// ([`super::icb::Dropped`]); `None` for any other object.
pub(crate) icb_dropped: Option<Box<super::icb::Dropped>>,
/// A buffer over a block's device cache that lays its surface's planes out at the GPU's
/// pitch: that layout, by which each texture over the buffer is placed in it
/// ([`Repitched`]); `None` for any other object.
pub(crate) laid_out: Option<Arc<Repitched>>,
/// The table's clock at its make.
made: u64,
/// The first clock of a view that names it: its make's, or the making message's view's where
@@ -163,6 +168,7 @@ impl<P: Platform> Entry<P> {
bytes: None,
over_view: false,
icb_dropped: None,
laid_out: None,
made: 0,
seen: 0,
}
@@ -180,6 +186,13 @@ impl<P: Platform> Entry<P> {
self
}
/// The entry, a buffer over a device cache laid out as `laid_out` says (the field
/// `laid_out`).
pub(crate) fn laid_out(mut self, laid_out: Option<Arc<Repitched>>) -> Self {
self.laid_out = laid_out;
self
}
/// The entry, a buffer whose contents are the guest's `bytes` (the field `bytes`).
pub(crate) fn with_bytes(mut self, bytes: Option<GuestBytes>) -> Self {
self.bytes = bytes;
+16 -2
View File
@@ -232,6 +232,10 @@ pub(crate) struct PlanePlace {
pub(crate) offset: u64,
/// Bytes from one of its rows to the next.
pub(crate) bytes_per_row: u64,
/// The texture's first byte in its surface and its rows' pitch there, as its message gave
/// them: `offset` and `bytes_per_row`, but over a device cache laid out at the GPU's pitch
/// ([`super::surface_cache::Repitched`]).
pub(crate) in_surface: (u64, u64),
/// The bytes of one of its rows ([`row_bytes`]): its width in its format's texels, or in its
/// blocks for a format of blocks, at most `bytes_per_row`, which it is where the format's size
/// is unknown.
@@ -284,6 +288,16 @@ pub(crate) fn row_bytes(info: &PixelFormatInfo, width: u32, bytes_per_row: u64)
across.saturating_mul(info.block_bytes).min(bytes_per_row)
}
/// The rows of a texture `height` pixels high of the format `info` describes: its texels down,
/// or its blocks down for a format of blocks ([`BLOCKS`]), `height` where the block's height is
/// unknown.
pub(crate) fn texel_rows(info: &PixelFormatInfo, height: u32) -> u64 {
match (info.flags & BLOCKS != 0, info.block_height) {
(true, 0) | (false, _) => u64::from(height),
(true, block_height) => u64::from(height).div_ceil(block_height),
}
}
/// One plane of a picture's output: `height` rows of `width` pixels, `row_bytes` each,
/// `bytes_per_row` apart, from `offset` in `buffer`: its last byte is the last row's last, which
/// may lie less than `bytes_per_row` from the buffer's end (a texture at a place inside a plane
@@ -816,8 +830,8 @@ impl<P: Platform> GraphicsDecoder<P> {
_ => None,
};
let plane = |p: PlanePlace| PagesPlane {
offset: p.offset,
bytes_per_row: p.bytes_per_row,
offset: p.in_surface.0,
bytes_per_row: p.in_surface.1,
row_bytes: p.row_bytes,
rows: p.height,
};
+17 -7
View File
@@ -488,9 +488,9 @@ Stages:
- **The device cache:** where the host's GPU does not share the CPU's
memory, the device has `RGFX_FEATURE_SURFACE_CACHE`, and a block the
kext marks cached (`RGFX_SURFACE_PAGES_CACHED`) gets a device cache on
the host: a Private buffer of the surface's bytes in the pages' layout,
which every buffer over the block is, so every texture over the
surface, of any shape, plane or format, lies over the same bytes. The
the host: a Private buffer of the surface's bytes, which every buffer
over the block is, so every texture over the surface, of any shape,
plane or format, lies over the same bytes. The
block's `SURFACE_PAGES` carries the surface's `RgfxSurfacePagesLayout`
where the bundle gave it, a deferred block's too, and the host lays
the cache out by it as by an ask's layout (below, *Device caches on a
@@ -505,7 +505,14 @@ Stages:
declares the bytes between a texture's rows unobserved, and a texture
whose rows lie with bytes between them may be an image over the
cache's memory too, where the runtime makes one (on an RTX 2080 it
makes none, for reasons of its own: `decode::surface_cache`). The
makes none, for reasons of its own: `decode::surface_cache`). Where a
plane's rows lie at another pitch than the GPU's own linear images lay
them (CoreAnimation names its backing stores' rows at multiples of 64
bytes, and RADV lays its images' at 256), the host lays the cache's
planes out at the GPU's pitch instead, each texture over a plane at
the same byte of the same row there, and the cache's copies move each
row between its place in the pages and its place in the cache
(`decode::surface_cache`, "Laid out at the GPU's pitch"). The
answer is that of a message with no layout. Each
command buffer names the cached surfaces it uses
(`RGFX_OP_SURFACE_USE`) with the kext's count of the surface's
@@ -815,9 +822,12 @@ Stages:
answers its GPU's alignment as well (the runtime's
`minimumLinearTextureAlignmentForPixelFormat:` of BGRA8: 256 on RADV,
discrete or not, and 128 on the RTX 3090 and RTX 2080, docs/lessons.md):
a texture over a surface's pages, or over its device cache, which keeps
the pages' layout, takes the GPU's own linear image there only where its
rows lie at the GPU's pitch, and otherwise is the runtime's shadow.
a texture over a surface's pages takes the GPU's own linear image
there only where its rows lie at the GPU's pitch, and otherwise is the
runtime's shadow; a device cache of a surface whose layout the kext
gives lays the planes' rows at the GPU's pitch (*The device cache*,
above), so a texture over it takes the GPU's image whatever the
surface's pitch.
At the same start the kext lowers the largest surface IOSurface
answers to `IOSurfaceGetPropertyMaximum`, as a Mac's GPU accelerators
do: as wide and as tall as the device's 2D textures (the device