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