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 | af1d193 | 2021-02-08 13:55:26 -0500 | [diff] [blame] | 11 | #include "src/gpu/vk/GrVkBuffer.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); |
Greg Daniel | de4bbdb | 2021-04-13 14:23:23 -0400 | [diff] [blame] | 59 | SkASSERT(!fIsActive || this->isWrapped()); |
jvanverth | 7ec9241 | 2016-07-06 09:24:57 -0700 | [diff] [blame] | 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 | af1d193 | 2021-02-08 13:55:26 -0500 | [diff] [blame] | 184 | VkBuffer vkBuffer = static_cast<const GrVkBuffer*>(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 | af1d193 | 2021-02-08 13:55:26 -0500 | [diff] [blame] | 202 | VkBuffer vkBuffer = static_cast<const GrVkBuffer*>(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 | af1d193 | 2021-02-08 13:55:26 -0500 | [diff] [blame] | 328 | VkBuffer vkBuffer = static_cast<const GrVkBuffer*>(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 | af1d193 | 2021-02-08 13:55:26 -0500 | [diff] [blame] | 346 | VkBuffer vkBuffer = static_cast<const GrVkBuffer*>(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, |
Greg Daniel | 60ec617 | 2021-04-16 11:31:58 -0400 | [diff] [blame] | 461 | sk_sp<const GrVkFramebuffer> framebuffer, |
Robert Phillips | 9521447 | 2017-08-08 18:00:03 -0400 | [diff] [blame] | 462 | const VkClearValue clearValues[], |
Greg Daniel | 1034425 | 2021-04-22 09:52:25 -0400 | [diff] [blame^] | 463 | const GrSurface* target, |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 464 | const SkIRect& bounds, |
| 465 | bool forSecondaryCB) { |
egdaniel | 9a6cf80 | 2016-06-08 08:22:05 -0700 | [diff] [blame] | 466 | SkASSERT(fIsActive); |
| 467 | SkASSERT(!fActiveRenderPass); |
Greg Daniel | fa3adf7 | 2019-11-07 09:53:41 -0500 | [diff] [blame] | 468 | |
Greg Daniel | 60ec617 | 2021-04-16 11:31:58 -0400 | [diff] [blame] | 469 | SkASSERT(framebuffer); |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 470 | |
Greg Daniel | ee54f23 | 2019-04-03 14:58:40 -0400 | [diff] [blame] | 471 | this->addingWork(gpu); |
Greg Daniel | f346df3 | 2019-04-03 14:52:13 -0400 | [diff] [blame] | 472 | |
egdaniel | 9a6cf80 | 2016-06-08 08:22:05 -0700 | [diff] [blame] | 473 | VkRenderPassBeginInfo beginInfo; |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 474 | VkRect2D renderArea; |
| 475 | renderArea.offset = { bounds.fLeft , bounds.fTop }; |
| 476 | renderArea.extent = { (uint32_t)bounds.width(), (uint32_t)bounds.height() }; |
| 477 | |
| 478 | memset(&beginInfo, 0, sizeof(VkRenderPassBeginInfo)); |
| 479 | beginInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO; |
| 480 | beginInfo.pNext = nullptr; |
| 481 | beginInfo.renderPass = renderPass->vkRenderPass(); |
Greg Daniel | fa3adf7 | 2019-11-07 09:53:41 -0500 | [diff] [blame] | 482 | beginInfo.framebuffer = framebuffer->framebuffer(); |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 483 | beginInfo.renderArea = renderArea; |
Greg Daniel | b68319a | 2018-02-23 16:08:28 -0500 | [diff] [blame] | 484 | beginInfo.clearValueCount = renderPass->clearValueCount(); |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 485 | beginInfo.pClearValues = clearValues; |
| 486 | |
| 487 | VkSubpassContents contents = forSecondaryCB ? VK_SUBPASS_CONTENTS_SECONDARY_COMMAND_BUFFERS |
| 488 | : VK_SUBPASS_CONTENTS_INLINE; |
| 489 | |
egdaniel | 9a6cf80 | 2016-06-08 08:22:05 -0700 | [diff] [blame] | 490 | GR_VK_CALL(gpu->vkInterface(), CmdBeginRenderPass(fCmdBuffer, &beginInfo, contents)); |
| 491 | fActiveRenderPass = renderPass; |
| 492 | this->addResource(renderPass); |
Greg Daniel | 60ec617 | 2021-04-16 11:31:58 -0400 | [diff] [blame] | 493 | this->addResource(std::move(framebuffer)); |
Greg Daniel | 38517c2 | 2021-03-29 16:01:19 -0400 | [diff] [blame] | 494 | this->addGrSurface(sk_ref_sp(target)); |
Greg Daniel | fa3adf7 | 2019-11-07 09:53:41 -0500 | [diff] [blame] | 495 | return true; |
egdaniel | 9a6cf80 | 2016-06-08 08:22:05 -0700 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | void GrVkPrimaryCommandBuffer::endRenderPass(const GrVkGpu* gpu) { |
| 499 | SkASSERT(fIsActive); |
| 500 | SkASSERT(fActiveRenderPass); |
Greg Daniel | ee54f23 | 2019-04-03 14:58:40 -0400 | [diff] [blame] | 501 | this->addingWork(gpu); |
egdaniel | 9a6cf80 | 2016-06-08 08:22:05 -0700 | [diff] [blame] | 502 | GR_VK_CALL(gpu->vkInterface(), CmdEndRenderPass(fCmdBuffer)); |
| 503 | fActiveRenderPass = nullptr; |
| 504 | } |
| 505 | |
Greg Daniel | a8c3210 | 2020-12-30 15:09:32 -0500 | [diff] [blame] | 506 | |
| 507 | void GrVkPrimaryCommandBuffer::nexSubpass(GrVkGpu* gpu, bool forSecondaryCB) { |
| 508 | SkASSERT(fIsActive); |
| 509 | SkASSERT(fActiveRenderPass); |
| 510 | VkSubpassContents contents = forSecondaryCB ? VK_SUBPASS_CONTENTS_SECONDARY_COMMAND_BUFFERS |
| 511 | : VK_SUBPASS_CONTENTS_INLINE; |
| 512 | GR_VK_CALL(gpu->vkInterface(), CmdNextSubpass(fCmdBuffer, contents)); |
| 513 | } |
| 514 | |
egdaniel | 9a6cf80 | 2016-06-08 08:22:05 -0700 | [diff] [blame] | 515 | void GrVkPrimaryCommandBuffer::executeCommands(const GrVkGpu* gpu, |
Greg Daniel | 8daf3b7 | 2019-07-30 09:57:26 -0400 | [diff] [blame] | 516 | std::unique_ptr<GrVkSecondaryCommandBuffer> buffer) { |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 517 | // The Vulkan spec allows secondary command buffers to be executed on a primary command buffer |
| 518 | // if the command pools both were created from were created with the same queue family. However, |
| 519 | // we currently always create them from the same pool. |
egdaniel | 9a6cf80 | 2016-06-08 08:22:05 -0700 | [diff] [blame] | 520 | SkASSERT(fIsActive); |
Greg Daniel | 77b53f6 | 2016-10-18 11:48:51 -0400 | [diff] [blame] | 521 | SkASSERT(!buffer->fIsActive); |
egdaniel | 9a6cf80 | 2016-06-08 08:22:05 -0700 | [diff] [blame] | 522 | SkASSERT(fActiveRenderPass); |
| 523 | SkASSERT(fActiveRenderPass->isCompatible(*buffer->fActiveRenderPass)); |
| 524 | |
Greg Daniel | ee54f23 | 2019-04-03 14:58:40 -0400 | [diff] [blame] | 525 | this->addingWork(gpu); |
Greg Daniel | f346df3 | 2019-04-03 14:52:13 -0400 | [diff] [blame] | 526 | |
egdaniel | 9a6cf80 | 2016-06-08 08:22:05 -0700 | [diff] [blame] | 527 | GR_VK_CALL(gpu->vkInterface(), CmdExecuteCommands(fCmdBuffer, 1, &buffer->fCmdBuffer)); |
Greg Daniel | 8daf3b7 | 2019-07-30 09:57:26 -0400 | [diff] [blame] | 528 | fSecondaryCommandBuffers.push_back(std::move(buffer)); |
egdaniel | 066df7c | 2016-06-08 14:02:27 -0700 | [diff] [blame] | 529 | // When executing a secondary command buffer all state (besides render pass state) becomes |
| 530 | // invalidated and must be reset. This includes bound buffers, pipelines, dynamic state, etc. |
| 531 | this->invalidateState(); |
egdaniel | 9a6cf80 | 2016-06-08 08:22:05 -0700 | [diff] [blame] | 532 | } |
| 533 | |
Greg Daniel | e118558 | 2019-12-04 11:29:44 -0500 | [diff] [blame] | 534 | static bool submit_to_queue(GrVkGpu* gpu, |
Greg Daniel | 48661b8 | 2018-01-22 16:11:35 -0500 | [diff] [blame] | 535 | VkQueue queue, |
| 536 | VkFence fence, |
| 537 | uint32_t waitCount, |
| 538 | const VkSemaphore* waitSemaphores, |
| 539 | const VkPipelineStageFlags* waitStages, |
| 540 | uint32_t commandBufferCount, |
| 541 | const VkCommandBuffer* commandBuffers, |
| 542 | uint32_t signalCount, |
Emircan Uysaler | 23ca4e7 | 2019-06-24 10:53:09 -0400 | [diff] [blame] | 543 | const VkSemaphore* signalSemaphores, |
| 544 | GrProtected protectedContext) { |
| 545 | VkProtectedSubmitInfo protectedSubmitInfo; |
| 546 | if (protectedContext == GrProtected::kYes) { |
| 547 | memset(&protectedSubmitInfo, 0, sizeof(VkProtectedSubmitInfo)); |
| 548 | protectedSubmitInfo.sType = VK_STRUCTURE_TYPE_PROTECTED_SUBMIT_INFO; |
| 549 | protectedSubmitInfo.pNext = nullptr; |
| 550 | protectedSubmitInfo.protectedSubmit = VK_TRUE; |
| 551 | } |
| 552 | |
Greg Daniel | 48661b8 | 2018-01-22 16:11:35 -0500 | [diff] [blame] | 553 | VkSubmitInfo submitInfo; |
| 554 | memset(&submitInfo, 0, sizeof(VkSubmitInfo)); |
| 555 | submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO; |
Emircan Uysaler | 23ca4e7 | 2019-06-24 10:53:09 -0400 | [diff] [blame] | 556 | submitInfo.pNext = protectedContext == GrProtected::kYes ? &protectedSubmitInfo : nullptr; |
Greg Daniel | 48661b8 | 2018-01-22 16:11:35 -0500 | [diff] [blame] | 557 | submitInfo.waitSemaphoreCount = waitCount; |
| 558 | submitInfo.pWaitSemaphores = waitSemaphores; |
| 559 | submitInfo.pWaitDstStageMask = waitStages; |
| 560 | submitInfo.commandBufferCount = commandBufferCount; |
| 561 | submitInfo.pCommandBuffers = commandBuffers; |
| 562 | submitInfo.signalSemaphoreCount = signalCount; |
| 563 | submitInfo.pSignalSemaphores = signalSemaphores; |
Greg Daniel | e118558 | 2019-12-04 11:29:44 -0500 | [diff] [blame] | 564 | VkResult result; |
| 565 | GR_VK_CALL_RESULT(gpu, result, QueueSubmit(queue, 1, &submitInfo, fence)); |
| 566 | return result == VK_SUCCESS; |
Greg Daniel | 48661b8 | 2018-01-22 16:11:35 -0500 | [diff] [blame] | 567 | } |
| 568 | |
Greg Daniel | e118558 | 2019-12-04 11:29:44 -0500 | [diff] [blame] | 569 | bool GrVkPrimaryCommandBuffer::submitToQueue( |
Greg Daniel | e643da6 | 2019-11-05 12:36:42 -0500 | [diff] [blame] | 570 | GrVkGpu* gpu, |
Greg Daniel | 6be3523 | 2017-03-01 17:01:09 -0500 | [diff] [blame] | 571 | VkQueue queue, |
Greg Daniel | 48661b8 | 2018-01-22 16:11:35 -0500 | [diff] [blame] | 572 | SkTArray<GrVkSemaphore::Resource*>& signalSemaphores, |
| 573 | SkTArray<GrVkSemaphore::Resource*>& waitSemaphores) { |
egdaniel | 9a6cf80 | 2016-06-08 08:22:05 -0700 | [diff] [blame] | 574 | SkASSERT(!fIsActive); |
| 575 | |
| 576 | VkResult err; |
jvanverth | 7ec9241 | 2016-07-06 09:24:57 -0700 | [diff] [blame] | 577 | if (VK_NULL_HANDLE == fSubmitFence) { |
| 578 | VkFenceCreateInfo fenceInfo; |
| 579 | memset(&fenceInfo, 0, sizeof(VkFenceCreateInfo)); |
| 580 | fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO; |
Greg Daniel | e118558 | 2019-12-04 11:29:44 -0500 | [diff] [blame] | 581 | GR_VK_CALL_RESULT(gpu, err, CreateFence(gpu->device(), &fenceInfo, nullptr, |
| 582 | &fSubmitFence)); |
| 583 | if (err) { |
| 584 | fSubmitFence = VK_NULL_HANDLE; |
| 585 | return false; |
| 586 | } |
jvanverth | 7ec9241 | 2016-07-06 09:24:57 -0700 | [diff] [blame] | 587 | } else { |
Greg Daniel | e118558 | 2019-12-04 11:29:44 -0500 | [diff] [blame] | 588 | // This cannot return DEVICE_LOST so we assert we succeeded. |
| 589 | GR_VK_CALL_RESULT(gpu, err, ResetFences(gpu->device(), 1, &fSubmitFence)); |
| 590 | SkASSERT(err == VK_SUCCESS); |
jvanverth | 7ec9241 | 2016-07-06 09:24:57 -0700 | [diff] [blame] | 591 | } |
egdaniel | 9a6cf80 | 2016-06-08 08:22:05 -0700 | [diff] [blame] | 592 | |
Greg Daniel | a5cb781 | 2017-06-16 09:45:32 -0400 | [diff] [blame] | 593 | int signalCount = signalSemaphores.count(); |
Greg Daniel | 6be3523 | 2017-03-01 17:01:09 -0500 | [diff] [blame] | 594 | int waitCount = waitSemaphores.count(); |
Greg Daniel | 6be3523 | 2017-03-01 17:01:09 -0500 | [diff] [blame] | 595 | |
Greg Daniel | e118558 | 2019-12-04 11:29:44 -0500 | [diff] [blame] | 596 | bool submitted = false; |
| 597 | |
Greg Daniel | 48661b8 | 2018-01-22 16:11:35 -0500 | [diff] [blame] | 598 | if (0 == signalCount && 0 == waitCount) { |
| 599 | // This command buffer has no dependent semaphores so we can simply just submit it to the |
| 600 | // queue with no worries. |
Greg Daniel | e118558 | 2019-12-04 11:29:44 -0500 | [diff] [blame] | 601 | submitted = submit_to_queue( |
| 602 | gpu, queue, fSubmitFence, 0, nullptr, nullptr, 1, &fCmdBuffer, 0, nullptr, |
| 603 | gpu->protectedContext() ? GrProtected::kYes : GrProtected::kNo); |
Greg Daniel | 48661b8 | 2018-01-22 16:11:35 -0500 | [diff] [blame] | 604 | } else { |
Greg Daniel | 48661b8 | 2018-01-22 16:11:35 -0500 | [diff] [blame] | 605 | SkTArray<VkSemaphore> vkSignalSems(signalCount); |
| 606 | for (int i = 0; i < signalCount; ++i) { |
| 607 | if (signalSemaphores[i]->shouldSignal()) { |
| 608 | this->addResource(signalSemaphores[i]); |
| 609 | vkSignalSems.push_back(signalSemaphores[i]->semaphore()); |
| 610 | } |
| 611 | } |
| 612 | |
| 613 | SkTArray<VkSemaphore> vkWaitSems(waitCount); |
| 614 | SkTArray<VkPipelineStageFlags> vkWaitStages(waitCount); |
| 615 | for (int i = 0; i < waitCount; ++i) { |
| 616 | if (waitSemaphores[i]->shouldWait()) { |
| 617 | this->addResource(waitSemaphores[i]); |
| 618 | vkWaitSems.push_back(waitSemaphores[i]->semaphore()); |
| 619 | vkWaitStages.push_back(VK_PIPELINE_STAGE_ALL_COMMANDS_BIT); |
| 620 | } |
| 621 | } |
Greg Daniel | e118558 | 2019-12-04 11:29:44 -0500 | [diff] [blame] | 622 | submitted = submit_to_queue(gpu, queue, fSubmitFence, vkWaitSems.count(), |
| 623 | vkWaitSems.begin(), vkWaitStages.begin(), 1, &fCmdBuffer, |
| 624 | vkSignalSems.count(), vkSignalSems.begin(), |
| 625 | gpu->protectedContext() ? GrProtected::kYes : GrProtected::kNo); |
| 626 | if (submitted) { |
| 627 | for (int i = 0; i < signalCount; ++i) { |
| 628 | signalSemaphores[i]->markAsSignaled(); |
| 629 | } |
| 630 | for (int i = 0; i < waitCount; ++i) { |
| 631 | waitSemaphores[i]->markAsWaited(); |
| 632 | } |
Greg Daniel | 48661b8 | 2018-01-22 16:11:35 -0500 | [diff] [blame] | 633 | } |
Greg Daniel | 48661b8 | 2018-01-22 16:11:35 -0500 | [diff] [blame] | 634 | } |
egdaniel | 9a6cf80 | 2016-06-08 08:22:05 -0700 | [diff] [blame] | 635 | |
Greg Daniel | e118558 | 2019-12-04 11:29:44 -0500 | [diff] [blame] | 636 | if (!submitted) { |
| 637 | // 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] | 638 | GR_VK_CALL(gpu->vkInterface(), DestroyFence(gpu->device(), fSubmitFence, nullptr)); |
| 639 | fSubmitFence = VK_NULL_HANDLE; |
Greg Daniel | e118558 | 2019-12-04 11:29:44 -0500 | [diff] [blame] | 640 | return false; |
egdaniel | 9a6cf80 | 2016-06-08 08:22:05 -0700 | [diff] [blame] | 641 | } |
Greg Daniel | e118558 | 2019-12-04 11:29:44 -0500 | [diff] [blame] | 642 | return true; |
egdaniel | 9a6cf80 | 2016-06-08 08:22:05 -0700 | [diff] [blame] | 643 | } |
| 644 | |
Greg Daniel | e118558 | 2019-12-04 11:29:44 -0500 | [diff] [blame] | 645 | void GrVkPrimaryCommandBuffer::forceSync(GrVkGpu* gpu) { |
Greg Daniel | fda4586 | 2021-02-09 17:55:07 -0500 | [diff] [blame] | 646 | if (fSubmitFence == VK_NULL_HANDLE) { |
| 647 | return; |
| 648 | } |
Greg Daniel | e118558 | 2019-12-04 11:29:44 -0500 | [diff] [blame] | 649 | GR_VK_CALL_ERRCHECK(gpu, WaitForFences(gpu->device(), 1, &fSubmitFence, true, UINT64_MAX)); |
| 650 | } |
| 651 | |
| 652 | bool GrVkPrimaryCommandBuffer::finished(GrVkGpu* gpu) { |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 653 | SkASSERT(!fIsActive); |
egdaniel | 9a6cf80 | 2016-06-08 08:22:05 -0700 | [diff] [blame] | 654 | if (VK_NULL_HANDLE == fSubmitFence) { |
| 655 | return true; |
| 656 | } |
| 657 | |
Greg Daniel | e118558 | 2019-12-04 11:29:44 -0500 | [diff] [blame] | 658 | VkResult err; |
| 659 | GR_VK_CALL_RESULT_NOCHECK(gpu, err, GetFenceStatus(gpu->device(), fSubmitFence)); |
egdaniel | 9a6cf80 | 2016-06-08 08:22:05 -0700 | [diff] [blame] | 660 | switch (err) { |
| 661 | case VK_SUCCESS: |
Greg Daniel | e118558 | 2019-12-04 11:29:44 -0500 | [diff] [blame] | 662 | case VK_ERROR_DEVICE_LOST: |
egdaniel | 9a6cf80 | 2016-06-08 08:22:05 -0700 | [diff] [blame] | 663 | return true; |
| 664 | |
| 665 | case VK_NOT_READY: |
| 666 | return false; |
| 667 | |
| 668 | default: |
| 669 | SkDebugf("Error getting fence status: %d\n", err); |
Greg Daniel | e118558 | 2019-12-04 11:29:44 -0500 | [diff] [blame] | 670 | SK_ABORT("Got an invalid fence status"); |
| 671 | return false; |
egdaniel | 9a6cf80 | 2016-06-08 08:22:05 -0700 | [diff] [blame] | 672 | } |
egdaniel | 9a6cf80 | 2016-06-08 08:22:05 -0700 | [diff] [blame] | 673 | } |
| 674 | |
Greg Daniel | a3aa75a | 2019-04-12 14:24:55 -0400 | [diff] [blame] | 675 | void GrVkPrimaryCommandBuffer::addFinishedProc(sk_sp<GrRefCntedCallback> finishedProc) { |
| 676 | fFinishedProcs.push_back(std::move(finishedProc)); |
| 677 | } |
| 678 | |
Jim Van Verth | 5082df1 | 2020-03-11 16:14:51 -0400 | [diff] [blame] | 679 | void GrVkPrimaryCommandBuffer::onReleaseResources() { |
jvanverth | 7ec9241 | 2016-07-06 09:24:57 -0700 | [diff] [blame] | 680 | for (int i = 0; i < fSecondaryCommandBuffers.count(); ++i) { |
Jim Van Verth | 5082df1 | 2020-03-11 16:14:51 -0400 | [diff] [blame] | 681 | fSecondaryCommandBuffers[i]->releaseResources(); |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 682 | } |
Greg Daniel | fe15962 | 2020-04-10 17:43:51 +0000 | [diff] [blame] | 683 | this->callFinishedProcs(); |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 684 | } |
| 685 | |
Greg Daniel | 0addbdf | 2019-11-25 15:03:58 -0500 | [diff] [blame] | 686 | void GrVkPrimaryCommandBuffer::recycleSecondaryCommandBuffers(GrVkCommandPool* cmdPool) { |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 687 | for (int i = 0; i < fSecondaryCommandBuffers.count(); ++i) { |
Greg Daniel | 0addbdf | 2019-11-25 15:03:58 -0500 | [diff] [blame] | 688 | fSecondaryCommandBuffers[i].release()->recycle(cmdPool); |
jvanverth | 7ec9241 | 2016-07-06 09:24:57 -0700 | [diff] [blame] | 689 | } |
| 690 | fSecondaryCommandBuffers.reset(); |
| 691 | } |
| 692 | |
egdaniel | 9a6cf80 | 2016-06-08 08:22:05 -0700 | [diff] [blame] | 693 | void GrVkPrimaryCommandBuffer::copyImage(const GrVkGpu* gpu, |
| 694 | GrVkImage* srcImage, |
| 695 | VkImageLayout srcLayout, |
| 696 | GrVkImage* dstImage, |
| 697 | VkImageLayout dstLayout, |
| 698 | uint32_t copyRegionCount, |
| 699 | const VkImageCopy* copyRegions) { |
| 700 | SkASSERT(fIsActive); |
| 701 | SkASSERT(!fActiveRenderPass); |
Greg Daniel | ee54f23 | 2019-04-03 14:58:40 -0400 | [diff] [blame] | 702 | this->addingWork(gpu); |
egdaniel | 9a6cf80 | 2016-06-08 08:22:05 -0700 | [diff] [blame] | 703 | this->addResource(srcImage->resource()); |
| 704 | this->addResource(dstImage->resource()); |
| 705 | GR_VK_CALL(gpu->vkInterface(), CmdCopyImage(fCmdBuffer, |
| 706 | srcImage->image(), |
| 707 | srcLayout, |
| 708 | dstImage->image(), |
| 709 | dstLayout, |
| 710 | copyRegionCount, |
| 711 | copyRegions)); |
| 712 | } |
| 713 | |
| 714 | void GrVkPrimaryCommandBuffer::blitImage(const GrVkGpu* gpu, |
Jim Van Verth | 3e19216 | 2020-03-10 16:23:16 -0400 | [diff] [blame] | 715 | const GrManagedResource* srcResource, |
egdaniel | 9a6cf80 | 2016-06-08 08:22:05 -0700 | [diff] [blame] | 716 | VkImage srcImage, |
| 717 | VkImageLayout srcLayout, |
Jim Van Verth | 3e19216 | 2020-03-10 16:23:16 -0400 | [diff] [blame] | 718 | const GrManagedResource* dstResource, |
egdaniel | 9a6cf80 | 2016-06-08 08:22:05 -0700 | [diff] [blame] | 719 | VkImage dstImage, |
| 720 | VkImageLayout dstLayout, |
| 721 | uint32_t blitRegionCount, |
| 722 | const VkImageBlit* blitRegions, |
| 723 | VkFilter filter) { |
| 724 | SkASSERT(fIsActive); |
| 725 | SkASSERT(!fActiveRenderPass); |
Greg Daniel | ee54f23 | 2019-04-03 14:58:40 -0400 | [diff] [blame] | 726 | this->addingWork(gpu); |
egdaniel | 9a6cf80 | 2016-06-08 08:22:05 -0700 | [diff] [blame] | 727 | this->addResource(srcResource); |
| 728 | this->addResource(dstResource); |
| 729 | GR_VK_CALL(gpu->vkInterface(), CmdBlitImage(fCmdBuffer, |
| 730 | srcImage, |
| 731 | srcLayout, |
| 732 | dstImage, |
| 733 | dstLayout, |
| 734 | blitRegionCount, |
| 735 | blitRegions, |
| 736 | filter)); |
| 737 | } |
| 738 | |
Greg Daniel | 6ecc911 | 2017-06-16 16:17:03 +0000 | [diff] [blame] | 739 | void GrVkPrimaryCommandBuffer::blitImage(const GrVkGpu* gpu, |
| 740 | const GrVkImage& srcImage, |
| 741 | const GrVkImage& dstImage, |
| 742 | uint32_t blitRegionCount, |
| 743 | const VkImageBlit* blitRegions, |
| 744 | VkFilter filter) { |
| 745 | this->blitImage(gpu, |
| 746 | srcImage.resource(), |
| 747 | srcImage.image(), |
| 748 | srcImage.currentLayout(), |
| 749 | dstImage.resource(), |
| 750 | dstImage.image(), |
| 751 | dstImage.currentLayout(), |
| 752 | blitRegionCount, |
| 753 | blitRegions, |
| 754 | filter); |
| 755 | } |
| 756 | |
| 757 | |
egdaniel | 9a6cf80 | 2016-06-08 08:22:05 -0700 | [diff] [blame] | 758 | void GrVkPrimaryCommandBuffer::copyImageToBuffer(const GrVkGpu* gpu, |
| 759 | GrVkImage* srcImage, |
| 760 | VkImageLayout srcLayout, |
Greg Daniel | 2e967df | 2021-02-08 10:38:31 -0500 | [diff] [blame] | 761 | sk_sp<GrGpuBuffer> dstBuffer, |
egdaniel | 9a6cf80 | 2016-06-08 08:22:05 -0700 | [diff] [blame] | 762 | uint32_t copyRegionCount, |
| 763 | const VkBufferImageCopy* copyRegions) { |
| 764 | SkASSERT(fIsActive); |
| 765 | SkASSERT(!fActiveRenderPass); |
Greg Daniel | ee54f23 | 2019-04-03 14:58:40 -0400 | [diff] [blame] | 766 | this->addingWork(gpu); |
Greg Daniel | af1d193 | 2021-02-08 13:55:26 -0500 | [diff] [blame] | 767 | GrVkBuffer* vkBuffer = static_cast<GrVkBuffer*>(dstBuffer.get()); |
egdaniel | 9a6cf80 | 2016-06-08 08:22:05 -0700 | [diff] [blame] | 768 | GR_VK_CALL(gpu->vkInterface(), CmdCopyImageToBuffer(fCmdBuffer, |
| 769 | srcImage->image(), |
| 770 | srcLayout, |
Greg Daniel | 2e967df | 2021-02-08 10:38:31 -0500 | [diff] [blame] | 771 | vkBuffer->vkBuffer(), |
egdaniel | 9a6cf80 | 2016-06-08 08:22:05 -0700 | [diff] [blame] | 772 | copyRegionCount, |
| 773 | copyRegions)); |
Greg Daniel | 2e967df | 2021-02-08 10:38:31 -0500 | [diff] [blame] | 774 | this->addResource(srcImage->resource()); |
| 775 | this->addGrBuffer(std::move(dstBuffer)); |
egdaniel | 9a6cf80 | 2016-06-08 08:22:05 -0700 | [diff] [blame] | 776 | } |
| 777 | |
| 778 | void GrVkPrimaryCommandBuffer::copyBufferToImage(const GrVkGpu* gpu, |
Greg Daniel | 2e967df | 2021-02-08 10:38:31 -0500 | [diff] [blame] | 779 | VkBuffer srcBuffer, |
egdaniel | 9a6cf80 | 2016-06-08 08:22:05 -0700 | [diff] [blame] | 780 | GrVkImage* dstImage, |
| 781 | VkImageLayout dstLayout, |
| 782 | uint32_t copyRegionCount, |
| 783 | const VkBufferImageCopy* copyRegions) { |
| 784 | SkASSERT(fIsActive); |
| 785 | SkASSERT(!fActiveRenderPass); |
Greg Daniel | ee54f23 | 2019-04-03 14:58:40 -0400 | [diff] [blame] | 786 | this->addingWork(gpu); |
Greg Daniel | 2e967df | 2021-02-08 10:38:31 -0500 | [diff] [blame] | 787 | |
egdaniel | 9a6cf80 | 2016-06-08 08:22:05 -0700 | [diff] [blame] | 788 | GR_VK_CALL(gpu->vkInterface(), CmdCopyBufferToImage(fCmdBuffer, |
Greg Daniel | 2e967df | 2021-02-08 10:38:31 -0500 | [diff] [blame] | 789 | srcBuffer, |
egdaniel | 9a6cf80 | 2016-06-08 08:22:05 -0700 | [diff] [blame] | 790 | dstImage->image(), |
| 791 | dstLayout, |
| 792 | copyRegionCount, |
| 793 | copyRegions)); |
Greg Daniel | 2e967df | 2021-02-08 10:38:31 -0500 | [diff] [blame] | 794 | this->addResource(dstImage->resource()); |
egdaniel | 9a6cf80 | 2016-06-08 08:22:05 -0700 | [diff] [blame] | 795 | } |
| 796 | |
Greg Daniel | 87d784f | 2021-02-02 15:36:06 -0500 | [diff] [blame] | 797 | void GrVkPrimaryCommandBuffer::copyBuffer(GrVkGpu* gpu, |
Greg Daniel | 2e967df | 2021-02-08 10:38:31 -0500 | [diff] [blame] | 798 | sk_sp<GrGpuBuffer> srcBuffer, |
| 799 | sk_sp<GrGpuBuffer> dstBuffer, |
Greg Daniel | 87d784f | 2021-02-02 15:36:06 -0500 | [diff] [blame] | 800 | uint32_t regionCount, |
| 801 | const VkBufferCopy* regions) { |
| 802 | SkASSERT(fIsActive); |
| 803 | SkASSERT(!fActiveRenderPass); |
| 804 | this->addingWork(gpu); |
| 805 | #ifdef SK_DEBUG |
| 806 | for (uint32_t i = 0; i < regionCount; ++i) { |
| 807 | const VkBufferCopy& region = regions[i]; |
| 808 | SkASSERT(region.size > 0); |
| 809 | SkASSERT(region.srcOffset < srcBuffer->size()); |
| 810 | SkASSERT(region.dstOffset < dstBuffer->size()); |
| 811 | SkASSERT(region.srcOffset + region.size <= srcBuffer->size()); |
| 812 | SkASSERT(region.dstOffset + region.size <= dstBuffer->size()); |
| 813 | } |
| 814 | #endif |
| 815 | |
Greg Daniel | af1d193 | 2021-02-08 13:55:26 -0500 | [diff] [blame] | 816 | const GrVkBuffer* srcVk = static_cast<GrVkBuffer*>(srcBuffer.get()); |
| 817 | const GrVkBuffer* dstVk = static_cast<GrVkBuffer*>(dstBuffer.get()); |
Greg Daniel | 2e967df | 2021-02-08 10:38:31 -0500 | [diff] [blame] | 818 | |
| 819 | GR_VK_CALL(gpu->vkInterface(), CmdCopyBuffer(fCmdBuffer, |
| 820 | srcVk->vkBuffer(), |
| 821 | dstVk->vkBuffer(), |
| 822 | regionCount, |
| 823 | regions)); |
| 824 | this->addGrBuffer(std::move(srcBuffer)); |
Greg Daniel | 87d784f | 2021-02-02 15:36:06 -0500 | [diff] [blame] | 825 | this->addGrBuffer(std::move(dstBuffer)); |
| 826 | } |
| 827 | |
jvanverth | a584de9 | 2016-06-30 09:10:52 -0700 | [diff] [blame] | 828 | void GrVkPrimaryCommandBuffer::updateBuffer(GrVkGpu* gpu, |
Greg Daniel | af1d193 | 2021-02-08 13:55:26 -0500 | [diff] [blame] | 829 | sk_sp<GrVkBuffer> dstBuffer, |
Greg Daniel | 87d784f | 2021-02-02 15:36:06 -0500 | [diff] [blame] | 830 | VkDeviceSize dstOffset, |
| 831 | VkDeviceSize dataSize, |
| 832 | const void* data) { |
| 833 | SkASSERT(fIsActive); |
| 834 | SkASSERT(!fActiveRenderPass); |
| 835 | SkASSERT(0 == (dstOffset & 0x03)); // four byte aligned |
| 836 | // TODO: handle larger transfer sizes |
| 837 | SkASSERT(dataSize <= 65536); |
| 838 | SkASSERT(0 == (dataSize & 0x03)); // four byte aligned |
| 839 | this->addingWork(gpu); |
| 840 | GR_VK_CALL( |
| 841 | gpu->vkInterface(), |
| 842 | CmdUpdateBuffer( |
| 843 | fCmdBuffer, dstBuffer->vkBuffer(), dstOffset, dataSize, (const uint32_t*)data)); |
| 844 | this->addGrBuffer(std::move(dstBuffer)); |
| 845 | } |
| 846 | |
egdaniel | 9a6cf80 | 2016-06-08 08:22:05 -0700 | [diff] [blame] | 847 | void GrVkPrimaryCommandBuffer::clearColorImage(const GrVkGpu* gpu, |
| 848 | GrVkImage* image, |
| 849 | const VkClearColorValue* color, |
| 850 | uint32_t subRangeCount, |
| 851 | const VkImageSubresourceRange* subRanges) { |
| 852 | SkASSERT(fIsActive); |
| 853 | SkASSERT(!fActiveRenderPass); |
Greg Daniel | ee54f23 | 2019-04-03 14:58:40 -0400 | [diff] [blame] | 854 | this->addingWork(gpu); |
egdaniel | 9a6cf80 | 2016-06-08 08:22:05 -0700 | [diff] [blame] | 855 | this->addResource(image->resource()); |
| 856 | GR_VK_CALL(gpu->vkInterface(), CmdClearColorImage(fCmdBuffer, |
| 857 | image->image(), |
| 858 | image->currentLayout(), |
| 859 | color, |
| 860 | subRangeCount, |
| 861 | subRanges)); |
| 862 | } |
| 863 | |
| 864 | void GrVkPrimaryCommandBuffer::clearDepthStencilImage(const GrVkGpu* gpu, |
| 865 | GrVkImage* image, |
| 866 | const VkClearDepthStencilValue* color, |
| 867 | uint32_t subRangeCount, |
| 868 | const VkImageSubresourceRange* subRanges) { |
| 869 | SkASSERT(fIsActive); |
| 870 | SkASSERT(!fActiveRenderPass); |
Greg Daniel | ee54f23 | 2019-04-03 14:58:40 -0400 | [diff] [blame] | 871 | this->addingWork(gpu); |
egdaniel | 9a6cf80 | 2016-06-08 08:22:05 -0700 | [diff] [blame] | 872 | this->addResource(image->resource()); |
| 873 | GR_VK_CALL(gpu->vkInterface(), CmdClearDepthStencilImage(fCmdBuffer, |
| 874 | image->image(), |
| 875 | image->currentLayout(), |
| 876 | color, |
| 877 | subRangeCount, |
| 878 | subRanges)); |
| 879 | } |
| 880 | |
egdaniel | 52ad251 | 2016-08-04 12:50:01 -0700 | [diff] [blame] | 881 | void GrVkPrimaryCommandBuffer::resolveImage(GrVkGpu* gpu, |
| 882 | const GrVkImage& srcImage, |
| 883 | const GrVkImage& dstImage, |
| 884 | uint32_t regionCount, |
| 885 | const VkImageResolve* regions) { |
| 886 | SkASSERT(fIsActive); |
| 887 | SkASSERT(!fActiveRenderPass); |
| 888 | |
Greg Daniel | ee54f23 | 2019-04-03 14:58:40 -0400 | [diff] [blame] | 889 | this->addingWork(gpu); |
egdaniel | 52ad251 | 2016-08-04 12:50:01 -0700 | [diff] [blame] | 890 | this->addResource(srcImage.resource()); |
| 891 | this->addResource(dstImage.resource()); |
| 892 | |
| 893 | GR_VK_CALL(gpu->vkInterface(), CmdResolveImage(fCmdBuffer, |
| 894 | srcImage.image(), |
| 895 | srcImage.currentLayout(), |
| 896 | dstImage.image(), |
| 897 | dstImage.currentLayout(), |
| 898 | regionCount, |
| 899 | regions)); |
| 900 | } |
| 901 | |
Jim Van Verth | 5082df1 | 2020-03-11 16:14:51 -0400 | [diff] [blame] | 902 | void GrVkPrimaryCommandBuffer::onFreeGPUData(const GrVkGpu* gpu) const { |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 903 | SkASSERT(!fActiveRenderPass); |
| 904 | // Destroy the fence, if any |
| 905 | if (VK_NULL_HANDLE != fSubmitFence) { |
| 906 | GR_VK_CALL(gpu->vkInterface(), DestroyFence(gpu->device(), fSubmitFence, nullptr)); |
| 907 | } |
Greg Daniel | 0addbdf | 2019-11-25 15:03:58 -0500 | [diff] [blame] | 908 | SkASSERT(!fSecondaryCommandBuffers.count()); |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 909 | } |
| 910 | |
egdaniel | 9a6cf80 | 2016-06-08 08:22:05 -0700 | [diff] [blame] | 911 | /////////////////////////////////////////////////////////////////////////////// |
| 912 | // SecondaryCommandBuffer |
| 913 | //////////////////////////////////////////////////////////////////////////////// |
| 914 | |
Greg Daniel | 315c8dc | 2019-11-26 15:41:27 -0500 | [diff] [blame] | 915 | GrVkSecondaryCommandBuffer* GrVkSecondaryCommandBuffer::Create(GrVkGpu* gpu, |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 916 | GrVkCommandPool* cmdPool) { |
Greg Daniel | 070cbaf | 2019-01-03 17:35:54 -0500 | [diff] [blame] | 917 | SkASSERT(cmdPool); |
egdaniel | 9a6cf80 | 2016-06-08 08:22:05 -0700 | [diff] [blame] | 918 | const VkCommandBufferAllocateInfo cmdInfo = { |
| 919 | VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, // sType |
Ben Wagner | a93a14a | 2017-08-28 10:34:05 -0400 | [diff] [blame] | 920 | nullptr, // pNext |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 921 | cmdPool->vkCommandPool(), // commandPool |
egdaniel | 9a6cf80 | 2016-06-08 08:22:05 -0700 | [diff] [blame] | 922 | VK_COMMAND_BUFFER_LEVEL_SECONDARY, // level |
| 923 | 1 // bufferCount |
| 924 | }; |
| 925 | |
| 926 | VkCommandBuffer cmdBuffer; |
Greg Daniel | 315c8dc | 2019-11-26 15:41:27 -0500 | [diff] [blame] | 927 | VkResult err; |
| 928 | GR_VK_CALL_RESULT(gpu, err, AllocateCommandBuffers(gpu->device(), &cmdInfo, &cmdBuffer)); |
egdaniel | 9a6cf80 | 2016-06-08 08:22:05 -0700 | [diff] [blame] | 929 | if (err) { |
| 930 | return nullptr; |
| 931 | } |
Greg Daniel | de4bbdb | 2021-04-13 14:23:23 -0400 | [diff] [blame] | 932 | return new GrVkSecondaryCommandBuffer(cmdBuffer, /*externalRenderPass=*/nullptr); |
egdaniel | 9a6cf80 | 2016-06-08 08:22:05 -0700 | [diff] [blame] | 933 | } |
| 934 | |
Greg Daniel | de4bbdb | 2021-04-13 14:23:23 -0400 | [diff] [blame] | 935 | GrVkSecondaryCommandBuffer* GrVkSecondaryCommandBuffer::Create( |
| 936 | VkCommandBuffer cmdBuffer, const GrVkRenderPass* externalRenderPass) { |
| 937 | return new GrVkSecondaryCommandBuffer(cmdBuffer, externalRenderPass); |
Greg Daniel | 070cbaf | 2019-01-03 17:35:54 -0500 | [diff] [blame] | 938 | } |
egdaniel | 9a6cf80 | 2016-06-08 08:22:05 -0700 | [diff] [blame] | 939 | |
Greg Daniel | e643da6 | 2019-11-05 12:36:42 -0500 | [diff] [blame] | 940 | void GrVkSecondaryCommandBuffer::begin(GrVkGpu* gpu, const GrVkFramebuffer* framebuffer, |
jvanverth | 7ec9241 | 2016-07-06 09:24:57 -0700 | [diff] [blame] | 941 | const GrVkRenderPass* compatibleRenderPass) { |
egdaniel | 9a6cf80 | 2016-06-08 08:22:05 -0700 | [diff] [blame] | 942 | SkASSERT(!fIsActive); |
Greg Daniel | de4bbdb | 2021-04-13 14:23:23 -0400 | [diff] [blame] | 943 | SkASSERT(!this->isWrapped()); |
jvanverth | 7ec9241 | 2016-07-06 09:24:57 -0700 | [diff] [blame] | 944 | SkASSERT(compatibleRenderPass); |
| 945 | fActiveRenderPass = compatibleRenderPass; |
egdaniel | 9a6cf80 | 2016-06-08 08:22:05 -0700 | [diff] [blame] | 946 | |
Greg Daniel | de4bbdb | 2021-04-13 14:23:23 -0400 | [diff] [blame] | 947 | VkCommandBufferInheritanceInfo inheritanceInfo; |
| 948 | memset(&inheritanceInfo, 0, sizeof(VkCommandBufferInheritanceInfo)); |
| 949 | inheritanceInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_INHERITANCE_INFO; |
| 950 | inheritanceInfo.pNext = nullptr; |
| 951 | inheritanceInfo.renderPass = fActiveRenderPass->vkRenderPass(); |
| 952 | inheritanceInfo.subpass = 0; // Currently only using 1 subpass for each render pass |
| 953 | inheritanceInfo.framebuffer = framebuffer ? framebuffer->framebuffer() : VK_NULL_HANDLE; |
| 954 | inheritanceInfo.occlusionQueryEnable = false; |
| 955 | inheritanceInfo.queryFlags = 0; |
| 956 | inheritanceInfo.pipelineStatistics = 0; |
egdaniel | 9a6cf80 | 2016-06-08 08:22:05 -0700 | [diff] [blame] | 957 | |
Greg Daniel | de4bbdb | 2021-04-13 14:23:23 -0400 | [diff] [blame] | 958 | VkCommandBufferBeginInfo cmdBufferBeginInfo; |
| 959 | memset(&cmdBufferBeginInfo, 0, sizeof(VkCommandBufferBeginInfo)); |
| 960 | cmdBufferBeginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO; |
| 961 | cmdBufferBeginInfo.pNext = nullptr; |
| 962 | cmdBufferBeginInfo.flags = VK_COMMAND_BUFFER_USAGE_RENDER_PASS_CONTINUE_BIT | |
| 963 | VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT; |
| 964 | cmdBufferBeginInfo.pInheritanceInfo = &inheritanceInfo; |
egdaniel | 9a6cf80 | 2016-06-08 08:22:05 -0700 | [diff] [blame] | 965 | |
Greg Daniel | de4bbdb | 2021-04-13 14:23:23 -0400 | [diff] [blame] | 966 | GR_VK_CALL_ERRCHECK(gpu, BeginCommandBuffer(fCmdBuffer, &cmdBufferBeginInfo)); |
| 967 | |
egdaniel | 9a6cf80 | 2016-06-08 08:22:05 -0700 | [diff] [blame] | 968 | fIsActive = true; |
| 969 | } |
| 970 | |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 971 | void GrVkSecondaryCommandBuffer::end(GrVkGpu* gpu) { |
egdaniel | 9a6cf80 | 2016-06-08 08:22:05 -0700 | [diff] [blame] | 972 | SkASSERT(fIsActive); |
Greg Daniel | de4bbdb | 2021-04-13 14:23:23 -0400 | [diff] [blame] | 973 | SkASSERT(!this->isWrapped()); |
| 974 | GR_VK_CALL_ERRCHECK(gpu, EndCommandBuffer(fCmdBuffer)); |
egdaniel | 9a6cf80 | 2016-06-08 08:22:05 -0700 | [diff] [blame] | 975 | this->invalidateState(); |
Robert Phillips | 04d2ce2 | 2019-04-03 13:20:43 -0400 | [diff] [blame] | 976 | fHasWork = false; |
Greg Daniel | de4bbdb | 2021-04-13 14:23:23 -0400 | [diff] [blame] | 977 | fIsActive = false; |
egdaniel | 9a6cf80 | 2016-06-08 08:22:05 -0700 | [diff] [blame] | 978 | } |
Greg Daniel | 8daf3b7 | 2019-07-30 09:57:26 -0400 | [diff] [blame] | 979 | |
Greg Daniel | 0addbdf | 2019-11-25 15:03:58 -0500 | [diff] [blame] | 980 | void GrVkSecondaryCommandBuffer::recycle(GrVkCommandPool* cmdPool) { |
Greg Daniel | 8daf3b7 | 2019-07-30 09:57:26 -0400 | [diff] [blame] | 981 | if (this->isWrapped()) { |
Greg Daniel | 8daf3b7 | 2019-07-30 09:57:26 -0400 | [diff] [blame] | 982 | delete this; |
| 983 | } else { |
Greg Daniel | 0addbdf | 2019-11-25 15:03:58 -0500 | [diff] [blame] | 984 | cmdPool->recycleSecondaryCommandBuffer(this); |
Greg Daniel | 8daf3b7 | 2019-07-30 09:57:26 -0400 | [diff] [blame] | 985 | } |
| 986 | } |
| 987 | |