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