Repository navigation
shader: lower pixel image descriptor size fields read as numbers - #3225
SimSalabimse wants to merge 6 commits into
Conversation
A pixel stage can read the exact GFX10 width, height and 3D depth fields of an image descriptor as numbers. Other words and uses stay refused. Fixes part of R0Y00#2022 Co-authored-by: SimSalabimse <SimSalabimse@users.noreply.github.com>
KelboLive
left a comment
There was a problem hiding this comment.
Review of PR #3225 — shader: lower pixel image descriptor size fields read as numbers
Summary
This PR adds support for lowering exact GFX10 image descriptor size fields (width, height, depth) as numeric values in the pixel stage HLSL translator. The implementation is well-scoped and correct.
Changes reviewed
tools/startup/gfx2hlsl.py: NewIMAGE_FIELDSdictionary mapping(word_index, instruction, control)→(field_name, expression). Newimage_field_expr()method to match scalar ops against known size-field patterns.image_spantracking in Stage class follows descriptor data across register moves. Helper function generation for GetDimensions calls at the Pipeline level.tests/startup_gfx2hlsl_image_desc_word_check.py: Comprehensive test suite — 10 accepted cases (width/height/depth for tex1d/tex2d/tex2da/tex3d), plus refused cases covering wrong words, wrong operations, vertex stage, buffer descriptors, m0 usage, and modified descriptor words.- Build/config files: CMakeLists.txt adds the test; .github/workflows/windows.yml includes it in CI; AGENTS.md documents the command; docs/experiments.md documents the feature with reproduction steps.
Assessment
- The implementation is well-scoped: only exact GFX10 size-field patterns are accepted, everything else stays refused.
- The
image_spantracking correctly follows descriptor data acrosss_mov_b32moves, which is important for correctness when registers are reused. - Test coverage is thorough — both positive (accepted translations) and negative (refused cases), plus the "without_field_expr" control that verifies every accepted case fails when the feature is removed.
- The comments reference Mesa radv_image.c for traceability of the field layout.
Minor notes
IMAGE_DIM_HASdictionary is defined but never used — could be removed or used as an assertion guard, though its absence doesn't affect correctness since refusal logic already handles this via per-dimension checks (e.g., refusing depth for 1D/2D/tex2da).IMAGE_DIM_NAMEdictionary is also defined but unused — same assessment as above.
Verdict: Approve
pigalot
left a comment
There was a problem hiding this comment.
I reviewed e264b8c7 merged over current main 58e3183a. The merge is clean (tree d99811ce) and git diff --cached --check is clean.
What I traced.
- The field table: it matches the GFX10 image descriptor layout. WIDTH−1 is split as word 1 bits 31:30 plus word 2 bits 11:0, HEIGHT−1 is word 2 bits 27:14, and DEPTH−1 is word 4 bits 12:0. Each
s_bfe_u32control,s_and_b32mask ands_lshr_b32shift inIMAGE_FIELDSextracts exactly that field. - The templates: each one rebuilds those bits from
GetDimensions(0), so the& 3,>> 2,& 0x3fffand<< 14/<< 30forms are right. - The CI and
AGENTS.mdedits: they only register the new check.
Finding: the image dimension is assigned per register, not per descriptor load.
-
What happens:
note_image_descriptors()scans the whole program once and gives each SGPR thedim:of any image instruction that uses it.image_word()then checks only that the register currently holds some 8-dword load. So a size-field read of one descriptor gets its dimension from an image instruction that samples a different descriptor loaded into the same registers. -
Probe: it uses the
lzfixture from your check. Before the fixture's owns_load_dwordx8 s[16:23], s[8:9], 0x0, I prepend:s_load_dwordx8 s[16:23], s[8:9], 0x20 s_waitcnt lgkmcnt(0) s_and_b32 s10, s18, 0xfffand then add
v_mov_b32_e32 v4, s10after the image instruction (splice(WORDS['lz'], ('v_mov_b32_e32 v4, s10',), 'after-image')). -
Result:
Pipeline(VS_WORDS, code, root_blob(flags=0))accepts it. It emitss10 = ((img_width_tex2d(r0) - 1u) >> 2);, and the manifest hassrv 0 tex2d table 0 32next tosrv 1 tex2d table 0 0. -
Why it matters: the descriptor at table offset 32 is never used by an image instruction, yet it is bound as a
Texture2Donly because the reload at offset 0 is sampled withdim:SQ_RSRC_IMG_2D. If that slot holds a 3D or 1D image, or a buffer, the bound view type is wrong, and the size read is wrong with it. Your docstring says "a load with no image instruction" stays refused. This is that case, reached through register reuse. -
Suggested direction (unconfirmed): key the dimension by the descriptor load the image instruction actually reads, for example
(sym[1], sym[2] + 4 * (sym[3] & ~7))from thedescsymbol at the image instruction. Then require the field read's symbol to match it. The probe above would make a good negative control.
What I ran.
python -B tests/<name>.pyforstartup_gfx2hlsl_image_desc_word_check,startup_image_sample_l_check,startup_gfx2hlsl_cselect_descnum_check,startup_gfx2hlsl_load_desc_check,startup_gfx2hlsl_scalar_m0_checkandstartup_gfx2hlsl_image_get_lod_check: all exit 0.python -B tests/startup_tools_lint_check.py tools/startup: PASS.ctest --test-dir out/build/dev --no-tests=error -j 8 -R '^startup_gfx2hlsl_' -E '(_warp|_hardware|startup_audio)': 153 of the 154 registered tests pass. The one failure is a test from another PR that was still registered in my build directory. Its file is not in this tree.
Non-blocking. The checks compare HLSL text only. No draw shows that GetDimensions(0) on the view the stand-in creates for srv N tex2d|tex1d|tex2da|tex3d returns the descriptor's size. That holds only under the base-mip contract the docs describe.
Verdict: changes requested on e264b8c7 for the per-register dimension.
Environment: Windows 11 25H2 build 26200.9974; MSVC 19.52.37029; Python 3.12.10; translator checks only, no GPU run.
CI: Build and test on Windows, Assemble and translate gfx1013 fixtures and Run platform-independent Python checks on Linux pass on e264b8c7.
This reply was AI-assisted.
R0Y00
left a comment
There was a problem hiding this comment.
I reproduced the same provenance bug in the opposite instruction order on e264b8c7 over main c7215add (trial tree 313b5535). The existing descriptor-field Python suite passes, and the following source-built pair adds a distinction a fix should preserve.
In the lz fixture, copy s18 to s28 before image_sample, which uses the descriptor from table byte offset 0. After image_sample, reload s[16:23] from table byte offset 32 and wait. Extract s_and_b32 s10, s28, 0xfff in the positive case or use s18 in the negative case, then move s10 to v4.
The copied value correctly emits img_width_tex2d(r0) and one SRV at offset 0. The reloaded s18 incorrectly emits img_width_tex2d(r1) and a second SRV at offset 32, even though no image instruction uses that descriptor. Keep the copied-descriptor case accepted and refuse the later unobserved reload. This complements the existing before-image repro and guards against a fix that rejects valid copies whenever a register is reused.
Co-authored-by: SimSalabimse <SimSalabimse@users.noreply.github.com>
A pixel size-field read uses the dimension of the descriptor load the image instruction reads. Another load into the same SGPRs does not borrow it. A copy of the sampled words still does. Co-authored-by: SimSalabimse <SimSalabimse@users.noreply.github.com>
|
After the provenance fix, a useful dimension-specific control is a non-square 3D texture with a view starting above resource mip 0. The VIctory #3613 gather test depends on the first accessible mip's dimensions, so it is a good separate example of why resource and view sizes must be distinguished. Compare the lowered |
pigalot
left a comment
There was a problem hiding this comment.
Retested 4f75193e integrated over current main 32221f1a.
Conflict with main. The merge now conflicts in tools/startup/gfx2hlsl.py, in Pipeline's SRV declaration loop. #2960 added 'texcube': 'TextureCube<float4>' to the same type map that this branch follows with the img_<component>_<kind> helper loop. For testing, I kept main's line and added this branch's helper loop under it (tree 5bf8a8d1, git diff --cached --check clean).
- That conflict is textual only. This branch's
IMAGE_DIMhas noSQ_RSRC_IMG_CUBEentry, so a cube-sampled descriptor gets no key inimage_desc_dim, and its size-field reads stay refused. - The branch needs a
mainmerge before it can land.
My changes request on e264b8c7 (dimension per register): addressed. note_image_descriptors() now keys the dimension by the descriptor load the image instruction reads, image_desc_key = (source, byte offset of the 8-dword image). It requires all eight operand words to be that load, unmodified. image_word() looks up the field read's own symbol.
- My probe, the offset-32 load into
s[16:23]before the fixture's load, read throughs_and_b32 s10, s18, 0xfff: now refused (numeric use of s10 holding descnum). It is in the check asearly. - R0Y00's order, a reload after the sample: the reloaded
s18refuses, and the copys28taken before the reload is still accepted, with one SRV,srv 0 tex2d table 0 0. Both are in the check (reloaded,kept). - The plain same-descriptor case is still accepted.
What I ran.
- Build: full
dev. - Tests:
ctest --test-dir out/build/dev --no-tests=error -j 8 -R '(^startup_gfx2hlsl_|image|cube)' -E '(_hardware$|startup_audio)'gives 346 tests: 338 pass, 0 fail and 8 are capability skips (native_image_store_exact_width_warp_*,native_image_atomic_warpandnative_image_cache_flags_cs_warp). - Lint:
python -B tests/startup_tools_lint_check.py tools/startuppasses.
Mutants of tools/startup/gfx2hlsl.py, each run against tests/startup_gfx2hlsl_image_desc_word_check.py after an unmutated baseline that passes:
| Mutant | Result |
|---|---|
image_word takes any keyed load's dimension (per-register borrowing back) |
killed (early) |
| key aligned to 4 dwords, not 8 | killed (3D depth refused) |
| two dimensions on one key keep the first | fails, though on a different assertion (used as tex2d and tex3d) |
| key drops the dword index | survives |
the 8-word same check is replaced by key is not None |
survives |
a scalar write other than a load or s_mov keeps the word's symbol in the pre-scan |
survives |
| the branch early-return is removed | survives |
the pre-scan ignores offset: |
survives |
the pre-scan s_mov drops the copy |
survives |
| the pre-scan load keeps the old words | survives |
Restored afterwards; git diff --quiet is clean.
Non-blocking. The fix itself is pinned. The pre-scan's own guards are not: each surviving mutant widens what gets a key, and no control fails.
- What would be unsound if removed:
- Dword index: the second image of an
s_load_dwordx16would borrow the first image's dimension. - The 8-word
samecheck: a mixed or modified operand would still be keyed. - Other scalar writes: a word overwritten by arithmetic would keep its descriptor symbol.
- The branch refusal: the linear pre-scan would follow a path that isn't taken.
- Dword index: the second image of an
- Controls that would pin them: a near-miss for each, for example:
- an
x16load whose two halves are sampled with differentdim:; - an image operand with one word replaced by
s_mov_b32 s17, 0; - an
s_add_u32 s16, s16, 0before the sample; - a forward
s_cbranch_scc0around the load.
- an
- The two remaining survivors only refuse more: dropping the
s_movcopy or keeping the old words.
Verdict: the per-register finding is addressed on 4f75193e. Approve on 4f75193e as reviewed. It needs a main merge for the #2960 conflict before it can land, and the merged head needs another look then. This supersedes my changes-requested review on e264b8c7.
Environment: Windows 11 25H2 build 26200.9974; MSVC 19.52.37029; Python 3.12.10; translator checks only, no GPU run.
CI: Build and test on Windows, Assemble and translate gfx1013 fixtures and Run platform-independent Python checks on Linux pass on 4f75193e.
This reply was AI-assisted.
Resolved: tools/startup/gfx2hlsl.py (Pipeline SRV declarations: kept main's texcube entry and this branch's img_<component>_<kind> helper loop).
|
I pushed a merge of current Conflict resolved. The only conflict was in What I ran on
Because I made this merge, my approval on This reply was AI-assisted. |
R0Y00
left a comment
There was a problem hiding this comment.
I reran the descriptor-word, 2D-array, 3D, image-arity and startup-tool lint checks on 735924db. The existing provenance cases passed, and the merge retains the cube declaration.
There is still an unsupported case entering the ordinary draw path. The experiment and image_field_expr both scope the lowering to views that start at resource mip 0. The generated helper always queries GetDimensions(0u, ...), while the manifest emits an ordinary texture SRV. In d3d12_draw.inl, the texture path copies the recorded SRV descriptor and passes it to CreateShaderResourceView without checking MostDetailedMip for this translation. Shader mip 0 is then the shifted view's first mip if the recorded view starts above resource mip 0. The current fixture checks the HLSL string and provenance, so it does not establish equivalence to the guest descriptor fields for that binding.
Please enforce the documented base-mip requirement for translations that read these fields, or demonstrate equivalent descriptor values for a shifted view. A source-built non-square texture bound once at resource mip 0 and once with MostDetailedMip=1 should distinguish the two. Keep the check scoped to numeric descriptor reads so ordinary sampling through a shifted view still works.
pigalot
left a comment
There was a problem hiding this comment.
Re-reviewed the merge of main into this branch at 735924db (parents 4f75193e and main 240e0fb9). git diff --check origin/main...735924db is clean.
Resolution. git show --remerge-diff 735924db shows one conflict hunk, in Pipeline SRV declarations in tools/startup/gfx2hlsl.py. The merge keeps main's 'texcube': 'TextureCube<float4>' entry from #2960 and this branch's img_<component>_<kind> helper loop. Nothing else changed. I also checked that the two sides don't meet: the branch's IMAGE_DIM map has no SQ_RSRC_IMG_CUBE. A cube descriptor therefore never enters image_desc_dim, so its size fields stay on the existing path. The new IMAGE_DIM_HAS / IMAGE_DIM_TYPE / IMAGE_DIM_READ lookups can't raise KeyError on texcube.
What I ran. I built offscreen_check in dev and release and ran ctest --test-dir out/build/<preset> --no-tests=error -R '^startup_(gfx2hlsl_.*|image_.*)$', with the _hardware tests at -j 1:
- 307 tests without hardware pass in Debug and Release
/O2, includingstartup_gfx2hlsl_image_desc_word_controlsandstartup_image_cube_controls. - 134 hardware tests pass, including
startup_image_cube_hardware.
Control and mutants on the resolved lines. Each was run against startup_gfx2hlsl_image_desc_word_controls and startup_image_cube_{controls,warp,hardware}:
| Change | Result |
|---|---|
tools/startup/gfx2hlsl.py back to main 240e0fb9 (control) |
the descriptor-word check fails ("numeric use of s10 holding descnum"); the cube tests pass |
resolution drops main's texcube SRV entry |
all three cube tests fail |
| resolution drops this branch's helper loop | the descriptor-word check fails (width lo bfe); the cube tests pass |
Restored afterwards; git diff --quiet is clean.
Open request. R0Y00's changes request on 735924db still applies, and the merge doesn't touch it. I read the code and it agrees with that request. The helper always calls GetDimensions(0u, ...) on the SRV the draw path creates from the recorded descriptor. With MostDetailedMip=1, that returns the size of the view's first mip. The descriptor's width and height fields still describe the resource's mip 0. I haven't run the shifted-view case, so this rests on reading only.
Verdict: the merge resolution is correct. No approval while R0Y00's request is open.
Environment: NVIDIA GeForce RTX 5090 driver 32.0.16.1088, Windows 11 25H2 build 26200.9974; MSVC 19.52.37029; SDK 10.0.28000.0 dxc.exe (the one CTest picked up); Python 3.12.10.
This reply was AI-assisted.
|
Thanks both. I keyed the image dimension by the descriptor load the image instruction actually reads, not by the register. The key is the source and the byte offset of that 8-dword image, and the field read's symbol has to match it. So a load at offset 32 into the same SGPRs no longer borrows the dimension from a sample of the offset-0 load. Both orders are in the check now. The offset-32 load, the wait and the Retested 4f75193 on Windows: image_desc_word passed twice, startup_gfx2hlsl_ 386/386 passed. AI-Assisted |
|
Thanks for the provenance fix. pigalot's per-register request is addressed on this head, and the field masks match the GFX10 layout and the translator's own
Two non-blocking points:
Environment: Linux, Python 3.13.5, translator checks only, no build or GPU run. This comment was AI-assisted. |
…-numeric Co-authored-by: SimSalabimse <SimSalabimse@users.noreply.github.com> # Conflicts: # tools/startup/gfx2hlsl.py
s_lshr_b32 of a size field lowers only when the descriptor word is the value being shifted. A size-field SRV is marked imagedim, and the draw path refuses that view when MostDetailedMip is not 0. Sampling a shifted view that does not read a size field is unchanged. Co-authored-by: SimSalabimse <SimSalabimse@users.noreply.github.com>
|
Retested efb89b9 on the Windows test PC (NVIDIA, 32-lane waves) after merging current main. The gfx2hlsl.py merge with the resource-heap change is resolved. Size-field reads mark the SRV passed 10/10 twice, exit 0. passed 4/4 once, exit 0. passed 388/388 once, exit 0. AI-Assisted |
Pixel shaders can now read the size fields of an image descriptor as numbers.
In gfx2hlsl, a pixel stage that uses an 8-dword image descriptor can extract width, height, and (for a 3D image) depth with the exact
s_bfe_u32,s_and_b32, ors_lshr_b32for that field. Those lower toGetDimensions(0)on the texture the image instruction already binds. Width is the usual split: word 1 bits 31:30 and word 2 bits 11:0, stored as width minus one. Height is word 2 bits 27:14. Depth is word 4 bits 12:0 and only for 3D.A whole word, any other word or field, a shift of word 2 that also keeps the resource-level bit, depth on a 1D, 2D, or 2D-array image, height on a 1D image, a modified word, and
m0still refuse, and the message says which word or op. Buffer descriptors and the compute and vertex stages are unchanged.Commands:
The new check passed. Startup lint passed. The python-linux list passed, 247 checks. On the Windows test PC, the image_desc_word tests passed twice (1/1 each, ctest exit 0).
Fixes part of #2022
AI-assisted (Cursor)