From a594cec4b635f6c533a839b27bda00e03819e3f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nazari=CC=81=20Gonza=CC=81lez?= Date: Sun, 25 Dec 2022 17:13:55 +0000 Subject: [PATCH] Added a feature to use a custom GL config for wayland and nvidia machines --- Cargo.toml | 1 + crates/notan_backend/Cargo.toml | 1 + crates/notan_winit/Cargo.toml | 1 + crates/notan_winit/src/gl_manager.rs | 96 ++++++++++++++++++---------- 4 files changed, 64 insertions(+), 35 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 9f96383f..eacc37a3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -57,6 +57,7 @@ texture_to_file = ["notan_graphics/texture_to_file"] random = ["notan_random"] glsl-to-spirv = ["notan_macro/glsl-to-spirv"] shaderc = ["notan_macro/shaderc"] +nvidia-wayland = ["notan_backend?/nvidia-wayland"] [dev-dependencies] egui_demo_lib = "0.20.0" diff --git a/crates/notan_backend/Cargo.toml b/crates/notan_backend/Cargo.toml index 1486d4e4..a8ff9fa8 100644 --- a/crates/notan_backend/Cargo.toml +++ b/crates/notan_backend/Cargo.toml @@ -22,3 +22,4 @@ audio = ["notan_web/audio", "notan_winit/audio"] links = ["notan_winit/links"] drop_files = ["notan_winit/drop_files", "notan_web/drop_files"] clipboard = ["notan_winit/clipboard", "notan_web/clipboard"] +nvidia-wayland = ["notan_winit/nvidia-wayland"] diff --git a/crates/notan_winit/Cargo.toml b/crates/notan_winit/Cargo.toml index 582f00be..97168c3c 100644 --- a/crates/notan_winit/Cargo.toml +++ b/crates/notan_winit/Cargo.toml @@ -32,3 +32,4 @@ audio = ["notan_app/audio", "notan_audio", "notan_oddio"] links = ["webbrowser"] drop_files = ["mime_guess"] clipboard = ["arboard", "notan_input"] +nvidia-wayland = [] diff --git a/crates/notan_winit/src/gl_manager.rs b/crates/notan_winit/src/gl_manager.rs index 6e7916f5..9b2f8976 100644 --- a/crates/notan_winit/src/gl_manager.rs +++ b/crates/notan_winit/src/gl_manager.rs @@ -1,4 +1,4 @@ -use glutin::config::{ConfigTemplateBuilder, GlConfig}; +use glutin::config::{Config, ConfigTemplateBuilder, GlConfig}; use glutin::context::{ ContextApi, ContextAttributesBuilder, GlProfile, NotCurrentGlContextSurfaceAccessor, PossiblyCurrentContext, Version, @@ -38,40 +38,7 @@ impl GlManager { let (window, gl_config) = DisplayBuilder::new() .with_window_builder(Some(builder)) - .build(event_loop, template, |configs| { - configs - .reduce(|accum, conf| { - let next_srgb = conf.srgb_capable(); - let next_transparency = conf.supports_transparency().unwrap_or(false); - let more_samples = conf.num_samples() > accum.num_samples(); - - // value of transparency for the priority check - let transparency_check = if config.transparent { - next_transparency - } else { - true - }; - - // priority 1: supports srgba, transparency and has more samples than current one - let full_support = next_srgb && transparency_check && more_samples; - - // priority 2: we don't care about transparency if it's not supported by next config - let srgba_plus_samples = next_srgb && more_samples; - - // priority 3: if it supports srgba is enough - let only_srgba = next_srgb; - - // select the config in order of priority - let select_config = full_support || srgba_plus_samples || only_srgba; - - if select_config { - conf - } else { - accum - } - }) - .unwrap() - }) + .build(event_loop, template, get_config(config)) .map_err(|e| { let mut err = String::from("Cannot select a valid OpenGL configuration"); if config.multisampling != 0 { @@ -184,3 +151,62 @@ impl GlManager { ); } } + +#[cfg(not(feature = "nvidia-wayland"))] +fn get_config( + config: &WindowConfig, +) -> impl FnOnce(Box + '_>) -> Config + '_ { + |configs| { + configs + .reduce(|accum, conf| { + let next_srgb = conf.srgb_capable(); + let next_transparency = conf.supports_transparency().unwrap_or(false); + let more_samples = conf.num_samples() > accum.num_samples(); + + // value of transparency for the priority check + let transparency_check = if config.transparent { + next_transparency + } else { + true + }; + + // priority 1: supports srgba, transparency and has more samples than current one + let full_support = next_srgb && transparency_check && more_samples; + + // priority 2: we don't care about transparency if it's not supported by next config + let srgba_plus_samples = next_srgb && more_samples; + + // priority 3: if it supports srgba is enough + let only_srgba = next_srgb; + + // select the config in order of priority + let select_config = full_support || srgba_plus_samples || only_srgba; + + if select_config { + conf + } else { + accum + } + }) + .unwrap() + } +} +#[cfg(feature = "nvidia-wayland")] +fn get_config( + _config: &WindowConfig, +) -> impl FnOnce(Box + '_>) -> Config + '_ { + |configs| { + configs + .reduce(|accum, conf| { + let transparency_check = conf.supports_transparency().unwrap_or(false) + & !accum.supports_transparency().unwrap_or(false); + + if transparency_check || conf.num_samples() > accum.num_samples() { + conf + } else { + accum + } + }) + .unwrap() + } +}