blob: 2eac2b2f2557ce086be52d910b3bab2978da106f [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
Brian Salomonff168d92018-06-23 15:17:27 -040047 GrVkPipeline* createPipeline(const GrPrimitiveProcessor& primProc,
48 const GrPipeline& pipeline,
csmartdaltonc633abb2016-11-01 08:55:55 -070049 const GrStencilSettings& stencil,
Greg Daniel164a9f02016-02-22 09:56:40 -050050 VkPipelineShaderStageCreateInfo* shaderStageInfo,
51 int shaderStageCount,
52 GrPrimitiveType primitiveType,
Greg Daniel99b88e02018-10-03 15:31:20 -040053 VkRenderPass compatibleRenderPass,
Greg Daniel164a9f02016-02-22 09:56:40 -050054 VkPipelineLayout layout);
55
egdanielbc9b2962016-09-27 08:00:53 -070056 GrVkCopyPipeline* findOrCreateCopyPipeline(const GrVkRenderTarget* dst,
57 VkPipelineShaderStageCreateInfo*,
58 VkPipelineLayout);
59
egdanield62e28b2016-06-07 08:43:30 -070060 GR_DEFINE_RESOURCE_HANDLE_CLASS(CompatibleRPHandle);
61
Greg Daniel164a9f02016-02-22 09:56:40 -050062 // Finds or creates a simple render pass that matches the target, increments the refcount,
egdanield62e28b2016-06-07 08:43:30 -070063 // and returns. The caller can optionally pass in a pointer to a CompatibleRPHandle. If this is
64 // non null it will be set to a handle that can be used in the furutre to quickly return a
65 // compatible GrVkRenderPasses without the need inspecting a GrVkRenderTarget.
66 const GrVkRenderPass* findCompatibleRenderPass(const GrVkRenderTarget& target,
67 CompatibleRPHandle* compatibleHandle = nullptr);
68 // The CompatibleRPHandle must be a valid handle previously set by a call to
69 // findCompatibleRenderPass(GrVkRenderTarget&, CompatibleRPHandle*).
70 const GrVkRenderPass* findCompatibleRenderPass(const CompatibleRPHandle& compatibleHandle);
71
Greg Danielb46add82019-01-02 14:51:29 -050072 const GrVkRenderPass* findCompatibleExternalRenderPass(VkRenderPass,
73 uint32_t colorAttachmentIndex);
74
egdaniel2feb0932016-06-08 06:48:09 -070075 // Finds or creates a render pass that matches the target and LoadStoreOps, increments the
76 // refcount, and returns. The caller can optionally pass in a pointer to a CompatibleRPHandle.
77 // If this is non null it will be set to a handle that can be used in the furutre to quickly
78 // return a GrVkRenderPasses without the need inspecting a GrVkRenderTarget.
egdanield62e28b2016-06-07 08:43:30 -070079 const GrVkRenderPass* findRenderPass(const GrVkRenderTarget& target,
egdaniel2feb0932016-06-08 06:48:09 -070080 const GrVkRenderPass::LoadStoreOps& colorOps,
egdaniel2feb0932016-06-08 06:48:09 -070081 const GrVkRenderPass::LoadStoreOps& stencilOps,
egdanield62e28b2016-06-07 08:43:30 -070082 CompatibleRPHandle* compatibleHandle = nullptr);
83
egdaniel2feb0932016-06-08 06:48:09 -070084 // The CompatibleRPHandle must be a valid handle previously set by a call to findRenderPass or
85 // findCompatibleRenderPass.
egdanield62e28b2016-06-07 08:43:30 -070086 const GrVkRenderPass* findRenderPass(const CompatibleRPHandle& compatibleHandle,
egdaniel2feb0932016-06-08 06:48:09 -070087 const GrVkRenderPass::LoadStoreOps& colorOps,
egdaniel2feb0932016-06-08 06:48:09 -070088 const GrVkRenderPass::LoadStoreOps& stencilOps);
89
Ethan Nicholas8e265a72018-12-12 16:22:40 -050090 GrVkCommandPool* findOrCreateCommandPool();
jvanverth7ec92412016-07-06 09:24:57 -070091
Ethan Nicholas8e265a72018-12-12 16:22:40 -050092 void checkCommandBuffers();
Ethan Nicholasbff4e072018-12-12 18:17:24 +000093
egdanielc2dc1b22016-03-18 13:18:23 -070094 // Finds or creates a compatible GrVkDescriptorPool for the requested type and count.
Greg Daniel164a9f02016-02-22 09:56:40 -050095 // The refcount is incremented and a pointer returned.
96 // TODO: Currently this will just create a descriptor pool without holding onto a ref itself
97 // so we currently do not reuse them. Rquires knowing if another draw is currently using
98 // the GrVkDescriptorPool, the ability to reset pools, and the ability to purge pools out
99 // of our cache of GrVkDescriptorPools.
egdanielc2dc1b22016-03-18 13:18:23 -0700100 GrVkDescriptorPool* findOrCreateCompatibleDescriptorPool(VkDescriptorType type, uint32_t count);
Greg Daniel164a9f02016-02-22 09:56:40 -0500101
Greg Daniel7e000222018-12-03 10:08:21 -0500102 // Finds or creates a compatible GrVkSampler based on the GrSamplerState and
103 // GrVkYcbcrConversionInfo. The refcount is incremented and a pointer returned.
104 GrVkSampler* findOrCreateCompatibleSampler(const GrSamplerState&,
105 const GrVkYcbcrConversionInfo& ycbcrInfo);
106
107 // Finds or creates a compatible GrVkSamplerYcbcrConversion based on the GrSamplerState and
108 // GrVkYcbcrConversionInfo. The refcount is incremented and a pointer returned.
109 GrVkSamplerYcbcrConversion* findOrCreateCompatibleSamplerYcbcrConversion(
110 const GrVkYcbcrConversionInfo& ycbcrInfo);
egdaniel8b6394c2016-03-04 07:35:10 -0800111
Greg Daniel9a51a862018-11-30 10:18:14 -0500112 GrVkPipelineState* findOrCreateCompatiblePipelineState(
113 const GrPipeline&,
114 const GrPrimitiveProcessor&,
115 const GrTextureProxy* const primProcProxies[],
116 GrPrimitiveType,
117 VkRenderPass compatibleRenderPass);
egdaniel22281c12016-03-23 13:49:40 -0700118
Greg Daniela7543782017-05-02 14:01:43 -0400119 void getSamplerDescriptorSetHandle(VkDescriptorType type,
120 const GrVkUniformHandler&,
egdaniel707bbd62016-07-26 07:19:47 -0700121 GrVkDescriptorSetManager::Handle* handle);
Greg Daniela7543782017-05-02 14:01:43 -0400122 void getSamplerDescriptorSetHandle(VkDescriptorType type,
123 const SkTArray<uint32_t>& visibilities,
egdaniel4d866df2016-08-25 13:52:00 -0700124 GrVkDescriptorSetManager::Handle* handle);
egdaniel707bbd62016-07-26 07:19:47 -0700125
126 // Returns the compatible VkDescriptorSetLayout to use for uniform buffers. The caller does not
127 // own the VkDescriptorSetLayout and thus should not delete it. This function should be used
128 // when the caller needs the layout to create a VkPipelineLayout.
129 VkDescriptorSetLayout getUniformDSLayout() const;
130
131 // Returns the compatible VkDescriptorSetLayout to use for a specific sampler handle. The caller
132 // does not own the VkDescriptorSetLayout and thus should not delete it. This function should be
133 // used when the caller needs the layout to create a VkPipelineLayout.
134 VkDescriptorSetLayout getSamplerDSLayout(const GrVkDescriptorSetManager::Handle&) const;
egdaniela95220d2016-07-21 11:50:37 -0700135
136 // Returns a GrVkDescriptorSet that can be used for uniform buffers. The GrVkDescriptorSet
137 // is already reffed for the caller.
138 const GrVkDescriptorSet* getUniformDescriptorSet();
139
140 // Returns a GrVkDescriptorSet that can be used for sampler descriptors that are compatible with
egdaniel707bbd62016-07-26 07:19:47 -0700141 // the GrVkDescriptorSetManager::Handle passed in. The GrVkDescriptorSet is already reffed for
egdaniela95220d2016-07-21 11:50:37 -0700142 // the caller.
egdaniela95220d2016-07-21 11:50:37 -0700143 const GrVkDescriptorSet* getSamplerDescriptorSet(const GrVkDescriptorSetManager::Handle&);
egdaniel778555c2016-05-02 06:50:36 -0700144
egdaniel778555c2016-05-02 06:50:36 -0700145
egdaniela95220d2016-07-21 11:50:37 -0700146 // Signals that the descriptor set passed it, which is compatible with the passed in handle,
147 // can be reused by the next allocation request.
148 void recycleDescriptorSet(const GrVkDescriptorSet* descSet,
149 const GrVkDescriptorSetManager::Handle&);
150
jvanverth4c6e47a2016-07-22 10:34:52 -0700151 // Creates or finds free uniform buffer resources of size GrVkUniformBuffer::kStandardSize.
152 // Anything larger will need to be created and released by the client.
153 const GrVkResource* findOrCreateStandardUniformBufferResource();
154
155 // Signals that the resource passed to it (which should be a uniform buffer resource)
156 // can be reused by the next uniform buffer resource request.
157 void recycleStandardUniformBufferResource(const GrVkResource*);
158
Greg Daniel164a9f02016-02-22 09:56:40 -0500159 // Destroy any cached resources. To be called before destroying the VkDevice.
160 // The assumption is that all queues are idle and all command buffers are finished.
161 // For resource tracing to work properly, this should be called after unrefing all other
162 // resource usages.
Jim Van Verth09557d72016-11-07 11:10:21 -0500163 // If deviceLost is true, then resources will not be checked to see if they've finished
164 // before deleting (see section 4.2.4 of the Vulkan spec).
165 void destroyResources(bool deviceLost);
Greg Daniel164a9f02016-02-22 09:56:40 -0500166
167 // Abandon any cached resources. To be used when the context/VkDevice is lost.
168 // For resource tracing to work properly, this should be called after unrefing all other
169 // resource usages.
170 void abandonResources();
171
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500172 void backgroundReset(GrVkCommandPool* pool);
173
174 void reset(GrVkCommandPool* pool);
175
Greg Daniel164a9f02016-02-22 09:56:40 -0500176private:
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500177
djsollenefe46d22016-04-29 06:41:35 -0700178#ifdef SK_DEBUG
egdanielaf132772016-03-28 12:39:29 -0700179#define GR_PIPELINE_STATE_CACHE_STATS
180#endif
181
egdaniel22281c12016-03-23 13:49:40 -0700182 class PipelineStateCache : public ::SkNoncopyable {
183 public:
184 PipelineStateCache(GrVkGpu* gpu);
185 ~PipelineStateCache();
186
187 void abandon();
188 void release();
Brian Salomonff168d92018-06-23 15:17:27 -0400189 GrVkPipelineState* refPipelineState(const GrPrimitiveProcessor&,
Greg Daniel9a51a862018-11-30 10:18:14 -0500190 const GrTextureProxy* const primProcProxies[],
Brian Salomonff168d92018-06-23 15:17:27 -0400191 const GrPipeline&,
Greg Daniel09eeefb2017-10-16 15:15:02 -0400192 GrPrimitiveType,
Greg Daniel99b88e02018-10-03 15:31:20 -0400193 VkRenderPass compatibleRenderPass);
egdaniel22281c12016-03-23 13:49:40 -0700194
195 private:
196 enum {
197 // We may actually have kMaxEntries+1 PipelineStates in context because we create a new
198 // PipelineState before evicting from the cache.
199 kMaxEntries = 128,
egdaniel22281c12016-03-23 13:49:40 -0700200 };
201
202 struct Entry;
203
Ethan Nicholas87f340e2017-01-03 14:32:01 -0500204 struct DescHash {
205 uint32_t operator()(const GrProgramDesc& desc) const {
206 return SkOpts::hash_fn(desc.asKey(), desc.keyLength(), 0);
207 }
208 };
egdaniel22281c12016-03-23 13:49:40 -0700209
Brian Salomon1471df92018-06-08 10:49:00 -0400210 SkLRUCache<const GrVkPipelineStateBuilder::Desc, std::unique_ptr<Entry>, DescHash> fMap;
egdanielaf132772016-03-28 12:39:29 -0700211
egdaniel22281c12016-03-23 13:49:40 -0700212 GrVkGpu* fGpu;
egdanielaf132772016-03-28 12:39:29 -0700213
214#ifdef GR_PIPELINE_STATE_CACHE_STATS
egdaniel22281c12016-03-23 13:49:40 -0700215 int fTotalRequests;
216 int fCacheMisses;
egdaniel22281c12016-03-23 13:49:40 -0700217#endif
218 };
219
egdanield62e28b2016-06-07 08:43:30 -0700220 class CompatibleRenderPassSet {
221 public:
222 // This will always construct the basic load store render pass (all attachments load and
223 // store their data) so that there is at least one compatible VkRenderPass that can be used
224 // with this set.
225 CompatibleRenderPassSet(const GrVkGpu* gpu, const GrVkRenderTarget& target);
226
227 bool isCompatible(const GrVkRenderTarget& target) const;
228
229 GrVkRenderPass* getCompatibleRenderPass() const {
230 // The first GrVkRenderpass should always exist since we create the basic load store
231 // render pass on create
232 SkASSERT(fRenderPasses[0]);
233 return fRenderPasses[0];
234 }
235
egdaniel2feb0932016-06-08 06:48:09 -0700236 GrVkRenderPass* getRenderPass(const GrVkGpu* gpu,
237 const GrVkRenderPass::LoadStoreOps& colorOps,
egdaniel2feb0932016-06-08 06:48:09 -0700238 const GrVkRenderPass::LoadStoreOps& stencilOps);
239
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500240 void releaseResources(GrVkGpu* gpu);
egdanield62e28b2016-06-07 08:43:30 -0700241 void abandonResources();
242
243 private:
244 SkSTArray<4, GrVkRenderPass*> fRenderPasses;
245 int fLastReturnedIndex;
246 };
247
Greg Daniel164a9f02016-02-22 09:56:40 -0500248 GrVkGpu* fGpu;
249
jvanverth03509ea2016-03-02 13:19:47 -0800250 // Central cache for creating pipelines
251 VkPipelineCache fPipelineCache;
252
egdanielbc9b2962016-09-27 08:00:53 -0700253 // Cache of previously created copy pipelines
254 SkTArray<GrVkCopyPipeline*> fCopyPipelines;
255
egdanield62e28b2016-06-07 08:43:30 -0700256 SkSTArray<4, CompatibleRenderPassSet> fRenderPassArray;
Greg Daniel164a9f02016-02-22 09:56:40 -0500257
Greg Danielb46add82019-01-02 14:51:29 -0500258 SkTArray<const GrVkRenderPass*> fExternalRenderPasses;
259
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500260 // Array of command pools that we are waiting on
261 SkSTArray<4, GrVkCommandPool*, true> fActiveCommandPools;
jvanverth7ec92412016-07-06 09:24:57 -0700262
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500263 // Array of available command pools that are not in flight
264 SkSTArray<4, GrVkCommandPool*, true> fAvailableCommandPools;
egdaniel8b6394c2016-03-04 07:35:10 -0800265
jvanverth4c6e47a2016-07-22 10:34:52 -0700266 // Array of available uniform buffer resources
egdaniel707bbd62016-07-26 07:19:47 -0700267 SkSTArray<16, const GrVkResource*, true> fAvailableUniformBufferResources;
jvanverth4c6e47a2016-07-22 10:34:52 -0700268
egdaniel8b6394c2016-03-04 07:35:10 -0800269 // Stores GrVkSampler objects that we've already created so we can reuse them across multiple
egdaniel22281c12016-03-23 13:49:40 -0700270 // GrVkPipelineStates
Greg Daniel7e000222018-12-03 10:08:21 -0500271 SkTDynamicHash<GrVkSampler, GrVkSampler::Key> fSamplers;
272
273 // Stores GrVkSamplerYcbcrConversion objects that we've already created so we can reuse them.
274 SkTDynamicHash<GrVkSamplerYcbcrConversion, GrVkSamplerYcbcrConversion::Key> fYcbcrConversions;
egdaniel22281c12016-03-23 13:49:40 -0700275
276 // Cache of GrVkPipelineStates
277 PipelineStateCache* fPipelineStateCache;
egdaniel778555c2016-05-02 06:50:36 -0700278
Greg Daniel18f96022017-05-04 15:09:03 -0400279 SkSTArray<4, std::unique_ptr<GrVkDescriptorSetManager>> fDescriptorSetManagers;
egdaniela95220d2016-07-21 11:50:37 -0700280
egdaniel707bbd62016-07-26 07:19:47 -0700281 GrVkDescriptorSetManager::Handle fUniformDSHandle;
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500282
283 std::recursive_mutex fBackgroundMutex;
Greg Daniel164a9f02016-02-22 09:56:40 -0500284};
285
286#endif