Skip to content
Merged
4 changes: 2 additions & 2 deletions docs/experiments.md
Original file line number Diff line number Diff line change
Expand Up @@ -15190,7 +15190,7 @@ For binary32 arithmetic and compare sources, the translator reads the inline int

## Startup translator: explicit-LOD image sampling

`tools/startup/gfx2hlsl.py` now lowers the gfx1013 `image_sample_l` form of a 2D or 3D texture sample to `tex.SampleLevel(smp, uv, level)`. The level is the last address register: the third for a contiguous 2D address, the fourth for 3D, and the last element of an NSA list. `tests/startup_image_sample_l_check.py` translates source-built fixtures (the words are checked against `llvm-mc`) and requires the level to come from that register; an address list `[v0, v1, v3]` must use `v3`, not the third consecutive register. Replacing the level with 0, replacing the call with implicit `Sample`, or reading the third register in every form each make the check fail. Offset (`_o`), compare (`_c_l`), gather, 1D, array, cube and multisample dimensions, an empty `dmask`, and the `unorm`, `r128`, `tfe`, `lwe`, `a16` and `d16` modifiers or any set reserved MIMG bit are refused with an `Unsupported` message; only the cache-policy bits `glc`, `slc` and `dlc` are accepted because they do not change the sampled value. The disassembler previously read `lwe` from reserved bit 14 of word 0; `llvm-mc` (19, gfx1013) encodes it in bit 17, and the check now has one `llvm-mc`-built fixture per refused modifier plus patched reserved-bit fixtures. Not measured: no draw samples a mip level on WARP or hardware through this lowering, so the level selection is checked on the generated HLSL text only. [HLSL SampleLevel](https://learn.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl-to-samplelevel)
`tools/startup/gfx2hlsl.py` now lowers the gfx1013 `image_sample_l` form of a 2D or 3D texture sample to `tex.SampleLevel(smp, uv, level)`. The level is the last address register: the third for a contiguous 2D address, the fourth for 3D, and the last element of an NSA list. `tests/startup_image_sample_l_check.py` translates source-built fixtures (the words are checked against `llvm-mc`) and requires the level to come from that register; an address list `[v0, v1, v3]` must use `v3`, not the third consecutive register. Replacing the level with 0, replacing the call with implicit `Sample`, or reading the third register in every form each make the check fail. Offset (`_o`), compare (`_c_l`), gather, 1D, array and multisample dimensions (a cube lowers since #2943, see [startup-image-cube](experiments/startup-image-cube.md)), an empty `dmask`, and the `unorm`, `r128`, `tfe`, `lwe`, `a16` and `d16` modifiers or any set reserved MIMG bit are refused with an `Unsupported` message; only the cache-policy bits `glc`, `slc` and `dlc` are accepted because they do not change the sampled value. The disassembler previously read `lwe` from reserved bit 14 of word 0; `llvm-mc` (19, gfx1013) encodes it in bit 17, and the check now has one `llvm-mc`-built fixture per refused modifier plus patched reserved-bit fixtures. Not measured: no draw samples a mip level on WARP or hardware through this lowering, so the level selection is checked on the generated HLSL text only. [HLSL SampleLevel](https://learn.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl-to-samplelevel)

## Startup translator: binary32 arithmetic

Expand All @@ -15211,7 +15211,7 @@ Not covered: inexact products. The translation writes both `v_fma_f32`, which ro

## Startup translator: biased image sampling

`tools/startup/gfx2hlsl.py` lowers the gfx1013 `image_sample_b` form in a pixel stage to `tex.SampleBias(smp, uv, bias)` for 2D, 2D-array and 3D textures. The RDNA 2 ISA (section 8.2.4, Table 43: "_B LOD BIAS, 1: lod bias, Add this BIAS to the LOD computed"; section 8.2.5: the address is `{ offset } { bias } { z-compare } { derivative } { body }` and the bias is "One Dword float") puts the bias in the FIRST address register, so `v[0:2]` is bias, u, v and the NSA list `[v3, v1, v2]` takes the bias from `v3`. LLVM 15 agrees: `IntrinsicsAMDGPU.td` gives `AMDGPUSample_b` the extra argument `llvm_anyfloat_ty "bias"` and `MIMGInstructions.td` the gfx10 opcode `0x25`. `llvm-mc` (LLVM 15, gfx1013) assembles the contiguous 2D, 2D-array, 3D and NSA forms, and the check compares every fixture against it. HLSL `SampleBias` adds the bias to the implicit mip level and documents the range -16.0 to 15.99; the ISA text states no hardware clamp, so a bias outside that range is an unverified difference. The form needs a pixel stage whose EXEC is still the entry mask with wave-uniform control flow (implicit derivatives); a uniform (scc) join or later loop does not drop that proof, while divergent EXEC or a vcc/exec-conditioned branch before the sample does (there the plain sample is lowered to level 0, which would drop the bias). It is also refused outside a pixel stage, and so are `image_sample_b_cl`, `image_sample_c_b*`, `image_sample_b_o`, `image_gather4_b`, 1D, cube and multisample dimensions, an empty `dmask`, the `unorm`, `r128`, `tfe`, `lwe`, `a16` and `d16` modifiers, set reserved MIMG bits, and a constant sampler whose LOD bias fields (word 2 bits 19:0, s6.8 and s2.4, scaled by `T#.perf_modulation` in the ISA) are not zero or whose mip filter is 0 (mipmapping disabled), or whose MIN_LOD is above its MAX_LOD. The guest clamp is kept: MIN_LOD (word 1 bits 11:0) and MAX_LOD (bits 23:12) are unsigned 4.8 fixed point (Mesa 24.0.0 `src/amd/registers/gfx10-rsrc.json`, `SQ_IMG_SAMP_WORD1`; `radeonsi/si_state.c` lines 5169-5170 encode them with `S_FIXED(x, 8)`), and the sampler of an `image_sample_b` is written as an `smpl` manifest line that the runtime decodes to `MinLOD = MIN_LOD / 256` and `MaxLOD = MAX_LOD / 256` (`src/startup_stubs/translated_sampler_bindings.h`); `smp` and `smpc` lines still leave MinLOD at 0 and MaxLOD unbounded (the next section moves `image_sample`, `_l` and `_lz` with a constant sampler to `smpl`). A sampler read from the sampler heap, including the default-sampler shortcut for a run-time heap load, or built from a register is refused for `image_sample_b`, because its LOD limits and bias cannot be shown to be irrelevant.
`tools/startup/gfx2hlsl.py` lowers the gfx1013 `image_sample_b` form in a pixel stage to `tex.SampleBias(smp, uv, bias)` for 2D, 2D-array and 3D textures. The RDNA 2 ISA (section 8.2.4, Table 43: "_B LOD BIAS, 1: lod bias, Add this BIAS to the LOD computed"; section 8.2.5: the address is `{ offset } { bias } { z-compare } { derivative } { body }` and the bias is "One Dword float") puts the bias in the FIRST address register, so `v[0:2]` is bias, u, v and the NSA list `[v3, v1, v2]` takes the bias from `v3`. LLVM 15 agrees: `IntrinsicsAMDGPU.td` gives `AMDGPUSample_b` the extra argument `llvm_anyfloat_ty "bias"` and `MIMGInstructions.td` the gfx10 opcode `0x25`. `llvm-mc` (LLVM 15, gfx1013) assembles the contiguous 2D, 2D-array, 3D and NSA forms, and the check compares every fixture against it. HLSL `SampleBias` adds the bias to the implicit mip level and documents the range -16.0 to 15.99; the ISA text states no hardware clamp, so a bias outside that range is an unverified difference. The form needs a pixel stage whose EXEC is still the entry mask with wave-uniform control flow (implicit derivatives); a uniform (scc) join or later loop does not drop that proof, while divergent EXEC or a vcc/exec-conditioned branch before the sample does (there the plain sample is lowered to level 0, which would drop the bias). It is also refused outside a pixel stage, and so are `image_sample_b_cl`, `image_sample_c_b*`, `image_sample_b_o`, `image_gather4_b`, 1D and multisample dimensions (a cube lowers since #2943, see [startup-image-cube](experiments/startup-image-cube.md)), an empty `dmask`, the `unorm`, `r128`, `tfe`, `lwe`, `a16` and `d16` modifiers, set reserved MIMG bits, and a constant sampler whose LOD bias fields (word 2 bits 19:0, s6.8 and s2.4, scaled by `T#.perf_modulation` in the ISA) are not zero or whose mip filter is 0 (mipmapping disabled), or whose MIN_LOD is above its MAX_LOD. The guest clamp is kept: MIN_LOD (word 1 bits 11:0) and MAX_LOD (bits 23:12) are unsigned 4.8 fixed point (Mesa 24.0.0 `src/amd/registers/gfx10-rsrc.json`, `SQ_IMG_SAMP_WORD1`; `radeonsi/si_state.c` lines 5169-5170 encode them with `S_FIXED(x, 8)`), and the sampler of an `image_sample_b` is written as an `smpl` manifest line that the runtime decodes to `MinLOD = MIN_LOD / 256` and `MaxLOD = MAX_LOD / 256` (`src/startup_stubs/translated_sampler_bindings.h`); `smp` and `smpc` lines still leave MinLOD at 0 and MaxLOD unbounded (the next section moves `image_sample`, `_l` and `_lz` with a constant sampler to `smpl`). A sampler read from the sampler heap, including the default-sampler shortcut for a run-time heap load, or built from a register is refused for `image_sample_b`, because its LOD limits and bias cannot be shown to be irrelevant.

`tests/startup_image_sample_b_check.py` translates source-built fixtures and, with `draw`, draws the 2D array through `offscreen_check --startup-image-2d-array-mips` (4x4, two slices, three mips whose cells all differ, point sampler with mip filter point; MAX_LOD 2 for the bias cases). Constant coordinates have no derivatives, so the draw rewrites only the `u` initialiser of the translated HLSL to `0.75 + 0.5 * (SV_Position.x - 0.5)`, an implicit LOD of 1 at the sampled pixel; the `SampleBias` call is untouched. Bias -1, 0 and +1 read mip 0, 1 and 2 on WARP with DXC and FXC. A uniform (scc) join before the sample is accepted and drawn (same mip as the plain bias -1 case); divergent EXEC or a vcc/exec-conditioned branch before the sample is refused. Negative controls: the same program with `image_sample` reads mip 1 for every case, and edited calls (bias replaced by 0, bias negated, bias and `u` exchanged, `Sample` for `SampleBias`) each read another cell. The sampler limits are discriminated by cases whose limit lies below a mip that exists: MAX_LOD 1 with bias +1 stays on mip 1 (unclamped: mip 2), MAX_LOD 0 with bias +1 stays on mip 0, MIN_LOD 2 with bias -1 reads mip 2 (unclamped: mip 0) and MIN_LOD 1 = MAX_LOD 1 with bias -1 reads mip 1; the MAX_LOD 2 bias cases cannot tell, because mip 2 is the last level. Each limit case is drawn again with an `smp` line instead of `smpl` (limits not carried) and must read the unclamped mip. `startup_sampler_comparison` checks the decoded MinLOD and MaxLOD. Replacing the bias by 0 or exchanging the bias with the first coordinate in `gfx2hlsl.py`, dropping the MaxLOD assignment in the runtime decoder, accepting a heap sampler, and the previous `gfx2hlsl.py`, each make a check fail. Not covered: a physical-adapter run (`startup_image_sample_b_hardware` is registered), a plain 2D or 3D draw (those forms are checked on the generated HLSL), fractional bias with a linear mip filter, bias outside -16 to 15.99, a fractional MIN_LOD or MAX_LOD (only whole mips are drawn; the 4.8 decode is checked without a device), a non-zero descriptor LOD bias (refused, not carried) and the limits of the other sample forms (see the next section).

Expand Down
83 changes: 83 additions & 0 deletions docs/experiments/startup-image-cube.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# Cube image sampling in gfx2hlsl (#2943)

`tools/startup/gfx2hlsl.py` lowers `image_sample`, `image_sample_lz`, `image_sample_l` and `image_sample_b` with
`dim:SQ_RSRC_IMG_CUBE` to `TextureCube<float4>` `Sample`, `SampleLevel` and `SampleBias` (outside a pixel stage the
implicit-LOD form samples level 0 and the bias form is refused, as for 2D textures), and
writes the manifest kind `texcube` for the binding. Before, the stage was refused with
`image dimension SQ_RSRC_IMG_CUBE`.

## The address

On gfx10 a cube sample takes three address dwords after any bias: the face coordinates `s` and `t` and the face
id. Mesa's `prepare_cube_coords` (`src/amd/common/nir/ac_nir_lower_tex.c`, read at Mesa 25.0.7 through the
`openbsd/xenocara` mirror, commit `d31967950e30`) builds `s = sc / |ma| + 1.5` and `t = tc / |ma| + 1.5` from
`v_cubesc`, `v_cubetc` and `v_cubema`, and a cube-array id as `8 * layer + face`. LLVM 15.0.7
`ConstantFoldAMDGCNCubeIntrinsic` (`llvm/lib/Analysis/ConstantFolding.cpp`) defines `v_cubema` as twice the signed
major coordinate, so `s` and `t` lie in [1, 2] and `s - 1`, `t - 1` are the position on the face.

The translator emits a helper `x2w_cube_dir(s, t, face)` that rebuilds a direction whose major component is +1 or
-1 for the face and whose other components are `u = 2(s - 1.5)` and `v = 2(t - 1.5)` with the signs of the per-face
`sc` and `tc` that the existing `v_cube*` lowering uses: +X (1, -v, -u), -X (-1, -v, u), +Y (u, 1, v), -Y (u, -1, -v),
+Z (u, -v, 1), -Z (-u, -v, -1). Projecting that direction again gives back `u` and `v`.

## Refused

- `image_load` and `image_load_mip` on a cube (`Load is not defined on a TextureCube`), `image_sample_d`
(cube derivatives are not converted), `image_get_lod`, `a16` and `d16`.
- A cube in a compute program: the compute dispatch has no `texcube` view binding.
- A constant sampler with `DISABLE_CUBE_WRAP` (`SQ_IMG_SAMP_WORD0` bit 28, Mesa
`src/amd/registers/gfx10-rsrc.json`): the guest then filters without crossing face edges, while a D3D12 cube
always filters across them. With the bit clear both cross.
- Compare, gather and store forms were already limited to 2D and 2D arrays and are unchanged.

## Limits

- A cube-array view is not recognisable from the instruction (gfx10 MIMG has no array bit; the descriptor sets
the dimension). The helper keeps the face bits of the id (`id & 7`) and samples layer 0 of a `TextureCube`,
which is wrong for a later layer of a cube array.
- A face id of 6 or 7 maps to face 5, a negative, NaN or huge id to face 0, and `s` or `t` outside `[1, 2]` are
assumed to clamp to `[-1, 1]` on the face edge. Mesa does not produce these for a finite non-zero direction;
clamping outside `[1, 2]` is not tested and none of these out-of-range inputs is measured.
- The implicit-LOD and bias forms take the desktop derivatives of the rebuilt direction. Inside one face these
follow the guest's face coordinates; a quad that straddles a face edge can choose another level.
- Heap and register samplers are not read at translation, so their `DISABLE_CUBE_WRAP` bit is not validated (a set
bit would filter differently at face edges), and their LOD range stays unvalidated as for 2D textures.
- Only pixel-stage draws are measured; the vertex-stage form is translated but not drawn.
- The runtime binding of a `texcube` view landed in PR #2938 (`7fcde81f`).

## Fixture

`tests/startup_image_cube_check.py` (controls, `binary <llvm-mc>`, `draw <dxc> <offscreen_check> <warp|hardware>`).
`offscreen_check --startup-image-cube` binds six 4x4 R32G32B32A32_FLOAT faces as a TextureCube whose texel (x, y)
of face f holds ((1 + x + 4y + 16f) / 128, x, y, f). The sampler is the all-zero inline sampler (point, wrap), and
every `s` and `t` is a texel centre (in `{1.125, 1.375, 1.625, 1.875}`).

- Seven programs set `(s, t, face)` as constants (`1.125` or `1.875`), without `v_cube*`, so the reference is only the
address meaning (`x = floor(4 * (s - 1))`, `y = floor(4 * (t - 1))`). The desktop driver picks the face of the
rebuilt direction itself, so a wrong face, sign, axis or `2.0 * (s - 1.5)` / `2.0 * (t - 1.5)` scale in the helper
reads another texel. Every face has a case whose texel has x != y.
- `image_sample`, `image_sample_l` and `image_sample_b` each draw one case.
- Controls: the face one higher and `s`, `t` exchanged must read those texels; `image_sample_l` and
`image_sample_b` with the face in the LOD or bias dword must read face 0.
- Fifteen translator mutants edit the emitted helper (for each face the sign of the `u` term flipped, `u` and `v`
exchanged, faces 2 and 3 exchanged, and dropping the `2.0 *` scale on `u` or `v`). Each must read the texel a D3D
cube lookup of its direction gives.
- Six programs run the `v_cube` chain (`v_cubema`, `v_cubesc`, `v_cubetc`, `v_cubeid`, `v_rcp_f32` of `|ma|` and
`v_fma_f32` with 1.5) on one direction per face. Each must match the face texel LLVM's table gives and a desktop
pixel shader that samples the same direction from the TextureCube.

Measured on 28.11.2026, commit f7da3f69 plus this change, Windows 11 24H2 (10.0.26100.9711), debug layer
unavailable on both adapters: the 22 reference draws (7 literal, 3 forms, 6 chain, 6 desktop TextureCube) matched
on NVIDIA Quadro M1000M (driver 32.0.15.7371) and on WARP (Microsoft Basic Render Driver 10.0.26100.9711), and all
19 controls and mutants read the texel they predict (`dev` and `release` `/O2`). With the translator change reverted,
the controls fail on `image dimension SQ_RSRC_IMG_CUBE`, and dropping the `2.0 *` scale on `u` fails both
`startup_image_cube_controls` and `startup_image_cube_warp`.
The 16 fixture programs reassemble to the recorded words with LLVM 15.0.7.

```powershell
ctest --preset dev --no-tests=error -R '^startup_image_cube_' -V
```

Not shown: that the console's face order and face orientation equal D3D's. That rests on LLVM's cube table being
the D3D cube face table. LOD and bias values on a cube are not tested, because only one mip is bound; the
`image_sample_l` and `image_sample_b` draws only show that the extra address dword is not read as a coordinate.
6 changes: 3 additions & 3 deletions docs/startup-runtime.md
Original file line number Diff line number Diff line change
Expand Up @@ -2601,8 +2601,8 @@ These are measured on one executable; treat them as hypotheses for others.
for a load. Before, the stage was refused and every draw through its pipeline was skipped.
Two source-built pixel stages draw one texel of a 2x2 texture with two slices on WARP and
hardware with DXC and FXC; a slice fixed to 0 and exchanged coordinates fail it
(`ctest --preset release --no-tests=error -R "^startup_image_2d_array_"`). Cube, multisampled
and 1D images are still refused. Not covered: binding a single-slice 2D view to an array declaration
(`ctest --preset release --no-tests=error -R "^startup_image_2d_array_"`). Multisampled
and 1D images are still refused here; cube sampling is lowered since #2943 (`startup_image_cube_`). Not covered: binding a single-slice 2D view to an array declaration
in the draw path.
- **2D array depth comparison.** `image_sample_c_lz` and `image_sample_c` (sampler MAX_LOD 0) with
`dim:SQ_RSRC_IMG_2D_ARRAY` take the reference, u, v and the slice as four address registers and lower to
Expand Down Expand Up @@ -2847,7 +2847,7 @@ These are measured on one executable; treat them as hypotheses for others.
mip 0 and mip 3 instead: `ctest --preset dev --no-tests=error -R '^startup_sampler_lod_' -V`.
- The startup shader translator lowers `image_sample_l` (explicit level, 2D and 3D, any `dmask`) to
`SampleLevel(sampler, uv, level)`, taking the level from the last address register. Offset, compare,
gather, clamp, cube, 1D and `unorm` forms are refused. Only the translation is checked
gather, clamp, 1D and `unorm` forms are refused; a cube lowers since #2943 ([note](experiments/startup-image-cube.md)). Only the translation is checked
(`python -B tests/startup_image_sample_l_check.py`); a WARP readback of distinct mip levels is still open.
- `image_sample_b` in a pixel stage lowers to `SampleBias(sampler, uv, bias)` for 2D, 2D-array and 3D textures; the bias is the
first address register. Offset, clamp, compare and gather forms, other stages, code after a branch, a sampler with a
Expand Down
1 change: 1 addition & 0 deletions tests/checks/pixel-binary/startup_image_cube.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
tests/startup_image_cube_check.py binary llvm-mc-15
1 change: 1 addition & 0 deletions tests/checks/python-linux/startup_image_cube.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
tests/startup_image_cube_check.py
12 changes: 12 additions & 0 deletions tests/cmake/startup_image_cube.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# gfx2hlsl cube image sampling (#2943): translation controls, then a six-face TextureCube drawn from literal face
# addresses, translator mutants and the v_cube chain, compared with the address meaning and a desktop TextureCube draw.
if(EXISTS "${INTAKE_PYTHON}" AND TARGET offscreen_check)
add_test(NAME startup_image_cube_controls COMMAND "${INTAKE_PYTHON}" -B
"${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_image_cube_check.py")
foreach(adapter IN ITEMS warp hardware)
add_test(NAME startup_image_cube_${adapter} COMMAND "${INTAKE_PYTHON}" -B
"${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_image_cube_check.py" draw "${DXC_EXECUTABLE}"
"$<TARGET_FILE:offscreen_check>" ${adapter})
set_tests_properties(startup_image_cube_${adapter} PROPERTIES TIMEOUT 300 SKIP_RETURN_CODE 77)
endforeach()
endif()
Loading