diff options
| author | Jan Trencansky <jan.trencansky@protonmail.com> | 2026-11-28 04:42:02 -0500 |
|---|---|---|
| committer | Jan Trencansky <jan.trencansky@protonmail.com> | 2026-11-28 04:42:02 -0500 |
| commit | 09f8cb66bfca1d3de7539287ccdbff631c32bdcd (patch) | |
| tree | 60ad8aa2278bb08d1ceef5b0f20d87016cae3a1b /runtime | |
| parent | 6bbeaa274a66909b397134b357821bd59932cddd (diff) | |
runtime: Round 9 WS2 — read+back cache/coalescing, single-lock title_frames, TitleTextureCells gap-bridging, 8 MiB pool & 3D linear mips
- Separate title_texel_writes from title_map_changes_ (retry t.memory only when pending/filled_from/!fills) and coalesce title_texel_writes::note() up to 16,384 spans before overflow
- Enable cache_success=guard + sync_map() for t.mapped while keeping cache_failure=guard&&!mapped
- Single-lock zero-shared_ptr title_frames::run_end & backed_batch hooks + Stage-2 TextureVqCache
- TitleTextureCells::arm_batch(): range_clean_since() at all 3 sites, kMaxBridgeGap=8 pin-guarded bridging, c->armed_map_gen, batched sync_tile_peers()
- Raise TitleImagePool::largest_slot to 8 MiB (16 MiB page cap), pool non-RT/non-DS deferred textures, best-fit TitleImageUpload::take_slot(), 16 MiB job.tiled retention, linear 3D mips>1, readable/back/arm/cpu_fallback_by[6] telemetry
AI-assisted: Antigravity (Gemini 4 Neon).
Diffstat (limited to 'runtime')
| -rw-r--r-- | runtime/shims/d3d12_title_image_upload.h | 20 | ||||
| -rw-r--r-- | runtime/shims/d3d12_vulkan_device.cpp | 502 | ||||
| -rw-r--r-- | runtime/shims/xmem.cpp | 45 | ||||
| -rw-r--r-- | runtime/src/title_frames.h | 39 | ||||
| -rw-r--r-- | runtime/src/title_page_watch.h | 37 | ||||
| -rw-r--r-- | runtime/src/title_texture_memory.h | 49 | ||||
| -rw-r--r-- | runtime/src/title_texture_watch.h | 180 | ||||
| -rw-r--r-- | runtime/src/vulkan_title_detile.h | 55 | ||||
| -rw-r--r-- | runtime/src/vulkan_title_image_pool.h | 13 | ||||
| -rw-r--r-- | runtime/test/title_texture_watch_test.cpp | 152 | ||||
| -rw-r--r-- | runtime/test/vk_title_detile_test.cpp | 198 | ||||
| -rw-r--r-- | runtime/test/vulkan_title_image_pool_test.cpp | 28 |
12 files changed, 1171 insertions, 147 deletions
diff --git a/runtime/shims/d3d12_title_image_upload.h b/runtime/shims/d3d12_title_image_upload.h index 8e37ab015..f157ccd61 100644 --- a/runtime/shims/d3d12_title_image_upload.h +++ b/runtime/shims/d3d12_title_image_upload.h | |||
| @@ -189,7 +189,7 @@ public: | |||
| 189 | regions_.push_back(copy); | 189 | regions_.push_back(copy); |
| 190 | } | 190 | } |
| 191 | } | 191 | } |
| 192 | Slot* slot = take_slot(d); | 192 | Slot* slot = take_slot(d, bytes); |
| 193 | if (!slot || !prepare(d, *slot, bytes)) return nullptr; | 193 | if (!slot || !prepare(d, *slot, bytes)) return nullptr; |
| 194 | slot->regions = regions_; | 194 | slot->regions = regions_; |
| 195 | slot->reserved = true; | 195 | slot->reserved = true; |
| @@ -659,20 +659,30 @@ private: | |||
| 659 | if (value_ && ready_) wait_value(d, value_); | 659 | if (value_ && ready_) wait_value(d, value_); |
| 660 | } | 660 | } |
| 661 | 661 | ||
| 662 | // A slot whose last transfer is done (looked up, not waited for); a new one while all are in flight, or the oldest | 662 | // Best-fit idle slot with capacity >= bytes (smallest fit to avoid burning large slots on small uploads); |
| 663 | // one's transfer waited for once there are kMaxSlots. | 663 | // otherwise the largest idle slot (grown in place so smaller slots stay reusable), a new slot up to kMaxSlots, |
| 664 | // or the oldest in-flight slot waited for. | ||
| 664 | template<class Device> | 665 | template<class Device> |
| 665 | Slot* take_slot(Device& d) { | 666 | Slot* take_slot(Device& d, VkDeviceSize bytes) { |
| 666 | const std::uint64_t reached = poll_ready(d); | 667 | const std::uint64_t reached = poll_ready(d); |
| 668 | Slot* best_fit = nullptr; | ||
| 669 | Slot* best_grow = nullptr; | ||
| 667 | Slot* oldest = nullptr; | 670 | Slot* oldest = nullptr; |
| 668 | for (auto& slot : slots_) { | 671 | for (auto& slot : slots_) { |
| 669 | if (slot->reserved) continue; | 672 | if (slot->reserved) continue; |
| 670 | if (!slot->value || slot->value <= reached) { | 673 | if (!slot->value || slot->value <= reached) { |
| 671 | slot->value = 0; | 674 | slot->value = 0; |
| 672 | return slot.get(); | 675 | if (slot->capacity >= bytes) { |
| 676 | if (!best_fit || slot->capacity < best_fit->capacity) best_fit = slot.get(); | ||
| 677 | } else if (!best_grow || slot->capacity > best_grow->capacity) { | ||
| 678 | best_grow = slot.get(); | ||
| 679 | } | ||
| 680 | continue; | ||
| 673 | } | 681 | } |
| 674 | if (!oldest || slot->value < oldest->value) oldest = slot.get(); | 682 | if (!oldest || slot->value < oldest->value) oldest = slot.get(); |
| 675 | } | 683 | } |
| 684 | if (best_fit) return best_fit; | ||
| 685 | if (best_grow) return best_grow; | ||
| 676 | if (slots_.size() < kMaxSlots) return slots_.emplace_back(std::make_unique<Slot>()).get(); | 686 | if (slots_.size() < kMaxSlots) return slots_.emplace_back(std::make_unique<Slot>()).get(); |
| 677 | return oldest && wait_slot(d, *oldest) ? oldest : nullptr; | 687 | return oldest && wait_slot(d, *oldest) ? oldest : nullptr; |
| 678 | } | 688 | } |
diff --git a/runtime/shims/d3d12_vulkan_device.cpp b/runtime/shims/d3d12_vulkan_device.cpp index 07c2bcf0f..445afc357 100644 --- a/runtime/shims/d3d12_vulkan_device.cpp +++ b/runtime/shims/d3d12_vulkan_device.cpp | |||
| @@ -1708,9 +1708,51 @@ struct TitleTextureCells { | |||
| 1708 | known_protect_gen_ = now; | 1708 | known_protect_gen_ = now; |
| 1709 | } | 1709 | } |
| 1710 | 1710 | ||
| 1711 | bool is_tile_pinned(std::uintptr_t tile) const noexcept { | ||
| 1712 | constexpr std::uintptr_t kTile = leonida::geometry_writes::tile_size; | ||
| 1713 | const std::uintptr_t tile_hi = (tile <= UINTPTR_MAX - kTile) ? (tile + kTile) : UINTPTR_MAX; | ||
| 1714 | for (const auto& [p_lo, p_hi] : run_pin_ranges_) { | ||
| 1715 | if (tile < p_hi && p_lo < tile_hi) return true; | ||
| 1716 | } | ||
| 1717 | return false; | ||
| 1718 | } | ||
| 1719 | |||
| 1720 | // True when no title_memory_map change after `since` overlapped [lo, hi). Sets `gen_out` to the generation | ||
| 1721 | // checked up to (leaf SRWLOCK shared read when generation moved). | ||
| 1722 | static bool range_clean_since(std::uint64_t since, std::uintptr_t lo, std::uintptr_t hi, std::uint64_t& gen_out) { | ||
| 1723 | const std::uint64_t cur = leonida::title_memory_map::generation(); | ||
| 1724 | if (cur == since) { | ||
| 1725 | gen_out = cur; | ||
| 1726 | return true; | ||
| 1727 | } | ||
| 1728 | bool overlapped = false; | ||
| 1729 | std::uint64_t now = cur; | ||
| 1730 | if (!leonida::title_memory_map::changes_since( | ||
| 1731 | since, now, lo, hi, [&](std::uintptr_t c_from, std::uintptr_t c_to) { | ||
| 1732 | if (c_from < c_to) overlapped = true; | ||
| 1733 | })) { | ||
| 1734 | return false; | ||
| 1735 | } | ||
| 1736 | gen_out = now; | ||
| 1737 | return !overlapped; | ||
| 1738 | } | ||
| 1739 | |||
| 1740 | struct ArmStats { | ||
| 1741 | std::uint64_t hot = 0; | ||
| 1742 | std::uint64_t already = 0; | ||
| 1743 | std::uint64_t backed = 0; | ||
| 1744 | std::uint64_t fast_vp = 0; | ||
| 1745 | std::uint64_t bridged = 0; | ||
| 1746 | std::uint64_t vq = 0; | ||
| 1747 | std::uint64_t vq_cache = 0; | ||
| 1748 | std::uint64_t vq_vp = 0; | ||
| 1749 | }; | ||
| 1750 | ArmStats arm_stats; | ||
| 1751 | |||
| 1711 | void arm_batch(const std::uintptr_t* tiles, Cell* const* cells, std::int8_t* results, std::size_t count) { | 1752 | void arm_batch(const std::uintptr_t* tiles, Cell* const* cells, std::int8_t* results, std::size_t count) { |
| 1712 | constexpr std::size_t kMaxBatchRunPages = 1024; // up to 64 MiB per contiguous VirtualProtect run | 1753 | constexpr std::size_t kMaxBatchRunPages = 1024; // up to 64 MiB per contiguous VirtualProtect run |
| 1713 | constexpr std::size_t kMaxLockBatchPages = 128; // pages per pin check + geometry_writes lock hold | 1754 | constexpr std::size_t kMaxLockBatchPages = 128; // pages per pin check + geometry_writes lock hold |
| 1755 | constexpr std::size_t kMaxBridgeGap = 8; // max already-armed cold gap tiles to bridge in one VP | ||
| 1714 | constexpr std::size_t kMaxKnownProtectEntries = 262144; | 1756 | constexpr std::size_t kMaxKnownProtectEntries = 262144; |
| 1715 | constexpr std::uintptr_t kTile = leonida::geometry_writes::tile_size; | 1757 | constexpr std::uintptr_t kTile = leonida::geometry_writes::tile_size; |
| 1716 | constexpr std::uintptr_t kHostPage = leonida::geometry_writes::page_size; | 1758 | constexpr std::uintptr_t kHostPage = leonida::geometry_writes::page_size; |
| @@ -1741,11 +1783,13 @@ struct TitleTextureCells { | |||
| 1741 | 1783 | ||
| 1742 | run_versions_.resize(batch_n); | 1784 | run_versions_.resize(batch_n); |
| 1743 | run_pinned_.assign(batch_n, 0); | 1785 | run_pinned_.assign(batch_n, 0); |
| 1744 | run_backed_.assign(batch_n, 0); | ||
| 1745 | run_eligible_.assign(batch_n, 0); | 1786 | run_eligible_.assign(batch_n, 0); |
| 1746 | run_failed_.assign(batch_n, 0); | 1787 | run_failed_.assign(batch_n, 0); |
| 1788 | run_pin_ranges_.clear(); | ||
| 1789 | run_need_backed_idx_.clear(); | ||
| 1790 | run_need_backed_vas_.clear(); | ||
| 1747 | 1791 | ||
| 1748 | // Snapshot versions BEFORE querying kernel pins, table-page backing, or taking geometry_writes::mutex. | 1792 | // Snapshot versions BEFORE querying kernel pins or taking geometry_writes::mutex. |
| 1749 | for (std::size_t k = 0; k < batch_n; ++k) | 1793 | for (std::size_t k = 0; k < batch_n; ++k) |
| 1750 | run_versions_[k] = batch_cells[k]->version.load(std::memory_order_acquire); | 1794 | run_versions_[k] = batch_cells[k]->version.load(std::memory_order_acquire); |
| 1751 | 1795 | ||
| @@ -1753,26 +1797,21 @@ struct TitleTextureCells { | |||
| 1753 | const std::uintptr_t last_tile = batch_tiles[batch_n - 1]; | 1797 | const std::uintptr_t last_tile = batch_tiles[batch_n - 1]; |
| 1754 | const std::uintptr_t pin_hi = (last_tile <= UINTPTR_MAX - kTile) ? (last_tile + kTile) : UINTPTR_MAX; | 1798 | const std::uintptr_t pin_hi = (last_tile <= UINTPTR_MAX - kTile) ? (last_tile + kTile) : UINTPTR_MAX; |
| 1755 | // Querying an OVERLAPPED pin can fault into xmem: NEVER call under geometry_writes::state().mutex. | 1799 | // Querying an OVERLAPPED pin can fault into xmem: NEVER call under geometry_writes::state().mutex. |
| 1800 | // Record every overlapping pin interval in run_pin_ranges_ so gap-bridging NEVER bridges a pinned tile. | ||
| 1756 | if (pin_lo < pin_hi) { | 1801 | if (pin_lo < pin_hi) { |
| 1757 | leonida::kernel_writes::for_each_pin(pin_lo, pin_hi, [&](std::uintptr_t p_lo, std::uintptr_t p_hi) { | 1802 | leonida::kernel_writes::for_each_pin(pin_lo, pin_hi, [&](std::uintptr_t p_lo, std::uintptr_t p_hi) { |
| 1803 | run_pin_ranges_.emplace_back(p_lo, p_hi); | ||
| 1758 | const std::uintptr_t first_tile = p_lo & ~(kTile - 1); | 1804 | const std::uintptr_t first_tile = p_lo & ~(kTile - 1); |
| 1759 | const std::uintptr_t* it = std::lower_bound(batch_tiles, batch_tiles + batch_n, first_tile); | 1805 | const std::uintptr_t* it = std::lower_bound(batch_tiles, batch_tiles + batch_n, first_tile); |
| 1760 | for (; it < batch_tiles + batch_n && *it < p_hi; ++it) | 1806 | for (; it < batch_tiles + batch_n && *it < p_hi; ++it) |
| 1761 | run_pinned_[std::size_t(it - batch_tiles)] = 1; | 1807 | run_pinned_[std::size_t(it - batch_tiles)] = 1; |
| 1762 | }); | 1808 | }); |
| 1763 | // Table-backed pages have no host view (their alias peers are armed separately in batch_reqs_). | ||
| 1764 | // Checked outside geometry_writes::state().mutex so g_frame_lock and s.mutex never nest; any | ||
| 1765 | // concurrent remap bumps c->version and is caught below by run_versions_[k] != c->version. | ||
| 1766 | if (leonida::title_frames::may_hold(pin_lo, pin_hi)) { | ||
| 1767 | for (std::size_t k = 0; k < batch_n; ++k) { | ||
| 1768 | if (!run_pinned_[k] && leonida::title_frames::backed(batch_tiles[k])) run_backed_[k] = 1; | ||
| 1769 | } | ||
| 1770 | } | ||
| 1771 | } | 1809 | } |
| 1810 | const bool may_hold_frames = pin_lo < pin_hi && leonida::title_frames::may_hold(pin_lo, pin_hi); | ||
| 1772 | 1811 | ||
| 1773 | auto& s = leonida::geometry_writes::state(); | 1812 | auto& s = leonida::geometry_writes::state(); |
| 1774 | { | 1813 | { |
| 1775 | std::lock_guard lock(s.mutex); | 1814 | std::unique_lock<std::mutex> lock(s.mutex); |
| 1776 | for (std::size_t k = 0; k < batch_n; ++k) { | 1815 | for (std::size_t k = 0; k < batch_n; ++k) { |
| 1777 | if (run_pinned_[k]) { | 1816 | if (run_pinned_[k]) { |
| 1778 | results[i + k] = 0; // busy: kernel write in progress (even on hot tiles) | 1817 | results[i + k] = 0; // busy: kernel write in progress (even on hot tiles) |
| @@ -1781,6 +1820,7 @@ struct TitleTextureCells { | |||
| 1781 | Cell* const c = batch_cells[k]; | 1820 | Cell* const c = batch_cells[k]; |
| 1782 | if (c->hot) { | 1821 | if (c->hot) { |
| 1783 | results[i + k] = 1; | 1822 | results[i + k] = 1; |
| 1823 | ++arm_stats.hot; | ||
| 1784 | continue; | 1824 | continue; |
| 1785 | } | 1825 | } |
| 1786 | if (run_versions_[k] != c->version.load(std::memory_order_relaxed)) { | 1826 | if (run_versions_[k] != c->version.load(std::memory_order_relaxed)) { |
| @@ -1789,7 +1829,7 @@ struct TitleTextureCells { | |||
| 1789 | } | 1829 | } |
| 1790 | // Zero-syscall fast path: all 16 host pages already guarded read-only (e.g. armed by | 1830 | // Zero-syscall fast path: all 16 host pages already guarded read-only (e.g. armed by |
| 1791 | // HotColdTracking::CellGuard earlier in this leader pass; guaranteed cleared by reopen(), | 1831 | // HotColdTracking::CellGuard earlier in this leader pass; guaranteed cleared by reopen(), |
| 1792 | // Event::replaced, TitleVirtualProtect, and unmap_page), or a table-backed page with no host view. | 1832 | // Event::replaced, TitleVirtualProtect, and unmap_page). |
| 1793 | bool all_armed = true; | 1833 | bool all_armed = true; |
| 1794 | for (const DWORD r : c->restore) { | 1834 | for (const DWORD r : c->restore) { |
| 1795 | if (!r) { | 1835 | if (!r) { |
| @@ -1797,15 +1837,63 @@ struct TitleTextureCells { | |||
| 1797 | break; | 1837 | break; |
| 1798 | } | 1838 | } |
| 1799 | } | 1839 | } |
| 1800 | if (all_armed || run_backed_[k]) { | 1840 | if (all_armed) { |
| 1801 | results[i + k] = 1; | 1841 | results[i + k] = 1; |
| 1842 | ++arm_stats.already; | ||
| 1802 | continue; | 1843 | continue; |
| 1803 | } | 1844 | } |
| 1804 | run_eligible_[k] = 1; | 1845 | if (may_hold_frames) { |
| 1846 | run_need_backed_idx_.push_back(k); | ||
| 1847 | run_need_backed_vas_.push_back(batch_tiles[k]); | ||
| 1848 | } else { | ||
| 1849 | run_eligible_[k] = 1; | ||
| 1850 | } | ||
| 1851 | } | ||
| 1852 | |||
| 1853 | // Batch-check table-page backing outside geometry_writes::state().mutex ONLY for tiles that are not | ||
| 1854 | // pinned, hot, or already all-armed (never nest g_frame_lock and s.mutex; single g_frame_lock hold | ||
| 1855 | // with zero shared_ptr refcount churn). Re-verify version/hot/all_armed after re-acquiring s.mutex. | ||
| 1856 | if (!run_need_backed_idx_.empty()) { | ||
| 1857 | lock.unlock(); | ||
| 1858 | run_need_backed_out_.resize(run_need_backed_vas_.size()); | ||
| 1859 | leonida::title_frames::backed_batch(run_need_backed_vas_.data(), run_need_backed_out_.data(), | ||
| 1860 | run_need_backed_vas_.size()); | ||
| 1861 | lock.lock(); | ||
| 1862 | for (std::size_t m = 0; m < run_need_backed_idx_.size(); ++m) { | ||
| 1863 | const std::size_t k = run_need_backed_idx_[m]; | ||
| 1864 | Cell* const c = batch_cells[k]; | ||
| 1865 | if (c->hot) { | ||
| 1866 | results[i + k] = 1; | ||
| 1867 | ++arm_stats.hot; | ||
| 1868 | continue; | ||
| 1869 | } | ||
| 1870 | if (run_versions_[k] != c->version.load(std::memory_order_relaxed)) { | ||
| 1871 | results[i + k] = -1; // write/remap/pin raced during backed_batch | ||
| 1872 | continue; | ||
| 1873 | } | ||
| 1874 | bool all_armed = true; | ||
| 1875 | for (const DWORD r : c->restore) { | ||
| 1876 | if (!r) { | ||
| 1877 | all_armed = false; | ||
| 1878 | break; | ||
| 1879 | } | ||
| 1880 | } | ||
| 1881 | if (all_armed) { | ||
| 1882 | results[i + k] = 1; | ||
| 1883 | ++arm_stats.already; | ||
| 1884 | continue; | ||
| 1885 | } | ||
| 1886 | if (run_need_backed_out_[m]) { | ||
| 1887 | results[i + k] = 1; | ||
| 1888 | ++arm_stats.backed; | ||
| 1889 | continue; | ||
| 1890 | } | ||
| 1891 | run_eligible_[k] = 1; | ||
| 1892 | } | ||
| 1805 | } | 1893 | } |
| 1806 | 1894 | ||
| 1807 | // Short-lived VirtualQuery cache within this lock hold: reuse uniform writable or already-read-only | 1895 | // Short-lived VirtualQuery cache within this lock hold: reuse uniform writable or already-read-only |
| 1808 | // MEM_COMMIT regions across non-contiguous tiles while title_memory_map::generation() is unchanged. | 1896 | // MEM_COMMIT regions across non-contiguous tiles while no map change overlapped [p, cached_hi). |
| 1809 | std::uintptr_t cached_lo = 0; | 1897 | std::uintptr_t cached_lo = 0; |
| 1810 | std::uintptr_t cached_hi = 0; | 1898 | std::uintptr_t cached_hi = 0; |
| 1811 | DWORD cached_protect = 0; | 1899 | DWORD cached_protect = 0; |
| @@ -1822,14 +1910,109 @@ struct TitleTextureCells { | |||
| 1822 | batch_tiles[sub_end] == batch_tiles[sub_end - 1] + kTile) { | 1910 | batch_tiles[sub_end] == batch_tiles[sub_end - 1] + kTile) { |
| 1823 | ++sub_end; | 1911 | ++sub_end; |
| 1824 | } | 1912 | } |
| 1913 | |||
| 1914 | // Gap-bridging fast path: coalesce scattered eligible tiles separated by <= kMaxBridgeGap | ||
| 1915 | // already-armed, unpinned cold tiles sharing identical known_protect/known_guarded/mapping and | ||
| 1916 | // uniform restore[0..15] == kp. Falls back to eligible-only [sub_start, sub_end) on any failure | ||
| 1917 | // or unexpected old protect (with mandatory VirtualProtect(old) rollback). | ||
| 1918 | if (has_known_protect_) { | ||
| 1919 | const auto it0 = known_protect_.find(batch_tiles[sub_start]); | ||
| 1920 | if (it0 != known_protect_.end() && it0->second.protect != 0 && | ||
| 1921 | it0->second.mapping == batch_cells[sub_start]->mapping.load(std::memory_order_acquire)) { | ||
| 1922 | const DWORD kp = it0->second.protect; | ||
| 1923 | const DWORD kg = it0->second.guarded; | ||
| 1924 | const std::uintptr_t span_lo = batch_tiles[sub_start]; | ||
| 1925 | std::size_t bridge_end = sub_start + 1; | ||
| 1926 | while (bridge_end < batch_n && run_eligible_[bridge_end]) { | ||
| 1927 | const std::uintptr_t prev_va = batch_tiles[bridge_end - 1]; | ||
| 1928 | const std::uintptr_t next_va = batch_tiles[bridge_end]; | ||
| 1929 | if (prev_va > UINTPTR_MAX - kTile || next_va > UINTPTR_MAX - kTile) break; | ||
| 1930 | const std::uintptr_t prev_end = prev_va + kTile; | ||
| 1931 | if (next_va < prev_end) break; | ||
| 1932 | const std::uintptr_t gap_bytes = next_va - prev_end; | ||
| 1933 | if (gap_bytes > kMaxBridgeGap * kTile) break; | ||
| 1934 | if ((next_va + kTile - span_lo) > kMaxBatchRunPages * kTile) break; | ||
| 1935 | const auto itn = known_protect_.find(next_va); | ||
| 1936 | if (itn == known_protect_.end() || itn->second.protect != kp || | ||
| 1937 | itn->second.guarded != kg || | ||
| 1938 | itn->second.mapping != | ||
| 1939 | batch_cells[bridge_end]->mapping.load(std::memory_order_acquire)) | ||
| 1940 | break; | ||
| 1941 | bool gap_ok = true; | ||
| 1942 | for (std::uintptr_t gap_va = prev_end; gap_va < next_va; gap_va += kTile) { | ||
| 1943 | if (is_tile_pinned(gap_va)) { | ||
| 1944 | gap_ok = false; | ||
| 1945 | break; | ||
| 1946 | } | ||
| 1947 | const auto git = known_protect_.find(gap_va); | ||
| 1948 | if (git == known_protect_.end() || git->second.protect != kp || | ||
| 1949 | git->second.guarded != kg) { | ||
| 1950 | gap_ok = false; | ||
| 1951 | break; | ||
| 1952 | } | ||
| 1953 | const auto cit = s.cells.find(gap_va); | ||
| 1954 | if (cit == s.cells.end()) { | ||
| 1955 | gap_ok = false; | ||
| 1956 | break; | ||
| 1957 | } | ||
| 1958 | const Cell& gc = cit->second; | ||
| 1959 | if (gc.hot || | ||
| 1960 | git->second.mapping != gc.mapping.load(std::memory_order_acquire)) { | ||
| 1961 | gap_ok = false; | ||
| 1962 | break; | ||
| 1963 | } | ||
| 1964 | for (const DWORD r : gc.restore) { | ||
| 1965 | if (r != kp) { | ||
| 1966 | gap_ok = false; | ||
| 1967 | break; | ||
| 1968 | } | ||
| 1969 | } | ||
| 1970 | if (!gap_ok) break; | ||
| 1971 | } | ||
| 1972 | if (!gap_ok) break; | ||
| 1973 | ++bridge_end; | ||
| 1974 | } | ||
| 1975 | if (bridge_end > sub_end) { | ||
| 1976 | const std::uintptr_t span_hi = batch_tiles[bridge_end - 1] + kTile; | ||
| 1977 | std::uint64_t verified_gen = 0; | ||
| 1978 | if (range_clean_since(known_protect_gen_, span_lo, span_hi, verified_gen)) { | ||
| 1979 | DWORD old{}; | ||
| 1980 | if (VirtualProtect(reinterpret_cast<void*>(span_lo), span_hi - span_lo, kg, | ||
| 1981 | &old)) { | ||
| 1982 | if (old == kp || old == kg) { | ||
| 1983 | const std::uint64_t armed_gen = | ||
| 1984 | (leonida::title_memory_map::generation() == verified_gen) | ||
| 1985 | ? verified_gen | ||
| 1986 | : Cell::kNoMapGen; | ||
| 1987 | for (std::size_t k = sub_start; k < bridge_end; ++k) { | ||
| 1988 | batch_cells[k]->restore.fill(kp); | ||
| 1989 | batch_cells[k]->armed_map_gen = armed_gen; | ||
| 1990 | results[i + k] = 1; | ||
| 1991 | } | ||
| 1992 | ++arm_stats.fast_vp; | ||
| 1993 | ++arm_stats.bridged; | ||
| 1994 | if (cached_hi && span_lo >= cached_lo && span_hi <= cached_hi) | ||
| 1995 | cached_lo = span_hi; | ||
| 1996 | sub_start = bridge_end; | ||
| 1997 | continue; | ||
| 1998 | } | ||
| 1999 | // Unexpected prior protection: restore `old` and fall back to eligible-only. | ||
| 2000 | DWORD dummy{}; | ||
| 2001 | VirtualProtect(reinterpret_cast<void*>(span_lo), span_hi - span_lo, old, | ||
| 2002 | &dummy); | ||
| 2003 | } | ||
| 2004 | } | ||
| 2005 | } | ||
| 2006 | } | ||
| 2007 | } | ||
| 2008 | |||
| 1825 | const std::uintptr_t sub_lo = batch_tiles[sub_start]; | 2009 | const std::uintptr_t sub_lo = batch_tiles[sub_start]; |
| 1826 | const std::uintptr_t sub_hi = sub_lo + (sub_end - sub_start) * kTile; | 2010 | const std::uintptr_t sub_hi = sub_lo + (sub_end - sub_start) * kTile; |
| 1827 | 2011 | ||
| 1828 | for (std::uintptr_t p = sub_lo; p < sub_hi;) { | 2012 | for (std::uintptr_t p = sub_lo; p < sub_hi;) { |
| 1829 | // Fast path: tile-aligned repeat arm with verified uniform protection and unchanged map | 2013 | // Fast path: tile-aligned repeat arm with verified uniform protection and no map change |
| 1830 | // generation & cell mapping -> VirtualProtect directly with 0 VirtualQuery syscalls. | 2014 | // overlapping [p, end) since known_protect_gen_ -> VirtualProtect directly with 0 VirtualQuery. |
| 1831 | if ((p & (kTile - 1)) == 0 && sub_hi - p >= kTile && has_known_protect_ && | 2015 | if ((p & (kTile - 1)) == 0 && sub_hi - p >= kTile && has_known_protect_) { |
| 1832 | leonida::title_memory_map::generation() == known_protect_gen_) { | ||
| 1833 | const std::size_t tile_k = sub_start + std::size_t((p - sub_lo) / kTile); | 2016 | const std::size_t tile_k = sub_start + std::size_t((p - sub_lo) / kTile); |
| 1834 | const auto it0 = known_protect_.find(batch_tiles[tile_k]); | 2017 | const auto it0 = known_protect_.find(batch_tiles[tile_k]); |
| 1835 | if (it0 != known_protect_.end() && it0->second.protect != 0 && | 2018 | if (it0 != known_protect_.end() && it0->second.protect != 0 && |
| @@ -1847,22 +2030,33 @@ struct TitleTextureCells { | |||
| 1847 | ++fast_end; | 2030 | ++fast_end; |
| 1848 | } | 2031 | } |
| 1849 | const std::uintptr_t end = p + (fast_end - tile_k) * kTile; | 2032 | const std::uintptr_t end = p + (fast_end - tile_k) * kTile; |
| 1850 | DWORD old{}; | 2033 | std::uint64_t verified_gen = 0; |
| 1851 | if (VirtualProtect(reinterpret_cast<void*>(p), end - p, kg, &old)) { | 2034 | if (range_clean_since(known_protect_gen_, p, end, verified_gen)) { |
| 1852 | if (old == kp || old == kg) { | 2035 | DWORD old{}; |
| 1853 | for (std::size_t t = tile_k; t < fast_end; ++t) | 2036 | if (VirtualProtect(reinterpret_cast<void*>(p), end - p, kg, &old)) { |
| 1854 | batch_cells[t]->restore.fill(kp); | 2037 | if (old == kp || old == kg) { |
| 1855 | if (cached_hi && p >= cached_lo && end <= cached_hi) cached_lo = end; | 2038 | const std::uint64_t armed_gen = |
| 1856 | p = end; | 2039 | (leonida::title_memory_map::generation() == verified_gen) |
| 1857 | continue; | 2040 | ? verified_gen |
| 2041 | : Cell::kNoMapGen; | ||
| 2042 | for (std::size_t t = tile_k; t < fast_end; ++t) { | ||
| 2043 | batch_cells[t]->restore.fill(kp); | ||
| 2044 | batch_cells[t]->armed_map_gen = armed_gen; | ||
| 2045 | } | ||
| 2046 | ++arm_stats.fast_vp; | ||
| 2047 | if (cached_hi && p >= cached_lo && end <= cached_hi) cached_lo = end; | ||
| 2048 | p = end; | ||
| 2049 | continue; | ||
| 2050 | } | ||
| 2051 | // Unexpected prior protection (caller bypassed title_memory_map::changed): | ||
| 2052 | // restore `old` before falling through to VirtualQuery + VirtualProtect. | ||
| 2053 | DWORD dummy{}; | ||
| 2054 | VirtualProtect(reinterpret_cast<void*>(p), end - p, old, &dummy); | ||
| 1858 | } | 2055 | } |
| 1859 | // Unexpected prior protection (caller bypassed title_memory_map::changed): | 2056 | for (std::size_t t = tile_k; t < fast_end; ++t) |
| 1860 | // restore `old` before falling through to VirtualQuery + VirtualProtect. | 2057 | known_protect_.erase(batch_tiles[t]); |
| 1861 | DWORD dummy{}; | 2058 | cached_hi = 0; |
| 1862 | VirtualProtect(reinterpret_cast<void*>(p), end - p, old, &dummy); | ||
| 1863 | } | 2059 | } |
| 1864 | for (std::size_t t = tile_k; t < fast_end; ++t) known_protect_.erase(batch_tiles[t]); | ||
| 1865 | cached_hi = 0; | ||
| 1866 | } | 2060 | } |
| 1867 | } | 2061 | } |
| 1868 | 2062 | ||
| @@ -1874,8 +2068,10 @@ struct TitleTextureCells { | |||
| 1874 | std::uint64_t query_gen = 0; | 2068 | std::uint64_t query_gen = 0; |
| 1875 | bool from_cache = false; | 2069 | bool from_cache = false; |
| 1876 | 2070 | ||
| 2071 | std::uint64_t cache_now = 0; | ||
| 1877 | if (p >= cached_lo && p < cached_hi && | 2072 | if (p >= cached_lo && p < cached_hi && |
| 1878 | leonida::title_memory_map::generation() == cached_gen) { | 2073 | range_clean_since(cached_gen, p, cached_hi, cache_now)) { |
| 2074 | cached_gen = cache_now; | ||
| 1879 | region_base = cached_lo; | 2075 | region_base = cached_lo; |
| 1880 | region_end = cached_hi; | 2076 | region_end = cached_hi; |
| 1881 | state = MEM_COMMIT; | 2077 | state = MEM_COMMIT; |
| @@ -1883,8 +2079,10 @@ struct TitleTextureCells { | |||
| 1883 | guarded = cached_guarded; | 2079 | guarded = cached_guarded; |
| 1884 | query_gen = cached_gen; | 2080 | query_gen = cached_gen; |
| 1885 | from_cache = true; | 2081 | from_cache = true; |
| 2082 | ++arm_stats.vq_cache; | ||
| 1886 | } else { | 2083 | } else { |
| 1887 | query_gen = leonida::title_memory_map::generation(); | 2084 | query_gen = leonida::title_memory_map::generation(); |
| 2085 | ++arm_stats.vq; | ||
| 1888 | MEMORY_BASIC_INFORMATION m{}; | 2086 | MEMORY_BASIC_INFORMATION m{}; |
| 1889 | if (!VirtualQuery(reinterpret_cast<void*>(p), &m, sizeof m)) { | 2087 | if (!VirtualQuery(reinterpret_cast<void*>(p), &m, sizeof m)) { |
| 1890 | const std::size_t tile_k = sub_start + std::size_t((p - sub_lo) / kTile); | 2088 | const std::size_t tile_k = sub_start + std::size_t((p - sub_lo) / kTile); |
| @@ -1922,26 +2120,30 @@ struct TitleTextureCells { | |||
| 1922 | } | 2120 | } |
| 1923 | if (state == MEM_COMMIT && !(protect & PAGE_GUARD) && guarded) { | 2121 | if (state == MEM_COMMIT && !(protect & PAGE_GUARD) && guarded) { |
| 1924 | DWORD old{}; | 2122 | DWORD old{}; |
| 2123 | ++arm_stats.vq_vp; | ||
| 1925 | if (VirtualProtect(reinterpret_cast<void*>(p), end - p, guarded, &old)) { | 2124 | if (VirtualProtect(reinterpret_cast<void*>(p), end - p, guarded, &old)) { |
| 1926 | for (std::uintptr_t q = p; q < end; q += kHostPage) { | 2125 | for (std::uintptr_t q = p; q < end; q += kHostPage) { |
| 1927 | const std::size_t tile_k = sub_start + std::size_t((q - sub_lo) / kTile); | 2126 | const std::size_t tile_k = sub_start + std::size_t((q - sub_lo) / kTile); |
| 1928 | const std::size_t sub_p = std::size_t((q & (kTile - 1)) / kHostPage); | 2127 | const std::size_t sub_p = std::size_t((q & (kTile - 1)) / kHostPage); |
| 1929 | batch_cells[tile_k]->restore[sub_p] = protect; | 2128 | batch_cells[tile_k]->restore[sub_p] = protect; |
| 2129 | batch_cells[tile_k]->armed_map_gen = Cell::kNoMapGen; | ||
| 1930 | } | 2130 | } |
| 1931 | if (query_gen == known_protect_gen_ && | 2131 | const std::uintptr_t first_full = (p + kTile - 1) & ~(kTile - 1); |
| 1932 | leonida::title_memory_map::generation() == known_protect_gen_) { | 2132 | const std::uintptr_t last_full = end & ~(kTile - 1); |
| 1933 | const std::uintptr_t first_full = (p + kTile - 1) & ~(kTile - 1); | 2133 | std::uint64_t after_gen = 0; |
| 1934 | const std::uintptr_t last_full = end & ~(kTile - 1); | 2134 | if (first_full < last_full && region_base <= first_full && region_end >= last_full && |
| 1935 | if (first_full < last_full && region_base <= first_full && | 2135 | range_clean_since(query_gen, first_full, last_full, after_gen)) { |
| 1936 | region_end >= last_full) { | 2136 | const std::uint64_t armed_gen = |
| 1937 | for (std::uintptr_t t = first_full; t < last_full; t += kTile) { | 2137 | (leonida::title_memory_map::generation() == after_gen) ? after_gen |
| 1938 | const std::size_t tile_k = sub_start + std::size_t((t - sub_lo) / kTile); | 2138 | : Cell::kNoMapGen; |
| 1939 | known_protect_[batch_tiles[tile_k]] = { | 2139 | for (std::uintptr_t t = first_full; t < last_full; t += kTile) { |
| 1940 | batch_cells[tile_k]->mapping.load(std::memory_order_acquire), protect, | 2140 | const std::size_t tile_k = sub_start + std::size_t((t - sub_lo) / kTile); |
| 1941 | guarded}; | 2141 | known_protect_[batch_tiles[tile_k]] = { |
| 1942 | } | 2142 | batch_cells[tile_k]->mapping.load(std::memory_order_acquire), protect, |
| 1943 | has_known_protect_ = true; | 2143 | guarded}; |
| 2144 | batch_cells[tile_k]->armed_map_gen = armed_gen; | ||
| 1944 | } | 2145 | } |
| 2146 | has_known_protect_ = true; | ||
| 1945 | } | 2147 | } |
| 1946 | if (cached_hi) cached_lo = end; | 2148 | if (cached_hi) cached_lo = end; |
| 1947 | } else if (from_cache) { | 2149 | } else if (from_cache) { |
| @@ -1952,10 +2154,12 @@ struct TitleTextureCells { | |||
| 1952 | for (std::uintptr_t t = p; t < end;) { | 2154 | for (std::uintptr_t t = p; t < end;) { |
| 1953 | const std::uintptr_t t_end = (std::min)(end, (t & ~(kTile - 1)) + kTile); | 2155 | const std::uintptr_t t_end = (std::min)(end, (t & ~(kTile - 1)) + kTile); |
| 1954 | const std::size_t tile_k = sub_start + std::size_t((t - sub_lo) / kTile); | 2156 | const std::size_t tile_k = sub_start + std::size_t((t - sub_lo) / kTile); |
| 2157 | ++arm_stats.vq_vp; | ||
| 1955 | if (VirtualProtect(reinterpret_cast<void*>(t), t_end - t, guarded, &old)) { | 2158 | if (VirtualProtect(reinterpret_cast<void*>(t), t_end - t, guarded, &old)) { |
| 1956 | for (std::uintptr_t q = t; q < t_end; q += kHostPage) { | 2159 | for (std::uintptr_t q = t; q < t_end; q += kHostPage) { |
| 1957 | const std::size_t sub_p = std::size_t((q & (kTile - 1)) / kHostPage); | 2160 | const std::size_t sub_p = std::size_t((q & (kTile - 1)) / kHostPage); |
| 1958 | batch_cells[tile_k]->restore[sub_p] = protect; | 2161 | batch_cells[tile_k]->restore[sub_p] = protect; |
| 2162 | batch_cells[tile_k]->armed_map_gen = Cell::kNoMapGen; | ||
| 1959 | } | 2163 | } |
| 1960 | } else { | 2164 | } else { |
| 1961 | run_failed_[tile_k] = 1; | 2165 | run_failed_[tile_k] = 1; |
| @@ -1987,9 +2191,18 @@ struct TitleTextureCells { | |||
| 1987 | bool has_known_protect_ = false; | 2191 | bool has_known_protect_ = false; |
| 1988 | std::vector<std::uint64_t> run_versions_; | 2192 | std::vector<std::uint64_t> run_versions_; |
| 1989 | std::vector<std::uint8_t> run_pinned_; | 2193 | std::vector<std::uint8_t> run_pinned_; |
| 1990 | std::vector<std::uint8_t> run_backed_; | ||
| 1991 | std::vector<std::uint8_t> run_eligible_; | 2194 | std::vector<std::uint8_t> run_eligible_; |
| 1992 | std::vector<std::uint8_t> run_failed_; | 2195 | std::vector<std::uint8_t> run_failed_; |
| 2196 | std::vector<std::pair<std::uintptr_t, std::uintptr_t>> run_pin_ranges_; | ||
| 2197 | std::vector<std::size_t> run_need_backed_idx_; | ||
| 2198 | std::vector<std::uintptr_t> run_need_backed_vas_; | ||
| 2199 | std::vector<std::uint8_t> run_need_backed_out_; | ||
| 2200 | void acquire_batch(const std::uintptr_t* tiles, Cell** out_cells, std::size_t count) { | ||
| 2201 | leonida::geometry_writes::acquire_batch(tiles, out_cells, count); | ||
| 2202 | } | ||
| 2203 | void release_batch(const std::pair<std::uintptr_t, bool>* items, std::size_t count) { | ||
| 2204 | leonida::geometry_writes::release_batch(items, count); | ||
| 2205 | } | ||
| 1993 | void aliases(std::uintptr_t tile, std::vector<std::uintptr_t>& out) { | 2206 | void aliases(std::uintptr_t tile, std::vector<std::uintptr_t>& out) { |
| 1994 | if (const auto f = leonida::geometry_writes::aliases.load(std::memory_order_acquire)) f(tile, out); | 2207 | if (const auto f = leonida::geometry_writes::aliases.load(std::memory_order_acquire)) f(tile, out); |
| 1995 | } | 2208 | } |
| @@ -3690,6 +3903,9 @@ struct DeviceState { | |||
| 3690 | std::uint64_t value = 0; | 3903 | std::uint64_t value = 0; |
| 3691 | std::uint64_t mirror_hash = 0; | 3904 | std::uint64_t mirror_hash = 0; |
| 3692 | int tile_cmp = 0; // +1 unchanged, -1 changed, 0 unknown/fallback | 3905 | int tile_cmp = 0; // +1 unchanged, -1 changed, 0 unknown/fallback |
| 3906 | // Why try_full_view() rejected this item (for cpu_fallback_by[6] telemetry): | ||
| 3907 | // 0 = none/accepted, 1 = unmirrored_plain, 2 = unmirrored_other, 3 = in_feed, 4 = pending_bind | ||
| 3908 | std::uint8_t reject_reason = 0; | ||
| 3693 | // Internal scratch for page_mirror_check_batch(): | 3909 | // Internal scratch for page_mirror_check_batch(): |
| 3694 | enum class Mode : std::uint8_t { none, full, partial_tiles } mode = Mode::none; | 3910 | enum class Mode : std::uint8_t { none, full, partial_tiles } mode = Mode::none; |
| 3695 | std::uint64_t mirror_va = 0; | 3911 | std::uint64_t mirror_va = 0; |
| @@ -14024,14 +14240,17 @@ public: | |||
| 14024 | bool deferred_stream() const { return deferred_stream_; } | 14240 | bool deferred_stream() const { return deferred_stream_; } |
| 14025 | 14241 | ||
| 14026 | VkDeviceSize unbound_bytes() const { return image_backing_bytes() ? 0 : deferred_bytes_; } | 14242 | VkDeviceSize unbound_bytes() const { return image_backing_bytes() ? 0 : deferred_bytes_; } |
| 14243 | bool pooled_image() const { return pooled_image_ != nullptr; } | ||
| 14027 | 14244 | ||
| 14028 | private: | 14245 | private: |
| 14029 | bool bind_placed_memory() { | 14246 | bool bind_placed_memory() { |
| 14030 | VkMemoryRequirements requirements{}; | 14247 | VkMemoryRequirements requirements{}; |
| 14031 | std::uint32_t type = UINT32_MAX; | 14248 | std::uint32_t type = UINT32_MAX; |
| 14032 | if (!image_backing_bytes_ && !bind_texture_memory(false, deferred_types_, requirements, type)) { | 14249 | const bool pool = !(desc_.Flags & (D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET | |
| 14250 | D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL)); | ||
| 14251 | if (!image_backing_bytes_ && !bind_texture_memory(pool, deferred_types_, requirements, type)) { | ||
| 14033 | if (!(requirements.memoryTypeBits & ~deferred_types_) || | 14252 | if (!(requirements.memoryTypeBits & ~deferred_types_) || |
| 14034 | !bind_texture_memory(false, ~deferred_types_, requirements, type)) return false; | 14253 | !bind_texture_memory(pool, ~deferred_types_, requirements, type)) return false; |
| 14035 | } | 14254 | } |
| 14036 | if (shared_zero_) { | 14255 | if (shared_zero_) { |
| 14037 | if (host_transfer_ && !host_initialize()) return false; | 14256 | if (host_transfer_ && !host_initialize()) return false; |
| @@ -18752,11 +18971,30 @@ void DeviceState::page_mirror_check_batch( | |||
| 18752 | if (mirror.dirty(p)) need_upload = true; | 18971 | if (mirror.dirty(p)) need_upload = true; |
| 18753 | }; | 18972 | }; |
| 18754 | 18973 | ||
| 18974 | const auto record_reject = [](TitleMirrorCheckItem& item, std::uint8_t reason) { | ||
| 18975 | // Priority: in_feed (3) / pending_bind (4) > unmirrored_plain (1) > unmirrored_other (2). | ||
| 18976 | if (item.reject_reason == 0 || item.reject_reason == 2 || (item.reject_reason == 1 && reason >= 3)) | ||
| 18977 | item.reject_reason = reason; | ||
| 18978 | }; | ||
| 18979 | |||
| 18755 | const auto try_full_view = [&](TitleMirrorCheckItem& item, std::uint64_t va, bool is_cpu_view) -> bool { | 18980 | const auto try_full_view = [&](TitleMirrorCheckItem& item, std::uint64_t va, bool is_cpu_view) -> bool { |
| 18756 | if (!va || !item.bytes || va + item.bytes < va) return false; | 18981 | if (!va || !item.bytes || va + item.bytes < va) return false; |
| 18757 | const std::uint64_t end = va + item.bytes; | 18982 | const std::uint64_t end = va + item.bytes; |
| 18758 | for (std::uint64_t p = va & ~(kPage - 1); p < end; p += kPage) { | 18983 | for (std::uint64_t p = va & ~(kPage - 1); p < end; p += kPage) { |
| 18759 | if (page_in_feed(p) || !mirror.mirrored(p) || (any_pending && mirror.has_pending(p))) return false; | 18984 | if (page_in_feed(p)) { |
| 18985 | record_reject(item, 3); | ||
| 18986 | return false; | ||
| 18987 | } | ||
| 18988 | if (!mirror.mirrored(p)) { | ||
| 18989 | auto pit = page_mirror.plain.upper_bound(p); | ||
| 18990 | const bool in_plain = (pit != page_mirror.plain.begin() && std::prev(pit)->second > p); | ||
| 18991 | record_reject(item, in_plain ? 1 : 2); | ||
| 18992 | return false; | ||
| 18993 | } | ||
| 18994 | if (any_pending && mirror.has_pending(p)) { | ||
| 18995 | record_reject(item, 4); | ||
| 18996 | return false; | ||
| 18997 | } | ||
| 18760 | if (p > UINT64_MAX - kPage) break; | 18998 | if (p > UINT64_MAX - kPage) break; |
| 18761 | } | 18999 | } |
| 18762 | const bool serial_clean = mirror_just_synced && mirror_synced_serial != 0 && item.ex_armed && | 19000 | const bool serial_clean = mirror_just_synced && mirror_synced_serial != 0 && item.ex_armed && |
| @@ -18790,6 +19028,7 @@ void DeviceState::page_mirror_check_batch( | |||
| 18790 | item.full_ok = false; | 19028 | item.full_ok = false; |
| 18791 | item.single_chunk = false; | 19029 | item.single_chunk = false; |
| 18792 | item.tile_cmp = 0; | 19030 | item.tile_cmp = 0; |
| 19031 | item.reject_reason = 0; | ||
| 18793 | item.mode = TitleMirrorCheckItem::Mode::none; | 19032 | item.mode = TitleMirrorCheckItem::Mode::none; |
| 18794 | item.mirror_va = 0; | 19033 | item.mirror_va = 0; |
| 18795 | if (!item.active || !item.bytes) continue; | 19034 | if (!item.active || !item.bytes) continue; |
| @@ -21476,7 +21715,7 @@ public: | |||
| 21476 | if (entry->fills && entry->filled_from) title_partial_bytes_ += entry->backing_bytes; | 21715 | if (entry->fills && entry->filled_from) title_partial_bytes_ += entry->backing_bytes; |
| 21477 | } | 21716 | } |
| 21478 | bool overflow = false; | 21717 | bool overflow = false; |
| 21479 | const auto written = leonida::title_texel_writes::take(overflow); | 21718 | auto written = leonida::title_texel_writes::take(overflow); |
| 21480 | stats.written_ranges += written.size(); | 21719 | stats.written_ranges += written.size(); |
| 21481 | title_map_changes_.clear(); | 21720 | title_map_changes_.clear(); |
| 21482 | std::uint64_t map_now = 0; | 21721 | std::uint64_t map_now = 0; |
| @@ -21501,16 +21740,37 @@ public: | |||
| 21501 | t.stale = true; | 21740 | t.stale = true; |
| 21502 | } | 21741 | } |
| 21503 | }; | 21742 | }; |
| 21504 | if (map_all || overflow) { | 21743 | // A pure texel write (XG CopyIntoSubresource / DirectStorage / slot-31 copy): do NOT invalidate cached |
| 21505 | watch.note_changed_and_for_each(0, UINTPTR_MAX, remapped, map_all); | 21744 | // readability on already-filled full-mip textures; only retry when pending, partially filled, or never filled |
| 21745 | // (preserves check_pending_stream_views and partial mip chain promotion). | ||
| 21746 | const auto texel_written = [&](std::uint64_t key) { | ||
| 21747 | const auto it = state_.title_textures.find(key); | ||
| 21748 | if (it != state_.title_textures.end()) { | ||
| 21749 | TitleTexture& t = *it->second; | ||
| 21750 | if (t.memory.pending() || t.filled_from != 0 || !t.fills) { | ||
| 21751 | t.memory.retry(); | ||
| 21752 | if (partial_mips() && !t.gpu_written && t.layout_known && !t.stale && t.resource && | ||
| 21753 | !t.layout_guessed && !t.layout_from_xg && (!t.fills || t.filled_from) && | ||
| 21754 | title_texture_readable(t) && (!t.fills || t.readable_from < t.filled_from)) | ||
| 21755 | t.stale = true; | ||
| 21756 | } | ||
| 21757 | } | ||
| 21758 | }; | ||
| 21759 | if (map_all) { | ||
| 21760 | watch.note_changed_and_for_each(0, UINTPTR_MAX, remapped, true); | ||
| 21506 | } else { | 21761 | } else { |
| 21507 | // Merge map changes and noted texel writes into disjoint sorted spans so duplicate/adjacent ranges (or | 21762 | if (!title_map_changes_.empty()) { |
| 21508 | // repeated full-space title_memory_map::changed() calls) touch each watched tile at most once. | 21763 | leonida::merge_title_writes(title_map_changes_); |
| 21509 | if (!written.empty()) | 21764 | for (const auto& [from, to] : title_map_changes_) |
| 21510 | title_map_changes_.insert(title_map_changes_.end(), written.begin(), written.end()); | 21765 | watch.note_changed_and_for_each(from, to, remapped, false); |
| 21511 | leonida::merge_title_writes(title_map_changes_); | 21766 | } |
| 21512 | for (const auto& [from, to] : title_map_changes_) | 21767 | if (overflow) { |
| 21513 | watch.note_changed_and_for_each(from, to, remapped); | 21768 | watch.note_changed_and_for_each(0, UINTPTR_MAX, texel_written, false); |
| 21769 | } else if (!written.empty()) { | ||
| 21770 | leonida::merge_title_writes(written); | ||
| 21771 | for (const auto& [from, to] : written) | ||
| 21772 | watch.note_changed_and_for_each(from, to, texel_written, false); | ||
| 21773 | } | ||
| 21514 | } | 21774 | } |
| 21515 | if (state_.titles_named_pending.load(std::memory_order_acquire)) { | 21775 | if (state_.titles_named_pending.load(std::memory_order_acquire)) { |
| 21516 | std::vector<std::uint64_t> named; | 21776 | std::vector<std::uint64_t> named; |
| @@ -21792,7 +22052,8 @@ public: | |||
| 21792 | job.entry.reset(); | 22052 | job.entry.reset(); |
| 21793 | job.resource = nullptr; | 22053 | job.resource = nullptr; |
| 21794 | job.slot = nullptr; | 22054 | job.slot = nullptr; |
| 21795 | if (job.tiled.capacity() > (2ull << 20)) std::vector<std::uint8_t>{}.swap(job.tiled); | 22055 | const std::size_t tiled_cap = (i < 4) ? (16ull << 20) : (2ull << 20); |
| 22056 | if (job.tiled.capacity() > tiled_cap) std::vector<std::uint8_t>{}.swap(job.tiled); | ||
| 21796 | if (job.piece_sums.capacity() > 8192) std::vector<std::uint64_t>{}.swap(job.piece_sums); | 22057 | if (job.piece_sums.capacity() > 8192) std::vector<std::uint64_t>{}.swap(job.piece_sums); |
| 21797 | } | 22058 | } |
| 21798 | title_job_count_ = 0; | 22059 | title_job_count_ = 0; |
| @@ -21825,6 +22086,9 @@ public: | |||
| 21825 | const bool have_mirror = state_.page_mirror.mirror != nullptr; | 22086 | const bool have_mirror = state_.page_mirror.mirror != nullptr; |
| 21826 | if (have_mirror) title_mirror_checks_.resize(title_candidates_.size()); | 22087 | if (have_mirror) title_mirror_checks_.resize(title_candidates_.size()); |
| 21827 | bool any_mirror_active = false; | 22088 | bool any_mirror_active = false; |
| 22089 | leonida::TextureVqCache stage2_vq_cache; | ||
| 22090 | const std::uint64_t probes_before = stats.probes[0]; | ||
| 22091 | std::uint64_t pass_back_ns = 0; | ||
| 21828 | 22092 | ||
| 21829 | for (std::size_t idx = 0; idx < title_candidates_.size(); ++idx) { | 22093 | for (std::size_t idx = 0; idx < title_candidates_.size(); ++idx) { |
| 21830 | TitleCandidate& cand = title_candidates_[idx]; | 22094 | TitleCandidate& cand = title_candidates_[idx]; |
| @@ -21836,7 +22100,7 @@ public: | |||
| 21836 | continue; | 22100 | continue; |
| 21837 | } | 22101 | } |
| 21838 | ++stats.checked; | 22102 | ++stats.checked; |
| 21839 | if (!title_texture_readable(t)) { | 22103 | if (!title_texture_readable(t, &stage2_vq_cache)) { |
| 21840 | not_filled(t); | 22104 | not_filled(t); |
| 21841 | if (have_mirror) title_mirror_checks_[idx].active = false; | 22105 | if (have_mirror) title_mirror_checks_[idx].active = false; |
| 21842 | continue; | 22106 | continue; |
| @@ -21849,10 +22113,21 @@ public: | |||
| 21849 | if (have_mirror) title_mirror_checks_[idx].active = false; | 22113 | if (have_mirror) title_mirror_checks_[idx].active = false; |
| 21850 | continue; | 22114 | continue; |
| 21851 | } | 22115 | } |
| 21852 | if (!back_title_texture(t)) { | 22116 | if (t.unbound_bytes) { |
| 21853 | not_filled(t); | 22117 | const auto back_start = clock::now(); |
| 21854 | if (have_mirror) title_mirror_checks_[idx].active = false; | 22118 | bool did_back = false, did_raw = false; |
| 21855 | continue; | 22119 | const bool backed_ok = back_title_texture(t, &did_back, &did_raw); |
| 22120 | pass_back_ns += std::uint64_t( | ||
| 22121 | std::chrono::duration_cast<std::chrono::nanoseconds>(clock::now() - back_start).count()); | ||
| 22122 | if (did_back) { | ||
| 22123 | ++stats.stage2_backed; | ||
| 22124 | if (did_raw) ++stats.stage2_raw_allocs; | ||
| 22125 | } | ||
| 22126 | if (!backed_ok) { | ||
| 22127 | not_filled(t); | ||
| 22128 | if (have_mirror) title_mirror_checks_[idx].active = false; | ||
| 22129 | continue; | ||
| 22130 | } | ||
| 21856 | } | 22131 | } |
| 21857 | const std::size_t tiles = std::size_t(((t.cpu + span + 0xffff) & ~std::uint64_t(0xffff)) - | 22132 | const std::size_t tiles = std::size_t(((t.cpu + span + 0xffff) & ~std::uint64_t(0xffff)) - |
| 21858 | (t.cpu & ~std::uint64_t(0xffff))) >> | 22133 | (t.cpu & ~std::uint64_t(0xffff))) >> |
| @@ -21881,8 +22156,12 @@ public: | |||
| 21881 | } | 22156 | } |
| 21882 | } | 22157 | } |
| 21883 | const auto t_after_probe = clock::now(); | 22158 | const auto t_after_probe = clock::now(); |
| 21884 | stats.examine_probe_ns += | 22159 | const std::uint64_t probe_total_ns = |
| 21885 | std::uint64_t(std::chrono::duration_cast<std::chrono::nanoseconds>(t_after_probe - t_after_arm).count()); | 22160 | std::uint64_t(std::chrono::duration_cast<std::chrono::nanoseconds>(t_after_probe - t_after_arm).count()); |
| 22161 | stats.examine_probe_ns += probe_total_ns; | ||
| 22162 | stats.examine_back_ns += pass_back_ns; | ||
| 22163 | stats.examine_readable_ns += probe_total_ns > pass_back_ns ? probe_total_ns - pass_back_ns : 0; | ||
| 22164 | stats.stage2_probes += stats.probes[0] - probes_before; | ||
| 21886 | 22165 | ||
| 21887 | // Stage 3: ONE page_mirror.mutex lock + ONE batched parallel TitleMirror::upload() for all candidates. | 22166 | // Stage 3: ONE page_mirror.mutex lock + ONE batched parallel TitleMirror::upload() for all candidates. |
| 21888 | if (have_mirror && any_mirror_active) { | 22167 | if (have_mirror && any_mirror_active) { |
| @@ -21984,8 +22263,25 @@ public: | |||
| 21984 | } | 22263 | } |
| 21985 | } | 22264 | } |
| 21986 | 22265 | ||
| 21987 | // CPU upload path: hash-before-reserve for ALL textures including tiles == 1 (`<= tiles`), and compare | 22266 | // CPU upload path: record fallback reason for telemetry, hash-before-reserve for ALL textures |
| 21988 | // t.mirror_hash on whole reads so CPU fallback after a GPU upload never re-uploads unchanged bytes. | 22267 | // including tiles == 1 (`<= tiles`), and compare t.mirror_hash on whole reads so CPU fallback after a |
| 22268 | // GPU upload never re-uploads unchanged bytes. | ||
| 22269 | if (have_mirror) { | ||
| 22270 | const auto& mc = title_mirror_checks_[idx]; | ||
| 22271 | std::size_t bucket = 1; // unmirrored_other | ||
| 22272 | if (mc.full_ok) { | ||
| 22273 | bucket = mc.single_chunk ? 5 : 4; // can_gpu_rejected : multi_chunk | ||
| 22274 | } else if (mc.reject_reason == 1) { | ||
| 22275 | bucket = 0; // unmirrored_plain | ||
| 22276 | } else if (mc.reject_reason == 3) { | ||
| 22277 | bucket = 2; // in_feed | ||
| 22278 | } else if (mc.reject_reason == 4) { | ||
| 22279 | bucket = 3; // pending_bind | ||
| 22280 | } | ||
| 22281 | ++stats.cpu_fallback_by[bucket]; | ||
| 22282 | } else { | ||
| 22283 | ++stats.cpu_fallback_by[1]; // unmirrored_other | ||
| 22284 | } | ||
| 21989 | TitleJob& job = next_title_job(); | 22285 | TitleJob& job = next_title_job(); |
| 21990 | job.entry = cand.entry; | 22286 | job.entry = cand.entry; |
| 21991 | job.resource = t.resource; | 22287 | job.resource = t.resource; |
| @@ -22129,20 +22425,45 @@ public: | |||
| 22129 | const double per_pass = 1e-6 / double((std::max)(std::uint64_t(1), title_split_passes_)); | 22425 | const double per_pass = 1e-6 / double((std::max)(std::uint64_t(1), title_split_passes_)); |
| 22130 | std::fprintf(stderr, | 22426 | std::fprintf(stderr, |
| 22131 | "[rt] d3d12: title texture pass split, ms a pass: %.3f finding what to look at (%.3f lock " | 22427 | "[rt] d3d12: title texture pass split, ms a pass: %.3f finding what to look at (%.3f lock " |
| 22132 | "wait), %.3f naming, arming and reserving (%.3f arm, %.3f read+back, %.3f mirror, %.3f " | 22428 | "wait), %.3f naming, arming and reserving (%.3f arm, %.3f readable (%llu probes), %.3f back " |
| 22133 | "publish), %.3f reading on %u threads (%.3f of it detiling, " | 22429 | "(%llu backed, %llu raw), %.3f mirror, %.3f publish), %.3f reading on %u threads (%.3f of it " |
| 22134 | "summed over them; %.1f MiB read whole a pass), %.3f recording and submitting the transfers\n", | 22430 | "detiling, summed over them; %.1f MiB read whole a pass), %.3f recording and submitting the " |
| 22431 | "transfers\n", | ||
| 22135 | double(stats.collect_ns) * per_pass, double(stats.lock_wait_ns) * per_pass, | 22432 | double(stats.collect_ns) * per_pass, double(stats.lock_wait_ns) * per_pass, |
| 22136 | double(stats.examine_ns) * per_pass, double(stats.examine_arm_ns) * per_pass, | 22433 | double(stats.examine_ns) * per_pass, double(stats.examine_arm_ns) * per_pass, |
| 22137 | double(stats.examine_probe_ns) * per_pass, double(stats.examine_mirror_ns) * per_pass, | 22434 | double(stats.examine_readable_ns) * per_pass, |
| 22435 | static_cast<unsigned long long>(stats.stage2_probes), | ||
| 22436 | double(stats.examine_back_ns) * per_pass, | ||
| 22437 | static_cast<unsigned long long>(stats.stage2_backed), | ||
| 22438 | static_cast<unsigned long long>(stats.stage2_raw_allocs), | ||
| 22439 | double(stats.examine_mirror_ns) * per_pass, | ||
| 22138 | double(stats.examine_publish_ns) * per_pass, double(stats.read_ns) * per_pass, | 22440 | double(stats.examine_publish_ns) * per_pass, double(stats.read_ns) * per_pass, |
| 22139 | title_read_pool_ ? title_read_pool_->threads() + 1 : 1, | 22441 | title_read_pool_ ? title_read_pool_->threads() + 1 : 1, |
| 22140 | double(stats.detile_ns) * per_pass, | 22442 | double(stats.detile_ns) * per_pass, |
| 22141 | double(stats.read_bytes) / 1048576.0 / double((std::max)(std::uint64_t(1), title_split_passes_)), | 22443 | double(stats.read_bytes) / 1048576.0 / double((std::max)(std::uint64_t(1), title_split_passes_)), |
| 22142 | double(stats.staged_ns) * per_pass); | 22444 | double(stats.staged_ns) * per_pass); |
| 22445 | const auto arm_s = state_.texture_cells.arm_stats; | ||
| 22446 | state_.texture_cells.arm_stats = {}; | ||
| 22447 | std::fprintf(stderr, | ||
| 22448 | "[rt] d3d12: title texture arm split: %llu hot, %llu already, %llu backed, %llu fast_vp " | ||
| 22449 | "(%llu bridged), %llu vq (%llu cache_hit), %llu vq_vp; cpu fallback by: %llu " | ||
| 22450 | "unmirrored_plain, %llu unmirrored_other, %llu in_feed, %llu pending_bind, %llu multi_chunk, " | ||
| 22451 | "%llu can_gpu_rejected\n", | ||
| 22452 | static_cast<unsigned long long>(arm_s.hot), static_cast<unsigned long long>(arm_s.already), | ||
| 22453 | static_cast<unsigned long long>(arm_s.backed), static_cast<unsigned long long>(arm_s.fast_vp), | ||
| 22454 | static_cast<unsigned long long>(arm_s.bridged), static_cast<unsigned long long>(arm_s.vq), | ||
| 22455 | static_cast<unsigned long long>(arm_s.vq_cache), static_cast<unsigned long long>(arm_s.vq_vp), | ||
| 22456 | static_cast<unsigned long long>(stats.cpu_fallback_by[0]), | ||
| 22457 | static_cast<unsigned long long>(stats.cpu_fallback_by[1]), | ||
| 22458 | static_cast<unsigned long long>(stats.cpu_fallback_by[2]), | ||
| 22459 | static_cast<unsigned long long>(stats.cpu_fallback_by[3]), | ||
| 22460 | static_cast<unsigned long long>(stats.cpu_fallback_by[4]), | ||
| 22461 | static_cast<unsigned long long>(stats.cpu_fallback_by[5])); | ||
| 22143 | stats.collect_ns = stats.lock_wait_ns = stats.examine_ns = stats.examine_arm_ns = stats.examine_probe_ns = | 22462 | stats.collect_ns = stats.lock_wait_ns = stats.examine_ns = stats.examine_arm_ns = stats.examine_probe_ns = |
| 22144 | stats.examine_mirror_ns = stats.examine_publish_ns = stats.read_ns = stats.detile_ns = | 22463 | stats.examine_readable_ns = stats.examine_back_ns = stats.examine_mirror_ns = |
| 22145 | stats.read_bytes = 0; | 22464 | stats.examine_publish_ns = stats.read_ns = stats.detile_ns = stats.read_bytes = 0; |
| 22465 | stats.stage2_probes = stats.stage2_backed = stats.stage2_raw_allocs = 0; | ||
| 22466 | stats.cpu_fallback_by = {}; | ||
| 22146 | stats.staged_ns = 0; | 22467 | stats.staged_ns = 0; |
| 22147 | title_split_passes_ = 0; | 22468 | title_split_passes_ = 0; |
| 22148 | stats.refresh_ns = stats.max_refresh_ns = stats.max_pass_bytes = 0; | 22469 | stats.refresh_ns = stats.max_refresh_ns = stats.max_pass_bytes = 0; |
| @@ -22167,12 +22488,17 @@ private: | |||
| 22167 | std::uint64_t partial_fills = 0, partial_textures = 0, partial_upgrades = 0, partial_refused = 0; | 22488 | std::uint64_t partial_fills = 0, partial_textures = 0, partial_upgrades = 0, partial_refused = 0; |
| 22168 | std::uint64_t read_whole = 0, unchanged_parts = 0, first_named = 0; | 22489 | std::uint64_t read_whole = 0, unchanged_parts = 0, first_named = 0; |
| 22169 | std::array<std::uint64_t, 5> whole_by{}; // why read whole: first, no parts, one tile, all written, none | 22490 | std::array<std::uint64_t, 5> whole_by{}; // why read whole: first, no parts, one tile, all written, none |
| 22491 | // Why Stage 4 fell back to CPU instead of GPU mirror: unmirrored_plain, unmirrored_other, in_feed, | ||
| 22492 | // pending_bind, multi_chunk, can_gpu_rejected. | ||
| 22493 | std::array<std::uint64_t, 6> cpu_fallback_by{}; | ||
| 22170 | // Split of refresh_ns: waiting on title_texture_mutex (lock_wait_ns), up to the textures to look at (map | 22494 | // Split of refresh_ns: waiting on title_texture_mutex (lock_wait_ns), up to the textures to look at (map |
| 22171 | // changes, notes, the watch's queue), naming and arming them (examine, mirror batch, readability, slot | 22495 | // changes, notes, the watch's queue), naming and arming them (examine, mirror batch, readability, slot |
| 22172 | // reservation), the read threads' work (and of it the detile, summed over the threads), and the transfers' | 22496 | // reservation), the read threads' work (and of it the detile, summed over the threads), and the transfers' |
| 22173 | // recording and submission (staged_ns); bytes read whole. | 22497 | // recording and submission (staged_ns); bytes read whole. |
| 22174 | std::uint64_t lock_wait_ns = 0, collect_ns = 0, examine_ns = 0, read_ns = 0, detile_ns = 0, read_bytes = 0; | 22498 | std::uint64_t lock_wait_ns = 0, collect_ns = 0, examine_ns = 0, read_ns = 0, detile_ns = 0, read_bytes = 0; |
| 22175 | std::uint64_t examine_arm_ns = 0, examine_probe_ns = 0, examine_mirror_ns = 0, examine_publish_ns = 0; | 22499 | std::uint64_t examine_arm_ns = 0, examine_probe_ns = 0, examine_readable_ns = 0, examine_back_ns = 0, |
| 22500 | examine_mirror_ns = 0, examine_publish_ns = 0; | ||
| 22501 | std::uint64_t stage2_probes = 0, stage2_backed = 0, stage2_raw_allocs = 0; | ||
| 22176 | std::uint64_t staged_fills = 0, staged_fills_logged = 0, staged_ns = 0; | 22502 | std::uint64_t staged_fills = 0, staged_fills_logged = 0, staged_ns = 0; |
| 22177 | std::uint64_t gpu_linear_fills = 0, gpu_detile_fills = 0, gpu_unchanged = 0, fast_passes = 0; | 22503 | std::uint64_t gpu_linear_fills = 0, gpu_detile_fills = 0, gpu_unchanged = 0, fast_passes = 0; |
| 22178 | // refresh_title_textures, since the last log line | 22504 | // refresh_title_textures, since the last log line |
| @@ -22843,10 +23169,12 @@ private: | |||
| 22843 | 23169 | ||
| 22844 | // texture_memory_readable_bytes, timed and counted (TitleTextureStats::probes). | 23170 | // texture_memory_readable_bytes, timed and counted (TitleTextureStats::probes). |
| 22845 | // True when a fill can use what is readable; t.readable_from says what that is. | 23171 | // True when a fill can use what is readable; t.readable_from says what that is. |
| 22846 | bool probe_title_texture(TitleTexture& t, bool waited, leonida::TexturePageCommitter commit = nullptr) { | 23172 | bool probe_title_texture(TitleTexture& t, bool waited, leonida::TexturePageCommitter commit = nullptr, |
| 23173 | leonida::TextureVqCache* vq_cache = nullptr) { | ||
| 22847 | const auto start = std::chrono::steady_clock::now(); | 23174 | const auto start = std::chrono::steady_clock::now(); |
| 22848 | t.readable_from = rt::xg_xs::first_mip_within( | 23175 | t.readable_from = rt::xg_xs::first_mip_within( |
| 22849 | t.layout, leonida::texture_memory_readable_bytes(std::uintptr_t(t.cpu), t.layout.layout.size, commit)); | 23176 | t.layout, leonida::texture_memory_readable_bytes(std::uintptr_t(t.cpu), t.layout.layout.size, commit, |
| 23177 | vq_cache)); | ||
| 22850 | const bool readable = t.readable_from <= fill_limit(t); | 23178 | const bool readable = t.readable_from <= fill_limit(t); |
| 22851 | const std::size_t where = title_pass_ ? 0 : 1; | 23179 | const std::size_t where = title_pass_ ? 0 : 1; |
| 22852 | TitleTextureStats& s = title_stats_; | 23180 | TitleTextureStats& s = title_stats_; |
| @@ -22859,19 +23187,21 @@ private: | |||
| 22859 | } | 23187 | } |
| 22860 | 23188 | ||
| 22861 | // A mapped texture can lose its pages between views or between refreshes. | 23189 | // A mapped texture can lose its pages between views or between refreshes. |
| 22862 | bool title_texture_readable(TitleTexture& t) { | 23190 | bool title_texture_readable(TitleTexture& t, leonida::TextureVqCache* vq_cache = nullptr) { |
| 22863 | const bool guard = check_pages_before_read(); | 23191 | const bool guard = check_pages_before_read(); |
| 22864 | if (!guard && !t.memory.pending()) return true; | 23192 | if (!guard && !t.memory.pending()) return true; |
| 22865 | const bool mapped = t.mapped && refresh_mapped_pages(); | 23193 | const bool mapped = t.mapped && refresh_mapped_pages(); |
| 22866 | if (!title_pass_ && guard && !mapped) | 23194 | if (!title_pass_ && guard) |
| 22867 | t.memory.sync_map(title_map_generation_, static_cast<std::uintptr_t>(t.cpu), t.layout.layout.size); | 23195 | t.memory.sync_map(title_map_generation_, static_cast<std::uintptr_t>(t.cpu), t.layout.layout.size); |
| 22868 | const auto commit = mapped ? leonida::kernel_writes::state().committer.load(std::memory_order_acquire) : nullptr; | 23196 | const auto commit = mapped ? leonida::kernel_writes::state().committer.load(std::memory_order_acquire) : nullptr; |
| 22869 | const bool waited = t.memory.pending(); | 23197 | const bool waited = t.memory.pending(); |
| 22870 | // The answer is kept (generation 0) until a map change over the texture, a write into it or the periodic | 23198 | // The answer is kept (generation 0) until a map change over the texture, a write into it or the periodic |
| 22871 | // retry of refresh_title_textures asks again. | 23199 | // retry of refresh_title_textures asks again. Keep cache_failure = guard && !mapped (a mapped plane may commit |
| 23200 | // via raw VirtualAlloc without notifying title_memory_map), while enabling cache_success = guard (unmaps | ||
| 23201 | // notify title_memory_map and reads are SEH-guarded). | ||
| 22872 | return t.memory.check(0, guard && !mapped, | 23202 | return t.memory.check(0, guard && !mapped, |
| 22873 | [&] { return probe_title_texture(t, waited, commit); }, | 23203 | [&] { return probe_title_texture(t, waited, commit, mapped ? nullptr : vq_cache); }, |
| 22874 | guard && !mapped); | 23204 | guard); |
| 22875 | } | 23205 | } |
| 22876 | 23206 | ||
| 22877 | // At a slot-70 view of a texture: defer uploads to the batched refresh_title_textures() pass before GPU | 23207 | // At a slot-70 view of a texture: defer uploads to the batched refresh_title_textures() pass before GPU |
| @@ -22983,12 +23313,14 @@ private: | |||
| 22983 | } | 23313 | } |
| 22984 | 23314 | ||
| 22985 | // A texture whose image has no memory yet gets it before its first fill. False (and stale) when it cannot. | 23315 | // A texture whose image has no memory yet gets it before its first fill. False (and stale) when it cannot. |
| 22986 | bool back_title_texture(TitleTexture& t) { | 23316 | bool back_title_texture(TitleTexture& t, bool* did_back = nullptr, bool* did_raw_alloc = nullptr) { |
| 22987 | if (!t.unbound_bytes) return true; | 23317 | if (!t.unbound_bytes) return true; |
| 22988 | if (t.owned ? !t.resource->back_streamed_texture() : !t.resource->image()) { | 23318 | if (t.owned ? !t.resource->back_streamed_texture() : !t.resource->image()) { |
| 22989 | t.stale = true; | 23319 | t.stale = true; |
| 22990 | return false; | 23320 | return false; |
| 22991 | } | 23321 | } |
| 23322 | if (did_back) *did_back = true; | ||
| 23323 | if (did_raw_alloc && t.resource) *did_raw_alloc = !t.resource->pooled_image(); | ||
| 22992 | t.mirror_hash_valid = false; | 23324 | t.mirror_hash_valid = false; |
| 22993 | t.set_resource(t.resource); | 23325 | t.set_resource(t.resource); |
| 22994 | return true; | 23326 | return true; |
diff --git a/runtime/shims/xmem.cpp b/runtime/shims/xmem.cpp index 19425e9cd..de99914d6 100644 --- a/runtime/shims/xmem.cpp +++ b/runtime/shims/xmem.cpp | |||
| @@ -784,6 +784,47 @@ bool table_frame(std::uintptr_t page, leonida::title_frames::Frame& out) { | |||
| 784 | return true; | 784 | return true; |
| 785 | } | 785 | } |
| 786 | 786 | ||
| 787 | // Single-lock, zero-shared_ptr run_end over g_table_frames (src/title_frames.h). | ||
| 788 | std::uintptr_t table_frame_run_end(std::uintptr_t from, std::uintptr_t to, bool& is_backed) { | ||
| 789 | constexpr std::uintptr_t kPage = leonida::title_frames::kPage; | ||
| 790 | if (from >= to) { | ||
| 791 | is_backed = false; | ||
| 792 | return to; | ||
| 793 | } | ||
| 794 | AcquireSRWLockShared(&g_frame_lock); | ||
| 795 | const auto has = [](std::uintptr_t p) -> bool { | ||
| 796 | const auto it = g_table_frames.find(p); | ||
| 797 | return it != g_table_frames.end() && it->second.section && it->second.section->scratch; | ||
| 798 | }; | ||
| 799 | const std::uintptr_t first_page = from & ~(kPage - 1); | ||
| 800 | is_backed = has(first_page); | ||
| 801 | if (!is_backed && g_table_frames.empty()) { | ||
| 802 | ReleaseSRWLockShared(&g_frame_lock); | ||
| 803 | return to; | ||
| 804 | } | ||
| 805 | std::uintptr_t at = first_page + kPage; | ||
| 806 | while (at < to && at > from && has(at) == is_backed) at += kPage; | ||
| 807 | ReleaseSRWLockShared(&g_frame_lock); | ||
| 808 | return at < to && at > from ? at : to; | ||
| 809 | } | ||
| 810 | |||
| 811 | // Single-lock, zero-shared_ptr batch existence check over g_table_frames (src/title_frames.h). | ||
| 812 | void table_frame_backed_batch(const std::uintptr_t* pages, std::uint8_t* out_backed, std::size_t count) { | ||
| 813 | constexpr std::uintptr_t kPage = leonida::title_frames::kPage; | ||
| 814 | if (!count) return; | ||
| 815 | AcquireSRWLockShared(&g_frame_lock); | ||
| 816 | if (g_table_frames.empty()) { | ||
| 817 | ReleaseSRWLockShared(&g_frame_lock); | ||
| 818 | for (std::size_t k = 0; k < count; ++k) out_backed[k] = 0; | ||
| 819 | return; | ||
| 820 | } | ||
| 821 | for (std::size_t k = 0; k < count; ++k) { | ||
| 822 | const auto it = g_table_frames.find(pages[k] & ~(kPage - 1)); | ||
| 823 | out_backed[k] = (it != g_table_frames.end() && it->second.section && it->second.section->scratch) ? 1u : 0u; | ||
| 824 | } | ||
| 825 | ReleaseSRWLockShared(&g_frame_lock); | ||
| 826 | } | ||
| 827 | |||
| 787 | // Appends `value` in hex (with 0x) or decimal at `at`, which has room for it: no CRT, so no lock and no allocation. | 828 | // Appends `value` in hex (with 0x) or decimal at `at`, which has room for it: no CRT, so no lock and no allocation. |
| 788 | char* append_number(char* at, unsigned long long value, bool hex) { | 829 | char* append_number(char* at, unsigned long long value, bool hex) { |
| 789 | char digits[24]; | 830 | char digits[24]; |
| @@ -3126,7 +3167,9 @@ void attach_mirror_locked(void* context, leonida::title_frames::Feed feed) { | |||
| 3126 | [[maybe_unused]] const bool g_mirror_attach_set = (leonida::title_frames::attach_fn = &attach_mirror, true); | 3167 | [[maybe_unused]] const bool g_mirror_attach_set = (leonida::title_frames::attach_fn = &attach_mirror, true); |
| 3127 | 3168 | ||
| 3128 | [[maybe_unused]] const bool g_title_frames_set = | 3169 | [[maybe_unused]] const bool g_title_frames_set = |
| 3129 | (leonida::title_frames::set_lookup(&table_frame, &geometry_aliases), true); | 3170 | (leonida::title_frames::set_lookup(&table_frame, &geometry_aliases, &table_frame_run_end, |
| 3171 | &table_frame_backed_batch), | ||
| 3172 | true); | ||
| 3130 | 3173 | ||
| 3131 | void set_fault_race_hook(FaultRaceHook hook) { g_fault_race_hook.store(hook, std::memory_order_release); } | 3174 | void set_fault_race_hook(FaultRaceHook hook) { g_fault_race_hook.store(hook, std::memory_order_release); } |
| 3132 | 3175 | ||
diff --git a/runtime/src/title_frames.h b/runtime/src/title_frames.h index 693cbf28a..5dc074989 100644 --- a/runtime/src/title_frames.h +++ b/runtime/src/title_frames.h | |||
| @@ -41,10 +41,15 @@ struct Frame { | |||
| 41 | using Lookup = bool (*)(std::uintptr_t page, Frame& out); | 41 | using Lookup = bool (*)(std::uintptr_t page, Frame& out); |
| 42 | // xmem's side: the host addresses the page's physical page is mapped at (its other addresses), appended to `out`. | 42 | // xmem's side: the host addresses the page's physical page is mapped at (its other addresses), appended to `out`. |
| 43 | using Peers = void (*)(std::uintptr_t page, std::vector<std::uintptr_t>& out); | 43 | using Peers = void (*)(std::uintptr_t page, std::vector<std::uintptr_t>& out); |
| 44 | // xmem's side: single-lock, zero-shared_ptr run_end and batch-backed checks over g_table_frames. | ||
| 45 | using RunEnd = std::uintptr_t (*)(std::uintptr_t from, std::uintptr_t to, bool& is_backed); | ||
| 46 | using BackedBatch = void (*)(const std::uintptr_t* pages, std::uint8_t* out_backed, std::size_t count); | ||
| 44 | 47 | ||
| 45 | struct State { | 48 | struct State { |
| 46 | std::atomic<Lookup> lookup{nullptr}; | 49 | std::atomic<Lookup> lookup{nullptr}; |
| 47 | std::atomic<Peers> peers{nullptr}; | 50 | std::atomic<Peers> peers{nullptr}; |
| 51 | std::atomic<RunEnd> run_end{nullptr}; | ||
| 52 | std::atomic<BackedBatch> backed_batch{nullptr}; | ||
| 48 | // Every page ever given a table entry lies in [lo, hi): a range outside costs a reader two loads, not a lookup. | 53 | // Every page ever given a table entry lies in [lo, hi): a range outside costs a reader two loads, not a lookup. |
| 49 | std::atomic<std::uintptr_t> lo{UINTPTR_MAX}, hi{0}; | 54 | std::atomic<std::uintptr_t> lo{UINTPTR_MAX}, hi{0}; |
| 50 | }; | 55 | }; |
| @@ -54,9 +59,12 @@ inline State& state() { | |||
| 54 | return s; | 59 | return s; |
| 55 | } | 60 | } |
| 56 | 61 | ||
| 57 | inline void set_lookup(Lookup lookup, Peers peers) { | 62 | inline void set_lookup(Lookup lookup, Peers peers, RunEnd run_end_fn = nullptr, BackedBatch backed_batch_fn = nullptr) { |
| 58 | state().peers.store(peers, std::memory_order_release); | 63 | State& s = state(); |
| 59 | state().lookup.store(lookup, std::memory_order_release); | 64 | s.peers.store(peers, std::memory_order_release); |
| 65 | s.run_end.store(run_end_fn, std::memory_order_release); | ||
| 66 | s.backed_batch.store(backed_batch_fn, std::memory_order_release); | ||
| 67 | s.lookup.store(lookup, std::memory_order_release); | ||
| 60 | } | 68 | } |
| 61 | 69 | ||
| 62 | // xmem, before the page at `page` gets a table entry: the bounds grow to hold it (they never shrink). | 70 | // xmem, before the page at `page` gets a table entry: the bounds grow to hold it (they never shrink). |
| @@ -103,11 +111,34 @@ inline void peers(std::uintptr_t va, std::vector<std::uintptr_t>& out) { | |||
| 103 | if (const Peers f = state().peers.load(std::memory_order_acquire)) f(va & ~(kPage - 1), out); | 111 | if (const Peers f = state().peers.load(std::memory_order_acquire)) f(va & ~(kPage - 1), out); |
| 104 | } | 112 | } |
| 105 | 113 | ||
| 114 | // Sets out_backed[k] = 1 iff the 64 KiB page holding pages[k] is a backed table page, else 0. | ||
| 115 | // Uses xmem's single-lock, zero-shared_ptr batch hook when registered; falls back to backed() otherwise. | ||
| 116 | inline void backed_batch(const std::uintptr_t* pages, std::uint8_t* out_backed, std::size_t count) { | ||
| 117 | if (!count) return; | ||
| 118 | const State& s = state(); | ||
| 119 | if (!s.lookup.load(std::memory_order_acquire) || | ||
| 120 | s.lo.load(std::memory_order_acquire) >= s.hi.load(std::memory_order_acquire)) { | ||
| 121 | for (std::size_t k = 0; k < count; ++k) out_backed[k] = 0; | ||
| 122 | return; | ||
| 123 | } | ||
| 124 | if (const BackedBatch fn = s.backed_batch.load(std::memory_order_acquire)) { | ||
| 125 | fn(pages, out_backed, count); | ||
| 126 | return; | ||
| 127 | } | ||
| 128 | for (std::size_t k = 0; k < count; ++k) out_backed[k] = backed(pages[k]) ? 1u : 0u; | ||
| 129 | } | ||
| 130 | |||
| 106 | // The end of the run that starts at `from`, at most `to`, whose pages are all table pages or all not; `is_backed` | 131 | // The end of the run that starts at `from`, at most `to`, whose pages are all table pages or all not; `is_backed` |
| 107 | // says which. At least one page, or up to `to`. | 132 | // says which. At least one page, or up to `to`. |
| 108 | inline std::uintptr_t run_end(std::uintptr_t from, std::uintptr_t to, bool& is_backed) { | 133 | inline std::uintptr_t run_end(std::uintptr_t from, std::uintptr_t to, bool& is_backed) { |
| 134 | if (!may_hold(from, to)) { | ||
| 135 | is_backed = false; | ||
| 136 | return to; | ||
| 137 | } | ||
| 138 | if (const RunEnd fn = state().run_end.load(std::memory_order_acquire)) { | ||
| 139 | return fn(from, to, is_backed); | ||
| 140 | } | ||
| 109 | is_backed = backed(from); | 141 | is_backed = backed(from); |
| 110 | if (!may_hold(from, to)) return to; | ||
| 111 | std::uintptr_t at = (from & ~(kPage - 1)) + kPage; | 142 | std::uintptr_t at = (from & ~(kPage - 1)) + kPage; |
| 112 | while (at < to && at > from && backed(at) == is_backed) at += kPage; | 143 | while (at < to && at > from && backed(at) == is_backed) at += kPage; |
| 113 | return at < to && at > from ? at : to; | 144 | return at < to && at > from ? at : to; |
diff --git a/runtime/src/title_page_watch.h b/runtime/src/title_page_watch.h index 6f7e369f3..05663a2b4 100644 --- a/runtime/src/title_page_watch.h +++ b/runtime/src/title_page_watch.h | |||
| @@ -387,9 +387,11 @@ inline std::uintptr_t to_host_view(std::uintptr_t va, std::uintptr_t bytes = 1) | |||
| 387 | namespace leonida::title_texel_writes { | 387 | namespace leonida::title_texel_writes { |
| 388 | 388 | ||
| 389 | struct State { | 389 | struct State { |
| 390 | static constexpr std::size_t kMaxMerged = 16384; | ||
| 390 | std::mutex mutex; | 391 | std::mutex mutex; |
| 391 | std::vector<std::pair<std::uintptr_t, std::uintptr_t>> ranges; // [begin, end) | 392 | std::vector<std::pair<std::uintptr_t, std::uintptr_t>> ranges; // [begin, end) |
| 392 | bool overflow = false; // too many to keep: check everything | 393 | std::size_t compact_at = kMaxMerged; |
| 394 | bool overflow = false; // too many disjoint spans to keep: check everything | ||
| 393 | std::atomic<bool> pending{false}; | 395 | std::atomic<bool> pending{false}; |
| 394 | }; | 396 | }; |
| 395 | 397 | ||
| @@ -402,9 +404,37 @@ inline void note(const void* begin, std::size_t bytes) { | |||
| 402 | if (!begin || !bytes) return; | 404 | if (!begin || !bytes) return; |
| 403 | State& s = state(); | 405 | State& s = state(); |
| 404 | std::lock_guard lock(s.mutex); | 406 | std::lock_guard lock(s.mutex); |
| 407 | if (s.overflow) { | ||
| 408 | s.pending.store(true, std::memory_order_release); | ||
| 409 | return; | ||
| 410 | } | ||
| 405 | const auto at = reinterpret_cast<std::uintptr_t>(begin); | 411 | const auto at = reinterpret_cast<std::uintptr_t>(begin); |
| 406 | if (s.ranges.size() < 4096) s.ranges.emplace_back(at, at + bytes); | 412 | const std::uintptr_t end = bytes > UINTPTR_MAX - at ? UINTPTR_MAX : at + bytes; |
| 407 | else s.overflow = true; | 413 | if (!s.ranges.empty() && at <= s.ranges.back().second && end >= s.ranges.back().first) { |
| 414 | s.ranges.back().first = (std::min)(s.ranges.back().first, at); | ||
| 415 | s.ranges.back().second = (std::max)(s.ranges.back().second, end); | ||
| 416 | s.pending.store(true, std::memory_order_release); | ||
| 417 | return; | ||
| 418 | } | ||
| 419 | s.ranges.emplace_back(at, end); | ||
| 420 | if (s.ranges.size() > s.compact_at) { | ||
| 421 | std::sort(s.ranges.begin(), s.ranges.end()); | ||
| 422 | std::size_t kept = 0; | ||
| 423 | for (std::size_t i = 1; i < s.ranges.size(); ++i) { | ||
| 424 | if (s.ranges[i].first <= s.ranges[kept].second) | ||
| 425 | s.ranges[kept].second = (std::max)(s.ranges[kept].second, s.ranges[i].second); | ||
| 426 | else | ||
| 427 | s.ranges[++kept] = s.ranges[i]; | ||
| 428 | } | ||
| 429 | s.ranges.resize(kept + 1); | ||
| 430 | if (s.ranges.size() > State::kMaxMerged) { | ||
| 431 | s.overflow = true; | ||
| 432 | s.ranges.clear(); | ||
| 433 | s.compact_at = State::kMaxMerged; | ||
| 434 | } else { | ||
| 435 | s.compact_at = (std::max)(State::kMaxMerged, s.ranges.size() + 4096); | ||
| 436 | } | ||
| 437 | } | ||
| 408 | s.pending.store(true, std::memory_order_release); | 438 | s.pending.store(true, std::memory_order_release); |
| 409 | } | 439 | } |
| 410 | 440 | ||
| @@ -415,6 +445,7 @@ inline std::vector<std::pair<std::uintptr_t, std::uintptr_t>> take(bool& overflo | |||
| 415 | if (!s.pending.load(std::memory_order_acquire)) return {}; | 445 | if (!s.pending.load(std::memory_order_acquire)) return {}; |
| 416 | std::lock_guard lock(s.mutex); | 446 | std::lock_guard lock(s.mutex); |
| 417 | overflow = std::exchange(s.overflow, false); | 447 | overflow = std::exchange(s.overflow, false); |
| 448 | s.compact_at = State::kMaxMerged; | ||
| 418 | s.pending.store(false, std::memory_order_release); | 449 | s.pending.store(false, std::memory_order_release); |
| 419 | return std::exchange(s.ranges, {}); | 450 | return std::exchange(s.ranges, {}); |
| 420 | } | 451 | } |
diff --git a/runtime/src/title_texture_memory.h b/runtime/src/title_texture_memory.h index 773105645..d02f69614 100644 --- a/runtime/src/title_texture_memory.h +++ b/runtime/src/title_texture_memory.h | |||
| @@ -55,11 +55,25 @@ private: | |||
| 55 | 55 | ||
| 56 | using TexturePageCommitter = bool (*)(const void*, std::size_t); | 56 | using TexturePageCommitter = bool (*)(const void*, std::size_t); |
| 57 | 57 | ||
| 58 | // Short-lived per-pass cache of the last verified readable MEM_COMMIT VirtualQuery region [lo, hi). | ||
| 59 | // Reused across VA-sorted texture probes only when `!commit` and no title_memory_map change overlapped [lo, hi). | ||
| 60 | struct TextureVqCache { | ||
| 61 | std::uintptr_t lo = 0; | ||
| 62 | std::uintptr_t hi = 0; | ||
| 63 | std::uint64_t gen = 0; | ||
| 64 | |||
| 65 | void reset() noexcept { | ||
| 66 | lo = hi = 0; | ||
| 67 | gen = 0; | ||
| 68 | } | ||
| 69 | }; | ||
| 70 | |||
| 58 | // How many of the bytes from `base` are readable, up to the first page that is not (at most `size`). A page xmem's | 71 | // How many of the bytes from `base` are readable, up to the first page that is not (at most `size`). A page xmem's |
| 59 | // frame table holds (a stream pool page queue slot 31 gave a physical page: no host view, src/title_frames.h) is | 72 | // frame table holds (a stream pool page queue slot 31 gave a physical page: no host view, src/title_frames.h) is |
| 60 | // readable through its frame. | 73 | // readable through its frame. |
| 61 | inline std::uint64_t texture_memory_readable_bytes(std::uintptr_t base, std::uint64_t size, | 74 | inline std::uint64_t texture_memory_readable_bytes(std::uintptr_t base, std::uint64_t size, |
| 62 | TexturePageCommitter commit = nullptr) { | 75 | TexturePageCommitter commit = nullptr, |
| 76 | TextureVqCache* vq_cache = nullptr) { | ||
| 63 | if (!base || !size || size > (std::numeric_limits<std::uintptr_t>::max)() - base) return 0; | 77 | if (!base || !size || size > (std::numeric_limits<std::uintptr_t>::max)() - base) return 0; |
| 64 | const auto end = base + size; | 78 | const auto end = base + size; |
| 65 | for (auto at = base; at < end;) { | 79 | for (auto at = base; at < end;) { |
| @@ -72,6 +86,29 @@ inline std::uint64_t texture_memory_readable_bytes(std::uintptr_t base, std::uin | |||
| 72 | continue; | 86 | continue; |
| 73 | } | 87 | } |
| 74 | } | 88 | } |
| 89 | if (vq_cache && !commit && at >= vq_cache->lo && at < vq_cache->hi) { | ||
| 90 | const std::uint64_t cur = title_memory_map::generation(); | ||
| 91 | bool clean = (cur == vq_cache->gen); | ||
| 92 | if (!clean) { | ||
| 93 | std::uint64_t now = cur; | ||
| 94 | bool overlapped = false; | ||
| 95 | if (title_memory_map::changes_since(vq_cache->gen, now, vq_cache->lo, vq_cache->hi, | ||
| 96 | [&](std::uintptr_t c_from, std::uintptr_t c_to) { | ||
| 97 | if (c_from < c_to) overlapped = true; | ||
| 98 | }) && | ||
| 99 | !overlapped) { | ||
| 100 | vq_cache->gen = now; | ||
| 101 | clean = true; | ||
| 102 | } else { | ||
| 103 | vq_cache->hi = 0; | ||
| 104 | } | ||
| 105 | } | ||
| 106 | if (clean) { | ||
| 107 | at = (std::min)(stop, vq_cache->hi); | ||
| 108 | continue; | ||
| 109 | } | ||
| 110 | } | ||
| 111 | const std::uint64_t query_gen = (vq_cache && !commit) ? title_memory_map::generation() : 0; | ||
| 75 | MEMORY_BASIC_INFORMATION region{}; | 112 | MEMORY_BASIC_INFORMATION region{}; |
| 76 | const auto* address = reinterpret_cast<const void*>(at); | 113 | const auto* address = reinterpret_cast<const void*>(at); |
| 77 | if (!VirtualQuery(address, ®ion, sizeof(region))) return at - base; | 114 | if (!VirtualQuery(address, ®ion, sizeof(region))) return at - base; |
| @@ -89,6 +126,11 @@ inline std::uint64_t texture_memory_readable_bytes(std::uintptr_t base, std::uin | |||
| 89 | const auto start = reinterpret_cast<std::uintptr_t>(region.BaseAddress); | 126 | const auto start = reinterpret_cast<std::uintptr_t>(region.BaseAddress); |
| 90 | if (at < start || at - start >= region.RegionSize) return at - base; | 127 | if (at < start || at - start >= region.RegionSize) return at - base; |
| 91 | const auto available = region.RegionSize - (at - start); | 128 | const auto available = region.RegionSize - (at - start); |
| 129 | if (vq_cache && !commit && region.RegionSize <= (std::numeric_limits<std::uintptr_t>::max)() - start) { | ||
| 130 | vq_cache->lo = start; | ||
| 131 | vq_cache->hi = start + region.RegionSize; | ||
| 132 | vq_cache->gen = query_gen; | ||
| 133 | } | ||
| 92 | if (available >= stop - at) { | 134 | if (available >= stop - at) { |
| 93 | at = stop; | 135 | at = stop; |
| 94 | continue; | 136 | continue; |
| @@ -99,8 +141,9 @@ inline std::uint64_t texture_memory_readable_bytes(std::uintptr_t base, std::uin | |||
| 99 | } | 141 | } |
| 100 | 142 | ||
| 101 | // Check the whole footprint. A committed first page says nothing about a streamed mip farther on. | 143 | // Check the whole footprint. A committed first page says nothing about a streamed mip farther on. |
| 102 | inline bool texture_memory_readable(std::uintptr_t base, std::uint64_t size, TexturePageCommitter commit = nullptr) { | 144 | inline bool texture_memory_readable(std::uintptr_t base, std::uint64_t size, TexturePageCommitter commit = nullptr, |
| 103 | return size && texture_memory_readable_bytes(base, size, commit) == size; | 145 | TextureVqCache* vq_cache = nullptr) { |
| 146 | return size && texture_memory_readable_bytes(base, size, commit, vq_cache) == size; | ||
| 104 | } | 147 | } |
| 105 | 148 | ||
| 106 | } // namespace leonida | 149 | } // namespace leonida |
diff --git a/runtime/src/title_texture_watch.h b/runtime/src/title_texture_watch.h index 5c947a9d2..7b21b0dbe 100644 --- a/runtime/src/title_texture_watch.h +++ b/runtime/src/title_texture_watch.h | |||
| @@ -445,6 +445,7 @@ public: | |||
| 445 | batch_tiles_.clear(); | 445 | batch_tiles_.clear(); |
| 446 | batch_reqs_.clear(); | 446 | batch_reqs_.clear(); |
| 447 | batch_tile_refs_.clear(); | 447 | batch_tile_refs_.clear(); |
| 448 | bool any_peer_sync = false; | ||
| 448 | 449 | ||
| 449 | auto queue_tile = [&](std::size_t key_idx, std::uintptr_t a, Tile& tile) { | 450 | auto queue_tile = [&](std::size_t key_idx, std::uintptr_t a, Tile& tile) { |
| 450 | if (tile.pass_written_gen == gen || tile.stale || tile.unarmed_index != kNoIndex) | 451 | if (tile.pass_written_gen == gen || tile.stale || tile.unarmed_index != kNoIndex) |
| @@ -457,8 +458,10 @@ public: | |||
| 457 | return; | 458 | return; |
| 458 | } | 459 | } |
| 459 | if (tile.batch_arm_gen == bgen) { | 460 | if (tile.batch_arm_gen == bgen) { |
| 460 | out[key_idx].max_version = (std::max)(out[key_idx].max_version, tile.batch_arm_version); | 461 | if (tile.batch_arm_slot != kNoIndex) |
| 461 | if (tile.batch_arm_slot != kNoIndex) batch_tile_refs_.emplace_back(key_idx, tile.batch_arm_slot); | 462 | batch_tile_refs_.emplace_back(key_idx, tile.batch_arm_slot); |
| 463 | else | ||
| 464 | out[key_idx].max_version = (std::max)(out[key_idx].max_version, tile.batch_arm_version); | ||
| 462 | return; | 465 | return; |
| 463 | } | 466 | } |
| 464 | const std::uint64_t mapping = tile.primary->mapping.load(std::memory_order_acquire); | 467 | const std::uint64_t mapping = tile.primary->mapping.load(std::memory_order_acquire); |
| @@ -475,22 +478,13 @@ public: | |||
| 475 | return; | 478 | return; |
| 476 | } | 479 | } |
| 477 | } | 480 | } |
| 478 | if (mapping != tile.mapping) { | 481 | const bool needs_peer_sync = (mapping != tile.mapping); |
| 479 | sync_tile_peers(a, tile, mapping); | 482 | if (needs_peer_sync) any_peer_sync = true; |
| 480 | } | ||
| 481 | std::uint64_t version = tile.primary->version.load(std::memory_order_acquire); | ||
| 482 | for (const auto& [peer, cell] : tile.peers) | ||
| 483 | version = (std::max)(version, cell->version.load(std::memory_order_acquire)); | ||
| 484 | const bool pure_hot_recheck = !tile.stale && tile.unarmed_index == kNoIndex && tile.hot_gen == pass_gen_ && | ||
| 485 | mapping == tile.mapping && version == tile.version; | ||
| 486 | const std::uint32_t slot = static_cast<std::uint32_t>(batch_tiles_.size()); | 483 | const std::uint32_t slot = static_cast<std::uint32_t>(batch_tiles_.size()); |
| 487 | tile.batch_arm_gen = bgen; | 484 | tile.batch_arm_gen = bgen; |
| 488 | tile.batch_arm_slot = slot; | 485 | tile.batch_arm_slot = slot; |
| 489 | tile.batch_arm_version = version; | 486 | tile.batch_arm_version = 0; |
| 490 | out[key_idx].max_version = (std::max)(out[key_idx].max_version, version); | 487 | batch_tiles_.push_back({a, &tile, 0, mapping, false, needs_peer_sync, 1}); |
| 491 | batch_tiles_.push_back({a, &tile, version, pure_hot_recheck, 1}); | ||
| 492 | batch_reqs_.push_back({a, tile.primary, slot}); | ||
| 493 | for (const auto& [peer, cell] : tile.peers) batch_reqs_.push_back({peer, cell, slot}); | ||
| 494 | batch_tile_refs_.emplace_back(key_idx, slot); | 488 | batch_tile_refs_.emplace_back(key_idx, slot); |
| 495 | }; | 489 | }; |
| 496 | 490 | ||
| @@ -534,6 +528,23 @@ public: | |||
| 534 | 528 | ||
| 535 | if (batch_tiles_.empty()) return; | 529 | if (batch_tiles_.empty()) return; |
| 536 | 530 | ||
| 531 | if (any_peer_sync) sync_batch_tile_peers(); | ||
| 532 | |||
| 533 | for (std::size_t slot = 0; slot < batch_tiles_.size(); ++slot) { | ||
| 534 | BatchTile& bt = batch_tiles_[slot]; | ||
| 535 | Tile& tile = *bt.tile; | ||
| 536 | std::uint64_t version = tile.primary->version.load(std::memory_order_acquire); | ||
| 537 | for (const auto& [peer, cell] : tile.peers) | ||
| 538 | version = (std::max)(version, cell->version.load(std::memory_order_acquire)); | ||
| 539 | bt.version = version; | ||
| 540 | bt.pure_hot_recheck = !bt.needs_peer_sync && !tile.stale && tile.unarmed_index == kNoIndex && | ||
| 541 | tile.hot_gen == pass_gen_ && version == tile.version; | ||
| 542 | tile.batch_arm_version = version; | ||
| 543 | const auto slot_u32 = static_cast<std::uint32_t>(slot); | ||
| 544 | batch_reqs_.push_back({bt.address, tile.primary, slot_u32}); | ||
| 545 | for (const auto& [peer, cell] : tile.peers) batch_reqs_.push_back({peer, cell, slot_u32}); | ||
| 546 | } | ||
| 547 | |||
| 537 | if (batch_reqs_.size() > 1) { | 548 | if (batch_reqs_.size() > 1) { |
| 538 | std::sort(batch_reqs_.begin(), batch_reqs_.end(), | 549 | std::sort(batch_reqs_.begin(), batch_reqs_.end(), |
| 539 | [](const BatchArmReq& lhs, const BatchArmReq& rhs) { return lhs.va < rhs.va; }); | 550 | [](const BatchArmReq& lhs, const BatchArmReq& rhs) { return lhs.va < rhs.va; }); |
| @@ -631,7 +642,9 @@ private: | |||
| 631 | std::uintptr_t address = 0; | 642 | std::uintptr_t address = 0; |
| 632 | Tile* tile = nullptr; | 643 | Tile* tile = nullptr; |
| 633 | std::uint64_t version = 0; | 644 | std::uint64_t version = 0; |
| 645 | std::uint64_t mapping = 0; | ||
| 634 | bool pure_hot_recheck = false; | 646 | bool pure_hot_recheck = false; |
| 647 | bool needs_peer_sync = false; | ||
| 635 | int result = 1; | 648 | int result = 1; |
| 636 | }; | 649 | }; |
| 637 | struct BatchArmReq { | 650 | struct BatchArmReq { |
| @@ -732,6 +745,25 @@ private: | |||
| 732 | } | 745 | } |
| 733 | 746 | ||
| 734 | void drop(std::uintptr_t address, Tile& tile) { | 747 | void drop(std::uintptr_t address, Tile& tile) { |
| 748 | if constexpr (requires(Watch& w, const std::pair<std::uintptr_t, bool>* items, std::size_t n) { | ||
| 749 | w.release_batch(items, n); | ||
| 750 | }) { | ||
| 751 | if (!tile.peers.empty()) { | ||
| 752 | peer_rel_items_.clear(); | ||
| 753 | peer_rel_items_.reserve(tile.peers.size() + (tile.primary ? 1 : 0)); | ||
| 754 | for (const auto& [peer, cell] : tile.peers) { | ||
| 755 | unlink_peer(peer, address); | ||
| 756 | peer_rel_items_.emplace_back(peer, false); | ||
| 757 | } | ||
| 758 | tile.peers.clear(); | ||
| 759 | if (tile.primary) { | ||
| 760 | peer_rel_items_.emplace_back(address, false); | ||
| 761 | tile.primary = nullptr; | ||
| 762 | } | ||
| 763 | watch_.release_batch(peer_rel_items_.data(), peer_rel_items_.size()); | ||
| 764 | return; | ||
| 765 | } | ||
| 766 | } | ||
| 735 | for (const auto& [peer, cell] : tile.peers) { | 767 | for (const auto& [peer, cell] : tile.peers) { |
| 736 | unlink_peer(peer, address); | 768 | unlink_peer(peer, address); |
| 737 | watch_.release(peer); | 769 | watch_.release(peer); |
| @@ -806,19 +838,116 @@ private: | |||
| 806 | auto old = std::move(tile.peers); | 838 | auto old = std::move(tile.peers); |
| 807 | tile.peers.clear(); | 839 | tile.peers.clear(); |
| 808 | tile.peers.reserve(aliases_.size()); | 840 | tile.peers.reserve(aliases_.size()); |
| 809 | for (const std::uintptr_t peer : aliases_) { | 841 | if (!aliases_.empty()) { |
| 810 | tile.peers.emplace_back(peer, watch_.acquire(peer)); | 842 | if constexpr (requires(Watch& w, const std::uintptr_t* vas, Cell** out, std::size_t n) { |
| 811 | by_peer_[peer].push_back(address); | 843 | w.acquire_batch(vas, out, n); |
| 844 | }) { | ||
| 845 | peer_acq_cells_.resize(aliases_.size()); | ||
| 846 | watch_.acquire_batch(aliases_.data(), peer_acq_cells_.data(), aliases_.size()); | ||
| 847 | for (std::size_t k = 0; k < aliases_.size(); ++k) { | ||
| 848 | const std::uintptr_t peer = aliases_[k]; | ||
| 849 | tile.peers.emplace_back(peer, peer_acq_cells_[k]); | ||
| 850 | by_peer_[peer].push_back(address); | ||
| 851 | } | ||
| 852 | } else { | ||
| 853 | for (const std::uintptr_t peer : aliases_) { | ||
| 854 | tile.peers.emplace_back(peer, watch_.acquire(peer)); | ||
| 855 | by_peer_[peer].push_back(address); | ||
| 856 | } | ||
| 857 | } | ||
| 812 | hot_sync_needed_ = true; | 858 | hot_sync_needed_ = true; |
| 813 | } | 859 | } |
| 814 | for (const auto& [peer, cell] : old) { | 860 | if (!old.empty()) { |
| 815 | unlink_peer(peer, address); | 861 | for (const auto& [peer, cell] : old) unlink_peer(peer, address); |
| 816 | watch_.release(peer); | 862 | if constexpr (requires(Watch& w, const std::pair<std::uintptr_t, bool>* items, std::size_t n) { |
| 863 | w.release_batch(items, n); | ||
| 864 | }) { | ||
| 865 | peer_rel_items_.clear(); | ||
| 866 | peer_rel_items_.reserve(old.size()); | ||
| 867 | for (const auto& [peer, cell] : old) peer_rel_items_.emplace_back(peer, false); | ||
| 868 | watch_.release_batch(peer_rel_items_.data(), peer_rel_items_.size()); | ||
| 869 | } else { | ||
| 870 | for (const auto& [peer, cell] : old) watch_.release(peer); | ||
| 871 | } | ||
| 817 | } | 872 | } |
| 818 | } | 873 | } |
| 819 | tile.mapping = mapping; | 874 | tile.mapping = mapping; |
| 820 | } | 875 | } |
| 821 | 876 | ||
| 877 | // Batches peer alias syncs across all remapped tiles in examine_batch() into at most ONE acquire_batch() + ONE | ||
| 878 | // release_batch() call (acquiring new peers before releasing old peers so surviving peers never drop refcount 0). | ||
| 879 | void sync_batch_tile_peers() { | ||
| 880 | peer_acq_vas_.clear(); | ||
| 881 | peer_acq_spans_.clear(); | ||
| 882 | peer_rel_items_.clear(); | ||
| 883 | for (std::size_t slot = 0; slot < batch_tiles_.size(); ++slot) { | ||
| 884 | BatchTile& bt = batch_tiles_[slot]; | ||
| 885 | if (!bt.needs_peer_sync) continue; | ||
| 886 | Tile& tile = *bt.tile; | ||
| 887 | const std::uintptr_t address = bt.address; | ||
| 888 | aliases_.clear(); | ||
| 889 | watch_.aliases(address, aliases_); | ||
| 890 | std::erase(aliases_, address); | ||
| 891 | if (aliases_.size() > 1) { | ||
| 892 | std::sort(aliases_.begin(), aliases_.end()); | ||
| 893 | aliases_.erase(std::unique(aliases_.begin(), aliases_.end()), aliases_.end()); | ||
| 894 | } | ||
| 895 | bool same_peers = aliases_.size() == tile.peers.size(); | ||
| 896 | if (same_peers) { | ||
| 897 | for (std::size_t k = 0; k < aliases_.size(); ++k) { | ||
| 898 | if (tile.peers[k].first != aliases_[k]) { | ||
| 899 | same_peers = false; | ||
| 900 | break; | ||
| 901 | } | ||
| 902 | } | ||
| 903 | } | ||
| 904 | if (!same_peers) { | ||
| 905 | for (const auto& [peer, cell] : tile.peers) { | ||
| 906 | unlink_peer(peer, address); | ||
| 907 | peer_rel_items_.emplace_back(peer, false); | ||
| 908 | } | ||
| 909 | tile.peers.clear(); | ||
| 910 | if (!aliases_.empty()) { | ||
| 911 | const std::size_t begin = peer_acq_vas_.size(); | ||
| 912 | peer_acq_vas_.insert(peer_acq_vas_.end(), aliases_.begin(), aliases_.end()); | ||
| 913 | peer_acq_spans_.push_back({slot, begin, aliases_.size()}); | ||
| 914 | hot_sync_needed_ = true; | ||
| 915 | } | ||
| 916 | } | ||
| 917 | tile.mapping = bt.mapping; | ||
| 918 | } | ||
| 919 | if (!peer_acq_vas_.empty()) { | ||
| 920 | peer_acq_cells_.resize(peer_acq_vas_.size()); | ||
| 921 | if constexpr (requires(Watch& w, const std::uintptr_t* vas, Cell** out, std::size_t n) { | ||
| 922 | w.acquire_batch(vas, out, n); | ||
| 923 | }) { | ||
| 924 | watch_.acquire_batch(peer_acq_vas_.data(), peer_acq_cells_.data(), peer_acq_vas_.size()); | ||
| 925 | } else { | ||
| 926 | for (std::size_t k = 0; k < peer_acq_vas_.size(); ++k) | ||
| 927 | peer_acq_cells_[k] = watch_.acquire(peer_acq_vas_[k]); | ||
| 928 | } | ||
| 929 | for (const auto& span : peer_acq_spans_) { | ||
| 930 | BatchTile& bt = batch_tiles_[span.slot]; | ||
| 931 | Tile& tile = *bt.tile; | ||
| 932 | tile.peers.reserve(span.count); | ||
| 933 | for (std::size_t k = 0; k < span.count; ++k) { | ||
| 934 | const std::uintptr_t peer = peer_acq_vas_[span.begin + k]; | ||
| 935 | tile.peers.emplace_back(peer, peer_acq_cells_[span.begin + k]); | ||
| 936 | by_peer_[peer].push_back(bt.address); | ||
| 937 | } | ||
| 938 | } | ||
| 939 | } | ||
| 940 | if (!peer_rel_items_.empty()) { | ||
| 941 | if constexpr (requires(Watch& w, const std::pair<std::uintptr_t, bool>* items, std::size_t n) { | ||
| 942 | w.release_batch(items, n); | ||
| 943 | }) { | ||
| 944 | watch_.release_batch(peer_rel_items_.data(), peer_rel_items_.size()); | ||
| 945 | } else { | ||
| 946 | for (const auto& [peer, was_hot] : peer_rel_items_) watch_.release(peer); | ||
| 947 | } | ||
| 948 | } | ||
| 949 | } | ||
| 950 | |||
| 822 | int arm(std::uintptr_t address, Tile& tile) { | 951 | int arm(std::uintptr_t address, Tile& tile) { |
| 823 | if (!watch_.enabled()) { | 952 | if (!watch_.enabled()) { |
| 824 | add_unarmed(address, tile); | 953 | add_unarmed(address, tile); |
| @@ -898,10 +1027,19 @@ private: | |||
| 898 | std::vector<BatchTile> batch_tiles_; | 1027 | std::vector<BatchTile> batch_tiles_; |
| 899 | std::vector<BatchArmReq> batch_reqs_; | 1028 | std::vector<BatchArmReq> batch_reqs_; |
| 900 | std::vector<std::pair<std::size_t, std::uint32_t>> batch_tile_refs_; | 1029 | std::vector<std::pair<std::size_t, std::uint32_t>> batch_tile_refs_; |
| 1030 | struct PeerAcqSpan { | ||
| 1031 | std::size_t slot = 0; | ||
| 1032 | std::size_t begin = 0; | ||
| 1033 | std::size_t count = 0; | ||
| 1034 | }; | ||
| 901 | std::vector<std::uintptr_t> batch_arm_vas_; | 1035 | std::vector<std::uintptr_t> batch_arm_vas_; |
| 902 | std::vector<Cell*> batch_arm_cells_; | 1036 | std::vector<Cell*> batch_arm_cells_; |
| 903 | std::vector<std::size_t> batch_req_unique_; | 1037 | std::vector<std::size_t> batch_req_unique_; |
| 904 | std::vector<std::int8_t> batch_arm_results_; | 1038 | std::vector<std::int8_t> batch_arm_results_; |
| 1039 | std::vector<std::uintptr_t> peer_acq_vas_; | ||
| 1040 | std::vector<Cell*> peer_acq_cells_; | ||
| 1041 | std::vector<PeerAcqSpan> peer_acq_spans_; | ||
| 1042 | std::vector<std::pair<std::uintptr_t, bool>> peer_rel_items_; | ||
| 905 | std::uint64_t pass_gen_ = 0; | 1043 | std::uint64_t pass_gen_ = 0; |
| 906 | std::uint64_t over_gen_ = 0; | 1044 | std::uint64_t over_gen_ = 0; |
| 907 | std::uint64_t batch_arm_gen_ = 0; | 1045 | std::uint64_t batch_arm_gen_ = 0; |
diff --git a/runtime/src/vulkan_title_detile.h b/runtime/src/vulkan_title_detile.h index e7e6fc4bd..522e5c1f2 100644 --- a/runtime/src/vulkan_title_detile.h +++ b/runtime/src/vulkan_title_detile.h | |||
| @@ -524,16 +524,17 @@ public: | |||
| 524 | if (desc.block_side != 1 && desc.block_side != 4) return false; | 524 | if (desc.block_side != 1 && desc.block_side != 4) return false; |
| 525 | if (desc.mips.empty() || desc.mips.size() > image_mips || !desc.array_layers || desc.array_layers > image_layers) | 525 | if (desc.mips.empty() || desc.mips.size() > image_mips || !desc.array_layers || desc.array_layers > image_layers) |
| 526 | return false; | 526 | return false; |
| 527 | if (volume && desc.mips.size() > 1) return false; | 527 | if (volume && desc.array_layers != 1) return false; |
| 528 | if (!desc.total_size || src_offset > chunk_size || desc.total_size > chunk_size - src_offset) return false; | 528 | if (!desc.total_size || src_offset > chunk_size || desc.total_size > chunk_size - src_offset) return false; |
| 529 | const VkDeviceSize align = std::max<VkDeviceSize>(4, desc.element_bytes); | 529 | const VkDeviceSize align = (std::max)(VkDeviceSize(4), VkDeviceSize(desc.element_bytes)); |
| 530 | if (src_offset % align != 0) return false; | 530 | if (src_offset % align != 0) return false; |
| 531 | if (volume && image_depth > 1 && (desc.slice_bytes % align) != 0) return false; | ||
| 531 | for (std::uint32_t layer = 0; layer < desc.array_layers; ++layer) { | 532 | for (std::uint32_t layer = 0; layer < desc.array_layers; ++layer) { |
| 532 | for (std::uint32_t level = 0; level < desc.mips.size(); ++level) { | 533 | for (std::uint32_t level = 0; level < desc.mips.size(); ++level) { |
| 533 | const auto& m = desc.mips[level]; | 534 | const auto& m = desc.mips[level]; |
| 534 | const std::uint32_t ew = std::max(1u, image_width >> level); | 535 | const std::uint32_t ew = (std::max)(1u, image_width >> level); |
| 535 | const std::uint32_t eh = std::max(1u, image_height >> level); | 536 | const std::uint32_t eh = (std::max)(1u, image_height >> level); |
| 536 | const std::uint32_t ed = volume ? std::max(1u, image_depth >> level) : 1u; | 537 | const std::uint32_t ed = volume ? (std::max)(1u, image_depth >> level) : 1u; |
| 537 | if (m.width_elements != (ew + desc.block_side - 1) / desc.block_side || | 538 | if (m.width_elements != (ew + desc.block_side - 1) / desc.block_side || |
| 538 | m.height_elements != (eh + desc.block_side - 1) / desc.block_side || | 539 | m.height_elements != (eh + desc.block_side - 1) / desc.block_side || |
| 539 | (volume && m.depth != ed)) | 540 | (volume && m.depth != ed)) |
| @@ -542,6 +543,14 @@ public: | |||
| 542 | if (volume && (m.padded_height * desc.block_side < eh)) return false; | 543 | if (volume && (m.padded_height * desc.block_side < eh)) return false; |
| 543 | const VkDeviceSize off = src_offset + VkDeviceSize(layer) * desc.slice_bytes + m.offset; | 544 | const VkDeviceSize off = src_offset + VkDeviceSize(layer) * desc.slice_bytes + m.offset; |
| 544 | if (off % align != 0) return false; | 545 | if (off % align != 0) return false; |
| 546 | if (volume && ed > 1) { | ||
| 547 | const VkDeviceSize row_stride = VkDeviceSize(m.pitch_texels / desc.block_side) * desc.element_bytes; | ||
| 548 | const VkDeviceSize mip_slice = row_stride * m.height_elements; | ||
| 549 | if (VkDeviceSize(ed - 1) > desc.total_size / (std::max)(VkDeviceSize(1), desc.slice_bytes)) | ||
| 550 | return false; | ||
| 551 | const VkDeviceSize last_slice_off = VkDeviceSize(ed - 1) * desc.slice_bytes + m.offset; | ||
| 552 | if (last_slice_off > desc.total_size || mip_slice > desc.total_size - last_slice_off) return false; | ||
| 553 | } | ||
| 545 | } | 554 | } |
| 546 | } | 555 | } |
| 547 | return true; | 556 | return true; |
| @@ -556,14 +565,34 @@ public: | |||
| 556 | for (std::uint32_t layer = 0; layer < desc.array_layers; ++layer) { | 565 | for (std::uint32_t layer = 0; layer < desc.array_layers; ++layer) { |
| 557 | for (std::uint32_t level = 0; level < desc.mips.size(); ++level) { | 566 | for (std::uint32_t level = 0; level < desc.mips.size(); ++level) { |
| 558 | const auto& m = desc.mips[level]; | 567 | const auto& m = desc.mips[level]; |
| 559 | VkBufferImageCopy copy{}; | 568 | const std::uint32_t ew = (std::max)(1u, image_width >> level); |
| 560 | copy.bufferOffset = src_offset + VkDeviceSize(layer) * desc.slice_bytes + m.offset; | 569 | const std::uint32_t eh = (std::max)(1u, image_height >> level); |
| 561 | copy.bufferRowLength = m.pitch_texels; | 570 | const std::uint32_t ed = volume ? (std::max)(1u, image_depth >> level) : 1u; |
| 562 | copy.bufferImageHeight = volume ? m.padded_height * desc.block_side : 0u; | 571 | const VkDeviceSize row_stride = VkDeviceSize(m.pitch_texels / desc.block_side) * desc.element_bytes; |
| 563 | copy.imageSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, level, layer, 1}; | 572 | const bool per_depth_slice = |
| 564 | copy.imageExtent = {std::max(1u, image_width >> level), std::max(1u, image_height >> level), | 573 | volume && ed > 1 && (desc.mips.size() > 1 || desc.slice_bytes != row_stride * m.padded_height); |
| 565 | volume ? std::max(1u, image_depth >> level) : 1u}; | 574 | if (per_depth_slice) { |
| 566 | out.push_back(copy); | 575 | // Console linear 3D stores each depth slice's full mip chain stride `desc.slice_bytes` apart, |
| 576 | // which exceeds one mip's `row_stride * padded_height` whenever mips > 1. | ||
| 577 | for (std::uint32_t z = 0; z < ed; ++z) { | ||
| 578 | VkBufferImageCopy copy{}; | ||
| 579 | copy.bufferOffset = src_offset + VkDeviceSize(z) * desc.slice_bytes + m.offset; | ||
| 580 | copy.bufferRowLength = m.pitch_texels; | ||
| 581 | copy.bufferImageHeight = 0u; | ||
| 582 | copy.imageSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, level, 0u, 1u}; | ||
| 583 | copy.imageOffset = {0, 0, static_cast<std::int32_t>(z)}; | ||
| 584 | copy.imageExtent = {ew, eh, 1u}; | ||
| 585 | out.push_back(copy); | ||
| 586 | } | ||
| 587 | } else { | ||
| 588 | VkBufferImageCopy copy{}; | ||
| 589 | copy.bufferOffset = src_offset + VkDeviceSize(layer) * desc.slice_bytes + m.offset; | ||
| 590 | copy.bufferRowLength = m.pitch_texels; | ||
| 591 | copy.bufferImageHeight = volume ? m.padded_height * desc.block_side : 0u; | ||
| 592 | copy.imageSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, level, layer, 1}; | ||
| 593 | copy.imageExtent = {ew, eh, ed}; | ||
| 594 | out.push_back(copy); | ||
| 595 | } | ||
| 567 | } | 596 | } |
| 568 | } | 597 | } |
| 569 | return !out.empty(); | 598 | return !out.empty(); |
diff --git a/runtime/src/vulkan_title_image_pool.h b/runtime/src/vulkan_title_image_pool.h index 94308f687..e7d93c7a8 100644 --- a/runtime/src/vulkan_title_image_pool.h +++ b/runtime/src/vulkan_title_image_pool.h | |||
| @@ -69,13 +69,18 @@ public: | |||
| 69 | std::unique_ptr<PooledTitleImage> bind(VkDevice device, VkImage image, const VkMemoryRequirements& req, | 69 | std::unique_ptr<PooledTitleImage> bind(VkDevice device, VkImage image, const VkMemoryRequirements& req, |
| 70 | PFN_vkBindImageMemory bind_image, PFN_vkDestroyImage destroy_image, | 70 | PFN_vkBindImageMemory bind_image, PFN_vkDestroyImage destroy_image, |
| 71 | PFN_vkFreeMemory free_memory, Allocate allocate) { | 71 | PFN_vkFreeMemory free_memory, Allocate allocate) { |
| 72 | constexpr VkDeviceSize largest_slot = VkDeviceSize{2} << 20; | 72 | constexpr VkDeviceSize largest_slot = VkDeviceSize{8} << 20; |
| 73 | const VkDeviceSize needed = std::max(req.size, req.alignment); | 73 | const VkDeviceSize needed = (std::max)(req.size, req.alignment); |
| 74 | if (!needed || needed > largest_slot || !req.memoryTypeBits) return {}; | 74 | if (!needed || needed > largest_slot || !req.memoryTypeBits) return {}; |
| 75 | // Eight size classes per power of two keep padding below one eighth (besides required alignment). | 75 | // Eight size classes per power of two keep padding below one eighth (besides required alignment). |
| 76 | const VkDeviceSize quantum = std::max(req.alignment, std::max(VkDeviceSize{1}, std::bit_floor(needed) / 8)); | 76 | const VkDeviceSize quantum = (std::max)(req.alignment, (std::max)(VkDeviceSize{1}, std::bit_floor(needed) / 8)); |
| 77 | const VkDeviceSize stride = (needed + quantum - 1) & ~(quantum - 1); | 77 | const VkDeviceSize stride = (needed + quantum - 1) & ~(quantum - 1); |
| 78 | const VkDeviceSize page_size = std::min(VkDeviceSize{8} << 20, 256 * stride); | 78 | const VkDeviceSize max_page = stride > (VkDeviceSize{2} << 20) ? (VkDeviceSize{16} << 20) : (VkDeviceSize{8} << 20); |
| 79 | if (!stride || stride > max_page) return {}; | ||
| 80 | const VkDeviceSize max_bytes = (std::min)(max_page, 256 * stride); | ||
| 81 | const std::uint32_t capacity = static_cast<std::uint32_t>(max_bytes / stride); | ||
| 82 | if (!capacity) return {}; | ||
| 83 | const VkDeviceSize page_size = stride * capacity; | ||
| 79 | auto allocation = std::make_unique<PooledTitleImage>(); | 84 | auto allocation = std::make_unique<PooledTitleImage>(); |
| 80 | std::lock_guard lock(mutex_); | 85 | std::lock_guard lock(mutex_); |
| 81 | auto& pages = classes_[stride]; | 86 | auto& pages = classes_[stride]; |
diff --git a/runtime/test/title_texture_watch_test.cpp b/runtime/test/title_texture_watch_test.cpp index b8f541fe6..56745c88b 100644 --- a/runtime/test/title_texture_watch_test.cpp +++ b/runtime/test/title_texture_watch_test.cpp | |||
| @@ -1,6 +1,7 @@ | |||
| 1 | // TitleTextureWatch (src/title_texture_watch.h): a title texture is looked at only when something says it may have | 1 | // TitleTextureWatch (src/title_texture_watch.h): a title texture is looked at only when something says it may have |
| 2 | // changed. A fake geometry watch stands in for the cells: a "write" bumps a tile's version and queues the tile, as | 2 | // changed. A fake geometry watch stands in for the cells: a "write" bumps a tile's version and queues the tile, as |
| 3 | // the write-fault handler does through geometry_writes::notify. | 3 | // the write-fault handler does through geometry_writes::notify. |
| 4 | #include "../src/title_frames.h" | ||
| 4 | #include "../src/title_texture_refresh.h" | 5 | #include "../src/title_texture_refresh.h" |
| 5 | #include "../src/title_texture_watch.h" | 6 | #include "../src/title_texture_watch.h" |
| 6 | 7 | ||
| @@ -1050,10 +1051,159 @@ int main() { | |||
| 1050 | watch.examine_batch(keys, res); | 1051 | watch.examine_batch(keys, res); |
| 1051 | EXPECT(res[0].max_version == raced_ver); | 1052 | EXPECT(res[0].max_version == raced_ver); |
| 1052 | } | 1053 | } |
| 1054 | { // Batched peer sync (acquire_batch / release_batch) in sync_tile_peers(), examine_batch(), and drop(): | ||
| 1055 | // across 4 remapping tiles with overlapping surviving peers, examine_batch() issues ONE acquire_batch() before | ||
| 1056 | // ONE release_batch() so surviving peers never drop refcount to 0. | ||
| 1057 | struct BatchPeerWatch : FakeWatch { | ||
| 1058 | std::uint64_t single_acq = 0, single_rel = 0; | ||
| 1059 | std::uint64_t batch_acq_calls = 0, batch_rel_calls = 0; | ||
| 1060 | std::uint64_t batch_acq_items = 0, batch_rel_items = 0; | ||
| 1061 | bool peer_dropped_to_zero = false; | ||
| 1062 | std::uintptr_t watch_nonzero_peer = 0; | ||
| 1063 | Cell* acquire(std::uintptr_t tile) { | ||
| 1064 | ++single_acq; | ||
| 1065 | return FakeWatch::acquire(tile); | ||
| 1066 | } | ||
| 1067 | void release(std::uintptr_t tile) { | ||
| 1068 | ++single_rel; | ||
| 1069 | FakeWatch::release(tile); | ||
| 1070 | } | ||
| 1071 | void acquire_batch(const std::uintptr_t* vas, Cell** out, std::size_t n) { | ||
| 1072 | ++batch_acq_calls; | ||
| 1073 | batch_acq_items += n; | ||
| 1074 | for (std::size_t i = 0; i < n; ++i) out[i] = FakeWatch::acquire(vas[i]); | ||
| 1075 | } | ||
| 1076 | void release_batch(const std::pair<std::uintptr_t, bool>* items, std::size_t n) { | ||
| 1077 | ++batch_rel_calls; | ||
| 1078 | batch_rel_items += n; | ||
| 1079 | for (std::size_t i = 0; i < n; ++i) { | ||
| 1080 | FakeWatch::release(items[i].first); | ||
| 1081 | if (watch_nonzero_peer && items[i].first == watch_nonzero_peer && !cells.count(watch_nonzero_peer)) | ||
| 1082 | peer_dropped_to_zero = true; | ||
| 1083 | } | ||
| 1084 | } | ||
| 1085 | void arm_batch(const std::uintptr_t* tiles, Cell* const* cell_ptrs, std::int8_t* results, | ||
| 1086 | std::size_t count) { | ||
| 1087 | for (std::size_t i = 0; i < count; ++i) { | ||
| 1088 | const int r = FakeWatch::arm(tiles[i], *cell_ptrs[i]); | ||
| 1089 | results[i] = static_cast<std::int8_t>(r > 0 ? 1 : (r == 0 ? 0 : -1)); | ||
| 1090 | } | ||
| 1091 | } | ||
| 1092 | }; | ||
| 1093 | BatchPeerWatch bpw; | ||
| 1094 | leonida::TitleTextureWatch<BatchPeerWatch> bwatch(bpw); | ||
| 1095 | const std::uintptr_t p0 = 0x6100000000, p1 = 0x6200000000, p2 = 0x6300000000, p3 = 0x6400000000; | ||
| 1096 | for (std::uint64_t k = 0; k < 4; ++k) { | ||
| 1097 | bpw.alias[base + k * kTile] = {p0, p1}; | ||
| 1098 | bwatch.track(k + 1, base + k * kTile, kTile); | ||
| 1099 | } | ||
| 1100 | std::vector<std::uint64_t> keys; | ||
| 1101 | std::vector<leonida::TitleTextureWatch<BatchPeerWatch>::BatchExamine> res; | ||
| 1102 | bwatch.collect([&](std::uint64_t k) { keys.push_back(k); }); | ||
| 1103 | EXPECT((keys == V{1, 2, 3, 4})); | ||
| 1104 | const std::uint64_t sa0 = bpw.single_acq; | ||
| 1105 | bwatch.examine_batch(keys, res); | ||
| 1106 | // All 4 tiles' initial peer syncs ran in ONE acquire_batch (8 items, 0 single_acq for peers). | ||
| 1107 | EXPECT(bpw.single_acq == sa0); | ||
| 1108 | EXPECT(bpw.batch_acq_calls == 1 && bpw.batch_acq_items == 8); | ||
| 1109 | EXPECT(bpw.batch_rel_calls == 0); | ||
| 1110 | EXPECT(bpw.cells.at(p0).refs == 4 && bpw.cells.at(p1).refs == 4); | ||
| 1111 | |||
| 1112 | // Remap all 4 tiles from {p0, p1} -> {p0, p2} (p0 survives and MUST never hit refcount 0!). | ||
| 1113 | bpw.watch_nonzero_peer = p0; | ||
| 1114 | for (std::uint64_t k = 0; k < 4; ++k) { | ||
| 1115 | bpw.alias[base + k * kTile] = {p0, p2}; | ||
| 1116 | bpw.write(base + k * kTile, /*mapping=*/true); | ||
| 1117 | } | ||
| 1118 | keys.clear(); | ||
| 1119 | bwatch.collect([&](std::uint64_t k) { keys.push_back(k); }); | ||
| 1120 | EXPECT((keys == V{1, 2, 3, 4})); | ||
| 1121 | bwatch.examine_batch(keys, res); | ||
| 1122 | EXPECT(!bpw.peer_dropped_to_zero); | ||
| 1123 | EXPECT(bpw.batch_acq_calls == 2 && bpw.batch_acq_items == 16); | ||
| 1124 | EXPECT(bpw.batch_rel_calls == 1 && bpw.batch_rel_items == 8); | ||
| 1125 | EXPECT(bpw.cells.at(p0).refs == 4 && bpw.cells.at(p2).refs == 4 && !bpw.cells.count(p1)); | ||
| 1126 | |||
| 1127 | // Single-key examine() with remap also routes through acquire_batch / release_batch. | ||
| 1128 | bpw.alias[base] = {p0, p3}; | ||
| 1129 | bpw.write(base, /*mapping=*/true); | ||
| 1130 | keys.clear(); | ||
| 1131 | bwatch.collect([&](std::uint64_t k) { keys.push_back(k); }); | ||
| 1132 | EXPECT((keys == V{1})); | ||
| 1133 | EXPECT(bwatch.examine(1) == leonida::TitleTextureWatch<BatchPeerWatch>::Look::armed); | ||
| 1134 | EXPECT(!bpw.peer_dropped_to_zero); | ||
| 1135 | EXPECT(bpw.batch_acq_calls == 3 && bpw.batch_rel_calls == 2); | ||
| 1136 | EXPECT(bpw.cells.at(p3).refs == 1 && bpw.cells.at(p2).refs == 3); | ||
| 1137 | |||
| 1138 | // untrack() releases primary + peers via release_batch. | ||
| 1139 | bpw.watch_nonzero_peer = 0; | ||
| 1140 | const std::uint64_t sr0 = bpw.single_rel; | ||
| 1141 | for (std::uint64_t k = 0; k < 4; ++k) bwatch.untrack(k + 1); | ||
| 1142 | EXPECT(bpw.single_rel == sr0); | ||
| 1143 | EXPECT(bpw.cells.empty()); | ||
| 1144 | } | ||
| 1145 | { // title_frames::run_end() and title_frames::backed_batch(): fast single-hook path & fallback path. | ||
| 1146 | static std::set<std::uintptr_t> s_pages; | ||
| 1147 | static std::uint8_t s_dummy_buf[65536]; | ||
| 1148 | static std::uint64_t s_lookup_calls = 0; | ||
| 1149 | static std::uint64_t s_run_end_calls = 0; | ||
| 1150 | static std::uint64_t s_backed_batch_calls = 0; | ||
| 1151 | const auto lookup_fn = [](std::uintptr_t page, leonida::title_frames::Frame& out) -> bool { | ||
| 1152 | ++s_lookup_calls; | ||
| 1153 | if (!s_pages.count(page)) return false; | ||
| 1154 | out.bytes = s_dummy_buf; | ||
| 1155 | return true; | ||
| 1156 | }; | ||
| 1157 | const auto run_end_fn = [](std::uintptr_t from, std::uintptr_t to, bool& is_backed) -> std::uintptr_t { | ||
| 1158 | ++s_run_end_calls; | ||
| 1159 | constexpr std::uintptr_t kP = leonida::title_frames::kPage; | ||
| 1160 | const std::uintptr_t p0 = from & ~(kP - 1); | ||
| 1161 | is_backed = s_pages.count(p0) != 0; | ||
| 1162 | std::uintptr_t at = p0 + kP; | ||
| 1163 | while (at < to && at > from && (s_pages.count(at) != 0) == is_backed) at += kP; | ||
| 1164 | return at < to && at > from ? at : to; | ||
| 1165 | }; | ||
| 1166 | const auto backed_batch_fn = [](const std::uintptr_t* pages, std::uint8_t* out, std::size_t n) { | ||
| 1167 | ++s_backed_batch_calls; | ||
| 1168 | constexpr std::uintptr_t kP = leonida::title_frames::kPage; | ||
| 1169 | for (std::size_t i = 0; i < n; ++i) out[i] = s_pages.count(pages[i] & ~(kP - 1)) ? 1u : 0u; | ||
| 1170 | }; | ||
| 1171 | |||
| 1172 | const std::uintptr_t tf_base = 0x700000000000ull; | ||
| 1173 | s_pages = {tf_base + 1 * kTile, tf_base + 2 * kTile, tf_base + 5 * kTile}; | ||
| 1174 | for (const std::uintptr_t p : s_pages) leonida::title_frames::note_page(p); | ||
| 1175 | |||
| 1176 | // 1. Registered fast hooks: run_end() and backed_batch() invoke fast hooks with ZERO per-page lookup_fn calls! | ||
| 1177 | leonida::title_frames::set_lookup(lookup_fn, nullptr, run_end_fn, backed_batch_fn); | ||
| 1178 | bool is_b = false; | ||
| 1179 | EXPECT(leonida::title_frames::run_end(tf_base, tf_base + 6 * kTile, is_b) == tf_base + 1 * kTile && !is_b); | ||
| 1180 | EXPECT(leonida::title_frames::run_end(tf_base + 1 * kTile, tf_base + 6 * kTile, is_b) == tf_base + 3 * kTile && | ||
| 1181 | is_b); | ||
| 1182 | EXPECT(leonida::title_frames::run_end(tf_base + 3 * kTile, tf_base + 6 * kTile, is_b) == tf_base + 5 * kTile && | ||
| 1183 | !is_b); | ||
| 1184 | EXPECT(leonida::title_frames::run_end(tf_base + 5 * kTile, tf_base + 6 * kTile, is_b) == tf_base + 6 * kTile && | ||
| 1185 | is_b); | ||
| 1186 | const std::uintptr_t query[6] = {tf_base, tf_base + 1 * kTile + 123, tf_base + 2 * kTile, | ||
| 1187 | tf_base + 3 * kTile, tf_base + 4 * kTile, tf_base + 5 * kTile}; | ||
| 1188 | std::uint8_t out[6] = {}; | ||
| 1189 | leonida::title_frames::backed_batch(query, out, 6); | ||
| 1190 | EXPECT(out[0] == 0 && out[1] == 1 && out[2] == 1 && out[3] == 0 && out[4] == 0 && out[5] == 1); | ||
| 1191 | EXPECT(s_run_end_calls == 4 && s_backed_batch_calls == 1 && s_lookup_calls == 0); | ||
| 1192 | |||
| 1193 | // 2. Fallback without fast hooks (2-arg set_lookup): identical results via lookup_fn. | ||
| 1194 | leonida::title_frames::set_lookup(lookup_fn, nullptr); | ||
| 1195 | std::uint8_t out2[6] = {}; | ||
| 1196 | leonida::title_frames::backed_batch(query, out2, 6); | ||
| 1197 | for (int i = 0; i < 6; ++i) EXPECT(out2[i] == out[i]); | ||
| 1198 | EXPECT(leonida::title_frames::run_end(tf_base + 1 * kTile, tf_base + 6 * kTile, is_b) == tf_base + 3 * kTile && | ||
| 1199 | is_b); | ||
| 1200 | EXPECT(s_lookup_calls > 0); | ||
| 1201 | leonida::title_frames::set_lookup(nullptr, nullptr); | ||
| 1202 | } | ||
| 1053 | if (failures) return 1; | 1203 | if (failures) return 1; |
| 1054 | std::puts("title texture watch: new, untouched, one page, two addresses, remap, every frame, unarmable, kernel " | 1204 | std::puts("title texture watch: new, untouched, one page, two addresses, remap, every frame, unarmable, kernel " |
| 1055 | "write in progress, lost queue, O(1) fast-path, buffer-hot decoupling, 1-tile hash-before-reserve, " | 1205 | "write in progress, lost queue, O(1) fast-path, buffer-hot decoupling, 1-tile hash-before-reserve, " |
| 1056 | "note_changed arm-skip, 86k-tile region index, hot_generation skip, non_hot_arms, mirror piece hash, " | 1206 | "note_changed arm-skip, 86k-tile region index, hot_generation skip, non_hot_arms, mirror piece hash, " |
| 1057 | "examine_batch, max_version, peer-skip, 41k-tex scale bench"); | 1207 | "examine_batch, max_version, peer-skip, batched peer sync, title_frames batch, 41k-tex scale bench"); |
| 1058 | return 0; | 1208 | return 0; |
| 1059 | } | 1209 | } |
diff --git a/runtime/test/vk_title_detile_test.cpp b/runtime/test/vk_title_detile_test.cpp index 326f1fc02..37adbd526 100644 --- a/runtime/test/vk_title_detile_test.cpp +++ b/runtime/test/vk_title_detile_test.cpp | |||
| @@ -12,6 +12,7 @@ | |||
| 12 | #include "vk_test_device.h" | 12 | #include "vk_test_device.h" |
| 13 | #include "../src/console_texture_layout.h" | 13 | #include "../src/console_texture_layout.h" |
| 14 | #include "../src/vulkan_title_detile.h" | 14 | #include "../src/vulkan_title_detile.h" |
| 15 | #include "../src/vulkan_title_image_pool.h" | ||
| 15 | 16 | ||
| 16 | #include <algorithm> | 17 | #include <algorithm> |
| 17 | #include <atomic> | 18 | #include <atomic> |
| @@ -631,8 +632,201 @@ int main() { | |||
| 631 | EXPECT(d.validation_errors() == 0); | 632 | EXPECT(d.validation_errors() == 0); |
| 632 | } | 633 | } |
| 633 | 634 | ||
| 634 | std::printf("vk_title_detile_test: %u cases + batched descriptor/cached PFN checks passed bit-for-bit " | 635 | // Verify linear 3D direct copy with mips > 1 (array_layers == 1) bit-for-bit against console_texture::Layout, |
| 635 | "(13 formats x 14 layout/mode/mip/array/odd/linear cases, 0 validation errors)\n", | 636 | // plus rejection of invalid array_layers != 1 for volume textures. |
| 637 | { | ||
| 638 | namespace ct = leonida::console_texture; | ||
| 639 | struct VolumeCase { | ||
| 640 | const char* name; | ||
| 641 | VkFormat vk_format; | ||
| 642 | std::uint32_t element_bytes; | ||
| 643 | std::uint32_t block_side; | ||
| 644 | std::uint32_t width; | ||
| 645 | std::uint32_t height; | ||
| 646 | std::uint32_t depth; | ||
| 647 | std::uint32_t mips; | ||
| 648 | VkDeviceSize src_offset; | ||
| 649 | }; | ||
| 650 | const VolumeCase vcases[] = { | ||
| 651 | {"RGBA8 32x24x8 mips=4", VK_FORMAT_R8G8B8A8_UNORM, 4, 1, 32, 24, 8, 4, 4096}, | ||
| 652 | {"R8 17x13x5 mips=3", VK_FORMAT_R8_UNORM, 1, 1, 17, 13, 5, 3, 256}, | ||
| 653 | {"RGBA16F 16x12x6 mips=4", VK_FORMAT_R16G16B16A16_SFLOAT, 8, 1, 16, 12, 6, 4, 512}, | ||
| 654 | {"BC1 32x32x4 mips=3", VK_FORMAT_BC1_RGBA_UNORM_BLOCK, 8, 4, 32, 32, 4, 3, 4096}, | ||
| 655 | {"RGBA8 16x16x4 mips=1", VK_FORMAT_R8G8B8A8_UNORM, 4, 1, 16, 16, 4, 1, 256}, | ||
| 656 | }; | ||
| 657 | for (const auto& vc : vcases) { | ||
| 658 | ct::Desc cd{}; | ||
| 659 | cd.dimension = ct::Dimension::tex3d; | ||
| 660 | cd.width = vc.width; | ||
| 661 | cd.height = vc.height; | ||
| 662 | cd.depth = vc.depth; | ||
| 663 | cd.mips = vc.mips; | ||
| 664 | cd.element_bytes = vc.element_bytes; | ||
| 665 | cd.block = vc.block_side; | ||
| 666 | cd.samples = 1; | ||
| 667 | cd.swizzle = ct::kLinear; | ||
| 668 | const auto layout_opt = ct::Layout::make(cd); | ||
| 669 | EXPECT(layout_opt.has_value()); | ||
| 670 | const ct::Layout& layout = *layout_opt; | ||
| 671 | leonida::TitleDetileDesc desc = to_detile_desc(layout); | ||
| 672 | desc.dimension = 4; | ||
| 673 | desc.array_layers = 1; | ||
| 674 | |||
| 675 | const VkDeviceSize chunk_size = vc.src_offset + layout.size + 4096; | ||
| 676 | Buffer mirror_chunk; | ||
| 677 | EXPECT(make_host_buffer(d, chunk_size, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, mirror_chunk)); | ||
| 678 | std::memset(mirror_chunk.mapped, 0xCC, std::size_t(chunk_size)); | ||
| 679 | std::uint8_t* const tiled_ptr = mirror_chunk.mapped + vc.src_offset; | ||
| 680 | std::uint64_t seed = 0x517cc1b727220a95ull ^ (serial * 0x94d049bb133111ebull); | ||
| 681 | for (std::size_t i = 0; i < layout.size; ++i) { | ||
| 682 | seed = seed * 6364136223846793005ull + 1442695040888963407ull; | ||
| 683 | tiled_ptr[i] = static_cast<std::uint8_t>(seed >> 56); | ||
| 684 | } | ||
| 685 | |||
| 686 | // Invalid array_layers > 1 on a 3D volume must be rejected. | ||
| 687 | auto bad_desc = desc; | ||
| 688 | bad_desc.array_layers = 2; | ||
| 689 | EXPECT(!leonida::TitleDetilePipeline::can_direct_linear(bad_desc, vc.src_offset, chunk_size, vc.width, | ||
| 690 | vc.height, vc.depth, vc.mips, 2, true)); | ||
| 691 | |||
| 692 | EXPECT(leonida::TitleDetilePipeline::can_direct_linear(desc, vc.src_offset, chunk_size, vc.width, | ||
| 693 | vc.height, vc.depth, vc.mips, 1, true)); | ||
| 694 | std::vector<VkBufferImageCopy> copy_regions; | ||
| 695 | EXPECT(leonida::TitleDetilePipeline::build_linear_regions(desc, vc.src_offset, vc.width, vc.height, | ||
| 696 | vc.depth, true, copy_regions)); | ||
| 697 | |||
| 698 | std::vector<std::vector<std::uint8_t>> cpu_truth; | ||
| 699 | std::vector<VkBufferImageCopy> rb_regions; | ||
| 700 | VkDeviceSize rb_bytes = 0; | ||
| 701 | const VkDeviceSize rb_align = (std::max)(VkDeviceSize(4u), VkDeviceSize(vc.element_bytes)) * 4u; | ||
| 702 | for (std::uint32_t mip = 0; mip < vc.mips; ++mip) { | ||
| 703 | const auto& m = layout.mips[mip]; | ||
| 704 | const std::size_t row_bytes = std::size_t(m.width) * vc.element_bytes; | ||
| 705 | const std::size_t slice_bytes = row_bytes * m.height; | ||
| 706 | const std::size_t vol_bytes = slice_bytes * m.depth; | ||
| 707 | std::vector<std::uint8_t> linear(vol_bytes, 0xA5); | ||
| 708 | EXPECT(layout.detile(tiled_ptr, std::size_t(layout.size), mip, 0, linear.data(), linear.size(), | ||
| 709 | row_bytes, slice_bytes) == ct::Error::ok); | ||
| 710 | cpu_truth.push_back(std::move(linear)); | ||
| 711 | |||
| 712 | VkBufferImageCopy rb{}; | ||
| 713 | rb.bufferOffset = leonida::detail::align_up_size(rb_bytes, rb_align); | ||
| 714 | rb.imageSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, mip, 0, 1}; | ||
| 715 | rb.imageExtent = {(std::max)(1u, vc.width >> mip), (std::max)(1u, vc.height >> mip), | ||
| 716 | (std::max)(1u, vc.depth >> mip)}; | ||
| 717 | rb_regions.push_back(rb); | ||
| 718 | rb_bytes = rb.bufferOffset + vol_bytes; | ||
| 719 | } | ||
| 720 | |||
| 721 | Image img; | ||
| 722 | img.device = d.device; | ||
| 723 | img.destroy = d.fn<PFN_vkDestroyImage>("vkDestroyImage"); | ||
| 724 | img.free_mem = d.fn<PFN_vkFreeMemory>("vkFreeMemory"); | ||
| 725 | VkImageCreateInfo ii{VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO}; | ||
| 726 | ii.imageType = VK_IMAGE_TYPE_3D; | ||
| 727 | ii.format = vc.vk_format; | ||
| 728 | ii.extent = {vc.width, vc.height, vc.depth}; | ||
| 729 | ii.mipLevels = vc.mips; | ||
| 730 | ii.arrayLayers = 1; | ||
| 731 | ii.samples = VK_SAMPLE_COUNT_1_BIT; | ||
| 732 | ii.tiling = VK_IMAGE_TILING_OPTIMAL; | ||
| 733 | ii.usage = VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_SAMPLED_BIT; | ||
| 734 | EXPECT(d.fn<PFN_vkCreateImage>("vkCreateImage")(d.device, &ii, nullptr, &img.handle) == VK_SUCCESS); | ||
| 735 | VkMemoryRequirements req{}; | ||
| 736 | d.fn<PFN_vkGetImageMemoryRequirements>("vkGetImageMemoryRequirements")(d.device, img.handle, &req); | ||
| 737 | img.memory = alloc_memory(d, req, 0, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT); | ||
| 738 | EXPECT(img.memory != VK_NULL_HANDLE); | ||
| 739 | EXPECT(d.fn<PFN_vkBindImageMemory>("vkBindImageMemory")(d.device, img.handle, img.memory, 0) == VK_SUCCESS); | ||
| 740 | |||
| 741 | Buffer readback; | ||
| 742 | EXPECT(make_host_buffer(d, (std::max)(rb_bytes, VkDeviceSize(64)), VK_BUFFER_USAGE_TRANSFER_DST_BIT, | ||
| 743 | readback)); | ||
| 744 | std::memset(readback.mapped, 0x5A, std::size_t(readback.size)); | ||
| 745 | |||
| 746 | const bool ran = d.run([&](VkCommandBuffer cb) { | ||
| 747 | const auto barrier = d.fn<PFN_vkCmdPipelineBarrier>("vkCmdPipelineBarrier"); | ||
| 748 | const auto copy_b2i = d.fn<PFN_vkCmdCopyBufferToImage>("vkCmdCopyBufferToImage"); | ||
| 749 | const auto copy_i2b = d.fn<PFN_vkCmdCopyImageToBuffer>("vkCmdCopyImageToBuffer"); | ||
| 750 | VkImageMemoryBarrier to_dst{VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER}; | ||
| 751 | to_dst.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT; | ||
| 752 | to_dst.oldLayout = VK_IMAGE_LAYOUT_UNDEFINED; | ||
| 753 | to_dst.newLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL; | ||
| 754 | to_dst.srcQueueFamilyIndex = to_dst.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; | ||
| 755 | to_dst.image = img.handle; | ||
| 756 | to_dst.subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, vc.mips, 0, 1}; | ||
| 757 | barrier(cb, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, nullptr, 0, | ||
| 758 | nullptr, 1, &to_dst); | ||
| 759 | copy_b2i(cb, mirror_chunk.handle, img.handle, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, | ||
| 760 | static_cast<std::uint32_t>(copy_regions.size()), copy_regions.data()); | ||
| 761 | VkImageMemoryBarrier to_src = to_dst; | ||
| 762 | to_src.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT; | ||
| 763 | to_src.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT; | ||
| 764 | to_src.oldLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL; | ||
| 765 | to_src.newLayout = VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL; | ||
| 766 | barrier(cb, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, nullptr, 0, nullptr, | ||
| 767 | 1, &to_src); | ||
| 768 | copy_i2b(cb, img.handle, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, readback.handle, | ||
| 769 | static_cast<std::uint32_t>(rb_regions.size()), rb_regions.data()); | ||
| 770 | VkMemoryBarrier host_vis{VK_STRUCTURE_TYPE_MEMORY_BARRIER}; | ||
| 771 | host_vis.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT; | ||
| 772 | host_vis.dstAccessMask = VK_ACCESS_HOST_READ_BIT; | ||
| 773 | barrier(cb, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_HOST_BIT, 0, 1, &host_vis, 0, nullptr, 0, | ||
| 774 | nullptr); | ||
| 775 | return true; | ||
| 776 | }); | ||
| 777 | EXPECT(ran); | ||
| 778 | for (std::uint32_t mip = 0; mip < vc.mips; ++mip) { | ||
| 779 | const auto& expected = cpu_truth[mip]; | ||
| 780 | const std::uint8_t* actual = readback.mapped + rb_regions[mip].bufferOffset; | ||
| 781 | if (std::memcmp(actual, expected.data(), expected.size()) != 0) { | ||
| 782 | std::fprintf(stderr, "3D LINEAR MISMATCH case=%s mip=%u\n", vc.name, mip); | ||
| 783 | return 1; | ||
| 784 | } | ||
| 785 | } | ||
| 786 | ++serial; | ||
| 787 | ++passed; | ||
| 788 | } | ||
| 789 | EXPECT(d.validation_errors() == 0); | ||
| 790 | } | ||
| 791 | |||
| 792 | // Verify TitleImagePool raises largest_slot to 8 MiB and caps >2 MiB page allocations at <= 16 MiB. | ||
| 793 | { | ||
| 794 | leonida::TitleImagePool pool; | ||
| 795 | std::vector<VkDeviceSize> alloc_sizes; | ||
| 796 | std::uint64_t next_mem = 1; | ||
| 797 | auto fake_bind = [](VkDevice, VkImage, VkDeviceMemory, VkDeviceSize) -> VkResult { return VK_SUCCESS; }; | ||
| 798 | auto fake_destroy_img = [](VkDevice, VkImage, const VkAllocationCallbacks*) {}; | ||
| 799 | auto fake_free_mem = [](VkDevice, VkDeviceMemory, const VkAllocationCallbacks*) {}; | ||
| 800 | auto fake_alloc = [&](VkDeviceSize size, std::uint32_t& type) -> VkDeviceMemory { | ||
| 801 | alloc_sizes.push_back(size); | ||
| 802 | type = 0; | ||
| 803 | return reinterpret_cast<VkDeviceMemory>(next_mem++); | ||
| 804 | }; | ||
| 805 | const VkDevice fake_dev = reinterpret_cast<VkDevice>(std::uintptr_t(1)); | ||
| 806 | const VkImage fake_img = reinterpret_cast<VkImage>(std::uintptr_t(1)); | ||
| 807 | |||
| 808 | // 5.5 MiB texture -> pooled in 11 MiB page (capacity 2, <= 16 MiB cap), second reuses same page! | ||
| 809 | VkMemoryRequirements req_55{5632ull << 10, 65536, 1u}; | ||
| 810 | auto a1 = pool.bind(fake_dev, fake_img, req_55, fake_bind, fake_destroy_img, fake_free_mem, fake_alloc); | ||
| 811 | auto a2 = pool.bind(fake_dev, fake_img, req_55, fake_bind, fake_destroy_img, fake_free_mem, fake_alloc); | ||
| 812 | EXPECT(a1 && a2 && a1->page == a2->page); | ||
| 813 | EXPECT(alloc_sizes.size() == 1 && alloc_sizes[0] <= (16ull << 20)); | ||
| 814 | |||
| 815 | // 8 MiB texture -> pooled in 16 MiB page (capacity 2). | ||
| 816 | VkMemoryRequirements req_8m{8ull << 20, 65536, 1u}; | ||
| 817 | auto b1 = pool.bind(fake_dev, fake_img, req_8m, fake_bind, fake_destroy_img, fake_free_mem, fake_alloc); | ||
| 818 | auto b2 = pool.bind(fake_dev, fake_img, req_8m, fake_bind, fake_destroy_img, fake_free_mem, fake_alloc); | ||
| 819 | EXPECT(b1 && b2 && b1->page == b2->page); | ||
| 820 | EXPECT(alloc_sizes.size() == 2 && alloc_sizes[1] == (16ull << 20)); | ||
| 821 | |||
| 822 | // > 8 MiB texture -> refused by pool (returns nullptr). | ||
| 823 | VkMemoryRequirements req_9m{9ull << 20, 65536, 1u}; | ||
| 824 | auto c1 = pool.bind(fake_dev, fake_img, req_9m, fake_bind, fake_destroy_img, fake_free_mem, fake_alloc); | ||
| 825 | EXPECT(!c1 && alloc_sizes.size() == 2); | ||
| 826 | } | ||
| 827 | |||
| 828 | std::printf("vk_title_detile_test: %u cases + batched descriptor/cached PFN + linear 3D multi-mip + pool 8MiB " | ||
| 829 | "checks passed bit-for-bit (0 validation errors)\n", | ||
| 636 | passed); | 830 | passed); |
| 637 | return 0; | 831 | return 0; |
| 638 | } | 832 | } |
diff --git a/runtime/test/vulkan_title_image_pool_test.cpp b/runtime/test/vulkan_title_image_pool_test.cpp index 9666e70c0..5e6c07f9c 100644 --- a/runtime/test/vulkan_title_image_pool_test.cpp +++ b/runtime/test/vulkan_title_image_pool_test.cpp | |||
| @@ -7,7 +7,7 @@ | |||
| 7 | namespace { | 7 | namespace { |
| 8 | unsigned allocated, freed, destroyed, next_type; | 8 | unsigned allocated, freed, destroyed, next_type; |
| 9 | bool fail_alloc, fail_bind; | 9 | bool fail_alloc, fail_bind; |
| 10 | VkDeviceSize last_offset; | 10 | VkDeviceSize last_offset, last_alloc_size; |
| 11 | VKAPI_ATTR VkResult VKAPI_CALL bind_image(VkDevice, VkImage, VkDeviceMemory, VkDeviceSize offset) { | 11 | VKAPI_ATTR VkResult VKAPI_CALL bind_image(VkDevice, VkImage, VkDeviceMemory, VkDeviceSize offset) { |
| 12 | last_offset = offset; | 12 | last_offset = offset; |
| 13 | return fail_bind ? VK_ERROR_OUT_OF_DEVICE_MEMORY : VK_SUCCESS; | 13 | return fail_bind ? VK_ERROR_OUT_OF_DEVICE_MEMORY : VK_SUCCESS; |
| @@ -24,7 +24,8 @@ int main() { | |||
| 24 | const auto make = [&] { | 24 | const auto make = [&] { |
| 25 | const auto image = (VkImage)(std::uintptr_t(++next_image)); | 25 | const auto image = (VkImage)(std::uintptr_t(++next_image)); |
| 26 | return pool.bind(VK_NULL_HANDLE, image, req, bind_image, destroy_image, free_memory, | 26 | return pool.bind(VK_NULL_HANDLE, image, req, bind_image, destroy_image, free_memory, |
| 27 | [](VkDeviceSize, std::uint32_t& type) { | 27 | [](VkDeviceSize size, std::uint32_t& type) { |
| 28 | last_alloc_size = size; | ||
| 28 | type = next_type; | 29 | type = next_type; |
| 29 | return fail_alloc ? VkDeviceMemory{} : (VkDeviceMemory)(std::uintptr_t(++allocated)); | 30 | return fail_alloc ? VkDeviceMemory{} : (VkDeviceMemory)(std::uintptr_t(++allocated)); |
| 30 | }); | 31 | }); |
| @@ -61,11 +62,28 @@ int main() { | |||
| 61 | fail_alloc = true; | 62 | fail_alloc = true; |
| 62 | CHECK(!make() && allocated == 4 && freed == 4); | 63 | CHECK(!make() && allocated == 4 && freed == 4); |
| 63 | fail_alloc = false; | 64 | fail_alloc = false; |
| 64 | req.size++; | 65 | |
| 65 | CHECK(!make() && allocated == 4); | 66 | // > 2 MiB .. 8 MiB classes cap page_size at 16 MiB (no VRAM bloat) and pack stride * capacity tightly. |
| 67 | req = {5632u << 10, 65536, 2}; | ||
| 68 | for (int i = 0; i < 3; ++i) live.push_back(make()); | ||
| 69 | CHECK(live[0] && live[1] && live[2] && live[0]->page == live[1]->page && live[2]->page != live[1]->page); | ||
| 70 | CHECK(last_alloc_size <= (16u << 20) && allocated == 6); | ||
| 71 | live.clear(); | ||
| 72 | CHECK(freed == 6); | ||
| 73 | |||
| 74 | req = {8u << 20, 65536, 2}; | ||
| 75 | for (int i = 0; i < 3; ++i) live.push_back(make()); | ||
| 76 | CHECK(live[0] && live[1] && live[2] && live[0]->page == live[1]->page && live[2]->page != live[1]->page); | ||
| 77 | CHECK(last_alloc_size == (16u << 20) && allocated == 8); | ||
| 78 | live.clear(); | ||
| 79 | CHECK(freed == 8); | ||
| 80 | |||
| 81 | // > 8 MiB exceeds largest_slot and falls back to dedicated allocation. | ||
| 82 | req.size = (8u << 20) + 1; | ||
| 83 | CHECK(!make() && allocated == 8); | ||
| 66 | req = {701440, 512, 2}; | 84 | req = {701440, 512, 2}; |
| 67 | a = make(); b = make(); | 85 | a = make(); b = make(); |
| 68 | CHECK(a && b && a->page == b->page && last_offset % req.alignment == 0); | 86 | CHECK(a && b && a->page == b->page && last_offset % req.alignment == 0); |
| 69 | CHECK(last_offset >= req.size && last_offset <= req.size + req.size / 8); | 87 | CHECK(last_offset >= req.size && last_offset <= req.size + req.size / 8); |
| 70 | std::puts("title image pool alignment, type separation, retirement, reuse and fallback OK"); | 88 | std::puts("title image pool alignment, type separation, retirement, reuse, 8MiB/16MiB cap and fallback OK"); |
| 71 | } | 89 | } |