Skip to content

Commit

Permalink
Use wgpu from main and make setting the window safe (and provide an u…
Browse files Browse the repository at this point in the history
…nsafe variant)
  • Loading branch information
Wumpf committed Jan 14, 2024
1 parent 828ddec commit 5297bc2
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 86 deletions.
12 changes: 10 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 2 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,6 @@ puffin = "0.18"
raw-window-handle = "0.5.0"
thiserror = "1.0.37"

wgpu = { path = "../wgpu/wgpu", features = [
wgpu = { git = "https://github.com/gfx-rs/wgpu", commit = "7774f31", features = [
"fragile-send-sync-non-atomic-wasm",
] } # Make the renderer `Sync` even on wasm32, because it makes the code simpler:

# Use this to build wgpu with WebGL support on the Web *instead* of using WebGPU.
#wgpu = { version = "0.18.0", features = ["webgl"] }
] } # Make the renderer `Sync` even on wasm32, because it makes the code simpler:# TODO: Use a stable version of wgpu.
10 changes: 5 additions & 5 deletions crates/eframe/src/native/glow_integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ struct Viewport {
// These three live and die together.
// TODO(emilk): clump them together into one struct!
gl_surface: Option<glutin::surface::Surface<glutin::surface::WindowSurface>>,
window: Option<Rc<Window>>,
window: Option<Arc<Window>>,
egui_winit: Option<egui_winit::State>,
}

Expand Down Expand Up @@ -373,7 +373,7 @@ impl WinitApp for GlowWinitApp {
self.running.as_ref().map(|r| &r.integration)
}

fn window(&self, window_id: WindowId) -> Option<Rc<Window>> {
fn window(&self, window_id: WindowId) -> Option<Arc<Window>> {
let running = self.running.as_ref()?;
let glutin = running.glutin.borrow();
let viewport_id = *glutin.viewport_from_window.get(&window_id)?;
Expand Down Expand Up @@ -952,7 +952,7 @@ impl GlutinWindowContext {
screenshot_requested: false,
viewport_ui_cb: None,
gl_surface: None,
window: window.map(Rc::new),
window: window.map(Arc::new),
egui_winit: None,
},
);
Expand Down Expand Up @@ -1031,7 +1031,7 @@ impl GlutinWindowContext {
);
viewport.info.minimized = window.is_minimized();
viewport.info.maximized = Some(window.is_maximized());
viewport.window.insert(Rc::new(window))
viewport.window.insert(Arc::new(window))
};

viewport.egui_winit.get_or_insert_with(|| {
Expand Down Expand Up @@ -1120,7 +1120,7 @@ impl GlutinWindowContext {
.expect("viewport doesn't exist")
}

fn window(&self, viewport_id: ViewportId) -> Rc<Window> {
fn window(&self, viewport_id: ViewportId) -> Arc<Window> {
self.viewport(viewport_id)
.window
.clone()
Expand Down
22 changes: 14 additions & 8 deletions crates/eframe/src/native/wgpu_integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ pub struct Viewport {

/// Window surface state that's initialized when the app starts running via a Resumed event
/// and on Android will also be destroyed if the application is paused.
window: Option<Rc<Window>>,
window: Option<Arc<Window>>,

/// `window` and `egui_winit` are initialized together.
egui_winit: Option<egui_winit::State>,
Expand Down Expand Up @@ -170,9 +170,11 @@ impl WgpuWinitApp {
self.native_options.viewport.transparent.unwrap_or(false),
);

let window = Arc::new(window);

{
crate::profile_scope!("set_window");
pollster::block_on(painter.set_window(ViewportId::ROOT, Some(&window)))?;
pollster::block_on(painter.set_window(ViewportId::ROOT, Some(window.clone())))?;
}

let wgpu_render_state = painter.render_state();
Expand Down Expand Up @@ -260,7 +262,7 @@ impl WgpuWinitApp {
},
screenshot_requested: false,
viewport_ui_cb: None,
window: Some(Rc::new(window)),
window: Some(window),
egui_winit: Some(egui_winit),
},
);
Expand Down Expand Up @@ -323,7 +325,7 @@ impl WinitApp for WgpuWinitApp {
self.running.as_ref().map(|r| &r.integration)
}

fn window(&self, window_id: WindowId) -> Option<Rc<Window>> {
fn window(&self, window_id: WindowId) -> Option<Arc<Window>> {
self.running
.as_ref()
.and_then(|r| {
Expand Down Expand Up @@ -544,7 +546,8 @@ impl WgpuWinitRunning {

{
crate::profile_scope!("set_window");
if let Err(err) = pollster::block_on(painter.set_window(viewport_id, Some(window)))
if let Err(err) =
pollster::block_on(painter.set_window(viewport_id, Some(window.clone())))
{
log::warn!("Failed to set window: {err}");
}
Expand Down Expand Up @@ -797,7 +800,10 @@ impl Viewport {
Ok(window) => {
windows_id.insert(window.id(), viewport_id);

if let Err(err) = pollster::block_on(painter.set_window(viewport_id, Some(&window)))
let window = Arc::new(window);

if let Err(err) =
pollster::block_on(painter.set_window(viewport_id, Some(window.clone())))
{
log::error!("on set_window: viewport_id {viewport_id:?} {err}");
}
Expand All @@ -813,7 +819,7 @@ impl Viewport {
self.info.minimized = window.is_minimized();
self.info.maximized = Some(window.is_maximized());

self.window = Some(Rc::new(window));
self.window = Some(window);
}
Err(err) => {
log::error!("Failed to create window: {err}");
Expand Down Expand Up @@ -930,7 +936,7 @@ fn render_immediate_viewport(

{
crate::profile_scope!("set_window");
if let Err(err) = pollster::block_on(painter.set_window(ids.this, Some(window))) {
if let Err(err) = pollster::block_on(painter.set_window(ids.this, Some(window.clone()))) {
log::error!(
"when rendering viewport_id={:?}, set_window Error {err}",
ids.this
Expand Down
4 changes: 2 additions & 2 deletions crates/eframe/src/native/winit_integration.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{rc::Rc, time::Instant};
use std::{sync::Arc, time::Instant};

use winit::{
event_loop::EventLoopWindowTarget,
Expand Down Expand Up @@ -68,7 +68,7 @@ pub trait WinitApp {

fn integration(&self) -> Option<&EpiIntegration>;

fn window(&self, window_id: WindowId) -> Option<Rc<Window>>;
fn window(&self, window_id: WindowId) -> Option<Arc<Window>>;

fn window_id_from_viewport_id(&self, id: ViewportId) -> Option<WindowId>;

Expand Down
Loading

0 comments on commit 5297bc2

Please sign in to comment.