renderer: finalize bindings after every range-changing operation #532

Closed
github-bot wants to merge 1 commit from gh-pr/532/head into gh-pr/532/base
Owner

@⁠CheesyPoofs346 on GitHub · 2026-09-09 07:20 UTC · original
CheesyPoofs346:renderer-finalize-bindings → main · +162 −15 · 6 files · 1 commits · closed without merging

Problem. A binding captured during draw preparation can be left pointing at a buffer that a later operation in the same draw retired. NativeStorageBuffer captures a VkBuffer handle, an offset and a packed byte adjustment; any later ObtainBuffer can reach CreateBuffer, which replaces the buffers covering a range with one wider buffer and retires the originals. Retirement is deferred past GPU completion, so the captured handle stays usable but stops being current: a later upload lands in the replacement, and a write through the stale alias lands in storage nothing else reads. RebindBuffers(pixel) can retire what RebindBuffers(vertex) captured, and RebindImages reaches CreateBuffer through ResolveTexture -> FindImage -> InitializeImage -> ObtainBufferForImage, so it can retire buffers both stages captured. The shader_data upload carries the packed memory offsets and ran before any of the image work, so the offsets and the handles could disagree.

Change. Split obtaining from publishing. Obtaining keeps its position and all of its side effects — synchronize, upload, dirty-state change, LRU touch — unchanged and unrepeated, and records how each binding was served. PublishBuffers then re-resolves every cache-backed binding from its guest range (a BufferId is exactly what retirement invalidates) through a new non-allocating BufferCache::FindPublishedOwner, and rewrites the descriptor, the packed offset and the shader_data/flattened_srt uploads from one settled snapshot. FinalizeBindings runs three passes over the whole stage span — rebind buffers, rebind images, publish — so no stage is finalized against a state a later stage changes; both the graphics and compute paths call it, so rebinding without publishing is not expressible. Stream-backed bindings are republished as issued: ObtainBuffer's stream fast path returns a ring allocation holding a private copy that no guest range owns, so re-resolving one by address would discard it. A published flag is cleared by anything that can retire a buffer and asserted in CommitBindings.

Effect. No measured performance change; performance was not measured for this and no improvement is claimed. Publication adds a page-table lookup per cache-backed binding and moves two uploads later in the same draw. This closes a silent-corruption path: bindings that read bytes that were never uploaded to the buffer they point at, and writes that land in a retired buffer.

Verification. Builds clean on this base. The equivalent change in a downstream tree is covered by a device-backed regression that proves the defect by GPU readback — the captured handle reads the pre-update byte while the live owner reads the updated one, with a control check first so the stale result cannot be attributed to missing upload tracking — and additionally checks that a stream-backed binding keeps its allocation, offset and contents, that publication fails loudly rather than allocating when a range has no owner, and that rebinding clears publication. That tree also ran two full GTA V city drives with the change, exit code 0, no errors and no publication failures; the driver reported no visual problems.

Limitations. Marked draft for these:

  • The regression above has not been ported to this base. Bringing the device harness up here crashes inside ObtainBuffer during test setup, unresolved, so this PR carries no automated regression. The fix itself is a re-implementation against this tree's structure, not the downstream patch.
  • Not exercised anywhere: an image-triggered replacement specifically (retirement is driven by a spanning ObtainBuffer in the downstream test), writable aliases (is_written bindings), and CommitBindings itself — the published assertion is verified by reading, not by running.
  • Reviewers may prefer the headless device setup used by ShaderRecompilerComputeTests over the SDL-window approach the downstream harness uses, if a regression is added here.

References

No external specification. Correctness argument only: NativeStorageBuffer captures buffer->Handle() into prepared.buffers, and CreateBuffer retires the buffers it replaces while BindGraphicsResources continues to rebind further stages and images afterwards.

**@⁠CheesyPoofs346** on GitHub · 2026-09-09 07:20 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/532) `CheesyPoofs346:renderer-finalize-bindings` → `main` · +162 −15 · 6 files · 1 commits · closed without merging **Problem.** A binding captured during draw preparation can be left pointing at a buffer that a later operation in the same draw retired. `NativeStorageBuffer` captures a `VkBuffer` handle, an offset and a packed byte adjustment; any later `ObtainBuffer` can reach `CreateBuffer`, which replaces the buffers covering a range with one wider buffer and retires the originals. Retirement is deferred past GPU completion, so the captured handle stays usable but stops being current: a later upload lands in the replacement, and a write through the stale alias lands in storage nothing else reads. `RebindBuffers(pixel)` can retire what `RebindBuffers(vertex)` captured, and `RebindImages` reaches `CreateBuffer` through `ResolveTexture -> FindImage -> InitializeImage -> ObtainBufferForImage`, so it can retire buffers both stages captured. The `shader_data` upload carries the packed memory offsets and ran before any of the image work, so the offsets and the handles could disagree. **Change.** Split obtaining from publishing. Obtaining keeps its position and all of its side effects — synchronize, upload, dirty-state change, LRU touch — unchanged and unrepeated, and records how each binding was served. `PublishBuffers` then re-resolves every cache-backed binding from its **guest range** (a `BufferId` is exactly what retirement invalidates) through a new non-allocating `BufferCache::FindPublishedOwner`, and rewrites the descriptor, the packed offset and the `shader_data`/`flattened_srt` uploads from one settled snapshot. `FinalizeBindings` runs three passes over the whole stage span — rebind buffers, rebind images, publish — so no stage is finalized against a state a later stage changes; both the graphics and compute paths call it, so rebinding without publishing is not expressible. Stream-backed bindings are republished as issued: `ObtainBuffer`'s stream fast path returns a ring allocation holding a private copy that no guest range owns, so re-resolving one by address would discard it. A `published` flag is cleared by anything that can retire a buffer and asserted in `CommitBindings`. **Effect.** No measured performance change; performance was not measured for this and no improvement is claimed. Publication adds a page-table lookup per cache-backed binding and moves two uploads later in the same draw. This closes a silent-corruption path: bindings that read bytes that were never uploaded to the buffer they point at, and writes that land in a retired buffer. **Verification.** Builds clean on this base. The equivalent change in a downstream tree is covered by a device-backed regression that proves the defect by GPU readback — the captured handle reads the pre-update byte while the live owner reads the updated one, with a control check first so the stale result cannot be attributed to missing upload tracking — and additionally checks that a stream-backed binding keeps its allocation, offset and contents, that publication fails loudly rather than allocating when a range has no owner, and that rebinding clears publication. That tree also ran two full GTA V city drives with the change, exit code 0, no errors and no publication failures; the driver reported no visual problems. **Limitations.** Marked draft for these: - The regression above has **not** been ported to this base. Bringing the device harness up here crashes inside `ObtainBuffer` during test setup, unresolved, so this PR carries no automated regression. The fix itself is a re-implementation against this tree's structure, not the downstream patch. - Not exercised anywhere: an image-triggered replacement specifically (retirement is driven by a spanning `ObtainBuffer` in the downstream test), writable aliases (`is_written` bindings), and `CommitBindings` itself — the `published` assertion is verified by reading, not by running. - Reviewers may prefer the headless device setup used by `ShaderRecompilerComputeTests` over the SDL-window approach the downstream harness uses, if a regression is added here. **References** No external specification. Correctness argument only: `NativeStorageBuffer` captures `buffer->Handle()` into `prepared.buffers`, and `CreateBuffer` retires the buffers it replaces while `BindGraphicsResources` continues to rebind further stages and images afterwards.
A binding captured during draw preparation can be left pointing at a buffer
that a later operation in the same draw retired.

NativeStorageBuffer captures a VkBuffer handle, an offset and a packed byte
adjustment. Any later ObtainBuffer can reach CreateBuffer, which replaces the
buffers covering a range with one wider buffer and retires the originals.
Retirement is deferred past GPU completion, so the captured handle stays
usable, but it stops being current: a later upload lands in the replacement,
and a write through the stale alias lands in storage nothing else reads.

RebindBuffers(pixel) can retire what RebindBuffers(vertex) captured, and
RebindImages reaches CreateBuffer through ResolveTexture -> FindImage ->
InitializeImage -> ObtainBufferForImage, so it can retire buffers both stages
captured. The shader_data upload carries the packed memory offsets and ran
before any of the image work.

Split obtaining from publishing. Obtaining keeps its position and its side
effects; publishing re-resolves each cache-backed binding from its guest range
and rewrites the descriptor, the packed offset and the uploads from one settled
snapshot. FinalizeBindings runs three passes over the whole stage span - rebind
buffers, rebind images, publish - so no stage is finalized against a state a
later stage changes. Both the graphics and compute paths call it.

Stream-backed bindings are republished as issued: ObtainBuffer's stream fast
path returns a ring allocation holding a private copy that no guest range owns,
so re-resolving one by address would discard it.

Co-Authored-By: Claude Opus 5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:24:35 +00:00
github-bot closed this pull request 2026-11-26 09:24:35 +00:00
Author
Owner

@⁠CheesyPoofs346 on GitHub · 2026-09-10 04:11 UTC · original

Do not merge — this introduces visible geometry corruption.

A/B on the same scene and save, two builds differing only by this change: with it, large parts of interior geometry explode into flat dark shards (ceilings, walls, whole surfaces) while characters and most props render correctly; without it the same scene is clean. The corruption is selective, which points at one of the non-uniform paths in publication — the per-draw dedup hit path or formatted/texel buffers — rather than the offset arithmetic being wrong for every binding.

That also corrects the Effect section above: I wrote that performance was not measured, which was true, but I did not state that behaviour was unverified in a real scene. It is now verified to be wrong.

Investigating; will update or close.

**@⁠CheesyPoofs346** on GitHub · 2026-09-10 04:11 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/532#issuecomment-5612875005) Do not merge — this introduces visible geometry corruption. A/B on the same scene and save, two builds differing only by this change: with it, large parts of interior geometry explode into flat dark shards (ceilings, walls, whole surfaces) while characters and most props render correctly; without it the same scene is clean. The corruption is selective, which points at one of the non-uniform paths in publication — the per-draw dedup hit path or formatted/texel buffers — rather than the offset arithmetic being wrong for every binding. That also corrects the Effect section above: I wrote that performance was not measured, which was true, but I did not state that behaviour was unverified in a real scene. It is now verified to be wrong. Investigating; will update or close.

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No description provided.