diff --git a/crates/metal2vulkan-runtime/src/reader_sizes.rs b/crates/metal2vulkan-runtime/src/reader_sizes.rs index ef0678a32..9a727180c 100644 --- a/crates/metal2vulkan-runtime/src/reader_sizes.rs +++ b/crates/metal2vulkan-runtime/src/reader_sizes.rs @@ -8,9 +8,9 @@ //! A draw's share is its scissors' pixels for each color it reads so, over the pixels of those //! colors' levels, in steps of 0.5% (one step at 100% and above); its texel bytes are those //! colors' bytes over their pixels, rounded down: a color's own format's bytes for a draw that -//! reads one color, or colors of one width. The rule takes the snapshot where the share times -//! the snapshot's cost per pixel is at most the texel bytes, so the line shows how many draws lie -//! near the rule's bound for each width. +//! reads one color, or colors of one width. The rule's bound on the share +//! (`render/snapshot.rs`'s area rule) turns on the texel bytes and on the colors' size, so the +//! line shows how many draws lie near it for each width. use std::collections::BTreeMap; use std::fmt::Write; diff --git a/crates/metal2vulkan-runtime/src/render.rs b/crates/metal2vulkan-runtime/src/render.rs index ca60a9c20..dbd38f3fb 100644 --- a/crates/metal2vulkan-runtime/src/render.rs +++ b/crates/metal2vulkan-runtime/src/render.rs @@ -456,6 +456,8 @@ use replay::{ DrawPatchesIndirect, Samplers, }; pub(crate) use replay::{RenderCall, RenderLog, RenderRoom, Table}; +#[cfg(test)] +pub(crate) use snapshot::snapshot_within; /// What a draw, or the render pipeline state or tessellation factors set for the draws, did /// wrong. diff --git a/crates/metal2vulkan-runtime/src/render/snapshot.rs b/crates/metal2vulkan-runtime/src/render/snapshot.rs index 57f41d09c..c181782c2 100644 --- a/crates/metal2vulkan-runtime/src/render/snapshot.rs +++ b/crates/metal2vulkan-runtime/src/render/snapshot.rs @@ -83,11 +83,9 @@ //! pins (below), no two of the colors of one image (whose //! snapshot is one); the draw costs no more so than the split would //! (`RenderCommandEncoder::snapshot_cheaper`: at any area where every such color is a texture -//! whose image lies over its buffer's bytes, `RenderCommandEncoder::over_bytes`; else its -//! scissors' area times [`SNAPSHOT_PIXEL_COST`] for each color against each color's level -//! times its texel bytes, whatever the snapshot's form, so a 4-byte color's reader, -//! `RGBA8Unorm`'s or `BGRA8Unorm`'s, takes the snapshot up to a fifth of the render area, an -//! `RGBA16Float` one's up to two fifths and an `RGBA32Float` one's up to four fifths); the binder keeps every such slot (in a pass with no depth or stencil attachment, on a device that also enabled +//! whose image lies over its buffer's bytes, `RenderCommandEncoder::over_bytes`; else by the +//! rule below); the binder keeps every such slot (in a pass with no depth or stencil +//! attachment, on a device that also enabled //! `fragmentStoresAndAtomics`) and the variant is made; no slot takes the snapshot slot as //! storage. The draw's instance holds those colors in the feedback loop layout from its render //! pass instance's begin, which records in a secondary: where the current instance @@ -98,6 +96,26 @@ //! (`WorkCounts::snapshot_inline_splits` counts the first cause). After it, an execution of //! render commands, and a draw that binds such a color's image as a storage image, record into //! another instance: the feedback loop layout serves neither. +//! - **The area rule.** Which form costs less turns on the share of its scissors the draw covers, +//! which its recording cannot know: the snapshot draw runs at the pixels the draw covers, the +//! split copies the color whatever the draw covers. The rule weighs each form at the share least +//! favourable to it, in bytes of the split's copy: the snapshot at [`READER_COST`] and +//! [`SNAPSHOT_PIXEL_COST`] at each pixel its scissors bound, for each color (the draw covering +//! them whole), the split at [`READER_COST`] and its copy of each color's level, its texel bytes +//! each, whatever the snapshot's form (the draw covering nothing). It takes the form whose cost +//! there is the smaller multiple of the other form's: the snapshot where `READER_COST * +//! (READER_COST + pixels * SNAPSHOT_PIXEL_COST) <= (READER_COST + bytes)^2`. A reader whose +//! snapshot costs no more than the split's copy where it covers its scissors whole takes it, as +//! does a reader through scissors of a large color whatever it covers: a 4-byte color's reader, +//! `RGBA8Unorm`'s or `BGRA8Unorm`'s, takes the snapshot through scissors of up to two fifths of +//! its level at any size, and through the whole level from 786,432 pixels (1024x768), an +//! `RGBA16Float` one's through four fifths, and its whole level from 65,536 pixels (256x256), +//! and an `RGBA32Float` one's at any area. On the Iris Xe, the Radeon 780M and the RX 7900 XTX +//! (`runtime_lane::attachment_read_costs::snapshot_area_costs`, 30 readers a frame over private +//! `BGRA8Unorm` targets of 1280x720 to 3662x2039, no scissor) a reader over 1.5% of the target +//! took 0.09 to 0.43 times the split's GPU time, and one over the whole target 1.04 to 1.28 +//! times on the Iris Xe, 2.4 to 2.8 times on the Radeon 780M and 1.3 to 2.1 times on the RX 7900 +//! XTX. //! - **Pins.** A fetch of a color in the pixel interlock reads and writes its image as a storage //! image, so a later fetch of a color whose image lacks `STORAGE` renders it through a stand-in //! (`attachment_stand_in.rs`), which the pass's end puts in place for the whole pass, copied in @@ -160,14 +178,35 @@ pub(super) fn form(plan: &BindingPlan, slot: u32) -> Option { plan.variant.snapshot_textures.get(&slot).copied() } -/// What the snapshot draw costs at each pixel a draw's scissors bound, for each color it -/// snapshots, in bytes of the split's copy (`RenderCommandEncoder::snapshot_cheaper`): one more -/// fragment invocation at the pixel, inside the pixel interlock, that reads the texel and stores -/// its snapshot, and the drawing draw's read of it. The invocation's cost, not the snapshot's -/// bytes: a snapshot of a color's codes, 4 bytes a pixel, costs about what one of the fetched -/// lanes' bits, 16, does. +/// What the snapshot draw costs at each pixel a draw covers, for each color it snapshots, in +/// bytes of the split's copy (the module docs' area rule): one more fragment invocation at the +/// pixel, inside the pixel interlock, that reads the texel and stores its snapshot, and the +/// drawing draw's read of it. The invocation's cost, not the snapshot's bytes: a snapshot of a +/// color's codes, 4 bytes a pixel, costs about what one of the fetched lanes' bits, 16, does. +/// Measured at about 14 on the Iris Xe and 20 on the Radeon 780M +/// (`runtime_lane::attachment_read_costs::snapshot_area_costs`). const SNAPSHOT_PIXEL_COST: u64 = 20; +/// What either form costs a draw whatever it covers, in bytes of the split's copy (the module +/// docs' area rule): the snapshot's two barriers and its second draw, or the split's end of one +/// render pass instance and begin of the next. Measured +/// (`runtime_lane::attachment_read_costs::snapshot_area_costs`) at 12 to 16 microseconds a +/// snapshot on the Iris Xe and the Radeon 780M, and up to 18 a split besides its copy, which moves +/// a byte in 9 to 12 picoseconds on the Iris Xe and 15 on the Radeon 780M: about a mebibyte of +/// it. +const READER_COST: u64 = 1 << 20; + +/// Whether a draw takes the snapshot by the module docs' area rule: its scissors bound `pixels` +/// pixels over all the colors it reads so, of `bytes` bytes in all in the colors' levels. +pub(crate) fn snapshot_within(pixels: u64, bytes: u64) -> bool { + let (fixed, pixels, bytes) = ( + u128::from(READER_COST), + u128::from(pixels), + u128::from(bytes), + ); + fixed * (fixed + pixels * u128::from(SNAPSHOT_PIXEL_COST)) <= (fixed + bytes).pow(2) +} + /// Which of a draw's two Vulkan draws one recording is (the module docs). #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub(super) enum Phase { @@ -418,11 +457,12 @@ impl RenderLowering { /// Whether a draw of `pipeline` that reads the pass's `colors` through snapshots costs no /// more than the split would: at any area over a buffer's bytes - /// ([`over_bytes`](Self::over_bytes)), else by [`snapshot_weights`](Self::snapshot_weights); - /// counted by its size where `METAL2VULKAN_LOG_PIPELINES` is set (`reader_sizes.rs`). + /// ([`over_bytes`](Self::over_bytes)), else by the area rule ([`snapshot_within`]) over + /// [`snapshot_weights`](Self::snapshot_weights); counted by its size where + /// `METAL2VULKAN_LOG_PIPELINES` is set (`reader_sizes.rs`). fn snapshot_cheaper(&self, pipeline: &DrawingState, colors: &[usize]) -> bool { let (covered, pixels, bytes) = self.snapshot_weights(pipeline, colors); - let within = self.over_bytes(colors) || covered * SNAPSHOT_PIXEL_COST <= bytes; + let within = self.over_bytes(colors) || snapshot_within(covered, bytes); if crate::pipeline::creation_log_enabled() { let device = self.core.device(); let line = crate::device::lock(&device.shared().reader_sizes) @@ -453,18 +493,16 @@ impl RenderLowering { } /// What weighs whether a draw of `pipeline` that reads the pass's `colors` through snapshots - /// costs no more than the split would: both forms are exact, so the draw takes the cheaper. The - /// snapshot draw costs [`SNAPSHOT_PIXEL_COST`] for each color at each pixel the draw covers, - /// which its Vulkan scissors bound (`EncoderState::vk_scissors`: each viewport's pixels within - /// its scissor rectangle, inside the render area); the split's copy is weighed as reading and - /// writing each color's every texel of the rendered level, its format's bytes each, an upper - /// bound: a copy of a one-level, one-slice texture's image moves only the texels around the - /// draw's scissors, or those the instances before changed (`ShadowCopies::cuts`), which errs - /// toward the snapshot. The split's pass break (each attachment stored and loaded again) is - /// left out, which errs toward the split. The - /// draw's pixels for every color (the scissors' pixels times the colors), the colors' pixels, - /// and their bytes; it takes the snapshot where the first times [`SNAPSHOT_PIXEL_COST`] is at - /// most the last. + /// costs no more than the split would: both forms are exact, so the draw takes the cheaper by + /// the area rule (the module docs). The snapshot draw costs [`SNAPSHOT_PIXEL_COST`] for each + /// color at each pixel the draw covers, at most those its Vulkan scissors bound + /// (`EncoderState::vk_scissors`: each viewport's pixels within its scissor rectangle, inside + /// the render area); the split's copy is weighed as reading and writing each color's every + /// texel of the rendered level, its format's bytes each, an upper bound: a copy of a + /// one-level, one-slice texture's image moves only the texels around the draw's scissors, or + /// those the instances before changed (`ShadowCopies::cuts`), which errs toward the snapshot. + /// The draw's pixels for every color (the scissors' pixels times the colors), the colors' + /// pixels, and their bytes. fn snapshot_weights(&self, pipeline: &DrawingState, colors: &[usize]) -> (u64, u64, u64) { let selects = pipeline.base().graphics.writes_viewport_index || self.amplification.offsets_viewports(); diff --git a/crates/metal2vulkan-runtime/src/tests/mod.rs b/crates/metal2vulkan-runtime/src/tests/mod.rs index 40d09175a..0ec416907 100644 --- a/crates/metal2vulkan-runtime/src/tests/mod.rs +++ b/crates/metal2vulkan-runtime/src/tests/mod.rs @@ -400,6 +400,7 @@ mod shared_draw_locks; mod shared_pointers; #[cfg(any(m2v_gpu, m2v_cpu_icd))] mod short_row_kinds; +mod snapshot_area; mod sorted_chunks; mod spares; mod sparse; diff --git a/crates/metal2vulkan-runtime/src/tests/snapshot_area.rs b/crates/metal2vulkan-runtime/src/tests/snapshot_area.rs new file mode 100644 index 000000000..af330d1d7 --- /dev/null +++ b/crates/metal2vulkan-runtime/src/tests/snapshot_area.rs @@ -0,0 +1,59 @@ +//! `render/snapshot.rs`'s area rule: a draw that reads its pass's texture through a snapshot or +//! takes the split by its form's cost at the share of its scissors least favourable to it. + +use crate::render::snapshot_within; + +/// The pixels of the consumer's desktop framebuffer, 3662x2039. +const DESKTOP: u64 = 3662 * 2039; + +/// A reader through no scissor over a private 4-byte target of the desktop's size takes the +/// snapshot, whatever it covers: covering the whole target the snapshot weighs 4.9 times the split +/// (20 bytes a pixel against the copy's 4, each beside a reader's fixed cost of a mebibyte), but +/// covering nothing the split weighs 29.5 times the snapshot. So do readers over 1080p and 720p +/// targets. +#[test] +fn a_reader_through_no_scissor_over_a_large_color_takes_the_snapshot() { + assert!(snapshot_within(DESKTOP, DESKTOP * 4)); + assert!(snapshot_within(1920 * 1080, 1920 * 1080 * 4)); + assert!(snapshot_within(1280 * 720, 1280 * 720 * 4)); +} + +/// A 4-byte color's reader through its whole level takes the snapshot from 786,432 pixels +/// (1024x768) and an 8-byte one's from 65,536 (256x256), not one pixel fewer; a 16-byte one's at +/// any size. +#[test] +fn a_reader_through_its_whole_level_takes_the_snapshot_from_a_size_by_its_texel_bytes() { + assert!(snapshot_within(786_432, 786_432 * 4)); + assert!(!snapshot_within(786_431, 786_431 * 4)); + assert!(snapshot_within(65_536, 65_536 * 8)); + assert!(!snapshot_within(65_535, 65_535 * 8)); + for pixels in [1, 64, 1024, 65_536, DESKTOP] { + assert!(snapshot_within(pixels, pixels * 16), "{pixels}"); + } +} + +/// A 4-byte color's reader takes the snapshot through scissors of up to two fifths of its level +/// at any size: over a 32x32 `RGBA8Unorm` target, through a quarter of it (256 pixels) but not +/// through the whole (1024). +#[test] +fn a_reader_through_two_fifths_of_a_small_level_takes_the_snapshot() { + assert!(snapshot_within(256, 1024 * 4)); + assert!(!snapshot_within(1024, 1024 * 4)); + for level in [1024, 65_536, 1 << 20] { + assert!(snapshot_within(level * 2 / 5, level * 4), "{level}"); + } +} + +/// Every reader whose snapshot over its whole scissors weighs no more than the split's copy takes +/// the snapshot, as before the fixed costs were weighed. +#[test] +fn a_snapshot_no_dearer_than_the_copy_over_its_whole_scissors_is_taken() { + for bytes in [0, 4, 400, 4096, 1 << 20, 30_000_000, 1 << 34] { + for pixels in [0, bytes / 40, bytes / 20] { + assert!( + snapshot_within(pixels, bytes), + "{pixels} pixels, {bytes} bytes" + ); + } + } +} diff --git a/crates/metal2vulkan-validation/src/runtime_lane/attachment_read_costs.rs b/crates/metal2vulkan-validation/src/runtime_lane/attachment_read_costs.rs index bd18883eb..5bfe7a036 100644 --- a/crates/metal2vulkan-validation/src/runtime_lane/attachment_read_costs.rs +++ b/crates/metal2vulkan-validation/src/runtime_lane/attachment_read_costs.rs @@ -9,8 +9,9 @@ //! window's rectangle through its scissor, a memory barrier over the render targets and a draw //! that reads the target at its own pixels over that rectangle (`tob_read`); then, in every other //! pass, a barrier and a draw over a window that samples the target at other pixels -//! (`a4_fragment_select_sample`). The windows cover more than a fifth of the target, so no own-pixel -//! read takes a snapshot (`render/snapshot.rs`). The frame waits for its last command buffer. +//! (`a4_fragment_select_sample`). Each own-pixel read takes a snapshot where the device takes the +//! path, as the area rule does through a window's scissor over this target (`render/snapshot.rs`), +//! else a split. The frame waits for its last command buffer. //! //! [`WARM`] frames run untimed, then [`TIMED`] frames are printed as one `ATTACHMENT-READ-COST` //! line: the GPU time per frame (each command buffer's GPU end less its start, summed), the wall @@ -58,6 +59,17 @@ //! device's in-place check answered, each line and its time printed, `SELF-READ-CHECK`), and the //! images made with feedback loop usage. //! +//! `snapshot_area_costs` weighs the area rule's two forms for readers through no scissor, as the +//! consumer's guest draws them: over a private `BGRA8Unorm` target of each of [`AREA_SIZES`], a +//! frame is one command buffer of one pass, loaded but in the first frame, which clears it, and +//! stored; [`SELF_PAIRS`] times a fill of a rectangle covering a share of the target +//! ([`AREA_SHARES`]), at a place of its own, a memory barrier over the render targets and a draw +//! over the same rectangle that reads the target at its own pixels (`tob_read`); each cell also +//! without the barriers and readers, the baseline. On the default device each reader takes the form +//! the rule picks, and on one that withholds `fragmentShaderPixelInterlock` the split. Printed per +//! cell (`SNAPSHOT-AREA-COST`): the median GPU time per frame of the baseline and of the readers +//! on each device, and per frame the readers snapshotted and split on each. +//! //! Ignored by default: measurements, not checks. Run them by name with `--run-ignored all`. use super::attachment_reads::{floats, function}; @@ -66,7 +78,7 @@ use super::host_memory::{no_copy, Mapping, GUEST_PAGE}; use super::{open_device, WAIT}; use crate::gpu::TEST_DEVICE; use metal2vulkan::bind::SamplerDescriptor; -use metal2vulkan::device::HostPageTexturePolicy; +use metal2vulkan::device::{HostPageTexturePolicy, VkFeature}; use metal2vulkan::metal::{ BarrierScope, BlendFactor, PixelFormat, PrimitiveType, RenderStages, TextureUsage, }; @@ -1038,3 +1050,145 @@ fn self_reads_on(device: &Device, policy: HostPageTexturePolicy) { drop((surface, private, buffer)); drop(mapping); } + +/// The `snapshot_area_costs` targets' sizes. +const AREA_SIZES: [[u64; 2]; 4] = [[1280, 720], [1920, 1080], [2560, 1440], SELF_SIZE]; +/// The share of its target each `snapshot_area_costs` rectangle covers, in thousandths. +const AREA_SHARES: [u64; 5] = [15, 32, 125, 500, 1000]; + +#[test] +#[ignore = "a release-mode measurement: run by name"] +fn snapshot_area_costs() { + let rule = open_device(TEST_DEVICE, DeviceOptions::default()).unwrap(); + let mut options = DeviceOptions::default(); + options + .withheld_features + .insert(VkFeature::FragmentShaderPixelInterlock); + let split = open_device(TEST_DEVICE, options).unwrap(); + let devices = [&rule, &split].map(|device| { + let bgra = PixelFormat::BGRA8Unorm; + let draws = ( + state(device, "tob_solid", "tob_renders.ll", bgra), + state(device, "tob_read", "tob_renders.ll", bgra), + ); + (device, device.new_command_queue().unwrap(), draws) + }); + for size in AREA_SIZES { + let targets = devices.each_ref().map(|(device, ..)| { + let mut d = + TextureDescriptor::texture_2d(PixelFormat::BGRA8Unorm, size[0], size[1], false); + d.usage = TextureUsage::RENDER_TARGET | TextureUsage::SHADER_READ; + d.resource_options = ResourceOptions::private(); + device.new_texture(&d).unwrap() + }); + for share in AREA_SHARES { + let side = + |s: u64| ((s as f64 * (share as f64 / 1000.0).sqrt()).round() as u64).clamp(1, s); + let rect = [side(size[0]), side(size[1])]; + let mut cells = Vec::new(); + for ((device, queue, draws), target) in devices.iter().zip(&targets) { + for baseline in [true, false] { + let mut frames = 0..; + let mut next = || frames.next().unwrap(); + let draws = (&draws.0, &draws.1); + for _ in 0..SELF_WARM { + area_frame(queue, target, draws, (size, rect), next(), baseline); + } + let before = device.work_counts(); + let gpu: Vec = (0..SELF_TIMED) + .map(|_| area_frame(queue, target, draws, (size, rect), next(), baseline)) + .collect(); + let after = device.work_counts(); + let per = |a: u64, b: u64| (b - a) as f64 / SELF_TIMED as f64; + cells.push(( + median(gpu) * 1e3, + per( + before.reading_draws_snapshotted, + after.reading_draws_snapshotted, + ), + per(before.attachment_read_splits, after.attachment_read_splits), + )); + } + } + let [rule_base, rule_read, split_base, split_read] = [0, 1, 2, 3].map(|i| cells[i]); + println!( + "SNAPSHOT-AREA-COST {} {}x{} share {share}/1000 rect {}x{}: GPU ms/frame: rule \ + {:.3} over {:.3} baseline (snapshotted {:.2}, splits {:.2}); split {:.3} over \ + {:.3} baseline (snapshotted {:.2}, splits {:.2})", + rule.name(), + size[0], + size[1], + rect[0], + rect[1], + rule_read.0, + rule_base.0, + rule_read.1, + rule_read.2, + split_read.0, + split_base.0, + split_read.1, + split_read.2, + ); + } + } +} + +/// One [`snapshot_area_costs`] frame over `target` of `size`, its `frame`th, on `queue`: its pairs +/// over rectangles of `rect`; with no barriers or readers where `baseline`. Its GPU time, in +/// seconds. +fn area_frame( + queue: &CommandQueue, + target: &Texture, + (fill, read): (&RenderPipelineState, &RenderPipelineState), + ([width, height], [w, h]): ([u64; 2], [u64; 2]), + frame: usize, + baseline: bool, +) -> f64 { + let mut d = RenderPassDescriptor::new(); + let color = &mut d.color_attachments[0]; + color.texture = Some(target.clone()); + color.load_action = match frame { + 0 => LoadAction::Clear, + _ => LoadAction::Load, + }; + color.store_action = StoreAction::Store; + let corners = |[x0, y0, x1, y1]: [u64; 4]| { + let x = |p: u64| p as f32 / width as f32 * 2.0 - 1.0; + let y = |p: u64| 1.0 - p as f32 / height as f32 * 2.0; + floats([x(x0), y(y1), x(x1), y(y0)]) + }; + let place = |pair: u64| { + let n = frame as u64 + pair; + let x = (n * 1373).checked_rem(width - w).unwrap_or(0); + let y = ((n * 3 + 1) * 787).checked_rem(height - h).unwrap_or(0); + [x, y, x + w, y + h] + }; + let command_buffer = queue.command_buffer().unwrap(); + let mut encoder = command_buffer.render_command_encoder(&d).unwrap(); + for pair in 0..SELF_PAIRS { + let rect = corners(place(pair)); + encoder.set_render_pipeline_state(fill).unwrap(); + let red = pair as f32 / SELF_PAIRS as f32; + encoder + .set_fragment_bytes(&floats([red, 0.5, 0.0, 1.0]), 0) + .unwrap(); + encoder.set_vertex_bytes(&rect, 0).unwrap(); + encoder + .draw_primitives(PrimitiveType::Triangle, 0, 6, 1, 0) + .unwrap(); + if baseline { + continue; + } + barrier(&mut encoder); + encoder.set_render_pipeline_state(read).unwrap(); + encoder.set_fragment_texture(Some(target), 0).unwrap(); + encoder + .draw_primitives(PrimitiveType::Triangle, 0, 6, 1, 0) + .unwrap(); + } + encoder.end_encoding().unwrap(); + command_buffer.commit().unwrap(); + command_buffer.wait_until_completed_for(WAIT).unwrap(); + assert_eq!(command_buffer.error(), None); + command_buffer.gpu_end_time().unwrap() - command_buffer.gpu_start_time().unwrap() +} diff --git a/crates/metal2vulkan-validation/src/runtime_lane/snapshot_reads.rs b/crates/metal2vulkan-validation/src/runtime_lane/snapshot_reads.rs index 2059a6573..a1f364239 100644 --- a/crates/metal2vulkan-validation/src/runtime_lane/snapshot_reads.rs +++ b/crates/metal2vulkan-validation/src/runtime_lane/snapshot_reads.rs @@ -27,11 +27,12 @@ //! reader in two instances (every pixel covered by two primitives of the one draw), a barrier, //! the reader once more: `3 * FILL`, then `6 * FILL`. A read of the attachment itself would //! race with the draw's own color writes, the second primitive reading the first's (`4 * FILL`). -//! - [`readers_take_the_snapshot_where_it_costs_less_than_the_copy`]: the same draws through -//! wider scissors, whatever the snapshot's form: over `RGBA8Unorm` through the whole target's -//! scissor (1024 pixels of 20 bytes against 1024 texels of 4) and through [`QUARTER`]'s (256 -//! of 20 against 1024 of 4), a split for each; over `RGBA16Float` through [`QUARTER`]'s (256 -//! of 20 against 1024 of 8), a snapshot for each where the device takes the path. +//! - [`readers_take_the_snapshot_by_the_area_rule`]: the same draws through wider scissors, +//! whatever the snapshot's form (`render/snapshot.rs`'s area rule, each form beside a reader's +//! fixed cost of a mebibyte): over `RGBA8Unorm` through the whole target's scissor (1024 pixels +//! of 20 bytes against a copy of 1024 texels of 4), a split for each; through [`QUARTER`]'s (256 +//! pixels, within two fifths of the target), and over `RGBA16Float` through [`QUARTER`]'s, a +//! snapshot for each where the device takes the path. //! - [`a_surfaces_readers_take_the_snapshot_at_any_area`]: the same draws over a surface over //! host pages, an `RGBA8Unorm` texture of an IOSurface plane over a no-copy buffer, through the //! whole target's scissor: a snapshot for each where the device takes the path and the image @@ -45,9 +46,9 @@ //! - [`each_form_reads_what_the_split_reads`]: the first test's draws over a target of each of //! `RGBA8Unorm`, `BGRA8Unorm`, `R8Unorm`, `RG8Unorm` (8-bit codes), `RGBA16Float` (half-float //! codes) and `RGB10A2Unorm` (bits): the bytes the split leaves, byte for byte, with a snapshot -//! for each reader where the device takes the path, the rule does (not `R8Unorm`'s, whose copy -//! moves 1024 bytes) and the device keeps the color in place (an `RGBA8Unorm` one's on every -//! such device; the counts printed say which others did). +//! for each reader where the device takes the path, the rule does (for each of them, `R8Unorm`'s +//! copy of 1024 bytes too) and the device keeps the color in place (an `RGBA8Unorm` one's on +//! every such device; the counts printed say which others did). //! - [`without_a_barrier_the_readers_read_the_texels_from_before_the_pass`]: the same draws with //! no barrier: every reader reads [`CLEARED`], the texel from before the pass at its own pixel, //! through the copy taken before the pass: `FILL + 3 * CLEARED`, no snapshot. The M3 Max's @@ -505,7 +506,7 @@ fn overlapping_primitives_read_what_the_draws_before_the_barrier_wrote() { } #[test] -fn readers_take_the_snapshot_where_it_costs_less_than_the_copy() { +fn readers_take_the_snapshot_by_the_area_rule() { on_default_policy( |_| {}, |device| { @@ -519,18 +520,18 @@ fn readers_take_the_snapshot_where_it_costs_less_than_the_copy() { (0, 2), "RGBA8Unorm over the target: snapshots, splits" ); + let (snapshotted, splits) = if snapshots(device) { (2, 0) } else { (0, 2) }; let (bytes, moved) = readers(device, &queue, narrow, true, Some(QUARTER)); eprintln!("snapshot reads, quarter scissor, RGBA8Unorm: {moved:?}"); holds("quarter scissor", &bytes, 6 * FILL); assert_eq!( (moved.snapshotted, moved.splits), - (0, 2), + (snapshotted, splits), "RGBA8Unorm over a quarter: snapshots, splits" ); let wide = PixelFormat::RGBA16Float; let (_, moved) = readers(device, &queue, wide, true, Some(QUARTER)); eprintln!("snapshot reads, quarter scissor, RGBA16Float: {moved:?}"); - let (snapshotted, splits) = if snapshots(device) { (2, 0) } else { (0, 2) }; assert_eq!( (moved.snapshotted, moved.splits), (snapshotted, splits), diff --git a/docs/FAST-PATHS.md b/docs/FAST-PATHS.md index c1d91a00b..e29fea05e 100644 --- a/docs/FAST-PATHS.md +++ b/docs/FAST-PATHS.md @@ -238,14 +238,20 @@ recorded here. a 1920x1080 surface) 0.56 to 0.99 times the split's GPU time through scissors of 960x540 and up, at most 1.08 times through the whole target and 1.2 times through a 320x180 scissor it covers half of or more. - Elsewhere the rule weighs the snapshot draw at 20 bytes of the split's copy for each pixel its - scissors bound, against a copy of every texel of the color (an upper bound of the split's copy, - which is cut to the scissors or to what changed for a one-level, one-slice texture), which fits - the RX 7900 XTX and the RTX 5080 Laptop GPU; on the Radeon 780M the split is faster from about - 14.5%, 17% and 42% of the target for 4-, 8- and 16-byte texels, so the rule costs up to 36% there - (`RGBA16Float` at 30% of a 1920x1080 target), and on the Iris Xe from an eighth of the reader's - scissor covered, whatever its area, so the rule costs up to 2.1 times there (a 320x180 scissor - covered whole over a private 1920x1080 `BGRA8Unorm` texture). + Elsewhere the area rule (`render/snapshot.rs`) weighs each form at the share of its scissors + least favourable to it, as a draw's recording cannot know what it covers: the snapshot draw at + 20 bytes of the split's copy for each pixel its scissors bound, the split at a copy of every + texel of the color (an upper bound of the split's copy, which is cut to the scissors or to what + changed for a one-level, one-slice texture), each beside a reader's fixed cost of a mebibyte of + the copy; it takes the form whose cost there is the smaller multiple of the other's. A 4-byte + color's reader takes the snapshot through scissors of up to two fifths of the target at any + size, and at any area from 1024x768. Through no scissor over private `BGRA8Unorm` targets of + 1280x720 to 3662x2039 (`runtime_lane::attachment_read_costs::snapshot_area_costs`), a reader + covering 1.5% of the target took 0.09 to 0.33 times the split's GPU time on the Iris Xe, 0.11 to + 0.24 on the Radeon 780M and 0.25 to 0.43 on the RX 7900 XTX; one covering the whole target, the + rule's worst case, 1.04 to 1.28 times, 2.4 to 2.8 times and 1.3 to 2.1 times. Through a small + scissor covered whole the snapshot costs up to 2.1 times the split on the Iris Xe (a 320x180 + scissor over a private 1920x1080 `BGRA8Unorm` texture). Other devices are not measured. - **A texture over a no-copy buffer's host pages, or over a host-visible buffer longer than 32 MiB, that a shader writes of 1- or 2-byte texels (or whose class's check has not passed for an access of its, or whose format, usage or pitch no image takes, or under