Skip to content
Closed
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
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4831,7 +4831,8 @@ if(EXISTS "${INTAKE_PYTHON}")
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)
# gate-ring-reuse: once those submits have completed, the ring must accept new uploads 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: 3 additions & 3 deletions docs/startup-runtime.md
Original file line number Diff line number Diff line change
Expand Up @@ -3358,9 +3358,9 @@ uncovered prefix, and right before
`OpenExistingHeapFromAddress` opens the heap). Anything else gets a
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.
chunks from 16 MiB up to the 256 MiB total cap. Completed chunks whose queue
submissions have finished on the GPU are reused; storage still in flight is
never reused, and exhaustion refuses new reservations.
`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
41 changes: 22 additions & 19 deletions src/startup_stubs/d3d12_draw.inl
Original file line number Diff line number Diff line change
Expand Up @@ -1226,13 +1226,14 @@ struct FrameCopy {
u64 address = 0;
u64 offset = 0;
ID3D12Resource* resource = nullptr;
u64 generation = 0;
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(u64 address, std::size_t bytes, u64* offset, ID3D12Resource** resource, ID3D12CommandList* list = nullptr)
{
if (frame_copies_frame != frame_number) {
frame_copies().clear();
Expand All @@ -1243,25 +1244,27 @@ 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;
if (validate_and_note_upload_ring_use(found->second.resource, found->second.generation, list)) {
*offset = found->second.offset;
if (resource) *resource = found->second.resource;
return true;
}
}
u64 fresh = 0;
ID3D12Resource* chunk = nullptr;
auto* ring = reserve_upload_ring(bytes, &fresh, &chunk);
if (!ring || !chunk) return false;
const auto reservation = reserve_upload_ring(bytes, list);
if (!reservation.mapped || !reservation.resource) return false;
const u64 fresh = reservation.offset;
auto* chunk = reservation.resource;
// 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, fresh, chunk, reservation.generation, 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(reservation.mapped, source, bytes);
if (known) {
static LONG refreshed;
if (first_calls(refreshed, 4))
Expand Down Expand Up @@ -1436,7 +1439,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(static_cast<std::size_t>(bytes), &offset, &chunk, list);
if (!rows || !chunk) {
state.done = false;
return false;
Expand All @@ -1458,7 +1461,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(static_cast<std::size_t>(bytes), &offset, &chunk, list);
if (!rows || !chunk) {
state.done = false;
return true;
Expand All @@ -1472,7 +1475,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(static_cast<std::size_t>(bytes), &offset, &chunk, list);
if (!rows || !chunk) {
state.done = false;
return true;
Expand All @@ -1488,7 +1491,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(static_cast<std::size_t>(bytes), &offset, &chunk, list);
if (!rows || !chunk) {
state.done = false;
return true;
Expand All @@ -1497,7 +1500,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(reinterpret_cast<u64>(memory) + stride * s + fp.Offset, static_cast<std::size_t>(bytes), &offset, &chunk, list) || !chunk) {
state.done = false;
return true;
}
Expand Down Expand Up @@ -2585,7 +2588,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(padded, &offset, &chunk, list) : nullptr;
if (target) {
std::memcpy(target, reinterpret_cast<const void*>(address), bytes);
std::memset(target + bytes, 0, padded - bytes);
Expand All @@ -2600,7 +2603,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(address, bytes, &offset, &chunk, list);
// 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 +2621,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(address, bytes, &offset, &chunk, list);
}
buffer_view(dest, have ? chunk : nullptr, offset, bytes);
count_buffer(have);
Expand Down Expand Up @@ -2723,7 +2726,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(index_address, index_total, &offset, &index_chunk, list) && 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