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