blob: 0a9db05e74ee024e64912dcd419ee37e8df4bb13 [file] [log] [blame]
Ethan Nicholas8e265a72018-12-12 16:22:40 -05001/*
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 Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/gpu/vk/GrVkCommandPool.h"
Ethan Nicholas8e265a72018-12-12 16:22:40 -05009
Adlai Hollera0693042020-10-14 11:23:11 -040010#include "src/gpu/GrDirectContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "src/gpu/vk/GrVkCommandBuffer.h"
12#include "src/gpu/vk/GrVkGpu.h"
Ethan Nicholas8e265a72018-12-12 16:22:40 -050013
Greg Daniele643da62019-11-05 12:36:42 -050014GrVkCommandPool* GrVkCommandPool::Create(GrVkGpu* gpu) {
Greg Daniel6253b572020-09-11 16:39:28 -040015 VkCommandPoolCreateFlags cmdPoolCreateFlags = VK_COMMAND_POOL_CREATE_TRANSIENT_BIT;
Greg Daniel8daf3b72019-07-30 09:57:26 -040016 if (gpu->protectedContext()) {
17 cmdPoolCreateFlags |= VK_COMMAND_POOL_CREATE_PROTECTED_BIT;
18 }
Emircan Uysaler23ca4e72019-06-24 10:53:09 -040019
Greg Daniel8daf3b72019-07-30 09:57:26 -040020 const VkCommandPoolCreateInfo cmdPoolInfo = {
21 VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO, // sType
22 nullptr, // pNext
23 cmdPoolCreateFlags, // CmdPoolCreateFlags
24 gpu->queueIndex(), // queueFamilyIndex
25 };
Greg Daniel9b63dc82019-11-06 09:21:55 -050026 VkResult result;
Greg Daniel8daf3b72019-07-30 09:57:26 -040027 VkCommandPool pool;
Greg Daniel9b63dc82019-11-06 09:21:55 -050028 GR_VK_CALL_RESULT(gpu, result, CreateCommandPool(gpu->device(), &cmdPoolInfo, nullptr, &pool));
29 if (result != VK_SUCCESS) {
30 return nullptr;
31 }
Greg Daniel315c8dc2019-11-26 15:41:27 -050032
33 GrVkPrimaryCommandBuffer* primaryCmdBuffer = GrVkPrimaryCommandBuffer::Create(gpu, pool);
34 if (!primaryCmdBuffer) {
35 GR_VK_CALL(gpu->vkInterface(), DestroyCommandPool(gpu->device(), pool, nullptr));
36 return nullptr;
37 }
38
39 return new GrVkCommandPool(gpu, pool, primaryCmdBuffer);
Ethan Nicholas8e265a72018-12-12 16:22:40 -050040}
41
Greg Daniel315c8dc2019-11-26 15:41:27 -050042GrVkCommandPool::GrVkCommandPool(GrVkGpu* gpu, VkCommandPool commandPool,
43 GrVkPrimaryCommandBuffer* primaryCmdBuffer)
Jim Van Verth5082df12020-03-11 16:14:51 -040044 : GrVkManagedResource(gpu)
45 , fCommandPool(commandPool)
Greg Daniel43833b02020-07-14 09:21:48 -040046 , fPrimaryCommandBuffer(primaryCmdBuffer)
47 , fMaxCachedSecondaryCommandBuffers(
48 gpu->vkCaps().maxPerPoolCachedSecondaryCommandBuffers()) {
Ethan Nicholas8e265a72018-12-12 16:22:40 -050049}
50
Greg Daniel8daf3b72019-07-30 09:57:26 -040051std::unique_ptr<GrVkSecondaryCommandBuffer> GrVkCommandPool::findOrCreateSecondaryCommandBuffer(
52 GrVkGpu* gpu) {
53 std::unique_ptr<GrVkSecondaryCommandBuffer> result;
Ethan Nicholas8e265a72018-12-12 16:22:40 -050054 if (fAvailableSecondaryBuffers.count()) {
Greg Daniel8daf3b72019-07-30 09:57:26 -040055 result = std::move(fAvailableSecondaryBuffers.back());
Ethan Nicholas8e265a72018-12-12 16:22:40 -050056 fAvailableSecondaryBuffers.pop_back();
Greg Daniel8daf3b72019-07-30 09:57:26 -040057 } else{
58 result.reset(GrVkSecondaryCommandBuffer::Create(gpu, this));
Ethan Nicholas8e265a72018-12-12 16:22:40 -050059 }
Greg Daniel8daf3b72019-07-30 09:57:26 -040060 return result;
Ethan Nicholas8e265a72018-12-12 16:22:40 -050061}
62
63void GrVkCommandPool::recycleSecondaryCommandBuffer(GrVkSecondaryCommandBuffer* buffer) {
Greg Daniel8daf3b72019-07-30 09:57:26 -040064 std::unique_ptr<GrVkSecondaryCommandBuffer> scb(buffer);
Greg Daniel43833b02020-07-14 09:21:48 -040065 if (fAvailableSecondaryBuffers.count() < fMaxCachedSecondaryCommandBuffers) {
66 fAvailableSecondaryBuffers.push_back(std::move(scb));
Greg Daniel0927dd62020-07-20 15:13:29 -040067 } else {
68 VkCommandBuffer vkBuffer = buffer->vkCommandBuffer();
69 GR_VK_CALL(fGpu->vkInterface(),
70 FreeCommandBuffers(fGpu->device(), fCommandPool, 1, &vkBuffer));
Greg Daniel43833b02020-07-14 09:21:48 -040071 }
Ethan Nicholas8e265a72018-12-12 16:22:40 -050072}
73
74void GrVkCommandPool::close() {
75 fOpen = false;
76}
77
78void GrVkCommandPool::reset(GrVkGpu* gpu) {
79 SkASSERT(!fOpen);
80 fOpen = true;
Greg Daniel95f0b162019-11-11 13:42:30 -050081 // We can't use the normal result macro calls here because we may call reset on a different
82 // thread and we can't be modifying the lost state on the GrVkGpu. We just call
83 // vkResetCommandPool and assume the "next" vulkan call will catch the lost device.
84 SkDEBUGCODE(VkResult result = )GR_VK_CALL(gpu->vkInterface(),
85 ResetCommandPool(gpu->device(), fCommandPool, 0));
86 SkASSERT(result == VK_SUCCESS || result == VK_ERROR_DEVICE_LOST);
Ethan Nicholas8e265a72018-12-12 16:22:40 -050087}
88
Jim Van Verth5082df12020-03-11 16:14:51 -040089void GrVkCommandPool::releaseResources() {
Brian Salomone39526b2019-06-24 16:35:53 -040090 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Ethan Nicholas8e265a72018-12-12 16:22:40 -050091 SkASSERT(!fOpen);
Jim Van Verth5082df12020-03-11 16:14:51 -040092 fPrimaryCommandBuffer->releaseResources();
Greg Daniel7a32f1b2019-11-26 16:18:01 -050093 fPrimaryCommandBuffer->recycleSecondaryCommandBuffers(this);
Ethan Nicholas8e265a72018-12-12 16:22:40 -050094}
95
Jim Van Verth5082df12020-03-11 16:14:51 -040096void GrVkCommandPool::freeGPUData() const {
Jim Van Verth3e192162020-03-10 16:23:16 -040097 // TODO: having freeGPUData virtual on GrManagedResource be const seems like a bad restriction since
Greg Daniel0addbdf2019-11-25 15:03:58 -050098 // we are changing the internal objects of these classes when it is called. We should go back a
99 // revisit how much of a headache it would be to make this function non-const
100 GrVkCommandPool* nonConstThis = const_cast<GrVkCommandPool*>(this);
101 nonConstThis->close();
Jim Van Verth5082df12020-03-11 16:14:51 -0400102 nonConstThis->releaseResources();
103 fPrimaryCommandBuffer->freeGPUData(fGpu, fCommandPool);
Greg Daniel8daf3b72019-07-30 09:57:26 -0400104 for (const auto& buffer : fAvailableSecondaryBuffers) {
Jim Van Verth5082df12020-03-11 16:14:51 -0400105 buffer->freeGPUData(fGpu, fCommandPool);
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500106 }
107 if (fCommandPool != VK_NULL_HANDLE) {
Jim Van Verth5082df12020-03-11 16:14:51 -0400108 GR_VK_CALL(fGpu->vkInterface(),
109 DestroyCommandPool(fGpu->device(), fCommandPool, nullptr));
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500110 }
111}