diff --git a/crates/reims-vgpu-vulkan/src/core/platform/device.rs b/crates/reims-vgpu-vulkan/src/core/platform/device.rs index 9b8ac6aac..29f0b3126 100644 --- a/crates/reims-vgpu-vulkan/src/core/platform/device.rs +++ b/crates/reims-vgpu-vulkan/src/core/platform/device.rs @@ -125,6 +125,23 @@ pub trait GpuDevice: 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> { + 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; diff --git a/crates/reims-vgpu-vulkan/src/core/testkit/impls/device.rs b/crates/reims-vgpu-vulkan/src/core/testkit/impls/device.rs index 8e299d74a..e543310c9 100644 --- a/crates/reims-vgpu-vulkan/src/core/testkit/impls/device.rs +++ b/crates/reims-vgpu-vulkan/src/core/testkit/impls/device.rs @@ -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 for Fk { ) } + /// 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::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<::CommandQueue> { fatal(self.make(Policy::Fatal, "newCommandQueue", vec![], |w| { Some(w.new_obj::(Model::None)) diff --git a/crates/reims-vgpu-vulkan/src/gpu/device.rs b/crates/reims-vgpu-vulkan/src/gpu/device.rs index c84544433..f5eae1f1e 100644 --- a/crates/reims-vgpu-vulkan/src/gpu/device.rs +++ b/crates/reims-vgpu-vulkan/src/gpu/device.rs @@ -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> { + 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>) -> VulkanBuffer { let id = raw.object_id(); @@ -1128,21 +1158,22 @@ impl GpuDevice for VulkanDevice { length: u64, options: u64, ) -> RaiseResult> { - 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> { + 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| { + 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 diff --git a/crates/reims-vgpu/src/graphics/decode/surface_cache.rs b/crates/reims-vgpu/src/graphics/decode/surface_cache.rs index cce46e953..a4e2c99c9 100644 --- a/crates/reims-vgpu/src/graphics/decode/surface_cache.rs +++ b/crates/reims-vgpu/src/graphics/decode/surface_cache.rs @@ -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 SurfaceCaches

{ } 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 SurfaceCaches

{ } 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 SurfaceCaches

{ } /// 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> { + /// 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> { #[cfg(unix)] if self.exportable { let made = device.new_exportable_buffer_of_length(ON, length, STORAGE_PRIVATE)?; @@ -1316,7 +1331,10 @@ impl SurfaceCaches

{ } 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()) diff --git a/docs/graphics-kext/design.md b/docs/graphics-kext/design.md index ad8a624b0..7990d899b 100644 --- a/docs/graphics-kext/design.md +++ b/docs/graphics-kext/design.md @@ -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").