Skip to content
This repository has been archived by the owner on Sep 28, 2023. It is now read-only.

Commit

Permalink
set alpha mode in wgpu. add a simple example
Browse files Browse the repository at this point in the history
  • Loading branch information
coderedart committed Jun 30, 2023
1 parent d9cc2ff commit ec528b5
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 4 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ members = [
# "crates/egui_render_rend3", # re-enable after rend3 upgrades to wgpu 0.16.
"examples/glfw_glow",
"examples/glfw_wgpu",
"examples/glfw_wgpu_simple",
# "examples/glfw_rend3",
"examples/sdl2_glow",
"examples/winit_wgpu",
Expand Down
2 changes: 1 addition & 1 deletion crates/egui_render_wgpu/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "egui_render_wgpu"
version = "0.4.1"
version = "0.4.2"
edition = "2021"
description = "egui rendering backend using wgpu"
license = "MIT"
Expand Down
21 changes: 18 additions & 3 deletions crates/egui_render_wgpu/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,20 @@ impl SurfaceManager {

let capabilities = self.surface.as_ref().unwrap().get_capabilities(adapter);
let supported_formats = capabilities.formats;
for alpha_mode in capabilities.alpha_modes {
debug!("supported alpha modes: {alpha_mode:#?}");
debug!(
"supported alpha modes: {:#?}",
&capabilities.alpha_modes[..]
);

if window_backend.get_config().transparent.unwrap_or_default() {
for alpha_mode in capabilities.alpha_modes.iter().copied() {
match alpha_mode {
CompositeAlphaMode::PreMultiplied | CompositeAlphaMode::PostMultiplied => {
self.surface_config.alpha_mode = alpha_mode;
}
_ => {}
}
}
}
debug!("supported formats of the surface: {supported_formats:#?}");

Expand Down Expand Up @@ -236,7 +248,10 @@ impl SurfaceManager {
if let Some(size) = window_backend.get_live_physical_size_framebuffer() {
self.surface_config.width = size[0];
self.surface_config.height = size[1];

info!(
"reconfiguring surface with config: {:#?}",
&self.surface_config
);
self.surface
.as_ref()
.unwrap()
Expand Down
13 changes: 13 additions & 0 deletions examples/glfw_wgpu_simple/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "glfw_wgpu_simple"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]

egui_backend = { path = "../../crates/egui_backend" }
egui_window_glfw_passthrough = { path = "../../crates/egui_window_glfw_passthrough" }
egui_render_wgpu = { path = "../../crates/egui_render_wgpu" }
tracing-subscriber = { version = "*", features = ["env-filter"] }
107 changes: 107 additions & 0 deletions examples/glfw_wgpu_simple/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
use egui_backend::{
egui::{self, Window},
BackendConfig, GfxBackend, UserApp, WindowBackend,
};
use egui_render_wgpu::WgpuBackend;
use egui_window_glfw_passthrough::{GlfwBackend, GlfwConfig};
use tracing_subscriber::prelude::__tracing_subscriber_SubscriberExt;
use tracing_subscriber::util::SubscriberInitExt;
struct App {
frame_count: usize,
egui_wants_input: bool,
is_window_receiving_events: bool,
egui_context: egui::Context,
wgpu_backend: WgpuBackend,
glfw_backend: GlfwBackend,
}

impl UserApp for App {
type UserWindowBackend = GlfwBackend;

fn get_all(
&mut self,
) -> (
&mut Self::UserWindowBackend,
&mut Self::UserGfxBackend,
&egui::Context,
) {
(
&mut self.glfw_backend,
&mut self.wgpu_backend,
&self.egui_context,
)
}
fn gui_run(&mut self) {
self.frame_count += 1;
let egui_context = self.egui_context.clone();
let egui_context = &&egui_context;
// draw a triangle
Window::new("egui user window").show(egui_context, |ui| {
ui.label(format!("frame number: {}", self.frame_count));
ui.label(format!("{:#?}", egui_context.pointer_latest_pos()));
ui.checkbox(
&mut self.is_window_receiving_events,
"Is Window receiving events?",
);
ui.checkbox(&mut self.egui_wants_input, "Does egui want input?");
});

self.is_window_receiving_events = !self.glfw_backend.window.is_mouse_passthrough();
if !self.is_window_receiving_events {
egui_context.request_repaint();
}
// don't forget to only ask egui if it wants input AFTER ending the frame
self.egui_wants_input =
egui_context.wants_pointer_input() || egui_context.wants_keyboard_input();
// if window is receiving events when egui doesn't want input. or if window not receiving events when egui wants input.
if self.is_window_receiving_events != self.egui_wants_input {
self.glfw_backend
.window
.set_mouse_passthrough(!self.egui_wants_input); // passthrough means not receiving events. so, if egui wants input, we set passthrough to false. otherwise true.
}
}

type UserGfxBackend = WgpuBackend;
}
impl App {
pub fn new(mut glfw_backend: GlfwBackend) -> Self {
let wgpu_backend = WgpuBackend::new(&mut glfw_backend, Default::default());
Self {
frame_count: 0,
egui_wants_input: false,
is_window_receiving_events: false,
egui_context: Default::default(),
wgpu_backend,
glfw_backend,
}
}
}

pub fn fake_main() {
tracing_subscriber::registry()
.with(tracing_subscriber::fmt::layer())
.with(tracing_subscriber::EnvFilter::from_default_env())
.init();
let window_backend = GlfwBackend::new(
GlfwConfig {
glfw_callback: Box::new(|glfw_context| {
glfw_context.window_hint(egui_window_glfw_passthrough::glfw::WindowHint::Floating(
true,
));
}),
..Default::default()
},
BackendConfig {
is_opengl: false,
opengl_config: Default::default(),
transparent: true.into(),
},
);

let app = App::new(window_backend);
<App as UserApp>::UserWindowBackend::run_event_loop(app);
}

fn main() {
fake_main();
}

0 comments on commit ec528b5

Please sign in to comment.