From 309c50e0ecf4391898891c204f826fa7de5906c8 Mon Sep 17 00:00:00 2001 From: wish131400 <2898869317@qq.com> Date: Sat, 28 Nov 2026 13:08:29 +0800 Subject: [PATCH] shader: preserve the direct fract binary32 upper bound Match the established fract32 bound from the Python translators in the newer direct DXIL lowering. Preserve NaNs and apply the cap before output modifiers. Refs #267, #2010. --- docs/experiments/native-fract-f32-bound.md | 46 ++++++++ src/gfx1013_direct_compute.inl | 9 +- tests/cmake/native_fract_f32.cmake | 11 ++ tests/native_fract_f32_check.py | 124 +++++++++++++++++++++ 4 files changed, 188 insertions(+), 2 deletions(-) create mode 100644 docs/experiments/native-fract-f32-bound.md create mode 100644 tests/cmake/native_fract_f32.cmake create mode 100644 tests/native_fract_f32_check.py diff --git a/docs/experiments/native-fract-f32-bound.md b/docs/experiments/native-fract-f32-bound.md new file mode 100644 index 0000000000..e55f023cb9 --- /dev/null +++ b/docs/experiments/native-fract-f32-bound.md @@ -0,0 +1,46 @@ +# Direct binary32 fract upper bound + +The direct DXIL frontend accepted `v_fract_f32` but emitted only DXIL `Frc`. +For the synthetic inputs `0xb0800000` (-2^-30) and `0xb1800000` (-2^-28), +that returned `0x3f800000` (1.0) on the tested desktop adapter. The gfx1013 +instruction keeps its result strictly below one, returning `0x3f7fffff` for +these inputs. The Python translator already applies this bound; see the +[earlier fract experiments](../experiments.md), +[#267](https://github.com/R0Y00/Xbox2Windows/pull/267) and +[#2010](https://github.com/R0Y00/Xbox2Windows/pull/2010). This is the same +boundary in the newer direct DXIL path, rather than a change to those Python +lowerings. + +The direct frontend now caps a result at or above 1.0 with an ordered comparison. +A NaN fails that comparison and passes through. The cap precedes the output +scale and clamp: `mul:2` of these tiny negative inputs must yield `0x3fffffff`, +not 2.0. Changing the direct frontend also changes its generated cache revision. + +The fixture constructs eight inputs and four instruction forms per input: +VOP1, VOP3 with input negation, VOP3 with clamp, and VOP3 with `mul:2`. +Each lane of two wave32 groups writes all 32 results. The readback compares +2,048 output words and 16 guard words, accepting NaNs by class. The controls +compile and validate DXIL and reject reference mutations that omit the cap or +replace NaN with the finite cap. + +```powershell +cmake --build --preset dev --target compile_dxil native_store_replay +ctest --preset dev --no-tests=error -V -R '^native_fract_f32_(controls|hardware)$' +``` + +At parent revision `240e0fb9c460f24d51683abe2d4cce2800b136c1`, the hardware +fixture failed on 384 of 2,064 words. With the bound, all words matched. +The adapter was an NVIDIA GeForce RTX 4070 Ti SUPER, driver 32.0.15.7270, +on Windows 10 build 19045, with DXC 1.9.2609 and Agility SDK 1.620.2. +The ordinary Windows 10 fixture executable reported the shader-model capability +skip; the measured runs relinked the same fixture objects locally with the +Agility SDK version/path exports and placed its matching runtime beside them. +The D3D12 debug layer was not enabled for those compute readbacks. +The compile controls, `native_f16_alu_ir`, `native_frexp_f32_controls` and +`native_mad32_controls` all passed. No console hardware or full program run +is claimed by these fixture results. + +After rebasing onto `aeef65b2da356b15cf1a81c5d6a73bd6cee77bcd`, the two +targets rebuilt, the compile controls passed, and the Agility-linked hardware +fixture again matched all 2,064 words. The ordinary executable still reported +the capability skip. diff --git a/src/gfx1013_direct_compute.inl b/src/gfx1013_direct_compute.inl index 46ba4ecf0b..dcde910032 100644 --- a/src/gfx1013_direct_compute.inl +++ b/src/gfx1013_direct_compute.inl @@ -2135,9 +2135,14 @@ struct DirectCompute { result = unary(26, a); else if (n == "v_floor_f32") result = unary(27, a); - else if (round) + else if (round) { result = unary(n == "v_fract_f32" ? 22 : n == "v_ceil_f32" ? 28 : 29, a); - else if (n == "v_sqrt_f32") + // A host Frc can round tiny negative inputs up to 1.0. The guest's + // result stays below 1.0; an ordered comparison keeps NaNs intact. + if (n == "v_fract_f32") + result = select_float(ir.value("fcmp oge float " + result + ", 1.0"), + ir.value("bitcast i32 1065353215 to float"), result); + } else if (n == "v_sqrt_f32") result = unary(24, a); else if (n == "v_rsq_f32") result = unary(25, a); diff --git a/tests/cmake/native_fract_f32.cmake b/tests/cmake/native_fract_f32.cmake new file mode 100644 index 0000000000..3d24a542c4 --- /dev/null +++ b/tests/cmake/native_fract_f32.cmake @@ -0,0 +1,11 @@ +if(TARGET compile_dxil AND TARGET native_store_replay AND EXISTS "${INTAKE_PYTHON}" AND + EXISTS "${DXC_BIN_DIR}/dxcompiler.dll" AND EXISTS "${DXC_BIN_DIR}/dxil.dll") + add_test(NAME native_fract_f32_controls COMMAND "${INTAKE_PYTHON}" -B + "${CMAKE_CURRENT_SOURCE_DIR}/tests/native_fract_f32_check.py" controls + "$" "${DXC_BIN_DIR}") + add_test(NAME native_fract_f32_hardware COMMAND "${INTAKE_PYTHON}" -B + "${CMAKE_CURRENT_SOURCE_DIR}/tests/native_fract_f32_check.py" hardware + "$" "${DXC_BIN_DIR}") + set_tests_properties(native_fract_f32_controls PROPERTIES TIMEOUT 60) + set_tests_properties(native_fract_f32_hardware PROPERTIES SKIP_RETURN_CODE 77 TIMEOUT 60) +endif() diff --git a/tests/native_fract_f32_check.py b/tests/native_fract_f32_check.py new file mode 100644 index 0000000000..305f41afd0 --- /dev/null +++ b/tests/native_fract_f32_check.py @@ -0,0 +1,124 @@ +"""Direct DXIL fract's strict upper bound, NaNs and output modifier order. + +The source-built VOP1 and VOP3 instructions write 32 words per lane in two +wave32 groups. Tiny negative values make a host Frc round to 1.0; gfx1013 +instead returns the greatest binary32 below 1.0. A NaN must stay a NaN. +""" +import math +import struct +import subprocess +import sys +import tempfile +from pathlib import Path + +from native_dpp_quad_check import request +from native_frexp_f32_check import v_mov_lit, vop1, vop3_unary +from native_mad32_check import PROLOGUE + +INPUTS = (0xb0800000, 0xb1800000, 0x3e800000, 0xbe800000, + 0, 0x80000000, 0x7fc12345, 0xc0400000) +CAP = 0x3f7fffff +SENTINEL = 0x69696969 +WORDS = 2064 + + +def bits(value): + return struct.unpack('= 1.0 else result + + +def expected_row(word, **mutation): + plain = fract(word, **mutation) + negated = fract(word ^ 0x80000000, **mutation) + clamped = bits(0.0 if math.isnan(value(plain)) else value(plain)) + scaled = bits(value(plain) * 2.0) + return plain, negated, clamped, scaled + + +def reference(**mutation): + row = [v for word in INPUTS for v in expected_row(word, **mutation)] + return [SENTINEL] * 8 + row * 64 + [SENTINEL] * 8 + + +def program(): + code = list(PROLOGUE) + for ci, word in enumerate(INPUTS): + code += v_mov_lit(1, word) + forms = ([vop1(0x20, 3, 257)], + vop3_unary(0x1a0, 3, 257, neg0=1), + vop3_unary(0x1a0, 3, 257, clamp=1), + vop3_unary(0x1a0, 3, 257, omod=1)) + for fi, form in enumerate(forms): + code += form + [0xe0701000 | (4 * (4 * ci + fi)), 0x80080308] + return code + [0xbf810000] + + +def is_nan(word): + return (word & 0x7fffffff) > 0x7f800000 + + +def matches(actual, expected): + return len(actual) == len(expected) and all( + a == e or (is_nan(a) and is_nan(e)) for a, e in zip(actual, expected)) + + +def controls(cli, sdk, work, root): + marker = request(work, root, program()) + output = work / 'fract.cso' + subprocess.run([cli, sdk, work / 'cache', 'gfx1013-direct-cs@0', marker, output], check=True) + dump = subprocess.run([Path(sdk) / 'dxc.exe', '-dumpbin', output], + capture_output=True, text=True, check=True).stdout + assert 'Frc(value)' in dump, dump + # Ordered comparison preserves NaN. The cap precedes the output scale + # and clamp, which the hardware mode checks through the stored words. + assert 'fcmp oge float' in dump and '1065353215 to float' in dump, 'missing ordered upper bound' + expected = reference() + assert not matches(reference(cap=False), expected), 'uncapped Frc escaped' + assert not matches(reference(nan_to_cap=True), expected), 'NaN-losing minimum escaped' + print('fract DXIL validates; uncapped and NaN-losing references differ') + return 0 + + +def hardware(replay, sdk, work, root): + marker = request(work, root, program()) + output = work / 'readback.bin' + result = subprocess.run([replay, sdk, marker, work / 'cache', '0', output, '--direct']) + if result.returncode == 77: + return 77 + result.check_returncode() + actual = struct.unpack(f'<{WORDS}I', output.read_bytes()) + expected = reference() + if not matches(actual, expected): + bad = [(i, f'{a:08x}', f'{e:08x}') for i, (a, e) in enumerate(zip(actual, expected)) + if a != e and not (is_nan(a) and is_nan(e))] + raise AssertionError(f'{len(bad)}/{WORDS} words differ: {bad[:12]}') + print('fract e32/e64: 2048 words and 16 guards matched (NaN by class)') + return 0 + + +def main(mode, executable, sdk): + with tempfile.TemporaryDirectory(prefix='x2w_native_fract_f32_') as temp: + work = Path(temp) + source = work / 'root.hlsl' + source.write_text('#define SIG "DescriptorTable(UAV(u0,numDescriptors=8))"') + root = work / 'root.bin' + subprocess.run([Path(sdk) / 'dxc.exe', '-T', 'rootsig_1_1', '-E', 'SIG', + '-Fo', root, source], check=True) + return {'controls': controls, 'hardware': hardware}[mode](executable, sdk, work, root.read_bytes()) + + +if __name__ == '__main__': + raise SystemExit(main(*sys.argv[1:]))