From 5b71a7dbb94a02fbdff9087f4b5c13132f7c50b6 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Sat, 30 Mar 2024 18:30:26 +0100 Subject: [PATCH 1/2] glow integration: reuse `change_gl_context` function --- crates/eframe/src/native/glow_integration.rs | 23 ++------------------ 1 file changed, 2 insertions(+), 21 deletions(-) diff --git a/crates/eframe/src/native/glow_integration.rs b/crates/eframe/src/native/glow_integration.rs index 8dd0f05f6e21..9ebeb2528065 100644 --- a/crates/eframe/src/native/glow_integration.rs +++ b/crates/eframe/src/native/glow_integration.rs @@ -1178,15 +1178,7 @@ impl GlutinWindowContext { if let Some(viewport) = self.viewports.get(&viewport_id) { if let Some(gl_surface) = &viewport.gl_surface { - self.current_gl_context = Some( - self.current_gl_context - .take() - .unwrap() - .make_not_current() - .unwrap() - .make_current(gl_surface) - .unwrap(), - ); + change_gl_context(&mut self.current_gl_context, gl_surface); gl_surface.resize( self.current_gl_context .as_ref() @@ -1436,18 +1428,7 @@ fn render_immediate_viewport( let screen_size_in_pixels: [u32; 2] = window.inner_size().into(); - { - crate::profile_function!("context-switch"); - *current_gl_context = Some( - current_gl_context - .take() - .unwrap() - .make_not_current() - .unwrap() - .make_current(gl_surface) - .unwrap(), - ); - } + change_gl_context(current_gl_context, gl_surface); let current_gl_context = current_gl_context.as_ref().unwrap(); From f1b9b801718af9a6114efafdadc39c5e4dea4e80 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Sat, 30 Mar 2024 18:30:35 +0100 Subject: [PATCH 2/2] Early-out from `change_gl_context` --- crates/eframe/src/native/glow_integration.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/crates/eframe/src/native/glow_integration.rs b/crates/eframe/src/native/glow_integration.rs index 9ebeb2528065..ba0b4d8fbd23 100644 --- a/crates/eframe/src/native/glow_integration.rs +++ b/crates/eframe/src/native/glow_integration.rs @@ -836,6 +836,13 @@ fn change_gl_context( ) { crate::profile_function!(); + if let Some(current_gl_context) = current_gl_context { + crate::profile_scope!("is_current"); + if gl_surface.is_current(current_gl_context) { + return; // Early-out to save a lot of time. + } + } + let not_current = { crate::profile_scope!("make_not_current"); current_gl_context @@ -844,6 +851,7 @@ fn change_gl_context( .make_not_current() .unwrap() }; + crate::profile_scope!("make_current"); *current_gl_context = Some(not_current.make_current(gl_surface).unwrap()); }