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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ The control-flow CPU translator supports [source-built string memory transfers](
| 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_'` |
| gfx1013 image gathers | Lower [one 2D `image_gather4_l` with a constant sampler and a known integer LOD](docs/experiments.md#gfx1013-2d-explicit-lod-image-gathers) on an RGBA32 FLOAT view. Compare all four taps at three mips and on a view that starts at mip 1. | `ctest --preset dev --no-tests=error -R '^compute_store_aot_gfx1013_gather4_l_'` |
| Startup compute mip-zero gather | Dispatch a [gfx1013 `image_gather4_lz`](docs/experiments.md#compute-mip-zero-gather) and compare four texels from the first visible mip against a CPU reference. | `ctest --preset dev --no-tests=error -R '^startup_compute_gather_lz'` |
| Startup compute FP32 array image store | Bind a [2D-array UAV](docs/experiments/startup-compute-array-uav.md) through the startup runtime, verify the nonzero first slice and per-lane EXEC, and compare every texel after the fence. | `ctest --preset dev --no-tests=error -R '^startup_compute_dispatch_array_'` |
| Startup compute D16 image store | Write [four packed half-floats](docs/experiments.md#startup-compute-d16-image-store) per active thread and check all three mips. | `ctest --preset dev --no-tests=error -R '^startup_compute_image_store_d16'` |
| Startup explicit-LOD 2D gather | Dispatch and draw [gfx1013 `image_gather4_l`](docs/experiments.md#explicit-lod-2d-gather) across explicit mips, view offsets, clamped edges, and dynamic per-lane LOD against CPU references. | `ctest --preset dev --no-tests=error -R '^startup_compute_gather_l'` |
| Startup compute buffer atomics | Dispatch [gfx1013 32-bit MUBUF raw buffer atomics](docs/experiments.md#startup-compute-raw-buffer-atomics) with thread contention, `glc` old-value returns, `cmpswap` order, and `EXEC` masking. | `ctest --preset dev --no-tests=error -R '^startup_compute_buffer_atomics'` |
Expand Down
3 changes: 2 additions & 1 deletion docs/experiments.md
Original file line number Diff line number Diff line change
Expand Up @@ -15174,7 +15174,8 @@ The startup compute translator accepts the full-mask FP32 image_store on
dim:SQ_RSRC_IMG_2D_ARRAY with three full-width address VGPRs for x, y and layer.
It emits an RWTexture2DArray<float4> store under the lane EXEC bit and records
uav N tex2da float table P O channels 4 in the version 2 manifest.
The startup runtime still needs the array UAV binding in #2742. Pixel arrays,
The [startup runtime array UAV binding](experiments/startup-compute-array-uav.md)
consumes this manifest. Pixel arrays,
packed D16 arrays and the 1D, 3D and cube store forms remain refused.

The standalone GPU fixture writes the same four words into mip zero of layer 1
Expand Down
33 changes: 33 additions & 0 deletions docs/experiments/startup-compute-array-uav.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Startup compute FP32 array UAV binding

Refs [#2742](https://github.com/R0Y00/Xbox2Windows/issues/2742).
Depends on the full-mask FP32 compute array image store lowering in
[#2744](https://github.com/R0Y00/Xbox2Windows/pull/2744). Pixel and packed-half
array stores remain refused.

The production startup compute manifest consumer accepts
`uav N tex2da float table P O channels 4`. It requires an explicitly described
2D array UAV with a valid single-plane mip and slice range, retains the caller's
FirstArraySlice and ArraySize and refuses a plain 2D UAV before Dispatch.
The existing 2D and raw buffer paths are unchanged. Shader layer indices are
view-relative and are not clamped.

The fixture publishes the encoded program through slot 67, translates it with
DXC, and binds a 32x8 RGBA32_FLOAT resource through slot 19. Its six physical
slices have distinct sentinels; the UAV selects four slices starting at
FirstArraySlice=1. Relative layers 1 and 3 write physical slices 2 and 4.
The actual translated dispatch goes through slot 14. After the fence, compare
all 6,144 words, including every adjacent and out-of-view slice. Full and
partial EXEC are checked for both layers. The wrong-layer and dropped-EXEC
controls must differ by 128 and 32 words respectively. A mismatched 2D view
must be refused with all 1,024 words untouched.

Reproduce on Windows (source-built fixtures only):

```powershell
cmake --build --preset dev --target startup_stream_consumer offscreen_check
ctest --preset dev --no-tests=error -j 1 -R '^startup_compute_dispatch_array_(warp|hardware)$' -V
```

No game input, retail program coverage, title execution or guest
out-of-bounds behavior is established.
25 changes: 20 additions & 5 deletions src/startup_stubs/d3d12_compute_dispatch.inl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// the key of its published request; gfx2hlsl.py --pending answers the request with <key>.txt (`stage cs`) and
// <key>.cs.cso. Bound from the compute tables recorded by slot 35: raw buffers (`srv <i> buf table <param> <byte>`
// inputs, `uav <i> table <param> <byte>` outputs) named by hardware buffer descriptors, and 2D textures
// (`srv <i> tex2d table <param> <byte>` inputs, `uav <i> tex2d float table <param> <byte> channels 4` outputs) named
// (`srv <i> tex2d table <param> <byte>` inputs, `uav <i> tex2d/tex2da float table <param> <byte> channels 4` outputs) named
// by the token of a view the title created (device slots 18 and 19). `smp` 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
Expand All @@ -23,6 +23,7 @@ struct ComputeTranslation {
int word_param = -1, word_byte = 0;
std::uint32_t at = 0, times = 0;
Source source = root_constant;
bool array = false;
};
u64 key = 0;
int state = 0; // 0 not loaded, 1 ready, 2 unavailable
Expand Down Expand Up @@ -125,6 +126,11 @@ const char* load_compute_translation(ComputeTranslation& tr)
else if (std::sscanf(line.c_str(), "uav %u tex2d float table %d %d channels 4%n", &i, &a, &b, &used) == 3 && whole() &&
i == tr.uavs.size())
tr.uavs.push_back({a, b, true});
else if (std::sscanf(line.c_str(), "uav %u tex2da float table %d %d channels 4%n", &i, &a, &b, &used) == 3 && whole() &&
i == tr.uavs.size()) {
tr.uavs.push_back({a, b, true});
tr.uavs.back().array = true;
}
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});
// A static sampler, plain (smp) or with the descriptor's LOD clamp (smpl), decoded as the draw path does.
Expand All @@ -136,7 +142,7 @@ const char* load_compute_translation(ComputeTranslation& tr)
i == tr.samplers.statics.size())
tr.samplers.read_manifest_line(line.c_str());
else return line.rfind("srv", 0) == 0 ? "a resource that is not a raw table buffer or a 2D texture"
: line.rfind("uav", 0) == 0 ? "an output that is not a raw table buffer or a 2D float texture"
: line.rfind("uav", 0) == 0 ? "an output that is not a raw table buffer or a 2D/2D-array float texture"
: line.rfind("udsrc", 0) == 0 ? "an entry value that is not a root constant or a buffer descriptor word"
: line.rfind("smp", 0) == 0 ? "a sampler that is not a plain static one" : "an unknown manifest line";
}
Expand Down Expand Up @@ -299,11 +305,20 @@ const char* record_translated_dispatch(Wrapper* self, const TranslatedDispatch&
if (!known || record.slot != 19) reason = "an output texture entry is not the token of an unordered access view";
else if (!created || !texture || !(resource.Flags & D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS))
reason = "an output texture view was not created on the desktop";
else if (described ? desc.ViewDimension != D3D12_UAV_DIMENSION_TEXTURE2D || desc.Texture2D.PlaneSlice
else if (slot.array) {
// The layer in HLSL is relative to FirstArraySlice: retain the caller's complete view.
const auto& a = desc.Texture2DArray;
if (!described || desc.ViewDimension != D3D12_UAV_DIMENSION_TEXTURE2DARRAY)
reason = "an output texture view is not a 2D array";
else if (resource.Dimension != D3D12_RESOURCE_DIMENSION_TEXTURE2D || resource.SampleDesc.Count != 1 ||
a.PlaneSlice || a.MipSlice >= resource.MipLevels || a.FirstArraySlice >= resource.DepthOrArraySize ||
!a.ArraySize || a.ArraySize > resource.DepthOrArraySize - a.FirstArraySlice)
reason = "an output texture array view is outside the resource";
} else if (described ? desc.ViewDimension != D3D12_UAV_DIMENSION_TEXTURE2D || desc.Texture2D.PlaneSlice
: resource.Dimension != D3D12_RESOURCE_DIMENSION_TEXTURE2D || resource.DepthOrArraySize != 1)
reason = "an output texture view is not 2D";
else if (x2w_startup::packed_target_format(format)) reason = "an output texture view has a packed format";
else if (!float_output_format(format)) reason = "an output texture format is not a float or normalized one";
if (!reason && x2w_startup::packed_target_format(format)) reason = "an output texture view has a packed format";
if (!reason && !float_output_format(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.
for (std::size_t input = 0; input < inputs && !reason; ++input)
Expand Down
8 changes: 8 additions & 0 deletions tests/cmake/startup_compute_dispatch_array.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
if(EXISTS "${INTAKE_PYTHON}" AND TARGET startup_stream_consumer)
foreach(adapter IN ITEMS warp hardware)
add_test(NAME startup_compute_dispatch_array_${adapter} COMMAND "${INTAKE_PYTHON}" -B
"${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_compute_dispatch_array_check.py" "$<TARGET_FILE:startup_stream_consumer>"
"${DXC_EXECUTABLE}" ${adapter})
set_tests_properties(startup_compute_dispatch_array_${adapter} PROPERTIES TIMEOUT 120 SKIP_RETURN_CODE 77)
endforeach()
endif()
105 changes: 105 additions & 0 deletions tests/startup_compute_dispatch_array_check.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
"""Startup FP32 array UAV binding through the production slot-67 / slot-14 path (#2742)."""
import os
from pathlib import Path
import subprocess
import sys
import tempfile

import startup_compute_image_store_check as A
import startup_compute_dispatch_texture_check as T
from startup_compute_request_check import DATA, prepare
from startup_compute_translate_check import state_a

WIDTH, HEIGHT, LAYERS, FIRST = 32, 8, 6, 1
WORDS = WIDTH * HEIGHT * 4


def reference(layer=None, rows=2, layers=LAYERS):
result = [0xbf000000 | (z << 12) | i for z in range(layers) for i in range(WORDS)]
if layer is not None:
for y in range(rows):
for x in range(4 * A.fixture.GROUPS):
at = (FIRST + layer) * WORDS + (y * WIDTH + x) * 4
result[at:at + 4] = A.ARRAY_VALUES
return result


def check(directory, consumer, dxc, adapter, layer, masked):
cache, env = prepare(directory)
env.update(X2W_D3D12_LIST_NOOP="1", X2W_D3D12_DEBUG="1")
if adapter == "hardware":
env["X2W_TEST_DISPATCH_HARDWARE"] = "1"
(directory / "compute_root").write_bytes(T.C.root_blob(3, table_range=True))
env["X2W_TEST_COMPUTE_ROOT"] = str(directory / "compute_root")
program = A.array_program(layer)
if masked:
assert program.count(A.ARRAY_STORE) == 1
program = program.replace(A.ARRAY_STORE, A.MASK + A.ARRAY_STORE)
(directory / "program").write_bytes(program)
(directory / "data").write_bytes(DATA)
(directory / "state").write_bytes(state_a(A.fixture.GROUP, A.RSRC2))
layout = "<%dI" % (WORDS * LAYERS)
mode = "texture-array"
got, result = T.run(directory, consumer, env, mode, layout)
print(result.stdout, end="")
assert got == reference(), "untranslated dispatch wrote the texture"
assert len(T.lines(result, "Dispatch(2, 1, 1) refused: no translation")) == 1, result.stderr[-3000:]
key = int(T.lines(result, "x2w-draw compute request")[0].split()[3], 16)
T.translate(cache, dxc, key)
manifest = (cache / f"{key:016x}.txt").read_text()
assert "uav 0 tex2da float table 1 64 channels 4" in manifest.splitlines(), manifest
hlsl = cache / f"{key:016x}.cs.hlsl"
cso = cache / f"{key:016x}.cs.cso"
source = hlsl.read_text()
assert "RWTexture2DArray<float4> w0 : register(u0);" in source
expected = reference(layer, 1 if masked else 2)
got, result = T.run(directory, consumer, env, mode, layout)
assert len(T.lines(result, "Dispatch(2, 1, 1) translated")) == 1, result.stderr[-3000:]
debug = T.clean(result)
wrong = sum(a != b for a, b in zip(got, expected))
assert wrong == 0, (layer, masked, wrong)
print(f"{adapter}: relative layer {layer}, physical layer {FIRST + layer}, partial EXEC={masked}: "
f"{len(got)} words, mismatches={wrong}; debug layer: {debug}", flush=True)

# The actual runtime must refuse a plain 2D view of the same format before dispatch.
got, result = T.run(directory, consumer, env, "texture-array-2d", "<%dI" % WORDS)
assert got == reference(layers=1)
refusal = T.lines(result, "Dispatch(2, 1, 1) refused:")
assert len(refusal) == 1 and refusal[0].endswith("an output texture view is not a 2D array"), result.stderr[-3000:]
assert not T.lines(result, "Dispatch(2, 1, 1) translated")
print(f"{adapter}: incompatible 2D UAV refused; {len(got)} words unchanged", flush=True)

# Negative controls run through the same production binding, not the offscreen shortcut.
guard = "if (exec & 1) " + A.ARRAY_LINE
if masked:
assert source.count(guard) == 1
mutant = source.replace(guard, A.ARRAY_LINE)
label, count = "dropped EXEC", 32
else:
index = "uint3(v8, v9, v10)"
assert source.count(index) == 1
delta = "+ 1u" if layer == 1 else "- 1u"
mutant = source.replace(index, f"uint3(v8, v9, v10 {delta})")
label, count = "wrong layer", 128
hlsl.write_text(mutant, encoding="ascii")
subprocess.run([dxc, "-Gis", "-T", "cs_6_0", "-E", "main", "-Fo", cso, hlsl], check=True)
got, result = T.run(directory, consumer, env, mode, layout)
assert len(T.lines(result, "Dispatch(2, 1, 1) translated")) == 1, result.stderr[-3000:]
T.clean(result)
wrong = sum(a != b for a, b in zip(got, expected))
assert wrong == count, (layer, label, wrong, count)
print(f"{adapter}: {label} control: {len(got)} words, mismatches={wrong} (expected {count})", flush=True)


def main(consumer, dxc, adapter):
with tempfile.TemporaryDirectory() as temp:
for layer in (1, 3):
for masked in (False, True):
check(Path(temp) / f"layer-{layer}-{masked}", consumer, dxc, adapter, layer, masked)
print(f"startup_compute_dispatch_array: PASS on {adapter}")


if __name__ == "__main__":
if len(sys.argv) != 4 or sys.argv[3] not in ("warp", "hardware"):
sys.exit(__doc__)
main(*sys.argv[1:])
Loading
Loading