runtime: the index buffer view size bounds the index fetch in the Vulkan replay (reads past the view are 0) #326

Open
github-bot wants to merge 6 commits from github-bot/gh-326 into main AGit
Owner

@⁠Andretuta on GitHub · 2026-11-24 03:10 UTC · original
ubiquitarianism:old-pr/10061 → main · +288 −11 · 5 files · 6 commits · draft

Carried over from old PR 10061 by tedin7 in the original Leonida repository, which went private on 23 Nov 2026. The code is the branch old-pr/10061, exactly that PR's last commit. All carried-over PRs are listed in #17.
Opened as a draft, and the CI runs the opening started were cancelled so 250 PRs don't fill the queue. Re-run CI here when you review it.
tedin7: this is your work. Feel free to take it over, by pushing to the branch or opening your own PR and closing this one.


Original description (mentions and #N references are disabled; the numbers refer to the old repository):

What

An indexed draw now honours the index buffer view's SizeInBytes in the Vulkan replay (prepare_draw, runtime/src/vulkan_replay.h):

  • draw wholly inside the view: the buffer is bound as before;
  • draw wholly outside the view (or no index buffer): binds the zero page and draws with firstIndex 0, so every index reads 0;
  • draw that crosses the view's end: indices inside the view are copied to a transfer-scratch buffer, the rest is zero, so no byte past the view is read (vkCmdBindIndexBuffer has no size without VK_KHR_maintenance5, so it binds to the end of the whole buffer);
  • null view over a draw bigger than the zero page: a zeroed scratch (previously it could read the arena bytes after the page);
  • scratch buffers get the index-buffer usage flag for the staged copy.

Why

D3D12 defines an index fetch outside the view as 0. The replay bound the whole buffer, so a title that leaves stale or larger data behind its view (common with suballocated index buffers) drew geometry it should not, or read other resources' bytes: wrong 3D rendering.

Rebased cleanly on current main; supersedes #7808 closed during org migration (same 5 commits, cherry-picked without conflicts) plus a new CPU-only test.

Tested

No GPU pod was available (all down), so:

  • NEW replay_index_view_test (native g++, no GPU, drives prepare_draw with fake Vulkan callbacks): 5 cases pass (inside / outside / straddle / null view / null view bigger than the zero page).
  • Negative control: the same cases against main's prepare_draw: 3 of 5 FAIL ("outside: the zero page is bound", "straddle: a staged copy is bound", "null view, large draw: a zeroed scratch covers the whole draw").
  • Neighbours, native g++, all pass: replay_copy_offset_test, replay_clear_gate_test, replay_texture_barrier_test, replay_write_immediate_predication_test.
  • d3d12_draw_test.cpp (the original GPU test with the pixel checks for view_out / view_in / straddle / null / zero / live) compiles clean with clang-cl 19 (-fsyntax-only), but I could NOT run it: it needs a Vulkan device (and main's d3d12_vulkan_device.cpp does not build until #9809/#9815 land). Its GPU result is unverified in this PR.
**@⁠Andretuta** on GitHub · 2026-11-24 03:10 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/326) `ubiquitarianism:old-pr/10061` → `main` · +288 −11 · 5 files · 6 commits · draft > Carried over from **old PR 10061** by `tedin7` in the original Leonida repository, which went private on 23 Nov 2026. The code is the branch `old-pr/10061`, exactly that PR's last commit. All carried-over PRs are listed in #17. > Opened as a draft, and the CI runs the opening started were cancelled so 250 PRs don't fill the queue. Re-run CI here when you review it. > `tedin7`: this is your work. Feel free to take it over, by pushing to the branch or opening your own PR and closing this one. --- Original description (mentions and `#N` references are disabled; the numbers refer to the old repository): ## What An indexed draw now honours the index buffer view's `SizeInBytes` in the Vulkan replay (`prepare_draw`, `runtime/src/vulkan_replay.h`): - draw wholly inside the view: the buffer is bound as before; - draw wholly outside the view (or no index buffer): binds the zero page and draws with firstIndex 0, so every index reads 0; - draw that crosses the view's end: indices inside the view are copied to a transfer-scratch buffer, the rest is zero, so no byte past the view is read (`vkCmdBindIndexBuffer` has no size without VK_KHR_maintenance5, so it binds to the end of the whole buffer); - null view over a draw bigger than the zero page: a zeroed scratch (previously it could read the arena bytes after the page); - scratch buffers get the index-buffer usage flag for the staged copy. ## Why D3D12 defines an index fetch outside the view as 0. The replay bound the whole buffer, so a title that leaves stale or larger data behind its view (common with suballocated index buffers) drew geometry it should not, or read other resources' bytes: wrong 3D rendering. Rebased cleanly on current main; supersedes `#7808` closed during org migration (same 5 commits, cherry-picked without conflicts) plus a new CPU-only test. ## Tested No GPU pod was available (all down), so: - NEW `replay_index_view_test` (native g++, no GPU, drives `prepare_draw` with fake Vulkan callbacks): 5 cases pass (inside / outside / straddle / null view / null view bigger than the zero page). - Negative control: the same cases against main's `prepare_draw`: 3 of 5 FAIL ("outside: the zero page is bound", "straddle: a staged copy is bound", "null view, large draw: a zeroed scratch covers the whole draw"). - Neighbours, native g++, all pass: replay_copy_offset_test, replay_clear_gate_test, replay_texture_barrier_test, replay_write_immediate_predication_test. - `d3d12_draw_test.cpp` (the original GPU test with the pixel checks for view_out / view_in / straddle / null / zero / live) compiles clean with clang-cl 19 (-fsyntax-only), but I could NOT run it: it needs a Vulkan device (and main's d3d12_vulkan_device.cpp does not build until `#9809`/#9815 land). Its GPU result is unverified in this PR.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 08:53:33 +00:00
Author
Owner

@⁠Sigtryggur-Mani-Gudmundsson on GitHub · 2026-11-24 03:25 UTC · original

I ran tedin7’s six commits through native Vulkan replay on Linux with an RTX 4070 Ti and Khronos synchronization validation enabled. On the original code, the two in-view controls passed and eight other indexed draws lit the wrong pixels. With the patch, all ten matched the expected image, with zero validation errors. Applying the series independently to de4c373 reproduced the result.

The fixture covers 16- and 32-bit indices, a 16-bit view ending halfway through an index, a view clipped at the resource end, and empty, null, or wholly outside views. It records a CommandList and exercises the fill, copy, index binding and indexed draw on the GPU. The Windows D3D12 entry point remains untested here.

Test source, pixel masks and reproduction commands

**@⁠Sigtryggur-Mani-Gudmundsson** on GitHub · 2026-11-24 03:25 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/326#issuecomment-7121079656) I ran tedin7’s six commits through native Vulkan replay on Linux with an RTX 4070 Ti and Khronos synchronization validation enabled. On the original code, the two in-view controls passed and eight other indexed draws lit the wrong pixels. With the patch, all ten matched the expected image, with zero validation errors. Applying the series independently to `de4c373` reproduced the result. The fixture covers 16- and 32-bit indices, a 16-bit view ending halfway through an index, a view clipped at the resource end, and empty, null, or wholly outside views. It records a `CommandList` and exercises the fill, copy, index binding and indexed draw on the GPU. The Windows D3D12 entry point remains untested here. [Test source, pixel masks and reproduction commands](https://github.com/Sigtryggur-Mani-Gudmundsson/graphics-runtime-patches/tree/1699e1111c547dcd456f4a92bd24e47e27db872e/tests/index-view)
This pull request has changes conflicting with the target branch.
  • runtime/src/vulkan_replay.h
  • runtime/src/vulkan_scratch.h
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/326/head:github-bot/gh-326'
git switch 'github-bot/gh-326'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-326'
git switch 'github-bot/gh-326'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-326'
git switch 'github-bot/gh-326'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-326'
git switch 'main'
git merge --squash 'github-bot/gh-326'
git switch 'main'
git merge --ff-only 'github-bot/gh-326'
git switch 'main'
git merge 'github-bot/gh-326'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.