diff --git a/Cargo.lock b/Cargo.lock index 6fe3304ca6..fb59ccbc93 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -636,10 +636,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8fc3cb4d91f53b50155bdcfd23f6a4c39ae1969c2ae85982b135750cccaf5fce" dependencies = [ "cfg-if 1.0.0", - "js-sys", "libc", "wasi 0.9.0+wasi-snapshot-preview1", - "wasm-bindgen", ] [[package]] @@ -1053,6 +1051,12 @@ dependencies = [ "unicode-xid", ] +[[package]] +name = "nanorand" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a51313c5820b0b02bd422f4b44776fbf47961755c74ce64afc73bfad10226c3" + [[package]] name = "ndk" version = "0.5.0" @@ -2061,12 +2065,12 @@ dependencies = [ "js-sys", "log", "naga", + "nanorand", "noise", "obj", "parking_lot 0.12.0", "png", "pollster", - "rand", "raw-window-handle", "serde", "smallvec", diff --git a/wgpu/Cargo.toml b/wgpu/Cargo.toml index 237590c5e9..46b5870c71 100644 --- a/wgpu/Cargo.toml +++ b/wgpu/Cargo.toml @@ -130,7 +130,7 @@ log = "0.4" noise = { version = "0.7", default-features = false } obj = "0.10" png = "0.17" -rand = "0.7.2" +nanorand = { version = "0.7", default-features = false, features = ["wyrand"] } winit = "0.26" [target.'cfg(not(target_arch = "wasm32"))'.dev-dependencies] @@ -295,4 +295,3 @@ console_error_panic_hook = "0.1.6" console_log = "0.1.2" # We need the Location feature in the framework examples web-sys = { version = "0.3.53", features = ["Location"] } -rand = { version = "0.7", features = ["wasm-bindgen"] } diff --git a/wgpu/examples/boids/main.rs b/wgpu/examples/boids/main.rs index 444a37561e..bb19d5066a 100644 --- a/wgpu/examples/boids/main.rs +++ b/wgpu/examples/boids/main.rs @@ -1,10 +1,7 @@ // Flocking boids example with gpu compute update pass // adapted from https://github.com/austinEng/webgpu-samples/blob/master/src/examples/computeBoids.ts -use rand::{ - distributions::{Distribution, Uniform}, - SeedableRng, -}; +use nanorand::{Rng, WyRand}; use std::{borrow::Cow, mem}; use wgpu::util::DeviceExt; @@ -183,13 +180,13 @@ impl framework::Example for Example { // buffer for all particles data of type [(posx,posy,velx,vely),...] let mut initial_particle_data = vec![0.0f32; (4 * NUM_PARTICLES) as usize]; - let mut rng = rand::rngs::StdRng::seed_from_u64(42); - let unif = Uniform::new_inclusive(-1.0, 1.0); + let mut rng = WyRand::new_seed(42); + let mut unif = || rng.generate::() * 2f32 - 1f32; // Generate a num (-1, 1) for particle_instance_chunk in initial_particle_data.chunks_mut(4) { - particle_instance_chunk[0] = unif.sample(&mut rng); // posx - particle_instance_chunk[1] = unif.sample(&mut rng); // posy - particle_instance_chunk[2] = unif.sample(&mut rng) * 0.1; // velx - particle_instance_chunk[3] = unif.sample(&mut rng) * 0.1; // vely + particle_instance_chunk[0] = unif(); // posx + particle_instance_chunk[1] = unif(); // posy + particle_instance_chunk[2] = unif() * 0.1; // velx + particle_instance_chunk[3] = unif() * 0.1; // vely } // creates two buffers of particle data each of size NUM_PARTICLES diff --git a/wgpu/examples/boids/screenshot.png b/wgpu/examples/boids/screenshot.png index f3c6796b1f..8f0fb7aaf6 100644 Binary files a/wgpu/examples/boids/screenshot.png and b/wgpu/examples/boids/screenshot.png differ diff --git a/wgpu/examples/bunnymark/main.rs b/wgpu/examples/bunnymark/main.rs index d6605172ec..47fa1d92d3 100644 --- a/wgpu/examples/bunnymark/main.rs +++ b/wgpu/examples/bunnymark/main.rs @@ -1,4 +1,5 @@ use bytemuck::{Pod, Zeroable}; +use nanorand::{Rng, WyRand}; use std::{borrow::Cow, mem}; use wgpu::util::DeviceExt; @@ -35,6 +36,7 @@ struct Example { bunnies: Vec, local_buffer: wgpu::Buffer, extent: [u32; 2], + rng: WyRand, } impl framework::Example for Example { @@ -234,6 +236,8 @@ impl framework::Example for Example { label: None, }); + let rng = WyRand::new_seed(42); + Example { pipeline, global_group, @@ -241,6 +245,7 @@ impl framework::Example for Example { bunnies: Vec::new(), local_buffer, extent: [config.width, config.height], + rng, } } @@ -256,14 +261,14 @@ impl framework::Example for Example { } = event { let spawn_count = 64 + self.bunnies.len() / 2; - let color = rand::random::(); + let color = self.rng.generate::(); println!( "Spawning {} bunnies, total at {}", spawn_count, self.bunnies.len() + spawn_count ); for _ in 0..spawn_count { - let speed = rand::random::() * MAX_VELOCITY - (MAX_VELOCITY * 0.5); + let speed = self.rng.generate::() * MAX_VELOCITY - (MAX_VELOCITY * 0.5); self.bunnies.push(Locals { position: [0.0, 0.5 * (self.extent[1] as f32)], velocity: [speed, 0.0], @@ -360,7 +365,7 @@ fn bunnymark() { height: 768, optional_features: wgpu::Features::default(), base_test_parameters: framework::test_common::TestParameters::default(), - tolerance: 1, - max_outliers: 50, + tolerance: 10, + max_outliers: 53, // Bounded by WARP }); } diff --git a/wgpu/examples/bunnymark/screenshot.png b/wgpu/examples/bunnymark/screenshot.png index 48deda7274..f8b8293cac 100644 Binary files a/wgpu/examples/bunnymark/screenshot.png and b/wgpu/examples/bunnymark/screenshot.png differ diff --git a/wgpu/examples/framework.rs b/wgpu/examples/framework.rs index b2065ca7b3..08d4fa9a78 100644 --- a/wgpu/examples/framework.rs +++ b/wgpu/examples/framework.rs @@ -574,6 +574,27 @@ pub fn test(mut params: FrameworkRefTest) { example.render(&dst_view, &ctx.device, &ctx.queue, &spawner); + // Handle specific case for bunnymark + #[allow(deprecated)] + if params.image_path == "/examples/bunnymark/screenshot.png" { + // Press spacebar to spawn bunnies + example.update(winit::event::WindowEvent::KeyboardInput { + input: winit::event::KeyboardInput { + scancode: 0, + state: winit::event::ElementState::Pressed, + virtual_keycode: Some(winit::event::VirtualKeyCode::Space), + modifiers: winit::event::ModifiersState::empty(), + }, + device_id: unsafe { winit::event::DeviceId::dummy() }, + is_synthetic: false, + }); + + // Step 3 extra frames + for _ in 0..3 { + example.render(&dst_view, &ctx.device, &ctx.queue, &spawner); + } + } + let mut cmd_buf = ctx .device .create_command_encoder(&wgpu::CommandEncoderDescriptor::default()); diff --git a/wgpu/examples/water/main.rs b/wgpu/examples/water/main.rs index 694c7f35e6..f4e8efea1f 100644 --- a/wgpu/examples/water/main.rs +++ b/wgpu/examples/water/main.rs @@ -5,7 +5,7 @@ mod point_gen; use bytemuck::{Pod, Zeroable}; use glam::Vec3; -use rand::SeedableRng; +use nanorand::{Rng, WyRand}; use std::{borrow::Cow, f32::consts, iter, mem}; use wgpu::util::DeviceExt; @@ -285,13 +285,12 @@ impl framework::Example for Example { let terrain_noise = noise::OpenSimplex::new(); // Random colouration - let mut terrain_random = rand::rngs::StdRng::seed_from_u64(42); + let mut terrain_random = WyRand::new_seed(42); // Generate terrain. The closure determines what each hexagon will look like. let terrain = point_gen::HexTerrainMesh::generate(SIZE, |point| -> point_gen::TerrainVertex { use noise::NoiseFn; - use rand::Rng; let noise = terrain_noise.get([point[0] as f64 / 5.0, point[1] as f64 / 5.0]) + 0.1; let y = noise as f32 * 22.0; @@ -314,7 +313,7 @@ impl framework::Example for Example { const SNOW: [u8; 4] = [175, 224, 237, 255]; // Random colouration. - let random = terrain_random.gen::() * 0.2 + 0.9; + let random = terrain_random.generate::() * 0.2 + 0.9; // Choose colour. let colour = if y <= 0.0 { diff --git a/wgpu/examples/water/screenshot.png b/wgpu/examples/water/screenshot.png index f2b6c8d806..a2a18364e7 100644 Binary files a/wgpu/examples/water/screenshot.png and b/wgpu/examples/water/screenshot.png differ diff --git a/wgpu/tests/common/image.rs b/wgpu/tests/common/image.rs index 8603f712d9..4af7419415 100644 --- a/wgpu/tests/common/image.rs +++ b/wgpu/tests/common/image.rs @@ -107,7 +107,7 @@ pub fn compare_image_output( .unwrap(); if outliers > max_outliers { - // Because the deta is mismatched, lets output the difference to a file. + // Because the data is mismatched, lets output the difference to a file. let old_path = Path::new(&path); let actual_path = Path::new(&path).with_file_name( OsString::from_str(