Files
metal2vulkan/crates/metal2vulkan-runtime/src/tests/buffer_making_bench.rs
T
Anees Iqbal a1175662e7 RUNTIME 2355: Textures over buffers: a buffer of 16 KiB or more holds Metal's allocation, a heap's buffer its heap
A texture over a buffer whose texels run past the length was always a
copy: a shadow image or a staged texture buffer whose bytes past the
length lay in a tail of its own. Metal's texels there are the
allocation's: a buffer of 16 KiB or more holds its length rounded up to
16 KiB (allocatedSize), zero when made, and a heap's buffer reaches its
heap's end, whose bytes are the heap's.

Now a buffer the device makes in the Metal address space with a length
of 16 KiB or more takes that allocation (buffer.rs's metal_allocation):
its VkBuffer and placement are its length rounded up to 16 KiB, which
its first fill clears with the rest (Buffer::clear_first: the host's
clear of a Shared or Managed buffer, the device's fill of a Private
one), with no command of its own. A texture keeps in place the bytes the
buffer's memory holds for it (Buffer::texel_reach: the allocation, a
heap's buffer's heap to its end, a no-copy buffer's length), so such a
texture is made as one inside the length is: an image over the buffer's
memory where the device makes one, a texture buffer viewed in place. The
shader-visible ranges, robustness, copy reach (a blit past the length is
still refused) and exported sizes stay at the length. Heap, no-copy,
wrapped and exportable buffers, and every buffer under 16 KiB, take
their length; past what the memory holds a texture keeps its tail, as
before. The runtime has no buffer allocatedSize; a heap's size and a heap
texture's allocated_size are unchanged.

The tails conformance cases now lie in place: their comments say so,
and the texture buffer case drops its staged-texture-buffer allowance,
which no device can fire there any more (its view starts at 16384).

Tests: buffer_tail_device's last-texel case lies as over a buffer that
holds it inside its length, with no tail; a new case over a 20000-byte
buffer (64x128 packed, ending at 32768) reads zero in the padding as
made, lies as inside the length, lands a kernel's writes past the length
in the buffer's memory, takes no copy where it is an image over the
buffer, and refuses a blit past the length; the heap case lies in the
heap, its kernel writes landing in the heap's other buffer; a new case
over a heap's second buffer keeps its texels past the heap's end in a
tail; the no-copy and 4096-byte cases keep their tails.

Docs: buffer_texture.rs's Tails, linear_plan.rs's Length, docs/API.md's
accepted difference and StagedTextureBuffer entry, and a FAST-PATHS
cost-of-matching-Metal entry for the padding.

Checks: the jobs that compiled and ran this commit are named in the
lane's report.

Evidence (lane jobs): m3-max-32551 (msi): clippy runtime+conformance -D warnings 0; 2632/2632 runtime+conformance; fast_paths +
api_surface 34/34. m3-max-32578 (lavapipe) at the tip: touched 56/56 (buffer_tail_device incl. the allocation, heap and
heap-end cases, texture_placement_device, linear_plan, buffer_texture, degradation_catalogue); runtime+validation partition
1/4 1572/1572. VkMemoryRequirements on lavapipe for a 16385-byte buffer: 32768 (alignment 64). The padding is cleared by the
buffer's existing first fill (no new command). allocatedSize: no buffer API; heap sizes unchanged. The PHOENIX run, the
release creation bench and lavapipe partitions 2-4 follow in the lane. Conformance: buffer-tails cases now in place; the M3
venue's cost records for buffer-tails-* re-bless on development.

Lane commits: d2ee5bca5b (bench), a8f3796f59, ee54952340 (heap cases in a placement heap).
2026-11-15 08:14:12 -08:00

132 lines
5.7 KiB
Rust

//! What new buffers of 16 KiB or more cost to make: a batch of [`COUNT`] buffers whose lengths
//! spread geometrically from 16 KiB + 1 to 4 MiB, `Shared` and `Private`, made, their first fills
//! run by one empty command buffer committed and waited for (a `Private` buffer's zeros are a
//! device fill its device's next submission records first, `texture/first_move.rs`; a `Shared`
//! one's are cleared on the host as it is made), and dropped, [`ROUNDS`] times. Printed as
//! `BUFFER-MAKING` lines: each round's making and fills, in milliseconds, the bytes the device
//! allocated (`WorkCounts::device_memory_allocated`) and the host cleared
//! (`new_buffer_bytes_cleared`), and the batch's lengths and `VkMemoryRequirements.size` summed;
//! the first round makes every buffer in new memory, the later ones take parked buffers
//! (`recycle.rs`). And `BUFFER-REQUIREMENTS` lines: a 16385-byte buffer's `VkBuffer`'s memory
//! requirements, and a plain `VkBuffer`'s of 16400 and of 32768 bytes on the same device. Run it
//! on two trees to compare them. Ignored: run it in release under the GPU safety cfg.
//!
//! Device tests: compiled only under the GPU safety cfgs (`m2v_gpu`, `m2v_cpu_icd`), on the kind
//! of device the cfg selects.
use super::blit_holds::{device, Scratch};
use crate::{Buffer, Device, ResourceOptions, WorkCounts};
use ash::vk;
use std::time::{Duration, Instant};
const WAIT: Duration = Duration::from_secs(120);
/// The buffers a batch makes, and the rounds.
const COUNT: u32 = 1000;
const ROUNDS: usize = 5;
/// The shortest and longest length of a batch.
const SHORTEST: u64 = (16 << 10) + 1;
const LONGEST: u64 = 4 << 20;
/// The batch's lengths: geometric from [`SHORTEST`] to [`LONGEST`].
fn lengths() -> Vec<u64> {
let ratio = (LONGEST as f64 / SHORTEST as f64).powf(1.0 / f64::from(COUNT - 1));
(0..COUNT)
.map(|at| ((SHORTEST as f64) * ratio.powi(at as i32)).round() as u64)
.map(|length| length.clamp(SHORTEST, LONGEST))
.collect()
}
/// `buffer`'s `VkBuffer`'s memory requirements.
fn requirements(device: &Device, buffer: &Buffer) -> vk::MemoryRequirements {
let (raw, _) = buffer.at(0);
// SAFETY: `raw` is the live buffer's `VkBuffer`, of this device.
unsafe { device.shared().vk().get_buffer_memory_requirements(raw) }
}
/// A plain `VkBuffer` of `size` bytes' memory requirements, of storage and transfer usage.
fn plain_requirements(device: &Device, size: u64) -> vk::MemoryRequirements {
let raw = device.shared().vk();
let usage = vk::BufferUsageFlags::STORAGE_BUFFER
| vk::BufferUsageFlags::TRANSFER_SRC
| vk::BufferUsageFlags::TRANSFER_DST;
let info = vk::BufferCreateInfo::default().size(size).usage(usage);
// SAFETY: the device is live and `info` valid.
let buffer = unsafe { crate::creation::buffer(raw, &info) }.unwrap();
// SAFETY: `buffer` is a live buffer of the device, unbound and unused.
let requirements = unsafe { raw.get_buffer_memory_requirements(buffer) };
// SAFETY: as above.
unsafe { raw.destroy_buffer(buffer, None) };
requirements
}
/// One round of `options`: the batch made, its fills run, then dropped.
fn round(device: &Device, options: ResourceOptions, lengths: &[u64]) -> String {
let queue = device.new_command_queue().unwrap();
let before = device.work_counts();
let started = Instant::now();
let batch: Vec<Buffer> = (lengths.iter())
.map(|&length| device.new_buffer(length, options).unwrap())
.collect();
let made = started.elapsed();
let started = Instant::now();
let command_buffer = queue.command_buffer().unwrap();
command_buffer.commit().unwrap();
command_buffer.wait_until_completed_for(WAIT).unwrap();
let filled = started.elapsed();
let after = device.work_counts();
let required: u64 = (batch.iter())
.map(|buffer| requirements(device, buffer).size)
.sum();
let lengths: u64 = batch.iter().map(Buffer::length).sum();
drop(batch);
let delta = |of: fn(&WorkCounts) -> u64| of(&after) - of(&before);
format!(
"made {:.2} ms, fills {:.2} ms, allocated {} bytes, host cleared {} bytes, lengths {} \
bytes, required {} bytes",
made.as_secs_f64() * 1e3,
filled.as_secs_f64() * 1e3,
delta(|counts| counts.device_memory_allocated),
delta(|counts| counts.new_buffer_bytes_cleared),
lengths,
required,
)
}
#[test]
#[ignore = "a release benchmark: run with --ignored --nocapture"]
fn new_buffers_of_16_kib_or_more() {
let scratch = Scratch::new("buffer-making-bench");
let device = device(&scratch.0);
let name = device.name();
for (storage, options) in [
("Shared", ResourceOptions::shared()),
("Private", ResourceOptions::private()),
] {
let buffer = device.new_buffer(SHORTEST, options).unwrap();
let made = requirements(&device, &buffer);
println!(
"BUFFER-REQUIREMENTS on {name}: a {SHORTEST}-byte {storage} buffer's VkBuffer: size \
{}, alignment {}",
made.size, made.alignment
);
}
for size in [SHORTEST.next_multiple_of(16), 32768] {
let plain = plain_requirements(&device, size);
println!(
"BUFFER-REQUIREMENTS on {name}: a plain VkBuffer of {size} bytes: size {}, \
alignment {}",
plain.size, plain.alignment
);
}
let lengths = lengths();
for (storage, options) in [
("Shared", ResourceOptions::shared()),
("Private", ResourceOptions::private()),
] {
for at in 0..ROUNDS {
let line = round(&device, options, &lengths);
println!("BUFFER-MAKING on {name}: {COUNT} {storage} buffers, round {at}: {line}");
}
}
}