D3D12.X: a copy within one memory window reads through a scratch buffer (#958) #993

Manually merged
github merged 2 commits from gh-pr/993/head into gh-pr/993/base 2026-11-26 04:39:41 +00:00
Owner

@⁠zohaiblazuli on GitHub · 2026-11-24 10:04 UTC · original
zohaiblazuli:d3d12x-copy-within-window → main · +251 −4 · 4 files · 2 commits · merged 2026-11-24 13:38 UTC

CopyMemoryToMemoryX now copies through scratch when its source and destination share one unified-memory window. D3D12 needs the window in different states for the read and the write, which produced debug-layer error 538 on the direct copy. Fixes #958.

What is in it

  • list_x.rs caps live and retiring scratch allocations at 64 MiB across the process. Each PC list keeps one buffer. A larger copy in the same recording can use it in chunks. The previous copy path remains available when scratch is unavailable.
  • list_x.rs and real.rs track completion on the submitting queue. on_release drops ownership, and buffers still referenced by submitted work stay charged until their fences complete. The log counts recorded scratch copies on first use and powers of two.
  • The cache test covers budget, growth and deferred release. The debug-layer e2e test requires the scratch counter and checks for invalid copy-source state.

Tested

  • 392dc0c on Windows 11 build 26300, Intel Core i5-14600K, NVIDIA RTX 3060 (driver 32.0.16.1742), Rust 1.94.0 MSVC. Debug tests and release smoke runs.
  • cargo test --workspace -- --test-threads=4 with RUST_TEST_NOCAPTURE=1 reported 742 passes, 0 failures and 41 ignored. The reported passes include gpu_geometry_wave32_prefix_count_over_a_mask and gpu_wave32_program_in_wave64_hardware, which both emitted SKIPPED for lack of wave64.
  • The scratch budget test and debug-layer e2e passed. The e2e logged the first recorded scratch copy.

Regression run

xcl-smoke v1
commit: 47efa2ecea0a12934719ae69b81930cf02a4b1c3
gpu: NVIDIA GeForce RTX 3060
title: game
seconds: 60
exit: 0x0
frames shown: 1241 (PresentX 1241)
presents per 2 s (last 3): 60 60 55
device removals: 0
crashes: 0
peak commit: 9292 MB
xcl-smoke v1
commit: 392dc0c393b0cc669e5c9f15b29e96dae89c7967
gpu: NVIDIA GeForce RTX 3060
title: game
seconds: 60
exit: 0x0
frames shown: 1517 (PresentX 1517)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 10423 MB

Not tested

  • AMD and Intel on this head. The 60-second retail smoke run emitted no scratch counter, so the debug-layer test title provides the scratch-path coverage. Retail rendering and gameplay were not inspected.

AI use

  • Claude Code with Claude Opus 5.5 wrote and tested the original copy fix.
  • Codex (GPT-6, exact model variant unavailable) implemented the review follow-up, resolved the rebase conflict, ran the tests and smoke runs, and updated this description.
**@⁠zohaiblazuli** on GitHub · 2026-11-24 10:04 UTC · [original](https://github.com/xcl-dev/xcl/pull/993) `zohaiblazuli:d3d12x-copy-within-window` → `main` · +251 −4 · 4 files · 2 commits · merged 2026-11-24 13:38 UTC `CopyMemoryToMemoryX` now copies through scratch when its source and destination share one unified-memory window. D3D12 needs the window in different states for the read and the write, which produced debug-layer error 538 on the direct copy. Fixes #958. **What is in it** - `list_x.rs` caps live and retiring scratch allocations at 64 MiB across the process. Each PC list keeps one buffer. A larger copy in the same recording can use it in chunks. The previous copy path remains available when scratch is unavailable. - `list_x.rs` and `real.rs` track completion on the submitting queue. `on_release` drops ownership, and buffers still referenced by submitted work stay charged until their fences complete. The log counts recorded scratch copies on first use and powers of two. - The cache test covers budget, growth and deferred release. The debug-layer e2e test requires the scratch counter and checks for invalid copy-source state. **Tested** - `392dc0c` on Windows 11 build 26300, Intel Core i5-14600K, NVIDIA RTX 3060 (driver 32.0.16.1742), Rust 1.94.0 MSVC. Debug tests and release smoke runs. - `cargo test --workspace -- --test-threads=4` with `RUST_TEST_NOCAPTURE=1` reported 742 passes, 0 failures and 41 ignored. The reported passes include `gpu_geometry_wave32_prefix_count_over_a_mask` and `gpu_wave32_program_in_wave64_hardware`, which both emitted `SKIPPED` for lack of wave64. - The scratch budget test and debug-layer e2e passed. The e2e logged the first recorded scratch copy. **Regression run** ``` xcl-smoke v1 commit: 47efa2ecea0a12934719ae69b81930cf02a4b1c3 gpu: NVIDIA GeForce RTX 3060 title: game seconds: 60 exit: 0x0 frames shown: 1241 (PresentX 1241) presents per 2 s (last 3): 60 60 55 device removals: 0 crashes: 0 peak commit: 9292 MB ``` ``` xcl-smoke v1 commit: 392dc0c393b0cc669e5c9f15b29e96dae89c7967 gpu: NVIDIA GeForce RTX 3060 title: game seconds: 60 exit: 0x0 frames shown: 1517 (PresentX 1517) presents per 2 s (last 3): 60 60 60 device removals: 0 crashes: 0 peak commit: 10423 MB ``` **Not tested** - AMD and Intel on this head. The 60-second retail smoke run emitted no scratch counter, so the debug-layer test title provides the scratch-path coverage. Retail rendering and gameplay were not inspected. **AI use** - Claude Code with Claude Opus 5.5 wrote and tested the original copy fix. - Codex (GPT-6, exact model variant unavailable) implemented the review follow-up, resolved the rebase conflict, ran the tests and smoke runs, and updated this description.
CopyMemoryToMemoryX with its source and destination in the same unified
memory window copied inside one buffer while that buffer was in
COPY_DEST. D3D12 does not define a copy from a buffer that is not in
COPY_SOURCE, and the debug layer reports it on every such copy (id 538),
as in the test title's console_list_methods (#958). One buffer cannot be
in both states at once, so the bytes now go to a scratch buffer of the
command list first and from there to the destination. A list's
executions run one after another and its barriers order its copies, so
one scratch per list is not shared with work that may run at the same
time. Without a scratch buffer (over 64 MB, or 256 of them made) the
copy stays as it was.

The debug-layer e2e test now also fails on a copy whose source is not
in COPY_SOURCE: it failed with the id 538 error before this change.

Fixes #958

Assisted-by: Claude Code (Claude Opus 5.5)
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Keep one scratch buffer per recording list and charge released buffers to a 64 MiB process budget until their queue fences complete. Log the first scratch copy and powers of two so retail runs show whether they use the path.

Assisted-by: Codex (GPT-6, exact model variant unavailable)
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:39:36 +00:00
github manually merged commit 392dc0c393 into gh-pr/993/base 2026-11-26 04:39:41 +00:00
Author
Owner

@⁠dcastell1 on GitHub · 2026-11-24 10:13 UTC · original

Verified on Windows 11 10.0.26300, NVIDIA GeForce RTX 5090 (driver 621.24), Rust 1.100.0 MSVC 14.44.35207, debug build, on main@02bfd1d (baseline 727 passed, 0 failed, 41 ignored on this box):

patch -p1 < this PR            # clean, no fuzz
cargo test --workspace -- --test-threads=4
727 passed; 0 failed; 41 ignored   (exit 0)

Same count as the baseline — the change is exercised by the existing coverage (the e2e is updated, not extended) and nothing regresses.

Tree restored afterwards (full diff against a clean checkout), hardware adapter, no Wine, no game data. AI-assisted (opencode): applied the patch to a built tree, ran the suite, reverted it.

**@⁠dcastell1** on GitHub · 2026-11-24 10:13 UTC · [original](https://github.com/xcl-dev/xcl/pull/993#issuecomment-7128494359) Verified on Windows 11 10.0.26300, NVIDIA GeForce RTX 5090 (driver 621.24), Rust 1.100.0 MSVC 14.44.35207, debug build, on `main@02bfd1d` (baseline **727 passed, 0 failed, 41 ignored** on this box): ``` patch -p1 < this PR # clean, no fuzz cargo test --workspace -- --test-threads=4 727 passed; 0 failed; 41 ignored (exit 0) ``` Same count as the baseline — the change is exercised by the existing coverage (the e2e is updated, not extended) and nothing regresses. Tree restored afterwards (full diff against a clean checkout), hardware adapter, no Wine, no game data. AI-assisted (opencode): applied the patch to a built tree, ran the suite, reverted it.
Author
Owner

@⁠ivall on GitHub · 2026-11-24 11:00 UTC · original

Thanks – the fix is right, and the e2e assertion is a good guard. One thing before it can be merged: the scratch buffers' memory is not bounded in practice.

  • A buffer is kept per (list, size) for the life of the process, and a list that needs a larger one gets another one next to the first: up to 256 buffers of up to 64 MB each, i.e. up to 16 GB.
  • On WDDM a committed default-heap buffer is charged to the process's commit one to one, and commit is what the retail title runs out of during world loading on 32 GB machines – so this path has to stay small even in the worst case.

Could you:

  1. cap the scratch memory of the whole process (say 64 MB in all; past it, the old path as now),
  2. give a list's scratch back when the list is released (list_x::on_release is the hook), and replace a list's smaller scratch instead of keeping both once its earlier executions are done – or simply keep one per list,
  3. add a counter (e.g. a log line on the first use and powers of two) so a retail run shows whether a title takes this path at all.

With that I'll run it on a retail title and merge it.

**@⁠ivall** on GitHub · 2026-11-24 11:00 UTC · [original](https://github.com/xcl-dev/xcl/pull/993#issuecomment-7129536919) Thanks – the fix is right, and the e2e assertion is a good guard. One thing before it can be merged: the scratch buffers' memory is not bounded in practice. - A buffer is kept per (list, size) for the life of the process, and a list that needs a larger one gets another one next to the first: up to 256 buffers of up to 64 MB each, i.e. up to 16 GB. - On WDDM a committed default-heap buffer is charged to the process's commit one to one, and commit is what the retail title runs out of during world loading on 32 GB machines – so this path has to stay small even in the worst case. Could you: 1. cap the scratch memory of the whole process (say 64 MB in all; past it, the old path as now), 2. give a list's scratch back when the list is released (`list_x::on_release` is the hook), and replace a list's smaller scratch instead of keeping both once its earlier executions are done – or simply keep one per list, 3. add a counter (e.g. a log line on the first use and powers of two) so a retail run shows whether a title takes this path at all. With that I'll run it on a retail title and merge it.
Author
Owner

@⁠DavidM1911 on GitHub · 2026-11-24 12:22 UTC · original

Independent verification, merged onto current main 384f7f7 locally (clean merge).

Machine: Windows 11, Ryzen 9 7900X + RTX 3060 Ti; Rust stable MSVC, debug.

  • cargo test -p xcl --lib list_x: 24 passed, 0 failed.
  • cargo build --workspace && cargo test -p xcl --test e2e: 3 passed, 0 failed — the e2e title run (which exercises the copy path on the real backend) is green.

AI use: test runs by Devin (Cognition, SWE-2); no code changes.

**@⁠DavidM1911** on GitHub · 2026-11-24 12:22 UTC · [original](https://github.com/xcl-dev/xcl/pull/993#issuecomment-7131346897) Independent verification, merged onto current `main` `384f7f7` locally (clean merge). **Machine:** Windows 11, Ryzen 9 7900X + RTX 3060 Ti; Rust stable MSVC, debug. - `cargo test -p xcl --lib list_x`: **24 passed, 0 failed**. - `cargo build --workspace && cargo test -p xcl --test e2e`: **3 passed, 0 failed** — the e2e title run (which exercises the copy path on the real backend) is green. AI use: test runs by Devin (Cognition, SWE-2); no code changes.
Author
Owner

@⁠srcamarda on GitHub · 2026-11-24 12:22 UTC · original

Tested on AMD RDNA3 (RX 7800 XT): head 17fc02a

Tested

  • AMD Radeon RX 7800 XT 16 GB (RDNA3), driver 32.0.32029.2010; Ryzen 5 5600G, 32 GB; Windows 11 Pro 26300; rustc 1.100.0. Debug build for the tests, release build for the smoke runs.
  • cargo test --workspace -- --test-threads=4 on 17fc02a: 724 passed, 0 failed, 41 ignored. No SKIPPED GPU tests on this adapter.
  • In that whole-suite run, test_title_draws_with_a_root_signature_under_the_debug_layer skipped: Graphics Tools were not installed (no d3d12SDKLayers.dll). Its SKIPPED line comes from eprintln!, which libtest captures, so the run still shows it as ok. After installing Graphics Tools I ran it again on its own: cargo test -p xcl --test e2e test_title_draws_with_a_root_signature_under_the_debug_layer: 1 passed. The debug layer reported only id 93 ×3 and id 1046 ×1, the deliberate ones.
  • The same test title on main 47efa2e (release xcl run … -- --hello with XCL_D3D12_DEBUG=1): failures: 0, debug layer id 93 ×3, id 1046 ×1 and id 538 ×1 (CopyBufferRegion … D3D12_RESOURCE_STATE_COPY_DEST). So on AMD too, the PR removes the 538 error and nothing else changes.

Regression run (retail title 1.0.2.1, scripts/smoke.ps1 -Seconds 60, the same data folder for both runs)

xcl-smoke v1
commit: 47efa2ecea0a12934719ae69b81930cf02a4b1c3
gpu: AMD Radeon RX 7800 XT
title: RetailGame_Dump
seconds: 60
exit: 0x0
frames shown: 1564 (PresentX 1564)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 15720 MB
xcl-smoke v1
commit: 17fc02a3f97a2564e6379d939506373f4816a3d4
gpu: AMD Radeon RX 7800 XT
title: RetailGame_Dump
seconds: 60
exit: 0x0
frames shown: 1585 (PresentX 1585)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 15713 MB

The PR's run: 101 % of main's frames, the same peak commit, no device removal, no crash. No input was given during the runs.

Not tested

  • The retail title under the debug layer.
  • Whether the retail title reaches the copy within one window: there is no counter for that path, so this is a regression check only.

Claude Code (Claude Opus 5.5), from runs on the hardware above.

**@⁠srcamarda** on GitHub · 2026-11-24 12:22 UTC · [original](https://github.com/xcl-dev/xcl/pull/993#issuecomment-7131359312) **Tested on AMD RDNA3 (RX 7800 XT): head `17fc02a`** **Tested** - AMD Radeon RX 7800 XT 16 GB (RDNA3), driver 32.0.32029.2010; Ryzen 5 5600G, 32 GB; Windows 11 Pro 26300; rustc 1.100.0. Debug build for the tests, release build for the smoke runs. - `cargo test --workspace -- --test-threads=4` on `17fc02a`: 724 passed, 0 failed, 41 ignored. No `SKIPPED` GPU tests on this adapter. - In that whole-suite run, `test_title_draws_with_a_root_signature_under_the_debug_layer` skipped: Graphics Tools were not installed (no `d3d12SDKLayers.dll`). Its `SKIPPED` line comes from `eprintln!`, which libtest captures, so the run still shows it as `ok`. After installing Graphics Tools I ran it again on its own: `cargo test -p xcl --test e2e test_title_draws_with_a_root_signature_under_the_debug_layer`: 1 passed. The debug layer reported only id 93 ×3 and id 1046 ×1, the deliberate ones. - The same test title on `main` `47efa2e` (release `xcl run … -- --hello` with `XCL_D3D12_DEBUG=1`): `failures: 0`, debug layer id 93 ×3, id 1046 ×1 and **id 538 ×1** (`CopyBufferRegion` … `D3D12_RESOURCE_STATE_COPY_DEST`). So on AMD too, the PR removes the 538 error and nothing else changes. **Regression run** (retail title 1.0.2.1, `scripts/smoke.ps1 -Seconds 60`, the same data folder for both runs) ``` xcl-smoke v1 commit: 47efa2ecea0a12934719ae69b81930cf02a4b1c3 gpu: AMD Radeon RX 7800 XT title: RetailGame_Dump seconds: 60 exit: 0x0 frames shown: 1564 (PresentX 1564) presents per 2 s (last 3): 60 60 60 device removals: 0 crashes: 0 peak commit: 15720 MB ``` ``` xcl-smoke v1 commit: 17fc02a3f97a2564e6379d939506373f4816a3d4 gpu: AMD Radeon RX 7800 XT title: RetailGame_Dump seconds: 60 exit: 0x0 frames shown: 1585 (PresentX 1585) presents per 2 s (last 3): 60 60 60 device removals: 0 crashes: 0 peak commit: 15713 MB ``` The PR's run: 101 % of `main`'s frames, the same peak commit, no device removal, no crash. No input was given during the runs. **Not tested** - The retail title under the debug layer. - Whether the retail title reaches the copy within one window: there is no counter for that path, so this is a regression check only. _Claude Code (Claude Opus 5.5), from runs on the hardware above._
Author
Owner

@⁠zohaiblazuli on GitHub · 2026-11-24 12:39 UTC · original · edited 2026-11-24 12:41 UTC

@⁠ivall Thanks for reviewing this. The updated head 392dc0c caps scratch at 64 MiB across the process, keeps one buffer per command list, and reclaims released buffers after the GPU finishes. The debug-layer test title logged its first scratch copy. The retail smoke run passed and emitted no scratch counter.

The PR description has the workspace test results and both smoke runs. Could you approve the fork CI run when you have a chance?

Also, if possible, I’d appreciate the Contributor role on the Discord server. My Discord is @overtimerumble (Claudenberg). :)

**@⁠zohaiblazuli** on GitHub · 2026-11-24 12:39 UTC · [original](https://github.com/xcl-dev/xcl/pull/993#issuecomment-7131745353) · edited 2026-11-24 12:41 UTC @⁠ivall Thanks for reviewing this. The updated head `392dc0c` caps scratch at 64 MiB across the process, keeps one buffer per command list, and reclaims released buffers after the GPU finishes. The debug-layer test title logged its first scratch copy. The retail smoke run passed and emitted no scratch counter. The PR description has the workspace test results and both smoke runs. Could you approve the [fork CI run](https://github.com/ivall/xcl/actions/runs/45395385108) when you have a chance? Also, if possible, I’d appreciate the Contributor role on the Discord server. My Discord is `@overtimerumble` (Claudenberg). :)
Author
Owner

@⁠ivall on GitHub · 2026-11-24 13:38 UTC · original

Merged – thanks for the rework: the 64 MiB budget, one buffer per list, release after the GPU and the counter are what I asked for. Two things on top, in 930e8b4:

  • on_create gives up a scratch buffer left at the new list's address by a list whose release was not seen; it would have been taken as the new recording's own, without asking whether the GPU is done with it. With a test that fails without it.
  • the README lines are left out: the README tells users what works, and these are internals.

Tested on the merged tree: cargo test --workspace --no-fail-fast -- --test-threads=4: 751 passed, 0 failed, 41 ignored, no SKIPPED (the debug-layer e2e test ran and passed; AMD). In the e2e run the test title's copy went through the scratch buffer. Retail title, 50 s at its menu without input: exit 0, no crash, no device removal, 1353 frames against 1373 on main, peak process commit 19.2 GB against 17.0 GB – the scratch counter logged nothing in that run, so the path was not taken and the difference is not from it.

Not tested: a retail title that takes this path.

**@⁠ivall** on GitHub · 2026-11-24 13:38 UTC · [original](https://github.com/xcl-dev/xcl/pull/993#issuecomment-7133116669) Merged – thanks for the rework: the 64 MiB budget, one buffer per list, release after the GPU and the counter are what I asked for. Two things on top, in 930e8b4: - `on_create` gives up a scratch buffer left at the new list's address by a list whose release was not seen; it would have been taken as the new recording's own, without asking whether the GPU is done with it. With a test that fails without it. - the README lines are left out: the README tells users what works, and these are internals. **Tested** on the merged tree: `cargo test --workspace --no-fail-fast -- --test-threads=4`: 751 passed, 0 failed, 41 ignored, no SKIPPED (the debug-layer e2e test ran and passed; AMD). In the e2e run the test title's copy went through the scratch buffer. Retail title, 50 s at its menu without input: exit 0, no crash, no device removal, 1353 frames against 1373 on `main`, peak process commit 19.2 GB against 17.0 GB – the scratch counter logged nothing in that run, so the path was not taken and the difference is not from it. **Not tested**: a retail title that takes this path.
Author
Owner

@⁠DavidM1911 on GitHub · 2026-11-26 03:32 UTC · original

Re-verification on RTX 3060 Ti (Windows 11), new head merged with current main:

  • cargo test -p xcl --lib: all passed, 0 failed, 27 ignored. The earlier merge conflict is resolved.

Thanks!

— Devin

**@⁠DavidM1911** on GitHub · 2026-11-26 03:32 UTC · [original](https://github.com/xcl-dev/xcl/pull/993#issuecomment-7181802737) Re-verification on RTX 3060 Ti (Windows 11), new head merged with current `main`: - `cargo test -p xcl --lib`: all passed, 0 failed, 27 ignored. The earlier merge conflict is resolved. Thanks! — Devin
This discussion has been locked. Commenting is limited to contributors.
No description provided.