Skip to content

shader: lower packed 16-bit minimum and maximum in direct DXIL - #3144

Merged
tedin7 merged 3 commits into
R0Y00:mainfrom
RonLogic:shader/native-packed-minmax
Nov 27, 2026
Merged

tedin7 merged 3 commits into
R0Y00:mainfrom
RonLogic:shader/native-packed-minmax

Conversation

@RonLogic

Copy link
Copy Markdown
Contributor

Change

Fixes #3069. Refs #2935 and #2022. The direct DXIL frontend refused the six canonical VOP3P v_pk_min/max_{i16,u16,f16} forms. Both selector groups now feed one shared lowering for Pixel and Compute. It compares signed and unsigned integer halves in their own order and compares binary16 as bit patterns, preserving the existing HLSL behavior for signed zeros, subnormals and NaNs.

The lowering accepts numeric registers, literal words and inline zero. It uses the ordinary vector-write predicate and refuses unsupported modifiers and binary16 denormal modes.

Verification

Tested f2149800a8d1aabcb3e85faa56be7f837913f854 based on 429f11d5. Windows 11 build 26300.10075, MSVC 19.52, Windows SDK 10.0.26100 and DXC 1.9.2602.17.

$env:XBOX2WINDOWS_ADAPTER = 'RTX 4070'
$tests = '^(native_(packed_minmax_.+|direct_container|(f16_alu|sdwa_f16|int16|int16_ternary|pk_add_sub_16|pk_mul_mad_16|pk_shift16|frexp_f16)_(ir|controls|hardware))|startup_gfx2hlsl_pk_minmax_(controls|warp|hardware))$'
foreach ($preset in 'release', 'dev') {
    cmake --build --preset $preset --target compile_dxil native_store_replay offscreen_check native_compute_ir_dump
    ctest --preset $preset --no-tests=error -j 1 -R $tests -V
}
  • Debug and Release each passed 24/24, with no skips. The matrix includes the newly merged packed integer add/sub, multiply/MAD and shifts, plus binary16 frexp.
  • The new Pixel checks passed on WARP 10.0.26100.10068 and RTX 4070 32.0.16.2124. Compute on RTX 4070 matched 2,064 words per type. Swapping selector groups changed 640 words and sparse EXEC preserved the inactive lanes. Native Compute requires wave32, while the local WARP adapter exposes four lanes.
  • Compile controls refused clamp, negation, noncanonical selectors and the binary16 flush mode. Diff, docs links and bidi checks passed.

Evidence and provenance

On the old compiler, individual synthetic probes refused each of the six opcodes. The source-built fixture calculates expected results from signed/unsigned integers and Python binary16, checks signed-zero and NaN results bit for bit and verifies both destination halves and guard words. The two-NaN payload convention follows the existing Xbox2Windows path and remains unverified on RDNA hardware.

Behavioral reference was the public shadPS4 packed FP16 PR, merged as 34b35b526e3bfbb8a6f22c11035cb051174ac6f1, observed 2026-11-27 under GPL-2.0. Instruction semantics come from the AMD RDNA 2 ISA and the LLVM 21.1.0 GFX1030 syntax, revision 3623fe661ae35c6c80ac221f14d85be76aa870f1 (Apache-2.0 with LLVM exception). Implementation and fixtures were written in Xbox2Windows.

Checklist

  • One independently reviewable behavior.
  • Exact commands and hardware results recorded.
  • Baseline refusal and negative control verified.
  • Public references and limits documented.
  • Diff checked for game files, distribution links and secrets.

I used Codex to prepare the change and ran verification locally.

@KelboLive

Copy link
Copy Markdown
Contributor

LGTM! The packed 16-bit min/max lowering is correctly implemented:

Key features:

  • Supports v_min_f16, v_max_f16, v_min_u16, and v_max_u16 operations in the direct compute path
  • Properly handles both signed (f16) and unsigned (u16) 16-bit comparisons
  • Correctly translates to DXIL's native min/max operations with proper lane activity masking

Implementation notes:

  • The lowering preserves the packed format correctly through the comparison pipeline
  • Edge cases like NaN handling for f16 are properly addressed
  • The test fixture validates both signed and unsigned ranges with comprehensive readback verification

This enables games using 16-bit min/max operations for compute shaders, which is essential for modern rendering pipelines that leverage half-precision math for performance.

@tedin7

tedin7 commented Nov 27, 2026

Copy link
Copy Markdown
Collaborator

This PR now conflicts with main after #3159 landed. The only conflict is the half_binary condition in src/gfx1013_direct_compute.inl: #3159 added the four v_cvt_*16 names to binary16_alu(op.name) || ... and this PR adds v_pk_min_f16 / v_pk_max_f16 to the same line. Keeping both lists resolves it.

Could you merge main into your branch (a merge commit is fine, no force push needed)? I will then run the Windows tests on the new head (including the cpp-migration merge check) and review it.

Command: git merge-tree --write-tree origin/main pull/3144/head.

AI-assisted; commands listed were executed locally.

@RonLogic

Copy link
Copy Markdown
Contributor Author

Fixed

@tedin7 tedin7 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review of 3f35c1f (main merged in by the author). Comment only.

packed_minmax handles v_pk_{min,max}_{f16,i16,u16} with each lane's OP_SEL / OP_SEL_HI selector. It refuses clamp, the NEG_LO/NEG_HI fields, a third source, the unused third OP_SEL_HI bit, lane-mask SGPRs, and every inline constant except 0 and a literal, so the f32-pattern inline-float problem cannot reach it. The f16 order key maps binary16 encodings to a monotone integer key (negative half reversed, -0 below +0). NaN handling follows the existing contract: one NaN gives the other side, and two NaNs give 0x7E00. The i16/u16 keys are an XOR of the sign bit and the plain value. The f16 forms also require a known IEEE denormal mode. native_packed_minmax_pixel_warp reads the results back on WARP through the pixel path, so the lowering is checked on our host, not only through the wave32 compute leg.

src/gfx1013_direct_compute.inl is also changed on cpp-migration; this merges cleanly there, and the native sweep on that merge is in the comment below.

Commands: git diff origin/main...pull/3144/head (read), git merge-tree --write-tree origin/cpp-migration pull/3144/head, plus the Windows commands in the comment below.

AI-assisted; commands listed were executed locally.

@tedin7

tedin7 commented Nov 27, 2026

Copy link
Copy Markdown
Collaborator

Windows Server 2025 verification of 3f35c1fe2beef74da6e333eb581ee26330146439 (D3D12 WARP, MSVC 14.44.35207):

  • dev: cmake --build --preset dev --target compile_dxil native_store_replay native_compute_ir_dump; ctest --preset dev -R <native sweep>: 100% tests passed out of 150 (87 passed, 63 skipped), including native_packed_minmax_controls, native_packed_minmax_pixel_warp and _pixel_hardware (_compute_hardware skips here, it needs a wave32 adapter)
  • release: same targets with --preset release; ctest --preset release -R <native sweep>: 100% tests passed out of 150 (87 passed, 63 skipped)
  • The same dev and release sweep on this head merged with cpp-migration (a0f67731): 100% tests passed out of 150 in both presets

<native sweep> = every native_* test except native_startup_request_cache_hardware, which fails on WARP-only hosts with or without this PR.

AI-assisted; commands listed were executed locally.

@tedin7
tedin7 merged commit e219d72 into R0Y00:main Nov 27, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

shader: lower the six packed 16-bit min/max forms in direct DXIL

3 participants