diff --git a/.github/workflows/windows.yml b/.github/workflows/windows.yml index a262aad90..226b0925f 100644 --- a/.github/workflows/windows.yml +++ b/.github/workflows/windows.yml @@ -66,6 +66,7 @@ jobs: (["tests/pixel_attribute_sampled_check.py", *COMMON_LLVM], {}), (["tests/compute_mimg_msaa_check.py", "--binary", "llvm-mc-15", "llvm-objcopy-15", "libLLVM-15.so.1"], {}), (["tests/compute_mimg_float_store_check.py", "--binary", "llvm-mc-15", "llvm-objcopy-15", "--llvm", "libLLVM-15.so.1"], {}), + (["tests/compute_mimg_image_uav_load_check.py", "--binary", "llvm-mc-15", "llvm-objcopy-15", "--llvm", "libLLVM-15.so.1"], {}), (["tests/compute_mimg_float_store_d16_check.py", "--binary", "llvm-mc-15", "llvm-objcopy-15", "--llvm", "libLLVM-15.so.1"], {}), (["tests/compute_image_atomics_gfx1013_check.py", "--binary", "llvm-mc-15", "llvm-objcopy-15", "--llvm", "libLLVM-15.so.1"], {}), (["tests/mesh_indexed_check.py", *COMMON_LLVM], {}), diff --git a/CMakeLists.txt b/CMakeLists.txt index 48760486c..cccc6d42a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7298,6 +7298,11 @@ if(EXISTS "${INTAKE_PYTHON}") "$" ${adapter}) set_tests_properties(compute_store_aot_gfx1013_image_store_${adapter} PROPERTIES TIMEOUT 60 SKIP_RETURN_CODE 77 ENVIRONMENT "FORM_COVERAGE_RECORD=${CMAKE_BINARY_DIR}/form_coverage/compute_store_aot_gfx1013_image_store_${adapter}.txt") + add_test(NAME compute_store_aot_gfx1013_image_uav_load_${adapter} COMMAND "${INTAKE_PYTHON}" -B + "${CMAKE_SOURCE_DIR}/tests/compute_mimg_image_uav_load_check.py" --draw "${DXC_EXECUTABLE}" + "$" ${adapter}) + set_tests_properties(compute_store_aot_gfx1013_image_uav_load_${adapter} PROPERTIES TIMEOUT 60 SKIP_RETURN_CODE 77 + ENVIRONMENT "FORM_COVERAGE_RECORD=${CMAKE_BINARY_DIR}/form_coverage/compute_store_aot_gfx1013_image_uav_load_${adapter}.txt") add_test(NAME compute_store_aot_gfx1013_image_store_d16_${adapter} COMMAND "${INTAKE_PYTHON}" -B "${CMAKE_SOURCE_DIR}/tests/compute_mimg_float_store_d16_check.py" --draw "${DXC_EXECUTABLE}" "$" ${adapter}) diff --git a/README.md b/README.md index b76042aa3..93c992bf7 100644 --- a/README.md +++ b/README.md @@ -137,7 +137,7 @@ The control-flow CPU translator supports [source-built string memory transfers]( | gfx1030 atomic buffer counters | Lower [fixed-address MUBUF bounded increment and decrement](docs/experiments.md#gfx1030-fixed-address-mubuf-atomic-counters) for 32 and 64 bits. | `ctest --preset dev --no-tests=error -R '^compute_store_aot_mubuf_counter'` | | gfx1030 atomic buffer reductions | Lower [fixed-address MUBUF integer reductions](docs/experiments.md#gfx1030-fixed-address-mubuf-atomic-reductions) and verify the final value under lane contention. | `ctest --preset dev --no-tests=error -R '^compute_store_aot_mubuf_reduction_'` | | gfx1030 raw vector reads | Lower [indexed `buffer_load_dwordx2/x3/x4`](docs/experiments.md#indexed-gfx1030-raw-vector-reads) and verify channel order, destination/index aliasing and untouched output. | `ctest --preset dev --no-tests=error -R '^compute_store_aot_mubuf_vector_'` | -| gfx1013 float image stores | Lower [full-mask 2D RGBA32 FLOAT stores and explicit-mip SRV reads](docs/experiments.md#gfx1013-2d-float-image-stores-and-mip-loads) and [D16 stores to RGBA16 FLOAT](docs/experiments.md#gfx1013-2d-d16-float-image-stores). Compare all three destination mips, including untouched texels. | `ctest --preset dev --no-tests=error -R '^compute_store_aot_gfx1013_image_store_'` | +| gfx1013 float image stores | Lower [full-mask 2D RGBA32 FLOAT stores and explicit-mip SRV reads](docs/experiments.md#gfx1013-2d-float-image-stores-and-mip-loads) [D16 stores to RGBA16 FLOAT](docs/experiments.md#gfx1013-2d-d16-float-image-stores) and [same-texel UAV loads](docs/experiments.md#gfx1013-same-texel-uav-loads). Compare all three destination mips, including untouched texels. | `ctest --preset dev --no-tests=error -R '^compute_store_aot_gfx1013_image_(store\|uav_load)_'` | | gfx1013 image atomics | Lower [2D `R32_UINT` `image_atomic_add`, `umax` and `cmpswap`](docs/experiments.md#gfx1013-2d-image-atomics) with the gfx1030 lowering and compare every texel of a three-mip texture, including contended adds. | `ctest --preset dev --no-tests=error -R '^compute_store_aot_gfx1013_image_atomics_'` | | gfx1030 image loads | Lower [typed RGBA32 and packed RGBA8 2D reads](docs/experiments.md#gfx1030-2d-image-loads), including explicit mip levels, with 32-bit or `a16` addresses and nonzero `dmask` values. Compare the UAV readback against a CPU reference. | `ctest --preset dev --no-tests=error -R '^compute_store_aot_mimg_(load|mip|packed)_'` | | gfx1030 image queries | Lower [2D mip dimensions and level count](docs/experiments.md#gfx1030-2d-image-resource-queries) for `image_get_resinfo`. | `ctest --preset dev --no-tests=error -R '^compute_store_aot_mimg_resinfo_'` | diff --git a/docs/experiments.md b/docs/experiments.md index c1152ff2e..c549314d5 100644 --- a/docs/experiments.md +++ b/docs/experiments.md @@ -3664,8 +3664,8 @@ For a raw UAV output, `--image-descriptor` names this SRV. For a typed image output, `--image-srv-descriptor` declares a distinct eight-word SRV descriptor. The caller supplies distinct, non-aliasing desktop resources in that combined case. Wait for descriptors with `lgkmcnt(0)` and the load with `vmcnt(0)`. -This profile refuses same-descriptor reads and writes, sample instructions, -atomics, A16, D16 and non-2D dimensions. +The explicit-mip SRV path refuses sample instructions, atomics, A16, D16 and +non-2D dimensions. The source-built fixture loads red and blue from mip 1 into consecutive VGPRs. A full store writes those two words and two distinct constants into 16 unique @@ -3692,6 +3692,41 @@ ctest --preset dev --no-tests=error -j 1 -R '^compute_store_aot_gfx1013_image_st python3 -B tests/compute_mimg_float_store_check.py --binary llvm-mc-15 llvm-objcopy-15 --llvm libLLVM-15.so.1 ``` +### gfx1013 same-texel UAV loads + +For a 2D `R32G32B32A32_FLOAT` image bound at `u0` with `--image-uav-mip 0`, +one plain `image_load` can read the same descriptor as the store. Both +instructions use DMASK `0xf`. The load lowers to +`asuint(outputData[uint2(x, y)])`. Its result VGPRs become available after +`s_waitcnt vmcnt(0)` under the existing wait rules. + +The load must precede the store and name the same two coordinate VGPRs with +the same values. The translator requires the dispatch X and Y IDs and local +Z size 1. The caller must dispatch a single Z group and keep accesses in +bounds. Each lane then reads and writes its own texel. The profile refuses +neighbour reads, shared destinations, a second UAV load, sparse masks, a16, +d16, array and volume views, and explicit-mip reads from the UAV descriptor. +The separate SRV path continues to require a distinct descriptor and resource. + +The fixture rotates RGBA to GBAR in 16 texels and compares every word of all +three destination mips. It checks 1,344 words, including 1,280 unchanged +sentinels. Replacing the load with zero causes 64 mismatches. The fixture +requires `TypedUAVLoadAdditionalFormats` and typed-load and typed-store support +for this format. A missing capability reports skip 77. The readback uses +normal binary32 values. NaNs, denormals, out-of-bounds behavior and native +console execution are outside the measurement. +[D3D12 typed UAV loads](https://learn.microsoft.com/en-us/windows/win32/direct3d12/typed-unordered-access-view-loads) + +```powershell +cmake --build --preset dev --target offscreen_check -j 2 +ctest --preset dev --no-tests=error -j 1 -R '^compute_store_aot_gfx1013_image_uav_load_(warp|hardware)$' -V +``` + +```bash +python3 -B tests/compute_mimg_image_uav_load_check.py +python3 -B tests/compute_mimg_image_uav_load_check.py --binary llvm-mc-15 llvm-objcopy-15 --llvm libLLVM-15.so.1 +``` + ### gfx1013 2D D16 float image stores Declare `--image-format R16G16B16A16_FLOAT` and `--image-uav-mip 0` diff --git a/tests/compute_check.cpp b/tests/compute_check.cpp index a3896560a..b129b4895 100644 --- a/tests/compute_check.cpp +++ b/tests/compute_check.cpp @@ -34,10 +34,12 @@ constexpr UINT texture_atomic_mask = texture_atomic_uint_flag | texture_atomic_s texture_atomic_uint_minmax_flag | texture_atomic_exchange_flag | texture_atomic_cas_flag; constexpr UINT texture_rgba32_float_flag = 1u << 30; -// Texture-store cases use scalar X group counts of 1 or 2, leaving bit 22 free. +// Texture-store cases use only scalar X group counts of 1 or 2, leaving bits 21 and 22 +// free below the format and view selectors. +constexpr UINT texture_uav_load_flag = 1u << 21; constexpr UINT texture_rgba16_float_flag = 1u << 22; constexpr UINT texture_format_mask = texture_rgba8_flag | texture_atomic_mask | texture_rgba32_float_flag | - texture_rgba16_float_flag; + texture_rgba16_float_flag | texture_uav_load_flag; constexpr UINT texture_width = 32, texture_height = 8, texture_channels = 4; constexpr UINT texture_mip_levels = 3; @@ -472,6 +474,10 @@ bool check_compute_texture_store_file(ID3D12Device* device, const char* path, co switch (selector) { case 0: break; case texture_rgba32_float_flag: format = DXGI_FORMAT_R32G32B32A32_FLOAT; break; + case texture_uav_load_flag: + format = DXGI_FORMAT_R32G32B32A32_FLOAT; + required |= D3D12_FORMAT_SUPPORT2_UAV_TYPED_LOAD; + break; case texture_rgba16_float_flag: format = DXGI_FORMAT_R16G16B16A16_FLOAT; break; case texture_rgba8_flag: format = DXGI_FORMAT_R8G8B8A8_UINT; break; case texture_atomic_uint_flag: @@ -499,6 +505,7 @@ bool check_compute_texture_store_file(ID3D12Device* device, const char* path, co default: throw std::runtime_error("Invalid compute image store format selector"); } const bool float_image = selector == texture_rgba32_float_flag; + const bool uav_load = selector == texture_uav_load_flag; const bool half_image = selector == texture_rgba16_float_flag; const UINT words_per_texel = texture_words_per_texel(format); const UINT base_words = texture_width * texture_height * words_per_texel; @@ -509,12 +516,21 @@ bool check_compute_texture_store_file(ID3D12Device* device, const char* path, co std::vector input_texels(float_image ? all_words : 0); if (float_image) source.read(reinterpret_cast(input_texels.data()), all_words * sizeof(UINT)); if (!source || source.peek() != std::char_traits::eof() || !groups || groups > 2 || mip >= texture_mip_levels - || ((selector & texture_atomic_mask) && mip != 0)) + || (((selector & texture_atomic_mask) || uav_load) && mip != 0)) throw std::runtime_error("Invalid compute image store reference"); D3D12_FEATURE_DATA_FORMAT_SUPPORT support{format}; check(device->CheckFeatureSupport(D3D12_FEATURE_FORMAT_SUPPORT, &support, sizeof(support)), "Check texture UAV format"); + if (uav_load) { + D3D12_FEATURE_DATA_D3D12_OPTIONS options{}; + check(device->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS, &options, sizeof(options)), + "Check typed UAV loads for additional formats"); + if (!options.TypedUAVLoadAdditionalFormats) { + std::puts("R32G32B32A32_FLOAT requires TypedUAVLoadAdditionalFormats"); + return false; + } + } if ((support.Support2 & required) != required) { std::printf("format=%u required_support2=0x%x support1=0x%x support2=0x%x\n", static_cast(format), required, static_cast(support.Support1), diff --git a/tests/compute_mimg_image_uav_load.s b/tests/compute_mimg_image_uav_load.s new file mode 100644 index 000000000..87a78812d --- /dev/null +++ b/tests/compute_mimg_image_uav_load.s @@ -0,0 +1,14 @@ +s_mov_b32 s12, s7 +s_mov_b32 s13, 0 +s_load_dwordx8 s[24:31], s[12:13], 0x40 +v_lshl_add_u32 v8, s10, 2, v0 +v_lshl_add_u32 v9, s11, 1, v1 +s_waitcnt lgkmcnt(0) +image_load v[14:17], v[8:9], s[24:31] dmask:0xf dim:SQ_RSRC_IMG_2D +s_waitcnt vmcnt(0) +v_mov_b32_e32 v18, v15 +v_mov_b32_e32 v19, v16 +v_mov_b32_e32 v20, v17 +v_mov_b32_e32 v21, v14 +image_store v[18:21], v[8:9], s[24:31] dmask:0xf dim:SQ_RSRC_IMG_2D +s_endpgm diff --git a/tests/compute_mimg_image_uav_load_check.py b/tests/compute_mimg_image_uav_load_check.py new file mode 100644 index 000000000..7d8d4ddb6 --- /dev/null +++ b/tests/compute_mimg_image_uav_load_check.py @@ -0,0 +1,172 @@ +"""Check the gfx1013 2D RGBA32 FLOAT same-texel UAV load and store.""" +import argparse +import os +from pathlib import Path +import struct +import subprocess +import sys +import tempfile + +from compute_store_check import aot, assemble, disassemble, rejects, store_container, variant + +HERE = Path(__file__).resolve().parent +LINES = (HERE / "compute_mimg_image_uav_load.s").read_text(encoding="ascii").splitlines() +GROUP = (4, 2, 1) +GROUPS = 2 +BINDING = dict(table_reg=7, group_reg=10, group_reg_y=11, image_descriptor=0x40, + image_format="R32G32B32A32_FLOAT", image_uav_mip=0) +LOAD = next(line for line in LINES if line.startswith("image_load")) +STORE = next(line for line in LINES if line.startswith("image_store")) +WAIT = "s_waitcnt vmcnt(0)" +ADDRESS_X, ADDRESS_Y = (line for line in LINES if line.startswith("v_lshl_add_u32")) +READ = "asuint(outputData[uint2(tid.x, tid.y)])" +WORDS = bytes.fromhex( # Little-endian instruction words from LLVM 15.0.7. + "07038cbe 80038dbe 06060cf4 400000fa 080046d7 0a040104 090046d7 0b020504" + " 7fc08cbf 080f00f0 080e0600 703f8cbf 0f03247e 1003267e 1103287e 0e032a7e" + " 080f20f0 08120600 000081bf" +) +UAV_LOAD_FLAG = 1 << 21 # tests/compute_check.cpp texture_uav_load_flag + + +def controls(): + source = aot.lower(LINES, GROUP, **BINDING) + assert "RWTexture2D outputData : register(u0);" in source, source + assert "register(t0)" not in source, source + read = f"uint4 image_load_0 = {READ};" + write = ("outputData[uint2(tid.x, tid.y)] = " + "asfloat(uint4(image_load_0.y, image_load_0.z, image_load_0.w, image_load_0.x));") + assert source.count(read) == source.count(write) == 1, source + assert source.index(read) < source.index(write), source + + load_index = LINES.index(LOAD) + prologue, read_and_store = LINES[:load_index], LINES[load_index:] + # Same address VGPRs, but X is one texel higher at the read than at the write. + neighbour = prologue + ["v_add_nc_u32_e32 v8, 1, v8", LOAD, WAIT, ADDRESS_X] + read_and_store[2:] + same_address = "Image UAV load and store must use the same coordinate VGPRs and values" + rejects(same_address, aot.lower, neighbour, GROUP, **BINDING) + # Identical values in different registers still do not meet this profile's coordinate rule. + copies = ["v_mov_b32_e32 v10, v8", "v_mov_b32_e32 v11, v9"] + different_regs = prologue + copies + variant(read_and_store, (LOAD, LOAD.replace("v[8:9]", "v[10:11]"))) + rejects(same_address, aot.lower, different_regs, GROUP, **BINDING) + unique = "Image UAV load/store requires unique dispatch X/Y coordinates and local Z size 1" + shared_x = variant(LINES, (ADDRESS_X, "v_mov_b32_e32 v8, 0")) + rejects(unique, aot.lower, shared_x, GROUP, **BINDING) + rejects(unique, aot.lower, LINES, (4, 2, 2), **BINDING) + store_data = [f"v_mov_b32_e32 v{reg}, 0x3f800000" for reg in range(18, 22)] + rejects("Image UAV load must precede the image store", aot.lower, + prologue + store_data + [STORE, LOAD, WAIT, "s_endpgm"], GROUP, **BINDING) + rejects("Only one image UAV load is supported", aot.lower, + prologue + [LOAD, WAIT] + read_and_store, GROUP, **BINDING) + rejects("Expected at least one observable store", aot.lower, + variant(LINES, (STORE, "")), GROUP, **BINDING) + rejects("before s_waitcnt vmcnt(0)", aot.lower, variant(LINES, (WAIT, "")), GROUP, **BINDING) + rejects("image UAV load requires full-width mip-zero access", aot.lower, + LINES, GROUP, **(BINDING | {"image_uav_mip": 1})) + + explicit_mip = LOAD.replace("image_load", "image_load_mip").replace("v[8:9]", "v[8:10]") + for changed, diagnostic in ( + (LOAD.replace("dmask:0xf", "dmask:0x5").replace("v[14:17]", "v[14:15]"), + "image UAV load requires full DMASK 0xf"), + (LOAD.replace("v[14:17]", "v[14:15]") + " d16", "Unsupported compute instruction"), + (LOAD.replace("v[8:9]", "v8") + " a16", "image UAV load requires full-width mip-zero access"), + (explicit_mip, "image_load_mip requires a separate SRV"), + (LOAD.replace("IMG_2D", "IMG_2D_ARRAY").replace("v[8:9]", "v[8:10]"), "Unsupported compute instruction"), + (LOAD.replace("IMG_2D", "IMG_3D").replace("v[8:9]", "v[8:10]"), "Unsupported compute instruction"), + ): + rejects(diagnostic, aot.lower, variant(LINES, (LOAD, changed)), GROUP, **BINDING) + rejects("overlapping image SRV descriptor", aot.lower, + LINES, GROUP, **(BINDING | {"image_srv_descriptor": 0x40})) + # A declared SRV at a distinct offset cannot make an explicit-mip load + # from the UAV's SGPRs legal, even after a valid same-texel read. + mixed_load = explicit_mip.replace("v[14:17]", "v[22:25]") + mixed = prologue + [LOAD, WAIT, "v_mov_b32_e32 v10, 0", mixed_load, WAIT] + read_and_store[2:] + rejects("Unverified image descriptor", aot.lower, mixed, GROUP, **(BINDING | {"image_srv_descriptor": 0x60})) + print("Same-texel UAV load translation controls passed", flush=True) + return source + + +def binary(directory, assembler, objcopy, library, source): + code = assemble(directory, assembler, objcopy, LINES) + assert code == WORDS, f"assembled {code.hex()} != recorded {WORDS.hex()}" + print("Same-texel gfx1013 machine words match llvm-mc", flush=True) + if not library: + return + decoded = aot.labelled(disassemble(code, library, offsets=True, cpu=b"gfx1013")) + assert [line for line in decoded if line.startswith("image_")] == [LOAD, STORE] + assert aot.lower(decoded, GROUP, **BINDING) == source + container, output = directory / "image-uav-load.dxbc", directory / "cli.hlsl" + container.write_bytes(store_container(code, GROUP)) + options = [arg for key, value in BINDING.items() for arg in ("--" + key.replace("_", "-"), str(value))] + subprocess.run([sys.executable, "-B", aot.__file__, container, "--output", output, "--llvm", library, + *options], check=True) + assert output.read_text() == source + + +def reference(): + initial, expected = [], [] + for mip in range(3): + width = 32 >> mip + for y in range(8 >> mip): + for x in range(width): + texel = [0x3f800000 + (mip << 16) + 4 * (y * width + x) + c for c in range(4)] + if mip == 0 and x < GROUP[0] * GROUPS and y < GROUP[1]: + initial.extend(texel) + expected.extend((*texel[1:], texel[0])) + else: + sentinels = [word | 0x80000000 for word in texel] + initial.extend(sentinels) + expected.extend(sentinels) + assert len(initial) == len(expected) == 1344 + assert sum(a != b for a, b in zip(initial, expected, strict=True)) == 64 + return initial + expected + + +def draw(directory, compiler, renderer, adapter, source): + hlsl, cso, case = (directory / f"image-uav-load.{ext}" for ext in ("hlsl", "cso", "case")) + payload = reference() + case.write_bytes(struct.pack(f"<2I{len(payload)}I", GROUPS | UAV_LOAD_FLAG, 0, *payload)) + without_read = source.replace(READ, "uint4(0u, 0u, 0u, 0u)") + assert without_read != source + if record := os.environ.get("FORM_COVERAGE_RECORD"): + path = Path(record) + path.parent.mkdir(parents=True, exist_ok=True) + path.write_text("image_load\nimage_store\n", encoding="ascii") + for label, code, mismatches in (("translated", source, 0), ("drop UAV read", without_read, 64)): + hlsl.write_text(code, encoding="ascii") + subprocess.run([compiler, "-T", "cs_6_0", "-E", "main", "-Fo", cso, hlsl], check=True) + result = subprocess.run([renderer, adapter, "--compute-texture-store-case", cso, case], + capture_output=True, text=True, timeout=15) + print(f"{label}:\n{result.stdout}{result.stderr}", end="", flush=True) + if result.returncode == 77: + return 77 + if "debug_layer=enabled" in result.stdout: + assert "debug_errors=0 debug_warnings=0" in result.stdout, label + assert f"compared_words=1344 mismatches={mismatches}" in result.stdout, label + assert result.returncode == int(mismatches != 0), label + if mismatches: + assert "mip=0 word=0 expected=3f800001 actual=00000000" in result.stderr, label + return 0 + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + modes = parser.add_mutually_exclusive_group() + modes.add_argument("--binary", nargs=2, metavar=("LLVM_MC", "LLVM_OBJCOPY")) + modes.add_argument("--draw", nargs=3, metavar=("DXC", "RENDERER", "ADAPTER")) + parser.add_argument("--llvm", metavar="LIBLLVM", help="Also check disassembly and the container CLI") + args = parser.parse_args() + source = controls() + if not (args.binary or args.draw): + return 0 + out = HERE.parent / "out" + out.mkdir(exist_ok=True) + with tempfile.TemporaryDirectory(prefix="compute_image_uav_load_", dir=out) as tmp: + directory = Path(tmp) + if args.binary: + binary(directory, *args.binary, args.llvm, source) + return 0 + return draw(directory, *args.draw, source) + + +if __name__ == "__main__": + raise SystemExit(main()) diff --git a/tools/compute_store_aot.py b/tools/compute_store_aot.py index 29e344ec7..3cd65bd18 100644 --- a/tools/compute_store_aot.py +++ b/tools/compute_store_aot.py @@ -625,6 +625,8 @@ def lower(instructions, group, *, table_reg, descriptor_offset=None, group_reg=N word index per formatted load, in program order, read by load_descriptor_format (see descriptor_load_hlsl). gfx1013 uses RGBA32 FLOAT for full-mask image_store or image_load_mip. When both are used, image_srv_descriptor supplies the separate t0 descriptor. + One full-mask plain image_load can read the same mip-zero UAV texel its + lane will write. This path requires a dispatch with one Z group. A full-mask 2D D16 image_store requires an RGBA16 FLOAT UAV. mubuf_d16_xyz_mode opts into a caller-verified W-preserving RGBA16 store. wave_size is the caller's statement that the program runs as wave32 (32). A lane index is only @@ -765,6 +767,7 @@ def lower(instructions, group, *, table_reg, descriptor_offset=None, group_reg=N staged_steps = [] ds_step_count = 0 image_step_count = 0 + image_uav_address = None lds_enabled = ds_group and 128 <= lds_bytes <= 65536 lds_word = None lds_strided_words = {} @@ -2157,6 +2160,7 @@ def lower(instructions, group, *, table_reg, descriptor_offset=None, group_reg=N data_words = 2 if d16 else len(channels) data = [int(n) for n in re.findall(r"\d+", m["data"])] first, last = data[0], data[-1] + uav_load = rgba32_float_image and image_output and m["kind"] == "load" and not mip if d16: if not write or a16 or mip or m["packed"]: raise ValueError("Unsupported compute instruction: D16 requires a 2D image_store " @@ -2172,15 +2176,26 @@ def lower(instructions, group, *, table_reg, descriptor_offset=None, group_reg=N raise ValueError("D16 image_store requires exactly two consecutive data VGPRs") elif rgba16_float_image: raise ValueError("RGBA16 FLOAT image UAV requires a D16 image_store") + elif uav_load: + if a16 or m["packed"] or image_uav_mip != 0: + raise ValueError("Unsupported image load target: image UAV load requires full-width mip-zero access") + if dmask != 0xf: + raise ValueError("Unsupported image load target: image UAV load requires full DMASK 0xf") + if stores: + raise ValueError("Image UAV load must precede the image store") + if image_uav_address is not None: + raise ValueError("Only one image UAV load is supported") elif cpu == "gfx1013" and (not rgba32_float_image or a16 or m["packed"] or (m["kind"], mip) not in (("store", False), ("load", True))): raise ValueError(f"Unsupported image {m['kind']} target: " "gfx1013 requires full-width 2D RGBA32 FLOAT image_store or image_load_mip") if rgba32_float_image and write and dmask != 0xf: raise ValueError("gfx1013 RGBA32 FLOAT image_store requires full DMASK 0xf") - separate_srv = not write and image_srv_descriptor is not None + separate_srv = not write and not uav_load and image_srv_descriptor is not None + if rgba32_float_image and image_output and m["kind"] == "load" and mip and not separate_srv: + raise ValueError("Unsupported image load target: image_load_mip requires a separate SRV") if ((image_format not in IMAGE_FORMATS and not float_image) or guard_span is not None - or (write != image_output and not separate_srv) + or (write != image_output and not separate_srv and not uav_load) or len(vaddr) != min(address_words, 2) or vaddr[-1] != vaddr[0] + address_words - 1 or vaddr[-1] > 255 or last != first + data_words - 1 or not 0 < dmask < (1 << (4 if float_image else IMAGE_FORMATS[image_format][1])) @@ -2212,6 +2227,13 @@ def lower(instructions, group, *, table_reg, descriptor_offset=None, group_reg=N level = coordinates[-1] if mip else 0 if not isinstance(level, int) or (level & 0xffff if a16 else level) != image_uav_mip: raise ValueError("Image store mip does not match the UAV view") + if image_uav_address is not None: + if image_uav_address != (vaddr, coordinates): + raise ValueError("Image UAV load and store must use the same coordinate VGPRs and values") + # Dispatch X/Y are injective in a single Z plane. The caller must + # dispatch one Z group, and the shader must have one local Z lane. + if coordinates != [("thread", 0), ("thread", 1)] or dims[2] != 1: + raise ValueError("Image UAV load/store requires unique dispatch X/Y coordinates and local Z size 1") values = [vector.get(first + index) for index in range(data_words)] if not all(is_word(value) for value in values): raise ValueError("Unverified image store data") @@ -2239,13 +2261,16 @@ def lower(instructions, group, *, table_reg, descriptor_offset=None, group_reg=N staged_steps.append((None, f" uint4 {name};\n" f" imageData.GetDimensions({level}, {name}.x, {name}.y, {name}.w);")) else: - lod = "0" - if mip: - mip_word = hlsl_word(coordinates[-1]) - lod = f"asint({mip_word}{' & 0xffffu' if a16 else ''})" - address = f"int3(asint({x}), asint({y}), {lod})" name = f"image_load_{image_step_count}" - load = f"imageData.Load({address})" + if uav_load: + image_uav_address = (vaddr, coordinates) + load = f"outputData[uint2({x}, {y})]" + else: + lod = "0" + if mip: + mip_word = hlsl_word(coordinates[-1]) + lod = f"asint({mip_word}{' & 0xffffu' if a16 else ''})" + load = f"imageData.Load(int3(asint({x}), asint({y}), {lod}))" if rgba32_float_image: staged_steps.append((None, f" uint4 {name} = asuint({load});")) else: