blob: 72d0b60a09112d7a84a194aa0d08dfdba85b11e4 [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
Greg Daniel54bfb182018-11-20 17:12:36 -050011#include "GrVkVulkan.h"
12
egdanield62e28b2016-06-07 08:43:30 -070013#include "GrResourceHandle.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050014#include "GrVkDescriptorPool.h"
egdaniela95220d2016-07-21 11:50:37 -070015#include "GrVkDescriptorSetManager.h"
Brian Salomon1471df92018-06-08 10:49:00 -040016#include "GrVkPipelineStateBuilder.h"
egdaniel2feb0932016-06-08 06:48:09 -070017#include "GrVkRenderPass.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050018#include "GrVkResource.h"
Greg Daniel7e000222018-12-03 10:08:21 -050019#include "GrVkSampler.h"
20#include "GrVkSamplerYcbcrConversion.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050021#include "GrVkUtil.h"
Ethan Nicholas87f340e2017-01-03 14:32:01 -050022#include "SkLRUCache.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050023#include "SkTArray.h"
egdaniel8b6394c2016-03-04 07:35:10 -080024#include "SkTDynamicHash.h"
egdanielaf132772016-03-28 12:39:29 -070025#include "SkTInternalLList.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050026
Ethan Nicholas8e265a72018-12-12 16:22:40 -050027#include <mutex>
28#include <thread>
29
30class GrVkCommandPool;
egdanielbc9b2962016-09-27 08:00:53 -070031class GrVkCopyPipeline;
Greg Daniel164a9f02016-02-22 09:56:40 -050032class GrVkGpu;
33class GrVkPipeline;
Brian Salomon1471df92018-06-08 10:49:00 -040034class GrVkPipelineState;
jvanverth7ec92412016-07-06 09:24:57 -070035class GrVkPrimaryCommandBuffer;
Greg Daniel164a9f02016-02-22 09:56:40 -050036class GrVkRenderTarget;
jvanverth7ec92412016-07-06 09:24:57 -070037class GrVkSecondaryCommandBuffer;
egdaniel707bbd62016-07-26 07:19:47 -070038class GrVkUniformHandler;
Greg Daniel164a9f02016-02-22 09:56:40 -050039
40class GrVkResourceProvider {
41public:
42 GrVkResourceProvider(GrVkGpu* gpu);
43 ~GrVkResourceProvider();
44
jvanverth03509ea2016-03-02 13:19:47 -080045 // Set up any initial vk objects
46 void init();
47
Brian Salomonff168d92018-06-23 15:17:27 -040048 GrVkPipeline* createPipeline(const GrPrimitiveProcessor& primProc,
49 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
egdaniel2feb0932016-06-08 06:48:09 -070073 // Finds or creates a render pass that matches the target and LoadStoreOps, increments the
74 // refcount, and returns. The caller can optionally pass in a pointer to a CompatibleRPHandle.
75 // If this is non null it will be set to a handle that can be used in the furutre to quickly
76 // return a GrVkRenderPasses without the need inspecting a GrVkRenderTarget.
egdanield62e28b2016-06-07 08:43:30 -070077 const GrVkRenderPass* findRenderPass(const GrVkRenderTarget& target,
egdaniel2feb0932016-06-08 06:48:09 -070078 const GrVkRenderPass::LoadStoreOps& colorOps,
egdaniel2feb0932016-06-08 06:48:09 -070079 const GrVkRenderPass::LoadStoreOps& stencilOps,
egdanield62e28b2016-06-07 08:43:30 -070080 CompatibleRPHandle* compatibleHandle = nullptr);
81
egdaniel2feb0932016-06-08 06:48:09 -070082 // The CompatibleRPHandle must be a valid handle previously set by a call to findRenderPass or
83 // findCompatibleRenderPass.
egdanield62e28b2016-06-07 08:43:30 -070084 const GrVkRenderPass* findRenderPass(const CompatibleRPHandle& compatibleHandle,
egdaniel2feb0932016-06-08 06:48:09 -070085 const GrVkRenderPass::LoadStoreOps& colorOps,
egdaniel2feb0932016-06-08 06:48:09 -070086 const GrVkRenderPass::LoadStoreOps& stencilOps);
87
Ethan Nicholas8e265a72018-12-12 16:22:40 -050088 GrVkCommandPool* findOrCreateCommandPool();
jvanverth7ec92412016-07-06 09:24:57 -070089
Ethan Nicholas8e265a72018-12-12 16:22:40 -050090 void checkCommandBuffers();
Ethan Nicholasbff4e072018-12-12 18:17:24 +000091
egdanielc2dc1b22016-03-18 13:18:23 -070092 // Finds or creates a compatible GrVkDescriptorPool for the requested type and count.
Greg Daniel164a9f02016-02-22 09:56:40 -050093 // The refcount is incremented and a pointer returned.
94 // TODO: Currently this will just create a descriptor pool without holding onto a ref itself
95 // so we currently do not reuse them. Rquires knowing if another draw is currently using
96 // the GrVkDescriptorPool, the ability to reset pools, and the ability to purge pools out
97 // of our cache of GrVkDescriptorPools.
egdanielc2dc1b22016-03-18 13:18:23 -070098 GrVkDescriptorPool* findOrCreateCompatibleDescriptorPool(VkDescriptorType type, uint32_t count);
Greg Daniel164a9f02016-02-22 09:56:40 -050099
Greg Daniel7e000222018-12-03 10:08:21 -0500100 // Finds or creates a compatible GrVkSampler based on the GrSamplerState and
101 // GrVkYcbcrConversionInfo. The refcount is incremented and a pointer returned.
102 GrVkSampler* findOrCreateCompatibleSampler(const GrSamplerState&,
103 const GrVkYcbcrConversionInfo& ycbcrInfo);
104
105 // Finds or creates a compatible GrVkSamplerYcbcrConversion based on the GrSamplerState and
106 // GrVkYcbcrConversionInfo. The refcount is incremented and a pointer returned.
107 GrVkSamplerYcbcrConversion* findOrCreateCompatibleSamplerYcbcrConversion(
108 const GrVkYcbcrConversionInfo& ycbcrInfo);
egdaniel8b6394c2016-03-04 07:35:10 -0800109
Greg Daniel9a51a862018-11-30 10:18:14 -0500110 GrVkPipelineState* findOrCreateCompatiblePipelineState(
111 const GrPipeline&,
112 const GrPrimitiveProcessor&,
113 const GrTextureProxy* const primProcProxies[],
114 GrPrimitiveType,
115 VkRenderPass compatibleRenderPass);
egdaniel22281c12016-03-23 13:49:40 -0700116
Greg Daniela7543782017-05-02 14:01:43 -0400117 void getSamplerDescriptorSetHandle(VkDescriptorType type,
118 const GrVkUniformHandler&,
egdaniel707bbd62016-07-26 07:19:47 -0700119 GrVkDescriptorSetManager::Handle* handle);
Greg Daniela7543782017-05-02 14:01:43 -0400120 void getSamplerDescriptorSetHandle(VkDescriptorType type,
121 const SkTArray<uint32_t>& visibilities,
egdaniel4d866df2016-08-25 13:52:00 -0700122 GrVkDescriptorSetManager::Handle* handle);
egdaniel707bbd62016-07-26 07:19:47 -0700123
124 // Returns the compatible VkDescriptorSetLayout to use for uniform buffers. The caller does not
125 // own the VkDescriptorSetLayout and thus should not delete it. This function should be used
126 // when the caller needs the layout to create a VkPipelineLayout.
127 VkDescriptorSetLayout getUniformDSLayout() const;
128
129 // Returns the compatible VkDescriptorSetLayout to use for a specific sampler handle. The caller
130 // does not own the VkDescriptorSetLayout and thus should not delete it. This function should be
131 // used when the caller needs the layout to create a VkPipelineLayout.
132 VkDescriptorSetLayout getSamplerDSLayout(const GrVkDescriptorSetManager::Handle&) const;
egdaniela95220d2016-07-21 11:50:37 -0700133
134 // Returns a GrVkDescriptorSet that can be used for uniform buffers. The GrVkDescriptorSet
135 // is already reffed for the caller.
136 const GrVkDescriptorSet* getUniformDescriptorSet();
137
138 // Returns a GrVkDescriptorSet that can be used for sampler descriptors that are compatible with
egdaniel707bbd62016-07-26 07:19:47 -0700139 // the GrVkDescriptorSetManager::Handle passed in. The GrVkDescriptorSet is already reffed for
egdaniela95220d2016-07-21 11:50:37 -0700140 // the caller.
egdaniela95220d2016-07-21 11:50:37 -0700141 const GrVkDescriptorSet* getSamplerDescriptorSet(const GrVkDescriptorSetManager::Handle&);
egdaniel778555c2016-05-02 06:50:36 -0700142
egdaniel778555c2016-05-02 06:50:36 -0700143
egdaniela95220d2016-07-21 11:50:37 -0700144 // Signals that the descriptor set passed it, which is compatible with the passed in handle,
145 // can be reused by the next allocation request.
146 void recycleDescriptorSet(const GrVkDescriptorSet* descSet,
147 const GrVkDescriptorSetManager::Handle&);
148
jvanverth4c6e47a2016-07-22 10:34:52 -0700149 // Creates or finds free uniform buffer resources of size GrVkUniformBuffer::kStandardSize.
150 // Anything larger will need to be created and released by the client.
151 const GrVkResource* findOrCreateStandardUniformBufferResource();
152
153 // Signals that the resource passed to it (which should be a uniform buffer resource)
154 // can be reused by the next uniform buffer resource request.
155 void recycleStandardUniformBufferResource(const GrVkResource*);
156
Greg Daniel164a9f02016-02-22 09:56:40 -0500157 // Destroy any cached resources. To be called before destroying the VkDevice.
158 // The assumption is that all queues are idle and all command buffers are finished.
159 // For resource tracing to work properly, this should be called after unrefing all other
160 // resource usages.
Jim Van Verth09557d72016-11-07 11:10:21 -0500161 // If deviceLost is true, then resources will not be checked to see if they've finished
162 // before deleting (see section 4.2.4 of the Vulkan spec).
163 void destroyResources(bool deviceLost);
Greg Daniel164a9f02016-02-22 09:56:40 -0500164
165 // Abandon any cached resources. To be used when the context/VkDevice is lost.
166 // For resource tracing to work properly, this should be called after unrefing all other
167 // resource usages.
168 void abandonResources();
169
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500170 void backgroundReset(GrVkCommandPool* pool);
171
172 void reset(GrVkCommandPool* pool);
173
Greg Daniel164a9f02016-02-22 09:56:40 -0500174private:
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500175
djsollenefe46d22016-04-29 06:41:35 -0700176#ifdef SK_DEBUG
egdanielaf132772016-03-28 12:39:29 -0700177#define GR_PIPELINE_STATE_CACHE_STATS
178#endif
179
egdaniel22281c12016-03-23 13:49:40 -0700180 class PipelineStateCache : public ::SkNoncopyable {
181 public:
182 PipelineStateCache(GrVkGpu* gpu);
183 ~PipelineStateCache();
184
185 void abandon();
186 void release();
Brian Salomonff168d92018-06-23 15:17:27 -0400187 GrVkPipelineState* refPipelineState(const GrPrimitiveProcessor&,
Greg Daniel9a51a862018-11-30 10:18:14 -0500188 const GrTextureProxy* const primProcProxies[],
Brian Salomonff168d92018-06-23 15:17:27 -0400189 const GrPipeline&,
Greg Daniel09eeefb2017-10-16 15:15:02 -0400190 GrPrimitiveType,
Greg Daniel99b88e02018-10-03 15:31:20 -0400191 VkRenderPass compatibleRenderPass);
egdaniel22281c12016-03-23 13:49:40 -0700192
193 private:
194 enum {
195 // We may actually have kMaxEntries+1 PipelineStates in context because we create a new
196 // PipelineState before evicting from the cache.
197 kMaxEntries = 128,
egdaniel22281c12016-03-23 13:49:40 -0700198 };
199
200 struct Entry;
201
Ethan Nicholas87f340e2017-01-03 14:32:01 -0500202 struct DescHash {
203 uint32_t operator()(const GrProgramDesc& desc) const {
204 return SkOpts::hash_fn(desc.asKey(), desc.keyLength(), 0);
205 }
206 };
egdaniel22281c12016-03-23 13:49:40 -0700207
Brian Salomon1471df92018-06-08 10:49:00 -0400208 SkLRUCache<const GrVkPipelineStateBuilder::Desc, std::unique_ptr<Entry>, DescHash> fMap;
egdanielaf132772016-03-28 12:39:29 -0700209
egdaniel22281c12016-03-23 13:49:40 -0700210 GrVkGpu* fGpu;
egdanielaf132772016-03-28 12:39:29 -0700211
212#ifdef GR_PIPELINE_STATE_CACHE_STATS
egdaniel22281c12016-03-23 13:49:40 -0700213 int fTotalRequests;
214 int fCacheMisses;
egdaniel22281c12016-03-23 13:49:40 -0700215#endif
216 };
217
egdanield62e28b2016-06-07 08:43:30 -0700218 class CompatibleRenderPassSet {
219 public:
220 // This will always construct the basic load store render pass (all attachments load and
221 // store their data) so that there is at least one compatible VkRenderPass that can be used
222 // with this set.
223 CompatibleRenderPassSet(const GrVkGpu* gpu, const GrVkRenderTarget& target);
224
225 bool isCompatible(const GrVkRenderTarget& target) const;
226
227 GrVkRenderPass* getCompatibleRenderPass() const {
228 // The first GrVkRenderpass should always exist since we create the basic load store
229 // render pass on create
230 SkASSERT(fRenderPasses[0]);
231 return fRenderPasses[0];
232 }
233
egdaniel2feb0932016-06-08 06:48:09 -0700234 GrVkRenderPass* getRenderPass(const GrVkGpu* gpu,
235 const GrVkRenderPass::LoadStoreOps& colorOps,
egdaniel2feb0932016-06-08 06:48:09 -0700236 const GrVkRenderPass::LoadStoreOps& stencilOps);
237
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500238 void releaseResources(GrVkGpu* gpu);
egdanield62e28b2016-06-07 08:43:30 -0700239 void abandonResources();
240
241 private:
242 SkSTArray<4, GrVkRenderPass*> fRenderPasses;
243 int fLastReturnedIndex;
244 };
245
Greg Daniel164a9f02016-02-22 09:56:40 -0500246 GrVkGpu* fGpu;
247
jvanverth03509ea2016-03-02 13:19:47 -0800248 // Central cache for creating pipelines
249 VkPipelineCache fPipelineCache;
250
egdanielbc9b2962016-09-27 08:00:53 -0700251 // Cache of previously created copy pipelines
252 SkTArray<GrVkCopyPipeline*> fCopyPipelines;
253
egdanield62e28b2016-06-07 08:43:30 -0700254 SkSTArray<4, CompatibleRenderPassSet> fRenderPassArray;
Greg Daniel164a9f02016-02-22 09:56:40 -0500255
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500256 // Array of command pools that we are waiting on
257 SkSTArray<4, GrVkCommandPool*, true> fActiveCommandPools;
jvanverth7ec92412016-07-06 09:24:57 -0700258
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500259 // Array of available command pools that are not in flight
260 SkSTArray<4, GrVkCommandPool*, true> fAvailableCommandPools;
egdaniel8b6394c2016-03-04 07:35:10 -0800261
jvanverth4c6e47a2016-07-22 10:34:52 -0700262 // Array of available uniform buffer resources
egdaniel707bbd62016-07-26 07:19:47 -0700263 SkSTArray<16, const GrVkResource*, true> fAvailableUniformBufferResources;
jvanverth4c6e47a2016-07-22 10:34:52 -0700264
egdaniel8b6394c2016-03-04 07:35:10 -0800265 // Stores GrVkSampler objects that we've already created so we can reuse them across multiple
egdaniel22281c12016-03-23 13:49:40 -0700266 // GrVkPipelineStates
Greg Daniel7e000222018-12-03 10:08:21 -0500267 SkTDynamicHash<GrVkSampler, GrVkSampler::Key> fSamplers;
268
269 // Stores GrVkSamplerYcbcrConversion objects that we've already created so we can reuse them.
270 SkTDynamicHash<GrVkSamplerYcbcrConversion, GrVkSamplerYcbcrConversion::Key> fYcbcrConversions;
egdaniel22281c12016-03-23 13:49:40 -0700271
272 // Cache of GrVkPipelineStates
273 PipelineStateCache* fPipelineStateCache;
egdaniel778555c2016-05-02 06:50:36 -0700274
Greg Daniel18f96022017-05-04 15:09:03 -0400275 SkSTArray<4, std::unique_ptr<GrVkDescriptorSetManager>> fDescriptorSetManagers;
egdaniela95220d2016-07-21 11:50:37 -0700276
egdaniel707bbd62016-07-26 07:19:47 -0700277 GrVkDescriptorSetManager::Handle fUniformDSHandle;
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500278
279 std::recursive_mutex fBackgroundMutex;
Greg Daniel164a9f02016-02-22 09:56:40 -0500280};
281
282#endif