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 | |
| 8 | #include "GrVkGpuCommandBuffer.h" |
| 9 | |
Greg Daniel | 64cc9aa | 2018-10-19 13:54:56 -0400 | [diff] [blame] | 10 | #include "GrBackendDrawableInfo.h" |
Robert Phillips | be9aff2 | 2019-02-15 11:33:22 -0500 | [diff] [blame] | 11 | #include "GrContextPriv.h" |
csmartdalton | 29df760 | 2016-08-31 11:55:52 -0700 | [diff] [blame] | 12 | #include "GrFixedClip.h" |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 13 | #include "GrMesh.h" |
Brian Salomon | 742e31d | 2016-12-07 17:06:19 -0500 | [diff] [blame] | 14 | #include "GrOpFlushState.h" |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 15 | #include "GrPipeline.h" |
| 16 | #include "GrRenderTargetPriv.h" |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 17 | #include "GrTexturePriv.h" |
egdaniel | 066df7c | 2016-06-08 14:02:27 -0700 | [diff] [blame] | 18 | #include "GrVkCommandBuffer.h" |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 19 | #include "GrVkCommandPool.h" |
egdaniel | 066df7c | 2016-06-08 14:02:27 -0700 | [diff] [blame] | 20 | #include "GrVkGpu.h" |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 21 | #include "GrVkPipeline.h" |
egdaniel | 066df7c | 2016-06-08 14:02:27 -0700 | [diff] [blame] | 22 | #include "GrVkRenderPass.h" |
| 23 | #include "GrVkRenderTarget.h" |
| 24 | #include "GrVkResourceProvider.h" |
Greg Daniel | 64cc9aa | 2018-10-19 13:54:56 -0400 | [diff] [blame] | 25 | #include "GrVkSemaphore.h" |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 26 | #include "GrVkTexture.h" |
Greg Daniel | 64cc9aa | 2018-10-19 13:54:56 -0400 | [diff] [blame] | 27 | #include "SkDrawable.h" |
Greg Daniel | 36a77ee | 2016-10-18 10:33:25 -0400 | [diff] [blame] | 28 | #include "SkRect.h" |
egdaniel | 066df7c | 2016-06-08 14:02:27 -0700 | [diff] [blame] | 29 | |
Brian Salomon | 5d8f1cc | 2019-04-24 09:03:53 -0400 | [diff] [blame^] | 30 | GrVkPrimaryCommandBufferTask::~GrVkPrimaryCommandBufferTask() = default; |
| 31 | GrVkPrimaryCommandBufferTask::GrVkPrimaryCommandBufferTask() = default; |
| 32 | |
| 33 | namespace { |
| 34 | |
| 35 | class InlineUpload : public GrVkPrimaryCommandBufferTask { |
| 36 | public: |
| 37 | InlineUpload(GrOpFlushState* state, const GrDeferredTextureUploadFn& upload) |
| 38 | : fFlushState(state), fUpload(upload) {} |
| 39 | ~InlineUpload() override = default; |
| 40 | |
| 41 | void execute(const Args& args) override { fFlushState->doUpload(fUpload); } |
| 42 | |
| 43 | private: |
| 44 | GrOpFlushState* fFlushState; |
| 45 | GrDeferredTextureUploadFn fUpload; |
| 46 | }; |
| 47 | |
| 48 | class Copy : public GrVkPrimaryCommandBufferTask { |
| 49 | public: |
| 50 | Copy(GrSurface* src, GrSurfaceOrigin srcOrigin, const SkIRect& srcRect, |
| 51 | const SkIPoint& dstPoint, bool shouldDiscardDst) |
| 52 | : fSrc(src) |
| 53 | , fSrcOrigin(srcOrigin) |
| 54 | , fSrcRect(srcRect) |
| 55 | , fDstPoint(dstPoint) |
| 56 | , fShouldDiscardDst(shouldDiscardDst) {} |
| 57 | ~Copy() override = default; |
| 58 | |
| 59 | void execute(const Args& args) override { |
| 60 | args.fGpu->copySurface(args.fSurface, args.fOrigin, fSrc.get(), fSrcOrigin, fSrcRect, |
| 61 | fDstPoint, fShouldDiscardDst); |
| 62 | } |
| 63 | |
| 64 | private: |
| 65 | using Src = GrPendingIOResource<GrSurface, kRead_GrIOType>; |
| 66 | Src fSrc; |
| 67 | GrSurfaceOrigin fSrcOrigin; |
| 68 | SkIRect fSrcRect; |
| 69 | SkIPoint fDstPoint; |
| 70 | bool fShouldDiscardDst; |
| 71 | }; |
| 72 | |
| 73 | } // anonymous namespace |
| 74 | |
| 75 | ///////////////////////////////////////////////////////////////////////////// |
| 76 | |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 77 | void GrVkGpuTextureCommandBuffer::copy(GrSurface* src, GrSurfaceOrigin srcOrigin, |
| 78 | const SkIRect& srcRect, const SkIPoint& dstPoint) { |
Brian Salomon | 5d8f1cc | 2019-04-24 09:03:53 -0400 | [diff] [blame^] | 79 | fTasks.emplace<Copy>(src, srcOrigin, srcRect, dstPoint, false); |
Greg Daniel | 500d58b | 2017-08-24 15:59:33 -0400 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | void GrVkGpuTextureCommandBuffer::insertEventMarker(const char* msg) { |
| 83 | // TODO: does Vulkan have a correlate? |
| 84 | } |
| 85 | |
| 86 | void GrVkGpuTextureCommandBuffer::submit() { |
Brian Salomon | 5d8f1cc | 2019-04-24 09:03:53 -0400 | [diff] [blame^] | 87 | GrVkPrimaryCommandBufferTask::Args taskArgs{fGpu, fTexture, fOrigin}; |
| 88 | for (auto& task : fTasks) { |
| 89 | task.execute(taskArgs); |
Greg Daniel | 500d58b | 2017-08-24 15:59:33 -0400 | [diff] [blame] | 90 | } |
| 91 | } |
| 92 | |
Greg Daniel | 500d58b | 2017-08-24 15:59:33 -0400 | [diff] [blame] | 93 | //////////////////////////////////////////////////////////////////////////////// |
| 94 | |
Robert Phillips | 6b47c7d | 2017-08-29 07:24:09 -0400 | [diff] [blame] | 95 | void get_vk_load_store_ops(GrLoadOp loadOpIn, GrStoreOp storeOpIn, |
egdaniel | 066df7c | 2016-06-08 14:02:27 -0700 | [diff] [blame] | 96 | VkAttachmentLoadOp* loadOp, VkAttachmentStoreOp* storeOp) { |
Robert Phillips | 9521447 | 2017-08-08 18:00:03 -0400 | [diff] [blame] | 97 | switch (loadOpIn) { |
Robert Phillips | 6b47c7d | 2017-08-29 07:24:09 -0400 | [diff] [blame] | 98 | case GrLoadOp::kLoad: |
egdaniel | 066df7c | 2016-06-08 14:02:27 -0700 | [diff] [blame] | 99 | *loadOp = VK_ATTACHMENT_LOAD_OP_LOAD; |
egdaniel | 066df7c | 2016-06-08 14:02:27 -0700 | [diff] [blame] | 100 | break; |
Robert Phillips | 6b47c7d | 2017-08-29 07:24:09 -0400 | [diff] [blame] | 101 | case GrLoadOp::kClear: |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 102 | *loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR; |
| 103 | break; |
Robert Phillips | 6b47c7d | 2017-08-29 07:24:09 -0400 | [diff] [blame] | 104 | case GrLoadOp::kDiscard: |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 105 | *loadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE; |
| 106 | break; |
| 107 | default: |
| 108 | SK_ABORT("Invalid LoadOp"); |
egdaniel | 066df7c | 2016-06-08 14:02:27 -0700 | [diff] [blame] | 109 | *loadOp = VK_ATTACHMENT_LOAD_OP_LOAD; |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Robert Phillips | 9521447 | 2017-08-08 18:00:03 -0400 | [diff] [blame] | 112 | switch (storeOpIn) { |
Robert Phillips | 6b47c7d | 2017-08-29 07:24:09 -0400 | [diff] [blame] | 113 | case GrStoreOp::kStore: |
egdaniel | 066df7c | 2016-06-08 14:02:27 -0700 | [diff] [blame] | 114 | *storeOp = VK_ATTACHMENT_STORE_OP_STORE; |
| 115 | break; |
Robert Phillips | 6b47c7d | 2017-08-29 07:24:09 -0400 | [diff] [blame] | 116 | case GrStoreOp::kDiscard: |
egdaniel | 066df7c | 2016-06-08 14:02:27 -0700 | [diff] [blame] | 117 | *storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE; |
| 118 | break; |
brianosman | 0bbc371 | 2016-06-14 04:53:09 -0700 | [diff] [blame] | 119 | default: |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 120 | SK_ABORT("Invalid StoreOp"); |
brianosman | 0bbc371 | 2016-06-14 04:53:09 -0700 | [diff] [blame] | 121 | *storeOp = VK_ATTACHMENT_STORE_OP_STORE; |
egdaniel | 066df7c | 2016-06-08 14:02:27 -0700 | [diff] [blame] | 122 | } |
| 123 | } |
| 124 | |
Brian Salomon | 24d377e | 2019-04-23 15:24:31 -0400 | [diff] [blame] | 125 | GrVkGpuRTCommandBuffer::GrVkGpuRTCommandBuffer(GrVkGpu* gpu) : fGpu(gpu) {} |
Brian Salomon | c293a29 | 2016-11-30 13:38:32 -0500 | [diff] [blame] | 126 | |
Greg Daniel | 500d58b | 2017-08-24 15:59:33 -0400 | [diff] [blame] | 127 | void GrVkGpuRTCommandBuffer::init() { |
Brian Salomon | c293a29 | 2016-11-30 13:38:32 -0500 | [diff] [blame] | 128 | GrVkRenderPass::LoadStoreOps vkColorOps(fVkColorLoadOp, fVkColorStoreOp); |
| 129 | GrVkRenderPass::LoadStoreOps vkStencilOps(fVkStencilLoadOp, fVkStencilStoreOp); |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 130 | |
Greg Daniel | 36a77ee | 2016-10-18 10:33:25 -0400 | [diff] [blame] | 131 | CommandBufferInfo& cbInfo = fCommandBufferInfos.push_back(); |
Brian Salomon | c293a29 | 2016-11-30 13:38:32 -0500 | [diff] [blame] | 132 | SkASSERT(fCommandBufferInfos.count() == 1); |
Greg Daniel | 22bc865 | 2017-03-22 15:45:43 -0400 | [diff] [blame] | 133 | fCurrentCmdInfo = 0; |
Greg Daniel | 36a77ee | 2016-10-18 10:33:25 -0400 | [diff] [blame] | 134 | |
Robert Phillips | 19e51dc | 2017-08-09 09:30:51 -0400 | [diff] [blame] | 135 | GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(fRenderTarget); |
| 136 | const GrVkResourceProvider::CompatibleRPHandle& rpHandle = vkRT->compatibleRenderPassHandle(); |
egdaniel | 066df7c | 2016-06-08 14:02:27 -0700 | [diff] [blame] | 137 | if (rpHandle.isValid()) { |
Greg Daniel | 36a77ee | 2016-10-18 10:33:25 -0400 | [diff] [blame] | 138 | cbInfo.fRenderPass = fGpu->resourceProvider().findRenderPass(rpHandle, |
| 139 | vkColorOps, |
| 140 | vkStencilOps); |
egdaniel | 066df7c | 2016-06-08 14:02:27 -0700 | [diff] [blame] | 141 | } else { |
Robert Phillips | 19e51dc | 2017-08-09 09:30:51 -0400 | [diff] [blame] | 142 | cbInfo.fRenderPass = fGpu->resourceProvider().findRenderPass(*vkRT, |
Greg Daniel | 36a77ee | 2016-10-18 10:33:25 -0400 | [diff] [blame] | 143 | vkColorOps, |
| 144 | vkStencilOps); |
egdaniel | 066df7c | 2016-06-08 14:02:27 -0700 | [diff] [blame] | 145 | } |
| 146 | |
Brian Osman | cb3d087 | 2018-10-16 15:19:28 -0400 | [diff] [blame] | 147 | cbInfo.fColorClearValue.color.float32[0] = fClearColor[0]; |
| 148 | cbInfo.fColorClearValue.color.float32[1] = fClearColor[1]; |
| 149 | cbInfo.fColorClearValue.color.float32[2] = fClearColor[2]; |
| 150 | cbInfo.fColorClearValue.color.float32[3] = fClearColor[3]; |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 151 | |
Robert Phillips | 380b90c | 2017-08-30 07:41:07 -0400 | [diff] [blame] | 152 | if (VK_ATTACHMENT_LOAD_OP_CLEAR == fVkColorLoadOp) { |
Greg Daniel | a41a74a | 2018-10-09 12:59:23 +0000 | [diff] [blame] | 153 | cbInfo.fBounds = SkRect::MakeWH(vkRT->width(), vkRT->height()); |
Robert Phillips | 380b90c | 2017-08-30 07:41:07 -0400 | [diff] [blame] | 154 | } else { |
| 155 | cbInfo.fBounds.setEmpty(); |
| 156 | } |
Greg Daniel | a3c68df | 2018-03-16 13:46:53 -0400 | [diff] [blame] | 157 | |
| 158 | if (VK_ATTACHMENT_LOAD_OP_CLEAR == fVkColorLoadOp) { |
| 159 | cbInfo.fLoadStoreState = LoadStoreState::kStartsWithClear; |
| 160 | } else if (VK_ATTACHMENT_LOAD_OP_LOAD == fVkColorLoadOp && |
| 161 | VK_ATTACHMENT_STORE_OP_STORE == fVkColorStoreOp) { |
| 162 | cbInfo.fLoadStoreState = LoadStoreState::kLoadAndStore; |
| 163 | } else if (VK_ATTACHMENT_LOAD_OP_DONT_CARE == fVkColorLoadOp) { |
| 164 | cbInfo.fLoadStoreState = LoadStoreState::kStartsWithDiscard; |
| 165 | } |
Greg Daniel | 36a77ee | 2016-10-18 10:33:25 -0400 | [diff] [blame] | 166 | |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 167 | cbInfo.fCommandBuffers.push_back(fGpu->cmdPool()->findOrCreateSecondaryCommandBuffer(fGpu)); |
Robert Phillips | 19e51dc | 2017-08-09 09:30:51 -0400 | [diff] [blame] | 168 | cbInfo.currentCmdBuf()->begin(fGpu, vkRT->framebuffer(), cbInfo.fRenderPass); |
egdaniel | 066df7c | 2016-06-08 14:02:27 -0700 | [diff] [blame] | 169 | } |
| 170 | |
Greg Daniel | 070cbaf | 2019-01-03 17:35:54 -0500 | [diff] [blame] | 171 | void GrVkGpuRTCommandBuffer::initWrapped() { |
| 172 | CommandBufferInfo& cbInfo = fCommandBufferInfos.push_back(); |
| 173 | SkASSERT(fCommandBufferInfos.count() == 1); |
| 174 | fCurrentCmdInfo = 0; |
| 175 | |
| 176 | GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(fRenderTarget); |
| 177 | SkASSERT(vkRT->wrapsSecondaryCommandBuffer()); |
| 178 | cbInfo.fRenderPass = vkRT->externalRenderPass(); |
| 179 | cbInfo.fRenderPass->ref(); |
| 180 | |
| 181 | cbInfo.fBounds.setEmpty(); |
| 182 | cbInfo.fCommandBuffers.push_back(vkRT->getExternalSecondaryCommandBuffer()); |
| 183 | cbInfo.fCommandBuffers[0]->ref(); |
| 184 | cbInfo.currentCmdBuf()->begin(fGpu, nullptr, cbInfo.fRenderPass); |
| 185 | } |
Brian Salomon | c293a29 | 2016-11-30 13:38:32 -0500 | [diff] [blame] | 186 | |
Greg Daniel | 500d58b | 2017-08-24 15:59:33 -0400 | [diff] [blame] | 187 | GrVkGpuRTCommandBuffer::~GrVkGpuRTCommandBuffer() { |
Robert Phillips | 5b5d84c | 2018-08-09 15:12:18 -0400 | [diff] [blame] | 188 | this->reset(); |
egdaniel | 066df7c | 2016-06-08 14:02:27 -0700 | [diff] [blame] | 189 | } |
| 190 | |
Greg Daniel | 500d58b | 2017-08-24 15:59:33 -0400 | [diff] [blame] | 191 | GrGpu* GrVkGpuRTCommandBuffer::gpu() { return fGpu; } |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 192 | |
Greg Daniel | 500d58b | 2017-08-24 15:59:33 -0400 | [diff] [blame] | 193 | void GrVkGpuRTCommandBuffer::end() { |
Greg Daniel | 22bc865 | 2017-03-22 15:45:43 -0400 | [diff] [blame] | 194 | if (fCurrentCmdInfo >= 0) { |
| 195 | fCommandBufferInfos[fCurrentCmdInfo].currentCmdBuf()->end(fGpu); |
Brian Salomon | c293a29 | 2016-11-30 13:38:32 -0500 | [diff] [blame] | 196 | } |
egdaniel | 066df7c | 2016-06-08 14:02:27 -0700 | [diff] [blame] | 197 | } |
| 198 | |
Greg Daniel | 500d58b | 2017-08-24 15:59:33 -0400 | [diff] [blame] | 199 | void GrVkGpuRTCommandBuffer::submit() { |
Brian Salomon | c293a29 | 2016-11-30 13:38:32 -0500 | [diff] [blame] | 200 | if (!fRenderTarget) { |
| 201 | return; |
| 202 | } |
Robert Phillips | 19e51dc | 2017-08-09 09:30:51 -0400 | [diff] [blame] | 203 | |
| 204 | GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(fRenderTarget); |
Robert Phillips | 19e51dc | 2017-08-09 09:30:51 -0400 | [diff] [blame] | 205 | GrVkImage* targetImage = vkRT->msaaImage() ? vkRT->msaaImage() : vkRT; |
Greg Daniel | 45a44de | 2018-02-27 10:07:29 -0500 | [diff] [blame] | 206 | GrStencilAttachment* stencil = fRenderTarget->renderTargetPriv().getStencilAttachment(); |
Brian Salomon | 24d377e | 2019-04-23 15:24:31 -0400 | [diff] [blame] | 207 | auto currPreCmd = fPreCommandBufferTasks.begin(); |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 208 | |
Brian Salomon | 5d8f1cc | 2019-04-24 09:03:53 -0400 | [diff] [blame^] | 209 | GrVkPrimaryCommandBufferTask::Args taskArgs{fGpu, fRenderTarget, fOrigin}; |
Greg Daniel | 36a77ee | 2016-10-18 10:33:25 -0400 | [diff] [blame] | 210 | for (int i = 0; i < fCommandBufferInfos.count(); ++i) { |
| 211 | CommandBufferInfo& cbInfo = fCommandBufferInfos[i]; |
| 212 | |
Brian Salomon | 24d377e | 2019-04-23 15:24:31 -0400 | [diff] [blame] | 213 | for (int c = 0; c < cbInfo.fNumPreCmds; ++c, ++currPreCmd) { |
Brian Salomon | 5d8f1cc | 2019-04-24 09:03:53 -0400 | [diff] [blame^] | 214 | currPreCmd->execute(taskArgs); |
Greg Daniel | 77b53f6 | 2016-10-18 11:48:51 -0400 | [diff] [blame] | 215 | } |
| 216 | |
Greg Daniel | 38c3d93 | 2018-03-16 14:22:30 -0400 | [diff] [blame] | 217 | // TODO: Many things create a scratch texture which adds the discard immediately, but then |
| 218 | // don't draw to it right away. This causes the discard to be ignored and we get yelled at |
| 219 | // for loading uninitialized data. However, once MDB lands with reordering, the discard will |
| 220 | // get reordered with the rest of the draw commands and we can remove the discard check. |
| 221 | if (cbInfo.fIsEmpty && |
| 222 | cbInfo.fLoadStoreState != LoadStoreState::kStartsWithClear && |
| 223 | cbInfo.fLoadStoreState != LoadStoreState::kStartsWithDiscard) { |
Greg Daniel | 77b53f6 | 2016-10-18 11:48:51 -0400 | [diff] [blame] | 224 | // We have sumbitted no actual draw commands to the command buffer and we are not using |
| 225 | // the render pass to do a clear so there is no need to submit anything. |
| 226 | continue; |
| 227 | } |
Greg Daniel | 38c3d93 | 2018-03-16 14:22:30 -0400 | [diff] [blame] | 228 | |
Greg Daniel | 070cbaf | 2019-01-03 17:35:54 -0500 | [diff] [blame] | 229 | // We don't want to actually submit the secondary command buffer if it is wrapped. |
| 230 | if (this->wrapsSecondaryCommandBuffer()) { |
| 231 | // If we have any sampled images set their layout now. |
Chris Dalton | 298238a | 2019-02-21 16:28:44 -0500 | [diff] [blame] | 232 | for (int j = 0; j < cbInfo.fSampledTextures.count(); ++j) { |
| 233 | cbInfo.fSampledTextures[j]->setImageLayout( |
| 234 | fGpu, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, VK_ACCESS_SHADER_READ_BIT, |
| 235 | VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT, false); |
Greg Daniel | 070cbaf | 2019-01-03 17:35:54 -0500 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | // There should have only been one secondary command buffer in the wrapped case so it is |
| 239 | // safe to just return here. |
| 240 | SkASSERT(fCommandBufferInfos.count() == 1); |
| 241 | return; |
| 242 | } |
| 243 | |
Greg Daniel | dbdba60 | 2018-04-20 11:52:43 -0400 | [diff] [blame] | 244 | // Make sure if we only have a discard load that we execute the discard on the whole image. |
| 245 | // TODO: Once we improve our tracking of discards so that we never end up flushing a discard |
| 246 | // call with no actually ops, remove this. |
| 247 | if (cbInfo.fIsEmpty && cbInfo.fLoadStoreState == LoadStoreState::kStartsWithDiscard) { |
Greg Daniel | a41a74a | 2018-10-09 12:59:23 +0000 | [diff] [blame] | 248 | cbInfo.fBounds = SkRect::MakeWH(vkRT->width(), vkRT->height()); |
Greg Daniel | dbdba60 | 2018-04-20 11:52:43 -0400 | [diff] [blame] | 249 | } |
| 250 | |
Greg Daniel | a41a74a | 2018-10-09 12:59:23 +0000 | [diff] [blame] | 251 | if (cbInfo.fBounds.intersect(0, 0, |
| 252 | SkIntToScalar(fRenderTarget->width()), |
| 253 | SkIntToScalar(fRenderTarget->height()))) { |
Greg Daniel | 38c3d93 | 2018-03-16 14:22:30 -0400 | [diff] [blame] | 254 | // Make sure we do the following layout changes after all copies, uploads, or any other |
| 255 | // pre-work is done since we may change the layouts in the pre-work. Also since the |
| 256 | // draws will be submitted in different render passes, we need to guard againts write |
| 257 | // and write issues. |
| 258 | |
| 259 | // Change layout of our render target so it can be used as the color attachment. |
Greg Daniel | f7828d0 | 2018-10-09 12:01:32 -0400 | [diff] [blame] | 260 | // TODO: If we know that we will never be blending or loading the attachment we could |
| 261 | // drop the VK_ACCESS_COLOR_ATTACHMENT_READ_BIT. |
Greg Daniel | 38c3d93 | 2018-03-16 14:22:30 -0400 | [diff] [blame] | 262 | targetImage->setImageLayout(fGpu, |
| 263 | VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, |
Greg Daniel | f7828d0 | 2018-10-09 12:01:32 -0400 | [diff] [blame] | 264 | VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | |
Greg Daniel | 38c3d93 | 2018-03-16 14:22:30 -0400 | [diff] [blame] | 265 | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT, |
Greg Daniel | f7828d0 | 2018-10-09 12:01:32 -0400 | [diff] [blame] | 266 | VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, |
Greg Daniel | 38c3d93 | 2018-03-16 14:22:30 -0400 | [diff] [blame] | 267 | false); |
| 268 | |
| 269 | // If we are using a stencil attachment we also need to update its layout |
| 270 | if (stencil) { |
| 271 | GrVkStencilAttachment* vkStencil = (GrVkStencilAttachment*)stencil; |
Greg Daniel | f7828d0 | 2018-10-09 12:01:32 -0400 | [diff] [blame] | 272 | // We need the write and read access bits since we may load and store the stencil. |
| 273 | // The initial load happens in the VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT so we |
| 274 | // wait there. |
Greg Daniel | 38c3d93 | 2018-03-16 14:22:30 -0400 | [diff] [blame] | 275 | vkStencil->setImageLayout(fGpu, |
| 276 | VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, |
| 277 | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT | |
| 278 | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT, |
Greg Daniel | f7828d0 | 2018-10-09 12:01:32 -0400 | [diff] [blame] | 279 | VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT, |
Greg Daniel | 38c3d93 | 2018-03-16 14:22:30 -0400 | [diff] [blame] | 280 | false); |
| 281 | } |
| 282 | |
| 283 | // If we have any sampled images set their layout now. |
Chris Dalton | 298238a | 2019-02-21 16:28:44 -0500 | [diff] [blame] | 284 | for (int j = 0; j < cbInfo.fSampledTextures.count(); ++j) { |
| 285 | cbInfo.fSampledTextures[j]->setImageLayout( |
| 286 | fGpu, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, VK_ACCESS_SHADER_READ_BIT, |
| 287 | VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT, false); |
Greg Daniel | 38c3d93 | 2018-03-16 14:22:30 -0400 | [diff] [blame] | 288 | } |
| 289 | |
Greg Daniel | 36a77ee | 2016-10-18 10:33:25 -0400 | [diff] [blame] | 290 | SkIRect iBounds; |
| 291 | cbInfo.fBounds.roundOut(&iBounds); |
| 292 | |
Greg Daniel | 22bc865 | 2017-03-22 15:45:43 -0400 | [diff] [blame] | 293 | fGpu->submitSecondaryCommandBuffer(cbInfo.fCommandBuffers, cbInfo.fRenderPass, |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 294 | &cbInfo.fColorClearValue, vkRT, fOrigin, iBounds); |
Greg Daniel | 36a77ee | 2016-10-18 10:33:25 -0400 | [diff] [blame] | 295 | } |
| 296 | } |
Brian Salomon | 24d377e | 2019-04-23 15:24:31 -0400 | [diff] [blame] | 297 | SkASSERT(currPreCmd == fPreCommandBufferTasks.end()); |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 298 | } |
| 299 | |
Robert Phillips | 5b5d84c | 2018-08-09 15:12:18 -0400 | [diff] [blame] | 300 | void GrVkGpuRTCommandBuffer::set(GrRenderTarget* rt, GrSurfaceOrigin origin, |
| 301 | const GrGpuRTCommandBuffer::LoadAndStoreInfo& colorInfo, |
| 302 | const GrGpuRTCommandBuffer::StencilLoadAndStoreInfo& stencilInfo) { |
| 303 | SkASSERT(!fRenderTarget); |
| 304 | SkASSERT(fCommandBufferInfos.empty()); |
| 305 | SkASSERT(-1 == fCurrentCmdInfo); |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 306 | SkASSERT(fGpu == rt->getContext()->priv().getGpu()); |
Robert Phillips | 5b5d84c | 2018-08-09 15:12:18 -0400 | [diff] [blame] | 307 | SkASSERT(!fLastPipelineState); |
| 308 | |
| 309 | this->INHERITED::set(rt, origin); |
| 310 | |
Greg Daniel | 070cbaf | 2019-01-03 17:35:54 -0500 | [diff] [blame] | 311 | if (this->wrapsSecondaryCommandBuffer()) { |
| 312 | this->initWrapped(); |
| 313 | return; |
| 314 | } |
| 315 | |
Brian Osman | 9a9baae | 2018-11-05 15:06:26 -0500 | [diff] [blame] | 316 | fClearColor = colorInfo.fClearColor; |
Robert Phillips | 5b5d84c | 2018-08-09 15:12:18 -0400 | [diff] [blame] | 317 | |
| 318 | get_vk_load_store_ops(colorInfo.fLoadOp, colorInfo.fStoreOp, |
| 319 | &fVkColorLoadOp, &fVkColorStoreOp); |
| 320 | |
| 321 | get_vk_load_store_ops(stencilInfo.fLoadOp, stencilInfo.fStoreOp, |
| 322 | &fVkStencilLoadOp, &fVkStencilStoreOp); |
| 323 | |
| 324 | this->init(); |
| 325 | } |
| 326 | |
| 327 | void GrVkGpuRTCommandBuffer::reset() { |
| 328 | for (int i = 0; i < fCommandBufferInfos.count(); ++i) { |
| 329 | CommandBufferInfo& cbInfo = fCommandBufferInfos[i]; |
| 330 | for (int j = 0; j < cbInfo.fCommandBuffers.count(); ++j) { |
| 331 | cbInfo.fCommandBuffers[j]->unref(fGpu); |
| 332 | } |
| 333 | cbInfo.fRenderPass->unref(fGpu); |
| 334 | } |
| 335 | fCommandBufferInfos.reset(); |
Brian Salomon | 24d377e | 2019-04-23 15:24:31 -0400 | [diff] [blame] | 336 | fPreCommandBufferTasks.reset(); |
Robert Phillips | 5b5d84c | 2018-08-09 15:12:18 -0400 | [diff] [blame] | 337 | |
| 338 | fCurrentCmdInfo = -1; |
| 339 | |
| 340 | fLastPipelineState = nullptr; |
| 341 | fRenderTarget = nullptr; |
| 342 | } |
| 343 | |
Greg Daniel | 070cbaf | 2019-01-03 17:35:54 -0500 | [diff] [blame] | 344 | bool GrVkGpuRTCommandBuffer::wrapsSecondaryCommandBuffer() const { |
| 345 | GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(fRenderTarget); |
| 346 | return vkRT->wrapsSecondaryCommandBuffer(); |
| 347 | } |
| 348 | |
Greg Daniel | 64cc9aa | 2018-10-19 13:54:56 -0400 | [diff] [blame] | 349 | //////////////////////////////////////////////////////////////////////////////// |
| 350 | |
Greg Daniel | 500d58b | 2017-08-24 15:59:33 -0400 | [diff] [blame] | 351 | void GrVkGpuRTCommandBuffer::discard() { |
Robert Phillips | 19e51dc | 2017-08-09 09:30:51 -0400 | [diff] [blame] | 352 | GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(fRenderTarget); |
Brian Salomon | c293a29 | 2016-11-30 13:38:32 -0500 | [diff] [blame] | 353 | |
Greg Daniel | 22bc865 | 2017-03-22 15:45:43 -0400 | [diff] [blame] | 354 | CommandBufferInfo& cbInfo = fCommandBufferInfos[fCurrentCmdInfo]; |
Greg Daniel | 77b53f6 | 2016-10-18 11:48:51 -0400 | [diff] [blame] | 355 | if (cbInfo.fIsEmpty) { |
Robert Phillips | 74c627f | 2017-08-09 10:28:00 -0400 | [diff] [blame] | 356 | // Change the render pass to do a don't-care load for both color & stencil |
egdaniel | 37535c9 | 2016-06-30 08:23:30 -0700 | [diff] [blame] | 357 | GrVkRenderPass::LoadStoreOps vkColorOps(VK_ATTACHMENT_LOAD_OP_DONT_CARE, |
| 358 | VK_ATTACHMENT_STORE_OP_STORE); |
| 359 | GrVkRenderPass::LoadStoreOps vkStencilOps(VK_ATTACHMENT_LOAD_OP_DONT_CARE, |
| 360 | VK_ATTACHMENT_STORE_OP_STORE); |
egdaniel | 37535c9 | 2016-06-30 08:23:30 -0700 | [diff] [blame] | 361 | |
Greg Daniel | 36a77ee | 2016-10-18 10:33:25 -0400 | [diff] [blame] | 362 | const GrVkRenderPass* oldRP = cbInfo.fRenderPass; |
egdaniel | 37535c9 | 2016-06-30 08:23:30 -0700 | [diff] [blame] | 363 | |
egdaniel | 37535c9 | 2016-06-30 08:23:30 -0700 | [diff] [blame] | 364 | const GrVkResourceProvider::CompatibleRPHandle& rpHandle = |
Robert Phillips | 19e51dc | 2017-08-09 09:30:51 -0400 | [diff] [blame] | 365 | vkRT->compatibleRenderPassHandle(); |
egdaniel | 37535c9 | 2016-06-30 08:23:30 -0700 | [diff] [blame] | 366 | if (rpHandle.isValid()) { |
Greg Daniel | 36a77ee | 2016-10-18 10:33:25 -0400 | [diff] [blame] | 367 | cbInfo.fRenderPass = fGpu->resourceProvider().findRenderPass(rpHandle, |
| 368 | vkColorOps, |
| 369 | vkStencilOps); |
egdaniel | 37535c9 | 2016-06-30 08:23:30 -0700 | [diff] [blame] | 370 | } else { |
Robert Phillips | 19e51dc | 2017-08-09 09:30:51 -0400 | [diff] [blame] | 371 | cbInfo.fRenderPass = fGpu->resourceProvider().findRenderPass(*vkRT, |
Greg Daniel | 36a77ee | 2016-10-18 10:33:25 -0400 | [diff] [blame] | 372 | vkColorOps, |
| 373 | vkStencilOps); |
egdaniel | 37535c9 | 2016-06-30 08:23:30 -0700 | [diff] [blame] | 374 | } |
| 375 | |
Greg Daniel | 36a77ee | 2016-10-18 10:33:25 -0400 | [diff] [blame] | 376 | SkASSERT(cbInfo.fRenderPass->isCompatible(*oldRP)); |
egdaniel | 37535c9 | 2016-06-30 08:23:30 -0700 | [diff] [blame] | 377 | oldRP->unref(fGpu); |
Greg Daniel | 5011f85 | 2016-10-28 15:07:16 -0400 | [diff] [blame] | 378 | cbInfo.fBounds.join(fRenderTarget->getBoundsRect()); |
Greg Daniel | a3c68df | 2018-03-16 13:46:53 -0400 | [diff] [blame] | 379 | cbInfo.fLoadStoreState = LoadStoreState::kStartsWithDiscard; |
egdaniel | 37535c9 | 2016-06-30 08:23:30 -0700 | [diff] [blame] | 380 | } |
| 381 | } |
| 382 | |
Greg Daniel | 500d58b | 2017-08-24 15:59:33 -0400 | [diff] [blame] | 383 | void GrVkGpuRTCommandBuffer::insertEventMarker(const char* msg) { |
Robert Phillips | 65a88fa | 2017-08-08 08:36:22 -0400 | [diff] [blame] | 384 | // TODO: does Vulkan have a correlate? |
| 385 | } |
| 386 | |
Greg Daniel | 500d58b | 2017-08-24 15:59:33 -0400 | [diff] [blame] | 387 | void GrVkGpuRTCommandBuffer::onClearStencilClip(const GrFixedClip& clip, bool insideStencilMask) { |
Chris Dalton | 94c0468 | 2017-11-01 17:15:06 -0600 | [diff] [blame] | 388 | SkASSERT(!clip.hasWindowRectangles()); |
| 389 | |
Jim Van Verth | 6a40abc | 2017-11-02 16:56:09 +0000 | [diff] [blame] | 390 | CommandBufferInfo& cbInfo = fCommandBufferInfos[fCurrentCmdInfo]; |
| 391 | |
Greg Daniel | 65a0927 | 2016-10-12 09:47:22 -0400 | [diff] [blame] | 392 | GrStencilAttachment* sb = fRenderTarget->renderTargetPriv().getStencilAttachment(); |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 393 | // this should only be called internally when we know we have a |
| 394 | // stencil buffer. |
| 395 | SkASSERT(sb); |
| 396 | int stencilBitCount = sb->bits(); |
| 397 | |
| 398 | // The contract with the callers does not guarantee that we preserve all bits in the stencil |
| 399 | // during this clear. Thus we will clear the entire stencil to the desired value. |
| 400 | |
| 401 | VkClearDepthStencilValue vkStencilColor; |
| 402 | memset(&vkStencilColor, 0, sizeof(VkClearDepthStencilValue)); |
csmartdalton | 29df760 | 2016-08-31 11:55:52 -0700 | [diff] [blame] | 403 | if (insideStencilMask) { |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 404 | vkStencilColor.stencil = (1 << (stencilBitCount - 1)); |
| 405 | } else { |
| 406 | vkStencilColor.stencil = 0; |
| 407 | } |
| 408 | |
| 409 | VkClearRect clearRect; |
| 410 | // Flip rect if necessary |
csmartdalton | 29df760 | 2016-08-31 11:55:52 -0700 | [diff] [blame] | 411 | SkIRect vkRect; |
Brian Salomon | d818ebf | 2018-07-02 14:08:49 +0000 | [diff] [blame] | 412 | if (!clip.scissorEnabled()) { |
Greg Daniel | a41a74a | 2018-10-09 12:59:23 +0000 | [diff] [blame] | 413 | vkRect.setXYWH(0, 0, fRenderTarget->width(), fRenderTarget->height()); |
Robert Phillips | 4f101a7 | 2017-07-28 08:42:04 -0400 | [diff] [blame] | 414 | } else if (kBottomLeft_GrSurfaceOrigin != fOrigin) { |
csmartdalton | 29df760 | 2016-08-31 11:55:52 -0700 | [diff] [blame] | 415 | vkRect = clip.scissorRect(); |
| 416 | } else { |
| 417 | const SkIRect& scissor = clip.scissorRect(); |
Greg Daniel | 65a0927 | 2016-10-12 09:47:22 -0400 | [diff] [blame] | 418 | vkRect.setLTRB(scissor.fLeft, fRenderTarget->height() - scissor.fBottom, |
| 419 | scissor.fRight, fRenderTarget->height() - scissor.fTop); |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | clearRect.rect.offset = { vkRect.fLeft, vkRect.fTop }; |
| 423 | clearRect.rect.extent = { (uint32_t)vkRect.width(), (uint32_t)vkRect.height() }; |
| 424 | |
| 425 | clearRect.baseArrayLayer = 0; |
| 426 | clearRect.layerCount = 1; |
| 427 | |
| 428 | uint32_t stencilIndex; |
Greg Daniel | 36a77ee | 2016-10-18 10:33:25 -0400 | [diff] [blame] | 429 | SkAssertResult(cbInfo.fRenderPass->stencilAttachmentIndex(&stencilIndex)); |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 430 | |
| 431 | VkClearAttachment attachment; |
| 432 | attachment.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT; |
| 433 | attachment.colorAttachment = 0; // this value shouldn't matter |
| 434 | attachment.clearValue.depthStencil = vkStencilColor; |
| 435 | |
Greg Daniel | 22bc865 | 2017-03-22 15:45:43 -0400 | [diff] [blame] | 436 | cbInfo.currentCmdBuf()->clearAttachments(fGpu, 1, &attachment, 1, &clearRect); |
Greg Daniel | 77b53f6 | 2016-10-18 11:48:51 -0400 | [diff] [blame] | 437 | cbInfo.fIsEmpty = false; |
Greg Daniel | 36a77ee | 2016-10-18 10:33:25 -0400 | [diff] [blame] | 438 | |
| 439 | // Update command buffer bounds |
Brian Salomon | d818ebf | 2018-07-02 14:08:49 +0000 | [diff] [blame] | 440 | if (!clip.scissorEnabled()) { |
Greg Daniel | 36a77ee | 2016-10-18 10:33:25 -0400 | [diff] [blame] | 441 | cbInfo.fBounds.join(fRenderTarget->getBoundsRect()); |
| 442 | } else { |
| 443 | cbInfo.fBounds.join(SkRect::Make(clip.scissorRect())); |
| 444 | } |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 445 | } |
| 446 | |
Brian Osman | 9a9baae | 2018-11-05 15:06:26 -0500 | [diff] [blame] | 447 | void GrVkGpuRTCommandBuffer::onClear(const GrFixedClip& clip, const SkPMColor4f& color) { |
Robert Phillips | 19e51dc | 2017-08-09 09:30:51 -0400 | [diff] [blame] | 448 | GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(fRenderTarget); |
| 449 | |
Jim Van Verth | 6a40abc | 2017-11-02 16:56:09 +0000 | [diff] [blame] | 450 | // parent class should never let us get here with no RT |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 451 | SkASSERT(!clip.hasWindowRectangles()); |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 452 | |
Greg Daniel | 22bc865 | 2017-03-22 15:45:43 -0400 | [diff] [blame] | 453 | CommandBufferInfo& cbInfo = fCommandBufferInfos[fCurrentCmdInfo]; |
Greg Daniel | 36a77ee | 2016-10-18 10:33:25 -0400 | [diff] [blame] | 454 | |
Brian Osman | 9a9baae | 2018-11-05 15:06:26 -0500 | [diff] [blame] | 455 | VkClearColorValue vkColor = {{color.fR, color.fG, color.fB, color.fA}}; |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 456 | |
Brian Salomon | d818ebf | 2018-07-02 14:08:49 +0000 | [diff] [blame] | 457 | if (cbInfo.fIsEmpty && !clip.scissorEnabled()) { |
Robert Phillips | 74c627f | 2017-08-09 10:28:00 -0400 | [diff] [blame] | 458 | // Change the render pass to do a clear load |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 459 | GrVkRenderPass::LoadStoreOps vkColorOps(VK_ATTACHMENT_LOAD_OP_CLEAR, |
| 460 | VK_ATTACHMENT_STORE_OP_STORE); |
Robert Phillips | 74c627f | 2017-08-09 10:28:00 -0400 | [diff] [blame] | 461 | // Preserve the stencil buffer's load & store settings |
| 462 | GrVkRenderPass::LoadStoreOps vkStencilOps(fVkStencilLoadOp, fVkStencilStoreOp); |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 463 | |
Greg Daniel | 36a77ee | 2016-10-18 10:33:25 -0400 | [diff] [blame] | 464 | const GrVkRenderPass* oldRP = cbInfo.fRenderPass; |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 465 | |
| 466 | const GrVkResourceProvider::CompatibleRPHandle& rpHandle = |
Robert Phillips | 19e51dc | 2017-08-09 09:30:51 -0400 | [diff] [blame] | 467 | vkRT->compatibleRenderPassHandle(); |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 468 | if (rpHandle.isValid()) { |
Greg Daniel | 36a77ee | 2016-10-18 10:33:25 -0400 | [diff] [blame] | 469 | cbInfo.fRenderPass = fGpu->resourceProvider().findRenderPass(rpHandle, |
| 470 | vkColorOps, |
| 471 | vkStencilOps); |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 472 | } else { |
Robert Phillips | 19e51dc | 2017-08-09 09:30:51 -0400 | [diff] [blame] | 473 | cbInfo.fRenderPass = fGpu->resourceProvider().findRenderPass(*vkRT, |
Greg Daniel | 36a77ee | 2016-10-18 10:33:25 -0400 | [diff] [blame] | 474 | vkColorOps, |
| 475 | vkStencilOps); |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 476 | } |
| 477 | |
Greg Daniel | 36a77ee | 2016-10-18 10:33:25 -0400 | [diff] [blame] | 478 | SkASSERT(cbInfo.fRenderPass->isCompatible(*oldRP)); |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 479 | oldRP->unref(fGpu); |
| 480 | |
Brian Osman | 9a9baae | 2018-11-05 15:06:26 -0500 | [diff] [blame] | 481 | cbInfo.fColorClearValue.color = {{color.fR, color.fG, color.fB, color.fA}}; |
Greg Daniel | a3c68df | 2018-03-16 13:46:53 -0400 | [diff] [blame] | 482 | cbInfo.fLoadStoreState = LoadStoreState::kStartsWithClear; |
Greg Daniel | 36a77ee | 2016-10-18 10:33:25 -0400 | [diff] [blame] | 483 | // Update command buffer bounds |
| 484 | cbInfo.fBounds.join(fRenderTarget->getBoundsRect()); |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 485 | return; |
| 486 | } |
| 487 | |
| 488 | // We always do a sub rect clear with clearAttachments since we are inside a render pass |
| 489 | VkClearRect clearRect; |
| 490 | // Flip rect if necessary |
csmartdalton | 29df760 | 2016-08-31 11:55:52 -0700 | [diff] [blame] | 491 | SkIRect vkRect; |
Brian Salomon | d818ebf | 2018-07-02 14:08:49 +0000 | [diff] [blame] | 492 | if (!clip.scissorEnabled()) { |
Greg Daniel | a41a74a | 2018-10-09 12:59:23 +0000 | [diff] [blame] | 493 | vkRect.setXYWH(0, 0, fRenderTarget->width(), fRenderTarget->height()); |
Robert Phillips | 4f101a7 | 2017-07-28 08:42:04 -0400 | [diff] [blame] | 494 | } else if (kBottomLeft_GrSurfaceOrigin != fOrigin) { |
csmartdalton | 29df760 | 2016-08-31 11:55:52 -0700 | [diff] [blame] | 495 | vkRect = clip.scissorRect(); |
| 496 | } else { |
| 497 | const SkIRect& scissor = clip.scissorRect(); |
Greg Daniel | 65a0927 | 2016-10-12 09:47:22 -0400 | [diff] [blame] | 498 | vkRect.setLTRB(scissor.fLeft, fRenderTarget->height() - scissor.fBottom, |
| 499 | scissor.fRight, fRenderTarget->height() - scissor.fTop); |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 500 | } |
| 501 | clearRect.rect.offset = { vkRect.fLeft, vkRect.fTop }; |
| 502 | clearRect.rect.extent = { (uint32_t)vkRect.width(), (uint32_t)vkRect.height() }; |
| 503 | clearRect.baseArrayLayer = 0; |
| 504 | clearRect.layerCount = 1; |
| 505 | |
| 506 | uint32_t colorIndex; |
Greg Daniel | 36a77ee | 2016-10-18 10:33:25 -0400 | [diff] [blame] | 507 | SkAssertResult(cbInfo.fRenderPass->colorAttachmentIndex(&colorIndex)); |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 508 | |
| 509 | VkClearAttachment attachment; |
| 510 | attachment.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; |
| 511 | attachment.colorAttachment = colorIndex; |
| 512 | attachment.clearValue.color = vkColor; |
| 513 | |
Greg Daniel | 22bc865 | 2017-03-22 15:45:43 -0400 | [diff] [blame] | 514 | cbInfo.currentCmdBuf()->clearAttachments(fGpu, 1, &attachment, 1, &clearRect); |
Greg Daniel | 77b53f6 | 2016-10-18 11:48:51 -0400 | [diff] [blame] | 515 | cbInfo.fIsEmpty = false; |
Greg Daniel | 36a77ee | 2016-10-18 10:33:25 -0400 | [diff] [blame] | 516 | |
| 517 | // Update command buffer bounds |
Brian Salomon | d818ebf | 2018-07-02 14:08:49 +0000 | [diff] [blame] | 518 | if (!clip.scissorEnabled()) { |
Greg Daniel | 36a77ee | 2016-10-18 10:33:25 -0400 | [diff] [blame] | 519 | cbInfo.fBounds.join(fRenderTarget->getBoundsRect()); |
| 520 | } else { |
| 521 | cbInfo.fBounds.join(SkRect::Make(clip.scissorRect())); |
| 522 | } |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 523 | return; |
| 524 | } |
| 525 | |
Greg Daniel | 500d58b | 2017-08-24 15:59:33 -0400 | [diff] [blame] | 526 | //////////////////////////////////////////////////////////////////////////////// |
| 527 | |
| 528 | void GrVkGpuRTCommandBuffer::addAdditionalCommandBuffer() { |
Robert Phillips | 19e51dc | 2017-08-09 09:30:51 -0400 | [diff] [blame] | 529 | GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(fRenderTarget); |
| 530 | |
Greg Daniel | 22bc865 | 2017-03-22 15:45:43 -0400 | [diff] [blame] | 531 | CommandBufferInfo& cbInfo = fCommandBufferInfos[fCurrentCmdInfo]; |
| 532 | cbInfo.currentCmdBuf()->end(fGpu); |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 533 | cbInfo.fCommandBuffers.push_back(fGpu->cmdPool()->findOrCreateSecondaryCommandBuffer(fGpu)); |
Robert Phillips | 19e51dc | 2017-08-09 09:30:51 -0400 | [diff] [blame] | 534 | cbInfo.currentCmdBuf()->begin(fGpu, vkRT->framebuffer(), cbInfo.fRenderPass); |
Greg Daniel | 22bc865 | 2017-03-22 15:45:43 -0400 | [diff] [blame] | 535 | } |
| 536 | |
Greg Daniel | 500d58b | 2017-08-24 15:59:33 -0400 | [diff] [blame] | 537 | void GrVkGpuRTCommandBuffer::addAdditionalRenderPass() { |
Robert Phillips | 19e51dc | 2017-08-09 09:30:51 -0400 | [diff] [blame] | 538 | GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(fRenderTarget); |
| 539 | |
Greg Daniel | 22bc865 | 2017-03-22 15:45:43 -0400 | [diff] [blame] | 540 | fCommandBufferInfos[fCurrentCmdInfo].currentCmdBuf()->end(fGpu); |
Greg Daniel | 77b53f6 | 2016-10-18 11:48:51 -0400 | [diff] [blame] | 541 | |
| 542 | CommandBufferInfo& cbInfo = fCommandBufferInfos.push_back(); |
Greg Daniel | 22bc865 | 2017-03-22 15:45:43 -0400 | [diff] [blame] | 543 | fCurrentCmdInfo++; |
Greg Daniel | 77b53f6 | 2016-10-18 11:48:51 -0400 | [diff] [blame] | 544 | |
| 545 | GrVkRenderPass::LoadStoreOps vkColorOps(VK_ATTACHMENT_LOAD_OP_LOAD, |
| 546 | VK_ATTACHMENT_STORE_OP_STORE); |
| 547 | GrVkRenderPass::LoadStoreOps vkStencilOps(VK_ATTACHMENT_LOAD_OP_LOAD, |
| 548 | VK_ATTACHMENT_STORE_OP_STORE); |
| 549 | |
| 550 | const GrVkResourceProvider::CompatibleRPHandle& rpHandle = |
Robert Phillips | 19e51dc | 2017-08-09 09:30:51 -0400 | [diff] [blame] | 551 | vkRT->compatibleRenderPassHandle(); |
Greg Daniel | 77b53f6 | 2016-10-18 11:48:51 -0400 | [diff] [blame] | 552 | if (rpHandle.isValid()) { |
| 553 | cbInfo.fRenderPass = fGpu->resourceProvider().findRenderPass(rpHandle, |
| 554 | vkColorOps, |
| 555 | vkStencilOps); |
| 556 | } else { |
Robert Phillips | 19e51dc | 2017-08-09 09:30:51 -0400 | [diff] [blame] | 557 | cbInfo.fRenderPass = fGpu->resourceProvider().findRenderPass(*vkRT, |
Greg Daniel | 77b53f6 | 2016-10-18 11:48:51 -0400 | [diff] [blame] | 558 | vkColorOps, |
| 559 | vkStencilOps); |
| 560 | } |
Greg Daniel | a3c68df | 2018-03-16 13:46:53 -0400 | [diff] [blame] | 561 | cbInfo.fLoadStoreState = LoadStoreState::kLoadAndStore; |
Greg Daniel | 77b53f6 | 2016-10-18 11:48:51 -0400 | [diff] [blame] | 562 | |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 563 | cbInfo.fCommandBuffers.push_back(fGpu->cmdPool()->findOrCreateSecondaryCommandBuffer(fGpu)); |
Greg Daniel | 77b53f6 | 2016-10-18 11:48:51 -0400 | [diff] [blame] | 564 | // It shouldn't matter what we set the clear color to here since we will assume loading of the |
| 565 | // attachment. |
| 566 | memset(&cbInfo.fColorClearValue, 0, sizeof(VkClearValue)); |
| 567 | cbInfo.fBounds.setEmpty(); |
Greg Daniel | 77b53f6 | 2016-10-18 11:48:51 -0400 | [diff] [blame] | 568 | |
Robert Phillips | 19e51dc | 2017-08-09 09:30:51 -0400 | [diff] [blame] | 569 | cbInfo.currentCmdBuf()->begin(fGpu, vkRT->framebuffer(), cbInfo.fRenderPass); |
Greg Daniel | 77b53f6 | 2016-10-18 11:48:51 -0400 | [diff] [blame] | 570 | } |
| 571 | |
Brian Salomon | 943ed79 | 2017-10-30 09:37:55 -0400 | [diff] [blame] | 572 | void GrVkGpuRTCommandBuffer::inlineUpload(GrOpFlushState* state, |
| 573 | GrDeferredTextureUploadFn& upload) { |
Greg Daniel | 22bc865 | 2017-03-22 15:45:43 -0400 | [diff] [blame] | 574 | if (!fCommandBufferInfos[fCurrentCmdInfo].fIsEmpty) { |
| 575 | this->addAdditionalRenderPass(); |
Greg Daniel | 77b53f6 | 2016-10-18 11:48:51 -0400 | [diff] [blame] | 576 | } |
Brian Salomon | 24d377e | 2019-04-23 15:24:31 -0400 | [diff] [blame] | 577 | |
Brian Salomon | 24d377e | 2019-04-23 15:24:31 -0400 | [diff] [blame] | 578 | fPreCommandBufferTasks.emplace<InlineUpload>(state, upload); |
| 579 | ++fCommandBufferInfos[fCurrentCmdInfo].fNumPreCmds; |
Greg Daniel | 77b53f6 | 2016-10-18 11:48:51 -0400 | [diff] [blame] | 580 | } |
| 581 | |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 582 | void GrVkGpuRTCommandBuffer::copy(GrSurface* src, GrSurfaceOrigin srcOrigin, const SkIRect& srcRect, |
Greg Daniel | 500d58b | 2017-08-24 15:59:33 -0400 | [diff] [blame] | 583 | const SkIPoint& dstPoint) { |
Greg Daniel | a3c68df | 2018-03-16 13:46:53 -0400 | [diff] [blame] | 584 | CommandBufferInfo& cbInfo = fCommandBufferInfos[fCurrentCmdInfo]; |
| 585 | if (!cbInfo.fIsEmpty || LoadStoreState::kStartsWithClear == cbInfo.fLoadStoreState) { |
Greg Daniel | 500d58b | 2017-08-24 15:59:33 -0400 | [diff] [blame] | 586 | this->addAdditionalRenderPass(); |
| 587 | } |
Greg Daniel | a3c68df | 2018-03-16 13:46:53 -0400 | [diff] [blame] | 588 | |
Brian Salomon | 24d377e | 2019-04-23 15:24:31 -0400 | [diff] [blame] | 589 | fPreCommandBufferTasks.emplace<Copy>( |
Greg Daniel | 55fa647 | 2018-03-16 16:13:10 -0400 | [diff] [blame] | 590 | src, srcOrigin, srcRect, dstPoint, |
| 591 | LoadStoreState::kStartsWithDiscard == cbInfo.fLoadStoreState); |
Brian Salomon | 24d377e | 2019-04-23 15:24:31 -0400 | [diff] [blame] | 592 | ++fCommandBufferInfos[fCurrentCmdInfo].fNumPreCmds; |
Greg Daniel | 55fa647 | 2018-03-16 16:13:10 -0400 | [diff] [blame] | 593 | |
Greg Daniel | a3c68df | 2018-03-16 13:46:53 -0400 | [diff] [blame] | 594 | if (LoadStoreState::kLoadAndStore != cbInfo.fLoadStoreState) { |
| 595 | // Change the render pass to do a load and store so we don't lose the results of our copy |
| 596 | GrVkRenderPass::LoadStoreOps vkColorOps(VK_ATTACHMENT_LOAD_OP_LOAD, |
| 597 | VK_ATTACHMENT_STORE_OP_STORE); |
| 598 | GrVkRenderPass::LoadStoreOps vkStencilOps(VK_ATTACHMENT_LOAD_OP_LOAD, |
| 599 | VK_ATTACHMENT_STORE_OP_STORE); |
| 600 | |
| 601 | const GrVkRenderPass* oldRP = cbInfo.fRenderPass; |
| 602 | |
| 603 | GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(fRenderTarget); |
| 604 | const GrVkResourceProvider::CompatibleRPHandle& rpHandle = |
| 605 | vkRT->compatibleRenderPassHandle(); |
| 606 | if (rpHandle.isValid()) { |
| 607 | cbInfo.fRenderPass = fGpu->resourceProvider().findRenderPass(rpHandle, |
| 608 | vkColorOps, |
| 609 | vkStencilOps); |
| 610 | } else { |
| 611 | cbInfo.fRenderPass = fGpu->resourceProvider().findRenderPass(*vkRT, |
| 612 | vkColorOps, |
| 613 | vkStencilOps); |
| 614 | } |
| 615 | SkASSERT(cbInfo.fRenderPass->isCompatible(*oldRP)); |
| 616 | oldRP->unref(fGpu); |
| 617 | |
| 618 | cbInfo.fLoadStoreState = LoadStoreState::kLoadAndStore; |
| 619 | |
| 620 | } |
Greg Daniel | 500d58b | 2017-08-24 15:59:33 -0400 | [diff] [blame] | 621 | } |
| 622 | |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 623 | //////////////////////////////////////////////////////////////////////////////// |
| 624 | |
Brian Salomon | dbf7072 | 2019-02-07 11:31:24 -0500 | [diff] [blame] | 625 | void GrVkGpuRTCommandBuffer::bindGeometry(const GrGpuBuffer* indexBuffer, |
| 626 | const GrGpuBuffer* vertexBuffer, |
| 627 | const GrGpuBuffer* instanceBuffer) { |
Chris Dalton | ff92650 | 2017-05-03 14:36:54 -0400 | [diff] [blame] | 628 | GrVkSecondaryCommandBuffer* currCmdBuf = fCommandBufferInfos[fCurrentCmdInfo].currentCmdBuf(); |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 629 | // There is no need to put any memory barriers to make sure host writes have finished here. |
| 630 | // When a command buffer is submitted to a queue, there is an implicit memory barrier that |
| 631 | // occurs for all host writes. Additionally, BufferMemoryBarriers are not allowed inside of |
| 632 | // an active RenderPass. |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 633 | |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 634 | // Here our vertex and instance inputs need to match the same 0-based bindings they were |
| 635 | // assigned in GrVkPipeline. That is, vertex first (if any) followed by instance. |
| 636 | uint32_t binding = 0; |
| 637 | |
Brian Salomon | 802cb31 | 2018-06-08 18:05:20 -0400 | [diff] [blame] | 638 | if (vertexBuffer) { |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 639 | SkASSERT(vertexBuffer); |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 640 | SkASSERT(!vertexBuffer->isMapped()); |
| 641 | |
| 642 | currCmdBuf->bindInputBuffer(fGpu, binding++, |
| 643 | static_cast<const GrVkVertexBuffer*>(vertexBuffer)); |
| 644 | } |
| 645 | |
Brian Salomon | 802cb31 | 2018-06-08 18:05:20 -0400 | [diff] [blame] | 646 | if (instanceBuffer) { |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 647 | SkASSERT(instanceBuffer); |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 648 | SkASSERT(!instanceBuffer->isMapped()); |
| 649 | |
| 650 | currCmdBuf->bindInputBuffer(fGpu, binding++, |
| 651 | static_cast<const GrVkVertexBuffer*>(instanceBuffer)); |
| 652 | } |
Chris Dalton | ff92650 | 2017-05-03 14:36:54 -0400 | [diff] [blame] | 653 | if (indexBuffer) { |
| 654 | SkASSERT(indexBuffer); |
| 655 | SkASSERT(!indexBuffer->isMapped()); |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 656 | |
Chris Dalton | ff92650 | 2017-05-03 14:36:54 -0400 | [diff] [blame] | 657 | currCmdBuf->bindIndexBuffer(fGpu, static_cast<const GrVkIndexBuffer*>(indexBuffer)); |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 658 | } |
| 659 | } |
| 660 | |
Brian Salomon | 4934890 | 2018-06-26 09:12:38 -0400 | [diff] [blame] | 661 | GrVkPipelineState* GrVkGpuRTCommandBuffer::prepareDrawState( |
| 662 | const GrPrimitiveProcessor& primProc, |
| 663 | const GrPipeline& pipeline, |
| 664 | const GrPipeline::FixedDynamicState* fixedDynamicState, |
| 665 | const GrPipeline::DynamicStateArrays* dynamicStateArrays, |
| 666 | GrPrimitiveType primitiveType) { |
Greg Daniel | 22bc865 | 2017-03-22 15:45:43 -0400 | [diff] [blame] | 667 | CommandBufferInfo& cbInfo = fCommandBufferInfos[fCurrentCmdInfo]; |
| 668 | SkASSERT(cbInfo.fRenderPass); |
Greg Daniel | 36a77ee | 2016-10-18 10:33:25 -0400 | [diff] [blame] | 669 | |
Greg Daniel | 99b88e0 | 2018-10-03 15:31:20 -0400 | [diff] [blame] | 670 | VkRenderPass compatibleRenderPass = cbInfo.fRenderPass->vkRenderPass(); |
| 671 | |
Greg Daniel | 9a51a86 | 2018-11-30 10:18:14 -0500 | [diff] [blame] | 672 | const GrTextureProxy* const* primProcProxies = nullptr; |
| 673 | if (dynamicStateArrays && dynamicStateArrays->fPrimitiveProcessorTextures) { |
| 674 | primProcProxies = dynamicStateArrays->fPrimitiveProcessorTextures; |
| 675 | } else if (fixedDynamicState) { |
| 676 | primProcProxies = fixedDynamicState->fPrimitiveProcessorTextures; |
| 677 | } |
| 678 | |
| 679 | SkASSERT(SkToBool(primProcProxies) == SkToBool(primProc.numTextureSamplers())); |
| 680 | |
Greg Daniel | 09eeefb | 2017-10-16 15:15:02 -0400 | [diff] [blame] | 681 | GrVkPipelineState* pipelineState = |
Robert Phillips | d0fe875 | 2019-01-31 14:13:59 -0500 | [diff] [blame] | 682 | fGpu->resourceProvider().findOrCreateCompatiblePipelineState(fRenderTarget, fOrigin, |
| 683 | pipeline, |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 684 | primProc, |
Greg Daniel | 9a51a86 | 2018-11-30 10:18:14 -0500 | [diff] [blame] | 685 | primProcProxies, |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 686 | primitiveType, |
Greg Daniel | 99b88e0 | 2018-10-03 15:31:20 -0400 | [diff] [blame] | 687 | compatibleRenderPass); |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 688 | if (!pipelineState) { |
| 689 | return pipelineState; |
| 690 | } |
| 691 | |
Greg Daniel | 22bc865 | 2017-03-22 15:45:43 -0400 | [diff] [blame] | 692 | if (!cbInfo.fIsEmpty && |
Greg Daniel | 09eeefb | 2017-10-16 15:15:02 -0400 | [diff] [blame] | 693 | fLastPipelineState && fLastPipelineState != pipelineState && |
Greg Daniel | e3cd691 | 2017-05-17 11:15:55 -0400 | [diff] [blame] | 694 | fGpu->vkCaps().newCBOnPipelineChange()) { |
Greg Daniel | 22bc865 | 2017-03-22 15:45:43 -0400 | [diff] [blame] | 695 | this->addAdditionalCommandBuffer(); |
| 696 | } |
Greg Daniel | 09eeefb | 2017-10-16 15:15:02 -0400 | [diff] [blame] | 697 | fLastPipelineState = pipelineState; |
Greg Daniel | 22bc865 | 2017-03-22 15:45:43 -0400 | [diff] [blame] | 698 | |
Brian Salomon | f723264 | 2018-09-19 08:58:08 -0400 | [diff] [blame] | 699 | pipelineState->bindPipeline(fGpu, cbInfo.currentCmdBuf()); |
Brian Salomon | cd7907b | 2018-08-30 08:36:18 -0400 | [diff] [blame] | 700 | |
Robert Phillips | d0fe875 | 2019-01-31 14:13:59 -0500 | [diff] [blame] | 701 | pipelineState->setAndBindUniforms(fGpu, fRenderTarget, fOrigin, |
| 702 | primProc, pipeline, cbInfo.currentCmdBuf()); |
Brian Salomon | f723264 | 2018-09-19 08:58:08 -0400 | [diff] [blame] | 703 | |
| 704 | // Check whether we need to bind textures between each GrMesh. If not we can bind them all now. |
| 705 | bool setTextures = !(dynamicStateArrays && dynamicStateArrays->fPrimitiveProcessorTextures); |
| 706 | if (setTextures) { |
Brian Salomon | f723264 | 2018-09-19 08:58:08 -0400 | [diff] [blame] | 707 | pipelineState->setAndBindTextures(fGpu, primProc, pipeline, primProcProxies, |
| 708 | cbInfo.currentCmdBuf()); |
| 709 | } |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 710 | |
Brian Salomon | d818ebf | 2018-07-02 14:08:49 +0000 | [diff] [blame] | 711 | if (!pipeline.isScissorEnabled()) { |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 712 | GrVkPipeline::SetDynamicScissorRectState(fGpu, cbInfo.currentCmdBuf(), |
Robert Phillips | d0fe875 | 2019-01-31 14:13:59 -0500 | [diff] [blame] | 713 | fRenderTarget, fOrigin, |
| 714 | SkIRect::MakeWH(fRenderTarget->width(), |
| 715 | fRenderTarget->height())); |
Brian Salomon | 4934890 | 2018-06-26 09:12:38 -0400 | [diff] [blame] | 716 | } else if (!dynamicStateArrays || !dynamicStateArrays->fScissorRects) { |
| 717 | SkASSERT(fixedDynamicState); |
Robert Phillips | d0fe875 | 2019-01-31 14:13:59 -0500 | [diff] [blame] | 718 | GrVkPipeline::SetDynamicScissorRectState(fGpu, cbInfo.currentCmdBuf(), fRenderTarget, |
| 719 | fOrigin, |
Brian Salomon | 4934890 | 2018-06-26 09:12:38 -0400 | [diff] [blame] | 720 | fixedDynamicState->fScissorRect); |
Chris Dalton | 46983b7 | 2017-06-06 12:27:16 -0600 | [diff] [blame] | 721 | } |
Robert Phillips | d0fe875 | 2019-01-31 14:13:59 -0500 | [diff] [blame] | 722 | GrVkPipeline::SetDynamicViewportState(fGpu, cbInfo.currentCmdBuf(), fRenderTarget); |
| 723 | GrVkPipeline::SetDynamicBlendConstantState(fGpu, cbInfo.currentCmdBuf(), |
| 724 | fRenderTarget->config(), |
Chris Dalton | 46983b7 | 2017-06-06 12:27:16 -0600 | [diff] [blame] | 725 | pipeline.getXferProcessor()); |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 726 | |
| 727 | return pipelineState; |
| 728 | } |
| 729 | |
Brian Salomon | ff168d9 | 2018-06-23 15:17:27 -0400 | [diff] [blame] | 730 | void GrVkGpuRTCommandBuffer::onDraw(const GrPrimitiveProcessor& primProc, |
| 731 | const GrPipeline& pipeline, |
Brian Salomon | 4934890 | 2018-06-26 09:12:38 -0400 | [diff] [blame] | 732 | const GrPipeline::FixedDynamicState* fixedDynamicState, |
| 733 | const GrPipeline::DynamicStateArrays* dynamicStateArrays, |
Greg Daniel | 500d58b | 2017-08-24 15:59:33 -0400 | [diff] [blame] | 734 | const GrMesh meshes[], |
Greg Daniel | 500d58b | 2017-08-24 15:59:33 -0400 | [diff] [blame] | 735 | int meshCount, |
| 736 | const SkRect& bounds) { |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 737 | if (!meshCount) { |
| 738 | return; |
| 739 | } |
Greg Daniel | ea022cd | 2018-03-16 11:10:03 -0400 | [diff] [blame] | 740 | |
| 741 | CommandBufferInfo& cbInfo = fCommandBufferInfos[fCurrentCmdInfo]; |
| 742 | |
Brian Salomon | e782f84 | 2018-07-31 13:53:11 -0400 | [diff] [blame] | 743 | auto prepareSampledImage = [&](GrTexture* texture, GrSamplerState::Filter filter) { |
| 744 | GrVkTexture* vkTexture = static_cast<GrVkTexture*>(texture); |
| 745 | // We may need to resolve the texture first if it is also a render target |
| 746 | GrVkRenderTarget* texRT = static_cast<GrVkRenderTarget*>(vkTexture->asRenderTarget()); |
| 747 | if (texRT) { |
Greg Daniel | 0a77f43 | 2018-12-06 11:23:32 -0500 | [diff] [blame] | 748 | fGpu->resolveRenderTargetNoFlush(texRT); |
Brian Salomon | e782f84 | 2018-07-31 13:53:11 -0400 | [diff] [blame] | 749 | } |
| 750 | |
| 751 | // Check if we need to regenerate any mip maps |
| 752 | if (GrSamplerState::Filter::kMipMap == filter && |
| 753 | (vkTexture->width() != 1 || vkTexture->height() != 1)) { |
| 754 | SkASSERT(vkTexture->texturePriv().mipMapped() == GrMipMapped::kYes); |
| 755 | if (vkTexture->texturePriv().mipMapsAreDirty()) { |
| 756 | fGpu->regenerateMipMapLevels(vkTexture); |
| 757 | } |
| 758 | } |
Brian Salomon | 5fd1057 | 2019-04-01 12:07:05 -0400 | [diff] [blame] | 759 | cbInfo.fSampledTextures.push_back(vkTexture); |
Brian Salomon | e782f84 | 2018-07-31 13:53:11 -0400 | [diff] [blame] | 760 | }; |
| 761 | |
Brian Salomon | f723264 | 2018-09-19 08:58:08 -0400 | [diff] [blame] | 762 | if (dynamicStateArrays && dynamicStateArrays->fPrimitiveProcessorTextures) { |
| 763 | for (int m = 0, i = 0; m < meshCount; ++m) { |
| 764 | for (int s = 0; s < primProc.numTextureSamplers(); ++s, ++i) { |
| 765 | auto texture = dynamicStateArrays->fPrimitiveProcessorTextures[i]->peekTexture(); |
| 766 | prepareSampledImage(texture, primProc.textureSampler(s).samplerState().filter()); |
| 767 | } |
| 768 | } |
| 769 | } else { |
| 770 | for (int i = 0; i < primProc.numTextureSamplers(); ++i) { |
| 771 | auto texture = fixedDynamicState->fPrimitiveProcessorTextures[i]->peekTexture(); |
| 772 | prepareSampledImage(texture, primProc.textureSampler(i).samplerState().filter()); |
| 773 | } |
Brian Salomon | e782f84 | 2018-07-31 13:53:11 -0400 | [diff] [blame] | 774 | } |
bsalomon | b58a2b4 | 2016-09-26 06:55:02 -0700 | [diff] [blame] | 775 | GrFragmentProcessor::Iter iter(pipeline); |
| 776 | while (const GrFragmentProcessor* fp = iter.next()) { |
Brian Salomon | e782f84 | 2018-07-31 13:53:11 -0400 | [diff] [blame] | 777 | for (int i = 0; i < fp->numTextureSamplers(); ++i) { |
| 778 | const GrFragmentProcessor::TextureSampler& sampler = fp->textureSampler(i); |
| 779 | prepareSampledImage(sampler.peekTexture(), sampler.samplerState().filter()); |
| 780 | } |
egdaniel | 2f5792a | 2016-07-06 08:51:23 -0700 | [diff] [blame] | 781 | } |
Robert Phillips | bb581ce | 2017-05-29 15:05:15 -0400 | [diff] [blame] | 782 | if (GrTexture* dstTexture = pipeline.peekDstTexture()) { |
Chris Dalton | 298238a | 2019-02-21 16:28:44 -0500 | [diff] [blame] | 783 | cbInfo.fSampledTextures.push_back(sk_ref_sp(static_cast<GrVkTexture*>(dstTexture))); |
Brian Salomon | 18dfa98 | 2017-04-03 16:57:43 -0400 | [diff] [blame] | 784 | } |
egdaniel | 2f5792a | 2016-07-06 08:51:23 -0700 | [diff] [blame] | 785 | |
Chris Dalton | bca46e2 | 2017-05-15 11:03:26 -0600 | [diff] [blame] | 786 | GrPrimitiveType primitiveType = meshes[0].primitiveType(); |
Brian Salomon | 4934890 | 2018-06-26 09:12:38 -0400 | [diff] [blame] | 787 | GrVkPipelineState* pipelineState = this->prepareDrawState(primProc, pipeline, fixedDynamicState, |
| 788 | dynamicStateArrays, primitiveType); |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 789 | if (!pipelineState) { |
| 790 | return; |
| 791 | } |
| 792 | |
Brian Salomon | d818ebf | 2018-07-02 14:08:49 +0000 | [diff] [blame] | 793 | bool dynamicScissor = |
| 794 | pipeline.isScissorEnabled() && dynamicStateArrays && dynamicStateArrays->fScissorRects; |
Brian Salomon | f723264 | 2018-09-19 08:58:08 -0400 | [diff] [blame] | 795 | bool dynamicTextures = dynamicStateArrays && dynamicStateArrays->fPrimitiveProcessorTextures; |
Brian Salomon | 4934890 | 2018-06-26 09:12:38 -0400 | [diff] [blame] | 796 | |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 797 | for (int i = 0; i < meshCount; ++i) { |
| 798 | const GrMesh& mesh = meshes[i]; |
Chris Dalton | bca46e2 | 2017-05-15 11:03:26 -0600 | [diff] [blame] | 799 | if (mesh.primitiveType() != primitiveType) { |
Chris Dalton | 6f24180 | 2017-05-08 13:58:38 -0400 | [diff] [blame] | 800 | SkDEBUGCODE(pipelineState = nullptr); |
Chris Dalton | bca46e2 | 2017-05-15 11:03:26 -0600 | [diff] [blame] | 801 | primitiveType = mesh.primitiveType(); |
Brian Salomon | 4934890 | 2018-06-26 09:12:38 -0400 | [diff] [blame] | 802 | pipelineState = this->prepareDrawState(primProc, pipeline, fixedDynamicState, |
| 803 | dynamicStateArrays, primitiveType); |
Chris Dalton | 6f24180 | 2017-05-08 13:58:38 -0400 | [diff] [blame] | 804 | if (!pipelineState) { |
| 805 | return; |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 806 | } |
Chris Dalton | 6f24180 | 2017-05-08 13:58:38 -0400 | [diff] [blame] | 807 | } |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 808 | |
Brian Salomon | 4934890 | 2018-06-26 09:12:38 -0400 | [diff] [blame] | 809 | if (dynamicScissor) { |
| 810 | GrVkPipeline::SetDynamicScissorRectState(fGpu, cbInfo.currentCmdBuf(), fRenderTarget, |
Robert Phillips | d0fe875 | 2019-01-31 14:13:59 -0500 | [diff] [blame] | 811 | fOrigin, |
Brian Salomon | 4934890 | 2018-06-26 09:12:38 -0400 | [diff] [blame] | 812 | dynamicStateArrays->fScissorRects[i]); |
Chris Dalton | 46983b7 | 2017-06-06 12:27:16 -0600 | [diff] [blame] | 813 | } |
Brian Salomon | f723264 | 2018-09-19 08:58:08 -0400 | [diff] [blame] | 814 | if (dynamicTextures) { |
| 815 | GrTextureProxy* const* meshProxies = dynamicStateArrays->fPrimitiveProcessorTextures + |
| 816 | primProc.numTextureSamplers() * i; |
| 817 | pipelineState->setAndBindTextures(fGpu, primProc, pipeline, meshProxies, |
| 818 | cbInfo.currentCmdBuf()); |
| 819 | } |
Chris Dalton | bca46e2 | 2017-05-15 11:03:26 -0600 | [diff] [blame] | 820 | SkASSERT(pipelineState); |
Brian Salomon | 802cb31 | 2018-06-08 18:05:20 -0400 | [diff] [blame] | 821 | mesh.sendToGpu(this); |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 822 | } |
| 823 | |
Greg Daniel | 36a77ee | 2016-10-18 10:33:25 -0400 | [diff] [blame] | 824 | cbInfo.fBounds.join(bounds); |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 825 | cbInfo.fIsEmpty = false; |
egdaniel | 066df7c | 2016-06-08 14:02:27 -0700 | [diff] [blame] | 826 | } |
| 827 | |
Brian Salomon | 802cb31 | 2018-06-08 18:05:20 -0400 | [diff] [blame] | 828 | void GrVkGpuRTCommandBuffer::sendInstancedMeshToGpu(GrPrimitiveType, |
Greg Daniel | 500d58b | 2017-08-24 15:59:33 -0400 | [diff] [blame] | 829 | const GrBuffer* vertexBuffer, |
| 830 | int vertexCount, |
| 831 | int baseVertex, |
| 832 | const GrBuffer* instanceBuffer, |
| 833 | int instanceCount, |
| 834 | int baseInstance) { |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 835 | CommandBufferInfo& cbInfo = fCommandBufferInfos[fCurrentCmdInfo]; |
Brian Salomon | dbf7072 | 2019-02-07 11:31:24 -0500 | [diff] [blame] | 836 | SkASSERT(!vertexBuffer || !vertexBuffer->isCpuBuffer()); |
| 837 | SkASSERT(!instanceBuffer || !instanceBuffer->isCpuBuffer()); |
| 838 | auto gpuVertexBuffer = static_cast<const GrGpuBuffer*>(vertexBuffer); |
| 839 | auto gpuInstanceBuffer = static_cast<const GrGpuBuffer*>(instanceBuffer); |
| 840 | this->bindGeometry(nullptr, gpuVertexBuffer, gpuInstanceBuffer); |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 841 | cbInfo.currentCmdBuf()->draw(fGpu, vertexCount, instanceCount, baseVertex, baseInstance); |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 842 | fGpu->stats()->incNumDraws(); |
| 843 | } |
| 844 | |
Brian Salomon | 802cb31 | 2018-06-08 18:05:20 -0400 | [diff] [blame] | 845 | void GrVkGpuRTCommandBuffer::sendIndexedInstancedMeshToGpu(GrPrimitiveType, |
Greg Daniel | 500d58b | 2017-08-24 15:59:33 -0400 | [diff] [blame] | 846 | const GrBuffer* indexBuffer, |
| 847 | int indexCount, |
| 848 | int baseIndex, |
| 849 | const GrBuffer* vertexBuffer, |
| 850 | int baseVertex, |
| 851 | const GrBuffer* instanceBuffer, |
| 852 | int instanceCount, |
Brian Salomon | 802cb31 | 2018-06-08 18:05:20 -0400 | [diff] [blame] | 853 | int baseInstance, |
| 854 | GrPrimitiveRestart restart) { |
| 855 | SkASSERT(restart == GrPrimitiveRestart::kNo); |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 856 | CommandBufferInfo& cbInfo = fCommandBufferInfos[fCurrentCmdInfo]; |
Brian Salomon | dbf7072 | 2019-02-07 11:31:24 -0500 | [diff] [blame] | 857 | SkASSERT(!vertexBuffer || !vertexBuffer->isCpuBuffer()); |
| 858 | SkASSERT(!instanceBuffer || !instanceBuffer->isCpuBuffer()); |
| 859 | SkASSERT(!indexBuffer->isCpuBuffer()); |
| 860 | auto gpuIndexxBuffer = static_cast<const GrGpuBuffer*>(indexBuffer); |
| 861 | auto gpuVertexBuffer = static_cast<const GrGpuBuffer*>(vertexBuffer); |
| 862 | auto gpuInstanceBuffer = static_cast<const GrGpuBuffer*>(instanceBuffer); |
| 863 | this->bindGeometry(gpuIndexxBuffer, gpuVertexBuffer, gpuInstanceBuffer); |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 864 | cbInfo.currentCmdBuf()->drawIndexed(fGpu, indexCount, instanceCount, |
| 865 | baseIndex, baseVertex, baseInstance); |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 866 | fGpu->stats()->incNumDraws(); |
| 867 | } |
Greg Daniel | 64cc9aa | 2018-10-19 13:54:56 -0400 | [diff] [blame] | 868 | |
| 869 | //////////////////////////////////////////////////////////////////////////////// |
| 870 | |
| 871 | void GrVkGpuRTCommandBuffer::executeDrawable(std::unique_ptr<SkDrawable::GpuDrawHandler> drawable) { |
| 872 | GrVkRenderTarget* target = static_cast<GrVkRenderTarget*>(fRenderTarget); |
| 873 | |
| 874 | GrVkImage* targetImage = target->msaaImage() ? target->msaaImage() : target; |
| 875 | |
| 876 | CommandBufferInfo& cbInfo = fCommandBufferInfos[fCurrentCmdInfo]; |
| 877 | VkRect2D bounds; |
| 878 | bounds.offset = { 0, 0 }; |
| 879 | bounds.extent = { 0, 0 }; |
| 880 | |
| 881 | GrVkDrawableInfo vkInfo; |
| 882 | vkInfo.fSecondaryCommandBuffer = cbInfo.currentCmdBuf()->vkCommandBuffer(); |
| 883 | vkInfo.fCompatibleRenderPass = cbInfo.fRenderPass->vkRenderPass(); |
Greg Daniel | b353eeb | 2018-12-05 11:01:58 -0500 | [diff] [blame] | 884 | SkAssertResult(cbInfo.fRenderPass->colorAttachmentIndex(&vkInfo.fColorAttachmentIndex)); |
Greg Daniel | 64cc9aa | 2018-10-19 13:54:56 -0400 | [diff] [blame] | 885 | vkInfo.fFormat = targetImage->imageFormat(); |
| 886 | vkInfo.fDrawBounds = &bounds; |
Stan Iliev | cb58060 | 2019-02-26 11:36:07 -0500 | [diff] [blame] | 887 | #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK |
| 888 | vkInfo.fImage = targetImage->image(); |
| 889 | #else |
| 890 | vkInfo.fImage = VK_NULL_HANDLE; |
| 891 | #endif //SK_BUILD_FOR_ANDROID_FRAMEWORK |
Greg Daniel | 64cc9aa | 2018-10-19 13:54:56 -0400 | [diff] [blame] | 892 | |
| 893 | GrBackendDrawableInfo info(vkInfo); |
| 894 | |
Eric Karl | c0b2ba2 | 2019-01-22 19:40:35 -0800 | [diff] [blame] | 895 | // After we draw into the command buffer via the drawable, cached state we have may be invalid. |
| 896 | cbInfo.currentCmdBuf()->invalidateState(); |
Eric Karl | a8878a1 | 2019-02-07 18:17:43 -0800 | [diff] [blame] | 897 | // Also assume that the drawable produced output. |
| 898 | cbInfo.fIsEmpty = false; |
Eric Karl | c0b2ba2 | 2019-01-22 19:40:35 -0800 | [diff] [blame] | 899 | |
Greg Daniel | 64cc9aa | 2018-10-19 13:54:56 -0400 | [diff] [blame] | 900 | drawable->draw(info); |
| 901 | fGpu->addDrawable(std::move(drawable)); |
| 902 | |
| 903 | if (bounds.extent.width == 0 || bounds.extent.height == 0) { |
| 904 | cbInfo.fBounds.join(target->getBoundsRect()); |
| 905 | } else { |
| 906 | cbInfo.fBounds.join(SkRect::MakeXYWH(bounds.offset.x, bounds.offset.y, |
| 907 | bounds.extent.width, bounds.extent.height)); |
| 908 | } |
| 909 | } |
| 910 | |