From 7e2560a1da43aa97512ca88775a0b5f3785e687e Mon Sep 17 00:00:00 2001 From: timbot103 <306940367+timbot103@users.noreply.github.com> Date: Wed, 2 Dec 2026 18:17:47 +1000 Subject: [PATCH] shader: extend native pixel descriptor waterfalls Part of #3814 --- README.md | 1 + docs/experiments/native-pixel-waterfall-w6.md | 39 ++++ src/gfx1013_direct_compute.inl | 2 +- src/gfx1013_direct_graphics.inl | 22 +- src/gfx1013_pixel_waterfall.inl | 97 +++++--- tests/cmake/native_pixel_waterfall_w6.cmake | 15 ++ tests/native_pixel_waterfall_w6_check.py | 214 ++++++++++++++++++ tests/native_sampled_pixel_gpu.cpp | 15 +- 8 files changed, 366 insertions(+), 39 deletions(-) create mode 100644 docs/experiments/native-pixel-waterfall-w6.md create mode 100644 tests/cmake/native_pixel_waterfall_w6.cmake create mode 100644 tests/native_pixel_waterfall_w6_check.py diff --git a/README.md b/README.md index 513d409af..6cd91e713 100644 --- a/README.md +++ b/README.md @@ -139,6 +139,7 @@ The control-flow CPU translator supports [source-built string memory transfers]( | gfx1013 pixel DPP quad broadcast | Lower `quad_perm:[k,k,k,k]` on VOP1/VOP2 after `s_wqm exec, exec` in a straight-line pixel stage to `QuadReadLaneAt` (`ps_6_0`, DXC); draw 16 x 16 pixels and compare with a CPU reference. [Scope and limits](docs/experiments.md#gfx1013-pixel-dpp16-quad-broadcast) | `ctest --preset dev --no-tests=error -R "^startup_gfx2hlsl_dpp_quad_broadcast" -V` | | gfx1013 compute readlane | Lower a statically selected, defined `v_readlane_b32` source through logical wave32 group-shared staging. [Scope and limits](docs/experiments.md#gfx1013-compute-readlane) | `ctest --preset dev --no-tests=error -R '^compute_store_aot_readlane_' -V` | | gfx1013 direct compute partial waves | Run partial final guest wave32 and wave64 groups with exact entry EXEC masks and compare raw UAV readback on WARP and hardware. [Lane mapping and limits](docs/experiments/native-compute-partial-final-wave.md) | `ctest --preset dev --no-tests=error -R '^native_partial_final_wave_' -V` | +| gfx1013 pixel descriptor waterfalls | Lower bounded per-lane texture selection through a root table or the resource heap. Check nonuniform DXIL handles and draw source-built explicit-level, explicit-gradient and mip-load fixtures. [Shapes and readback](docs/experiments/native-pixel-waterfall-w6.md) | `ctest --preset dev --no-tests=error -R '^native_pixel_waterfall_w6_' -V` | | gfx1030 typed-buffer writes | Lower [indexed UINT `tbuffer_store_format_*`](docs/experiments.md#indexed-gfx1030-typed-buffer-writes) for one to four 32-bit or packed 16-bit components. | `ctest --preset dev --no-tests=error -R '^compute_store_aot_mtbuf_store_'` | | Static shader recompilation | Translate pixel shader fixtures to desktop DXIL and check the output, including all 32 bits of an unsigned conversion. The title alias-table profile reads one to four DWORDs per FLAT load or GLOBAL load with `off`. FLAT requires `flat_global_only=True`, with the caller excluding the scratch and LDS apertures. The x3 fixture checks 35 draws across 19 shader and address cases. Select a local record by name and, for duplicate names, by decoded DXBC SHA-256. [Supported instructions and bindings](docs/experiments.md#offline-pixel-shader-recompilation) · [Float-to-uint readback and entry inputs](docs/experiments.md#unsigned-pixel-conversion-and-entry-inputs) · [DS swizzle and no-op](docs/experiments.md#quad-local-ds-swizzle-in-pixel-shaders) | `ctest --preset dev -R '^pixel_aot_' -V` | | gfx1030 raw subword writes | Lower [indexed byte and halfword MUBUF stores](docs/experiments.md#indexed-gfx1030-raw-subword-writes) with a single writer to the containing DWORD and verify that neighboring bytes stay intact. | `ctest --preset dev --no-tests=error -R '^compute_store_aot_mubuf_store_subword_'` | diff --git a/docs/experiments/native-pixel-waterfall-w6.md b/docs/experiments/native-pixel-waterfall-w6.md new file mode 100644 index 000000000..435d72979 --- /dev/null +++ b/docs/experiments/native-pixel-waterfall-w6.md @@ -0,0 +1,39 @@ +# Native gfx1013 pixel descriptor waterfalls + +The direct pixel frontend can project a single-block descriptor waterfall +onto individual lanes. The pass reads a lane key, selects lanes with that key, +loads an eight-DWORD SRV descriptor and executes one read-only image operation. +`s_andn2_wrexec` removes the sampled lanes, and a separate saved EXEC restores +coverage on exit. A pass can select lanes with `v_cmpx_eq` or with `v_cmp_eq` +followed immediately by `s_and_saveexec` at the guest mask width. It can also +contain `s_clause` and shift the key into a separate SGPR. + +The aligned SMEM immediate shifts the proven slot range before checking the +root table or declaring a logical heap span. The image still has to satisfy +the direct texture-view and sampler checks. The added image operations are +`image_load_mip`, `image_get_resinfo`, `image_sample_d`, `image_sample_l`, +`image_gather4_l` and `image_gather4_lz`. Biased samples and implicit gathers +would need a per-quad derivative proof. Comparison samples need a nonuniform +comparison-view path. + +```powershell +cmake --build --preset dev --target compile_dxil native_sampled_pixel_gpu +ctest --preset pr --no-tests=error -R '^native_pixel_waterfall_w6_controls$' -V +$env:XBOX2WINDOWS_ADAPTER = 'RTX 5080' +ctest --preset dev --no-tests=error -R '^native_pixel_waterfall_w6_(warp|hardware)$' -V +``` + +The controls check ten bound-table translations and one heap translation. They +check the table manifest offsets, nonuniform handles, and expected DXIL image +calls. Refusal controls cover scalar values that escape the pass, incorrect +compare inputs, overlapping destinations, and misaligned, wrapping or +out-of-range table spans. + +The draw fixture compares 28 covered pixels and four scissored pixels for each +of four shaders. It checks alternating slots, an aligned immediate that shifts +selection from slots 0 and 1 to slots 1 and 2, and mip 1 selection through a +varying-key explicit-LOD sample, explicit gradients, and an integer texel load. +The fixture was checked in Debug and Release on WARP and on an RTX 5080 +(driver 32.0.16.2140, Windows 11 build 26200.9974, D3D12 debug layer off). +These measurements cover source-built shaders. The effect on a package's +shader census remains unmeasured. diff --git a/src/gfx1013_direct_compute.inl b/src/gfx1013_direct_compute.inl index 749b71c80..1a5be015f 100644 --- a/src/gfx1013_direct_compute.inl +++ b/src/gfx1013_direct_compute.inl @@ -1406,7 +1406,7 @@ struct DirectCompute { // table. Declare that span only and index it nonuniformly. const auto &sym = s.at(base); if (kind != ComputeViewKind::texture_srv || (dim != 1 && dim != 5) || base + 8 > 106 || - sym.type != Symbol::Type::indexed_descriptor || sym.value != *selected->table || sym.offset || + sym.type != Symbol::Type::indexed_descriptor || sym.value != *selected->table || (sym.offset & 31) || selected->first > selected->last || selected->last > 0x07ffffffu) refuse(i.pc, "unproven bound-table waterfall descriptor"); State span = s; diff --git a/src/gfx1013_direct_graphics.inl b/src/gfx1013_direct_graphics.inl index 0ee9bd2fc..4900dc8d3 100644 --- a/src/gfx1013_direct_graphics.inl +++ b/src/gfx1013_direct_graphics.inl @@ -2218,8 +2218,18 @@ NativeComputeShader lower_graphics(GuestShaderStage &stage, unsigned first_srv = mask_release.broken = &broken_compare_masks; if (waterfalls.restores.count(pc)) return; if (auto dead=waterfalls.poisoned.find(pc);dead!=waterfalls.poisoned.end()) - for (unsigned reg:dead->second) alu.write(reg,{}); - if (waterfalls.exits.count(pc)) alu.write(128,"0"); // no remaining lanes + for (unsigned reg:dead->second) { + alu.write(reg,{}); + const unsigned mask=stage.wave32 ? reg : reg&~1u; + saved_exec.erase(mask); + invariant_entry_masks.erase(mask); + compare_masks.erase(mask); + broken_compare_masks.erase(mask); + } + if (waterfalls.exits.count(pc)) { + alu.write(128,"0"); // no remaining lanes + compare_scc_unknown=false; + } if (auto access=waterfalls.accesses.find(pc);access!=waterfalls.accesses.end()) { const auto &which=access->second; auto key=alu.read(which.key); @@ -2235,6 +2245,14 @@ NativeComputeShader lower_graphics(GuestShaderStage &stage, unsigned first_srv = const auto &descriptor=state.at(((i.b>>16)&31)*4); if (descriptor.definition!=which.descriptor_pc || !alu.graphics_predicate.empty()) refuse(pc,"waterfall requires the selected descriptor and its enclosing pixel coverage"); + const unsigned base_slot=descriptor.offset/32; + if (last>0x07ffffffu-base_slot) + refuse(pc,"waterfall descriptor span crosses a 32-bit descriptor-byte wrap"); + if (base_slot) { + key=alu.ir.binary("add",key,to_string(base_slot)); + first+=base_slot; + last+=base_slot; + } DirectCompute::PixelHeapIndex lanes_key{key,first,last,true}; if (which.table!=compute_resource_heap) lanes_key.table=which.table; alu.image_io(i,state,lanes_key); diff --git a/src/gfx1013_pixel_waterfall.inl b/src/gfx1013_pixel_waterfall.inl index d9b6f807f..c31f6ac6e 100644 --- a/src/gfx1013_pixel_waterfall.inl +++ b/src/gfx1013_pixel_waterfall.inl @@ -9,11 +9,26 @@ struct PixelWaterfalls { std::map> poisoned; std::set exits, saves, restores; + static bool readonly_image(const Inst &i) { + if (i.enc!=Encoding::Image) return false; + // Use only the existing nonuniform float SRV path. Plain implicit + // sample has explicit quad-gradient lowering in image_io. + switch (i.op) { + case 0: case 1: case 14: // load, load_mip, get_resinfo + case 32: case 34: case 36: case 39: // sample, sample_d, sample_l, sample_lz + case 68: case 71: // gather4_l, gather4_lz + return true; + default: + return false; + } + } + void find(Program &p, bool wave32) { auto &code=p.code; const auto mov=wave32 ? "s_mov_b32" : "s_mov_b64"; + const auto intersect=wave32 ? "s_and_saveexec_b32" : "s_and_saveexec_b64"; const auto remaining_op=wave32 ? "s_andn2_wrexec_b32" : "s_andn2_wrexec_b64"; - auto idle=[](const Inst &i) {return i.name=="s_waitcnt" || i.name=="s_nop";}; + auto idle=[](const Inst &i) {return i.name=="s_waitcnt" || i.name=="s_nop" || i.name=="s_clause";}; auto pair=[&](unsigned r) {return wave32 ? r<=105 : r<=104 && !(r&1);}; for (size_t tail=2; tail+16) reject("the pass has operations besides the key read, compare, shift, descriptor load and image"); - const auto &read=code[body[0]], &cmp=code[body[1]], &load=code[body[body.size()-2]], - &image=code[body.back()]; + const size_t load_index=body[body.size()-2]; + const auto &read=code[body[0]], &cmp=code[body[1]], &load=code[load_index], &image=code[body.back()]; + const bool cmpx=(cmp.name=="v_cmpx_eq_i32" || cmp.name=="v_cmpx_eq_u32") && cmp.dst==126; + const bool compare=(cmp.name=="v_cmp_eq_i32" || cmp.name=="v_cmp_eq_u32") && + (cmp.dst==106 || pair(cmp.dst)); + const bool keys_match=(cmp.src[0]==read.dst && cmp.src[1]==read.src[0]) || + (cmp.src[1]==read.dst && cmp.src[0]==read.src[0]); if (read.src[0]<256 || read.subword || read.dpp_control || - (cmp.name!="v_cmpx_eq_i32" && cmp.name!="v_cmpx_eq_u32") || cmp.dst!=126 || - cmp.src[0]!=read.dst || cmp.src[1]!=read.src[0] || cmp.abs || cmp.neg || cmp.scale || - cmp.clamp || cmp.subword || cmp.dpp_control) + (!cmpx && !compare) || !keys_match || cmp.abs || cmp.neg || cmp.scale || cmp.clamp || + cmp.subword || cmp.dpp_control || (cmp.wide && (cmp.a&0x7800))) reject("the compare does not select the lanes holding the read key"); - if (load.enc!=Encoding::Smem || load.op!=3 || - load.dst>98 || (load.b>>25)!=read.dst || (load.b&0xfffff) || - image.enc!=Encoding::Image || (image.op!=32 && image.op!=39 && image.op!=0) || + size_t next=2; + const Inst *save=nullptr; + if (compare) { + save=&code[body[next++]]; + if (save->pc!=cmp.next || save->name!=intersect || save->src[0]!=cmp.dst || !pair(save->dst)) + reject("s_and_saveexec does not immediately select the compared lanes"); + } + if (body.size()!=next+2 && body.size()!=next+3) + reject("the pass has operations besides the key read, compare, shift, descriptor load and image"); + const Inst *shift=body.size()==next+3 ? &code[body[next]] : nullptr; + const unsigned offset_reg=shift ? shift->dst : read.dst; + if (load.enc!=Encoding::Smem || load.op!=3 || load.dst>98 || (load.b>>25)!=offset_reg || + !readonly_image(image) || ((image.b>>16)&31)*4!=load.dst) reject("the image does not use the descriptor loaded at the read key"); - const auto &table=p.before[body[body.size()-2]].at(load.src[0]); - // A bound table is addressed through its zero-extended 64-bit pointer. - if (table.type!=Symbol::Type::table || table.value==compute_sampler_heap || - (table.value!=compute_resource_heap && - !(p.before[body[body.size()-2]].at(load.src[0]+1)==Symbol{Symbol::Type::constant,0}))) - reject("the descriptor is not read from a bound table or the resource heap"); + if (load.b&31) reject("the descriptor byte offset is not 32-byte aligned"); // The slot or the already shifted byte offset may be the key. - bool bytes=body.size()==4; - if (!bytes) { - const auto &shift=code[body[2]]; - if (shift.name!="s_lshl_b32" || shift.dst!=read.dst || shift.src[0]!=read.dst || - !(source(shift,shift.src[1],p.before[body[2]])==Symbol{Symbol::Type::constant,5})) - reject("the slot key is not shifted to a descriptor byte offset"); - } + if (shift && (shift->name!="s_lshl_b32" || shift->dst>105 || shift->src[0]!=read.dst || + !(source(*shift,shift->src[1],p.before[body[next]])==Symbol{Symbol::Type::constant,5}))) + reject("the slot key is not shifted to a descriptor byte offset"); const unsigned width=wave32 ? 1u : 2u; auto overlaps=[](unsigned a,unsigned na,unsigned b,unsigned nb) {return adst)) + reject("the s_and_saveexec destination overlaps the key, descriptor or EXEC copies"); + const auto &table=p.before[load_index].at(load.src[0]); + // A bound table is addressed through its zero-extended 64-bit pointer. + if (table.type!=Symbol::Type::table || table.value==compute_sampler_heap || + (table.value!=compute_resource_heap && + !(p.before[load_index].at(load.src[0]+1)==Symbol{Symbol::Type::constant,0}))) + reject("the descriptor is not read from a bound table or the resource heap"); // Copies of exactly the entering EXEC must reach both the retry and // the restore. No alternate edge may enter past either copy. auto anchor=[&](unsigned r)->size_t { @@ -109,10 +145,13 @@ struct PixelWaterfalls { // consumers earlier in the shader; only this validated restore // is an identity on the original coverage. forget(read.pc,read.dst,1); + if (compare) forget(cmp.pc,cmp.dst,width); + if (save) forget(save->pc,save->dst,width); + if (shift && shift->dst!=read.dst) forget(shift->pc,shift->dst,1); forget(load.pc,load.dst,8); forget(advance.pc,advance.dst,width); exits.insert(advance.pc); - accesses.emplace(image.pc,Access{read.src[0],load.pc,bytes,table.value}); + accesses.emplace(image.pc,Access{read.src[0],load.pc,!shift,table.value}); for (size_t j=head;j<=tail;++j) if (code[j].pc!=image.pc) GraphicsMaskRegions::skip_proven(code[j]); // The final iteration's uniform descriptor cannot escape the loop diff --git a/tests/cmake/native_pixel_waterfall_w6.cmake b/tests/cmake/native_pixel_waterfall_w6.cmake new file mode 100644 index 000000000..9259a2d02 --- /dev/null +++ b/tests/cmake/native_pixel_waterfall_w6.cmake @@ -0,0 +1,15 @@ +if(TARGET compile_dxil AND EXISTS "${INTAKE_PYTHON}" AND EXISTS "${DXC_BIN_DIR}/dxcompiler.dll" + AND EXISTS "${DXC_BIN_DIR}/dxil.dll") + set(w6_check "${CMAKE_CURRENT_SOURCE_DIR}/tests/native_pixel_waterfall_w6_check.py") + add_test(NAME native_pixel_waterfall_w6_controls COMMAND "${INTAKE_PYTHON}" -B + "${w6_check}" "$" "${DXC_BIN_DIR}") + set_tests_properties(native_pixel_waterfall_w6_controls PROPERTIES TIMEOUT 60) + if(TARGET native_sampled_pixel_gpu) + foreach(adapter IN ITEMS warp hardware) + add_test(NAME native_pixel_waterfall_w6_${adapter} COMMAND "${INTAKE_PYTHON}" -B + "${w6_check}" "$" "${DXC_BIN_DIR}" + "$" ${adapter}) + set_tests_properties(native_pixel_waterfall_w6_${adapter} PROPERTIES SKIP_RETURN_CODE 77 TIMEOUT 60) + endforeach() + endif() +endif() diff --git a/tests/native_pixel_waterfall_w6_check.py b/tests/native_pixel_waterfall_w6_check.py new file mode 100644 index 000000000..243fe42c2 --- /dev/null +++ b/tests/native_pixel_waterfall_w6_check.py @@ -0,0 +1,214 @@ +"""Compile and draw source-built W6 pixel descriptor waterfalls.""" + +import re +import subprocess +import sys +from pathlib import Path +from tempfile import TemporaryDirectory + +from native_direct_container_check import patched_pixel, publish, signature, vertex, words +from native_indexed_pixel_texture_check import MIP_POINT_SAMPLER, NONUNIFORM_SPAN, WATERFALL_TABLE, table_source +from native_scalar_lds_check import sop1, vmove + + +def waterfall(image, *, compare="cmpx", reverse=False, signed=False, wave32=False, clause=False, + separate_shift=False, byte_key=False, offset=0, top_slot=False, + compare_key=50, compare_vgpr=9, save_src=None, save_dst=58, escape=None): + # s50 holds the key read from v9, s[52:53] the parent EXEC, and + # s[54:55] the pending lanes. s[48:49] points to the descriptor table. + move = 3 if wave32 else 4 + block = [0x36121281] # v_and_b32 v9, 1, v9 + if top_slot: + block += [0x381212ff, 0x07fffffe] # v_or_b32 v9, 0x07fffffe, v9 + if byte_key: + block += [0x34121285] # v_lshlrev_b32 v9, 5, v9 + block += [sop1(move, 52, 126), sop1(move, 54, 126)] + head = len(block) + block += [0x7e640509] # v_readfirstlane_b32 s50, v9 + cmp_dst = {"cmpx": 126, "vcc": 106, "sgpr": 56}[compare] + cmp_op = 0xd2 if compare == "cmpx" else 0xc2 + if signed: + cmp_op -= 0x40 + if reverse or compare == "sgpr": + a, b = compare_key, 256 + compare_vgpr + if reverse: + a, b = b, a + block += [0xd4000000 | (cmp_op << 16) | cmp_dst, a | (b << 9)] + else: + block += [0x7c000000 | (cmp_op << 17) | (compare_vgpr << 9) | compare_key] + if compare != "cmpx": + block.append(sop1(0x3c if wave32 else 0x24, save_dst, cmp_dst if save_src is None else save_src)) + offset_reg = 51 if separate_shift else 50 + if not byte_key: + block += [0x8f008532 | (offset_reg << 16)] # s_lshl_b32 sOff, s50, 5 + if clause: + block += [0xbfa10000] # s_clause 0 + block += [0xf40c0418, (offset_reg << 25) | offset, 0xbf8cc07f, *image] # load, wait, image + block += [sop1(0x47 if wave32 else 0x3a, 54, 54)] + block += [0xbf890000 | ((head - len(block) - 1) & 0xffff), sop1(move, 126, 52)] + if escape is not None: + block += [vmove(32, escape)] + return block + + +def image(op, addr, mask=7): + return [0xf0000008 | (op << 18) | (mask << 8), (11 << 21) | (4 << 16) | (32 << 8) | addr] + + +def root_signature(work, sdk, name, declaration): + hlsl, binary = work / f"{name}.hlsl", work / f"{name}.root" + hlsl.write_text(f'#define RS "{declaration}"\n', encoding="ascii") + subprocess.run([sdk / "dxc.exe", "-T", "rootsig_1_1", "-E", "RS", hlsl, "-Fo", binary], check=True) + return binary.read_bytes() + + +def compile_pair(work, compiler, sdk, name, vs, ps, root, *, ps_inputs=None, refusal=None): + request, output = work / name, work / "objects" + key = publish(request, vs, ps, root, ps_inputs=ps_inputs) + result = subprocess.run([compiler, sdk, work / "cache", "gfx1013-container-pair", "--requests", request, output], + capture_output=True, text=True) + manifest = output / f"{key}.txt" + if refusal: + assert result.returncode and refusal in result.stderr and not manifest.exists(), (name, result.stderr) + print(name, "refused") + return None + assert result.returncode == 0, (name, result.stdout, result.stderr) + return manifest.read_text().splitlines(), output / f"{key}.ps.cso", output / f"{key}.vs.cso" + + +def check_dxil(sdk, ps, calls, binding_span=None): + dump = subprocess.check_output([sdk / "dxc.exe", "-dumpbin", ps], text=True) + if binding_span: + assert [(int(lo), int(hi)) for lo, hi in NONUNIFORM_SPAN.findall(dump)] == [binding_span], dump + else: + assert re.search(r"call %dx\.types\.Handle @dx\.op\.createHandleFromHeap\([^\n]+i1 false, i1 true\)", dump), dump + for op, count in calls.items(): + assert len(re.findall(rf"call %dx\.types\.\w+(?:\.f32)? @dx\.op\.{op}\(", dump)) == count, dump + + +def controls(work, compiler, sdk): + root = root_signature(work, sdk, "table", "DescriptorTable(SRV(t0, numDescriptors=4))") + vs = vertex() + # s44:47 = normalized clamp, point XY/mip, max LOD 15.996. + sampler = [0xbeac0392, 0xbead03ff, 0x00fff000, 0xbeae03ff, 0x04000000, 0xbeaf0380] + + def code(op, heap=False, reuse_saved_exec=False, **variant): + initial = [sop1(4, 58, 126), sop1(10, 126, 126)] if reuse_saved_exec else [sop1(3, s, 129) for s in (58, 59)] + setup = [sop1(3, 48, 8 if heap else 0), sop1(3, 49, 128), *sampler, + 0x7e120f00, # v9 = u32(SV_Position.x) + *[vmove(v, 128) for v in range(10, 16)], sop1(3, 51, 129), sop1(3, 56, 129)] + mask = 1 if op in (68, 71) else 7 + return initial + setup + waterfall(image(op, 10, mask), **variant) + + def pixel(op=39, *, reuse_saved_exec=False, **variant): + program = code(op, reuse_saved_exec=reuse_saved_exec, **variant) + if reuse_saved_exec: + # An old exact-coverage tag must not survive the overwrite of s[58:59]. + program += [0x87fe3a7e] # s_and_b64 exec, exec, s[58:59] + return patched_pixel(len(program), program, table=True, wave32=variant.get("wave32", False), + input_address=1 << 8, input_signature=((b"ISG1", signature(b"SV_Position", 1)),), + export=(0xf800180f, 0x20202020 if op not in (68, 71) else 0x23222120)) + + cases = ( + ("clause", 39, {"clause": True}, {"sampleLevel.f32": 1}), + ("vcc", 39, {"compare": "vcc"}, {"sampleLevel.f32": 1}), + ("sgpr-reverse", 39, {"compare": "sgpr", "reverse": True, "signed": True}, {"sampleLevel.f32": 1}), + ("wave32", 39, {"compare": "vcc", "wave32": True, "byte_key": True}, {"sampleLevel.f32": 1}), + ("sample-l", 36, {"separate_shift": True, "offset": 32}, {"sampleLevel.f32": 1}), + ("sample-d", 34, {"reverse": True}, {"sampleGrad.f32": 1}), + ("load-mip", 1, {"offset": 64, "byte_key": True}, {"textureLoad.f32": 1}), + ("get-resinfo", 14, {}, {"getDimensions": 1}), + ("gather-l", 68, {}, {"textureLoad.f32": 4}), + ("gather-lz", 71, {}, {"textureGather.f32": 1}), + ) + for name, op, variant, calls in cases: + manifest, ps, _ = compile_pair(work, compiler, sdk, name, vs, pixel(op, **variant), root) + offset = variant.get("offset", 0) + views = [line for line in manifest if line.startswith("srv ")] + assert views == [f"srv {j} tex2d table 0 {offset + 32 * j}" for j in range(2)], views + check_dxil(sdk, ps, calls, (0, 1)) + print(name, "bound span", offset, offset + 32) + + # Exercise the heap path as well as the linked bound-table path above. + source, ps = work / "heap", work / "heap.cso" + source.with_suffix(".pslayout").write_text("meshps1 256 256 64 guest_root\n", encoding="ascii") + source.with_suffix(".root").write_bytes(root_signature(work, sdk, "heap-root", + "RootFlags(CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED)")) + source.write_bytes(words(*code(1, heap=True, compare="vcc", separate_shift=True, offset=32), + 0xf800180f, 0x20202020, 0xbf810000)) + result = subprocess.run([compiler, sdk, work / "cache", "gfx1013-raw-mesh-ps", source, ps], + capture_output=True, text=True) + assert result.returncode == 0, result.stderr + check_dxil(sdk, ps, {"textureLoad.f32": 1}) + print("heap load-mip with a nonuniform handle") + + failures = ( + ("escape-offset", {"separate_shift": True, "escape": 51}, "graphics register undefined on an incoming path: 51"), + ("escape-save", {"compare": "vcc", "escape": 58}, "graphics register undefined on an incoming path: 58"), + ("escape-save-high", {"compare": "vcc", "escape": 59}, "graphics register undefined on an incoming path: 59"), + ("escape-compare", {"compare": "sgpr", "escape": 56}, "unbound numeric SGPR"), + ("wrong-vgpr", {"compare": "vcc", "compare_vgpr": 10}, "compare does not select"), + ("wrong-sgpr", {"compare": "vcc", "compare_key": 51}, "compare does not select"), + ("wrong-compare-mask", {"compare": "sgpr", "save_src": 106}, "does not immediately select"), + ("unaligned-immediate", {"offset": 16}, "descriptor byte offset is not 32-byte aligned"), + ("offset-short-table", {"offset": 96}, "indexed SRV span exceeds its declared root range"), + ("offset-wrap", {"offset": 32, "top_slot": True}, "waterfall descriptor span crosses a 32-bit descriptor-byte wrap"), + ) + for name, variant, refusal in failures: + compile_pair(work, compiler, sdk, name, vs, pixel(**variant), root, refusal=refusal) + for reg in (16, 48, 50, 52, 54): + compile_pair(work, compiler, sdk, f"save-overlaps-s{reg}", vs, pixel(compare="vcc", save_dst=reg), root, + refusal="s_and_saveexec destination overlaps") + compile_pair(work, compiler, sdk, "escape-old-exec-tag", vs, pixel(compare="vcc", reuse_saved_exec=True), root, + refusal="graphics wave or control-flow instruction") + + +def draw(work, compiler, sdk, writer, adapter): + subprocess.run([writer, "--write-fixture", work], check=True) + vs, ps, root = [(work / name).read_bytes() for name in ("vertex.bin", "pixel.bin", "root.bin")] + source, table_start, initial = table_source(ps) + first_sample = table_start + 14 + assert source[first_sample:first_sample + 2] == [0xf09c0708, 0x01642006] + prefix = [*initial, *source[:table_start], *source[table_start + 2:table_start + 4], + 0xf40c0618, 0xfa000040, *MIP_POINT_SAMPLER, *source[table_start + 7:first_sample], + 0x10120cf6, 0x101212f4, 0x7e120f09] # v9 = pixel column from U + # Payloads for explicit level one, explicit gradients selecting mip 1, + # and integer texel coordinates at mip 1. + level = [vmove(60, 262), vmove(61, 263), vmove(62, 242)] + grad = [vmove(60, 255), 0x3e000000, vmove(61, 128), vmove(62, 128), + vmove(63, 255), 0x3e800000, vmove(64, 262), vmove(65, 263)] + load = [0x10780cff, 0x41000000, 0x107a0ef6, 0x7e780f3c, 0x7e7a0f3d, vmove(62, 129)] + cases = ( + ("vcc-clause", 39, 6, [], "alternate", {"compare": "vcc", "clause": True}), + ("sgpr-sample-l-offset", 36, 60, level, "offset", {"compare": "sgpr", "reverse": True, + "separate_shift": True, "offset": 32}), + ("sample-d", 34, 60, grad, "implicit", {"reverse": True}), + ("load-mip", 1, 60, load, "implicit", {"byte_key": True}), + ) + for name, op, addr, prep, mode, variant in cases: + program = words(*prefix, *prep, *waterfall(image(op, addr), **variant), *source[first_sample + 2:]) + manifest, pixel, vertex_dxil = compile_pair(work, compiler, sdk, name, vs, program, root, ps_inputs=(2, 2)) + views = [] + for line in manifest: + if not line.startswith("srv "): + continue + _, register, kind, table, parameter, offset = line.split() + view = WATERFALL_TABLE[int(offset)] + assert (int(register), kind, table, parameter) == (len(views), "buf" if view == "buf" else "tex2d", + "table", "0"), line + views.append(view) + result = subprocess.run([writer, "--waterfall", adapter, sdk, work / "cache", pixel, vertex_dxil, + mode, ",".join(views)], capture_output=True, text=True) + print(name, result.stdout.strip(), result.stderr.strip()) + if result.returncode == 77: + return 77 + assert result.returncode == 0, (name, result.stderr) + return 0 + + +if __name__ == "__main__": + compiler, sdk, *gpu = sys.argv[1:] + with TemporaryDirectory(prefix="w6-", dir=Path(compiler).resolve().parent) as folder: + if gpu: + sys.exit(draw(Path(folder), compiler, Path(sdk), *gpu)) + controls(Path(folder), compiler, Path(sdk)) diff --git a/tests/native_sampled_pixel_gpu.cpp b/tests/native_sampled_pixel_gpu.cpp index 98ec11ff4..80bc15dd3 100644 --- a/tests/native_sampled_pixel_gpu.cpp +++ b/tests/native_sampled_pixel_gpu.cpp @@ -226,10 +226,10 @@ constexpr WaterfallTexture waterfall_textures[3]{ // The guest program selects slot (column & 1) << spread from each pixel's own // U coordinate, samples that slot into MRT0 and samples C at mip 0 into MRT1. // Expected colors use only the texture contents above and the pixel position. -Color waterfall_expected(unsigned x,unsigned y,bool spread,bool implicit) { +Color waterfall_expected(unsigned x,unsigned y,bool spread,bool mip_one,bool offset) { if(x==width-1)return sentinel; // scissored; this column runs helper lanes only - const auto& selected=waterfall_textures[(x&1u)<<(spread?1:0)]; - const auto& rgb=selected.mip[implicit?1:0][selected.rows ? y>=height/2 : x>=width/2]; + const auto& selected=waterfall_textures[((x&1u)<<(spread?1:0))+unsigned(offset)]; + const auto& rgb=selected.mip[mip_one?1:0][selected.rows ? y>=height/2 : x>=width/2]; const auto& fixed=waterfall_textures[2].mip[0][y>=height/2]; return {rgb[0],rgb[1],rgb[2],fixed[0]}; } @@ -237,13 +237,14 @@ Color waterfall_expected(unsigned x,unsigned y,bool spread,bool implicit) { // --waterfall ADAPTER SDK CACHE PS VS MODE LAYOUT. MODE is alternate (slots 0 // and 1), empty (slots 0 and 2 of a three-slot span; slot 1 has no lane) or // implicit (alternate with implicit LOD; mip 1 is correct only when the quad -// gradient includes the helper lanes). LAYOUT names the guest view behind each +// gradient includes the helper lanes) or offset (slots 1 and 2 at mip 1). +// LAYOUT names the guest view behind each // translated SRV register, in register order: buf, A, B or C. int run_waterfall(int argc,wchar_t** argv) { if(argc!=9)return 2; const std::wstring_view mode=argv[7]; - const bool implicit=mode==L"implicit",spread=mode==L"empty"; - if(!implicit && !spread && mode!=L"alternate")return 2; + const bool offset=mode==L"offset",mip_one=mode==L"implicit" || offset,spread=mode==L"empty"; + if(!mip_one && !spread && mode!=L"alternate")return 2; std::vector layout; // -1 is the offset buffer for(std::wstring_view rest=argv[8];;) { const auto comma=rest.find(L','); @@ -364,7 +365,7 @@ dst.Store4(16*(id.y*8+id.x),asuint(float4(a.Load(int3(id.xy,0)),b.Load(int3(id.x D3D12_RANGE range{0,bytes};check(readback->Map(0,&range,&data),"map waterfall result"); std::string failure; for(unsigned y=0;y(data)[y*width+x]; if(actual==expected)continue; char text[256];