// // Copyright 2023 The ANGLE Project Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // // mtl_library_cache.h: // Defines classes for caching of mtl libraries // #ifndef LIBANGLE_RENDERER_METAL_MTL_LIBRARY_CACHE_H_ #define LIBANGLE_RENDERER_METAL_MTL_LIBRARY_CACHE_H_ #include "libANGLE/renderer/metal/mtl_utils.h" #include namespace rx { namespace mtl { class LibraryCache : angle::NonCopyable { public: LibraryCache(); angle::ObjCPtr> get(const std::shared_ptr &source, const std::map ¯os, bool disableFastMath, bool usesInvariance); angle::ObjCPtr> getOrCompileShaderLibrary( DisplayMtl *displayMtl, const std::shared_ptr &source, const std::map ¯os, bool disableFastMath, bool usesInvariance, angle::ObjCPtr *errorOut); private: struct LibraryKey { LibraryKey() = default; LibraryKey(const std::shared_ptr &source, const std::map ¯os, bool disableFastMath, bool usesInvariance); std::shared_ptr source; std::map macros; bool disableFastMath; bool usesInvariance; bool operator==(const LibraryKey &other) const; }; struct LibraryKeyHasher { // Hash function size_t operator()(const LibraryKey &k) const; // Comparison bool operator()(const LibraryKey &a, const LibraryKey &b) const; }; struct LibraryCacheEntry : angle::NonCopyable { LibraryCacheEntry() = default; ~LibraryCacheEntry(); LibraryCacheEntry(LibraryCacheEntry &&moveFrom); // library can only go from the null -> not null state. It is safe to check if the library // already exists without locking. angle::ObjCPtr> library; // Lock for this specific library to avoid multiple threads compiling the same shader at // once. std::mutex lock; }; std::shared_ptr getCacheEntry(LibraryKey &&key); static constexpr unsigned int kMaxCachedLibraries = 128; // The cache tries to clean up this many states at once. static constexpr unsigned int kGCLimit = 32; // Lock for searching and adding new entries to the cache std::mutex mCacheLock; using CacheMap = angle::base:: HashingMRUCache, LibraryKeyHasher>; CacheMap mCache; }; } // namespace mtl } // namespace rx #endif // LIBANGLE_RENDERER_METAL_MTL_LIBRARY_CACHE_H_