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"
16
17 #include "VkCommandBuffer.hpp"
18 #include "VkDestroy.hpp"
19
20 #include <algorithm>
21 #include <new>
22
23 namespace vk {
24
CommandPool(const VkCommandPoolCreateInfo * pCreateInfo,void * mem)25 CommandPool::CommandPool(const VkCommandPoolCreateInfo *pCreateInfo, void *mem)
26 {
27 }
28
destroy(const VkAllocationCallbacks * pAllocator)29 void CommandPool::destroy(const VkAllocationCallbacks *pAllocator)
30 {
31 // Free command Buffers allocated in allocateCommandBuffers
32 for(auto commandBuffer : commandBuffers)
33 {
34 vk::destroy(commandBuffer, NULL_ALLOCATION_CALLBACKS);
35 }
36 }
37
ComputeRequiredAllocationSize(const VkCommandPoolCreateInfo * pCreateInfo)38 size_t CommandPool::ComputeRequiredAllocationSize(const VkCommandPoolCreateInfo *pCreateInfo)
39 {
40 return 0;
41 }
42
allocateCommandBuffers(Device * device,VkCommandBufferLevel level,uint32_t commandBufferCount,VkCommandBuffer * pCommandBuffers)43 VkResult CommandPool::allocateCommandBuffers(Device *device, VkCommandBufferLevel level, uint32_t commandBufferCount, VkCommandBuffer *pCommandBuffers)
44 {
45 for(uint32_t i = 0; i < commandBufferCount; i++)
46 {
47 // TODO(b/119409619): Allocate command buffers from the pool memory.
48 void *deviceMemory = vk::allocateHostMemory(sizeof(DispatchableCommandBuffer), REQUIRED_MEMORY_ALIGNMENT,
49 NULL_ALLOCATION_CALLBACKS, DispatchableCommandBuffer::GetAllocationScope());
50 ASSERT(deviceMemory);
51 DispatchableCommandBuffer *commandBuffer = new(deviceMemory) DispatchableCommandBuffer(device, level);
52 if(commandBuffer)
53 {
54 pCommandBuffers[i] = *commandBuffer;
55 }
56 else
57 {
58 for(uint32_t j = 0; j < i; j++)
59 {
60 vk::destroy(pCommandBuffers[j], NULL_ALLOCATION_CALLBACKS);
61 }
62
63 for(uint32_t j = 0; j < commandBufferCount; j++)
64 {
65 pCommandBuffers[j] = VK_NULL_HANDLE;
66 }
67
68 return VK_ERROR_OUT_OF_DEVICE_MEMORY;
69 }
70 }
71
72 commandBuffers.insert(pCommandBuffers, pCommandBuffers + commandBufferCount);
73
74 return VK_SUCCESS;
75 }
76
freeCommandBuffers(uint32_t commandBufferCount,const VkCommandBuffer * pCommandBuffers)77 void CommandPool::freeCommandBuffers(uint32_t commandBufferCount, const VkCommandBuffer *pCommandBuffers)
78 {
79 for(uint32_t i = 0; i < commandBufferCount; i++)
80 {
81 commandBuffers.erase(pCommandBuffers[i]);
82 vk::destroy(pCommandBuffers[i], NULL_ALLOCATION_CALLBACKS);
83 }
84 }
85
reset(VkCommandPoolResetFlags flags)86 VkResult CommandPool::reset(VkCommandPoolResetFlags flags)
87 {
88 // According the Vulkan 1.1 spec:
89 // "All command buffers that have been allocated from
90 // the command pool are put in the initial state."
91 for(auto commandBuffer : commandBuffers)
92 {
93 vk::Cast(commandBuffer)->reset(flags);
94 }
95
96 return VK_SUCCESS;
97 }
98
trim(VkCommandPoolTrimFlags flags)99 void CommandPool::trim(VkCommandPoolTrimFlags flags)
100 {
101 // TODO (b/119827933): Optimize memory usage here
102 }
103
104 } // namespace vk
105