Shaders: D16 image reads of UNORM / SNORM texels are one ULP low for about half the codes #2006
Labels
No labels
accessibility
area: build
area: formats
area: gpu
area: loader
area: runtime
area: shader
bug
documentation
duplicate
enhancement
good first issue
gpu: amd
gpu: intel
gpu: nvidia
help wanted
host: wine
invalid
out of scope
question
request
run report
triage: confirmed
triage: has patch
triage: merge candidate
triage: needs info
triage: needs rebase
triage: needs work
triage: proposed close
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/Marvisak--VIctory#2006
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@upipibi on GitHub · 2026-11-25 02:48 UTC · original
What happens
image()inemit/lower.rs(around line 8292) packs every D16 channel withlegacyF32ToF16of the f32 the host view returned. The comment there says this rounds to nearest even, but on D3D12legacyF32ToF16rounds toward zero. I measured that on an RTX 3070 (616.56) and on WARP:1 + 3·2⁻¹¹gives0x3c01, 70000 gives0x7bff.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_UNORMcodes off by one ULP (code 9 gives0x2884, where RDNA2 gives0x2885). 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
word1 >> 20 & 0x1FF. Pick per channel betweenlegacyF32ToF16(float formats) and an exact single rounding (UNORM/SNORM). The exact rounding needs only integer ops:code / (2^w − 1)repeats itsw-bit code in binary, so(code << 32) / (2^w − 1)gives the bits that decide the rounding, and there are no ties. #1940'snorm_f16already does this for buffers, with constant widths. Images would need the width from the format table at run time.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).
@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 D16image_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.0x2884→0x28850x3048→0x3049Both 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).
@DenRakEiw on GitHub · 2026-11-25 03:28 UTC · original
Two implementation notes from checking
origin/main(940d32f) before attempting a fix:T# word 1 is not available in GPU memory for Option 1: In
crates/victory-shader/src/emit/lower.rs,s_load_dwordx8from descriptor tables is virtualized to a PCResourceDescriptorHeapindex (descriptor_dataonly reconstructs dimensions viadx.op.getDimensions, and explicitly rejects"a descriptor's address or format read as data"). So Option 1 would requirevictory-gputo mirror a sideband format/rounding-mode bit per view-heap slot (or specialize pipelines as in Option 2).Once the float vs. norm bit is known,
lower.rsalready 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 off64_to_f16_rne(f32_round(code / (2^w - 1)))vs. exactf64_to_f16_rne(code / (2^w - 1)):And because it operates on the returned
f32, it also works uniformly for filteredimage_sampleas well asimage_load, without needing a per-format bit-width lookup table.AI-assisted (Gemini 4 Neon).
@Ingenioeren on GitHub · 2026-11-25 03:35 UTC · original
I checked the 16-bit cases using real
R16_UNORMandR16_SNORMtexture loads on an RX 6750 XT (driver32.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 from940d32fand native SM 6.6 HLSL. DXC emitted the samelegacyF32ToF16conversion used by VIctory's D16 image lowering. D16 conversion in the current sourceThose 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.
@astrosteveo on GitHub · 2026-11-25 04:20 UTC · original
The same format problem reaches typed buffer loads:
buffer_load_format_d16_*converts withlegacyF32ToF16without knowing the view's format. On an RTX 5080 under vkd3d-proton, RGBA32F data is rounded to nearest (1 + 3/2048 gives0x3c02, 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).@upipibi on GitHub · 2026-11-25 04:21 UTC · original
Thanks, that's an important qualifier. My "
legacyF32ToF16rounds 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:
0x7BFFinstead of infinity, as on RDNA2.(
#2030with 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).
@YaBulochka on GitHub · 2026-11-25 04:58 UTC · original
RADV/RDNA3 data point, measured on the GPU:
imageLoadof 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_UNORM256x1 image and anR16_UNORM256x256 image filled with every code through a staging copy; a compute shader doesimageLoad(...).rfor 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:
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
legacyF32ToF16step inemit/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
imageLoadconversion, 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_SNORMand to atexelFetch-of-R32G32_FLOATcomparison if that helps settle it; the whole thing is a ~200-line Vulkan compute program and needs no title run.@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
legacyF32ToF16rounds 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_loadfrom 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:20 UTC · original
Thanks, DenRakEiw. That settles the shape of the fix. Putting the thread together:
victory-gpuwhen the view is made: is the format UNORM / SNORM, or float?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 filteredimage_sample.f32_to_f16(.., rtz: true), because RDNA2 truncates those (boykopovar/AnyPS5#22750).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).
@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 incrates/victory-shader/src/emit/lower.rs(Lower::image) converts every channel withlegacyF32ToF16, with no knowledge of the bound texture's format; itsshortcut: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 withmainand need a rebase. #2064's test also passes onmain, so it does not address this. The typed-buffer D16 path is #1940's side.Generated by Claude Code
@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. Currentmainis63bcca4.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 (whatlegacyF32ToF16does 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_nearestlocally so it dumps its readback. That checks the R channel and the G channel (the reversed code) of every texel.-D warningsmaine87c4f1victory-shader+victory-gpuruns fail 5 tests, and the same 5 fail alone on940d32f.main's per-channellegacyF32ToF16put 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 againstmain's truncation.victory-shader,victory-gpu)descriptor/host.rs,lower.rs,shader.rs,stride_tests.rs,synthetic_tests.rs3fdf5e9940d32f.f7870d3d16_channels), not image readsmain,940d32f, #2030 and #2125, because native D3D12'slegacyF32ToF16already truncates. The PR reports the failure only under vkd3d-proton, which I did not test.victory-shader)lower.rsThe
hardwarebackend 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):crates/victory-shader/src/emit/lower.rs:8076,Source::Constant). A point sampler loaded from the heap or picked by aSource::Phitakes 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_SRGBformats as normalized. What RDNA2 does for D16 reads of sRGB views has not been measured in this thread.A4B4G4R4_UNORMis 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 ontomain(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).
@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: stored0xffafreturned D160x3bfe(single-round exact fraction0x3bfd), and stored0xffefreturned0x3c00(reference0x3bff).A linear sample between
0xff6fand0xffefreturned f320x3f7fb000on both devices, identical to the point read of0xffaf. Here rounding the paired filtered f32 gives D160x3bfe, 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@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.
tbuffer_load_format_d16_xyzw(format in the instruction);buffer_load_format_d16_xyzw(format in the V#);image_load … dmask:0xf unorm d16through a 1D linear T# over the same memory;d16, for the f32 each one returns.tbuffer_load_format_d16_xyzwwords throughbuild_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,bufferandimaged16 loads return the same half for every code. Each half iscode / (2^n - 1)rounded once:legacyF32ToF16on D3D12)8_8_8_8_UNORM8_8_8_8_SNORM16_16_16_16_UNORM16_16_16_16_SNORM10_10_10_2_UNORM(10- and 2-bit fields)2_10_10_10_SNORMThese are the "2 of 65,536" codes from above, for each 16-bit format:
0xffaf0x3f7fb0000x3bfd0x3bfe0x3bfd0xffef0x3f7ff0000x3bff0x3c000x3bff0x7ff70x3f7ff0000x3bff0x3c000x3bff0x80090xbf7ff0000xbbff0xbc000xbbffIn 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, orimage_norm16in #2030. That also confirms passerbyyhx's reference values for0xffafand0xffef.2. Float texels: toward zero.
0x7bff, not infinity.0x7f800001 → 0x7c01,0x7fbfffff → 0x7dff,0x7fc02000 → 0x7e01.3. The conversion ignores the shader's float mode. I re-ran the FLOAT32 values, 1,024 rows of
16_16_16_16_UNORMand all8_8_8_8_UNORMcodes 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):
8_8_8_8U / S16_16_16_16U / S (128-row sample with the 4 codes)32_32_32_32_FLOATmain63bcca4, Radeon and RTX 5080f41d919), Radeon and RTX 5080main'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 touchd16_channels.I didn't run the image path through the translator; the views in
build_valu_csare 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.