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 | 1a9714a | 2019-04-12 11:48:12 -0400 | [diff] [blame] | 19 | #include <new> |
Alexis Hetu | 9c4ecae | 2018-11-20 16:26:10 -0500 | [diff] [blame] | 20 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 21 | namespace vk { |
Alexis Hetu | 9c4ecae | 2018-11-20 16:26:10 -0500 | [diff] [blame] | 22 | |
Ben Clayton | 2ed93ab | 2019-12-17 20:38:03 +0000 | [diff] [blame] | 23 | CommandPool::CommandPool(const VkCommandPoolCreateInfo *pCreateInfo, void *mem) |
Alexis Hetu | 9c4ecae | 2018-11-20 16:26:10 -0500 | [diff] [blame] | 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 |
Ben Clayton | 2ed93ab | 2019-12-17 20:38:03 +0000 | [diff] [blame] | 26 | void *deviceMemory = vk::allocate(sizeof(std::set<VkCommandBuffer>), REQUIRED_MEMORY_ALIGNMENT, |
Alexis Hetu | 1a9714a | 2019-04-12 11:48:12 -0400 | [diff] [blame] | 27 | DEVICE_MEMORY, GetAllocationScope()); |
| 28 | ASSERT(deviceMemory); |
Ben Clayton | 2ed93ab | 2019-12-17 20:38:03 +0000 | [diff] [blame] | 29 | commandBuffers = new(deviceMemory) std::set<VkCommandBuffer>(); |
Alexis Hetu | 9c4ecae | 2018-11-20 16:26:10 -0500 | [diff] [blame] | 30 | } |
| 31 | |
Ben Clayton | 2ed93ab | 2019-12-17 20:38:03 +0000 | [diff] [blame] | 32 | void CommandPool::destroy(const VkAllocationCallbacks *pAllocator) |
Alexis Hetu | 9c4ecae | 2018-11-20 16:26:10 -0500 | [diff] [blame] | 33 | { |
Alexis Hetu | bffee5e | 2018-11-19 11:30:43 -0500 | [diff] [blame] | 34 | // Free command Buffers allocated in allocateCommandBuffers |
| 35 | for(auto commandBuffer : *commandBuffers) |
| 36 | { |
| 37 | vk::destroy(commandBuffer, DEVICE_MEMORY); |
| 38 | } |
| 39 | |
| 40 | // FIXME (b/119409619): use an allocator here so we can control all memory allocations |
Alexis Hetu | 1a9714a | 2019-04-12 11:48:12 -0400 | [diff] [blame] | 41 | vk::deallocate(commandBuffers, DEVICE_MEMORY); |
Alexis Hetu | 9c4ecae | 2018-11-20 16:26:10 -0500 | [diff] [blame] | 42 | } |
| 43 | |
Ben Clayton | 2ed93ab | 2019-12-17 20:38:03 +0000 | [diff] [blame] | 44 | size_t CommandPool::ComputeRequiredAllocationSize(const VkCommandPoolCreateInfo *pCreateInfo) |
Alexis Hetu | 9c4ecae | 2018-11-20 16:26:10 -0500 | [diff] [blame] | 45 | { |
| 46 | return 0; |
| 47 | } |
| 48 | |
Ben Clayton | a4e06ca | 2019-12-03 12:38:14 +0000 | [diff] [blame] | 49 | VkResult CommandPool::allocateCommandBuffers(Device *device, VkCommandBufferLevel level, uint32_t commandBufferCount, VkCommandBuffer *pCommandBuffers) |
Alexis Hetu | bffee5e | 2018-11-19 11:30:43 -0500 | [diff] [blame] | 50 | { |
| 51 | for(uint32_t i = 0; i < commandBufferCount; i++) |
| 52 | { |
Alexis Hetu | 1a9714a | 2019-04-12 11:48:12 -0400 | [diff] [blame] | 53 | // FIXME (b/119409619): use an allocator here so we can control all memory allocations |
Ben Clayton | 2ed93ab | 2019-12-17 20:38:03 +0000 | [diff] [blame] | 54 | void *deviceMemory = vk::allocate(sizeof(DispatchableCommandBuffer), REQUIRED_MEMORY_ALIGNMENT, |
Alexis Hetu | 1a9714a | 2019-04-12 11:48:12 -0400 | [diff] [blame] | 55 | DEVICE_MEMORY, DispatchableCommandBuffer::GetAllocationScope()); |
| 56 | ASSERT(deviceMemory); |
Ben Clayton | a4e06ca | 2019-12-03 12:38:14 +0000 | [diff] [blame] | 57 | DispatchableCommandBuffer *commandBuffer = new(deviceMemory) DispatchableCommandBuffer(device, level); |
Alexis Hetu | bffee5e | 2018-11-19 11:30:43 -0500 | [diff] [blame] | 58 | if(commandBuffer) |
| 59 | { |
| 60 | pCommandBuffers[i] = *commandBuffer; |
| 61 | } |
| 62 | else |
| 63 | { |
| 64 | for(uint32_t j = 0; j < i; j++) |
| 65 | { |
| 66 | vk::destroy(pCommandBuffers[j], DEVICE_MEMORY); |
| 67 | } |
| 68 | for(uint32_t j = 0; j < commandBufferCount; j++) |
| 69 | { |
| 70 | pCommandBuffers[j] = VK_NULL_HANDLE; |
| 71 | } |
| 72 | return VK_ERROR_OUT_OF_DEVICE_MEMORY; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | commandBuffers->insert(pCommandBuffers, pCommandBuffers + commandBufferCount); |
| 77 | |
| 78 | return VK_SUCCESS; |
| 79 | } |
| 80 | |
Ben Clayton | 2ed93ab | 2019-12-17 20:38:03 +0000 | [diff] [blame] | 81 | void CommandPool::freeCommandBuffers(uint32_t commandBufferCount, const VkCommandBuffer *pCommandBuffers) |
Alexis Hetu | bffee5e | 2018-11-19 11:30:43 -0500 | [diff] [blame] | 82 | { |
| 83 | for(uint32_t i = 0; i < commandBufferCount; ++i) |
| 84 | { |
| 85 | commandBuffers->erase(pCommandBuffers[i]); |
| 86 | vk::destroy(pCommandBuffers[i], DEVICE_MEMORY); |
| 87 | } |
| 88 | } |
| 89 | |
Alexis Hetu | cd610c9 | 2019-02-01 16:47:51 -0500 | [diff] [blame] | 90 | VkResult CommandPool::reset(VkCommandPoolResetFlags flags) |
| 91 | { |
| 92 | // According the Vulkan 1.1 spec: |
| 93 | // "All command buffers that have been allocated from |
| 94 | // the command pool are put in the initial state." |
| 95 | for(auto commandBuffer : *commandBuffers) |
| 96 | { |
Alexis Hetu | bd4cf81 | 2019-06-14 15:14:07 -0400 | [diff] [blame] | 97 | vk::Cast(commandBuffer)->reset(flags); |
Alexis Hetu | cd610c9 | 2019-02-01 16:47:51 -0500 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | // According the Vulkan 1.1 spec: |
| 101 | // "Resetting a command pool recycles all of the |
| 102 | // resources from all of the command buffers allocated |
| 103 | // from the command pool back to the command pool." |
| 104 | commandBuffers->clear(); |
| 105 | |
| 106 | return VK_SUCCESS; |
| 107 | } |
| 108 | |
| 109 | void CommandPool::trim(VkCommandPoolTrimFlags flags) |
| 110 | { |
| 111 | // TODO (b/119827933): Optimize memory usage here |
| 112 | } |
| 113 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 114 | } // namespace vk |