Repository navigation
GPU: guard subresource and region bounds in texture copies and MSAA resolves - #2047
fuzeofficlal wants to merge 1 commit into
Conversation
|
Triage verdict — Keep the bounds checks, retain valid MSAA copies and regional resolves ( Evidence
Checks and limits
Next step
This verification was AI-assisted. |
catminou4
left a comment
There was a problem hiding this comment.
Static review only: victory-gpu is Windows-only (D3D12/windows_sys) — nothing compiles or runs on macOS; source review of the diff.
Subresource/region bounds guarded in texture copies — memory-safety hardening on the copy path.
eb6f971 to
af6e4a0
Compare
|
Updated in
|
|
Automated triage for the maintainers. Merge candidate. On your reply: Generated by Claude Code |
…esolves Validate subresource indices (including planar depth and stencil copies), mip and footprint extents, block alignment, destination coordinates, sample counts, and element sizes before recording CopyTextureRegion, CopyResource, ResolveSubresource, or ResolveSubresourceRegion on the host command list. Empty CopyTextureRegion source boxes (left >= right, top >= bottom, or front >= back) return silently per the D3D12 specification, while every non-empty rejection logs once per reason so dropped copies from host translation mismatches (such as shared placed textures in resource::shared_texture before per-subresource tracking Marvisak#594, console 16-bit depth-stencil format translation, or mismatched mip/extent host textures) remain visible. Whole-subresource multisampled CopyTextureRegion calls with matching sample descriptions, zero destination offsets, and a null source box continue to run, and mismatched resolve subresource extents route to the regional clipping path while out-of-bounds or incompatible calls return before transitioning resources or allocating upload memory.
af6e4a0 to
bd6b3f9
Compare
|
Updated in
|
|
Native Windows/NVIDIA evidence for the whole-resource shared-host-texture guard, plus two supplemental regression tests for bd6b3f9. RTX 5070 Ti 16 GB, driver 621.01; i7-14700, 32 GB RAM, Windows 11 Pro 26200. Runtime motivation: before the guard, a D3D12 debug run logged 48 ID 538 errors from CopyResource: the 2560x1440, format 19 depth texture destination was in COPY_SOURCE rather than COPY_DEST (planes/subresources 0 and 1). I independently narrowed this down to distinct title objects sharing one host resource, then found the equivalent guard already in this PR. With only the equivalent whole-CopyResource early return added to the local integration stack, the guard's once-only diagnostic fired during the title run. The 345.7-second debug run had zero texture ID 538 errors and no logged device removal. It was stopped manually. This is evidence for this particular guard, not validation of every check in the larger PR. Frames reached differ, so these counts are observational, not a benchmark. Buffer state errors and shader translation refusals remain; rendering is still severely malformed and not playable. A preceding attempt ended early without a captured exit code or logged GPU-removal message; it is not counted as a successful stability run. Synthetic validation, independently on this PR's exact head plus the two tests below:
The same two tests also pass on main 940d32f with only the narrow guard added (hardware and WARP). Separately, the combined local stack passes 752 workspace tests (11 ignored) and 166 GPU debug tests (3 ignored), build, Clippy with -D warnings, and asset policy. Those combined-stack counts are not claimed for this PR alone. Runtime stack for reproducibility: main 940d32f, #2177 plus a local fallback fix, GPU commit 93c4e18 from #1926, #1762, #1906, #2098, #89, #189, #1152, #1803, #2276 and #2155, then the narrow equivalent guard. Wave64 auto selected Emulate; GPU timing off, default loop limit, no skipped pipelines. The full #2047 was used for the isolated synthetic tests, not for this title run. The patch below adds only the two synthetic tests to bd6b3f9. No game data, private paths, screenshots or raw logs are included. This is supplemental evidence for the existing PR, not a claim to have originated its guard. Integration and test authoring were assisted by OpenAI Codex. Supplemental test-only patch against bd6b3f9diff --git a/crates/victory-gpu/src/list.rs b/crates/victory-gpu/src/list.rs
index 4e20aae..11d6fb5 100644
--- a/crates/victory-gpu/src/list.rs
+++ b/crates/victory-gpu/src/list.rs
@@ -3047,4 +3047,221 @@ mod tests {
}
assert_eq!(DEBUG_ERRORS.load(Ordering::Relaxed), 0);
}
+
+ #[cfg(windows)]
+ #[test]
+ fn whole_texture_copies_skip_shared_host_resources() {
+ use windows::core::Interface;
+ let Some(host) = crate::host::host() else {
+ return;
+ };
+ let null = std::ptr::null();
+ let iid = crate::object::guid(
+ 0x696442be,
+ 0xa72e,
+ 0x4059,
+ [0xbc, 0x79, 0x5b, 0x5c, 0x98, 0x04, 0x0f, 0xad],
+ );
+ let release = |o: This| slot::<extern "system" fn(This) -> u32>(o, 0x10)(o);
+ let mut allocator = null;
+ assert_eq!(
+ create_allocator(null, 0, &ALLOCATOR_IIDS[0], &mut allocator),
+ S_OK
+ );
+ let mut list = null;
+ assert_eq!(
+ create_list(null, 0, 0, allocator, null, &LIST_IIDS[1], &mut list),
+ S_OK
+ );
+ let hl = host_list(list).expect("a host command list");
+
+ // Distinct title objects at one address share a host texture. Cover
+ // both CPU-uploaded colour and the two-plane depth/stencil case.
+ for (format, flags) in [(28u32, 0u32), (118, 2)] {
+ let layout = std::alloc::Layout::from_size_align(1 << 20, 1 << 16).unwrap();
+ // SAFETY: a nonzero layout; title texture caches retain this memory.
+ let memory = unsafe { std::alloc::alloc_zeroed(layout) };
+ assert!(!memory.is_null());
+ let words = [
+ 3,
+ 0,
+ 0,
+ 0,
+ 32,
+ 0,
+ 32,
+ 1 | 1 << 16,
+ format,
+ 1,
+ 0,
+ 0,
+ flags,
+ 0,
+ ];
+ let make = || {
+ let mut out = null;
+ assert_eq!(
+ crate::resource::create_at_address(
+ null,
+ memory as u64,
+ words.as_ptr().cast(),
+ 0,
+ null.cast(),
+ &iid,
+ &mut out,
+ ),
+ S_OK
+ );
+ out
+ };
+ let (first, alias) = (make(), make());
+ assert_ne!(first, alias);
+ let a = crate::resource::host_copy(first).unwrap();
+ let b = crate::resource::host_copy(alias).unwrap();
+ assert_eq!(a.texture.as_raw(), b.texture.as_raw());
+ let initial = a.state.load(Ordering::Relaxed);
+ for written in [false, true] {
+ a.host_written.store(written, Ordering::Relaxed);
+ copy_resource(list, alias, first);
+ copy_resource_x(list, first, alias, 0);
+ copy_resource(list, first, first);
+ // A no-op must not suppress a later upload from title memory,
+ // allocate staging, or claim the list's first real texture use.
+ assert_eq!(a.host_written.load(Ordering::Relaxed), written);
+ assert!(a.staging.lock().unwrap().is_none());
+ assert!(hl.first_use(a.texture.as_raw() as usize));
+ assert_eq!(close(list), S_OK);
+ host.execute(&[hl]);
+ assert_eq!(host.log_messages(), None);
+ assert_eq!(a.state.load(Ordering::Relaxed), initial);
+ assert_eq!(reset(list, allocator, null), S_OK);
+ }
+ for object in [alias, first] {
+ assert_eq!(release(object), 0);
+ }
+ }
+ for object in [list, allocator] {
+ assert_eq!(release(object), 0);
+ }
+ }
+
+ #[cfg(windows)]
+ #[test]
+ fn whole_texture_copies_between_distinct_hosts_preserve_pixels() {
+ use windows::Win32::Graphics::Direct3D12::*;
+ let Some(host) = crate::host::host() else {
+ return;
+ };
+ let null = std::ptr::null();
+ let iid = crate::object::guid(
+ 0x696442be,
+ 0xa72e,
+ 0x4059,
+ [0xbc, 0x79, 0x5b, 0x5c, 0x98, 0x04, 0x0f, 0xad],
+ );
+ let make = || {
+ let heap = [1u32, 0, 0, 0, 0];
+ let words = [3u32, 0, 0, 0, 32, 0, 32, 1 | 1 << 16, 28, 1, 0, 0, 1, 0];
+ let mut out = null;
+ assert_eq!(
+ crate::resource::create_committed(
+ null,
+ heap.as_ptr().cast(),
+ 0,
+ words.as_ptr().cast(),
+ 0,
+ null.cast(),
+ &iid,
+ &mut out,
+ ),
+ S_OK
+ );
+ out
+ };
+ let (src, dst) = (make(), make());
+ let from = crate::resource::host_resource(src).unwrap();
+ let to = crate::resource::host_resource(dst).unwrap();
+ assert_ne!(from, to);
+ let mut allocator = null;
+ assert_eq!(
+ create_allocator(null, 0, &ALLOCATOR_IIDS[0], &mut allocator),
+ S_OK
+ );
+ let mut list = null;
+ assert_eq!(
+ create_list(null, 0, 0, allocator, null, &LIST_IIDS[1], &mut list),
+ S_OK
+ );
+ let hl = host_list(list).unwrap();
+ ensure_state(hl, src, D3D12_RESOURCE_STATE_RENDER_TARGET.0 as u32);
+ // SAFETY: a live render target and an open list; its prelude sets the state.
+ unsafe {
+ hl.list
+ .ClearRenderTargetView(host.rtv(from, None), &[1.0, 0.0, 1.0, 1.0], None)
+ };
+ crate::texture::host_written(src);
+ copy_resource(list, src, src);
+ copy_resource_x(list, dst, src, 0);
+ assert!(
+ crate::resource::host_copy(dst)
+ .unwrap()
+ .host_written
+ .load(Ordering::Relaxed)
+ );
+
+ let mut footprint = D3D12_PLACED_SUBRESOURCE_FOOTPRINT::default();
+ let mut bytes = 0;
+ // SAFETY: plain queries on a live texture with one subresource.
+ unsafe {
+ host.device.GetCopyableFootprints(
+ &to.GetDesc(),
+ 0,
+ 1,
+ 0,
+ Some(&mut footprint),
+ None,
+ None,
+ Some(&mut bytes),
+ )
+ };
+ let (output, mapped) = host.shared_buffer(bytes).unwrap();
+ let read_to = D3D12_TEXTURE_COPY_LOCATION {
+ // SAFETY: a borrowed COM pointer; output stays live through submission.
+ pResource: unsafe { std::mem::transmute_copy(&output) },
+ Type: D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT,
+ Anonymous: D3D12_TEXTURE_COPY_LOCATION_0 {
+ PlacedFootprint: footprint,
+ },
+ };
+ let read_from = D3D12_TEXTURE_COPY_LOCATION {
+ // SAFETY: a borrowed COM pointer; dst stays live through submission.
+ pResource: unsafe { std::mem::transmute_copy(to) },
+ Type: D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX,
+ Anonymous: D3D12_TEXTURE_COPY_LOCATION_0 {
+ SubresourceIndex: 0,
+ },
+ };
+ ensure_state(hl, dst, COPY_SOURCE);
+ // SAFETY: valid locations and states; the buffer promotes to COPY_DEST.
+ unsafe {
+ hl.list
+ .CopyTextureRegion(&read_to, 0, 0, 0, &read_from, None)
+ };
+ assert_eq!(close(list), S_OK);
+ host.execute(&[hl]);
+ assert_eq!(host.log_messages(), None);
+ for y in 0..32usize {
+ for x in 0..32usize {
+ let at = y * footprint.Footprint.RowPitch as usize + x * 4;
+ // SAFETY: within the mapped footprint; execute waits for completion.
+ let pixel = unsafe { std::slice::from_raw_parts(mapped.add(at), 4) };
+ assert_eq!(pixel, [255, 0, 255, 255], "pixel ({x}, {y})");
+ }
+ }
+ assert_eq!(reset(list, allocator, null), S_OK);
+ let release = |o: This| slot::<extern "system" fn(This) -> u32>(o, 0x10)(o);
+ for object in [dst, src, list, allocator] {
+ assert_eq!(release(object), 0);
+ }
+ }
} |
Summary
D3D12_BOXregions, destination coordinates, sample counts, subresource dimensions, same-host-texture aliases, and element sizes before recordingCopyTextureRegion,CopyResource, orResolveSubresourceon the host command list.Testing
VICTORY_D3D12_DEBUG=1 cargo test -p victory-gpu --lib --locked -- copy_and_resolve_reject(verified on NVIDIA RTX 5090, AMD Radeon Graphics, and D3D12 WARP with 0 debug errors)VICTORY_D3D12_WARP=1 cargo test -p victory-gpu --features diagnostics --lockedcargo clippy --workspace --all-targets --all-features --locked -- -D warningscargo xtask policy