blob: 86c3c7eaa36717954d48e6374be526d55187133d [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,
Greg Daniel59dc1482019-02-22 10:46:38 -050042 const GrVkResource* resource,
Greg Daniel164a9f02016-02-22 09:56:40 -050043 VkPipelineStageFlags srcStageMask,
44 VkPipelineStageFlags dstStageMask,
45 bool byRegion,
46 BarrierType barrierType,
Greg Daniel59dc1482019-02-22 10:46:38 -050047 void* barrier);
Greg Daniel164a9f02016-02-22 09:56:40 -050048
Greg Daniel6ecc9112017-06-16 16:17:03 +000049 void bindInputBuffer(GrVkGpu* gpu, uint32_t binding, const GrVkVertexBuffer* vbuffer);
Chris Dalton1d616352017-05-31 12:51:23 -060050
Greg Daniel6ecc9112017-06-16 16:17:03 +000051 void bindIndexBuffer(GrVkGpu* gpu, const GrVkIndexBuffer* ibuffer);
Greg Daniel164a9f02016-02-22 09:56:40 -050052
egdaniel58a8d922016-04-21 08:03:10 -070053 void bindPipeline(const GrVkGpu* gpu, const GrVkPipeline* pipeline);
54
Greg Daniel164a9f02016-02-22 09:56:40 -050055 void bindDescriptorSets(const GrVkGpu* gpu,
egdaniel22281c12016-03-23 13:49:40 -070056 GrVkPipelineState*,
Greg Daniel7d918fd2018-06-19 15:22:01 -040057 GrVkPipelineLayout* layout,
Greg Daniel164a9f02016-02-22 09:56:40 -050058 uint32_t firstSet,
59 uint32_t setCount,
60 const VkDescriptorSet* descriptorSets,
61 uint32_t dynamicOffsetCount,
62 const uint32_t* dynamicOffsets);
63
egdanielbc9b2962016-09-27 08:00:53 -070064 void bindDescriptorSets(const GrVkGpu* gpu,
65 const SkTArray<const GrVkRecycledResource*>&,
66 const SkTArray<const GrVkResource*>&,
Greg Daniel7d918fd2018-06-19 15:22:01 -040067 GrVkPipelineLayout* layout,
egdanielbc9b2962016-09-27 08:00:53 -070068 uint32_t firstSet,
69 uint32_t setCount,
70 const VkDescriptorSet* descriptorSets,
71 uint32_t dynamicOffsetCount,
72 const uint32_t* dynamicOffsets);
73
Ethan Nicholas8e265a72018-12-12 16:22:40 -050074 GrVkCommandPool* commandPool() { return fCmdPool; }
75
egdaniel470d77a2016-03-18 12:50:27 -070076 void setViewport(const GrVkGpu* gpu,
77 uint32_t firstViewport,
78 uint32_t viewportCount,
79 const VkViewport* viewports);
80
81 void setScissor(const GrVkGpu* gpu,
82 uint32_t firstScissor,
83 uint32_t scissorCount,
84 const VkRect2D* scissors);
85
86 void setBlendConstants(const GrVkGpu* gpu, const float blendConstants[4]);
87
egdaniel9a6cf802016-06-08 08:22:05 -070088 // Commands that only work inside of a render pass
89 void clearAttachments(const GrVkGpu* gpu,
90 int numAttachments,
91 const VkClearAttachment* attachments,
92 int numRects,
93 const VkClearRect* clearRects) const;
94
95 void drawIndexed(const GrVkGpu* gpu,
96 uint32_t indexCount,
97 uint32_t instanceCount,
98 uint32_t firstIndex,
99 int32_t vertexOffset,
100 uint32_t firstInstance) const;
101
102 void draw(const GrVkGpu* gpu,
103 uint32_t vertexCount,
104 uint32_t instanceCount,
105 uint32_t firstVertex,
106 uint32_t firstInstance) const;
107
Greg Daniel7d918fd2018-06-19 15:22:01 -0400108 // Add ref-counted resource that will be tracked and released when this command buffer finishes
109 // execution
egdaniel9a6cf802016-06-08 08:22:05 -0700110 void addResource(const GrVkResource* resource) {
111 resource->ref();
Brian Salomon614c1a82018-12-19 15:42:06 -0500112 resource->notifyAddedToCommandBuffer();
egdaniel594739c2016-09-20 12:39:25 -0700113 fTrackedResources.append(1, &resource);
egdaniel9a6cf802016-06-08 08:22:05 -0700114 }
115
egdanielc1be9bc2016-07-20 08:33:00 -0700116 // Add ref-counted resource that will be tracked and released when this command buffer finishes
117 // execution. When it is released, it will signal that the resource can be recycled for reuse.
118 void addRecycledResource(const GrVkRecycledResource* resource) {
119 resource->ref();
Brian Salomon614c1a82018-12-19 15:42:06 -0500120 resource->notifyAddedToCommandBuffer();
egdaniel594739c2016-09-20 12:39:25 -0700121 fTrackedRecycledResources.append(1, &resource);
egdanielc1be9bc2016-07-20 08:33:00 -0700122 }
123
Greg Daniel7d918fd2018-06-19 15:22:01 -0400124 // Add ref-counted resource that will be tracked and released when this command buffer finishes
125 // recording.
126 void addRecordingResource(const GrVkResource* resource) {
127 resource->ref();
Brian Salomon614c1a82018-12-19 15:42:06 -0500128 resource->notifyAddedToCommandBuffer();
Greg Daniel7d918fd2018-06-19 15:22:01 -0400129 fTrackedRecordingResources.append(1, &resource);
130 }
131
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500132 void releaseResources(GrVkGpu* gpu);
jvanverth7ec92412016-07-06 09:24:57 -0700133
egdaniel9a6cf802016-06-08 08:22:05 -0700134protected:
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500135 GrVkCommandBuffer(VkCommandBuffer cmdBuffer, GrVkCommandPool* cmdPool,
Greg Daniel070cbaf2019-01-03 17:35:54 -0500136 const GrVkRenderPass* rp = nullptr)
egdaniel594739c2016-09-20 12:39:25 -0700137 : fIsActive(false)
egdaniel9a6cf802016-06-08 08:22:05 -0700138 , fActiveRenderPass(rp)
139 , fCmdBuffer(cmdBuffer)
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500140 , fCmdPool(cmdPool)
egdaniel594739c2016-09-20 12:39:25 -0700141 , fNumResets(0) {
142 fTrackedResources.setReserve(kInitialTrackedResourcesCount);
143 fTrackedRecycledResources.setReserve(kInitialTrackedResourcesCount);
Greg Daniel7d918fd2018-06-19 15:22:01 -0400144 fTrackedRecordingResources.setReserve(kInitialTrackedResourcesCount);
egdaniel9a6cf802016-06-08 08:22:05 -0700145 this->invalidateState();
146 }
egdaniel594739c2016-09-20 12:39:25 -0700147
Greg Daniel070cbaf2019-01-03 17:35:54 -0500148 bool isWrapped() const {
149 return fCmdPool == nullptr;
150 }
151
egdaniel594739c2016-09-20 12:39:25 -0700152 SkTDArray<const GrVkResource*> fTrackedResources;
153 SkTDArray<const GrVkRecycledResource*> fTrackedRecycledResources;
Greg Daniel7d918fd2018-06-19 15:22:01 -0400154 SkTDArray<const GrVkResource*> fTrackedRecordingResources;
egdaniel9a6cf802016-06-08 08:22:05 -0700155
156 // Tracks whether we are in the middle of a command buffer begin/end calls and thus can add
157 // new commands to the buffer;
158 bool fIsActive;
159
160 // Stores a pointer to the current active render pass (i.e. begin has been called but not
161 // end). A nullptr means there is no active render pass. The GrVKCommandBuffer does not own
162 // the render pass.
163 const GrVkRenderPass* fActiveRenderPass;
164
165 VkCommandBuffer fCmdBuffer;
egdaniel9a6cf802016-06-08 08:22:05 -0700166
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500167 // Raw pointer, not refcounted. The command pool controls the command buffer's lifespan, so
168 // it's guaranteed to outlive us.
169 GrVkCommandPool* fCmdPool;
170
egdaniel9a6cf802016-06-08 08:22:05 -0700171private:
172 static const int kInitialTrackedResourcesCount = 32;
173
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500174 void freeGPUData(GrVkGpu* gpu) const final override;
175 virtual void onFreeGPUData(GrVkGpu* gpu) const = 0;
176 void abandonGPUData() const final override;
177 virtual void onAbandonGPUData() const = 0;
egdaniel9a6cf802016-06-08 08:22:05 -0700178
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500179 virtual void onReleaseResources(GrVkGpu* gpu) {}
jvanverth7ec92412016-07-06 09:24:57 -0700180
Greg Daniel6ecc9112017-06-16 16:17:03 +0000181 static constexpr uint32_t kMaxInputBuffers = 2;
182
Chris Dalton1d616352017-05-31 12:51:23 -0600183 VkBuffer fBoundInputBuffers[kMaxInputBuffers];
184 VkBuffer fBoundIndexBuffer;
egdaniel9a6cf802016-06-08 08:22:05 -0700185
egdaniel594739c2016-09-20 12:39:25 -0700186 // When resetting the command buffer, we remove the tracked resources from their arrays, and
187 // we prefer to not free all the memory every time so usually we just rewind. However, to avoid
188 // all arrays growing to the max size, after so many resets we'll do a full reset of the tracked
189 // resource arrays.
190 static const int kNumRewindResetsBeforeFullReset = 8;
191 int fNumResets;
192
egdaniel9a6cf802016-06-08 08:22:05 -0700193 // Cached values used for dynamic state updates
194 VkViewport fCachedViewport;
195 VkRect2D fCachedScissor;
196 float fCachedBlendConstant[4];
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500197
198#ifdef SK_DEBUG
199 mutable bool fResourcesReleased = false;
200#endif
egdaniel9a6cf802016-06-08 08:22:05 -0700201};
202
203class GrVkSecondaryCommandBuffer;
204
205class GrVkPrimaryCommandBuffer : public GrVkCommandBuffer {
206public:
egdaniel9cb63402016-06-23 08:37:05 -0700207 ~GrVkPrimaryCommandBuffer() override;
208
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500209 static GrVkPrimaryCommandBuffer* Create(const GrVkGpu* gpu, GrVkCommandPool* cmdPool);
egdaniel9a6cf802016-06-08 08:22:05 -0700210
211 void begin(const GrVkGpu* gpu);
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500212 void end(GrVkGpu* gpu);
egdaniel9a6cf802016-06-08 08:22:05 -0700213
214 // Begins render pass on this command buffer. The framebuffer from GrVkRenderTarget will be used
215 // in the render pass.
216 void beginRenderPass(const GrVkGpu* gpu,
217 const GrVkRenderPass* renderPass,
Robert Phillips95214472017-08-08 18:00:03 -0400218 const VkClearValue clearValues[],
egdaniel9cb63402016-06-23 08:37:05 -0700219 const GrVkRenderTarget& target,
220 const SkIRect& bounds,
221 bool forSecondaryCB);
egdaniel9a6cf802016-06-08 08:22:05 -0700222 void endRenderPass(const GrVkGpu* gpu);
223
224 // Submits the SecondaryCommandBuffer into this command buffer. It is required that we are
225 // currently inside a render pass that is compatible with the one used to create the
226 // SecondaryCommandBuffer.
227 void executeCommands(const GrVkGpu* gpu,
jvanverth7ec92412016-07-06 09:24:57 -0700228 GrVkSecondaryCommandBuffer* secondaryBuffer);
egdaniel9a6cf802016-06-08 08:22:05 -0700229
Greg Daniel164a9f02016-02-22 09:56:40 -0500230 // Commands that only work outside of a render pass
231 void clearColorImage(const GrVkGpu* gpu,
232 GrVkImage* image,
233 const VkClearColorValue* color,
234 uint32_t subRangeCount,
235 const VkImageSubresourceRange* subRanges);
236
egdaniel3d5d9ac2016-03-01 12:56:15 -0800237 void clearDepthStencilImage(const GrVkGpu* gpu,
238 GrVkImage* image,
239 const VkClearDepthStencilValue* color,
240 uint32_t subRangeCount,
241 const VkImageSubresourceRange* subRanges);
242
Greg Daniel164a9f02016-02-22 09:56:40 -0500243 void copyImage(const GrVkGpu* gpu,
244 GrVkImage* srcImage,
245 VkImageLayout srcLayout,
246 GrVkImage* dstImage,
247 VkImageLayout dstLayout,
248 uint32_t copyRegionCount,
249 const VkImageCopy* copyRegions);
250
egdaniel17b89252016-04-05 07:23:38 -0700251 void blitImage(const GrVkGpu* gpu,
egdanielb2df0c22016-05-13 11:30:37 -0700252 const GrVkResource* srcResource,
253 VkImage srcImage,
egdaniel17b89252016-04-05 07:23:38 -0700254 VkImageLayout srcLayout,
egdanielb2df0c22016-05-13 11:30:37 -0700255 const GrVkResource* dstResource,
256 VkImage dstImage,
egdaniel17b89252016-04-05 07:23:38 -0700257 VkImageLayout dstLayout,
258 uint32_t blitRegionCount,
259 const VkImageBlit* blitRegions,
260 VkFilter filter);
261
egdanielb2df0c22016-05-13 11:30:37 -0700262 void blitImage(const GrVkGpu* gpu,
263 const GrVkImage& srcImage,
264 const GrVkImage& dstImage,
265 uint32_t blitRegionCount,
266 const VkImageBlit* blitRegions,
Greg Daniel6ecc9112017-06-16 16:17:03 +0000267 VkFilter filter);
egdanielb2df0c22016-05-13 11:30:37 -0700268
Greg Daniel164a9f02016-02-22 09:56:40 -0500269 void copyImageToBuffer(const GrVkGpu* gpu,
270 GrVkImage* srcImage,
271 VkImageLayout srcLayout,
272 GrVkTransferBuffer* dstBuffer,
273 uint32_t copyRegionCount,
274 const VkBufferImageCopy* copyRegions);
275
276 void copyBufferToImage(const GrVkGpu* gpu,
277 GrVkTransferBuffer* srcBuffer,
278 GrVkImage* dstImage,
279 VkImageLayout dstLayout,
280 uint32_t copyRegionCount,
281 const VkBufferImageCopy* copyRegions);
282
Greg Daniel6888c0d2017-08-25 11:55:50 -0400283 void copyBuffer(GrVkGpu* gpu,
284 GrVkBuffer* srcBuffer,
285 GrVkBuffer* dstBuffer,
286 uint32_t regionCount,
287 const VkBufferCopy* regions);
288
jvanvertha584de92016-06-30 09:10:52 -0700289 void updateBuffer(GrVkGpu* gpu,
290 GrVkBuffer* dstBuffer,
291 VkDeviceSize dstOffset,
292 VkDeviceSize dataSize,
293 const void* data);
294
egdaniel52ad2512016-08-04 12:50:01 -0700295 void resolveImage(GrVkGpu* gpu,
296 const GrVkImage& srcImage,
297 const GrVkImage& dstImage,
298 uint32_t regionCount,
299 const VkImageResolve* regions);
300
Greg Daniel6be35232017-03-01 17:01:09 -0500301 void submitToQueue(const GrVkGpu* gpu, VkQueue queue, GrVkGpu::SyncQueue sync,
Greg Daniel48661b82018-01-22 16:11:35 -0500302 SkTArray<GrVkSemaphore::Resource*>& signalSemaphores,
303 SkTArray<GrVkSemaphore::Resource*>& waitSemaphores);
egdaniel9a6cf802016-06-08 08:22:05 -0700304 bool finished(const GrVkGpu* gpu) const;
Greg Daniel164a9f02016-02-22 09:56:40 -0500305
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500306 void recycleSecondaryCommandBuffers();
307
jvanverth7ec92412016-07-06 09:24:57 -0700308#ifdef SK_TRACE_VK_RESOURCES
309 void dumpInfo() const override {
310 SkDebugf("GrVkPrimaryCommandBuffer: %d (%d refs)\n", fCmdBuffer, this->getRefCnt());
311 }
312#endif
313
Greg Daniel164a9f02016-02-22 09:56:40 -0500314private:
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500315 explicit GrVkPrimaryCommandBuffer(VkCommandBuffer cmdBuffer, GrVkCommandPool* cmdPool)
316 : INHERITED(cmdBuffer, cmdPool)
egdaniel9cb63402016-06-23 08:37:05 -0700317 , fSubmitFence(VK_NULL_HANDLE) {}
318
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500319 void onFreeGPUData(GrVkGpu* gpu) const override;
egdaniel9cb63402016-06-23 08:37:05 -0700320
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500321 void onAbandonGPUData() const override;
322
323 void onReleaseResources(GrVkGpu* gpu) override;
jvanverth7ec92412016-07-06 09:24:57 -0700324
325 SkTArray<GrVkSecondaryCommandBuffer*, true> fSecondaryCommandBuffers;
326 VkFence fSubmitFence;
Greg Daniel164a9f02016-02-22 09:56:40 -0500327
egdaniel9a6cf802016-06-08 08:22:05 -0700328 typedef GrVkCommandBuffer INHERITED;
Greg Daniel164a9f02016-02-22 09:56:40 -0500329};
330
egdaniel9a6cf802016-06-08 08:22:05 -0700331class GrVkSecondaryCommandBuffer : public GrVkCommandBuffer {
332public:
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500333 static GrVkSecondaryCommandBuffer* Create(const GrVkGpu* gpu, GrVkCommandPool* cmdPool);
Greg Daniel070cbaf2019-01-03 17:35:54 -0500334 // Used for wrapping an external secondary command buffer.
335 static GrVkSecondaryCommandBuffer* Create(VkCommandBuffer externalSecondaryCB);
egdaniel9a6cf802016-06-08 08:22:05 -0700336
jvanverth7ec92412016-07-06 09:24:57 -0700337 void begin(const GrVkGpu* gpu, const GrVkFramebuffer* framebuffer,
338 const GrVkRenderPass* compatibleRenderPass);
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500339 void end(GrVkGpu* gpu);
egdaniel9a6cf802016-06-08 08:22:05 -0700340
Greg Daniel64cc9aa2018-10-19 13:54:56 -0400341 VkCommandBuffer vkCommandBuffer() { return fCmdBuffer; }
342
jvanverth7ec92412016-07-06 09:24:57 -0700343#ifdef SK_TRACE_VK_RESOURCES
344 void dumpInfo() const override {
345 SkDebugf("GrVkSecondaryCommandBuffer: %d (%d refs)\n", fCmdBuffer, this->getRefCnt());
346 }
347#endif
348
egdaniel9a6cf802016-06-08 08:22:05 -0700349private:
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500350 explicit GrVkSecondaryCommandBuffer(VkCommandBuffer cmdBuffer, GrVkCommandPool* cmdPool)
351 : INHERITED(cmdBuffer, cmdPool) {}
egdaniel9a6cf802016-06-08 08:22:05 -0700352
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500353 void onFreeGPUData(GrVkGpu* gpu) const override {}
354
355 void onAbandonGPUData() const override {}
egdaniel9cb63402016-06-23 08:37:05 -0700356
egdaniel9a6cf802016-06-08 08:22:05 -0700357 friend class GrVkPrimaryCommandBuffer;
358
359 typedef GrVkCommandBuffer INHERITED;
360};
Greg Daniel164a9f02016-02-22 09:56:40 -0500361
362#endif