Skip to content

Commit

Permalink
Run on visionOS
Browse files Browse the repository at this point in the history
  • Loading branch information
jinleili committed Jun 22, 2023
1 parent c7be38e commit 04df8a5
Show file tree
Hide file tree
Showing 10 changed files with 194 additions and 257 deletions.
207 changes: 97 additions & 110 deletions Cargo.lock

Large diffs are not rendered by default.

16 changes: 7 additions & 9 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@ log = "0.4"
noise = { version = "0.7", default-features = false }
pollster = "0.2"
rand = { version = "0.7.2" }
wgpu = "0.16"
hal = { git = "https://github.com/gfx-rs/wgpu", package = "wgpu-hal", version = "0.16" }
# wgpu = { git = "https://github.com/gfx-rs/wgpu", rev = "7c25c00f" }
# wgpu = "0.16"
# wgpu = { git = "https://github.com/gfx-rs/wgpu", rev = "45efae31" }
# wgpu = { path = "../../forks/wgpu/wgpu" }
# hal = { package = "wgpu-hal", path = "../../forks/wgpu/wgpu-hal" }
wgpu = { git = "https://github.com/jinleili/wgpu", branch="visionOS" }

async-executor = "1.0"
winit = "0.27.1"
Expand All @@ -34,13 +33,12 @@ android_logger = "0.10.1"
jni = "0.19"
jni_fn = "0.1"
ndk-sys = "*"
ash = "0.37.2"
ash = "0.37.3"

# wasm target
web-sys = "0.3.60"
web-sys = "0.3.61"
wasm-bindgen = "0.2.83"
js-sys = "0.3.60"
wasm-bindgen-futures = "0.4.33"
wee_alloc = "0.4.5"
js-sys = "0.3.63"
wasm-bindgen-futures = "0.4.34"
# parking_lot 0.12 switches from `winapi` to `windows`; permit either
parking_lot = ">=0.11,<0.13"
3 changes: 0 additions & 3 deletions app-surface/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,3 @@ web-sys = { workspace = true, features = [
"Location"
] }
wasm-bindgen.workspace = true
# The `wasm-bindgen` crate provides the bare minimum functionality needed
# to interact with JavaScript.
wee_alloc = { workspace = true, optional = true }
20 changes: 11 additions & 9 deletions iOS/wgpu_in_iOS_App.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@
AD4B1FE126EB1B90008A6AEE /* Project object */ = {
isa = PBXProject;
attributes = {
LastSwiftUpdateCheck = 1400;
LastSwiftUpdateCheck = 1500;
LastUpgradeCheck = 1400;
TargetAttributes = {
AD4B1FE826EB1B90008A6AEE = {
Expand Down Expand Up @@ -323,8 +323,8 @@
CURRENT_PROJECT_VERSION = 1.0;
DEVELOPMENT_TEAM = P3HQ9GDFCW;
INFOPLIST_FILE = wgpu_phone/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 12.5;
"IPHONEOS_DEPLOYMENT_TARGET[sdk=macosx*]" = 14.2;
IPHONEOS_DEPLOYMENT_TARGET = 13;
"IPHONEOS_DEPLOYMENT_TARGET[sdk=macosx*]" = 12;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
Expand All @@ -340,10 +340,11 @@
PRODUCT_NAME = "$(TARGET_NAME)";
SUPPORTED_PLATFORMS = "iphoneos iphonesimulator";
SUPPORTS_MACCATALYST = NO;
SUPPORTS_MAC_DESIGNED_FOR_IPHONE_IPAD = YES;
SUPPORTS_MAC_DESIGNED_FOR_IPHONE_IPAD = NO;
SUPPORTS_XR_DESIGNED_FOR_IPHONE_IPAD = YES;
SWIFT_OBJC_BRIDGING_HEADER = "base/wgpu_test-Bridging-Header.h";
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2,6";
TARGETED_DEVICE_FAMILY = "1,2,6,7";
};
name = Debug;
};
Expand All @@ -357,8 +358,8 @@
CURRENT_PROJECT_VERSION = 1.0;
DEVELOPMENT_TEAM = P3HQ9GDFCW;
INFOPLIST_FILE = wgpu_phone/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 12.5;
"IPHONEOS_DEPLOYMENT_TARGET[sdk=macosx*]" = 14.2;
IPHONEOS_DEPLOYMENT_TARGET = 13;
"IPHONEOS_DEPLOYMENT_TARGET[sdk=macosx*]" = 12;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
Expand All @@ -375,10 +376,11 @@
PRODUCT_NAME = "$(TARGET_NAME)";
SUPPORTED_PLATFORMS = "iphoneos iphonesimulator";
SUPPORTS_MACCATALYST = NO;
SUPPORTS_MAC_DESIGNED_FOR_IPHONE_IPAD = YES;
SUPPORTS_MAC_DESIGNED_FOR_IPHONE_IPAD = NO;
SUPPORTS_XR_DESIGNED_FOR_IPHONE_IPAD = YES;
SWIFT_OBJC_BRIDGING_HEADER = "base/wgpu_test-Bridging-Header.h";
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2,6";
TARGETED_DEVICE_FAMILY = "1,2,6,7";
};
name = Release;
};
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,17 @@
<EnvironmentVariable
key = "RUST_BACKTRACE"
value = "full"
isEnabled = "NO">
isEnabled = "YES">
</EnvironmentVariable>
<EnvironmentVariable
key = "METAL_DEVICE_WRAPPER_TYPE"
value = "1"
isEnabled = "NO">
isEnabled = "YES">
</EnvironmentVariable>
<EnvironmentVariable
key = "gfx_backend_metal"
value = "debug"
isEnabled = "NO">
isEnabled = "YES">
</EnvironmentVariable>
</EnvironmentVariables>
</LaunchAction>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
<key>orderHint</key>
<integer>2</integer>
</dict>
<key>wgpu_on_visionOS.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>1</integer>
</dict>
<key>wgpu_phone.xcscheme_^#shared#^_</key>
<dict>
<key>isShown</key>
Expand Down
166 changes: 58 additions & 108 deletions wgpu-in-app/src/examples/boids.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ struct SimParams {
#[allow(dead_code)]
pub struct Boids {
particle_bind_groups: Vec<wgpu::BindGroup>,
dynamic_bind_group: wgpu::BindGroup,
particle_buffers: Vec<wgpu::Buffer>,
vertices_buffer: wgpu::Buffer,
compute_pipeline: wgpu::ComputePipeline,
Expand All @@ -57,104 +56,71 @@ impl Boids {
"../../wgsl_shader/draw.wgsl"
))),
});
log::info!("boids 1");
let param_data = SimParams {
delta_t: 0.04,
rule1_distance: 0.1,
rule2_distance: 0.025,
rule3_distance: 0.025,
rule1_scale: 0.02,
rule2_scale: 0.05,
rule3_scale: 0.005,
offset: 0u32,
};

let sim_param_buffer = device.create_buffer(&wgpu::BufferDescriptor {
label: None,
size: 5 * 256,
// buffer for simulation parameters uniform
let sim_param_data = [
0.04f32, // deltaT
0.1, // rule1Distance
0.025, // rule2Distance
0.025, // rule3Distance
0.02, // rule1Scale
0.05, // rule2Scale
0.005, // rule3Scale
]
.to_vec();
let sim_param_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("Simulation Parameter Buffer"),
contents: bytemuck::cast_slice(&sim_param_data),
usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
mapped_at_creation: false,
});
log::info!("boids 1 0");

for i in 0..5 {
let mut item = param_data;
item.offset = 64 * 5 * i;
log::info!("boids write_buffer 0");

app_surface.queue.write_buffer(
&sim_param_buffer,
256 * i as wgpu::BufferAddress,
bytemuck::bytes_of(&item),
);
log::info!("boids write_buffer 1 {}", i);
}
log::info!("boids 2");

let dynamic_bgl = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
entries: &[wgpu::BindGroupLayoutEntry {
binding: 0,
visibility: wgpu::ShaderStages::COMPUTE,
ty: wgpu::BindingType::Buffer {
ty: wgpu::BufferBindingType::Uniform,
has_dynamic_offset: true,
min_binding_size: wgpu::BufferSize::new((mem::size_of::<SimParams>()) as _),
},
count: None,
}],
label: None,
});
let dynamic_bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
layout: &dynamic_bgl,
entries: &[wgpu::BindGroupEntry {
binding: 0,
resource: wgpu::BindingResource::Buffer(wgpu::BufferBinding {
buffer: &sim_param_buffer,
offset: 0,
size: wgpu::BufferSize::new(256),
}),
}],
label: None,
});
log::info!("boids 3");

// create compute bind layout group and compute pipeline layout
let bgl_desc = wgpu::BindGroupLayoutDescriptor {
entries: &[
wgpu::BindGroupLayoutEntry {
binding: 0,
visibility: wgpu::ShaderStages::COMPUTE,
ty: wgpu::BindingType::Buffer {
ty: wgpu::BufferBindingType::Storage { read_only: true },
has_dynamic_offset: false,
min_binding_size: wgpu::BufferSize::new((NUM_PARTICLES * 16) as _),
let compute_bind_group_layout =
device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
entries: &[
wgpu::BindGroupLayoutEntry {
binding: 0,
visibility: wgpu::ShaderStages::COMPUTE,
ty: wgpu::BindingType::Buffer {
ty: wgpu::BufferBindingType::Uniform,
has_dynamic_offset: false,
min_binding_size: wgpu::BufferSize::new(
(sim_param_data.len() * mem::size_of::<f32>()) as _,
),
},
count: None,
},
count: None,
},
wgpu::BindGroupLayoutEntry {
binding: 1,
visibility: wgpu::ShaderStages::COMPUTE,
ty: wgpu::BindingType::Buffer {
ty: wgpu::BufferBindingType::Storage { read_only: false },
has_dynamic_offset: false,
min_binding_size: wgpu::BufferSize::new((NUM_PARTICLES * 16) as _),
wgpu::BindGroupLayoutEntry {
binding: 1,
visibility: wgpu::ShaderStages::COMPUTE,
ty: wgpu::BindingType::Buffer {
ty: wgpu::BufferBindingType::Storage { read_only: true },
has_dynamic_offset: false,
min_binding_size: wgpu::BufferSize::new((NUM_PARTICLES * 16) as _),
},
count: None,
},
count: None,
},
],
label: None,
};
let compute_bind_group_layout = device.create_bind_group_layout(&bgl_desc);

wgpu::BindGroupLayoutEntry {
binding: 2,
visibility: wgpu::ShaderStages::COMPUTE,
ty: wgpu::BindingType::Buffer {
ty: wgpu::BufferBindingType::Storage { read_only: false },
has_dynamic_offset: false,
min_binding_size: wgpu::BufferSize::new((NUM_PARTICLES * 16) as _),
},
count: None,
},
],
label: None,
});
let compute_pipeline_layout =
device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
label: Some("compute"),
bind_group_layouts: &[&compute_bind_group_layout, &dynamic_bgl],
bind_group_layouts: &[&compute_bind_group_layout],
push_constant_ranges: &[],
});
// create render pipeline with empty bind group layout
log::info!("boids 4");

// create render pipeline with empty bind group layout
let render_pipeline_layout =
device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
label: Some("render"),
Expand Down Expand Up @@ -190,7 +156,6 @@ impl Boids {
multisample: wgpu::MultisampleState::default(),
multiview: None,
});
log::info!("boids 5");

// create compute pipeline
let compute_pipeline = device.create_compute_pipeline(&wgpu::ComputePipelineDescriptor {
Expand All @@ -199,7 +164,6 @@ impl Boids {
module: &compute_shader,
entry_point: "main",
});
log::info!("boids 6");

// buffer for the three 2d triangle vertices of each instance
let vertex_buffer_data = [-0.01f32, -0.02, 0.01, -0.02, 0.00, 0.02];
Expand Down Expand Up @@ -244,10 +208,14 @@ impl Boids {
entries: &[
wgpu::BindGroupEntry {
binding: 0,
resource: particle_buffers[i].as_entire_binding(),
resource: sim_param_buffer.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 1,
resource: particle_buffers[i].as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 2,
resource: particle_buffers[(i + 1) % 2].as_entire_binding(), // bind to opposite buffer
},
],
Expand All @@ -261,7 +229,6 @@ impl Boids {

Self {
particle_bind_groups,
dynamic_bind_group,
particle_buffers,
vertices_buffer,
compute_pipeline,
Expand Down Expand Up @@ -309,24 +276,8 @@ impl Example for Boids {
.begin_compute_pass(&wgpu::ComputePassDescriptor { label: None });
cpass.set_pipeline(&self.compute_pipeline);
cpass.set_bind_group(0, &self.particle_bind_groups[self.frame_num % 2], &[]);
for i in 0..5 {
cpass.set_bind_group(1, &self.dynamic_bind_group, &[256 * i]);
cpass.dispatch_workgroups(5, 1, 1);
}
cpass.dispatch_workgroups(self.work_group_count, 1, 1);
}
// {
// let mut cpass = command_encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
// label: None,
// ty: wgpu::ComputePassType::Concurrent,
// });
// cpass.set_pipeline(&self.compute_pipeline);
// cpass.set_bind_group(0, &self.particle_bind_groups[self.frame_num % 2], &[]);

// for i in 1..5 {
// cpass.set_bind_group(1, &self.dynamic_bind_group, &[256 * i]);
// cpass.dispatch_workgroups(5, 1, 1);
// }
// }
command_encoder.pop_debug_group();

command_encoder.push_debug_group("render boids");
Expand All @@ -347,7 +298,6 @@ impl Example for Boids {

// done
queue.submit(Some(command_encoder.finish()));
queue.submit(None);
}
frame.present();
// update frame count
Expand Down
2 changes: 1 addition & 1 deletion wgpu-in-app/src/ffi/ios.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use app_surface::{AppSurface, IOSViewObj};

#[no_mangle]
pub fn create_wgpu_canvas(ios_obj: IOSViewObj) -> *mut libc::c_void {
println!("create_wgpu_canvas {}", ios_obj.maximum_frames);
println!("create_wgpu_canvas, maximum frames: {}", ios_obj.maximum_frames);
let obj = WgpuCanvas::new(AppSurface::new(ios_obj), 0_i32);
// 使用 Box 对 Rust 对象进行装箱操作。
// 我们无法将 Rust 对象直接传递给外部语言,通过装箱来传递此对象的胖指针
Expand Down

0 comments on commit 04df8a5

Please sign in to comment.