egui-wgpu renderer renaming (#2021)

- `RenderPass` -> `Renderer`
- `RenderPass::execute` -> `Renderer::render`
- `RenderPass::execute_with_renderpass` -> `Renderer::render_onto_renderpass`
- reexport `Renderer` in `lib.rs`
This commit is contained in:
Luis Wirth 2022-09-07 14:20:21 +02:00 committed by GitHub
parent 0e62c0e50b
commit 4b6826575e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 62 additions and 56 deletions

View file

@ -3,6 +3,10 @@ All notable changes to the `egui-wgpu` integration will be noted in this file.
## Unreleased ## Unreleased
* Rename `RenderPass` to `Renderer`
* Rename `RenderPass::execute` to `RenderPass::render`
* Rename `RenderPass::execute_with_renderpass` to `Renderer::render_onto_renderpass`
* Reexport `Renderer`
## 0.19.0 - 2022-08-20 ## 0.19.0 - 2022-08-20

View file

@ -11,6 +11,7 @@ pub use wgpu;
/// Low-level painting of [`egui`] on [`wgpu`]. /// Low-level painting of [`egui`] on [`wgpu`].
pub mod renderer; pub mod renderer;
pub use renderer::CallbackFn; pub use renderer::CallbackFn;
pub use renderer::Renderer;
/// Module for painting [`egui`] with [`wgpu`] on [`winit`]. /// Module for painting [`egui`] with [`wgpu`] on [`winit`].
#[cfg(feature = "winit")] #[cfg(feature = "winit")]

View file

@ -19,7 +19,7 @@ use wgpu::util::DeviceExt as _;
/// can issue draw commands. /// can issue draw commands.
/// ///
/// The final argument of both the `prepare` and `paint` callbacks is a the /// The final argument of both the `prepare` and `paint` callbacks is a the
/// [`paint_callback_resources`][crate::renderer::RenderPass::paint_callback_resources]. /// [`paint_callback_resources`][crate::renderer::Renderer::paint_callback_resources].
/// `paint_callback_resources` has the same lifetime as the Egui render pass, so it can be used to /// `paint_callback_resources` has the same lifetime as the Egui render pass, so it can be used to
/// store buffers, pipelines, and other information that needs to be accessed during the render /// store buffers, pipelines, and other information that needs to be accessed during the render
/// pass. /// pass.
@ -118,9 +118,9 @@ struct SizedBuffer {
size: usize, size: usize,
} }
/// Render pass to render a egui based GUI. /// Renderer for a egui based GUI.
pub struct RenderPass { pub struct Renderer {
render_pipeline: wgpu::RenderPipeline, pipeline: wgpu::RenderPipeline,
depth_texture: Option<(wgpu::Texture, wgpu::TextureView)>, depth_texture: Option<(wgpu::Texture, wgpu::TextureView)>,
index_buffers: Vec<SizedBuffer>, index_buffers: Vec<SizedBuffer>,
vertex_buffers: Vec<SizedBuffer>, vertex_buffers: Vec<SizedBuffer>,
@ -137,8 +137,8 @@ pub struct RenderPass {
pub paint_callback_resources: TypeMap, pub paint_callback_resources: TypeMap,
} }
impl RenderPass { impl Renderer {
/// Creates a new render pass to render a egui UI. /// Creates a renderer for a egui UI.
/// ///
/// If the format passed is not a *Srgb format, the shader will automatically convert to `sRGB` colors in the shader. /// If the format passed is not a *Srgb format, the shader will automatically convert to `sRGB` colors in the shader.
pub fn new( pub fn new(
@ -231,7 +231,7 @@ impl RenderPass {
bias: wgpu::DepthBiasState::default(), bias: wgpu::DepthBiasState::default(),
}); });
let render_pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor { let pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
label: Some("egui_pipeline"), label: Some("egui_pipeline"),
layout: Some(&pipeline_layout), layout: Some(&pipeline_layout),
vertex: wgpu::VertexState { vertex: wgpu::VertexState {
@ -290,7 +290,7 @@ impl RenderPass {
}); });
Self { Self {
render_pipeline, pipeline,
vertex_buffers: Vec::with_capacity(64), vertex_buffers: Vec::with_capacity(64),
index_buffers: Vec::with_capacity(64), index_buffers: Vec::with_capacity(64),
uniform_buffer, uniform_buffer,
@ -323,8 +323,8 @@ impl RenderPass {
self.depth_texture = Some((texture, view)); self.depth_texture = Some((texture, view));
} }
/// Executes the egui render pass. /// Executes the renderer on its own render pass.
pub fn execute( pub fn render(
&self, &self,
encoder: &mut wgpu::CommandEncoder, encoder: &mut wgpu::CommandEncoder,
color_attachment: &wgpu::TextureView, color_attachment: &wgpu::TextureView,
@ -349,7 +349,7 @@ impl RenderPass {
} }
}); });
let mut rpass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor { let mut render_pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
color_attachments: &[Some(wgpu::RenderPassColorAttachment { color_attachments: &[Some(wgpu::RenderPassColorAttachment {
view: color_attachment, view: color_attachment,
resolve_target: None, resolve_target: None,
@ -359,26 +359,23 @@ impl RenderPass {
}, },
})], })],
depth_stencil_attachment, depth_stencil_attachment,
label: Some("egui main render pass"), label: Some("egui_render_pass"),
}); });
rpass.push_debug_group("egui_pass");
self.execute_with_renderpass(&mut rpass, paint_jobs, screen_descriptor); self.render_onto_renderpass(&mut render_pass, paint_jobs, screen_descriptor);
rpass.pop_debug_group();
} }
/// Executes the egui render pass onto an existing wgpu renderpass. /// Executes the egui renderer onto an existing wgpu renderpass.
pub fn execute_with_renderpass<'rpass>( pub fn render_onto_renderpass<'rp>(
&'rpass self, &'rp self,
rpass: &mut wgpu::RenderPass<'rpass>, render_pass: &mut wgpu::RenderPass<'rp>,
paint_jobs: &[egui::epaint::ClippedPrimitive], paint_jobs: &[egui::epaint::ClippedPrimitive],
screen_descriptor: &ScreenDescriptor, screen_descriptor: &ScreenDescriptor,
) { ) {
let pixels_per_point = screen_descriptor.pixels_per_point; let pixels_per_point = screen_descriptor.pixels_per_point;
let size_in_pixels = screen_descriptor.size_in_pixels; let size_in_pixels = screen_descriptor.size_in_pixels;
// Whether or not we need to reset the renderpass state because a paint callback has just // Whether or not we need to reset the render pass because a paint callback has just
// run. // run.
let mut needs_reset = true; let mut needs_reset = true;
@ -391,7 +388,7 @@ impl RenderPass {
} in paint_jobs } in paint_jobs
{ {
if needs_reset { if needs_reset {
rpass.set_viewport( render_pass.set_viewport(
0.0, 0.0,
0.0, 0.0,
size_in_pixels[0] as f32, size_in_pixels[0] as f32,
@ -399,8 +396,8 @@ impl RenderPass {
0.0, 0.0,
1.0, 1.0,
); );
rpass.set_pipeline(&self.render_pipeline); render_pass.set_pipeline(&self.pipeline);
rpass.set_bind_group(0, &self.uniform_bind_group, &[]); render_pass.set_bind_group(0, &self.uniform_bind_group, &[]);
needs_reset = false; needs_reset = false;
} }
@ -417,7 +414,7 @@ impl RenderPass {
continue; continue;
} }
rpass.set_scissor_rect(rect.x, rect.y, rect.width, rect.height); render_pass.set_scissor_rect(rect.x, rect.y, rect.width, rect.height);
} }
match primitive { match primitive {
@ -426,13 +423,13 @@ impl RenderPass {
let vertex_buffer = vertex_buffers.next().unwrap(); let vertex_buffer = vertex_buffers.next().unwrap();
if let Some((_texture, bind_group)) = self.textures.get(&mesh.texture_id) { if let Some((_texture, bind_group)) = self.textures.get(&mesh.texture_id) {
rpass.set_bind_group(1, bind_group, &[]); render_pass.set_bind_group(1, bind_group, &[]);
rpass.set_index_buffer( render_pass.set_index_buffer(
index_buffer.buffer.slice(..), index_buffer.buffer.slice(..),
wgpu::IndexFormat::Uint32, wgpu::IndexFormat::Uint32,
); );
rpass.set_vertex_buffer(0, vertex_buffer.buffer.slice(..)); render_pass.set_vertex_buffer(0, vertex_buffer.buffer.slice(..));
rpass.draw_indexed(0..mesh.indices.len() as u32, 0, 0..1); render_pass.draw_indexed(0..mesh.indices.len() as u32, 0, 0..1);
} else { } else {
tracing::warn!("Missing texture: {:?}", mesh.texture_id); tracing::warn!("Missing texture: {:?}", mesh.texture_id);
} }
@ -461,7 +458,7 @@ impl RenderPass {
let rect_max_x = rect_max_x.round(); let rect_max_x = rect_max_x.round();
let rect_max_y = rect_max_y.round(); let rect_max_y = rect_max_y.round();
rpass.set_viewport( render_pass.set_viewport(
rect_min_x, rect_min_x,
rect_min_y, rect_min_y,
rect_max_x - rect_min_x, rect_max_x - rect_min_x,
@ -478,7 +475,7 @@ impl RenderPass {
pixels_per_point, pixels_per_point,
screen_size_px: size_in_pixels, screen_size_px: size_in_pixels,
}, },
rpass, render_pass,
&self.paint_callback_resources, &self.paint_callback_resources,
); );
} }
@ -486,10 +483,10 @@ impl RenderPass {
} }
} }
rpass.set_scissor_rect(0, 0, size_in_pixels[0], size_in_pixels[1]); render_pass.set_scissor_rect(0, 0, size_in_pixels[0], size_in_pixels[1]);
} }
/// Should be called before `execute()`. /// Should be called before `render()`.
pub fn update_texture( pub fn update_texture(
&mut self, &mut self,
device: &wgpu::Device, device: &wgpu::Device,
@ -768,8 +765,8 @@ impl RenderPass {
*user_texture_binding = bind_group; *user_texture_binding = bind_group;
} }
/// Uploads the uniform, vertex and index data used by the render pass. /// Uploads the uniform, vertex and index data used by the renderer.
/// Should be called before `execute()`. /// Should be called before `render()`.
pub fn update_buffers( pub fn update_buffers(
&mut self, &mut self,
device: &wgpu::Device, device: &wgpu::Device,
@ -922,7 +919,7 @@ impl ScissorRect {
} }
#[test] #[test]
fn render_pass_impl_send_sync() { fn renderer_impl_send_sync() {
fn assert_send_sync<T: Send + Sync>() {} fn assert_send_sync<T: Send + Sync>() {}
assert_send_sync::<RenderPass>(); assert_send_sync::<Renderer>();
} }

View file

@ -4,7 +4,7 @@ use egui::mutex::RwLock;
use tracing::error; use tracing::error;
use wgpu::{Adapter, Instance, Surface}; use wgpu::{Adapter, Instance, Surface};
use crate::renderer; use crate::{renderer, Renderer};
/// Access to the render state for egui, which can be useful in combination with /// Access to the render state for egui, which can be useful in combination with
/// [`egui::PaintCallback`]s for custom rendering using WGPU. /// [`egui::PaintCallback`]s for custom rendering using WGPU.
@ -13,7 +13,7 @@ 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: wgpu::TextureFormat, pub target_format: wgpu::TextureFormat,
pub egui_rpass: Arc<RwLock<renderer::RenderPass>>, pub renderer: Arc<RwLock<Renderer>>,
} }
struct SurfaceState { struct SurfaceState {
@ -90,14 +90,13 @@ impl<'a> Painter<'a> {
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();
let rpass = let renderer = Renderer::new(&device, target_format, self.msaa_samples, self.depth_bits);
renderer::RenderPass::new(&device, target_format, self.msaa_samples, self.depth_bits);
RenderState { RenderState {
device: Arc::new(device), device: Arc::new(device),
queue: Arc::new(queue), queue: Arc::new(queue),
target_format, target_format,
egui_rpass: Arc::new(RwLock::new(rpass)), renderer: Arc::new(RwLock::new(renderer)),
} }
} }
@ -151,7 +150,7 @@ impl<'a> Painter<'a> {
surface_state.height = height_in_pixels; surface_state.height = height_in_pixels;
if self.depth_bits > 0 { if self.depth_bits > 0 {
render_state.egui_rpass.write().update_depth_texture( render_state.renderer.write().update_depth_texture(
&render_state.device, &render_state.device,
width_in_pixels, width_in_pixels,
height_in_pixels, height_in_pixels,
@ -272,12 +271,17 @@ impl<'a> Painter<'a> {
}; };
{ {
let mut rpass = render_state.egui_rpass.write(); let mut renderer = render_state.renderer.write();
for (id, image_delta) in &textures_delta.set { for (id, image_delta) in &textures_delta.set {
rpass.update_texture(&render_state.device, &render_state.queue, *id, image_delta); renderer.update_texture(
&render_state.device,
&render_state.queue,
*id,
image_delta,
);
} }
rpass.update_buffers( renderer.update_buffers(
&render_state.device, &render_state.device,
&render_state.queue, &render_state.queue,
clipped_primitives, clipped_primitives,
@ -286,7 +290,7 @@ impl<'a> Painter<'a> {
} }
// Record all render passes. // Record all render passes.
render_state.egui_rpass.read().execute( render_state.renderer.read().render(
&mut encoder, &mut encoder,
&output_view, &output_view,
clipped_primitives, clipped_primitives,
@ -300,9 +304,9 @@ impl<'a> Painter<'a> {
); );
{ {
let mut rpass = render_state.egui_rpass.write(); let mut renderer = render_state.renderer.write();
for id in &textures_delta.free { for id in &textures_delta.free {
rpass.free_texture(id); renderer.free_texture(id);
} }
} }

View file

@ -82,7 +82,7 @@ impl Custom3d {
// 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.
wgpu_render_state wgpu_render_state
.egui_rpass .renderer
.write() .write()
.paint_callback_resources .paint_callback_resources
.insert(TriangleRenderResources { .insert(TriangleRenderResources {
@ -142,9 +142,9 @@ impl Custom3d {
let resources: &TriangleRenderResources = paint_callback_resources.get().unwrap(); let resources: &TriangleRenderResources = paint_callback_resources.get().unwrap();
resources.prepare(device, queue, angle); resources.prepare(device, queue, angle);
}) })
.paint(move |_info, rpass, paint_callback_resources| { .paint(move |_info, render_pass, paint_callback_resources| {
let resources: &TriangleRenderResources = paint_callback_resources.get().unwrap(); let resources: &TriangleRenderResources = paint_callback_resources.get().unwrap();
resources.paint(rpass); resources.paint(render_pass);
}); });
let callback = egui::PaintCallback { let callback = egui::PaintCallback {
@ -168,10 +168,10 @@ impl TriangleRenderResources {
queue.write_buffer(&self.uniform_buffer, 0, bytemuck::cast_slice(&[angle])); queue.write_buffer(&self.uniform_buffer, 0, bytemuck::cast_slice(&[angle]));
} }
fn paint<'rpass>(&'rpass self, rpass: &mut wgpu::RenderPass<'rpass>) { fn paint<'rp>(&'rp self, render_pass: &mut wgpu::RenderPass<'rp>) {
// Draw our triangle! // Draw our triangle!
rpass.set_pipeline(&self.pipeline); render_pass.set_pipeline(&self.pipeline);
rpass.set_bind_group(0, &self.bind_group, &[]); render_pass.set_bind_group(0, &self.bind_group, &[]);
rpass.draw(0..3, 0..1); render_pass.draw(0..3, 0..1);
} }
} }