Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "src/gpu/vk/GrVkCommandBuffer.h" |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 9 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "include/core/SkRect.h" |
| 11 | #include "src/gpu/vk/GrVkCommandPool.h" |
| 12 | #include "src/gpu/vk/GrVkFramebuffer.h" |
| 13 | #include "src/gpu/vk/GrVkGpu.h" |
| 14 | #include "src/gpu/vk/GrVkImage.h" |
| 15 | #include "src/gpu/vk/GrVkImageView.h" |
Chris Dalton | 10ee0b2 | 2020-04-02 16:28:52 -0600 | [diff] [blame] | 16 | #include "src/gpu/vk/GrVkMeshBuffer.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 17 | #include "src/gpu/vk/GrVkPipeline.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 18 | #include "src/gpu/vk/GrVkPipelineState.h" |
| 19 | #include "src/gpu/vk/GrVkPipelineState.h" |
| 20 | #include "src/gpu/vk/GrVkRenderPass.h" |
| 21 | #include "src/gpu/vk/GrVkRenderTarget.h" |
| 22 | #include "src/gpu/vk/GrVkTransferBuffer.h" |
| 23 | #include "src/gpu/vk/GrVkUtil.h" |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 24 | |
| 25 | void GrVkCommandBuffer::invalidateState() { |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 26 | for (auto& boundInputBuffer : fBoundInputBuffers) { |
| 27 | boundInputBuffer = VK_NULL_HANDLE; |
| 28 | } |
egdaniel | 470d77a | 2016-03-18 12:50:27 -0700 | [diff] [blame] | 29 | fBoundIndexBuffer = VK_NULL_HANDLE; |
egdaniel | 470d77a | 2016-03-18 12:50:27 -0700 | [diff] [blame] | 30 | |
| 31 | memset(&fCachedViewport, 0, sizeof(VkViewport)); |
| 32 | fCachedViewport.width = - 1.0f; // Viewport must have a width greater than 0 |
| 33 | |
| 34 | memset(&fCachedScissor, 0, sizeof(VkRect2D)); |
| 35 | fCachedScissor.offset.x = -1; // Scissor offset must be greater that 0 to be valid |
| 36 | |
| 37 | for (int i = 0; i < 4; ++i) { |
| 38 | fCachedBlendConstant[i] = -1.0; |
| 39 | } |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 40 | } |
| 41 | |
Jim Van Verth | 5082df1 | 2020-03-11 16:14:51 -0400 | [diff] [blame] | 42 | void GrVkCommandBuffer::freeGPUData(const GrGpu* gpu, VkCommandPool cmdPool) const { |
Brian Salomon | e39526b | 2019-06-24 16:35:53 -0400 | [diff] [blame] | 43 | TRACE_EVENT0("skia.gpu", TRACE_FUNC); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 44 | SkASSERT(!fIsActive); |
Greg Daniel | 0addbdf | 2019-11-25 15:03:58 -0500 | [diff] [blame] | 45 | SkASSERT(!fTrackedResources.count()); |
| 46 | SkASSERT(!fTrackedRecycledResources.count()); |
| 47 | SkASSERT(cmdPool != VK_NULL_HANDLE); |
| 48 | SkASSERT(!this->isWrapped()); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 49 | |
Jim Van Verth | 3e19216 | 2020-03-10 16:23:16 -0400 | [diff] [blame] | 50 | GrVkGpu* vkGpu = (GrVkGpu*)gpu; |
| 51 | GR_VK_CALL(vkGpu->vkInterface(), FreeCommandBuffers(vkGpu->device(), cmdPool, 1, &fCmdBuffer)); |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 52 | |
Jim Van Verth | 3e19216 | 2020-03-10 16:23:16 -0400 | [diff] [blame] | 53 | this->onFreeGPUData(vkGpu); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 54 | } |
| 55 | |
Jim Van Verth | 5082df1 | 2020-03-11 16:14:51 -0400 | [diff] [blame] | 56 | void GrVkCommandBuffer::releaseResources() { |
Brian Salomon | e39526b | 2019-06-24 16:35:53 -0400 | [diff] [blame] | 57 | TRACE_EVENT0("skia.gpu", TRACE_FUNC); |
jvanverth | 7ec9241 | 2016-07-06 09:24:57 -0700 | [diff] [blame] | 58 | SkASSERT(!fIsActive); |
| 59 | for (int i = 0; i < fTrackedResources.count(); ++i) { |
Jim Van Verth | 3e19216 | 2020-03-10 16:23:16 -0400 | [diff] [blame] | 60 | fTrackedResources[i]->notifyFinishedWithWorkOnGpu(); |
Jim Van Verth | 5082df1 | 2020-03-11 16:14:51 -0400 | [diff] [blame] | 61 | fTrackedResources[i]->unref(); |
jvanverth | 7ec9241 | 2016-07-06 09:24:57 -0700 | [diff] [blame] | 62 | } |
egdaniel | c1be9bc | 2016-07-20 08:33:00 -0700 | [diff] [blame] | 63 | for (int i = 0; i < fTrackedRecycledResources.count(); ++i) { |
Jim Van Verth | 3e19216 | 2020-03-10 16:23:16 -0400 | [diff] [blame] | 64 | fTrackedRecycledResources[i]->notifyFinishedWithWorkOnGpu(); |
Jim Van Verth | 5082df1 | 2020-03-11 16:14:51 -0400 | [diff] [blame] | 65 | fTrackedRecycledResources[i]->recycle(); |
egdaniel | c1be9bc | 2016-07-20 08:33:00 -0700 | [diff] [blame] | 66 | } |
egdaniel | 594739c | 2016-09-20 12:39:25 -0700 | [diff] [blame] | 67 | |
| 68 | if (++fNumResets > kNumRewindResetsBeforeFullReset) { |
| 69 | fTrackedResources.reset(); |
| 70 | fTrackedRecycledResources.reset(); |
| 71 | fTrackedResources.setReserve(kInitialTrackedResourcesCount); |
| 72 | fTrackedRecycledResources.setReserve(kInitialTrackedResourcesCount); |
| 73 | fNumResets = 0; |
| 74 | } else { |
| 75 | fTrackedResources.rewind(); |
| 76 | fTrackedRecycledResources.rewind(); |
| 77 | } |
| 78 | |
jvanverth | 7ec9241 | 2016-07-06 09:24:57 -0700 | [diff] [blame] | 79 | this->invalidateState(); |
| 80 | |
Jim Van Verth | 5082df1 | 2020-03-11 16:14:51 -0400 | [diff] [blame] | 81 | this->onReleaseResources(); |
jvanverth | 7ec9241 | 2016-07-06 09:24:57 -0700 | [diff] [blame] | 82 | } |
| 83 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 84 | //////////////////////////////////////////////////////////////////////////////// |
| 85 | // CommandBuffer commands |
| 86 | //////////////////////////////////////////////////////////////////////////////// |
| 87 | |
| 88 | void GrVkCommandBuffer::pipelineBarrier(const GrVkGpu* gpu, |
Jim Van Verth | 3e19216 | 2020-03-10 16:23:16 -0400 | [diff] [blame] | 89 | const GrManagedResource* resource, |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 90 | VkPipelineStageFlags srcStageMask, |
| 91 | VkPipelineStageFlags dstStageMask, |
| 92 | bool byRegion, |
| 93 | BarrierType barrierType, |
Greg Daniel | 59dc148 | 2019-02-22 10:46:38 -0500 | [diff] [blame] | 94 | void* barrier) { |
Greg Daniel | 070cbaf | 2019-01-03 17:35:54 -0500 | [diff] [blame] | 95 | SkASSERT(!this->isWrapped()); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 96 | SkASSERT(fIsActive); |
egdaniel | 58a8d92 | 2016-04-21 08:03:10 -0700 | [diff] [blame] | 97 | // For images we can have barriers inside of render passes but they require us to add more |
| 98 | // support in subpasses which need self dependencies to have barriers inside them. Also, we can |
| 99 | // never have buffer barriers inside of a render pass. For now we will just assert that we are |
| 100 | // not in a render pass. |
| 101 | SkASSERT(!fActiveRenderPass); |
Greg Daniel | f346df3 | 2019-04-03 14:52:13 -0400 | [diff] [blame] | 102 | |
Greg Daniel | ee54f23 | 2019-04-03 14:58:40 -0400 | [diff] [blame] | 103 | if (barrierType == kBufferMemory_BarrierType) { |
| 104 | const VkBufferMemoryBarrier* barrierPtr = reinterpret_cast<VkBufferMemoryBarrier*>(barrier); |
| 105 | fBufferBarriers.push_back(*barrierPtr); |
| 106 | } else { |
| 107 | SkASSERT(barrierType == kImageMemory_BarrierType); |
| 108 | const VkImageMemoryBarrier* barrierPtr = reinterpret_cast<VkImageMemoryBarrier*>(barrier); |
Greg Daniel | 212ff05 | 2019-04-09 10:41:34 -0400 | [diff] [blame] | 109 | // We need to check if we are adding a pipeline barrier that covers part of the same |
| 110 | // subresource range as a barrier that is already in current batch. If it does, then we must |
| 111 | // submit the first batch because the vulkan spec does not define a specific ordering for |
| 112 | // barriers submitted in the same batch. |
| 113 | // TODO: Look if we can gain anything by merging barriers together instead of submitting |
| 114 | // the old ones. |
| 115 | for (int i = 0; i < fImageBarriers.count(); ++i) { |
| 116 | VkImageMemoryBarrier& currentBarrier = fImageBarriers[i]; |
| 117 | if (barrierPtr->image == currentBarrier.image) { |
| 118 | const VkImageSubresourceRange newRange = barrierPtr->subresourceRange; |
| 119 | const VkImageSubresourceRange oldRange = currentBarrier.subresourceRange; |
| 120 | SkASSERT(newRange.aspectMask == oldRange.aspectMask); |
| 121 | SkASSERT(newRange.baseArrayLayer == oldRange.baseArrayLayer); |
| 122 | SkASSERT(newRange.layerCount == oldRange.layerCount); |
| 123 | uint32_t newStart = newRange.baseMipLevel; |
| 124 | uint32_t newEnd = newRange.baseMipLevel + newRange.levelCount - 1; |
| 125 | uint32_t oldStart = oldRange.baseMipLevel; |
| 126 | uint32_t oldEnd = oldRange.baseMipLevel + oldRange.levelCount - 1; |
Brian Osman | 788b916 | 2020-02-07 10:36:46 -0500 | [diff] [blame] | 127 | if (std::max(newStart, oldStart) <= std::min(newEnd, oldEnd)) { |
Greg Daniel | 212ff05 | 2019-04-09 10:41:34 -0400 | [diff] [blame] | 128 | this->submitPipelineBarriers(gpu); |
| 129 | break; |
| 130 | } |
| 131 | } |
| 132 | } |
Greg Daniel | ee54f23 | 2019-04-03 14:58:40 -0400 | [diff] [blame] | 133 | fImageBarriers.push_back(*barrierPtr); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 134 | } |
Greg Daniel | ee54f23 | 2019-04-03 14:58:40 -0400 | [diff] [blame] | 135 | fBarriersByRegion |= byRegion; |
| 136 | |
| 137 | fSrcStageMask = fSrcStageMask | srcStageMask; |
| 138 | fDstStageMask = fDstStageMask | dstStageMask; |
| 139 | |
| 140 | fHasWork = true; |
Greg Daniel | 59dc148 | 2019-02-22 10:46:38 -0500 | [diff] [blame] | 141 | if (resource) { |
| 142 | this->addResource(resource); |
| 143 | } |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 144 | } |
| 145 | |
Greg Daniel | ee54f23 | 2019-04-03 14:58:40 -0400 | [diff] [blame] | 146 | void GrVkCommandBuffer::submitPipelineBarriers(const GrVkGpu* gpu) { |
| 147 | SkASSERT(fIsActive); |
| 148 | |
| 149 | // Currently we never submit a pipeline barrier without at least one memory barrier. |
| 150 | if (fBufferBarriers.count() || fImageBarriers.count()) { |
| 151 | // For images we can have barriers inside of render passes but they require us to add more |
| 152 | // support in subpasses which need self dependencies to have barriers inside them. Also, we |
| 153 | // can never have buffer barriers inside of a render pass. For now we will just assert that |
| 154 | // we are not in a render pass. |
| 155 | SkASSERT(!fActiveRenderPass); |
| 156 | SkASSERT(!this->isWrapped()); |
| 157 | SkASSERT(fSrcStageMask && fDstStageMask); |
| 158 | |
| 159 | VkDependencyFlags dependencyFlags = fBarriersByRegion ? VK_DEPENDENCY_BY_REGION_BIT : 0; |
| 160 | GR_VK_CALL(gpu->vkInterface(), CmdPipelineBarrier( |
| 161 | fCmdBuffer, fSrcStageMask, fDstStageMask, dependencyFlags, 0, nullptr, |
| 162 | fBufferBarriers.count(), fBufferBarriers.begin(), |
| 163 | fImageBarriers.count(), fImageBarriers.begin())); |
| 164 | fBufferBarriers.reset(); |
| 165 | fImageBarriers.reset(); |
| 166 | fBarriersByRegion = false; |
| 167 | fSrcStageMask = 0; |
| 168 | fDstStageMask = 0; |
| 169 | } |
| 170 | SkASSERT(!fBufferBarriers.count()); |
| 171 | SkASSERT(!fImageBarriers.count()); |
| 172 | SkASSERT(!fBarriersByRegion); |
| 173 | SkASSERT(!fSrcStageMask); |
| 174 | SkASSERT(!fDstStageMask); |
| 175 | } |
| 176 | |
| 177 | |
Greg Daniel | 6ecc911 | 2017-06-16 16:17:03 +0000 | [diff] [blame] | 178 | void GrVkCommandBuffer::bindInputBuffer(GrVkGpu* gpu, uint32_t binding, |
Chris Dalton | 10ee0b2 | 2020-04-02 16:28:52 -0600 | [diff] [blame] | 179 | const GrVkMeshBuffer* vbuffer) { |
Greg Daniel | 6ecc911 | 2017-06-16 16:17:03 +0000 | [diff] [blame] | 180 | VkBuffer vkBuffer = vbuffer->buffer(); |
| 181 | SkASSERT(VK_NULL_HANDLE != vkBuffer); |
| 182 | SkASSERT(binding < kMaxInputBuffers); |
| 183 | // TODO: once vbuffer->offset() no longer always returns 0, we will need to track the offset |
| 184 | // to know if we can skip binding or not. |
| 185 | if (vkBuffer != fBoundInputBuffers[binding]) { |
| 186 | VkDeviceSize offset = vbuffer->offset(); |
| 187 | GR_VK_CALL(gpu->vkInterface(), CmdBindVertexBuffers(fCmdBuffer, |
| 188 | binding, |
| 189 | 1, |
| 190 | &vkBuffer, |
| 191 | &offset)); |
| 192 | fBoundInputBuffers[binding] = vkBuffer; |
Greg Daniel | 59dc148 | 2019-02-22 10:46:38 -0500 | [diff] [blame] | 193 | this->addResource(vbuffer->resource()); |
Greg Daniel | 6ecc911 | 2017-06-16 16:17:03 +0000 | [diff] [blame] | 194 | } |
| 195 | } |
| 196 | |
Chris Dalton | 10ee0b2 | 2020-04-02 16:28:52 -0600 | [diff] [blame] | 197 | void GrVkCommandBuffer::bindIndexBuffer(GrVkGpu* gpu, const GrVkMeshBuffer* ibuffer) { |
Greg Daniel | 6ecc911 | 2017-06-16 16:17:03 +0000 | [diff] [blame] | 198 | VkBuffer vkBuffer = ibuffer->buffer(); |
| 199 | SkASSERT(VK_NULL_HANDLE != vkBuffer); |
| 200 | // TODO: once ibuffer->offset() no longer always returns 0, we will need to track the offset |
| 201 | // to know if we can skip binding or not. |
| 202 | if (vkBuffer != fBoundIndexBuffer) { |
| 203 | GR_VK_CALL(gpu->vkInterface(), CmdBindIndexBuffer(fCmdBuffer, |
| 204 | vkBuffer, |
| 205 | ibuffer->offset(), |
| 206 | VK_INDEX_TYPE_UINT16)); |
| 207 | fBoundIndexBuffer = vkBuffer; |
Greg Daniel | 59dc148 | 2019-02-22 10:46:38 -0500 | [diff] [blame] | 208 | this->addResource(ibuffer->resource()); |
Greg Daniel | 6ecc911 | 2017-06-16 16:17:03 +0000 | [diff] [blame] | 209 | } |
| 210 | } |
| 211 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 212 | void GrVkCommandBuffer::clearAttachments(const GrVkGpu* gpu, |
| 213 | int numAttachments, |
| 214 | const VkClearAttachment* attachments, |
| 215 | int numRects, |
Greg Daniel | f346df3 | 2019-04-03 14:52:13 -0400 | [diff] [blame] | 216 | const VkClearRect* clearRects) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 217 | SkASSERT(fIsActive); |
| 218 | SkASSERT(fActiveRenderPass); |
| 219 | SkASSERT(numAttachments > 0); |
| 220 | SkASSERT(numRects > 0); |
Greg Daniel | f346df3 | 2019-04-03 14:52:13 -0400 | [diff] [blame] | 221 | |
Greg Daniel | ee54f23 | 2019-04-03 14:58:40 -0400 | [diff] [blame] | 222 | this->addingWork(gpu); |
Greg Daniel | f346df3 | 2019-04-03 14:52:13 -0400 | [diff] [blame] | 223 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 224 | #ifdef SK_DEBUG |
| 225 | for (int i = 0; i < numAttachments; ++i) { |
| 226 | if (attachments[i].aspectMask == VK_IMAGE_ASPECT_COLOR_BIT) { |
| 227 | uint32_t testIndex; |
| 228 | SkAssertResult(fActiveRenderPass->colorAttachmentIndex(&testIndex)); |
| 229 | SkASSERT(testIndex == attachments[i].colorAttachment); |
| 230 | } |
| 231 | } |
| 232 | #endif |
| 233 | GR_VK_CALL(gpu->vkInterface(), CmdClearAttachments(fCmdBuffer, |
| 234 | numAttachments, |
| 235 | attachments, |
| 236 | numRects, |
| 237 | clearRects)); |
Greg Daniel | a718a61 | 2019-10-07 16:25:41 -0400 | [diff] [blame] | 238 | if (gpu->vkCaps().mustInvalidatePrimaryCmdBufferStateAfterClearAttachments()) { |
| 239 | this->invalidateState(); |
| 240 | } |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | void GrVkCommandBuffer::bindDescriptorSets(const GrVkGpu* gpu, |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 244 | GrVkPipelineState* pipelineState, |
Greg Daniel | eecc687 | 2019-07-29 13:21:37 -0400 | [diff] [blame] | 245 | VkPipelineLayout layout, |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 246 | uint32_t firstSet, |
| 247 | uint32_t setCount, |
| 248 | const VkDescriptorSet* descriptorSets, |
| 249 | uint32_t dynamicOffsetCount, |
| 250 | const uint32_t* dynamicOffsets) { |
| 251 | SkASSERT(fIsActive); |
| 252 | GR_VK_CALL(gpu->vkInterface(), CmdBindDescriptorSets(fCmdBuffer, |
| 253 | VK_PIPELINE_BIND_POINT_GRAPHICS, |
Greg Daniel | eecc687 | 2019-07-29 13:21:37 -0400 | [diff] [blame] | 254 | layout, |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 255 | firstSet, |
| 256 | setCount, |
| 257 | descriptorSets, |
| 258 | dynamicOffsetCount, |
| 259 | dynamicOffsets)); |
egdaniel | bc9b296 | 2016-09-27 08:00:53 -0700 | [diff] [blame] | 260 | } |
| 261 | |
egdaniel | 470d77a | 2016-03-18 12:50:27 -0700 | [diff] [blame] | 262 | void GrVkCommandBuffer::bindPipeline(const GrVkGpu* gpu, const GrVkPipeline* pipeline) { |
| 263 | SkASSERT(fIsActive); |
egdaniel | 470d77a | 2016-03-18 12:50:27 -0700 | [diff] [blame] | 264 | GR_VK_CALL(gpu->vkInterface(), CmdBindPipeline(fCmdBuffer, |
| 265 | VK_PIPELINE_BIND_POINT_GRAPHICS, |
| 266 | pipeline->pipeline())); |
egdaniel | ec44099 | 2016-09-13 09:54:11 -0700 | [diff] [blame] | 267 | this->addResource(pipeline); |
egdaniel | 470d77a | 2016-03-18 12:50:27 -0700 | [diff] [blame] | 268 | } |
| 269 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 270 | void GrVkCommandBuffer::drawIndexed(const GrVkGpu* gpu, |
| 271 | uint32_t indexCount, |
| 272 | uint32_t instanceCount, |
| 273 | uint32_t firstIndex, |
| 274 | int32_t vertexOffset, |
Greg Daniel | f346df3 | 2019-04-03 14:52:13 -0400 | [diff] [blame] | 275 | uint32_t firstInstance) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 276 | SkASSERT(fIsActive); |
| 277 | SkASSERT(fActiveRenderPass); |
Greg Daniel | ee54f23 | 2019-04-03 14:58:40 -0400 | [diff] [blame] | 278 | this->addingWork(gpu); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 279 | GR_VK_CALL(gpu->vkInterface(), CmdDrawIndexed(fCmdBuffer, |
| 280 | indexCount, |
| 281 | instanceCount, |
| 282 | firstIndex, |
| 283 | vertexOffset, |
| 284 | firstInstance)); |
| 285 | } |
| 286 | |
| 287 | void GrVkCommandBuffer::draw(const GrVkGpu* gpu, |
| 288 | uint32_t vertexCount, |
| 289 | uint32_t instanceCount, |
| 290 | uint32_t firstVertex, |
Greg Daniel | f346df3 | 2019-04-03 14:52:13 -0400 | [diff] [blame] | 291 | uint32_t firstInstance) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 292 | SkASSERT(fIsActive); |
| 293 | SkASSERT(fActiveRenderPass); |
Greg Daniel | ee54f23 | 2019-04-03 14:58:40 -0400 | [diff] [blame] | 294 | this->addingWork(gpu); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 295 | GR_VK_CALL(gpu->vkInterface(), CmdDraw(fCmdBuffer, |
| 296 | vertexCount, |
| 297 | instanceCount, |
| 298 | firstVertex, |
| 299 | firstInstance)); |
| 300 | } |
egdaniel | 470d77a | 2016-03-18 12:50:27 -0700 | [diff] [blame] | 301 | |
Chris Dalton | 03fdf6a | 2020-04-07 12:31:59 -0600 | [diff] [blame^] | 302 | void GrVkCommandBuffer::drawIndirect(const GrVkGpu* gpu, |
| 303 | const GrVkMeshBuffer* indirectBuffer, |
| 304 | VkDeviceSize offset, |
| 305 | uint32_t drawCount, |
| 306 | uint32_t stride) { |
| 307 | SkASSERT(fIsActive); |
| 308 | SkASSERT(fActiveRenderPass); |
| 309 | SkASSERT(!indirectBuffer->isCpuBuffer()); |
| 310 | this->addingWork(gpu); |
| 311 | GR_VK_CALL(gpu->vkInterface(), CmdDrawIndirect(fCmdBuffer, |
| 312 | indirectBuffer->buffer(), |
| 313 | offset, |
| 314 | drawCount, |
| 315 | stride)); |
| 316 | } |
| 317 | |
| 318 | void GrVkCommandBuffer::drawIndexedIndirect(const GrVkGpu* gpu, |
| 319 | const GrVkMeshBuffer* indirectBuffer, |
| 320 | VkDeviceSize offset, |
| 321 | uint32_t drawCount, |
| 322 | uint32_t stride) { |
| 323 | SkASSERT(fIsActive); |
| 324 | SkASSERT(fActiveRenderPass); |
| 325 | SkASSERT(!indirectBuffer->isCpuBuffer()); |
| 326 | this->addingWork(gpu); |
| 327 | GR_VK_CALL(gpu->vkInterface(), CmdDrawIndexedIndirect(fCmdBuffer, |
| 328 | indirectBuffer->buffer(), |
| 329 | offset, |
| 330 | drawCount, |
| 331 | stride)); |
| 332 | } |
| 333 | |
egdaniel | 470d77a | 2016-03-18 12:50:27 -0700 | [diff] [blame] | 334 | void GrVkCommandBuffer::setViewport(const GrVkGpu* gpu, |
| 335 | uint32_t firstViewport, |
| 336 | uint32_t viewportCount, |
| 337 | const VkViewport* viewports) { |
| 338 | SkASSERT(fIsActive); |
| 339 | SkASSERT(1 == viewportCount); |
| 340 | if (memcmp(viewports, &fCachedViewport, sizeof(VkViewport))) { |
| 341 | GR_VK_CALL(gpu->vkInterface(), CmdSetViewport(fCmdBuffer, |
| 342 | firstViewport, |
| 343 | viewportCount, |
| 344 | viewports)); |
| 345 | fCachedViewport = viewports[0]; |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | void GrVkCommandBuffer::setScissor(const GrVkGpu* gpu, |
| 350 | uint32_t firstScissor, |
| 351 | uint32_t scissorCount, |
| 352 | const VkRect2D* scissors) { |
| 353 | SkASSERT(fIsActive); |
| 354 | SkASSERT(1 == scissorCount); |
| 355 | if (memcmp(scissors, &fCachedScissor, sizeof(VkRect2D))) { |
| 356 | GR_VK_CALL(gpu->vkInterface(), CmdSetScissor(fCmdBuffer, |
| 357 | firstScissor, |
| 358 | scissorCount, |
| 359 | scissors)); |
| 360 | fCachedScissor = scissors[0]; |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | void GrVkCommandBuffer::setBlendConstants(const GrVkGpu* gpu, |
| 365 | const float blendConstants[4]) { |
| 366 | SkASSERT(fIsActive); |
| 367 | if (memcmp(blendConstants, fCachedBlendConstant, 4 * sizeof(float))) { |
| 368 | GR_VK_CALL(gpu->vkInterface(), CmdSetBlendConstants(fCmdBuffer, blendConstants)); |
| 369 | memcpy(fCachedBlendConstant, blendConstants, 4 * sizeof(float)); |
| 370 | } |
| 371 | } |
egdaniel | 9a6cf80 | 2016-06-08 08:22:05 -0700 | [diff] [blame] | 372 | |
Greg Daniel | ee54f23 | 2019-04-03 14:58:40 -0400 | [diff] [blame] | 373 | void GrVkCommandBuffer::addingWork(const GrVkGpu* gpu) { |
| 374 | this->submitPipelineBarriers(gpu); |
| 375 | fHasWork = true; |
| 376 | } |
| 377 | |
egdaniel | 9a6cf80 | 2016-06-08 08:22:05 -0700 | [diff] [blame] | 378 | /////////////////////////////////////////////////////////////////////////////// |
| 379 | // PrimaryCommandBuffer |
| 380 | //////////////////////////////////////////////////////////////////////////////// |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 381 | GrVkPrimaryCommandBuffer::~GrVkPrimaryCommandBuffer() { |
| 382 | // Should have ended any render pass we're in the middle of |
| 383 | SkASSERT(!fActiveRenderPass); |
| 384 | } |
| 385 | |
Greg Daniel | 315c8dc | 2019-11-26 15:41:27 -0500 | [diff] [blame] | 386 | GrVkPrimaryCommandBuffer* GrVkPrimaryCommandBuffer::Create(GrVkGpu* gpu, |
Greg Daniel | 0addbdf | 2019-11-25 15:03:58 -0500 | [diff] [blame] | 387 | VkCommandPool cmdPool) { |
egdaniel | 9a6cf80 | 2016-06-08 08:22:05 -0700 | [diff] [blame] | 388 | const VkCommandBufferAllocateInfo cmdInfo = { |
| 389 | VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, // sType |
Ben Wagner | a93a14a | 2017-08-28 10:34:05 -0400 | [diff] [blame] | 390 | nullptr, // pNext |
Greg Daniel | 0addbdf | 2019-11-25 15:03:58 -0500 | [diff] [blame] | 391 | cmdPool, // commandPool |
egdaniel | 9a6cf80 | 2016-06-08 08:22:05 -0700 | [diff] [blame] | 392 | VK_COMMAND_BUFFER_LEVEL_PRIMARY, // level |
| 393 | 1 // bufferCount |
| 394 | }; |
| 395 | |
| 396 | VkCommandBuffer cmdBuffer; |
Greg Daniel | 315c8dc | 2019-11-26 15:41:27 -0500 | [diff] [blame] | 397 | VkResult err; |
| 398 | GR_VK_CALL_RESULT(gpu, err, AllocateCommandBuffers(gpu->device(), &cmdInfo, &cmdBuffer)); |
egdaniel | 9a6cf80 | 2016-06-08 08:22:05 -0700 | [diff] [blame] | 399 | if (err) { |
| 400 | return nullptr; |
| 401 | } |
Greg Daniel | 0addbdf | 2019-11-25 15:03:58 -0500 | [diff] [blame] | 402 | return new GrVkPrimaryCommandBuffer(cmdBuffer); |
egdaniel | 9a6cf80 | 2016-06-08 08:22:05 -0700 | [diff] [blame] | 403 | } |
| 404 | |
Greg Daniel | e643da6 | 2019-11-05 12:36:42 -0500 | [diff] [blame] | 405 | void GrVkPrimaryCommandBuffer::begin(GrVkGpu* gpu) { |
egdaniel | 9a6cf80 | 2016-06-08 08:22:05 -0700 | [diff] [blame] | 406 | SkASSERT(!fIsActive); |
| 407 | VkCommandBufferBeginInfo cmdBufferBeginInfo; |
| 408 | memset(&cmdBufferBeginInfo, 0, sizeof(VkCommandBufferBeginInfo)); |
| 409 | cmdBufferBeginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO; |
| 410 | cmdBufferBeginInfo.pNext = nullptr; |
| 411 | cmdBufferBeginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT; |
| 412 | cmdBufferBeginInfo.pInheritanceInfo = nullptr; |
| 413 | |
Greg Daniel | e643da6 | 2019-11-05 12:36:42 -0500 | [diff] [blame] | 414 | GR_VK_CALL_ERRCHECK(gpu, BeginCommandBuffer(fCmdBuffer, &cmdBufferBeginInfo)); |
egdaniel | 9a6cf80 | 2016-06-08 08:22:05 -0700 | [diff] [blame] | 415 | fIsActive = true; |
| 416 | } |
| 417 | |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 418 | void GrVkPrimaryCommandBuffer::end(GrVkGpu* gpu) { |
egdaniel | 9a6cf80 | 2016-06-08 08:22:05 -0700 | [diff] [blame] | 419 | SkASSERT(fIsActive); |
| 420 | SkASSERT(!fActiveRenderPass); |
Greg Daniel | ee54f23 | 2019-04-03 14:58:40 -0400 | [diff] [blame] | 421 | |
| 422 | this->submitPipelineBarriers(gpu); |
| 423 | |
Greg Daniel | e643da6 | 2019-11-05 12:36:42 -0500 | [diff] [blame] | 424 | GR_VK_CALL_ERRCHECK(gpu, EndCommandBuffer(fCmdBuffer)); |
egdaniel | 9a6cf80 | 2016-06-08 08:22:05 -0700 | [diff] [blame] | 425 | this->invalidateState(); |
| 426 | fIsActive = false; |
Robert Phillips | 04d2ce2 | 2019-04-03 13:20:43 -0400 | [diff] [blame] | 427 | fHasWork = false; |
egdaniel | 9a6cf80 | 2016-06-08 08:22:05 -0700 | [diff] [blame] | 428 | } |
| 429 | |
Greg Daniel | fa3adf7 | 2019-11-07 09:53:41 -0500 | [diff] [blame] | 430 | bool GrVkPrimaryCommandBuffer::beginRenderPass(GrVkGpu* gpu, |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 431 | const GrVkRenderPass* renderPass, |
Robert Phillips | 9521447 | 2017-08-08 18:00:03 -0400 | [diff] [blame] | 432 | const VkClearValue clearValues[], |
Greg Daniel | fa3adf7 | 2019-11-07 09:53:41 -0500 | [diff] [blame] | 433 | GrVkRenderTarget* target, |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 434 | const SkIRect& bounds, |
| 435 | bool forSecondaryCB) { |
egdaniel | 9a6cf80 | 2016-06-08 08:22:05 -0700 | [diff] [blame] | 436 | SkASSERT(fIsActive); |
| 437 | SkASSERT(!fActiveRenderPass); |
Greg Daniel | fa3adf7 | 2019-11-07 09:53:41 -0500 | [diff] [blame] | 438 | SkASSERT(renderPass->isCompatible(*target)); |
| 439 | |
| 440 | const GrVkFramebuffer* framebuffer = target->getFramebuffer(); |
| 441 | if (!framebuffer) { |
| 442 | return false; |
| 443 | } |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 444 | |
Greg Daniel | ee54f23 | 2019-04-03 14:58:40 -0400 | [diff] [blame] | 445 | this->addingWork(gpu); |
Greg Daniel | f346df3 | 2019-04-03 14:52:13 -0400 | [diff] [blame] | 446 | |
egdaniel | 9a6cf80 | 2016-06-08 08:22:05 -0700 | [diff] [blame] | 447 | VkRenderPassBeginInfo beginInfo; |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 448 | VkRect2D renderArea; |
| 449 | renderArea.offset = { bounds.fLeft , bounds.fTop }; |
| 450 | renderArea.extent = { (uint32_t)bounds.width(), (uint32_t)bounds.height() }; |
| 451 | |
| 452 | memset(&beginInfo, 0, sizeof(VkRenderPassBeginInfo)); |
| 453 | beginInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO; |
| 454 | beginInfo.pNext = nullptr; |
| 455 | beginInfo.renderPass = renderPass->vkRenderPass(); |
Greg Daniel | fa3adf7 | 2019-11-07 09:53:41 -0500 | [diff] [blame] | 456 | beginInfo.framebuffer = framebuffer->framebuffer(); |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 457 | beginInfo.renderArea = renderArea; |
Greg Daniel | b68319a | 2018-02-23 16:08:28 -0500 | [diff] [blame] | 458 | beginInfo.clearValueCount = renderPass->clearValueCount(); |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 459 | beginInfo.pClearValues = clearValues; |
| 460 | |
| 461 | VkSubpassContents contents = forSecondaryCB ? VK_SUBPASS_CONTENTS_SECONDARY_COMMAND_BUFFERS |
| 462 | : VK_SUBPASS_CONTENTS_INLINE; |
| 463 | |
egdaniel | 9a6cf80 | 2016-06-08 08:22:05 -0700 | [diff] [blame] | 464 | GR_VK_CALL(gpu->vkInterface(), CmdBeginRenderPass(fCmdBuffer, &beginInfo, contents)); |
| 465 | fActiveRenderPass = renderPass; |
| 466 | this->addResource(renderPass); |
Greg Daniel | fa3adf7 | 2019-11-07 09:53:41 -0500 | [diff] [blame] | 467 | target->addResources(*this); |
| 468 | return true; |
egdaniel | 9a6cf80 | 2016-06-08 08:22:05 -0700 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | void GrVkPrimaryCommandBuffer::endRenderPass(const GrVkGpu* gpu) { |
| 472 | SkASSERT(fIsActive); |
| 473 | SkASSERT(fActiveRenderPass); |
Greg Daniel | ee54f23 | 2019-04-03 14:58:40 -0400 | [diff] [blame] | 474 | this->addingWork(gpu); |
egdaniel | 9a6cf80 | 2016-06-08 08:22:05 -0700 | [diff] [blame] | 475 | GR_VK_CALL(gpu->vkInterface(), CmdEndRenderPass(fCmdBuffer)); |
| 476 | fActiveRenderPass = nullptr; |
| 477 | } |
| 478 | |
| 479 | void GrVkPrimaryCommandBuffer::executeCommands(const GrVkGpu* gpu, |
Greg Daniel | 8daf3b7 | 2019-07-30 09:57:26 -0400 | [diff] [blame] | 480 | std::unique_ptr<GrVkSecondaryCommandBuffer> buffer) { |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 481 | // The Vulkan spec allows secondary command buffers to be executed on a primary command buffer |
| 482 | // if the command pools both were created from were created with the same queue family. However, |
| 483 | // we currently always create them from the same pool. |
egdaniel | 9a6cf80 | 2016-06-08 08:22:05 -0700 | [diff] [blame] | 484 | SkASSERT(fIsActive); |
Greg Daniel | 77b53f6 | 2016-10-18 11:48:51 -0400 | [diff] [blame] | 485 | SkASSERT(!buffer->fIsActive); |
egdaniel | 9a6cf80 | 2016-06-08 08:22:05 -0700 | [diff] [blame] | 486 | SkASSERT(fActiveRenderPass); |
| 487 | SkASSERT(fActiveRenderPass->isCompatible(*buffer->fActiveRenderPass)); |
| 488 | |
Greg Daniel | ee54f23 | 2019-04-03 14:58:40 -0400 | [diff] [blame] | 489 | this->addingWork(gpu); |
Greg Daniel | f346df3 | 2019-04-03 14:52:13 -0400 | [diff] [blame] | 490 | |
egdaniel | 9a6cf80 | 2016-06-08 08:22:05 -0700 | [diff] [blame] | 491 | GR_VK_CALL(gpu->vkInterface(), CmdExecuteCommands(fCmdBuffer, 1, &buffer->fCmdBuffer)); |
Greg Daniel | 8daf3b7 | 2019-07-30 09:57:26 -0400 | [diff] [blame] | 492 | fSecondaryCommandBuffers.push_back(std::move(buffer)); |
egdaniel | 066df7c | 2016-06-08 14:02:27 -0700 | [diff] [blame] | 493 | // When executing a secondary command buffer all state (besides render pass state) becomes |
| 494 | // invalidated and must be reset. This includes bound buffers, pipelines, dynamic state, etc. |
| 495 | this->invalidateState(); |
egdaniel | 9a6cf80 | 2016-06-08 08:22:05 -0700 | [diff] [blame] | 496 | } |
| 497 | |
Greg Daniel | e118558 | 2019-12-04 11:29:44 -0500 | [diff] [blame] | 498 | static bool submit_to_queue(GrVkGpu* gpu, |
Greg Daniel | 48661b8 | 2018-01-22 16:11:35 -0500 | [diff] [blame] | 499 | VkQueue queue, |
| 500 | VkFence fence, |
| 501 | uint32_t waitCount, |
| 502 | const VkSemaphore* waitSemaphores, |
| 503 | const VkPipelineStageFlags* waitStages, |
| 504 | uint32_t commandBufferCount, |
| 505 | const VkCommandBuffer* commandBuffers, |
| 506 | uint32_t signalCount, |
Emircan Uysaler | 23ca4e7 | 2019-06-24 10:53:09 -0400 | [diff] [blame] | 507 | const VkSemaphore* signalSemaphores, |
| 508 | GrProtected protectedContext) { |
| 509 | VkProtectedSubmitInfo protectedSubmitInfo; |
| 510 | if (protectedContext == GrProtected::kYes) { |
| 511 | memset(&protectedSubmitInfo, 0, sizeof(VkProtectedSubmitInfo)); |
| 512 | protectedSubmitInfo.sType = VK_STRUCTURE_TYPE_PROTECTED_SUBMIT_INFO; |
| 513 | protectedSubmitInfo.pNext = nullptr; |
| 514 | protectedSubmitInfo.protectedSubmit = VK_TRUE; |
| 515 | } |
| 516 | |
Greg Daniel | 48661b8 | 2018-01-22 16:11:35 -0500 | [diff] [blame] | 517 | VkSubmitInfo submitInfo; |
| 518 | memset(&submitInfo, 0, sizeof(VkSubmitInfo)); |
| 519 | submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO; |
Emircan Uysaler | 23ca4e7 | 2019-06-24 10:53:09 -0400 | [diff] [blame] | 520 | submitInfo.pNext = protectedContext == GrProtected::kYes ? &protectedSubmitInfo : nullptr; |
Greg Daniel | 48661b8 | 2018-01-22 16:11:35 -0500 | [diff] [blame] | 521 | submitInfo.waitSemaphoreCount = waitCount; |
| 522 | submitInfo.pWaitSemaphores = waitSemaphores; |
| 523 | submitInfo.pWaitDstStageMask = waitStages; |
| 524 | submitInfo.commandBufferCount = commandBufferCount; |
| 525 | submitInfo.pCommandBuffers = commandBuffers; |
| 526 | submitInfo.signalSemaphoreCount = signalCount; |
| 527 | submitInfo.pSignalSemaphores = signalSemaphores; |
Greg Daniel | e118558 | 2019-12-04 11:29:44 -0500 | [diff] [blame] | 528 | VkResult result; |
| 529 | GR_VK_CALL_RESULT(gpu, result, QueueSubmit(queue, 1, &submitInfo, fence)); |
| 530 | return result == VK_SUCCESS; |
Greg Daniel | 48661b8 | 2018-01-22 16:11:35 -0500 | [diff] [blame] | 531 | } |
| 532 | |
Greg Daniel | e118558 | 2019-12-04 11:29:44 -0500 | [diff] [blame] | 533 | bool GrVkPrimaryCommandBuffer::submitToQueue( |
Greg Daniel | e643da6 | 2019-11-05 12:36:42 -0500 | [diff] [blame] | 534 | GrVkGpu* gpu, |
Greg Daniel | 6be3523 | 2017-03-01 17:01:09 -0500 | [diff] [blame] | 535 | VkQueue queue, |
Greg Daniel | 48661b8 | 2018-01-22 16:11:35 -0500 | [diff] [blame] | 536 | SkTArray<GrVkSemaphore::Resource*>& signalSemaphores, |
| 537 | SkTArray<GrVkSemaphore::Resource*>& waitSemaphores) { |
egdaniel | 9a6cf80 | 2016-06-08 08:22:05 -0700 | [diff] [blame] | 538 | SkASSERT(!fIsActive); |
| 539 | |
| 540 | VkResult err; |
jvanverth | 7ec9241 | 2016-07-06 09:24:57 -0700 | [diff] [blame] | 541 | if (VK_NULL_HANDLE == fSubmitFence) { |
| 542 | VkFenceCreateInfo fenceInfo; |
| 543 | memset(&fenceInfo, 0, sizeof(VkFenceCreateInfo)); |
| 544 | fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO; |
Greg Daniel | e118558 | 2019-12-04 11:29:44 -0500 | [diff] [blame] | 545 | GR_VK_CALL_RESULT(gpu, err, CreateFence(gpu->device(), &fenceInfo, nullptr, |
| 546 | &fSubmitFence)); |
| 547 | if (err) { |
| 548 | fSubmitFence = VK_NULL_HANDLE; |
| 549 | return false; |
| 550 | } |
jvanverth | 7ec9241 | 2016-07-06 09:24:57 -0700 | [diff] [blame] | 551 | } else { |
Greg Daniel | e118558 | 2019-12-04 11:29:44 -0500 | [diff] [blame] | 552 | // This cannot return DEVICE_LOST so we assert we succeeded. |
| 553 | GR_VK_CALL_RESULT(gpu, err, ResetFences(gpu->device(), 1, &fSubmitFence)); |
| 554 | SkASSERT(err == VK_SUCCESS); |
jvanverth | 7ec9241 | 2016-07-06 09:24:57 -0700 | [diff] [blame] | 555 | } |
egdaniel | 9a6cf80 | 2016-06-08 08:22:05 -0700 | [diff] [blame] | 556 | |
Greg Daniel | a5cb781 | 2017-06-16 09:45:32 -0400 | [diff] [blame] | 557 | int signalCount = signalSemaphores.count(); |
Greg Daniel | 6be3523 | 2017-03-01 17:01:09 -0500 | [diff] [blame] | 558 | int waitCount = waitSemaphores.count(); |
Greg Daniel | 6be3523 | 2017-03-01 17:01:09 -0500 | [diff] [blame] | 559 | |
Greg Daniel | e118558 | 2019-12-04 11:29:44 -0500 | [diff] [blame] | 560 | bool submitted = false; |
| 561 | |
Greg Daniel | 48661b8 | 2018-01-22 16:11:35 -0500 | [diff] [blame] | 562 | if (0 == signalCount && 0 == waitCount) { |
| 563 | // This command buffer has no dependent semaphores so we can simply just submit it to the |
| 564 | // queue with no worries. |
Greg Daniel | e118558 | 2019-12-04 11:29:44 -0500 | [diff] [blame] | 565 | submitted = submit_to_queue( |
| 566 | gpu, queue, fSubmitFence, 0, nullptr, nullptr, 1, &fCmdBuffer, 0, nullptr, |
| 567 | gpu->protectedContext() ? GrProtected::kYes : GrProtected::kNo); |
Greg Daniel | 48661b8 | 2018-01-22 16:11:35 -0500 | [diff] [blame] | 568 | } else { |
Greg Daniel | 48661b8 | 2018-01-22 16:11:35 -0500 | [diff] [blame] | 569 | SkTArray<VkSemaphore> vkSignalSems(signalCount); |
| 570 | for (int i = 0; i < signalCount; ++i) { |
| 571 | if (signalSemaphores[i]->shouldSignal()) { |
| 572 | this->addResource(signalSemaphores[i]); |
| 573 | vkSignalSems.push_back(signalSemaphores[i]->semaphore()); |
| 574 | } |
| 575 | } |
| 576 | |
| 577 | SkTArray<VkSemaphore> vkWaitSems(waitCount); |
| 578 | SkTArray<VkPipelineStageFlags> vkWaitStages(waitCount); |
| 579 | for (int i = 0; i < waitCount; ++i) { |
| 580 | if (waitSemaphores[i]->shouldWait()) { |
| 581 | this->addResource(waitSemaphores[i]); |
| 582 | vkWaitSems.push_back(waitSemaphores[i]->semaphore()); |
| 583 | vkWaitStages.push_back(VK_PIPELINE_STAGE_ALL_COMMANDS_BIT); |
| 584 | } |
| 585 | } |
Greg Daniel | e118558 | 2019-12-04 11:29:44 -0500 | [diff] [blame] | 586 | submitted = submit_to_queue(gpu, queue, fSubmitFence, vkWaitSems.count(), |
| 587 | vkWaitSems.begin(), vkWaitStages.begin(), 1, &fCmdBuffer, |
| 588 | vkSignalSems.count(), vkSignalSems.begin(), |
| 589 | gpu->protectedContext() ? GrProtected::kYes : GrProtected::kNo); |
| 590 | if (submitted) { |
| 591 | for (int i = 0; i < signalCount; ++i) { |
| 592 | signalSemaphores[i]->markAsSignaled(); |
| 593 | } |
| 594 | for (int i = 0; i < waitCount; ++i) { |
| 595 | waitSemaphores[i]->markAsWaited(); |
| 596 | } |
Greg Daniel | 48661b8 | 2018-01-22 16:11:35 -0500 | [diff] [blame] | 597 | } |
Greg Daniel | 48661b8 | 2018-01-22 16:11:35 -0500 | [diff] [blame] | 598 | } |
egdaniel | 9a6cf80 | 2016-06-08 08:22:05 -0700 | [diff] [blame] | 599 | |
Greg Daniel | e118558 | 2019-12-04 11:29:44 -0500 | [diff] [blame] | 600 | if (!submitted) { |
| 601 | // Destroy the fence or else we will try to wait forever for it to finish. |
egdaniel | 9a6cf80 | 2016-06-08 08:22:05 -0700 | [diff] [blame] | 602 | GR_VK_CALL(gpu->vkInterface(), DestroyFence(gpu->device(), fSubmitFence, nullptr)); |
| 603 | fSubmitFence = VK_NULL_HANDLE; |
Greg Daniel | e118558 | 2019-12-04 11:29:44 -0500 | [diff] [blame] | 604 | return false; |
egdaniel | 9a6cf80 | 2016-06-08 08:22:05 -0700 | [diff] [blame] | 605 | } |
Greg Daniel | e118558 | 2019-12-04 11:29:44 -0500 | [diff] [blame] | 606 | return true; |
egdaniel | 9a6cf80 | 2016-06-08 08:22:05 -0700 | [diff] [blame] | 607 | } |
| 608 | |
Greg Daniel | e118558 | 2019-12-04 11:29:44 -0500 | [diff] [blame] | 609 | void GrVkPrimaryCommandBuffer::forceSync(GrVkGpu* gpu) { |
| 610 | SkASSERT(fSubmitFence != VK_NULL_HANDLE); |
| 611 | GR_VK_CALL_ERRCHECK(gpu, WaitForFences(gpu->device(), 1, &fSubmitFence, true, UINT64_MAX)); |
| 612 | } |
| 613 | |
| 614 | bool GrVkPrimaryCommandBuffer::finished(GrVkGpu* gpu) { |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 615 | SkASSERT(!fIsActive); |
egdaniel | 9a6cf80 | 2016-06-08 08:22:05 -0700 | [diff] [blame] | 616 | if (VK_NULL_HANDLE == fSubmitFence) { |
| 617 | return true; |
| 618 | } |
| 619 | |
Greg Daniel | e118558 | 2019-12-04 11:29:44 -0500 | [diff] [blame] | 620 | VkResult err; |
| 621 | GR_VK_CALL_RESULT_NOCHECK(gpu, err, GetFenceStatus(gpu->device(), fSubmitFence)); |
egdaniel | 9a6cf80 | 2016-06-08 08:22:05 -0700 | [diff] [blame] | 622 | switch (err) { |
| 623 | case VK_SUCCESS: |
Greg Daniel | e118558 | 2019-12-04 11:29:44 -0500 | [diff] [blame] | 624 | case VK_ERROR_DEVICE_LOST: |
egdaniel | 9a6cf80 | 2016-06-08 08:22:05 -0700 | [diff] [blame] | 625 | return true; |
| 626 | |
| 627 | case VK_NOT_READY: |
| 628 | return false; |
| 629 | |
| 630 | default: |
| 631 | SkDebugf("Error getting fence status: %d\n", err); |
Greg Daniel | e118558 | 2019-12-04 11:29:44 -0500 | [diff] [blame] | 632 | SK_ABORT("Got an invalid fence status"); |
| 633 | return false; |
egdaniel | 9a6cf80 | 2016-06-08 08:22:05 -0700 | [diff] [blame] | 634 | } |
egdaniel | 9a6cf80 | 2016-06-08 08:22:05 -0700 | [diff] [blame] | 635 | } |
| 636 | |
Greg Daniel | a3aa75a | 2019-04-12 14:24:55 -0400 | [diff] [blame] | 637 | void GrVkPrimaryCommandBuffer::addFinishedProc(sk_sp<GrRefCntedCallback> finishedProc) { |
| 638 | fFinishedProcs.push_back(std::move(finishedProc)); |
| 639 | } |
| 640 | |
Jim Van Verth | 5082df1 | 2020-03-11 16:14:51 -0400 | [diff] [blame] | 641 | void GrVkPrimaryCommandBuffer::onReleaseResources() { |
jvanverth | 7ec9241 | 2016-07-06 09:24:57 -0700 | [diff] [blame] | 642 | for (int i = 0; i < fSecondaryCommandBuffers.count(); ++i) { |
Jim Van Verth | 5082df1 | 2020-03-11 16:14:51 -0400 | [diff] [blame] | 643 | fSecondaryCommandBuffers[i]->releaseResources(); |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 644 | } |
Brian Salomon | ab32f65 | 2019-05-10 14:24:50 -0400 | [diff] [blame] | 645 | fFinishedProcs.reset(); |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 646 | } |
| 647 | |
Greg Daniel | 0addbdf | 2019-11-25 15:03:58 -0500 | [diff] [blame] | 648 | void GrVkPrimaryCommandBuffer::recycleSecondaryCommandBuffers(GrVkCommandPool* cmdPool) { |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 649 | for (int i = 0; i < fSecondaryCommandBuffers.count(); ++i) { |
Greg Daniel | 0addbdf | 2019-11-25 15:03:58 -0500 | [diff] [blame] | 650 | fSecondaryCommandBuffers[i].release()->recycle(cmdPool); |
jvanverth | 7ec9241 | 2016-07-06 09:24:57 -0700 | [diff] [blame] | 651 | } |
| 652 | fSecondaryCommandBuffers.reset(); |
| 653 | } |
| 654 | |
egdaniel | 9a6cf80 | 2016-06-08 08:22:05 -0700 | [diff] [blame] | 655 | void GrVkPrimaryCommandBuffer::copyImage(const GrVkGpu* gpu, |
| 656 | GrVkImage* srcImage, |
| 657 | VkImageLayout srcLayout, |
| 658 | GrVkImage* dstImage, |
| 659 | VkImageLayout dstLayout, |
| 660 | uint32_t copyRegionCount, |
| 661 | const VkImageCopy* copyRegions) { |
| 662 | SkASSERT(fIsActive); |
| 663 | SkASSERT(!fActiveRenderPass); |
Greg Daniel | ee54f23 | 2019-04-03 14:58:40 -0400 | [diff] [blame] | 664 | this->addingWork(gpu); |
egdaniel | 9a6cf80 | 2016-06-08 08:22:05 -0700 | [diff] [blame] | 665 | this->addResource(srcImage->resource()); |
| 666 | this->addResource(dstImage->resource()); |
| 667 | GR_VK_CALL(gpu->vkInterface(), CmdCopyImage(fCmdBuffer, |
| 668 | srcImage->image(), |
| 669 | srcLayout, |
| 670 | dstImage->image(), |
| 671 | dstLayout, |
| 672 | copyRegionCount, |
| 673 | copyRegions)); |
| 674 | } |
| 675 | |
| 676 | void GrVkPrimaryCommandBuffer::blitImage(const GrVkGpu* gpu, |
Jim Van Verth | 3e19216 | 2020-03-10 16:23:16 -0400 | [diff] [blame] | 677 | const GrManagedResource* srcResource, |
egdaniel | 9a6cf80 | 2016-06-08 08:22:05 -0700 | [diff] [blame] | 678 | VkImage srcImage, |
| 679 | VkImageLayout srcLayout, |
Jim Van Verth | 3e19216 | 2020-03-10 16:23:16 -0400 | [diff] [blame] | 680 | const GrManagedResource* dstResource, |
egdaniel | 9a6cf80 | 2016-06-08 08:22:05 -0700 | [diff] [blame] | 681 | VkImage dstImage, |
| 682 | VkImageLayout dstLayout, |
| 683 | uint32_t blitRegionCount, |
| 684 | const VkImageBlit* blitRegions, |
| 685 | VkFilter filter) { |
| 686 | SkASSERT(fIsActive); |
| 687 | SkASSERT(!fActiveRenderPass); |
Greg Daniel | ee54f23 | 2019-04-03 14:58:40 -0400 | [diff] [blame] | 688 | this->addingWork(gpu); |
egdaniel | 9a6cf80 | 2016-06-08 08:22:05 -0700 | [diff] [blame] | 689 | this->addResource(srcResource); |
| 690 | this->addResource(dstResource); |
| 691 | GR_VK_CALL(gpu->vkInterface(), CmdBlitImage(fCmdBuffer, |
| 692 | srcImage, |
| 693 | srcLayout, |
| 694 | dstImage, |
| 695 | dstLayout, |
| 696 | blitRegionCount, |
| 697 | blitRegions, |
| 698 | filter)); |
| 699 | } |
| 700 | |
Greg Daniel | 6ecc911 | 2017-06-16 16:17:03 +0000 | [diff] [blame] | 701 | void GrVkPrimaryCommandBuffer::blitImage(const GrVkGpu* gpu, |
| 702 | const GrVkImage& srcImage, |
| 703 | const GrVkImage& dstImage, |
| 704 | uint32_t blitRegionCount, |
| 705 | const VkImageBlit* blitRegions, |
| 706 | VkFilter filter) { |
| 707 | this->blitImage(gpu, |
| 708 | srcImage.resource(), |
| 709 | srcImage.image(), |
| 710 | srcImage.currentLayout(), |
| 711 | dstImage.resource(), |
| 712 | dstImage.image(), |
| 713 | dstImage.currentLayout(), |
| 714 | blitRegionCount, |
| 715 | blitRegions, |
| 716 | filter); |
| 717 | } |
| 718 | |
| 719 | |
egdaniel | 9a6cf80 | 2016-06-08 08:22:05 -0700 | [diff] [blame] | 720 | void GrVkPrimaryCommandBuffer::copyImageToBuffer(const GrVkGpu* gpu, |
| 721 | GrVkImage* srcImage, |
| 722 | VkImageLayout srcLayout, |
| 723 | GrVkTransferBuffer* dstBuffer, |
| 724 | uint32_t copyRegionCount, |
| 725 | const VkBufferImageCopy* copyRegions) { |
| 726 | SkASSERT(fIsActive); |
| 727 | SkASSERT(!fActiveRenderPass); |
Greg Daniel | ee54f23 | 2019-04-03 14:58:40 -0400 | [diff] [blame] | 728 | this->addingWork(gpu); |
egdaniel | 9a6cf80 | 2016-06-08 08:22:05 -0700 | [diff] [blame] | 729 | this->addResource(srcImage->resource()); |
| 730 | this->addResource(dstBuffer->resource()); |
| 731 | GR_VK_CALL(gpu->vkInterface(), CmdCopyImageToBuffer(fCmdBuffer, |
| 732 | srcImage->image(), |
| 733 | srcLayout, |
| 734 | dstBuffer->buffer(), |
| 735 | copyRegionCount, |
| 736 | copyRegions)); |
| 737 | } |
| 738 | |
| 739 | void GrVkPrimaryCommandBuffer::copyBufferToImage(const GrVkGpu* gpu, |
| 740 | GrVkTransferBuffer* srcBuffer, |
| 741 | GrVkImage* dstImage, |
| 742 | VkImageLayout dstLayout, |
| 743 | uint32_t copyRegionCount, |
| 744 | const VkBufferImageCopy* copyRegions) { |
| 745 | SkASSERT(fIsActive); |
| 746 | SkASSERT(!fActiveRenderPass); |
Greg Daniel | ee54f23 | 2019-04-03 14:58:40 -0400 | [diff] [blame] | 747 | this->addingWork(gpu); |
egdaniel | 9a6cf80 | 2016-06-08 08:22:05 -0700 | [diff] [blame] | 748 | this->addResource(srcBuffer->resource()); |
| 749 | this->addResource(dstImage->resource()); |
| 750 | GR_VK_CALL(gpu->vkInterface(), CmdCopyBufferToImage(fCmdBuffer, |
| 751 | srcBuffer->buffer(), |
| 752 | dstImage->image(), |
| 753 | dstLayout, |
| 754 | copyRegionCount, |
| 755 | copyRegions)); |
| 756 | } |
| 757 | |
Greg Daniel | 6888c0d | 2017-08-25 11:55:50 -0400 | [diff] [blame] | 758 | |
| 759 | void GrVkPrimaryCommandBuffer::copyBuffer(GrVkGpu* gpu, |
| 760 | GrVkBuffer* srcBuffer, |
| 761 | GrVkBuffer* dstBuffer, |
| 762 | uint32_t regionCount, |
| 763 | const VkBufferCopy* regions) { |
| 764 | SkASSERT(fIsActive); |
| 765 | SkASSERT(!fActiveRenderPass); |
Greg Daniel | ee54f23 | 2019-04-03 14:58:40 -0400 | [diff] [blame] | 766 | this->addingWork(gpu); |
Greg Daniel | 6888c0d | 2017-08-25 11:55:50 -0400 | [diff] [blame] | 767 | #ifdef SK_DEBUG |
| 768 | for (uint32_t i = 0; i < regionCount; ++i) { |
| 769 | const VkBufferCopy& region = regions[i]; |
| 770 | SkASSERT(region.size > 0); |
| 771 | SkASSERT(region.srcOffset < srcBuffer->size()); |
| 772 | SkASSERT(region.dstOffset < dstBuffer->size()); |
| 773 | SkASSERT(region.srcOffset + region.size <= srcBuffer->size()); |
| 774 | SkASSERT(region.dstOffset + region.size <= dstBuffer->size()); |
| 775 | } |
| 776 | #endif |
| 777 | this->addResource(srcBuffer->resource()); |
| 778 | this->addResource(dstBuffer->resource()); |
| 779 | GR_VK_CALL(gpu->vkInterface(), CmdCopyBuffer(fCmdBuffer, |
| 780 | srcBuffer->buffer(), |
| 781 | dstBuffer->buffer(), |
| 782 | regionCount, |
| 783 | regions)); |
| 784 | } |
| 785 | |
jvanverth | a584de9 | 2016-06-30 09:10:52 -0700 | [diff] [blame] | 786 | void GrVkPrimaryCommandBuffer::updateBuffer(GrVkGpu* gpu, |
| 787 | GrVkBuffer* dstBuffer, |
| 788 | VkDeviceSize dstOffset, |
| 789 | VkDeviceSize dataSize, |
| 790 | const void* data) { |
| 791 | SkASSERT(fIsActive); |
| 792 | SkASSERT(!fActiveRenderPass); |
| 793 | SkASSERT(0 == (dstOffset & 0x03)); // four byte aligned |
| 794 | // TODO: handle larger transfer sizes |
| 795 | SkASSERT(dataSize <= 65536); |
| 796 | SkASSERT(0 == (dataSize & 0x03)); // four byte aligned |
Greg Daniel | ee54f23 | 2019-04-03 14:58:40 -0400 | [diff] [blame] | 797 | this->addingWork(gpu); |
jvanverth | a584de9 | 2016-06-30 09:10:52 -0700 | [diff] [blame] | 798 | this->addResource(dstBuffer->resource()); |
| 799 | GR_VK_CALL(gpu->vkInterface(), CmdUpdateBuffer(fCmdBuffer, |
| 800 | dstBuffer->buffer(), |
| 801 | dstOffset, |
| 802 | dataSize, |
| 803 | (const uint32_t*) data)); |
| 804 | } |
| 805 | |
egdaniel | 9a6cf80 | 2016-06-08 08:22:05 -0700 | [diff] [blame] | 806 | void GrVkPrimaryCommandBuffer::clearColorImage(const GrVkGpu* gpu, |
| 807 | GrVkImage* image, |
| 808 | const VkClearColorValue* color, |
| 809 | uint32_t subRangeCount, |
| 810 | const VkImageSubresourceRange* subRanges) { |
| 811 | SkASSERT(fIsActive); |
| 812 | SkASSERT(!fActiveRenderPass); |
Greg Daniel | ee54f23 | 2019-04-03 14:58:40 -0400 | [diff] [blame] | 813 | this->addingWork(gpu); |
egdaniel | 9a6cf80 | 2016-06-08 08:22:05 -0700 | [diff] [blame] | 814 | this->addResource(image->resource()); |
| 815 | GR_VK_CALL(gpu->vkInterface(), CmdClearColorImage(fCmdBuffer, |
| 816 | image->image(), |
| 817 | image->currentLayout(), |
| 818 | color, |
| 819 | subRangeCount, |
| 820 | subRanges)); |
| 821 | } |
| 822 | |
| 823 | void GrVkPrimaryCommandBuffer::clearDepthStencilImage(const GrVkGpu* gpu, |
| 824 | GrVkImage* image, |
| 825 | const VkClearDepthStencilValue* color, |
| 826 | uint32_t subRangeCount, |
| 827 | const VkImageSubresourceRange* subRanges) { |
| 828 | SkASSERT(fIsActive); |
| 829 | SkASSERT(!fActiveRenderPass); |
Greg Daniel | ee54f23 | 2019-04-03 14:58:40 -0400 | [diff] [blame] | 830 | this->addingWork(gpu); |
egdaniel | 9a6cf80 | 2016-06-08 08:22:05 -0700 | [diff] [blame] | 831 | this->addResource(image->resource()); |
| 832 | GR_VK_CALL(gpu->vkInterface(), CmdClearDepthStencilImage(fCmdBuffer, |
| 833 | image->image(), |
| 834 | image->currentLayout(), |
| 835 | color, |
| 836 | subRangeCount, |
| 837 | subRanges)); |
| 838 | } |
| 839 | |
egdaniel | 52ad251 | 2016-08-04 12:50:01 -0700 | [diff] [blame] | 840 | void GrVkPrimaryCommandBuffer::resolveImage(GrVkGpu* gpu, |
| 841 | const GrVkImage& srcImage, |
| 842 | const GrVkImage& dstImage, |
| 843 | uint32_t regionCount, |
| 844 | const VkImageResolve* regions) { |
| 845 | SkASSERT(fIsActive); |
| 846 | SkASSERT(!fActiveRenderPass); |
| 847 | |
Greg Daniel | ee54f23 | 2019-04-03 14:58:40 -0400 | [diff] [blame] | 848 | this->addingWork(gpu); |
egdaniel | 52ad251 | 2016-08-04 12:50:01 -0700 | [diff] [blame] | 849 | this->addResource(srcImage.resource()); |
| 850 | this->addResource(dstImage.resource()); |
| 851 | |
| 852 | GR_VK_CALL(gpu->vkInterface(), CmdResolveImage(fCmdBuffer, |
| 853 | srcImage.image(), |
| 854 | srcImage.currentLayout(), |
| 855 | dstImage.image(), |
| 856 | dstImage.currentLayout(), |
| 857 | regionCount, |
| 858 | regions)); |
| 859 | } |
| 860 | |
Jim Van Verth | 5082df1 | 2020-03-11 16:14:51 -0400 | [diff] [blame] | 861 | void GrVkPrimaryCommandBuffer::onFreeGPUData(const GrVkGpu* gpu) const { |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 862 | SkASSERT(!fActiveRenderPass); |
| 863 | // Destroy the fence, if any |
| 864 | if (VK_NULL_HANDLE != fSubmitFence) { |
| 865 | GR_VK_CALL(gpu->vkInterface(), DestroyFence(gpu->device(), fSubmitFence, nullptr)); |
| 866 | } |
Greg Daniel | 0addbdf | 2019-11-25 15:03:58 -0500 | [diff] [blame] | 867 | SkASSERT(!fSecondaryCommandBuffers.count()); |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 868 | } |
| 869 | |
egdaniel | 9a6cf80 | 2016-06-08 08:22:05 -0700 | [diff] [blame] | 870 | /////////////////////////////////////////////////////////////////////////////// |
| 871 | // SecondaryCommandBuffer |
| 872 | //////////////////////////////////////////////////////////////////////////////// |
| 873 | |
Greg Daniel | 315c8dc | 2019-11-26 15:41:27 -0500 | [diff] [blame] | 874 | GrVkSecondaryCommandBuffer* GrVkSecondaryCommandBuffer::Create(GrVkGpu* gpu, |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 875 | GrVkCommandPool* cmdPool) { |
Greg Daniel | 070cbaf | 2019-01-03 17:35:54 -0500 | [diff] [blame] | 876 | SkASSERT(cmdPool); |
egdaniel | 9a6cf80 | 2016-06-08 08:22:05 -0700 | [diff] [blame] | 877 | const VkCommandBufferAllocateInfo cmdInfo = { |
| 878 | VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, // sType |
Ben Wagner | a93a14a | 2017-08-28 10:34:05 -0400 | [diff] [blame] | 879 | nullptr, // pNext |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 880 | cmdPool->vkCommandPool(), // commandPool |
egdaniel | 9a6cf80 | 2016-06-08 08:22:05 -0700 | [diff] [blame] | 881 | VK_COMMAND_BUFFER_LEVEL_SECONDARY, // level |
| 882 | 1 // bufferCount |
| 883 | }; |
| 884 | |
| 885 | VkCommandBuffer cmdBuffer; |
Greg Daniel | 315c8dc | 2019-11-26 15:41:27 -0500 | [diff] [blame] | 886 | VkResult err; |
| 887 | GR_VK_CALL_RESULT(gpu, err, AllocateCommandBuffers(gpu->device(), &cmdInfo, &cmdBuffer)); |
egdaniel | 9a6cf80 | 2016-06-08 08:22:05 -0700 | [diff] [blame] | 888 | if (err) { |
| 889 | return nullptr; |
| 890 | } |
Greg Daniel | 0addbdf | 2019-11-25 15:03:58 -0500 | [diff] [blame] | 891 | return new GrVkSecondaryCommandBuffer(cmdBuffer, false); |
egdaniel | 9a6cf80 | 2016-06-08 08:22:05 -0700 | [diff] [blame] | 892 | } |
| 893 | |
Greg Daniel | 070cbaf | 2019-01-03 17:35:54 -0500 | [diff] [blame] | 894 | GrVkSecondaryCommandBuffer* GrVkSecondaryCommandBuffer::Create(VkCommandBuffer cmdBuffer) { |
Greg Daniel | 0addbdf | 2019-11-25 15:03:58 -0500 | [diff] [blame] | 895 | return new GrVkSecondaryCommandBuffer(cmdBuffer, true); |
Greg Daniel | 070cbaf | 2019-01-03 17:35:54 -0500 | [diff] [blame] | 896 | } |
egdaniel | 9a6cf80 | 2016-06-08 08:22:05 -0700 | [diff] [blame] | 897 | |
Greg Daniel | e643da6 | 2019-11-05 12:36:42 -0500 | [diff] [blame] | 898 | void GrVkSecondaryCommandBuffer::begin(GrVkGpu* gpu, const GrVkFramebuffer* framebuffer, |
jvanverth | 7ec9241 | 2016-07-06 09:24:57 -0700 | [diff] [blame] | 899 | const GrVkRenderPass* compatibleRenderPass) { |
egdaniel | 9a6cf80 | 2016-06-08 08:22:05 -0700 | [diff] [blame] | 900 | SkASSERT(!fIsActive); |
jvanverth | 7ec9241 | 2016-07-06 09:24:57 -0700 | [diff] [blame] | 901 | SkASSERT(compatibleRenderPass); |
| 902 | fActiveRenderPass = compatibleRenderPass; |
egdaniel | 9a6cf80 | 2016-06-08 08:22:05 -0700 | [diff] [blame] | 903 | |
Greg Daniel | 070cbaf | 2019-01-03 17:35:54 -0500 | [diff] [blame] | 904 | if (!this->isWrapped()) { |
| 905 | VkCommandBufferInheritanceInfo inheritanceInfo; |
| 906 | memset(&inheritanceInfo, 0, sizeof(VkCommandBufferInheritanceInfo)); |
| 907 | inheritanceInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_INHERITANCE_INFO; |
| 908 | inheritanceInfo.pNext = nullptr; |
| 909 | inheritanceInfo.renderPass = fActiveRenderPass->vkRenderPass(); |
| 910 | inheritanceInfo.subpass = 0; // Currently only using 1 subpass for each render pass |
| 911 | inheritanceInfo.framebuffer = framebuffer ? framebuffer->framebuffer() : VK_NULL_HANDLE; |
| 912 | inheritanceInfo.occlusionQueryEnable = false; |
| 913 | inheritanceInfo.queryFlags = 0; |
| 914 | inheritanceInfo.pipelineStatistics = 0; |
egdaniel | 9a6cf80 | 2016-06-08 08:22:05 -0700 | [diff] [blame] | 915 | |
Greg Daniel | 070cbaf | 2019-01-03 17:35:54 -0500 | [diff] [blame] | 916 | VkCommandBufferBeginInfo cmdBufferBeginInfo; |
| 917 | memset(&cmdBufferBeginInfo, 0, sizeof(VkCommandBufferBeginInfo)); |
| 918 | cmdBufferBeginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO; |
| 919 | cmdBufferBeginInfo.pNext = nullptr; |
| 920 | cmdBufferBeginInfo.flags = VK_COMMAND_BUFFER_USAGE_RENDER_PASS_CONTINUE_BIT | |
| 921 | VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT; |
| 922 | cmdBufferBeginInfo.pInheritanceInfo = &inheritanceInfo; |
egdaniel | 9a6cf80 | 2016-06-08 08:22:05 -0700 | [diff] [blame] | 923 | |
Greg Daniel | e643da6 | 2019-11-05 12:36:42 -0500 | [diff] [blame] | 924 | GR_VK_CALL_ERRCHECK(gpu, BeginCommandBuffer(fCmdBuffer, &cmdBufferBeginInfo)); |
Greg Daniel | 070cbaf | 2019-01-03 17:35:54 -0500 | [diff] [blame] | 925 | } |
egdaniel | 9a6cf80 | 2016-06-08 08:22:05 -0700 | [diff] [blame] | 926 | fIsActive = true; |
| 927 | } |
| 928 | |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 929 | void GrVkSecondaryCommandBuffer::end(GrVkGpu* gpu) { |
egdaniel | 9a6cf80 | 2016-06-08 08:22:05 -0700 | [diff] [blame] | 930 | SkASSERT(fIsActive); |
Greg Daniel | 070cbaf | 2019-01-03 17:35:54 -0500 | [diff] [blame] | 931 | if (!this->isWrapped()) { |
Greg Daniel | e643da6 | 2019-11-05 12:36:42 -0500 | [diff] [blame] | 932 | GR_VK_CALL_ERRCHECK(gpu, EndCommandBuffer(fCmdBuffer)); |
Greg Daniel | 070cbaf | 2019-01-03 17:35:54 -0500 | [diff] [blame] | 933 | } |
egdaniel | 9a6cf80 | 2016-06-08 08:22:05 -0700 | [diff] [blame] | 934 | this->invalidateState(); |
| 935 | fIsActive = false; |
Robert Phillips | 04d2ce2 | 2019-04-03 13:20:43 -0400 | [diff] [blame] | 936 | fHasWork = false; |
egdaniel | 9a6cf80 | 2016-06-08 08:22:05 -0700 | [diff] [blame] | 937 | } |
Greg Daniel | 8daf3b7 | 2019-07-30 09:57:26 -0400 | [diff] [blame] | 938 | |
Greg Daniel | 0addbdf | 2019-11-25 15:03:58 -0500 | [diff] [blame] | 939 | void GrVkSecondaryCommandBuffer::recycle(GrVkCommandPool* cmdPool) { |
Greg Daniel | 8daf3b7 | 2019-07-30 09:57:26 -0400 | [diff] [blame] | 940 | if (this->isWrapped()) { |
Greg Daniel | 8daf3b7 | 2019-07-30 09:57:26 -0400 | [diff] [blame] | 941 | delete this; |
| 942 | } else { |
Greg Daniel | 0addbdf | 2019-11-25 15:03:58 -0500 | [diff] [blame] | 943 | cmdPool->recycleSecondaryCommandBuffer(this); |
Greg Daniel | 8daf3b7 | 2019-07-30 09:57:26 -0400 | [diff] [blame] | 944 | } |
| 945 | } |
| 946 | |