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 | |
csmartdalton | 29df760 | 2016-08-31 11:55:52 -0700 | [diff] [blame] | 10 | #include "GrFixedClip.h" |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 11 | #include "GrMesh.h" |
Brian Salomon | 742e31d | 2016-12-07 17:06:19 -0500 | [diff] [blame] | 12 | #include "GrOpFlushState.h" |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 13 | #include "GrPipeline.h" |
| 14 | #include "GrRenderTargetPriv.h" |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 15 | #include "GrTexturePriv.h" |
egdaniel | 066df7c | 2016-06-08 14:02:27 -0700 | [diff] [blame] | 16 | #include "GrVkCommandBuffer.h" |
| 17 | #include "GrVkGpu.h" |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 18 | #include "GrVkPipeline.h" |
egdaniel | 066df7c | 2016-06-08 14:02:27 -0700 | [diff] [blame] | 19 | #include "GrVkRenderPass.h" |
| 20 | #include "GrVkRenderTarget.h" |
| 21 | #include "GrVkResourceProvider.h" |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 22 | #include "GrVkTexture.h" |
Greg Daniel | 36a77ee | 2016-10-18 10:33:25 -0400 | [diff] [blame] | 23 | #include "SkRect.h" |
egdaniel | 066df7c | 2016-06-08 14:02:27 -0700 | [diff] [blame] | 24 | |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 25 | void GrVkGpuTextureCommandBuffer::copy(GrSurface* src, GrSurfaceOrigin srcOrigin, |
| 26 | const SkIRect& srcRect, const SkIPoint& dstPoint) { |
| 27 | fCopies.emplace_back(src, srcOrigin, srcRect, dstPoint); |
Greg Daniel | 500d58b | 2017-08-24 15:59:33 -0400 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | void GrVkGpuTextureCommandBuffer::insertEventMarker(const char* msg) { |
| 31 | // TODO: does Vulkan have a correlate? |
| 32 | } |
| 33 | |
| 34 | void GrVkGpuTextureCommandBuffer::submit() { |
| 35 | for (int i = 0; i < fCopies.count(); ++i) { |
| 36 | CopyInfo& copyInfo = fCopies[i]; |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 37 | fGpu->copySurface(fTexture, fOrigin, copyInfo.fSrc, copyInfo.fSrcOrigin, copyInfo.fSrcRect, |
| 38 | copyInfo.fDstPoint); |
Greg Daniel | 500d58b | 2017-08-24 15:59:33 -0400 | [diff] [blame] | 39 | } |
| 40 | } |
| 41 | |
| 42 | GrVkGpuTextureCommandBuffer::~GrVkGpuTextureCommandBuffer() {} |
| 43 | |
| 44 | //////////////////////////////////////////////////////////////////////////////// |
| 45 | |
Robert Phillips | 6b47c7d | 2017-08-29 07:24:09 -0400 | [diff] [blame] | 46 | void get_vk_load_store_ops(GrLoadOp loadOpIn, GrStoreOp storeOpIn, |
egdaniel | 066df7c | 2016-06-08 14:02:27 -0700 | [diff] [blame] | 47 | VkAttachmentLoadOp* loadOp, VkAttachmentStoreOp* storeOp) { |
Robert Phillips | 9521447 | 2017-08-08 18:00:03 -0400 | [diff] [blame] | 48 | switch (loadOpIn) { |
Robert Phillips | 6b47c7d | 2017-08-29 07:24:09 -0400 | [diff] [blame] | 49 | case GrLoadOp::kLoad: |
egdaniel | 066df7c | 2016-06-08 14:02:27 -0700 | [diff] [blame] | 50 | *loadOp = VK_ATTACHMENT_LOAD_OP_LOAD; |
egdaniel | 066df7c | 2016-06-08 14:02:27 -0700 | [diff] [blame] | 51 | break; |
Robert Phillips | 6b47c7d | 2017-08-29 07:24:09 -0400 | [diff] [blame] | 52 | case GrLoadOp::kClear: |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 53 | *loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR; |
| 54 | break; |
Robert Phillips | 6b47c7d | 2017-08-29 07:24:09 -0400 | [diff] [blame] | 55 | case GrLoadOp::kDiscard: |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 56 | *loadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE; |
| 57 | break; |
| 58 | default: |
| 59 | SK_ABORT("Invalid LoadOp"); |
egdaniel | 066df7c | 2016-06-08 14:02:27 -0700 | [diff] [blame] | 60 | *loadOp = VK_ATTACHMENT_LOAD_OP_LOAD; |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 61 | } |
| 62 | |
Robert Phillips | 9521447 | 2017-08-08 18:00:03 -0400 | [diff] [blame] | 63 | switch (storeOpIn) { |
Robert Phillips | 6b47c7d | 2017-08-29 07:24:09 -0400 | [diff] [blame] | 64 | case GrStoreOp::kStore: |
egdaniel | 066df7c | 2016-06-08 14:02:27 -0700 | [diff] [blame] | 65 | *storeOp = VK_ATTACHMENT_STORE_OP_STORE; |
| 66 | break; |
Robert Phillips | 6b47c7d | 2017-08-29 07:24:09 -0400 | [diff] [blame] | 67 | case GrStoreOp::kDiscard: |
egdaniel | 066df7c | 2016-06-08 14:02:27 -0700 | [diff] [blame] | 68 | *storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE; |
| 69 | break; |
brianosman | 0bbc371 | 2016-06-14 04:53:09 -0700 | [diff] [blame] | 70 | default: |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 71 | SK_ABORT("Invalid StoreOp"); |
brianosman | 0bbc371 | 2016-06-14 04:53:09 -0700 | [diff] [blame] | 72 | *storeOp = VK_ATTACHMENT_STORE_OP_STORE; |
egdaniel | 066df7c | 2016-06-08 14:02:27 -0700 | [diff] [blame] | 73 | } |
| 74 | } |
| 75 | |
Greg Daniel | 500d58b | 2017-08-24 15:59:33 -0400 | [diff] [blame] | 76 | GrVkGpuRTCommandBuffer::GrVkGpuRTCommandBuffer(GrVkGpu* gpu, |
| 77 | GrRenderTarget* rt, GrSurfaceOrigin origin, |
| 78 | const LoadAndStoreInfo& colorInfo, |
| 79 | const StencilLoadAndStoreInfo& stencilInfo) |
Robert Phillips | 19e51dc | 2017-08-09 09:30:51 -0400 | [diff] [blame] | 80 | : INHERITED(rt, origin) |
| 81 | , fGpu(gpu) |
| 82 | , fClearColor(GrColor4f::FromGrColor(colorInfo.fClearColor)) |
| 83 | , fLastPipelineState(nullptr) { |
Robert Phillips | 9521447 | 2017-08-08 18:00:03 -0400 | [diff] [blame] | 84 | get_vk_load_store_ops(colorInfo.fLoadOp, colorInfo.fStoreOp, |
| 85 | &fVkColorLoadOp, &fVkColorStoreOp); |
egdaniel | 066df7c | 2016-06-08 14:02:27 -0700 | [diff] [blame] | 86 | |
Robert Phillips | 9521447 | 2017-08-08 18:00:03 -0400 | [diff] [blame] | 87 | get_vk_load_store_ops(stencilInfo.fLoadOp, stencilInfo.fStoreOp, |
| 88 | &fVkStencilLoadOp, &fVkStencilStoreOp); |
Greg Daniel | 22bc865 | 2017-03-22 15:45:43 -0400 | [diff] [blame] | 89 | fCurrentCmdInfo = -1; |
Robert Phillips | 9521447 | 2017-08-08 18:00:03 -0400 | [diff] [blame] | 90 | |
Robert Phillips | 19e51dc | 2017-08-09 09:30:51 -0400 | [diff] [blame] | 91 | this->init(); |
Brian Salomon | c293a29 | 2016-11-30 13:38:32 -0500 | [diff] [blame] | 92 | } |
| 93 | |
Greg Daniel | 500d58b | 2017-08-24 15:59:33 -0400 | [diff] [blame] | 94 | void GrVkGpuRTCommandBuffer::init() { |
Brian Salomon | c293a29 | 2016-11-30 13:38:32 -0500 | [diff] [blame] | 95 | GrVkRenderPass::LoadStoreOps vkColorOps(fVkColorLoadOp, fVkColorStoreOp); |
| 96 | GrVkRenderPass::LoadStoreOps vkStencilOps(fVkStencilLoadOp, fVkStencilStoreOp); |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 97 | |
Greg Daniel | 36a77ee | 2016-10-18 10:33:25 -0400 | [diff] [blame] | 98 | CommandBufferInfo& cbInfo = fCommandBufferInfos.push_back(); |
Brian Salomon | c293a29 | 2016-11-30 13:38:32 -0500 | [diff] [blame] | 99 | SkASSERT(fCommandBufferInfos.count() == 1); |
Greg Daniel | 22bc865 | 2017-03-22 15:45:43 -0400 | [diff] [blame] | 100 | fCurrentCmdInfo = 0; |
Greg Daniel | 36a77ee | 2016-10-18 10:33:25 -0400 | [diff] [blame] | 101 | |
Robert Phillips | 19e51dc | 2017-08-09 09:30:51 -0400 | [diff] [blame] | 102 | GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(fRenderTarget); |
| 103 | const GrVkResourceProvider::CompatibleRPHandle& rpHandle = vkRT->compatibleRenderPassHandle(); |
egdaniel | 066df7c | 2016-06-08 14:02:27 -0700 | [diff] [blame] | 104 | if (rpHandle.isValid()) { |
Greg Daniel | 36a77ee | 2016-10-18 10:33:25 -0400 | [diff] [blame] | 105 | cbInfo.fRenderPass = fGpu->resourceProvider().findRenderPass(rpHandle, |
| 106 | vkColorOps, |
| 107 | vkStencilOps); |
egdaniel | 066df7c | 2016-06-08 14:02:27 -0700 | [diff] [blame] | 108 | } else { |
Robert Phillips | 19e51dc | 2017-08-09 09:30:51 -0400 | [diff] [blame] | 109 | cbInfo.fRenderPass = fGpu->resourceProvider().findRenderPass(*vkRT, |
Greg Daniel | 36a77ee | 2016-10-18 10:33:25 -0400 | [diff] [blame] | 110 | vkColorOps, |
| 111 | vkStencilOps); |
egdaniel | 066df7c | 2016-06-08 14:02:27 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Brian Salomon | c293a29 | 2016-11-30 13:38:32 -0500 | [diff] [blame] | 114 | cbInfo.fColorClearValue.color.float32[0] = fClearColor.fRGBA[0]; |
| 115 | cbInfo.fColorClearValue.color.float32[1] = fClearColor.fRGBA[1]; |
| 116 | cbInfo.fColorClearValue.color.float32[2] = fClearColor.fRGBA[2]; |
| 117 | cbInfo.fColorClearValue.color.float32[3] = fClearColor.fRGBA[3]; |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 118 | |
Robert Phillips | 380b90c | 2017-08-30 07:41:07 -0400 | [diff] [blame^] | 119 | if (VK_ATTACHMENT_LOAD_OP_CLEAR == fVkColorLoadOp) { |
| 120 | cbInfo.fBounds = SkRect::MakeWH(vkRT->width(), vkRT->height()); |
| 121 | } else { |
| 122 | cbInfo.fBounds.setEmpty(); |
| 123 | } |
Greg Daniel | 77b53f6 | 2016-10-18 11:48:51 -0400 | [diff] [blame] | 124 | cbInfo.fIsEmpty = true; |
| 125 | cbInfo.fStartsWithClear = false; |
Greg Daniel | 36a77ee | 2016-10-18 10:33:25 -0400 | [diff] [blame] | 126 | |
Greg Daniel | 22bc865 | 2017-03-22 15:45:43 -0400 | [diff] [blame] | 127 | cbInfo.fCommandBuffers.push_back(fGpu->resourceProvider().findOrCreateSecondaryCommandBuffer()); |
Robert Phillips | 19e51dc | 2017-08-09 09:30:51 -0400 | [diff] [blame] | 128 | cbInfo.currentCmdBuf()->begin(fGpu, vkRT->framebuffer(), cbInfo.fRenderPass); |
egdaniel | 066df7c | 2016-06-08 14:02:27 -0700 | [diff] [blame] | 129 | } |
| 130 | |
Brian Salomon | c293a29 | 2016-11-30 13:38:32 -0500 | [diff] [blame] | 131 | |
Greg Daniel | 500d58b | 2017-08-24 15:59:33 -0400 | [diff] [blame] | 132 | GrVkGpuRTCommandBuffer::~GrVkGpuRTCommandBuffer() { |
Greg Daniel | 36a77ee | 2016-10-18 10:33:25 -0400 | [diff] [blame] | 133 | for (int i = 0; i < fCommandBufferInfos.count(); ++i) { |
| 134 | CommandBufferInfo& cbInfo = fCommandBufferInfos[i]; |
Greg Daniel | 22bc865 | 2017-03-22 15:45:43 -0400 | [diff] [blame] | 135 | for (int j = 0; j < cbInfo.fCommandBuffers.count(); ++j) { |
| 136 | cbInfo.fCommandBuffers[j]->unref(fGpu); |
| 137 | } |
Greg Daniel | 36a77ee | 2016-10-18 10:33:25 -0400 | [diff] [blame] | 138 | cbInfo.fRenderPass->unref(fGpu); |
| 139 | } |
egdaniel | 066df7c | 2016-06-08 14:02:27 -0700 | [diff] [blame] | 140 | } |
| 141 | |
Greg Daniel | 500d58b | 2017-08-24 15:59:33 -0400 | [diff] [blame] | 142 | GrGpu* GrVkGpuRTCommandBuffer::gpu() { return fGpu; } |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 143 | |
Greg Daniel | 500d58b | 2017-08-24 15:59:33 -0400 | [diff] [blame] | 144 | void GrVkGpuRTCommandBuffer::end() { |
Greg Daniel | 22bc865 | 2017-03-22 15:45:43 -0400 | [diff] [blame] | 145 | if (fCurrentCmdInfo >= 0) { |
| 146 | fCommandBufferInfos[fCurrentCmdInfo].currentCmdBuf()->end(fGpu); |
Brian Salomon | c293a29 | 2016-11-30 13:38:32 -0500 | [diff] [blame] | 147 | } |
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 | void GrVkGpuRTCommandBuffer::submit() { |
Brian Salomon | c293a29 | 2016-11-30 13:38:32 -0500 | [diff] [blame] | 151 | if (!fRenderTarget) { |
| 152 | return; |
| 153 | } |
Robert Phillips | 19e51dc | 2017-08-09 09:30:51 -0400 | [diff] [blame] | 154 | |
| 155 | GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(fRenderTarget); |
| 156 | |
egdaniel | ce3bfb1 | 2016-08-26 11:05:13 -0700 | [diff] [blame] | 157 | // Change layout of our render target so it can be used as the color attachment. Currently |
| 158 | // we don't attach the resolve to the framebuffer so no need to change its layout. |
Robert Phillips | 19e51dc | 2017-08-09 09:30:51 -0400 | [diff] [blame] | 159 | GrVkImage* targetImage = vkRT->msaaImage() ? vkRT->msaaImage() : vkRT; |
egdaniel | bc9b296 | 2016-09-27 08:00:53 -0700 | [diff] [blame] | 160 | |
| 161 | // Change layout of our render target so it can be used as the color attachment |
egdaniel | ce3bfb1 | 2016-08-26 11:05:13 -0700 | [diff] [blame] | 162 | targetImage->setImageLayout(fGpu, |
| 163 | VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, |
| 164 | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT, |
egdaniel | bc9b296 | 2016-09-27 08:00:53 -0700 | [diff] [blame] | 165 | VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT, |
egdaniel | ce3bfb1 | 2016-08-26 11:05:13 -0700 | [diff] [blame] | 166 | false); |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 167 | |
| 168 | // If we are using a stencil attachment we also need to update its layout |
| 169 | if (GrStencilAttachment* stencil = fRenderTarget->renderTargetPriv().getStencilAttachment()) { |
| 170 | GrVkStencilAttachment* vkStencil = (GrVkStencilAttachment*)stencil; |
| 171 | vkStencil->setImageLayout(fGpu, |
| 172 | VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, |
| 173 | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT | |
| 174 | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT, |
egdaniel | bc9b296 | 2016-09-27 08:00:53 -0700 | [diff] [blame] | 175 | VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT, |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 176 | false); |
| 177 | } |
| 178 | |
Greg Daniel | 36a77ee | 2016-10-18 10:33:25 -0400 | [diff] [blame] | 179 | for (int i = 0; i < fCommandBufferInfos.count(); ++i) { |
| 180 | CommandBufferInfo& cbInfo = fCommandBufferInfos[i]; |
| 181 | |
Greg Daniel | 77b53f6 | 2016-10-18 11:48:51 -0400 | [diff] [blame] | 182 | for (int j = 0; j < cbInfo.fPreDrawUploads.count(); ++j) { |
| 183 | InlineUploadInfo& iuInfo = cbInfo.fPreDrawUploads[j]; |
| 184 | iuInfo.fFlushState->doUpload(iuInfo.fUpload); |
| 185 | } |
| 186 | |
Greg Daniel | 500d58b | 2017-08-24 15:59:33 -0400 | [diff] [blame] | 187 | for (int j = 0; j < cbInfo.fPreCopies.count(); ++j) { |
| 188 | CopyInfo& copyInfo = cbInfo.fPreCopies[j]; |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 189 | fGpu->copySurface(fRenderTarget, fOrigin, copyInfo.fSrc, copyInfo.fSrcOrigin, |
| 190 | copyInfo.fSrcRect, copyInfo.fDstPoint); |
Greg Daniel | 500d58b | 2017-08-24 15:59:33 -0400 | [diff] [blame] | 191 | } |
| 192 | |
Greg Daniel | 77b53f6 | 2016-10-18 11:48:51 -0400 | [diff] [blame] | 193 | // TODO: We can't add this optimization yet since many things create a scratch texture which |
| 194 | // adds the discard immediately, but then don't draw to it right away. This causes the |
| 195 | // discard to be ignored and we get yelled at for loading uninitialized data. However, once |
Robert Phillips | 74c627f | 2017-08-09 10:28:00 -0400 | [diff] [blame] | 196 | // MDB lands, the discard will get reordered with the rest of the draw commands and we can |
Greg Daniel | 77b53f6 | 2016-10-18 11:48:51 -0400 | [diff] [blame] | 197 | // re-enable this. |
| 198 | #if 0 |
| 199 | if (cbInfo.fIsEmpty && !cbInfo.fStartsWithClear) { |
| 200 | // We have sumbitted no actual draw commands to the command buffer and we are not using |
| 201 | // the render pass to do a clear so there is no need to submit anything. |
| 202 | continue; |
| 203 | } |
| 204 | #endif |
Greg Daniel | 36a77ee | 2016-10-18 10:33:25 -0400 | [diff] [blame] | 205 | if (cbInfo.fBounds.intersect(0, 0, |
| 206 | SkIntToScalar(fRenderTarget->width()), |
| 207 | SkIntToScalar(fRenderTarget->height()))) { |
| 208 | SkIRect iBounds; |
| 209 | cbInfo.fBounds.roundOut(&iBounds); |
| 210 | |
Greg Daniel | 22bc865 | 2017-03-22 15:45:43 -0400 | [diff] [blame] | 211 | fGpu->submitSecondaryCommandBuffer(cbInfo.fCommandBuffers, cbInfo.fRenderPass, |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 212 | &cbInfo.fColorClearValue, vkRT, fOrigin, iBounds); |
Greg Daniel | 36a77ee | 2016-10-18 10:33:25 -0400 | [diff] [blame] | 213 | } |
| 214 | } |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 215 | } |
| 216 | |
Greg Daniel | 500d58b | 2017-08-24 15:59:33 -0400 | [diff] [blame] | 217 | void GrVkGpuRTCommandBuffer::discard() { |
Robert Phillips | 19e51dc | 2017-08-09 09:30:51 -0400 | [diff] [blame] | 218 | GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(fRenderTarget); |
Brian Salomon | c293a29 | 2016-11-30 13:38:32 -0500 | [diff] [blame] | 219 | |
Greg Daniel | 22bc865 | 2017-03-22 15:45:43 -0400 | [diff] [blame] | 220 | CommandBufferInfo& cbInfo = fCommandBufferInfos[fCurrentCmdInfo]; |
Greg Daniel | 77b53f6 | 2016-10-18 11:48:51 -0400 | [diff] [blame] | 221 | if (cbInfo.fIsEmpty) { |
Robert Phillips | 74c627f | 2017-08-09 10:28:00 -0400 | [diff] [blame] | 222 | // 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] | 223 | GrVkRenderPass::LoadStoreOps vkColorOps(VK_ATTACHMENT_LOAD_OP_DONT_CARE, |
| 224 | VK_ATTACHMENT_STORE_OP_STORE); |
| 225 | GrVkRenderPass::LoadStoreOps vkStencilOps(VK_ATTACHMENT_LOAD_OP_DONT_CARE, |
| 226 | VK_ATTACHMENT_STORE_OP_STORE); |
egdaniel | 37535c9 | 2016-06-30 08:23:30 -0700 | [diff] [blame] | 227 | |
Greg Daniel | 36a77ee | 2016-10-18 10:33:25 -0400 | [diff] [blame] | 228 | const GrVkRenderPass* oldRP = cbInfo.fRenderPass; |
egdaniel | 37535c9 | 2016-06-30 08:23:30 -0700 | [diff] [blame] | 229 | |
egdaniel | 37535c9 | 2016-06-30 08:23:30 -0700 | [diff] [blame] | 230 | const GrVkResourceProvider::CompatibleRPHandle& rpHandle = |
Robert Phillips | 19e51dc | 2017-08-09 09:30:51 -0400 | [diff] [blame] | 231 | vkRT->compatibleRenderPassHandle(); |
egdaniel | 37535c9 | 2016-06-30 08:23:30 -0700 | [diff] [blame] | 232 | if (rpHandle.isValid()) { |
Greg Daniel | 36a77ee | 2016-10-18 10:33:25 -0400 | [diff] [blame] | 233 | cbInfo.fRenderPass = fGpu->resourceProvider().findRenderPass(rpHandle, |
| 234 | vkColorOps, |
| 235 | vkStencilOps); |
egdaniel | 37535c9 | 2016-06-30 08:23:30 -0700 | [diff] [blame] | 236 | } else { |
Robert Phillips | 19e51dc | 2017-08-09 09:30:51 -0400 | [diff] [blame] | 237 | cbInfo.fRenderPass = fGpu->resourceProvider().findRenderPass(*vkRT, |
Greg Daniel | 36a77ee | 2016-10-18 10:33:25 -0400 | [diff] [blame] | 238 | vkColorOps, |
| 239 | vkStencilOps); |
egdaniel | 37535c9 | 2016-06-30 08:23:30 -0700 | [diff] [blame] | 240 | } |
| 241 | |
Greg Daniel | 36a77ee | 2016-10-18 10:33:25 -0400 | [diff] [blame] | 242 | SkASSERT(cbInfo.fRenderPass->isCompatible(*oldRP)); |
egdaniel | 37535c9 | 2016-06-30 08:23:30 -0700 | [diff] [blame] | 243 | oldRP->unref(fGpu); |
Greg Daniel | 5011f85 | 2016-10-28 15:07:16 -0400 | [diff] [blame] | 244 | cbInfo.fBounds.join(fRenderTarget->getBoundsRect()); |
Greg Daniel | 77b53f6 | 2016-10-18 11:48:51 -0400 | [diff] [blame] | 245 | cbInfo.fStartsWithClear = false; |
egdaniel | 37535c9 | 2016-06-30 08:23:30 -0700 | [diff] [blame] | 246 | } |
| 247 | } |
| 248 | |
Greg Daniel | 500d58b | 2017-08-24 15:59:33 -0400 | [diff] [blame] | 249 | void GrVkGpuRTCommandBuffer::insertEventMarker(const char* msg) { |
Robert Phillips | 65a88fa | 2017-08-08 08:36:22 -0400 | [diff] [blame] | 250 | // TODO: does Vulkan have a correlate? |
| 251 | } |
| 252 | |
Greg Daniel | 500d58b | 2017-08-24 15:59:33 -0400 | [diff] [blame] | 253 | void GrVkGpuRTCommandBuffer::onClearStencilClip(const GrFixedClip& clip, bool insideStencilMask) { |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 254 | SkASSERT(!clip.hasWindowRectangles()); |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 255 | |
Greg Daniel | 22bc865 | 2017-03-22 15:45:43 -0400 | [diff] [blame] | 256 | CommandBufferInfo& cbInfo = fCommandBufferInfos[fCurrentCmdInfo]; |
Greg Daniel | 36a77ee | 2016-10-18 10:33:25 -0400 | [diff] [blame] | 257 | |
Greg Daniel | 65a0927 | 2016-10-12 09:47:22 -0400 | [diff] [blame] | 258 | GrStencilAttachment* sb = fRenderTarget->renderTargetPriv().getStencilAttachment(); |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 259 | // this should only be called internally when we know we have a |
| 260 | // stencil buffer. |
| 261 | SkASSERT(sb); |
| 262 | int stencilBitCount = sb->bits(); |
| 263 | |
| 264 | // The contract with the callers does not guarantee that we preserve all bits in the stencil |
| 265 | // during this clear. Thus we will clear the entire stencil to the desired value. |
| 266 | |
| 267 | VkClearDepthStencilValue vkStencilColor; |
| 268 | memset(&vkStencilColor, 0, sizeof(VkClearDepthStencilValue)); |
csmartdalton | 29df760 | 2016-08-31 11:55:52 -0700 | [diff] [blame] | 269 | if (insideStencilMask) { |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 270 | vkStencilColor.stencil = (1 << (stencilBitCount - 1)); |
| 271 | } else { |
| 272 | vkStencilColor.stencil = 0; |
| 273 | } |
| 274 | |
| 275 | VkClearRect clearRect; |
| 276 | // Flip rect if necessary |
csmartdalton | 29df760 | 2016-08-31 11:55:52 -0700 | [diff] [blame] | 277 | SkIRect vkRect; |
| 278 | if (!clip.scissorEnabled()) { |
Greg Daniel | 65a0927 | 2016-10-12 09:47:22 -0400 | [diff] [blame] | 279 | vkRect.setXYWH(0, 0, fRenderTarget->width(), fRenderTarget->height()); |
Robert Phillips | 4f101a7 | 2017-07-28 08:42:04 -0400 | [diff] [blame] | 280 | } else if (kBottomLeft_GrSurfaceOrigin != fOrigin) { |
csmartdalton | 29df760 | 2016-08-31 11:55:52 -0700 | [diff] [blame] | 281 | vkRect = clip.scissorRect(); |
| 282 | } else { |
| 283 | const SkIRect& scissor = clip.scissorRect(); |
Greg Daniel | 65a0927 | 2016-10-12 09:47:22 -0400 | [diff] [blame] | 284 | vkRect.setLTRB(scissor.fLeft, fRenderTarget->height() - scissor.fBottom, |
| 285 | scissor.fRight, fRenderTarget->height() - scissor.fTop); |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | clearRect.rect.offset = { vkRect.fLeft, vkRect.fTop }; |
| 289 | clearRect.rect.extent = { (uint32_t)vkRect.width(), (uint32_t)vkRect.height() }; |
| 290 | |
| 291 | clearRect.baseArrayLayer = 0; |
| 292 | clearRect.layerCount = 1; |
| 293 | |
| 294 | uint32_t stencilIndex; |
Greg Daniel | 36a77ee | 2016-10-18 10:33:25 -0400 | [diff] [blame] | 295 | SkAssertResult(cbInfo.fRenderPass->stencilAttachmentIndex(&stencilIndex)); |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 296 | |
| 297 | VkClearAttachment attachment; |
| 298 | attachment.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT; |
| 299 | attachment.colorAttachment = 0; // this value shouldn't matter |
| 300 | attachment.clearValue.depthStencil = vkStencilColor; |
| 301 | |
Greg Daniel | 22bc865 | 2017-03-22 15:45:43 -0400 | [diff] [blame] | 302 | cbInfo.currentCmdBuf()->clearAttachments(fGpu, 1, &attachment, 1, &clearRect); |
Greg Daniel | 77b53f6 | 2016-10-18 11:48:51 -0400 | [diff] [blame] | 303 | cbInfo.fIsEmpty = false; |
Greg Daniel | 36a77ee | 2016-10-18 10:33:25 -0400 | [diff] [blame] | 304 | |
| 305 | // Update command buffer bounds |
| 306 | if (!clip.scissorEnabled()) { |
| 307 | cbInfo.fBounds.join(fRenderTarget->getBoundsRect()); |
| 308 | } else { |
| 309 | cbInfo.fBounds.join(SkRect::Make(clip.scissorRect())); |
| 310 | } |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 311 | } |
| 312 | |
Greg Daniel | 500d58b | 2017-08-24 15:59:33 -0400 | [diff] [blame] | 313 | void GrVkGpuRTCommandBuffer::onClear(const GrFixedClip& clip, GrColor color) { |
Robert Phillips | 19e51dc | 2017-08-09 09:30:51 -0400 | [diff] [blame] | 314 | GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(fRenderTarget); |
| 315 | |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 316 | // parent class should never let us get here with no RT |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 317 | SkASSERT(!clip.hasWindowRectangles()); |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 318 | |
Greg Daniel | 22bc865 | 2017-03-22 15:45:43 -0400 | [diff] [blame] | 319 | CommandBufferInfo& cbInfo = fCommandBufferInfos[fCurrentCmdInfo]; |
Greg Daniel | 36a77ee | 2016-10-18 10:33:25 -0400 | [diff] [blame] | 320 | |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 321 | VkClearColorValue vkColor; |
| 322 | GrColorToRGBAFloat(color, vkColor.float32); |
| 323 | |
Greg Daniel | 77b53f6 | 2016-10-18 11:48:51 -0400 | [diff] [blame] | 324 | if (cbInfo.fIsEmpty && !clip.scissorEnabled()) { |
Robert Phillips | 74c627f | 2017-08-09 10:28:00 -0400 | [diff] [blame] | 325 | // Change the render pass to do a clear load |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 326 | GrVkRenderPass::LoadStoreOps vkColorOps(VK_ATTACHMENT_LOAD_OP_CLEAR, |
| 327 | VK_ATTACHMENT_STORE_OP_STORE); |
Robert Phillips | 74c627f | 2017-08-09 10:28:00 -0400 | [diff] [blame] | 328 | // Preserve the stencil buffer's load & store settings |
| 329 | GrVkRenderPass::LoadStoreOps vkStencilOps(fVkStencilLoadOp, fVkStencilStoreOp); |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 330 | |
Greg Daniel | 36a77ee | 2016-10-18 10:33:25 -0400 | [diff] [blame] | 331 | const GrVkRenderPass* oldRP = cbInfo.fRenderPass; |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 332 | |
| 333 | const GrVkResourceProvider::CompatibleRPHandle& rpHandle = |
Robert Phillips | 19e51dc | 2017-08-09 09:30:51 -0400 | [diff] [blame] | 334 | vkRT->compatibleRenderPassHandle(); |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 335 | if (rpHandle.isValid()) { |
Greg Daniel | 36a77ee | 2016-10-18 10:33:25 -0400 | [diff] [blame] | 336 | cbInfo.fRenderPass = fGpu->resourceProvider().findRenderPass(rpHandle, |
| 337 | vkColorOps, |
| 338 | vkStencilOps); |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 339 | } else { |
Robert Phillips | 19e51dc | 2017-08-09 09:30:51 -0400 | [diff] [blame] | 340 | cbInfo.fRenderPass = fGpu->resourceProvider().findRenderPass(*vkRT, |
Greg Daniel | 36a77ee | 2016-10-18 10:33:25 -0400 | [diff] [blame] | 341 | vkColorOps, |
| 342 | vkStencilOps); |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 343 | } |
| 344 | |
Greg Daniel | 36a77ee | 2016-10-18 10:33:25 -0400 | [diff] [blame] | 345 | SkASSERT(cbInfo.fRenderPass->isCompatible(*oldRP)); |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 346 | oldRP->unref(fGpu); |
| 347 | |
Greg Daniel | 36a77ee | 2016-10-18 10:33:25 -0400 | [diff] [blame] | 348 | GrColorToRGBAFloat(color, cbInfo.fColorClearValue.color.float32); |
Greg Daniel | 77b53f6 | 2016-10-18 11:48:51 -0400 | [diff] [blame] | 349 | cbInfo.fStartsWithClear = true; |
Greg Daniel | 36a77ee | 2016-10-18 10:33:25 -0400 | [diff] [blame] | 350 | |
| 351 | // Update command buffer bounds |
| 352 | cbInfo.fBounds.join(fRenderTarget->getBoundsRect()); |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 353 | return; |
| 354 | } |
| 355 | |
| 356 | // We always do a sub rect clear with clearAttachments since we are inside a render pass |
| 357 | VkClearRect clearRect; |
| 358 | // Flip rect if necessary |
csmartdalton | 29df760 | 2016-08-31 11:55:52 -0700 | [diff] [blame] | 359 | SkIRect vkRect; |
| 360 | if (!clip.scissorEnabled()) { |
Greg Daniel | 65a0927 | 2016-10-12 09:47:22 -0400 | [diff] [blame] | 361 | vkRect.setXYWH(0, 0, fRenderTarget->width(), fRenderTarget->height()); |
Robert Phillips | 4f101a7 | 2017-07-28 08:42:04 -0400 | [diff] [blame] | 362 | } else if (kBottomLeft_GrSurfaceOrigin != fOrigin) { |
csmartdalton | 29df760 | 2016-08-31 11:55:52 -0700 | [diff] [blame] | 363 | vkRect = clip.scissorRect(); |
| 364 | } else { |
| 365 | const SkIRect& scissor = clip.scissorRect(); |
Greg Daniel | 65a0927 | 2016-10-12 09:47:22 -0400 | [diff] [blame] | 366 | vkRect.setLTRB(scissor.fLeft, fRenderTarget->height() - scissor.fBottom, |
| 367 | scissor.fRight, fRenderTarget->height() - scissor.fTop); |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 368 | } |
| 369 | clearRect.rect.offset = { vkRect.fLeft, vkRect.fTop }; |
| 370 | clearRect.rect.extent = { (uint32_t)vkRect.width(), (uint32_t)vkRect.height() }; |
| 371 | clearRect.baseArrayLayer = 0; |
| 372 | clearRect.layerCount = 1; |
| 373 | |
| 374 | uint32_t colorIndex; |
Greg Daniel | 36a77ee | 2016-10-18 10:33:25 -0400 | [diff] [blame] | 375 | SkAssertResult(cbInfo.fRenderPass->colorAttachmentIndex(&colorIndex)); |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 376 | |
| 377 | VkClearAttachment attachment; |
| 378 | attachment.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; |
| 379 | attachment.colorAttachment = colorIndex; |
| 380 | attachment.clearValue.color = vkColor; |
| 381 | |
Greg Daniel | 22bc865 | 2017-03-22 15:45:43 -0400 | [diff] [blame] | 382 | cbInfo.currentCmdBuf()->clearAttachments(fGpu, 1, &attachment, 1, &clearRect); |
Greg Daniel | 77b53f6 | 2016-10-18 11:48:51 -0400 | [diff] [blame] | 383 | cbInfo.fIsEmpty = false; |
Greg Daniel | 36a77ee | 2016-10-18 10:33:25 -0400 | [diff] [blame] | 384 | |
| 385 | // Update command buffer bounds |
| 386 | if (!clip.scissorEnabled()) { |
| 387 | cbInfo.fBounds.join(fRenderTarget->getBoundsRect()); |
| 388 | } else { |
| 389 | cbInfo.fBounds.join(SkRect::Make(clip.scissorRect())); |
| 390 | } |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 391 | return; |
| 392 | } |
| 393 | |
Greg Daniel | 500d58b | 2017-08-24 15:59:33 -0400 | [diff] [blame] | 394 | //////////////////////////////////////////////////////////////////////////////// |
| 395 | |
| 396 | void GrVkGpuRTCommandBuffer::addAdditionalCommandBuffer() { |
Robert Phillips | 19e51dc | 2017-08-09 09:30:51 -0400 | [diff] [blame] | 397 | GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(fRenderTarget); |
| 398 | |
Greg Daniel | 22bc865 | 2017-03-22 15:45:43 -0400 | [diff] [blame] | 399 | CommandBufferInfo& cbInfo = fCommandBufferInfos[fCurrentCmdInfo]; |
| 400 | cbInfo.currentCmdBuf()->end(fGpu); |
| 401 | cbInfo.fCommandBuffers.push_back(fGpu->resourceProvider().findOrCreateSecondaryCommandBuffer()); |
Robert Phillips | 19e51dc | 2017-08-09 09:30:51 -0400 | [diff] [blame] | 402 | cbInfo.currentCmdBuf()->begin(fGpu, vkRT->framebuffer(), cbInfo.fRenderPass); |
Greg Daniel | 22bc865 | 2017-03-22 15:45:43 -0400 | [diff] [blame] | 403 | } |
| 404 | |
Greg Daniel | 500d58b | 2017-08-24 15:59:33 -0400 | [diff] [blame] | 405 | void GrVkGpuRTCommandBuffer::addAdditionalRenderPass() { |
Robert Phillips | 19e51dc | 2017-08-09 09:30:51 -0400 | [diff] [blame] | 406 | GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(fRenderTarget); |
| 407 | |
Greg Daniel | 22bc865 | 2017-03-22 15:45:43 -0400 | [diff] [blame] | 408 | fCommandBufferInfos[fCurrentCmdInfo].currentCmdBuf()->end(fGpu); |
Greg Daniel | 77b53f6 | 2016-10-18 11:48:51 -0400 | [diff] [blame] | 409 | |
| 410 | CommandBufferInfo& cbInfo = fCommandBufferInfos.push_back(); |
Greg Daniel | 22bc865 | 2017-03-22 15:45:43 -0400 | [diff] [blame] | 411 | fCurrentCmdInfo++; |
Greg Daniel | 77b53f6 | 2016-10-18 11:48:51 -0400 | [diff] [blame] | 412 | |
| 413 | GrVkRenderPass::LoadStoreOps vkColorOps(VK_ATTACHMENT_LOAD_OP_LOAD, |
| 414 | VK_ATTACHMENT_STORE_OP_STORE); |
| 415 | GrVkRenderPass::LoadStoreOps vkStencilOps(VK_ATTACHMENT_LOAD_OP_LOAD, |
| 416 | VK_ATTACHMENT_STORE_OP_STORE); |
| 417 | |
| 418 | const GrVkResourceProvider::CompatibleRPHandle& rpHandle = |
Robert Phillips | 19e51dc | 2017-08-09 09:30:51 -0400 | [diff] [blame] | 419 | vkRT->compatibleRenderPassHandle(); |
Greg Daniel | 77b53f6 | 2016-10-18 11:48:51 -0400 | [diff] [blame] | 420 | if (rpHandle.isValid()) { |
| 421 | cbInfo.fRenderPass = fGpu->resourceProvider().findRenderPass(rpHandle, |
| 422 | vkColorOps, |
| 423 | vkStencilOps); |
| 424 | } else { |
Robert Phillips | 19e51dc | 2017-08-09 09:30:51 -0400 | [diff] [blame] | 425 | cbInfo.fRenderPass = fGpu->resourceProvider().findRenderPass(*vkRT, |
Greg Daniel | 77b53f6 | 2016-10-18 11:48:51 -0400 | [diff] [blame] | 426 | vkColorOps, |
| 427 | vkStencilOps); |
| 428 | } |
| 429 | |
Greg Daniel | 22bc865 | 2017-03-22 15:45:43 -0400 | [diff] [blame] | 430 | cbInfo.fCommandBuffers.push_back(fGpu->resourceProvider().findOrCreateSecondaryCommandBuffer()); |
Greg Daniel | 77b53f6 | 2016-10-18 11:48:51 -0400 | [diff] [blame] | 431 | // It shouldn't matter what we set the clear color to here since we will assume loading of the |
| 432 | // attachment. |
| 433 | memset(&cbInfo.fColorClearValue, 0, sizeof(VkClearValue)); |
| 434 | cbInfo.fBounds.setEmpty(); |
| 435 | cbInfo.fIsEmpty = true; |
| 436 | cbInfo.fStartsWithClear = false; |
| 437 | |
Robert Phillips | 19e51dc | 2017-08-09 09:30:51 -0400 | [diff] [blame] | 438 | cbInfo.currentCmdBuf()->begin(fGpu, vkRT->framebuffer(), cbInfo.fRenderPass); |
Greg Daniel | 77b53f6 | 2016-10-18 11:48:51 -0400 | [diff] [blame] | 439 | } |
| 440 | |
Greg Daniel | 500d58b | 2017-08-24 15:59:33 -0400 | [diff] [blame] | 441 | void GrVkGpuRTCommandBuffer::inlineUpload(GrOpFlushState* state, GrDrawOp::DeferredUploadFn& upload) { |
Robert Phillips | 19e51dc | 2017-08-09 09:30:51 -0400 | [diff] [blame] | 442 | |
Greg Daniel | 22bc865 | 2017-03-22 15:45:43 -0400 | [diff] [blame] | 443 | if (!fCommandBufferInfos[fCurrentCmdInfo].fIsEmpty) { |
| 444 | this->addAdditionalRenderPass(); |
Greg Daniel | 77b53f6 | 2016-10-18 11:48:51 -0400 | [diff] [blame] | 445 | } |
Greg Daniel | 22bc865 | 2017-03-22 15:45:43 -0400 | [diff] [blame] | 446 | fCommandBufferInfos[fCurrentCmdInfo].fPreDrawUploads.emplace_back(state, upload); |
Greg Daniel | 77b53f6 | 2016-10-18 11:48:51 -0400 | [diff] [blame] | 447 | } |
| 448 | |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 449 | void GrVkGpuRTCommandBuffer::copy(GrSurface* src, GrSurfaceOrigin srcOrigin, const SkIRect& srcRect, |
Greg Daniel | 500d58b | 2017-08-24 15:59:33 -0400 | [diff] [blame] | 450 | const SkIPoint& dstPoint) { |
| 451 | if (!fCommandBufferInfos[fCurrentCmdInfo].fIsEmpty || |
| 452 | fCommandBufferInfos[fCurrentCmdInfo].fStartsWithClear) { |
| 453 | this->addAdditionalRenderPass(); |
| 454 | } |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 455 | fCommandBufferInfos[fCurrentCmdInfo].fPreCopies.emplace_back(src, srcOrigin, srcRect, dstPoint); |
Greg Daniel | 500d58b | 2017-08-24 15:59:33 -0400 | [diff] [blame] | 456 | } |
| 457 | |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 458 | //////////////////////////////////////////////////////////////////////////////// |
| 459 | |
Greg Daniel | 500d58b | 2017-08-24 15:59:33 -0400 | [diff] [blame] | 460 | void GrVkGpuRTCommandBuffer::bindGeometry(const GrPrimitiveProcessor& primProc, |
| 461 | const GrBuffer* indexBuffer, |
| 462 | const GrBuffer* vertexBuffer, |
| 463 | const GrBuffer* instanceBuffer) { |
Chris Dalton | ff92650 | 2017-05-03 14:36:54 -0400 | [diff] [blame] | 464 | GrVkSecondaryCommandBuffer* currCmdBuf = fCommandBufferInfos[fCurrentCmdInfo].currentCmdBuf(); |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 465 | // There is no need to put any memory barriers to make sure host writes have finished here. |
| 466 | // When a command buffer is submitted to a queue, there is an implicit memory barrier that |
| 467 | // occurs for all host writes. Additionally, BufferMemoryBarriers are not allowed inside of |
| 468 | // an active RenderPass. |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 469 | |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 470 | // Here our vertex and instance inputs need to match the same 0-based bindings they were |
| 471 | // assigned in GrVkPipeline. That is, vertex first (if any) followed by instance. |
| 472 | uint32_t binding = 0; |
| 473 | |
| 474 | if (primProc.hasVertexAttribs()) { |
| 475 | SkASSERT(vertexBuffer); |
| 476 | SkASSERT(!vertexBuffer->isCPUBacked()); |
| 477 | SkASSERT(!vertexBuffer->isMapped()); |
| 478 | |
| 479 | currCmdBuf->bindInputBuffer(fGpu, binding++, |
| 480 | static_cast<const GrVkVertexBuffer*>(vertexBuffer)); |
| 481 | } |
| 482 | |
| 483 | if (primProc.hasInstanceAttribs()) { |
| 484 | SkASSERT(instanceBuffer); |
| 485 | SkASSERT(!instanceBuffer->isCPUBacked()); |
| 486 | SkASSERT(!instanceBuffer->isMapped()); |
| 487 | |
| 488 | currCmdBuf->bindInputBuffer(fGpu, binding++, |
| 489 | static_cast<const GrVkVertexBuffer*>(instanceBuffer)); |
| 490 | } |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 491 | |
Chris Dalton | ff92650 | 2017-05-03 14:36:54 -0400 | [diff] [blame] | 492 | if (indexBuffer) { |
| 493 | SkASSERT(indexBuffer); |
| 494 | SkASSERT(!indexBuffer->isMapped()); |
| 495 | SkASSERT(!indexBuffer->isCPUBacked()); |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 496 | |
Chris Dalton | ff92650 | 2017-05-03 14:36:54 -0400 | [diff] [blame] | 497 | currCmdBuf->bindIndexBuffer(fGpu, static_cast<const GrVkIndexBuffer*>(indexBuffer)); |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 498 | } |
| 499 | } |
| 500 | |
Greg Daniel | 500d58b | 2017-08-24 15:59:33 -0400 | [diff] [blame] | 501 | sk_sp<GrVkPipelineState> GrVkGpuRTCommandBuffer::prepareDrawState( |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 502 | const GrPipeline& pipeline, |
| 503 | const GrPrimitiveProcessor& primProc, |
Chris Dalton | 46983b7 | 2017-06-06 12:27:16 -0600 | [diff] [blame] | 504 | GrPrimitiveType primitiveType, |
| 505 | bool hasDynamicState) { |
Greg Daniel | 22bc865 | 2017-03-22 15:45:43 -0400 | [diff] [blame] | 506 | CommandBufferInfo& cbInfo = fCommandBufferInfos[fCurrentCmdInfo]; |
| 507 | SkASSERT(cbInfo.fRenderPass); |
Greg Daniel | 36a77ee | 2016-10-18 10:33:25 -0400 | [diff] [blame] | 508 | |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 509 | sk_sp<GrVkPipelineState> pipelineState = |
| 510 | fGpu->resourceProvider().findOrCreateCompatiblePipelineState(pipeline, |
| 511 | primProc, |
| 512 | primitiveType, |
Greg Daniel | 36a77ee | 2016-10-18 10:33:25 -0400 | [diff] [blame] | 513 | *cbInfo.fRenderPass); |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 514 | if (!pipelineState) { |
| 515 | return pipelineState; |
| 516 | } |
| 517 | |
Greg Daniel | 22bc865 | 2017-03-22 15:45:43 -0400 | [diff] [blame] | 518 | if (!cbInfo.fIsEmpty && |
| 519 | fLastPipelineState && fLastPipelineState != pipelineState.get() && |
Greg Daniel | e3cd691 | 2017-05-17 11:15:55 -0400 | [diff] [blame] | 520 | fGpu->vkCaps().newCBOnPipelineChange()) { |
Greg Daniel | 22bc865 | 2017-03-22 15:45:43 -0400 | [diff] [blame] | 521 | this->addAdditionalCommandBuffer(); |
| 522 | } |
| 523 | fLastPipelineState = pipelineState.get(); |
| 524 | |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 525 | pipelineState->setData(fGpu, primProc, pipeline); |
| 526 | |
Greg Daniel | 22bc865 | 2017-03-22 15:45:43 -0400 | [diff] [blame] | 527 | pipelineState->bind(fGpu, cbInfo.currentCmdBuf()); |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 528 | |
Robert Phillips | 2890fbf | 2017-07-26 15:48:41 -0400 | [diff] [blame] | 529 | GrRenderTarget* rt = pipeline.renderTarget(); |
Chris Dalton | 46983b7 | 2017-06-06 12:27:16 -0600 | [diff] [blame] | 530 | |
| 531 | if (!pipeline.getScissorState().enabled()) { |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 532 | GrVkPipeline::SetDynamicScissorRectState(fGpu, cbInfo.currentCmdBuf(), |
| 533 | rt, pipeline.proxy()->origin(), |
Chris Dalton | 46983b7 | 2017-06-06 12:27:16 -0600 | [diff] [blame] | 534 | SkIRect::MakeWH(rt->width(), rt->height())); |
| 535 | } else if (!hasDynamicState) { |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 536 | GrVkPipeline::SetDynamicScissorRectState(fGpu, cbInfo.currentCmdBuf(), |
| 537 | rt, pipeline.proxy()->origin(), |
Chris Dalton | 46983b7 | 2017-06-06 12:27:16 -0600 | [diff] [blame] | 538 | pipeline.getScissorState().rect()); |
| 539 | } |
| 540 | GrVkPipeline::SetDynamicViewportState(fGpu, cbInfo.currentCmdBuf(), rt); |
| 541 | GrVkPipeline::SetDynamicBlendConstantState(fGpu, cbInfo.currentCmdBuf(), rt->config(), |
| 542 | pipeline.getXferProcessor()); |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 543 | |
| 544 | return pipelineState; |
| 545 | } |
| 546 | |
Brian Salomon | 18dfa98 | 2017-04-03 16:57:43 -0400 | [diff] [blame] | 547 | static void set_texture_layout(GrVkTexture* vkTexture, GrVkGpu* gpu) { |
| 548 | // TODO: If we ever decide to create the secondary command buffers ahead of time before we |
| 549 | // are actually going to submit them, we will need to track the sampled images and delay |
| 550 | // adding the layout change/barrier until we are ready to submit. |
| 551 | vkTexture->setImageLayout(gpu, |
| 552 | VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, |
| 553 | VK_ACCESS_SHADER_READ_BIT, |
| 554 | VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT, |
| 555 | false); |
| 556 | } |
| 557 | |
Brian Salomon | ab015ef | 2017-04-04 10:15:51 -0400 | [diff] [blame] | 558 | static void prepare_sampled_images(const GrResourceIOProcessor& processor, GrVkGpu* gpu) { |
Brian Salomon | 0bbecb2 | 2016-11-17 11:38:22 -0500 | [diff] [blame] | 559 | for (int i = 0; i < processor.numTextureSamplers(); ++i) { |
Brian Salomon | ab015ef | 2017-04-04 10:15:51 -0400 | [diff] [blame] | 560 | const GrResourceIOProcessor::TextureSampler& sampler = processor.textureSampler(i); |
Robert Phillips | 9bee2e5 | 2017-05-29 12:37:20 -0400 | [diff] [blame] | 561 | GrVkTexture* vkTexture = static_cast<GrVkTexture*>(sampler.peekTexture()); |
egdaniel | 6693355 | 2016-08-24 07:22:19 -0700 | [diff] [blame] | 562 | |
egdaniel | 8d2141f | 2016-09-02 11:19:13 -0700 | [diff] [blame] | 563 | // We may need to resolve the texture first if it is also a render target |
| 564 | GrVkRenderTarget* texRT = static_cast<GrVkRenderTarget*>(vkTexture->asRenderTarget()); |
| 565 | if (texRT) { |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 566 | gpu->onResolveRenderTarget(texRT, sampler.proxy()->origin()); |
egdaniel | 8d2141f | 2016-09-02 11:19:13 -0700 | [diff] [blame] | 567 | } |
| 568 | |
Brian Salomon | 514baff | 2016-11-17 15:17:07 -0500 | [diff] [blame] | 569 | const GrSamplerParams& params = sampler.params(); |
egdaniel | 8d2141f | 2016-09-02 11:19:13 -0700 | [diff] [blame] | 570 | // Check if we need to regenerate any mip maps |
Brian Salomon | 514baff | 2016-11-17 15:17:07 -0500 | [diff] [blame] | 571 | if (GrSamplerParams::kMipMap_FilterMode == params.filterMode()) { |
egdaniel | 8d2141f | 2016-09-02 11:19:13 -0700 | [diff] [blame] | 572 | if (vkTexture->texturePriv().mipMapsAreDirty()) { |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 573 | gpu->generateMipmap(vkTexture, sampler.proxy()->origin()); |
egdaniel | 8d2141f | 2016-09-02 11:19:13 -0700 | [diff] [blame] | 574 | vkTexture->texturePriv().dirtyMipMaps(false); |
egdaniel | 6693355 | 2016-08-24 07:22:19 -0700 | [diff] [blame] | 575 | } |
egdaniel | 8d2141f | 2016-09-02 11:19:13 -0700 | [diff] [blame] | 576 | } |
Brian Salomon | 18dfa98 | 2017-04-03 16:57:43 -0400 | [diff] [blame] | 577 | set_texture_layout(vkTexture, gpu); |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 578 | } |
| 579 | } |
| 580 | |
Greg Daniel | 500d58b | 2017-08-24 15:59:33 -0400 | [diff] [blame] | 581 | void GrVkGpuRTCommandBuffer::onDraw(const GrPipeline& pipeline, |
| 582 | const GrPrimitiveProcessor& primProc, |
| 583 | const GrMesh meshes[], |
| 584 | const GrPipeline::DynamicState dynamicStates[], |
| 585 | int meshCount, |
| 586 | const SkRect& bounds) { |
Robert Phillips | 19e51dc | 2017-08-09 09:30:51 -0400 | [diff] [blame] | 587 | SkASSERT(pipeline.renderTarget() == fRenderTarget); |
Brian Salomon | c293a29 | 2016-11-30 13:38:32 -0500 | [diff] [blame] | 588 | |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 589 | if (!meshCount) { |
| 590 | return; |
| 591 | } |
egdaniel | 8d2141f | 2016-09-02 11:19:13 -0700 | [diff] [blame] | 592 | prepare_sampled_images(primProc, fGpu); |
bsalomon | b58a2b4 | 2016-09-26 06:55:02 -0700 | [diff] [blame] | 593 | GrFragmentProcessor::Iter iter(pipeline); |
| 594 | while (const GrFragmentProcessor* fp = iter.next()) { |
| 595 | prepare_sampled_images(*fp, fGpu); |
egdaniel | 2f5792a | 2016-07-06 08:51:23 -0700 | [diff] [blame] | 596 | } |
Robert Phillips | bb581ce | 2017-05-29 15:05:15 -0400 | [diff] [blame] | 597 | if (GrTexture* dstTexture = pipeline.peekDstTexture()) { |
| 598 | set_texture_layout(static_cast<GrVkTexture*>(dstTexture), fGpu); |
Brian Salomon | 18dfa98 | 2017-04-03 16:57:43 -0400 | [diff] [blame] | 599 | } |
egdaniel | 2f5792a | 2016-07-06 08:51:23 -0700 | [diff] [blame] | 600 | |
Chris Dalton | bca46e2 | 2017-05-15 11:03:26 -0600 | [diff] [blame] | 601 | GrPrimitiveType primitiveType = meshes[0].primitiveType(); |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 602 | sk_sp<GrVkPipelineState> pipelineState = this->prepareDrawState(pipeline, |
| 603 | primProc, |
Chris Dalton | 46983b7 | 2017-06-06 12:27:16 -0600 | [diff] [blame] | 604 | primitiveType, |
| 605 | SkToBool(dynamicStates)); |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 606 | if (!pipelineState) { |
| 607 | return; |
| 608 | } |
| 609 | |
Chris Dalton | 46983b7 | 2017-06-06 12:27:16 -0600 | [diff] [blame] | 610 | CommandBufferInfo& cbInfo = fCommandBufferInfos[fCurrentCmdInfo]; |
| 611 | |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 612 | for (int i = 0; i < meshCount; ++i) { |
| 613 | const GrMesh& mesh = meshes[i]; |
Chris Dalton | bca46e2 | 2017-05-15 11:03:26 -0600 | [diff] [blame] | 614 | if (mesh.primitiveType() != primitiveType) { |
Chris Dalton | 6f24180 | 2017-05-08 13:58:38 -0400 | [diff] [blame] | 615 | // Technically we don't have to call this here (since there is a safety check in |
| 616 | // pipelineState:setData but this will allow for quicker freeing of resources if the |
| 617 | // pipelineState sits in a cache for a while. |
| 618 | pipelineState->freeTempResources(fGpu); |
| 619 | SkDEBUGCODE(pipelineState = nullptr); |
Chris Dalton | bca46e2 | 2017-05-15 11:03:26 -0600 | [diff] [blame] | 620 | primitiveType = mesh.primitiveType(); |
Chris Dalton | 6f24180 | 2017-05-08 13:58:38 -0400 | [diff] [blame] | 621 | pipelineState = this->prepareDrawState(pipeline, |
| 622 | primProc, |
Chris Dalton | 46983b7 | 2017-06-06 12:27:16 -0600 | [diff] [blame] | 623 | primitiveType, |
| 624 | SkToBool(dynamicStates)); |
Chris Dalton | 6f24180 | 2017-05-08 13:58:38 -0400 | [diff] [blame] | 625 | if (!pipelineState) { |
| 626 | return; |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 627 | } |
Chris Dalton | 6f24180 | 2017-05-08 13:58:38 -0400 | [diff] [blame] | 628 | } |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 629 | |
Chris Dalton | 46983b7 | 2017-06-06 12:27:16 -0600 | [diff] [blame] | 630 | if (dynamicStates) { |
| 631 | if (pipeline.getScissorState().enabled()) { |
| 632 | GrVkPipeline::SetDynamicScissorRectState(fGpu, cbInfo.currentCmdBuf(), |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 633 | fRenderTarget, pipeline.proxy()->origin(), |
Robert Phillips | 19e51dc | 2017-08-09 09:30:51 -0400 | [diff] [blame] | 634 | dynamicStates[i].fScissorRect); |
Chris Dalton | 46983b7 | 2017-06-06 12:27:16 -0600 | [diff] [blame] | 635 | } |
| 636 | } |
| 637 | |
Chris Dalton | bca46e2 | 2017-05-15 11:03:26 -0600 | [diff] [blame] | 638 | SkASSERT(pipelineState); |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 639 | mesh.sendToGpu(primProc, this); |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 640 | } |
| 641 | |
Greg Daniel | 36a77ee | 2016-10-18 10:33:25 -0400 | [diff] [blame] | 642 | cbInfo.fBounds.join(bounds); |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 643 | cbInfo.fIsEmpty = false; |
Greg Daniel | 36a77ee | 2016-10-18 10:33:25 -0400 | [diff] [blame] | 644 | |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 645 | // Technically we don't have to call this here (since there is a safety check in |
| 646 | // pipelineState:setData but this will allow for quicker freeing of resources if the |
| 647 | // pipelineState sits in a cache for a while. |
| 648 | pipelineState->freeTempResources(fGpu); |
egdaniel | 066df7c | 2016-06-08 14:02:27 -0700 | [diff] [blame] | 649 | } |
| 650 | |
Greg Daniel | 500d58b | 2017-08-24 15:59:33 -0400 | [diff] [blame] | 651 | void GrVkGpuRTCommandBuffer::sendInstancedMeshToGpu(const GrPrimitiveProcessor& primProc, |
| 652 | GrPrimitiveType, |
| 653 | const GrBuffer* vertexBuffer, |
| 654 | int vertexCount, |
| 655 | int baseVertex, |
| 656 | const GrBuffer* instanceBuffer, |
| 657 | int instanceCount, |
| 658 | int baseInstance) { |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 659 | CommandBufferInfo& cbInfo = fCommandBufferInfos[fCurrentCmdInfo]; |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 660 | this->bindGeometry(primProc, nullptr, vertexBuffer, instanceBuffer); |
| 661 | cbInfo.currentCmdBuf()->draw(fGpu, vertexCount, instanceCount, baseVertex, baseInstance); |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 662 | fGpu->stats()->incNumDraws(); |
| 663 | } |
| 664 | |
Greg Daniel | 500d58b | 2017-08-24 15:59:33 -0400 | [diff] [blame] | 665 | void GrVkGpuRTCommandBuffer::sendIndexedInstancedMeshToGpu(const GrPrimitiveProcessor& primProc, |
| 666 | GrPrimitiveType, |
| 667 | const GrBuffer* indexBuffer, |
| 668 | int indexCount, |
| 669 | int baseIndex, |
| 670 | const GrBuffer* vertexBuffer, |
| 671 | int baseVertex, |
| 672 | const GrBuffer* instanceBuffer, |
| 673 | int instanceCount, |
| 674 | int baseInstance) { |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 675 | CommandBufferInfo& cbInfo = fCommandBufferInfos[fCurrentCmdInfo]; |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 676 | this->bindGeometry(primProc, indexBuffer, vertexBuffer, instanceBuffer); |
| 677 | cbInfo.currentCmdBuf()->drawIndexed(fGpu, indexCount, instanceCount, |
| 678 | baseIndex, baseVertex, baseInstance); |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 679 | fGpu->stats()->incNumDraws(); |
| 680 | } |
| 681 | |