blob: 628cdb221f18fe2f08678b4142679d310ff4c615 [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
Greg Daniel2d41d0d2019-08-26 11:08:51 -04008#include "src/gpu/vk/GrVkOpsRenderPass.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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "src/gpu/vk/GrVkCommandBuffer.h"
20#include "src/gpu/vk/GrVkCommandPool.h"
21#include "src/gpu/vk/GrVkGpu.h"
22#include "src/gpu/vk/GrVkPipeline.h"
23#include "src/gpu/vk/GrVkRenderPass.h"
24#include "src/gpu/vk/GrVkRenderTarget.h"
25#include "src/gpu/vk/GrVkResourceProvider.h"
26#include "src/gpu/vk/GrVkSemaphore.h"
27#include "src/gpu/vk/GrVkTexture.h"
egdaniel066df7c2016-06-08 14:02:27 -070028
Brian Salomon5d8f1cc2019-04-24 09:03:53 -040029GrVkPrimaryCommandBufferTask::~GrVkPrimaryCommandBufferTask() = default;
30GrVkPrimaryCommandBufferTask::GrVkPrimaryCommandBufferTask() = default;
31
32namespace {
33
34class InlineUpload : public GrVkPrimaryCommandBufferTask {
35public:
36 InlineUpload(GrOpFlushState* state, const GrDeferredTextureUploadFn& upload)
37 : fFlushState(state), fUpload(upload) {}
Brian Salomon5d8f1cc2019-04-24 09:03:53 -040038
39 void execute(const Args& args) override { fFlushState->doUpload(fUpload); }
40
41private:
42 GrOpFlushState* fFlushState;
43 GrDeferredTextureUploadFn fUpload;
44};
45
Brian Salomon5d8f1cc2019-04-24 09:03:53 -040046} // anonymous namespace
47
48/////////////////////////////////////////////////////////////////////////////
49
Robert Phillips6b47c7d2017-08-29 07:24:09 -040050void get_vk_load_store_ops(GrLoadOp loadOpIn, GrStoreOp storeOpIn,
egdaniel066df7c2016-06-08 14:02:27 -070051 VkAttachmentLoadOp* loadOp, VkAttachmentStoreOp* storeOp) {
Robert Phillips95214472017-08-08 18:00:03 -040052 switch (loadOpIn) {
Robert Phillips6b47c7d2017-08-29 07:24:09 -040053 case GrLoadOp::kLoad:
egdaniel066df7c2016-06-08 14:02:27 -070054 *loadOp = VK_ATTACHMENT_LOAD_OP_LOAD;
egdaniel066df7c2016-06-08 14:02:27 -070055 break;
Robert Phillips6b47c7d2017-08-29 07:24:09 -040056 case GrLoadOp::kClear:
egdaniel9cb63402016-06-23 08:37:05 -070057 *loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
58 break;
Robert Phillips6b47c7d2017-08-29 07:24:09 -040059 case GrLoadOp::kDiscard:
egdaniel9cb63402016-06-23 08:37:05 -070060 *loadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
61 break;
62 default:
63 SK_ABORT("Invalid LoadOp");
egdaniel066df7c2016-06-08 14:02:27 -070064 *loadOp = VK_ATTACHMENT_LOAD_OP_LOAD;
egdaniel9cb63402016-06-23 08:37:05 -070065 }
66
Robert Phillips95214472017-08-08 18:00:03 -040067 switch (storeOpIn) {
Robert Phillips6b47c7d2017-08-29 07:24:09 -040068 case GrStoreOp::kStore:
egdaniel066df7c2016-06-08 14:02:27 -070069 *storeOp = VK_ATTACHMENT_STORE_OP_STORE;
70 break;
Robert Phillips6b47c7d2017-08-29 07:24:09 -040071 case GrStoreOp::kDiscard:
egdaniel066df7c2016-06-08 14:02:27 -070072 *storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
73 break;
brianosman0bbc3712016-06-14 04:53:09 -070074 default:
egdaniel9cb63402016-06-23 08:37:05 -070075 SK_ABORT("Invalid StoreOp");
brianosman0bbc3712016-06-14 04:53:09 -070076 *storeOp = VK_ATTACHMENT_STORE_OP_STORE;
egdaniel066df7c2016-06-08 14:02:27 -070077 }
78}
79
Greg Daniel2d41d0d2019-08-26 11:08:51 -040080GrVkOpsRenderPass::GrVkOpsRenderPass(GrVkGpu* gpu) : fGpu(gpu) {}
Brian Salomonc293a292016-11-30 13:38:32 -050081
Greg Daniel2d41d0d2019-08-26 11:08:51 -040082void GrVkOpsRenderPass::init() {
Brian Salomonc293a292016-11-30 13:38:32 -050083 GrVkRenderPass::LoadStoreOps vkColorOps(fVkColorLoadOp, fVkColorStoreOp);
84 GrVkRenderPass::LoadStoreOps vkStencilOps(fVkStencilLoadOp, fVkStencilStoreOp);
egdaniel9cb63402016-06-23 08:37:05 -070085
Greg Daniel36a77ee2016-10-18 10:33:25 -040086 CommandBufferInfo& cbInfo = fCommandBufferInfos.push_back();
Brian Salomonc293a292016-11-30 13:38:32 -050087 SkASSERT(fCommandBufferInfos.count() == 1);
Greg Daniel22bc8652017-03-22 15:45:43 -040088 fCurrentCmdInfo = 0;
Greg Daniel36a77ee2016-10-18 10:33:25 -040089
Robert Phillips19e51dc2017-08-09 09:30:51 -040090 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(fRenderTarget);
91 const GrVkResourceProvider::CompatibleRPHandle& rpHandle = vkRT->compatibleRenderPassHandle();
egdaniel066df7c2016-06-08 14:02:27 -070092 if (rpHandle.isValid()) {
Greg Daniel36a77ee2016-10-18 10:33:25 -040093 cbInfo.fRenderPass = fGpu->resourceProvider().findRenderPass(rpHandle,
94 vkColorOps,
95 vkStencilOps);
egdaniel066df7c2016-06-08 14:02:27 -070096 } else {
Robert Phillips19e51dc2017-08-09 09:30:51 -040097 cbInfo.fRenderPass = fGpu->resourceProvider().findRenderPass(*vkRT,
Greg Daniel36a77ee2016-10-18 10:33:25 -040098 vkColorOps,
99 vkStencilOps);
egdaniel066df7c2016-06-08 14:02:27 -0700100 }
101
Brian Osmancb3d0872018-10-16 15:19:28 -0400102 cbInfo.fColorClearValue.color.float32[0] = fClearColor[0];
103 cbInfo.fColorClearValue.color.float32[1] = fClearColor[1];
104 cbInfo.fColorClearValue.color.float32[2] = fClearColor[2];
105 cbInfo.fColorClearValue.color.float32[3] = fClearColor[3];
egdaniel9cb63402016-06-23 08:37:05 -0700106
Robert Phillips380b90c2017-08-30 07:41:07 -0400107 if (VK_ATTACHMENT_LOAD_OP_CLEAR == fVkColorLoadOp) {
Greg Daniela41a74a2018-10-09 12:59:23 +0000108 cbInfo.fBounds = SkRect::MakeWH(vkRT->width(), vkRT->height());
Robert Phillips380b90c2017-08-30 07:41:07 -0400109 } else {
110 cbInfo.fBounds.setEmpty();
111 }
Greg Daniela3c68df2018-03-16 13:46:53 -0400112
113 if (VK_ATTACHMENT_LOAD_OP_CLEAR == fVkColorLoadOp) {
114 cbInfo.fLoadStoreState = LoadStoreState::kStartsWithClear;
115 } else if (VK_ATTACHMENT_LOAD_OP_LOAD == fVkColorLoadOp &&
116 VK_ATTACHMENT_STORE_OP_STORE == fVkColorStoreOp) {
117 cbInfo.fLoadStoreState = LoadStoreState::kLoadAndStore;
118 } else if (VK_ATTACHMENT_LOAD_OP_DONT_CARE == fVkColorLoadOp) {
119 cbInfo.fLoadStoreState = LoadStoreState::kStartsWithDiscard;
120 }
Greg Daniel36a77ee2016-10-18 10:33:25 -0400121
Greg Daniel228518f2019-08-07 16:55:17 -0400122 cbInfo.fCommandBuffer = fGpu->cmdPool()->findOrCreateSecondaryCommandBuffer(fGpu);
Robert Phillips19e51dc2017-08-09 09:30:51 -0400123 cbInfo.currentCmdBuf()->begin(fGpu, vkRT->framebuffer(), cbInfo.fRenderPass);
egdaniel066df7c2016-06-08 14:02:27 -0700124}
125
Greg Daniel2d41d0d2019-08-26 11:08:51 -0400126void GrVkOpsRenderPass::initWrapped() {
Greg Daniel070cbaf2019-01-03 17:35:54 -0500127 CommandBufferInfo& cbInfo = fCommandBufferInfos.push_back();
128 SkASSERT(fCommandBufferInfos.count() == 1);
129 fCurrentCmdInfo = 0;
130
131 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(fRenderTarget);
132 SkASSERT(vkRT->wrapsSecondaryCommandBuffer());
133 cbInfo.fRenderPass = vkRT->externalRenderPass();
134 cbInfo.fRenderPass->ref();
135
136 cbInfo.fBounds.setEmpty();
Greg Daniel228518f2019-08-07 16:55:17 -0400137 cbInfo.fCommandBuffer.reset(
Greg Daniel8daf3b72019-07-30 09:57:26 -0400138 GrVkSecondaryCommandBuffer::Create(vkRT->getExternalSecondaryCommandBuffer()));
Greg Daniel070cbaf2019-01-03 17:35:54 -0500139 cbInfo.currentCmdBuf()->begin(fGpu, nullptr, cbInfo.fRenderPass);
140}
Brian Salomonc293a292016-11-30 13:38:32 -0500141
Greg Daniel2d41d0d2019-08-26 11:08:51 -0400142GrVkOpsRenderPass::~GrVkOpsRenderPass() {
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400143 this->reset();
egdaniel066df7c2016-06-08 14:02:27 -0700144}
145
Greg Daniel2d41d0d2019-08-26 11:08:51 -0400146GrGpu* GrVkOpsRenderPass::gpu() { return fGpu; }
egdaniel9cb63402016-06-23 08:37:05 -0700147
Greg Daniel2d41d0d2019-08-26 11:08:51 -0400148void GrVkOpsRenderPass::end() {
Greg Daniel22bc8652017-03-22 15:45:43 -0400149 if (fCurrentCmdInfo >= 0) {
150 fCommandBufferInfos[fCurrentCmdInfo].currentCmdBuf()->end(fGpu);
Brian Salomonc293a292016-11-30 13:38:32 -0500151 }
egdaniel066df7c2016-06-08 14:02:27 -0700152}
153
Greg Daniel2d41d0d2019-08-26 11:08:51 -0400154void GrVkOpsRenderPass::submit() {
Brian Salomonc293a292016-11-30 13:38:32 -0500155 if (!fRenderTarget) {
156 return;
157 }
Robert Phillips19e51dc2017-08-09 09:30:51 -0400158
159 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(fRenderTarget);
Robert Phillips19e51dc2017-08-09 09:30:51 -0400160 GrVkImage* targetImage = vkRT->msaaImage() ? vkRT->msaaImage() : vkRT;
Greg Daniel45a44de2018-02-27 10:07:29 -0500161 GrStencilAttachment* stencil = fRenderTarget->renderTargetPriv().getStencilAttachment();
Brian Salomon24d377e2019-04-23 15:24:31 -0400162 auto currPreCmd = fPreCommandBufferTasks.begin();
egdaniel9cb63402016-06-23 08:37:05 -0700163
Greg Daniel46cfbc62019-06-07 11:43:30 -0400164 GrVkPrimaryCommandBufferTask::Args taskArgs{fGpu, fRenderTarget};
Greg Daniel36a77ee2016-10-18 10:33:25 -0400165 for (int i = 0; i < fCommandBufferInfos.count(); ++i) {
166 CommandBufferInfo& cbInfo = fCommandBufferInfos[i];
167
Brian Salomon24d377e2019-04-23 15:24:31 -0400168 for (int c = 0; c < cbInfo.fNumPreCmds; ++c, ++currPreCmd) {
Brian Salomon5d8f1cc2019-04-24 09:03:53 -0400169 currPreCmd->execute(taskArgs);
Greg Daniel77b53f62016-10-18 11:48:51 -0400170 }
171
Greg Daniel38c3d932018-03-16 14:22:30 -0400172 // TODO: Many things create a scratch texture which adds the discard immediately, but then
173 // don't draw to it right away. This causes the discard to be ignored and we get yelled at
174 // for loading uninitialized data. However, once MDB lands with reordering, the discard will
175 // get reordered with the rest of the draw commands and we can remove the discard check.
176 if (cbInfo.fIsEmpty &&
177 cbInfo.fLoadStoreState != LoadStoreState::kStartsWithClear &&
178 cbInfo.fLoadStoreState != LoadStoreState::kStartsWithDiscard) {
Greg Daniel77b53f62016-10-18 11:48:51 -0400179 // We have sumbitted no actual draw commands to the command buffer and we are not using
180 // the render pass to do a clear so there is no need to submit anything.
181 continue;
182 }
Greg Daniel38c3d932018-03-16 14:22:30 -0400183
Greg Daniel070cbaf2019-01-03 17:35:54 -0500184 // We don't want to actually submit the secondary command buffer if it is wrapped.
185 if (this->wrapsSecondaryCommandBuffer()) {
186 // If we have any sampled images set their layout now.
Chris Dalton298238a2019-02-21 16:28:44 -0500187 for (int j = 0; j < cbInfo.fSampledTextures.count(); ++j) {
188 cbInfo.fSampledTextures[j]->setImageLayout(
189 fGpu, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, VK_ACCESS_SHADER_READ_BIT,
190 VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT, false);
Greg Daniel070cbaf2019-01-03 17:35:54 -0500191 }
192
193 // There should have only been one secondary command buffer in the wrapped case so it is
194 // safe to just return here.
195 SkASSERT(fCommandBufferInfos.count() == 1);
196 return;
197 }
198
Greg Danieldbdba602018-04-20 11:52:43 -0400199 // Make sure if we only have a discard load that we execute the discard on the whole image.
200 // TODO: Once we improve our tracking of discards so that we never end up flushing a discard
201 // call with no actually ops, remove this.
202 if (cbInfo.fIsEmpty && cbInfo.fLoadStoreState == LoadStoreState::kStartsWithDiscard) {
Greg Daniela41a74a2018-10-09 12:59:23 +0000203 cbInfo.fBounds = SkRect::MakeWH(vkRT->width(), vkRT->height());
Greg Danieldbdba602018-04-20 11:52:43 -0400204 }
205
Mike Reed9ea63152019-08-22 16:19:50 -0400206 if (cbInfo.fBounds.intersect(SkRect::MakeIWH(fRenderTarget->width(),
207 fRenderTarget->height()))) {
Greg Daniel38c3d932018-03-16 14:22:30 -0400208 // Make sure we do the following layout changes after all copies, uploads, or any other
209 // pre-work is done since we may change the layouts in the pre-work. Also since the
210 // draws will be submitted in different render passes, we need to guard againts write
211 // and write issues.
212
213 // Change layout of our render target so it can be used as the color attachment.
Greg Danielf7828d02018-10-09 12:01:32 -0400214 // TODO: If we know that we will never be blending or loading the attachment we could
215 // drop the VK_ACCESS_COLOR_ATTACHMENT_READ_BIT.
Greg Daniel38c3d932018-03-16 14:22:30 -0400216 targetImage->setImageLayout(fGpu,
217 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Greg Danielf7828d02018-10-09 12:01:32 -0400218 VK_ACCESS_COLOR_ATTACHMENT_READ_BIT |
Greg Daniel38c3d932018-03-16 14:22:30 -0400219 VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
Greg Danielf7828d02018-10-09 12:01:32 -0400220 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
Greg Daniel38c3d932018-03-16 14:22:30 -0400221 false);
222
223 // If we are using a stencil attachment we also need to update its layout
224 if (stencil) {
225 GrVkStencilAttachment* vkStencil = (GrVkStencilAttachment*)stencil;
Greg Danielf7828d02018-10-09 12:01:32 -0400226 // We need the write and read access bits since we may load and store the stencil.
227 // The initial load happens in the VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT so we
228 // wait there.
Greg Daniel38c3d932018-03-16 14:22:30 -0400229 vkStencil->setImageLayout(fGpu,
230 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
231 VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT |
232 VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT,
Greg Danielf7828d02018-10-09 12:01:32 -0400233 VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT,
Greg Daniel38c3d932018-03-16 14:22:30 -0400234 false);
235 }
236
237 // If we have any sampled images set their layout now.
Chris Dalton298238a2019-02-21 16:28:44 -0500238 for (int j = 0; j < cbInfo.fSampledTextures.count(); ++j) {
239 cbInfo.fSampledTextures[j]->setImageLayout(
240 fGpu, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, VK_ACCESS_SHADER_READ_BIT,
241 VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT, false);
Greg Daniel38c3d932018-03-16 14:22:30 -0400242 }
243
Greg Daniel36a77ee2016-10-18 10:33:25 -0400244 SkIRect iBounds;
245 cbInfo.fBounds.roundOut(&iBounds);
246
Greg Daniel228518f2019-08-07 16:55:17 -0400247 fGpu->submitSecondaryCommandBuffer(std::move(cbInfo.fCommandBuffer), cbInfo.fRenderPass,
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400248 &cbInfo.fColorClearValue, vkRT, fOrigin, iBounds);
Greg Daniel36a77ee2016-10-18 10:33:25 -0400249 }
250 }
Brian Salomon24d377e2019-04-23 15:24:31 -0400251 SkASSERT(currPreCmd == fPreCommandBufferTasks.end());
egdaniel9cb63402016-06-23 08:37:05 -0700252}
253
Greg Daniel2d41d0d2019-08-26 11:08:51 -0400254void GrVkOpsRenderPass::set(GrRenderTarget* rt, GrSurfaceOrigin origin,
255 const GrOpsRenderPass::LoadAndStoreInfo& colorInfo,
256 const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilInfo) {
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400257 SkASSERT(!fRenderTarget);
258 SkASSERT(fCommandBufferInfos.empty());
259 SkASSERT(-1 == fCurrentCmdInfo);
Robert Phillips9da87e02019-02-04 13:26:26 -0500260 SkASSERT(fGpu == rt->getContext()->priv().getGpu());
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400261 SkASSERT(!fLastPipelineState);
262
Greg Danielb0c7ad12019-06-06 17:23:35 +0000263#ifdef SK_DEBUG
264 fIsActive = true;
265#endif
266
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400267 this->INHERITED::set(rt, origin);
268
Greg Daniel070cbaf2019-01-03 17:35:54 -0500269 if (this->wrapsSecondaryCommandBuffer()) {
270 this->initWrapped();
271 return;
272 }
273
Brian Osman9a9baae2018-11-05 15:06:26 -0500274 fClearColor = colorInfo.fClearColor;
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400275
276 get_vk_load_store_ops(colorInfo.fLoadOp, colorInfo.fStoreOp,
277 &fVkColorLoadOp, &fVkColorStoreOp);
278
279 get_vk_load_store_ops(stencilInfo.fLoadOp, stencilInfo.fStoreOp,
280 &fVkStencilLoadOp, &fVkStencilStoreOp);
281
282 this->init();
283}
284
Greg Daniel2d41d0d2019-08-26 11:08:51 -0400285void GrVkOpsRenderPass::reset() {
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400286 for (int i = 0; i < fCommandBufferInfos.count(); ++i) {
287 CommandBufferInfo& cbInfo = fCommandBufferInfos[i];
Greg Daniel228518f2019-08-07 16:55:17 -0400288 if (cbInfo.fCommandBuffer) {
289 cbInfo.fCommandBuffer.release()->recycle(fGpu);
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400290 }
291 cbInfo.fRenderPass->unref(fGpu);
292 }
293 fCommandBufferInfos.reset();
Brian Salomon24d377e2019-04-23 15:24:31 -0400294 fPreCommandBufferTasks.reset();
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400295
296 fCurrentCmdInfo = -1;
297
298 fLastPipelineState = nullptr;
299 fRenderTarget = nullptr;
Greg Danielb0c7ad12019-06-06 17:23:35 +0000300
301#ifdef SK_DEBUG
302 fIsActive = false;
303#endif
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400304}
305
Greg Daniel2d41d0d2019-08-26 11:08:51 -0400306bool GrVkOpsRenderPass::wrapsSecondaryCommandBuffer() const {
Greg Daniel070cbaf2019-01-03 17:35:54 -0500307 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(fRenderTarget);
308 return vkRT->wrapsSecondaryCommandBuffer();
309}
310
Greg Daniel64cc9aa2018-10-19 13:54:56 -0400311////////////////////////////////////////////////////////////////////////////////
312
Greg Daniel2d41d0d2019-08-26 11:08:51 -0400313void GrVkOpsRenderPass::insertEventMarker(const char* msg) {
Robert Phillips65a88fa2017-08-08 08:36:22 -0400314 // TODO: does Vulkan have a correlate?
315}
316
Greg Daniel2d41d0d2019-08-26 11:08:51 -0400317void GrVkOpsRenderPass::onClearStencilClip(const GrFixedClip& clip, bool insideStencilMask) {
Chris Dalton94c04682017-11-01 17:15:06 -0600318 SkASSERT(!clip.hasWindowRectangles());
319
Jim Van Verth6a40abc2017-11-02 16:56:09 +0000320 CommandBufferInfo& cbInfo = fCommandBufferInfos[fCurrentCmdInfo];
321
Greg Daniel65a09272016-10-12 09:47:22 -0400322 GrStencilAttachment* sb = fRenderTarget->renderTargetPriv().getStencilAttachment();
egdaniel9cb63402016-06-23 08:37:05 -0700323 // this should only be called internally when we know we have a
324 // stencil buffer.
325 SkASSERT(sb);
326 int stencilBitCount = sb->bits();
327
328 // The contract with the callers does not guarantee that we preserve all bits in the stencil
329 // during this clear. Thus we will clear the entire stencil to the desired value.
330
331 VkClearDepthStencilValue vkStencilColor;
332 memset(&vkStencilColor, 0, sizeof(VkClearDepthStencilValue));
csmartdalton29df7602016-08-31 11:55:52 -0700333 if (insideStencilMask) {
egdaniel9cb63402016-06-23 08:37:05 -0700334 vkStencilColor.stencil = (1 << (stencilBitCount - 1));
335 } else {
336 vkStencilColor.stencil = 0;
337 }
338
339 VkClearRect clearRect;
340 // Flip rect if necessary
csmartdalton29df7602016-08-31 11:55:52 -0700341 SkIRect vkRect;
Brian Salomond818ebf2018-07-02 14:08:49 +0000342 if (!clip.scissorEnabled()) {
Greg Daniela41a74a2018-10-09 12:59:23 +0000343 vkRect.setXYWH(0, 0, fRenderTarget->width(), fRenderTarget->height());
Robert Phillips4f101a72017-07-28 08:42:04 -0400344 } else if (kBottomLeft_GrSurfaceOrigin != fOrigin) {
csmartdalton29df7602016-08-31 11:55:52 -0700345 vkRect = clip.scissorRect();
346 } else {
347 const SkIRect& scissor = clip.scissorRect();
Greg Daniel65a09272016-10-12 09:47:22 -0400348 vkRect.setLTRB(scissor.fLeft, fRenderTarget->height() - scissor.fBottom,
349 scissor.fRight, fRenderTarget->height() - scissor.fTop);
egdaniel9cb63402016-06-23 08:37:05 -0700350 }
351
352 clearRect.rect.offset = { vkRect.fLeft, vkRect.fTop };
353 clearRect.rect.extent = { (uint32_t)vkRect.width(), (uint32_t)vkRect.height() };
354
355 clearRect.baseArrayLayer = 0;
356 clearRect.layerCount = 1;
357
358 uint32_t stencilIndex;
Greg Daniel36a77ee2016-10-18 10:33:25 -0400359 SkAssertResult(cbInfo.fRenderPass->stencilAttachmentIndex(&stencilIndex));
egdaniel9cb63402016-06-23 08:37:05 -0700360
361 VkClearAttachment attachment;
362 attachment.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
363 attachment.colorAttachment = 0; // this value shouldn't matter
364 attachment.clearValue.depthStencil = vkStencilColor;
365
Greg Daniel22bc8652017-03-22 15:45:43 -0400366 cbInfo.currentCmdBuf()->clearAttachments(fGpu, 1, &attachment, 1, &clearRect);
Greg Daniel77b53f62016-10-18 11:48:51 -0400367 cbInfo.fIsEmpty = false;
Greg Daniel36a77ee2016-10-18 10:33:25 -0400368
369 // Update command buffer bounds
Brian Salomond818ebf2018-07-02 14:08:49 +0000370 if (!clip.scissorEnabled()) {
Greg Daniel36a77ee2016-10-18 10:33:25 -0400371 cbInfo.fBounds.join(fRenderTarget->getBoundsRect());
372 } else {
373 cbInfo.fBounds.join(SkRect::Make(clip.scissorRect()));
374 }
egdaniel9cb63402016-06-23 08:37:05 -0700375}
376
Greg Daniel2d41d0d2019-08-26 11:08:51 -0400377void GrVkOpsRenderPass::onClear(const GrFixedClip& clip, const SkPMColor4f& color) {
Jim Van Verth6a40abc2017-11-02 16:56:09 +0000378 // parent class should never let us get here with no RT
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700379 SkASSERT(!clip.hasWindowRectangles());
egdaniel9cb63402016-06-23 08:37:05 -0700380
Greg Daniel22bc8652017-03-22 15:45:43 -0400381 CommandBufferInfo& cbInfo = fCommandBufferInfos[fCurrentCmdInfo];
Greg Daniel36a77ee2016-10-18 10:33:25 -0400382
Brian Osman9a9baae2018-11-05 15:06:26 -0500383 VkClearColorValue vkColor = {{color.fR, color.fG, color.fB, color.fA}};
egdaniel9cb63402016-06-23 08:37:05 -0700384
Greg Daniel674ee742019-08-27 13:12:33 -0400385 // If we end up in a situation where we are calling clear without a scissior then in general it
386 // means we missed an opportunity higher up the stack to set the load op to be a clear. However,
387 // there are situations where higher up we couldn't discard the previous ops and set a clear
388 // load op (e.g. if we needed to execute a wait op). Thus we also have the empty check here.
Greg Daniel4fe92572019-09-03 11:16:40 -0400389 // TODO: Make the waitOp a RenderTask instead so we can clear out the GrOpsTask for a clear. We
390 // can then reenable this assert assuming we can't get messed up by a waitOp.
391 //SkASSERT(!cbInfo.fIsEmpty || clip.scissorEnabled());
egdaniel9cb63402016-06-23 08:37:05 -0700392
393 // We always do a sub rect clear with clearAttachments since we are inside a render pass
394 VkClearRect clearRect;
395 // Flip rect if necessary
csmartdalton29df7602016-08-31 11:55:52 -0700396 SkIRect vkRect;
Brian Salomond818ebf2018-07-02 14:08:49 +0000397 if (!clip.scissorEnabled()) {
Greg Daniela41a74a2018-10-09 12:59:23 +0000398 vkRect.setXYWH(0, 0, fRenderTarget->width(), fRenderTarget->height());
Robert Phillips4f101a72017-07-28 08:42:04 -0400399 } else if (kBottomLeft_GrSurfaceOrigin != fOrigin) {
csmartdalton29df7602016-08-31 11:55:52 -0700400 vkRect = clip.scissorRect();
401 } else {
402 const SkIRect& scissor = clip.scissorRect();
Greg Daniel65a09272016-10-12 09:47:22 -0400403 vkRect.setLTRB(scissor.fLeft, fRenderTarget->height() - scissor.fBottom,
404 scissor.fRight, fRenderTarget->height() - scissor.fTop);
egdaniel9cb63402016-06-23 08:37:05 -0700405 }
406 clearRect.rect.offset = { vkRect.fLeft, vkRect.fTop };
407 clearRect.rect.extent = { (uint32_t)vkRect.width(), (uint32_t)vkRect.height() };
408 clearRect.baseArrayLayer = 0;
409 clearRect.layerCount = 1;
410
411 uint32_t colorIndex;
Greg Daniel36a77ee2016-10-18 10:33:25 -0400412 SkAssertResult(cbInfo.fRenderPass->colorAttachmentIndex(&colorIndex));
egdaniel9cb63402016-06-23 08:37:05 -0700413
414 VkClearAttachment attachment;
415 attachment.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
416 attachment.colorAttachment = colorIndex;
417 attachment.clearValue.color = vkColor;
418
Greg Daniel22bc8652017-03-22 15:45:43 -0400419 cbInfo.currentCmdBuf()->clearAttachments(fGpu, 1, &attachment, 1, &clearRect);
Greg Daniel77b53f62016-10-18 11:48:51 -0400420 cbInfo.fIsEmpty = false;
Greg Daniel36a77ee2016-10-18 10:33:25 -0400421
422 // Update command buffer bounds
Brian Salomond818ebf2018-07-02 14:08:49 +0000423 if (!clip.scissorEnabled()) {
Greg Daniel36a77ee2016-10-18 10:33:25 -0400424 cbInfo.fBounds.join(fRenderTarget->getBoundsRect());
425 } else {
426 cbInfo.fBounds.join(SkRect::Make(clip.scissorRect()));
427 }
egdaniel9cb63402016-06-23 08:37:05 -0700428 return;
429}
430
Greg Daniel500d58b2017-08-24 15:59:33 -0400431////////////////////////////////////////////////////////////////////////////////
432
Greg Daniel2d41d0d2019-08-26 11:08:51 -0400433void GrVkOpsRenderPass::addAdditionalRenderPass() {
Robert Phillips19e51dc2017-08-09 09:30:51 -0400434 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(fRenderTarget);
435
Greg Daniel22bc8652017-03-22 15:45:43 -0400436 fCommandBufferInfos[fCurrentCmdInfo].currentCmdBuf()->end(fGpu);
Greg Daniel77b53f62016-10-18 11:48:51 -0400437
438 CommandBufferInfo& cbInfo = fCommandBufferInfos.push_back();
Greg Daniel22bc8652017-03-22 15:45:43 -0400439 fCurrentCmdInfo++;
Greg Daniel77b53f62016-10-18 11:48:51 -0400440
441 GrVkRenderPass::LoadStoreOps vkColorOps(VK_ATTACHMENT_LOAD_OP_LOAD,
442 VK_ATTACHMENT_STORE_OP_STORE);
443 GrVkRenderPass::LoadStoreOps vkStencilOps(VK_ATTACHMENT_LOAD_OP_LOAD,
444 VK_ATTACHMENT_STORE_OP_STORE);
445
446 const GrVkResourceProvider::CompatibleRPHandle& rpHandle =
Robert Phillips19e51dc2017-08-09 09:30:51 -0400447 vkRT->compatibleRenderPassHandle();
Greg Daniel77b53f62016-10-18 11:48:51 -0400448 if (rpHandle.isValid()) {
449 cbInfo.fRenderPass = fGpu->resourceProvider().findRenderPass(rpHandle,
450 vkColorOps,
451 vkStencilOps);
452 } else {
Robert Phillips19e51dc2017-08-09 09:30:51 -0400453 cbInfo.fRenderPass = fGpu->resourceProvider().findRenderPass(*vkRT,
Greg Daniel77b53f62016-10-18 11:48:51 -0400454 vkColorOps,
455 vkStencilOps);
456 }
Greg Daniela3c68df2018-03-16 13:46:53 -0400457 cbInfo.fLoadStoreState = LoadStoreState::kLoadAndStore;
Greg Daniel77b53f62016-10-18 11:48:51 -0400458
Greg Daniel228518f2019-08-07 16:55:17 -0400459 cbInfo.fCommandBuffer = fGpu->cmdPool()->findOrCreateSecondaryCommandBuffer(fGpu);
Greg Daniel77b53f62016-10-18 11:48:51 -0400460 // It shouldn't matter what we set the clear color to here since we will assume loading of the
461 // attachment.
462 memset(&cbInfo.fColorClearValue, 0, sizeof(VkClearValue));
463 cbInfo.fBounds.setEmpty();
Greg Daniel77b53f62016-10-18 11:48:51 -0400464
Robert Phillips19e51dc2017-08-09 09:30:51 -0400465 cbInfo.currentCmdBuf()->begin(fGpu, vkRT->framebuffer(), cbInfo.fRenderPass);
Greg Daniel77b53f62016-10-18 11:48:51 -0400466}
467
Greg Daniel2d41d0d2019-08-26 11:08:51 -0400468void GrVkOpsRenderPass::inlineUpload(GrOpFlushState* state,
Brian Salomon943ed792017-10-30 09:37:55 -0400469 GrDeferredTextureUploadFn& upload) {
Greg Daniel22bc8652017-03-22 15:45:43 -0400470 if (!fCommandBufferInfos[fCurrentCmdInfo].fIsEmpty) {
471 this->addAdditionalRenderPass();
Greg Daniel77b53f62016-10-18 11:48:51 -0400472 }
Brian Salomon24d377e2019-04-23 15:24:31 -0400473
Brian Salomon24d377e2019-04-23 15:24:31 -0400474 fPreCommandBufferTasks.emplace<InlineUpload>(state, upload);
475 ++fCommandBufferInfos[fCurrentCmdInfo].fNumPreCmds;
Greg Daniel77b53f62016-10-18 11:48:51 -0400476}
477
egdaniel9cb63402016-06-23 08:37:05 -0700478////////////////////////////////////////////////////////////////////////////////
479
Greg Daniel2d41d0d2019-08-26 11:08:51 -0400480void GrVkOpsRenderPass::bindGeometry(const GrGpuBuffer* indexBuffer,
Brian Salomondbf70722019-02-07 11:31:24 -0500481 const GrGpuBuffer* vertexBuffer,
482 const GrGpuBuffer* instanceBuffer) {
Chris Daltonff926502017-05-03 14:36:54 -0400483 GrVkSecondaryCommandBuffer* currCmdBuf = fCommandBufferInfos[fCurrentCmdInfo].currentCmdBuf();
egdaniel9cb63402016-06-23 08:37:05 -0700484 // There is no need to put any memory barriers to make sure host writes have finished here.
485 // When a command buffer is submitted to a queue, there is an implicit memory barrier that
486 // occurs for all host writes. Additionally, BufferMemoryBarriers are not allowed inside of
487 // an active RenderPass.
egdaniel9cb63402016-06-23 08:37:05 -0700488
Chris Dalton1d616352017-05-31 12:51:23 -0600489 // Here our vertex and instance inputs need to match the same 0-based bindings they were
490 // assigned in GrVkPipeline. That is, vertex first (if any) followed by instance.
491 uint32_t binding = 0;
492
Brian Salomon802cb312018-06-08 18:05:20 -0400493 if (vertexBuffer) {
Chris Dalton1d616352017-05-31 12:51:23 -0600494 SkASSERT(vertexBuffer);
Chris Dalton1d616352017-05-31 12:51:23 -0600495 SkASSERT(!vertexBuffer->isMapped());
496
497 currCmdBuf->bindInputBuffer(fGpu, binding++,
498 static_cast<const GrVkVertexBuffer*>(vertexBuffer));
499 }
500
Brian Salomon802cb312018-06-08 18:05:20 -0400501 if (instanceBuffer) {
Chris Dalton1d616352017-05-31 12:51:23 -0600502 SkASSERT(instanceBuffer);
Chris Dalton1d616352017-05-31 12:51:23 -0600503 SkASSERT(!instanceBuffer->isMapped());
504
505 currCmdBuf->bindInputBuffer(fGpu, binding++,
506 static_cast<const GrVkVertexBuffer*>(instanceBuffer));
507 }
Chris Daltonff926502017-05-03 14:36:54 -0400508 if (indexBuffer) {
509 SkASSERT(indexBuffer);
510 SkASSERT(!indexBuffer->isMapped());
egdaniel9cb63402016-06-23 08:37:05 -0700511
Chris Daltonff926502017-05-03 14:36:54 -0400512 currCmdBuf->bindIndexBuffer(fGpu, static_cast<const GrVkIndexBuffer*>(indexBuffer));
egdaniel9cb63402016-06-23 08:37:05 -0700513 }
514}
515
Greg Daniel2d41d0d2019-08-26 11:08:51 -0400516GrVkPipelineState* GrVkOpsRenderPass::prepareDrawState(
Brian Salomon49348902018-06-26 09:12:38 -0400517 const GrPrimitiveProcessor& primProc,
518 const GrPipeline& pipeline,
519 const GrPipeline::FixedDynamicState* fixedDynamicState,
520 const GrPipeline::DynamicStateArrays* dynamicStateArrays,
521 GrPrimitiveType primitiveType) {
Greg Daniel22bc8652017-03-22 15:45:43 -0400522 CommandBufferInfo& cbInfo = fCommandBufferInfos[fCurrentCmdInfo];
523 SkASSERT(cbInfo.fRenderPass);
Greg Daniel36a77ee2016-10-18 10:33:25 -0400524
Greg Daniel99b88e02018-10-03 15:31:20 -0400525 VkRenderPass compatibleRenderPass = cbInfo.fRenderPass->vkRenderPass();
526
Greg Daniel9a51a862018-11-30 10:18:14 -0500527 const GrTextureProxy* const* primProcProxies = nullptr;
528 if (dynamicStateArrays && dynamicStateArrays->fPrimitiveProcessorTextures) {
529 primProcProxies = dynamicStateArrays->fPrimitiveProcessorTextures;
530 } else if (fixedDynamicState) {
531 primProcProxies = fixedDynamicState->fPrimitiveProcessorTextures;
532 }
533
534 SkASSERT(SkToBool(primProcProxies) == SkToBool(primProc.numTextureSamplers()));
535
Greg Daniel09eeefb2017-10-16 15:15:02 -0400536 GrVkPipelineState* pipelineState =
Robert Phillipsd0fe8752019-01-31 14:13:59 -0500537 fGpu->resourceProvider().findOrCreateCompatiblePipelineState(fRenderTarget, fOrigin,
538 pipeline,
egdaniel9cb63402016-06-23 08:37:05 -0700539 primProc,
Greg Daniel9a51a862018-11-30 10:18:14 -0500540 primProcProxies,
egdaniel9cb63402016-06-23 08:37:05 -0700541 primitiveType,
Greg Daniel99b88e02018-10-03 15:31:20 -0400542 compatibleRenderPass);
egdaniel9cb63402016-06-23 08:37:05 -0700543 if (!pipelineState) {
544 return pipelineState;
545 }
546
Greg Daniel09eeefb2017-10-16 15:15:02 -0400547 fLastPipelineState = pipelineState;
Greg Daniel22bc8652017-03-22 15:45:43 -0400548
Brian Salomonf7232642018-09-19 08:58:08 -0400549 pipelineState->bindPipeline(fGpu, cbInfo.currentCmdBuf());
Brian Salomoncd7907b2018-08-30 08:36:18 -0400550
Robert Phillipsd0fe8752019-01-31 14:13:59 -0500551 pipelineState->setAndBindUniforms(fGpu, fRenderTarget, fOrigin,
552 primProc, pipeline, cbInfo.currentCmdBuf());
Brian Salomonf7232642018-09-19 08:58:08 -0400553
554 // Check whether we need to bind textures between each GrMesh. If not we can bind them all now.
555 bool setTextures = !(dynamicStateArrays && dynamicStateArrays->fPrimitiveProcessorTextures);
556 if (setTextures) {
Brian Salomonf7232642018-09-19 08:58:08 -0400557 pipelineState->setAndBindTextures(fGpu, primProc, pipeline, primProcProxies,
558 cbInfo.currentCmdBuf());
559 }
egdaniel9cb63402016-06-23 08:37:05 -0700560
Brian Salomond818ebf2018-07-02 14:08:49 +0000561 if (!pipeline.isScissorEnabled()) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400562 GrVkPipeline::SetDynamicScissorRectState(fGpu, cbInfo.currentCmdBuf(),
Robert Phillipsd0fe8752019-01-31 14:13:59 -0500563 fRenderTarget, fOrigin,
564 SkIRect::MakeWH(fRenderTarget->width(),
565 fRenderTarget->height()));
Brian Salomon49348902018-06-26 09:12:38 -0400566 } else if (!dynamicStateArrays || !dynamicStateArrays->fScissorRects) {
567 SkASSERT(fixedDynamicState);
Robert Phillipsd0fe8752019-01-31 14:13:59 -0500568 GrVkPipeline::SetDynamicScissorRectState(fGpu, cbInfo.currentCmdBuf(), fRenderTarget,
569 fOrigin,
Brian Salomon49348902018-06-26 09:12:38 -0400570 fixedDynamicState->fScissorRect);
Chris Dalton46983b72017-06-06 12:27:16 -0600571 }
Robert Phillipsd0fe8752019-01-31 14:13:59 -0500572 GrVkPipeline::SetDynamicViewportState(fGpu, cbInfo.currentCmdBuf(), fRenderTarget);
573 GrVkPipeline::SetDynamicBlendConstantState(fGpu, cbInfo.currentCmdBuf(),
Greg Daniel2c3398d2019-06-19 11:58:01 -0400574 pipeline.outputSwizzle(),
Chris Dalton46983b72017-06-06 12:27:16 -0600575 pipeline.getXferProcessor());
egdaniel9cb63402016-06-23 08:37:05 -0700576
577 return pipelineState;
578}
579
Greg Daniel2d41d0d2019-08-26 11:08:51 -0400580void GrVkOpsRenderPass::onDraw(const GrPrimitiveProcessor& primProc,
Brian Salomonff168d92018-06-23 15:17:27 -0400581 const GrPipeline& pipeline,
Brian Salomon49348902018-06-26 09:12:38 -0400582 const GrPipeline::FixedDynamicState* fixedDynamicState,
583 const GrPipeline::DynamicStateArrays* dynamicStateArrays,
Greg Daniel500d58b2017-08-24 15:59:33 -0400584 const GrMesh meshes[],
Greg Daniel500d58b2017-08-24 15:59:33 -0400585 int meshCount,
586 const SkRect& bounds) {
egdaniel9cb63402016-06-23 08:37:05 -0700587 if (!meshCount) {
588 return;
589 }
Greg Danielea022cd2018-03-16 11:10:03 -0400590
591 CommandBufferInfo& cbInfo = fCommandBufferInfos[fCurrentCmdInfo];
592
Brian Salomonf7232642018-09-19 08:58:08 -0400593 if (dynamicStateArrays && dynamicStateArrays->fPrimitiveProcessorTextures) {
594 for (int m = 0, i = 0; m < meshCount; ++m) {
595 for (int s = 0; s < primProc.numTextureSamplers(); ++s, ++i) {
596 auto texture = dynamicStateArrays->fPrimitiveProcessorTextures[i]->peekTexture();
Chris Dalton0d6f7752019-08-19 12:21:27 -0600597 this->appendSampledTexture(texture);
Brian Salomonf7232642018-09-19 08:58:08 -0400598 }
599 }
600 } else {
601 for (int i = 0; i < primProc.numTextureSamplers(); ++i) {
602 auto texture = fixedDynamicState->fPrimitiveProcessorTextures[i]->peekTexture();
Chris Dalton0d6f7752019-08-19 12:21:27 -0600603 this->appendSampledTexture(texture);
Brian Salomonf7232642018-09-19 08:58:08 -0400604 }
Brian Salomone782f842018-07-31 13:53:11 -0400605 }
bsalomonb58a2b42016-09-26 06:55:02 -0700606 GrFragmentProcessor::Iter iter(pipeline);
607 while (const GrFragmentProcessor* fp = iter.next()) {
Brian Salomone782f842018-07-31 13:53:11 -0400608 for (int i = 0; i < fp->numTextureSamplers(); ++i) {
609 const GrFragmentProcessor::TextureSampler& sampler = fp->textureSampler(i);
Chris Dalton0d6f7752019-08-19 12:21:27 -0600610 this->appendSampledTexture(sampler.peekTexture());
Brian Salomone782f842018-07-31 13:53:11 -0400611 }
egdaniel2f5792a2016-07-06 08:51:23 -0700612 }
Robert Phillipsbb581ce2017-05-29 15:05:15 -0400613 if (GrTexture* dstTexture = pipeline.peekDstTexture()) {
Chris Dalton0d6f7752019-08-19 12:21:27 -0600614 this->appendSampledTexture(dstTexture);
Brian Salomon18dfa982017-04-03 16:57:43 -0400615 }
egdaniel2f5792a2016-07-06 08:51:23 -0700616
Chris Daltonbca46e22017-05-15 11:03:26 -0600617 GrPrimitiveType primitiveType = meshes[0].primitiveType();
Brian Salomon49348902018-06-26 09:12:38 -0400618 GrVkPipelineState* pipelineState = this->prepareDrawState(primProc, pipeline, fixedDynamicState,
619 dynamicStateArrays, primitiveType);
egdaniel9cb63402016-06-23 08:37:05 -0700620 if (!pipelineState) {
621 return;
622 }
623
Brian Salomond818ebf2018-07-02 14:08:49 +0000624 bool dynamicScissor =
625 pipeline.isScissorEnabled() && dynamicStateArrays && dynamicStateArrays->fScissorRects;
Brian Salomonf7232642018-09-19 08:58:08 -0400626 bool dynamicTextures = dynamicStateArrays && dynamicStateArrays->fPrimitiveProcessorTextures;
Brian Salomon49348902018-06-26 09:12:38 -0400627
egdaniel9cb63402016-06-23 08:37:05 -0700628 for (int i = 0; i < meshCount; ++i) {
629 const GrMesh& mesh = meshes[i];
Chris Daltonbca46e22017-05-15 11:03:26 -0600630 if (mesh.primitiveType() != primitiveType) {
Chris Dalton6f241802017-05-08 13:58:38 -0400631 SkDEBUGCODE(pipelineState = nullptr);
Chris Daltonbca46e22017-05-15 11:03:26 -0600632 primitiveType = mesh.primitiveType();
Brian Salomon49348902018-06-26 09:12:38 -0400633 pipelineState = this->prepareDrawState(primProc, pipeline, fixedDynamicState,
634 dynamicStateArrays, primitiveType);
Chris Dalton6f241802017-05-08 13:58:38 -0400635 if (!pipelineState) {
636 return;
egdaniel9cb63402016-06-23 08:37:05 -0700637 }
Chris Dalton6f241802017-05-08 13:58:38 -0400638 }
egdaniel9cb63402016-06-23 08:37:05 -0700639
Brian Salomon49348902018-06-26 09:12:38 -0400640 if (dynamicScissor) {
641 GrVkPipeline::SetDynamicScissorRectState(fGpu, cbInfo.currentCmdBuf(), fRenderTarget,
Robert Phillipsd0fe8752019-01-31 14:13:59 -0500642 fOrigin,
Brian Salomon49348902018-06-26 09:12:38 -0400643 dynamicStateArrays->fScissorRects[i]);
Chris Dalton46983b72017-06-06 12:27:16 -0600644 }
Brian Salomonf7232642018-09-19 08:58:08 -0400645 if (dynamicTextures) {
646 GrTextureProxy* const* meshProxies = dynamicStateArrays->fPrimitiveProcessorTextures +
647 primProc.numTextureSamplers() * i;
648 pipelineState->setAndBindTextures(fGpu, primProc, pipeline, meshProxies,
649 cbInfo.currentCmdBuf());
650 }
Chris Daltonbca46e22017-05-15 11:03:26 -0600651 SkASSERT(pipelineState);
Brian Salomon802cb312018-06-08 18:05:20 -0400652 mesh.sendToGpu(this);
egdaniel9cb63402016-06-23 08:37:05 -0700653 }
654
Greg Daniel36a77ee2016-10-18 10:33:25 -0400655 cbInfo.fBounds.join(bounds);
Chris Dalton114a3c02017-05-26 15:17:19 -0600656 cbInfo.fIsEmpty = false;
egdaniel066df7c2016-06-08 14:02:27 -0700657}
658
Greg Daniel2d41d0d2019-08-26 11:08:51 -0400659void GrVkOpsRenderPass::appendSampledTexture(GrTexture* tex) {
Chris Dalton0d6f7752019-08-19 12:21:27 -0600660 SkASSERT(!tex->isProtected() || (fRenderTarget->isProtected() && fGpu->protectedContext()));
Robert Phillipse1efd382019-08-21 10:07:10 -0400661 GrVkTexture* vkTex = static_cast<GrVkTexture*>(tex);
662
663 fCommandBufferInfos[fCurrentCmdInfo].fSampledTextures.push_back(sk_ref_sp(vkTex));
Chris Dalton0d6f7752019-08-19 12:21:27 -0600664}
665
Greg Daniel2d41d0d2019-08-26 11:08:51 -0400666void GrVkOpsRenderPass::sendInstancedMeshToGpu(GrPrimitiveType,
667 const GrBuffer* vertexBuffer,
668 int vertexCount,
669 int baseVertex,
670 const GrBuffer* instanceBuffer,
671 int instanceCount,
672 int baseInstance) {
Chris Dalton114a3c02017-05-26 15:17:19 -0600673 CommandBufferInfo& cbInfo = fCommandBufferInfos[fCurrentCmdInfo];
Brian Salomondbf70722019-02-07 11:31:24 -0500674 SkASSERT(!vertexBuffer || !vertexBuffer->isCpuBuffer());
675 SkASSERT(!instanceBuffer || !instanceBuffer->isCpuBuffer());
676 auto gpuVertexBuffer = static_cast<const GrGpuBuffer*>(vertexBuffer);
677 auto gpuInstanceBuffer = static_cast<const GrGpuBuffer*>(instanceBuffer);
678 this->bindGeometry(nullptr, gpuVertexBuffer, gpuInstanceBuffer);
Chris Dalton1d616352017-05-31 12:51:23 -0600679 cbInfo.currentCmdBuf()->draw(fGpu, vertexCount, instanceCount, baseVertex, baseInstance);
Chris Dalton114a3c02017-05-26 15:17:19 -0600680 fGpu->stats()->incNumDraws();
681}
682
Greg Daniel2d41d0d2019-08-26 11:08:51 -0400683void GrVkOpsRenderPass::sendIndexedInstancedMeshToGpu(GrPrimitiveType,
684 const GrBuffer* indexBuffer,
685 int indexCount,
686 int baseIndex,
687 const GrBuffer* vertexBuffer,
688 int baseVertex,
689 const GrBuffer* instanceBuffer,
690 int instanceCount,
691 int baseInstance,
692 GrPrimitiveRestart restart) {
Brian Salomon802cb312018-06-08 18:05:20 -0400693 SkASSERT(restart == GrPrimitiveRestart::kNo);
Chris Dalton114a3c02017-05-26 15:17:19 -0600694 CommandBufferInfo& cbInfo = fCommandBufferInfos[fCurrentCmdInfo];
Brian Salomondbf70722019-02-07 11:31:24 -0500695 SkASSERT(!vertexBuffer || !vertexBuffer->isCpuBuffer());
696 SkASSERT(!instanceBuffer || !instanceBuffer->isCpuBuffer());
697 SkASSERT(!indexBuffer->isCpuBuffer());
698 auto gpuIndexxBuffer = static_cast<const GrGpuBuffer*>(indexBuffer);
699 auto gpuVertexBuffer = static_cast<const GrGpuBuffer*>(vertexBuffer);
700 auto gpuInstanceBuffer = static_cast<const GrGpuBuffer*>(instanceBuffer);
701 this->bindGeometry(gpuIndexxBuffer, gpuVertexBuffer, gpuInstanceBuffer);
Chris Dalton1d616352017-05-31 12:51:23 -0600702 cbInfo.currentCmdBuf()->drawIndexed(fGpu, indexCount, instanceCount,
703 baseIndex, baseVertex, baseInstance);
Chris Dalton114a3c02017-05-26 15:17:19 -0600704 fGpu->stats()->incNumDraws();
705}
Greg Daniel64cc9aa2018-10-19 13:54:56 -0400706
707////////////////////////////////////////////////////////////////////////////////
708
Greg Daniel2d41d0d2019-08-26 11:08:51 -0400709void GrVkOpsRenderPass::executeDrawable(std::unique_ptr<SkDrawable::GpuDrawHandler> drawable) {
Greg Daniel64cc9aa2018-10-19 13:54:56 -0400710 GrVkRenderTarget* target = static_cast<GrVkRenderTarget*>(fRenderTarget);
711
712 GrVkImage* targetImage = target->msaaImage() ? target->msaaImage() : target;
713
714 CommandBufferInfo& cbInfo = fCommandBufferInfos[fCurrentCmdInfo];
715 VkRect2D bounds;
716 bounds.offset = { 0, 0 };
717 bounds.extent = { 0, 0 };
718
719 GrVkDrawableInfo vkInfo;
720 vkInfo.fSecondaryCommandBuffer = cbInfo.currentCmdBuf()->vkCommandBuffer();
721 vkInfo.fCompatibleRenderPass = cbInfo.fRenderPass->vkRenderPass();
Greg Danielb353eeb2018-12-05 11:01:58 -0500722 SkAssertResult(cbInfo.fRenderPass->colorAttachmentIndex(&vkInfo.fColorAttachmentIndex));
Greg Daniel64cc9aa2018-10-19 13:54:56 -0400723 vkInfo.fFormat = targetImage->imageFormat();
724 vkInfo.fDrawBounds = &bounds;
Stan Ilievcb580602019-02-26 11:36:07 -0500725#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
726 vkInfo.fImage = targetImage->image();
727#else
728 vkInfo.fImage = VK_NULL_HANDLE;
729#endif //SK_BUILD_FOR_ANDROID_FRAMEWORK
Greg Daniel64cc9aa2018-10-19 13:54:56 -0400730
731 GrBackendDrawableInfo info(vkInfo);
732
Eric Karlc0b2ba22019-01-22 19:40:35 -0800733 // After we draw into the command buffer via the drawable, cached state we have may be invalid.
734 cbInfo.currentCmdBuf()->invalidateState();
Eric Karla8878a12019-02-07 18:17:43 -0800735 // Also assume that the drawable produced output.
736 cbInfo.fIsEmpty = false;
Eric Karlc0b2ba22019-01-22 19:40:35 -0800737
Greg Daniel64cc9aa2018-10-19 13:54:56 -0400738 drawable->draw(info);
739 fGpu->addDrawable(std::move(drawable));
740
741 if (bounds.extent.width == 0 || bounds.extent.height == 0) {
742 cbInfo.fBounds.join(target->getBoundsRect());
743 } else {
744 cbInfo.fBounds.join(SkRect::MakeXYWH(bounds.offset.x, bounds.offset.y,
745 bounds.extent.width, bounds.extent.height));
746 }
747}
748