Skip to content

Commit

Permalink
Update to wgpu v23
Browse files Browse the repository at this point in the history
  • Loading branch information
jinleili committed Oct 31, 2024
1 parent 8172108 commit 01b6904
Show file tree
Hide file tree
Showing 20 changed files with 358 additions and 358 deletions.
Binary file not shown.
340 changes: 173 additions & 167 deletions Cargo.lock

Large diffs are not rendered by default.

18 changes: 9 additions & 9 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,25 @@ members = ["wgpu-in-app", "app-surface", "cargo-so"]
default-members = ["wgpu-in-app"]

[workspace.dependencies]
bytemuck = { version = "1.16", features = ["derive"] }
bytemuck = { version = "1.19", features = ["derive"] }
cfg-if = "1.0"
glam = "0.28"
glam = "0.29"
log = "0.4"
noise = { version = "0.8", default-features = false }
pollster = "0.3"
rand = "0.7.2"
wgpu = "22.1"
wgpu = "23"
# wgpu = { git = "https://github.com/gfx-rs/wgpu", rev = "445fa6019b47079c9d336881dbee1c3be3ed4c38" }
# wgpu = { git = "https://github.com/jinleili/wgpu", branch="visionOS" }
winit = { version = "=0.30.2" }
winit = { version = "=0.30.5" }
web-time = "1"
raw-window-handle = "0.6"
env_logger = "0.11"

# macOS, iOS
libc = "0.2"
objc = "0.2.7"
core-graphics = "0.23.1"
core-graphics = "0.24"

# android target
android_logger = "0.14"
Expand All @@ -32,7 +32,7 @@ ndk-sys = "0.6"
ash = "0.38"

# wasm target
web-sys = "0.3.69"
wasm-bindgen = "0.2.87"
js-sys = "0.3.69"
wasm-bindgen-futures = "0.4.42"
web-sys = "0.3.72"
wasm-bindgen = "0.2.95"
js-sys = "0.3.72"
wasm-bindgen-futures = "0.4.45"
2 changes: 1 addition & 1 deletion 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.1.0"
version = "1.2.0"
rust-version = "1.76"
repository = "https://github.com/jinleili/wgpu-in-app"
keywords = ["android", "SurfaceView", "ios", "macos", "wgpu"]
Expand Down
33 changes: 8 additions & 25 deletions app-surface/src/android.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ use std::sync::{Arc, Mutex};
pub struct AppSurface {
pub native_window: Arc<NativeWindow>,
pub scale_factor: f32,
pub sdq: crate::SurfaceDeviceQueue,
pub instance: wgpu::Instance,
pub ctx: crate::IASDQContext,
pub callback_to_app: Option<extern "C" fn(arg: i32)>,
}

Expand All @@ -23,38 +22,22 @@ impl AppSurface {
backends,
..Default::default()
});

let handle: Box<dyn wgpu::WindowHandle> = Box::new(native_window.clone());
let surface = instance
.create_surface(wgpu::SurfaceTarget::Window(handle))
.unwrap();
let (adapter, device, queue) =
pollster::block_on(crate::request_device(&instance, &surface));

let caps = surface.get_capabilities(&adapter);

let config = wgpu::SurfaceConfiguration {
usage: wgpu::TextureUsages::RENDER_ATTACHMENT,
format: wgpu::TextureFormat::Rgba8UnormSrgb,
width: native_window.get_width(),
height: native_window.get_height(),
present_mode: wgpu::PresentMode::Fifo,
alpha_mode: caps.alpha_modes[0],
view_formats: vec![],
desired_maximum_frame_latency: 2,
};
surface.configure(&device, &config);
let ctx = pollster::block_on(crate::create_iasdq_context(
instance,
surface,
(native_window.get_width(), native_window.get_height()),
));

Self {
native_window,
scale_factor: 1.0,
sdq: crate::SurfaceDeviceQueue {
surface: Arc::new(surface),
config,
adapter: Arc::new(adapter),
device: Arc::new(device),
queue: Arc::new(queue),
},
instance,
ctx,
callback_to_app: None,
}
}
Expand Down
58 changes: 3 additions & 55 deletions app-surface/src/app_surface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +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 ctx: crate::IASDQContext,
pub callback_to_app: Option<extern "C" fn(arg: i32)>,
pub temporary_directory: &'static str,
pub library_directory: &'static str,
Expand Down Expand Up @@ -107,65 +106,14 @@ impl AppSurface {
}
};

let (adapter, device, queue) = crate::request_device(&instance, &surface).await;
let caps = surface.get_capabilities(&adapter);

let modes = caps.alpha_modes;
let alpha_mode = if modes.contains(&wgpu::CompositeAlphaMode::PreMultiplied) {
// wasm can only support this alpha mode
wgpu::CompositeAlphaMode::PreMultiplied
} else if modes.contains(&wgpu::CompositeAlphaMode::PostMultiplied) {
// Metal alpha mode
wgpu::CompositeAlphaMode::PostMultiplied
} else if modes.contains(&wgpu::CompositeAlphaMode::Inherit) {
// Vulkan on Android
wgpu::CompositeAlphaMode::Inherit
} else {
modes[0]
};
let prefered = caps.formats[0];
let format = if cfg!(all(target_arch = "wasm32", not(feature = "webgl"))) {
// Chrome WebGPU doesn't support sRGB:
// unsupported swap chain format "xxxx8unorm-srgb"
prefered.remove_srgb_suffix()
} else {
prefered
};
let view_formats = if cfg!(feature = "webgl") {
// panicked at 'Error in Surface::configure: Validation Error
// Caused by:
// Downlevel flags DownlevelFlags(SURFACE_VIEW_FORMATS) are required but not supported on the device.
vec![]
} else {
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.0,
height: physical.1,
present_mode: wgpu::PresentMode::Fifo,
alpha_mode,
view_formats,
desired_maximum_frame_latency: 2,
};
surface.configure(&device, &config);
let ctx = crate::create_iasdq_context(instance, surface, view_setting.physical_size).await;

AppSurface {
view: Some(view),
is_offscreen_canvas,
scale_factor,
maximum_frames: 60,
instance: Arc::new(instance),
sdq: crate::SurfaceDeviceQueue {
surface: Arc::new(surface),
config,
adapter: Arc::new(adapter),
device: Arc::new(device),
queue: Arc::new(queue),
},
ctx,
callback_to_app: None,
temporary_directory: "",
library_directory: "",
Expand Down
43 changes: 6 additions & 37 deletions app-surface/src/ios.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub struct IOSViewObj {
pub struct AppSurface {
pub view: *mut Object,
pub scale_factor: f32,
pub sdq: crate::SurfaceDeviceQueue,
pub ctx: crate::IASDQContext,
pub maximum_frames: i32,
pub callback_to_app: Option<extern "C" fn(arg: i32)>,
pub temporary_directory: &'static str,
Expand Down Expand Up @@ -52,34 +52,13 @@ impl AppSurface {
))
.expect("Surface creation failed")
};
let (adapter, device, queue) =
pollster::block_on(crate::request_device(&instance, &surface));
let caps = surface.get_capabilities(&adapter);
let config = wgpu::SurfaceConfiguration {
usage: wgpu::TextureUsages::RENDER_ATTACHMENT,
// CAMatalLayer's pixel format default value is MTLPixelFormatBGRA8Unorm.
// https://developer.apple.com/documentation/quartzcore/cametallayer/1478155-pixelformat?language=objc
// format: wgpu::TextureFormat::Bgra8Unorm,
// format: surface.get_supported_formats(&adapter)[0],
format: caps.formats[0],
width: physical.0,
height: physical.1,
present_mode: wgpu::PresentMode::Fifo,
alpha_mode: wgpu::CompositeAlphaMode::PostMultiplied,
view_formats: vec![],
desired_maximum_frame_latency: 2,
};
surface.configure(&device, &config);

let ctx = pollster::block_on(crate::create_iasdq_context(instance, surface, physical));

AppSurface {
view: obj.view,
scale_factor,
sdq: crate::SurfaceDeviceQueue {
surface: Arc::new(surface),
config,
adapter: Arc::new(adapter),
device: Arc::new(device),
queue: Arc::new(queue),
},
ctx,
callback_to_app: Some(obj.callback_to_swift),
maximum_frames: obj.maximum_frames,
temporary_directory: "",
Expand All @@ -98,18 +77,8 @@ impl AppSurface {

fn get_scale_factor(obj: *mut Object) -> f32 {
let mut _scale_factor: CGFloat = 1.0;
#[cfg(target_os = "macos")]
unsafe {
let window: *mut Object = msg_send![obj, window];
if !window.is_null() {
_scale_factor = msg_send![window, backingScaleFactor];
}
};

#[cfg(target_os = "ios")]
{
_scale_factor = unsafe { msg_send![obj, contentScaleFactor] };
}
_scale_factor = unsafe { msg_send![obj, contentScaleFactor] };

_scale_factor as f32
}
69 changes: 62 additions & 7 deletions app-surface/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,16 @@ pub struct ViewSize {
pub height: u32,
}

pub struct SurfaceDeviceQueue {
pub struct IASDQContext {
pub instance: Arc<wgpu::Instance>,
pub surface: Arc<wgpu::Surface<'static>>,
pub config: wgpu::SurfaceConfiguration,
pub adapter: Arc<wgpu::Adapter>,
pub device: Arc<wgpu::Device>,
pub queue: Arc<wgpu::Queue>,
}

impl SurfaceDeviceQueue {
impl IASDQContext {
pub fn update_config_format(&mut self, format: wgpu::TextureFormat) {
self.config.format = format;
if cfg!(feature = "webgl") {
Expand All @@ -38,9 +39,9 @@ impl SurfaceDeviceQueue {
}

impl Deref for AppSurface {
type Target = SurfaceDeviceQueue;
type Target = IASDQContext;
fn deref(&self) -> &Self::Target {
&self.sdq
&self.ctx
}
}

Expand Down Expand Up @@ -103,8 +104,8 @@ impl SurfaceFrame for AppSurface {

fn resize_surface(&mut self) {
let size = self.get_view_size();
self.sdq.config.width = size.0;
self.sdq.config.height = size.1;
self.ctx.config.width = size.0;
self.ctx.config.height = size.1;
self.surface.configure(&self.device, &self.config);
}

Expand All @@ -131,7 +132,57 @@ impl SurfaceFrame for AppSurface {
}
}

#[allow(unused)]
async fn create_iasdq_context(
instance: Instance,
surface: Surface<'static>,
physical_size: (u32, u32),
) -> IASDQContext {
let (adapter, device, queue) = crate::request_device(&instance, &surface).await;

let caps = surface.get_capabilities(&adapter);
let prefered = caps.formats[0];

let format = if cfg!(all(target_arch = "wasm32", not(feature = "webgl"))) {
// Chrome WebGPU doesn't support sRGB:
// unsupported swap chain format "xxxx8unorm-srgb"
prefered.remove_srgb_suffix()
} else {
prefered
};
let view_formats = if cfg!(feature = "webgl") {
// panicked at 'Error in Surface::configure: Validation Error
// Caused by:
// Downlevel flags DownlevelFlags(SURFACE_VIEW_FORMATS) are required but not supported on the device.
vec![]
} else {
if format.is_srgb() {
// HarmonyOS 不支持 view_formats 格式
// format 的值与 view_formats 的值一致时,configure 内部会自动忽略 view_formats 的值
vec![format]
} else {
vec![format.add_srgb_suffix()]
}
};

let mut config = surface
.get_default_config(&adapter, physical_size.0, physical_size.1)
.expect("Surface isn't supported by the adapter.");

config.view_formats = view_formats;
config.format = format;

surface.configure(&device, &config);

IASDQContext {
instance: Arc::new(instance),
surface: Arc::new(surface),
config,
adapter: Arc::new(adapter),
device: Arc::new(device),
queue: Arc::new(queue),
}
}

async fn request_device(
instance: &Instance,
surface: &Surface<'static>,
Expand All @@ -145,14 +196,17 @@ async fn request_device(
})
.await
.expect("No suitable GPU adapters found on the system!");

let adapter_info = adapter.get_info();
println!("Using {} ({:?})", adapter_info.name, adapter_info.backend);

let base_dir = std::env::var("CARGO_MANIFEST_DIR");
let _trace_path = if let Ok(base_dir) = base_dir {
Some(std::path::PathBuf::from(&base_dir).join("WGPU_TRACE_ERROR"))
} else {
None
};

let res = adapter
.request_device(
&wgpu::DeviceDescriptor {
Expand All @@ -164,6 +218,7 @@ async fn request_device(
None,
)
.await;

match res {
Err(err) => {
panic!("request_device failed: {err:?}");
Expand Down
Loading

0 comments on commit 01b6904

Please sign in to comment.