Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "src/gpu/vk/GrVkCommandPool.h" |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 9 | |
Robert Phillips | 06273bc | 2021-08-11 15:43:50 -0400 | [diff] [blame] | 10 | #include "src/core/SkTraceEvent.h" |
Adlai Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame] | 11 | #include "src/gpu/GrDirectContextPriv.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 12 | #include "src/gpu/vk/GrVkCommandBuffer.h" |
| 13 | #include "src/gpu/vk/GrVkGpu.h" |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 14 | |
Greg Daniel | e643da6 | 2019-11-05 12:36:42 -0500 | [diff] [blame] | 15 | GrVkCommandPool* GrVkCommandPool::Create(GrVkGpu* gpu) { |
Greg Daniel | 6253b57 | 2020-09-11 16:39:28 -0400 | [diff] [blame] | 16 | VkCommandPoolCreateFlags cmdPoolCreateFlags = VK_COMMAND_POOL_CREATE_TRANSIENT_BIT; |
Greg Daniel | 8daf3b7 | 2019-07-30 09:57:26 -0400 | [diff] [blame] | 17 | if (gpu->protectedContext()) { |
| 18 | cmdPoolCreateFlags |= VK_COMMAND_POOL_CREATE_PROTECTED_BIT; |
| 19 | } |
Emircan Uysaler | 23ca4e7 | 2019-06-24 10:53:09 -0400 | [diff] [blame] | 20 | |
Greg Daniel | 8daf3b7 | 2019-07-30 09:57:26 -0400 | [diff] [blame] | 21 | const VkCommandPoolCreateInfo cmdPoolInfo = { |
| 22 | VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO, // sType |
| 23 | nullptr, // pNext |
| 24 | cmdPoolCreateFlags, // CmdPoolCreateFlags |
| 25 | gpu->queueIndex(), // queueFamilyIndex |
| 26 | }; |
Greg Daniel | 9b63dc8 | 2019-11-06 09:21:55 -0500 | [diff] [blame] | 27 | VkResult result; |
Greg Daniel | 8daf3b7 | 2019-07-30 09:57:26 -0400 | [diff] [blame] | 28 | VkCommandPool pool; |
Greg Daniel | 9b63dc8 | 2019-11-06 09:21:55 -0500 | [diff] [blame] | 29 | GR_VK_CALL_RESULT(gpu, result, CreateCommandPool(gpu->device(), &cmdPoolInfo, nullptr, &pool)); |
| 30 | if (result != VK_SUCCESS) { |
| 31 | return nullptr; |
| 32 | } |
Greg Daniel | 315c8dc | 2019-11-26 15:41:27 -0500 | [diff] [blame] | 33 | |
| 34 | GrVkPrimaryCommandBuffer* primaryCmdBuffer = GrVkPrimaryCommandBuffer::Create(gpu, pool); |
| 35 | if (!primaryCmdBuffer) { |
| 36 | GR_VK_CALL(gpu->vkInterface(), DestroyCommandPool(gpu->device(), pool, nullptr)); |
| 37 | return nullptr; |
| 38 | } |
| 39 | |
| 40 | return new GrVkCommandPool(gpu, pool, primaryCmdBuffer); |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 41 | } |
| 42 | |
Greg Daniel | 315c8dc | 2019-11-26 15:41:27 -0500 | [diff] [blame] | 43 | GrVkCommandPool::GrVkCommandPool(GrVkGpu* gpu, VkCommandPool commandPool, |
| 44 | GrVkPrimaryCommandBuffer* primaryCmdBuffer) |
Jim Van Verth | 5082df1 | 2020-03-11 16:14:51 -0400 | [diff] [blame] | 45 | : GrVkManagedResource(gpu) |
| 46 | , fCommandPool(commandPool) |
Greg Daniel | 43833b0 | 2020-07-14 09:21:48 -0400 | [diff] [blame] | 47 | , fPrimaryCommandBuffer(primaryCmdBuffer) |
| 48 | , fMaxCachedSecondaryCommandBuffers( |
| 49 | gpu->vkCaps().maxPerPoolCachedSecondaryCommandBuffers()) { |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 50 | } |
| 51 | |
Greg Daniel | 8daf3b7 | 2019-07-30 09:57:26 -0400 | [diff] [blame] | 52 | std::unique_ptr<GrVkSecondaryCommandBuffer> GrVkCommandPool::findOrCreateSecondaryCommandBuffer( |
| 53 | GrVkGpu* gpu) { |
| 54 | std::unique_ptr<GrVkSecondaryCommandBuffer> result; |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 55 | if (fAvailableSecondaryBuffers.count()) { |
Greg Daniel | 8daf3b7 | 2019-07-30 09:57:26 -0400 | [diff] [blame] | 56 | result = std::move(fAvailableSecondaryBuffers.back()); |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 57 | fAvailableSecondaryBuffers.pop_back(); |
Greg Daniel | 8daf3b7 | 2019-07-30 09:57:26 -0400 | [diff] [blame] | 58 | } else{ |
| 59 | result.reset(GrVkSecondaryCommandBuffer::Create(gpu, this)); |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 60 | } |
Greg Daniel | 8daf3b7 | 2019-07-30 09:57:26 -0400 | [diff] [blame] | 61 | return result; |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | void GrVkCommandPool::recycleSecondaryCommandBuffer(GrVkSecondaryCommandBuffer* buffer) { |
Greg Daniel | 8daf3b7 | 2019-07-30 09:57:26 -0400 | [diff] [blame] | 65 | std::unique_ptr<GrVkSecondaryCommandBuffer> scb(buffer); |
Greg Daniel | 43833b0 | 2020-07-14 09:21:48 -0400 | [diff] [blame] | 66 | if (fAvailableSecondaryBuffers.count() < fMaxCachedSecondaryCommandBuffers) { |
| 67 | fAvailableSecondaryBuffers.push_back(std::move(scb)); |
Greg Daniel | 0927dd6 | 2020-07-20 15:13:29 -0400 | [diff] [blame] | 68 | } else { |
| 69 | VkCommandBuffer vkBuffer = buffer->vkCommandBuffer(); |
| 70 | GR_VK_CALL(fGpu->vkInterface(), |
| 71 | FreeCommandBuffers(fGpu->device(), fCommandPool, 1, &vkBuffer)); |
Greg Daniel | 43833b0 | 2020-07-14 09:21:48 -0400 | [diff] [blame] | 72 | } |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | void GrVkCommandPool::close() { |
| 76 | fOpen = false; |
| 77 | } |
| 78 | |
| 79 | void GrVkCommandPool::reset(GrVkGpu* gpu) { |
| 80 | SkASSERT(!fOpen); |
| 81 | fOpen = true; |
Greg Daniel | 95f0b16 | 2019-11-11 13:42:30 -0500 | [diff] [blame] | 82 | // We can't use the normal result macro calls here because we may call reset on a different |
| 83 | // thread and we can't be modifying the lost state on the GrVkGpu. We just call |
| 84 | // vkResetCommandPool and assume the "next" vulkan call will catch the lost device. |
| 85 | SkDEBUGCODE(VkResult result = )GR_VK_CALL(gpu->vkInterface(), |
| 86 | ResetCommandPool(gpu->device(), fCommandPool, 0)); |
| 87 | SkASSERT(result == VK_SUCCESS || result == VK_ERROR_DEVICE_LOST); |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 88 | } |
| 89 | |
Jim Van Verth | 5082df1 | 2020-03-11 16:14:51 -0400 | [diff] [blame] | 90 | void GrVkCommandPool::releaseResources() { |
Brian Salomon | e39526b | 2019-06-24 16:35:53 -0400 | [diff] [blame] | 91 | TRACE_EVENT0("skia.gpu", TRACE_FUNC); |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 92 | SkASSERT(!fOpen); |
Jim Van Verth | 5082df1 | 2020-03-11 16:14:51 -0400 | [diff] [blame] | 93 | fPrimaryCommandBuffer->releaseResources(); |
Greg Daniel | 7a32f1b | 2019-11-26 16:18:01 -0500 | [diff] [blame] | 94 | fPrimaryCommandBuffer->recycleSecondaryCommandBuffers(this); |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 95 | } |
| 96 | |
Jim Van Verth | 5082df1 | 2020-03-11 16:14:51 -0400 | [diff] [blame] | 97 | void GrVkCommandPool::freeGPUData() const { |
Jim Van Verth | 3e19216 | 2020-03-10 16:23:16 -0400 | [diff] [blame] | 98 | // TODO: having freeGPUData virtual on GrManagedResource be const seems like a bad restriction since |
Greg Daniel | 0addbdf | 2019-11-25 15:03:58 -0500 | [diff] [blame] | 99 | // we are changing the internal objects of these classes when it is called. We should go back a |
| 100 | // revisit how much of a headache it would be to make this function non-const |
| 101 | GrVkCommandPool* nonConstThis = const_cast<GrVkCommandPool*>(this); |
| 102 | nonConstThis->close(); |
Jim Van Verth | 5082df1 | 2020-03-11 16:14:51 -0400 | [diff] [blame] | 103 | nonConstThis->releaseResources(); |
| 104 | fPrimaryCommandBuffer->freeGPUData(fGpu, fCommandPool); |
Greg Daniel | 8daf3b7 | 2019-07-30 09:57:26 -0400 | [diff] [blame] | 105 | for (const auto& buffer : fAvailableSecondaryBuffers) { |
Jim Van Verth | 5082df1 | 2020-03-11 16:14:51 -0400 | [diff] [blame] | 106 | buffer->freeGPUData(fGpu, fCommandPool); |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 107 | } |
| 108 | if (fCommandPool != VK_NULL_HANDLE) { |
Jim Van Verth | 5082df1 | 2020-03-11 16:14:51 -0400 | [diff] [blame] | 109 | GR_VK_CALL(fGpu->vkInterface(), |
| 110 | DestroyCommandPool(fGpu->device(), fCommandPool, nullptr)); |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 111 | } |
| 112 | } |