// ((Triforce, 11-26, the GPU audit's C22) One union of the captures' range lists for every caller, each time a list changes // (vk_memory.cpp capture_union_take_locked, refresh_pool_ranges_locked; RX_VK_CAP_UNION_SHARED, on; =0: as before; =check: // both ways, the differences counted; the lab knob nocapunion: as before in its phases). // // The captures' range lists are four: three whose pages the captures take (the mirrored imports, clipped to the game's // pools with RX_VK_CAP_ONLY_GPU=1; the texture storage; the small pools) and what the clip leaves out, which they do not // take, published for the replay's checks. capture_make (a submit's capture) and capture_ranges_merged (the replay's // collection) each kept a union of their own behind a version of their own, so a thread that does both, the game's // Presentation thread in the plain mode, built it twice at every change of a list: three sorts over ~4400 ranges, two // clips, and the left-out ranges published again, which has every thread copy them again at its next question // (cap_excl_hit, cap_excl_hit_gap with its gap memo, cap_excl_page, cap_excl_kept). Profiled at the Market: // capture_union_locked 2.2% of that thread's time, the sorts under it 2%; how often the lists change there is not measured // (the capture's 10 s line counts the builds now). // // What changes, and what does not: // - The union is built once a version of the lists (cap_ranges_ver, which every writer of a list moves under cap_ranges_mu, // where the union is kept too): get(). A caller still copies it into its own thread's list behind its own version: the // callers read their list after the lock is given back. The left-out ranges are published at that one build; between two // builds of one version nothing else can change them (cap_pool_born_now edits them and moves the version at once). // - The sorts are merges of runs: every list arrives sorted (the imports from an ordered map, the pools merged, the texture // storage merged, a clip's parts in its input's order), so a sort of two or three of them end to end is a merge. Each // run is asked whether it is sorted, and sorted when it is not (counted), so nothing is assumed: sort_runs gives what // std::sort gives (pairs of integers: equal ones are the same), and the join is ranges_merged's. build() is // capture_union_locked with those. // - The pool table's look kept a map from each small pool's start to the capture that saw it first, made again at every // look (a find and an insert a pool), which only RX_VK_CAPTURE_EAGER reads (off unless set): first_seen_needed(). // The lists decide which pages a capture takes, and a wrong one is stale pages in a mirror (a grey world, the ground's mist // gone for a frame): they must be the same vectors as before. tests/test_capunion.py compares them over random lists; // =check compares them in the game.) #pragma once #include #include #include #include #include namespace capunion { using Range = std::pair; using Ranges = std::vector; /// The switch: RX_VK_CAP_UNION_SHARED (nullptr or "": not set, on; "0": as before; "check": both ways, compared) enum Mode { OFF = 0, ON = 1, CHECK = 2 }; inline int mode(const char* env) { if (!env || !*env) return ON; if (!strcmp(env, "check")) return CHECK; return env[0] == '0' && !env[1] ? OFF : ON; } /// Whether the shared union and the runs are used now (knob: the lab knob nocapunion in one of its phases) inline bool shared(int mode, bool knob) { return mode != OFF && !knob; } /// Whether the pool table's look keeps each small pool's first capture: RX_VK_CAPTURE_EAGER's count reads it and nothing /// else does (shared_now false: kept as before) inline bool first_seen_needed(uint64_t eager_captures, bool shared_now) { return eager_captures != 0 || !shared_now; } /// v sorted: the ranges that overlap or touch joined, in place (ranges_merged's loop) inline void join(Ranges& v) { size_t m = 0; for (size_t i = 0; i < v.size(); i++) { if (m && v[i].first <= v[m - 1].second) v[m - 1].second = std::max(v[m - 1].second, v[i].second); else v[m++] = v[i]; } v.resize(m); } /// v sorted as std::sort sorts it, where v is three runs end to end, [0, cut1) [cut1, cut2) [cut2, size) (a cut past the /// end or before the other is brought in): a run that is not sorted is sorted (counted in *sorts), then the runs are merged inline void sort_runs(Ranges& v, size_t cut1, size_t cut2, uint64_t* sorts) { const size_t n = v.size(), c1 = std::min(cut1, n), c2 = std::min(std::max(cut2, c1), n); const size_t at[4] = {0, c1, c2, n}; for (int i = 0; i < 3; i++) { if (std::is_sorted(v.begin() + at[i], v.begin() + at[i + 1])) continue; std::sort(v.begin() + at[i], v.begin() + at[i + 1]); if (sorts) ++*sorts; } // (runs already in order end to end are left alone) if (c1 > 0 && c1 < c2 && v[c1] < v[c1 - 1]) std::inplace_merge(v.begin(), v.begin() + c1, v.begin() + c2); if (c2 > 0 && c2 < n && v[c2] < v[c2 - 1]) std::inplace_merge(v.begin(), v.begin() + c2, v.end()); } /// ranges_merged of v, where v is three runs (sort_runs) inline Ranges merged_runs(Ranges v, size_t cut1, size_t cut2, uint64_t* sorts) { sort_runs(v, cut1, cut2, sorts); join(v); return v; } /// a and b sorted and merged: a's parts inside b (in = true) or outside it (vk_memory.cpp's ranges_clip) inline Ranges clip(const Ranges& a, const Ranges& b, bool in) { Ranges out; size_t j = 0; for (const auto& r : a) { uint64_t at = r.first; while (j < b.size() && b[j].second <= at) j++; for (size_t k = j; k < b.size() && b[k].first < r.second; k++) { const uint64_t lo = std::max(at, b[k].first), hi = std::min(r.second, b[k].second); if (!in && lo > at) out.push_back({at, lo}); if (in && lo < hi) out.push_back({lo, hi}); at = std::max(at, hi); } if (!in && at < r.second) out.push_back({at, r.second}); } return out; } struct Built { Ranges uni; // (the captures' ranges) Ranges excl; // (the imports' parts outside every pool and texture storage: written only when excl_made) bool excl_made = false; // (only_gpu 1 or 2: the left-out ranges are to be published) }; /// capture_union_locked: the imports (clipped to the pools with only_gpu 1), the texture storage and the small pools, their /// union sorted and merged; with only_gpu 1 or 2 the imports' parts outside pools and tex too. sorts: the runs that did not /// arrive sorted, added inline void build(const Ranges& imports, const Ranges& pools, const Ranges& tex, const Ranges& small_pools, int only_gpu, Built& out, uint64_t* sorts) { Ranges imp = imports; out.excl_made = false; if (only_gpu) { const size_t whole = imp.size(); // (one run) imp = merged_runs(std::move(imp), whole, whole, sorts); Ranges keep; keep.reserve(pools.size() + tex.size()); keep = pools; keep.insert(keep.end(), tex.begin(), tex.end()); const size_t all = keep.size(); keep = merged_runs(std::move(keep), pools.size(), all, sorts); out.excl = clip(imp, keep, false); out.excl_made = true; if (only_gpu == 1) imp = clip(imp, pools, true); } const size_t cut1 = imp.size(), cut2 = cut1 + tex.size(); imp.reserve(cut2 + small_pools.size()); imp.insert(imp.end(), tex.begin(), tex.end()); imp.insert(imp.end(), small_pools.begin(), small_pools.end()); sort_runs(imp, cut1, cut2, sorts); join(imp); out.uni = std::move(imp); } /// The union kept for every caller (under the lists' lock), and the version of the lists it was built at (0: none yet; the /// lists' version starts at 1) struct Shared { uint64_t ver = 0; Ranges uni; }; /// The union at the lists' version ver: make(uni) builds it when the kept one is of another version (*built says which) template inline const Ranges& get(Shared& s, uint64_t ver, Make&& make, bool* built) { const bool stale = s.ver != ver; if (stale) make(s.uni), s.ver = ver; if (built) *built = stale; return s.uni; } } // namespace capunion