From fd0eeb26624c7f624764ef47d51b5b45f9852347 Mon Sep 17 00:00:00 2001 From: Stephen Gutekanst Date: Wed, 1 Dec 2021 10:18:03 +0000 Subject: [PATCH] gpu: handle resize events in example (fixes Vulkan crash on resize) Signed-off-by: Stephen Gutekanst --- gpu/src/dawn/hello_triangle.zig | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/gpu/src/dawn/hello_triangle.zig b/gpu/src/dawn/hello_triangle.zig index 14103c67..c5414da4 100644 --- a/gpu/src/dawn/hello_triangle.zig +++ b/gpu/src/dawn/hello_triangle.zig @@ -108,6 +108,20 @@ pub fn main() !void { c.wgpuShaderModuleRelease(vs_module); c.wgpuShaderModuleRelease(fs_module); + // Reconfigure the swap chain with the new framebuffer width/height, otherwise e.g. the Vulkan + // device would be lost after a resize. + const CallbackPayload = struct { + swap_chain: c.WGPUSwapChain, + swap_chain_format: c.WGPUTextureFormat, + }; + setup.window.setUserPointer(CallbackPayload, &.{ .swap_chain = swap_chain, .swap_chain_format = swap_chain_format }); + setup.window.setFramebufferSizeCallback((struct { + fn callback(window: glfw.Window, width: isize, height: isize) void { + const pl = window.getUserPointer(*CallbackPayload); + c.wgpuSwapChainConfigure(pl.?.swap_chain, pl.?.swap_chain_format, c.WGPUTextureUsage_RenderAttachment, @intCast(u32, width), @intCast(u32, height)); + } + }).callback); + while (!setup.window.shouldClose()) { try frame(.{ .device = setup.device,