-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path01_instance_creation.zig
99 lines (76 loc) · 2.69 KB
/
01_instance_creation.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
const std = @import("std");
const glfw = @import("glfw");
const vk = @import("vulkan");
const WIDTH: u32 = 800;
const HEIGHT: u32 = 600;
const BaseDispatch = vk.BaseWrapper(.{
.createInstance = true,
});
const InstanceDispatch = vk.InstanceWrapper(.{
.destroyInstance = true,
});
const HelloTriangleApplication = struct {
const Self = @This();
window: ?glfw.Window = null,
vkb: BaseDispatch = undefined,
vki: InstanceDispatch = undefined,
instance: vk.Instance = .null_handle,
pub fn init() Self {
return Self{};
}
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();
}
fn mainLoop(self: *Self) !void {
while (!self.window.?.shouldClose()) {
try glfw.pollEvents();
}
}
pub fn deinit(self: *Self) void {
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);
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 glfw_extensions = try glfw.getRequiredInstanceExtensions();
const create_info = vk.InstanceCreateInfo{
.flags = .{},
.p_application_info = &app_info,
.enabled_layer_count = 0,
.pp_enabled_layer_names = undefined,
.enabled_extension_count = @intCast(u32, glfw_extensions.len),
.pp_enabled_extension_names = glfw_extensions.ptr,
};
self.instance = try self.vkb.createInstance(&create_info, null);
self.vki = try InstanceDispatch.load(self.instance, vk_proc);
}
};
pub fn main() void {
var app = HelloTriangleApplication.init();
defer app.deinit();
app.run() catch |err| {
std.log.err("application exited with error: {any}", .{err});
return;
};
}