Debian Codegraph @

src/gpu/ganesh/vk/GrVkResourceProvider.h

raw
1 /*
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
11 #include "include/core/SkRefCnt.h"
12 #include "include/private/base/SkAssert.h"
13 #include "include/private/base/SkTArray.h"
14 #include "include/private/gpu/vk/SkiaVulkan.h"
15 #include "src/core/SkChecksum.h"
16 #include "src/core/SkLRUCache.h"
17 #include "src/core/SkTDynamicHash.h"
18 #include "src/gpu/ganesh/GrProgramDesc.h"
19 #include "src/gpu/ganesh/GrResourceHandle.h"
20 #include "src/gpu/ganesh/GrSamplerState.h"
21 #include "src/gpu/ganesh/GrThreadSafePipelineBuilder.h"
22 #include "src/gpu/ganesh/vk/GrVkDescriptorSetManager.h"
23 #include "src/gpu/ganesh/vk/GrVkPipeline.h"
24 #include "src/gpu/ganesh/vk/GrVkRenderPass.h"
25 #include "src/gpu/ganesh/vk/GrVkSampler.h"
26 #include "src/gpu/ganesh/vk/GrVkSamplerYcbcrConversion.h"
27
28 #include <cstddef>
29 #include <cstdint>
30 #include <memory>
31
32 class GrProgramInfo;
33 class GrRenderTarget;
34 class GrVkCommandPool;
35 class GrVkDescriptorPool;
36 class GrVkDescriptorSet;
37 class GrVkGpu;
38 class GrVkPipelineState;
39 class GrVkRenderTarget;
40 class GrVkUniformHandler;
41 namespace skgpu {
42 class RefCntedCallback;
43 struct VulkanYcbcrConversionInfo;
44 } // namespace skgpu
45
46 class GrVkResourceProvider {
47 public:
48 GrVkResourceProvider(GrVkGpu* gpu);
49 ~GrVkResourceProvider();
50
51 GrThreadSafePipelineBuilder* pipelineStateCache() {
52 return fPipelineStateCache.get();
53 }
54
55 sk_sp<GrThreadSafePipelineBuilder> refPipelineStateCache() {
56 return fPipelineStateCache;
57 }
58
59 // Set up any initial vk objects
60 void init();
61
62 sk_sp<const GrVkPipeline> makePipeline(const GrProgramInfo&,
63 VkPipelineShaderStageCreateInfo* shaderStageInfo,
64 int shaderStageCount,
65 VkRenderPass compatibleRenderPass,
66 VkPipelineLayout layout,
67 uint32_t subpass);
68
69 GR_DEFINE_RESOURCE_HANDLE_CLASS(CompatibleRPHandle)
70
71 using SelfDependencyFlags = GrVkRenderPass::SelfDependencyFlags;
72 using LoadFromResolve = GrVkRenderPass::LoadFromResolve;
73
74 // Finds or creates a simple render pass that matches the target, increments the refcount,
75 // and returns. The caller can optionally pass in a pointer to a CompatibleRPHandle. If this is
76 // non null it will be set to a handle that can be used in the furutre to quickly return a
77 // compatible GrVkRenderPasses without the need inspecting a GrVkRenderTarget.
78 const GrVkRenderPass* findCompatibleRenderPass(GrVkRenderTarget* target,
79 CompatibleRPHandle* compatibleHandle,
80 bool withResolve,
81 bool withStencil,
82 SelfDependencyFlags selfDepFlags,
83 LoadFromResolve);
84 const GrVkRenderPass* findCompatibleRenderPass(GrVkRenderPass::AttachmentsDescriptor*,
85 GrVkRenderPass::AttachmentFlags,
86 SelfDependencyFlags selfDepFlags,
87 LoadFromResolve,
88 CompatibleRPHandle* compatibleHandle = nullptr);
89
90 const GrVkRenderPass* findCompatibleExternalRenderPass(VkRenderPass,
91 uint32_t colorAttachmentIndex);
92
93
94 // Finds or creates a render pass that matches the target and LoadStoreOps, increments the
95 // refcount, and returns. The caller can optionally pass in a pointer to a CompatibleRPHandle.
96 // If this is non null it will be set to a handle that can be used in the future to quickly
97 // return a GrVkRenderPass without the need to inspect a GrVkRenderTarget.
98 // TODO: sk_sp?
99 const GrVkRenderPass* findRenderPass(GrVkRenderTarget* target,
100 const GrVkRenderPass::LoadStoreOps& colorOps,
101 const GrVkRenderPass::LoadStoreOps& resolveOps,
102 const GrVkRenderPass::LoadStoreOps& stencilOps,
103 CompatibleRPHandle* compatibleHandle,
104 bool withResolve,
105 bool withStencil,
106 SelfDependencyFlags selfDepFlags,
107 LoadFromResolve);
108
109 // The CompatibleRPHandle must be a valid handle previously set by a call to findRenderPass or
110 // findCompatibleRenderPass.
111 const GrVkRenderPass* findRenderPass(const CompatibleRPHandle& compatibleHandle,
112 const GrVkRenderPass::LoadStoreOps& colorOps,
113 const GrVkRenderPass::LoadStoreOps& resolveOps,
114 const GrVkRenderPass::LoadStoreOps& stencilOps);
115
116 GrVkCommandPool* findOrCreateCommandPool();
117
118 void checkCommandBuffers();
119
120 void forceSyncAllCommandBuffers();
121
122 // We must add the finishedProc to all active command buffers since we may have flushed work
123 // that the client cares about before they explicitly called flush and the GPU may reorder
124 // command execution. So we make sure all previously submitted work finishes before we call the
125 // finishedProc.
126 void addFinishedProcToActiveCommandBuffers(sk_sp<skgpu::RefCntedCallback> finishedCallback);
127
128 // Finds or creates a compatible GrVkDescriptorPool for the requested type and count.
129 // The refcount is incremented and a pointer returned.
130 // TODO: Currently this will just create a descriptor pool without holding onto a ref itself
131 // so we currently do not reuse them. Rquires knowing if another draw is currently using
132 // the GrVkDescriptorPool, the ability to reset pools, and the ability to purge pools out
133 // of our cache of GrVkDescriptorPools.
134 GrVkDescriptorPool* findOrCreateCompatibleDescriptorPool(VkDescriptorType type, uint32_t count);
135
136 // Finds or creates a compatible GrVkSampler based on the GrSamplerState and
137 // skgpu::VulkanYcbcrConversionInfo. The refcount is incremented and a pointer returned.
138 GrVkSampler* findOrCreateCompatibleSampler(GrSamplerState,
139 const skgpu::VulkanYcbcrConversionInfo& ycbcrInfo);
140
141 // Finds or creates a compatible GrVkSamplerYcbcrConversion based on the GrSamplerState and
142 // skgpu::VulkanYcbcrConversionInfo. The refcount is incremented and a pointer returned.
143 GrVkSamplerYcbcrConversion* findOrCreateCompatibleSamplerYcbcrConversion(
144 const skgpu::VulkanYcbcrConversionInfo& ycbcrInfo);
145
146 GrVkPipelineState* findOrCreateCompatiblePipelineState(
147 GrRenderTarget*,
148 const GrProgramInfo&,
149 VkRenderPass compatibleRenderPass,
150 bool overrideSubpassForResolveLoad);
151
152 GrVkPipelineState* findOrCreateCompatiblePipelineState(
153 const GrProgramDesc&,
154 const GrProgramInfo&,
155 VkRenderPass compatibleRenderPass,
156 GrThreadSafePipelineBuilder::Stats::ProgramCacheResult* stat);
157
158 sk_sp<const GrVkPipeline> findOrCreateMSAALoadPipeline(
159 const GrVkRenderPass& renderPass,
160 int numSamples,
161 VkPipelineShaderStageCreateInfo*,
162 VkPipelineLayout);
163
164 void getSamplerDescriptorSetHandle(VkDescriptorType type,
165 const GrVkUniformHandler&,
166 GrVkDescriptorSetManager::Handle* handle);
167
168 // This is a convenience function to return a descriptor set for zero sammples. When making a
169 // VkPipelineLayout we must pass in an array of valid descriptor set handles. However, we have
170 // set up our system to have the descriptor sets be in the order uniform, sampler, input. So
171 // if we have a uniform and input we will need to have a valid handle for the sampler as well.
172 // When using the GrVkMSAALoadManager this is the case, but we also don't have a
173 // GrVkUniformHandler to pass into the more general function. Thus we use this call instead.
174 void getZeroSamplerDescriptorSetHandle(GrVkDescriptorSetManager::Handle* handle);
175
176 // Returns the compatible VkDescriptorSetLayout to use for uniform buffers. The caller does not
177 // own the VkDescriptorSetLayout and thus should not delete it. This function should be used
178 // when the caller needs the layout to create a VkPipelineLayout.
179 VkDescriptorSetLayout getUniformDSLayout() const;
180
181 // Returns the compatible VkDescriptorSetLayout to use for input attachments. The caller does
182 // not own the VkDescriptorSetLayout and thus should not delete it. This function should be used
183 // when the caller needs the layout to create a VkPipelineLayout.
184 VkDescriptorSetLayout getInputDSLayout() const;
185
186 // Returns the compatible VkDescriptorSetLayout to use for a specific sampler handle. The caller
187 // does not own the VkDescriptorSetLayout and thus should not delete it. This function should be
188 // used when the caller needs the layout to create a VkPipelineLayout.
189 VkDescriptorSetLayout getSamplerDSLayout(const GrVkDescriptorSetManager::Handle&) const;
190
191 // Returns a GrVkDescriptorSet that can be used for uniform buffers. The GrVkDescriptorSet
192 // is already reffed for the caller.
193 const GrVkDescriptorSet* getUniformDescriptorSet();
194
195 // Returns a GrVkDescriptorSet that can be used for sampler descriptors that are compatible with
196 // the GrVkDescriptorSetManager::Handle passed in. The GrVkDescriptorSet is already reffed for
197 // the caller.
198 const GrVkDescriptorSet* getSamplerDescriptorSet(const GrVkDescriptorSetManager::Handle&);
199
200 // Returns a GrVkDescriptorSet that can be used for input attachments. The GrVkDescriptorSet
201 // is already reffed for the caller.
202 const GrVkDescriptorSet* getInputDescriptorSet();
203
204 // Signals that the descriptor set passed it, which is compatible with the passed in handle,
205 // can be reused by the next allocation request.
206 void recycleDescriptorSet(const GrVkDescriptorSet* descSet,
207 const GrVkDescriptorSetManager::Handle&);
208
209 void storePipelineCacheData(size_t maxSize);
210
211 // Destroy any cached resources. To be called before destroying the VkDevice.
212 // The assumption is that all queues are idle and all command buffers are finished.
213 // For resource tracing to work properly, this should be called after unrefing all other
214 // resource usages.
215 void destroyResources();
216
217 // Currently we just release available command pools (which also releases their buffers). The
218 // command buffers and pools take up the most memory. Other objects (e.g. samples,
219 // ycbcr conversions, etc.) tend to be fairly light weight and not worth the effort to remove
220 // them and then possibly remake them. Additionally many of those objects have refs/handles that
221 // are held by other objects that aren't deleted here. Thus the memory wins for removing these
222 // objects from the cache are probably not worth the complexity of safely releasing them.
223 void releaseUnlockedBackendObjects();
224
225 #if defined(GPU_TEST_UTILS)
226 void resetShaderCacheForTesting() const { fPipelineStateCache->release(); }
227 #endif
228
229 private:
230 class PipelineStateCache : public GrThreadSafePipelineBuilder {
231 public:
232 PipelineStateCache(GrVkGpu* gpu);
233 ~PipelineStateCache() override;
234
235 void release();
236 GrVkPipelineState* findOrCreatePipelineState(GrRenderTarget*,
237 const GrProgramInfo&,
238 VkRenderPass compatibleRenderPass,
239 bool overrideSubpassForResolveLoad);
240 GrVkPipelineState* findOrCreatePipelineState(const GrProgramDesc& desc,
241 const GrProgramInfo& programInfo,
242 VkRenderPass compatibleRenderPass,
243 Stats::ProgramCacheResult* stat) {
244 return this->findOrCreatePipelineStateImpl(desc, programInfo, compatibleRenderPass,
245 false, stat);
246 }
247
248 private:
249 struct Entry;
250
251 GrVkPipelineState* findOrCreatePipelineStateImpl(const GrProgramDesc&,
252 const GrProgramInfo&,
253 VkRenderPass compatibleRenderPass,
254 bool overrideSubpassForResolveLoad,
255 Stats::ProgramCacheResult*);
256
257 struct DescHash {
258 uint32_t operator()(const GrProgramDesc& desc) const {
259 return SkChecksum::Hash32(desc.asKey(), desc.keyLength());
260 }
261 };
262
263 SkLRUCache<const GrProgramDesc, std::unique_ptr<Entry>, DescHash> fMap;
264
265 GrVkGpu* fGpu;
266 };
267
268 class CompatibleRenderPassSet {
269 public:
270 // This will always construct the basic load store render pass (all attachments load and
271 // store their data) so that there is at least one compatible VkRenderPass that can be used
272 // with this set.
273 CompatibleRenderPassSet(GrVkRenderPass* renderPass);
274
275 bool isCompatible(const GrVkRenderPass::AttachmentsDescriptor&,
276 GrVkRenderPass::AttachmentFlags,
277 SelfDependencyFlags selfDepFlags,
278 LoadFromResolve) const;
279
280 const GrVkRenderPass* getCompatibleRenderPass() const {
281 // The first GrVkRenderpass should always exist since we create the basic load store
282 // render pass on create
283 SkASSERT(fRenderPasses[0]);
284 return fRenderPasses[0];
285 }
286
287 GrVkRenderPass* getRenderPass(GrVkGpu* gpu,
288 const GrVkRenderPass::LoadStoreOps& colorOps,
289 const GrVkRenderPass::LoadStoreOps& resolveOps,
290 const GrVkRenderPass::LoadStoreOps& stencilOps);
291
292 void releaseResources();
293
294 private:
295 skia_private::STArray<4, GrVkRenderPass*> fRenderPasses;
296 int fLastReturnedIndex;
297 };
298
299 VkPipelineCache pipelineCache();
300
301 GrVkGpu* fGpu;
302
303 // Central cache for creating pipelines
304 VkPipelineCache fPipelineCache;
305
306 struct MSAALoadPipeline {
307 sk_sp<const GrVkPipeline> fPipeline;
308 const GrVkRenderPass* fRenderPass;
309 };
310
311 // Cache of previously created msaa load pipelines
312 skia_private::TArray<MSAALoadPipeline> fMSAALoadPipelines;
313
314 skia_private::STArray<4, CompatibleRenderPassSet> fRenderPassArray;
315
316 skia_private::TArray<const GrVkRenderPass*> fExternalRenderPasses;
317
318 // Array of command pools that we are waiting on
319 skia_private::STArray<4, GrVkCommandPool*, true> fActiveCommandPools;
320
321 // Array of available command pools that are not in flight
322 skia_private::STArray<4, GrVkCommandPool*, true> fAvailableCommandPools;
323
324 // Stores GrVkSampler objects that we've already created so we can reuse them across multiple
325 // GrVkPipelineStates
326 SkTDynamicHash<GrVkSampler, GrVkSampler::Key> fSamplers;
327
328 // Stores GrVkSamplerYcbcrConversion objects that we've already created so we can reuse them.
329 SkTDynamicHash<GrVkSamplerYcbcrConversion, GrVkSamplerYcbcrConversion::Key> fYcbcrConversions;
330
331 // Cache of GrVkPipelineStates
332 sk_sp<PipelineStateCache> fPipelineStateCache;
333
334 skia_private::STArray<4, std::unique_ptr<GrVkDescriptorSetManager>> fDescriptorSetManagers;
335
336 GrVkDescriptorSetManager::Handle fUniformDSHandle;
337 GrVkDescriptorSetManager::Handle fInputDSHandle;
338 };
339
340 #endif