blob: 10c6c824e620645f337359d5cb46cc5d9b641f68 [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::insertEventMarker(const char* msg) {
Robert Phillips65a88fa2017-08-08 08:36:22 -0400382 // TODO: does Vulkan have a correlate?
383}
384
Greg Daniel500d58b2017-08-24 15:59:33 -0400385void GrVkGpuRTCommandBuffer::onClearStencilClip(const GrFixedClip& clip, bool insideStencilMask) {
Chris Dalton94c04682017-11-01 17:15:06 -0600386 SkASSERT(!clip.hasWindowRectangles());
387
Jim Van Verth6a40abc2017-11-02 16:56:09 +0000388 CommandBufferInfo& cbInfo = fCommandBufferInfos[fCurrentCmdInfo];
389
Greg Daniel65a09272016-10-12 09:47:22 -0400390 GrStencilAttachment* sb = fRenderTarget->renderTargetPriv().getStencilAttachment();
egdaniel9cb63402016-06-23 08:37:05 -0700391 // this should only be called internally when we know we have a
392 // stencil buffer.
393 SkASSERT(sb);
394 int stencilBitCount = sb->bits();
395
396 // The contract with the callers does not guarantee that we preserve all bits in the stencil
397 // during this clear. Thus we will clear the entire stencil to the desired value.
398
399 VkClearDepthStencilValue vkStencilColor;
400 memset(&vkStencilColor, 0, sizeof(VkClearDepthStencilValue));
csmartdalton29df7602016-08-31 11:55:52 -0700401 if (insideStencilMask) {
egdaniel9cb63402016-06-23 08:37:05 -0700402 vkStencilColor.stencil = (1 << (stencilBitCount - 1));
403 } else {
404 vkStencilColor.stencil = 0;
405 }
406
407 VkClearRect clearRect;
408 // Flip rect if necessary
csmartdalton29df7602016-08-31 11:55:52 -0700409 SkIRect vkRect;
Brian Salomond818ebf2018-07-02 14:08:49 +0000410 if (!clip.scissorEnabled()) {
Greg Daniela41a74a2018-10-09 12:59:23 +0000411 vkRect.setXYWH(0, 0, fRenderTarget->width(), fRenderTarget->height());
Robert Phillips4f101a72017-07-28 08:42:04 -0400412 } else if (kBottomLeft_GrSurfaceOrigin != fOrigin) {
csmartdalton29df7602016-08-31 11:55:52 -0700413 vkRect = clip.scissorRect();
414 } else {
415 const SkIRect& scissor = clip.scissorRect();
Greg Daniel65a09272016-10-12 09:47:22 -0400416 vkRect.setLTRB(scissor.fLeft, fRenderTarget->height() - scissor.fBottom,
417 scissor.fRight, fRenderTarget->height() - scissor.fTop);
egdaniel9cb63402016-06-23 08:37:05 -0700418 }
419
420 clearRect.rect.offset = { vkRect.fLeft, vkRect.fTop };
421 clearRect.rect.extent = { (uint32_t)vkRect.width(), (uint32_t)vkRect.height() };
422
423 clearRect.baseArrayLayer = 0;
424 clearRect.layerCount = 1;
425
426 uint32_t stencilIndex;
Greg Daniel36a77ee2016-10-18 10:33:25 -0400427 SkAssertResult(cbInfo.fRenderPass->stencilAttachmentIndex(&stencilIndex));
egdaniel9cb63402016-06-23 08:37:05 -0700428
429 VkClearAttachment attachment;
430 attachment.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
431 attachment.colorAttachment = 0; // this value shouldn't matter
432 attachment.clearValue.depthStencil = vkStencilColor;
433
Greg Daniel22bc8652017-03-22 15:45:43 -0400434 cbInfo.currentCmdBuf()->clearAttachments(fGpu, 1, &attachment, 1, &clearRect);
Greg Daniel77b53f62016-10-18 11:48:51 -0400435 cbInfo.fIsEmpty = false;
Greg Daniel36a77ee2016-10-18 10:33:25 -0400436
437 // Update command buffer bounds
Brian Salomond818ebf2018-07-02 14:08:49 +0000438 if (!clip.scissorEnabled()) {
Greg Daniel36a77ee2016-10-18 10:33:25 -0400439 cbInfo.fBounds.join(fRenderTarget->getBoundsRect());
440 } else {
441 cbInfo.fBounds.join(SkRect::Make(clip.scissorRect()));
442 }
egdaniel9cb63402016-06-23 08:37:05 -0700443}
444
Brian Osman9a9baae2018-11-05 15:06:26 -0500445void GrVkGpuRTCommandBuffer::onClear(const GrFixedClip& clip, const SkPMColor4f& color) {
Robert Phillips19e51dc2017-08-09 09:30:51 -0400446 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(fRenderTarget);
447
Jim Van Verth6a40abc2017-11-02 16:56:09 +0000448 // parent class should never let us get here with no RT
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700449 SkASSERT(!clip.hasWindowRectangles());
egdaniel9cb63402016-06-23 08:37:05 -0700450
Greg Daniel22bc8652017-03-22 15:45:43 -0400451 CommandBufferInfo& cbInfo = fCommandBufferInfos[fCurrentCmdInfo];
Greg Daniel36a77ee2016-10-18 10:33:25 -0400452
Brian Osman9a9baae2018-11-05 15:06:26 -0500453 VkClearColorValue vkColor = {{color.fR, color.fG, color.fB, color.fA}};
egdaniel9cb63402016-06-23 08:37:05 -0700454
Brian Salomond818ebf2018-07-02 14:08:49 +0000455 if (cbInfo.fIsEmpty && !clip.scissorEnabled()) {
Robert Phillips74c627f2017-08-09 10:28:00 -0400456 // Change the render pass to do a clear load
egdaniel9cb63402016-06-23 08:37:05 -0700457 GrVkRenderPass::LoadStoreOps vkColorOps(VK_ATTACHMENT_LOAD_OP_CLEAR,
458 VK_ATTACHMENT_STORE_OP_STORE);
Robert Phillips74c627f2017-08-09 10:28:00 -0400459 // Preserve the stencil buffer's load & store settings
460 GrVkRenderPass::LoadStoreOps vkStencilOps(fVkStencilLoadOp, fVkStencilStoreOp);
egdaniel9cb63402016-06-23 08:37:05 -0700461
Greg Daniel36a77ee2016-10-18 10:33:25 -0400462 const GrVkRenderPass* oldRP = cbInfo.fRenderPass;
egdaniel9cb63402016-06-23 08:37:05 -0700463
464 const GrVkResourceProvider::CompatibleRPHandle& rpHandle =
Robert Phillips19e51dc2017-08-09 09:30:51 -0400465 vkRT->compatibleRenderPassHandle();
egdaniel9cb63402016-06-23 08:37:05 -0700466 if (rpHandle.isValid()) {
Greg Daniel36a77ee2016-10-18 10:33:25 -0400467 cbInfo.fRenderPass = fGpu->resourceProvider().findRenderPass(rpHandle,
468 vkColorOps,
469 vkStencilOps);
egdaniel9cb63402016-06-23 08:37:05 -0700470 } else {
Robert Phillips19e51dc2017-08-09 09:30:51 -0400471 cbInfo.fRenderPass = fGpu->resourceProvider().findRenderPass(*vkRT,
Greg Daniel36a77ee2016-10-18 10:33:25 -0400472 vkColorOps,
473 vkStencilOps);
egdaniel9cb63402016-06-23 08:37:05 -0700474 }
475
Greg Daniel36a77ee2016-10-18 10:33:25 -0400476 SkASSERT(cbInfo.fRenderPass->isCompatible(*oldRP));
egdaniel9cb63402016-06-23 08:37:05 -0700477 oldRP->unref(fGpu);
478
Brian Osman9a9baae2018-11-05 15:06:26 -0500479 cbInfo.fColorClearValue.color = {{color.fR, color.fG, color.fB, color.fA}};
Greg Daniela3c68df2018-03-16 13:46:53 -0400480 cbInfo.fLoadStoreState = LoadStoreState::kStartsWithClear;
Greg Daniel36a77ee2016-10-18 10:33:25 -0400481 // Update command buffer bounds
482 cbInfo.fBounds.join(fRenderTarget->getBoundsRect());
egdaniel9cb63402016-06-23 08:37:05 -0700483 return;
484 }
485
486 // We always do a sub rect clear with clearAttachments since we are inside a render pass
487 VkClearRect clearRect;
488 // Flip rect if necessary
csmartdalton29df7602016-08-31 11:55:52 -0700489 SkIRect vkRect;
Brian Salomond818ebf2018-07-02 14:08:49 +0000490 if (!clip.scissorEnabled()) {
Greg Daniela41a74a2018-10-09 12:59:23 +0000491 vkRect.setXYWH(0, 0, fRenderTarget->width(), fRenderTarget->height());
Robert Phillips4f101a72017-07-28 08:42:04 -0400492 } else if (kBottomLeft_GrSurfaceOrigin != fOrigin) {
csmartdalton29df7602016-08-31 11:55:52 -0700493 vkRect = clip.scissorRect();
494 } else {
495 const SkIRect& scissor = clip.scissorRect();
Greg Daniel65a09272016-10-12 09:47:22 -0400496 vkRect.setLTRB(scissor.fLeft, fRenderTarget->height() - scissor.fBottom,
497 scissor.fRight, fRenderTarget->height() - scissor.fTop);
egdaniel9cb63402016-06-23 08:37:05 -0700498 }
499 clearRect.rect.offset = { vkRect.fLeft, vkRect.fTop };
500 clearRect.rect.extent = { (uint32_t)vkRect.width(), (uint32_t)vkRect.height() };
501 clearRect.baseArrayLayer = 0;
502 clearRect.layerCount = 1;
503
504 uint32_t colorIndex;
Greg Daniel36a77ee2016-10-18 10:33:25 -0400505 SkAssertResult(cbInfo.fRenderPass->colorAttachmentIndex(&colorIndex));
egdaniel9cb63402016-06-23 08:37:05 -0700506
507 VkClearAttachment attachment;
508 attachment.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
509 attachment.colorAttachment = colorIndex;
510 attachment.clearValue.color = vkColor;
511
Greg Daniel22bc8652017-03-22 15:45:43 -0400512 cbInfo.currentCmdBuf()->clearAttachments(fGpu, 1, &attachment, 1, &clearRect);
Greg Daniel77b53f62016-10-18 11:48:51 -0400513 cbInfo.fIsEmpty = false;
Greg Daniel36a77ee2016-10-18 10:33:25 -0400514
515 // Update command buffer bounds
Brian Salomond818ebf2018-07-02 14:08:49 +0000516 if (!clip.scissorEnabled()) {
Greg Daniel36a77ee2016-10-18 10:33:25 -0400517 cbInfo.fBounds.join(fRenderTarget->getBoundsRect());
518 } else {
519 cbInfo.fBounds.join(SkRect::Make(clip.scissorRect()));
520 }
egdaniel9cb63402016-06-23 08:37:05 -0700521 return;
522}
523
Greg Daniel500d58b2017-08-24 15:59:33 -0400524////////////////////////////////////////////////////////////////////////////////
525
526void GrVkGpuRTCommandBuffer::addAdditionalCommandBuffer() {
Robert Phillips19e51dc2017-08-09 09:30:51 -0400527 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(fRenderTarget);
528
Greg Daniel22bc8652017-03-22 15:45:43 -0400529 CommandBufferInfo& cbInfo = fCommandBufferInfos[fCurrentCmdInfo];
530 cbInfo.currentCmdBuf()->end(fGpu);
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500531 cbInfo.fCommandBuffers.push_back(fGpu->cmdPool()->findOrCreateSecondaryCommandBuffer(fGpu));
Robert Phillips19e51dc2017-08-09 09:30:51 -0400532 cbInfo.currentCmdBuf()->begin(fGpu, vkRT->framebuffer(), cbInfo.fRenderPass);
Greg Daniel22bc8652017-03-22 15:45:43 -0400533}
534
Greg Daniel500d58b2017-08-24 15:59:33 -0400535void GrVkGpuRTCommandBuffer::addAdditionalRenderPass() {
Robert Phillips19e51dc2017-08-09 09:30:51 -0400536 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(fRenderTarget);
537
Greg Daniel22bc8652017-03-22 15:45:43 -0400538 fCommandBufferInfos[fCurrentCmdInfo].currentCmdBuf()->end(fGpu);
Greg Daniel77b53f62016-10-18 11:48:51 -0400539
540 CommandBufferInfo& cbInfo = fCommandBufferInfos.push_back();
Greg Daniel22bc8652017-03-22 15:45:43 -0400541 fCurrentCmdInfo++;
Greg Daniel77b53f62016-10-18 11:48:51 -0400542
543 GrVkRenderPass::LoadStoreOps vkColorOps(VK_ATTACHMENT_LOAD_OP_LOAD,
544 VK_ATTACHMENT_STORE_OP_STORE);
545 GrVkRenderPass::LoadStoreOps vkStencilOps(VK_ATTACHMENT_LOAD_OP_LOAD,
546 VK_ATTACHMENT_STORE_OP_STORE);
547
548 const GrVkResourceProvider::CompatibleRPHandle& rpHandle =
Robert Phillips19e51dc2017-08-09 09:30:51 -0400549 vkRT->compatibleRenderPassHandle();
Greg Daniel77b53f62016-10-18 11:48:51 -0400550 if (rpHandle.isValid()) {
551 cbInfo.fRenderPass = fGpu->resourceProvider().findRenderPass(rpHandle,
552 vkColorOps,
553 vkStencilOps);
554 } else {
Robert Phillips19e51dc2017-08-09 09:30:51 -0400555 cbInfo.fRenderPass = fGpu->resourceProvider().findRenderPass(*vkRT,
Greg Daniel77b53f62016-10-18 11:48:51 -0400556 vkColorOps,
557 vkStencilOps);
558 }
Greg Daniela3c68df2018-03-16 13:46:53 -0400559 cbInfo.fLoadStoreState = LoadStoreState::kLoadAndStore;
Greg Daniel77b53f62016-10-18 11:48:51 -0400560
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500561 cbInfo.fCommandBuffers.push_back(fGpu->cmdPool()->findOrCreateSecondaryCommandBuffer(fGpu));
Greg Daniel77b53f62016-10-18 11:48:51 -0400562 // It shouldn't matter what we set the clear color to here since we will assume loading of the
563 // attachment.
564 memset(&cbInfo.fColorClearValue, 0, sizeof(VkClearValue));
565 cbInfo.fBounds.setEmpty();
Greg Daniel77b53f62016-10-18 11:48:51 -0400566
Robert Phillips19e51dc2017-08-09 09:30:51 -0400567 cbInfo.currentCmdBuf()->begin(fGpu, vkRT->framebuffer(), cbInfo.fRenderPass);
Greg Daniel77b53f62016-10-18 11:48:51 -0400568}
569
Brian Salomon943ed792017-10-30 09:37:55 -0400570void GrVkGpuRTCommandBuffer::inlineUpload(GrOpFlushState* state,
571 GrDeferredTextureUploadFn& upload) {
Greg Daniel22bc8652017-03-22 15:45:43 -0400572 if (!fCommandBufferInfos[fCurrentCmdInfo].fIsEmpty) {
573 this->addAdditionalRenderPass();
Greg Daniel77b53f62016-10-18 11:48:51 -0400574 }
Brian Salomon24d377e2019-04-23 15:24:31 -0400575
Brian Salomon24d377e2019-04-23 15:24:31 -0400576 fPreCommandBufferTasks.emplace<InlineUpload>(state, upload);
577 ++fCommandBufferInfos[fCurrentCmdInfo].fNumPreCmds;
Greg Daniel77b53f62016-10-18 11:48:51 -0400578}
579
Greg Daniel46cfbc62019-06-07 11:43:30 -0400580void GrVkGpuRTCommandBuffer::copy(GrSurface* src, const SkIRect& srcRect,
Greg Daniel500d58b2017-08-24 15:59:33 -0400581 const SkIPoint& dstPoint) {
Greg Daniela3c68df2018-03-16 13:46:53 -0400582 CommandBufferInfo& cbInfo = fCommandBufferInfos[fCurrentCmdInfo];
583 if (!cbInfo.fIsEmpty || LoadStoreState::kStartsWithClear == cbInfo.fLoadStoreState) {
Greg Daniel500d58b2017-08-24 15:59:33 -0400584 this->addAdditionalRenderPass();
585 }
Greg Daniela3c68df2018-03-16 13:46:53 -0400586
Brian Salomon24d377e2019-04-23 15:24:31 -0400587 fPreCommandBufferTasks.emplace<Copy>(
Greg Daniel46cfbc62019-06-07 11:43:30 -0400588 src, srcRect, dstPoint, LoadStoreState::kStartsWithDiscard == cbInfo.fLoadStoreState);
Brian Salomon24d377e2019-04-23 15:24:31 -0400589 ++fCommandBufferInfos[fCurrentCmdInfo].fNumPreCmds;
Greg Daniel55fa6472018-03-16 16:13:10 -0400590
Greg Daniela3c68df2018-03-16 13:46:53 -0400591 if (LoadStoreState::kLoadAndStore != cbInfo.fLoadStoreState) {
592 // Change the render pass to do a load and store so we don't lose the results of our copy
593 GrVkRenderPass::LoadStoreOps vkColorOps(VK_ATTACHMENT_LOAD_OP_LOAD,
594 VK_ATTACHMENT_STORE_OP_STORE);
595 GrVkRenderPass::LoadStoreOps vkStencilOps(VK_ATTACHMENT_LOAD_OP_LOAD,
596 VK_ATTACHMENT_STORE_OP_STORE);
597
598 const GrVkRenderPass* oldRP = cbInfo.fRenderPass;
599
600 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(fRenderTarget);
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400601 SkASSERT(!src->isProtected() || (fRenderTarget->isProtected() && fGpu->protectedContext()));
Greg Daniela3c68df2018-03-16 13:46:53 -0400602 const GrVkResourceProvider::CompatibleRPHandle& rpHandle =
603 vkRT->compatibleRenderPassHandle();
604 if (rpHandle.isValid()) {
605 cbInfo.fRenderPass = fGpu->resourceProvider().findRenderPass(rpHandle,
606 vkColorOps,
607 vkStencilOps);
608 } else {
609 cbInfo.fRenderPass = fGpu->resourceProvider().findRenderPass(*vkRT,
610 vkColorOps,
611 vkStencilOps);
612 }
613 SkASSERT(cbInfo.fRenderPass->isCompatible(*oldRP));
614 oldRP->unref(fGpu);
615
616 cbInfo.fLoadStoreState = LoadStoreState::kLoadAndStore;
617
618 }
Greg Daniel500d58b2017-08-24 15:59:33 -0400619}
620
Brian Salomonab32f652019-05-10 14:24:50 -0400621void GrVkGpuRTCommandBuffer::transferFrom(const SkIRect& srcRect, GrColorType bufferColorType,
622 GrGpuBuffer* transferBuffer, size_t offset) {
623 CommandBufferInfo& cbInfo = fCommandBufferInfos[fCurrentCmdInfo];
624 if (!cbInfo.fIsEmpty) {
625 this->addAdditionalRenderPass();
626 }
627 fPreCommandBufferTasks.emplace<TransferFrom>(srcRect, bufferColorType, transferBuffer, offset);
628 ++fCommandBufferInfos[fCurrentCmdInfo].fNumPreCmds;
629}
630
egdaniel9cb63402016-06-23 08:37:05 -0700631////////////////////////////////////////////////////////////////////////////////
632
Brian Salomondbf70722019-02-07 11:31:24 -0500633void GrVkGpuRTCommandBuffer::bindGeometry(const GrGpuBuffer* indexBuffer,
634 const GrGpuBuffer* vertexBuffer,
635 const GrGpuBuffer* instanceBuffer) {
Chris Daltonff926502017-05-03 14:36:54 -0400636 GrVkSecondaryCommandBuffer* currCmdBuf = fCommandBufferInfos[fCurrentCmdInfo].currentCmdBuf();
egdaniel9cb63402016-06-23 08:37:05 -0700637 // There is no need to put any memory barriers to make sure host writes have finished here.
638 // When a command buffer is submitted to a queue, there is an implicit memory barrier that
639 // occurs for all host writes. Additionally, BufferMemoryBarriers are not allowed inside of
640 // an active RenderPass.
egdaniel9cb63402016-06-23 08:37:05 -0700641
Chris Dalton1d616352017-05-31 12:51:23 -0600642 // Here our vertex and instance inputs need to match the same 0-based bindings they were
643 // assigned in GrVkPipeline. That is, vertex first (if any) followed by instance.
644 uint32_t binding = 0;
645
Brian Salomon802cb312018-06-08 18:05:20 -0400646 if (vertexBuffer) {
Chris Dalton1d616352017-05-31 12:51:23 -0600647 SkASSERT(vertexBuffer);
Chris Dalton1d616352017-05-31 12:51:23 -0600648 SkASSERT(!vertexBuffer->isMapped());
649
650 currCmdBuf->bindInputBuffer(fGpu, binding++,
651 static_cast<const GrVkVertexBuffer*>(vertexBuffer));
652 }
653
Brian Salomon802cb312018-06-08 18:05:20 -0400654 if (instanceBuffer) {
Chris Dalton1d616352017-05-31 12:51:23 -0600655 SkASSERT(instanceBuffer);
Chris Dalton1d616352017-05-31 12:51:23 -0600656 SkASSERT(!instanceBuffer->isMapped());
657
658 currCmdBuf->bindInputBuffer(fGpu, binding++,
659 static_cast<const GrVkVertexBuffer*>(instanceBuffer));
660 }
Chris Daltonff926502017-05-03 14:36:54 -0400661 if (indexBuffer) {
662 SkASSERT(indexBuffer);
663 SkASSERT(!indexBuffer->isMapped());
egdaniel9cb63402016-06-23 08:37:05 -0700664
Chris Daltonff926502017-05-03 14:36:54 -0400665 currCmdBuf->bindIndexBuffer(fGpu, static_cast<const GrVkIndexBuffer*>(indexBuffer));
egdaniel9cb63402016-06-23 08:37:05 -0700666 }
667}
668
Brian Salomon49348902018-06-26 09:12:38 -0400669GrVkPipelineState* GrVkGpuRTCommandBuffer::prepareDrawState(
670 const GrPrimitiveProcessor& primProc,
671 const GrPipeline& pipeline,
672 const GrPipeline::FixedDynamicState* fixedDynamicState,
673 const GrPipeline::DynamicStateArrays* dynamicStateArrays,
674 GrPrimitiveType primitiveType) {
Greg Daniel22bc8652017-03-22 15:45:43 -0400675 CommandBufferInfo& cbInfo = fCommandBufferInfos[fCurrentCmdInfo];
676 SkASSERT(cbInfo.fRenderPass);
Greg Daniel36a77ee2016-10-18 10:33:25 -0400677
Greg Daniel99b88e02018-10-03 15:31:20 -0400678 VkRenderPass compatibleRenderPass = cbInfo.fRenderPass->vkRenderPass();
679
Greg Daniel9a51a862018-11-30 10:18:14 -0500680 const GrTextureProxy* const* primProcProxies = nullptr;
681 if (dynamicStateArrays && dynamicStateArrays->fPrimitiveProcessorTextures) {
682 primProcProxies = dynamicStateArrays->fPrimitiveProcessorTextures;
683 } else if (fixedDynamicState) {
684 primProcProxies = fixedDynamicState->fPrimitiveProcessorTextures;
685 }
686
687 SkASSERT(SkToBool(primProcProxies) == SkToBool(primProc.numTextureSamplers()));
688
Greg Daniel09eeefb2017-10-16 15:15:02 -0400689 GrVkPipelineState* pipelineState =
Robert Phillipsd0fe8752019-01-31 14:13:59 -0500690 fGpu->resourceProvider().findOrCreateCompatiblePipelineState(fRenderTarget, fOrigin,
691 pipeline,
egdaniel9cb63402016-06-23 08:37:05 -0700692 primProc,
Greg Daniel9a51a862018-11-30 10:18:14 -0500693 primProcProxies,
egdaniel9cb63402016-06-23 08:37:05 -0700694 primitiveType,
Greg Daniel99b88e02018-10-03 15:31:20 -0400695 compatibleRenderPass);
egdaniel9cb63402016-06-23 08:37:05 -0700696 if (!pipelineState) {
697 return pipelineState;
698 }
699
Greg Daniel22bc8652017-03-22 15:45:43 -0400700 if (!cbInfo.fIsEmpty &&
Greg Daniel09eeefb2017-10-16 15:15:02 -0400701 fLastPipelineState && fLastPipelineState != pipelineState &&
Greg Daniele3cd6912017-05-17 11:15:55 -0400702 fGpu->vkCaps().newCBOnPipelineChange()) {
Greg Daniel22bc8652017-03-22 15:45:43 -0400703 this->addAdditionalCommandBuffer();
704 }
Greg Daniel09eeefb2017-10-16 15:15:02 -0400705 fLastPipelineState = pipelineState;
Greg Daniel22bc8652017-03-22 15:45:43 -0400706
Brian Salomonf7232642018-09-19 08:58:08 -0400707 pipelineState->bindPipeline(fGpu, cbInfo.currentCmdBuf());
Brian Salomoncd7907b2018-08-30 08:36:18 -0400708
Robert Phillipsd0fe8752019-01-31 14:13:59 -0500709 pipelineState->setAndBindUniforms(fGpu, fRenderTarget, fOrigin,
710 primProc, pipeline, cbInfo.currentCmdBuf());
Brian Salomonf7232642018-09-19 08:58:08 -0400711
712 // Check whether we need to bind textures between each GrMesh. If not we can bind them all now.
713 bool setTextures = !(dynamicStateArrays && dynamicStateArrays->fPrimitiveProcessorTextures);
714 if (setTextures) {
Brian Salomonf7232642018-09-19 08:58:08 -0400715 pipelineState->setAndBindTextures(fGpu, primProc, pipeline, primProcProxies,
716 cbInfo.currentCmdBuf());
717 }
egdaniel9cb63402016-06-23 08:37:05 -0700718
Brian Salomond818ebf2018-07-02 14:08:49 +0000719 if (!pipeline.isScissorEnabled()) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400720 GrVkPipeline::SetDynamicScissorRectState(fGpu, cbInfo.currentCmdBuf(),
Robert Phillipsd0fe8752019-01-31 14:13:59 -0500721 fRenderTarget, fOrigin,
722 SkIRect::MakeWH(fRenderTarget->width(),
723 fRenderTarget->height()));
Brian Salomon49348902018-06-26 09:12:38 -0400724 } else if (!dynamicStateArrays || !dynamicStateArrays->fScissorRects) {
725 SkASSERT(fixedDynamicState);
Robert Phillipsd0fe8752019-01-31 14:13:59 -0500726 GrVkPipeline::SetDynamicScissorRectState(fGpu, cbInfo.currentCmdBuf(), fRenderTarget,
727 fOrigin,
Brian Salomon49348902018-06-26 09:12:38 -0400728 fixedDynamicState->fScissorRect);
Chris Dalton46983b72017-06-06 12:27:16 -0600729 }
Robert Phillipsd0fe8752019-01-31 14:13:59 -0500730 GrVkPipeline::SetDynamicViewportState(fGpu, cbInfo.currentCmdBuf(), fRenderTarget);
731 GrVkPipeline::SetDynamicBlendConstantState(fGpu, cbInfo.currentCmdBuf(),
Greg Daniel2c3398d2019-06-19 11:58:01 -0400732 pipeline.outputSwizzle(),
Chris Dalton46983b72017-06-06 12:27:16 -0600733 pipeline.getXferProcessor());
egdaniel9cb63402016-06-23 08:37:05 -0700734
735 return pipelineState;
736}
737
Brian Salomonff168d92018-06-23 15:17:27 -0400738void GrVkGpuRTCommandBuffer::onDraw(const GrPrimitiveProcessor& primProc,
739 const GrPipeline& pipeline,
Brian Salomon49348902018-06-26 09:12:38 -0400740 const GrPipeline::FixedDynamicState* fixedDynamicState,
741 const GrPipeline::DynamicStateArrays* dynamicStateArrays,
Greg Daniel500d58b2017-08-24 15:59:33 -0400742 const GrMesh meshes[],
Greg Daniel500d58b2017-08-24 15:59:33 -0400743 int meshCount,
744 const SkRect& bounds) {
egdaniel9cb63402016-06-23 08:37:05 -0700745 if (!meshCount) {
746 return;
747 }
Greg Danielea022cd2018-03-16 11:10:03 -0400748
749 CommandBufferInfo& cbInfo = fCommandBufferInfos[fCurrentCmdInfo];
750
Brian Salomone782f842018-07-31 13:53:11 -0400751 auto prepareSampledImage = [&](GrTexture* texture, GrSamplerState::Filter filter) {
752 GrVkTexture* vkTexture = static_cast<GrVkTexture*>(texture);
753 // We may need to resolve the texture first if it is also a render target
754 GrVkRenderTarget* texRT = static_cast<GrVkRenderTarget*>(vkTexture->asRenderTarget());
755 if (texRT) {
Greg Daniel0a77f432018-12-06 11:23:32 -0500756 fGpu->resolveRenderTargetNoFlush(texRT);
Brian Salomone782f842018-07-31 13:53:11 -0400757 }
758
759 // Check if we need to regenerate any mip maps
760 if (GrSamplerState::Filter::kMipMap == filter &&
761 (vkTexture->width() != 1 || vkTexture->height() != 1)) {
762 SkASSERT(vkTexture->texturePriv().mipMapped() == GrMipMapped::kYes);
763 if (vkTexture->texturePriv().mipMapsAreDirty()) {
764 fGpu->regenerateMipMapLevels(vkTexture);
765 }
766 }
Brian Salomon5fd10572019-04-01 12:07:05 -0400767 cbInfo.fSampledTextures.push_back(vkTexture);
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400768
769 SkASSERT(!texture->isProtected() ||
770 (fRenderTarget->isProtected() && fGpu->protectedContext()));
Brian Salomone782f842018-07-31 13:53:11 -0400771 };
772
Brian Salomonf7232642018-09-19 08:58:08 -0400773 if (dynamicStateArrays && dynamicStateArrays->fPrimitiveProcessorTextures) {
774 for (int m = 0, i = 0; m < meshCount; ++m) {
775 for (int s = 0; s < primProc.numTextureSamplers(); ++s, ++i) {
776 auto texture = dynamicStateArrays->fPrimitiveProcessorTextures[i]->peekTexture();
777 prepareSampledImage(texture, primProc.textureSampler(s).samplerState().filter());
778 }
779 }
780 } else {
781 for (int i = 0; i < primProc.numTextureSamplers(); ++i) {
782 auto texture = fixedDynamicState->fPrimitiveProcessorTextures[i]->peekTexture();
783 prepareSampledImage(texture, primProc.textureSampler(i).samplerState().filter());
784 }
Brian Salomone782f842018-07-31 13:53:11 -0400785 }
bsalomonb58a2b42016-09-26 06:55:02 -0700786 GrFragmentProcessor::Iter iter(pipeline);
787 while (const GrFragmentProcessor* fp = iter.next()) {
Brian Salomone782f842018-07-31 13:53:11 -0400788 for (int i = 0; i < fp->numTextureSamplers(); ++i) {
789 const GrFragmentProcessor::TextureSampler& sampler = fp->textureSampler(i);
790 prepareSampledImage(sampler.peekTexture(), sampler.samplerState().filter());
791 }
egdaniel2f5792a2016-07-06 08:51:23 -0700792 }
Robert Phillipsbb581ce2017-05-29 15:05:15 -0400793 if (GrTexture* dstTexture = pipeline.peekDstTexture()) {
Chris Dalton298238a2019-02-21 16:28:44 -0500794 cbInfo.fSampledTextures.push_back(sk_ref_sp(static_cast<GrVkTexture*>(dstTexture)));
Brian Salomon18dfa982017-04-03 16:57:43 -0400795 }
egdaniel2f5792a2016-07-06 08:51:23 -0700796
Chris Daltonbca46e22017-05-15 11:03:26 -0600797 GrPrimitiveType primitiveType = meshes[0].primitiveType();
Brian Salomon49348902018-06-26 09:12:38 -0400798 GrVkPipelineState* pipelineState = this->prepareDrawState(primProc, pipeline, fixedDynamicState,
799 dynamicStateArrays, primitiveType);
egdaniel9cb63402016-06-23 08:37:05 -0700800 if (!pipelineState) {
801 return;
802 }
803
Brian Salomond818ebf2018-07-02 14:08:49 +0000804 bool dynamicScissor =
805 pipeline.isScissorEnabled() && dynamicStateArrays && dynamicStateArrays->fScissorRects;
Brian Salomonf7232642018-09-19 08:58:08 -0400806 bool dynamicTextures = dynamicStateArrays && dynamicStateArrays->fPrimitiveProcessorTextures;
Brian Salomon49348902018-06-26 09:12:38 -0400807
egdaniel9cb63402016-06-23 08:37:05 -0700808 for (int i = 0; i < meshCount; ++i) {
809 const GrMesh& mesh = meshes[i];
Chris Daltonbca46e22017-05-15 11:03:26 -0600810 if (mesh.primitiveType() != primitiveType) {
Chris Dalton6f241802017-05-08 13:58:38 -0400811 SkDEBUGCODE(pipelineState = nullptr);
Chris Daltonbca46e22017-05-15 11:03:26 -0600812 primitiveType = mesh.primitiveType();
Brian Salomon49348902018-06-26 09:12:38 -0400813 pipelineState = this->prepareDrawState(primProc, pipeline, fixedDynamicState,
814 dynamicStateArrays, primitiveType);
Chris Dalton6f241802017-05-08 13:58:38 -0400815 if (!pipelineState) {
816 return;
egdaniel9cb63402016-06-23 08:37:05 -0700817 }
Chris Dalton6f241802017-05-08 13:58:38 -0400818 }
egdaniel9cb63402016-06-23 08:37:05 -0700819
Brian Salomon49348902018-06-26 09:12:38 -0400820 if (dynamicScissor) {
821 GrVkPipeline::SetDynamicScissorRectState(fGpu, cbInfo.currentCmdBuf(), fRenderTarget,
Robert Phillipsd0fe8752019-01-31 14:13:59 -0500822 fOrigin,
Brian Salomon49348902018-06-26 09:12:38 -0400823 dynamicStateArrays->fScissorRects[i]);
Chris Dalton46983b72017-06-06 12:27:16 -0600824 }
Brian Salomonf7232642018-09-19 08:58:08 -0400825 if (dynamicTextures) {
826 GrTextureProxy* const* meshProxies = dynamicStateArrays->fPrimitiveProcessorTextures +
827 primProc.numTextureSamplers() * i;
828 pipelineState->setAndBindTextures(fGpu, primProc, pipeline, meshProxies,
829 cbInfo.currentCmdBuf());
830 }
Chris Daltonbca46e22017-05-15 11:03:26 -0600831 SkASSERT(pipelineState);
Brian Salomon802cb312018-06-08 18:05:20 -0400832 mesh.sendToGpu(this);
egdaniel9cb63402016-06-23 08:37:05 -0700833 }
834
Greg Daniel36a77ee2016-10-18 10:33:25 -0400835 cbInfo.fBounds.join(bounds);
Chris Dalton114a3c02017-05-26 15:17:19 -0600836 cbInfo.fIsEmpty = false;
egdaniel066df7c2016-06-08 14:02:27 -0700837}
838
Brian Salomon802cb312018-06-08 18:05:20 -0400839void GrVkGpuRTCommandBuffer::sendInstancedMeshToGpu(GrPrimitiveType,
Greg Daniel500d58b2017-08-24 15:59:33 -0400840 const GrBuffer* vertexBuffer,
841 int vertexCount,
842 int baseVertex,
843 const GrBuffer* instanceBuffer,
844 int instanceCount,
845 int baseInstance) {
Chris Dalton114a3c02017-05-26 15:17:19 -0600846 CommandBufferInfo& cbInfo = fCommandBufferInfos[fCurrentCmdInfo];
Brian Salomondbf70722019-02-07 11:31:24 -0500847 SkASSERT(!vertexBuffer || !vertexBuffer->isCpuBuffer());
848 SkASSERT(!instanceBuffer || !instanceBuffer->isCpuBuffer());
849 auto gpuVertexBuffer = static_cast<const GrGpuBuffer*>(vertexBuffer);
850 auto gpuInstanceBuffer = static_cast<const GrGpuBuffer*>(instanceBuffer);
851 this->bindGeometry(nullptr, gpuVertexBuffer, gpuInstanceBuffer);
Chris Dalton1d616352017-05-31 12:51:23 -0600852 cbInfo.currentCmdBuf()->draw(fGpu, vertexCount, instanceCount, baseVertex, baseInstance);
Chris Dalton114a3c02017-05-26 15:17:19 -0600853 fGpu->stats()->incNumDraws();
854}
855
Brian Salomon802cb312018-06-08 18:05:20 -0400856void GrVkGpuRTCommandBuffer::sendIndexedInstancedMeshToGpu(GrPrimitiveType,
Greg Daniel500d58b2017-08-24 15:59:33 -0400857 const GrBuffer* indexBuffer,
858 int indexCount,
859 int baseIndex,
860 const GrBuffer* vertexBuffer,
861 int baseVertex,
862 const GrBuffer* instanceBuffer,
863 int instanceCount,
Brian Salomon802cb312018-06-08 18:05:20 -0400864 int baseInstance,
865 GrPrimitiveRestart restart) {
866 SkASSERT(restart == GrPrimitiveRestart::kNo);
Chris Dalton114a3c02017-05-26 15:17:19 -0600867 CommandBufferInfo& cbInfo = fCommandBufferInfos[fCurrentCmdInfo];
Brian Salomondbf70722019-02-07 11:31:24 -0500868 SkASSERT(!vertexBuffer || !vertexBuffer->isCpuBuffer());
869 SkASSERT(!instanceBuffer || !instanceBuffer->isCpuBuffer());
870 SkASSERT(!indexBuffer->isCpuBuffer());
871 auto gpuIndexxBuffer = static_cast<const GrGpuBuffer*>(indexBuffer);
872 auto gpuVertexBuffer = static_cast<const GrGpuBuffer*>(vertexBuffer);
873 auto gpuInstanceBuffer = static_cast<const GrGpuBuffer*>(instanceBuffer);
874 this->bindGeometry(gpuIndexxBuffer, gpuVertexBuffer, gpuInstanceBuffer);
Chris Dalton1d616352017-05-31 12:51:23 -0600875 cbInfo.currentCmdBuf()->drawIndexed(fGpu, indexCount, instanceCount,
876 baseIndex, baseVertex, baseInstance);
Chris Dalton114a3c02017-05-26 15:17:19 -0600877 fGpu->stats()->incNumDraws();
878}
Greg Daniel64cc9aa2018-10-19 13:54:56 -0400879
880////////////////////////////////////////////////////////////////////////////////
881
882void GrVkGpuRTCommandBuffer::executeDrawable(std::unique_ptr<SkDrawable::GpuDrawHandler> drawable) {
883 GrVkRenderTarget* target = static_cast<GrVkRenderTarget*>(fRenderTarget);
884
885 GrVkImage* targetImage = target->msaaImage() ? target->msaaImage() : target;
886
887 CommandBufferInfo& cbInfo = fCommandBufferInfos[fCurrentCmdInfo];
888 VkRect2D bounds;
889 bounds.offset = { 0, 0 };
890 bounds.extent = { 0, 0 };
891
892 GrVkDrawableInfo vkInfo;
893 vkInfo.fSecondaryCommandBuffer = cbInfo.currentCmdBuf()->vkCommandBuffer();
894 vkInfo.fCompatibleRenderPass = cbInfo.fRenderPass->vkRenderPass();
Greg Danielb353eeb2018-12-05 11:01:58 -0500895 SkAssertResult(cbInfo.fRenderPass->colorAttachmentIndex(&vkInfo.fColorAttachmentIndex));
Greg Daniel64cc9aa2018-10-19 13:54:56 -0400896 vkInfo.fFormat = targetImage->imageFormat();
897 vkInfo.fDrawBounds = &bounds;
Stan Ilievcb580602019-02-26 11:36:07 -0500898#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
899 vkInfo.fImage = targetImage->image();
900#else
901 vkInfo.fImage = VK_NULL_HANDLE;
902#endif //SK_BUILD_FOR_ANDROID_FRAMEWORK
Greg Daniel64cc9aa2018-10-19 13:54:56 -0400903
904 GrBackendDrawableInfo info(vkInfo);
905
Eric Karlc0b2ba22019-01-22 19:40:35 -0800906 // After we draw into the command buffer via the drawable, cached state we have may be invalid.
907 cbInfo.currentCmdBuf()->invalidateState();
Eric Karla8878a12019-02-07 18:17:43 -0800908 // Also assume that the drawable produced output.
909 cbInfo.fIsEmpty = false;
Eric Karlc0b2ba22019-01-22 19:40:35 -0800910
Greg Daniel64cc9aa2018-10-19 13:54:56 -0400911 drawable->draw(info);
912 fGpu->addDrawable(std::move(drawable));
913
914 if (bounds.extent.width == 0 || bounds.extent.height == 0) {
915 cbInfo.fBounds.join(target->getBoundsRect());
916 } else {
917 cbInfo.fBounds.join(SkRect::MakeXYWH(bounds.offset.x, bounds.offset.y,
918 bounds.extent.width, bounds.extent.height));
919 }
920}
921