blob: 57481f98f3c0c3a0e6bb5ff4ca4995c60638e78b [file] [log] [blame]
Greg Daniel164a9f02016-02-22 09:56:40 -05001/*
2* Copyright 2016 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 GrVkResourceProvider_DEFINED
9#define GrVkResourceProvider_DEFINED
10
egdanield62e28b2016-06-07 08:43:30 -070011#include "GrResourceHandle.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050012#include "GrVkDescriptorPool.h"
egdaniela95220d2016-07-21 11:50:37 -070013#include "GrVkDescriptorSetManager.h"
Brian Salomon1471df92018-06-08 10:49:00 -040014#include "GrVkPipelineStateBuilder.h"
egdaniel2feb0932016-06-08 06:48:09 -070015#include "GrVkRenderPass.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050016#include "GrVkResource.h"
Greg Daniel7e000222018-12-03 10:08:21 -050017#include "GrVkSampler.h"
18#include "GrVkSamplerYcbcrConversion.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050019#include "GrVkUtil.h"
Ethan Nicholas87f340e2017-01-03 14:32:01 -050020#include "SkLRUCache.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050021#include "SkTArray.h"
egdaniel8b6394c2016-03-04 07:35:10 -080022#include "SkTDynamicHash.h"
egdanielaf132772016-03-28 12:39:29 -070023#include "SkTInternalLList.h"
Greg Daniel487132b2018-12-20 14:09:36 -050024#include "vk/GrVkTypes.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050025
Ethan Nicholas8e265a72018-12-12 16:22:40 -050026#include <mutex>
27#include <thread>
28
29class GrVkCommandPool;
egdanielbc9b2962016-09-27 08:00:53 -070030class GrVkCopyPipeline;
Greg Daniel164a9f02016-02-22 09:56:40 -050031class GrVkGpu;
32class GrVkPipeline;
Brian Salomon1471df92018-06-08 10:49:00 -040033class GrVkPipelineState;
jvanverth7ec92412016-07-06 09:24:57 -070034class GrVkPrimaryCommandBuffer;
Greg Daniel164a9f02016-02-22 09:56:40 -050035class GrVkRenderTarget;
jvanverth7ec92412016-07-06 09:24:57 -070036class GrVkSecondaryCommandBuffer;
egdaniel707bbd62016-07-26 07:19:47 -070037class GrVkUniformHandler;
Greg Daniel164a9f02016-02-22 09:56:40 -050038
39class GrVkResourceProvider {
40public:
41 GrVkResourceProvider(GrVkGpu* gpu);
42 ~GrVkResourceProvider();
43
jvanverth03509ea2016-03-02 13:19:47 -080044 // Set up any initial vk objects
45 void init();
46
Robert Phillipsd0fe8752019-01-31 14:13:59 -050047 GrVkPipeline* createPipeline(int numColorSamples,
48 const GrPrimitiveProcessor& primProc,
Brian Salomonff168d92018-06-23 15:17:27 -040049 const GrPipeline& pipeline,
csmartdaltonc633abb2016-11-01 08:55:55 -070050 const GrStencilSettings& stencil,
Greg Daniel164a9f02016-02-22 09:56:40 -050051 VkPipelineShaderStageCreateInfo* shaderStageInfo,
52 int shaderStageCount,
53 GrPrimitiveType primitiveType,
Greg Daniel99b88e02018-10-03 15:31:20 -040054 VkRenderPass compatibleRenderPass,
Greg Daniel164a9f02016-02-22 09:56:40 -050055 VkPipelineLayout layout);
56
egdanielbc9b2962016-09-27 08:00:53 -070057 GrVkCopyPipeline* findOrCreateCopyPipeline(const GrVkRenderTarget* dst,
58 VkPipelineShaderStageCreateInfo*,
59 VkPipelineLayout);
60
egdanield62e28b2016-06-07 08:43:30 -070061 GR_DEFINE_RESOURCE_HANDLE_CLASS(CompatibleRPHandle);
62
Greg Daniel164a9f02016-02-22 09:56:40 -050063 // Finds or creates a simple render pass that matches the target, increments the refcount,
egdanield62e28b2016-06-07 08:43:30 -070064 // and returns. The caller can optionally pass in a pointer to a CompatibleRPHandle. If this is
65 // non null it will be set to a handle that can be used in the furutre to quickly return a
66 // compatible GrVkRenderPasses without the need inspecting a GrVkRenderTarget.
67 const GrVkRenderPass* findCompatibleRenderPass(const GrVkRenderTarget& target,
68 CompatibleRPHandle* compatibleHandle = nullptr);
69 // The CompatibleRPHandle must be a valid handle previously set by a call to
70 // findCompatibleRenderPass(GrVkRenderTarget&, CompatibleRPHandle*).
71 const GrVkRenderPass* findCompatibleRenderPass(const CompatibleRPHandle& compatibleHandle);
72
Greg Danielb46add82019-01-02 14:51:29 -050073 const GrVkRenderPass* findCompatibleExternalRenderPass(VkRenderPass,
74 uint32_t colorAttachmentIndex);
75
egdaniel2feb0932016-06-08 06:48:09 -070076 // Finds or creates a render pass that matches the target and LoadStoreOps, increments the
77 // refcount, and returns. The caller can optionally pass in a pointer to a CompatibleRPHandle.
78 // If this is non null it will be set to a handle that can be used in the furutre to quickly
79 // return a GrVkRenderPasses without the need inspecting a GrVkRenderTarget.
egdanield62e28b2016-06-07 08:43:30 -070080 const GrVkRenderPass* findRenderPass(const GrVkRenderTarget& target,
egdaniel2feb0932016-06-08 06:48:09 -070081 const GrVkRenderPass::LoadStoreOps& colorOps,
egdaniel2feb0932016-06-08 06:48:09 -070082 const GrVkRenderPass::LoadStoreOps& stencilOps,
egdanield62e28b2016-06-07 08:43:30 -070083 CompatibleRPHandle* compatibleHandle = nullptr);
84
egdaniel2feb0932016-06-08 06:48:09 -070085 // The CompatibleRPHandle must be a valid handle previously set by a call to findRenderPass or
86 // findCompatibleRenderPass.
egdanield62e28b2016-06-07 08:43:30 -070087 const GrVkRenderPass* findRenderPass(const CompatibleRPHandle& compatibleHandle,
egdaniel2feb0932016-06-08 06:48:09 -070088 const GrVkRenderPass::LoadStoreOps& colorOps,
egdaniel2feb0932016-06-08 06:48:09 -070089 const GrVkRenderPass::LoadStoreOps& stencilOps);
90
Ethan Nicholas8e265a72018-12-12 16:22:40 -050091 GrVkCommandPool* findOrCreateCommandPool();
jvanverth7ec92412016-07-06 09:24:57 -070092
Ethan Nicholas8e265a72018-12-12 16:22:40 -050093 void checkCommandBuffers();
Ethan Nicholasbff4e072018-12-12 18:17:24 +000094
egdanielc2dc1b22016-03-18 13:18:23 -070095 // Finds or creates a compatible GrVkDescriptorPool for the requested type and count.
Greg Daniel164a9f02016-02-22 09:56:40 -050096 // The refcount is incremented and a pointer returned.
97 // TODO: Currently this will just create a descriptor pool without holding onto a ref itself
98 // so we currently do not reuse them. Rquires knowing if another draw is currently using
99 // the GrVkDescriptorPool, the ability to reset pools, and the ability to purge pools out
100 // of our cache of GrVkDescriptorPools.
egdanielc2dc1b22016-03-18 13:18:23 -0700101 GrVkDescriptorPool* findOrCreateCompatibleDescriptorPool(VkDescriptorType type, uint32_t count);
Greg Daniel164a9f02016-02-22 09:56:40 -0500102
Greg Daniel7e000222018-12-03 10:08:21 -0500103 // Finds or creates a compatible GrVkSampler based on the GrSamplerState and
104 // GrVkYcbcrConversionInfo. The refcount is incremented and a pointer returned.
105 GrVkSampler* findOrCreateCompatibleSampler(const GrSamplerState&,
106 const GrVkYcbcrConversionInfo& ycbcrInfo);
107
108 // Finds or creates a compatible GrVkSamplerYcbcrConversion based on the GrSamplerState and
109 // GrVkYcbcrConversionInfo. The refcount is incremented and a pointer returned.
110 GrVkSamplerYcbcrConversion* findOrCreateCompatibleSamplerYcbcrConversion(
111 const GrVkYcbcrConversionInfo& ycbcrInfo);
egdaniel8b6394c2016-03-04 07:35:10 -0800112
Greg Daniel9a51a862018-11-30 10:18:14 -0500113 GrVkPipelineState* findOrCreateCompatiblePipelineState(
Robert Phillipsd0fe8752019-01-31 14:13:59 -0500114 GrRenderTarget*, GrSurfaceOrigin,
Greg Daniel9a51a862018-11-30 10:18:14 -0500115 const GrPipeline&,
116 const GrPrimitiveProcessor&,
117 const GrTextureProxy* const primProcProxies[],
118 GrPrimitiveType,
119 VkRenderPass compatibleRenderPass);
egdaniel22281c12016-03-23 13:49:40 -0700120
Greg Daniela7543782017-05-02 14:01:43 -0400121 void getSamplerDescriptorSetHandle(VkDescriptorType type,
122 const GrVkUniformHandler&,
egdaniel707bbd62016-07-26 07:19:47 -0700123 GrVkDescriptorSetManager::Handle* handle);
Greg Daniela7543782017-05-02 14:01:43 -0400124 void getSamplerDescriptorSetHandle(VkDescriptorType type,
125 const SkTArray<uint32_t>& visibilities,
egdaniel4d866df2016-08-25 13:52:00 -0700126 GrVkDescriptorSetManager::Handle* handle);
egdaniel707bbd62016-07-26 07:19:47 -0700127
128 // Returns the compatible VkDescriptorSetLayout to use for uniform buffers. The caller does not
129 // own the VkDescriptorSetLayout and thus should not delete it. This function should be used
130 // when the caller needs the layout to create a VkPipelineLayout.
131 VkDescriptorSetLayout getUniformDSLayout() const;
132
133 // Returns the compatible VkDescriptorSetLayout to use for a specific sampler handle. The caller
134 // does not own the VkDescriptorSetLayout and thus should not delete it. This function should be
135 // used when the caller needs the layout to create a VkPipelineLayout.
136 VkDescriptorSetLayout getSamplerDSLayout(const GrVkDescriptorSetManager::Handle&) const;
egdaniela95220d2016-07-21 11:50:37 -0700137
138 // Returns a GrVkDescriptorSet that can be used for uniform buffers. The GrVkDescriptorSet
139 // is already reffed for the caller.
140 const GrVkDescriptorSet* getUniformDescriptorSet();
141
142 // Returns a GrVkDescriptorSet that can be used for sampler descriptors that are compatible with
egdaniel707bbd62016-07-26 07:19:47 -0700143 // the GrVkDescriptorSetManager::Handle passed in. The GrVkDescriptorSet is already reffed for
egdaniela95220d2016-07-21 11:50:37 -0700144 // the caller.
egdaniela95220d2016-07-21 11:50:37 -0700145 const GrVkDescriptorSet* getSamplerDescriptorSet(const GrVkDescriptorSetManager::Handle&);
egdaniel778555c2016-05-02 06:50:36 -0700146
egdaniel778555c2016-05-02 06:50:36 -0700147
egdaniela95220d2016-07-21 11:50:37 -0700148 // Signals that the descriptor set passed it, which is compatible with the passed in handle,
149 // can be reused by the next allocation request.
150 void recycleDescriptorSet(const GrVkDescriptorSet* descSet,
151 const GrVkDescriptorSetManager::Handle&);
152
jvanverth4c6e47a2016-07-22 10:34:52 -0700153 // Creates or finds free uniform buffer resources of size GrVkUniformBuffer::kStandardSize.
154 // Anything larger will need to be created and released by the client.
155 const GrVkResource* findOrCreateStandardUniformBufferResource();
156
157 // Signals that the resource passed to it (which should be a uniform buffer resource)
158 // can be reused by the next uniform buffer resource request.
159 void recycleStandardUniformBufferResource(const GrVkResource*);
160
Greg Daniela870b462019-01-08 15:49:46 -0500161 void storePipelineCacheData();
162
Greg Daniel164a9f02016-02-22 09:56:40 -0500163 // Destroy any cached resources. To be called before destroying the VkDevice.
164 // The assumption is that all queues are idle and all command buffers are finished.
165 // For resource tracing to work properly, this should be called after unrefing all other
166 // resource usages.
Jim Van Verth09557d72016-11-07 11:10:21 -0500167 // If deviceLost is true, then resources will not be checked to see if they've finished
168 // before deleting (see section 4.2.4 of the Vulkan spec).
169 void destroyResources(bool deviceLost);
Greg Daniel164a9f02016-02-22 09:56:40 -0500170
171 // Abandon any cached resources. To be used when the context/VkDevice is lost.
172 // For resource tracing to work properly, this should be called after unrefing all other
173 // resource usages.
174 void abandonResources();
175
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500176 void backgroundReset(GrVkCommandPool* pool);
177
178 void reset(GrVkCommandPool* pool);
179
Greg Daniel164a9f02016-02-22 09:56:40 -0500180private:
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500181
djsollenefe46d22016-04-29 06:41:35 -0700182#ifdef SK_DEBUG
egdanielaf132772016-03-28 12:39:29 -0700183#define GR_PIPELINE_STATE_CACHE_STATS
184#endif
185
egdaniel22281c12016-03-23 13:49:40 -0700186 class PipelineStateCache : public ::SkNoncopyable {
187 public:
188 PipelineStateCache(GrVkGpu* gpu);
189 ~PipelineStateCache();
190
191 void abandon();
192 void release();
Robert Phillipsd0fe8752019-01-31 14:13:59 -0500193 GrVkPipelineState* refPipelineState(GrRenderTarget*, GrSurfaceOrigin,
194 const GrPrimitiveProcessor&,
Greg Daniel9a51a862018-11-30 10:18:14 -0500195 const GrTextureProxy* const primProcProxies[],
Brian Salomonff168d92018-06-23 15:17:27 -0400196 const GrPipeline&,
Greg Daniel09eeefb2017-10-16 15:15:02 -0400197 GrPrimitiveType,
Greg Daniel99b88e02018-10-03 15:31:20 -0400198 VkRenderPass compatibleRenderPass);
egdaniel22281c12016-03-23 13:49:40 -0700199
200 private:
201 enum {
202 // We may actually have kMaxEntries+1 PipelineStates in context because we create a new
203 // PipelineState before evicting from the cache.
204 kMaxEntries = 128,
egdaniel22281c12016-03-23 13:49:40 -0700205 };
206
207 struct Entry;
208
Ethan Nicholas87f340e2017-01-03 14:32:01 -0500209 struct DescHash {
210 uint32_t operator()(const GrProgramDesc& desc) const {
211 return SkOpts::hash_fn(desc.asKey(), desc.keyLength(), 0);
212 }
213 };
egdaniel22281c12016-03-23 13:49:40 -0700214
Brian Salomon1471df92018-06-08 10:49:00 -0400215 SkLRUCache<const GrVkPipelineStateBuilder::Desc, std::unique_ptr<Entry>, DescHash> fMap;
egdanielaf132772016-03-28 12:39:29 -0700216
egdaniel22281c12016-03-23 13:49:40 -0700217 GrVkGpu* fGpu;
egdanielaf132772016-03-28 12:39:29 -0700218
219#ifdef GR_PIPELINE_STATE_CACHE_STATS
egdaniel22281c12016-03-23 13:49:40 -0700220 int fTotalRequests;
221 int fCacheMisses;
egdaniel22281c12016-03-23 13:49:40 -0700222#endif
223 };
224
egdanield62e28b2016-06-07 08:43:30 -0700225 class CompatibleRenderPassSet {
226 public:
227 // This will always construct the basic load store render pass (all attachments load and
228 // store their data) so that there is at least one compatible VkRenderPass that can be used
229 // with this set.
230 CompatibleRenderPassSet(const GrVkGpu* gpu, const GrVkRenderTarget& target);
231
232 bool isCompatible(const GrVkRenderTarget& target) const;
233
234 GrVkRenderPass* getCompatibleRenderPass() const {
235 // The first GrVkRenderpass should always exist since we create the basic load store
236 // render pass on create
237 SkASSERT(fRenderPasses[0]);
238 return fRenderPasses[0];
239 }
240
egdaniel2feb0932016-06-08 06:48:09 -0700241 GrVkRenderPass* getRenderPass(const GrVkGpu* gpu,
242 const GrVkRenderPass::LoadStoreOps& colorOps,
egdaniel2feb0932016-06-08 06:48:09 -0700243 const GrVkRenderPass::LoadStoreOps& stencilOps);
244
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500245 void releaseResources(GrVkGpu* gpu);
egdanield62e28b2016-06-07 08:43:30 -0700246 void abandonResources();
247
248 private:
249 SkSTArray<4, GrVkRenderPass*> fRenderPasses;
250 int fLastReturnedIndex;
251 };
252
Greg Daniela870b462019-01-08 15:49:46 -0500253 VkPipelineCache pipelineCache();
254
Greg Daniel164a9f02016-02-22 09:56:40 -0500255 GrVkGpu* fGpu;
256
jvanverth03509ea2016-03-02 13:19:47 -0800257 // Central cache for creating pipelines
258 VkPipelineCache fPipelineCache;
259
egdanielbc9b2962016-09-27 08:00:53 -0700260 // Cache of previously created copy pipelines
261 SkTArray<GrVkCopyPipeline*> fCopyPipelines;
262
egdanield62e28b2016-06-07 08:43:30 -0700263 SkSTArray<4, CompatibleRenderPassSet> fRenderPassArray;
Greg Daniel164a9f02016-02-22 09:56:40 -0500264
Greg Danielb46add82019-01-02 14:51:29 -0500265 SkTArray<const GrVkRenderPass*> fExternalRenderPasses;
266
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500267 // Array of command pools that we are waiting on
268 SkSTArray<4, GrVkCommandPool*, true> fActiveCommandPools;
jvanverth7ec92412016-07-06 09:24:57 -0700269
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500270 // Array of available command pools that are not in flight
271 SkSTArray<4, GrVkCommandPool*, true> fAvailableCommandPools;
egdaniel8b6394c2016-03-04 07:35:10 -0800272
jvanverth4c6e47a2016-07-22 10:34:52 -0700273 // Array of available uniform buffer resources
egdaniel707bbd62016-07-26 07:19:47 -0700274 SkSTArray<16, const GrVkResource*, true> fAvailableUniformBufferResources;
jvanverth4c6e47a2016-07-22 10:34:52 -0700275
egdaniel8b6394c2016-03-04 07:35:10 -0800276 // Stores GrVkSampler objects that we've already created so we can reuse them across multiple
egdaniel22281c12016-03-23 13:49:40 -0700277 // GrVkPipelineStates
Greg Daniel7e000222018-12-03 10:08:21 -0500278 SkTDynamicHash<GrVkSampler, GrVkSampler::Key> fSamplers;
279
280 // Stores GrVkSamplerYcbcrConversion objects that we've already created so we can reuse them.
281 SkTDynamicHash<GrVkSamplerYcbcrConversion, GrVkSamplerYcbcrConversion::Key> fYcbcrConversions;
egdaniel22281c12016-03-23 13:49:40 -0700282
283 // Cache of GrVkPipelineStates
284 PipelineStateCache* fPipelineStateCache;
egdaniel778555c2016-05-02 06:50:36 -0700285
Greg Daniel18f96022017-05-04 15:09:03 -0400286 SkSTArray<4, std::unique_ptr<GrVkDescriptorSetManager>> fDescriptorSetManagers;
egdaniela95220d2016-07-21 11:50:37 -0700287
egdaniel707bbd62016-07-26 07:19:47 -0700288 GrVkDescriptorSetManager::Handle fUniformDSHandle;
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500289
290 std::recursive_mutex fBackgroundMutex;
Greg Daniel164a9f02016-02-22 09:56:40 -0500291};
292
293#endif