Debian Codegraph @

src/gpu/ganesh/vk/GrVkOpsRenderPass.cpp

raw ◀ ▶
Source reconstructed at patch state 1 of 2 — not the installed (fully patched) source.
2 Debian patches touch this file
1 /*
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
8 #include "src/gpu/ganesh/vk/GrVkOpsRenderPass.h"
9
10 #include "include/core/SkDrawable.h"
11 #include "include/core/SkRect.h"
12 #include "include/core/SkSize.h"
13 #include "include/gpu/ganesh/GrBackendSurface.h"
14 #include "include/gpu/ganesh/GrDirectContext.h"
15 #include "include/gpu/ganesh/GrTypes.h"
16 #include "include/gpu/ganesh/vk/GrBackendDrawableInfo.h"
17 #include "include/gpu/ganesh/vk/GrVkTypes.h"
18 #include "include/private/base/SkAssert.h"
19 #include "include/private/base/SkTo.h"
20 #include "include/private/gpu/ganesh/GrTypesPriv.h"
21 #include "src/gpu/GpuRefCnt.h"
22 #include "src/gpu/ganesh/GrAttachment.h"
23 #include "src/gpu/ganesh/GrBackendUtils.h"
24 #include "src/gpu/ganesh/GrBuffer.h"
25 #include "src/gpu/ganesh/GrCaps.h"
26 #include "src/gpu/ganesh/GrDirectContextPriv.h"
27 #include "src/gpu/ganesh/GrDrawIndirectCommand.h"
28 #include "src/gpu/ganesh/GrGpuBuffer.h"
29 #include "src/gpu/ganesh/GrNativeRect.h"
30 #include "src/gpu/ganesh/GrOpFlushState.h"
31 #include "src/gpu/ganesh/GrPipeline.h"
32 #include "src/gpu/ganesh/GrProgramInfo.h"
33 #include "src/gpu/ganesh/GrRenderTarget.h"
34 #include "src/gpu/ganesh/GrScissorState.h"
35 #include "src/gpu/ganesh/GrSurfaceProxy.h"
36 #include "src/gpu/ganesh/GrTexture.h"
37 #include "src/gpu/ganesh/effects/GrTextureEffect.h"
38 #include "src/gpu/ganesh/vk/GrVkCaps.h"
39 #include "src/gpu/ganesh/vk/GrVkCommandBuffer.h"
40 #include "src/gpu/ganesh/vk/GrVkCommandPool.h"
41 #include "src/gpu/ganesh/vk/GrVkDescriptorSet.h"
42 #include "src/gpu/ganesh/vk/GrVkFramebuffer.h"
43 #include "src/gpu/ganesh/vk/GrVkGpu.h"
44 #include "src/gpu/ganesh/vk/GrVkImage.h"
45 #include "src/gpu/ganesh/vk/GrVkPipeline.h"
46 #include "src/gpu/ganesh/vk/GrVkPipelineState.h"
47 #include "src/gpu/ganesh/vk/GrVkRenderPass.h"
48 #include "src/gpu/ganesh/vk/GrVkRenderTarget.h"
49 #include "src/gpu/ganesh/vk/GrVkResourceProvider.h"
50 #include "src/gpu/ganesh/vk/GrVkTexture.h"
51
52 #include <algorithm>
53 #include <cstring>
54 #include <functional>
55 #include <utility>
56
57 class GrGpu;
58
59 using namespace skia_private;
60
61 /////////////////////////////////////////////////////////////////////////////
62
63 static VkAttachmentLoadOp get_vk_load_op(GrLoadOp loadOpIn) {
64 switch (loadOpIn) {
65 case GrLoadOp::kLoad: return VK_ATTACHMENT_LOAD_OP_LOAD;
66 case GrLoadOp::kClear: return VK_ATTACHMENT_LOAD_OP_CLEAR;
67 case GrLoadOp::kDiscard: return VK_ATTACHMENT_LOAD_OP_DONT_CARE;
68 }
69 SkUNREACHABLE;
70 }
71
72 static VkAttachmentStoreOp get_vk_store_op(GrStoreOp storeOpIn) {
73 switch (storeOpIn) {
74 case GrStoreOp::kStore: return VK_ATTACHMENT_STORE_OP_STORE;
75 case GrStoreOp::kDiscard: return VK_ATTACHMENT_STORE_OP_DONT_CARE;
76 }
77 SkUNREACHABLE;
78 }
79
80 static void get_vk_load_store_ops(GrLoadOp loadOpIn, GrStoreOp storeOpIn,
81 VkAttachmentLoadOp* loadOp, VkAttachmentStoreOp* storeOp) {
82 *loadOp = get_vk_load_op(loadOpIn);
83 *storeOp = get_vk_store_op(storeOpIn);
84 }
85
86 GrVkOpsRenderPass::GrVkOpsRenderPass(GrVkGpu* gpu) : fGpu(gpu) {}
87
88 void GrVkOpsRenderPass::setAttachmentLayouts(LoadFromResolve loadFromResolve) {
89 bool withStencil = fCurrentRenderPass->hasStencilAttachment();
90 bool withResolve = fCurrentRenderPass->hasResolveAttachment();
91
92 if (fSelfDependencyFlags == SelfDependencyFlags::kForInputAttachment) {
93 // We need to use the GENERAL layout in this case since we'll be using texture barriers
94 // with an input attachment.
95 VkAccessFlags dstAccess = VK_ACCESS_INPUT_ATTACHMENT_READ_BIT |
96 VK_ACCESS_COLOR_ATTACHMENT_READ_BIT |
97 VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
98 VkPipelineStageFlags dstStages = VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT |
99 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
100 fFramebuffer->colorAttachment()->setImageLayout(
101 fGpu, VK_IMAGE_LAYOUT_GENERAL, dstAccess, dstStages, false);
102 } else {
103 // Change layout of our render target so it can be used as the color attachment.
104 // TODO: If we know that we will never be blending or loading the attachment we could drop
105 // the VK_ACCESS_COLOR_ATTACHMENT_READ_BIT.
106 fFramebuffer->colorAttachment()->setImageLayout(
107 fGpu,
108 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
109 VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
110 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
111 false);
112 }
113
114 if (withResolve) {
115 GrVkImage* resolveAttachment = fFramebuffer->resolveAttachment();
116 SkASSERT(resolveAttachment);
117 if (loadFromResolve == LoadFromResolve::kLoad) {
118 // We need input access to do the shader read and color read access to do the attachment
119 // load.
120 VkAccessFlags dstAccess =
121 VK_ACCESS_INPUT_ATTACHMENT_READ_BIT | VK_ACCESS_COLOR_ATTACHMENT_READ_BIT;
122 VkPipelineStageFlags dstStages = VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT |
123 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
124 resolveAttachment->setImageLayout(fGpu,
125 VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
126 dstAccess,
127 dstStages,
128 false);
129 } else {
130 resolveAttachment->setImageLayout(
131 fGpu,
132 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
133 VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
134 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
135 false);
136 }
137 }
138
139 // If we are using a stencil attachment we also need to update its layout
140 if (withStencil) {
141 auto* vkStencil = fFramebuffer->stencilAttachment();
142 SkASSERT(vkStencil);
143
144 // We need the write and read access bits since we may load and store the stencil.
145 // The initial load happens in the VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT so we
146 // wait there.
147 vkStencil->setImageLayout(fGpu,
148 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
149 VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT |
150 VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT,
151 VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT,
152 false);
153 }
154 }
155
156 // The RenderArea bounds we pass into BeginRenderPass must have a start x value that is a multiple
157 // of the granularity. The width must also be a multiple of the granularity or eaqual to the width
158 // the the entire attachment. Similar requirements for the y and height components.
159 void adjust_bounds_to_granularity(SkIRect* dstBounds,
160 const SkIRect& srcBounds,
161 const VkExtent2D& granularity,
162 int maxWidth,
163 int maxHeight) {
164 // Adjust Width
165 if ((0 != granularity.width && 1 != granularity.width)) {
166 // Start with the right side of rect so we know if we end up going pass the maxWidth.
167 int rightAdj = srcBounds.fRight % granularity.width;
168 if (rightAdj != 0) {
169 rightAdj = granularity.width - rightAdj;
170 }
171 dstBounds->fRight = srcBounds.fRight + rightAdj;
172 if (dstBounds->fRight > maxWidth) {
173 dstBounds->fRight = maxWidth;
174 dstBounds->fLeft = 0;
175 } else {
176 dstBounds->fLeft = srcBounds.fLeft - srcBounds.fLeft % granularity.width;
177 }
178 } else {
179 dstBounds->fLeft = srcBounds.fLeft;
180 dstBounds->fRight = srcBounds.fRight;
181 }
182
183 // Adjust height
184 if ((0 != granularity.height && 1 != granularity.height)) {
185 // Start with the bottom side of rect so we know if we end up going pass the maxHeight.
186 int bottomAdj = srcBounds.fBottom % granularity.height;
187 if (bottomAdj != 0) {
188 bottomAdj = granularity.height - bottomAdj;
189 }
190 dstBounds->fBottom = srcBounds.fBottom + bottomAdj;
191 if (dstBounds->fBottom > maxHeight) {
192 dstBounds->fBottom = maxHeight;
193 dstBounds->fTop = 0;
194 } else {
195 dstBounds->fTop = srcBounds.fTop - srcBounds.fTop % granularity.height;
196 }
197 } else {
198 dstBounds->fTop = srcBounds.fTop;
199 dstBounds->fBottom = srcBounds.fBottom;
200 }
201 }
202
203 bool GrVkOpsRenderPass::beginRenderPass(const VkClearValue& clearColor,
204 LoadFromResolve loadFromResolve) {
205 this->setAttachmentLayouts(loadFromResolve);
206
207 bool firstSubpassUsesSecondaryCB =
208 loadFromResolve != LoadFromResolve::kLoad && SkToBool(fCurrentSecondaryCommandBuffer);
209
210 bool useFullBounds = fCurrentRenderPass->hasResolveAttachment() &&
211 fGpu->vkCaps().mustLoadFullImageWithDiscardableMSAA();
212
213 auto dimensions = fFramebuffer->colorAttachment()->dimensions();
214
215 auto nativeBounds = GrNativeRect::MakeIRectRelativeTo(
216 fOrigin,
217 dimensions.height(), useFullBounds ? SkIRect::MakeSize(dimensions) : fBounds);
218
219 // The bounds we use for the render pass should be of the granularity supported
220 // by the device.
221 const VkExtent2D& granularity = fCurrentRenderPass->granularity();
222 SkIRect adjustedBounds;
223 if ((0 != granularity.width && 1 != granularity.width) ||
224 (0 != granularity.height && 1 != granularity.height)) {
225 adjust_bounds_to_granularity(&adjustedBounds,
226 nativeBounds,
227 granularity,
228 dimensions.width(),
229 dimensions.height());
230 } else {
231 adjustedBounds = nativeBounds;
232 }
233
234 if (!fGpu->beginRenderPass(fCurrentRenderPass, fFramebuffer, &clearColor, fRenderTarget,
235 adjustedBounds, firstSubpassUsesSecondaryCB)) {
236 if (fCurrentSecondaryCommandBuffer) {
237 fCurrentSecondaryCommandBuffer->end(fGpu);
238 }
239 fCurrentRenderPass = nullptr;
240 return false;
241 }
242
243 if (loadFromResolve == LoadFromResolve::kLoad) {
244 this->loadResolveIntoMSAA(adjustedBounds);
245 }
246
247 return true;
248 }
249
250 bool GrVkOpsRenderPass::init(const GrOpsRenderPass::LoadAndStoreInfo& colorInfo,
251 const GrOpsRenderPass::LoadAndStoreInfo& resolveInfo,
252 const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilInfo) {
253 VkAttachmentLoadOp loadOp;
254 VkAttachmentStoreOp storeOp;
255 get_vk_load_store_ops(colorInfo.fLoadOp, colorInfo.fStoreOp, &loadOp, &storeOp);
256 GrVkRenderPass::LoadStoreOps vkColorOps(loadOp, storeOp);
257
258 get_vk_load_store_ops(resolveInfo.fLoadOp, resolveInfo.fStoreOp, &loadOp, &storeOp);
259 GrVkRenderPass::LoadStoreOps vkResolveOps(loadOp, storeOp);
260
261 get_vk_load_store_ops(stencilInfo.fLoadOp, stencilInfo.fStoreOp, &loadOp, &storeOp);
262 GrVkRenderPass::LoadStoreOps vkStencilOps(loadOp, storeOp);
263
264 GrVkResourceProvider::CompatibleRPHandle rpHandle = fFramebuffer->compatibleRenderPassHandle();
265 SkASSERT(rpHandle.isValid());
266 fCurrentRenderPass = fGpu->resourceProvider().findRenderPass(rpHandle,
267 vkColorOps,
268 vkResolveOps,
269 vkStencilOps);
270
271 if (!fCurrentRenderPass) {
272 return false;
273 }
274
275 if (!fGpu->vkCaps().preferPrimaryOverSecondaryCommandBuffers()) {
276 SkASSERT(fGpu->cmdPool());
277 fCurrentSecondaryCommandBuffer = fGpu->cmdPool()->findOrCreateSecondaryCommandBuffer(fGpu);
278 if (!fCurrentSecondaryCommandBuffer) {
279 fCurrentRenderPass = nullptr;
280 return false;
281 }
282 fCurrentSecondaryCommandBuffer->begin(fGpu, fFramebuffer.get(), fCurrentRenderPass);
283 }
284
285 VkClearValue vkClearColor;
286 vkClearColor.color.float32[0] = colorInfo.fClearColor[0];
287 vkClearColor.color.float32[1] = colorInfo.fClearColor[1];
288 vkClearColor.color.float32[2] = colorInfo.fClearColor[2];
289 vkClearColor.color.float32[3] = colorInfo.fClearColor[3];
290
291 return this->beginRenderPass(vkClearColor, fLoadFromResolve);
292 }
293
294 bool GrVkOpsRenderPass::initWrapped() {
295 SkASSERT(fFramebuffer->isExternal());
296 fCurrentRenderPass = fFramebuffer->externalRenderPass();
297 SkASSERT(fCurrentRenderPass);
298 fCurrentRenderPass->ref();
299
300 fCurrentSecondaryCommandBuffer = fFramebuffer->externalCommandBuffer();
301 if (!fCurrentSecondaryCommandBuffer) {
302 return false;
303 }
304 return true;
305 }
306
307 GrVkOpsRenderPass::~GrVkOpsRenderPass() {
308 this->reset();
309 }
310
311 GrGpu* GrVkOpsRenderPass::gpu() { return fGpu; }
312
313 GrVkCommandBuffer* GrVkOpsRenderPass::currentCommandBuffer() {
314 if (fCurrentSecondaryCommandBuffer) {
315 return fCurrentSecondaryCommandBuffer.get();
316 }
317 // We checked this when we setup the GrVkOpsRenderPass and it should not have changed while we
318 // are still using this object.
319 SkASSERT(fGpu->currentCommandBuffer());
320 return fGpu->currentCommandBuffer();
321 }
322
323 void GrVkOpsRenderPass::loadResolveIntoMSAA(const SkIRect& nativeBounds) {
324 fGpu->loadMSAAFromResolve(this->currentCommandBuffer(), *fCurrentRenderPass,
325 fFramebuffer->colorAttachment(), fFramebuffer->resolveAttachment(),
326 nativeBounds);
327 fGpu->currentCommandBuffer()->nexSubpass(fGpu, SkToBool(fCurrentSecondaryCommandBuffer));
328
329 // If we loaded the resolve attachment, then we would have set the image layout to be
330 // VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL so that it could be used at the start as an input
331 // attachment. However, when we switched to the main subpass it will transition the layout
332 // internally to VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL. Thus we need to update our tracking
333 // of the layout to match the new layout.
334 SkASSERT(fFramebuffer->resolveAttachment());
335 fFramebuffer->resolveAttachment()->updateImageLayout(VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
336 }
337
338 void GrVkOpsRenderPass::submit() {
339 if (!fRenderTarget) {
340 return;
341 }
342 if (!fCurrentRenderPass) {
343 SkASSERT(fGpu->isDeviceLost());
344 return;
345 }
346
347 // We don't want to actually submit the secondary command buffer if it is wrapped.
348 if (this->wrapsSecondaryCommandBuffer()) {
349 // We pass the ownership of the GrVkSecondaryCommandBuffer to the external framebuffer
350 // since it's lifetime matches the lifetime we need to keep the GrManagedResources on the
351 // GrVkSecondaryCommandBuffer alive.
352 fFramebuffer->returnExternalGrSecondaryCommandBuffer(
353 std::move(fCurrentSecondaryCommandBuffer));
354 return;
355 }
356
357 if (fCurrentSecondaryCommandBuffer) {
358 fGpu->submitSecondaryCommandBuffer(std::move(fCurrentSecondaryCommandBuffer));
359 }
360 fGpu->endRenderPass(fRenderTarget, fOrigin, fBounds);
361 }
362
363 bool GrVkOpsRenderPass::set(GrRenderTarget* rt,
364 sk_sp<GrVkFramebuffer> framebuffer,
365 GrSurfaceOrigin origin,
366 const SkIRect& bounds,
367 const GrOpsRenderPass::LoadAndStoreInfo& colorInfo,
368 const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilInfo,
369 const GrOpsRenderPass::LoadAndStoreInfo& resolveInfo,
370 GrVkRenderPass::SelfDependencyFlags selfDepFlags,
371 GrVkRenderPass::LoadFromResolve loadFromResolve,
372 const TArray<GrSurfaceProxy*, true>& sampledProxies) {
373 SkASSERT(!fRenderTarget);
374 SkASSERT(fGpu == rt->getContext()->priv().getGpu());
375
376 #ifdef SK_DEBUG
377 fIsActive = true;
378 #endif
379
380 // We check to make sure the GrVkGpu has a valid current command buffer instead of each time we
381 // access it. If the command buffer is valid here should be valid throughout the use of the
382 // render pass since nothing should trigger a submit while this render pass is active.
383 if (!fGpu->currentCommandBuffer()) {
384 return false;
385 }
386
387 this->INHERITED::set(rt, origin);
388
389 for (int i = 0; i < sampledProxies.size(); ++i) {
390 if (sampledProxies[i]->isInstantiated()) {
391 SkASSERT(sampledProxies[i]->asTextureProxy());
392 GrVkTexture* vkTex = static_cast<GrVkTexture*>(sampledProxies[i]->peekTexture());
393 SkASSERT(vkTex);
394 GrVkImage* texture = vkTex->textureImage();
395 SkASSERT(texture);
396 texture->setImageLayout(
397 fGpu, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, VK_ACCESS_SHADER_READ_BIT,
398 VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT, false);
399 }
400 }
401
402 SkASSERT(framebuffer);
403 fFramebuffer = std::move(framebuffer);
404
405 SkASSERT(bounds.isEmpty() ||
406 SkIRect::MakeSize(fFramebuffer->colorAttachment()->dimensions()).contains(bounds));
407 fBounds = bounds;
408
409 fSelfDependencyFlags = selfDepFlags;
410 fLoadFromResolve = loadFromResolve;
411
412 if (this->wrapsSecondaryCommandBuffer()) {
413 return this->initWrapped();
414 }
415
416 return this->init(colorInfo, resolveInfo, stencilInfo);
417 }
418
419 void GrVkOpsRenderPass::reset() {
420 if (fCurrentSecondaryCommandBuffer) {
421 // The active GrVkCommandPool on the GrVkGpu should still be the same pool we got the
422 // secondary command buffer from since we haven't submitted any work yet.
423 SkASSERT(fGpu->cmdPool());
424 fCurrentSecondaryCommandBuffer.release()->recycle(fGpu->cmdPool());
425 }
426 if (fCurrentRenderPass) {
427 fCurrentRenderPass->unref();
428 fCurrentRenderPass = nullptr;
429 }
430 fCurrentCBIsEmpty = true;
431
432 fRenderTarget = nullptr;
433 fFramebuffer.reset();
434
435 fSelfDependencyFlags = GrVkRenderPass::SelfDependencyFlags::kNone;
436
437 fLoadFromResolve = LoadFromResolve::kNo;
438 fOverridePipelinesForResolveLoad = false;
439
440 #ifdef SK_DEBUG
441 fIsActive = false;
442 #endif
443 }
444
445 bool GrVkOpsRenderPass::wrapsSecondaryCommandBuffer() const {
446 return fFramebuffer->isExternal();
447 }
448
449 ////////////////////////////////////////////////////////////////////////////////
450
451 void GrVkOpsRenderPass::onClearStencilClip(const GrScissorState& scissor, bool insideStencilMask) {
452 if (!fCurrentRenderPass) {
453 SkASSERT(fGpu->isDeviceLost());
454 return;
455 }
456
457 GrAttachment* sb = fFramebuffer->stencilAttachment();
458 // this should only be called internally when we know we have a
459 // stencil buffer.
460 SkASSERT(sb);
461 int stencilBitCount = GrBackendFormatStencilBits(sb->backendFormat());
462
463 // The contract with the callers does not guarantee that we preserve all bits in the stencil
464 // during this clear. Thus we will clear the entire stencil to the desired value.
465
466 VkClearDepthStencilValue vkStencilColor;
467 memset(&vkStencilColor, 0, sizeof(VkClearDepthStencilValue));
468 if (insideStencilMask) {
469 vkStencilColor.stencil = (1 << (stencilBitCount - 1));
470 } else {
471 vkStencilColor.stencil = 0;
472 }
473
474 VkClearRect clearRect;
475 // Flip rect if necessary
476 SkIRect vkRect;
477 if (!scissor.enabled()) {
478 vkRect.setXYWH(0, 0, sb->width(), sb->height());
479 } else if (kBottomLeft_GrSurfaceOrigin != fOrigin) {
480 vkRect = scissor.rect();
481 } else {
482 vkRect.setLTRB(scissor.rect().fLeft, sb->height() - scissor.rect().fBottom,
483 scissor.rect().fRight, sb->height() - scissor.rect().fTop);
484 }
485
486 clearRect.rect.offset = { vkRect.fLeft, vkRect.fTop };
487 clearRect.rect.extent = { (uint32_t)vkRect.width(), (uint32_t)vkRect.height() };
488
489 clearRect.baseArrayLayer = 0;
490 clearRect.layerCount = 1;
491
492 uint32_t stencilIndex;
493 SkAssertResult(fCurrentRenderPass->stencilAttachmentIndex(&stencilIndex));
494
495 VkClearAttachment attachment;
496 attachment.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
497 attachment.colorAttachment = 0; // this value shouldn't matter
498 attachment.clearValue.depthStencil = vkStencilColor;
499
500 this->currentCommandBuffer()->clearAttachments(fGpu, 1, &attachment, 1, &clearRect);
501 fCurrentCBIsEmpty = false;
502 }
503
504 void GrVkOpsRenderPass::onClear(const GrScissorState& scissor, std::array<float, 4> color) {
505 if (!fCurrentRenderPass) {
506 SkASSERT(fGpu->isDeviceLost());
507 return;
508 }
509
510 VkClearColorValue vkColor = {{color[0], color[1], color[2], color[3]}};
511
512 // If we end up in a situation where we are calling clear without a scissior then in general it
513 // means we missed an opportunity higher up the stack to set the load op to be a clear. However,
514 // there are situations where higher up we couldn't discard the previous ops and set a clear
515 // load op (e.g. if we needed to execute a wait op). Thus we also have the empty check here.
516 // TODO: Make the waitOp a RenderTask instead so we can clear out the OpsTask for a clear. We
517 // can then reenable this assert assuming we can't get messed up by a waitOp.
518 //SkASSERT(!fCurrentCBIsEmpty || scissor);
519
520 auto dimensions = fFramebuffer->colorAttachment()->dimensions();
521 // We always do a sub rect clear with clearAttachments since we are inside a render pass
522 VkClearRect clearRect;
523 // Flip rect if necessary
524 SkIRect vkRect;
525 if (!scissor.enabled()) {
526 vkRect.setSize(dimensions);
527 } else if (kBottomLeft_GrSurfaceOrigin != fOrigin) {
528 vkRect = scissor.rect();
529 } else {
530 vkRect.setLTRB(scissor.rect().fLeft, dimensions.height() - scissor.rect().fBottom,
531 scissor.rect().fRight, dimensions.height() - scissor.rect().fTop);
532 }
533 clearRect.rect.offset = { vkRect.fLeft, vkRect.fTop };
534 clearRect.rect.extent = { (uint32_t)vkRect.width(), (uint32_t)vkRect.height() };
535 clearRect.baseArrayLayer = 0;
536 clearRect.layerCount = 1;
537
538 uint32_t colorIndex;
539 SkAssertResult(fCurrentRenderPass->colorAttachmentIndex(&colorIndex));
540
541 VkClearAttachment attachment;
542 attachment.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
543 attachment.colorAttachment = colorIndex;
544 attachment.clearValue.color = vkColor;
545
546 this->currentCommandBuffer()->clearAttachments(fGpu, 1, &attachment, 1, &clearRect);
547 fCurrentCBIsEmpty = false;
548 }
549
550 ////////////////////////////////////////////////////////////////////////////////
551
552 void GrVkOpsRenderPass::addAdditionalRenderPass(bool mustUseSecondaryCommandBuffer) {
553 SkASSERT(!this->wrapsSecondaryCommandBuffer());
554
555 bool withResolve = fFramebuffer->resolveAttachment();
556 bool withStencil = fFramebuffer->stencilAttachment();
557
558 // If we have a resolve attachment we must do a resolve load in the new render pass since we
559 // broke up the original one. GrProgramInfos were made without any knowledge that the render
560 // pass may be split up. Thus they may try to make VkPipelines that only use one subpass. We
561 // need to override that to make sure they are compatible with the extra load subpass.
562 fOverridePipelinesForResolveLoad |=
563 withResolve && fCurrentRenderPass->loadFromResolve() != LoadFromResolve::kLoad;
564
565 GrVkRenderPass::LoadStoreOps vkColorOps(VK_ATTACHMENT_LOAD_OP_LOAD,
566 VK_ATTACHMENT_STORE_OP_STORE);
567 GrVkRenderPass::LoadStoreOps vkResolveOps(VK_ATTACHMENT_LOAD_OP_LOAD,
568 VK_ATTACHMENT_STORE_OP_STORE);
569 LoadFromResolve loadFromResolve = LoadFromResolve::kNo;
570 if (withResolve) {
571 vkColorOps = {VK_ATTACHMENT_LOAD_OP_DONT_CARE, VK_ATTACHMENT_STORE_OP_DONT_CARE};
572 loadFromResolve = LoadFromResolve::kLoad;
573 }
574 GrVkRenderPass::LoadStoreOps vkStencilOps(VK_ATTACHMENT_LOAD_OP_LOAD,
575 VK_ATTACHMENT_STORE_OP_STORE);
576
577 SkASSERT(fCurrentRenderPass);
578 fCurrentRenderPass->unref();
579 fCurrentRenderPass = nullptr;
580
581 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(fRenderTarget);
582 auto fb = vkRT->getFramebuffer(withResolve, withStencil, fSelfDependencyFlags, loadFromResolve);
583 if (!fb) {
584 return;
585 }
586 fFramebuffer = sk_ref_sp(fb);
587
588 SkASSERT(fFramebuffer);
589 const GrVkResourceProvider::CompatibleRPHandle& rpHandle =
590 fFramebuffer->compatibleRenderPassHandle();
591 SkASSERT(rpHandle.isValid());
592
593 fCurrentRenderPass = fGpu->resourceProvider().findRenderPass(rpHandle,
594 vkColorOps,
595 vkResolveOps,
596 vkStencilOps);
597
598 if (!fCurrentRenderPass) {
599 return;
600 }
601
602 if (!fGpu->vkCaps().preferPrimaryOverSecondaryCommandBuffers() ||
603 mustUseSecondaryCommandBuffer) {
604 SkASSERT(fGpu->cmdPool());
605 fCurrentSecondaryCommandBuffer = fGpu->cmdPool()->findOrCreateSecondaryCommandBuffer(fGpu);
606 if (!fCurrentSecondaryCommandBuffer) {
607 fCurrentRenderPass = nullptr;
608 return;
609 }
610 fCurrentSecondaryCommandBuffer->begin(fGpu, fFramebuffer.get(), fCurrentRenderPass);
611 }
612
613 VkClearValue vkClearColor;
614 memset(&vkClearColor, 0, sizeof(VkClearValue));
615
616 this->beginRenderPass(vkClearColor, loadFromResolve);
617 }
618
619 bool GrVkOpsRenderPass::inlineUpload(GrOpFlushState* state, GrDeferredTextureUploadFn& upload) {
620 if (!fCurrentRenderPass) {
621 SkASSERT(fGpu->isDeviceLost());
622 return false;
623 }
624
625 // Skia should not manipulate a wrapped (externally-owned) secondary command buffer, which
626 // is necessary in order to perform the requested inline upload.
627 if (this->wrapsSecondaryCommandBuffer()) {
628 return false;
629 }
630
631 if (fCurrentSecondaryCommandBuffer) {
632 fCurrentSecondaryCommandBuffer->end(fGpu);
633 fGpu->submitSecondaryCommandBuffer(std::move(fCurrentSecondaryCommandBuffer));
634 }
635 fGpu->endRenderPass(fRenderTarget, fOrigin, fBounds);
636
637 // We pass in true here to signal that after the upload we need to set the upload textures
638 // layout back to VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL.
639 state->doUpload(upload, /*shouldPrepareSurfaceForSampling=*/true);
640
641 this->addAdditionalRenderPass(false);
642
643 return true;
644 }
645
646 ////////////////////////////////////////////////////////////////////////////////
647
648 void GrVkOpsRenderPass::onEnd() {
649 if (fCurrentSecondaryCommandBuffer && !this->wrapsSecondaryCommandBuffer()) {
650 fCurrentSecondaryCommandBuffer->end(fGpu);
651 }
652 }
653
654 bool GrVkOpsRenderPass::onBindPipeline(const GrProgramInfo& programInfo, const SkRect& drawBounds) {
655 if (!fCurrentRenderPass) {
656 SkASSERT(fGpu->isDeviceLost());
657 return false;
658 }
659
660 SkRect rtRect = SkRect::Make(fBounds);
661 if (rtRect.intersect(drawBounds)) {
662 rtRect.roundOut(&fCurrentPipelineBounds);
663 } else {
664 fCurrentPipelineBounds.setEmpty();
665 }
666
667 GrVkCommandBuffer* currentCB = this->currentCommandBuffer();
668 SkASSERT(fCurrentRenderPass);
669
670 VkRenderPass compatibleRenderPass = fCurrentRenderPass->vkRenderPass();
671 fCurrentPipelineState = fGpu->resourceProvider().findOrCreateCompatiblePipelineState(
672 fRenderTarget, programInfo, compatibleRenderPass, fOverridePipelinesForResolveLoad);
673 if (!fCurrentPipelineState) {
674 return false;
675 }
676
677 fCurrentPipelineState->bindPipeline(fGpu, currentCB);
678
679 // Both the 'programInfo' and this renderPass have an origin. Since they come from the
680 // same place (i.e., the target renderTargetProxy) they had best agree.
681 SkASSERT(programInfo.origin() == fOrigin);
682
683 auto colorAttachment = fFramebuffer->colorAttachment();
684 if (!fCurrentPipelineState->setAndBindUniforms(fGpu, colorAttachment->dimensions(), programInfo,
685 currentCB)) {
686 return false;
687 }
688
689 if (!programInfo.pipeline().isScissorTestEnabled()) {
690 // "Disable" scissor by setting it to the full pipeline bounds.
691 GrVkPipeline::SetDynamicScissorRectState(
692 fGpu, currentCB, colorAttachment->dimensions(), fOrigin,
693 fCurrentPipelineBounds);
694 }
695 GrVkPipeline::SetDynamicViewportState(fGpu, currentCB, colorAttachment->dimensions());
696 GrVkPipeline::SetDynamicBlendConstantState(fGpu, currentCB,
697 programInfo.pipeline().writeSwizzle(),
698 programInfo.pipeline().getXferProcessor());
699
700 return true;
701 }
702
703 void GrVkOpsRenderPass::onSetScissorRect(const SkIRect& scissor) {
704 SkIRect combinedScissorRect;
705 if (!combinedScissorRect.intersect(fCurrentPipelineBounds, scissor)) {
706 combinedScissorRect = SkIRect::MakeEmpty();
707 }
708 GrVkPipeline::SetDynamicScissorRectState(fGpu, this->currentCommandBuffer(),
709 fFramebuffer->colorAttachment()->dimensions(),
710 fOrigin, combinedScissorRect);
711 }
712
713 #ifdef SK_DEBUG
714 void check_sampled_texture(GrTexture* tex, GrAttachment* colorAttachment, GrVkGpu* gpu) {
715 SkASSERT(!tex->isProtected() || (colorAttachment->isProtected() && gpu->protectedContext()));
716 auto vkTex = static_cast<GrVkTexture*>(tex)->textureImage();
717 SkASSERT(vkTex->currentLayout() == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
718 }
719 #endif
720
721 bool GrVkOpsRenderPass::onBindTextures(const GrGeometryProcessor& geomProc,
722 const GrSurfaceProxy* const geomProcTextures[],
723 const GrPipeline& pipeline) {
724 #ifdef SK_DEBUG
725 SkASSERT(fCurrentPipelineState);
726 auto colorAttachment = fFramebuffer->colorAttachment();
727 for (int i = 0; i < geomProc.numTextureSamplers(); ++i) {
728 check_sampled_texture(geomProcTextures[i]->peekTexture(), colorAttachment, fGpu);
729 }
730 pipeline.visitTextureEffects([&](const GrTextureEffect& te) {
731 check_sampled_texture(te.texture(), colorAttachment, fGpu);
732 });
733 if (GrTexture* dstTexture = pipeline.peekDstTexture()) {
734 check_sampled_texture(dstTexture, colorAttachment, fGpu);
735 }
736 #endif
737 if (!fCurrentPipelineState->setAndBindTextures(fGpu, geomProc, pipeline, geomProcTextures,
738 this->currentCommandBuffer())) {
739 return false;
740 }
741 if (fSelfDependencyFlags == SelfDependencyFlags::kForInputAttachment) {
742 // We bind the color attachment as an input attachment
743 auto ds = fFramebuffer->colorAttachment()->inputDescSetForBlending(fGpu);
744 if (!ds) {
745 return false;
746 }
747 return fCurrentPipelineState->setAndBindInputAttachment(fGpu, std::move(ds),
748 this->currentCommandBuffer());
749 }
750 return true;
751 }
752
753 void GrVkOpsRenderPass::onBindBuffers(sk_sp<const GrBuffer> indexBuffer,
754 sk_sp<const GrBuffer> instanceBuffer,
755 sk_sp<const GrBuffer> vertexBuffer,
756 GrPrimitiveRestart primRestart) {
757 SkASSERT(GrPrimitiveRestart::kNo == primRestart);
758 if (!fCurrentRenderPass) {
759 SkASSERT(fGpu->isDeviceLost());
760 return;
761 }
762 SkASSERT(fCurrentPipelineState);
763 SkASSERT(!fGpu->caps()->usePrimitiveRestart()); // Ignore primitiveRestart parameter.
764
765 GrVkCommandBuffer* currCmdBuf = this->currentCommandBuffer();
766 SkASSERT(currCmdBuf);
767
768 // There is no need to put any memory barriers to make sure host writes have finished here.
769 // When a command buffer is submitted to a queue, there is an implicit memory barrier that
770 // occurs for all host writes. Additionally, BufferMemoryBarriers are not allowed inside of
771 // an active RenderPass.
772
773 // Here our vertex and instance inputs need to match the same 0-based bindings they were
774 // assigned in GrVkPipeline. That is, vertex first (if any) followed by instance.
775 uint32_t binding = 0;
776 if (vertexBuffer) {
777 SkDEBUGCODE(auto* gpuVertexBuffer = static_cast<const GrGpuBuffer*>(vertexBuffer.get()));
778 SkASSERT(!gpuVertexBuffer->isCpuBuffer());
779 SkASSERT(!gpuVertexBuffer->isMapped());
780 currCmdBuf->bindInputBuffer(fGpu, binding++, std::move(vertexBuffer));
781 }
782 if (instanceBuffer) {
783 SkDEBUGCODE(auto* gpuInstanceBuffer =
784 static_cast<const GrGpuBuffer*>(instanceBuffer.get()));
785 SkASSERT(!gpuInstanceBuffer->isCpuBuffer());
786 SkASSERT(!gpuInstanceBuffer->isMapped());
787 currCmdBuf->bindInputBuffer(fGpu, binding++, std::move(instanceBuffer));
788 }
789 if (indexBuffer) {
790 SkDEBUGCODE(auto* gpuIndexBuffer = static_cast<const GrGpuBuffer*>(indexBuffer.get()));
791 SkASSERT(!gpuIndexBuffer->isCpuBuffer());
792 SkASSERT(!gpuIndexBuffer->isMapped());
793 currCmdBuf->bindIndexBuffer(fGpu, std::move(indexBuffer));
794 }
795 }
796
797 void GrVkOpsRenderPass::onDrawInstanced(int instanceCount,
798 int baseInstance,
799 int vertexCount, int baseVertex) {
800 if (!fCurrentRenderPass) {
801 SkASSERT(fGpu->isDeviceLost());
802 return;
803 }
804 SkASSERT(fCurrentPipelineState);
805 this->currentCommandBuffer()->draw(fGpu, vertexCount, instanceCount, baseVertex, baseInstance);
806 fGpu->stats()->incNumDraws();
807 fCurrentCBIsEmpty = false;
808 }
809
810 void GrVkOpsRenderPass::onDrawIndexedInstanced(int indexCount, int baseIndex, int instanceCount,
811 int baseInstance, int baseVertex) {
812 if (!fCurrentRenderPass) {
813 SkASSERT(fGpu->isDeviceLost());
814 return;
815 }
816 SkASSERT(fCurrentPipelineState);
817 this->currentCommandBuffer()->drawIndexed(fGpu, indexCount, instanceCount,
818 baseIndex, baseVertex, baseInstance);
819 fGpu->stats()->incNumDraws();
820 fCurrentCBIsEmpty = false;
821 }
822
823 void GrVkOpsRenderPass::onDrawIndirect(const GrBuffer* drawIndirectBuffer, size_t offset,
824 int drawCount) {
825 SkASSERT(!drawIndirectBuffer->isCpuBuffer());
826 if (!fCurrentRenderPass) {
827 SkASSERT(fGpu->isDeviceLost());
828 return;
829 }
830 const GrVkCaps& caps = fGpu->vkCaps();
831 SkASSERT(caps.nativeDrawIndirectSupport());
832 SkASSERT(fCurrentPipelineState);
833
834 const uint32_t maxDrawCount = caps.maxDrawIndirectDrawCount();
835 uint32_t remainingDraws = drawCount;
836 const size_t stride = sizeof(GrDrawIndirectCommand);
837 while (remainingDraws >= 1) {
838 uint32_t currDrawCount = std::min(remainingDraws, maxDrawCount);
839 this->currentCommandBuffer()->drawIndirect(
840 fGpu, sk_ref_sp(drawIndirectBuffer), offset, currDrawCount, stride);
841 remainingDraws -= currDrawCount;
842 offset += stride * currDrawCount;
843 fGpu->stats()->incNumDraws();
844 }
845 fCurrentCBIsEmpty = false;
846 }
847
848 void GrVkOpsRenderPass::onDrawIndexedIndirect(const GrBuffer* drawIndirectBuffer, size_t offset,
849 int drawCount) {
850 SkASSERT(!drawIndirectBuffer->isCpuBuffer());
851 if (!fCurrentRenderPass) {
852 SkASSERT(fGpu->isDeviceLost());
853 return;
854 }
855 const GrVkCaps& caps = fGpu->vkCaps();
856 SkASSERT(caps.nativeDrawIndirectSupport());
857 SkASSERT(fCurrentPipelineState);
858 const uint32_t maxDrawCount = caps.maxDrawIndirectDrawCount();
859 uint32_t remainingDraws = drawCount;
860 const size_t stride = sizeof(GrDrawIndexedIndirectCommand);
861 while (remainingDraws >= 1) {
862 uint32_t currDrawCount = std::min(remainingDraws, maxDrawCount);
863 this->currentCommandBuffer()->drawIndexedIndirect(
864 fGpu, sk_ref_sp(drawIndirectBuffer), offset, currDrawCount, stride);
865 remainingDraws -= currDrawCount;
866 offset += stride * currDrawCount;
867 fGpu->stats()->incNumDraws();
868 }
869 fCurrentCBIsEmpty = false;
870 }
871
872 ////////////////////////////////////////////////////////////////////////////////
873
874 void GrVkOpsRenderPass::onExecuteDrawable(std::unique_ptr<SkDrawable::GpuDrawHandler> drawable) {
875 if (!fCurrentRenderPass) {
876 SkASSERT(fGpu->isDeviceLost());
877 return;
878 }
879
880 VkRect2D bounds;
881 bounds.offset = { 0, 0 };
882 bounds.extent = { 0, 0 };
883
884 if (!fCurrentSecondaryCommandBuffer) {
885 fGpu->endRenderPass(fRenderTarget, fOrigin, fBounds);
886 this->addAdditionalRenderPass(true);
887 // We may have failed to start a new render pass
888 if (!fCurrentRenderPass) {
889 SkASSERT(fGpu->isDeviceLost());
890 return;
891 }
892 }
893 SkASSERT(fCurrentSecondaryCommandBuffer);
894
895 GrVkDrawableInfo vkInfo;
896 vkInfo.fSecondaryCommandBuffer = fCurrentSecondaryCommandBuffer->vkCommandBuffer();
897 vkInfo.fCompatibleRenderPass = fCurrentRenderPass->vkRenderPass();
898 SkAssertResult(fCurrentRenderPass->colorAttachmentIndex(&vkInfo.fColorAttachmentIndex));
899 vkInfo.fFormat = fFramebuffer->colorAttachment()->imageFormat();
900 vkInfo.fDrawBounds = &bounds;
901 #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
902 vkInfo.fFromSwapchainOrAndroidWindow =
903 fFramebuffer->colorAttachment()->vkImageInfo().fPartOfSwapchainOrAndroidWindow;
904 #endif //SK_BUILD_FOR_ANDROID_FRAMEWORK
905
906 GrBackendDrawableInfo info(vkInfo);
907
908 // After we draw into the command buffer via the drawable, cached state we have may be invalid.
909 this->currentCommandBuffer()->invalidateState();
910 // Also assume that the drawable produced output.
911 fCurrentCBIsEmpty = false;
912
913 drawable->draw(info);
914 fGpu->addDrawable(std::move(drawable));
915 }