wgpu_quick_start/sync_create.rs
1//! This is a module that contains a synchronous function to create a new `MySurface`.
2//!
3
4use super::MyDevice;
5
6use super::GPUStarterResult;
7
8#[cfg(feature = "enable-sync-winit")]
9use winit::window::Window;
10
11/// This synchronous function helps create a new surface from the given window.
12///
13/// Available with `sync` feature. (Hence, not available by default).
14///
15/// Can be enabled as below in the crate when being used.
16///
17/// ```toml
18/// wgpu_quick_start = { version="0.28.3", features = ["enable-sync-winit"] }
19/// ```
20///
21/// ## Usage
22///
23/// Following is an example of using `create_new_device` from a "winit" `Window`.
24///
25///
26/// ```rust
27/// use wgpu_quick_start::{MyDevice, create_new_device};
28/// use winit::{event::WindowEvent, window::WindowAttributes};
29/// use winit_app::{AppWindowEvent, Application};
30///
31///
32/// fn launch() -> Result<(), Box<dyn std::error::Error>> {
33/// let winit_app = Application::new();
34/// let mut opt_device: Option<Box<dyn MyDevice>> = None;
35/// winit_app.run(
36/// WindowAttributes::default().with_title("wgpu starter app"),
37/// move |app_window_event| match app_window_event {
38/// AppWindowEvent::NewWindow(window) => match create_new_device(window) {
39/// Ok(value) => {
40/// opt_device = Some(value);
41/// }
42/// Err(err) => {
43/// // warning - Error creating new surface from the window
44/// }
45/// },
46/// AppWindowEvent::OnWindowEvent(event, event_loop) => {
47/// if let Some(local_device) = opt_device.as_mut() {
48/// // Handle those events
49/// }
50/// }
51/// },
52/// )?;
53/// Ok(())
54/// }
55/// ```
56pub fn create_new_device(window: Box<dyn Window>) -> GPUStarterResult<Box<dyn MyDevice>> {
57 use crate::my_device::MyDeviceImpl;
58
59 let size = window.surface_size();
60 let device = pollster::block_on(MyDeviceImpl::new(window, (size.width, size.height)))?;
61 Ok(Box::new(device))
62}