blob: 726095d0d0f7efcaf1d27446f4984e8b9b57325a [file] [log] [blame]
egdaniel066df7c2016-06-08 14:02:27 -07001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/gpu/vk/GrVkGpuCommandBuffer.h"
egdaniel066df7c2016-06-08 14:02:27 -07009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkDrawable.h"
11#include "include/core/SkRect.h"
12#include "include/gpu/GrBackendDrawableInfo.h"
13#include "src/gpu/GrContextPriv.h"
14#include "src/gpu/GrFixedClip.h"
15#include "src/gpu/GrMesh.h"
16#include "src/gpu/GrOpFlushState.h"
17#include "src/gpu/GrPipeline.h"
18#include "src/gpu/GrRenderTargetPriv.h"
19#include "src/gpu/GrTexturePriv.h"
20#include "src/gpu/vk/GrVkCommandBuffer.h"
21#include "src/gpu/vk/GrVkCommandPool.h"
22#include "src/gpu/vk/GrVkGpu.h"
23#include "src/gpu/vk/GrVkPipeline.h"
24#include "src/gpu/vk/GrVkRenderPass.h"
25#include "src/gpu/vk/GrVkRenderTarget.h"
26#include "src/gpu/vk/GrVkResourceProvider.h"
27#include "src/gpu/vk/GrVkSemaphore.h"
28#include "src/gpu/vk/GrVkTexture.h"
egdaniel066df7c2016-06-08 14:02:27 -070029
Brian Salomon5d8f1cc2019-04-24 09:03:53 -040030GrVkPrimaryCommandBufferTask::~GrVkPrimaryCommandBufferTask() = default;
31GrVkPrimaryCommandBufferTask::GrVkPrimaryCommandBufferTask() = default;
32
33namespace {
34
35class InlineUpload : public GrVkPrimaryCommandBufferTask {
36public:
37 InlineUpload(GrOpFlushState* state, const GrDeferredTextureUploadFn& upload)
38 : fFlushState(state), fUpload(upload) {}
Brian Salomon5d8f1cc2019-04-24 09:03:53 -040039
40 void execute(const Args& args) override { fFlushState->doUpload(fUpload); }
41
42private:
43 GrOpFlushState* fFlushState;
44 GrDeferredTextureUploadFn fUpload;
45};
46
47class Copy : public GrVkPrimaryCommandBufferTask {
48public:
Greg Daniel84ea0492019-06-05 16:52:02 -040049 Copy(GrSurface* src, const SkIRect& srcRect, const SkIPoint& dstPoint, bool shouldDiscardDst)
Brian Salomon5d8f1cc2019-04-24 09:03:53 -040050 : fSrc(src)
Brian Salomon5d8f1cc2019-04-24 09:03:53 -040051 , fSrcRect(srcRect)
52 , fDstPoint(dstPoint)
53 , fShouldDiscardDst(shouldDiscardDst) {}
Brian Salomon5d8f1cc2019-04-24 09:03:53 -040054
55 void execute(const Args& args) override {
Greg Daniel84ea0492019-06-05 16:52:02 -040056 args.fGpu->copySurface(args.fSurface, fSrc.get(), fSrcRect, fDstPoint, fShouldDiscardDst);
Brian Salomon5d8f1cc2019-04-24 09:03:53 -040057 }
58
59private:
60 using Src = GrPendingIOResource<GrSurface, kRead_GrIOType>;
61 Src fSrc;
Brian Salomon5d8f1cc2019-04-24 09:03:53 -040062 SkIRect fSrcRect;
63 SkIPoint fDstPoint;
64 bool fShouldDiscardDst;
65};
66
Brian Salomonab32f652019-05-10 14:24:50 -040067class TransferFrom : public GrVkPrimaryCommandBufferTask {
68public:
69 TransferFrom(const SkIRect& srcRect, GrColorType bufferColorType, GrGpuBuffer* transferBuffer,
70 size_t offset)
71 : fTransferBuffer(sk_ref_sp(transferBuffer))
72 , fOffset(offset)
73 , fSrcRect(srcRect)
74 , fBufferColorType(bufferColorType) {}
75
76 void execute(const Args& args) override {
77 args.fGpu->transferPixelsFrom(args.fSurface, fSrcRect.fLeft, fSrcRect.fTop,
78 fSrcRect.width(), fSrcRect.height(), fBufferColorType,
79 fTransferBuffer.get(), fOffset);
80 }
81
82private:
83 sk_sp<GrGpuBuffer> fTransferBuffer;
84 size_t fOffset;
85 SkIRect fSrcRect;
86 GrColorType fBufferColorType;
87};
88
Brian Salomon5d8f1cc2019-04-24 09:03:53 -040089} // anonymous namespace
90
91/////////////////////////////////////////////////////////////////////////////
92
Greg Daniel84ea0492019-06-05 16:52:02 -040093void GrVkGpuTextureCommandBuffer::copy(GrSurface* src, const SkIRect& srcRect,
94 const SkIPoint& dstPoint) {
95 fTasks.emplace<Copy>(src, srcRect, dstPoint, false);
Greg Daniel500d58b2017-08-24 15:59:33 -040096}
97
Brian Salomonab32f652019-05-10 14:24:50 -040098void GrVkGpuTextureCommandBuffer::transferFrom(const SkIRect& srcRect, GrColorType bufferColorType,
99 GrGpuBuffer* transferBuffer, size_t offset) {
100 fTasks.emplace<TransferFrom>(srcRect, bufferColorType, transferBuffer, offset);
101}
102
Greg Daniel500d58b2017-08-24 15:59:33 -0400103void GrVkGpuTextureCommandBuffer::insertEventMarker(const char* msg) {
104 // TODO: does Vulkan have a correlate?
105}
106
107void GrVkGpuTextureCommandBuffer::submit() {
Greg Daniel84ea0492019-06-05 16:52:02 -0400108 GrVkPrimaryCommandBufferTask::Args taskArgs{fGpu, fTexture};
Brian Salomon5d8f1cc2019-04-24 09:03:53 -0400109 for (auto& task : fTasks) {
110 task.execute(taskArgs);
Greg Daniel500d58b2017-08-24 15:59:33 -0400111 }
112}
113
Greg Daniel500d58b2017-08-24 15:59:33 -0400114////////////////////////////////////////////////////////////////////////////////
115
Robert Phillips6b47c7d2017-08-29 07:24:09 -0400116void get_vk_load_store_ops(GrLoadOp loadOpIn, GrStoreOp storeOpIn,
egdaniel066df7c2016-06-08 14:02:27 -0700117 VkAttachmentLoadOp* loadOp, VkAttachmentStoreOp* storeOp) {
Robert Phillips95214472017-08-08 18:00:03 -0400118 switch (loadOpIn) {
Robert Phillips6b47c7d2017-08-29 07:24:09 -0400119 case GrLoadOp::kLoad:
egdaniel066df7c2016-06-08 14:02:27 -0700120 *loadOp = VK_ATTACHMENT_LOAD_OP_LOAD;
egdaniel066df7c2016-06-08 14:02:27 -0700121 break;
Robert Phillips6b47c7d2017-08-29 07:24:09 -0400122 case GrLoadOp::kClear:
egdaniel9cb63402016-06-23 08:37:05 -0700123 *loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
124 break;
Robert Phillips6b47c7d2017-08-29 07:24:09 -0400125 case GrLoadOp::kDiscard:
egdaniel9cb63402016-06-23 08:37:05 -0700126 *loadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
127 break;
128 default:
129 SK_ABORT("Invalid LoadOp");
egdaniel066df7c2016-06-08 14:02:27 -0700130 *loadOp = VK_ATTACHMENT_LOAD_OP_LOAD;
egdaniel9cb63402016-06-23 08:37:05 -0700131 }
132
Robert Phillips95214472017-08-08 18:00:03 -0400133 switch (storeOpIn) {
Robert Phillips6b47c7d2017-08-29 07:24:09 -0400134 case GrStoreOp::kStore:
egdaniel066df7c2016-06-08 14:02:27 -0700135 *storeOp = VK_ATTACHMENT_STORE_OP_STORE;
136 break;
Robert Phillips6b47c7d2017-08-29 07:24:09 -0400137 case GrStoreOp::kDiscard:
egdaniel066df7c2016-06-08 14:02:27 -0700138 *storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
139 break;
brianosman0bbc3712016-06-14 04:53:09 -0700140 default:
egdaniel9cb63402016-06-23 08:37:05 -0700141 SK_ABORT("Invalid StoreOp");
brianosman0bbc3712016-06-14 04:53:09 -0700142 *storeOp = VK_ATTACHMENT_STORE_OP_STORE;
egdaniel066df7c2016-06-08 14:02:27 -0700143 }
144}
145
Brian Salomon24d377e2019-04-23 15:24:31 -0400146GrVkGpuRTCommandBuffer::GrVkGpuRTCommandBuffer(GrVkGpu* gpu) : fGpu(gpu) {}
Brian Salomonc293a292016-11-30 13:38:32 -0500147
Greg Daniel500d58b2017-08-24 15:59:33 -0400148void GrVkGpuRTCommandBuffer::init() {
Brian Salomonc293a292016-11-30 13:38:32 -0500149 GrVkRenderPass::LoadStoreOps vkColorOps(fVkColorLoadOp, fVkColorStoreOp);
150 GrVkRenderPass::LoadStoreOps vkStencilOps(fVkStencilLoadOp, fVkStencilStoreOp);
egdaniel9cb63402016-06-23 08:37:05 -0700151
Greg Daniel36a77ee2016-10-18 10:33:25 -0400152 CommandBufferInfo& cbInfo = fCommandBufferInfos.push_back();
Brian Salomonc293a292016-11-30 13:38:32 -0500153 SkASSERT(fCommandBufferInfos.count() == 1);
Greg Daniel22bc8652017-03-22 15:45:43 -0400154 fCurrentCmdInfo = 0;
Greg Daniel36a77ee2016-10-18 10:33:25 -0400155
Robert Phillips19e51dc2017-08-09 09:30:51 -0400156 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(fRenderTarget);
157 const GrVkResourceProvider::CompatibleRPHandle& rpHandle = vkRT->compatibleRenderPassHandle();
egdaniel066df7c2016-06-08 14:02:27 -0700158 if (rpHandle.isValid()) {
Greg Daniel36a77ee2016-10-18 10:33:25 -0400159 cbInfo.fRenderPass = fGpu->resourceProvider().findRenderPass(rpHandle,
160 vkColorOps,
161 vkStencilOps);
egdaniel066df7c2016-06-08 14:02:27 -0700162 } else {
Robert Phillips19e51dc2017-08-09 09:30:51 -0400163 cbInfo.fRenderPass = fGpu->resourceProvider().findRenderPass(*vkRT,
Greg Daniel36a77ee2016-10-18 10:33:25 -0400164 vkColorOps,
165 vkStencilOps);
egdaniel066df7c2016-06-08 14:02:27 -0700166 }
167
Brian Osmancb3d0872018-10-16 15:19:28 -0400168 cbInfo.fColorClearValue.color.float32[0] = fClearColor[0];
169 cbInfo.fColorClearValue.color.float32[1] = fClearColor[1];
170 cbInfo.fColorClearValue.color.float32[2] = fClearColor[2];
171 cbInfo.fColorClearValue.color.float32[3] = fClearColor[3];
egdaniel9cb63402016-06-23 08:37:05 -0700172
Robert Phillips380b90c2017-08-30 07:41:07 -0400173 if (VK_ATTACHMENT_LOAD_OP_CLEAR == fVkColorLoadOp) {
Greg Daniela41a74a2018-10-09 12:59:23 +0000174 cbInfo.fBounds = SkRect::MakeWH(vkRT->width(), vkRT->height());
Robert Phillips380b90c2017-08-30 07:41:07 -0400175 } else {
176 cbInfo.fBounds.setEmpty();
177 }
Greg Daniela3c68df2018-03-16 13:46:53 -0400178
179 if (VK_ATTACHMENT_LOAD_OP_CLEAR == fVkColorLoadOp) {
180 cbInfo.fLoadStoreState = LoadStoreState::kStartsWithClear;
181 } else if (VK_ATTACHMENT_LOAD_OP_LOAD == fVkColorLoadOp &&
182 VK_ATTACHMENT_STORE_OP_STORE == fVkColorStoreOp) {
183 cbInfo.fLoadStoreState = LoadStoreState::kLoadAndStore;
184 } else if (VK_ATTACHMENT_LOAD_OP_DONT_CARE == fVkColorLoadOp) {
185 cbInfo.fLoadStoreState = LoadStoreState::kStartsWithDiscard;
186 }
Greg Daniel36a77ee2016-10-18 10:33:25 -0400187
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500188 cbInfo.fCommandBuffers.push_back(fGpu->cmdPool()->findOrCreateSecondaryCommandBuffer(fGpu));
Robert Phillips19e51dc2017-08-09 09:30:51 -0400189 cbInfo.currentCmdBuf()->begin(fGpu, vkRT->framebuffer(), cbInfo.fRenderPass);
egdaniel066df7c2016-06-08 14:02:27 -0700190}
191
Greg Daniel070cbaf2019-01-03 17:35:54 -0500192void GrVkGpuRTCommandBuffer::initWrapped() {
193 CommandBufferInfo& cbInfo = fCommandBufferInfos.push_back();
194 SkASSERT(fCommandBufferInfos.count() == 1);
195 fCurrentCmdInfo = 0;
196
197 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(fRenderTarget);
198 SkASSERT(vkRT->wrapsSecondaryCommandBuffer());
199 cbInfo.fRenderPass = vkRT->externalRenderPass();
200 cbInfo.fRenderPass->ref();
201
202 cbInfo.fBounds.setEmpty();
203 cbInfo.fCommandBuffers.push_back(vkRT->getExternalSecondaryCommandBuffer());
204 cbInfo.fCommandBuffers[0]->ref();
205 cbInfo.currentCmdBuf()->begin(fGpu, nullptr, cbInfo.fRenderPass);
206}
Brian Salomonc293a292016-11-30 13:38:32 -0500207
Greg Daniel500d58b2017-08-24 15:59:33 -0400208GrVkGpuRTCommandBuffer::~GrVkGpuRTCommandBuffer() {
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400209 this->reset();
egdaniel066df7c2016-06-08 14:02:27 -0700210}
211
Greg Daniel500d58b2017-08-24 15:59:33 -0400212GrGpu* GrVkGpuRTCommandBuffer::gpu() { return fGpu; }
egdaniel9cb63402016-06-23 08:37:05 -0700213
Greg Daniel500d58b2017-08-24 15:59:33 -0400214void GrVkGpuRTCommandBuffer::end() {
Greg Daniel22bc8652017-03-22 15:45:43 -0400215 if (fCurrentCmdInfo >= 0) {
216 fCommandBufferInfos[fCurrentCmdInfo].currentCmdBuf()->end(fGpu);
Brian Salomonc293a292016-11-30 13:38:32 -0500217 }
egdaniel066df7c2016-06-08 14:02:27 -0700218}
219
Greg Daniel500d58b2017-08-24 15:59:33 -0400220void GrVkGpuRTCommandBuffer::submit() {
Brian Salomonc293a292016-11-30 13:38:32 -0500221 if (!fRenderTarget) {
222 return;
223 }
Robert Phillips19e51dc2017-08-09 09:30:51 -0400224
225 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(fRenderTarget);
Robert Phillips19e51dc2017-08-09 09:30:51 -0400226 GrVkImage* targetImage = vkRT->msaaImage() ? vkRT->msaaImage() : vkRT;
Greg Daniel45a44de2018-02-27 10:07:29 -0500227 GrStencilAttachment* stencil = fRenderTarget->renderTargetPriv().getStencilAttachment();
Brian Salomon24d377e2019-04-23 15:24:31 -0400228 auto currPreCmd = fPreCommandBufferTasks.begin();
egdaniel9cb63402016-06-23 08:37:05 -0700229
Greg Daniel84ea0492019-06-05 16:52:02 -0400230 GrVkPrimaryCommandBufferTask::Args taskArgs{fGpu, fRenderTarget};
Greg Daniel36a77ee2016-10-18 10:33:25 -0400231 for (int i = 0; i < fCommandBufferInfos.count(); ++i) {
232 CommandBufferInfo& cbInfo = fCommandBufferInfos[i];
233
Brian Salomon24d377e2019-04-23 15:24:31 -0400234 for (int c = 0; c < cbInfo.fNumPreCmds; ++c, ++currPreCmd) {
Brian Salomon5d8f1cc2019-04-24 09:03:53 -0400235 currPreCmd->execute(taskArgs);
Greg Daniel77b53f62016-10-18 11:48:51 -0400236 }
237
Greg Daniel38c3d932018-03-16 14:22:30 -0400238 // TODO: Many things create a scratch texture which adds the discard immediately, but then
239 // don't draw to it right away. This causes the discard to be ignored and we get yelled at
240 // for loading uninitialized data. However, once MDB lands with reordering, the discard will
241 // get reordered with the rest of the draw commands and we can remove the discard check.
242 if (cbInfo.fIsEmpty &&
243 cbInfo.fLoadStoreState != LoadStoreState::kStartsWithClear &&
244 cbInfo.fLoadStoreState != LoadStoreState::kStartsWithDiscard) {
Greg Daniel77b53f62016-10-18 11:48:51 -0400245 // We have sumbitted no actual draw commands to the command buffer and we are not using
246 // the render pass to do a clear so there is no need to submit anything.
247 continue;
248 }
Greg Daniel38c3d932018-03-16 14:22:30 -0400249
Greg Daniel070cbaf2019-01-03 17:35:54 -0500250 // We don't want to actually submit the secondary command buffer if it is wrapped.
251 if (this->wrapsSecondaryCommandBuffer()) {
252 // If we have any sampled images set their layout now.
Chris Dalton298238a2019-02-21 16:28:44 -0500253 for (int j = 0; j < cbInfo.fSampledTextures.count(); ++j) {
254 cbInfo.fSampledTextures[j]->setImageLayout(
255 fGpu, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, VK_ACCESS_SHADER_READ_BIT,
256 VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT, false);
Greg Daniel070cbaf2019-01-03 17:35:54 -0500257 }
258
259 // There should have only been one secondary command buffer in the wrapped case so it is
260 // safe to just return here.
261 SkASSERT(fCommandBufferInfos.count() == 1);
262 return;
263 }
264
Greg Danieldbdba602018-04-20 11:52:43 -0400265 // Make sure if we only have a discard load that we execute the discard on the whole image.
266 // TODO: Once we improve our tracking of discards so that we never end up flushing a discard
267 // call with no actually ops, remove this.
268 if (cbInfo.fIsEmpty && cbInfo.fLoadStoreState == LoadStoreState::kStartsWithDiscard) {
Greg Daniela41a74a2018-10-09 12:59:23 +0000269 cbInfo.fBounds = SkRect::MakeWH(vkRT->width(), vkRT->height());
Greg Danieldbdba602018-04-20 11:52:43 -0400270 }
271
Greg Daniela41a74a2018-10-09 12:59:23 +0000272 if (cbInfo.fBounds.intersect(0, 0,
273 SkIntToScalar(fRenderTarget->width()),
274 SkIntToScalar(fRenderTarget->height()))) {
Greg Daniel38c3d932018-03-16 14:22:30 -0400275 // Make sure we do the following layout changes after all copies, uploads, or any other
276 // pre-work is done since we may change the layouts in the pre-work. Also since the
277 // draws will be submitted in different render passes, we need to guard againts write
278 // and write issues.
279
280 // Change layout of our render target so it can be used as the color attachment.
Greg Danielf7828d02018-10-09 12:01:32 -0400281 // TODO: If we know that we will never be blending or loading the attachment we could
282 // drop the VK_ACCESS_COLOR_ATTACHMENT_READ_BIT.
Greg Daniel38c3d932018-03-16 14:22:30 -0400283 targetImage->setImageLayout(fGpu,
284 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Greg Danielf7828d02018-10-09 12:01:32 -0400285 VK_ACCESS_COLOR_ATTACHMENT_READ_BIT |
Greg Daniel38c3d932018-03-16 14:22:30 -0400286 VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
Greg Danielf7828d02018-10-09 12:01:32 -0400287 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
Greg Daniel38c3d932018-03-16 14:22:30 -0400288 false);
289
290 // If we are using a stencil attachment we also need to update its layout
291 if (stencil) {
292 GrVkStencilAttachment* vkStencil = (GrVkStencilAttachment*)stencil;
Greg Danielf7828d02018-10-09 12:01:32 -0400293 // We need the write and read access bits since we may load and store the stencil.
294 // The initial load happens in the VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT so we
295 // wait there.
Greg Daniel38c3d932018-03-16 14:22:30 -0400296 vkStencil->setImageLayout(fGpu,
297 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
298 VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT |
299 VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT,
Greg Danielf7828d02018-10-09 12:01:32 -0400300 VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT,
Greg Daniel38c3d932018-03-16 14:22:30 -0400301 false);
302 }
303
304 // If we have any sampled images set their layout now.
Chris Dalton298238a2019-02-21 16:28:44 -0500305 for (int j = 0; j < cbInfo.fSampledTextures.count(); ++j) {
306 cbInfo.fSampledTextures[j]->setImageLayout(
307 fGpu, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, VK_ACCESS_SHADER_READ_BIT,
308 VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT, false);
Greg Daniel38c3d932018-03-16 14:22:30 -0400309 }
310
Greg Daniel36a77ee2016-10-18 10:33:25 -0400311 SkIRect iBounds;
312 cbInfo.fBounds.roundOut(&iBounds);
313
Greg Daniel22bc8652017-03-22 15:45:43 -0400314 fGpu->submitSecondaryCommandBuffer(cbInfo.fCommandBuffers, cbInfo.fRenderPass,
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400315 &cbInfo.fColorClearValue, vkRT, fOrigin, iBounds);
Greg Daniel36a77ee2016-10-18 10:33:25 -0400316 }
317 }
Brian Salomon24d377e2019-04-23 15:24:31 -0400318 SkASSERT(currPreCmd == fPreCommandBufferTasks.end());
egdaniel9cb63402016-06-23 08:37:05 -0700319}
320
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400321void GrVkGpuRTCommandBuffer::set(GrRenderTarget* rt, GrSurfaceOrigin origin,
322 const GrGpuRTCommandBuffer::LoadAndStoreInfo& colorInfo,
323 const GrGpuRTCommandBuffer::StencilLoadAndStoreInfo& stencilInfo) {
324 SkASSERT(!fRenderTarget);
325 SkASSERT(fCommandBufferInfos.empty());
326 SkASSERT(-1 == fCurrentCmdInfo);
Robert Phillips9da87e02019-02-04 13:26:26 -0500327 SkASSERT(fGpu == rt->getContext()->priv().getGpu());
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400328 SkASSERT(!fLastPipelineState);
329
330 this->INHERITED::set(rt, origin);
331
Greg Daniel070cbaf2019-01-03 17:35:54 -0500332 if (this->wrapsSecondaryCommandBuffer()) {
333 this->initWrapped();
334 return;
335 }
336
Brian Osman9a9baae2018-11-05 15:06:26 -0500337 fClearColor = colorInfo.fClearColor;
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400338
339 get_vk_load_store_ops(colorInfo.fLoadOp, colorInfo.fStoreOp,
340 &fVkColorLoadOp, &fVkColorStoreOp);
341
342 get_vk_load_store_ops(stencilInfo.fLoadOp, stencilInfo.fStoreOp,
343 &fVkStencilLoadOp, &fVkStencilStoreOp);
344
345 this->init();
346}
347
348void GrVkGpuRTCommandBuffer::reset() {
349 for (int i = 0; i < fCommandBufferInfos.count(); ++i) {
350 CommandBufferInfo& cbInfo = fCommandBufferInfos[i];
351 for (int j = 0; j < cbInfo.fCommandBuffers.count(); ++j) {
352 cbInfo.fCommandBuffers[j]->unref(fGpu);
353 }
354 cbInfo.fRenderPass->unref(fGpu);
355 }
356 fCommandBufferInfos.reset();
Brian Salomon24d377e2019-04-23 15:24:31 -0400357 fPreCommandBufferTasks.reset();
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400358
359 fCurrentCmdInfo = -1;
360
361 fLastPipelineState = nullptr;
362 fRenderTarget = nullptr;
363}
364
Greg Daniel070cbaf2019-01-03 17:35:54 -0500365bool GrVkGpuRTCommandBuffer::wrapsSecondaryCommandBuffer() const {
366 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(fRenderTarget);
367 return vkRT->wrapsSecondaryCommandBuffer();
368}
369
Greg Daniel64cc9aa2018-10-19 13:54:56 -0400370////////////////////////////////////////////////////////////////////////////////
371
Greg Daniel500d58b2017-08-24 15:59:33 -0400372void GrVkGpuRTCommandBuffer::discard() {
Robert Phillips19e51dc2017-08-09 09:30:51 -0400373 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(fRenderTarget);
Brian Salomonc293a292016-11-30 13:38:32 -0500374
Greg Daniel22bc8652017-03-22 15:45:43 -0400375 CommandBufferInfo& cbInfo = fCommandBufferInfos[fCurrentCmdInfo];
Greg Daniel77b53f62016-10-18 11:48:51 -0400376 if (cbInfo.fIsEmpty) {
Robert Phillips74c627f2017-08-09 10:28:00 -0400377 // Change the render pass to do a don't-care load for both color & stencil
egdaniel37535c92016-06-30 08:23:30 -0700378 GrVkRenderPass::LoadStoreOps vkColorOps(VK_ATTACHMENT_LOAD_OP_DONT_CARE,
379 VK_ATTACHMENT_STORE_OP_STORE);
380 GrVkRenderPass::LoadStoreOps vkStencilOps(VK_ATTACHMENT_LOAD_OP_DONT_CARE,
381 VK_ATTACHMENT_STORE_OP_STORE);
egdaniel37535c92016-06-30 08:23:30 -0700382
Greg Daniel36a77ee2016-10-18 10:33:25 -0400383 const GrVkRenderPass* oldRP = cbInfo.fRenderPass;
egdaniel37535c92016-06-30 08:23:30 -0700384
egdaniel37535c92016-06-30 08:23:30 -0700385 const GrVkResourceProvider::CompatibleRPHandle& rpHandle =
Robert Phillips19e51dc2017-08-09 09:30:51 -0400386 vkRT->compatibleRenderPassHandle();
egdaniel37535c92016-06-30 08:23:30 -0700387 if (rpHandle.isValid()) {
Greg Daniel36a77ee2016-10-18 10:33:25 -0400388 cbInfo.fRenderPass = fGpu->resourceProvider().findRenderPass(rpHandle,
389 vkColorOps,
390 vkStencilOps);
egdaniel37535c92016-06-30 08:23:30 -0700391 } else {
Robert Phillips19e51dc2017-08-09 09:30:51 -0400392 cbInfo.fRenderPass = fGpu->resourceProvider().findRenderPass(*vkRT,
Greg Daniel36a77ee2016-10-18 10:33:25 -0400393 vkColorOps,
394 vkStencilOps);
egdaniel37535c92016-06-30 08:23:30 -0700395 }
396
Greg Daniel36a77ee2016-10-18 10:33:25 -0400397 SkASSERT(cbInfo.fRenderPass->isCompatible(*oldRP));
egdaniel37535c92016-06-30 08:23:30 -0700398 oldRP->unref(fGpu);
Greg Daniel5011f852016-10-28 15:07:16 -0400399 cbInfo.fBounds.join(fRenderTarget->getBoundsRect());
Greg Daniela3c68df2018-03-16 13:46:53 -0400400 cbInfo.fLoadStoreState = LoadStoreState::kStartsWithDiscard;
egdaniel37535c92016-06-30 08:23:30 -0700401 }
402}
403
Greg Daniel500d58b2017-08-24 15:59:33 -0400404void GrVkGpuRTCommandBuffer::insertEventMarker(const char* msg) {
Robert Phillips65a88fa2017-08-08 08:36:22 -0400405 // TODO: does Vulkan have a correlate?
406}
407
Greg Daniel500d58b2017-08-24 15:59:33 -0400408void GrVkGpuRTCommandBuffer::onClearStencilClip(const GrFixedClip& clip, bool insideStencilMask) {
Chris Dalton94c04682017-11-01 17:15:06 -0600409 SkASSERT(!clip.hasWindowRectangles());
410
Jim Van Verth6a40abc2017-11-02 16:56:09 +0000411 CommandBufferInfo& cbInfo = fCommandBufferInfos[fCurrentCmdInfo];
412
Greg Daniel65a09272016-10-12 09:47:22 -0400413 GrStencilAttachment* sb = fRenderTarget->renderTargetPriv().getStencilAttachment();
egdaniel9cb63402016-06-23 08:37:05 -0700414 // this should only be called internally when we know we have a
415 // stencil buffer.
416 SkASSERT(sb);
417 int stencilBitCount = sb->bits();
418
419 // The contract with the callers does not guarantee that we preserve all bits in the stencil
420 // during this clear. Thus we will clear the entire stencil to the desired value.
421
422 VkClearDepthStencilValue vkStencilColor;
423 memset(&vkStencilColor, 0, sizeof(VkClearDepthStencilValue));
csmartdalton29df7602016-08-31 11:55:52 -0700424 if (insideStencilMask) {
egdaniel9cb63402016-06-23 08:37:05 -0700425 vkStencilColor.stencil = (1 << (stencilBitCount - 1));
426 } else {
427 vkStencilColor.stencil = 0;
428 }
429
430 VkClearRect clearRect;
431 // Flip rect if necessary
csmartdalton29df7602016-08-31 11:55:52 -0700432 SkIRect vkRect;
Brian Salomond818ebf2018-07-02 14:08:49 +0000433 if (!clip.scissorEnabled()) {
Greg Daniela41a74a2018-10-09 12:59:23 +0000434 vkRect.setXYWH(0, 0, fRenderTarget->width(), fRenderTarget->height());
Robert Phillips4f101a72017-07-28 08:42:04 -0400435 } else if (kBottomLeft_GrSurfaceOrigin != fOrigin) {
csmartdalton29df7602016-08-31 11:55:52 -0700436 vkRect = clip.scissorRect();
437 } else {
438 const SkIRect& scissor = clip.scissorRect();
Greg Daniel65a09272016-10-12 09:47:22 -0400439 vkRect.setLTRB(scissor.fLeft, fRenderTarget->height() - scissor.fBottom,
440 scissor.fRight, fRenderTarget->height() - scissor.fTop);
egdaniel9cb63402016-06-23 08:37:05 -0700441 }
442
443 clearRect.rect.offset = { vkRect.fLeft, vkRect.fTop };
444 clearRect.rect.extent = { (uint32_t)vkRect.width(), (uint32_t)vkRect.height() };
445
446 clearRect.baseArrayLayer = 0;
447 clearRect.layerCount = 1;
448
449 uint32_t stencilIndex;
Greg Daniel36a77ee2016-10-18 10:33:25 -0400450 SkAssertResult(cbInfo.fRenderPass->stencilAttachmentIndex(&stencilIndex));
egdaniel9cb63402016-06-23 08:37:05 -0700451
452 VkClearAttachment attachment;
453 attachment.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
454 attachment.colorAttachment = 0; // this value shouldn't matter
455 attachment.clearValue.depthStencil = vkStencilColor;
456
Greg Daniel22bc8652017-03-22 15:45:43 -0400457 cbInfo.currentCmdBuf()->clearAttachments(fGpu, 1, &attachment, 1, &clearRect);
Greg Daniel77b53f62016-10-18 11:48:51 -0400458 cbInfo.fIsEmpty = false;
Greg Daniel36a77ee2016-10-18 10:33:25 -0400459
460 // Update command buffer bounds
Brian Salomond818ebf2018-07-02 14:08:49 +0000461 if (!clip.scissorEnabled()) {
Greg Daniel36a77ee2016-10-18 10:33:25 -0400462 cbInfo.fBounds.join(fRenderTarget->getBoundsRect());
463 } else {
464 cbInfo.fBounds.join(SkRect::Make(clip.scissorRect()));
465 }
egdaniel9cb63402016-06-23 08:37:05 -0700466}
467
Brian Osman9a9baae2018-11-05 15:06:26 -0500468void GrVkGpuRTCommandBuffer::onClear(const GrFixedClip& clip, const SkPMColor4f& color) {
Robert Phillips19e51dc2017-08-09 09:30:51 -0400469 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(fRenderTarget);
470
Jim Van Verth6a40abc2017-11-02 16:56:09 +0000471 // parent class should never let us get here with no RT
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700472 SkASSERT(!clip.hasWindowRectangles());
egdaniel9cb63402016-06-23 08:37:05 -0700473
Greg Daniel22bc8652017-03-22 15:45:43 -0400474 CommandBufferInfo& cbInfo = fCommandBufferInfos[fCurrentCmdInfo];
Greg Daniel36a77ee2016-10-18 10:33:25 -0400475
Brian Osman9a9baae2018-11-05 15:06:26 -0500476 VkClearColorValue vkColor = {{color.fR, color.fG, color.fB, color.fA}};
egdaniel9cb63402016-06-23 08:37:05 -0700477
Brian Salomond818ebf2018-07-02 14:08:49 +0000478 if (cbInfo.fIsEmpty && !clip.scissorEnabled()) {
Robert Phillips74c627f2017-08-09 10:28:00 -0400479 // Change the render pass to do a clear load
egdaniel9cb63402016-06-23 08:37:05 -0700480 GrVkRenderPass::LoadStoreOps vkColorOps(VK_ATTACHMENT_LOAD_OP_CLEAR,
481 VK_ATTACHMENT_STORE_OP_STORE);
Robert Phillips74c627f2017-08-09 10:28:00 -0400482 // Preserve the stencil buffer's load & store settings
483 GrVkRenderPass::LoadStoreOps vkStencilOps(fVkStencilLoadOp, fVkStencilStoreOp);
egdaniel9cb63402016-06-23 08:37:05 -0700484
Greg Daniel36a77ee2016-10-18 10:33:25 -0400485 const GrVkRenderPass* oldRP = cbInfo.fRenderPass;
egdaniel9cb63402016-06-23 08:37:05 -0700486
487 const GrVkResourceProvider::CompatibleRPHandle& rpHandle =
Robert Phillips19e51dc2017-08-09 09:30:51 -0400488 vkRT->compatibleRenderPassHandle();
egdaniel9cb63402016-06-23 08:37:05 -0700489 if (rpHandle.isValid()) {
Greg Daniel36a77ee2016-10-18 10:33:25 -0400490 cbInfo.fRenderPass = fGpu->resourceProvider().findRenderPass(rpHandle,
491 vkColorOps,
492 vkStencilOps);
egdaniel9cb63402016-06-23 08:37:05 -0700493 } else {
Robert Phillips19e51dc2017-08-09 09:30:51 -0400494 cbInfo.fRenderPass = fGpu->resourceProvider().findRenderPass(*vkRT,
Greg Daniel36a77ee2016-10-18 10:33:25 -0400495 vkColorOps,
496 vkStencilOps);
egdaniel9cb63402016-06-23 08:37:05 -0700497 }
498
Greg Daniel36a77ee2016-10-18 10:33:25 -0400499 SkASSERT(cbInfo.fRenderPass->isCompatible(*oldRP));
egdaniel9cb63402016-06-23 08:37:05 -0700500 oldRP->unref(fGpu);
501
Brian Osman9a9baae2018-11-05 15:06:26 -0500502 cbInfo.fColorClearValue.color = {{color.fR, color.fG, color.fB, color.fA}};
Greg Daniela3c68df2018-03-16 13:46:53 -0400503 cbInfo.fLoadStoreState = LoadStoreState::kStartsWithClear;
Greg Daniel36a77ee2016-10-18 10:33:25 -0400504 // Update command buffer bounds
505 cbInfo.fBounds.join(fRenderTarget->getBoundsRect());
egdaniel9cb63402016-06-23 08:37:05 -0700506 return;
507 }
508
509 // We always do a sub rect clear with clearAttachments since we are inside a render pass
510 VkClearRect clearRect;
511 // Flip rect if necessary
csmartdalton29df7602016-08-31 11:55:52 -0700512 SkIRect vkRect;
Brian Salomond818ebf2018-07-02 14:08:49 +0000513 if (!clip.scissorEnabled()) {
Greg Daniela41a74a2018-10-09 12:59:23 +0000514 vkRect.setXYWH(0, 0, fRenderTarget->width(), fRenderTarget->height());
Robert Phillips4f101a72017-07-28 08:42:04 -0400515 } else if (kBottomLeft_GrSurfaceOrigin != fOrigin) {
csmartdalton29df7602016-08-31 11:55:52 -0700516 vkRect = clip.scissorRect();
517 } else {
518 const SkIRect& scissor = clip.scissorRect();
Greg Daniel65a09272016-10-12 09:47:22 -0400519 vkRect.setLTRB(scissor.fLeft, fRenderTarget->height() - scissor.fBottom,
520 scissor.fRight, fRenderTarget->height() - scissor.fTop);
egdaniel9cb63402016-06-23 08:37:05 -0700521 }
522 clearRect.rect.offset = { vkRect.fLeft, vkRect.fTop };
523 clearRect.rect.extent = { (uint32_t)vkRect.width(), (uint32_t)vkRect.height() };
524 clearRect.baseArrayLayer = 0;
525 clearRect.layerCount = 1;
526
527 uint32_t colorIndex;
Greg Daniel36a77ee2016-10-18 10:33:25 -0400528 SkAssertResult(cbInfo.fRenderPass->colorAttachmentIndex(&colorIndex));
egdaniel9cb63402016-06-23 08:37:05 -0700529
530 VkClearAttachment attachment;
531 attachment.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
532 attachment.colorAttachment = colorIndex;
533 attachment.clearValue.color = vkColor;
534
Greg Daniel22bc8652017-03-22 15:45:43 -0400535 cbInfo.currentCmdBuf()->clearAttachments(fGpu, 1, &attachment, 1, &clearRect);
Greg Daniel77b53f62016-10-18 11:48:51 -0400536 cbInfo.fIsEmpty = false;
Greg Daniel36a77ee2016-10-18 10:33:25 -0400537
538 // Update command buffer bounds
Brian Salomond818ebf2018-07-02 14:08:49 +0000539 if (!clip.scissorEnabled()) {
Greg Daniel36a77ee2016-10-18 10:33:25 -0400540 cbInfo.fBounds.join(fRenderTarget->getBoundsRect());
541 } else {
542 cbInfo.fBounds.join(SkRect::Make(clip.scissorRect()));
543 }
egdaniel9cb63402016-06-23 08:37:05 -0700544 return;
545}
546
Greg Daniel500d58b2017-08-24 15:59:33 -0400547////////////////////////////////////////////////////////////////////////////////
548
549void GrVkGpuRTCommandBuffer::addAdditionalCommandBuffer() {
Robert Phillips19e51dc2017-08-09 09:30:51 -0400550 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(fRenderTarget);
551
Greg Daniel22bc8652017-03-22 15:45:43 -0400552 CommandBufferInfo& cbInfo = fCommandBufferInfos[fCurrentCmdInfo];
553 cbInfo.currentCmdBuf()->end(fGpu);
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500554 cbInfo.fCommandBuffers.push_back(fGpu->cmdPool()->findOrCreateSecondaryCommandBuffer(fGpu));
Robert Phillips19e51dc2017-08-09 09:30:51 -0400555 cbInfo.currentCmdBuf()->begin(fGpu, vkRT->framebuffer(), cbInfo.fRenderPass);
Greg Daniel22bc8652017-03-22 15:45:43 -0400556}
557
Greg Daniel500d58b2017-08-24 15:59:33 -0400558void GrVkGpuRTCommandBuffer::addAdditionalRenderPass() {
Robert Phillips19e51dc2017-08-09 09:30:51 -0400559 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(fRenderTarget);
560
Greg Daniel22bc8652017-03-22 15:45:43 -0400561 fCommandBufferInfos[fCurrentCmdInfo].currentCmdBuf()->end(fGpu);
Greg Daniel77b53f62016-10-18 11:48:51 -0400562
563 CommandBufferInfo& cbInfo = fCommandBufferInfos.push_back();
Greg Daniel22bc8652017-03-22 15:45:43 -0400564 fCurrentCmdInfo++;
Greg Daniel77b53f62016-10-18 11:48:51 -0400565
566 GrVkRenderPass::LoadStoreOps vkColorOps(VK_ATTACHMENT_LOAD_OP_LOAD,
567 VK_ATTACHMENT_STORE_OP_STORE);
568 GrVkRenderPass::LoadStoreOps vkStencilOps(VK_ATTACHMENT_LOAD_OP_LOAD,
569 VK_ATTACHMENT_STORE_OP_STORE);
570
571 const GrVkResourceProvider::CompatibleRPHandle& rpHandle =
Robert Phillips19e51dc2017-08-09 09:30:51 -0400572 vkRT->compatibleRenderPassHandle();
Greg Daniel77b53f62016-10-18 11:48:51 -0400573 if (rpHandle.isValid()) {
574 cbInfo.fRenderPass = fGpu->resourceProvider().findRenderPass(rpHandle,
575 vkColorOps,
576 vkStencilOps);
577 } else {
Robert Phillips19e51dc2017-08-09 09:30:51 -0400578 cbInfo.fRenderPass = fGpu->resourceProvider().findRenderPass(*vkRT,
Greg Daniel77b53f62016-10-18 11:48:51 -0400579 vkColorOps,
580 vkStencilOps);
581 }
Greg Daniela3c68df2018-03-16 13:46:53 -0400582 cbInfo.fLoadStoreState = LoadStoreState::kLoadAndStore;
Greg Daniel77b53f62016-10-18 11:48:51 -0400583
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500584 cbInfo.fCommandBuffers.push_back(fGpu->cmdPool()->findOrCreateSecondaryCommandBuffer(fGpu));
Greg Daniel77b53f62016-10-18 11:48:51 -0400585 // It shouldn't matter what we set the clear color to here since we will assume loading of the
586 // attachment.
587 memset(&cbInfo.fColorClearValue, 0, sizeof(VkClearValue));
588 cbInfo.fBounds.setEmpty();
Greg Daniel77b53f62016-10-18 11:48:51 -0400589
Robert Phillips19e51dc2017-08-09 09:30:51 -0400590 cbInfo.currentCmdBuf()->begin(fGpu, vkRT->framebuffer(), cbInfo.fRenderPass);
Greg Daniel77b53f62016-10-18 11:48:51 -0400591}
592
Brian Salomon943ed792017-10-30 09:37:55 -0400593void GrVkGpuRTCommandBuffer::inlineUpload(GrOpFlushState* state,
594 GrDeferredTextureUploadFn& upload) {
Greg Daniel22bc8652017-03-22 15:45:43 -0400595 if (!fCommandBufferInfos[fCurrentCmdInfo].fIsEmpty) {
596 this->addAdditionalRenderPass();
Greg Daniel77b53f62016-10-18 11:48:51 -0400597 }
Brian Salomon24d377e2019-04-23 15:24:31 -0400598
Brian Salomon24d377e2019-04-23 15:24:31 -0400599 fPreCommandBufferTasks.emplace<InlineUpload>(state, upload);
600 ++fCommandBufferInfos[fCurrentCmdInfo].fNumPreCmds;
Greg Daniel77b53f62016-10-18 11:48:51 -0400601}
602
Greg Daniel84ea0492019-06-05 16:52:02 -0400603void GrVkGpuRTCommandBuffer::copy(GrSurface* src, const SkIRect& srcRect,
Greg Daniel500d58b2017-08-24 15:59:33 -0400604 const SkIPoint& dstPoint) {
Greg Daniela3c68df2018-03-16 13:46:53 -0400605 CommandBufferInfo& cbInfo = fCommandBufferInfos[fCurrentCmdInfo];
606 if (!cbInfo.fIsEmpty || LoadStoreState::kStartsWithClear == cbInfo.fLoadStoreState) {
Greg Daniel500d58b2017-08-24 15:59:33 -0400607 this->addAdditionalRenderPass();
608 }
Greg Daniela3c68df2018-03-16 13:46:53 -0400609
Brian Salomon24d377e2019-04-23 15:24:31 -0400610 fPreCommandBufferTasks.emplace<Copy>(
Greg Daniel84ea0492019-06-05 16:52:02 -0400611 src, srcRect, dstPoint, LoadStoreState::kStartsWithDiscard == cbInfo.fLoadStoreState);
Brian Salomon24d377e2019-04-23 15:24:31 -0400612 ++fCommandBufferInfos[fCurrentCmdInfo].fNumPreCmds;
Greg Daniel55fa6472018-03-16 16:13:10 -0400613
Greg Daniela3c68df2018-03-16 13:46:53 -0400614 if (LoadStoreState::kLoadAndStore != cbInfo.fLoadStoreState) {
615 // Change the render pass to do a load and store so we don't lose the results of our copy
616 GrVkRenderPass::LoadStoreOps vkColorOps(VK_ATTACHMENT_LOAD_OP_LOAD,
617 VK_ATTACHMENT_STORE_OP_STORE);
618 GrVkRenderPass::LoadStoreOps vkStencilOps(VK_ATTACHMENT_LOAD_OP_LOAD,
619 VK_ATTACHMENT_STORE_OP_STORE);
620
621 const GrVkRenderPass* oldRP = cbInfo.fRenderPass;
622
623 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(fRenderTarget);
624 const GrVkResourceProvider::CompatibleRPHandle& rpHandle =
625 vkRT->compatibleRenderPassHandle();
626 if (rpHandle.isValid()) {
627 cbInfo.fRenderPass = fGpu->resourceProvider().findRenderPass(rpHandle,
628 vkColorOps,
629 vkStencilOps);
630 } else {
631 cbInfo.fRenderPass = fGpu->resourceProvider().findRenderPass(*vkRT,
632 vkColorOps,
633 vkStencilOps);
634 }
635 SkASSERT(cbInfo.fRenderPass->isCompatible(*oldRP));
636 oldRP->unref(fGpu);
637
638 cbInfo.fLoadStoreState = LoadStoreState::kLoadAndStore;
639
640 }
Greg Daniel500d58b2017-08-24 15:59:33 -0400641}
642
Brian Salomonab32f652019-05-10 14:24:50 -0400643void GrVkGpuRTCommandBuffer::transferFrom(const SkIRect& srcRect, GrColorType bufferColorType,
644 GrGpuBuffer* transferBuffer, size_t offset) {
645 CommandBufferInfo& cbInfo = fCommandBufferInfos[fCurrentCmdInfo];
646 if (!cbInfo.fIsEmpty) {
647 this->addAdditionalRenderPass();
648 }
649 fPreCommandBufferTasks.emplace<TransferFrom>(srcRect, bufferColorType, transferBuffer, offset);
650 ++fCommandBufferInfos[fCurrentCmdInfo].fNumPreCmds;
651}
652
egdaniel9cb63402016-06-23 08:37:05 -0700653////////////////////////////////////////////////////////////////////////////////
654
Brian Salomondbf70722019-02-07 11:31:24 -0500655void GrVkGpuRTCommandBuffer::bindGeometry(const GrGpuBuffer* indexBuffer,
656 const GrGpuBuffer* vertexBuffer,
657 const GrGpuBuffer* instanceBuffer) {
Chris Daltonff926502017-05-03 14:36:54 -0400658 GrVkSecondaryCommandBuffer* currCmdBuf = fCommandBufferInfos[fCurrentCmdInfo].currentCmdBuf();
egdaniel9cb63402016-06-23 08:37:05 -0700659 // There is no need to put any memory barriers to make sure host writes have finished here.
660 // When a command buffer is submitted to a queue, there is an implicit memory barrier that
661 // occurs for all host writes. Additionally, BufferMemoryBarriers are not allowed inside of
662 // an active RenderPass.
egdaniel9cb63402016-06-23 08:37:05 -0700663
Chris Dalton1d616352017-05-31 12:51:23 -0600664 // Here our vertex and instance inputs need to match the same 0-based bindings they were
665 // assigned in GrVkPipeline. That is, vertex first (if any) followed by instance.
666 uint32_t binding = 0;
667
Brian Salomon802cb312018-06-08 18:05:20 -0400668 if (vertexBuffer) {
Chris Dalton1d616352017-05-31 12:51:23 -0600669 SkASSERT(vertexBuffer);
Chris Dalton1d616352017-05-31 12:51:23 -0600670 SkASSERT(!vertexBuffer->isMapped());
671
672 currCmdBuf->bindInputBuffer(fGpu, binding++,
673 static_cast<const GrVkVertexBuffer*>(vertexBuffer));
674 }
675
Brian Salomon802cb312018-06-08 18:05:20 -0400676 if (instanceBuffer) {
Chris Dalton1d616352017-05-31 12:51:23 -0600677 SkASSERT(instanceBuffer);
Chris Dalton1d616352017-05-31 12:51:23 -0600678 SkASSERT(!instanceBuffer->isMapped());
679
680 currCmdBuf->bindInputBuffer(fGpu, binding++,
681 static_cast<const GrVkVertexBuffer*>(instanceBuffer));
682 }
Chris Daltonff926502017-05-03 14:36:54 -0400683 if (indexBuffer) {
684 SkASSERT(indexBuffer);
685 SkASSERT(!indexBuffer->isMapped());
egdaniel9cb63402016-06-23 08:37:05 -0700686
Chris Daltonff926502017-05-03 14:36:54 -0400687 currCmdBuf->bindIndexBuffer(fGpu, static_cast<const GrVkIndexBuffer*>(indexBuffer));
egdaniel9cb63402016-06-23 08:37:05 -0700688 }
689}
690
Brian Salomon49348902018-06-26 09:12:38 -0400691GrVkPipelineState* GrVkGpuRTCommandBuffer::prepareDrawState(
692 const GrPrimitiveProcessor& primProc,
693 const GrPipeline& pipeline,
694 const GrPipeline::FixedDynamicState* fixedDynamicState,
695 const GrPipeline::DynamicStateArrays* dynamicStateArrays,
696 GrPrimitiveType primitiveType) {
Greg Daniel22bc8652017-03-22 15:45:43 -0400697 CommandBufferInfo& cbInfo = fCommandBufferInfos[fCurrentCmdInfo];
698 SkASSERT(cbInfo.fRenderPass);
Greg Daniel36a77ee2016-10-18 10:33:25 -0400699
Greg Daniel99b88e02018-10-03 15:31:20 -0400700 VkRenderPass compatibleRenderPass = cbInfo.fRenderPass->vkRenderPass();
701
Greg Daniel9a51a862018-11-30 10:18:14 -0500702 const GrTextureProxy* const* primProcProxies = nullptr;
703 if (dynamicStateArrays && dynamicStateArrays->fPrimitiveProcessorTextures) {
704 primProcProxies = dynamicStateArrays->fPrimitiveProcessorTextures;
705 } else if (fixedDynamicState) {
706 primProcProxies = fixedDynamicState->fPrimitiveProcessorTextures;
707 }
708
709 SkASSERT(SkToBool(primProcProxies) == SkToBool(primProc.numTextureSamplers()));
710
Greg Daniel09eeefb2017-10-16 15:15:02 -0400711 GrVkPipelineState* pipelineState =
Robert Phillipsd0fe8752019-01-31 14:13:59 -0500712 fGpu->resourceProvider().findOrCreateCompatiblePipelineState(fRenderTarget, fOrigin,
713 pipeline,
egdaniel9cb63402016-06-23 08:37:05 -0700714 primProc,
Greg Daniel9a51a862018-11-30 10:18:14 -0500715 primProcProxies,
egdaniel9cb63402016-06-23 08:37:05 -0700716 primitiveType,
Greg Daniel99b88e02018-10-03 15:31:20 -0400717 compatibleRenderPass);
egdaniel9cb63402016-06-23 08:37:05 -0700718 if (!pipelineState) {
719 return pipelineState;
720 }
721
Greg Daniel22bc8652017-03-22 15:45:43 -0400722 if (!cbInfo.fIsEmpty &&
Greg Daniel09eeefb2017-10-16 15:15:02 -0400723 fLastPipelineState && fLastPipelineState != pipelineState &&
Greg Daniele3cd6912017-05-17 11:15:55 -0400724 fGpu->vkCaps().newCBOnPipelineChange()) {
Greg Daniel22bc8652017-03-22 15:45:43 -0400725 this->addAdditionalCommandBuffer();
726 }
Greg Daniel09eeefb2017-10-16 15:15:02 -0400727 fLastPipelineState = pipelineState;
Greg Daniel22bc8652017-03-22 15:45:43 -0400728
Brian Salomonf7232642018-09-19 08:58:08 -0400729 pipelineState->bindPipeline(fGpu, cbInfo.currentCmdBuf());
Brian Salomoncd7907b2018-08-30 08:36:18 -0400730
Robert Phillipsd0fe8752019-01-31 14:13:59 -0500731 pipelineState->setAndBindUniforms(fGpu, fRenderTarget, fOrigin,
732 primProc, pipeline, cbInfo.currentCmdBuf());
Brian Salomonf7232642018-09-19 08:58:08 -0400733
734 // Check whether we need to bind textures between each GrMesh. If not we can bind them all now.
735 bool setTextures = !(dynamicStateArrays && dynamicStateArrays->fPrimitiveProcessorTextures);
736 if (setTextures) {
Brian Salomonf7232642018-09-19 08:58:08 -0400737 pipelineState->setAndBindTextures(fGpu, primProc, pipeline, primProcProxies,
738 cbInfo.currentCmdBuf());
739 }
egdaniel9cb63402016-06-23 08:37:05 -0700740
Brian Salomond818ebf2018-07-02 14:08:49 +0000741 if (!pipeline.isScissorEnabled()) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400742 GrVkPipeline::SetDynamicScissorRectState(fGpu, cbInfo.currentCmdBuf(),
Robert Phillipsd0fe8752019-01-31 14:13:59 -0500743 fRenderTarget, fOrigin,
744 SkIRect::MakeWH(fRenderTarget->width(),
745 fRenderTarget->height()));
Brian Salomon49348902018-06-26 09:12:38 -0400746 } else if (!dynamicStateArrays || !dynamicStateArrays->fScissorRects) {
747 SkASSERT(fixedDynamicState);
Robert Phillipsd0fe8752019-01-31 14:13:59 -0500748 GrVkPipeline::SetDynamicScissorRectState(fGpu, cbInfo.currentCmdBuf(), fRenderTarget,
749 fOrigin,
Brian Salomon49348902018-06-26 09:12:38 -0400750 fixedDynamicState->fScissorRect);
Chris Dalton46983b72017-06-06 12:27:16 -0600751 }
Robert Phillipsd0fe8752019-01-31 14:13:59 -0500752 GrVkPipeline::SetDynamicViewportState(fGpu, cbInfo.currentCmdBuf(), fRenderTarget);
753 GrVkPipeline::SetDynamicBlendConstantState(fGpu, cbInfo.currentCmdBuf(),
754 fRenderTarget->config(),
Chris Dalton46983b72017-06-06 12:27:16 -0600755 pipeline.getXferProcessor());
egdaniel9cb63402016-06-23 08:37:05 -0700756
757 return pipelineState;
758}
759
Brian Salomonff168d92018-06-23 15:17:27 -0400760void GrVkGpuRTCommandBuffer::onDraw(const GrPrimitiveProcessor& primProc,
761 const GrPipeline& pipeline,
Brian Salomon49348902018-06-26 09:12:38 -0400762 const GrPipeline::FixedDynamicState* fixedDynamicState,
763 const GrPipeline::DynamicStateArrays* dynamicStateArrays,
Greg Daniel500d58b2017-08-24 15:59:33 -0400764 const GrMesh meshes[],
Greg Daniel500d58b2017-08-24 15:59:33 -0400765 int meshCount,
766 const SkRect& bounds) {
egdaniel9cb63402016-06-23 08:37:05 -0700767 if (!meshCount) {
768 return;
769 }
Greg Danielea022cd2018-03-16 11:10:03 -0400770
771 CommandBufferInfo& cbInfo = fCommandBufferInfos[fCurrentCmdInfo];
772
Brian Salomone782f842018-07-31 13:53:11 -0400773 auto prepareSampledImage = [&](GrTexture* texture, GrSamplerState::Filter filter) {
774 GrVkTexture* vkTexture = static_cast<GrVkTexture*>(texture);
775 // We may need to resolve the texture first if it is also a render target
776 GrVkRenderTarget* texRT = static_cast<GrVkRenderTarget*>(vkTexture->asRenderTarget());
777 if (texRT) {
Greg Daniel0a77f432018-12-06 11:23:32 -0500778 fGpu->resolveRenderTargetNoFlush(texRT);
Brian Salomone782f842018-07-31 13:53:11 -0400779 }
780
781 // Check if we need to regenerate any mip maps
782 if (GrSamplerState::Filter::kMipMap == filter &&
783 (vkTexture->width() != 1 || vkTexture->height() != 1)) {
784 SkASSERT(vkTexture->texturePriv().mipMapped() == GrMipMapped::kYes);
785 if (vkTexture->texturePriv().mipMapsAreDirty()) {
786 fGpu->regenerateMipMapLevels(vkTexture);
787 }
788 }
Brian Salomon5fd10572019-04-01 12:07:05 -0400789 cbInfo.fSampledTextures.push_back(vkTexture);
Brian Salomone782f842018-07-31 13:53:11 -0400790 };
791
Brian Salomonf7232642018-09-19 08:58:08 -0400792 if (dynamicStateArrays && dynamicStateArrays->fPrimitiveProcessorTextures) {
793 for (int m = 0, i = 0; m < meshCount; ++m) {
794 for (int s = 0; s < primProc.numTextureSamplers(); ++s, ++i) {
795 auto texture = dynamicStateArrays->fPrimitiveProcessorTextures[i]->peekTexture();
796 prepareSampledImage(texture, primProc.textureSampler(s).samplerState().filter());
797 }
798 }
799 } else {
800 for (int i = 0; i < primProc.numTextureSamplers(); ++i) {
801 auto texture = fixedDynamicState->fPrimitiveProcessorTextures[i]->peekTexture();
802 prepareSampledImage(texture, primProc.textureSampler(i).samplerState().filter());
803 }
Brian Salomone782f842018-07-31 13:53:11 -0400804 }
bsalomonb58a2b42016-09-26 06:55:02 -0700805 GrFragmentProcessor::Iter iter(pipeline);
806 while (const GrFragmentProcessor* fp = iter.next()) {
Brian Salomone782f842018-07-31 13:53:11 -0400807 for (int i = 0; i < fp->numTextureSamplers(); ++i) {
808 const GrFragmentProcessor::TextureSampler& sampler = fp->textureSampler(i);
809 prepareSampledImage(sampler.peekTexture(), sampler.samplerState().filter());
810 }
egdaniel2f5792a2016-07-06 08:51:23 -0700811 }
Robert Phillipsbb581ce2017-05-29 15:05:15 -0400812 if (GrTexture* dstTexture = pipeline.peekDstTexture()) {
Chris Dalton298238a2019-02-21 16:28:44 -0500813 cbInfo.fSampledTextures.push_back(sk_ref_sp(static_cast<GrVkTexture*>(dstTexture)));
Brian Salomon18dfa982017-04-03 16:57:43 -0400814 }
egdaniel2f5792a2016-07-06 08:51:23 -0700815
Chris Daltonbca46e22017-05-15 11:03:26 -0600816 GrPrimitiveType primitiveType = meshes[0].primitiveType();
Brian Salomon49348902018-06-26 09:12:38 -0400817 GrVkPipelineState* pipelineState = this->prepareDrawState(primProc, pipeline, fixedDynamicState,
818 dynamicStateArrays, primitiveType);
egdaniel9cb63402016-06-23 08:37:05 -0700819 if (!pipelineState) {
820 return;
821 }
822
Brian Salomond818ebf2018-07-02 14:08:49 +0000823 bool dynamicScissor =
824 pipeline.isScissorEnabled() && dynamicStateArrays && dynamicStateArrays->fScissorRects;
Brian Salomonf7232642018-09-19 08:58:08 -0400825 bool dynamicTextures = dynamicStateArrays && dynamicStateArrays->fPrimitiveProcessorTextures;
Brian Salomon49348902018-06-26 09:12:38 -0400826
egdaniel9cb63402016-06-23 08:37:05 -0700827 for (int i = 0; i < meshCount; ++i) {
828 const GrMesh& mesh = meshes[i];
Chris Daltonbca46e22017-05-15 11:03:26 -0600829 if (mesh.primitiveType() != primitiveType) {
Chris Dalton6f241802017-05-08 13:58:38 -0400830 SkDEBUGCODE(pipelineState = nullptr);
Chris Daltonbca46e22017-05-15 11:03:26 -0600831 primitiveType = mesh.primitiveType();
Brian Salomon49348902018-06-26 09:12:38 -0400832 pipelineState = this->prepareDrawState(primProc, pipeline, fixedDynamicState,
833 dynamicStateArrays, primitiveType);
Chris Dalton6f241802017-05-08 13:58:38 -0400834 if (!pipelineState) {
835 return;
egdaniel9cb63402016-06-23 08:37:05 -0700836 }
Chris Dalton6f241802017-05-08 13:58:38 -0400837 }
egdaniel9cb63402016-06-23 08:37:05 -0700838
Brian Salomon49348902018-06-26 09:12:38 -0400839 if (dynamicScissor) {
840 GrVkPipeline::SetDynamicScissorRectState(fGpu, cbInfo.currentCmdBuf(), fRenderTarget,
Robert Phillipsd0fe8752019-01-31 14:13:59 -0500841 fOrigin,
Brian Salomon49348902018-06-26 09:12:38 -0400842 dynamicStateArrays->fScissorRects[i]);
Chris Dalton46983b72017-06-06 12:27:16 -0600843 }
Brian Salomonf7232642018-09-19 08:58:08 -0400844 if (dynamicTextures) {
845 GrTextureProxy* const* meshProxies = dynamicStateArrays->fPrimitiveProcessorTextures +
846 primProc.numTextureSamplers() * i;
847 pipelineState->setAndBindTextures(fGpu, primProc, pipeline, meshProxies,
848 cbInfo.currentCmdBuf());
849 }
Chris Daltonbca46e22017-05-15 11:03:26 -0600850 SkASSERT(pipelineState);
Brian Salomon802cb312018-06-08 18:05:20 -0400851 mesh.sendToGpu(this);
egdaniel9cb63402016-06-23 08:37:05 -0700852 }
853
Greg Daniel36a77ee2016-10-18 10:33:25 -0400854 cbInfo.fBounds.join(bounds);
Chris Dalton114a3c02017-05-26 15:17:19 -0600855 cbInfo.fIsEmpty = false;
egdaniel066df7c2016-06-08 14:02:27 -0700856}
857
Brian Salomon802cb312018-06-08 18:05:20 -0400858void GrVkGpuRTCommandBuffer::sendInstancedMeshToGpu(GrPrimitiveType,
Greg Daniel500d58b2017-08-24 15:59:33 -0400859 const GrBuffer* vertexBuffer,
860 int vertexCount,
861 int baseVertex,
862 const GrBuffer* instanceBuffer,
863 int instanceCount,
864 int baseInstance) {
Chris Dalton114a3c02017-05-26 15:17:19 -0600865 CommandBufferInfo& cbInfo = fCommandBufferInfos[fCurrentCmdInfo];
Brian Salomondbf70722019-02-07 11:31:24 -0500866 SkASSERT(!vertexBuffer || !vertexBuffer->isCpuBuffer());
867 SkASSERT(!instanceBuffer || !instanceBuffer->isCpuBuffer());
868 auto gpuVertexBuffer = static_cast<const GrGpuBuffer*>(vertexBuffer);
869 auto gpuInstanceBuffer = static_cast<const GrGpuBuffer*>(instanceBuffer);
870 this->bindGeometry(nullptr, gpuVertexBuffer, gpuInstanceBuffer);
Chris Dalton1d616352017-05-31 12:51:23 -0600871 cbInfo.currentCmdBuf()->draw(fGpu, vertexCount, instanceCount, baseVertex, baseInstance);
Chris Dalton114a3c02017-05-26 15:17:19 -0600872 fGpu->stats()->incNumDraws();
873}
874
Brian Salomon802cb312018-06-08 18:05:20 -0400875void GrVkGpuRTCommandBuffer::sendIndexedInstancedMeshToGpu(GrPrimitiveType,
Greg Daniel500d58b2017-08-24 15:59:33 -0400876 const GrBuffer* indexBuffer,
877 int indexCount,
878 int baseIndex,
879 const GrBuffer* vertexBuffer,
880 int baseVertex,
881 const GrBuffer* instanceBuffer,
882 int instanceCount,
Brian Salomon802cb312018-06-08 18:05:20 -0400883 int baseInstance,
884 GrPrimitiveRestart restart) {
885 SkASSERT(restart == GrPrimitiveRestart::kNo);
Chris Dalton114a3c02017-05-26 15:17:19 -0600886 CommandBufferInfo& cbInfo = fCommandBufferInfos[fCurrentCmdInfo];
Brian Salomondbf70722019-02-07 11:31:24 -0500887 SkASSERT(!vertexBuffer || !vertexBuffer->isCpuBuffer());
888 SkASSERT(!instanceBuffer || !instanceBuffer->isCpuBuffer());
889 SkASSERT(!indexBuffer->isCpuBuffer());
890 auto gpuIndexxBuffer = static_cast<const GrGpuBuffer*>(indexBuffer);
891 auto gpuVertexBuffer = static_cast<const GrGpuBuffer*>(vertexBuffer);
892 auto gpuInstanceBuffer = static_cast<const GrGpuBuffer*>(instanceBuffer);
893 this->bindGeometry(gpuIndexxBuffer, gpuVertexBuffer, gpuInstanceBuffer);
Chris Dalton1d616352017-05-31 12:51:23 -0600894 cbInfo.currentCmdBuf()->drawIndexed(fGpu, indexCount, instanceCount,
895 baseIndex, baseVertex, baseInstance);
Chris Dalton114a3c02017-05-26 15:17:19 -0600896 fGpu->stats()->incNumDraws();
897}
Greg Daniel64cc9aa2018-10-19 13:54:56 -0400898
899////////////////////////////////////////////////////////////////////////////////
900
901void GrVkGpuRTCommandBuffer::executeDrawable(std::unique_ptr<SkDrawable::GpuDrawHandler> drawable) {
902 GrVkRenderTarget* target = static_cast<GrVkRenderTarget*>(fRenderTarget);
903
904 GrVkImage* targetImage = target->msaaImage() ? target->msaaImage() : target;
905
906 CommandBufferInfo& cbInfo = fCommandBufferInfos[fCurrentCmdInfo];
907 VkRect2D bounds;
908 bounds.offset = { 0, 0 };
909 bounds.extent = { 0, 0 };
910
911 GrVkDrawableInfo vkInfo;
912 vkInfo.fSecondaryCommandBuffer = cbInfo.currentCmdBuf()->vkCommandBuffer();
913 vkInfo.fCompatibleRenderPass = cbInfo.fRenderPass->vkRenderPass();
Greg Danielb353eeb2018-12-05 11:01:58 -0500914 SkAssertResult(cbInfo.fRenderPass->colorAttachmentIndex(&vkInfo.fColorAttachmentIndex));
Greg Daniel64cc9aa2018-10-19 13:54:56 -0400915 vkInfo.fFormat = targetImage->imageFormat();
916 vkInfo.fDrawBounds = &bounds;
Stan Ilievcb580602019-02-26 11:36:07 -0500917#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
918 vkInfo.fImage = targetImage->image();
919#else
920 vkInfo.fImage = VK_NULL_HANDLE;
921#endif //SK_BUILD_FOR_ANDROID_FRAMEWORK
Greg Daniel64cc9aa2018-10-19 13:54:56 -0400922
923 GrBackendDrawableInfo info(vkInfo);
924
Eric Karlc0b2ba22019-01-22 19:40:35 -0800925 // After we draw into the command buffer via the drawable, cached state we have may be invalid.
926 cbInfo.currentCmdBuf()->invalidateState();
Eric Karla8878a12019-02-07 18:17:43 -0800927 // Also assume that the drawable produced output.
928 cbInfo.fIsEmpty = false;
Eric Karlc0b2ba22019-01-22 19:40:35 -0800929
Greg Daniel64cc9aa2018-10-19 13:54:56 -0400930 drawable->draw(info);
931 fGpu->addDrawable(std::move(drawable));
932
933 if (bounds.extent.width == 0 || bounds.extent.height == 0) {
934 cbInfo.fBounds.join(target->getBoundsRect());
935 } else {
936 cbInfo.fBounds.join(SkRect::MakeXYWH(bounds.offset.x, bounds.offset.y,
937 bounds.extent.width, bounds.extent.height));
938 }
939}
940