diff --git a/Core/Core/RenderEngine.cpp b/Core/Core/RenderEngine.cpp index e86e6de..9db638e 100644 --- a/Core/Core/RenderEngine.cpp +++ b/Core/Core/RenderEngine.cpp @@ -363,6 +363,37 @@ void RenderEngine::create_image_views() } } +void RenderEngine::create_render_pass() +{ + VkAttachmentDescription attachment_description{}; + attachment_description.format = swapchain_image_format_; + attachment_description.samples = VK_SAMPLE_COUNT_1_BIT; + attachment_description.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR; + attachment_description.storeOp = VK_ATTACHMENT_STORE_OP_STORE; + attachment_description.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE; + attachment_description.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE; + attachment_description.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED; + attachment_description.finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR; + + VkAttachmentReference attachment_reference{}; + attachment_reference.attachment = 0; + attachment_reference.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL; + + VkSubpassDescription subpass_description{}; + subpass_description.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS; + subpass_description.colorAttachmentCount = 1; + subpass_description.pColorAttachments = &attachment_reference; + + VkRenderPassCreateInfo render_pass_info{}; + render_pass_info.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO; + render_pass_info.attachmentCount = 1; + render_pass_info.pAttachments = &attachment_description; + render_pass_info.subpassCount = 1; + render_pass_info.pSubpasses = &subpass_description; + + VK_CHECK(vkCreateRenderPass(device_, &render_pass_info, nullptr, &render_pass_)); +} + void RenderEngine::create_graphycs_pipleline() { std::ifstream vertex_shader_file("Shaders/vertex.spv", std::ios::binary); @@ -389,10 +420,10 @@ void RenderEngine::create_graphycs_pipleline() vertex_shader_stage_info.pName = "main"; VkPipelineShaderStageCreateInfo fragment_shader_stage_info{}; - vertex_shader_stage_info.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO; - vertex_shader_stage_info.stage = VK_SHADER_STAGE_FRAGMENT_BIT; - vertex_shader_stage_info.module = fragment_shader; - vertex_shader_stage_info.pName = "main"; + fragment_shader_stage_info.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO; + fragment_shader_stage_info.stage = VK_SHADER_STAGE_FRAGMENT_BIT; + fragment_shader_stage_info.module = fragment_shader; + fragment_shader_stage_info.pName = "main"; VkPipelineShaderStageCreateInfo shader_stages[] = {vertex_shader_stage_info, fragment_shader_stage_info}; const std::vector dynamic_states = { @@ -446,6 +477,11 @@ void RenderEngine::create_graphycs_pipleline() rasterization_info.frontFace = VK_FRONT_FACE_CLOCKWISE; rasterization_info.depthBiasEnable = VK_FALSE; + VkPipelineMultisampleStateCreateInfo multisample_info{}; + multisample_info.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO; + multisample_info.sampleShadingEnable = VK_FALSE; + multisample_info.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT; + VkPipelineColorBlendAttachmentState color_blend_attachment_state{}; color_blend_attachment_state.colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT; color_blend_attachment_state.blendEnable = VK_TRUE; @@ -460,6 +496,7 @@ void RenderEngine::create_graphycs_pipleline() color_blend_info.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO; color_blend_info.logicOpEnable = VK_FALSE; color_blend_info.attachmentCount = 1; + color_blend_info.pAttachments = &color_blend_attachment_state; color_blend_info.blendConstants[0] = 0.0f; color_blend_info.blendConstants[1] = 0.0f; color_blend_info.blendConstants[2] = 0.0f; @@ -469,6 +506,26 @@ void RenderEngine::create_graphycs_pipleline() pipeline_layout_info.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO; VK_CHECK(vkCreatePipelineLayout(device_, &pipeline_layout_info, nullptr, &pipeline_layout_)); + + VkGraphicsPipelineCreateInfo graphics_pipeline_info{}; + graphics_pipeline_info.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO; + graphics_pipeline_info.stageCount = 2; + graphics_pipeline_info.pStages = shader_stages; + graphics_pipeline_info.layout = pipeline_layout_; + graphics_pipeline_info.pColorBlendState = &color_blend_info; + graphics_pipeline_info.pDynamicState = &dynamic_state_info; + graphics_pipeline_info.pInputAssemblyState = &input_assembly_info; + graphics_pipeline_info.pMultisampleState = &multisample_info; + graphics_pipeline_info.pRasterizationState = &rasterization_info; + graphics_pipeline_info.pViewportState = &viewport_info; + graphics_pipeline_info.pVertexInputState = &vertex_input_info; + graphics_pipeline_info.pDepthStencilState = nullptr; + graphics_pipeline_info.renderPass = render_pass_; + graphics_pipeline_info.subpass = 0; + graphics_pipeline_info.basePipelineHandle = nullptr; + graphics_pipeline_info.basePipelineIndex = -1; + + VK_CHECK(vkCreateGraphicsPipelines(device_, nullptr, 1, &graphics_pipeline_info, nullptr, &graphycs_pipeline_)); vkDestroyShaderModule(device_, vertex_shader, nullptr); vkDestroyShaderModule(device_, fragment_shader, nullptr); @@ -492,13 +549,20 @@ window_(window) create_logical_device(); create_swapchain(desired_present_mode); create_image_views(); + create_render_pass(); create_graphycs_pipleline(); } RenderEngine::~RenderEngine() { + if (graphycs_pipeline_ != VK_NULL_HANDLE) + vkDestroyPipeline(device_, graphycs_pipeline_, nullptr); + if (pipeline_layout_ != VK_NULL_HANDLE) vkDestroyPipelineLayout(device_, pipeline_layout_, nullptr); + + if (render_pass_ != VK_NULL_HANDLE) + vkDestroyRenderPass(device_, render_pass_, nullptr); for (auto image_view : swapchain_image_views_) vkDestroyImageView(device_, image_view, nullptr); diff --git a/Core/Core/RenderEngine.h b/Core/Core/RenderEngine.h index 17cc08f..aac5ed3 100644 --- a/Core/Core/RenderEngine.h +++ b/Core/Core/RenderEngine.h @@ -48,7 +48,9 @@ class RenderEngine VkFormat swapchain_image_format_; VkExtent2D swapchain_extent_; std::vector swapchain_image_views_; + VkRenderPass render_pass_ = VK_NULL_HANDLE; VkPipelineLayout pipeline_layout_ = VK_NULL_HANDLE; + VkPipeline graphycs_pipeline_ = VK_NULL_HANDLE; #ifdef _DEBUG VkResult enable_layer_validation(VkDebugUtilsMessengerCreateInfoEXT* create_info); @@ -71,6 +73,7 @@ class RenderEngine void create_logical_device(); void create_swapchain(VkPresentModeKHR desired_present_mode); void create_image_views(); + void create_render_pass(); void create_graphycs_pipleline(); public: // Can throw the exception