Shaders: keep d16 bits of heap-picked integer views; nearest float for normalized buffer codes #240

Manually merged
github merged 2 commits from github-bot/gh-240 into gh-pr/240/base 2026-12-06 01:07:29 +00:00 AGit
Owner

@⁠ambrosia-works on GitHub · 2026-12-05 22:00 UTC · original
ambrosia-works:fix/d16-integer-views-norm-codes → main · +1088 −42 · 12 files · 2 commits · merged 2026-12-05 23:34 UTC

Summary

Two shader-translator correctness fixes, ported from the cool_fox fork (9a6c829d6, after VIctory 4243a96a and da82e444). Every shader is translated anew: lower.rs and shader.rs are hashed into the translation cache key.

1. d16 data of integer typed views picked from the heap keeps its bits

The facial animation network keeps its tensors in R16G16B16A16_UINT / R16_UINT typed buffer views and reads and writes them as d16 data. halves.rs already handles such views at fixed table places. For a view the program picks from the view heap at run time, the translator still took d16 data as f16 and converted it, which changes the integer bits. This is a candidate cause of the broken facial animation on AMD (only one eye blinks, mouths warp while talking). I have not verified that in-game yet.

  • The host now marks typed buffer views of UINT/SINT formats in the table of strides (VIEW_INTEGER = bit 31, VIEW_SIGNED = bit 30; descriptor/host.rs view_kind).
  • lower.rs integer_view reads the marks:
    • d16 stores zero- or sign-extend each 16-bit channel.
    • d16 loads keep the low 16 bits.
  • halves.rs's unsigned-twin path is unchanged and still runs first.
  • Every reader of the stride masks the marks off with VIEW_STRIDE_BITS: slot_stride, and the four replacement shaders that read t255 directly (3592426e3e9142d8, a74a0746279f960d, aeb93e0888250bd3, b58eb3ca6ee46427). The t255 row in shaders/replace/README.md describes the new bits.

2. Normalized buffer codes give the nearest float

format_load divided UNORM/SNORM codes by 2^n - 1, and the host's division is not exact. On the RX 7900 XT, 8-bit code 3 loads one ulp high, and 16-bit code 257, multiplied back by 65535 and truncated, comes out as 256. Titles truncate x * (2^n - 1) back to the code (for example a material index in a vertex colour's alpha), so a quotient one ulp low becomes the code below.

  • Codes of up to 16 bits now go through norm_quotient / exact_norm, which give the float nearest the quotient.
  • The ends stay exactly 1.0 and -1.0.

Verification

  • cargo test --release --locked -p larp-shader: 6 passed (5 new), run on an RX 7900 XT and WARP.
    • CPU check: every code of widths 2–16 against the exact nearest float.
    • GPU checks: normalized ends, nearest quotients, codes truncating back to themselves, and d16 data of heap-picked UINT/SINT/FLOAT views.
  • cargo test --release --locked -p larp-gpu: 19 passed, 1 ignored (1 new: the marks in the table of strides).
  • With the old code put back, both new GPU checks fail on the 7900 XT:
    • 8-bit unsigned normalized 0x3 loads as 0x3c40c0c2, want 0x3c40c0c1
    • stored: 0xffffffff, want 0xbeef1234
  • cargo check --release --locked -p larp: no new warnings.

Limits

  • The d16 fix relies on the driver passing integer bits through the float handle the translator keeps for these views. AMD and WARP do; NVIDIA is untested.
  • A view picked from a table at a run-time index is still treated as a float view, as in the original fork.
**@⁠ambrosia-works** on GitHub · 2026-12-05 22:00 UTC · [original](https://github.com/gamingwithhomie/larp/pull/240) `ambrosia-works:fix/d16-integer-views-norm-codes` → `main` · +1088 −42 · 12 files · 2 commits · merged 2026-12-05 23:34 UTC ## Summary Two shader-translator correctness fixes, ported from the cool_fox fork (`9a6c829d6`, after VIctory `4243a96a` and `da82e444`). Every shader is translated anew: `lower.rs` and `shader.rs` are hashed into the translation cache key. ### 1. d16 data of integer typed views picked from the heap keeps its bits The facial animation network keeps its tensors in `R16G16B16A16_UINT` / `R16_UINT` typed buffer views and reads and writes them as d16 data. `halves.rs` already handles such views at fixed table places. For a view the program picks from the view heap at run time, the translator still took d16 data as f16 and converted it, which changes the integer bits. This is a candidate cause of the broken facial animation on AMD (only one eye blinks, mouths warp while talking). I have not verified that in-game yet. - The host now marks typed buffer views of UINT/SINT formats in the table of strides (`VIEW_INTEGER` = bit 31, `VIEW_SIGNED` = bit 30; `descriptor/host.rs` `view_kind`). - `lower.rs` `integer_view` reads the marks: - d16 stores zero- or sign-extend each 16-bit channel. - d16 loads keep the low 16 bits. - `halves.rs`'s unsigned-twin path is unchanged and still runs first. - Every reader of the stride masks the marks off with `VIEW_STRIDE_BITS`: `slot_stride`, and the four replacement shaders that read `t255` directly (`3592426e3e9142d8`, `a74a0746279f960d`, `aeb93e0888250bd3`, `b58eb3ca6ee46427`). The `t255` row in `shaders/replace/README.md` describes the new bits. ### 2. Normalized buffer codes give the nearest float `format_load` divided UNORM/SNORM codes by `2^n - 1`, and the host's division is not exact. On the RX 7900 XT, 8-bit code 3 loads one ulp high, and 16-bit code 257, multiplied back by 65535 and truncated, comes out as 256. Titles truncate `x * (2^n - 1)` back to the code (for example a material index in a vertex colour's alpha), so a quotient one ulp low becomes the code below. - Codes of up to 16 bits now go through `norm_quotient` / `exact_norm`, which give the float nearest the quotient. - The ends stay exactly 1.0 and -1.0. ## Verification - `cargo test --release --locked -p larp-shader`: 6 passed (5 new), run on an RX 7900 XT and WARP. - CPU check: every code of widths 2–16 against the exact nearest float. - GPU checks: normalized ends, nearest quotients, codes truncating back to themselves, and d16 data of heap-picked UINT/SINT/FLOAT views. - `cargo test --release --locked -p larp-gpu`: 19 passed, 1 ignored (1 new: the marks in the table of strides). - With the old code put back, both new GPU checks fail on the 7900 XT: - `8-bit unsigned normalized 0x3 loads as 0x3c40c0c2, want 0x3c40c0c1` - `stored: 0xffffffff, want 0xbeef1234` - `cargo check --release --locked -p larp`: no new warnings. ## Limits - The d16 fix relies on the driver passing integer bits through the float handle the translator keeps for these views. AMD and WARP do; NVIDIA is untested. - A view picked from a table at a run-time index is still treated as a float view, as in the original fork.
Ported from cool_fox 9a6c829d6 (VIctory 4243a96a, da82e444).

- d16 typed accesses through a view the program picks from the view heap:
  the host marks typed buffer views of UINT/SINT formats in the table of
  strides (VIEW_INTEGER, VIEW_SIGNED in bits 31/30, larp-gpu
  descriptor/host.rs view_kind), and lower.rs integer_view reads the marks:
  d16 stores zero-/sign-extend each 16-bit channel and d16 loads keep the
  low 16 bits, instead of converting halves (the face network keeps its
  tensors in R16G16B16A16_UINT / R16_UINT views). halves.rs's unsigned-twin
  path for fixed table places is unchanged and taken first. Every reader of
  the stride masks the marks off (VIEW_STRIDE_BITS): slot_stride, and the
  four replacement shaders that read t255 directly.
- format_load: UNORM/SNORM codes of up to 16 bits give the float nearest
  their quotient (norm_quotient, exact_norm) instead of the host's division,
  which the RX 7900 XT does one ulp off (8-bit code 3 one ulp high; 16-bit
  257 truncated back to 256); the ends stay exactly 1.0 and -1.0.
- Tests (emit/buffer_tests.rs): the quotient method against the exact
  nearest float for every code of widths 2-16 (CPU), and synthetic programs
  run on the host GPU and WARP: normalized ends, nearest quotients,
  truncation back to the code, d16 data of heap-picked UINT/SINT/FLOAT
  views; larp-gpu: the marks in the table of strides.

Every shader is translated anew (lower.rs and shader.rs are hashed).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-12-05 22:05:15 +00:00
# Conflicts:
#	crates/larp-gpu/src/descriptor/host.rs
Author
Owner

@⁠gamingwithhomie on GitHub · 2026-12-05 23:08 UTC · original

Queued for dedicated review (1,088 lines across shader/view handling - too large to fast-merge with the v1.1.4 batch). It touches heap-picked view semantics that interact with the AMD wide-heap work (#183/#227), so it will be reviewed together with those. Not lost - next review round.

**@⁠gamingwithhomie** on GitHub · 2026-12-05 23:08 UTC · [original](https://github.com/gamingwithhomie/larp/pull/240#issuecomment-7447341636) Queued for dedicated review (1,088 lines across shader/view handling - too large to fast-merge with the v1.1.4 batch). It touches heap-picked view semantics that interact with the AMD wide-heap work (#183/#227), so it will be reviewed together with those. Not lost - next review round.
github-bot changed target branch from main to gh-pr/240/base 2026-12-05 23:37:58 +00:00
github-bot referenced this pull request from a commit 2026-12-06 01:07:23 +00:00
github manually merged commit bbd6259fbe into gh-pr/240/base 2026-12-06 01:07:29 +00:00
Author
Owner

@⁠christopear on GitHub · 2026-12-06 02:45 UTC · original

NVIDIA follow-up on the d16 test

I reproduced a distinction between the stored bytes and the immediate raw read on upstream main 459db13. RTX 3090, NVIDIA 595.99.02, Proton Experimental, Windows MSVC release tests. The original PR called out missing NVIDIA coverage.

cargo test --release --locked -p larp-shader --lib d16_data_of_integer_views_picked_from_the_heap_keeps_its_bits -- --nocapture

I cross-built the test and ran the executable under Proton. The unchanged test failed on three fresh runs. The typed load cases passed. For each store, the raw load in the same dispatch returned zero. The two adapter labels resolve to the same NVIDIA device on this setup, so this is one hardware result.

I repeated two controls twice each:

  • Keep the original shader and assertion. Also copy bytes 64–71 from the target after its existing transition to COPY_SOURCE and inspect them after fence completion. Both stored words were correct every time, even as the original assertion failed. UINT and SINT gave beef1234 80017fff. FLOAT gave 40003c00 3555c200.
  • Starting from the unchanged test again, insert only body.push(0xbf8a0000); between body.extend(access) and body.extend(out) in the synthetic program. With this barrier the original assertion passed.

An earlier instrumented run on a custom build prefilled the destination with distinct markers. The immediate reads returned those previous markers. This points to visibility between the two views on this stack. It doesn't establish which layer is responsible or a gameplay regression.

I'd keep a separate assertion on the final stored bits so a visibility failure is distinguishable from conversion damage. A native Windows NVIDIA run of the unchanged test would help narrow this down. I would keep the barrier experiment confined to the test.

Diagnostic readback patch against 459db13, retaining the failing assertion
--- a/crates/larp-shader/src/emit/buffer_tests.rs
+++ b/crates/larp-shader/src/emit/buffer_tests.rs
@@ -412,7 +412,7 @@
                 .map_err(|e| e.to_string())?;
             let target = buffer(device, 256, D3D12_HEAP_TYPE_DEFAULT, D3D12_RESOURCE_STATE_UNORDERED_ACCESS);
             let zeros = buffer(device, 1 << 16, D3D12_HEAP_TYPE_DEFAULT, D3D12_RESOURCE_STATE_COMMON);
-            let back = buffer(device, 4 * n.max(1), D3D12_HEAP_TYPE_READBACK, D3D12_RESOURCE_STATE_COPY_DEST);
+            let back = buffer(device, 12 * n.max(1), D3D12_HEAP_TYPE_READBACK, D3D12_RESOURCE_STATE_COPY_DEST);
             let heap: ID3D12DescriptorHeap = device
                 .CreateDescriptorHeap(&D3D12_DESCRIPTOR_HEAP_DESC {
                     Type: D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV,
@@ -529,6 +529,9 @@
                     D3D12_RESOURCE_STATE_COPY_SOURCE,
                 )]);
                 list.CopyBufferRegion(&back, 4 * i as u64, &target, 0, 4);
+                if typed.is_some_and(|v| v.read.is_none()) {
+                    list.CopyBufferRegion(&back, 4 * n + 8 * i as u64, &target, 64, 8);
+                }
                 list.ResourceBarrier(&[transition(
                     &target,
                     D3D12_RESOURCE_STATE_COPY_SOURCE,
@@ -552,6 +555,13 @@
             let mut p = std::ptr::null_mut();
             back.Map(0, None, Some(&mut p)).map_err(|e| e.to_string())?;
             let out = std::slice::from_raw_parts(p.cast::<u32>(), cases.len()).to_vec();
+            if typed.is_some_and(|v| v.read.is_none()) {
+                let words = std::slice::from_raw_parts(p.cast::<u32>().add(cases.len()), 2 * cases.len());
+                for (got, case) in words.chunks_exact(2).zip(cases) {
+                    eprintln!("post-dispatch {got:x?}, immediate {out:x?}");
+                    assert_eq!(got, &case[..2]);
+                }
+            }
             back.Unmap(0, None);
             Ok(out)
         }
**@⁠christopear** on GitHub · 2026-12-06 02:45 UTC · [original](https://github.com/gamingwithhomie/larp/pull/240#issuecomment-7450185069) **NVIDIA follow-up on the d16 test** I reproduced a distinction between the stored bytes and the immediate raw read on upstream main `459db13`. RTX 3090, NVIDIA 595.99.02, Proton Experimental, Windows MSVC release tests. The original PR called out missing NVIDIA coverage. ```sh cargo test --release --locked -p larp-shader --lib d16_data_of_integer_views_picked_from_the_heap_keeps_its_bits -- --nocapture ``` I cross-built the test and ran the executable under Proton. The unchanged test failed on three fresh runs. The typed load cases passed. For each store, the raw load in the same dispatch returned zero. The two adapter labels resolve to the same NVIDIA device on this setup, so this is one hardware result. I repeated two controls twice each: * Keep the original shader and assertion. Also copy bytes 64–71 from the target after its existing transition to COPY_SOURCE and inspect them after fence completion. Both stored words were correct every time, even as the original assertion failed. UINT and SINT gave `beef1234 80017fff`. FLOAT gave `40003c00 3555c200`. * Starting from the unchanged test again, insert only `body.push(0xbf8a0000);` between `body.extend(access)` and `body.extend(out)` in the synthetic program. With this barrier the original assertion passed. An earlier instrumented run on a custom build prefilled the destination with distinct markers. The immediate reads returned those previous markers. This points to visibility between the two views on this stack. It doesn't establish which layer is responsible or a gameplay regression. I'd keep a separate assertion on the final stored bits so a visibility failure is distinguishable from conversion damage. A native Windows NVIDIA run of the unchanged test would help narrow this down. I would keep the barrier experiment confined to the test. <details> <summary>Diagnostic readback patch against 459db13, retaining the failing assertion</summary> ```diff --- a/crates/larp-shader/src/emit/buffer_tests.rs +++ b/crates/larp-shader/src/emit/buffer_tests.rs @@ -412,7 +412,7 @@ .map_err(|e| e.to_string())?; let target = buffer(device, 256, D3D12_HEAP_TYPE_DEFAULT, D3D12_RESOURCE_STATE_UNORDERED_ACCESS); let zeros = buffer(device, 1 << 16, D3D12_HEAP_TYPE_DEFAULT, D3D12_RESOURCE_STATE_COMMON); - let back = buffer(device, 4 * n.max(1), D3D12_HEAP_TYPE_READBACK, D3D12_RESOURCE_STATE_COPY_DEST); + let back = buffer(device, 12 * n.max(1), D3D12_HEAP_TYPE_READBACK, D3D12_RESOURCE_STATE_COPY_DEST); let heap: ID3D12DescriptorHeap = device .CreateDescriptorHeap(&D3D12_DESCRIPTOR_HEAP_DESC { Type: D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV, @@ -529,6 +529,9 @@ D3D12_RESOURCE_STATE_COPY_SOURCE, )]); list.CopyBufferRegion(&back, 4 * i as u64, &target, 0, 4); + if typed.is_some_and(|v| v.read.is_none()) { + list.CopyBufferRegion(&back, 4 * n + 8 * i as u64, &target, 64, 8); + } list.ResourceBarrier(&[transition( &target, D3D12_RESOURCE_STATE_COPY_SOURCE, @@ -552,6 +555,13 @@ let mut p = std::ptr::null_mut(); back.Map(0, None, Some(&mut p)).map_err(|e| e.to_string())?; let out = std::slice::from_raw_parts(p.cast::<u32>(), cases.len()).to_vec(); + if typed.is_some_and(|v| v.read.is_none()) { + let words = std::slice::from_raw_parts(p.cast::<u32>().add(cases.len()), 2 * cases.len()); + for (got, case) in words.chunks_exact(2).zip(cases) { + eprintln!("post-dispatch {got:x?}, immediate {out:x?}"); + assert_eq!(got, &case[..2]); + } + } back.Unmap(0, None); Ok(out) } ``` </details>
This discussion has been locked. Commenting is limited to contributors.
No description provided.