Lugdunum  0.1.0
CommandBuffer.cpp
Go to the documentation of this file.
2 
5 
6 namespace lug {
7 namespace Graphics {
8 namespace Vulkan {
9 namespace API {
10 namespace Builder {
11 
12 CommandBuffer::CommandBuffer(const API::Device& device, const API::CommandPool& commandPool) : _device{device}, _commandPool(commandPool) {}
13 
14 bool CommandBuffer::build(API::CommandBuffer& instance, VkResult* returnResult) {
15  std::vector<API::CommandBuffer> descriptorSets(1);
16  if (!build(descriptorSets, returnResult)) {
17  return false;
18  }
19  instance = std::move(descriptorSets[0]);
20  return true;
21 }
22 
23 std::unique_ptr<API::CommandBuffer> CommandBuffer::build(VkResult* returnResult) {
24  std::vector<API::CommandBuffer> commandBuffers(1);
25  return build(commandBuffers, returnResult) ? std::make_unique<API::CommandBuffer>(std::move(commandBuffers[0])) : nullptr;
26 }
27 
28 bool CommandBuffer::build(std::vector<API::CommandBuffer>& commandBuffers, VkResult* returnResult) {
29  // Create the commandBuffer creation information for vkCreateCommandBuffer
30  const VkCommandBufferAllocateInfo allocateInfo{
31  /* allocateInfo.sType */ VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
32  /* allocateInfo.pNext */ nullptr,
33  /* allocateInfo.commandPool */ static_cast<VkCommandPool>(_commandPool),
34  /* allocateInfo.level */ _level,
35  /* allocateInfo.commandBufferCount */ static_cast<uint32_t>(commandBuffers.size()),
36  };
37 
38  std::vector<VkCommandBuffer> vkCommandBuffers(commandBuffers.size());
39 
40  // Create the commandBuffers
41  VkResult result = vkAllocateCommandBuffers(static_cast<VkDevice>(_device), &allocateInfo, vkCommandBuffers.data());
42 
43  if (returnResult) {
44  *returnResult = result;
45  }
46 
47  if (result != VK_SUCCESS) {
48  return false;
49  }
50 
51  for (uint32_t i = 0; i < commandBuffers.size(); ++i) {
52  commandBuffers[i] = API::CommandBuffer(vkCommandBuffers[i], &_commandPool);
53  }
54 
55  return true;
56 }
57 
58 std::vector<std::unique_ptr<API::CommandBuffer>> CommandBuffer::build(uint32_t count, VkResult* returnResult) {
59  // Output vector
60  std::vector<API::CommandBuffer> commandBuffers(count);
61  std::vector<std::unique_ptr<API::CommandBuffer>> outCommandBuffers(count);
62 
63  // Build the command buffers
64  if (!build(commandBuffers, returnResult)) {
65  return {};
66  }
67 
68  // Initialize the unique_ptr array
69  for (uint32_t i = 0; i < count; ++i) {
70  outCommandBuffers[i] = std::make_unique<API::CommandBuffer>(std::move(commandBuffers[i]));
71  }
72 
73  return outCommandBuffers;
74 }
75 
76 } // Builder
77 } // API
78 } // Vulkan
79 } // Graphics
80 } // lug
bool build(API::CommandBuffer &instance, VkResult *returnResult=nullptr)
CommandBuffer(const API::Device &device, const API::CommandPool &commandPool)