-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path15_hello_triangle.zig
943 lines (786 loc) · 36.4 KB
/
15_hello_triangle.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
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
const std = @import("std");
const builtin = @import("builtin");
const Allocator = std.mem.Allocator;
const glfw = @import("glfw");
const vk = @import("vulkan");
const resources = @import("resources");
const WIDTH: u32 = 800;
const HEIGHT: u32 = 600;
const MAX_FRAMES_IN_FLIGHT: u32 = 2;
const validation_layers = [_][*:0]const u8{"VK_LAYER_KHRONOS_validation"};
const device_extensions = [_][*:0]const u8{vk.extension_info.khr_swapchain.name};
const enable_validation_layers: bool = switch (builtin.mode) {
.Debug, .ReleaseSafe => true,
else => false,
};
const BaseDispatch = vk.BaseWrapper(.{
.createInstance = true,
.enumerateInstanceLayerProperties = true,
});
const InstanceDispatch = vk.InstanceWrapper(.{
.createDebugUtilsMessengerEXT = enable_validation_layers,
.createDevice = true,
.destroyDebugUtilsMessengerEXT = enable_validation_layers,
.destroyInstance = true,
.destroySurfaceKHR = true,
.enumerateDeviceExtensionProperties = true,
.enumeratePhysicalDevices = true,
.getDeviceProcAddr = true,
.getPhysicalDeviceQueueFamilyProperties = true,
.getPhysicalDeviceSurfaceCapabilitiesKHR = true,
.getPhysicalDeviceSurfaceFormatsKHR = true,
.getPhysicalDeviceSurfacePresentModesKHR = true,
.getPhysicalDeviceSurfaceSupportKHR = true,
});
const DeviceDispatch = vk.DeviceWrapper(.{
.acquireNextImageKHR = true,
.allocateCommandBuffers = true,
.beginCommandBuffer = true,
.cmdBeginRenderPass = true,
.cmdBindPipeline = true,
.cmdDraw = true,
.cmdEndRenderPass = true,
.cmdSetViewport = true,
.cmdSetScissor = true,
.createCommandPool = true,
.createFence = true,
.createFramebuffer = true,
.createGraphicsPipelines = true,
.createImageView = true,
.createPipelineLayout = true,
.createRenderPass = true,
.createSemaphore = true,
.createShaderModule = true,
.createSwapchainKHR = true,
.destroyCommandPool = true,
.destroyDevice = true,
.destroyFence = true,
.destroyFramebuffer = true,
.destroyImageView = true,
.destroyPipeline = true,
.destroyPipelineLayout = true,
.destroyRenderPass = true,
.destroySemaphore = true,
.destroyShaderModule = true,
.destroySwapchainKHR = true,
.deviceWaitIdle = true,
.endCommandBuffer = true,
.getDeviceQueue = true,
.getSwapchainImagesKHR = true,
.queuePresentKHR = true,
.queueSubmit = true,
.resetCommandBuffer = true,
.resetFences = true,
.waitForFences = true,
});
const QueueFamilyIndices = struct {
graphics_family: ?u32 = null,
present_family: ?u32 = null,
fn isComplete(self: *const QueueFamilyIndices) bool {
return self.graphics_family != null and self.present_family != null;
}
};
pub const SwapChainSupportDetails = struct {
allocator: Allocator,
capabilities: vk.SurfaceCapabilitiesKHR = undefined,
formats: ?[]vk.SurfaceFormatKHR = null,
present_modes: ?[]vk.PresentModeKHR = null,
pub fn init(allocator: Allocator) SwapChainSupportDetails {
return .{ .allocator = allocator };
}
pub fn deinit(self: SwapChainSupportDetails) void {
if (self.formats != null) self.allocator.free(self.formats.?);
if (self.present_modes != null) self.allocator.free(self.present_modes.?);
}
};
const HelloTriangleApplication = struct {
const Self = @This();
allocator: Allocator,
window: ?glfw.Window = null,
vkb: BaseDispatch = undefined,
vki: InstanceDispatch = undefined,
vkd: DeviceDispatch = undefined,
instance: vk.Instance = .null_handle,
debug_messenger: vk.DebugUtilsMessengerEXT = .null_handle,
surface: vk.SurfaceKHR = .null_handle,
physical_device: vk.PhysicalDevice = .null_handle,
device: vk.Device = .null_handle,
graphics_queue: vk.Queue = .null_handle,
present_queue: vk.Queue = .null_handle,
swap_chain: vk.SwapchainKHR = .null_handle,
swap_chain_images: ?[]vk.Image = null,
swap_chain_image_format: vk.Format = .@"undefined",
swap_chain_extent: vk.Extent2D = .{ .width = 0, .height = 0 },
swap_chain_image_views: ?[]vk.ImageView = null,
swap_chain_framebuffers: ?[]vk.Framebuffer = null,
render_pass: vk.RenderPass = .null_handle,
pipeline_layout: vk.PipelineLayout = .null_handle,
graphics_pipeline: vk.Pipeline = .null_handle,
command_pool: vk.CommandPool = .null_handle,
command_buffer: vk.CommandBuffer = .null_handle,
image_available_semaphore: vk.Semaphore = .null_handle,
render_finished_semaphore: vk.Semaphore = .null_handle,
in_flight_fence: vk.Fence = .null_handle,
pub fn init(allocator: Allocator) Self {
return Self{ .allocator = allocator };
}
pub fn run(self: *Self) !void {
try self.initWindow();
try self.initVulkan();
try self.mainLoop();
}
fn initWindow(self: *Self) !void {
try glfw.init(.{});
self.window = try glfw.Window.create(WIDTH, HEIGHT, "Vulkan", null, null, .{
.client_api = .no_api,
.resizable = false,
});
}
fn initVulkan(self: *Self) !void {
try self.createInstance();
try self.setupDebugMessenger();
try self.createSurface();
try self.pickPhysicalDevice();
try self.createLogicalDevice();
try self.createSwapChain();
try self.createImageViews();
try self.createRenderPass();
try self.createGraphicsPipeline();
try self.createFramebuffers();
try self.createCommandPool();
try self.createCommandBuffer();
try self.createSyncObjects();
}
fn mainLoop(self: *Self) !void {
while (!self.window.?.shouldClose()) {
try glfw.pollEvents();
try self.drawFrame();
}
_ = try self.vkd.deviceWaitIdle(self.device);
}
pub fn deinit(self: *Self) void {
if (self.render_finished_semaphore != .null_handle) self.vkd.destroySemaphore(self.device, self.render_finished_semaphore, null);
if (self.image_available_semaphore != .null_handle) self.vkd.destroySemaphore(self.device, self.image_available_semaphore, null);
if (self.in_flight_fence != .null_handle) self.vkd.destroyFence(self.device, self.in_flight_fence, null);
if (self.command_pool != .null_handle) self.vkd.destroyCommandPool(self.device, self.command_pool, null);
if (self.swap_chain_framebuffers != null) {
for (self.swap_chain_framebuffers.?) |framebuffer| {
self.vkd.destroyFramebuffer(self.device, framebuffer, null);
}
self.allocator.free(self.swap_chain_framebuffers.?);
}
if (self.graphics_pipeline != .null_handle) self.vkd.destroyPipeline(self.device, self.graphics_pipeline, null);
if (self.pipeline_layout != .null_handle) self.vkd.destroyPipelineLayout(self.device, self.pipeline_layout, null);
if (self.render_pass != .null_handle) self.vkd.destroyRenderPass(self.device, self.render_pass, null);
if (self.swap_chain_image_views != null) {
for (self.swap_chain_image_views.?) |image_view| {
self.vkd.destroyImageView(self.device, image_view, null);
}
self.allocator.free(self.swap_chain_image_views.?);
}
if (self.swap_chain_images != null) self.allocator.free(self.swap_chain_images.?);
if (self.swap_chain != .null_handle) self.vkd.destroySwapchainKHR(self.device, self.swap_chain, null);
if (self.device != .null_handle) self.vkd.destroyDevice(self.device, null);
if (enable_validation_layers and self.debug_messenger != .null_handle) self.vki.destroyDebugUtilsMessengerEXT(self.instance, self.debug_messenger, null);
if (self.surface != .null_handle) self.vki.destroySurfaceKHR(self.instance, self.surface, null);
if (self.instance != .null_handle) self.vki.destroyInstance(self.instance, null);
if (self.window != null) self.window.?.destroy();
glfw.terminate();
}
fn createInstance(self: *Self) !void {
const vk_proc = @ptrCast(*const fn (instance: vk.Instance, procname: [*:0]const u8) callconv(.C) vk.PfnVoidFunction, &glfw.getInstanceProcAddress);
self.vkb = try BaseDispatch.load(vk_proc);
if (enable_validation_layers and !try self.checkValidationLayerSupport()) {
return error.MissingValidationLayer;
}
const app_info = vk.ApplicationInfo{
.p_application_name = "Hello Triangle",
.application_version = vk.makeApiVersion(1, 0, 0, 0),
.p_engine_name = "No Engine",
.engine_version = vk.makeApiVersion(1, 0, 0, 0),
.api_version = vk.API_VERSION_1_2,
};
const extensions = try getRequiredExtensions(self.allocator);
defer extensions.deinit();
var create_info = vk.InstanceCreateInfo{
.flags = .{},
.p_application_info = &app_info,
.enabled_layer_count = 0,
.pp_enabled_layer_names = undefined,
.enabled_extension_count = @intCast(u32, extensions.items.len),
.pp_enabled_extension_names = extensions.items.ptr,
};
if (enable_validation_layers) {
create_info.enabled_layer_count = validation_layers.len;
create_info.pp_enabled_layer_names = &validation_layers;
var debug_create_info: vk.DebugUtilsMessengerCreateInfoEXT = undefined;
populateDebugMessengerCreateInfo(&debug_create_info);
create_info.p_next = &debug_create_info;
}
self.instance = try self.vkb.createInstance(&create_info, null);
self.vki = try InstanceDispatch.load(self.instance, vk_proc);
}
fn populateDebugMessengerCreateInfo(create_info: *vk.DebugUtilsMessengerCreateInfoEXT) void {
create_info.* = .{
.flags = .{},
.message_severity = .{
.verbose_bit_ext = true,
.warning_bit_ext = true,
.error_bit_ext = true,
},
.message_type = .{
.general_bit_ext = true,
.validation_bit_ext = true,
.performance_bit_ext = true,
},
.pfn_user_callback = debugCallback,
.p_user_data = null,
};
}
fn setupDebugMessenger(self: *Self) !void {
if (!enable_validation_layers) return;
var create_info: vk.DebugUtilsMessengerCreateInfoEXT = undefined;
populateDebugMessengerCreateInfo(&create_info);
self.debug_messenger = try self.vki.createDebugUtilsMessengerEXT(self.instance, &create_info, null);
}
fn createSurface(self: *Self) !void {
if ((try glfw.createWindowSurface(self.instance, self.window.?, null, &self.surface)) != @enumToInt(vk.Result.success)) {
return error.SurfaceInitFailed;
}
}
fn pickPhysicalDevice(self: *Self) !void {
var device_count: u32 = undefined;
_ = try self.vki.enumeratePhysicalDevices(self.instance, &device_count, null);
if (device_count == 0) {
return error.NoGPUsSupportVulkan;
}
const devices = try self.allocator.alloc(vk.PhysicalDevice, device_count);
defer self.allocator.free(devices);
_ = try self.vki.enumeratePhysicalDevices(self.instance, &device_count, devices.ptr);
for (devices) |device| {
if (try self.isDeviceSuitable(device)) {
self.physical_device = device;
break;
}
}
if (self.physical_device == .null_handle) {
return error.NoSuitableDevice;
}
}
fn createLogicalDevice(self: *Self) !void {
const indices = try self.findQueueFamilies(self.physical_device);
const queue_priority = [_]f32{1};
var queue_create_info = [_]vk.DeviceQueueCreateInfo{
.{
.flags = .{},
.queue_family_index = indices.graphics_family.?,
.queue_count = 1,
.p_queue_priorities = &queue_priority,
},
.{
.flags = .{},
.queue_family_index = indices.present_family.?,
.queue_count = 1,
.p_queue_priorities = &queue_priority,
},
};
var create_info = vk.DeviceCreateInfo{
.flags = .{},
.queue_create_info_count = queue_create_info.len,
.p_queue_create_infos = &queue_create_info,
.enabled_layer_count = 0,
.pp_enabled_layer_names = undefined,
.enabled_extension_count = device_extensions.len,
.pp_enabled_extension_names = &device_extensions,
.p_enabled_features = null,
};
if (enable_validation_layers) {
create_info.enabled_layer_count = validation_layers.len;
create_info.pp_enabled_layer_names = &validation_layers;
}
self.device = try self.vki.createDevice(self.physical_device, &create_info, null);
self.vkd = try DeviceDispatch.load(self.device, self.vki.dispatch.vkGetDeviceProcAddr);
self.graphics_queue = self.vkd.getDeviceQueue(self.device, indices.graphics_family.?, 0);
self.present_queue = self.vkd.getDeviceQueue(self.device, indices.present_family.?, 0);
}
fn createSwapChain(self: *Self) !void {
const swap_chain_support = try self.querySwapChainSupport(self.physical_device);
defer swap_chain_support.deinit();
const surface_format: vk.SurfaceFormatKHR = chooseSwapSurfaceFormat(swap_chain_support.formats.?);
const present_mode: vk.PresentModeKHR = chooseSwapPresentMode(swap_chain_support.present_modes.?);
const extent: vk.Extent2D = try self.chooseSwapExtent(swap_chain_support.capabilities);
var image_count = swap_chain_support.capabilities.min_image_count + 1;
if (swap_chain_support.capabilities.max_image_count > 0) {
image_count = std.math.min(image_count, swap_chain_support.capabilities.max_image_count);
}
const indices = try self.findQueueFamilies(self.physical_device);
const queue_family_indices = [_]u32{ indices.graphics_family.?, indices.present_family.? };
const sharing_mode: vk.SharingMode = if (indices.graphics_family.? != indices.present_family.?)
.concurrent
else
.exclusive;
self.swap_chain = try self.vkd.createSwapchainKHR(self.device, &.{
.flags = .{},
.surface = self.surface,
.min_image_count = image_count,
.image_format = surface_format.format,
.image_color_space = surface_format.color_space,
.image_extent = extent,
.image_array_layers = 1,
.image_usage = .{ .color_attachment_bit = true },
.image_sharing_mode = sharing_mode,
.queue_family_index_count = queue_family_indices.len,
.p_queue_family_indices = &queue_family_indices,
.pre_transform = swap_chain_support.capabilities.current_transform,
.composite_alpha = .{ .opaque_bit_khr = true },
.present_mode = present_mode,
.clipped = vk.TRUE,
.old_swapchain = .null_handle,
}, null);
_ = try self.vkd.getSwapchainImagesKHR(self.device, self.swap_chain, &image_count, null);
self.swap_chain_images = try self.allocator.alloc(vk.Image, image_count);
_ = try self.vkd.getSwapchainImagesKHR(self.device, self.swap_chain, &image_count, self.swap_chain_images.?.ptr);
self.swap_chain_image_format = surface_format.format;
self.swap_chain_extent = extent;
}
fn createImageViews(self: *Self) !void {
self.swap_chain_image_views = try self.allocator.alloc(vk.ImageView, self.swap_chain_images.?.len);
for (self.swap_chain_images.?) |image, i| {
self.swap_chain_image_views.?[i] = try self.vkd.createImageView(self.device, &.{
.flags = .{},
.image = image,
.view_type = .@"2d",
.format = self.swap_chain_image_format,
.components = .{ .r = .identity, .g = .identity, .b = .identity, .a = .identity },
.subresource_range = .{
.aspect_mask = .{ .color_bit = true },
.base_mip_level = 0,
.level_count = 1,
.base_array_layer = 0,
.layer_count = 1,
},
}, null);
}
}
fn createRenderPass(self: *Self) !void {
const color_attachment = [_]vk.AttachmentDescription{.{
.flags = .{},
.format = self.swap_chain_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_ref = [_]vk.AttachmentReference{.{
.attachment = 0,
.layout = .color_attachment_optimal,
}};
const subpass = [_]vk.SubpassDescription{.{
.flags = .{},
.pipeline_bind_point = .graphics,
.input_attachment_count = 0,
.p_input_attachments = undefined,
.color_attachment_count = color_attachment_ref.len,
.p_color_attachments = &color_attachment_ref,
.p_resolve_attachments = null,
.p_depth_stencil_attachment = null,
.preserve_attachment_count = 0,
.p_preserve_attachments = undefined,
}};
const dependencies = [_]vk.SubpassDependency{.{
.src_subpass = vk.SUBPASS_EXTERNAL,
.dst_subpass = 0,
.src_stage_mask = .{ .color_attachment_output_bit = true },
.src_access_mask = .{},
.dst_stage_mask = .{ .color_attachment_output_bit = true },
.dst_access_mask = .{ .color_attachment_write_bit = true },
.dependency_flags = .{},
}};
self.render_pass = try self.vkd.createRenderPass(self.device, &.{
.flags = .{},
.attachment_count = color_attachment.len,
.p_attachments = &color_attachment,
.subpass_count = subpass.len,
.p_subpasses = &subpass,
.dependency_count = dependencies.len,
.p_dependencies = &dependencies,
}, null);
}
fn createGraphicsPipeline(self: *Self) !void {
const vert_shader_module: vk.ShaderModule = try self.createShaderModule(resources.vert_09);
defer self.vkd.destroyShaderModule(self.device, vert_shader_module, null);
const frag_shader_module: vk.ShaderModule = try self.createShaderModule(resources.frag_09);
defer self.vkd.destroyShaderModule(self.device, frag_shader_module, null);
const shader_stages = [_]vk.PipelineShaderStageCreateInfo{
.{
.flags = .{},
.stage = .{ .vertex_bit = true },
.module = vert_shader_module,
.p_name = "main",
.p_specialization_info = null,
},
.{
.flags = .{},
.stage = .{ .fragment_bit = true },
.module = frag_shader_module,
.p_name = "main",
.p_specialization_info = null,
},
};
const vertex_input_info = vk.PipelineVertexInputStateCreateInfo{
.flags = .{},
.vertex_binding_description_count = 0,
.p_vertex_binding_descriptions = undefined,
.vertex_attribute_description_count = 0,
.p_vertex_attribute_descriptions = undefined,
};
const input_assembly = vk.PipelineInputAssemblyStateCreateInfo{
.flags = .{},
.topology = .triangle_list,
.primitive_restart_enable = vk.FALSE,
};
const viewport_state = vk.PipelineViewportStateCreateInfo{
.flags = .{},
.viewport_count = 1,
.p_viewports = undefined,
.scissor_count = 1,
.p_scissors = undefined,
};
const rasterizer = vk.PipelineRasterizationStateCreateInfo{
.flags = .{},
.depth_clamp_enable = vk.FALSE,
.rasterizer_discard_enable = vk.FALSE,
.polygon_mode = .fill,
.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,
.line_width = 1,
};
const multisampling = vk.PipelineMultisampleStateCreateInfo{
.flags = .{},
.rasterization_samples = .{ .@"1_bit" = true },
.sample_shading_enable = vk.FALSE,
.min_sample_shading = 1,
.p_sample_mask = null,
.alpha_to_coverage_enable = vk.FALSE,
.alpha_to_one_enable = vk.FALSE,
};
const color_blend_attachment = [_]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_blending = vk.PipelineColorBlendStateCreateInfo{
.flags = .{},
.logic_op_enable = vk.FALSE,
.logic_op = .copy,
.attachment_count = color_blend_attachment.len,
.p_attachments = &color_blend_attachment,
.blend_constants = [_]f32{ 0, 0, 0, 0 },
};
const dynamic_states = [_]vk.DynamicState{ .viewport, .scissor };
const dynamic_state = vk.PipelineDynamicStateCreateInfo{
.flags = .{},
.dynamic_state_count = dynamic_states.len,
.p_dynamic_states = &dynamic_states,
};
self.pipeline_layout = try self.vkd.createPipelineLayout(self.device, &.{
.flags = .{},
.set_layout_count = 0,
.p_set_layouts = undefined,
.push_constant_range_count = 0,
.p_push_constant_ranges = undefined,
}, null);
const pipeline_info = [_]vk.GraphicsPipelineCreateInfo{.{
.flags = .{},
.stage_count = shader_stages.len,
.p_stages = &shader_stages,
.p_vertex_input_state = &vertex_input_info,
.p_input_assembly_state = &input_assembly,
.p_tessellation_state = null,
.p_viewport_state = &viewport_state,
.p_rasterization_state = &rasterizer,
.p_multisample_state = &multisampling,
.p_depth_stencil_state = null,
.p_color_blend_state = &color_blending,
.p_dynamic_state = &dynamic_state,
.layout = self.pipeline_layout,
.render_pass = self.render_pass,
.subpass = 0,
.base_pipeline_handle = .null_handle,
.base_pipeline_index = -1,
}};
_ = try self.vkd.createGraphicsPipelines(
self.device,
.null_handle,
pipeline_info.len,
&pipeline_info,
null,
@ptrCast([*]vk.Pipeline, &self.graphics_pipeline),
);
}
fn createFramebuffers(self: *Self) !void {
self.swap_chain_framebuffers = try self.allocator.alloc(vk.Framebuffer, self.swap_chain_image_views.?.len);
for (self.swap_chain_framebuffers.?) |*framebuffer, i| {
const attachments = [_]vk.ImageView{self.swap_chain_image_views.?[i]};
framebuffer.* = try self.vkd.createFramebuffer(self.device, &.{
.flags = .{},
.render_pass = self.render_pass,
.attachment_count = attachments.len,
.p_attachments = &attachments,
.width = self.swap_chain_extent.width,
.height = self.swap_chain_extent.height,
.layers = 1,
}, null);
}
}
fn createCommandPool(self: *Self) !void {
const queue_family_indices = try self.findQueueFamilies(self.physical_device);
self.command_pool = try self.vkd.createCommandPool(self.device, &.{
.flags = .{ .reset_command_buffer_bit = true },
.queue_family_index = queue_family_indices.graphics_family.?,
}, null);
}
fn createCommandBuffer(self: *Self) !void {
try self.vkd.allocateCommandBuffers(self.device, &.{
.command_pool = self.command_pool,
.level = .primary,
.command_buffer_count = 1,
}, @ptrCast([*]vk.CommandBuffer, &self.command_buffer));
}
fn recordCommandBuffer(self: *Self, command_buffer: vk.CommandBuffer, image_index: u32) !void {
try self.vkd.beginCommandBuffer(command_buffer, &.{
.flags = .{},
.p_inheritance_info = null,
});
const clear_values = [_]vk.ClearValue{.{
.color = .{ .float_32 = .{ 0, 0, 0, 1 } },
}};
const render_pass_info = vk.RenderPassBeginInfo{
.render_pass = self.render_pass,
.framebuffer = self.swap_chain_framebuffers.?[image_index],
.render_area = vk.Rect2D{
.offset = .{ .x = 0, .y = 0 },
.extent = self.swap_chain_extent,
},
.clear_value_count = clear_values.len,
.p_clear_values = &clear_values,
};
self.vkd.cmdBeginRenderPass(command_buffer, &render_pass_info, .@"inline");
{
self.vkd.cmdBindPipeline(command_buffer, .graphics, self.graphics_pipeline);
const viewports = [_]vk.Viewport{.{
.x = 0,
.y = 0,
.width = @intToFloat(f32, self.swap_chain_extent.width),
.height = @intToFloat(f32, self.swap_chain_extent.height),
.min_depth = 0,
.max_depth = 1,
}};
self.vkd.cmdSetViewport(command_buffer, 0, viewports.len, &viewports);
const scissors = [_]vk.Rect2D{.{
.offset = .{ .x = 0, .y = 0 },
.extent = self.swap_chain_extent,
}};
self.vkd.cmdSetScissor(command_buffer, 0, scissors.len, &scissors);
self.vkd.cmdDraw(command_buffer, 3, 1, 0, 0);
}
self.vkd.cmdEndRenderPass(command_buffer);
try self.vkd.endCommandBuffer(command_buffer);
}
fn createSyncObjects(self: *Self) !void {
self.image_available_semaphore = try self.vkd.createSemaphore(self.device, &.{ .flags = .{} }, null);
self.render_finished_semaphore = try self.vkd.createSemaphore(self.device, &.{ .flags = .{} }, null);
self.in_flight_fence = try self.vkd.createFence(self.device, &.{ .flags = .{ .signaled_bit = true } }, null);
}
fn drawFrame(self: *Self) !void {
_ = try self.vkd.waitForFences(self.device, 1, @ptrCast([*]const vk.Fence, &self.in_flight_fence), vk.TRUE, std.math.maxInt(u64));
try self.vkd.resetFences(self.device, 1, @ptrCast([*]const vk.Fence, &self.in_flight_fence));
const result = try self.vkd.acquireNextImageKHR(self.device, self.swap_chain, std.math.maxInt(u64), self.image_available_semaphore, .null_handle);
try self.vkd.resetCommandBuffer(self.command_buffer, .{});
try self.recordCommandBuffer(self.command_buffer, result.image_index);
const wait_semaphores = [_]vk.Semaphore{self.image_available_semaphore};
const wait_stages = [_]vk.PipelineStageFlags{.{ .color_attachment_output_bit = true }};
const signal_semaphores = [_]vk.Semaphore{self.render_finished_semaphore};
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 = 1,
.p_command_buffers = @ptrCast([*]const vk.CommandBuffer, &self.command_buffer),
.signal_semaphore_count = signal_semaphores.len,
.p_signal_semaphores = &signal_semaphores,
};
_ = try self.vkd.queueSubmit(self.graphics_queue, 1, &[_]vk.SubmitInfo{submit_info}, self.in_flight_fence);
_ = try self.vkd.queuePresentKHR(self.present_queue, &.{
.wait_semaphore_count = signal_semaphores.len,
.p_wait_semaphores = &signal_semaphores,
.swapchain_count = 1,
.p_swapchains = @ptrCast([*]const vk.SwapchainKHR, &self.swap_chain),
.p_image_indices = @ptrCast([*]const u32, &result.image_index),
.p_results = null,
});
}
fn createShaderModule(self: *Self, code: []const u8) !vk.ShaderModule {
return try self.vkd.createShaderModule(self.device, &.{
.flags = .{},
.code_size = code.len,
.p_code = @ptrCast([*]const u32, code),
}, null);
}
fn chooseSwapSurfaceFormat(available_formats: []vk.SurfaceFormatKHR) vk.SurfaceFormatKHR {
for (available_formats) |available_format| {
if (available_format.format == .b8g8r8a8_srgb and available_format.color_space == .srgb_nonlinear_khr) {
return available_format;
}
}
return available_formats[0];
}
fn chooseSwapPresentMode(available_present_modes: []vk.PresentModeKHR) vk.PresentModeKHR {
for (available_present_modes) |available_present_mode| {
if (available_present_mode == .mailbox_khr) {
return available_present_mode;
}
}
return .fifo_khr;
}
fn chooseSwapExtent(self: *Self, capabilities: vk.SurfaceCapabilitiesKHR) !vk.Extent2D {
if (capabilities.current_extent.width != 0xFFFF_FFFF) {
return capabilities.current_extent;
} else {
const window_size = try self.window.?.getFramebufferSize();
return vk.Extent2D{
.width = std.math.clamp(window_size.width, capabilities.min_image_extent.width, capabilities.max_image_extent.width),
.height = std.math.clamp(window_size.height, capabilities.min_image_extent.height, capabilities.max_image_extent.height),
};
}
}
fn querySwapChainSupport(self: *Self, device: vk.PhysicalDevice) !SwapChainSupportDetails {
var details = SwapChainSupportDetails.init(self.allocator);
details.capabilities = try self.vki.getPhysicalDeviceSurfaceCapabilitiesKHR(device, self.surface);
var format_count: u32 = undefined;
_ = try self.vki.getPhysicalDeviceSurfaceFormatsKHR(device, self.surface, &format_count, null);
if (format_count != 0) {
details.formats = try details.allocator.alloc(vk.SurfaceFormatKHR, format_count);
_ = try self.vki.getPhysicalDeviceSurfaceFormatsKHR(device, self.surface, &format_count, details.formats.?.ptr);
}
var present_mode_count: u32 = undefined;
_ = try self.vki.getPhysicalDeviceSurfacePresentModesKHR(device, self.surface, &present_mode_count, null);
if (present_mode_count != 0) {
details.present_modes = try details.allocator.alloc(vk.PresentModeKHR, present_mode_count);
_ = try self.vki.getPhysicalDeviceSurfacePresentModesKHR(device, self.surface, &present_mode_count, details.present_modes.?.ptr);
}
return details;
}
fn isDeviceSuitable(self: *Self, device: vk.PhysicalDevice) !bool {
const indices = try self.findQueueFamilies(device);
const extensions_supported = try self.checkDeviceExtensionSupport(device);
var swap_chain_adequate = false;
if (extensions_supported) {
const swap_chain_support = try self.querySwapChainSupport(device);
defer swap_chain_support.deinit();
swap_chain_adequate = swap_chain_support.formats != null and swap_chain_support.present_modes != null;
}
return indices.isComplete() and extensions_supported and swap_chain_adequate;
}
fn checkDeviceExtensionSupport(self: *Self, device: vk.PhysicalDevice) !bool {
var extension_count: u32 = undefined;
_ = try self.vki.enumerateDeviceExtensionProperties(device, null, &extension_count, null);
const available_extensions = try self.allocator.alloc(vk.ExtensionProperties, extension_count);
defer self.allocator.free(available_extensions);
_ = try self.vki.enumerateDeviceExtensionProperties(device, null, &extension_count, available_extensions.ptr);
const required_extensions = device_extensions[0..];
for (required_extensions) |required_extension| {
for (available_extensions) |available_extension| {
const len = std.mem.indexOfScalar(u8, &available_extension.extension_name, 0).?;
const available_extension_name = available_extension.extension_name[0..len];
if (std.mem.eql(u8, std.mem.span(required_extension), available_extension_name)) {
break;
}
} else {
return false;
}
}
return true;
}
fn findQueueFamilies(self: *Self, device: vk.PhysicalDevice) !QueueFamilyIndices {
var indices: QueueFamilyIndices = .{};
var queue_family_count: u32 = 0;
self.vki.getPhysicalDeviceQueueFamilyProperties(device, &queue_family_count, null);
const queue_families = try self.allocator.alloc(vk.QueueFamilyProperties, queue_family_count);
defer self.allocator.free(queue_families);
self.vki.getPhysicalDeviceQueueFamilyProperties(device, &queue_family_count, queue_families.ptr);
for (queue_families) |queue_family, i| {
if (indices.graphics_family == null and queue_family.queue_flags.graphics_bit) {
indices.graphics_family = @intCast(u32, i);
} else if (indices.present_family == null and (try self.vki.getPhysicalDeviceSurfaceSupportKHR(device, @intCast(u32, i), self.surface)) == vk.TRUE) {
indices.present_family = @intCast(u32, i);
}
if (indices.isComplete()) {
break;
}
}
return indices;
}
fn getRequiredExtensions(allocator: Allocator) !std.ArrayListAligned([*:0]const u8, null) {
var extensions = std.ArrayList([*:0]const u8).init(allocator);
try extensions.appendSlice(try glfw.getRequiredInstanceExtensions());
if (enable_validation_layers) {
try extensions.append(vk.extension_info.ext_debug_utils.name);
}
return extensions;
}
fn checkValidationLayerSupport(self: *Self) !bool {
var layer_count: u32 = undefined;
_ = try self.vkb.enumerateInstanceLayerProperties(&layer_count, null);
var available_layers = try self.allocator.alloc(vk.LayerProperties, layer_count);
defer self.allocator.free(available_layers);
_ = try self.vkb.enumerateInstanceLayerProperties(&layer_count, available_layers.ptr);
for (validation_layers) |layer_name| {
var layer_found: bool = false;
for (available_layers) |layer_properties| {
const available_len = std.mem.indexOfScalar(u8, &layer_properties.layer_name, 0).?;
const available_layer_name = layer_properties.layer_name[0..available_len];
if (std.mem.eql(u8, std.mem.span(layer_name), available_layer_name)) {
layer_found = true;
break;
}
}
if (!layer_found) {
return false;
}
}
return true;
}
fn debugCallback(_: vk.DebugUtilsMessageSeverityFlagsEXT, _: vk.DebugUtilsMessageTypeFlagsEXT, p_callback_data: ?*const vk.DebugUtilsMessengerCallbackDataEXT, _: ?*anyopaque) callconv(vk.vulkan_call_conv) vk.Bool32 {
if (p_callback_data != null) {
std.log.debug("validation layer: {s}", .{p_callback_data.?.p_message});
}
return vk.FALSE;
}
};
pub fn main() void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer {
const leaked = gpa.deinit();
if (leaked) std.log.err("MemLeak", .{});
}
const allocator = gpa.allocator();
var app = HelloTriangleApplication.init(allocator);
defer app.deinit();
app.run() catch |err| {
std.log.err("application exited with error: {any}", .{err});
return;
};
}