blob: 43ecf1302bbde8affa626d8b2317502b26e1dd34 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/private/SkTArray.h"
12#include "src/core/SkLRUCache.h"
13#include "src/gpu/mtl/GrMtlCopyPipelineState.h"
Jim Van Verth75c53262019-04-26 12:23:51 -040014#include "src/gpu/mtl/GrMtlDepthStencil.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "src/gpu/mtl/GrMtlPipelineStateBuilder.h"
Jim Van Verth75c53262019-04-26 12:23:51 -040016#include "src/gpu/mtl/GrMtlSampler.h"
Timothy Liange30739a2018-07-31 10:51:17 -040017
18#import <metal/metal.h>
19
20class GrMtlGpu;
21
22class GrMtlResourceProvider {
23public:
Jim Van Verth1223e7f2019-02-28 17:38:35 -050024 GrMtlResourceProvider(GrMtlGpu* gpu);
Timothy Liange30739a2018-07-31 10:51:17 -040025
26 GrMtlCopyPipelineState* findOrCreateCopyPipelineState(MTLPixelFormat dstPixelFormat,
27 id<MTLFunction> vertexFunction,
28 id<MTLFunction> fragmentFunction,
29 MTLVertexDescriptor* vertexDescriptor);
30
Jim Van Verth1223e7f2019-02-28 17:38:35 -050031 GrMtlPipelineState* findOrCreateCompatiblePipelineState(
32 GrRenderTarget*, GrSurfaceOrigin,
33 const GrPipeline&,
34 const GrPrimitiveProcessor&,
35 const GrTextureProxy* const primProcProxies[],
36 GrPrimitiveType);
37
Jim Van Verth75c53262019-04-26 12:23:51 -040038 // Finds or creates a compatible MTLDepthStencilState based on the GrStencilSettings.
39 GrMtlDepthStencil* findOrCreateCompatibleDepthStencilState(const GrStencilSettings&,
40 GrSurfaceOrigin);
41
42 // Finds or creates a compatible MTLSamplerState based on the GrSamplerState.
43 GrMtlSampler* findOrCreateCompatibleSampler(const GrSamplerState&, uint32_t maxMipLevel);
44
Jim Van Verth35a67eb2019-05-03 10:58:40 -040045 id<MTLBuffer> getDynamicBuffer(size_t size, size_t* offset);
46
Timothy Liange30739a2018-07-31 10:51:17 -040047private:
Jim Van Verth1223e7f2019-02-28 17:38:35 -050048#ifdef SK_DEBUG
49#define GR_PIPELINE_STATE_CACHE_STATS
50#endif
51
52 class PipelineStateCache : public ::SkNoncopyable {
53 public:
54 PipelineStateCache(GrMtlGpu* gpu);
55 ~PipelineStateCache();
56
57 GrMtlPipelineState* refPipelineState(GrRenderTarget*, GrSurfaceOrigin,
58 const GrPrimitiveProcessor&,
59 const GrTextureProxy* const primProcProxies[],
60 const GrPipeline&,
61 GrPrimitiveType);
62
63 private:
64 enum {
65 // We may actually have kMaxEntries+1 PipelineStates in context because we create a new
66 // PipelineState before evicting from the cache.
67 kMaxEntries = 128,
68 };
69
70 struct Entry;
71
72 struct DescHash {
73 uint32_t operator()(const GrProgramDesc& desc) const {
74 return SkOpts::hash_fn(desc.asKey(), desc.keyLength(), 0);
75 }
76 };
77
78 SkLRUCache<const GrMtlPipelineStateBuilder::Desc, std::unique_ptr<Entry>, DescHash> fMap;
79
80 GrMtlGpu* fGpu;
81
82#ifdef GR_PIPELINE_STATE_CACHE_STATS
83 int fTotalRequests;
84 int fCacheMisses;
85#endif
86 };
87
Timothy Liange30739a2018-07-31 10:51:17 -040088 SkTArray<std::unique_ptr<GrMtlCopyPipelineState>> fCopyPipelineStateCache;
89
90 GrMtlGpu* fGpu;
Jim Van Verth1223e7f2019-02-28 17:38:35 -050091
92 // Cache of GrMtlPipelineStates
93 std::unique_ptr<PipelineStateCache> fPipelineStateCache;
Jim Van Verth75c53262019-04-26 12:23:51 -040094
95 SkTDynamicHash<GrMtlSampler, GrMtlSampler::Key> fSamplers;
96 SkTDynamicHash<GrMtlDepthStencil, GrMtlDepthStencil::Key> fDepthStencilStates;
Jim Van Verth35a67eb2019-05-03 10:58:40 -040097
98 // Buffer state
99 struct BufferState {
100 id<MTLBuffer> fAllocation;
101 size_t fAllocationSize;
102 size_t fNextOffset;
103 };
104 BufferState fBufferState;
Timothy Liange30739a2018-07-31 10:51:17 -0400105};
106
107#endif