Shaders: D16 image reads of UNORM / SNORM texels are one ULP low for about half the codes #2006

Open
opened 2026-11-26 04:51:21 +00:00 by github-bot · 12 comments
Owner

@⁠upipibi on GitHub · 2026-11-25 02:48 UTC · original

What happens

image() in emit/lower.rs (around line 8292) packs every D16 channel with legacyF32ToF16 of the f32 the host view returned. The comment there says this rounds to nearest even, but on D3D12 legacyF32ToF16 rounds toward zero. I measured that on an RTX 3070 (616.56) and on WARP: 1 + 3·2⁻¹¹ gives 0x3c01, 70000 gives 0x7bff.

For float texels that's right: RDNA2 packs D16 reads of 32-bit float texels toward zero too (boykopovar/AnyPS5#22750).

For UNORM / SNORM texels it isn't. RDNA2 rounds code / (2^w − 1) once, to nearest even. The host has already rounded that value to f32, so truncating it again leaves about half of all codes one unit in the last place low. Exhaustively: 123 of 256 8-bit UNORM codes, and 31,740 of 65,536 16-bit ones.

The same pipeline in xcl-dev/xcl measured exactly that on the RTX 3070: 123 of 256 8_8_8_8_UNORM codes off by one ULP (code 9 gives 0x2884, where RDNA2 gives 0x2885). xcl-dev/xcl#2026 has the details. I haven't measured VIctory's image path itself, because the shader tests' Views are RGBA32F only.

Why it isn't a one-line fix

image() never reads the T#, so at translation time it doesn't know whether a D16 channel is UNORM, SNORM or float. The buffer path knows the format statically, and #1940 fixes that side.

Options

  1. Read T# word 1 at run time: format = word1 >> 20 & 0x1FF. Pick per channel between legacyF32ToF16 (float formats) and an exact single rounding (UNORM/SNORM). The exact rounding needs only integer ops: code / (2^w − 1) repeats its w-bit code in binary, so (code << 32) / (2^w − 1) gives the bits that decide the rounding, and there are no ties. #1940's norm_f16 already does this for buffers, with constant widths. Images would need the width from the format table at run time.
  2. Specialise per view format on the host, if pipelines are already keyed by something that knows the bound views.

With option 1, the cost is a few integer ops per D16 channel, paid only by programs that use D16 image reads.

AI-assisted (Claude Code).

**@⁠upipibi** on GitHub · 2026-11-25 02:48 UTC · [original](https://github.com/Marvisak/VIctory/issues/2006) ### What happens `image()` in `emit/lower.rs` (around line 8292) packs every D16 channel with `legacyF32ToF16` of the f32 the host view returned. The comment there says this rounds to nearest even, but on D3D12 `legacyF32ToF16` rounds **toward zero**. I measured that on an RTX 3070 (616.56) and on WARP: `1 + 3·2⁻¹¹` gives `0x3c01`, 70000 gives `0x7bff`. For **float** texels that's right: RDNA2 packs D16 reads of 32-bit float texels toward zero too (boykopovar/AnyPS5#22750). For **UNORM / SNORM** texels it isn't. RDNA2 rounds `code / (2^w − 1)` once, to nearest even. The host has already rounded that value to f32, so truncating it again leaves about half of all codes one unit in the last place low. Exhaustively: 123 of 256 8-bit UNORM codes, and 31,740 of 65,536 16-bit ones. The same pipeline in xcl-dev/xcl measured exactly that on the RTX 3070: 123 of 256 `8_8_8_8_UNORM` codes off by one ULP (code 9 gives `0x2884`, where RDNA2 gives `0x2885`). xcl-dev/xcl#2026 has the details. I haven't measured VIctory's image path itself, because the shader tests' `View`s are RGBA32F only. ### Why it isn't a one-line fix `image()` never reads the T#, so at translation time it doesn't know whether a D16 channel is UNORM, SNORM or float. The buffer path knows the format statically, and #1940 fixes that side. ### Options 1. **Read T# word 1 at run time:** format = `word1 >> 20 & 0x1FF`. Pick per channel between `legacyF32ToF16` (float formats) and an exact single rounding (UNORM/SNORM). The exact rounding needs only integer ops: `code / (2^w − 1)` repeats its `w`-bit code in binary, so `(code << 32) / (2^w − 1)` gives the bits that decide the rounding, and there are no ties. #1940's `norm_f16` already does this for buffers, with constant widths. Images would need the width from the format table at run time. 2. **Specialise per view format** on the host, if pipelines are already keyed by something that knows the bound views. With option 1, the cost is a few integer ops per D16 channel, paid only by programs that use D16 image reads. AI-assisted (Claude Code).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:51:21 +00:00
Author
Owner

@⁠BaggyALPHA on GitHub · 2026-11-25 03:02 UTC · original

Confirmed on VIctory itself at upstream 940d32f, on RTX 3080 (driver 621.24) and WARP. A local synthetic test lowered a D16 image_load, ran it against real typed RGBA8 texture SRVs, and read back both packed channels. I tested all 256 codes per format and repeated the sweep three times. DXC 1.8 validated the translated program.

Texture format RTX 3080 mismatches vs nearest-even WARP mismatches vs nearest-even Example, actual → expected
RGBA8 UNORM 123 / 256 123 / 256 code 9, 0x2884 → 0x2885
RGBA8 SNORM 110 / 256 110 / 256 code +17, 0x3048 → 0x3049

Both backends matched the round-toward-zero model for every tested code. Positive and negative RGBA32F controls on the same translated image load also matched round-toward-zero, so a blanket rounding-mode change would alter that path. The surrounding shader test group passed 6/6 with the local probe included.

The probe adapts the synthetic image-load test pattern in #1437 and adds typed texture inputs. The coordination claim covered reproduction. The format-aware fix remains open. All inputs were synthetic. No game files were used.

AI-assisted (OpenAI Codex).

**@⁠BaggyALPHA** on GitHub · 2026-11-25 03:02 UTC · [original](https://github.com/Marvisak/VIctory/issues/2006#issuecomment-7151529475) Confirmed on VIctory itself at upstream `940d32f`, on RTX 3080 (driver 621.24) and WARP. A local synthetic test lowered a D16 `image_load`, ran it against real typed RGBA8 texture SRVs, and read back both packed channels. I tested all 256 codes per format and repeated the sweep three times. DXC 1.8 validated the translated program. | Texture format | RTX 3080 mismatches vs nearest-even | WARP mismatches vs nearest-even | Example, actual → expected | | --- | ---: | ---: | --- | | RGBA8 UNORM | 123 / 256 | 123 / 256 | code 9, `0x2884` → `0x2885` | | RGBA8 SNORM | 110 / 256 | 110 / 256 | code +17, `0x3048` → `0x3049` | Both backends matched the round-toward-zero model for every tested code. Positive and negative RGBA32F controls on the same translated image load also matched round-toward-zero, so a blanket rounding-mode change would alter that path. The surrounding shader test group passed 6/6 with the local probe included. The probe adapts the synthetic image-load test pattern in #1437 and adds typed texture inputs. The [coordination claim](https://git.gtahub.st/github/Marvisak--VIctory/issues/1633) covered reproduction. The format-aware fix remains open. All inputs were synthetic. No game files were used. AI-assisted (OpenAI Codex).
Author
Owner

@⁠DenRakEiw on GitHub · 2026-11-25 03:28 UTC · original

Two implementation notes from checking origin/main (940d32f) before attempting a fix:

  1. T# word 1 is not available in GPU memory for Option 1: In crates/victory-shader/src/emit/lower.rs, s_load_dwordx8 from descriptor tables is virtualized to a PC ResourceDescriptorHeap index (descriptor_data only reconstructs dimensions via dx.op.getDimensions, and explicitly rejects "a descriptor's address or format read as data"). So Option 1 would require victory-gpu to mirror a sideband format/rounding-mode bit per view-heap slot (or specialize pipelines as in Option 2).

  2. Once the float vs. norm bit is known, lower.rs already has the RNE converter and almost no double-rounding occurs: Lower::f32_to_f16(..., rtz: false) already implements IEEE-754 binary16 RNE in integer ops. Exhaustive check of f64_to_f16_rne(f32_round(code / (2^w - 1))) vs. exact f64_to_f16_rne(code / (2^w - 1)):

    • 4 / 5 / 6 / 8 / 10-bit UNORM & SNORM: 0 mismatches (100% exact, fixing all 123/256 8-bit UNORM codes)
    • 16-bit UNORM: 2 / 65,536 double-rounding mismatches (vs. 31,740 / 65,536 with RTZ)
    • 16-bit SNORM: 2 / 65,535 double-rounding mismatches

    And because it operates on the returned f32, it also works uniformly for filtered image_sample as well as image_load, without needing a per-format bit-width lookup table.

AI-assisted (Gemini 4 Neon).

**@⁠DenRakEiw** on GitHub · 2026-11-25 03:28 UTC · [original](https://github.com/Marvisak/VIctory/issues/2006#issuecomment-7151940110) Two implementation notes from checking `origin/main` (`940d32f`) before attempting a fix: 1. **T# word 1 is not available in GPU memory for Option 1:** In `crates/victory-shader/src/emit/lower.rs`, `s_load_dwordx8` from descriptor tables is virtualized to a PC `ResourceDescriptorHeap` index (`descriptor_data` only reconstructs dimensions via `dx.op.getDimensions`, and explicitly rejects `"a descriptor's address or format read as data"`). So Option 1 would require `victory-gpu` to mirror a sideband format/rounding-mode bit per view-heap slot (or specialize pipelines as in Option 2). 2. **Once the float vs. norm bit is known, `lower.rs` already has the RNE converter and almost no double-rounding occurs:** `Lower::f32_to_f16(..., rtz: false)` already implements IEEE-754 binary16 RNE in integer ops. Exhaustive check of `f64_to_f16_rne(f32_round(code / (2^w - 1)))` vs. exact `f64_to_f16_rne(code / (2^w - 1))`: - 4 / 5 / 6 / 8 / 10-bit UNORM & SNORM: **0 mismatches** (100% exact, fixing all 123/256 8-bit UNORM codes) - 16-bit UNORM: **2 / 65,536** double-rounding mismatches (vs. 31,740 / 65,536 with RTZ) - 16-bit SNORM: **2 / 65,535** double-rounding mismatches And because it operates on the returned `f32`, it also works uniformly for filtered `image_sample` as well as `image_load`, without needing a per-format bit-width lookup table. AI-assisted (Gemini 4 Neon).
Author
Owner

@⁠Ingenioeren on GitHub · 2026-11-25 03:35 UTC · original

I checked the 16-bit cases using real R16_UNORM and R16_SNORM texture loads on an RX 6750 XT (driver 32.0.21045.29003) and WARP on Windows 11 build 26200. I swept all 65,536 stored codes per format. The probe used the D3D12 readback helper from 940d32f and native SM 6.6 HLSL. DXC emitted the same legacyF32ToF16 conversion used by VIctory's D16 image lowering. D16 conversion in the current source

Format Legacy conversion differed from direct nearest-even Nearest-even on the returned f32 differed from direct nearest-even
R16_UNORM 31,740 / 65,536 2 / 65,536 (codes 65455 and 65519)
R16_SNORM 30,716 / 65,536 2 / 65,536 (codes ±32759)

Those counts matched on both backends. WARP returned 1,536 SNORM f32 values one f32 ULP away from the correctly rounded ratio. That difference did not change which codes disagreed in the final half result.

For an unfiltered Load, rounding the returned f32 multiplied by the format scale (65535 or 32767) back to an integer recovered the original normalized code on both backends, including the clamped SNORM minimum. That gives a way to get exact half rounding if the load's 16-bit format is already known. I did not test filtered samples or change the translator.

AI-assisted. One synthetic test passed on both backends. No game files used.

**@⁠Ingenioeren** on GitHub · 2026-11-25 03:35 UTC · [original](https://github.com/Marvisak/VIctory/issues/2006#issuecomment-7152051782) I checked the 16-bit cases using real `R16_UNORM` and `R16_SNORM` texture loads on an RX 6750 XT (driver `32.0.21045.29003`) and WARP on Windows 11 build 26200. I swept all 65,536 stored codes per format. The probe used the D3D12 readback helper from `940d32f` and native SM 6.6 HLSL. DXC emitted the same `legacyF32ToF16` conversion used by VIctory's D16 image lowering. [D16 conversion in the current source](https://github.com/Marvisak/VIctory/blob/940d32f69b22265690a0b5a15d6ded572ba33baf/crates/victory-shader/src/emit/lower.rs#L8290-L8299) | Format | Legacy conversion differed from direct nearest-even | Nearest-even on the returned f32 differed from direct nearest-even | | --- | ---: | ---: | | R16_UNORM | 31,740 / 65,536 | 2 / 65,536 (codes 65455 and 65519) | | R16_SNORM | 30,716 / 65,536 | 2 / 65,536 (codes ±32759) | Those counts matched on both backends. WARP returned 1,536 SNORM f32 values one f32 ULP away from the correctly rounded ratio. That difference did not change which codes disagreed in the final half result. For an unfiltered `Load`, rounding the returned f32 multiplied by the format scale (65535 or 32767) back to an integer recovered the original normalized code on both backends, including the clamped SNORM minimum. That gives a way to get exact half rounding if the load's 16-bit format is already known. I did not test filtered samples or change the translator. _AI-assisted. One synthetic test passed on both backends. No game files used._
Author
Owner

@⁠astrosteveo on GitHub · 2026-11-25 04:20 UTC · original

The same format problem reaches typed buffer loads: buffer_load_format_d16_* converts with legacyF32ToF16 without knowing the view's format. On an RTX 5080 under vkd3d-proton, RGBA32F data is rounded to nearest (1 + 3/2048 gives 0x3c02, 70000 gives infinity), where #1940 says RDNA2 truncates. I did not test normalized typed views or Windows. #2030's per-view tag covers textures only. AI-assisted (Claude).

**@⁠astrosteveo** on GitHub · 2026-11-25 04:20 UTC · [original](https://github.com/Marvisak/VIctory/issues/2006#issuecomment-7152748633) The same format problem reaches typed buffer loads: `buffer_load_format_d16_*` converts with `legacyF32ToF16` without knowing the view's format. On an RTX 5080 under vkd3d-proton, RGBA32F data is rounded to nearest (1 + 3/2048 gives `0x3c02`, 70000 gives infinity), where #1940 says RDNA2 truncates. I did not test normalized typed views or Windows. #2030's per-view tag covers textures only. AI-assisted (Claude).
Author
Owner

@⁠upipibi on GitHub · 2026-11-25 04:21 UTC · original

Thanks, that's an important qualifier. My "legacyF32ToF16 rounds toward zero" was measured on native D3D12 (RTX 3070 616.56 and WARP). Your vkd3d-proton result shows dxil-spirv lowers it with round-to-nearest-even, so the float path matches RDNA2 only on native D3D12.

A fix that holds on every host would not depend on the host's conversion in either case:

  • float channels: truncate explicitly in integer ops: f32 bits → sign, exponent, top 10 mantissa bits, with the denormal and overflow cases giving 0x7BFF instead of infinity, as on RDNA2.
  • UNORM / SNORM: the exact single rounding from option 1, which is already integer-only.

(#2030 with the per-view tag is xcl-dev/xcl#2030, not a VIctory PR. xcl-dev/xcl has the same dependency on the host conversion, and I'll raise it there separately.)

AI-assisted (Claude Code).

**@⁠upipibi** on GitHub · 2026-11-25 04:21 UTC · [original](https://github.com/Marvisak/VIctory/issues/2006#issuecomment-7152765942) Thanks, that's an important qualifier. My "`legacyF32ToF16` rounds toward zero" was measured on native D3D12 (RTX 3070 616.56 and WARP). Your vkd3d-proton result shows dxil-spirv lowers it with round-to-nearest-even, so the float path matches RDNA2 only on native D3D12. A fix that holds on every host would not depend on the host's conversion in either case: - **float channels:** truncate explicitly in integer ops: f32 bits → sign, exponent, top 10 mantissa bits, with the denormal and overflow cases giving `0x7BFF` instead of infinity, as on RDNA2. - **UNORM / SNORM:** the exact single rounding from option 1, which is already integer-only. (`#2030` with the per-view tag is xcl-dev/xcl#2030, not a VIctory PR. xcl-dev/xcl has the same dependency on the host conversion, and I'll raise it there separately.) AI-assisted (Claude Code).
Author
Owner

@⁠YaBulochka on GitHub · 2026-11-25 04:58 UTC · original

RADV/RDNA3 data point, measured on the GPU: imageLoad of UNORM texels is exact here, so the driver does not merge the d16 loads the way the Windows path was said to.

Setup. Linux, Vulkan 1.4, RADV, Radeon RX 7600 (NAVI33, gfx1102). Minimal compute test: an R8_UNORM 256x1 image and an R16_UNORM 256x256 image filled with every code through a staging copy; a compute shader does imageLoad(...).r for each texel and writes the float into an SSBO; the host compares bit-for-bit against (float)((double)code / (2^n - 1)) (nearest-even f32).

Result:

R8_UNORM : 0/256 differ from round(code/255),   max diff 0
R16_UNORM: 0/65536 differ from round(code/65535), max diff 0

So on RADV the value arriving at the shader is already the correctly rounded f32 - there is no second truncation to hide behind. That means the legacyF32ToF16 step in emit/lower.rs (round toward zero) is the only thing that loses the half-ULP here, and the fix is needed for Linux/RADV as well, not only for the hosts that were measured.

Caveat, stated plainly: this is the Vulkan imageLoad conversion, not D3D12's typed load through the host view; if the runtime builds the d16 read differently (a raw view plus a pack in the shader), the driver path I measured is not the one being used and the numbers above do not apply to that path.

I can extend the same harness to R16_SNORM and to a texelFetch-of-R32G32_FLOAT comparison if that helps settle it; the whole thing is a ~200-line Vulkan compute program and needs no title run.

**@⁠YaBulochka** on GitHub · 2026-11-25 04:58 UTC · [original](https://github.com/Marvisak/VIctory/issues/2006#issuecomment-7153333427) RADV/RDNA3 data point, measured on the GPU: `imageLoad` of UNORM texels is exact here, so the driver does **not** merge the d16 loads the way the Windows path was said to. **Setup.** Linux, Vulkan 1.4, RADV, **Radeon RX 7600 (NAVI33, gfx1102)**. Minimal compute test: an `R8_UNORM` 256x1 image and an `R16_UNORM` 256x256 image filled with every code through a staging copy; a compute shader does `imageLoad(...).r` for each texel and writes the float into an SSBO; the host compares bit-for-bit against `(float)((double)code / (2^n - 1))` (nearest-even f32). **Result:** ``` R8_UNORM : 0/256 differ from round(code/255), max diff 0 R16_UNORM: 0/65536 differ from round(code/65535), max diff 0 ``` So on RADV the value arriving at the shader is already the correctly rounded f32 - there is no second truncation to hide behind. That means the `legacyF32ToF16` step in `emit/lower.rs` (round toward zero) is the only thing that loses the half-ULP here, and the fix is needed for Linux/RADV as well, not only for the hosts that were measured. Caveat, stated plainly: this is the Vulkan `imageLoad` conversion, not D3D12's typed load through the host view; if the runtime builds the d16 read differently (a raw view plus a pack in the shader), the driver path I measured is not the one being used and the numbers above do not apply to that path. I can extend the same harness to `R16_SNORM` and to a `texelFetch`-of-`R32G32_FLOAT` comparison if that helps settle it; the whole thing is a ~200-line Vulkan compute program and needs no title run.
Author
Owner

@⁠upipibi on GitHub · 2026-11-25 05:06 UTC · original

Thanks, that matches the premise here: the f32 the shader receives is correctly rounded on every host measured so far. The half ULP is lost only when that f32 is truncated to f16.

On Linux, though, VIctory reaches the GPU through vkd3d-proton, and there legacyF32ToF16 rounds to nearest (astrosteveo's measurement above). So under RADV / vkd3d-proton the 123-of-256 truncation error shouldn't appear. What remains on that path is double rounding (f32, then f16). That's wrong for only a handful of 16-bit codes: boykopovar/AnyPS5 counted 3 to 4 for their packHalf2x16 path. The integer-only rounding in option 1 avoids both, which is why I'd still do it on every host.

If you extend the harness, the most useful run would be through vkd3d-proton's D3D12 path: a D16 image_load from VIctory itself on R16_UNORM / R16_SNORM. That's the path this issue is about.

AI-assisted (Claude Code).

**@⁠upipibi** on GitHub · 2026-11-25 05:06 UTC · [original](https://github.com/Marvisak/VIctory/issues/2006#issuecomment-7153462577) Thanks, that matches the premise here: the f32 the shader receives is correctly rounded on every host measured so far. The half ULP is lost only when that f32 is truncated to f16. On Linux, though, VIctory reaches the GPU through vkd3d-proton, and there `legacyF32ToF16` rounds to nearest (astrosteveo's measurement above). So under RADV / vkd3d-proton the 123-of-256 truncation error shouldn't appear. What remains on that path is double rounding (f32, then f16). That's wrong for only a handful of 16-bit codes: boykopovar/AnyPS5 counted 3 to 4 for their packHalf2x16 path. The integer-only rounding in option 1 avoids both, which is why I'd still do it on every host. If you extend the harness, the most useful run would be through vkd3d-proton's D3D12 path: a D16 `image_load` from VIctory itself on R16_UNORM / R16_SNORM. That's the path this issue is about. AI-assisted (Claude Code).
Author
Owner

@⁠upipibi on GitHub · 2026-11-25 05:20 UTC · original

Thanks, DenRakEiw. That settles the shape of the fix. Putting the thread together:

  1. The shader can't read the format. Descriptor-table T# loads are virtualised, so option 1 as written doesn't fit. What it needs is one bit per view-heap slot, mirrored by victory-gpu when the view is made: is the format UNORM / SNORM, or float?
  2. UNORM / SNORM: Lower::f32_to_f16(.., rtz: false) on the returned f32. By your count that's exact up to 10 bits and leaves 2 codes of 65,536 at 16 bits. It also covers filtered image_sample.
  3. Float texels: f32_to_f16(.., rtz: true), because RDNA2 truncates those (boykopovar/AnyPS5#22750).
  4. Use VIctory's own integer converter in both cases, not legacyF32ToF16. The host op truncates on native D3D12 but rounds to nearest under vkd3d-proton (astrosteveo above), so it can't be right for both kinds of texel on every host.

The 2 remaining 16-bit codes would need the exact integer rounding from the code, so they can stay a known limit unless a title shows them.

AI-assisted (Claude Code).

**@⁠upipibi** on GitHub · 2026-11-25 05:20 UTC · [original](https://github.com/Marvisak/VIctory/issues/2006#issuecomment-7153673915) Thanks, DenRakEiw. That settles the shape of the fix. Putting the thread together: 1. **The shader can't read the format.** Descriptor-table T# loads are virtualised, so option 1 as written doesn't fit. What it needs is one bit per view-heap slot, mirrored by `victory-gpu` when the view is made: is the format UNORM / SNORM, or float? 2. **UNORM / SNORM:** `Lower::f32_to_f16(.., rtz: false)` on the returned f32. By your count that's exact up to 10 bits and leaves 2 codes of 65,536 at 16 bits. It also covers filtered `image_sample`. 3. **Float texels:** `f32_to_f16(.., rtz: true)`, because RDNA2 truncates those (boykopovar/AnyPS5#22750). 4. **Use VIctory's own integer converter in both cases, not `legacyF32ToF16`.** The host op truncates on native D3D12 but rounds to nearest under vkd3d-proton (astrosteveo above), so it can't be right for both kinds of texel on every host. The 2 remaining 16-bit codes would need the exact integer rounding from the code, so they can stay a known limit unless a title shows them. AI-assisted (Claude Code).
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 12:49 UTC · original · edited 2026-11-26 06:48 UTC

Automated triage for the maintainers.

Confirmed in main (63bcca4). The D16 path of the image lowering in crates/victory-shader/src/emit/lower.rs (Lower::image) converts every channel with legacyF32ToF16, with no knowledge of the bound texture's format; its shortcut: comment says the result is rounded to nearest even, which the native D3D12 measurements above (RTX 3070, RTX 3080, WARP: 123/256 RGBA8 UNORM and 110/256 SNORM codes one ULP low) contradict. The host op also differs between native D3D12 (toward zero) and vkd3d-proton (to nearest), so the result depends on the host.

Open PRs: #2030 ("round D16 image reads according to the bound texture format") and #2125, which contains #2030 plus the #2087 integer fix. New since the last triage: a comparison on an RTX 4070 and WARP found 0 mismatches for every 8- and 16-bit UNORM/SNORM code with either, and #2030's test fails with main's conversion put back; both conflict with main and need a rebase. #2064's test also passes on main, so it does not address this. The typed-buffer D16 path is #1940's side.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 12:49 UTC · [original](https://github.com/Marvisak/VIctory/issues/2006#issuecomment-7162907066) · edited 2026-11-26 06:48 UTC <!-- victory-issue-triage v1 verdict=confirmed seen=2026-11-26T04:12:35Z at=2026-11-26T07:20:00Z --> Automated triage for the maintainers. Confirmed in `main` (`63bcca4`). The D16 path of the image lowering in `crates/victory-shader/src/emit/lower.rs` (`Lower::image`) converts every channel with `legacyF32ToF16`, with no knowledge of the bound texture's format; its `shortcut:` comment says the result is rounded to nearest even, which the native D3D12 measurements above (RTX 3070, RTX 3080, WARP: 123/256 RGBA8 UNORM and 110/256 SNORM codes one ULP low) contradict. The host op also differs between native D3D12 (toward zero) and vkd3d-proton (to nearest), so the result depends on the host. Open PRs: #2030 ("round D16 image reads according to the bound texture format") and #2125, which contains #2030 plus the #2087 integer fix. **New since the last triage:** a comparison on an RTX 4070 and WARP found 0 mismatches for every 8- and 16-bit UNORM/SNORM code with either, and #2030's test fails with `main`'s conversion put back; both conflict with `main` and need a rebase. #2064's test also passes on `main`, so it does not address this. The typed-buffer D16 path is #1940's side. --- _Generated by [Claude Code](https://claude.ai/code)_
Author
Owner

@⁠pawrenity on GitHub · 2026-11-26 04:12 UTC · original

Comparative verification: #2030, #2125 and #2064

I checked the three open PRs on NVIDIA GeForce RTX 4070, Windows 11, plus WARP. All three branch from 940d32f. Current main is 63bcca4.

Independent reference. A standalone Python script computed the exactly rounded (nearest-even) f16 of code / (2^w − 1) for every 8- and 16-bit UNORM and SNORM code, using exact rationals, with SNORM −2^(w−1) clamped to −1. Against that table, truncating the correctly rounded f32 (what legacyF32ToF16 does on native D3D12) is wrong for 123/256 UNORM8, 110/256 SNORM8, 31,740/65,536 UNORM16 and 30,716/65,536 SNORM16 codes. Rounding that f32 to nearest even leaves 2/65,536 UNORM16 codes wrong. All five numbers match the issue thread.

To compare the GPU output with this table, I patched each PR's d16_image_load_rounds_normalized_texels_to_nearest locally so it dumps its readback. That checks the R channel and the G channel (the reversed code) of every texel.

PR Head Scope Own tests GPU output vs exact table (RTX 4070 / WARP) Fails without the fix Cross-tests Clippy -D warnings Merges onto main Defects
#2030 e87c4f1 this issue (D16 image reads: normalized, float) 3 D16 tests pass. Full victory-shader + victory-gpu runs fail 5 tests, and the same 5 fail alone on 940d32f. 0 mismatches for UNORM8, SNORM8, UNORM16 and SNORM16, on both backends yes, with the PR's test infrastructure and main's per-channel legacyF32ToF16 put back: 123/256, 110/256, 31,740/65,536 and 30,716/65,536 codes wrong per channel on both backends, exactly the truncation model. The point/linear sample test still passed with that conversion, because it guards against double rounding (nearest of the f32), not against main's truncation. #2125's integer test fails here (all 32 texels in each of 6 UINT/SINT formats), which is expected: that is #2087. #2064's test passes. clean (victory-shader, victory-gpu) conflicts in descriptor/host.rs, lower.rs, shader.rs, stride_tests.rs, synthetic_tests.rs none blocking; see notes
#2125 3fdf5e9 #2087 (D16 integer textures). Contains all four #2030 commits. 4 D16 tests pass. Full runs fail the same 5 tests as 940d32f. 0 mismatches, as with #2030 inherits #2030's result #2064's test passes clean conflicts (same five files) none found
#2064 f7870d3 loosely related: D16 typed buffer loads of 32-bit floats (d16_channels), not image reads its test passes n/a: does not touch the image path no, not on Windows. Its test also passes on main, 940d32f, #2030 and #2125, because native D3D12's legacyF32ToF16 already truncates. The PR reports the failure only under vkd3d-proton, which I did not test. — clean (victory-shader) conflicts in lower.rs none found

The hardware backend in these tests was confirmed as the RTX 4070. I printed the adapter name in a local patch.

Notes on #2030 (non-blocking, from reading the code at e87c4f1):

  • The exact 16-bit path covers loads, gathers, and samples whose sampler is a static point sampler (crates/victory-shader/src/emit/lower.rs:8076, Source::Constant). A point sampler loaded from the heap or picked by a Source::Phi takes nearest-of-f32 rounding. Per the reference, that path is wrong for 2/65,536 16-bit codes. I did not exercise it on the GPU.
  • texture_rounding (crates/victory-shader/src/emit/host.rs:21-59) tags the _SRGB formats as normalized. What RDNA2 does for D16 reads of sRGB views has not been measured in this thread. A4B4G4R4_UNORM is not in the list. The follow-up commits offered in the PR thread, which cover these points, are not on the head.

Recommendation. #2030 is the fix for this issue. Its output matched the exact reference for all 8- and 16-bit UNORM and SNORM codes on the RTX 4070 and on WARP, and its test catches main's conversion. It needs a rebase onto main (five conflicting files) before it can land. #2125 is #2030 plus a separate #2087 change. If #2125 lands, #2030 is included, so the two need one decision, not two. #2064 does not fix this issue. On Windows its test does not tell the old and new code apart, so its value depends on the vkd3d-proton result its author reported.

Synthetic tests only. No title run.

AI-assisted (Claude Code).

**@⁠pawrenity** on GitHub · 2026-11-26 04:12 UTC · [original](https://github.com/Marvisak/VIctory/issues/2006#issuecomment-7182392010) ### Comparative verification: #2030, #2125 and #2064 I checked the three open PRs on NVIDIA GeForce RTX 4070, Windows 11, plus WARP. All three branch from `940d32f`. Current `main` is `63bcca4`. **Independent reference.** A standalone Python script computed the exactly rounded (nearest-even) f16 of `code / (2^w − 1)` for every 8- and 16-bit UNORM and SNORM code, using exact rationals, with SNORM −2^(w−1) clamped to −1. Against that table, truncating the correctly rounded f32 (what `legacyF32ToF16` does on native D3D12) is wrong for 123/256 UNORM8, 110/256 SNORM8, 31,740/65,536 UNORM16 and 30,716/65,536 SNORM16 codes. Rounding that f32 to nearest even leaves 2/65,536 UNORM16 codes wrong. All five numbers match the issue thread. To compare the GPU output with this table, I patched each PR's `d16_image_load_rounds_normalized_texels_to_nearest` locally so it dumps its readback. That checks the R channel and the G channel (the reversed code) of every texel. | PR | Head | Scope | Own tests | GPU output vs exact table (RTX 4070 / WARP) | Fails without the fix | Cross-tests | Clippy `-D warnings` | Merges onto `main` | Defects | | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | | #2030 | `e87c4f1` | this issue (D16 image reads: normalized, float) | 3 D16 tests pass. Full `victory-shader` + `victory-gpu` runs fail 5 tests, and the same 5 fail alone on `940d32f`. | 0 mismatches for UNORM8, SNORM8, UNORM16 and SNORM16, on both backends | yes, with the PR's test infrastructure and `main`'s per-channel `legacyF32ToF16` put back: 123/256, 110/256, 31,740/65,536 and 30,716/65,536 codes wrong per channel on both backends, exactly the truncation model. The point/linear sample test still passed with that conversion, because it guards against double rounding (nearest of the f32), not against `main`'s truncation. | #2125's integer test fails here (all 32 texels in each of 6 UINT/SINT formats), which is expected: that is #2087. #2064's test passes. | clean (`victory-shader`, `victory-gpu`) | **conflicts** in `descriptor/host.rs`, `lower.rs`, `shader.rs`, `stride_tests.rs`, `synthetic_tests.rs` | none blocking; see notes | | #2125 | `3fdf5e9` | #2087 (D16 integer textures). Contains all four #2030 commits. | 4 D16 tests pass. Full runs fail the same 5 tests as `940d32f`. | 0 mismatches, as with #2030 | inherits #2030's result | #2064's test passes | clean | **conflicts** (same five files) | none found | | #2064 | `f7870d3` | loosely related: D16 **typed buffer** loads of 32-bit floats (`d16_channels`), not image reads | its test passes | n/a: does not touch the image path | **no, not on Windows.** Its test also passes on `main`, `940d32f`, #2030 and #2125, because native D3D12's `legacyF32ToF16` already truncates. The PR reports the failure only under vkd3d-proton, which I did not test. | — | clean (`victory-shader`) | **conflicts** in `lower.rs` | none found | The `hardware` backend in these tests was confirmed as the RTX 4070. I printed the adapter name in a local patch. **Notes on #2030 (non-blocking, from reading the code at `e87c4f1`):** - The exact 16-bit path covers loads, gathers, and samples whose sampler is a static point sampler (`crates/victory-shader/src/emit/lower.rs:8076`, `Source::Constant`). A point sampler loaded from the heap or picked by a `Source::Phi` takes nearest-of-f32 rounding. Per the reference, that path is wrong for 2/65,536 16-bit codes. I did not exercise it on the GPU. - `texture_rounding` (`crates/victory-shader/src/emit/host.rs:21-59`) tags the `_SRGB` formats as normalized. What RDNA2 does for D16 reads of sRGB views has not been measured in this thread. `A4B4G4R4_UNORM` is not in the list. The follow-up commits offered in the PR thread, which cover these points, are not on the head. **Recommendation.** #2030 is the fix for this issue. Its output matched the exact reference for all 8- and 16-bit UNORM and SNORM codes on the RTX 4070 and on WARP, and its test catches `main`'s conversion. It needs a rebase onto `main` (five conflicting files) before it can land. #2125 is #2030 plus a separate #2087 change. If #2125 lands, #2030 is included, so the two need one decision, not two. #2064 does not fix this issue. On Windows its test does not tell the old and new code apart, so its value depends on the vkd3d-proton result its author reported. Synthetic tests only. No title run. AI-assisted (Claude Code).
Author
Owner

@⁠passerbyyhx on GitHub · 2026-11-26 07:23 UTC · original

I measured the runtime sampler-heap case left open in the comparative verification. On #2030 at e87c4f1, the RTX 4090 Laptop and WARP gave identical point results on RGBA16_UNORM: stored 0xffaf returned D16 0x3bfe (single-round exact fraction 0x3bfd), and stored 0xffef returned 0x3c00 (reference 0x3bff).

A linear sample between 0xff6f and 0xffef returned f32 0x3f7fb000 on both devices, identical to the point read of 0xffaf. Here rounding the paired filtered f32 gives D16 0x3bfe, which matched the measured linear result. The same returned f32 bits thus need different treatment for these two references. Switching the same physical sampler slot back to point restored the original values.

The two exact point expectations above are calculated from code / 65535; the linear reference uses each device’s f32 read from the same dispatch. This measurement used synthetic texels. Six-case matrix and repeat command · Opt-in fixture PR

**@⁠passerbyyhx** on GitHub · 2026-11-26 07:23 UTC · [original](https://github.com/Marvisak/VIctory/issues/2006#issuecomment-7185401002) I measured the runtime sampler-heap case left open in the [comparative verification](https://git.gtahub.st/github/Marvisak--VIctory/issues/2006). On #2030 at `e87c4f1`, the RTX 4090 Laptop and WARP gave identical point results on RGBA16_UNORM: stored `0xffaf` returned D16 `0x3bfe` (single-round exact fraction `0x3bfd`), and stored `0xffef` returned `0x3c00` (reference `0x3bff`). A linear sample between `0xff6f` and `0xffef` returned f32 `0x3f7fb000` on both devices, identical to the point read of `0xffaf`. Here rounding the paired filtered f32 gives D16 `0x3bfe`, which matched the measured linear result. The same returned f32 bits thus need different treatment for these two references. Switching the same physical sampler slot back to point restored the original values. The two exact point expectations above are calculated from `code / 65535`; the linear reference uses each device’s f32 read from the same dispatch. This measurement used synthetic texels. [Six-case matrix and repeat command](https://github.com/passerbyyhx/VIctory/blob/a310863e4c8cf965c4b8c6a9ecbb9f4e96e8684f/docs/d16-runtime-sampler-measurement.md) · [Opt-in fixture PR](https://github.com/AlanQiang0418/VIctory/pull/2)
Author
Owner

@⁠Lyrnc on GitHub · 2026-11-26 18:52 UTC · original

RDNA2 hardware values for the open questions here, measured on gfx1036. The hardware rounds UNORM / SNORM data to f16 exactly once. That includes the four 16-bit codes where rounding the f32 to nearest gives a different half. Float texels are truncated. Image and buffer d16 loads agree on every value.

Setup.

  • Hardware: the Radeon iGPU of a Ryzen 7 9800X3D, gfx1036, AMD driver 32.0.21042.62, Windows 11. A HIP kernel (assembled by the driver's amd_comgr) runs, per lane over its own bytes:
    • tbuffer_load_format_d16_xyzw (format in the instruction);
    • buffer_load_format_d16_xyzw (format in the V#);
    • image_load … dmask:0xf unorm d16 through a 1D linear T# over the same memory;
    • the same loads without d16, for the f32 each one returns.
  • Inputs: every code. 256 per 8-bit format, 65,536 per 16-bit format, 1,024 per 10-bit field. For 32-bit floats, 376 values: f16 ties, values just above and below a tie at every exponent around the f16 range, overflow, f16 denormals, NaNs.
  • Translator: the same tbuffer_load_format_d16_xyzw words through build_valu_cs (the buffer path, d16_channels). They ran on that Radeon and on an RTX 5080 (driver 610.88), one adapter at a time with a local switch.

PC RDNA2 silicon, not the console's GPU. No game data.

1. UNORM / SNORM: one exact rounding, to nearest even. tbuffer, buffer and image d16 loads return the same half for every code. Each half is code / (2^n - 1) rounded once:

format codes matching the exact rounding f32 then nearest even f32 then toward zero (legacyF32ToF16 on D3D12)
8_8_8_8_UNORM 256 / 256 256 133
8_8_8_8_SNORM 256 / 256 256 146
16_16_16_16_UNORM 65,536 / 65,536 65,534 33,796
16_16_16_16_SNORM 65,536 / 65,536 65,534 34,820
10_10_10_2_UNORM (10- and 2-bit fields) 4,096 / 4,096 channels 4,096 2,566
2_10_10_10_SNORM 4,096 / 4,096 channels 4,096 2,578

These are the "2 of 65,536" codes from above, for each 16-bit format:

format, code f32 the load returns d16 on gfx1036 f32 → nearest even f32 → toward zero
UNORM16 0xffaf 0x3f7fb000 0x3bfd 0x3bfe 0x3bfd
UNORM16 0xffef 0x3f7ff000 0x3bff 0x3c00 0x3bff
SNORM16 0x7ff7 0x3f7ff000 0x3bff 0x3c00 0x3bff
SNORM16 0x8009 0xbf7ff000 0xbbff 0xbc00 0xbbff

In all four, the f32 lies exactly on the f16 midpoint, while the exact ratio lies just below it. So no rounding of the returned f32 alone gives the hardware's half here. The code has to come back first, for example round(|f| × 65535) as Ingenioeren described, or image_norm16 in #2030. That also confirms passerbyyhx's reference values for 0xffaf and 0xffef.

2. Float texels: toward zero.

  • 32-bit floats: all 369 non-NaN values are truncated. That includes f16 denormals, and overflow gives 0x7bff, not infinity.
  • NaN: keeps the top 10 mantissa bits and is not quieted. If those 10 bits are all 0, the mantissa becomes 1, so the NaN never turns into infinity. Examples: 0x7f800001 → 0x7c01, 0x7fbfffff → 0x7dff, 0x7fc02000 → 0x7e01.
  • 16-bit float texels: come back with their stored bits, all 65,536, NaNs and denormals included.

3. The conversion ignores the shader's float mode. I re-ran the FLOAT32 values, 1,024 rows of 16_16_16_16_UNORM and all 8_8_8_8_UNORM codes in four other modes: f16 denormals flushed, f16 rounding toward zero, f16 rounding toward +inf, and IEEE on. The results were identical: 0 differences.

The translator's buffer path against these values (channels that differ):

tree 8_8_8_8 U / S 16_16_16_16 U / S (128-row sample with the 4 codes) 10-bit formats 32_32_32_32_FLOAT
main 63bcca4, Radeon and RTX 5080 123 / 110 of 256 227 / 208 of 512 382 and 379 of 1,024 0 (NaN payloads aside)
#1940 (f41d919), Radeon and RTX 5080 0 / 0 0 / 0 0 and 0 0 (NaN payloads aside)

main's numbers are the ones this issue gives. #1940's integer rounding matches the hardware on every code, the four above included. I ran #1940 on a local merge with other PRs that do not touch d16_channels.

I didn't run the image path through the translator; the views in build_valu_cs are raw. But on the hardware, the image d16 loads gave exactly what the buffer loads gave, row for row. So the tables above are what #2030 and #2125 should match.

AI use: Claude Code (Claude Opus 5.5) wrote the probe and ran it on the GPUs above; the numbers come from those runs.

**@⁠Lyrnc** on GitHub · 2026-11-26 18:52 UTC · [original](https://github.com/Marvisak/VIctory/issues/2006#issuecomment-7200947174) RDNA2 hardware values for the open questions here, measured on gfx1036. The hardware rounds UNORM / SNORM data to f16 **exactly once**. That includes the four 16-bit codes where rounding the f32 to nearest gives a different half. Float texels are truncated. Image and buffer d16 loads agree on every value. **Setup.** - **Hardware:** the Radeon iGPU of a Ryzen 7 9800X3D, **gfx1036**, AMD driver 32.0.21042.62, Windows 11. A HIP kernel (assembled by the driver's amd_comgr) runs, per lane over its own bytes: - `tbuffer_load_format_d16_xyzw` (format in the instruction); - `buffer_load_format_d16_xyzw` (format in the V#); - `image_load … dmask:0xf unorm d16` through a 1D linear T# over the same memory; - the same loads without `d16`, for the f32 each one returns. - **Inputs:** every code. 256 per 8-bit format, 65,536 per 16-bit format, 1,024 per 10-bit field. For 32-bit floats, 376 values: f16 ties, values just above and below a tie at every exponent around the f16 range, overflow, f16 denormals, NaNs. - **Translator:** the same `tbuffer_load_format_d16_xyzw` words through `build_valu_cs` (the buffer path, `d16_channels`). They ran on that Radeon and on an RTX 5080 (driver 610.88), one adapter at a time with a local switch. PC RDNA2 silicon, not the console's GPU. No game data. **1. UNORM / SNORM: one exact rounding, to nearest even.** `tbuffer`, `buffer` and `image` d16 loads return the same half for every code. Each half is `code / (2^n - 1)` rounded once: | format | codes matching the exact rounding | f32 then nearest even | f32 then toward zero (`legacyF32ToF16` on D3D12) | |---|---|---|---| | `8_8_8_8_UNORM` | 256 / 256 | 256 | 133 | | `8_8_8_8_SNORM` | 256 / 256 | 256 | 146 | | `16_16_16_16_UNORM` | 65,536 / 65,536 | 65,534 | 33,796 | | `16_16_16_16_SNORM` | 65,536 / 65,536 | 65,534 | 34,820 | | `10_10_10_2_UNORM` (10- and 2-bit fields) | 4,096 / 4,096 channels | 4,096 | 2,566 | | `2_10_10_10_SNORM` | 4,096 / 4,096 channels | 4,096 | 2,578 | These are the "2 of 65,536" codes from above, for each 16-bit format: | format, code | f32 the load returns | d16 on gfx1036 | f32 → nearest even | f32 → toward zero | |---|---|---|---|---| | UNORM16 `0xffaf` | `0x3f7fb000` | `0x3bfd` | `0x3bfe` | `0x3bfd` | | UNORM16 `0xffef` | `0x3f7ff000` | `0x3bff` | `0x3c00` | `0x3bff` | | SNORM16 `0x7ff7` | `0x3f7ff000` | `0x3bff` | `0x3c00` | `0x3bff` | | SNORM16 `0x8009` | `0xbf7ff000` | `0xbbff` | `0xbc00` | `0xbbff` | In all four, the f32 lies exactly on the f16 midpoint, while the exact ratio lies just below it. So no rounding of the returned f32 alone gives the hardware's half here. The code has to come back first, for example `round(|f| × 65535)` as Ingenioeren described, or `image_norm16` in #2030. That also confirms passerbyyhx's reference values for `0xffaf` and `0xffef`. **2. Float texels: toward zero.** - **32-bit floats:** all 369 non-NaN values are truncated. That includes f16 denormals, and overflow gives `0x7bff`, not infinity. - **NaN:** keeps the top 10 mantissa bits and is not quieted. If those 10 bits are all 0, the mantissa becomes 1, so the NaN never turns into infinity. Examples: `0x7f800001 → 0x7c01`, `0x7fbfffff → 0x7dff`, `0x7fc02000 → 0x7e01`. - **16-bit float texels:** come back with their stored bits, all 65,536, NaNs and denormals included. **3. The conversion ignores the shader's float mode.** I re-ran the FLOAT32 values, 1,024 rows of `16_16_16_16_UNORM` and all `8_8_8_8_UNORM` codes in four other modes: f16 denormals flushed, f16 rounding toward zero, f16 rounding toward +inf, and IEEE on. The results were identical: 0 differences. **The translator's buffer path against these values (channels that differ):** | tree | `8_8_8_8` U / S | `16_16_16_16` U / S (128-row sample with the 4 codes) | 10-bit formats | `32_32_32_32_FLOAT` | |---|---|---|---|---| | `main` `63bcca4`, Radeon and RTX 5080 | 123 / 110 of 256 | 227 / 208 of 512 | 382 and 379 of 1,024 | 0 (NaN payloads aside) | | #1940 (`f41d919`), Radeon and RTX 5080 | 0 / 0 | 0 / 0 | 0 and 0 | 0 (NaN payloads aside) | `main`'s numbers are the ones this issue gives. #1940's integer rounding matches the hardware on every code, the four above included. I ran #1940 on a local merge with other PRs that do not touch `d16_channels`. I didn't run the image path through the translator; the views in `build_valu_cs` are raw. But on the hardware, the image d16 loads gave exactly what the buffer loads gave, row for row. So the tables above are what #2030 and #2125 should match. AI use: Claude Code (Claude Opus 5.5) wrote the probe and ran it on the GPUs above; the numbers come from those runs.
This discussion has been locked. Commenting is limited to contributors.
No milestone
No assignees
1 participant
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
github/Marvisak--VIctory#2006
No description provided.