upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/runtime/src/title_texture_refresh.h
diff options
context:
space:
mode:
authorJan Trencansky <jan.trencansky@protonmail.com>2026-11-30 04:34:17 -0500
committerJan Trencansky <jan.trencansky@protonmail.com>2026-11-30 04:34:17 -0500
commitefc1929908a728d95fa09ffef0a743d2a649b86d (patch)
tree9e2b3a70ef49d7ec265301cf85606686f371ac57 /runtime/src/title_texture_refresh.h
parent1aefed0dacd492a040909c9a423f270ebdab84f4 (diff)
runtime: eliminate Windows 11 full-space commit_pages_locked broadcast storm, ShadowPages/TitleTextureCells guard conflict, and streaming budget bursts
Diffstat (limited to 'runtime/src/title_texture_refresh.h')
-rw-r--r--runtime/src/title_texture_refresh.h128
1 files changed, 128 insertions, 0 deletions
diff --git a/runtime/src/title_texture_refresh.h b/runtime/src/title_texture_refresh.h
index 30ae34234..8b1a9a00e 100644
--- a/runtime/src/title_texture_refresh.h
+++ b/runtime/src/title_texture_refresh.h
@@ -2,11 +2,14 @@
2#include "shadow_keep.h" 2#include "shadow_keep.h"
3 3
4#include <algorithm> 4#include <algorithm>
5#include <atomic>
5#include <bit> 6#include <bit>
6#include <cmath> 7#include <cmath>
7#include <cstdint> 8#include <cstdint>
8#include <deque> 9#include <deque>
10#include <limits>
9#include <map> 11#include <map>
12#include <memory>
10#include <utility> 13#include <utility>
11#include <vector> 14#include <vector>
12 15
@@ -414,4 +417,129 @@ inline std::uint32_t effective_min_mip(std::uint32_t first_mip, float min_lod) {
414 return (std::max)(first_mip, static_cast<std::uint32_t>(std::floor(min_lod))); 417 return (std::max)(first_mip, static_cast<std::uint32_t>(std::floor(min_lod)));
415} 418}
416 419
420// Per-submission streaming upload budget for unmapped first-time streamed textures and partial-mip upgrades.
421// Never defers CPU-mapped textures (e.g. multi-plane video frames that must land together), already-filled dynamic
422// texel updates (from == filled_from), or the first streamed texture of a pass (guaranteeing forward progress for
423// arbitrarily large textures and single-texture tests). Partial-mip upgrades already have valid lower mips clamped via
424// VK_EXT_image_view_min_lod, so they use half the stream budget to coalesce mip-by-mip arrivals across frames.
425class TitleStreamBudget {
426public:
427 explicit TitleStreamBudget(std::uint64_t budget_bytes) noexcept : budget_bytes_(budget_bytes) {}
428
429 bool allow(bool mapped, bool already_filled, bool partial_upgrade, std::uint64_t span) noexcept {
430 if (mapped) return true;
431 if (already_filled && !partial_upgrade) return true;
432 if (!budget_bytes_) return true;
433 const std::uint64_t limit = partial_upgrade ? (budget_bytes_ > 1 ? budget_bytes_ / 2 : 1) : budget_bytes_;
434 if (scheduled_bytes_ > 0 && (span > limit || scheduled_bytes_ > limit - span)) return false;
435 scheduled_bytes_ += span;
436 return true;
437 }
438
439 std::uint64_t scheduled_bytes() const noexcept { return scheduled_bytes_; }
440 std::uint64_t budget_bytes() const noexcept { return budget_bytes_; }
441
442private:
443 std::uint64_t budget_bytes_ = 0;
444 std::uint64_t scheduled_bytes_ = 0;
445};
446
447// Sorts, deduplicates, and coalesces page-aligned addresses into contiguous half-open [lo, hi) runs of `page_size`
448// bytes, emitting each run via `emit(lo, hi)`. Used by xmem::commit_pages_locked() to notify only affected title VAs.
449template<class Alloc, class Emit>
450void coalesce_page_runs(std::vector<std::uintptr_t, Alloc>& pages, std::uintptr_t page_size, Emit&& emit) {
451 if (pages.empty() || !page_size) return;
452 if (pages.size() > 1) {
453 std::sort(pages.begin(), pages.end());
454 pages.erase(std::unique(pages.begin(), pages.end()), pages.end());
455 }
456 constexpr std::uintptr_t kMax = (std::numeric_limits<std::uintptr_t>::max)();
457 for (std::size_t i = 0; i < pages.size();) {
458 const std::uintptr_t lo = pages[i];
459 std::uintptr_t hi = lo <= kMax - page_size ? lo + page_size : kMax;
460 ++i;
461 while (i < pages.size() && pages[i] == hi) {
462 hi = hi <= kMax - page_size ? hi + page_size : kMax;
463 ++i;
464 }
465 if (lo < hi) emit(lo, hi);
466 }
467}
468
469// Lock-free 2-bit-per-page commit/notification tracker for physical-page sections (xmem.cpp).
470// Bit 0 (`kCommitted`): scratch backing is committed in host memory.
471// Bit 1 (`kNotified`): all mapped/table VAs of this page have been notified via title_memory_map::changed(lo, hi).
472// Template Alloc allows xmem.cpp to back the word array from leonida::host_heap so fault-path callers never touch
473// the process heap.
474template<class Alloc = std::allocator<std::uint64_t>>
475class PageCommitBits {
476 using Word = std::atomic<std::uint64_t>;
477 using WordAlloc = typename std::allocator_traits<Alloc>::template rebind_alloc<Word>;
478 using WordTraits = std::allocator_traits<WordAlloc>;
479
480public:
481 PageCommitBits() = default;
482 ~PageCommitBits() { reset(); }
483 PageCommitBits(const PageCommitBits&) = delete;
484 PageCommitBits& operator=(const PageCommitBits&) = delete;
485
486 void init(std::size_t page_count, bool all_committed) {
487 reset();
488 if (!page_count) return;
489 const std::size_t words = (page_count - 1u) / 32u + 1u;
490 WordAlloc alloc;
491 Word* const mem = WordTraits::allocate(alloc, words);
492 const std::uint64_t fill = all_committed ? ~std::uint64_t(0) : std::uint64_t(0);
493 for (std::size_t i = 0; i < words; ++i) WordTraits::construct(alloc, mem + i, fill);
494 words_ = mem;
495 word_count_ = words;
496 page_count_ = page_count;
497 }
498
499 void reset() noexcept {
500 if (!words_) return;
501 WordAlloc alloc;
502 for (std::size_t i = 0; i < word_count_; ++i) WordTraits::destroy(alloc, words_ + i);
503 WordTraits::deallocate(alloc, words_, word_count_);
504 words_ = nullptr;
505 word_count_ = 0;
506 page_count_ = 0;
507 }
508
509 std::size_t size() const noexcept { return page_count_; }
510
511 bool is_committed(std::size_t idx) const noexcept {
512 if (idx >= page_count_) return false;
513 const std::uint64_t mask = std::uint64_t(1) << ((idx & 31u) * 2u);
514 return (words_[idx >> 5].load(std::memory_order_acquire) & mask) != 0;
515 }
516
517 bool is_notified(std::size_t idx) const noexcept {
518 if (idx >= page_count_) return false;
519 const std::uint64_t mask = std::uint64_t(2) << ((idx & 31u) * 2u);
520 return (words_[idx >> 5].load(std::memory_order_acquire) & mask) != 0;
521 }
522
523 // Marks page `idx` committed. Returns true if it transitioned from uncommitted -> committed.
524 bool mark_committed(std::size_t idx) noexcept {
525 if (idx >= page_count_) return false;
526 const std::uint64_t mask = std::uint64_t(1) << ((idx & 31u) * 2u);
527 return (words_[idx >> 5].fetch_or(mask, std::memory_order_acq_rel) & mask) == 0;
528 }
529
530 // Marks page `idx` both committed and notified. Returns true iff `kNotified` transitioned 0 -> 1.
531 bool mark_committed_and_notified(std::size_t idx) noexcept {
532 if (idx >= page_count_) return false;
533 const unsigned shift = static_cast<unsigned>((idx & 31u) * 2u);
534 const std::uint64_t both = std::uint64_t(3) << shift;
535 const std::uint64_t notified = std::uint64_t(2) << shift;
536 return (words_[idx >> 5].fetch_or(both, std::memory_order_acq_rel) & notified) == 0;
537 }
538
539private:
540 Word* words_ = nullptr;
541 std::size_t word_count_ = 0;
542 std::size_t page_count_ = 0;
543};
544
417} // namespace leonida 545} // namespace leonida