From 1c0ab3b1bd1c8af3752afb3aa6fe4c744882f58e Mon Sep 17 00:00:00 2001 From: Leyukaka <45659323+Leyukaka@users.noreply.github.com> Date: Sat, 28 Nov 2026 01:58:28 +0100 Subject: [PATCH] compute: store prefix-mask images at their exact width in direct compute Non-D16 image_store with dmask 0x1, 0x3 or 0x7 (2D, 2D array, 3D) is lowered as a typed UAV store of float, float2 or float3 with every component written, as the D16 path already does for masks 1 and 3. The manifest carries the channel count (`channels 1|2|3`, plus tex3d), and the runtime refuses the dispatch unless the bound texture has exactly that many channels ("an exact-width image store requires a matching output texture channel count") and supports typed UAV stores. No channel is left unwritten, so there is no read-modify-write and no race between invocations. Non-prefix masks stay refused. New test native_image_store_exact_width (WARP and hardware): R32_FLOAT, R32G32_FLOAT and R11G11B10_FLOAT readbacks in 2D, a 2D array and 3D, with sentinel texels, through the published manifest and the native route; an RGBA8 texture with a 0x7 store is refused at run time and a 0x9 mask by the translator. The native pass runs first, so an adapter without 32-lane waves (inbox WARP) reports the capability skip. --- CMakeLists.txt | 8 ++ .../direct-compute-exact-width-image-store.md | 26 ++++ src/direct_compute_cache.cpp | 7 +- src/gfx1013_compute_native.h | 15 ++- src/gfx1013_direct_compute.inl | 9 +- src/startup_stubs/d3d12_compute_dispatch.inl | 60 ++++++--- tests/native_image_store_exact_width_check.py | 116 ++++++++++++++++++ tests/startup_stream_consumer.cpp | 39 ++++-- 8 files changed, 252 insertions(+), 28 deletions(-) create mode 100644 docs/experiments/direct-compute-exact-width-image-store.md create mode 100644 tests/native_image_store_exact_width_check.py diff --git a/CMakeLists.txt b/CMakeLists.txt index 8021175afc..ff12f0859e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1967,6 +1967,14 @@ if(TARGET compile_dxil AND EXISTS "${DXC_BIN_DIR}/dxcompiler.dll" AND EXISTS "${ add_test(NAME native_direct_container COMMAND "${INTAKE_PYTHON}" -B "${CMAKE_SOURCE_DIR}/tests/native_direct_container_check.py" "$" "${DXC_BIN_DIR}") + foreach(adapter IN ITEMS warp hardware) + foreach(case IN ITEMS r rg rgb array volume rgba sparse) + add_test(NAME native_image_store_exact_width_${adapter}_${case} + COMMAND "${INTAKE_PYTHON}" -B "${CMAKE_SOURCE_DIR}/tests/native_image_store_exact_width_check.py" + "$" "${DXC_BIN_DIR}" "$" ${adapter} ${case}) + set_tests_properties(native_image_store_exact_width_${adapter}_${case} PROPERTIES TIMEOUT 90 SKIP_RETURN_CODE 77) + endforeach() + endforeach() add_test(NAME native_dpp_mirror_hardware COMMAND "${INTAKE_PYTHON}" -B "${CMAKE_SOURCE_DIR}/tests/native_dpp_mirror_check.py" "$" "$" "$" "${DXC_BIN_DIR}") diff --git a/docs/experiments/direct-compute-exact-width-image-store.md b/docs/experiments/direct-compute-exact-width-image-store.md new file mode 100644 index 0000000000..dd95da6d9a --- /dev/null +++ b/docs/experiments/direct-compute-exact-width-image-store.md @@ -0,0 +1,26 @@ +# Exact-width image stores in direct compute + +The direct gfx1013 compute translator lowers non-D16 `image_store` with +`dmask:0x1`, `0x3`, or `0x7` on 2D, 2D array, and 3D images. It consumes +one, two, or three consecutive VDATA VGPRs and declares a typed float, +float2, or float3 UAV. The DXIL store uses the full typed-store mask. +The narrower resource type determines which components reach the texture. +The `unorm` instruction modifier continues to leave the integer texel +coordinates unchanged. + +Manifest version 2 describes these outputs with +`uav float table channels <1|2|3>`. +The in-process path gets the same width from the translated binding. +The runtime requires the view and resource formats to have the specified +component count and checks adapter support for typed stores on the resource +format. It creates the UAV with that format and retains the bound subresource +range. This makes each accepted store a whole texel write. Sparse masks still +receive the translator's descriptor modifier refusal. + +The readback fixture dispatches two X groups of 4x2x1 threads into a 32x8 mip. +It explicitly sets EXEC to `0x55`, writing columns 0, 2, 4, and 6 in rows +0 and 1. The array and volume cases use relative slice 1 of a view that +starts at physical slice 1. Every other texel retains its sentinel. +The format cases use R32_FLOAT, R32G32_FLOAT, and R11G11B10_FLOAT. The +RGBA8 control leaves the complete image untouched and checks the runtime +channel-count refusal. diff --git a/src/direct_compute_cache.cpp b/src/direct_compute_cache.cpp index 3ebf00d4e8..3aaba58c17 100644 --- a/src/direct_compute_cache.cpp +++ b/src/direct_compute_cache.cpp @@ -131,7 +131,7 @@ NativeComputeShader decode(std::string_view bytes) { unsigned n = in.count(1000000); shader.bindings.reserve(n); for (unsigned j = 0; j < n; ++j) { - auto kind = static_cast(in.count(static_cast(ComputeViewKind::uint_texture_uav))); + auto kind = static_cast(in.count(static_cast(ComputeViewKind::float3_texture_uav))); ComputeViewBinding b{kind, in.word(), in.word(), in.word(), in.word(), in.word(), in.word(), in.count(7)}; b.scalar_table = static_cast(in.count(unsigned(ComputeViewBinding::ScalarTable::srv))); const bool sampler_heap = kind == ComputeViewKind::heap_sampler; @@ -775,6 +775,11 @@ std::optional format_compute_runtime_manifest(const NativeComputeSh } else if (b.kind == ComputeViewKind::uint_texture_uav && b.image_dimension == 1 && b.array_count == 1) { uav_lines << "uav " << b.shader_register << " tex2d uint table " << b.root_parameter << ' ' << b.byte_offset << " channels 1\n"; + } else if (const auto channels = exact_texture_uav_channels(b.kind); channels && b.array_count == 1 && + (b.image_dimension == 1 || b.image_dimension == 2 || b.image_dimension == 5)) { + const char *dimension = b.image_dimension == 2 ? "tex3d" : b.image_dimension == 5 ? "tex2da" : "tex2d"; + uav_lines << "uav " << b.shader_register << ' ' << dimension << " float table " << b.root_parameter + << ' ' << b.byte_offset << " channels " << channels << '\n'; } else if ((b.kind == ComputeViewKind::texture_uav || b.kind == ComputeViewKind::half4_texture_uav) && (b.image_dimension == 1 || b.image_dimension == 5) && b.array_count == 1) { uav_lines << "uav " << b.shader_register << (b.image_dimension == 5 ? " tex2da" : " tex2d") diff --git a/src/gfx1013_compute_native.h b/src/gfx1013_compute_native.h index 5b039f7514..1ba36eaaa4 100644 --- a/src/gfx1013_compute_native.h +++ b/src/gfx1013_compute_native.h @@ -40,8 +40,19 @@ enum class ComputeViewKind { // dynamic_sampler() (LOD clamp applied, valid for the explicit-LOD forms translated here); entry 2047 is the runtime // fallback and never read. heap_sampler, - uint_texture_uav + uint_texture_uav, + float_texture_uav, + float2_texture_uav, + float3_texture_uav }; +inline unsigned exact_texture_uav_channels(ComputeViewKind kind) { + switch (kind) { + case ComputeViewKind::float_texture_uav: return 1; + case ComputeViewKind::float2_texture_uav: return 2; + case ComputeViewKind::float3_texture_uav: return 3; + default: return 0; + } +} inline constexpr unsigned compute_resource_heap = ~0u; // the directly indexed sampler heap; entry n is title byte offset 16 * n inline constexpr unsigned compute_sampler_heap = ~0u - 1; @@ -57,7 +68,7 @@ struct IndexedAtomicView { inline bool compute_uav(ComputeViewKind kind) { return kind == ComputeViewKind::texture_uav || kind == ComputeViewKind::half_scalar_uav || kind == ComputeViewKind::half2_texture_uav || kind == ComputeViewKind::half4_texture_uav || - kind == ComputeViewKind::uint_texture_uav || compute_buffer_uav(kind); + kind == ComputeViewKind::uint_texture_uav || exact_texture_uav_channels(kind) || compute_buffer_uav(kind); } struct ComputeViewBinding { ComputeViewKind kind; diff --git a/src/gfx1013_direct_compute.inl b/src/gfx1013_direct_compute.inl index 2dcfb7d42f..efed91b370 100644 --- a/src/gfx1013_direct_compute.inl +++ b/src/gfx1013_direct_compute.inl @@ -840,6 +840,8 @@ struct DirectCompute { constexpr unsigned dx_kind[]{1, 2, 4, 5, 6, 7, 3, 8}; constexpr unsigned coord_count[]{1, 2, 3, 3, 2, 3, 2, 3}; const unsigned coords = coord_count[dim]; + const bool exact_store = store && !half && !graphics_input && (dim == 1 || dim == 2 || dim == 5) && + (mask == 1 || mask == 3 || mask == 7); if ((i.op != 0 && i.op != 1 && !store && !sample && !gather && !gather_lod && !dimensions) || !mask || (i.a & 0x3e0c0u) || (i.b & 0x3c000000u) || (gather && ((mask & (mask - 1)) || (dim != 1 && dim != 3 && dim != 5))) || (query && ((dim != 1 && dim != 3) || (mask & ~3u))) || @@ -847,7 +849,7 @@ struct DirectCompute { (compare_gather && dim == 3) || ((sample || gather) && (i.a & 0x1000u) && gradient) || (dimensions && (dim == 4 || dim >= 6 || (i.b & 0x40000000u))) || - (store && !half && mask != 15) || (half && mask != 1 && mask != 3 && mask != 15) || + (store && !half && mask != 15 && !exact_store) || (half && mask != 1 && mask != 3 && mask != 15) || s.at(base).type != Symbol::Type::descriptor) refuse(i.pc, "direct image opcode or descriptor modifiers"); if (gather_lod && (dim != 1 || (mask & (mask - 1)) || (i.a & 0x1006u) || (i.b & 0xc0000000u) || i.dst > 252)) @@ -856,10 +858,13 @@ struct DirectCompute { (dim >= 6 && (sample || gather || store || lod))) refuse(i.pc, "unsupported cube or multisample operation"); const bool d16_sample = !store && (i.b >> 31); - const unsigned channels = half && mask == 1 ? 1 : half && mask == 3 ? 2 : 4; + const unsigned channels = (half || exact_store) && mask == 1 ? 1 : + (half || exact_store) && mask == 3 ? 2 : exact_store ? 3 : 4; const bool cube_sample = dim == 3 && (sample || gather); const auto kind = half ? (channels == 1 ? ComputeViewKind::half_scalar_uav : channels == 2 ? ComputeViewKind::half2_texture_uav : ComputeViewKind::half4_texture_uav) + : exact_store ? (channels == 1 ? ComputeViewKind::float_texture_uav : + channels == 2 ? ComputeViewKind::float2_texture_uav : ComputeViewKind::float3_texture_uav) : store ? ComputeViewKind::texture_uav : cube_sample ? (compare ? ComputeViewKind::cube_comparison_srv : ComputeViewKind::cube_sample_srv) : d16_sample ? ComputeViewKind::d16_sample_srv diff --git a/src/startup_stubs/d3d12_compute_dispatch.inl b/src/startup_stubs/d3d12_compute_dispatch.inl index 59dda9a95b..76f00f7268 100644 --- a/src/startup_stubs/d3d12_compute_dispatch.inl +++ b/src/startup_stubs/d3d12_compute_dispatch.inl @@ -3,7 +3,7 @@ // .cs.cso. Bound from the compute tables recorded by slot 35 and compute root SRVs recorded by slot 43: raw // buffers (`srv buf table ` and `srv buf root ` inputs, `uav table ` // outputs) named by hardware buffer descriptors or 64-bit root SRV addresses, and textures -// (`srv tex2d table ` inputs, `uav tex1d/tex2d/tex2da/tex3d float table channels 4` outputs) named +// (`srv tex2d table ` inputs, `uav tex1d/tex2d/tex2da/tex3d float table channels ` outputs) named // by the token of a view the title created (device slots 18 and 19). `smp`, `smpl` and `smpc` lines become static samplers. Entry // values come from the compute root constants of slots 37 and 39, or from the descriptor a buffer input or output is // bound from: its stride and word 3 (`stride`, `descword3`), and word 3, the upper half of word 1 and the record @@ -36,6 +36,7 @@ struct ComputeTranslation { D3D12_UAV_DIMENSION uav_dimension = D3D12_UAV_DIMENSION_UNKNOWN; bool root = false; bool atomic_uint_texture = false; + unsigned channels = 4; }; u64 key = 0; int state = 0; // 0 not loaded, 1 ready, 2 unavailable @@ -60,19 +61,24 @@ struct ComputeTranslation { } }; -// Formats a `float` typed output may be viewed with: the program stores float4. -bool float_output_format(DXGI_FORMAT format) +// Component counts of the float and normalized formats accepted for typed outputs. +unsigned float_output_channels(DXGI_FORMAT format) { switch (format) { case DXGI_FORMAT_R32G32B32A32_FLOAT: case DXGI_FORMAT_R16G16B16A16_FLOAT: case DXGI_FORMAT_R16G16B16A16_UNORM: - case DXGI_FORMAT_R16G16B16A16_SNORM: case DXGI_FORMAT_R32G32_FLOAT: case DXGI_FORMAT_R10G10B10A2_UNORM: - case DXGI_FORMAT_R11G11B10_FLOAT: case DXGI_FORMAT_R8G8B8A8_UNORM: case DXGI_FORMAT_R8G8B8A8_SNORM: - case DXGI_FORMAT_R16G16_FLOAT: case DXGI_FORMAT_R16G16_UNORM: case DXGI_FORMAT_R16G16_SNORM: case DXGI_FORMAT_R32_FLOAT: - case DXGI_FORMAT_R8G8_UNORM: case DXGI_FORMAT_R8G8_SNORM: case DXGI_FORMAT_R16_FLOAT: case DXGI_FORMAT_R16_UNORM: - case DXGI_FORMAT_R16_SNORM: case DXGI_FORMAT_R8_UNORM: case DXGI_FORMAT_R8_SNORM: case DXGI_FORMAT_B8G8R8A8_UNORM: - return true; + case DXGI_FORMAT_R16G16B16A16_SNORM: case DXGI_FORMAT_R10G10B10A2_UNORM: + case DXGI_FORMAT_R8G8B8A8_UNORM: case DXGI_FORMAT_R8G8B8A8_SNORM: case DXGI_FORMAT_B8G8R8A8_UNORM: + return 4; + case DXGI_FORMAT_R11G11B10_FLOAT: + return 3; + case DXGI_FORMAT_R32G32_FLOAT: case DXGI_FORMAT_R16G16_FLOAT: case DXGI_FORMAT_R16G16_UNORM: + case DXGI_FORMAT_R16G16_SNORM: case DXGI_FORMAT_R8G8_UNORM: case DXGI_FORMAT_R8G8_SNORM: + return 2; + case DXGI_FORMAT_R32_FLOAT: case DXGI_FORMAT_R16_FLOAT: case DXGI_FORMAT_R16_UNORM: + case DXGI_FORMAT_R16_SNORM: case DXGI_FORMAT_R8_UNORM: case DXGI_FORMAT_R8_SNORM: + return 1; default: - return false; + return 0; } } std::unordered_map& compute_translations() { static std::unordered_map m; return m; } @@ -186,10 +192,12 @@ const char* load_compute_translation(ComputeTranslation& tr) tr.uavs.back().uav_dimension = D3D12_UAV_DIMENSION_TEXTURE2D; tr.uavs.back().atomic_uint_texture = true; } - else if (std::sscanf(line.c_str(), "uav %u %15s float table %d %d channels 4%n", &i, name, &a, &b, &used) == 4 && whole() && - i == tr.uavs.size() && uav_dimension() != D3D12_UAV_DIMENSION_UNKNOWN) { + else if (std::sscanf(line.c_str(), "uav %u %15s float table %d %d channels %u%n", &i, name, &a, &b, &x, &used) == 5 && + whole() && i == tr.uavs.size() && x >= 1 && x <= 4 && uav_dimension() != D3D12_UAV_DIMENSION_UNKNOWN && + (x == 4 || uav_dimension() != D3D12_UAV_DIMENSION_TEXTURE1D)) { tr.uavs.push_back({a, b, true}); tr.uavs.back().uav_dimension = uav_dimension(); + tr.uavs.back().channels = x; } else if (std::sscanf(line.c_str(), "srv %u tex2d table %d %d%n", &i, &a, &b, &used) == 3 && whole() && i == tr.srvs.size()) tr.srvs.push_back({a, b, true}); @@ -225,7 +233,7 @@ const char* load_compute_translation(ComputeTranslation& tr) // X2W_NATIVE_SHADERS=: the first Dispatch of a slot 67 request translates // it in this process with the C++ compiler (src/gfx1013_compute_native.cpp), before a published .txt is read // and with no offline translator running. The dispatch path below binds the result, so a program is accepted only -// with root constants, raw table buffers it loads per lane (buffer_load_dword) and raw table buffers it stores to. +// with root constants, raw table buffers and exact-width typed texture outputs. // A refusal is logged and leaves the key to the published translations. draw_lock held. Returns whether tr is ready. bool translate_compute_natively(ComputeTranslation& tr) { @@ -254,14 +262,22 @@ bool translate_compute_natively(ComputeTranslation& tr) std::sort(program.bindings.begin(), program.bindings.end(), [](const auto& a, const auto& b) { return a.shader_register < b.shader_register; }); for (const auto& view : program.bindings) { - // The two kinds a fixture has run through this path: a raw buffer read per lane, and one written. - const bool output = view.kind == x2w::ComputeViewKind::raw_buffer_uav; + const unsigned channels = x2w::exact_texture_uav_channels(view.kind); + const bool output = channels || view.kind == x2w::ComputeViewKind::raw_buffer_uav; auto& slots = output ? tr.uavs : tr.srvs; if ((!output && view.kind != x2w::ComputeViewKind::raw_vector_srv) || view.array_count != 1 || view.root_parameter >= 16 || view.shader_register != slots.size()) throw std::runtime_error("view kind " + std::to_string(static_cast(view.kind)) + " is not bound for a natively translated program"); slots.push_back({static_cast(view.root_parameter), static_cast(view.byte_offset)}); + if (channels) { + auto& slot = slots.back(); + slot.texture = true; + slot.channels = channels; + slot.uav_dimension = view.image_dimension == 2 ? D3D12_UAV_DIMENSION_TEXTURE3D : + view.image_dimension == 5 ? D3D12_UAV_DIMENSION_TEXTURE2DARRAY : + D3D12_UAV_DIMENSION_TEXTURE2D; + } } if (!program.samplers.empty() || tr.uds.size() > 32 || tr.srvs.size() > 32 || tr.uavs.size() > 8) throw std::runtime_error("a sampler, or more entry values, inputs or outputs than the dispatch path binds"); @@ -550,7 +566,19 @@ const char* record_translated_dispatch(Wrapper* self, const TranslatedDispatch& if (!reason && x2w_startup::packed_target_format(format)) reason = "an output texture view has a packed format"; if (!reason && slot.atomic_uint_texture && format != DXGI_FORMAT_R32_UINT) reason = "an image atomic requires an R32_UINT UAV"; - if (!reason && !slot.atomic_uint_texture && !float_output_format(format)) + if (!reason && slot.channels < 4) { + if (float_output_channels(format) != slot.channels || float_output_channels(resource.Format) != slot.channels) + reason = "an exact-width image store requires a matching output texture channel count"; + else { + D3D12_FEATURE_DATA_FORMAT_SUPPORT support{resource.Format}; + if (FAILED(device_of(the_device)->CheckFeatureSupport(D3D12_FEATURE_FORMAT_SUPPORT, &support, + sizeof(support))) || + !(support.Support2 & D3D12_FORMAT_SUPPORT2_UAV_TYPED_STORE)) + reason = "an exact-width image store requires typed UAV store support"; + desc.Format = resource.Format; + } + } + if (!reason && !slot.atomic_uint_texture && !float_output_channels(format)) reason = "an output texture format is not a float or normalized one"; // One state per resource here: a texture read and written by the same call is refused. Two outputs // of one texture (two mips) share the unordered access state. diff --git a/tests/native_image_store_exact_width_check.py b/tests/native_image_store_exact_width_check.py new file mode 100644 index 0000000000..b5b4ba620c --- /dev/null +++ b/tests/native_image_store_exact_width_check.py @@ -0,0 +1,116 @@ +"""Compile source-built gfx1013 image stores and exercise the startup Dispatch binder.""" +from pathlib import Path +import subprocess +import sys +import tempfile + +from native_direct_container_check import words +from startup_compute_dispatch_check import lines +from startup_compute_dispatch_texture_check import clean, run +from startup_compute_image_store_check import ARRAY_END, ARRAY_PROLOGUE, ARRAY_SLICE, RSRC2 +from startup_compute_request_check import DATA, prepare +from startup_compute_translate_check import state_a +from startup_gfx2hlsl_compute_check import root_blob + + +CASES = { + "r": (1, 1, "tex2d", 1), + "rg": (3, 1, "tex2d", 2), + "rgb": (7, 1, "tex2d", 1), + "array": (7, 5, "tex2da", 1), + "volume": (7, 2, "tex3d", 1), + "rgba": (7, 1, "tex2d", 1), + "sparse": (9, 1, "tex2d", 1), +} +OPCODE_REFUSAL = "direct image opcode or descriptor modifiers" +CHANNEL_REFUSAL = "an exact-width image store requires a matching output texture channel count" + + +def program(mask, dimension): + # ARRAY_PROLOGUE loads the image descriptor from table 1 + 64 into s[24:31]. + # v8 = 4 * group_id.x + local_id.x, v9 = 2 * group_id.y + local_id.y. + # v10 = 1 for array/volume. EXEC explicitly selects the even lanes of each 4x2 group. + values = (0x3F800000, 0x40000000, 0x40800000) + data = [] + for i in range(mask.bit_count()): + data.extend((0x7E1C02FF + (i << 17), values[i])) # v_mov_b32 v14+i, literal + store = words(0xF0201000 | (mask << 8) | (dimension << 3), 0x00060E08) + return (ARRAY_PROLOGUE + ARRAY_SLICE[1] + words(0xBF8CC07F) + words(*data) + + words(0xBEFE03FF, 0x55) + store + ARRAY_END) + + +def check(directory, cli, sdk, consumer, adapter, case): + mask, dimension, view, texel_words = CASES[case] + cache, env = prepare(directory) + env.update(X2W_D3D12_LIST_NOOP="1", X2W_D3D12_DEBUG="1", + X2W_TEST_COMPUTE_ROOT=str(directory / "compute_root")) + env.pop("X2W_NATIVE_SHADERS", None) + env.pop("X2W_TEST_DISPATCH_HARDWARE", None) + if adapter == "hardware": + env["X2W_TEST_DISPATCH_HARDWARE"] = "1" + (directory / "compute_root").write_bytes(root_blob(3, table_range=True)) + (directory / "program").write_bytes(program(mask, dimension)) + (directory / "data").write_bytes(DATA) + (directory / "state").write_bytes(state_a((4, 2, 1), RSRC2)) + + # Each mip under test is 32x8. Array/volume use six slices and a view starting + # at slice 1, so the shader's relative coordinate 1 writes physical slice 2. + layers = 6 if dimension != 1 else 1 + initial = [0x30000000 + i for i in range(layers * 32 * 8 * texel_words)] + mode = "texture-exact-" + ("rgba" if case == "sparse" else case) + layout = f"<{len(initial)}I" + got, result = run(directory, consumer, env, mode, layout) + assert got == initial and lines(result, "Dispatch(2, 1, 1) refused: no translation"), result.stderr + key = lines(result, "x2w-draw compute request")[0].split()[3] + compiled = subprocess.run([cli, sdk, directory / "compiler-cache", "gfx1013-direct-cs@0", + "--requests", cache, cache], capture_output=True, text=True, timeout=30) + diagnostic = compiled.stdout + compiled.stderr + manifest = cache / f"{key}.txt" + if case == "sparse": + assert compiled.returncode and OPCODE_REFUSAL in diagnostic and not manifest.exists(), diagnostic + print(diagnostic, end="") + return 0 + assert compiled.returncode == 0 and manifest.is_file(), diagnostic + assert f"uav 0 {view} float table 1 64 channels {mask.bit_count()}" in manifest.read_text().splitlines() + + expected = initial.copy() + if case != "rgba": + # Packed unsigned floats for (1, 2, 4), with 6/6/5 mantissa bits. + packed = 0x3C0 | (0x400 << 11) | (0x220 << 22) + values = {"r": [0x3F800000], "rg": [0x3F800000, 0x40000000]}.get(case, [packed]) + for y in range(2): + for x in (0, 2, 4, 6): # two X groups, local lanes 0, 2, 4 and 6 in each group + i = (((2 if layers > 1 else 0) * 8 + y) * 32 + x) * texel_words + expected[i:i + texel_words] = values + + # Native translation first with the published manifest removed (no fallback), so an + # adapter without 32-lane waves reports the capability skip before any PSO is made; + # then bind solely from the restored manifest. + published = manifest.read_bytes() + for native in (True, False): + if native: + manifest.unlink() + env["X2W_NATIVE_SHADERS"] = sdk + else: + manifest.write_bytes(published) + env.pop("X2W_NATIVE_SHADERS", None) + got, result = run(directory, consumer, env, mode, layout) + if native and lines(result, "not translated natively: the adapter has no 32-lane waves"): + assert got == initial, result.stderr + return 77 + assert got == expected, (case, native, result.stderr, + next(i for i, (actual, want) in enumerate(zip(got, expected)) if actual != want)) + if native: + assert lines(result, "translated natively:"), result.stderr + if case == "rgba": + assert lines(result, f"Dispatch(2, 1, 1) refused: {CHANNEL_REFUSAL}"), result.stderr + else: + assert lines(result, "Dispatch(2, 1, 1) translated"), result.stderr + clean(result) + print(f"{adapter} {case}: checked all {len(initial)} words through both binders") + return 0 + + +if __name__ == "__main__": + with tempfile.TemporaryDirectory() as temp: + sys.exit(check(Path(temp), *sys.argv[1:])) diff --git a/tests/startup_stream_consumer.cpp b/tests/startup_stream_consumer.cpp index 4b5f44da20..30a9bdb414 100644 --- a/tests/startup_stream_consumer.cpp +++ b/tests/startup_stream_consumer.cpp @@ -530,21 +530,23 @@ static int dispatch_slot67(IDXGIAdapter* adapter, void* device, const std::strin // and its view RGBA16_UINT; texture-store-occupied then asks for a view of a texture without unordered access at the same // handle, which keeps the first view; texture-store-self also creates a shader resource view of the texture at table byte 0. // texture-store-mips adds a second UAV of the same texture. The swapped mode reverses the two mip views. -static int texture_slot67(void* device, const std::string& mode) +static int texture_slot67(IDXGIAdapter* adapter, void* device, const std::string& mode) { constexpr UINT width = 32; - const bool volume = mode == "texture-volume" || mode == "texture-volume-bad-w"; + const bool exact = mode.rfind("texture-exact-", 0) == 0; + const bool volume = mode == "texture-volume" || mode == "texture-volume-bad-w" || mode == "texture-exact-volume"; const bool line = mode == "texture-line" || mode == "texture-line-bad-mip"; const UINT first_mip = volume || line ? 1 : 0; const UINT height = line ? 1 : 8; const bool fp32 = volume || line || mode == "texture-array" || mode == "texture-array-2d"; const bool atomic_uint = mode == "texture-store-atomic"; - const UINT layers = volume || mode == "texture-array" || mode == "texture-array-d16" ? 6 : 1; + const UINT layers = volume || mode == "texture-array" || mode == "texture-array-d16" || + mode == "texture-exact-array" ? 6 : 1; const UINT subresources = volume ? 1 : layers; const bool swapped = mode == "texture-store-mips-swapped"; const bool dual = swapped || mode == "texture-store-mips"; const UINT mips = dual ? 2 : 1; - const UINT pixel_bytes = atomic_uint ? 4 : fp32 ? 16 : 8; + const UINT pixel_bytes = exact ? (mode == "texture-exact-rg" ? 8 : 4) : atomic_uint ? 4 : fp32 ? 16 : 8; const UINT pitch = (std::max)(width * pixel_bytes, UINT{D3D12_TEXTURE_DATA_PITCH_ALIGNMENT}); const UINT slice_bytes = pitch * (height + (dual ? height / 2 : 0)), bytes = slice_bytes * layers; const auto offset = [&](UINT layer, UINT mip) { return UINT64{layer} * slice_bytes + (mip ? pitch * height : 0); }; @@ -552,8 +554,23 @@ static int texture_slot67(void* device, const std::string& mode) const auto make = [&](D3D12_HEAP_TYPE type, const D3D12_RESOURCE_DESC& desc, D3D12_RESOURCE_STATES state) { return make_committed(device, type, desc, state); }; - const DXGI_FORMAT format = atomic_uint ? DXGI_FORMAT_R32_UINT : fp32 ? DXGI_FORMAT_R32G32B32A32_FLOAT : + const DXGI_FORMAT format = mode == "texture-exact-r" ? DXGI_FORMAT_R32_FLOAT : + mode == "texture-exact-rg" ? DXGI_FORMAT_R32G32_FLOAT : + mode == "texture-exact-rgba" ? DXGI_FORMAT_R8G8B8A8_UNORM : + exact ? DXGI_FORMAT_R11G11B10_FLOAT : + atomic_uint ? DXGI_FORMAT_R32_UINT : fp32 ? DXGI_FORMAT_R32G32B32A32_FLOAT : mode == "texture-store-uint" ? DXGI_FORMAT_R16G16B16A16_UINT : DXGI_FORMAT_R16G16B16A16_FLOAT; + if (exact) { + ComPtr desktop; + D3D12_FEATURE_DATA_FORMAT_SUPPORT support{format}; + if (FAILED(D3D12CreateDevice(adapter, D3D_FEATURE_LEVEL_11_0, IID_PPV_ARGS(&desktop))) || + FAILED(desktop->CheckFeatureSupport(D3D12_FEATURE_FORMAT_SUPPORT, &support, sizeof(support))) || + !(support.Support2 & D3D12_FORMAT_SUPPORT2_UAV_TYPED_STORE) || + (volume && !(support.Support1 & D3D12_FORMAT_SUPPORT1_TEXTURE3D))) { + std::printf("exact-width image store format %u unavailable\n", static_cast(format)); + return 77; + } + } const auto dimension = volume ? D3D12_RESOURCE_DIMENSION_TEXTURE3D : line ? D3D12_RESOURCE_DIMENSION_TEXTURE1D : D3D12_RESOURCE_DIMENSION_TEXTURE2D; const D3D12_RESOURCE_DESC texture_desc{dimension, 0, width << first_mip, line ? 1 : height << first_mip, @@ -567,7 +584,15 @@ static int texture_slot67(void* device, const std::string& mode) void* const readback = make(D3D12_HEAP_TYPE_READBACK, buffer_desc, D3D12_RESOURCE_STATE_COPY_DEST); void* memory = nullptr; if (!texture || !upload || !readback || FAILED(static_cast(invoke(upload, 8, 0, 0, word(&memory)))) || !memory) return 71; - if (atomic_uint) { + if (exact) { + for (UINT layer = 0; layer < layers; ++layer) + for (UINT y = 0; y < height; ++y) { + auto* row = reinterpret_cast(static_cast(memory) + offset(layer, 0) + y * pitch); + const UINT row_words = width * pixel_bytes / sizeof(UINT); + for (UINT x = 0; x < row_words; ++x) + row[x] = 0x30000000u + (layer * height + y) * row_words + x; + } + } else if (atomic_uint) { auto* words = static_cast(memory); for (UINT y = 0; y < height; ++y) for (UINT x = 0; x < width; ++x) @@ -808,7 +833,7 @@ int main(int argc, char** argv) return dispatch_slot67(adapter.Get(), device, argv[8]); } if (argc == 9 && std::strcmp(argv[4], "slot67") == 0 && std::strncmp(argv[8], "texture", 7) == 0) - return create_slot67(device, argv[5], argv[6], argv[7], argv[8]) ? texture_slot67(device, argv[8]) : 10; + return create_slot67(device, argv[5], argv[6], argv[7], argv[8]) ? texture_slot67(adapter.Get(), device, argv[8]) : 10; if ((argc == 8 || argc == 9) && std::strcmp(argv[4], "slot67") == 0) return create_slot67(device, argv[5], argv[6], argv[7], argc == 9 ? argv[8] : "valid") ? 0 : 10; if (GetEnvironmentVariableW(L"X2W_TEST_TRUNCATED_ONLY", nullptr, 0)) return 0;