shader: lower v_sad_hi_u8 and v_msad_u8 in gfx2hlsl #2724

Closed
opened 2026-11-26 04:18:02 +00:00 by triggered02 · 0 comments
triggered02 commented 2026-11-26 04:18:02 +00:00 (Migrated from github.com)
Contributor

Imported from R0Y00/Xbox2Windows. Originally posted by triggered02 on 2026-11-26T04:18:02Z.


gfx2hlsl still refuses two unsigned sum-of-absolute-differences forms that the CPU AOT path already lowers: v_sad_hi_u8 and v_msad_u8. They came up in the census group of refused shader instructions; unlike the missing-lowering issues with no reference, this one has a reference implementation in this repository, so the shader side can be pinned against it.

Gap

tools/pixel_aot.py (the CPU AOT lowering table) has, next to sad_u8 / sad_u16 / sad_u32:

"sad_hi_u8": (3, "(sad4_u8({0}, {1}, false) << 16u) + {2}"),
"msad_u8":   (3, "sad4_u8({0}, {1}, true) + {2}"),

with the table's own note: "src1 is the reference: v_msad_u8 leaves out each byte lane whose src1 byte is zero".

tools/startup/gfx2hlsl.py has only:

if base in ('v_sad_u8', 'v_sad_u16', 'v_sad_u32'):
    ...

and the matching sad_u8 / sad_u16 / sad_u32 HLSL helpers, so both forms fall through to Unsupported.

Synthetic repro

Encode either VOP3 form (opcode 347 V_SAD_HI_U8, 369 V_MSAD_U8 in the RDNA 2 reference used by tools/disasm_rdna2.py) with three vector sources and translate:

python -B tools/startup/gfx2hlsl.py --text '<encoded v_sad_hi_u8>' ...   # REFUSED: v_sad_hi_u8 / no such handler

The smallest reproduction is the one the existing tests/startup_gfx2hlsl_sad_check.py already uses for v_sad_u8: one program with four lanes per form computing v_sad_* into a lane, exported through a colour target. That fixture, extended with the two forms, is the acceptance vehicle.

Expected behavior

  • v_sad_hi_u8 dst, src0, src1, src2 lowers to the sad_hi_u8 helper and the form is recorded in uses_sad like the other three.
  • v_msad_u8 lowers to an msad_u8 helper that skips each byte lane whose src1 byte is zero, matching the skip_zero argument sad4_u8 already takes in the AOT table.
  • Both keep the existing modulo-2^32 addition of src2, as sad_u8 does.

Acceptance

A new pure-Python check in the style of tests/startup_gfx2hlsl_sad_check.py (encode, translate, compare against a reference computed from the AOT formulas for both forms), registered as a CTest, plus tests/startup_translator_check.py and tests/form_coverage_check.py still passing. Proposed selection: ctest --preset dev --no-tests=error -R '^startup_gfx2hlsl_sad'.

NOT established

  • Which byte set hi selects. The AOT table computes the full four-byte SAD and shifts the result left 16 (sad4_u8 << 16), which is not the same thing as taking only the high byte of each 16-bit word. This issue does not decide which is the hardware-correct reading; a lowering that mirrors pixel_aot is internally consistent with the repository's reference but still needs a hardware or WARP fixture (or an ISA quote specific to V_SAD_HI_U8) before anyone claims the register value matches the console.
  • Whether the two forms appear in the title's shader census at all; this is filed from the coverage gap, not from a refused title program.
<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6619399814 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/issues/2732). Originally posted by [triggered02](https://github.com/triggered02) on 2026-11-26T04:18:02Z. --- `gfx2hlsl` still refuses two unsigned sum-of-absolute-differences forms that the **CPU AOT path already lowers**: `v_sad_hi_u8` and `v_msad_u8`. They came up in the census group of refused shader instructions; unlike the missing-lowering issues with no reference, this one has a reference implementation in this repository, so the shader side can be pinned against it. ## Gap `tools/pixel_aot.py` (the CPU AOT lowering table) has, next to `sad_u8` / `sad_u16` / `sad_u32`: ```python "sad_hi_u8": (3, "(sad4_u8({0}, {1}, false) << 16u) + {2}"), "msad_u8": (3, "sad4_u8({0}, {1}, true) + {2}"), ``` with the table's own note: *"src1 is the reference: v_msad_u8 leaves out each byte lane whose src1 byte is zero"*. `tools/startup/gfx2hlsl.py` has only: ```python if base in ('v_sad_u8', 'v_sad_u16', 'v_sad_u32'): ... ``` and the matching `sad_u8` / `sad_u16` / `sad_u32` HLSL helpers, so both forms fall through to `Unsupported`. ## Synthetic repro Encode either VOP3 form (opcode 347 `V_SAD_HI_U8`, 369 `V_MSAD_U8` in the RDNA 2 reference used by `tools/disasm_rdna2.py`) with three vector sources and translate: ``` python -B tools/startup/gfx2hlsl.py --text '<encoded v_sad_hi_u8>' ... # REFUSED: v_sad_hi_u8 / no such handler ``` The smallest reproduction is the one the existing `tests/startup_gfx2hlsl_sad_check.py` already uses for `v_sad_u8`: one program with four lanes per form computing `v_sad_*` into a lane, exported through a colour target. That fixture, extended with the two forms, is the acceptance vehicle. ## Expected behavior - `v_sad_hi_u8 dst, src0, src1, src2` lowers to the `sad_hi_u8` helper and the form is recorded in `uses_sad` like the other three. - `v_msad_u8` lowers to an `msad_u8` helper that skips each byte lane whose `src1` byte is zero, matching the `skip_zero` argument `sad4_u8` already takes in the AOT table. - Both keep the existing modulo-2^32 addition of `src2`, as `sad_u8` does. ## Acceptance A new pure-Python check in the style of `tests/startup_gfx2hlsl_sad_check.py` (encode, translate, compare against a reference computed from the AOT formulas for both forms), registered as a CTest, plus `tests/startup_translator_check.py` and `tests/form_coverage_check.py` still passing. Proposed selection: `ctest --preset dev --no-tests=error -R '^startup_gfx2hlsl_sad'`. ## NOT established - **Which byte set `hi` selects.** The AOT table computes the full four-byte SAD and shifts the result left 16 (`sad4_u8 << 16`), which is not the same thing as taking only the high byte of each 16-bit word. This issue does not decide which is the hardware-correct reading; a lowering that mirrors `pixel_aot` is internally consistent with the repository's reference but still needs a hardware or WARP fixture (or an ISA quote specific to V_SAD_HI_U8) before anyone claims the register value matches the console. - Whether the two forms appear in the title's shader census at all; this is filed from the coverage gap, not from a refused title program.
Sign in to join this conversation.
No labels
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.

Reference
R0Y00/Xbox2Windows#2724
No description provided.