Skip to content

startup: keep the implicit LOD of a plain image_sample past a uniform join in gfx2hlsl - #2699

Merged
R0Y00 merged 6 commits into
R0Y00:mainfrom
tedin7:x2w-plain-sample-implicit-lod
Nov 26, 2026
Merged

R0Y00 merged 6 commits into
R0Y00:mainfrom
tedin7:x2w-plain-sample-implicit-lod

Conversation

@tedin7

@tedin7 tedin7 commented Nov 26, 2026 •

Copy link
Copy Markdown
Collaborator

Part of #1723

What this unblocks: a plain image_sample past a uniform branch or join in a pixel shader samples the mip the guest's derivatives pick, instead of always mip 0.

What changed

  • tools/startup/gfx2hlsl.py: after a block boundary a plain image_sample was lowered to SampleLevel(.., 0). It now lowers to the implicit-LOD Sample where the rule image_sample_b and image_get_lod already use holds: pixel stage, entry EXEC mask, wave-uniform control flow so far (uniform_cf), and no divergent back edge over the call (checked after the pass, like derivative_sites).
  • Everywhere else the call stays SampleLevel(.., 0) exactly as before. Nothing new is refused: a plain sample that translated before still translates.
  • tests/startup_image_sample_implicit_lod_check.py (registered as startup_image_sample_implicit_lod_controls): on the recorded c_lz fixture with the MIMG opcode set to image_sample: straight line, s_cbranch_scc0 and a uniform join give Sample; divergent EXEC, s_cbranch_vccz/vccnz/execz/execnz before the call, and a divergent back edge over it stay level 0.

Why it is correct
A plain image_sample takes its LOD from the coordinate derivatives of the pixel quad (RDNA 2 ISA, MIMG sample). HLSL Sample does the same. The quad runs the call together exactly when image_sample_b / image_get_lod are accepted today, so the same condition decides it here.

What stays level 0
Shaders whose EXEC is no longer the entry mask (for example the s_wqm bracket an LLVM-built pixel shader puts around a sample) and divergent control flow. Those need their own proof and are not changed here.

Verification:

  • python3 tests/startup_image_sample_implicit_lod_check.py: passes on this branch; on origin/main (fa189d2) it fails at the branch case (SampleLevel(.., 0)).
  • Sibling checks, unchanged and passing: startup_image_sample_b_check.py, startup_gfx2hlsl_image_get_lod_check.py, startup_image_sample_lod_check.py, startup_inline_sampler_check.py, startup_image_sample_c_check.py.
  • All 107 tests/startup_gfx2hlsl*check*.py and tests/startup_*pixel*check*.py run on this branch and on origin/main: identical pass/fail (the same 2 draw checks, startup_gfx2hlsl_export_null_draw_check.py and startup_gfx2hlsl_mul24_align_draw_check.py, fail on both without the Windows draw runner).
  • GPU readback of the new path: tests/startup_image_sample_b_check.py (draw, registered as startup_image_sample_b_warp / _hardware) now draws the recorded array fixture with a uniform join before a plain image_sample. It must read mip 1 (the implicit LOD from the derivatives); the mip-0 expectation, what SampleLevel(.., 0) reads, must fail on the readback (negative control).
  • Win2025 (head 904f21e plus the commit that adds that readback, default 5-slot pool), python3 x2w_pipe.py win2025 2699 --patch <diff> --no-skip-build --regex 'startup_image_sample_b|startup_image_sample_implicit_lod|startup_gfx2hlsl_image_get_lod|startup_image_sample_lod|startup_inline_sampler': dev 10/10 and release 10/10 passed (startup_image_sample_b_warp / _hardware, startup_image_sample_lod_warp / _hardware, startup_inline_sampler_warp / _hardware and the controls). An earlier run without a fresh build failed at the existing sample_b and sample_lod draws with "Expected one inline sampler in the image 3D draw" (a stale draw executable on the node, also in the untouched sample_lod draw); startup_image_sample_implicit_lod_controls passed in that run (11/11 controls and inline-sampler tests), and the rerun's slot did not list it.
  • Commands to paste: cmake --build --preset dev --target offscreen_check then ctest --preset dev --no-tests=error -R '^(startup_image_sample_b|startup_image_sample_implicit_lod|startup_image_sample_lod)' -V.

AI-assisted; commands listed were executed locally.

tedin7 and others added 2 commits November 26, 2026 03:32
… join in gfx2hlsl

A plain image_sample takes its LOD from the pixel quad's derivatives. After a block boundary it was lowered to SampleLevel(.., 0), so the guest's mip was lost. Where the rule image_sample_b and image_get_lod already use holds (pixel stage, entry EXEC mask, wave-uniform control flow, no divergent back edge over the call) it is now the implicit-LOD Sample; everywhere else it stays level 0, and nothing new is refused.
The implicit-LOD lowering of a plain image_sample after a join was only checked as HLSL text. Draw the recorded array fixture with a uniform join in front of the image_sample: it must read mip 1 (implicit LOD from the derivatives), and the mip-0 expectation, what SampleLevel(.., 0) would read, must fail. with_join_before_sample now finds any image_sample form.
@tedin7
tedin7 marked this pull request as ready for review November 26, 2026 02:48
@tedin7

tedin7 commented Nov 26, 2026

Copy link
Copy Markdown
Collaborator Author

Ready for review, @MasterDomino.

Added after the independent check: a WARP/hardware readback of the new path in startup_image_sample_b_check.py draw (plain image_sample after a uniform join reads mip 1; the mip-0 expectation must fail). Win2025 on head 904f21e plus that commit (9cc7c38): dev 10/10 and release 10/10 passed with x2w_pipe.py win2025 2699 --patch <diff> --no-skip-build --regex <image sample tests>. Details are in the PR body.

AI-assisted; commands listed were executed locally.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 26, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-26T03:02:43.205883Z 9cc7c38 Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 9cc7c38305

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread tools/startup/gfx2hlsl.py Resolved
…oop closed by an unconditional branch

A plain image_sample in such a loop ran on uneven quads on later passes. The loop-safety check now also covers backward branches of any kind that hold a wave-divergent branch.
@tedin7

tedin7 commented Nov 26, 2026

Copy link
Copy Markdown
Collaborator Author

Independent re-check of the fix pushed after Ready (7ac50ab, divergent_exit_loop), head 10c5bb0.

  • Counter-shapes: a divergent forward exit (vccnz/vccz/execz/execnz) with a loop closed by s_branch or by a uniform s_cbranch_scc* reads level 0; a loop whose exit and back edge are both uniform (scc proven by a compare) keeps the implicit-LOD Sample, so no precision is lost versus before the fix.
  • tests/startup_image_sample_implicit_lod_check.py fails on the parent of the fix (exit_7 reads Sample() and passes with it.
  • Win2025 (dev + release) on this head: x2w_pipe.py win2025 2699 --py-checks tests/startup_image_sample_implicit_lod_check.py --targets offscreen_check --regex 'startup_image_sample|implicit_lod|plain_sample': 19/19 passed.

AI-assisted; commands listed were executed locally.

@R0Y00

R0Y00 commented Nov 26, 2026

Copy link
Copy Markdown
Owner

I resolved the main conflict in 05e3fdc by sharing the DPP proof’s divergent-branch map. The control suites and FXC/DXC compilation pass for both a uniform join and a divergent loop exit. Before merging a change from explicit level zero to implicit LOD, please add one draw with distinct mip colors and derivatives that reliably choose a nonzero mip after the uniform join. The same shader with its generated Sample call forced to SampleLevel(..., 0) should read the mip-zero color. This will verify the behavior beyond the emitted call spelling.

@R0Y00 R0Y00 left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

The added CTest runs the generated gfx1013 pixel shader after a uniform join. A complete three-mip SRV and a nonzero-base SRV both selected physical mip 2 (RGBA words 3f020000). Forcing the otherwise identical Sample call to view-relative level zero selected physical mip 0 and physical mip 1 respectively (3c000000 and 3e840000). DXC and FXC both passed on Windows 11 build 26200, WARP 10.0.26100.9818 and RTX 4060 Laptop 32.0.16.1656. Debug warnings/errors were zero, and the Windows release configure registered the host and two draw checks.

@R0Y00
R0Y00 merged commit 5921876 into R0Y00:main Nov 26, 2026
5 checks passed
@MasterDomino

Copy link
Copy Markdown
Collaborator

Post-merge note on #2699 (merged as 59218765), from a review that finished after the merge.

  1. Cache revision. This changed the HLSL of programs that already translate: a plain sample past a uniform branch or join went from SampleLevel(.., 0) to Sample(..), and the branch and join cases of the new check translate on the previous main. It landed at x2w-startup-shader-19, which startup: fold s_mov_b32 copies of a constant SGPR so inline samplers translate (Part of #1723) #2708 had set four minutes earlier. Entries cached under 19 before this merge keep level 0 until the next graphics bump. gfx2hlsl: accept mask-preserving lane-mask writes in an execz block #2723 and shader: support BUF_FMT_2_10_10_10_UNORM fetch format in gfx2hlsl (Part of #1723) #2006 both need a bump to 20 anyway, so the first of them to land re-keys these entries too.
  2. @R0Y00's requested control. Locally, I replaced the generated .Sample( with .SampleLevel(..., 0) in the plain-join draw of startup_image_sample_b_check.py. WARP and the RX 9070 XT then read 3d800000,40400000,3f800000,00000000 (texel_cell(0, 0), the mip-0 colour), while the translated shader reads mip 1 (3e880000,3f800000,00000000,40000000). So the draw does tell the two apart, but the check's own negative control only flips the expectation. Adding that forced-level-0 shader to the check would pin it.
  3. Docs. The description says the s_wqm bracket stays level 0. After a uniform join, s_mov_b64 s[0:1], exec; s_wqm_b64 exec, exec; image_sample ...; s_and_b64 exec, exec, s[0:1] now emits Sample. That's fine, since WQM makes the derivatives valid, but the wording should match.

Checked and still level 0: divergent EXEC, a discard-style s_andn2_b64 exec, exec, vcc after the join, vcc/exec branches, divergent back edges, and divergent exits closed by s_branch or a uniform s_cbranch_scc0.

Pre-existing: the derivative_sites check for image_sample_b / image_get_lod still uses only divergent_back_edges, so the forward-exit loop shape Codex found likely applies to them as well.

Runs (Windows 11 build 26300; RX 9070 XT, driver 32.0.32029.2010; WARP 10.0.26100.9818): ctest --preset dev -R "startup_image_sample|startup_gfx2hlsl_image|startup_inline_sampler" -V 27/27 at 05e3fdcd.

AI-assisted; commands listed were executed locally.

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.

3 participants