Alexis Hetu | 9c4ecae | 2018-11-20 16:26:10 -0500 | [diff] [blame] | 1 | // 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 Hetu | cd610c9 | 2019-02-01 16:47:51 -0500 | [diff] [blame^] | 16 | #include "VkCommandBuffer.hpp" |
Alexis Hetu | bffee5e | 2018-11-19 11:30:43 -0500 | [diff] [blame] | 17 | #include "VkDestroy.h" |
| 18 | #include <algorithm> |
Alexis Hetu | 9c4ecae | 2018-11-20 16:26:10 -0500 | [diff] [blame] | 19 | |
| 20 | namespace vk |
| 21 | { |
| 22 | |
| 23 | CommandPool::CommandPool(const VkCommandPoolCreateInfo* pCreateInfo, void* mem) |
| 24 | { |
Alexis Hetu | bffee5e | 2018-11-19 11:30:43 -0500 | [diff] [blame] | 25 | // FIXME (b/119409619): use an allocator here so we can control all memory allocations |
| 26 | commandBuffers = new std::set<VkCommandBuffer>(); |
Alexis Hetu | 9c4ecae | 2018-11-20 16:26:10 -0500 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | void CommandPool::destroy(const VkAllocationCallbacks* pAllocator) |
| 30 | { |
Alexis Hetu | bffee5e | 2018-11-19 11:30:43 -0500 | [diff] [blame] | 31 | // 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 Hetu | 9c4ecae | 2018-11-20 16:26:10 -0500 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | size_t CommandPool::ComputeRequiredAllocationSize(const VkCommandPoolCreateInfo* pCreateInfo) |
| 42 | { |
| 43 | return 0; |
| 44 | } |
| 45 | |
Alexis Hetu | bffee5e | 2018-11-19 11:30:43 -0500 | [diff] [blame] | 46 | VkResult 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 | |
| 74 | void 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 Hetu | cd610c9 | 2019-02-01 16:47:51 -0500 | [diff] [blame^] | 83 | VkResult 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 | |
| 102 | void CommandPool::trim(VkCommandPoolTrimFlags flags) |
| 103 | { |
| 104 | // TODO (b/119827933): Optimize memory usage here |
| 105 | } |
| 106 | |
Alexis Hetu | 9c4ecae | 2018-11-20 16:26:10 -0500 | [diff] [blame] | 107 | } // namespace vk |