Hash :
9b00af52
Author :
Date :
2023-02-01T11:10:32
Metal: Add an in-memory MTLLibrary cache. Add a small cache for (msl + compile parameters) -> MTLLibrary at the egl::Display level. In regular executions of Chrome, the same shaders (particularly vertex) are compiled multiple times in different programs. Tested for a regular Chrome startup + open wikipedia + motionmark 1.2: 112/282 (40%) cache hits. Several different caching methods were profiled (LinkProgram perf test) - struct key with std::map : 303309 - struct key with std::unordered_map : 308090 - binary blob key with std::map : 263595 - binary blob key with std::unordered_map : 286051 - struct key + is_transparent with std::unordered_map : 304877 - struct key + is_transparent with absl::flat_hash_map : 335686 Using is_transparent allows us to search the hash map without copying the shader source string to construct the key structure. Bug: chromium:1385510 Change-Id: Ieec4ba526fe286276a4af7114d89cde32a8f9e1d Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/4214012 Reviewed-by: Shahbaz Youssefi <syoussefi@chromium.org> Commit-Queue: Geoff Lang <geofflang@chromium.org> Reviewed-by: Kenneth Russell <kbr@chromium.org>
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 98 99 100
//
// 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.mm:
// Defines classes for caching of mtl libraries
//
#include "libANGLE/renderer/metal/mtl_library_cache.h"
#include "common/hash_utils.h"
#include "common/mathutil.h"
#include "libANGLE/renderer/metal/ContextMtl.h"
namespace rx
{
namespace mtl
{
AutoObjCPtr<id<MTLLibrary>> LibraryCache::getOrCompileShaderLibrary(
ContextMtl *context,
const std::string &source,
const std::map<std::string, std::string> ¯os,
bool enableFastMath,
AutoObjCPtr<NSError *> *errorOut)
{
const angle::FeaturesMtl &features = context->getDisplay()->getFeatures();
if (!features.enableInMemoryMtlLibraryCache.enabled)
{
return CreateShaderLibrary(context->getMetalDevice(), source, macros, enableFastMath,
errorOut);
}
LibraryKey::LValueTuple lValueKey = std::tie(source, macros, enableFastMath);
auto iter = mCache.find(lValueKey);
if (iter != mCache.end())
{
return iter->second;
}
AutoObjCPtr<id<MTLLibrary>> library =
CreateShaderLibrary(context->getMetalDevice(), source, macros, enableFastMath, errorOut);
if (library)
{
mCache.insert_or_assign(LibraryKey(lValueKey), library);
}
return library;
}
LibraryCache::LibraryKey::LibraryKey(const LValueTuple &fromTuple)
{
source = std::get<0>(fromTuple);
macros = std::get<1>(fromTuple);
enableFastMath = std::get<2>(fromTuple);
}
LibraryCache::LibraryKey::LValueTuple LibraryCache::LibraryKey::tie() const
{
return std::tie(source, macros, enableFastMath);
}
size_t LibraryCache::LibraryKeyCompare::operator()(const LibraryKey::LValueTuple &k) const
{
size_t hash = std::hash<std::string>()(std::get<0>(k));
for (const auto ¯o : std::get<1>(k))
{
hash =
hash ^ std::hash<std::string>()(macro.first) ^ std::hash<std::string>()(macro.second);
}
hash = hash ^ std::hash<bool>()(std::get<2>(k));
return hash;
}
size_t LibraryCache::LibraryKeyCompare::operator()(const LibraryKey &k) const
{
return operator()(k.tie());
}
bool LibraryCache::LibraryKeyCompare::operator()(const LibraryKey &a, const LibraryKey &b) const
{
return a.tie() == b.tie();
}
bool LibraryCache::LibraryKeyCompare::operator()(const LibraryKey &a,
const LibraryKey::LValueTuple &b) const
{
return a.tie() == b;
}
bool LibraryCache::LibraryKeyCompare::operator()(const LibraryKey::LValueTuple &a,
const LibraryKey &b) const
{
return a == b.tie();
}
} // namespace mtl
} // namespace rx