GPU: typed buffer views off their host copy's elements read a copy of their own #230

Open
murillojndem wants to merge 2 commits from upstream-pr/230 into main
murillojndem commented 2026-11-29 20:46:37 +00:00 (Migrated from github.com)

Imported from DexNom/VIctory. Originally posted by murillojndem on 2026-11-29T20:46:37Z.


A typed buffer view of a title buffer that lies inside a larger host copy (crate::shadow) cannot always be expressed there: Direct3D begins a typed view at FirstElement times the element size of the buffer it views. When the title buffer begins at an offset in the copy that is not a multiple of the element size, rebase_srv returns SrvError::Alignment and buffer_srv leaves the slot null, so shaders read zeros through the view.

In runs of the user's on an RX 7900 XT (logs of 2026-11-29), the real cases were R32G32B32_FLOAT views (format 6) of 40 to 50 elements in a copy of 182,452,224 bytes, 8 bytes past an element (for example offset=22205600, 44 elements: 12-byte SRV could not address the first whole element in its host buffer). The doubling log counted 16 distinct ones in four minutes with ray tracing on, and 2 to 8 in runs with it off. The other buffer views those logs left null had addresses from 0x7 to 0xb or no elements.

Change:

  • own_copy_srv (descriptor/host.rs): such a typed view (not raw, not structured, its bytes beginning on a dword of the copy, at most 64 KiB) is made at element 0 of a buffer of its own in the card's memory, which holds the view's bytes. One own copy serves every slot that shows the same bytes of the same copy: the title slot and the dynamic slots of each table made afresh with it. OwnCopies keys them by (source copy, first byte, bytes) and counts the slots that show each. A slot that shows another resource gives it up (HostHeap::keep_sparse, and the texture-pressure branch of HostHeap::release, which takes slots' resources itself), and so does each slot of a heap that is dropped. The last one removes it. All own copies together take at most 16 MiB of the card's memory, counted in the 64 KiB each buffer takes (so at most 256); past that, views are left null as before (logged once). The buffer is made outside the registry's lock.
  • Each submission's first list's prelude begins with a copy into each own copy from its source (record_feeds in host.rs), after the CPU refreshes of the submission and the card's prelude (card_before, an earlier ExecuteCommandLists). The prelude is the first command list of its ExecuteCommandLists, so both buffers start in the common state; the copies promote them, and explicit transitions put them back in it (the source once, and not at all for an upload-heap source) for the lists that follow, which read the own copies as views.
  • An own copy does not hold its source (a Weak): one whose source the host let go of is no longer filled, and buffer_owner_current no longer treats its slots as current, so they are made again from the new owner, as a slot on another copy is today. A slot that shows an own copy of the current owner is current, so it is not made again at every pass.
  • VICTORY_OWN_COPIES=0 leaves such views null, as before.

What a shader reads through such a view: the copy's bytes as the submission finds them, after its refreshes, which is what a view made on the copy reads of bytes the title wrote on the CPU. What earlier lists of the same submission write into those bytes it reads from the next submission on; on the console the view reads them at once. So bytes the card rewrites each frame are a frame late, and if the title rewrites a range with other data each frame, the view shows the previous data where main showed zeros. The views in the logs above kept their addresses (16 in four minutes), which points to the first case, but I could not tell which draws read them. That case is not rare upstream: Marvisak/VIctory#1405 counted 1,938 ranges written by shaders within the submission that read them in a 10-minute run on an RTX 4070, about ten a frame.

Upstream (Marvisak/VIctory) tried this three ways, none merged:

  • #986 (closed) copied an off-element view's bytes into the list's scratch memory at each submission;
  • #1019 (closed) gave such a view a host copy of just its bytes, refreshed from title memory, and covered unordered views too, with the same limit within a submission as here;
  • #1405 (open, stacked on #1019 and on #334's buffer states, also closed) copies on the GPU right before the read for ranges shaders write.

None of them is in this repository. This change takes the direction of #1019 for typed shader views, filling the own copy on the card from the buffer's copy instead of from title memory, so it also shows what earlier submissions' shaders wrote before their copy-back lands. Reading within a submission would need buffer states tracked across the submission's lists, which this repository does not have either.

Tests:

  • descriptor::host::tests::a_shader_reads_the_title_bytes_of_a_typed_view_off_the_elements_of_its_copy: a compute shader reads two R32G32B32_FLOAT views through heap slots whose title buffers begin 260 and 812 bytes into one copy (8 past an element each), 40 and 16 elements, and writes them out; it gets the title's floats. Two own copies are filled from one source.
  • a_typed_view_off_the_elements_of_its_copy_reads_an_own_copy_until_an_owner_places_it_on_one replaces null_typed_srv_rebinds_when_a_new_owner_places_it_on_an_element: the slot shows an own copy filled from byte 256 of the outer copy and is current; a copy of the title buffer itself takes the slot (the own copy goes); the own copy comes back when that copy goes, and goes with the heap.
  • slots_that_show_one_view_share_its_own_copy_while_any_shows_it: a title slot and two dynamic slots share one own copy (3 slots); a slot made again with the same view leaves 3; a release under texture pressure gives two back; a null view in the last one removes it.
  • an_own_copy_holds_the_elements_of_the_view_inside_the_host_copy: the range, including the view from the log above, the cut at the copy's end, the dword rule and the 64 KiB limit.
  • Mutants: without the copies in the prelude the shader reads 0.0 everywhere; with the view left null as on main, three tests fail; with the texture-pressure release not giving slots back, the sharing test fails (3 slots instead of 1). Without the transitions back to the common state no test fails: neither WARP nor the debug layer reports a buffer left in a copy state across the lists of one submission. The transitions are there because a buffer in a copy state is not in the state the next lists use it in.
  • victory-gpu --lib: 222 passed, 0 failed, 4 ignored on WARP, with and without VICTORY_RELAY=1, and on the RX 7900 XT (main: 219, 0, 4); 242, 0, 4 in a diagnostics build with VICTORY_D3D12_DEBUG=1 on WARP. With VICTORY_RELAY=1, resource::tests::buffer_data_reaches_the_host_texture sometimes fails on this branch and on main alike (on main, 2 of 3 runs of it alone).
  • Clippy with -D warnings, with and without --all-features, and cargo xtask policy pass. The touched files are not rustfmt-formatted on main (1,156 rustfmt differences in victory-gpu there, 1,180 here); the new code follows the style around it.

Limits:

  • Typed shader views only. Unordered access views, raw views and structured views that cannot begin where their buffers lie are left null as before, and so are typed views of more than 64 KiB.
  • A view left null for want of room stays null until the title writes its slot again.
  • Each submission while an own copy exists copies all of them, whether its lists read them or not (16 views of under 600 bytes in the run above; at most 256 buffers).
  • HEAP_VIEWS_ELSEWHERE (diagnostics) counts the slots that show an own copy among those showing another buffer than their range finds. Leaving them out would touch a line #95 reworks.
  • The effect on the picture is not measured.

Generated with Claude Code

<!-- greyfinch-sync:DexNom/VIctory:issue:5554945152 --> Imported from [DexNom/VIctory](https://github.com/DexNom/VIctory/pull/230). Originally posted by [murillojndem](https://github.com/murillojndem) on 2026-11-29T20:46:37Z. --- A typed buffer view of a title buffer that lies inside a larger host copy (`crate::shadow`) cannot always be expressed there: Direct3D begins a typed view at `FirstElement` times the element size of the buffer it views. When the title buffer begins at an offset in the copy that is not a multiple of the element size, `rebase_srv` returns `SrvError::Alignment` and `buffer_srv` leaves the slot null, so shaders read zeros through the view. In runs of the user's on an RX 7900 XT (logs of 2026-11-29), the real cases were R32G32B32_FLOAT views (format 6) of 40 to 50 elements in a copy of 182,452,224 bytes, 8 bytes past an element (for example `offset=22205600`, 44 elements: `12-byte SRV could not address the first whole element in its host buffer`). The doubling log counted 16 distinct ones in four minutes with ray tracing on, and 2 to 8 in runs with it off. The other buffer views those logs left null had addresses from 0x7 to 0xb or no elements. Change: - `own_copy_srv` (`descriptor/host.rs`): such a typed view (not raw, not structured, its bytes beginning on a dword of the copy, at most 64 KiB) is made at element 0 of a buffer of its own in the card's memory, which holds the view's bytes. One own copy serves every slot that shows the same bytes of the same copy: the title slot and the dynamic slots of each table made afresh with it. `OwnCopies` keys them by (source copy, first byte, bytes) and counts the slots that show each. A slot that shows another resource gives it up (`HostHeap::keep_sparse`, and the texture-pressure branch of `HostHeap::release`, which takes slots' resources itself), and so does each slot of a heap that is dropped. The last one removes it. All own copies together take at most 16 MiB of the card's memory, counted in the 64 KiB each buffer takes (so at most 256); past that, views are left null as before (logged once). The buffer is made outside the registry's lock. - Each submission's first list's prelude begins with a copy into each own copy from its source (`record_feeds` in `host.rs`), after the CPU refreshes of the submission and the card's prelude (`card_before`, an earlier `ExecuteCommandLists`). The prelude is the first command list of its `ExecuteCommandLists`, so both buffers start in the common state; the copies promote them, and explicit transitions put them back in it (the source once, and not at all for an upload-heap source) for the lists that follow, which read the own copies as views. - An own copy does not hold its source (a `Weak`): one whose source the host let go of is no longer filled, and `buffer_owner_current` no longer treats its slots as current, so they are made again from the new owner, as a slot on another copy is today. A slot that shows an own copy of the current owner is current, so it is not made again at every pass. - `VICTORY_OWN_COPIES=0` leaves such views null, as before. What a shader reads through such a view: the copy's bytes as the submission finds them, after its refreshes, which is what a view made on the copy reads of bytes the title wrote on the CPU. What earlier lists of the same submission write into those bytes it reads from the next submission on; on the console the view reads them at once. So bytes the card rewrites each frame are a frame late, and if the title rewrites a range with other data each frame, the view shows the previous data where main showed zeros. The views in the logs above kept their addresses (16 in four minutes), which points to the first case, but I could not tell which draws read them. That case is not rare upstream: Marvisak/VIctory#1405 counted 1,938 ranges written by shaders within the submission that read them in a 10-minute run on an RTX 4070, about ten a frame. Upstream (Marvisak/VIctory) tried this three ways, none merged: - #986 (closed) copied an off-element view's bytes into the list's scratch memory at each submission; - #1019 (closed) gave such a view a host copy of just its bytes, refreshed from title memory, and covered unordered views too, with the same limit within a submission as here; - #1405 (open, stacked on #1019 and on #334's buffer states, also closed) copies on the GPU right before the read for ranges shaders write. None of them is in this repository. This change takes the direction of #1019 for typed shader views, filling the own copy on the card from the buffer's copy instead of from title memory, so it also shows what earlier submissions' shaders wrote before their copy-back lands. Reading within a submission would need buffer states tracked across the submission's lists, which this repository does not have either. Tests: - `descriptor::host::tests::a_shader_reads_the_title_bytes_of_a_typed_view_off_the_elements_of_its_copy`: a compute shader reads two R32G32B32_FLOAT views through heap slots whose title buffers begin 260 and 812 bytes into one copy (8 past an element each), 40 and 16 elements, and writes them out; it gets the title's floats. Two own copies are filled from one source. - `a_typed_view_off_the_elements_of_its_copy_reads_an_own_copy_until_an_owner_places_it_on_one` replaces `null_typed_srv_rebinds_when_a_new_owner_places_it_on_an_element`: the slot shows an own copy filled from byte 256 of the outer copy and is current; a copy of the title buffer itself takes the slot (the own copy goes); the own copy comes back when that copy goes, and goes with the heap. - `slots_that_show_one_view_share_its_own_copy_while_any_shows_it`: a title slot and two dynamic slots share one own copy (3 slots); a slot made again with the same view leaves 3; a release under texture pressure gives two back; a null view in the last one removes it. - `an_own_copy_holds_the_elements_of_the_view_inside_the_host_copy`: the range, including the view from the log above, the cut at the copy's end, the dword rule and the 64 KiB limit. - Mutants: without the copies in the prelude the shader reads 0.0 everywhere; with the view left null as on main, three tests fail; with the texture-pressure release not giving slots back, the sharing test fails (3 slots instead of 1). Without the transitions back to the common state no test fails: neither WARP nor the debug layer reports a buffer left in a copy state across the lists of one submission. The transitions are there because a buffer in a copy state is not in the state the next lists use it in. - `victory-gpu --lib`: 222 passed, 0 failed, 4 ignored on WARP, with and without `VICTORY_RELAY=1`, and on the RX 7900 XT (main: 219, 0, 4); 242, 0, 4 in a diagnostics build with `VICTORY_D3D12_DEBUG=1` on WARP. With `VICTORY_RELAY=1`, `resource::tests::buffer_data_reaches_the_host_texture` sometimes fails on this branch and on main alike (on main, 2 of 3 runs of it alone). - Clippy with `-D warnings`, with and without `--all-features`, and `cargo xtask policy` pass. The touched files are not rustfmt-formatted on main (1,156 rustfmt differences in victory-gpu there, 1,180 here); the new code follows the style around it. Limits: - Typed shader views only. Unordered access views, raw views and structured views that cannot begin where their buffers lie are left null as before, and so are typed views of more than 64 KiB. - A view left null for want of room stays null until the title writes its slot again. - Each submission while an own copy exists copies all of them, whether its lists read them or not (16 views of under 600 bytes in the run above; at most 256 buffers). - `HEAP_VIEWS_ELSEWHERE` (diagnostics) counts the slots that show an own copy among those showing another buffer than their range finds. Leaving them out would touch a line #95 reworks. - The effect on the picture is not measured. Generated with [Claude Code](https://claude.com/claude-code)
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/230:upstream-pr/230'
git switch 'upstream-pr/230'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/230'
git switch 'upstream-pr/230'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/230'
git switch 'upstream-pr/230'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/230'
git switch 'main'
git merge --squash 'upstream-pr/230'
git switch 'main'
git merge --ff-only 'upstream-pr/230'
git switch 'main'
git merge 'upstream-pr/230'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
DexNom/VIctory!230
No description provided.