-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.zig
628 lines (534 loc) · 20.7 KB
/
main.zig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
const std = @import("std");
const c = @import("c.zig");
const vk = @import("vulkan");
const vkk = @import("vk-kickstart");
const dispatch = @import("dispatch.zig");
const Window = @import("Window.zig");
const Shaders = @import("shaders");
const GraphicsContext = @import("GraphicsContext.zig");
const Device = GraphicsContext.Device;
const Queue = GraphicsContext.Queue;
const CommandBuffer = GraphicsContext.CommandBuffer;
pub const vkk_options = struct {
// Constants below remove the need for an allocator. They all have default
// values but can be overriden if they are too big/small.
// Instance override
pub const instance_override = struct {
// pub const max_extensions = 64;
// pub const max_layers = 64;
};
// Physical device override
pub const physical_device_override = struct {
// pub const max_handles = 6;
// pub const max_enabled_extensions = 16;
// pub const max_available_extensions = 512;
// pub const max_queue_families = 16;
};
// Swapchain override
pub const swapchain_override = struct {
// pub const max_surface_formats = 32;
};
};
const max_frames_in_flight = 2;
const SyncObjects = struct {
image_available_semaphores: [max_frames_in_flight]vk.Semaphore,
render_finished_semaphores: [max_frames_in_flight]vk.Semaphore,
in_flight_fences: [max_frames_in_flight]vk.Fence,
};
const FrameSyncObjects = struct {
image_available_semaphore: vk.Semaphore,
render_finished_semaphore: vk.Semaphore,
in_flight_fence: vk.Fence,
};
fn errorCallback(error_code: i32, description: [*c]const u8) callconv(.C) void {
std.log.err("glfw: {}: {s}\n", .{ error_code, description });
}
const window_width = 800;
const window_height = 600;
pub fn main() !void {
if (c.glfwInit() == c.GLFW_FALSE) return error.GlfwInitFailed;
defer c.glfwTerminate();
_ = c.glfwSetErrorCallback(errorCallback);
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();
const window = try Window.init(allocator, window_width, window_height, "vk-kickstart");
defer window.deinit(allocator);
var ctx = try GraphicsContext.init(allocator, window);
defer ctx.deinit(allocator);
const device = ctx.device;
var swapchain = try vkk.Swapchain.create(
device.handle,
ctx.physical_device.handle,
ctx.surface,
.{
.graphics_queue_index = ctx.graphics_queue_index,
.present_queue_index = ctx.present_queue_index,
.desired_extent = .{ .width = window_width, .height = window_height },
},
null,
);
defer device.destroySwapchainKHR(swapchain.handle, null);
var images = try allocator.alloc(vk.Image, swapchain.image_count);
defer allocator.free(images);
try swapchain.getImages(images);
var image_views = try allocator.alloc(vk.ImageView, swapchain.image_count);
defer allocator.free(image_views);
try swapchain.getImageViews(images, image_views, null);
defer {
for (image_views) |view| {
device.destroyImageView(view, null);
}
}
const render_pass = try createRenderPass(device, swapchain.image_format);
defer device.destroyRenderPass(render_pass, null);
var framebuffers = try createFramebuffers(allocator, device, swapchain.extent, swapchain.image_count, image_views, render_pass);
defer destroyFramebuffers(allocator, device, framebuffers);
const sync = try createSyncObjects(device);
defer destroySyncObjects(device, sync);
const vertex_shader = try createShaderModule(device, &Shaders.shader_vert);
defer device.destroyShaderModule(vertex_shader, null);
const fragment_shader = try createShaderModule(device, &Shaders.shader_frag);
defer device.destroyShaderModule(fragment_shader, null);
const pipeline_layout_info = vk.PipelineLayoutCreateInfo{};
const pipeline_layout = try device.createPipelineLayout(&pipeline_layout_info, null);
defer device.destroyPipelineLayout(pipeline_layout, null);
const pipeline = try createGraphicsPipeline(device, render_pass, vertex_shader, fragment_shader, pipeline_layout);
defer device.destroyPipeline(pipeline, null);
const command_pool = try createCommandPool(device, ctx.graphics_queue_index);
defer device.destroyCommandPool(command_pool, null);
const command_buffers = try createCommandBuffers(allocator, device, command_pool, max_frames_in_flight);
defer allocator.free(command_buffers);
var current_frame: u32 = 0;
var should_recreate_swapchain = false;
while (!window.shouldClose()) {
c.glfwPollEvents();
if (should_recreate_swapchain) {
swapchain = try recreateSwapchain(
allocator,
&ctx,
window,
&swapchain,
&images,
&image_views,
render_pass,
&framebuffers,
);
should_recreate_swapchain = false;
}
if (window.framebuffer_resized) {
should_recreate_swapchain = true;
window.framebuffer_resized = false;
continue;
}
const result = try device.waitForFences(1, @ptrCast(&sync.in_flight_fences[current_frame]), vk.TRUE, std.math.maxInt(u64));
std.debug.assert(result == .success);
const next_image_result = device.acquireNextImageKHR(
swapchain.handle,
std.math.maxInt(u64),
sync.image_available_semaphores[current_frame],
.null_handle,
) catch |err| {
if (err == error.OutOfDateKHR) {
should_recreate_swapchain = true;
continue;
}
return err;
};
std.debug.assert(next_image_result.result == .success);
const image_index = next_image_result.image_index;
try recordCommandBuffer(&ctx, command_buffers[current_frame], pipeline, render_pass, framebuffers[image_index], swapchain.extent);
const frame_sync_objects = FrameSyncObjects{
.image_available_semaphore = sync.image_available_semaphores[current_frame],
.render_finished_semaphore = sync.render_finished_semaphores[current_frame],
.in_flight_fence = sync.in_flight_fences[current_frame],
};
if (!try drawFrame(
&ctx,
command_buffers[current_frame],
frame_sync_objects,
swapchain.handle,
image_index,
)) {
should_recreate_swapchain = true;
}
current_frame = (current_frame + 1) % max_frames_in_flight;
}
try device.deviceWaitIdle();
}
fn drawFrame(
ctx: *const GraphicsContext,
command_buffer: vk.CommandBuffer,
sync: FrameSyncObjects,
swapchain: vk.SwapchainKHR,
image_index: u32,
) !bool {
const wait_semaphores = [_]vk.Semaphore{sync.image_available_semaphore};
const wait_stages = [_]vk.PipelineStageFlags{.{ .color_attachment_output_bit = true }};
const signal_semaphores = [_]vk.Semaphore{sync.render_finished_semaphore};
const command_buffers = [_]vk.CommandBuffer{command_buffer};
const submit_info = vk.SubmitInfo{
.wait_semaphore_count = wait_semaphores.len,
.p_wait_semaphores = &wait_semaphores,
.p_wait_dst_stage_mask = &wait_stages,
.command_buffer_count = command_buffers.len,
.p_command_buffers = &command_buffers,
.signal_semaphore_count = signal_semaphores.len,
.p_signal_semaphores = &signal_semaphores,
};
const fences = [_]vk.Fence{sync.in_flight_fence};
try ctx.device.resetFences(fences.len, &fences);
const submits = [_]vk.SubmitInfo{submit_info};
try ctx.graphics_queue.submit(submits.len, &submits, sync.in_flight_fence);
const indices = [_]u32{image_index};
const swapchains = [_]vk.SwapchainKHR{swapchain};
const present_info = vk.PresentInfoKHR{
.wait_semaphore_count = signal_semaphores.len,
.p_wait_semaphores = &signal_semaphores,
.swapchain_count = swapchains.len,
.p_swapchains = &swapchains,
.p_image_indices = &indices,
};
const present_result = ctx.present_queue.presentKHR(&present_info) catch |err| {
if (err == error.OutOfDateKHR) {
return false;
}
return err;
};
if (present_result == .suboptimal_khr) {
return false;
}
return true;
}
fn recreateSwapchain(
allocator: std.mem.Allocator,
ctx: *const GraphicsContext,
window: *Window,
old_swapchain: *vkk.Swapchain,
images: *[]vk.Image,
image_views: *[]vk.ImageView,
render_pass: vk.RenderPass,
framebuffers: *[]vk.Framebuffer,
) !vkk.Swapchain {
var extent = window.extent();
while (extent.width == 0 or extent.height == 0) {
extent = window.extent();
c.glfwWaitEvents();
}
try ctx.device.deviceWaitIdle();
const swapchain = try vkk.Swapchain.create(
ctx.device.handle,
ctx.physical_device.handle,
old_swapchain.surface,
.{
.graphics_queue_index = ctx.graphics_queue_index,
.present_queue_index = ctx.present_queue_index,
.desired_extent = .{ .width = extent.width, .height = extent.height },
.old_swapchain = old_swapchain.handle,
},
null,
);
for (image_views.*) |view| {
ctx.device.destroyImageView(view, null);
}
ctx.device.destroySwapchainKHR(old_swapchain.handle, null);
destroyFramebuffers(allocator, ctx.device, framebuffers.*);
try swapchain.getImages(images.*);
try swapchain.getImageViews(images.*, image_views.*, null);
framebuffers.* = try createFramebuffers(
allocator,
ctx.device,
extent,
swapchain.image_count,
image_views.*,
render_pass,
);
return swapchain;
}
fn destroyFramebuffers(allocator: std.mem.Allocator, device: Device, framebuffers: []const vk.Framebuffer) void {
for (framebuffers) |framebuffer| {
device.destroyFramebuffer(framebuffer, null);
}
allocator.free(framebuffers);
}
fn destroySyncObjects(device: Device, sync: SyncObjects) void {
for (sync.image_available_semaphores) |semaphore| {
device.destroySemaphore(semaphore, null);
}
for (sync.render_finished_semaphores) |semaphore| {
device.destroySemaphore(semaphore, null);
}
for (sync.in_flight_fences) |fence| {
device.destroyFence(fence, null);
}
}
fn recordCommandBuffer(
ctx: *const GraphicsContext,
command_buffer: vk.CommandBuffer,
pipeline: vk.Pipeline,
render_pass: vk.RenderPass,
framebuffer: vk.Framebuffer,
extent: vk.Extent2D,
) !void {
const begin_info = vk.CommandBufferBeginInfo{};
try ctx.device.beginCommandBuffer(command_buffer, &begin_info);
const cmd = CommandBuffer.init(command_buffer, ctx.vkd);
const clear_values = [_]vk.ClearValue{
.{ .color = .{ .float_32 = .{ 0.1, 0.1, 0.1, 1 } } },
};
const render_pass_begin_info = vk.RenderPassBeginInfo{
.render_pass = render_pass,
.framebuffer = framebuffer,
.render_area = .{
.offset = .{ .x = 0, .y = 0 },
.extent = extent,
},
.clear_value_count = clear_values.len,
.p_clear_values = &clear_values,
};
cmd.beginRenderPass(&render_pass_begin_info, .@"inline");
const viewport = vk.Viewport{
.x = 0,
.y = 0,
.width = @floatFromInt(extent.width),
.height = @floatFromInt(extent.height),
.min_depth = 0,
.max_depth = 1,
};
cmd.setViewport(0, 1, @ptrCast(&viewport));
const scissor = vk.Rect2D{
.offset = .{ .x = 0, .y = 0 },
.extent = extent,
};
cmd.setScissor(0, 1, @ptrCast(&scissor));
cmd.bindPipeline(.graphics, pipeline);
cmd.draw(3, 1, 0, 0);
cmd.endRenderPass();
try ctx.device.endCommandBuffer(command_buffer);
}
fn createCommandBuffers(
allocator: std.mem.Allocator,
device: Device,
command_pool: vk.CommandPool,
count: u32,
) ![]vk.CommandBuffer {
const command_buffers = try allocator.alloc(vk.CommandBuffer, count);
errdefer allocator.free(command_buffers);
const command_buffer_info = vk.CommandBufferAllocateInfo{
.command_pool = command_pool,
.level = .primary,
.command_buffer_count = count,
};
try device.allocateCommandBuffers(&command_buffer_info, command_buffers.ptr);
return command_buffers;
}
fn createCommandPool(device: Device, queue_family_index: u32) !vk.CommandPool {
const create_info = vk.CommandPoolCreateInfo{
.flags = .{ .reset_command_buffer_bit = true },
.queue_family_index = queue_family_index,
};
return device.createCommandPool(&create_info, null);
}
fn createGraphicsPipeline(
device: Device,
render_pass: vk.RenderPass,
vertex_shader: vk.ShaderModule,
fragment_shader: vk.ShaderModule,
pipeline_layout: vk.PipelineLayout,
) !vk.Pipeline {
const shader_stages = [2]vk.PipelineShaderStageCreateInfo{
.{
.stage = .{ .vertex_bit = true },
.module = vertex_shader,
.p_name = "main",
},
.{
.stage = .{ .fragment_bit = true },
.module = fragment_shader,
.p_name = "main",
},
};
const dynamic_states = [_]vk.DynamicState{ .viewport, .scissor };
const dynamic_state_info = vk.PipelineDynamicStateCreateInfo{
.dynamic_state_count = dynamic_states.len,
.p_dynamic_states = &dynamic_states,
};
const viewport_state_info = vk.PipelineViewportStateCreateInfo{
.viewport_count = 1,
.scissor_count = 1,
};
const vertex_input_info = vk.PipelineVertexInputStateCreateInfo{};
const input_assembly_info = vk.PipelineInputAssemblyStateCreateInfo{
.topology = .triangle_list,
.primitive_restart_enable = vk.FALSE,
};
const rasterizer_info = vk.PipelineRasterizationStateCreateInfo{
.depth_clamp_enable = vk.FALSE,
.rasterizer_discard_enable = vk.FALSE,
.polygon_mode = .fill,
.line_width = 1,
.cull_mode = .{ .back_bit = true },
.front_face = .clockwise,
.depth_bias_enable = vk.FALSE,
.depth_bias_constant_factor = 0,
.depth_bias_clamp = 0,
.depth_bias_slope_factor = 0,
};
const multisampling_info = vk.PipelineMultisampleStateCreateInfo{
.rasterization_samples = .{ .@"1_bit" = true },
.sample_shading_enable = vk.FALSE,
.min_sample_shading = 1,
.alpha_to_coverage_enable = vk.FALSE,
.alpha_to_one_enable = vk.FALSE,
};
const color_blend_attachments = [_]vk.PipelineColorBlendAttachmentState{.{
.blend_enable = vk.FALSE,
.src_color_blend_factor = .one,
.dst_color_blend_factor = .zero,
.color_blend_op = .add,
.src_alpha_blend_factor = .one,
.dst_alpha_blend_factor = .zero,
.alpha_blend_op = .add,
.color_write_mask = .{ .r_bit = true, .g_bit = true, .b_bit = true, .a_bit = true },
}};
const color_blend_info = vk.PipelineColorBlendStateCreateInfo{
.logic_op_enable = vk.FALSE,
.logic_op = .copy,
.attachment_count = color_blend_attachments.len,
.p_attachments = &color_blend_attachments,
.blend_constants = .{ 0, 0, 0, 0 },
};
const pipeline_info = vk.GraphicsPipelineCreateInfo{
.stage_count = shader_stages.len,
.p_stages = &shader_stages,
.p_vertex_input_state = &vertex_input_info,
.p_input_assembly_state = &input_assembly_info,
.p_viewport_state = &viewport_state_info,
.p_rasterization_state = &rasterizer_info,
.p_multisample_state = &multisampling_info,
.p_color_blend_state = &color_blend_info,
.p_dynamic_state = &dynamic_state_info,
.layout = pipeline_layout,
.render_pass = render_pass,
.subpass = 0,
.base_pipeline_index = -1,
};
var graphics_pipeline: vk.Pipeline = .null_handle;
const result = try device.createGraphicsPipelines(
.null_handle,
1,
@ptrCast(&pipeline_info),
null,
@ptrCast(&graphics_pipeline),
);
errdefer device.destroyPipeline(graphics_pipeline, null);
if (result != .success) return error.PipelineCreationFailed;
return graphics_pipeline;
}
fn createShaderModule(device: Device, bytecode: []align(4) const u8) !vk.ShaderModule {
const create_info = vk.ShaderModuleCreateInfo{
.code_size = bytecode.len,
.p_code = std.mem.bytesAsSlice(u32, bytecode).ptr,
};
return device.createShaderModule(&create_info, null);
}
fn createSyncObjects(device: Device) !SyncObjects {
var image_available_semaphores = [_]vk.Semaphore{.null_handle} ** max_frames_in_flight;
var render_finished_semaphores = [_]vk.Semaphore{.null_handle} ** max_frames_in_flight;
var in_flight_fences = [_]vk.Fence{.null_handle} ** max_frames_in_flight;
errdefer {
for (image_available_semaphores) |semaphore| {
if (semaphore == .null_handle) continue;
device.destroySemaphore(semaphore, null);
}
for (render_finished_semaphores) |semaphore| {
if (semaphore == .null_handle) continue;
device.destroySemaphore(semaphore, null);
}
for (in_flight_fences) |fence| {
if (fence == .null_handle) continue;
device.destroyFence(fence, null);
}
}
const semaphore_info = vk.SemaphoreCreateInfo{};
const fence_info = vk.FenceCreateInfo{ .flags = .{ .signaled_bit = true } };
for (0..max_frames_in_flight) |i| {
image_available_semaphores[i] = try device.createSemaphore(&semaphore_info, null);
render_finished_semaphores[i] = try device.createSemaphore(&semaphore_info, null);
in_flight_fences[i] = try device.createFence(&fence_info, null);
}
return .{
.image_available_semaphores = image_available_semaphores,
.render_finished_semaphores = render_finished_semaphores,
.in_flight_fences = in_flight_fences,
};
}
fn createFramebuffers(
allocator: std.mem.Allocator,
device: Device,
extent: vk.Extent2D,
image_count: u32,
image_views: []vk.ImageView,
render_pass: vk.RenderPass,
) ![]vk.Framebuffer {
var framebuffers = try std.ArrayList(vk.Framebuffer).initCapacity(allocator, image_count);
errdefer {
for (framebuffers.items) |framebuffer| {
device.destroyFramebuffer(framebuffer, null);
}
framebuffers.deinit();
}
for (0..image_count) |i| {
const attachments = [_]vk.ImageView{image_views[i]};
const framebuffer_info = vk.FramebufferCreateInfo{
.render_pass = render_pass,
.attachment_count = attachments.len,
.p_attachments = &attachments,
.width = extent.width,
.height = extent.height,
.layers = 1,
};
const framebuffer = try device.createFramebuffer(&framebuffer_info, null);
try framebuffers.append(framebuffer);
}
return framebuffers.toOwnedSlice();
}
fn createRenderPass(device: Device, image_format: vk.Format) !vk.RenderPass {
const color_attachment = vk.AttachmentDescription{
.format = image_format,
.samples = .{ .@"1_bit" = true },
.load_op = .clear,
.store_op = .store,
.stencil_load_op = .dont_care,
.stencil_store_op = .dont_care,
.initial_layout = .undefined,
.final_layout = .present_src_khr,
};
const color_attachment_refs = [_]vk.AttachmentReference{.{
.attachment = 0,
.layout = .color_attachment_optimal,
}};
const subpasses = [_]vk.SubpassDescription{.{
.pipeline_bind_point = .graphics,
.color_attachment_count = color_attachment_refs.len,
.p_color_attachments = &color_attachment_refs,
}};
const dependencies = [_]vk.SubpassDependency{.{
.src_subpass = vk.SUBPASS_EXTERNAL,
.dst_subpass = 0,
.src_stage_mask = .{ .color_attachment_output_bit = true, .early_fragment_tests_bit = true },
.src_access_mask = .{},
.dst_stage_mask = .{ .color_attachment_output_bit = true, .early_fragment_tests_bit = true },
.dst_access_mask = .{ .color_attachment_write_bit = true },
}};
const attachments = [_]vk.AttachmentDescription{color_attachment};
const renderpass_info = vk.RenderPassCreateInfo{
.attachment_count = attachments.len,
.p_attachments = &attachments,
.subpass_count = subpasses.len,
.p_subpasses = &subpasses,
.dependency_count = dependencies.len,
.p_dependencies = &dependencies,
};
return device.createRenderPass(&renderpass_info, null);
}