diff --git a/Cargo.toml b/Cargo.toml index 6495ddc042..5b5b0d6bda 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -53,7 +53,7 @@ cgmath = "0.17" log = "0.4" png = "0.15" winit = { version = "0.22", features = ["web-sys"] } -rand = "0.7.2" +rand = { version = "0.7.2", features = ["wasm-bindgen"] } bytemuck = "1" futures = "0.3" diff --git a/examples/boids/boids.comp b/examples/boids/boids.comp index 4a3d9d10bd..69a4ca6ddf 100644 --- a/examples/boids/boids.comp +++ b/examples/boids/boids.comp @@ -1,7 +1,8 @@ #version 450 -// this shader expects NUM_PARTICLES and PARTICLES_PER_GROUP -// defines to be inserted by main.rs +// These should match the Rust constants defined in main.rs +#define NUM_PARTICLES 1500 +#define PARTICLES_PER_GROUP 64 layout(local_size_x = PARTICLES_PER_GROUP) in; diff --git a/examples/boids/boids.comp.spv b/examples/boids/boids.comp.spv new file mode 100644 index 0000000000..cadd91ef22 Binary files /dev/null and b/examples/boids/boids.comp.spv differ diff --git a/examples/boids/main.rs b/examples/boids/main.rs index 2cec94bebb..d171786e67 100644 --- a/examples/boids/main.rs +++ b/examples/boids/main.rs @@ -6,8 +6,6 @@ extern crate rand; #[path = "../framework.rs"] mod framework; -use std::fmt::Write; - use wgpu::vertex_attr_array; // number of boid particles to simulate @@ -35,34 +33,19 @@ impl framework::Example for Example { sc_desc: &wgpu::SwapChainDescriptor, device: &wgpu::Device, ) -> (Self, Option) { - // loads comp shader source and adds shared constants as defines to comp shader - - const BOIDS_SOURCE: &str = include_str!("boids.comp"); - const HEADER: &str = "#version 450"; - assert_eq!(BOIDS_SOURCE.lines().next(), Some(HEADER)); - - let mut boids_source_str = String::from(HEADER); - write!( - boids_source_str, - "\n#define NUM_PARTICLES {}\n#define PARTICLES_PER_GROUP {}", - NUM_PARTICLES, PARTICLES_PER_GROUP - ) - .unwrap(); - boids_source_str += &BOIDS_SOURCE[HEADER.len()..]; - // load (and compile) shaders and create shader modules - let boids = framework::load_glsl(&boids_source_str, framework::ShaderStage::Compute); - let boids_module = device.create_shader_module(&boids); + let boids = include_bytes!("boids.comp.spv"); + let boids_module = device + .create_shader_module(&wgpu::read_spirv(std::io::Cursor::new(&boids[..])).unwrap()); - let vs = framework::load_glsl(include_str!("shader.vert"), framework::ShaderStage::Vertex); - let vs_module = device.create_shader_module(&vs); + let vs = include_bytes!("shader.vert.spv"); + let vs_module = + device.create_shader_module(&wgpu::read_spirv(std::io::Cursor::new(&vs[..])).unwrap()); - let fs = framework::load_glsl( - include_str!("shader.frag"), - framework::ShaderStage::Fragment, - ); - let fs_module = device.create_shader_module(&fs); + let fs = include_bytes!("shader.frag.spv"); + let fs_module = + device.create_shader_module(&wgpu::read_spirv(std::io::Cursor::new(&fs[..])).unwrap()); // create compute bind layout group and compute pipeline layout diff --git a/examples/boids/shader.frag.spv b/examples/boids/shader.frag.spv new file mode 100644 index 0000000000..3b3d8fbd71 Binary files /dev/null and b/examples/boids/shader.frag.spv differ diff --git a/examples/boids/shader.vert.spv b/examples/boids/shader.vert.spv new file mode 100644 index 0000000000..3e95e49fbd Binary files /dev/null and b/examples/boids/shader.vert.spv differ diff --git a/examples/framework.rs b/examples/framework.rs index 4445318068..a6702c7ece 100644 --- a/examples/framework.rs +++ b/examples/framework.rs @@ -94,7 +94,8 @@ async fn run_async(event_loop: EventLoop<()>, window: Window) { *control_flow = if cfg!(feature = "metal-auto-capture") { ControlFlow::Exit } else { - ControlFlow::Poll + // TODO: ControlFlow::Poll + ControlFlow::Wait }; match event { event::Event::MainEventsCleared => window.request_redraw(),