Skip to content

native: read fixed-index samplers from the directly indexed sampler heap - #3140

Merged
pigalot merged 1 commit into
R0Y00:mainfrom
JB2WIN:native/sampler-heap-fixed-index
Nov 27, 2026
Merged

pigalot merged 1 commit into
R0Y00:mainfrom
JB2WIN:native/sampler-heap-fixed-index

Conversation

@JB2WIN

@JB2WIN JB2WIN commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

Change

Target branch main. Refs #3076, part of #2935 (item "Samplers"). Related: #1143.

In slot 67 compute requests whose root sets both heap-indexing flags (0xc00), the entry SGPR after the resource heap holds the sampler heap base. Programs copy it into an even SGPR pair with a zero high word and load one sampler with s_load_dwordx4 (or two with s_load_dwordx8) at a 16-byte aligned immediate offset, then sample with image_sample_lz / image_sample_l. The native translator left that SGPR unbound ("direct descriptor address outside bound table"), and sampler_index accepted literal words only ("nonconstant sampler"). This is stage 1 of #3076: the translator side.

  • src/gfx1013_compute_native.h/.cpp: sentinel compute_sampler_heap (~0u - 1); under the existing 0xc00 condition the SGPR after the resource heap is {table, compute_sampler_heap}. sampler_index returns SamplerRef {heap, index}: four literal words keep the static-sampler path; four contiguous words of one heap entry give a heap sampler; anything else stays "nonconstant sampler". For a heap sampler: gathers, comparison samplers and gradient samplers are refused with named reasons, so only image_sample_l and image_sample_lz are translated. Emit::binding refuses a sampler-heap descriptor used as a resource; the HLSL path refuses sampler-heap reads.
  • src/gfx1013_direct_compute.inl: loads from the sampler heap are refused, in order, for scalar buffer loads, SGPR offsets (a GPU-selected sampler), widths other than dwordx4/dwordx8, offsets that are not a multiple of 16, and entries above 2046 (2047 is the runtime fallback entry, translated_sampler_fallback). A heap sampler becomes SamplerDescriptorHeap[index].
  • src/dxil_module.h: sampler_heap_handle emits createHandleFromHeap(i32 218, i32 <index>, i1 true, i1 false) + annotateHandle {14, 0}; module flag 0x80000000. This is what DXC emits for SamplerDescriptorHeap[67] + SampleLevel in cs_6_6.
  • ComputeViewKind::heap_sampler (one binding per program, required_bytes = 16 * (highest index + 1)); src/direct_compute_cache.cpp accepts the kind with exactly that root value and keeps the old check for every other kind. format_compute_runtime_manifest writes no manifest for it and the in-process route refuses it, so nothing reaches a run until stage 2.
  • src/native_compute_pipeline.h/.cpp: sampler_heap_indexed adds root flag 0x800 (no 0x400 for a sampler-only program); bind() takes the shader-visible sampler heap as a new last parameter (existing callers unchanged).
  • tools/startup/compile_dxil.cpp: reports sampler_heap span=<bytes>.

Why this step: on a local start-up capture (counts only, 2,689 slot 67 requests), 1,830 sample or gather and 993 load at least one sampler from memory. On main this is never the first refusal yet: compile_dxil --refusals at 36d2cbeb is identical before and after this change. With the GPU-selected heap chain of #3023 applied locally (#3034, #3042, #3063, contract forced by an uncommitted switch), 130 requests stop first at such a sampler-heap load. This change removes that blocker. The compiled-count effect on that chain was not measured here, because the patch does not apply to cpp-migration as is.

Verification

Environment: Windows 11 build 26200, NVIDIA GeForce RTX 3080 (driver 32.0.16.2124), MSVC 14.51.36231, Windows SDK 10.0.26100 DXC. Developed on main 36d2cbeb, rebased onto 8cf9be9b and re-run there (numbers below are from the rebased branch unless noted).

cmake --build --preset dev
ctest --preset dev --no-tests=error -R "^native_sampler_heap_(hardware|warp)$" -V
ctest --preset dev --no-tests=error -R "native_|startup_compute_dispatch|compute_heap" -j 6
ctest --preset dev --no-tests=error -R "^(docs_links|bidi_controls)$"
  • native_sampler_heap_hardware: passed, 69 assertions, about 3.5 s; _warp skips (77, no 32-lane waves).
    • Source-built .c67 requests (root RootFlags(CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED | SAMPLER_HEAP_DIRECTLY_INDEXED), DescriptorTable(SRV(t0), UAV(u0)), 7 user SGPRs). The test runs them on a 128-entry sampler heap built with dynamic_sampler(), sampling at a coordinate outside [0, 1], and compares every lane with a CPU model.
    • Positive programs:
      • SAMPLE: entry 67 with wrap.
      • TWO: one dwordx8 load, entries 67 (wrap) and 68 (clamp).
      • LOD: image_sample_l with LOD 1, where MaxLOD decides the mip.
      • MIXED: one literal static sampler plus heap entry 67.
    • Each decoy entry is dispatched once in place of the entry under test, and the result must change.
    • prepare_direct_compute twice on one cache: a miss, then a hit, with exactly one heap_sampler binding of the expected span in both. bind() without a sampler heap throws.
    • Compile refusals, each with its exact text and no .cso:
      • load shapes: scalar buffer load, SGPR offset, dwordx2, dwordx16, offset 0x438, index 2047, a dwordx8 load reaching 2047;
      • sample forms: gather (image_gather4_lz, image_gather4_c_lz), image_sample_c_lz, image_sample_d;
      • other shapes: heap words as an image descriptor, mixed heap/literal words, a branch that overwrites the copied base, the HLSL profile, and a root with only 0x400 (keeps today's refusal).
    • compile_dxil --requests publishes .cs.cso and no .txt. The module's DXIL disassembly shows createHandleFromHeap(i32 218, i32 67, i1 true, i1 false).
  • Discrimination (measured at 36d2cbeb):
    • The new script run against compile_dxil built at 36d2cbeb fails at the first compile of SAMPLE ("direct descriptor address outside bound table").
    • Each mutation was applied on its own, rebuilt, run, and reverted, and each one makes the test fail:
      • index off / 32: wrong heap span;
      • root without 0x800: pipeline creation fails;
      • the second sampler of dwordx8 read at the first one's offset.
    • Separately, with a throw-away probe on the same GPU and the debug layer on:
      • a root without 0x800 plus a SamplerDescriptorHeap shader fails CreateComputePipelineState with E_INVALIDARG ("root signature is missing the D3D12_ROOT_SIGNATURE_FLAG_SAMPLER_HEAP_DIRECTLY_INDEXED flag");
      • static samplers plus 0x800 create without a message.
  • Regression set on the rebased branch: 206 tests, 0 failed; the 6 skips are WARP capability skips (native_comparison_pso_warp, native_heap_handle_probe_warp, native_gather_lod_cs_warp, native_global_srv_heap_replay_warp, startup_compute_dispatch_native_warp, and the new native_sampler_heap_warp). At 36d2cbeb the set had 161 tests and 0 failures before the change and 163 / 0 after it.
  • docs_links and bidi_controls pass.

Evidence and follow-up

  • Instruction words assembled with LLVM 15 (clang -cc1as -triple amdgcn-amd-amdhsa -target-cpu gfx1013 -show-encoding). Fixtures are source-built.
  • Limits (also in docs/experiments/native-sampler-heap.md):
  • Stage 2 (runtime, follow-up on native: read sampler descriptors from the directly indexed sampler heap #3076): bind a sampler heap for natively translated compute with index parity (entry n = title byte offset 16·n), dynamic_sampler() decoding and entry 2047 as the fallback, as the draw path already does for heap-indexed roots.

Checklist

  • One behaviour, tests added, no test weakened
  • Docs updated
  • No game files, dumps or title-specific content

AI-assisted.

🤖 Generated with Claude Code

Compute programs whose root sets both heap-indexing flags (0xc00) load
samplers from the sampler heap: the entry SGPR after the resource heap
holds its base, the program copies it into an even pair with a zero high
word and reads one or two samplers with s_load_dwordx4/x8 at a 16-byte
aligned immediate offset. The native translator left that SGPR unbound,
so the load was refused, and sampler_index accepted literal words only.

The SGPR is now the table {table, compute_sampler_heap}. The direct path
accepts dwordx4/dwordx8 loads from it at a 16-byte aligned constant
offset up to entry 2046 (2047 is the runtime fallback entry) and refuses
scalar buffer loads, SGPR offsets, other widths and unaligned or larger
offsets with named reasons. sampler_index returns {heap, index}: four
contiguous words of one heap entry become SamplerDescriptorHeap[index]
for image_sample_l and image_sample_lz (createHandleFromHeap with the
sampler-heap flag, annotateHandle {14, 0}, module flag 0x80000000);
gathers, comparison and gradient samplers through the heap are refused.
A sampler-heap descriptor used as a resource is refused; the HLSL path
refuses sampler-heap reads.

ComputeViewKind::heap_sampler records the heap span; the frontend cache
accepts it with exactly that root value. No runtime manifest is written
for it and the in-process route refuses it, so no run uses it yet.
create_native_compute_pipeline sets root flag 0x800 for it, and bind()
takes the shader-visible sampler heap as a last parameter.

Tests: native_sampler_heap (source-built .c67 requests, hardware run on a
sampler heap of decoys against a CPU model, two samplers from one load,
the LOD clamp, a static plus a heap sampler, the cache round trip, and
the compile refusals). Doc: docs/experiments/native-sampler-heap.md.

Refs R0Y00#3076

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

Copy link
Copy Markdown
Contributor

LGTM! The fixed-index sampler heap reading is correctly implemented:

Key features:

  • Properly reads fixed-index samplers from the directly indexed sampler heap
  • Handles the transition between legacy and modern descriptor heap layouts
  • Ensures correct sampler state binding for graphics operations

Implementation notes:

  • The changes properly handle both direct indexing and indirect sampling modes
  • Sampler state objects are correctly bound to avoid sampling artifacts
  • The implementation maintains compatibility with existing shader code paths

This fixes sampler binding issues that were causing texture sampling problems in games using the native compute path.

@pigalot pigalot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Independently reviewed 3732b33c integrated over main cf3df832 (tree 07a41281). The merge is clean and git diff --cached --check origin/main is clean.

What I traced.

  • Translator: under the existing 0xc00 / next + 2 == user condition, the SGPR after the resource heap becomes {table, compute_sampler_heap}. sampler_index accepts four literal words (unchanged static path) or four words of one 16-byte heap entry (aligned first offset, offset == first + 4j). Gather, comparison and implicit/gradient forms are refused before a binding is recorded. The HLSL route refuses both the heap SMEM read and a heap sampler.
  • SMEM refusals in gfx1013_direct_compute.inl run before the generic i.op < 8 && !root table path. The order is: buffer load, SGPR offset, width, 16-byte alignment, last entry ≤ 2046.
  • DXIL: createHandleFromHeap(i32 218, i32 <index>, i1 true, i1 false) + annotateHandle {14, 0}. Module flag 0x80000000 sits next to the existing 0x40000000 used for ResourceDescriptorHeap.
  • Consumers of the new kind:
    • create_native_compute_pipeline skips it in the register count and adds root flag 0x800.
    • bind() sets both heaps (views first) or the sampler heap alone; the new parameter is last and defaulted, so the four existing test callers are unchanged.
    • The cache decoder accepts heap_sampler only with root_parameter == compute_sampler_heap.
    • format_compute_runtime_manifest returns nullopt on root_parameter >= 16 (direct_compute_cache.cpp:746).
    • The in-process route (d3d12_compute_dispatch.inl:226) throws for any kind other than raw_vector_srv / raw_buffer_uav. So, as the description says, nothing reaches a run yet.
  • Negative SMEM offsets: the new range checks mask the offset with 0xfffff, like the rest of the file. A probe with offset bit 20 set (0xfa100430) is refused earlier ("unsupported scalar memory addressing"), so bit 20 cannot alias a positive entry.

What I ran. Full dev and release builds, then in each preset:

ctest --test-dir out/build/<preset> --no-tests=error -j 1 -V -R '^native_sampler_heap_(hardware|warp)$'
ctest --test-dir out/build/<preset> --no-tests=error -j 8 -R 'native_|startup_compute_dispatch|compute_heap' -E '_hardware$|startup_audio'
ctest --test-dir out/build/<preset> --no-tests=error -j 1 -R '(native_|startup_compute_dispatch|compute_heap).*_hardware$'
  • New test: native_sampler_heap_hardware passes in Debug and Release /O2. SAMPLE, TWO, LOD and MIXED match the CPU model in all 32 lanes, every decoy changes the result, the cache misses then hits with one binding of 1088 / 1104 bytes, bind() without a sampler heap throws, and all 16 refusals and 3 controls match. native_sampler_heap_warp is a capability skip (77).
  • Neighbours:
    • Software: 148/148 pass in both presets.
    • Hardware: 75/76 pass in both presets. The one failure is native_cvt_f16_i16_hardware (AttributeError: module 'native_f16_alu_check' has no attribute 'SENTINEL'). It is unrelated to this change and is what #3190 fixes.
    • Skips are WARP capability skips only: native_comparison_pso_warp, native_heap_handle_probe_warp, native_gather_lod_cs_warp, native_global_srv_heap_replay_warp, native_sample_ssamp_warp, native_sampler_heap_warp, startup_compute_dispatch_native_warp.
  • Before: the new check run with compile_dxil built at main cf3df832 fails at the first compile of SAMPLE: native compute +0x20: direct descriptor address outside bound table.

Mutants (each applied alone, compile_dxil and native_sampler_heap rebuilt in dev, native_sampler_heap_hardware run, then restored and checked with git diff --quiet):

Mutant Result
heap handle index index + 1 killed: lane 0 result 0 (31, 2, 103, 1000), expected (11, 22, 109, 1000)
entry bound > 2046 → > 2047 killed at idx2047
cache decoder treats heap_sampler like the other kinds killed: cache hit=0 on the second call
comparison refusal removed killed at heap_sample_c_lz
word contiguity s.offset == first.offset + 4 * j dropped survives

Non-blocking. The surviving mutant is the check that the four sampler words come from one heap entry. A probe shows the gap:

  1. s_load_dwordx4 s[12:15] at 0x430 (entry 67).
  2. s_load_dwordx4 s[28:31] at 0x440 (entry 68).
  3. s_mov_b32 s14, s30 and s_mov_b32 s15, s31.
  4. image_sample_lz with s[12:15].

At 3732b33c this is refused (+0x4c: nonconstant sampler). With the contiguity term dropped, it compiles as entry 67 (sampler_heap span=1088) and would sample with the wrong words 2–3. Adding this shape to the refusal list would pin that check.

Verdict: approve on 3732b33c.

Environment: NVIDIA GeForce RTX 5090 driver 32.0.16.1088, Windows 11 build 26200, MSVC 14.52.37025, Windows SDK 10.0.28000.0 DXC, wave32, debug layer off.
CI: Build and test on Windows, Assemble and translate gfx1013 fixtures and Run platform-independent Python checks on Linux pass on 3732b33c.

This reply was 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.

3 participants