blob: b280820e194eaa24daec1b5171ff481b59851597 [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
Brian Salomon5d8f1cc2019-04-24 09:03:53 -040047} // anonymous namespace
48
49/////////////////////////////////////////////////////////////////////////////
50
Greg Daniel500d58b2017-08-24 15:59:33 -040051void GrVkGpuTextureCommandBuffer::insertEventMarker(const char* msg) {
52 // TODO: does Vulkan have a correlate?
53}
54
55void GrVkGpuTextureCommandBuffer::submit() {
Greg Daniel46cfbc62019-06-07 11:43:30 -040056 GrVkPrimaryCommandBufferTask::Args taskArgs{fGpu, fTexture};
Brian Salomon5d8f1cc2019-04-24 09:03:53 -040057 for (auto& task : fTasks) {
58 task.execute(taskArgs);
Greg Daniel500d58b2017-08-24 15:59:33 -040059 }
60}
61
Greg Daniel500d58b2017-08-24 15:59:33 -040062////////////////////////////////////////////////////////////////////////////////
63
Robert Phillips6b47c7d2017-08-29 07:24:09 -040064void get_vk_load_store_ops(GrLoadOp loadOpIn, GrStoreOp storeOpIn,
egdaniel066df7c2016-06-08 14:02:27 -070065 VkAttachmentLoadOp* loadOp, VkAttachmentStoreOp* storeOp) {
Robert Phillips95214472017-08-08 18:00:03 -040066 switch (loadOpIn) {
Robert Phillips6b47c7d2017-08-29 07:24:09 -040067 case GrLoadOp::kLoad:
egdaniel066df7c2016-06-08 14:02:27 -070068 *loadOp = VK_ATTACHMENT_LOAD_OP_LOAD;
egdaniel066df7c2016-06-08 14:02:27 -070069 break;
Robert Phillips6b47c7d2017-08-29 07:24:09 -040070 case GrLoadOp::kClear:
egdaniel9cb63402016-06-23 08:37:05 -070071 *loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
72 break;
Robert Phillips6b47c7d2017-08-29 07:24:09 -040073 case GrLoadOp::kDiscard:
egdaniel9cb63402016-06-23 08:37:05 -070074 *loadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
75 break;
76 default:
77 SK_ABORT("Invalid LoadOp");
egdaniel066df7c2016-06-08 14:02:27 -070078 *loadOp = VK_ATTACHMENT_LOAD_OP_LOAD;
egdaniel9cb63402016-06-23 08:37:05 -070079 }
80
Robert Phillips95214472017-08-08 18:00:03 -040081 switch (storeOpIn) {
Robert Phillips6b47c7d2017-08-29 07:24:09 -040082 case GrStoreOp::kStore:
egdaniel066df7c2016-06-08 14:02:27 -070083 *storeOp = VK_ATTACHMENT_STORE_OP_STORE;
84 break;
Robert Phillips6b47c7d2017-08-29 07:24:09 -040085 case GrStoreOp::kDiscard:
egdaniel066df7c2016-06-08 14:02:27 -070086 *storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
87 break;
brianosman0bbc3712016-06-14 04:53:09 -070088 default:
egdaniel9cb63402016-06-23 08:37:05 -070089 SK_ABORT("Invalid StoreOp");
brianosman0bbc3712016-06-14 04:53:09 -070090 *storeOp = VK_ATTACHMENT_STORE_OP_STORE;
egdaniel066df7c2016-06-08 14:02:27 -070091 }
92}
93
Brian Salomon24d377e2019-04-23 15:24:31 -040094GrVkGpuRTCommandBuffer::GrVkGpuRTCommandBuffer(GrVkGpu* gpu) : fGpu(gpu) {}
Brian Salomonc293a292016-11-30 13:38:32 -050095
Greg Daniel500d58b2017-08-24 15:59:33 -040096void GrVkGpuRTCommandBuffer::init() {
Brian Salomonc293a292016-11-30 13:38:32 -050097 GrVkRenderPass::LoadStoreOps vkColorOps(fVkColorLoadOp, fVkColorStoreOp);
98 GrVkRenderPass::LoadStoreOps vkStencilOps(fVkStencilLoadOp, fVkStencilStoreOp);
egdaniel9cb63402016-06-23 08:37:05 -070099
Greg Daniel36a77ee2016-10-18 10:33:25 -0400100 CommandBufferInfo& cbInfo = fCommandBufferInfos.push_back();
Brian Salomonc293a292016-11-30 13:38:32 -0500101 SkASSERT(fCommandBufferInfos.count() == 1);
Greg Daniel22bc8652017-03-22 15:45:43 -0400102 fCurrentCmdInfo = 0;
Greg Daniel36a77ee2016-10-18 10:33:25 -0400103
Robert Phillips19e51dc2017-08-09 09:30:51 -0400104 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(fRenderTarget);
105 const GrVkResourceProvider::CompatibleRPHandle& rpHandle = vkRT->compatibleRenderPassHandle();
egdaniel066df7c2016-06-08 14:02:27 -0700106 if (rpHandle.isValid()) {
Greg Daniel36a77ee2016-10-18 10:33:25 -0400107 cbInfo.fRenderPass = fGpu->resourceProvider().findRenderPass(rpHandle,
108 vkColorOps,
109 vkStencilOps);
egdaniel066df7c2016-06-08 14:02:27 -0700110 } else {
Robert Phillips19e51dc2017-08-09 09:30:51 -0400111 cbInfo.fRenderPass = fGpu->resourceProvider().findRenderPass(*vkRT,
Greg Daniel36a77ee2016-10-18 10:33:25 -0400112 vkColorOps,
113 vkStencilOps);
egdaniel066df7c2016-06-08 14:02:27 -0700114 }
115
Brian Osmancb3d0872018-10-16 15:19:28 -0400116 cbInfo.fColorClearValue.color.float32[0] = fClearColor[0];
117 cbInfo.fColorClearValue.color.float32[1] = fClearColor[1];
118 cbInfo.fColorClearValue.color.float32[2] = fClearColor[2];
119 cbInfo.fColorClearValue.color.float32[3] = fClearColor[3];
egdaniel9cb63402016-06-23 08:37:05 -0700120
Robert Phillips380b90c2017-08-30 07:41:07 -0400121 if (VK_ATTACHMENT_LOAD_OP_CLEAR == fVkColorLoadOp) {
Greg Daniela41a74a2018-10-09 12:59:23 +0000122 cbInfo.fBounds = SkRect::MakeWH(vkRT->width(), vkRT->height());
Robert Phillips380b90c2017-08-30 07:41:07 -0400123 } else {
124 cbInfo.fBounds.setEmpty();
125 }
Greg Daniela3c68df2018-03-16 13:46:53 -0400126
127 if (VK_ATTACHMENT_LOAD_OP_CLEAR == fVkColorLoadOp) {
128 cbInfo.fLoadStoreState = LoadStoreState::kStartsWithClear;
129 } else if (VK_ATTACHMENT_LOAD_OP_LOAD == fVkColorLoadOp &&
130 VK_ATTACHMENT_STORE_OP_STORE == fVkColorStoreOp) {
131 cbInfo.fLoadStoreState = LoadStoreState::kLoadAndStore;
132 } else if (VK_ATTACHMENT_LOAD_OP_DONT_CARE == fVkColorLoadOp) {
133 cbInfo.fLoadStoreState = LoadStoreState::kStartsWithDiscard;
134 }
Greg Daniel36a77ee2016-10-18 10:33:25 -0400135
Greg Daniel228518f2019-08-07 16:55:17 -0400136 cbInfo.fCommandBuffer = fGpu->cmdPool()->findOrCreateSecondaryCommandBuffer(fGpu);
Robert Phillips19e51dc2017-08-09 09:30:51 -0400137 cbInfo.currentCmdBuf()->begin(fGpu, vkRT->framebuffer(), cbInfo.fRenderPass);
egdaniel066df7c2016-06-08 14:02:27 -0700138}
139
Greg Daniel070cbaf2019-01-03 17:35:54 -0500140void GrVkGpuRTCommandBuffer::initWrapped() {
141 CommandBufferInfo& cbInfo = fCommandBufferInfos.push_back();
142 SkASSERT(fCommandBufferInfos.count() == 1);
143 fCurrentCmdInfo = 0;
144
145 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(fRenderTarget);
146 SkASSERT(vkRT->wrapsSecondaryCommandBuffer());
147 cbInfo.fRenderPass = vkRT->externalRenderPass();
148 cbInfo.fRenderPass->ref();
149
150 cbInfo.fBounds.setEmpty();
Greg Daniel228518f2019-08-07 16:55:17 -0400151 cbInfo.fCommandBuffer.reset(
Greg Daniel8daf3b72019-07-30 09:57:26 -0400152 GrVkSecondaryCommandBuffer::Create(vkRT->getExternalSecondaryCommandBuffer()));
Greg Daniel070cbaf2019-01-03 17:35:54 -0500153 cbInfo.currentCmdBuf()->begin(fGpu, nullptr, cbInfo.fRenderPass);
154}
Brian Salomonc293a292016-11-30 13:38:32 -0500155
Greg Daniel500d58b2017-08-24 15:59:33 -0400156GrVkGpuRTCommandBuffer::~GrVkGpuRTCommandBuffer() {
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400157 this->reset();
egdaniel066df7c2016-06-08 14:02:27 -0700158}
159
Greg Daniel500d58b2017-08-24 15:59:33 -0400160GrGpu* GrVkGpuRTCommandBuffer::gpu() { return fGpu; }
egdaniel9cb63402016-06-23 08:37:05 -0700161
Greg Daniel500d58b2017-08-24 15:59:33 -0400162void GrVkGpuRTCommandBuffer::end() {
Greg Daniel22bc8652017-03-22 15:45:43 -0400163 if (fCurrentCmdInfo >= 0) {
164 fCommandBufferInfos[fCurrentCmdInfo].currentCmdBuf()->end(fGpu);
Brian Salomonc293a292016-11-30 13:38:32 -0500165 }
egdaniel066df7c2016-06-08 14:02:27 -0700166}
167
Greg Daniel500d58b2017-08-24 15:59:33 -0400168void GrVkGpuRTCommandBuffer::submit() {
Brian Salomonc293a292016-11-30 13:38:32 -0500169 if (!fRenderTarget) {
170 return;
171 }
Robert Phillips19e51dc2017-08-09 09:30:51 -0400172
173 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(fRenderTarget);
Robert Phillips19e51dc2017-08-09 09:30:51 -0400174 GrVkImage* targetImage = vkRT->msaaImage() ? vkRT->msaaImage() : vkRT;
Greg Daniel45a44de2018-02-27 10:07:29 -0500175 GrStencilAttachment* stencil = fRenderTarget->renderTargetPriv().getStencilAttachment();
Brian Salomon24d377e2019-04-23 15:24:31 -0400176 auto currPreCmd = fPreCommandBufferTasks.begin();
egdaniel9cb63402016-06-23 08:37:05 -0700177
Greg Daniel46cfbc62019-06-07 11:43:30 -0400178 GrVkPrimaryCommandBufferTask::Args taskArgs{fGpu, fRenderTarget};
Greg Daniel36a77ee2016-10-18 10:33:25 -0400179 for (int i = 0; i < fCommandBufferInfos.count(); ++i) {
180 CommandBufferInfo& cbInfo = fCommandBufferInfos[i];
181
Brian Salomon24d377e2019-04-23 15:24:31 -0400182 for (int c = 0; c < cbInfo.fNumPreCmds; ++c, ++currPreCmd) {
Brian Salomon5d8f1cc2019-04-24 09:03:53 -0400183 currPreCmd->execute(taskArgs);
Greg Daniel77b53f62016-10-18 11:48:51 -0400184 }
185
Greg Daniel38c3d932018-03-16 14:22:30 -0400186 // TODO: Many things create a scratch texture which adds the discard immediately, but then
187 // don't draw to it right away. This causes the discard to be ignored and we get yelled at
188 // for loading uninitialized data. However, once MDB lands with reordering, the discard will
189 // get reordered with the rest of the draw commands and we can remove the discard check.
190 if (cbInfo.fIsEmpty &&
191 cbInfo.fLoadStoreState != LoadStoreState::kStartsWithClear &&
192 cbInfo.fLoadStoreState != LoadStoreState::kStartsWithDiscard) {
Greg Daniel77b53f62016-10-18 11:48:51 -0400193 // We have sumbitted no actual draw commands to the command buffer and we are not using
194 // the render pass to do a clear so there is no need to submit anything.
195 continue;
196 }
Greg Daniel38c3d932018-03-16 14:22:30 -0400197
Greg Daniel070cbaf2019-01-03 17:35:54 -0500198 // We don't want to actually submit the secondary command buffer if it is wrapped.
199 if (this->wrapsSecondaryCommandBuffer()) {
200 // If we have any sampled images set their layout now.
Chris Dalton298238a2019-02-21 16:28:44 -0500201 for (int j = 0; j < cbInfo.fSampledTextures.count(); ++j) {
202 cbInfo.fSampledTextures[j]->setImageLayout(
203 fGpu, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, VK_ACCESS_SHADER_READ_BIT,
204 VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT, false);
Greg Daniel070cbaf2019-01-03 17:35:54 -0500205 }
206
207 // There should have only been one secondary command buffer in the wrapped case so it is
208 // safe to just return here.
209 SkASSERT(fCommandBufferInfos.count() == 1);
210 return;
211 }
212
Greg Danieldbdba602018-04-20 11:52:43 -0400213 // Make sure if we only have a discard load that we execute the discard on the whole image.
214 // TODO: Once we improve our tracking of discards so that we never end up flushing a discard
215 // call with no actually ops, remove this.
216 if (cbInfo.fIsEmpty && cbInfo.fLoadStoreState == LoadStoreState::kStartsWithDiscard) {
Greg Daniela41a74a2018-10-09 12:59:23 +0000217 cbInfo.fBounds = SkRect::MakeWH(vkRT->width(), vkRT->height());
Greg Danieldbdba602018-04-20 11:52:43 -0400218 }
219
Mike Reed9ea63152019-08-22 16:19:50 -0400220 if (cbInfo.fBounds.intersect(SkRect::MakeIWH(fRenderTarget->width(),
221 fRenderTarget->height()))) {
Greg Daniel38c3d932018-03-16 14:22:30 -0400222 // Make sure we do the following layout changes after all copies, uploads, or any other
223 // pre-work is done since we may change the layouts in the pre-work. Also since the
224 // draws will be submitted in different render passes, we need to guard againts write
225 // and write issues.
226
227 // Change layout of our render target so it can be used as the color attachment.
Greg Danielf7828d02018-10-09 12:01:32 -0400228 // TODO: If we know that we will never be blending or loading the attachment we could
229 // drop the VK_ACCESS_COLOR_ATTACHMENT_READ_BIT.
Greg Daniel38c3d932018-03-16 14:22:30 -0400230 targetImage->setImageLayout(fGpu,
231 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Greg Danielf7828d02018-10-09 12:01:32 -0400232 VK_ACCESS_COLOR_ATTACHMENT_READ_BIT |
Greg Daniel38c3d932018-03-16 14:22:30 -0400233 VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
Greg Danielf7828d02018-10-09 12:01:32 -0400234 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
Greg Daniel38c3d932018-03-16 14:22:30 -0400235 false);
236
237 // If we are using a stencil attachment we also need to update its layout
238 if (stencil) {
239 GrVkStencilAttachment* vkStencil = (GrVkStencilAttachment*)stencil;
Greg Danielf7828d02018-10-09 12:01:32 -0400240 // We need the write and read access bits since we may load and store the stencil.
241 // The initial load happens in the VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT so we
242 // wait there.
Greg Daniel38c3d932018-03-16 14:22:30 -0400243 vkStencil->setImageLayout(fGpu,
244 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
245 VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT |
246 VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT,
Greg Danielf7828d02018-10-09 12:01:32 -0400247 VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT,
Greg Daniel38c3d932018-03-16 14:22:30 -0400248 false);
249 }
250
251 // If we have any sampled images set their layout now.
Chris Dalton298238a2019-02-21 16:28:44 -0500252 for (int j = 0; j < cbInfo.fSampledTextures.count(); ++j) {
253 cbInfo.fSampledTextures[j]->setImageLayout(
254 fGpu, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, VK_ACCESS_SHADER_READ_BIT,
255 VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT, false);
Greg Daniel38c3d932018-03-16 14:22:30 -0400256 }
257
Greg Daniel36a77ee2016-10-18 10:33:25 -0400258 SkIRect iBounds;
259 cbInfo.fBounds.roundOut(&iBounds);
260
Greg Daniel228518f2019-08-07 16:55:17 -0400261 fGpu->submitSecondaryCommandBuffer(std::move(cbInfo.fCommandBuffer), cbInfo.fRenderPass,
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400262 &cbInfo.fColorClearValue, vkRT, fOrigin, iBounds);
Greg Daniel36a77ee2016-10-18 10:33:25 -0400263 }
264 }
Brian Salomon24d377e2019-04-23 15:24:31 -0400265 SkASSERT(currPreCmd == fPreCommandBufferTasks.end());
egdaniel9cb63402016-06-23 08:37:05 -0700266}
267
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400268void GrVkGpuRTCommandBuffer::set(GrRenderTarget* rt, GrSurfaceOrigin origin,
269 const GrGpuRTCommandBuffer::LoadAndStoreInfo& colorInfo,
270 const GrGpuRTCommandBuffer::StencilLoadAndStoreInfo& stencilInfo) {
271 SkASSERT(!fRenderTarget);
272 SkASSERT(fCommandBufferInfos.empty());
273 SkASSERT(-1 == fCurrentCmdInfo);
Robert Phillips9da87e02019-02-04 13:26:26 -0500274 SkASSERT(fGpu == rt->getContext()->priv().getGpu());
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400275 SkASSERT(!fLastPipelineState);
276
Greg Danielb0c7ad12019-06-06 17:23:35 +0000277#ifdef SK_DEBUG
278 fIsActive = true;
279#endif
280
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400281 this->INHERITED::set(rt, origin);
282
Greg Daniel070cbaf2019-01-03 17:35:54 -0500283 if (this->wrapsSecondaryCommandBuffer()) {
284 this->initWrapped();
285 return;
286 }
287
Brian Osman9a9baae2018-11-05 15:06:26 -0500288 fClearColor = colorInfo.fClearColor;
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400289
290 get_vk_load_store_ops(colorInfo.fLoadOp, colorInfo.fStoreOp,
291 &fVkColorLoadOp, &fVkColorStoreOp);
292
293 get_vk_load_store_ops(stencilInfo.fLoadOp, stencilInfo.fStoreOp,
294 &fVkStencilLoadOp, &fVkStencilStoreOp);
295
296 this->init();
297}
298
299void GrVkGpuRTCommandBuffer::reset() {
300 for (int i = 0; i < fCommandBufferInfos.count(); ++i) {
301 CommandBufferInfo& cbInfo = fCommandBufferInfos[i];
Greg Daniel228518f2019-08-07 16:55:17 -0400302 if (cbInfo.fCommandBuffer) {
303 cbInfo.fCommandBuffer.release()->recycle(fGpu);
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400304 }
305 cbInfo.fRenderPass->unref(fGpu);
306 }
307 fCommandBufferInfos.reset();
Brian Salomon24d377e2019-04-23 15:24:31 -0400308 fPreCommandBufferTasks.reset();
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400309
310 fCurrentCmdInfo = -1;
311
312 fLastPipelineState = nullptr;
313 fRenderTarget = nullptr;
Greg Danielb0c7ad12019-06-06 17:23:35 +0000314
315#ifdef SK_DEBUG
316 fIsActive = false;
317#endif
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400318}
319
Greg Daniel070cbaf2019-01-03 17:35:54 -0500320bool GrVkGpuRTCommandBuffer::wrapsSecondaryCommandBuffer() const {
321 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(fRenderTarget);
322 return vkRT->wrapsSecondaryCommandBuffer();
323}
324
Greg Daniel64cc9aa2018-10-19 13:54:56 -0400325////////////////////////////////////////////////////////////////////////////////
326
Greg Daniel500d58b2017-08-24 15:59:33 -0400327void GrVkGpuRTCommandBuffer::insertEventMarker(const char* msg) {
Robert Phillips65a88fa2017-08-08 08:36:22 -0400328 // TODO: does Vulkan have a correlate?
329}
330
Greg Daniel500d58b2017-08-24 15:59:33 -0400331void GrVkGpuRTCommandBuffer::onClearStencilClip(const GrFixedClip& clip, bool insideStencilMask) {
Chris Dalton94c04682017-11-01 17:15:06 -0600332 SkASSERT(!clip.hasWindowRectangles());
333
Jim Van Verth6a40abc2017-11-02 16:56:09 +0000334 CommandBufferInfo& cbInfo = fCommandBufferInfos[fCurrentCmdInfo];
335
Greg Daniel65a09272016-10-12 09:47:22 -0400336 GrStencilAttachment* sb = fRenderTarget->renderTargetPriv().getStencilAttachment();
egdaniel9cb63402016-06-23 08:37:05 -0700337 // this should only be called internally when we know we have a
338 // stencil buffer.
339 SkASSERT(sb);
340 int stencilBitCount = sb->bits();
341
342 // The contract with the callers does not guarantee that we preserve all bits in the stencil
343 // during this clear. Thus we will clear the entire stencil to the desired value.
344
345 VkClearDepthStencilValue vkStencilColor;
346 memset(&vkStencilColor, 0, sizeof(VkClearDepthStencilValue));
csmartdalton29df7602016-08-31 11:55:52 -0700347 if (insideStencilMask) {
egdaniel9cb63402016-06-23 08:37:05 -0700348 vkStencilColor.stencil = (1 << (stencilBitCount - 1));
349 } else {
350 vkStencilColor.stencil = 0;
351 }
352
353 VkClearRect clearRect;
354 // Flip rect if necessary
csmartdalton29df7602016-08-31 11:55:52 -0700355 SkIRect vkRect;
Brian Salomond818ebf2018-07-02 14:08:49 +0000356 if (!clip.scissorEnabled()) {
Greg Daniela41a74a2018-10-09 12:59:23 +0000357 vkRect.setXYWH(0, 0, fRenderTarget->width(), fRenderTarget->height());
Robert Phillips4f101a72017-07-28 08:42:04 -0400358 } else if (kBottomLeft_GrSurfaceOrigin != fOrigin) {
csmartdalton29df7602016-08-31 11:55:52 -0700359 vkRect = clip.scissorRect();
360 } else {
361 const SkIRect& scissor = clip.scissorRect();
Greg Daniel65a09272016-10-12 09:47:22 -0400362 vkRect.setLTRB(scissor.fLeft, fRenderTarget->height() - scissor.fBottom,
363 scissor.fRight, fRenderTarget->height() - scissor.fTop);
egdaniel9cb63402016-06-23 08:37:05 -0700364 }
365
366 clearRect.rect.offset = { vkRect.fLeft, vkRect.fTop };
367 clearRect.rect.extent = { (uint32_t)vkRect.width(), (uint32_t)vkRect.height() };
368
369 clearRect.baseArrayLayer = 0;
370 clearRect.layerCount = 1;
371
372 uint32_t stencilIndex;
Greg Daniel36a77ee2016-10-18 10:33:25 -0400373 SkAssertResult(cbInfo.fRenderPass->stencilAttachmentIndex(&stencilIndex));
egdaniel9cb63402016-06-23 08:37:05 -0700374
375 VkClearAttachment attachment;
376 attachment.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
377 attachment.colorAttachment = 0; // this value shouldn't matter
378 attachment.clearValue.depthStencil = vkStencilColor;
379
Greg Daniel22bc8652017-03-22 15:45:43 -0400380 cbInfo.currentCmdBuf()->clearAttachments(fGpu, 1, &attachment, 1, &clearRect);
Greg Daniel77b53f62016-10-18 11:48:51 -0400381 cbInfo.fIsEmpty = false;
Greg Daniel36a77ee2016-10-18 10:33:25 -0400382
383 // Update command buffer bounds
Brian Salomond818ebf2018-07-02 14:08:49 +0000384 if (!clip.scissorEnabled()) {
Greg Daniel36a77ee2016-10-18 10:33:25 -0400385 cbInfo.fBounds.join(fRenderTarget->getBoundsRect());
386 } else {
387 cbInfo.fBounds.join(SkRect::Make(clip.scissorRect()));
388 }
egdaniel9cb63402016-06-23 08:37:05 -0700389}
390
Brian Osman9a9baae2018-11-05 15:06:26 -0500391void GrVkGpuRTCommandBuffer::onClear(const GrFixedClip& clip, const SkPMColor4f& color) {
Robert Phillips19e51dc2017-08-09 09:30:51 -0400392 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(fRenderTarget);
393
Jim Van Verth6a40abc2017-11-02 16:56:09 +0000394 // parent class should never let us get here with no RT
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700395 SkASSERT(!clip.hasWindowRectangles());
egdaniel9cb63402016-06-23 08:37:05 -0700396
Greg Daniel22bc8652017-03-22 15:45:43 -0400397 CommandBufferInfo& cbInfo = fCommandBufferInfos[fCurrentCmdInfo];
Greg Daniel36a77ee2016-10-18 10:33:25 -0400398
Brian Osman9a9baae2018-11-05 15:06:26 -0500399 VkClearColorValue vkColor = {{color.fR, color.fG, color.fB, color.fA}};
egdaniel9cb63402016-06-23 08:37:05 -0700400
Brian Salomond818ebf2018-07-02 14:08:49 +0000401 if (cbInfo.fIsEmpty && !clip.scissorEnabled()) {
Robert Phillips74c627f2017-08-09 10:28:00 -0400402 // Change the render pass to do a clear load
egdaniel9cb63402016-06-23 08:37:05 -0700403 GrVkRenderPass::LoadStoreOps vkColorOps(VK_ATTACHMENT_LOAD_OP_CLEAR,
404 VK_ATTACHMENT_STORE_OP_STORE);
Robert Phillips74c627f2017-08-09 10:28:00 -0400405 // Preserve the stencil buffer's load & store settings
406 GrVkRenderPass::LoadStoreOps vkStencilOps(fVkStencilLoadOp, fVkStencilStoreOp);
egdaniel9cb63402016-06-23 08:37:05 -0700407
Greg Daniel36a77ee2016-10-18 10:33:25 -0400408 const GrVkRenderPass* oldRP = cbInfo.fRenderPass;
egdaniel9cb63402016-06-23 08:37:05 -0700409
410 const GrVkResourceProvider::CompatibleRPHandle& rpHandle =
Robert Phillips19e51dc2017-08-09 09:30:51 -0400411 vkRT->compatibleRenderPassHandle();
egdaniel9cb63402016-06-23 08:37:05 -0700412 if (rpHandle.isValid()) {
Greg Daniel36a77ee2016-10-18 10:33:25 -0400413 cbInfo.fRenderPass = fGpu->resourceProvider().findRenderPass(rpHandle,
414 vkColorOps,
415 vkStencilOps);
egdaniel9cb63402016-06-23 08:37:05 -0700416 } else {
Robert Phillips19e51dc2017-08-09 09:30:51 -0400417 cbInfo.fRenderPass = fGpu->resourceProvider().findRenderPass(*vkRT,
Greg Daniel36a77ee2016-10-18 10:33:25 -0400418 vkColorOps,
419 vkStencilOps);
egdaniel9cb63402016-06-23 08:37:05 -0700420 }
421
Greg Daniel36a77ee2016-10-18 10:33:25 -0400422 SkASSERT(cbInfo.fRenderPass->isCompatible(*oldRP));
egdaniel9cb63402016-06-23 08:37:05 -0700423 oldRP->unref(fGpu);
424
Brian Osman9a9baae2018-11-05 15:06:26 -0500425 cbInfo.fColorClearValue.color = {{color.fR, color.fG, color.fB, color.fA}};
Greg Daniela3c68df2018-03-16 13:46:53 -0400426 cbInfo.fLoadStoreState = LoadStoreState::kStartsWithClear;
Greg Daniel36a77ee2016-10-18 10:33:25 -0400427 // Update command buffer bounds
428 cbInfo.fBounds.join(fRenderTarget->getBoundsRect());
egdaniel9cb63402016-06-23 08:37:05 -0700429 return;
430 }
431
432 // We always do a sub rect clear with clearAttachments since we are inside a render pass
433 VkClearRect clearRect;
434 // Flip rect if necessary
csmartdalton29df7602016-08-31 11:55:52 -0700435 SkIRect vkRect;
Brian Salomond818ebf2018-07-02 14:08:49 +0000436 if (!clip.scissorEnabled()) {
Greg Daniela41a74a2018-10-09 12:59:23 +0000437 vkRect.setXYWH(0, 0, fRenderTarget->width(), fRenderTarget->height());
Robert Phillips4f101a72017-07-28 08:42:04 -0400438 } else if (kBottomLeft_GrSurfaceOrigin != fOrigin) {
csmartdalton29df7602016-08-31 11:55:52 -0700439 vkRect = clip.scissorRect();
440 } else {
441 const SkIRect& scissor = clip.scissorRect();
Greg Daniel65a09272016-10-12 09:47:22 -0400442 vkRect.setLTRB(scissor.fLeft, fRenderTarget->height() - scissor.fBottom,
443 scissor.fRight, fRenderTarget->height() - scissor.fTop);
egdaniel9cb63402016-06-23 08:37:05 -0700444 }
445 clearRect.rect.offset = { vkRect.fLeft, vkRect.fTop };
446 clearRect.rect.extent = { (uint32_t)vkRect.width(), (uint32_t)vkRect.height() };
447 clearRect.baseArrayLayer = 0;
448 clearRect.layerCount = 1;
449
450 uint32_t colorIndex;
Greg Daniel36a77ee2016-10-18 10:33:25 -0400451 SkAssertResult(cbInfo.fRenderPass->colorAttachmentIndex(&colorIndex));
egdaniel9cb63402016-06-23 08:37:05 -0700452
453 VkClearAttachment attachment;
454 attachment.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
455 attachment.colorAttachment = colorIndex;
456 attachment.clearValue.color = vkColor;
457
Greg Daniel22bc8652017-03-22 15:45:43 -0400458 cbInfo.currentCmdBuf()->clearAttachments(fGpu, 1, &attachment, 1, &clearRect);
Greg Daniel77b53f62016-10-18 11:48:51 -0400459 cbInfo.fIsEmpty = false;
Greg Daniel36a77ee2016-10-18 10:33:25 -0400460
461 // Update command buffer bounds
Brian Salomond818ebf2018-07-02 14:08:49 +0000462 if (!clip.scissorEnabled()) {
Greg Daniel36a77ee2016-10-18 10:33:25 -0400463 cbInfo.fBounds.join(fRenderTarget->getBoundsRect());
464 } else {
465 cbInfo.fBounds.join(SkRect::Make(clip.scissorRect()));
466 }
egdaniel9cb63402016-06-23 08:37:05 -0700467 return;
468}
469
Greg Daniel500d58b2017-08-24 15:59:33 -0400470////////////////////////////////////////////////////////////////////////////////
471
Greg Daniel500d58b2017-08-24 15:59:33 -0400472void GrVkGpuRTCommandBuffer::addAdditionalRenderPass() {
Robert Phillips19e51dc2017-08-09 09:30:51 -0400473 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(fRenderTarget);
474
Greg Daniel22bc8652017-03-22 15:45:43 -0400475 fCommandBufferInfos[fCurrentCmdInfo].currentCmdBuf()->end(fGpu);
Greg Daniel77b53f62016-10-18 11:48:51 -0400476
477 CommandBufferInfo& cbInfo = fCommandBufferInfos.push_back();
Greg Daniel22bc8652017-03-22 15:45:43 -0400478 fCurrentCmdInfo++;
Greg Daniel77b53f62016-10-18 11:48:51 -0400479
480 GrVkRenderPass::LoadStoreOps vkColorOps(VK_ATTACHMENT_LOAD_OP_LOAD,
481 VK_ATTACHMENT_STORE_OP_STORE);
482 GrVkRenderPass::LoadStoreOps vkStencilOps(VK_ATTACHMENT_LOAD_OP_LOAD,
483 VK_ATTACHMENT_STORE_OP_STORE);
484
485 const GrVkResourceProvider::CompatibleRPHandle& rpHandle =
Robert Phillips19e51dc2017-08-09 09:30:51 -0400486 vkRT->compatibleRenderPassHandle();
Greg Daniel77b53f62016-10-18 11:48:51 -0400487 if (rpHandle.isValid()) {
488 cbInfo.fRenderPass = fGpu->resourceProvider().findRenderPass(rpHandle,
489 vkColorOps,
490 vkStencilOps);
491 } else {
Robert Phillips19e51dc2017-08-09 09:30:51 -0400492 cbInfo.fRenderPass = fGpu->resourceProvider().findRenderPass(*vkRT,
Greg Daniel77b53f62016-10-18 11:48:51 -0400493 vkColorOps,
494 vkStencilOps);
495 }
Greg Daniela3c68df2018-03-16 13:46:53 -0400496 cbInfo.fLoadStoreState = LoadStoreState::kLoadAndStore;
Greg Daniel77b53f62016-10-18 11:48:51 -0400497
Greg Daniel228518f2019-08-07 16:55:17 -0400498 cbInfo.fCommandBuffer = fGpu->cmdPool()->findOrCreateSecondaryCommandBuffer(fGpu);
Greg Daniel77b53f62016-10-18 11:48:51 -0400499 // It shouldn't matter what we set the clear color to here since we will assume loading of the
500 // attachment.
501 memset(&cbInfo.fColorClearValue, 0, sizeof(VkClearValue));
502 cbInfo.fBounds.setEmpty();
Greg Daniel77b53f62016-10-18 11:48:51 -0400503
Robert Phillips19e51dc2017-08-09 09:30:51 -0400504 cbInfo.currentCmdBuf()->begin(fGpu, vkRT->framebuffer(), cbInfo.fRenderPass);
Greg Daniel77b53f62016-10-18 11:48:51 -0400505}
506
Brian Salomon943ed792017-10-30 09:37:55 -0400507void GrVkGpuRTCommandBuffer::inlineUpload(GrOpFlushState* state,
508 GrDeferredTextureUploadFn& upload) {
Greg Daniel22bc8652017-03-22 15:45:43 -0400509 if (!fCommandBufferInfos[fCurrentCmdInfo].fIsEmpty) {
510 this->addAdditionalRenderPass();
Greg Daniel77b53f62016-10-18 11:48:51 -0400511 }
Brian Salomon24d377e2019-04-23 15:24:31 -0400512
Brian Salomon24d377e2019-04-23 15:24:31 -0400513 fPreCommandBufferTasks.emplace<InlineUpload>(state, upload);
514 ++fCommandBufferInfos[fCurrentCmdInfo].fNumPreCmds;
Greg Daniel77b53f62016-10-18 11:48:51 -0400515}
516
egdaniel9cb63402016-06-23 08:37:05 -0700517////////////////////////////////////////////////////////////////////////////////
518
Brian Salomondbf70722019-02-07 11:31:24 -0500519void GrVkGpuRTCommandBuffer::bindGeometry(const GrGpuBuffer* indexBuffer,
520 const GrGpuBuffer* vertexBuffer,
521 const GrGpuBuffer* instanceBuffer) {
Chris Daltonff926502017-05-03 14:36:54 -0400522 GrVkSecondaryCommandBuffer* currCmdBuf = fCommandBufferInfos[fCurrentCmdInfo].currentCmdBuf();
egdaniel9cb63402016-06-23 08:37:05 -0700523 // There is no need to put any memory barriers to make sure host writes have finished here.
524 // When a command buffer is submitted to a queue, there is an implicit memory barrier that
525 // occurs for all host writes. Additionally, BufferMemoryBarriers are not allowed inside of
526 // an active RenderPass.
egdaniel9cb63402016-06-23 08:37:05 -0700527
Chris Dalton1d616352017-05-31 12:51:23 -0600528 // Here our vertex and instance inputs need to match the same 0-based bindings they were
529 // assigned in GrVkPipeline. That is, vertex first (if any) followed by instance.
530 uint32_t binding = 0;
531
Brian Salomon802cb312018-06-08 18:05:20 -0400532 if (vertexBuffer) {
Chris Dalton1d616352017-05-31 12:51:23 -0600533 SkASSERT(vertexBuffer);
Chris Dalton1d616352017-05-31 12:51:23 -0600534 SkASSERT(!vertexBuffer->isMapped());
535
536 currCmdBuf->bindInputBuffer(fGpu, binding++,
537 static_cast<const GrVkVertexBuffer*>(vertexBuffer));
538 }
539
Brian Salomon802cb312018-06-08 18:05:20 -0400540 if (instanceBuffer) {
Chris Dalton1d616352017-05-31 12:51:23 -0600541 SkASSERT(instanceBuffer);
Chris Dalton1d616352017-05-31 12:51:23 -0600542 SkASSERT(!instanceBuffer->isMapped());
543
544 currCmdBuf->bindInputBuffer(fGpu, binding++,
545 static_cast<const GrVkVertexBuffer*>(instanceBuffer));
546 }
Chris Daltonff926502017-05-03 14:36:54 -0400547 if (indexBuffer) {
548 SkASSERT(indexBuffer);
549 SkASSERT(!indexBuffer->isMapped());
egdaniel9cb63402016-06-23 08:37:05 -0700550
Chris Daltonff926502017-05-03 14:36:54 -0400551 currCmdBuf->bindIndexBuffer(fGpu, static_cast<const GrVkIndexBuffer*>(indexBuffer));
egdaniel9cb63402016-06-23 08:37:05 -0700552 }
553}
554
Brian Salomon49348902018-06-26 09:12:38 -0400555GrVkPipelineState* GrVkGpuRTCommandBuffer::prepareDrawState(
556 const GrPrimitiveProcessor& primProc,
557 const GrPipeline& pipeline,
558 const GrPipeline::FixedDynamicState* fixedDynamicState,
559 const GrPipeline::DynamicStateArrays* dynamicStateArrays,
560 GrPrimitiveType primitiveType) {
Greg Daniel22bc8652017-03-22 15:45:43 -0400561 CommandBufferInfo& cbInfo = fCommandBufferInfos[fCurrentCmdInfo];
562 SkASSERT(cbInfo.fRenderPass);
Greg Daniel36a77ee2016-10-18 10:33:25 -0400563
Greg Daniel99b88e02018-10-03 15:31:20 -0400564 VkRenderPass compatibleRenderPass = cbInfo.fRenderPass->vkRenderPass();
565
Greg Daniel9a51a862018-11-30 10:18:14 -0500566 const GrTextureProxy* const* primProcProxies = nullptr;
567 if (dynamicStateArrays && dynamicStateArrays->fPrimitiveProcessorTextures) {
568 primProcProxies = dynamicStateArrays->fPrimitiveProcessorTextures;
569 } else if (fixedDynamicState) {
570 primProcProxies = fixedDynamicState->fPrimitiveProcessorTextures;
571 }
572
573 SkASSERT(SkToBool(primProcProxies) == SkToBool(primProc.numTextureSamplers()));
574
Greg Daniel09eeefb2017-10-16 15:15:02 -0400575 GrVkPipelineState* pipelineState =
Robert Phillipsd0fe8752019-01-31 14:13:59 -0500576 fGpu->resourceProvider().findOrCreateCompatiblePipelineState(fRenderTarget, fOrigin,
577 pipeline,
egdaniel9cb63402016-06-23 08:37:05 -0700578 primProc,
Greg Daniel9a51a862018-11-30 10:18:14 -0500579 primProcProxies,
egdaniel9cb63402016-06-23 08:37:05 -0700580 primitiveType,
Greg Daniel99b88e02018-10-03 15:31:20 -0400581 compatibleRenderPass);
egdaniel9cb63402016-06-23 08:37:05 -0700582 if (!pipelineState) {
583 return pipelineState;
584 }
585
Greg Daniel09eeefb2017-10-16 15:15:02 -0400586 fLastPipelineState = pipelineState;
Greg Daniel22bc8652017-03-22 15:45:43 -0400587
Brian Salomonf7232642018-09-19 08:58:08 -0400588 pipelineState->bindPipeline(fGpu, cbInfo.currentCmdBuf());
Brian Salomoncd7907b2018-08-30 08:36:18 -0400589
Robert Phillipsd0fe8752019-01-31 14:13:59 -0500590 pipelineState->setAndBindUniforms(fGpu, fRenderTarget, fOrigin,
591 primProc, pipeline, cbInfo.currentCmdBuf());
Brian Salomonf7232642018-09-19 08:58:08 -0400592
593 // Check whether we need to bind textures between each GrMesh. If not we can bind them all now.
594 bool setTextures = !(dynamicStateArrays && dynamicStateArrays->fPrimitiveProcessorTextures);
595 if (setTextures) {
Brian Salomonf7232642018-09-19 08:58:08 -0400596 pipelineState->setAndBindTextures(fGpu, primProc, pipeline, primProcProxies,
597 cbInfo.currentCmdBuf());
598 }
egdaniel9cb63402016-06-23 08:37:05 -0700599
Brian Salomond818ebf2018-07-02 14:08:49 +0000600 if (!pipeline.isScissorEnabled()) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400601 GrVkPipeline::SetDynamicScissorRectState(fGpu, cbInfo.currentCmdBuf(),
Robert Phillipsd0fe8752019-01-31 14:13:59 -0500602 fRenderTarget, fOrigin,
603 SkIRect::MakeWH(fRenderTarget->width(),
604 fRenderTarget->height()));
Brian Salomon49348902018-06-26 09:12:38 -0400605 } else if (!dynamicStateArrays || !dynamicStateArrays->fScissorRects) {
606 SkASSERT(fixedDynamicState);
Robert Phillipsd0fe8752019-01-31 14:13:59 -0500607 GrVkPipeline::SetDynamicScissorRectState(fGpu, cbInfo.currentCmdBuf(), fRenderTarget,
608 fOrigin,
Brian Salomon49348902018-06-26 09:12:38 -0400609 fixedDynamicState->fScissorRect);
Chris Dalton46983b72017-06-06 12:27:16 -0600610 }
Robert Phillipsd0fe8752019-01-31 14:13:59 -0500611 GrVkPipeline::SetDynamicViewportState(fGpu, cbInfo.currentCmdBuf(), fRenderTarget);
612 GrVkPipeline::SetDynamicBlendConstantState(fGpu, cbInfo.currentCmdBuf(),
Greg Daniel2c3398d2019-06-19 11:58:01 -0400613 pipeline.outputSwizzle(),
Chris Dalton46983b72017-06-06 12:27:16 -0600614 pipeline.getXferProcessor());
egdaniel9cb63402016-06-23 08:37:05 -0700615
616 return pipelineState;
617}
618
Brian Salomonff168d92018-06-23 15:17:27 -0400619void GrVkGpuRTCommandBuffer::onDraw(const GrPrimitiveProcessor& primProc,
620 const GrPipeline& pipeline,
Brian Salomon49348902018-06-26 09:12:38 -0400621 const GrPipeline::FixedDynamicState* fixedDynamicState,
622 const GrPipeline::DynamicStateArrays* dynamicStateArrays,
Greg Daniel500d58b2017-08-24 15:59:33 -0400623 const GrMesh meshes[],
Greg Daniel500d58b2017-08-24 15:59:33 -0400624 int meshCount,
625 const SkRect& bounds) {
egdaniel9cb63402016-06-23 08:37:05 -0700626 if (!meshCount) {
627 return;
628 }
Greg Danielea022cd2018-03-16 11:10:03 -0400629
630 CommandBufferInfo& cbInfo = fCommandBufferInfos[fCurrentCmdInfo];
631
Brian Salomone782f842018-07-31 13:53:11 -0400632 auto prepareSampledImage = [&](GrTexture* texture, GrSamplerState::Filter filter) {
633 GrVkTexture* vkTexture = static_cast<GrVkTexture*>(texture);
634 // We may need to resolve the texture first if it is also a render target
635 GrVkRenderTarget* texRT = static_cast<GrVkRenderTarget*>(vkTexture->asRenderTarget());
Chris Dalton3d770272019-08-14 09:24:37 -0600636 if (texRT && texRT->needsResolve()) {
Greg Daniel0a77f432018-12-06 11:23:32 -0500637 fGpu->resolveRenderTargetNoFlush(texRT);
Chris Dalton3d770272019-08-14 09:24:37 -0600638 // TEMPORARY: MSAA resolve will have dirtied mipmaps. This goes away once we switch
Greg Danielf41b2bd2019-08-22 16:19:24 -0400639 // to resolving MSAA from the opsTask as well.
Chris Dalton3d770272019-08-14 09:24:37 -0600640 if (GrSamplerState::Filter::kMipMap == filter &&
641 (vkTexture->width() != 1 || vkTexture->height() != 1)) {
642 SkASSERT(vkTexture->texturePriv().mipMapped() == GrMipMapped::kYes);
643 SkASSERT(vkTexture->texturePriv().mipMapsAreDirty());
644 fGpu->regenerateMipMapLevels(vkTexture);
645 }
Brian Salomone782f842018-07-31 13:53:11 -0400646 }
647
Greg Danielf41b2bd2019-08-22 16:19:24 -0400648 // Ensure mip maps were all resolved ahead of time by the opsTask.
Brian Salomone782f842018-07-31 13:53:11 -0400649 if (GrSamplerState::Filter::kMipMap == filter &&
650 (vkTexture->width() != 1 || vkTexture->height() != 1)) {
651 SkASSERT(vkTexture->texturePriv().mipMapped() == GrMipMapped::kYes);
Chris Dalton3d770272019-08-14 09:24:37 -0600652 SkASSERT(!vkTexture->texturePriv().mipMapsAreDirty());
Brian Salomone782f842018-07-31 13:53:11 -0400653 }
Brian Salomone782f842018-07-31 13:53:11 -0400654 };
655
Brian Salomonf7232642018-09-19 08:58:08 -0400656 if (dynamicStateArrays && dynamicStateArrays->fPrimitiveProcessorTextures) {
657 for (int m = 0, i = 0; m < meshCount; ++m) {
658 for (int s = 0; s < primProc.numTextureSamplers(); ++s, ++i) {
659 auto texture = dynamicStateArrays->fPrimitiveProcessorTextures[i]->peekTexture();
660 prepareSampledImage(texture, primProc.textureSampler(s).samplerState().filter());
Chris Dalton0d6f7752019-08-19 12:21:27 -0600661 this->appendSampledTexture(texture);
Brian Salomonf7232642018-09-19 08:58:08 -0400662 }
663 }
664 } else {
665 for (int i = 0; i < primProc.numTextureSamplers(); ++i) {
666 auto texture = fixedDynamicState->fPrimitiveProcessorTextures[i]->peekTexture();
667 prepareSampledImage(texture, primProc.textureSampler(i).samplerState().filter());
Chris Dalton0d6f7752019-08-19 12:21:27 -0600668 this->appendSampledTexture(texture);
Brian Salomonf7232642018-09-19 08:58:08 -0400669 }
Brian Salomone782f842018-07-31 13:53:11 -0400670 }
bsalomonb58a2b42016-09-26 06:55:02 -0700671 GrFragmentProcessor::Iter iter(pipeline);
672 while (const GrFragmentProcessor* fp = iter.next()) {
Brian Salomone782f842018-07-31 13:53:11 -0400673 for (int i = 0; i < fp->numTextureSamplers(); ++i) {
674 const GrFragmentProcessor::TextureSampler& sampler = fp->textureSampler(i);
675 prepareSampledImage(sampler.peekTexture(), sampler.samplerState().filter());
Chris Dalton0d6f7752019-08-19 12:21:27 -0600676 this->appendSampledTexture(sampler.peekTexture());
Brian Salomone782f842018-07-31 13:53:11 -0400677 }
egdaniel2f5792a2016-07-06 08:51:23 -0700678 }
Robert Phillipsbb581ce2017-05-29 15:05:15 -0400679 if (GrTexture* dstTexture = pipeline.peekDstTexture()) {
Chris Dalton0d6f7752019-08-19 12:21:27 -0600680 this->appendSampledTexture(dstTexture);
Brian Salomon18dfa982017-04-03 16:57:43 -0400681 }
egdaniel2f5792a2016-07-06 08:51:23 -0700682
Chris Daltonbca46e22017-05-15 11:03:26 -0600683 GrPrimitiveType primitiveType = meshes[0].primitiveType();
Brian Salomon49348902018-06-26 09:12:38 -0400684 GrVkPipelineState* pipelineState = this->prepareDrawState(primProc, pipeline, fixedDynamicState,
685 dynamicStateArrays, primitiveType);
egdaniel9cb63402016-06-23 08:37:05 -0700686 if (!pipelineState) {
687 return;
688 }
689
Brian Salomond818ebf2018-07-02 14:08:49 +0000690 bool dynamicScissor =
691 pipeline.isScissorEnabled() && dynamicStateArrays && dynamicStateArrays->fScissorRects;
Brian Salomonf7232642018-09-19 08:58:08 -0400692 bool dynamicTextures = dynamicStateArrays && dynamicStateArrays->fPrimitiveProcessorTextures;
Brian Salomon49348902018-06-26 09:12:38 -0400693
egdaniel9cb63402016-06-23 08:37:05 -0700694 for (int i = 0; i < meshCount; ++i) {
695 const GrMesh& mesh = meshes[i];
Chris Daltonbca46e22017-05-15 11:03:26 -0600696 if (mesh.primitiveType() != primitiveType) {
Chris Dalton6f241802017-05-08 13:58:38 -0400697 SkDEBUGCODE(pipelineState = nullptr);
Chris Daltonbca46e22017-05-15 11:03:26 -0600698 primitiveType = mesh.primitiveType();
Brian Salomon49348902018-06-26 09:12:38 -0400699 pipelineState = this->prepareDrawState(primProc, pipeline, fixedDynamicState,
700 dynamicStateArrays, primitiveType);
Chris Dalton6f241802017-05-08 13:58:38 -0400701 if (!pipelineState) {
702 return;
egdaniel9cb63402016-06-23 08:37:05 -0700703 }
Chris Dalton6f241802017-05-08 13:58:38 -0400704 }
egdaniel9cb63402016-06-23 08:37:05 -0700705
Brian Salomon49348902018-06-26 09:12:38 -0400706 if (dynamicScissor) {
707 GrVkPipeline::SetDynamicScissorRectState(fGpu, cbInfo.currentCmdBuf(), fRenderTarget,
Robert Phillipsd0fe8752019-01-31 14:13:59 -0500708 fOrigin,
Brian Salomon49348902018-06-26 09:12:38 -0400709 dynamicStateArrays->fScissorRects[i]);
Chris Dalton46983b72017-06-06 12:27:16 -0600710 }
Brian Salomonf7232642018-09-19 08:58:08 -0400711 if (dynamicTextures) {
712 GrTextureProxy* const* meshProxies = dynamicStateArrays->fPrimitiveProcessorTextures +
713 primProc.numTextureSamplers() * i;
714 pipelineState->setAndBindTextures(fGpu, primProc, pipeline, meshProxies,
715 cbInfo.currentCmdBuf());
716 }
Chris Daltonbca46e22017-05-15 11:03:26 -0600717 SkASSERT(pipelineState);
Brian Salomon802cb312018-06-08 18:05:20 -0400718 mesh.sendToGpu(this);
egdaniel9cb63402016-06-23 08:37:05 -0700719 }
720
Greg Daniel36a77ee2016-10-18 10:33:25 -0400721 cbInfo.fBounds.join(bounds);
Chris Dalton114a3c02017-05-26 15:17:19 -0600722 cbInfo.fIsEmpty = false;
egdaniel066df7c2016-06-08 14:02:27 -0700723}
724
Chris Dalton0d6f7752019-08-19 12:21:27 -0600725void GrVkGpuRTCommandBuffer::appendSampledTexture(GrTexture* tex) {
726 SkASSERT(!tex->isProtected() || (fRenderTarget->isProtected() && fGpu->protectedContext()));
Robert Phillipse1efd382019-08-21 10:07:10 -0400727 GrVkTexture* vkTex = static_cast<GrVkTexture*>(tex);
728
729 fCommandBufferInfos[fCurrentCmdInfo].fSampledTextures.push_back(sk_ref_sp(vkTex));
Chris Dalton0d6f7752019-08-19 12:21:27 -0600730}
731
Brian Salomon802cb312018-06-08 18:05:20 -0400732void GrVkGpuRTCommandBuffer::sendInstancedMeshToGpu(GrPrimitiveType,
Greg Daniel500d58b2017-08-24 15:59:33 -0400733 const GrBuffer* vertexBuffer,
734 int vertexCount,
735 int baseVertex,
736 const GrBuffer* instanceBuffer,
737 int instanceCount,
738 int baseInstance) {
Chris Dalton114a3c02017-05-26 15:17:19 -0600739 CommandBufferInfo& cbInfo = fCommandBufferInfos[fCurrentCmdInfo];
Brian Salomondbf70722019-02-07 11:31:24 -0500740 SkASSERT(!vertexBuffer || !vertexBuffer->isCpuBuffer());
741 SkASSERT(!instanceBuffer || !instanceBuffer->isCpuBuffer());
742 auto gpuVertexBuffer = static_cast<const GrGpuBuffer*>(vertexBuffer);
743 auto gpuInstanceBuffer = static_cast<const GrGpuBuffer*>(instanceBuffer);
744 this->bindGeometry(nullptr, gpuVertexBuffer, gpuInstanceBuffer);
Chris Dalton1d616352017-05-31 12:51:23 -0600745 cbInfo.currentCmdBuf()->draw(fGpu, vertexCount, instanceCount, baseVertex, baseInstance);
Chris Dalton114a3c02017-05-26 15:17:19 -0600746 fGpu->stats()->incNumDraws();
747}
748
Brian Salomon802cb312018-06-08 18:05:20 -0400749void GrVkGpuRTCommandBuffer::sendIndexedInstancedMeshToGpu(GrPrimitiveType,
Greg Daniel500d58b2017-08-24 15:59:33 -0400750 const GrBuffer* indexBuffer,
751 int indexCount,
752 int baseIndex,
753 const GrBuffer* vertexBuffer,
754 int baseVertex,
755 const GrBuffer* instanceBuffer,
756 int instanceCount,
Brian Salomon802cb312018-06-08 18:05:20 -0400757 int baseInstance,
758 GrPrimitiveRestart restart) {
759 SkASSERT(restart == GrPrimitiveRestart::kNo);
Chris Dalton114a3c02017-05-26 15:17:19 -0600760 CommandBufferInfo& cbInfo = fCommandBufferInfos[fCurrentCmdInfo];
Brian Salomondbf70722019-02-07 11:31:24 -0500761 SkASSERT(!vertexBuffer || !vertexBuffer->isCpuBuffer());
762 SkASSERT(!instanceBuffer || !instanceBuffer->isCpuBuffer());
763 SkASSERT(!indexBuffer->isCpuBuffer());
764 auto gpuIndexxBuffer = static_cast<const GrGpuBuffer*>(indexBuffer);
765 auto gpuVertexBuffer = static_cast<const GrGpuBuffer*>(vertexBuffer);
766 auto gpuInstanceBuffer = static_cast<const GrGpuBuffer*>(instanceBuffer);
767 this->bindGeometry(gpuIndexxBuffer, gpuVertexBuffer, gpuInstanceBuffer);
Chris Dalton1d616352017-05-31 12:51:23 -0600768 cbInfo.currentCmdBuf()->drawIndexed(fGpu, indexCount, instanceCount,
769 baseIndex, baseVertex, baseInstance);
Chris Dalton114a3c02017-05-26 15:17:19 -0600770 fGpu->stats()->incNumDraws();
771}
Greg Daniel64cc9aa2018-10-19 13:54:56 -0400772
773////////////////////////////////////////////////////////////////////////////////
774
775void GrVkGpuRTCommandBuffer::executeDrawable(std::unique_ptr<SkDrawable::GpuDrawHandler> drawable) {
776 GrVkRenderTarget* target = static_cast<GrVkRenderTarget*>(fRenderTarget);
777
778 GrVkImage* targetImage = target->msaaImage() ? target->msaaImage() : target;
779
780 CommandBufferInfo& cbInfo = fCommandBufferInfos[fCurrentCmdInfo];
781 VkRect2D bounds;
782 bounds.offset = { 0, 0 };
783 bounds.extent = { 0, 0 };
784
785 GrVkDrawableInfo vkInfo;
786 vkInfo.fSecondaryCommandBuffer = cbInfo.currentCmdBuf()->vkCommandBuffer();
787 vkInfo.fCompatibleRenderPass = cbInfo.fRenderPass->vkRenderPass();
Greg Danielb353eeb2018-12-05 11:01:58 -0500788 SkAssertResult(cbInfo.fRenderPass->colorAttachmentIndex(&vkInfo.fColorAttachmentIndex));
Greg Daniel64cc9aa2018-10-19 13:54:56 -0400789 vkInfo.fFormat = targetImage->imageFormat();
790 vkInfo.fDrawBounds = &bounds;
Stan Ilievcb580602019-02-26 11:36:07 -0500791#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
792 vkInfo.fImage = targetImage->image();
793#else
794 vkInfo.fImage = VK_NULL_HANDLE;
795#endif //SK_BUILD_FOR_ANDROID_FRAMEWORK
Greg Daniel64cc9aa2018-10-19 13:54:56 -0400796
797 GrBackendDrawableInfo info(vkInfo);
798
Eric Karlc0b2ba22019-01-22 19:40:35 -0800799 // After we draw into the command buffer via the drawable, cached state we have may be invalid.
800 cbInfo.currentCmdBuf()->invalidateState();
Eric Karla8878a12019-02-07 18:17:43 -0800801 // Also assume that the drawable produced output.
802 cbInfo.fIsEmpty = false;
Eric Karlc0b2ba22019-01-22 19:40:35 -0800803
Greg Daniel64cc9aa2018-10-19 13:54:56 -0400804 drawable->draw(info);
805 fGpu->addDrawable(std::move(drawable));
806
807 if (bounds.extent.width == 0 || bounds.extent.height == 0) {
808 cbInfo.fBounds.join(target->getBoundsRect());
809 } else {
810 cbInfo.fBounds.join(SkRect::MakeXYWH(bounds.offset.x, bounds.offset.y,
811 bounds.extent.width, bounds.extent.height));
812 }
813}
814