blob: efac60289c41cd65959d0a84f0aa1b2614472e45 [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();
Greg Daniel8daf3b72019-07-30 09:57:26 -0400204 std::unique_ptr<GrVkSecondaryCommandBuffer> scb(
205 GrVkSecondaryCommandBuffer::Create(vkRT->getExternalSecondaryCommandBuffer()));
206 cbInfo.fCommandBuffers.push_back(std::move(scb));
Greg Daniel070cbaf2019-01-03 17:35:54 -0500207 cbInfo.currentCmdBuf()->begin(fGpu, nullptr, cbInfo.fRenderPass);
208}
Brian Salomonc293a292016-11-30 13:38:32 -0500209
Greg Daniel500d58b2017-08-24 15:59:33 -0400210GrVkGpuRTCommandBuffer::~GrVkGpuRTCommandBuffer() {
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400211 this->reset();
egdaniel066df7c2016-06-08 14:02:27 -0700212}
213
Greg Daniel500d58b2017-08-24 15:59:33 -0400214GrGpu* GrVkGpuRTCommandBuffer::gpu() { return fGpu; }
egdaniel9cb63402016-06-23 08:37:05 -0700215
Greg Daniel500d58b2017-08-24 15:59:33 -0400216void GrVkGpuRTCommandBuffer::end() {
Greg Daniel22bc8652017-03-22 15:45:43 -0400217 if (fCurrentCmdInfo >= 0) {
218 fCommandBufferInfos[fCurrentCmdInfo].currentCmdBuf()->end(fGpu);
Brian Salomonc293a292016-11-30 13:38:32 -0500219 }
egdaniel066df7c2016-06-08 14:02:27 -0700220}
221
Greg Daniel500d58b2017-08-24 15:59:33 -0400222void GrVkGpuRTCommandBuffer::submit() {
Brian Salomonc293a292016-11-30 13:38:32 -0500223 if (!fRenderTarget) {
224 return;
225 }
Robert Phillips19e51dc2017-08-09 09:30:51 -0400226
227 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(fRenderTarget);
Robert Phillips19e51dc2017-08-09 09:30:51 -0400228 GrVkImage* targetImage = vkRT->msaaImage() ? vkRT->msaaImage() : vkRT;
Greg Daniel45a44de2018-02-27 10:07:29 -0500229 GrStencilAttachment* stencil = fRenderTarget->renderTargetPriv().getStencilAttachment();
Brian Salomon24d377e2019-04-23 15:24:31 -0400230 auto currPreCmd = fPreCommandBufferTasks.begin();
egdaniel9cb63402016-06-23 08:37:05 -0700231
Greg Daniel46cfbc62019-06-07 11:43:30 -0400232 GrVkPrimaryCommandBufferTask::Args taskArgs{fGpu, fRenderTarget};
Greg Daniel36a77ee2016-10-18 10:33:25 -0400233 for (int i = 0; i < fCommandBufferInfos.count(); ++i) {
234 CommandBufferInfo& cbInfo = fCommandBufferInfos[i];
235
Brian Salomon24d377e2019-04-23 15:24:31 -0400236 for (int c = 0; c < cbInfo.fNumPreCmds; ++c, ++currPreCmd) {
Brian Salomon5d8f1cc2019-04-24 09:03:53 -0400237 currPreCmd->execute(taskArgs);
Greg Daniel77b53f62016-10-18 11:48:51 -0400238 }
239
Greg Daniel38c3d932018-03-16 14:22:30 -0400240 // TODO: Many things create a scratch texture which adds the discard immediately, but then
241 // don't draw to it right away. This causes the discard to be ignored and we get yelled at
242 // for loading uninitialized data. However, once MDB lands with reordering, the discard will
243 // get reordered with the rest of the draw commands and we can remove the discard check.
244 if (cbInfo.fIsEmpty &&
245 cbInfo.fLoadStoreState != LoadStoreState::kStartsWithClear &&
246 cbInfo.fLoadStoreState != LoadStoreState::kStartsWithDiscard) {
Greg Daniel77b53f62016-10-18 11:48:51 -0400247 // We have sumbitted no actual draw commands to the command buffer and we are not using
248 // the render pass to do a clear so there is no need to submit anything.
249 continue;
250 }
Greg Daniel38c3d932018-03-16 14:22:30 -0400251
Greg Daniel070cbaf2019-01-03 17:35:54 -0500252 // We don't want to actually submit the secondary command buffer if it is wrapped.
253 if (this->wrapsSecondaryCommandBuffer()) {
254 // If we have any sampled images set their layout now.
Chris Dalton298238a2019-02-21 16:28:44 -0500255 for (int j = 0; j < cbInfo.fSampledTextures.count(); ++j) {
256 cbInfo.fSampledTextures[j]->setImageLayout(
257 fGpu, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, VK_ACCESS_SHADER_READ_BIT,
258 VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT, false);
Greg Daniel070cbaf2019-01-03 17:35:54 -0500259 }
260
261 // There should have only been one secondary command buffer in the wrapped case so it is
262 // safe to just return here.
263 SkASSERT(fCommandBufferInfos.count() == 1);
264 return;
265 }
266
Greg Danieldbdba602018-04-20 11:52:43 -0400267 // Make sure if we only have a discard load that we execute the discard on the whole image.
268 // TODO: Once we improve our tracking of discards so that we never end up flushing a discard
269 // call with no actually ops, remove this.
270 if (cbInfo.fIsEmpty && cbInfo.fLoadStoreState == LoadStoreState::kStartsWithDiscard) {
Greg Daniela41a74a2018-10-09 12:59:23 +0000271 cbInfo.fBounds = SkRect::MakeWH(vkRT->width(), vkRT->height());
Greg Danieldbdba602018-04-20 11:52:43 -0400272 }
273
Greg Daniela41a74a2018-10-09 12:59:23 +0000274 if (cbInfo.fBounds.intersect(0, 0,
275 SkIntToScalar(fRenderTarget->width()),
276 SkIntToScalar(fRenderTarget->height()))) {
Greg Daniel38c3d932018-03-16 14:22:30 -0400277 // Make sure we do the following layout changes after all copies, uploads, or any other
278 // pre-work is done since we may change the layouts in the pre-work. Also since the
279 // draws will be submitted in different render passes, we need to guard againts write
280 // and write issues.
281
282 // Change layout of our render target so it can be used as the color attachment.
Greg Danielf7828d02018-10-09 12:01:32 -0400283 // TODO: If we know that we will never be blending or loading the attachment we could
284 // drop the VK_ACCESS_COLOR_ATTACHMENT_READ_BIT.
Greg Daniel38c3d932018-03-16 14:22:30 -0400285 targetImage->setImageLayout(fGpu,
286 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Greg Danielf7828d02018-10-09 12:01:32 -0400287 VK_ACCESS_COLOR_ATTACHMENT_READ_BIT |
Greg Daniel38c3d932018-03-16 14:22:30 -0400288 VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
Greg Danielf7828d02018-10-09 12:01:32 -0400289 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
Greg Daniel38c3d932018-03-16 14:22:30 -0400290 false);
291
292 // If we are using a stencil attachment we also need to update its layout
293 if (stencil) {
294 GrVkStencilAttachment* vkStencil = (GrVkStencilAttachment*)stencil;
Greg Danielf7828d02018-10-09 12:01:32 -0400295 // We need the write and read access bits since we may load and store the stencil.
296 // The initial load happens in the VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT so we
297 // wait there.
Greg Daniel38c3d932018-03-16 14:22:30 -0400298 vkStencil->setImageLayout(fGpu,
299 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
300 VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT |
301 VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT,
Greg Danielf7828d02018-10-09 12:01:32 -0400302 VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT,
Greg Daniel38c3d932018-03-16 14:22:30 -0400303 false);
304 }
305
306 // If we have any sampled images set their layout now.
Chris Dalton298238a2019-02-21 16:28:44 -0500307 for (int j = 0; j < cbInfo.fSampledTextures.count(); ++j) {
308 cbInfo.fSampledTextures[j]->setImageLayout(
309 fGpu, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, VK_ACCESS_SHADER_READ_BIT,
310 VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT, false);
Greg Daniel38c3d932018-03-16 14:22:30 -0400311 }
312
Greg Daniel36a77ee2016-10-18 10:33:25 -0400313 SkIRect iBounds;
314 cbInfo.fBounds.roundOut(&iBounds);
315
Greg Daniel22bc8652017-03-22 15:45:43 -0400316 fGpu->submitSecondaryCommandBuffer(cbInfo.fCommandBuffers, cbInfo.fRenderPass,
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400317 &cbInfo.fColorClearValue, vkRT, fOrigin, iBounds);
Greg Daniel36a77ee2016-10-18 10:33:25 -0400318 }
319 }
Brian Salomon24d377e2019-04-23 15:24:31 -0400320 SkASSERT(currPreCmd == fPreCommandBufferTasks.end());
egdaniel9cb63402016-06-23 08:37:05 -0700321}
322
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400323void GrVkGpuRTCommandBuffer::set(GrRenderTarget* rt, GrSurfaceOrigin origin,
324 const GrGpuRTCommandBuffer::LoadAndStoreInfo& colorInfo,
325 const GrGpuRTCommandBuffer::StencilLoadAndStoreInfo& stencilInfo) {
326 SkASSERT(!fRenderTarget);
327 SkASSERT(fCommandBufferInfos.empty());
328 SkASSERT(-1 == fCurrentCmdInfo);
Robert Phillips9da87e02019-02-04 13:26:26 -0500329 SkASSERT(fGpu == rt->getContext()->priv().getGpu());
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400330 SkASSERT(!fLastPipelineState);
331
Greg Danielb0c7ad12019-06-06 17:23:35 +0000332#ifdef SK_DEBUG
333 fIsActive = true;
334#endif
335
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400336 this->INHERITED::set(rt, origin);
337
Greg Daniel070cbaf2019-01-03 17:35:54 -0500338 if (this->wrapsSecondaryCommandBuffer()) {
339 this->initWrapped();
340 return;
341 }
342
Brian Osman9a9baae2018-11-05 15:06:26 -0500343 fClearColor = colorInfo.fClearColor;
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400344
345 get_vk_load_store_ops(colorInfo.fLoadOp, colorInfo.fStoreOp,
346 &fVkColorLoadOp, &fVkColorStoreOp);
347
348 get_vk_load_store_ops(stencilInfo.fLoadOp, stencilInfo.fStoreOp,
349 &fVkStencilLoadOp, &fVkStencilStoreOp);
350
351 this->init();
352}
353
354void GrVkGpuRTCommandBuffer::reset() {
355 for (int i = 0; i < fCommandBufferInfos.count(); ++i) {
356 CommandBufferInfo& cbInfo = fCommandBufferInfos[i];
357 for (int j = 0; j < cbInfo.fCommandBuffers.count(); ++j) {
Greg Daniel8daf3b72019-07-30 09:57:26 -0400358 if (cbInfo.fCommandBuffers[j]) {
359 cbInfo.fCommandBuffers[j].release()->recycle(fGpu);
360 }
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400361 }
362 cbInfo.fRenderPass->unref(fGpu);
363 }
364 fCommandBufferInfos.reset();
Brian Salomon24d377e2019-04-23 15:24:31 -0400365 fPreCommandBufferTasks.reset();
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400366
367 fCurrentCmdInfo = -1;
368
369 fLastPipelineState = nullptr;
370 fRenderTarget = nullptr;
Greg Danielb0c7ad12019-06-06 17:23:35 +0000371
372#ifdef SK_DEBUG
373 fIsActive = false;
374#endif
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400375}
376
Greg Daniel070cbaf2019-01-03 17:35:54 -0500377bool GrVkGpuRTCommandBuffer::wrapsSecondaryCommandBuffer() const {
378 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(fRenderTarget);
379 return vkRT->wrapsSecondaryCommandBuffer();
380}
381
Greg Daniel64cc9aa2018-10-19 13:54:56 -0400382////////////////////////////////////////////////////////////////////////////////
383
Greg Daniel500d58b2017-08-24 15:59:33 -0400384void GrVkGpuRTCommandBuffer::insertEventMarker(const char* msg) {
Robert Phillips65a88fa2017-08-08 08:36:22 -0400385 // TODO: does Vulkan have a correlate?
386}
387
Greg Daniel500d58b2017-08-24 15:59:33 -0400388void GrVkGpuRTCommandBuffer::onClearStencilClip(const GrFixedClip& clip, bool insideStencilMask) {
Chris Dalton94c04682017-11-01 17:15:06 -0600389 SkASSERT(!clip.hasWindowRectangles());
390
Jim Van Verth6a40abc2017-11-02 16:56:09 +0000391 CommandBufferInfo& cbInfo = fCommandBufferInfos[fCurrentCmdInfo];
392
Greg Daniel65a09272016-10-12 09:47:22 -0400393 GrStencilAttachment* sb = fRenderTarget->renderTargetPriv().getStencilAttachment();
egdaniel9cb63402016-06-23 08:37:05 -0700394 // this should only be called internally when we know we have a
395 // stencil buffer.
396 SkASSERT(sb);
397 int stencilBitCount = sb->bits();
398
399 // The contract with the callers does not guarantee that we preserve all bits in the stencil
400 // during this clear. Thus we will clear the entire stencil to the desired value.
401
402 VkClearDepthStencilValue vkStencilColor;
403 memset(&vkStencilColor, 0, sizeof(VkClearDepthStencilValue));
csmartdalton29df7602016-08-31 11:55:52 -0700404 if (insideStencilMask) {
egdaniel9cb63402016-06-23 08:37:05 -0700405 vkStencilColor.stencil = (1 << (stencilBitCount - 1));
406 } else {
407 vkStencilColor.stencil = 0;
408 }
409
410 VkClearRect clearRect;
411 // Flip rect if necessary
csmartdalton29df7602016-08-31 11:55:52 -0700412 SkIRect vkRect;
Brian Salomond818ebf2018-07-02 14:08:49 +0000413 if (!clip.scissorEnabled()) {
Greg Daniela41a74a2018-10-09 12:59:23 +0000414 vkRect.setXYWH(0, 0, fRenderTarget->width(), fRenderTarget->height());
Robert Phillips4f101a72017-07-28 08:42:04 -0400415 } else if (kBottomLeft_GrSurfaceOrigin != fOrigin) {
csmartdalton29df7602016-08-31 11:55:52 -0700416 vkRect = clip.scissorRect();
417 } else {
418 const SkIRect& scissor = clip.scissorRect();
Greg Daniel65a09272016-10-12 09:47:22 -0400419 vkRect.setLTRB(scissor.fLeft, fRenderTarget->height() - scissor.fBottom,
420 scissor.fRight, fRenderTarget->height() - scissor.fTop);
egdaniel9cb63402016-06-23 08:37:05 -0700421 }
422
423 clearRect.rect.offset = { vkRect.fLeft, vkRect.fTop };
424 clearRect.rect.extent = { (uint32_t)vkRect.width(), (uint32_t)vkRect.height() };
425
426 clearRect.baseArrayLayer = 0;
427 clearRect.layerCount = 1;
428
429 uint32_t stencilIndex;
Greg Daniel36a77ee2016-10-18 10:33:25 -0400430 SkAssertResult(cbInfo.fRenderPass->stencilAttachmentIndex(&stencilIndex));
egdaniel9cb63402016-06-23 08:37:05 -0700431
432 VkClearAttachment attachment;
433 attachment.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
434 attachment.colorAttachment = 0; // this value shouldn't matter
435 attachment.clearValue.depthStencil = vkStencilColor;
436
Greg Daniel22bc8652017-03-22 15:45:43 -0400437 cbInfo.currentCmdBuf()->clearAttachments(fGpu, 1, &attachment, 1, &clearRect);
Greg Daniel77b53f62016-10-18 11:48:51 -0400438 cbInfo.fIsEmpty = false;
Greg Daniel36a77ee2016-10-18 10:33:25 -0400439
440 // Update command buffer bounds
Brian Salomond818ebf2018-07-02 14:08:49 +0000441 if (!clip.scissorEnabled()) {
Greg Daniel36a77ee2016-10-18 10:33:25 -0400442 cbInfo.fBounds.join(fRenderTarget->getBoundsRect());
443 } else {
444 cbInfo.fBounds.join(SkRect::Make(clip.scissorRect()));
445 }
egdaniel9cb63402016-06-23 08:37:05 -0700446}
447
Brian Osman9a9baae2018-11-05 15:06:26 -0500448void GrVkGpuRTCommandBuffer::onClear(const GrFixedClip& clip, const SkPMColor4f& color) {
Robert Phillips19e51dc2017-08-09 09:30:51 -0400449 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(fRenderTarget);
450
Jim Van Verth6a40abc2017-11-02 16:56:09 +0000451 // parent class should never let us get here with no RT
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700452 SkASSERT(!clip.hasWindowRectangles());
egdaniel9cb63402016-06-23 08:37:05 -0700453
Greg Daniel22bc8652017-03-22 15:45:43 -0400454 CommandBufferInfo& cbInfo = fCommandBufferInfos[fCurrentCmdInfo];
Greg Daniel36a77ee2016-10-18 10:33:25 -0400455
Brian Osman9a9baae2018-11-05 15:06:26 -0500456 VkClearColorValue vkColor = {{color.fR, color.fG, color.fB, color.fA}};
egdaniel9cb63402016-06-23 08:37:05 -0700457
Brian Salomond818ebf2018-07-02 14:08:49 +0000458 if (cbInfo.fIsEmpty && !clip.scissorEnabled()) {
Robert Phillips74c627f2017-08-09 10:28:00 -0400459 // Change the render pass to do a clear load
egdaniel9cb63402016-06-23 08:37:05 -0700460 GrVkRenderPass::LoadStoreOps vkColorOps(VK_ATTACHMENT_LOAD_OP_CLEAR,
461 VK_ATTACHMENT_STORE_OP_STORE);
Robert Phillips74c627f2017-08-09 10:28:00 -0400462 // Preserve the stencil buffer's load & store settings
463 GrVkRenderPass::LoadStoreOps vkStencilOps(fVkStencilLoadOp, fVkStencilStoreOp);
egdaniel9cb63402016-06-23 08:37:05 -0700464
Greg Daniel36a77ee2016-10-18 10:33:25 -0400465 const GrVkRenderPass* oldRP = cbInfo.fRenderPass;
egdaniel9cb63402016-06-23 08:37:05 -0700466
467 const GrVkResourceProvider::CompatibleRPHandle& rpHandle =
Robert Phillips19e51dc2017-08-09 09:30:51 -0400468 vkRT->compatibleRenderPassHandle();
egdaniel9cb63402016-06-23 08:37:05 -0700469 if (rpHandle.isValid()) {
Greg Daniel36a77ee2016-10-18 10:33:25 -0400470 cbInfo.fRenderPass = fGpu->resourceProvider().findRenderPass(rpHandle,
471 vkColorOps,
472 vkStencilOps);
egdaniel9cb63402016-06-23 08:37:05 -0700473 } else {
Robert Phillips19e51dc2017-08-09 09:30:51 -0400474 cbInfo.fRenderPass = fGpu->resourceProvider().findRenderPass(*vkRT,
Greg Daniel36a77ee2016-10-18 10:33:25 -0400475 vkColorOps,
476 vkStencilOps);
egdaniel9cb63402016-06-23 08:37:05 -0700477 }
478
Greg Daniel36a77ee2016-10-18 10:33:25 -0400479 SkASSERT(cbInfo.fRenderPass->isCompatible(*oldRP));
egdaniel9cb63402016-06-23 08:37:05 -0700480 oldRP->unref(fGpu);
481
Brian Osman9a9baae2018-11-05 15:06:26 -0500482 cbInfo.fColorClearValue.color = {{color.fR, color.fG, color.fB, color.fA}};
Greg Daniela3c68df2018-03-16 13:46:53 -0400483 cbInfo.fLoadStoreState = LoadStoreState::kStartsWithClear;
Greg Daniel36a77ee2016-10-18 10:33:25 -0400484 // Update command buffer bounds
485 cbInfo.fBounds.join(fRenderTarget->getBoundsRect());
egdaniel9cb63402016-06-23 08:37:05 -0700486 return;
487 }
488
489 // We always do a sub rect clear with clearAttachments since we are inside a render pass
490 VkClearRect clearRect;
491 // Flip rect if necessary
csmartdalton29df7602016-08-31 11:55:52 -0700492 SkIRect vkRect;
Brian Salomond818ebf2018-07-02 14:08:49 +0000493 if (!clip.scissorEnabled()) {
Greg Daniela41a74a2018-10-09 12:59:23 +0000494 vkRect.setXYWH(0, 0, fRenderTarget->width(), fRenderTarget->height());
Robert Phillips4f101a72017-07-28 08:42:04 -0400495 } else if (kBottomLeft_GrSurfaceOrigin != fOrigin) {
csmartdalton29df7602016-08-31 11:55:52 -0700496 vkRect = clip.scissorRect();
497 } else {
498 const SkIRect& scissor = clip.scissorRect();
Greg Daniel65a09272016-10-12 09:47:22 -0400499 vkRect.setLTRB(scissor.fLeft, fRenderTarget->height() - scissor.fBottom,
500 scissor.fRight, fRenderTarget->height() - scissor.fTop);
egdaniel9cb63402016-06-23 08:37:05 -0700501 }
502 clearRect.rect.offset = { vkRect.fLeft, vkRect.fTop };
503 clearRect.rect.extent = { (uint32_t)vkRect.width(), (uint32_t)vkRect.height() };
504 clearRect.baseArrayLayer = 0;
505 clearRect.layerCount = 1;
506
507 uint32_t colorIndex;
Greg Daniel36a77ee2016-10-18 10:33:25 -0400508 SkAssertResult(cbInfo.fRenderPass->colorAttachmentIndex(&colorIndex));
egdaniel9cb63402016-06-23 08:37:05 -0700509
510 VkClearAttachment attachment;
511 attachment.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
512 attachment.colorAttachment = colorIndex;
513 attachment.clearValue.color = vkColor;
514
Greg Daniel22bc8652017-03-22 15:45:43 -0400515 cbInfo.currentCmdBuf()->clearAttachments(fGpu, 1, &attachment, 1, &clearRect);
Greg Daniel77b53f62016-10-18 11:48:51 -0400516 cbInfo.fIsEmpty = false;
Greg Daniel36a77ee2016-10-18 10:33:25 -0400517
518 // Update command buffer bounds
Brian Salomond818ebf2018-07-02 14:08:49 +0000519 if (!clip.scissorEnabled()) {
Greg Daniel36a77ee2016-10-18 10:33:25 -0400520 cbInfo.fBounds.join(fRenderTarget->getBoundsRect());
521 } else {
522 cbInfo.fBounds.join(SkRect::Make(clip.scissorRect()));
523 }
egdaniel9cb63402016-06-23 08:37:05 -0700524 return;
525}
526
Greg Daniel500d58b2017-08-24 15:59:33 -0400527////////////////////////////////////////////////////////////////////////////////
528
529void GrVkGpuRTCommandBuffer::addAdditionalCommandBuffer() {
Robert Phillips19e51dc2017-08-09 09:30:51 -0400530 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(fRenderTarget);
531
Greg Daniel22bc8652017-03-22 15:45:43 -0400532 CommandBufferInfo& cbInfo = fCommandBufferInfos[fCurrentCmdInfo];
533 cbInfo.currentCmdBuf()->end(fGpu);
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500534 cbInfo.fCommandBuffers.push_back(fGpu->cmdPool()->findOrCreateSecondaryCommandBuffer(fGpu));
Robert Phillips19e51dc2017-08-09 09:30:51 -0400535 cbInfo.currentCmdBuf()->begin(fGpu, vkRT->framebuffer(), cbInfo.fRenderPass);
Greg Daniel22bc8652017-03-22 15:45:43 -0400536}
537
Greg Daniel500d58b2017-08-24 15:59:33 -0400538void GrVkGpuRTCommandBuffer::addAdditionalRenderPass() {
Robert Phillips19e51dc2017-08-09 09:30:51 -0400539 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(fRenderTarget);
540
Greg Daniel22bc8652017-03-22 15:45:43 -0400541 fCommandBufferInfos[fCurrentCmdInfo].currentCmdBuf()->end(fGpu);
Greg Daniel77b53f62016-10-18 11:48:51 -0400542
543 CommandBufferInfo& cbInfo = fCommandBufferInfos.push_back();
Greg Daniel22bc8652017-03-22 15:45:43 -0400544 fCurrentCmdInfo++;
Greg Daniel77b53f62016-10-18 11:48:51 -0400545
546 GrVkRenderPass::LoadStoreOps vkColorOps(VK_ATTACHMENT_LOAD_OP_LOAD,
547 VK_ATTACHMENT_STORE_OP_STORE);
548 GrVkRenderPass::LoadStoreOps vkStencilOps(VK_ATTACHMENT_LOAD_OP_LOAD,
549 VK_ATTACHMENT_STORE_OP_STORE);
550
551 const GrVkResourceProvider::CompatibleRPHandle& rpHandle =
Robert Phillips19e51dc2017-08-09 09:30:51 -0400552 vkRT->compatibleRenderPassHandle();
Greg Daniel77b53f62016-10-18 11:48:51 -0400553 if (rpHandle.isValid()) {
554 cbInfo.fRenderPass = fGpu->resourceProvider().findRenderPass(rpHandle,
555 vkColorOps,
556 vkStencilOps);
557 } else {
Robert Phillips19e51dc2017-08-09 09:30:51 -0400558 cbInfo.fRenderPass = fGpu->resourceProvider().findRenderPass(*vkRT,
Greg Daniel77b53f62016-10-18 11:48:51 -0400559 vkColorOps,
560 vkStencilOps);
561 }
Greg Daniela3c68df2018-03-16 13:46:53 -0400562 cbInfo.fLoadStoreState = LoadStoreState::kLoadAndStore;
Greg Daniel77b53f62016-10-18 11:48:51 -0400563
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500564 cbInfo.fCommandBuffers.push_back(fGpu->cmdPool()->findOrCreateSecondaryCommandBuffer(fGpu));
Greg Daniel77b53f62016-10-18 11:48:51 -0400565 // It shouldn't matter what we set the clear color to here since we will assume loading of the
566 // attachment.
567 memset(&cbInfo.fColorClearValue, 0, sizeof(VkClearValue));
568 cbInfo.fBounds.setEmpty();
Greg Daniel77b53f62016-10-18 11:48:51 -0400569
Robert Phillips19e51dc2017-08-09 09:30:51 -0400570 cbInfo.currentCmdBuf()->begin(fGpu, vkRT->framebuffer(), cbInfo.fRenderPass);
Greg Daniel77b53f62016-10-18 11:48:51 -0400571}
572
Brian Salomon943ed792017-10-30 09:37:55 -0400573void GrVkGpuRTCommandBuffer::inlineUpload(GrOpFlushState* state,
574 GrDeferredTextureUploadFn& upload) {
Greg Daniel22bc8652017-03-22 15:45:43 -0400575 if (!fCommandBufferInfos[fCurrentCmdInfo].fIsEmpty) {
576 this->addAdditionalRenderPass();
Greg Daniel77b53f62016-10-18 11:48:51 -0400577 }
Brian Salomon24d377e2019-04-23 15:24:31 -0400578
Brian Salomon24d377e2019-04-23 15:24:31 -0400579 fPreCommandBufferTasks.emplace<InlineUpload>(state, upload);
580 ++fCommandBufferInfos[fCurrentCmdInfo].fNumPreCmds;
Greg Daniel77b53f62016-10-18 11:48:51 -0400581}
582
Greg Daniel46cfbc62019-06-07 11:43:30 -0400583void GrVkGpuRTCommandBuffer::copy(GrSurface* src, const SkIRect& srcRect,
Greg Daniel500d58b2017-08-24 15:59:33 -0400584 const SkIPoint& dstPoint) {
Greg Daniela3c68df2018-03-16 13:46:53 -0400585 CommandBufferInfo& cbInfo = fCommandBufferInfos[fCurrentCmdInfo];
586 if (!cbInfo.fIsEmpty || LoadStoreState::kStartsWithClear == cbInfo.fLoadStoreState) {
Greg Daniel500d58b2017-08-24 15:59:33 -0400587 this->addAdditionalRenderPass();
588 }
Greg Daniela3c68df2018-03-16 13:46:53 -0400589
Brian Salomon24d377e2019-04-23 15:24:31 -0400590 fPreCommandBufferTasks.emplace<Copy>(
Greg Daniel46cfbc62019-06-07 11:43:30 -0400591 src, srcRect, dstPoint, LoadStoreState::kStartsWithDiscard == cbInfo.fLoadStoreState);
Brian Salomon24d377e2019-04-23 15:24:31 -0400592 ++fCommandBufferInfos[fCurrentCmdInfo].fNumPreCmds;
Greg Daniel55fa6472018-03-16 16:13:10 -0400593
Greg Daniela3c68df2018-03-16 13:46:53 -0400594 if (LoadStoreState::kLoadAndStore != cbInfo.fLoadStoreState) {
595 // Change the render pass to do a load and store so we don't lose the results of our copy
596 GrVkRenderPass::LoadStoreOps vkColorOps(VK_ATTACHMENT_LOAD_OP_LOAD,
597 VK_ATTACHMENT_STORE_OP_STORE);
598 GrVkRenderPass::LoadStoreOps vkStencilOps(VK_ATTACHMENT_LOAD_OP_LOAD,
599 VK_ATTACHMENT_STORE_OP_STORE);
600
601 const GrVkRenderPass* oldRP = cbInfo.fRenderPass;
602
603 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(fRenderTarget);
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400604 SkASSERT(!src->isProtected() || (fRenderTarget->isProtected() && fGpu->protectedContext()));
Greg Daniela3c68df2018-03-16 13:46:53 -0400605 const GrVkResourceProvider::CompatibleRPHandle& rpHandle =
606 vkRT->compatibleRenderPassHandle();
607 if (rpHandle.isValid()) {
608 cbInfo.fRenderPass = fGpu->resourceProvider().findRenderPass(rpHandle,
609 vkColorOps,
610 vkStencilOps);
611 } else {
612 cbInfo.fRenderPass = fGpu->resourceProvider().findRenderPass(*vkRT,
613 vkColorOps,
614 vkStencilOps);
615 }
616 SkASSERT(cbInfo.fRenderPass->isCompatible(*oldRP));
617 oldRP->unref(fGpu);
618
619 cbInfo.fLoadStoreState = LoadStoreState::kLoadAndStore;
620
621 }
Greg Daniel500d58b2017-08-24 15:59:33 -0400622}
623
Brian Salomonab32f652019-05-10 14:24:50 -0400624void GrVkGpuRTCommandBuffer::transferFrom(const SkIRect& srcRect, GrColorType bufferColorType,
625 GrGpuBuffer* transferBuffer, size_t offset) {
626 CommandBufferInfo& cbInfo = fCommandBufferInfos[fCurrentCmdInfo];
627 if (!cbInfo.fIsEmpty) {
628 this->addAdditionalRenderPass();
629 }
630 fPreCommandBufferTasks.emplace<TransferFrom>(srcRect, bufferColorType, transferBuffer, offset);
631 ++fCommandBufferInfos[fCurrentCmdInfo].fNumPreCmds;
632}
633
egdaniel9cb63402016-06-23 08:37:05 -0700634////////////////////////////////////////////////////////////////////////////////
635
Brian Salomondbf70722019-02-07 11:31:24 -0500636void GrVkGpuRTCommandBuffer::bindGeometry(const GrGpuBuffer* indexBuffer,
637 const GrGpuBuffer* vertexBuffer,
638 const GrGpuBuffer* instanceBuffer) {
Chris Daltonff926502017-05-03 14:36:54 -0400639 GrVkSecondaryCommandBuffer* currCmdBuf = fCommandBufferInfos[fCurrentCmdInfo].currentCmdBuf();
egdaniel9cb63402016-06-23 08:37:05 -0700640 // There is no need to put any memory barriers to make sure host writes have finished here.
641 // When a command buffer is submitted to a queue, there is an implicit memory barrier that
642 // occurs for all host writes. Additionally, BufferMemoryBarriers are not allowed inside of
643 // an active RenderPass.
egdaniel9cb63402016-06-23 08:37:05 -0700644
Chris Dalton1d616352017-05-31 12:51:23 -0600645 // Here our vertex and instance inputs need to match the same 0-based bindings they were
646 // assigned in GrVkPipeline. That is, vertex first (if any) followed by instance.
647 uint32_t binding = 0;
648
Brian Salomon802cb312018-06-08 18:05:20 -0400649 if (vertexBuffer) {
Chris Dalton1d616352017-05-31 12:51:23 -0600650 SkASSERT(vertexBuffer);
Chris Dalton1d616352017-05-31 12:51:23 -0600651 SkASSERT(!vertexBuffer->isMapped());
652
653 currCmdBuf->bindInputBuffer(fGpu, binding++,
654 static_cast<const GrVkVertexBuffer*>(vertexBuffer));
655 }
656
Brian Salomon802cb312018-06-08 18:05:20 -0400657 if (instanceBuffer) {
Chris Dalton1d616352017-05-31 12:51:23 -0600658 SkASSERT(instanceBuffer);
Chris Dalton1d616352017-05-31 12:51:23 -0600659 SkASSERT(!instanceBuffer->isMapped());
660
661 currCmdBuf->bindInputBuffer(fGpu, binding++,
662 static_cast<const GrVkVertexBuffer*>(instanceBuffer));
663 }
Chris Daltonff926502017-05-03 14:36:54 -0400664 if (indexBuffer) {
665 SkASSERT(indexBuffer);
666 SkASSERT(!indexBuffer->isMapped());
egdaniel9cb63402016-06-23 08:37:05 -0700667
Chris Daltonff926502017-05-03 14:36:54 -0400668 currCmdBuf->bindIndexBuffer(fGpu, static_cast<const GrVkIndexBuffer*>(indexBuffer));
egdaniel9cb63402016-06-23 08:37:05 -0700669 }
670}
671
Brian Salomon49348902018-06-26 09:12:38 -0400672GrVkPipelineState* GrVkGpuRTCommandBuffer::prepareDrawState(
673 const GrPrimitiveProcessor& primProc,
674 const GrPipeline& pipeline,
675 const GrPipeline::FixedDynamicState* fixedDynamicState,
676 const GrPipeline::DynamicStateArrays* dynamicStateArrays,
677 GrPrimitiveType primitiveType) {
Greg Daniel22bc8652017-03-22 15:45:43 -0400678 CommandBufferInfo& cbInfo = fCommandBufferInfos[fCurrentCmdInfo];
679 SkASSERT(cbInfo.fRenderPass);
Greg Daniel36a77ee2016-10-18 10:33:25 -0400680
Greg Daniel99b88e02018-10-03 15:31:20 -0400681 VkRenderPass compatibleRenderPass = cbInfo.fRenderPass->vkRenderPass();
682
Greg Daniel9a51a862018-11-30 10:18:14 -0500683 const GrTextureProxy* const* primProcProxies = nullptr;
684 if (dynamicStateArrays && dynamicStateArrays->fPrimitiveProcessorTextures) {
685 primProcProxies = dynamicStateArrays->fPrimitiveProcessorTextures;
686 } else if (fixedDynamicState) {
687 primProcProxies = fixedDynamicState->fPrimitiveProcessorTextures;
688 }
689
690 SkASSERT(SkToBool(primProcProxies) == SkToBool(primProc.numTextureSamplers()));
691
Greg Daniel09eeefb2017-10-16 15:15:02 -0400692 GrVkPipelineState* pipelineState =
Robert Phillipsd0fe8752019-01-31 14:13:59 -0500693 fGpu->resourceProvider().findOrCreateCompatiblePipelineState(fRenderTarget, fOrigin,
694 pipeline,
egdaniel9cb63402016-06-23 08:37:05 -0700695 primProc,
Greg Daniel9a51a862018-11-30 10:18:14 -0500696 primProcProxies,
egdaniel9cb63402016-06-23 08:37:05 -0700697 primitiveType,
Greg Daniel99b88e02018-10-03 15:31:20 -0400698 compatibleRenderPass);
egdaniel9cb63402016-06-23 08:37:05 -0700699 if (!pipelineState) {
700 return pipelineState;
701 }
702
Greg Daniel22bc8652017-03-22 15:45:43 -0400703 if (!cbInfo.fIsEmpty &&
Greg Daniel09eeefb2017-10-16 15:15:02 -0400704 fLastPipelineState && fLastPipelineState != pipelineState &&
Greg Daniele3cd6912017-05-17 11:15:55 -0400705 fGpu->vkCaps().newCBOnPipelineChange()) {
Greg Daniel22bc8652017-03-22 15:45:43 -0400706 this->addAdditionalCommandBuffer();
707 }
Greg Daniel09eeefb2017-10-16 15:15:02 -0400708 fLastPipelineState = pipelineState;
Greg Daniel22bc8652017-03-22 15:45:43 -0400709
Brian Salomonf7232642018-09-19 08:58:08 -0400710 pipelineState->bindPipeline(fGpu, cbInfo.currentCmdBuf());
Brian Salomoncd7907b2018-08-30 08:36:18 -0400711
Robert Phillipsd0fe8752019-01-31 14:13:59 -0500712 pipelineState->setAndBindUniforms(fGpu, fRenderTarget, fOrigin,
713 primProc, pipeline, cbInfo.currentCmdBuf());
Brian Salomonf7232642018-09-19 08:58:08 -0400714
715 // Check whether we need to bind textures between each GrMesh. If not we can bind them all now.
716 bool setTextures = !(dynamicStateArrays && dynamicStateArrays->fPrimitiveProcessorTextures);
717 if (setTextures) {
Brian Salomonf7232642018-09-19 08:58:08 -0400718 pipelineState->setAndBindTextures(fGpu, primProc, pipeline, primProcProxies,
719 cbInfo.currentCmdBuf());
720 }
egdaniel9cb63402016-06-23 08:37:05 -0700721
Brian Salomond818ebf2018-07-02 14:08:49 +0000722 if (!pipeline.isScissorEnabled()) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400723 GrVkPipeline::SetDynamicScissorRectState(fGpu, cbInfo.currentCmdBuf(),
Robert Phillipsd0fe8752019-01-31 14:13:59 -0500724 fRenderTarget, fOrigin,
725 SkIRect::MakeWH(fRenderTarget->width(),
726 fRenderTarget->height()));
Brian Salomon49348902018-06-26 09:12:38 -0400727 } else if (!dynamicStateArrays || !dynamicStateArrays->fScissorRects) {
728 SkASSERT(fixedDynamicState);
Robert Phillipsd0fe8752019-01-31 14:13:59 -0500729 GrVkPipeline::SetDynamicScissorRectState(fGpu, cbInfo.currentCmdBuf(), fRenderTarget,
730 fOrigin,
Brian Salomon49348902018-06-26 09:12:38 -0400731 fixedDynamicState->fScissorRect);
Chris Dalton46983b72017-06-06 12:27:16 -0600732 }
Robert Phillipsd0fe8752019-01-31 14:13:59 -0500733 GrVkPipeline::SetDynamicViewportState(fGpu, cbInfo.currentCmdBuf(), fRenderTarget);
734 GrVkPipeline::SetDynamicBlendConstantState(fGpu, cbInfo.currentCmdBuf(),
Greg Daniel2c3398d2019-06-19 11:58:01 -0400735 pipeline.outputSwizzle(),
Chris Dalton46983b72017-06-06 12:27:16 -0600736 pipeline.getXferProcessor());
egdaniel9cb63402016-06-23 08:37:05 -0700737
738 return pipelineState;
739}
740
Brian Salomonff168d92018-06-23 15:17:27 -0400741void GrVkGpuRTCommandBuffer::onDraw(const GrPrimitiveProcessor& primProc,
742 const GrPipeline& pipeline,
Brian Salomon49348902018-06-26 09:12:38 -0400743 const GrPipeline::FixedDynamicState* fixedDynamicState,
744 const GrPipeline::DynamicStateArrays* dynamicStateArrays,
Greg Daniel500d58b2017-08-24 15:59:33 -0400745 const GrMesh meshes[],
Greg Daniel500d58b2017-08-24 15:59:33 -0400746 int meshCount,
747 const SkRect& bounds) {
egdaniel9cb63402016-06-23 08:37:05 -0700748 if (!meshCount) {
749 return;
750 }
Greg Danielea022cd2018-03-16 11:10:03 -0400751
752 CommandBufferInfo& cbInfo = fCommandBufferInfos[fCurrentCmdInfo];
753
Brian Salomone782f842018-07-31 13:53:11 -0400754 auto prepareSampledImage = [&](GrTexture* texture, GrSamplerState::Filter filter) {
755 GrVkTexture* vkTexture = static_cast<GrVkTexture*>(texture);
756 // We may need to resolve the texture first if it is also a render target
757 GrVkRenderTarget* texRT = static_cast<GrVkRenderTarget*>(vkTexture->asRenderTarget());
758 if (texRT) {
Greg Daniel0a77f432018-12-06 11:23:32 -0500759 fGpu->resolveRenderTargetNoFlush(texRT);
Brian Salomone782f842018-07-31 13:53:11 -0400760 }
761
762 // Check if we need to regenerate any mip maps
763 if (GrSamplerState::Filter::kMipMap == filter &&
764 (vkTexture->width() != 1 || vkTexture->height() != 1)) {
765 SkASSERT(vkTexture->texturePriv().mipMapped() == GrMipMapped::kYes);
766 if (vkTexture->texturePriv().mipMapsAreDirty()) {
767 fGpu->regenerateMipMapLevels(vkTexture);
768 }
769 }
Brian Salomon5fd10572019-04-01 12:07:05 -0400770 cbInfo.fSampledTextures.push_back(vkTexture);
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400771
772 SkASSERT(!texture->isProtected() ||
773 (fRenderTarget->isProtected() && fGpu->protectedContext()));
Brian Salomone782f842018-07-31 13:53:11 -0400774 };
775
Brian Salomonf7232642018-09-19 08:58:08 -0400776 if (dynamicStateArrays && dynamicStateArrays->fPrimitiveProcessorTextures) {
777 for (int m = 0, i = 0; m < meshCount; ++m) {
778 for (int s = 0; s < primProc.numTextureSamplers(); ++s, ++i) {
779 auto texture = dynamicStateArrays->fPrimitiveProcessorTextures[i]->peekTexture();
780 prepareSampledImage(texture, primProc.textureSampler(s).samplerState().filter());
781 }
782 }
783 } else {
784 for (int i = 0; i < primProc.numTextureSamplers(); ++i) {
785 auto texture = fixedDynamicState->fPrimitiveProcessorTextures[i]->peekTexture();
786 prepareSampledImage(texture, primProc.textureSampler(i).samplerState().filter());
787 }
Brian Salomone782f842018-07-31 13:53:11 -0400788 }
bsalomonb58a2b42016-09-26 06:55:02 -0700789 GrFragmentProcessor::Iter iter(pipeline);
790 while (const GrFragmentProcessor* fp = iter.next()) {
Brian Salomone782f842018-07-31 13:53:11 -0400791 for (int i = 0; i < fp->numTextureSamplers(); ++i) {
792 const GrFragmentProcessor::TextureSampler& sampler = fp->textureSampler(i);
793 prepareSampledImage(sampler.peekTexture(), sampler.samplerState().filter());
794 }
egdaniel2f5792a2016-07-06 08:51:23 -0700795 }
Robert Phillipsbb581ce2017-05-29 15:05:15 -0400796 if (GrTexture* dstTexture = pipeline.peekDstTexture()) {
Chris Dalton298238a2019-02-21 16:28:44 -0500797 cbInfo.fSampledTextures.push_back(sk_ref_sp(static_cast<GrVkTexture*>(dstTexture)));
Brian Salomon18dfa982017-04-03 16:57:43 -0400798 }
egdaniel2f5792a2016-07-06 08:51:23 -0700799
Chris Daltonbca46e22017-05-15 11:03:26 -0600800 GrPrimitiveType primitiveType = meshes[0].primitiveType();
Brian Salomon49348902018-06-26 09:12:38 -0400801 GrVkPipelineState* pipelineState = this->prepareDrawState(primProc, pipeline, fixedDynamicState,
802 dynamicStateArrays, primitiveType);
egdaniel9cb63402016-06-23 08:37:05 -0700803 if (!pipelineState) {
804 return;
805 }
806
Brian Salomond818ebf2018-07-02 14:08:49 +0000807 bool dynamicScissor =
808 pipeline.isScissorEnabled() && dynamicStateArrays && dynamicStateArrays->fScissorRects;
Brian Salomonf7232642018-09-19 08:58:08 -0400809 bool dynamicTextures = dynamicStateArrays && dynamicStateArrays->fPrimitiveProcessorTextures;
Brian Salomon49348902018-06-26 09:12:38 -0400810
egdaniel9cb63402016-06-23 08:37:05 -0700811 for (int i = 0; i < meshCount; ++i) {
812 const GrMesh& mesh = meshes[i];
Chris Daltonbca46e22017-05-15 11:03:26 -0600813 if (mesh.primitiveType() != primitiveType) {
Chris Dalton6f241802017-05-08 13:58:38 -0400814 SkDEBUGCODE(pipelineState = nullptr);
Chris Daltonbca46e22017-05-15 11:03:26 -0600815 primitiveType = mesh.primitiveType();
Brian Salomon49348902018-06-26 09:12:38 -0400816 pipelineState = this->prepareDrawState(primProc, pipeline, fixedDynamicState,
817 dynamicStateArrays, primitiveType);
Chris Dalton6f241802017-05-08 13:58:38 -0400818 if (!pipelineState) {
819 return;
egdaniel9cb63402016-06-23 08:37:05 -0700820 }
Chris Dalton6f241802017-05-08 13:58:38 -0400821 }
egdaniel9cb63402016-06-23 08:37:05 -0700822
Brian Salomon49348902018-06-26 09:12:38 -0400823 if (dynamicScissor) {
824 GrVkPipeline::SetDynamicScissorRectState(fGpu, cbInfo.currentCmdBuf(), fRenderTarget,
Robert Phillipsd0fe8752019-01-31 14:13:59 -0500825 fOrigin,
Brian Salomon49348902018-06-26 09:12:38 -0400826 dynamicStateArrays->fScissorRects[i]);
Chris Dalton46983b72017-06-06 12:27:16 -0600827 }
Brian Salomonf7232642018-09-19 08:58:08 -0400828 if (dynamicTextures) {
829 GrTextureProxy* const* meshProxies = dynamicStateArrays->fPrimitiveProcessorTextures +
830 primProc.numTextureSamplers() * i;
831 pipelineState->setAndBindTextures(fGpu, primProc, pipeline, meshProxies,
832 cbInfo.currentCmdBuf());
833 }
Chris Daltonbca46e22017-05-15 11:03:26 -0600834 SkASSERT(pipelineState);
Brian Salomon802cb312018-06-08 18:05:20 -0400835 mesh.sendToGpu(this);
egdaniel9cb63402016-06-23 08:37:05 -0700836 }
837
Greg Daniel36a77ee2016-10-18 10:33:25 -0400838 cbInfo.fBounds.join(bounds);
Chris Dalton114a3c02017-05-26 15:17:19 -0600839 cbInfo.fIsEmpty = false;
egdaniel066df7c2016-06-08 14:02:27 -0700840}
841
Brian Salomon802cb312018-06-08 18:05:20 -0400842void GrVkGpuRTCommandBuffer::sendInstancedMeshToGpu(GrPrimitiveType,
Greg Daniel500d58b2017-08-24 15:59:33 -0400843 const GrBuffer* vertexBuffer,
844 int vertexCount,
845 int baseVertex,
846 const GrBuffer* instanceBuffer,
847 int instanceCount,
848 int baseInstance) {
Chris Dalton114a3c02017-05-26 15:17:19 -0600849 CommandBufferInfo& cbInfo = fCommandBufferInfos[fCurrentCmdInfo];
Brian Salomondbf70722019-02-07 11:31:24 -0500850 SkASSERT(!vertexBuffer || !vertexBuffer->isCpuBuffer());
851 SkASSERT(!instanceBuffer || !instanceBuffer->isCpuBuffer());
852 auto gpuVertexBuffer = static_cast<const GrGpuBuffer*>(vertexBuffer);
853 auto gpuInstanceBuffer = static_cast<const GrGpuBuffer*>(instanceBuffer);
854 this->bindGeometry(nullptr, gpuVertexBuffer, gpuInstanceBuffer);
Chris Dalton1d616352017-05-31 12:51:23 -0600855 cbInfo.currentCmdBuf()->draw(fGpu, vertexCount, instanceCount, baseVertex, baseInstance);
Chris Dalton114a3c02017-05-26 15:17:19 -0600856 fGpu->stats()->incNumDraws();
857}
858
Brian Salomon802cb312018-06-08 18:05:20 -0400859void GrVkGpuRTCommandBuffer::sendIndexedInstancedMeshToGpu(GrPrimitiveType,
Greg Daniel500d58b2017-08-24 15:59:33 -0400860 const GrBuffer* indexBuffer,
861 int indexCount,
862 int baseIndex,
863 const GrBuffer* vertexBuffer,
864 int baseVertex,
865 const GrBuffer* instanceBuffer,
866 int instanceCount,
Brian Salomon802cb312018-06-08 18:05:20 -0400867 int baseInstance,
868 GrPrimitiveRestart restart) {
869 SkASSERT(restart == GrPrimitiveRestart::kNo);
Chris Dalton114a3c02017-05-26 15:17:19 -0600870 CommandBufferInfo& cbInfo = fCommandBufferInfos[fCurrentCmdInfo];
Brian Salomondbf70722019-02-07 11:31:24 -0500871 SkASSERT(!vertexBuffer || !vertexBuffer->isCpuBuffer());
872 SkASSERT(!instanceBuffer || !instanceBuffer->isCpuBuffer());
873 SkASSERT(!indexBuffer->isCpuBuffer());
874 auto gpuIndexxBuffer = static_cast<const GrGpuBuffer*>(indexBuffer);
875 auto gpuVertexBuffer = static_cast<const GrGpuBuffer*>(vertexBuffer);
876 auto gpuInstanceBuffer = static_cast<const GrGpuBuffer*>(instanceBuffer);
877 this->bindGeometry(gpuIndexxBuffer, gpuVertexBuffer, gpuInstanceBuffer);
Chris Dalton1d616352017-05-31 12:51:23 -0600878 cbInfo.currentCmdBuf()->drawIndexed(fGpu, indexCount, instanceCount,
879 baseIndex, baseVertex, baseInstance);
Chris Dalton114a3c02017-05-26 15:17:19 -0600880 fGpu->stats()->incNumDraws();
881}
Greg Daniel64cc9aa2018-10-19 13:54:56 -0400882
883////////////////////////////////////////////////////////////////////////////////
884
885void GrVkGpuRTCommandBuffer::executeDrawable(std::unique_ptr<SkDrawable::GpuDrawHandler> drawable) {
886 GrVkRenderTarget* target = static_cast<GrVkRenderTarget*>(fRenderTarget);
887
888 GrVkImage* targetImage = target->msaaImage() ? target->msaaImage() : target;
889
890 CommandBufferInfo& cbInfo = fCommandBufferInfos[fCurrentCmdInfo];
891 VkRect2D bounds;
892 bounds.offset = { 0, 0 };
893 bounds.extent = { 0, 0 };
894
895 GrVkDrawableInfo vkInfo;
896 vkInfo.fSecondaryCommandBuffer = cbInfo.currentCmdBuf()->vkCommandBuffer();
897 vkInfo.fCompatibleRenderPass = cbInfo.fRenderPass->vkRenderPass();
Greg Danielb353eeb2018-12-05 11:01:58 -0500898 SkAssertResult(cbInfo.fRenderPass->colorAttachmentIndex(&vkInfo.fColorAttachmentIndex));
Greg Daniel64cc9aa2018-10-19 13:54:56 -0400899 vkInfo.fFormat = targetImage->imageFormat();
900 vkInfo.fDrawBounds = &bounds;
Stan Ilievcb580602019-02-26 11:36:07 -0500901#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
902 vkInfo.fImage = targetImage->image();
903#else
904 vkInfo.fImage = VK_NULL_HANDLE;
905#endif //SK_BUILD_FOR_ANDROID_FRAMEWORK
Greg Daniel64cc9aa2018-10-19 13:54:56 -0400906
907 GrBackendDrawableInfo info(vkInfo);
908
Eric Karlc0b2ba22019-01-22 19:40:35 -0800909 // After we draw into the command buffer via the drawable, cached state we have may be invalid.
910 cbInfo.currentCmdBuf()->invalidateState();
Eric Karla8878a12019-02-07 18:17:43 -0800911 // Also assume that the drawable produced output.
912 cbInfo.fIsEmpty = false;
Eric Karlc0b2ba22019-01-22 19:40:35 -0800913
Greg Daniel64cc9aa2018-10-19 13:54:56 -0400914 drawable->draw(info);
915 fGpu->addDrawable(std::move(drawable));
916
917 if (bounds.extent.width == 0 || bounds.extent.height == 0) {
918 cbInfo.fBounds.join(target->getBoundsRect());
919 } else {
920 cbInfo.fBounds.join(SkRect::MakeXYWH(bounds.offset.x, bounds.offset.y,
921 bounds.extent.width, bounds.extent.height));
922 }
923}
924