1 #include <kiba/renderer/vulkan/pipeline.h>
3 #include <kiba/renderer/vulkan/shader.h>
4 #include <kiba/renderer/vulkan/vertex_buffer.h>
7 if ((context->pipeline.shaders = array_create(VkPipelineShaderStageCreateInfo, 2, &context->alloc.kiba_alloc))
9 KB_ERROR(
"could not create array to hold shader info");
12 if (!vulkan_shader_create(context,
"assets/shaders/builtin.test-shader.vert.spv", SHADER_TYPE_VERTEX)) {
13 KB_ERROR(
"could not load vertex shader code");
16 if (!vulkan_shader_create(context,
"assets/shaders/builtin.test-shader.frag.spv", SHADER_TYPE_FRAGMENT)) {
17 KB_ERROR(
"could not load fragment shader code");
21 VkDynamicState dynamic_states[] = {
22 VK_DYNAMIC_STATE_VIEWPORT,
23 VK_DYNAMIC_STATE_SCISSOR,
27 VkPipelineDynamicStateCreateInfo dynamic_state_info = {
28 .sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO,
29 .dynamicStateCount = 2,
30 .pDynamicStates = dynamic_states,
34 VkVertexInputBindingDescription input_binding_description = {
36 .inputRate = VK_VERTEX_INPUT_RATE_VERTEX,
39 VkVertexInputAttributeDescription attribute_descriptions[3] = {
43 .format = VK_FORMAT_R32G32B32_SFLOAT,
49 .format = VK_FORMAT_R32G32B32_SFLOAT,
50 .offset =
sizeof(float) * 3,
55 .format = VK_FORMAT_R32G32_SFLOAT,
56 .offset =
sizeof(float) * 6,
59 VkPipelineVertexInputStateCreateInfo vertex_input_info = {
60 .sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO,
61 .vertexBindingDescriptionCount = 1,
62 .pVertexBindingDescriptions = &input_binding_description,
63 .vertexAttributeDescriptionCount =
sizeof(attribute_descriptions) /
sizeof(attribute_descriptions[0]),
64 .pVertexAttributeDescriptions = attribute_descriptions,
68 VkPipelineInputAssemblyStateCreateInfo input_assembly_info = {
69 .sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO,
70 .topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST,
71 .primitiveRestartEnable = VK_FALSE,
87 VkPipelineViewportStateCreateInfo viewport_state_info = {
88 .sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO,
94 VkPipelineRasterizationStateCreateInfo rasterizer_info = {
95 .sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO,
96 .depthClampEnable = VK_FALSE,
97 .rasterizerDiscardEnable = VK_FALSE,
98 .polygonMode = VK_POLYGON_MODE_FILL,
100 .cullMode = VK_CULL_MODE_BACK_BIT,
101 .frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE,
102 .depthBiasEnable = VK_FALSE,
103 .depthBiasConstantFactor = 0.0f,
104 .depthBiasClamp = 0.0f,
105 .depthBiasSlopeFactor = 0.0f,
109 VkPipelineMultisampleStateCreateInfo multisampling_info = {
110 .sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO,
111 .sampleShadingEnable = VK_FALSE,
112 .rasterizationSamples = VK_SAMPLE_COUNT_1_BIT,
113 .minSampleShading = 1.0f,
115 .alphaToCoverageEnable = VK_FALSE,
116 .alphaToOneEnable = VK_FALSE,
120 VkPipelineColorBlendAttachmentState color_blend_attachment = {
122 VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT,
123 .blendEnable = VK_FALSE,
124 .srcColorBlendFactor = VK_BLEND_FACTOR_ONE,
125 .dstColorBlendFactor = VK_BLEND_FACTOR_ZERO,
126 .colorBlendOp = VK_BLEND_OP_ADD,
127 .srcAlphaBlendFactor = VK_BLEND_FACTOR_ONE,
128 .dstAlphaBlendFactor = VK_BLEND_FACTOR_ZERO,
129 .alphaBlendOp = VK_BLEND_OP_ADD,
131 VkPipelineColorBlendStateCreateInfo color_blend_info = {
132 .sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO,
133 .logicOpEnable = VK_FALSE,
134 .logicOp = VK_LOGIC_OP_COPY,
135 .attachmentCount = 1,
136 .pAttachments = &color_blend_attachment,
137 .blendConstants = {0.0f, 0.0f, 0.0f, 0.0f},
141 VkPipelineLayoutCreateInfo pipeline_layout_info = {
142 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
144 .pSetLayouts = &context->pipeline.descriptor_set_layout,
145 .pushConstantRangeCount = 0,
146 .pPushConstantRanges = NULL,
148 VK_CALL_B8(vkCreatePipelineLayout(context->device.logical,
149 &pipeline_layout_info,
150 &context->alloc.vulkan_callbacks,
151 &context->pipeline.layout));
153 VkPipelineDepthStencilStateCreateInfo depth_info = {
154 .sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO,
155 .depthTestEnable = VK_TRUE,
156 .depthWriteEnable = VK_TRUE,
157 .depthCompareOp = VK_COMPARE_OP_LESS,
158 .depthBoundsTestEnable = VK_FALSE,
159 .minDepthBounds = 0.f,
160 .maxDepthBounds = 1.f,
161 .stencilTestEnable = VK_FALSE,
164 VkGraphicsPipelineCreateInfo pipeline_info = {
165 .sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO,
167 .pStages = context->pipeline.shaders,
168 .pVertexInputState = &vertex_input_info,
169 .pInputAssemblyState = &input_assembly_info,
170 .pViewportState = &viewport_state_info,
171 .pRasterizationState = &rasterizer_info,
172 .pMultisampleState = &multisampling_info,
173 .pDepthStencilState = &depth_info,
174 .pColorBlendState = &color_blend_info,
175 .pDynamicState = &dynamic_state_info,
176 .layout = context->pipeline.layout,
177 .renderPass = context->pipeline.renderpass,
179 .basePipelineHandle = VK_NULL_HANDLE,
180 .basePipelineIndex = -1,
182 VK_CALL_B8(vkCreateGraphicsPipelines(context->device.logical,
186 &context->alloc.vulkan_callbacks,
187 &context->pipeline.graphics_pipeline));
193 vkDestroyPipeline(context->device.logical, context->pipeline.graphics_pipeline, &context->alloc.vulkan_callbacks);
194 vkDestroyPipelineLayout(context->device.logical, context->pipeline.layout, &context->alloc.vulkan_callbacks);
195 array_for_each(VkPipelineShaderStageCreateInfo, shader_create_info, context->pipeline.shaders) {
196 vkDestroyShaderModule(context->device.logical, shader_create_info->module, &context->alloc.vulkan_callbacks);
198 array_destroy(&context->pipeline.shaders);
#define KB_NULL
Value of an invalid ptr (nullptr).
#define KB_ERROR(...)
Log entry with error log level.