diff --git a/CMakeLists.txt b/CMakeLists.txt index d52759dd97..113ff4ffe4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3391,6 +3391,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") # Handlers that do not model a modifier they can receive refuse it (#1771). add_test(NAME startup_gfx2hlsl_modifier_free COMMAND "${INTAKE_PYTHON}" -B "${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_gfx2hlsl_modifier_free_check.py") @@ -3684,6 +3687,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 92d6a07453..886cecb61b 100644 --- a/docs/experiments.md +++ b/docs/experiments.md @@ -16209,6 +16209,24 @@ ctest --preset dev --no-tests=error -R '^startup_resource_(retire|budget)' -V ctest --preset dev --no-tests=error -R '^startup_compute_tg_size_' -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 -j 1 -R '^startup_gfx2hlsl_image_1d($|_)' -V +``` + +The draw fixture binds a three-mip Texture1D with a distinct value per mip. With LOD +inputs 1 and 2, the zero-level form reads level 0 and the explicit form reads the +provided level. Forcing the explicit level to zero changes the level-2 readback. + ## Startup translator: v_mad_u32_u16 [`tools/startup/gfx2hlsl.py`](../tools/startup/gfx2hlsl.py) now lowers the unmodified gfx1013 VOP3A instruction `v_mad_u32_u16` (`0x373` in [`tools/disasm_rdna2.py`](../tools/disasm_rdna2.py)) via `VEC_U` as `({0} & 0xffffu) * ({1} & 0xffffu) + {2}` (unsigned 32-bit wraparound). In `Stage.lower`, `S0` and `S1` are read with `low16_only=True` because only bits `[15:0]` of those operands are consumed (allowing odd-dmask `d16` `image_load` results in `S0`/`S1` and clearing `low16_only_v` on the 32-bit destination), while `S2` is read with `low16_only=False` (refusing `low16_only_v` sources) so all 32 bits of the addend are preserved. Modifiers (`op_sel`, `clamp`, `omod`, `abs`, `neg`) and wrong operand counts remain refused. diff --git a/tests/offscreen_check.cpp b/tests/offscreen_check.cpp index 5c1b653c8d..7ed885fd38 100644 --- a/tests/offscreen_check.cpp +++ b/tests/offscreen_check.cpp @@ -1322,8 +1322,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')) { @@ -1340,8 +1342,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) { @@ -1680,6 +1682,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 new file mode 100644 index 0000000000..d24436c815 --- /dev/null +++ b/tests/startup_gfx2hlsl_image_1d_check.py @@ -0,0 +1,211 @@ +"""1D image sampling in tools/startup/gfx2hlsl.py (issue #1721). + +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 +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 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')) +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('> 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 6c0070de66..8305017fdf 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"); diff --git a/tests/startup_image_sample_b_check.py b/tests/startup_image_sample_b_check.py index f72cc4242b..61be14e1e0 100644 --- a/tests/startup_image_sample_b_check.py +++ b/tests/startup_image_sample_b_check.py @@ -453,7 +453,7 @@ def controls(): refused('gather', 'image_gather4_b') refused('cube', 'image dimension SQ_RSRC_IMG_CUBE') refused('msaa', 'image dimension SQ_RSRC_IMG_') - refused('dim1d', 'unverified 1D image sample address or modifiers') + refused('dim1d', 'unverified 1D image b address or modifiers') refused('unorm', 'image_sample_b modifier unorm') refused('tfe', 'image_sample_b modifier tfe') refused('a16', 'image_sample_b modifier a16') 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') diff --git a/tests/startup_image_sample_l_check.py b/tests/startup_image_sample_l_check.py index d406e77dc8..72d4a0fffa 100644 --- a/tests/startup_image_sample_l_check.py +++ b/tests/startup_image_sample_l_check.py @@ -251,11 +251,15 @@ def controls(): refused('d16', 'image_sample_l modifier d16') # half-precision result layout refused('r128', 'image_sample_l modifier r128') # four-dword resource descriptor refused('dmask0', 'empty dmask') - refused('dim1d', 'unverified 1D image sample address or modifiers') + # A 1D image has one coordinate and the level in the next address dword, so it lowers to Texture1D + # with the same SampleLevel call shape a 2D image uses (issue #1721). + one_d = stage('dim1d') + assert 'Texture1D' 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 15b3fa1241..c2686e4d5a 100644 --- a/tools/startup/gfx2hlsl.py +++ b/tools/startup/gfx2hlsl.py @@ -5264,8 +5264,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 grads = None if how == 'd': @@ -5302,7 +5310,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]