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
42 changes: 34 additions & 8 deletions tests/startup_compute_dispatch_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
stand-in list, the descriptor heap, the pipeline, its root signature, two root constants (word 0 by slot 37, word 1
by slot 39, with a slot 40 call of other words in between), one table of two
hardware buffer descriptors and Dispatch(3, 2, 2). Input and output are placed buffers that alias the consumer's
memory; the output words are read from that memory after the queue's fence. Four runs share one cache:
memory; the output words are read from that memory after the queue's fence. The runs share one cache:
1. before translation: the request is published, the Dispatch is refused with "no translation" and every output
word keeps the sentinel (the check fails when nothing translated is dispatched);
Expand All @@ -19,7 +19,10 @@
call), dispatch-root-changed (another compute root and back after the constants), dispatch-reset (list closed
and reset after the constants), dispatch-wrong-root (recorded under a root the pipeline was not created with)
and dispatch-same-buffer (the output buffer is also the input).
Exit 77 when the adapter is not available.
7. slot 35 selects the correct table at descriptor index 2 instead of the swapped pair at the heap start;
setting the same heap again preserves the table, but changing the compute root or view heap after binding
must refuse at the table lookup, not an earlier check.
Exit 77 only if the initial probe cannot use the adapter or required heaps.
"""
import os
from pathlib import Path
Expand All @@ -34,16 +37,19 @@
from startup_stream_check import gfx2hlsl as G


def dispatch(directory, consumer, env, mode):
def dispatch(directory, consumer, env, mode, *, probe=False):
out = directory / 'out.bin'
out.unlink(missing_ok=True)
args = [consumer, directory / 'vertex', directory / 'pixel', directory / 'root', 'slot67',
directory / 'program', directory / 'data', directory / 'state', mode]
result = subprocess.run(args, env=dict(env, X2W_TEST_DISPATCH_OUT=str(out)), capture_output=True, text=True)
if result.returncode == 77:
if 'device removed' in result.stderr.lower():
raise RuntimeError(f'{mode}: device removed\n{result.stderr[-3000:]}')
if result.returncode == 77 and probe:
print(result.stdout, end='')
sys.exit(77)
assert result.returncode == 0, f'{mode}: consumer exit {result.returncode}\n{result.stderr[-3000:]}'
if result.returncode:
raise RuntimeError(f'{mode}: consumer exit {result.returncode}\n{result.stdout[-1000:]}\n{result.stderr[-3000:]}')
return list(struct.unpack('<%dI' % C.WORDS, out.read_bytes())), result


Expand Down Expand Up @@ -75,7 +81,7 @@ def main(consumer, dxc, adapter):
untouched, expected = [C.SENTINEL] * C.WORDS, C.expected('A')
assert expected != untouched

got, result = dispatch(directory, consumer, env, 'dispatch')
got, result = dispatch(directory, consumer, env, 'dispatch', probe=True)
print(result.stdout, end='')
assert got == untouched, 'a Dispatch without a translation wrote the output'
assert len(lines(result, 'Dispatch(3, 2, 2) refused: no translation')) == 1, result.stderr[-3000:]
Expand Down Expand Up @@ -129,8 +135,28 @@ def main(consumer, dxc, adapter):
assert got == untouched, f'{mode}: a refused Dispatch wrote the output'
refused = lines(result, 'refused: ')
assert len(refused) == 1 and refused[0].endswith('refused: ' + reason), f'{mode}: {refused}\n{result.stderr[-3000:]}'
print(f'startup_compute_dispatch: PASS on {adapter} (all {C.WORDS} words match after translation and with the '
'same root set again; untouched before translation, without a Dispatch and for 6 refusals)')
debug_enabled = bool(layer and '0x00000000' in layer[0])
for mode in ('dispatch-table-root-changed', 'dispatch-table-heap-changed', 'dispatch-table-offset'):
got, result = dispatch(directory, consumer, env, mode)
if debug_enabled:
initialized = lines(result, 'x2w-d3d12 debug layer: ')
assert len(initialized) == 1 and '0x00000000' in initialized[0], f'{mode}: debug layer not initialized'
refused = lines(result, 'refused: ')
translated = lines(result, 'Dispatch(3, 2, 2) translated')
if mode == 'dispatch-table-offset':
wrong = sum(a != b for a, b in zip(got, expected))
assert not wrong, f'{mode}: {wrong} words differ from the CPU reference\n{result.stderr[-3000:]}'
assert len(translated) == 1 and not refused, result.stderr[-3000:]
print(f'{mode}: all {C.WORDS} words match the CPU reference')
else:
written = sum(word != C.SENTINEL for word in got)
assert not written, f'{mode}: {written} output words changed after table invalidation\n{result.stderr[-3000:]}'
assert not translated and len(refused) == 1 and refused[0].endswith(
'Dispatch(3, 2, 2) refused: a table descriptor is not bound'), result.stderr[-3000:]
print(f'{mode}: exact table refusal, all {C.WORDS} sentinel words unchanged')
errors = [line for line in lines(result, 'x2w-d3d12-debug ') if ' sev=0 ' in line or ' sev=1 ' in line]
assert not errors, errors[0]
print(f'startup_compute_dispatch: PASS on {adapter}')


if __name__ == '__main__':
Expand Down
50 changes: 46 additions & 4 deletions tests/startup_stream_consumer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ static void* make_committed(void* device, D3D12_HEAP_TYPE type, const D3D12_RESO
// For any mode, X2W_TEST_DISPATCH_IN names 1280 bytes that replace the input words, and X2W_TEST_DISPATCH_FIELDS
// names six words that replace, for the input and then the output descriptor, the stride (word 1 bits 16-31), the
// record count (word 2) and word 3, which are otherwise 4, 320 and 0.
// Table controls bind away from the heap start or change the root/view heap after slot 35 without rebinding.
static int dispatch_slot67(IDXGIAdapter* adapter, void* device, const std::string& mode)
{
constexpr UINT words = 320;
Expand Down Expand Up @@ -285,7 +286,25 @@ static int dispatch_slot67(IDXGIAdapter* adapter, void* device, const std::strin
const UINT alias[4] = {static_cast<UINT>(address), static_cast<UINT>(address >> 32) | (4u << 16), 5, 0};
std::memcpy(descriptors[1], alias, sizeof(alias));
}
std::memcpy(reinterpret_cast<void*>(cpu), descriptors, sizeof(descriptors));
const bool change_table_root = mode == "dispatch-table-root-changed";
const bool change_table_heap = mode == "dispatch-table-heap-changed";
StreamWord table_gpu = gpu;
if (mode == "dispatch-table-offset") {
const UINT increment = static_cast<UINT>(invoke(device, 15, D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV));
if (increment < sizeof(descriptors[0])) return 73;
const StreamWord offset = StreamWord{2} * increment;
// The heap starts with the wrong pair, but both records are well-formed. The correct pair starts at index 2.
std::memcpy(reinterpret_cast<void*>(cpu), descriptors[1], sizeof(descriptors[1]));
std::memcpy(reinterpret_cast<void*>(cpu + sizeof(descriptors[1])), descriptors[0], sizeof(descriptors[0]));
std::memcpy(reinterpret_cast<void*>(cpu + offset), descriptors, sizeof(descriptors));
table_gpu += offset;
} else {
std::memcpy(reinterpret_cast<void*>(cpu), descriptors, sizeof(descriptors));
}
void* other_heap = nullptr;
if (change_table_heap &&
(FAILED(static_cast<HRESULT>(invoke(device, 14, word(&heap_desc), word(&__uuidof(ID3D12DescriptorHeap)),
word(&other_heap)))) || !other_heap || other_heap == heap)) return 72;
const bool heap_record = mode.rfind("dispatch-heap", 0) == 0;
if (heap_record)
for (const UINT at : {0u, 32u, 64u})
Expand Down Expand Up @@ -367,7 +386,18 @@ static int dispatch_slot67(IDXGIAdapter* adapter, void* device, const std::strin
mode == "dispatch-heap-word-past" ? at : mode == "dispatch-heap-word-placed" ? words * 4 : bytes, 0};
std::memcpy(reinterpret_cast<void*>(cpu + 96), view, sizeof(view));
}
invoke(list, 35, 1, gpu);
invoke(list, 35, 1, table_gpu);
if (change_table_root) {
invoke(list, 33, word(slot67_other_root));
invoke(list, 33, word(slot67_root));
}
if (change_table_heap) invoke(list, 32, 1, word(&other_heap));
if (change_table_root || change_table_heap) {
// Give Dispatch the expected entry values without rebinding slot 35.
invoke(list, 37, 0, constants[0], 0);
invoke(list, 39, 0, 1, word(&constants[1]), 1);
}
if (mode == "dispatch-table-offset") invoke(list, 32, 1, word(&heap)); // same heap keeps the offset table
if (indirect) record.execute(list, mode == "dispatch-indirect-multi" ? 2 : 1,
mode == "dispatch-indirect-counted", mode == "dispatch-indirect-outside");
else if (mode == "dispatch-reference-2" || mode == "dispatch-reference-5")
Expand All @@ -376,8 +406,20 @@ static int dispatch_slot67(IDXGIAdapter* adapter, void* device, const std::strin
if (FAILED(static_cast<HRESULT>(invoke(list, 9)))) return 75;
invoke(queue, 10, 1, word(&list));
if (FAILED(static_cast<HRESULT>(invoke(queue, 14, word(fence), 1)))) return 76;
for (const ULONGLONG deadline = GetTickCount64() + 10000; invoke(fence, 8) < 1;) {
if (GetTickCount64() >= deadline) return 78;
// Render mode publishes the guest fence value after desktop queue completion.
const ULONGLONG deadline = GetTickCount64() + 10000;
for (;;) {
const auto completed = invoke(fence, 8);
const HRESULT removed = static_cast<HRESULT>(invoke(device, 37));
if (completed == ~UINT64{0} || FAILED(removed)) {
std::fprintf(stderr, "slot 67 Dispatch fence: device removed (0x%08lx)\n", static_cast<unsigned long>(removed));
return 78;
}
if (completed == 1) break;
if (GetTickCount64() >= deadline) {
std::fputs("slot 67 Dispatch fence: timed out\n", stderr);
return 78;
}
Sleep(1);
}
if (indirect && !record.check_record(groups)) return 80;
Expand Down
Loading