Debian Codegraph @

Source/Core/VideoCommon/Assets/CustomAssetCache.h

raw symbols SCIP
1 // Copyright 2025 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #pragma once
5
6 #include <list>
7 #include <map>
8 #include <memory>
9 #include <set>
10 #include <vector>
11
12 #include "Common/CommonTypes.h"
13
14 #include "VideoCommon/Assets/AssetListener.h"
15 #include "VideoCommon/Assets/CustomAsset.h"
16 #include "VideoCommon/Assets/CustomAssetLibrary.h"
17 #include "VideoCommon/Assets/CustomAssetLoader.h"
18
19 namespace VideoCommon
20 {
21 // The asset cache manages custom assets (textures, shaders, meshes).
22 // These assets are loaded using a priority system,
23 // where assets requested more often gets loaded first. This system
24 // also tracks memory usage and if memory usage goes over a calculated limit,
25 // then assets will be purged with older assets being targeted first.
26 class CustomAssetCache
27 {
28 public:
29 // A generic interface to describe an asset
30 // and load state
31 struct AssetData
32 {
33 std::unique_ptr<CustomAsset> asset;
34
35 std::vector<AssetListener*> listeners;
36
37 CustomAsset::TimeType load_request_time = {};
38 bool has_load_error = false;
39
40 enum class LoadStatus
41 {
42 PendingReload,
43 LoadFinished,
44 Unloaded,
45 };
46 LoadStatus load_status = LoadStatus::PendingReload;
47 };
48
49 template <typename T>
50 T* CreateAsset(const CustomAssetLibrary::AssetID& asset_id,
51 std::shared_ptr<VideoCommon::CustomAssetLibrary> library, AssetListener* listener)
52 {
53 const auto [it, added] =
54 m_asset_id_to_handle.try_emplace(asset_id, m_asset_handle_to_data.size());
55 if (added)
56 {
57 AssetData asset_data;
58 asset_data.asset = std::make_unique<T>(std::move(library), asset_id, it->second);
59 asset_data.load_request_time = {};
60 asset_data.has_load_error = false;
61
62 m_asset_handle_to_data.insert_or_assign(it->second, std::move(asset_data));
63 }
64
65 auto& asset_data_from_handle = m_asset_handle_to_data[it->second];
66 asset_data_from_handle.listeners.push_back(listener);
67 asset_data_from_handle.load_status = AssetData::LoadStatus::PendingReload;
68
69 return static_cast<T*>(asset_data_from_handle.asset.get());
70 }
71
72 AssetData* GetAssetData(const CustomAssetLibrary::AssetID& asset_id)
73 {
74 const auto it_handle = m_asset_id_to_handle.find(asset_id);
75 if (it_handle == m_asset_id_to_handle.end())
76 return nullptr;
77 return &m_asset_handle_to_data[it_handle->second];
78 }
79
80 void Initialize();
81 void Shutdown();
82
83 void Reset();
84
85 // Request that an asset be reloaded
86 void MarkAssetDirty(const CustomAssetLibrary::AssetID& asset_id);
87
88 // Notify the system that we are interested in this asset and
89 // are waiting for it to be loaded
90 void MarkAssetPending(CustomAsset* asset);
91
92 // Notify the system we are interested in this asset and
93 // it has seen activity
94 void MarkAssetActive(CustomAsset* asset);
95
96 void Update();
97
98 private:
99 void ProcessDirtyAssets();
100 void ProcessLoadedAssets();
101 void RemoveAssetsUntilBelowMemoryLimit();
102
103 // Maintains a priority-sorted list of assets.
104 // Used to figure out which assets to load or unload first.
105 // Most recently used assets get marked with highest priority.
106 class AssetPriorityQueue
107 {
108 public:
109 const auto& Elements() const { return m_assets; }
110
111 // Inserts or moves the asset to the top of the queue.
112 void MakeAssetHighestPriority(u64 asset_handle, CustomAsset* asset)
113 {
114 RemoveAsset(asset_handle);
115 m_assets.push_front(asset);
116
117 // See CreateAsset for how a handle gets defined
118 if (asset_handle >= m_iterator_lookup.size())
119 m_iterator_lookup.resize(asset_handle + 1, m_assets.end());
120
121 m_iterator_lookup[asset_handle] = m_assets.begin();
122 }
123
124 // Inserts an asset at lowest priority or
125 // does nothing if asset is already in the queue.
126 void InsertAsset(u64 asset_handle, CustomAsset* asset)
127 {
128 if (asset_handle >= m_iterator_lookup.size())
129 m_iterator_lookup.resize(asset_handle + 1, m_assets.end());
130
131 if (m_iterator_lookup[asset_handle] == m_assets.end())
132 {
133 m_assets.push_back(asset);
134 m_iterator_lookup[asset_handle] = std::prev(m_assets.end());
135 }
136 }
137
138 CustomAsset* RemoveLowestPriorityAsset()
139 {
140 if (m_assets.empty()) [[unlikely]]
141 return nullptr;
142 auto* const ret = m_assets.back();
143 if (ret != nullptr)
144 {
145 m_iterator_lookup[ret->GetHandle()] = m_assets.end();
146 }
147 m_assets.pop_back();
148 return ret;
149 }
150
151 void RemoveAsset(u64 asset_handle)
152 {
153 if (asset_handle >= m_iterator_lookup.size())
154 return;
155
156 const auto iter = m_iterator_lookup[asset_handle];
157 if (iter != m_assets.end())
158 {
159 m_assets.erase(iter);
160 m_iterator_lookup[asset_handle] = m_assets.end();
161 }
162 }
163
164 bool IsEmpty() const { return m_assets.empty(); }
165
166 std::size_t Size() const { return m_assets.size(); }
167
168 private:
169 std::list<CustomAsset*> m_assets;
170
171 // Handle-to-iterator lookup for fast access.
172 // Grows as needed on insert.
173 std::vector<decltype(m_assets)::iterator> m_iterator_lookup;
174 };
175
176 // Assets that are currently active in memory, in order of most recently used by the game.
177 AssetPriorityQueue m_active_assets;
178
179 // Assets that need to be loaded.
180 // e.g. Because the game tried to use them or because they changed on disk.
181 // Ordered by most recently used.
182 AssetPriorityQueue m_pending_assets;
183
184 std::map<std::size_t, AssetData> m_asset_handle_to_data;
185 std::map<CustomAssetLibrary::AssetID, std::size_t> m_asset_id_to_handle;
186
187 // Memory used by currently "loaded" assets.
188 u64 m_ram_used = 0;
189
190 // A calculated amount of memory to avoid exceeding.
191 u64 m_max_ram_available = 0;
192
193 std::mutex m_dirty_mutex;
194 std::set<CustomAssetLibrary::AssetID> m_dirty_assets;
195
196 CustomAssetLoader m_asset_loader;
197 };
198
199 } // namespace VideoCommon