blob: 85c5738e4f7e7243fe5da863a5604e88f9e1b00e [file] [log] [blame]
Alexis Hetu9c4ecae2018-11-20 16:26:10 -05001// Copyright 2018 The SwiftShader Authors. All Rights Reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#include "VkCommandPool.hpp"
Alexis Hetucd610c92019-02-01 16:47:51 -050016#include "VkCommandBuffer.hpp"
Alexis Hetubffee5e2018-11-19 11:30:43 -050017#include "VkDestroy.h"
18#include <algorithm>
Alexis Hetu9c4ecae2018-11-20 16:26:10 -050019
20namespace vk
21{
22
23CommandPool::CommandPool(const VkCommandPoolCreateInfo* pCreateInfo, void* mem)
24{
Alexis Hetubffee5e2018-11-19 11:30:43 -050025 // FIXME (b/119409619): use an allocator here so we can control all memory allocations
26 commandBuffers = new std::set<VkCommandBuffer>();
Alexis Hetu9c4ecae2018-11-20 16:26:10 -050027}
28
29void CommandPool::destroy(const VkAllocationCallbacks* pAllocator)
30{
Alexis Hetubffee5e2018-11-19 11:30:43 -050031 // Free command Buffers allocated in allocateCommandBuffers
32 for(auto commandBuffer : *commandBuffers)
33 {
34 vk::destroy(commandBuffer, DEVICE_MEMORY);
35 }
36
37 // FIXME (b/119409619): use an allocator here so we can control all memory allocations
38 delete commandBuffers;
Alexis Hetu9c4ecae2018-11-20 16:26:10 -050039}
40
41size_t CommandPool::ComputeRequiredAllocationSize(const VkCommandPoolCreateInfo* pCreateInfo)
42{
43 return 0;
44}
45
Alexis Hetubffee5e2018-11-19 11:30:43 -050046VkResult CommandPool::allocateCommandBuffers(VkCommandBufferLevel level, uint32_t commandBufferCount, VkCommandBuffer* pCommandBuffers)
47{
48 for(uint32_t i = 0; i < commandBufferCount; i++)
49 {
50 DispatchableCommandBuffer* commandBuffer = new (DEVICE_MEMORY) DispatchableCommandBuffer(level);
51 if(commandBuffer)
52 {
53 pCommandBuffers[i] = *commandBuffer;
54 }
55 else
56 {
57 for(uint32_t j = 0; j < i; j++)
58 {
59 vk::destroy(pCommandBuffers[j], DEVICE_MEMORY);
60 }
61 for(uint32_t j = 0; j < commandBufferCount; j++)
62 {
63 pCommandBuffers[j] = VK_NULL_HANDLE;
64 }
65 return VK_ERROR_OUT_OF_DEVICE_MEMORY;
66 }
67 }
68
69 commandBuffers->insert(pCommandBuffers, pCommandBuffers + commandBufferCount);
70
71 return VK_SUCCESS;
72}
73
74void CommandPool::freeCommandBuffers(uint32_t commandBufferCount, const VkCommandBuffer* pCommandBuffers)
75{
76 for(uint32_t i = 0; i < commandBufferCount; ++i)
77 {
78 commandBuffers->erase(pCommandBuffers[i]);
79 vk::destroy(pCommandBuffers[i], DEVICE_MEMORY);
80 }
81}
82
Alexis Hetucd610c92019-02-01 16:47:51 -050083VkResult CommandPool::reset(VkCommandPoolResetFlags flags)
84{
85 // According the Vulkan 1.1 spec:
86 // "All command buffers that have been allocated from
87 // the command pool are put in the initial state."
88 for(auto commandBuffer : *commandBuffers)
89 {
90 Cast(commandBuffer)->reset(flags);
91 }
92
93 // According the Vulkan 1.1 spec:
94 // "Resetting a command pool recycles all of the
95 // resources from all of the command buffers allocated
96 // from the command pool back to the command pool."
97 commandBuffers->clear();
98
99 return VK_SUCCESS;
100}
101
102void CommandPool::trim(VkCommandPoolTrimFlags flags)
103{
104 // TODO (b/119827933): Optimize memory usage here
105}
106
Alexis Hetu9c4ecae2018-11-20 16:26:10 -0500107} // namespace vk