From f9759cb536f67d78e26d77b448c1940abcfa33c8 Mon Sep 17 00:00:00 2001 From: HampusM Date: Thu, 28 May 2026 20:18:35 +0200 Subject: feat(opengl-bindings): add fn to make context current without surface --- opengl-bindings/src/lib.rs | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) (limited to 'opengl-bindings') diff --git a/opengl-bindings/src/lib.rs b/opengl-bindings/src/lib.rs index e4ae27d..3dcd61d 100644 --- a/opengl-bindings/src/lib.rs +++ b/opengl-bindings/src/lib.rs @@ -51,7 +51,7 @@ impl MaybeCurrentContextWithFns Ok(Self { context, fns: Box::new(gl) }) } - /// Attempts to make this context & the specified surface current in the current + /// Attempts to make this context & the specified surface current in the calling /// thread. /// /// # Errors @@ -70,6 +70,20 @@ impl MaybeCurrentContextWithFns Ok(()) } + /// Attempts to make this context current on the calling thread. + /// + /// # Errors + /// Returns `Err` if making this context current fails. + pub fn make_current_surfaceless(&self) -> Result<(), MakeContextCurrentError> + { + if !self.context.is_current() { + make_glutin_context_current_surfaceless(&self.context) + .map_err(MakeContextCurrentError)?; + } + + Ok(()) + } + #[must_use] pub fn context(&self) -> &PossiblyCurrentContext { @@ -89,6 +103,28 @@ impl MaybeCurrentContextWithFns #[error("Failed to make context current")] pub struct MakeContextCurrentError(#[source] glutin::error::Error); +fn make_glutin_context_current_surfaceless( + context: &PossiblyCurrentContext, +) -> Result<(), glutin::error::Error> +{ + #[cfg(not(any(target_os = "linux", target_os = "windows", target_os = "macos")))] + compile_error!("Unsupported target OS"); + + match context { + #[cfg(any(target_os = "windows", target_os = "linux"))] + PossiblyCurrentContext::Egl(context) => context.make_current_surfaceless(), + + #[cfg(target_os = "linux")] + PossiblyCurrentContext::Glx(context) => context.make_current_surfaceless(), + + #[cfg(target_os = "windows")] + PossiblyCurrentContext::Wgl(context) => context.make_current_surfaceless(), + + #[cfg(target_os = "macos")] + PossiblyCurrentContext::Cgl(context) => context.make_current_surfaceless(), + } +} + mod sys { #![allow( -- cgit v1.2.3-18-g5258