diff options
Diffstat (limited to 'runtime/src/title_texture_refresh.h')
| -rw-r--r-- | runtime/src/title_texture_refresh.h | 87 |
1 files changed, 84 insertions, 3 deletions
diff --git a/runtime/src/title_texture_refresh.h b/runtime/src/title_texture_refresh.h index ed7c0f2b6..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 | ||
| 9 | using TitleWriteRanges = std::vector<std::pair<std::uintptr_t, std::uintptr_t>>; | 11 | using 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. | ||
| 15 | class 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 | |||
| 23 | public: | ||
| 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. |
| 12 | inline void merge_title_writes(TitleWriteRanges& writes) { | 56 | inline 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. |
| 36 | template<class Registry> | 80 | template<class Registry> |
| 37 | std::vector<std::pair<std::uint64_t, typename Registry::mapped_type>> title_texture_snapshot(const Registry& textures) { | 81 | std::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 | ||
| @@ -60,4 +107,38 @@ void poll_title_textures(const std::vector<Entry>& entries, std::uint64_t& curso | |||
| 60 | } | 107 | } |
| 61 | } | 108 | } |
| 62 | 109 | ||
| 110 | // Visit the marked textures of a snapshot (mapped by the title, or known stale) under a time limit | ||
| 111 | // (LEONIDA_TITLE_EVERY_BUDGET_US). Those `urgent` answers true for (a mapped texture never filled yet) are all | ||
| 112 | // visited first, in placement order, whatever the limit. The others are visited | ||
| 113 | // from `cursor`, as poll_title_textures does for the unmarked ones, until `exhausted()` answers true after a visit; | ||
| 114 | // the next pass resumes at the entry after the last one visited, so every marked texture gets its turn however many | ||
| 115 | // there are. An entry skipped as unmarked costs no check. | ||
| 116 | template<class Entry, class Visit, class Urgent, class Exhausted> | ||
| 117 | void visit_marked_title_textures(const std::vector<Entry>& entries, std::uint64_t& cursor, Visit visit, Urgent urgent, | ||
| 118 | Exhausted exhausted) { | ||
| 119 | if (entries.empty()) return; | ||
| 120 | // Which entries are urgent is decided before any visit: a visit fills the texture, and it must not come round | ||
| 121 | // again in the rotation of the same pass. | ||
| 122 | std::vector<char> was_urgent(entries.size()); | ||
| 123 | for (std::size_t i = 0; i < entries.size(); ++i) { | ||
| 124 | const auto& texture = entries[i].second; | ||
| 125 | was_urgent[i] = (texture->mapped || texture->stale) && urgent(*texture); | ||
| 126 | } | ||
| 127 | for (std::size_t i = 0; i < entries.size(); ++i) | ||
| 128 | if (was_urgent[i]) visit(*entries[i].second); | ||
| 129 | const auto first = std::lower_bound(entries.begin(), entries.end(), cursor, | ||
| 130 | [](const Entry& entry, std::uint64_t at) { return entry.first < at; }); | ||
| 131 | auto index = std::size_t(first - entries.begin()); | ||
| 132 | if (index == entries.size()) index = 0; | ||
| 133 | for (auto left = entries.size(); left; --left) { | ||
| 134 | const auto& [address, texture] = entries[index]; | ||
| 135 | cursor = address + 1; | ||
| 136 | if ((texture->mapped || texture->stale) && !was_urgent[index]) { | ||
| 137 | visit(*texture); | ||
| 138 | if (exhausted()) break; | ||
| 139 | } | ||
| 140 | index = index + 1 == entries.size() ? 0 : index + 1; | ||
| 141 | } | ||
| 142 | } | ||
| 143 | |||
| 63 | } // namespace leonida | 144 | } // namespace leonida |