diff --git a/crates/metal2vulkan-runtime/examples/host_ab/main.rs b/crates/metal2vulkan-runtime/examples/host_ab/main.rs index e7587cba6..43de9ebe1 100644 --- a/crates/metal2vulkan-runtime/examples/host_ab/main.rs +++ b/crates/metal2vulkan-runtime/examples/host_ab/main.rs @@ -22,7 +22,9 @@ //! pipeline state switched between two), `passes` (as `pipeline`, in render passes of three //! draws, as a window server draws) and `compositor` (as `bind`, in render passes of 300 draws, //! each run of 10 draws by the next of three pipeline states, as a window server's frame -//! draws), and a compute encoder of as many single-threadgroup +//! draws) and `host` (each draw the vertex buffer bound at the next 256 bytes of no-copy +//! buffers of 64 KiB over host pages, as a guest's `setVertexBytes:` reaches its upload +//! chunks), and a compute encoder of as many single-threadgroup //! dispatches, `static`, `offsets` (both buffers' offsets each dispatch) and `churn` (the //! pipeline state and both buffers' offsets each dispatch). Per draw and dispatch: the encoding thread's wall time from the command buffer's //! creation through `endEncoding`, and the whole process's CPU time until the wait returned. @@ -210,6 +212,7 @@ //! `--metallib PATH` (skip the compile; not with `pipeline`), `--cache DIR` (the runtime's shader //! cache, with `--side`), `--variants a,b,...` (the encode workload's variants alone: //! `draw_static`, `draw_offset`, `draw_bind`, `draw_pipeline`, `draw_passes`, `draw_compositor`, +//! `draw_host`, //! `dispatch_static`, `dispatch_offsets`, `dispatch_churn`, `dispatch_resident_0`, `_1`, `_256`, //! `_4096`, `_same_4096`, `dispatch_resident_moving_0`, `_16`, `_256`, `_4096`, //! `dispatch_resident_textures_256`, `_4096`, `dispatch_handles`; for a profile of one; and the diff --git a/crates/metal2vulkan-runtime/examples/host_ab/workloads.rs b/crates/metal2vulkan-runtime/examples/host_ab/workloads.rs index 0f8f36c1f..79af4ac74 100644 --- a/crates/metal2vulkan-runtime/examples/host_ab/workloads.rs +++ b/crates/metal2vulkan-runtime/examples/host_ab/workloads.rs @@ -286,8 +286,16 @@ enum Churn { /// one): a window server's frame of a few hundred draws a pass, each binding its own buffer /// and texture, its few pipeline states the same every frame. Compositor, + /// Each draw's own rectangle, at the next [`RECT_STRIDE`] bytes of no-copy buffers over host + /// pages of [`HOST_CHUNK`] bytes each (`setVertexBuffer:offset:atIndex:` of the chunk at the + /// offset): a guest's `setVertexBytes:` as its upload chunks hand it on. + Host, } +/// The bytes of each no-copy buffer [`Churn::Host`] draws from, a guest's upload chunk: 256 +/// draws' rectangles. +const HOST_CHUNK: usize = 64 * 1024; + /// The draws of each pass of [`Churn::Passes`]. const PASS_DRAWS: usize = 3; @@ -304,6 +312,7 @@ impl Churn { Churn::Pipeline => "pipeline", Churn::Passes => "passes", Churn::Compositor => "compositor", + Churn::Host => "host", } } @@ -317,13 +326,14 @@ impl Churn { } } -/// Encode one render pass of `draws` small draws with `churn` (module docs of `main.rs`). +/// Encode one render pass of `draws` small draws with `churn` (module docs of `main.rs`), the +/// host churn's from `chunks` ([`host_chunks`]). fn encode_draws( g: &G, s: &Scene, command_buffer: &G::CommandBuffer, - draws: usize, - churn: Churn, + (draws, churn): (usize, Churn), + chunks: &[G::Buffer], ) { let mut encoder = g.render_encoder(command_buffer, &s.target, true); g.set_render_pipeline(&mut encoder, &s.textured[0]); @@ -346,7 +356,14 @@ fn encode_draws( if matches!(churn, Churn::Offset) { g.set_vertex_buffer_offset(&mut encoder, (draw % RECTS) * RECT_STRIDE, 0); } - if !matches!(churn, Churn::Static | Churn::Offset) { + if matches!(churn, Churn::Host) { + let at = draw * RECT_STRIDE; + g.set_vertex_buffer(&mut encoder, &chunks[at / HOST_CHUNK], at % HOST_CHUNK, 0); + } + if matches!( + churn, + Churn::Bind | Churn::Pipeline | Churn::Passes | Churn::Compositor + ) { let buffer = &s.rects[(draw / 7) % RECT_BUFFERS]; g.set_vertex_buffer(&mut encoder, buffer, (draw % RECTS) * RECT_STRIDE, 0); g.set_fragment_texture(&mut encoder, &s.textures[draw % TEXTURES], 0); @@ -359,6 +376,32 @@ fn encode_draws( g.end_render(encoder); } +/// [`Churn::Host`]'s chunks for `draws` draws: no-copy buffers of [`HOST_CHUNK`] bytes each over +/// one mapping of host pages, draw `d`'s rectangle at `d * RECT_STRIDE` bytes of them end to end. +fn host_chunks(g: &G, draws: usize) -> Vec { + let chunks = (draws * RECT_STRIDE).div_ceil(HOST_CHUNK).max(1); + let rects = rect_bytes(0); + // SAFETY: kept for the process's life, past every buffer over it. + let pages = unsafe { host_pages(chunks * HOST_CHUNK) }; + // SAFETY: the mapping's bytes, read-write, which nothing else holds yet. + let bytes = unsafe { std::slice::from_raw_parts_mut(pages, chunks * HOST_CHUNK) }; + for (at, rect) in bytes + .as_chunks_mut::() + .0 + .iter_mut() + .enumerate() + { + let r = at % RECTS; + rect.copy_from_slice(&rects[r * RECT_STRIDE..][..RECT_STRIDE]); + } + (0..chunks) + .map(|chunk| { + // SAFETY: page-aligned read-write bytes of the mapping, which outlives the buffer. + unsafe { g.buffer_no_copy(pages.add(chunk * HOST_CHUNK), HOST_CHUNK) } + }) + .collect() +} + /// Encode one compute encoder of `dispatches` small dispatches, changing the buffers' offsets and /// the pipeline state at each where `churn`. fn encode_dispatches( @@ -555,17 +598,22 @@ pub fn encode( Churn::Pipeline, Churn::Passes, Churn::Compositor, + Churn::Host, ] { if !runs(format!("draw_{}", churn.name())) { continue; } + let chunks = match churn { + Churn::Host => host_chunks(g, n), + _ => vec![], + }; let (mut wall, mut cpu, mut commit, mut total) = (vec![], vec![], vec![], vec![]); for run in 0..sizes.timed + 3 { g.pool(|| { let cpu0 = process_cpu(); let t0 = Instant::now(); let command_buffer = g.command_buffer(); - encode_draws(g, s, &command_buffer, n, churn); + encode_draws(g, s, &command_buffer, (n, churn), &chunks); let t1 = Instant::now(); g.commit(&command_buffer); let t2 = Instant::now(); @@ -925,7 +973,7 @@ fn encode_tiny(g: &G, s: &Scene, command_buffer: &G::CommandBuffer, t Tiny::Empty => {} Tiny::Fill => g.fill(command_buffer, &s.fill, 256), Tiny::Dispatch => encode_dispatches(g, s, command_buffer, 1, DispatchChurn::Static), - Tiny::Draw => encode_draws(g, s, command_buffer, 1, Churn::Static), + Tiny::Draw => encode_draws(g, s, command_buffer, (1, Churn::Static), &[]), } }