text: #include "commandPool.h"
  
  #include <stdexcept>
  
  #include "device.h"
  
  void CommandPool::init(Device* inDevice)
  {
      device = inDevice;
      if (!device->isInitialized())
      {
          throw std::runtime_error("Device was uninitialized while attempting to initialize commandPool");
      }
      
      createCommandPool();
  }
  
  void CommandPool::cleanup()
  {
      vkFreeCommandBuffers(device->getLogicalDevice(), vkCommandPool,
          static_cast<uint32_t>(commandBuffers.size()),
          commandBuffers.data()
      );
  
      vkDestroyCommandPool(device->getLogicalDevice(), vkCommandPool, nullptr);
  }
  
  VkCommandBuffer CommandPool::beginSingleTimeCommands()
  {
      VkCommandBufferAllocateInfo allocInfo = {};
      allocInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
      allocInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
      allocInfo.commandPool = vkCommandPool;
      allocInfo.commandBufferCount = 1;
  
      VkCommandBuffer commandBuffer;
      vkAllocateCommandBuffers(device->getLogicalDevice(), &allocInfo, &commandBuffer);
  
      VkCommandBufferBeginInfo beginInfo = {};
      beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
      beginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
  
      vkBeginCommandBuffer(commandBuffer, &beginInfo);
  
      return commandBuffer;
  }
  
  void CommandPool::endSingleTimeCommands(VkCommandBuffer commandBuffer)
  {
      vkEndCommandBuffer(commandBuffer);
  
      VkSubmitInfo submitInfo = {};
      submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
      submitInfo.commandBufferCount = 1;
      submitInfo.pCommandBuffers = &commandBuffer;
  
      vkQueueSubmit(device->getGraphicsQueue(), 1, &submitInfo, VK_NULL_HANDLE);
      vkQueueWaitIdle(device->getGraphicsQueue());
  
      vkFreeCommandBuffers(device->getLogicalDevice(), vkCommandPool, 1, &commandBuffer);
  }
  
  void CommandPool::createCommandPool()
  {
      QueueFamilyIndices queueFamilyIndices = device->findQueueFamilies();
  
      VkCommandPoolCreateInfo poolInfo = {};
      poolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
      poolInfo.queueFamilyIndex = queueFamilyIndices.graphicsFamily;
      poolInfo.flags = 0; // Optional
  
      if (vkCreateCommandPool(device->getLogicalDevice(), &poolInfo, nullptr, &vkCommandPool) != VK_SUCCESS)
      {
          throw std::runtime_error("Failed to create command pool!");
      }
  }