egdaniel | 066df7c | 2016-06-08 14:02:27 -0700 | [diff] [blame] | 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 | |
Greg Daniel | 2d41d0d | 2019-08-26 11:08:51 -0400 | [diff] [blame] | 8 | #include "src/gpu/vk/GrVkOpsRenderPass.h" |
egdaniel | 066df7c | 2016-06-08 14:02:27 -0700 | [diff] [blame] | 9 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "include/core/SkDrawable.h" |
| 11 | #include "include/core/SkRect.h" |
| 12 | #include "include/gpu/GrBackendDrawableInfo.h" |
| 13 | #include "src/gpu/GrContextPriv.h" |
| 14 | #include "src/gpu/GrFixedClip.h" |
| 15 | #include "src/gpu/GrMesh.h" |
| 16 | #include "src/gpu/GrOpFlushState.h" |
| 17 | #include "src/gpu/GrPipeline.h" |
| 18 | #include "src/gpu/GrRenderTargetPriv.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 19 | #include "src/gpu/vk/GrVkCommandBuffer.h" |
| 20 | #include "src/gpu/vk/GrVkCommandPool.h" |
| 21 | #include "src/gpu/vk/GrVkGpu.h" |
| 22 | #include "src/gpu/vk/GrVkPipeline.h" |
| 23 | #include "src/gpu/vk/GrVkRenderPass.h" |
| 24 | #include "src/gpu/vk/GrVkRenderTarget.h" |
| 25 | #include "src/gpu/vk/GrVkResourceProvider.h" |
| 26 | #include "src/gpu/vk/GrVkSemaphore.h" |
| 27 | #include "src/gpu/vk/GrVkTexture.h" |
egdaniel | 066df7c | 2016-06-08 14:02:27 -0700 | [diff] [blame] | 28 | |
Brian Salomon | 5d8f1cc | 2019-04-24 09:03:53 -0400 | [diff] [blame] | 29 | ///////////////////////////////////////////////////////////////////////////// |
| 30 | |
Robert Phillips | 6b47c7d | 2017-08-29 07:24:09 -0400 | [diff] [blame] | 31 | void get_vk_load_store_ops(GrLoadOp loadOpIn, GrStoreOp storeOpIn, |
egdaniel | 066df7c | 2016-06-08 14:02:27 -0700 | [diff] [blame] | 32 | VkAttachmentLoadOp* loadOp, VkAttachmentStoreOp* storeOp) { |
Robert Phillips | 9521447 | 2017-08-08 18:00:03 -0400 | [diff] [blame] | 33 | switch (loadOpIn) { |
Robert Phillips | 6b47c7d | 2017-08-29 07:24:09 -0400 | [diff] [blame] | 34 | case GrLoadOp::kLoad: |
egdaniel | 066df7c | 2016-06-08 14:02:27 -0700 | [diff] [blame] | 35 | *loadOp = VK_ATTACHMENT_LOAD_OP_LOAD; |
egdaniel | 066df7c | 2016-06-08 14:02:27 -0700 | [diff] [blame] | 36 | break; |
Robert Phillips | 6b47c7d | 2017-08-29 07:24:09 -0400 | [diff] [blame] | 37 | case GrLoadOp::kClear: |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 38 | *loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR; |
| 39 | break; |
Robert Phillips | 6b47c7d | 2017-08-29 07:24:09 -0400 | [diff] [blame] | 40 | case GrLoadOp::kDiscard: |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 41 | *loadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE; |
| 42 | break; |
| 43 | default: |
| 44 | SK_ABORT("Invalid LoadOp"); |
egdaniel | 066df7c | 2016-06-08 14:02:27 -0700 | [diff] [blame] | 45 | *loadOp = VK_ATTACHMENT_LOAD_OP_LOAD; |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 46 | } |
| 47 | |
Robert Phillips | 9521447 | 2017-08-08 18:00:03 -0400 | [diff] [blame] | 48 | switch (storeOpIn) { |
Robert Phillips | 6b47c7d | 2017-08-29 07:24:09 -0400 | [diff] [blame] | 49 | case GrStoreOp::kStore: |
egdaniel | 066df7c | 2016-06-08 14:02:27 -0700 | [diff] [blame] | 50 | *storeOp = VK_ATTACHMENT_STORE_OP_STORE; |
| 51 | break; |
Robert Phillips | 6b47c7d | 2017-08-29 07:24:09 -0400 | [diff] [blame] | 52 | case GrStoreOp::kDiscard: |
egdaniel | 066df7c | 2016-06-08 14:02:27 -0700 | [diff] [blame] | 53 | *storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE; |
| 54 | break; |
brianosman | 0bbc371 | 2016-06-14 04:53:09 -0700 | [diff] [blame] | 55 | default: |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 56 | SK_ABORT("Invalid StoreOp"); |
brianosman | 0bbc371 | 2016-06-14 04:53:09 -0700 | [diff] [blame] | 57 | *storeOp = VK_ATTACHMENT_STORE_OP_STORE; |
egdaniel | 066df7c | 2016-06-08 14:02:27 -0700 | [diff] [blame] | 58 | } |
| 59 | } |
| 60 | |
Greg Daniel | 2d41d0d | 2019-08-26 11:08:51 -0400 | [diff] [blame] | 61 | GrVkOpsRenderPass::GrVkOpsRenderPass(GrVkGpu* gpu) : fGpu(gpu) {} |
Brian Salomon | c293a29 | 2016-11-30 13:38:32 -0500 | [diff] [blame] | 62 | |
Greg Daniel | f0c681e | 2019-09-05 14:07:41 -0400 | [diff] [blame] | 63 | void GrVkOpsRenderPass::init(const GrOpsRenderPass::LoadAndStoreInfo& colorInfo, |
| 64 | const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilInfo, |
| 65 | const SkPMColor4f& clearColor) { |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 66 | |
Greg Daniel | f0c681e | 2019-09-05 14:07:41 -0400 | [diff] [blame] | 67 | VkAttachmentLoadOp loadOp; |
| 68 | VkAttachmentStoreOp storeOp; |
| 69 | get_vk_load_store_ops(colorInfo.fLoadOp, colorInfo.fStoreOp, |
| 70 | &loadOp, &storeOp); |
| 71 | GrVkRenderPass::LoadStoreOps vkColorOps(loadOp, storeOp); |
| 72 | |
| 73 | get_vk_load_store_ops(stencilInfo.fLoadOp, stencilInfo.fStoreOp, |
| 74 | &loadOp, &storeOp); |
| 75 | GrVkRenderPass::LoadStoreOps vkStencilOps(loadOp, storeOp); |
Greg Daniel | 36a77ee | 2016-10-18 10:33:25 -0400 | [diff] [blame] | 76 | |
Robert Phillips | 19e51dc | 2017-08-09 09:30:51 -0400 | [diff] [blame] | 77 | GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(fRenderTarget); |
Greg Daniel | f0c681e | 2019-09-05 14:07:41 -0400 | [diff] [blame] | 78 | GrVkImage* targetImage = vkRT->msaaImage() ? vkRT->msaaImage() : vkRT; |
| 79 | |
| 80 | // Change layout of our render target so it can be used as the color attachment. |
| 81 | // TODO: If we know that we will never be blending or loading the attachment we could drop the |
| 82 | // VK_ACCESS_COLOR_ATTACHMENT_READ_BIT. |
| 83 | targetImage->setImageLayout(fGpu, |
| 84 | VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, |
| 85 | VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | |
| 86 | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT, |
| 87 | VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, |
| 88 | false); |
| 89 | |
| 90 | // If we are using a stencil attachment we also need to update its layout |
| 91 | if (GrStencilAttachment* stencil = fRenderTarget->renderTargetPriv().getStencilAttachment()) { |
| 92 | GrVkStencilAttachment* vkStencil = (GrVkStencilAttachment*)stencil; |
| 93 | // We need the write and read access bits since we may load and store the stencil. |
| 94 | // The initial load happens in the VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT so we |
| 95 | // wait there. |
| 96 | vkStencil->setImageLayout(fGpu, |
| 97 | VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, |
| 98 | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT | |
| 99 | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT, |
| 100 | VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT, |
| 101 | false); |
| 102 | } |
| 103 | |
Robert Phillips | 19e51dc | 2017-08-09 09:30:51 -0400 | [diff] [blame] | 104 | const GrVkResourceProvider::CompatibleRPHandle& rpHandle = vkRT->compatibleRenderPassHandle(); |
egdaniel | 066df7c | 2016-06-08 14:02:27 -0700 | [diff] [blame] | 105 | if (rpHandle.isValid()) { |
Greg Daniel | f0c681e | 2019-09-05 14:07:41 -0400 | [diff] [blame] | 106 | fCurrentRenderPass = fGpu->resourceProvider().findRenderPass(rpHandle, |
Greg Daniel | 36a77ee | 2016-10-18 10:33:25 -0400 | [diff] [blame] | 107 | vkColorOps, |
| 108 | vkStencilOps); |
egdaniel | 066df7c | 2016-06-08 14:02:27 -0700 | [diff] [blame] | 109 | } else { |
Greg Daniel | f0c681e | 2019-09-05 14:07:41 -0400 | [diff] [blame] | 110 | fCurrentRenderPass = fGpu->resourceProvider().findRenderPass(*vkRT, |
Greg Daniel | 36a77ee | 2016-10-18 10:33:25 -0400 | [diff] [blame] | 111 | vkColorOps, |
| 112 | vkStencilOps); |
egdaniel | 066df7c | 2016-06-08 14:02:27 -0700 | [diff] [blame] | 113 | } |
Greg Daniel | f0c681e | 2019-09-05 14:07:41 -0400 | [diff] [blame] | 114 | SkASSERT(fCurrentRenderPass); |
egdaniel | 066df7c | 2016-06-08 14:02:27 -0700 | [diff] [blame] | 115 | |
Greg Daniel | f0c681e | 2019-09-05 14:07:41 -0400 | [diff] [blame] | 116 | VkClearValue vkClearColor; |
| 117 | vkClearColor.color.float32[0] = clearColor[0]; |
| 118 | vkClearColor.color.float32[1] = clearColor[1]; |
| 119 | vkClearColor.color.float32[2] = clearColor[2]; |
| 120 | vkClearColor.color.float32[3] = clearColor[3]; |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 121 | |
Greg Daniel | 28d40b2 | 2019-09-09 15:00:15 +0000 | [diff] [blame] | 122 | if (!fGpu->vkCaps().preferPrimaryOverSecondaryCommandBuffers()) { |
| 123 | fCurrentSecondaryCommandBuffer = fGpu->cmdPool()->findOrCreateSecondaryCommandBuffer(fGpu); |
| 124 | fCurrentSecondaryCommandBuffer->begin(fGpu, vkRT->framebuffer(), fCurrentRenderPass); |
| 125 | } |
Greg Daniel | a3c68df | 2018-03-16 13:46:53 -0400 | [diff] [blame] | 126 | |
Greg Daniel | 28d40b2 | 2019-09-09 15:00:15 +0000 | [diff] [blame] | 127 | fGpu->beginRenderPass(fCurrentRenderPass, &vkClearColor, vkRT, fOrigin, fBounds, |
| 128 | SkToBool(fCurrentSecondaryCommandBuffer)); |
egdaniel | 066df7c | 2016-06-08 14:02:27 -0700 | [diff] [blame] | 129 | } |
| 130 | |
Greg Daniel | 2d41d0d | 2019-08-26 11:08:51 -0400 | [diff] [blame] | 131 | void GrVkOpsRenderPass::initWrapped() { |
Greg Daniel | 070cbaf | 2019-01-03 17:35:54 -0500 | [diff] [blame] | 132 | GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(fRenderTarget); |
| 133 | SkASSERT(vkRT->wrapsSecondaryCommandBuffer()); |
Greg Daniel | f0c681e | 2019-09-05 14:07:41 -0400 | [diff] [blame] | 134 | fCurrentRenderPass = vkRT->externalRenderPass(); |
| 135 | SkASSERT(fCurrentRenderPass); |
| 136 | fCurrentRenderPass->ref(); |
Greg Daniel | 070cbaf | 2019-01-03 17:35:54 -0500 | [diff] [blame] | 137 | |
Greg Daniel | f0c681e | 2019-09-05 14:07:41 -0400 | [diff] [blame] | 138 | fCurrentSecondaryCommandBuffer.reset( |
Greg Daniel | 8daf3b7 | 2019-07-30 09:57:26 -0400 | [diff] [blame] | 139 | GrVkSecondaryCommandBuffer::Create(vkRT->getExternalSecondaryCommandBuffer())); |
Greg Daniel | f0c681e | 2019-09-05 14:07:41 -0400 | [diff] [blame] | 140 | fCurrentSecondaryCommandBuffer->begin(fGpu, nullptr, fCurrentRenderPass); |
Greg Daniel | 070cbaf | 2019-01-03 17:35:54 -0500 | [diff] [blame] | 141 | } |
Brian Salomon | c293a29 | 2016-11-30 13:38:32 -0500 | [diff] [blame] | 142 | |
Greg Daniel | 2d41d0d | 2019-08-26 11:08:51 -0400 | [diff] [blame] | 143 | GrVkOpsRenderPass::~GrVkOpsRenderPass() { |
Robert Phillips | 5b5d84c | 2018-08-09 15:12:18 -0400 | [diff] [blame] | 144 | this->reset(); |
egdaniel | 066df7c | 2016-06-08 14:02:27 -0700 | [diff] [blame] | 145 | } |
| 146 | |
Greg Daniel | 2d41d0d | 2019-08-26 11:08:51 -0400 | [diff] [blame] | 147 | GrGpu* GrVkOpsRenderPass::gpu() { return fGpu; } |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 148 | |
Greg Daniel | f0c681e | 2019-09-05 14:07:41 -0400 | [diff] [blame] | 149 | GrVkCommandBuffer* GrVkOpsRenderPass::currentCommandBuffer() { |
Greg Daniel | 28d40b2 | 2019-09-09 15:00:15 +0000 | [diff] [blame] | 150 | if (fCurrentSecondaryCommandBuffer) { |
| 151 | return fCurrentSecondaryCommandBuffer.get(); |
| 152 | } |
| 153 | return fGpu->currentCommandBuffer(); |
Greg Daniel | f0c681e | 2019-09-05 14:07:41 -0400 | [diff] [blame] | 154 | } |
| 155 | |
Greg Daniel | 2d41d0d | 2019-08-26 11:08:51 -0400 | [diff] [blame] | 156 | void GrVkOpsRenderPass::end() { |
Greg Daniel | f0c681e | 2019-09-05 14:07:41 -0400 | [diff] [blame] | 157 | if (fCurrentSecondaryCommandBuffer) { |
| 158 | fCurrentSecondaryCommandBuffer->end(fGpu); |
Brian Salomon | c293a29 | 2016-11-30 13:38:32 -0500 | [diff] [blame] | 159 | } |
egdaniel | 066df7c | 2016-06-08 14:02:27 -0700 | [diff] [blame] | 160 | } |
| 161 | |
Greg Daniel | 2d41d0d | 2019-08-26 11:08:51 -0400 | [diff] [blame] | 162 | void GrVkOpsRenderPass::submit() { |
Brian Salomon | c293a29 | 2016-11-30 13:38:32 -0500 | [diff] [blame] | 163 | if (!fRenderTarget) { |
| 164 | return; |
| 165 | } |
Robert Phillips | 19e51dc | 2017-08-09 09:30:51 -0400 | [diff] [blame] | 166 | |
Greg Daniel | f0c681e | 2019-09-05 14:07:41 -0400 | [diff] [blame] | 167 | // We don't want to actually submit the secondary command buffer if it is wrapped. |
| 168 | if (this->wrapsSecondaryCommandBuffer()) { |
| 169 | return; |
Greg Daniel | 36a77ee | 2016-10-18 10:33:25 -0400 | [diff] [blame] | 170 | } |
Greg Daniel | f0c681e | 2019-09-05 14:07:41 -0400 | [diff] [blame] | 171 | |
| 172 | if (fCurrentSecondaryCommandBuffer) { |
| 173 | fGpu->submitSecondaryCommandBuffer(std::move(fCurrentSecondaryCommandBuffer)); |
| 174 | } |
| 175 | fGpu->endRenderPass(fRenderTarget, fOrigin, fBounds); |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 176 | } |
| 177 | |
Greg Daniel | 2d41d0d | 2019-08-26 11:08:51 -0400 | [diff] [blame] | 178 | void GrVkOpsRenderPass::set(GrRenderTarget* rt, GrSurfaceOrigin origin, |
| 179 | const GrOpsRenderPass::LoadAndStoreInfo& colorInfo, |
Greg Daniel | b20d7e5 | 2019-09-03 13:54:39 -0400 | [diff] [blame] | 180 | const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilInfo, |
| 181 | const SkTArray<GrTextureProxy*, true>& sampledProxies) { |
Robert Phillips | 5b5d84c | 2018-08-09 15:12:18 -0400 | [diff] [blame] | 182 | SkASSERT(!fRenderTarget); |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 183 | SkASSERT(fGpu == rt->getContext()->priv().getGpu()); |
Robert Phillips | 5b5d84c | 2018-08-09 15:12:18 -0400 | [diff] [blame] | 184 | |
Greg Daniel | b0c7ad1 | 2019-06-06 17:23:35 +0000 | [diff] [blame] | 185 | #ifdef SK_DEBUG |
| 186 | fIsActive = true; |
| 187 | #endif |
| 188 | |
Robert Phillips | 5b5d84c | 2018-08-09 15:12:18 -0400 | [diff] [blame] | 189 | this->INHERITED::set(rt, origin); |
| 190 | |
Greg Daniel | b20d7e5 | 2019-09-03 13:54:39 -0400 | [diff] [blame] | 191 | for (int i = 0; i < sampledProxies.count(); ++i) { |
| 192 | if (sampledProxies[i]->isInstantiated()) { |
| 193 | GrVkTexture* vkTex = static_cast<GrVkTexture*>(sampledProxies[i]->peekTexture()); |
| 194 | SkASSERT(vkTex); |
| 195 | vkTex->setImageLayout( |
| 196 | fGpu, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, VK_ACCESS_SHADER_READ_BIT, |
| 197 | VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT, false); |
| 198 | } |
| 199 | } |
| 200 | |
Greg Daniel | 070cbaf | 2019-01-03 17:35:54 -0500 | [diff] [blame] | 201 | if (this->wrapsSecondaryCommandBuffer()) { |
| 202 | this->initWrapped(); |
| 203 | return; |
| 204 | } |
| 205 | |
Greg Daniel | f0c681e | 2019-09-05 14:07:41 -0400 | [diff] [blame] | 206 | // TODO: This should be passed in via the GrOpsTask instead of always setting it to the full |
| 207 | // render target bounds. |
| 208 | fBounds = SkIRect::MakeWH(fRenderTarget->width(), fRenderTarget->height()); |
Robert Phillips | 5b5d84c | 2018-08-09 15:12:18 -0400 | [diff] [blame] | 209 | |
Greg Daniel | f0c681e | 2019-09-05 14:07:41 -0400 | [diff] [blame] | 210 | this->init(colorInfo, stencilInfo, colorInfo.fClearColor); |
Robert Phillips | 5b5d84c | 2018-08-09 15:12:18 -0400 | [diff] [blame] | 211 | } |
| 212 | |
Greg Daniel | 2d41d0d | 2019-08-26 11:08:51 -0400 | [diff] [blame] | 213 | void GrVkOpsRenderPass::reset() { |
Greg Daniel | f0c681e | 2019-09-05 14:07:41 -0400 | [diff] [blame] | 214 | if (fCurrentSecondaryCommandBuffer) { |
| 215 | fCurrentSecondaryCommandBuffer.release()->recycle(fGpu); |
Robert Phillips | 5b5d84c | 2018-08-09 15:12:18 -0400 | [diff] [blame] | 216 | } |
Greg Daniel | f0c681e | 2019-09-05 14:07:41 -0400 | [diff] [blame] | 217 | if (fCurrentRenderPass) { |
| 218 | fCurrentRenderPass->unref(fGpu); |
| 219 | fCurrentRenderPass = nullptr; |
| 220 | } |
| 221 | fCurrentCBIsEmpty = true; |
Robert Phillips | 5b5d84c | 2018-08-09 15:12:18 -0400 | [diff] [blame] | 222 | |
Robert Phillips | 5b5d84c | 2018-08-09 15:12:18 -0400 | [diff] [blame] | 223 | fRenderTarget = nullptr; |
Greg Daniel | b0c7ad1 | 2019-06-06 17:23:35 +0000 | [diff] [blame] | 224 | |
| 225 | #ifdef SK_DEBUG |
| 226 | fIsActive = false; |
| 227 | #endif |
Robert Phillips | 5b5d84c | 2018-08-09 15:12:18 -0400 | [diff] [blame] | 228 | } |
| 229 | |
Greg Daniel | 2d41d0d | 2019-08-26 11:08:51 -0400 | [diff] [blame] | 230 | bool GrVkOpsRenderPass::wrapsSecondaryCommandBuffer() const { |
Greg Daniel | 070cbaf | 2019-01-03 17:35:54 -0500 | [diff] [blame] | 231 | GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(fRenderTarget); |
| 232 | return vkRT->wrapsSecondaryCommandBuffer(); |
| 233 | } |
| 234 | |
Greg Daniel | 64cc9aa | 2018-10-19 13:54:56 -0400 | [diff] [blame] | 235 | //////////////////////////////////////////////////////////////////////////////// |
| 236 | |
Greg Daniel | 2d41d0d | 2019-08-26 11:08:51 -0400 | [diff] [blame] | 237 | void GrVkOpsRenderPass::insertEventMarker(const char* msg) { |
Robert Phillips | 65a88fa | 2017-08-08 08:36:22 -0400 | [diff] [blame] | 238 | // TODO: does Vulkan have a correlate? |
| 239 | } |
| 240 | |
Greg Daniel | 2d41d0d | 2019-08-26 11:08:51 -0400 | [diff] [blame] | 241 | void GrVkOpsRenderPass::onClearStencilClip(const GrFixedClip& clip, bool insideStencilMask) { |
Chris Dalton | 94c0468 | 2017-11-01 17:15:06 -0600 | [diff] [blame] | 242 | SkASSERT(!clip.hasWindowRectangles()); |
| 243 | |
Greg Daniel | 65a0927 | 2016-10-12 09:47:22 -0400 | [diff] [blame] | 244 | GrStencilAttachment* sb = fRenderTarget->renderTargetPriv().getStencilAttachment(); |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 245 | // this should only be called internally when we know we have a |
| 246 | // stencil buffer. |
| 247 | SkASSERT(sb); |
| 248 | int stencilBitCount = sb->bits(); |
| 249 | |
| 250 | // The contract with the callers does not guarantee that we preserve all bits in the stencil |
| 251 | // during this clear. Thus we will clear the entire stencil to the desired value. |
| 252 | |
| 253 | VkClearDepthStencilValue vkStencilColor; |
| 254 | memset(&vkStencilColor, 0, sizeof(VkClearDepthStencilValue)); |
csmartdalton | 29df760 | 2016-08-31 11:55:52 -0700 | [diff] [blame] | 255 | if (insideStencilMask) { |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 256 | vkStencilColor.stencil = (1 << (stencilBitCount - 1)); |
| 257 | } else { |
| 258 | vkStencilColor.stencil = 0; |
| 259 | } |
| 260 | |
| 261 | VkClearRect clearRect; |
| 262 | // Flip rect if necessary |
csmartdalton | 29df760 | 2016-08-31 11:55:52 -0700 | [diff] [blame] | 263 | SkIRect vkRect; |
Brian Salomon | d818ebf | 2018-07-02 14:08:49 +0000 | [diff] [blame] | 264 | if (!clip.scissorEnabled()) { |
Greg Daniel | a41a74a | 2018-10-09 12:59:23 +0000 | [diff] [blame] | 265 | vkRect.setXYWH(0, 0, fRenderTarget->width(), fRenderTarget->height()); |
Robert Phillips | 4f101a7 | 2017-07-28 08:42:04 -0400 | [diff] [blame] | 266 | } else if (kBottomLeft_GrSurfaceOrigin != fOrigin) { |
csmartdalton | 29df760 | 2016-08-31 11:55:52 -0700 | [diff] [blame] | 267 | vkRect = clip.scissorRect(); |
| 268 | } else { |
| 269 | const SkIRect& scissor = clip.scissorRect(); |
Greg Daniel | 65a0927 | 2016-10-12 09:47:22 -0400 | [diff] [blame] | 270 | vkRect.setLTRB(scissor.fLeft, fRenderTarget->height() - scissor.fBottom, |
| 271 | scissor.fRight, fRenderTarget->height() - scissor.fTop); |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | clearRect.rect.offset = { vkRect.fLeft, vkRect.fTop }; |
| 275 | clearRect.rect.extent = { (uint32_t)vkRect.width(), (uint32_t)vkRect.height() }; |
| 276 | |
| 277 | clearRect.baseArrayLayer = 0; |
| 278 | clearRect.layerCount = 1; |
| 279 | |
| 280 | uint32_t stencilIndex; |
Greg Daniel | f0c681e | 2019-09-05 14:07:41 -0400 | [diff] [blame] | 281 | SkAssertResult(fCurrentRenderPass->stencilAttachmentIndex(&stencilIndex)); |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 282 | |
| 283 | VkClearAttachment attachment; |
| 284 | attachment.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT; |
| 285 | attachment.colorAttachment = 0; // this value shouldn't matter |
| 286 | attachment.clearValue.depthStencil = vkStencilColor; |
| 287 | |
Greg Daniel | f0c681e | 2019-09-05 14:07:41 -0400 | [diff] [blame] | 288 | this->currentCommandBuffer()->clearAttachments(fGpu, 1, &attachment, 1, &clearRect); |
| 289 | fCurrentCBIsEmpty = false; |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 290 | } |
| 291 | |
Greg Daniel | 2d41d0d | 2019-08-26 11:08:51 -0400 | [diff] [blame] | 292 | void GrVkOpsRenderPass::onClear(const GrFixedClip& clip, const SkPMColor4f& color) { |
Jim Van Verth | 6a40abc | 2017-11-02 16:56:09 +0000 | [diff] [blame] | 293 | // parent class should never let us get here with no RT |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 294 | SkASSERT(!clip.hasWindowRectangles()); |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 295 | |
Brian Osman | 9a9baae | 2018-11-05 15:06:26 -0500 | [diff] [blame] | 296 | VkClearColorValue vkColor = {{color.fR, color.fG, color.fB, color.fA}}; |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 297 | |
Greg Daniel | 674ee74 | 2019-08-27 13:12:33 -0400 | [diff] [blame] | 298 | // If we end up in a situation where we are calling clear without a scissior then in general it |
| 299 | // means we missed an opportunity higher up the stack to set the load op to be a clear. However, |
| 300 | // there are situations where higher up we couldn't discard the previous ops and set a clear |
| 301 | // load op (e.g. if we needed to execute a wait op). Thus we also have the empty check here. |
Greg Daniel | 4fe9257 | 2019-09-03 11:16:40 -0400 | [diff] [blame] | 302 | // TODO: Make the waitOp a RenderTask instead so we can clear out the GrOpsTask for a clear. We |
| 303 | // can then reenable this assert assuming we can't get messed up by a waitOp. |
Greg Daniel | f0c681e | 2019-09-05 14:07:41 -0400 | [diff] [blame] | 304 | //SkASSERT(!fCurrentCBIsEmpty || clip.scissorEnabled()); |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 305 | |
| 306 | // We always do a sub rect clear with clearAttachments since we are inside a render pass |
| 307 | VkClearRect clearRect; |
| 308 | // Flip rect if necessary |
csmartdalton | 29df760 | 2016-08-31 11:55:52 -0700 | [diff] [blame] | 309 | SkIRect vkRect; |
Brian Salomon | d818ebf | 2018-07-02 14:08:49 +0000 | [diff] [blame] | 310 | if (!clip.scissorEnabled()) { |
Greg Daniel | a41a74a | 2018-10-09 12:59:23 +0000 | [diff] [blame] | 311 | vkRect.setXYWH(0, 0, fRenderTarget->width(), fRenderTarget->height()); |
Robert Phillips | 4f101a7 | 2017-07-28 08:42:04 -0400 | [diff] [blame] | 312 | } else if (kBottomLeft_GrSurfaceOrigin != fOrigin) { |
csmartdalton | 29df760 | 2016-08-31 11:55:52 -0700 | [diff] [blame] | 313 | vkRect = clip.scissorRect(); |
| 314 | } else { |
| 315 | const SkIRect& scissor = clip.scissorRect(); |
Greg Daniel | 65a0927 | 2016-10-12 09:47:22 -0400 | [diff] [blame] | 316 | vkRect.setLTRB(scissor.fLeft, fRenderTarget->height() - scissor.fBottom, |
| 317 | scissor.fRight, fRenderTarget->height() - scissor.fTop); |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 318 | } |
| 319 | clearRect.rect.offset = { vkRect.fLeft, vkRect.fTop }; |
| 320 | clearRect.rect.extent = { (uint32_t)vkRect.width(), (uint32_t)vkRect.height() }; |
| 321 | clearRect.baseArrayLayer = 0; |
| 322 | clearRect.layerCount = 1; |
| 323 | |
| 324 | uint32_t colorIndex; |
Greg Daniel | f0c681e | 2019-09-05 14:07:41 -0400 | [diff] [blame] | 325 | SkAssertResult(fCurrentRenderPass->colorAttachmentIndex(&colorIndex)); |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 326 | |
| 327 | VkClearAttachment attachment; |
| 328 | attachment.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; |
| 329 | attachment.colorAttachment = colorIndex; |
| 330 | attachment.clearValue.color = vkColor; |
| 331 | |
Greg Daniel | f0c681e | 2019-09-05 14:07:41 -0400 | [diff] [blame] | 332 | this->currentCommandBuffer()->clearAttachments(fGpu, 1, &attachment, 1, &clearRect); |
| 333 | fCurrentCBIsEmpty = false; |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 334 | return; |
| 335 | } |
| 336 | |
Greg Daniel | 500d58b | 2017-08-24 15:59:33 -0400 | [diff] [blame] | 337 | //////////////////////////////////////////////////////////////////////////////// |
| 338 | |
Greg Daniel | 28d40b2 | 2019-09-09 15:00:15 +0000 | [diff] [blame] | 339 | void GrVkOpsRenderPass::addAdditionalRenderPass(bool mustUseSecondaryCommandBuffer) { |
Greg Daniel | f0c681e | 2019-09-05 14:07:41 -0400 | [diff] [blame] | 340 | SkASSERT(!this->wrapsSecondaryCommandBuffer()); |
Robert Phillips | 19e51dc | 2017-08-09 09:30:51 -0400 | [diff] [blame] | 341 | GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(fRenderTarget); |
| 342 | |
Greg Daniel | 77b53f6 | 2016-10-18 11:48:51 -0400 | [diff] [blame] | 343 | GrVkRenderPass::LoadStoreOps vkColorOps(VK_ATTACHMENT_LOAD_OP_LOAD, |
| 344 | VK_ATTACHMENT_STORE_OP_STORE); |
| 345 | GrVkRenderPass::LoadStoreOps vkStencilOps(VK_ATTACHMENT_LOAD_OP_LOAD, |
| 346 | VK_ATTACHMENT_STORE_OP_STORE); |
| 347 | |
| 348 | const GrVkResourceProvider::CompatibleRPHandle& rpHandle = |
Robert Phillips | 19e51dc | 2017-08-09 09:30:51 -0400 | [diff] [blame] | 349 | vkRT->compatibleRenderPassHandle(); |
Greg Daniel | f0c681e | 2019-09-05 14:07:41 -0400 | [diff] [blame] | 350 | SkASSERT(fCurrentRenderPass); |
| 351 | fCurrentRenderPass->unref(fGpu); |
Greg Daniel | 77b53f6 | 2016-10-18 11:48:51 -0400 | [diff] [blame] | 352 | if (rpHandle.isValid()) { |
Greg Daniel | f0c681e | 2019-09-05 14:07:41 -0400 | [diff] [blame] | 353 | fCurrentRenderPass = fGpu->resourceProvider().findRenderPass(rpHandle, |
Greg Daniel | 77b53f6 | 2016-10-18 11:48:51 -0400 | [diff] [blame] | 354 | vkColorOps, |
| 355 | vkStencilOps); |
| 356 | } else { |
Greg Daniel | f0c681e | 2019-09-05 14:07:41 -0400 | [diff] [blame] | 357 | fCurrentRenderPass = fGpu->resourceProvider().findRenderPass(*vkRT, |
Greg Daniel | 77b53f6 | 2016-10-18 11:48:51 -0400 | [diff] [blame] | 358 | vkColorOps, |
| 359 | vkStencilOps); |
| 360 | } |
Greg Daniel | f0c681e | 2019-09-05 14:07:41 -0400 | [diff] [blame] | 361 | SkASSERT(fCurrentRenderPass); |
Greg Daniel | 77b53f6 | 2016-10-18 11:48:51 -0400 | [diff] [blame] | 362 | |
Greg Daniel | f0c681e | 2019-09-05 14:07:41 -0400 | [diff] [blame] | 363 | VkClearValue vkClearColor; |
| 364 | memset(&vkClearColor, 0, sizeof(VkClearValue)); |
Greg Daniel | 28d40b2 | 2019-09-09 15:00:15 +0000 | [diff] [blame] | 365 | |
| 366 | if (!fGpu->vkCaps().preferPrimaryOverSecondaryCommandBuffers() || |
| 367 | mustUseSecondaryCommandBuffer) { |
| 368 | fCurrentSecondaryCommandBuffer = fGpu->cmdPool()->findOrCreateSecondaryCommandBuffer(fGpu); |
| 369 | fCurrentSecondaryCommandBuffer->begin(fGpu, vkRT->framebuffer(), fCurrentRenderPass); |
| 370 | } |
| 371 | |
Greg Daniel | f0c681e | 2019-09-05 14:07:41 -0400 | [diff] [blame] | 372 | // We use the same fBounds as the whole GrVkOpsRenderPass since we have no way of tracking the |
| 373 | // bounds in GrOpsTask for parts before and after inline uploads separately. |
Greg Daniel | 28d40b2 | 2019-09-09 15:00:15 +0000 | [diff] [blame] | 374 | fGpu->beginRenderPass(fCurrentRenderPass, &vkClearColor, vkRT, fOrigin, fBounds, |
| 375 | SkToBool(fCurrentSecondaryCommandBuffer)); |
Greg Daniel | 77b53f6 | 2016-10-18 11:48:51 -0400 | [diff] [blame] | 376 | } |
| 377 | |
Greg Daniel | 2d41d0d | 2019-08-26 11:08:51 -0400 | [diff] [blame] | 378 | void GrVkOpsRenderPass::inlineUpload(GrOpFlushState* state, |
Brian Salomon | 943ed79 | 2017-10-30 09:37:55 -0400 | [diff] [blame] | 379 | GrDeferredTextureUploadFn& upload) { |
Greg Daniel | f0c681e | 2019-09-05 14:07:41 -0400 | [diff] [blame] | 380 | if (fCurrentSecondaryCommandBuffer) { |
| 381 | fCurrentSecondaryCommandBuffer->end(fGpu); |
| 382 | fGpu->submitSecondaryCommandBuffer(std::move(fCurrentSecondaryCommandBuffer)); |
Greg Daniel | 77b53f6 | 2016-10-18 11:48:51 -0400 | [diff] [blame] | 383 | } |
Greg Daniel | f0c681e | 2019-09-05 14:07:41 -0400 | [diff] [blame] | 384 | fGpu->endRenderPass(fRenderTarget, fOrigin, fBounds); |
Brian Salomon | 24d377e | 2019-04-23 15:24:31 -0400 | [diff] [blame] | 385 | |
Greg Daniel | f0c681e | 2019-09-05 14:07:41 -0400 | [diff] [blame] | 386 | // We pass in true here to signal that after the upload we need to set the upload textures |
| 387 | // layout back to VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL. |
| 388 | state->doUpload(upload, true); |
| 389 | |
Greg Daniel | 28d40b2 | 2019-09-09 15:00:15 +0000 | [diff] [blame] | 390 | this->addAdditionalRenderPass(false); |
Greg Daniel | 77b53f6 | 2016-10-18 11:48:51 -0400 | [diff] [blame] | 391 | } |
| 392 | |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 393 | //////////////////////////////////////////////////////////////////////////////// |
| 394 | |
Greg Daniel | 2d41d0d | 2019-08-26 11:08:51 -0400 | [diff] [blame] | 395 | void GrVkOpsRenderPass::bindGeometry(const GrGpuBuffer* indexBuffer, |
Brian Salomon | dbf7072 | 2019-02-07 11:31:24 -0500 | [diff] [blame] | 396 | const GrGpuBuffer* vertexBuffer, |
| 397 | const GrGpuBuffer* instanceBuffer) { |
Greg Daniel | f0c681e | 2019-09-05 14:07:41 -0400 | [diff] [blame] | 398 | GrVkCommandBuffer* currCmdBuf = this->currentCommandBuffer(); |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 399 | // There is no need to put any memory barriers to make sure host writes have finished here. |
| 400 | // When a command buffer is submitted to a queue, there is an implicit memory barrier that |
| 401 | // occurs for all host writes. Additionally, BufferMemoryBarriers are not allowed inside of |
| 402 | // an active RenderPass. |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 403 | |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 404 | // Here our vertex and instance inputs need to match the same 0-based bindings they were |
| 405 | // assigned in GrVkPipeline. That is, vertex first (if any) followed by instance. |
| 406 | uint32_t binding = 0; |
| 407 | |
Brian Salomon | 802cb31 | 2018-06-08 18:05:20 -0400 | [diff] [blame] | 408 | if (vertexBuffer) { |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 409 | SkASSERT(vertexBuffer); |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 410 | SkASSERT(!vertexBuffer->isMapped()); |
| 411 | |
| 412 | currCmdBuf->bindInputBuffer(fGpu, binding++, |
| 413 | static_cast<const GrVkVertexBuffer*>(vertexBuffer)); |
| 414 | } |
| 415 | |
Brian Salomon | 802cb31 | 2018-06-08 18:05:20 -0400 | [diff] [blame] | 416 | if (instanceBuffer) { |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 417 | SkASSERT(instanceBuffer); |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 418 | SkASSERT(!instanceBuffer->isMapped()); |
| 419 | |
| 420 | currCmdBuf->bindInputBuffer(fGpu, binding++, |
| 421 | static_cast<const GrVkVertexBuffer*>(instanceBuffer)); |
| 422 | } |
Chris Dalton | ff92650 | 2017-05-03 14:36:54 -0400 | [diff] [blame] | 423 | if (indexBuffer) { |
| 424 | SkASSERT(indexBuffer); |
| 425 | SkASSERT(!indexBuffer->isMapped()); |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 426 | |
Chris Dalton | ff92650 | 2017-05-03 14:36:54 -0400 | [diff] [blame] | 427 | currCmdBuf->bindIndexBuffer(fGpu, static_cast<const GrVkIndexBuffer*>(indexBuffer)); |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 428 | } |
| 429 | } |
| 430 | |
Greg Daniel | 2d41d0d | 2019-08-26 11:08:51 -0400 | [diff] [blame] | 431 | GrVkPipelineState* GrVkOpsRenderPass::prepareDrawState( |
Brian Salomon | 4934890 | 2018-06-26 09:12:38 -0400 | [diff] [blame] | 432 | const GrPrimitiveProcessor& primProc, |
| 433 | const GrPipeline& pipeline, |
| 434 | const GrPipeline::FixedDynamicState* fixedDynamicState, |
| 435 | const GrPipeline::DynamicStateArrays* dynamicStateArrays, |
| 436 | GrPrimitiveType primitiveType) { |
Greg Daniel | f0c681e | 2019-09-05 14:07:41 -0400 | [diff] [blame] | 437 | GrVkCommandBuffer* currentCB = this->currentCommandBuffer(); |
| 438 | SkASSERT(fCurrentRenderPass); |
Greg Daniel | 36a77ee | 2016-10-18 10:33:25 -0400 | [diff] [blame] | 439 | |
Greg Daniel | f0c681e | 2019-09-05 14:07:41 -0400 | [diff] [blame] | 440 | VkRenderPass compatibleRenderPass = fCurrentRenderPass->vkRenderPass(); |
Greg Daniel | 99b88e0 | 2018-10-03 15:31:20 -0400 | [diff] [blame] | 441 | |
Greg Daniel | 9a51a86 | 2018-11-30 10:18:14 -0500 | [diff] [blame] | 442 | const GrTextureProxy* const* primProcProxies = nullptr; |
| 443 | if (dynamicStateArrays && dynamicStateArrays->fPrimitiveProcessorTextures) { |
| 444 | primProcProxies = dynamicStateArrays->fPrimitiveProcessorTextures; |
| 445 | } else if (fixedDynamicState) { |
| 446 | primProcProxies = fixedDynamicState->fPrimitiveProcessorTextures; |
| 447 | } |
| 448 | |
| 449 | SkASSERT(SkToBool(primProcProxies) == SkToBool(primProc.numTextureSamplers())); |
| 450 | |
Greg Daniel | 09eeefb | 2017-10-16 15:15:02 -0400 | [diff] [blame] | 451 | GrVkPipelineState* pipelineState = |
Robert Phillips | d0fe875 | 2019-01-31 14:13:59 -0500 | [diff] [blame] | 452 | fGpu->resourceProvider().findOrCreateCompatiblePipelineState(fRenderTarget, fOrigin, |
| 453 | pipeline, |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 454 | primProc, |
Greg Daniel | 9a51a86 | 2018-11-30 10:18:14 -0500 | [diff] [blame] | 455 | primProcProxies, |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 456 | primitiveType, |
Greg Daniel | 99b88e0 | 2018-10-03 15:31:20 -0400 | [diff] [blame] | 457 | compatibleRenderPass); |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 458 | if (!pipelineState) { |
| 459 | return pipelineState; |
| 460 | } |
| 461 | |
Greg Daniel | f0c681e | 2019-09-05 14:07:41 -0400 | [diff] [blame] | 462 | pipelineState->bindPipeline(fGpu, currentCB); |
Greg Daniel | 22bc865 | 2017-03-22 15:45:43 -0400 | [diff] [blame] | 463 | |
Greg Daniel | f0c681e | 2019-09-05 14:07:41 -0400 | [diff] [blame] | 464 | pipelineState->setAndBindUniforms(fGpu, fRenderTarget, fOrigin, primProc, pipeline, currentCB); |
Brian Salomon | f723264 | 2018-09-19 08:58:08 -0400 | [diff] [blame] | 465 | |
| 466 | // Check whether we need to bind textures between each GrMesh. If not we can bind them all now. |
| 467 | bool setTextures = !(dynamicStateArrays && dynamicStateArrays->fPrimitiveProcessorTextures); |
| 468 | if (setTextures) { |
Greg Daniel | f0c681e | 2019-09-05 14:07:41 -0400 | [diff] [blame] | 469 | pipelineState->setAndBindTextures(fGpu, primProc, pipeline, primProcProxies, currentCB); |
Brian Salomon | f723264 | 2018-09-19 08:58:08 -0400 | [diff] [blame] | 470 | } |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 471 | |
Brian Salomon | d818ebf | 2018-07-02 14:08:49 +0000 | [diff] [blame] | 472 | if (!pipeline.isScissorEnabled()) { |
Greg Daniel | f0c681e | 2019-09-05 14:07:41 -0400 | [diff] [blame] | 473 | GrVkPipeline::SetDynamicScissorRectState(fGpu, currentCB, fRenderTarget, fOrigin, |
Robert Phillips | d0fe875 | 2019-01-31 14:13:59 -0500 | [diff] [blame] | 474 | SkIRect::MakeWH(fRenderTarget->width(), |
| 475 | fRenderTarget->height())); |
Brian Salomon | 4934890 | 2018-06-26 09:12:38 -0400 | [diff] [blame] | 476 | } else if (!dynamicStateArrays || !dynamicStateArrays->fScissorRects) { |
| 477 | SkASSERT(fixedDynamicState); |
Greg Daniel | f0c681e | 2019-09-05 14:07:41 -0400 | [diff] [blame] | 478 | GrVkPipeline::SetDynamicScissorRectState(fGpu, currentCB, fRenderTarget, fOrigin, |
Brian Salomon | 4934890 | 2018-06-26 09:12:38 -0400 | [diff] [blame] | 479 | fixedDynamicState->fScissorRect); |
Chris Dalton | 46983b7 | 2017-06-06 12:27:16 -0600 | [diff] [blame] | 480 | } |
Greg Daniel | f0c681e | 2019-09-05 14:07:41 -0400 | [diff] [blame] | 481 | GrVkPipeline::SetDynamicViewportState(fGpu, currentCB, fRenderTarget); |
| 482 | GrVkPipeline::SetDynamicBlendConstantState(fGpu, currentCB, pipeline.outputSwizzle(), |
Chris Dalton | 46983b7 | 2017-06-06 12:27:16 -0600 | [diff] [blame] | 483 | pipeline.getXferProcessor()); |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 484 | |
| 485 | return pipelineState; |
| 486 | } |
| 487 | |
Greg Daniel | b20d7e5 | 2019-09-03 13:54:39 -0400 | [diff] [blame] | 488 | #ifdef SK_DEBUG |
| 489 | void check_sampled_texture(GrTexture* tex, GrRenderTarget* rt, GrVkGpu* gpu) { |
| 490 | SkASSERT(!tex->isProtected() || (rt->isProtected() && gpu->protectedContext())); |
| 491 | GrVkTexture* vkTex = static_cast<GrVkTexture*>(tex); |
| 492 | SkASSERT(vkTex->currentLayout() == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL); |
| 493 | } |
| 494 | #endif |
| 495 | |
| 496 | |
Greg Daniel | 2d41d0d | 2019-08-26 11:08:51 -0400 | [diff] [blame] | 497 | void GrVkOpsRenderPass::onDraw(const GrPrimitiveProcessor& primProc, |
Brian Salomon | ff168d9 | 2018-06-23 15:17:27 -0400 | [diff] [blame] | 498 | const GrPipeline& pipeline, |
Brian Salomon | 4934890 | 2018-06-26 09:12:38 -0400 | [diff] [blame] | 499 | const GrPipeline::FixedDynamicState* fixedDynamicState, |
| 500 | const GrPipeline::DynamicStateArrays* dynamicStateArrays, |
Greg Daniel | 500d58b | 2017-08-24 15:59:33 -0400 | [diff] [blame] | 501 | const GrMesh meshes[], |
Greg Daniel | 500d58b | 2017-08-24 15:59:33 -0400 | [diff] [blame] | 502 | int meshCount, |
| 503 | const SkRect& bounds) { |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 504 | if (!meshCount) { |
| 505 | return; |
| 506 | } |
Greg Daniel | ea022cd | 2018-03-16 11:10:03 -0400 | [diff] [blame] | 507 | |
Greg Daniel | b20d7e5 | 2019-09-03 13:54:39 -0400 | [diff] [blame] | 508 | #ifdef SK_DEBUG |
Brian Salomon | f723264 | 2018-09-19 08:58:08 -0400 | [diff] [blame] | 509 | if (dynamicStateArrays && dynamicStateArrays->fPrimitiveProcessorTextures) { |
| 510 | for (int m = 0, i = 0; m < meshCount; ++m) { |
| 511 | for (int s = 0; s < primProc.numTextureSamplers(); ++s, ++i) { |
| 512 | auto texture = dynamicStateArrays->fPrimitiveProcessorTextures[i]->peekTexture(); |
Greg Daniel | b20d7e5 | 2019-09-03 13:54:39 -0400 | [diff] [blame] | 513 | check_sampled_texture(texture, fRenderTarget, fGpu); |
Brian Salomon | f723264 | 2018-09-19 08:58:08 -0400 | [diff] [blame] | 514 | } |
| 515 | } |
| 516 | } else { |
| 517 | for (int i = 0; i < primProc.numTextureSamplers(); ++i) { |
| 518 | auto texture = fixedDynamicState->fPrimitiveProcessorTextures[i]->peekTexture(); |
Greg Daniel | b20d7e5 | 2019-09-03 13:54:39 -0400 | [diff] [blame] | 519 | check_sampled_texture(texture, fRenderTarget, fGpu); |
Brian Salomon | f723264 | 2018-09-19 08:58:08 -0400 | [diff] [blame] | 520 | } |
Brian Salomon | e782f84 | 2018-07-31 13:53:11 -0400 | [diff] [blame] | 521 | } |
bsalomon | b58a2b4 | 2016-09-26 06:55:02 -0700 | [diff] [blame] | 522 | GrFragmentProcessor::Iter iter(pipeline); |
| 523 | while (const GrFragmentProcessor* fp = iter.next()) { |
Brian Salomon | e782f84 | 2018-07-31 13:53:11 -0400 | [diff] [blame] | 524 | for (int i = 0; i < fp->numTextureSamplers(); ++i) { |
| 525 | const GrFragmentProcessor::TextureSampler& sampler = fp->textureSampler(i); |
Greg Daniel | b20d7e5 | 2019-09-03 13:54:39 -0400 | [diff] [blame] | 526 | check_sampled_texture(sampler.peekTexture(), fRenderTarget, fGpu); |
Brian Salomon | e782f84 | 2018-07-31 13:53:11 -0400 | [diff] [blame] | 527 | } |
egdaniel | 2f5792a | 2016-07-06 08:51:23 -0700 | [diff] [blame] | 528 | } |
Robert Phillips | bb581ce | 2017-05-29 15:05:15 -0400 | [diff] [blame] | 529 | if (GrTexture* dstTexture = pipeline.peekDstTexture()) { |
Greg Daniel | b20d7e5 | 2019-09-03 13:54:39 -0400 | [diff] [blame] | 530 | check_sampled_texture(dstTexture, fRenderTarget, fGpu); |
Brian Salomon | 18dfa98 | 2017-04-03 16:57:43 -0400 | [diff] [blame] | 531 | } |
Greg Daniel | b20d7e5 | 2019-09-03 13:54:39 -0400 | [diff] [blame] | 532 | #endif |
egdaniel | 2f5792a | 2016-07-06 08:51:23 -0700 | [diff] [blame] | 533 | |
Chris Dalton | bca46e2 | 2017-05-15 11:03:26 -0600 | [diff] [blame] | 534 | GrPrimitiveType primitiveType = meshes[0].primitiveType(); |
Brian Salomon | 4934890 | 2018-06-26 09:12:38 -0400 | [diff] [blame] | 535 | GrVkPipelineState* pipelineState = this->prepareDrawState(primProc, pipeline, fixedDynamicState, |
| 536 | dynamicStateArrays, primitiveType); |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 537 | if (!pipelineState) { |
| 538 | return; |
| 539 | } |
| 540 | |
Brian Salomon | d818ebf | 2018-07-02 14:08:49 +0000 | [diff] [blame] | 541 | bool dynamicScissor = |
| 542 | pipeline.isScissorEnabled() && dynamicStateArrays && dynamicStateArrays->fScissorRects; |
Brian Salomon | f723264 | 2018-09-19 08:58:08 -0400 | [diff] [blame] | 543 | bool dynamicTextures = dynamicStateArrays && dynamicStateArrays->fPrimitiveProcessorTextures; |
Brian Salomon | 4934890 | 2018-06-26 09:12:38 -0400 | [diff] [blame] | 544 | |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 545 | for (int i = 0; i < meshCount; ++i) { |
| 546 | const GrMesh& mesh = meshes[i]; |
Chris Dalton | bca46e2 | 2017-05-15 11:03:26 -0600 | [diff] [blame] | 547 | if (mesh.primitiveType() != primitiveType) { |
Chris Dalton | 6f24180 | 2017-05-08 13:58:38 -0400 | [diff] [blame] | 548 | SkDEBUGCODE(pipelineState = nullptr); |
Chris Dalton | bca46e2 | 2017-05-15 11:03:26 -0600 | [diff] [blame] | 549 | primitiveType = mesh.primitiveType(); |
Brian Salomon | 4934890 | 2018-06-26 09:12:38 -0400 | [diff] [blame] | 550 | pipelineState = this->prepareDrawState(primProc, pipeline, fixedDynamicState, |
| 551 | dynamicStateArrays, primitiveType); |
Chris Dalton | 6f24180 | 2017-05-08 13:58:38 -0400 | [diff] [blame] | 552 | if (!pipelineState) { |
| 553 | return; |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 554 | } |
Chris Dalton | 6f24180 | 2017-05-08 13:58:38 -0400 | [diff] [blame] | 555 | } |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 556 | |
Brian Salomon | 4934890 | 2018-06-26 09:12:38 -0400 | [diff] [blame] | 557 | if (dynamicScissor) { |
Greg Daniel | f0c681e | 2019-09-05 14:07:41 -0400 | [diff] [blame] | 558 | GrVkPipeline::SetDynamicScissorRectState(fGpu, this->currentCommandBuffer(), |
| 559 | fRenderTarget, fOrigin, |
Brian Salomon | 4934890 | 2018-06-26 09:12:38 -0400 | [diff] [blame] | 560 | dynamicStateArrays->fScissorRects[i]); |
Chris Dalton | 46983b7 | 2017-06-06 12:27:16 -0600 | [diff] [blame] | 561 | } |
Brian Salomon | f723264 | 2018-09-19 08:58:08 -0400 | [diff] [blame] | 562 | if (dynamicTextures) { |
| 563 | GrTextureProxy* const* meshProxies = dynamicStateArrays->fPrimitiveProcessorTextures + |
| 564 | primProc.numTextureSamplers() * i; |
| 565 | pipelineState->setAndBindTextures(fGpu, primProc, pipeline, meshProxies, |
Greg Daniel | f0c681e | 2019-09-05 14:07:41 -0400 | [diff] [blame] | 566 | this->currentCommandBuffer()); |
Brian Salomon | f723264 | 2018-09-19 08:58:08 -0400 | [diff] [blame] | 567 | } |
Chris Dalton | bca46e2 | 2017-05-15 11:03:26 -0600 | [diff] [blame] | 568 | SkASSERT(pipelineState); |
Brian Salomon | 802cb31 | 2018-06-08 18:05:20 -0400 | [diff] [blame] | 569 | mesh.sendToGpu(this); |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 570 | } |
| 571 | |
Greg Daniel | f0c681e | 2019-09-05 14:07:41 -0400 | [diff] [blame] | 572 | fCurrentCBIsEmpty = false; |
egdaniel | 066df7c | 2016-06-08 14:02:27 -0700 | [diff] [blame] | 573 | } |
| 574 | |
Greg Daniel | 2d41d0d | 2019-08-26 11:08:51 -0400 | [diff] [blame] | 575 | void GrVkOpsRenderPass::sendInstancedMeshToGpu(GrPrimitiveType, |
| 576 | const GrBuffer* vertexBuffer, |
| 577 | int vertexCount, |
| 578 | int baseVertex, |
| 579 | const GrBuffer* instanceBuffer, |
| 580 | int instanceCount, |
| 581 | int baseInstance) { |
Brian Salomon | dbf7072 | 2019-02-07 11:31:24 -0500 | [diff] [blame] | 582 | SkASSERT(!vertexBuffer || !vertexBuffer->isCpuBuffer()); |
| 583 | SkASSERT(!instanceBuffer || !instanceBuffer->isCpuBuffer()); |
| 584 | auto gpuVertexBuffer = static_cast<const GrGpuBuffer*>(vertexBuffer); |
| 585 | auto gpuInstanceBuffer = static_cast<const GrGpuBuffer*>(instanceBuffer); |
| 586 | this->bindGeometry(nullptr, gpuVertexBuffer, gpuInstanceBuffer); |
Greg Daniel | f0c681e | 2019-09-05 14:07:41 -0400 | [diff] [blame] | 587 | this->currentCommandBuffer()->draw(fGpu, vertexCount, instanceCount, baseVertex, baseInstance); |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 588 | fGpu->stats()->incNumDraws(); |
| 589 | } |
| 590 | |
Greg Daniel | 2d41d0d | 2019-08-26 11:08:51 -0400 | [diff] [blame] | 591 | void GrVkOpsRenderPass::sendIndexedInstancedMeshToGpu(GrPrimitiveType, |
| 592 | const GrBuffer* indexBuffer, |
| 593 | int indexCount, |
| 594 | int baseIndex, |
| 595 | const GrBuffer* vertexBuffer, |
| 596 | int baseVertex, |
| 597 | const GrBuffer* instanceBuffer, |
| 598 | int instanceCount, |
| 599 | int baseInstance, |
| 600 | GrPrimitiveRestart restart) { |
Brian Salomon | 802cb31 | 2018-06-08 18:05:20 -0400 | [diff] [blame] | 601 | SkASSERT(restart == GrPrimitiveRestart::kNo); |
Brian Salomon | dbf7072 | 2019-02-07 11:31:24 -0500 | [diff] [blame] | 602 | SkASSERT(!vertexBuffer || !vertexBuffer->isCpuBuffer()); |
| 603 | SkASSERT(!instanceBuffer || !instanceBuffer->isCpuBuffer()); |
| 604 | SkASSERT(!indexBuffer->isCpuBuffer()); |
| 605 | auto gpuIndexxBuffer = static_cast<const GrGpuBuffer*>(indexBuffer); |
| 606 | auto gpuVertexBuffer = static_cast<const GrGpuBuffer*>(vertexBuffer); |
| 607 | auto gpuInstanceBuffer = static_cast<const GrGpuBuffer*>(instanceBuffer); |
| 608 | this->bindGeometry(gpuIndexxBuffer, gpuVertexBuffer, gpuInstanceBuffer); |
Greg Daniel | f0c681e | 2019-09-05 14:07:41 -0400 | [diff] [blame] | 609 | this->currentCommandBuffer()->drawIndexed(fGpu, indexCount, instanceCount, |
| 610 | baseIndex, baseVertex, baseInstance); |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 611 | fGpu->stats()->incNumDraws(); |
| 612 | } |
Greg Daniel | 64cc9aa | 2018-10-19 13:54:56 -0400 | [diff] [blame] | 613 | |
| 614 | //////////////////////////////////////////////////////////////////////////////// |
| 615 | |
Greg Daniel | 2d41d0d | 2019-08-26 11:08:51 -0400 | [diff] [blame] | 616 | void GrVkOpsRenderPass::executeDrawable(std::unique_ptr<SkDrawable::GpuDrawHandler> drawable) { |
Greg Daniel | 64cc9aa | 2018-10-19 13:54:56 -0400 | [diff] [blame] | 617 | GrVkRenderTarget* target = static_cast<GrVkRenderTarget*>(fRenderTarget); |
| 618 | |
| 619 | GrVkImage* targetImage = target->msaaImage() ? target->msaaImage() : target; |
| 620 | |
Greg Daniel | 64cc9aa | 2018-10-19 13:54:56 -0400 | [diff] [blame] | 621 | VkRect2D bounds; |
| 622 | bounds.offset = { 0, 0 }; |
| 623 | bounds.extent = { 0, 0 }; |
| 624 | |
Greg Daniel | 28d40b2 | 2019-09-09 15:00:15 +0000 | [diff] [blame] | 625 | if (!fCurrentSecondaryCommandBuffer) { |
| 626 | fGpu->endRenderPass(fRenderTarget, fOrigin, fBounds); |
| 627 | this->addAdditionalRenderPass(true); |
| 628 | } |
Greg Daniel | f0c681e | 2019-09-05 14:07:41 -0400 | [diff] [blame] | 629 | SkASSERT(fCurrentSecondaryCommandBuffer); |
| 630 | |
Greg Daniel | 64cc9aa | 2018-10-19 13:54:56 -0400 | [diff] [blame] | 631 | GrVkDrawableInfo vkInfo; |
Greg Daniel | f0c681e | 2019-09-05 14:07:41 -0400 | [diff] [blame] | 632 | vkInfo.fSecondaryCommandBuffer = fCurrentSecondaryCommandBuffer->vkCommandBuffer(); |
| 633 | vkInfo.fCompatibleRenderPass = fCurrentRenderPass->vkRenderPass(); |
| 634 | SkAssertResult(fCurrentRenderPass->colorAttachmentIndex(&vkInfo.fColorAttachmentIndex)); |
Greg Daniel | 64cc9aa | 2018-10-19 13:54:56 -0400 | [diff] [blame] | 635 | vkInfo.fFormat = targetImage->imageFormat(); |
| 636 | vkInfo.fDrawBounds = &bounds; |
Stan Iliev | cb58060 | 2019-02-26 11:36:07 -0500 | [diff] [blame] | 637 | #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK |
| 638 | vkInfo.fImage = targetImage->image(); |
| 639 | #else |
| 640 | vkInfo.fImage = VK_NULL_HANDLE; |
| 641 | #endif //SK_BUILD_FOR_ANDROID_FRAMEWORK |
Greg Daniel | 64cc9aa | 2018-10-19 13:54:56 -0400 | [diff] [blame] | 642 | |
| 643 | GrBackendDrawableInfo info(vkInfo); |
| 644 | |
Eric Karl | c0b2ba2 | 2019-01-22 19:40:35 -0800 | [diff] [blame] | 645 | // After we draw into the command buffer via the drawable, cached state we have may be invalid. |
Greg Daniel | f0c681e | 2019-09-05 14:07:41 -0400 | [diff] [blame] | 646 | this->currentCommandBuffer()->invalidateState(); |
Eric Karl | a8878a1 | 2019-02-07 18:17:43 -0800 | [diff] [blame] | 647 | // Also assume that the drawable produced output. |
Greg Daniel | f0c681e | 2019-09-05 14:07:41 -0400 | [diff] [blame] | 648 | fCurrentCBIsEmpty = false; |
Eric Karl | c0b2ba2 | 2019-01-22 19:40:35 -0800 | [diff] [blame] | 649 | |
Greg Daniel | 64cc9aa | 2018-10-19 13:54:56 -0400 | [diff] [blame] | 650 | drawable->draw(info); |
| 651 | fGpu->addDrawable(std::move(drawable)); |
Greg Daniel | 64cc9aa | 2018-10-19 13:54:56 -0400 | [diff] [blame] | 652 | } |
| 653 | |