blob: 8fd5f592502df999477764284b31c63c235bf00e [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:
Brian Salomonf77c1462019-08-01 15:19:29 -040069 TransferFrom(const SkIRect& srcRect, GrColorType surfaceColorType, GrColorType bufferColorType,
70 GrGpuBuffer* transferBuffer, size_t offset)
Brian Salomonab32f652019-05-10 14:24:50 -040071 : fTransferBuffer(sk_ref_sp(transferBuffer))
72 , fOffset(offset)
73 , fSrcRect(srcRect)
Brian Salomonf77c1462019-08-01 15:19:29 -040074 , fSurfaceColorType(surfaceColorType)
Brian Salomonab32f652019-05-10 14:24:50 -040075 , fBufferColorType(bufferColorType) {}
76
77 void execute(const Args& args) override {
78 args.fGpu->transferPixelsFrom(args.fSurface, fSrcRect.fLeft, fSrcRect.fTop,
Brian Salomonf77c1462019-08-01 15:19:29 -040079 fSrcRect.width(), fSrcRect.height(), fSurfaceColorType,
80 fBufferColorType, fTransferBuffer.get(), fOffset);
Brian Salomonab32f652019-05-10 14:24:50 -040081 }
82
83private:
84 sk_sp<GrGpuBuffer> fTransferBuffer;
85 size_t fOffset;
86 SkIRect fSrcRect;
Brian Salomonf77c1462019-08-01 15:19:29 -040087 GrColorType fSurfaceColorType;
Brian Salomonab32f652019-05-10 14:24:50 -040088 GrColorType fBufferColorType;
89};
90
Brian Salomon5d8f1cc2019-04-24 09:03:53 -040091} // anonymous namespace
92
93/////////////////////////////////////////////////////////////////////////////
94
Greg Daniel46cfbc62019-06-07 11:43:30 -040095void GrVkGpuTextureCommandBuffer::copy(GrSurface* src, const SkIRect& srcRect,
96 const SkIPoint& dstPoint) {
Emircan Uysaler23ca4e72019-06-24 10:53:09 -040097 SkASSERT(!src->isProtected() || (fTexture->isProtected() && fGpu->protectedContext()));
Greg Daniel46cfbc62019-06-07 11:43:30 -040098 fTasks.emplace<Copy>(src, srcRect, dstPoint, false);
Greg Daniel500d58b2017-08-24 15:59:33 -040099}
100
Brian Salomonf77c1462019-08-01 15:19:29 -0400101void GrVkGpuTextureCommandBuffer::transferFrom(const SkIRect& srcRect, GrColorType surfaceColorType,
102 GrColorType bufferColorType,
Brian Salomonab32f652019-05-10 14:24:50 -0400103 GrGpuBuffer* transferBuffer, size_t offset) {
Brian Salomonf77c1462019-08-01 15:19:29 -0400104 fTasks.emplace<TransferFrom>(srcRect, surfaceColorType, bufferColorType, transferBuffer,
105 offset);
Brian Salomonab32f652019-05-10 14:24:50 -0400106}
107
Greg Daniel500d58b2017-08-24 15:59:33 -0400108void GrVkGpuTextureCommandBuffer::insertEventMarker(const char* msg) {
109 // TODO: does Vulkan have a correlate?
110}
111
112void GrVkGpuTextureCommandBuffer::submit() {
Greg Daniel46cfbc62019-06-07 11:43:30 -0400113 GrVkPrimaryCommandBufferTask::Args taskArgs{fGpu, fTexture};
Brian Salomon5d8f1cc2019-04-24 09:03:53 -0400114 for (auto& task : fTasks) {
115 task.execute(taskArgs);
Greg Daniel500d58b2017-08-24 15:59:33 -0400116 }
117}
118
Greg Daniel500d58b2017-08-24 15:59:33 -0400119////////////////////////////////////////////////////////////////////////////////
120
Robert Phillips6b47c7d2017-08-29 07:24:09 -0400121void get_vk_load_store_ops(GrLoadOp loadOpIn, GrStoreOp storeOpIn,
egdaniel066df7c2016-06-08 14:02:27 -0700122 VkAttachmentLoadOp* loadOp, VkAttachmentStoreOp* storeOp) {
Robert Phillips95214472017-08-08 18:00:03 -0400123 switch (loadOpIn) {
Robert Phillips6b47c7d2017-08-29 07:24:09 -0400124 case GrLoadOp::kLoad:
egdaniel066df7c2016-06-08 14:02:27 -0700125 *loadOp = VK_ATTACHMENT_LOAD_OP_LOAD;
egdaniel066df7c2016-06-08 14:02:27 -0700126 break;
Robert Phillips6b47c7d2017-08-29 07:24:09 -0400127 case GrLoadOp::kClear:
egdaniel9cb63402016-06-23 08:37:05 -0700128 *loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
129 break;
Robert Phillips6b47c7d2017-08-29 07:24:09 -0400130 case GrLoadOp::kDiscard:
egdaniel9cb63402016-06-23 08:37:05 -0700131 *loadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
132 break;
133 default:
134 SK_ABORT("Invalid LoadOp");
egdaniel066df7c2016-06-08 14:02:27 -0700135 *loadOp = VK_ATTACHMENT_LOAD_OP_LOAD;
egdaniel9cb63402016-06-23 08:37:05 -0700136 }
137
Robert Phillips95214472017-08-08 18:00:03 -0400138 switch (storeOpIn) {
Robert Phillips6b47c7d2017-08-29 07:24:09 -0400139 case GrStoreOp::kStore:
egdaniel066df7c2016-06-08 14:02:27 -0700140 *storeOp = VK_ATTACHMENT_STORE_OP_STORE;
141 break;
Robert Phillips6b47c7d2017-08-29 07:24:09 -0400142 case GrStoreOp::kDiscard:
egdaniel066df7c2016-06-08 14:02:27 -0700143 *storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
144 break;
brianosman0bbc3712016-06-14 04:53:09 -0700145 default:
egdaniel9cb63402016-06-23 08:37:05 -0700146 SK_ABORT("Invalid StoreOp");
brianosman0bbc3712016-06-14 04:53:09 -0700147 *storeOp = VK_ATTACHMENT_STORE_OP_STORE;
egdaniel066df7c2016-06-08 14:02:27 -0700148 }
149}
150
Brian Salomon24d377e2019-04-23 15:24:31 -0400151GrVkGpuRTCommandBuffer::GrVkGpuRTCommandBuffer(GrVkGpu* gpu) : fGpu(gpu) {}
Brian Salomonc293a292016-11-30 13:38:32 -0500152
Greg Daniel500d58b2017-08-24 15:59:33 -0400153void GrVkGpuRTCommandBuffer::init() {
Brian Salomonc293a292016-11-30 13:38:32 -0500154 GrVkRenderPass::LoadStoreOps vkColorOps(fVkColorLoadOp, fVkColorStoreOp);
155 GrVkRenderPass::LoadStoreOps vkStencilOps(fVkStencilLoadOp, fVkStencilStoreOp);
egdaniel9cb63402016-06-23 08:37:05 -0700156
Greg Daniel36a77ee2016-10-18 10:33:25 -0400157 CommandBufferInfo& cbInfo = fCommandBufferInfos.push_back();
Brian Salomonc293a292016-11-30 13:38:32 -0500158 SkASSERT(fCommandBufferInfos.count() == 1);
Greg Daniel22bc8652017-03-22 15:45:43 -0400159 fCurrentCmdInfo = 0;
Greg Daniel36a77ee2016-10-18 10:33:25 -0400160
Robert Phillips19e51dc2017-08-09 09:30:51 -0400161 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(fRenderTarget);
162 const GrVkResourceProvider::CompatibleRPHandle& rpHandle = vkRT->compatibleRenderPassHandle();
egdaniel066df7c2016-06-08 14:02:27 -0700163 if (rpHandle.isValid()) {
Greg Daniel36a77ee2016-10-18 10:33:25 -0400164 cbInfo.fRenderPass = fGpu->resourceProvider().findRenderPass(rpHandle,
165 vkColorOps,
166 vkStencilOps);
egdaniel066df7c2016-06-08 14:02:27 -0700167 } else {
Robert Phillips19e51dc2017-08-09 09:30:51 -0400168 cbInfo.fRenderPass = fGpu->resourceProvider().findRenderPass(*vkRT,
Greg Daniel36a77ee2016-10-18 10:33:25 -0400169 vkColorOps,
170 vkStencilOps);
egdaniel066df7c2016-06-08 14:02:27 -0700171 }
172
Brian Osmancb3d0872018-10-16 15:19:28 -0400173 cbInfo.fColorClearValue.color.float32[0] = fClearColor[0];
174 cbInfo.fColorClearValue.color.float32[1] = fClearColor[1];
175 cbInfo.fColorClearValue.color.float32[2] = fClearColor[2];
176 cbInfo.fColorClearValue.color.float32[3] = fClearColor[3];
egdaniel9cb63402016-06-23 08:37:05 -0700177
Robert Phillips380b90c2017-08-30 07:41:07 -0400178 if (VK_ATTACHMENT_LOAD_OP_CLEAR == fVkColorLoadOp) {
Greg Daniela41a74a2018-10-09 12:59:23 +0000179 cbInfo.fBounds = SkRect::MakeWH(vkRT->width(), vkRT->height());
Robert Phillips380b90c2017-08-30 07:41:07 -0400180 } else {
181 cbInfo.fBounds.setEmpty();
182 }
Greg Daniela3c68df2018-03-16 13:46:53 -0400183
184 if (VK_ATTACHMENT_LOAD_OP_CLEAR == fVkColorLoadOp) {
185 cbInfo.fLoadStoreState = LoadStoreState::kStartsWithClear;
186 } else if (VK_ATTACHMENT_LOAD_OP_LOAD == fVkColorLoadOp &&
187 VK_ATTACHMENT_STORE_OP_STORE == fVkColorStoreOp) {
188 cbInfo.fLoadStoreState = LoadStoreState::kLoadAndStore;
189 } else if (VK_ATTACHMENT_LOAD_OP_DONT_CARE == fVkColorLoadOp) {
190 cbInfo.fLoadStoreState = LoadStoreState::kStartsWithDiscard;
191 }
Greg Daniel36a77ee2016-10-18 10:33:25 -0400192
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500193 cbInfo.fCommandBuffers.push_back(fGpu->cmdPool()->findOrCreateSecondaryCommandBuffer(fGpu));
Robert Phillips19e51dc2017-08-09 09:30:51 -0400194 cbInfo.currentCmdBuf()->begin(fGpu, vkRT->framebuffer(), cbInfo.fRenderPass);
egdaniel066df7c2016-06-08 14:02:27 -0700195}
196
Greg Daniel070cbaf2019-01-03 17:35:54 -0500197void GrVkGpuRTCommandBuffer::initWrapped() {
198 CommandBufferInfo& cbInfo = fCommandBufferInfos.push_back();
199 SkASSERT(fCommandBufferInfos.count() == 1);
200 fCurrentCmdInfo = 0;
201
202 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(fRenderTarget);
203 SkASSERT(vkRT->wrapsSecondaryCommandBuffer());
204 cbInfo.fRenderPass = vkRT->externalRenderPass();
205 cbInfo.fRenderPass->ref();
206
207 cbInfo.fBounds.setEmpty();
Greg Daniel8daf3b72019-07-30 09:57:26 -0400208 std::unique_ptr<GrVkSecondaryCommandBuffer> scb(
209 GrVkSecondaryCommandBuffer::Create(vkRT->getExternalSecondaryCommandBuffer()));
210 cbInfo.fCommandBuffers.push_back(std::move(scb));
Greg Daniel070cbaf2019-01-03 17:35:54 -0500211 cbInfo.currentCmdBuf()->begin(fGpu, nullptr, cbInfo.fRenderPass);
212}
Brian Salomonc293a292016-11-30 13:38:32 -0500213
Greg Daniel500d58b2017-08-24 15:59:33 -0400214GrVkGpuRTCommandBuffer::~GrVkGpuRTCommandBuffer() {
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400215 this->reset();
egdaniel066df7c2016-06-08 14:02:27 -0700216}
217
Greg Daniel500d58b2017-08-24 15:59:33 -0400218GrGpu* GrVkGpuRTCommandBuffer::gpu() { return fGpu; }
egdaniel9cb63402016-06-23 08:37:05 -0700219
Greg Daniel500d58b2017-08-24 15:59:33 -0400220void GrVkGpuRTCommandBuffer::end() {
Greg Daniel22bc8652017-03-22 15:45:43 -0400221 if (fCurrentCmdInfo >= 0) {
222 fCommandBufferInfos[fCurrentCmdInfo].currentCmdBuf()->end(fGpu);
Brian Salomonc293a292016-11-30 13:38:32 -0500223 }
egdaniel066df7c2016-06-08 14:02:27 -0700224}
225
Greg Daniel500d58b2017-08-24 15:59:33 -0400226void GrVkGpuRTCommandBuffer::submit() {
Brian Salomonc293a292016-11-30 13:38:32 -0500227 if (!fRenderTarget) {
228 return;
229 }
Robert Phillips19e51dc2017-08-09 09:30:51 -0400230
231 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(fRenderTarget);
Robert Phillips19e51dc2017-08-09 09:30:51 -0400232 GrVkImage* targetImage = vkRT->msaaImage() ? vkRT->msaaImage() : vkRT;
Greg Daniel45a44de2018-02-27 10:07:29 -0500233 GrStencilAttachment* stencil = fRenderTarget->renderTargetPriv().getStencilAttachment();
Brian Salomon24d377e2019-04-23 15:24:31 -0400234 auto currPreCmd = fPreCommandBufferTasks.begin();
egdaniel9cb63402016-06-23 08:37:05 -0700235
Greg Daniel46cfbc62019-06-07 11:43:30 -0400236 GrVkPrimaryCommandBufferTask::Args taskArgs{fGpu, fRenderTarget};
Greg Daniel36a77ee2016-10-18 10:33:25 -0400237 for (int i = 0; i < fCommandBufferInfos.count(); ++i) {
238 CommandBufferInfo& cbInfo = fCommandBufferInfos[i];
239
Brian Salomon24d377e2019-04-23 15:24:31 -0400240 for (int c = 0; c < cbInfo.fNumPreCmds; ++c, ++currPreCmd) {
Brian Salomon5d8f1cc2019-04-24 09:03:53 -0400241 currPreCmd->execute(taskArgs);
Greg Daniel77b53f62016-10-18 11:48:51 -0400242 }
243
Greg Daniel38c3d932018-03-16 14:22:30 -0400244 // TODO: Many things create a scratch texture which adds the discard immediately, but then
245 // don't draw to it right away. This causes the discard to be ignored and we get yelled at
246 // for loading uninitialized data. However, once MDB lands with reordering, the discard will
247 // get reordered with the rest of the draw commands and we can remove the discard check.
248 if (cbInfo.fIsEmpty &&
249 cbInfo.fLoadStoreState != LoadStoreState::kStartsWithClear &&
250 cbInfo.fLoadStoreState != LoadStoreState::kStartsWithDiscard) {
Greg Daniel77b53f62016-10-18 11:48:51 -0400251 // We have sumbitted no actual draw commands to the command buffer and we are not using
252 // the render pass to do a clear so there is no need to submit anything.
253 continue;
254 }
Greg Daniel38c3d932018-03-16 14:22:30 -0400255
Greg Daniel070cbaf2019-01-03 17:35:54 -0500256 // We don't want to actually submit the secondary command buffer if it is wrapped.
257 if (this->wrapsSecondaryCommandBuffer()) {
258 // If we have any sampled images set their layout now.
Chris Dalton298238a2019-02-21 16:28:44 -0500259 for (int j = 0; j < cbInfo.fSampledTextures.count(); ++j) {
260 cbInfo.fSampledTextures[j]->setImageLayout(
261 fGpu, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, VK_ACCESS_SHADER_READ_BIT,
262 VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT, false);
Greg Daniel070cbaf2019-01-03 17:35:54 -0500263 }
264
265 // There should have only been one secondary command buffer in the wrapped case so it is
266 // safe to just return here.
267 SkASSERT(fCommandBufferInfos.count() == 1);
268 return;
269 }
270
Greg Danieldbdba602018-04-20 11:52:43 -0400271 // Make sure if we only have a discard load that we execute the discard on the whole image.
272 // TODO: Once we improve our tracking of discards so that we never end up flushing a discard
273 // call with no actually ops, remove this.
274 if (cbInfo.fIsEmpty && cbInfo.fLoadStoreState == LoadStoreState::kStartsWithDiscard) {
Greg Daniela41a74a2018-10-09 12:59:23 +0000275 cbInfo.fBounds = SkRect::MakeWH(vkRT->width(), vkRT->height());
Greg Danieldbdba602018-04-20 11:52:43 -0400276 }
277
Greg Daniela41a74a2018-10-09 12:59:23 +0000278 if (cbInfo.fBounds.intersect(0, 0,
279 SkIntToScalar(fRenderTarget->width()),
280 SkIntToScalar(fRenderTarget->height()))) {
Greg Daniel38c3d932018-03-16 14:22:30 -0400281 // Make sure we do the following layout changes after all copies, uploads, or any other
282 // pre-work is done since we may change the layouts in the pre-work. Also since the
283 // draws will be submitted in different render passes, we need to guard againts write
284 // and write issues.
285
286 // Change layout of our render target so it can be used as the color attachment.
Greg Danielf7828d02018-10-09 12:01:32 -0400287 // TODO: If we know that we will never be blending or loading the attachment we could
288 // drop the VK_ACCESS_COLOR_ATTACHMENT_READ_BIT.
Greg Daniel38c3d932018-03-16 14:22:30 -0400289 targetImage->setImageLayout(fGpu,
290 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Greg Danielf7828d02018-10-09 12:01:32 -0400291 VK_ACCESS_COLOR_ATTACHMENT_READ_BIT |
Greg Daniel38c3d932018-03-16 14:22:30 -0400292 VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
Greg Danielf7828d02018-10-09 12:01:32 -0400293 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
Greg Daniel38c3d932018-03-16 14:22:30 -0400294 false);
295
296 // If we are using a stencil attachment we also need to update its layout
297 if (stencil) {
298 GrVkStencilAttachment* vkStencil = (GrVkStencilAttachment*)stencil;
Greg Danielf7828d02018-10-09 12:01:32 -0400299 // We need the write and read access bits since we may load and store the stencil.
300 // The initial load happens in the VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT so we
301 // wait there.
Greg Daniel38c3d932018-03-16 14:22:30 -0400302 vkStencil->setImageLayout(fGpu,
303 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
304 VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT |
305 VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT,
Greg Danielf7828d02018-10-09 12:01:32 -0400306 VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT,
Greg Daniel38c3d932018-03-16 14:22:30 -0400307 false);
308 }
309
310 // If we have any sampled images set their layout now.
Chris Dalton298238a2019-02-21 16:28:44 -0500311 for (int j = 0; j < cbInfo.fSampledTextures.count(); ++j) {
312 cbInfo.fSampledTextures[j]->setImageLayout(
313 fGpu, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, VK_ACCESS_SHADER_READ_BIT,
314 VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT, false);
Greg Daniel38c3d932018-03-16 14:22:30 -0400315 }
316
Greg Daniel36a77ee2016-10-18 10:33:25 -0400317 SkIRect iBounds;
318 cbInfo.fBounds.roundOut(&iBounds);
319
Greg Daniel22bc8652017-03-22 15:45:43 -0400320 fGpu->submitSecondaryCommandBuffer(cbInfo.fCommandBuffers, cbInfo.fRenderPass,
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400321 &cbInfo.fColorClearValue, vkRT, fOrigin, iBounds);
Greg Daniel36a77ee2016-10-18 10:33:25 -0400322 }
323 }
Brian Salomon24d377e2019-04-23 15:24:31 -0400324 SkASSERT(currPreCmd == fPreCommandBufferTasks.end());
egdaniel9cb63402016-06-23 08:37:05 -0700325}
326
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400327void GrVkGpuRTCommandBuffer::set(GrRenderTarget* rt, GrSurfaceOrigin origin,
328 const GrGpuRTCommandBuffer::LoadAndStoreInfo& colorInfo,
329 const GrGpuRTCommandBuffer::StencilLoadAndStoreInfo& stencilInfo) {
330 SkASSERT(!fRenderTarget);
331 SkASSERT(fCommandBufferInfos.empty());
332 SkASSERT(-1 == fCurrentCmdInfo);
Robert Phillips9da87e02019-02-04 13:26:26 -0500333 SkASSERT(fGpu == rt->getContext()->priv().getGpu());
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400334 SkASSERT(!fLastPipelineState);
335
Greg Danielb0c7ad12019-06-06 17:23:35 +0000336#ifdef SK_DEBUG
337 fIsActive = true;
338#endif
339
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400340 this->INHERITED::set(rt, origin);
341
Greg Daniel070cbaf2019-01-03 17:35:54 -0500342 if (this->wrapsSecondaryCommandBuffer()) {
343 this->initWrapped();
344 return;
345 }
346
Brian Osman9a9baae2018-11-05 15:06:26 -0500347 fClearColor = colorInfo.fClearColor;
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400348
349 get_vk_load_store_ops(colorInfo.fLoadOp, colorInfo.fStoreOp,
350 &fVkColorLoadOp, &fVkColorStoreOp);
351
352 get_vk_load_store_ops(stencilInfo.fLoadOp, stencilInfo.fStoreOp,
353 &fVkStencilLoadOp, &fVkStencilStoreOp);
354
355 this->init();
356}
357
358void GrVkGpuRTCommandBuffer::reset() {
359 for (int i = 0; i < fCommandBufferInfos.count(); ++i) {
360 CommandBufferInfo& cbInfo = fCommandBufferInfos[i];
361 for (int j = 0; j < cbInfo.fCommandBuffers.count(); ++j) {
Greg Daniel8daf3b72019-07-30 09:57:26 -0400362 if (cbInfo.fCommandBuffers[j]) {
363 cbInfo.fCommandBuffers[j].release()->recycle(fGpu);
364 }
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400365 }
366 cbInfo.fRenderPass->unref(fGpu);
367 }
368 fCommandBufferInfos.reset();
Brian Salomon24d377e2019-04-23 15:24:31 -0400369 fPreCommandBufferTasks.reset();
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400370
371 fCurrentCmdInfo = -1;
372
373 fLastPipelineState = nullptr;
374 fRenderTarget = nullptr;
Greg Danielb0c7ad12019-06-06 17:23:35 +0000375
376#ifdef SK_DEBUG
377 fIsActive = false;
378#endif
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400379}
380
Greg Daniel070cbaf2019-01-03 17:35:54 -0500381bool GrVkGpuRTCommandBuffer::wrapsSecondaryCommandBuffer() const {
382 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(fRenderTarget);
383 return vkRT->wrapsSecondaryCommandBuffer();
384}
385
Greg Daniel64cc9aa2018-10-19 13:54:56 -0400386////////////////////////////////////////////////////////////////////////////////
387
Greg Daniel500d58b2017-08-24 15:59:33 -0400388void GrVkGpuRTCommandBuffer::insertEventMarker(const char* msg) {
Robert Phillips65a88fa2017-08-08 08:36:22 -0400389 // TODO: does Vulkan have a correlate?
390}
391
Greg Daniel500d58b2017-08-24 15:59:33 -0400392void GrVkGpuRTCommandBuffer::onClearStencilClip(const GrFixedClip& clip, bool insideStencilMask) {
Chris Dalton94c04682017-11-01 17:15:06 -0600393 SkASSERT(!clip.hasWindowRectangles());
394
Jim Van Verth6a40abc2017-11-02 16:56:09 +0000395 CommandBufferInfo& cbInfo = fCommandBufferInfos[fCurrentCmdInfo];
396
Greg Daniel65a09272016-10-12 09:47:22 -0400397 GrStencilAttachment* sb = fRenderTarget->renderTargetPriv().getStencilAttachment();
egdaniel9cb63402016-06-23 08:37:05 -0700398 // this should only be called internally when we know we have a
399 // stencil buffer.
400 SkASSERT(sb);
401 int stencilBitCount = sb->bits();
402
403 // The contract with the callers does not guarantee that we preserve all bits in the stencil
404 // during this clear. Thus we will clear the entire stencil to the desired value.
405
406 VkClearDepthStencilValue vkStencilColor;
407 memset(&vkStencilColor, 0, sizeof(VkClearDepthStencilValue));
csmartdalton29df7602016-08-31 11:55:52 -0700408 if (insideStencilMask) {
egdaniel9cb63402016-06-23 08:37:05 -0700409 vkStencilColor.stencil = (1 << (stencilBitCount - 1));
410 } else {
411 vkStencilColor.stencil = 0;
412 }
413
414 VkClearRect clearRect;
415 // Flip rect if necessary
csmartdalton29df7602016-08-31 11:55:52 -0700416 SkIRect vkRect;
Brian Salomond818ebf2018-07-02 14:08:49 +0000417 if (!clip.scissorEnabled()) {
Greg Daniela41a74a2018-10-09 12:59:23 +0000418 vkRect.setXYWH(0, 0, fRenderTarget->width(), fRenderTarget->height());
Robert Phillips4f101a72017-07-28 08:42:04 -0400419 } else if (kBottomLeft_GrSurfaceOrigin != fOrigin) {
csmartdalton29df7602016-08-31 11:55:52 -0700420 vkRect = clip.scissorRect();
421 } else {
422 const SkIRect& scissor = clip.scissorRect();
Greg Daniel65a09272016-10-12 09:47:22 -0400423 vkRect.setLTRB(scissor.fLeft, fRenderTarget->height() - scissor.fBottom,
424 scissor.fRight, fRenderTarget->height() - scissor.fTop);
egdaniel9cb63402016-06-23 08:37:05 -0700425 }
426
427 clearRect.rect.offset = { vkRect.fLeft, vkRect.fTop };
428 clearRect.rect.extent = { (uint32_t)vkRect.width(), (uint32_t)vkRect.height() };
429
430 clearRect.baseArrayLayer = 0;
431 clearRect.layerCount = 1;
432
433 uint32_t stencilIndex;
Greg Daniel36a77ee2016-10-18 10:33:25 -0400434 SkAssertResult(cbInfo.fRenderPass->stencilAttachmentIndex(&stencilIndex));
egdaniel9cb63402016-06-23 08:37:05 -0700435
436 VkClearAttachment attachment;
437 attachment.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
438 attachment.colorAttachment = 0; // this value shouldn't matter
439 attachment.clearValue.depthStencil = vkStencilColor;
440
Greg Daniel22bc8652017-03-22 15:45:43 -0400441 cbInfo.currentCmdBuf()->clearAttachments(fGpu, 1, &attachment, 1, &clearRect);
Greg Daniel77b53f62016-10-18 11:48:51 -0400442 cbInfo.fIsEmpty = false;
Greg Daniel36a77ee2016-10-18 10:33:25 -0400443
444 // Update command buffer bounds
Brian Salomond818ebf2018-07-02 14:08:49 +0000445 if (!clip.scissorEnabled()) {
Greg Daniel36a77ee2016-10-18 10:33:25 -0400446 cbInfo.fBounds.join(fRenderTarget->getBoundsRect());
447 } else {
448 cbInfo.fBounds.join(SkRect::Make(clip.scissorRect()));
449 }
egdaniel9cb63402016-06-23 08:37:05 -0700450}
451
Brian Osman9a9baae2018-11-05 15:06:26 -0500452void GrVkGpuRTCommandBuffer::onClear(const GrFixedClip& clip, const SkPMColor4f& color) {
Robert Phillips19e51dc2017-08-09 09:30:51 -0400453 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(fRenderTarget);
454
Jim Van Verth6a40abc2017-11-02 16:56:09 +0000455 // parent class should never let us get here with no RT
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700456 SkASSERT(!clip.hasWindowRectangles());
egdaniel9cb63402016-06-23 08:37:05 -0700457
Greg Daniel22bc8652017-03-22 15:45:43 -0400458 CommandBufferInfo& cbInfo = fCommandBufferInfos[fCurrentCmdInfo];
Greg Daniel36a77ee2016-10-18 10:33:25 -0400459
Brian Osman9a9baae2018-11-05 15:06:26 -0500460 VkClearColorValue vkColor = {{color.fR, color.fG, color.fB, color.fA}};
egdaniel9cb63402016-06-23 08:37:05 -0700461
Brian Salomond818ebf2018-07-02 14:08:49 +0000462 if (cbInfo.fIsEmpty && !clip.scissorEnabled()) {
Robert Phillips74c627f2017-08-09 10:28:00 -0400463 // Change the render pass to do a clear load
egdaniel9cb63402016-06-23 08:37:05 -0700464 GrVkRenderPass::LoadStoreOps vkColorOps(VK_ATTACHMENT_LOAD_OP_CLEAR,
465 VK_ATTACHMENT_STORE_OP_STORE);
Robert Phillips74c627f2017-08-09 10:28:00 -0400466 // Preserve the stencil buffer's load & store settings
467 GrVkRenderPass::LoadStoreOps vkStencilOps(fVkStencilLoadOp, fVkStencilStoreOp);
egdaniel9cb63402016-06-23 08:37:05 -0700468
Greg Daniel36a77ee2016-10-18 10:33:25 -0400469 const GrVkRenderPass* oldRP = cbInfo.fRenderPass;
egdaniel9cb63402016-06-23 08:37:05 -0700470
471 const GrVkResourceProvider::CompatibleRPHandle& rpHandle =
Robert Phillips19e51dc2017-08-09 09:30:51 -0400472 vkRT->compatibleRenderPassHandle();
egdaniel9cb63402016-06-23 08:37:05 -0700473 if (rpHandle.isValid()) {
Greg Daniel36a77ee2016-10-18 10:33:25 -0400474 cbInfo.fRenderPass = fGpu->resourceProvider().findRenderPass(rpHandle,
475 vkColorOps,
476 vkStencilOps);
egdaniel9cb63402016-06-23 08:37:05 -0700477 } else {
Robert Phillips19e51dc2017-08-09 09:30:51 -0400478 cbInfo.fRenderPass = fGpu->resourceProvider().findRenderPass(*vkRT,
Greg Daniel36a77ee2016-10-18 10:33:25 -0400479 vkColorOps,
480 vkStencilOps);
egdaniel9cb63402016-06-23 08:37:05 -0700481 }
482
Greg Daniel36a77ee2016-10-18 10:33:25 -0400483 SkASSERT(cbInfo.fRenderPass->isCompatible(*oldRP));
egdaniel9cb63402016-06-23 08:37:05 -0700484 oldRP->unref(fGpu);
485
Brian Osman9a9baae2018-11-05 15:06:26 -0500486 cbInfo.fColorClearValue.color = {{color.fR, color.fG, color.fB, color.fA}};
Greg Daniela3c68df2018-03-16 13:46:53 -0400487 cbInfo.fLoadStoreState = LoadStoreState::kStartsWithClear;
Greg Daniel36a77ee2016-10-18 10:33:25 -0400488 // Update command buffer bounds
489 cbInfo.fBounds.join(fRenderTarget->getBoundsRect());
egdaniel9cb63402016-06-23 08:37:05 -0700490 return;
491 }
492
493 // We always do a sub rect clear with clearAttachments since we are inside a render pass
494 VkClearRect clearRect;
495 // Flip rect if necessary
csmartdalton29df7602016-08-31 11:55:52 -0700496 SkIRect vkRect;
Brian Salomond818ebf2018-07-02 14:08:49 +0000497 if (!clip.scissorEnabled()) {
Greg Daniela41a74a2018-10-09 12:59:23 +0000498 vkRect.setXYWH(0, 0, fRenderTarget->width(), fRenderTarget->height());
Robert Phillips4f101a72017-07-28 08:42:04 -0400499 } else if (kBottomLeft_GrSurfaceOrigin != fOrigin) {
csmartdalton29df7602016-08-31 11:55:52 -0700500 vkRect = clip.scissorRect();
501 } else {
502 const SkIRect& scissor = clip.scissorRect();
Greg Daniel65a09272016-10-12 09:47:22 -0400503 vkRect.setLTRB(scissor.fLeft, fRenderTarget->height() - scissor.fBottom,
504 scissor.fRight, fRenderTarget->height() - scissor.fTop);
egdaniel9cb63402016-06-23 08:37:05 -0700505 }
506 clearRect.rect.offset = { vkRect.fLeft, vkRect.fTop };
507 clearRect.rect.extent = { (uint32_t)vkRect.width(), (uint32_t)vkRect.height() };
508 clearRect.baseArrayLayer = 0;
509 clearRect.layerCount = 1;
510
511 uint32_t colorIndex;
Greg Daniel36a77ee2016-10-18 10:33:25 -0400512 SkAssertResult(cbInfo.fRenderPass->colorAttachmentIndex(&colorIndex));
egdaniel9cb63402016-06-23 08:37:05 -0700513
514 VkClearAttachment attachment;
515 attachment.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
516 attachment.colorAttachment = colorIndex;
517 attachment.clearValue.color = vkColor;
518
Greg Daniel22bc8652017-03-22 15:45:43 -0400519 cbInfo.currentCmdBuf()->clearAttachments(fGpu, 1, &attachment, 1, &clearRect);
Greg Daniel77b53f62016-10-18 11:48:51 -0400520 cbInfo.fIsEmpty = false;
Greg Daniel36a77ee2016-10-18 10:33:25 -0400521
522 // Update command buffer bounds
Brian Salomond818ebf2018-07-02 14:08:49 +0000523 if (!clip.scissorEnabled()) {
Greg Daniel36a77ee2016-10-18 10:33:25 -0400524 cbInfo.fBounds.join(fRenderTarget->getBoundsRect());
525 } else {
526 cbInfo.fBounds.join(SkRect::Make(clip.scissorRect()));
527 }
egdaniel9cb63402016-06-23 08:37:05 -0700528 return;
529}
530
Greg Daniel500d58b2017-08-24 15:59:33 -0400531////////////////////////////////////////////////////////////////////////////////
532
533void GrVkGpuRTCommandBuffer::addAdditionalCommandBuffer() {
Robert Phillips19e51dc2017-08-09 09:30:51 -0400534 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(fRenderTarget);
535
Greg Daniel22bc8652017-03-22 15:45:43 -0400536 CommandBufferInfo& cbInfo = fCommandBufferInfos[fCurrentCmdInfo];
537 cbInfo.currentCmdBuf()->end(fGpu);
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500538 cbInfo.fCommandBuffers.push_back(fGpu->cmdPool()->findOrCreateSecondaryCommandBuffer(fGpu));
Robert Phillips19e51dc2017-08-09 09:30:51 -0400539 cbInfo.currentCmdBuf()->begin(fGpu, vkRT->framebuffer(), cbInfo.fRenderPass);
Greg Daniel22bc8652017-03-22 15:45:43 -0400540}
541
Greg Daniel500d58b2017-08-24 15:59:33 -0400542void GrVkGpuRTCommandBuffer::addAdditionalRenderPass() {
Robert Phillips19e51dc2017-08-09 09:30:51 -0400543 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(fRenderTarget);
544
Greg Daniel22bc8652017-03-22 15:45:43 -0400545 fCommandBufferInfos[fCurrentCmdInfo].currentCmdBuf()->end(fGpu);
Greg Daniel77b53f62016-10-18 11:48:51 -0400546
547 CommandBufferInfo& cbInfo = fCommandBufferInfos.push_back();
Greg Daniel22bc8652017-03-22 15:45:43 -0400548 fCurrentCmdInfo++;
Greg Daniel77b53f62016-10-18 11:48:51 -0400549
550 GrVkRenderPass::LoadStoreOps vkColorOps(VK_ATTACHMENT_LOAD_OP_LOAD,
551 VK_ATTACHMENT_STORE_OP_STORE);
552 GrVkRenderPass::LoadStoreOps vkStencilOps(VK_ATTACHMENT_LOAD_OP_LOAD,
553 VK_ATTACHMENT_STORE_OP_STORE);
554
555 const GrVkResourceProvider::CompatibleRPHandle& rpHandle =
Robert Phillips19e51dc2017-08-09 09:30:51 -0400556 vkRT->compatibleRenderPassHandle();
Greg Daniel77b53f62016-10-18 11:48:51 -0400557 if (rpHandle.isValid()) {
558 cbInfo.fRenderPass = fGpu->resourceProvider().findRenderPass(rpHandle,
559 vkColorOps,
560 vkStencilOps);
561 } else {
Robert Phillips19e51dc2017-08-09 09:30:51 -0400562 cbInfo.fRenderPass = fGpu->resourceProvider().findRenderPass(*vkRT,
Greg Daniel77b53f62016-10-18 11:48:51 -0400563 vkColorOps,
564 vkStencilOps);
565 }
Greg Daniela3c68df2018-03-16 13:46:53 -0400566 cbInfo.fLoadStoreState = LoadStoreState::kLoadAndStore;
Greg Daniel77b53f62016-10-18 11:48:51 -0400567
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500568 cbInfo.fCommandBuffers.push_back(fGpu->cmdPool()->findOrCreateSecondaryCommandBuffer(fGpu));
Greg Daniel77b53f62016-10-18 11:48:51 -0400569 // It shouldn't matter what we set the clear color to here since we will assume loading of the
570 // attachment.
571 memset(&cbInfo.fColorClearValue, 0, sizeof(VkClearValue));
572 cbInfo.fBounds.setEmpty();
Greg Daniel77b53f62016-10-18 11:48:51 -0400573
Robert Phillips19e51dc2017-08-09 09:30:51 -0400574 cbInfo.currentCmdBuf()->begin(fGpu, vkRT->framebuffer(), cbInfo.fRenderPass);
Greg Daniel77b53f62016-10-18 11:48:51 -0400575}
576
Brian Salomon943ed792017-10-30 09:37:55 -0400577void GrVkGpuRTCommandBuffer::inlineUpload(GrOpFlushState* state,
578 GrDeferredTextureUploadFn& upload) {
Greg Daniel22bc8652017-03-22 15:45:43 -0400579 if (!fCommandBufferInfos[fCurrentCmdInfo].fIsEmpty) {
580 this->addAdditionalRenderPass();
Greg Daniel77b53f62016-10-18 11:48:51 -0400581 }
Brian Salomon24d377e2019-04-23 15:24:31 -0400582
Brian Salomon24d377e2019-04-23 15:24:31 -0400583 fPreCommandBufferTasks.emplace<InlineUpload>(state, upload);
584 ++fCommandBufferInfos[fCurrentCmdInfo].fNumPreCmds;
Greg Daniel77b53f62016-10-18 11:48:51 -0400585}
586
Greg Daniel46cfbc62019-06-07 11:43:30 -0400587void GrVkGpuRTCommandBuffer::copy(GrSurface* src, const SkIRect& srcRect,
Greg Daniel500d58b2017-08-24 15:59:33 -0400588 const SkIPoint& dstPoint) {
Greg Daniela3c68df2018-03-16 13:46:53 -0400589 CommandBufferInfo& cbInfo = fCommandBufferInfos[fCurrentCmdInfo];
590 if (!cbInfo.fIsEmpty || LoadStoreState::kStartsWithClear == cbInfo.fLoadStoreState) {
Greg Daniel500d58b2017-08-24 15:59:33 -0400591 this->addAdditionalRenderPass();
592 }
Greg Daniela3c68df2018-03-16 13:46:53 -0400593
Brian Salomon24d377e2019-04-23 15:24:31 -0400594 fPreCommandBufferTasks.emplace<Copy>(
Greg Daniel46cfbc62019-06-07 11:43:30 -0400595 src, srcRect, dstPoint, LoadStoreState::kStartsWithDiscard == cbInfo.fLoadStoreState);
Brian Salomon24d377e2019-04-23 15:24:31 -0400596 ++fCommandBufferInfos[fCurrentCmdInfo].fNumPreCmds;
Greg Daniel55fa6472018-03-16 16:13:10 -0400597
Greg Daniela3c68df2018-03-16 13:46:53 -0400598 if (LoadStoreState::kLoadAndStore != cbInfo.fLoadStoreState) {
599 // Change the render pass to do a load and store so we don't lose the results of our copy
600 GrVkRenderPass::LoadStoreOps vkColorOps(VK_ATTACHMENT_LOAD_OP_LOAD,
601 VK_ATTACHMENT_STORE_OP_STORE);
602 GrVkRenderPass::LoadStoreOps vkStencilOps(VK_ATTACHMENT_LOAD_OP_LOAD,
603 VK_ATTACHMENT_STORE_OP_STORE);
604
605 const GrVkRenderPass* oldRP = cbInfo.fRenderPass;
606
607 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(fRenderTarget);
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400608 SkASSERT(!src->isProtected() || (fRenderTarget->isProtected() && fGpu->protectedContext()));
Greg Daniela3c68df2018-03-16 13:46:53 -0400609 const GrVkResourceProvider::CompatibleRPHandle& rpHandle =
610 vkRT->compatibleRenderPassHandle();
611 if (rpHandle.isValid()) {
612 cbInfo.fRenderPass = fGpu->resourceProvider().findRenderPass(rpHandle,
613 vkColorOps,
614 vkStencilOps);
615 } else {
616 cbInfo.fRenderPass = fGpu->resourceProvider().findRenderPass(*vkRT,
617 vkColorOps,
618 vkStencilOps);
619 }
620 SkASSERT(cbInfo.fRenderPass->isCompatible(*oldRP));
621 oldRP->unref(fGpu);
622
623 cbInfo.fLoadStoreState = LoadStoreState::kLoadAndStore;
624
625 }
Greg Daniel500d58b2017-08-24 15:59:33 -0400626}
627
Brian Salomonf77c1462019-08-01 15:19:29 -0400628void GrVkGpuRTCommandBuffer::transferFrom(const SkIRect& srcRect, GrColorType surfaceColorType,
629 GrColorType bufferColorType, GrGpuBuffer* transferBuffer,
630 size_t offset) {
Brian Salomonab32f652019-05-10 14:24:50 -0400631 CommandBufferInfo& cbInfo = fCommandBufferInfos[fCurrentCmdInfo];
632 if (!cbInfo.fIsEmpty) {
633 this->addAdditionalRenderPass();
634 }
Brian Salomonf77c1462019-08-01 15:19:29 -0400635 fPreCommandBufferTasks.emplace<TransferFrom>(srcRect, surfaceColorType, bufferColorType,
636 transferBuffer, offset);
Brian Salomonab32f652019-05-10 14:24:50 -0400637 ++fCommandBufferInfos[fCurrentCmdInfo].fNumPreCmds;
638}
639
egdaniel9cb63402016-06-23 08:37:05 -0700640////////////////////////////////////////////////////////////////////////////////
641
Brian Salomondbf70722019-02-07 11:31:24 -0500642void GrVkGpuRTCommandBuffer::bindGeometry(const GrGpuBuffer* indexBuffer,
643 const GrGpuBuffer* vertexBuffer,
644 const GrGpuBuffer* instanceBuffer) {
Chris Daltonff926502017-05-03 14:36:54 -0400645 GrVkSecondaryCommandBuffer* currCmdBuf = fCommandBufferInfos[fCurrentCmdInfo].currentCmdBuf();
egdaniel9cb63402016-06-23 08:37:05 -0700646 // There is no need to put any memory barriers to make sure host writes have finished here.
647 // When a command buffer is submitted to a queue, there is an implicit memory barrier that
648 // occurs for all host writes. Additionally, BufferMemoryBarriers are not allowed inside of
649 // an active RenderPass.
egdaniel9cb63402016-06-23 08:37:05 -0700650
Chris Dalton1d616352017-05-31 12:51:23 -0600651 // Here our vertex and instance inputs need to match the same 0-based bindings they were
652 // assigned in GrVkPipeline. That is, vertex first (if any) followed by instance.
653 uint32_t binding = 0;
654
Brian Salomon802cb312018-06-08 18:05:20 -0400655 if (vertexBuffer) {
Chris Dalton1d616352017-05-31 12:51:23 -0600656 SkASSERT(vertexBuffer);
Chris Dalton1d616352017-05-31 12:51:23 -0600657 SkASSERT(!vertexBuffer->isMapped());
658
659 currCmdBuf->bindInputBuffer(fGpu, binding++,
660 static_cast<const GrVkVertexBuffer*>(vertexBuffer));
661 }
662
Brian Salomon802cb312018-06-08 18:05:20 -0400663 if (instanceBuffer) {
Chris Dalton1d616352017-05-31 12:51:23 -0600664 SkASSERT(instanceBuffer);
Chris Dalton1d616352017-05-31 12:51:23 -0600665 SkASSERT(!instanceBuffer->isMapped());
666
667 currCmdBuf->bindInputBuffer(fGpu, binding++,
668 static_cast<const GrVkVertexBuffer*>(instanceBuffer));
669 }
Chris Daltonff926502017-05-03 14:36:54 -0400670 if (indexBuffer) {
671 SkASSERT(indexBuffer);
672 SkASSERT(!indexBuffer->isMapped());
egdaniel9cb63402016-06-23 08:37:05 -0700673
Chris Daltonff926502017-05-03 14:36:54 -0400674 currCmdBuf->bindIndexBuffer(fGpu, static_cast<const GrVkIndexBuffer*>(indexBuffer));
egdaniel9cb63402016-06-23 08:37:05 -0700675 }
676}
677
Brian Salomon49348902018-06-26 09:12:38 -0400678GrVkPipelineState* GrVkGpuRTCommandBuffer::prepareDrawState(
679 const GrPrimitiveProcessor& primProc,
680 const GrPipeline& pipeline,
681 const GrPipeline::FixedDynamicState* fixedDynamicState,
682 const GrPipeline::DynamicStateArrays* dynamicStateArrays,
683 GrPrimitiveType primitiveType) {
Greg Daniel22bc8652017-03-22 15:45:43 -0400684 CommandBufferInfo& cbInfo = fCommandBufferInfos[fCurrentCmdInfo];
685 SkASSERT(cbInfo.fRenderPass);
Greg Daniel36a77ee2016-10-18 10:33:25 -0400686
Greg Daniel99b88e02018-10-03 15:31:20 -0400687 VkRenderPass compatibleRenderPass = cbInfo.fRenderPass->vkRenderPass();
688
Greg Daniel9a51a862018-11-30 10:18:14 -0500689 const GrTextureProxy* const* primProcProxies = nullptr;
690 if (dynamicStateArrays && dynamicStateArrays->fPrimitiveProcessorTextures) {
691 primProcProxies = dynamicStateArrays->fPrimitiveProcessorTextures;
692 } else if (fixedDynamicState) {
693 primProcProxies = fixedDynamicState->fPrimitiveProcessorTextures;
694 }
695
696 SkASSERT(SkToBool(primProcProxies) == SkToBool(primProc.numTextureSamplers()));
697
Greg Daniel09eeefb2017-10-16 15:15:02 -0400698 GrVkPipelineState* pipelineState =
Robert Phillipsd0fe8752019-01-31 14:13:59 -0500699 fGpu->resourceProvider().findOrCreateCompatiblePipelineState(fRenderTarget, fOrigin,
700 pipeline,
egdaniel9cb63402016-06-23 08:37:05 -0700701 primProc,
Greg Daniel9a51a862018-11-30 10:18:14 -0500702 primProcProxies,
egdaniel9cb63402016-06-23 08:37:05 -0700703 primitiveType,
Greg Daniel99b88e02018-10-03 15:31:20 -0400704 compatibleRenderPass);
egdaniel9cb63402016-06-23 08:37:05 -0700705 if (!pipelineState) {
706 return pipelineState;
707 }
708
Greg Daniel22bc8652017-03-22 15:45:43 -0400709 if (!cbInfo.fIsEmpty &&
Greg Daniel09eeefb2017-10-16 15:15:02 -0400710 fLastPipelineState && fLastPipelineState != pipelineState &&
Greg Daniele3cd6912017-05-17 11:15:55 -0400711 fGpu->vkCaps().newCBOnPipelineChange()) {
Greg Daniel22bc8652017-03-22 15:45:43 -0400712 this->addAdditionalCommandBuffer();
713 }
Greg Daniel09eeefb2017-10-16 15:15:02 -0400714 fLastPipelineState = pipelineState;
Greg Daniel22bc8652017-03-22 15:45:43 -0400715
Brian Salomonf7232642018-09-19 08:58:08 -0400716 pipelineState->bindPipeline(fGpu, cbInfo.currentCmdBuf());
Brian Salomoncd7907b2018-08-30 08:36:18 -0400717
Robert Phillipsd0fe8752019-01-31 14:13:59 -0500718 pipelineState->setAndBindUniforms(fGpu, fRenderTarget, fOrigin,
719 primProc, pipeline, cbInfo.currentCmdBuf());
Brian Salomonf7232642018-09-19 08:58:08 -0400720
721 // Check whether we need to bind textures between each GrMesh. If not we can bind them all now.
722 bool setTextures = !(dynamicStateArrays && dynamicStateArrays->fPrimitiveProcessorTextures);
723 if (setTextures) {
Brian Salomonf7232642018-09-19 08:58:08 -0400724 pipelineState->setAndBindTextures(fGpu, primProc, pipeline, primProcProxies,
725 cbInfo.currentCmdBuf());
726 }
egdaniel9cb63402016-06-23 08:37:05 -0700727
Brian Salomond818ebf2018-07-02 14:08:49 +0000728 if (!pipeline.isScissorEnabled()) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400729 GrVkPipeline::SetDynamicScissorRectState(fGpu, cbInfo.currentCmdBuf(),
Robert Phillipsd0fe8752019-01-31 14:13:59 -0500730 fRenderTarget, fOrigin,
731 SkIRect::MakeWH(fRenderTarget->width(),
732 fRenderTarget->height()));
Brian Salomon49348902018-06-26 09:12:38 -0400733 } else if (!dynamicStateArrays || !dynamicStateArrays->fScissorRects) {
734 SkASSERT(fixedDynamicState);
Robert Phillipsd0fe8752019-01-31 14:13:59 -0500735 GrVkPipeline::SetDynamicScissorRectState(fGpu, cbInfo.currentCmdBuf(), fRenderTarget,
736 fOrigin,
Brian Salomon49348902018-06-26 09:12:38 -0400737 fixedDynamicState->fScissorRect);
Chris Dalton46983b72017-06-06 12:27:16 -0600738 }
Robert Phillipsd0fe8752019-01-31 14:13:59 -0500739 GrVkPipeline::SetDynamicViewportState(fGpu, cbInfo.currentCmdBuf(), fRenderTarget);
740 GrVkPipeline::SetDynamicBlendConstantState(fGpu, cbInfo.currentCmdBuf(),
Greg Daniel2c3398d2019-06-19 11:58:01 -0400741 pipeline.outputSwizzle(),
Chris Dalton46983b72017-06-06 12:27:16 -0600742 pipeline.getXferProcessor());
egdaniel9cb63402016-06-23 08:37:05 -0700743
744 return pipelineState;
745}
746
Brian Salomonff168d92018-06-23 15:17:27 -0400747void GrVkGpuRTCommandBuffer::onDraw(const GrPrimitiveProcessor& primProc,
748 const GrPipeline& pipeline,
Brian Salomon49348902018-06-26 09:12:38 -0400749 const GrPipeline::FixedDynamicState* fixedDynamicState,
750 const GrPipeline::DynamicStateArrays* dynamicStateArrays,
Greg Daniel500d58b2017-08-24 15:59:33 -0400751 const GrMesh meshes[],
Greg Daniel500d58b2017-08-24 15:59:33 -0400752 int meshCount,
753 const SkRect& bounds) {
egdaniel9cb63402016-06-23 08:37:05 -0700754 if (!meshCount) {
755 return;
756 }
Greg Danielea022cd2018-03-16 11:10:03 -0400757
758 CommandBufferInfo& cbInfo = fCommandBufferInfos[fCurrentCmdInfo];
759
Brian Salomone782f842018-07-31 13:53:11 -0400760 auto prepareSampledImage = [&](GrTexture* texture, GrSamplerState::Filter filter) {
761 GrVkTexture* vkTexture = static_cast<GrVkTexture*>(texture);
762 // We may need to resolve the texture first if it is also a render target
763 GrVkRenderTarget* texRT = static_cast<GrVkRenderTarget*>(vkTexture->asRenderTarget());
764 if (texRT) {
Greg Daniel0a77f432018-12-06 11:23:32 -0500765 fGpu->resolveRenderTargetNoFlush(texRT);
Brian Salomone782f842018-07-31 13:53:11 -0400766 }
767
768 // Check if we need to regenerate any mip maps
769 if (GrSamplerState::Filter::kMipMap == filter &&
770 (vkTexture->width() != 1 || vkTexture->height() != 1)) {
771 SkASSERT(vkTexture->texturePriv().mipMapped() == GrMipMapped::kYes);
772 if (vkTexture->texturePriv().mipMapsAreDirty()) {
773 fGpu->regenerateMipMapLevels(vkTexture);
774 }
775 }
Brian Salomon5fd10572019-04-01 12:07:05 -0400776 cbInfo.fSampledTextures.push_back(vkTexture);
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400777
778 SkASSERT(!texture->isProtected() ||
779 (fRenderTarget->isProtected() && fGpu->protectedContext()));
Brian Salomone782f842018-07-31 13:53:11 -0400780 };
781
Brian Salomonf7232642018-09-19 08:58:08 -0400782 if (dynamicStateArrays && dynamicStateArrays->fPrimitiveProcessorTextures) {
783 for (int m = 0, i = 0; m < meshCount; ++m) {
784 for (int s = 0; s < primProc.numTextureSamplers(); ++s, ++i) {
785 auto texture = dynamicStateArrays->fPrimitiveProcessorTextures[i]->peekTexture();
786 prepareSampledImage(texture, primProc.textureSampler(s).samplerState().filter());
787 }
788 }
789 } else {
790 for (int i = 0; i < primProc.numTextureSamplers(); ++i) {
791 auto texture = fixedDynamicState->fPrimitiveProcessorTextures[i]->peekTexture();
792 prepareSampledImage(texture, primProc.textureSampler(i).samplerState().filter());
793 }
Brian Salomone782f842018-07-31 13:53:11 -0400794 }
bsalomonb58a2b42016-09-26 06:55:02 -0700795 GrFragmentProcessor::Iter iter(pipeline);
796 while (const GrFragmentProcessor* fp = iter.next()) {
Brian Salomone782f842018-07-31 13:53:11 -0400797 for (int i = 0; i < fp->numTextureSamplers(); ++i) {
798 const GrFragmentProcessor::TextureSampler& sampler = fp->textureSampler(i);
799 prepareSampledImage(sampler.peekTexture(), sampler.samplerState().filter());
800 }
egdaniel2f5792a2016-07-06 08:51:23 -0700801 }
Robert Phillipsbb581ce2017-05-29 15:05:15 -0400802 if (GrTexture* dstTexture = pipeline.peekDstTexture()) {
Chris Dalton298238a2019-02-21 16:28:44 -0500803 cbInfo.fSampledTextures.push_back(sk_ref_sp(static_cast<GrVkTexture*>(dstTexture)));
Brian Salomon18dfa982017-04-03 16:57:43 -0400804 }
egdaniel2f5792a2016-07-06 08:51:23 -0700805
Chris Daltonbca46e22017-05-15 11:03:26 -0600806 GrPrimitiveType primitiveType = meshes[0].primitiveType();
Brian Salomon49348902018-06-26 09:12:38 -0400807 GrVkPipelineState* pipelineState = this->prepareDrawState(primProc, pipeline, fixedDynamicState,
808 dynamicStateArrays, primitiveType);
egdaniel9cb63402016-06-23 08:37:05 -0700809 if (!pipelineState) {
810 return;
811 }
812
Brian Salomond818ebf2018-07-02 14:08:49 +0000813 bool dynamicScissor =
814 pipeline.isScissorEnabled() && dynamicStateArrays && dynamicStateArrays->fScissorRects;
Brian Salomonf7232642018-09-19 08:58:08 -0400815 bool dynamicTextures = dynamicStateArrays && dynamicStateArrays->fPrimitiveProcessorTextures;
Brian Salomon49348902018-06-26 09:12:38 -0400816
egdaniel9cb63402016-06-23 08:37:05 -0700817 for (int i = 0; i < meshCount; ++i) {
818 const GrMesh& mesh = meshes[i];
Chris Daltonbca46e22017-05-15 11:03:26 -0600819 if (mesh.primitiveType() != primitiveType) {
Chris Dalton6f241802017-05-08 13:58:38 -0400820 SkDEBUGCODE(pipelineState = nullptr);
Chris Daltonbca46e22017-05-15 11:03:26 -0600821 primitiveType = mesh.primitiveType();
Brian Salomon49348902018-06-26 09:12:38 -0400822 pipelineState = this->prepareDrawState(primProc, pipeline, fixedDynamicState,
823 dynamicStateArrays, primitiveType);
Chris Dalton6f241802017-05-08 13:58:38 -0400824 if (!pipelineState) {
825 return;
egdaniel9cb63402016-06-23 08:37:05 -0700826 }
Chris Dalton6f241802017-05-08 13:58:38 -0400827 }
egdaniel9cb63402016-06-23 08:37:05 -0700828
Brian Salomon49348902018-06-26 09:12:38 -0400829 if (dynamicScissor) {
830 GrVkPipeline::SetDynamicScissorRectState(fGpu, cbInfo.currentCmdBuf(), fRenderTarget,
Robert Phillipsd0fe8752019-01-31 14:13:59 -0500831 fOrigin,
Brian Salomon49348902018-06-26 09:12:38 -0400832 dynamicStateArrays->fScissorRects[i]);
Chris Dalton46983b72017-06-06 12:27:16 -0600833 }
Brian Salomonf7232642018-09-19 08:58:08 -0400834 if (dynamicTextures) {
835 GrTextureProxy* const* meshProxies = dynamicStateArrays->fPrimitiveProcessorTextures +
836 primProc.numTextureSamplers() * i;
837 pipelineState->setAndBindTextures(fGpu, primProc, pipeline, meshProxies,
838 cbInfo.currentCmdBuf());
839 }
Chris Daltonbca46e22017-05-15 11:03:26 -0600840 SkASSERT(pipelineState);
Brian Salomon802cb312018-06-08 18:05:20 -0400841 mesh.sendToGpu(this);
egdaniel9cb63402016-06-23 08:37:05 -0700842 }
843
Greg Daniel36a77ee2016-10-18 10:33:25 -0400844 cbInfo.fBounds.join(bounds);
Chris Dalton114a3c02017-05-26 15:17:19 -0600845 cbInfo.fIsEmpty = false;
egdaniel066df7c2016-06-08 14:02:27 -0700846}
847
Brian Salomon802cb312018-06-08 18:05:20 -0400848void GrVkGpuRTCommandBuffer::sendInstancedMeshToGpu(GrPrimitiveType,
Greg Daniel500d58b2017-08-24 15:59:33 -0400849 const GrBuffer* vertexBuffer,
850 int vertexCount,
851 int baseVertex,
852 const GrBuffer* instanceBuffer,
853 int instanceCount,
854 int baseInstance) {
Chris Dalton114a3c02017-05-26 15:17:19 -0600855 CommandBufferInfo& cbInfo = fCommandBufferInfos[fCurrentCmdInfo];
Brian Salomondbf70722019-02-07 11:31:24 -0500856 SkASSERT(!vertexBuffer || !vertexBuffer->isCpuBuffer());
857 SkASSERT(!instanceBuffer || !instanceBuffer->isCpuBuffer());
858 auto gpuVertexBuffer = static_cast<const GrGpuBuffer*>(vertexBuffer);
859 auto gpuInstanceBuffer = static_cast<const GrGpuBuffer*>(instanceBuffer);
860 this->bindGeometry(nullptr, gpuVertexBuffer, gpuInstanceBuffer);
Chris Dalton1d616352017-05-31 12:51:23 -0600861 cbInfo.currentCmdBuf()->draw(fGpu, vertexCount, instanceCount, baseVertex, baseInstance);
Chris Dalton114a3c02017-05-26 15:17:19 -0600862 fGpu->stats()->incNumDraws();
863}
864
Brian Salomon802cb312018-06-08 18:05:20 -0400865void GrVkGpuRTCommandBuffer::sendIndexedInstancedMeshToGpu(GrPrimitiveType,
Greg Daniel500d58b2017-08-24 15:59:33 -0400866 const GrBuffer* indexBuffer,
867 int indexCount,
868 int baseIndex,
869 const GrBuffer* vertexBuffer,
870 int baseVertex,
871 const GrBuffer* instanceBuffer,
872 int instanceCount,
Brian Salomon802cb312018-06-08 18:05:20 -0400873 int baseInstance,
874 GrPrimitiveRestart restart) {
875 SkASSERT(restart == GrPrimitiveRestart::kNo);
Chris Dalton114a3c02017-05-26 15:17:19 -0600876 CommandBufferInfo& cbInfo = fCommandBufferInfos[fCurrentCmdInfo];
Brian Salomondbf70722019-02-07 11:31:24 -0500877 SkASSERT(!vertexBuffer || !vertexBuffer->isCpuBuffer());
878 SkASSERT(!instanceBuffer || !instanceBuffer->isCpuBuffer());
879 SkASSERT(!indexBuffer->isCpuBuffer());
880 auto gpuIndexxBuffer = static_cast<const GrGpuBuffer*>(indexBuffer);
881 auto gpuVertexBuffer = static_cast<const GrGpuBuffer*>(vertexBuffer);
882 auto gpuInstanceBuffer = static_cast<const GrGpuBuffer*>(instanceBuffer);
883 this->bindGeometry(gpuIndexxBuffer, gpuVertexBuffer, gpuInstanceBuffer);
Chris Dalton1d616352017-05-31 12:51:23 -0600884 cbInfo.currentCmdBuf()->drawIndexed(fGpu, indexCount, instanceCount,
885 baseIndex, baseVertex, baseInstance);
Chris Dalton114a3c02017-05-26 15:17:19 -0600886 fGpu->stats()->incNumDraws();
887}
Greg Daniel64cc9aa2018-10-19 13:54:56 -0400888
889////////////////////////////////////////////////////////////////////////////////
890
891void GrVkGpuRTCommandBuffer::executeDrawable(std::unique_ptr<SkDrawable::GpuDrawHandler> drawable) {
892 GrVkRenderTarget* target = static_cast<GrVkRenderTarget*>(fRenderTarget);
893
894 GrVkImage* targetImage = target->msaaImage() ? target->msaaImage() : target;
895
896 CommandBufferInfo& cbInfo = fCommandBufferInfos[fCurrentCmdInfo];
897 VkRect2D bounds;
898 bounds.offset = { 0, 0 };
899 bounds.extent = { 0, 0 };
900
901 GrVkDrawableInfo vkInfo;
902 vkInfo.fSecondaryCommandBuffer = cbInfo.currentCmdBuf()->vkCommandBuffer();
903 vkInfo.fCompatibleRenderPass = cbInfo.fRenderPass->vkRenderPass();
Greg Danielb353eeb2018-12-05 11:01:58 -0500904 SkAssertResult(cbInfo.fRenderPass->colorAttachmentIndex(&vkInfo.fColorAttachmentIndex));
Greg Daniel64cc9aa2018-10-19 13:54:56 -0400905 vkInfo.fFormat = targetImage->imageFormat();
906 vkInfo.fDrawBounds = &bounds;
Stan Ilievcb580602019-02-26 11:36:07 -0500907#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
908 vkInfo.fImage = targetImage->image();
909#else
910 vkInfo.fImage = VK_NULL_HANDLE;
911#endif //SK_BUILD_FOR_ANDROID_FRAMEWORK
Greg Daniel64cc9aa2018-10-19 13:54:56 -0400912
913 GrBackendDrawableInfo info(vkInfo);
914
Eric Karlc0b2ba22019-01-22 19:40:35 -0800915 // After we draw into the command buffer via the drawable, cached state we have may be invalid.
916 cbInfo.currentCmdBuf()->invalidateState();
Eric Karla8878a12019-02-07 18:17:43 -0800917 // Also assume that the drawable produced output.
918 cbInfo.fIsEmpty = false;
Eric Karlc0b2ba22019-01-22 19:40:35 -0800919
Greg Daniel64cc9aa2018-10-19 13:54:56 -0400920 drawable->draw(info);
921 fGpu->addDrawable(std::move(drawable));
922
923 if (bounds.extent.width == 0 || bounds.extent.height == 0) {
924 cbInfo.fBounds.join(target->getBoundsRect());
925 } else {
926 cbInfo.fBounds.join(SkRect::MakeXYWH(bounds.offset.x, bounds.offset.y,
927 bounds.extent.width, bounds.extent.height));
928 }
929}
930