From d9da03befceee7555171c4a5f547d0cdc7c45b4f Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Sun, 8 Nov 2026 14:55:14 -0800 Subject: [PATCH] Kept shadowed draws: a lane of draws that read their pass's copies, kept and apart; the docs say what a kept draw may use tests::kept_shadowed_draws: one pass over two private BGRA8Unorm targets draws tob_read over rectangles reading the first target's copy (the pass's first draw, a draw kept, a draw again), a draw rebound (vertex bytes bound again, its shadow the kept draw's), a draw of the second target's copy (another shadow: it takes every step), a draw through a scissor (its read's bounds another where the device bounds own-pixel reads: it takes every step) and draws again. The same frame runs again with setTessellationFactorScale: before each draw but the first, which keeps no draw. Both frames must answer Metal's reds, with the same shadow copies, texels copied in, reads through copies and at own pixels, splits and inline passes; the kept frame's draws keep all but the pass's first (by its instance alone: draws_unkept_shadowed 0), with its draws again, rebound and stopped counted as the build's checks leave them. The docs: WorkCounts::draws_unkept_shadowed, draws_as_before and draws_rebound, and the draw-again and rebound module docs, name what a kept draw may use now. Verification: to follow. (cherry picked from commit 6ad8d65e2effad074f67868ed0d62f3a050e0b55) --- .../src/render/draw_again.rs | 3 +- .../src/render/draw_again/rebound.rs | 20 +- .../src/tests/kept_shadowed_draws.rs | 299 ++++++++++++++++++ crates/metal2vulkan-runtime/src/tests/mod.rs | 2 + .../metal2vulkan-runtime/src/work_counts.rs | 26 +- 5 files changed, 333 insertions(+), 17 deletions(-) create mode 100644 crates/metal2vulkan-runtime/src/tests/kept_shadowed_draws.rs diff --git a/crates/metal2vulkan-runtime/src/render/draw_again.rs b/crates/metal2vulkan-runtime/src/render/draw_again.rs index 4386894cf..7c0520c6c 100644 --- a/crates/metal2vulkan-runtime/src/render/draw_again.rs +++ b/crates/metal2vulkan-runtime/src/render/draw_again.rs @@ -73,7 +73,8 @@ //! holds, and adding the same again changes nothing: its drawn area and coverage (rectangles //! and boxes, `clear_reach::DrawnArea`, `linear_attachment::Coverage`), the pass's reads //! (`PassStartReads::add`, which takes an equal range once) and the instance's gathered uses -//! (`InstanceShadows::add`, which skips the uses of the last draw), its writes (none), its noted +//! (`InstanceShadows::add`, which skips the uses of the last draw and joins its shadowed +//! textures' as nothing new), its writes (none), its noted //! uses, its retains (each a hold of an object held already: `LastRetained`, a table's stamp, //! an index buffer the kept draw held), the shape memo's entries and the bind state (found as //! the kept draw left them), and the pass's first instance, which the kept draw decided diff --git a/crates/metal2vulkan-runtime/src/render/draw_again/rebound.rs b/crates/metal2vulkan-runtime/src/render/draw_again/rebound.rs index 1b5e01ecf..4834205fd 100644 --- a/crates/metal2vulkan-runtime/src/render/draw_again/rebound.rs +++ b/crates/metal2vulkan-runtime/src/render/draw_again/rebound.rs @@ -25,16 +25,21 @@ //! a texture's view and a sampler state's `VkSampler` as every step makes them, through its //! encoder's memo), and the set its encoder's last set or its command buffer's holds, else a new //! one (`EncoderCore::set_zero_of`); its push bytes and vertex buffers; and its uses, whole -//! (`binding::command_uses_into`). And the checks a draw of changed tables takes: a pulled +//! (`binding::command_uses_into`), a texture of a color attachment of its pass bound through its +//! copy (`texture/attached_reads.rs`); and its uses of shadowed textures +//! (`RenderLowering::draw_shadows`). And the checks a draw of changed tables takes: a pulled //! attribute's reach, the stages' function tables, the attachments' reads. Each is the function //! every step of the draw runs, on the same inputs. //! - **Where it stops.** At a changed shape or runtime samplers' address modes, a plan that reads a //! changed slot some other way (a texture buffer, a YCbCr texture's planes, an argument sampler), //! a refusal of any of those steps, a pass whose fragments wrote memory (a stage before them may //! read a copy of it), a changed texture where its functions use a memoryless texture, the plan -//! samples or writes a color attachment's texture, or an extended-range texture binds through a -//! shadow, a shadowed texture among its uses, or a barrier its fragments need after an earlier -//! draw's geometry writes: the draw takes every step, which answers as it would have. Only the +//! writes a color attachment's texture, or an extended-range texture binds through a shadow, a +//! read of a color its pass clears behind its load actions, uses of shadowed textures other than +//! the kept draw's (another shadow, or one used otherwise: written, or its reads bounded +//! elsewhere, as a scissor restated bounds a read at the fragments' own pixels), or a barrier +//! its fragments need after an earlier draw's geometry writes: the draw takes every step, which +//! answers as it would have. Only the //! attachments' reads' refusal is answered here, the last of its checks, which may have //! announced. //! - **What it records and notes.** Its uses noted as any draw notes them (`note_reaching`, the @@ -54,7 +59,9 @@ //! - **A draw moved.** Where every call since the kept draw moved a buffer slot's offset (or //! attribute stride) alone, the slot binding the buffer it bound (`argument_table::Changed`), the //! draw is moved (`WorkCounts::draws_moved`): its tables bind the kept draw's objects, so it -//! looks at no stage's function tables, shadowed texture or detached buffer again; and where its +//! looks at no stage's function tables or detached buffer again, nor at shadowed textures where +//! the kept draw used none (where it used some, its reads' bounds may follow a scissor +//! restated, so it works them out as a draw rebound does); and where its //! commands' descriptor ranges are the kept draw's, it keeps the kept draw's set-0 writes and //! dynamic offsets (`binding::rebind_into` writes a buffer's descriptor of its buffer and range //! alone): where nothing recorded through the record since the kept draw, the record holds that @@ -75,7 +82,8 @@ //! kept draw and every second after it work out the rebind, then take every step instead, which //! must keep the draw, with the same commands, set-0 writes, dynamic offsets, push bytes, vertex //! buffers and uses. What a draw moved skips is asserted where it skips it: its function tables -//! pass, it meets no shadow and no detached buffer, it pushes the kept draw's set-0 writes, a set +//! pass, it meets no shadow where its kept draw met none and no detached buffer, it pushes the +//! kept draw's set-0 writes, a set //! 0 it pushes or binds none of is the record's, its uses are unwritten in a pass whose //! fragments wrote nothing, and of a draw narrowed, its other uses share no attachment memory, //! meet no geometry write the moved ones do not, add nothing to the pass's reads, are noted and diff --git a/crates/metal2vulkan-runtime/src/tests/kept_shadowed_draws.rs b/crates/metal2vulkan-runtime/src/tests/kept_shadowed_draws.rs new file mode 100644 index 000000000..9f2e747e2 --- /dev/null +++ b/crates/metal2vulkan-runtime/src/tests/kept_shadowed_draws.rs @@ -0,0 +1,299 @@ +//! Draws that read the texture of a color attachment of their own pass through its copy +//! (`texture/attached_reads.rs`), kept to draw again (`render/draw_again.rs`): the shadowed +//! textures a kept draw used are gathered by its instance, so a draw again, or a draw rebound that +//! uses the same shadows each as the kept draw used it, gathers nothing new, and a draw rebound +//! whose shadows differ takes every step. +//! +//! One pass over two private `BGRA8Unorm` targets, both loaded and stored, both cleared before by +//! a pass of their own to a red of their own, draws a rectangle with `tob_read` (each fragment +//! writes into the first target the texel of its slot-0 texture under it): the first target's +//! texels over [`FIRST_RECT`], three times (the pass's first draw, kept nothing by its instance; +//! the next, kept; the next, drawn again); over [`SECOND_RECT`], its vertex bytes bound again (a +//! draw rebound, its shadow the kept draw's); the second target's texels there (its shadow +//! another: it takes every step); through the scissor [`SCISSOR`] (its read's bounds another, +//! where the device bounds a read at its own pixels: it takes every step); and twice again. The +//! same frame runs again with a call that keeps no draw before each draw but the first +//! (`setTessellationFactorScale:`, which no draw of the pass reads), so each takes every step. +//! +//! Both frames answer as Metal does, the first target's texels from before the pass where its +//! own were read and the second's over [`SECOND_RECT`], with the same copies (the shadow copies, +//! the texels they move, the reads through copies and at the fragments' own pixels, the splits); +//! and the first frame's draws are kept, none for its shadows, so its draws again and rebound are +//! counted where the second frame has none. +//! +//! Device tests: compiled only under the validation crate's GPU safety cfgs (`m2v_gpu`, +//! `m2v_cpu_icd`), on the kind of device the cfg selects. The functions are `tob_rect` and +//! `tob_read` of the owned fixture `validation/fixtures/public/engine/tob_renders.ll`. + +use super::blit_holds::{device, Scratch, WAIT}; +use super::encoder_holds::{bytes_of, library}; +use crate::{ + BlitOption, ClearColor, CommandQueue, Device, LoadAction, Origin, PipelineOptions, + RenderPassDescriptor, RenderPipelineState, ResourceOptions, ScissorRect, Size, StoreAction, + Texture, TextureDescriptor, WorkCounts, +}; +use metal2vulkan::metal::{PixelFormat, PrimitiveType, TextureUsage}; +use metal2vulkan::pipeline::{ColorAttachmentDescriptor, RenderPipelineDescriptor, StageSource}; + +/// The targets' side, in pixels, and format. +const SIDE: u64 = 32; +const FORMAT: PixelFormat = PixelFormat::BGRA8Unorm; + +/// The reds the targets are cleared to. +const FIRST_RED: u8 = 0x40; +const SECOND_RED: u8 = 0xc0; + +/// The rectangles the draws cover, `[x0, y0, x1, y1]`. +const FIRST_RECT: [u64; 4] = [0, 0, 16, 16]; +const SECOND_RECT: [u64; 4] = [8, 4, 24, 20]; + +/// The scissor of the last draws, inside [`SECOND_RECT`]. +const SCISSOR: ScissorRect = ScissorRect { + x: 12, + y: 8, + width: 8, + height: 8, +}; + +/// The state of `tob_rect` and `tob_read` into two colors of [`FORMAT`]. +fn state(device: &Device) -> RenderPipelineState { + let function = |name: &str| { + let function = library(device, "engine/tob_renders.ll") + .new_function_with_name(name) + .unwrap(); + StageSource::Function(function.function_descriptor()) + }; + let mut descriptor = RenderPipelineDescriptor::new(function("tob_rect")); + descriptor.fragment = Some(function("tob_read")); + for color in &mut descriptor.color_attachments[..2] { + *color = Some(ColorAttachmentDescriptor::new(FORMAT)); + } + device + .new_render_pipeline_state(&descriptor, &PipelineOptions::default()) + .unwrap() +} + +/// A private [`FORMAT`] render target that shaders read. +fn target(device: &Device) -> Texture { + let mut descriptor = TextureDescriptor::texture_2d(FORMAT, SIDE, SIDE, false); + descriptor.usage = TextureUsage::RENDER_TARGET | TextureUsage::SHADER_READ; + descriptor.resource_options = ResourceOptions::private(); + device.new_texture(&descriptor).unwrap() +} + +/// The corners, in normalized device coordinates, of the targets' pixels `[x0, x1) x [y0, y1)`. +fn corners([x0, y0, x1, y1]: [u64; 4]) -> Vec { + let x = |p: u64| p as f32 / SIDE as f32 * 2.0 - 1.0; + let y = |p: u64| 1.0 - p as f32 / SIDE as f32 * 2.0; + bytes_of(&[x(x0), y(y1), x(x1), y(y0)], f32::to_le_bytes) +} + +/// A pass descriptor over `targets`, each loaded `load` (cleared to its red) and stored. +fn over(targets: &[Texture; 2], load: LoadAction) -> RenderPassDescriptor { + let mut descriptor = RenderPassDescriptor::new(); + for ((color, target), red) in (descriptor.color_attachments.iter_mut()) + .zip(targets) + .zip([FIRST_RED, SECOND_RED]) + { + color.texture = Some(target.clone()); + color.load_action = load; + color.clear_color = ClearColor::new(f64::from(red) / 255.0, 0.0, 0.0, 1.0); + color.store_action = StoreAction::Store; + } + descriptor +} + +/// The red of each of `texture`'s pixels, by row. +fn reds(queue: &CommandQueue, texture: &Texture) -> Vec { + let row = SIDE * 4; + let back = (queue.device()) + .new_buffer(SIDE * row, ResourceOptions::shared()) + .unwrap(); + let command_buffer = queue.command_buffer().unwrap(); + let mut blit = command_buffer.blit_command_encoder().unwrap(); + (blit.copy_from_texture_to_buffer( + texture, + 0, + 0, + Origin::new(0, 0, 0), + Size::new(SIDE, SIDE, 1), + &back, + 0, + row, + SIDE * row, + BlitOption::None, + )) + .unwrap(); + blit.end_encoding().unwrap(); + command_buffer.commit().unwrap(); + command_buffer.wait_until_completed_for(WAIT).unwrap(); + let texels = super::merge_scratch::read(&back, SIDE * row); + // `BGRA8Unorm`'s red is its third byte. + texels.chunks(4).map(|texel| texel[2]).collect() +} + +/// The frame over two new targets (the module docs), each draw but the first after a call that +/// keeps no draw where `apart`: the device's counts before and after the pass, and the first +/// target's reds and the second's after it. +fn frame( + queue: &CommandQueue, + state: &RenderPipelineState, + apart: bool, +) -> ((WorkCounts, WorkCounts), [Vec; 2]) { + let device = queue.device(); + let targets = [target(device), target(device)]; + let command_buffer = queue.command_buffer().unwrap(); + let clears = command_buffer.render_command_encoder(&over(&targets, LoadAction::Clear)); + clears.unwrap().end_encoding().unwrap(); + command_buffer.commit().unwrap(); + command_buffer.wait_until_completed_for(WAIT).unwrap(); + let before = device.work_counts(); + let command_buffer = queue.command_buffer().unwrap(); + let pass = over(&targets, LoadAction::Load); + let mut encoder = command_buffer.render_command_encoder(&pass).unwrap(); + let mut drawn = 0; + let mut draw = |encoder: &mut crate::RenderCommandEncoder| { + if apart && drawn > 0 { + encoder.set_tessellation_factor_scale(1.0); + } + (encoder.draw_primitives(PrimitiveType::Triangle, 0, 6, 1, 0)).unwrap(); + drawn += 1; + }; + encoder.set_render_pipeline_state(state).unwrap(); + encoder.set_vertex_bytes(&corners(FIRST_RECT), 0).unwrap(); + (encoder.set_fragment_texture(Some(&targets[0]), 0)).unwrap(); + for _ in 0..3 { + draw(&mut encoder); + } + encoder.set_vertex_bytes(&corners(SECOND_RECT), 0).unwrap(); + draw(&mut encoder); + (encoder.set_fragment_texture(Some(&targets[1]), 0)).unwrap(); + draw(&mut encoder); + encoder.set_scissor_rect(SCISSOR).unwrap(); + for _ in 0..3 { + draw(&mut encoder); + } + encoder.end_encoding().unwrap(); + command_buffer.commit().unwrap(); + command_buffer.wait_until_completed_for(WAIT).unwrap(); + assert_eq!(command_buffer.error(), None); + let after = device.work_counts(); + let reds = targets.each_ref().map(|target| reds(queue, target)); + ((before, after), reds) +} + +/// What a frame's pass moved of the counts between `before` and `after`. +#[derive(Debug, PartialEq, Eq)] +struct Copies { + /// `WorkCounts::shadow_copies`, `shadow_texels_copied_in`, `attachment_reads_through_copies`, + /// `own_pixel_reads`, `attachment_read_splits` and `render_passes_begun_inline`. + shadow_copies: u64, + copied_in: u64, + through_copies: u64, + own_pixel: u64, + splits: u64, + inline: u64, +} + +/// What a frame's pass kept, by the same counts: `draws_unkept`, `draws_unkept_shadowed`, +/// `draws_as_before`, `draws_rebound` and `draws_rebind_stopped`. +#[derive(Debug, PartialEq, Eq)] +struct Keeps { + unkept: u64, + unkept_shadowed: u64, + as_before: u64, + rebound: u64, + stopped: u64, +} + +fn moved((before, after): &(WorkCounts, WorkCounts)) -> (Copies, Keeps) { + let delta = |count: fn(&WorkCounts) -> u64| count(after) - count(before); + let copies = Copies { + shadow_copies: delta(|c| c.shadow_copies), + copied_in: delta(|c| c.shadow_texels_copied_in), + through_copies: delta(|c| c.attachment_reads_through_copies), + own_pixel: delta(|c| c.own_pixel_reads), + splits: delta(|c| c.attachment_read_splits), + inline: delta(|c| c.render_passes_begun_inline), + }; + let keeps = Keeps { + unkept: delta(|c| c.draws_unkept), + unkept_shadowed: delta(|c| c.draws_unkept_shadowed), + as_before: delta(|c| c.draws_as_before), + rebound: delta(|c| c.draws_rebound), + stopped: delta(|c| c.draws_rebind_stopped), + }; + (copies, keeps) +} + +/// The first target's reds after the frame: its own from before the pass, but the second +/// target's over [`SECOND_RECT`], which the draws of its texels covered last there. +fn first_reds() -> Vec { + let [x0, y0, x1, y1] = SECOND_RECT; + (0..SIDE * SIDE) + .map(|at| { + let (x, y) = (at % SIDE, at / SIDE); + match (x0..x1).contains(&x) && (y0..y1).contains(&y) { + true => SECOND_RED, + false => FIRST_RED, + } + }) + .collect() +} + +#[test] +fn draws_that_read_their_pass_s_copies_are_kept_with_the_copies_of_every_step() { + let scratch = Scratch::new("kept-shadowed-draws"); + let device = device(&scratch.0); + let queue = device.new_command_queue().unwrap(); + let state = state(&device); + let (kept_counts, kept_reds) = frame(&queue, &state, false); + let (apart_counts, apart_reds) = frame(&queue, &state, true); + let want = [first_reds(), vec![SECOND_RED; (SIDE * SIDE) as usize]]; + for (frame, reds) in [("kept", &kept_reds), ("apart", &apart_reds)] { + for (target, (reds, want)) in reds.iter().zip(&want).enumerate() { + if let Some(at) = (0..reds.len()).find(|&at| reds[at] != want[at]) { + let (x, y) = (at as u64 % SIDE, at as u64 / SIDE); + panic!( + "{frame} frame: target {target}'s pixel ({x}, {y}) holds red {}, not {}", + reds[at], want[at] + ); + } + } + } + let (kept_copies, kept) = moved(&kept_counts); + let (apart_copies, apart) = moved(&apart_counts); + eprintln!( + "KEPT-SHADOWED-DRAWS {}: kept {kept_copies:?} {kept:?}; apart {apart_copies:?} {apart:?}", + device.name() + ); + // The same copies, every draw a read through a copy. + assert_eq!(kept_copies, apart_copies, "the kept frame's copies"); + assert_eq!(kept_copies.through_copies, 8, "the reads through copies"); + // The pass's first draw keeps nothing, by its instance alone. A draw again, or rebound, the + // first after a draw kept and every second after it takes every step in builds with debug + // assertions (`render/draw_again.rs`): of the draws again after the third draw kept, the + // first; of those after the last, the first. The scissor's draw is stopped where the device + // bounds the reads at their own pixels, as its read's bounds change, else rebound. + let bounded = kept_copies.own_pixel > 0; + let debug = cfg!(debug_assertions); + let want = Keeps { + unkept: 1, + unkept_shadowed: 0, + as_before: match debug { + true => 1, + false => 3, + }, + rebound: 1 + u64::from(!bounded && !debug), + stopped: 1 + u64::from(bounded), + }; + assert_eq!(kept, want, "the kept frame's keeps on {}", device.name()); + let apart_want = Keeps { + unkept: 1, + unkept_shadowed: 0, + as_before: 0, + rebound: 0, + stopped: 0, + }; + assert_eq!(apart, apart_want, "the apart frame's keeps"); +} diff --git a/crates/metal2vulkan-runtime/src/tests/mod.rs b/crates/metal2vulkan-runtime/src/tests/mod.rs index bfe445f59..1c411b319 100644 --- a/crates/metal2vulkan-runtime/src/tests/mod.rs +++ b/crates/metal2vulkan-runtime/src/tests/mod.rs @@ -256,6 +256,8 @@ mod kept_heap_uses; #[cfg(any(m2v_gpu, m2v_cpu_icd))] mod kept_images; #[cfg(any(m2v_gpu, m2v_cpu_icd))] +mod kept_shadowed_draws; +#[cfg(any(m2v_gpu, m2v_cpu_icd))] mod layer_claim; #[cfg(any(m2v_gpu, m2v_cpu_icd))] mod learned_coverage; diff --git a/crates/metal2vulkan-runtime/src/work_counts.rs b/crates/metal2vulkan-runtime/src/work_counts.rs index 3e71955eb..88c1fc883 100644 --- a/crates/metal2vulkan-runtime/src/work_counts.rs +++ b/crates/metal2vulkan-runtime/src/work_counts.rs @@ -5950,20 +5950,24 @@ pub struct WorkCounts { /// generation, the device's host aliases kept their version and the encoder kept its parts /// and held images, after a draw that recorded one Vulkan draw into the instance as it /// stood, through a `VkPipeline` made ahead, with no barrier in its instance, no write among - /// its uses, no copy, shadow, snapshot, split, gather, index bounds or rewrite pass, mesh or - /// tessellation work, set 1, root table, rate map, color group or extended-range color, and - /// no notice the device announces at every occurrence (`render/draw_again.rs`). Each is - /// counted in `commands_noted_as_before`, `draws_pushing_set_zero` and - /// `provoking_last_pipeline_draws` as the draw it repeats was, and in no count of work it - /// does not take (`draw_shapes_reused`, `set_zero_sets_reused`, `draw_states_recorded`). + /// its uses, no copy, snapshot, split, gather, index bounds or rewrite pass, mesh or + /// tessellation work, set 1, root table, rate map, color group or extended-range color, no + /// read of a color its pass clears behind its load actions, and no notice the device + /// announces at every occurrence (`render/draw_again.rs`); its shadowed textures, where it + /// used any, gathered by its instance already. Each is counted in `commands_noted_as_before`, + /// `draws_pushing_set_zero`, `provoking_last_pipeline_draws`, + /// `attachment_reads_through_copies` and `own_pixel_reads` as the draw it repeats was, and in + /// no count of work it does not take (`draw_shapes_reused`, `set_zero_sets_reused`, + /// `draw_states_recorded`). pub draws_as_before: u64, /// Render encoders' direct draws recorded as their encoder's last draw again after calls /// that bound other buffers, offsets or bytes at buffer slots, or other textures or sampler /// states at texture and sampler slots, rather than taking every step of a draw: a draw that /// `draws_as_before` would count but for those calls, where the bind state kept its shape and /// its runtime samplers' address modes, the slots are read through set-0 descriptors - /// (dynamic or pushed), push fields or fixed-function vertex buffers alone, and the uses they - /// make need no barrier, copy or shadow (`render/draw_again/rebound.rs`). Each reads its + /// (dynamic or pushed), push fields or fixed-function vertex buffers alone, the uses they make + /// need no barrier or copy, and the draw uses the shadowed textures the kept draw used, each + /// as it used it (`render/draw_again/rebound.rs`). Each reads its /// changed stage tables in part (`stage_tables_read_in_part`), writes again only those slots' /// descriptors, dynamic offsets, push fields and vertex buffers, notes its uses as any draw /// does, and records the state commands that changed (`draw_states_recorded`) and its draw; @@ -6010,8 +6014,10 @@ pub struct WorkCounts { pub draws_unkept_instance_steps: u64, /// Of `draws_unkept`, those that recorded after a barrier in their render pass instance. pub draws_unkept_barrier: u64, - /// Of `draws_unkept`, those with a shadowed texture among their uses, or that read through a - /// snapshot or an extended-range color's shadow. + /// Of `draws_unkept`, those that read a color their pass clears behind its load actions + /// through a copy (`texture/attached_reads.rs`), or that read through a snapshot or an + /// extended-range color's shadow. A draw that uses other shadowed textures is kept: its + /// instance gathered them (`render/draw_again.rs`). pub draws_unkept_shadowed: u64, /// Of `draws_unkept`, those that read a heap's set, a texture or sampler table, a root table /// or a push hole, or whose argument samplers left states out.