Skip to content

startup: lower cube image sampling in gfx2hlsl - #2960

Draft
legacy-code-archaeologist wants to merge 3 commits into
R0Y00:mainfrom
legacy-code-archaeologist:startup/gfx2hlsl-image-cube
Draft

legacy-code-archaeologist wants to merge 3 commits into
R0Y00:mainfrom
legacy-code-archaeologist:startup/gfx2hlsl-image-cube

Conversation

@legacy-code-archaeologist

Copy link
Copy Markdown
Contributor

Change

tools/startup/gfx2hlsl.py lowers image_sample, image_sample_lz, image_sample_l and image_sample_b on dim:SQ_RSRC_IMG_CUBE to TextureCube<float4> Sample, SampleLevel and SampleBias, and writes the manifest kind texcube. Fixes #2943.

  • Address. A gfx10 cube sample addresses (s, t, face). Mesa's prepare_cube_coords (src/amd/common/nir/ac_nir_lower_tex.c) builds s = sc/|ma| + 1.5 and t = tc/|ma| + 1.5. LLVM 15.0.7 ConstantFoldAMDGCNCubeIntrinsic defines v_cubema as twice the major coordinate. So s and t lie in [1, 2].
  • Direction. An emitted helper x2w_cube_dir rebuilds a direction whose major component is ±1. Its other components carry the per-face signs of the existing v_cubesc/v_cubetc lowering, so projecting it again returns the same face coordinates.
  • Refused by name. Cube loads, image_sample_d, image_get_lod, a16, d16, compute stages, and constant samplers with DISABLE_CUBE_WRAP (word 0 bit 28, Mesa gfx10-rsrc.json).
  • Runtime. This touches only the translator and test harness. The runtime binding of texcube views is startup: bind cube views of console-layout textures in slot 70 #2938, which is still a draft. On main, d3d12_draw.inl reads an unknown srv kind as a buffer, so this must land after startup: bind cube views of console-layout textures in slot 70 #2938. That is why this PR is a draft too. Once both land, the sentence in startup: bind cube views of console-layout textures in slot 70 #2938's docs that gfx2hlsl "does not emit that kind yet" is out of date.

Verification

Commit 44a7d7d3 on main 6ee878d8, Windows 10.0.26100.9711 (24H2), dev preset. Debug layer unavailable on both adapters.

  • ctest --preset dev --no-tests=error -R '^startup_image_cube_' -V: 3/3 passed.
    • NVIDIA Quadro M1000M (driver 32.0.15.7371): 22 reference draws matched; 17 controls and mutants each read the texel they predict.
    • WARP (Microsoft Basic Render Driver 10.0.26100.9711): the same.
  • ctest --preset dev --no-tests=error -R '^startup_image_|^startup_gfx2hlsl_cube' -j 2: 53/53 passed, no skips. The baseline before the change was 50/50.
  • python -B tests/startup_image_cube_check.py binary <llvm-mc 15>: all 16 fixture programs reassemble to the recorded words. Run locally through clang -cc1as -show-encoding of LLVM 15.0.7.
  • The full python-linux list from the workflow, through tools/run_checks_parallel.py: all ok. startup_run_sh and startup_build_rev exit 77 on Windows.
  • python -B tests/docs_links_check.py: ok.
  • With the translator change reverted, the new controls fail with image dimension SQ_RSRC_IMG_CUBE.

Evidence and follow-up

Fixture. offscreen_check --startup-image-cube binds six 2x2 R32G32B32A32_FLOAT faces as a TextureCube. Texel (x, y) of face f holds ((1 + x + 2y + 4f) / 32, x, y, f). All sources are source-built.

  • Seven programs pass (s, t, face) as constants, without v_cube*. The reference is only the address meaning, so a wrong face, sign or axis in the helper reads another texel, chosen by the desktop driver's own face selection.
  • 13 helper mutants: for each face, the u sign flipped and u/v exchanged, plus faces 2 and 3 exchanged. Each must read the texel a D3D lookup of the mutated direction gives.
  • Guest-input controls: face + 1, s and t exchanged, and the face placed in the LOD or bias dword.
  • Six v_cube chains, one per face, must match both the reference and a desktop pixel shader that samples the same direction.

Changed expectations, intended.

  • The cube refusals in startup_image_sample_l_check.py and startup_image_sample_b_check.py become acceptance checks.
  • In startup_image_3d_check.py, the 1D words with the dim field set to cube now form a valid three-register cube sample: the disassembler sizes the address from the dimension. Its unverified-dimension case therefore uses MSAA, which is still refused.

Limits (all documented in docs/experiments/startup-image-cube.md):

  • That the console's face order and orientation equal D3D's rests on LLVM's cube table matching the D3D cube face table. It is not measured on the console.
  • Only one mip is bound, so LOD and bias values on a cube are untested. The _l and _b draws only show that the extra dword is not read as a coordinate.
  • A cube-array view samples layer 0. GFX10 MIMG has no array bit, so this cannot be refused from the instruction.
  • Heap and register samplers do not validate DISABLE_CUBE_WRAP.
  • Only pixel stages are drawn.

Checklist

  • This pull request covers one reviewable behavior.
  • I included the exact command and result for every check I ran.
  • I considered a negative control or failure case for a new fixture.
  • I updated README/docs when a capability or build requirement changed.
  • I did not include game files, dumps, distribution links or secrets.

AI-assisted.

🤖 Generated with Claude Code

image_sample, image_sample_lz, image_sample_l and image_sample_b on
dim:SQ_RSRC_IMG_CUBE now lower to TextureCube<float4>, with the
manifest kind texcube. The gfx10 address (s, t, face), where
s = sc/|ma| + 1.5 (Mesa prepare_cube_coords) and ma is twice the major
coordinate (LLVM ConstantFoldAMDGCNCubeIntrinsic), is rebuilt into a
direction by an emitted helper x2w_cube_dir. Cube loads, image_sample_d,
image_get_lod, a16, d16, compute stages and constant samplers with
DISABLE_CUBE_WRAP stay refused by name.

offscreen_check --startup-image-cube binds six 2x2 faces as a
TextureCube. tests/startup_image_cube_check.py draws literal face
addresses against the address meaning, 13 helper mutants, guest-input
controls and the v_cube chain per face against a desktop TextureCube
draw. The cube cases of the sample_l and sample_b checks change from
refusal to acceptance; the 3D check's unverified-dimension case moves
to MSAA, since the same words now form a valid cube sample.

The runtime binding of texcube views is R0Y00#2938; this must land after it.

AI-assisted.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@legacy-code-archaeologist

Copy link
Copy Markdown
Contributor Author

Cross-check against the native path: on cpp-migration (f934df52), cube_axes in src/gfx1013_direct_compute.inl already rebuilds a cube direction for the direct C++ frontend. It uses the same per-face table as x2w_cube_dir here: u = 2s - 3, v = 2t - 3, +X (1, -v, -u), -X (-1, -v, u), +Y (u, 1, v), -Y (u, -1, -v), +Z (u, -v, 1), -Z (-u, -v, -1). So both translators agree on the face convention.

There are two differences, both outside what the fixture draws:

  • The native path keeps face >> 3 as a cube-array layer for a TextureCubeArray. This lowering samples layer 0 of a TextureCube.
  • For face ids 6 and 7, the native path's selects resolve to +Z and -Z. Here they map to face 5.

AI-assisted.

@KelboLive KelboLive left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

PR #2960 implements cube image sampling in gfx2hlsl. Complex translation that needs careful review.

What works

  • Address reconstruction: correct derivation from Mesa prepare_cube_coords. s,t in [1,2], face ID from
    cubesc/cubetc/cubema registers. The s-1, t-1 face positioning is exactly right.
  • x2w_cube_dir: reconstructs a direction vector whose major component is +/-1 for the face. Other components
    carry u=2(s-1.5), v=2(t-1.5) with per-face signs from the existing v_cube* lowering. Projecting again
    returns the same face coordinates. Correct for all 6 faces.
  • Refusal list is thorough: image_load/image_load_mip (cube loads not defined on TextureCube), image_sample_d
    (cube derivatives not converted), image_get_lod, a16, d16, compute stages, DISABLE_CUBE_WRAP constant sampler.
  • Clear dependency note: "this must land after #2938" for texcube runtime binding. Good practice.
  • Experiment doc startup-image-cube.md is excellent: documents the Mesa source, LLVM intrinsics, refused forms,
    and known limitations (cube-array). The 254-line gfx2hlsl.py change is well-organized.
  • Test harness adds cube support to check_startup_image_3d with a six-face TextureCube fixture.

Issues

  1. Cube-array limitation: The doc correctly notes gfx10 MIMG has no array bit so cube-array views cannot
    be distinguished from cube views from the instruction. The helper keeps face bits (id & 7) and samples
    layer 0 of a TextureCube which is wrong for cube-array. This should at least be a refuse-with-reason or
    a dev-build assertion when a cube-array descriptor is bound.
  2. DISABLE_CUBE_WRAP constant sampler: The guest filters without crossing face edges when the bit is set, but
    D3D12 TextureCube always filters across edges. The refusal is correct, but the note says "With the bit
    clear both cross" — are they guaranteed to match even without the bit? Border texel values may differ
    between the GPU-computed and the HLSL-projected direction.
  3. x2w_cube_dir projection math: u = 2(s-1.5) = 2s-3. For s in [1,2]: u in [-1, 1]. When projecting
    the direction back, does this handle the case where the major component is not unique (corner of a cube
    face)? The math assumes one component is strictly largest, which is true for face coordinates but could
    be ambiguous at face corners.
  4. Test dependency on #2938 runtime: The PR is a draft and cannot be tested end-to-end without the runtime.
    Thegfx2hlsl-only translations can be checked but the draw-time binding cannot. Consider adding a unit test
    that checks the emitted HLSL directly.

Recommendations

  • Add a refuse-with-reason for cube-array (if possible to detect from the descriptor).
  • Verify DISABLE_CUBE_WRAP equality at face corners with a test harness edge case.
  • Add inline comments near x2w_cube_dir explaining why u,v are computed as 2(s-1.5).

Overall: solid implementation. The experiment doc is thorough and the refusal cases are well-reasoned.

@KelboLive KelboLive left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

What works

  1. Comprehensive lowering of all four sample forms (image_sample,
    _lz, _l, _b) to HLSL TypeCube Sample/SampleLevel/SampleBias.
  2. Excellent experiment doc tracing Mesa's prepare_cube_coords and
    LLVM's ConstantFoldAMDGCNCubeIntrinsic, with clear enumeration of
    refused forms and limits (cube-array ambiguity, face-wrap, bias).
  3. The x2w_cube_dir helper correctly reconstructs the direction vector
    with per-face component ordering matching the standard cube layout.

Issues

  1. Compute dispatch has no texcube view binding; the refusal is
    correct but the error message format should be consistent with other
    unbound-type refusals in the manifest binding code.
  2. Cube-array is silently sampled as layer 0 (helper keeps id & 7).
    The doc notes this, but if cube-array textures appear in the title
    the silent fallback will produce wrong results. Consider adding an
    explicit refusal if the descriptor can be checked.
  3. The experiment doc mentions bias is refused outside pixel stage
    (matches 2D handling). Verify the same check exists in the code.

Recommendations

  • Verify tests/startup_image_cube_check.py covers all four lowered forms
    (Sample, SampleLevel, SampleBias) and not just implicit LOD.
  • If cube-array is expected in the title, add an explicit refusal in
    gfx2hlsl and update the experiment doc.
  • Consider adding srv binding for texcube in the startup draw dispatcher
    for a subsequent PR.

Overall

Strong implementation with excellent documentation. Clear enum of
limitations and correct refusal of derivatives. Approvable.

Resolve tools/startup/gfx2hlsl.py: keep the cube DISABLE_CUBE_WRAP
refusal and the cube direction next to main's sampler joins and its
three-coordinate get_lod guard.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@tedin7

tedin7 commented Nov 27, 2026

Copy link
Copy Markdown
Collaborator

This PR now conflicts with main. Conflicting files:

  • tools/startup/gfx2hlsl.py

Could you merge main into your branch (a merge commit, no force push needed) and resolve these? Thank you.

AI-assisted; commands listed were executed locally. (git merge-tree origin/main <head>)

@legacy-code-archaeologist

Copy link
Copy Markdown
Contributor Author

Thanks for review @tedin7
On it as soon as I get home in like 2 hours, if it is urgent someone else can take over.

Resolve tools/startup/gfx2hlsl.py: keep both uses_bvh/bvh and uses_cube_dir/texcube in Pipeline.
@legacy-code-archaeologist

Copy link
Copy Markdown
Contributor Author

@tedin7 Merged \upstream/main\ (\35f7197e) into \startup/gfx2hlsl-image-cube\ in commit \982cdfe9\ (keeping both \uses_bvh/\�vh\ and \uses_cube_dir/\ excube\ in \ ools/startup/gfx2hlsl.py).

Re-verified locally on \982cdfe9\ (\dev\ preset):

  • \cmake --build --preset dev --target offscreen_check: ok
  • \ctest --preset dev --no-tests=error -R '^startup_image_cube_' -V: 3/3 passed (\startup_image_cube_controls, \startup_image_cube_warp, \startup_image_cube_hardware\ on NVIDIA Quadro M1000M driver 32.0.15.7371 and WARP 10.0.26100.9711)
  • \ctest --preset dev --no-tests=error -R '^startup_image_|^startup_gfx2hlsl_cube|^startup_gfx2hlsl_bvh' -j 2: 59/59 passed
  • \python -B tests/docs_links_check.py: passed (70 Markdown files, 584 links)

Keeping #2960 as a draft until #2938 lands.

AI-assisted.

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.

startup: gfx2hlsl refuses cube image sampling (dim:SQ_RSRC_IMG_CUBE)

3 participants