Files
metal2vulkan/crates/metal2vulkan-runtime/src/tests/depth_ranges.rs
T
Anees Iqbal 86b5d6faf3 Cut the encoding thread's allocations, locks and copies per draw and per render pass
A draw, a pass open and a pass end on the encoding thread now do far less work that no answer
needs. Every change records the same commands in the same order, retains and lets go of the same
objects at the same points, and announces the same notices with the same text.

What changes:
- Kept objects. The device keeps every VkRenderPass it makes, by everything it is made of (at most
  256, destroyed with the device), so passes alike make none (WorkCounts::render_passes_made). A
  pipeline state keeps the last four pipelines its draws found, not only the last one. A device's
  depth-range facts are read once at its making. A pass layout's kept-attachment notices
  (extended-range-bits-kept, snorm-minimum-kept, shared-exponent-kept) are decided once per
  layout, dropped wherever the layout changes, and checked against a fresh decision at every draw
  in debug builds.
- Storage reused. Each draw's commands (push bytes and writes, descriptor ranges, extents, uses)
  are written over the last draw's storage; push field bytes sit in place up to 32 bytes; a draw's
  host-memory uses go into a vector its encoder keeps; the pass layout and pass-start tables move
  as boxes, not 700-byte values.
- Lists in place. A pass's shape lists, default viewport and scissor, first drawn rectangle,
  colour sets (as bit masks), clear values and attachment extents are held in place, bounded by the
  eight colour attachments; a pass end makes no list it would leave empty; a draw's attachment
  reads are checked one attachment at a time; a command buffer's dynamic states are bits.
- Fewer locks. A render encoder retains its own argument tables through its exclusive borrow, with
  no lock; set 0 is allocated under the bind's lock; the settled variant and plan move out instead
  of cloning; a draw's pipeline state, variant objects, VkPipeline and samplers are retained once
  per encoder, the first eight of each kind in place. A draw's use keys are kept and named only
  where the last command's use there took another key. The latest used memo entry stays where it
  lies when it is used again.
- Fewer commands. A stencil state whose front and back values match is set by one command with
  VK_STENCIL_FACE_FRONT_AND_BACK, which the Vulkan specification defines as updating both faces.
- The recorded-state checks are #[inline], so their cost no longer depends on which codegen unit
  holds them.
- runtime_lane::frame_costs' allocation bounds drop to the new counts (pass open 14 -> 4.5, pass
  end 40 -> 19, a draw's bounds with them) and fail without these cuts. The pass-open bound allows
  one more allocation on Apple's platforms, where Rust's standard library boxes a mutex at its
  first lock.

Why: the consumer's guest is bound by its encoding thread. Its task thread spent 6.61 ms a frame
in draws and 1.77 ms in pass ends, and allocation, B-tree lookups, memmoves and locked instructions
were large shares of both.

Verified:
- Allocations (counting allocator): on the RX 7900 XTX (RADV), a guest-shaped pass's draw 39.70 ->
  32.70 for the reused commands and a pass open 11 -> 5 for the lists in place; on the UHD 770
  (ANV), a pass end 22.43 -> 16.91 and a pass open 5.00 -> 4.00, the same on lavapipe (debug).
- User-space instructions, release A/B medians: render passes kept, a frame -1.9% on the Iris Xe
  (about 113 vkCreateRenderPass2 calls a frame before, none after); the boxed moves and depth-range
  facts about -2% a draw each on the RX 7900 XTX and -1.2..-1.5% on the UHD 770; the kept notices
  -0.9% a draw on the UHD 770; locked instructions per process 22.45M -> 21.73M on the UHD 770.
- Unit tests: field_bytes_hold_what_they_were_made_of, equal_faces_take_one_stencil_command,
  depth_ranges, using_the_latest_entry_again_keeps_the_order, and the graphics binder's tests
  checking every instantiation written over earlier commands equals a fresh one. Lane tests in
  draw_work (a pass drawing as the one before makes no render pass; a state switching keys finds
  its pipelines again).
- On lavapipe (debug): the runtime unit suite 1066 passed; the stand-in, extended, split, inline,
  tile, reformat, snapshot, held-depth, feedback, instance, render-pass and frame-cost lanes 228 of
  234 (the 6 failures passed when rerun alone with the validation binaries built). The notices'
  lanes 38 passed on lavapipe (debug, the assertion at every draw) and on the UHD 770. The
  pass-open bound passed on the M3 Max under MoltenVK.
- clippy -D warnings (plain, m2v_gpu, m2v_cpu_icd) and rustdoc.
2026-10-19 00:15:59 +03:00

36 lines
1.5 KiB
Rust

//! A device's depth-range facts (`render_state::DepthRanges`), read once at its making.
use super::image_plan::committed;
use crate::render_state::DepthRanges;
use metal2vulkan::device::{VkExtension, VkFeature};
/// Each committed profile's facts are its enabled sets' answers: the native range where it enabled
/// `VK_EXT_depth_range_unrestricted`, `depthClamp`, `depthClipEnable` and `depthClampControl`,
/// the unrestricted range where it enabled the extension. The profiles hold devices of the native
/// range, of the extension alone and of neither, so draws on each take the facts their device's
/// profile has, never another's.
#[test]
fn a_device_s_depth_range_facts_are_its_profile_s() {
let mut seen = Vec::new();
for (name, _, device) in committed() {
let enabled = device.profile().enabled();
let unrestricted = (enabled.extensions).contains(&VkExtension::ExtDepthRangeUnrestricted);
let features = [
VkFeature::DepthClamp,
VkFeature::DepthClipEnable,
VkFeature::DepthClampControl,
];
let native = unrestricted && features.iter().all(|f| enabled.features.contains(f));
let facts = DepthRanges::of(device.profile());
assert_eq!(
(facts.native, facts.unrestricted),
(native, unrestricted),
"{name}"
);
seen.push((native, unrestricted));
}
for kind in [(true, true), (false, true), (false, false)] {
assert!(seen.contains(&kind), "a committed profile of {kind:?}");
}
}