21 CommandPool::CommandPool(
void) {
22 VkDevice device = VulkanContext::getCurrent()->getDevice()->getHandle();
23 QueueFamilyIndices queueFamilyIndices = VulkanContext::getCurrent()->getPhysicalDevice()->getQueueFamilyIndices();
26 VkCommandPoolCreateInfo createInfo = {};
27 createInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
28 createInfo.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
29 createInfo.queueFamilyIndex =
static_cast<uint32_t
> (queueFamilyIndices.
graphics);
32 if (vkCreateCommandPool(device, &createInfo,
nullptr, &handle) != VK_SUCCESS) {
33 throw runtime_error(
"Could not create a command pool!");
37 CommandPool::~CommandPool(
void) {
38 VkDevice device = VulkanContext::getCurrent()->getDevice()->getHandle();
41 vkDestroyCommandPool(device, handle,
nullptr);
44 VkCommandPool CommandPool::getHandle() {
48 CommandBuffer::CommandBuffer(
void) {
49 VkDevice device = VulkanContext::getCurrent()->getDevice()->getHandle();
50 VkCommandPool commandPool = VulkanContext::getCurrent()->getCommandPool()->getHandle();
53 VkCommandBufferAllocateInfo allocateInfo = {};
54 allocateInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
55 allocateInfo.commandPool = commandPool;
56 allocateInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
57 allocateInfo.commandBufferCount = 1;
60 if (vkAllocateCommandBuffers(device, &allocateInfo, &handle) != VK_SUCCESS) {
61 throw runtime_error(
"Could not allocate the command buffers!");
65 CommandBuffer::~CommandBuffer(
void) {
66 VkDevice device = VulkanContext::getCurrent()->getDevice()->getHandle();
67 VkCommandPool commandPool = VulkanContext::getCurrent()->getCommandPool()->getHandle();
70 vkFreeCommandBuffers(device, commandPool, 1, &handle);
73 VkCommandBuffer CommandBuffer::getHandle() {
77 void CommandBuffer::begin() {
78 VkDevice device = VulkanContext::getCurrent()->getDevice()->getHandle();
79 VkFence fence = VulkanContext::getCurrent()->getRenderFence()->getHandle();
82 vkWaitForFences(device, 1, &fence, VK_TRUE, std::numeric_limits<uint64_t>::max());
83 vkResetFences(device, 1, &fence);
86 VkCommandBufferBeginInfo beginInfo = {};
87 beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
88 beginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
91 if (vkBeginCommandBuffer(handle, &beginInfo) != VK_SUCCESS) {
92 throw runtime_error(
"Could not begin recording the command buffers!");
96 void CommandBuffer::end() {
97 if (vkEndCommandBuffer(handle) != VK_SUCCESS) {
98 throw runtime_error(
"Could not record the command buffer!");
102 void CommandBuffer::beginRenderPass(
RenderPass* renderPass) {
103 VkFramebuffer framebuffer = VulkanContext::getCurrent()->getSwapchainFramebuffer()->getHandle();
104 SwapchainData swapchainData = VulkanContext::getCurrent()->getSwapchain()->getSwapchainData();
107 vector<VkClearValue> clearValues(2);
108 clearValues[0].color = {0.0f, 0.0f, 0.0f, 1.0f};
109 clearValues[1].depthStencil = {1.0f, 0};
112 VkRenderPassBeginInfo renderPassBegin = {};
113 renderPassBegin.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
114 renderPassBegin.renderPass = renderPass->
getHandle();
115 renderPassBegin.framebuffer = framebuffer;
116 renderPassBegin.renderArea.offset.x = 0;
117 renderPassBegin.renderArea.offset.y = 0;
118 renderPassBegin.renderArea.extent = swapchainData.
extent;
119 renderPassBegin.clearValueCount = clearValues.size();
120 renderPassBegin.pClearValues = clearValues.data();
123 vkCmdBeginRenderPass(handle, &renderPassBegin, VK_SUBPASS_CONTENTS_INLINE);
126 void CommandBuffer::endRenderPass() {
127 vkCmdEndRenderPass(handle);
131 vkCmdBindPipeline(handle, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline->
getHandle());
134 void CommandBuffer::bindVertexBuffer(
VertexBuffer* vertexBuffer, uint32_t binding) {
135 VkDeviceSize offset = 0;
136 VkBuffer vertexBufferHandle = vertexBuffer->
getHandle();
137 vkCmdBindVertexBuffers(handle, binding, 1, &vertexBufferHandle, &offset);
142 VkDescriptorSet descriptorSetHandle = descriptorSet->
getHandle();
143 vkCmdBindDescriptorSets(handle, VK_PIPELINE_BIND_POINT_GRAPHICS, layout, 0, 1, &descriptorSetHandle, 0,
nullptr);
146 void CommandBuffer::draw(uint32_t vertexCount) {
147 vkCmdDraw(handle, vertexCount, 1, 0, 0);
150 void CommandBuffer::submit() {
151 VkQueue queue = VulkanContext::getCurrent()->getDevice()->getQueues().graphics;
152 VkFence fence = VulkanContext::getCurrent()->getRenderFence()->getHandle();
155 VkSubmitInfo submitInfo = {};
156 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
157 submitInfo.commandBufferCount = 1;
158 submitInfo.pCommandBuffers = &handle;
161 if (vkQueueSubmit(queue, 1, &submitInfo, fence) != VK_SUCCESS) {
162 throw runtime_error(
"Could not submit the draw command buffer!");
VkRenderPass getHandle()
Returns the handle of the render pass.
This struct stores relevant queue family indices.
VkBuffer getHandle()
Returns the handle of the vertex buffer.
VkPipeline getHandle()
Returns the handle of the graphics pipeline.
Generic namespace for the SimpleGL framework.
VkExtent2D extent
The swapchain extent.
This class wraps a Vulkan descriptor set.
This struct stores relevant swapchain data.
This class wraps a Vulkan vertex buffer.
VkDescriptorSet getHandle()
Returns the handle of the descriptor set.
This class wraps a Vulkan graphics pipeline.
This class wraps a Vulkan render pass.
VkPipelineLayout getPipelineLayout()
Returns the pipeline layout of the graphics pipeline.
int32_t graphics
The index of the graphics queue family.