Added better failure logging, added create hex pipeline
							parent
							
								
									40d31430c2
								
							
						
					
					
						commit
						62d17bee4a
					
				| @ -0,0 +1,17 @@ | ||||
| #ifndef HEX_H | ||||
| #define HEX_H | ||||
| 
 | ||||
| #include "gpu.h" | ||||
| 
 | ||||
| typedef struct HexPushStruct { | ||||
|   VkDeviceAddress context; | ||||
|   VkDeviceAddress world; | ||||
| } HexPush; | ||||
| 
 | ||||
| VkResult create_hex_pipeline( | ||||
|   VkDevice device, | ||||
|   VkRenderPass render_pass, | ||||
|   GraphicsPipeline* graphics, | ||||
|   ComputePipeline* compute); | ||||
| 
 | ||||
| #endif | ||||
| @ -0,0 +1,201 @@ | ||||
| #include "hex.h" | ||||
| #include "gpu.h" | ||||
| #include "vulkan/vulkan_core.h" | ||||
| 
 | ||||
| VkResult create_hex_pipeline( | ||||
|   VkDevice device, | ||||
|   VkRenderPass render_pass, | ||||
|   GraphicsPipeline* graphics, | ||||
|   ComputePipeline* compute) { | ||||
|   VkResult result; | ||||
|    | ||||
|   // Compute Pipeline
 | ||||
| 
 | ||||
|   VkShaderModule compute_shader = load_shader_file("shader/hex.comp.spv", device); | ||||
|   if(compute_shader == VK_NULL_HANDLE) { | ||||
|     return VK_ERROR_UNKNOWN; | ||||
|   } | ||||
| 
 | ||||
|   VkPipelineShaderStageCreateInfo compute_shader_stage = { | ||||
|     .sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO, | ||||
|     .stage = VK_SHADER_STAGE_COMPUTE_BIT, | ||||
|     .pName = "main", | ||||
|     .module = compute_shader, | ||||
|   }; | ||||
| 
 | ||||
|   VkPushConstantRange push_constant = { | ||||
|     .size = sizeof(HexPush), | ||||
|     .offset = 0, | ||||
|     .stageFlags = VK_SHADER_STAGE_COMPUTE_BIT | VK_SHADER_STAGE_VERTEX_BIT, | ||||
|   }; | ||||
| 
 | ||||
|   VkPipelineLayoutCreateInfo compute_layout_info = { | ||||
|     .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO, | ||||
|     .pPushConstantRanges = &push_constant, | ||||
|     .pushConstantRangeCount = 1, | ||||
|   }; | ||||
| 
 | ||||
|   VK_RESULT(vkCreatePipelineLayout(device, &compute_layout_info, NULL, &compute->layout)); | ||||
| 
 | ||||
|   VkComputePipelineCreateInfo compute_pipeline_info = { | ||||
|     .sType = VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO, | ||||
|     .stage = compute_shader_stage, | ||||
|     .layout = compute->layout, | ||||
|   }; | ||||
| 
 | ||||
|   VK_RESULT(vkCreateComputePipelines(device, VK_NULL_HANDLE, 1, &compute_pipeline_info, NULL, &compute->pipeline)); | ||||
| 
 | ||||
|   // Graphics Pipeline
 | ||||
|    | ||||
|   VkShaderModule vert_shader = load_shader_file("shader/hex.vert.spv", device); | ||||
|   VkShaderModule frag_shader = load_shader_file("shader/hex.frag.spv", device); | ||||
| 
 | ||||
|   VkPipelineShaderStageCreateInfo graphics_stages[] = { | ||||
|     { | ||||
|       .sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO, | ||||
|       .stage = VK_SHADER_STAGE_VERTEX_BIT, | ||||
|       .pName = "main", | ||||
|       .module = vert_shader, | ||||
|     }, | ||||
|     { | ||||
|       .sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO, | ||||
|       .stage = VK_SHADER_STAGE_FRAGMENT_BIT, | ||||
|       .pName = "main", | ||||
|       .module = frag_shader, | ||||
|     }, | ||||
|   }; | ||||
| 
 | ||||
|   VkPipelineLayoutCreateInfo graphics_layout_info = { | ||||
|     .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO, | ||||
|     .pPushConstantRanges = &push_constant, | ||||
|     .pushConstantRangeCount = 1, | ||||
|   }; | ||||
| 
 | ||||
|   VK_RESULT(vkCreatePipelineLayout(device, &graphics_layout_info, NULL, &graphics->layout)); | ||||
| 
 | ||||
|   VkPipelineVertexInputStateCreateInfo vertex_info = { | ||||
|     .sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO, | ||||
|   }; | ||||
| 
 | ||||
|   VkPipelineInputAssemblyStateCreateInfo input_info = { | ||||
|     .sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO, | ||||
|     .topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST, | ||||
|     .primitiveRestartEnable = VK_FALSE, | ||||
|   }; | ||||
| 
 | ||||
|   VkViewport viewport = { | ||||
|     .x = 0.0f, | ||||
|     .y = 0.0f, | ||||
|     .width = (float)(100), | ||||
|     .height = (float)(100), | ||||
|     .minDepth = 0.0f, | ||||
|     .maxDepth = 1.0f, | ||||
|   }; | ||||
| 
 | ||||
|   VkRect2D scissor = { | ||||
|     .offset = { | ||||
|       .x = 0, | ||||
|       .y = 0, | ||||
|     }, | ||||
|     .extent = { | ||||
|       .width = 100, | ||||
|       .height = 100, | ||||
|     }, | ||||
|   }; | ||||
| 
 | ||||
|   VkPipelineViewportStateCreateInfo viewport_info = { | ||||
|     .sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO, | ||||
|     .viewportCount = 1, | ||||
|     .pViewports = &viewport, | ||||
|     .scissorCount = 1, | ||||
|     .pScissors = &scissor, | ||||
|   }; | ||||
| 
 | ||||
|   VkPipelineRasterizationStateCreateInfo raster_info = { | ||||
|     .sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO, | ||||
|     .depthClampEnable = VK_TRUE, | ||||
|     .rasterizerDiscardEnable = VK_FALSE, | ||||
|     .polygonMode = VK_POLYGON_MODE_FILL, | ||||
|     .lineWidth = 1.0f, | ||||
|     .cullMode = VK_CULL_MODE_BACK_BIT, | ||||
|     .frontFace = VK_FRONT_FACE_CLOCKWISE, | ||||
|     .depthBiasEnable = VK_FALSE, | ||||
|     .depthBiasConstantFactor = 0.0f, | ||||
|     .depthBiasClamp = 0.0f, | ||||
|     .depthBiasSlopeFactor = 0.0f, | ||||
|   }; | ||||
| 
 | ||||
|   VkPipelineColorBlendAttachmentState blend_attachments = { | ||||
|     .colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT, | ||||
|     .blendEnable = VK_TRUE, | ||||
|     .srcColorBlendFactor = VK_BLEND_FACTOR_SRC_ALPHA, | ||||
|     .dstColorBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA, | ||||
|     .colorBlendOp = VK_BLEND_OP_ADD, | ||||
|     .srcAlphaBlendFactor = VK_BLEND_FACTOR_ZERO, | ||||
|     .dstAlphaBlendFactor = VK_BLEND_FACTOR_ZERO, | ||||
|     .alphaBlendOp = VK_BLEND_OP_ADD, | ||||
|   }; | ||||
| 
 | ||||
|   VkPipelineColorBlendStateCreateInfo blend_info = { | ||||
|     .sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO, | ||||
|     .logicOpEnable = VK_FALSE, | ||||
|     .logicOp = VK_LOGIC_OP_COPY, | ||||
|     .attachmentCount = 1, | ||||
|     .pAttachments = &blend_attachments, | ||||
|   }; | ||||
| 
 | ||||
|   VkDynamicState dynamic_states[] = { | ||||
|     VK_DYNAMIC_STATE_VIEWPORT, | ||||
|     VK_DYNAMIC_STATE_SCISSOR, | ||||
|   }; | ||||
| 
 | ||||
|   VkPipelineDynamicStateCreateInfo dynamic_info = { | ||||
|     .sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO, | ||||
|     .dynamicStateCount = sizeof(dynamic_states)/sizeof(VkDynamicState), | ||||
|     .pDynamicStates = dynamic_states, | ||||
|   }; | ||||
| 
 | ||||
|   VkPipelineMultisampleStateCreateInfo multisample_info = { | ||||
|     .sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO, | ||||
|     .sampleShadingEnable = VK_FALSE, | ||||
|     .rasterizationSamples = VK_SAMPLE_COUNT_1_BIT, | ||||
|     .minSampleShading = 1.0f, | ||||
|     .pSampleMask = 0, | ||||
|     .alphaToCoverageEnable = VK_FALSE, | ||||
|     .alphaToOneEnable = VK_FALSE, | ||||
|   }; | ||||
| 
 | ||||
|   VkPipelineDepthStencilStateCreateInfo depth_info = { | ||||
|     .sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO, | ||||
|     .depthTestEnable = VK_TRUE, | ||||
|     .depthWriteEnable = VK_TRUE, | ||||
|     .depthCompareOp = VK_COMPARE_OP_LESS, | ||||
|     .depthBoundsTestEnable = VK_TRUE, | ||||
|     .minDepthBounds = 0.0f, | ||||
|     .maxDepthBounds = 1.0f, | ||||
|     .stencilTestEnable = VK_FALSE, | ||||
|   }; | ||||
| 
 | ||||
|   VkGraphicsPipelineCreateInfo graphics_pipeline_info = { | ||||
|     .sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO, | ||||
|     .layout = graphics->layout, | ||||
|     .stageCount = sizeof(graphics_stages)/sizeof(VkPipelineShaderStageCreateInfo), | ||||
|     .pStages = graphics_stages, | ||||
|     .pVertexInputState = &vertex_info, | ||||
|     .pInputAssemblyState = &input_info, | ||||
|     .pViewportState = &viewport_info, | ||||
|     .pRasterizationState = &raster_info, | ||||
|     .pColorBlendState = &blend_info, | ||||
|     .pDynamicState = &dynamic_info, | ||||
|     .pMultisampleState = &multisample_info, | ||||
|     .pDepthStencilState = &depth_info, | ||||
|     .renderPass = render_pass, | ||||
|     .subpass = 0, | ||||
|     .basePipelineHandle = VK_NULL_HANDLE, | ||||
|     .basePipelineIndex = -1, | ||||
|   }; | ||||
| 
 | ||||
|   VK_RESULT(vkCreateGraphicsPipelines(device, VK_NULL_HANDLE, 1, &graphics_pipeline_info, NULL, &graphics->pipeline)); | ||||
| 
 | ||||
|   return VK_SUCCESS; | ||||
| } | ||||
		Loading…
	
		Reference in New Issue