🐎 Declare a laid-out device cache's bytes between rows unobserved
A device cache on a GPU that shares the CPU's memory exists because textures over the surface's pages cannot lie in place. A 3662x2038 desktop's surface is one of them: 14648 bytes of texels in rows 14848 apart. metal2vulkan then made each texture over the cache a shadow, as an image at that pitch holds the bytes between rows, and Vulkan lets layout moves and a render pass's load and store write them. On the 780M that cost about 1 G texels/s of copies in a Finder drag (pad boot m3-max-7495: 169 shadowed textures, drag 36 fps). A laid-out cache's copies into the pages already take the texels alone (d48b1fe76), and nothing else reads those bytes, so the cache's buffer now says so. GpuDevice::new_buffer_of_length_padding_unobserved is a buffer as newBufferWithLength:options: makes one. The Vulkan platform makes it with metal2vulkan's BufferDescriptor::with_padding_unobserved, and the default makes it as any other buffer. SurfaceCaches::new_cache makes a laid-out cache so. An exportable cache, a deferred one and one with no layout are made as before: only a GPU of its own memory exports or defers caches, and only a GPU that shares the CPU's memory lays them out. What changes for the guest: a texture over such a cache is an image over the cache's memory, which may change the bytes between rows there. No copy carries them into the pages, so what the guest's CPU reads of the surface is unchanged. The host does fewer copies. Measured on the 780M (cold boots of macOS 26.7.2 on um870 and um790, each against development without this commit on the same runtime, 0 fatal): 12 or 13 textures a boot lie over laid-out caches of small padded surfaces, where each was a shadow (unobserved_padding_aliases), all as Safari's, Finder's and TextEdit's windows open; 5 to 12 fewer shadowed textures, and the shadows' copies back over the session fall from 23.5-28.8M texels to 12.2-12.8M, as those textures' writes land in the cache. Copies in and the frame rate (60) are unchanged. The desktop's surface, 3648 wide since7638d2ea0, is not padded and lies in place in every run. Tests: - Over a buffer with the declaration, a texture lies over the buffer's memory (unobserved_padding_aliases +1, no shadow). This holds where the device lays its linear images' rows at multiples of an alignment past Metal's 16 bytes (RADV's 256). Over a buffer without the declaration, the texture is a shadow. - A laid-out cache's buffer is made by the new call, and one with no layout by newBufferWithLength:options:.
This commit is contained in:
1 parent
246ab4403a
commit
df50639ea5
5 files changed
+225
-23
No files matched your search
@@ -125,6 +125,23 @@ pub trait GpuDevice<P: Platform>: Handle {
|
||||
let _ = (on, length, options);
|
||||
Ok(None)
|
||||
}
|
||||
/// No Metal counterpart: a buffer as [`new_buffer_of_length`](GpuDevice::new_buffer_of_length)'s,
|
||||
/// that declares the bytes between the rows (and slices) of a texture over it unobserved: a
|
||||
/// texture whose rows lie with bytes of the buffer between them may hold those bytes in its
|
||||
/// memory, and change them, and so lies over the buffer's memory where the platform would
|
||||
/// otherwise make it a copy of its own. The graphics lane makes a device cache so where the
|
||||
/// kext's ask gave its surface's layout, as its copies into the pages take the surface's
|
||||
/// texels alone (`decode::surface_cache`). The default, for a platform with no such
|
||||
/// declaration, makes the buffer as `new_buffer_of_length` does. Reached both under a handler
|
||||
/// and with none: `on` says which this call is.
|
||||
fn new_buffer_of_length_padding_unobserved(
|
||||
&self,
|
||||
on: OnFault,
|
||||
length: u64,
|
||||
options: u64,
|
||||
) -> RaiseResult<Option<P::Buffer>> {
|
||||
self.new_buffer_of_length(on, length, options)
|
||||
}
|
||||
/// sel: newCommandQueue
|
||||
/// Never nil-checked. No path reaches it under a handler: a raise inside it dies at the send.
|
||||
fn new_command_queue(&self) -> Option<P::CommandQueue>;
|
||||
|
||||
@@ -37,6 +37,10 @@ use crate::core::raise::{OnFault, RaiseResult};
|
||||
/// of Metal's names it.
|
||||
pub const UNSHARED_QUEUE: &str = "newCommandQueue (unshared)";
|
||||
|
||||
/// The name the fake logs and scripts [`GpuDevice::new_buffer_of_length_padding_unobserved`]
|
||||
/// under: no selector of Metal's names it.
|
||||
pub const PADDING_UNOBSERVED_BUFFER: &str = "newBufferWithLength:options: (padding unobserved)";
|
||||
|
||||
/// A device's model: its capabilities.
|
||||
#[derive(Debug)]
|
||||
pub struct DeviceModel {
|
||||
@@ -172,6 +176,22 @@ impl GpuDevice<FakePlatform> for Fk<kind::Device> {
|
||||
)
|
||||
}
|
||||
|
||||
/// Logged and scripted under [`PADDING_UNOBSERVED_BUFFER`], as no selector of Metal's names
|
||||
/// it; its buffer is `newBufferWithLength:options:`'s.
|
||||
fn new_buffer_of_length_padding_unobserved(
|
||||
&self,
|
||||
on: OnFault,
|
||||
length: u64,
|
||||
options: u64,
|
||||
) -> RaiseResult<Option<<FakePlatform as Platform>::Buffer>> {
|
||||
self.make(
|
||||
Policy::On(on),
|
||||
PADDING_UNOBSERVED_BUFFER,
|
||||
vec![Arg::U64(length), Arg::U64(options)],
|
||||
|w| new_buffer(w, length, options, Backing::Fresh, None),
|
||||
)
|
||||
}
|
||||
|
||||
fn new_command_queue(&self) -> Option<<FakePlatform as Platform>::CommandQueue> {
|
||||
fatal(self.make(Policy::Fatal, "newCommandQueue", vec![], |w| {
|
||||
Some(w.new_obj::<kind::CommandQueue>(Model::None))
|
||||
|
||||
@@ -663,6 +663,36 @@ impl VulkanDevice {
|
||||
Some(self.texture_of(raw, None, 0, Arc::new(metal_space::Hold::NONE)))
|
||||
}
|
||||
|
||||
/// `newBufferWithLength:options:`'s buffer (`new_buffer_of_length`), declaring the bytes
|
||||
/// between a texture's rows over it unobserved where `padding_unobserved`
|
||||
/// (`new_buffer_of_length_padding_unobserved`).
|
||||
fn buffer_of_length(
|
||||
&self,
|
||||
length: u64,
|
||||
options: u64,
|
||||
padding_unobserved: bool,
|
||||
) -> RaiseResult<Option<VulkanBuffer>> {
|
||||
let sel = "newBufferWithLength:options:";
|
||||
let options = crate::gpu::resource_options(sel, options);
|
||||
let (placement, mut placing) = metal_space::placing(length, own_bytes(options), None);
|
||||
let pool = pool_slot(sel, &self.raw, &mut placing)?;
|
||||
let descriptor = rt::BufferDescriptor::new(length, options, rt::BufferContents::Zero)
|
||||
.with_placement(placement);
|
||||
let descriptor = match padding_unobserved {
|
||||
true => descriptor.with_padding_unobserved(),
|
||||
false => descriptor,
|
||||
};
|
||||
let descriptor = with_token(in_slot(descriptor, &pool), placing.token());
|
||||
let made = create_buffer(
|
||||
sel,
|
||||
placed_nil(&placing, Nil::BufferLength),
|
||||
placing.pin_refused(),
|
||||
|| self.raw.new_buffer_with_descriptor(descriptor),
|
||||
);
|
||||
metal_space::let_go(pool);
|
||||
made.map(|b| b.map(|raw| self.buffer(raw, None)))
|
||||
}
|
||||
|
||||
/// The handle of a buffer this device made, over `surface` when it was made on one.
|
||||
pub(crate) fn buffer(&self, raw: rt::Buffer, surface: Option<Arc<OsSurface>>) -> VulkanBuffer {
|
||||
let id = raw.object_id();
|
||||
@@ -1128,21 +1158,22 @@ impl GpuDevice<VulkanPlatform> for VulkanDevice {
|
||||
length: u64,
|
||||
options: u64,
|
||||
) -> RaiseResult<Option<VulkanBuffer>> {
|
||||
let sel = "newBufferWithLength:options:";
|
||||
let options = crate::gpu::resource_options(sel, options);
|
||||
let (placement, mut placing) = metal_space::placing(length, own_bytes(options), None);
|
||||
let pool = pool_slot(sel, &self.raw, &mut placing)?;
|
||||
let descriptor = rt::BufferDescriptor::new(length, options, rt::BufferContents::Zero)
|
||||
.with_placement(placement);
|
||||
let descriptor = with_token(in_slot(descriptor, &pool), placing.token());
|
||||
let made = create_buffer(
|
||||
sel,
|
||||
placed_nil(&placing, Nil::BufferLength),
|
||||
placing.pin_refused(),
|
||||
|| self.raw.new_buffer_with_descriptor(descriptor),
|
||||
);
|
||||
metal_space::let_go(pool);
|
||||
made.map(|b| b.map(|raw| self.buffer(raw, None)))
|
||||
self.buffer_of_length(length, options, false)
|
||||
}
|
||||
|
||||
/// As `new_buffer_of_length`'s, with the runtime's declaration that the bytes between the
|
||||
/// rows and slices of a texture over the buffer are unobserved
|
||||
/// (`BufferDescriptor::with_padding_unobserved`): a texture whose rows lie with bytes between
|
||||
/// them is then a linear image over the buffer's memory that holds them, where the device
|
||||
/// lays one out at the texture's rows (`WorkCounts::unobserved_padding_aliases`), and not a
|
||||
/// shadow (`TextureShadowReason::BytesBetweenRows`).
|
||||
fn new_buffer_of_length_padding_unobserved(
|
||||
&self,
|
||||
_on: OnFault,
|
||||
length: u64,
|
||||
options: u64,
|
||||
) -> RaiseResult<Option<VulkanBuffer>> {
|
||||
self.buffer_of_length(length, options, true)
|
||||
}
|
||||
|
||||
/// The runtime's exportable buffer (`new_exportable_buffer`): a block of its own, exportable
|
||||
@@ -2516,6 +2547,73 @@ pub(crate) mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
/// A texture whose rows lie with bytes between them, over a Private buffer that declares those
|
||||
/// bytes unobserved (`new_buffer_of_length_padding_unobserved`, the graphics lane's laid-out
|
||||
/// device caches), is an image over the buffer's memory that holds them, where the device's
|
||||
/// own linear images lay their rows at the texture's pitch: the runtime counts it among the
|
||||
/// images that do (`unobserved_padding_aliases`) and shadows nothing. Over a buffer made as
|
||||
/// any other the same texture is a shadow (`BytesBetweenRows`), counted among the shadowed
|
||||
/// textures. The rows lie as the guest's surfaces' do on a host that answers its linear row
|
||||
/// alignment (`linear_row_alignment`): one texel past the alignment, a row of `BGRA8Unorm`
|
||||
/// texels 4 bytes past it, in rows twice it apart. Where it is past Metal's 16 bytes the
|
||||
/// device's images lay their rows at its multiples (the runtime's probe of them: 256 on
|
||||
/// RADV), so at the texture's pitch. At 16 bytes a device may lay them tighter (ANV lays them
|
||||
/// 4 bytes apart), its image of the texture lying at another pitch, and the texture is a
|
||||
/// shadow over either buffer
|
||||
#[test]
|
||||
fn a_texture_over_a_buffer_with_padding_unobserved_lies_over_its_memory() {
|
||||
use crate::core::platform::Platform as _;
|
||||
use crate::core::platform::resource::Buffer as _;
|
||||
use crate::core::platform::values::TextureDesc;
|
||||
|
||||
use crate::platform::tests::platform;
|
||||
|
||||
const PRIVATE: u64 = 0x20;
|
||||
let d = dev();
|
||||
let align = d.linear_row_alignment();
|
||||
assert!(align >= 16 && align.is_power_of_two(), "{align}");
|
||||
let (width, height, pitch) = (align / 4 + 1, 8, 2 * align);
|
||||
// The checks the device starts make images of their own: they end before the counts are
|
||||
// read, so only this test's textures move them.
|
||||
#[cfg(target_os = "linux")]
|
||||
settle_device_checks(&d);
|
||||
let desc = platform().texture_descriptor(&TextureDesc {
|
||||
pixel_format: 80,
|
||||
width,
|
||||
height,
|
||||
usage: 0x5,
|
||||
resource_options: Some(PRIVATE),
|
||||
..TextureDesc::default()
|
||||
});
|
||||
let counts = || {
|
||||
let counts = d.raw.work_counts();
|
||||
(counts.unobserved_padding_aliases, counts.shadowed_textures)
|
||||
};
|
||||
let texture_over = |buffer: Option<VulkanBuffer>| {
|
||||
let before = counts();
|
||||
let texture = (buffer.expect("a buffer")).new_texture(OnFault::Return, &desc, 0, pitch);
|
||||
assert!(matches!(texture, Ok(Some(_))), "a texture");
|
||||
let after = counts();
|
||||
(after.0 - before.0, after.1 - before.1)
|
||||
};
|
||||
let length = pitch * height;
|
||||
let declared = d.new_buffer_of_length_padding_unobserved(OnFault::Return, length, PRIVATE);
|
||||
let declared = texture_over(declared.unwrap());
|
||||
let plain = texture_over(
|
||||
d.new_buffer_of_length(OnFault::Return, length, PRIVATE)
|
||||
.unwrap(),
|
||||
);
|
||||
assert_eq!(plain, (0, 1), "a shadow over a buffer made as any other");
|
||||
if align > 16 {
|
||||
assert_eq!(declared, (1, 0), "over the buffer's memory, at {align}");
|
||||
} else {
|
||||
assert!(
|
||||
declared == (1, 0) || declared == (0, 1),
|
||||
"over its memory or a shadow: {declared:?}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// A sparse heap takes its range where the model of Metal's space places it: unpinned or at a
|
||||
/// pin it honours it is made; at a pin it refuses (one off a 32-KiB granule) it dies naming
|
||||
/// the pin, as the runtime would make it and Metal's answer there is not measured
|
||||
|
||||
@@ -55,7 +55,12 @@
|
||||
//! texels alone, each plane's rows' bytes ([`texel_spans`]): the bytes beside them, between
|
||||
//! rows or outside the planes, are the guest's CPU's alone, as no texture's texel lies there
|
||||
//! and the device makes no buffer over an IOSurface, so they stay as it wrote them, whatever a
|
||||
//! render over the cache leaves in its own. A fill copies every byte in.
|
||||
//! render over the cache leaves in its own. A fill copies every byte in. Nothing else reads
|
||||
//! those bytes of the cache, so its buffer declares them unobserved
|
||||
//! ([`GpuDevice::new_buffer_of_length_padding_unobserved`]): a texture over it whose rows lie
|
||||
//! with bytes between them may hold and change them, and lies over the cache's memory where
|
||||
//! the platform would otherwise make it a copy of its own (on the Vulkan platform, metal2vulkan's
|
||||
//! shadow, which each command buffer copies in before its use and back after its write).
|
||||
//! A download or a release copies nothing where, as its copy is made (at once, or in the
|
||||
//! finish of a buffer begun before it), the CPU's generation in the block's header
|
||||
//! ([`CPU_GEN_OFFSET`]) has moved past the cache's: the lock that asked gave up waiting for the
|
||||
@@ -120,6 +125,7 @@
|
||||
//! command buffer of its own, and a refused copy by runs encodes nothing in it.
|
||||
//!
|
||||
//! [`GpuDevice::copies_guest_ram`]: reims_vgpu_vulkan::core::platform::device::GpuDevice::copies_guest_ram
|
||||
//! [`GpuDevice::new_buffer_of_length_padding_unobserved`]: reims_vgpu_vulkan::core::platform::device::GpuDevice::new_buffer_of_length_padding_unobserved
|
||||
//!
|
||||
//! What the guest observes is IOSurface's contract under its locks: a lock after a GPU write sees
|
||||
//! it, and a GPU use after a read-write lock sees the CPU's writes. A CPU write made without a
|
||||
@@ -996,7 +1002,7 @@ impl<P: Platform> SurfaceCaches<P> {
|
||||
}
|
||||
self.released(device, block, 0, None, None, Box::new(|| {}))?;
|
||||
}
|
||||
let Some(cache) = self.new_cache(device, length)? else {
|
||||
let Some(cache) = self.new_cache(device, length, layout.is_some())? else {
|
||||
return Ok(false);
|
||||
};
|
||||
let Some(pages) = pages()? else {
|
||||
@@ -1112,7 +1118,7 @@ impl<P: Platform> SurfaceCaches<P> {
|
||||
}
|
||||
let room = self.deferred_budget.saturating_sub(self.deferred_bytes);
|
||||
let made = if length <= room {
|
||||
self.new_cache(device, length)?
|
||||
self.new_cache(device, length, false)?
|
||||
} else {
|
||||
None
|
||||
};
|
||||
@@ -1304,9 +1310,18 @@ impl<P: Platform> SurfaceCaches<P> {
|
||||
}
|
||||
|
||||
/// A new cache's buffer of `length` bytes, Private: exportable where the host's video
|
||||
/// imports caches ([`Caches::set_exportable`]) and the platform makes one, else as any other
|
||||
/// buffer.
|
||||
fn new_cache(&mut self, device: &P::Device, length: u64) -> RaiseResult<Option<P::Buffer>> {
|
||||
/// imports caches ([`Caches::set_exportable`]) and the platform makes one; else, where
|
||||
/// `laid_out` (the kext's ask gave the surface's layout, so the cache's copies into the pages
|
||||
/// take its texels alone: [`SurfaceCache::texels`]), one that declares the bytes between a
|
||||
/// texture's rows unobserved, which a texture over the cache whose rows lie with bytes
|
||||
/// between them may hold and change in place
|
||||
/// ([`GpuDevice::new_buffer_of_length_padding_unobserved`]); else as any other buffer.
|
||||
fn new_cache(
|
||||
&mut self,
|
||||
device: &P::Device,
|
||||
length: u64,
|
||||
laid_out: bool,
|
||||
) -> RaiseResult<Option<P::Buffer>> {
|
||||
#[cfg(unix)]
|
||||
if self.exportable {
|
||||
let made = device.new_exportable_buffer_of_length(ON, length, STORAGE_PRIVATE)?;
|
||||
@@ -1316,7 +1331,10 @@ impl<P: Platform> SurfaceCaches<P> {
|
||||
}
|
||||
self.counts.unexportable += 1;
|
||||
}
|
||||
device.new_buffer_of_length(ON, length, STORAGE_PRIVATE)
|
||||
match laid_out {
|
||||
true => device.new_buffer_of_length_padding_unobserved(ON, length, STORAGE_PRIVATE),
|
||||
false => device.new_buffer_of_length(ON, length, STORAGE_PRIVATE),
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether the block at `block` has a cache.
|
||||
@@ -5817,6 +5835,49 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
/// A cache made for a block the device answered a cache with its surface's layout is a buffer
|
||||
/// that declares the bytes between a texture's rows unobserved, as its copies into the pages
|
||||
/// take the texels alone, so a texture over it whose rows lie with bytes between them lies in
|
||||
/// place; a cache of a block named cached with no layout is made as any other buffer
|
||||
#[test]
|
||||
fn a_laid_out_caches_buffer_declares_the_bytes_between_rows_unobserved() {
|
||||
use super::super::surface_pages::tests::one_plane;
|
||||
use reims_vgpu_graphics::wire::RGFX_STATUS_CACHE;
|
||||
use reims_vgpu_vulkan::core::platform::values::PagesInPlace;
|
||||
use reims_vgpu_vulkan::core::testkit::impls::device::PADDING_UNOBSERVED_BUFFER;
|
||||
const NEW_BUFFER: &str = "newBufferWithLength:options:";
|
||||
let mut r = rig(16);
|
||||
let page = r.page;
|
||||
r.dec.set_unified_memory(true);
|
||||
let world = Arc::clone(r.p.world());
|
||||
world.answer_surface_pages(PagesInPlace::No("the rows end inside a line".into()));
|
||||
let made = |r: &Rig, mark: u64| {
|
||||
let calls = r.p.call_log().since(mark);
|
||||
let count = |sel: &str| calls.iter().filter(|c| c.sel == sel).count();
|
||||
(count(PADDING_UNOBSERVED_BUFFER), count(NEW_BUFFER))
|
||||
};
|
||||
let layout = one_plane(page, page - 48, 3);
|
||||
assert_eq!(
|
||||
r.pages_asking(BLOCK, &[(1, 4)], 4 * page, Some(layout)),
|
||||
RGFX_STATUS_CACHE
|
||||
);
|
||||
let mark = r.p.call_log().mark();
|
||||
assert_eq!(
|
||||
r.pages_cached(BLOCK, &[(1, 4)], 4 * page, true),
|
||||
RGFX_STATUS_OK
|
||||
);
|
||||
assert_eq!(made(&r, mark), (1, 0), "laid out: declared");
|
||||
|
||||
let other = BLOCK + 0x8000;
|
||||
let mark = r.p.call_log().mark();
|
||||
assert_eq!(
|
||||
r.pages_cached(other, &[(8, 2)], 2 * page, true),
|
||||
RGFX_STATUS_OK
|
||||
);
|
||||
assert!(r.dec.caches().has(other));
|
||||
assert_eq!(made(&r, mark), (0, 1), "no layout: as any other buffer");
|
||||
}
|
||||
|
||||
/// The (source offset, destination offset, size) of each copy in `calls`.
|
||||
fn offsets(calls: &[Call]) -> Vec<(u64, u64, u64)> {
|
||||
(calls.iter())
|
||||
|
||||
@@ -573,7 +573,13 @@ Stages:
|
||||
into the pages copy the surface's texels alone, each plane's rows from
|
||||
the layout the ask gave, so the bytes between rows stay as the guest's
|
||||
CPU wrote them, whatever a render over the cache leaves in its own
|
||||
(`decode/surface_cache.rs`, "Download"). A block asked with no
|
||||
(`decode/surface_cache.rs`, "Download"). So the cache's buffer
|
||||
declares the bytes between a texture's rows unobserved
|
||||
(`GpuDevice::new_buffer_of_length_padding_unobserved`; metal2vulkan's
|
||||
`BufferDescriptor::padding_unobserved`), and a texture over it whose
|
||||
rows lie with bytes between them, the desktop's among them, is an
|
||||
image over the cache's own memory that holds them, not another copy
|
||||
of the texels. A block asked with no
|
||||
layout (an older bundle's) stays in place, as every block of such a
|
||||
host did before (`reims-vgpu`'s `graphics/decode/surface_pages.rs`,
|
||||
"Device caches where the pages cannot serve").
|
||||
|
||||
Reference in new issue
Block a user