blob: d130f0c06dfc831597e224ad6c99639399199471 [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"
egdaniel22281c12016-03-23 13:49:40 -070014#include "GrVkPipelineState.h"
egdaniel2feb0932016-06-08 06:48:09 -070015#include "GrVkRenderPass.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050016#include "GrVkResource.h"
17#include "GrVkUtil.h"
Ethan Nicholas87f340e2017-01-03 14:32:01 -050018#include "SkLRUCache.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050019#include "SkTArray.h"
egdaniel8b6394c2016-03-04 07:35:10 -080020#include "SkTDynamicHash.h"
egdanielaf132772016-03-28 12:39:29 -070021#include "SkTInternalLList.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050022
jvanverthe50f3e72016-03-28 07:03:06 -070023#include "vk/GrVkDefines.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050024
25class GrPipeline;
26class GrPrimitiveProcessor;
Brian Salomon2bbdcc42017-09-07 12:36:34 -040027class GrSamplerState;
egdanielbc9b2962016-09-27 08:00:53 -070028class GrVkCopyPipeline;
Greg Daniel164a9f02016-02-22 09:56:40 -050029class GrVkGpu;
30class GrVkPipeline;
jvanverth7ec92412016-07-06 09:24:57 -070031class GrVkPrimaryCommandBuffer;
Greg Daniel164a9f02016-02-22 09:56:40 -050032class GrVkRenderTarget;
egdaniel8b6394c2016-03-04 07:35:10 -080033class GrVkSampler;
jvanverth7ec92412016-07-06 09:24:57 -070034class GrVkSecondaryCommandBuffer;
egdaniel707bbd62016-07-26 07:19:47 -070035class GrVkUniformHandler;
Greg Daniel164a9f02016-02-22 09:56:40 -050036
37class GrVkResourceProvider {
38public:
39 GrVkResourceProvider(GrVkGpu* gpu);
40 ~GrVkResourceProvider();
41
jvanverth03509ea2016-03-02 13:19:47 -080042 // Set up any initial vk objects
43 void init();
44
Greg Daniel164a9f02016-02-22 09:56:40 -050045 GrVkPipeline* createPipeline(const GrPipeline& pipeline,
csmartdaltonc633abb2016-11-01 08:55:55 -070046 const GrStencilSettings& stencil,
Greg Daniel164a9f02016-02-22 09:56:40 -050047 const GrPrimitiveProcessor& primProc,
48 VkPipelineShaderStageCreateInfo* shaderStageInfo,
49 int shaderStageCount,
50 GrPrimitiveType primitiveType,
51 const GrVkRenderPass& renderPass,
52 VkPipelineLayout layout);
53
egdanielbc9b2962016-09-27 08:00:53 -070054 GrVkCopyPipeline* findOrCreateCopyPipeline(const GrVkRenderTarget* dst,
55 VkPipelineShaderStageCreateInfo*,
56 VkPipelineLayout);
57
egdanield62e28b2016-06-07 08:43:30 -070058 GR_DEFINE_RESOURCE_HANDLE_CLASS(CompatibleRPHandle);
59
Greg Daniel164a9f02016-02-22 09:56:40 -050060 // Finds or creates a simple render pass that matches the target, increments the refcount,
egdanield62e28b2016-06-07 08:43:30 -070061 // and returns. The caller can optionally pass in a pointer to a CompatibleRPHandle. If this is
62 // non null it will be set to a handle that can be used in the furutre to quickly return a
63 // compatible GrVkRenderPasses without the need inspecting a GrVkRenderTarget.
64 const GrVkRenderPass* findCompatibleRenderPass(const GrVkRenderTarget& target,
65 CompatibleRPHandle* compatibleHandle = nullptr);
66 // The CompatibleRPHandle must be a valid handle previously set by a call to
67 // findCompatibleRenderPass(GrVkRenderTarget&, CompatibleRPHandle*).
68 const GrVkRenderPass* findCompatibleRenderPass(const CompatibleRPHandle& compatibleHandle);
69
egdaniel2feb0932016-06-08 06:48:09 -070070 // Finds or creates a render pass that matches the target and LoadStoreOps, increments the
71 // refcount, and returns. The caller can optionally pass in a pointer to a CompatibleRPHandle.
72 // If this is non null it will be set to a handle that can be used in the furutre to quickly
73 // return a GrVkRenderPasses without the need inspecting a GrVkRenderTarget.
egdanield62e28b2016-06-07 08:43:30 -070074 const GrVkRenderPass* findRenderPass(const GrVkRenderTarget& target,
egdaniel2feb0932016-06-08 06:48:09 -070075 const GrVkRenderPass::LoadStoreOps& colorOps,
egdaniel2feb0932016-06-08 06:48:09 -070076 const GrVkRenderPass::LoadStoreOps& stencilOps,
egdanield62e28b2016-06-07 08:43:30 -070077 CompatibleRPHandle* compatibleHandle = nullptr);
78
egdaniel2feb0932016-06-08 06:48:09 -070079 // The CompatibleRPHandle must be a valid handle previously set by a call to findRenderPass or
80 // findCompatibleRenderPass.
egdanield62e28b2016-06-07 08:43:30 -070081 const GrVkRenderPass* findRenderPass(const CompatibleRPHandle& compatibleHandle,
egdaniel2feb0932016-06-08 06:48:09 -070082 const GrVkRenderPass::LoadStoreOps& colorOps,
egdaniel2feb0932016-06-08 06:48:09 -070083 const GrVkRenderPass::LoadStoreOps& stencilOps);
84
jvanverth7ec92412016-07-06 09:24:57 -070085 GrVkPrimaryCommandBuffer* findOrCreatePrimaryCommandBuffer();
Greg Daniel164a9f02016-02-22 09:56:40 -050086 void checkCommandBuffers();
87
jvanverth7ec92412016-07-06 09:24:57 -070088 GrVkSecondaryCommandBuffer* findOrCreateSecondaryCommandBuffer();
89 void recycleSecondaryCommandBuffer(GrVkSecondaryCommandBuffer* cb);
90
egdanielc2dc1b22016-03-18 13:18:23 -070091 // Finds or creates a compatible GrVkDescriptorPool for the requested type and count.
Greg Daniel164a9f02016-02-22 09:56:40 -050092 // The refcount is incremented and a pointer returned.
93 // TODO: Currently this will just create a descriptor pool without holding onto a ref itself
94 // so we currently do not reuse them. Rquires knowing if another draw is currently using
95 // the GrVkDescriptorPool, the ability to reset pools, and the ability to purge pools out
96 // of our cache of GrVkDescriptorPools.
egdanielc2dc1b22016-03-18 13:18:23 -070097 GrVkDescriptorPool* findOrCreateCompatibleDescriptorPool(VkDescriptorType type, uint32_t count);
Greg Daniel164a9f02016-02-22 09:56:40 -050098
Brian Salomon2bbdcc42017-09-07 12:36:34 -040099 // Finds or creates a compatible GrVkSampler based on the GrSamplerState.
egdaniel8b6394c2016-03-04 07:35:10 -0800100 // The refcount is incremented and a pointer returned.
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400101 GrVkSampler* findOrCreateCompatibleSampler(const GrSamplerState&, uint32_t maxMipLevel);
egdaniel8b6394c2016-03-04 07:35:10 -0800102
Greg Daniel09eeefb2017-10-16 15:15:02 -0400103 GrVkPipelineState* findOrCreateCompatiblePipelineState(const GrPipeline&,
104 const GrPrimitiveProcessor&,
105 GrPrimitiveType,
106 const GrVkRenderPass& renderPass);
egdaniel22281c12016-03-23 13:49:40 -0700107
Greg Daniela7543782017-05-02 14:01:43 -0400108 void getSamplerDescriptorSetHandle(VkDescriptorType type,
109 const GrVkUniformHandler&,
egdaniel707bbd62016-07-26 07:19:47 -0700110 GrVkDescriptorSetManager::Handle* handle);
Greg Daniela7543782017-05-02 14:01:43 -0400111 void getSamplerDescriptorSetHandle(VkDescriptorType type,
112 const SkTArray<uint32_t>& visibilities,
egdaniel4d866df2016-08-25 13:52:00 -0700113 GrVkDescriptorSetManager::Handle* handle);
egdaniel707bbd62016-07-26 07:19:47 -0700114
115 // Returns the compatible VkDescriptorSetLayout to use for uniform buffers. The caller does not
116 // own the VkDescriptorSetLayout and thus should not delete it. This function should be used
117 // when the caller needs the layout to create a VkPipelineLayout.
118 VkDescriptorSetLayout getUniformDSLayout() const;
119
120 // Returns the compatible VkDescriptorSetLayout to use for a specific sampler handle. The caller
121 // does not own the VkDescriptorSetLayout and thus should not delete it. This function should be
122 // used when the caller needs the layout to create a VkPipelineLayout.
123 VkDescriptorSetLayout getSamplerDSLayout(const GrVkDescriptorSetManager::Handle&) const;
egdaniela95220d2016-07-21 11:50:37 -0700124
125 // Returns a GrVkDescriptorSet that can be used for uniform buffers. The GrVkDescriptorSet
126 // is already reffed for the caller.
127 const GrVkDescriptorSet* getUniformDescriptorSet();
128
129 // Returns a GrVkDescriptorSet that can be used for sampler descriptors that are compatible with
egdaniel707bbd62016-07-26 07:19:47 -0700130 // the GrVkDescriptorSetManager::Handle passed in. The GrVkDescriptorSet is already reffed for
egdaniela95220d2016-07-21 11:50:37 -0700131 // the caller.
egdaniela95220d2016-07-21 11:50:37 -0700132 const GrVkDescriptorSet* getSamplerDescriptorSet(const GrVkDescriptorSetManager::Handle&);
egdaniel778555c2016-05-02 06:50:36 -0700133
egdaniel778555c2016-05-02 06:50:36 -0700134
egdaniela95220d2016-07-21 11:50:37 -0700135 // Signals that the descriptor set passed it, which is compatible with the passed in handle,
136 // can be reused by the next allocation request.
137 void recycleDescriptorSet(const GrVkDescriptorSet* descSet,
138 const GrVkDescriptorSetManager::Handle&);
139
jvanverth4c6e47a2016-07-22 10:34:52 -0700140 // Creates or finds free uniform buffer resources of size GrVkUniformBuffer::kStandardSize.
141 // Anything larger will need to be created and released by the client.
142 const GrVkResource* findOrCreateStandardUniformBufferResource();
143
144 // Signals that the resource passed to it (which should be a uniform buffer resource)
145 // can be reused by the next uniform buffer resource request.
146 void recycleStandardUniformBufferResource(const GrVkResource*);
147
Greg Daniel164a9f02016-02-22 09:56:40 -0500148 // Destroy any cached resources. To be called before destroying the VkDevice.
149 // The assumption is that all queues are idle and all command buffers are finished.
150 // For resource tracing to work properly, this should be called after unrefing all other
151 // resource usages.
Jim Van Verth09557d72016-11-07 11:10:21 -0500152 // If deviceLost is true, then resources will not be checked to see if they've finished
153 // before deleting (see section 4.2.4 of the Vulkan spec).
154 void destroyResources(bool deviceLost);
Greg Daniel164a9f02016-02-22 09:56:40 -0500155
156 // Abandon any cached resources. To be used when the context/VkDevice is lost.
157 // For resource tracing to work properly, this should be called after unrefing all other
158 // resource usages.
159 void abandonResources();
160
161private:
djsollenefe46d22016-04-29 06:41:35 -0700162#ifdef SK_DEBUG
egdanielaf132772016-03-28 12:39:29 -0700163#define GR_PIPELINE_STATE_CACHE_STATS
164#endif
165
egdaniel22281c12016-03-23 13:49:40 -0700166 class PipelineStateCache : public ::SkNoncopyable {
167 public:
168 PipelineStateCache(GrVkGpu* gpu);
169 ~PipelineStateCache();
170
171 void abandon();
172 void release();
Greg Daniel09eeefb2017-10-16 15:15:02 -0400173 GrVkPipelineState* refPipelineState(const GrPipeline&,
174 const GrPrimitiveProcessor&,
175 GrPrimitiveType,
176 const GrVkRenderPass& renderPass);
egdaniel22281c12016-03-23 13:49:40 -0700177
178 private:
179 enum {
180 // We may actually have kMaxEntries+1 PipelineStates in context because we create a new
181 // PipelineState before evicting from the cache.
182 kMaxEntries = 128,
egdaniel22281c12016-03-23 13:49:40 -0700183 };
184
185 struct Entry;
186
Ethan Nicholas87f340e2017-01-03 14:32:01 -0500187 struct DescHash {
188 uint32_t operator()(const GrProgramDesc& desc) const {
189 return SkOpts::hash_fn(desc.asKey(), desc.keyLength(), 0);
190 }
191 };
egdaniel22281c12016-03-23 13:49:40 -0700192
Ethan Nicholas87f340e2017-01-03 14:32:01 -0500193 SkLRUCache<const GrVkPipelineState::Desc, std::unique_ptr<Entry>, DescHash> fMap;
egdanielaf132772016-03-28 12:39:29 -0700194
egdaniel22281c12016-03-23 13:49:40 -0700195 GrVkGpu* fGpu;
egdanielaf132772016-03-28 12:39:29 -0700196
197#ifdef GR_PIPELINE_STATE_CACHE_STATS
egdaniel22281c12016-03-23 13:49:40 -0700198 int fTotalRequests;
199 int fCacheMisses;
egdaniel22281c12016-03-23 13:49:40 -0700200#endif
201 };
202
egdanield62e28b2016-06-07 08:43:30 -0700203 class CompatibleRenderPassSet {
204 public:
205 // This will always construct the basic load store render pass (all attachments load and
206 // store their data) so that there is at least one compatible VkRenderPass that can be used
207 // with this set.
208 CompatibleRenderPassSet(const GrVkGpu* gpu, const GrVkRenderTarget& target);
209
210 bool isCompatible(const GrVkRenderTarget& target) const;
211
212 GrVkRenderPass* getCompatibleRenderPass() const {
213 // The first GrVkRenderpass should always exist since we create the basic load store
214 // render pass on create
215 SkASSERT(fRenderPasses[0]);
216 return fRenderPasses[0];
217 }
218
egdaniel2feb0932016-06-08 06:48:09 -0700219 GrVkRenderPass* getRenderPass(const GrVkGpu* gpu,
220 const GrVkRenderPass::LoadStoreOps& colorOps,
egdaniel2feb0932016-06-08 06:48:09 -0700221 const GrVkRenderPass::LoadStoreOps& stencilOps);
222
egdanield62e28b2016-06-07 08:43:30 -0700223 void releaseResources(const GrVkGpu* gpu);
224 void abandonResources();
225
226 private:
227 SkSTArray<4, GrVkRenderPass*> fRenderPasses;
228 int fLastReturnedIndex;
229 };
230
Greg Daniel164a9f02016-02-22 09:56:40 -0500231 GrVkGpu* fGpu;
232
jvanverth03509ea2016-03-02 13:19:47 -0800233 // Central cache for creating pipelines
234 VkPipelineCache fPipelineCache;
235
egdanielbc9b2962016-09-27 08:00:53 -0700236 // Cache of previously created copy pipelines
237 SkTArray<GrVkCopyPipeline*> fCopyPipelines;
238
egdanield62e28b2016-06-07 08:43:30 -0700239 SkSTArray<4, CompatibleRenderPassSet> fRenderPassArray;
Greg Daniel164a9f02016-02-22 09:56:40 -0500240
jvanverth7ec92412016-07-06 09:24:57 -0700241 // Array of PrimaryCommandBuffers that are currently in flight
egdaniel707bbd62016-07-26 07:19:47 -0700242 SkSTArray<4, GrVkPrimaryCommandBuffer*, true> fActiveCommandBuffers;
jvanverth7ec92412016-07-06 09:24:57 -0700243 // Array of available primary command buffers that are not in flight
egdaniel707bbd62016-07-26 07:19:47 -0700244 SkSTArray<4, GrVkPrimaryCommandBuffer*, true> fAvailableCommandBuffers;
jvanverth7ec92412016-07-06 09:24:57 -0700245
246 // Array of available secondary command buffers
egdaniel707bbd62016-07-26 07:19:47 -0700247 SkSTArray<16, GrVkSecondaryCommandBuffer*, true> fAvailableSecondaryCommandBuffers;
egdaniel8b6394c2016-03-04 07:35:10 -0800248
jvanverth4c6e47a2016-07-22 10:34:52 -0700249 // Array of available uniform buffer resources
egdaniel707bbd62016-07-26 07:19:47 -0700250 SkSTArray<16, const GrVkResource*, true> fAvailableUniformBufferResources;
jvanverth4c6e47a2016-07-22 10:34:52 -0700251
egdaniel8b6394c2016-03-04 07:35:10 -0800252 // Stores GrVkSampler objects that we've already created so we can reuse them across multiple
egdaniel22281c12016-03-23 13:49:40 -0700253 // GrVkPipelineStates
jvanverth62340062016-04-26 08:01:44 -0700254 SkTDynamicHash<GrVkSampler, uint16_t> fSamplers;
egdaniel22281c12016-03-23 13:49:40 -0700255
256 // Cache of GrVkPipelineStates
257 PipelineStateCache* fPipelineStateCache;
egdaniel778555c2016-05-02 06:50:36 -0700258
Greg Daniel18f96022017-05-04 15:09:03 -0400259 SkSTArray<4, std::unique_ptr<GrVkDescriptorSetManager>> fDescriptorSetManagers;
egdaniela95220d2016-07-21 11:50:37 -0700260
egdaniel707bbd62016-07-26 07:19:47 -0700261 GrVkDescriptorSetManager::Handle fUniformDSHandle;
Greg Daniel164a9f02016-02-22 09:56:40 -0500262};
263
264#endif