Browse Source

glow on web: simplify the webgl2 vs webgl1 selection

pull/1179/head
Emil Ernerfeldt 3 years ago
parent
commit
e957674467
  1. 90
      egui_web/src/glow_wrapping.rs

90
egui_web/src/glow_wrapping.rs

@ -86,42 +86,64 @@ impl crate::Painter for WrappedGlowPainter {
}
/// Returns glow context and shader prefix.
fn init_glow_context_from_canvas(canvas: &HtmlCanvasElement) -> (glow::Context, &str) {
fn init_glow_context_from_canvas(canvas: &HtmlCanvasElement) -> (glow::Context, &'static str) {
const BEST_FIRST: bool = true;
let result = if BEST_FIRST {
// Trying WebGl2 first
init_webgl2(canvas).or_else(|| init_webgl1(canvas))
} else {
// Trying WebGl1 first (useful for testing).
crate::console_warn("Looking for WebGL1 first");
init_webgl1(canvas).or_else(|| init_webgl2(canvas))
};
if let Some(result) = result {
result
} else {
panic!("WebGL isn't supported");
}
}
fn init_webgl1(canvas: &HtmlCanvasElement) -> Option<(glow::Context, &'static str)> {
let gl1_ctx = canvas
.get_context("webgl")
.expect("Failed to query about WebGL2 context");
let gl1_ctx = gl1_ctx?;
crate::console_log("WebGL1 selected.");
let gl1_ctx = gl1_ctx
.dyn_into::<web_sys::WebGlRenderingContext>()
.unwrap();
let shader_prefix = if crate::webgl1_requires_brightening(&gl1_ctx) {
crate::console_log("Enabling webkitGTK brightening workaround.");
"#define APPLY_BRIGHTENING_GAMMA"
} else {
""
};
let glow_ctx = glow::Context::from_webgl1_context(gl1_ctx);
Some((glow_ctx, shader_prefix))
}
fn init_webgl2(canvas: &HtmlCanvasElement) -> Option<(glow::Context, &'static str)> {
let gl2_ctx = canvas
.get_context("webgl2")
.expect("Failed to query about WebGL2 context");
if let Some(gl2_ctx) = gl2_ctx {
crate::console_log("WebGL2 found.");
let gl2_ctx = gl2_ctx
.dyn_into::<web_sys::WebGl2RenderingContext>()
.unwrap();
let glow_ctx = glow::Context::from_webgl2_context(gl2_ctx);
let shader_prefix = "";
(glow_ctx, shader_prefix)
} else {
let gl1 = canvas
.get_context("webgl")
.expect("Failed to query about WebGL1 context");
if let Some(gl1) = gl1 {
crate::console_log("WebGL2 not available - falling back to WebGL1.");
let gl1_ctx = gl1.dyn_into::<web_sys::WebGlRenderingContext>().unwrap();
let shader_prefix = if crate::webgl1_requires_brightening(&gl1_ctx) {
crate::console_log("Enabling webkitGTK brightening workaround.");
"#define APPLY_BRIGHTENING_GAMMA"
} else {
""
};
let glow_ctx = glow::Context::from_webgl1_context(gl1_ctx);
(glow_ctx, shader_prefix)
} else {
panic!("Failed to get WebGL context.");
}
}
let gl2_ctx = gl2_ctx?;
crate::console_log("WebGL2 selected.");
let gl2_ctx = gl2_ctx
.dyn_into::<web_sys::WebGl2RenderingContext>()
.unwrap();
let glow_ctx = glow::Context::from_webgl2_context(gl2_ctx);
let shader_prefix = "";
Some((glow_ctx, shader_prefix))
}
trait DummyWebGLConstructor {
@ -133,10 +155,10 @@ trait DummyWebGLConstructor {
#[cfg(not(target_arch = "wasm32"))]
impl DummyWebGLConstructor for glow::Context {
fn from_webgl1_context(_context: WebGlRenderingContext) -> Self {
panic!("you cant use egui_web(glow) on native")
panic!("you can't use egui_web(glow) on native")
}
fn from_webgl2_context(_context: WebGl2RenderingContext) -> Self {
panic!("you cant use egui_web(glow) on native")
panic!("you can't use egui_web(glow) on native")
}
}

Loading…
Cancel
Save