Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions docs/experiments.md
Original file line number Diff line number Diff line change
Expand Up @@ -17846,3 +17846,14 @@ ctest --preset dev --no-tests=error -j1 -R '^(native_pixel_raw_uav_(warp|hardwar
```

Repeat with the release preset. The source-built shader pair goes through request publication, native DXIL, actual PSO/draw and GPU readback. The fixture checks full buffer and color-target contents, nonzero/short/end views, decoy and invalid descriptors; the validator also checks alignment and bounds. Local RX 7900 XT/WARP Debug and Release runs passed 54 cases (3,456 DWORDs and 1,728 pixels); the independent review over main 886a5a28 reproduced the same counts on RTX 4060 Laptop and WARP with the D3D12 debug layer enabled and zero errors. Actual title rendering impact is not established.

## Native pixel raw DWORD atomic add

The native pixel compiler supports raw DWORD buffer atomic add in a proven straight-line guest block, with exact coverage/EXEC, including restoration from whole-quad entry. GLC returns the old value, preserves inactive VGPRs, and returns zero for an active out-of-view access; without GLC the addend is unchanged. The runtime uses the single raw pixel UAV direct non-indexed draw contract above. Indexed atomics, other atomic operations, general CFG and unproven WQM remain refused (#3216, #3217).

```powershell
cmake --build --preset dev --target startup_stream_consumer compile_dxil
ctest --preset dev --no-tests=error -j1 -R '^native_pixel_atomic_(warp|hardware)$' -V
```

Repeat with the release preset. The source-built fixture verifies 13 actual draws and four refusal controls per adapter/configuration, including contention, guard words, active out-of-view, odd raster edges and real WQM quad-neighbor reads with and without GLC. WQM+GLC records returned old values through a second exact atomic in a private lane slot and checks a valid serialization of the varying addends. Local RX 7900 XT/WARP Debug/Release integration over main 4b0350e passed 52 draws, 3328 DWORD and 1664 pixel comparisons, and 16 refusal controls. The debug layer was unavailable; actual title rendering is not established.
26 changes: 25 additions & 1 deletion src/gfx1013_direct_compute.inl
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ struct DirectCompute {
return ir.invoke("%dx.types.ResRet.i32", raw ? "rawBufferLoad.i32" : "bufferLoad.i32", raw ? 139 : 68, args,
"nounwind readonly");
}
void buffer_atomic(const Inst &i, const State &s) {
void buffer_atomic(const Inst &i, const State &s, const string &pixel_predicate = {}) {
// The operation set and VGPR ordering follow gfx2hlsl.MUBUF_ATOMIC_OPS.
static const std::map<Name, unsigned> operations{
{"buffer_atomic_add", 0}, {"buffer_atomic_sub", 0}, {"buffer_atomic_and", 1}, {"buffer_atomic_or", 2},
Expand Down Expand Up @@ -424,6 +424,30 @@ struct DirectCompute {
args.emplace_back("i32", read(data + 1));
args.emplace_back("i32", value);
auto tag = to_string(label_number++), pred = active();
if (!pixel_predicate.empty()) {
// Graphics uses SSA rather than the compute VGPR allocas. The caller
// proves a single guest block and supplies exact coverage and EXEC.
// Keep the original VGPR for inactive lanes, including helpers.
const auto size = ir.invoke("%dx.types.Dimensions", "getDimensions", 72,
{{"%dx.types.Handle", h}, {"i32", "undef"}}, "nounwind readonly");
const auto bytes = ir.value("extractvalue %dx.types.Dimensions " + size + ", 0");
const auto in_view = cmp("ult", address, bytes); // aligned DWORD, raw view length is a multiple of 4
const auto fallback = (i.a & 0x4000) ? select(pixel_predicate, "0", value) : "";
const auto execute = ir.value("and i1 " + pixel_predicate + ", " + in_view);
ir.body << " br i1 " << execute << ", label %atomic" << tag << ", label %atomicSkip" << tag
<< "\natomic" << tag << ":\n";
const auto previous = ir.invoke("i32", "atomicBinOp.i32", 78, args, "nounwind");
ir.body << " br label %atomicEnd" << tag << "\natomicSkip" << tag
<< ":\n br label %atomicEnd" << tag << "\natomicEnd" << tag << ":\n";
if (i.a & 0x4000) {
auto returned = ir.value("phi i32 [ " + previous + ", %atomic" + tag + " ], [ " +
fallback + ", %atomicSkip" + tag + " ]");
// Contention makes the old value varying even with uniform operands.
ir.dependencies[returned].set(128);
write(data, returned);
}
return;
}
if (indexed) {
if (i.a & 0x4000) {
auto out_of_range = ir.value("and i1 " + pred + ", " + ir.value("xor i1 " + in_range + ", true"));
Expand Down
19 changes: 16 additions & 3 deletions src/gfx1013_direct_graphics.inl
Original file line number Diff line number Diff line change
Expand Up @@ -805,6 +805,7 @@ struct GraphicsMaskRegions {
if (two_arms && (j == middle || j == middle + 1)) continue;
if (body.pc != program.code[j - 1].next || body.next != program.code[j + 1].pc ||
!(local_alu(body) || body.name == "s_waitcnt" || body.name == "s_nop" ||
(!vertex && body.name == "buffer_atomic_add" && !(body.a & 0x2000)) ||
(body.enc == Encoding::Image && body.op == 0x27)))
refuse(body.pc, "masked graphics region requires lane-local vector arithmetic");
}
Expand Down Expand Up @@ -1771,7 +1772,8 @@ NativeComputeShader lower_graphics(GuestShaderStage &stage, unsigned first_srv =
i.enc != Encoding::Sop1 && i.enc != Encoding::Sop2 && i.enc != Encoding::Sopk &&
i.enc != Encoding::Export &&
!(i.name == "v_mov_b32" && i.dst >= 256 && !i.dpp_control && !i.subword) &&
!(i.enc==Encoding::Image && i.op>=0x20 && i.op<=0x3f))
!(i.enc==Encoding::Image && i.op>=0x20 && i.op<=0x3f) &&
!(i.name == "buffer_atomic_add" && !(i.a & 0x2000)))
refuse(pc,"unsupported exact-mask operation after whole-quad entry");
if ((i.name == "s_wqm_b32" || i.name == "s_wqm_b64") && i.dst == 126 && i.src[0] == 126) {
if (!stage.wave32 && i.name == "s_wqm_b32")
Expand Down Expand Up @@ -1942,8 +1944,19 @@ NativeComputeShader lower_graphics(GuestShaderStage &stage, unsigned first_srv =
i.name.find("mbcnt") != Name::npos || i.name == "v_cndmask_b32")
refuse(i.pc, "graphics wave or control-flow instruction");
if (i.enc == Encoding::Smem || i.enc == Encoding::Mubuf) {
if (i.name.find("atomic") != Name::npos)
refuse(i.pc, "graphics buffer atomic");
if (i.name.find("atomic") != Name::npos) {
// ponytail: branch splitting stays inside one guest block; general
// graphics CFG edges would need to track the new predecessor labels.
if (!pixel || i.name != "buffer_atomic_add" || (i.a & 0x2000) || !straight)
refuse(i.pc, "graphics buffer atomic requires a straight-line pixel raw DWORD add");
if (whole_quad && !exact_after_wqm)
refuse(i.pc, "pixel atomic requires exact coverage after WQM");
auto pred = covered_lane();
if (!alu.graphics_predicate.empty())
pred = alu.ir.value("and i1 " + pred + ", " + alu.graphics_predicate);
alu.buffer_atomic(i, state, pred);
return;
}
alu.memory(i, state);
if (i.name == "buffer_load_format" && (i.a & 0x3ffffu) == 0 && alu.graphics_predicate.empty()) {
// A read-only uniform table entry and the literal element zero
Expand Down
10 changes: 10 additions & 0 deletions tests/cmake/native_pixel_atomic.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Native raw DWORD atomic add through the graphics runtime's published cache.
if(TARGET compile_dxil AND TARGET startup_stream_consumer
AND EXISTS "${INTAKE_PYTHON}" AND EXISTS "${DXC_BIN_DIR}/dxcompiler.dll" AND EXISTS "${DXC_BIN_DIR}/dxil.dll")
foreach(backend IN ITEMS warp hardware)
add_test(NAME native_pixel_atomic_${backend} COMMAND "${INTAKE_PYTHON}" -B
"${CMAKE_SOURCE_DIR}/tests/native_pixel_atomic_check.py"
"$<TARGET_FILE:compile_dxil>" "$<TARGET_FILE:startup_stream_consumer>" "${DXC_BIN_DIR}" ${backend})
set_tests_properties(native_pixel_atomic_${backend} PROPERTIES SKIP_RETURN_CODE 77 TIMEOUT 120)
endforeach()
endif()
194 changes: 194 additions & 0 deletions tests/native_pixel_atomic_check.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
"""Native pixel atomics through offline publication and the real draw path."""
import os
from pathlib import Path
import re
import subprocess
import sys
import tempfile
from types import SimpleNamespace

from native_direct_container_check import patched_pixel, signature
from native_startup_request_cache_check import stream_vertex_container
from startup_pixel_uav_check import run_case, DRAWN_LINE, CLEAR

MASK = 0xffffffff
RED = bytes((255, 0, 0, 255))


def shader(stride, glc=True, threshold=None, offset=0, delta=1, wqm=False, bad=None, dynamic=False):
vop1 = lambda op, dst, src: 0x7e000000 | dst << 17 | op << 9 | src
vop2 = lambda op, dst, src0, src1: op << 25 | dst << 17 | src1 << 9 | src0
code = [0xbe810380, 0xf40c0200, 0xfa000000 | stride]
if wqm or bad == "wqm":
code += [0xbe98047e, 0xbefe0a7e] # Save exact EXEC, enter WQM.
# v0 = x, v1 = y. Per-pixel output address = 4*(4*y+x+1).
code += [vop1(7, 0, 256), vop1(7, 1, 257), vop1(1, 4, 255), delta,
vop1(1, 2, 242), vop1(1, 3, 128)]
if dynamic:
# Two contended counters at the last two DWORDs of the view.
code += [vop2(0x1b, 5, 129, 0), vop2(0x1a, 5, 130, 5)]
if bad == "branch":
code += [0xbf820000] # Unflattened guest CFG edge.
if threshold is not None:
code += [0x7d080080 | threshold, 0xbe84246a, 0xbf880002]
if wqm:
# Horizontal quad swap of integer SV_Position.x. At x=2 on the
# odd edge this consumes the helper at x=3, not a constant.
code += [0x7e0802fa, 0xff0cb100] # v4 = quadSwapX(v0).
if glc:
# Preserve v0/v1 for the quad read; form the private output offset
# in WQM before restoring exact EXEC. v6 = 4*(4*y+x).
code += [vop2(0x1a, 1, 130, 1), vop2(0x1c, 6, 257, 0),
vop2(0x1a, 6, 130, 6)]
code += [0xbefe0418] # Restore exact EXEC.
op = 0xe0c80000 | (0x4000 if glc else 0) | offset
if bad == "other":
op = 0xe0cc0000
if bad == "indexed":
op |= 0x2000
if dynamic:
op |= 0x1000
code += [op, 0x80020405 if dynamic else 0x80020400] # v4 is both addend and GLC destination.
if threshold is not None:
code += [0xbefe0404] # Restore the proven original EXEC.
if wqm and glc:
# A second already-supported exact atomic add records the returned old
# value into a disjoint lane slot; no extra exact-mask ALU/store allowed.
code += [0xe0c81004, 0x80020406]
elif not wqm and bad != "wqm":
# Store returned VGPR (or unchanged addend) after reconvergence.
code += [vop2(0x1a, 1, 130, 1), vop2(0x1c, 0, 257, 0),
vop2(0x1a, 0, 130, 0), 0xe0701004, 0x80020400]
return patched_pixel(len(code), code, table=True, input_address=3 << 8,
input_signature=((b"ISG1", signature(b"SV_Position", 1)),),
export=(0xf800180f, 0x02030302))


def main(cli, consumer, sdk, backend):
with tempfile.TemporaryDirectory() as tmp:
folder = Path(tmp)
vs = folder / "vs"
vs.write_bytes(stream_vertex_container())
env = dict(os.environ, X2W_D3D12_PERMISSIVE="1", X2W_D3D12_LIST_NOOP="1",
X2W_D3D12_RENDER="1", X2W_D3D12_DEVICE_NOOP="1",
X2W_TEST_DEBUG_LAYER="1", X2W_D3D12_DEBUG="0")
# The guest descriptor increment differs between AMD and app-local WARP.
probe = folder / "probe"
probe.mkdir()
ps = probe / "ps"
root = probe / "root"
ps.write_bytes(shader(32))
publish = dict(env, X2W_SHADER_CACHE=str(probe), X2W_TEST_PIXEL_UAV="raw-atomic")
publish.pop("X2W_TEST_DEBUG_LAYER")
if backend == "hardware":
publish["X2W_TEST_DISPATCH_HARDWARE"] = "1"
r = subprocess.run([consumer, vs, ps, root], env=publish, capture_output=True, text=True)
if r.returncode == 77:
return 77
assert r.returncode == 0, r.stderr
stride = int(re.search(r"raw descriptor stride: (\d+)", r.stdout)[1])
print(f"descriptor stride={stride}", flush=True)

def case(name, *, glc=True, threshold=None, offset=0, delta=1, edge=False, wqm=False, dynamic=False):
work = folder / name
work.mkdir()
cache = work / "cache"
cache.mkdir()
ps, root = work / "ps", work / "root"
ps.write_bytes(shader(stride, glc, threshold, offset, delta, wqm, dynamic=dynamic))
mode = "raw-atomic-edge" if edge else "raw-atomic"
p = dict(publish, X2W_SHADER_CACHE=str(cache), X2W_TEST_PIXEL_UAV=mode)
r = subprocess.run([consumer, vs, ps, root], env=p, capture_output=True, text=True)
assert r.returncode == 0, r.stderr
r = subprocess.run([cli, sdk, folder/"frontend", "gfx1013-container-pair", "--requests", cache, cache],
capture_output=True, text=True)
assert r.returncode == 0, (name, r.stdout, r.stderr)
stage, = cache.glob("*.ps.cso")
manifest = stage.with_name(stage.name.replace(".ps.cso", ".txt"))
assert f"uav 0 raw table 0 {stride}" in manifest.read_text()
ir = subprocess.check_output([Path(sdk)/"dxc.exe", "-dumpbin", stage], text=True)
assert "@dx.op.atomicBinOp.i32" in ir and "@dx.op.isHelperLane.i1" in ir, ir
if wqm:
assert "@dx.op.quadOp.i32" in ir, ir
program = SimpleNamespace(directory=work, cache=cache, consumer=consumer, vs=vs, ps=ps, root=root)
rc, out, log, pixels, words = run_case(program, mode, env, backend)
if rc == 77:
sys.exit(77)
assert rc == 0 and "device removed reason: 0x00000000" in out, (name, rc, out, log)
assert DRAWN_LINE in log, (name, log)
if "debug layer: on" in out:
assert "debug layer: on, errors: 0" in out, out
coverage = [(x,y) for y in range(3 if edge else 4) for x in range(3 if edge else 4)]
active = [(x,y) for x,y in coverage if threshold is None or x < threshold]
want_pixels = b"".join(RED if (x,y) in coverage else CLEAR for y in range(4) for x in range(8))
assert pixels == want_pixels, (name, pixels.hex(), want_pixels.hex())
expected = [0xabcd0000 + i for i in range(64)]
base = 36
for group in range(2 if dynamic else 1):
lanes = [(x,y) for x,y in active if not dynamic or x%2 == group]
target = base + offset//4 + group
in_view = offset//4 + group < 20
seed = expected[target] if in_view else 0
if in_view:
increment = sum(x ^ 1 for x,y in lanes) if wqm else delta*len(lanes)
expected[target] = (seed + increment) & MASK
if not wqm or glc:
returned = []
for x,y in coverage:
if dynamic and x%2 != group:
continue
pos = base + 1 + 4*y+x
if glc and (x,y) in lanes:
returned.append((words[pos]-expected[pos]) & MASK if wqm else words[pos])
expected[pos] = words[pos] # Compare the full old-value multiset independently.
else:
expected[pos] = (x ^ 1) if wqm else delta
want_old = sorted(((seed + delta*j) & MASK) if in_view else 0 for j in range(len(lanes)))
if wqm and glc and in_view:
# Validate a serialization, including zero increments, without imposing GPU lane order.
from functools import lru_cache
from collections import Counter
edges = Counter((old, x ^ 1) for old,(x,y) in zip(returned, lanes))
pairs = tuple(edges)
@lru_cache(None)
def chain(value, counts):
if not any(counts):
return value == expected[target]
return any(n and old == value and chain((value+add)&MASK,
counts[:i]+(n-1,)+counts[i+1:])
for i, ((old,add),n) in enumerate(zip(pairs,counts)))
assert chain(seed, tuple(edges.values())), (name, edges, seed, expected[target])
else:
assert sorted(returned) == (want_old if glc else []), (name, returned, want_old)
assert words == expected, (name, [(i,hex(a),hex(b)) for i,(a,b) in enumerate(zip(words,expected)) if a!=b], len(words))
print(name, f"{len(coverage)} covered, {len(active)} active; 64 DWORDs and 32 pixels exact",
next((l for l in out.splitlines() if l.startswith("adapter=")), ""), flush=True)

case("contention-return")
case("contention-no-return", glc=False)
case("half-exec", threshold=2)
case("empty-exec", threshold=0)
case("all-exec", threshold=4)
case("wrap", delta=0x60000001)
case("last-dword", offset=76)
case("out-of-view", offset=80)
case("dynamic-two-counters", offset=72, dynamic=True)
case("odd-edge", edge=True)
case("wqm-restore-edge-helper-read", glc=False, edge=True, wqm=True)
case("wqm-glc-edge-helper-read", edge=True, wqm=True)
case("wqm-glc-out-of-view", offset=80, edge=True, wqm=True)
for bad, reason in (("other", "graphics buffer atomic"), ("indexed", "indexed compute atomic"),
("wqm", "pixel atomic requires exact coverage"), ("branch", "straight-line pixel raw DWORD add")):
source = folder / (bad+".ps")
dest = folder / (bad+".cso")
source.write_bytes(shader(stride, bad=bad))
r = subprocess.run([cli,sdk,folder/"frontend","gfx1013-container-ps",source,dest],
capture_output=True,text=True)
assert r.returncode != 0 and not dest.exists() and reason in r.stderr, (bad,r.stderr)
print(bad, r.stderr.strip(), flush=True)
print(backend, "13 actual draws and 4 refusal controls passed")
return 0


if __name__ == "__main__":
sys.exit(main(*sys.argv[1:]))
7 changes: 6 additions & 1 deletion tests/startup_pixel_uav.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,8 @@ int draw_raw_uavs(ID3D12Device* desktop, void* guest, void* list, const std::str
if (!cpu || !gpu || stride < 32) return 73;
D3D12_UNORDERED_ACCESS_VIEW_DESC view{DXGI_FORMAT_R32_TYPELESS, D3D12_UAV_DIMENSION_BUFFER};
view.Buffer.FirstElement = 4;
view.Buffer.NumElements = 16;
const bool atomic = mode.rfind("raw-atomic", 0) == 0;
view.Buffer.NumElements = atomic ? 20 : 16;
view.Buffer.Flags = D3D12_BUFFER_UAV_FLAG_RAW;
invoke(guest, 19, ptr(fixture.resources[0]), 0, ptr(&view), cpu);
if (mode == "raw-short") view.Buffer.NumElements = 8;
Expand All @@ -316,6 +317,10 @@ int draw_raw_uavs(ID3D12Device* desktop, void* guest, void* list, const std::str
std::printf("raw pixel UAV case %s, title descriptor stride %u\n", mode.c_str(), stride);
invoke(list, 32, 1, ptr(&views));
if (mode != "raw-no-table") invoke(list, 36, 0, gpu);
if (mode == "raw-atomic-edge") {
const D3D12_RECT scissor{0, 0, 3, 3};
invoke(list, 22, 1, ptr(&scissor));
}
invoke(list, 12, 3, 1);
return 0;
}
Expand Down
Loading