Skip to content

gpu, docs: copy within one buffer through a staging buffer - #50

Merged
simnJS merged 2 commits into
mainfrom
gpu/copies-within-one-buffer
Nov 25, 2026
Merged

simnJS merged 2 commits into
mainfrom
gpu/copies-within-one-buffer

Conversation

@fogydes

@fogydes fogydes commented Nov 25, 2026 •

Copy link
Copy Markdown
Collaborator

What and why

Copying between two ranges of one host buffer left the source in COPY_DEST, which D3D12 rejects with message 538. Every same-buffer copy now uses a separate staging buffer so the source and destination states remain valid.

Card-resident copies stage on the card; other copies use a shared buffer. The staging resource stays alive with the recording and returns to COMMON after the copy. If allocation fails, a counted warning reports the size and memory kind before the existing CPU fallback. That fallback can miss shader writes earlier in the list; bounded staging/reuse remains follow-up work.

The two message-538 allowances are removed, and gpu-measure.md describes staging for all same-buffer copies. With #49 and #51 on main, these were the last allowances: remove the unused allowance helper and reporting state from testgpu.rs, plus its stale CONTRIBUTING.md reference. Deliberate-invalid-input scopes and strict debug-layer failures remain intact.

Part of: #45
Depends on: none

Area

gpu, docs: shadow.rs, copy regression tests in list.rs and host/buffers.rs, testgpu.rs, CONTRIBUTING.md, and docs/analysis/gpu-measure.md.

Tests

  • cargo xtask pr-check passes on my machine
  • New or changed behaviour has tests that need no game files, or I explain below why it cannot

The existing non-overlapping same-buffer test and card-buffer shader-write/copy-back test exercise the fix. The latter also checks that card staging was used. Fresh strict WARP diagnostics and full WARP PR-check results are recorded in the review reply for the published head.

Ran on a title: no new title run for this integration cleanup.

Provenance

  • I wrote all of this change myself for XTP, or it only moves code already in XTP.
  • Some of it comes from elsewhere. Each source, its licence and the files concerned are listed here, and every licence is compatible with GPL-3.0-or-later:

AI assistance: Claude Code drafted the initial fix and the card-staging, failure-warning, documentation and test follow-ups. Codex rebased, reviewed callers and verified the result. No external code was copied or translated. I answer for every line.

Checklist

  • No game data, keys, console SDK code or headers, and nothing that circumvents DRM (asset policy)
  • No code copied from xcl or AnyPS5 (references only: read them to understand, write your own)
  • I agree to license this contribution under GPL-3.0-or-later
  • Title and every commit follow <area>: <summary>; one topic; under 400 changed lines, or the reason is above
  • Docs updated where behaviour, commands or variables changed (README, docs/)

@github-actions github-actions Bot added the area/gpu touches crates/xtp-gpu label Nov 25, 2026
@fogydes
fogydes requested a review from simnJS November 25, 2026 06:27
@github-actions github-actions Bot added the size/S 10 to 99 changed lines label Nov 25, 2026
@fogydes
fogydes changed the base branch from ci/gpu-tests-debug-layer to main November 25, 2026 06:36
@fogydes
fogydes force-pushed the gpu/copies-within-one-buffer branch from 479ba8d to e634776 Compare November 25, 2026 06:42
simnJS
simnJS previously approved these changes Nov 25, 2026

@simnJS simnJS left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for tracking this down. The fix is right: one buffer cannot be in COPY_SOURCE and COPY_DEST at once, so a copy-engine copy within one buffer has to go through a second buffer. The two tests whose 538 allowances you removed do cover the non-overlapping case (copy_memory(at + 448, at, 64) in list.rs, the page-5 copy inside one card buffer in host/buffers.rs), and the debug-layer job is green. Approving.

Two points for a follow-up:

  1. Cost per copy. A non-overlapping copy within one buffer used to allocate nothing. Now each one places a new System-pool buffer on the recording thread (CreatePlacedResource, Map, the lease, at least 64 KiB of pool held until the list is reset), and Pool::place zeroes any reused range on the CPU although the GPU overwrites all of it. With XTP_CARD_BUFFERS=1, a copy that stayed in card memory now crosses PCIe twice. A list recording hundreds of small copies inside one ring buffer would hold tens of MiB of the pool. A count of same-buffer copies per frame would show whether this matters; if it does, one staging buffer per list reused across its copies, or no zeroing for this use, would fix it.

  2. Failure path. If shared_buffer fails, copy now returns false for any same-buffer copy, and the caller falls back to a CPU copy of title memory at submission, which misses shader writes made earlier in the list. Overlapping copies already behaved so and it needs memory exhaustion, but a once! warning there would help.

Nits: the title becomes the squash commit, so it should be imperative, e.g. "gpu: copy within one buffer through a staging buffer". Please also drop the Co-Authored-By trailer; the description already records the AI assistance, which is where the project keeps it.

I will merge as soon as the trailer is gone and the title is updated (a force-push of the reworded commit is fine).

@fogydes

fogydes commented Nov 25, 2026

Copy link
Copy Markdown
Collaborator Author

Pushed 3db9e91 on top: follow-ups from an in-house review.

  • Card-resident buffers: a copy within a buffer whose host copy is on the card now stages in a card buffer (buffers::card_buffer), not system memory, so it no longer crosses PCIe twice. The staging buffer goes back to COMMON at the end.
  • When no staging buffer can be made, copy still returns false (the caller does the copy on the CPU at submission), but now logs a counted warning with the length. copy's doc comment says that same-buffer copies stage, where, and what the CPU fallback misses.
  • docs/analysis/gpu-measure.md now says all copies within one buffer stage, not only overlapping ones.
  • a_shader_write_to_a_copy_on_the_card_goes_back also checks that its copy was staged on the card.

The debug-layer suite and the full WARP pr-check pass. Left for later: reusing one staging buffer per list, and splitting very large copies into chunks.

@github-actions github-actions Bot added the area/docs docs/, Markdown label Nov 25, 2026
@simnJS

simnJS commented Nov 25, 2026

Copy link
Copy Markdown
Collaborator

Thanks for the follow-ups: the card-side staging and the throttled warning both look right. Before I merge:

  • please make the title imperative and name both areas, e.g. gpu, docs: copy within one buffer through a staging buffer;
  • please drop the Co-Authored-By trailer from both commits (a force-push is fine), and say in the AI line that the follow-up commit was drafted with Claude.

The two gpu-debug-layer failures (message 1419) also fail on main and are unrelated to this PR; we're fixing them there.

@fogydes
fogydes force-pushed the gpu/copies-within-one-buffer branch from 3db9e91 to b5b8d70 Compare November 25, 2026 15:23
@fogydes fogydes changed the title gpu: copies within one buffer go through staging (message 538) gpu, docs: copy within one buffer through a staging buffer Nov 25, 2026
@fogydes

fogydes commented Nov 25, 2026

Copy link
Copy Markdown
Collaborator Author

Pushed b5b8d70 on 4a0cdb3, with the imperative title and no Co-Authored-By trailers. The description records both Claude's original/follow-up work and Codex's integration and verification.

The functional patch is unchanged from the prepared branch: same-buffer copies stage separately, card-resident copies use card staging, the staging state returns to COMMON, and allocation failure emits the counted warning. Both 538 allowances are gone.

Fresh verification from this worktree passed on the first run:

  • Strict WARP diagnostics: 589 GPU unit tests plus 3 UMA integration tests; no message 538.
  • Full WARP PR-check: all six steps passed, 1,544 tests passed overall with 68 ignored. Binary timestamps confirmed all 12 workspace unit-test binaries were rebuilt after this worktree's source touch.

The previous silent CPU fallback on staging allocation failure remains a documented limitation. Staging reuse/chunking and a same-buffer-copy frequency measurement are follow-ups. Current main merges cleanly with this head.

@fogydes
fogydes requested a review from simnJS November 25, 2026 15:23

@simnJS simnJS left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks: the title, the trailers and the AI line are fixed, and the card staging (COMMON→DEST→SOURCE→COMMON) looks right.

One blocker: both clippy jobs fail because known_layer_errors is now never used (crates/xtp-gpu/src/testgpu.rs:103). Main removed its other caller in a1367f5, so dropping the two 538 allowances in this PR leaves it unused once merged. Please delete the function, which is cleaner than #[allow(dead_code)], then rebase on main and I'll merge once CI is green.

Route every same-buffer copy through a staging resource so D3D12 never reads and writes one resource in incompatible states. Keep the fallback warning when staging allocation fails.
@fogydes
fogydes force-pushed the gpu/copies-within-one-buffer branch from b5b8d70 to fa35267 Compare November 25, 2026 16:22
@github-actions github-actions Bot added policy changes the rules, licence or automation: a maintainer reviews size/M 100 to 399 changed lines and removed size/S 10 to 99 changed lines labels Nov 25, 2026
@fogydes

fogydes commented Nov 25, 2026

Copy link
Copy Markdown
Collaborator Author

Addressed the Clippy blocker on fa35267b745001460dab277216f46c0f151bbcf0, rebased onto main fafebe98ea5d52b0c03364fc16d6c5e0abc188f5.

  • Deleted known_layer_errors, its thread-local allowance state/filtering, and allowance-only reporting flag. No dead-code suppression added.
  • Removed the stale CONTRIBUTING.md reference. Deliberate-invalid-input scopes and strict error reporting remain intact.
  • Same-buffer staging behavior and its regression tests are unchanged by this cleanup.

Fresh local checks on that exact head:

  • Strict WARP diagnostics: 632 unit tests and 3 integration tests passed, with no tracked-error allowances left.
  • Default and all-feature Clippy with warnings denied: passed.
  • Full WARP cargo xtask pr-check: all six stages passed with default test threading.
  • Actual committed headers, title, and updated body passed conventions; no Co-Authored-By trailers.

The first full-check attempt inherited RUST_TEST_THREADS=1 from the strict-layer command. Three loader fastfail tests then rejected libtest's same-line stdout prefix. After diagnosing that environment dependence, all 12 fastfail tests and the complete standard-threading PR check passed. That initial failure is retained in the local evidence; no tests were skipped or source changed to hide it.

The PR description includes the cleanup and keeps the existing allocation-failure fallback limitation explicit. No new title or hardware run. Please re-review the current head once CI finishes.

@fogydes
fogydes requested a review from simnJS November 25, 2026 16:22

@simnJS simnJS left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks: known_layer_errors and its state are gone, the branch is rebased, and both clippy jobs and the debug-layer job are green. The card staging and the counted warning are unchanged. Approving; I'll merge.

@simnJS
simnJS merged commit 86f9da9 into main Nov 25, 2026
12 of 13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/docs docs/, Markdown area/gpu touches crates/xtp-gpu policy changes the rules, licence or automation: a maintainer reviews size/M 100 to 399 changed lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants