Skip to content

D3D12.X: copy texture regions with unaligned footprints through scratch - #2873

Open
tiagonleal wants to merge 1 commit into
xcl-dev:mainfrom
tiagonleal:copy-texture-region-aligned-footprints
Open

tiagonleal wants to merge 1 commit into
xcl-dev:mainfrom
tiagonleal:copy-texture-region-aligned-footprints

Conversation

@tiagonleal

Copy link
Copy Markdown
Contributor

copy_texture_region_x passed console placed footprints to the PC as they were. On PC D3D12, a placed footprint's Offset must be aligned to D3D12_TEXTURE_DATA_PLACEMENT_ALIGNMENT (512) "unless D3D12_FEATURE_DATA_D3D12_OPTIONS13::UnrestrictedBufferTextureCopyPitchSupported is TRUE" (D3D12_PLACED_SUBRESOURCE_FOOTPRINT). RowPitch must be a multiple of D3D12_TEXTURE_DATA_PITCH_ALIGNMENT (256) (D3D12_SUBRESOURCE_FOOTPRINT). When the capability is present, offset and row pitch only need to be aligned to the format's unit size (DirectX-Specs, VulkanOn12). Invalid parameters to CopyTextureRegion are undefined behaviour and "might result in incorrect rendering, clipping, no copy, or even the removal of the rendering device" (CopyTextureRegion). The console accepts tighter footprints. The code already noticed these footprints, but only logged them under XCL_TRACE_FRAME, for the source side only and before adding the placement delta.

Such a copy now goes through the list's copy scratch (copy_scratch) at offset 0, with the pitch rounded up to 256. XCL queries OPTIONS13 once. If the device reports UnrestrictedBufferTextureCopyPitchSupported and the offset and pitch are whole units of the format, the call goes through unchanged. A failed query counts as FALSE.

For buffer to texture, only the rows and slices in the source box are copied into the scratch. That is one CopyBufferRegion when only the offset is unaligned, or one per row of blocks otherwise, stopping at the end of the source buffer. For texture to buffer, the texture is copied into the scratch and only the bytes the copy writes are copied back. Bytes per block come from GetCopyableFootprints of the texture subresource. Aligned footprints take the same path as before. If the list has no scratch large enough, the call is issued as before and a warning is logged. That happens in a bundle, when the 64 MiB budget is used up, or when the list already recorded a smaller scratch.

Based on e120925e, the last main commit before #2555 (see Regression run).

What is in it

  • crates/xcl/src/hle/d3d12x/list_x.rs:
    • pure footprint_plan: pass through, or the scratch pitch, size and buffer copies; takes the unit size when the capability is present
    • unrestricted_copy_pitch: the cached OPTIONS13 probe
    • copy_texture_region_realigned: the copies and scratch transitions, called from copy_texture_region_x after pc_location; the box is clamped to the rows and slices it names
  • Tests in footprint_plan_tests:
    • aligned footprint
    • odd pitch
    • odd offset with aligned pitch
    • both odd, with the last row clamped
    • BC1 rows of blocks
    • 3D slices
    • texture to buffer
    • source box rows and slices
    • capability TRUE and FALSE

Tested

  • Windows 11 Pro 10.0.26300, NVIDIA GeForce RTX 5070 Ti 16 GB (driver 617.42), AMD Ryzen 7 9800X3D, 32 GB RAM, rustc 1.100.0 (0ef1bc386 2026-11-09); debug tests, release build
  • cargo test -p xcl --lib footprint_plan: 9 passed, 0 failed, 3 of 3 runs on 1f3d506
  • With footprint_plan forced to return PassThrough (the old behaviour) in the working tree, the same command gives 1 passed, 8 failed. The 7 planner tests that expect a scratch copy fail at the panic!("expected a scratch copy") in the test helper (list_x.rs:4491), and unrestricted_copy_pitch_takes_unit_aligned_footprints fails at its assertion (list_x.rs:4505). Only an_aligned_footprint_goes_as_it_is passes.
  • cargo test --workspace --no-fail-fast -- --test-threads=4, run twice on 1f3d506: 1447 passed, 4 failed, 47 ignored both times (25 SKIPPED lines: 21 no wave64, 1 no heap windows, 3 recompiler cases refused as unsupported). The 4 failures are gpu::dxil::fuzz_tests::fuzz_validate_and_gpu_0..3 (CreateComputePipelineState returns E_OUTOFMEMORY, 0x8007000E). They fail the same way on e120925e on this machine (cargo test -p xcl --lib fuzz_validate_and_gpu: 0 passed, 4 failed).

Regression run
scripts/smoke.ps1 -Seconds 60 -Title 9cc6a39e, default settings, release builds of both commits in clean trees, main first, one run each.

main (e120925):

xcl-smoke v1
commit: e120925e3abc9956a8b9bae631969d32e8ccd43c
gpu: NVIDIA GeForce RTX 5070 Ti
title: 9cc6a39e
seconds: 60
exit: 0x0
frames shown: 1607 (PresentX 1607)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 16118 MB

This PR:

xcl-smoke v1
commit: 1f3d506d0d87f72e3179e66c333e8e5f76935f9b
gpu: NVIDIA GeForce RTX 5070 Ti
title: 9cc6a39e
seconds: 60
exit: 0x0
frames shown: 1614 (PresentX 1614)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 16195 MB

NVIDIA profile for xcl.exe: shader heap 1024 MB

Both runs are on e120925e, the last main commit before #2555; I could not finish a first launch preparation on a commit that has it (stopped after 2 h 20 min at about 23,860 of 26,503 records on this machine), so the preparation added by #2555 is not covered.

Not tested

  • This adapter reports UnrestrictedBufferTextureCopyPitchSupported TRUE (read with a standalone CheckFeatureSupport(OPTIONS13) call on this machine). So the scratch path runs here only for footprints whose offset or pitch is not a whole unit of the format. The probe log line did not appear in the PR's smoke log, so no copy in that 60 s run reached it.
  • The console-list replay path (console_list.rs:855) still passes footprints as they are
  • No device test of the realigned copy itself: the tests cover the planner, not the barriers, the scratch copies, the probe or GetCopyableFootprints
  • Planar formats
  • The capability probe is cached for the process, which assumes one device
  • Whether the retail title issues misaligned footprints is not known: the existing trace line prints only under XCL_TRACE_FRAME, which our runs did not set
  • A main commit that contains xcl run: prepare shaders in the game window before title execution #2555
  • AMD and Intel GPUs

AI use

  • Claude Code with Claude Fable 5.2 and Anthropic subagents (Claude Opus 5.5, Claude Sonnet 5.5) helped developing.

🤖 Generated with Claude Code

PC D3D12 requires a placed footprint's Offset to be a multiple of
D3D12_TEXTURE_DATA_PLACEMENT_ALIGNMENT (512) and its RowPitch a multiple
of D3D12_TEXTURE_DATA_PITCH_ALIGNMENT (256), unless the device reports
OPTIONS13.UnrestrictedBufferTextureCopyPitchSupported, in which case both
only need to be multiples of the format's unit size. CopyTextureRegion
with an invalid footprint is undefined behaviour (incorrect rendering, no
copy, or device removal). Console footprints can be packed tighter.
copy_texture_region_x noticed this only under XCL_TRACE_FRAME and still
passed the footprint to the PC.

footprint_plan (pure) decides between passing the call through and
copying through the list's copy scratch at offset 0 with the pitch rounded
up to 256. It passes the call through when the footprint is aligned, or
when the device has the unrestricted capability (probed once; a failed
query counts as unsupported) and offset and pitch are whole units. For
buffer->texture only the rows and slices of the source box are copied:
one buffer copy when only the offset is off, one per row of blocks
otherwise, clamped to the end of the source buffer. For texture->buffer
only the bytes the copy writes are copied back, so bytes between rows and
outside the box keep their contents. With no scratch large enough the
call goes as before, with a warning.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant