RUNTIME 2225 and 2242 let an indirect command buffer's setters write without the lock. RUNTIME
2247 put the records in the runtime's own imported pages wherever the device imports host
pointers, so every carry there keeps the pages, the pointer and the records (Kept; the carry's
forget_keys sends the next named set under the lock), and an unlocked write needs no tracking.
Records that still lie in pooled memory (a device without host-pointer import, or a failed
import) move at a recovery (Lost, every command reset), and an unlocked write could hold their
old pointer across the carry's release: a use-after-free.
- Pooled records only: each unlocked setter call counts itself with one read-modify-write
(decided per ICB at creation, counted_write #[cold]); the carry waits the count out before it
releases the old parts. Own-pages records write as before RUNTIME 2247, behind one branch.
- A getter's read of the slots past the records (overflow) holds their parts, as record reads do.
- WorkCounts::indirect_record_reset_gpu_nanos: the GPU time of each resetCommandsInBuffer
dispatch, timed with the rate-map pieces' timestamp slots (a third kind), read when the command
buffer lets go of what it retained; no sync added; 0 where timestamps are absent or wait for
every queue's.
- set_each's setters run inlined in its loop (#[inline(always)] on set_one): the consumer saw the
set passed by pointer read wide after narrow writes (a store-forwarding stall).
- HOWTO: each live ICB's records are one held import.
Verified (carryrace, 13fd624294 on 31a07f2867, msi lavapipe):
- Suite 27209: 92/92 (ICB carry race, reset timing, rate-mapped, indirect, executions, device
recovery, fresh pages, no pointer import, recycling); pooled tests print Lost, own pages Kept;
the reset timing read 0 ns for a copy alone and 1.28 ms for a reset.
- Negative controls: with the count off, the carry-waits test fails (the recovery ended while an
unlocked write held the records' bytes); with bytes read at batch creation and no getter hold,
the unlocked-set and getter tests crash (SIGSEGV).
- Bench 27171 (Core Ultra 9 275HX, one core, least of 3 passes), 256 commands, development
before 2247 / 2247 / this: compute set_compute_command 70.8-74.8 / 70.8-72.4 / 71.2-71.9 ns;
full slot 95.9-106.1 / 98.1-100.6 / 102.4-105.8; draw only 32.4-33.5 / 32.4-32.9 / 33.3-34.0.
Pooled path with the count +35 ns a command. Inlined set_one: batched render rows -10 ns, compute
-3 to -4 ns. Submissions with 0 / 64 / 256 live ICBs: 104.8 / 99.6 / 100.3 us median.
- API tests 15/15, rustdoc 0. Clippy's only errors at this base are development's 2248 lints in
render.rs and render/fan_out/tests.rs, untouched here.
RUNTIME_VERSION 2248 -> 2249.
312 lines
13 KiB
Rust
312 lines
13 KiB
Rust
//! What the CPU spends encoding the commands of a render indirect command buffer through
|
|
//! `IndirectRenderCommand`'s setters, in nanoseconds a command, the median and least of [`REPS`]
|
|
//! loops of [`COMMANDS`] commands after one untimed, on a buffer of [`COMMANDS`] (whose records
|
|
//! outgrow a CPU's last-level cache) and on one of [`CACHED`] (whose records fit in its L2), its
|
|
//! commands encoded in turn over and over:
|
|
//!
|
|
//! - **full slot**: `indirect_render_command_at(i)`, a render pipeline state, two vertex buffers
|
|
//! (one at `(i % 16) * 16`), a fragment buffer and a draw of primitives, the consumer's
|
|
//! `icb_slots_cost` loop through the runtime alone;
|
|
//! - **compute slot**: `indirect_compute_command_at(i)` of a buffer of dispatches, a compute
|
|
//! pipeline state, two kernel buffers (one at `(i % 16) * 16`) and a dispatch of threadgroups;
|
|
//! - **set_each**: each slot's setters in one `set_each` call;
|
|
//! - **set_render_command**, **set_compute_command**: each slot's setters in one call of the
|
|
//! buffer's, with no command's handle;
|
|
//! - **three fields**: `indirect_render_command_at(i)`, a vertex buffer at `(i % 8) * 256`, a
|
|
//! fragment buffer at `(i % 4) * 64` and a draw of primitives, a consumer's loop;
|
|
//! - **draw only**: the command and its draw;
|
|
//! - **command only**: `indirect_render_command_at(i)` alone.
|
|
//!
|
|
//! Each case runs on the device the cfg selects, whose indirect command buffers' records lie in
|
|
//! the runtime's own pages where it imports host pointers, then (`pooled`) on one made without
|
|
//! `VK_EXT_external_memory_host`, whose records lie in pooled memory, where each setter's write
|
|
//! without the lock counts itself under way for a recovery's carry (`writers.rs`).
|
|
//!
|
|
//! Printed as `ICB-ENCODE` lines, beside the sizes of `Result<(), Error>` and `Error`. Nothing is
|
|
//! submitted. Beside it, what live indirect command buffers add to a submission
|
|
//! ([`icb_submission_costs`]), printed as `ICB-SUBMIT` lines. Ignored: run them in release, under
|
|
//! the GPU safety cfg.
|
|
//!
|
|
//! A device test: compiled only under the validation crate's GPU safety cfgs (`m2v_gpu`,
|
|
//! `m2v_cpu_icd`), on the kind of device the cfg selects.
|
|
|
|
use super::blit_holds::{device, options, Scratch};
|
|
use super::indirect_command_buffer_device::{compute_state, render_state, EXAMPLES};
|
|
use crate::{
|
|
Device, DrawPrimitiveType, Error, IndirectCommandBuffer, IndirectCommandBufferDescriptor,
|
|
IndirectComputeSet, IndirectRenderSet, ResourceOptions, Size,
|
|
};
|
|
use metal2vulkan::bind::indirect_commands::COMMAND_BYTES;
|
|
use metal2vulkan::device::VkExtension;
|
|
use metal2vulkan::metal::IndirectCommandType;
|
|
use std::hint::black_box;
|
|
use std::time::{Duration, Instant};
|
|
|
|
/// The commands of the large buffer, and the commands encoded a loop.
|
|
const COMMANDS: u64 = 16384;
|
|
|
|
/// The commands of the small buffer.
|
|
const CACHED: u64 = 256;
|
|
|
|
/// The timed loops of each case, after one untimed.
|
|
const REPS: usize = 20;
|
|
|
|
/// The median and least of [`REPS`] runs of `encode` over [`COMMANDS`] commands, the `i`th of
|
|
/// them at index `i % size`, after one untimed, in nanoseconds a command.
|
|
fn per_command(size: u64, encode: impl Fn(u64)) -> (f64, f64) {
|
|
let run = || {
|
|
let start = Instant::now();
|
|
for i in 0..COMMANDS {
|
|
encode(i % size);
|
|
}
|
|
start.elapsed().as_nanos() as f64 / COMMANDS as f64
|
|
};
|
|
run();
|
|
let mut times: Vec<f64> = (0..REPS).map(|_| run()).collect();
|
|
times.sort_by(f64::total_cmp);
|
|
(times[times.len() / 2], times[0])
|
|
}
|
|
|
|
fn print(case: &str, (median, least): (f64, f64)) {
|
|
eprintln!("ICB-ENCODE {case}: median {median:.1} ns a command, least {least:.1}");
|
|
}
|
|
|
|
/// The setters' cost a command (the module docs).
|
|
#[test]
|
|
#[ignore = "a release-mode benchmark"]
|
|
fn icb_encode_costs() {
|
|
let scratch = Scratch::new("icb-encode");
|
|
encode_costs(&device(&scratch.0), "");
|
|
let mut pooled = options(&scratch.0);
|
|
pooled.withheld_extensions = [VkExtension::ExtExternalMemoryHost].into_iter().collect();
|
|
encode_costs(&Device::system_default_with(&pooled).unwrap(), ", pooled");
|
|
}
|
|
|
|
/// [`icb_encode_costs`]'s cases on `device`, each named after `of`.
|
|
fn encode_costs(device: &Device, of: &str) {
|
|
let mut descriptor = IndirectCommandBufferDescriptor::new();
|
|
descriptor.command_types = IndirectCommandType::DRAW
|
|
| IndirectCommandType::DRAW_INDEXED
|
|
| IndirectCommandType::DRAW_PATCHES
|
|
| IndirectCommandType::DRAW_INDEXED_PATCHES;
|
|
descriptor.max_vertex_buffer_bind_count = 2;
|
|
descriptor.max_fragment_buffer_bind_count = 1;
|
|
let shared = ResourceOptions::shared();
|
|
let (vertex, fragment) = (
|
|
device.new_buffer(65536, shared).unwrap(),
|
|
device.new_buffer(65536, shared).unwrap(),
|
|
);
|
|
let strip = DrawPrimitiveType::from_raw(4).unwrap();
|
|
let triangle = DrawPrimitiveType::from_raw(3).unwrap();
|
|
let render = render_state(device, true);
|
|
let compute = compute_state(device, EXAMPLES, "scale_and_offset", true);
|
|
let mut compute_descriptor = IndirectCommandBufferDescriptor::new();
|
|
compute_descriptor.command_types = IndirectCommandType::CONCURRENT_DISPATCH;
|
|
compute_descriptor.max_kernel_buffer_bind_count = 2;
|
|
let group = Size::new(1, 1, 1);
|
|
eprintln!(
|
|
"ICB-ENCODE {}: Result<(), Error> {} bytes, Error {} bytes, a record {COMMAND_BYTES} \
|
|
bytes, {COMMANDS} commands a loop",
|
|
device.name(),
|
|
size_of::<Result<(), Error>>(),
|
|
size_of::<Error>(),
|
|
);
|
|
for size in [COMMANDS, CACHED] {
|
|
let icb: IndirectCommandBuffer = device
|
|
.new_indirect_command_buffer(&descriptor, size, shared)
|
|
.unwrap();
|
|
let records = size * COMMAND_BYTES as u64 / 1024;
|
|
let own = icb.records_in_own_pages();
|
|
eprintln!("ICB-ENCODE {size} commands{of}: records in the runtime's own pages: {own}");
|
|
print(
|
|
&format!("{size} commands{of} ({records} KiB of records), full slot"),
|
|
per_command(size, |i| {
|
|
let command = icb.indirect_render_command_at(i).unwrap();
|
|
command.set_render_pipeline_state(&render).unwrap();
|
|
command
|
|
.set_vertex_buffer(Some(&vertex), (i % 16) * 16, 0)
|
|
.unwrap();
|
|
command.set_vertex_buffer(Some(&fragment), 0, 1).unwrap();
|
|
command.set_fragment_buffer(Some(&vertex), 256, 0).unwrap();
|
|
command.draw_primitives(triangle, 0, 3, 1, 0).unwrap();
|
|
}),
|
|
);
|
|
let render_sets = |i: u64| {
|
|
[
|
|
IndirectRenderSet::SetRenderPipelineState(&render),
|
|
IndirectRenderSet::SetVertexBuffer {
|
|
buffer: Some(&vertex),
|
|
offset: (i % 16) * 16,
|
|
index: 0,
|
|
},
|
|
IndirectRenderSet::SetVertexBuffer {
|
|
buffer: Some(&fragment),
|
|
offset: 0,
|
|
index: 1,
|
|
},
|
|
IndirectRenderSet::SetFragmentBuffer {
|
|
buffer: Some(&vertex),
|
|
offset: 256,
|
|
index: 0,
|
|
},
|
|
IndirectRenderSet::DrawPrimitives {
|
|
primitive_type: triangle,
|
|
vertex_start: 0,
|
|
vertex_count: 3,
|
|
instance_count: 1,
|
|
base_instance: 0,
|
|
},
|
|
]
|
|
};
|
|
print(
|
|
&format!("{size} commands{of}, full slot, set_each"),
|
|
per_command(size, |i| {
|
|
let command = icb.indirect_render_command_at(i).unwrap();
|
|
command.set_each(render_sets(i)).unwrap();
|
|
}),
|
|
);
|
|
print(
|
|
&format!("{size} commands{of}, full slot, set_render_command"),
|
|
per_command(size, |i| {
|
|
icb.set_render_command(i, render_sets(i)).unwrap();
|
|
}),
|
|
);
|
|
let dispatches: IndirectCommandBuffer = device
|
|
.new_indirect_command_buffer(&compute_descriptor, size, shared)
|
|
.unwrap();
|
|
print(
|
|
&format!("{size} commands{of}, compute slot"),
|
|
per_command(size, |i| {
|
|
let command = dispatches.indirect_compute_command_at(i).unwrap();
|
|
command.set_compute_pipeline_state(&compute).unwrap();
|
|
command
|
|
.set_kernel_buffer(Some(&vertex), (i % 16) * 16, 0)
|
|
.unwrap();
|
|
command.set_kernel_buffer(Some(&fragment), 0, 1).unwrap();
|
|
command
|
|
.concurrent_dispatch_threadgroups(group, group)
|
|
.unwrap();
|
|
}),
|
|
);
|
|
let compute_sets = |i: u64| {
|
|
[
|
|
IndirectComputeSet::SetComputePipelineState(&compute),
|
|
IndirectComputeSet::SetKernelBuffer {
|
|
buffer: Some(&vertex),
|
|
offset: (i % 16) * 16,
|
|
index: 0,
|
|
},
|
|
IndirectComputeSet::SetKernelBuffer {
|
|
buffer: Some(&fragment),
|
|
offset: 0,
|
|
index: 1,
|
|
},
|
|
IndirectComputeSet::ConcurrentDispatchThreadgroups {
|
|
threadgroups: group,
|
|
threads_per_threadgroup: group,
|
|
},
|
|
]
|
|
};
|
|
print(
|
|
&format!("{size} commands{of}, compute slot, set_each"),
|
|
per_command(size, |i| {
|
|
let command = dispatches.indirect_compute_command_at(i).unwrap();
|
|
command.set_each(compute_sets(i)).unwrap();
|
|
}),
|
|
);
|
|
print(
|
|
&format!("{size} commands{of}, compute slot, set_compute_command"),
|
|
per_command(size, |i| {
|
|
dispatches.set_compute_command(i, compute_sets(i)).unwrap();
|
|
}),
|
|
);
|
|
print(
|
|
&format!("{size} commands{of}, three fields"),
|
|
per_command(size, |i| {
|
|
let command = icb.indirect_render_command_at(i).unwrap();
|
|
command
|
|
.set_vertex_buffer(Some(&vertex), (i % 8) * 256, 0)
|
|
.unwrap();
|
|
command
|
|
.set_fragment_buffer(Some(&fragment), (i % 4) * 64, 0)
|
|
.unwrap();
|
|
command.draw_primitives(strip, 1, 3, 2, 7).unwrap();
|
|
}),
|
|
);
|
|
print(
|
|
&format!("{size} commands{of}, draw only"),
|
|
per_command(size, |i| {
|
|
let command = icb.indirect_render_command_at(i).unwrap();
|
|
command.draw_primitives(strip, 1, 3, 2, 7).unwrap();
|
|
}),
|
|
);
|
|
print(
|
|
&format!("{size} commands{of}, command only"),
|
|
per_command(size, |i| {
|
|
black_box(icb.indirect_render_command_at(i).unwrap());
|
|
}),
|
|
);
|
|
}
|
|
}
|
|
|
|
/// The submissions each case of [`icb_submission_costs`] times, after one untimed.
|
|
const SUBMITS: usize = 400;
|
|
|
|
/// The indirect command buffers [`icb_submission_costs`] holds alive.
|
|
const LIVE: [u64; 2] = [64, 256];
|
|
|
|
/// What live indirect command buffers add to a submission: the median and least of [`SUBMITS`]
|
|
/// commits of a command buffer of one 16-byte fill, each waited for, with none of the bench's
|
|
/// buffers alive, then with [`LIVE`] of them alive, each of 64 commands. Where the device imports
|
|
/// host pointers each one's records are a held import of their own (`fresh_pages.rs`, counted by
|
|
/// `WorkCounts::fresh_page_buffers_made`), which a driver that lists every held import in every
|
|
/// submission pays for each (the import tax, `submit_check.rs`).
|
|
#[test]
|
|
#[ignore = "a release-mode benchmark"]
|
|
fn icb_submission_costs() {
|
|
let scratch = Scratch::new("icb-submit");
|
|
let device = device(&scratch.0);
|
|
let queue = device.new_command_queue().unwrap();
|
|
let target = device.new_buffer(4096, ResourceOptions::private()).unwrap();
|
|
let submit = || {
|
|
let command_buffer = queue.command_buffer().unwrap();
|
|
let mut blit = command_buffer.blit_command_encoder().unwrap();
|
|
blit.fill_buffer(&target, 0..16, 1).unwrap();
|
|
blit.end_encoding().unwrap();
|
|
let start = Instant::now();
|
|
command_buffer.commit().unwrap();
|
|
command_buffer.wait_until_completed().unwrap();
|
|
start.elapsed()
|
|
};
|
|
let costs = || {
|
|
submit();
|
|
let mut times: Vec<Duration> = (0..SUBMITS).map(|_| submit()).collect();
|
|
times.sort();
|
|
(times[times.len() / 2], times[0])
|
|
};
|
|
let mut descriptor = IndirectCommandBufferDescriptor::new();
|
|
descriptor.command_types = IndirectCommandType::DRAW;
|
|
descriptor.max_vertex_buffer_bind_count = 2;
|
|
eprintln!("ICB-SUBMIT {}", device.name());
|
|
let (median, least) = costs();
|
|
eprintln!("ICB-SUBMIT none alive: median {median:?} a submission, least {least:?}");
|
|
for live in LIVE {
|
|
let before = device.work_counts().fresh_page_buffers_made;
|
|
let held: Vec<IndirectCommandBuffer> = (0..live)
|
|
.map(|_| {
|
|
device
|
|
.new_indirect_command_buffer(&descriptor, 64, ResourceOptions::shared())
|
|
.unwrap()
|
|
})
|
|
.collect();
|
|
let imports = device.work_counts().fresh_page_buffers_made - before;
|
|
let own = held.iter().filter(|icb| icb.records_in_own_pages()).count();
|
|
let (median, least) = costs();
|
|
eprintln!(
|
|
"ICB-SUBMIT {live} alive ({own} in the runtime's own pages, {imports} fresh-page \
|
|
buffers made): median {median:?} a submission, least {least:?}"
|
|
);
|
|
drop(held);
|
|
}
|
|
}
|