startup: translate run-time sampler heap indexing into a bounded table #1210
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/1217"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from R0Y00/Xbox2Windows. Originally posted by Jess-Doit on 2026-11-24T04:06:06Z.
Change
Refs #1143. Translator half, sampler heap only. #1192 is the matching desktop check.
With
X2W_SAMPLER_HEAP=1,tools/startup/gfx2hlsl.pytranslates ans_load_dwordx4from the sampler heap with a register offset into a bounded sampler table, instead of the fixed default sampler:SamplerState qh[2048] : register(s0, space1)is the D3D12 limit for a shader-visible sampler heap. The sample readsqh[x2w_sampler_entry(offset)].x2w_sampler_entryreturnsoffset >> 4for an offset that is a multiple of 16 and below 2047 × 16 (32752), and 2047 otherwise. The host can't check an offset the shader computes, so the guard is in the generated shader.smpheap 2048 1 fallback 2047(count, register space, fallback entry), so the runtime can bind a matching table. The shortcut'spipe.notesentry isn't written in this mode.mixes sampler heap words).Off by default: the stand-in doesn't bind that table yet. Its root signatures still carry static samplers only, so a translated pipeline with
smpheapwould fail to create. Without the switch, the output is unchanged, shortcut included. The follow-up builds the table in the stand-in from the title's sampler-heap entries, decoded like today's static samplers, and then turns this on.Not covered: constant sampler-heap offsets are still refused, as before. Resource-heap indexing (#1143's other half) is also not covered. One finding for whoever takes constant offsets: on in-box WARP
10.0.19041.5794, a compile-time-constant index into the sampler table (qh[1]) read entry 0 with both DXCps_6_0and FXCps_5_1. A run-time index that always evaluated to 1 read entry 1. The GTX 1060 read entry 1 in all four combinations.Fixture
tests/startup_sampler_heap_check.pycarries a source-built gfx1013 vertex+pixel pair: assembly source plus the recorded words.SAMPLER_HEAP_DIRECTLY_INDEXED).offscreen_check --startup-sampler-heap,tests/startup_sampler_heap.cpp): a 2×1 R32_FLOAT texture {0.25, 0.75} and a 2048-entry point-sampler table. Entry 0 wraps, entry 1 clamps, entries 2 to 2046 use a black border, and the fallback entry 2047 uses a border of 0.5. So the colour names the entry the shader indexed.qh[s24 >> 4]/s24 = ud1/smpheap 2048 1translation; the default output unchanged;X2W_SAMPLER_HEAP=1selects the table; constant offset still refused in both modes; the mixed operand refusedbinary <llvm-mc>(gfx1013 job)draw <dxc> <offscreen_check> <adapter> [fxc](cteststartup_sampler_heap_{warp,hardware})0xfffffff0and the unaligned 8 read3e800000,3f400000,0,0, then3f000000(the fallback) three times; the negative control, the translated shader with its index replaced byqh[0], must fail at offset 16 withactual=3e800000Verification
Tested on Windows 10 22H2
19045.6456with a GTX 1060 6 GB (10de:1c03, driver32.0.15.6094) and in-box WARP (1414:008c,10.0.19041.5794), D3D12 debug layer enabled. DXC and FXC are from Windows SDK10.0.26100.0.Tested
1e0a9834(a7ce0018with main3a156358merged in). Debug and Release each passed 3/3 (startup_sampler_heap_controls,_warp,_hardware) with zero skips. On both adapters and with both compilers, the seven offsets read their expected words, and the dropped-index shader failed at offset 16 withactual=3e800000.binarymode matched the recorded words with a local LLVM 23.1.6llvm-mc; the CI job runs LLVM 15.tests/docs_links_check.pyandgit diff --checkare clean.AI-assisted (Claude Code).
Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T04:21:37Z.
Review: COMMENTED
Reviewed commit
3835fe22.Diff & edge-case audit
tools/startup/gfx2hlsl.py: WithX2W_SAMPLER_HEAP=1(orsampler_heap=True),s_load_dwordx4from('smpheap',)with a register offset emitss<d> = <offset>;and tracks('smpheap', d, i)across all 4 words;heap_samplerverifies all 4 words come from the same heap load (rejecting overwritten words viaUnsupported) and emitsqh[s<d> >> 4]overSamplerState qh[2048] : register(s0, space1)withsmpheap 2048 1in the manifest. Constant sampler-heap offsets remain rejected.qh[s<d> >> 4]does not clamps<d> >> 4to[0, 2047]in HLSL, so an out-of-range runtime byte offset (>= 32768) would index past the 2048-entry table; the fixture covers valid indices0,1,2, and2047.tests/startup_sampler_heap.cpp,tests/offscreen_check.cpp,tests/startup_sampler_heap_check.py,CMakeLists.txt,docs/startup-runtime.md,AGENTS.md,.github/workflows/windows.yml: Consistent and title-neutral.Checks run on Linux (
3835fe22)/root/.local/share/x2w-rev-venv/bin/python -B tests/startup_sampler_heap_check.py: PASS (5/5 control assertions: runtime offset, constant offset refused in both modes, opt-out default sampler,X2W_SAMPLER_HEAP=1selection, overwrittens25refused)./root/.local/share/x2w-rev-venv/bin/python -B tests/startup_sampler_heap_check.py binary llvm-mc-19: PASS (fixture words match llvm-mc)./root/.local/share/x2w-rev-venv/bin/python -B tests/rdna2_tools_check.py(pure-Python mode): PASS (128 tests: 113 passed, 15 skipped, 0 failed)./root/.local/share/x2w-rev-venv/bin/python -B tests/docs_links_check.py: PASS (16 Markdown files, 274 links)./root/.local/share/x2w-rev-venv/bin/python -B tests/bidi_controls_check.py: PASS (1020 files)./root/.local/share/x2w-rev-venv/bin/python -B tests/form_coverage_check.py: PASS./root/.local/share/x2w-rev-venv/bin/python -B tests/xgameruntime_table_check.py: PASS (28 exports)./root/.local/share/x2w-rev-venv/bin/python -B tests/gpu_report_format_check.py: PASS (10/10)./root/.local/share/x2w-rev-venv/bin/python -B tests/hardware_reports_check.py: PASS (11/11).Note: Windows MSVC/DXC/FXC build and
startup_sampler_heap_warp/startup_sampler_heap_hardwaredraw tests were not executed locally.Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T04:37:16Z.
Review: COMMENTED
Linux retest at commit
a7ce0018(delta3835fe22..a7ce0018; Windows build and D3D12 runtime tests were not executed locally).Delta
3835fe22..a7ce0018auditqh[s<d> >> 4]finding (offset >= 32768) is fixed:tools/startup/gfx2hlsl.pynow reserves entrySAMPLER_HEAP_FALLBACK = 2047(SAMPLER_HEAP_SIZE - 1) and emitsuint x2w_sampler_entry(uint offset) { return (offset & 15u) != 0 || offset >= 32752u ? 2047u : offset >> 4; }, indexingqh[x2w_sampler_entry(s<d>)]and emittingsmpheap 2048 1 fallback 2047in the manifest. Any unaligned offset or offset>= 32752(including>= 32768and0xfffffff0) is clamped to valid index2047.tests/startup_sampler_heap.cpp&tests/startup_sampler_heap_check.py&docs/startup-runtime.md: updated to reserve entry2047with border0.5fas the fallback and add Windows draw cases for2046 * 16,2047 * 16,0xfffffff0, and unaligned8.Checks run (Linux, commit
a7ce0018)LLVM_LIB=/usr/lib/llvm-19/lib/libLLVM-19.so /root/.local/share/x2w-rev-venv/bin/python -B tests/startup_sampler_heap_check.py: passed (run-time offset: qh[x2w_sampler_entry(s24)] with s24 = ud1 (root constant 1); unaligned or >= 32752 reads entry 2047; manifest smpheap 2048 1 fallback 2047; constant-offset, opt-out, and overwritten-register controls passed).LLVM_LIB=/usr/lib/llvm-19/lib/libLLVM-19.so /root/.local/share/x2w-rev-venv/bin/python -B tests/startup_sampler_heap_check.py binary /usr/bin/llvm-mc-19: passed (fixture words match llvm-mc)./root/.local/share/x2w-rev-venv/bin/python -B tests/docs_links_check.py: passed (Self-test passed. Checked 16 Markdown files and 274 links.)./root/.local/share/x2w-rev-venv/bin/python -B tests/bidi_controls_check.py: passed (Self-test passed. Checked 1020 tracked source, script and build/CI files: no bidirectional controls.).startup_sampler_heap_*D3D12 draw tests: not executed on Linux.Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T04:48:10Z.
Review: COMMENTED
Retested
1e0a9834(deltaa7ce0018..1e0a9834, merge withmain) on Linux. Windows build and D3D12startup_sampler_heap_*draw tests were not run locally.Delta audit (
a7ce0018..1e0a9834).github/workflows/windows.yml,AGENTS.md,CMakeLists.txt,docs/startup-runtime.md,tests/desktop_gpu.h,tests/offscreen_check.cpp,tests/startup_sampler_heap.cpp,tests/startup_sampler_heap_check.py,tools/startup/gfx2hlsl.py) relative toa7ce0018; the delta only mergesmain(#1174,#1216,#1177).x2w_sampler_entry(uint offset)bounds/alignment guard ((offset & 15u) != 0 || offset >= 32752u ? 2047u : offset >> 4),smpheap 2048 1 fallback 2047manifest contract, and docs/tests remain intact and title-neutral.Linux checks (
/root/.local/share/x2w-rev-venv/bin/python -B)tests/startup_sampler_heap_check.py: passed (5/5 control checks, exit 0)tests/startup_sampler_heap_check.py binary /usr/bin/llvm-mc-19: passed (fixture words match llvm-mc, exit 0)tests/form_coverage_check.py: passed (form coverage checks passed, exit 0)tests/rdna2_tools_check.py(pure-Python mode): passed (129 tests: 114 passed, 15 skipped, 0 failed)tests/docs_links_check.py: passed (16 Markdown files, 280 links, exit 0)tests/bidi_controls_check.py: passed (1046 tracked files, exit 0)git diff --check origin/main..HEAD: passed (exit 0)offscreen_checkandstartup_sampler_heap_{controls,warp,hardware}: not run locally.Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-24T04:22:49Z.
The three source-built sampler heap tests passed locally in Release, including WARP and RTX. The authored expression qh[offset >> 4] currently accepts any offset value, while the table has only 2,048 entries. Please document the alignment and range preconditions at the manifest boundary and say how the host will enforce them before enabling this mode. If the caller cannot enforce them, keep the access guarded in the generated shader. A control for the last valid entry and the first out-of-range byte offset would make that boundary reproducible.
Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T04:24:52Z.
Windows Server 2025 retest at
3835fe22(Windows Server 2025 Build 26100, MSVC 14.44.35207, AMD Ryzen 5 PRO 5650U, WARP):Debug (
devpreset)Commands:
Results: 3 passed, 0 failed, 0 skipped (Total Test time:
2.01 sec)startup_sampler_heap_controls: Passed (0.20 sec)startup_sampler_heap_warp: Passed (1.06 sec)startup_sampler_heap_hardware: Passed (0.72 sec)Release (
/O2,releasepreset)Commands:
Results: 3 passed, 0 failed, 0 skipped (Total Test time:
1.92 sec)startup_sampler_heap_controls: Passed (0.18 sec)startup_sampler_heap_warp: Passed (0.90 sec)startup_sampler_heap_hardware: Passed (0.82 sec)Imported from R0Y00/Xbox2Windows. Originally posted by Jess-Doit on 2026-11-24T04:28:35Z.
Thanks for checking this, and thanks @tedin7 for the Server 2025 retest. Fixed in
a7ce0018. The host can't check an offset the shader computes, so the guard is in the generated shader:qh[x2w_sampler_entry(offset)]. That'soffset >> 4for a multiple of 16 below 2047 × 16 = 32752, and 2047 otherwise, including unaligned offsets.smpheap 2048 1 fallback 2047, so the binding side knows which entry to fill, and can count reads that land on it. The contract is written next to the constants ingfx2hlsl.pyand indocs/startup-runtime.md.The draw now also covers the boundary. The harness gives entry 2047 a 0.5 border to tell it apart:
000000003f0000000xfffffff03f0000003f000000Debug and Release pass 3/3 on a GTX 1060 and in-box WARP (Windows 10
19045.6456), with DXCps_6_0and FXCps_5_1. The dropped-index control still fails at offset 16. The mode stays opt-in.AI-assisted (Claude Code).