Fix crash when minimizing window

Wait for the device to be idle before exiting
This commit is contained in:
Sirius902
2024-01-23 23:12:56 -08:00
parent d5e577f3f0
commit e3bc661349

View File

@@ -68,7 +68,7 @@ pub fn main() !void {
const gc = try GraphicsContext.init(allocator, app_name, window); const gc = try GraphicsContext.init(allocator, app_name, window);
defer gc.deinit(); defer gc.deinit();
std.debug.print("Using device: {s}\n", .{gc.deviceName()}); std.log.debug("Using device: {s}", .{gc.deviceName()});
var swapchain = try Swapchain.init(&gc, allocator, extent); var swapchain = try Swapchain.init(&gc, allocator, extent);
defer swapchain.deinit(); defer swapchain.deinit();
@@ -122,6 +122,16 @@ pub fn main() !void {
defer destroyCommandBuffers(&gc, pool, allocator, cmdbufs); defer destroyCommandBuffers(&gc, pool, allocator, cmdbufs);
while (c.glfwWindowShouldClose(window) == c.GLFW_FALSE) { while (c.glfwWindowShouldClose(window) == c.GLFW_FALSE) {
var w: c_int = undefined;
var h: c_int = undefined;
c.glfwGetFramebufferSize(window, &w, &h);
// Don't present or resize swapchain while the window is minimized
if (w == 0 or h == 0) {
c.glfwPollEvents();
continue;
}
const cmdbuf = cmdbufs[swapchain.image_index]; const cmdbuf = cmdbufs[swapchain.image_index];
const state = swapchain.present(cmdbuf) catch |err| switch (err) { const state = swapchain.present(cmdbuf) catch |err| switch (err) {
@@ -129,10 +139,6 @@ pub fn main() !void {
else => |narrow| return narrow, else => |narrow| return narrow,
}; };
var w: c_int = undefined;
var h: c_int = undefined;
c.glfwGetWindowSize(window, &w, &h);
if (state == .suboptimal or extent.width != @as(u32, @intCast(w)) or extent.height != @as(u32, @intCast(h))) { if (state == .suboptimal or extent.width != @as(u32, @intCast(w)) or extent.height != @as(u32, @intCast(h))) {
extent.width = @intCast(w); extent.width = @intCast(w);
extent.height = @intCast(h); extent.height = @intCast(h);
@@ -158,6 +164,7 @@ pub fn main() !void {
} }
try swapchain.waitForAllFences(); try swapchain.waitForAllFences();
try gc.vkd.deviceWaitIdle(gc.dev);
} }
fn uploadVertices(gc: *const GraphicsContext, pool: vk.CommandPool, buffer: vk.Buffer) !void { fn uploadVertices(gc: *const GraphicsContext, pool: vk.CommandPool, buffer: vk.Buffer) !void {
@@ -177,9 +184,7 @@ fn uploadVertices(gc: *const GraphicsContext, pool: vk.CommandPool, buffer: vk.B
defer gc.vkd.unmapMemory(gc.dev, staging_memory); defer gc.vkd.unmapMemory(gc.dev, staging_memory);
const gpu_vertices: [*]Vertex = @ptrCast(@alignCast(data)); const gpu_vertices: [*]Vertex = @ptrCast(@alignCast(data));
for (vertices, 0..) |vertex, i| { @memcpy(gpu_vertices, vertices[0..]);
gpu_vertices[i] = vertex;
}
} }
try copyBuffer(gc, pool, buffer, staging_buffer, @sizeOf(@TypeOf(vertices))); try copyBuffer(gc, pool, buffer, staging_buffer, @sizeOf(@TypeOf(vertices)));