blob: ab115654ab1825207f43622637c6b2096fe8926b [file] [log] [blame]
Greg Daniel164a9f02016-02-22 09:56:40 -05001/*
2* Copyright 2015 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 GrVkCommandBuffer_DEFINED
9#define GrVkCommandBuffer_DEFINED
10
11#include "GrVkGpu.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050012#include "GrVkResource.h"
Greg Daniel6be35232017-03-01 17:01:09 -050013#include "GrVkSemaphore.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050014#include "GrVkUtil.h"
Greg Daniel487132b2018-12-20 14:09:36 -050015#include "vk/GrVkTypes.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050016
Greg Daniel6ecc9112017-06-16 16:17:03 +000017class GrVkBuffer;
egdaniel9a6cf802016-06-08 08:22:05 -070018class GrVkFramebuffer;
Greg Daniel6ecc9112017-06-16 16:17:03 +000019class GrVkIndexBuffer;
20class GrVkImage;
egdaniel470d77a2016-03-18 12:50:27 -070021class GrVkPipeline;
Greg Daniel6ecc9112017-06-16 16:17:03 +000022class GrVkPipelineState;
Greg Daniel164a9f02016-02-22 09:56:40 -050023class GrVkRenderPass;
24class GrVkRenderTarget;
25class GrVkTransferBuffer;
Greg Daniel6ecc9112017-06-16 16:17:03 +000026class GrVkVertexBuffer;
Greg Daniel164a9f02016-02-22 09:56:40 -050027
28class GrVkCommandBuffer : public GrVkResource {
29public:
Greg Daniel164a9f02016-02-22 09:56:40 -050030 void invalidateState();
31
Greg Daniel164a9f02016-02-22 09:56:40 -050032 ////////////////////////////////////////////////////////////////////////////
Greg Daniel164a9f02016-02-22 09:56:40 -050033 // CommandBuffer commands
34 ////////////////////////////////////////////////////////////////////////////
35 enum BarrierType {
36 kMemory_BarrierType,
37 kBufferMemory_BarrierType,
38 kImageMemory_BarrierType
39 };
40
41 void pipelineBarrier(const GrVkGpu* gpu,
42 VkPipelineStageFlags srcStageMask,
43 VkPipelineStageFlags dstStageMask,
44 bool byRegion,
45 BarrierType barrierType,
46 void* barrier) const;
47
Greg Daniel6ecc9112017-06-16 16:17:03 +000048 void bindInputBuffer(GrVkGpu* gpu, uint32_t binding, const GrVkVertexBuffer* vbuffer);
Chris Dalton1d616352017-05-31 12:51:23 -060049
Greg Daniel6ecc9112017-06-16 16:17:03 +000050 void bindIndexBuffer(GrVkGpu* gpu, const GrVkIndexBuffer* ibuffer);
Greg Daniel164a9f02016-02-22 09:56:40 -050051
egdaniel58a8d922016-04-21 08:03:10 -070052 void bindPipeline(const GrVkGpu* gpu, const GrVkPipeline* pipeline);
53
Greg Daniel164a9f02016-02-22 09:56:40 -050054 void bindDescriptorSets(const GrVkGpu* gpu,
egdaniel22281c12016-03-23 13:49:40 -070055 GrVkPipelineState*,
Greg Daniel7d918fd2018-06-19 15:22:01 -040056 GrVkPipelineLayout* layout,
Greg Daniel164a9f02016-02-22 09:56:40 -050057 uint32_t firstSet,
58 uint32_t setCount,
59 const VkDescriptorSet* descriptorSets,
60 uint32_t dynamicOffsetCount,
61 const uint32_t* dynamicOffsets);
62
egdanielbc9b2962016-09-27 08:00:53 -070063 void bindDescriptorSets(const GrVkGpu* gpu,
64 const SkTArray<const GrVkRecycledResource*>&,
65 const SkTArray<const GrVkResource*>&,
Greg Daniel7d918fd2018-06-19 15:22:01 -040066 GrVkPipelineLayout* layout,
egdanielbc9b2962016-09-27 08:00:53 -070067 uint32_t firstSet,
68 uint32_t setCount,
69 const VkDescriptorSet* descriptorSets,
70 uint32_t dynamicOffsetCount,
71 const uint32_t* dynamicOffsets);
72
Ethan Nicholas8e265a72018-12-12 16:22:40 -050073 GrVkCommandPool* commandPool() { return fCmdPool; }
74
egdaniel470d77a2016-03-18 12:50:27 -070075 void setViewport(const GrVkGpu* gpu,
76 uint32_t firstViewport,
77 uint32_t viewportCount,
78 const VkViewport* viewports);
79
80 void setScissor(const GrVkGpu* gpu,
81 uint32_t firstScissor,
82 uint32_t scissorCount,
83 const VkRect2D* scissors);
84
85 void setBlendConstants(const GrVkGpu* gpu, const float blendConstants[4]);
86
egdaniel9a6cf802016-06-08 08:22:05 -070087 // Commands that only work inside of a render pass
88 void clearAttachments(const GrVkGpu* gpu,
89 int numAttachments,
90 const VkClearAttachment* attachments,
91 int numRects,
92 const VkClearRect* clearRects) const;
93
94 void drawIndexed(const GrVkGpu* gpu,
95 uint32_t indexCount,
96 uint32_t instanceCount,
97 uint32_t firstIndex,
98 int32_t vertexOffset,
99 uint32_t firstInstance) const;
100
101 void draw(const GrVkGpu* gpu,
102 uint32_t vertexCount,
103 uint32_t instanceCount,
104 uint32_t firstVertex,
105 uint32_t firstInstance) const;
106
Greg Daniel7d918fd2018-06-19 15:22:01 -0400107 // Add ref-counted resource that will be tracked and released when this command buffer finishes
108 // execution
egdaniel9a6cf802016-06-08 08:22:05 -0700109 void addResource(const GrVkResource* resource) {
110 resource->ref();
Brian Salomon614c1a82018-12-19 15:42:06 -0500111 resource->notifyAddedToCommandBuffer();
egdaniel594739c2016-09-20 12:39:25 -0700112 fTrackedResources.append(1, &resource);
egdaniel9a6cf802016-06-08 08:22:05 -0700113 }
114
egdanielc1be9bc2016-07-20 08:33:00 -0700115 // Add ref-counted resource that will be tracked and released when this command buffer finishes
116 // execution. When it is released, it will signal that the resource can be recycled for reuse.
117 void addRecycledResource(const GrVkRecycledResource* resource) {
118 resource->ref();
Brian Salomon614c1a82018-12-19 15:42:06 -0500119 resource->notifyAddedToCommandBuffer();
egdaniel594739c2016-09-20 12:39:25 -0700120 fTrackedRecycledResources.append(1, &resource);
egdanielc1be9bc2016-07-20 08:33:00 -0700121 }
122
Greg Daniel7d918fd2018-06-19 15:22:01 -0400123 // Add ref-counted resource that will be tracked and released when this command buffer finishes
124 // recording.
125 void addRecordingResource(const GrVkResource* resource) {
126 resource->ref();
Brian Salomon614c1a82018-12-19 15:42:06 -0500127 resource->notifyAddedToCommandBuffer();
Greg Daniel7d918fd2018-06-19 15:22:01 -0400128 fTrackedRecordingResources.append(1, &resource);
129 }
130
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500131 void releaseResources(GrVkGpu* gpu);
jvanverth7ec92412016-07-06 09:24:57 -0700132
egdaniel9a6cf802016-06-08 08:22:05 -0700133protected:
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500134 GrVkCommandBuffer(VkCommandBuffer cmdBuffer, GrVkCommandPool* cmdPool,
Greg Daniel070cbaf2019-01-03 17:35:54 -0500135 const GrVkRenderPass* rp = nullptr)
egdaniel594739c2016-09-20 12:39:25 -0700136 : fIsActive(false)
egdaniel9a6cf802016-06-08 08:22:05 -0700137 , fActiveRenderPass(rp)
138 , fCmdBuffer(cmdBuffer)
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500139 , fCmdPool(cmdPool)
egdaniel594739c2016-09-20 12:39:25 -0700140 , fNumResets(0) {
141 fTrackedResources.setReserve(kInitialTrackedResourcesCount);
142 fTrackedRecycledResources.setReserve(kInitialTrackedResourcesCount);
Greg Daniel7d918fd2018-06-19 15:22:01 -0400143 fTrackedRecordingResources.setReserve(kInitialTrackedResourcesCount);
egdaniel9a6cf802016-06-08 08:22:05 -0700144 this->invalidateState();
145 }
egdaniel594739c2016-09-20 12:39:25 -0700146
Greg Daniel070cbaf2019-01-03 17:35:54 -0500147 bool isWrapped() const {
148 return fCmdPool == nullptr;
149 }
150
egdaniel594739c2016-09-20 12:39:25 -0700151 SkTDArray<const GrVkResource*> fTrackedResources;
152 SkTDArray<const GrVkRecycledResource*> fTrackedRecycledResources;
Greg Daniel7d918fd2018-06-19 15:22:01 -0400153 SkTDArray<const GrVkResource*> fTrackedRecordingResources;
egdaniel9a6cf802016-06-08 08:22:05 -0700154
155 // Tracks whether we are in the middle of a command buffer begin/end calls and thus can add
156 // new commands to the buffer;
157 bool fIsActive;
158
159 // Stores a pointer to the current active render pass (i.e. begin has been called but not
160 // end). A nullptr means there is no active render pass. The GrVKCommandBuffer does not own
161 // the render pass.
162 const GrVkRenderPass* fActiveRenderPass;
163
164 VkCommandBuffer fCmdBuffer;
egdaniel9a6cf802016-06-08 08:22:05 -0700165
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500166 // Raw pointer, not refcounted. The command pool controls the command buffer's lifespan, so
167 // it's guaranteed to outlive us.
168 GrVkCommandPool* fCmdPool;
169
egdaniel9a6cf802016-06-08 08:22:05 -0700170private:
171 static const int kInitialTrackedResourcesCount = 32;
172
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500173 void freeGPUData(GrVkGpu* gpu) const final override;
174 virtual void onFreeGPUData(GrVkGpu* gpu) const = 0;
175 void abandonGPUData() const final override;
176 virtual void onAbandonGPUData() const = 0;
egdaniel9a6cf802016-06-08 08:22:05 -0700177
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500178 virtual void onReleaseResources(GrVkGpu* gpu) {}
jvanverth7ec92412016-07-06 09:24:57 -0700179
Greg Daniel6ecc9112017-06-16 16:17:03 +0000180 static constexpr uint32_t kMaxInputBuffers = 2;
181
Chris Dalton1d616352017-05-31 12:51:23 -0600182 VkBuffer fBoundInputBuffers[kMaxInputBuffers];
183 VkBuffer fBoundIndexBuffer;
egdaniel9a6cf802016-06-08 08:22:05 -0700184
egdaniel594739c2016-09-20 12:39:25 -0700185 // When resetting the command buffer, we remove the tracked resources from their arrays, and
186 // we prefer to not free all the memory every time so usually we just rewind. However, to avoid
187 // all arrays growing to the max size, after so many resets we'll do a full reset of the tracked
188 // resource arrays.
189 static const int kNumRewindResetsBeforeFullReset = 8;
190 int fNumResets;
191
egdaniel9a6cf802016-06-08 08:22:05 -0700192 // Cached values used for dynamic state updates
193 VkViewport fCachedViewport;
194 VkRect2D fCachedScissor;
195 float fCachedBlendConstant[4];
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500196
197#ifdef SK_DEBUG
198 mutable bool fResourcesReleased = false;
199#endif
egdaniel9a6cf802016-06-08 08:22:05 -0700200};
201
202class GrVkSecondaryCommandBuffer;
203
204class GrVkPrimaryCommandBuffer : public GrVkCommandBuffer {
205public:
egdaniel9cb63402016-06-23 08:37:05 -0700206 ~GrVkPrimaryCommandBuffer() override;
207
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500208 static GrVkPrimaryCommandBuffer* Create(const GrVkGpu* gpu, GrVkCommandPool* cmdPool);
egdaniel9a6cf802016-06-08 08:22:05 -0700209
210 void begin(const GrVkGpu* gpu);
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500211 void end(GrVkGpu* gpu);
egdaniel9a6cf802016-06-08 08:22:05 -0700212
213 // Begins render pass on this command buffer. The framebuffer from GrVkRenderTarget will be used
214 // in the render pass.
215 void beginRenderPass(const GrVkGpu* gpu,
216 const GrVkRenderPass* renderPass,
Robert Phillips95214472017-08-08 18:00:03 -0400217 const VkClearValue clearValues[],
egdaniel9cb63402016-06-23 08:37:05 -0700218 const GrVkRenderTarget& target,
219 const SkIRect& bounds,
220 bool forSecondaryCB);
egdaniel9a6cf802016-06-08 08:22:05 -0700221 void endRenderPass(const GrVkGpu* gpu);
222
223 // Submits the SecondaryCommandBuffer into this command buffer. It is required that we are
224 // currently inside a render pass that is compatible with the one used to create the
225 // SecondaryCommandBuffer.
226 void executeCommands(const GrVkGpu* gpu,
jvanverth7ec92412016-07-06 09:24:57 -0700227 GrVkSecondaryCommandBuffer* secondaryBuffer);
egdaniel9a6cf802016-06-08 08:22:05 -0700228
Greg Daniel164a9f02016-02-22 09:56:40 -0500229 // Commands that only work outside of a render pass
230 void clearColorImage(const GrVkGpu* gpu,
231 GrVkImage* image,
232 const VkClearColorValue* color,
233 uint32_t subRangeCount,
234 const VkImageSubresourceRange* subRanges);
235
egdaniel3d5d9ac2016-03-01 12:56:15 -0800236 void clearDepthStencilImage(const GrVkGpu* gpu,
237 GrVkImage* image,
238 const VkClearDepthStencilValue* color,
239 uint32_t subRangeCount,
240 const VkImageSubresourceRange* subRanges);
241
Greg Daniel164a9f02016-02-22 09:56:40 -0500242 void copyImage(const GrVkGpu* gpu,
243 GrVkImage* srcImage,
244 VkImageLayout srcLayout,
245 GrVkImage* dstImage,
246 VkImageLayout dstLayout,
247 uint32_t copyRegionCount,
248 const VkImageCopy* copyRegions);
249
egdaniel17b89252016-04-05 07:23:38 -0700250 void blitImage(const GrVkGpu* gpu,
egdanielb2df0c22016-05-13 11:30:37 -0700251 const GrVkResource* srcResource,
252 VkImage srcImage,
egdaniel17b89252016-04-05 07:23:38 -0700253 VkImageLayout srcLayout,
egdanielb2df0c22016-05-13 11:30:37 -0700254 const GrVkResource* dstResource,
255 VkImage dstImage,
egdaniel17b89252016-04-05 07:23:38 -0700256 VkImageLayout dstLayout,
257 uint32_t blitRegionCount,
258 const VkImageBlit* blitRegions,
259 VkFilter filter);
260
egdanielb2df0c22016-05-13 11:30:37 -0700261 void blitImage(const GrVkGpu* gpu,
262 const GrVkImage& srcImage,
263 const GrVkImage& dstImage,
264 uint32_t blitRegionCount,
265 const VkImageBlit* blitRegions,
Greg Daniel6ecc9112017-06-16 16:17:03 +0000266 VkFilter filter);
egdanielb2df0c22016-05-13 11:30:37 -0700267
Greg Daniel164a9f02016-02-22 09:56:40 -0500268 void copyImageToBuffer(const GrVkGpu* gpu,
269 GrVkImage* srcImage,
270 VkImageLayout srcLayout,
271 GrVkTransferBuffer* dstBuffer,
272 uint32_t copyRegionCount,
273 const VkBufferImageCopy* copyRegions);
274
275 void copyBufferToImage(const GrVkGpu* gpu,
276 GrVkTransferBuffer* srcBuffer,
277 GrVkImage* dstImage,
278 VkImageLayout dstLayout,
279 uint32_t copyRegionCount,
280 const VkBufferImageCopy* copyRegions);
281
Greg Daniel6888c0d2017-08-25 11:55:50 -0400282 void copyBuffer(GrVkGpu* gpu,
283 GrVkBuffer* srcBuffer,
284 GrVkBuffer* dstBuffer,
285 uint32_t regionCount,
286 const VkBufferCopy* regions);
287
jvanvertha584de92016-06-30 09:10:52 -0700288 void updateBuffer(GrVkGpu* gpu,
289 GrVkBuffer* dstBuffer,
290 VkDeviceSize dstOffset,
291 VkDeviceSize dataSize,
292 const void* data);
293
egdaniel52ad2512016-08-04 12:50:01 -0700294 void resolveImage(GrVkGpu* gpu,
295 const GrVkImage& srcImage,
296 const GrVkImage& dstImage,
297 uint32_t regionCount,
298 const VkImageResolve* regions);
299
Greg Daniel6be35232017-03-01 17:01:09 -0500300 void submitToQueue(const GrVkGpu* gpu, VkQueue queue, GrVkGpu::SyncQueue sync,
Greg Daniel48661b82018-01-22 16:11:35 -0500301 SkTArray<GrVkSemaphore::Resource*>& signalSemaphores,
302 SkTArray<GrVkSemaphore::Resource*>& waitSemaphores);
egdaniel9a6cf802016-06-08 08:22:05 -0700303 bool finished(const GrVkGpu* gpu) const;
Greg Daniel164a9f02016-02-22 09:56:40 -0500304
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500305 void recycleSecondaryCommandBuffers();
306
jvanverth7ec92412016-07-06 09:24:57 -0700307#ifdef SK_TRACE_VK_RESOURCES
308 void dumpInfo() const override {
309 SkDebugf("GrVkPrimaryCommandBuffer: %d (%d refs)\n", fCmdBuffer, this->getRefCnt());
310 }
311#endif
312
Greg Daniel164a9f02016-02-22 09:56:40 -0500313private:
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500314 explicit GrVkPrimaryCommandBuffer(VkCommandBuffer cmdBuffer, GrVkCommandPool* cmdPool)
315 : INHERITED(cmdBuffer, cmdPool)
egdaniel9cb63402016-06-23 08:37:05 -0700316 , fSubmitFence(VK_NULL_HANDLE) {}
317
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500318 void onFreeGPUData(GrVkGpu* gpu) const override;
egdaniel9cb63402016-06-23 08:37:05 -0700319
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500320 void onAbandonGPUData() const override;
321
322 void onReleaseResources(GrVkGpu* gpu) override;
jvanverth7ec92412016-07-06 09:24:57 -0700323
324 SkTArray<GrVkSecondaryCommandBuffer*, true> fSecondaryCommandBuffers;
325 VkFence fSubmitFence;
Greg Daniel164a9f02016-02-22 09:56:40 -0500326
egdaniel9a6cf802016-06-08 08:22:05 -0700327 typedef GrVkCommandBuffer INHERITED;
Greg Daniel164a9f02016-02-22 09:56:40 -0500328};
329
egdaniel9a6cf802016-06-08 08:22:05 -0700330class GrVkSecondaryCommandBuffer : public GrVkCommandBuffer {
331public:
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500332 static GrVkSecondaryCommandBuffer* Create(const GrVkGpu* gpu, GrVkCommandPool* cmdPool);
Greg Daniel070cbaf2019-01-03 17:35:54 -0500333 // Used for wrapping an external secondary command buffer.
334 static GrVkSecondaryCommandBuffer* Create(VkCommandBuffer externalSecondaryCB);
egdaniel9a6cf802016-06-08 08:22:05 -0700335
jvanverth7ec92412016-07-06 09:24:57 -0700336 void begin(const GrVkGpu* gpu, const GrVkFramebuffer* framebuffer,
337 const GrVkRenderPass* compatibleRenderPass);
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500338 void end(GrVkGpu* gpu);
egdaniel9a6cf802016-06-08 08:22:05 -0700339
Greg Daniel64cc9aa2018-10-19 13:54:56 -0400340 VkCommandBuffer vkCommandBuffer() { return fCmdBuffer; }
341
jvanverth7ec92412016-07-06 09:24:57 -0700342#ifdef SK_TRACE_VK_RESOURCES
343 void dumpInfo() const override {
344 SkDebugf("GrVkSecondaryCommandBuffer: %d (%d refs)\n", fCmdBuffer, this->getRefCnt());
345 }
346#endif
347
egdaniel9a6cf802016-06-08 08:22:05 -0700348private:
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500349 explicit GrVkSecondaryCommandBuffer(VkCommandBuffer cmdBuffer, GrVkCommandPool* cmdPool)
350 : INHERITED(cmdBuffer, cmdPool) {}
egdaniel9a6cf802016-06-08 08:22:05 -0700351
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500352 void onFreeGPUData(GrVkGpu* gpu) const override {}
353
354 void onAbandonGPUData() const override {}
egdaniel9cb63402016-06-23 08:37:05 -0700355
egdaniel9a6cf802016-06-08 08:22:05 -0700356 friend class GrVkPrimaryCommandBuffer;
357
358 typedef GrVkCommandBuffer INHERITED;
359};
Greg Daniel164a9f02016-02-22 09:56:40 -0500360
361#endif