Added WebGlContextOption for eframe::WebOptions (#1803)
* Added WebGlContextOption for eframe::WebOptions * Fix doclink * Fix minor doc issue Co-authored-by: xxvvii <xuwei@aecg.com.cn> Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com>
This commit is contained in:
parent
c062bca6ee
commit
e76c919c7e
3 changed files with 37 additions and 10 deletions
|
@ -330,6 +330,11 @@ pub struct WebOptions {
|
||||||
///
|
///
|
||||||
/// Default: `Theme::Dark`.
|
/// Default: `Theme::Dark`.
|
||||||
pub default_theme: Theme,
|
pub default_theme: Theme,
|
||||||
|
|
||||||
|
/// Which version of WebGl context to select
|
||||||
|
///
|
||||||
|
/// Default: [`WebGlContextOption::BestFirst`].
|
||||||
|
pub webgl_context_option: WebGlContextOption,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(target_arch = "wasm32")]
|
#[cfg(target_arch = "wasm32")]
|
||||||
|
@ -338,6 +343,7 @@ impl Default for WebOptions {
|
||||||
Self {
|
Self {
|
||||||
follow_system_theme: true,
|
follow_system_theme: true,
|
||||||
default_theme: Theme::Dark,
|
default_theme: Theme::Dark,
|
||||||
|
webgl_context_option: WebGlContextOption::BestFirst,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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.
|
/// What rendering backend to use.
|
||||||
///
|
///
|
||||||
/// You need to enable the "glow" and "wgpu" features to have a choice.
|
/// You need to enable the "glow" and "wgpu" features to have a choice.
|
||||||
|
|
|
@ -160,7 +160,8 @@ impl AppRunner {
|
||||||
web_options: crate::WebOptions,
|
web_options: crate::WebOptions,
|
||||||
app_creator: epi::AppCreator,
|
app_creator: epi::AppCreator,
|
||||||
) -> Result<Self, JsValue> {
|
) -> 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 {
|
let system_theme = if web_options.follow_system_theme {
|
||||||
super::system_theme()
|
super::system_theme()
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
use crate::WebGlContextOption;
|
||||||
use egui::{ClippedPrimitive, Rgba};
|
use egui::{ClippedPrimitive, Rgba};
|
||||||
use egui_glow::glow;
|
use egui_glow::glow;
|
||||||
use wasm_bindgen::JsCast;
|
use wasm_bindgen::JsCast;
|
||||||
|
@ -13,10 +14,10 @@ pub(crate) struct WrappedGlowPainter {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl 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 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 gl = std::sync::Arc::new(gl);
|
||||||
|
|
||||||
let dimension = [canvas.width() as i32, canvas.height() as i32];
|
let dimension = [canvas.width() as i32, canvas.height() as i32];
|
||||||
|
@ -91,16 +92,19 @@ impl WrappedGlowPainter {
|
||||||
/// Returns glow context and shader prefix.
|
/// Returns glow context and shader prefix.
|
||||||
fn init_glow_context_from_canvas(
|
fn init_glow_context_from_canvas(
|
||||||
canvas: &HtmlCanvasElement,
|
canvas: &HtmlCanvasElement,
|
||||||
|
options: WebGlContextOption,
|
||||||
) -> Result<(glow::Context, &'static str), String> {
|
) -> Result<(glow::Context, &'static str), String> {
|
||||||
const BEST_FIRST: bool = true;
|
let result = match options {
|
||||||
|
// Force use WebGl1
|
||||||
let result = if BEST_FIRST {
|
WebGlContextOption::WebGl1 => init_webgl1(canvas),
|
||||||
|
// Force use WebGl2
|
||||||
|
WebGlContextOption::WebGl2 => init_webgl2(canvas),
|
||||||
// Trying WebGl2 first
|
// Trying WebGl2 first
|
||||||
init_webgl2(canvas).or_else(|| init_webgl1(canvas))
|
WebGlContextOption::BestFirst => init_webgl2(canvas).or_else(|| init_webgl1(canvas)),
|
||||||
} else {
|
|
||||||
// Trying WebGl1 first (useful for testing).
|
// Trying WebGl1 first (useful for testing).
|
||||||
tracing::warn!("Looking for WebGL1 first");
|
WebGlContextOption::CompatibilityFirst => {
|
||||||
init_webgl1(canvas).or_else(|| init_webgl2(canvas))
|
init_webgl1(canvas).or_else(|| init_webgl2(canvas))
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
if let Some(result) = result {
|
if let Some(result) = result {
|
||||||
|
|
Loading…
Reference in a new issue