Disable custom 3D painting example in the egui demo app for Es100 (#1945)

Closes https://github.com/emilk/egui/issues/1944
This commit is contained in:
Emil Ernerfeldt 2022-08-20 19:54:18 +02:00 committed by GitHub
parent 97ce103209
commit bd5250f85d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 68 additions and 36 deletions

1
Cargo.lock generated
View File

@ -1259,6 +1259,7 @@ dependencies = [
"poll-promise",
"pollster",
"serde",
"tracing",
"tracing-subscriber",
"tracing-wasm",
"wgpu",

View File

@ -55,7 +55,7 @@ screen_reader = [
"tts",
]
## Use [`wgpu`](https://docs.rs/wgpu) for painting (via [`egui_wgpu`](https://github.com/emilk/egui/tree/master/crates/egui_wgpu)).
## Use [`wgpu`](https://docs.rs/wgpu) for painting (via [`egui-wgpu`](https://github.com/emilk/egui/tree/master/crates/egui-wgpu)).
## This overrides the `glow` feature.
wgpu = ["dep:wgpu", "egui-wgpu"]

View File

@ -42,6 +42,7 @@ chrono = { version = "0.4", features = ["js-sys", "wasmbind"] }
eframe = { version = "0.19.0", path = "../eframe", default-features = false }
egui = { version = "0.19.0", path = "../egui", features = ["extra_debug_asserts"] }
egui_demo_lib = { version = "0.19.0", path = "../egui_demo_lib", features = ["chrono"] }
tracing = "0.1"
# Optional dependencies:

View File

@ -11,13 +11,12 @@ pub struct Custom3d {
}
impl Custom3d {
pub fn new<'a>(cc: &'a eframe::CreationContext<'a>) -> Self {
Self {
rotating_triangle: Arc::new(Mutex::new(RotatingTriangle::new(
cc.gl.as_ref().expect("GL Enabled"),
))),
pub fn new<'a>(cc: &'a eframe::CreationContext<'a>) -> Option<Self> {
let gl = cc.gl.as_ref()?;
Some(Self {
rotating_triangle: Arc::new(Mutex::new(RotatingTriangle::new(gl)?)),
angle: 0.0,
}
})
}
}
@ -81,18 +80,22 @@ struct RotatingTriangle {
#[allow(unsafe_code)] // we need unsafe code to use glow
impl RotatingTriangle {
fn new(gl: &glow::Context) -> Self {
fn new(gl: &glow::Context) -> Option<Self> {
use glow::HasContext as _;
let shader_version = if cfg!(target_arch = "wasm32") {
"#version 300 es"
} else {
"#version 330"
};
let shader_version = egui_glow::ShaderVersion::get(gl);
unsafe {
let program = gl.create_program().expect("Cannot create program");
if !shader_version.is_new_shader_interface() {
tracing::warn!(
"Custom 3D painting hasn't been ported to {:?}",
shader_version
);
return None;
}
let (vertex_shader_source, fragment_shader_source) = (
r#"
const vec2 verts[3] = vec2[3](
@ -134,10 +137,20 @@ impl RotatingTriangle {
let shader = gl
.create_shader(*shader_type)
.expect("Cannot create shader");
gl.shader_source(shader, &format!("{}\n{}", shader_version, shader_source));
gl.shader_source(
shader,
&format!(
"{}\n{}",
shader_version.version_declaration(),
shader_source
),
);
gl.compile_shader(shader);
if !gl.get_shader_compile_status(shader) {
panic!("{}", gl.get_shader_info_log(shader));
panic!(
"Failed to compile custom_3d_glow: {}",
gl.get_shader_info_log(shader)
);
}
gl.attach_shader(program, shader);
shader
@ -158,10 +171,10 @@ impl RotatingTriangle {
.create_vertex_array()
.expect("Cannot create vertex array");
Self {
Some(Self {
program,
vertex_array,
}
})
}
}

View File

@ -10,10 +10,10 @@ pub struct Custom3d {
}
impl Custom3d {
pub fn new<'a>(cc: &'a eframe::CreationContext<'a>) -> Self {
pub fn new<'a>(cc: &'a eframe::CreationContext<'a>) -> Option<Self> {
// Get the WGPU render state from the eframe creation context. This can also be retrieved
// from `eframe::Frame` when you don't have a `CreationContext` available.
let wgpu_render_state = cc.wgpu_render_state.as_ref().expect("WGPU enabled");
let wgpu_render_state = cc.wgpu_render_state.as_ref()?;
let device = &wgpu_render_state.device;
@ -91,7 +91,7 @@ impl Custom3d {
uniform_buffer,
});
Self { angle: 0.0 }
Some(Self { angle: 0.0 })
}
}

View File

@ -95,7 +95,7 @@ pub struct WrapApp {
state: State,
#[cfg(any(feature = "glow", feature = "wgpu"))]
custom3d: crate::apps::Custom3d,
custom3d: Option<crate::apps::Custom3d>,
dropped_files: Vec<egui::DroppedFile>,
}
@ -148,11 +148,13 @@ impl WrapApp {
];
#[cfg(any(feature = "glow", feature = "wgpu"))]
vec.push((
"🔺 3D painting",
"custom3d",
&mut self.custom3d as &mut dyn eframe::App,
));
if let Some(custom3d) = &mut self.custom3d {
vec.push((
"🔺 3D painting",
"custom3d",
custom3d as &mut dyn eframe::App,
));
}
vec.push((
"🎨 Color test",
@ -219,7 +221,9 @@ impl eframe::App for WrapApp {
#[cfg(feature = "glow")]
fn on_exit(&mut self, gl: Option<&glow::Context>) {
self.custom3d.on_exit(gl);
if let Some(custom3d) = &mut self.custom3d {
custom3d.on_exit(gl);
}
}
}

View File

@ -19,6 +19,8 @@ mod post_process;
mod shader_version;
mod vao;
pub use shader_version::ShaderVersion;
#[cfg(all(not(target_arch = "wasm32"), feature = "winit"))]
pub mod winit;
#[cfg(all(not(target_arch = "wasm32"), feature = "winit"))]

View File

@ -114,7 +114,7 @@ impl Painter {
let shader_version = ShaderVersion::get(&gl);
let is_webgl_1 = shader_version == ShaderVersion::Es100;
let header = shader_version.version();
let header = shader_version.version_declaration();
tracing::debug!("Shader header: {:?}.", header);
let srgb_support = gl.supported_extensions().contains("EXT_sRGB");
@ -155,7 +155,11 @@ impl Painter {
"{}\n{}\n{}\n{}",
header,
shader_prefix,
shader_version.is_new_shader_interface(),
if shader_version.is_new_shader_interface() {
"#define NEW_SHADER_INTERFACE\n"
} else {
""
},
VERT_SRC
),
)?;
@ -167,7 +171,11 @@ impl Painter {
header,
shader_prefix,
srgb_support_define,
shader_version.is_new_shader_interface(),
if shader_version.is_new_shader_interface() {
"#define NEW_SHADER_INTERFACE\n"
} else {
""
},
FRAG_SRC
),
)?;

View File

@ -2,9 +2,10 @@
use std::convert::TryInto;
/// Helper for parsing and interpreting the OpenGL shader version.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[allow(dead_code)]
pub(crate) enum ShaderVersion {
pub enum ShaderVersion {
Gl120,
Gl140,
Es100,
@ -12,7 +13,7 @@ pub(crate) enum ShaderVersion {
}
impl ShaderVersion {
pub(crate) fn get(gl: &glow::Context) -> Self {
pub fn get(gl: &glow::Context) -> Self {
use glow::HasContext as _;
let shading_lang_string =
unsafe { gl.get_parameter_string(glow::SHADING_LANGUAGE_VERSION) };
@ -52,7 +53,8 @@ impl ShaderVersion {
}
}
pub(crate) fn version(&self) -> &'static str {
/// Goes on top of the shader.
pub fn version_declaration(&self) -> &'static str {
match self {
Self::Gl120 => "#version 120\n",
Self::Gl140 => "#version 140\n",
@ -61,10 +63,11 @@ impl ShaderVersion {
}
}
pub(crate) fn is_new_shader_interface(&self) -> &'static str {
/// If true, use `in/out`. If `false`, use `varying` and `gl_FragColor`.
pub fn is_new_shader_interface(&self) -> bool {
match self {
ShaderVersion::Es300 | ShaderVersion::Gl140 => "#define NEW_SHADER_INTERFACE\n",
_ => "",
Self::Gl120 | Self::Es100 => false,
Self::Es300 | Self::Gl140 => true,
}
}
}