blob: d6ca67e6ffcb8b70b47c379059217f7a4a2e14a2 [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.
389 SkASSERT(!cbInfo.fIsEmpty || clip.scissorEnabled());
egdaniel9cb63402016-06-23 08:37:05 -0700390
391 // We always do a sub rect clear with clearAttachments since we are inside a render pass
392 VkClearRect clearRect;
393 // Flip rect if necessary
csmartdalton29df7602016-08-31 11:55:52 -0700394 SkIRect vkRect;
Brian Salomond818ebf2018-07-02 14:08:49 +0000395 if (!clip.scissorEnabled()) {
Greg Daniela41a74a2018-10-09 12:59:23 +0000396 vkRect.setXYWH(0, 0, fRenderTarget->width(), fRenderTarget->height());
Robert Phillips4f101a72017-07-28 08:42:04 -0400397 } else if (kBottomLeft_GrSurfaceOrigin != fOrigin) {
csmartdalton29df7602016-08-31 11:55:52 -0700398 vkRect = clip.scissorRect();
399 } else {
400 const SkIRect& scissor = clip.scissorRect();
Greg Daniel65a09272016-10-12 09:47:22 -0400401 vkRect.setLTRB(scissor.fLeft, fRenderTarget->height() - scissor.fBottom,
402 scissor.fRight, fRenderTarget->height() - scissor.fTop);
egdaniel9cb63402016-06-23 08:37:05 -0700403 }
404 clearRect.rect.offset = { vkRect.fLeft, vkRect.fTop };
405 clearRect.rect.extent = { (uint32_t)vkRect.width(), (uint32_t)vkRect.height() };
406 clearRect.baseArrayLayer = 0;
407 clearRect.layerCount = 1;
408
409 uint32_t colorIndex;
Greg Daniel36a77ee2016-10-18 10:33:25 -0400410 SkAssertResult(cbInfo.fRenderPass->colorAttachmentIndex(&colorIndex));
egdaniel9cb63402016-06-23 08:37:05 -0700411
412 VkClearAttachment attachment;
413 attachment.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
414 attachment.colorAttachment = colorIndex;
415 attachment.clearValue.color = vkColor;
416
Greg Daniel22bc8652017-03-22 15:45:43 -0400417 cbInfo.currentCmdBuf()->clearAttachments(fGpu, 1, &attachment, 1, &clearRect);
Greg Daniel77b53f62016-10-18 11:48:51 -0400418 cbInfo.fIsEmpty = false;
Greg Daniel36a77ee2016-10-18 10:33:25 -0400419
420 // Update command buffer bounds
Brian Salomond818ebf2018-07-02 14:08:49 +0000421 if (!clip.scissorEnabled()) {
Greg Daniel36a77ee2016-10-18 10:33:25 -0400422 cbInfo.fBounds.join(fRenderTarget->getBoundsRect());
423 } else {
424 cbInfo.fBounds.join(SkRect::Make(clip.scissorRect()));
425 }
egdaniel9cb63402016-06-23 08:37:05 -0700426 return;
427}
428
Greg Daniel500d58b2017-08-24 15:59:33 -0400429////////////////////////////////////////////////////////////////////////////////
430
Greg Daniel2d41d0d2019-08-26 11:08:51 -0400431void GrVkOpsRenderPass::addAdditionalRenderPass() {
Robert Phillips19e51dc2017-08-09 09:30:51 -0400432 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(fRenderTarget);
433
Greg Daniel22bc8652017-03-22 15:45:43 -0400434 fCommandBufferInfos[fCurrentCmdInfo].currentCmdBuf()->end(fGpu);
Greg Daniel77b53f62016-10-18 11:48:51 -0400435
436 CommandBufferInfo& cbInfo = fCommandBufferInfos.push_back();
Greg Daniel22bc8652017-03-22 15:45:43 -0400437 fCurrentCmdInfo++;
Greg Daniel77b53f62016-10-18 11:48:51 -0400438
439 GrVkRenderPass::LoadStoreOps vkColorOps(VK_ATTACHMENT_LOAD_OP_LOAD,
440 VK_ATTACHMENT_STORE_OP_STORE);
441 GrVkRenderPass::LoadStoreOps vkStencilOps(VK_ATTACHMENT_LOAD_OP_LOAD,
442 VK_ATTACHMENT_STORE_OP_STORE);
443
444 const GrVkResourceProvider::CompatibleRPHandle& rpHandle =
Robert Phillips19e51dc2017-08-09 09:30:51 -0400445 vkRT->compatibleRenderPassHandle();
Greg Daniel77b53f62016-10-18 11:48:51 -0400446 if (rpHandle.isValid()) {
447 cbInfo.fRenderPass = fGpu->resourceProvider().findRenderPass(rpHandle,
448 vkColorOps,
449 vkStencilOps);
450 } else {
Robert Phillips19e51dc2017-08-09 09:30:51 -0400451 cbInfo.fRenderPass = fGpu->resourceProvider().findRenderPass(*vkRT,
Greg Daniel77b53f62016-10-18 11:48:51 -0400452 vkColorOps,
453 vkStencilOps);
454 }
Greg Daniela3c68df2018-03-16 13:46:53 -0400455 cbInfo.fLoadStoreState = LoadStoreState::kLoadAndStore;
Greg Daniel77b53f62016-10-18 11:48:51 -0400456
Greg Daniel228518f2019-08-07 16:55:17 -0400457 cbInfo.fCommandBuffer = fGpu->cmdPool()->findOrCreateSecondaryCommandBuffer(fGpu);
Greg Daniel77b53f62016-10-18 11:48:51 -0400458 // It shouldn't matter what we set the clear color to here since we will assume loading of the
459 // attachment.
460 memset(&cbInfo.fColorClearValue, 0, sizeof(VkClearValue));
461 cbInfo.fBounds.setEmpty();
Greg Daniel77b53f62016-10-18 11:48:51 -0400462
Robert Phillips19e51dc2017-08-09 09:30:51 -0400463 cbInfo.currentCmdBuf()->begin(fGpu, vkRT->framebuffer(), cbInfo.fRenderPass);
Greg Daniel77b53f62016-10-18 11:48:51 -0400464}
465
Greg Daniel2d41d0d2019-08-26 11:08:51 -0400466void GrVkOpsRenderPass::inlineUpload(GrOpFlushState* state,
Brian Salomon943ed792017-10-30 09:37:55 -0400467 GrDeferredTextureUploadFn& upload) {
Greg Daniel22bc8652017-03-22 15:45:43 -0400468 if (!fCommandBufferInfos[fCurrentCmdInfo].fIsEmpty) {
469 this->addAdditionalRenderPass();
Greg Daniel77b53f62016-10-18 11:48:51 -0400470 }
Brian Salomon24d377e2019-04-23 15:24:31 -0400471
Brian Salomon24d377e2019-04-23 15:24:31 -0400472 fPreCommandBufferTasks.emplace<InlineUpload>(state, upload);
473 ++fCommandBufferInfos[fCurrentCmdInfo].fNumPreCmds;
Greg Daniel77b53f62016-10-18 11:48:51 -0400474}
475
egdaniel9cb63402016-06-23 08:37:05 -0700476////////////////////////////////////////////////////////////////////////////////
477
Greg Daniel2d41d0d2019-08-26 11:08:51 -0400478void GrVkOpsRenderPass::bindGeometry(const GrGpuBuffer* indexBuffer,
Brian Salomondbf70722019-02-07 11:31:24 -0500479 const GrGpuBuffer* vertexBuffer,
480 const GrGpuBuffer* instanceBuffer) {
Chris Daltonff926502017-05-03 14:36:54 -0400481 GrVkSecondaryCommandBuffer* currCmdBuf = fCommandBufferInfos[fCurrentCmdInfo].currentCmdBuf();
egdaniel9cb63402016-06-23 08:37:05 -0700482 // There is no need to put any memory barriers to make sure host writes have finished here.
483 // When a command buffer is submitted to a queue, there is an implicit memory barrier that
484 // occurs for all host writes. Additionally, BufferMemoryBarriers are not allowed inside of
485 // an active RenderPass.
egdaniel9cb63402016-06-23 08:37:05 -0700486
Chris Dalton1d616352017-05-31 12:51:23 -0600487 // Here our vertex and instance inputs need to match the same 0-based bindings they were
488 // assigned in GrVkPipeline. That is, vertex first (if any) followed by instance.
489 uint32_t binding = 0;
490
Brian Salomon802cb312018-06-08 18:05:20 -0400491 if (vertexBuffer) {
Chris Dalton1d616352017-05-31 12:51:23 -0600492 SkASSERT(vertexBuffer);
Chris Dalton1d616352017-05-31 12:51:23 -0600493 SkASSERT(!vertexBuffer->isMapped());
494
495 currCmdBuf->bindInputBuffer(fGpu, binding++,
496 static_cast<const GrVkVertexBuffer*>(vertexBuffer));
497 }
498
Brian Salomon802cb312018-06-08 18:05:20 -0400499 if (instanceBuffer) {
Chris Dalton1d616352017-05-31 12:51:23 -0600500 SkASSERT(instanceBuffer);
Chris Dalton1d616352017-05-31 12:51:23 -0600501 SkASSERT(!instanceBuffer->isMapped());
502
503 currCmdBuf->bindInputBuffer(fGpu, binding++,
504 static_cast<const GrVkVertexBuffer*>(instanceBuffer));
505 }
Chris Daltonff926502017-05-03 14:36:54 -0400506 if (indexBuffer) {
507 SkASSERT(indexBuffer);
508 SkASSERT(!indexBuffer->isMapped());
egdaniel9cb63402016-06-23 08:37:05 -0700509
Chris Daltonff926502017-05-03 14:36:54 -0400510 currCmdBuf->bindIndexBuffer(fGpu, static_cast<const GrVkIndexBuffer*>(indexBuffer));
egdaniel9cb63402016-06-23 08:37:05 -0700511 }
512}
513
Greg Daniel2d41d0d2019-08-26 11:08:51 -0400514GrVkPipelineState* GrVkOpsRenderPass::prepareDrawState(
Brian Salomon49348902018-06-26 09:12:38 -0400515 const GrPrimitiveProcessor& primProc,
516 const GrPipeline& pipeline,
517 const GrPipeline::FixedDynamicState* fixedDynamicState,
518 const GrPipeline::DynamicStateArrays* dynamicStateArrays,
519 GrPrimitiveType primitiveType) {
Greg Daniel22bc8652017-03-22 15:45:43 -0400520 CommandBufferInfo& cbInfo = fCommandBufferInfos[fCurrentCmdInfo];
521 SkASSERT(cbInfo.fRenderPass);
Greg Daniel36a77ee2016-10-18 10:33:25 -0400522
Greg Daniel99b88e02018-10-03 15:31:20 -0400523 VkRenderPass compatibleRenderPass = cbInfo.fRenderPass->vkRenderPass();
524
Greg Daniel9a51a862018-11-30 10:18:14 -0500525 const GrTextureProxy* const* primProcProxies = nullptr;
526 if (dynamicStateArrays && dynamicStateArrays->fPrimitiveProcessorTextures) {
527 primProcProxies = dynamicStateArrays->fPrimitiveProcessorTextures;
528 } else if (fixedDynamicState) {
529 primProcProxies = fixedDynamicState->fPrimitiveProcessorTextures;
530 }
531
532 SkASSERT(SkToBool(primProcProxies) == SkToBool(primProc.numTextureSamplers()));
533
Greg Daniel09eeefb2017-10-16 15:15:02 -0400534 GrVkPipelineState* pipelineState =
Robert Phillipsd0fe8752019-01-31 14:13:59 -0500535 fGpu->resourceProvider().findOrCreateCompatiblePipelineState(fRenderTarget, fOrigin,
536 pipeline,
egdaniel9cb63402016-06-23 08:37:05 -0700537 primProc,
Greg Daniel9a51a862018-11-30 10:18:14 -0500538 primProcProxies,
egdaniel9cb63402016-06-23 08:37:05 -0700539 primitiveType,
Greg Daniel99b88e02018-10-03 15:31:20 -0400540 compatibleRenderPass);
egdaniel9cb63402016-06-23 08:37:05 -0700541 if (!pipelineState) {
542 return pipelineState;
543 }
544
Greg Daniel09eeefb2017-10-16 15:15:02 -0400545 fLastPipelineState = pipelineState;
Greg Daniel22bc8652017-03-22 15:45:43 -0400546
Brian Salomonf7232642018-09-19 08:58:08 -0400547 pipelineState->bindPipeline(fGpu, cbInfo.currentCmdBuf());
Brian Salomoncd7907b2018-08-30 08:36:18 -0400548
Robert Phillipsd0fe8752019-01-31 14:13:59 -0500549 pipelineState->setAndBindUniforms(fGpu, fRenderTarget, fOrigin,
550 primProc, pipeline, cbInfo.currentCmdBuf());
Brian Salomonf7232642018-09-19 08:58:08 -0400551
552 // Check whether we need to bind textures between each GrMesh. If not we can bind them all now.
553 bool setTextures = !(dynamicStateArrays && dynamicStateArrays->fPrimitiveProcessorTextures);
554 if (setTextures) {
Brian Salomonf7232642018-09-19 08:58:08 -0400555 pipelineState->setAndBindTextures(fGpu, primProc, pipeline, primProcProxies,
556 cbInfo.currentCmdBuf());
557 }
egdaniel9cb63402016-06-23 08:37:05 -0700558
Brian Salomond818ebf2018-07-02 14:08:49 +0000559 if (!pipeline.isScissorEnabled()) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400560 GrVkPipeline::SetDynamicScissorRectState(fGpu, cbInfo.currentCmdBuf(),
Robert Phillipsd0fe8752019-01-31 14:13:59 -0500561 fRenderTarget, fOrigin,
562 SkIRect::MakeWH(fRenderTarget->width(),
563 fRenderTarget->height()));
Brian Salomon49348902018-06-26 09:12:38 -0400564 } else if (!dynamicStateArrays || !dynamicStateArrays->fScissorRects) {
565 SkASSERT(fixedDynamicState);
Robert Phillipsd0fe8752019-01-31 14:13:59 -0500566 GrVkPipeline::SetDynamicScissorRectState(fGpu, cbInfo.currentCmdBuf(), fRenderTarget,
567 fOrigin,
Brian Salomon49348902018-06-26 09:12:38 -0400568 fixedDynamicState->fScissorRect);
Chris Dalton46983b72017-06-06 12:27:16 -0600569 }
Robert Phillipsd0fe8752019-01-31 14:13:59 -0500570 GrVkPipeline::SetDynamicViewportState(fGpu, cbInfo.currentCmdBuf(), fRenderTarget);
571 GrVkPipeline::SetDynamicBlendConstantState(fGpu, cbInfo.currentCmdBuf(),
Greg Daniel2c3398d2019-06-19 11:58:01 -0400572 pipeline.outputSwizzle(),
Chris Dalton46983b72017-06-06 12:27:16 -0600573 pipeline.getXferProcessor());
egdaniel9cb63402016-06-23 08:37:05 -0700574
575 return pipelineState;
576}
577
Greg Daniel2d41d0d2019-08-26 11:08:51 -0400578void GrVkOpsRenderPass::onDraw(const GrPrimitiveProcessor& primProc,
Brian Salomonff168d92018-06-23 15:17:27 -0400579 const GrPipeline& pipeline,
Brian Salomon49348902018-06-26 09:12:38 -0400580 const GrPipeline::FixedDynamicState* fixedDynamicState,
581 const GrPipeline::DynamicStateArrays* dynamicStateArrays,
Greg Daniel500d58b2017-08-24 15:59:33 -0400582 const GrMesh meshes[],
Greg Daniel500d58b2017-08-24 15:59:33 -0400583 int meshCount,
584 const SkRect& bounds) {
egdaniel9cb63402016-06-23 08:37:05 -0700585 if (!meshCount) {
586 return;
587 }
Greg Danielea022cd2018-03-16 11:10:03 -0400588
589 CommandBufferInfo& cbInfo = fCommandBufferInfos[fCurrentCmdInfo];
590
Brian Salomonf7232642018-09-19 08:58:08 -0400591 if (dynamicStateArrays && dynamicStateArrays->fPrimitiveProcessorTextures) {
592 for (int m = 0, i = 0; m < meshCount; ++m) {
593 for (int s = 0; s < primProc.numTextureSamplers(); ++s, ++i) {
594 auto texture = dynamicStateArrays->fPrimitiveProcessorTextures[i]->peekTexture();
Chris Dalton0d6f7752019-08-19 12:21:27 -0600595 this->appendSampledTexture(texture);
Brian Salomonf7232642018-09-19 08:58:08 -0400596 }
597 }
598 } else {
599 for (int i = 0; i < primProc.numTextureSamplers(); ++i) {
600 auto texture = fixedDynamicState->fPrimitiveProcessorTextures[i]->peekTexture();
Chris Dalton0d6f7752019-08-19 12:21:27 -0600601 this->appendSampledTexture(texture);
Brian Salomonf7232642018-09-19 08:58:08 -0400602 }
Brian Salomone782f842018-07-31 13:53:11 -0400603 }
bsalomonb58a2b42016-09-26 06:55:02 -0700604 GrFragmentProcessor::Iter iter(pipeline);
605 while (const GrFragmentProcessor* fp = iter.next()) {
Brian Salomone782f842018-07-31 13:53:11 -0400606 for (int i = 0; i < fp->numTextureSamplers(); ++i) {
607 const GrFragmentProcessor::TextureSampler& sampler = fp->textureSampler(i);
Chris Dalton0d6f7752019-08-19 12:21:27 -0600608 this->appendSampledTexture(sampler.peekTexture());
Brian Salomone782f842018-07-31 13:53:11 -0400609 }
egdaniel2f5792a2016-07-06 08:51:23 -0700610 }
Robert Phillipsbb581ce2017-05-29 15:05:15 -0400611 if (GrTexture* dstTexture = pipeline.peekDstTexture()) {
Chris Dalton0d6f7752019-08-19 12:21:27 -0600612 this->appendSampledTexture(dstTexture);
Brian Salomon18dfa982017-04-03 16:57:43 -0400613 }
egdaniel2f5792a2016-07-06 08:51:23 -0700614
Chris Daltonbca46e22017-05-15 11:03:26 -0600615 GrPrimitiveType primitiveType = meshes[0].primitiveType();
Brian Salomon49348902018-06-26 09:12:38 -0400616 GrVkPipelineState* pipelineState = this->prepareDrawState(primProc, pipeline, fixedDynamicState,
617 dynamicStateArrays, primitiveType);
egdaniel9cb63402016-06-23 08:37:05 -0700618 if (!pipelineState) {
619 return;
620 }
621
Brian Salomond818ebf2018-07-02 14:08:49 +0000622 bool dynamicScissor =
623 pipeline.isScissorEnabled() && dynamicStateArrays && dynamicStateArrays->fScissorRects;
Brian Salomonf7232642018-09-19 08:58:08 -0400624 bool dynamicTextures = dynamicStateArrays && dynamicStateArrays->fPrimitiveProcessorTextures;
Brian Salomon49348902018-06-26 09:12:38 -0400625
egdaniel9cb63402016-06-23 08:37:05 -0700626 for (int i = 0; i < meshCount; ++i) {
627 const GrMesh& mesh = meshes[i];
Chris Daltonbca46e22017-05-15 11:03:26 -0600628 if (mesh.primitiveType() != primitiveType) {
Chris Dalton6f241802017-05-08 13:58:38 -0400629 SkDEBUGCODE(pipelineState = nullptr);
Chris Daltonbca46e22017-05-15 11:03:26 -0600630 primitiveType = mesh.primitiveType();
Brian Salomon49348902018-06-26 09:12:38 -0400631 pipelineState = this->prepareDrawState(primProc, pipeline, fixedDynamicState,
632 dynamicStateArrays, primitiveType);
Chris Dalton6f241802017-05-08 13:58:38 -0400633 if (!pipelineState) {
634 return;
egdaniel9cb63402016-06-23 08:37:05 -0700635 }
Chris Dalton6f241802017-05-08 13:58:38 -0400636 }
egdaniel9cb63402016-06-23 08:37:05 -0700637
Brian Salomon49348902018-06-26 09:12:38 -0400638 if (dynamicScissor) {
639 GrVkPipeline::SetDynamicScissorRectState(fGpu, cbInfo.currentCmdBuf(), fRenderTarget,
Robert Phillipsd0fe8752019-01-31 14:13:59 -0500640 fOrigin,
Brian Salomon49348902018-06-26 09:12:38 -0400641 dynamicStateArrays->fScissorRects[i]);
Chris Dalton46983b72017-06-06 12:27:16 -0600642 }
Brian Salomonf7232642018-09-19 08:58:08 -0400643 if (dynamicTextures) {
644 GrTextureProxy* const* meshProxies = dynamicStateArrays->fPrimitiveProcessorTextures +
645 primProc.numTextureSamplers() * i;
646 pipelineState->setAndBindTextures(fGpu, primProc, pipeline, meshProxies,
647 cbInfo.currentCmdBuf());
648 }
Chris Daltonbca46e22017-05-15 11:03:26 -0600649 SkASSERT(pipelineState);
Brian Salomon802cb312018-06-08 18:05:20 -0400650 mesh.sendToGpu(this);
egdaniel9cb63402016-06-23 08:37:05 -0700651 }
652
Greg Daniel36a77ee2016-10-18 10:33:25 -0400653 cbInfo.fBounds.join(bounds);
Chris Dalton114a3c02017-05-26 15:17:19 -0600654 cbInfo.fIsEmpty = false;
egdaniel066df7c2016-06-08 14:02:27 -0700655}
656
Greg Daniel2d41d0d2019-08-26 11:08:51 -0400657void GrVkOpsRenderPass::appendSampledTexture(GrTexture* tex) {
Chris Dalton0d6f7752019-08-19 12:21:27 -0600658 SkASSERT(!tex->isProtected() || (fRenderTarget->isProtected() && fGpu->protectedContext()));
Robert Phillipse1efd382019-08-21 10:07:10 -0400659 GrVkTexture* vkTex = static_cast<GrVkTexture*>(tex);
660
661 fCommandBufferInfos[fCurrentCmdInfo].fSampledTextures.push_back(sk_ref_sp(vkTex));
Chris Dalton0d6f7752019-08-19 12:21:27 -0600662}
663
Greg Daniel2d41d0d2019-08-26 11:08:51 -0400664void GrVkOpsRenderPass::sendInstancedMeshToGpu(GrPrimitiveType,
665 const GrBuffer* vertexBuffer,
666 int vertexCount,
667 int baseVertex,
668 const GrBuffer* instanceBuffer,
669 int instanceCount,
670 int baseInstance) {
Chris Dalton114a3c02017-05-26 15:17:19 -0600671 CommandBufferInfo& cbInfo = fCommandBufferInfos[fCurrentCmdInfo];
Brian Salomondbf70722019-02-07 11:31:24 -0500672 SkASSERT(!vertexBuffer || !vertexBuffer->isCpuBuffer());
673 SkASSERT(!instanceBuffer || !instanceBuffer->isCpuBuffer());
674 auto gpuVertexBuffer = static_cast<const GrGpuBuffer*>(vertexBuffer);
675 auto gpuInstanceBuffer = static_cast<const GrGpuBuffer*>(instanceBuffer);
676 this->bindGeometry(nullptr, gpuVertexBuffer, gpuInstanceBuffer);
Chris Dalton1d616352017-05-31 12:51:23 -0600677 cbInfo.currentCmdBuf()->draw(fGpu, vertexCount, instanceCount, baseVertex, baseInstance);
Chris Dalton114a3c02017-05-26 15:17:19 -0600678 fGpu->stats()->incNumDraws();
679}
680
Greg Daniel2d41d0d2019-08-26 11:08:51 -0400681void GrVkOpsRenderPass::sendIndexedInstancedMeshToGpu(GrPrimitiveType,
682 const GrBuffer* indexBuffer,
683 int indexCount,
684 int baseIndex,
685 const GrBuffer* vertexBuffer,
686 int baseVertex,
687 const GrBuffer* instanceBuffer,
688 int instanceCount,
689 int baseInstance,
690 GrPrimitiveRestart restart) {
Brian Salomon802cb312018-06-08 18:05:20 -0400691 SkASSERT(restart == GrPrimitiveRestart::kNo);
Chris Dalton114a3c02017-05-26 15:17:19 -0600692 CommandBufferInfo& cbInfo = fCommandBufferInfos[fCurrentCmdInfo];
Brian Salomondbf70722019-02-07 11:31:24 -0500693 SkASSERT(!vertexBuffer || !vertexBuffer->isCpuBuffer());
694 SkASSERT(!instanceBuffer || !instanceBuffer->isCpuBuffer());
695 SkASSERT(!indexBuffer->isCpuBuffer());
696 auto gpuIndexxBuffer = static_cast<const GrGpuBuffer*>(indexBuffer);
697 auto gpuVertexBuffer = static_cast<const GrGpuBuffer*>(vertexBuffer);
698 auto gpuInstanceBuffer = static_cast<const GrGpuBuffer*>(instanceBuffer);
699 this->bindGeometry(gpuIndexxBuffer, gpuVertexBuffer, gpuInstanceBuffer);
Chris Dalton1d616352017-05-31 12:51:23 -0600700 cbInfo.currentCmdBuf()->drawIndexed(fGpu, indexCount, instanceCount,
701 baseIndex, baseVertex, baseInstance);
Chris Dalton114a3c02017-05-26 15:17:19 -0600702 fGpu->stats()->incNumDraws();
703}
Greg Daniel64cc9aa2018-10-19 13:54:56 -0400704
705////////////////////////////////////////////////////////////////////////////////
706
Greg Daniel2d41d0d2019-08-26 11:08:51 -0400707void GrVkOpsRenderPass::executeDrawable(std::unique_ptr<SkDrawable::GpuDrawHandler> drawable) {
Greg Daniel64cc9aa2018-10-19 13:54:56 -0400708 GrVkRenderTarget* target = static_cast<GrVkRenderTarget*>(fRenderTarget);
709
710 GrVkImage* targetImage = target->msaaImage() ? target->msaaImage() : target;
711
712 CommandBufferInfo& cbInfo = fCommandBufferInfos[fCurrentCmdInfo];
713 VkRect2D bounds;
714 bounds.offset = { 0, 0 };
715 bounds.extent = { 0, 0 };
716
717 GrVkDrawableInfo vkInfo;
718 vkInfo.fSecondaryCommandBuffer = cbInfo.currentCmdBuf()->vkCommandBuffer();
719 vkInfo.fCompatibleRenderPass = cbInfo.fRenderPass->vkRenderPass();
Greg Danielb353eeb2018-12-05 11:01:58 -0500720 SkAssertResult(cbInfo.fRenderPass->colorAttachmentIndex(&vkInfo.fColorAttachmentIndex));
Greg Daniel64cc9aa2018-10-19 13:54:56 -0400721 vkInfo.fFormat = targetImage->imageFormat();
722 vkInfo.fDrawBounds = &bounds;
Stan Ilievcb580602019-02-26 11:36:07 -0500723#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
724 vkInfo.fImage = targetImage->image();
725#else
726 vkInfo.fImage = VK_NULL_HANDLE;
727#endif //SK_BUILD_FOR_ANDROID_FRAMEWORK
Greg Daniel64cc9aa2018-10-19 13:54:56 -0400728
729 GrBackendDrawableInfo info(vkInfo);
730
Eric Karlc0b2ba22019-01-22 19:40:35 -0800731 // After we draw into the command buffer via the drawable, cached state we have may be invalid.
732 cbInfo.currentCmdBuf()->invalidateState();
Eric Karla8878a12019-02-07 18:17:43 -0800733 // Also assume that the drawable produced output.
734 cbInfo.fIsEmpty = false;
Eric Karlc0b2ba22019-01-22 19:40:35 -0800735
Greg Daniel64cc9aa2018-10-19 13:54:56 -0400736 drawable->draw(info);
737 fGpu->addDrawable(std::move(drawable));
738
739 if (bounds.extent.width == 0 || bounds.extent.height == 0) {
740 cbInfo.fBounds.join(target->getBoundsRect());
741 } else {
742 cbInfo.fBounds.join(SkRect::MakeXYWH(bounds.offset.x, bounds.offset.y,
743 bounds.extent.width, bounds.extent.height));
744 }
745}
746