diff --git a/docs/experiments.md b/docs/experiments.md index 13e4d28d40..6afa01b625 100644 --- a/docs/experiments.md +++ b/docs/experiments.md @@ -17121,7 +17121,14 @@ With a device slot 67 placeholder bound, command-list slot 14 looks up the trans `startup_compute_dispatch_two_mips_` writes two distinct patterns through two UAVs of one RGBA16_FLOAT texture in the same translated Dispatch. The test compares all 1,280 half values in the 32×8 and 16×4 subresources, including the sentinel values outside the written rectangles. Before translation both mips keep their sentinels. Exchanging the two views moves the patterns to opposite mips and changes 64 half values relative to the ordered case. -The log line `x2w-render compute dispatches: translated , refused , no translation , not modelled ` follows the render totals; "not modelled" counts calls whose manifest exists but holds a line the runtime does not bind. It is followed by a census: the refused calls per first refusal reason of their key, and every dispatched key by call count, each with its count, the calls recorded and the first refused table descriptor. +Every 300 render frames, `x2w-render upload ring: reserved MiB in reservations, refusals , capacity MiB` reports +cumulative successful reservations and their 512-byte-rounded sizes, including reservations in recycled chunks. +`refusals` counts reservation attempts that exhaust the allocator's available options. The submit-time rejection +of a list whose chunks were recycled is logged separately. `capacity` is the total allocated chunk size. +With `X2W_D3D12_RING_RELEASE_AT_SUBMIT=1`, completed fenced submits can make chunks reusable sooner, +so cumulative reserved bytes can exceed capacity. These totals do not measure current occupancy. + +The log line `x2w-render compute dispatches: translated , refused , no translation , not modelled , builder gate refused ` follows the upload ring totals. The builder gate field counts dispatches refused by `X2W_COMPUTE_BUILDER_GATE`. The "not modelled" field counts calls whose manifest exists but holds a line the runtime does not bind. It is followed by a census: the refused calls per first refusal reason of their key, and every dispatched key by call count, each with its count, the calls recorded and the first refused table descriptor. The sampling fixture uses a 1x1 texture, so it does not tell sampler filter or address modes apart. Programs with other texture kinds, sampler heaps, the same texture as input and output, or buffer outputs in memory that is not aliased are not dispatched. diff --git a/src/startup_stubs/d3d12_draw.inl b/src/startup_stubs/d3d12_draw.inl index 849c9bc687..393dc9e550 100644 --- a/src/startup_stubs/d3d12_draw.inl +++ b/src/startup_stubs/d3d12_draw.inl @@ -2853,7 +2853,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(self, padded, &offset, &chunk) : nullptr; + const bool readable_range = readable_window(address, bytes) >= bytes; + auto* target = readable_range ? reserve_upload_ring(self, 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); @@ -2868,12 +2878,15 @@ 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(self, address, bytes, &offset, &chunk); + const bool readable_range = address && bytes && readable_window(address, bytes) >= bytes; + bool have = readable_range && 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. 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)) @@ -2881,6 +2894,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 9dd921a6a8..bb125404dd 100644 --- a/src/startup_stubs/d3d12_xs_device.cpp +++ b/src/startup_stubs/d3d12_xs_device.cpp @@ -2684,12 +2684,19 @@ ID3D12PipelineState* build_stream_pipeline(const D3D12_PIPELINE_STATE_STREAM_DES // no recording can submit its bytes and every queue that used it has finished. With // X2W_D3D12_RING_RELEASE_AT_SUBMIT=1 every chunk is 16 MiB, up to 512 MiB, and a fenced submit releases its lists. +volatile LONG64 upload_ring_reserved_bytes, upload_ring_capacity_bytes; +volatile LONG upload_ring_reservations, upload_ring_refusals; + UploadReservation reserve_upload_ring(Wrapper* list, std::size_t bytes) { constexpr u64 max_alloc_bytes = max_upload_ring_bytes / 4; 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); + }; auto& chunks = upload_chunks; static unsigned active = 0; @@ -2704,7 +2711,10 @@ UploadReservation reserve_upload_ring(Wrapper* list, std::size_t bytes) }; if (!chunks.empty()) { reservation = reserve_upload_chunk(chunks[active], rounded); - if (reservation.mapped) return finish(active); + if (reservation.mapped) { + note_reserved(rounded); + return finish(active); + } } const bool release_at_submit = ring_release_at_submit(); @@ -2734,7 +2744,9 @@ UploadReservation reserve_upload_ring(Wrapper* list, 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)); reservation = reserve_upload_chunk(chunks.back(), rounded); + note_reserved(rounded); return finish(static_cast(chunks.size() - 1)); } catch (const std::bad_alloc&) { created->Unmap(0, nullptr); @@ -2751,15 +2763,22 @@ UploadReservation reserve_upload_ring(Wrapper* list, std::size_t bytes) // Allocation failed or the cap was reached. Older unused tails are still safe. for (unsigned index = 0; index < chunks.size(); ++index) { reservation = reserve_upload_chunk(chunks[index], rounded); - if (reservation.mapped) return finish(index); + if (reservation.mapped) { + note_reserved(rounded); + return finish(index); + } } for (unsigned index = 0; index < chunks.size(); ++index) { reservation = reuse_upload_chunk(chunks[index], index, rounded, queue_submits); - if (reservation.mapped) return finish(index); + if (reservation.mapped) { + note_reserved(rounded); + return finish(index); + } } ReleaseSRWLockExclusive(&allocator_lock); + InterlockedIncrement(&upload_ring_refusals); static LONG refused; if (first_calls(refused, 8)) log("x2w-draw upload ring reservation refused: no unused capacity"); @@ -7389,12 +7408,12 @@ HRESULT __stdcall queue_present(Wrapper* self, UINT planes, const void* plane_pa root_srv_copies_made); 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)], @@ -7405,6 +7424,9 @@ HRESULT __stdcall queue_present(Wrapper* self, UINT planes, const void* plane_pa log("x2w-draw pso recipes totals: loaded %ld, precreated %ld, mismatches %ld, fallbacks %ld", pso_recipes_loaded, pso_recipes_precreated, pso_recipe_mismatches, pso_recipe_fallbacks); 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, builder gate refused %ld", compute_dispatches_translated, compute_dispatches_refused, compute_dispatches_untranslated, compute_dispatches_unmodelled, compute_builder_gate_refusals); 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) {