Skip to content

Commit

Permalink
Prepare update to wgpu 0.19
Browse files Browse the repository at this point in the history
  • Loading branch information
jinleili committed Jan 16, 2024
1 parent c216def commit e144c3f
Show file tree
Hide file tree
Showing 11 changed files with 609 additions and 547 deletions.
Binary file not shown.
866 changes: 447 additions & 419 deletions Cargo.lock

Large diffs are not rendered by default.

20 changes: 10 additions & 10 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,21 @@ default-members = ["wgpu-in-app"]

[workspace.dependencies]
bytemuck = { version = "1.14", features = ["derive"] }
glam = "0.24"
cfg-if = "1.0"
glam = "0.25"
log = "0.4"
noise = { version = "0.8", default-features = false }
pollster = "0.3"
rand = "0.7.2"
wgpu = "0.18"
# wgpu = { git = "https://github.com/gfx-rs/wgpu", rev = "757245cd" }
# wgpu = "0.18"
wgpu = { git = "https://github.com/gfx-rs/wgpu", rev = "445fa6019b47079c9d336881dbee1c3be3ed4c38" }
# wgpu = { path = "../../forks/wgpu/wgpu" }
# wgpu = { git = "https://github.com/jinleili/wgpu", branch="visionOS" }

async-executor = "1.6"
winit = "0.28.7"
raw-window-handle = "0.5"
# winit = "0.28.7"
winit = { version = "0.29.10" }
raw-window-handle = "0.6"
env_logger = "0.10"

# macOS, iOS
Expand All @@ -33,9 +35,7 @@ ndk-sys = "0.4.1"
ash = "0.37.3"

# wasm target
web-sys = "0.3.64"
web-sys = "0.3.67"
wasm-bindgen = "0.2.87"
js-sys = "0.3.64"
wasm-bindgen-futures = "0.4.34"
# parking_lot 0.12 switches from `winapi` to `windows`; permit either
parking_lot = ">=0.11,<0.13"
js-sys = "0.3.67"
wasm-bindgen-futures = "0.4.40"
1 change: 1 addition & 0 deletions app-surface/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ default = []
webgl = ["wgpu/webgl"]

[dependencies]
cfg-if.workspace = true
log.workspace = true
glam.workspace = true
pollster.workspace = true
Expand Down
7 changes: 4 additions & 3 deletions app-surface/src/android.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,15 @@ impl Drop for NativeWindow {

unsafe impl HasRawWindowHandle for NativeWindow {
fn raw_window_handle(&self) -> RawWindowHandle {
let mut handle = AndroidNdkWindowHandle::empty();
handle.a_native_window = self.a_native_window as *mut _ as *mut c_void;
let mut handle = AndroidNdkWindowHandle::new(NonNull::new(
self.a_native_window as *mut _ as *mut c_void,
));
RawWindowHandle::AndroidNdk(handle)
}
}

unsafe impl HasRawDisplayHandle for NativeWindow {
fn raw_display_handle(&self) -> RawDisplayHandle {
RawDisplayHandle::Android(AndroidDisplayHandle::empty())
RawDisplayHandle::Android(AndroidDisplayHandle::new())
}
}
109 changes: 86 additions & 23 deletions app-surface/src/app_surface.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use std::sync::Arc;
use winit::window::Window;

pub struct AppSurface {
pub view: winit::window::Window,
pub view: Option<Arc<Window>>,
pub is_offscreen_canvas: bool,
pub scale_factor: f32,
pub maximum_frames: i32,
pub sdq: crate::SurfaceDeviceQueue,
Expand All @@ -10,9 +12,57 @@ pub struct AppSurface {
pub library_directory: &'static str,
}

#[derive(Default)]
struct ViewSetting {
view: Option<Window>,
scale_factor: f32,
physical_size: (u32, u32),
#[cfg(target_arch = "wasm32")]
offscreen_canvas: Option<web_sys::OffscreenCanvas>,
}

impl AppSurface {
pub async fn new(view: winit::window::Window) -> Self {
let scale_factor = view.scale_factor();
#[allow(clippy::needless_update)]
pub async fn new(view: Window) -> Self {
let scale_factor = view.scale_factor() as f32;
let physical_size = view.inner_size();
let view_setting = ViewSetting {
scale_factor,
physical_size: (physical_size.width, physical_size.height),
view: Some(view),
..Default::default()
};

Self::create(view_setting).await
}

#[cfg(target_arch = "wasm32")]
pub async fn from_offscreen_canvas(
offscreen_canvas: web_sys::OffscreenCanvas,
scale_factor: f32,
physical_size: (u32, u32),
) -> Self {
let view_setting = ViewSetting {
scale_factor,
physical_size,
offscreen_canvas: Some(offscreen_canvas),
..Default::default()
};
Self::create(view_setting).await
}

#[allow(unused_variables)]
async fn create(view_setting: ViewSetting) -> Self {
let view = Arc::new(view_setting.view.unwrap());
#[cfg(not(target_arch = "wasm32"))]
let is_offscreen_canvas = false;
#[cfg(target_arch = "wasm32")]
let is_offscreen_canvas = if view_setting.offscreen_canvas.is_some() {
true
} else {
false
};
let scale_factor = view_setting.scale_factor;
let default_backends = if cfg!(feature = "webgl") {
wgpu::Backends::GL
} else {
Expand All @@ -23,20 +73,27 @@ impl AppSurface {
backends,
..Default::default()
});
let physical = view.inner_size();
let surface = unsafe {
#[cfg(target_arch = "wasm32")]
let surface = {
use winit::platform::web::WindowExtWebSys;
instance.create_surface_from_canvas(view.canvas())
};
#[cfg(not(target_arch = "wasm32"))]
let surface = instance.create_surface(&view);
match surface {
Ok(surface) => surface,
Err(e) => {
panic!("Failed to create surface: {e:?}");
}
cfg_if::cfg_if! {
if #[cfg(target_arch = "wasm32")] {
let surface = if is_offscreen_canvas {
// let offscreen = canvas.transfer_control_to_offscreen().unwrap();
instance.create_surface_from_offscreen_canvas(
view_setting.offscreen_canvas.unwrap(),
)
} else {
use winit::platform::web::WindowExtWebSys;
let canvas: web_sys::HtmlCanvasElement =
view.as_ref().canvas().unwrap();
instance.create_surface_from_canvas(canvas)
};
} else {
let surface = instance.create_surface(view.clone());
}
}
let surface = match surface {
Ok(surface) => surface,
Err(e) => {
panic!("Failed to create surface: {e:?}");
}
};

Expand Down Expand Up @@ -73,20 +130,22 @@ impl AppSurface {
vec![format.add_srgb_suffix(), format.remove_srgb_suffix()]
};

let physical = view_setting.physical_size;
let config = wgpu::SurfaceConfiguration {
usage: wgpu::TextureUsages::RENDER_ATTACHMENT,
format,
width: physical.width,
height: physical.height,
width: physical.0,
height: physical.1,
present_mode: wgpu::PresentMode::Fifo,
alpha_mode,
view_formats,
};
surface.configure(&device, &config);

AppSurface {
view,
scale_factor: scale_factor as f32,
view: Some(view),
is_offscreen_canvas,
scale_factor,
maximum_frames: 60,
sdq: crate::SurfaceDeviceQueue {
surface,
Expand All @@ -102,7 +161,11 @@ impl AppSurface {
}

pub fn get_view_size(&self) -> (u32, u32) {
let physical = self.view.inner_size();
(physical.width, physical.height)
if self.is_offscreen_canvas {
panic!("Offscreen canvas cannot provide any DOM interfaces.");
} else {
let physical = self.view.as_ref().unwrap().inner_size();
(physical.width, physical.height)
}
}
}
8 changes: 7 additions & 1 deletion app-surface/src/ios.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,13 @@ impl AppSurface {
backends,
..Default::default()
});
let surface = unsafe { instance.create_surface_from_core_animation_layer(obj.metal_layer) };
let surface = unsafe {
instance
.create_surface_unsafe(wgpu::SurfaceTargetUnsafe::CoreAnimationLayer(
obj.metal_layer,
))
.expect("Surface creation failed")
};
let (adapter, device, queue) =
pollster::block_on(crate::request_device(&instance, &surface));
let caps = surface.get_capabilities(&adapter);
Expand Down
17 changes: 13 additions & 4 deletions app-surface/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub struct ViewSize {
}

pub struct SurfaceDeviceQueue {
pub surface: wgpu::Surface,
pub surface: wgpu::Surface<'static>,
pub config: wgpu::SurfaceConfiguration,
pub adapter: wgpu::Adapter,
pub device: Arc<wgpu::Device>,
Expand Down Expand Up @@ -47,6 +47,8 @@ pub trait SurfaceFrame {
fn view_size(&self) -> ViewSize;
// After App view's size or orientation changed, need to resize surface.
fn resize_surface(&mut self);
#[cfg(target_arch = "wasm32")]
fn resize_surface_by_size(&mut self, size: (u32, u32));
fn pintch(&mut self, _touch: Touch, _scale: f32) {}
fn touch(&mut self, _touch: Touch) {}
fn normalize_touch_point(&self, _touch_point_x: f32, _touch_point_y: f32) -> (f32, f32) {
Expand Down Expand Up @@ -105,6 +107,13 @@ impl SurfaceFrame for AppSurface {
self.surface.configure(&self.device, &self.config);
}

#[cfg(target_arch = "wasm32")]
fn resize_surface_by_size(&mut self, size: (u32, u32)) {
self.sdq.config.width = size.0;
self.sdq.config.height = size.1;
self.surface.configure(&self.device, &self.config);
}

fn normalize_touch_point(&self, touch_point_x: f32, touch_point_y: f32) -> (f32, f32) {
let size = self.get_view_size();
(
Expand All @@ -124,7 +133,7 @@ impl SurfaceFrame for AppSurface {
#[allow(unused)]
async fn request_device(
instance: &Instance,
surface: &Surface,
surface: &Surface<'static>,
) -> (wgpu::Adapter, wgpu::Device, wgpu::Queue) {
let adapter = instance
.request_adapter(&wgpu::RequestAdapterOptions {
Expand All @@ -147,8 +156,8 @@ async fn request_device(
.request_device(
&wgpu::DeviceDescriptor {
label: None,
features: adapter.features(),
limits: adapter.limits(),
required_features: adapter.features(),
required_limits: adapter.limits(),
},
None,
)
Expand Down
4 changes: 1 addition & 3 deletions wgpu-in-app/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,4 @@ web-sys = { workspace = true, features = [
] }
wasm-bindgen.workspace = true
js-sys.workspace = true
wasm-bindgen-futures.workspace = true
# parking_lot 0.12 switches from `winapi` to `windows`; permit either
parking_lot.workspace = true
wasm-bindgen-futures.workspace = true
9 changes: 8 additions & 1 deletion wgpu-in-app/src/examples/hdr_image_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ impl HDRImageView {
label: None,
view_formats: &[],
},
wgpu::util::TextureDataOrder::MipMajor,
&astc_data[16..],
);
let texture_view = texture.create_view(&wgpu::TextureViewDescriptor {
Expand Down Expand Up @@ -105,9 +106,15 @@ impl HDRImageView {
});

let pipeline_vertex_buffers = [];
let render_pipeline_layout =
device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
label: Some("render"),
bind_group_layouts: &[&bind_group_layout],
push_constant_ranges: &[],
});
let pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
label: Some("bufferless pipeline"),
layout: None,
layout: Some(&render_pipeline_layout),
vertex: wgpu::VertexState {
module: &shader_module,
entry_point: "vs_main",
Expand Down
Loading

0 comments on commit e144c3f

Please sign in to comment.