Skip to content

Commit

Permalink
Run puffin and puffin_egui on wasm32 on web (#112)
Browse files Browse the repository at this point in the history
  • Loading branch information
emilk authored Dec 9, 2022
1 parent dcd7d80 commit b533707
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 27 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
<!-- next-header -->
## [Unreleased] - ReleaseDate

- [PR#112](https://github.com/EmbarkStudios/puffin/pull/112) You can now compile and run `puffin` on the web if you enable the "web" feature.

## [0.14.0] - 2022-11-07

### Fixed
Expand Down
11 changes: 7 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions puffin-imgui/examples/imgui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,18 +92,18 @@ impl System {
gl_window.window().request_redraw();
}
Event::RedrawRequested(_) => {
let mut ui = imgui.frame();
let ui = imgui.frame();

let mut run = true;
run_ui(&mut run, &mut ui);
run_ui(&mut run, ui);
if !run {
*control_flow = ControlFlow::Exit;
}

let gl_window = display.gl_window();
let mut target = display.draw();
target.clear_color_srgb(1.0, 1.0, 1.0, 1.0);
platform.prepare_render(&ui, gl_window.window());
platform.prepare_render(ui, gl_window.window());
let draw_data = imgui.render();
renderer
.render(&mut target, draw_data)
Expand Down
22 changes: 16 additions & 6 deletions puffin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,21 @@ include = ["**/*.rs", "Cargo.toml", "README.md"]
[package.metadata.docs.rs]
all-features = true


[features]
default = []
packing = ["anyhow", "bincode", "parking_lot", "serde", "zstd", "ruzstd"]

# Feature for enabling loading/saving data to a binary stream and/or file.
serialization = ["packing"]

# Enable this to be able to run puffin inside a browser when compiling to wasm
web = ["instant/wasm-bindgen", "dep:js-sys"]


[dependencies]
byteorder = { version = "1.0" }
instant = { version = "0.1" }
once_cell = "1.0"

# Optional:
Expand All @@ -28,18 +41,15 @@ serde = { version = "1.0", features = ["derive", "rc"], optional = true }
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
zstd = { version = "0.11.1", optional = true } # native only

# web:
[target.'cfg(target_arch = "wasm32")'.dependencies]
js-sys = { version = "0.3", optional = true }
ruzstd = { version = "0.3.0", optional = true } # works on wasm


[dev-dependencies]
criterion = "0.4"

[features]
default = []
packing = ["anyhow", "bincode", "parking_lot", "serde", "zstd", "ruzstd"]
# Feature for enabling loading/saving data to a binary stream and/or file.
serialization = ["packing"]

[[bench]]
name = "benchmark"
harness = false
30 changes: 21 additions & 9 deletions puffin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -536,21 +536,33 @@ impl GlobalProfiler {
/// Returns a high-precision, monotonically increasing nanosecond count since unix epoch.
#[inline]
pub fn now_ns() -> NanoSecond {
// This can maybe be optimized
use once_cell::sync::Lazy;
use std::time::Instant;
#[cfg(target_arch = "wasm32")]
fn nanos_since_epoch() -> NanoSecond {
#[cfg(feature = "web")]
{
(js_sys::Date::new_0().get_time() * 1e6) as _
}
#[cfg(not(feature = "web"))]
{
0 // We won't get correct date-times, but that's fine.
}
}

fn epoch_offset_and_start() -> (NanoSecond, Instant) {
#[cfg(not(target_arch = "wasm32"))]
fn nanos_since_epoch() -> NanoSecond {
if let Ok(duration_since_epoch) = std::time::UNIX_EPOCH.elapsed() {
let nanos_since_epoch = duration_since_epoch.as_nanos() as NanoSecond;
(nanos_since_epoch, Instant::now())
duration_since_epoch.as_nanos() as NanoSecond
} else {
// system time is set before 1970. this should be quite rare.
(0, Instant::now())
0 // system time is set before 1970. this should be quite rare.
}
}

static START_TIME: Lazy<(NanoSecond, Instant)> = Lazy::new(epoch_offset_and_start);
// This can maybe be optimized
use instant::Instant;
use once_cell::sync::Lazy;

static START_TIME: Lazy<(NanoSecond, Instant)> =
Lazy::new(|| (nanos_since_epoch(), Instant::now()));
START_TIME.0 + START_TIME.1.elapsed().as_nanos() as NanoSecond
}

Expand Down
2 changes: 2 additions & 0 deletions puffin_egui/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ All notable changes to the egui crate will be documented in this file.
<!-- next-header -->
## [Unreleased] - ReleaseDate

- [PR#112](https://github.com/EmbarkStudios/puffin/pull/112) You can now compile and run `puffin_egui` on the web

## [0.18.0] - 2022-11-08

- Require `puffin` 0.14.0
Expand Down
3 changes: 2 additions & 1 deletion puffin_egui/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@ include = [
[dependencies]
chrono = "0.4"
egui = "0.19.0"
indexmap = { version = "1.9.1", features = ["serde"] }
instant = "0.1"
natord = "1.0.9"
once_cell = "1.7"
puffin = { version = "0.14.0", path = "../puffin", features = ["packing"] }
serde = { version = "1.0", features = ["derive"], optional = true }
vec1 = "1.8"
indexmap = { version = "1.9.1", features = ["serde"] }

[dev-dependencies]
eframe = "0.19.0"
Expand Down
8 changes: 4 additions & 4 deletions puffin_egui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ pub struct ProfilerUi {

/// When did we last run a pass to pack all the frames?
#[cfg_attr(feature = "serde", serde(skip))]
last_pack_pass: Option<std::time::Instant>,
last_pack_pass: Option<instant::Instant>,
}

impl Default for ProfilerUi {
Expand Down Expand Up @@ -428,16 +428,16 @@ impl ProfilerUi {
}
let last_pack_pass = self
.last_pack_pass
.get_or_insert_with(std::time::Instant::now);
.get_or_insert_with(instant::Instant::now);
let time_since_last_pack = last_pack_pass.elapsed();
if time_since_last_pack > std::time::Duration::from_secs(1) {
if time_since_last_pack > instant::Duration::from_secs(1) {
puffin::profile_scope!("pack_pass");
for frame in self.all_known_frames(frame_view) {
if !self.is_selected(frame_view, frame.frame_index()) {
frame.pack();
}
}
self.last_pack_pass = Some(std::time::Instant::now());
self.last_pack_pass = Some(instant::Instant::now());
}
}

Expand Down

0 comments on commit b533707

Please sign in to comment.