shader: cache immediate sampler handles in the entry block #438

Open
Paliton wants to merge 2 commits from Paliton/Darner-fork:pr/shader-sampler-cache into Main
First-time contributor

Contributor role (optional)

  • Fluxer ID:

What it changes

  • The problem on main: read from the code (crates/darner-shader/src/emit/shader.rs). Unlike root_cbv, root_srv, and root_uav — which emit their createHandleFromBinding + annotateHandle once in self.entry and cache the handle — Shader::sampler_handle emitted a fresh createHandleFromBinding + annotateHandle pair in the caller's block on every texture sample call.

Caches immediate sampler handles keyed by (slot, is_cmp) in Shader::sampler_handles and emits them once in self.entry, matching root_cbv/root_srv/root_uav. Purely exact (no semantic or visual change); adds a unit test sampler_handle_is_cached_in_entry_block.

How it was tested

  • Tested revision: 1efcf68 (based on f3840d7)

  • Commands and results:

    • cargo xtask policy -> asset policy: 555 files ok
    • cargo clippy --workspace --all-targets --locked -- -D warnings -> ok (0 warnings)
    • cargo clippy --workspace --all-targets --all-features --locked -- -D warnings -> ok (0 warnings)
    • cargo test -p darner-shader --lib --locked sampler_handle -- --test-threads=1 -> 1 passed; 0 failed
    • cargo test --workspace --locked -> all non-flaky tests pass (1772/1773 in darner-gpu; the single failure is the pre-existing watch::tests::ask_once_by_res_splits_reservations_without_refusals flake fixed by #262)
  • Changes a default: yes (emitted DXIL reuses cached entry-block sampler handles; exact equivalence, no behaviour change).

  • A/B result: 2 runs each of 240 s automated opening sequence from the same snapshot, --profile diag --features diagnostics (generic x86-64 build), pure play-defaults, RTX 5070 12 GB + i7-14700KF, Series X, 1440p -> 4K, DLSS off, FG off, RT off. Both B runs beat both A runs in Window 2 and Window 3 (steady-state GPU-bound):

    Metric (2 runs each) A (off, f3840d7) B (on, 1efcf68)
    Total frames (240 s) 5,367 / 5,537 (mean 5,452) 5,569 / 5,533 (mean 5,551, +99)
    Window 1 (90-130 s) fps / med ms 21.23 / 22.43 fps (46.42 / 44.97 ms) 21.69 / 22.43 fps (45.11 / 44.98 ms)
    Window 2 (130-180 s) fps / med ms 21.24 / 21.86 fps (45.77 / 44.46 ms) 22.07 / 22.06 fps (44.26 / 44.10 ms)
    Window 3 (180-235 s) fps (mean) 21.52 / 22.16 (21.84 fps) 22.27 / 22.24 (22.25 fps, +0.41)
    Window 3 median frame time 45.26 / 43.89 (44.58 ms) 43.66 / 43.77 (43.72 ms, -0.86 ms)
    Window 3 p95 / p99 frame time 47.1 / 94.3 ms (mean) 46.7 / 93.1 ms (mean)
    Window 3 GPU (card_lists_ms) 43.63 / 42.43 (43.03 ms) 42.14 / 42.16 (42.15 ms, -0.89 ms)
  • Gates run on that revision: all three AGENTS.md gates + cargo xtask policy on 1efcf68.

  • Gameplay, when relevant: RTX 5070 12 GB, Series X, 1440p -> 4K, DLSS off, FG off, RT off. Visuals verified clean across captured frames.

  • Shader wave sizes tested, when relevant: 32 lanes (RTX 5070).

  • Hardware dependence: general (translator IR only); driver impact depends on how aggressively the GPU compiler already hoists createHandleFromBinding.

  • Coverage gaps: tested on NVIDIA RTX 5070 (32-lane) only; untested on AMD/Intel or 64-lane wave size.

Checklist

  • I ran this change in the game, or say below why it can't be (docs, tests only).
  • This PR contains one change based on main.
  • The diff follows docs/asset-policy.md.
  • I recorded this PR's dependencies below.
    • Depends on: none

Checked for overlap

  • I searched the open pull requests for the same issue and for the files this touches.
  • Related PRs: #303, #394 (also touch crates/darner-shader/src/emit/shader.rs, non-overlapping lines)
  • Replaces or overlaps: none found
## Contributor role (optional) - Fluxer ID: ## What it changes - The problem on main: read from the code (`crates/darner-shader/src/emit/shader.rs`). Unlike `root_cbv`, `root_srv`, and `root_uav` — which emit their `createHandleFromBinding` + `annotateHandle` once in `self.entry` and cache the handle — `Shader::sampler_handle` emitted a fresh `createHandleFromBinding` + `annotateHandle` pair in the caller's block on every texture sample call. Caches immediate sampler handles keyed by `(slot, is_cmp)` in `Shader::sampler_handles` and emits them once in `self.entry`, matching `root_cbv`/`root_srv`/`root_uav`. Purely exact (no semantic or visual change); adds a unit test `sampler_handle_is_cached_in_entry_block`. ## How it was tested - Tested revision: `1efcf68` (based on `f3840d7`) - Commands and results: - `cargo xtask policy` -> `asset policy: 555 files ok` - `cargo clippy --workspace --all-targets --locked -- -D warnings` -> ok (0 warnings) - `cargo clippy --workspace --all-targets --all-features --locked -- -D warnings` -> ok (0 warnings) - `cargo test -p darner-shader --lib --locked sampler_handle -- --test-threads=1` -> `1 passed; 0 failed` - `cargo test --workspace --locked` -> all non-flaky tests pass (`1772/1773` in `darner-gpu`; the single failure is the pre-existing `watch::tests::ask_once_by_res_splits_reservations_without_refusals` flake fixed by #262) - Changes a default: yes (emitted DXIL reuses cached entry-block sampler handles; exact equivalence, no behaviour change). - A/B result: 2 runs each of 240 s automated opening sequence from the same snapshot, `--profile diag --features diagnostics` (generic x86-64 build), pure `play-defaults`, RTX 5070 12 GB + i7-14700KF, Series X, 1440p -> 4K, DLSS off, FG off, RT off. Both B runs beat both A runs in Window 2 and Window 3 (steady-state GPU-bound): | Metric (2 runs each) | A (off, `f3840d7`) | B (on, `1efcf68`) | |---|---|---| | Total frames (240 s) | 5,367 / 5,537 (mean 5,452) | 5,569 / 5,533 (mean **5,551**, +99) | | Window 1 (90-130 s) fps / med ms | 21.23 / 22.43 fps (46.42 / 44.97 ms) | 21.69 / 22.43 fps (45.11 / 44.98 ms) | | Window 2 (130-180 s) fps / med ms | 21.24 / 21.86 fps (45.77 / 44.46 ms) | 22.07 / 22.06 fps (44.26 / 44.10 ms) | | Window 3 (180-235 s) fps (mean) | 21.52 / 22.16 (**21.84 fps**) | 22.27 / 22.24 (**22.25 fps**, +0.41) | | Window 3 median frame time | 45.26 / 43.89 (**44.58 ms**) | 43.66 / 43.77 (**43.72 ms**, -0.86 ms) | | Window 3 p95 / p99 frame time | 47.1 / 94.3 ms (mean) | 46.7 / 93.1 ms (mean) | | Window 3 GPU (`card_lists_ms`) | 43.63 / 42.43 (**43.03 ms**) | 42.14 / 42.16 (**42.15 ms**, -0.89 ms) | - Gates run on that revision: all three `AGENTS.md` gates + `cargo xtask policy` on `1efcf68`. - Gameplay, when relevant: RTX 5070 12 GB, Series X, 1440p -> 4K, DLSS off, FG off, RT off. Visuals verified clean across captured frames. - Shader wave sizes tested, when relevant: 32 lanes (RTX 5070). - Hardware dependence: general (translator IR only); driver impact depends on how aggressively the GPU compiler already hoists `createHandleFromBinding`. - Coverage gaps: tested on NVIDIA RTX 5070 (32-lane) only; untested on AMD/Intel or 64-lane wave size. ## Checklist - [x] I ran this change in the game, or say below why it can't be (docs, tests only). - [x] This PR contains one change based on `main`. - [x] The diff follows `docs/asset-policy.md`. - [x] I recorded this PR's dependencies below. - Depends on: none ## Checked for overlap - [x] I searched the open pull requests for the same issue and for the files this touches. - Related PRs: #303, #394 (also touch `crates/darner-shader/src/emit/shader.rs`, non-overlapping lines) - Replaces or overlaps: none found
Contributor

Test report on an RTX 4080, together with #439: no measurable change in card time here.

PC: RTX 4080 16 GB (driver 621.40), i7-13700K, 64 GB, Windows 11 26300. Series X profile, 1440p with DLSS Quality, frame generation off, hardware ray tracing on, Reflex off.
Builds: A = main 47d21cd + #334, #337, #457, #274, #397, #312, #304, #303. B = A + #438 (9483e65) + #439 (77add49); both change the translations, so B had two warm-up runs first (the process building pipelines at about 22 logical processors) and the table has the third.
Method: the opening scene of one save, entered the same way every run (no input after the start screen); medians of per-second values from the host log in three 45-second windows from the moment the world appears.

A (warm) B (warm)
card time in the title's lists, ms a frame 23.5 / 23.5 / 23.5 23.4 / 23.3 / 23.4
frame time, ms 26.2 / 25.8 / 25.7 25.6 / 25.5 / 25.6

0.1 to 0.2 ms, within the run-to-run spread (about 0.3 ms); the driver's compiler may already fold the repeated handles and checks. Picture 60 s in matches A; no errors in the log besides Streamline's presentCommon() was not observed once at start, which A logs too.

Not tested: each PR on its own, more than one run each, other cards.

Test report on an RTX 4080, together with #439: no measurable change in card time here. **PC:** RTX 4080 16 GB (driver 621.40), i7-13700K, 64 GB, Windows 11 26300. Series X profile, 1440p with DLSS Quality, frame generation off, hardware ray tracing on, Reflex off. **Builds:** A = main 47d21cd + #334, #337, #457, #274, #397, #312, #304, #303. B = A + #438 (9483e65) + #439 (77add49); both change the translations, so B had two warm-up runs first (the process building pipelines at about 22 logical processors) and the table has the third. **Method:** the opening scene of one save, entered the same way every run (no input after the start screen); medians of per-second values from the host log in three 45-second windows from the moment the world appears. | | A (warm) | B (warm) | |---|---|---| | card time in the title's lists, ms a frame | 23.5 / 23.5 / 23.5 | 23.4 / 23.3 / 23.4 | | frame time, ms | 26.2 / 25.8 / 25.7 | 25.6 / 25.5 / 25.6 | 0.1 to 0.2 ms, within the run-to-run spread (about 0.3 ms); the driver's compiler may already fold the repeated handles and checks. Picture 60 s in matches A; no errors in the log besides Streamline's `presentCommon() was not observed` once at start, which A logs too. Not tested: each PR on its own, more than one run each, other cards.
First-time contributor

Merged onto c8258f9, passed cargo xtask policy, clippy --all-features --all-targets -- -D warnings (0 warnings), and 1,819 darner-gpu + 219 darner unit tests, then verified through a full 16-minute release shader preload (DARNER_RDNA_FLOAT=2) and two 30,000+ frame Series X sessions on RTX 5070 Ti (16 GB) / Core Ultra 7 265KF / 64 GB DDR5-6400 with zero sampler-binding issues, crashes, or shader regressions.

Merged onto `c8258f9`, passed `cargo xtask policy`, `clippy --all-features --all-targets -- -D warnings` (0 warnings), and `1,819 darner-gpu` + `219 darner` unit tests, then verified through a full 16-minute release shader preload (`DARNER_RDNA_FLOAT=2`) and two 30,000+ frame Series X sessions on RTX 5070 Ti (16 GB) / Core Ultra 7 265KF / 64 GB DDR5-6400 with zero sampler-binding issues, crashes, or shader regressions.
TrustNo1 changed title from [pr-performance][shaders] shader: cache immediate sampler handles in the entry block to shader: cache immediate sampler handles in the entry block 2026-12-06 03:29:03 +00:00
Owner

Test needed — commit 9483e652; what to check:

  • The problem on main: read from the code (crates/darner-shader/src/emit/shader.rs). Unlike root_cbv, root_srv, and root_uav — which emit their createHandleFromBinding + annotateHandle once in self.entry and cache the handle — Shader::sampler_handle emitted a fresh createHandleFromBinding + annotateHandle pair in the caller's block on every texture sample call.

Caches immediate sampler handles keyed by (slot, is_cmp) in Shader::sampler_handles and emits them once in self.entry, matching root_cbv/root_srv/root_uav. Purely exact (no semantic or visual change); adds a unit test sampler_handle_is_cached_in_entry_block.

Evidence to post: card + driver, settings, commit tested, before/after (switch off vs on, two runs each), fps median and p99, and one line saying you looked at the picture.

Reply testing to take this run; it's free again after 24 h with no results.

**Test needed** — commit `9483e652`; what to check: - The problem on main: read from the code (`crates/darner-shader/src/emit/shader.rs`). Unlike `root_cbv`, `root_srv`, and `root_uav` — which emit their `createHandleFromBinding` + `annotateHandle` once in `self.entry` and cache the handle — `Shader::sampler_handle` emitted a fresh `createHandleFromBinding` + `annotateHandle` pair in the caller's block on every texture sample call. Caches immediate sampler handles keyed by `(slot, is_cmp)` in `Shader::sampler_handles` and emits them once in `self.entry`, matching `root_cbv`/`root_srv`/`root_uav`. Purely exact (no semantic or visual change); adds a unit test `sampler_handle_is_cached_in_entry_block`. Evidence to post: card + driver, settings, commit tested, before/after (switch off vs on, two runs each), fps median and p99, and one line saying you looked at the picture. Reply `testing` to take this run; it's free again after 24 h with no results.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u 'pr/shader-sampler-cache:Paliton-pr/shader-sampler-cache'
git switch 'Paliton-pr/shader-sampler-cache'

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 'Paliton-pr/shader-sampler-cache'
git switch 'Paliton-pr/shader-sampler-cache'
git rebase 'Main'
git switch 'Main'
git merge --ff-only 'Paliton-pr/shader-sampler-cache'
git switch 'Paliton-pr/shader-sampler-cache'
git rebase 'Main'
git switch 'Main'
git merge --no-ff 'Paliton-pr/shader-sampler-cache'
git switch 'Main'
git merge --squash 'Paliton-pr/shader-sampler-cache'
git switch 'Main'
git merge --ff-only 'Paliton-pr/shader-sampler-cache'
git switch 'Main'
git merge 'Paliton-pr/shader-sampler-cache'
git push origin 'Main'
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
4 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
TrustNo1/Darner!438
No description provided.