Repository navigation
native: read fixed-index samplers from the directly indexed sampler heap - #3140
Conversation
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>
|
LGTM! The fixed-index sampler heap reading is correctly implemented: Key features:
Implementation notes:
This fixes sampler binding issues that were causing texture sampling problems in games using the native compute path. |
pigalot
left a comment
There was a problem hiding this comment.
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 == usercondition, the SGPR after the resource heap becomes{table, compute_sampler_heap}.sampler_indexaccepts 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.inlrun before the generici.op < 8 && !roottable 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 flag0x80000000sits next to the existing0x40000000used forResourceDescriptorHeap. - Consumers of the new kind:
create_native_compute_pipelineskips it in the register count and adds root flag0x800.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_sampleronly withroot_parameter == compute_sampler_heap. format_compute_runtime_manifestreturnsnulloptonroot_parameter >= 16(direct_compute_cache.cpp:746).- The in-process route (
d3d12_compute_dispatch.inl:226) throws for any kind other thanraw_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_hardwarepasses 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_warpis 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_dxilbuilt atmaincf3df832fails 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:
s_load_dwordx4 s[12:15]at0x430(entry 67).s_load_dwordx4 s[28:31]at0x440(entry 68).s_mov_b32 s14, s30ands_mov_b32 s15, s31.image_sample_lzwiths[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.
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 withs_load_dwordx8) at a 16-byte aligned immediate offset, then sample withimage_sample_lz/image_sample_l. The native translator left that SGPRunbound("direct descriptor address outside bound table"), andsampler_indexaccepted literal words only ("nonconstant sampler"). This is stage 1 of #3076: the translator side.src/gfx1013_compute_native.h/.cpp: sentinelcompute_sampler_heap(~0u - 1); under the existing 0xc00 condition the SGPR after the resource heap is{table, compute_sampler_heap}.sampler_indexreturnsSamplerRef {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 onlyimage_sample_landimage_sample_lzare translated.Emit::bindingrefuses 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 thandwordx4/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 becomesSamplerDescriptorHeap[index].src/dxil_module.h:sampler_heap_handleemitscreateHandleFromHeap(i32 218, i32 <index>, i1 true, i1 false)+annotateHandle{14, 0}; module flag0x80000000. This is what DXC emits forSamplerDescriptorHeap[67]+SampleLevelin cs_6_6.ComputeViewKind::heap_sampler(one binding per program,required_bytes = 16 * (highest index + 1));src/direct_compute_cache.cppaccepts the kind with exactly that root value and keeps the old check for every other kind.format_compute_runtime_manifestwrites 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_indexedadds root flag0x800(no0x400for a sampler-only program);bind()takes the shader-visible sampler heap as a new last parameter (existing callers unchanged).tools/startup/compile_dxil.cpp: reportssampler_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
mainthis is never the first refusal yet:compile_dxil --refusalsat36d2cbebis 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 tocpp-migrationas 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
main36d2cbeb, rebased onto8cf9be9band re-run there (numbers below are from the rebased branch unless noted).native_sampler_heap_hardware: passed, 69 assertions, about 3.5 s;_warpskips (77, no 32-lane waves)..c67requests (rootRootFlags(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 withdynamic_sampler(), sampling at a coordinate outside [0, 1], and compares every lane with a CPU model.dwordx8load, entries 67 (wrap) and 68 (clamp).image_sample_lwith LOD 1, where MaxLOD decides the mip.prepare_direct_computetwice on one cache: a miss, then a hit, with exactly oneheap_samplerbinding of the expected span in both.bind()without a sampler heap throws..cso:dwordx2,dwordx16, offset 0x438, index 2047, adwordx8load reaching 2047;image_gather4_lz,image_gather4_c_lz),image_sample_c_lz,image_sample_d;compile_dxil --requestspublishes.cs.csoand no.txt. The module's DXIL disassembly showscreateHandleFromHeap(i32 218, i32 67, i1 true, i1 false).36d2cbeb):compile_dxilbuilt at36d2cbebfails at the first compile of SAMPLE ("direct descriptor address outside bound table").off / 32: wrong heap span;0x800: pipeline creation fails;dwordx8read at the first one's offset.0x800plus aSamplerDescriptorHeapshader failsCreateComputePipelineStatewithE_INVALIDARG("root signature is missing the D3D12_ROOT_SIGNATURE_FLAG_SAMPLER_HEAP_DIRECTLY_INDEXED flag");0x800create without a message.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 newnative_sampler_heap_warp). At36d2cbebthe set had 161 tests and 0 failures before the change and 163 / 0 after it.docs_linksandbidi_controlspass.Evidence and follow-up
clang -cc1as -triple amdgcn-amd-amdhsa -target-cpu gfx1013 -show-encoding). Fixtures are source-built.docs/experiments/native-sampler-heap.md):0x800are unchanged.dynamic_sampler()decoding and entry 2047 as the fallback, as the draw path already does for heap-indexed roots.Checklist
AI-assisted.
🤖 Generated with Claude Code