From 9e446a579feefa1dc8bc73538bd405ee28c3072b Mon Sep 17 00:00:00 2001 From: Warwick Date: Thu, 16 Jan 2025 15:59:34 +0000 Subject: [PATCH] completed up to framebuffers in the vulkan tutorial. --- src/main.c | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/src/main.c b/src/main.c index 083ed0a..400655c 100644 --- a/src/main.c +++ b/src/main.c @@ -57,6 +57,9 @@ typedef struct Application { uint32_t swapChainImageViewCount; VkRenderPass renderPass; VkPipelineLayout pipelineLayout; + VkPipeline graphicsPipeline; + VkFramebuffer *swapChainFramebuffers; + uint32_t swapChainFramebufferCount; } Application; typedef struct SwapChainSupportDetails { @@ -775,6 +778,18 @@ void createGraphicsPipeline(Application *app) { colorBlendAttachment.dstAlphaBlendFactor = VK_BLEND_FACTOR_ZERO; // Optional colorBlendAttachment.alphaBlendOp = VK_BLEND_OP_ADD; // Optional + VkPipelineColorBlendStateCreateInfo colorBlending = {0}; + colorBlending.sType = + VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO; + colorBlending.logicOpEnable = VK_FALSE; + colorBlending.logicOp = VK_LOGIC_OP_COPY; + colorBlending.attachmentCount = 1; + colorBlending.pAttachments = &colorBlendAttachment; + colorBlending.blendConstants[0] = 0.0f; + colorBlending.blendConstants[1] = 0.0f; + colorBlending.blendConstants[2] = 0.0f; + colorBlending.blendConstants[3] = 0.0f; + VkPipelineLayoutCreateInfo pipelineLayoutInfo = {0}; pipelineLayoutInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO; pipelineLayoutInfo.setLayoutCount = 0; // Optional @@ -788,6 +803,28 @@ void createGraphicsPipeline(Application *app) { exit(EXIT_FAILURE); } + VkGraphicsPipelineCreateInfo pipelineInfo = {0}; + pipelineInfo.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO; + pipelineInfo.stageCount = 2; + pipelineInfo.pStages = shaderStages; + pipelineInfo.pVertexInputState = &vertexInputInfo; + pipelineInfo.pInputAssemblyState = &inputAssembly; + pipelineInfo.pViewportState = &viewportState; + pipelineInfo.pRasterizationState = &rasterizer; + pipelineInfo.pMultisampleState = &multisampling; + pipelineInfo.pColorBlendState = &colorBlending; + pipelineInfo.pDynamicState = &dynamicState; + pipelineInfo.layout = app->pipelineLayout; + pipelineInfo.renderPass = app->renderPass; + pipelineInfo.subpass = 0; + pipelineInfo.basePipelineHandle = VK_NULL_HANDLE; + + if (vkCreateGraphicsPipelines(app->device, VK_NULL_HANDLE, 1, &pipelineInfo, + NULL, &app->graphicsPipeline) != VK_SUCCESS) { + fprintf(stderr, "Failed to create graphics pipeline!"); + exit(EXIT_FAILURE); + } + vkDestroyShaderModule(app->device, fragShaderModule, NULL); vkDestroyShaderModule(app->device, vertShaderModule, NULL); } @@ -826,6 +863,31 @@ void createRenderPass(Application *app) { } } +void createFramebuffers(Application *app) { + app->swapChainFramebufferCount = app->swapChainImageViewCount; + app->swapChainFramebuffers = + malloc(app->swapChainFramebufferCount * sizeof(VkFramebuffer)); + + for (size_t i = 0; i < app->swapChainImageViewCount; i++) { + VkImageView attachments[] = {app->swapChainImageViews[i]}; + + VkFramebufferCreateInfo framebufferInfo = {0}; + framebufferInfo.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO; + framebufferInfo.renderPass = app->renderPass; + framebufferInfo.attachmentCount = 1; + framebufferInfo.pAttachments = attachments; + framebufferInfo.width = app->swapChainExtent.width; + framebufferInfo.height = app->swapChainExtent.height; + framebufferInfo.layers = 1; + + if (vkCreateFramebuffer(app->device, &framebufferInfo, NULL, + &app->swapChainFramebuffers[i]) != VK_SUCCESS) { + fprintf(stderr, "Failed to create framebuffer!"); + exit(EXIT_FAILURE); + } + } +} + void initVulkan(Application *app) { createInstance(app); setupDebugMessenger(app); @@ -836,6 +898,7 @@ void initVulkan(Application *app) { createImageViews(app); createRenderPass(app); createGraphicsPipeline(app); + createFramebuffers(app); } void mainLoop(Application *app) { @@ -845,6 +908,11 @@ void mainLoop(Application *app) { } void cleanup(Application *app) { + for (uint i = 0; i < app->swapChainFramebufferCount; i++) { + vkDestroyFramebuffer(app->device, app->swapChainFramebuffers[i], NULL); + } + free(app->swapChainFramebuffers); + vkDestroyPipeline(app->device, app->graphicsPipeline, NULL); vkDestroyPipelineLayout(app->device, app->pipelineLayout, NULL); vkDestroyRenderPass(app->device, app->renderPass, NULL); for (size_t i = 0; i < app->swapChainImageViewCount; i++) {