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-25 19:04:24 -0500
committerJan Trencansky <jan.trencansky@protonmail.com>2026-11-25 19:04:24 -0500
commit8d832fd10913064e1c8aec74191e3ba8f70211c8 (patch)
treee365199d7d86ebf592a5a0dc2f07cda4b74b58a7 /runtime/src/title_texture_refresh.h
parent5da3b6be3cefc1499452ba8c8370a50092674d61 (diff)
parentc50ed94b35b37b697ab9c5fd176f3738480b4db5 (diff)
Merge main into rt-title-every-budget (#2122)
#2098 split the every-pass loop of refresh_title_textures by mode: with LEONIDA_TEXTURE_REFRESH_ASYNC every persistent mapping is polled each pass and only stale unmapped textures are visited, without it all mapped or stale ones are. The async branch is kept as main has it. LEONIDA_TITLE_EVERY_BUDGET_US applies to the synchronous pass, the one before a submission on the submitting thread; under async the pass runs on the refresh worker and the limit does not apply (README says so). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'runtime/src/title_texture_refresh.h')
-rw-r--r--runtime/src/title_texture_refresh.h53
1 files changed, 50 insertions, 3 deletions
diff --git a/runtime/src/title_texture_refresh.h b/runtime/src/title_texture_refresh.h
index c3102aff7..feb8a0cc2 100644
--- a/runtime/src/title_texture_refresh.h
+++ b/runtime/src/title_texture_refresh.h
@@ -1,5 +1,7 @@
1#pragma once 1#pragma once
2#include <algorithm> 2#include <algorithm>
3#include <bit>
4#include <map>
3#include <cstdint> 5#include <cstdint>
4#include <utility> 6#include <utility>
5#include <vector> 7#include <vector>
@@ -8,6 +10,48 @@ namespace leonida {
8 10
9using TitleWriteRanges = std::vector<std::pair<std::uintptr_t, std::uintptr_t>>; 11using TitleWriteRanges = std::vector<std::pair<std::uintptr_t, std::uintptr_t>>;
10 12
13// Store each texture once, in the smallest aligned power-of-two page containing its CPU range.
14// A journal interval needs only the overlapping pages at each occupied size. Aliases keep distinct keys.
15class TitleTextureRangeIndex {
16 using Address = std::uint64_t;
17 struct Range { Address first, last; };
18 using PageKey = std::pair<Address, Address>; // page, registry key
19 std::map<unsigned, std::map<PageKey, Range>> pages_;
20 std::map<Address, std::pair<unsigned, Address>> location_;
21 static Address page(Address at, unsigned shift) { return shift == 64 ? 0 : at >> shift; }
22
23public:
24 void erase(Address key) {
25 const auto where = location_.find(key);
26 if (where == location_.end()) return;
27 const auto [shift, number] = where->second;
28 auto level = pages_.find(shift);
29 level->second.erase({number, key});
30 if (level->second.empty()) pages_.erase(level);
31 location_.erase(where);
32 }
33 void set(Address key, Address first, Address bytes) {
34 erase(key);
35 if (!bytes) return;
36 const Address last = first + std::min(bytes - 1, UINT64_MAX - first);
37 const unsigned shift = unsigned(std::bit_width(first ^ last));
38 const Address number = page(first, shift);
39 pages_[shift][{number, key}] = {first, last};
40 location_[key] = {shift, number};
41 }
42 template<class Visit>
43 void overlapping(Address from, Address to, Visit visit) const {
44 if (from >= to) return;
45 for (const auto& [shift, entries] : pages_) {
46 const Address last = page(to - 1, shift);
47 for (auto it = entries.lower_bound({page(from, shift), 0});
48 it != entries.end() && it->first.first <= last; ++it) {
49 if (it->second.first < to && from <= it->second.last) visit(it->first.second);
50 }
51 }
52 }
53};
54
11// Sorted, disjoint half-open intervals let each texture find its first possible writer by end address. 55// Sorted, disjoint half-open intervals let each texture find its first possible writer by end address.
12inline void merge_title_writes(TitleWriteRanges& writes) { 56inline void merge_title_writes(TitleWriteRanges& writes) {
13 std::sort(writes.begin(), writes.end()); 57 std::sort(writes.begin(), writes.end());
@@ -34,10 +78,13 @@ inline bool title_write_overlaps(const TitleWriteRanges& writes, std::uintptr_t
34// the walk would step through a freed node. The list holds a reference to each entry, so an entry erased during the 78// the walk would step through a freed node. The list holds a reference to each entry, so an entry erased during the
35// pass is still alive when its turn comes. 79// pass is still alive when its turn comes.
36template<class Registry> 80template<class Registry>
37std::vector<std::pair<std::uint64_t, typename Registry::mapped_type>> title_texture_snapshot(const Registry& textures) { 81std::vector<std::pair<std::uint64_t, typename Registry::mapped_type>> title_texture_snapshot(
82 const Registry& textures, std::uint64_t start = 0, std::size_t limit = SIZE_MAX) {
38 std::vector<std::pair<std::uint64_t, typename Registry::mapped_type>> entries; 83 std::vector<std::pair<std::uint64_t, typename Registry::mapped_type>> entries;
39 entries.reserve(textures.size()); 84 const auto count = std::min(textures.size(), limit);
40 for (const auto& [address, texture] : textures) entries.emplace_back(address, texture); 85 entries.reserve(count);
86 for (auto it = textures.lower_bound(start); it != textures.end() && entries.size() < count; ++it)
87 entries.emplace_back(it->first, it->second);
41 return entries; 88 return entries;
42} 89}
43 90