diff --git a/crates/reims-vgpu-graphics/include/reims_graphics_wire.h b/crates/reims-vgpu-graphics/include/reims_graphics_wire.h index b20371c50..27885a579 100644 --- a/crates/reims-vgpu-graphics/include/reims_graphics_wire.h +++ b/crates/reims-vgpu-graphics/include/reims_graphics_wire.h @@ -1041,7 +1041,8 @@ // 10 are reserved. #define RGFX_REG_FEATURES_LO 8 -// `FEATURES_HI`, RO: feature bits 32 to 63. +// `FEATURES_HI`, RO: feature bits 32 to 63, the ones the host offers, which the guest takes +// through [`RGFX_REG_DRIVER_FEATURES_HI`]: [`RGFX_FEATURE_SURFACE_DEFERRED_CARRY`]. #define RGFX_REG_FEATURES_HI 12 // `FEATURES_LO` bit 0, `SURFACE_PAGES`: the host maps an IOSurface's own guest pages for its GPU @@ -1273,6 +1274,20 @@ // `SURFACE_CACHE_REFUSED`. #define RGFX_FEATURE_SURFACE_DEFERRED_ASK (1 << 30) +// `FEATURES_HI` bit 0, feature bit 32, `SURFACE_DEFERRED_CARRY`, which the guest takes through +// [`RGFX_REG_DRIVER_FEATURES_HI`], and which means nothing without +// [`RGFX_FEATURE_SURFACE_DEFERRED`] or [`RGFX_FEATURE_SURFACE_DEFERRED_ASK`]: a deferral +// ([`RGFX_SURFACE_PAGES_DEFERRED`]) of a surface no CPU lock has touched, a few of whose pages +// are resident all the same (written by a process without a lock), may carry those pages, its +// runs the surface's bytes in order, each run of pages that are not resident +// [`RGFX_PAGE_RUN_ZEROS`]. The host makes the cache of zeros as for any deferral, and fills it +// from the carried pages, the zeros runs left as they are, as a new cache's first fill: before +// what reads the cache next, or never where a decode writes the surface's planes whole first. +// The kext keeps the carried pages wired until their block's pages are named +// ([`RGFX_SURFACE_PAGES_ATTACH`]) or its release is answered. A host offers it where it offers +// either deferral; one that does not never gets a deferral with runs. +#define RGFX_FEATURE_SURFACE_DEFERRED_CARRY 4294967296 + // `STATUS`, RW: writing [`RGFX_STATUS_RESET`] resets the device; the guest writes // [`RGFX_STATUS_DRIVER_READY`] once it has read the rings' headers. #define RGFX_REG_STATUS 16 @@ -2041,6 +2056,11 @@ // answer all the same. A host that does not know the bit copies, as for any release. #define RGFX_BLOCK_RELEASE_UNHELD (1 << 1) +// [`RgfxPageRun::address`] of a run of a carrying deferral's surface +// ([`RGFX_FEATURE_SURFACE_DEFERRED_CARRY`]) whose pages are not resident: the cache holds its +// make's zeros there. No page's address: it is no multiple of the guest's 4 KiB page. +#define RGFX_PAGE_RUN_ZEROS UINT64_MAX + // Where a `SURFACE_PAGES` message's [`RgfxSurfacePagesCache`] starts: right after its // [`RgfxSurfacePages`]. A message whose `size` is at least // `RGFX_SURFACE_PAGES_CACHE_OFFSET + size_of::()` carries it; one of @@ -2064,14 +2084,19 @@ // [`RGFX_FEATURE_SURFACE_DEFERRED`], or [`RGFX_FEATURE_SURFACE_DEFERRED_ASK`] and then with // [`RGFX_SURFACE_PAGES_ASK_CACHE`]: the block's surface has no pages yet (a fresh surface no CPU // lock and no other device has touched), and its contents live in the device cache alone, as a -// GPU of its own memory keeps a surface only the GPU uses. The message has `run_count` 0; the -// host makes the cache, fills it with zeros before any use, holds the CPU's generation 0, and -// makes no view and watches no pages. Its `BLOCK_RELEASE` copies nothing, there being no pages +// GPU of its own memory keeps a surface only the GPU uses. The message has `run_count` 0, or, +// where the guest took [`RGFX_FEATURE_SURFACE_DEFERRED_CARRY`], runs of the surface's bytes in +// order that carry its resident pages, the others [`RGFX_PAGE_RUN_ZEROS`]; the host makes the +// cache, fills it with zeros before any use, holds the CPU's generation 0 (or, carrying, owes +// the carried pages' fill, as a new cache owes its first), and makes no view and watches no +// pages but the carried ones. Its `BLOCK_RELEASE` copies nothing, there being no pages // (the kext names them first where the surface still lives). Answered [`RGFX_STATUS_REFUSED`] // where the host made no cache (its GPU's memory, or its budget, is spent), and, asking, where // the surface's textures might lie in place over its pages. One with `ASK_CACHE` where the host // does not offer `SURFACE_DEFERRED_ASK`, or without it where it does, is -// [`RGFX_STATUS_INVALID_ARGUMENT`]. +// [`RGFX_STATUS_INVALID_ARGUMENT`], and so is one with runs where the host does not offer +// `SURFACE_DEFERRED_CARRY`, or whose runs are not whole pages summing to the surface's bytes, or +// carry none. #define RGFX_SURFACE_PAGES_DEFERRED (1 << 2) // [`RgfxSurfacePagesCache::flags`], with [`RGFX_SURFACE_PAGES_CACHED`]: the pages of a deferred @@ -5780,7 +5805,8 @@ typedef struct { } RgfxOpWaitForFence; // A run of an IOSurface's guest RAM, which `SURFACE_PAGES` lists: `length` bytes at guest -// physical address `address`, both multiples of the guest's 4 KiB page. +// physical address `address`, both multiples of the guest's 4 KiB page (but a carrying +// deferral's [`RGFX_PAGE_RUN_ZEROS`]). typedef struct { // The run's first guest physical address. uint64_t address; diff --git a/crates/reims-vgpu-graphics/src/device.rs b/crates/reims-vgpu-graphics/src/device.rs index ca029fd5a..b8bd25b34 100644 --- a/crates/reims-vgpu-graphics/src/device.rs +++ b/crates/reims-vgpu-graphics/src/device.rs @@ -246,7 +246,7 @@ impl Room { const MIN_QUEUE_RING_DATA: u64 = 4096; /// The feature bits whose meaning the host knows, which a `DRIVER_FEATURES` write may take: -/// `RGFX_FEATURE_*`, bits 0 to 8 and 11 to 30. +/// `RGFX_FEATURE_*`, bits 0 to 8 and 11 to 32. const KNOWN_FEATURES: u64 = (RGFX_FEATURE_SURFACE_PAGES | RGFX_FEATURE_SURFACE_CACHE | RGFX_FEATURE_CB_SUBMITTED @@ -275,7 +275,8 @@ const KNOWN_FEATURES: u64 = (RGFX_FEATURE_SURFACE_PAGES | RGFX_FEATURE_OPENCL_MODE | RGFX_FEATURE_PRIVATE_RENDER_STATE | RGFX_FEATURE_SURFACE_CACHE_REFUSED - | RGFX_FEATURE_SURFACE_DEFERRED_ASK) as u64; + | RGFX_FEATURE_SURFACE_DEFERRED_ASK) as u64 + | RGFX_FEATURE_SURFACE_DEFERRED_CARRY; /// The `RGFX_QUEUE_*` flags the host carries out, each with the feature the guest's driver must /// take for a `QUEUE_CREATE` to ask for it: [`RGFX_QUEUE_UNSHARED`] alone, so a create that asks diff --git a/crates/reims-vgpu-graphics/src/wire.rs b/crates/reims-vgpu-graphics/src/wire.rs index cb959617e..c49eaf0a9 100644 --- a/crates/reims-vgpu-graphics/src/wire.rs +++ b/crates/reims-vgpu-graphics/src/wire.rs @@ -68,7 +68,8 @@ pub const RGFX_REG_VERSION: u32 = 0x004; /// [`RGFX_FEATURE_SURFACE_CACHE_REFUSED`] and [`RGFX_FEATURE_SURFACE_DEFERRED_ASK`]). Bits 9 and /// 10 are reserved. pub const RGFX_REG_FEATURES_LO: u32 = 0x008; -/// `FEATURES_HI`, RO: feature bits 32 to 63. +/// `FEATURES_HI`, RO: feature bits 32 to 63, the ones the host offers, which the guest takes +/// through [`RGFX_REG_DRIVER_FEATURES_HI`]: [`RGFX_FEATURE_SURFACE_DEFERRED_CARRY`]. pub const RGFX_REG_FEATURES_HI: u32 = 0x00c; /// `FEATURES_LO` bit 0, `SURFACE_PAGES`: the host maps an IOSurface's own guest pages for its GPU /// to use in place (`SURFACE_PAGES`, `BUFFER_OVER_SURFACE_PAGES`); without it a surface's GPU @@ -270,6 +271,19 @@ pub const RGFX_FEATURE_SURFACE_CACHE_REFUSED: u32 = 1 << 29; /// bytes of its GPU's memory such caches may hold. A host offers it where it offers /// `SURFACE_CACHE_REFUSED`. pub const RGFX_FEATURE_SURFACE_DEFERRED_ASK: u32 = 1 << 30; +/// `FEATURES_HI` bit 0, feature bit 32, `SURFACE_DEFERRED_CARRY`, which the guest takes through +/// [`RGFX_REG_DRIVER_FEATURES_HI`], and which means nothing without +/// [`RGFX_FEATURE_SURFACE_DEFERRED`] or [`RGFX_FEATURE_SURFACE_DEFERRED_ASK`]: a deferral +/// ([`RGFX_SURFACE_PAGES_DEFERRED`]) of a surface no CPU lock has touched, a few of whose pages +/// are resident all the same (written by a process without a lock), may carry those pages, its +/// runs the surface's bytes in order, each run of pages that are not resident +/// [`RGFX_PAGE_RUN_ZEROS`]. The host makes the cache of zeros as for any deferral, and fills it +/// from the carried pages, the zeros runs left as they are, as a new cache's first fill: before +/// what reads the cache next, or never where a decode writes the surface's planes whole first. +/// The kext keeps the carried pages wired until their block's pages are named +/// ([`RGFX_SURFACE_PAGES_ATTACH`]) or its release is answered. A host offers it where it offers +/// either deferral; one that does not never gets a deferral with runs. +pub const RGFX_FEATURE_SURFACE_DEFERRED_CARRY: u64 = 0x1_0000_0000; /// `STATUS`, RW: writing [`RGFX_STATUS_RESET`] resets the device; the guest writes /// [`RGFX_STATUS_DRIVER_READY`] once it has read the rings' headers. pub const RGFX_REG_STATUS: u32 = 0x010; @@ -1596,8 +1610,14 @@ pub const RGFX_BLOCK_RELEASE_NO_CLIENT: u32 = 1 << 0; /// answer all the same. A host that does not know the bit copies, as for any release. pub const RGFX_BLOCK_RELEASE_UNHELD: u32 = 1 << 1; +/// [`RgfxPageRun::address`] of a run of a carrying deferral's surface +/// ([`RGFX_FEATURE_SURFACE_DEFERRED_CARRY`]) whose pages are not resident: the cache holds its +/// make's zeros there. No page's address: it is no multiple of the guest's 4 KiB page. +pub const RGFX_PAGE_RUN_ZEROS: u64 = u64::MAX; + /// A run of an IOSurface's guest RAM, which `SURFACE_PAGES` lists: `length` bytes at guest -/// physical address `address`, both multiples of the guest's 4 KiB page. +/// physical address `address`, both multiples of the guest's 4 KiB page (but a carrying +/// deferral's [`RGFX_PAGE_RUN_ZEROS`]). #[repr(C)] #[derive( Clone, Copy, Debug, Default, PartialEq, Eq, FromBytes, IntoBytes, Immutable, KnownLayout, @@ -1654,14 +1674,19 @@ pub const RGFX_SURFACE_PAGES_PRIVATE: u32 = 1 << 1; /// [`RGFX_FEATURE_SURFACE_DEFERRED`], or [`RGFX_FEATURE_SURFACE_DEFERRED_ASK`] and then with /// [`RGFX_SURFACE_PAGES_ASK_CACHE`]: the block's surface has no pages yet (a fresh surface no CPU /// lock and no other device has touched), and its contents live in the device cache alone, as a -/// GPU of its own memory keeps a surface only the GPU uses. The message has `run_count` 0; the -/// host makes the cache, fills it with zeros before any use, holds the CPU's generation 0, and -/// makes no view and watches no pages. Its `BLOCK_RELEASE` copies nothing, there being no pages +/// GPU of its own memory keeps a surface only the GPU uses. The message has `run_count` 0, or, +/// where the guest took [`RGFX_FEATURE_SURFACE_DEFERRED_CARRY`], runs of the surface's bytes in +/// order that carry its resident pages, the others [`RGFX_PAGE_RUN_ZEROS`]; the host makes the +/// cache, fills it with zeros before any use, holds the CPU's generation 0 (or, carrying, owes +/// the carried pages' fill, as a new cache owes its first), and makes no view and watches no +/// pages but the carried ones. Its `BLOCK_RELEASE` copies nothing, there being no pages /// (the kext names them first where the surface still lives). Answered [`RGFX_STATUS_REFUSED`] /// where the host made no cache (its GPU's memory, or its budget, is spent), and, asking, where /// the surface's textures might lie in place over its pages. One with `ASK_CACHE` where the host /// does not offer `SURFACE_DEFERRED_ASK`, or without it where it does, is -/// [`RGFX_STATUS_INVALID_ARGUMENT`]. +/// [`RGFX_STATUS_INVALID_ARGUMENT`], and so is one with runs where the host does not offer +/// `SURFACE_DEFERRED_CARRY`, or whose runs are not whole pages summing to the surface's bytes, or +/// carry none. pub const RGFX_SURFACE_PAGES_DEFERRED: u32 = 1 << 2; /// [`RgfxSurfacePagesCache::flags`], with [`RGFX_SURFACE_PAGES_CACHED`]: the pages of a deferred /// block ([`RGFX_SURFACE_PAGES_DEFERRED`]), which the kext wired at the surface's first CPU lock diff --git a/crates/reims-vgpu/src/graphics.rs b/crates/reims-vgpu/src/graphics.rs index b505e8196..4bf13cc63 100644 --- a/crates/reims-vgpu/src/graphics.rs +++ b/crates/reims-vgpu/src/graphics.rs @@ -61,14 +61,15 @@ use reims_vgpu_graphics::wire::{ RGFX_FEATURE_PAGES_BUFFERS, RGFX_FEATURE_PRIVATE_RENDER_STATE, RGFX_FEATURE_SHARED_EVENT_NAMES, RGFX_FEATURE_SMALL_ID_CHUNKS, RGFX_FEATURE_SUBMIT_ROOM, RGFX_FEATURE_SURFACE_CACHE, RGFX_FEATURE_SURFACE_CACHE_REFUSED, RGFX_FEATURE_SURFACE_DEFERRED, - RGFX_FEATURE_SURFACE_DEFERRED_ASK, RGFX_FEATURE_SURFACE_ORDER, RGFX_FEATURE_SURFACE_PAGES, - RGFX_FEATURE_UNSHARED_QUEUES, RGFX_RESOURCE_ID_CHUNK, RGFX_RESOURCE_ID_CHUNKS, - RGFX_STATUS_INVALID_ARGUMENT, RGFX_STATUS_OK, RGFX_STATUS_REFUSED, RGFX_VIDEO_CAPS_CODECS, - RGFX_VIDEO_CAPS_OFFSET, RGFX_VIDEO_CAPS_OUTPUT_CACHED, RGFX_VIDEO_CAPS_VERSION, RgfxBlockGrant, - RgfxBlockRelease, RgfxBufferOverPages, RgfxCbComplete, RgfxCbScheduled, RgfxCbSubmitted, - RgfxColorEntry, RgfxDisplayColor, RgfxEventSignalled, RgfxHeapOverPages, RgfxIcbState, - RgfxLibraryLookup, RgfxMsgHeader, RgfxPageRun, RgfxSamplerState, RgfxSharedEventName, - RgfxSurfacePages, RgfxSurfacePagesLayout, RgfxVideoCaps, + RGFX_FEATURE_SURFACE_DEFERRED_ASK, RGFX_FEATURE_SURFACE_DEFERRED_CARRY, + RGFX_FEATURE_SURFACE_ORDER, RGFX_FEATURE_SURFACE_PAGES, RGFX_FEATURE_UNSHARED_QUEUES, + RGFX_RESOURCE_ID_CHUNK, RGFX_RESOURCE_ID_CHUNKS, RGFX_STATUS_INVALID_ARGUMENT, RGFX_STATUS_OK, + RGFX_STATUS_REFUSED, RGFX_VIDEO_CAPS_CODECS, RGFX_VIDEO_CAPS_OFFSET, + RGFX_VIDEO_CAPS_OUTPUT_CACHED, RGFX_VIDEO_CAPS_VERSION, RgfxBlockGrant, RgfxBlockRelease, + RgfxBufferOverPages, RgfxCbComplete, RgfxCbScheduled, RgfxCbSubmitted, RgfxColorEntry, + RgfxDisplayColor, RgfxEventSignalled, RgfxHeapOverPages, RgfxIcbState, RgfxLibraryLookup, + RgfxMsgHeader, RgfxPageRun, RgfxSamplerState, RgfxSharedEventName, RgfxSurfacePages, + RgfxSurfacePagesLayout, RgfxVideoCaps, }; use reims_vgpu_graphics::{ CompleteAnswer, CompletionPoster, CursorImage, Host, Mode, PresentAnswer, QueueClaim, Scanout, @@ -351,18 +352,20 @@ impl GraphicsDevice

{ // A host whose GPU shares the CPU's memory keeps a device cache only of the // surfaces whose pages its textures cannot use in place, where the kext asks, // and keeps a fresh one of those in its cache alone where the kext defers it, - // asking. + // asking, the few pages a process wrote of it without a lock carried in. (true, true) => { in_place | u64::from(RGFX_FEATURE_SURFACE_CACHE_REFUSED) | u64::from(RGFX_FEATURE_SURFACE_DEFERRED_ASK) + | RGFX_FEATURE_SURFACE_DEFERRED_CARRY } // A host that keeps caches keeps fresh surfaces in them alone where the kext - // defers their pages. + // defers their pages, the few a process wrote without a lock carried in. (true, false) => { in_place | u64::from(RGFX_FEATURE_SURFACE_CACHE) | u64::from(RGFX_FEATURE_SURFACE_DEFERRED) + | RGFX_FEATURE_SURFACE_DEFERRED_CARRY } }; let deferred_budget = deferred_budget(features, gpu_device.own_memory_budget()); @@ -370,6 +373,9 @@ impl GraphicsDevice

{ if features & u64::from(RGFX_FEATURE_SURFACE_DEFERRED_ASK) != 0 { decoder.set_deferred_asks(); } + if features & RGFX_FEATURE_SURFACE_DEFERRED_CARRY != 0 { + decoder.set_deferred_carries(); + } // The processes read the features from the device info, as no register is theirs. info.features_lo = u8::try_from(features & 0xff).unwrap_or_default(); // A host whose surfaces lie in place in their pages has the guest align their rows as diff --git a/crates/reims-vgpu/src/graphics/decode/cmdbuf.rs b/crates/reims-vgpu/src/graphics/decode/cmdbuf.rs index 4d3329c81..21c36dab2 100644 --- a/crates/reims-vgpu/src/graphics/decode/cmdbuf.rs +++ b/crates/reims-vgpu/src/graphics/decode/cmdbuf.rs @@ -3795,15 +3795,45 @@ mod tests { /// bitstream buffer 2, the H.264 session 40 of a fake host's video, buffer 6 over the block /// and textures 7 and 8 over its planes; and what the video is handed. fn cached_video() -> (super::super::surface_pages::tests::Rig, Arc) { - use super::super::surface_pages::tests::{BLOCK, rig}; - use reims_vgpu_graphics::wire::RGFX_STATUS_OK; + video_rig(false) + } + + /// [`cached_video`]'s rig, its block, where `carrying`, deferred carrying its first page, a + /// page of guest RAM, the other a run of zeros, as a decoder's new output with a page its + /// process wrote without a lock is (`RGFX_FEATURE_SURFACE_DEFERRED_CARRY`). + fn video_rig(carrying: bool) -> (super::super::surface_pages::tests::Rig, Arc) { + use super::super::surface_pages::tests::{BLOCK, RAM, rig}; + use reims_vgpu_graphics::wire::{ + RGFX_PAGE_RUN_ZEROS, RGFX_STATUS_OK, RGFX_SURFACE_PAGES_CACHED, + RGFX_SURFACE_PAGES_DEFERRED, RgfxPageRun, + }; let mut r = rig(8); let page = r.page; - assert_eq!( - r.pages_cached(BLOCK, &[(1, 2)], 2 * page, true), - RGFX_STATUS_OK - ); + if carrying { + r.dec.set_deferred_budget(8 * page); + r.dec.set_deferred_carries(); + let runs = [ + RgfxPageRun { + address: RAM + page, + length: page, + }, + RgfxPageRun { + address: RGFX_PAGE_RUN_ZEROS, + length: page, + }, + ]; + let flags = RGFX_SURFACE_PAGES_CACHED | RGFX_SURFACE_PAGES_DEFERRED; + assert_eq!( + r.pages_of_runs(BLOCK, 0, &runs, 2 * page, flags, None), + RGFX_STATUS_OK + ); + } else { + assert_eq!( + r.pages_cached(BLOCK, &[(1, 2)], 2 * page, true), + RGFX_STATUS_OK + ); + } let queue = RgfxQueueCreate { header: RgfxMsgHeader { r#type: RGFX_CMD_QUEUE_CREATE, @@ -3888,11 +3918,17 @@ mod tests { /// output, whose planes' rows are the surface's bytes 32 to 1568. The block's cache owes its /// first fill, its zeros done. fn cached_video_whole() -> (super::super::surface_pages::tests::Rig, Arc) { + video_whole(false) + } + + /// [`cached_video_whole`] of [`video_rig`]'s rig, its block deferred carrying a page where + /// `carrying`. + fn video_whole(carrying: bool) -> (super::super::surface_pages::tests::Rig, Arc) { use reims_vgpu_graphics::wire::{ RGFX_SURFACE_FACTS_OFFSET, RGFX_YCBCR_MATRIX_ITU_R_709_2, RgfxSurfaceFacts, }; - let (mut r, seen) = cached_video(); + let (mut r, seen) = video_rig(carrying); let facts = RgfxSurfaceFacts { surface_pixel_format: u32::from_be_bytes(*b"420v"), plane_count: 2, @@ -3996,6 +4032,47 @@ mod tests { assert!(done.load(Ordering::SeqCst)); } + /// A decoder's new output deferred carrying a page its process wrote without a lock, a + /// command buffer of a decode alone whose textures are the whole of the surface's two planes, + /// takes no fill of its cache either: the carried page's first fill is taken as met, as a new + /// cache's is, nothing copied in, and the cache holds the use's generation, so a later use of + /// it fills nothing + #[test] + fn a_decode_takes_a_carried_fill_as_met() { + let (mut r, seen) = video_whole(true); + let cache = video_cache(&r); + let mark = r.p.call_log().mark(); + assert_eq!( + r.dec + .decode(&r.device, &writes_cached_video(100, 1, true)) + .unwrap(), + Ok(()) + ); + assert_eq!(*seen.cached.lock(), [[true, true]], "into the cache"); + let (into, _) = cache_copies(&r, mark, cache); + assert_eq!(into, 0, "no fill"); + let counts = r.dec.caches().counts(); + assert_eq!( + ( + counts.carried, + counts.fills, + counts.made_fills, + counts.decode_firsts + ), + (1, 0, 0, 1) + ); + r.p.world().complete_all(CompleteOn::Caller); + let mark = r.p.call_log().mark(); + assert_eq!( + r.dec + .decode(&r.device, &writes_cached_video(101, 2, false)) + .unwrap(), + Ok(()) + ); + let (into, _) = cache_copies(&r, mark, cache); + assert_eq!(into, 0, "generation 0 held"); + } + /// A decoder's new output whose picture fails to land holds the cache's zeros where the /// picture did not write, and no byte of the pages or of any other memory: its cache was made /// zeros, the whole of it, before the decode, and nothing was copied into it. The command diff --git a/crates/reims-vgpu/src/graphics/decode/mod.rs b/crates/reims-vgpu/src/graphics/decode/mod.rs index 2b8a1d00d..b668c23d8 100644 --- a/crates/reims-vgpu/src/graphics/decode/mod.rs +++ b/crates/reims-vgpu/src/graphics/decode/mod.rs @@ -103,34 +103,34 @@ use reims_vgpu_graphics::wire::{ RGFX_EVT_SAMPLER_STATE, RGFX_EVT_SIZE_AND_ALIGN, RGFX_LINKED_BINARY_FUNCTIONS, RGFX_LINKED_FUNCTIONS, RGFX_LINKED_GROUP, RGFX_LINKED_PRIVATE_FUNCTIONS, RGFX_MESH_PIPELINE_MESH_TG_MULTIPLE_OF_EXEC_WIDTH, - RGFX_MESH_PIPELINE_OBJECT_TG_MULTIPLE_OF_EXEC_WIDTH, RGFX_PIPELINE_KIND_COMPUTE, - RGFX_PIPELINE_KIND_MESH, RGFX_PIPELINE_KIND_RENDER, RGFX_PIPELINE_OK, - RGFX_PIPELINE_REFLECT_SUPPORT_ICB, RGFX_PIPELINE_REFLECT_THREADGROUP_MATCHES_TILE, - RGFX_PIPELINE_REFUSED, RGFX_PRIVATE_RENDER_DEPTH_STENCIL_WRITE_DISABLED, - RGFX_PRIVATE_RENDER_LOGIC_OPERATION, RGFX_PRIVATE_RENDER_OPENGL_MODE, - RGFX_PRIVATE_RENDER_SAMPLE_COVERAGE_INVERT, RGFX_RENDER_PIPELINE_ALPHA_TO_COVERAGE, - RGFX_RENDER_PIPELINE_ALPHA_TO_ONE, RGFX_RENDER_PIPELINE_PRIVATE_STATE, - RGFX_RENDER_PIPELINE_RASTERIZATION_ENABLED, RGFX_RENDER_PIPELINE_SUPPORT_ICB, - RGFX_RESOURCE_ID_CHUNK, RGFX_RESOURCE_ID_LIMIT, RGFX_RESOURCE_ID_SMALL_CHUNK, - RGFX_RESOURCE_ID_SMALL_CHUNKS, RGFX_STATUS_CACHE, RGFX_STATUS_INVALID_ARGUMENT, RGFX_STATUS_OK, - RGFX_STATUS_REFUSED, RGFX_SURFACE_FACTS_OFFSET, RGFX_SURFACE_NO_WRITERS, - RGFX_SURFACE_PAGES_ASK_CACHE, RGFX_SURFACE_PAGES_ATTACH, RGFX_SURFACE_PAGES_CACHED, - RGFX_SURFACE_PAGES_DEFERRED, RGFX_SURFACE_PAGES_PRIVATE, RGFX_YCBCR_MATRIX_DCI_P3, - RGFX_YCBCR_MATRIX_ITU_R_601_4, RGFX_YCBCR_MATRIX_ITU_R_709_2, RGFX_YCBCR_MATRIX_ITU_R_2020, - RGFX_YCBCR_MATRIX_NONE, RGFX_YCBCR_MATRIX_OTHER, RGFX_YCBCR_MATRIX_P3_D65, - RGFX_YCBCR_MATRIX_SMPTE_240M_1995, RgfxBufferAtOffset, RgfxBufferCreate, - RgfxBufferOverHostMemory, RgfxBufferOverPages, RgfxCbComplete, RgfxCbScheduled, - RgfxCbSubmitted, RgfxColorAttachmentDesc, RgfxComputePipelineCreate, RgfxDepthStencilCreate, - RgfxEventSignalled, RgfxFunctionConstant, RgfxHeapCreate, RgfxHeapOverHostMemory, - RgfxHeapOverPages, RgfxHeapTextureSizeAndAlign, RgfxLinearTextureFromBuffer, - RgfxLinkedFunction, RgfxMeshPipelineReflection, RgfxMeshRenderPipelineCreate, RgfxMsgHeader, - RgfxObjectCreate, RgfxObjectDestroy, RgfxPageRun, RgfxPipelineReflection, RgfxQueueCreate, - RgfxRenderPipelineCreate, RgfxRenderPipelinePrivateState, RgfxResourceIdChunk, - RgfxSamplerCreate, RgfxSamplerState, RgfxSharedEventCreate, RgfxSharedEventSetValue, - RgfxSizeAndAlign, RgfxStencilDescriptor, RgfxSurfaceFacts, RgfxSurfacePages, - RgfxSurfacePagesLayout, RgfxSurfaceWriter, RgfxSurfaceWriters, RgfxTextureCreate, - RgfxTextureDescriptor, RgfxTextureFromBuffer, RgfxTextureFromIOSurface, RgfxVertexAttribute, - RgfxVertexBufferLayout, RgfxVideoSessionCreate, + RGFX_MESH_PIPELINE_OBJECT_TG_MULTIPLE_OF_EXEC_WIDTH, RGFX_PAGE_RUN_ZEROS, + RGFX_PIPELINE_KIND_COMPUTE, RGFX_PIPELINE_KIND_MESH, RGFX_PIPELINE_KIND_RENDER, + RGFX_PIPELINE_OK, RGFX_PIPELINE_REFLECT_SUPPORT_ICB, + RGFX_PIPELINE_REFLECT_THREADGROUP_MATCHES_TILE, RGFX_PIPELINE_REFUSED, + RGFX_PRIVATE_RENDER_DEPTH_STENCIL_WRITE_DISABLED, RGFX_PRIVATE_RENDER_LOGIC_OPERATION, + RGFX_PRIVATE_RENDER_OPENGL_MODE, RGFX_PRIVATE_RENDER_SAMPLE_COVERAGE_INVERT, + RGFX_RENDER_PIPELINE_ALPHA_TO_COVERAGE, RGFX_RENDER_PIPELINE_ALPHA_TO_ONE, + RGFX_RENDER_PIPELINE_PRIVATE_STATE, RGFX_RENDER_PIPELINE_RASTERIZATION_ENABLED, + RGFX_RENDER_PIPELINE_SUPPORT_ICB, RGFX_RESOURCE_ID_CHUNK, RGFX_RESOURCE_ID_LIMIT, + RGFX_RESOURCE_ID_SMALL_CHUNK, RGFX_RESOURCE_ID_SMALL_CHUNKS, RGFX_STATUS_CACHE, + RGFX_STATUS_INVALID_ARGUMENT, RGFX_STATUS_OK, RGFX_STATUS_REFUSED, RGFX_SURFACE_FACTS_OFFSET, + RGFX_SURFACE_NO_WRITERS, RGFX_SURFACE_PAGES_ASK_CACHE, RGFX_SURFACE_PAGES_ATTACH, + RGFX_SURFACE_PAGES_CACHED, RGFX_SURFACE_PAGES_DEFERRED, RGFX_SURFACE_PAGES_PRIVATE, + RGFX_YCBCR_MATRIX_DCI_P3, RGFX_YCBCR_MATRIX_ITU_R_601_4, RGFX_YCBCR_MATRIX_ITU_R_709_2, + RGFX_YCBCR_MATRIX_ITU_R_2020, RGFX_YCBCR_MATRIX_NONE, RGFX_YCBCR_MATRIX_OTHER, + RGFX_YCBCR_MATRIX_P3_D65, RGFX_YCBCR_MATRIX_SMPTE_240M_1995, RgfxBufferAtOffset, + RgfxBufferCreate, RgfxBufferOverHostMemory, RgfxBufferOverPages, RgfxCbComplete, + RgfxCbScheduled, RgfxCbSubmitted, RgfxColorAttachmentDesc, RgfxComputePipelineCreate, + RgfxDepthStencilCreate, RgfxEventSignalled, RgfxFunctionConstant, RgfxHeapCreate, + RgfxHeapOverHostMemory, RgfxHeapOverPages, RgfxHeapTextureSizeAndAlign, + RgfxLinearTextureFromBuffer, RgfxLinkedFunction, RgfxMeshPipelineReflection, + RgfxMeshRenderPipelineCreate, RgfxMsgHeader, RgfxObjectCreate, RgfxObjectDestroy, RgfxPageRun, + RgfxPipelineReflection, RgfxQueueCreate, RgfxRenderPipelineCreate, + RgfxRenderPipelinePrivateState, RgfxResourceIdChunk, RgfxSamplerCreate, RgfxSamplerState, + RgfxSharedEventCreate, RgfxSharedEventSetValue, RgfxSizeAndAlign, RgfxStencilDescriptor, + RgfxSurfaceFacts, RgfxSurfacePages, RgfxSurfacePagesLayout, RgfxSurfaceWriter, + RgfxSurfaceWriters, RgfxTextureCreate, RgfxTextureDescriptor, RgfxTextureFromBuffer, + RgfxTextureFromIOSurface, RgfxVertexAttribute, RgfxVertexBufferLayout, RgfxVideoSessionCreate, }; use reims_vgpu_video::core::device_memory::MemoryLife; use reims_vgpu_vulkan::core::host::HostPtr; @@ -1021,6 +1021,13 @@ impl GraphicsDecoder

{ self.surface_caches.set_deferred_asks(); } + /// Has a deferral carry its surface's resident pages where it has some from now on, as the + /// device offers `SURFACE_DEFERRED_CARRY` ([`Self::deferred_surface_pages`]): set once, at + /// the device's make. + pub(crate) fn set_deferred_carries(&self) { + self.surface_caches.set_deferred_carries(); + } + /// Whether a cached block on `device` is named by its runs, with no view: the device copies /// the guest's RAM by them (`surface_pages`, "By runs"). fn copies_by_runs(&self, device: &P::Device) -> bool { @@ -1212,8 +1219,8 @@ impl GraphicsDecoder

{ kext counts them: {} of them written since their last copy, dropped \ uncopied ({} MiB), {} copied as taken again; {} copies out not made, the CPU's \ generation past the cache's; {} \ - deferred, {} refused, {} refused as they might lie in place, {} named \ - their pages ({} copied), {} released unnamed; {} early copies of {} MiB ({} on the read-back queue), {} \ + deferred ({} carrying pages), {} refused, {} refused as they might lie in \ + place, {} named their pages ({} copied), {} released unnamed; {} early copies of {} MiB ({} on the read-back queue), {} \ downloads they answered, {} unread; {} copy by runs, {} copies by runs \ refused; {} downloads or releases answered {} ms or more after they were \ asked; {} fills made behind the command buffers they followed, {} waits on \ @@ -1226,7 +1233,7 @@ impl GraphicsDecoder

{ d.downloads, d.downloaded_bytes >> 20, d.clean, d.host_writes, d.unread, d.unheld_released, d.unheld, d.unheld_bytes >> 20, d.retaken, d.behind, - d.deferred, d.deferred_refused, d.deferred_in_place, d.attached, + d.deferred, d.carried, d.deferred_refused, d.deferred_in_place, d.attached, d.attached_copied, d.unattached, d.early, d.early_bytes >> 20, d.early_aside, d.early_read, d.early_unread, d.by_runs, d.runs_refused, @@ -1411,7 +1418,14 @@ impl GraphicsDecoder

{ /// [`RGFX_STATUS_INVALID_ARGUMENT`] for a block with no deferred cache whose pages are not /// named, with no view made. /// - /// A message with both flags, or without `CACHED`, or a deferred block's with runs, is + /// A deferral may carry its surface's resident pages, where the device offers + /// `SURFACE_DEFERRED_CARRY`: its runs are the surface's bytes in order, those of pages that + /// are not resident [`RGFX_PAGE_RUN_ZEROS`], and its cache owes a first fill of the others + /// ([`surface_cache::CarriedPages`]). + /// + /// A message with both flags, or without `CACHED`, or a deferred block's with runs where the + /// device offers no `SURFACE_DEFERRED_CARRY`, or with runs that are not whole guest pages + /// summing to the surface, in the guest's RAM the host maps, or that carry none, is /// [`RGFX_STATUS_INVALID_ARGUMENT`], and changes nothing; so is a deferred block's that asks /// where the device offers no `SURFACE_DEFERRED_ASK`, or with a layout that does not lie in /// its bytes, or that does not ask where the device offers it, and pages that ask. `None`, @@ -1433,11 +1447,14 @@ impl GraphicsDecoder

{ let cached = flags & RGFX_SURFACE_PAGES_CACHED != 0; let private = flags & RGFX_SURFACE_PAGES_PRIVATE != 0; let ask = flags & RGFX_SURFACE_PAGES_ASK_CACHE != 0; - let asks = self.surface_caches.lock().deferred_asks(); + let (asks, carries) = { + let caches = self.surface_caches.lock(); + (caches.deferred_asks(), caches.deferred_carries()) + }; if !cached || private || (deferred && attach) - || (deferred && !runs.is_empty()) + || (deferred && !runs.is_empty() && !carries) || (attach && ask) || (deferred && ask != asks) { @@ -1484,6 +1501,23 @@ impl GraphicsDecoder

{ Some(planes) } }; + // The pages it carries, where it carries some. + let (carried, host_pages) = if runs.is_empty() { + (None, Vec::new()) + } else { + let Some((carried, host_pages)) = self.carried_pages(runs, pages.length) else { + log::warn!( + target: "reims_vgpu::graphics", + "the IOSurface block at {block:#x}: deferred carrying {} runs that are \ + not whole guest pages summing to its {:#x} bytes in the guest's RAM, or \ + carry none: refused", + runs.len(), + pages.length + ); + return Some(RGFX_STATUS_INVALID_ARGUMENT); + }; + (Some(carried), host_pages) + }; if !surface_pages::SurfaceViews::deferred(views, block, settle) { return None; } @@ -1493,6 +1527,8 @@ impl GraphicsDecoder

{ pages.length, pages.block_size, planes, + carried, + host_pages, ); return Some(match made { Ok(true) => RGFX_STATUS_OK, @@ -1595,6 +1631,51 @@ impl GraphicsDecoder

{ Some(RGFX_STATUS_REFUSED) } + /// The pages a carrying deferral's `runs` carry, of a surface of `length` bytes + /// ([`Self::deferred_surface_pages`]): each guest page of a run that is not + /// [`RGFX_PAGE_RUN_ZEROS`], as the surface's byte it holds, its host address in the guest's + /// RAM and its bytes; and their host addresses as runs, which the cache watches. `None` where + /// a run is not whole guest pages, they do not sum to the surface's bytes in whole pages, a + /// page lies outside the guest's RAM the host maps, or none is carried. + fn carried_pages( + &self, + runs: &[RgfxPageRun], + length: u64, + ) -> Option<(surface_cache::CarriedPages, Vec>)> { + let page = surface_pages::GUEST_PAGE; + let whole = length.checked_next_multiple_of(page)?; + let mut at = 0u64; + let mut carried = Vec::new(); + let mut named = Vec::new(); + let views = self.shared.surface_views.lock(); + for run in runs { + if run.length == 0 || !run.length.is_multiple_of(page) { + return None; + } + if run.address != RGFX_PAGE_RUN_ZEROS { + if !run.address.is_multiple_of(page) { + return None; + } + let mut from = at; + for (host, bytes) in views.host_pages_in_order(std::slice::from_ref(run))? { + carried.push((from, host, bytes)); + from += bytes; + } + named.push(*run); + } + at = at.checked_add(run.length)?; + } + if at != whole { + return None; + } + let host_pages = views.host_runs(&named); + drop(views); + Some(( + surface_cache::CarriedPages::new(carried, whole)?, + host_pages, + )) + } + /// Gives the block at `block`, whose view of `runs` was just made, or which is named by its /// runs, a device cache where the kext marked it `cached` ([`surface_cache`]); else the block /// keeps its view alone, and its buffers lie over the pages. A block named by its runs that gets no cache takes its view now. diff --git a/crates/reims-vgpu/src/graphics/decode/surface_cache.rs b/crates/reims-vgpu/src/graphics/decode/surface_cache.rs index 20e6033f7..9685a421d 100644 --- a/crates/reims-vgpu/src/graphics/decode/surface_cache.rs +++ b/crates/reims-vgpu/src/graphics/decode/surface_cache.rs @@ -252,6 +252,10 @@ pub(crate) struct SurfaceCache { /// The other end of each copy: the surface's own pages; `None` for a deferred block whose /// pages the kext has not named yet. pages: Option>, + /// A deferred block's carried pages, while its pages are not named + /// ([`RGFX_FEATURE_SURFACE_DEFERRED_CARRY`](reims_vgpu_graphics::wire::RGFX_FEATURE_SURFACE_DEFERRED_CARRY)): + /// what its first fill copies in, over its make's zeros. + carried: Option>, /// The surface's bytes. length: u64, /// The bytes of the surface's texels, where the kext's ask laid the surface out (its @@ -271,7 +275,7 @@ pub(crate) struct SurfaceCache { /// The CPU's generation of the surface the cache's first fill is of, owed from the cache's /// make, which made it zeros, until the first fill of the cache or a decode that takes it as /// met (the module doc, "Fill"); `None` then, and for a deferred block's cache, which owes - /// none. + /// none but where it carries pages (generation 0, of them alone). first_fill: Option, /// The context of the first buffer over the block while its first fill is owed (`Some(None)` /// for one of no context): a command buffer of its with work that may reach the cache with no @@ -506,6 +510,36 @@ impl CopiedPages { } } +/// A deferred block's carried pages ([`SurfaceCache::carried`]): each run of its surface's +/// resident pages as the surface's byte it starts at, its host address in the guest's RAM, which +/// the kext holds wired until the block's pages are named or its release is answered, and its +/// bytes, in the surface's order. +pub(crate) struct CarriedPages { + runs: Vec<(u64, HostPtr, u64)>, +} + +impl CarriedPages { + /// The carried pages of a surface of `length` bytes, from `runs` (the surface's byte, host + /// address, bytes), where each lies in the surface after the one before it and they hold + /// some bytes; `None` otherwise. + pub(crate) fn new(runs: Vec<(u64, HostPtr, u64)>, length: u64) -> Option { + let mut end = 0u64; + for &(at, _, bytes) in &runs { + let run_end = at.checked_add(bytes)?; + if at < end || bytes == 0 || run_end > length { + return None; + } + end = run_end; + } + (!runs.is_empty()).then_some(CarriedPages { runs }) + } + + /// Their bytes. + fn bytes(&self) -> u64 { + self.runs.iter().map(|&(_, _, bytes)| bytes).sum() + } +} + /// The host's transfer queue, on which every copy runs, and the event its fills signal. struct Transfer { /// Unshared ([`new_command_queue_unshared`]): a present of a surface waits for its fill, and @@ -690,6 +724,8 @@ pub(crate) struct CacheCounts { pub(crate) attached: u64, /// Of them, pages the host made no view of, or no buffer over, copied instead. pub(crate) attached_copied: u64, + /// Deferred blocks' caches made carrying pages ([`SurfaceCache::carried`]). + pub(crate) carried: u64, pub(crate) unattached: u64, /// Early copies of caches into their pages, behind a writer of a surface that reads back /// ([`SurfaceCaches::early_copies`]), and their bytes. @@ -814,6 +850,9 @@ pub(crate) struct SurfaceCaches { /// The device offers `SURFACE_DEFERRED_ASK`: a deferred block asks with its surface's layout /// ([`Self::set_deferred_asks`]). deferred_asks: bool, + /// Whether a deferral may carry its surface's resident pages, as the device offers + /// `SURFACE_DEFERRED_CARRY` ([`Self::set_deferred_carries`]). + deferred_carries: bool, /// The copies of caches into pages asked and not yet landed ([`Self::copies_into`]), those /// landed let go of as each is added. into_pages: Vec, @@ -976,6 +1015,7 @@ impl SurfaceCaches

{ deferred_budget: 0, deferred_bytes: 0, deferred_asks: false, + deferred_carries: false, into_pages: Vec::new(), exportable: false, owed: HashSet::new(), @@ -1088,6 +1128,7 @@ impl SurfaceCaches

{ SurfaceCache { device: cache, pages: Some(pages), + carried: None, length, texels: layout.as_deref().map(texel_spans), generation: None, @@ -1260,10 +1301,14 @@ impl SurfaceCaches

{ /// A cache of zeros for the deferred block at `block` (the module doc), of `length` bytes, /// at the CPU's generation 0, its block `block_size` bytes, its surface laid out as `layout` /// where its ask gave it (a device that shares the CPU's memory: [`Self::make`]'s caches of a - /// layout): zeroed on the transfer queue, whose fill every use of it waits for. `Ok(false)`, - /// with no cache, where the deferred caches' budget has no room for it, or the platform made - /// no buffer or no transfer queue for it. A cache the block had goes first, as at a release - /// ([`SurfaceCaches::released`]). + /// layout): zeroed on the transfer queue, whose fill every use of it waits for. Where it + /// `carried` pages (their host addresses in the guest's RAM `host_pages`), it owes a first + /// fill of them at generation 0, as a new cache owes one of its pages ([`Self::make`]), + /// which the zeroing comes before on the same queue, and their pages are watched until the + /// block's are named. `Ok(false)`, with no cache, where the deferred caches' budget has no + /// room for it, or the platform made no buffer or no transfer queue for it. A cache the + /// block had goes first, as at a release ([`SurfaceCaches::released`]). + #[allow(clippy::too_many_arguments)] // the deferral's, and its carried pages pub(crate) fn make_deferred( &mut self, device: &P::Device, @@ -1271,6 +1316,8 @@ impl SurfaceCaches

{ length: u64, block_size: u64, layout: Option>, + carried: Option, + host_pages: Vec>, ) -> RaiseResult { if self.by_block.contains_key(&block) { self.released(device, block, 0, None, None, Box::new(|| {}))?; @@ -1290,18 +1337,21 @@ impl SurfaceCaches

{ self.counts.deferred_refused += 1; return Ok(false); }; + let carries = carried.is_some(); self.by_block.insert( block, SurfaceCache { device: cache, pages: None, + carried: carried.map(Arc::new), length, texels: layout.as_deref().map(texel_spans), - generation: Some(0), + // A carrying block holds no generation until its first fill, as a new cache. + generation: (!carries).then_some(0), host_read: 0, written: false, filled_at, - first_fill: None, + first_fill: carries.then_some(0), owner: None, zeros: false, users: HashMap::new(), @@ -1314,6 +1364,15 @@ impl SurfaceCaches

{ life, }, ); + if carries { + let pages = Pages { + runs: host_pages, + writes: 0, + }; + self.watched.0.lock().insert(block, pages); + self.owed.insert(block); + self.counts.carried += 1; + } self.deferred_bytes += length; self.counts.made += 1; self.counts.deferred += 1; @@ -1394,6 +1453,7 @@ impl SurfaceCaches

{ }; if let Some(cache) = self.by_block.get_mut(&block) { cache.pages = Some(ends); + cache.carried = None; } let pages = Pages { runs: host_pages, @@ -1421,6 +1481,7 @@ impl SurfaceCaches

{ }; if let Some(cache) = self.by_block.get_mut(&block) { cache.pages = Some(CachePages::Copied(Arc::new(copied))); + cache.carried = None; } let pages = Pages { runs: host_pages, @@ -1465,6 +1526,17 @@ impl SurfaceCaches

{ self.deferred_asks = true; } + /// Has a deferral carry its surface's resident pages where it has some from now on, as the + /// device offers `SURFACE_DEFERRED_CARRY`. + pub(crate) fn set_deferred_carries(&mut self) { + self.deferred_carries = true; + } + + /// Whether a deferral may carry pages ([`Self::set_deferred_carries`]). + pub(crate) fn deferred_carries(&self) -> bool { + self.deferred_carries + } + /// Whether a deferred block asks with its surface's layout ([`Self::set_deferred_asks`]). pub(crate) fn deferred_asks(&self) -> bool { self.deferred_asks @@ -2335,9 +2407,9 @@ impl SurfaceCaches

{ else { return Ok(None); }; - // A deferred block's pages, not named yet, hold nothing the cache does not: the CPU has - // not locked the surface for writing. - if cache.pages.is_none() { + // A deferred block's pages, not named yet, hold nothing the cache does not but those it + // carried: the CPU has not locked the surface for writing. + if cache.pages.is_none() && cache.carried.is_none() { return Ok(None); } let Some(cb) = transfer.queue.command_buffer(ON)? else { @@ -3114,6 +3186,12 @@ impl Caches

{ self.state.lock().set_deferred_asks(); } + /// Has a deferral carry its surface's resident pages from now on + /// ([`SurfaceCaches::set_deferred_carries`]): set once, at the device's make. + pub(crate) fn set_deferred_carries(&self) { + self.state.lock().set_deferred_carries(); + } + /// Waits, letting the lock go meanwhile, while a command buffer begun and not finished that /// may write the block at `block`'s cache is, or a copy of it asked waits for command buffers /// ([`SurfaceCaches::deferred`]): for the block's pages named again, whose buffers made on @@ -3603,8 +3681,9 @@ impl Caches

{ } /// A cache of zeros for the deferred block at `block` ([`SurfaceCaches::make_deferred`]), - /// after every command buffer begun and not finished that may write the cache it had, and the - /// copies of it asked. + /// carrying `carried` where given, after every command buffer begun and not finished that may + /// write the cache it had, and the copies of it asked. + #[allow(clippy::too_many_arguments)] // the deferral's, and its carried pages pub(crate) fn make_deferred( &self, device: &P::Device, @@ -3612,10 +3691,14 @@ impl Caches

{ length: u64, block_size: u64, layout: Option>, + carried: Option, + host_pages: Vec>, ) -> RaiseResult { let mut state = self.state.lock(); self.wait_copies(&mut state, block); - state.make_deferred(device, block, length, block_size, layout) + state.make_deferred( + device, block, length, block_size, layout, carried, host_pages, + ) } /// The deferred block at `block`'s pages taken for its cache ([`SurfaceCaches::attach`]), @@ -3919,7 +4002,9 @@ impl Staging

{ /// through the view, which a refused copy by runs makes now (the module doc; counted in /// `counts`); or, for copied pages, through a Shared buffer of the copy's own, which the host's /// CPU fills from the pages now for a copy in, and which the caller keeps until the command -/// buffer completes, then empties into the pages for a copy out ([`Staging::done`]). Where no +/// buffer completes, then empties into the pages for a copy out ([`Staging::done`]). A deferred +/// block's carried pages, its pages not named, are copied in the same way, each run to its own +/// bytes of the cache, the rest left as it is; nothing is copied out of such a cache. Where no /// view or buffer can be made, nothing is copied, as where no encoder is. fn copy( device: &P::Device, @@ -4003,6 +4088,38 @@ fn copy( let into = (way == Way::Out).then(|| Arc::clone(copied)); staged = Some(Staging { buffer, into }); } + if cache.pages.is_none() + && way == Way::In + && let Some(carried) = &cache.carried + && let Some(buffer) = + device.new_buffer_of_length(ON, carried.bytes(), STORAGE_SHARED)? + { + let mut from = 0u64; + for &(at, host, bytes) in &carried.runs { + // SAFETY: `bytes` of the new buffer's Shared bytes from `from`, which no GPU work + // uses yet; the carried pages are the kext's, wired until their block's pages are + // named, which drops them from the cache first. + unsafe { + std::ptr::copy_nonoverlapping( + host.as_ptr::(), + buffer.contents().as_ptr::().add(from.to_usize()), + bytes.to_usize(), + ); + } + blit.encode( + ON, + BlitCmd::CopyBufferToBuffer { + source_buffer: Some(&buffer), + source_offset: from, + destination_buffer: Some(&cache.device), + destination_offset: at, + size: bytes, + }, + )?; + from += bytes; + } + staged = Some(Staging { buffer, into: None }); + } } blit.encode(ON, BlitCmd::EndEncoding)?; Ok(staged) @@ -4017,11 +4134,11 @@ mod tests { RgfxOpHeader, }; use reims_vgpu_graphics::wire::{ - RGFX_CMD_COMMAND_BUFFER, RGFX_CMD_OBJECT_DESTROY, RGFX_CMD_QUEUE_CREATE, RGFX_STATUS_CACHE, - RGFX_STATUS_INVALID_ARGUMENT, RGFX_STATUS_OK, RGFX_STATUS_REFUSED, - RGFX_SURFACE_PAGES_ASK_CACHE, RGFX_SURFACE_PAGES_ATTACH, RGFX_SURFACE_PAGES_CACHED, - RGFX_SURFACE_PAGES_DEFERRED, RgfxMsgHeader, RgfxObjectDestroy, RgfxPageRun, - RgfxQueueCreate, RgfxSurfacePages, RgfxSurfacePagesLayout, + RGFX_CMD_COMMAND_BUFFER, RGFX_CMD_OBJECT_DESTROY, RGFX_CMD_QUEUE_CREATE, + RGFX_PAGE_RUN_ZEROS, RGFX_STATUS_CACHE, RGFX_STATUS_INVALID_ARGUMENT, RGFX_STATUS_OK, + RGFX_STATUS_REFUSED, RGFX_SURFACE_PAGES_ASK_CACHE, RGFX_SURFACE_PAGES_ATTACH, + RGFX_SURFACE_PAGES_CACHED, RGFX_SURFACE_PAGES_DEFERRED, RgfxMsgHeader, RgfxObjectDestroy, + RgfxPageRun, RgfxQueueCreate, RgfxSurfacePages, RgfxSurfacePagesLayout, }; use reims_vgpu_vulkan::core::platform::values::PagesInPlace; use reims_vgpu_vulkan::core::testkit::FakePlatform; @@ -7391,6 +7508,239 @@ mod tests { assert!(!r.dec.caches().has(third)); } + /// A rig of [`deferring`]'s whose device lets a deferral carry its surface's resident pages, + /// as `SURFACE_DEFERRED_CARRY` has it. + fn deferring_carrying(budget: u64) -> Rig { + let r = deferring(budget); + r.dec.set_deferred_carries(); + r + } + + /// The runs of a carrying deferral of a surface of `pages` pages: each surface page of + /// `carried` (the surface's page, a guest RAM page) that page of guest RAM, every other a + /// run of zeros. + fn carrying_runs(r: &Rig, pages: u64, carried: &[(u64, u64)]) -> Vec { + (0..pages) + .map(|at| RgfxPageRun { + address: (carried.iter()) + .find(|&&(page, _)| page == at) + .map_or(RGFX_PAGE_RUN_ZEROS, |&(_, ram)| RAM + ram * r.page), + length: r.page, + }) + .collect() + } + + /// A `SURFACE_PAGES` of the deferred block at `block`, of `pages` pages, carrying `carried` + /// ([`carrying_runs`]), asking with `layout` where given; its answer. + fn defer_carrying( + r: &mut Rig, + block: u64, + pages: u64, + carried: &[(u64, u64)], + layout: Option, + ) -> u32 { + let runs = carrying_runs(r, pages, carried); + let ask = if layout.is_some() { + RGFX_SURFACE_PAGES_ASK_CACHE + } else { + 0 + }; + let flags = RGFX_SURFACE_PAGES_CACHED | RGFX_SURFACE_PAGES_DEFERRED | ask; + r.pages_of_runs(block, 0, &runs, pages * r.page, flags, layout) + } + + /// Each copy in `calls`: its source, source offset, destination, destination offset and size. + fn copies_at(calls: &[Call]) -> Vec<(u64, u64, u64, u64, u64)> { + let id = |a: &Arg| a.as_obj().map_or(0, |o| o.0); + let at = |a: &Arg| a.as_u64().unwrap_or(u64::MAX); + (calls.iter()) + .filter(|c| c.sel == COPY) + .map(|c| { + let a = &c.args; + (id(&a[0]), at(&a[1]), id(&a[2]), at(&a[3]), at(&a[4])) + }) + .collect() + } + + /// A deferral that carries its surface's resident pages is a cache of zeros, with no view, + /// that owes a first fill of those pages alone: nothing is copied at the deferral, and the + /// first use that reads the cache, at the CPU's generation 0, copies each carried page in at + /// its own bytes of the cache, through a Shared buffer the host's CPU filled from the page as + /// the fill was made, the rest of the cache left as its zeroing left it. The cache then holds + /// generation 0, so a use after it fills nothing + #[test] + fn a_carrying_deferral_fills_its_pages_in_before_a_read() { + let mut r = deferring_carrying(8); + let page = r.page; + let mark = r.p.call_log().mark(); + assert_eq!( + defer_carrying(&mut r, BLOCK, 3, &[(1, 4)], None), + RGFX_STATUS_OK + ); + let calls = r.p.call_log().since(mark); + let cache = cache_id(&r); + assert_eq!(calls.iter().filter(|c| c.sel == FILL).count(), 1, "zeroed"); + assert!(copies(&calls).is_empty(), "nothing copied at the deferral"); + assert_eq!(r.dec.views_of_surfaces().len(), (0, 0), "no view"); + let counts = r.dec.caches().counts(); + assert_eq!((counts.deferred, counts.carried), (1, 1)); + assert_eq!(r.dec.caches().unattached(BLOCK), Some(3 * page)); + + // The page as the fill finds it, written after the deferral. + let ram = ram_bytes(&r, 4, page, Some(0x3c)); + let mark = r.p.call_log().mark(); + use_surfaces(&mut r, 1, 0x100, &[(BLOCK, 0, false)]); + let filled = copies_at(&r.p.call_log().since(mark)); + assert_eq!(filled.len(), 1, "the carried page alone: {filled:?}"); + let (staging, from, into, at, size) = filled[0]; + assert_ne!(staging, cache); + assert_eq!( + (from, into, at, size), + (0, cache, page, page), + "at its own bytes" + ); + assert_eq!(buffer_bytes(&r, staging, None), ram, "the page's bytes"); + r.p.world().complete_all(CompleteOn::Caller); + let counts = r.dec.caches().counts(); + assert_eq!((counts.fills, counts.made_fills), (1, 1)); + + let mark = r.p.call_log().mark(); + use_surfaces(&mut r, 2, 0x101, &[(BLOCK, 0, true)]); + assert!( + copies(&r.p.call_log().since(mark)).is_empty(), + "generation 0 held" + ); + } + + /// A carrying deferral that asks with its surface's layout fills its carried page in whole, + /// as every fill copies every byte in, those beside the rows' texels among them. Its pages, + /// named at a CPU lock, are taken for the cache, and the download that follows copies each + /// row's texels alone, as an asked cache's downloads do + #[test] + fn a_carrying_deferral_that_asks_fills_its_pages_in_whole() { + let mut r = deferring_carrying(8); + r.dec.set_unified_memory(true); + r.dec.set_deferred_asks(); + let page = r.page; + let (layout, _) = short_rows(page); + r.p.world() + .answer_fresh_surfaces(Some("the rows end inside a line".into())); + assert_eq!( + defer_carrying(&mut r, BLOCK, 2, &[(0, 1)], Some(layout)), + RGFX_STATUS_OK + ); + let cache = cache_id(&r); + let ram = ram_bytes(&r, 1, page, Some(0x3c)); + let mark = r.p.call_log().mark(); + use_surfaces(&mut r, 1, 0x100, &[(BLOCK, 0, true)]); + let filled = copies_at(&r.p.call_log().since(mark)); + assert_eq!(filled.len(), 1, "{filled:?}"); + let (staging, from, into, at, size) = filled[0]; + assert_eq!( + (from, into, at, size), + (0, cache, 0, page), + "the whole page, the bytes beside its row's texels too" + ); + assert_eq!(buffer_bytes(&r, staging, None), ram); + r.p.world().complete_all(CompleteOn::Caller); + + assert_eq!( + attach(&mut r, BLOCK, 7, &[(1, 2)], 2 * page), + RGFX_STATUS_OK + ); + assert_eq!(r.dec.caches().unattached(BLOCK), None); + let mark = r.p.call_log().mark(); + let done = download(&r, BLOCK); + let out = copies(&r.p.call_log().since(mark)); + let pages = out[0].1; + assert_ne!(pages, cache); + assert_eq!(out, vec![(cache, pages); 2], "each row's texels alone"); + r.p.world().complete_all(CompleteOn::Caller); + assert!(done.load(Ordering::SeqCst)); + } + + /// A carrying deferral whose pages are named before anything read its cache still owes its + /// first fill, now of all its pages: the carried ones go with the naming, and the first use + /// fills the cache from the pages' view, whole + #[test] + fn a_carrying_deferrals_naming_owes_its_first_fill_of_all_its_pages() { + let mut r = deferring_carrying(8); + let page = r.page; + assert_eq!( + defer_carrying(&mut r, BLOCK, 2, &[(0, 1)], None), + RGFX_STATUS_OK + ); + let cache = cache_id(&r); + assert_eq!( + attach(&mut r, BLOCK, 0, &[(1, 2)], 2 * page), + RGFX_STATUS_OK + ); + let mark = r.p.call_log().mark(); + use_surfaces(&mut r, 1, 0x100, &[(BLOCK, 0, false)]); + let filled = copies_at(&r.p.call_log().since(mark)); + assert_eq!(filled.len(), 1, "{filled:?}"); + let (pages, from, into, at, size) = filled[0]; + assert_ne!(pages, cache); + assert_eq!( + (from, into, at, size), + (0, cache, 0, 2 * page), + "the whole surface, from its pages" + ); + } + + /// A carrying deferral whose runs are not whole guest pages summing to its surface's bytes, + /// lie outside the guest's RAM the host maps, or carry no page, is an invalid argument and + /// makes nothing; so is a deferral with runs where the device lets none carry. One past the + /// budget is refused with nothing made, as any deferral is, so the kext names its pages + /// instead + #[test] + fn a_carrying_deferral_carries_whole_pages_of_its_surface_or_is_refused() { + let mut r = deferring_carrying(8); + let page = r.page; + let flags = RGFX_SURFACE_PAGES_CACHED | RGFX_SURFACE_PAGES_DEFERRED; + let run = |address, length| RgfxPageRun { address, length }; + let zeros = RGFX_PAGE_RUN_ZEROS; + let bad = [ + vec![run(RAM + page, page)], + vec![run(RAM + page, page), run(zeros, page), run(zeros, page)], + vec![run(zeros, page), run(zeros, page)], + vec![ + run(RAM + page, page), + run(zeros, page - 0x800), + run(zeros, 0x800), + ], + vec![run(RAM + page + 0x800, page), run(zeros, page)], + vec![run(RAM + 64 * page, page), run(zeros, page)], + vec![run(RAM + page, page), run(zeros, 0), run(zeros, page)], + ]; + for runs in bad { + assert_eq!( + r.pages_of_runs(BLOCK, 0, &runs, 2 * page, flags, None), + RGFX_STATUS_INVALID_ARGUMENT, + "{runs:?}" + ); + } + assert!(!r.dec.caches().has(BLOCK)); + assert_eq!(r.dec.caches().counts().deferred, 0); + + let mut plain = deferring(8); + assert_eq!( + defer_carrying(&mut plain, BLOCK, 2, &[(0, 1)], None), + RGFX_STATUS_INVALID_ARGUMENT, + "a device that lets no deferral carry" + ); + assert!(!plain.dec.caches().has(BLOCK)); + + let mut full = deferring_carrying(1); + assert_eq!( + defer_carrying(&mut full, BLOCK, 2, &[(0, 1)], None), + RGFX_STATUS_REFUSED, + "past the budget" + ); + assert!(!full.dec.caches().has(BLOCK)); + assert_eq!(full.dec.caches().counts().deferred_refused, 1); + } + /// A deferred block the budget has no room for, or whose cache the platform does not make, is /// refused and leaves nothing behind, so the kext's /// `SURFACE_PAGES` of its pages that follows makes its cache as any block's. A release gives diff --git a/crates/reims-vgpu/src/graphics/decode/surface_pages.rs b/crates/reims-vgpu/src/graphics/decode/surface_pages.rs index 43d2f518b..47a04226e 100644 --- a/crates/reims-vgpu/src/graphics/decode/surface_pages.rs +++ b/crates/reims-vgpu/src/graphics/decode/surface_pages.rs @@ -1877,6 +1877,19 @@ pub(super) mod tests { length: n * self.page, }) .collect(); + self.pages_of_runs(block, context, &runs, length, flags, layout) + } + + /// [`Self::pages_laid_out`] of `runs` as the message carries them. + pub(crate) fn pages_of_runs( + &mut self, + block: u64, + context: u32, + runs: &[RgfxPageRun], + length: u64, + flags: u32, + layout: Option, + ) -> u32 { let m = RgfxSurfacePages { header: header(RGFX_CMD_SURFACE_PAGES), context, @@ -1886,7 +1899,7 @@ pub(super) mod tests { runs_offset: block + 0x1000, length, }; - (self.dec).surface_pages_laid_out(&self.device, &m, &runs, flags, layout) + (self.dec).surface_pages_laid_out(&self.device, &m, runs, flags, layout) } /// A `BUFFER_OVER_SURFACE_PAGES` of `length` bytes over the block at `block`. diff --git a/crates/reims-vgpu/src/graphics/tests.rs b/crates/reims-vgpu/src/graphics/tests.rs index 8b6859e28..2d5d54851 100644 --- a/crates/reims-vgpu/src/graphics/tests.rs +++ b/crates/reims-vgpu/src/graphics/tests.rs @@ -7585,8 +7585,10 @@ fn a_release_the_platform_dies_at_is_refused() { /// the CPU's memory, so a device with `SURFACE_PAGES` answers which surfaces need a device cache /// (`SURFACE_CACHE_REFUSED`), keeps none of the rest, defers a fresh one of those that would need /// one whatever its pages (`SURFACE_DEFERRED_ASK`), within a quarter of its GPU's own memory's -/// budget, and has the guest align the surfaces' rows as its GPU's images take them in place (the -/// device info's `linear_row_alignment`), which a device of no surface pages leaves as they were +/// budget, the few pages a process wrote of it without a lock carried in +/// (`SURFACE_DEFERRED_CARRY`, `FEATURES_HI` bit 0), and has the guest align the surfaces' rows as +/// its GPU's images take them in place (the device info's `linear_row_alignment`), which a device +/// of no surface pages leaves as they were #[test] fn a_memory_map_gives_the_surface_pages_feature_on_a_guest_sized_page() { use reims_vgpu_vulkan::core::testkit::GuestRam; @@ -7637,7 +7639,15 @@ fn a_memory_map_gives_the_surface_pages_feature_on_a_guest_sized_page() { | RGFX_FEATURE_MESH_PIPELINES | RGFX_FEATURE_SHARED_EVENT_NAMES ); - assert_eq!(device.mmio_read(RGFX_REG_FEATURES_HI.into()), 0); + let carries = want & RGFX_FEATURE_SURFACE_DEFERRED_ASK != 0; + assert_eq!( + u64::from(device.mmio_read(RGFX_REG_FEATURES_HI.into())) << 32, + if carries { + RGFX_FEATURE_SURFACE_DEFERRED_CARRY + } else { + 0 + } + ); // The fake's GPU shares the CPU's memory: deferred blocks only where they ask. let budget = if want & RGFX_FEATURE_SURFACE_DEFERRED_ASK != 0 { FAKE_OWN_MEMORY / 4 diff --git a/firmware/prebuilt/ReimsGraphics.toml b/firmware/prebuilt/ReimsGraphics.toml index 107dd0721..e473be875 100644 --- a/firmware/prebuilt/ReimsGraphics.toml +++ b/firmware/prebuilt/ReimsGraphics.toml @@ -6,7 +6,7 @@ rom = "749434f87f562895548f60681cd4cd8bf6cc0d0e967ac2fea297674426709161" [sources] -"crates/reims-vgpu-graphics/include/reims_graphics_wire.h" = "775f0edf372a9256500afccb34e76718749452783f465a2cc5aea336bd6ff6cd" +"crates/reims-vgpu-graphics/include/reims_graphics_wire.h" = "24fe325bd743cf3bba7ced1ebadb3a814b4a03f817d4fb58e1f64569bf822d7d" "firmware/Makefile" = "d85b14a3b40cc59b7c07a9e3a3a273c257e81c5674963c70c3c45eb386e653d0" "firmware/src/OptionRom.S" = "b910fe61a880580996a81134fba75900d3a9f48ca266e76fe2d6b8eb5a011eb9" "firmware/src/ReimsGraphics.c" = "554751ff15edbdf69a038d5b2aefd8315b9c54610c090c0890d9c72b13dacdef" diff --git a/guest/prebuilt-arm64e/sources.toml b/guest/prebuilt-arm64e/sources.toml index 12107e2a4..faee1f75b 100644 --- a/guest/prebuilt-arm64e/sources.toml +++ b/guest/prebuilt-arm64e/sources.toml @@ -8,7 +8,7 @@ sdk = "MacOSX27.2.sdk" clang = "Apple clang version 21.0.0 (clang-2100.4.1.1)" [sources] -"crates/reims-vgpu-graphics/include/reims_graphics_wire.h" = "775f0edf372a9256500afccb34e76718749452783f465a2cc5aea336bd6ff6cd" +"crates/reims-vgpu-graphics/include/reims_graphics_wire.h" = "24fe325bd743cf3bba7ced1ebadb3a814b4a03f817d4fb58e1f64569bf822d7d" "guest/Makefile" = "cbf9d33e8d7f44c264ecbb74ce63bb377d69bd952bde3914fc47d084a3031f14" "guest/bundle/Info.plist" = "eb22780c5ed1da8871f709d31818af04c47cdb4c75fbe1c437e192dd1bc0956f" "guest/bundle/Makefile" = "d148138cfccf18cf7b920271b3b006348d1cb3a4f034da8b2b0849bc19fd804a" diff --git a/guest/prebuilt/sources.toml b/guest/prebuilt/sources.toml index 87d2a4395..40660a319 100644 --- a/guest/prebuilt/sources.toml +++ b/guest/prebuilt/sources.toml @@ -8,7 +8,7 @@ sdk = "MacOSX27.2.sdk" clang = "Apple clang version 21.0.0 (clang-2100.4.1.1)" [sources] -"crates/reims-vgpu-graphics/include/reims_graphics_wire.h" = "775f0edf372a9256500afccb34e76718749452783f465a2cc5aea336bd6ff6cd" +"crates/reims-vgpu-graphics/include/reims_graphics_wire.h" = "24fe325bd743cf3bba7ced1ebadb3a814b4a03f817d4fb58e1f64569bf822d7d" "guest/Makefile" = "cbf9d33e8d7f44c264ecbb74ce63bb377d69bd952bde3914fc47d084a3031f14" "guest/bundle/Info.plist" = "eb22780c5ed1da8871f709d31818af04c47cdb4c75fbe1c437e192dd1bc0956f" "guest/bundle/Makefile" = "d148138cfccf18cf7b920271b3b006348d1cb3a4f034da8b2b0849bc19fd804a"