Completed command buffer chapter
This commit is contained in:
parent
9e446a579f
commit
41056d6026
1 changed files with 91 additions and 5 deletions
96
src/main.c
96
src/main.c
|
|
@ -60,6 +60,8 @@ typedef struct Application {
|
|||
VkPipeline graphicsPipeline;
|
||||
VkFramebuffer *swapChainFramebuffers;
|
||||
uint32_t swapChainFramebufferCount;
|
||||
VkCommandPool commandPool;
|
||||
VkCommandBuffer commandBuffer;
|
||||
} Application;
|
||||
|
||||
typedef struct SwapChainSupportDetails {
|
||||
|
|
@ -737,11 +739,6 @@ void createGraphicsPipeline(Application *app) {
|
|||
viewportState.viewportCount = 1;
|
||||
viewportState.scissorCount = 1;
|
||||
|
||||
VkRect2D scissor = {0};
|
||||
scissor.offset.x = 0;
|
||||
scissor.offset.y = 0;
|
||||
scissor.extent = app->swapChainExtent;
|
||||
|
||||
VkPipelineRasterizationStateCreateInfo rasterizer = {0};
|
||||
rasterizer.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
|
||||
rasterizer.depthClampEnable = VK_FALSE;
|
||||
|
|
@ -888,6 +885,92 @@ void createFramebuffers(Application *app) {
|
|||
}
|
||||
}
|
||||
|
||||
void CreateCommandPool(Application *app) {
|
||||
QueueFamilyIndices queueFamilyIndices =
|
||||
findQueueFamilies(app->physicalDevice, &app->surface);
|
||||
|
||||
VkCommandPoolCreateInfo poolInfo = {0};
|
||||
poolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
|
||||
poolInfo.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
|
||||
poolInfo.queueFamilyIndex = queueFamilyIndices.graphicsFamily;
|
||||
|
||||
if (vkCreateCommandPool(app->device, &poolInfo, NULL, &app->commandPool) !=
|
||||
VK_SUCCESS) {
|
||||
fprintf(stderr, "Failed to create command pool!");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
void recordCommandBuffer(Application *app, VkCommandBuffer commandBuffer,
|
||||
uint32_t imageIndex) {
|
||||
VkCommandBufferBeginInfo beginInfo = {0};
|
||||
beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
|
||||
beginInfo.flags = 0; // Optional
|
||||
beginInfo.pInheritanceInfo = NULL; // Optional
|
||||
|
||||
if (vkBeginCommandBuffer(commandBuffer, &beginInfo) != VK_SUCCESS) {
|
||||
fprintf(stderr, "Failed to begin recording command buffer!");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
VkRenderPassBeginInfo renderPassInfo = {0};
|
||||
renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
|
||||
renderPassInfo.renderPass = app->renderPass;
|
||||
renderPassInfo.framebuffer = app->swapChainFramebuffers[imageIndex];
|
||||
|
||||
renderPassInfo.renderArea.offset.x = 0;
|
||||
renderPassInfo.renderArea.offset.y = 0;
|
||||
renderPassInfo.renderArea.extent = app->swapChainExtent;
|
||||
|
||||
VkClearValue clearColor = {{{0.0f, 0.0f, 0.0f, 1.0f}}};
|
||||
renderPassInfo.clearValueCount = 1;
|
||||
renderPassInfo.pClearValues = &clearColor;
|
||||
|
||||
vkCmdBeginRenderPass(commandBuffer, &renderPassInfo,
|
||||
VK_SUBPASS_CONTENTS_INLINE);
|
||||
|
||||
vkCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS,
|
||||
app->graphicsPipeline);
|
||||
|
||||
VkViewport viewport = {0};
|
||||
viewport.x = 0.0f;
|
||||
viewport.y = 0.0f;
|
||||
viewport.width = (float)app->swapChainExtent.width;
|
||||
viewport.height = (float)app->swapChainExtent.height;
|
||||
viewport.minDepth = 0.0f;
|
||||
viewport.maxDepth = 1.0f;
|
||||
vkCmdSetViewport(commandBuffer, 0, 1, &viewport);
|
||||
|
||||
VkRect2D scissor = {0};
|
||||
scissor.offset.x = 0;
|
||||
scissor.offset.y = 0;
|
||||
scissor.extent = app->swapChainExtent;
|
||||
vkCmdSetScissor(commandBuffer, 0, 1, &scissor);
|
||||
|
||||
vkCmdDraw(commandBuffer, 3, 1, 0, 0);
|
||||
|
||||
vkCmdEndRenderPass(commandBuffer);
|
||||
|
||||
if (vkEndCommandBuffer(commandBuffer) != VK_SUCCESS) {
|
||||
fprintf(stderr, "Failed to record command buffer!");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
void createCommandBuffer(Application *app) {
|
||||
VkCommandBufferAllocateInfo allocInfo = {0};
|
||||
allocInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
|
||||
allocInfo.commandPool = app->commandPool;
|
||||
allocInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
|
||||
allocInfo.commandBufferCount = 1;
|
||||
|
||||
if (vkAllocateCommandBuffers(app->device, &allocInfo, &app->commandBuffer) !=
|
||||
VK_SUCCESS) {
|
||||
fprintf(stderr, "Failed to allocate command buffers!");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
void initVulkan(Application *app) {
|
||||
createInstance(app);
|
||||
setupDebugMessenger(app);
|
||||
|
|
@ -899,6 +982,8 @@ void initVulkan(Application *app) {
|
|||
createRenderPass(app);
|
||||
createGraphicsPipeline(app);
|
||||
createFramebuffers(app);
|
||||
CreateCommandPool(app);
|
||||
createCommandBuffer(app);
|
||||
}
|
||||
|
||||
void mainLoop(Application *app) {
|
||||
|
|
@ -908,6 +993,7 @@ void mainLoop(Application *app) {
|
|||
}
|
||||
|
||||
void cleanup(Application *app) {
|
||||
vkDestroyCommandPool(app->device, app->commandPool, NULL);
|
||||
for (uint i = 0; i < app->swapChainFramebufferCount; i++) {
|
||||
vkDestroyFramebuffer(app->device, app->swapChainFramebuffers[i], NULL);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue