From 06aec428de149e96ed7c4b6b53d704087570fcbc Mon Sep 17 00:00:00 2001 From: foxnne Date: Sun, 1 Dec 2024 13:02:23 -0600 Subject: [PATCH] core: windows: Get triangle showing, still freezing on run. Comment out more windows, set value of window back on presentFrame. --- examples/core-triangle/App.zig | 79 ++++++------- src/Core.zig | 13 +-- src/core/Windows.zig | 208 ++++++++++++++++----------------- 3 files changed, 142 insertions(+), 158 deletions(-) diff --git a/examples/core-triangle/App.zig b/examples/core-triangle/App.zig index 0a280dbce3..9523da6abf 100644 --- a/examples/core-triangle/App.zig +++ b/examples/core-triangle/App.zig @@ -26,47 +26,44 @@ pub fn init( core.on_exit = app_mod.id.deinit; const main_window = core.windows.getValue(core.main_window); - if (main_window.native != null) { - // if window.native is not null, the window is initialized - // Create our shader module - const shader_module = main_window.device.createShaderModuleWGSL("shader.wgsl", @embedFile("shader.wgsl")); - defer shader_module.release(); - - // Blend state describes how rendered colors get blended - const blend = gpu.BlendState{}; - - // Color target describes e.g. the pixel format of the window we are rendering to. - const color_target = gpu.ColorTargetState{ - .format = core.windows.get(core.main_window, .framebuffer_format), - .blend = &blend, - }; - - // Fragment state describes which shader and entrypoint to use for rendering fragments. - const fragment = gpu.FragmentState.init(.{ + // Create our shader module + const shader_module = main_window.device.createShaderModuleWGSL("shader.wgsl", @embedFile("shader.wgsl")); + defer shader_module.release(); + + // Blend state describes how rendered colors get blended + const blend = gpu.BlendState{}; + + // Color target describes e.g. the pixel format of the window we are rendering to. + const color_target = gpu.ColorTargetState{ + .format = main_window.framebuffer_format, + .blend = &blend, + }; + + // Fragment state describes which shader and entrypoint to use for rendering fragments. + const fragment = gpu.FragmentState.init(.{ + .module = shader_module, + .entry_point = "frag_main", + .targets = &.{color_target}, + }); + + // Create our render pipeline that will ultimately get pixels onto the screen. + const label = @tagName(mach_module) ++ ".init"; + const pipeline_descriptor = gpu.RenderPipeline.Descriptor{ + .label = label, + .fragment = &fragment, + .vertex = gpu.VertexState{ .module = shader_module, - .entry_point = "frag_main", - .targets = &.{color_target}, - }); - - // Create our render pipeline that will ultimately get pixels onto the screen. - const label = @tagName(mach_module) ++ ".init"; - const pipeline_descriptor = gpu.RenderPipeline.Descriptor{ - .label = label, - .fragment = &fragment, - .vertex = gpu.VertexState{ - .module = shader_module, - .entry_point = "vertex_main", - }, - }; - const pipeline = main_window.device.createRenderPipeline(&pipeline_descriptor); - - // Store our render pipeline in our module's state, so we can access it later on. - app.* = .{ - .title_timer = try mach.time.Timer.start(), - .pipeline = pipeline, - }; - } + .entry_point = "vertex_main", + }, + }; + const pipeline = main_window.device.createRenderPipeline(&pipeline_descriptor); + + // Store our render pipeline in our module's state, so we can access it later on. + app.* = .{ + .title_timer = try mach.time.Timer.start(), + .pipeline = pipeline, + }; } // TODO(object): window-title @@ -101,9 +98,7 @@ pub fn tick(app: *App, core: *mach.Core) void { } } - var main_window = core.windows.getValue(core.main_window); - - // Window is ready when device is not null + const main_window = core.windows.getValue(core.main_window); // Grab the back buffer of the swapchain // TODO(Core) diff --git a/src/Core.zig b/src/Core.zig index d143c0fe2d..8811437ebc 100644 --- a/src/Core.zig +++ b/src/Core.zig @@ -162,7 +162,6 @@ pub fn init(core: *Core) !void { pub fn initWindow(core: *Core, window_id: mach.ObjectID) !void { var core_window = core.windows.getValue(window_id); defer core.windows.setValueRaw(window_id, core_window); - core_window.instance = gpu.createInstance(null) orelse { log.err("failed to create GPU instance", .{}); std.process.exit(1); @@ -247,7 +246,6 @@ pub fn main(core: *Core, core_mod: mach.Mod(Core)) !void { if (core.on_exit == null) @panic("core.on_exit callback must be set"); try Platform.tick(core); - core_mod.run(core.on_tick.?); core_mod.call(.presentFrame); @@ -526,6 +524,7 @@ pub fn presentFrame(core: *Core, core_mod: mach.Mod(Core)) !void { var windows = core.windows.slice(); while (windows.next()) |window_id| { var core_window = core.windows.getValue(window_id); + defer core.windows.setValueRaw(window_id, core_window); mach.sysgpu.Impl.deviceTick(core_window.device); @@ -556,16 +555,6 @@ pub fn presentFrame(core: *Core, core_mod: mach.Mod(Core)) !void { } } - // TODO(important): update this information in response to resize events rather than - // after frame submission - // var win = core.windows.getAll(core.main_window).?; - // win.framebuffer_format = core.descriptor.format; - // win.framebuffer_width = core.descriptor.width; - // win.framebuffer_height = core.descriptor.height; - // win.width = core.platform.size.width; - // win.height = core.platform.size.height; - // core.windows.setAll(core.main_window, win); - // Record to frame rate frequency monitor that a frame was finished. core.frame.tick(); diff --git a/src/core/Windows.zig b/src/core/Windows.zig index 5e82d24d2a..0db0bdfe78 100644 --- a/src/core/Windows.zig +++ b/src/core/Windows.zig @@ -238,8 +238,6 @@ fn initWindow( _ = w.SetWindowLongPtrW(native_window, w.GWLP_USERDATA, @bitCast(@intFromPtr(&context))); - _ = w.ShowWindow(native_window, w.SW_SHOW); - restoreWindowPosition(core, window_id); const size = getClientRect(core, window_id); @@ -251,6 +249,7 @@ fn initWindow( core_window.native = native; core.windows.setValueRaw(window_id, core_window); try core.initWindow(window_id); + _ = w.ShowWindow(native_window, w.SW_SHOW); } // pub fn update(self: *Win32) !void { @@ -262,116 +261,116 @@ fn initWindow( // } // } -pub fn setTitle(self: *Win32, title: [:0]const u8) void { - const wtitle = std.unicode.utf8ToUtf16LeAllocZ(self.allocator, title) catch { - self.state.oom.set(); - return; - }; - defer self.allocator.free(wtitle); - _ = w.SetWindowTextW(self.window, wtitle); -} - -pub fn setDisplayMode(self: *Win32, mode: DisplayMode) void { - self.display_mode = mode; - - switch (mode) { - .windowed => { - const window_style: w.WINDOW_STYLE = if (self.border) w.WS_OVERLAPPEDWINDOW else w.WS_POPUPWINDOW; - const window_ex_style = w.WINDOW_EX_STYLE{ .APPWINDOW = 1 }; - - _ = w.SetWindowLongW(self.window, w.GWL_STYLE, @bitCast(window_style)); - _ = w.SetWindowLongW(self.window, w.GWL_EXSTYLE, @bitCast(window_ex_style)); - - restoreWindowPosition(self); - }, - .fullscreen => { - // TODO (win32) - change to use exclusive fullscreen using ChangeDisplaySetting - - _ = w.GetWindowRect(self.window, &self.saved_window_rect); - - const window_style = w.WINDOW_STYLE{ .POPUP = 1, .VISIBLE = 1 }; - const window_ex_style = w.WINDOW_EX_STYLE{ .APPWINDOW = 1 }; - - _ = w.SetWindowLongW(self.window, w.GWL_STYLE, @bitCast(window_style)); - _ = w.SetWindowLongW(self.window, w.GWL_EXSTYLE, @bitCast(window_ex_style)); - - const monitor = w.MonitorFromWindow(self.window, w.MONITOR_DEFAULTTONEAREST); - var monitor_info: w.MONITORINFO = undefined; - monitor_info.cbSize = @sizeOf(w.MONITORINFO); - if (w.GetMonitorInfoW(monitor, &monitor_info) == w.TRUE) { - _ = w.SetWindowPos(self.window, null, monitor_info.rcMonitor.left, monitor_info.rcMonitor.top, monitor_info.rcMonitor.right - monitor_info.rcMonitor.left, monitor_info.rcMonitor.bottom - monitor_info.rcMonitor.top, w.SWP_NOZORDER); - } - }, - .borderless => { - _ = w.GetWindowRect(self.window, &self.saved_window_rect); - - const window_style = w.WINDOW_STYLE{ .POPUP = 1, .VISIBLE = 1 }; - const window_ex_style = w.WINDOW_EX_STYLE{ .APPWINDOW = 1 }; - - _ = w.SetWindowLongW(self.window, w.GWL_STYLE, @bitCast(window_style)); - _ = w.SetWindowLongW(self.window, w.GWL_EXSTYLE, @bitCast(window_ex_style)); +// pub fn setTitle(self: *Win32, title: [:0]const u8) void { +// const wtitle = std.unicode.utf8ToUtf16LeAllocZ(self.allocator, title) catch { +// self.state.oom.set(); +// return; +// }; +// defer self.allocator.free(wtitle); +// _ = w.SetWindowTextW(self.window, wtitle); +// } - const monitor = w.MonitorFromWindow(self.window, w.MONITOR_DEFAULTTONEAREST); - var monitor_info: w.MONITORINFO = undefined; - monitor_info.cbSize = @sizeOf(w.MONITORINFO); - if (w.GetMonitorInfoW(monitor, &monitor_info) == w.TRUE) { - _ = w.SetWindowPos(self.window, null, monitor_info.rcMonitor.left, monitor_info.rcMonitor.top, monitor_info.rcMonitor.right - monitor_info.rcMonitor.left, monitor_info.rcMonitor.bottom - monitor_info.rcMonitor.top, w.SWP_NOZORDER); - } - }, - } -} +// pub fn setDisplayMode(self: *Win32, mode: DisplayMode) void { +// self.display_mode = mode; + +// switch (mode) { +// .windowed => { +// const window_style: w.WINDOW_STYLE = if (self.border) w.WS_OVERLAPPEDWINDOW else w.WS_POPUPWINDOW; +// const window_ex_style = w.WINDOW_EX_STYLE{ .APPWINDOW = 1 }; + +// _ = w.SetWindowLongW(self.window, w.GWL_STYLE, @bitCast(window_style)); +// _ = w.SetWindowLongW(self.window, w.GWL_EXSTYLE, @bitCast(window_ex_style)); + +// restoreWindowPosition(self); +// }, +// .fullscreen => { +// // TODO (win32) - change to use exclusive fullscreen using ChangeDisplaySetting + +// _ = w.GetWindowRect(self.window, &self.saved_window_rect); + +// const window_style = w.WINDOW_STYLE{ .POPUP = 1, .VISIBLE = 1 }; +// const window_ex_style = w.WINDOW_EX_STYLE{ .APPWINDOW = 1 }; + +// _ = w.SetWindowLongW(self.window, w.GWL_STYLE, @bitCast(window_style)); +// _ = w.SetWindowLongW(self.window, w.GWL_EXSTYLE, @bitCast(window_ex_style)); + +// const monitor = w.MonitorFromWindow(self.window, w.MONITOR_DEFAULTTONEAREST); +// var monitor_info: w.MONITORINFO = undefined; +// monitor_info.cbSize = @sizeOf(w.MONITORINFO); +// if (w.GetMonitorInfoW(monitor, &monitor_info) == w.TRUE) { +// _ = w.SetWindowPos(self.window, null, monitor_info.rcMonitor.left, monitor_info.rcMonitor.top, monitor_info.rcMonitor.right - monitor_info.rcMonitor.left, monitor_info.rcMonitor.bottom - monitor_info.rcMonitor.top, w.SWP_NOZORDER); +// } +// }, +// .borderless => { +// _ = w.GetWindowRect(self.window, &self.saved_window_rect); + +// const window_style = w.WINDOW_STYLE{ .POPUP = 1, .VISIBLE = 1 }; +// const window_ex_style = w.WINDOW_EX_STYLE{ .APPWINDOW = 1 }; + +// _ = w.SetWindowLongW(self.window, w.GWL_STYLE, @bitCast(window_style)); +// _ = w.SetWindowLongW(self.window, w.GWL_EXSTYLE, @bitCast(window_ex_style)); + +// const monitor = w.MonitorFromWindow(self.window, w.MONITOR_DEFAULTTONEAREST); +// var monitor_info: w.MONITORINFO = undefined; +// monitor_info.cbSize = @sizeOf(w.MONITORINFO); +// if (w.GetMonitorInfoW(monitor, &monitor_info) == w.TRUE) { +// _ = w.SetWindowPos(self.window, null, monitor_info.rcMonitor.left, monitor_info.rcMonitor.top, monitor_info.rcMonitor.right - monitor_info.rcMonitor.left, monitor_info.rcMonitor.bottom - monitor_info.rcMonitor.top, w.SWP_NOZORDER); +// } +// }, +// } +// } -pub fn setBorder(self: *Win32, value: bool) void { - const overlappedwindow: i32 = @bitCast(w.WS_OVERLAPPEDWINDOW); - const popupwindow: i32 = @bitCast(w.WS_POPUPWINDOW); - _ = w.SetWindowLongW(self.window, w.GWL_STYLE, if (value) overlappedwindow else popupwindow); - self.border = value; -} +// pub fn setBorder(self: *Win32, value: bool) void { +// const overlappedwindow: i32 = @bitCast(w.WS_OVERLAPPEDWINDOW); +// const popupwindow: i32 = @bitCast(w.WS_POPUPWINDOW); +// _ = w.SetWindowLongW(self.window, w.GWL_STYLE, if (value) overlappedwindow else popupwindow); +// self.border = value; +// } -pub fn setHeadless(self: *Win32, value: bool) void { - _ = w.ShowWindow(self.window, if (value) w.SW_HIDE else w.SW_SHOW); - self.headless = value; -} +// pub fn setHeadless(self: *Win32, value: bool) void { +// _ = w.ShowWindow(self.window, if (value) w.SW_HIDE else w.SW_SHOW); +// self.headless = value; +// } -pub fn setVSync(self: *Win32, mode: VSyncMode) void { - self.vsync_mode = mode; -} +// pub fn setVSync(self: *Win32, mode: VSyncMode) void { +// self.vsync_mode = mode; +// } -pub fn setSize(self: *Win32, value: Size) void { - // TODO (win32) - use AdjustClientRect to get correct client rect. - _ = w.SetWindowPos(self.window, null, 0, 0, @as(i32, @intCast(value.width)), @as(i32, @intCast(value.height)), w.SET_WINDOW_POS_FLAGS{ .NOMOVE = 1, .NOZORDER = 1, .NOACTIVATE = 1 }); - self.size = value; -} +// pub fn setSize(self: *Win32, value: Size) void { +// // TODO (win32) - use AdjustClientRect to get correct client rect. +// _ = w.SetWindowPos(self.window, null, 0, 0, @as(i32, @intCast(value.width)), @as(i32, @intCast(value.height)), w.SET_WINDOW_POS_FLAGS{ .NOMOVE = 1, .NOZORDER = 1, .NOACTIVATE = 1 }); +// self.size = value; +// } -pub fn setCursorMode(self: *Win32, mode: CursorMode) void { - switch (mode) { - .normal => while (w.ShowCursor(w.TRUE) < 0) {}, - .hidden => while (w.ShowCursor(w.FALSE) >= 0) {}, - .disabled => {}, - } - self.cursor_mode = mode; -} +// pub fn setCursorMode(self: *Win32, mode: CursorMode) void { +// switch (mode) { +// .normal => while (w.ShowCursor(w.TRUE) < 0) {}, +// .hidden => while (w.ShowCursor(w.FALSE) >= 0) {}, +// .disabled => {}, +// } +// self.cursor_mode = mode; +// } -pub fn setCursorShape(self: *Win32, shape: CursorShape) void { - const name: i32 = switch (shape) { - .arrow => w.IDC_ARROW, - .ibeam => w.IDC_IBEAM, - .crosshair => w.IDC_CROSS, - .pointing_hand => w.IDC_HAND, - .resize_ew => w.IDC_SIZEWE, - .resize_ns => w.IDC_SIZENS, - .resize_nwse => w.IDC_SIZENWSE, - .resize_nesw => w.IDC_SIZENESW, - .resize_all => w.IDC_SIZEALL, - .not_allowed => w.IDC_NO, - }; - _ = w.SetCursor(w.LoadCursorW(null, @ptrFromInt(@as(usize, @intCast(name))))); - self.cursor_shape = shape; -} +// pub fn setCursorShape(self: *Win32, shape: CursorShape) void { +// const name: i32 = switch (shape) { +// .arrow => w.IDC_ARROW, +// .ibeam => w.IDC_IBEAM, +// .crosshair => w.IDC_CROSS, +// .pointing_hand => w.IDC_HAND, +// .resize_ew => w.IDC_SIZEWE, +// .resize_ns => w.IDC_SIZENS, +// .resize_nwse => w.IDC_SIZENWSE, +// .resize_nesw => w.IDC_SIZENESW, +// .resize_all => w.IDC_SIZEALL, +// .not_allowed => w.IDC_NO, +// }; +// _ = w.SetCursor(w.LoadCursorW(null, @ptrFromInt(@as(usize, @intCast(name))))); +// self.cursor_shape = shape; +// } -pub fn nativeWindowWin32(self: *Win32) w.HWND { - return self.window; -} +// pub fn nativeWindowWin32(self: *Win32) w.HWND { +// return self.window; +// } // ----------------------------- // Internal functions @@ -615,6 +614,7 @@ fn wndProc(wnd: w.HWND, msg: u32, wParam: w.WPARAM, lParam: w.LPARAM) callconv(w }, else => return w.DefWindowProcW(wnd, msg, wParam, lParam), } + return w.DefWindowProcW(wnd, msg, wParam, lParam); }