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