Skip to content

Commit

Permalink
Allow creating WgpuTestRenderer from existing RenderState
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasmerlin committed Dec 29, 2024
1 parent a531bad commit 8f5f0ec
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
3 changes: 3 additions & 0 deletions crates/egui_kittest/src/renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ pub trait TestRenderer {
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>,
Expand Down
21 changes: 19 additions & 2 deletions crates/egui_kittest/src/wgpu.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::texture_to_image::texture_to_image;
use eframe::epaint::TextureId;
use egui::TexturesDelta;
use egui_wgpu::wgpu::{Backends, StoreOp, TextureFormat};
use egui_wgpu::{wgpu, RenderState, ScreenDescriptor, WgpuSetup};
Expand All @@ -16,7 +17,7 @@ pub fn default_wgpu_setup() -> egui_wgpu::WgpuSetup {
}
}

pub(crate) fn create_render_state(setup: WgpuSetup) -> egui_wgpu::RenderState {
pub fn create_render_state(setup: WgpuSetup) -> egui_wgpu::RenderState {
let instance = match &setup {
WgpuSetup::Existing { instance, .. } => instance.clone(),
WgpuSetup::CreateNew { .. } => Default::default(),
Expand All @@ -33,7 +34,7 @@ pub(crate) fn create_render_state(setup: WgpuSetup) -> egui_wgpu::RenderState {
1,
false,
))
.expect("Failed to create render state")
.expect("Failed to create render state")
}

/// Utility to render snapshots from a [`crate::Harness`] using [`egui_wgpu`].
Expand Down Expand Up @@ -61,6 +62,22 @@ impl WgpuTestRenderer {
render_state: create_render_state(setup),
}
}

/// Create a new [`WgpuTestRenderer`] from an existing [`RenderState`].
///
/// # 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 {
Expand Down

0 comments on commit 8f5f0ec

Please sign in to comment.