pixel_aot: lower depth-comparison sampling (image_sample_c_lz) to SampleCmpLevelZero #704

Closed
opened 2026-11-22 21:41:04 +00:00 by roansystemlabs · 2 comments
roansystemlabs commented 2026-11-22 21:41:04 +00:00 (Migrated from github.com)
Contributor

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


Problem

tools/pixel_aot.py lowers image_sample and image_sample_lz, but no depth-comparison form. Every image_sample_c* stops translation. Shadow-map lookups (percentage-closer filtering) use these instructions, so a pixel shader that reads a shadow map cannot reach DXIL through the internal path today. The SPIR-V bridge has comparison sampling (#670, #676); pixel_aot does not.

I could not measure how many archive PS records stop here, because I do not have the archive. tools/scan_unsupported_shaders.py already classifies image_sample_c as MIMG, so a maintainer rescan could count it.

Proposal

One reviewable change in pixel_aot.py, with its own test:

  1. Accept image_sample_c_lz vD, v[a:a+2], s[T:T+7], s[S:S+3] dmask:0x1 dim:SQ_RSRC_IMG_2D. In the RDNA2 address layout, the depth reference comes before the coordinates, so v[a] is the reference and v[a+1:a+2] is (u, v). The result is one float in vD. I will also accept the NSA spelling if LLVM 15 prints it for gfx1013.
  2. Keep the current contract for the sampler words: the four --sampler-words must match the SGPRs exactly, as today. In addition, decode the fields that change the result of a comparison sample:
    • word 0: DEPTH_COMPARE_FUNC (bits 14:12), the X/Y clamp modes, MAX_ANISO_RATIO, FORCE_UNNORMALIZED and FILTER_MODE;
    • word 1: MIN_LOD;
    • word 2: XY_MAG_FILTER, XY_MIN_FILTER, MIP_FILTER and the LOD biases.
      The first version supports point and bilinear filtering (one filter for both min and mag), clamp-to-edge addressing, no anisotropy, normalized coordinates and the default blend filter mode. Any other value is rejected with a named error.
  3. Emit SamplerComparisonState at s0 and tex.SampleCmpLevelZero(samp, uv, ref). Also expose the decoded desktop state (D3D12_FILTER_COMPARISON_*, D3D12_COMPARISON_FUNC_*, address mode) through a helper and a comment in the HLSL, so a host can create the matching sampler. The hardware compare function maps one to one onto D3D12_COMPARISON_FUNC (NEVER … ALWAYS; D3D value = hardware value + 1), with the reference on the left: ref OP texel.
  4. A program that mixes comparison and ordinary sampling is rejected for now, because both would need s0.

The existing fixed root signature in tests/pixel_aot_vs.hlsl has one non-comparison static sampler, and a static sampler cannot carry a per-case compare function. So I would not change it. Instead, the test uses a small separate host path with a sampler descriptor heap, built from the decoded state, like tests/spirv_window/sampler_readback.cpp does for the bridge. If you prefer a different binding (a second static sampler at s1, or the translator emitting a root signature), I will follow that.

Alternatives

  • Emulate the comparison in HLSL (Load or Gather, compare, weight by hand). This needs no new sampler, but hardware PCF is cheaper and closer to what the console does. Rejected for the first version.
  • Implement image_sample_c (implicit LOD), _c_l, _c_b, _c_o and image_gather4_c together. That is a larger review. I would rather land _c_lz first; the others follow the same decoding and would be separate PRs.

Verification

Source-built synthetic programs only (written with LLVM 15 syntax, assembled in the Ubuntu job). An 8×8 R32_FLOAT depth texture with distinct values, references from a constant, from an interpolated attribute and from ALU, and all eight compare functions. The expected images come from an independent CPU model of ref OP texel (and bilinear weights for the PCF case). They do not reuse translator output. Runs on _warp and _hardware (RTX 3070, Windows 11, debug layer on), plus the pure-Python checks from AGENTS.md.

Negative controls on both adapters: LESS and GREATER swapped in the descriptor, the reference replaced by a coordinate, the reference ignored (constant), point and bilinear swapped, u and v swapped. CPU controls reject unsupported descriptor fields, dmask other than 0x1, a mixed program and an overlapping write.

Limits stated up front:

  • The census impact is not measured (no archive here).
  • Only _c_lz on 2D textures. Cube shadow maps, array slices and offsets (_o) are out of scope.
  • Border-colour clamp is not in the first version. Shadow maps often use it, so it is the next candidate.

This work is AI-assisted.

Checklist

  • I checked for an existing issue or pull request for this change. (No issue or PR mentions image_sample_c, sample_c or SampleCmp for pixel_aot. The open PRs that touch tools/pixel_aot.py, including #510 and #506, do not change comparison sampling. #670, #676 and #681 cover the SPIR-V bridge only.)
  • I did not include game files, dumps, distribution links or credentials.
<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6548147134 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/issues/710). Originally posted by [roansystemlabs](https://github.com/roansystemlabs) on 2026-11-22T21:41:04Z. --- ### Problem `tools/pixel_aot.py` lowers `image_sample` and `image_sample_lz`, but no depth-comparison form. Every `image_sample_c*` stops translation. Shadow-map lookups (percentage-closer filtering) use these instructions, so a pixel shader that reads a shadow map cannot reach DXIL through the internal path today. The SPIR-V bridge has comparison sampling (#670, #676); `pixel_aot` does not. I could not measure how many archive PS records stop here, because I do not have the archive. `tools/scan_unsupported_shaders.py` already classifies `image_sample_c` as MIMG, so a maintainer rescan could count it. ### Proposal One reviewable change in `pixel_aot.py`, with its own test: 1. Accept `image_sample_c_lz vD, v[a:a+2], s[T:T+7], s[S:S+3] dmask:0x1 dim:SQ_RSRC_IMG_2D`. In the RDNA2 address layout, the depth reference comes before the coordinates, so `v[a]` is the reference and `v[a+1:a+2]` is (u, v). The result is one float in `vD`. I will also accept the NSA spelling if LLVM 15 prints it for gfx1013. 2. Keep the current contract for the sampler words: the four `--sampler-words` must match the SGPRs exactly, as today. In addition, decode the fields that change the result of a comparison sample: - word 0: `DEPTH_COMPARE_FUNC` (bits 14:12), the X/Y clamp modes, `MAX_ANISO_RATIO`, `FORCE_UNNORMALIZED` and `FILTER_MODE`; - word 1: `MIN_LOD`; - word 2: `XY_MAG_FILTER`, `XY_MIN_FILTER`, `MIP_FILTER` and the LOD biases. The first version supports point and bilinear filtering (one filter for both min and mag), clamp-to-edge addressing, no anisotropy, normalized coordinates and the default blend filter mode. Any other value is rejected with a named error. 3. Emit `SamplerComparisonState` at `s0` and `tex.SampleCmpLevelZero(samp, uv, ref)`. Also expose the decoded desktop state (`D3D12_FILTER_COMPARISON_*`, `D3D12_COMPARISON_FUNC_*`, address mode) through a helper and a comment in the HLSL, so a host can create the matching sampler. The hardware compare function maps one to one onto `D3D12_COMPARISON_FUNC` (NEVER … ALWAYS; D3D value = hardware value + 1), with the reference on the left: `ref OP texel`. 4. A program that mixes comparison and ordinary sampling is rejected for now, because both would need `s0`. The existing fixed root signature in `tests/pixel_aot_vs.hlsl` has one non-comparison static sampler, and a static sampler cannot carry a per-case compare function. So I would not change it. Instead, the test uses a small separate host path with a sampler descriptor heap, built from the decoded state, like `tests/spirv_window/sampler_readback.cpp` does for the bridge. If you prefer a different binding (a second static sampler at `s1`, or the translator emitting a root signature), I will follow that. ### Alternatives - Emulate the comparison in HLSL (`Load` or `Gather`, compare, weight by hand). This needs no new sampler, but hardware PCF is cheaper and closer to what the console does. Rejected for the first version. - Implement `image_sample_c` (implicit LOD), `_c_l`, `_c_b`, `_c_o` and `image_gather4_c` together. That is a larger review. I would rather land `_c_lz` first; the others follow the same decoding and would be separate PRs. ### Verification Source-built synthetic programs only (written with LLVM 15 syntax, assembled in the Ubuntu job). An 8×8 `R32_FLOAT` depth texture with distinct values, references from a constant, from an interpolated attribute and from ALU, and all eight compare functions. The expected images come from an independent CPU model of `ref OP texel` (and bilinear weights for the PCF case). They do not reuse translator output. Runs on `_warp` and `_hardware` (RTX 3070, Windows 11, debug layer on), plus the pure-Python checks from `AGENTS.md`. Negative controls on both adapters: LESS and GREATER swapped in the descriptor, the reference replaced by a coordinate, the reference ignored (constant), point and bilinear swapped, `u` and `v` swapped. CPU controls reject unsupported descriptor fields, `dmask` other than 0x1, a mixed program and an overlapping write. Limits stated up front: - The census impact is not measured (no archive here). - Only `_c_lz` on 2D textures. Cube shadow maps, array slices and offsets (`_o`) are out of scope. - Border-colour clamp is not in the first version. Shadow maps often use it, so it is the next candidate. This work is AI-assisted. ### Checklist - [x] I checked for an existing issue or pull request for this change. (No issue or PR mentions `image_sample_c`, `sample_c` or `SampleCmp` for `pixel_aot`. The open PRs that touch `tools/pixel_aot.py`, including #510 and #506, do not change comparison sampling. #670, #676 and #681 cover the SPIR-V bridge only.) - [x] I did not include game files, dumps, distribution links or credentials.
R0Y00 commented 2026-11-24 06:48:47 +00:00 (Migrated from github.com)
Author
Contributor

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-22T21:55:25Z.


Please go ahead with a focused 2D _c_lz PR. Keep the existing ordinary-sampler path and give the comparison-only path explicit desktop sampler state so the test host can bind the decoded settings. Reject mixed sampling until the profile can declare both bindings. For the native instruction side, include the LLVM 15 assembly/disassembly for the reference/coordinate order and cite the descriptor-field and compare-order evidence. For the draw, distinguish LESS from GREATER, include equality where the strict and inclusive functions disagree, and use a depth neighbourhood where bilinear compare-then-filter disagrees with comparing a filtered depth. Keep the texture view, host sampler and generated HLSL under the same fixture, with an independent pixel reference.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7086415735 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/issues/710#issuecomment-7086415735). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-22T21:55:25Z. --- Please go ahead with a focused 2D `_c_lz` PR. Keep the existing ordinary-sampler path and give the comparison-only path explicit desktop sampler state so the test host can bind the decoded settings. Reject mixed sampling until the profile can declare both bindings. For the native instruction side, include the LLVM 15 assembly/disassembly for the reference/coordinate order and cite the descriptor-field and compare-order evidence. For the draw, distinguish LESS from GREATER, include equality where the strict and inclusive functions disagree, and use a depth neighbourhood where bilinear compare-then-filter disagrees with comparing a filtered depth. Keep the texture view, host sampler and generated HLSL under the same fixture, with an independent pixel reference.
R0Y00 commented 2026-11-24 06:48:54 +00:00 (Migrated from github.com)
Author
Contributor

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-23T00:47:41Z.


The focused 2D image_sample_c_lz implementation merged in #718. It keeps comparison sampler state explicit and rejects mixed ordinary/comparison sampling. The WARP and RTX 3070 readbacks cover all eight comparison functions, equality, point and bilinear neighborhoods, and the coordinate-boundary and compare/filter mismatch controls. The accepted sampler and reference domain is documented.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7088638972 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/issues/710#issuecomment-7088638972). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-23T00:47:41Z. --- The focused 2D `image_sample_c_lz` implementation merged in #718. It keeps comparison sampler state explicit and rejects mixed ordinary/comparison sampling. The WARP and RTX 3070 readbacks cover all eight comparison functions, equality, point and bilinear neighborhoods, and the coordinate-boundary and compare/filter mismatch controls. The accepted sampler and reference domain is documented.
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#704
No description provided.