-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path05_window_surface.zig
349 lines (277 loc) · 11.9 KB
/
05_window_surface.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
const std = @import("std");
const builtin = @import("builtin");
const Allocator = std.mem.Allocator;
const glfw = @import("glfw");
const vk = @import("vulkan");
const WIDTH: u32 = 800;
const HEIGHT: u32 = 600;
const validation_layers = [_][*:0]const u8{"VK_LAYER_KHRONOS_validation"};
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,
.enumeratePhysicalDevices = true,
.getDeviceProcAddr = true,
.getPhysicalDeviceQueueFamilyProperties = true,
.getPhysicalDeviceSurfaceSupportKHR = true,
});
const DeviceDispatch = vk.DeviceWrapper(.{
.destroyDevice = true,
.getDeviceQueue = 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;
}
};
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,
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();
}
fn mainLoop(self: *Self) !void {
while (!self.window.?.shouldClose()) {
try glfw.pollEvents();
}
}
pub fn deinit(self: *Self) void {
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 = 0,
.pp_enabled_extension_names = undefined,
.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 isDeviceSuitable(self: *Self, device: vk.PhysicalDevice) !bool {
const indices = try self.findQueueFamilies(device);
return indices.isComplete();
}
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;
};
}