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
10 changes: 10 additions & 0 deletions docs/experiments.md
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,16 @@ The source is a same-device desktop resource with no title shadow, a unique alia
or an already covered range. Later overlapping copies invalidate earlier coverage.
The recording discards this proof at Close, so another recording needs its own writer.

`X2W_TRANSLATED_TITLE_INPUT_SNAPSHOT=1` lets a translated compute Dispatch read an input that lies in a placed buffer which does
not alias title memory. That buffer's desktop copy is a committed default buffer that holds zeros, so the bytes the title wrote
exist only in title memory. The input resolves in this order: a validated GPU copy of the current recording (the proof above,
which needs `X2W_TRANSLATED_INDIRECT_COPY_RANGES=1`), else a snapshot of the range (at most 16 MiB) taken from title memory
into the snapshot pool when the Dispatch is recorded, else the refusal `an input is in a placed buffer that does not alias title
memory`. A guest write after the recording is not seen, and data a GPU pass wrote into such a buffer is not either. The first
use per pipeline key logs `x2w-draw compute <key> input <n> (...) comes from a GPU copy this list made|a snapshot of title
memory`, and the render totals line counts both paths. Off by default. To turn it on for a run, set `$env:X2W_TRANSLATED_TITLE_INPUT_SNAPSHOT = "1"` in the PowerShell session before
starting `tools/startup/run.ps1`, which passes the session's environment on to the run. The snapshots have a pool of their own (128 MiB, chunks of 16 MiB, built like the shared upload ring: a chunk is reused once no recording can submit its bytes and every queue that used it has finished), so draw and texture uploads cannot starve them and a long run is not stopped by a full pool; a reservation it cannot serve logs `x2w-draw snapshot pool reservation refused: no unused capacity`. `startup_compute_dispatch_input` covers it with the `dispatch-input-snapshot` case and, for the recycling, `dispatch-input-snapshot-long` (48 frames of an 8 MiB snapshot, 384 MiB in all).

The source-built fixture reproduces a 68-byte request shape with source offset `0xbfffa0`.
Its compute writer fills the source with contrasting colours. The consumer reads red
from the second DWORD in the destination while the title-side green sentinel stays
Expand Down
14 changes: 13 additions & 1 deletion src/startup_stubs/d3d12_compute_dispatch.inl
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ struct ComputeTranslation {
const char* unmodelled = nullptr; // why a manifest that exists cannot be used
u64 dispatches = 0, translated = 0; // census: Dispatch calls with this key bound, and those recorded
const char* first_refusal = nullptr; // reason of the first refused call
bool input_path_logged = false; // the first time a placed input came from a GPU copy or a title snapshot
std::uint32_t refusal_words[4] = {}; // the table descriptor that reason was about, if any; a view token's
// last word is replaced by its view format | dimension << 16
~ComputeTranslation()
Expand Down Expand Up @@ -83,6 +84,7 @@ unsigned float_output_channels(DXGI_FORMAT format)
}
std::unordered_map<u64, ComputeTranslation>& compute_translations() { static std::unordered_map<u64, ComputeTranslation> m; return m; }
LONG compute_dispatches_translated, compute_dispatches_refused, compute_dispatches_untranslated, compute_dispatches_unmodelled;
LONG compute_inputs_gpu_copied, compute_inputs_snapshot, compute_inputs_snapshot_kib; // inputs by path: this list's GPU copy, a title snapshot

// The root signature and pipeline of a translation whose entry values and views are filled in. Returns why it is
// unavailable, or null when it is ready.
Expand Down Expand Up @@ -591,7 +593,17 @@ const char* record_translated_dispatch(Wrapper* self, const TranslatedDispatch&
}
if (!output) {
// Each condition of a complete GPU-backed buffer range has its own reason.
const IndirectBufferView view = indirect_table_buffer(self, words, false, &reason, 16u << 20);
const IndirectBufferView view = indirect_table_buffer(self, words, false, &reason, 16u << 20, true);
if (view.source == 1) InterlockedIncrement(&compute_inputs_gpu_copied);
if (view.source == 2) {
InterlockedIncrement(&compute_inputs_snapshot);
InterlockedExchangeAdd(&compute_inputs_snapshot_kib, static_cast<LONG>((view.bytes + 1023) / 1024));
}
if (view.source && !tr->input_path_logged) {
tr->input_path_logged = true;
log("x2w-draw compute %016llx input %zu (%s %d %d) comes from %s (%llu bytes)", key, i, slot.heap ? "heap" : "table", slot.a, slot.b,
view.source == 1 ? "a GPU copy this list made" : view.source == 3 ? "zeros for a null descriptor" : "a snapshot of title memory", view.bytes);
}
if (view.resource && view.offset % D3D12_RAW_UAV_SRV_BYTE_ALIGNMENT) reason = "an input buffer range is not 16-byte aligned in its resource";
if (reason && !tr->first_refusal) // once per key: which input, and where its descriptor was read
log("x2w-draw compute %016llx first refused input %zu (%s %d %d), descriptor at 0x%llx", key, i,
Expand Down
45 changes: 41 additions & 4 deletions src/startup_stubs/d3d12_draw.inl
Original file line number Diff line number Diff line change
Expand Up @@ -2284,14 +2284,24 @@ struct IndirectBufferView {
u64 offset = 0, bytes = 0;
UINT stride = 0;
bool upload = false;
int source = 0; // 0 aliased or any other backing, 1 a GPU copy this list made (X2W_TRANSLATED_INDIRECT_COPY_RANGES), 2 a snapshot of title memory, 3 zeros for a null descriptor
};

// X2W_TRANSLATED_TITLE_INPUT_SNAPSHOT=1: a translated compute input in a placed buffer that does not alias title memory
// (a committed default buffer that holds zeros) is read from title memory, snapshotted into its own recycled pool.
bool title_input_snapshot_enabled()
{
static const bool enabled = x2w_local::env_on("X2W_TRANSLATED_TITLE_INPUT_SNAPSHOT");
return enabled;
}

// For indirect draws, bind the complete descriptor range on a known GPU backing. There is no
// fallback to copying an unrelated or stale title allocation into the draw upload ring.
// why, when given, receives the condition an empty result failed on.
IndirectBufferView indirect_table_buffer(Wrapper* self, const std::uint32_t* words, bool allow_address_alias = false,
const char** why = nullptr,
u64 limit = u64{4} << D3D12_REQ_BUFFER_RESOURCE_TEXEL_COUNT_2_TO_EXP)
u64 limit = u64{4} << D3D12_REQ_BUFFER_RESOURCE_TEXEL_COUNT_2_TO_EXP,
bool allow_title_snapshot = false)
{
const auto refuse = [why](const char* reason) {
if (why) *why = reason;
Expand Down Expand Up @@ -2349,12 +2359,23 @@ IndirectBufferView indirect_table_buffer(Wrapper* self, const std::uint32_t* wor
stride = (words[1] >> 16) & 0x3fff;
bytes = u64{stride ? stride : 1} * words[2];
}
// A compute input whose four descriptor words are all zero is an unbound (null) buffer: the hardware reads zeros from
// it, so the dispatch gets 16 bytes of zeros instead of being refused. Any other empty range is still refused.
if (!bytes && allow_title_snapshot && !words[0] && !words[1] && !words[2] && !words[3]) {
u64 zero_offset = 0;
ID3D12Resource* zero_ring = nullptr;
unsigned char* zero_bytes = reserve_upload_ring(self, 16, &zero_offset, &zero_ring, true);
if (!zero_bytes || !zero_ring) return refuse("a null input could not get its zero bytes");
std::memset(zero_bytes, 0, 16);
return {zero_ring, zero_offset, 16, 0, true, 3};
}
if (!bytes) return refuse("an input buffer descriptor has zero records");
if (bytes > limit || bytes % 4) return refuse(limit == (16u << 20)
? "an input buffer range is over 16 MiB or not whole dwords"
: "an input buffer range exceeds the D3D12 raw SRV limit or is not whole dwords");
ID3D12Resource* native = nullptr;
bool upload = false;
int input_source = 0;
Wrapper* device = self->own().list_device;
if (address) {
if (const auto allocation = list_upload_range(self, address, bytes); allocation.resource) {
Expand All @@ -2375,8 +2396,24 @@ IndirectBufferView indirect_table_buffer(Wrapper* self, const std::uint32_t* wor
owner = placed.owner;
native = static_cast<ID3D12Resource*>(owner->own().real);
offset = address - reinterpret_cast<u64>(owner->own().title_memory);
if (!placed.alias && (!native || !list_copied_gpu_range(self, native, offset, bytes)))
return refuse("an input is in a placed buffer that does not alias title memory");
if (!placed.alias) {
if (native && list_copied_gpu_range(self, native, offset, bytes)) {
input_source = 1; // a GPU copy of this list filled the range: the GPU buffer is the truth
} else if (allow_title_snapshot && title_input_snapshot_enabled() && bytes <= (u64{16} << 20)) {
// The buffer is a committed default buffer that holds zeros (it does not alias title memory), so the
// bytes the title wrote are only in title memory: snapshot the range into the snapshot pool now. A later
// guest write is not seen (as title_backed_source for copies); data a GPU pass wrote into this buffer is not either.
u64 ring_offset = 0;
ID3D12Resource* ring = nullptr;
if (readable_window(address, static_cast<std::size_t>(bytes)) < bytes)
return refuse("an input range in title memory is not readable");
if (!ring_copy(self, address, static_cast<std::size_t>(bytes), &ring_offset, &ring, true) || !ring)
return refuse("an input range could not be copied from title memory");
return {ring, ring_offset, bytes, stride, true, 2};
} else {
return refuse("an input is in a placed buffer that does not alias title memory");
}
}
} else if (own_memory) {
native = static_cast<ID3D12Resource*>(owner->own().real);
} else if (owner && !owner->own().title_memory) {
Expand All @@ -2400,7 +2437,7 @@ IndirectBufferView indirect_table_buffer(Wrapper* self, const std::uint32_t* wor
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("an input range is outside its desktop buffer");
return {native, offset, bytes, stride, upload};
return {native, offset, bytes, stride, upload, input_source};
}

struct FormattedBufferDescriptor {
Expand Down
Loading
Loading