blob: 6862436dceaa954bf07227809a70f7017b49442a [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"
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
Robert Phillips6b47c7d2017-08-29 07:24:09 -040051void get_vk_load_store_ops(GrLoadOp loadOpIn, GrStoreOp storeOpIn,
egdaniel066df7c2016-06-08 14:02:27 -070052 VkAttachmentLoadOp* loadOp, VkAttachmentStoreOp* storeOp) {
Robert Phillips95214472017-08-08 18:00:03 -040053 switch (loadOpIn) {
Robert Phillips6b47c7d2017-08-29 07:24:09 -040054 case GrLoadOp::kLoad:
egdaniel066df7c2016-06-08 14:02:27 -070055 *loadOp = VK_ATTACHMENT_LOAD_OP_LOAD;
egdaniel066df7c2016-06-08 14:02:27 -070056 break;
Robert Phillips6b47c7d2017-08-29 07:24:09 -040057 case GrLoadOp::kClear:
egdaniel9cb63402016-06-23 08:37:05 -070058 *loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
59 break;
Robert Phillips6b47c7d2017-08-29 07:24:09 -040060 case GrLoadOp::kDiscard:
egdaniel9cb63402016-06-23 08:37:05 -070061 *loadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
62 break;
63 default:
64 SK_ABORT("Invalid LoadOp");
egdaniel066df7c2016-06-08 14:02:27 -070065 *loadOp = VK_ATTACHMENT_LOAD_OP_LOAD;
egdaniel9cb63402016-06-23 08:37:05 -070066 }
67
Robert Phillips95214472017-08-08 18:00:03 -040068 switch (storeOpIn) {
Robert Phillips6b47c7d2017-08-29 07:24:09 -040069 case GrStoreOp::kStore:
egdaniel066df7c2016-06-08 14:02:27 -070070 *storeOp = VK_ATTACHMENT_STORE_OP_STORE;
71 break;
Robert Phillips6b47c7d2017-08-29 07:24:09 -040072 case GrStoreOp::kDiscard:
egdaniel066df7c2016-06-08 14:02:27 -070073 *storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
74 break;
brianosman0bbc3712016-06-14 04:53:09 -070075 default:
egdaniel9cb63402016-06-23 08:37:05 -070076 SK_ABORT("Invalid StoreOp");
brianosman0bbc3712016-06-14 04:53:09 -070077 *storeOp = VK_ATTACHMENT_STORE_OP_STORE;
egdaniel066df7c2016-06-08 14:02:27 -070078 }
79}
80
Greg Daniel2d41d0d2019-08-26 11:08:51 -040081GrVkOpsRenderPass::GrVkOpsRenderPass(GrVkGpu* gpu) : fGpu(gpu) {}
Brian Salomonc293a292016-11-30 13:38:32 -050082
Greg Daniel2d41d0d2019-08-26 11:08:51 -040083void GrVkOpsRenderPass::init() {
Brian Salomonc293a292016-11-30 13:38:32 -050084 GrVkRenderPass::LoadStoreOps vkColorOps(fVkColorLoadOp, fVkColorStoreOp);
85 GrVkRenderPass::LoadStoreOps vkStencilOps(fVkStencilLoadOp, fVkStencilStoreOp);
egdaniel9cb63402016-06-23 08:37:05 -070086
Greg Daniel36a77ee2016-10-18 10:33:25 -040087 CommandBufferInfo& cbInfo = fCommandBufferInfos.push_back();
Brian Salomonc293a292016-11-30 13:38:32 -050088 SkASSERT(fCommandBufferInfos.count() == 1);
Greg Daniel22bc8652017-03-22 15:45:43 -040089 fCurrentCmdInfo = 0;
Greg Daniel36a77ee2016-10-18 10:33:25 -040090
Robert Phillips19e51dc2017-08-09 09:30:51 -040091 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(fRenderTarget);
92 const GrVkResourceProvider::CompatibleRPHandle& rpHandle = vkRT->compatibleRenderPassHandle();
egdaniel066df7c2016-06-08 14:02:27 -070093 if (rpHandle.isValid()) {
Greg Daniel36a77ee2016-10-18 10:33:25 -040094 cbInfo.fRenderPass = fGpu->resourceProvider().findRenderPass(rpHandle,
95 vkColorOps,
96 vkStencilOps);
egdaniel066df7c2016-06-08 14:02:27 -070097 } else {
Robert Phillips19e51dc2017-08-09 09:30:51 -040098 cbInfo.fRenderPass = fGpu->resourceProvider().findRenderPass(*vkRT,
Greg Daniel36a77ee2016-10-18 10:33:25 -040099 vkColorOps,
100 vkStencilOps);
egdaniel066df7c2016-06-08 14:02:27 -0700101 }
102
Brian Osmancb3d0872018-10-16 15:19:28 -0400103 cbInfo.fColorClearValue.color.float32[0] = fClearColor[0];
104 cbInfo.fColorClearValue.color.float32[1] = fClearColor[1];
105 cbInfo.fColorClearValue.color.float32[2] = fClearColor[2];
106 cbInfo.fColorClearValue.color.float32[3] = fClearColor[3];
egdaniel9cb63402016-06-23 08:37:05 -0700107
Robert Phillips380b90c2017-08-30 07:41:07 -0400108 if (VK_ATTACHMENT_LOAD_OP_CLEAR == fVkColorLoadOp) {
Greg Daniela41a74a2018-10-09 12:59:23 +0000109 cbInfo.fBounds = SkRect::MakeWH(vkRT->width(), vkRT->height());
Robert Phillips380b90c2017-08-30 07:41:07 -0400110 } else {
111 cbInfo.fBounds.setEmpty();
112 }
Greg Daniela3c68df2018-03-16 13:46:53 -0400113
114 if (VK_ATTACHMENT_LOAD_OP_CLEAR == fVkColorLoadOp) {
115 cbInfo.fLoadStoreState = LoadStoreState::kStartsWithClear;
116 } else if (VK_ATTACHMENT_LOAD_OP_LOAD == fVkColorLoadOp &&
117 VK_ATTACHMENT_STORE_OP_STORE == fVkColorStoreOp) {
118 cbInfo.fLoadStoreState = LoadStoreState::kLoadAndStore;
119 } else if (VK_ATTACHMENT_LOAD_OP_DONT_CARE == fVkColorLoadOp) {
120 cbInfo.fLoadStoreState = LoadStoreState::kStartsWithDiscard;
121 }
Greg Daniel36a77ee2016-10-18 10:33:25 -0400122
Greg Daniel228518f2019-08-07 16:55:17 -0400123 cbInfo.fCommandBuffer = fGpu->cmdPool()->findOrCreateSecondaryCommandBuffer(fGpu);
Robert Phillips19e51dc2017-08-09 09:30:51 -0400124 cbInfo.currentCmdBuf()->begin(fGpu, vkRT->framebuffer(), cbInfo.fRenderPass);
egdaniel066df7c2016-06-08 14:02:27 -0700125}
126
Greg Daniel2d41d0d2019-08-26 11:08:51 -0400127void GrVkOpsRenderPass::initWrapped() {
Greg Daniel070cbaf2019-01-03 17:35:54 -0500128 CommandBufferInfo& cbInfo = fCommandBufferInfos.push_back();
129 SkASSERT(fCommandBufferInfos.count() == 1);
130 fCurrentCmdInfo = 0;
131
132 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(fRenderTarget);
133 SkASSERT(vkRT->wrapsSecondaryCommandBuffer());
134 cbInfo.fRenderPass = vkRT->externalRenderPass();
135 cbInfo.fRenderPass->ref();
136
137 cbInfo.fBounds.setEmpty();
Greg Daniel228518f2019-08-07 16:55:17 -0400138 cbInfo.fCommandBuffer.reset(
Greg Daniel8daf3b72019-07-30 09:57:26 -0400139 GrVkSecondaryCommandBuffer::Create(vkRT->getExternalSecondaryCommandBuffer()));
Greg Daniel070cbaf2019-01-03 17:35:54 -0500140 cbInfo.currentCmdBuf()->begin(fGpu, nullptr, cbInfo.fRenderPass);
141}
Brian Salomonc293a292016-11-30 13:38:32 -0500142
Greg Daniel2d41d0d2019-08-26 11:08:51 -0400143GrVkOpsRenderPass::~GrVkOpsRenderPass() {
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400144 this->reset();
egdaniel066df7c2016-06-08 14:02:27 -0700145}
146
Greg Daniel2d41d0d2019-08-26 11:08:51 -0400147GrGpu* GrVkOpsRenderPass::gpu() { return fGpu; }
egdaniel9cb63402016-06-23 08:37:05 -0700148
Greg Daniel2d41d0d2019-08-26 11:08:51 -0400149void GrVkOpsRenderPass::end() {
Greg Daniel22bc8652017-03-22 15:45:43 -0400150 if (fCurrentCmdInfo >= 0) {
151 fCommandBufferInfos[fCurrentCmdInfo].currentCmdBuf()->end(fGpu);
Brian Salomonc293a292016-11-30 13:38:32 -0500152 }
egdaniel066df7c2016-06-08 14:02:27 -0700153}
154
Greg Daniel2d41d0d2019-08-26 11:08:51 -0400155void GrVkOpsRenderPass::submit() {
Brian Salomonc293a292016-11-30 13:38:32 -0500156 if (!fRenderTarget) {
157 return;
158 }
Robert Phillips19e51dc2017-08-09 09:30:51 -0400159
160 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(fRenderTarget);
Robert Phillips19e51dc2017-08-09 09:30:51 -0400161 GrVkImage* targetImage = vkRT->msaaImage() ? vkRT->msaaImage() : vkRT;
Greg Daniel45a44de2018-02-27 10:07:29 -0500162 GrStencilAttachment* stencil = fRenderTarget->renderTargetPriv().getStencilAttachment();
Brian Salomon24d377e2019-04-23 15:24:31 -0400163 auto currPreCmd = fPreCommandBufferTasks.begin();
egdaniel9cb63402016-06-23 08:37:05 -0700164
Greg Daniel46cfbc62019-06-07 11:43:30 -0400165 GrVkPrimaryCommandBufferTask::Args taskArgs{fGpu, fRenderTarget};
Greg Daniel36a77ee2016-10-18 10:33:25 -0400166 for (int i = 0; i < fCommandBufferInfos.count(); ++i) {
167 CommandBufferInfo& cbInfo = fCommandBufferInfos[i];
168
Brian Salomon24d377e2019-04-23 15:24:31 -0400169 for (int c = 0; c < cbInfo.fNumPreCmds; ++c, ++currPreCmd) {
Brian Salomon5d8f1cc2019-04-24 09:03:53 -0400170 currPreCmd->execute(taskArgs);
Greg Daniel77b53f62016-10-18 11:48:51 -0400171 }
172
Greg Daniel38c3d932018-03-16 14:22:30 -0400173 // TODO: Many things create a scratch texture which adds the discard immediately, but then
174 // don't draw to it right away. This causes the discard to be ignored and we get yelled at
175 // for loading uninitialized data. However, once MDB lands with reordering, the discard will
176 // get reordered with the rest of the draw commands and we can remove the discard check.
177 if (cbInfo.fIsEmpty &&
178 cbInfo.fLoadStoreState != LoadStoreState::kStartsWithClear &&
179 cbInfo.fLoadStoreState != LoadStoreState::kStartsWithDiscard) {
Greg Daniel77b53f62016-10-18 11:48:51 -0400180 // We have sumbitted no actual draw commands to the command buffer and we are not using
181 // the render pass to do a clear so there is no need to submit anything.
182 continue;
183 }
Greg Daniel38c3d932018-03-16 14:22:30 -0400184
Greg Daniel070cbaf2019-01-03 17:35:54 -0500185 // We don't want to actually submit the secondary command buffer if it is wrapped.
186 if (this->wrapsSecondaryCommandBuffer()) {
187 // If we have any sampled images set their layout now.
Chris Dalton298238a2019-02-21 16:28:44 -0500188 for (int j = 0; j < cbInfo.fSampledTextures.count(); ++j) {
189 cbInfo.fSampledTextures[j]->setImageLayout(
190 fGpu, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, VK_ACCESS_SHADER_READ_BIT,
191 VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT, false);
Greg Daniel070cbaf2019-01-03 17:35:54 -0500192 }
193
194 // There should have only been one secondary command buffer in the wrapped case so it is
195 // safe to just return here.
196 SkASSERT(fCommandBufferInfos.count() == 1);
197 return;
198 }
199
Greg Danieldbdba602018-04-20 11:52:43 -0400200 // Make sure if we only have a discard load that we execute the discard on the whole image.
201 // TODO: Once we improve our tracking of discards so that we never end up flushing a discard
202 // call with no actually ops, remove this.
203 if (cbInfo.fIsEmpty && cbInfo.fLoadStoreState == LoadStoreState::kStartsWithDiscard) {
Greg Daniela41a74a2018-10-09 12:59:23 +0000204 cbInfo.fBounds = SkRect::MakeWH(vkRT->width(), vkRT->height());
Greg Danieldbdba602018-04-20 11:52:43 -0400205 }
206
Mike Reed9ea63152019-08-22 16:19:50 -0400207 if (cbInfo.fBounds.intersect(SkRect::MakeIWH(fRenderTarget->width(),
208 fRenderTarget->height()))) {
Greg Daniel38c3d932018-03-16 14:22:30 -0400209 // Make sure we do the following layout changes after all copies, uploads, or any other
210 // pre-work is done since we may change the layouts in the pre-work. Also since the
211 // draws will be submitted in different render passes, we need to guard againts write
212 // and write issues.
213
214 // Change layout of our render target so it can be used as the color attachment.
Greg Danielf7828d02018-10-09 12:01:32 -0400215 // TODO: If we know that we will never be blending or loading the attachment we could
216 // drop the VK_ACCESS_COLOR_ATTACHMENT_READ_BIT.
Greg Daniel38c3d932018-03-16 14:22:30 -0400217 targetImage->setImageLayout(fGpu,
218 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Greg Danielf7828d02018-10-09 12:01:32 -0400219 VK_ACCESS_COLOR_ATTACHMENT_READ_BIT |
Greg Daniel38c3d932018-03-16 14:22:30 -0400220 VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
Greg Danielf7828d02018-10-09 12:01:32 -0400221 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
Greg Daniel38c3d932018-03-16 14:22:30 -0400222 false);
223
224 // If we are using a stencil attachment we also need to update its layout
225 if (stencil) {
226 GrVkStencilAttachment* vkStencil = (GrVkStencilAttachment*)stencil;
Greg Danielf7828d02018-10-09 12:01:32 -0400227 // We need the write and read access bits since we may load and store the stencil.
228 // The initial load happens in the VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT so we
229 // wait there.
Greg Daniel38c3d932018-03-16 14:22:30 -0400230 vkStencil->setImageLayout(fGpu,
231 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
232 VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT |
233 VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT,
Greg Danielf7828d02018-10-09 12:01:32 -0400234 VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT,
Greg Daniel38c3d932018-03-16 14:22:30 -0400235 false);
236 }
237
238 // If we have any sampled images set their layout now.
Chris Dalton298238a2019-02-21 16:28:44 -0500239 for (int j = 0; j < cbInfo.fSampledTextures.count(); ++j) {
240 cbInfo.fSampledTextures[j]->setImageLayout(
241 fGpu, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, VK_ACCESS_SHADER_READ_BIT,
242 VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT, false);
Greg Daniel38c3d932018-03-16 14:22:30 -0400243 }
244
Greg Daniel36a77ee2016-10-18 10:33:25 -0400245 SkIRect iBounds;
246 cbInfo.fBounds.roundOut(&iBounds);
247
Greg Daniel228518f2019-08-07 16:55:17 -0400248 fGpu->submitSecondaryCommandBuffer(std::move(cbInfo.fCommandBuffer), cbInfo.fRenderPass,
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400249 &cbInfo.fColorClearValue, vkRT, fOrigin, iBounds);
Greg Daniel36a77ee2016-10-18 10:33:25 -0400250 }
251 }
Brian Salomon24d377e2019-04-23 15:24:31 -0400252 SkASSERT(currPreCmd == fPreCommandBufferTasks.end());
egdaniel9cb63402016-06-23 08:37:05 -0700253}
254
Greg Daniel2d41d0d2019-08-26 11:08:51 -0400255void GrVkOpsRenderPass::set(GrRenderTarget* rt, GrSurfaceOrigin origin,
256 const GrOpsRenderPass::LoadAndStoreInfo& colorInfo,
257 const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilInfo) {
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400258 SkASSERT(!fRenderTarget);
259 SkASSERT(fCommandBufferInfos.empty());
260 SkASSERT(-1 == fCurrentCmdInfo);
Robert Phillips9da87e02019-02-04 13:26:26 -0500261 SkASSERT(fGpu == rt->getContext()->priv().getGpu());
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400262 SkASSERT(!fLastPipelineState);
263
Greg Danielb0c7ad12019-06-06 17:23:35 +0000264#ifdef SK_DEBUG
265 fIsActive = true;
266#endif
267
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400268 this->INHERITED::set(rt, origin);
269
Greg Daniel070cbaf2019-01-03 17:35:54 -0500270 if (this->wrapsSecondaryCommandBuffer()) {
271 this->initWrapped();
272 return;
273 }
274
Brian Osman9a9baae2018-11-05 15:06:26 -0500275 fClearColor = colorInfo.fClearColor;
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400276
277 get_vk_load_store_ops(colorInfo.fLoadOp, colorInfo.fStoreOp,
278 &fVkColorLoadOp, &fVkColorStoreOp);
279
280 get_vk_load_store_ops(stencilInfo.fLoadOp, stencilInfo.fStoreOp,
281 &fVkStencilLoadOp, &fVkStencilStoreOp);
282
283 this->init();
284}
285
Greg Daniel2d41d0d2019-08-26 11:08:51 -0400286void GrVkOpsRenderPass::reset() {
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400287 for (int i = 0; i < fCommandBufferInfos.count(); ++i) {
288 CommandBufferInfo& cbInfo = fCommandBufferInfos[i];
Greg Daniel228518f2019-08-07 16:55:17 -0400289 if (cbInfo.fCommandBuffer) {
290 cbInfo.fCommandBuffer.release()->recycle(fGpu);
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400291 }
292 cbInfo.fRenderPass->unref(fGpu);
293 }
294 fCommandBufferInfos.reset();
Brian Salomon24d377e2019-04-23 15:24:31 -0400295 fPreCommandBufferTasks.reset();
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400296
297 fCurrentCmdInfo = -1;
298
299 fLastPipelineState = nullptr;
300 fRenderTarget = nullptr;
Greg Danielb0c7ad12019-06-06 17:23:35 +0000301
302#ifdef SK_DEBUG
303 fIsActive = false;
304#endif
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400305}
306
Greg Daniel2d41d0d2019-08-26 11:08:51 -0400307bool GrVkOpsRenderPass::wrapsSecondaryCommandBuffer() const {
Greg Daniel070cbaf2019-01-03 17:35:54 -0500308 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(fRenderTarget);
309 return vkRT->wrapsSecondaryCommandBuffer();
310}
311
Greg Daniel64cc9aa2018-10-19 13:54:56 -0400312////////////////////////////////////////////////////////////////////////////////
313
Greg Daniel2d41d0d2019-08-26 11:08:51 -0400314void GrVkOpsRenderPass::insertEventMarker(const char* msg) {
Robert Phillips65a88fa2017-08-08 08:36:22 -0400315 // TODO: does Vulkan have a correlate?
316}
317
Greg Daniel2d41d0d2019-08-26 11:08:51 -0400318void GrVkOpsRenderPass::onClearStencilClip(const GrFixedClip& clip, bool insideStencilMask) {
Chris Dalton94c04682017-11-01 17:15:06 -0600319 SkASSERT(!clip.hasWindowRectangles());
320
Jim Van Verth6a40abc2017-11-02 16:56:09 +0000321 CommandBufferInfo& cbInfo = fCommandBufferInfos[fCurrentCmdInfo];
322
Greg Daniel65a09272016-10-12 09:47:22 -0400323 GrStencilAttachment* sb = fRenderTarget->renderTargetPriv().getStencilAttachment();
egdaniel9cb63402016-06-23 08:37:05 -0700324 // this should only be called internally when we know we have a
325 // stencil buffer.
326 SkASSERT(sb);
327 int stencilBitCount = sb->bits();
328
329 // The contract with the callers does not guarantee that we preserve all bits in the stencil
330 // during this clear. Thus we will clear the entire stencil to the desired value.
331
332 VkClearDepthStencilValue vkStencilColor;
333 memset(&vkStencilColor, 0, sizeof(VkClearDepthStencilValue));
csmartdalton29df7602016-08-31 11:55:52 -0700334 if (insideStencilMask) {
egdaniel9cb63402016-06-23 08:37:05 -0700335 vkStencilColor.stencil = (1 << (stencilBitCount - 1));
336 } else {
337 vkStencilColor.stencil = 0;
338 }
339
340 VkClearRect clearRect;
341 // Flip rect if necessary
csmartdalton29df7602016-08-31 11:55:52 -0700342 SkIRect vkRect;
Brian Salomond818ebf2018-07-02 14:08:49 +0000343 if (!clip.scissorEnabled()) {
Greg Daniela41a74a2018-10-09 12:59:23 +0000344 vkRect.setXYWH(0, 0, fRenderTarget->width(), fRenderTarget->height());
Robert Phillips4f101a72017-07-28 08:42:04 -0400345 } else if (kBottomLeft_GrSurfaceOrigin != fOrigin) {
csmartdalton29df7602016-08-31 11:55:52 -0700346 vkRect = clip.scissorRect();
347 } else {
348 const SkIRect& scissor = clip.scissorRect();
Greg Daniel65a09272016-10-12 09:47:22 -0400349 vkRect.setLTRB(scissor.fLeft, fRenderTarget->height() - scissor.fBottom,
350 scissor.fRight, fRenderTarget->height() - scissor.fTop);
egdaniel9cb63402016-06-23 08:37:05 -0700351 }
352
353 clearRect.rect.offset = { vkRect.fLeft, vkRect.fTop };
354 clearRect.rect.extent = { (uint32_t)vkRect.width(), (uint32_t)vkRect.height() };
355
356 clearRect.baseArrayLayer = 0;
357 clearRect.layerCount = 1;
358
359 uint32_t stencilIndex;
Greg Daniel36a77ee2016-10-18 10:33:25 -0400360 SkAssertResult(cbInfo.fRenderPass->stencilAttachmentIndex(&stencilIndex));
egdaniel9cb63402016-06-23 08:37:05 -0700361
362 VkClearAttachment attachment;
363 attachment.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
364 attachment.colorAttachment = 0; // this value shouldn't matter
365 attachment.clearValue.depthStencil = vkStencilColor;
366
Greg Daniel22bc8652017-03-22 15:45:43 -0400367 cbInfo.currentCmdBuf()->clearAttachments(fGpu, 1, &attachment, 1, &clearRect);
Greg Daniel77b53f62016-10-18 11:48:51 -0400368 cbInfo.fIsEmpty = false;
Greg Daniel36a77ee2016-10-18 10:33:25 -0400369
370 // Update command buffer bounds
Brian Salomond818ebf2018-07-02 14:08:49 +0000371 if (!clip.scissorEnabled()) {
Greg Daniel36a77ee2016-10-18 10:33:25 -0400372 cbInfo.fBounds.join(fRenderTarget->getBoundsRect());
373 } else {
374 cbInfo.fBounds.join(SkRect::Make(clip.scissorRect()));
375 }
egdaniel9cb63402016-06-23 08:37:05 -0700376}
377
Greg Daniel2d41d0d2019-08-26 11:08:51 -0400378void GrVkOpsRenderPass::onClear(const GrFixedClip& clip, const SkPMColor4f& color) {
Robert Phillips19e51dc2017-08-09 09:30:51 -0400379 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(fRenderTarget);
380
Jim Van Verth6a40abc2017-11-02 16:56:09 +0000381 // parent class should never let us get here with no RT
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700382 SkASSERT(!clip.hasWindowRectangles());
egdaniel9cb63402016-06-23 08:37:05 -0700383
Greg Daniel22bc8652017-03-22 15:45:43 -0400384 CommandBufferInfo& cbInfo = fCommandBufferInfos[fCurrentCmdInfo];
Greg Daniel36a77ee2016-10-18 10:33:25 -0400385
Brian Osman9a9baae2018-11-05 15:06:26 -0500386 VkClearColorValue vkColor = {{color.fR, color.fG, color.fB, color.fA}};
egdaniel9cb63402016-06-23 08:37:05 -0700387
Brian Salomond818ebf2018-07-02 14:08:49 +0000388 if (cbInfo.fIsEmpty && !clip.scissorEnabled()) {
Robert Phillips74c627f2017-08-09 10:28:00 -0400389 // Change the render pass to do a clear load
egdaniel9cb63402016-06-23 08:37:05 -0700390 GrVkRenderPass::LoadStoreOps vkColorOps(VK_ATTACHMENT_LOAD_OP_CLEAR,
391 VK_ATTACHMENT_STORE_OP_STORE);
Robert Phillips74c627f2017-08-09 10:28:00 -0400392 // Preserve the stencil buffer's load & store settings
393 GrVkRenderPass::LoadStoreOps vkStencilOps(fVkStencilLoadOp, fVkStencilStoreOp);
egdaniel9cb63402016-06-23 08:37:05 -0700394
Greg Daniel36a77ee2016-10-18 10:33:25 -0400395 const GrVkRenderPass* oldRP = cbInfo.fRenderPass;
egdaniel9cb63402016-06-23 08:37:05 -0700396
397 const GrVkResourceProvider::CompatibleRPHandle& rpHandle =
Robert Phillips19e51dc2017-08-09 09:30:51 -0400398 vkRT->compatibleRenderPassHandle();
egdaniel9cb63402016-06-23 08:37:05 -0700399 if (rpHandle.isValid()) {
Greg Daniel36a77ee2016-10-18 10:33:25 -0400400 cbInfo.fRenderPass = fGpu->resourceProvider().findRenderPass(rpHandle,
401 vkColorOps,
402 vkStencilOps);
egdaniel9cb63402016-06-23 08:37:05 -0700403 } else {
Robert Phillips19e51dc2017-08-09 09:30:51 -0400404 cbInfo.fRenderPass = fGpu->resourceProvider().findRenderPass(*vkRT,
Greg Daniel36a77ee2016-10-18 10:33:25 -0400405 vkColorOps,
406 vkStencilOps);
egdaniel9cb63402016-06-23 08:37:05 -0700407 }
408
Greg Daniel36a77ee2016-10-18 10:33:25 -0400409 SkASSERT(cbInfo.fRenderPass->isCompatible(*oldRP));
egdaniel9cb63402016-06-23 08:37:05 -0700410 oldRP->unref(fGpu);
411
Brian Osman9a9baae2018-11-05 15:06:26 -0500412 cbInfo.fColorClearValue.color = {{color.fR, color.fG, color.fB, color.fA}};
Greg Daniela3c68df2018-03-16 13:46:53 -0400413 cbInfo.fLoadStoreState = LoadStoreState::kStartsWithClear;
Greg Daniel36a77ee2016-10-18 10:33:25 -0400414 // Update command buffer bounds
415 cbInfo.fBounds.join(fRenderTarget->getBoundsRect());
egdaniel9cb63402016-06-23 08:37:05 -0700416 return;
417 }
418
419 // We always do a sub rect clear with clearAttachments since we are inside a render pass
420 VkClearRect clearRect;
421 // Flip rect if necessary
csmartdalton29df7602016-08-31 11:55:52 -0700422 SkIRect vkRect;
Brian Salomond818ebf2018-07-02 14:08:49 +0000423 if (!clip.scissorEnabled()) {
Greg Daniela41a74a2018-10-09 12:59:23 +0000424 vkRect.setXYWH(0, 0, fRenderTarget->width(), fRenderTarget->height());
Robert Phillips4f101a72017-07-28 08:42:04 -0400425 } else if (kBottomLeft_GrSurfaceOrigin != fOrigin) {
csmartdalton29df7602016-08-31 11:55:52 -0700426 vkRect = clip.scissorRect();
427 } else {
428 const SkIRect& scissor = clip.scissorRect();
Greg Daniel65a09272016-10-12 09:47:22 -0400429 vkRect.setLTRB(scissor.fLeft, fRenderTarget->height() - scissor.fBottom,
430 scissor.fRight, fRenderTarget->height() - scissor.fTop);
egdaniel9cb63402016-06-23 08:37:05 -0700431 }
432 clearRect.rect.offset = { vkRect.fLeft, vkRect.fTop };
433 clearRect.rect.extent = { (uint32_t)vkRect.width(), (uint32_t)vkRect.height() };
434 clearRect.baseArrayLayer = 0;
435 clearRect.layerCount = 1;
436
437 uint32_t colorIndex;
Greg Daniel36a77ee2016-10-18 10:33:25 -0400438 SkAssertResult(cbInfo.fRenderPass->colorAttachmentIndex(&colorIndex));
egdaniel9cb63402016-06-23 08:37:05 -0700439
440 VkClearAttachment attachment;
441 attachment.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
442 attachment.colorAttachment = colorIndex;
443 attachment.clearValue.color = vkColor;
444
Greg Daniel22bc8652017-03-22 15:45:43 -0400445 cbInfo.currentCmdBuf()->clearAttachments(fGpu, 1, &attachment, 1, &clearRect);
Greg Daniel77b53f62016-10-18 11:48:51 -0400446 cbInfo.fIsEmpty = false;
Greg Daniel36a77ee2016-10-18 10:33:25 -0400447
448 // Update command buffer bounds
Brian Salomond818ebf2018-07-02 14:08:49 +0000449 if (!clip.scissorEnabled()) {
Greg Daniel36a77ee2016-10-18 10:33:25 -0400450 cbInfo.fBounds.join(fRenderTarget->getBoundsRect());
451 } else {
452 cbInfo.fBounds.join(SkRect::Make(clip.scissorRect()));
453 }
egdaniel9cb63402016-06-23 08:37:05 -0700454 return;
455}
456
Greg Daniel500d58b2017-08-24 15:59:33 -0400457////////////////////////////////////////////////////////////////////////////////
458
Greg Daniel2d41d0d2019-08-26 11:08:51 -0400459void GrVkOpsRenderPass::addAdditionalRenderPass() {
Robert Phillips19e51dc2017-08-09 09:30:51 -0400460 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(fRenderTarget);
461
Greg Daniel22bc8652017-03-22 15:45:43 -0400462 fCommandBufferInfos[fCurrentCmdInfo].currentCmdBuf()->end(fGpu);
Greg Daniel77b53f62016-10-18 11:48:51 -0400463
464 CommandBufferInfo& cbInfo = fCommandBufferInfos.push_back();
Greg Daniel22bc8652017-03-22 15:45:43 -0400465 fCurrentCmdInfo++;
Greg Daniel77b53f62016-10-18 11:48:51 -0400466
467 GrVkRenderPass::LoadStoreOps vkColorOps(VK_ATTACHMENT_LOAD_OP_LOAD,
468 VK_ATTACHMENT_STORE_OP_STORE);
469 GrVkRenderPass::LoadStoreOps vkStencilOps(VK_ATTACHMENT_LOAD_OP_LOAD,
470 VK_ATTACHMENT_STORE_OP_STORE);
471
472 const GrVkResourceProvider::CompatibleRPHandle& rpHandle =
Robert Phillips19e51dc2017-08-09 09:30:51 -0400473 vkRT->compatibleRenderPassHandle();
Greg Daniel77b53f62016-10-18 11:48:51 -0400474 if (rpHandle.isValid()) {
475 cbInfo.fRenderPass = fGpu->resourceProvider().findRenderPass(rpHandle,
476 vkColorOps,
477 vkStencilOps);
478 } else {
Robert Phillips19e51dc2017-08-09 09:30:51 -0400479 cbInfo.fRenderPass = fGpu->resourceProvider().findRenderPass(*vkRT,
Greg Daniel77b53f62016-10-18 11:48:51 -0400480 vkColorOps,
481 vkStencilOps);
482 }
Greg Daniela3c68df2018-03-16 13:46:53 -0400483 cbInfo.fLoadStoreState = LoadStoreState::kLoadAndStore;
Greg Daniel77b53f62016-10-18 11:48:51 -0400484
Greg Daniel228518f2019-08-07 16:55:17 -0400485 cbInfo.fCommandBuffer = fGpu->cmdPool()->findOrCreateSecondaryCommandBuffer(fGpu);
Greg Daniel77b53f62016-10-18 11:48:51 -0400486 // It shouldn't matter what we set the clear color to here since we will assume loading of the
487 // attachment.
488 memset(&cbInfo.fColorClearValue, 0, sizeof(VkClearValue));
489 cbInfo.fBounds.setEmpty();
Greg Daniel77b53f62016-10-18 11:48:51 -0400490
Robert Phillips19e51dc2017-08-09 09:30:51 -0400491 cbInfo.currentCmdBuf()->begin(fGpu, vkRT->framebuffer(), cbInfo.fRenderPass);
Greg Daniel77b53f62016-10-18 11:48:51 -0400492}
493
Greg Daniel2d41d0d2019-08-26 11:08:51 -0400494void GrVkOpsRenderPass::inlineUpload(GrOpFlushState* state,
Brian Salomon943ed792017-10-30 09:37:55 -0400495 GrDeferredTextureUploadFn& upload) {
Greg Daniel22bc8652017-03-22 15:45:43 -0400496 if (!fCommandBufferInfos[fCurrentCmdInfo].fIsEmpty) {
497 this->addAdditionalRenderPass();
Greg Daniel77b53f62016-10-18 11:48:51 -0400498 }
Brian Salomon24d377e2019-04-23 15:24:31 -0400499
Brian Salomon24d377e2019-04-23 15:24:31 -0400500 fPreCommandBufferTasks.emplace<InlineUpload>(state, upload);
501 ++fCommandBufferInfos[fCurrentCmdInfo].fNumPreCmds;
Greg Daniel77b53f62016-10-18 11:48:51 -0400502}
503
egdaniel9cb63402016-06-23 08:37:05 -0700504////////////////////////////////////////////////////////////////////////////////
505
Greg Daniel2d41d0d2019-08-26 11:08:51 -0400506void GrVkOpsRenderPass::bindGeometry(const GrGpuBuffer* indexBuffer,
Brian Salomondbf70722019-02-07 11:31:24 -0500507 const GrGpuBuffer* vertexBuffer,
508 const GrGpuBuffer* instanceBuffer) {
Chris Daltonff926502017-05-03 14:36:54 -0400509 GrVkSecondaryCommandBuffer* currCmdBuf = fCommandBufferInfos[fCurrentCmdInfo].currentCmdBuf();
egdaniel9cb63402016-06-23 08:37:05 -0700510 // There is no need to put any memory barriers to make sure host writes have finished here.
511 // When a command buffer is submitted to a queue, there is an implicit memory barrier that
512 // occurs for all host writes. Additionally, BufferMemoryBarriers are not allowed inside of
513 // an active RenderPass.
egdaniel9cb63402016-06-23 08:37:05 -0700514
Chris Dalton1d616352017-05-31 12:51:23 -0600515 // Here our vertex and instance inputs need to match the same 0-based bindings they were
516 // assigned in GrVkPipeline. That is, vertex first (if any) followed by instance.
517 uint32_t binding = 0;
518
Brian Salomon802cb312018-06-08 18:05:20 -0400519 if (vertexBuffer) {
Chris Dalton1d616352017-05-31 12:51:23 -0600520 SkASSERT(vertexBuffer);
Chris Dalton1d616352017-05-31 12:51:23 -0600521 SkASSERT(!vertexBuffer->isMapped());
522
523 currCmdBuf->bindInputBuffer(fGpu, binding++,
524 static_cast<const GrVkVertexBuffer*>(vertexBuffer));
525 }
526
Brian Salomon802cb312018-06-08 18:05:20 -0400527 if (instanceBuffer) {
Chris Dalton1d616352017-05-31 12:51:23 -0600528 SkASSERT(instanceBuffer);
Chris Dalton1d616352017-05-31 12:51:23 -0600529 SkASSERT(!instanceBuffer->isMapped());
530
531 currCmdBuf->bindInputBuffer(fGpu, binding++,
532 static_cast<const GrVkVertexBuffer*>(instanceBuffer));
533 }
Chris Daltonff926502017-05-03 14:36:54 -0400534 if (indexBuffer) {
535 SkASSERT(indexBuffer);
536 SkASSERT(!indexBuffer->isMapped());
egdaniel9cb63402016-06-23 08:37:05 -0700537
Chris Daltonff926502017-05-03 14:36:54 -0400538 currCmdBuf->bindIndexBuffer(fGpu, static_cast<const GrVkIndexBuffer*>(indexBuffer));
egdaniel9cb63402016-06-23 08:37:05 -0700539 }
540}
541
Greg Daniel2d41d0d2019-08-26 11:08:51 -0400542GrVkPipelineState* GrVkOpsRenderPass::prepareDrawState(
Brian Salomon49348902018-06-26 09:12:38 -0400543 const GrPrimitiveProcessor& primProc,
544 const GrPipeline& pipeline,
545 const GrPipeline::FixedDynamicState* fixedDynamicState,
546 const GrPipeline::DynamicStateArrays* dynamicStateArrays,
547 GrPrimitiveType primitiveType) {
Greg Daniel22bc8652017-03-22 15:45:43 -0400548 CommandBufferInfo& cbInfo = fCommandBufferInfos[fCurrentCmdInfo];
549 SkASSERT(cbInfo.fRenderPass);
Greg Daniel36a77ee2016-10-18 10:33:25 -0400550
Greg Daniel99b88e02018-10-03 15:31:20 -0400551 VkRenderPass compatibleRenderPass = cbInfo.fRenderPass->vkRenderPass();
552
Greg Daniel9a51a862018-11-30 10:18:14 -0500553 const GrTextureProxy* const* primProcProxies = nullptr;
554 if (dynamicStateArrays && dynamicStateArrays->fPrimitiveProcessorTextures) {
555 primProcProxies = dynamicStateArrays->fPrimitiveProcessorTextures;
556 } else if (fixedDynamicState) {
557 primProcProxies = fixedDynamicState->fPrimitiveProcessorTextures;
558 }
559
560 SkASSERT(SkToBool(primProcProxies) == SkToBool(primProc.numTextureSamplers()));
561
Greg Daniel09eeefb2017-10-16 15:15:02 -0400562 GrVkPipelineState* pipelineState =
Robert Phillipsd0fe8752019-01-31 14:13:59 -0500563 fGpu->resourceProvider().findOrCreateCompatiblePipelineState(fRenderTarget, fOrigin,
564 pipeline,
egdaniel9cb63402016-06-23 08:37:05 -0700565 primProc,
Greg Daniel9a51a862018-11-30 10:18:14 -0500566 primProcProxies,
egdaniel9cb63402016-06-23 08:37:05 -0700567 primitiveType,
Greg Daniel99b88e02018-10-03 15:31:20 -0400568 compatibleRenderPass);
egdaniel9cb63402016-06-23 08:37:05 -0700569 if (!pipelineState) {
570 return pipelineState;
571 }
572
Greg Daniel09eeefb2017-10-16 15:15:02 -0400573 fLastPipelineState = pipelineState;
Greg Daniel22bc8652017-03-22 15:45:43 -0400574
Brian Salomonf7232642018-09-19 08:58:08 -0400575 pipelineState->bindPipeline(fGpu, cbInfo.currentCmdBuf());
Brian Salomoncd7907b2018-08-30 08:36:18 -0400576
Robert Phillipsd0fe8752019-01-31 14:13:59 -0500577 pipelineState->setAndBindUniforms(fGpu, fRenderTarget, fOrigin,
578 primProc, pipeline, cbInfo.currentCmdBuf());
Brian Salomonf7232642018-09-19 08:58:08 -0400579
580 // Check whether we need to bind textures between each GrMesh. If not we can bind them all now.
581 bool setTextures = !(dynamicStateArrays && dynamicStateArrays->fPrimitiveProcessorTextures);
582 if (setTextures) {
Brian Salomonf7232642018-09-19 08:58:08 -0400583 pipelineState->setAndBindTextures(fGpu, primProc, pipeline, primProcProxies,
584 cbInfo.currentCmdBuf());
585 }
egdaniel9cb63402016-06-23 08:37:05 -0700586
Brian Salomond818ebf2018-07-02 14:08:49 +0000587 if (!pipeline.isScissorEnabled()) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400588 GrVkPipeline::SetDynamicScissorRectState(fGpu, cbInfo.currentCmdBuf(),
Robert Phillipsd0fe8752019-01-31 14:13:59 -0500589 fRenderTarget, fOrigin,
590 SkIRect::MakeWH(fRenderTarget->width(),
591 fRenderTarget->height()));
Brian Salomon49348902018-06-26 09:12:38 -0400592 } else if (!dynamicStateArrays || !dynamicStateArrays->fScissorRects) {
593 SkASSERT(fixedDynamicState);
Robert Phillipsd0fe8752019-01-31 14:13:59 -0500594 GrVkPipeline::SetDynamicScissorRectState(fGpu, cbInfo.currentCmdBuf(), fRenderTarget,
595 fOrigin,
Brian Salomon49348902018-06-26 09:12:38 -0400596 fixedDynamicState->fScissorRect);
Chris Dalton46983b72017-06-06 12:27:16 -0600597 }
Robert Phillipsd0fe8752019-01-31 14:13:59 -0500598 GrVkPipeline::SetDynamicViewportState(fGpu, cbInfo.currentCmdBuf(), fRenderTarget);
599 GrVkPipeline::SetDynamicBlendConstantState(fGpu, cbInfo.currentCmdBuf(),
Greg Daniel2c3398d2019-06-19 11:58:01 -0400600 pipeline.outputSwizzle(),
Chris Dalton46983b72017-06-06 12:27:16 -0600601 pipeline.getXferProcessor());
egdaniel9cb63402016-06-23 08:37:05 -0700602
603 return pipelineState;
604}
605
Greg Daniel2d41d0d2019-08-26 11:08:51 -0400606void GrVkOpsRenderPass::onDraw(const GrPrimitiveProcessor& primProc,
Brian Salomonff168d92018-06-23 15:17:27 -0400607 const GrPipeline& pipeline,
Brian Salomon49348902018-06-26 09:12:38 -0400608 const GrPipeline::FixedDynamicState* fixedDynamicState,
609 const GrPipeline::DynamicStateArrays* dynamicStateArrays,
Greg Daniel500d58b2017-08-24 15:59:33 -0400610 const GrMesh meshes[],
Greg Daniel500d58b2017-08-24 15:59:33 -0400611 int meshCount,
612 const SkRect& bounds) {
egdaniel9cb63402016-06-23 08:37:05 -0700613 if (!meshCount) {
614 return;
615 }
Greg Danielea022cd2018-03-16 11:10:03 -0400616
617 CommandBufferInfo& cbInfo = fCommandBufferInfos[fCurrentCmdInfo];
618
Brian Salomone782f842018-07-31 13:53:11 -0400619 auto prepareSampledImage = [&](GrTexture* texture, GrSamplerState::Filter filter) {
620 GrVkTexture* vkTexture = static_cast<GrVkTexture*>(texture);
621 // We may need to resolve the texture first if it is also a render target
622 GrVkRenderTarget* texRT = static_cast<GrVkRenderTarget*>(vkTexture->asRenderTarget());
Chris Dalton3d770272019-08-14 09:24:37 -0600623 if (texRT && texRT->needsResolve()) {
Greg Daniel0a77f432018-12-06 11:23:32 -0500624 fGpu->resolveRenderTargetNoFlush(texRT);
Chris Dalton3d770272019-08-14 09:24:37 -0600625 // TEMPORARY: MSAA resolve will have dirtied mipmaps. This goes away once we switch
Greg Danielf41b2bd2019-08-22 16:19:24 -0400626 // to resolving MSAA from the opsTask as well.
Chris Dalton3d770272019-08-14 09:24:37 -0600627 if (GrSamplerState::Filter::kMipMap == filter &&
628 (vkTexture->width() != 1 || vkTexture->height() != 1)) {
629 SkASSERT(vkTexture->texturePriv().mipMapped() == GrMipMapped::kYes);
630 SkASSERT(vkTexture->texturePriv().mipMapsAreDirty());
631 fGpu->regenerateMipMapLevels(vkTexture);
632 }
Brian Salomone782f842018-07-31 13:53:11 -0400633 }
634
Greg Danielf41b2bd2019-08-22 16:19:24 -0400635 // Ensure mip maps were all resolved ahead of time by the opsTask.
Brian Salomone782f842018-07-31 13:53:11 -0400636 if (GrSamplerState::Filter::kMipMap == filter &&
637 (vkTexture->width() != 1 || vkTexture->height() != 1)) {
638 SkASSERT(vkTexture->texturePriv().mipMapped() == GrMipMapped::kYes);
Chris Dalton3d770272019-08-14 09:24:37 -0600639 SkASSERT(!vkTexture->texturePriv().mipMapsAreDirty());
Brian Salomone782f842018-07-31 13:53:11 -0400640 }
Brian Salomone782f842018-07-31 13:53:11 -0400641 };
642
Brian Salomonf7232642018-09-19 08:58:08 -0400643 if (dynamicStateArrays && dynamicStateArrays->fPrimitiveProcessorTextures) {
644 for (int m = 0, i = 0; m < meshCount; ++m) {
645 for (int s = 0; s < primProc.numTextureSamplers(); ++s, ++i) {
646 auto texture = dynamicStateArrays->fPrimitiveProcessorTextures[i]->peekTexture();
647 prepareSampledImage(texture, primProc.textureSampler(s).samplerState().filter());
Chris Dalton0d6f7752019-08-19 12:21:27 -0600648 this->appendSampledTexture(texture);
Brian Salomonf7232642018-09-19 08:58:08 -0400649 }
650 }
651 } else {
652 for (int i = 0; i < primProc.numTextureSamplers(); ++i) {
653 auto texture = fixedDynamicState->fPrimitiveProcessorTextures[i]->peekTexture();
654 prepareSampledImage(texture, primProc.textureSampler(i).samplerState().filter());
Chris Dalton0d6f7752019-08-19 12:21:27 -0600655 this->appendSampledTexture(texture);
Brian Salomonf7232642018-09-19 08:58:08 -0400656 }
Brian Salomone782f842018-07-31 13:53:11 -0400657 }
bsalomonb58a2b42016-09-26 06:55:02 -0700658 GrFragmentProcessor::Iter iter(pipeline);
659 while (const GrFragmentProcessor* fp = iter.next()) {
Brian Salomone782f842018-07-31 13:53:11 -0400660 for (int i = 0; i < fp->numTextureSamplers(); ++i) {
661 const GrFragmentProcessor::TextureSampler& sampler = fp->textureSampler(i);
662 prepareSampledImage(sampler.peekTexture(), sampler.samplerState().filter());
Chris Dalton0d6f7752019-08-19 12:21:27 -0600663 this->appendSampledTexture(sampler.peekTexture());
Brian Salomone782f842018-07-31 13:53:11 -0400664 }
egdaniel2f5792a2016-07-06 08:51:23 -0700665 }
Robert Phillipsbb581ce2017-05-29 15:05:15 -0400666 if (GrTexture* dstTexture = pipeline.peekDstTexture()) {
Chris Dalton0d6f7752019-08-19 12:21:27 -0600667 this->appendSampledTexture(dstTexture);
Brian Salomon18dfa982017-04-03 16:57:43 -0400668 }
egdaniel2f5792a2016-07-06 08:51:23 -0700669
Chris Daltonbca46e22017-05-15 11:03:26 -0600670 GrPrimitiveType primitiveType = meshes[0].primitiveType();
Brian Salomon49348902018-06-26 09:12:38 -0400671 GrVkPipelineState* pipelineState = this->prepareDrawState(primProc, pipeline, fixedDynamicState,
672 dynamicStateArrays, primitiveType);
egdaniel9cb63402016-06-23 08:37:05 -0700673 if (!pipelineState) {
674 return;
675 }
676
Brian Salomond818ebf2018-07-02 14:08:49 +0000677 bool dynamicScissor =
678 pipeline.isScissorEnabled() && dynamicStateArrays && dynamicStateArrays->fScissorRects;
Brian Salomonf7232642018-09-19 08:58:08 -0400679 bool dynamicTextures = dynamicStateArrays && dynamicStateArrays->fPrimitiveProcessorTextures;
Brian Salomon49348902018-06-26 09:12:38 -0400680
egdaniel9cb63402016-06-23 08:37:05 -0700681 for (int i = 0; i < meshCount; ++i) {
682 const GrMesh& mesh = meshes[i];
Chris Daltonbca46e22017-05-15 11:03:26 -0600683 if (mesh.primitiveType() != primitiveType) {
Chris Dalton6f241802017-05-08 13:58:38 -0400684 SkDEBUGCODE(pipelineState = nullptr);
Chris Daltonbca46e22017-05-15 11:03:26 -0600685 primitiveType = mesh.primitiveType();
Brian Salomon49348902018-06-26 09:12:38 -0400686 pipelineState = this->prepareDrawState(primProc, pipeline, fixedDynamicState,
687 dynamicStateArrays, primitiveType);
Chris Dalton6f241802017-05-08 13:58:38 -0400688 if (!pipelineState) {
689 return;
egdaniel9cb63402016-06-23 08:37:05 -0700690 }
Chris Dalton6f241802017-05-08 13:58:38 -0400691 }
egdaniel9cb63402016-06-23 08:37:05 -0700692
Brian Salomon49348902018-06-26 09:12:38 -0400693 if (dynamicScissor) {
694 GrVkPipeline::SetDynamicScissorRectState(fGpu, cbInfo.currentCmdBuf(), fRenderTarget,
Robert Phillipsd0fe8752019-01-31 14:13:59 -0500695 fOrigin,
Brian Salomon49348902018-06-26 09:12:38 -0400696 dynamicStateArrays->fScissorRects[i]);
Chris Dalton46983b72017-06-06 12:27:16 -0600697 }
Brian Salomonf7232642018-09-19 08:58:08 -0400698 if (dynamicTextures) {
699 GrTextureProxy* const* meshProxies = dynamicStateArrays->fPrimitiveProcessorTextures +
700 primProc.numTextureSamplers() * i;
701 pipelineState->setAndBindTextures(fGpu, primProc, pipeline, meshProxies,
702 cbInfo.currentCmdBuf());
703 }
Chris Daltonbca46e22017-05-15 11:03:26 -0600704 SkASSERT(pipelineState);
Brian Salomon802cb312018-06-08 18:05:20 -0400705 mesh.sendToGpu(this);
egdaniel9cb63402016-06-23 08:37:05 -0700706 }
707
Greg Daniel36a77ee2016-10-18 10:33:25 -0400708 cbInfo.fBounds.join(bounds);
Chris Dalton114a3c02017-05-26 15:17:19 -0600709 cbInfo.fIsEmpty = false;
egdaniel066df7c2016-06-08 14:02:27 -0700710}
711
Greg Daniel2d41d0d2019-08-26 11:08:51 -0400712void GrVkOpsRenderPass::appendSampledTexture(GrTexture* tex) {
Chris Dalton0d6f7752019-08-19 12:21:27 -0600713 SkASSERT(!tex->isProtected() || (fRenderTarget->isProtected() && fGpu->protectedContext()));
Robert Phillipse1efd382019-08-21 10:07:10 -0400714 GrVkTexture* vkTex = static_cast<GrVkTexture*>(tex);
715
716 fCommandBufferInfos[fCurrentCmdInfo].fSampledTextures.push_back(sk_ref_sp(vkTex));
Chris Dalton0d6f7752019-08-19 12:21:27 -0600717}
718
Greg Daniel2d41d0d2019-08-26 11:08:51 -0400719void GrVkOpsRenderPass::sendInstancedMeshToGpu(GrPrimitiveType,
720 const GrBuffer* vertexBuffer,
721 int vertexCount,
722 int baseVertex,
723 const GrBuffer* instanceBuffer,
724 int instanceCount,
725 int baseInstance) {
Chris Dalton114a3c02017-05-26 15:17:19 -0600726 CommandBufferInfo& cbInfo = fCommandBufferInfos[fCurrentCmdInfo];
Brian Salomondbf70722019-02-07 11:31:24 -0500727 SkASSERT(!vertexBuffer || !vertexBuffer->isCpuBuffer());
728 SkASSERT(!instanceBuffer || !instanceBuffer->isCpuBuffer());
729 auto gpuVertexBuffer = static_cast<const GrGpuBuffer*>(vertexBuffer);
730 auto gpuInstanceBuffer = static_cast<const GrGpuBuffer*>(instanceBuffer);
731 this->bindGeometry(nullptr, gpuVertexBuffer, gpuInstanceBuffer);
Chris Dalton1d616352017-05-31 12:51:23 -0600732 cbInfo.currentCmdBuf()->draw(fGpu, vertexCount, instanceCount, baseVertex, baseInstance);
Chris Dalton114a3c02017-05-26 15:17:19 -0600733 fGpu->stats()->incNumDraws();
734}
735
Greg Daniel2d41d0d2019-08-26 11:08:51 -0400736void GrVkOpsRenderPass::sendIndexedInstancedMeshToGpu(GrPrimitiveType,
737 const GrBuffer* indexBuffer,
738 int indexCount,
739 int baseIndex,
740 const GrBuffer* vertexBuffer,
741 int baseVertex,
742 const GrBuffer* instanceBuffer,
743 int instanceCount,
744 int baseInstance,
745 GrPrimitiveRestart restart) {
Brian Salomon802cb312018-06-08 18:05:20 -0400746 SkASSERT(restart == GrPrimitiveRestart::kNo);
Chris Dalton114a3c02017-05-26 15:17:19 -0600747 CommandBufferInfo& cbInfo = fCommandBufferInfos[fCurrentCmdInfo];
Brian Salomondbf70722019-02-07 11:31:24 -0500748 SkASSERT(!vertexBuffer || !vertexBuffer->isCpuBuffer());
749 SkASSERT(!instanceBuffer || !instanceBuffer->isCpuBuffer());
750 SkASSERT(!indexBuffer->isCpuBuffer());
751 auto gpuIndexxBuffer = static_cast<const GrGpuBuffer*>(indexBuffer);
752 auto gpuVertexBuffer = static_cast<const GrGpuBuffer*>(vertexBuffer);
753 auto gpuInstanceBuffer = static_cast<const GrGpuBuffer*>(instanceBuffer);
754 this->bindGeometry(gpuIndexxBuffer, gpuVertexBuffer, gpuInstanceBuffer);
Chris Dalton1d616352017-05-31 12:51:23 -0600755 cbInfo.currentCmdBuf()->drawIndexed(fGpu, indexCount, instanceCount,
756 baseIndex, baseVertex, baseInstance);
Chris Dalton114a3c02017-05-26 15:17:19 -0600757 fGpu->stats()->incNumDraws();
758}
Greg Daniel64cc9aa2018-10-19 13:54:56 -0400759
760////////////////////////////////////////////////////////////////////////////////
761
Greg Daniel2d41d0d2019-08-26 11:08:51 -0400762void GrVkOpsRenderPass::executeDrawable(std::unique_ptr<SkDrawable::GpuDrawHandler> drawable) {
Greg Daniel64cc9aa2018-10-19 13:54:56 -0400763 GrVkRenderTarget* target = static_cast<GrVkRenderTarget*>(fRenderTarget);
764
765 GrVkImage* targetImage = target->msaaImage() ? target->msaaImage() : target;
766
767 CommandBufferInfo& cbInfo = fCommandBufferInfos[fCurrentCmdInfo];
768 VkRect2D bounds;
769 bounds.offset = { 0, 0 };
770 bounds.extent = { 0, 0 };
771
772 GrVkDrawableInfo vkInfo;
773 vkInfo.fSecondaryCommandBuffer = cbInfo.currentCmdBuf()->vkCommandBuffer();
774 vkInfo.fCompatibleRenderPass = cbInfo.fRenderPass->vkRenderPass();
Greg Danielb353eeb2018-12-05 11:01:58 -0500775 SkAssertResult(cbInfo.fRenderPass->colorAttachmentIndex(&vkInfo.fColorAttachmentIndex));
Greg Daniel64cc9aa2018-10-19 13:54:56 -0400776 vkInfo.fFormat = targetImage->imageFormat();
777 vkInfo.fDrawBounds = &bounds;
Stan Ilievcb580602019-02-26 11:36:07 -0500778#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
779 vkInfo.fImage = targetImage->image();
780#else
781 vkInfo.fImage = VK_NULL_HANDLE;
782#endif //SK_BUILD_FOR_ANDROID_FRAMEWORK
Greg Daniel64cc9aa2018-10-19 13:54:56 -0400783
784 GrBackendDrawableInfo info(vkInfo);
785
Eric Karlc0b2ba22019-01-22 19:40:35 -0800786 // After we draw into the command buffer via the drawable, cached state we have may be invalid.
787 cbInfo.currentCmdBuf()->invalidateState();
Eric Karla8878a12019-02-07 18:17:43 -0800788 // Also assume that the drawable produced output.
789 cbInfo.fIsEmpty = false;
Eric Karlc0b2ba22019-01-22 19:40:35 -0800790
Greg Daniel64cc9aa2018-10-19 13:54:56 -0400791 drawable->draw(info);
792 fGpu->addDrawable(std::move(drawable));
793
794 if (bounds.extent.width == 0 || bounds.extent.height == 0) {
795 cbInfo.fBounds.join(target->getBoundsRect());
796 } else {
797 cbInfo.fBounds.join(SkRect::MakeXYWH(bounds.offset.x, bounds.offset.y,
798 bounds.extent.width, bounds.extent.height));
799 }
800}
801