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