diff --git a/src/startup_stubs/d3d12_compute_dispatch.inl b/src/startup_stubs/d3d12_compute_dispatch.inl index 3dfc274449..ca7c40d944 100644 --- a/src/startup_stubs/d3d12_compute_dispatch.inl +++ b/src/startup_stubs/d3d12_compute_dispatch.inl @@ -2,7 +2,8 @@ // the key of its published request; gfx2hlsl.py --pending answers the request with .txt (`stage cs`) and // .cs.cso. Bound from the compute tables recorded by slot 35 and compute root SRVs recorded by slot 43: raw // buffers (`srv buf table ` and `srv buf root ` inputs, `uav table ` -// outputs) named by hardware buffer descriptors or 64-bit root SRV addresses, and textures +// outputs) named by hardware buffer descriptors or 64-bit root SRV addresses or, for outputs, by the token of a raw, +// structured or typed buffer view the title created (device slot 19), and textures // (`srv tex2d table ` inputs, `uav tex1d/tex2d/tex2da/tex3d float table 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 @@ -533,12 +534,57 @@ const char* record_translated_dispatch(Wrapper* self, const TranslatedDispatch& std::memcpy(views[i].words, words, sizeof(views[i].words)); continue; } + if (words[0] == view_token_magic) { + // An output named by the token of a raw, structured or typed buffer UAV the title created (device slot 19) is + // bound in that view's desktop resource over the view's own range, as an input token is (slot 18). + ViewRecord record{}; + bool known = false; + AcquireSRWLockShared(¬e_lock); + if (words[1] < view_records().size()) { + record = view_records()[words[1]]; + known = true; + } + ReleaseSRWLockShared(¬e_lock); + const Wrapper* const owner = known ? as_wrapper(reinterpret_cast(record.resource)) : nullptr; + auto* const buffer = owner && owner->own().kind == resource_kind ? static_cast(owner->own().real) : nullptr; + D3D12_UNORDERED_ACCESS_VIEW_DESC desc{}; + if (known && record.desc_bytes >= sizeof(desc)) std::memcpy(&desc, record.desc, sizeof(desc)); + if (desc.Format) desc.Format = desktop_format(desc.Format); + const bool raw = desc.Buffer.Flags == D3D12_BUFFER_UAV_FLAG_RAW && desc.Format == DXGI_FORMAT_R32_TYPELESS && + !desc.Buffer.StructureByteStride; + const bool structured = desc.Buffer.Flags == D3D12_BUFFER_UAV_FLAG_NONE && desc.Format == DXGI_FORMAT_UNKNOWN && + desc.Buffer.StructureByteStride; + const bool typed = desc.Buffer.Flags == D3D12_BUFFER_UAV_FLAG_NONE && !desc.Buffer.StructureByteStride; + const UINT element = raw ? 4 : structured ? desc.Buffer.StructureByteStride : + typed ? x2w_startup::typed_buffer_format_bytes(desc.Format) : 0; + const D3D12_RESOURCE_DESC resource = buffer ? buffer->GetDesc() : D3D12_RESOURCE_DESC{}; + const u64 offset = element && desc.Buffer.FirstElement <= UINT64_MAX / element ? desc.Buffer.FirstElement * element : ~u64{}; + const u64 bytes = u64{desc.Buffer.NumElements} * element; + if (!known || record.slot != 19 || !buffer || record.desc_bytes < sizeof(desc) || + desc.ViewDimension != D3D12_UAV_DIMENSION_BUFFER || resource.Dimension != D3D12_RESOURCE_DIMENSION_BUFFER || + !(resource.Flags & D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS)) + reason = "an output view token is not a buffer UAV of a desktop resource"; + else if (!element) reason = "an output buffer view has no element size"; + else if (!bytes || bytes % 4 || bytes > (16u << 20) || offset % D3D12_RAW_UAV_SRV_BYTE_ALIGNMENT || + offset > resource.Width || bytes > resource.Width - offset) + reason = "an output buffer view range is empty, unaligned or outside its resource"; + // The entry values an output descriptor supplies are words of a hardware descriptor, which a token has not. + for (const ComputeTranslation::Slot& ud : tr->uds) + if (!reason && ud.source >= ComputeTranslation::output_word3 && ud.a >= 0 && + inputs + static_cast(ud.a) == i) + reason = "an entry value reads the descriptor of an output named by a view token"; + for (std::size_t input = 0; input < inputs && !reason; ++input) + if (views[input].resource == buffer) reason = "an output buffer is also an input"; + views[i] = {buffer, offset, bytes, true}; + std::memcpy(views[i].words, words, sizeof(views[i].words)); + continue; + } // An output is written by the GPU: only a placed buffer that aliases the title's memory shows the result - // to the title. shortcut: view tokens and copied placements are refused, add them with their first user. + // to the title. shortcut: copied placements are refused, add them with their first user. const u64 address = words[0] | (u64{words[1] & 0xffff} << 32); const UINT stride = (words[1] >> 16) & 0x3fff; const u64 bytes = u64{stride ? stride : 1} * words[2]; - const auto placed = words[0] == view_token_magic || !bytes || bytes % 4 || bytes > (16u << 20) + const auto placed = !bytes || bytes % 4 || bytes > (16u << 20) ? PlacedVertexView{} : placed_buffer_range(self->own().list_device, address, bytes); const ResourceInfo* info = placed.alias ? resource_info(placed.owner) : nullptr; const u64 offset = placed.alias ? address - reinterpret_cast(placed.owner->own().title_memory) : 0; diff --git a/tests/startup_compute_dispatch_check.py b/tests/startup_compute_dispatch_check.py index 33ca1733c4..08259995a9 100644 --- a/tests/startup_compute_dispatch_check.py +++ b/tests/startup_compute_dispatch_check.py @@ -22,6 +22,10 @@ 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. +8. the output named by the token of a buffer view of the output buffer, all 320 words matching and nothing refused: + dispatch-output-token (raw), dispatch-output-token-typed (R32_UINT), and dispatch-output-token-structured + (stride 8) and dispatch-output-token-typed8 (R16G16B16A16_FLOAT), whose 8-byte elements catch an element size + taken as 4. Exit 77 only if the initial probe cannot use the adapter or required heaps. """ import os @@ -156,6 +160,13 @@ def main(consumer, dxc, adapter): 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] + for mode in ('dispatch-output-token', 'dispatch-output-token-typed', 'dispatch-output-token-structured', + 'dispatch-output-token-typed8'): + got, result = dispatch(directory, consumer, env, mode) + 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 not lines(result, 'refused: '), result.stderr[-3000:] + print(f'{mode}: all {C.WORDS} words match the CPU reference') print(f'startup_compute_dispatch: PASS on {adapter}') diff --git a/tests/startup_stream_consumer.cpp b/tests/startup_stream_consumer.cpp index 5e75f5f394..6f1cebd535 100644 --- a/tests/startup_stream_consumer.cpp +++ b/tests/startup_stream_consumer.cpp @@ -207,7 +207,11 @@ static void* make_committed(void* device, D3D12_HEAP_TYPE type, const D3D12_RESO // Controls on the recorded state: dispatch-missing-word leaves out the slot 37 call; dispatch-root-changed sets // another compute root and the first one again after the constants, dispatch-same-root sets the same root again; // dispatch-reset closes and resets the list after the constants; dispatch-wrong-root records under the other -// root; dispatch-same-buffer names the output buffer as the input too. +// root; dispatch-same-buffer names the output buffer as the input too. dispatch-output-token writes, where the output +// descriptor was, the token of a raw unordered access view of the output buffer (device slot 19); +// dispatch-output-token-typed makes that view R32_UINT. dispatch-output-token-structured (stride 8) and +// dispatch-output-token-typed8 (R16G16B16A16_FLOAT) describe the same 1280 bytes as 160 8-byte elements, so an +// element size taken as 4 binds only the first half of the output. // Input descriptors the dispatch refuses, one reason each: dispatch-input-empty has zero records (a nonzero base, // word 3 zero), dispatch-input-past ends 16 bytes past the input buffer, dispatch-input-unplaced names memory between // the two buffers, dispatch-input-copied a third buffer placed there at an offset the desktop cannot alias, and @@ -416,6 +420,21 @@ static int dispatch_slot67(IDXGIAdapter* adapter, void* device, const std::strin if (FAILED(static_cast(invoke(list, 9))) || FAILED(static_cast(invoke(list, 139, word(allocator))))) return 74; bind(); } + if (mode.rfind("dispatch-output-token", 0) == 0) { + D3D12_UNORDERED_ACCESS_VIEW_DESC view{DXGI_FORMAT_R32_TYPELESS, D3D12_UAV_DIMENSION_BUFFER}; + view.Buffer = {0, words, 0, 0, D3D12_BUFFER_UAV_FLAG_RAW}; + if (mode == "dispatch-output-token-typed") { + view.Format = DXGI_FORMAT_R32_UINT; + view.Buffer.Flags = D3D12_BUFFER_UAV_FLAG_NONE; + } else if (mode == "dispatch-output-token-structured") { + view.Format = DXGI_FORMAT_UNKNOWN; + view.Buffer = {0, words / 2, 8, 0, D3D12_BUFFER_UAV_FLAG_NONE}; + } else if (mode == "dispatch-output-token-typed8") { + view.Format = DXGI_FORMAT_R16G16B16A16_FLOAT; + view.Buffer = {0, words / 2, 0, 0, D3D12_BUFFER_UAV_FLAG_NONE}; + } + invoke(device, 19, word(placed[1]), 0, word(&view), cpu + sizeof(descriptors[0])); + } if (mode != "dispatch-missing-word") invoke(list, 37, 0, constants[0], 0); invoke(list, 40, 0, 2, word(graphics), 0); invoke(list, 39, 0, 1, word(&constants[1]), 1);