Timothy Liang | e30739a | 2018-07-31 10:51:17 -0400 | [diff] [blame] | 1 | /* |
| 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 Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "include/private/SkTArray.h" |
| 12 | #include "src/core/SkLRUCache.h" |
| 13 | #include "src/gpu/mtl/GrMtlCopyPipelineState.h" |
Jim Van Verth | 75c5326 | 2019-04-26 12:23:51 -0400 | [diff] [blame] | 14 | #include "src/gpu/mtl/GrMtlDepthStencil.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 15 | #include "src/gpu/mtl/GrMtlPipelineStateBuilder.h" |
Jim Van Verth | 75c5326 | 2019-04-26 12:23:51 -0400 | [diff] [blame] | 16 | #include "src/gpu/mtl/GrMtlSampler.h" |
Timothy Liang | e30739a | 2018-07-31 10:51:17 -0400 | [diff] [blame] | 17 | |
| 18 | #import <metal/metal.h> |
| 19 | |
| 20 | class GrMtlGpu; |
| 21 | |
| 22 | class GrMtlResourceProvider { |
| 23 | public: |
Jim Van Verth | 1223e7f | 2019-02-28 17:38:35 -0500 | [diff] [blame] | 24 | GrMtlResourceProvider(GrMtlGpu* gpu); |
Timothy Liang | e30739a | 2018-07-31 10:51:17 -0400 | [diff] [blame] | 25 | |
| 26 | GrMtlCopyPipelineState* findOrCreateCopyPipelineState(MTLPixelFormat dstPixelFormat, |
| 27 | id<MTLFunction> vertexFunction, |
| 28 | id<MTLFunction> fragmentFunction, |
| 29 | MTLVertexDescriptor* vertexDescriptor); |
| 30 | |
Jim Van Verth | 1223e7f | 2019-02-28 17:38:35 -0500 | [diff] [blame] | 31 | GrMtlPipelineState* findOrCreateCompatiblePipelineState( |
| 32 | GrRenderTarget*, GrSurfaceOrigin, |
| 33 | const GrPipeline&, |
| 34 | const GrPrimitiveProcessor&, |
| 35 | const GrTextureProxy* const primProcProxies[], |
| 36 | GrPrimitiveType); |
| 37 | |
Jim Van Verth | 75c5326 | 2019-04-26 12:23:51 -0400 | [diff] [blame] | 38 | // 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 Verth | 35a67eb | 2019-05-03 10:58:40 -0400 | [diff] [blame] | 45 | id<MTLBuffer> getDynamicBuffer(size_t size, size_t* offset); |
| 46 | |
Jim Van Verth | cf23f58 | 2019-05-22 09:46:57 -0400 | [diff] [blame^] | 47 | // Destroy any cached resources. To be called before releasing the MtlDevice. |
| 48 | void destroyResources(); |
| 49 | |
Timothy Liang | e30739a | 2018-07-31 10:51:17 -0400 | [diff] [blame] | 50 | private: |
Jim Van Verth | 1223e7f | 2019-02-28 17:38:35 -0500 | [diff] [blame] | 51 | #ifdef SK_DEBUG |
| 52 | #define GR_PIPELINE_STATE_CACHE_STATS |
| 53 | #endif |
| 54 | |
| 55 | class PipelineStateCache : public ::SkNoncopyable { |
| 56 | public: |
| 57 | PipelineStateCache(GrMtlGpu* gpu); |
| 58 | ~PipelineStateCache(); |
| 59 | |
Jim Van Verth | cf23f58 | 2019-05-22 09:46:57 -0400 | [diff] [blame^] | 60 | void release(); |
Jim Van Verth | 1223e7f | 2019-02-28 17:38:35 -0500 | [diff] [blame] | 61 | GrMtlPipelineState* refPipelineState(GrRenderTarget*, GrSurfaceOrigin, |
| 62 | const GrPrimitiveProcessor&, |
| 63 | const GrTextureProxy* const primProcProxies[], |
| 64 | const GrPipeline&, |
| 65 | GrPrimitiveType); |
| 66 | |
| 67 | private: |
| 68 | enum { |
| 69 | // We may actually have kMaxEntries+1 PipelineStates in context because we create a new |
| 70 | // PipelineState before evicting from the cache. |
| 71 | kMaxEntries = 128, |
| 72 | }; |
| 73 | |
| 74 | struct Entry; |
| 75 | |
| 76 | struct DescHash { |
| 77 | uint32_t operator()(const GrProgramDesc& desc) const { |
| 78 | return SkOpts::hash_fn(desc.asKey(), desc.keyLength(), 0); |
| 79 | } |
| 80 | }; |
| 81 | |
| 82 | SkLRUCache<const GrMtlPipelineStateBuilder::Desc, std::unique_ptr<Entry>, DescHash> fMap; |
| 83 | |
| 84 | GrMtlGpu* fGpu; |
| 85 | |
| 86 | #ifdef GR_PIPELINE_STATE_CACHE_STATS |
| 87 | int fTotalRequests; |
| 88 | int fCacheMisses; |
| 89 | #endif |
| 90 | }; |
| 91 | |
Timothy Liang | e30739a | 2018-07-31 10:51:17 -0400 | [diff] [blame] | 92 | SkTArray<std::unique_ptr<GrMtlCopyPipelineState>> fCopyPipelineStateCache; |
| 93 | |
| 94 | GrMtlGpu* fGpu; |
Jim Van Verth | 1223e7f | 2019-02-28 17:38:35 -0500 | [diff] [blame] | 95 | |
| 96 | // Cache of GrMtlPipelineStates |
| 97 | std::unique_ptr<PipelineStateCache> fPipelineStateCache; |
Jim Van Verth | 75c5326 | 2019-04-26 12:23:51 -0400 | [diff] [blame] | 98 | |
| 99 | SkTDynamicHash<GrMtlSampler, GrMtlSampler::Key> fSamplers; |
| 100 | SkTDynamicHash<GrMtlDepthStencil, GrMtlDepthStencil::Key> fDepthStencilStates; |
Jim Van Verth | 35a67eb | 2019-05-03 10:58:40 -0400 | [diff] [blame] | 101 | |
| 102 | // Buffer state |
| 103 | struct BufferState { |
| 104 | id<MTLBuffer> fAllocation; |
| 105 | size_t fAllocationSize; |
| 106 | size_t fNextOffset; |
| 107 | }; |
| 108 | BufferState fBufferState; |
Timothy Liang | e30739a | 2018-07-31 10:51:17 -0400 | [diff] [blame] | 109 | }; |
| 110 | |
| 111 | #endif |