From c06a91d408a0af915adfeb472a30c85a84e40a1e Mon Sep 17 00:00:00 2001 From: triggered02 <153171514+triggered02@users.noreply.github.com> Date: Wed, 25 Nov 2026 21:44:24 +0530 Subject: [PATCH 1/3] startup: lower 1D image sampling with an explicit level --- CMakeLists.txt | 3 + docs/experiments.md | 14 ++ tests/startup_gfx2hlsl_image_1d_check.py | 158 +++++++++++++++++++++++ tests/startup_image_sample_b_check.py | 2 +- tests/startup_image_sample_l_check.py | 8 +- tools/startup/gfx2hlsl.py | 15 ++- 6 files changed, 194 insertions(+), 6 deletions(-) create mode 100644 tests/startup_gfx2hlsl_image_1d_check.py diff --git a/CMakeLists.txt b/CMakeLists.txt index 0c19656804..6c1da07e05 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3041,6 +3041,9 @@ if(EXISTS "${INTAKE_PYTHON}") # lowering of the same selections. add_test(NAME startup_gfx2hlsl_sdwa COMMAND "${INTAKE_PYTHON}" -B "${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_gfx2hlsl_sdwa_check.py") + # 1D image sampling with an explicit level (#1721). + add_test(NAME startup_gfx2hlsl_image_1d COMMAND "${INTAKE_PYTHON}" -B + "${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_gfx2hlsl_image_1d_check.py") # image_sample_c_lz with sampler word 2 bits 28-30 set (#1723). add_test(NAME startup_gfx2hlsl_sample_c_lz_mip COMMAND "${INTAKE_PYTHON}" -B "${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_gfx2hlsl_sample_c_lz_mip_check.py") diff --git a/docs/experiments.md b/docs/experiments.md index 6a206cc2dc..f4b29da331 100644 --- a/docs/experiments.md +++ b/docs/experiments.md @@ -15108,3 +15108,17 @@ Building on #2443 (Slice 1), `X2W_D3D12_RELEASE_RETIRED=1` extends desktop objec ```powershell ctest --preset dev --no-tests=error -R '^startup_resource_(retire|budget)' -V ``` + +## Explicit-level 1D image sampling in the startup translator + +[The startup translator](../tools/startup/gfx2hlsl.py) now lowers 1D `image_sample_lz` and `image_sample_l` onto `Texture1D.SampleLevel` alongside the existing implicit-level `image_sample` path (#1721): + +- `image_sample_lz` takes one address dword (`u`) and lowers to `tex.SampleLevel(smp, F(u), 0)`. +- `image_sample_l` takes two address dwords (`u`, then the explicit `lod`) and lowers to `tex.SampleLevel(smp, F(u), F(lod))`. +- Both explicit-level forms require a non-empty `dmask` and accept only `dim:SQ_RSRC_IMG_1D`, `dmask:*`, and the cache-policy modifiers (`glc`, `slc`, `dlc`). Plain `image_sample` on 1D keeps its existing rule (still refusing cache-policy bits), and layout or address modifiers (`unorm`, `r128`, `a16`, `d16`, `tfe`, `lwe`, reserved bits), wrong address counts, empty `dmask`, `image_sample_b`, and `image_load` on 1D stay refused. + +[The 1D image check](../tests/startup_gfx2hlsl_image_1d_check.py) builds synthetic MIMG instructions against the decoder layout and verifies `image_sample_lz`, `image_sample_l`, plain `image_sample`, 2D separation, cache-policy equivalence, empty `dmask`, and refused forms. Run: + +```powershell +ctest --preset dev --no-tests=error -R '^startup_gfx2hlsl_image_1d$' -V +``` diff --git a/tests/startup_gfx2hlsl_image_1d_check.py b/tests/startup_gfx2hlsl_image_1d_check.py new file mode 100644 index 0000000000..0d355c3bd3 --- /dev/null +++ b/tests/startup_gfx2hlsl_image_1d_check.py @@ -0,0 +1,158 @@ +"""1D image sampling in tools/startup/gfx2hlsl.py (issue #1721). + +usage: startup_gfx2hlsl_image_1d_check.py + +A 1D image has one coordinate, and `image_sample_lz` / `image_sample_l` carry the explicit level in the next +address dword. The translator accepted 1D only for the implicit-level `image_sample`, so the two explicit-level +forms were refused - 2 of the 29 refused shaders in the first loading-screen cache (#870). + +The instruction bytes are **constructed here** from the field layout the checked-in decoder reads +(`tools/disasm_rdna2.py`, MIMG branch): + w0: [31:26] = 0b111100, [24:18] opcode low 7 bits, [0] opcode bit 7, [11:8] dmask, [5:3] dimension, + [2:1] NSA dword count, [12] unorm, [15] r128 + w1: [7:0] vaddr, [15:8] vdata, [20:16] srsrc/4, [25:21] sampler/4 +""" +import os +import struct +import sys + +here = os.path.dirname(os.path.abspath(__file__)) +sys.path.insert(0, os.path.join(here, '..', 'tools')) +sys.path.insert(0, os.path.join(here, '..', 'tools', 'startup')) +import gfx2hlsl as G # noqa: E402 + +failures = [] + + +def check(label, condition, detail=''): + print(f"{label}: {'ok' if condition else 'MISMATCH'}") + if not condition: + failures.append(label) + if detail: + print(' ' + detail) + + +IMG_LOAD = 0x00 +IMG_SAMPLE = 0x20 +IMG_SAMPLE_L = 0x24 +IMG_SAMPLE_LZ = 0x27 +DIM_1D = 0 +DIM_2D = 1 + + +def encode_mimg(op, *, dmask=0xF, dim=DIM_1D, vaddr=0, vdata=4, srsrc=8, ssamp=12, nsa=0, + extra_addrs=(), unorm=0, r128=0, glc=0, slc=0, dlc=0): + """Encode one MIMG instruction; returns its bytes (8 + 4 per NSA dword).""" + w0 = ((0b111100 << 26) | ((op & 0x7F) << 18) | (((op >> 7) & 1) << 0) + | ((dmask & 0xF) << 8) | ((dim & 0x7) << 3) | ((nsa & 0x3) << 1) + | ((unorm & 1) << 12) | ((r128 & 1) << 15) | ((dlc & 1) << 7) + | ((glc & 1) << 13) | ((slc & 1) << 25)) + w1 = ((vaddr & 0xFF) | ((vdata & 0xFF) << 8) | (((srsrc // 4) & 0x1F) << 16) + | (((ssamp // 4) & 0x1F) << 21)) + return struct.pack('' in one_d, one_d + assert re.search(r'SampleLevel\(\w+, F\(v0\), F\(v1\)\)', one_d), one_d refused('msaa', 'image dimension SQ_RSRC_IMG_') for name in RESERVED: refused(name, 'image_sample_l modifier reserved') # bits LLVM ignores must not be silently accepted - print('refused: offset, compare, gather, cube, 1D, MSAA, unorm, a16, tfe, lwe, d16, r128, dmask 0, reserved bits') + print('refused: offset, compare, gather, cube, MSAA, unorm, a16, tfe, lwe, d16, r128, dmask 0, reserved bits') # Cache-policy bits do not change the sampled value: same lowering as the plain form. for name in ('glc', 'slc', 'dlc'): diff --git a/tools/startup/gfx2hlsl.py b/tools/startup/gfx2hlsl.py index dcfdf2ab1e..f3d4f38c9d 100644 --- a/tools/startup/gfx2hlsl.py +++ b/tools/startup/gfx2hlsl.py @@ -3895,8 +3895,16 @@ def image(self, ops, mods, how): else: r = reg_range(coords) cs = ['v%d' % n for n in range(r[1], r[2] + 1)] - if one and (how != 'sample' or len(cs) != 1 or not dmask or any(not m.startswith(('dim:', 'dmask:')) for m in mods)): - raise Unsupported('unverified 1D image sample address or modifiers') + # A 1D image has one coordinate. sample_lz and sample_l carry the explicit level in the next + # address dword, so their address is x then level; Texture1D.SampleLevel takes the location as a + # float, which is what the `one` branch below builds. Anything else stays refused by name. + if one: + expected = {'sample': 1, 'lz': 1, 'l': 2}.get(how) + if expected is None or len(cs) != expected or not dmask: + raise Unsupported('unverified 1D image %s address or modifiers' % how) + for m in mods: + if not (m.startswith(('dim:', 'dmask:')) or (how != 'sample' and m in ('dlc', 'glc', 'slc'))): + raise Unsupported('1D image %s modifier %s' % (how, m)) lod = bias = None if how in ('l', 'b'): # image_sample_l: the address dwords are the coordinates and then the explicit level. image_sample_b: @@ -3912,7 +3920,8 @@ def image(self, ops, mods, how): raise Unsupported('%s modifier %s' % (name, m)) if dmask == 0: raise Unsupported('%s with an empty dmask' % name) - if len(cs) != (4 if three else 3): + expected = 4 if three else (2 if one else 3) + if len(cs) != expected: raise Unsupported('%s with %d address registers' % (name, len(cs))) if how == 'l': lod, cs = cs[-1], cs[:-1] From 8e04d8de19ffb2ba21994a31a5865a3f4f5953f0 Mon Sep 17 00:00:00 2001 From: Royoo <3230853833@qq.com> Date: Thu, 26 Nov 2026 10:16:36 +0800 Subject: [PATCH 2/3] Draw the 1D explicit LOD cases against distinct mip values --- CMakeLists.txt | 4 ++ docs/experiments.md | 6 +++ tests/offscreen_check.cpp | 11 +++-- tests/startup_gfx2hlsl_image_1d_check.py | 57 +++++++++++++++++++++++- tests/startup_image_3d.cpp | 2 +- 5 files changed, 73 insertions(+), 7 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 6c1da07e05..435fc17873 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3262,6 +3262,10 @@ if(EXISTS "${INTAKE_PYTHON}") "${CMAKE_SOURCE_DIR}/tests/startup_image_3d_check.py" draw "${DXC_EXECUTABLE}" "$" ${adapter} $<$:${STARTUP_FXC}>) set_tests_properties(startup_image_3d_${adapter} PROPERTIES TIMEOUT 120 SKIP_RETURN_CODE 77) + add_test(NAME startup_gfx2hlsl_image_1d_${adapter} COMMAND "${INTAKE_PYTHON}" -B + "${CMAKE_SOURCE_DIR}/tests/startup_gfx2hlsl_image_1d_check.py" draw "${DXC_EXECUTABLE}" + "$" ${adapter}) + set_tests_properties(startup_gfx2hlsl_image_1d_${adapter} PROPERTIES TIMEOUT 120 SKIP_RETURN_CODE 77) add_test(NAME startup_image_load_mip_${adapter} COMMAND "${INTAKE_PYTHON}" -B "${CMAKE_SOURCE_DIR}/tests/startup_image_load_mip_check.py" --draw "${DXC_EXECUTABLE}" "$" ${adapter}) diff --git a/docs/experiments.md b/docs/experiments.md index f4b29da331..7287c6728f 100644 --- a/docs/experiments.md +++ b/docs/experiments.md @@ -256,6 +256,12 @@ nonempty component mask. The image draw test samples the alpha channel of a two- Texture1D at two coordinates and compares exact words. Selecting red changes both results. Run `ctest --preset dev -R startup_image_3d --no-tests=error` for this and the 3D controls. +The explicit-level fixture binds a three-mip Texture1D with a distinct value per mip. +With LOD inputs 1 and 2, `image_sample_lz` reads level 0 and `image_sample_l` reads the +provided level. A variant that fixes the explicit LOD at zero returns the level-zero +value when the reference expects level 2. Run +`ctest --preset dev --no-tests=error -j 1 -R '^startup_gfx2hlsl_image_1d($|_)' -V`. + The runtime binds an explicit or default 1D view and uploads single-array-slice CPU-backed textures using their desktop copyable footprints. Run `ctest --preset dev -R '^startup_1d_srv_' --no-tests=error` to check RGBA8 texel and alpha values, rebinding, and a mismatched 2D view on hardware and WARP. diff --git a/tests/offscreen_check.cpp b/tests/offscreen_check.cpp index 57f64c8cf8..7592b7809d 100644 --- a/tests/offscreen_check.cpp +++ b/tests/offscreen_check.cpp @@ -1268,8 +1268,10 @@ OperationResult run_startup_sampler_heap(const OperationContext& ctx) // mip levels whose cells all differ (tests/startup_image_3d.cpp). OperationResult run_startup_image_3d(const OperationContext& ctx) { - const bool mips = std::strcmp(ctx.argv[2], "--startup-image-2d-array-mips") == 0; - const bool array = mips || std::strcmp(ctx.argv[2], "--startup-image-2d-array") == 0; + const bool array_mips = std::strcmp(ctx.argv[2], "--startup-image-2d-array-mips") == 0; + const bool one_dimensional_mips = std::strcmp(ctx.argv[2], "--startup-image-1d-mips") == 0; + const bool array = array_mips || std::strcmp(ctx.argv[2], "--startup-image-2d-array") == 0; + const bool one_dimensional = one_dimensional_mips || std::strcmp(ctx.argv[2], "--startup-image-1d") == 0; const bool two_dimensional = std::strcmp(ctx.argv[2], "--startup-image-2d") == 0; float z = 0.0f; if (two_dimensional && ctx.argv[5][0] == '0' && (ctx.argv[5][1] == 'x' || ctx.argv[5][1] == 'X')) { @@ -1286,8 +1288,8 @@ OperationResult run_startup_image_3d(const OperationContext& ctx) return {OutcomeKind::fail, "expected value is not four hex words"}; float color[4]{}; const char* sampler = ctx.argc == 8 ? ctx.argv[7] : "smp 0 0x0 0x0 0x0 0x0"; - check_startup_image_3d(ctx.device, ctx.argv[3], ctx.argv[4], z, color, sampler, array, mips ? 3 : 1, - std::strcmp(ctx.argv[2], "--startup-image-1d") == 0, two_dimensional); + check_startup_image_3d(ctx.device, ctx.argv[3], ctx.argv[4], z, color, sampler, array, + array_mips || one_dimensional_mips ? 3 : 1, one_dimensional, two_dimensional); std::uint32_t bits[4]; std::memcpy(bits, color, sizeof(bits)); if (std::memcmp(bits, expected, sizeof(bits)) != 0) { @@ -1581,6 +1583,7 @@ const OperationSpec kOperations[] = { {"--sampler-index-control", 3, 3, nullptr, kGateSm60SamplerIndexControl, run_sampler_index_control}, {"--startup-sampler-heap", 7, 9, nullptr, kNoGate, run_startup_sampler_heap}, {"--startup-image-1d", 7, 8, nullptr, kNoGate, run_startup_image_3d}, + {"--startup-image-1d-mips", 7, 8, nullptr, kNoGate, run_startup_image_3d}, {"--startup-image-2d", 7, 8, nullptr, kNoGate, run_startup_image_3d}, {"--startup-image-2d-view", 7, 7, nullptr, kNoGate, run_startup_image_2d_view}, {"--startup-image-3d", 7, 8, nullptr, kNoGate, run_startup_image_3d}, diff --git a/tests/startup_gfx2hlsl_image_1d_check.py b/tests/startup_gfx2hlsl_image_1d_check.py index 0d355c3bd3..d24436c815 100644 --- a/tests/startup_gfx2hlsl_image_1d_check.py +++ b/tests/startup_gfx2hlsl_image_1d_check.py @@ -1,6 +1,6 @@ """1D image sampling in tools/startup/gfx2hlsl.py (issue #1721). -usage: startup_gfx2hlsl_image_1d_check.py +usage: startup_gfx2hlsl_image_1d_check.py [draw ] A 1D image has one coordinate, and `image_sample_lz` / `image_sample_l` carry the explicit level in the next address dword. The translator accepted 1D only for the implicit-level `image_sample`, so the two explicit-level @@ -14,7 +14,10 @@ """ import os import struct +import subprocess import sys +import tempfile +from pathlib import Path here = os.path.dirname(os.path.abspath(__file__)) sys.path.insert(0, os.path.join(here, '..', 'tools')) @@ -155,4 +158,54 @@ def refused(code): for text in failures: print(' ' + text) sys.exit(1) -print('PASS') \ No newline at end of file +print('PASS') + + +def draw(dxc, renderer, adapter): + from startup_image_3d_check import FULLSCREEN_VS, WORDS_1D, compile_hlsl, translate as translate_pair + + # Use the existing Texture1D draw, with v1 reading the provided root constant for the explicit LOD. + sample = encode_mimg(IMG_SAMPLE, vdata=0, srsrc=16, ssamp=24) + words = WORDS_1D.replace(bytes.fromhex('ff02027e0000803e'), bytes.fromhex('0902027e')) + assert sample in words + + def texel(level): + x = (4 >> level) * 3 // 4 + rgba = ((1 + x + 32 * level) / 128, x, 0, 0.75 if x else 0.5) + return ','.join(f'{struct.unpack(" 1: + if len(sys.argv) != 5 or sys.argv[1] != 'draw': + sys.exit(__doc__) + draw(*sys.argv[2:]) diff --git a/tests/startup_image_3d.cpp b/tests/startup_image_3d.cpp index 64f0a5e674..954efa5a72 100644 --- a/tests/startup_image_3d.cpp +++ b/tests/startup_image_3d.cpp @@ -70,7 +70,7 @@ void check_startup_image_3d(ID3D12Device* device, const char* vertex_path, const float color[4], const char* sampler_line, bool array, UINT levels, bool one_dimensional, bool two_dimensional, UINT first_mip, UINT view_levels) { - if (levels < 1 || levels > max_levels || (levels > 1 && !array && !two_dimensional)) + if (levels < 1 || levels > max_levels || (levels > 1 && !array && !two_dimensional && !one_dimensional)) throw std::runtime_error("Unsupported image mip level count"); if (view_levels && (!two_dimensional || first_mip >= levels || view_levels > levels - first_mip)) throw std::runtime_error("2D mip window exceeds the resource"); From ec0c3137be7cd8d8d3518070af21d0c5672d4573 Mon Sep 17 00:00:00 2001 From: Royoo <3230853833@qq.com> Date: Thu, 26 Nov 2026 10:40:13 +0800 Subject: [PATCH 3/3] Keep the explicit-gradient refusal control aligned with the 1D diagnostic --- tests/startup_image_sample_d_check.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/startup_image_sample_d_check.py b/tests/startup_image_sample_d_check.py index 719903e18d..d684232c5c 100644 --- a/tests/startup_image_sample_d_check.py +++ b/tests/startup_image_sample_d_check.py @@ -108,7 +108,7 @@ def controls(): refused('cd', 'image_sample_c_d') refused('do', 'image_sample_d_o') - refused('d1', 'unverified 1D image sample address or modifiers') + refused('d1', 'unverified 1D image d address or modifiers') refused('dtfe', 'image_sample_d modifier tfe') print('refused: compare, offset, 1D, tfe')