blob: d893d2e970197c3349d31678234eb3bf4e0b9d3e [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 Daniel46cfbc62019-06-07 11:43:30 -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 Daniel46cfbc62019-06-07 11:43:30 -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 Daniel46cfbc62019-06-07 11:43:30 -040093void GrVkGpuTextureCommandBuffer::copy(GrSurface* src, const SkIRect& srcRect,
94 const SkIPoint& dstPoint) {
Emircan Uysaler23ca4e72019-06-24 10:53:09 -040095 SkASSERT(!src->isProtected() || (fTexture->isProtected() && fGpu->protectedContext()));
Greg Daniel46cfbc62019-06-07 11:43:30 -040096 fTasks.emplace<Copy>(src, srcRect, dstPoint, false);
Greg Daniel500d58b2017-08-24 15:59:33 -040097}
98
Brian Salomonab32f652019-05-10 14:24:50 -040099void GrVkGpuTextureCommandBuffer::transferFrom(const SkIRect& srcRect, GrColorType bufferColorType,
100 GrGpuBuffer* transferBuffer, size_t offset) {
101 fTasks.emplace<TransferFrom>(srcRect, bufferColorType, transferBuffer, offset);
102}
103
Greg Daniel500d58b2017-08-24 15:59:33 -0400104void GrVkGpuTextureCommandBuffer::insertEventMarker(const char* msg) {
105 // TODO: does Vulkan have a correlate?
106}
107
108void GrVkGpuTextureCommandBuffer::submit() {
Greg Daniel46cfbc62019-06-07 11:43:30 -0400109 GrVkPrimaryCommandBufferTask::Args taskArgs{fGpu, fTexture};
Brian Salomon5d8f1cc2019-04-24 09:03:53 -0400110 for (auto& task : fTasks) {
111 task.execute(taskArgs);
Greg Daniel500d58b2017-08-24 15:59:33 -0400112 }
113}
114
Greg Daniel500d58b2017-08-24 15:59:33 -0400115////////////////////////////////////////////////////////////////////////////////
116
Robert Phillips6b47c7d2017-08-29 07:24:09 -0400117void get_vk_load_store_ops(GrLoadOp loadOpIn, GrStoreOp storeOpIn,
egdaniel066df7c2016-06-08 14:02:27 -0700118 VkAttachmentLoadOp* loadOp, VkAttachmentStoreOp* storeOp) {
Robert Phillips95214472017-08-08 18:00:03 -0400119 switch (loadOpIn) {
Robert Phillips6b47c7d2017-08-29 07:24:09 -0400120 case GrLoadOp::kLoad:
egdaniel066df7c2016-06-08 14:02:27 -0700121 *loadOp = VK_ATTACHMENT_LOAD_OP_LOAD;
egdaniel066df7c2016-06-08 14:02:27 -0700122 break;
Robert Phillips6b47c7d2017-08-29 07:24:09 -0400123 case GrLoadOp::kClear:
egdaniel9cb63402016-06-23 08:37:05 -0700124 *loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
125 break;
Robert Phillips6b47c7d2017-08-29 07:24:09 -0400126 case GrLoadOp::kDiscard:
egdaniel9cb63402016-06-23 08:37:05 -0700127 *loadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
128 break;
129 default:
130 SK_ABORT("Invalid LoadOp");
egdaniel066df7c2016-06-08 14:02:27 -0700131 *loadOp = VK_ATTACHMENT_LOAD_OP_LOAD;
egdaniel9cb63402016-06-23 08:37:05 -0700132 }
133
Robert Phillips95214472017-08-08 18:00:03 -0400134 switch (storeOpIn) {
Robert Phillips6b47c7d2017-08-29 07:24:09 -0400135 case GrStoreOp::kStore:
egdaniel066df7c2016-06-08 14:02:27 -0700136 *storeOp = VK_ATTACHMENT_STORE_OP_STORE;
137 break;
Robert Phillips6b47c7d2017-08-29 07:24:09 -0400138 case GrStoreOp::kDiscard:
egdaniel066df7c2016-06-08 14:02:27 -0700139 *storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
140 break;
brianosman0bbc3712016-06-14 04:53:09 -0700141 default:
egdaniel9cb63402016-06-23 08:37:05 -0700142 SK_ABORT("Invalid StoreOp");
brianosman0bbc3712016-06-14 04:53:09 -0700143 *storeOp = VK_ATTACHMENT_STORE_OP_STORE;
egdaniel066df7c2016-06-08 14:02:27 -0700144 }
145}
146
Brian Salomon24d377e2019-04-23 15:24:31 -0400147GrVkGpuRTCommandBuffer::GrVkGpuRTCommandBuffer(GrVkGpu* gpu) : fGpu(gpu) {}
Brian Salomonc293a292016-11-30 13:38:32 -0500148
Greg Daniel500d58b2017-08-24 15:59:33 -0400149void GrVkGpuRTCommandBuffer::init() {
Brian Salomonc293a292016-11-30 13:38:32 -0500150 GrVkRenderPass::LoadStoreOps vkColorOps(fVkColorLoadOp, fVkColorStoreOp);
151 GrVkRenderPass::LoadStoreOps vkStencilOps(fVkStencilLoadOp, fVkStencilStoreOp);
egdaniel9cb63402016-06-23 08:37:05 -0700152
Greg Daniel36a77ee2016-10-18 10:33:25 -0400153 CommandBufferInfo& cbInfo = fCommandBufferInfos.push_back();
Brian Salomonc293a292016-11-30 13:38:32 -0500154 SkASSERT(fCommandBufferInfos.count() == 1);
Greg Daniel22bc8652017-03-22 15:45:43 -0400155 fCurrentCmdInfo = 0;
Greg Daniel36a77ee2016-10-18 10:33:25 -0400156
Robert Phillips19e51dc2017-08-09 09:30:51 -0400157 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(fRenderTarget);
158 const GrVkResourceProvider::CompatibleRPHandle& rpHandle = vkRT->compatibleRenderPassHandle();
egdaniel066df7c2016-06-08 14:02:27 -0700159 if (rpHandle.isValid()) {
Greg Daniel36a77ee2016-10-18 10:33:25 -0400160 cbInfo.fRenderPass = fGpu->resourceProvider().findRenderPass(rpHandle,
161 vkColorOps,
162 vkStencilOps);
egdaniel066df7c2016-06-08 14:02:27 -0700163 } else {
Robert Phillips19e51dc2017-08-09 09:30:51 -0400164 cbInfo.fRenderPass = fGpu->resourceProvider().findRenderPass(*vkRT,
Greg Daniel36a77ee2016-10-18 10:33:25 -0400165 vkColorOps,
166 vkStencilOps);
egdaniel066df7c2016-06-08 14:02:27 -0700167 }
168
Brian Osmancb3d0872018-10-16 15:19:28 -0400169 cbInfo.fColorClearValue.color.float32[0] = fClearColor[0];
170 cbInfo.fColorClearValue.color.float32[1] = fClearColor[1];
171 cbInfo.fColorClearValue.color.float32[2] = fClearColor[2];
172 cbInfo.fColorClearValue.color.float32[3] = fClearColor[3];
egdaniel9cb63402016-06-23 08:37:05 -0700173
Robert Phillips380b90c2017-08-30 07:41:07 -0400174 if (VK_ATTACHMENT_LOAD_OP_CLEAR == fVkColorLoadOp) {
Greg Daniela41a74a2018-10-09 12:59:23 +0000175 cbInfo.fBounds = SkRect::MakeWH(vkRT->width(), vkRT->height());
Robert Phillips380b90c2017-08-30 07:41:07 -0400176 } else {
177 cbInfo.fBounds.setEmpty();
178 }
Greg Daniela3c68df2018-03-16 13:46:53 -0400179
180 if (VK_ATTACHMENT_LOAD_OP_CLEAR == fVkColorLoadOp) {
181 cbInfo.fLoadStoreState = LoadStoreState::kStartsWithClear;
182 } else if (VK_ATTACHMENT_LOAD_OP_LOAD == fVkColorLoadOp &&
183 VK_ATTACHMENT_STORE_OP_STORE == fVkColorStoreOp) {
184 cbInfo.fLoadStoreState = LoadStoreState::kLoadAndStore;
185 } else if (VK_ATTACHMENT_LOAD_OP_DONT_CARE == fVkColorLoadOp) {
186 cbInfo.fLoadStoreState = LoadStoreState::kStartsWithDiscard;
187 }
Greg Daniel36a77ee2016-10-18 10:33:25 -0400188
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500189 cbInfo.fCommandBuffers.push_back(fGpu->cmdPool()->findOrCreateSecondaryCommandBuffer(fGpu));
Robert Phillips19e51dc2017-08-09 09:30:51 -0400190 cbInfo.currentCmdBuf()->begin(fGpu, vkRT->framebuffer(), cbInfo.fRenderPass);
egdaniel066df7c2016-06-08 14:02:27 -0700191}
192
Greg Daniel070cbaf2019-01-03 17:35:54 -0500193void GrVkGpuRTCommandBuffer::initWrapped() {
194 CommandBufferInfo& cbInfo = fCommandBufferInfos.push_back();
195 SkASSERT(fCommandBufferInfos.count() == 1);
196 fCurrentCmdInfo = 0;
197
198 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(fRenderTarget);
199 SkASSERT(vkRT->wrapsSecondaryCommandBuffer());
200 cbInfo.fRenderPass = vkRT->externalRenderPass();
201 cbInfo.fRenderPass->ref();
202
203 cbInfo.fBounds.setEmpty();
204 cbInfo.fCommandBuffers.push_back(vkRT->getExternalSecondaryCommandBuffer());
205 cbInfo.fCommandBuffers[0]->ref();
206 cbInfo.currentCmdBuf()->begin(fGpu, nullptr, cbInfo.fRenderPass);
207}
Brian Salomonc293a292016-11-30 13:38:32 -0500208
Greg Daniel500d58b2017-08-24 15:59:33 -0400209GrVkGpuRTCommandBuffer::~GrVkGpuRTCommandBuffer() {
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400210 this->reset();
egdaniel066df7c2016-06-08 14:02:27 -0700211}
212
Greg Daniel500d58b2017-08-24 15:59:33 -0400213GrGpu* GrVkGpuRTCommandBuffer::gpu() { return fGpu; }
egdaniel9cb63402016-06-23 08:37:05 -0700214
Greg Daniel500d58b2017-08-24 15:59:33 -0400215void GrVkGpuRTCommandBuffer::end() {
Greg Daniel22bc8652017-03-22 15:45:43 -0400216 if (fCurrentCmdInfo >= 0) {
217 fCommandBufferInfos[fCurrentCmdInfo].currentCmdBuf()->end(fGpu);
Brian Salomonc293a292016-11-30 13:38:32 -0500218 }
egdaniel066df7c2016-06-08 14:02:27 -0700219}
220
Greg Daniel500d58b2017-08-24 15:59:33 -0400221void GrVkGpuRTCommandBuffer::submit() {
Brian Salomonc293a292016-11-30 13:38:32 -0500222 if (!fRenderTarget) {
223 return;
224 }
Robert Phillips19e51dc2017-08-09 09:30:51 -0400225
226 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(fRenderTarget);
Robert Phillips19e51dc2017-08-09 09:30:51 -0400227 GrVkImage* targetImage = vkRT->msaaImage() ? vkRT->msaaImage() : vkRT;
Greg Daniel45a44de2018-02-27 10:07:29 -0500228 GrStencilAttachment* stencil = fRenderTarget->renderTargetPriv().getStencilAttachment();
Brian Salomon24d377e2019-04-23 15:24:31 -0400229 auto currPreCmd = fPreCommandBufferTasks.begin();
egdaniel9cb63402016-06-23 08:37:05 -0700230
Greg Daniel46cfbc62019-06-07 11:43:30 -0400231 GrVkPrimaryCommandBufferTask::Args taskArgs{fGpu, fRenderTarget};
Greg Daniel36a77ee2016-10-18 10:33:25 -0400232 for (int i = 0; i < fCommandBufferInfos.count(); ++i) {
233 CommandBufferInfo& cbInfo = fCommandBufferInfos[i];
234
Brian Salomon24d377e2019-04-23 15:24:31 -0400235 for (int c = 0; c < cbInfo.fNumPreCmds; ++c, ++currPreCmd) {
Brian Salomon5d8f1cc2019-04-24 09:03:53 -0400236 currPreCmd->execute(taskArgs);
Greg Daniel77b53f62016-10-18 11:48:51 -0400237 }
238
Greg Daniel38c3d932018-03-16 14:22:30 -0400239 // TODO: Many things create a scratch texture which adds the discard immediately, but then
240 // don't draw to it right away. This causes the discard to be ignored and we get yelled at
241 // for loading uninitialized data. However, once MDB lands with reordering, the discard will
242 // get reordered with the rest of the draw commands and we can remove the discard check.
243 if (cbInfo.fIsEmpty &&
244 cbInfo.fLoadStoreState != LoadStoreState::kStartsWithClear &&
245 cbInfo.fLoadStoreState != LoadStoreState::kStartsWithDiscard) {
Greg Daniel77b53f62016-10-18 11:48:51 -0400246 // We have sumbitted no actual draw commands to the command buffer and we are not using
247 // the render pass to do a clear so there is no need to submit anything.
248 continue;
249 }
Greg Daniel38c3d932018-03-16 14:22:30 -0400250
Greg Daniel070cbaf2019-01-03 17:35:54 -0500251 // We don't want to actually submit the secondary command buffer if it is wrapped.
252 if (this->wrapsSecondaryCommandBuffer()) {
253 // If we have any sampled images set their layout now.
Chris Dalton298238a2019-02-21 16:28:44 -0500254 for (int j = 0; j < cbInfo.fSampledTextures.count(); ++j) {
255 cbInfo.fSampledTextures[j]->setImageLayout(
256 fGpu, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, VK_ACCESS_SHADER_READ_BIT,
257 VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT, false);
Greg Daniel070cbaf2019-01-03 17:35:54 -0500258 }
259
260 // There should have only been one secondary command buffer in the wrapped case so it is
261 // safe to just return here.
262 SkASSERT(fCommandBufferInfos.count() == 1);
263 return;
264 }
265
Greg Danieldbdba602018-04-20 11:52:43 -0400266 // Make sure if we only have a discard load that we execute the discard on the whole image.
267 // TODO: Once we improve our tracking of discards so that we never end up flushing a discard
268 // call with no actually ops, remove this.
269 if (cbInfo.fIsEmpty && cbInfo.fLoadStoreState == LoadStoreState::kStartsWithDiscard) {
Greg Daniela41a74a2018-10-09 12:59:23 +0000270 cbInfo.fBounds = SkRect::MakeWH(vkRT->width(), vkRT->height());
Greg Danieldbdba602018-04-20 11:52:43 -0400271 }
272
Greg Daniela41a74a2018-10-09 12:59:23 +0000273 if (cbInfo.fBounds.intersect(0, 0,
274 SkIntToScalar(fRenderTarget->width()),
275 SkIntToScalar(fRenderTarget->height()))) {
Greg Daniel38c3d932018-03-16 14:22:30 -0400276 // Make sure we do the following layout changes after all copies, uploads, or any other
277 // pre-work is done since we may change the layouts in the pre-work. Also since the
278 // draws will be submitted in different render passes, we need to guard againts write
279 // and write issues.
280
281 // Change layout of our render target so it can be used as the color attachment.
Greg Danielf7828d02018-10-09 12:01:32 -0400282 // TODO: If we know that we will never be blending or loading the attachment we could
283 // drop the VK_ACCESS_COLOR_ATTACHMENT_READ_BIT.
Greg Daniel38c3d932018-03-16 14:22:30 -0400284 targetImage->setImageLayout(fGpu,
285 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Greg Danielf7828d02018-10-09 12:01:32 -0400286 VK_ACCESS_COLOR_ATTACHMENT_READ_BIT |
Greg Daniel38c3d932018-03-16 14:22:30 -0400287 VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
Greg Danielf7828d02018-10-09 12:01:32 -0400288 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
Greg Daniel38c3d932018-03-16 14:22:30 -0400289 false);
290
291 // If we are using a stencil attachment we also need to update its layout
292 if (stencil) {
293 GrVkStencilAttachment* vkStencil = (GrVkStencilAttachment*)stencil;
Greg Danielf7828d02018-10-09 12:01:32 -0400294 // We need the write and read access bits since we may load and store the stencil.
295 // The initial load happens in the VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT so we
296 // wait there.
Greg Daniel38c3d932018-03-16 14:22:30 -0400297 vkStencil->setImageLayout(fGpu,
298 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
299 VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT |
300 VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT,
Greg Danielf7828d02018-10-09 12:01:32 -0400301 VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT,
Greg Daniel38c3d932018-03-16 14:22:30 -0400302 false);
303 }
304
305 // If we have any sampled images set their layout now.
Chris Dalton298238a2019-02-21 16:28:44 -0500306 for (int j = 0; j < cbInfo.fSampledTextures.count(); ++j) {
307 cbInfo.fSampledTextures[j]->setImageLayout(
308 fGpu, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, VK_ACCESS_SHADER_READ_BIT,
309 VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT, false);
Greg Daniel38c3d932018-03-16 14:22:30 -0400310 }
311
Greg Daniel36a77ee2016-10-18 10:33:25 -0400312 SkIRect iBounds;
313 cbInfo.fBounds.roundOut(&iBounds);
314
Greg Daniel22bc8652017-03-22 15:45:43 -0400315 fGpu->submitSecondaryCommandBuffer(cbInfo.fCommandBuffers, cbInfo.fRenderPass,
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400316 &cbInfo.fColorClearValue, vkRT, fOrigin, iBounds);
Greg Daniel36a77ee2016-10-18 10:33:25 -0400317 }
318 }
Brian Salomon24d377e2019-04-23 15:24:31 -0400319 SkASSERT(currPreCmd == fPreCommandBufferTasks.end());
egdaniel9cb63402016-06-23 08:37:05 -0700320}
321
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400322void GrVkGpuRTCommandBuffer::set(GrRenderTarget* rt, GrSurfaceOrigin origin,
323 const GrGpuRTCommandBuffer::LoadAndStoreInfo& colorInfo,
324 const GrGpuRTCommandBuffer::StencilLoadAndStoreInfo& stencilInfo) {
325 SkASSERT(!fRenderTarget);
326 SkASSERT(fCommandBufferInfos.empty());
327 SkASSERT(-1 == fCurrentCmdInfo);
Robert Phillips9da87e02019-02-04 13:26:26 -0500328 SkASSERT(fGpu == rt->getContext()->priv().getGpu());
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400329 SkASSERT(!fLastPipelineState);
330
Greg Danielb0c7ad12019-06-06 17:23:35 +0000331#ifdef SK_DEBUG
332 fIsActive = true;
333#endif
334
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400335 this->INHERITED::set(rt, origin);
336
Greg Daniel070cbaf2019-01-03 17:35:54 -0500337 if (this->wrapsSecondaryCommandBuffer()) {
338 this->initWrapped();
339 return;
340 }
341
Brian Osman9a9baae2018-11-05 15:06:26 -0500342 fClearColor = colorInfo.fClearColor;
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400343
344 get_vk_load_store_ops(colorInfo.fLoadOp, colorInfo.fStoreOp,
345 &fVkColorLoadOp, &fVkColorStoreOp);
346
347 get_vk_load_store_ops(stencilInfo.fLoadOp, stencilInfo.fStoreOp,
348 &fVkStencilLoadOp, &fVkStencilStoreOp);
349
350 this->init();
351}
352
353void GrVkGpuRTCommandBuffer::reset() {
354 for (int i = 0; i < fCommandBufferInfos.count(); ++i) {
355 CommandBufferInfo& cbInfo = fCommandBufferInfos[i];
356 for (int j = 0; j < cbInfo.fCommandBuffers.count(); ++j) {
357 cbInfo.fCommandBuffers[j]->unref(fGpu);
358 }
359 cbInfo.fRenderPass->unref(fGpu);
360 }
361 fCommandBufferInfos.reset();
Brian Salomon24d377e2019-04-23 15:24:31 -0400362 fPreCommandBufferTasks.reset();
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400363
364 fCurrentCmdInfo = -1;
365
366 fLastPipelineState = nullptr;
367 fRenderTarget = nullptr;
Greg Danielb0c7ad12019-06-06 17:23:35 +0000368
369#ifdef SK_DEBUG
370 fIsActive = false;
371#endif
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400372}
373
Greg Daniel070cbaf2019-01-03 17:35:54 -0500374bool GrVkGpuRTCommandBuffer::wrapsSecondaryCommandBuffer() const {
375 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(fRenderTarget);
376 return vkRT->wrapsSecondaryCommandBuffer();
377}
378
Greg Daniel64cc9aa2018-10-19 13:54:56 -0400379////////////////////////////////////////////////////////////////////////////////
380
Greg Daniel500d58b2017-08-24 15:59:33 -0400381void GrVkGpuRTCommandBuffer::discard() {
Robert Phillips19e51dc2017-08-09 09:30:51 -0400382 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(fRenderTarget);
Brian Salomonc293a292016-11-30 13:38:32 -0500383
Greg Daniel22bc8652017-03-22 15:45:43 -0400384 CommandBufferInfo& cbInfo = fCommandBufferInfos[fCurrentCmdInfo];
Greg Daniel77b53f62016-10-18 11:48:51 -0400385 if (cbInfo.fIsEmpty) {
Robert Phillips74c627f2017-08-09 10:28:00 -0400386 // Change the render pass to do a don't-care load for both color & stencil
egdaniel37535c92016-06-30 08:23:30 -0700387 GrVkRenderPass::LoadStoreOps vkColorOps(VK_ATTACHMENT_LOAD_OP_DONT_CARE,
388 VK_ATTACHMENT_STORE_OP_STORE);
389 GrVkRenderPass::LoadStoreOps vkStencilOps(VK_ATTACHMENT_LOAD_OP_DONT_CARE,
390 VK_ATTACHMENT_STORE_OP_STORE);
egdaniel37535c92016-06-30 08:23:30 -0700391
Greg Daniel36a77ee2016-10-18 10:33:25 -0400392 const GrVkRenderPass* oldRP = cbInfo.fRenderPass;
egdaniel37535c92016-06-30 08:23:30 -0700393
egdaniel37535c92016-06-30 08:23:30 -0700394 const GrVkResourceProvider::CompatibleRPHandle& rpHandle =
Robert Phillips19e51dc2017-08-09 09:30:51 -0400395 vkRT->compatibleRenderPassHandle();
egdaniel37535c92016-06-30 08:23:30 -0700396 if (rpHandle.isValid()) {
Greg Daniel36a77ee2016-10-18 10:33:25 -0400397 cbInfo.fRenderPass = fGpu->resourceProvider().findRenderPass(rpHandle,
398 vkColorOps,
399 vkStencilOps);
egdaniel37535c92016-06-30 08:23:30 -0700400 } else {
Robert Phillips19e51dc2017-08-09 09:30:51 -0400401 cbInfo.fRenderPass = fGpu->resourceProvider().findRenderPass(*vkRT,
Greg Daniel36a77ee2016-10-18 10:33:25 -0400402 vkColorOps,
403 vkStencilOps);
egdaniel37535c92016-06-30 08:23:30 -0700404 }
405
Greg Daniel36a77ee2016-10-18 10:33:25 -0400406 SkASSERT(cbInfo.fRenderPass->isCompatible(*oldRP));
egdaniel37535c92016-06-30 08:23:30 -0700407 oldRP->unref(fGpu);
Greg Daniel5011f852016-10-28 15:07:16 -0400408 cbInfo.fBounds.join(fRenderTarget->getBoundsRect());
Greg Daniela3c68df2018-03-16 13:46:53 -0400409 cbInfo.fLoadStoreState = LoadStoreState::kStartsWithDiscard;
egdaniel37535c92016-06-30 08:23:30 -0700410 }
411}
412
Greg Daniel500d58b2017-08-24 15:59:33 -0400413void GrVkGpuRTCommandBuffer::insertEventMarker(const char* msg) {
Robert Phillips65a88fa2017-08-08 08:36:22 -0400414 // TODO: does Vulkan have a correlate?
415}
416
Greg Daniel500d58b2017-08-24 15:59:33 -0400417void GrVkGpuRTCommandBuffer::onClearStencilClip(const GrFixedClip& clip, bool insideStencilMask) {
Chris Dalton94c04682017-11-01 17:15:06 -0600418 SkASSERT(!clip.hasWindowRectangles());
419
Jim Van Verth6a40abc2017-11-02 16:56:09 +0000420 CommandBufferInfo& cbInfo = fCommandBufferInfos[fCurrentCmdInfo];
421
Greg Daniel65a09272016-10-12 09:47:22 -0400422 GrStencilAttachment* sb = fRenderTarget->renderTargetPriv().getStencilAttachment();
egdaniel9cb63402016-06-23 08:37:05 -0700423 // this should only be called internally when we know we have a
424 // stencil buffer.
425 SkASSERT(sb);
426 int stencilBitCount = sb->bits();
427
428 // The contract with the callers does not guarantee that we preserve all bits in the stencil
429 // during this clear. Thus we will clear the entire stencil to the desired value.
430
431 VkClearDepthStencilValue vkStencilColor;
432 memset(&vkStencilColor, 0, sizeof(VkClearDepthStencilValue));
csmartdalton29df7602016-08-31 11:55:52 -0700433 if (insideStencilMask) {
egdaniel9cb63402016-06-23 08:37:05 -0700434 vkStencilColor.stencil = (1 << (stencilBitCount - 1));
435 } else {
436 vkStencilColor.stencil = 0;
437 }
438
439 VkClearRect clearRect;
440 // Flip rect if necessary
csmartdalton29df7602016-08-31 11:55:52 -0700441 SkIRect vkRect;
Brian Salomond818ebf2018-07-02 14:08:49 +0000442 if (!clip.scissorEnabled()) {
Greg Daniela41a74a2018-10-09 12:59:23 +0000443 vkRect.setXYWH(0, 0, fRenderTarget->width(), fRenderTarget->height());
Robert Phillips4f101a72017-07-28 08:42:04 -0400444 } else if (kBottomLeft_GrSurfaceOrigin != fOrigin) {
csmartdalton29df7602016-08-31 11:55:52 -0700445 vkRect = clip.scissorRect();
446 } else {
447 const SkIRect& scissor = clip.scissorRect();
Greg Daniel65a09272016-10-12 09:47:22 -0400448 vkRect.setLTRB(scissor.fLeft, fRenderTarget->height() - scissor.fBottom,
449 scissor.fRight, fRenderTarget->height() - scissor.fTop);
egdaniel9cb63402016-06-23 08:37:05 -0700450 }
451
452 clearRect.rect.offset = { vkRect.fLeft, vkRect.fTop };
453 clearRect.rect.extent = { (uint32_t)vkRect.width(), (uint32_t)vkRect.height() };
454
455 clearRect.baseArrayLayer = 0;
456 clearRect.layerCount = 1;
457
458 uint32_t stencilIndex;
Greg Daniel36a77ee2016-10-18 10:33:25 -0400459 SkAssertResult(cbInfo.fRenderPass->stencilAttachmentIndex(&stencilIndex));
egdaniel9cb63402016-06-23 08:37:05 -0700460
461 VkClearAttachment attachment;
462 attachment.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
463 attachment.colorAttachment = 0; // this value shouldn't matter
464 attachment.clearValue.depthStencil = vkStencilColor;
465
Greg Daniel22bc8652017-03-22 15:45:43 -0400466 cbInfo.currentCmdBuf()->clearAttachments(fGpu, 1, &attachment, 1, &clearRect);
Greg Daniel77b53f62016-10-18 11:48:51 -0400467 cbInfo.fIsEmpty = false;
Greg Daniel36a77ee2016-10-18 10:33:25 -0400468
469 // Update command buffer bounds
Brian Salomond818ebf2018-07-02 14:08:49 +0000470 if (!clip.scissorEnabled()) {
Greg Daniel36a77ee2016-10-18 10:33:25 -0400471 cbInfo.fBounds.join(fRenderTarget->getBoundsRect());
472 } else {
473 cbInfo.fBounds.join(SkRect::Make(clip.scissorRect()));
474 }
egdaniel9cb63402016-06-23 08:37:05 -0700475}
476
Brian Osman9a9baae2018-11-05 15:06:26 -0500477void GrVkGpuRTCommandBuffer::onClear(const GrFixedClip& clip, const SkPMColor4f& color) {
Robert Phillips19e51dc2017-08-09 09:30:51 -0400478 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(fRenderTarget);
479
Jim Van Verth6a40abc2017-11-02 16:56:09 +0000480 // parent class should never let us get here with no RT
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700481 SkASSERT(!clip.hasWindowRectangles());
egdaniel9cb63402016-06-23 08:37:05 -0700482
Greg Daniel22bc8652017-03-22 15:45:43 -0400483 CommandBufferInfo& cbInfo = fCommandBufferInfos[fCurrentCmdInfo];
Greg Daniel36a77ee2016-10-18 10:33:25 -0400484
Brian Osman9a9baae2018-11-05 15:06:26 -0500485 VkClearColorValue vkColor = {{color.fR, color.fG, color.fB, color.fA}};
egdaniel9cb63402016-06-23 08:37:05 -0700486
Brian Salomond818ebf2018-07-02 14:08:49 +0000487 if (cbInfo.fIsEmpty && !clip.scissorEnabled()) {
Robert Phillips74c627f2017-08-09 10:28:00 -0400488 // Change the render pass to do a clear load
egdaniel9cb63402016-06-23 08:37:05 -0700489 GrVkRenderPass::LoadStoreOps vkColorOps(VK_ATTACHMENT_LOAD_OP_CLEAR,
490 VK_ATTACHMENT_STORE_OP_STORE);
Robert Phillips74c627f2017-08-09 10:28:00 -0400491 // Preserve the stencil buffer's load & store settings
492 GrVkRenderPass::LoadStoreOps vkStencilOps(fVkStencilLoadOp, fVkStencilStoreOp);
egdaniel9cb63402016-06-23 08:37:05 -0700493
Greg Daniel36a77ee2016-10-18 10:33:25 -0400494 const GrVkRenderPass* oldRP = cbInfo.fRenderPass;
egdaniel9cb63402016-06-23 08:37:05 -0700495
496 const GrVkResourceProvider::CompatibleRPHandle& rpHandle =
Robert Phillips19e51dc2017-08-09 09:30:51 -0400497 vkRT->compatibleRenderPassHandle();
egdaniel9cb63402016-06-23 08:37:05 -0700498 if (rpHandle.isValid()) {
Greg Daniel36a77ee2016-10-18 10:33:25 -0400499 cbInfo.fRenderPass = fGpu->resourceProvider().findRenderPass(rpHandle,
500 vkColorOps,
501 vkStencilOps);
egdaniel9cb63402016-06-23 08:37:05 -0700502 } else {
Robert Phillips19e51dc2017-08-09 09:30:51 -0400503 cbInfo.fRenderPass = fGpu->resourceProvider().findRenderPass(*vkRT,
Greg Daniel36a77ee2016-10-18 10:33:25 -0400504 vkColorOps,
505 vkStencilOps);
egdaniel9cb63402016-06-23 08:37:05 -0700506 }
507
Greg Daniel36a77ee2016-10-18 10:33:25 -0400508 SkASSERT(cbInfo.fRenderPass->isCompatible(*oldRP));
egdaniel9cb63402016-06-23 08:37:05 -0700509 oldRP->unref(fGpu);
510
Brian Osman9a9baae2018-11-05 15:06:26 -0500511 cbInfo.fColorClearValue.color = {{color.fR, color.fG, color.fB, color.fA}};
Greg Daniela3c68df2018-03-16 13:46:53 -0400512 cbInfo.fLoadStoreState = LoadStoreState::kStartsWithClear;
Greg Daniel36a77ee2016-10-18 10:33:25 -0400513 // Update command buffer bounds
514 cbInfo.fBounds.join(fRenderTarget->getBoundsRect());
egdaniel9cb63402016-06-23 08:37:05 -0700515 return;
516 }
517
518 // We always do a sub rect clear with clearAttachments since we are inside a render pass
519 VkClearRect clearRect;
520 // Flip rect if necessary
csmartdalton29df7602016-08-31 11:55:52 -0700521 SkIRect vkRect;
Brian Salomond818ebf2018-07-02 14:08:49 +0000522 if (!clip.scissorEnabled()) {
Greg Daniela41a74a2018-10-09 12:59:23 +0000523 vkRect.setXYWH(0, 0, fRenderTarget->width(), fRenderTarget->height());
Robert Phillips4f101a72017-07-28 08:42:04 -0400524 } else if (kBottomLeft_GrSurfaceOrigin != fOrigin) {
csmartdalton29df7602016-08-31 11:55:52 -0700525 vkRect = clip.scissorRect();
526 } else {
527 const SkIRect& scissor = clip.scissorRect();
Greg Daniel65a09272016-10-12 09:47:22 -0400528 vkRect.setLTRB(scissor.fLeft, fRenderTarget->height() - scissor.fBottom,
529 scissor.fRight, fRenderTarget->height() - scissor.fTop);
egdaniel9cb63402016-06-23 08:37:05 -0700530 }
531 clearRect.rect.offset = { vkRect.fLeft, vkRect.fTop };
532 clearRect.rect.extent = { (uint32_t)vkRect.width(), (uint32_t)vkRect.height() };
533 clearRect.baseArrayLayer = 0;
534 clearRect.layerCount = 1;
535
536 uint32_t colorIndex;
Greg Daniel36a77ee2016-10-18 10:33:25 -0400537 SkAssertResult(cbInfo.fRenderPass->colorAttachmentIndex(&colorIndex));
egdaniel9cb63402016-06-23 08:37:05 -0700538
539 VkClearAttachment attachment;
540 attachment.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
541 attachment.colorAttachment = colorIndex;
542 attachment.clearValue.color = vkColor;
543
Greg Daniel22bc8652017-03-22 15:45:43 -0400544 cbInfo.currentCmdBuf()->clearAttachments(fGpu, 1, &attachment, 1, &clearRect);
Greg Daniel77b53f62016-10-18 11:48:51 -0400545 cbInfo.fIsEmpty = false;
Greg Daniel36a77ee2016-10-18 10:33:25 -0400546
547 // Update command buffer bounds
Brian Salomond818ebf2018-07-02 14:08:49 +0000548 if (!clip.scissorEnabled()) {
Greg Daniel36a77ee2016-10-18 10:33:25 -0400549 cbInfo.fBounds.join(fRenderTarget->getBoundsRect());
550 } else {
551 cbInfo.fBounds.join(SkRect::Make(clip.scissorRect()));
552 }
egdaniel9cb63402016-06-23 08:37:05 -0700553 return;
554}
555
Greg Daniel500d58b2017-08-24 15:59:33 -0400556////////////////////////////////////////////////////////////////////////////////
557
558void GrVkGpuRTCommandBuffer::addAdditionalCommandBuffer() {
Robert Phillips19e51dc2017-08-09 09:30:51 -0400559 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(fRenderTarget);
560
Greg Daniel22bc8652017-03-22 15:45:43 -0400561 CommandBufferInfo& cbInfo = fCommandBufferInfos[fCurrentCmdInfo];
562 cbInfo.currentCmdBuf()->end(fGpu);
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500563 cbInfo.fCommandBuffers.push_back(fGpu->cmdPool()->findOrCreateSecondaryCommandBuffer(fGpu));
Robert Phillips19e51dc2017-08-09 09:30:51 -0400564 cbInfo.currentCmdBuf()->begin(fGpu, vkRT->framebuffer(), cbInfo.fRenderPass);
Greg Daniel22bc8652017-03-22 15:45:43 -0400565}
566
Greg Daniel500d58b2017-08-24 15:59:33 -0400567void GrVkGpuRTCommandBuffer::addAdditionalRenderPass() {
Robert Phillips19e51dc2017-08-09 09:30:51 -0400568 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(fRenderTarget);
569
Greg Daniel22bc8652017-03-22 15:45:43 -0400570 fCommandBufferInfos[fCurrentCmdInfo].currentCmdBuf()->end(fGpu);
Greg Daniel77b53f62016-10-18 11:48:51 -0400571
572 CommandBufferInfo& cbInfo = fCommandBufferInfos.push_back();
Greg Daniel22bc8652017-03-22 15:45:43 -0400573 fCurrentCmdInfo++;
Greg Daniel77b53f62016-10-18 11:48:51 -0400574
575 GrVkRenderPass::LoadStoreOps vkColorOps(VK_ATTACHMENT_LOAD_OP_LOAD,
576 VK_ATTACHMENT_STORE_OP_STORE);
577 GrVkRenderPass::LoadStoreOps vkStencilOps(VK_ATTACHMENT_LOAD_OP_LOAD,
578 VK_ATTACHMENT_STORE_OP_STORE);
579
580 const GrVkResourceProvider::CompatibleRPHandle& rpHandle =
Robert Phillips19e51dc2017-08-09 09:30:51 -0400581 vkRT->compatibleRenderPassHandle();
Greg Daniel77b53f62016-10-18 11:48:51 -0400582 if (rpHandle.isValid()) {
583 cbInfo.fRenderPass = fGpu->resourceProvider().findRenderPass(rpHandle,
584 vkColorOps,
585 vkStencilOps);
586 } else {
Robert Phillips19e51dc2017-08-09 09:30:51 -0400587 cbInfo.fRenderPass = fGpu->resourceProvider().findRenderPass(*vkRT,
Greg Daniel77b53f62016-10-18 11:48:51 -0400588 vkColorOps,
589 vkStencilOps);
590 }
Greg Daniela3c68df2018-03-16 13:46:53 -0400591 cbInfo.fLoadStoreState = LoadStoreState::kLoadAndStore;
Greg Daniel77b53f62016-10-18 11:48:51 -0400592
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500593 cbInfo.fCommandBuffers.push_back(fGpu->cmdPool()->findOrCreateSecondaryCommandBuffer(fGpu));
Greg Daniel77b53f62016-10-18 11:48:51 -0400594 // It shouldn't matter what we set the clear color to here since we will assume loading of the
595 // attachment.
596 memset(&cbInfo.fColorClearValue, 0, sizeof(VkClearValue));
597 cbInfo.fBounds.setEmpty();
Greg Daniel77b53f62016-10-18 11:48:51 -0400598
Robert Phillips19e51dc2017-08-09 09:30:51 -0400599 cbInfo.currentCmdBuf()->begin(fGpu, vkRT->framebuffer(), cbInfo.fRenderPass);
Greg Daniel77b53f62016-10-18 11:48:51 -0400600}
601
Brian Salomon943ed792017-10-30 09:37:55 -0400602void GrVkGpuRTCommandBuffer::inlineUpload(GrOpFlushState* state,
603 GrDeferredTextureUploadFn& upload) {
Greg Daniel22bc8652017-03-22 15:45:43 -0400604 if (!fCommandBufferInfos[fCurrentCmdInfo].fIsEmpty) {
605 this->addAdditionalRenderPass();
Greg Daniel77b53f62016-10-18 11:48:51 -0400606 }
Brian Salomon24d377e2019-04-23 15:24:31 -0400607
Brian Salomon24d377e2019-04-23 15:24:31 -0400608 fPreCommandBufferTasks.emplace<InlineUpload>(state, upload);
609 ++fCommandBufferInfos[fCurrentCmdInfo].fNumPreCmds;
Greg Daniel77b53f62016-10-18 11:48:51 -0400610}
611
Greg Daniel46cfbc62019-06-07 11:43:30 -0400612void GrVkGpuRTCommandBuffer::copy(GrSurface* src, const SkIRect& srcRect,
Greg Daniel500d58b2017-08-24 15:59:33 -0400613 const SkIPoint& dstPoint) {
Greg Daniela3c68df2018-03-16 13:46:53 -0400614 CommandBufferInfo& cbInfo = fCommandBufferInfos[fCurrentCmdInfo];
615 if (!cbInfo.fIsEmpty || LoadStoreState::kStartsWithClear == cbInfo.fLoadStoreState) {
Greg Daniel500d58b2017-08-24 15:59:33 -0400616 this->addAdditionalRenderPass();
617 }
Greg Daniela3c68df2018-03-16 13:46:53 -0400618
Brian Salomon24d377e2019-04-23 15:24:31 -0400619 fPreCommandBufferTasks.emplace<Copy>(
Greg Daniel46cfbc62019-06-07 11:43:30 -0400620 src, srcRect, dstPoint, LoadStoreState::kStartsWithDiscard == cbInfo.fLoadStoreState);
Brian Salomon24d377e2019-04-23 15:24:31 -0400621 ++fCommandBufferInfos[fCurrentCmdInfo].fNumPreCmds;
Greg Daniel55fa6472018-03-16 16:13:10 -0400622
Greg Daniela3c68df2018-03-16 13:46:53 -0400623 if (LoadStoreState::kLoadAndStore != cbInfo.fLoadStoreState) {
624 // Change the render pass to do a load and store so we don't lose the results of our copy
625 GrVkRenderPass::LoadStoreOps vkColorOps(VK_ATTACHMENT_LOAD_OP_LOAD,
626 VK_ATTACHMENT_STORE_OP_STORE);
627 GrVkRenderPass::LoadStoreOps vkStencilOps(VK_ATTACHMENT_LOAD_OP_LOAD,
628 VK_ATTACHMENT_STORE_OP_STORE);
629
630 const GrVkRenderPass* oldRP = cbInfo.fRenderPass;
631
632 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(fRenderTarget);
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400633 SkASSERT(!src->isProtected() || (fRenderTarget->isProtected() && fGpu->protectedContext()));
Greg Daniela3c68df2018-03-16 13:46:53 -0400634 const GrVkResourceProvider::CompatibleRPHandle& rpHandle =
635 vkRT->compatibleRenderPassHandle();
636 if (rpHandle.isValid()) {
637 cbInfo.fRenderPass = fGpu->resourceProvider().findRenderPass(rpHandle,
638 vkColorOps,
639 vkStencilOps);
640 } else {
641 cbInfo.fRenderPass = fGpu->resourceProvider().findRenderPass(*vkRT,
642 vkColorOps,
643 vkStencilOps);
644 }
645 SkASSERT(cbInfo.fRenderPass->isCompatible(*oldRP));
646 oldRP->unref(fGpu);
647
648 cbInfo.fLoadStoreState = LoadStoreState::kLoadAndStore;
649
650 }
Greg Daniel500d58b2017-08-24 15:59:33 -0400651}
652
Brian Salomonab32f652019-05-10 14:24:50 -0400653void GrVkGpuRTCommandBuffer::transferFrom(const SkIRect& srcRect, GrColorType bufferColorType,
654 GrGpuBuffer* transferBuffer, size_t offset) {
655 CommandBufferInfo& cbInfo = fCommandBufferInfos[fCurrentCmdInfo];
656 if (!cbInfo.fIsEmpty) {
657 this->addAdditionalRenderPass();
658 }
659 fPreCommandBufferTasks.emplace<TransferFrom>(srcRect, bufferColorType, transferBuffer, offset);
660 ++fCommandBufferInfos[fCurrentCmdInfo].fNumPreCmds;
661}
662
egdaniel9cb63402016-06-23 08:37:05 -0700663////////////////////////////////////////////////////////////////////////////////
664
Brian Salomondbf70722019-02-07 11:31:24 -0500665void GrVkGpuRTCommandBuffer::bindGeometry(const GrGpuBuffer* indexBuffer,
666 const GrGpuBuffer* vertexBuffer,
667 const GrGpuBuffer* instanceBuffer) {
Chris Daltonff926502017-05-03 14:36:54 -0400668 GrVkSecondaryCommandBuffer* currCmdBuf = fCommandBufferInfos[fCurrentCmdInfo].currentCmdBuf();
egdaniel9cb63402016-06-23 08:37:05 -0700669 // There is no need to put any memory barriers to make sure host writes have finished here.
670 // When a command buffer is submitted to a queue, there is an implicit memory barrier that
671 // occurs for all host writes. Additionally, BufferMemoryBarriers are not allowed inside of
672 // an active RenderPass.
egdaniel9cb63402016-06-23 08:37:05 -0700673
Chris Dalton1d616352017-05-31 12:51:23 -0600674 // Here our vertex and instance inputs need to match the same 0-based bindings they were
675 // assigned in GrVkPipeline. That is, vertex first (if any) followed by instance.
676 uint32_t binding = 0;
677
Brian Salomon802cb312018-06-08 18:05:20 -0400678 if (vertexBuffer) {
Chris Dalton1d616352017-05-31 12:51:23 -0600679 SkASSERT(vertexBuffer);
Chris Dalton1d616352017-05-31 12:51:23 -0600680 SkASSERT(!vertexBuffer->isMapped());
681
682 currCmdBuf->bindInputBuffer(fGpu, binding++,
683 static_cast<const GrVkVertexBuffer*>(vertexBuffer));
684 }
685
Brian Salomon802cb312018-06-08 18:05:20 -0400686 if (instanceBuffer) {
Chris Dalton1d616352017-05-31 12:51:23 -0600687 SkASSERT(instanceBuffer);
Chris Dalton1d616352017-05-31 12:51:23 -0600688 SkASSERT(!instanceBuffer->isMapped());
689
690 currCmdBuf->bindInputBuffer(fGpu, binding++,
691 static_cast<const GrVkVertexBuffer*>(instanceBuffer));
692 }
Chris Daltonff926502017-05-03 14:36:54 -0400693 if (indexBuffer) {
694 SkASSERT(indexBuffer);
695 SkASSERT(!indexBuffer->isMapped());
egdaniel9cb63402016-06-23 08:37:05 -0700696
Chris Daltonff926502017-05-03 14:36:54 -0400697 currCmdBuf->bindIndexBuffer(fGpu, static_cast<const GrVkIndexBuffer*>(indexBuffer));
egdaniel9cb63402016-06-23 08:37:05 -0700698 }
699}
700
Brian Salomon49348902018-06-26 09:12:38 -0400701GrVkPipelineState* GrVkGpuRTCommandBuffer::prepareDrawState(
702 const GrPrimitiveProcessor& primProc,
703 const GrPipeline& pipeline,
704 const GrPipeline::FixedDynamicState* fixedDynamicState,
705 const GrPipeline::DynamicStateArrays* dynamicStateArrays,
706 GrPrimitiveType primitiveType) {
Greg Daniel22bc8652017-03-22 15:45:43 -0400707 CommandBufferInfo& cbInfo = fCommandBufferInfos[fCurrentCmdInfo];
708 SkASSERT(cbInfo.fRenderPass);
Greg Daniel36a77ee2016-10-18 10:33:25 -0400709
Greg Daniel99b88e02018-10-03 15:31:20 -0400710 VkRenderPass compatibleRenderPass = cbInfo.fRenderPass->vkRenderPass();
711
Greg Daniel9a51a862018-11-30 10:18:14 -0500712 const GrTextureProxy* const* primProcProxies = nullptr;
713 if (dynamicStateArrays && dynamicStateArrays->fPrimitiveProcessorTextures) {
714 primProcProxies = dynamicStateArrays->fPrimitiveProcessorTextures;
715 } else if (fixedDynamicState) {
716 primProcProxies = fixedDynamicState->fPrimitiveProcessorTextures;
717 }
718
719 SkASSERT(SkToBool(primProcProxies) == SkToBool(primProc.numTextureSamplers()));
720
Greg Daniel09eeefb2017-10-16 15:15:02 -0400721 GrVkPipelineState* pipelineState =
Robert Phillipsd0fe8752019-01-31 14:13:59 -0500722 fGpu->resourceProvider().findOrCreateCompatiblePipelineState(fRenderTarget, fOrigin,
723 pipeline,
egdaniel9cb63402016-06-23 08:37:05 -0700724 primProc,
Greg Daniel9a51a862018-11-30 10:18:14 -0500725 primProcProxies,
egdaniel9cb63402016-06-23 08:37:05 -0700726 primitiveType,
Greg Daniel99b88e02018-10-03 15:31:20 -0400727 compatibleRenderPass);
egdaniel9cb63402016-06-23 08:37:05 -0700728 if (!pipelineState) {
729 return pipelineState;
730 }
731
Greg Daniel22bc8652017-03-22 15:45:43 -0400732 if (!cbInfo.fIsEmpty &&
Greg Daniel09eeefb2017-10-16 15:15:02 -0400733 fLastPipelineState && fLastPipelineState != pipelineState &&
Greg Daniele3cd6912017-05-17 11:15:55 -0400734 fGpu->vkCaps().newCBOnPipelineChange()) {
Greg Daniel22bc8652017-03-22 15:45:43 -0400735 this->addAdditionalCommandBuffer();
736 }
Greg Daniel09eeefb2017-10-16 15:15:02 -0400737 fLastPipelineState = pipelineState;
Greg Daniel22bc8652017-03-22 15:45:43 -0400738
Brian Salomonf7232642018-09-19 08:58:08 -0400739 pipelineState->bindPipeline(fGpu, cbInfo.currentCmdBuf());
Brian Salomoncd7907b2018-08-30 08:36:18 -0400740
Robert Phillipsd0fe8752019-01-31 14:13:59 -0500741 pipelineState->setAndBindUniforms(fGpu, fRenderTarget, fOrigin,
742 primProc, pipeline, cbInfo.currentCmdBuf());
Brian Salomonf7232642018-09-19 08:58:08 -0400743
744 // Check whether we need to bind textures between each GrMesh. If not we can bind them all now.
745 bool setTextures = !(dynamicStateArrays && dynamicStateArrays->fPrimitiveProcessorTextures);
746 if (setTextures) {
Brian Salomonf7232642018-09-19 08:58:08 -0400747 pipelineState->setAndBindTextures(fGpu, primProc, pipeline, primProcProxies,
748 cbInfo.currentCmdBuf());
749 }
egdaniel9cb63402016-06-23 08:37:05 -0700750
Brian Salomond818ebf2018-07-02 14:08:49 +0000751 if (!pipeline.isScissorEnabled()) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400752 GrVkPipeline::SetDynamicScissorRectState(fGpu, cbInfo.currentCmdBuf(),
Robert Phillipsd0fe8752019-01-31 14:13:59 -0500753 fRenderTarget, fOrigin,
754 SkIRect::MakeWH(fRenderTarget->width(),
755 fRenderTarget->height()));
Brian Salomon49348902018-06-26 09:12:38 -0400756 } else if (!dynamicStateArrays || !dynamicStateArrays->fScissorRects) {
757 SkASSERT(fixedDynamicState);
Robert Phillipsd0fe8752019-01-31 14:13:59 -0500758 GrVkPipeline::SetDynamicScissorRectState(fGpu, cbInfo.currentCmdBuf(), fRenderTarget,
759 fOrigin,
Brian Salomon49348902018-06-26 09:12:38 -0400760 fixedDynamicState->fScissorRect);
Chris Dalton46983b72017-06-06 12:27:16 -0600761 }
Robert Phillipsd0fe8752019-01-31 14:13:59 -0500762 GrVkPipeline::SetDynamicViewportState(fGpu, cbInfo.currentCmdBuf(), fRenderTarget);
763 GrVkPipeline::SetDynamicBlendConstantState(fGpu, cbInfo.currentCmdBuf(),
Greg Daniel2c3398d2019-06-19 11:58:01 -0400764 pipeline.outputSwizzle(),
Chris Dalton46983b72017-06-06 12:27:16 -0600765 pipeline.getXferProcessor());
egdaniel9cb63402016-06-23 08:37:05 -0700766
767 return pipelineState;
768}
769
Brian Salomonff168d92018-06-23 15:17:27 -0400770void GrVkGpuRTCommandBuffer::onDraw(const GrPrimitiveProcessor& primProc,
771 const GrPipeline& pipeline,
Brian Salomon49348902018-06-26 09:12:38 -0400772 const GrPipeline::FixedDynamicState* fixedDynamicState,
773 const GrPipeline::DynamicStateArrays* dynamicStateArrays,
Greg Daniel500d58b2017-08-24 15:59:33 -0400774 const GrMesh meshes[],
Greg Daniel500d58b2017-08-24 15:59:33 -0400775 int meshCount,
776 const SkRect& bounds) {
egdaniel9cb63402016-06-23 08:37:05 -0700777 if (!meshCount) {
778 return;
779 }
Greg Danielea022cd2018-03-16 11:10:03 -0400780
781 CommandBufferInfo& cbInfo = fCommandBufferInfos[fCurrentCmdInfo];
782
Brian Salomone782f842018-07-31 13:53:11 -0400783 auto prepareSampledImage = [&](GrTexture* texture, GrSamplerState::Filter filter) {
784 GrVkTexture* vkTexture = static_cast<GrVkTexture*>(texture);
785 // We may need to resolve the texture first if it is also a render target
786 GrVkRenderTarget* texRT = static_cast<GrVkRenderTarget*>(vkTexture->asRenderTarget());
787 if (texRT) {
Greg Daniel0a77f432018-12-06 11:23:32 -0500788 fGpu->resolveRenderTargetNoFlush(texRT);
Brian Salomone782f842018-07-31 13:53:11 -0400789 }
790
791 // Check if we need to regenerate any mip maps
792 if (GrSamplerState::Filter::kMipMap == filter &&
793 (vkTexture->width() != 1 || vkTexture->height() != 1)) {
794 SkASSERT(vkTexture->texturePriv().mipMapped() == GrMipMapped::kYes);
795 if (vkTexture->texturePriv().mipMapsAreDirty()) {
796 fGpu->regenerateMipMapLevels(vkTexture);
797 }
798 }
Brian Salomon5fd10572019-04-01 12:07:05 -0400799 cbInfo.fSampledTextures.push_back(vkTexture);
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400800
801 SkASSERT(!texture->isProtected() ||
802 (fRenderTarget->isProtected() && fGpu->protectedContext()));
Brian Salomone782f842018-07-31 13:53:11 -0400803 };
804
Brian Salomonf7232642018-09-19 08:58:08 -0400805 if (dynamicStateArrays && dynamicStateArrays->fPrimitiveProcessorTextures) {
806 for (int m = 0, i = 0; m < meshCount; ++m) {
807 for (int s = 0; s < primProc.numTextureSamplers(); ++s, ++i) {
808 auto texture = dynamicStateArrays->fPrimitiveProcessorTextures[i]->peekTexture();
809 prepareSampledImage(texture, primProc.textureSampler(s).samplerState().filter());
810 }
811 }
812 } else {
813 for (int i = 0; i < primProc.numTextureSamplers(); ++i) {
814 auto texture = fixedDynamicState->fPrimitiveProcessorTextures[i]->peekTexture();
815 prepareSampledImage(texture, primProc.textureSampler(i).samplerState().filter());
816 }
Brian Salomone782f842018-07-31 13:53:11 -0400817 }
bsalomonb58a2b42016-09-26 06:55:02 -0700818 GrFragmentProcessor::Iter iter(pipeline);
819 while (const GrFragmentProcessor* fp = iter.next()) {
Brian Salomone782f842018-07-31 13:53:11 -0400820 for (int i = 0; i < fp->numTextureSamplers(); ++i) {
821 const GrFragmentProcessor::TextureSampler& sampler = fp->textureSampler(i);
822 prepareSampledImage(sampler.peekTexture(), sampler.samplerState().filter());
823 }
egdaniel2f5792a2016-07-06 08:51:23 -0700824 }
Robert Phillipsbb581ce2017-05-29 15:05:15 -0400825 if (GrTexture* dstTexture = pipeline.peekDstTexture()) {
Chris Dalton298238a2019-02-21 16:28:44 -0500826 cbInfo.fSampledTextures.push_back(sk_ref_sp(static_cast<GrVkTexture*>(dstTexture)));
Brian Salomon18dfa982017-04-03 16:57:43 -0400827 }
egdaniel2f5792a2016-07-06 08:51:23 -0700828
Chris Daltonbca46e22017-05-15 11:03:26 -0600829 GrPrimitiveType primitiveType = meshes[0].primitiveType();
Brian Salomon49348902018-06-26 09:12:38 -0400830 GrVkPipelineState* pipelineState = this->prepareDrawState(primProc, pipeline, fixedDynamicState,
831 dynamicStateArrays, primitiveType);
egdaniel9cb63402016-06-23 08:37:05 -0700832 if (!pipelineState) {
833 return;
834 }
835
Brian Salomond818ebf2018-07-02 14:08:49 +0000836 bool dynamicScissor =
837 pipeline.isScissorEnabled() && dynamicStateArrays && dynamicStateArrays->fScissorRects;
Brian Salomonf7232642018-09-19 08:58:08 -0400838 bool dynamicTextures = dynamicStateArrays && dynamicStateArrays->fPrimitiveProcessorTextures;
Brian Salomon49348902018-06-26 09:12:38 -0400839
egdaniel9cb63402016-06-23 08:37:05 -0700840 for (int i = 0; i < meshCount; ++i) {
841 const GrMesh& mesh = meshes[i];
Chris Daltonbca46e22017-05-15 11:03:26 -0600842 if (mesh.primitiveType() != primitiveType) {
Chris Dalton6f241802017-05-08 13:58:38 -0400843 SkDEBUGCODE(pipelineState = nullptr);
Chris Daltonbca46e22017-05-15 11:03:26 -0600844 primitiveType = mesh.primitiveType();
Brian Salomon49348902018-06-26 09:12:38 -0400845 pipelineState = this->prepareDrawState(primProc, pipeline, fixedDynamicState,
846 dynamicStateArrays, primitiveType);
Chris Dalton6f241802017-05-08 13:58:38 -0400847 if (!pipelineState) {
848 return;
egdaniel9cb63402016-06-23 08:37:05 -0700849 }
Chris Dalton6f241802017-05-08 13:58:38 -0400850 }
egdaniel9cb63402016-06-23 08:37:05 -0700851
Brian Salomon49348902018-06-26 09:12:38 -0400852 if (dynamicScissor) {
853 GrVkPipeline::SetDynamicScissorRectState(fGpu, cbInfo.currentCmdBuf(), fRenderTarget,
Robert Phillipsd0fe8752019-01-31 14:13:59 -0500854 fOrigin,
Brian Salomon49348902018-06-26 09:12:38 -0400855 dynamicStateArrays->fScissorRects[i]);
Chris Dalton46983b72017-06-06 12:27:16 -0600856 }
Brian Salomonf7232642018-09-19 08:58:08 -0400857 if (dynamicTextures) {
858 GrTextureProxy* const* meshProxies = dynamicStateArrays->fPrimitiveProcessorTextures +
859 primProc.numTextureSamplers() * i;
860 pipelineState->setAndBindTextures(fGpu, primProc, pipeline, meshProxies,
861 cbInfo.currentCmdBuf());
862 }
Chris Daltonbca46e22017-05-15 11:03:26 -0600863 SkASSERT(pipelineState);
Brian Salomon802cb312018-06-08 18:05:20 -0400864 mesh.sendToGpu(this);
egdaniel9cb63402016-06-23 08:37:05 -0700865 }
866
Greg Daniel36a77ee2016-10-18 10:33:25 -0400867 cbInfo.fBounds.join(bounds);
Chris Dalton114a3c02017-05-26 15:17:19 -0600868 cbInfo.fIsEmpty = false;
egdaniel066df7c2016-06-08 14:02:27 -0700869}
870
Brian Salomon802cb312018-06-08 18:05:20 -0400871void GrVkGpuRTCommandBuffer::sendInstancedMeshToGpu(GrPrimitiveType,
Greg Daniel500d58b2017-08-24 15:59:33 -0400872 const GrBuffer* vertexBuffer,
873 int vertexCount,
874 int baseVertex,
875 const GrBuffer* instanceBuffer,
876 int instanceCount,
877 int baseInstance) {
Chris Dalton114a3c02017-05-26 15:17:19 -0600878 CommandBufferInfo& cbInfo = fCommandBufferInfos[fCurrentCmdInfo];
Brian Salomondbf70722019-02-07 11:31:24 -0500879 SkASSERT(!vertexBuffer || !vertexBuffer->isCpuBuffer());
880 SkASSERT(!instanceBuffer || !instanceBuffer->isCpuBuffer());
881 auto gpuVertexBuffer = static_cast<const GrGpuBuffer*>(vertexBuffer);
882 auto gpuInstanceBuffer = static_cast<const GrGpuBuffer*>(instanceBuffer);
883 this->bindGeometry(nullptr, gpuVertexBuffer, gpuInstanceBuffer);
Chris Dalton1d616352017-05-31 12:51:23 -0600884 cbInfo.currentCmdBuf()->draw(fGpu, vertexCount, instanceCount, baseVertex, baseInstance);
Chris Dalton114a3c02017-05-26 15:17:19 -0600885 fGpu->stats()->incNumDraws();
886}
887
Brian Salomon802cb312018-06-08 18:05:20 -0400888void GrVkGpuRTCommandBuffer::sendIndexedInstancedMeshToGpu(GrPrimitiveType,
Greg Daniel500d58b2017-08-24 15:59:33 -0400889 const GrBuffer* indexBuffer,
890 int indexCount,
891 int baseIndex,
892 const GrBuffer* vertexBuffer,
893 int baseVertex,
894 const GrBuffer* instanceBuffer,
895 int instanceCount,
Brian Salomon802cb312018-06-08 18:05:20 -0400896 int baseInstance,
897 GrPrimitiveRestart restart) {
898 SkASSERT(restart == GrPrimitiveRestart::kNo);
Chris Dalton114a3c02017-05-26 15:17:19 -0600899 CommandBufferInfo& cbInfo = fCommandBufferInfos[fCurrentCmdInfo];
Brian Salomondbf70722019-02-07 11:31:24 -0500900 SkASSERT(!vertexBuffer || !vertexBuffer->isCpuBuffer());
901 SkASSERT(!instanceBuffer || !instanceBuffer->isCpuBuffer());
902 SkASSERT(!indexBuffer->isCpuBuffer());
903 auto gpuIndexxBuffer = static_cast<const GrGpuBuffer*>(indexBuffer);
904 auto gpuVertexBuffer = static_cast<const GrGpuBuffer*>(vertexBuffer);
905 auto gpuInstanceBuffer = static_cast<const GrGpuBuffer*>(instanceBuffer);
906 this->bindGeometry(gpuIndexxBuffer, gpuVertexBuffer, gpuInstanceBuffer);
Chris Dalton1d616352017-05-31 12:51:23 -0600907 cbInfo.currentCmdBuf()->drawIndexed(fGpu, indexCount, instanceCount,
908 baseIndex, baseVertex, baseInstance);
Chris Dalton114a3c02017-05-26 15:17:19 -0600909 fGpu->stats()->incNumDraws();
910}
Greg Daniel64cc9aa2018-10-19 13:54:56 -0400911
912////////////////////////////////////////////////////////////////////////////////
913
914void GrVkGpuRTCommandBuffer::executeDrawable(std::unique_ptr<SkDrawable::GpuDrawHandler> drawable) {
915 GrVkRenderTarget* target = static_cast<GrVkRenderTarget*>(fRenderTarget);
916
917 GrVkImage* targetImage = target->msaaImage() ? target->msaaImage() : target;
918
919 CommandBufferInfo& cbInfo = fCommandBufferInfos[fCurrentCmdInfo];
920 VkRect2D bounds;
921 bounds.offset = { 0, 0 };
922 bounds.extent = { 0, 0 };
923
924 GrVkDrawableInfo vkInfo;
925 vkInfo.fSecondaryCommandBuffer = cbInfo.currentCmdBuf()->vkCommandBuffer();
926 vkInfo.fCompatibleRenderPass = cbInfo.fRenderPass->vkRenderPass();
Greg Danielb353eeb2018-12-05 11:01:58 -0500927 SkAssertResult(cbInfo.fRenderPass->colorAttachmentIndex(&vkInfo.fColorAttachmentIndex));
Greg Daniel64cc9aa2018-10-19 13:54:56 -0400928 vkInfo.fFormat = targetImage->imageFormat();
929 vkInfo.fDrawBounds = &bounds;
Stan Ilievcb580602019-02-26 11:36:07 -0500930#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
931 vkInfo.fImage = targetImage->image();
932#else
933 vkInfo.fImage = VK_NULL_HANDLE;
934#endif //SK_BUILD_FOR_ANDROID_FRAMEWORK
Greg Daniel64cc9aa2018-10-19 13:54:56 -0400935
936 GrBackendDrawableInfo info(vkInfo);
937
Eric Karlc0b2ba22019-01-22 19:40:35 -0800938 // After we draw into the command buffer via the drawable, cached state we have may be invalid.
939 cbInfo.currentCmdBuf()->invalidateState();
Eric Karla8878a12019-02-07 18:17:43 -0800940 // Also assume that the drawable produced output.
941 cbInfo.fIsEmpty = false;
Eric Karlc0b2ba22019-01-22 19:40:35 -0800942
Greg Daniel64cc9aa2018-10-19 13:54:56 -0400943 drawable->draw(info);
944 fGpu->addDrawable(std::move(drawable));
945
946 if (bounds.extent.width == 0 || bounds.extent.height == 0) {
947 cbInfo.fBounds.join(target->getBoundsRect());
948 } else {
949 cbInfo.fBounds.join(SkRect::MakeXYWH(bounds.offset.x, bounds.offset.y,
950 bounds.extent.width, bounds.extent.height));
951 }
952}
953