WIP: fix(fsr4): the tail is written before its reader also when the unrolled shader arrives between bind and dispatch #1163

Draft
TrashPanda wants to merge 1 commit from TrashPanda/Longshot:fix/fsr4-tail-order into main
Contributor

Type: fix · Risk: low · Tested on: Linux, RTX 4070 Ti (game-free checks) · Needs: AMD RDNA 4 with FSR 4

Why

With AMD FSR 4's shader tuning, a tuned pipeline published between bind and dispatch could run once on weight tables not written yet, a frame of wrong upscaling on RDNA 4.

Restored from Hookshot 2952, by jarynclouatre, and split out of #287 so it can be reviewed and merged on its own.

What changed

cmd_dispatch in gpu/vk/vk_fsr4.cpp decides from what the bind put in place (struct Bind), not from the published pipeline at dispatch time.

Commits, each with its original author and message:

0ae73075 fix(fsr4): the tail is written before its reader also when the unrolled shader arrives between bind and dispatch (jarynclouatre)

With AMD FSR 4's shader tuning (https://github.com/NotRexed/Hookshot/pull/2798), a tuned pipeline reads its
weight tables from a tail at the end of the library's storage buffer,
and tune::cmd_dispatch writes that tail before the first dispatch that
reads it. The unrolled pipeline is built by a background job and
published into better from its own thread. cmd_bind_pipeline binds
the library's pipeline when no unrolled one exists yet, but
cmd_dispatch asked better again at dispatch time. When the job
published between the two, the dispatch took the unrolled pipeline as
bound: it skipped the tail's upload (due stayed set) and the fallback
to AMD's own pipeline, while the tail-reading pipeline ran once on a
tail not written yet.

Change

bound keeps, per command buffer, the library's pipeline and whether
the bind put the unrolled one in its place (struct Bind).
cmd_dispatch decides from that, not from better at dispatch time.
The twin path still gets the library's pipeline from it. No lock is held
across a driver call that was not held before.

Testing

  • tests/test_fsr4_bind_dispatch.py (new): cuts cmd_bind_pipeline,
    cmd_dispatch and the structs they use out of vk_fsr4.cpp and runs
    them against recording stand-ins for the driver's calls, with no GPU and
    no spirv-val. Three cases: the library's pipeline bound and the
    unrolled one published before the dispatch (the tail is written and
    due cleared); the unrolled one bound (no tail written); a tail that
    cannot be read with the unrolled one published in between (AMD's own
    pipeline is bound). The first and third fail on main and pass here.
  • Linux renderer built with HOOKSHOT_FSR=1: vk_fsr4.cpp compiles.
  • Full suite on Linux: 2 failures, each for a tool this machine lacks
    (test_bake_portability: zstandard;
    nx2/test_texture_init_barrier.py: clang++); both fail the same on
    main.
  • Not run on an RDNA 4 card.

Restored-from: https://github.com/NotRexed/Hookshot/pull/2952

Measurements

Verdict: nothing to measure: a correctness fix off the frame path, covered by game-free checks.

Flags and settings

None.

Testing

Tested: On Linux (kernel 7.0), RTX 4070 Ti, main @ 71b1edd8, this PR @ 0ae73075: the checks that read the changed files (tests/run_all.py --changed origin/main) pass. The full suite (tests/run_all.py) has not been run on this branch yet; a comment will follow when it has.

Not tested: An RDNA 4 card. Opened as work in progress until it runs in the game.

Must be tested:

  • AMD RDNA 4, FSR 4 (Test Farm: @Beanstalk an AMD RDNA 4 card with FSR 4): play 5 minutes with FSR 4 from a cold shader cache, expecting no wrong frame

Risks

What could break: FSR 4 on RDNA 4.

Turning it off: revert the commit.

Compatibility: Nothing changes.

Checklist

Everyone

  • One topic.
  • The title is a Conventional Commit (type(scope): description) of the status line's type.
  • Why gives the reason plainly and links the issues that fit (the tracker searched).
  • Tested on my own PC where it runs (in the game, for a change the game runs), as Testing's Tested says; what
    can't be run by hand is in Testing's Not testable by hand, with why. Not ticked yet: The in-game run never reached the changed FSR 4 path, which needs an RDNA 4 card.
  • Measurements has a verdict, or one line saying why there is nothing to measure.
  • Nothing gets worse that I know of: any cost (speed, latency, memory, a setup that worked before) is measured and
    in Risks.
  • Testing names what was tested, what wasn't, and what must still be tested, where and how.
  • Rebased on current main.
  • No game files, dumps, build output or generated files.
  • third_party/ untouched (a new folder with a PROVENANCE.md instead).
  • The license is OK: GPL-3.0-or-later, nothing incompatible and nothing decompiled.
  • python -I -B tests/run_all.py passes (any failure that also fails on main is named).

When it applies

  • A game-free test added for the fix, failing on main and passing here.
**Type:** fix · **Risk:** low · **Tested on:** Linux, RTX 4070 Ti (game-free checks) · **Needs:** AMD RDNA 4 with FSR 4 ## Why With AMD FSR 4's shader tuning, a tuned pipeline published between bind and dispatch could run once on weight tables not written yet, a frame of wrong upscaling on RDNA 4. Restored from [Hookshot 2952](https://github.com/NotRexed/Hookshot/pull/2952), by jarynclouatre, and split out of [#287](https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/287) so it can be reviewed and merged on its own. ## What changed `cmd_dispatch` in `gpu/vk/vk_fsr4.cpp` decides from what the bind put in place (`struct Bind`), not from the published pipeline at dispatch time. Commits, each with its original author and message: <details><summary><code>0ae73075</code> fix(fsr4): the tail is written before its reader also when the unrolled shader arrives between bind and dispatch (jarynclouatre)</summary> With AMD FSR 4's shader tuning (https://github.com/NotRexed/Hookshot/pull/2798), a tuned pipeline reads its weight tables from a tail at the end of the library's storage buffer, and `tune::cmd_dispatch` writes that tail before the first dispatch that reads it. The unrolled pipeline is built by a background job and published into `better` from its own thread. `cmd_bind_pipeline` binds the library's pipeline when no unrolled one exists yet, but `cmd_dispatch` asked `better` again at dispatch time. When the job published between the two, the dispatch took the unrolled pipeline as bound: it skipped the tail's upload (`due` stayed set) and the fallback to AMD's own pipeline, while the tail-reading pipeline ran once on a tail not written yet. #### Change `bound` keeps, per command buffer, the library's pipeline and whether the bind put the unrolled one in its place (`struct Bind`). `cmd_dispatch` decides from that, not from `better` at dispatch time. The twin path still gets the library's pipeline from it. No lock is held across a driver call that was not held before. #### Testing - `tests/test_fsr4_bind_dispatch.py` (new): cuts `cmd_bind_pipeline`, `cmd_dispatch` and the structs they use out of `vk_fsr4.cpp` and runs them against recording stand-ins for the driver's calls, with no GPU and no `spirv-val`. Three cases: the library's pipeline bound and the unrolled one published before the dispatch (the tail is written and `due` cleared); the unrolled one bound (no tail written); a tail that cannot be read with the unrolled one published in between (AMD's own pipeline is bound). The first and third fail on main and pass here. - Linux renderer built with `HOOKSHOT_FSR=1`: `vk_fsr4.cpp` compiles. - Full suite on Linux: 2 failures, each for a tool this machine lacks (`test_bake_portability`: `zstandard`; `nx2/test_texture_init_barrier.py`: `clang++`); both fail the same on main. - Not run on an RDNA 4 card. Restored-from: https://github.com/NotRexed/Hookshot/pull/2952 </details> ## Measurements **Verdict:** nothing to measure: a correctness fix off the frame path, covered by game-free checks. ### Flags and settings None. ## Testing **Tested:** On Linux (kernel 7.0), RTX 4070 Ti, `main` @ 71b1edd8, this PR @ 0ae73075: `the checks that read the changed files (`tests/run_all.py --changed origin/main`)` pass. The full suite (`tests/run_all.py`) has not been run on this branch yet; a comment will follow when it has. **Not tested:** An RDNA 4 card. Opened as work in progress until it runs in the game. **Must be tested:** - AMD RDNA 4, FSR 4 (Test Farm: @Beanstalk an AMD RDNA 4 card with FSR 4): play 5 minutes with FSR 4 from a cold shader cache, expecting no wrong frame ## Risks **What could break:** FSR 4 on RDNA 4. **Turning it off:** revert the commit. **Compatibility:** Nothing changes. ## Checklist ### Everyone - [x] One topic. - [x] The title is a Conventional Commit (`type(scope): description`) of the status line's type. - [x] Why gives the reason plainly and links the issues that fit (the tracker searched). - [ ] Tested on my own PC where it runs (in the game, for a change the game runs), as Testing's Tested says; what can't be run by hand is in Testing's Not testable by hand, with why. Not ticked yet: The in-game run never reached the changed FSR 4 path, which needs an RDNA 4 card. - [x] Measurements has a verdict, or one line saying why there is nothing to measure. - [x] Nothing gets worse that I know of: any cost (speed, latency, memory, a setup that worked before) is measured and in Risks. - [x] Testing names what was tested, what wasn't, and what must still be tested, where and how. - [x] Rebased on current `main`. - [x] No game files, dumps, build output or generated files. - [x] `third_party/` untouched (a new folder with a `PROVENANCE.md` instead). - [x] The license is OK: GPL-3.0-or-later, nothing incompatible and nothing decompiled. - [ ] `python -I -B tests/run_all.py` passes (any failure that also fails on `main` is named). ### When it applies - [x] A game-free test added for the fix, failing on `main` and passing here.
fix(fsr4): the tail is written before its reader also when the unrolled shader arrives between bind and dispatch
Some checks are pending
CI / Conventional Commit title (pull_request) Blocked by required conditions
CI / Python source checks (ubuntu-24.04, part 1/2) (pull_request) Blocked by required conditions
CI / Python source checks (ubuntu-24.04, part 2/2) (pull_request) Blocked by required conditions
CI / SPIR-V fixtures (global narrow load/store, direct surface atomics, F2I immediate) (pull_request) Blocked by required conditions
CI / Workflow lint (actionlint) (pull_request) Blocked by required conditions
Guide screenshots / Guide images and local UI source review (pull_request) Blocked by required conditions
Third-party parts / As their notes say (pull_request) Blocked by required conditions
Third-party parts / The same as upstream (pull_request) Blocked by required conditions
Website / Website on Cloudflare (pull_request_target) Has been skipped
PR description / Description follows the template (pull_request_target) Successful in 9s
0ae7307516
With AMD FSR 4's shader tuning (https://github.com/NotRexed/Hookshot/pull/2798), a tuned pipeline reads its
weight tables from a tail at the end of the library's storage buffer,
and `tune::cmd_dispatch` writes that tail before the first dispatch that
reads it. The unrolled pipeline is built by a background job and
published into `better` from its own thread. `cmd_bind_pipeline` binds
the library's pipeline when no unrolled one exists yet, but
`cmd_dispatch` asked `better` again at dispatch time. When the job
published between the two, the dispatch took the unrolled pipeline as
bound: it skipped the tail's upload (`due` stayed set) and the fallback
to AMD's own pipeline, while the tail-reading pipeline ran once on a
tail not written yet.

## Change

`bound` keeps, per command buffer, the library's pipeline and whether
the bind put the unrolled one in its place (`struct Bind`).
`cmd_dispatch` decides from that, not from `better` at dispatch time.
The twin path still gets the library's pipeline from it. No lock is held
across a driver call that was not held before.

## Testing

- `tests/test_fsr4_bind_dispatch.py` (new): cuts `cmd_bind_pipeline`,
`cmd_dispatch` and the structs they use out of `vk_fsr4.cpp` and runs
them against recording stand-ins for the driver's calls, with no GPU and
no `spirv-val`. Three cases: the library's pipeline bound and the
unrolled one published before the dispatch (the tail is written and
`due` cleared); the unrolled one bound (no tail written); a tail that
cannot be read with the unrolled one published in between (AMD's own
pipeline is bound). The first and third fail on main and pass here.
- Linux renderer built with `HOOKSHOT_FSR=1`: `vk_fsr4.cpp` compiles.
- Full suite on Linux: 2 failures, each for a tool this machine lacks
(`test_bake_portability`: `zstandard`;
`nx2/test_texture_init_barrier.py`: `clang++`); both fail the same on
main.
- Not run on an RDNA 4 card.

Restored-from: https://github.com/NotRexed/Hookshot/pull/2952
Author
Contributor

Tested in game: Linux (Ubuntu 26.04.1, kernel 7.0.0-43), RTX 4070 Ti 12 GB, NVIDIA 595.99.02, Ryzen 7 9800X3D: this branch built together with the other 18 split from #218 and #284-#288, merged onto main @ 71b1edd8 (make_game.py --update, build v3.0.0-rc.5-82-g5952bb3d), then played by a scripted tour through the control bridge (scripts/memtour.py: a save loaded, Zora's Domain, the Market, Hyrule Field and Death Mountain Trail, 40 s each with walking and camera swings). All 4 stops reached, a clean quit, and no device loss or Vulkan error in the log. FSR 4 does not run on this NVIDIA card, so the changed path did not run; it needs an RDNA 4 card.

Tested in game: Linux (Ubuntu 26.04.1, kernel 7.0.0-43), RTX 4070 Ti 12 GB, NVIDIA 595.99.02, Ryzen 7 9800X3D: this branch built together with the other 18 split from #218 and #284-#288, merged onto main @ 71b1edd8 (make_game.py --update, build v3.0.0-rc.5-82-g5952bb3d), then played by a scripted tour through the control bridge (scripts/memtour.py: a save loaded, Zora's Domain, the Market, Hyrule Field and Death Mountain Trail, 40 s each with walking and camera swings). All 4 stops reached, a clean quit, and no device loss or Vulkan error in the log. FSR 4 does not run on this NVIDIA card, so the changed path did not run; it needs an RDNA 4 card.
Owner

Brought up to the ready-for-review standard (#1454): the checklist's Everyone list has two new boxes, added here from what this PR's description and comments show (please check they match what you know).

  • Tested on my own PC: not ticked: The in-game run never reached the changed FSR 4 path, which needs an RDNA 4 card. Run it yourself on your PC (in the game, for a change the game runs) and say where and what you saw in Testing's Tested; or, when the fix's own case can't be run by hand, add a **Not testable by hand:** line saying why and what stands in for it. Then tick it.
  • Nothing gets worse that I know of: ticked: Verdict is nothing to measure for a correctness fix off the frame path, and Risks names no slowdown.
Brought up to the [ready-for-review standard](https://git.magicbean.dev/MagicBeanDev/Longshot/src/branch/main/CONTRIBUTING.md#ready-for-review-out-of-wip) (#1454): the checklist's Everyone list has two new boxes, added here from what this PR's description and comments show (please check they match what you know). - *Tested on my own PC*: not ticked: The in-game run never reached the changed FSR 4 path, which needs an RDNA 4 card. Run it yourself on your PC (in the game, for a change the game runs) and say where and what you saw in Testing's *Tested*; or, when the fix's own case can't be run by hand, add a `**Not testable by hand:**` line saying why and what stands in for it. Then tick it. - *Nothing gets worse that I know of*: ticked: Verdict is nothing to measure for a correctness fix off the frame path, and Risks names no slowdown.
Some checks are pending
CI / Conventional Commit title (pull_request) Blocked by required conditions
CI / Python source checks (ubuntu-24.04, part 1/2) (pull_request) Blocked by required conditions
CI / Python source checks (ubuntu-24.04, part 2/2) (pull_request) Blocked by required conditions
CI / SPIR-V fixtures (global narrow load/store, direct surface atomics, F2I immediate) (pull_request) Blocked by required conditions
CI / Workflow lint (actionlint) (pull_request) Blocked by required conditions
Guide screenshots / Guide images and local UI source review (pull_request) Blocked by required conditions
Third-party parts / As their notes say (pull_request) Blocked by required conditions
Third-party parts / The same as upstream (pull_request) Blocked by required conditions
Website / Website on Cloudflare (pull_request_target) Has been skipped
PR description / Description follows the template (pull_request_target) Successful in 9s
This pull request is marked as a work in progress.
Some workflows are waiting to be reviewed.
This branch is out-of-date with the base branch
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u 'fix/fsr4-tail-order:TrashPanda-fix/fsr4-tail-order'
git switch 'TrashPanda-fix/fsr4-tail-order'
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
MagicBeanDev/Longshot!1163
No description provided.