Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WASM fix #2

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# These are supported funding model platforms

github: aevyrie
32 changes: 25 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bevy_framepace"
version = "0.14.1"
version = "0.15.0"
edition = "2021"
resolver = "2"
description = "Frame pacing and frame limiting for Bevy"
Expand All @@ -11,21 +11,39 @@ documentation = "https://docs.rs/bevy_framepace"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
bevy = { version = "0.12", default-features = false, features = [
"bevy_render",
"bevy_winit",
] }
bevy_app = "0.13"
bevy_ecs = "0.13"
bevy_diagnostic = "0.13"
bevy_log = "0.13"
bevy_render = "0.13"
bevy_reflect = "0.13"
bevy_time = "0.13"
bevy_utils = "0.13"
bevy_window = "0.13"
bevy_winit = "0.13"
# Non-bevy
spin_sleep = "1.0"

[features]
default = ["framepace_debug", "bevy/x11"]
default = ["framepace_debug", "bevy_winit/x11"]
framepace_debug = []

[dev-dependencies]
bevy = { version = "0.12", default-features = false, features = [
bevy = { version = "0.13", default-features = false, features = [
"bevy_gizmos",
"bevy_text",
"bevy_ui",
"default_font",
"multi-threaded",
"x11",
] }

[[example]]
name = "demo"
path = "examples/demo.rs"
required-features = ["default"]

[[example]]
name = "minimal"
path = "examples/minimal.rs"
required-features = ["default"]
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ I intend to track the `main` branch of Bevy. PRs supporting this are welcome!

| bevy | bevy_framepace |
| ---- | ------------------- |
| 0.13 | 0.15 |
| 0.12 | 0.14 |
| 0.11 | 0.13 |
| 0.10 | 0.12 |
Expand Down
2 changes: 1 addition & 1 deletion examples/demo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ struct EnableText;

fn toggle_plugin(
mut settings: ResMut<bevy_framepace::FramepaceSettings>,
input: Res<Input<KeyCode>>,
input: Res<ButtonInput<KeyCode>>,
) {
if input.just_pressed(KeyCode::Space) {
use bevy_framepace::Limiter;
Expand Down
34 changes: 14 additions & 20 deletions src/debug.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
//! Adds diagnostic logging and a cursor for debugging.

use bevy::{
diagnostic::{Diagnostic, DiagnosticId, Diagnostics, RegisterDiagnostic},
prelude::*,
};
use bevy_app::prelude::*;
use bevy_diagnostic::{Diagnostic, DiagnosticPath, Diagnostics, RegisterDiagnostic};
use bevy_ecs::prelude::*;
use bevy_time::prelude::*;

/// Adds [`Diagnostics`] data from `bevy_framepace`
pub struct DiagnosticsPlugin;
Expand All @@ -12,24 +12,18 @@ impl Plugin for DiagnosticsPlugin {
fn build(&self, app: &mut App) {
app.add_systems(Update, Self::diagnostic_system);

app.register_diagnostic(
Diagnostic::new(Self::FRAMEPACE_FRAMETIME, "framepace::frametime", 128)
.with_suffix("ms"),
);
app.register_diagnostic(
Diagnostic::new(Self::FRAMEPACE_OVERSLEEP, "framepace::oversleep", 128)
.with_suffix("µs"),
);
app.register_diagnostic(Diagnostic::new(Self::FRAMEPACE_FRAMETIME).with_suffix("ms"));
app.register_diagnostic(Diagnostic::new(Self::FRAMEPACE_OVERSLEEP).with_suffix("µs"));
}
}

impl DiagnosticsPlugin {
/// [`DiagnosticId`] for the frametime
pub const FRAMEPACE_FRAMETIME: DiagnosticId =
DiagnosticId::from_u128(8021378406439507683279787892187089153);
/// [`DiagnosticId`] for failures to meet frame time target
pub const FRAMEPACE_OVERSLEEP: DiagnosticId =
DiagnosticId::from_u128(978023490268634078905367093342937);
/// [`DiagnosticPath`] for the frametime
pub const FRAMEPACE_FRAMETIME: DiagnosticPath =
DiagnosticPath::const_new("framepace/frametime");
/// [`DiagnosticPath`] for failures to meet frame time target
pub const FRAMEPACE_OVERSLEEP: DiagnosticPath =
DiagnosticPath::const_new("framepace/oversleep");

/// Updates diagnostic data from measurements
pub fn diagnostic_system(
Expand All @@ -44,7 +38,7 @@ impl DiagnosticsPlugin {
let frametime_millis = stats.frametime.try_lock().unwrap().as_secs_f64() * 1_000_f64;
let error_micros = stats.oversleep.try_lock().unwrap().as_secs_f64() * 1_000_000_f64;

diagnostics.add_measurement(Self::FRAMEPACE_FRAMETIME, || frametime_millis);
diagnostics.add_measurement(Self::FRAMEPACE_OVERSLEEP, || error_micros);
diagnostics.add_measurement(&Self::FRAMEPACE_FRAMETIME, || frametime_millis);
diagnostics.add_measurement(&Self::FRAMEPACE_OVERSLEEP, || error_micros);
}
}
33 changes: 23 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! This is a [`bevy`] plugin that adds framepacing and framelimiting to improve input latency and
//! This is a `bevy` plugin that adds framepacing and framelimiting to improve input latency and
//! power use.
//!
//! # How it works
Expand Down Expand Up @@ -27,13 +27,18 @@

#![deny(missing_docs)]

use bevy_app::prelude::*;
use bevy_ecs::prelude::*;
use bevy_reflect::prelude::*;
use bevy_render::{Render, RenderApp, RenderSet};
use bevy_utils::Instant;

#[cfg(not(target_arch = "wasm32"))]
use bevy::winit::WinitWindows;
use bevy::{
prelude::*,
render::{pipelined_rendering::RenderExtractApp, RenderApp, RenderSet},
utils::Instant,
};
use bevy_render::pipelined_rendering::RenderExtractApp;
#[cfg(not(target_arch = "wasm32"))]
use bevy_window::prelude::*;
#[cfg(not(target_arch = "wasm32"))]
use bevy_winit::WinitWindows;

use std::{
sync::{Arc, Mutex},
Expand All @@ -43,6 +48,12 @@ use std::{
#[cfg(feature = "framepace_debug")]
pub mod debug;

/// Bevy does not export `RenderExtractApp` on wasm32, so we create a dummy label to ensure this
/// compiles on wasm32.
#[cfg(target_arch = "wasm32")]
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, bevy_app::AppLabel)]
struct RenderExtractApp;

/// Adds framepacing and framelimiting functionality to your [`App`].
#[derive(Debug, Clone, Component)]
pub struct FramepacePlugin;
Expand Down Expand Up @@ -78,7 +89,7 @@ impl Plugin for FramepacePlugin {
.insert_resource(limit)
.insert_resource(stats)
.add_systems(
bevy::render::Render,
Render,
framerate_limiter
.in_set(RenderSet::Cleanup)
.after(World::clear_entities),
Expand Down Expand Up @@ -115,6 +126,7 @@ struct FramepaceSettingsProxy {
limiter: Arc<Mutex<Limiter>>,
}

#[cfg(not(target_arch = "wasm32"))]
impl FramepaceSettingsProxy {
fn is_enabled(&self) -> bool {
self.limiter.try_lock().iter().any(|l| l.is_enabled())
Expand Down Expand Up @@ -199,7 +211,7 @@ fn get_display_refresh_rate(
Limiter::Off => {
#[cfg(feature = "framepace_debug")]
if settings.is_changed() {
info!("Frame limiter disabled");
bevy_log::info!("Frame limiter disabled");
}
return;
}
Expand All @@ -208,7 +220,7 @@ fn get_display_refresh_rate(
if let Ok(mut limit) = frame_limit.0.try_lock() {
if new_frametime != *limit {
#[cfg(feature = "framepace_debug")]
info!("Frametime limit changed to: {:?}", new_frametime);
bevy_log::info!("Frametime limit changed to: {:?}", new_frametime);
*limit = new_frametime;
}
}
Expand Down Expand Up @@ -249,6 +261,7 @@ pub struct FramePaceStats {
/// `spin_sleep` sleeps as long as possible given the platform's sleep accuracy, and spins for the
/// remainder. The dependency is however not WASM compatible, which is fine, because frame limiting
/// should not be used in a browser; this would compete with the browser's frame limiter.
#[allow(unused_variables)]
fn framerate_limiter(
mut timer: ResMut<FrameTimer>,
target_frametime: Res<FrametimeLimit>,
Expand Down