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
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1267,6 +1267,7 @@ target_sources(startup_stream_consumer PRIVATE tests/startup_frame_copy.cpp)
target_sources(startup_stream_consumer PRIVATE tests/startup_root_reset.cpp)
target_sources(startup_stream_consumer PRIVATE tests/startup_memory_srv_diag.cpp)
target_sources(startup_stream_consumer PRIVATE tests/startup_pixel_uav.cpp)
target_sources(startup_stream_consumer PRIVATE tests/startup_slot67_indirect.cpp tests/desktop_gpu.cpp)

add_executable(startup_stream_depth tests/startup_stream_depth.cpp $<TARGET_OBJECTS:startup_stream_setup_obj>)
target_compile_features(startup_stream_depth PRIVATE cxx_std_17)
Expand Down Expand Up @@ -4534,6 +4535,10 @@ if(EXISTS "${INTAKE_PYTHON}")
add_test(NAME startup_compute_dispatch_texture_${adapter} COMMAND "${INTAKE_PYTHON}" -B
"${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_compute_dispatch_texture_check.py" "$<TARGET_FILE:startup_stream_consumer>"
"${DXC_EXECUTABLE}" ${adapter})
add_test(NAME startup_slot67_indirect_dispatch_${adapter} COMMAND "${INTAKE_PYTHON}" -B
"${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_slot67_indirect_dispatch_check.py" "$<TARGET_FILE:startup_stream_consumer>"
"${DXC_EXECUTABLE}" ${adapter})
set_tests_properties(startup_slot67_indirect_dispatch_${adapter} PROPERTIES TIMEOUT 120 SKIP_RETURN_CODE 77)
add_test(NAME startup_compute_dispatch_heap_${adapter} COMMAND "${INTAKE_PYTHON}" -B
"${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_compute_dispatch_heap_check.py" "$<TARGET_FILE:startup_stream_consumer>"
"${DXC_EXECUTABLE}" ${adapter})
Expand Down
19 changes: 19 additions & 0 deletions docs/experiments.md
Original file line number Diff line number Diff line change
Expand Up @@ -16755,6 +16755,25 @@ cmake --build --preset dev --target startup_stream_consumer
ctest --preset dev --no-tests=error -R "^startup_compute_dispatch_" -V
```

### GPU-written indirect Dispatch

For a translated slot 67 pipeline with fixed-table raw buffers, command-list slot 61 accepts one rootless
Dispatch record in a placed argument buffer. It binds the same translated pipeline and descriptors as direct
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.
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
buffer, an out-of-range record and an argument buffer also bound as shader output.

```powershell
cmake --build --preset release --target startup_stream_consumer
ctest --preset release --no-tests=error -j 1 -R '^startup_slot67_indirect_dispatch_(warp|hardware)$' -V
python -B tests/startup_slot67_indirect_dispatch_check.py mutation
```

### Entry values from a buffer descriptor

A formatted buffer load passes the input's stride and word 3 as entry values (`udsrc <i> stride <srv>`, `udsrc <i> descword3 <srv>`), 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.
Expand Down
34 changes: 23 additions & 11 deletions src/startup_stubs/d3d12_compute_dispatch.inl
Original file line number Diff line number Diff line change
Expand Up @@ -165,14 +165,13 @@ const char* load_compute_translation(ComputeTranslation& tr)
return nullptr;
}

// True when the bound pipeline is a slot 67 placeholder with a request key: the call is then settled here
// (dispatched, or refused and logged). False leaves it to the desktop-pipeline decision of list_dispatch.
bool dispatch_translated(Wrapper* self, UINT x, UINT y, UINT z)
// Both callers have an open list and a slot 67 request. The indirect caller has checked the signature and argument range.
const char* record_translated_dispatch(Wrapper* self, const TranslatedDispatch& dispatch)
{
auto* list = recording(self);
Pass& pass = pass_of(self);
const u64 key = list && pass.pipeline ? pass.pipeline->own().compute_request_key : 0;
if (!key) return false;
const u64 key = pass.pipeline->own().compute_request_key;
const auto [x, y, z] = dispatch.groups;
const char* reason = nullptr;
LONG* counter = &compute_dispatches_refused;
struct View {
Expand Down Expand Up @@ -216,6 +215,10 @@ bool dispatch_translated(Wrapper* self, UINT x, UINT y, UINT z)
for (std::size_t i = 0; i < count && !reason; ++i) {
const bool output = i >= inputs;
const ComputeTranslation::Slot slot = output ? tr->uavs[i - inputs] : tr->srvs[i];
if (dispatch.signature && (slot.heap || slot.texture)) {
reason = "translated indirect Dispatch requires fixed-table raw buffers";
break;
}
std::uint32_t index = slot.a < 0 || slot.b < 0 ? 0 : pass.compute_constants.word(slot.a, slot.b);
std::memset(words, 0, sizeof(words));
if (slot.word_param >= 0) {
Expand Down Expand Up @@ -339,6 +342,9 @@ bool dispatch_translated(Wrapper* self, UINT x, UINT y, UINT z)
views[i] = {placed.alias, offset, bytes, true};
std::memcpy(views[i].words, words, sizeof(views[i].words));
}
if (dispatch.argument)
for (std::size_t i = 0; i < count && !reason; ++i)
if (views[i].resource == dispatch.argument) reason = "the indirect argument buffer is also a bound shader resource";
// An entry value from a descriptor comes from the words its view was bound from in the loop above.
std::uint32_t values[32] = {};
for (std::size_t i = 0; !reason && i < tr->uds.size(); ++i) {
Expand Down Expand Up @@ -377,8 +383,9 @@ bool dispatch_translated(Wrapper* self, UINT x, UINT y, UINT z)
InterlockedIncrement(counter);
// Dispatches that arrive before the offline translation must not use up the lines for bind refusals.
static LONG untranslated, refusals;
if (first_calls(counter == &compute_dispatches_refused ? refusals : untranslated, 16)) log("x2w-draw compute %016llx Dispatch(%u, %u, %u) refused: %s", key, x, y, z, reason);
return true;
if (!dispatch.signature && first_calls(counter == &compute_dispatches_refused ? refusals : untranslated, 16))
log("x2w-draw compute %016llx Dispatch(%u, %u, %u) refused: %s", key, x, y, z, reason);
return reason;
}
const UINT n = static_cast<UINT>(count);
UINT first = static_cast<UINT>(InterlockedExchangeAdd(&draw_heap_cursor, static_cast<LONG>(n))) % draw_heap_size;
Expand Down Expand Up @@ -425,7 +432,12 @@ bool dispatch_translated(Wrapper* self, UINT x, UINT y, UINT z)
list->SetComputeRootSignature(tr->root.Get());
if (!tr->uds.empty()) list->SetComputeRoot32BitConstants(0, static_cast<UINT>(tr->uds.size()), values, 0);
if (n) list->SetComputeRootDescriptorTable(1, D3D12_GPU_DESCRIPTOR_HANDLE{draw_heap->GetGPUDescriptorHandleForHeapStart().ptr + u64{first} * draw_heap_increment});
list->Dispatch(x, y, z);
if (dispatch.signature) {
require_state(self, dispatch.argument, D3D12_RESOURCE_STATE_INDIRECT_ARGUMENT);
list->ExecuteIndirect(dispatch.signature, 1, dispatch.argument, dispatch.offset, nullptr, 0);
} else {
list->Dispatch(x, y, z);
}
for (std::size_t i = inputs; i < count; ++i) { // a later dispatch on this list reads what this one wrote
D3D12_RESOURCE_BARRIER barrier{};
barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_UAV;
Expand All @@ -441,13 +453,13 @@ bool dispatch_translated(Wrapper* self, UINT x, UINT y, UINT z)
++tr->translated;
ReleaseSRWLockExclusive(&draw_lock);
static LONG checks; // as translated draws do: the queue's execute drains the rest
if ((InterlockedIncrement(&checks) & 63) == 1) check_device("translated dispatch", 14);
if ((InterlockedIncrement(&checks) & 63) == 1) check_device("translated dispatch", dispatch.signature ? 61 : 14);
InterlockedIncrement(&compute_dispatches_translated);
static LONG logged;
if (first_calls(logged, 8))
if (!dispatch.signature && first_calls(logged, 8))
log("x2w-draw compute %016llx Dispatch(%u, %u, %u) translated: entry [%x %x %x %x], %zu inputs, %zu outputs", key, x, y, z,
values[0], values[1], values[2], values[3], inputs, count - inputs);
return true;
return nullptr;
}

// With the periodic totals: refused dispatches by the first refusal reason of their key, then every key by
Expand Down
25 changes: 22 additions & 3 deletions src/startup_stubs/d3d12_xs_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2951,6 +2951,13 @@ struct IndirectIndexView {
};
const char* draw_translated_indirect(Wrapper* self, ID3D12CommandSignature* signature, ID3D12Resource* argument,
u64 offset, const IndirectIndexView& indices);
struct TranslatedDispatch {
D3D12_DISPATCH_ARGUMENTS groups{};
ID3D12CommandSignature* signature = nullptr;
ID3D12Resource* argument = nullptr;
u64 offset = 0;
};
const char* record_translated_dispatch(Wrapper* self, const TranslatedDispatch& dispatch);
u64 known_pipeline_key(Wrapper* pipeline);

namespace indirect_other_refusal_code {
Expand Down Expand Up @@ -3053,6 +3060,7 @@ void __stdcall list_execute_indirect(Wrapper* self, void* signature, UINT count,
const bool graphics = indexed || mesh || command == IndirectCommand::draw;
Pass& pass = pass_of(self);
refusal_pipeline = pass.pipeline;
const bool translated_compute = command == IndirectCommand::dispatch && pass.pipeline && pass.pipeline->own().compute_request_key;
const bool translated = (command == IndirectCommand::draw || indexed) && pass.pipeline && !pass.pipeline->own().real &&
(pass.graphics_root || pass.deferred_root) && x2w_local::env_on("X2W_TRANSLATED_INDIRECT_DRAW");
u64 bytes = sig->own().indirect_bytes;
Expand Down Expand Up @@ -3132,6 +3140,9 @@ void __stdcall list_execute_indirect(Wrapper* self, void* signature, UINT count,
if (samples.Count != 1 || samples.Quality)
return refuse("draw requires a single-sample render target", indirect_other, indirect_other_refusal_code::multisample_target);
}
} else if (translated_compute) {
if (count != 1 || count_buffer || sig->own().signature_root)
return refuse("translated Dispatch requires one rootless command and no count buffer", indirect_compute_pipeline);
} else if (!pass.compute_pipeline || !pass.compute_root) {
return refuse("requires a desktop compute pipeline and root", indirect_compute_pipeline);
} else if (sig->own().signature_root && sig->own().signature_root != pass.compute_root) {
Expand All @@ -3158,6 +3169,12 @@ void __stdcall list_execute_indirect(Wrapper* self, void* signature, UINT count,
if (!info || info->initial_state != D3D12_RESOURCE_STATE_COMMON)
return refuse("argument buffer does not use COMMON state tracking", indirect_other, indirect_other_refusal_code::argument_not_common_state);
auto* resource = static_cast<ID3D12Resource*>(buffer->own().real);
if (translated_compute) {
const TranslatedDispatch dispatch{{}, static_cast<ID3D12CommandSignature*>(sig->own().real), resource, offset};
if (const char* failure = record_translated_dispatch(self, dispatch)) return refuse(failure, indirect_compute_pipeline);
InterlockedIncrement64(&indirect_calls[indirect_forwarded][static_cast<int>(kind)]);
return;
}
IndirectIndexView index_view;
if (indexed) {
auto view = pass.ib;
Expand Down Expand Up @@ -3837,15 +3854,17 @@ void __stdcall list_root_cbv(Wrapper* self, UINT parameter, u64 address)
pass.root_address[parameter] = (address && readable_window(address, 1)) ? address : 0;
}
// List slot 14, Dispatch. Forwarded only with a desktop compute pipeline and root bound and group counts within the limit.
bool dispatch_translated(Wrapper* self, UINT x, UINT y, UINT z);
void __stdcall list_dispatch(Wrapper* self, UINT x, UINT y, UINT z)
{
if (dispatch_translated(self, x, y, z)) return; // a slot 67 placeholder never reaches the desktop decision
auto* list = recording(self);
const Pass& pass = pass_of(self);
if (list && pass.pipeline && pass.pipeline->own().compute_request_key) {
record_translated_dispatch(self, {{x, y, z}});
return;
}
x2w_startup::ListDispatch request;
request.list_open = list != nullptr;
if (list) request.type = list->GetType();
const Pass& pass = pass_of(self);
request.compute_pipeline = pass.compute_pipeline;
request.compute_root = pass.compute_root != nullptr;
request.compute_root_has_arguments = pass.compute_root_has_arguments;
Expand Down
28 changes: 5 additions & 23 deletions tests/startup_indirect_dispatch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <wrl/client.h>

#include "adapter_name_filter.h"
#include "desktop_gpu.h"
#include "startup_indirect_writer.h"

#include <algorithm>
#include <array>
Expand Down Expand Up @@ -116,22 +116,6 @@ Method slot(IUnknown* object, unsigned index)
return reinterpret_cast<Method>((*reinterpret_cast<void***>(object))[index]);
}

template <typename T>
ComPtr<T> native_object(ID3D12Device* device, ID3D12Device* guest, IUnknown* object)
{
// SetPrivateDataInterface unwraps a guest argument before the call reaches the shared desktop device.
constexpr GUID key{0x10a62244, 0x1615, 0x4953, {0xa2, 0x52, 0xbe, 0x42, 0x35, 0x27, 0xa0, 0x61}};
check(guest->SetPrivateDataInterface(key, object), "publish underlying object");
ComPtr<IUnknown> stored;
UINT bytes = sizeof(IUnknown*);
const HRESULT read = device->GetPrivateData(key, &bytes, stored.GetAddressOf());
check(guest->SetPrivateDataInterface(key, nullptr), "clear underlying object");
check(read, "retrieve underlying object");
ComPtr<T> result;
check(stored.As(&result), "query underlying object");
return result;
}

ComPtr<IUnknown> guest_pipeline(ID3D12Device* guest, ID3D12RootSignature* root)
{
const auto code = compile(constants ? "consume_constant_args" : "consume_args");
Expand Down Expand Up @@ -187,7 +171,7 @@ bool run(ID3D12Device* device, ID3D12Device* guest, ID3D12RootSignature* root, I
check(slot<Place>(guest, 133)(guest, static_cast<char*>(memory_backing.get()) + 4, &argument_desc,
D3D12_RESOURCE_STATE_COMMON, nullptr, __uuidof(ID3D12Resource),
reinterpret_cast<void**>(guest_arguments.GetAddressOf())), "place guest argument buffer");
const auto arguments = native_object<ID3D12Resource>(device, guest, guest_arguments.Get());
const auto arguments = indirect_native_object<ID3D12Resource>(device, guest, guest_arguments.Get());
auto result = make_buffer(device, result_bytes, D3D12_HEAP_TYPE_DEFAULT, D3D12_RESOURCE_STATE_COPY_DEST,
D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS);
void* memory = nullptr;
Expand All @@ -205,7 +189,7 @@ bool run(ID3D12Device* device, ID3D12Device* guest, ID3D12RootSignature* root, I
reinterpret_cast<void**>(guest_list.GetAddressOf())), "create guest command list");
using Reset = HRESULT (WINAPI*)(IUnknown*, IUnknown*, void*, void*);
check(slot<Reset>(guest_list.Get(), 139)(guest_list.Get(), allocator.Get(), nullptr, nullptr), "reset guest command list");
const auto native_list = native_object<ID3D12GraphicsCommandList>(device, guest, guest_list.Get());
const auto native_list = indirect_native_object<ID3D12GraphicsCommandList>(device, guest, guest_list.Get());
auto* list = native_list.Get();
transition(list, arguments.Get(), D3D12_RESOURCE_STATE_COMMON, D3D12_RESOURCE_STATE_COPY_DEST);
list->CopyBufferRegion(arguments.Get(), 0, upload.Get(), 0, argument_bytes);
Expand All @@ -216,9 +200,7 @@ bool run(ID3D12Device* device, ID3D12Device* guest, ID3D12RootSignature* root, I
if (writer) {
transition(list, arguments.Get(), argument_state, D3D12_RESOURCE_STATE_UNORDERED_ACCESS);
argument_state = D3D12_RESOURCE_STATE_UNORDERED_ACCESS;
list->SetPipelineState(writer);
list->SetComputeRootUnorderedAccessView(0, arguments->GetGPUVirtualAddress());
list->Dispatch(1, 1, 1);
record_indirect_writer(list, writer, arguments.Get());
}
transition(list, arguments.Get(), argument_state, D3D12_RESOURCE_STATE_COPY_SOURCE);
list->CopyBufferRegion(readback.Get(), 0, arguments.Get(), 0, argument_bytes);
Expand Down Expand Up @@ -271,7 +253,7 @@ bool run(ID3D12Device* device, ID3D12Device* guest, ID3D12RootSignature* root, I
ComPtr<IUnknown> guest_queue;
check(guest->CreateCommandQueue(&queue_desc, __uuidof(ID3D12CommandQueue),
reinterpret_cast<void**>(guest_queue.GetAddressOf())), "create guest queue");
const auto queue = native_object<ID3D12CommandQueue>(device, guest, guest_queue.Get());
const auto queue = indirect_native_object<ID3D12CommandQueue>(device, guest, guest_queue.Get());
ComPtr<ID3D12Fence> gate;
check(device->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&gate)), "create gate fence");
check(queue->Wait(gate.Get(), 1), "hold the guest queue on the gate");
Expand Down
26 changes: 26 additions & 0 deletions tests/startup_indirect_writer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#pragma once
#include "desktop_gpu.h"

template <typename T>
Microsoft::WRL::ComPtr<T> indirect_native_object(ID3D12Device* device, ID3D12Device* guest, IUnknown* object)
{
// SetPrivateDataInterface unwraps a guest argument before the call reaches the shared desktop device.
constexpr GUID key{0x10a62244, 0x1615, 0x4953, {0xa2, 0x52, 0xbe, 0x42, 0x35, 0x27, 0xa0, 0x61}};
check(guest->SetPrivateDataInterface(key, object), "publish underlying object");
Microsoft::WRL::ComPtr<IUnknown> stored;
UINT bytes = sizeof(IUnknown*);
const HRESULT read = device->GetPrivateData(key, &bytes, stored.GetAddressOf());
check(guest->SetPrivateDataInterface(key, nullptr), "clear underlying object");
check(read, "retrieve underlying object");
Microsoft::WRL::ComPtr<T> result;
check(stored.As(&result), "query underlying object");
return result;
}

inline void record_indirect_writer(ID3D12GraphicsCommandList* list, ID3D12PipelineState* writer,
ID3D12Resource* arguments)
{
list->SetPipelineState(writer);
list->SetComputeRootUnorderedAccessView(0, arguments->GetGPUVirtualAddress());
list->Dispatch(1, 1, 1);
}
Loading
Loading