eframe: rename render_state to wgpu_render_state for added clarity

This commit is contained in:
Emil Ernerfeldt 2022-08-08 12:15:31 +02:00
parent 66c601f775
commit 9e41fa021a
6 changed files with 37 additions and 23 deletions

View file

@ -27,13 +27,18 @@ pub struct CreationContext<'s> {
/// The [`glow::Context`] allows you to initialize OpenGL resources (e.g. shaders) that /// The [`glow::Context`] allows you to initialize OpenGL resources (e.g. shaders) that
/// you might want to use later from a [`egui::PaintCallback`]. /// you might want to use later from a [`egui::PaintCallback`].
///
/// Only available when compiling with the `glow` feature and using [`Renderer::Glow`].
#[cfg(feature = "glow")] #[cfg(feature = "glow")]
pub gl: Option<std::sync::Arc<glow::Context>>, pub gl: Option<std::sync::Arc<glow::Context>>,
/// Can be used to manage GPU resources for custom rendering with WGPU using /// The underlying WGPU render state.
/// [`egui::PaintCallback`]s. ///
/// Only available when compiling with the `wgpu` feature and using [`Renderer::Wgpu`].
///
/// Can be used to manage GPU resources for custom rendering with WGPU using [`egui::PaintCallback`]s.
#[cfg(feature = "wgpu")] #[cfg(feature = "wgpu")]
pub render_state: Option<egui_wgpu::RenderState>, pub wgpu_render_state: Option<egui_wgpu::RenderState>,
} }
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@ -510,10 +515,9 @@ pub struct Frame {
#[cfg(feature = "glow")] #[cfg(feature = "glow")]
pub(crate) gl: Option<std::sync::Arc<glow::Context>>, pub(crate) gl: Option<std::sync::Arc<glow::Context>>,
/// Can be used to manage GPU resources for custom rendering with WGPU using /// Can be used to manage GPU resources for custom rendering with WGPU using [`egui::PaintCallback`]s.
/// [`egui::PaintCallback`]s.
#[cfg(feature = "wgpu")] #[cfg(feature = "wgpu")]
pub render_state: Option<egui_wgpu::RenderState>, pub(crate) wgpu_render_state: Option<egui_wgpu::RenderState>,
} }
impl Frame { impl Frame {
@ -551,12 +555,22 @@ impl Frame {
/// ([`egui`] only collects [`egui::Shape`]s and then eframe paints them all in one go later on). /// ([`egui`] only collects [`egui::Shape`]s and then eframe paints them all in one go later on).
/// ///
/// To get a [`glow`] context you need to compile with the `glow` feature flag, /// To get a [`glow`] context you need to compile with the `glow` feature flag,
/// and run eframe with the glow backend. /// and run eframe using [`Renderer::Glow`].
#[cfg(feature = "glow")] #[cfg(feature = "glow")]
pub fn gl(&self) -> Option<&std::sync::Arc<glow::Context>> { pub fn gl(&self) -> Option<&std::sync::Arc<glow::Context>> {
self.gl.as_ref() self.gl.as_ref()
} }
/// The underlying WGPU render state.
///
/// Only available when compiling with the `wgpu` feature and using [`Renderer::Wgpu`].
///
/// Can be used to manage GPU resources for custom rendering with WGPU using [`egui::PaintCallback`]s.
#[cfg(feature = "wgpu")]
pub fn wgpu_render_state(&self) -> Option<&egui_wgpu::RenderState> {
self.wgpu_render_state.as_ref()
}
/// Signal the app to stop/exit/quit the app (only works for native apps, not web apps). /// Signal the app to stop/exit/quit the app (only works for native apps, not web apps).
/// The framework will not quit immediately, but at the end of the this frame. /// The framework will not quit immediately, but at the end of the this frame.
#[cfg(not(target_arch = "wasm32"))] #[cfg(not(target_arch = "wasm32"))]

View file

@ -196,7 +196,7 @@ impl EpiIntegration {
system_theme: Option<Theme>, system_theme: Option<Theme>,
storage: Option<Box<dyn epi::Storage>>, storage: Option<Box<dyn epi::Storage>>,
#[cfg(feature = "glow")] gl: Option<std::sync::Arc<glow::Context>>, #[cfg(feature = "glow")] gl: Option<std::sync::Arc<glow::Context>>,
#[cfg(feature = "wgpu")] render_state: Option<egui_wgpu::RenderState>, #[cfg(feature = "wgpu")] wgpu_render_state: Option<egui_wgpu::RenderState>,
) -> Self { ) -> Self {
let egui_ctx = egui::Context::default(); let egui_ctx = egui::Context::default();
@ -214,7 +214,7 @@ impl EpiIntegration {
#[cfg(feature = "glow")] #[cfg(feature = "glow")]
gl, gl,
#[cfg(feature = "wgpu")] #[cfg(feature = "wgpu")]
render_state, wgpu_render_state,
}; };
let mut egui_winit = egui_winit::State::new(event_loop); let mut egui_winit = egui_winit::State::new(event_loop);

View file

@ -254,7 +254,7 @@ mod glow_integration {
storage: integration.frame.storage(), storage: integration.frame.storage(),
gl: Some(gl.clone()), gl: Some(gl.clone()),
#[cfg(feature = "wgpu")] #[cfg(feature = "wgpu")]
render_state: None, wgpu_render_state: None,
}); });
if app.warm_up_enabled() { if app.warm_up_enabled() {
@ -484,7 +484,7 @@ mod wgpu_integration {
painter painter
}; };
let render_state = painter.get_render_state().expect("Uninitialized"); let wgpu_render_state = painter.render_state().expect("Uninitialized");
let system_theme = native_options.system_theme(); let system_theme = native_options.system_theme();
let mut integration = epi_integration::EpiIntegration::new( let mut integration = epi_integration::EpiIntegration::new(
@ -495,7 +495,7 @@ mod wgpu_integration {
storage, storage,
#[cfg(feature = "glow")] #[cfg(feature = "glow")]
None, None,
Some(render_state.clone()), Some(wgpu_render_state.clone()),
); );
let theme = system_theme.unwrap_or(native_options.default_theme); let theme = system_theme.unwrap_or(native_options.default_theme);
integration.egui_ctx.set_visuals(theme.egui_visuals()); integration.egui_ctx.set_visuals(theme.egui_visuals());
@ -513,7 +513,7 @@ mod wgpu_integration {
storage: integration.frame.storage(), storage: integration.frame.storage(),
#[cfg(feature = "glow")] #[cfg(feature = "glow")]
gl: None, gl: None,
render_state: Some(render_state), wgpu_render_state: Some(wgpu_render_state),
}); });
if app.warm_up_enabled() { if app.warm_up_enabled() {

View file

@ -219,7 +219,7 @@ impl AppRunner {
#[cfg(feature = "glow")] #[cfg(feature = "glow")]
gl: Some(painter.painter.gl().clone()), gl: Some(painter.painter.gl().clone()),
#[cfg(feature = "wgpu")] #[cfg(feature = "wgpu")]
render_state: None, wgpu_render_state: None,
}); });
let frame = epi::Frame { let frame = epi::Frame {
@ -229,7 +229,7 @@ impl AppRunner {
#[cfg(feature = "glow")] #[cfg(feature = "glow")]
gl: Some(painter.gl().clone()), gl: Some(painter.gl().clone()),
#[cfg(feature = "wgpu")] #[cfg(feature = "wgpu")]
render_state: None, wgpu_render_state: None,
}; };
let needs_repaint: std::sync::Arc<NeedRepaint> = Default::default(); let needs_repaint: std::sync::Arc<NeedRepaint> = Default::default();

View file

@ -2,7 +2,7 @@ use std::sync::Arc;
use egui::mutex::RwLock; use egui::mutex::RwLock;
use tracing::error; use tracing::error;
use wgpu::{Adapter, Instance, Surface, TextureFormat}; use wgpu::{Adapter, Instance, Surface};
use crate::renderer; use crate::renderer;
@ -12,7 +12,7 @@ use crate::renderer;
pub struct RenderState { pub struct RenderState {
pub device: Arc<wgpu::Device>, pub device: Arc<wgpu::Device>,
pub queue: Arc<wgpu::Queue>, pub queue: Arc<wgpu::Queue>,
pub target_format: TextureFormat, pub target_format: wgpu::TextureFormat,
pub egui_rpass: Arc<RwLock<renderer::RenderPass>>, pub egui_rpass: Arc<RwLock<renderer::RenderPass>>,
} }
@ -75,14 +75,14 @@ impl<'a> Painter<'a> {
/// Get the [`RenderState`]. /// Get the [`RenderState`].
/// ///
/// Will return [`None`] if the render state has not been initialized yet. /// Will return [`None`] if the render state has not been initialized yet.
pub fn get_render_state(&self) -> Option<RenderState> { pub fn render_state(&self) -> Option<RenderState> {
self.render_state.as_ref().cloned() self.render_state.as_ref().cloned()
} }
async fn init_render_state( async fn init_render_state(
&self, &self,
adapter: &Adapter, adapter: &Adapter,
target_format: TextureFormat, target_format: wgpu::TextureFormat,
) -> RenderState { ) -> RenderState {
let (device, queue) = let (device, queue) =
pollster::block_on(adapter.request_device(&self.device_descriptor, None)).unwrap(); pollster::block_on(adapter.request_device(&self.device_descriptor, None)).unwrap();

View file

@ -13,9 +13,9 @@ impl Custom3d {
pub fn new<'a>(cc: &'a eframe::CreationContext<'a>) -> Self { pub fn new<'a>(cc: &'a eframe::CreationContext<'a>) -> Self {
// Get the WGPU render state from the eframe creation context. This can also be retrieved // Get the WGPU render state from the eframe creation context. This can also be retrieved
// from `eframe::Frame` when you don't have a `CreationContext` available. // from `eframe::Frame` when you don't have a `CreationContext` available.
let render_state = cc.render_state.as_ref().expect("WGPU enabled"); let wgpu_render_state = cc.wgpu_render_state.as_ref().expect("WGPU enabled");
let device = &render_state.device; let device = &wgpu_render_state.device;
let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor { let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
label: None, label: None,
@ -53,7 +53,7 @@ impl Custom3d {
fragment: Some(wgpu::FragmentState { fragment: Some(wgpu::FragmentState {
module: &shader, module: &shader,
entry_point: "fs_main", entry_point: "fs_main",
targets: &[Some(render_state.target_format.into())], targets: &[Some(wgpu_render_state.target_format.into())],
}), }),
primitive: wgpu::PrimitiveState::default(), primitive: wgpu::PrimitiveState::default(),
depth_stencil: None, depth_stencil: None,
@ -81,7 +81,7 @@ impl Custom3d {
// Because the graphics pipeline must have the same lifetime as the egui render pass, // Because the graphics pipeline must have the same lifetime as the egui render pass,
// instead of storing the pipeline in our `Custom3D` struct, we insert it into the // instead of storing the pipeline in our `Custom3D` struct, we insert it into the
// `paint_callback_resources` type map, which is stored alongside the render pass. // `paint_callback_resources` type map, which is stored alongside the render pass.
render_state wgpu_render_state
.egui_rpass .egui_rpass
.write() .write()
.paint_callback_resources .paint_callback_resources