startup: lower image_get_lod on a 2D texture in gfx2hlsl #2311

Manually merged
github merged 2 commits from gh-pr/2311/head into gh-pr/2311/base 2026-11-27 00:14:45 +00:00
Owner

@⁠denn0x on GitHub · 2026-11-25 11:48 UTC · original
denn0x:startup/image-get-lod → main · +149 −0 · 5 files · 2 commits · merged 2026-11-25 12:18 UTC

What

image_get_lod returns [0], the clamped LOD a sample would use, and [1], the computed LOD before the clamp. This is Mesa nir_texop_lod, which ACO emits as image_get_lod; GLSL textureQueryLod x/y. tools/startup/gfx2hlsl.py had no handler, so a pixel stage that uses it was refused.

It is now lowered to HLSL CalculateLevelOfDetail and CalculateLevelOfDetailUnclamped on the same texture and sampler, in t.x and t.y.

  • Control flow: both calls take implicit derivatives, so the rule of image_sample_b applies: a pixel stage with the entry mask and no branches.
  • Sampler: the clamped value depends on the sampler's LOD limits and bias. The sampler must be a constant descriptor, under the same validation as image_sample_b: no LOD bias, mipmapping enabled, MIN_LOD <= MAX_LOD. Its limits are carried to the static sampler. A heap or register sampler is refused.
  • Accepted form: dim 2D, two address registers, dmask 0x1-0x3, and the cache-policy bits (glc, slc, dlc). Everything else is refused by name.
  • Assumption: the view's mip range is assumed to clamp the same way on both sides. This is not measured here.

Test

tests/startup_gfx2hlsl_image_get_lod_check.py (new). It is registered in CMake as startup_gfx2hlsl_image_get_lod_controls, in the Python CI job and in AGENTS.md.

The fixture is synthetic. It is the recorded c_lz stage of startup_image_sample_c_check with three changes: the MIMG opcode set to 0x60, sampler word 2 loaded from a literal (mip filter point), and for the second form dmask 0x3.

  • Lowered: dmask 0x1 and 0x3 lower to the two calls.
  • Refused (controls): dmask 0x4, a 2D array, a branch before the read, a sampler LOD bias, and a sampler with mipmapping disabled.
  • Negative control: without op_image_get_lod the stage is refused, as on main.
python -B tests/startup_gfx2hlsl_image_get_lod_check.py
image_get_lod: CalculateLevelOfDetail / Unclamped for dmask 0x1 and 0x3; dmask 0x4, a 2D array, a branch, a sampler bias and a sampler without mipmapping refused; refused without the handler

On main, the check fails with gfx2hlsl.Unsupported: image_get_lod.

The same check with draw <dxc> <fxc> also compiles the dmask 0x3 stage. Both compile with the Windows SDK 10.0.26100 compilers: dxc for ps_6_0 and fxc for ps_5_1.

  • Translator checks: every tests/startup_gfx2hlsl_*_check.py, tests/startup_image_*_check.py, startup_translator_check.py and startup_flat_param_check.py passes on this branch.
  • Build: all builds (Ninja Release, MSVC Build Tools 18, Windows 11). Device-free, so there were no GPU runs and no capability skips.

Refs #1723

**@⁠denn0x** on GitHub · 2026-11-25 11:48 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/2311) `denn0x:startup/image-get-lod` → `main` · +149 −0 · 5 files · 2 commits · merged 2026-11-25 12:18 UTC ## What `image_get_lod` returns [0], the clamped LOD a sample would use, and [1], the computed LOD before the clamp. This is Mesa `nir_texop_lod`, which ACO emits as `image_get_lod`; GLSL `textureQueryLod` x/y. `tools/startup/gfx2hlsl.py` had no handler, so a pixel stage that uses it was refused. It is now lowered to HLSL `CalculateLevelOfDetail` and `CalculateLevelOfDetailUnclamped` on the same texture and sampler, in `t.x` and `t.y`. - **Control flow:** both calls take implicit derivatives, so the rule of `image_sample_b` applies: a pixel stage with the entry mask and no branches. - **Sampler:** the clamped value depends on the sampler's LOD limits and bias. The sampler must be a constant descriptor, under the same validation as `image_sample_b`: no LOD bias, mipmapping enabled, MIN_LOD <= MAX_LOD. Its limits are carried to the static sampler. A heap or register sampler is refused. - **Accepted form:** dim 2D, two address registers, dmask 0x1-0x3, and the cache-policy bits (glc, slc, dlc). Everything else is refused by name. - **Assumption:** the view's mip range is assumed to clamp the same way on both sides. This is not measured here. ## Test `tests/startup_gfx2hlsl_image_get_lod_check.py` (new). It is registered in CMake as `startup_gfx2hlsl_image_get_lod_controls`, in the Python CI job and in AGENTS.md. The fixture is synthetic. It is the recorded `c_lz` stage of `startup_image_sample_c_check` with three changes: the MIMG opcode set to 0x60, sampler word 2 loaded from a literal (mip filter point), and for the second form dmask 0x3. - **Lowered:** dmask 0x1 and 0x3 lower to the two calls. - **Refused (controls):** dmask 0x4, a 2D array, a branch before the read, a sampler LOD bias, and a sampler with mipmapping disabled. - **Negative control:** without `op_image_get_lod` the stage is refused, as on `main`. ``` python -B tests/startup_gfx2hlsl_image_get_lod_check.py image_get_lod: CalculateLevelOfDetail / Unclamped for dmask 0x1 and 0x3; dmask 0x4, a 2D array, a branch, a sampler bias and a sampler without mipmapping refused; refused without the handler ``` On `main`, the check fails with `gfx2hlsl.Unsupported: image_get_lod`. The same check with `draw <dxc> <fxc>` also compiles the dmask 0x3 stage. Both compile with the Windows SDK 10.0.26100 compilers: dxc for `ps_6_0` and fxc for `ps_5_1`. - **Translator checks:** every `tests/startup_gfx2hlsl_*_check.py`, `tests/startup_image_*_check.py`, `startup_translator_check.py` and `startup_flat_param_check.py` passes on this branch. - **Build:** `all` builds (Ninja Release, MSVC Build Tools 18, Windows 11). Device-free, so there were no GPU runs and no capability skips. Refs #1723
image_get_lod returns [0], the clamped LOD a sample would use, and
[1], the computed LOD before the clamp (Mesa nir_texop_lod, which ACO
emits as image_get_lod; GLSL textureQueryLod x/y). It is lowered to
HLSL CalculateLevelOfDetail and CalculateLevelOfDetailUnclamped on the
same texture and sampler.

Both calls take implicit derivatives, so the rule of image_sample_b
applies: pixel stage, entry mask, no branches. The clamped value
depends on the sampler's LOD limits and bias, so the sampler must be a
constant descriptor under the same validation as image_sample_b (no
bias, mipmapping enabled, MIN_LOD <= MAX_LOD), with its limits carried
to the static sampler. Only dim 2D, two address registers, dmask
0x1-0x3 and the cache-policy bits are accepted.

Test: tests/startup_gfx2hlsl_image_get_lod_check.py (synthetic fixture
derived from the recorded c_lz stage, MIMG opcode 0x60). Controls:
dmask 0x4, a 2D array, a branch, a sampler bias and a sampler with
mipmapping disabled are refused; negative control: the stage is
refused without op_image_get_lod, as on main.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-27 00:14:44 +00:00
github manually merged commit 752af52358 into gh-pr/2311/base 2026-11-27 00:14:45 +00:00
Author
Owner

@⁠RavenCurseBurg on GitHub · 2026-11-25 12:04 UTC · original

Tested 96b7468e on RTX 4060 (32.0.15.9579), Intel UHD 770 (32.0.101.5973) and WARP (10.0.26100.9711). The startup translator selected the correct channels for dmask 1, 2, and 3 on all three backends. Each adapter passed nine 8×8 draws. Every strict fixture comparison exited 0; all 64 RGBA32F pixels per draw matched the expected vector within 1/256.

I kept the fixture’s descriptor and query and used LLVM 15.0.7 to assemble a packed-position input instead of the two constant coordinates. The synthetic stage unpacks unsigned X and Y, converts each to float and multiplies by 1/16, 1/8 or 1/4. On the 8×8 one-mip view the clamped reference is zero and the three raw references are −1, 0 and +1. Mask 2 packed raw into v4; mask 3 exported (clamped, raw, clamped, raw). For example, the negative-LOD case returned (0, -1, 0, -1) under mask 3. Replacing the unclamped method with the clamped method made all three adapters exit 1 and changed all 64 pixels to (0, 0, 0, 0).

The draw used --check-pixel-case, DXC 1.9.2609.5, ps_hw_inputs=(0x8000, 0x8000) and the offscreen vertex shader. Its output declares TEXCOORD0 before SV_Position, so I inserted the matching unused TEXCOORD0 in the generated input signature. The fixture bound its own point sampler. This measures channel packing and derivatives on a single-mip view; guest sampler installation and multi-mip view limits remain open.

**@⁠RavenCurseBurg** on GitHub · 2026-11-25 12:04 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/2311#issuecomment-7161891901) Tested 96b7468e on RTX 4060 (32.0.15.9579), Intel UHD 770 (32.0.101.5973) and WARP (10.0.26100.9711). The startup translator selected the correct channels for dmask 1, **2**, and 3 on all three backends. Each adapter passed nine 8×8 draws. Every strict fixture comparison exited 0; all 64 RGBA32F pixels per draw matched the expected vector within 1/256. I kept the fixture’s descriptor and query and used LLVM 15.0.7 to assemble a packed-position input instead of the two constant coordinates. The synthetic stage unpacks unsigned X and Y, converts each to float and multiplies by 1/16, 1/8 or 1/4. On the 8×8 one-mip view the clamped reference is zero and the three raw references are −1, 0 and +1. Mask 2 packed raw into v4; mask 3 exported `(clamped, raw, clamped, raw)`. For example, the negative-LOD case returned `(0, -1, 0, -1)` under mask 3. Replacing the unclamped method with the clamped method made all three adapters exit 1 and changed all 64 pixels to `(0, 0, 0, 0)`. The draw used `--check-pixel-case`, DXC 1.9.2609.5, `ps_hw_inputs=(0x8000, 0x8000)` and the offscreen vertex shader. Its output declares TEXCOORD0 before SV_Position, so I inserted the matching unused TEXCOORD0 in the generated input signature. The fixture bound its own point sampler. This measures channel packing and derivatives on a single-mip view; guest sampler installation and multi-mip view limits remain open.
Author
Owner

@⁠denn0x on GitHub · 2026-11-25 12:09 UTC · original

Thanks, both of you.

  • dmask 0x2 (tedin7's nit): the check now asserts that dmask 0x2 writes v4 = U(t.y); and reads no t.x. The commit is on the branch; python -B tests/startup_gfx2hlsl_image_get_lod_check.py passes.
  • Readback (RavenCurseBurg): three backends agree on channels and derivatives for dmask 1, 2 and 3, and the clamped-for-unclamped mutation turns every pixel to zero. That covers the gap the text check leaves.
  • Still open: as you note, this does not measure the guest sampler installation or the mip limits of a multi-mip view. That stays an assumption in the PR text.
**@⁠denn0x** on GitHub · 2026-11-25 12:09 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/2311#issuecomment-7162000219) Thanks, both of you. - **dmask 0x2 (tedin7's nit):** the check now asserts that dmask 0x2 writes `v4 = U(t.y);` and reads no `t.x`. The commit is on the branch; `python -B tests/startup_gfx2hlsl_image_get_lod_check.py` passes. - **Readback (RavenCurseBurg):** three backends agree on channels and derivatives for dmask 1, 2 and 3, and the clamped-for-unclamped mutation turns every pixel to zero. That covers the gap the text check leaves. - **Still open:** as you note, this does not measure the guest sampler installation or the mip limits of a multi-mip view. That stays an assumption in the PR text.
Author
Owner

@⁠RavenCurseBurg on GitHub · 2026-11-25 12:46 UTC · original

I measured the two open desktop cases with the startup translator's emitted smpl line installed through create_translated_root. A synthetic gfx1013 pixel shader and an independently written HLSL shader returned the same four binary32 words in all 36 paired comparisons (12 cases on each adapter). Each draw reads one RGBA32F pixel. The source texture was 4×4 with three mips. I’m putting together a regression test for the sampler and view cases.

Mip filter SRV first/count UV step per pixel Sampler min/max Returned (clamped, raw)
Point 0/3 1 0, 2 (2, 2)
Point 0/3 1 0, 0.75 (1, 2)
Point 1/2 1 0, 2 (1, 1)
Point 1/1 1 0, 2 (0, 1)
Point 0/1 1 0, 2 (0, 2)
Linear 0/3 1 0, 0.75 (0.75, 2)
Linear 0/3 1/8 0.5, 2 (0.5, −1)

Changing only the manifest tag from smpl to smp in the second row restored (2, 2) on all three. The raw value followed the dimensions of the most detailed visible mip. With point mip filtering and MinLOD exactly 0.5 or 1.5, WARP and NVIDIA selected 1 and 2, while Intel selected 0 and 1. The independent shader reproduced the same split. With linear mip filtering all three returned the exact fractional MinLOD values. The native console side of the comparison remains unmeasured.

Windows 11, DXC 1.9.2609.5, RTX 4060 (32.0.15.9579), UHD 770 (32.0.101.5973), WARP (10.0.26100.9711). Measurement driver and all cases · renderer source · pinned renderer build.

**@⁠RavenCurseBurg** on GitHub · 2026-11-25 12:46 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/2311#issuecomment-7162845528) I measured the two open desktop cases with the startup translator's emitted `smpl` line installed through `create_translated_root`. A synthetic gfx1013 pixel shader and an independently written HLSL shader returned the same four binary32 words in all 36 paired comparisons (12 cases on each adapter). Each draw reads one RGBA32F pixel. The source texture was 4×4 with three mips. I’m putting together a regression test for the sampler and view cases. | Mip filter | SRV first/count | UV step per pixel | Sampler min/max | Returned (clamped, raw) | | --- | --- | ---: | --- | --- | | Point | 0/3 | 1 | 0, 2 | (2, 2) | | Point | 0/3 | 1 | 0, 0.75 | (1, 2) | | Point | 1/2 | 1 | 0, 2 | (1, 1) | | Point | 1/1 | 1 | 0, 2 | (0, 1) | | Point | 0/1 | 1 | 0, 2 | (0, 2) | | Linear | 0/3 | 1 | 0, 0.75 | (0.75, 2) | | Linear | 0/3 | 1/8 | 0.5, 2 | (0.5, −1) | Changing only the manifest tag from `smpl` to `smp` in the second row restored (2, 2) on all three. The raw value followed the dimensions of the most detailed **visible** mip. With point mip filtering and MinLOD exactly 0.5 or 1.5, WARP and NVIDIA selected 1 and 2, while Intel selected 0 and 1. The independent shader reproduced the same split. With linear mip filtering all three returned the exact fractional MinLOD values. The native console side of the comparison remains unmeasured. Windows 11, DXC 1.9.2609.5, RTX 4060 (32.0.15.9579), UHD 770 (32.0.101.5973), WARP (10.0.26100.9711). [Measurement driver and all cases](https://github.com/RavenCurseBurg/Xbox2Windows/blob/93110487b64af8fac4d0518f71621f314287bb10/tests/startup_get_lod_view_measure.py) · [renderer source](https://github.com/RavenCurseBurg/Xbox2Windows/blob/2a6f9133cf733bfa2f22da274cdbbd4e3f9d167c/tests/startup_image_3d.cpp) · [pinned renderer build](https://github.com/RavenCurseBurg/Xbox2Windows/actions/runs/45615469912).
Author
Owner

@⁠tedin7 on GitHub · 2026-11-25 11:50 UTC · original · reviewed

Review of head 96b7468edac87a66c80c6eaa50e6822eed583c10 (comment only, no approval).

Verdict: no P1, no P2. Logic matches the ISA/GLSL meaning (image_get_lod: x = clamped LOD, y = unclamped LOD).

Checked

  • Scope: 5 files, +147 (gfx2hlsl.py, new test, CMake, windows.yml, AGENTS.md). Title neutral, MERGEABLE, not draft.
  • CI at review time: Linux Python, gfx1013 fixtures and Windows jobs were still pending. I did not wait for them.
  • Result mapping: dmask bits map to t.x/t.y through the existing packing loop (gfx2hlsl.py ~2404-2409), so dmask 0x2 would write the unclamped LOD to the first dest register, as the ISA packs it.
  • Guards: the pixel stage, entry mask and no-branch rule is the same as image_sample_b. The sampler must be constant and is validated with lod_sampler_words, with its LOD limits carried.

Commands run locally (worktree at the head SHA)

  • python3 -B tests/startup_gfx2hlsl_image_get_lod_check.py -> PASS
  • python3 -B tests/startup_image_sample_c_check.py (sibling fixture) -> PASS
  • python3 tests/docs_links_check.py -> 18 files, 446 links OK
  • python3 tests/bidi_controls_check.py -> OK
  • git diff --check origin/main...HEAD -> clean
  • Mutation 1: second call changed from CalculateLevelOfDetailUnclamped to CalculateLevelOfDetail -> test FAILS (killed).
  • Mutation 2: dropped the dmask & ~3 guard -> test FAILS (accepted, expected 'image_get_lod form', killed).
  • Both mutations reverted with git checkout -- ..
  • I did not run the Windows WARP job. The call text is verified only as HLSL text on Linux.

Nits (not blocking)

  • Nit: dmask 0x2 alone is accepted by the guard but not covered by the test. A one-line control would pin the packing.
  • Nit: the test only checks the HLSL text (plus optional dxc/fxc compile via draw). For a lowering of math semantics, a D3D12 WARP readback against a CPU reference of the LOD would be stronger. The PR already states that the mip-range clamp assumption is unmeasured.

AI-assisted; commands listed were executed locally.

**@⁠tedin7** on GitHub · 2026-11-25 11:50 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/2311#pullrequestreview-6057068967) · **reviewed** Review of head `96b7468edac87a66c80c6eaa50e6822eed583c10` (comment only, no approval). **Verdict:** no P1, no P2. Logic matches the ISA/GLSL meaning (`image_get_lod`: x = clamped LOD, y = unclamped LOD). **Checked** - Scope: 5 files, +147 (gfx2hlsl.py, new test, CMake, windows.yml, AGENTS.md). Title neutral, MERGEABLE, not draft. - CI at review time: Linux Python, gfx1013 fixtures and Windows jobs were still `pending`. I did not wait for them. - Result mapping: `dmask` bits map to `t.x`/`t.y` through the existing packing loop (`gfx2hlsl.py` ~2404-2409), so dmask 0x2 would write the unclamped LOD to the first dest register, as the ISA packs it. - Guards: the pixel stage, entry mask and no-branch rule is the same as `image_sample_b`. The sampler must be constant and is validated with `lod_sampler_words`, with its LOD limits carried. **Commands run locally (worktree at the head SHA)** - `python3 -B tests/startup_gfx2hlsl_image_get_lod_check.py` -> PASS - `python3 -B tests/startup_image_sample_c_check.py` (sibling fixture) -> PASS - `python3 tests/docs_links_check.py` -> 18 files, 446 links OK - `python3 tests/bidi_controls_check.py` -> OK - `git diff --check origin/main...HEAD` -> clean - Mutation 1: second call changed from `CalculateLevelOfDetailUnclamped` to `CalculateLevelOfDetail` -> test FAILS (killed). - Mutation 2: dropped the `dmask & ~3` guard -> test FAILS (`accepted, expected 'image_get_lod form'`, killed). - Both mutations reverted with `git checkout -- .`. - I did not run the Windows WARP job. The call text is verified only as HLSL text on Linux. **Nits (not blocking)** - Nit: `dmask 0x2` alone is accepted by the guard but not covered by the test. A one-line control would pin the packing. - Nit: the test only checks the HLSL text (plus optional dxc/fxc compile via `draw`). For a lowering of math semantics, a D3D12 WARP readback against a CPU reference of the LOD would be stronger. The PR already states that the mip-range clamp assumption is unmeasured. AI-assisted; commands listed were executed locally.
This discussion has been locked. Commenting is limited to contributors.
No description provided.