Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,7 @@ failure. CI also runs `actionlint` on the workflows.
raises on its thread. Locally, with Graphics Tools installed:
`$env:XTP_TEST_ADAPTER='warp'; $env:XTP_D3D12_DEBUG='1'; $env:RUST_TEST_THREADS='1'; cargo test -p xtp-gpu --features diagnostics --locked`.
Wrap a call that hands the device invalid input on purpose in
`testgpu::expect_layer_errors`. A test with errors tracked by an issue
names their message ids with `testgpu::known_layer_errors`.
`testgpu::expect_layer_errors`.
- Such tests assert on properties (it decodes, sizes match, the hash of our
output is stable for your input), never on snapshots of game content, and
they write nothing below the repository (`xtp_core::ensure_outside_repo`).
Expand Down
12 changes: 8 additions & 4 deletions crates/xtp-gpu/src/host/buffers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1124,14 +1124,14 @@ mod tests {
/// What shaders write to a copy in the card's memory goes back to title
/// memory after the GPU ran, and only that: no snapshot is taken, the
/// title's bytes around stay. A later pass reads the first's output on
/// the card, and a GPU copy's destination goes back too.
/// the card, and a GPU copy's destination goes back too (a copy within
/// the buffer, staged in the card's memory, which the debug layer
/// passes when on).
#[test]
fn a_shader_write_to_a_copy_on_the_card_goes_back() {
use crate::testgpu::{Kernel, Pass, TitleBuffer, pattern};
let test = "host::buffers::tests::a_shader_write_to_a_copy_on_the_card_goes_back";
with_cards(test, |host| {
// The GPU copy within one buffer (`shadow::copy`).
crate::testgpu::known_layer_errors(&[538], "#45");
let kernel = Kernel::new(host);
let big = TitleBuffer::new(2 << 20);
big.set_words(|i| 0x7e00_0000 + i as u32);
Expand All @@ -1142,8 +1142,12 @@ mod tests {
kernel.dispatch(&first, Pass::Fill, at + SCRATCH, at + OUT);
first.close();
kernel.dispatch(&second, Pass::Copy, at + OUT, at + COPY);
// A GPU copy of 256 bytes of the first's output, page 5.
// A GPU copy of 256 bytes of the first's output, page 5: within
// the buffer, so staged, in the card's memory too.
let staged = || crate::shadow::CARD_STAGED.load(Ordering::Relaxed);
let before = staged();
assert!(crate::shadow::copy(&second, at + (5 << 16), at + OUT, 256));
assert_eq!(staged(), before + 1, "staged on the card");
second.close();
host.execute(&[&first]);
assert_eq!(shadow.snapshots(), 0, "no snapshot of a copy on the card");
Expand Down
1 change: 0 additions & 1 deletion crates/xtp-gpu/src/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2987,7 +2987,6 @@ mod tests {
/// source were read whole first.
#[test]
fn resource_and_memory_copies_run_on_the_gpu() {
crate::testgpu::known_layer_errors(&[538], "#45");
let test = "list::tests::gpu::resource_and_memory_copies_run_on_the_gpu";
let Some(gpu) = device(test) else { return };
let host = gpu.host();
Expand Down
60 changes: 47 additions & 13 deletions crates/xtp-gpu/src/shadow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3426,6 +3426,12 @@ fn card_limits() -> (u64, u64) {
pub(crate) static CARD_CHANGED: std::sync::atomic::AtomicUsize =
std::sync::atomic::AtomicUsize::new(0);

/// For tests: GPU copies within a copy in the card's memory that staged
/// in the card's memory too ([`copy`]).
#[cfg(test)]
pub(crate) static CARD_STAGED: std::sync::atomic::AtomicUsize =
std::sync::atomic::AtomicUsize::new(0);

/// Bytes of the card sides alive.
static CARD_BYTES: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);

Expand Down Expand Up @@ -4635,6 +4641,15 @@ pub(crate) fn existing(address: u64, len: u64) -> Option<Arc<Shadow>> {
/// title memory once the list has run. Returns false, recording nothing,
/// when neither side has a copy: title memory is then all there is, and
/// the caller copies it on the CPU at submission.
///
/// A copy within one host buffer (both ranges in the same copy) goes
/// through a staging buffer made for it, which the list keeps until it is
/// reset: one buffer cannot be the source and the destination of a GPU copy
/// at once. The staging buffer is in the card's memory when the buffer is
/// ([`Shadow::carded`]), else in system memory. If it cannot be made, this
/// returns false too (logged, counted): the caller then copies title memory
/// on the CPU at submission, which misses what shaders wrote to the source
/// earlier in the list and what they read of the destination later.
pub(crate) fn copy(list: &HostList, dst: u64, src: u64, len: u64) -> bool {
if len == 0 {
return true;
Expand All @@ -4652,12 +4667,36 @@ pub(crate) fn copy(list: &HostList, dst: u64, src: u64, len: u64) -> bool {
};
let (at, from_at) = (to.offset(dst), from.offset(src));
let same = from.buffer.as_raw() == to.buffer.as_raw();
// Overlapping ranges of one buffer go through a buffer of the list's
// own, as if the source were read whole first.
let through = if same && from_at < at + len && at < from_at + len {
let Some((through, _)) = host.shared_buffer(len) else {
// Copies within one buffer go through a staging buffer made for this
// copy: one buffer cannot be in COPY_SOURCE and COPY_DEST at once, and
// the debug layer rejects a source in COPY_DEST (message 538).
// Overlapping ranges then also read the source whole first. A buffer in
// the card's memory stages there too, not across the bus.
let through = if same {
let carded = from.carded();
let staging = if carded {
crate::host::buffers::card_buffer(&host.device, len)
} else {
host.shared_buffer(len).map(|(buffer, _)| buffer)
};
let Some(through) = staging else {
static FAILED: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
let count = FAILED.fetch_add(1, Ordering::Relaxed) + 1;
if count.is_power_of_two() {
tracing::warn!(
count,
len,
carded,
"no staging buffer for a GPU copy within one buffer: copied on the CPU at \
submission, without what shaders of the list wrote or read around it"
);
}
return false;
};
#[cfg(test)]
if carded {
CARD_STAGED.fetch_add(1, Ordering::Relaxed);
}
list.keep(&through);
Some(through)
} else {
Expand Down Expand Up @@ -4692,15 +4731,10 @@ pub(crate) fn copy(list: &HostList, dst: u64, src: u64, len: u64) -> bool {
transition(through, dest, source),
]);
list.list.CopyBufferRegion(b, at, through, 0, len);
list.list.ResourceBarrier(&[transition(b, dest, common)]);
} else if same {
// One buffer is the source and the destination: copied within
// it as a destination.
let b = &to.buffer;
list.list
.ResourceBarrier(&[uav_barrier(), transition(b, common, dest)]);
list.list.CopyBufferRegion(b, at, b, from_at, len);
list.list.ResourceBarrier(&[transition(b, dest, common)]);
list.list.ResourceBarrier(&[
transition(b, dest, common),
transition(through, source, common),
]);
} else {
let (a, b) = (&from.buffer, &to.buffer);
list.list.ResourceBarrier(&[
Expand Down
40 changes: 3 additions & 37 deletions crates/xtp-gpu/src/testgpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,17 +91,6 @@ impl Gpu {
thread_local! {
/// Set while this thread hands the device invalid input on purpose.
static EXPECTING: std::cell::Cell<bool> = const { std::cell::Cell::new(false) };
/// Message ids of known errors this thread's test raises, and the issue
/// that tracks them.
static KNOWN: std::cell::RefCell<(Vec<i32>, &'static str)> =
const { std::cell::RefCell::new((Vec::new(), "")) };
}

/// For a test that raises known errors (tracked by `issue`) with these
/// message ids: they are reported on standard error instead of failing it,
/// so the rest of what it checks still runs under the layer.
pub(crate) fn known_layer_errors(ids: &[i32], issue: &'static str) {
KNOWN.with(|k| *k.borrow_mut() = (ids.to_vec(), issue));
}

/// Runs `f`, which hands the device invalid input on purpose: the errors
Expand Down Expand Up @@ -191,20 +180,14 @@ fn serial() -> bool {
}

/// Takes the errors this thread raised, those raised on threads that are
/// no test's, and (run serially) those another test's thread left; known
/// ones (see [`known_layer_errors`]) are reported on standard error
/// instead.
/// no test's, and (run serially) those another test's thread left.
fn layer_errors() -> Vec<String> {
let me = thread_name();
let (known, issue) = KNOWN.with(|k| k.borrow().clone());
let serial = serial();
let mut errors = lock(&LAYER_ERRORS);
let (mut taken, mut tolerated) = (Vec::new(), 0);
let mut taken = Vec::new();
errors.retain(|e| {
if e.thread == me && known.contains(&e.id) {
tolerated += 1;
false
} else if e.thread == me {
if e.thread == me {
taken.push(format!("{} (message id {})", e.text, e.id));
false
} else if !is_test_thread(&e.thread) {
Expand All @@ -223,25 +206,9 @@ fn layer_errors() -> Vec<String> {
true
}
});
drop(errors);
if tolerated > 0 {
let _ = writeln!(
std::io::stderr(),
"KNOWN {me}: {tolerated} D3D12 debug layer error(s) with ids {known:?} ({issue})"
);
} else if !known.is_empty() && WATCHING_ON.load(std::sync::atomic::Ordering::Relaxed) {
let _ = writeln!(
std::io::stderr(),
"KNOWN {me}: no D3D12 debug layer error with ids {known:?} this run; drop the \
allowance once {issue} is fixed"
);
}
taken
}

/// Whether the layer reports to us (see [`watch`]).
static WATCHING_ON: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);

/// Has the debug layer report its errors to [`LAYER_ERRORS`] as they are
/// raised, on the thread that made the call, before the host moves them
/// into its log.
Expand Down Expand Up @@ -282,7 +249,6 @@ pub(crate) fn watch(host: &Host) -> bool {
)
}
.map_err(|e| format!("no D3D12 message callback: {e}"))?;
WATCHING_ON.store(true, std::sync::atomic::Ordering::Relaxed);
Ok(true)
});
match watching {
Expand Down
3 changes: 2 additions & 1 deletion docs/analysis/gpu-measure.md
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,8 @@ The ordering tests of G3 and G4 (`list::tests::gpu`) record small compute
kernels into title lists, standing in for the title's passes:

- dispatch, copy, dispatch in one submission;
- whole-buffer copies and memory copies, overlapping ones within one buffer;
- whole-buffer copies and memory copies, among them copies within one
buffer (staged in a buffer of their own, overlapping or not);
- a copy without host copies, which stays on the CPU;
- memory writes read by a later dispatch.

Expand Down
Loading