shader: lower image_gather4_lz with repeat (wrap) addressing in gfx2hlsl (Part of #1723) #2752

Closed
tedin7 wants to merge 5 commits from upstream-pr/2760 into main
tedin7 commented 2026-11-26 06:32:39 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-26T06:32:39Z.


Part of #1723

What this unblocks: image_gather4_lz with a repeat (wrap) sampler, the same bilinear / point-mip sampler the clamp form already accepts, now translates instead of being refused with "requires normalized clamp".
Source: AMD "RDNA 2" Instruction Set Architecture reference guide, MIMG instruction IMAGE_GATHER4_LZ and the sampler (S#) clamp / wrap address fields; Microsoft D3D12 documentation, D3D12_TEXTURE_ADDRESS_MODE_WRAP and HLSL Gather.

What changed

  • gather_2d_setup takes a native_gather flag, set only by image_gather4_lz (the form lowered to the native HLSL Gather). For it, CLAMP_X and CLAMP_Y (SQ_IMG_SAMP_WORD0 bits 2:0 and 5:3) of 0 (repeat) are read as the clamp-to-last-texel value for the profile check only (gather_2d_wrap_as_clamp).
  • The static sampler is still built from the guest's own words, and translated_sampler_bindings.h already maps the field value 0 to D3D12_TEXTURE_ADDRESS_MODE_WRAP, so the footprint wraps across the edge as the repeat addressing does (D3D11.3 functional spec 22.4.2: "Only the addressing modes of the sampler are used").

What stays refused

  • Mirror, mirror-once, half-border and border clamps in X or Y.
  • Any other sampler field outside the profile (filters, LOD fields, bias, word 3).
  • image_gather4_l, which emulates the gather with texel loads and an explicit clamp: it does not call the new function and still refuses repeat.

Tests

Extends tests/startup_image_gather_lz_check.py (no new test file):

  • Controls: every X/Y mix of repeat and clamp translates and binds the guest words; 24 mirror / border words stay refused; the profile of the other callers is unchanged; a negative control that removes the substitution refuses repeat again.
  • Draw (--draw, WARP and hardware): footprints that straddle u = 0, u = 1, v = 0 and v = 1 (one axis and both, plus an out-of-range coordinate) read the texels of the opposite edge, per channel, against a CPU reference that wraps the four taps. The same draws with the clamp sampler read the clamped texels (clamp control), the clamp sampler against the repeat expectation must fail (wrong-sampler control), and the existing mutant (wrong channel) must fail.

Draw harness

tests/pixel_aot_render.cpp check_pixel_gather used one fixed clamp static sampler, so a repeat draw could never wrap. --check-pixel-gather now takes an optional smp manifest line and a vertex shader and builds the static sampler from the guest words with decode_statics (the same mapping the runtime uses in translated_sampler_bindings.h, which sends the field value 0 to WRAP); without them it keeps the clamp default, so the other callers are unchanged. The comparison gather shares the new root-signature helper. startup_image_gather_lz_check.py --draw passes the manifest line for every draw, so the clamp and wrong-sampler controls now really read with the clamp sampler.

Verification

  • python3 -B tests/startup_image_gather_lz_check.py: PASS (the repeat controls fail on origin/main)
  • python3 -B tests/startup_image_gather_c_lz_check.py, tests/startup_compute_gather_lz_check.py, tests/startup_compute_gather_l_check.py: PASS
  • Win2025 WARP dev+release (--draw): pending Stage-2.

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6620975208 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2760). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-26T06:32:39Z. --- Part of #1723 What this unblocks: `image_gather4_lz` with a repeat (wrap) sampler, the same bilinear / point-mip sampler the clamp form already accepts, now translates instead of being refused with "requires normalized clamp". Source: AMD "RDNA 2" Instruction Set Architecture reference guide, MIMG instruction IMAGE_GATHER4_LZ and the sampler (S#) clamp / wrap address fields; Microsoft D3D12 documentation, `D3D12_TEXTURE_ADDRESS_MODE_WRAP` and HLSL `Gather`. ## What changed - `gather_2d_setup` takes a `native_gather` flag, set only by `image_gather4_lz` (the form lowered to the native HLSL `Gather`). For it, CLAMP_X and CLAMP_Y (SQ_IMG_SAMP_WORD0 bits 2:0 and 5:3) of 0 (repeat) are read as the clamp-to-last-texel value for the profile check only (`gather_2d_wrap_as_clamp`). - The static sampler is still built from the guest's own words, and `translated_sampler_bindings.h` already maps the field value 0 to `D3D12_TEXTURE_ADDRESS_MODE_WRAP`, so the footprint wraps across the edge as the repeat addressing does (D3D11.3 functional spec 22.4.2: "Only the addressing modes of the sampler are used"). ## What stays refused - Mirror, mirror-once, half-border and border clamps in X or Y. - Any other sampler field outside the profile (filters, LOD fields, bias, word 3). - `image_gather4_l`, which emulates the gather with texel loads and an explicit clamp: it does not call the new function and still refuses repeat. ## Tests Extends `tests/startup_image_gather_lz_check.py` (no new test file): - Controls: every X/Y mix of repeat and clamp translates and binds the guest words; 24 mirror / border words stay refused; the profile of the other callers is unchanged; a negative control that removes the substitution refuses repeat again. - Draw (`--draw`, WARP and hardware): footprints that straddle u = 0, u = 1, v = 0 and v = 1 (one axis and both, plus an out-of-range coordinate) read the texels of the opposite edge, per channel, against a CPU reference that wraps the four taps. The same draws with the clamp sampler read the clamped texels (clamp control), the clamp sampler against the repeat expectation must fail (wrong-sampler control), and the existing mutant (wrong channel) must fail. ## Draw harness `tests/pixel_aot_render.cpp` `check_pixel_gather` used one fixed clamp static sampler, so a repeat draw could never wrap. `--check-pixel-gather` now takes an optional `smp` manifest line and a vertex shader and builds the static sampler from the guest words with `decode_statics` (the same mapping the runtime uses in `translated_sampler_bindings.h`, which sends the field value 0 to WRAP); without them it keeps the clamp default, so the other callers are unchanged. The comparison gather shares the new root-signature helper. `startup_image_gather_lz_check.py --draw` passes the manifest line for every draw, so the clamp and wrong-sampler controls now really read with the clamp sampler. ## Verification - `python3 -B tests/startup_image_gather_lz_check.py`: PASS (the repeat controls fail on `origin/main`) - `python3 -B tests/startup_image_gather_c_lz_check.py`, `tests/startup_compute_gather_lz_check.py`, `tests/startup_compute_gather_l_check.py`: PASS - Win2025 WARP dev+release (`--draw`): pending Stage-2. AI-assisted; commands listed were executed locally.
upstream-sync closed this pull request 2026-11-26 11:02:44 +00:00
tedin7 commented 2026-11-26 11:02:58 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-26T10:12:40Z.


Withdrawn: shader translation is moving to the native C++ frontend (see #870), so we are not continuing this change to the Python translator. The branch stays available if any part is useful later. Thank you to everyone who reviewed and tested it.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7189056900 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2760#issuecomment-7189056900). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-26T10:12:40Z. --- Withdrawn: shader translation is moving to the native C++ frontend (see #870), so we are not continuing this change to the Python translator. The branch stays available if any part is useful later. Thank you to everyone who reviewed and tested it.

Pull request closed

Sign in to join this conversation.
No reviewers
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!2752
No description provided.