From 9c74cd9eabb77381d5fd9048639c6dd13c29759a Mon Sep 17 00:00:00 2001 From: PolyMeilex Date: Wed, 21 Feb 2024 20:28:20 +0100 Subject: [PATCH] WGPU create surface no longer requires unsafe, we can just Arc the window --- neothesia/src/main.rs | 12 +++++++++--- neothesia/src/target.rs | 6 ++++-- wgpu-jumpstart/src/gpu.rs | 10 +++------- 3 files changed, 16 insertions(+), 12 deletions(-) diff --git a/neothesia/src/main.rs b/neothesia/src/main.rs index 27fe0ae5..520b23a3 100644 --- a/neothesia/src/main.rs +++ b/neothesia/src/main.rs @@ -8,6 +8,7 @@ mod song; mod target; mod utils; +use std::sync::Arc; use std::time::Duration; use iced_core::Renderer; @@ -304,9 +305,14 @@ fn init(builder: winit::window::WindowBuilder) -> (EventLoop, Ta }); let size = window.inner_size(); - let (gpu, surface) = - futures::executor::block_on(Gpu::for_window(&instance, &window, size.width, size.height)) - .unwrap(); + let window = Arc::new(window); + let (gpu, surface) = futures::executor::block_on(Gpu::for_window( + &instance, + window.clone(), + size.width, + size.height, + )) + .unwrap(); let target = Target::new(window, window_state, proxy, gpu); diff --git a/neothesia/src/target.rs b/neothesia/src/target.rs index a74eb768..5b56ef44 100644 --- a/neothesia/src/target.rs +++ b/neothesia/src/target.rs @@ -1,3 +1,5 @@ +use std::sync::Arc; + use crate::config::Config; use crate::input_manager::InputManager; use crate::render::TextRenderer; @@ -11,7 +13,7 @@ use crate::iced_utils::IcedManager; use winit::window::Window; pub struct Target { - pub window: Window, + pub window: Arc, pub iced_manager: IcedManager, pub window_state: WindowState, @@ -31,7 +33,7 @@ pub struct Target { impl Target { pub fn new( - window: Window, + window: Arc, window_state: WindowState, proxy: EventLoopProxy, gpu: Gpu, diff --git a/wgpu-jumpstart/src/gpu.rs b/wgpu-jumpstart/src/gpu.rs index 22f08fa2..89106197 100644 --- a/wgpu-jumpstart/src/gpu.rs +++ b/wgpu-jumpstart/src/gpu.rs @@ -1,5 +1,3 @@ -use raw_window_handle::{HasDisplayHandle, HasWindowHandle}; - use super::color::Color; use super::GpuInitError; @@ -17,15 +15,13 @@ pub struct Gpu { } impl Gpu { - pub async fn for_window( + pub async fn for_window( instance: &wgpu::Instance, - window: &H, + window: impl Into>, width: u32, height: u32, ) -> Result<(Self, Surface), GpuInitError> { - let surface = unsafe { - instance.create_surface_unsafe(wgpu::SurfaceTargetUnsafe::from_window(window).unwrap()) - }?; + let surface = instance.create_surface(window.into())?; let gpu = Self::new(instance, Some(&surface)).await?; let surface = Surface::new(&gpu.device, surface, gpu.texture_format, width, height);