host_ab: draw_host, each draw's vertex buffer bound at the next 256 bytes of no-copy host chunks

The encode workload gains a variant over the buffer kind a guest's setVertexBytes: reaches the
runtime through: no-copy buffers of 64 KiB over host pages (one mapping, a buffer a chunk, 256
draws' rectangles each), each draw binding the chunk its rectangle lies in at its offset
(setVertexBuffer:offset:atIndex:), as reims-vgpu's draws.10k measurement does. The other variants
draw as before (encode_draws takes the chunks, empty for them).

Verified: clippy over host_ab, all features. Run on the RTX 5080 Laptop GPU at the lane base and at
the draw path's step 4 (one run each, 3000 and 9000 draws a pass, encode us a draw): draw_host
0.642 / 0.617 and 0.584 / 0.528, draw_offset 0.536 / 0.532 and 0.490 / 0.516.

(cherry picked from commit 691332c6c3076d78b68abed22960227d3c220e27)
(cherry picked from commit 07145d5cfde93f81acf46fe8ae84e306cd2760ae)
This commit is contained in:
Anees Iqbal committed 2026-11-03 11:08:02 -08:00
1 parent 32b1920f21
commit 7148b9518f
2 files changed
+58 -7

No files matched your search

@@ -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
@@ -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: Gpu>(
g: &G,
s: &Scene<G>,
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<G: Gpu>(
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: Gpu>(
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: Gpu>(g: &G, draws: usize) -> Vec<G::Buffer> {
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::<RECT_STRIDE>()
.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<G: Gpu>(
@@ -555,17 +598,22 @@ pub fn encode<G: Gpu>(
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: Gpu>(g: &G, s: &Scene<G>, 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), &[]),
}
}