forked from linebender/vello
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HACK: Temporary hack to work around linebender#276
Fixed the WASM crashes by reverting to wgpu = 0.14. This surfaced two more bugs in the repo for which I included fixes. A big issue is wasm32-unknown-unknown does not support std::time::Instant, so I implemented a polyfill based on rust-lang/rust#48564 (comment). The animated examples, SVG loader, and the frame statistics monitor won't work in WASM without this. Once there is progress on the wgpu end, I'll turn that into a proper PR. Other issues: * The WGSL won't compile on native since this version of wgpu/naga doesn't support `const`. Chrome Canary in WASM works though. * There are serious visual artifacts in the examples when run in the browser.
- Loading branch information
Showing
9 changed files
with
123 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
// Adapted from https://github.com/rust-lang/rust/issues/48564#issuecomment-505114709 | ||
|
||
#![allow(dead_code, unused_imports)] | ||
|
||
use std::convert::TryInto; | ||
use std::ops::{Add, AddAssign, Sub, SubAssign}; | ||
|
||
#[cfg(target_arch = "wasm32")] | ||
use wasm_bindgen::prelude::*; | ||
|
||
pub use std::time::*; | ||
|
||
#[cfg(not(target_arch = "wasm32"))] | ||
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] | ||
pub struct Instant(std::time::Instant); | ||
|
||
#[cfg(not(target_arch = "wasm32"))] | ||
impl Instant { | ||
pub fn now() -> Self { | ||
Self(std::time::Instant::now()) | ||
} | ||
pub fn duration_since(&self, earlier: Instant) -> Duration { | ||
self.0.duration_since(earlier.0) | ||
} | ||
pub fn elapsed(&self) -> Duration { | ||
self.0.elapsed() | ||
} | ||
pub fn checked_add(&self, duration: Duration) -> Option<Self> { | ||
self.0.checked_add(duration).map(|i| Self(i)) | ||
} | ||
pub fn checked_sub(&self, duration: Duration) -> Option<Self> { | ||
self.0.checked_sub(duration).map(|i| Self(i)) | ||
} | ||
} | ||
|
||
#[cfg(target_arch = "wasm32")] | ||
#[wasm_bindgen(inline_js = r#" | ||
export function performance_now() { | ||
return performance.now(); | ||
}"#)] | ||
extern "C" { | ||
fn performance_now() -> f64; | ||
} | ||
|
||
#[cfg(target_arch = "wasm32")] | ||
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] | ||
pub struct Instant(u64); | ||
|
||
#[cfg(target_arch = "wasm32")] | ||
impl Instant { | ||
pub fn now() -> Self { | ||
Self((performance_now() * 1000.0) as u64) | ||
} | ||
pub fn duration_since(&self, earlier: Instant) -> Duration { | ||
Duration::from_micros(self.0 - earlier.0) | ||
} | ||
pub fn elapsed(&self) -> Duration { | ||
Self::now().duration_since(*self) | ||
} | ||
pub fn checked_add(&self, duration: Duration) -> Option<Self> { | ||
match duration.as_micros().try_into() { | ||
Ok(duration) => self.0.checked_add(duration).map(|i| Self(i)), | ||
Err(_) => None, | ||
} | ||
} | ||
pub fn checked_sub(&self, duration: Duration) -> Option<Self> { | ||
match duration.as_micros().try_into() { | ||
Ok(duration) => self.0.checked_sub(duration).map(|i| Self(i)), | ||
Err(_) => None, | ||
} | ||
} | ||
} | ||
|
||
impl Add<Duration> for Instant { | ||
type Output = Instant; | ||
fn add(self, other: Duration) -> Instant { | ||
self.checked_add(other).unwrap() | ||
} | ||
} | ||
impl Sub<Duration> for Instant { | ||
type Output = Instant; | ||
fn sub(self, other: Duration) -> Instant { | ||
self.checked_sub(other).unwrap() | ||
} | ||
} | ||
impl Sub<Instant> for Instant { | ||
type Output = Duration; | ||
fn sub(self, other: Instant) -> Duration { | ||
self.duration_since(other) | ||
} | ||
} | ||
impl AddAssign<Duration> for Instant { | ||
fn add_assign(&mut self, other: Duration) { | ||
*self = *self + other; | ||
} | ||
} | ||
impl SubAssign<Duration> for Instant { | ||
fn sub_assign(&mut self, other: Duration) { | ||
*self = *self - other; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters