[pr-performance][shaders] Shaders: a buffer view's start remainder is only added (DARNER_VIEW_START_FAST, on; 0 = the checked form) #303

Open
cafe wants to merge 1 commit from cafe/view-start-fast into main AGit
Contributor

Contributor role (optional)

  • Fluxer ID: Cafe#9189

What it changes

  • The problem on main: a structured view that starts at a dword but not at its stride is read as a raw view of the whole buffer, and the shader adds the start's remainder (DARNER_VIEW_START_REMAINDER, on: the dark neon signs fix). The checked lowering also asks each such view's size (one GetDimensions per view) and checks the extent with selects on every access. On an RTX 2080 Ti that costs about 2.6 ms of GPU time a frame.

The host already widens such a view (descriptor/host/viewstart.rs, window()), so it ends where the console's view ends, and the card's own bounds check keeps every access inside it. The extent check never cuts anything the host view would not, with one exception: an address within about 24 bytes below 2^32 wraps in the short form and can read up to 12 bytes before the view's start, inside the same buffer (no access we know of does that). With this change the remainder is only added: no size query, no extent check.

  • emit/lower/viewstart.rs: the short form, the default. DARNER_VIEW_START_FAST=0 gives the checked form as before.
  • emit/shader.rs, emit/mod.rs: the switch, view_start_fast().
  • shader_cache.rs: the switch is in the shader cache key, with a test that it changes the key.
  • README: one row.

How it was tested

  • Tested revision: game runs on our branch from an older main (de8d42006) with this commit; the picture check and the gates on main f3840d73d + this commit (72f50293a). viewstart.rs and the host's window() are the same on both.

  • Changes a default: yes, for every card (the shaders are rebuilt once).

  • A/B result (RTX 2080 Ti, house save, the same 50 s after the world shows, pipelines warm, 2 runs each):

    A (checked form, the default now) B (this change)
    GPU time a frame 54.3, 54.2 ms 51.8, 51.4 ms
    fps 17.98, 17.97 18.73, 18.85

    For comparison, DARNER_VIEW_START_REMAINDER=0 (before the signs fix): 50.6 ms, 19.21 fps. For context, latest main f3840d73d at its defaults measures 53.7 ms / 18.11 fps in the same place (one run).

  • Picture: a recorded frame of the house (1,551 unaligned views a frame there), translated again with DARNER_VIEW_START_FAST=0 and =1: 1,551 shaders differ, 0 pipelines refused, the replayed picture is the same to the pixel. The replay has no ray tracing.

  • Gates run on 72f50293a: cargo build --locked --release -p darner ok; cargo clippy --locked --release -p darner-shader -p darner-gpu --all-targets -- -D warnings ok; cargo test --locked --release -p darner-shader --lib -- --skip oracle64: 488 passed, 67 failed, the same 67 as on main f3840d73d on this PC (they need a Shader Model 6.6 device, which Windows 10's own D3D12 lacks, or DXIL validation). Not run this time: the darner-gpu tests (they create a D3D12 device), including the new shader cache key test. Not run: the whole-workspace gates.

  • Gameplay: RTX 2080 Ti, Windows 10 22H2, Series S profile, 2560x1440 output, DLSS Performance, hardware ray tracing, no frame generation.

  • Shader wave sizes tested: 32 lanes (NVIDIA).

  • Hardware dependence: likely general (fewer instructions); untested on AMD and Intel. On AMD the remainder fix is off by default, so nothing changes there unless it is turned on.

  • Coverage gaps: no check by eye of the night signs with this change (only the replayed daytime frame); only the house scene measured on its own.

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: none found (searched VIEW_START, start remainder, structured view; no open pull request changes emit/lower/viewstart.rs).
  • Replaces or overlaps: none found
## Contributor role (optional) - Fluxer ID: `Cafe#9189` ## What it changes - The problem on main: a structured view that starts at a dword but not at its stride is read as a raw view of the whole buffer, and the shader adds the start's remainder (`DARNER_VIEW_START_REMAINDER`, on: the dark neon signs fix). The checked lowering also asks each such view's size (one `GetDimensions` per view) and checks the extent with selects on every access. On an RTX 2080 Ti that costs about 2.6 ms of GPU time a frame. The host already widens such a view (`descriptor/host/viewstart.rs`, `window()`), so it ends where the console's view ends, and the card's own bounds check keeps every access inside it. The extent check never cuts anything the host view would not, with one exception: an address within about 24 bytes below 2^32 wraps in the short form and can read up to 12 bytes before the view's start, inside the same buffer (no access we know of does that). With this change the remainder is only added: no size query, no extent check. - `emit/lower/viewstart.rs`: the short form, the default. `DARNER_VIEW_START_FAST=0` gives the checked form as before. - `emit/shader.rs`, `emit/mod.rs`: the switch, `view_start_fast()`. - `shader_cache.rs`: the switch is in the shader cache key, with a test that it changes the key. - README: one row. ## How it was tested - Tested revision: game runs on our branch from an older `main` (de8d42006) with this commit; the picture check and the gates on `main` f3840d73d + this commit (72f50293a). `viewstart.rs` and the host's `window()` are the same on both. - Changes a default: yes, for every card (the shaders are rebuilt once). - A/B result (RTX 2080 Ti, house save, the same 50 s after the world shows, pipelines warm, 2 runs each): | | A (checked form, the default now) | B (this change) | |---|---|---| | GPU time a frame | 54.3, 54.2 ms | 51.8, 51.4 ms | | fps | 17.98, 17.97 | 18.73, 18.85 | For comparison, `DARNER_VIEW_START_REMAINDER=0` (before the signs fix): 50.6 ms, 19.21 fps. For context, latest `main` f3840d73d at its defaults measures 53.7 ms / 18.11 fps in the same place (one run). - Picture: a recorded frame of the house (1,551 unaligned views a frame there), translated again with `DARNER_VIEW_START_FAST=0` and `=1`: 1,551 shaders differ, 0 pipelines refused, the replayed picture is the same to the pixel. The replay has no ray tracing. - Gates run on 72f50293a: `cargo build --locked --release -p darner` ok; `cargo clippy --locked --release -p darner-shader -p darner-gpu --all-targets -- -D warnings` ok; `cargo test --locked --release -p darner-shader --lib -- --skip oracle64`: 488 passed, 67 failed, the same 67 as on `main` f3840d73d on this PC (they need a Shader Model 6.6 device, which Windows 10's own D3D12 lacks, or DXIL validation). Not run this time: the `darner-gpu` tests (they create a D3D12 device), including the new shader cache key test. Not run: the whole-workspace gates. - Gameplay: RTX 2080 Ti, Windows 10 22H2, Series S profile, 2560x1440 output, DLSS Performance, hardware ray tracing, no frame generation. - Shader wave sizes tested: 32 lanes (NVIDIA). - Hardware dependence: likely general (fewer instructions); untested on AMD and Intel. On AMD the remainder fix is off by default, so nothing changes there unless it is turned on. - Coverage gaps: no check by eye of the night signs with this change (only the replayed daytime frame); only the house scene measured on its own. ## 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: none found (searched VIEW_START, start remainder, structured view; no open pull request changes `emit/lower/viewstart.rs`). - Replaces or overlaps: none found
Collaborator

Thanks for this, and for the A/B table. I read the change against the code on 72f50293a. The reasoning holds, with one small edge (3) and four questions.

What I checked

  1. The host writes the remainder into the stride word only when window() widened the view (descriptor/host.rs:6022, make_twinned gives the remainder only for a widened view, descriptor/host/viewstart.rs, window). A view that was not widened carries remainder 0, and then both forms leave the address alone (view_byte: the checked form's has_bias select returns the plain byte).
  2. The widened view starts at start & !15 and is count * stride + remainder bytes long. So for one dword, the card's test addr + remainder + 4 <= size + remainder is the same as the checked form's addr + 4 <= logical.
  3. One edge: the checked form sends a wrapped address to "outside". In the fast form addr + delta + remainder can wrap past 2^32 and land in the first remainder bytes of the widened view, which are bytes before the console view's start but inside the buffer. That needs an address within about 24 bytes below 2^32 (the remainder is 4, 8 or 12, plus a delta of up to 12; for example a small negative offset). I doubt it matters, but it is not "outside either way".
  4. Every caller goes through view_byte: view_dwords and the stores and loads (lower.rs:7932, 8042, 8119, 13862, 13918 and the buffer_byte calls from 14000 on), the scalar constant loads (lower.rs:8110, 8142) and the BVH node fetches (intersect.rs:306, 518). None of them uses the skipped size in fast mode: view_bounds only hands it on to view_byte.

Questions

  • A multi-dword access that straddles the view's end (view_dwords up to 4 dwords, the buffer_byte(..., 4 * count) stores and loads, BVH node fetches of n * 4 bytes): the checked form drops the whole access (the long_enough/inside test uses the full width). The fast form leaves it to the card's own out-of-bounds rule for raw buffer loads, which may return the in-range dwords and zero for the rest. Is that intended, and does it match what the console does for such an access?
  • The replay that gave the same picture had no ray tracing, but this also changes the BVH node fetches (intersect.rs:518). Could you check once with ray tracing on: a replay that includes it, or the night signs and a ray-traced scene by eye? The PR says the night signs were not looked at with this change.
  • Since it changes a default and the shader cache key, the PR template asks for the workspace gates: cargo clippy --workspace --all-targets --locked -- -D warnings, the same with --all-features, cargo test --workspace --locked, and the darner-gpu tests including the new view_start_fast_changes_the_shader_cache_key. The PR says these were not run, and I understand Windows 10 can't run some of them (the Shader Model 6.6 ones). We can run the full set on our PC (Windows 11, RTX 2070 SUPER) and report the result here on 72f50293a.
  • On the rebuild: with the switch on by default and in the cache key, every player rebuilds their shaders once with the release that carries it. It would be kind to land it in the same release as other changes that already change the translator output or the cache key, so players rebuild once, not twice.

A/B offer

When our PC is free we can run the same A/B on an RTX 2070 SUPER (Turing, the same generation as the 2080 Ti), DARNER_VIEW_START_FAST=0 against the default, as launch pairs in ABBA order (the switch is in the shader cache key, so it can't change while the game runs), and post the numbers with the n and the spread here. Say if you'd rather we didn't.

This comment was written with AI assistance (Claude Code); nothing here has been run yet.

Thanks for this, and for the A/B table. I read the change against the code on 72f50293a. The reasoning holds, with one small edge (3) and four questions. **What I checked** 1. The host writes the remainder into the stride word only when `window()` widened the view (`descriptor/host.rs:6022`, `make_twinned` gives the remainder only for a widened view, `descriptor/host/viewstart.rs`, `window`). A view that was not widened carries remainder 0, and then both forms leave the address alone (`view_byte`: the checked form's `has_bias` select returns the plain byte). 2. The widened view starts at `start & !15` and is `count * stride + remainder` bytes long. So for one dword, the card's test `addr + remainder + 4 <= size + remainder` is the same as the checked form's `addr + 4 <= logical`. 3. One edge: the checked form sends a wrapped address to "outside". In the fast form `addr + delta + remainder` can wrap past 2^32 and land in the first `remainder` bytes of the widened view, which are bytes before the console view's start but inside the buffer. That needs an address within about 24 bytes below 2^32 (the remainder is 4, 8 or 12, plus a delta of up to 12; for example a small negative offset). I doubt it matters, but it is not "outside either way". 4. Every caller goes through `view_byte`: `view_dwords` and the stores and loads (`lower.rs:7932, 8042, 8119, 13862, 13918` and the `buffer_byte` calls from `14000` on), the scalar constant loads (`lower.rs:8110, 8142`) and the BVH node fetches (`intersect.rs:306, 518`). None of them uses the skipped size in fast mode: `view_bounds` only hands it on to `view_byte`. **Questions** - A multi-dword access that straddles the view's end (`view_dwords` up to 4 dwords, the `buffer_byte(..., 4 * count)` stores and loads, BVH node fetches of `n * 4` bytes): the checked form drops the whole access (the `long_enough`/`inside` test uses the full width). The fast form leaves it to the card's own out-of-bounds rule for raw buffer loads, which may return the in-range dwords and zero for the rest. Is that intended, and does it match what the console does for such an access? - The replay that gave the same picture had no ray tracing, but this also changes the BVH node fetches (`intersect.rs:518`). Could you check once with ray tracing on: a replay that includes it, or the night signs and a ray-traced scene by eye? The PR says the night signs were not looked at with this change. - Since it changes a default and the shader cache key, the PR template asks for the workspace gates: `cargo clippy --workspace --all-targets --locked -- -D warnings`, the same with `--all-features`, `cargo test --workspace --locked`, and the `darner-gpu` tests including the new `view_start_fast_changes_the_shader_cache_key`. The PR says these were not run, and I understand Windows 10 can't run some of them (the Shader Model 6.6 ones). We can run the full set on our PC (Windows 11, RTX 2070 SUPER) and report the result here on 72f50293a. - On the rebuild: with the switch on by default and in the cache key, every player rebuilds their shaders once with the release that carries it. It would be kind to land it in the same release as other changes that already change the translator output or the cache key, so players rebuild once, not twice. **A/B offer** When our PC is free we can run the same A/B on an RTX 2070 SUPER (Turing, the same generation as the 2080 Ti), `DARNER_VIEW_START_FAST=0` against the default, as launch pairs in ABBA order (the switch is in the shader cache key, so it can't change while the game runs), and post the numbers with the n and the spread here. Say if you'd rather we didn't. This comment was written with AI assistance (Claude Code); nothing here has been run yet.
Author
Contributor

Thanks for the careful read. Answers:

  • Edge 3 (wrap past 2^32): agreed, it is not "outside either way". The fast form can then read up to 12 bytes before the view's start, inside the same buffer. That needs an address within about 24 bytes below 2^32, which no access we know of produces. I'll correct the description.
  • A multi-dword access across the view's end: yes, that is intended. The console checks each dword on its own: the dwords inside are read and the rest are 0. Darner's own test scalar_buffer_loads_read_the_same_dwords_one_call_or_four expects the same thing. So here the fast form is closer to the console than the checked form, which drops the whole access.
  • Ray tracing: fair point. I'll run the replay pixel check again on a capture with ray tracing on, and look at the night signs in a ray-traced scene, then post the result here.
  • Gates: yes please. Some of them can't run on Windows 10 here (Shader Model 6.6), so your full run on 72f50293a would help.
  • A/B on your 2070 SUPER: yes please, a second Turing card is very welcome.
  • The rebuild: agreed. It's best to land this with other changes to the cache key; I'll leave the timing to TrustNo1.
Thanks for the careful read. Answers: - **Edge 3 (wrap past 2^32):** agreed, it is not "outside either way". The fast form can then read up to 12 bytes before the view's start, inside the same buffer. That needs an address within about 24 bytes below 2^32, which no access we know of produces. I'll correct the description. - **A multi-dword access across the view's end:** yes, that is intended. The console checks each dword on its own: the dwords inside are read and the rest are 0. Darner's own test `scalar_buffer_loads_read_the_same_dwords_one_call_or_four` expects the same thing. So here the fast form is closer to the console than the checked form, which drops the whole access. - **Ray tracing:** fair point. I'll run the replay pixel check again on a capture with ray tracing on, and look at the night signs in a ray-traced scene, then post the result here. - **Gates:** yes please. Some of them can't run on Windows 10 here (Shader Model 6.6), so your full run on 72f50293a would help. - **A/B on your 2070 SUPER:** yes please, a second Turing card is very welcome. - **The rebuild:** agreed. It's best to land this with other changes to the cache key; I'll leave the timing to TrustNo1.
This pull request has changes conflicting with the target branch.
  • crates/darner-gpu/src/shader_cache.rs
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/303/head:cafe/view-start-fast'
git switch 'cafe/view-start-fast'

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 'cafe/view-start-fast'
git switch 'cafe/view-start-fast'
git rebase 'main'
git switch 'main'
git merge --ff-only 'cafe/view-start-fast'
git switch 'cafe/view-start-fast'
git rebase 'main'
git switch 'main'
git merge --no-ff 'cafe/view-start-fast'
git switch 'main'
git merge --squash 'cafe/view-start-fast'
git switch 'main'
git merge --ff-only 'cafe/view-start-fast'
git switch 'main'
git merge 'cafe/view-start-fast'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
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
TrustNo1/Darner!303
No description provided.