upleb.uk

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

summaryrefslogtreecommitdiff
path: root/runtime/src/title_texture_memory.h
blob: ff9f004e0719281426ee636a3011852e09bf51c5 (plain)
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
#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_; }
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);

// 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) {
    if (!base || !size || size > (std::numeric_limits<std::uintptr_t>::max)() - base) return false;
    const auto end = base + size;
    for (auto at = base; at < end;) {
        MEMORY_BASIC_INFORMATION region{};
        const auto* address = reinterpret_cast<const void*>(at);
        if (!VirtualQuery(address, &region, sizeof(region))) return false;
        // 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, &region, sizeof(region))) return false;
        }
        if (region.State != MEM_COMMIT || (region.Protect & PAGE_GUARD)) return false;
        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: return false;
        }
        const auto start = reinterpret_cast<std::uintptr_t>(region.BaseAddress);
        if (at < start || at - start >= region.RegionSize) return false;
        const auto available = region.RegionSize - (at - start);
        if (available >= end - at) return true;
        at += available;
    }
    return true;
}

}  // namespace leonida