mirror of
https://github.com/emilk/egui.git
synced 2026-09-02 14:50:03 -04:00
Add Harness::new_eframe and TestRenderer trait (#5539)
Co-authored-by: Andreas Reich <r_andreas2@web.de>
This commit is contained in:
@@ -5,37 +5,22 @@ type AppKindUiState<'a, State> = Box<dyn FnMut(&mut egui::Ui, &mut State) + 'a>;
|
||||
type AppKindContext<'a> = Box<dyn FnMut(&egui::Context) + 'a>;
|
||||
type AppKindUi<'a> = Box<dyn FnMut(&mut egui::Ui) + 'a>;
|
||||
|
||||
/// In order to access the [`eframe::App`] trait from the generic `State`, we store a function pointer
|
||||
/// here that will return the dyn trait from the struct. In the builder we have the correct where
|
||||
/// clause to be able to create this.
|
||||
/// Later we can use it anywhere to get the [`eframe::App`] from the `State`.
|
||||
#[cfg(feature = "eframe")]
|
||||
type AppKindEframe<'a, State> = (fn(&mut State) -> &mut dyn eframe::App, eframe::Frame);
|
||||
|
||||
pub(crate) enum AppKind<'a, State> {
|
||||
Context(AppKindContext<'a>),
|
||||
Ui(AppKindUi<'a>),
|
||||
ContextState(AppKindContextState<'a, State>),
|
||||
UiState(AppKindUiState<'a, State>),
|
||||
#[cfg(feature = "eframe")]
|
||||
Eframe(AppKindEframe<'a, State>),
|
||||
}
|
||||
|
||||
// TODO(lucasmerlin): These aren't working unfortunately :(
|
||||
// I think they should work though: https://geo-ant.github.io/blog/2021/rust-traits-and-variadic-functions/
|
||||
// pub trait IntoAppKind<'a, UiKind> {
|
||||
// fn into_harness_kind(self) -> AppKind<'a>;
|
||||
// }
|
||||
//
|
||||
// impl<'a, F> IntoAppKind<'a, &egui::Context> for F
|
||||
// where
|
||||
// F: FnMut(&egui::Context) + 'a,
|
||||
// {
|
||||
// fn into_harness_kind(self) -> AppKind<'a> {
|
||||
// AppKind::Context(Box::new(self))
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// impl<'a, F> IntoAppKind<'a, &mut egui::Ui> for F
|
||||
// where
|
||||
// F: FnMut(&mut egui::Ui) + 'a,
|
||||
// {
|
||||
// fn into_harness_kind(self) -> AppKind<'a> {
|
||||
// AppKind::Ui(Box::new(self))
|
||||
// }
|
||||
// }
|
||||
|
||||
impl<'a, State> AppKind<'a, State> {
|
||||
pub fn run(
|
||||
&mut self,
|
||||
@@ -54,6 +39,12 @@ impl<'a, State> AppKind<'a, State> {
|
||||
f(ctx, state);
|
||||
None
|
||||
}
|
||||
#[cfg(feature = "eframe")]
|
||||
AppKind::Eframe((get_app, frame)) => {
|
||||
let app = get_app(state);
|
||||
app.update(ctx, frame);
|
||||
None
|
||||
}
|
||||
kind_ui => Some(kind_ui.run_ui(ctx, state, sizing_pass)),
|
||||
}
|
||||
}
|
||||
@@ -78,7 +69,9 @@ impl<'a, State> AppKind<'a, State> {
|
||||
.show(ui, |ui| match self {
|
||||
AppKind::Ui(f) => f(ui),
|
||||
AppKind::UiState(f) => f(ui, state),
|
||||
_ => unreachable!(),
|
||||
_ => unreachable!(
|
||||
"run_ui should only be called with AppKind::Ui or AppKind UiState"
|
||||
),
|
||||
});
|
||||
})
|
||||
.response
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
use crate::app_kind::AppKind;
|
||||
use crate::Harness;
|
||||
use crate::wgpu::WgpuTestRenderer;
|
||||
use crate::{Harness, LazyRenderer, TestRenderer};
|
||||
use egui::{Pos2, Rect, Vec2};
|
||||
use std::marker::PhantomData;
|
||||
|
||||
@@ -8,6 +9,7 @@ pub struct HarnessBuilder<State = ()> {
|
||||
pub(crate) screen_rect: Rect,
|
||||
pub(crate) pixels_per_point: f32,
|
||||
pub(crate) state: PhantomData<State>,
|
||||
pub(crate) renderer: Box<dyn TestRenderer>,
|
||||
}
|
||||
|
||||
impl<State> Default for HarnessBuilder<State> {
|
||||
@@ -16,6 +18,7 @@ impl<State> Default for HarnessBuilder<State> {
|
||||
screen_rect: Rect::from_min_size(Pos2::ZERO, Vec2::new(800.0, 600.0)),
|
||||
pixels_per_point: 1.0,
|
||||
state: PhantomData,
|
||||
renderer: Box::new(LazyRenderer::default()),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -37,6 +40,29 @@ impl<State> HarnessBuilder<State> {
|
||||
self
|
||||
}
|
||||
|
||||
/// Set the [`TestRenderer`] to use for rendering.
|
||||
///
|
||||
/// By default, a [`LazyRenderer`] is used.
|
||||
#[inline]
|
||||
pub fn renderer(mut self, renderer: impl TestRenderer + 'static) -> Self {
|
||||
self.renderer = Box::new(renderer);
|
||||
self
|
||||
}
|
||||
|
||||
/// Enable wgpu rendering with a default setup suitable for testing.
|
||||
///
|
||||
/// This sets up a [`WgpuTestRenderer`] with the default setup.
|
||||
#[cfg(feature = "wgpu")]
|
||||
pub fn wgpu(self) -> Self {
|
||||
self.renderer(WgpuTestRenderer::default())
|
||||
}
|
||||
|
||||
/// Enable wgpu rendering with the given setup.
|
||||
#[cfg(feature = "wgpu")]
|
||||
pub fn wgpu_setup(self, setup: egui_wgpu::WgpuSetup) -> Self {
|
||||
self.renderer(WgpuTestRenderer::from_setup(setup))
|
||||
}
|
||||
|
||||
/// Create a new Harness with the given app closure and a state.
|
||||
///
|
||||
/// The app closure will immediately be called once to create the initial ui.
|
||||
@@ -66,7 +92,7 @@ impl<State> HarnessBuilder<State> {
|
||||
app: impl FnMut(&egui::Context, &mut State) + 'a,
|
||||
state: State,
|
||||
) -> Harness<'a, State> {
|
||||
Harness::from_builder(&self, AppKind::ContextState(Box::new(app)), state)
|
||||
Harness::from_builder(self, AppKind::ContextState(Box::new(app)), state, None)
|
||||
}
|
||||
|
||||
/// Create a new Harness with the given ui closure and a state.
|
||||
@@ -95,7 +121,30 @@ impl<State> HarnessBuilder<State> {
|
||||
app: impl FnMut(&mut egui::Ui, &mut State) + 'a,
|
||||
state: State,
|
||||
) -> Harness<'a, State> {
|
||||
Harness::from_builder(&self, AppKind::UiState(Box::new(app)), state)
|
||||
Harness::from_builder(self, AppKind::UiState(Box::new(app)), state, None)
|
||||
}
|
||||
|
||||
/// Create a new [Harness] from the given eframe creation closure.
|
||||
/// The app can be accessed via the [`Harness::state`] / [`Harness::state_mut`] methods.
|
||||
#[cfg(feature = "eframe")]
|
||||
pub fn build_eframe<'a>(
|
||||
self,
|
||||
build: impl FnOnce(&mut eframe::CreationContext<'a>) -> State,
|
||||
) -> Harness<'a, State>
|
||||
where
|
||||
State: eframe::App,
|
||||
{
|
||||
let ctx = egui::Context::default();
|
||||
|
||||
let mut cc = eframe::CreationContext::_new_kittest(ctx.clone());
|
||||
let mut frame = eframe::Frame::_new_kittest();
|
||||
|
||||
self.renderer.setup_eframe(&mut cc, &mut frame);
|
||||
|
||||
let app = build(&mut cc);
|
||||
|
||||
let kind = AppKind::Eframe((|state| state, frame));
|
||||
Harness::from_builder(self, kind, app, Some(ctx))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -119,7 +168,7 @@ impl HarnessBuilder {
|
||||
/// });
|
||||
/// ```
|
||||
pub fn build<'a>(self, app: impl FnMut(&egui::Context) + 'a) -> Harness<'a> {
|
||||
Harness::from_builder(&self, AppKind::Context(Box::new(app)), ())
|
||||
Harness::from_builder(self, AppKind::Context(Box::new(app)), (), None)
|
||||
}
|
||||
|
||||
/// Create a new Harness with the given ui closure.
|
||||
@@ -138,6 +187,6 @@ impl HarnessBuilder {
|
||||
/// });
|
||||
/// ```
|
||||
pub fn build_ui<'a>(self, app: impl FnMut(&mut egui::Ui) + 'a) -> Harness<'a> {
|
||||
Harness::from_builder(&self, AppKind::Ui(Box::new(app)), ())
|
||||
Harness::from_builder(self, AppKind::Ui(Box::new(app)), (), None)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,18 +12,21 @@ mod snapshot;
|
||||
pub use snapshot::*;
|
||||
use std::fmt::{Debug, Formatter};
|
||||
mod app_kind;
|
||||
mod renderer;
|
||||
#[cfg(feature = "wgpu")]
|
||||
mod texture_to_image;
|
||||
#[cfg(feature = "wgpu")]
|
||||
pub mod wgpu;
|
||||
|
||||
pub use kittest;
|
||||
use std::mem;
|
||||
|
||||
use crate::app_kind::AppKind;
|
||||
use crate::event::EventState;
|
||||
|
||||
pub use builder::*;
|
||||
use egui::{Pos2, Rect, TexturesDelta, Vec2, ViewportId};
|
||||
pub use renderer::*;
|
||||
|
||||
use egui::{Modifiers, Pos2, Rect, Vec2, ViewportId};
|
||||
use kittest::{Node, Queryable};
|
||||
|
||||
/// The test Harness. This contains everything needed to run the test.
|
||||
@@ -37,11 +40,11 @@ pub struct Harness<'a, State = ()> {
|
||||
input: egui::RawInput,
|
||||
kittest: kittest::State,
|
||||
output: egui::FullOutput,
|
||||
texture_deltas: Vec<TexturesDelta>,
|
||||
app: AppKind<'a, State>,
|
||||
event_state: EventState,
|
||||
response: Option<egui::Response>,
|
||||
state: State,
|
||||
renderer: Box<dyn TestRenderer>,
|
||||
}
|
||||
|
||||
impl<'a, State> Debug for Harness<'a, State> {
|
||||
@@ -52,11 +55,12 @@ impl<'a, State> Debug for Harness<'a, State> {
|
||||
|
||||
impl<'a, State> Harness<'a, State> {
|
||||
pub(crate) fn from_builder(
|
||||
builder: &HarnessBuilder<State>,
|
||||
builder: HarnessBuilder<State>,
|
||||
mut app: AppKind<'a, State>,
|
||||
mut state: State,
|
||||
ctx: Option<egui::Context>,
|
||||
) -> Self {
|
||||
let ctx = egui::Context::default();
|
||||
let ctx = ctx.unwrap_or_default();
|
||||
ctx.enable_accesskit();
|
||||
let mut input = egui::RawInput {
|
||||
screen_rect: Some(builder.screen_rect),
|
||||
@@ -73,6 +77,9 @@ impl<'a, State> Harness<'a, State> {
|
||||
response = app.run(ctx, &mut state, false);
|
||||
});
|
||||
|
||||
let mut renderer = builder.renderer;
|
||||
renderer.handle_delta(&output.textures_delta);
|
||||
|
||||
let mut harness = Self {
|
||||
app,
|
||||
ctx,
|
||||
@@ -84,11 +91,11 @@ impl<'a, State> Harness<'a, State> {
|
||||
.take()
|
||||
.expect("AccessKit was disabled"),
|
||||
),
|
||||
texture_deltas: vec![mem::take(&mut output.textures_delta)],
|
||||
output,
|
||||
response,
|
||||
event_state: EventState::default(),
|
||||
state,
|
||||
renderer,
|
||||
};
|
||||
// Run the harness until it is stable, ensuring that all Areas are shown and animations are done
|
||||
harness.run();
|
||||
@@ -153,6 +160,15 @@ impl<'a, State> Harness<'a, State> {
|
||||
Self::builder().build_ui_state(app, state)
|
||||
}
|
||||
|
||||
/// Create a new [Harness] from the given eframe creation closure.
|
||||
#[cfg(feature = "eframe")]
|
||||
pub fn new_eframe(builder: impl FnOnce(&mut eframe::CreationContext<'a>) -> State) -> Self
|
||||
where
|
||||
State: eframe::App,
|
||||
{
|
||||
Self::builder().build_eframe(builder)
|
||||
}
|
||||
|
||||
/// Set the size of the window.
|
||||
/// Note: If you only want to set the size once at the beginning,
|
||||
/// prefer using [`HarnessBuilder::with_size`].
|
||||
@@ -194,8 +210,7 @@ impl<'a, State> Harness<'a, State> {
|
||||
.take()
|
||||
.expect("AccessKit was disabled"),
|
||||
);
|
||||
self.texture_deltas
|
||||
.push(mem::take(&mut output.textures_delta));
|
||||
self.renderer.handle_delta(&output.textures_delta);
|
||||
self.output = output;
|
||||
}
|
||||
|
||||
@@ -253,21 +268,35 @@ impl<'a, State> Harness<'a, State> {
|
||||
/// Press a key.
|
||||
/// This will create a key down event and a key up event.
|
||||
pub fn press_key(&mut self, key: egui::Key) {
|
||||
self.press_key_modifiers(Modifiers::default(), key);
|
||||
}
|
||||
|
||||
/// Press a key with modifiers.
|
||||
/// This will create a key down event and a key up event.
|
||||
pub fn press_key_modifiers(&mut self, modifiers: Modifiers, key: egui::Key) {
|
||||
self.input.events.push(egui::Event::Key {
|
||||
key,
|
||||
pressed: true,
|
||||
modifiers: Default::default(),
|
||||
modifiers,
|
||||
repeat: false,
|
||||
physical_key: None,
|
||||
});
|
||||
self.input.events.push(egui::Event::Key {
|
||||
key,
|
||||
pressed: false,
|
||||
modifiers: Default::default(),
|
||||
modifiers,
|
||||
repeat: false,
|
||||
physical_key: None,
|
||||
});
|
||||
}
|
||||
|
||||
/// Render the last output to an image.
|
||||
///
|
||||
/// # Errors
|
||||
/// Returns an error if the rendering fails.
|
||||
pub fn render(&mut self) -> Result<image::RgbaImage, String> {
|
||||
self.renderer.render(&self.ctx, &self.output)
|
||||
}
|
||||
}
|
||||
|
||||
/// Utilities for stateless harnesses.
|
||||
|
||||
81
crates/egui_kittest/src/renderer.rs
Normal file
81
crates/egui_kittest/src/renderer.rs
Normal file
@@ -0,0 +1,81 @@
|
||||
use egui::{Context, FullOutput, TexturesDelta};
|
||||
use image::RgbaImage;
|
||||
|
||||
pub trait TestRenderer {
|
||||
/// We use this to pass the glow / wgpu render state to [`eframe::Frame`].
|
||||
#[cfg(feature = "eframe")]
|
||||
fn setup_eframe(&self, _cc: &mut eframe::CreationContext<'_>, _frame: &mut eframe::Frame) {}
|
||||
|
||||
/// Handle a [`TexturesDelta`] by updating the renderer's textures.
|
||||
fn handle_delta(&mut self, delta: &TexturesDelta);
|
||||
|
||||
/// Render the [`crate::Harness`] and return the resulting image.
|
||||
///
|
||||
/// # Errors
|
||||
/// Returns an error if the rendering fails.
|
||||
fn render(&mut self, ctx: &Context, output: &FullOutput) -> Result<RgbaImage, String>;
|
||||
}
|
||||
|
||||
/// A lazy renderer that initializes the renderer on the first render call.
|
||||
///
|
||||
/// By default, this will create a wgpu renderer if the wgpu feature is enabled.
|
||||
pub enum LazyRenderer {
|
||||
Uninitialized {
|
||||
texture_ops: Vec<egui::TexturesDelta>,
|
||||
builder: Option<Box<dyn FnOnce() -> Box<dyn TestRenderer>>>,
|
||||
},
|
||||
Initialized {
|
||||
renderer: Box<dyn TestRenderer>,
|
||||
},
|
||||
}
|
||||
|
||||
impl Default for LazyRenderer {
|
||||
fn default() -> Self {
|
||||
#[cfg(feature = "wgpu")]
|
||||
return Self::new(crate::wgpu::WgpuTestRenderer::new);
|
||||
#[cfg(not(feature = "wgpu"))]
|
||||
return Self::Uninitialized {
|
||||
texture_ops: Vec::new(),
|
||||
builder: None,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
impl LazyRenderer {
|
||||
pub fn new<T: TestRenderer + 'static>(create_renderer: impl FnOnce() -> T + 'static) -> Self {
|
||||
Self::Uninitialized {
|
||||
texture_ops: Vec::new(),
|
||||
builder: Some(Box::new(move || Box::new(create_renderer()))),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl TestRenderer for LazyRenderer {
|
||||
fn handle_delta(&mut self, delta: &TexturesDelta) {
|
||||
match self {
|
||||
Self::Uninitialized { texture_ops, .. } => texture_ops.push(delta.clone()),
|
||||
Self::Initialized { renderer } => renderer.handle_delta(delta),
|
||||
}
|
||||
}
|
||||
|
||||
fn render(&mut self, ctx: &Context, output: &FullOutput) -> Result<RgbaImage, String> {
|
||||
match self {
|
||||
Self::Uninitialized {
|
||||
texture_ops,
|
||||
builder: build,
|
||||
} => {
|
||||
let mut renderer = build.take().ok_or({
|
||||
"No default renderer available. \
|
||||
Enable the wgpu feature or set one via HarnessBuilder::renderer"
|
||||
})?();
|
||||
for delta in texture_ops.drain(..) {
|
||||
renderer.handle_delta(&delta);
|
||||
}
|
||||
let image = renderer.render(ctx, output)?;
|
||||
*self = Self::Initialized { renderer };
|
||||
Ok(image)
|
||||
}
|
||||
Self::Initialized { renderer } => renderer.render(ctx, output),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -93,6 +93,12 @@ pub enum SnapshotError {
|
||||
/// The error that occurred
|
||||
err: ImageError,
|
||||
},
|
||||
|
||||
/// Error rendering the image
|
||||
RenderError {
|
||||
/// The error that occurred
|
||||
err: String,
|
||||
},
|
||||
}
|
||||
|
||||
const HOW_TO_UPDATE_SCREENSHOTS: &str =
|
||||
@@ -142,6 +148,9 @@ impl Display for SnapshotError {
|
||||
let path = std::path::absolute(path).unwrap_or(path.clone());
|
||||
write!(f, "Error writing snapshot: {err:?}\nAt: {path:?}")
|
||||
}
|
||||
Self::RenderError { err } => {
|
||||
write!(f, "Error rendering image: {err:?}")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -315,7 +324,7 @@ pub fn image_snapshot(current: &image::RgbaImage, name: &str) {
|
||||
|
||||
#[cfg(feature = "wgpu")]
|
||||
impl<State> Harness<'_, State> {
|
||||
/// Render a image using a default [`crate::wgpu::TestRenderer`] and compare it to the snapshot
|
||||
/// Render a image using the setup [`crate::TestRenderer`] and compare it to the snapshot
|
||||
/// with custom options.
|
||||
///
|
||||
/// If you want to change the default options for your whole project, you could create an
|
||||
@@ -323,7 +332,7 @@ impl<State> Harness<'_, State> {
|
||||
/// new `my_image_snapshot` function on the Harness that calls this function with the desired options.
|
||||
/// You could additionally use the
|
||||
/// [disallowed_methods](https://rust-lang.github.io/rust-clippy/master/#disallowed_methods)
|
||||
/// lint to disable use of the [`Harness::wgpu_snapshot`] to prevent accidentally using the wrong defaults.
|
||||
/// lint to disable use of the [`Harness::snapshot`] to prevent accidentally using the wrong defaults.
|
||||
///
|
||||
/// The snapshot files will be saved under [`SnapshotOptions::output_path`].
|
||||
/// The snapshot will be saved under `{output_path}/{name}.png`.
|
||||
@@ -331,31 +340,35 @@ impl<State> Harness<'_, State> {
|
||||
/// If new image didn't match the snapshot, a diff image will be saved under `{output_path}/{name}.diff.png`.
|
||||
///
|
||||
/// # Errors
|
||||
/// Returns a [`SnapshotError`] if the image does not match the snapshot or if there was an error
|
||||
/// reading or writing the snapshot.
|
||||
pub fn try_wgpu_snapshot_options(
|
||||
&self,
|
||||
/// Returns a [`SnapshotError`] if the image does not match the snapshot, if there was an
|
||||
/// error reading or writing the snapshot, if the rendering fails or if no default renderer is available.
|
||||
pub fn try_snapshot_options(
|
||||
&mut self,
|
||||
name: &str,
|
||||
options: &SnapshotOptions,
|
||||
) -> Result<(), SnapshotError> {
|
||||
let image = crate::wgpu::TestRenderer::new().render(self);
|
||||
let image = self
|
||||
.render()
|
||||
.map_err(|err| SnapshotError::RenderError { err })?;
|
||||
try_image_snapshot_options(&image, name, options)
|
||||
}
|
||||
|
||||
/// Render a image using a default [`crate::wgpu::TestRenderer`] and compare it to the snapshot.
|
||||
/// Render a image using the setup [`crate::TestRenderer`] and compare it to the snapshot.
|
||||
/// The snapshot will be saved under `tests/snapshots/{name}.png`.
|
||||
/// The new image from the last test run will be saved under `tests/snapshots/{name}.new.png`.
|
||||
/// If new image didn't match the snapshot, a diff image will be saved under `tests/snapshots/{name}.diff.png`.
|
||||
///
|
||||
/// # Errors
|
||||
/// Returns a [`SnapshotError`] if the image does not match the snapshot or if there was an error
|
||||
/// reading or writing the snapshot.
|
||||
pub fn try_wgpu_snapshot(&self, name: &str) -> Result<(), SnapshotError> {
|
||||
let image = crate::wgpu::TestRenderer::new().render(self);
|
||||
/// Returns a [`SnapshotError`] if the image does not match the snapshot, if there was an
|
||||
/// error reading or writing the snapshot, if the rendering fails or if no default renderer is available.
|
||||
pub fn try_snapshot(&mut self, name: &str) -> Result<(), SnapshotError> {
|
||||
let image = self
|
||||
.render()
|
||||
.map_err(|err| SnapshotError::RenderError { err })?;
|
||||
try_image_snapshot(&image, name)
|
||||
}
|
||||
|
||||
/// Render a image using a default [`crate::wgpu::TestRenderer`] and compare it to the snapshot
|
||||
/// Render a image using the setup [`crate::TestRenderer`] and compare it to the snapshot
|
||||
/// with custom options.
|
||||
///
|
||||
/// If you want to change the default options for your whole project, you could create an
|
||||
@@ -363,7 +376,7 @@ impl<State> Harness<'_, State> {
|
||||
/// new `my_image_snapshot` function on the Harness that calls this function with the desired options.
|
||||
/// You could additionally use the
|
||||
/// [disallowed_methods](https://rust-lang.github.io/rust-clippy/master/#disallowed_methods)
|
||||
/// lint to disable use of the [`Harness::wgpu_snapshot`] to prevent accidentally using the wrong defaults.
|
||||
/// lint to disable use of the [`Harness::snapshot`] to prevent accidentally using the wrong defaults.
|
||||
///
|
||||
/// The snapshot files will be saved under [`SnapshotOptions::output_path`].
|
||||
/// The snapshot will be saved under `{output_path}/{name}.png`.
|
||||
@@ -371,11 +384,11 @@ impl<State> Harness<'_, State> {
|
||||
/// If new image didn't match the snapshot, a diff image will be saved under `{output_path}/{name}.diff.png`.
|
||||
///
|
||||
/// # Panics
|
||||
/// Panics if the image does not match the snapshot or if there was an error reading or writing the
|
||||
/// snapshot.
|
||||
/// Panics if the image does not match the snapshot, if there was an error reading or writing the
|
||||
/// snapshot, if the rendering fails or if no default renderer is available.
|
||||
#[track_caller]
|
||||
pub fn wgpu_snapshot_options(&self, name: &str, options: &SnapshotOptions) {
|
||||
match self.try_wgpu_snapshot_options(name, options) {
|
||||
pub fn snapshot_options(&mut self, name: &str, options: &SnapshotOptions) {
|
||||
match self.try_snapshot_options(name, options) {
|
||||
Ok(_) => {}
|
||||
Err(err) => {
|
||||
panic!("{}", err);
|
||||
@@ -383,17 +396,17 @@ impl<State> Harness<'_, State> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Render a image using a default [`crate::wgpu::TestRenderer`] and compare it to the snapshot.
|
||||
/// Render a image using the setup [`crate::TestRenderer`] and compare it to the snapshot.
|
||||
/// The snapshot will be saved under `tests/snapshots/{name}.png`.
|
||||
/// The new image from the last test run will be saved under `tests/snapshots/{name}.new.png`.
|
||||
/// If new image didn't match the snapshot, a diff image will be saved under `tests/snapshots/{name}.diff.png`.
|
||||
///
|
||||
/// # Panics
|
||||
/// Panics if the image does not match the snapshot or if there was an error reading or writing the
|
||||
/// snapshot.
|
||||
/// Panics if the image does not match the snapshot, if there was an error reading or writing the
|
||||
/// snapshot, if the rendering fails or if no default renderer is available.
|
||||
#[track_caller]
|
||||
pub fn wgpu_snapshot(&self, name: &str) {
|
||||
match self.try_wgpu_snapshot(name) {
|
||||
pub fn snapshot(&mut self, name: &str) {
|
||||
match self.try_snapshot(name) {
|
||||
Ok(_) => {}
|
||||
Err(err) => {
|
||||
panic!("{}", err);
|
||||
@@ -401,3 +414,45 @@ impl<State> Harness<'_, State> {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Deprecated wgpu_snapshot functions
|
||||
// TODO(lucasmerlin): Remove in 0.32
|
||||
#[allow(clippy::missing_errors_doc)]
|
||||
#[cfg(feature = "wgpu")]
|
||||
impl<State> Harness<'_, State> {
|
||||
#[deprecated(
|
||||
since = "0.31.0",
|
||||
note = "Use `try_snapshot_options` instead. This function will be removed in 0.32"
|
||||
)]
|
||||
pub fn try_wgpu_snapshot_options(
|
||||
&mut self,
|
||||
name: &str,
|
||||
options: &SnapshotOptions,
|
||||
) -> Result<(), SnapshotError> {
|
||||
self.try_snapshot_options(name, options)
|
||||
}
|
||||
|
||||
#[deprecated(
|
||||
since = "0.31.0",
|
||||
note = "Use `try_snapshot` instead. This function will be removed in 0.32"
|
||||
)]
|
||||
pub fn try_wgpu_snapshot(&mut self, name: &str) -> Result<(), SnapshotError> {
|
||||
self.try_snapshot(name)
|
||||
}
|
||||
|
||||
#[deprecated(
|
||||
since = "0.31.0",
|
||||
note = "Use `snapshot_options` instead. This function will be removed in 0.32"
|
||||
)]
|
||||
pub fn wgpu_snapshot_options(&mut self, name: &str, options: &SnapshotOptions) {
|
||||
self.snapshot_options(name, options);
|
||||
}
|
||||
|
||||
#[deprecated(
|
||||
since = "0.31.0",
|
||||
note = "Use `snapshot` instead. This function will be removed in 0.32"
|
||||
)]
|
||||
pub fn wgpu_snapshot(&mut self, name: &str) {
|
||||
self.snapshot(name);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,122 +1,152 @@
|
||||
use crate::texture_to_image::texture_to_image;
|
||||
use crate::Harness;
|
||||
use egui_wgpu::wgpu::{Backends, InstanceDescriptor, StoreOp, TextureFormat};
|
||||
use egui_wgpu::{wgpu, ScreenDescriptor};
|
||||
use eframe::epaint::TextureId;
|
||||
use egui::TexturesDelta;
|
||||
use egui_wgpu::wgpu::{Backends, StoreOp, TextureFormat};
|
||||
use egui_wgpu::{wgpu, RenderState, ScreenDescriptor, WgpuSetup};
|
||||
use image::RgbaImage;
|
||||
use std::iter::once;
|
||||
use std::sync::Arc;
|
||||
use wgpu::Maintain;
|
||||
|
||||
/// Utility to render snapshots from a [`Harness`] using [`egui_wgpu`].
|
||||
pub struct TestRenderer {
|
||||
device: wgpu::Device,
|
||||
queue: wgpu::Queue,
|
||||
dithering: bool,
|
||||
// TODO(#5506): Replace this with the setup from https://github.com/emilk/egui/pull/5506
|
||||
pub fn default_wgpu_setup() -> egui_wgpu::WgpuSetup {
|
||||
egui_wgpu::WgpuSetup::CreateNew {
|
||||
supported_backends: Backends::all(),
|
||||
device_descriptor: Arc::new(|_| wgpu::DeviceDescriptor::default()),
|
||||
power_preference: wgpu::PowerPreference::default(),
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for TestRenderer {
|
||||
pub fn create_render_state(setup: WgpuSetup) -> egui_wgpu::RenderState {
|
||||
let instance = match &setup {
|
||||
WgpuSetup::Existing { instance, .. } => instance.clone(),
|
||||
WgpuSetup::CreateNew { .. } => Default::default(),
|
||||
};
|
||||
|
||||
pollster::block_on(egui_wgpu::RenderState::create(
|
||||
&egui_wgpu::WgpuConfiguration {
|
||||
wgpu_setup: setup,
|
||||
..Default::default()
|
||||
},
|
||||
&instance,
|
||||
None,
|
||||
None,
|
||||
1,
|
||||
false,
|
||||
))
|
||||
.expect("Failed to create render state")
|
||||
}
|
||||
|
||||
/// Utility to render snapshots from a [`crate::Harness`] using [`egui_wgpu`].
|
||||
pub struct WgpuTestRenderer {
|
||||
render_state: RenderState,
|
||||
}
|
||||
|
||||
impl Default for WgpuTestRenderer {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl TestRenderer {
|
||||
/// Create a new [`TestRenderer`] using a default [`wgpu::Instance`].
|
||||
impl WgpuTestRenderer {
|
||||
/// Create a new [`WgpuTestRenderer`] with the default setup.
|
||||
pub fn new() -> Self {
|
||||
let instance = wgpu::Instance::new(InstanceDescriptor::default());
|
||||
|
||||
let adapters = instance.enumerate_adapters(Backends::all());
|
||||
let adapter = adapters.first().expect("No adapter found");
|
||||
|
||||
let (device, queue) = pollster::block_on(adapter.request_device(
|
||||
&wgpu::DeviceDescriptor {
|
||||
label: Some("Egui Device"),
|
||||
memory_hints: Default::default(),
|
||||
required_limits: Default::default(),
|
||||
required_features: Default::default(),
|
||||
},
|
||||
None,
|
||||
))
|
||||
.expect("Failed to create device");
|
||||
|
||||
Self::create(device, queue)
|
||||
}
|
||||
|
||||
/// Create a new [`TestRenderer`] using the provided [`wgpu::Device`] and [`wgpu::Queue`].
|
||||
pub fn create(device: wgpu::Device, queue: wgpu::Queue) -> Self {
|
||||
Self {
|
||||
device,
|
||||
queue,
|
||||
dithering: false,
|
||||
render_state: create_render_state(default_wgpu_setup()),
|
||||
}
|
||||
}
|
||||
|
||||
/// Enable or disable dithering.
|
||||
/// Create a new [`WgpuTestRenderer`] with the given setup.
|
||||
pub fn from_setup(setup: WgpuSetup) -> Self {
|
||||
Self {
|
||||
render_state: create_render_state(setup),
|
||||
}
|
||||
}
|
||||
|
||||
/// Create a new [`WgpuTestRenderer`] from an existing [`RenderState`].
|
||||
///
|
||||
/// Disabled by default.
|
||||
#[inline]
|
||||
pub fn with_dithering(mut self, dithering: bool) -> Self {
|
||||
self.dithering = dithering;
|
||||
self
|
||||
/// # Panics
|
||||
/// Panics if the [`RenderState`] has been used before.
|
||||
pub fn from_render_state(render_state: RenderState) -> Self {
|
||||
assert!(
|
||||
render_state
|
||||
.renderer
|
||||
.read()
|
||||
.texture(&TextureId::Managed(0))
|
||||
.is_none(),
|
||||
"The RenderState passed in has been used before, pass in a fresh RenderState instead."
|
||||
);
|
||||
Self { render_state }
|
||||
}
|
||||
}
|
||||
|
||||
impl crate::TestRenderer for WgpuTestRenderer {
|
||||
#[cfg(feature = "eframe")]
|
||||
fn setup_eframe(&self, cc: &mut eframe::CreationContext<'_>, frame: &mut eframe::Frame) {
|
||||
cc.wgpu_render_state = Some(self.render_state.clone());
|
||||
frame.wgpu_render_state = Some(self.render_state.clone());
|
||||
}
|
||||
|
||||
/// Render the [`Harness`] and return the resulting image.
|
||||
pub fn render<State>(&self, harness: &Harness<'_, State>) -> RgbaImage {
|
||||
// We need to create a new renderer each time we render, since the renderer stores
|
||||
// textures related to the Harnesses' egui Context.
|
||||
// Calling the renderer from different Harnesses would cause problems if we store the renderer.
|
||||
let mut renderer = egui_wgpu::Renderer::new(
|
||||
&self.device,
|
||||
TextureFormat::Rgba8Unorm,
|
||||
None,
|
||||
1,
|
||||
self.dithering,
|
||||
);
|
||||
|
||||
for delta in &harness.texture_deltas {
|
||||
for (id, image_delta) in &delta.set {
|
||||
renderer.update_texture(&self.device, &self.queue, *id, image_delta);
|
||||
}
|
||||
fn handle_delta(&mut self, delta: &TexturesDelta) {
|
||||
let mut renderer = self.render_state.renderer.write();
|
||||
for (id, image) in &delta.set {
|
||||
renderer.update_texture(
|
||||
&self.render_state.device,
|
||||
&self.render_state.queue,
|
||||
*id,
|
||||
image,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
let mut encoder = self
|
||||
.device
|
||||
.create_command_encoder(&wgpu::CommandEncoderDescriptor {
|
||||
label: Some("Egui Command Encoder"),
|
||||
});
|
||||
/// Render the [`crate::Harness`] and return the resulting image.
|
||||
fn render(
|
||||
&mut self,
|
||||
ctx: &egui::Context,
|
||||
output: &egui::FullOutput,
|
||||
) -> Result<RgbaImage, String> {
|
||||
let mut renderer = self.render_state.renderer.write();
|
||||
|
||||
let size = harness.ctx.screen_rect().size() * harness.ctx.pixels_per_point();
|
||||
let mut encoder =
|
||||
self.render_state
|
||||
.device
|
||||
.create_command_encoder(&wgpu::CommandEncoderDescriptor {
|
||||
label: Some("Egui Command Encoder"),
|
||||
});
|
||||
|
||||
let size = ctx.screen_rect().size() * ctx.pixels_per_point();
|
||||
let screen = ScreenDescriptor {
|
||||
pixels_per_point: harness.ctx.pixels_per_point(),
|
||||
pixels_per_point: ctx.pixels_per_point(),
|
||||
size_in_pixels: [size.x.round() as u32, size.y.round() as u32],
|
||||
};
|
||||
|
||||
let tessellated = harness.ctx.tessellate(
|
||||
harness.output().shapes.clone(),
|
||||
harness.ctx.pixels_per_point(),
|
||||
);
|
||||
let tessellated = ctx.tessellate(output.shapes.clone(), ctx.pixels_per_point());
|
||||
|
||||
let user_buffers = renderer.update_buffers(
|
||||
&self.device,
|
||||
&self.queue,
|
||||
&self.render_state.device,
|
||||
&self.render_state.queue,
|
||||
&mut encoder,
|
||||
&tessellated,
|
||||
&screen,
|
||||
);
|
||||
|
||||
let texture = self.device.create_texture(&wgpu::TextureDescriptor {
|
||||
label: Some("Egui Texture"),
|
||||
size: wgpu::Extent3d {
|
||||
width: screen.size_in_pixels[0],
|
||||
height: screen.size_in_pixels[1],
|
||||
depth_or_array_layers: 1,
|
||||
},
|
||||
mip_level_count: 1,
|
||||
sample_count: 1,
|
||||
dimension: wgpu::TextureDimension::D2,
|
||||
format: TextureFormat::Rgba8Unorm,
|
||||
usage: wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::COPY_SRC,
|
||||
view_formats: &[],
|
||||
});
|
||||
let texture = self
|
||||
.render_state
|
||||
.device
|
||||
.create_texture(&wgpu::TextureDescriptor {
|
||||
label: Some("Egui Texture"),
|
||||
size: wgpu::Extent3d {
|
||||
width: screen.size_in_pixels[0],
|
||||
height: screen.size_in_pixels[1],
|
||||
depth_or_array_layers: 1,
|
||||
},
|
||||
mip_level_count: 1,
|
||||
sample_count: 1,
|
||||
dimension: wgpu::TextureDimension::D2,
|
||||
format: TextureFormat::Rgba8Unorm,
|
||||
usage: wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::COPY_SRC,
|
||||
view_formats: &[],
|
||||
});
|
||||
|
||||
let texture_view = texture.create_view(&wgpu::TextureViewDescriptor::default());
|
||||
|
||||
@@ -141,11 +171,16 @@ impl TestRenderer {
|
||||
renderer.render(&mut pass, &tessellated, &screen);
|
||||
}
|
||||
|
||||
self.queue
|
||||
self.render_state
|
||||
.queue
|
||||
.submit(user_buffers.into_iter().chain(once(encoder.finish())));
|
||||
|
||||
self.device.poll(Maintain::Wait);
|
||||
self.render_state.device.poll(Maintain::Wait);
|
||||
|
||||
texture_to_image(&self.device, &self.queue, &texture)
|
||||
Ok(texture_to_image(
|
||||
&self.render_state.device,
|
||||
&self.render_state.queue,
|
||||
&texture,
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user