D3D12.X: encode full RDNA2 S# words in dev_create_sampler_x #3248

Closed
Kalmarv wants to merge 1 commit from upstream-pr/3248 into main
Kalmarv commented 2026-11-29 00:25:41 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Kalmarv on 2026-11-29T00:25:41Z.


Encode full RDNA2 S# words in dev_create_sampler_x instead of writing [0, 0, 0, slot.index & 0xFFF] into the console sampler heap.

Direct-DXIL IMAGE_GATHER4_L* / IMAGE_GATHER4_C_L* lowering (mimg_gather4_lod in gpu/dxil/lower_image.rs) reads the console sampler's S# metadata directly from the shader-visible console sampler heap (w[0] clamp/comparison/unorm, w[1] min/max LOD, w[2] LOD bias and mip filter, w[3][31:30] border color) alongside the PC sampler heap index in w[3][11:0]. Because dev_create_sampler_x previously left w[0..2] and w[3][31:30] zeroed, heap-created samplers read back as WRAP, MAX_LOD = 0, MIP_FILTER = NONE, DEPTH_COMPARE_FUNC = NEVER, and transparent-black border whenever sampled through mimg_gather4_lod.

What is in it

  • crates/xcl/src/gpu/desc.rs: add xcl_sampler_for_desc(&D3D12_SAMPLER_DESC, u32) -> [u32; 4] encoding RDNA2 S# words (w[0..3], keeping sampler_heap_index & 0xFFF in w[3][11:0] and border colour type in w[3][31:30], saturating MaxLOD = f32::MAX to 0xFFF, and encoding point mip filter as SQ_TEX_MIP_FILTER_POINT = 1); add round-trip unit test view_tests::sampler_desc_encodes_and_decodes_back.
  • crates/xcl/src/hle/d3d12x/real.rs: wire xcl_sampler_for_desc into dev_create_sampler_x (preserving the [0, 0, 0, slot.index & 0xFFF] fallback if desc is null); add WARP test descriptor_heap_tests::dev_create_sampler_writes_ssharp_and_copy_preserves_it (verifying CopyDescriptorsSimple keeps w[0..2] and w[3][31:30] while renumbering w[3][11:0]) and end-to-end GPU test descriptor_heap_tests::gpu_heap_sampler_gather4_l_and_c_l_use_created_ssharp (verifying Direct-DXIL IMAGE_GATHER4_L explicit mip 1, IMAGE_GATHER4_C_L LESS_EQUAL, and opaque-white BORDER addressing using the S# written by dev_create_sampler_x; skips cleanly on non-wave32/no-GPU adapters).

Tested

  • Hardware / toolchain: NVIDIA GeForce RTX 5070 Ti (driver 32.0.16.1074), AMD Ryzen 9 9950X, Windows 11 Pro (build 26200), rustc 1.99.0 (b940084d7 2026-09-28), DXC 1.9 (XCL_DXC_DIR), debug for tests and release for xcl-smoke.
  • Targeted tests (cargo test -p xcl --lib -- sampler_desc_encodes_and_decodes_back dev_create_sampler_writes_ssharp_and_copy_preserves_it gpu_heap_sampler_gather4_l_and_c_l_use_created_ssharp --nocapture): 3 passed, 0 failed, 0 skipped (the WARP and GPU tests fail before the fix: IMAGE_GATHER4_L mip 1 sampled mip 0 0.1 instead of mip 1 0.75 with s_words=[0, 0, 0, 0]).
  • Full workspace (cargo test --workspace -- --test-threads=4 --skip test_title_runs_and_game_folder_is_untouched --skip uma_driver::tests::): 2589 passed, 0 failed, 49 ignored.
  • SKIPPED GPU tests on this adapter (26 total): 21 no wave64 (RTX 5070 Ti is wave32), 3 wave32 per-lane #1426 f16/cvt-f16 refusals, 2 no heap windows (opt-in XCL_HEAP_WINDOWS=1).
  • Retail title (ea3b5e4d) 150 s regression run below (0 device removals, 0 crashes, 1647 frames vs 1695 on main, 32425 MB peak commit vs 31546 MB on main).

Regression run

xcl-smoke v1
commit: f0e8fdd5c3942ddd399d5ee11384c8595c5aacff
gpu: NVIDIA GeForce RTX 5070 Ti
title: ea3b5e4d
seconds: 150
exit: 0x0
frames shown: 1695 (PresentX 1695)
presents per 2 s (last 3): 0 0 0
device removals: 0
crashes: 0
peak commit: 31546 MB
xcl-smoke v1
commit: 5a3a97c94a484d2a5a4c16b6596715f86e3cdd61
gpu: NVIDIA GeForce RTX 5070 Ti
title: ea3b5e4d
seconds: 150
exit: 0x0
frames shown: 1647 (PresentX 1647)
presents per 2 s (last 3): 0 0 0
device removals: 0
crashes: 0
peak commit: 32425 MB

Not tested

  • AMD RDNA / wave64 GPUs.
  • Custom non-black/white border colour register tables (BORDER_COLOR_TYPE == 3 custom border table pointers are mapped to the closest standard BORDER_COLOR_TYPE 0/1/2).
  • uma_driver::tests:: (skipped locally due to pre-existing 0x7C10003 exit on NVIDIA) and test_title_runs_and_game_folder_is_untouched.

AI use

  • Antigravity (Gemini) used for root-cause verification, implementation, test authoring, and local verification; all diffs, tests, and smoke outputs reviewed and run locally.
<!-- greyfinch-sync:ivall/xcl:issue:5546015454 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3248). Originally posted by [Kalmarv](https://github.com/Kalmarv) on 2026-11-29T00:25:41Z. --- Encode full RDNA2 `S#` words in `dev_create_sampler_x` instead of writing `[0, 0, 0, slot.index & 0xFFF]` into the console sampler heap. Direct-DXIL `IMAGE_GATHER4_L*` / `IMAGE_GATHER4_C_L*` lowering (`mimg_gather4_lod` in `gpu/dxil/lower_image.rs`) reads the console sampler's `S#` metadata directly from the shader-visible console sampler heap (`w[0]` clamp/comparison/unorm, `w[1]` min/max LOD, `w[2]` LOD bias and mip filter, `w[3][31:30]` border color) alongside the PC sampler heap index in `w[3][11:0]`. Because `dev_create_sampler_x` previously left `w[0..2]` and `w[3][31:30]` zeroed, heap-created samplers read back as `WRAP`, `MAX_LOD = 0`, `MIP_FILTER = NONE`, `DEPTH_COMPARE_FUNC = NEVER`, and transparent-black border whenever sampled through `mimg_gather4_lod`. **What is in it** - `crates/xcl/src/gpu/desc.rs`: add `xcl_sampler_for_desc(&D3D12_SAMPLER_DESC, u32) -> [u32; 4]` encoding RDNA2 `S#` words (`w[0..3]`, keeping `sampler_heap_index & 0xFFF` in `w[3][11:0]` and border colour type in `w[3][31:30]`, saturating `MaxLOD = f32::MAX` to `0xFFF`, and encoding point mip filter as `SQ_TEX_MIP_FILTER_POINT = 1`); add round-trip unit test `view_tests::sampler_desc_encodes_and_decodes_back`. - `crates/xcl/src/hle/d3d12x/real.rs`: wire `xcl_sampler_for_desc` into `dev_create_sampler_x` (preserving the `[0, 0, 0, slot.index & 0xFFF]` fallback if `desc` is null); add WARP test `descriptor_heap_tests::dev_create_sampler_writes_ssharp_and_copy_preserves_it` (verifying `CopyDescriptorsSimple` keeps `w[0..2]` and `w[3][31:30]` while renumbering `w[3][11:0]`) and end-to-end GPU test `descriptor_heap_tests::gpu_heap_sampler_gather4_l_and_c_l_use_created_ssharp` (verifying Direct-DXIL `IMAGE_GATHER4_L` explicit mip 1, `IMAGE_GATHER4_C_L` `LESS_EQUAL`, and opaque-white `BORDER` addressing using the `S#` written by `dev_create_sampler_x`; skips cleanly on non-wave32/no-GPU adapters). **Tested** - Hardware / toolchain: NVIDIA GeForce RTX 5070 Ti (driver 32.0.16.1074), AMD Ryzen 9 9950X, Windows 11 Pro (build 26200), `rustc 1.99.0 (b940084d7 2026-09-28)`, DXC 1.9 (`XCL_DXC_DIR`), debug for tests and release for `xcl-smoke`. - Targeted tests (`cargo test -p xcl --lib -- sampler_desc_encodes_and_decodes_back dev_create_sampler_writes_ssharp_and_copy_preserves_it gpu_heap_sampler_gather4_l_and_c_l_use_created_ssharp --nocapture`): 3 passed, 0 failed, 0 skipped (the WARP and GPU tests fail before the fix: `IMAGE_GATHER4_L mip 1` sampled mip 0 `0.1` instead of mip 1 `0.75` with `s_words=[0, 0, 0, 0]`). - Full workspace (`cargo test --workspace -- --test-threads=4 --skip test_title_runs_and_game_folder_is_untouched --skip uma_driver::tests::`): 2589 passed, 0 failed, 49 ignored. - `SKIPPED` GPU tests on this adapter (26 total): 21 `no wave64` (RTX 5070 Ti is wave32), 3 wave32 per-lane `#1426` f16/cvt-f16 refusals, 2 `no heap windows` (opt-in `XCL_HEAP_WINDOWS=1`). - Retail title (`ea3b5e4d`) 150 s regression run below (`0` device removals, `0` crashes, `1647` frames vs `1695` on `main`, `32425 MB` peak commit vs `31546 MB` on `main`). **Regression run** ``` xcl-smoke v1 commit: f0e8fdd5c3942ddd399d5ee11384c8595c5aacff gpu: NVIDIA GeForce RTX 5070 Ti title: ea3b5e4d seconds: 150 exit: 0x0 frames shown: 1695 (PresentX 1695) presents per 2 s (last 3): 0 0 0 device removals: 0 crashes: 0 peak commit: 31546 MB ``` ``` xcl-smoke v1 commit: 5a3a97c94a484d2a5a4c16b6596715f86e3cdd61 gpu: NVIDIA GeForce RTX 5070 Ti title: ea3b5e4d seconds: 150 exit: 0x0 frames shown: 1647 (PresentX 1647) presents per 2 s (last 3): 0 0 0 device removals: 0 crashes: 0 peak commit: 32425 MB ``` **Not tested** - AMD RDNA / wave64 GPUs. - Custom non-black/white border colour register tables (`BORDER_COLOR_TYPE == 3` custom border table pointers are mapped to the closest standard `BORDER_COLOR_TYPE` 0/1/2). - `uma_driver::tests::` (skipped locally due to pre-existing `0x7C10003` exit on NVIDIA) and `test_title_runs_and_game_folder_is_untouched`. **AI use** - Antigravity (Gemini) used for root-cause verification, implementation, test authoring, and local verification; all diffs, tests, and smoke outputs reviewed and run locally.
Foniass commented 2026-12-01 07:03:37 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Foniass on 2026-12-01T07:03:37Z.


merge-check
base: d0832e614f
head: 5a3a97c94a
merge: clean
build: cargo build --workspace -> ok
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 2893 passed, 0 failed, 51 ignored; SKIPPED: 28 (20 no wave64, 2 no d3d12SDKLayers.dll, 1 no D3D12 debug layer, 1 no heap windows, 1 cannot disable the driver cache, 3 others; the same set as on main); failing: none
review: read the whole diff (gpu/desc.rs +129, real.rs +265/-3, ~60 lines of runtime code). On main CreateSampler (#22) and CreateSamplerX (#61) write [0, 0, 0, idx]; now every heap sampler carries its real S# words 0-2 and border colour type. The encoding matches sampler_d3d12 bit for bit and the test round-trips through it: address modes (0/1/2/3/6), aniso ratio, compare function, min/max reduction, u4.8 min/max LOD (f32::MAX saturates to 0xFFF), s5.8 LOD bias, mag/min/Z/mip filters, border types 0/1/2; FORCE_UNNORMALIZED is never set, as D3D12 requires. Findings: (1) a change beyond the described scope: the HLSL fallback for implicit-LOD offset sampling in pixel shaders (recomp/emit.rs ~2882-2902) reads the mip filter from S# word 2; heap samplers used to read 0 and take one Sample, and linear-mip ones now take the two-mip blend (3 samples). Probably more correct, but neither described nor tested. (2) MIN_MAG_ANISOTROPIC_MIP_POINT (0x54) and its variants are encoded with a linear mip filter (desc.rs ~371), since the aniso bit forces mip = 2. (3) AI use says "Antigravity (Gemini)", not an exact model; the suite ran with --skip filters (disclosed). Not from this PR: under a collapsed sampler waterfall, mimg_gather4_lod reads words 0-2 from SGPRs the collapse no longer writes (lower_image.rs ~513-516). Fine: the null-desc fallback, CopyDescriptors keeping words 0-2 and the border type, and a GPU test that fails on main.
performance: 60 s smoke of a retail title, main d0832e61 vs this merge (28aa4bc9): 1516 -> 1664 frames shown (109 %; last presents per 2 s: 60 60 60), peak commit 18133 -> 17377 MB, 0 device removals, 0 crashes.
verdict: safe to merge (the description should mention the HLSL implicit-LOD offset path change and name the AI model; aniso-mip-point filters encode a linear mip)
hardware / tool and model: RTX 5070 Ti (driver 621.14), Ryzen 7 7800X3D, Windows 11 26300 / Claude Code with Claude Opus 5.5

AI-written and posted by Foniass without a human check of each point.

<!-- greyfinch-sync:ivall/xcl:comment:7303598148 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3248#issuecomment-7303598148). Originally posted by [Foniass](https://github.com/Foniass) on 2026-12-01T07:03:37Z. --- merge-check base: d0832e614fa7d6ae027bb4feb27ccb9121f54a8f head: 5a3a97c94a484d2a5a4c16b6596715f86e3cdd61 merge: clean build: cargo build --workspace -> ok tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 2893 passed, 0 failed, 51 ignored; SKIPPED: 28 (20 no wave64, 2 no d3d12SDKLayers.dll, 1 no D3D12 debug layer, 1 no heap windows, 1 cannot disable the driver cache, 3 others; the same set as on main); failing: none review: read the whole diff (gpu/desc.rs +129, real.rs +265/-3, ~60 lines of runtime code). On main CreateSampler (#22) and CreateSamplerX (#61) write `[0, 0, 0, idx]`; now every heap sampler carries its real S# words 0-2 and border colour type. The encoding matches `sampler_d3d12` bit for bit and the test round-trips through it: address modes (0/1/2/3/6), aniso ratio, compare function, min/max reduction, u4.8 min/max LOD (f32::MAX saturates to 0xFFF), s5.8 LOD bias, mag/min/Z/mip filters, border types 0/1/2; FORCE_UNNORMALIZED is never set, as D3D12 requires. Findings: (1) a change beyond the described scope: the HLSL fallback for implicit-LOD offset sampling in pixel shaders (recomp/emit.rs ~2882-2902) reads the mip filter from S# word 2; heap samplers used to read 0 and take one `Sample`, and linear-mip ones now take the two-mip blend (3 samples). Probably more correct, but neither described nor tested. (2) `MIN_MAG_ANISOTROPIC_MIP_POINT` (0x54) and its variants are encoded with a linear mip filter (desc.rs ~371), since the aniso bit forces mip = 2. (3) AI use says "Antigravity (Gemini)", not an exact model; the suite ran with `--skip` filters (disclosed). Not from this PR: under a collapsed sampler waterfall, `mimg_gather4_lod` reads words 0-2 from SGPRs the collapse no longer writes (lower_image.rs ~513-516). Fine: the null-desc fallback, CopyDescriptors keeping words 0-2 and the border type, and a GPU test that fails on main. performance: 60 s smoke of a retail title, main d0832e61 vs this merge (28aa4bc9): 1516 -> 1664 frames shown (109 %; last presents per 2 s: 60 60 60), peak commit 18133 -> 17377 MB, 0 device removals, 0 crashes. verdict: safe to merge (the description should mention the HLSL implicit-LOD offset path change and name the AI model; aniso-mip-point filters encode a linear mip) hardware / tool and model: RTX 5070 Ti (driver 621.14), Ryzen 7 7800X3D, Windows 11 26300 / Claude Code with Claude Opus 5.5 AI-written and posted by Foniass without a human check of each point.
stijn-boer commented 2026-12-02 18:24:19 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by stijn-boer on 2026-12-02T18:24:19Z.


merge-check
base: c53e969602
head: 5a3a97c94a
verdict: not yet: the targeted AMD descriptor and explicit-LOD gather tests passed and the workspace and smoke exits reproduced on main. The same-head review identified unverified implicit-LOD fallback behavior and anisotropic mip-point encoding in the changed sampler path.

Windows / AMD RDNA4 (native D3D12) — sampler encoding and explicit-LOD gather checks on 5a3a97c

Clean merge onto main at c53e969 (merge commit 8e2b481592b47d981fb9e62914c10e93a7e93c47). git diff --check was clean.

Setup

  • AMD Radeon RX 9060 XT (driver 32.0.32029.2010), Ryzen 7 5700X, Windows 10 21H2 build 19044.6332
  • Rust 1.100.0, debug builds, Agility SDK 1.620.2 with matching d3d12SDKLayers.dll, DXC 1.9.2609.5

Tested

cargo build --workspace passed. cargo test --workspace --no-fail-fast -- --test-threads=4 --show-output exited 101 on both builds.

Build Passed Failed Ignored SKIPPED notices
main 338 3 23 0
Merged PR 341 3 23 0

Counts are partial because two test processes aborted on each build.

  • Tests that failed on the PR and passed on main — none observed in the comparable results.
  • New focused tests — 3 passed, 0 failed, 0 ignored, 0 skip notices with --test-threads=1 --show-output --nocapture. This includes descriptor encode/decode, CreateSampler and copy preservation, and AMD gather4_l and gather4_c_l draws with the created descriptor.
  • Debug-layer coverage — both debug-layer E2E tests, including DRED, and uploads_start_from_each_subresources_state passed.
Shared failures and process aborts
  • fuzz_validate_and_gpu_1, fuzz_validate_and_gpu_3, and two_pass_driver_cache_reuses_both_passes_and_recovers_corrupt_blobs reported failures on both builds.
  • The library and gpu_compute_e2e test processes exited with 0xc0000409 on both builds.

Retail smoke

Ran scripts/smoke.ps1 -Game <local folder> -Seconds 60 -Xcl <debug binary> with XCL_UMA_DRIVER=mixed for title hash 2c508222.

Build Exit Frames logged Device removals logged Peak commit
main 0xc0000409 None 0 1320 MB
Merged PR, first attempt 0xc0000409 None 0 4911 MB
Merged PR, repeat from fresh data root 0xc0000409 None 0 1320 MB

The first run's higher startup commit did not reproduce. These exits leave frame and steady-state memory behavior unverified.

Diff review

The selected tests checked that the sampler words encode the descriptor and survive a copy, and that translated explicit-LOD gathers use the encoded sampler fields.

Not tested

Remaining workspace tests after the aborts and retail performance.

AI use

Codex (GPT-6) merged and built the PR, ran the tests, compared the results with main, reviewed the diff, and drafted this report.

<!-- greyfinch-sync:ivall/xcl:comment:7354525077 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3248#issuecomment-7354525077). Originally posted by [stijn-boer](https://github.com/stijn-boer) on 2026-12-02T18:24:19Z. --- merge-check base: c53e96960265a82477a9c98677afb0402e98d486 head: 5a3a97c94a484d2a5a4c16b6596715f86e3cdd61 verdict: not yet: the targeted AMD descriptor and explicit-LOD gather tests passed and the workspace and smoke exits reproduced on main. The [same-head review](https://github.com/xcl-dev/xcl/pull/3248#issuecomment-7303598148) identified unverified implicit-LOD fallback behavior and anisotropic mip-point encoding in the changed sampler path. **Windows / AMD RDNA4 (native D3D12) — sampler encoding and explicit-LOD gather checks on `5a3a97c`** Clean merge onto `main` at `c53e969` (merge commit `8e2b481592b47d981fb9e62914c10e93a7e93c47`). `git diff --check` was clean. **Setup** - AMD Radeon RX 9060 XT (driver 32.0.32029.2010), Ryzen 7 5700X, Windows 10 21H2 build 19044.6332 - Rust 1.100.0, debug builds, Agility SDK 1.620.2 with matching `d3d12SDKLayers.dll`, DXC 1.9.2609.5 **Tested** `cargo build --workspace` passed. `cargo test --workspace --no-fail-fast -- --test-threads=4 --show-output` exited 101 on both builds. | Build | Passed | Failed | Ignored | `SKIPPED` notices | |---|---:|---:|---:|---:| | `main` | 338 | 3 | 23 | 0 | | Merged PR | 341 | 3 | 23 | 0 | Counts are partial because two test processes aborted on each build. - **Tests that failed on the PR and passed on `main`** — none observed in the comparable results. - **New focused tests** — 3 passed, 0 failed, 0 ignored, 0 skip notices with `--test-threads=1 --show-output --nocapture`. This includes descriptor encode/decode, CreateSampler and copy preservation, and AMD `gather4_l` and `gather4_c_l` draws with the created descriptor. - **Debug-layer coverage** — both debug-layer E2E tests, including DRED, and `uploads_start_from_each_subresources_state` passed. <details><summary>Shared failures and process aborts</summary> - `fuzz_validate_and_gpu_1`, `fuzz_validate_and_gpu_3`, and `two_pass_driver_cache_reuses_both_passes_and_recovers_corrupt_blobs` reported failures on both builds. - The library and `gpu_compute_e2e` test processes exited with `0xc0000409` on both builds. </details> **Retail smoke** Ran `scripts/smoke.ps1 -Game <local folder> -Seconds 60 -Xcl <debug binary>` with `XCL_UMA_DRIVER=mixed` for title hash `2c508222`. | Build | Exit | Frames logged | Device removals logged | Peak commit | |---|---|---|---:|---:| | `main` | `0xc0000409` | None | 0 | 1320 MB | | Merged PR, first attempt | `0xc0000409` | None | 0 | 4911 MB | | Merged PR, repeat from fresh data root | `0xc0000409` | None | 0 | 1320 MB | The first run's higher startup commit did not reproduce. These exits leave frame and steady-state memory behavior unverified. **Diff review** The selected tests checked that the sampler words encode the descriptor and survive a copy, and that translated explicit-LOD gathers use the encoded sampler fields. **Not tested** Remaining workspace tests after the aborts and retail performance. **AI use** Codex (GPT-6) merged and built the PR, ran the tests, compared the results with `main`, reviewed the diff, and drafted this report.
gut0to commented 2026-12-02 20:23:31 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by gut0to on 2026-12-02T20:23:31Z.

Code review location: crates/xcl/src/gpu/desc.rs; line 397

@@ -346,6 +346,68 @@ pub fn xcl_sampler_words(s: &[u32; 4], sampler_heap_index: u32) -> [u32; 4] {
     w
 }
 
+/// Encode a D3D12 sampler description into an RDNA2 S# in the XCL shader ABI
+/// (`w[3][11:0] = sampler_heap_index & 0xFFF`, `w[3][31:30] = BORDER_COLOR_TYPE`),
+/// preserving address modes, filter/anisotropy/reduction mode, depth compare function,
+/// LOD clamps, LOD bias, and border colour for shader paths that decode S# directly
+/// (explicit-LOD `image_gather4_l*` / `image_gather4_c_l*` and per-mip offset sampling).
+pub fn xcl_sampler_for_desc(d: &D3D12_SAMPLER_DESC, sampler_heap_index: u32) -> [u32; 4] {
+    let clamp = |m: D3D12_TEXTURE_ADDRESS_MODE| -> u32 {
+        match m {
+            D3D12_TEXTURE_ADDRESS_MODE_WRAP => 0,
+            D3D12_TEXTURE_ADDRESS_MODE_MIRROR => 1,
+            D3D12_TEXTURE_ADDRESS_MODE_CLAMP => 2,
+            D3D12_TEXTURE_ADDRESS_MODE_BORDER => 6,
+            D3D12_TEXTURE_ADDRESS_MODE_MIRROR_ONCE => 3,
+            _ => 0,
+        }
+    };
+    let f = d.Filter.0 as u32;
+    let aniso_bit = (f & 0x40) != 0;
+    let max_aniso_ratio = if aniso_bit { d.MaxAnisotropy.clamp(1, 16).ilog2().min(4) } else { 0 };
+    let (mag, min) = if aniso_bit { (3u32, 3u32) } else { ((f >> 2) & 1, (f >> 4) & 1) };
+    // D3D12 has no MIP_FILTER_NONE; encode point mip as 1 and linear mip as 2 so explicit-LOD
+    // instructions honor MinLOD/MaxLOD instead of collapsing to floor(T# MIN_LOD).
+    let mip = if aniso_bit || (f & 1) != 0 { 2u32 } else { 1u32 };
+    let z_filter = if aniso_bit || ((f >> 2) & 1) != 0 || ((f >> 4) & 1) != 0 { 2u32 } else { 1u32 };
+    let filter_mode = match (f >> 7) & 3 {
+        2 => 1u32, // MINIMUM -> SQ_IMG_FILTER_MODE_MIN
+        3 => 2u32, // MAXIMUM -> SQ_IMG_FILTER_MODE_MAX
+        _ => 0u32, // STANDARD / COMPARISON -> SQ_IMG_FILTER_MODE_BLEND
+    };
+    // D3D12_COMPARISON_FUNC 1..=8 (NEVER..ALWAYS) -> SQ_TEX_DEPTH_COMPARE 0..=7
+    let depth_cmp = (d.ComparisonFunc.0 as u32).saturating_sub(1).min(7);
+    let u4_8 = |v: f32| -> u32 {
+        if v.is_nan() {
+            0
+        } else {
+            (v.clamp(0.0, 4095.0 / 256.0) * 256.0).round() as u32
+        }
+    };
+    let s5_8 = |v: f32| -> u32 {
+        if v.is_nan() {
+            0
+        } else {
+            ((v.clamp(-32.0, 8191.0 / 256.0) * 256.0).round() as i32 as u32) & 0x3FFF
+        }
+    };
+    let border_type: u32 = match d.BorderColor {
+        [r, g, b, a] if r == 1.0 && g == 1.0 && b == 1.0 && a == 1.0 => 2,
+        [r, g, b, a] if r == 0.0 && g == 0.0 && b == 0.0 && a == 1.0 => 1,
+        _ => 0,

Medium: This maps every valid border color other than opaque black or white to BORDER_COLOR_TYPE=0 (transparent black). The explicit-LOD gather paths documented above consume this S# directly, so a custom D3D12 border such as [0.25, 0.5, 0.75, 1.0] produces the wrong out-of-range sample. Could the custom color be preserved for these paths, or use a fallback when it cannot be represented, with a GPU test for a border-addressed gather? AI-assisted review: GPT-6 helped inspect the S# encoding; I traced the direct-gather path and checked the D3D12 sampler contract and RDNA2 border-color field.

<!-- greyfinch-sync:ivall/xcl:review-comment:4775503384 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3248#discussion_r4775503384). Originally posted by [gut0to](https://github.com/gut0to) on 2026-12-02T20:23:31Z. **Code review location:** `crates/xcl/src/gpu/desc.rs`; line 397 ~~~~diff @@ -346,6 +346,68 @@ pub fn xcl_sampler_words(s: &[u32; 4], sampler_heap_index: u32) -> [u32; 4] { w } +/// Encode a D3D12 sampler description into an RDNA2 S# in the XCL shader ABI +/// (`w[3][11:0] = sampler_heap_index & 0xFFF`, `w[3][31:30] = BORDER_COLOR_TYPE`), +/// preserving address modes, filter/anisotropy/reduction mode, depth compare function, +/// LOD clamps, LOD bias, and border colour for shader paths that decode S# directly +/// (explicit-LOD `image_gather4_l*` / `image_gather4_c_l*` and per-mip offset sampling). +pub fn xcl_sampler_for_desc(d: &D3D12_SAMPLER_DESC, sampler_heap_index: u32) -> [u32; 4] { + let clamp = |m: D3D12_TEXTURE_ADDRESS_MODE| -> u32 { + match m { + D3D12_TEXTURE_ADDRESS_MODE_WRAP => 0, + D3D12_TEXTURE_ADDRESS_MODE_MIRROR => 1, + D3D12_TEXTURE_ADDRESS_MODE_CLAMP => 2, + D3D12_TEXTURE_ADDRESS_MODE_BORDER => 6, + D3D12_TEXTURE_ADDRESS_MODE_MIRROR_ONCE => 3, + _ => 0, + } + }; + let f = d.Filter.0 as u32; + let aniso_bit = (f & 0x40) != 0; + let max_aniso_ratio = if aniso_bit { d.MaxAnisotropy.clamp(1, 16).ilog2().min(4) } else { 0 }; + let (mag, min) = if aniso_bit { (3u32, 3u32) } else { ((f >> 2) & 1, (f >> 4) & 1) }; + // D3D12 has no MIP_FILTER_NONE; encode point mip as 1 and linear mip as 2 so explicit-LOD + // instructions honor MinLOD/MaxLOD instead of collapsing to floor(T# MIN_LOD). + let mip = if aniso_bit || (f & 1) != 0 { 2u32 } else { 1u32 }; + let z_filter = if aniso_bit || ((f >> 2) & 1) != 0 || ((f >> 4) & 1) != 0 { 2u32 } else { 1u32 }; + let filter_mode = match (f >> 7) & 3 { + 2 => 1u32, // MINIMUM -> SQ_IMG_FILTER_MODE_MIN + 3 => 2u32, // MAXIMUM -> SQ_IMG_FILTER_MODE_MAX + _ => 0u32, // STANDARD / COMPARISON -> SQ_IMG_FILTER_MODE_BLEND + }; + // D3D12_COMPARISON_FUNC 1..=8 (NEVER..ALWAYS) -> SQ_TEX_DEPTH_COMPARE 0..=7 + let depth_cmp = (d.ComparisonFunc.0 as u32).saturating_sub(1).min(7); + let u4_8 = |v: f32| -> u32 { + if v.is_nan() { + 0 + } else { + (v.clamp(0.0, 4095.0 / 256.0) * 256.0).round() as u32 + } + }; + let s5_8 = |v: f32| -> u32 { + if v.is_nan() { + 0 + } else { + ((v.clamp(-32.0, 8191.0 / 256.0) * 256.0).round() as i32 as u32) & 0x3FFF + } + }; + let border_type: u32 = match d.BorderColor { + [r, g, b, a] if r == 1.0 && g == 1.0 && b == 1.0 && a == 1.0 => 2, + [r, g, b, a] if r == 0.0 && g == 0.0 && b == 0.0 && a == 1.0 => 1, + _ => 0, ~~~~ --- **Medium**: This maps every valid border color other than opaque black or white to `BORDER_COLOR_TYPE=0` (transparent black). The explicit-LOD gather paths documented above consume this S# directly, so a custom D3D12 border such as `[0.25, 0.5, 0.75, 1.0]` produces the wrong out-of-range sample. Could the custom color be preserved for these paths, or use a fallback when it cannot be represented, with a GPU test for a border-addressed gather? AI-assisted review: GPT-6 helped inspect the S# encoding; I traced the direct-gather path and checked the D3D12 sampler contract and RDNA2 border-color field.
gut0to commented 2026-12-02 20:23:31 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by gut0to on 2026-12-02T20:23:31Z.

Review: COMMENTED


AI-assisted review: GPT-6 helped inspect the sampler encoding; I verified the changed mapping and the direct-S# gather path.

<!-- greyfinch-sync:ivall/xcl:review:6158311256 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3248#pullrequestreview-6158311256). Originally posted by [gut0to](https://github.com/gut0to) on 2026-12-02T20:23:31Z. **Review:** COMMENTED --- AI-assisted review: GPT-6 helped inspect the sampler encoding; I verified the changed mapping and the direct-S# gather path.
Kalmarv commented 2026-12-02 20:50:04 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Kalmarv on 2026-12-02T20:50:04Z.


Closing: consolidated into #3730 alongside the T# MIN_LOD part of #3258, addressing @Foniass's review notes (MIN_MAG_ANISOTROPIC_MIP_POINT mip-filter encoding and documenting the HLSL implicit-LOD offset path).

<!-- greyfinch-sync:ivall/xcl:comment:7358390400 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3248#issuecomment-7358390400). Originally posted by [Kalmarv](https://github.com/Kalmarv) on 2026-12-02T20:50:04Z. --- Closing: consolidated into #3730 alongside the `T# MIN_LOD` part of #3258, addressing @Foniass's review notes (`MIN_MAG_ANISOTROPIC_MIP_POINT` mip-filter encoding and documenting the HLSL implicit-LOD offset path).

Pull request closed

Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
xcl-dev/xcl!3248
No description provided.