Skip to content

GPU: a view's own copy that shaders feed reads its buffer's copy on the GPU (stacked on #334 and #1019) - #1405

Open
SantannaCarlos wants to merge 9 commits into
Marvisak:mainfrom
SantannaCarlos:fix/own-copy-same-submission
Open

SantannaCarlos wants to merge 9 commits into
Marvisak:mainfrom
SantannaCarlos:fix/own-copy-same-submission

Conversation

@SantannaCarlos

@SantannaCarlos SantannaCarlos commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Stacked on #334 (buffer states) and #1019 (views off an element): only the last two commits are new here, e8902a3 and ad2025a (fixes from review). The merge brings #334 as it is (c99e429); the commits before it are #1019's.

What

#1019 gives a view that starts off an element of its buffer's host copy a copy of just its bytes. That copy is brought up to date from title memory when the submission is prepared, so it does not see what the submission's shaders wrote in that range in the buffer's copy before the read; a view on an element of the same bytes does. In the world this is common: in a 10-minute run on an RTX 4070, 1,938 such ranges had bytes written by shaders in the 174 MB buffer's copy within the submission that read them (median 4.5 KB, about ten a frame). This was the known limit noted in #1019.

  • The copy-back of a buffer's copy now marks the copies cut from it whose range holds bytes its shaders changed.
  • A shader view of a marked copy reads what a view on an element reads: the buffer's copy is brought up to date as for such a view, and its bytes are copied into the view's copy on the GPU right before the read, with the states GPU: transition host copies of title memory between shader writes and reads #334 tracks (read for the source, copy destination and then read for the view's copy).
  • The mark stays, since the title produces that data on the GPU every frame; copies never marked keep their own refresh, so the cost is only where it is needed.

Not covered:

  • an unordered view with its own copy still writes it apart, so within a submission other views of those bytes see its writes only after the copy-back;
  • the first submission whose shaders write a copy's range is still read the old way: the mark comes from that submission's copy-back, and lists recorded before it keep the old path for that submission;
  • views read only through directly indexed heaps, which GPU: transition host copies of title memory between shader writes and reads #334 does not track either.

Tested

What I could not show: a visible difference in the dumped targets. The captures fall on different moments of the scene between runs, so they do not compare.

realhenners and others added 8 commits November 24, 2026 12:34
… reads

Host copies of title buffers start in the common state and relied on
implicit promotion, which allows a buffer to be written (unordered access)
or read in a list, not both. The world's 174 MB heap is both: the argument
clamp and title passes write it, draws read vertices and indices from it.
indirect.rs also moved the argument buffer from unordered access to
indirect arguments and back after every execution, whatever state the
buffer was really in.

Each list now tracks the state of every host copy it uses (BufferStates):
reads (vertex, index, constant, shader resource, arguments) in GENERIC_READ,
shader writes in UNORDERED_ACCESS, with a transition where the use changes.
The list's prelude moves each buffer from common to its first state and the
list moves it back before closing, so state never leaks into the next list
of a submission. indirect::execute replaces the fixed UAV <-> INDIRECT_ARGUMENT
barriers.

Refs Marvisak#199, Marvisak#264.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Review of Marvisak#334 (r4tedx, QemmHD): buffer_state kept whatever state was asked
for last, so a dispatch writing a copy through a table UAV and reading the
same copy through an SRV or root view listed after it ran its writes in
GENERIC_READ, and nothing then separated those writes from a later vertex
or index read. One host copy backs most of the title's buffers, so this is
likely.

BufferStates now counts commands (State::bind and the argument clamp start
one) and remembers which command last wrote each copy: a read asked for in
that command leaves the copy in UNORDERED_ACCESS, in either binding order.
The next command's read transitions it as before.

The debug-layer test's child process unsets VICTORY_D3D12_DEBUG: with it
set in a diagnostics build, Host::execute drained the message queue before
the test read it (Hiiva). With the closing return to COMMON disabled, the
test now fails with id 527 in that configuration too.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…copy

A view of memory the title placed itself (methods 0x230 and 0x238) names
its own start, which need not be a multiple of the view's stride, so its
slot was left null and shaders read zeros through it. Such a view now
reads a copy of just its bytes that starts at the view, made on first use
and keyed by (start, length); the slot holds it, the map holds a weak
reference.

The copy is a host copy like any other, so its own refresh brings it up to
date from title memory, copying only the pages the title wrote. That is
sound because a submission's copy-back runs before the next submission is
prepared (Host::execute waits for the GPU before copying back), so title
memory holds what the GPU wrote by then. Nothing copies from the buffer's
copy: that would copy the view's whole range again on every submission,
and in the world these views sum to hundreds of megabytes.

Known limit, commented where the copy is made: being prepared from title
memory, the copy does not see what a shader of the same submission wrote
in that range before the read. Correcting that needs every host buffer's
state tracked across the submission (PR Marvisak#334), so the bytes can be copied
on the GPU inside the list instead. Measured in the world, this version
keeps the collision-detection compute running to the time limit on both
cards, where a per-submission null rule of the same copy caused the
device to go 1 to 12 s after first use.

The step also unifies the view's range: buffer_srv chose its copy from the
resource and readable refreshed from the view, and the title's nested
174 MB heaps made the two differ. One view_range_of now feeds both.

The warning a buffer shader view left null names its kind (placed or
registered), its stride and its range, which is what the logs need to
confirm that only placed views misalign. A diagnostics build leaves the
views of the strides in VICTORY_NULL_VIEW_STRIDES null, to confirm in the
game which ones a pass needs.
… own copy

An unordered access view of memory the title placed itself starts where
the title put it, which need not be a multiple of its stride from the
start of its buffer's host copy. Such a slot was left null, so what the
shader wrote through it was lost. It now writes the view's own copy, the
one a shader view off an element already reads (one copy per range, so a
shader view and an unordered view of the same bytes share it).

readable declares the view's range on that copy, so the copy-back puts
the bytes the GPU wrote there into title memory after the submission, and
every other copy of that memory picks them up at its next refresh. A view
with its own copy is now brought up to date in that copy only: the
buffer's copy was refreshed for it too, which nothing read. Which copy a
view uses is decided as the slot decided it (off_element), not by the
range alone: an on-element view of the same bytes with another stride
writes the buffer's copy, and its writes must go back from there.

buffer_range, the views' copies and the new check share one reading of
the descriptor (buffer_parts, extent), so the range a slot's copy is keyed
by and the one readable declares cannot differ.

Known limit, as for shader views: within one submission the copy and the
buffer's copy do not see each other's writes (PR Marvisak#334).
Two recording threads can allocate separate aligned buffers for the
same range. The last cache insert wins, leaving a descriptor that
points at a different buffer from the one refreshed at submission.

Reuse any live copy published while allocation was in progress.
Exercise concurrent publication and verify that each caller holds the
copy that receives the refreshed bytes.
A slot finds its buffer's copy from the resource it views (make), while
readable looked a copy up again from the view's range. With a title
buffer registered inside another, that finds the inner buffer's copy: the
copy the descriptor reads was not brought up to date, and what shaders
wrote was copied back from a copy they never wrote. aycow reported it on
Marvisak#1019, with a fix and fixtures on AMD, NVIDIA and WARP.

As in that fix, the slot now keeps the copy it views, its buffer's or a
view's own, and hands that one to readable (make_at, mirror, copy_at).
That also replaces the separate list of own copies and the off-element
recheck readable needed: the slot already knows which copy it is of.

Co-authored-by: asc <aydanscowan@gmail.com>
…he GPU

A copy of just a view's bytes is brought up to date from title memory when
the submission is prepared, so it missed what the submission's shaders
wrote in that range in the buffer's copy before the read. In the world
that is common: in a 10-minute run on an RTX 4070, 1,938 such ranges had
bytes written by shaders in the 174 MB buffer's copy within the
submission that read them (median 4.5 KB, about ten a frame).

The copy-back of a buffer's copy now marks the copies cut from it whose
range holds bytes its shaders changed. A shader view of a marked copy
reads what a view on an element of the same bytes reads: the buffer's
copy is brought up to date as for such a view, and its bytes are copied
into the view's copy on the GPU right before the read, with the states
Marvisak#334 tracks (read for the source, copy destination then read for the
view's copy). The mark stays, since the title produces such data on the
GPU every frame; copies never marked keep their own refresh.

Unordered views with their own copy still write it apart: within a
submission, other views of those bytes see their writes only after the
copy-back.
@aycow

aycow commented Nov 24, 2026

Copy link
Copy Markdown

Thanks for following up on #1019. I reviewed exact e8902a3 specifically against f173d36 (#185) and 709647f (#387). The composition has conflicts, and two source-level edge cases stood out. These are review findings pending native reproduction.

  1. The aligned cut cache keys by title address/length, while the cut now permanently owns its source. A minimal check is to request identical aligned spans from two distinct live source generations. Each cut should retain and follow only its own source. The +48/+52 test uses separate keys.
  2. An ordinary parent UAV table can bind before a fresh table containing a fed cut SRV and a CBV. The same-command write precedence can then suppress the source transition before the immediate internal copy. An ordered preparation phase before final dispatch states would also make that copy's native source/destination touches easier to track for GPU: run submissions asynchronously on a host fence timeline #595.

…oves both buffers

Two cases from aycow's review of this change:

- Copies of views' bytes were found again by start and length, while a
  copy now follows the buffer's copy it was cut from. The same bytes cut
  from another copy of that memory (a title buffer released and
  registered again) got the first copy, which follows one nothing writes
  any more. The map now keys them by that copy as well.
- The copy on the GPU asked Marvisak#334's buffer states for its states, where a
  command's own write wins: after an unordered view of the buffer's copy
  was bound for the same command, the source stayed in the unordered
  access state for the copy. HostList::copy_buffer now moves both buffers
  to the copy's states and back to the ones the command has them in
  (BufferStates::around_copy), whatever the command declared.
@SantannaCarlos

Copy link
Copy Markdown
Contributor Author

@aycow both hold; thank you. Fixed in ad2025a on this branch:

  1. A copy of a view's bytes is now found again by start, length and the buffer's copy it was cut from, so the same bytes cut from another copy of that memory (a title buffer released and registered again) get a copy of their own, which follows and is marked by that copy only. New test: the same span cut from two copies gives two copies; shader writes found by one copy's copy-back mark only the copy cut from it.
  2. The GPU copy no longer goes through buffer_state, where the command's own write wins. HostList::copy_buffer moves the source to the copy-source state and the view's copy to copy-destination, records the copy, and moves each back to the state the command has it in (BufferStates::around_copy); a first use records the copy's state for the prelude. New test: after the same command binds the source for writing, a copy from it still transitions it (write, copy source, write), and a read state is left as it is. Keeping the copy and its two transitions in one call should also make it one unit for GPU: run submissions asynchronously on a host fence timeline #595 to track.

cargo test --workspace (victory-gpu 63 passed, 1 ignored), clippy with and without --all-features, and the asset policy pass. On the conflicts with #185 and #387: I haven't resolved those here; this stays on #334 and #1019 as they are.

@aycow

aycow commented Nov 24, 2026 •

Copy link
Copy Markdown

Thanks for the fixes in ad2025a. The identical-range, distinct-source regression passed on AMD Radeon Graphics, RTX 5070 Laptop and WARP. An additional native fixture dispatches a UAV producer, copies the alias while the parent is already bound for the next dispatch, then reads the alias while writing the parent again. Both destination-state variants read 43 and wrote 44 on all three with full GBV and no warnings or errors. The previous sequence triggered WARP errors 527 and 538 because the copy source was still UAV.

These results cover the case where the copy path is selected. First-use discovery, already-recorded lists and directly indexed descriptors still need separate controls. #185/#387 and #595 composition have source conflicts. #595 will also need the exact parent and cut read/write touches and native backing ownership through completion.

The author-preserving #1405/#387 composition is now independently tested against current #387 0899b7d. Both inline state variants, all 20 ordering checks, and all five shadow cases passed on AMD, NVIDIA, and WARP with full GBV. Both workspace Clippy configurations and asset policy passed. The test overlay retains the named test-device setup. The first unmarked parent-to-cut read within one native list still needs earlier producer tracking. #185 direct heaps and #595 fence ownership remain separate integration work.

@catminou4 catminou4 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Static review only: victory-gpu is Windows-only (D3D12/windows_sys) — nothing compiles or runs on macOS; this is source review of a large diff plus the PR's own Windows tests.

A view's own copy that shaders feed now reads its buffer's copy: make_at/copy_at/mirror per slot, buffer_parts/extent/shader_extent/unordered_extent/view_range_of/view_copy/plan compute the readable extent from the SRV/UAV desc words (14 tests). This is the kind of copy-planning logic that is easy to get subtly wrong (offset/element size/structure stride) — the 14 tests are the evidence; please point to which ones cover structured buffers and byte-address buffers specifically. Windows-only.

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

Needs rebase. This pull request conflicts with main (conflicting: crates/victory-gpu/src/descriptor/host.rs, crates/victory-gpu/src/draw.rs, crates/victory-gpu/src/host.rs, crates/victory-gpu/src/indirect.rs, crates/victory-gpu/src/shadow.rs). It will be reviewed once it is rebased onto the current main.


Generated by Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants