Skip to content

Commit

Permalink
Added WebGlContextOption for eframe::WebOptions (#1803)
Browse files Browse the repository at this point in the history
* Added WebGlContextOption for eframe::WebOptions

* Fix doclink

* Fix minor doc issue

Co-authored-by: xxvvii <[email protected]>
Co-authored-by: Emil Ernerfeldt <[email protected]>
  • Loading branch information
3 people authored Jul 8, 2022
1 parent c062bca commit e76c919
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 10 deletions.
22 changes: 22 additions & 0 deletions eframe/src/epi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,11 @@ pub struct WebOptions {
///
/// Default: `Theme::Dark`.
pub default_theme: Theme,

/// Which version of WebGl context to select
///
/// Default: [`WebGlContextOption::BestFirst`].
pub webgl_context_option: WebGlContextOption,
}

#[cfg(target_arch = "wasm32")]
Expand All @@ -338,6 +343,7 @@ impl Default for WebOptions {
Self {
follow_system_theme: true,
default_theme: Theme::Dark,
webgl_context_option: WebGlContextOption::BestFirst,
}
}
}
Expand Down Expand Up @@ -368,6 +374,22 @@ impl Theme {

// ----------------------------------------------------------------------------

/// WebGl Context options
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub enum WebGlContextOption {
/// Force Use WebGL1.
WebGl1,
/// Force use WebGL2.
WebGl2,
/// Use WebGl2 first.
BestFirst,
/// Use WebGl1 first
CompatibilityFirst,
}

// ----------------------------------------------------------------------------

/// What rendering backend to use.
///
/// You need to enable the "glow" and "wgpu" features to have a choice.
Expand Down
3 changes: 2 additions & 1 deletion eframe/src/web/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,8 @@ impl AppRunner {
web_options: crate::WebOptions,
app_creator: epi::AppCreator,
) -> Result<Self, JsValue> {
let painter = WrappedGlowPainter::new(canvas_id).map_err(JsValue::from)?; // fail early
let painter = WrappedGlowPainter::new(canvas_id, web_options.webgl_context_option)
.map_err(JsValue::from)?; // fail early

let system_theme = if web_options.follow_system_theme {
super::system_theme()
Expand Down
22 changes: 13 additions & 9 deletions eframe/src/web/glow_wrapping.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::WebGlContextOption;
use egui::{ClippedPrimitive, Rgba};
use egui_glow::glow;
use wasm_bindgen::JsCast;
Expand All @@ -13,10 +14,10 @@ pub(crate) struct WrappedGlowPainter {
}

impl WrappedGlowPainter {
pub fn new(canvas_id: &str) -> Result<Self, String> {
pub fn new(canvas_id: &str, options: WebGlContextOption) -> Result<Self, String> {
let canvas = super::canvas_element_or_die(canvas_id);

let (gl, shader_prefix) = init_glow_context_from_canvas(&canvas)?;
let (gl, shader_prefix) = init_glow_context_from_canvas(&canvas, options)?;
let gl = std::sync::Arc::new(gl);

let dimension = [canvas.width() as i32, canvas.height() as i32];
Expand Down Expand Up @@ -91,16 +92,19 @@ impl WrappedGlowPainter {
/// Returns glow context and shader prefix.
fn init_glow_context_from_canvas(
canvas: &HtmlCanvasElement,
options: WebGlContextOption,
) -> Result<(glow::Context, &'static str), String> {
const BEST_FIRST: bool = true;

let result = if BEST_FIRST {
let result = match options {
// Force use WebGl1
WebGlContextOption::WebGl1 => init_webgl1(canvas),
// Force use WebGl2
WebGlContextOption::WebGl2 => init_webgl2(canvas),
// Trying WebGl2 first
init_webgl2(canvas).or_else(|| init_webgl1(canvas))
} else {
WebGlContextOption::BestFirst => init_webgl2(canvas).or_else(|| init_webgl1(canvas)),
// Trying WebGl1 first (useful for testing).
tracing::warn!("Looking for WebGL1 first");
init_webgl1(canvas).or_else(|| init_webgl2(canvas))
WebGlContextOption::CompatibilityFirst => {
init_webgl1(canvas).or_else(|| init_webgl2(canvas))
}
};

if let Some(result) = result {
Expand Down

0 comments on commit e76c919

Please sign in to comment.