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 | 09eeefb | 2017-10-16 15:15:02 -0400 | [diff] [blame] | 501 | GrVkPipelineState* GrVkGpuRTCommandBuffer::prepareDrawState(const GrPipeline& pipeline, |
| 502 | const GrPrimitiveProcessor& primProc, |
| 503 | GrPrimitiveType primitiveType, |
| 504 | bool hasDynamicState) { |
Greg Daniel | 22bc865 | 2017-03-22 15:45:43 -0400 | [diff] [blame] | 505 | CommandBufferInfo& cbInfo = fCommandBufferInfos[fCurrentCmdInfo]; |
| 506 | SkASSERT(cbInfo.fRenderPass); |
Greg Daniel | 36a77ee | 2016-10-18 10:33:25 -0400 | [diff] [blame] | 507 | |
Greg Daniel | 09eeefb | 2017-10-16 15:15:02 -0400 | [diff] [blame] | 508 | GrVkPipelineState* pipelineState = |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 509 | fGpu->resourceProvider().findOrCreateCompatiblePipelineState(pipeline, |
| 510 | primProc, |
| 511 | primitiveType, |
Greg Daniel | 36a77ee | 2016-10-18 10:33:25 -0400 | [diff] [blame] | 512 | *cbInfo.fRenderPass); |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 513 | if (!pipelineState) { |
| 514 | return pipelineState; |
| 515 | } |
| 516 | |
Greg Daniel | 22bc865 | 2017-03-22 15:45:43 -0400 | [diff] [blame] | 517 | if (!cbInfo.fIsEmpty && |
Greg Daniel | 09eeefb | 2017-10-16 15:15:02 -0400 | [diff] [blame] | 518 | fLastPipelineState && fLastPipelineState != pipelineState && |
Greg Daniel | e3cd691 | 2017-05-17 11:15:55 -0400 | [diff] [blame] | 519 | fGpu->vkCaps().newCBOnPipelineChange()) { |
Greg Daniel | 22bc865 | 2017-03-22 15:45:43 -0400 | [diff] [blame] | 520 | this->addAdditionalCommandBuffer(); |
| 521 | } |
Greg Daniel | 09eeefb | 2017-10-16 15:15:02 -0400 | [diff] [blame] | 522 | fLastPipelineState = pipelineState; |
Greg Daniel | 22bc865 | 2017-03-22 15:45:43 -0400 | [diff] [blame] | 523 | |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 524 | pipelineState->setData(fGpu, primProc, pipeline); |
| 525 | |
Greg Daniel | 22bc865 | 2017-03-22 15:45:43 -0400 | [diff] [blame] | 526 | pipelineState->bind(fGpu, cbInfo.currentCmdBuf()); |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 527 | |
Robert Phillips | 2890fbf | 2017-07-26 15:48:41 -0400 | [diff] [blame] | 528 | GrRenderTarget* rt = pipeline.renderTarget(); |
Chris Dalton | 46983b7 | 2017-06-06 12:27:16 -0600 | [diff] [blame] | 529 | |
| 530 | if (!pipeline.getScissorState().enabled()) { |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 531 | GrVkPipeline::SetDynamicScissorRectState(fGpu, cbInfo.currentCmdBuf(), |
| 532 | rt, pipeline.proxy()->origin(), |
Chris Dalton | 46983b7 | 2017-06-06 12:27:16 -0600 | [diff] [blame] | 533 | SkIRect::MakeWH(rt->width(), rt->height())); |
| 534 | } else if (!hasDynamicState) { |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 535 | GrVkPipeline::SetDynamicScissorRectState(fGpu, cbInfo.currentCmdBuf(), |
| 536 | rt, pipeline.proxy()->origin(), |
Chris Dalton | 46983b7 | 2017-06-06 12:27:16 -0600 | [diff] [blame] | 537 | pipeline.getScissorState().rect()); |
| 538 | } |
| 539 | GrVkPipeline::SetDynamicViewportState(fGpu, cbInfo.currentCmdBuf(), rt); |
| 540 | GrVkPipeline::SetDynamicBlendConstantState(fGpu, cbInfo.currentCmdBuf(), rt->config(), |
| 541 | pipeline.getXferProcessor()); |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 542 | |
| 543 | return pipelineState; |
| 544 | } |
| 545 | |
Brian Salomon | 18dfa98 | 2017-04-03 16:57:43 -0400 | [diff] [blame] | 546 | static void set_texture_layout(GrVkTexture* vkTexture, GrVkGpu* gpu) { |
| 547 | // TODO: If we ever decide to create the secondary command buffers ahead of time before we |
| 548 | // are actually going to submit them, we will need to track the sampled images and delay |
| 549 | // adding the layout change/barrier until we are ready to submit. |
| 550 | vkTexture->setImageLayout(gpu, |
| 551 | VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, |
| 552 | VK_ACCESS_SHADER_READ_BIT, |
| 553 | VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT, |
| 554 | false); |
| 555 | } |
| 556 | |
Brian Salomon | ab015ef | 2017-04-04 10:15:51 -0400 | [diff] [blame] | 557 | static void prepare_sampled_images(const GrResourceIOProcessor& processor, GrVkGpu* gpu) { |
Brian Salomon | 0bbecb2 | 2016-11-17 11:38:22 -0500 | [diff] [blame] | 558 | for (int i = 0; i < processor.numTextureSamplers(); ++i) { |
Brian Salomon | ab015ef | 2017-04-04 10:15:51 -0400 | [diff] [blame] | 559 | const GrResourceIOProcessor::TextureSampler& sampler = processor.textureSampler(i); |
Robert Phillips | 9bee2e5 | 2017-05-29 12:37:20 -0400 | [diff] [blame] | 560 | GrVkTexture* vkTexture = static_cast<GrVkTexture*>(sampler.peekTexture()); |
egdaniel | 6693355 | 2016-08-24 07:22:19 -0700 | [diff] [blame] | 561 | |
egdaniel | 8d2141f | 2016-09-02 11:19:13 -0700 | [diff] [blame] | 562 | // We may need to resolve the texture first if it is also a render target |
| 563 | GrVkRenderTarget* texRT = static_cast<GrVkRenderTarget*>(vkTexture->asRenderTarget()); |
| 564 | if (texRT) { |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 565 | gpu->onResolveRenderTarget(texRT, sampler.proxy()->origin()); |
egdaniel | 8d2141f | 2016-09-02 11:19:13 -0700 | [diff] [blame] | 566 | } |
| 567 | |
egdaniel | 8d2141f | 2016-09-02 11:19:13 -0700 | [diff] [blame] | 568 | // Check if we need to regenerate any mip maps |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 569 | if (GrSamplerState::Filter::kMipMap == sampler.samplerState().filter()) { |
egdaniel | 8d2141f | 2016-09-02 11:19:13 -0700 | [diff] [blame] | 570 | if (vkTexture->texturePriv().mipMapsAreDirty()) { |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 571 | gpu->generateMipmap(vkTexture, sampler.proxy()->origin()); |
Greg Daniel | 0fc4d2d | 2017-10-12 11:23:36 -0400 | [diff] [blame] | 572 | vkTexture->texturePriv().markMipMapsClean(); |
egdaniel | 6693355 | 2016-08-24 07:22:19 -0700 | [diff] [blame] | 573 | } |
egdaniel | 8d2141f | 2016-09-02 11:19:13 -0700 | [diff] [blame] | 574 | } |
Brian Salomon | 18dfa98 | 2017-04-03 16:57:43 -0400 | [diff] [blame] | 575 | set_texture_layout(vkTexture, gpu); |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 576 | } |
| 577 | } |
| 578 | |
Greg Daniel | 500d58b | 2017-08-24 15:59:33 -0400 | [diff] [blame] | 579 | void GrVkGpuRTCommandBuffer::onDraw(const GrPipeline& pipeline, |
| 580 | const GrPrimitiveProcessor& primProc, |
| 581 | const GrMesh meshes[], |
| 582 | const GrPipeline::DynamicState dynamicStates[], |
| 583 | int meshCount, |
| 584 | const SkRect& bounds) { |
Robert Phillips | 19e51dc | 2017-08-09 09:30:51 -0400 | [diff] [blame] | 585 | SkASSERT(pipeline.renderTarget() == fRenderTarget); |
Brian Salomon | c293a29 | 2016-11-30 13:38:32 -0500 | [diff] [blame] | 586 | |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 587 | if (!meshCount) { |
| 588 | return; |
| 589 | } |
egdaniel | 8d2141f | 2016-09-02 11:19:13 -0700 | [diff] [blame] | 590 | prepare_sampled_images(primProc, fGpu); |
bsalomon | b58a2b4 | 2016-09-26 06:55:02 -0700 | [diff] [blame] | 591 | GrFragmentProcessor::Iter iter(pipeline); |
| 592 | while (const GrFragmentProcessor* fp = iter.next()) { |
| 593 | prepare_sampled_images(*fp, fGpu); |
egdaniel | 2f5792a | 2016-07-06 08:51:23 -0700 | [diff] [blame] | 594 | } |
Robert Phillips | bb581ce | 2017-05-29 15:05:15 -0400 | [diff] [blame] | 595 | if (GrTexture* dstTexture = pipeline.peekDstTexture()) { |
| 596 | set_texture_layout(static_cast<GrVkTexture*>(dstTexture), fGpu); |
Brian Salomon | 18dfa98 | 2017-04-03 16:57:43 -0400 | [diff] [blame] | 597 | } |
egdaniel | 2f5792a | 2016-07-06 08:51:23 -0700 | [diff] [blame] | 598 | |
Chris Dalton | bca46e2 | 2017-05-15 11:03:26 -0600 | [diff] [blame] | 599 | GrPrimitiveType primitiveType = meshes[0].primitiveType(); |
Greg Daniel | 09eeefb | 2017-10-16 15:15:02 -0400 | [diff] [blame] | 600 | GrVkPipelineState* pipelineState = this->prepareDrawState(pipeline, |
| 601 | primProc, |
| 602 | primitiveType, |
| 603 | SkToBool(dynamicStates)); |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 604 | if (!pipelineState) { |
| 605 | return; |
| 606 | } |
| 607 | |
Chris Dalton | 46983b7 | 2017-06-06 12:27:16 -0600 | [diff] [blame] | 608 | CommandBufferInfo& cbInfo = fCommandBufferInfos[fCurrentCmdInfo]; |
| 609 | |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 610 | for (int i = 0; i < meshCount; ++i) { |
| 611 | const GrMesh& mesh = meshes[i]; |
Chris Dalton | bca46e2 | 2017-05-15 11:03:26 -0600 | [diff] [blame] | 612 | if (mesh.primitiveType() != primitiveType) { |
Chris Dalton | 6f24180 | 2017-05-08 13:58:38 -0400 | [diff] [blame] | 613 | // Technically we don't have to call this here (since there is a safety check in |
| 614 | // pipelineState:setData but this will allow for quicker freeing of resources if the |
| 615 | // pipelineState sits in a cache for a while. |
| 616 | pipelineState->freeTempResources(fGpu); |
| 617 | SkDEBUGCODE(pipelineState = nullptr); |
Chris Dalton | bca46e2 | 2017-05-15 11:03:26 -0600 | [diff] [blame] | 618 | primitiveType = mesh.primitiveType(); |
Chris Dalton | 6f24180 | 2017-05-08 13:58:38 -0400 | [diff] [blame] | 619 | pipelineState = this->prepareDrawState(pipeline, |
| 620 | primProc, |
Chris Dalton | 46983b7 | 2017-06-06 12:27:16 -0600 | [diff] [blame] | 621 | primitiveType, |
| 622 | SkToBool(dynamicStates)); |
Chris Dalton | 6f24180 | 2017-05-08 13:58:38 -0400 | [diff] [blame] | 623 | if (!pipelineState) { |
| 624 | return; |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 625 | } |
Chris Dalton | 6f24180 | 2017-05-08 13:58:38 -0400 | [diff] [blame] | 626 | } |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 627 | |
Chris Dalton | 46983b7 | 2017-06-06 12:27:16 -0600 | [diff] [blame] | 628 | if (dynamicStates) { |
| 629 | if (pipeline.getScissorState().enabled()) { |
| 630 | GrVkPipeline::SetDynamicScissorRectState(fGpu, cbInfo.currentCmdBuf(), |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 631 | fRenderTarget, pipeline.proxy()->origin(), |
Robert Phillips | 19e51dc | 2017-08-09 09:30:51 -0400 | [diff] [blame] | 632 | dynamicStates[i].fScissorRect); |
Chris Dalton | 46983b7 | 2017-06-06 12:27:16 -0600 | [diff] [blame] | 633 | } |
| 634 | } |
| 635 | |
Chris Dalton | bca46e2 | 2017-05-15 11:03:26 -0600 | [diff] [blame] | 636 | SkASSERT(pipelineState); |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 637 | mesh.sendToGpu(primProc, this); |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 638 | } |
| 639 | |
Greg Daniel | 36a77ee | 2016-10-18 10:33:25 -0400 | [diff] [blame] | 640 | cbInfo.fBounds.join(bounds); |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 641 | cbInfo.fIsEmpty = false; |
Greg Daniel | 36a77ee | 2016-10-18 10:33:25 -0400 | [diff] [blame] | 642 | |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 643 | // Technically we don't have to call this here (since there is a safety check in |
| 644 | // pipelineState:setData but this will allow for quicker freeing of resources if the |
| 645 | // pipelineState sits in a cache for a while. |
| 646 | pipelineState->freeTempResources(fGpu); |
egdaniel | 066df7c | 2016-06-08 14:02:27 -0700 | [diff] [blame] | 647 | } |
| 648 | |
Greg Daniel | 500d58b | 2017-08-24 15:59:33 -0400 | [diff] [blame] | 649 | void GrVkGpuRTCommandBuffer::sendInstancedMeshToGpu(const GrPrimitiveProcessor& primProc, |
| 650 | GrPrimitiveType, |
| 651 | const GrBuffer* vertexBuffer, |
| 652 | int vertexCount, |
| 653 | int baseVertex, |
| 654 | const GrBuffer* instanceBuffer, |
| 655 | int instanceCount, |
| 656 | int baseInstance) { |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 657 | CommandBufferInfo& cbInfo = fCommandBufferInfos[fCurrentCmdInfo]; |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 658 | this->bindGeometry(primProc, nullptr, vertexBuffer, instanceBuffer); |
| 659 | cbInfo.currentCmdBuf()->draw(fGpu, vertexCount, instanceCount, baseVertex, baseInstance); |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 660 | fGpu->stats()->incNumDraws(); |
| 661 | } |
| 662 | |
Greg Daniel | 500d58b | 2017-08-24 15:59:33 -0400 | [diff] [blame] | 663 | void GrVkGpuRTCommandBuffer::sendIndexedInstancedMeshToGpu(const GrPrimitiveProcessor& primProc, |
| 664 | GrPrimitiveType, |
| 665 | const GrBuffer* indexBuffer, |
| 666 | int indexCount, |
| 667 | int baseIndex, |
| 668 | const GrBuffer* vertexBuffer, |
| 669 | int baseVertex, |
| 670 | const GrBuffer* instanceBuffer, |
| 671 | int instanceCount, |
| 672 | int baseInstance) { |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 673 | CommandBufferInfo& cbInfo = fCommandBufferInfos[fCurrentCmdInfo]; |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 674 | this->bindGeometry(primProc, indexBuffer, vertexBuffer, instanceBuffer); |
| 675 | cbInfo.currentCmdBuf()->drawIndexed(fGpu, indexCount, instanceCount, |
| 676 | baseIndex, baseVertex, baseInstance); |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 677 | fGpu->stats()->incNumDraws(); |
| 678 | } |
| 679 | |