blob: 399f685547c2ff34a57321b1368ea4afddc7745b [file] [log] [blame]
Timothy Liange30739a2018-07-31 10:51:17 -04001/*
2 * Copyright 2018 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6*/
7
8#ifndef GrMtlResourceProvider_DEFINED
9#define GrMtlResourceProvider_DEFINED
10
Jim Van Verthbbf85f92019-06-20 12:38:38 -040011#include "include/private/SkSpinlock.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "include/private/SkTArray.h"
13#include "src/core/SkLRUCache.h"
Robert Phillips03e4c952019-11-26 16:20:22 -050014#include "src/gpu/GrProgramDesc.h"
Jim Van Verth75c53262019-04-26 12:23:51 -040015#include "src/gpu/mtl/GrMtlDepthStencil.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "src/gpu/mtl/GrMtlPipelineStateBuilder.h"
Jim Van Verth75c53262019-04-26 12:23:51 -040017#include "src/gpu/mtl/GrMtlSampler.h"
Timothy Liange30739a2018-07-31 10:51:17 -040018
Aaron O'Mullan829b6a02019-07-08 01:31:14 +020019#import <Metal/Metal.h>
Timothy Liange30739a2018-07-31 10:51:17 -040020
21class GrMtlGpu;
Jim Van Verthbbf85f92019-06-20 12:38:38 -040022class GrMtlCommandBuffer;
Timothy Liange30739a2018-07-31 10:51:17 -040023
24class GrMtlResourceProvider {
25public:
Jim Van Verth1223e7f2019-02-28 17:38:35 -050026 GrMtlResourceProvider(GrMtlGpu* gpu);
Timothy Liange30739a2018-07-31 10:51:17 -040027
Jim Van Verth84ded4d2021-03-03 13:29:38 -050028 GrMtlPipelineState* findOrCreateCompatiblePipelineState(const GrProgramDesc&,
Robert Phillipsfcaae482019-11-07 10:17:03 -050029 const GrProgramInfo&);
Jim Van Verth7d195ff2021-03-09 14:59:58 -050030 bool precompileShader(const SkData& key, const SkData& data);
Jim Van Verth1223e7f2019-02-28 17:38:35 -050031
Jim Van Verth75c53262019-04-26 12:23:51 -040032 // Finds or creates a compatible MTLDepthStencilState based on the GrStencilSettings.
33 GrMtlDepthStencil* findOrCreateCompatibleDepthStencilState(const GrStencilSettings&,
34 GrSurfaceOrigin);
35
36 // Finds or creates a compatible MTLSamplerState based on the GrSamplerState.
Brian Salomonccb61422020-01-09 10:46:36 -050037 GrMtlSampler* findOrCreateCompatibleSampler(GrSamplerState);
Jim Van Verth75c53262019-04-26 12:23:51 -040038
Jim Van Verthcf23f582019-05-22 09:46:57 -040039 // Destroy any cached resources. To be called before releasing the MtlDevice.
40 void destroyResources();
41
John Stiles38b7d2f2020-06-24 12:13:31 -040042#if GR_TEST_UTILS
43 void resetShaderCacheForTesting() const { fPipelineStateCache->release(); }
44#endif
45
Timothy Liange30739a2018-07-31 10:51:17 -040046private:
Jim Van Verth1223e7f2019-02-28 17:38:35 -050047#ifdef SK_DEBUG
48#define GR_PIPELINE_STATE_CACHE_STATS
49#endif
50
51 class PipelineStateCache : public ::SkNoncopyable {
52 public:
53 PipelineStateCache(GrMtlGpu* gpu);
54 ~PipelineStateCache();
55
Jim Van Verthcf23f582019-05-22 09:46:57 -040056 void release();
Jim Van Verth84ded4d2021-03-03 13:29:38 -050057 GrMtlPipelineState* refPipelineState(const GrProgramDesc&, const GrProgramInfo&);
Jim Van Verth7d195ff2021-03-09 14:59:58 -050058 bool precompileShader(const SkData& key, const SkData& data);
Jim Van Verth1223e7f2019-02-28 17:38:35 -050059
60 private:
Jim Van Verth1223e7f2019-02-28 17:38:35 -050061 struct Entry;
62
63 struct DescHash {
64 uint32_t operator()(const GrProgramDesc& desc) const {
65 return SkOpts::hash_fn(desc.asKey(), desc.keyLength(), 0);
66 }
67 };
68
Robert Phillips03e4c952019-11-26 16:20:22 -050069 SkLRUCache<const GrProgramDesc, std::unique_ptr<Entry>, DescHash> fMap;
Jim Van Verth1223e7f2019-02-28 17:38:35 -050070
Robert Phillips03e4c952019-11-26 16:20:22 -050071 GrMtlGpu* fGpu;
Jim Van Verth1223e7f2019-02-28 17:38:35 -050072
73#ifdef GR_PIPELINE_STATE_CACHE_STATS
74 int fTotalRequests;
75 int fCacheMisses;
76#endif
77 };
78
Timothy Liange30739a2018-07-31 10:51:17 -040079 GrMtlGpu* fGpu;
Jim Van Verth1223e7f2019-02-28 17:38:35 -050080
81 // Cache of GrMtlPipelineStates
82 std::unique_ptr<PipelineStateCache> fPipelineStateCache;
Jim Van Verth75c53262019-04-26 12:23:51 -040083
84 SkTDynamicHash<GrMtlSampler, GrMtlSampler::Key> fSamplers;
85 SkTDynamicHash<GrMtlDepthStencil, GrMtlDepthStencil::Key> fDepthStencilStates;
Timothy Liange30739a2018-07-31 10:51:17 -040086};
87
88#endif