Don't use rayon in examples on wasm

This commit is contained in:
Liam Murphy 2022-02-23 19:13:26 +11:00
parent d735510f72
commit 7b4717fdc4
3 changed files with 21 additions and 3 deletions

View file

@ -37,6 +37,16 @@ version = "0.3.55"
features = ["CanvasRenderingContext2d", "Document", "Element", "HtmlCanvasElement", "ImageData", "Window"] features = ["CanvasRenderingContext2d", "Document", "Element", "HtmlCanvasElement", "ImageData", "Window"]
[dev-dependencies] [dev-dependencies]
instant = "0.1.12"
winit = "0.26.1" winit = "0.26.1"
[dev-dependencies.image]
version = "0.23.14"
# Disable rayon on web
default-features = false
features = ["jpeg"]
[target.'cfg(not(target_arch = "wasm32"))'.dev-dependencies]
# Turn rayon back on everywhere else; creating the separate entry resets the features to default.
image = "0.23.14" image = "0.23.14"
rayon = "1.5.1" rayon = "1.5.1"

View file

@ -1,5 +1,6 @@
use std::f64::consts::PI; use std::f64::consts::PI;
use std::time::Instant; use instant::Instant;
#[cfg(not(target_arch = "wasm32"))]
use rayon::prelude::*; use rayon::prelude::*;
use softbuffer::GraphicsContext; use softbuffer::GraphicsContext;
use winit::event::{Event, WindowEvent}; use winit::event::{Event, WindowEvent};
@ -64,7 +65,7 @@ fn main() {
} }
fn pre_render_frames(width: usize, height: usize) -> Vec<Vec<u32>>{ fn pre_render_frames(width: usize, height: usize) -> Vec<Vec<u32>>{
(0..60).into_par_iter().map(|frame_id|{ let render = |frame_id|{
let elapsed = ((frame_id as f64)/(60.0))*2.0*PI; let elapsed = ((frame_id as f64)/(60.0))*2.0*PI;
let buffer = (0..((width * height) as usize)) let buffer = (0..((width * height) as usize))
.map(|index| { .map(|index| {
@ -81,5 +82,11 @@ fn pre_render_frames(width: usize, height: usize) -> Vec<Vec<u32>>{
.collect::<Vec<_>>(); .collect::<Vec<_>>();
buffer buffer
}).collect() };
#[cfg(target_arch = "wasm32")]
return (0..60).map(render).collect();
#[cfg(not(target_arch = "wasm32"))]
(0..60).into_par_iter().map(render).collect()
} }

View file

@ -16,6 +16,7 @@ pub enum SoftBufferError<W: HasRawWindowHandle> {
PlatformError(Option<String>, Option<Box<dyn Error>>) PlatformError(Option<String>, Option<Box<dyn Error>>)
} }
#[allow(unused)] // This isn't used on all platforms
pub(crate) fn unwrap<T, E: std::error::Error + 'static, W: HasRawWindowHandle>(res: Result<T, E>, str: &str) -> Result<T, SoftBufferError<W>>{ pub(crate) fn unwrap<T, E: std::error::Error + 'static, W: HasRawWindowHandle>(res: Result<T, E>, str: &str) -> Result<T, SoftBufferError<W>>{
match res{ match res{
Ok(t) => Ok(t), Ok(t) => Ok(t),