🐎 Make a scratch buffer of the host's own without zeroing it

A staged copy of an IOSurface device cache on rt makes a Shared buffer per
copy, and newBufferWithLength:options: zeroes every byte of it through the
mapping before it returns, as Metal's contract asks of a buffer the guest
sees. The staged copies' buffers are the host's own, and every byte they
read is written first, so the zeroing is pure cost: on an x86-64 macOS 26
desktop walk on a 780M the staging makes took 406 us a copy, 3.2% of a
frame's CPU, most of it zeroing the 141 MB buffers of a 6016x6016 surface's
downloads.

GpuDevice gains new_scratch_buffer_of_length, a buffer as
new_buffer_of_length's whose bytes its maker writes before anything reads
them. Its default makes the buffer as new_buffer_of_length does, so the
Vulkan platform and the testkit are as they were; rt makes a Shared one in
a block of its own with no zeroing (new_buffer with no zero_from), and any
other as before. Nothing the guest names is made so.
This commit is contained in:
Anees Iqbal committed 2026-11-19 06:34:46 -08:00
1 parent d37fe66861
commit 47135d241f
3 files changed
+91

No files matched your search

@@ -105,6 +105,20 @@ pub trait GpuDevice<P: Platform>: Handle {
options: u64,
) -> RaiseResult<Option<P::Buffer>>;
/// No Metal counterpart: a buffer as [`new_buffer_of_length`](GpuDevice::new_buffer_of_length)'s,
/// the host's own, whose bytes its maker writes before anything reads them: the platform need
/// not zero them. The graphics lane's staged copies of the IOSurface device caches stage
/// through one (`decode::surface_cache`, "Staged copies"); nothing the guest names is made
/// so. The default 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_scratch_buffer_of_length(
&self,
on: OnFault,
length: u64,
options: u64,
) -> RaiseResult<Option<P::Buffer>> {
self.new_buffer_of_length(on, length, options)
}
/// No Metal counterpart: a buffer as [`new_buffer_of_length`](GpuDevice::new_buffer_of_length)'s,
/// in an allocation of its own whose memory another API imports
/// ([`Buffer::export_memory`](super::resource::Buffer::export_memory)): the host's video
/// writes its pictures into the IOSurface device caches so, on its own GPU work. `Ok(None)`
@@ -282,6 +282,33 @@ pub fn of_length(
}
}
/// [`of_length`]'s buffer whose bytes its maker writes before anything reads them
/// ([`GpuDevice::new_scratch_buffer_of_length`](crate::core::platform::device::GpuDevice::new_scratch_buffer_of_length)):
/// a Shared one in a block of its own, as [`own`]'s, with its bytes as the block held them, not
/// zeroed; any other as `of_length`'s.
pub fn scratch_of_length(
device: &RtObj<RtDevice>,
sel: &'static str,
length: u64,
raw: u64,
) -> Option<RtObj<RtBuffer>> {
let o = match options(raw, false) {
Ok(o) => o,
Err(why) => return refused_metal(sel, &why),
};
if o.storage != Storage::Shared || length == 0 || length > rt::MAX_BUFFER_LENGTH {
return made(device, sel, length, o, None, None);
}
let block = take(device, sel, rt::MemoryClass::Shared, length)?;
match device.rt().new_buffer(block.id, 0, length, None) {
Ok(made) => Some(buffer(device, made, o.answered)),
Err(error) => {
rt_refused_create(sel, &error);
None
}
}
}
/// `newBufferWithDescriptor:` of `desc` on `device`: a no-copy one as [`over_host_memory`]'s, its
/// deallocator given the bytes' address and the length; another as [`of_length`]'s, at the
/// pinned address where it lies in a known block, and holding a copy of the contents where given.
@@ -1275,4 +1302,41 @@ mod tests {
.expect("never a raise");
assert!(texture.is_some());
}
/// On the GPU the test holds: a scratch buffer is a Shared buffer of the length asked,
/// answering its options, its bytes mapped and none of them zeroed, where
/// `new_buffer_of_length`'s are all zeroed; a Private one is made as `new_buffer_of_length`
/// makes it, zeroed; Metal's refusals nil.
#[test]
fn scratch_buffers_on_vulkan() {
let platform = RtPlatform::new();
let device = (platform.system_default_device()).expect("an rt device on the GPU it holds");
let on = OnFault::Return;
let zeroed = || device.rt().counters().metrics().bytes_zeroed;
let scratch = |length, options| {
(device.new_scratch_buffer_of_length(on, length, options)).expect("never a raise")
};
let before = zeroed();
let buffer = scratch(100_000, 0).expect("a Shared scratch buffer");
assert_eq!(
(buffer.length(), buffer.storage_mode(), buffer.resource_options()),
(100_000, 0, 0x200)
);
assert_ne!(buffer.contents(), HostPtr::NULL);
assert_ne!(buffer.gpu_address(), 0);
assert_eq!(zeroed(), before, "a scratch buffer's bytes are not zeroed");
let before = zeroed();
let plain = (device.new_buffer_of_length(on, 100_000, 0)).expect("never a raise");
assert!(plain.is_some());
assert!(zeroed() >= before + 100_000, "a new buffer's bytes are zeroed");
let before = zeroed();
let private = scratch(4096, 0x20).expect("a Private scratch buffer");
assert_eq!((private.storage_mode(), private.contents()), (2, HostPtr::NULL));
assert!(zeroed() >= before + 4096, "a Private one is made as any, zeroed");
assert!(scratch(0, 0).is_none() && scratch(64, 0x30).is_none());
}
}
@@ -572,6 +572,19 @@ impl GpuDevice<RtPlatform> for RtObj<RtDevice> {
Ok(buffer::of_length(self, sel, length, options))
}
/// [`new_buffer_of_length`](GpuDevice::new_buffer_of_length)'s, a Shared one not zeroed
/// ([`buffer::scratch_of_length`]).
fn new_scratch_buffer_of_length(
&self,
on: OnFault,
length: u64,
options: u64,
) -> RaiseResult<Option<<RtPlatform as Platform>::Buffer>> {
let _ = on;
let sel = "newBufferWithLength:options:";
Ok(buffer::scratch_of_length(self, sel, length, options))
}
/// rt's queue, of Metal's default `maxCommandBufferCount`; nil where rt refuses it.
fn new_command_queue(&self) -> Option<RtObj<RtQueue>> {
match self.rt.new_command_queue(METAL_MAX_COMMAND_BUFFER_COUNT) {