1 // 2 // Copyright 2023 The ANGLE Project Authors. All rights reserved. 3 // Use of this source code is governed by a BSD-style license that can be 4 // found in the LICENSE file. 5 // 6 // mtl_pipeline_cache.h: 7 // Defines classes for caching of mtl pipelines 8 // 9 10 #ifndef LIBANGLE_RENDERER_METAL_MTL_PIPELINE_CACHE_H_ 11 #define LIBANGLE_RENDERER_METAL_MTL_PIPELINE_CACHE_H_ 12 13 #include "common/hash_utils.h" 14 #include "libANGLE/SizedMRUCache.h" 15 #include "libANGLE/renderer/metal/mtl_utils.h" 16 17 namespace rx 18 { 19 namespace mtl 20 { 21 22 struct PipelineKey 23 { 24 AutoObjCPtr<id<MTLFunction>> vertexShader; 25 AutoObjCPtr<id<MTLFunction>> fragmentShader; 26 RenderPipelineDesc pipelineDesc; 27 28 AutoObjCPtr<id<MTLFunction>> computeShader; 29 30 bool isRenderPipeline() const; 31 32 bool operator==(const PipelineKey &rhs) const; 33 size_t hash() const; 34 }; 35 36 } // namespace mtl 37 } // namespace rx 38 39 namespace std 40 { 41 42 template <> 43 struct hash<rx::mtl::PipelineKey> 44 { 45 size_t operator()(const rx::mtl::PipelineKey &key) const { return key.hash(); } 46 }; 47 48 } // namespace std 49 50 namespace rx 51 { 52 namespace mtl 53 { 54 55 class PipelineCache : angle::NonCopyable 56 { 57 public: 58 PipelineCache(); 59 60 angle::Result getRenderPipeline(ContextMtl *context, 61 id<MTLFunction> vertexShader, 62 id<MTLFunction> fragmentShader, 63 const RenderPipelineDesc &desc, 64 AutoObjCPtr<id<MTLRenderPipelineState>> *outRenderPipeline); 65 angle::Result getComputePipeline(ContextMtl *context, 66 id<MTLFunction> computeShader, 67 AutoObjCPtr<id<MTLComputePipelineState>> *outComputePipeline); 68 69 private: 70 static constexpr unsigned int kMaxPipelines = 128; 71 72 // The cache tries to clean up this many states at once. 73 static constexpr unsigned int kGCLimit = 32; 74 75 struct PipelineVariant 76 { 77 AutoObjCPtr<id<MTLRenderPipelineState>> renderPipeline; 78 AutoObjCPtr<id<MTLComputePipelineState>> computePipeline; 79 }; 80 81 using RenderPipelineMap = angle::base::HashingMRUCache<PipelineKey, PipelineVariant>; 82 RenderPipelineMap mPipelineCache; 83 }; 84 85 } // namespace mtl 86 } // namespace rx 87 88 #endif // LIBANGLE_RENDERER_METAL_MTL_PIPELINE_CACHE_H_ 89