shader: lower fixed-mip 2D texture gathers at startup #1920

Merged
avt0m4t48 merged 4 commits from upstream-pr/1927 into main 2026-11-24 21:56:24 +00:00
avt0m4t48 commented 2026-11-24 21:31:03 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by avt0m4t48 on 2026-11-24T21:31:03Z.


Refs #1802. Lower the reported image_gather4_lz 2D pixel instruction for a validated constant sampler. The one-hot mask selects the texture channel, and the four taps fill four contiguous registers. The fixed guest level and Direct3D Gather both select mip zero of the bound texture view, including an SRV starting at a later resource mip. Implicit, explicit-LOD, comparison and offset gathers remain refused.

The Linux encoded test fails on the upstream translator and passes with the patch. LLVM 15 reassembles all four channel cases. Linux DXC and dxv validated the generated DXIL. Windows WARP with debug enabled matched every channel in nine 64-pixel cases, and changing the first tap made the check fail at (0,0). Windows FXC also compiled all four exact startup requests to ps_5_1. Windows verification

ctest --preset release --no-tests=error -R '^startup_image_gather_lz(_warp|_hardware)?$' -V after building offscreen_check. Physical GPU and application sampling results remain open. Source-built inputs only.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6591815446 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1927). Originally posted by [avt0m4t48](https://github.com/avt0m4t48) on 2026-11-24T21:31:03Z. --- Refs #1802. Lower the reported `image_gather4_lz` 2D pixel instruction for a validated constant sampler. The one-hot mask selects the texture channel, and the four taps fill four contiguous registers. The fixed guest level and Direct3D Gather both select mip zero of the bound texture view, including an SRV starting at a later resource mip. Implicit, explicit-LOD, comparison and offset gathers remain refused. The Linux encoded test fails on the upstream translator and passes with the patch. LLVM 15 reassembles all four channel cases. Linux DXC and dxv validated the generated DXIL. Windows WARP with debug enabled matched every channel in nine 64-pixel cases, and changing the first tap made the check fail at (0,0). Windows FXC also compiled all four exact startup requests to ps_5_1. [Windows verification](https://github.com/avt0m4t48/Xbox2Windows/actions/runs/45491201874) `ctest --preset release --no-tests=error -R '^startup_image_gather_lz(_warp|_hardware)?$' -V` after building `offscreen_check`. Physical GPU and application sampling results remain open. Source-built inputs only.
upstream-sync closed this pull request 2026-11-24 22:00:54 +00:00
akimfomin commented 2026-11-24 22:01:04 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by akimfomin on 2026-11-24T21:41:04Z.


Independent physical-GPU validation of exact head 682dd4f9993e33c61ddc693671ce6975981a281f, following the outstanding hardware check in this PR.

  • RTX 4070 Ti SUPER, NVIDIA driver 32.0.16.1714, Windows 11 build 28000.3501, DXC 1.9.2609.5. The hardware adapter was explicitly selected and printed by the renderer. D3D12 debug layer was unavailable.
  • ctest --preset release --no-tests=error -R '^startup_image_gather_lz_hardware$' -V: passed. Nine synthetic draws on hardware, 64 pixels and 256 channel values each, maximum absolute error 0. The wrong-tap control was rejected at pixel (0,0), channel 0 (expected 548, got 532).
  • ctest --preset release --no-tests=error -R '^startup_image_gather_lz(_warp)?$' -V: both passed. WARP matched the same nine outputs and rejected the identical negative control. The software adapter was Microsoft Basic Render Driver 10.0.28000.3365.

I built the offscreen_check target in a local release build with LLVM-MinGW Clang and LLVM MASM 23.1.6, and used the checked-in fixture and CTest. Source files were unchanged. PortBridge checked the receiving discussion for the remaining physical-GPU coverage. Codex (GPT-6 family) prepared and ran the validation.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7145253833 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1927#issuecomment-7145253833). Originally posted by [akimfomin](https://github.com/akimfomin) on 2026-11-24T21:41:04Z. --- Independent physical-GPU validation of exact head `682dd4f9993e33c61ddc693671ce6975981a281f`, following the outstanding hardware check in this PR. - RTX 4070 Ti SUPER, NVIDIA driver 32.0.16.1714, Windows 11 build 28000.3501, DXC 1.9.2609.5. The hardware adapter was explicitly selected and printed by the renderer. D3D12 debug layer was unavailable. - `ctest --preset release --no-tests=error -R '^startup_image_gather_lz_hardware$' -V`: passed. Nine synthetic draws on hardware, 64 pixels and 256 channel values each, maximum absolute error 0. The wrong-tap control was rejected at pixel (0,0), channel 0 (expected 548, got 532). - `ctest --preset release --no-tests=error -R '^startup_image_gather_lz(_warp)?$' -V`: both passed. WARP matched the same nine outputs and rejected the identical negative control. The software adapter was Microsoft Basic Render Driver 10.0.28000.3365. I built the `offscreen_check` target in a local release build with LLVM-MinGW Clang and LLVM MASM 23.1.6, and used the checked-in fixture and CTest. Source files were unchanged. [PortBridge](https://github.com/akimfomin/PortBridge) checked the receiving discussion for the remaining physical-GPU coverage. Codex (GPT-6 family) prepared and ran the validation.
ChazY11 commented 2026-11-24 22:01:07 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by ChazY11 on 2026-11-24T22:00:58Z.


Post-merge check: two acceptances the fixture does not pin

Verifying the merged state on main (82f0db65):

out/pe-env/Scripts/python.exe -B tests/startup_image_gather_lz_check.py
Four fixed-mip gather channels and unsupported encodings checked      exit 0

Four mutations to op_image_gather4_lz in tools/startup/gfx2hlsl.py, each reverted:

mutation result
'dmask:0x1': 'Red' ↔ 'dmask:0x2': 'Green' caught
self.emit('… .Gather%s …') → GatherRed hard-coded caught
check_gather_sampler(words, 1, comparison=False) → (words, 0, …) not caught
dst[2] - dst[1] != 3 or dst[2] > 255 dropped from the destination check not caught

What the second survivor disables, exactly

check_gather_sampler line 252:

or w1 & ~0x00fff000 or (w1 >> 12) < ((mips - 1) << 8)

With mips = 1 the bound is 0, so MAX_LOD must be at or above the last visible mip — the reason the call passes 1 rather than the guest level. With mips = 0 the bound becomes (0 - 1) << 8 = -256, and since w1 >> 12 is non-negative the comparison is always false: the MAX_LOD sanity check disappears entirely. A sampler that clamps sampling to an earlier mip than the _lz instruction implies would then translate as if it agreed with the guest.

The fixture has no case with MAX_LOD below the bound, so nothing notices.

The other survivor

Dropping dst[2] - dst[1] != 3 or dst[2] > 255 accepts a destination that is not four contiguous registers, e.g. v[5:6]. The four set_v writes then run past the declared range (for i, component in enumerate('xyzw', dst[1])), silently clobbering neighbouring VGPRs instead of refusing.

Suggested cases, both refusals only

# MAX_LOD below the last visible mip must be refused, not accepted as if it matched.
with self.assertRaisesRegex(Unsupported, "Unsupported gather sampler"):
    translate(gather_with_sampler_word1(0x00000000))     # MAX_LOD 0

# The destination must be four contiguous registers.
with self.assertRaisesRegex(Unsupported, "four destination registers"):
    translate(gather_with_dest("v[5:6]"))

Both are cheap and neither needs a device.

What is good about the merged change

The channel map, the one-hot dmask requirement, the constant-sampler requirement and the dim:/modifier whitelist are all pinned by the mutations above — the first two are what I could break and they failed loudly. The comment at lines 1241–1242 ("both the guest _lz and D3D Gather select mip zero of the bound view") is exactly the kind of note a later reader needs, and it is why the 1 argument is worth defending with a case.

Verdict

Merged and working for the accepted forms; two refusal paths are unguarded. Reporting rather than opening a change on top of a fresh merge — say the word and I will send the two cases.

  • AI-assisted.
<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7145688618 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1927#issuecomment-7145688618). Originally posted by [ChazY11](https://github.com/ChazY11) on 2026-11-24T22:00:58Z. --- ## Post-merge check: two acceptances the fixture does not pin Verifying the merged state on `main` (`82f0db65`): ```powershell out/pe-env/Scripts/python.exe -B tests/startup_image_gather_lz_check.py Four fixed-mip gather channels and unsupported encodings checked exit 0 ``` Four mutations to `op_image_gather4_lz` in `tools/startup/gfx2hlsl.py`, each reverted: | mutation | result | |---|---| | `'dmask:0x1': 'Red'` ↔ `'dmask:0x2': 'Green'` | **caught** | | `self.emit('… .Gather%s …')` → `GatherRed` hard-coded | **caught** | | `check_gather_sampler(words, 1, comparison=False)` → `(words, 0, …)` | **not caught** | | `dst[2] - dst[1] != 3 or dst[2] > 255` dropped from the destination check | **not caught** | ### What the second survivor disables, exactly `check_gather_sampler` line 252: ```python or w1 & ~0x00fff000 or (w1 >> 12) < ((mips - 1) << 8) ``` With `mips = 1` the bound is `0`, so `MAX_LOD` must be at or above the last visible mip — the reason the call passes `1` rather than the guest level. With `mips = 0` the bound becomes `(0 - 1) << 8 = -256`, and since `w1 >> 12` is non-negative the comparison is always false: **the `MAX_LOD` sanity check disappears entirely**. A sampler that clamps sampling to an earlier mip than the `_lz` instruction implies would then translate as if it agreed with the guest. The fixture has no case with `MAX_LOD` below the bound, so nothing notices. ### The other survivor Dropping `dst[2] - dst[1] != 3 or dst[2] > 255` accepts a destination that is not four contiguous registers, e.g. `v[5:6]`. The four `set_v` writes then run past the declared range (`for i, component in enumerate('xyzw', dst[1])`), silently clobbering neighbouring VGPRs instead of refusing. ### Suggested cases, both refusals only ```python # MAX_LOD below the last visible mip must be refused, not accepted as if it matched. with self.assertRaisesRegex(Unsupported, "Unsupported gather sampler"): translate(gather_with_sampler_word1(0x00000000)) # MAX_LOD 0 # The destination must be four contiguous registers. with self.assertRaisesRegex(Unsupported, "four destination registers"): translate(gather_with_dest("v[5:6]")) ``` Both are cheap and neither needs a device. ## What is good about the merged change The channel map, the one-hot `dmask` requirement, the constant-sampler requirement and the `dim:`/modifier whitelist are all pinned by the mutations above — the first two are what I could break and they failed loudly. The comment at lines 1241–1242 ("both the guest `_lz` and D3D Gather select mip zero of the bound view") is exactly the kind of note a later reader needs, and it is why the `1` argument is worth defending with a case. ## Verdict Merged and working for the accepted forms; two refusal paths are unguarded. Reporting rather than opening a change on top of a fresh merge — say the word and I will send the two cases. - AI-assisted.
ChazY11 commented 2026-11-24 23:03:36 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by ChazY11 on 2026-11-24T22:01:53Z.


Correction to my previous comment: the mips mutation is equivalent, not a gap

I claimed that check_gather_sampler(words, 1, …) → (words, 0, …) disables the MAX_LOD check. Working the arithmetic
through properly, that is wrong and I should not have published it:

or (w1 >> 12) < ((mips - 1) << 8)
  • mips = 1 → bound 0, and w1 >> 12 is non-negative, so the term is never true either way.
  • mips = 0 → bound -256, so w1 >> 12 < -256 is also never true.

Both calls leave the MAX_LOD term vacuous, so the mutation changes nothing observable. The real content of
check_gather_sampler for this call site is w0 == 0x12 (normalized clamp, no truncation), w1 & ~0x00fff000 (only the
MAX_LOD/MIN_LOD fields may be set), w2 == 0x04500000 (bilinear XY, point mip) and w3 == 0 — and those are pinned:
bad_wrap and bad_min_lod in the fixture both fail on the w0 field, and the manifest assertion pins all four words.

Given the _lz form always reads mip 0, an unconstrained MAX_LOD is a defensible choice rather than a hole; if that is the
intent, the parenthesised bound is dead for every reachable value and a reader will try to work out what it excludes.

The second mutation still stands

Dropping dst[2] - dst[1] != 3 or dst[2] > 255 from the destination check is a real acceptance change: v[5:6] (two counted
registers) then reaches

for i, component in enumerate('xyzw', dst[1]):
    self.set_v(i, 'U(t.%s)' % component)

and writes v5..v8, three of which are outside the declared range — silently clobbering neighbouring VGPRs instead of refusing.
The fixture has no case for a non-quad destination, so it survives.

# Four contiguous destination registers are required.
refused(shader_with_dest('v[5:6]'), 'four destination registers')

That one is worth a case; it is the same class as the nsa address check already in controls().

  • AI-assisted.
<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7145708975 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1927#issuecomment-7145708975). Originally posted by [ChazY11](https://github.com/ChazY11) on 2026-11-24T22:01:53Z. --- ## Correction to my previous comment: the `mips` mutation is equivalent, not a gap I claimed that `check_gather_sampler(words, 1, …)` → `(words, 0, …)` disables the `MAX_LOD` check. Working the arithmetic through properly, that is wrong and I should not have published it: ```python or (w1 >> 12) < ((mips - 1) << 8) ``` - `mips = 1` → bound `0`, and `w1 >> 12` is non-negative, so the term is **never true either way**. - `mips = 0` → bound `-256`, so `w1 >> 12 < -256` is also never true. Both calls leave the `MAX_LOD` term vacuous, so the mutation changes nothing observable. The real content of `check_gather_sampler` for this call site is `w0 == 0x12` (normalized clamp, no truncation), `w1 & ~0x00fff000` (only the `MAX_LOD`/`MIN_LOD` fields may be set), `w2 == 0x04500000` (bilinear XY, point mip) and `w3 == 0` — and those *are* pinned: `bad_wrap` and `bad_min_lod` in the fixture both fail on the `w0` field, and the manifest assertion pins all four words. Given the `_lz` form always reads mip 0, an unconstrained `MAX_LOD` is a defensible choice rather than a hole; if that is the intent, the parenthesised bound is dead for every reachable value and a reader will try to work out what it excludes. ## The second mutation still stands Dropping `dst[2] - dst[1] != 3 or dst[2] > 255` from the destination check is a real acceptance change: `v[5:6]` (two counted registers) then reaches ```python for i, component in enumerate('xyzw', dst[1]): self.set_v(i, 'U(t.%s)' % component) ``` and writes `v5..v8`, three of which are outside the declared range — silently clobbering neighbouring VGPRs instead of refusing. The fixture has no case for a non-quad destination, so it survives. ```python # Four contiguous destination registers are required. refused(shader_with_dest('v[5:6]'), 'four destination registers') ``` That one is worth a case; it is the same class as the `nsa` address check already in `controls()`. - AI-assisted.
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!1920
No description provided.