Shaders: normalized buffer codes give the nearest float, cube gradients take DXIL's base-2 logarithm, d16 data of integer views keeps its bits, unordered views of vertex programs are declared

- VIctory da82e444: the host's division was a step off for some codes, and a
  material index cut from one was the code below.
- DexNom/VIctory 921d8d3b (its cube part, with its test): the level was taken
  from a natural logarithm DXIL does not have, 1.44 times too high; the
  instruction's least level was lost.
- VIctory 4243a96a, for buffers a program picks from the heap.
- VIctory a1a2077a.
Every shader is translated anew.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
localandClaude Opus 5.5 committed 2026-11-25 10:50:14 -06:00
1 parent 350c686481
commit 9a6c829d68
9 files changed
+501 -36

No files matched your search

@@ -415,6 +415,66 @@ fn normalized_buffer_formats_load_their_ends_exactly() {
}
}
/// Codes of the normalized formats sampled below: every 8-bit code; for
/// 16 bits a stride, the RX 6700 XT's division misses (257, 261, ...) and
/// the ends.
fn normalized_codes(bits: u32) -> Vec<u32> {
if bits <= 10 {
return (0..1 << bits).collect();
}
let edges = [1, 2, 3, 255, 256, 257, 261, 265, 269, 511, 512, 1000, 0x7FFF, 0x8000, 0x8001, 0xFFFE];
(0..=0xFFFF).step_by(257).chain(edges).collect()
}
/// Every normalized code loads as the float nearest its exact quotient,
/// as the console's buffer unit converts it, not as the host divides (the
/// RX 6700 XT gave 125 of the 8-bit codes one ulp high and 16-bit codes
/// such as 257 one ulp low): unsigned and signed, 8, 10 (the x channel of
/// 2_10_10_10) and 16 bits, the signed formats' two lowest codes -1.0.
#[test]
fn normalized_buffer_formats_load_correctly_rounded_quotients() {
let x = 0;
// (format, bits, signed): 8_UNORM, 16_UNORM, 8_SNORM, 16_SNORM,
// 2_10_10_10_UNORM and _SNORM.
for (format, bits, signed) in [(1u8, 8u32, false), (7, 16, false), (2, 8, true), (8, 16, true), (50, 10, false), (51, 10, true)] {
let shader = tbuffer_load_of_v1(x, format);
let cases: Vec<[u32; 4]> = normalized_codes(bits).iter().map(|&a| [a, 0, 0, 0]).collect();
run_cases(&shader, &cases, |backend, _, [a, ..], got| {
let want = if signed {
let top = (1i32 << (bits - 1)) - 1;
let s = ((a << (32 - bits)) as i32) >> (32 - bits);
(s.max(-top) as f32 / top as f32).to_bits()
} else {
(a as f32 / ((1u32 << bits) - 1) as f32).to_bits()
};
let sign = if signed { "signed" } else { "unsigned" };
assert_eq!(got, want, "{backend}: {bits}-bit {sign} normalized {a:#x} loads as {got:#010x}, want {want:#010x}");
});
}
}
/// A title truncates `x * (2^n - 1)` back to the code it stored (the
/// material index a mesh program reads from a vertex colour's alpha, as
/// `v_mul_legacy_f32` by 255.0 then `v_cvt_u32_f32`): every code comes back
/// as itself, on hardware and WARP, which a quotient one ulp low breaks.
#[test]
fn normalized_codes_truncate_back_to_themselves() {
let x = 0;
for (format, bits) in [(1u8, 8u32), (7, 16)] {
let scale = (((1u32 << bits) - 1) as f32).to_bits();
let mut body = buffer_store_dword(1, 8, 0).to_vec();
body.extend(mtbuf(x, 0, 8, format));
// v_mul_legacy_f32 v0, literal, v0; v_cvt_u32_f32 v0, v0.
body.extend([0x07 << 25 | 0xFF, scale]);
body.push(vop1(0x07, 0, vgpr(0)));
let shader = build_valu_cs(&body);
let cases: Vec<[u32; 4]> = normalized_codes(bits).iter().map(|&a| [a, 0, 0, 0]).collect();
run_cases(&shader, &cases, |backend, _, [a, ..], got| {
assert_eq!(got, a, "{backend}: {bits}-bit code {a} comes back as {got}");
});
}
}
#[test]
fn packed_add_selects_halves_of_inline_constants() {
// Add the inline constant 1/(2*pi) to v1 (zero) to read back its two halves.