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
6 changes: 4 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4830,8 +4830,10 @@ if(EXISTS "${INTAKE_PYTHON}")
"$<TARGET_FILE:startup_stream_consumer>" "${backend}")
set_tests_properties(startup_address_console_${backend} PROPERTIES TIMEOUT 180 SKIP_RETURN_CODE 77)
# Uploads of by-address textures recorded while the queue waits on a fence keep their bytes until the
# queue has run them. The ring control fills them from the shared upload ring and must lose the first.
foreach(gate IN ITEMS gate gate-ring)
# queue has run them. The ring control fills them from the shared upload ring, which must refuse the uploads
# past its 256 MiB cap while the queue is held. gate-ring-reuse: once those submits have completed, the ring
# must accept a new upload again.
foreach(gate IN ITEMS gate gate-ring gate-ring-reuse)
string(REPLACE "-" "_" gate_name "${gate}")
add_test(NAME startup_address_console_${gate_name}_${backend} COMMAND "${CMAKE_COMMAND}" -E env "X2W_FXC=${STARTUP_FXC}"
"${INTAKE_PYTHON}" -B "${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_address_console_check.py"
Expand Down
6 changes: 6 additions & 0 deletions docs/experiments.md
Original file line number Diff line number Diff line change
Expand Up @@ -16335,6 +16335,12 @@ instead. It requires a reservation refusal after exhaustion, payload 0
preserved on the left, the null view on the right, and no retained-upload
release line. The ring no longer reuses live storage. The revised Windows
readback control remains unmeasured on macOS.
`startup_address_console_gate_ring_reuse_{warp,hardware}` runs the same
sequence, opens the gate, waits until every submit has completed and then
draws payload 18 from the ring over the right half. That reservation must not
be refused ([#2837](https://github.com/R0Y00/Xbox2Windows/issues/2837)). With
chunk reuse disabled, as before the fix, it fails while `gate_ring` still
passes.

The equations are checked against addrlib and against the three existing layout headers, not against
console memory. Which layouts a given console build writes for which textures is not established by
Expand Down
11 changes: 9 additions & 2 deletions docs/startup-runtime.md
Original file line number Diff line number Diff line change
Expand Up @@ -3353,8 +3353,15 @@ uncovered prefix, and right before
committed default buffer that does not alias the title memory, holds zeros and is reported as
success (`not aliased` in the log). `reserve_upload_ring` grows in demand
chunks from 16 MiB up to the 256 MiB total cap. A reservation owns its mapped
bytes for the process; the allocator may use an older unused tail but
refuses exhaustion or allocation failure rather than resetting live storage.
bytes until its chunk is recycled. The allocator tries the active chunk, the
remaining capacity and older unused tails first. Only then does it recycle a
chunk, and only one that no command-list recording holds (a closed list keeps
its hold until Reset, Release or a failed Close, because it can be submitted
again) and whose last submit on every queue has completed. A failed fence
signal or a removed device keeps the chunk busy. When no chunk qualifies, the
reservation is refused; live storage is never reset. Recycling bumps the
chunk's generation, and a cached frame copy from an older generation is not
reused.
`upload_reservation` exercises the shared cursor and returned-pointer contract
without D3D12; on non-Windows hosts run
`c++ -std=c++17 -Isrc tests/upload_reservation_check.cpp -o out/upload_reservation_check && out/upload_reservation_check`.
Expand Down
56 changes: 31 additions & 25 deletions src/startup_stubs/d3d12_draw.inl
Original file line number Diff line number Diff line change
Expand Up @@ -1071,8 +1071,7 @@ std::unordered_map<ID3D12Resource*, TextureUpload>& texture_uploads() { static s
u64 address_upload_revision;

// A texture published in a newly supported console layout is not filled through the shared upload ring.
// The ring is reused without waiting for any queue, and a queue held behind a fence can execute a
// recorded copy long after later uploads have passed over its source bytes. Each such upload owns
// The bounded ring can fill while a queue waits on a fence. Each such upload owns
// its upload buffer instead. The buffer and the texture it fills stay referenced by the recording,
// and by every submit of that recording until the queue it went to has completed the submit, as
// for the sampler heaps above.
Expand Down Expand Up @@ -1224,15 +1223,14 @@ bool prepare_draw_sampler_heap(Pass& pass, ID3D12GraphicsCommandList* list, bool
// upload ring is write-combined memory that is slow to read, so the comparison is against this copy, not the ring.
struct FrameCopy {
u64 address = 0;
u64 offset = 0;
ID3D12Resource* resource = nullptr;
UploadReservation slice;
std::vector<unsigned char> content;
};
std::unordered_map<u64, FrameCopy>& frame_copies() { static std::unordered_map<u64, FrameCopy> m; return m; }
LONG frame_copies_frame = -1;

// Callers have just probed that [address, address + bytes) is readable (readable_window), and draw_lock is held.
bool frame_copy(u64 address, std::size_t bytes, u64* offset, ID3D12Resource** resource)
bool frame_copy(Wrapper* self, u64 address, std::size_t bytes, u64* offset, ID3D12Resource** resource)
{
if (frame_copies_frame != frame_number) {
frame_copies().clear();
Expand All @@ -1243,32 +1241,40 @@ bool frame_copy(u64 address, std::size_t bytes, u64* offset, ID3D12Resource** re
const auto found = frame_copies().find(key);
const bool known = found != frame_copies().end() && found->second.address == address && found->second.content.size() == bytes;
if (known && std::memcmp(found->second.content.data(), title, bytes) == 0) {
*offset = found->second.offset;
if (resource) *resource = found->second.resource;
return true;
const auto& slice = found->second.slice;
AcquireSRWLockExclusive(&allocator_lock);
for (unsigned i = 0; i < upload_chunks.size(); ++i) {
auto& chunk = upload_chunks[i];
if (chunk.resource == slice.resource && chunk.generation == slice.generation) {
chunk.hold(self->own().upload_chunks, i);
ReleaseSRWLockExclusive(&allocator_lock);
*offset = slice.offset;
if (resource) *resource = slice.resource;
return true;
}
}
ReleaseSRWLockExclusive(&allocator_lock);
}
u64 fresh = 0;
ID3D12Resource* chunk = nullptr;
auto* ring = reserve_upload_ring(bytes, &fresh, &chunk);
if (!ring || !chunk) return false;
const auto slice = reserve_upload_ring(self, bytes);
if (!slice.mapped) return false;
// Cache insertion and snapshot allocation are optional. Fill the ring directly if they fail.
const unsigned char* source = title;
try {
std::vector<unsigned char> content(title, title + bytes);
// Use the stored snapshot to fill the ring so the GPU bytes and subsequent comparisons agree.
auto entry = frame_copies().insert_or_assign(key, FrameCopy{address, fresh, chunk, std::move(content)});
auto entry = frame_copies().insert_or_assign(key, FrameCopy{address, slice, std::move(content)});
source = entry.first->second.content.data();
} catch (const std::bad_alloc&) {
// The ring slice is already reserved. The draw can proceed without another CPU allocation.
}
std::memcpy(ring, source, bytes);
std::memcpy(slice.mapped, source, bytes);
if (known) {
static LONG refreshed;
if (first_calls(refreshed, 4))
log("x2w-draw frame copy of 0x%llx (%zu bytes) refreshed: the title changed it since an earlier draw of this frame", address, bytes);
}
*offset = fresh;
if (resource) *resource = chunk;
*offset = slice.offset;
if (resource) *resource = slice.resource;
return true;
}

Expand Down Expand Up @@ -1436,7 +1442,7 @@ bool upload_texture(Wrapper* self, Wrapper* resource, ID3D12Resource* texture)
if (owned.cpu) {
owned.cursor += (bytes + placement - 1) & ~(placement - 1);
} else {
rows = reserve_upload_ring(static_cast<std::size_t>(bytes), &offset, &chunk);
rows = reserve_upload_ring(self, static_cast<std::size_t>(bytes), &offset, &chunk);
if (!rows || !chunk) {
state.done = false;
return false;
Expand All @@ -1458,7 +1464,7 @@ bool upload_texture(Wrapper* self, Wrapper* resource, ID3D12Resource* texture)
const u64 bytes = plan.bytes(i);
u64 offset = 0;
ID3D12Resource* chunk = nullptr;
auto* rows = reserve_upload_ring(static_cast<std::size_t>(bytes), &offset, &chunk);
auto* rows = reserve_upload_ring(self, static_cast<std::size_t>(bytes), &offset, &chunk);
if (!rows || !chunk) {
state.done = false;
return true;
Expand All @@ -1472,7 +1478,7 @@ bool upload_texture(Wrapper* self, Wrapper* resource, ID3D12Resource* texture)
u64 offset = 0;
const auto bytes = plan.bytes(0);
ID3D12Resource* chunk = nullptr;
auto* rows = reserve_upload_ring(static_cast<std::size_t>(bytes), &offset, &chunk);
auto* rows = reserve_upload_ring(self, static_cast<std::size_t>(bytes), &offset, &chunk);
if (!rows || !chunk) {
state.done = false;
return true;
Expand All @@ -1488,7 +1494,7 @@ bool upload_texture(Wrapper* self, Wrapper* resource, ID3D12Resource* texture)
u64 offset = 0;
ID3D12Resource* chunk = nullptr;
if (is_compact) {
auto* const rows = reserve_upload_ring(static_cast<std::size_t>(bytes), &offset, &chunk);
auto* const rows = reserve_upload_ring(self, static_cast<std::size_t>(bytes), &offset, &chunk);
if (!rows || !chunk) {
state.done = false;
return true;
Expand All @@ -1497,7 +1503,7 @@ bool upload_texture(Wrapper* self, Wrapper* resource, ID3D12Resource* texture)
for (u64 y = 0; y < level.rows; ++y)
std::memcpy(rows + y * fp.Footprint.RowPitch, memory + level.offset + y * level.row_bytes,
static_cast<std::size_t>(level.row_bytes));
} else if (!ring_copy(reinterpret_cast<u64>(memory) + stride * s + fp.Offset, static_cast<std::size_t>(bytes), &offset, &chunk) || !chunk) {
} else if (!ring_copy(self, reinterpret_cast<u64>(memory) + stride * s + fp.Offset, static_cast<std::size_t>(bytes), &offset, &chunk) || !chunk) {
state.done = false;
return true;
}
Expand Down Expand Up @@ -2585,7 +2591,7 @@ bool draw_translated(Wrapper* self, bool indexed, UINT count, UINT instances, UI
// to a DWORD, so initialise its final padding without reading the guest.
const auto padded = (bytes + 3) & ~std::size_t{3};
ID3D12Resource* chunk = nullptr;
auto* target = readable_window(address, bytes) >= bytes ? reserve_upload_ring(padded, &offset, &chunk) : nullptr;
auto* target = readable_window(address, bytes) >= bytes ? reserve_upload_ring(self, padded, &offset, &chunk) : nullptr;
if (target) {
std::memcpy(target, reinterpret_cast<const void*>(address), bytes);
std::memset(target + bytes, 0, padded - bytes);
Expand All @@ -2600,7 +2606,7 @@ bool draw_translated(Wrapper* self, bool indexed, UINT count, UINT instances, UI
continue;
}
ID3D12Resource* chunk = nullptr;
bool have = address && bytes && readable_window(address, bytes) >= bytes && frame_copy(address, bytes, &offset, &chunk);
bool have = address && bytes && readable_window(address, bytes) >= bytes && frame_copy(self, address, bytes, &offset, &chunk);
// Experiment (X2W_DRAW_NULL_IDENTITY=1): an empty buffer slot reads as repeated identity
// matrices instead of zeros. The start-up screen's last pass takes its world matrix from a
// slot the title leaves empty here; why is not known yet.
Expand All @@ -2618,7 +2624,7 @@ bool draw_translated(Wrapper* self, bool indexed, UINT count, UINT instances, UI
for (int k = 0; k < 64; ++k) identity[k] = (k % 16) % 5 == 0 ? 1.0f : 0.0f;
address = reinterpret_cast<u64>(identity);
bytes = sizeof(identity);
have = frame_copy(address, bytes, &offset, &chunk);
have = frame_copy(self, address, bytes, &offset, &chunk);
}
buffer_view(dest, have ? chunk : nullptr, offset, bytes);
count_buffer(have);
Expand Down Expand Up @@ -2723,7 +2729,7 @@ bool draw_translated(Wrapper* self, bool indexed, UINT count, UINT instances, UI
auto view = live_indices;
if (live_index_resource) {
require_state(self, live_index_resource, D3D12_RESOURCE_STATE_INDEX_BUFFER);
} else if (indices_ok && frame_copy(index_address, index_total, &offset, &index_chunk) && index_chunk) {
} else if (indices_ok && frame_copy(self, index_address, index_total, &offset, &index_chunk) && index_chunk) {
view = {index_chunk->GetGPUVirtualAddress() + offset, static_cast<UINT>(index_total),
index_bytes == 2 ? DXGI_FORMAT_R16_UINT : DXGI_FORMAT_R32_UINT};
}
Expand Down
Loading
Loading