Skip to content

gpu/ambient: LDS-tile gather compute pass and scope internal UAV barriers (D3D12-GPU-AMBIENT-LDS-GATHER) - #2893

Open
djrolfes wants to merge 1 commit into
ProjectLeonida:mainfrom
djrolfes:djrolfes/gpu-ambient-lds-gather
Open

djrolfes wants to merge 1 commit into
ProjectLeonida:mainfrom
djrolfes:djrolfes/gpu-ambient-lds-gather

Conversation

@djrolfes

@djrolfes djrolfes commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: D3D12-GPU-AMBIENT-LDS-GATHER.

What and why

Tile the ambient::gather compute shader through workgroup shared memory (LDS) for 64× fewer global UAV reads and scope internal ambient UAV barriers (d3d12/crates/leonida-gpu/src/ambient.rs).

  • Why:
    1. On main, gather runs [numthreads(8, 2, 1)] (Dispatch(20, 45, 1)) and every one of the 14,400 (160 × 90) threads independently reads all CELLS = 2,304 entries of cells[] (cells[CELLS + 1 + i] and cells[i].rgb) from global UAV memory.
    2. Every internal transition (keep → mean, mean → ..., place → gather) emitted a global pResource = NULL UAV barrier, and Role::Resolve also emitted a pre-dispatch global UAV barrier before place + gather even though neither touches views.image.
  • Change:
    • Converts gather to [numthreads(8, 8, 1)] (Dispatch(20, 12, 1)), cooperatively loading 64-cell tiles into groupshared float4 tile_pos[64] and groupshared float3 tile_light[64] (CELLS = 64 × 36 = 2,304, an exact multiple of 64), reducing global UAV reads by 64× while guarding partial-group rows (90 % 8 == 2) with bool active = at.x < PX && at.y < PY; so inactive threads still participate in cooperative loads and GroupMemoryBarrierWithGroupSync() before returning.
    • Extends uav_barrier(list, resource: Option<&ID3D12Resource>) to scope internal cells transitions (keep → mean, after mean, place → gather) to Some(&pass.cells).
    • In Role::Resolve, defers the global uav_barrier(l, None) until immediately before pass.raise (synchronizing both pass.points written by gather and the title's views.image UAV write before raise reads/modifies views.image).
  • Evidence classification: READ IN CODE (d3d12/crates/leonida-gpu/src/ambient.rs) + live D3D12 GPU red→green test (gather_dispatches_the_partial_group) + MEASURED 300 s in-world Option-A roam gate with LEONIDA_AMBIENT=0.8 and local mid-roam video visual/flicker check.

Tested

  • python3 -I scripts/check_no_game_data.py d3d12/crates/leonida-gpu/src/ambient.rs → exit 0.
  • Cross-compiled leonida-gpu unit test binary (x86_64-pc-windows-msvc) and ran live D3D12 GPU tests under Wine + VKD3D-Proton 3.2.0 (NVIDIA GeForce RTX 3070 Ti, driver 620.73.04):
    • RED on origin/main (only the new GPU test ambient::tests::gather_dispatches_the_partial_group applied, dispatching (20, 12, 1) and asserting all 160 × 90 output points receive the final tile CELLS - 1):
      running 1 test
      thread 'ambient::tests::gather_dispatches_the_partial_group' panicked:
      point 3840, channel 0: [NaN, NaN, NaN, NaN]
      test result: FAILED. 0 passed; 1 failed; 0 ignored
      
      (3840 = 24 × 160 — rows 24..90 were left unwritten under main's [numthreads(8, 2, 1)].)
    • GREEN on PR head e9e8b257b (rebased onto 3e7b34a52):
      running 4 tests
      test ambient::tests::gather_dispatches_the_partial_group ... ok
      test ambient::tests::roles_by_name ... ok
      test ambient::tests::settings ... ok
      test ambient::tests::the_passes_build ... ok
      test result: ok. 4 passed; 0 failed; 0 ignored; 0 measured; 687 filtered out
      
  • 300 s in-world game gate (MEASURED on exact PR head e9e8b257b, Series S + DLSS Quality 1280×720 → 1920×1080, LEONIDA_AMBIENT=0.8, Option A mods/gate_driver mode = roam):
    • Verdict: PASS (310.35 s in-world after world entry at 74.71 s).
    • Frames & pacing: 1,580 frames after +60 s (1,648 total world presents), 9.85 median FPS (10.90 end median FPS), 405.56 ms p95 frame time, 6.964 s longest present gap (<= 10.0 s).
    • Memory & stability: 6,692 MiB peak VRAM (<= 7,400 MiB), 0 device losses (0x887A0005), 0 crashes, 0 panics, 0 faults before stop, 0 kernel NVRM: Xid errors.
    • Option A roam control: GATE CONTROL reached at t = 79.7 s (moved = 7.62 m, mode = roam), 30 GATE MARK roam ticks, 0 GATE FAIL.
    • Local visual video check: Captured 48 consecutive presented frames mid-roam with LEONIDA_AMBIENT=0.8 (reversals_ge_5 = 0, blink_pct_mean = 0.402%); visual inspection confirmed clean indirect ambient lighting with zero tile seams (including the partial bottom row 90 % 8 == 2), zero NaN/black blocks, and zero flicker during camera motion (temporary capture deleted locally; no frames attached).

GPUs tested: NVIDIA GeForce RTX 3070 Ti 8 GB, driver 620.73.04, Arch Linux (Wine 11.22 + VKD3D-Proton 3.2.0).

AI-assisted: yes (authored by Gemini 4 Neon; reviewed in a separate independent review pass by Gemini 4 Neon).

Checklist

  • Based on current main (3e7b34a52) and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (check_no_game_data.py clean on both source and PR description).
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (n/a — GPU ambient stand-in compute pass optimization)

@merzleckeier

Copy link
Copy Markdown
Contributor

Nonuniform gather readback across both adapters at e9e8b25 + 0ff8a53. All four ambient unit tests passed on each GPU. I then ran main's 8×2 gather and this PR's 8×8 tiled gather against the same 2,304 synthetic cells with varying positions, RGB and weights (including zero weights) and a null depth SRV. Both readbacks wrote all 14,400 points (172,800 finite floats).

Adapter (Wine 11.22 / VKD3D-Proton 3.1.0) Comparison to main
RTX 4070 SUPER, 620.73.04 All 172,800 floats bit-identical
Radeon Raphael, RADV 26.3.1 Maximum absolute difference 2.98e-7

The readback required an actual D3D12 device. Dispatching only 11 tiled groups vertically as a negative control left exactly the last two rows (3,840 floats) unwritten on each GPU. This exercises the gather kernel and partial-group coverage. The full ambient pass sequence and frame-time effect were outside the fixture.

MEASURED with synthetic inputs. AI-assisted (Codex).

@github-actions github-actions Bot added the needs rebase Conflicts with the base branch: rebase it on main label Dec 1, 2026

@KelboLive KelboLive left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tiling is faithful: accumulation order is unchanged (base 0→2304 step 64, j 0→63), 20×12 groups of 8×8 covers 160×96 ⊇ 160×90, and active plus both group barriers keep the 90 % 8 == 2 tail legal; CELLS = 2304 = 36×64 keeps the tile loads in range. Barrier scoping is sound: keep→mean and place→gather touch only cells, and the global barrier before raise still orders gather's points and the title's image write ahead of raise's read-modify-write (ambient.rs:614), with the post-raise barrier kept — I found no new raise/resolve race. 632/0/59 on the crate, and the new test passes on the real adapter and on WARP.

The test, however, pins the kernel, not the wiring:

  • It hardcodes its own Dispatch(20,12,1) (ambient.rs:969) instead of the call-site grid, so shrinking the production dispatch to 20×11 leaves all 632 tests green — I verified it.
  • It never calls after(), so deleting the pre-gather cells barrier also leaves the suite green — verified.
  • It returns silently when no device exists (:915), so on a host without one it is a no-op rather than a SKIP.

Sharing one grid const between the call site and the test would make the full-coverage assertion guard the wiring, and calling after() would give the barrier a test.

Two notes:

  • CELLS % 64 == 0 is an unstated invariant (an out-of-range load if CX ever stops being 64) — a static assert would pin it.
  • "64× fewer global UAV reads" is per-thread; the total is 60× (14,400 → 15,360 threads, 33.2 M → 0.55 M cell loads). Worth correcting in the body.

Merge: the only conflict is the appended test at the end of mod tests, because #2904 merged at 17:14Z and already appended the_colour_drops_compile; keep both (and the two closing braces). If #2908 lands first, switch the new test's host() to require_host() so it SKIPs loudly.

@Kal0s-design

Copy link
Copy Markdown
Contributor

Checked on native Windows 11 with the native D3D12 driver (no VKD3D), rustc 1.98.1.

Heads-up: this now conflicts with main (d2a564b66). fe612066a added the_colour_drops_compile at the end of ambient.rs's tests, in the same spot as gather_dispatches_the_partial_group. It is only the two tests touching; keeping both resolves it. I resolved it that way locally and tested the result.

Head e9e8b257b + main d2a564b66, conflict resolved by keeping both tests:

  • cargo test -p leonida-gpu --lib ambient:: on the card: 5 passed, 0 failed.
  • The same with LEONIDA_D3D12_WARP=1 (software adapter): 5 passed. gather_dispatches_the_partial_group also passes there, so the partial last row is written on WARP too.

Barrier scoping. I went through which UAV each pass writes. keep, mean and place write only cells, gather writes only points, and raise writes the title's view. So scoping the keep -> mean -> ... and place -> gather barriers to pass.cells covers every dependency. The global barrier before raise covers points and the title's view. Radiance still starts with the global barrier, which keep needs since it reads the title's radiance UAV. I see no missing wait.

What the LDS tiling buys on a native driver. Same random 2,304 cells and null depth SRV on both sides; gather alone, run 201 times in one list with a UAV barrier between dispatches. Best of 5 rounds, minus a 1-dispatch list. Two runs each:

per gather dispatch
main (8x2, Dispatch(20, 45, 1)) 0.191 / 0.201 ms
this PR (8x8 + LDS, Dispatch(20, 12, 1)) 0.079 / 0.087 ms

That is about 2.3-2.4x faster for the dispatch, about 0.11 ms a frame when the pass runs. These are wall-clock times around a synchronous submit, not GPU timestamps, so treat them as approximate.

One wording note on the description: the "RED on origin/main" is the new test dispatching (20, 12, 1) against main's 8x2 shader. main itself dispatches (20, 45, 1) and covers all 90 rows, so that run shows the test needs the PR's group size. It is not a defect on main.

Not checked: the game.

GPUs tested: one hardware adapter (native D3D12 driver) and WARP.

AI-assisted: yes (Claude Code).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs rebase Conflicts with the base branch: rebase it on main

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants