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: 6 additions & 0 deletions runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down
21 changes: 12 additions & 9 deletions runtime/shims/d3d12_vulkan_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -3207,6 +3208,7 @@ struct DeviceState {
std::vector<std::uint8_t> mirror; // the title's bytes as the tracker last copied them
std::vector<std::uint8_t> made; // the title's bytes each arena slot was last made from
std::vector<std::uint8_t> 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<std::uint32_t> retries;
std::set<std::uint32_t> empty_slots;
std::uint32_t next_empty = 0;
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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<uint32_t*>(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
Expand Down Expand Up @@ -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<const std::uint8_t*>(static_cast<std::uintptr_t>(console_va)),
heap->input, std::size_t(bytes));
}
Expand Down
115 changes: 115 additions & 0 deletions runtime/src/console_heap_vsharp_slots.h
Original file line number Diff line number Diff line change
@@ -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 <algorithm>
#include <bit>
#include <cstddef>
#include <cstdint>
#include <vector>

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<unsigned>(first & 63);
const unsigned end_bit = static_cast<unsigned>(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::size_t>(std::popcount(cleared));
}
return;
}
if (const std::uint64_t cleared = words_[w] & head_mask) {
words_[w] &= ~head_mask;
live_count_ -= static_cast<std::size_t>(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::size_t>(std::popcount(cleared));
}
}
if (const std::uint64_t cleared = words_[last_w] & tail_mask) {
words_[last_w] &= ~tail_mask;
live_count_ -= static_cast<std::size_t>(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 <class F>
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<unsigned>(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<std::uint64_t> words_;
};

} // namespace leonida
Loading
Loading