| 1 | // Copyright 2025 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "VideoCommon/Assets/CustomAssetCache.h" |
| 5 | |
| 6 | #include "Common/Logging/Log.h" |
| 7 | #include "Common/MemoryUtil.h" |
| 8 | |
| 9 | #include "UICommon/UICommon.h" |
| 10 | |
| 11 | #include "VideoCommon/Assets/CustomAsset.h" |
| 12 | |
| 13 | namespace VideoCommon |
| 14 | { |
| 15 | void CustomAssetCache::Initialize() |
| 16 | { |
| 17 | // Use half of available system memory but leave at least 2GiB unused for system stability. |
| 18 | constexpr size_t must_keep_unused = 2 * size_t(1024 * 1024 * 1024); |
| 19 | |
| 20 | const size_t sys_mem = Common::MemPhysical(); |
| 21 | const size_t keep_unused_mem = std::max(sys_mem / 2, std::min(sys_mem, must_keep_unused)); |
| 22 | |
| 23 | m_max_ram_available = sys_mem - keep_unused_mem; |
| 24 | |
| 25 | if (m_max_ram_available == 0) |
| 26 | ERROR_LOG_FMT(VIDEO, "Not enough system memory for custom resources."); |
| 27 | |
| 28 | m_asset_loader.Initialize(); |
| 29 | } |
| 30 | |
| 31 | void CustomAssetCache::Shutdown() |
| 32 | { |
| 33 | Reset(); |
| 34 | |
| 35 | m_asset_loader.Shutdown(); |
| 36 | } |
| 37 | |
| 38 | void CustomAssetCache::Reset() |
| 39 | { |
| 40 | m_asset_loader.Reset(true); |
| 41 | |
| 42 | m_active_assets = {}; |
| 43 | m_pending_assets = {}; |
| 44 | m_asset_handle_to_data.clear(); |
| 45 | m_asset_id_to_handle.clear(); |
| 46 | m_dirty_assets.clear(); |
| 47 | m_ram_used = 0; |
| 48 | } |
| 49 | |
| 50 | void CustomAssetCache::MarkAssetDirty(const CustomAssetLibrary::AssetID& asset_id) |
| 51 | { |
| 52 | std::lock_guard guard(m_dirty_mutex); |
| 53 | m_dirty_assets.insert(asset_id); |
| 54 | } |
| 55 | |
| 56 | void CustomAssetCache::MarkAssetPending(CustomAsset* asset) |
| 57 | { |
| 58 | m_pending_assets.MakeAssetHighestPriority(asset->GetHandle(), asset); |
| 59 | } |
| 60 | |
| 61 | void CustomAssetCache::MarkAssetActive(CustomAsset* asset) |
| 62 | { |
| 63 | m_active_assets.MakeAssetHighestPriority(asset->GetHandle(), asset); |
| 64 | } |
| 65 | |
| 66 | void CustomAssetCache::Update() |
| 67 | { |
| 68 | ProcessDirtyAssets(); |
| 69 | ProcessLoadedAssets(); |
| 70 | |
| 71 | if (m_ram_used > m_max_ram_available) |
| 72 | { |
| 73 | RemoveAssetsUntilBelowMemoryLimit(); |
| 74 | } |
| 75 | |
| 76 | if (m_pending_assets.IsEmpty()) |
| 77 | return; |
| 78 | |
| 79 | if (m_ram_used > m_max_ram_available) |
| 80 | return; |
| 81 | |
| 82 | const u64 allowed_memory = m_max_ram_available - m_ram_used; |
| 83 | m_asset_loader.ScheduleAssetsToLoad(m_pending_assets.Elements(), allowed_memory); |
| 84 | } |
| 85 | |
| 86 | void CustomAssetCache::ProcessDirtyAssets() |
| 87 | { |
| 88 | decltype(m_dirty_assets) dirty_assets; |
| 89 | |
| 90 | if (const auto lk = std::unique_lock{m_dirty_mutex, std::try_to_lock}) |
| 91 | std::swap(dirty_assets, m_dirty_assets); |
| 92 | |
| 93 | const auto now = CustomAsset::ClockType::now(); |
| 94 | for (const auto& asset_id : dirty_assets) |
| 95 | { |
| 96 | if (const auto it = m_asset_id_to_handle.find(asset_id); it != m_asset_id_to_handle.end()) |
| 97 | { |
| 98 | const auto asset_handle = it->second; |
| 99 | AssetData& asset_data = m_asset_handle_to_data[asset_handle]; |
| 100 | asset_data.load_status = AssetData::LoadStatus::PendingReload; |
| 101 | asset_data.load_request_time = now; |
| 102 | |
| 103 | // Asset was reloaded, clear any errors we might have |
| 104 | asset_data.has_load_error = false; |
| 105 | |
| 106 | m_pending_assets.InsertAsset(it->second, asset_data.asset.get()); |
| 107 | |
| 108 | DEBUG_LOG_FMT(VIDEO, "Dirty asset pending reload: {}", asset_data.asset->GetAssetId()); |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | void CustomAssetCache::ProcessLoadedAssets() |
| 114 | { |
| 115 | const auto load_results = m_asset_loader.TakeLoadResults(); |
| 116 | |
| 117 | // Update the ram with the change in memory from the loader |
| 118 | // |
| 119 | // Note: Assets with outstanding reload requests will have |
| 120 | // two copies in memory temporarily (the old data stored in |
| 121 | // the asset shared_ptr that the resource manager owns, and |
| 122 | // the new data loaded from the loader in the asset's shared_ptr) |
| 123 | // This temporary duplication will not be reflected in the |
| 124 | // resource manager's ram used |
| 125 | m_ram_used += load_results.change_in_memory; |
| 126 | |
| 127 | for (const auto& [handle, load_successful] : load_results.asset_handles) |
| 128 | { |
| 129 | AssetData& asset_data = m_asset_handle_to_data[handle]; |
| 130 | |
| 131 | // If we have a reload request that is newer than our loaded time |
| 132 | // we need to wait for another reload. |
| 133 | if (asset_data.load_request_time > asset_data.asset->GetLastLoadedTime()) |
| 134 | continue; |
| 135 | |
| 136 | m_pending_assets.RemoveAsset(handle); |
| 137 | |
| 138 | asset_data.load_request_time = {}; |
| 139 | if (!load_successful) |
| 140 | { |
| 141 | asset_data.has_load_error = true; |
| 142 | } |
| 143 | else |
| 144 | { |
| 145 | m_active_assets.InsertAsset(handle, asset_data.asset.get()); |
| 146 | asset_data.load_status = AssetData::LoadStatus::LoadFinished; |
| 147 | } |
| 148 | |
| 149 | for (const auto& listener : asset_data.listeners) |
| 150 | { |
| 151 | if (load_successful) |
| 152 | listener->NotifyAssetLoadSuccess(); |
| 153 | else |
| 154 | listener->NotifyAssetLoadFailed(); |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | void CustomAssetCache::RemoveAssetsUntilBelowMemoryLimit() |
| 160 | { |
| 161 | const u64 threshold_ram = m_max_ram_available * 8 / 10; |
| 162 | |
| 163 | if (m_ram_used > threshold_ram) |
| 164 | { |
| 165 | INFO_LOG_FMT(VIDEO, "Memory usage over threshold: {}", UICommon::FormatSize(m_ram_used)); |
| 166 | } |
| 167 | |
| 168 | // Clear out least recently used resources until |
| 169 | // we get safely in our threshold |
| 170 | while (m_ram_used > threshold_ram && m_active_assets.Size() > 0) |
| 171 | { |
| 172 | auto* const asset = m_active_assets.RemoveLowestPriorityAsset(); |
| 173 | |
| 174 | AssetData& asset_data = m_asset_handle_to_data[asset->GetHandle()]; |
| 175 | |
| 176 | for (const auto& listener : asset_data.listeners) |
| 177 | { |
| 178 | listener->AssetUnloaded(); |
| 179 | } |
| 180 | |
| 181 | // Remove the asset's copy |
| 182 | const std::size_t bytes_unloaded = asset_data.asset->Unload(); |
| 183 | m_ram_used -= bytes_unloaded; |
| 184 | |
| 185 | asset_data.load_status = AssetData::LoadStatus::Unloaded; |
| 186 | asset_data.load_request_time = {}; |
| 187 | |
| 188 | INFO_LOG_FMT(VIDEO, "Unloading asset: {} ({})", asset_data.asset->GetAssetId(), |
| 189 | UICommon::FormatSize(bytes_unloaded)); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | } // namespace VideoCommon |