blob: 9cafcb4054d86f42e9f0edffa8ec552a4906d274 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/gpu/vk/GrVkTypes.h"
12#include "include/private/SkTArray.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "src/core/SkLRUCache.h"
14#include "src/core/SkTDynamicHash.h"
Ben Wagner729a23f2019-05-17 16:29:34 -040015#include "src/core/SkTInternalLList.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "src/gpu/GrResourceHandle.h"
17#include "src/gpu/vk/GrVkDescriptorPool.h"
18#include "src/gpu/vk/GrVkDescriptorSetManager.h"
19#include "src/gpu/vk/GrVkPipelineStateBuilder.h"
20#include "src/gpu/vk/GrVkRenderPass.h"
21#include "src/gpu/vk/GrVkResource.h"
22#include "src/gpu/vk/GrVkSampler.h"
23#include "src/gpu/vk/GrVkSamplerYcbcrConversion.h"
24#include "src/gpu/vk/GrVkUtil.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;
Greg Daniel164a9f02016-02-22 09:56:40 -050030class GrVkGpu;
31class GrVkPipeline;
Brian Salomon1471df92018-06-08 10:49:00 -040032class GrVkPipelineState;
jvanverth7ec92412016-07-06 09:24:57 -070033class GrVkPrimaryCommandBuffer;
Greg Daniel164a9f02016-02-22 09:56:40 -050034class GrVkRenderTarget;
jvanverth7ec92412016-07-06 09:24:57 -070035class GrVkSecondaryCommandBuffer;
egdaniel707bbd62016-07-26 07:19:47 -070036class GrVkUniformHandler;
Greg Daniel164a9f02016-02-22 09:56:40 -050037
38class GrVkResourceProvider {
39public:
40 GrVkResourceProvider(GrVkGpu* gpu);
41 ~GrVkResourceProvider();
42
jvanverth03509ea2016-03-02 13:19:47 -080043 // Set up any initial vk objects
44 void init();
45
Robert Phillips901aff02019-10-08 12:32:56 -040046 GrVkPipeline* createPipeline(const GrProgramInfo&,
Robert Phillips6c2aa7a2019-10-17 19:06:39 +000047 const GrStencilSettings& stencil,
Greg Daniel164a9f02016-02-22 09:56:40 -050048 VkPipelineShaderStageCreateInfo* shaderStageInfo,
49 int shaderStageCount,
50 GrPrimitiveType primitiveType,
Greg Daniel99b88e02018-10-03 15:31:20 -040051 VkRenderPass compatibleRenderPass,
Greg Daniel164a9f02016-02-22 09:56:40 -050052 VkPipelineLayout layout);
53
egdanield62e28b2016-06-07 08:43:30 -070054 GR_DEFINE_RESOURCE_HANDLE_CLASS(CompatibleRPHandle);
55
Greg Daniel164a9f02016-02-22 09:56:40 -050056 // Finds or creates a simple render pass that matches the target, increments the refcount,
egdanield62e28b2016-06-07 08:43:30 -070057 // and returns. The caller can optionally pass in a pointer to a CompatibleRPHandle. If this is
58 // non null it will be set to a handle that can be used in the furutre to quickly return a
59 // compatible GrVkRenderPasses without the need inspecting a GrVkRenderTarget.
60 const GrVkRenderPass* findCompatibleRenderPass(const GrVkRenderTarget& target,
61 CompatibleRPHandle* compatibleHandle = nullptr);
62 // The CompatibleRPHandle must be a valid handle previously set by a call to
63 // findCompatibleRenderPass(GrVkRenderTarget&, CompatibleRPHandle*).
64 const GrVkRenderPass* findCompatibleRenderPass(const CompatibleRPHandle& compatibleHandle);
65
Greg Danielb46add82019-01-02 14:51:29 -050066 const GrVkRenderPass* findCompatibleExternalRenderPass(VkRenderPass,
67 uint32_t colorAttachmentIndex);
68
egdaniel2feb0932016-06-08 06:48:09 -070069 // Finds or creates a render pass that matches the target and LoadStoreOps, increments the
70 // refcount, and returns. The caller can optionally pass in a pointer to a CompatibleRPHandle.
71 // If this is non null it will be set to a handle that can be used in the furutre to quickly
72 // return a GrVkRenderPasses without the need inspecting a GrVkRenderTarget.
Greg Danielfa3adf72019-11-07 09:53:41 -050073 const GrVkRenderPass* findRenderPass(GrVkRenderTarget* target,
egdaniel2feb0932016-06-08 06:48:09 -070074 const GrVkRenderPass::LoadStoreOps& colorOps,
egdaniel2feb0932016-06-08 06:48:09 -070075 const GrVkRenderPass::LoadStoreOps& stencilOps,
egdanield62e28b2016-06-07 08:43:30 -070076 CompatibleRPHandle* compatibleHandle = nullptr);
77
egdaniel2feb0932016-06-08 06:48:09 -070078 // The CompatibleRPHandle must be a valid handle previously set by a call to findRenderPass or
79 // findCompatibleRenderPass.
egdanield62e28b2016-06-07 08:43:30 -070080 const GrVkRenderPass* findRenderPass(const CompatibleRPHandle& compatibleHandle,
egdaniel2feb0932016-06-08 06:48:09 -070081 const GrVkRenderPass::LoadStoreOps& colorOps,
egdaniel2feb0932016-06-08 06:48:09 -070082 const GrVkRenderPass::LoadStoreOps& stencilOps);
83
Ethan Nicholas8e265a72018-12-12 16:22:40 -050084 GrVkCommandPool* findOrCreateCommandPool();
jvanverth7ec92412016-07-06 09:24:57 -070085
Ethan Nicholas8e265a72018-12-12 16:22:40 -050086 void checkCommandBuffers();
Ethan Nicholasbff4e072018-12-12 18:17:24 +000087
Greg Daniela3aa75a2019-04-12 14:24:55 -040088 // We must add the finishedProc to all active command buffers since we may have flushed work
89 // that the client cares about before they explicitly called flush and the GPU may reorder
90 // command execution. So we make sure all previously submitted work finishes before we call the
91 // finishedProc.
92 void addFinishedProcToActiveCommandBuffers(GrGpuFinishedProc finishedProc,
93 GrGpuFinishedContext finishedContext);
94
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 Phillips901aff02019-10-08 12:32:56 -0400114 GrRenderTarget*,
115 const GrProgramInfo&,
Greg Daniel9a51a862018-11-30 10:18:14 -0500116 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 Daniela870b462019-01-08 15:49:46 -0500159 void storePipelineCacheData();
160
Greg Daniel164a9f02016-02-22 09:56:40 -0500161 // Destroy any cached resources. To be called before destroying the VkDevice.
162 // The assumption is that all queues are idle and all command buffers are finished.
163 // For resource tracing to work properly, this should be called after unrefing all other
164 // resource usages.
Jim Van Verth09557d72016-11-07 11:10:21 -0500165 // If deviceLost is true, then resources will not be checked to see if they've finished
166 // before deleting (see section 4.2.4 of the Vulkan spec).
167 void destroyResources(bool deviceLost);
Greg Daniel164a9f02016-02-22 09:56:40 -0500168
169 // Abandon any cached resources. To be used when the context/VkDevice is lost.
170 // For resource tracing to work properly, this should be called after unrefing all other
171 // resource usages.
172 void abandonResources();
173
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500174 void backgroundReset(GrVkCommandPool* pool);
175
176 void reset(GrVkCommandPool* pool);
177
Brian Osmanfd7657c2019-04-25 11:34:07 -0400178#if GR_TEST_UTILS
179 void resetShaderCacheForTesting() const { fPipelineStateCache->release(); }
180#endif
181
Greg Daniel164a9f02016-02-22 09:56:40 -0500182private:
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500183
djsollenefe46d22016-04-29 06:41:35 -0700184#ifdef SK_DEBUG
egdanielaf132772016-03-28 12:39:29 -0700185#define GR_PIPELINE_STATE_CACHE_STATS
186#endif
187
egdaniel22281c12016-03-23 13:49:40 -0700188 class PipelineStateCache : public ::SkNoncopyable {
189 public:
190 PipelineStateCache(GrVkGpu* gpu);
191 ~PipelineStateCache();
192
193 void abandon();
194 void release();
Robert Phillips901aff02019-10-08 12:32:56 -0400195 GrVkPipelineState* refPipelineState(GrRenderTarget*,
196 const GrProgramInfo&,
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:
egdaniel22281c12016-03-23 13:49:40 -0700201 struct Entry;
202
Ethan Nicholas87f340e2017-01-03 14:32:01 -0500203 struct DescHash {
204 uint32_t operator()(const GrProgramDesc& desc) const {
205 return SkOpts::hash_fn(desc.asKey(), desc.keyLength(), 0);
206 }
207 };
egdaniel22281c12016-03-23 13:49:40 -0700208
Brian Salomon1471df92018-06-08 10:49:00 -0400209 SkLRUCache<const GrVkPipelineStateBuilder::Desc, std::unique_ptr<Entry>, DescHash> fMap;
egdanielaf132772016-03-28 12:39:29 -0700210
egdaniel22281c12016-03-23 13:49:40 -0700211 GrVkGpu* fGpu;
egdanielaf132772016-03-28 12:39:29 -0700212
213#ifdef GR_PIPELINE_STATE_CACHE_STATS
egdaniel22281c12016-03-23 13:49:40 -0700214 int fTotalRequests;
215 int fCacheMisses;
egdaniel22281c12016-03-23 13:49:40 -0700216#endif
217 };
218
egdanield62e28b2016-06-07 08:43:30 -0700219 class CompatibleRenderPassSet {
220 public:
221 // This will always construct the basic load store render pass (all attachments load and
222 // store their data) so that there is at least one compatible VkRenderPass that can be used
223 // with this set.
Greg Daniele643da62019-11-05 12:36:42 -0500224 CompatibleRenderPassSet(GrVkGpu* gpu, const GrVkRenderTarget& target);
egdanield62e28b2016-06-07 08:43:30 -0700225
226 bool isCompatible(const GrVkRenderTarget& target) const;
227
228 GrVkRenderPass* getCompatibleRenderPass() const {
229 // The first GrVkRenderpass should always exist since we create the basic load store
230 // render pass on create
231 SkASSERT(fRenderPasses[0]);
232 return fRenderPasses[0];
233 }
234
Greg Daniele643da62019-11-05 12:36:42 -0500235 GrVkRenderPass* getRenderPass(GrVkGpu* gpu,
egdaniel2feb0932016-06-08 06:48:09 -0700236 const GrVkRenderPass::LoadStoreOps& colorOps,
egdaniel2feb0932016-06-08 06:48:09 -0700237 const GrVkRenderPass::LoadStoreOps& stencilOps);
238
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500239 void releaseResources(GrVkGpu* gpu);
egdanield62e28b2016-06-07 08:43:30 -0700240 void abandonResources();
241
242 private:
243 SkSTArray<4, GrVkRenderPass*> fRenderPasses;
244 int fLastReturnedIndex;
245 };
246
Greg Daniela870b462019-01-08 15:49:46 -0500247 VkPipelineCache pipelineCache();
248
Greg Daniel164a9f02016-02-22 09:56:40 -0500249 GrVkGpu* fGpu;
250
jvanverth03509ea2016-03-02 13:19:47 -0800251 // Central cache for creating pipelines
252 VkPipelineCache fPipelineCache;
253
egdanield62e28b2016-06-07 08:43:30 -0700254 SkSTArray<4, CompatibleRenderPassSet> fRenderPassArray;
Greg Daniel164a9f02016-02-22 09:56:40 -0500255
Greg Danielb46add82019-01-02 14:51:29 -0500256 SkTArray<const GrVkRenderPass*> fExternalRenderPasses;
257
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500258 // Array of command pools that we are waiting on
259 SkSTArray<4, GrVkCommandPool*, true> fActiveCommandPools;
jvanverth7ec92412016-07-06 09:24:57 -0700260
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500261 // Array of available command pools that are not in flight
262 SkSTArray<4, GrVkCommandPool*, true> fAvailableCommandPools;
egdaniel8b6394c2016-03-04 07:35:10 -0800263
jvanverth4c6e47a2016-07-22 10:34:52 -0700264 // Array of available uniform buffer resources
egdaniel707bbd62016-07-26 07:19:47 -0700265 SkSTArray<16, const GrVkResource*, true> fAvailableUniformBufferResources;
jvanverth4c6e47a2016-07-22 10:34:52 -0700266
egdaniel8b6394c2016-03-04 07:35:10 -0800267 // Stores GrVkSampler objects that we've already created so we can reuse them across multiple
egdaniel22281c12016-03-23 13:49:40 -0700268 // GrVkPipelineStates
Greg Daniel7e000222018-12-03 10:08:21 -0500269 SkTDynamicHash<GrVkSampler, GrVkSampler::Key> fSamplers;
270
271 // Stores GrVkSamplerYcbcrConversion objects that we've already created so we can reuse them.
272 SkTDynamicHash<GrVkSamplerYcbcrConversion, GrVkSamplerYcbcrConversion::Key> fYcbcrConversions;
egdaniel22281c12016-03-23 13:49:40 -0700273
274 // Cache of GrVkPipelineStates
275 PipelineStateCache* fPipelineStateCache;
egdaniel778555c2016-05-02 06:50:36 -0700276
Greg Daniel18f96022017-05-04 15:09:03 -0400277 SkSTArray<4, std::unique_ptr<GrVkDescriptorSetManager>> fDescriptorSetManagers;
egdaniela95220d2016-07-21 11:50:37 -0700278
egdaniel707bbd62016-07-26 07:19:47 -0700279 GrVkDescriptorSetManager::Handle fUniformDSHandle;
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500280
281 std::recursive_mutex fBackgroundMutex;
Greg Daniel164a9f02016-02-22 09:56:40 -0500282};
283
284#endif