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