Skip to content

Commit

Permalink
Pass out pixels_per_point in output for use in tesselation
Browse files Browse the repository at this point in the history
  • Loading branch information
emilk committed Nov 15, 2023
1 parent c18eb19 commit 1b1be3c
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 25 deletions.
14 changes: 4 additions & 10 deletions crates/eframe/src/native/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,7 @@ mod glow_integration {
platform_output,
textures_delta,
shapes,
pixels_per_point,
viewports: viewports_out,
viewport_commands,
} = full_output;
Expand All @@ -580,10 +581,6 @@ mod glow_integration {
integration.post_update(app.as_mut(), window);
integration.handle_platform_output(window, viewport_id, platform_output, egui_winit);

let pixels_per_point = integration
.egui_ctx
.input_for(viewport_id, |i| i.pixels_per_point());

let clipped_primitives = integration.egui_ctx.tessellate(shapes, pixels_per_point);

*current_gl_context = Some(
Expand Down Expand Up @@ -1581,7 +1578,7 @@ mod glow_integration {
let screen_size_in_pixels: [u32; 2] = window.inner_size().into();

let pixels_per_point = egui_ctx.input_for(ids.this, |i| i.pixels_per_point());
let clipped_primitives = egui_ctx.tessellate(output.shapes, pixels_per_point);
let clipped_primitives = egui_ctx.tessellate(output.shapes, output.pixels_per_point);

let mut painter = painter.borrow_mut();

Expand Down Expand Up @@ -2181,7 +2178,7 @@ mod wgpu_integration {
}

let pixels_per_point = egui_ctx.input_for(ids.this, |i| i.pixels_per_point());
let clipped_primitives = egui_ctx.tessellate(output.shapes, pixels_per_point);
let clipped_primitives = egui_ctx.tessellate(output.shapes, output.pixels_per_point);
painter.paint_and_update_textures(
ids.this,
pixels_per_point,
Expand Down Expand Up @@ -2504,17 +2501,14 @@ mod wgpu_integration {
platform_output,
textures_delta,
shapes,
pixels_per_point,
viewports: out_viewports,
viewport_commands,
} = full_output;

integration.handle_platform_output(window, viewport_id, platform_output, egui_winit);

{
let pixels_per_point = integration
.egui_ctx
.input_for(viewport_id, |i| i.pixels_per_point());

let clipped_primitives = integration.egui_ctx.tessellate(shapes, pixels_per_point);

let screenshot_requested = &mut integration.frame.output.screenshot_requested;
Expand Down
4 changes: 3 additions & 1 deletion crates/egui/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ impl ContextImpl {
/// });
/// });
/// handle_platform_output(full_output.platform_output);
/// let clipped_primitives = ctx.tessellate(full_output.shapes); // create triangles to paint
/// let clipped_primitives = ctx.tessellate(full_output.shapes, full_output.pixels_per_point);
/// paint(full_output.textures_delta, clipped_primitives);
/// }
/// ```
Expand Down Expand Up @@ -1486,6 +1486,7 @@ impl ContextImpl {
fn end_frame(&mut self) -> FullOutput {
let ended_viewport_id = self.viewport_id();
let viewport = self.viewports.entry(ended_viewport_id).or_default();
let pixels_per_point = viewport.input.pixels_per_point;

if viewport.input.wants_repaint() {
self.repaint.requested_repaint(&ended_viewport_id);
Expand Down Expand Up @@ -1607,6 +1608,7 @@ impl ContextImpl {
platform_output,
textures_delta,
shapes,
pixels_per_point,
viewports: out_viewports,
// We should not process viewport commands when we are a sync viewport, because that will cause a deadlock for egui backend
viewport_commands: if is_last {
Expand Down
7 changes: 7 additions & 0 deletions crates/egui/src/data/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ pub struct FullOutput {
/// You can use [`crate::Context::tessellate`] to turn this into triangles.
pub shapes: Vec<epaint::ClippedShape>,

/// The number of physical pixels per logical ui point, for the viewport that was updated.
///
/// You can pass this to [`Context::tesselate`] together with [`Self::shapes`].
pub pixels_per_point: f32,

/// All the active viewports, excluding the root.
pub viewports: Vec<ViewportOutput>,

Expand All @@ -35,13 +40,15 @@ impl FullOutput {
platform_output,
textures_delta,
shapes,
pixels_per_point,
mut viewports,
mut viewport_commands,
} = newer;

self.platform_output.append(platform_output);
self.textures_delta.append(textures_delta);
self.shapes = shapes; // Only paint the latest
self.pixels_per_point = pixels_per_point; // Use latest
self.viewports.append(&mut viewports);
self.viewport_commands.append(&mut viewport_commands);
}
Expand Down
2 changes: 1 addition & 1 deletion crates/egui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@
//! });
//! });
//! handle_platform_output(full_output.platform_output);
//! let clipped_primitives = ctx.tessellate(full_output.shapes); // create triangles to paint
//! let clipped_primitives = ctx.tessellate(full_output.shapes, full_output.pixels_per_point);
//! paint(full_output.textures_delta, clipped_primitives);
//! }
//! ```
Expand Down
5 changes: 2 additions & 3 deletions crates/egui_demo_lib/benches/benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,14 @@ pub fn criterion_benchmark(c: &mut Criterion) {
{
let ctx = egui::Context::default();
let mut demo_windows = egui_demo_lib::DemoWindows::default();
let pixels_per_point = 1.0;

// The most end-to-end benchmark.
c.bench_function("demo_with_tessellate__realistic", |b| {
b.iter(|| {
let full_output = ctx.run(RawInput::default(), |ctx| {
demo_windows.ui(ctx);
});
ctx.tessellate(full_output.shapes, pixels_per_point)
ctx.tessellate(full_output.shapes, full_output.pixels_per_point)
});
});

Expand All @@ -33,7 +32,7 @@ pub fn criterion_benchmark(c: &mut Criterion) {
demo_windows.ui(ctx);
});
c.bench_function("demo_only_tessellate", |b| {
b.iter(|| ctx.tessellate(full_output.shapes.clone(), pixels_per_point));
b.iter(|| ctx.tessellate(full_output.shapes.clone(), full_output.pixels_per_point));
});
}

Expand Down
6 changes: 2 additions & 4 deletions crates/egui_demo_lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,13 @@ fn test_egui_e2e() {
let mut demo_windows = crate::DemoWindows::default();
let ctx = egui::Context::default();
let raw_input = egui::RawInput::default();
let pixels_per_point = 1.0;

const NUM_FRAMES: usize = 5;
for _ in 0..NUM_FRAMES {
let full_output = ctx.run(raw_input.clone(), |ctx| {
demo_windows.ui(ctx);
});
let clipped_primitives = ctx.tessellate(full_output.shapes, pixels_per_point);
let clipped_primitives = ctx.tessellate(full_output.shapes, full_output.pixels_per_point);
assert!(!clipped_primitives.is_empty());
}
}
Expand All @@ -91,14 +90,13 @@ fn test_egui_zero_window_size() {
screen_rect: Some(egui::Rect::from_min_max(egui::Pos2::ZERO, egui::Pos2::ZERO)),
..Default::default()
};
let pixels_per_point = 1.0;

const NUM_FRAMES: usize = 5;
for _ in 0..NUM_FRAMES {
let full_output = ctx.run(raw_input.clone(), |ctx| {
demo_windows.ui(ctx);
});
let clipped_primitives = ctx.tessellate(full_output.shapes, pixels_per_point);
let clipped_primitives = ctx.tessellate(full_output.shapes, full_output.pixels_per_point);
assert!(
clipped_primitives.is_empty(),
"There should be nothing to show, has at least one primitive with clip_rect: {:?}",
Expand Down
20 changes: 14 additions & 6 deletions crates/egui_glow/src/winit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ pub struct EguiGlow {
pub egui_winit: egui_winit::State,
pub painter: crate::Painter,

// output from the last update:
shapes: Vec<egui::epaint::ClippedShape>,
pixels_per_point: f32,
textures_delta: egui::TexturesDelta,
}

Expand All @@ -28,15 +30,19 @@ impl EguiGlow {
})
.unwrap();

let egui_winit = egui_winit::State::new(
event_loop,
native_pixels_per_point,
Some(painter.max_texture_side()),
);
let pixels_per_point = egui_winit.pixels_per_point();

Self {
egui_ctx: Default::default(),
egui_winit: egui_winit::State::new(
event_loop,
native_pixels_per_point,
Some(painter.max_texture_side()),
),
egui_winit,
painter,
shapes: Default::default(),
pixels_per_point,
textures_delta: Default::default(),
}
}
Expand All @@ -55,6 +61,7 @@ impl EguiGlow {
platform_output,
textures_delta,
shapes,
pixels_per_point,
viewports,
viewport_commands,
} = self.egui_ctx.run(raw_input, run_ui);
Expand All @@ -76,6 +83,7 @@ impl EguiGlow {
);

self.shapes = shapes;
self.pixels_per_point = pixels_per_point;
self.textures_delta.append(textures_delta);
}

Expand All @@ -88,7 +96,7 @@ impl EguiGlow {
self.painter.set_texture(id, &image_delta);
}

let pixels_per_point = self.egui_ctx.pixels_per_point();
let pixels_per_point = self.pixels_per_point;
let clipped_primitives = self.egui_ctx.tessellate(shapes, pixels_per_point);
let dimensions: [u32; 2] = window.inner_size().into();
self.painter
Expand Down

0 comments on commit 1b1be3c

Please sign in to comment.