1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
|
#pragma once
#include <windows.h>
#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <limits>
#include "title_page_watch.h"
namespace leonida {
// Keep negative probes until the title changes the map or a writer reports this footprint. With cache_success,
// positive ones too: a caller that is told of every map change over the footprint (retry or sync_map) and reads it
// under a guard (a page gone since faults into a defer) need not walk the pages before every read.
class TextureReadability {
public:
template<class Probe>
bool check(std::uint64_t generation, bool cache_failure, Probe probe, bool cache_success = false) {
if (cache_failure && pending_ && !retry_ && generation_ == generation) return false;
if (cache_success && probed_ && !pending_ && !retry_ && generation_ == generation) return true;
generation_ = generation; // snapshot before the probe so a concurrent map change is noticed
map_generation_ = title_memory_map::generation();
retry_ = false;
probed_ = true;
pending_ = !probe();
return !pending_;
}
// Between refresh passes, check whether the map journal recorded a change over [cpu, cpu + size) since both
// this texture's last probe and the last pass's sweep.
void sync_map(std::uint64_t pass_generation, std::uintptr_t cpu, std::uint64_t size) {
if (!probed_ || retry_) return;
const std::uint64_t since = (std::max)(map_generation_, pass_generation);
if (title_memory_map::generation() == since) return;
const std::uintptr_t end = size > (std::numeric_limits<std::uintptr_t>::max)() - cpu
? (std::numeric_limits<std::uintptr_t>::max)()
: cpu + static_cast<std::uintptr_t>(size);
std::uint64_t now = 0;
bool overlapped = false;
if (!title_memory_map::changes_since(
since, now, cpu, end, [&](std::uintptr_t, std::uintptr_t) { overlapped = true; }) ||
overlapped) {
retry_ = true;
} else {
map_generation_ = now;
}
}
void defer() { pending_ = true; } // mapping disappeared between the probe and the read
void retry() { retry_ = true; }
bool pending() const { return pending_; }
bool needs_probe(std::uint64_t generation, bool cache_failure, bool cache_success = false) const noexcept {
if (cache_failure && pending_ && !retry_ && generation_ == generation) return false;
if (cache_success && probed_ && !pending_ && !retry_ && generation_ == generation) return false;
return true;
}
private:
std::uint64_t generation_ = 0;
std::uint64_t map_generation_ = 0;
bool pending_ = false, retry_ = false, probed_ = false;
};
using TexturePageCommitter = bool (*)(const void*, std::size_t);
// Short-lived per-pass cache of the last VirtualQuery region [lo, hi) (both readable MEM_COMMIT regions and
// non-guard unreadable regions such as MEM_RESERVE / MEM_FREE / PAGE_NOACCESS). Reused across VA-sorted texture
// probes only when `!commit` and no title_memory_map change overlapped [lo, hi).
struct TextureVqCache {
std::uintptr_t lo = 0;
std::uintptr_t hi = 0;
std::uint64_t gen = 0;
bool readable = false;
void reset() noexcept {
lo = hi = 0;
gen = 0;
readable = false;
}
};
// How many of the bytes from `base` are readable, up to the first page that is not (at most `size`). A page xmem's
// frame table holds (a stream pool page queue slot 31 gave a physical page: no host view, src/title_frames.h) is
// readable through its frame.
inline std::uint64_t texture_memory_readable_bytes(std::uintptr_t base, std::uint64_t size,
TexturePageCommitter commit = nullptr,
TextureVqCache* vq_cache = nullptr) {
if (!base || !size || size > (std::numeric_limits<std::uintptr_t>::max)() - base) return 0;
const auto end = base + size;
// Hoist the non-table-frame boundary `[at, host_stop)` outside the VirtualQuery loop so VMA fragmentation
// (64 KiB alternating RO/RW regions) does not re-acquire title_frames' g_frame_lock on every step.
std::uintptr_t host_stop = title_frames::may_hold(base, end) ? base : end;
for (auto at = base; at < end;) {
if (at >= host_stop) {
if (title_frames::may_hold(at, end)) {
bool backed = false;
host_stop = title_frames::run_end(at, end, backed);
if (backed) {
at = host_stop;
continue;
}
} else {
host_stop = end;
}
}
const auto stop = host_stop;
if (vq_cache && !commit && at >= vq_cache->lo && at < vq_cache->hi) {
const std::uint64_t cur = title_memory_map::generation();
bool clean = (cur == vq_cache->gen);
if (!clean) {
std::uint64_t now = cur;
bool overlapped = false;
if (title_memory_map::changes_since(vq_cache->gen, now, vq_cache->lo, vq_cache->hi,
[&](std::uintptr_t c_from, std::uintptr_t c_to) {
if (c_from < c_to) overlapped = true;
}) &&
!overlapped) {
vq_cache->gen = now;
clean = true;
} else {
vq_cache->hi = 0;
}
}
if (clean) {
if (!vq_cache->readable) return at - base;
at = (std::min)(stop, vq_cache->hi);
continue;
}
}
const std::uint64_t query_gen = (vq_cache && !commit) ? title_memory_map::generation() : 0;
MEMORY_BASIC_INFORMATION region{};
const auto* address = reinterpret_cast<const void*>(at);
if (!VirtualQuery(address, ®ion, sizeof(region))) return at - base;
// A persistent Map can name an xmem section whose physical pages commit on first touch.
// Only xmem may commit them. A bare reservation or an inaccessible page must still defer.
if (commit && region.State == MEM_RESERVE && region.Type == MEM_MAPPED) {
if (!commit(address, end - at) || !VirtualQuery(address, ®ion, sizeof(region))) return at - base;
}
bool region_readable = (region.State == MEM_COMMIT) && !(region.Protect & PAGE_GUARD);
if (region_readable) {
switch (region.Protect & 0xff) {
case PAGE_READONLY: case PAGE_READWRITE: case PAGE_WRITECOPY:
case PAGE_EXECUTE_READ: case PAGE_EXECUTE_READWRITE: case PAGE_EXECUTE_WRITECOPY: break;
default: region_readable = false; break;
}
}
const auto start = reinterpret_cast<std::uintptr_t>(region.BaseAddress);
if (at < start || at - start >= region.RegionSize) return at - base;
const bool cacheable_neg = !(region.State == MEM_COMMIT && (region.Protect & PAGE_GUARD));
if (vq_cache && !commit && (region_readable || cacheable_neg) &&
region.RegionSize <= (std::numeric_limits<std::uintptr_t>::max)() - start) {
vq_cache->lo = start;
vq_cache->hi = start + region.RegionSize;
vq_cache->gen = query_gen;
vq_cache->readable = region_readable;
}
if (!region_readable) return at - base;
const auto available = region.RegionSize - (at - start);
if (available >= stop - at) {
at = stop;
continue;
}
at += available;
}
return size;
}
// Check the whole footprint. A committed first page says nothing about a streamed mip farther on.
inline bool texture_memory_readable(std::uintptr_t base, std::uint64_t size, TexturePageCommitter commit = nullptr,
TextureVqCache* vq_cache = nullptr) {
return size && texture_memory_readable_bytes(base, size, commit, vq_cache) == size;
}
} // namespace leonida
|