From 166b62a7142d7f4b82034deb8afec36dc13738aa Mon Sep 17 00:00:00 2001 From: c1 Date: Thu, 26 Nov 2026 15:40:55 +0100 Subject: [PATCH 1/2] startup: count a failed buffer copy as the null cause ring_full and refuse the draw; report upload ring statistics --- src/startup_stubs/d3d12_draw.inl | 25 ++++++++++++-- src/startup_stubs/d3d12_xs_device.cpp | 19 +++++++++-- src/startup_stubs/null_binding_cause.h | 7 ++-- tests/startup_null_cause_labels_check.cpp | 40 ++++++++++++++--------- 4 files changed, 69 insertions(+), 22 deletions(-) diff --git a/src/startup_stubs/d3d12_draw.inl b/src/startup_stubs/d3d12_draw.inl index a67724e605..1f8ac9ecc8 100644 --- a/src/startup_stubs/d3d12_draw.inl +++ b/src/startup_stubs/d3d12_draw.inl @@ -2585,7 +2585,17 @@ 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; + const bool readable_range = readable_window(address, bytes) >= bytes; + auto* target = readable_range ? reserve_upload_ring(padded, &offset, &chunk) : nullptr; + if (readable_range && !target) { + // The guest memory is there; only the copy failed. A null binding would read zeros as if the title had bound nothing. + static LONG copy_refused; + if (first_calls(copy_refused, 6)) + log("x2w-draw %016llx srv %u: buffer copy failed (upload ring), draw refused", tr->key, i); + count_null_binding(x2w_local::NullCause::ring_full); + ReleaseSRWLockExclusive(&draw_lock); + return false; + } if (target) { std::memcpy(target, reinterpret_cast(address), bytes); std::memset(target + bytes, 0, padded - bytes); @@ -2600,7 +2610,8 @@ 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); + const bool readable_range = address && bytes && readable_window(address, bytes) >= bytes; + bool have = readable_range && frame_copy(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. @@ -2613,6 +2624,16 @@ bool draw_translated(Wrapper* self, bool indexed, UINT count, UINT instances, UI ReleaseSRWLockExclusive(&draw_lock); return false; } + if (readable_range && !have) { + // The guest memory is readable and only the frame copy failed (upload ring full): refuse the draw instead of + // binding null, which would read zeros as if the title had bound nothing. + static LONG copy_refused; + if (first_calls(copy_refused, 6)) + log("x2w-draw %016llx srv %u: buffer copy failed (upload ring), draw refused", tr->key, i); + count_null_binding(x2w_local::NullCause::ring_full); + ReleaseSRWLockExclusive(&draw_lock); + return false; + } if (!have && identity_for_null) { static float identity[64]; for (int k = 0; k < 64; ++k) identity[k] = (k % 16) % 5 == 0 ? 1.0f : 0.0f; diff --git a/src/startup_stubs/d3d12_xs_device.cpp b/src/startup_stubs/d3d12_xs_device.cpp index 57c89ef8ed..cb4d194c73 100644 --- a/src/startup_stubs/d3d12_xs_device.cpp +++ b/src/startup_stubs/d3d12_xs_device.cpp @@ -2417,6 +2417,9 @@ ID3D12PipelineState* build_stream_pipeline(const D3D12_PIPELINE_STATE_STREAM_DES // 16 MiB up to 256 MiB in total and stay alive for the process. Exhaustion // refuses new reservations rather than reusing storage without completion. +volatile LONG64 upload_ring_reserved_bytes, upload_ring_capacity_bytes; +volatile LONG upload_ring_reservations, upload_ring_refusals; + UploadReservation reserve_upload_ring(std::size_t bytes) { constexpr u64 initial_chunk_bytes = u64{16} << 20; @@ -2425,6 +2428,10 @@ UploadReservation reserve_upload_ring(std::size_t bytes) UploadReservation reservation; if (bytes == 0 || bytes > max_alloc_bytes) return reservation; const u64 rounded = (static_cast(bytes) + 511) & ~u64{511}; + const auto note_reserved = [](u64 reserved) { + InterlockedAdd64(&upload_ring_reserved_bytes, static_cast(reserved)); + InterlockedIncrement(&upload_ring_reservations); + }; static SRWLOCK lock = SRWLOCK_INIT; static std::vector chunks; @@ -2436,6 +2443,7 @@ UploadReservation reserve_upload_ring(std::size_t bytes) reservation = reserve_upload_chunk(chunks[active], rounded); if (reservation.mapped) { ReleaseSRWLockExclusive(&lock); + note_reserved(rounded); return reservation; } } @@ -2465,9 +2473,11 @@ UploadReservation reserve_upload_ring(std::size_t bytes) try { chunks.push_back(UploadRingChunk{created, static_cast(base), chunk_bytes, 0}); total_bytes += chunk_bytes; + InterlockedAdd64(&upload_ring_capacity_bytes, static_cast(chunk_bytes)); active = chunks.size() - 1; reservation = reserve_upload_chunk(chunks.back(), rounded); ReleaseSRWLockExclusive(&lock); + note_reserved(rounded); return reservation; } catch (const std::bad_alloc&) { created->Unmap(0, nullptr); @@ -2487,11 +2497,13 @@ UploadReservation reserve_upload_ring(std::size_t bytes) if (reservation.mapped) { active = index; ReleaseSRWLockExclusive(&lock); + note_reserved(rounded); return reservation; } } ReleaseSRWLockExclusive(&lock); + InterlockedIncrement(&upload_ring_refusals); static LONG refused; if (first_calls(refused, 8)) log("x2w-draw upload ring reservation refused: no unused capacity"); @@ -6785,12 +6797,12 @@ HRESULT __stdcall queue_present(Wrapper* self, UINT planes, const void* plane_pa } if (render_mode() && frame % 300 == 0) log("x2w-render totals: resets %ld (failed %ld), closes %ld, clears %ld, target binds %ld (unresolved %ld), executes %ld, draws %ld (skipped %ld), " - "translated draws %ld (not drawn %ld), pipelines translated %ld / missing %ld, null bindings %ld (entry %ld, buffer %ld, token %ld, wrapper %ld, dimension %ld), " + "translated draws %ld (not drawn %ld), pipelines translated %ld / missing %ld, null bindings %ld (entry %ld, buffer %ld, token %ld, wrapper %ld, dimension %ld, ring_full %ld), " "formatted-load draws refused %ld, textures uploaded %ld (%ld KiB, unreadable %ld), heap-indexed roots deferred %ld (real draws refused: view heap %ld, " "sampler heap %ld, both heaps %ld, state %ld), pipelines first looked up by ExecuteIndirect %ld, compute requests published %ld (already present %ld, failed %ld, pointer-like blob qwords %ld)", render_resets, render_reset_failures, render_closes, render_clears, render_target_binds, render_target_unresolved, render_executes, render_draws, render_draws_skipped, translated_draws, untranslated_draws, translation_ready, translation_missing, null_total, - null_causes[0], null_causes[1], null_causes[2], null_causes[3], null_causes[4], formatted_draws_refused, textures_uploaded, + null_causes[0], null_causes[1], null_causes[2], null_causes[3], null_causes[4], null_causes[5], formatted_draws_refused, textures_uploaded, texture_bytes_uploaded, textures_unreadable, heap_roots_deferred, heap_root_refusals[static_cast(x2w_startup::RealRootDecision::view_heap_missing)], heap_root_refusals[static_cast(x2w_startup::RealRootDecision::sampler_heap_missing)], @@ -6798,6 +6810,9 @@ HRESULT __stdcall queue_present(Wrapper* self, UINT planes, const void* plane_pa heap_root_refusals[static_cast(x2w_startup::RealRootDecision::state_not_restorable)], indirect_first_lookups, compute_requests_published, compute_requests_present, compute_requests_failed, compute_blob_pointer_words); if (render_mode() && frame % 300 == 0) { + log("x2w-render upload ring: reserved %llu MiB in %ld reservations, refusals %ld, capacity %llu MiB", + static_cast(upload_ring_reserved_bytes) >> 20, upload_ring_reservations, upload_ring_refusals, + static_cast(upload_ring_capacity_bytes) >> 20); log("x2w-render compute dispatches: translated %ld, refused %ld, no translation %ld, not modelled %ld", compute_dispatches_translated, compute_dispatches_refused, compute_dispatches_untranslated, compute_dispatches_unmodelled); log_compute_census(); diff --git a/src/startup_stubs/null_binding_cause.h b/src/startup_stubs/null_binding_cause.h index faf05a2b25..8fe58a7946 100644 --- a/src/startup_stubs/null_binding_cause.h +++ b/src/startup_stubs/null_binding_cause.h @@ -10,9 +10,10 @@ enum class NullCause : int { unknown_token, // descriptor memory names a view the wrapper does not know texture_wrapper, // the view's resource is missing or is not a resource wrapper dimension_mismatch, // the view dimension does not match the shader + ring_full, // the guest memory was readable but the frame copy found no upload ring space (the draw is refused) }; -constexpr int null_cause_count = 5; -constexpr const char* null_cause_names[null_cause_count] = {"entry", "buffer", "token", "wrapper", "dimension"}; +constexpr int null_cause_count = 6; +constexpr const char* null_cause_names[null_cause_count] = {"entry", "buffer", "token", "wrapper", "dimension", "ring_full"}; struct BindingFacts { bool texture = false; @@ -23,10 +24,12 @@ struct BindingFacts { bool resource_wrapper = false; // texture: the view resolves to a resource wrapper bool dimension_matches = true; // texture: the view dimension suits the shader bool bound = false; // buffer: a readable range was bound + bool ring_failed = false; // buffer: the range is readable but the upload ring had no space for its copy }; inline NullCause classify_null_binding(const BindingFacts& f) { + if (!f.texture && f.ring_failed) return NullCause::ring_full; // A buffer that ended up bound (also through the identity fallback) is not a null binding. if (!f.texture && f.bound) return NullCause::none; if (f.from_table && !f.entry_readable) return NullCause::table_entry; diff --git a/tests/startup_null_cause_labels_check.cpp b/tests/startup_null_cause_labels_check.cpp index 0d3811db4b..e9455430f5 100644 --- a/tests/startup_null_cause_labels_check.cpp +++ b/tests/startup_null_cause_labels_check.cpp @@ -10,7 +10,7 @@ namespace { using x2w_local::BindingFacts; int failures, cases; -const char* const labels[] = {"entry", "buffer", "token", "wrapper", "dimension"}; +const char* const labels[] = {"entry", "buffer", "token", "wrapper", "dimension", "ring_full"}; // Facts as the texture binding builds them: from a table, with a readable entry carrying a known // view token for a resource wrapper of the shader's dimension. @@ -68,13 +68,13 @@ int main(int argc, char** argv) std::printf("unknown mutation\n"); return 2; } - const int entry = 0, buffer = swapped ? 2 : 1, token = swapped ? 1 : 2, wrapper = 3, dimension = 4; + const int entry = 0, buffer = swapped ? 2 : 1, token = swapped ? 1 : 2, wrapper = 3, dimension = 4, ring_full = 5; - if (x2w_local::null_cause_count != 5) { - std::printf("FAIL: %d causes, expected 5\n", x2w_local::null_cause_count); + if (x2w_local::null_cause_count != 6) { + std::printf("FAIL: %d causes, expected 6\n", x2w_local::null_cause_count); return 1; } - for (int i = 0; i < 5; ++i) { + for (int i = 0; i < 6; ++i) { ++cases; if (std::strcmp(x2w_local::null_cause_names[i], labels[i]) != 0) { std::printf("FAIL: slot %d named \"%s\", expected \"%s\"\n", i, x2w_local::null_cause_names[i], labels[i]); @@ -101,6 +101,12 @@ int main(int argc, char** argv) f = texture(); f.dimension_matches = false; counted("texture view of another dimension", f, dimension); + f = table_buffer(false); + f.ring_failed = true; + counted("table buffer whose copy found no upload ring space", f, ring_full); + f = table_buffer(true); + f.ring_failed = true; + counted("a ring failure outranks a bound flag only for buffers", f, ring_full); // A frame of bindings in which each cause occurs a different power of two times, so a count // printed under another label changes the line. Valid bindings add nothing. @@ -113,32 +119,34 @@ int main(int argc, char** argv) unknown.token_known = unknown.resource_wrapper = false; no_wrapper.resource_wrapper = false; mismatch.dimension_matches = false; + BindingFacts ring_buffer = table_buffer(false); + ring_buffer.ring_failed = true; const Binding frame[] = { {texture(), 3}, {table_buffer(true), 2}, {unreadable, 1}, {table_buffer(false), 2}, - {unknown, 4}, {no_wrapper, 8}, {mismatch, 16}, + {unknown, 4}, {no_wrapper, 8}, {mismatch, 16}, {ring_buffer, 32}, }; - long total = 0, by_slot[5] = {}; + long total = 0, by_slot[6] = {}; for (const Binding& b : frame) { const int slot = slot_of(b.facts); if (slot == static_cast(x2w_local::NullCause::none)) continue; - if (slot < 0 || slot >= 5) { - std::printf("FAIL: a null binding in slot %d, outside the five printed\n", slot); + if (slot < 0 || slot >= 6) { + std::printf("FAIL: a null binding in slot %d, outside the six printed\n", slot); return 1; } total += b.times; by_slot[slot] += b.times; } // The device's format string, with the slots in the order it passes them. - char line[160]; - std::snprintf(line, sizeof(line), "null bindings %ld (entry %ld, buffer %ld, token %ld, wrapper %ld, dimension %ld)", - total, by_slot[0], by_slot[1], by_slot[2], by_slot[3], by_slot[4]); - const char* const expected = swapped ? "null bindings 31 (entry 1, buffer 4, token 2, wrapper 8, dimension 16)" - : "null bindings 31 (entry 1, buffer 2, token 4, wrapper 8, dimension 16)"; + char line[200]; + std::snprintf(line, sizeof(line), "null bindings %ld (entry %ld, buffer %ld, token %ld, wrapper %ld, dimension %ld, ring_full %ld)", + total, by_slot[0], by_slot[1], by_slot[2], by_slot[3], by_slot[4], by_slot[5]); + const char* const expected = swapped ? "null bindings 63 (entry 1, buffer 4, token 2, wrapper 8, dimension 16, ring_full 32)" + : "null bindings 63 (entry 1, buffer 2, token 4, wrapper 8, dimension 16, ring_full 32)"; same_text("totals line", line, expected); // The same counts labelled from the header's names. const char* const* n = x2w_local::null_cause_names; - std::snprintf(line, sizeof(line), "null bindings %ld (%s %ld, %s %ld, %s %ld, %s %ld, %s %ld)", total, n[0], by_slot[0], - n[1], by_slot[1], n[2], by_slot[2], n[3], by_slot[3], n[4], by_slot[4]); + std::snprintf(line, sizeof(line), "null bindings %ld (%s %ld, %s %ld, %s %ld, %s %ld, %s %ld, %s %ld)", total, n[0], by_slot[0], + n[1], by_slot[1], n[2], by_slot[2], n[3], by_slot[3], n[4], by_slot[4], n[5], by_slot[5]); same_text("totals line from the header's names", line, expected); if (failures) { From 07222b46ca69ec819147a3ff65bd07ffdc41bb7f Mon Sep 17 00:00:00 2001 From: c1 Date: Thu, 26 Nov 2026 16:09:04 +0100 Subject: [PATCH 2/2] fix: count ring_full for a formatted buffer whose frame copy failed before refusing the draw --- src/startup_stubs/d3d12_draw.inl | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/startup_stubs/d3d12_draw.inl b/src/startup_stubs/d3d12_draw.inl index 1f8ac9ecc8..4feadb4ef7 100644 --- a/src/startup_stubs/d3d12_draw.inl +++ b/src/startup_stubs/d3d12_draw.inl @@ -2617,6 +2617,8 @@ bool draw_translated(Wrapper* self, bool indexed, UINT count, UINT instances, UI // slot the title leaves empty here; why is not known yet. static const bool identity_for_null = x2w_local::env_on("X2W_DRAW_NULL_IDENTITY"); if (!have && formatted.entry) { + // A readable range whose copy failed (upload ring full) is a ring_full refusal, not an unresolved buffer. + if (readable_range) count_null_binding(x2w_local::NullCause::ring_full); InterlockedIncrement(&formatted_draws_refused); static LONG refused; if (first_calls(refused, 6))