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
79 changes: 79 additions & 0 deletions docs/experiments/startup-compute-root-srv.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# Startup compute root SRV (`srv <i> buf root <param>`) manifest consumer

Refs [#2618](https://github.com/R0Y00/Xbox2Windows/issues/2618),
[#2274](https://github.com/R0Y00/Xbox2Windows/issues/2274).
Follows the compute `global_load_dword` lowering in
[#2715](https://github.com/R0Y00/Xbox2Windows/pull/2715) (ticket `C500-01/031`).

When a translated compute shader loads through a 64-bit root SRV pointer via
`global_load_dword`, `tools/startup/gfx2hlsl.py` emits `srv <i> buf root <param>`
in the compute manifest (`<key>.txt`) and lowers the read to
`ByteAddressBuffer r<i> : register(t<i>)` in the translated descriptor table.

The production startup compute manifest consumer in
`src/startup_stubs/d3d12_compute_dispatch.inl` and command-list tracking in
`src/startup_stubs/d3d12_xs_device.cpp` now consume `srv <i> buf root <param>`:

- Command-list slot 43 (`SetComputeRootShaderResourceView`) records the guest
address and a bound bit per compute root parameter on the open recording while
a compute root signature is bound. Binding a different compute root signature
in slot 33 (`SetComputeRootSignature`), closing the list (slot 9), or
resetting the list (slot 139) clears the recorded compute root SRV bindings;
re-binding the same compute root signature preserves them.
- `load_compute_translation` parses `srv %u buf root %d%n` (requiring an exact
whole-line match, `param >= 0`, and sequential `i == tr.srvs.size()`), and
rejects descriptor-word entry values (`stride`, `descword3`) that reference a
root SRV slot.
- At translated compute `Dispatch` (slot 14), `compute_root_srv_buffer` resolves
each `buf root <param>` input by verifying that `<param>` in the bound compute
root signature has `D3D12_ROOT_PARAMETER_TYPE_SRV`, was set by slot 43 since
the compute root signature was bound or reset, has a non-null 4-byte-aligned
guest address at a 16-byte-aligned offset (`D3D12_RAW_UAV_SRV_BYTE_ALIGNMENT`)
inside either a recording upload allocation (slot 148) or a live aliased
placed buffer in `COMMON` state (slot 133) with at least one whole dword
remaining, and does not alias an output UAV buffer. The resolved range is
bound as a raw `DXGI_FORMAT_R32_TYPELESS` `ByteAddressBuffer` view from that
offset to the end of its backing allocation or placed buffer (up to 16 MiB).

## Verification

`tests/startup_compute_root_srv_dispatch_check.py` (registered in
`tests/cmake/startup_compute_root_srv.cmake`) drives source-built gfx1013
compute `global_load_dword` programs (1-root-SRV and 2-root-SRV variants)
through slot 67, slot 33, slot 43, slot 37/39, slot 35, and slot 14 in
`startup_stream_consumer` with `X2W_D3D12_DEBUG=1` on both `warp` and `hardware`
(`AMD Radeon RX 9070 XT`):

- **Single and two-root-SRV dispatches:** 3x2x2 groups of 2x3x4 threads (288
active lanes, 320 total output words) load at offset `0` and `+128` bytes from
one or two distinct placed input buffers (`srv 0 buf root 2` and
`srv 1 buf root 3`), including re-binding the same compute root signature,
non-zero byte offsets into placed buffers (`+16` bytes on root SRV 0, `+32`
bytes on root SRV 1) and recording upload memory (`+16` bytes via slot 148),
and out-of-bounds loads (`v4 + 128u` past the bounded 1,280-byte backing)
returning `0` rather than reading adjacent `0xdeadbeef` / `0xfeedface`
neighbor words in the same 64 KiB page.
- **Refusal controls:** malformed manifest lines (`root -1`, trailing tokens,
non-sequential SRV index, `udsrc ... stride` on a root SRV), unset root SRV
slot (first or second), root SRV cleared by `Reset` or by binding a different
compute root signature, wrong root parameter kind (`D3D12_ROOT_PARAMETER_TYPE_CBV`
at `<param>`), null guest address, unaligned guest address (`addr + 2`),
non-16-byte-aligned interior resource offset (`addr + 4`), unmapped guest
address between placed buffers, non-aliased copied placed buffer, and
input-aliases-output-UAV hazard all refuse cleanly with their expected log
reasons and leave all 320 output sentinel words (`0xc5c5c5c5`) untouched.
- **Negative controls:** skipping `Dispatch`, swapping `srv 0 buf root 2` and
`srv 1 buf root 3` in the manifest, or dropping the `+ 128u` offset in the
translated HLSL changes all 288 active output words as predicted.

Reproduce on Windows:

```powershell
cmake --build --preset release --target d3d12_xs_shim startup_stream_consumer
ctest --preset release -j1 --no-tests=error -R '^startup_compute_root_srv_dispatch_(warp|hardware)$' -V
```

What remains a hypothesis: retail title compute dispatches that combine root SRV
pointers with unplaced host memory outside slot-133 placed buffers and slot-148
recording upload allocations are not yet modelled here and remain refused until
a title call site establishes their backing contract.
93 changes: 89 additions & 4 deletions src/startup_stubs/d3d12_compute_dispatch.inl
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// Translated compute programs at Dispatch (list slot 14). A device slot 67 pipeline is a placeholder that keeps
// 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
// <key>.cs.cso. Bound from the compute tables recorded by slot 35 and compute root SRVs recorded by slot 43: raw
// buffers (`srv <i> buf table <param> <byte>` and `srv <i> buf root <param>` inputs, `uav <i> table <param> <byte>`
// outputs) named by hardware buffer descriptors or 64-bit root SRV addresses, and 2D textures
// (`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
Expand All @@ -24,6 +25,7 @@ struct ComputeTranslation {
std::uint32_t at = 0, times = 0;
Source source = root_constant;
bool array = false;
bool root = false;
};
u64 key = 0;
int state = 0; // 0 not loaded, 1 ready, 2 unavailable
Expand Down Expand Up @@ -112,6 +114,11 @@ const char* load_compute_translation(ComputeTranslation& tr)
tr.uds.push_back({a, 0, false, false, -1, 0, 0, 0, source()});
else if (std::sscanf(line.c_str(), "srv %u buf table %d %d%n", &i, &a, &b, &used) == 3 && whole() && i == tr.srvs.size())
tr.srvs.push_back({a, b});
else if (std::sscanf(line.c_str(), "srv %u buf root %d%n", &i, &a, &used) == 2 && whole() && a >= 0 &&
i == tr.srvs.size()) {
tr.srvs.push_back({a, 0});
tr.srvs.back().root = true;
}
else if (std::sscanf(line.c_str(), "srv %u buf heap rootconst %d %d%n", &i, &a, &b, &used) == 3 && whole() &&
i == tr.srvs.size())
tr.srvs.push_back({a, b, false, true});
Expand Down Expand Up @@ -151,7 +158,7 @@ const char* load_compute_translation(ComputeTranslation& tr)
for (const ComputeTranslation::Slot& ud : tr.uds) {
const auto& slots = ud.source >= ComputeTranslation::output_word3 ? tr.uavs : tr.srvs;
if (ud.source != ComputeTranslation::root_constant &&
(ud.a < 0 || static_cast<std::size_t>(ud.a) >= slots.size() || slots[ud.a].texture))
(ud.a < 0 || static_cast<std::size_t>(ud.a) >= slots.size() || slots[ud.a].texture || slots[ud.a].root))
return "an entry value names a descriptor that is not a buffer input or output";
}
x2w_startup::ComPtr<ID3DBlob> code;
Expand All @@ -171,6 +178,74 @@ const char* load_compute_translation(ComputeTranslation& tr)
return nullptr;
}

// Resolve a compute root SRV (`srv <i> buf root <param>`) from the guest address recorded by slot 43 for <param>
// to the end of its backing upload allocation or aliased placed buffer (up to the 16 MiB compute raw buffer limit).
IndirectBufferView compute_root_srv_buffer(Wrapper* self, const Pass& pass, int param, std::uint32_t (&words)[8],
const char** why)
{
const auto refuse = [why](const char* reason) {
if (why) *why = reason;
return IndirectBufferView{};
};
if (why) *why = nullptr;
const auto* root_params = pass.compute_root_wrapper ? root_info(pass.compute_root_wrapper) : nullptr;
if (!root_params || param < 0 || static_cast<std::size_t>(param) >= root_params->size() ||
static_cast<std::size_t>(param) >= pass.compute_root_srvs.size() ||
(*root_params)[param].type != D3D12_ROOT_PARAMETER_TYPE_SRV)
return refuse("a compute root input is not a shader resource view parameter");
if (!(pass.compute_root_srv_set & (u64{1} << param)))
return refuse("a compute root SRV was not set since the compute root");
const u64 address = pass.compute_root_srvs[param];
words[0] = static_cast<std::uint32_t>(address);
words[1] = static_cast<std::uint32_t>(address >> 32);
if (!address) return refuse("a compute root SRV has a null address");
if (address % 4) return refuse("a compute root SRV address is not 4-byte aligned");
constexpr u64 limit = 16u << 20;
if (const auto allocation = list_upload_tail(self, address); allocation.resource) {
if (allocation.offset % D3D12_RAW_UAV_SRV_BYTE_ALIGNMENT)
return refuse("a compute root SRV range is not 16-byte aligned in its resource");
const u64 bytes = (std::min)(allocation.bytes, limit) & ~u64{3};
if (bytes < 4)
return refuse("a compute root SRV has fewer than 4 aligned bytes in its upload allocation");
words[2] = static_cast<std::uint32_t>(bytes);
return {allocation.resource, allocation.offset, bytes, 0, true};
}
Wrapper* device = self->own().list_device;
const auto first = placed_buffer_range(device, address, 4);
if (first.reject) return refuse("a compute root SRV crosses the end of a placed buffer");
if (!first.owner) return refuse("a compute root SRV is in title memory outside any placed buffer");
if (!InterlockedCompareExchange(&first.owner->own().refs, 0, 0))
return refuse("a compute root SRV's resource is released");
const auto* info = resource_info(first.owner);
if (!info || info->initial_state != D3D12_RESOURCE_STATE_COMMON)
return refuse("a compute root SRV's resource was not created in the common state");
const u64 base = reinterpret_cast<u64>(first.owner->own().title_memory);
if (!base || address < base || address - base >= info->bytes)
return refuse("a compute root SRV range is outside its desktop buffer");
const u64 offset = address - base;
if (offset % D3D12_RAW_UAV_SRV_BYTE_ALIGNMENT)
return refuse("a compute root SRV range is not 16-byte aligned in its resource");
const u64 bytes = (std::min)(info->bytes - offset, limit) & ~u64{3};
if (bytes < 4)
return refuse("a compute root SRV has fewer than 4 aligned bytes in its placed buffer");
const auto placed = placed_buffer_range(device, address, bytes);
if (placed.reject || placed.owner != first.owner)
return refuse("a compute root SRV crosses the end of a placed buffer");
auto* native = static_cast<ID3D12Resource*>(placed.owner->own().real);
if (!placed.alias && (!native || !list_copied_gpu_range(self, native, offset, bytes)))
return refuse("a compute root SRV is in a placed buffer that does not alias title memory");
x2w_startup::ComPtr<ID3D12Device> native_device;
if (!native || FAILED(native->GetDevice(IID_PPV_ARGS(native_device.GetAddressOf()))) ||
native_device.Get() != device_of(device))
return refuse("a compute root SRV's resource is on another device");
const auto desc = native->GetDesc();
if (desc.Dimension != D3D12_RESOURCE_DIMENSION_BUFFER || (desc.Flags & D3D12_RESOURCE_FLAG_DENY_SHADER_RESOURCE) ||
offset > desc.Width || bytes > desc.Width - offset)
return refuse("a compute root SRV range is outside its desktop buffer");
words[2] = static_cast<std::uint32_t>(bytes);
return {native, offset, bytes, 0, false};
}

// 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)
{
Expand Down Expand Up @@ -221,12 +296,22 @@ const char* record_translated_dispatch(Wrapper* self, const TranslatedDispatch&
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)) {
if (dispatch.signature && (slot.heap || slot.texture || slot.root)) {
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 (!output && slot.root) {
const IndirectBufferView view = compute_root_srv_buffer(self, pass, slot.a, words, &reason);
if (reason && !tr->first_refusal)
log("x2w-draw compute %016llx first refused input %zu (root %d), address 0x%llx", key, i, slot.a,
u64{words[0]} | (u64{words[1]} << 32));
views[i] = {view.resource, view.offset, view.bytes, !view.upload};
views[i].stride = view.stride;
std::memcpy(views[i].words, words, sizeof(views[i].words));
continue;
}
if (slot.word_param >= 0) {
// The index word is read now from the recording's upload memory, where the title wrote it (no GPU command
// writes there), at the dword-aligned 32-bit byte offset the program reads it at.
Expand Down
33 changes: 32 additions & 1 deletion src/startup_stubs/d3d12_xs_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1596,6 +1596,25 @@ x2w_upload::Allocation list_upload_range(Wrapper* self, UINT64 address, UINT64 b
ReleaseSRWLockShared(&allocator_lock);
return allocation;
}
x2w_upload::Allocation list_upload_tail(Wrapper* self, UINT64 address)
{
x2w_upload::Allocation allocation{};
AcquireSRWLockShared(&allocator_lock);
const auto& state = self->own();
if (state.list_open && state.list_uploads) {
for (auto it = state.list_uploads->allocations.rbegin(); it != state.list_uploads->allocations.rend(); ++it) {
if (address < it->cpu) continue;
const UINT64 offset = address - it->cpu;
if (offset < it->bytes) {
allocation = {static_cast<uintptr_t>(address), it->gpu + offset, it->bytes - offset, it->resource,
it->offset + offset};
break;
}
}
}
ReleaseSRWLockShared(&allocator_lock);
return allocation;
}
UINT64 list_upload_gpu(Wrapper* self, UINT64 address, UINT64 bytes) { return list_upload_range(self, address, bytes).gpu; }

// Fixture lookup. The full range must belong to this list's open recording.
Expand Down Expand Up @@ -2601,6 +2620,8 @@ struct Pass {
std::uint32_t constants[16][8] = {};
unsigned char constant_words_bound[16] = {};
x2w_root_constants::Store compute_constants; // compute root constants (slots 37 and 39)
std::array<u64, 64> compute_root_srvs{}; // title address of each compute root SRV (slot 43)
u64 compute_root_srv_set = 0; // bit per compute root parameter set by slot 43 since slot 33 / Reset
Wrapper* compute_root_wrapper = nullptr; // compute root signature last set by slot 33
D3D12_VERTEX_BUFFER_VIEW vb[8] = {};
PlacedVertexView placed_vb[8] = {};
Expand Down Expand Up @@ -2730,6 +2751,8 @@ void retire_drop_recording(Wrapper* list)
void reset_list_dynamic_state(Wrapper* self)
{
if (!self->own().pass) return;
pass_of(self).compute_root_srvs.fill(0);
pass_of(self).compute_root_srv_set = 0;
pass_of(self).stencil_reference = 0;
pass_of(self).viewport_set = false;
pass_of(self).scissor_set = false;
Expand Down Expand Up @@ -2837,7 +2860,11 @@ void __stdcall list_compute_root(Wrapper* self, void* root)
Wrapper* w = as_wrapper(root);
Pass& pass = pass_of(self);
// A different root signature invalidates the title's cached compute root arguments.
if (pass.compute_root_wrapper != w) pass.compute_constants.clear();
if (pass.compute_root_wrapper != w) {
pass.compute_constants.clear();
pass.compute_root_srvs.fill(0);
pass.compute_root_srv_set = 0;
}
pass.compute_root_wrapper = w;
const bool real = w && w->own().kind == root_signature_kind && w->own().real && !root_heap_flags(w);
pass.compute_root = real ? static_cast<ID3D12RootSignature*>(w->own().real) : nullptr;
Expand Down Expand Up @@ -3864,6 +3891,10 @@ void __stdcall list_compute_upload_srv(Wrapper* self, UINT parameter, UINT64 add
{
auto* list = recording(self);
auto& pass = pass_of(self);
if (list && pass.compute_root_wrapper && parameter < pass.compute_root_srvs.size()) {
pass.compute_root_srvs[parameter] = address;
pass.compute_root_srv_set |= u64{1} << parameter;
}
const auto* params = pass.compute_params;
if (!list || !params || parameter >= params->size() || parameter >= pass.desktop_compute_upload_srvs.size() ||
(*params)[parameter].type != D3D12_ROOT_PARAMETER_TYPE_SRV) return;
Expand Down
8 changes: 8 additions & 0 deletions tests/cmake/startup_compute_root_srv.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_root_srv_dispatch_${adapter} COMMAND "${INTAKE_PYTHON}" -B
"${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_compute_root_srv_dispatch_check.py"
"$<TARGET_FILE:startup_stream_consumer>" "${DXC_EXECUTABLE}" ${adapter})
set_tests_properties(startup_compute_root_srv_dispatch_${adapter} PROPERTIES TIMEOUT 120 SKIP_RETURN_CODE 77)
endforeach()
endif()
Loading
Loading