On the Iris Xe (ANV) a pass that clears and stores a linear image, and a draw into it, write back whole the 64-byte line that holds a row's last texels. That puts an older value back over a CPU write beside the texels, so the check fails render targets over host pages there, and every such texture is a shadow. The write-back cannot reach a CPU write when no line holds both a texel and a byte beside the texels: the probe read 0/100 for rows that end on a line, and 0/25 for a 3840x2160 surface of tight rows. A 3840x2160 BGRA8Unorm surface's 15360-byte rows are 240 lines each. The check's verdicts are now kept per class, access and row ends (in_place::RowEnds): - The line is the device's nonCoherentAtomSize. vkspec defines it as "the size and alignment in bytes that bounds concurrent access to host-mapped device memory". - RowEnds::of reads the texels' address, offset and pitches. Texels are OnLines where their first byte and their end lie on a line, and so do each row's (or slice's) start and end where bytes lie between rows (or slices). Elsewhere they are InLines. - The check runs the shapes whose texels share no line first (Shape::rows; seven of SHAPES at 64-byte lines). A byte changed beside one of them fails both row ends. A byte changed beside a shape that shares a line fails InLines alone, since the OnLines rounds have already run (the verdicts function). A device that fails InLines now also runs its OnLines shapes. - SHAPES gains two shapes whose rows end inside a 16-byte line, at the 16-byte pitch a driver that lays rows tight uses: BGRA8Unorm 101x51 at 416 bytes and RGBA16Float 33x17 at 272, the same shapes STORE_SHAPES already holds. MoltenVK makes no linear image with bytes between rows except at that pitch, and its line is 16 bytes. Without these two, none of its shapes whose texels share a line was made, so its reads and renders were Unchecked for those texels: on the M3, a probe of this change without them gave Pointer and Fresh reads and renders Unchecked 5/5 for InLines (m3-max-6750). - SHAPES and STORE_SHAPES gain the same widths at the 64-byte pitch of a driver that lays rows at the next multiple of 64: BGRA8Unorm 101x51 at 448 and RGBA16Float 33x17 at 320, and RGBA8Uint 101x51 at 448 and RG32Uint 33x17 at 320. On lavapipe (llvmpipe's linear images take 64-byte rows, its line 64 bytes) no shape whose texels share a line was made without them: tests::in_place_check::the_in_place_check_checks_fresh_pages_where_held_imports_cost_submissions read the Fresh verdicts Unchecked for all three accesses of those texels (m3-max-6991). The shape test now holds that drivers whose rows lie at the next multiple of 16, 64, 128 or 256 bytes each have such a shape, for each access. - The record holds 18 verdicts. CHECK_VERSION goes to 6, so a record of the old check is not read. - The report keeps its nine lines, now the InLines verdicts (the old verdict exactly, since an OnLines failure fails both), and adds nine ", rows on lines" lines after them. - The texture's own row ends decide, everywhere a texture asks for its verdict: its making (Texels::rows), a shadow's move in place (MoveWait, move_wanted) and a copied rebind into fresh pages (fresh_pages_take_images, from the watch's place in its pages). ShadowCause::CheckPending, CheckFailed and Unchecked name the row ends, and the notice says them. - Device::in_place_check_line (doc-hidden) answers the line. The lanes that hold a texture to its verdict compute its row ends themselves (runtime_lane::in_place_rows, apart from the runtime's reckoning) and read that row ends' line. Docs: FAST-PATHS row 41 and the API guide describe the row ends. The ShadowedTexture notice docs name them. The known gap for rows that end on a line is closed and deleted. Tests: RowEnds::of covers an offset, an address, a pitch and slices off a line, tight rows that do or do not end on a line, a 1-byte line, the 3840 and 3662-wide surfaces, and the check's shapes per class. The verdicts function: a change beside OnLines texels fails both row ends, a change beside InLines texels fails InLines alone, and a forced failure and an unchecked class are covered. The decision test runs every class with both row ends. The record test covers 18 verdicts. Verified, the whole check on fresh caches (the probe test, this change plus the test alone; the Iris Xe and RADV runs at 5beb0be2a5, before the 64-byte shapes, on development0950c1a0ab): - Iris Xe (RPL-P), ANV, Mesa 26.2.6, 64-byte line (m3-max-6892), 20 runs, every run the same: reads Passed for both row ends, Pointer and Fresh; renders and writes Failed for texels that share a line and Passed for texels that share none, Pointer and Fresh; DmaBuf Unchecked for all six. The failures are a byte between two rows: a clear and store or a draw of R8Unorm 61x37 at 256 bytes a row (renders), and storage writes of RGBA8Uint 101x51 at 512 (writes). 323-383 ms a check (median 345), against 156-342 ms before (m3-max-6226): a class that fails for texels that share a line now runs its rounds for texels that share none to the end, as a passing device's do. A build job ran on the same host meanwhile. - M3 Max, MoltenVK 1.4.2, 16-byte line: Pointer and Fresh Passed 5/5 for reads, renders and writes, for both row ends (12); DmaBuf Unchecked for all six (no dma-bufs on macOS). 290-304 ms a check at 5beb0be2a5 (m3-max-6804); with the 64-byte shapes, at 19a7c36f69 on developmentee5d3cfab9(m3-max-7142), the same verdicts and 306-311 ms, the first run 548 ms. Before this change, 299-582 ms (m3-max-6160). That earlier run had six verdicts Passed and the three DmaBuf ones Unchecked, not all nine Passed as 31b19fa7f0's body says. No M3 cost record's case changes path: every verdict the M3 passed before passes for both row ends. - RX 7900 XTX, RADV, Mesa 26.2.6, 64-byte line (m3-max-6817): all 18 Passed 5/5. 535-561 ms a check, against 626-642 ms before (m3-max-6523). Verified, self_reads on the Iris Xe (m3-max-6892, release, each process on a cache of its own), the previous commit's lane alone (before) against this change (after), two rounds each, GPU ms per frame (median) of the baseline and of 30 self-read pairs: - 3840x2160 surface over host pages, InPlace policy: before a shadow, 3 shadow copies a frame, 8294400 texels copied in, 1 keep and 1 merge; baseline 3.487 and 3.526, self-reads 4.994 and 5.014. After in place: no shadow copy, no texel copied in, no keep or merge; baseline 1.676 and 1.674, self-reads 4.544 and 4.559 (30 snapshots a frame, as before). - 3662x2039 surface, InPlace policy: a shadow before and after (its rows end inside a line); self-reads 4.141 and 4.187 before, 4.168 and 4.205 after. - Private targets, unchanged: 3662x2039 self-reads 11.48 to 12.12, 3840x2160 14.68 to 15.04 (30 splits a frame, before and after, on development0950c1a0ab). Under 61a367cb51's area rule, landed since, these readers are expected to take snapshots instead (not run here). - The check, as the lane's device ran it: 202.6 and 193.8 ms before, 411.5 and 369.7 after. The lanes that hold a texture to its verdict passed on the Iris Xe under validation (in_place_textures 7, linear_attachments 25, snapshot_reads 7, linear_alignments 3, first_use_writes 1, device_copies 5; m3-max-6892). Checked at b0043502d1 (this change before the 64-byte shapes, on development ee5d3cfab9; homelab-gem10-1, m3-max-6991): clippy with --cfg m2v_cpu_icd and with --cfg m2v_gpu, all targets and features, clean; rustdoc with -D warnings clean; the API tables, the ledger test, check_draw and tests::in_place (44) passed; on lavapipe 306 of the 307 touched tests passed, and the one that failed is the fresh-pages test the 64-byte shapes fix (above). That test and the runtime's in-place tests on lavapipe at this commit: m3-max-7152 (homelab-um870-1). The RADV probe at this commit plus the probe test: m3-max-7153, then its run on the RX 7900 XTX. (cherry picked from commit f815203f074e1b2f922cd2a2d337ba31ea51e02f)
326 lines
16 KiB
Rust
326 lines
16 KiB
Rust
//! A CPU write to bytes of a texture over host pages that the texture's first use does not read
|
|
//! stays, on the device the cfg selects, under the validation layer: Metal never writes such
|
|
//! bytes, so it keeps every such write (an M3 Max probe: every CPU write between rows, and to
|
|
//! texels the command buffer did not read, stayed in each of 10 rounds).
|
|
//!
|
|
//! A texture that only reads (`ShaderRead` usage) is made over a no-copy buffer of anonymous pages
|
|
//! at each of [`LAYOUTS`] (a 1080p surface's packed rows, and a browser tile's padded ones), and
|
|
//! its first use is a pass whose draw reads it inside a scissor of the top half of its rows. Around
|
|
//! that command buffer's commit, another thread writes a byte of a texel in each row of the bottom
|
|
//! half, the byte after each such row's texels where the rows are padded, and bytes past the last
|
|
//! row: one row after another, [`SPACING`] apart, starting at each of [`STARTS`] from the commit
|
|
//! (a fresh texture each), so the writes cover the commit, the submission and the first use. Every
|
|
//! write stays, and the pass reads the texels of the top half as they were.
|
|
//!
|
|
//! Under both settings of `HostPageTexturePolicy`, each held to the path it took:
|
|
//!
|
|
//! - **`InPlace`** (the default), once the device's check of the first move over host pages
|
|
//! answered (`Device::settle_in_place_checks`): where it passed for pages imported by their
|
|
//! pointer, the texture is an image aliasing the pages (`vk_image` answers one;
|
|
//! `WorkCounts::host_textures_in_place`), its first move the image's own layout transition from
|
|
//! `UNDEFINED`, which Vulkan lets read and write every byte bound to it and the device's check
|
|
//! showed leaving them alone; or a shadow its refusal counts (`host_texture_shadows_refused_*`:
|
|
//! the driver lays no image at the plane's pitch). Where the check did not pass, a shadow counted
|
|
//! by why.
|
|
//! - **`Shadow`**: a shadow, whose first use copies the texels in and writes none of the pages'
|
|
//! bytes (`host_texture_shadows_exact`).
|
|
//!
|
|
//! Printed (`FIRST-USE-WRITES`): per setting, layout and start, the form, the writes undone and
|
|
//! the texels read that differ; and the check's verdicts (`IN-PLACE-CHECK`).
|
|
|
|
use super::host_memory::{counting, no_copy, not_importable, Mapping, GUEST_PAGE};
|
|
use super::in_place_rows;
|
|
use super::no_copy_textures::{floats, staged};
|
|
use super::pitched_surfaces::reading;
|
|
use super::{layer_reported_nothing_at_all, validated_device_with, Messages, ScratchCache, WAIT};
|
|
use crate::gpu::TEST_DEVICE;
|
|
use metal2vulkan::device::HostPageTexturePolicy;
|
|
use metal2vulkan::metal::{PixelFormat, PrimitiveType, TextureUsage};
|
|
use metal2vulkan_runtime::{
|
|
BlitOption, ClearColor, IoSurfacePlane, LoadAction, Origin, RenderPassDescriptor,
|
|
ResourceOptions, ScissorRect, Size, StoreAction, TextureDescriptor,
|
|
};
|
|
use std::ptr::NonNull;
|
|
use std::time::{Duration, Instant};
|
|
|
|
/// A surface's `BGRA8Unorm` plane: its name, width, height and bytes per row.
|
|
const LAYOUTS: [(&str, u64, u64, u64); 2] = [
|
|
("1920x1080/7680", 1920, 1080, 7680),
|
|
("1307x512/5248", 1307, 512, 5248),
|
|
];
|
|
|
|
/// When the writes start, in microseconds from the commit (before it where negative): the commit,
|
|
/// the submission and the first use take a time of their own on each device, so the writes start
|
|
/// at several times and some of them overlap each.
|
|
const STARTS: [i64; 6] = [-2000, -1000, -300, 0, 500, 2000];
|
|
|
|
/// The time between two rows' writes.
|
|
const SPACING: Duration = Duration::from_micros(4);
|
|
|
|
/// The bytes past the last row the pages hold, and the CPU writes the first and last of.
|
|
const PAST: u64 = 64;
|
|
|
|
/// The time before the first of a round's two steps (the commit, the writes' start).
|
|
const LEAD: Duration = Duration::from_millis(1);
|
|
|
|
fn spin_until(at: Instant) {
|
|
while Instant::now() < at {
|
|
std::hint::spin_loop();
|
|
}
|
|
}
|
|
|
|
/// The pages' first byte, for the writing thread: the test keeps the mapping alive until the
|
|
/// thread is joined, and the bytes it writes are no other thread's.
|
|
#[derive(Clone, Copy)]
|
|
struct Pages(NonNull<u8>);
|
|
|
|
// SAFETY: see the type's docs; the thread writes bytes no other thread reads or writes meanwhile.
|
|
unsafe impl Send for Pages {}
|
|
|
|
/// How long a lane waits for the device's check of the first move over host pages.
|
|
pub(crate) const CHECK_WAIT: Duration = Duration::from_secs(300);
|
|
|
|
#[test]
|
|
fn a_cpu_write_to_bytes_a_first_use_does_not_read_stays() {
|
|
let mut lapses = Vec::new();
|
|
for policy in [
|
|
HostPageTexturePolicy::InPlace,
|
|
HostPageTexturePolicy::Shadow,
|
|
] {
|
|
first_use_writes_under(policy, &mut lapses);
|
|
}
|
|
assert!(
|
|
lapses.is_empty(),
|
|
"a first use undid a CPU write, or changed or misread bytes, or took another path:\n{}",
|
|
lapses.join("\n")
|
|
);
|
|
}
|
|
|
|
/// The lane's rounds on a device of `policy`, their lapses pushed to `lapses`.
|
|
fn first_use_writes_under(policy: HostPageTexturePolicy, lapses: &mut Vec<String>) {
|
|
let cache = ScratchCache::new().unwrap();
|
|
let messages = Messages::default();
|
|
{
|
|
let device = validated_device_with(TEST_DEVICE, &messages, cache.path(), |options| {
|
|
options.policy.host_page_textures = policy;
|
|
})
|
|
.unwrap();
|
|
// The check answers before any texture is made, so each takes the path its verdict says.
|
|
let in_place = policy == HostPageTexturePolicy::InPlace;
|
|
if in_place {
|
|
assert!(
|
|
device.settle_in_place_checks(CHECK_WAIT),
|
|
"the check answered"
|
|
);
|
|
for line in device.in_place_check_report() {
|
|
eprintln!("IN-PLACE-CHECK on {}: {line}", device.name());
|
|
}
|
|
eprintln!(
|
|
"IN-PLACE-CHECK on {}: took {:?}",
|
|
device.name(),
|
|
device.in_place_check_took()
|
|
);
|
|
}
|
|
let queue = device.new_command_queue().unwrap();
|
|
let reads = reading(&device, PixelFormat::BGRA8Unorm, "tob_read");
|
|
let corners = staged(&device, &floats([-1.0, -1.0, 1.0, 1.0]));
|
|
'layouts: for (name, w, h, bpr) in LAYOUTS {
|
|
let row = w * 4;
|
|
let bound = bpr * h;
|
|
let len = ((bound + PAST) as usize).next_multiple_of(GUEST_PAGE);
|
|
let top = h / 2;
|
|
// The bytes the thread writes, in order: a texel's in each row of the bottom half, the
|
|
// byte after the row's texels where the rows are padded, and bytes past the last row.
|
|
let mut targets = Vec::new();
|
|
for y in top..h {
|
|
targets.push((y * bpr + (y % w) * 4) as usize);
|
|
if bpr > row {
|
|
targets.push((y * bpr + row) as usize);
|
|
}
|
|
}
|
|
targets.extend([bound as usize, (bound + PAST - 1) as usize]);
|
|
for start in STARTS {
|
|
let mapping = Mapping::anonymous(len);
|
|
let pages = counting(len, 7);
|
|
mapping.write(0, &pages);
|
|
// SAFETY: the mapping outlives the buffer and the texture (dropped first).
|
|
let made = unsafe {
|
|
no_copy(
|
|
&device,
|
|
mapping.pointer(),
|
|
len,
|
|
ResourceOptions::default(),
|
|
None,
|
|
)
|
|
};
|
|
let buffer = match made {
|
|
Ok(buffer) => buffer,
|
|
Err(error) if not_importable(&error) => {
|
|
eprintln!("FIRST-USE-WRITES: the device imports no host memory: {error:?}");
|
|
break 'layouts;
|
|
}
|
|
Err(error) => panic!("new_buffer_with_bytes_no_copy: {error:?}"),
|
|
};
|
|
// The verdict its texels take, at the start of the pages.
|
|
let first = buffer.contents().map_or(0, |at| at.as_ptr() as u64);
|
|
let on_lines = in_place_rows::on_lines(&device, first, row, bpr, h);
|
|
let pointer_passed = in_place_rows::passed(&device, "Pointer", on_lines);
|
|
let mut d = TextureDescriptor::texture_2d(PixelFormat::BGRA8Unorm, w, h, false);
|
|
d.usage = TextureUsage::SHADER_READ;
|
|
d.resource_options = buffer.resource_options();
|
|
let before = device.work_counts();
|
|
let texture = buffer
|
|
.new_texture_with_iosurface_plane(&d, IoSurfacePlane::new(0, bpr, false))
|
|
.unwrap();
|
|
let after = device.work_counts();
|
|
// The path the texture took, held to the setting and the check's verdict.
|
|
let aliased = after.host_textures_in_place - before.host_textures_in_place;
|
|
let refused = [
|
|
after.host_texture_shadows_refused_pitch
|
|
- before.host_texture_shadows_refused_pitch,
|
|
after.host_texture_shadows_refused_image
|
|
- before.host_texture_shadows_refused_image,
|
|
after.host_texture_shadows_refused_placement
|
|
- before.host_texture_shadows_refused_placement,
|
|
]
|
|
.iter()
|
|
.sum::<u64>();
|
|
let exact = after.host_texture_shadows_exact - before.host_texture_shadows_exact;
|
|
// SAFETY: the handle is only compared, while the texture lives.
|
|
let form = match unsafe { texture.vk_image() } {
|
|
Some(_) => "aliased",
|
|
None => "shadowed",
|
|
};
|
|
let took = match (in_place, pointer_passed) {
|
|
(false, _) => exact == 1 && form == "shadowed",
|
|
(true, true) => {
|
|
(aliased == 1 && form == "aliased") || (refused == 1 && form == "shadowed")
|
|
}
|
|
(true, false) => aliased == 0 && form == "shadowed",
|
|
};
|
|
if !took {
|
|
lapses.push(format!(
|
|
"{policy:?}, {name}: the texture is {form} (in place {aliased}, refused \
|
|
{refused}, exact {exact}), the pointer check passed: {pointer_passed}"
|
|
));
|
|
}
|
|
let mut d = TextureDescriptor::texture_2d(PixelFormat::BGRA8Unorm, w, h, false);
|
|
d.usage = TextureUsage::RENDER_TARGET;
|
|
d.resource_options = ResourceOptions::private();
|
|
let target = device.new_texture(&d).unwrap();
|
|
let read = device
|
|
.new_buffer(row * h, ResourceOptions::shared())
|
|
.unwrap();
|
|
let command_buffer = queue.command_buffer().unwrap();
|
|
let mut pass = RenderPassDescriptor::new();
|
|
let color = &mut pass.color_attachments[0];
|
|
color.texture = Some(target.clone());
|
|
color.load_action = LoadAction::Clear;
|
|
color.store_action = StoreAction::Store;
|
|
color.clear_color = ClearColor::new(0.0, 0.0, 0.0, 0.0);
|
|
let mut encoder = command_buffer.render_command_encoder(&pass).unwrap();
|
|
encoder.set_render_pipeline_state(&reads).unwrap();
|
|
encoder
|
|
.set_scissor_rect(ScissorRect::new(0, 0, w, top))
|
|
.unwrap();
|
|
encoder.set_vertex_buffer(Some(&corners), 0, 0).unwrap();
|
|
encoder.set_fragment_texture(Some(&texture), 0).unwrap();
|
|
encoder
|
|
.draw_primitives(PrimitiveType::Triangle, 0, 6, 1, 0)
|
|
.unwrap();
|
|
encoder.end_encoding().unwrap();
|
|
let mut blit = command_buffer.blit_command_encoder().unwrap();
|
|
blit.copy_from_texture_to_buffer(
|
|
&target,
|
|
0,
|
|
0,
|
|
Origin::new(0, 0, 0),
|
|
Size::new(w, h, 1),
|
|
&read,
|
|
0,
|
|
row,
|
|
row * h,
|
|
BlitOption::None,
|
|
)
|
|
.unwrap();
|
|
blit.end_encoding().unwrap();
|
|
let expected: Vec<u8> = {
|
|
let mut expected = pages.clone();
|
|
for &at in &targets {
|
|
expected[at] = pages[at] ^ 0xA5;
|
|
}
|
|
expected
|
|
};
|
|
let base = Instant::now() + LEAD;
|
|
let shift = Duration::from_micros(start.unsigned_abs());
|
|
let (writes_at, commit_at) = match start < 0 {
|
|
true => (base, base + shift),
|
|
false => (base + shift, base),
|
|
};
|
|
let at = Pages(mapping.pointer());
|
|
std::thread::scope(|scope| {
|
|
let (targets, expected) = (&targets, &expected);
|
|
let writer = scope.spawn(move || {
|
|
let pages = at;
|
|
spin_until(writes_at);
|
|
for (k, &byte) in targets.iter().enumerate() {
|
|
spin_until(writes_at + SPACING * k as u32);
|
|
// SAFETY: `byte` lies inside the mapping, which outlives the thread,
|
|
// and no other thread touches it meanwhile.
|
|
unsafe { pages.0.as_ptr().add(byte).write_volatile(expected[byte]) };
|
|
}
|
|
});
|
|
spin_until(commit_at);
|
|
command_buffer.commit().unwrap();
|
|
command_buffer.wait_until_completed_for(WAIT).unwrap();
|
|
writer.join().unwrap();
|
|
});
|
|
assert_eq!(command_buffer.error(), None);
|
|
let now = mapping.read(0..len);
|
|
let undone: Vec<usize> = (targets.iter().copied())
|
|
.filter(|&at| now[at] != expected[at])
|
|
.collect();
|
|
let changed = (0..len).filter(|&at| now[at] != expected[at]).count() - undone.len();
|
|
let contents = read.contents().unwrap();
|
|
// SAFETY: the buffer's `row * h` bytes, which no command writes any longer.
|
|
let got =
|
|
unsafe { std::slice::from_raw_parts(contents.as_ptr(), (row * h) as usize) };
|
|
let differ = (0..top)
|
|
.flat_map(|y| (0..w).map(move |x| (y, x)))
|
|
.filter(|&(y, x)| {
|
|
let at = (y * bpr + x * 4) as usize;
|
|
let out = (y * row + x * 4) as usize;
|
|
got[out..out + 4] != pages[at..at + 4]
|
|
})
|
|
.count();
|
|
let what = format!("{policy:?}, {name}, writes from {start} us of the commit");
|
|
eprintln!(
|
|
"FIRST-USE-WRITES on {}: {what}: {form}: {} of {} writes undone, {changed} \
|
|
other bytes changed, {differ} texels read differ",
|
|
device.name(),
|
|
undone.len(),
|
|
targets.len(),
|
|
);
|
|
if let Some(&at) = undone.first() {
|
|
lapses.push(format!(
|
|
"{what}: {} of {} CPU writes undone, the first at byte {at}",
|
|
undone.len(),
|
|
targets.len()
|
|
));
|
|
}
|
|
if changed != 0 {
|
|
lapses.push(format!("{what}: {changed} bytes no one wrote changed"));
|
|
}
|
|
if differ != 0 {
|
|
lapses.push(format!(
|
|
"{what}: {differ} texels of the top half read differ from the pages'"
|
|
));
|
|
}
|
|
drop((texture, buffer, target, read));
|
|
drop(mapping);
|
|
}
|
|
}
|
|
}
|
|
layer_reported_nothing_at_all(&messages);
|
|
}
|