Skip to content

Commit

Permalink
Update to wgpu v24
Browse files Browse the repository at this point in the history
  • Loading branch information
jinleili committed Jan 16, 2025
1 parent 77e0573 commit f777eb0
Show file tree
Hide file tree
Showing 14 changed files with 205 additions and 156 deletions.
Binary file not shown.
272 changes: 160 additions & 112 deletions Cargo.lock

Large diffs are not rendered by default.

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

[workspace.dependencies]
bytemuck = { version = "1.19", features = ["derive"] }
bytemuck = { version = "1.21", features = ["derive", "min_const_generics"] }
cfg-if = "1.0"
glam = "0.29"
log = "0.4"
noise = { version = "0.8", default-features = false }
pollster = "0.3"
rand = "0.7.2"
wgpu = "23"
wgpu = "24"
# wgpu = { git = "https://github.com/jinleili/wgpu", branch="visionOS" }
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.24"
core-graphics-types = "0.2"

# android target
android_logger = "0.14"
Expand All @@ -30,7 +29,7 @@ jni_fn = "0.1"
ndk-sys = "0.6"

# wasm target
web-sys = "0.3.76"
wasm-bindgen = "0.2.99"
js-sys = "0.3.76"
wasm-bindgen-futures = "0.4.49"
web-sys = "0.3.77"
wasm-bindgen = "0.2.100"
js-sys = "0.3.77"
wasm-bindgen-futures = "0.4.50"
7 changes: 3 additions & 4 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, Android and Web apps without relying on winit."
edition = "2021"
version = "1.3.3"
version = "1.4.0"
rust-version = "1.76"
repository = "https://github.com/jinleili/wgpu-in-app"
keywords = ["android", "SurfaceView", "CAMetalLayer", "Canvas", "wgpu"]
Expand Down Expand Up @@ -37,10 +37,10 @@ wasm-bindgen = { workspace = true, optional = true }
[target.'cfg(any(target_os = "macos", target_os = "windows", target_os = "linux"))'.dependencies]
winit.workspace = true

[target.'cfg(any(target_os = "macos", target_os = "ios"))'.dependencies]
[target.'cfg(target_vendor = "apple")'.dependencies]
libc.workspace = true
objc.workspace = true
core-graphics.workspace = true
core-graphics-types.workspace = true
env_logger.workspace = true
raw-window-handle.workspace = true

Expand All @@ -54,5 +54,4 @@ raw-window-handle.workspace = true
winit.workspace = true
web-sys = { workspace = true }
wasm-bindgen.workspace = true
web-time.workspace = true
raw-window-handle.workspace = true
2 changes: 1 addition & 1 deletion app-surface/src/android.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ impl AppSurface {
pub fn new(env: *mut JNIEnv, surface: jobject) -> Self {
let native_window = Arc::new(NativeWindow::new(env, surface));
let backends = wgpu::Backends::VULKAN;
let instance = wgpu::Instance::new(wgpu::InstanceDescriptor {
let instance = wgpu::Instance::new(&wgpu::InstanceDescriptor {
backends,
..Default::default()
});
Expand Down
4 changes: 2 additions & 2 deletions app-surface/src/app_surface_use_winit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ impl AppSurface {
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 {
let backends = wgpu::Backends::from_env().unwrap_or(default_backends);
let instance = wgpu::Instance::new(&wgpu::InstanceDescriptor {
backends,
..Default::default()
});
Expand Down
4 changes: 2 additions & 2 deletions app-surface/src/ios.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use core_graphics::{base::CGFloat, geometry::CGRect};
use core_graphics_types::{base::CGFloat, geometry::CGRect};
use libc::c_void;
use objc::{runtime::Object, *};
use std::marker::Sync;
Expand Down Expand Up @@ -40,7 +40,7 @@ impl AppSurface {
(s.size.height as f32 * scale_factor) as u32,
);
let backends = wgpu::Backends::METAL;
let instance = wgpu::Instance::new(wgpu::InstanceDescriptor {
let instance = wgpu::Instance::new(&wgpu::InstanceDescriptor {
backends,
..Default::default()
});
Expand Down
2 changes: 1 addition & 1 deletion app-surface/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ async fn request_device(
) -> (wgpu::Adapter, wgpu::Device, wgpu::Queue) {
let adapter = instance
.request_adapter(&wgpu::RequestAdapterOptions {
power_preference: wgpu::util::power_preference_from_env()
power_preference: wgpu::PowerPreference::from_env()
.unwrap_or(wgpu::PowerPreference::HighPerformance),
force_fallback_adapter: false,
compatible_surface: Some(surface),
Expand Down
2 changes: 1 addition & 1 deletion app-surface/src/web_rwh/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl AppSurface {
);

let backends = wgpu::Backends::BROWSER_WEBGPU;
let instance = wgpu::Instance::new(wgpu::InstanceDescriptor {
let instance = wgpu::Instance::new(&wgpu::InstanceDescriptor {
backends,
..Default::default()
});
Expand Down
2 changes: 1 addition & 1 deletion wgpu-in-app/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ env_logger.workspace = true
[target.'cfg(any(target_os = "macos", target_os = "windows", target_os = "linux"))'.dependencies]
winit.workspace = true

[target.'cfg(target_os = "ios")'.dependencies]
[target.'cfg(target_vendor = "apple")'.dependencies]
libc.workspace = true

[target.'cfg(target_os = "android")'.dependencies]
Expand Down
2 changes: 1 addition & 1 deletion wgpu-in-app/src/examples/cube.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ impl Cube {
queue.write_texture(
texture.as_image_copy(),
&texels,
wgpu::ImageDataLayout {
wgpu::TexelCopyBufferLayout {
offset: 0,
bytes_per_row: Some(size),
rows_per_image: None,
Expand Down
1 change: 1 addition & 0 deletions wgpu-in-app/src/examples/shadow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,7 @@ impl Shadow {
label: Some("shadow"),
format: None,
dimension: Some(wgpu::TextureViewDimension::D2),
usage: None,
aspect: wgpu::TextureAspect::All,
base_mip_level: 0,
mip_level_count: None,
Expand Down
2 changes: 2 additions & 0 deletions wgpu-in-app/src/ffi/ios.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ pub fn create_wgpu_canvas(ios_obj: IOSViewObj) -> *mut libc::c_void {
"create_wgpu_canvas, maximum frames: {}",
ios_obj.maximum_frames
);
crate::init_logger();

let obj = WgpuCanvas::new(AppSurface::new(ios_obj), 0_i32);
// 使用 Box 对 Rust 对象进行装箱操作。
// 我们无法将 Rust 对象直接传递给外部语言,通过装箱来传递此对象的胖指针
Expand Down
46 changes: 23 additions & 23 deletions wgpu-in-app/wgsl_shader/shadow.wgsl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
struct Globals {
view_proj: mat4x4f,
view_proj: mat4x4<f32>,
num_lights: vec4<u32>,
};

Expand All @@ -8,23 +8,23 @@ struct Globals {
var<uniform> u_globals: Globals;

struct Entity {
world: mat4x4f,
color: vec4f,
world: mat4x4<f32>,
color: vec4<f32>,
};

@group(1)
@binding(0)
var<uniform> u_entity: Entity;

@vertex
fn vs_bake(@location(0) position: vec4<i32>) -> @builtin(position) vec4f {
return u_globals.view_proj * u_entity.world * vec4f(position);
fn vs_bake(@location(0) position: vec4<i32>) -> @builtin(position) vec4<f32> {
return u_globals.view_proj * u_entity.world * vec4<f32>(position);
}

struct VertexOutput {
@builtin(position) proj_position: vec4f,
@location(0) world_normal: vec3f,
@location(1) world_position: vec4f
@builtin(position) proj_position: vec4<f32>,
@location(0) world_normal: vec3<f32>,
@location(1) world_position: vec4<f32>
};

@vertex
Expand All @@ -33,9 +33,9 @@ fn vs_main(
@location(1) normal: vec4<i32>,
) -> VertexOutput {
let w = u_entity.world;
let world_pos = u_entity.world * vec4f(position);
let world_pos = u_entity.world * vec4<f32>(position);
var result: VertexOutput;
result.world_normal = mat3x3f(w.x.xyz, w.y.xyz, w.z.xyz) * vec3f(normal.xyz);
result.world_normal = mat3x3<f32>(w[0].xyz, w[1].xyz, w[2].xyz) * vec3<f32>(normal.xyz);
result.world_position = world_pos;
result.proj_position = u_globals.view_proj * world_pos;
return result;
Expand All @@ -44,9 +44,9 @@ fn vs_main(
// fragment shader

struct Light {
proj: mat4x4f,
pos: vec4f,
color: vec4f,
proj: mat4x4<f32>,
pos: vec4<f32>,
color: vec4<f32>,
};

@group(0)
Expand All @@ -62,27 +62,27 @@ var t_shadow: texture_depth_2d_array;
@binding(3)
var sampler_shadow: sampler_comparison;

fn fetch_shadow(light_id: u32, homogeneous_coords: vec4f) -> f32 {
fn fetch_shadow(light_id: u32, homogeneous_coords: vec4<f32>) -> f32 {
if (homogeneous_coords.w <= 0.0) {
return 1.0;
}
// compensate for the Y-flip difference between the NDC and texture coordinates
let flip_correction = vec2f(0.5, -0.5);
let flip_correction = vec2<f32>(0.5, -0.5);
// compute texture coordinates for shadow lookup
let proj_correction = 1.0 / homogeneous_coords.w;
let light_local = homogeneous_coords.xy * flip_correction * proj_correction + vec2f(0.5, 0.5);
let light_local = homogeneous_coords.xy * flip_correction * proj_correction + vec2<f32>(0.5, 0.5);
// do the lookup, using HW PCF and comparison
return textureSampleCompareLevel(t_shadow, sampler_shadow, light_local, i32(light_id), homogeneous_coords.z * proj_correction);
}

const c_ambient: vec3f = vec3f(0.05, 0.05, 0.05);
const c_ambient: vec3<f32> = vec3<f32>(0.05, 0.05, 0.05);
const c_max_lights: u32 = 10u;

@fragment
fn fs_main(vertex: VertexOutput) -> @location(0) vec4f {
fn fs_main(vertex: VertexOutput) -> @location(0) vec4<f32> {
let normal = normalize(vertex.world_normal);
// accumulate color
var color: vec3f = c_ambient;
var color: vec3<f32> = c_ambient;
for(var i = 0u; i < min(u_globals.num_lights.x, c_max_lights); i += 1u) {
let light = s_lights[i];
// project into the light space
Expand All @@ -94,14 +94,14 @@ fn fs_main(vertex: VertexOutput) -> @location(0) vec4f {
color += shadow * diffuse * light.color.xyz;
}
// multiply the light by material color
return vec4f(color, 1.0) * u_entity.color;
return vec4<f32>(color, 1.0) * u_entity.color;
}

// The fragment entrypoint used when storage buffers are not available for the lights
@fragment
fn fs_main_without_storage(vertex: VertexOutput) -> @location(0) vec4f {
fn fs_main_without_storage(vertex: VertexOutput) -> @location(0) vec4<f32> {
let normal = normalize(vertex.world_normal);
var color: vec3f = c_ambient;
var color: vec3<f32> = c_ambient;
for(var i = 0u; i < min(u_globals.num_lights.x, c_max_lights); i += 1u) {
// This line is the only difference from the entrypoint above. It uses the lights
// uniform instead of the lights storage buffer
Expand All @@ -111,5 +111,5 @@ fn fs_main_without_storage(vertex: VertexOutput) -> @location(0) vec4f {
let diffuse = max(0.0, dot(normal, light_dir));
color += shadow * diffuse * light.color.xyz;
}
return vec4f(color, 1.0) * u_entity.color;
return vec4<f32>(color, 1.0) * u_entity.color;
}

0 comments on commit f777eb0

Please sign in to comment.