game_launcher 1.1.0
Modern Linux game launcher with Steam / Proton / Wine integration
Loading...
Searching...
No Matches
shader_cache_manager.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// ==============================================================================
3// shader_cache_manager.h
4// ==============================================================================
5// Shader cache management for gaming on Linux
6// ==============================================================================
7// Manages shader caches for various graphics APIs and drivers:
8// - Mesa (AMD/Intel) shader cache
9// - NVIDIA shader cache
10// - DXVK shader cache
11// - VKD3D shader cache
12// - Wine/Proton state caches
13// - Steam shader pre-caching
14//
15// ## About Shader Caches
16// Shader compilation can cause stuttering in games. Caches store compiled
17// shaders to avoid recompilation. However, corrupted caches can cause crashes.
18// This service helps manage these caches.
19//
20// ## Thread Safety
21// All public methods are thread-safe.
22// ==============================================================================
23
24#pragma once
25
26#include <chrono>
27#include <filesystem>
28#include <functional>
29#include <memory>
30#include <optional>
31#include <string>
32#include <vector>
33
34// ==============================================================================
35// Data Structures
36// ==============================================================================
37
38/**
39 * @enum ShaderCacheType
40 * @brief Types of shader caches
41 */
43{
44 Mesa, ///< Mesa (AMD/Intel) GLCache and shader_cache
45 NVIDIA, ///< NVIDIA proprietary driver cache
46 DXVK, ///< DXVK Vulkan-based D3D implementation
47 VKD3D, ///< VKD3D-Proton D3D12 implementation
48 SteamShader, ///< Steam's shader pre-caching
49 Fossilize, ///< Fossilize pipeline cache
50 WineState, ///< Wine/Proton state cache
51 All ///< All cache types
52};
53
54/**
55 * @struct ShaderCacheInfo
56 * @brief Information about a shader cache
57 */
59{
61 std::string name; ///< Human-readable name
62 std::filesystem::path path; ///< Path to cache directory/file
63 int64_t size = 0; ///< Size in bytes
64 int fileCount = 0; ///< Number of cache files
65 std::chrono::system_clock::time_point lastModified;
66 bool exists = false; ///< Whether the cache exists
67
68 std::string getFormattedSize() const;
69 std::string getTypeName() const;
70};
71
72/**
73 * @struct GameShaderCache
74 * @brief Shader cache information for a specific game
75 */
77{
78 std::string gameId;
79 std::string gameName;
80 std::vector<ShaderCacheInfo> caches;
81 int64_t totalSize = 0;
82 bool hasDXVK = false;
83 bool hasVKD3D = false;
84 bool hasSteamCache = false;
85
86 std::string getFormattedTotalSize() const;
87};
88
89/**
90 * @struct ShaderCacheStats
91 * @brief Overall shader cache statistics
92 */
94{
95 int64_t totalSize = 0;
96 int64_t mesaSize = 0;
97 int64_t nvidiaSize = 0;
98 int64_t dxvkSize = 0;
99 int64_t vkd3dSize = 0;
100 int64_t steamSize = 0;
101 int64_t fossilizeSize = 0;
102 int64_t wineStateSize = 0;
104 int totalFiles = 0;
105
106 std::string getFormattedTotalSize() const;
107};
108
109/**
110 * @struct CacheClearResult
111 * @brief Result of a cache clearing operation
112 */
114{
115 bool success = true;
116 int64_t bytesCleared = 0;
119 std::vector<std::string> errors;
120
121 std::string getFormattedBytesCleared() const;
122};
123
124// ==============================================================================
125// ShaderCacheManager Class
126// ==============================================================================
127
128/**
129 * @class ShaderCacheManager
130 * @brief Service for managing shader caches
131 *
132 * Provides methods for discovering, analyzing, and clearing shader caches
133 * to help troubleshoot graphics issues and free disk space.
134 *
135 * ## Usage Example
136 * @code
137 * ShaderCacheManager manager;
138 *
139 * // Get overall stats
140 * auto stats = manager.getStats();
141 * std::cout << "Total cache size: " << stats.getFormattedTotalSize() << std::endl;
142 *
143 * // Clear DXVK cache for a game
144 * manager.clearGameCache("doom-eternal", ShaderCacheType::DXVK);
145 *
146 * // Clear all caches (careful!)
147 * manager.clearAllCaches();
148 * @endcode
149 */
151{
152 public:
153 using ProgressCallback = std::function<void(int percent, const std::string& status)>;
154
157
158 // ==========================================================================
159 // Cache Discovery
160 // ==========================================================================
161
162 /**
163 * @brief Get all detected shader caches
164 * @return Vector of cache info
165 */
166 std::vector<ShaderCacheInfo> getAllCaches() const;
167
168 /**
169 * @brief Get caches by type
170 * @param type Cache type to filter
171 * @return Vector of matching caches
172 */
173 std::vector<ShaderCacheInfo> getCachesByType(ShaderCacheType type) const;
174
175 /**
176 * @brief Get cache info for a specific path
177 * @param path Cache directory path
178 * @return Cache info if valid cache directory
179 */
180 std::optional<ShaderCacheInfo> getCacheInfo(const std::filesystem::path& path) const;
181
182 /**
183 * @brief Scan for all shader caches on the system
184 * @param progressCallback Progress updates
185 * @return Number of caches found
186 */
187 int scanCaches(const ProgressCallback& progressCallback = nullptr);
188
189 // ==========================================================================
190 // Game-Specific Caches
191 // ==========================================================================
192
193 /**
194 * @brief Get shader caches for a specific game
195 * @param gameId Game ID
196 * @param winePrefix Wine prefix path (for DXVK/VKD3D)
197 * @param steamAppId Steam app ID (for Steam shader cache)
198 * @return Game shader cache info
199 */
200 GameShaderCache getGameCache(const std::string& gameId,
201 const std::filesystem::path& winePrefix = {},
202 uint32_t steamAppId = 0) const;
203
204 /**
205 * @brief Find DXVK cache for a game
206 * @param winePrefix Wine prefix path
207 * @return DXVK cache info if found
208 */
209 std::optional<ShaderCacheInfo> findDXVKCache(const std::filesystem::path& winePrefix) const;
210
211 /**
212 * @brief Find VKD3D cache for a game
213 * @param winePrefix Wine prefix path
214 * @return VKD3D cache info if found
215 */
216 std::optional<ShaderCacheInfo> findVKD3DCache(const std::filesystem::path& winePrefix) const;
217
218 /**
219 * @brief Find Steam shader cache for a game
220 * @param steamAppId Steam app ID
221 * @return Steam shader cache info if found
222 */
223 std::optional<ShaderCacheInfo> findSteamShaderCache(uint32_t steamAppId) const;
224
225 // ==========================================================================
226 // Statistics
227 // ==========================================================================
228
229 /**
230 * @brief Get overall shader cache statistics
231 * @return Cache statistics
232 */
234
235 /**
236 * @brief Get total cache size
237 * @return Size in bytes
238 */
239 int64_t getTotalCacheSize() const;
240
241 /**
242 * @brief Get cache size by type
243 * @param type Cache type
244 * @return Size in bytes
245 */
246 int64_t getCacheSizeByType(ShaderCacheType type) const;
247
248 // ==========================================================================
249 // Cache Clearing
250 // ==========================================================================
251
252 /**
253 * @brief Clear a specific cache
254 * @param cachePath Path to cache directory
255 * @return Clear result
256 */
257 CacheClearResult clearCache(const std::filesystem::path& cachePath);
258
259 /**
260 * @brief Clear all caches of a specific type
261 * @param type Cache type to clear
262 * @param progressCallback Progress updates
263 * @return Clear result
264 */
266 const ProgressCallback& progressCallback = nullptr);
267
268 /**
269 * @brief Clear shader cache for a specific game
270 * @param gameId Game ID
271 * @param type Cache type (or All)
272 * @param winePrefix Wine prefix path
273 * @param steamAppId Steam app ID
274 * @return Clear result
275 */
276 CacheClearResult clearGameCache(const std::string& gameId,
278 const std::filesystem::path& winePrefix = {},
279 uint32_t steamAppId = 0);
280
281 /**
282 * @brief Clear all shader caches (use with caution!)
283 * @param progressCallback Progress updates
284 * @return Clear result
285 */
286 CacheClearResult clearAllCaches(const ProgressCallback& progressCallback = nullptr);
287
288 /**
289 * @brief Clear old/stale caches
290 * @param olderThan Clear caches older than this duration
291 * @param progressCallback Progress updates
292 * @return Clear result
293 */
294 CacheClearResult
295 clearStaleCaches(std::chrono::hours olderThan = std::chrono::hours(720), // 30 days
296 const ProgressCallback& progressCallback = nullptr);
297
298 // ==========================================================================
299 // Backup & Restore
300 // ==========================================================================
301
302 /**
303 * @brief Backup a shader cache before clearing
304 * @param cachePath Cache to backup
305 * @param backupDir Backup destination
306 * @return Backup path, or empty on failure
307 */
308 std::filesystem::path backupCache(const std::filesystem::path& cachePath,
309 const std::filesystem::path& backupDir = {});
310
311 /**
312 * @brief Restore a shader cache from backup
313 * @param backupPath Backup to restore
314 * @param targetPath Target cache location
315 * @return True on success
316 */
317 bool restoreCache(const std::filesystem::path& backupPath,
318 const std::filesystem::path& targetPath);
319
320 /**
321 * @brief Get list of cache backups
322 * @return Vector of backup paths
323 */
324 std::vector<std::filesystem::path> getCacheBackups() const;
325
326 /**
327 * @brief Delete old cache backups
328 * @param keepCount Number of recent backups to keep per cache
329 * @return Number of backups deleted
330 */
331 int pruneBackups(int keepCount = 3);
332
333 // ==========================================================================
334 // Configuration
335 // ==========================================================================
336
337 /**
338 * @brief Set the backup directory
339 * @param path Directory for cache backups
340 */
341 void setBackupDirectory(const std::filesystem::path& path);
342
343 /**
344 * @brief Get the backup directory
345 */
346 std::filesystem::path getBackupDirectory() const;
347
348 /**
349 * @brief Add a custom cache location to scan
350 * @param path Path to scan
351 * @param type Cache type
352 */
353 void addCustomCacheLocation(const std::filesystem::path& path, ShaderCacheType type);
354
355 /**
356 * @brief Set Steam library paths
357 * @param paths Steam library root paths
358 */
359 void setSteamLibraryPaths(const std::vector<std::filesystem::path>& paths);
360
361 // ==========================================================================
362 // Utility
363 // ==========================================================================
364
365 /**
366 * @brief Check if a path is a valid shader cache
367 * @param path Path to check
368 * @return Cache type if valid, nullopt otherwise
369 */
370 std::optional<ShaderCacheType> identifyCacheType(const std::filesystem::path& path) const;
371
372 /**
373 * @brief Get the default cache location for a type
374 * @param type Cache type
375 * @return Default path(s) for this cache type
376 */
377 std::vector<std::filesystem::path> getDefaultCacheLocations(ShaderCacheType type) const;
378
379 private:
380 struct Impl;
381 std::unique_ptr<Impl> impl_;
382
383 // Internal helpers
384 void scanMesaCaches();
385 void scanNVIDIACaches();
386 void scanDXVKCaches();
387 void scanVKD3DCaches();
388 void scanSteamCaches();
389 void scanFossilizeCaches();
390 void scanWineStateCaches();
391
392 int64_t calculateDirectorySize(const std::filesystem::path& path) const;
393 int countFiles(const std::filesystem::path& path) const;
394};
395
396/**
397 * @brief Get human-readable cache type name
398 */
399std::string shaderCacheTypeName(ShaderCacheType type);
std::optional< ShaderCacheInfo > findSteamShaderCache(uint32_t steamAppId) const
Find Steam shader cache for a game.
std::vector< ShaderCacheInfo > getCachesByType(ShaderCacheType type) const
Get caches by type.
std::filesystem::path backupCache(const std::filesystem::path &cachePath, const std::filesystem::path &backupDir={})
Backup a shader cache before clearing.
std::filesystem::path getBackupDirectory() const
Get the backup directory.
void setBackupDirectory(const std::filesystem::path &path)
Set the backup directory.
std::optional< ShaderCacheType > identifyCacheType(const std::filesystem::path &path) const
Check if a path is a valid shader cache.
CacheClearResult clearCache(const std::filesystem::path &cachePath)
Clear a specific cache.
int scanCaches(const ProgressCallback &progressCallback=nullptr)
Scan for all shader caches on the system.
int64_t getTotalCacheSize() const
Get total cache size.
void addCustomCacheLocation(const std::filesystem::path &path, ShaderCacheType type)
Add a custom cache location to scan.
std::vector< std::filesystem::path > getDefaultCacheLocations(ShaderCacheType type) const
Get the default cache location for a type.
void setSteamLibraryPaths(const std::vector< std::filesystem::path > &paths)
Set Steam library paths.
ShaderCacheStats getStats() const
Get overall shader cache statistics.
CacheClearResult clearCachesByType(ShaderCacheType type, const ProgressCallback &progressCallback=nullptr)
Clear all caches of a specific type.
CacheClearResult clearAllCaches(const ProgressCallback &progressCallback=nullptr)
Clear all shader caches (use with caution!).
std::optional< ShaderCacheInfo > findDXVKCache(const std::filesystem::path &winePrefix) const
Find DXVK cache for a game.
std::vector< std::filesystem::path > getCacheBackups() const
Get list of cache backups.
std::optional< ShaderCacheInfo > findVKD3DCache(const std::filesystem::path &winePrefix) const
Find VKD3D cache for a game.
GameShaderCache getGameCache(const std::string &gameId, const std::filesystem::path &winePrefix={}, uint32_t steamAppId=0) const
Get shader caches for a specific game.
int64_t getCacheSizeByType(ShaderCacheType type) const
Get cache size by type.
int pruneBackups(int keepCount=3)
Delete old cache backups.
std::function< void(int percent, const std::string &status)> ProgressCallback
std::optional< ShaderCacheInfo > getCacheInfo(const std::filesystem::path &path) const
Get cache info for a specific path.
std::vector< ShaderCacheInfo > getAllCaches() const
Get all detected shader caches.
CacheClearResult clearGameCache(const std::string &gameId, ShaderCacheType type=ShaderCacheType::All, const std::filesystem::path &winePrefix={}, uint32_t steamAppId=0)
Clear shader cache for a specific game.
bool restoreCache(const std::filesystem::path &backupPath, const std::filesystem::path &targetPath)
Restore a shader cache from backup.
CacheClearResult clearStaleCaches(std::chrono::hours olderThan=std::chrono::hours(720), const ProgressCallback &progressCallback=nullptr)
Clear old/stale caches.
ShaderCacheType
Types of shader caches.
@ WineState
Wine/Proton state cache.
@ Fossilize
Fossilize pipeline cache.
@ Mesa
Mesa (AMD/Intel) GLCache and shader_cache.
@ NVIDIA
NVIDIA proprietary driver cache.
@ DXVK
DXVK Vulkan-based D3D implementation.
@ All
All cache types.
@ VKD3D
VKD3D-Proton D3D12 implementation.
@ SteamShader
Steam's shader pre-caching.
std::string shaderCacheTypeName(ShaderCacheType type)
Get human-readable cache type name.
Result of a cache clearing operation.
std::vector< std::string > errors
std::string getFormattedBytesCleared() const
Shader cache information for a specific game.
std::string getFormattedTotalSize() const
std::vector< ShaderCacheInfo > caches
Information about a shader cache.
bool exists
Whether the cache exists.
std::string name
Human-readable name.
int64_t size
Size in bytes.
int fileCount
Number of cache files.
std::string getTypeName() const
std::chrono::system_clock::time_point lastModified
std::string getFormattedSize() const
ShaderCacheType type
std::filesystem::path path
Path to cache directory/file.
Overall shader cache statistics.
std::string getFormattedTotalSize() const