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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "src/gpu/GrContextPriv.h" |
| 11 | #include "src/gpu/vk/GrVkCommandBuffer.h" |
| 12 | #include "src/gpu/vk/GrVkGpu.h" |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 13 | |
Greg Daniel | e643da6 | 2019-11-05 12:36:42 -0500 | [diff] [blame] | 14 | GrVkCommandPool* GrVkCommandPool::Create(GrVkGpu* gpu) { |
Greg Daniel | 8daf3b7 | 2019-07-30 09:57:26 -0400 | [diff] [blame] | 15 | VkCommandPoolCreateFlags cmdPoolCreateFlags = |
| 16 | VK_COMMAND_POOL_CREATE_TRANSIENT_BIT | |
| 17 | VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT; |
| 18 | if (gpu->protectedContext()) { |
| 19 | cmdPoolCreateFlags |= VK_COMMAND_POOL_CREATE_PROTECTED_BIT; |
| 20 | } |
Emircan Uysaler | 23ca4e7 | 2019-06-24 10:53:09 -0400 | [diff] [blame] | 21 | |
Greg Daniel | 8daf3b7 | 2019-07-30 09:57:26 -0400 | [diff] [blame] | 22 | const VkCommandPoolCreateInfo cmdPoolInfo = { |
| 23 | VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO, // sType |
| 24 | nullptr, // pNext |
| 25 | cmdPoolCreateFlags, // CmdPoolCreateFlags |
| 26 | gpu->queueIndex(), // queueFamilyIndex |
| 27 | }; |
Greg Daniel | 9b63dc8 | 2019-11-06 09:21:55 -0500 | [diff] [blame] | 28 | VkResult result; |
Greg Daniel | 8daf3b7 | 2019-07-30 09:57:26 -0400 | [diff] [blame] | 29 | VkCommandPool pool; |
Greg Daniel | 9b63dc8 | 2019-11-06 09:21:55 -0500 | [diff] [blame] | 30 | GR_VK_CALL_RESULT(gpu, result, CreateCommandPool(gpu->device(), &cmdPoolInfo, nullptr, &pool)); |
| 31 | if (result != VK_SUCCESS) { |
| 32 | return nullptr; |
| 33 | } |
Greg Daniel | 8daf3b7 | 2019-07-30 09:57:26 -0400 | [diff] [blame] | 34 | return new GrVkCommandPool(gpu, pool); |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | GrVkCommandPool::GrVkCommandPool(const GrVkGpu* gpu, VkCommandPool commandPool) |
| 38 | : fCommandPool(commandPool) { |
Greg Daniel | 0addbdf | 2019-11-25 15:03:58 -0500 | [diff] [blame] | 39 | fPrimaryCommandBuffer.reset(GrVkPrimaryCommandBuffer::Create(gpu, fCommandPool)); |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 40 | } |
| 41 | |
Greg Daniel | 8daf3b7 | 2019-07-30 09:57:26 -0400 | [diff] [blame] | 42 | std::unique_ptr<GrVkSecondaryCommandBuffer> GrVkCommandPool::findOrCreateSecondaryCommandBuffer( |
| 43 | GrVkGpu* gpu) { |
| 44 | std::unique_ptr<GrVkSecondaryCommandBuffer> result; |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 45 | if (fAvailableSecondaryBuffers.count()) { |
Greg Daniel | 8daf3b7 | 2019-07-30 09:57:26 -0400 | [diff] [blame] | 46 | result = std::move(fAvailableSecondaryBuffers.back()); |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 47 | fAvailableSecondaryBuffers.pop_back(); |
Greg Daniel | 8daf3b7 | 2019-07-30 09:57:26 -0400 | [diff] [blame] | 48 | } else{ |
| 49 | result.reset(GrVkSecondaryCommandBuffer::Create(gpu, this)); |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 50 | } |
Greg Daniel | 8daf3b7 | 2019-07-30 09:57:26 -0400 | [diff] [blame] | 51 | return result; |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | void GrVkCommandPool::recycleSecondaryCommandBuffer(GrVkSecondaryCommandBuffer* buffer) { |
Greg Daniel | 8daf3b7 | 2019-07-30 09:57:26 -0400 | [diff] [blame] | 55 | std::unique_ptr<GrVkSecondaryCommandBuffer> scb(buffer); |
| 56 | fAvailableSecondaryBuffers.push_back(std::move(scb)); |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | void GrVkCommandPool::close() { |
| 60 | fOpen = false; |
| 61 | } |
| 62 | |
| 63 | void GrVkCommandPool::reset(GrVkGpu* gpu) { |
| 64 | SkASSERT(!fOpen); |
| 65 | fOpen = true; |
Greg Daniel | 95f0b16 | 2019-11-11 13:42:30 -0500 | [diff] [blame] | 66 | // We can't use the normal result macro calls here because we may call reset on a different |
| 67 | // thread and we can't be modifying the lost state on the GrVkGpu. We just call |
| 68 | // vkResetCommandPool and assume the "next" vulkan call will catch the lost device. |
| 69 | SkDEBUGCODE(VkResult result = )GR_VK_CALL(gpu->vkInterface(), |
| 70 | ResetCommandPool(gpu->device(), fCommandPool, 0)); |
| 71 | SkASSERT(result == VK_SUCCESS || result == VK_ERROR_DEVICE_LOST); |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | void GrVkCommandPool::releaseResources(GrVkGpu* gpu) { |
Brian Salomon | e39526b | 2019-06-24 16:35:53 -0400 | [diff] [blame] | 75 | TRACE_EVENT0("skia.gpu", TRACE_FUNC); |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 76 | SkASSERT(!fOpen); |
| 77 | fPrimaryCommandBuffer->releaseResources(gpu); |
Greg Daniel | 7a32f1b | 2019-11-26 16:18:01 -0500 | [diff] [blame^] | 78 | fPrimaryCommandBuffer->recycleSecondaryCommandBuffers(this); |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | void GrVkCommandPool::abandonGPUData() const { |
Greg Daniel | 8daf3b7 | 2019-07-30 09:57:26 -0400 | [diff] [blame] | 82 | fPrimaryCommandBuffer->abandonGPUData(); |
| 83 | for (const auto& buffer : fAvailableSecondaryBuffers) { |
| 84 | buffer->abandonGPUData(); |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 85 | } |
| 86 | } |
| 87 | |
| 88 | void GrVkCommandPool::freeGPUData(GrVkGpu* gpu) const { |
Greg Daniel | 0addbdf | 2019-11-25 15:03:58 -0500 | [diff] [blame] | 89 | // TODO: having freeGPUData virtual on GrVkResource be const seems like a bad restriction since |
| 90 | // we are changing the internal objects of these classes when it is called. We should go back a |
| 91 | // revisit how much of a headache it would be to make this function non-const |
| 92 | GrVkCommandPool* nonConstThis = const_cast<GrVkCommandPool*>(this); |
| 93 | nonConstThis->close(); |
| 94 | nonConstThis->releaseResources(gpu); |
| 95 | fPrimaryCommandBuffer->freeGPUData(gpu, fCommandPool); |
Greg Daniel | 8daf3b7 | 2019-07-30 09:57:26 -0400 | [diff] [blame] | 96 | for (const auto& buffer : fAvailableSecondaryBuffers) { |
Greg Daniel | 0addbdf | 2019-11-25 15:03:58 -0500 | [diff] [blame] | 97 | buffer->freeGPUData(gpu, fCommandPool); |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 98 | } |
| 99 | if (fCommandPool != VK_NULL_HANDLE) { |
| 100 | GR_VK_CALL(gpu->vkInterface(), |
| 101 | DestroyCommandPool(gpu->device(), fCommandPool, nullptr)); |
| 102 | } |
| 103 | } |