From 57d850822e0c2261b6ba3cc3246c307df6b6f875 Mon Sep 17 00:00:00 2001 From: Qwerasd Date: Wed, 14 Aug 2024 23:43:17 -0400 Subject: [PATCH 1/2] macos/opengl: lock context while rendering to stop resize crashes --- build.zig | 4 ++++ src/renderer/OpenGL.zig | 8 ++++++++ 2 files changed, 12 insertions(+) diff --git a/build.zig b/build.zig index 3b2f9028c..0165de802 100644 --- a/build.zig +++ b/build.zig @@ -1117,6 +1117,10 @@ fn addDeps( step.root_module.addImport("macos", macos_dep.module("macos")); step.linkLibrary(macos_dep.artifact("macos")); try static_libs.append(macos_dep.artifact("macos").getEmittedBin()); + + if (config.renderer == .opengl) { + step.linkFramework("OpenGL"); + } } // cimgui diff --git a/src/renderer/OpenGL.zig b/src/renderer/OpenGL.zig index 498a1e967..1f7fb1ea2 100644 --- a/src/renderer/OpenGL.zig +++ b/src/renderer/OpenGL.zig @@ -2030,6 +2030,14 @@ pub fn drawFrame(self: *OpenGL, surface: *apprt.Surface) !void { } } + const is_darwin = builtin.target.isDarwin(); + const ogl = if (comptime is_darwin) @cImport({ + @cInclude("OpenGL/OpenGL.h"); + }) else {}; + const cgl_ctx = if (comptime is_darwin) ogl.CGLGetCurrentContext(); + if (comptime is_darwin) _ = ogl.CGLLockContext(cgl_ctx); + defer _ = if (comptime is_darwin) ogl.CGLUnlockContext(cgl_ctx); + // Draw our terminal cells try self.drawCellProgram(gl_state); From bc667714dd5ad2de106d0d00fe47bdcdbefd0060 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 15 Aug 2024 14:25:05 -0700 Subject: [PATCH 2/2] renderer/opengl: add comment explaning ogl lock on darwin --- src/renderer/OpenGL.zig | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/renderer/OpenGL.zig b/src/renderer/OpenGL.zig index 1f7fb1ea2..d8aa9b92e 100644 --- a/src/renderer/OpenGL.zig +++ b/src/renderer/OpenGL.zig @@ -2030,6 +2030,12 @@ pub fn drawFrame(self: *OpenGL, surface: *apprt.Surface) !void { } } + // In the "OpenGL Programming Guide for Mac" it explains that: "When you + // use an NSOpenGLView object with OpenGL calls that are issued from a + // thread other than the main one, you must set up mutex locking." + // This locks the context and avoids crashes that can happen due to + // races with the underlying Metal layer that Apple is using to + // implement OpenGL. const is_darwin = builtin.target.isDarwin(); const ogl = if (comptime is_darwin) @cImport({ @cInclude("OpenGL/OpenGL.h");