Browse Source

added pipeline class

Jef Belmans 2 years ago
parent
commit
921d5442bb

+ 113 - 4
coral_renderer/coral_pipeline.cpp

@@ -1,7 +1,11 @@
 #include "coral_pipeline.h"
 
+// STD
 #include <iostream>
 #include <fstream>
+#include <cassert>
+
+#include "vk_initializers.h"
 
 using namespace coral_3d;
 
@@ -14,9 +18,64 @@ coral_pipeline::coral_pipeline(
 	create_graphics_pipeline(vert_file_path, frag_file_path, config_info);
 }
 
+coral_pipeline::~coral_pipeline()
+{
+	vkDestroyShaderModule(device_.device(), vert_shader_module_, nullptr);
+	vkDestroyShaderModule(device_.device(), frag_shader_module_, nullptr);
+	vkDestroyPipeline(device_.device(), graphics_pipeline_, nullptr);
+}
+
 PipelineConfigInfo coral_3d::coral_pipeline::default_pipeline_config_info(uint32_t width, uint32_t height)
 {
 	PipelineConfigInfo config_info{};
+
+	// IA
+	config_info.input_assembly_info = vkinit::input_assembly_ci(VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST);
+
+	// VIEWPORT
+	config_info.viewport.x = 0.f;
+	config_info.viewport.y = 0.f;
+	config_info.viewport.width = static_cast<float>(width);
+	config_info.viewport.height = static_cast<float>(height);
+	config_info.viewport.minDepth = 0.f;
+	config_info.viewport.maxDepth = 1.f;
+
+	// SCISSOR
+	config_info.scissor.offset = { 0,0 };
+	config_info.scissor.extent = {width, height};
+
+	// VIEWPORT INFO
+	config_info.viewport_info.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
+	config_info.viewport_info.pNext = nullptr;
+	config_info.viewport_info.viewportCount = 1;
+	config_info.viewport_info.pViewports = &config_info.viewport;
+	config_info.viewport_info.scissorCount = 1;
+	config_info.viewport_info.pScissors = &config_info.scissor;
+
+	// RASTERIZER
+	config_info.rasterization_info = vkinit::rasterization_state_ci(VK_POLYGON_MODE_FILL);
+
+	// MULTISAMPLER
+	config_info.multisample_info = vkinit::multisample_state_ci();
+
+	// BLEND ATTACHMENT
+	config_info.color_blend_attachment = vkinit::color_blend_attachment_state();
+
+	// BLEND INFO
+	config_info.color_blend_info.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
+	config_info.color_blend_info.pNext = nullptr;
+	config_info.color_blend_info.logicOpEnable = VK_FALSE;
+	config_info.color_blend_info.logicOp = VK_LOGIC_OP_COPY;
+	config_info.color_blend_info.attachmentCount = 1;
+	config_info.color_blend_info.pAttachments = &config_info.color_blend_attachment;
+	config_info.color_blend_info.blendConstants[0] = 0.f;
+	config_info.color_blend_info.blendConstants[1] = 0.f;
+	config_info.color_blend_info.blendConstants[2] = 0.f;
+	config_info.color_blend_info.blendConstants[3] = 0.f;
+
+	// DEPTH INFO
+	config_info.depth_stencil_info = vkinit::depth_stencil_ci(true, true, VK_COMPARE_OP_LESS_OR_EQUAL);
+
 	return config_info;
 }
 
@@ -44,11 +103,61 @@ void coral_pipeline::create_graphics_pipeline(
 	const std::string& frag_file_path,
 	const PipelineConfigInfo& config_info)
 {
-	auto vertShader = read_file(vert_file_path);
-	auto fragShader = read_file(frag_file_path);
+	assert(config_info.pipeline_layout != VK_NULL_HANDLE &&
+		"ERROR! coral_pipeline::create_graphics_pipeline() >> no pipeline layout provided in config_info ");
+
+	assert(config_info.render_pass != VK_NULL_HANDLE &&
+		"ERROR! coral_pipeline::create_graphics_pipeline() >> no render pass provided in config_info ");
+
+	auto vert_code = read_file(vert_file_path);
+	auto frag_code = read_file(frag_file_path);
+
+	create_shader_module(vert_code, &vert_shader_module_);
+	create_shader_module(frag_code, &frag_shader_module_);
+
+	VkPipelineShaderStageCreateInfo shader_stages[2];
+
+	shader_stages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
+	shader_stages[0].pNext = nullptr;
+	shader_stages[0].stage = VK_SHADER_STAGE_VERTEX_BIT;
+	shader_stages[0].module = vert_shader_module_;
+	shader_stages[0].pName = "main";
+	shader_stages[0].flags = 0;
+	shader_stages[0].pSpecializationInfo = nullptr;
+
+	shader_stages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
+	shader_stages[1].pNext = nullptr;
+	shader_stages[1].stage = VK_SHADER_STAGE_FRAGMENT_BIT;
+	shader_stages[1].module = frag_shader_module_;
+	shader_stages[1].pName = "main";
+	shader_stages[1].flags = 0;
+	shader_stages[1].pSpecializationInfo = nullptr;
+
+	VkPipelineVertexInputStateCreateInfo vertex_input_info{ vkinit::vertex_input_state_ci() };
+
+	VkGraphicsPipelineCreateInfo pipeline_info{};
+	pipeline_info.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
+	pipeline_info.stageCount = 2;
+	pipeline_info.pStages = shader_stages;
+	pipeline_info.pVertexInputState = &vertex_input_info;
+	pipeline_info.pInputAssemblyState = &config_info.input_assembly_info;
+	pipeline_info.pViewportState = &config_info.viewport_info;
+	pipeline_info.pRasterizationState = &config_info.rasterization_info;
+	pipeline_info.pMultisampleState = &config_info.multisample_info;
+	pipeline_info.pColorBlendState = &config_info.color_blend_info;
+	pipeline_info.pDepthStencilState = &config_info.depth_stencil_info;
+	pipeline_info.pDynamicState = nullptr;
+
+	pipeline_info.layout = config_info.pipeline_layout;
+	pipeline_info.renderPass = config_info.render_pass;
+	pipeline_info.subpass = config_info.subpass;
+
+	pipeline_info.basePipelineIndex = -1;
+	pipeline_info.basePipelineHandle = VK_NULL_HANDLE;
 
-	std::cout << "Vertex shader code size: " << vertShader.size() << "\n";
-	std::cout << "Frag shader code size: " << fragShader.size() << "\n";
+	if (vkCreateGraphicsPipelines(device_.device(), VK_NULL_HANDLE, 1, &pipeline_info, nullptr, &graphics_pipeline_)
+		!= VK_SUCCESS)
+		throw std::runtime_error("ERROR! coral_pipeline::create_graphics_pipeline() >> Failed to create graphics pipeline!");
 }
 
 void coral_3d::coral_pipeline::create_shader_module(const std::vector<char>& code, VkShaderModule* shader_module)

+ 16 - 2
coral_renderer/coral_pipeline.h

@@ -8,7 +8,21 @@
 
 namespace coral_3d
 {
-	struct PipelineConfigInfo{};
+	struct PipelineConfigInfo
+	{
+		VkViewport viewport;
+		VkRect2D scissor;
+		VkPipelineViewportStateCreateInfo viewport_info;
+		VkPipelineInputAssemblyStateCreateInfo input_assembly_info;
+		VkPipelineRasterizationStateCreateInfo rasterization_info;
+		VkPipelineMultisampleStateCreateInfo multisample_info;
+		VkPipelineColorBlendAttachmentState color_blend_attachment;
+		VkPipelineColorBlendStateCreateInfo color_blend_info;
+		VkPipelineDepthStencilStateCreateInfo depth_stencil_info;
+		VkPipelineLayout pipeline_layout = nullptr;
+		VkRenderPass render_pass = nullptr;
+		uint32_t subpass = 0;
+	};
 
 	class coral_pipeline final
 	{
@@ -19,7 +33,7 @@ namespace coral_3d
 			const std::string& frag_file_path,
 			const PipelineConfigInfo& config_info);
 
-		~coral_pipeline() {};
+		~coral_pipeline();
 
 		coral_pipeline(const coral_pipeline&) = delete;
 		coral_pipeline& operator=(const coral_pipeline&) = delete;

+ 4 - 0
coral_renderer/vk_initializers.cpp

@@ -108,6 +108,9 @@ VkPipelineVertexInputStateCreateInfo vkinit::vertex_input_state_ci()
 	info.vertexBindingDescriptionCount = 0;
 	info.vertexAttributeDescriptionCount = 0;
 
+	info.pVertexAttributeDescriptions = nullptr;
+	info.pVertexBindingDescriptions = nullptr;
+
 	return info;
 }
 
@@ -172,6 +175,7 @@ VkPipelineMultisampleStateCreateInfo vkinit::multisample_state_ci()
 VkPipelineColorBlendAttachmentState vkinit::color_blend_attachment_state()
 {
 	VkPipelineColorBlendAttachmentState info{};
+
 	info.colorWriteMask = 
 		VK_COLOR_COMPONENT_R_BIT |
 		VK_COLOR_COMPONENT_G_BIT |

BIN
out/build/x64-Debug/.ninja_deps


+ 10 - 0
out/build/x64-Debug/.ninja_log

@@ -44,3 +44,13 @@
 1439	1642	7101063204872836	coral_renderer/coral_renderer.exe	20dbc57cca0c9133
 12	1293	7101063541931709	coral_renderer/CMakeFiles/coral_renderer.dir/coral_device.cpp.obj	f65feb738e17e8d7
 1293	1485	7101063543131099	coral_renderer/coral_renderer.exe	20dbc57cca0c9133
+20	383	7101131425879773	coral_renderer/CMakeFiles/coral_renderer.dir/vk_initializers.cpp.obj	e2dbff82d1c3ba0d
+8	952	7101131431560818	coral_renderer/CMakeFiles/coral_renderer.dir/first_app.cpp.obj	2fa3f3bbdc72680a
+4	1199	7101131434017667	coral_renderer/CMakeFiles/coral_renderer.dir/main.cpp.obj	f207d5295f665d92
+12	1228	7101131434324805	coral_renderer/CMakeFiles/coral_renderer.dir/coral_pipeline.cpp.obj	429a0363ba93b7c7
+17	1815	7101131440161744	coral_renderer/CMakeFiles/coral_renderer.dir/coral_device.cpp.obj	f65feb738e17e8d7
+1815	2070	7101131441889449	coral_renderer/coral_renderer.exe	20dbc57cca0c9133
+6	805	7101132283528428	coral_renderer/CMakeFiles/coral_renderer.dir/coral_pipeline.cpp.obj	429a0363ba93b7c7
+806	992	7101132284614039	coral_renderer/coral_renderer.exe	20dbc57cca0c9133
+12	902	7101133893372813	coral_renderer/CMakeFiles/coral_renderer.dir/coral_pipeline.cpp.obj	429a0363ba93b7c7
+902	1151	7101133894664193	coral_renderer/coral_renderer.exe	20dbc57cca0c9133

BIN
out/build/x64-Debug/coral_renderer/CMakeFiles/coral_renderer.dir/vc140.pdb


BIN
out/build/x64-Debug/coral_renderer/coral_renderer.ilk


BIN
out/build/x64-Debug/coral_renderer/coral_renderer.pdb