From 013609d4e6e64140f5052ddd1959405a3e3268c8 Mon Sep 17 00:00:00 2001 From: agilesh Date: Tue, 24 Nov 2026 21:20:49 +0530 Subject: [PATCH] shader: lower the v_cvt_f32_ubyte SDWA unpacking forms in pixel_aot Lower the four sub-dword unsigned byte conversion variants under SDWA (v_cvt_f32_ubyte0_sdwa, v_cvt_f32_ubyte1_sdwa, v_cvt_f32_ubyte2_sdwa, and v_cvt_f32_ubyte3_sdwa) in tools/pixel_aot.py. In accordance with maintainer review coordinating with PR #1584, this first slice establishes whole-dword destinations (dst_sel:DWORD with dst_unused:UNUSED_PAD or UNUSED_PRESERVE) and the common zero-extended source selectors (BYTE_0..3, WORD_0..1, and DWORD) across VGPR and SGPR sources, with optional saturation clamp. Float modifiers on integer source bits, registers out of range, integer literals, outstanding sample destination collisions, sign-extension (sext(src0)), partial destinations (dst_sel:BYTE_* / WORD_*), UNUSED_SEXT, and selections reaching into undefined upper halves of 16-bit results (low16/half16) are refused with ValueError. Adds static lowering checks and negative controls in tests/pixel_aot_check.py and rdna2_tools_check.py, unifies both sdwa_unsigned_bytes in SDWA_UNARY (draw-sdwa-unary) and the dedicated live SGPR-driven draw-sdwa-cvt-ubyte fixture with synthetic bit oracles and mutation controls, and updates form coverage (+4 forms on gfx1013 and rdna2). Co-authored-by: Jack <157014679+itzJaack@users.noreply.github.com> --- CMakeLists.txt | 5 + docs/experiments.md | 6 ++ tests/form_coverage_check.py | 1 + tests/pixel_aot_check.py | 151 +++++++++++++++++++++++++++++- tests/rdna2_tools_check.py | 8 ++ tools/pixel_aot.py | 28 ++++++ tools/scan_unsupported_shaders.py | 1 + 7 files changed, 199 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 53165e3f8d..c4c065fb7f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6348,6 +6348,11 @@ if(EXISTS "${INTAKE_PYTHON}") "$" ${adapter}) set_tests_properties(pixel_aot_sdwa_integer_${adapter} PROPERTIES TIMEOUT 60 SKIP_RETURN_CODE 77 ENVIRONMENT "FORM_COVERAGE_RECORD=${CMAKE_BINARY_DIR}/form_coverage/pixel_aot_sdwa_integer_${adapter}.txt") + add_test(NAME pixel_aot_sdwa_cvt_ubyte_${adapter} COMMAND "${INTAKE_PYTHON}" -B + "${CMAKE_SOURCE_DIR}/tests/pixel_aot_check.py" draw-sdwa-cvt-ubyte "${DXC_EXECUTABLE}" + "$" ${adapter}) + set_tests_properties(pixel_aot_sdwa_cvt_ubyte_${adapter} PROPERTIES TIMEOUT 60 SKIP_RETURN_CODE 77 + ENVIRONMENT "FORM_COVERAGE_RECORD=${CMAKE_BINARY_DIR}/form_coverage/pixel_aot_sdwa_cvt_ubyte_${adapter}.txt") add_test(NAME pixel_aot_e64_spellings_${adapter} COMMAND "${INTAKE_PYTHON}" -B "${CMAKE_SOURCE_DIR}/tests/pixel_aot_check.py" draw-e64-spellings "${DXC_EXECUTABLE}" "$" ${adapter}) diff --git a/docs/experiments.md b/docs/experiments.md index 75c4bfe5e4..ecece8fe1e 100644 --- a/docs/experiments.md +++ b/docs/experiments.md @@ -5761,6 +5761,12 @@ The SDWA forms of the exact arithmetic (`add`, `sub`, `subrev`, `mul`, `mul_lega The SDWA forms of the remaining unary binary32 operations (`v_exp_f32`, `v_log_f32`, `v_rcp_f32`, `v_rcp_iflag_f32`, `v_rsq_f32`, `v_sqrt_f32`, `v_sin_f32`, `v_cos_f32`, `v_rndne_f32` and `v_frexp_mant_f32`) go through the same path: each reuses its e32/e64 lowering unchanged, with `src0_sel:DWORD` only, and the result is placed with the SDWA move rules. The translator emits the same shader for the whole-dword SDWA spelling and the e32 spelling of each operation, and the controls assert that. A byte or word source, a `src1_sel`, a second operand, an output modifier, `UNUSED_SEXT` on a partial destination and `UNUSED_PRESERVE` over an untracked destination are refused for every one of the ten. Three programs are drawn with two constant rows each. D3D allows `exp2`, `log2`, a reciprocal, `rsqrt`, `sqrt`, `sin` and `cos` an error, so the first two programs choose inputs whose true result is an integer, add 0.5 and take the floor, which returns that integer for any result within 0.5 of it: exp of `-A`, log of `|B|`, rcp of C and rcp_iflag of `-|D|` give 8, 5, 8, -4 and then 16, 3, -2, -16; rsq of `|A|`, sqrt of B, sin of C and cos of `-D` (in revolutions) give 4, 7, 1, -1 and then 2, 3, -1, 1. That checks which operation ran and its source modifiers, not last-place accuracy. The third program is exact and is compared directly: `v_rndne_f32_sdwa` of the ties 2.5 and -3.5 (2 and -4), `v_frexp_mant_f32_sdwa` of `-B` with `clamp`, the low byte of a rounded tie kept in `BYTE_2` of 1.0 under `UNUSED_PRESERVE`, and the low byte of a significand padded into `BYTE_3`. 26 wrong references, each replacing one channel, must report `pixel_mismatch`: a neighbouring operation for each approximate one (natural exponential and logarithm, rsq for rcp, sqrt and rcp for rsq, rsq for sqrt, sin and cos swapped, radians for revolutions), a copied source, a dropped negation, ties rounded up or toward zero, a dropped clamp, padding for preserving, the neighbouring byte and an unplaced result. That is 32 draws from three compiles and three renderer launches. They run as their own tests, `pixel_aot_sdwa_unary_hardware` and `pixel_aot_sdwa_unary_warp` (`tests/pixel_aot_check.py draw-sdwa-unary`), with their own coverage record, and the LLVM 15 round trip covers the three programs. They passed on the NVIDIA GeForce RTX 3060 (driver 32.0.15.9636) and on WARP (Microsoft Basic Render Driver 10.0.19041.5794), Windows 10 (10.0.19045.6466), debug layer enabled with no errors or warnings. Not shown: last-place accuracy of the approximate operations on any adapter, a partial destination for an approximate result (its low bits have no single reference), inputs outside these rows (zero, negative arguments to log, rsq and sqrt, infinities, NaN, denormals), the integer-divide flag of `v_rcp_iflag_f32`, and what the console computes. +The SDWA unsigned byte conversions (`v_cvt_f32_ubyte0_sdwa`, `v_cvt_f32_ubyte1_sdwa`, `v_cvt_f32_ubyte2_sdwa` and `v_cvt_f32_ubyte3_sdwa`, AMD RDNA 2 ISA ยง12.11 opcode `0x11..0x14`, LLVM 15 `VOP1Instructions.td`: `V_CVT_F32_UBYTEN_sdwa_gfx10`) extract an 8-bit unsigned integer channel from the selected source sub-dword and convert it to floating-point (`dst = float((sdwa_source(src0, src0_sel, False) >> (8 * N)) & 0xff)`). In accordance with the coordinated first slice, `dst_sel:DWORD` with `dst_unused:UNUSED_PAD` or `UNUSED_PRESERVE` and common zero-extended source selectors (`BYTE_0..3`, `WORD_0..1` and `DWORD`) are accepted across VGPR and SGPR sources, with optional `clamp` (`saturate()`). Float modifiers on integer source bits (`-`, `|...|`), register numbers out of range (`v256`, `s106`), integer literals, outstanding sample destination collisions, sign-extension (`sext(src0)`), partial sub-dword destinations (`dst_sel:BYTE_*`, `WORD_*`), `UNUSED_SEXT`, and selections reaching into undefined upper halves of 16-bit results (`low16`/`half16`) are refused with `ValueError`. Testing covers both `SDWA_UNARY` (`sdwa_unsigned_bytes` drawn under `pixel_aot_sdwa_unary_warp` and `_hardware`) and dedicated live SGPR-driven draws (`sdwa_cvt_ubyte_plain` under `pixel_aot_sdwa_cvt_ubyte_warp` and `_hardware`, `tests/pixel_aot_check.py draw-sdwa-cvt-ubyte`), with synthetic oracle verification, channel mismatch tests, and shader mutation checks (opcode mutation, dropped clamp), adding 4 covered forms on `gfx1013` and `rdna2`. + +```powershell +ctest --preset dev --no-tests=error -R '^pixel_aot_sdwa_cvt_ubyte_(warp|hardware)$' -V +``` + Signed and unsigned 16-bit integer comparisons (`v_cmp_{eq,ne,lt,le,gt,ge}_{i16,u16}` in e32 and e64 encodings) compare only the low 16 bits of each source: the lowering sign-extends them with `asint(x << 16) >> 16` or masks them with `& 0xffff`, and the result feeds `v_cndmask_b32` through the same tracked lane mask as the other comparisons. A source may be a register, an integer inline constant from -16 to 64 or a literal up to `0xffff`. LLVM prints a float inline constant on a 16-bit integer operand as its binary16 literal (`1.0` as `0x3c00`), so float tokens, wider literals, source modifiers, `clamp` and `op_sel` are refused. The exec-writing `v_cmpx_*` forms stay rejected outside the `v_cmpx` region openers described above, which admit the e32 integer forms (the f16 comparisons and `v_cmp_class_f16` are lowered by their own branches in `tools/pixel_aot.py`), and `tests/form_coverage_check.py` asserts that all 24 16-bit integer forms, the 32 32-bit integer forms, the 32 64-bit integer forms, and the 34 binary16 and 34 binary32 floating-point forms (156 compare forms in all) are counted while those neighbours are not. The binary fixtures cover the 24 forms, and the draws (`tests/pixel_aot_check.py draw-comparisons`) evaluate all six predicates per kind and encoding on six word pairs. Each word is a normal binary32 value whose upper half differs from the lower one where a 32-bit comparison would disagree, and the pairs include `0x8000` against 0, equal low halves, -1 against 1, `0x7fff` against `0x8000` and both ends of the inline-constant range. Three references must report a pixel mismatch: a signed shader against the unsigned reference, `eq` replaced by `ne`, and a 32-bit comparison against the 16-bit reference on equal low halves. Signed and unsigned 64-bit integer comparisons (`v_cmp_{f,lt,eq,le,gt,ne,ge,t}_{i64,u64}` in e32, suffix-less and e64 spellings, [#794](https://github.com/R0Y00/Xbox2Windows/issues/794)) compare two words per source, so the HLSL needs no 64-bit type. Without a suffix the encoding is chosen as LLVM 15 does: e32 when the destination is `vcc`/`vcc_lo` and src1 is a VGPR pair, e64 otherwise. The high words decide unless they are equal, and then the low words, which always compare unsigned. A signed comparison reads only the high word as `int`. Every word is read as raw bits with `asuint` (`asint` for a signed high word), bound to a local before the comparison, and no arithmetic touches a float view. The result is a tracked lane condition like the other comparisons, so `v_cndmask_b32` consumes it unchanged. A source is a VGPR pair `v[a:a+1]` with `a` from 0 to 254 whose two registers hold tracked numbers, an even-aligned SGPR pair, or an integer inline constant from -16 to 64 sign-extended to 64 bits (`-1` is all ones). On e32 (VOPC) the destination must be `vcc_lo` or `vcc` and src1 must be a VGPR pair, because LLVM 15 rejects an SGPR there. On e64 the destination is `vcc_lo`, `vcc`, an SGPR or an aligned SGPR pair, as for the narrower comparisons, and the wave64 spellings (`vcc`, `s[52:53]`) lower to the same HLSL as the wave32 ones. Refused with a message that names the cause: 32-bit literals, float and hex tokens, constants outside -16..64, register ranges other than a pair, untracked or pending sources, an SGPR destination or SGPR src1 on e32, and misaligned or out-of-range SGPR pairs. Source modifiers, `clamp`, `op_sel`, the `v_cmpx_*` forms, f64 and the unsupported predicates, and the DPP and SDWA forms are refused with the generic `Unsupported instruction` message, which does not name the cause. The 32-bit literal extension rule is not sourced, so literals stay outside this change; refusing float inline constants (LLVM accepts `1.0`) is the translator's choice. diff --git a/tests/form_coverage_check.py b/tests/form_coverage_check.py index 5415b9f1ba..48e617e37b 100644 --- a/tests/form_coverage_check.py +++ b/tests/form_coverage_check.py @@ -412,6 +412,7 @@ def check_shader_records(): # SDWA forms of the unary binary32 operations, whole-dword sources only. sdwa_unary_forms = {("SDWA", f"v_{op}_f32_sdwa") for op in ( "exp", "log", "rcp", "rcp_iflag", "rsq", "sqrt", "sin", "cos", "rndne", "frexp_mant")} + sdwa_unary_forms.update(("SDWA", f"v_cvt_f32_ubyte{byte}_sdwa") for byte in range(4)) assert sdwa_unary_forms <= covered, (name, sdwa_unary_forms - covered) # Integer, binary16, binary32 and binary64 comparisons: e32 counts as VOPC and e64 as VOP3, for every predicate # the translator lowers; the rejected exec-writing neighbours stay out. diff --git a/tests/pixel_aot_check.py b/tests/pixel_aot_check.py index bb6b9b3a31..727acdcd89 100644 --- a/tests/pixel_aot_check.py +++ b/tests/pixel_aot_check.py @@ -7434,6 +7434,17 @@ def sdwa_unary_rounded(*values): "ties rounded up, placed"), (0, 3, lambda a, b, c, d: half_rtz(math.frexp(d)[0]), "result not placed"), (0, 3, lambda a, b, c, d: sdwa_unary_place(d, 0, 24, 8) * 2.0, "wrong byte value placed"))), + ("sdwa_unsigned_bytes", [ + "v_mov_b32_e32 v18, s44", + "v_cvt_f32_ubyte0_sdwa v12, v18 dst_sel:DWORD dst_unused:UNUSED_PAD src0_sel:BYTE_2", + "v_cvt_f32_ubyte1_sdwa v13, s45 dst_sel:DWORD dst_unused:UNUSED_PRESERVE src0_sel:WORD_1", + "v_cvt_f32_ubyte2_sdwa v14, s46 dst_sel:DWORD dst_unused:UNUSED_PAD src0_sel:DWORD", + "v_cvt_f32_ubyte3_sdwa v15, s47 dst_sel:DWORD dst_unused:UNUSED_PAD src0_sel:DWORD", + ], ((0x4180ff12, 0xc0123456, 0x40ff0000, 0x3f880000), + (0x400000ff, 0x3f128000, 0x40000001, 0xc0100000)), + lambda a, b, c, d: tuple(_byte(_f32_word(x), n) for x, n in zip((a, b, c, d), (2, 3, 2, 3))), + ((0, 0, lambda a, b, c, d: _byte(_f32_word(a), 0), "ignored BYTE_2 selection"), + (0, 1, lambda a, b, c, d: _byte(_f32_word(b), 1), "ignored WORD_1 selection"))), ) @@ -7453,6 +7464,12 @@ def check_sdwa_unary_lowering(): """Static checks for SDWA_UNARY: each form reuses its e32/e64 lowering, and the refusals stay.""" bodies = [translate(with_constants(body)).split("float4 main", 1)[1] for _, body, *_ in SDWA_UNARY] assert set(SDWA_UNARY_OPS) <= ACCEPTED, set(SDWA_UNARY_OPS) - ACCEPTED + byte_op = "v_cvt_f32_ubyte3_sdwa v12, s44 dst_sel:DWORD dst_unused:UNUSED_PAD src0_sel:BYTE_0" + clamped = translate(with_constants([byte_op.replace(" dst_sel", " clamp dst_sel")])) + assert "saturate(float(" in clamped + for invalid in (byte_op.replace("s44 ", "7 "), byte_op.replace("dst_sel:DWORD", "dst_sel:BYTE_1"), + byte_op.replace("s44 ", "sext(s44) "), byte_op.replace("v12,", "v256,")): + must_reject(translate, with_constants([invalid])) for text in ("exp2((-params[0].w))", "log2(abs(params[1].x))", "(1.0f / params[1].y)", "(1.0f / (-abs(params[1].z)))"): assert text in bodies[0], (text, bodies[0]) @@ -7535,6 +7552,134 @@ def add_case(row, values, status): return 0 +SDWA_CVT_UBYTE_OPS = tuple(f"v_cvt_f32_ubyte{i}_sdwa" for i in range(4)) + +SDWA_CVT_UBYTE_PROGRAMS = [ + # Program 0: All 4 byte indices, whole-dword, plain and clamp, SGPR and VGPR sources. + [ + "v_mov_b32_e32 v18, s45", + "v_cvt_f32_ubyte0_sdwa v12, s44 dst_sel:DWORD dst_unused:UNUSED_PAD src0_sel:BYTE_2", + "v_cvt_f32_ubyte1_sdwa v13, v18 dst_sel:DWORD dst_unused:UNUSED_PRESERVE src0_sel:WORD_1", + "v_cvt_f32_ubyte2_sdwa v14, s46 clamp dst_sel:DWORD dst_unused:UNUSED_PAD src0_sel:DWORD", + "v_cvt_f32_ubyte3_sdwa v15, s47 dst_sel:DWORD dst_unused:UNUSED_PAD src0_sel:DWORD", + ], +] + + +def sdwa_cvt_ubyte_ref0(a, b, c, d): + return [ + float((a >> 16) & 0xff), + float((b >> 24) & 0xff), + min(1.0, float((c >> 16) & 0xff)), + float((d >> 24) & 0xff), + ] + + +SDWA_CVT_UBYTE_TEST_SETS = ( + (0x12345678, 0x9abcdef0, 0x00020000, 0x40000000), + (0x80000000, 0x0000007f, 0x00000000, 0x01000000), + (0xffffffff, 0x80808080, 0x00ff0000, 0x00000000), + (0x00000001, 0x0000ff00, 0x00010000, 0x80000000), +) + + +def check_sdwa_cvt_ubyte_lowering(): + """Static checks for SDWA v_cvt_f32_ubyte[0-3]_sdwa lowering and refusals.""" + for prog in SDWA_CVT_UBYTE_PROGRAMS: + hlsl = translate(with_constants(prog)) + for i in range(4): + assert f">> {8 * i}u) & 0xffu)" in hlsl, (i, hlsl) + assert set(SDWA_CVT_UBYTE_OPS) <= ACCEPTED, set(SDWA_CVT_UBYTE_OPS) - ACCEPTED + + hlsl0 = translate(with_constants(SDWA_CVT_UBYTE_PROGRAMS[0])) + assert "saturate(" in hlsl0 + + for mod in ("-s44", "|s44|", "-v18", "|v18|"): + must_reject(translate, with_constants([f"v_cvt_f32_ubyte0_sdwa v12, {mod} dst_sel:DWORD dst_unused:UNUSED_PAD src0_sel:DWORD"])) + + must_reject(translate, with_constants(["v_cvt_f32_ubyte0_sdwa v256, s44 dst_sel:DWORD dst_unused:UNUSED_PAD src0_sel:DWORD"])) + must_reject(translate, with_constants(["v_cvt_f32_ubyte0_sdwa v12, s106 dst_sel:DWORD dst_unused:UNUSED_PAD src0_sel:DWORD"])) + must_reject(translate, with_constants(["v_cvt_f32_ubyte0_sdwa v12, v256 dst_sel:DWORD dst_unused:UNUSED_PAD src0_sel:DWORD"])) + must_reject(translate, with_constants(["v_cvt_f32_ubyte0_sdwa v12, 7 dst_sel:DWORD dst_unused:UNUSED_PAD src0_sel:DWORD"])) + must_reject(translate, with_constants(["v_cvt_f32_ubyte0_sdwa v12, sext(s44) dst_sel:DWORD dst_unused:UNUSED_PAD src0_sel:DWORD"])) + must_reject(translate, with_constants(["v_cvt_f32_ubyte0_sdwa v12, s44 dst_sel:DWORD dst_unused:UNUSED_SEXT src0_sel:DWORD"])) + + for partial in ("BYTE_0", "BYTE_1", "BYTE_2", "BYTE_3", "WORD_0", "WORD_1"): + must_reject(translate, with_constants([f"v_cvt_f32_ubyte0_sdwa v12, s44 dst_sel:{partial} dst_unused:UNUSED_PAD src0_sel:DWORD"])) + must_reject(translate, with_constants([f"v_cvt_f32_ubyte0_sdwa v12, s44 dst_sel:{partial} dst_unused:UNUSED_PRESERVE src0_sel:DWORD"])) + + low16_prog = [ + "v_mov_b32_e32 v18, s45", + "v_add_nc_u16 v18, v18, 1", + "v_cvt_f32_ubyte0_sdwa v12, v18 dst_sel:DWORD dst_unused:UNUSED_PAD src0_sel:WORD_1", + ] + expect_value_error(translate, with_constants(low16_prog), "Reads the undefined high half of a 16-bit result") + + +def check_sdwa_cvt_ubyte_draws(directory, compiler, renderer, backend): + """Draw the SDWA unsigned byte conversions under whole-dword destination selection.""" + programs = ( + ("sdwa_cvt_ubyte_plain", SDWA_CVT_UBYTE_PROGRAMS[0], sdwa_cvt_ubyte_ref0), + ) + for name, body, ref_func in programs: + source = directory / f"{name}.hlsl" + shader = directory / f"{name}.cso" + hlsl = translate(with_constants(body)) + source.write_text(hlsl, encoding="ascii") + compile_shader(compiler, source, shader) + + cases = [] + for index, row in enumerate(SDWA_CVT_UBYTE_TEST_SETS): + expected = ref_func(*row) + assert tuple(map(half_rtz, expected)) == tuple(expected), (name, expected) + constants = bytearray(256) + struct.pack_into("<4I", constants, 12, *row) + + for channel in range(4): + wrong_values = list(expected) + wrong_values[channel] = expected[channel] + 1.0 if expected[channel] < 254.0 else 0.0 + mismatch_path = directory / f"{name}-case{index}-bad{channel}.case" + mismatch_path.write_bytes(bytes(constants) + struct.pack("<4f", *wrong_values) * 64) + cases.append((mismatch_path, "mismatch")) + + pass_path = directory / f"{name}-case{index}-pass.case" + pass_path.write_bytes(bytes(constants) + struct.pack("<4f", *expected) * 64) + cases.append((pass_path, "pass")) + + try: + run_pixel_batch(renderer, backend, shader, cases) + except subprocess.CalledProcessError as error: + if error.returncode == 77: + return 77 + raise + + mutations = [ + ("sdwa_cvt_ubyte_mut_op", SDWA_CVT_UBYTE_PROGRAMS[0], "v_cvt_f32_ubyte0_sdwa", "v_cvt_f32_ubyte1_sdwa", sdwa_cvt_ubyte_ref0), + ("sdwa_cvt_ubyte_mut_clamp", SDWA_CVT_UBYTE_PROGRAMS[0], "clamp dst_sel:DWORD", "dst_sel:DWORD", sdwa_cvt_ubyte_ref0), + ] + for mut_name, orig_body, old_tok, new_tok, ref_func in mutations: + mut_body = [line.replace(old_tok, new_tok) for line in orig_body] + assert mut_body != orig_body, mut_name + source = directory / f"{mut_name}.hlsl" + shader = directory / f"{mut_name}.cso" + source.write_text(translate(with_constants(mut_body)), encoding="ascii") + compile_shader(compiler, source, shader) + + row = SDWA_CVT_UBYTE_TEST_SETS[0] + expected = ref_func(*row) + constants = bytearray(256) + struct.pack_into("<4I", constants, 12, *row) + mut_path = directory / f"{mut_name}.case" + mut_path.write_bytes(bytes(constants) + struct.pack("<4f", *expected) * 64) + try: + run_pixel_batch(renderer, backend, shader, [(mut_path, "mismatch")]) + except subprocess.CalledProcessError as error: + if error.returncode == 77: + return 77 + raise + return 0 + + def check_constant_descriptor_bounds() -> None: for offset in (0, 28, 32): program = [line.replace("s[20:21], 0x40", f"s[20:21], {offset:#x}") @@ -15161,6 +15306,8 @@ def _check_controls_inline(directory): # A partial UNUSED_PRESERVE result needs a tracked numeric destination, as for SDWA moves. for register in ("v0", "v8", "v20"): must_reject(translate, with_constants([sdwa_arith("floor", register, ["s44"], "BYTE_2", "UNUSED_PRESERVE")])) + check_sdwa_unary_lowering() + check_sdwa_cvt_ubyte_lowering() program = assembly(False) for old, new in (("s_mov_b32 s21, 0", "s_mov_b32 s21, 1"), @@ -22019,7 +22166,8 @@ def main(): "draw-region-body", "draw-region-waits", "draw-region-scalar-alu", "draw-region-scalar-load", "draw-div-fmas", "draw-cmp64", "draw-cmp-f64", "draw-shift64", "draw-f16-arith", "draw-f16-round", "draw-f16-fmac-ldexp", "draw-f16-cvt-int16", "draw-f16-to-int16", "draw-flat-scratch", "draw-smem-pointers", - "draw-dpp-cvt", "draw-dpp-movrel", "draw-dpp-f16", "draw-dpp-extra", "draw-pk-fmac"): + "draw-dpp-cvt", "draw-dpp-movrel", "draw-dpp-f16", "draw-dpp-extra", "draw-pk-fmac", + "draw-sdwa-cvt-ubyte"): try: draw = {"draw-blend-scalar": check_blend_and_scalar_draws, "draw-dpp-cvt": check_dpp_cvt_draw, @@ -22070,6 +22218,7 @@ def main(): "draw-cmp-f64": check_cmpf64_draw, "draw-shift64": check_shift64_draw, "draw-pk-fmac": check_pk_fmac_draw, + "draw-sdwa-cvt-ubyte": check_sdwa_cvt_ubyte_draws, "draw-flat-scratch": check_flat_scratch_draws, "draw-smem-pointers": check_smem_pointer_draw, "draw-dpp-f16": check_dpp_f16_draw}[mode] diff --git a/tests/rdna2_tools_check.py b/tests/rdna2_tools_check.py index f47c7560b9..f4435a6598 100644 --- a/tests/rdna2_tools_check.py +++ b/tests/rdna2_tools_check.py @@ -5470,6 +5470,14 @@ def test_supported_shader_instructions(self): "v_mad_f32_sdwa v1, s2, v3, v4 dst_sel:DWORD dst_unused:UNUSED_PAD src0_sel:DWORD src1_sel:DWORD")) self.assertTrue(is_opcode_recognized("v_mov_b32_sdwa")) self.assertTrue(is_opcode_recognized("v_cvt_f32_f16_sdwa")) + for idx in range(4): + ubyte_line = f"v_cvt_f32_ubyte{idx}_sdwa v1, s2 dst_sel:DWORD dst_unused:UNUSED_PAD src0_sel:DWORD" + self.assertTrue(is_instruction_supported(ubyte_line), ubyte_line) + self.assertTrue(is_instruction_supported(f"v_cvt_f32_ubyte{idx}_sdwa v1, v2 clamp dst_sel:DWORD dst_unused:UNUSED_PRESERVE src0_sel:BYTE_1"), ubyte_line) + self.assertFalse(is_instruction_supported(ubyte_line.replace("UNUSED_PAD", "UNUSED_SEXT")), ubyte_line) + self.assertFalse(is_instruction_supported(ubyte_line.replace("dst_sel:DWORD", "dst_sel:WORD_0")), ubyte_line) + self.assertFalse(is_instruction_supported(ubyte_line.replace("s2", "sext(s2)")), ubyte_line) + self.assertTrue(is_opcode_recognized(f"v_cvt_f32_ubyte{idx}_sdwa")) self.assertTrue(is_opcode_recognized("v_pk_add_f16")) self.assertTrue(is_opcode_recognized("v_dot4_i32_i8")) self.assertTrue(is_instruction_supported("v_cvt_f32_u32_e32 v1, v2")) diff --git a/tools/pixel_aot.py b/tools/pixel_aot.py index bd550c8f8a..7db699a621 100644 --- a/tools/pixel_aot.py +++ b/tools/pixel_aot.py @@ -4458,6 +4458,34 @@ def sample_coord(reg): expression = f"saturate({expression})" vector[dst] = f"alu{i}" lines.append(f" precise float alu{i} = {expression};") + elif m := match(r"v_cvt_f32_ubyte([0-3])_sdwa (v\d+), ([vs]\d+)( clamp)? dst_sel:DWORD dst_unused:UNUSED_(?:PAD|PRESERVE) src0_sel:(BYTE_[0-3]|WORD_[01]|DWORD)"): + # Sub-dword unsigned byte extraction and conversion to float under SDWA with DWORD destination. + # The source sub-dword field is selected and zero-extended before byte N is extracted as an unsigned 8-bit integer. + byte_idx, dst, src, clamp, src_sel = m.groups() + if mask != "entry" or not 0 <= int(dst[1:]) <= 255: + raise ValueError("Invalid SDWA byte conversion") + if src.startswith("s") and int(src[1:]) > 105: + raise ValueError(f"Register out of range: {src}") + if src.startswith("v") and int(src[1:]) > 255: + raise ValueError(f"Register out of range: {src}") + if dst in sample_dest: + raise ValueError("Write overlaps an outstanding sample") + if (src_name := low16_name(src, vector)) is not None: + if src_sel not in ("WORD_0", "BYTE_0", "BYTE_1"): + raise ValueError("Reads the undefined high half of a 16-bit result") + word = f"asuint({src_name})" + elif (half_name := half16_name(src, vector)) is not None: + if src_sel != "WORD_0": + raise ValueError("Reads the undefined high half of a 16-bit result") + word = f"asuint({half_name})" + else: + word = int_operand(src, scalar, vector, pending, sample_dest, signed=False) + selected = sdwa_source(word, src_sel, False) + expr = f"float((({selected}) >> {8 * int(byte_idx)}u) & 0xffu)" + if clamp: + expr = f"saturate({expr})" + vector[dst] = f"alu{i}" + lines.append(f" precise float alu{i} = {expr};") elif m := match(rf"v_({'|'.join(SDWA_ALU)})_f32_sdwa (v\d+), (.+?)( clamp)? dst_sel:(BYTE_[0-3]|WORD_[01]|DWORD) dst_unused:(UNUSED_PAD|UNUSED_SEXT|UNUSED_PRESERVE) src0_sel:DWORD( src1_sel:DWORD)?"): # SDWA forms of binary32 arithmetic take whole-dword sources, so the binary32 denormal mode never # applies to a zero-extended field; the result is placed like an SDWA move. diff --git a/tools/scan_unsupported_shaders.py b/tools/scan_unsupported_shaders.py index c7dd46b11f..c95af736d7 100644 --- a/tools/scan_unsupported_shaders.py +++ b/tools/scan_unsupported_shaders.py @@ -164,6 +164,7 @@ def lower_rejection_causes(results: list[dict]) -> dict: r"v_cvt_i32_f32_e32 v\d+, (?:[vs]\d+|0x[0-9a-f]{1,8})", rf"v_cvt_(?:i32|u32)_f32_e64 v\d+, {CVT_F32_E64_SOURCE}", r"v_cvt_f32_ubyte[0-3](?:_e32|_e64)?\s+v\d+,\s+(?:[vs]\d+|0x[0-9a-f]{1,8}|\d+)(?:\s+clamp)?", + r"v_cvt_f32_ubyte[0-3]_sdwa\s+v\d+,\s+[vs]\d+(?:\s+clamp)?\s+dst_sel:DWORD\s+dst_unused:UNUSED_(?:PAD|PRESERVE)\s+src0_sel:(?:BYTE_[0-3]|WORD_[01]|DWORD)", r"v_cvt_off_f32_i4(?:_e32|_e64)?\s+v\d+,\s+(?:[vs]\d+|0x[0-9a-f]{1,8}|\d+)(?:\s+clamp)?", r"v_cvt_f32_(?:u32|i32)_e32\s+v\d+,\s+(?:[vs]\d+|0x[0-9a-f]{1,8}|-?\d+)", r"v_cvt_f32_(?:u32|i32)_e64\s+v\d+,\s+(?:[vs]\d+|0x[0-9a-f]{1,8}|-?\d+)(?:\s+clamp)?",