Skip to content

Commit 59da26c

Browse files
authored
Add performance rendering example (#198)
1 parent 14d690d commit 59da26c

File tree

11 files changed

+202
-0
lines changed

11 files changed

+202
-0
lines changed

Cargo.lock

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -910,6 +910,7 @@ dependencies = [
910910
"linked_axes",
911911
"log",
912912
"markers",
913+
"performance",
913914
"plot_span",
914915
"save_plot",
915916
"stacked_bar",
@@ -2688,6 +2689,16 @@ version = "2.3.2"
26882689
source = "registry+https://github.com/rust-lang/crates.io-index"
26892690
checksum = "9b4f627cb1b25917193a259e49bdad08f671f8d9708acfd5fe0a8c1455d87220"
26902691

2692+
[[package]]
2693+
name = "performance"
2694+
version = "0.1.0"
2695+
dependencies = [
2696+
"eframe",
2697+
"egui_plot",
2698+
"env_logger",
2699+
"examples_utils",
2700+
]
2701+
26912702
[[package]]
26922703
name = "pin-project"
26932704
version = "1.1.10"

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ legend_sort = { version = "0.1.0", path = "examples/legend_sort" }
3535
lines = { version = "0.1.0", path = "examples/lines" }
3636
linked_axes = { version = "0.1.0", path = "examples/linked_axes" }
3737
markers = { version = "0.1.0", path = "examples/markers" }
38+
performance = { version = "0.1.0", path = "examples/performance" }
3839
plot_span = { version = "0.1.0", path = "examples/plot_span" }
3940
save_plot = { version = "0.1.0", path = "examples/save_plot" }
4041
stacked_bar = { version = "0.1.0", path = "examples/stacked_bar" }

demo/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ legend_sort.workspace = true
4141
lines.workspace = true
4242
linked_axes.workspace = true
4343
markers.workspace = true
44+
performance.workspace = true
4445
plot_span.workspace = true
4546
save_plot.workspace = true
4647
stacked_bar.workspace = true

demo/src/app.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ impl DemoGallery {
4747
Box::new(lines::LineExample::default()),
4848
Box::new(linked_axes::LinkedAxesExample::default()),
4949
Box::new(markers::MarkerDemo::default()),
50+
Box::new(performance::PerformanceDemo::default()),
5051
Box::new(plot_span::PlotSpanDemo::default()),
5152
Box::new(save_plot::SavePlotExample::default()),
5253
Box::new(stacked_bar::StackedBarExample::default()),

examples/performance/Cargo.toml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
[package]
2+
name = "performance"
3+
version = "0.1.0"
4+
authors = ["egui_plot contributors"]
5+
license.workspace = true
6+
edition.workspace = true
7+
rust-version.workspace = true
8+
publish = false
9+
10+
[lints]
11+
workspace = true
12+
13+
[dependencies]
14+
eframe = { workspace = true, features = ["default"] }
15+
egui_plot.workspace = true
16+
env_logger = { workspace = true, default-features = false, features = [
17+
"auto-color",
18+
"humantime",
19+
] }
20+
examples_utils.workspace = true
21+
22+
[package.metadata.cargo-shear]
23+
ignored = ["env_logger"] # env_logger used by make_main! macro

examples/performance/README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Performance Demo
2+
3+
This example demonstrates plotting performance with a large number of markers. Use the controls to adjust the number of markers and observe rendering performance.
4+
5+
## Running
6+
7+
Native
8+
```sh
9+
cargo run -p performance
10+
```
11+
12+
Web (WASM)
13+
```sh
14+
cd examples/performance
15+
trunk serve
16+
```
17+
18+
![](screenshot.png)
19+
20+
Lines changed: 3 additions & 0 deletions
Loading
Lines changed: 3 additions & 0 deletions
Loading

examples/performance/src/app.rs

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
use eframe::egui;
2+
use eframe::egui::Response;
3+
use egui_plot::MarkerShape;
4+
use egui_plot::Plot;
5+
use egui_plot::Points;
6+
7+
/// Simple LCG pseudo-random number generator. Returns a value in [0.0, 1.0].
8+
fn rng(state: &mut u64) -> f64 {
9+
let mut x = *state;
10+
x ^= x << 13;
11+
x ^= x >> 7;
12+
x ^= x << 17;
13+
*state = x;
14+
(x as f64) / (u64::MAX as f64)
15+
}
16+
17+
fn make_markers(target_count: usize) -> Vec<[f64; 2]> {
18+
let mut state = 42u64;
19+
(0..target_count).map(|_| [rng(&mut state), rng(&mut state)]).collect()
20+
}
21+
22+
pub struct PerformanceDemo {
23+
target_count: usize,
24+
marker_radius: f32,
25+
markers: Vec<[f64; 2]>,
26+
marker_shape: MarkerShape,
27+
}
28+
29+
impl Default for PerformanceDemo {
30+
fn default() -> Self {
31+
Self {
32+
target_count: 100,
33+
marker_radius: 1.0,
34+
markers: make_markers(100),
35+
marker_shape: MarkerShape::Circle,
36+
}
37+
}
38+
}
39+
40+
impl PerformanceDemo {
41+
pub fn show_plot(&self, ui: &mut egui::Ui) -> Response {
42+
Plot::new("performance_demo")
43+
.data_aspect(1.0)
44+
.show(ui, |plot_ui| {
45+
plot_ui.points(
46+
Points::new("markers", self.markers.clone())
47+
.radius(self.marker_radius)
48+
.shape(self.marker_shape)
49+
.filled(true),
50+
);
51+
})
52+
.response
53+
}
54+
55+
pub fn show_controls(&mut self, ui: &mut egui::Ui) -> Response {
56+
ui.ctx().request_repaint(); // Continuous repaint for FPS counter
57+
let fps = (1.0 / ui.ctx().input(|i| i.stable_dt)).round();
58+
59+
ui.horizontal(|ui| {
60+
ui.label("Markers:");
61+
if ui
62+
.add(
63+
egui::DragValue::new(&mut self.target_count)
64+
.speed(100)
65+
.range(100..=10_000_000),
66+
)
67+
.changed()
68+
{
69+
self.markers = make_markers(self.target_count);
70+
}
71+
72+
ui.label("Radius:");
73+
ui.add(
74+
egui::DragValue::new(&mut self.marker_radius)
75+
.speed(0.1)
76+
.range(0.5..=5.0),
77+
);
78+
79+
ui.label("Shape:");
80+
egui::ComboBox::from_id_salt("marker_shape")
81+
.selected_text(format!("{:?}", self.marker_shape))
82+
.show_ui(ui, |ui| {
83+
for shape in MarkerShape::all() {
84+
ui.selectable_value(&mut self.marker_shape, shape, format!("{shape:?}"));
85+
}
86+
});
87+
88+
ui.label(format!("FPS: {fps}"));
89+
});
90+
91+
ui.label("Note: Less than 100k markers should work fine, beyond that may cause issues.");
92+
ui.response()
93+
}
94+
}

examples/performance/src/lib.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#![doc = include_str!("../README.md")]
2+
3+
use eframe::egui;
4+
use examples_utils::PlotExample;
5+
6+
mod app;
7+
pub use app::PerformanceDemo;
8+
9+
impl PlotExample for PerformanceDemo {
10+
fn name(&self) -> &'static str {
11+
"performance"
12+
}
13+
14+
fn title(&self) -> &'static str {
15+
"Performance Demo"
16+
}
17+
18+
fn description(&self) -> &'static str {
19+
"This example demonstrates plotting performance with a large number of markers. Use the controls to adjust the number of markers and observe rendering performance."
20+
}
21+
22+
fn tags(&self) -> &'static [&'static str] {
23+
&["performance", "markers"]
24+
}
25+
26+
fn thumbnail_bytes(&self) -> &'static [u8] {
27+
include_bytes!("../screenshot_thumb.png")
28+
}
29+
30+
fn code_bytes(&self) -> &'static [u8] {
31+
include_bytes!("./app.rs")
32+
}
33+
34+
fn show_ui(&mut self, ui: &mut egui::Ui) -> egui::Response {
35+
self.show_plot(ui)
36+
}
37+
38+
fn show_controls(&mut self, ui: &mut egui::Ui) -> egui::Response {
39+
self.show_controls(ui)
40+
}
41+
}

0 commit comments

Comments
 (0)