Debian Codegraph @

src/gpu/ganesh/vk/GrVkOpsRenderPass.cpp

raw ◀ ▶
Source reconstructed at patch state 0 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 void GrVkOpsRenderPass::inlineUpload(GrOpFlushState* state, GrDeferredTextureUploadFn& upload) {
620 if (!fCurrentRenderPass) {
621 SkASSERT(fGpu->isDeviceLost());
622 return;
623 }
624 if (fCurrentSecondaryCommandBuffer) {
625 fCurrentSecondaryCommandBuffer->end(fGpu);
626 fGpu->submitSecondaryCommandBuffer(std::move(fCurrentSecondaryCommandBuffer));
627 }
628 fGpu->endRenderPass(fRenderTarget, fOrigin, fBounds);
629
630 // We pass in true here to signal that after the upload we need to set the upload textures
631 // layout back to VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL.
632 state->doUpload(upload, true);
633
634 this->addAdditionalRenderPass(false);
635 }
636
637 ////////////////////////////////////////////////////////////////////////////////
638
639 void GrVkOpsRenderPass::onEnd() {
640 if (fCurrentSecondaryCommandBuffer && !this->wrapsSecondaryCommandBuffer()) {
641 fCurrentSecondaryCommandBuffer->end(fGpu);
642 }
643 }
644
645 bool GrVkOpsRenderPass::onBindPipeline(const GrProgramInfo& programInfo, const SkRect& drawBounds) {
646 if (!fCurrentRenderPass) {
647 SkASSERT(fGpu->isDeviceLost());
648 return false;
649 }
650
651 SkRect rtRect = SkRect::Make(fBounds);
652 if (rtRect.intersect(drawBounds)) {
653 rtRect.roundOut(&fCurrentPipelineBounds);
654 } else {
655 fCurrentPipelineBounds.setEmpty();
656 }
657
658 GrVkCommandBuffer* currentCB = this->currentCommandBuffer();
659 SkASSERT(fCurrentRenderPass);
660
661 VkRenderPass compatibleRenderPass = fCurrentRenderPass->vkRenderPass();
662 fCurrentPipelineState = fGpu->resourceProvider().findOrCreateCompatiblePipelineState(
663 fRenderTarget, programInfo, compatibleRenderPass, fOverridePipelinesForResolveLoad);
664 if (!fCurrentPipelineState) {
665 return false;
666 }
667
668 fCurrentPipelineState->bindPipeline(fGpu, currentCB);
669
670 // Both the 'programInfo' and this renderPass have an origin. Since they come from the
671 // same place (i.e., the target renderTargetProxy) they had best agree.
672 SkASSERT(programInfo.origin() == fOrigin);
673
674 auto colorAttachment = fFramebuffer->colorAttachment();
675 if (!fCurrentPipelineState->setAndBindUniforms(fGpu, colorAttachment->dimensions(), programInfo,
676 currentCB)) {
677 return false;
678 }
679
680 if (!programInfo.pipeline().isScissorTestEnabled()) {
681 // "Disable" scissor by setting it to the full pipeline bounds.
682 GrVkPipeline::SetDynamicScissorRectState(
683 fGpu, currentCB, colorAttachment->dimensions(), fOrigin,
684 fCurrentPipelineBounds);
685 }
686 GrVkPipeline::SetDynamicViewportState(fGpu, currentCB, colorAttachment->dimensions());
687 GrVkPipeline::SetDynamicBlendConstantState(fGpu, currentCB,
688 programInfo.pipeline().writeSwizzle(),
689 programInfo.pipeline().getXferProcessor());
690
691 return true;
692 }
693
694 void GrVkOpsRenderPass::onSetScissorRect(const SkIRect& scissor) {
695 SkIRect combinedScissorRect;
696 if (!combinedScissorRect.intersect(fCurrentPipelineBounds, scissor)) {
697 combinedScissorRect = SkIRect::MakeEmpty();
698 }
699 GrVkPipeline::SetDynamicScissorRectState(fGpu, this->currentCommandBuffer(),
700 fFramebuffer->colorAttachment()->dimensions(),
701 fOrigin, combinedScissorRect);
702 }
703
704 #ifdef SK_DEBUG
705 void check_sampled_texture(GrTexture* tex, GrAttachment* colorAttachment, GrVkGpu* gpu) {
706 SkASSERT(!tex->isProtected() || (colorAttachment->isProtected() && gpu->protectedContext()));
707 auto vkTex = static_cast<GrVkTexture*>(tex)->textureImage();
708 SkASSERT(vkTex->currentLayout() == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
709 }
710 #endif
711
712 bool GrVkOpsRenderPass::onBindTextures(const GrGeometryProcessor& geomProc,
713 const GrSurfaceProxy* const geomProcTextures[],
714 const GrPipeline& pipeline) {
715 #ifdef SK_DEBUG
716 SkASSERT(fCurrentPipelineState);
717 auto colorAttachment = fFramebuffer->colorAttachment();
718 for (int i = 0; i < geomProc.numTextureSamplers(); ++i) {
719 check_sampled_texture(geomProcTextures[i]->peekTexture(), colorAttachment, fGpu);
720 }
721 pipeline.visitTextureEffects([&](const GrTextureEffect& te) {
722 check_sampled_texture(te.texture(), colorAttachment, fGpu);
723 });
724 if (GrTexture* dstTexture = pipeline.peekDstTexture()) {
725 check_sampled_texture(dstTexture, colorAttachment, fGpu);
726 }
727 #endif
728 if (!fCurrentPipelineState->setAndBindTextures(fGpu, geomProc, pipeline, geomProcTextures,
729 this->currentCommandBuffer())) {
730 return false;
731 }
732 if (fSelfDependencyFlags == SelfDependencyFlags::kForInputAttachment) {
733 // We bind the color attachment as an input attachment
734 auto ds = fFramebuffer->colorAttachment()->inputDescSetForBlending(fGpu);
735 if (!ds) {
736 return false;
737 }
738 return fCurrentPipelineState->setAndBindInputAttachment(fGpu, std::move(ds),
739 this->currentCommandBuffer());
740 }
741 return true;
742 }
743
744 void GrVkOpsRenderPass::onBindBuffers(sk_sp<const GrBuffer> indexBuffer,
745 sk_sp<const GrBuffer> instanceBuffer,
746 sk_sp<const GrBuffer> vertexBuffer,
747 GrPrimitiveRestart primRestart) {
748 SkASSERT(GrPrimitiveRestart::kNo == primRestart);
749 if (!fCurrentRenderPass) {
750 SkASSERT(fGpu->isDeviceLost());
751 return;
752 }
753 SkASSERT(fCurrentPipelineState);
754 SkASSERT(!fGpu->caps()->usePrimitiveRestart()); // Ignore primitiveRestart parameter.
755
756 GrVkCommandBuffer* currCmdBuf = this->currentCommandBuffer();
757 SkASSERT(currCmdBuf);
758
759 // There is no need to put any memory barriers to make sure host writes have finished here.
760 // When a command buffer is submitted to a queue, there is an implicit memory barrier that
761 // occurs for all host writes. Additionally, BufferMemoryBarriers are not allowed inside of
762 // an active RenderPass.
763
764 // Here our vertex and instance inputs need to match the same 0-based bindings they were
765 // assigned in GrVkPipeline. That is, vertex first (if any) followed by instance.
766 uint32_t binding = 0;
767 if (vertexBuffer) {
768 SkDEBUGCODE(auto* gpuVertexBuffer = static_cast<const GrGpuBuffer*>(vertexBuffer.get()));
769 SkASSERT(!gpuVertexBuffer->isCpuBuffer());
770 SkASSERT(!gpuVertexBuffer->isMapped());
771 currCmdBuf->bindInputBuffer(fGpu, binding++, std::move(vertexBuffer));
772 }
773 if (instanceBuffer) {
774 SkDEBUGCODE(auto* gpuInstanceBuffer =
775 static_cast<const GrGpuBuffer*>(instanceBuffer.get()));
776 SkASSERT(!gpuInstanceBuffer->isCpuBuffer());
777 SkASSERT(!gpuInstanceBuffer->isMapped());
778 currCmdBuf->bindInputBuffer(fGpu, binding++, std::move(instanceBuffer));
779 }
780 if (indexBuffer) {
781 SkDEBUGCODE(auto* gpuIndexBuffer = static_cast<const GrGpuBuffer*>(indexBuffer.get()));
782 SkASSERT(!gpuIndexBuffer->isCpuBuffer());
783 SkASSERT(!gpuIndexBuffer->isMapped());
784 currCmdBuf->bindIndexBuffer(fGpu, std::move(indexBuffer));
785 }
786 }
787
788 void GrVkOpsRenderPass::onDrawInstanced(int instanceCount,
789 int baseInstance,
790 int vertexCount, int baseVertex) {
791 if (!fCurrentRenderPass) {
792 SkASSERT(fGpu->isDeviceLost());
793 return;
794 }
795 SkASSERT(fCurrentPipelineState);
796 this->currentCommandBuffer()->draw(fGpu, vertexCount, instanceCount, baseVertex, baseInstance);
797 fGpu->stats()->incNumDraws();
798 fCurrentCBIsEmpty = false;
799 }
800
801 void GrVkOpsRenderPass::onDrawIndexedInstanced(int indexCount, int baseIndex, int instanceCount,
802 int baseInstance, int baseVertex) {
803 if (!fCurrentRenderPass) {
804 SkASSERT(fGpu->isDeviceLost());
805 return;
806 }
807 SkASSERT(fCurrentPipelineState);
808 this->currentCommandBuffer()->drawIndexed(fGpu, indexCount, instanceCount,
809 baseIndex, baseVertex, baseInstance);
810 fGpu->stats()->incNumDraws();
811 fCurrentCBIsEmpty = false;
812 }
813
814 void GrVkOpsRenderPass::onDrawIndirect(const GrBuffer* drawIndirectBuffer, size_t offset,
815 int drawCount) {
816 SkASSERT(!drawIndirectBuffer->isCpuBuffer());
817 if (!fCurrentRenderPass) {
818 SkASSERT(fGpu->isDeviceLost());
819 return;
820 }
821 const GrVkCaps& caps = fGpu->vkCaps();
822 SkASSERT(caps.nativeDrawIndirectSupport());
823 SkASSERT(fCurrentPipelineState);
824
825 const uint32_t maxDrawCount = caps.maxDrawIndirectDrawCount();
826 uint32_t remainingDraws = drawCount;
827 const size_t stride = sizeof(GrDrawIndirectCommand);
828 while (remainingDraws >= 1) {
829 uint32_t currDrawCount = std::min(remainingDraws, maxDrawCount);
830 this->currentCommandBuffer()->drawIndirect(
831 fGpu, sk_ref_sp(drawIndirectBuffer), offset, currDrawCount, stride);
832 remainingDraws -= currDrawCount;
833 offset += stride * currDrawCount;
834 fGpu->stats()->incNumDraws();
835 }
836 fCurrentCBIsEmpty = false;
837 }
838
839 void GrVkOpsRenderPass::onDrawIndexedIndirect(const GrBuffer* drawIndirectBuffer, size_t offset,
840 int drawCount) {
841 SkASSERT(!drawIndirectBuffer->isCpuBuffer());
842 if (!fCurrentRenderPass) {
843 SkASSERT(fGpu->isDeviceLost());
844 return;
845 }
846 const GrVkCaps& caps = fGpu->vkCaps();
847 SkASSERT(caps.nativeDrawIndirectSupport());
848 SkASSERT(fCurrentPipelineState);
849 const uint32_t maxDrawCount = caps.maxDrawIndirectDrawCount();
850 uint32_t remainingDraws = drawCount;
851 const size_t stride = sizeof(GrDrawIndexedIndirectCommand);
852 while (remainingDraws >= 1) {
853 uint32_t currDrawCount = std::min(remainingDraws, maxDrawCount);
854 this->currentCommandBuffer()->drawIndexedIndirect(
855 fGpu, sk_ref_sp(drawIndirectBuffer), offset, currDrawCount, stride);
856 remainingDraws -= currDrawCount;
857 offset += stride * currDrawCount;
858 fGpu->stats()->incNumDraws();
859 }
860 fCurrentCBIsEmpty = false;
861 }
862
863 ////////////////////////////////////////////////////////////////////////////////
864
865 void GrVkOpsRenderPass::onExecuteDrawable(std::unique_ptr<SkDrawable::GpuDrawHandler> drawable) {
866 if (!fCurrentRenderPass) {
867 SkASSERT(fGpu->isDeviceLost());
868 return;
869 }
870
871 VkRect2D bounds;
872 bounds.offset = { 0, 0 };
873 bounds.extent = { 0, 0 };
874
875 if (!fCurrentSecondaryCommandBuffer) {
876 fGpu->endRenderPass(fRenderTarget, fOrigin, fBounds);
877 this->addAdditionalRenderPass(true);
878 // We may have failed to start a new render pass
879 if (!fCurrentRenderPass) {
880 SkASSERT(fGpu->isDeviceLost());
881 return;
882 }
883 }
884 SkASSERT(fCurrentSecondaryCommandBuffer);
885
886 GrVkDrawableInfo vkInfo;
887 vkInfo.fSecondaryCommandBuffer = fCurrentSecondaryCommandBuffer->vkCommandBuffer();
888 vkInfo.fCompatibleRenderPass = fCurrentRenderPass->vkRenderPass();
889 SkAssertResult(fCurrentRenderPass->colorAttachmentIndex(&vkInfo.fColorAttachmentIndex));
890 vkInfo.fFormat = fFramebuffer->colorAttachment()->imageFormat();
891 vkInfo.fDrawBounds = &bounds;
892 #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
893 vkInfo.fFromSwapchainOrAndroidWindow =
894 fFramebuffer->colorAttachment()->vkImageInfo().fPartOfSwapchainOrAndroidWindow;
895 #endif //SK_BUILD_FOR_ANDROID_FRAMEWORK
896
897 GrBackendDrawableInfo info(vkInfo);
898
899 // After we draw into the command buffer via the drawable, cached state we have may be invalid.
900 this->currentCommandBuffer()->invalidateState();
901 // Also assume that the drawable produced output.
902 fCurrentCBIsEmpty = false;
903
904 drawable->draw(info);
905 fGpu->addDrawable(std::move(drawable));
906 }