From 3228f8c99de739f03798f5e84b9447fba0495e2d Mon Sep 17 00:00:00 2001 From: Buhe <41640988+xiaoqi72665@users.noreply.github.com> Date: Sat, 28 Nov 2026 07:18:05 +0800 Subject: [PATCH] test: trace GPU-written indirect Dispatch to offscreen pixels --- docs/experiments.md | 37 +++- tests/startup_slot67_indirect.cpp | 184 ++++++++++++++++-- tests/startup_slot67_indirect.h | 12 +- .../startup_slot67_indirect_dispatch_check.py | 62 ++++++ tests/startup_stream_consumer.cpp | 65 ++++--- 5 files changed, 323 insertions(+), 37 deletions(-) diff --git a/docs/experiments.md b/docs/experiments.md index 5a933f0e57..85111b13b9 100644 --- a/docs/experiments.md +++ b/docs/experiments.md @@ -17178,7 +17178,8 @@ Dispatch record in a placed argument buffer. It binds the same translated pipeli Dispatch, transitions the argument buffer and records desktop `ExecuteIndirect`. The GPU reads the group counts. The fixture records a compute producer, a copy of the argument record and the indirect consumer on one list. -After the queue fence it checks both guard words, all three argument words, the input and all 320 output words. +After the queue fence it checks the leading guard, the neighboring three-word guard record, all three argument words, +the input and all 320 output words. Counts of 2 and 5 write exactly that many words. Direct Dispatch with the same bindings and counts must produce the same outputs. Omitting the producer leaves the zero record and the output sentinel intact. Omitting the translation preserves the output and reports the refusal. Other controls exercise multiple commands, a count @@ -17190,6 +17191,40 @@ ctest --preset release --no-tests=error -j 1 -R '^startup_slot67_indirect_dispat python -B tests/startup_slot67_indirect_dispatch_check.py mutation ``` +#### GPU-written indirect Dispatch to an offscreen pixel (#3209) + +The same fixture also records the GPU writer on a first guest DIRECT list submission, waits for guest +fence value 1, and checks the seven DWORDs (leading guard, 12-byte Dispatch at byte interval [4,16), +neighboring three-DWORD guard at [16,28)). A second recording on that queue copies the same placed +resource immediately before guest list slot 61, then submits and waits for value 2 before checking the +second snapshot. The native resource pointer and GPU virtual address must agree across both boundaries. +The translated slot 67 shader reads its normal raw-buffer table and XORs one input word per group into +the aliased output UAV. The check requires a translated dispatch in the census, not just an untouched buffer. + +A source-built desktop vertex/pixel shader draws a fullscreen triangle to an 8x4 RGBA8 offscreen target. +The pixel shader reads the actual output buffer via a root SRV, discards the sentinel, and writes the +low three bytes of the compute word as RGB. The graphics recording uses the same underlying DIRECT +queue after the guest consumer fence; no CPU readback is copied into the graphics input. The native +queue and output resource identities are checked. Separate clear and post-draw copies are checked +after the graphics fence. The input and all 320 output words are also checked. + +| Case | Both argument snapshots | Changed pixels / 32 | +| --- | --- | ---: | +| GPU writer, X=2 | (2,1,1) | 8 | +| GPU writer, X=5 | (5,1,1) | 20 | +| GPU writer, X=0 | (0,1,1) | 0 | +| Writer omitted | (0,0,0) | 0 | + +The unchanged pixels must match the clear value, and all changed pixels must match their exact RGB +reference. The stale-SRV control binds the original input instead of the compute output; the unchanged +positive pixel oracle rejects it, while the independently predicted stale colours match the GPU readback. +The device-free mutation mode also detects individual changed pixel words and short readbacks. +The existing outside-record, multi-command, count-buffer, output-alias and manifest refusal controls +remain in the same test. The observer checks native D3D12 debug errors when the info queue is available. +The commands above run both the original and extended fixtures; use dev instead of release for Debug. +This is a synthetic producer-to-pixel proof through translated indirect Compute, with a desktop graphics +observer; it does not establish a translated graphics shader, a retail-scene producer, or a performance gain. + ### Entry values from a buffer descriptor A formatted buffer load passes the input's stride and word 3 as entry values (`udsrc stride `, `udsrc descword3 `), and a formatted buffer store (`buffer_store_format_d16_x`, and `buffer_store_format_xyzw` of #2530) passes the output's word 3, the upper half of word 1 and the record count (`uav_descword3`, `uav_stride`, `uav_numrecords`). At Dispatch the stand-in takes each from the descriptor words the same call binds the view from: the stride as the draw path takes it (from the hardware descriptor, or from the title's buffer view for a token), word 3 through the draw path's formatted-buffer check (a token's word is built from its view; a FORMAT the program does not decode refuses the dispatch with `a formatted input has no word 3 the program decodes`), and the output words unmasked, since the store helper checks the swizzle bits itself. A descriptor that is not bound refuses the dispatch as before; an entry value naming a texture slot leaves the pipeline untranslated, and so does any other source. diff --git a/tests/startup_slot67_indirect.cpp b/tests/startup_slot67_indirect.cpp index f3389e2a13..6cb52681fd 100644 --- a/tests/startup_slot67_indirect.cpp +++ b/tests/startup_slot67_indirect.cpp @@ -2,17 +2,19 @@ #include "startup_indirect_writer.h" #include "startup_stream_setup.h" #include +#include #include #include #include +#include extern "C" __declspec(dllimport) HRESULT WINAPI D3D12XboxCreateDevice(IUnknown*, const void*, REFIID, void**); using Microsoft::WRL::ComPtr; namespace { -using Record = std::array; -constexpr Record initial{0xa5a5a5a5, 0, 0, 0, 0x5a5a5a5a}; -constexpr UINT offset = sizeof(UINT); // guards on either side of the three-DWORD record +using Record = std::array; +constexpr Record initial{0xa5a5a5a5, 0, 0, 0, 0x5a5a5a5a, 0x12345678, 0x9abcdef0}; +constexpr UINT offset = sizeof(UINT); // leading guard and a neighboring three-DWORD guard record constexpr char producer[] = R"( RWByteAddressBuffer args : register(u0); cbuffer Size : register(b0) { uint groups; }; @@ -22,7 +24,8 @@ void main() { args.Store3(4, uint3(groups, 1, 1)); } StreamWord word(const void* value) { return reinterpret_cast(value); } } -void Slot67Indirect::prepare(IDXGIAdapter* adapter, void* guest_device, void* guest_list, void* backing, UINT groups) +void Slot67Indirect::prepare(IDXGIAdapter* adapter, void* guest_device, void* guest_list, void* backing, UINT groups, bool write_zero, + void* guest_queue, void* guest_output) { auto* guest = static_cast(guest_device); ComPtr device; @@ -32,7 +35,17 @@ void Slot67Indirect::prepare(IDXGIAdapter* adapter, void* guest_device, void* gu DXGI_FORMAT_UNKNOWN, {1, 0}, D3D12_TEXTURE_LAYOUT_ROW_MAJOR, D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS}; check(static_cast(invoke(guest, 133, word(backing), word(&desc), D3D12_RESOURCE_STATE_COMMON, 0, word(&__uuidof(ID3D12Resource)), word(placed.GetAddressOf()))), "place indirect arguments"); - auto args = indirect_native_object(device.Get(), guest, placed.Get()); + native_args = indirect_native_object(device.Get(), guest, placed.Get()); + const auto& args = native_args; + if (guest_queue) { + native_queue = indirect_native_object(device.Get(), guest, static_cast(guest_queue)); + native_output = indirect_native_object(device.Get(), guest, static_cast(guest_output)); + std::printf("chain queue=%p output=%p gpu=0x%llx\n", static_cast(native_queue.Get()), + static_cast(native_output.Get()), static_cast(native_output->GetGPUVirtualAddress())); + } + std::printf("writer resource=%p gpu=0x%llx interval=[4,16) writer=%s\n", + static_cast(args.Get()), static_cast(args->GetGPUVirtualAddress()), + groups || write_zero ? "dispatch" : "omitted"); upload = make_buffer(device.Get(), sizeof(initial), D3D12_HEAP_TYPE_UPLOAD, D3D12_RESOURCE_STATE_GENERIC_READ); readback = make_buffer(device.Get(), sizeof(initial), D3D12_HEAP_TYPE_READBACK, D3D12_RESOURCE_STATE_COPY_DEST); const D3D12_RANGE empty{0, 0}; @@ -43,7 +56,7 @@ void Slot67Indirect::prepare(IDXGIAdapter* adapter, void* guest_device, void* gu transition(list.Get(), args.Get(), D3D12_RESOURCE_STATE_COMMON, D3D12_RESOURCE_STATE_COPY_DEST); list->CopyBufferRegion(args.Get(), 0, upload.Get(), 0, sizeof(initial)); auto state = D3D12_RESOURCE_STATE_COPY_DEST; - if (groups) { + if (groups || write_zero) { D3D12_ROOT_PARAMETER params[2]{}; params[0].ParameterType = D3D12_ROOT_PARAMETER_TYPE_UAV; params[1].ParameterType = D3D12_ROOT_PARAMETER_TYPE_32BIT_CONSTANTS; @@ -78,16 +91,165 @@ void Slot67Indirect::execute(void* list, UINT commands, bool count_buffer, bool count_buffer ? word(placed.Get()) : 0, count_buffer ? offset : 0); } -bool Slot67Indirect::check_record(UINT groups) const +void Slot67Indirect::capture_before_consumer(IDXGIAdapter* adapter, void* guest_device, void* guest_list) +{ + auto* guest = static_cast(guest_device); + ComPtr device; + check(D3D12XboxCreateDevice(adapter, nullptr, IID_PPV_ARGS(&device)), "open consumer's desktop device"); + auto list = indirect_native_object(device.Get(), guest, static_cast(guest_list)); + auto args = indirect_native_object(device.Get(), guest, placed.Get()); + if (args.Get() != native_args.Get() || args->GetGPUVirtualAddress() != native_args->GetGPUVirtualAddress()) + throw std::runtime_error("indirect argument resource changed"); + std::printf("consumer resource=%p gpu=0x%llx interval=[4,16)\n", + static_cast(args.Get()), static_cast(args->GetGPUVirtualAddress())); + before_consumer = make_buffer(device.Get(), sizeof(initial), D3D12_HEAP_TYPE_READBACK, D3D12_RESOURCE_STATE_COPY_DEST); + transition(list.Get(), args.Get(), D3D12_RESOURCE_STATE_COMMON, D3D12_RESOURCE_STATE_COPY_SOURCE); + list->CopyBufferRegion(before_consumer.Get(), 0, args.Get(), 0, sizeof(initial)); + transition(list.Get(), args.Get(), D3D12_RESOURCE_STATE_COPY_SOURCE, D3D12_RESOURCE_STATE_COMMON); +} + +bool Slot67Indirect::check_record(UINT groups, bool write_zero, bool consumer_boundary) const { const D3D12_RANGE range{0, sizeof(initial)}, empty{0, 0}; void* mapped = nullptr; - check(readback->Map(0, &range, &mapped), "map GPU argument readback after consumer fence"); + auto* snapshot = consumer_boundary ? before_consumer.Get() : readback.Get(); + check(snapshot->Map(0, &range, &mapped), "map GPU argument readback after queue fence"); Record actual{}; std::memcpy(actual.data(), mapped, sizeof(actual)); - readback->Unmap(0, &empty); - const Record expected{initial.front(), groups, groups ? 1u : 0u, groups ? 1u : 0u, initial.back()}; - std::printf("GPU argument record: %08x [%u,%u,%u] %08x\n", actual[0], actual[1], actual[2], actual[3], actual[4]); + snapshot->Unmap(0, &empty); + const UINT yz = groups || write_zero ? 1u : 0u; + Record expected = initial; + expected[1] = groups; + expected[2] = expected[3] = yz; + std::printf("GPU argument record: %08x [%u,%u,%u] %08x %08x %08x (%s)\n", + actual[0], actual[1], actual[2], actual[3], actual[4], actual[5], actual[6], + consumer_boundary ? "before consumer" : "after writer"); std::fflush(stdout); return actual == expected; } + +// A source-built desktop graphics observer. The slot-67 shader's actual aliased UAV is the SRV; +// no CPU copy or CPU-derived colour is used as a draw input. Both captures are fenced on the same +// underlying DIRECT queue as the writer and translated ExecuteIndirect. +void Slot67Indirect::draw_pixels(IDXGIAdapter* adapter, void* guest_device, void* guest_queue, void* guest_output, bool stale) const +{ + auto* guest = static_cast(guest_device); + ComPtr device; + check(D3D12XboxCreateDevice(adapter, nullptr, IID_PPV_ARGS(&device)), "open graphics observer's desktop device"); + auto queue = indirect_native_object(device.Get(), guest, static_cast(guest_queue)); + auto output = indirect_native_object(device.Get(), guest, static_cast(guest_output)); + if (queue.Get() != native_queue.Get() || (output.Get() == native_output.Get()) == stale) + throw std::runtime_error("graphics queue/output identity mismatch"); + std::printf("graphics queue=%p output=%p gpu=0x%llx\n", static_cast(queue.Get()), + static_cast(output.Get()), static_cast(output->GetGPUVirtualAddress())); + constexpr char shader[] = R"( +ByteAddressBuffer result : register(t0); +float4 vs(uint id : SV_VertexID) : SV_Position { + return float4(id == 1 ? 3 : -1, id == 2 ? -3 : 1, 0, 1); +} +float4 ps(float4 position : SV_Position) : SV_Target { + uint word = result.Load(uint(position.x) * 4); + if (word == 0xc5c5c5c5) discard; + return float4(word & 255, (word >> 8) & 255, (word >> 16) & 255, 255) / 255.0; +} +)"; + D3D12_ROOT_PARAMETER param{}; + param.ParameterType = D3D12_ROOT_PARAMETER_TYPE_SRV; + param.ShaderVisibility = D3D12_SHADER_VISIBILITY_PIXEL; + const D3D12_ROOT_SIGNATURE_DESC root_desc{1, ¶m, 0, nullptr, D3D12_ROOT_SIGNATURE_FLAG_NONE}; + ComPtr blob, vs, ps; + ComPtr graphics_root; + check(D3D12SerializeRootSignature(&root_desc, D3D_ROOT_SIGNATURE_VERSION_1, &blob, nullptr), "serialize pixel root"); + check(device->CreateRootSignature(0, blob->GetBufferPointer(), blob->GetBufferSize(), IID_PPV_ARGS(&graphics_root)), "create pixel root"); + check(D3DCompile(shader, sizeof(shader)-1, nullptr, nullptr, nullptr, "vs", "vs_5_0", 0, 0, &vs, nullptr), "compile observer VS"); + check(D3DCompile(shader, sizeof(shader)-1, nullptr, nullptr, nullptr, "ps", "ps_5_0", 0, 0, &ps, nullptr), "compile observer PS"); + constexpr UINT width = 8, height = 4; + constexpr DXGI_FORMAT format = DXGI_FORMAT_R8G8B8A8_UNORM; + D3D12_GRAPHICS_PIPELINE_STATE_DESC pso{}; + pso.pRootSignature = graphics_root.Get(); + pso.VS = {vs->GetBufferPointer(), vs->GetBufferSize()}; + pso.PS = {ps->GetBufferPointer(), ps->GetBufferSize()}; + pso.BlendState.RenderTarget[0].RenderTargetWriteMask = D3D12_COLOR_WRITE_ENABLE_ALL; + pso.SampleMask = UINT_MAX; + pso.RasterizerState.FillMode = D3D12_FILL_MODE_SOLID; + pso.RasterizerState.CullMode = D3D12_CULL_MODE_NONE; + pso.RasterizerState.DepthClipEnable = TRUE; + pso.PrimitiveTopologyType = D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE; + pso.NumRenderTargets = 1; + pso.RTVFormats[0] = format; + pso.SampleDesc.Count = 1; + ComPtr pipeline; + check(device->CreateGraphicsPipelineState(&pso, IID_PPV_ARGS(&pipeline)), "create observer graphics pipeline"); + const D3D12_RESOURCE_DESC desc{D3D12_RESOURCE_DIMENSION_TEXTURE2D, 0, width, height, 1, 1, + format, {1, 0}, D3D12_TEXTURE_LAYOUT_UNKNOWN, D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET}; + const D3D12_CLEAR_VALUE clear{format, {0, 0, 0, 1}}; + auto target = resource(device.Get(), desc, D3D12_HEAP_TYPE_DEFAULT, D3D12_RESOURCE_STATE_RENDER_TARGET, &clear); + D3D12_PLACED_SUBRESOURCE_FOOTPRINT footprint{}; + UINT64 bytes = 0; + device->GetCopyableFootprints(&desc, 0, 1, 0, &footprint, nullptr, nullptr, &bytes); + ComPtr captures[2]; + for (auto& capture : captures) + capture = make_buffer(device.Get(), bytes, D3D12_HEAP_TYPE_READBACK, D3D12_RESOURCE_STATE_COPY_DEST); + const D3D12_DESCRIPTOR_HEAP_DESC rtv_desc{D3D12_DESCRIPTOR_HEAP_TYPE_RTV, 1, D3D12_DESCRIPTOR_HEAP_FLAG_NONE}; + ComPtr rtvs; + check(device->CreateDescriptorHeap(&rtv_desc, IID_PPV_ARGS(&rtvs)), "create observer RTV heap"); + const auto rtv = rtvs->GetCPUDescriptorHandleForHeapStart(); + device->CreateRenderTargetView(target.Get(), nullptr, rtv); + ComputeCommands commands(device.Get(), queue.Get(), pipeline.Get()); + auto* list = commands.list.Get(); + const auto capture = [&](ID3D12Resource* readback) { + transition(list, target.Get(), D3D12_RESOURCE_STATE_RENDER_TARGET, D3D12_RESOURCE_STATE_COPY_SOURCE); + D3D12_TEXTURE_COPY_LOCATION to{readback, D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT}; + to.PlacedFootprint = footprint; + const D3D12_TEXTURE_COPY_LOCATION from{target.Get(), D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX}; + list->CopyTextureRegion(&to, 0, 0, 0, &from, nullptr); + transition(list, target.Get(), D3D12_RESOURCE_STATE_COPY_SOURCE, D3D12_RESOURCE_STATE_RENDER_TARGET); + }; + list->ClearRenderTargetView(rtv, clear.Color, 0, nullptr); + capture(captures[0].Get()); + transition(list, output.Get(), D3D12_RESOURCE_STATE_COMMON, D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE); + list->SetGraphicsRootSignature(graphics_root.Get()); + list->SetGraphicsRootShaderResourceView(0, output->GetGPUVirtualAddress()); + const D3D12_VIEWPORT viewport{0, 0, width, height, 0, 1}; + const D3D12_RECT scissor{0, 0, width, height}; + list->RSSetViewports(1, &viewport); + list->RSSetScissorRects(1, &scissor); + list->OMSetRenderTargets(1, &rtv, FALSE, nullptr); + list->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST); + list->DrawInstanced(3, 1, 0, 0); + capture(captures[1].Get()); + transition(list, output.Get(), D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE, D3D12_RESOURCE_STATE_COMMON); + commands.submit_and_wait(device.Get()); + std::puts("graphics fence completed: clear and draw captures"); + ComPtr info; + if (SUCCEEDED(device.As(&info))) { + for (UINT64 i = 0, n = info->GetNumStoredMessagesAllowedByRetrievalFilter(); i < n; ++i) { + SIZE_T size = 0; + check(info->GetMessage(i, nullptr, &size), "size graphics debug message"); + std::vector storage(size); + auto* message = reinterpret_cast(storage.data()); + check(info->GetMessage(i, message, &size), "read graphics debug message"); + if (message->Severity <= D3D12_MESSAGE_SEVERITY_ERROR) { + std::fprintf(stderr, "graphics debug error: %s\n", message->pDescription); + throw std::runtime_error("graphics debug layer error"); + } + } + std::puts("graphics debug layer: no errors"); + } else { + std::puts("graphics debug layer: unavailable"); + } + char path[MAX_PATH]; + const DWORD n = GetEnvironmentVariableA("X2W_TEST_INDIRECT_PIXEL_OUT", path, MAX_PATH); + if (!n || n >= MAX_PATH) throw std::runtime_error("missing pixel output path"); + std::ofstream file(path, std::ios::binary); + const D3D12_RANGE range{0, static_cast(bytes)}, empty{0, 0}; + for (const auto& readback : captures) { + void* mapped = nullptr; + check(readback->Map(0, &range, &mapped), "map fenced pixel capture"); + for (UINT y = 0; y < height; ++y) + file.write(static_cast(mapped) + footprint.Offset + y * footprint.Footprint.RowPitch, width * sizeof(UINT)); + readback->Unmap(0, &empty); + } + file.close(); + if (!file) throw std::runtime_error("write pixel capture"); +} diff --git a/tests/startup_slot67_indirect.h b/tests/startup_slot67_indirect.h index 7aaba4a6aa..7acc7b0d47 100644 --- a/tests/startup_slot67_indirect.h +++ b/tests/startup_slot67_indirect.h @@ -7,10 +7,16 @@ struct Slot67Indirect { Microsoft::WRL::ComPtr root; Microsoft::WRL::ComPtr writer; - Microsoft::WRL::ComPtr placed, upload, readback; + Microsoft::WRL::ComPtr placed, upload, readback, before_consumer, native_args; Microsoft::WRL::ComPtr signature; + Microsoft::WRL::ComPtr native_queue; + Microsoft::WRL::ComPtr native_output; - void prepare(IDXGIAdapter* adapter, void* guest_device, void* guest_list, void* backing, UINT groups); + void prepare(IDXGIAdapter* adapter, void* guest_device, void* guest_list, void* backing, UINT groups, bool write_zero = false, + void* guest_queue = nullptr, void* guest_output = nullptr); void execute(void* guest_list, UINT commands, bool count_buffer, bool outside) const; - bool check_record(UINT groups) const; + void capture_before_consumer(IDXGIAdapter* adapter, void* guest_device, void* guest_list); + bool check_record(UINT groups, bool write_zero = false, bool consumer_boundary = false) const; + // The guest list has closed and its fence has completed; the output rests in COMMON. + void draw_pixels(IDXGIAdapter* adapter, void* guest_device, void* guest_queue, void* guest_output, bool stale) const; }; diff --git a/tests/startup_slot67_indirect_dispatch_check.py b/tests/startup_slot67_indirect_dispatch_check.py index c6f1db0490..b40606dbac 100644 --- a/tests/startup_slot67_indirect_dispatch_check.py +++ b/tests/startup_slot67_indirect_dispatch_check.py @@ -3,8 +3,11 @@ `mutation` runs the device-free half: it builds the same 320-word expectation as `check()`, verifies that the prefilled sentinel readback matches zero written words, that one flipped consumer word and one flipped sentinel are both caught, and that the 2-group and 5-group references differ only at indices 2, 3 and 4. +#3209 additionally fences the writer and consumer separately and draws actual GPU output to an offscreen target. """ import os +import re +import struct from pathlib import Path import subprocess import sys @@ -83,6 +86,54 @@ def check(directory, consumer, env, mode, groups, written, direct=False): return result +def expected_pixels(words): + # Eight columns, four rows. The native observer discards sentinel pixels and encodes + # the low three bytes of each actual GPU output word into an RGBA8 render target. + return [(0xff000000 if word == C.SENTINEL else 0xff000000 | (word & 0xffffff)) + for word in words[:8]] * 4 + + +def check_pixels(directory, consumer, env, mode, groups, written, writer=True, stale=False): + path = directory / 'pixels.bin' + path.unlink(missing_ok=True) + actual, run = dispatch(directory, consumer, dict(env, X2W_TEST_INDIRECT_PIXEL_OUT=str(path)), + 'dispatch-indirect-pixel-' + mode) + print(run.stdout, end='') + assert not lines(run, 'ExecuteIndirect refused'), run.stderr[-3000:] + census = lines(run, 'x2w-render compute census key ') + assert any('1 dispatches, 1 translated' in line for line in census), run.stderr[-3000:] + assert compare_words(actual, expected_words(written)) == [], (mode, 'compute output mismatch') + identity = re.findall(r'(?:writer|consumer) resource=(\S+) gpu=(\S+) interval=\[4,16\)', run.stdout) + assert len(identity) == 2 and identity[0] == identity[1], run.stdout + yz = int(writer) + record = f'a5a5a5a5 [{groups},{yz},{yz}] 5a5a5a5a 12345678 9abcdef0' + assert f'GPU argument record: {record} (after writer)' in run.stdout, run.stdout + assert f'GPU argument record: {record} (before consumer)' in run.stdout, run.stdout + assert f'writer={"dispatch" if writer else "omitted"}' in run.stdout, run.stdout + queues = re.findall(r'guest queue=(\S+) (?:writer fence=1|consumer fence=2) completed', run.stdout) + assert len(queues) == 2 and queues[0] == queues[1], run.stdout + native = re.findall(r'(?:chain|graphics) queue=(\S+) output=(\S+) gpu=(\S+)', run.stdout) + assert len(native) == 2 and native[0][0] == native[1][0], run.stdout + assert (native[0][1:] != native[1][1:]) == stale, run.stdout + stages = ['writer fence=1 completed', '(after writer)', 'consumer resource=', 'consumer fence=2 completed', + '(before consumer)', 'graphics queue=', 'graphics fence completed'] + assert [run.stdout.index(stage) for stage in stages] == sorted(run.stdout.index(stage) for stage in stages), run.stdout + pixels = list(struct.unpack('<64I', path.read_bytes())) + assert pixels[:32] == [0xff000000] * 32, (mode, 'clear capture') + expected = expected_pixels(expected_words(written)) + if stale: + assert compare_words(pixels[32:], expected), 'stale SRV incorrectly passed the positive oracle' + expected = expected_pixels([(0x8b02a7f1 + i * 0x413b1) & C.MASK for i in range(8)]) + assert compare_words(pixels[32:], expected) == [], (mode, pixels[32:], expected) + for line in lines(run, 'x2w-d3d12-debug '): + assert ' sev=0 ' not in line and ' sev=1 ' not in line, line + print('pixel first row:', ' '.join(f'{word:08x}' for word in pixels[32:40])) + print(f'pixel {mode}: 320 compute words, two 7-DWORD argument snapshots, 64 RGBA8 pixels match; ' + f'{sum(v != 0xff000000 for v in pixels[32:])}/32 changed' + + (' (stale SRV rejected by positive oracle)' if stale else '')) + return run + + def check_mutation(): sentinel = [C.SENTINEL] * C.WORDS assert len(sentinel) == 320 and C.WORDS == 320, (len(sentinel), C.WORDS) @@ -102,6 +153,14 @@ def check_mutation(): differing = [i for i in range(C.WORDS) if two[i] != five[i]] assert differing == [2, 3, 4], f'2/5 group references differ at {differing}' assert compare_words(two, five) == [2, 3, 4] + for groups in (0, 2, 5): + pixels = expected_pixels(expected_words(groups)) + for index in (0, 7, 31): + mutant = list(pixels) + mutant[index] ^= 1 + assert compare_words(mutant, pixels) == [index] + assert compare_words(pixels[:-1], pixels) + assert compare_words(expected_pixels(two), expected_pixels(five)) == [y * 8 + x for y in range(4) for x in (2, 3, 4)] print(f'mutation oracle: {C.WORDS} words, sentinel 0x{C.SENTINEL:08x}; consumer and sentinel flips detected; ' '2/5 references differ at [2, 3, 4]') @@ -134,6 +193,9 @@ def main(consumer, dxc, adapter): assert not lines(run, 'refused:'), run.stderr[-2500:] if groups: check(directory, consumer, env, str(groups), groups, groups, direct=True) + for mode, groups, writer in (('2', 2, True), ('5', 5, True), ('zero', 0, True), ('omit', 0, False)): + check_pixels(directory, consumer, env, mode, groups, groups, writer) + check_pixels(directory, consumer, env, '5-stale', 5, 5, stale=True) for mode, reason in (('multi', 'one rootless command'), ('counted', 'no count buffer'), ('outside', 'outside a unique placed buffer'), ('output-alias', 'also a bound shader resource')): run = check(directory, consumer, env, mode, 5, 0) diff --git a/tests/startup_stream_consumer.cpp b/tests/startup_stream_consumer.cpp index 0b91c663d8..4b5f44da20 100644 --- a/tests/startup_stream_consumer.cpp +++ b/tests/startup_stream_consumer.cpp @@ -229,8 +229,11 @@ static int dispatch_slot67(IDXGIAdapter* adapter, void* device, const std::strin constexpr UINT words = 320; const bool shared_uav = mode == "dispatch-read-write-uav"; const bool indirect = mode.rfind("dispatch-indirect-", 0) == 0; + const bool pixel = mode.rfind("dispatch-indirect-pixel-", 0) == 0; + const bool write_zero = mode == "dispatch-indirect-pixel-zero"; const bool root_srv = mode.rfind("dispatch-root-srv", 0) == 0; - const UINT groups = mode == "dispatch-indirect-zero" ? 0u : mode == "dispatch-indirect-2" ? 2u : 5u; + const UINT groups = mode == "dispatch-indirect-zero" || write_zero || mode == "dispatch-indirect-pixel-omit" ? 0u : + mode == "dispatch-indirect-2" || mode == "dispatch-indirect-pixel-2" ? 2u : 5u; auto* memory = static_cast(VirtualAlloc(nullptr, (indirect || root_srv) ? 0x30000 : 0x20000, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE)); if (!memory) return 70; UINT* const buffers[2] = {memory, memory + 0x10000 / sizeof(UINT)}; @@ -348,8 +351,37 @@ static int dispatch_slot67(IDXGIAdapter* adapter, void* device, const std::strin invoke(list, 33, word(mode == "dispatch-wrong-root" ? slot67_other_root : slot67_root)); }; if (FAILED(static_cast(invoke(list, 139, word(allocator))))) return 74; + const auto submit = [&](UINT64 value) -> int { + if (FAILED(static_cast(invoke(list, 9)))) return 75; + invoke(queue, 10, 1, word(&list)); + if (FAILED(static_cast(invoke(queue, 14, word(fence), value)))) return 76; + // 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(invoke(device, 37)); + if (completed == ~UINT64{0} || FAILED(removed)) { + std::fprintf(stderr, "slot 67 Dispatch fence: device removed (0x%08lx)\n", static_cast(removed)); + return 78; + } + if (completed == value) break; + if (GetTickCount64() >= deadline) { + std::fputs("slot 67 Dispatch fence: timed out\n", stderr); + return 78; + } + Sleep(1); + } + return 0; + }; Slot67Indirect record; - if (indirect) record.prepare(adapter, device, list, memory + 0x20000 / sizeof(UINT), groups); + if (indirect) record.prepare(adapter, device, list, memory + 0x20000 / sizeof(UINT), groups, write_zero, + pixel ? queue : nullptr, pixel ? placed[1] : nullptr); + if (pixel) { + if (const int rc = submit(1)) return rc; + std::printf("guest queue=%p writer fence=1 completed\n", queue); + if (!record.check_record(groups, write_zero)) return 80; + if (FAILED(static_cast(invoke(list, 139, word(allocator))))) return 74; + } bind(); if (mode.rfind("dispatch-sampled", 0) == 0 || mode == "dispatch-input-token") { const bool depth = mode == "dispatch-sampled-depth"; @@ -465,31 +497,20 @@ static int dispatch_slot67(IDXGIAdapter* adapter, void* device, const std::strin 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 (pixel) record.capture_before_consumer(adapter, device, list); 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") invoke(list, 14, mode == "dispatch-reference-2" ? 2 : 5, 1, 1); else if (mode != "dispatch-none" && mode != "dispatch-root-srv-none") invoke(list, 14, 3, 2, 2); - if (FAILED(static_cast(invoke(list, 9)))) return 75; - invoke(queue, 10, 1, word(&list)); - if (FAILED(static_cast(invoke(queue, 14, word(fence), 1)))) return 76; - // 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(invoke(device, 37)); - if (completed == ~UINT64{0} || FAILED(removed)) { - std::fprintf(stderr, "slot 67 Dispatch fence: device removed (0x%08lx)\n", static_cast(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; + if (const int rc = submit(pixel ? 2 : 1)) return rc; + if (pixel) { + std::printf("guest queue=%p consumer fence=2 completed\n", queue); + if (!record.check_record(groups, write_zero, true)) return 80; + const bool stale = mode == "dispatch-indirect-pixel-5-stale"; + record.draw_pixels(adapter, device, queue, placed[stale ? 0 : 1], stale); + } + if (indirect && !pixel && !record.check_record(groups)) return 80; for (UINT i = 0; i < words; ++i) if (buffers[0][i] != input[i]) return 79; // the input must stay as written x2w_d3d12_compute_census();