Skip to content

Commit

Permalink
Update winit to v0.30
Browse files Browse the repository at this point in the history
  • Loading branch information
jinleili committed Aug 4, 2024
1 parent 91ed41f commit 111e256
Show file tree
Hide file tree
Showing 12 changed files with 816 additions and 465 deletions.
Binary file not shown.
1,023 changes: 647 additions & 376 deletions Cargo.lock

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ log = "0.4"
noise = { version = "0.8", default-features = false }
pollster = "0.3"
rand = "0.7.2"
wgpu = "22.0"
wgpu = "22.1"
# wgpu = { git = "https://github.com/gfx-rs/wgpu", rev = "445fa6019b47079c9d336881dbee1c3be3ed4c38" }
# wgpu = { git = "https://github.com/jinleili/wgpu", branch="visionOS" }
winit = { version = "0.29.15" }
winit = { version = "=0.30.2" }
web-time = "1"
raw-window-handle = "0.6"
env_logger = "0.11"

Expand All @@ -27,7 +28,7 @@ core-graphics = "0.23.1"
android_logger = "0.14"
jni = "0.21"
jni_fn = "0.1"
ndk-sys = "0.4.1"
ndk-sys = "0.6"
ash = "0.38"

# wasm target
Expand Down
4 changes: 2 additions & 2 deletions app-surface/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "app-surface"
authors = ["jinleili"]
description = "Integrate wgpu into your existing iOS and Android apps."
edition = "2021"
version = "1.0.0"
version = "1.1.0"
rust-version = "1.76"
repository = "https://github.com/jinleili/wgpu-in-app"
keywords = ["android", "SurfaceView", "ios", "macos", "wgpu"]
Expand Down Expand Up @@ -39,10 +39,10 @@ android_logger.workspace = true
jni.workspace = true
ndk-sys.workspace = true
raw-window-handle.workspace = true
# hal = { workspace = true, features = ["renderdoc", "vulkan"] }
ash.workspace = true

[target.'cfg(target_arch = "wasm32")'.dependencies]
winit.workspace = true
web-sys = { workspace = true, features = ["Document", "Window", "Location"] }
wasm-bindgen.workspace = true
web-time.workspace = true
4 changes: 2 additions & 2 deletions app-surface/src/android.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ impl AppSurface {
native_window,
scale_factor: 1.0,
sdq: crate::SurfaceDeviceQueue {
surface,
surface: Arc::new(surface),
config,
adapter,
adapter: Arc::new(adapter),
device: Arc::new(device),
queue: Arc::new(queue),
},
Expand Down
21 changes: 16 additions & 5 deletions app-surface/src/app_surface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub struct AppSurface {
pub is_offscreen_canvas: bool,
pub scale_factor: f32,
pub maximum_frames: i32,
pub instance: Arc<wgpu::Instance>,
pub sdq: crate::SurfaceDeviceQueue,
pub callback_to_app: Option<extern "C" fn(arg: i32)>,
pub temporary_directory: &'static str,
Expand All @@ -14,7 +15,7 @@ pub struct AppSurface {

#[derive(Default)]
struct ViewSetting {
view: Option<Window>,
view: Option<Arc<Window>>,
scale_factor: f32,
physical_size: (u32, u32),
#[cfg(target_arch = "wasm32")]
Expand All @@ -23,7 +24,7 @@ struct ViewSetting {

impl AppSurface {
#[allow(clippy::needless_update)]
pub async fn new(view: Window) -> Self {
pub async fn new(view: Arc<Window>) -> Self {
let scale_factor = view.scale_factor() as f32;
let mut physical_size = view.inner_size();
physical_size.width = physical_size.width.max(1);
Expand Down Expand Up @@ -59,7 +60,7 @@ impl AppSurface {

#[allow(unused_variables)]
async fn create(view_setting: ViewSetting) -> Self {
let view = Arc::new(view_setting.view.unwrap());
let view = view_setting.view.unwrap();
#[cfg(not(target_arch = "wasm32"))]
let is_offscreen_canvas = false;
#[cfg(target_arch = "wasm32")]
Expand All @@ -74,6 +75,7 @@ impl AppSurface {
} else {
wgpu::Backends::PRIMARY
};
log::info!("{:?}", default_backends);
let backends = wgpu::util::backend_bits_from_env().unwrap_or(default_backends);
let instance = wgpu::Instance::new(wgpu::InstanceDescriptor {
backends,
Expand Down Expand Up @@ -156,10 +158,11 @@ impl AppSurface {
is_offscreen_canvas,
scale_factor,
maximum_frames: 60,
instance: Arc::new(instance),
sdq: crate::SurfaceDeviceQueue {
surface,
surface: Arc::new(surface),
config,
adapter,
adapter: Arc::new(adapter),
device: Arc::new(device),
queue: Arc::new(queue),
},
Expand All @@ -177,4 +180,12 @@ impl AppSurface {
(physical.width.max(1), physical.height.max(1))
}
}

pub fn request_redraw(&self) {
self.view.as_ref().unwrap().request_redraw();
}

pub fn pre_present_notify(&self) {
self.view.as_ref().unwrap().pre_present_notify();
}
}
4 changes: 2 additions & 2 deletions app-surface/src/ios.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ impl AppSurface {
view: obj.view,
scale_factor,
sdq: crate::SurfaceDeviceQueue {
surface,
surface: Arc::new(surface),
config,
adapter,
adapter: Arc::new(adapter),
device: Arc::new(device),
queue: Arc::new(queue),
},
Expand Down
4 changes: 2 additions & 2 deletions app-surface/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ pub struct ViewSize {
}

pub struct SurfaceDeviceQueue {
pub surface: wgpu::Surface<'static>,
pub surface: Arc<wgpu::Surface<'static>>,
pub config: wgpu::SurfaceConfiguration,
pub adapter: wgpu::Adapter,
pub adapter: Arc<wgpu::Adapter>,
pub device: Arc<wgpu::Device>,
pub queue: Arc<wgpu::Queue>,
}
Expand Down
2 changes: 1 addition & 1 deletion wgpu-in-app/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "wgpu-in-app"
authors = ["jinleili"]
version = "1.0.0"
version = "1.1.0"
edition = "2021"
rust-version = "1.76"

Expand Down
132 changes: 132 additions & 0 deletions wgpu-in-app/src/desktop.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
use std::thread;

use app_surface::AppSurface;
use std::sync::Arc;
use std::time;
use crate::WgpuCanvas;

use winit::{
application::ApplicationHandler,
event::{ElementState, KeyEvent, StartCause, WindowEvent},
event_loop::{ActiveEventLoop, ControlFlow, EventLoop},
keyboard::{Key, KeyCode, NamedKey, PhysicalKey},
window::{Window, WindowId},
};

pub fn run() -> Result<(), impl std::error::Error> {
let events_loop = EventLoop::new().unwrap();
let mut app = WgpuApp::default();
events_loop.run_app(&mut app)
}

const WAIT_TIME: time::Duration = time::Duration::from_millis(16);
const POLL_SLEEP_TIME: time::Duration = time::Duration::from_millis(16);

#[derive(Default, Debug, Clone, Copy, PartialEq, Eq)]
enum Mode {
Wait,
WaitUntil,
#[default]
Poll,
}

#[derive(Default)]
struct WgpuApp {
mode: Mode,
wait_cancelled: bool,
close_requested: bool,
canvas: Option<WgpuCanvas>,
}

impl WgpuApp {
fn get_canvas(&mut self) -> &mut WgpuCanvas {
self.canvas.as_mut().unwrap()
}
}

impl ApplicationHandler for WgpuApp {
fn new_events(&mut self, _event_loop: &ActiveEventLoop, cause: StartCause) {
self.wait_cancelled = match cause {
StartCause::WaitCancelled { .. } => self.mode == Mode::WaitUntil,
_ => false,
}
}

fn resumed(&mut self, event_loop: &ActiveEventLoop) {
if self.canvas.is_some() {
return;
}

let window_attributes = Window::default_attributes().with_title("Wgpu on Desktop");
let window = Arc::new(event_loop.create_window(window_attributes).unwrap());

let app_view = pollster::block_on(AppSurface::new(window));

self.canvas = Some(WgpuCanvas::new(app_view, 0));
self.get_canvas().app_surface.request_redraw();
}

fn window_event(
&mut self,
_event_loop: &ActiveEventLoop,
_window_id: WindowId,
event: WindowEvent,
) {
match event {
WindowEvent::CloseRequested => {
self.close_requested = true;
}
WindowEvent::Resized(size) => {
if size.width == 0 || size.height == 0 {
println!("Window minimized!");
} else {
self.get_canvas().resize();
}
}
WindowEvent::KeyboardInput {
event:
KeyEvent {
physical_key: PhysicalKey::Code(key),
state: ElementState::Pressed,
..
},
..
} => match key {
KeyCode::Digit1 => self.get_canvas().change_example(1),
KeyCode::Digit2 => self.get_canvas().change_example(2),
KeyCode::Digit3 => self.get_canvas().change_example(3),
KeyCode::Digit4 => self.get_canvas().change_example(4),
KeyCode::Digit5 => self.get_canvas().change_example(5),
_ => self.get_canvas().change_example(0),
},
WindowEvent::RedrawRequested => {
self.get_canvas().app_surface.pre_present_notify();

self.get_canvas().enter_frame();

self.get_canvas().app_surface.request_redraw();
}
_ => (),
}
}

fn about_to_wait(&mut self, event_loop: &ActiveEventLoop) {
match self.mode {
Mode::Wait => event_loop.set_control_flow(ControlFlow::Wait),
Mode::WaitUntil => {
if !self.wait_cancelled {
event_loop
.set_control_flow(ControlFlow::WaitUntil(time::Instant::now() + WAIT_TIME));
}
}
Mode::Poll => {
thread::sleep(POLL_SLEEP_TIME);
event_loop.set_control_flow(ControlFlow::Poll);
}
};

if self.close_requested {
event_loop.exit();
}
}
}
3 changes: 3 additions & 0 deletions wgpu-in-app/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ mod ffi;

#[cfg(all(target_os = "android", target_os = "ios"))]
pub use ffi::*;

#[cfg(any(target_os = "macos", target_os = "windows", target_os = "linux"))]
pub mod desktop;
77 changes: 5 additions & 72 deletions wgpu-in-app/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,76 +1,9 @@
#![allow(clippy::single_match)]

#[cfg(any(target_os = "android", target_os = "ios"))]
fn main() {}

#[cfg(all(
not(target_os = "android"),
not(target_os = "ios"),
not(target_arch = "wasm32")
))]
fn main() {
use app_surface::AppSurface;
use wgpu_in_app::WgpuCanvas;
use winit::{
event::{ElementState, Event, KeyEvent, WindowEvent},
event_loop::EventLoop,
keyboard::{Key, KeyCode, NamedKey, PhysicalKey},
};

let events_loop = EventLoop::new().unwrap();
let size = winit::dpi::Size::Logical(winit::dpi::LogicalSize {
width: 1200.0,
height: 800.0,
});
let builder = winit::window::WindowBuilder::new()
.with_inner_size(size)
.with_max_inner_size(size)
.with_transparent(true)
.with_title("wgpu on Desktop");
let window = builder.build(&events_loop).unwrap();

let app_view = pollster::block_on(AppSurface::new(window));
let mut canvas = WgpuCanvas::new(app_view, 0);

let _ = events_loop.run(move |event, elwt| {
if let Event::WindowEvent { event, .. } = event {
match event {
WindowEvent::KeyboardInput {
event:
KeyEvent {
logical_key: Key::Named(NamedKey::Escape),
..
},
..
}
| WindowEvent::CloseRequested => elwt.exit(),
WindowEvent::Resized(size) => {
if size.width == 0 || size.height == 0 {
println!("Window minimized!");
} else {
canvas.resize();
}
}
WindowEvent::KeyboardInput {
event:
KeyEvent {
physical_key: PhysicalKey::Code(key),
state: ElementState::Pressed,
..
},
..
} => match key {
KeyCode::Digit1 => canvas.change_example(1),
KeyCode::Digit2 => canvas.change_example(2),
KeyCode::Digit3 => canvas.change_example(3),
KeyCode::Digit4 => canvas.change_example(4),
KeyCode::Digit5 => canvas.change_example(5),
_ => canvas.change_example(0),
},
WindowEvent::RedrawRequested => {
canvas.enter_frame();
canvas.app_surface.view.as_ref().unwrap().request_redraw();
}
_ => (),
}
}
});
#[cfg(any(target_os = "macos", target_os = "windows", target_os = "linux"))]
fn main() -> Result<(), impl std::error::Error> {
wgpu_in_app::desktop::run()
}

0 comments on commit 111e256

Please sign in to comment.