summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorEdward O'Callaghan <funfunctor@folklore1984.net>2017-02-08 01:13:18 +1100
committerEdward O'Callaghan <funfunctor@folklore1984.net>2017-02-08 01:13:18 +1100
commit590f6a79802feead544b1ab46b2aae42f2633599 (patch)
tree58b19f8044fd3a8b79b4d2b12514e8dc3d1d02ec /src
parentfa3f688360218b3f8a049797342e8026064fd4c1 (diff)
src/main.cpp: Implement CommandPools && CommandBuffers
Signed-off-by: Edward O'Callaghan <funfunctor@folklore1984.net>
Diffstat (limited to 'src')
-rw-r--r--src/main.cpp57
1 files changed, 57 insertions, 0 deletions
diff --git a/src/main.cpp b/src/main.cpp
index 31a0c14..2609358 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -47,6 +47,9 @@ private:
std::vector<VDeleter<VkImageView>> swapChainImageViews;
std::vector<VDeleter<VkFramebuffer>> swapChainFramebuffers;
+ VDeleter<VkCommandPool> commandPool{device, vkDestroyCommandPool};
+ std::vector<VkCommandBuffer> commandBuffers;
+
struct QueueFamilyIndices {
int graphicsFamily = -1;
int presentFamily = -1;
@@ -94,6 +97,60 @@ private:
createRenderPass();
createGraphicsPipeline();
createFramebuffers();
+ createCommandPool();
+ createCommandBuffers();
+ }
+
+ void createCommandPool() {
+ QueueFamilyIndices queueFamilyIndices = findQueueFamilies(physicalDevice);
+
+ VkCommandPoolCreateInfo poolInfo = {};
+ poolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
+ poolInfo.queueFamilyIndex = queueFamilyIndices.graphicsFamily;
+ poolInfo.flags = 0; // Optional
+
+ if (vkCreateCommandPool(device, &poolInfo, nullptr, commandPool.replace()) != VK_SUCCESS)
+ throw std::runtime_error("failed to create command pool!");
+ }
+
+ void createCommandBuffers() {
+ commandBuffers.resize(swapChainFramebuffers.size());
+
+ VkCommandBufferAllocateInfo allocInfo = {};
+ allocInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
+ allocInfo.commandPool = commandPool;
+ allocInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
+ allocInfo.commandBufferCount = (uint32_t) commandBuffers.size();
+
+ if (vkAllocateCommandBuffers(device, &allocInfo, commandBuffers.data()) != VK_SUCCESS)
+ throw std::runtime_error("failed to allocate command buffers!");
+
+ for (size_t i = 0; i < commandBuffers.size(); i++) {
+ VkCommandBufferBeginInfo beginInfo = {};
+ beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
+ beginInfo.flags = VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT;
+
+ vkBeginCommandBuffer(commandBuffers[i], &beginInfo);
+
+ VkRenderPassBeginInfo renderPassInfo = {};
+ renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
+ renderPassInfo.renderPass = renderPass;
+ renderPassInfo.framebuffer = swapChainFramebuffers[i];
+ renderPassInfo.renderArea.offset = {0, 0};
+ renderPassInfo.renderArea.extent = swapChainExtent;
+
+ VkClearValue clearColor = {0.0f, 0.0f, 0.0f, 1.0f};
+ renderPassInfo.clearValueCount = 1;
+ renderPassInfo.pClearValues = &clearColor;
+
+ vkCmdBeginRenderPass(commandBuffers[i], &renderPassInfo, VK_SUBPASS_CONTENTS_INLINE);
+ vkCmdBindPipeline(commandBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, graphicsPipeline);
+ vkCmdDraw(commandBuffers[i], 3, 1, 0, 0);
+ vkCmdEndRenderPass(commandBuffers[i]);
+
+ if (vkEndCommandBuffer(commandBuffers[i]) != VK_SUCCESS)
+ throw std::runtime_error("failed to record command buffer!");
+ }
}
void createFramebuffers() {