Hash :
d4083c79
Author :
Date :
2025-01-31T15:06:36
Metal: Make ObjCPtr available to all modules Move ObjCPtr implementation to src/common/ObjCPtr.h, angle::ObjCPtr so that it's available also outside libANGLE. Bug: angleproject:393263506 Change-Id: I8ecd5632c7ae33ef4b409fc820fad229e91bc0ab Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/6219318 Auto-Submit: Kimmo Kinnunen <kkinnunen@apple.com> Reviewed-by: Geoff Lang <geofflang@chromium.org> Commit-Queue: Kimmo Kinnunen <kkinnunen@apple.com>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
//
// 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 <type_traits>
namespace rx
{
namespace mtl
{
class LibraryCache : angle::NonCopyable
{
public:
LibraryCache();
angle::ObjCPtr<id<MTLLibrary>> get(const std::shared_ptr<const std::string> &source,
const std::map<std::string, std::string> ¯os,
bool disableFastMath,
bool usesInvariance);
angle::ObjCPtr<id<MTLLibrary>> getOrCompileShaderLibrary(
DisplayMtl *displayMtl,
const std::shared_ptr<const std::string> &source,
const std::map<std::string, std::string> ¯os,
bool disableFastMath,
bool usesInvariance,
angle::ObjCPtr<NSError> *errorOut);
private:
struct LibraryKey
{
LibraryKey() = default;
LibraryKey(const std::shared_ptr<const std::string> &source,
const std::map<std::string, std::string> ¯os,
bool disableFastMath,
bool usesInvariance);
std::shared_ptr<const std::string> source;
std::map<std::string, std::string> 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<id<MTLLibrary>> library;
// Lock for this specific library to avoid multiple threads compiling the same shader at
// once.
std::mutex lock;
};
LibraryCacheEntry &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<LibraryKey, LibraryCacheEntry, LibraryKeyHasher>;
CacheMap mCache;
};
} // namespace mtl
} // namespace rx
#endif // LIBANGLE_RENDERER_METAL_MTL_LIBRARY_CACHE_H_