From fe5f08a4b18ec02eb9dcaabb987000692f06bfb6 Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Sun, 8 Nov 2026 05:19:55 -0800 Subject: [PATCH] Host RAM copies take runs at word starts and any end, copied by regions reims's laid-out device cache downloads hand copy_to_host_ram each texel row's bytes (encode_out): runs that start on the device's linear texture alignment, always a 4-byte word, and end at any byte, short of the row's padding, which the guest's CPU owns. merged() refused every such run (Problem::HostRam) and the consumer fell back to a view of the surface's pages: on ANV one mapping per 4 KiB page, 278 of 294 device caches in boot 8267. Now a run starts on a 4-byte word and ends at any byte past it. Copies whose runs are all whole 4 KiB pages keep the pages copy's one dispatch per chunk, unchanged. Any other copy records each run's bytes in a chunk as a region of vkCmdCopyBuffer between the import that holds them (whole pages, imported as before) and the buffer: byte offsets and sizes, so no other byte of the runs' pages is read or written. The regions of one import are one command, at most 1024 each (MOST_REGIONS). A scatter whose runs name a byte twice takes a round of commands per overlap, so no byte is written by two regions of one command (VUID-vkCmdCopyBuffer-pRegions-12482), the later run's bytes left; a buffer over host pages the runs overlap is refused (VUID-vkCmdCopyBuffer-pRegions-00117). Counted in the new WorkCounts::host_ram_copy_regions. Tests: copy.rs's unit tests (word starts, any ends, rows that touch merge, whole_pages, rounds); host_ram_copy_device's runs_inside_pages_move_their_bytes_alone_and_leave_the_rest_of_their_pages (2046-byte rows at a 2048 pitch from 16 bytes into a page, a run ending 2 bytes before a page's end, one across the first chunk's end, one of 3 bytes; pattern sentinels around them; gather into Shared and Private, scatter back; 10 regions, no dispatch; twice-named bytes; the overlapping buffer refused); the whole-page test now also asserts no regions; the refusal test's cases off a word. host_ram_copy_bench gains the laid-out download: a call a row, one call, and the view with a blit a row. Docs: copy.rs module and both calls' API docs, Problem::HostRam, docs/HOWTO.md, the runtime README; docs/REFUSALS.md regenerated and the API signatures re-blessed (the new WorkCounts field). (cherry picked from commit 1ba8aba84076208a1eccaf65dd4993058ed53431) --- crates/metal2vulkan-runtime/README.md | 9 +- crates/metal2vulkan-runtime/src/error.rs | 7 +- .../metal2vulkan-runtime/src/host_ram/copy.rs | 282 +++++++++++++++--- .../src/tests/host_ram_copy_bench.rs | 142 +++++++++ .../src/tests/host_ram_copy_device.rs | 173 ++++++++++- .../metal2vulkan-runtime/src/work_counts.rs | 14 +- crates/metal2vulkan/tests/api_signatures.tsv | 1 + docs/HOWTO.md | 23 +- docs/REFUSALS.md | 4 +- 9 files changed, 596 insertions(+), 59 deletions(-) diff --git a/crates/metal2vulkan-runtime/README.md b/crates/metal2vulkan-runtime/README.md index 76a0a90e1..a0ea58da2 100644 --- a/crates/metal2vulkan-runtime/README.md +++ b/crates/metal2vulkan-runtime/README.md @@ -535,10 +535,11 @@ Calls Metal does not have, for a consumer that emulates a Metal device: source of dma-bufs of its pages: a device whose own check finds held pointer imports taxing every submission (RADV), or that refuses the RAM's pointer, imports each chunk as one dma-buf. - `BlitCommandEncoder::copy_from_host_ram` and `copy_to_host_ram`: runs of a used guest RAM - (4 KiB-aligned host addresses, whole 4 KiB pages) gathered into consecutive bytes of a buffer, - or scattered back, by one compute dispatch per chunk the runs touch, from the device's chunk - imports, with no view of the pages and no import of their own; ordered and seen as a copy - between the buffer and a no-copy buffer over the pages + (host addresses from a 4-byte word to any byte) gathered into consecutive bytes of a buffer, + or scattered back, from the device's chunk imports: whole 4 KiB pages by one compute dispatch + per chunk the runs touch, other runs by a copy region per run in a chunk, which touches no + other byte of their pages; with no view of the pages and no import of their own; ordered and + seen as a copy between the buffer and a no-copy buffer over the pages ([HOWTO](../../docs/HOWTO.md#guest-ram-imported-whole)). - `HostBytes`: bytes at a pointer that may change while a call reads them (a guest's memory), which `Texture::replace_region_with_host_bytes` and `BufferContents::CopyHost` copy once through diff --git a/crates/metal2vulkan-runtime/src/error.rs b/crates/metal2vulkan-runtime/src/error.rs index d451adf29..586577cb8 100644 --- a/crates/metal2vulkan-runtime/src/error.rs +++ b/crates/metal2vulkan-runtime/src/error.rs @@ -564,9 +564,10 @@ pub enum Problem { /// RAM: no range, or a range that is empty, starts or ends off the host page, runs past the /// address space's end or overlaps another (of the same RAM, or of another RAM the device /// uses). For an answer: runs that are not whole host pages or do not cover exactly the pages - /// asked for. For a copy: a RAM the device does not use, a run that is empty, starts or ends - /// off 4 KiB or lies outside the RAM's ranges, or bytes of the buffer that start off a 4-byte - /// word of its memory. + /// asked for. For a copy: a RAM the device does not use, a run that is empty, starts off a + /// 4-byte word or lies outside the RAM's ranges, bytes of the buffer that start off a 4-byte + /// word of its memory, or, of runs not all whole 4 KiB pages, a buffer over host pages the + /// runs' bytes overlap. /// /// Basis: ours (a runtime extension's own contract: Metal imports no guest RAM). HostRam, diff --git a/crates/metal2vulkan-runtime/src/host_ram/copy.rs b/crates/metal2vulkan-runtime/src/host_ram/copy.rs index 17d166a1f..0361df892 100644 --- a/crates/metal2vulkan-runtime/src/host_ram/copy.rs +++ b/crates/metal2vulkan-runtime/src/host_ram/copy.rs @@ -5,10 +5,12 @@ //! scatter of guest pages copies it with no view of the pages and, where the RAM's chunks are //! imported already (`host_ram.rs`), no import of its own. //! -//! - **The runs** are host addresses in the RAM's ranges, each 4 KiB-aligned and whole 4 KiB -//! pages ([`PAGE`]), in the order the buffer's bytes take them. A run that starts where the one -//! before it ended is one run with it ([`merged`]); a run that starts before (a descending -//! neighbour) stays apart. Each run is cut where a chunk ends ([`pieces`]), and each chunk the +//! - **The runs** are host addresses in the RAM's ranges, each starting on a 4-byte word +//! ([`WORD`]) and ending at any byte past it, in the order the buffer's bytes take them: a +//! surface's whole pages, or each of its rows' texels alone, whose last bytes the guest's CPU +//! may own past them. A run that starts where the one before it ended is one run with it +//! ([`merged`]); a run that starts before (a descending neighbour) stays apart. Each run is cut +//! where a chunk ends ([`pieces`]), and each chunk the //! pieces touch is taken as an object over it takes it (`RamOnDevice::for_copy`): its import //! where it is made; else, where an object over it would be bound in its own run's import //! (`RamOnDevice::for_object`), an import of each of the copy's ranges of it alone (those less @@ -23,14 +25,24 @@ //! imports the chunk, or waits for its import //! under way, which is the chunk's one import (`Device::import_chunk`), made and counted as an //! object's is. -//! - **One dispatch per chunk.** The pieces become a table of 4 KiB pages, each the chunk's page -//! and the buffer's ([`plan`]), uploaded into the command buffer's arena, and the pages of each -//! chunk one dispatch of `bind::pages_copy_module` (a gather or a scatter) over the chunk's -//! whole `VkBuffer`, the buffer's bytes and the table: as many dispatches as chunks the runs -//! touch, never one region per run. A copy whose bytes pass the device's storage range, or whose -//! pages of one chunk pass its workgroup count, takes more (each window of the buffer's bytes, -//! each part of the pages). Counted: `WorkCounts::host_ram_copies`, `host_ram_copy_bytes` and -//! `host_ram_copy_dispatches`. +//! - **Whole pages: one dispatch per chunk.** Where every run is whole 4 KiB pages from a +//! multiple of them ([`whole_pages`]), the pieces become a table of 4 KiB pages, each the +//! chunk's page and the buffer's ([`plan`]), uploaded into the command buffer's arena, and the +//! pages of each chunk one dispatch of `bind::pages_copy_module` (a gather or a scatter) over +//! the chunk's whole `VkBuffer`, the buffer's bytes and the table: as many dispatches as chunks +//! the runs touch, never one region per run. A copy whose bytes pass the device's storage +//! range, or whose pages of one chunk pass its workgroup count, takes more (each window of the +//! buffer's bytes, each part of the pages). Counted: `WorkCounts::host_ram_copies`, +//! `host_ram_copy_bytes` and `host_ram_copy_dispatches`. +//! - **Other runs: regions.** Where a run starts or ends inside a page, each piece is a region of +//! `vkCmdCopyBuffer` between its import (the chunk's, or the pages it covers imported alone, as +//! above: whole pages, imported as now) and the buffer, which takes byte offsets and sizes: the +//! copy reads or writes the runs' bytes and no other byte of their pages, a row's padding and +//! the pages around a surface left as they were. The regions of one import are one command, at +//! most [`MOST_REGIONS`] each. A scatter whose runs name a byte twice takes a round of commands +//! for each ([`rounds`]), no byte written by two regions of one command; and a buffer over host +//! pages the runs' bytes overlap is refused, no region reading and writing one byte. Counted: +//! `WorkCounts::host_ram_copies`, `host_ram_copy_bytes` and `host_ram_copy_regions`. //! - **Ordered as a copy between the buffer and a no-copy buffer over the pages.** Each chunk a //! copy uses has a key of its own, its uses' bytes those of the chunk, which hazard tracking //! places over the chunk's host bytes (`host_aliases.rs`'s placements), as it places every @@ -48,16 +60,17 @@ //! (`HostRam::with_dirty_pages`), the chunk's key is the log's, so a scatter's writes are //! reported to it as the GPU's in place (`dirty_pages.rs`): every copy of those pages (a copied //! host buffer's, a shadow's, whose logs are the RAM's where their runs are) copies them in. -//! - **What is refused** ([`Problem::HostRam`]): a run that is empty, starts or ends off 4 KiB, or -//! lies outside the RAM's ranges; a RAM the device does not use; bytes of the buffer that start -//! off a 4-byte word of its memory. A device without `VK_EXT_external_memory_host` uses no RAM +//! - **What is refused** ([`Problem::HostRam`]): a run that is empty, starts off a 4-byte word, +//! or lies outside the RAM's ranges; a RAM the device does not use; bytes of the buffer that +//! start off a 4-byte word of its memory; a copy of runs not all whole pages with a buffer over +//! host pages the runs' bytes overlap. A device without `VK_EXT_external_memory_host` uses no RAM //! (`Unsupported`, naming it); the buffer's bytes past its length are a range past it //! ([`Problem::RangeOutOfBounds`]). A chunk the device cannot import fails the copy with the //! import's error. use super::{HostRam, RamChunk, RamOnDevice}; -use crate::blit::{bytes_in, reads_by, writes_by, BlitCommandEncoder}; -use crate::buffer::Buffer; +use crate::blit::{bytes_in, reads_by, writes_by, BlitCommandEncoder, TRANSFER}; +use crate::buffer::{Buffer, CallerMemory}; use crate::dirty_pages::PagesLog; use crate::error::{invalid, Error, Problem}; use crate::owned_kernel::{OwnedBinds, OwnedModule, OwnedSet}; @@ -148,13 +161,20 @@ impl RamOnDevice { } } -/// `runs`, each whole [`PAGE`]s from a multiple of it, with each that starts where the one before -/// it ended joined to it; `None` where a run is empty or off the page. +/// The unit a run starts on: a 4-byte word of host address. Its end may be any byte. +pub(crate) const WORD: u64 = 4; + +/// The most regions of one `vkCmdCopyBuffer` of a copy by regions (`record_host_ram_copy`): a +/// copy of more takes more commands, so no one command's array grows with the runs (a laid-out +/// 4K surface's rows are 2160 runs a plane). +pub(crate) const MOST_REGIONS: usize = 1024; + +/// `runs`, each starting on a [`WORD`] and holding at least a byte, with each that starts where +/// the one before it ended joined to it; `None` where a run is empty or starts off a word. pub(crate) fn merged(runs: &[Range]) -> Option>> { let mut out: Vec> = Vec::with_capacity(runs.len()); for run in runs { - let whole = run.start.is_multiple_of(PAGE) && run.end.is_multiple_of(PAGE); - if run.start >= run.end || !whole { + if run.start >= run.end || !run.start.is_multiple_of(WORD) { return None; } match out.last_mut() { @@ -165,6 +185,38 @@ pub(crate) fn merged(runs: &[Range]) -> Option>> { Some(out) } +/// Whether every one of `runs` is whole [`PAGE`]s from a multiple of it: a copy of such runs is +/// one dispatch of the pages copy per chunk; any other, regions of `vkCmdCopyBuffer`. +pub(crate) fn whole_pages(runs: &[Range]) -> bool { + (runs.iter()).all(|run| run.start.is_multiple_of(PAGE) && run.end.is_multiple_of(PAGE)) +} + +/// The pieces of a copy by regions as rounds of `vkCmdCopyBuffer`s: each round the pieces (by +/// index, in order) whose host bytes no other piece of the round's touches, so that no byte is +/// written by two regions of one command; a later piece over bytes an earlier one of the round +/// touched starts the next round, ordered after it as a command that writes them again is. Only a +/// scatter's runs that name a byte twice take more than one. +pub(crate) fn rounds(host: &[Range]) -> Vec> { + let mut rounds: Vec> = Vec::new(); + // The round's host bytes so far, apart, by start. + let mut taken: BTreeMap = BTreeMap::new(); + for (index, range) in host.iter().enumerate() { + // Of the round's ranges, the one that starts last before this one ends is the only one + // that may overlap it: those before it end before it starts. + let before = taken.range(..range.end).next_back(); + let overlaps = before.is_some_and(|(_, &end)| end > range.start); + match rounds.last_mut() { + Some(round) if !overlaps => round.push(index), + _ => { + rounds.push(vec![index]); + taken.clear(); + } + } + taken.insert(range.start, range.end); + } + rounds +} + /// A run's bytes inside one chunk: `length` bytes from byte `offset` of chunk `slot`, at byte /// `at` of the copy. #[derive(Clone, Copy, Debug, PartialEq, Eq)] @@ -332,10 +384,13 @@ pub(crate) struct HostRamCall { copy: PagesCopy, /// The device's use of the RAM; `None` where it uses none such. ram: Option>, - /// The runs, merged where they are whole pages ([`merged`]), else as given. + /// The runs, merged where each starts on a word and holds a byte ([`merged`]), else as given. runs: Box<[Range]>, - /// Whether the runs are whole pages. - whole: bool, + /// Whether the runs each start on a word and hold a byte (merged). + checked: bool, + /// Whether they are whole pages besides ([`whole_pages`]): copied by the pages copy's + /// dispatches, else by regions of `vkCmdCopyBuffer`. + pages: bool, buffer: Buffer, offset: u64, } @@ -351,15 +406,17 @@ impl HostRamCall { ) -> HostRamCall { let ram = (device.shared().host_ram.used().into_iter()) .find(|used| std::ptr::eq(Arc::as_ptr(&used.ram), ram)); - let (runs, whole) = match merged(runs) { + let (runs, checked) = match merged(runs) { Some(merged) => (merged.into_boxed_slice(), true), None => (runs.into(), false), }; + let pages = checked && whole_pages(&runs); HostRamCall { copy, ram, runs, - whole, + checked, + pages, buffer: buffer.clone(), offset, } @@ -376,8 +433,10 @@ impl BlitCommandEncoder { /// `destination_offset`, in the order given, by the GPU, from the device's imports of the /// RAM's chunks: a runtime extension, Metal has none (a program copies from a no-copy buffer /// over the pages, `copy_from_buffer_to_buffer`). The runs are host addresses inside the RAM's - /// ranges, each starting on 4 KiB and whole 4 KiB pages. Adjacent ascending runs are one; - /// the pages of each chunk the runs touch are one dispatch (the module docs of + /// ranges, each starting on a 4-byte word and ending at any byte past it. Adjacent ascending + /// runs are one; where every run is whole 4 KiB pages from a multiple of 4 KiB, the pages of + /// each chunk the runs touch are one dispatch, and otherwise each run's bytes in a chunk are + /// one region of a copy, which reads no other byte of their pages (the module docs of /// `host_ram/copy.rs`), with no view of the pages and no import of its own. It is ordered and /// seen as a copy from a no-copy buffer over the pages into `destination` would be: the CPU's /// writes to the pages before the commit are seen, hazard tracking orders it against every @@ -393,8 +452,9 @@ impl BlitCommandEncoder { /// /// Refused: a device without `VK_EXT_external_memory_host` (`Unsupported`, naming it); a RAM /// the device does not use ([`Device::use_host_ram`](crate::Device::use_host_ram)), a run that - /// is empty, starts or ends off 4 KiB or lies outside the RAM's ranges, or bytes of - /// `destination` that start off a 4-byte word of its memory (`Invalid`, `Problem::HostRam`); + /// is empty, starts off a 4-byte word or lies outside the RAM's ranges, bytes of + /// `destination` that start off a 4-byte word of its memory, or, of runs not all whole pages, + /// a `destination` over host pages the runs' bytes overlap (`Invalid`, `Problem::HostRam`); /// bytes past `destination`'s length or a buffer of another device (`Invalid`); and a chunk /// the device cannot import (the import's error). A caller falls back to its own copy. pub fn copy_from_host_ram( @@ -421,8 +481,10 @@ impl BlitCommandEncoder { /// would be: hazard tracking orders it against every other use of the same host bytes, the /// GPU's writes are in the pages once the command buffer completed, and where the RAM came /// with a log of its pages' writes (`HostRam::with_dirty_pages`) they are reported to it as - /// the GPU's writes in place, so every copy the runtime keeps of those pages takes them. - /// Runs that name one page twice leave it as either wrote it. + /// the GPU's writes in place, so every copy the runtime keeps of those pages takes them. A + /// run that starts or ends inside a page writes its bytes alone: the rest of the page keeps + /// what it held. Runs that name one page twice leave it as either wrote it, where they are + /// whole pages; other runs that name a byte twice leave it as the later wrote it. /// /// Refused as [`copy_from_host_ram`](Self::copy_from_host_ram), `source` in place of the /// destination. @@ -468,7 +530,7 @@ impl BlitCommandEncoder { let ram = (call.ram.as_ref()) .filter(|ram| shared.host_ram.uses(ram)) .ok_or_else(refused)?; - if !call.whole { + if !call.checked { return Err(refused()); } let spans = ram.spans(); @@ -530,6 +592,10 @@ impl BlitCommandEncoder { let in_imports = in_imports.ok_or_else(|| { crate::error::internal("a host RAM copy's piece outside the imports made for it") })?; + if !call.pages { + let pieces = (&pieces[..], &in_imports[..]); + return self.record_regions(call, ram, pieces, imports, (raw, at, bytes)); + } let (table, dispatches) = plan(&in_imports, window_pages, most_groups).ok_or_else(refused)?; let kernel = device.owned_kernel(OwnedModule::PagesCopy(call.copy))?; @@ -626,6 +692,118 @@ impl BlitCommandEncoder { } Ok(()) } + + /// Record `call`, whose runs are not all whole pages, as regions of `vkCmdCopyBuffer` between + /// `imports` and the buffer's bytes `bytes`, from byte `at` of its `raw` (the module docs): + /// `pieces` as cut by the RAM's chunks and as they lie in the imports, one region each, the + /// regions of one import at most [`MOST_REGIONS`] a command. A scatter whose pieces write a + /// byte twice takes a round of commands for each ([`rounds`]), each ordered after the one + /// before as a later write of the same bytes is. Refused (`Problem::HostRam`) where the buffer + /// lies over host pages the pieces' bytes overlap: no region may read and write one byte. + fn record_regions( + &mut self, + call: &HostRamCall, + ram: &RamOnDevice, + (pieces, in_imports): (&[Piece], &[Piece]), + imports: Vec>, + (raw, at, bytes): (vk::Buffer, u64, Range), + ) -> Result<(), Error> { + let call_name = match call.copy { + PagesCopy::Gather => "copy_from_host_ram", + PagesCopy::Scatter => "copy_to_host_ram", + }; + let device = self.core().device().clone(); + // Each piece's host bytes. + let host: Vec> = (in_imports.iter()) + .map(|piece| { + let start = imports[piece.slot].span.start + piece.offset; + start..start + piece.length + }) + .collect(); + if call.buffer.caller_memory() == Some(CallerMemory::HostPages) { + if let Some(contents) = call.buffer.contents() { + let start = contents.as_ptr().addr() as u64 + call.offset; + let own = start..start + (bytes.end - bytes.start); + if (host.iter()).any(|range| range.start < own.end && own.start < range.end) { + return Err(invalid(call_name, Problem::HostRam)); + } + } + } + let read = Access::new(TRANSFER, vk::AccessFlags2::TRANSFER_READ); + let write = Access::new(TRANSFER, vk::AccessFlags2::TRANSFER_WRITE); + // A gather writes the buffer's bytes, each once; a scatter the pieces' host bytes. + let by_round = match call.copy { + PagesCopy::Gather => vec![(0..pieces.len()).collect()], + PagesCopy::Scatter => rounds(&host), + }; + let mut regions_recorded = 0u64; + for round in &by_round { + // The round's regions, by import, in order. + let mut by_import: BTreeMap> = BTreeMap::new(); + for &index in round { + let piece = &in_imports[index]; + let (import_at, buffer_at) = (piece.offset, at + piece.at); + let (src_offset, dst_offset) = match call.copy { + PagesCopy::Gather => (import_at, buffer_at), + PagesCopy::Scatter => (buffer_at, import_at), + }; + let region = vk::BufferCopy { + src_offset, + dst_offset, + size: piece.length, + }; + by_import.entry(piece.slot).or_default().push(region); + } + // The round's uses: the chunks' bytes it reads or writes, by their keys, and the + // buffer's. + let round_pieces: Vec = round.iter().map(|&index| pieces[index]).collect(); + let mut uses = Vec::new(); + for (slot, ranges) in chunk_ranges(&round_pieces, MOST_RANGES) { + let key = ram.chunk_key(&device, slot); + for range in ranges { + let range = UseRange::Bytes(range); + uses.push(match call.copy { + PagesCopy::Gather => Use::reads(key, true, range, read), + PagesCopy::Scatter => Use::writes(key, true, range, write), + }); + } + } + uses.push(match call.copy { + PagesCopy::Gather => writes_by(&call.buffer, bytes.clone(), write), + PagesCopy::Scatter => reads_by(&call.buffer, bytes.clone(), read), + }); + regions_recorded += round.len() as u64; + let imports = &imports; + self.command_in(TRANSFER, &uses, |functions, command_buffer| { + for (slot, regions) in &by_import { + let chunk = imports[*slot].raw; + let (source, destination) = match call.copy { + PagesCopy::Gather => (chunk, raw), + PagesCopy::Scatter => (raw, chunk), + }; + for part in regions.chunks(MOST_REGIONS) { + // SAFETY: the encoder's command buffer is recording, outside a render + // pass; the imports and the buffer are live until it completes (held + // below and by `blit_at`); each region lies inside its import (`pieces`, + // the imports made over them) and inside the buffer's bytes (`bytes_in`), + // and no byte is written by two regions of one command (`rounds`) nor + // read and written by one (the buffer apart from the pieces' bytes). + unsafe { + functions.cmd_copy_buffer(command_buffer, source, destination, part) + }; + } + } + })?; + } + let total = bytes.end - bytes.start; + device.shared().counts.host_ram_copied(total, 0); + device + .shared() + .counts + .host_ram_copy_regions(regions_recorded); + self.core().retain(imports); + Ok(()) + } } #[cfg(test)] @@ -665,15 +843,47 @@ mod tests { } #[test] - fn runs_off_the_page_or_empty_are_refused() { + fn runs_off_a_word_or_empty_are_refused_and_ends_take_any_byte() { assert_eq!(merged(&one(PAGE..PAGE)), None, "empty"); - assert_eq!(merged(&one(1..PAGE + 1)), None, "off the page"); - assert_eq!(merged(&one(0..PAGE + 1)), None, "not whole pages"); + assert_eq!(merged(&one(1..PAGE + 1)), None, "a start off a word"); + assert_eq!(merged(&one(PAGE + 2..PAGE + 8)), None, "a start off a word"); let backwards = Range { start: 2 * PAGE, end: PAGE, }; assert_eq!(merged(&one(backwards)), None, "backwards"); + // A start on a word, an end at any byte: an 854-texel row of R8 ends 2 bytes past one. + assert_eq!(merged(&one(16..16 + 854)), Some(one(16..16 + 854))); + assert_eq!(merged(&one(0..PAGE + 1)), Some(one(0..PAGE + 1))); + // Rows that touch are one run; rows apart stay apart. + let rows = [ + 2048 * 3..2048 * 3 + 2046, + 2048 * 4..2048 * 4 + 2046, + 2048 * 4 + 2046..2048 * 5, + ]; + assert_eq!( + merged(&rows), + Some(vec![2048 * 3..2048 * 3 + 2046, 2048 * 4..2048 * 5]) + ); + assert!(whole_pages(&[0..PAGE, 3 * PAGE..5 * PAGE])); + assert!(!whole_pages(&[0..PAGE, 3 * PAGE..5 * PAGE - 2])); + assert!(!whole_pages(&[16..PAGE])); + assert!(whole_pages(&[])); + } + + /// A scatter's pieces take one round where no byte is named twice, and a new round at each + /// piece over bytes the round took. + #[test] + fn a_round_of_regions_writes_no_byte_twice() { + let rows: Vec> = (0..4).map(|y| y * 2048..y * 2048 + 2046).collect(); + assert_eq!(rounds(&rows), vec![vec![0, 1, 2, 3]]); + // The second names the first's last two bytes; the fourth the third's whole, and the + // first's (an earlier round's) too. + let twice = [16..100, 96..200, 300..400, 0..400]; + assert_eq!(rounds(&twice), vec![vec![0], vec![1, 2], vec![3]]); + // Touching ranges share no byte. + assert_eq!(rounds(&[0..4, 4..8, 8..12]), vec![vec![0, 1, 2]]); + assert_eq!(rounds(&[]), Vec::>::new()); } #[test] diff --git a/crates/metal2vulkan-runtime/src/tests/host_ram_copy_bench.rs b/crates/metal2vulkan-runtime/src/tests/host_ram_copy_bench.rs index 77f57e59c..1654aeec5 100644 --- a/crates/metal2vulkan-runtime/src/tests/host_ram_copy_bench.rs +++ b/crates/metal2vulkan-runtime/src/tests/host_ram_copy_bench.rs @@ -5,6 +5,12 @@ //! buffer made with `HostMemory::with_runs`, so imported by its pointer) and a blit between it and //! the buffer. //! +//! Then the same surface's pages as rows of [`ROW_BYTES`] bytes of texels at a [`PITCH`]-byte +//! pitch, downloaded as a consumer downloads a laid-out device cache: each row's texels alone, +//! by `copy_to_host_ram` of the surface's runs cut to the row, one call a row (and once as one +//! call of every row's runs), each run ending inside a page, so copied by regions; and by the +//! view, a blit a row from the buffer into it (`LAID-OUT` lines). +//! //! The surface: a 3840x2054 `BGRA8Unorm` one, 31,549,440 bytes, as whole runs of `UNIT` bytes //! (7,703 of 4 KiB) scattered over a 1 GiB RAM: about 75% of them the page below the one before //! (a descending neighbour, a run of its own), 20% the page above it (merged into it), and the @@ -31,6 +37,10 @@ const WAIT: Duration = Duration::from_secs(60); const SURFACE: u64 = 3840 * 2054 * 4; const RAM_BYTES: u64 = 1 << 30; const ITERATIONS: usize = 40; +/// The laid-out surface's pitch and its rows' bytes of texels: 2054 rows of a 7677-texel-wide +/// surface of 2-byte texels, each row ending 2 bytes past a word, 6 bytes short of the pitch. +const PITCH: u64 = 3840 * 4; +const ROW_BYTES: u64 = PITCH - 6; /// A small deterministic generator (xorshift). struct Rng(u64); @@ -323,6 +333,135 @@ fn view_copies( ); } +/// The runs of `runs` (the surface's, in its order) that hold its bytes `span`, cut to them: a +/// row's texels as the consumer hands them to `copy_to_host_ram`. +fn runs_of(runs: &[Range], span: &Range) -> Vec> { + let mut out = Vec::new(); + let mut at = 0u64; + for run in runs { + let length = run.end - run.start; + let (from, to) = (span.start.max(at), span.end.min(at + length)); + if from < to { + out.push(run.start + (from - at)..run.start + (to - at)); + } + at += length; + if at >= span.end { + break; + } + } + out +} + +/// The laid-out download (the module docs) of the surface whose pages are `runs`, from `cache` +/// into the RAM: by runs a call a row, by runs in one call, and by the view a blit a row. +fn laid_out_copies( + device: &Device, + queue: &CommandQueue, + (ram, fd, start): (&HostRam, i32, u64), + runs: &[Range], + cache: &Buffer, +) { + let rows = SURFACE / PITCH; + let spans: Vec> = (0..rows) + .map(|row| row * PITCH..row * PITCH + ROW_BYTES) + .collect(); + let row_runs: Vec>> = spans.iter().map(|span| runs_of(runs, span)).collect(); + let every: Vec> = row_runs.iter().flatten().cloned().collect(); + let what = format!( + "LAID-OUT {rows} rows of {ROW_BYTES} at {PITCH} ({} runs)", + every.len() + ); + // Warm: every chunk imported, the paths' first use made. + let mut warm = Figures::default(); + timed(queue, &mut warm, |encoder| { + for (span, runs) in spans.iter().zip(&row_runs) { + encoder + .copy_to_host_ram(cache, span.start, ram, runs) + .unwrap(); + } + Duration::ZERO + }); + let (mut by_row, mut at_once) = (Figures::default(), Figures::default()); + let before = device.work_counts(); + for _ in 0..ITERATIONS { + timed(queue, &mut by_row, |encoder| { + let started = thread_cpu(); + for (span, runs) in spans.iter().zip(&row_runs) { + encoder + .copy_to_host_ram(cache, span.start, ram, runs) + .unwrap(); + } + thread_cpu() - started + }); + } + let after = device.work_counts(); + let calls = after.host_ram_copies - before.host_ram_copies; + by_row.print(&format!("{what} copy_to_host_ram a call a row")); + eprintln!( + "HOST-RAM-COPY-BENCH {what}: {calls} calls, {} regions and {} dispatches a download", + (after.host_ram_copy_regions - before.host_ram_copy_regions) / ITERATIONS as u64, + (after.host_ram_copy_dispatches - before.host_ram_copy_dispatches) / ITERATIONS as u64, + ); + // The rows' runs in one call, from a buffer of the rows' texels alone. + let texels = device + .new_buffer(rows * ROW_BYTES, ResourceOptions::private()) + .unwrap(); + for _ in 0..ITERATIONS { + timed(queue, &mut at_once, |encoder| { + let started = thread_cpu(); + encoder.copy_to_host_ram(&texels, 0, ram, &every).unwrap(); + thread_cpu() - started + }); + } + at_once.print(&format!("{what} copy_to_host_ram in one call")); + // The view: per download, a view of the surface's pages, a no-copy buffer over it, a blit a + // row from the cache into it, both let go of. + let length: u64 = runs.iter().map(|run| run.end - run.start).sum(); + let mut figures = Figures::default(); + let (mut maps, mut makes, mut releases) = (Vec::new(), Vec::new(), Vec::new()); + for _ in 0..ITERATIONS { + let started = Instant::now(); + let view = map_view(fd, start, runs); + maps.push(started.elapsed()); + let pointer = NonNull::new(view as *mut u8).unwrap(); + // SAFETY: the view's bytes, mapped until the bench unmaps them below, after the buffer + // went and its command buffer completed. + let memory = unsafe { HostMemory::new(pointer) }.with_runs(view_resolver(view, runs)); + let started = Instant::now(); + // SAFETY: as above. + let buffer = unsafe { + device.new_buffer_with_bytes_no_copy(memory, length, ResourceOptions::default(), None) + } + .unwrap(); + makes.push(started.elapsed()); + timed(queue, &mut figures, |encoder| { + let started = thread_cpu(); + for span in &spans { + let size = span.end - span.start; + encoder + .copy_from_buffer_to_buffer(cache, span.start, &buffer, span.start, size) + .unwrap(); + } + thread_cpu() - started + }); + let started = Instant::now(); + drop(buffer); + // SAFETY: the bench's view, which nothing uses any more. + unsafe { libc::munmap(view as *mut libc::c_void, length as usize) }; + device.host_range_remapped(pointer, length); + releases.push(started.elapsed()); + } + figures.print(&format!("{what} view copy_from_buffer_to_buffer a row")); + eprintln!( + "HOST-RAM-COPY-BENCH {what} view: {} mappings, map {:.3} ms, buffer made {:.3} ms, \ + released {:.3} ms", + runs.len(), + median(&mut maps), + median(&mut makes), + median(&mut releases), + ); +} + #[test] #[ignore = "a release benchmark: run with --ignored --nocapture"] fn host_ram_copy_bench() { @@ -383,6 +522,9 @@ fn host_ram_copy_bench() { if unit == host_page { view_copies(&device, &queue, (fd, start), &runs, &cache, &what); } + if unit == 4096 { + laid_out_copies(&device, &queue, (&ram, fd, start), &runs, &cache); + } } // An empty command buffer's commit to completion, for scale. let mut empty = Figures::default(); diff --git a/crates/metal2vulkan-runtime/src/tests/host_ram_copy_device.rs b/crates/metal2vulkan-runtime/src/tests/host_ram_copy_device.rs index 0323e1f90..2884786d8 100644 --- a/crates/metal2vulkan-runtime/src/tests/host_ram_copy_device.rs +++ b/crates/metal2vulkan-runtime/src/tests/host_ram_copy_device.rs @@ -20,8 +20,14 @@ //! read-only view of them: a copy, never bound in the chunk, whose commit after the CPU wrote //! one page compares that page alone (the RAM's log, `copied_copy_in_bytes_skipped`); the GPU's //! writes to it are dropped. -//! - **Refused**: runs off the page, empty or outside the RAM, a RAM the device does not use (or -//! no longer does), bytes past the buffer and an offset off a word; no runs copy nothing. +//! - **Runs inside pages**: rows cut to their texels (2046 bytes at a 2048-byte pitch, from 16 +//! bytes into a page), a run ending 2 bytes before a page's end, one across the first chunk's +//! end and one of 3 bytes, gathered into a `Shared` and a `Private` buffer and scattered back, +//! one region for each run's bytes in a chunk and no dispatch: every other byte of their pages +//! (each a pattern) stays. A scatter whose runs name bytes twice leaves the later's; a buffer +//! over host pages the runs' bytes overlap is refused. +//! - **Refused**: runs off a word, empty or outside the RAM, a RAM the device does not use (or no +//! longer does), bytes past the buffer and an offset off a word; no runs copy nothing. //! - **A chunk taken as an object takes it**, on a device whose thread imports ahead: a gather //! from the RAM's first chunk finds it imported ahead and opens the full window past it, and a //! scatter into a chunk past the window imports its page alone while the thread imports the @@ -306,6 +312,11 @@ fn a_gather_and_a_scatter_move_every_runs_bytes_in_order() { 2, "one dispatch per chunk the runs touch" ); + assert_eq!( + after.host_ram_copy_regions - before.host_ram_copy_regions, + 0, + "whole pages take no regions" + ); assert_eq!( after.host_ram_chunks_imported - before.host_ram_chunks_imported, 2, @@ -349,6 +360,158 @@ fn a_gather_and_a_scatter_move_every_runs_bytes_in_order() { ); } +/// A surface's rows cut to their texels, 2046 bytes at a 2048-byte pitch from 16 bytes into a +/// page (so that one crosses a page's end), a run ending 2 bytes before a page's end, one across +/// the first chunk's end and one of 3 bytes in the second chunk. +fn runs_inside_pages(start: u64) -> Vec> { + let mut runs: Vec> = (0..6) + .map(|y| { + let row = start + 20 * PAGE + 16 + 2048 * y; + row..row + 2046 + }) + .collect(); + runs.push(start + 30 * PAGE + 100..start + 31 * PAGE - 2); + runs.push(start + (SECOND - 1) * PAGE + 8..start + SECOND * PAGE + 1001); + runs.push(start + (SECOND + 5) * PAGE + 4..start + (SECOND + 5) * PAGE + 7); + runs +} + +#[test] +fn runs_inside_pages_move_their_bytes_alone_and_leave_the_rest_of_their_pages() { + let Some(device) = device() else { + return; + }; + let (ram, start) = new_ram(None); + let _used = device.use_host_ram(&ram).unwrap(); + let runs = runs_inside_pages(start); + // Every byte of the pages the runs touch holds a pattern: those around the runs are the + // sentinels, a row's padding and the bytes before and after the surface. + let touched = [ + start + 20 * PAGE..start + 24 * PAGE, + start + 30 * PAGE..start + 31 * PAGE, + start + (SECOND - 1) * PAGE..start + (SECOND + 1) * PAGE, + start + (SECOND + 5) * PAGE..start + (SECOND + 6) * PAGE, + ]; + for pages in &touched { + write_pattern(pages.clone(), 7); + } + let total: u64 = runs.iter().map(|run| run.end - run.start).sum(); + // A gather into a `Shared` buffer from byte 260: its bytes before and after stay. + let before = device.work_counts(); + let out = shared(&device, 260 + total + 64, |_| 0xee); + blit(&device, |encoder| { + encoder.copy_from_host_ram(&ram, &runs, &out, 260).unwrap(); + }); + let got = contents(&out); + assert!(got[..260].iter().all(|&byte| byte == 0xee)); + assert!(got[260 + total as usize..].iter().all(|&byte| byte == 0xee)); + assert!( + got[260..260 + total as usize] == gathered(&runs)[..], + "the gathered bytes" + ); + let after = device.work_counts(); + assert_eq!(after.host_ram_copies - before.host_ram_copies, 1); + assert_eq!( + after.host_ram_copy_bytes - before.host_ram_copy_bytes, + total + ); + assert_eq!( + after.host_ram_copy_regions - before.host_ram_copy_regions, + 10, + "a region for each run's bytes in a chunk: nine runs, one across the chunks' end" + ); + assert_eq!( + after.host_ram_copy_dispatches - before.host_ram_copy_dispatches, + 0, + "no pages copy" + ); + // Into a `Private` buffer, read back by a blit. + let private = device + .new_buffer(total, ResourceOptions::private()) + .unwrap(); + let back = shared(&device, total, |_| 0); + blit(&device, |encoder| { + encoder + .copy_from_host_ram(&ram, &runs, &private, 0) + .unwrap(); + encoder + .copy_from_buffer_to_buffer(&private, 0, &back, 0, total) + .unwrap(); + }); + assert!( + contents(&back) == gathered(&runs), + "through a private buffer" + ); + // A scatter back of new bytes, from byte 64 of its source: each run takes its bytes in order, + // and no other byte of the RAM changes, the sentinels in the runs' pages among them. + let source = shared(&device, 64 + total, |at| pattern(at, 2)); + let mut want = bytes(start..start + RAM_BYTES); + let mut at = 64; + for run in &runs { + for address in run.clone() { + want[(address - start) as usize] = pattern(at, 2); + at += 1; + } + } + blit(&device, |encoder| { + encoder.copy_to_host_ram(&source, 64, &ram, &runs).unwrap(); + }); + let now = bytes(start..start + RAM_BYTES); + for pages in &touched { + let (from, to) = ((pages.start - start) as usize, (pages.end - start) as usize); + assert!( + now[from..to] == want[from..to], + "the runs' pages {pages:x?}" + ); + } + assert!( + now == want, + "the scattered bytes, and no other byte of the RAM" + ); + // A scatter whose runs name bytes twice: the later run's bytes are the ones left. + let twice = [ + start + 40 * PAGE + 16..start + 40 * PAGE + 100, + start + 40 * PAGE + 96..start + 40 * PAGE + 202, + ]; + let length: u64 = twice.iter().map(|run| run.end - run.start).sum(); + let source = shared(&device, length, |at| pattern(at, 3)); + let mut want = bytes(start + 40 * PAGE..start + 41 * PAGE); + let mut at = 0; + for run in &twice { + for address in run.clone() { + want[(address - start - 40 * PAGE) as usize] = pattern(at, 3); + at += 1; + } + } + blit(&device, |encoder| { + encoder.copy_to_host_ram(&source, 0, &ram, &twice).unwrap(); + }); + assert!( + bytes(start + 40 * PAGE..start + 41 * PAGE) == want, + "the later of two runs over the same bytes" + ); + // A buffer over host pages the runs' bytes overlap is refused: no region reads and writes + // one byte. One over other pages is not. + let over = over_ram(&device, start + 50 * PAGE, PAGE, itself(), true); + let inside = one(start + 50 * PAGE + 16..start + 50 * PAGE + 64); + let mut encoder_result = None; + run( + &device, + |command_buffer| { + let mut encoder = command_buffer.blit_command_encoder().unwrap(); + encoder_result = Some(encoder.copy_from_host_ram(&ram, &inside, &over, 0)); + let apart = one(start + 60 * PAGE + 16..start + 60 * PAGE + 64); + encoder.copy_from_host_ram(&ram, &apart, &over, 0).unwrap(); + encoder.end_encoding().unwrap(); + }, + || {}, + ); + assert!( + refused(encoder_result.unwrap(), true), + "a buffer over the runs' own bytes" + ); +} + #[test] fn the_cpus_writes_before_the_commit_are_gathered_and_a_scatter_is_in_the_pages_after() { let Some(device) = device() else { @@ -683,7 +846,7 @@ fn a_private_bit_copy_over_the_pages_copies_in_only_the_pages_the_log_reports() } #[test] -fn copies_off_the_page_outside_the_ram_or_past_the_buffer_are_refused() { +fn copies_off_a_word_outside_the_ram_or_past_the_buffer_are_refused() { let Some(device) = device() else { return; }; @@ -703,8 +866,8 @@ fn copies_off_the_page_outside_the_ram_or_past_the_buffer_are_refused() { )); let used = device.use_host_ram(&ram).unwrap(); for runs in [ - one(page(1) + 4..page(2) + 4), - one(page(1)..page(1) + 100), + one(page(1) + 2..page(2)), + one(page(1) + 1..page(1) + 100), one(page(1)..page(1)), one(page(2)..page(1)), one(start - PAGE..start), diff --git a/crates/metal2vulkan-runtime/src/work_counts.rs b/crates/metal2vulkan-runtime/src/work_counts.rs index 405c2f783..200bef859 100644 --- a/crates/metal2vulkan-runtime/src/work_counts.rs +++ b/crates/metal2vulkan-runtime/src/work_counts.rs @@ -351,6 +351,7 @@ pub(crate) struct Counters { host_ram_copies: AtomicU64, host_ram_copy_bytes: AtomicU64, host_ram_copy_dispatches: AtomicU64, + host_ram_copy_regions: AtomicU64, host_ram_chunks_imported: AtomicU64, host_ram_dma_buf_imports: AtomicU64, host_ram_pointer_imports_failed: AtomicU64, @@ -2916,6 +2917,12 @@ impl Counters { (self.host_ram_copy_dispatches).fetch_add(dispatches, Ordering::Relaxed); } + /// A blit encoder recorded a copy between a guest RAM and a buffer whose runs are not all + /// whole pages as `regions` regions of `vkCmdCopyBuffer` (`host_ram/copy.rs`). + pub(crate) fn host_ram_copy_regions(&self, regions: u64) { + (self.host_ram_copy_regions).fetch_add(regions, Ordering::Relaxed); + } + /// A host import the device never releases while idle, for `because`, was made (`kept`), or /// went. pub(crate) fn host_import_kept(&self, because: KeptBecause, kept: bool) { @@ -6053,8 +6060,12 @@ pub struct WorkCounts { /// Their bytes. pub host_ram_copy_bytes: u64, /// Their dispatches: one for each chunk a copy's runs touch (more where its bytes pass the - /// device's storage range or its pages of a chunk its workgroup count), never one per run. + /// device's storage range or its pages of a chunk its workgroup count), never one per run, of + /// the copies whose runs are all whole pages. pub host_ram_copy_dispatches: u64, + /// The regions of `vkCmdCopyBuffer` of the copies whose runs are not all whole pages (a run + /// that starts or ends inside a page): one for each run's bytes in a chunk. + pub host_ram_copy_regions: u64, /// The chunks of guest RAM the device imported (`Device::use_host_ram`): 64 MiB of a RAM's /// range each (its last fewer), each once, by the device's own thread ahead of the objects /// over it or by the first object made over it where that thread had not imported it. @@ -7520,6 +7531,7 @@ impl Device { host_ram_copies: counters.host_ram_copies.load(Ordering::Relaxed), host_ram_copy_bytes: counters.host_ram_copy_bytes.load(Ordering::Relaxed), host_ram_copy_dispatches: counters.host_ram_copy_dispatches.load(Ordering::Relaxed), + host_ram_copy_regions: counters.host_ram_copy_regions.load(Ordering::Relaxed), host_ram_chunks_imported: counters.host_ram_chunks_imported.load(Ordering::Relaxed), host_ram_dma_buf_imports: counters.host_ram_dma_buf_imports.load(Ordering::Relaxed), host_ram_pointer_imports_failed: counters diff --git a/crates/metal2vulkan/tests/api_signatures.tsv b/crates/metal2vulkan/tests/api_signatures.tsv index 5bd808b3f..0e1ccabae 100644 --- a/crates/metal2vulkan/tests/api_signatures.tsv +++ b/crates/metal2vulkan/tests/api_signatures.tsv @@ -2793,6 +2793,7 @@ field metal2vulkan-runtime::WorkCounts.host_ram_chunks_released pub host_ram_chu field metal2vulkan-runtime::WorkCounts.host_ram_copies pub host_ram_copies: u64 field metal2vulkan-runtime::WorkCounts.host_ram_copy_bytes pub host_ram_copy_bytes: u64 field metal2vulkan-runtime::WorkCounts.host_ram_copy_dispatches pub host_ram_copy_dispatches: u64 +field metal2vulkan-runtime::WorkCounts.host_ram_copy_regions pub host_ram_copy_regions: u64 field metal2vulkan-runtime::WorkCounts.host_ram_dma_buf_imports pub host_ram_dma_buf_imports: u64 field metal2vulkan-runtime::WorkCounts.host_ram_pointer_imports_failed pub host_ram_pointer_imports_failed: u64 field metal2vulkan-runtime::WorkCounts.host_range_lookups pub host_range_lookups: u64 diff --git a/docs/HOWTO.md b/docs/HOWTO.md index 0ebf3154a..a9166ce11 100644 --- a/docs/HOWTO.md +++ b/docs/HOWTO.md @@ -626,12 +626,18 @@ submission, can give the device the guest's RAM once instead: the device uses into consecutive bytes of `destination` from `offset`, in the order given, and `copy_to_host_ram(source, offset, ram, runs)` scatters them back: for a surface whose pages are a scatter of guest pages, with no view of them and no import of its own. Each run is a host - address range inside the RAM's ranges, 4 KiB-aligned and whole 4 KiB pages; a run that starts - where the one before ended joins it, a descending neighbour stays apart, and each is cut where - a chunk ends. The pages become a table of 4 KiB pages in the command buffer's upload arena and - one compute dispatch for each chunk they touch, over the chunk's own `VkBuffer` (more only past - the device's storage range or workgroup count): never a copy region per run - (`WorkCounts::host_ram_copies`, `host_ram_copy_bytes`, `host_ram_copy_dispatches`). A copy takes + address range inside the RAM's ranges that starts on a 4-byte word and ends at any byte past + it: a surface's whole pages, or each of its rows' texels alone, whose padding and edge pages the + guest's CPU may own; a run that starts where the one before ended joins it, a descending + neighbour stays apart, and each is cut where a chunk ends. Where every run is whole 4 KiB pages + from a multiple of 4 KiB, the pages become a table of 4 KiB pages in the command buffer's upload + arena and one compute dispatch for each chunk they touch, over the chunk's own `VkBuffer` (more + only past the device's storage range or workgroup count): never a copy region per run + (`WorkCounts::host_ram_copies`, `host_ram_copy_bytes`, `host_ram_copy_dispatches`). Otherwise + each run's bytes in a chunk are one region of a `vkCmdCopyBuffer` between the import that holds + them (whole pages, imported as for any copy) and the buffer, the regions of one import one + command of at most 1024: no other byte of their pages is read or written + (`WorkCounts::host_ram_copy_regions`). A copy takes each chunk it touches as an object over it does, and the thread's window moves past it; but where an object over a chunk not imported yet is bound in an import of its own run, a copy makes none: the chunk is imported first, or the thread's import of it waited for. Each copy is ordered @@ -644,8 +650,9 @@ submission, can give the device the guest's RAM once instead: completion, and where the RAM came with a log its scatters are reported to it as the GPU's writes in place, so copied buffers and shadows over those pages copy them in at their next commit. Refused: a device without `VK_EXT_external_memory_host` (`Unsupported`); a RAM the device does - not use, a run empty, off 4 KiB or outside the RAM, or bytes of the buffer that start off a - 4-byte word of its memory (`Invalid`, `Problem::HostRam`); bytes past the buffer (`Invalid`, + not use, a run empty, starting off a 4-byte word or outside the RAM, bytes of the buffer that + start off a 4-byte word of its memory, or, of runs not all whole pages, a buffer over host + pages the runs' bytes overlap (`Invalid`, `Problem::HostRam`); bytes past the buffer (`Invalid`, `Problem::RangeOutOfBounds`); a chunk the device cannot import (the import's error). A caller falls back to its own copy on any of them. diff --git a/docs/REFUSALS.md b/docs/REFUSALS.md index cfe165f07..e27b12b7e 100644 --- a/docs/REFUSALS.md +++ b/docs/REFUSALS.md @@ -335,7 +335,7 @@ Columns: | `ScheduledSignalAfterCompletion` | A scheduled signal encoded once the command buffer completed, or failed (status `Error`) (`CommandBuffer::encode_signal_event_scheduled`) | every device (a public call's own check of its arguments) | `metal-measured`: an M3 Max, macOS 27.0.1: a segmentation fault in 10 of 15 runs, as the consumer measured | 1: `crates/metal2vulkan-runtime/src/command_buffer/mod.rs (encode_signal_event_scheduled)` | `-` | | `ForeignObject` | An object made by another device | every device (a public call's own check of its arguments) | `unknown`: Metal's answer for an object another device made is unmeasured | 1: `crates/metal2vulkan-runtime/src/device/line.rs (foreign)` | `-` | | `DmaBuf` | Dma-bufs a `DmaBufSource` returned (a runtime extension | every device (a public call's own check of its arguments) | `ours`: a runtime extension's own contract: Metal imports no dma-buf | 2: `crates/metal2vulkan-runtime/src/dma_buf.rs (import_from_source)`, `crates/metal2vulkan-runtime/src/dma_buf.rs (ram_span_as_dma_buf)` | `-` | -| `HostRam` | Guest RAM, or a run resolver's answer, that breaks its runtime extension's contract (`HostRam::new`, `Device::use_host_ram`,... | every device (a public call's own check of its arguments) | `ours`: a runtime extension's own contract: Metal imports no guest RAM | 4: `crates/metal2vulkan-runtime/src/host_ram.rs (new)`, `crates/metal2vulkan-runtime/src/host_ram.rs (route_runs)` (+2) | `-` | +| `HostRam` | Guest RAM, or a run resolver's answer, that breaks its runtime extension's contract (`HostRam::new`, `Device::use_host_ram`,... | every device (a public call's own check of its arguments) | `ours`: a runtime extension's own contract: Metal imports no guest RAM | 5: `crates/metal2vulkan-runtime/src/host_ram.rs (new)`, `crates/metal2vulkan-runtime/src/host_ram.rs (route_runs)` (+3) | `-` | | `WrappedImage` | An image the caller made that cannot be wrapped as a texture of the descriptor (`new_texture_with_vk_image`) | every device (a public call's own check of its arguments) | `ours`: a runtime extension: Metal wraps no Vulkan image | 2: `crates/metal2vulkan-runtime/src/texture/wrapped.rs (new_texture_with_vk_image)`, `crates/metal2vulkan-runtime/src/texture/wrapped.rs (new_texture_with_vk_image_plane)` | `-` | | `NotShareable` | A handle of a texture that is not shareable, for which Metal answers nil, an [`Error::Nil`] | every device (a public call's own check of its arguments) | `unknown`: Metal's nil for a texture that is not shareable is unmeasured | 1: `crates/metal2vulkan-runtime/src/texture/shared.rs (new_shared_texture_handle)` | `-` | | `Semaphore` | A semaphore that cannot be one of the caller's binary semaphores | every device (a public call's own check of its arguments) | `ours`: a runtime extension: Metal has no binary semaphores | 1: `crates/metal2vulkan-runtime/src/command_buffer/mod.rs (binary_command)` | `-` | @@ -818,7 +818,7 @@ Each refusal's sites, with the function that builds it. - `invalid::ScheduledSignalAfterCompletion`: `crates/metal2vulkan-runtime/src/command_buffer/mod.rs (encode_signal_event_scheduled)` - `invalid::ForeignObject`: `crates/metal2vulkan-runtime/src/device/line.rs (foreign)` - `invalid::DmaBuf`: `crates/metal2vulkan-runtime/src/dma_buf.rs (import_from_source)`; `crates/metal2vulkan-runtime/src/dma_buf.rs (ram_span_as_dma_buf)` -- `invalid::HostRam`: `crates/metal2vulkan-runtime/src/host_ram.rs (new)`; `crates/metal2vulkan-runtime/src/host_ram.rs (route_runs)`; `crates/metal2vulkan-runtime/src/host_ram.rs (use_host_ram_now)`; `crates/metal2vulkan-runtime/src/host_ram/copy.rs (record_host_ram_copy)` +- `invalid::HostRam`: `crates/metal2vulkan-runtime/src/host_ram.rs (new)`; `crates/metal2vulkan-runtime/src/host_ram.rs (route_runs)`; `crates/metal2vulkan-runtime/src/host_ram.rs (use_host_ram_now)`; `crates/metal2vulkan-runtime/src/host_ram/copy.rs (record_host_ram_copy)`; `crates/metal2vulkan-runtime/src/host_ram/copy.rs (record_regions)` - `invalid::WrappedImage`: `crates/metal2vulkan-runtime/src/texture/wrapped.rs (new_texture_with_vk_image)`; `crates/metal2vulkan-runtime/src/texture/wrapped.rs (new_texture_with_vk_image_plane)` - `invalid::NotShareable`: `crates/metal2vulkan-runtime/src/texture/shared.rs (new_shared_texture_handle)` - `invalid::Semaphore`: `crates/metal2vulkan-runtime/src/command_buffer/mod.rs (binary_command)`