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