@@ -28,6 +28,8 @@ var swapChainImageFormat: c.VkFormat = undefined;
28
28
var swapChainExtent : c.VkExtent2D = undefined ;
29
29
var swapChainImageViews : []c.VkImageView = undefined ;
30
30
var renderPass : c.VkRenderPass = undefined ;
31
+ var pipelineLayout : c.VkPipelineLayout = undefined ;
32
+ var graphicsPipeline : c.VkPipeline = undefined ;
31
33
32
34
const QueueFamilyIndices = struct {
33
35
graphicsFamily : ? u32 ,
@@ -82,9 +84,9 @@ pub fn main() !void {
82
84
83
85
while (c .glfwWindowShouldClose (window ) == 0 ) {
84
86
c .glfwPollEvents ();
85
- // drawFrame();
87
+ drawFrame ();
86
88
}
87
- // c.vkDeviceWaitIdle(device );
89
+ try checkSuccess ( c .vkDeviceWaitIdle (global_device ) );
88
90
89
91
std .debug .warn ("TODO port the cleanup function" );
90
92
}
@@ -98,14 +100,209 @@ fn initVulkan(allocator: *Allocator, window: *c.GLFWwindow) !void {
98
100
try createSwapChain (allocator );
99
101
try createImageViews (allocator );
100
102
try createRenderPass ();
103
+ try createGraphicsPipeline (allocator );
101
104
// TODO
102
- //createGraphicsPipeline();
103
105
//createFramebuffers();
104
106
//createCommandPool();
105
107
//createCommandBuffers();
106
108
//createSyncObjects();
107
109
}
108
110
111
+ fn createShaderModule (code : []align (@alignOf (u32 )) const u8 ) ! c.VkShaderModule {
112
+ const createInfo = c.VkShaderModuleCreateInfo {
113
+ .sType = c .VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO ,
114
+ .codeSize = code .len ,
115
+ .pCode = @bytesToSlice (u32 , code ).ptr ,
116
+
117
+ .pNext = null ,
118
+ .flags = 0 ,
119
+ };
120
+
121
+ var shaderModule : c.VkShaderModule = undefined ;
122
+ try checkSuccess (c .vkCreateShaderModule (global_device , & createInfo , null , & shaderModule ));
123
+
124
+ return shaderModule ;
125
+ }
126
+
127
+ fn createGraphicsPipeline (allocator : * Allocator ) ! void {
128
+ const vertShaderCode = try std .io .readFileAllocAligned (allocator , "shaders/vert.spv" , @alignOf (u32 ));
129
+ defer allocator .free (vertShaderCode );
130
+
131
+ const fragShaderCode = try std .io .readFileAllocAligned (allocator , "shaders/frag.spv" , @alignOf (u32 ));
132
+ defer allocator .free (fragShaderCode );
133
+
134
+ const vertShaderModule = try createShaderModule (vertShaderCode );
135
+ const fragShaderModule = try createShaderModule (fragShaderCode );
136
+
137
+ const vertShaderStageInfo = c.VkPipelineShaderStageCreateInfo {
138
+ .sType = c .VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO ,
139
+ .stage = c .VK_SHADER_STAGE_VERTEX_BIT ,
140
+ .module = vertShaderModule ,
141
+ .pName = c"main" ,
142
+
143
+ .pNext = null ,
144
+ .flags = 0 ,
145
+ .pSpecializationInfo = null ,
146
+ };
147
+
148
+ const fragShaderStageInfo = c.VkPipelineShaderStageCreateInfo {
149
+ .sType = c .VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO ,
150
+ .stage = c .VK_SHADER_STAGE_FRAGMENT_BIT ,
151
+ .module = fragShaderModule ,
152
+ .pName = c"main" ,
153
+ .pNext = null ,
154
+ .flags = 0 ,
155
+
156
+ .pSpecializationInfo = null ,
157
+ };
158
+
159
+ const shaderStages = []c.VkPipelineShaderStageCreateInfo { vertShaderStageInfo , fragShaderStageInfo };
160
+
161
+ const vertexInputInfo = c.VkPipelineVertexInputStateCreateInfo {
162
+ .sType = c .VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO ,
163
+ .vertexBindingDescriptionCount = 0 ,
164
+ .vertexAttributeDescriptionCount = 0 ,
165
+
166
+ .pVertexBindingDescriptions = null ,
167
+ .pVertexAttributeDescriptions = null ,
168
+ .pNext = null ,
169
+ .flags = 0 ,
170
+ };
171
+
172
+ const inputAssembly = c.VkPipelineInputAssemblyStateCreateInfo {
173
+ .sType = c .VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO ,
174
+ .topology = c .VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST ,
175
+ .primitiveRestartEnable = c .VK_FALSE ,
176
+ .pNext = null ,
177
+ .flags = 0 ,
178
+ };
179
+
180
+ const viewport = []c.VkViewport {c.VkViewport {
181
+ .x = 0.0 ,
182
+ .y = 0.0 ,
183
+ .width = @intToFloat (f32 , swapChainExtent .width ),
184
+ .height = @intToFloat (f32 , swapChainExtent .height ),
185
+ .minDepth = 0.0 ,
186
+ .maxDepth = 1.0 ,
187
+ }};
188
+
189
+ const scissor = []c.VkRect2D {c.VkRect2D {
190
+ .offset = c.VkOffset2D { .x = 0 , .y = 0 },
191
+ .extent = swapChainExtent ,
192
+ }};
193
+
194
+ const viewportState = c.VkPipelineViewportStateCreateInfo {
195
+ .sType = c .VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO ,
196
+ .viewportCount = 1 ,
197
+ .pViewports = & viewport ,
198
+ .scissorCount = 1 ,
199
+ .pScissors = & scissor ,
200
+
201
+ .pNext = null ,
202
+ .flags = 0 ,
203
+ };
204
+
205
+ const rasterizer = c.VkPipelineRasterizationStateCreateInfo {
206
+ .sType = c .VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO ,
207
+ .depthClampEnable = c .VK_FALSE ,
208
+ .rasterizerDiscardEnable = c .VK_FALSE ,
209
+ .polygonMode = c .VK_POLYGON_MODE_FILL ,
210
+ .lineWidth = 1.0 ,
211
+ .cullMode = @intCast (u32 , @enumToInt (c .VK_CULL_MODE_BACK_BIT )),
212
+ .frontFace = c .VK_FRONT_FACE_CLOCKWISE ,
213
+ .depthBiasEnable = c .VK_FALSE ,
214
+
215
+ .pNext = null ,
216
+ .flags = 0 ,
217
+ .depthBiasConstantFactor = 0 ,
218
+ .depthBiasClamp = 0 ,
219
+ .depthBiasSlopeFactor = 0 ,
220
+ };
221
+
222
+ const multisampling = c.VkPipelineMultisampleStateCreateInfo {
223
+ .sType = c .VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO ,
224
+ .sampleShadingEnable = c .VK_FALSE ,
225
+ .rasterizationSamples = c .VK_SAMPLE_COUNT_1_BIT ,
226
+ .pNext = null ,
227
+ .flags = 0 ,
228
+ .minSampleShading = 0 ,
229
+ .pSampleMask = null ,
230
+ .alphaToCoverageEnable = 0 ,
231
+ .alphaToOneEnable = 0 ,
232
+ };
233
+
234
+ const colorBlendAttachment = c.VkPipelineColorBlendAttachmentState {
235
+ .colorWriteMask = c .VK_COLOR_COMPONENT_R_BIT | c .VK_COLOR_COMPONENT_G_BIT | c .VK_COLOR_COMPONENT_B_BIT | c .VK_COLOR_COMPONENT_A_BIT ,
236
+ .blendEnable = c .VK_FALSE ,
237
+
238
+ .srcColorBlendFactor = c .VK_BLEND_FACTOR_ZERO ,
239
+ .dstColorBlendFactor = c .VK_BLEND_FACTOR_ZERO ,
240
+ .colorBlendOp = c .VK_BLEND_OP_ADD ,
241
+ .srcAlphaBlendFactor = c .VK_BLEND_FACTOR_ZERO ,
242
+ .dstAlphaBlendFactor = c .VK_BLEND_FACTOR_ZERO ,
243
+ .alphaBlendOp = c .VK_BLEND_OP_ADD ,
244
+ };
245
+
246
+ const colorBlending = c.VkPipelineColorBlendStateCreateInfo {
247
+ .sType = c .VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO ,
248
+ .logicOpEnable = c .VK_FALSE ,
249
+ .logicOp = c .VK_LOGIC_OP_COPY ,
250
+ .attachmentCount = 1 ,
251
+ .pAttachments = & colorBlendAttachment ,
252
+ .blendConstants = []f32 { 0 , 0 , 0 , 0 },
253
+
254
+ .pNext = null ,
255
+ .flags = 0 ,
256
+ };
257
+
258
+ const pipelineLayoutInfo = c.VkPipelineLayoutCreateInfo {
259
+ .sType = c .VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO ,
260
+ .setLayoutCount = 0 ,
261
+ .pushConstantRangeCount = 0 ,
262
+ .pNext = null ,
263
+ .flags = 0 ,
264
+ .pSetLayouts = null ,
265
+ .pPushConstantRanges = null ,
266
+ };
267
+
268
+ try checkSuccess (c .vkCreatePipelineLayout (global_device , & pipelineLayoutInfo , null , & pipelineLayout ));
269
+
270
+ const pipelineInfo = []c.VkGraphicsPipelineCreateInfo {c.VkGraphicsPipelineCreateInfo {
271
+ .sType = c .VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO ,
272
+ .stageCount = @intCast (u32 , shaderStages .len ),
273
+ .pStages = & shaderStages ,
274
+ .pVertexInputState = & vertexInputInfo ,
275
+ .pInputAssemblyState = & inputAssembly ,
276
+ .pViewportState = & viewportState ,
277
+ .pRasterizationState = & rasterizer ,
278
+ .pMultisampleState = & multisampling ,
279
+ .pColorBlendState = & colorBlending ,
280
+ .layout = pipelineLayout ,
281
+ .renderPass = renderPass ,
282
+ .subpass = 0 ,
283
+ .basePipelineHandle = null ,
284
+
285
+ .pNext = null ,
286
+ .flags = 0 ,
287
+ .pTessellationState = null ,
288
+ .pDepthStencilState = null ,
289
+ .pDynamicState = null ,
290
+ .basePipelineIndex = 0 ,
291
+ }};
292
+
293
+ try checkSuccess (c .vkCreateGraphicsPipelines (
294
+ global_device ,
295
+ null ,
296
+ @intCast (u32 , pipelineInfo .len ),
297
+ & pipelineInfo ,
298
+ null ,
299
+ (* [1 ]c .VkPipeline )(& graphicsPipeline ),
300
+ ));
301
+
302
+ c .vkDestroyShaderModule (global_device , fragShaderModule , null );
303
+ c .vkDestroyShaderModule (global_device , vertShaderModule , null );
304
+ }
305
+
109
306
fn createRenderPass () ! void {
110
307
const colorAttachment = c.VkAttachmentDescription {
111
308
.format = swapChainImageFormat ,
0 commit comments