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 angle::ObjCPtr<id<MTLFunction>> vertexShader; 25 angle::ObjCPtr<id<MTLFunction>> fragmentShader; 26 RenderPipelineDesc pipelineDesc; 27 28 angle::ObjCPtr<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 angle::ObjCPtr<id<MTLRenderPipelineState>> *outRenderPipeline); 65 angle::Result getComputePipeline( 66 ContextMtl *context, 67 id<MTLFunction> computeShader, 68 angle::ObjCPtr<id<MTLComputePipelineState>> *outComputePipeline); 69 70 private: 71 static constexpr unsigned int kMaxPipelines = 128; 72 73 // The cache tries to clean up this many states at once. 74 static constexpr unsigned int kGCLimit = 32; 75 76 struct PipelineVariant 77 { 78 angle::ObjCPtr<id<MTLRenderPipelineState>> renderPipeline; 79 angle::ObjCPtr<id<MTLComputePipelineState>> computePipeline; 80 }; 81 82 using RenderPipelineMap = angle::base::HashingMRUCache<PipelineKey, PipelineVariant>; 83 RenderPipelineMap mPipelineCache; 84 }; 85 86 } // namespace mtl 87 } // namespace rx 88 89 #endif // LIBANGLE_RENDERER_METAL_MTL_PIPELINE_CACHE_H_ 90