1cb93a386Sopenharmony_ci// Copyright 2018 The SwiftShader Authors. All Rights Reserved. 2cb93a386Sopenharmony_ci// 3cb93a386Sopenharmony_ci// Licensed under the Apache License, Version 2.0 (the "License"); 4cb93a386Sopenharmony_ci// you may not use this file except in compliance with the License. 5cb93a386Sopenharmony_ci// You may obtain a copy of the License at 6cb93a386Sopenharmony_ci// 7cb93a386Sopenharmony_ci// http://www.apache.org/licenses/LICENSE-2.0 8cb93a386Sopenharmony_ci// 9cb93a386Sopenharmony_ci// Unless required by applicable law or agreed to in writing, software 10cb93a386Sopenharmony_ci// distributed under the License is distributed on an "AS IS" BASIS, 11cb93a386Sopenharmony_ci// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12cb93a386Sopenharmony_ci// See the License for the specific language governing permissions and 13cb93a386Sopenharmony_ci// limitations under the License. 14cb93a386Sopenharmony_ci 15cb93a386Sopenharmony_ci#include "VkCommandPool.hpp" 16cb93a386Sopenharmony_ci 17cb93a386Sopenharmony_ci#include "VkCommandBuffer.hpp" 18cb93a386Sopenharmony_ci#include "VkDestroy.hpp" 19cb93a386Sopenharmony_ci 20cb93a386Sopenharmony_ci#include <algorithm> 21cb93a386Sopenharmony_ci#include <new> 22cb93a386Sopenharmony_ci 23cb93a386Sopenharmony_cinamespace vk { 24cb93a386Sopenharmony_ci 25cb93a386Sopenharmony_ciCommandPool::CommandPool(const VkCommandPoolCreateInfo *pCreateInfo, void *mem) 26cb93a386Sopenharmony_ci{ 27cb93a386Sopenharmony_ci} 28cb93a386Sopenharmony_ci 29cb93a386Sopenharmony_civoid CommandPool::destroy(const VkAllocationCallbacks *pAllocator) 30cb93a386Sopenharmony_ci{ 31cb93a386Sopenharmony_ci // Free command Buffers allocated in allocateCommandBuffers 32cb93a386Sopenharmony_ci for(auto commandBuffer : commandBuffers) 33cb93a386Sopenharmony_ci { 34cb93a386Sopenharmony_ci vk::destroy(commandBuffer, NULL_ALLOCATION_CALLBACKS); 35cb93a386Sopenharmony_ci } 36cb93a386Sopenharmony_ci} 37cb93a386Sopenharmony_ci 38cb93a386Sopenharmony_cisize_t CommandPool::ComputeRequiredAllocationSize(const VkCommandPoolCreateInfo *pCreateInfo) 39cb93a386Sopenharmony_ci{ 40cb93a386Sopenharmony_ci return 0; 41cb93a386Sopenharmony_ci} 42cb93a386Sopenharmony_ci 43cb93a386Sopenharmony_ciVkResult CommandPool::allocateCommandBuffers(Device *device, VkCommandBufferLevel level, uint32_t commandBufferCount, VkCommandBuffer *pCommandBuffers) 44cb93a386Sopenharmony_ci{ 45cb93a386Sopenharmony_ci for(uint32_t i = 0; i < commandBufferCount; i++) 46cb93a386Sopenharmony_ci { 47cb93a386Sopenharmony_ci // TODO(b/119409619): Allocate command buffers from the pool memory. 48cb93a386Sopenharmony_ci void *deviceMemory = vk::allocateHostMemory(sizeof(DispatchableCommandBuffer), REQUIRED_MEMORY_ALIGNMENT, 49cb93a386Sopenharmony_ci NULL_ALLOCATION_CALLBACKS, DispatchableCommandBuffer::GetAllocationScope()); 50cb93a386Sopenharmony_ci ASSERT(deviceMemory); 51cb93a386Sopenharmony_ci DispatchableCommandBuffer *commandBuffer = new(deviceMemory) DispatchableCommandBuffer(device, level); 52cb93a386Sopenharmony_ci if(commandBuffer) 53cb93a386Sopenharmony_ci { 54cb93a386Sopenharmony_ci pCommandBuffers[i] = *commandBuffer; 55cb93a386Sopenharmony_ci } 56cb93a386Sopenharmony_ci else 57cb93a386Sopenharmony_ci { 58cb93a386Sopenharmony_ci for(uint32_t j = 0; j < i; j++) 59cb93a386Sopenharmony_ci { 60cb93a386Sopenharmony_ci vk::destroy(pCommandBuffers[j], NULL_ALLOCATION_CALLBACKS); 61cb93a386Sopenharmony_ci } 62cb93a386Sopenharmony_ci 63cb93a386Sopenharmony_ci for(uint32_t j = 0; j < commandBufferCount; j++) 64cb93a386Sopenharmony_ci { 65cb93a386Sopenharmony_ci pCommandBuffers[j] = VK_NULL_HANDLE; 66cb93a386Sopenharmony_ci } 67cb93a386Sopenharmony_ci 68cb93a386Sopenharmony_ci return VK_ERROR_OUT_OF_DEVICE_MEMORY; 69cb93a386Sopenharmony_ci } 70cb93a386Sopenharmony_ci } 71cb93a386Sopenharmony_ci 72cb93a386Sopenharmony_ci commandBuffers.insert(pCommandBuffers, pCommandBuffers + commandBufferCount); 73cb93a386Sopenharmony_ci 74cb93a386Sopenharmony_ci return VK_SUCCESS; 75cb93a386Sopenharmony_ci} 76cb93a386Sopenharmony_ci 77cb93a386Sopenharmony_civoid CommandPool::freeCommandBuffers(uint32_t commandBufferCount, const VkCommandBuffer *pCommandBuffers) 78cb93a386Sopenharmony_ci{ 79cb93a386Sopenharmony_ci for(uint32_t i = 0; i < commandBufferCount; i++) 80cb93a386Sopenharmony_ci { 81cb93a386Sopenharmony_ci commandBuffers.erase(pCommandBuffers[i]); 82cb93a386Sopenharmony_ci vk::destroy(pCommandBuffers[i], NULL_ALLOCATION_CALLBACKS); 83cb93a386Sopenharmony_ci } 84cb93a386Sopenharmony_ci} 85cb93a386Sopenharmony_ci 86cb93a386Sopenharmony_ciVkResult CommandPool::reset(VkCommandPoolResetFlags flags) 87cb93a386Sopenharmony_ci{ 88cb93a386Sopenharmony_ci // According the Vulkan 1.1 spec: 89cb93a386Sopenharmony_ci // "All command buffers that have been allocated from 90cb93a386Sopenharmony_ci // the command pool are put in the initial state." 91cb93a386Sopenharmony_ci for(auto commandBuffer : commandBuffers) 92cb93a386Sopenharmony_ci { 93cb93a386Sopenharmony_ci vk::Cast(commandBuffer)->reset(flags); 94cb93a386Sopenharmony_ci } 95cb93a386Sopenharmony_ci 96cb93a386Sopenharmony_ci return VK_SUCCESS; 97cb93a386Sopenharmony_ci} 98cb93a386Sopenharmony_ci 99cb93a386Sopenharmony_civoid CommandPool::trim(VkCommandPoolTrimFlags flags) 100cb93a386Sopenharmony_ci{ 101cb93a386Sopenharmony_ci // TODO (b/119827933): Optimize memory usage here 102cb93a386Sopenharmony_ci} 103cb93a386Sopenharmony_ci 104cb93a386Sopenharmony_ci} // namespace vk 105