diff --git a/runtime/CMakeLists.txt b/runtime/CMakeLists.txt index c1d0e7cb53..3c5ebfe9ef 100644 --- a/runtime/CMakeLists.txt +++ b/runtime/CMakeLists.txt @@ -4383,6 +4383,12 @@ if(TARGET Vulkan::Headers) target_compile_options(title_window_plan_bounds_test PRIVATE /W4 /permissive- /utf-8) endif() add_test(NAME title_window_plan_bounds COMMAND title_window_plan_bounds_test) + # Host-RAM bitset of live console resource-heap V# slots (src/console_heap_vsharp_slots.h). + add_executable(console_heap_vsharp_slots_test test/console_heap_vsharp_slots_test.cpp) + if(WIN32 AND MSVC) + target_compile_options(console_heap_vsharp_slots_test PRIVATE /W4 /permissive- /utf-8) + endif() + add_test(NAME console_heap_vsharp_slots COMMAND console_heap_vsharp_slots_test) # Queue construction cleans its Vulkan handles if holder registration runs out of host memory. add_executable(vk_host_texture_order_test test/vk_host_texture_order_test.cpp) target_link_libraries(vk_host_texture_order_test PRIVATE Vulkan::Headers ${CMAKE_DL_LIBS}) diff --git a/runtime/shims/d3d12_vulkan_device.cpp b/runtime/shims/d3d12_vulkan_device.cpp index 45f899b2f3..b3fd8c418b 100644 --- a/runtime/shims/d3d12_vulkan_device.cpp +++ b/runtime/shims/d3d12_vulkan_device.cpp @@ -60,6 +60,7 @@ #include "../src/spirv_f16_halves.h" #include "../src/spirv_stores.h" #include "../src/title_va_alias.h" +#include "../src/console_heap_vsharp_slots.h" #include "../src/title_window_plan.h" #include "../src/vulkan_present.h" #include "../src/vulkan_device_filter.h" @@ -3207,6 +3208,7 @@ struct DeviceState { std::vector mirror; // the title's bytes as the tracker last copied them std::vector made; // the title's bytes each arena slot was last made from std::vector retry; // per slot: not settled yet (fix_console_slot), in `retries` + leonida::ConsoleHeapVsharpSlots live_vsharps; // slots whose GPU copy holds a live buffer V# std::vector retries; std::set empty_slots; std::uint32_t next_empty = 0; @@ -3286,6 +3288,7 @@ struct DeviceState { if (empty) heap.empty_slots.insert(std::uint32_t(slot)); else if (!heap.empty_slots.empty()) heap.empty_slots.erase(std::uint32_t(slot)); std::memcpy(heap.copy + slot * kDescriptorSize, w, kDescriptorSize); + heap.live_vsharps.set(slot, leonida::ConsoleHeapVsharpSlots::is_live_vsharp(w)); return settled; } @@ -3316,6 +3319,7 @@ struct DeviceState { heap.fix_over = true; if (heap.fix_over) { std::memset(heap.copy + at, 0, kDescriptorSize); + heap.live_vsharps.clear(slot); ++heap.deferred_fixes; if (!heap.retry[slot]) { heap.retry[slot] = 1; @@ -3401,6 +3405,7 @@ struct DeviceState { const auto end = std::uint32_t((offset + bytes) / kDescriptorSize); heap.empty_slots.erase(heap.empty_slots.lower_bound(first), heap.empty_slots.lower_bound(end)); for (auto slot = first; slot < end; ++slot) heap.retry[slot] = 0; + heap.live_vsharps.clear_range(first, end); }); const auto t1 = clock::now(); heap.fix_start = t1; @@ -3413,16 +3418,13 @@ struct DeviceState { heap.alias_generation = alias_generation; heap.checked_generation = generation; // Even an unchanged title slot may name a buffer released since the last submission. - for (size_t at = 0; at + kDescriptorSize <= heap.bytes; at += kDescriptorSize) { - auto* w = reinterpret_cast(heap.copy + at); - if (!(w[3] >> 30) && (w[2] || w[6])) { - const auto slot = at / kDescriptorSize; - if (!fix_console_slot(heap, slot) && !heap.retry[slot]) { - heap.retry[slot] = 1; - heap.retries.push_back(uint32_t(slot)); - } + // Walk the host-RAM V# bitset instead of reading every slot of `heap.copy` (which may be uncached WC VRAM). + heap.live_vsharps.for_each([&](std::size_t slot) { + if (!fix_console_slot(heap, slot) && !heap.retry[slot]) { + heap.retry[slot] = 1; + heap.retries.push_back(uint32_t(slot)); } - } + }); } const auto t2 = clock::now(); // A settled zero can acquire GPU backing without a CPU map event. Probe at most eight in @@ -3640,6 +3642,7 @@ struct DeviceState { heap->previous = heap->made.data(); } heap->retry.assign(std::size_t(bytes / kDescriptorSize), 0); + heap->live_vsharps.reset(std::size_t(bytes / kDescriptorSize)); heap->pages = make_shadow_pages(reinterpret_cast(static_cast(console_va)), heap->input, std::size_t(bytes)); } diff --git a/runtime/src/console_heap_vsharp_slots.h b/runtime/src/console_heap_vsharp_slots.h new file mode 100644 index 0000000000..0b0830053b --- /dev/null +++ b/runtime/src/console_heap_vsharp_slots.h @@ -0,0 +1,115 @@ +#pragma once +// Host-RAM bitset of console resource-heap slots whose GPU copy (`heap.copy`) currently holds a live buffer V# +// (`!(w[3] >> 30) && (w[2] || w[6])`). +// +// When a buffer backing or title VA alias is released, `refresh_console_heap` must re-run `fix_console_slot` on every +// slot that might still point at the released allocation. Scanning `heap.copy` linearly reads every 32-byte slot of +// the heap (4-5.5+ MiB): when `heap.copy` sits in `arena.mapped` (uncached write-combined GPU memory), each release +// triggers ~180k uncached PCIe reads. Tracking the live V# slots in host RAM keeps invalidation scans strictly in CPU +// cache (zero reads from `heap.copy`) and skips empty and T# slots 64 at a time. + +#include +#include +#include +#include +#include + +namespace leonida { + +class ConsoleHeapVsharpSlots { +public: + // Matches the descriptor predicate for a non-empty buffer V# (primary or counter half) in a 32-byte slot. + static constexpr bool is_live_vsharp(const std::uint32_t* w) noexcept { + return !(w[3] >> 30) && (w[2] || w[6]); + } + + void reset(std::size_t slot_count) { + slot_count_ = slot_count; + live_count_ = 0; + words_.assign((slot_count + 63) / 64, 0); + } + + void set(std::size_t slot, bool live) noexcept { + if (slot >= slot_count_) return; + std::uint64_t& word = words_[slot >> 6]; + const std::uint64_t mask = std::uint64_t{1} << (slot & 63); + const bool was = (word & mask) != 0; + if (live == was) return; + if (live) { + word |= mask; + ++live_count_; + } else { + word &= ~mask; + --live_count_; + } + } + + void clear(std::size_t slot) noexcept { set(slot, false); } + + // Clears every slot in [first, end), clamping to slot_count(). + void clear_range(std::size_t first, std::size_t end) noexcept { + if (first >= end || first >= slot_count_ || live_count_ == 0) return; + end = (std::min)(end, slot_count_); + std::size_t w = first >> 6; + const std::size_t last_w = (end - 1) >> 6; + const unsigned first_bit = static_cast(first & 63); + const unsigned end_bit = static_cast(end & 63); + const std::uint64_t head_mask = ~std::uint64_t{0} << first_bit; + const std::uint64_t tail_mask = end_bit ? ((std::uint64_t{1} << end_bit) - 1) : ~std::uint64_t{0}; + if (w == last_w) { + const std::uint64_t mask = head_mask & tail_mask; + const std::uint64_t cleared = words_[w] & mask; + if (cleared) { + words_[w] &= ~mask; + live_count_ -= static_cast(std::popcount(cleared)); + } + return; + } + if (const std::uint64_t cleared = words_[w] & head_mask) { + words_[w] &= ~head_mask; + live_count_ -= static_cast(std::popcount(cleared)); + } + for (++w; w < last_w; ++w) { + if (const std::uint64_t cleared = words_[w]) { + words_[w] = 0; + live_count_ -= static_cast(std::popcount(cleared)); + } + } + if (const std::uint64_t cleared = words_[last_w] & tail_mask) { + words_[last_w] &= ~tail_mask; + live_count_ -= static_cast(std::popcount(cleared)); + } + } + + // Calls fn(slot) in ascending slot order for every set bit present when its 64-slot word is entered. + // Safe if fn mutates the bit for `slot` (e.g., fix_console_slot clearing an invalidated V#). + template + void for_each(F&& fn) const { + if (live_count_ == 0) return; + for (std::size_t w = 0; w < words_.size(); ++w) { + std::uint64_t bits = words_[w]; + if (!bits) continue; + const std::size_t base = w << 6; + while (bits) { + const unsigned bit = static_cast(std::countr_zero(bits)); + bits &= bits - 1; + fn(base + bit); + } + } + } + + bool test(std::size_t slot) const noexcept { + return slot < slot_count_ && ((words_[slot >> 6] >> (slot & 63)) & 1u) != 0; + } + + std::size_t slot_count() const noexcept { return slot_count_; } + std::size_t live_count() const noexcept { return live_count_; } + bool empty() const noexcept { return live_count_ == 0; } + +private: + std::size_t slot_count_ = 0; + std::size_t live_count_ = 0; + std::vector words_; +}; + +} // namespace leonida diff --git a/runtime/test/console_heap_vsharp_slots_test.cpp b/runtime/test/console_heap_vsharp_slots_test.cpp new file mode 100644 index 0000000000..ebb74be452 --- /dev/null +++ b/runtime/test/console_heap_vsharp_slots_test.cpp @@ -0,0 +1,259 @@ +// Portable CPU-only unit test for leonida::ConsoleHeapVsharpSlots (runtime/src/console_heap_vsharp_slots.h). +// Verifies bit-for-bit equivalence with a linear scan of 32-byte descriptor slots, boundary handling, range clears, +// callback-time bit clearing (as in refresh_console_heap invalidation passes), and a 5.5 MiB heap stress check. +#include "../src/console_heap_vsharp_slots.h" + +#include +#include +#include +#include +#include + +#define EXPECT(condition) \ + do { \ + if (!(condition)) { \ + std::fprintf(stderr, "FAIL line %d: %s\n", __LINE__, #condition); \ + return 1; \ + } \ + } while (0) + +namespace { + +using leonida::ConsoleHeapVsharpSlots; + +std::vector collect(const ConsoleHeapVsharpSlots& slots) { + std::vector out; + slots.for_each([&](std::size_t slot) { out.push_back(slot); }); + return out; +} + +int test_predicate_exactness() { + // Primary buffer V#: top 2 bits of w[3] are 0, w[2] (num_records) != 0. + std::uint32_t primary[8] = {0x1000, 0x20, 64, 0x3fffffffu, 0, 0, 0, 0}; + EXPECT(ConsoleHeapVsharpSlots::is_live_vsharp(primary)); + + // Counter-only buffer V#: w[2] == 0, w[6] != 0, top 2 bits of w[3] == 0. + std::uint32_t counter[8] = {0, 0, 0, 0, 0x2000, 0, 16, 0}; + EXPECT(ConsoleHeapVsharpSlots::is_live_vsharp(counter)); + + // Empty / zeroed V#: w[2] == 0 and w[6] == 0. + std::uint32_t empty[8] = {0x1000, 0, 0, 0x00010000u, 0x2000, 0, 0, 0}; + EXPECT(!ConsoleHeapVsharpSlots::is_live_vsharp(empty)); + + // Image T# (w[3] >> 30 != 0): must NOT be treated as a buffer V# even if w[2]/w[6] are non-zero. + for (std::uint32_t hi : {1u, 2u, 3u}) { + std::uint32_t tsharp[8] = {0x1000, 0, 128, (hi << 30) | 0x55u, 0, 0, 64, 0}; + EXPECT(!ConsoleHeapVsharpSlots::is_live_vsharp(tsharp)); + } + + // not_tsharp zeroes lower 16 bytes (w[0..3] = 0) while leaving upper 16 bytes (w[4..7]): + // if w[6] is non-zero, predicate matches the legacy `!(w[3] >> 30) && (w[2] || w[6])` check. + std::uint32_t not_tsharp_with_hi[8] = {0, 0, 0, 0, 0, 0, 1, 0}; + EXPECT(ConsoleHeapVsharpSlots::is_live_vsharp(not_tsharp_with_hi)); + return 0; +} + +int test_boundaries_and_ranges() { + // Zero-size heap. + ConsoleHeapVsharpSlots zero; + zero.reset(0); + EXPECT(zero.empty() && zero.slot_count() == 0 && zero.live_count() == 0); + zero.set(0, true); + zero.clear(0); + zero.clear_range(0, 10); + EXPECT(zero.empty() && !zero.test(0) && collect(zero).empty()); + + // Word boundary sizes: 1, 63, 64, 65, 128, 129, 257. + for (std::size_t n : {std::size_t(1), std::size_t(63), std::size_t(64), std::size_t(65), std::size_t(128), + std::size_t(129), std::size_t(257)}) { + ConsoleHeapVsharpSlots s; + s.reset(n); + EXPECT(s.slot_count() == n && s.empty()); + // Out-of-range writes are ignored. + s.set(n, true); + s.set(n + 100, true); + EXPECT(s.empty() && !s.test(n)); + + // Set every slot, check idempotency and exact ordering. + for (std::size_t i = 0; i < n; ++i) s.set(i, true); + s.set(0, true); + s.set(n - 1, true); + EXPECT(s.live_count() == n); + const auto all = collect(s); + EXPECT(all.size() == n); + for (std::size_t i = 0; i < n; ++i) EXPECT(all[i] == i && s.test(i)); + + // Inverted / empty / past-end clear_range is a no-op. + s.clear_range(5, 5); + s.clear_range(10, 2); + s.clear_range(n, n + 64); + EXPECT(s.live_count() == n); + + // Partial single-word and multi-word clear_range. + if (n >= 129) { + s.clear_range(3, 19); // within word 0 + s.clear_range(60, 128); // spans word 0 tail + all of word 1 + EXPECT(s.live_count() == n - 16 - 68); + EXPECT(s.test(2) && !s.test(3) && !s.test(18) && s.test(19)); + EXPECT(s.test(59) && !s.test(60) && !s.test(127) && s.test(128)); + } + + // Clear past end clamps cleanly. + s.clear_range(0, n + 500); + EXPECT(s.empty() && s.live_count() == 0 && collect(s).empty()); + } + return 0; +} + +int test_in_place_invalidation_mutation() { + // Mirrors refresh_console_heap: for_each visits each live V# slot and calls fix_console_slot, which may clear + // that slot's bit when its backing buffer was released. + ConsoleHeapVsharpSlots s; + s.reset(260); + const std::vector initial = {0, 1, 62, 63, 64, 65, 127, 128, 191, 255, 259}; + for (std::size_t slot : initial) s.set(slot, true); + EXPECT(s.live_count() == initial.size()); + + std::vector visited; + s.for_each([&](std::size_t slot) { + visited.push_back(slot); + // Simulate releasing every even-numbered slot during the invalidation pass. + if ((slot & 1u) == 0) s.set(slot, false); + }); + EXPECT(visited == initial); + + std::vector remaining; + for (std::size_t slot : initial) + if ((slot & 1u) != 0) remaining.push_back(slot); + EXPECT(s.live_count() == remaining.size()); + EXPECT(collect(s) == remaining); + + // Clear the remaining slots and confirm empty fast path visits nothing. + s.for_each([&](std::size_t slot) { s.clear(slot); }); + EXPECT(s.empty() && collect(s).empty()); + return 0; +} + +int test_differential_equivalence_against_linear_copy() { + // Differential model: maintain a synthetic 32-byte-per-slot `heap.copy` buffer alongside ConsoleHeapVsharpSlots + // across thousands of random writes, deferred zeroes, page-range zeroes, and invalidation passes. + constexpr std::size_t kSlots = 1029; + std::vector> gpu_copy(kSlots); + for (auto& slot : gpu_copy) slot.fill(0); + + ConsoleHeapVsharpSlots tracked; + tracked.reset(kSlots); + + auto linear_scan = [&] { + std::vector expected; + for (std::size_t i = 0; i < kSlots; ++i) + if (ConsoleHeapVsharpSlots::is_live_vsharp(gpu_copy[i].data())) expected.push_back(i); + return expected; + }; + + std::uint64_t rng = 0x9e3779b97f4a7c15ull; + auto next_u32 = [&]() -> std::uint32_t { + rng ^= rng >> 12; + rng ^= rng << 25; + rng ^= rng >> 27; + return static_cast((rng * 0x2545f4914f6cdd1dull) >> 32); + }; + + for (unsigned step = 0; step < 4000; ++step) { + const unsigned op = next_u32() % 10; + if (op < 6) { + // Simulate fix_console_slot writing a descriptor to heap.copy. + const std::size_t slot = next_u32() % kSlots; + std::array w{}; + const unsigned kind = next_u32() % 5; + if (kind == 0) { + // Live primary V# + w[0] = next_u32(); + w[2] = (next_u32() % 256) + 1; + w[3] = next_u32() & 0x3fffffffu; + } else if (kind == 1) { + // Live counter V# only + w[6] = (next_u32() % 64) + 1; + } else if (kind == 2) { + // Empty V# (zero records) + w[0] = 0; + w[2] = 0; + w[3] = next_u32() & 0x3fffffffu; + w[6] = 0; + } else if (kind == 3) { + // Image T# + w[2] = 128; + w[3] = (2u << 30) | (next_u32() & 0x3fffffffu); + w[6] = 32; + } else { + // not_tsharp (lower 16B zeroed, upper 16B arbitrary) + w[4] = next_u32(); + w[6] = (next_u32() & 1u) ? 7u : 0u; + } + gpu_copy[slot] = w; + tracked.set(slot, ConsoleHeapVsharpSlots::is_live_vsharp(w.data())); + } else if (op < 8) { + // Simulate fix_console_slots deferred zeroing. + const std::size_t slot = next_u32() % kSlots; + gpu_copy[slot].fill(0); + tracked.clear(slot); + } else if (op == 8) { + // Simulate for_each_zeroed (both page-aligned 128-slot and arbitrary sub-ranges). + const std::size_t first = next_u32() % (kSlots + 16); + const std::size_t len = (next_u32() % 200); + const std::size_t end = first + len; + for (std::size_t i = first; i < end && i < kSlots; ++i) gpu_copy[i].fill(0); + tracked.clear_range(first, end); + } else { + // Simulate refresh_console_heap invalidation pass clearing a subset of visited slots. + const std::vector expected = linear_scan(); + std::vector got; + tracked.for_each([&](std::size_t slot) { + got.push_back(slot); + if ((slot % 3) == 0) { + gpu_copy[slot].fill(0); + tracked.set(slot, false); + } + }); + EXPECT(got == expected); + } + + if ((step & 63) == 63) { + const std::vector expected = linear_scan(); + EXPECT(tracked.live_count() == expected.size()); + EXPECT(collect(tracked) == expected); + } + } + EXPECT(collect(tracked) == linear_scan()); + return 0; +} + +int test_retail_sized_heap_sparse_scan() { + // 5.5 MiB resource heap = 180,224 32-byte slots (matching retail console descriptor heap). + constexpr std::size_t kRetailSlots = (5632u * 1024u) / 32u; + ConsoleHeapVsharpSlots tracked; + tracked.reset(kRetailSlots); + + const std::vector hot = {0, 63, 64, 4095, 4096, 65536, 131072, kRetailSlots - 1}; + for (std::size_t slot : hot) tracked.set(slot, true); + EXPECT(tracked.live_count() == hot.size()); + EXPECT(collect(tracked) == hot); + + // Zero a 4 KiB page (128 slots) containing slots 4096..4223. + tracked.clear_range(4096, 4096 + 128); + EXPECT(!tracked.test(4096) && tracked.test(4095)); + EXPECT(tracked.live_count() == hot.size() - 1); + return 0; +} + +} // namespace + +int main() { + if (test_predicate_exactness()) return 1; + if (test_boundaries_and_ranges()) return 1; + if (test_in_place_invalidation_mutation()) return 1; + if (test_differential_equivalence_against_linear_copy()) return 1; + if (test_retail_sized_heap_sparse_scan()) return 1; + std::printf("console_heap_vsharp_slots_test: ok\n"); + return 0; +}