From c1836f0175fb5106ca316ff6cf60990e0cee2ae7 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Thu, 20 Oct 2022 10:27:49 +0200 Subject: [PATCH 1/6] Inline method --- crates/fj-viewer/src/graphics/renderer.rs | 54 +++++++++++------------ 1 file changed, 25 insertions(+), 29 deletions(-) diff --git a/crates/fj-viewer/src/graphics/renderer.rs b/crates/fj-viewer/src/graphics/renderer.rs index 9b2952add..ab7e48145 100644 --- a/crates/fj-viewer/src/graphics/renderer.rs +++ b/crates/fj-viewer/src/graphics/renderer.rs @@ -223,7 +223,31 @@ impl Renderer { &wgpu::CommandEncoderDescriptor { label: None }, ); - self.clear_views(&mut encoder, &color_view); + // Need this block here, as a render pass only takes effect once it's + // dropped. + { + encoder.begin_render_pass(&wgpu::RenderPassDescriptor { + label: None, + color_attachments: &[Some(wgpu::RenderPassColorAttachment { + view: &color_view, + resolve_target: None, + ops: wgpu::Operations { + load: wgpu::LoadOp::Clear(wgpu::Color::WHITE), + store: true, + }, + })], + depth_stencil_attachment: Some( + wgpu::RenderPassDepthStencilAttachment { + view: &self.depth_view, + depth_ops: Some(wgpu::Operations { + load: wgpu::LoadOp::Clear(1.0), + store: true, + }), + stencil_ops: None, + }, + ), + }); + }; let drawables = Drawables::new(&self.geometries, &self.pipelines); @@ -300,34 +324,6 @@ impl Renderer { texture.create_view(&wgpu::TextureViewDescriptor::default()) } - fn clear_views( - &self, - encoder: &mut wgpu::CommandEncoder, - view: &wgpu::TextureView, - ) { - encoder.begin_render_pass(&wgpu::RenderPassDescriptor { - label: None, - color_attachments: &[Some(wgpu::RenderPassColorAttachment { - view, - resolve_target: None, - ops: wgpu::Operations { - load: wgpu::LoadOp::Clear(wgpu::Color::WHITE), - store: true, - }, - })], - depth_stencil_attachment: Some( - wgpu::RenderPassDepthStencilAttachment { - view: &self.depth_view, - depth_ops: Some(wgpu::Operations { - load: wgpu::LoadOp::Clear(1.0), - store: true, - }), - stencil_ops: None, - }, - ), - }); - } - /// Returns true if the renderer's adapter can draw lines pub fn is_line_drawing_available(&self) -> bool { self.features.contains(wgpu::Features::POLYGON_MODE_LINE) From ab149fbe241d6a7d90a7567998c14335336be533 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Thu, 20 Oct 2022 10:32:11 +0200 Subject: [PATCH 2/6] Rename lifetime --- crates/fj-viewer/src/graphics/drawables.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/crates/fj-viewer/src/graphics/drawables.rs b/crates/fj-viewer/src/graphics/drawables.rs index dba51ccb3..5b1dfda14 100644 --- a/crates/fj-viewer/src/graphics/drawables.rs +++ b/crates/fj-viewer/src/graphics/drawables.rs @@ -19,13 +19,13 @@ impl<'r> Drawables<'r> { } } -pub struct Drawable<'r> { - pub geometry: &'r Geometry, - pub pipeline: &'r Pipeline, +pub struct Drawable<'a> { + pub geometry: &'a Geometry, + pub pipeline: &'a Pipeline, } -impl<'r> Drawable<'r> { - fn new(geometry: &'r Geometry, pipeline: &'r Pipeline) -> Self { +impl<'a> Drawable<'a> { + fn new(geometry: &'a Geometry, pipeline: &'a Pipeline) -> Self { Self { geometry, pipeline } } From b6d2b9c3050173ff0279e37f90597f852ba3d07b Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Thu, 20 Oct 2022 10:38:37 +0200 Subject: [PATCH 3/6] Share one render pass between all `Drawable`s --- crates/fj-viewer/src/graphics/drawables.rs | 35 ++------- crates/fj-viewer/src/graphics/renderer.rs | 88 ++++++++++------------ 2 files changed, 44 insertions(+), 79 deletions(-) diff --git a/crates/fj-viewer/src/graphics/drawables.rs b/crates/fj-viewer/src/graphics/drawables.rs index 5b1dfda14..5e7da4c3a 100644 --- a/crates/fj-viewer/src/graphics/drawables.rs +++ b/crates/fj-viewer/src/graphics/drawables.rs @@ -29,36 +29,13 @@ impl<'a> Drawable<'a> { Self { geometry, pipeline } } - pub fn draw( + pub fn draw<'b>( &self, - encoder: &mut wgpu::CommandEncoder, - color_view: &wgpu::TextureView, - depth_view: &wgpu::TextureView, - bind_group: &wgpu::BindGroup, - ) { - let mut render_pass = - encoder.begin_render_pass(&wgpu::RenderPassDescriptor { - label: None, - color_attachments: &[Some(wgpu::RenderPassColorAttachment { - view: color_view, - resolve_target: None, - ops: wgpu::Operations { - load: wgpu::LoadOp::Load, - store: true, - }, - })], - depth_stencil_attachment: Some( - wgpu::RenderPassDepthStencilAttachment { - view: depth_view, - depth_ops: Some(wgpu::Operations { - load: wgpu::LoadOp::Load, - store: true, - }), - stencil_ops: None, - }, - ), - }); - + bind_group: &'b wgpu::BindGroup, + render_pass: &mut wgpu::RenderPass<'b>, + ) where + 'a: 'b, + { render_pass.set_pipeline(&self.pipeline.0); render_pass.set_bind_group(0, bind_group, &[]); render_pass.set_vertex_buffer(0, self.geometry.vertex_buffer.slice(..)); diff --git a/crates/fj-viewer/src/graphics/renderer.rs b/crates/fj-viewer/src/graphics/renderer.rs index ab7e48145..834d1dd03 100644 --- a/crates/fj-viewer/src/graphics/renderer.rs +++ b/crates/fj-viewer/src/graphics/renderer.rs @@ -226,58 +226,46 @@ impl Renderer { // Need this block here, as a render pass only takes effect once it's // dropped. { - encoder.begin_render_pass(&wgpu::RenderPassDescriptor { - label: None, - color_attachments: &[Some(wgpu::RenderPassColorAttachment { - view: &color_view, - resolve_target: None, - ops: wgpu::Operations { - load: wgpu::LoadOp::Clear(wgpu::Color::WHITE), - store: true, - }, - })], - depth_stencil_attachment: Some( - wgpu::RenderPassDepthStencilAttachment { - view: &self.depth_view, - depth_ops: Some(wgpu::Operations { - load: wgpu::LoadOp::Clear(1.0), - store: true, - }), - stencil_ops: None, - }, - ), - }); - }; - - let drawables = Drawables::new(&self.geometries, &self.pipelines); - - if config.draw_model { - drawables.model.draw( - &mut encoder, - &color_view, - &self.depth_view, - &self.bind_group, - ); - } - - if self.is_line_drawing_available() { - if config.draw_mesh { - drawables.mesh.draw( - &mut encoder, - &color_view, - &self.depth_view, - &self.bind_group, - ); + let mut render_pass = + encoder.begin_render_pass(&wgpu::RenderPassDescriptor { + label: None, + color_attachments: &[Some( + wgpu::RenderPassColorAttachment { + view: &color_view, + resolve_target: None, + ops: wgpu::Operations { + load: wgpu::LoadOp::Clear(wgpu::Color::WHITE), + store: true, + }, + }, + )], + depth_stencil_attachment: Some( + wgpu::RenderPassDepthStencilAttachment { + view: &self.depth_view, + depth_ops: Some(wgpu::Operations { + load: wgpu::LoadOp::Clear(1.0), + store: true, + }), + stencil_ops: None, + }, + ), + }); + + let drawables = Drawables::new(&self.geometries, &self.pipelines); + + if config.draw_model { + drawables.model.draw(&self.bind_group, &mut render_pass); } - if config.draw_debug { - drawables.lines.draw( - &mut encoder, - &color_view, - &self.depth_view, - &self.bind_group, - ); + + if self.is_line_drawing_available() { + if config.draw_mesh { + drawables.mesh.draw(&self.bind_group, &mut render_pass); + } + if config.draw_debug { + drawables.lines.draw(&self.bind_group, &mut render_pass); + } } - } + }; gui.draw( &self.device, From dd897dd4b5514b8e674a0e4ac2c195eb3aa89bce Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Thu, 20 Oct 2022 10:40:30 +0200 Subject: [PATCH 4/6] Refactor --- crates/fj-viewer/src/graphics/drawables.rs | 8 ++------ crates/fj-viewer/src/graphics/renderer.rs | 7 ++++--- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/crates/fj-viewer/src/graphics/drawables.rs b/crates/fj-viewer/src/graphics/drawables.rs index 5e7da4c3a..b553dea62 100644 --- a/crates/fj-viewer/src/graphics/drawables.rs +++ b/crates/fj-viewer/src/graphics/drawables.rs @@ -29,15 +29,11 @@ impl<'a> Drawable<'a> { Self { geometry, pipeline } } - pub fn draw<'b>( - &self, - bind_group: &'b wgpu::BindGroup, - render_pass: &mut wgpu::RenderPass<'b>, - ) where + pub fn draw<'b>(&self, render_pass: &mut wgpu::RenderPass<'b>) + where 'a: 'b, { render_pass.set_pipeline(&self.pipeline.0); - render_pass.set_bind_group(0, bind_group, &[]); render_pass.set_vertex_buffer(0, self.geometry.vertex_buffer.slice(..)); render_pass.set_index_buffer( self.geometry.index_buffer.slice(..), diff --git a/crates/fj-viewer/src/graphics/renderer.rs b/crates/fj-viewer/src/graphics/renderer.rs index 834d1dd03..8cf46d9cb 100644 --- a/crates/fj-viewer/src/graphics/renderer.rs +++ b/crates/fj-viewer/src/graphics/renderer.rs @@ -250,19 +250,20 @@ impl Renderer { }, ), }); + render_pass.set_bind_group(0, &self.bind_group, &[]); let drawables = Drawables::new(&self.geometries, &self.pipelines); if config.draw_model { - drawables.model.draw(&self.bind_group, &mut render_pass); + drawables.model.draw(&mut render_pass); } if self.is_line_drawing_available() { if config.draw_mesh { - drawables.mesh.draw(&self.bind_group, &mut render_pass); + drawables.mesh.draw(&mut render_pass); } if config.draw_debug { - drawables.lines.draw(&self.bind_group, &mut render_pass); + drawables.lines.draw(&mut render_pass); } } }; From d4f606d24f7315304a0b4c7e4c33383f0695d55b Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Thu, 20 Oct 2022 10:47:26 +0200 Subject: [PATCH 5/6] Refactor --- crates/fj-viewer/src/gui.rs | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/crates/fj-viewer/src/gui.rs b/crates/fj-viewer/src/gui.rs index 266099a85..08e96f9cc 100644 --- a/crates/fj-viewer/src/gui.rs +++ b/crates/fj-viewer/src/gui.rs @@ -15,7 +15,7 @@ //! use fj_interop::status_report::StatusReport; -use fj_math::Aabb; +use fj_math::{Aabb, Scalar}; use crate::graphics::DrawConfig; @@ -87,17 +87,10 @@ impl Gui { ) { self.context.begin_frame(egui_input); - fn get_bbox_size_text(aabb: &Aabb<3>) -> String { - /* Render size of model bounding box */ - let bbsize = aabb.size().components; - let info = format!( - "Model bounding box size:\n{:0.1} {:0.1} {:0.1}", - bbsize[0].into_f32(), - bbsize[1].into_f32(), - bbsize[2].into_f32() - ); - info - } + let bounding_box_size = { + let [x, y, z] = aabb.size().components.map(Scalar::into_f32); + format!("Model bounding box size:\n{x:0.1} {y:0.1} {z:0.1}") + }; egui::SidePanel::left("fj-left-panel").show(&self.context, |ui| { ui.add_space(16.0); @@ -116,7 +109,7 @@ impl Gui { "Rendering device does not have line rendering feature support" ); ui.add_space(16.0); - ui.strong(get_bbox_size_text(aabb)); + ui.strong(bounding_box_size); }); ui.add_space(16.0); From 08b9a734b134355514b881de0fcfc869788d58d7 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Thu, 20 Oct 2022 10:55:52 +0200 Subject: [PATCH 6/6] Update `Debug` implementation --- crates/fj-viewer/src/gui.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/fj-viewer/src/gui.rs b/crates/fj-viewer/src/gui.rs index 08e96f9cc..549f897b1 100644 --- a/crates/fj-viewer/src/gui.rs +++ b/crates/fj-viewer/src/gui.rs @@ -279,7 +279,7 @@ impl Gui { impl std::fmt::Debug for Gui { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.write_str("EguiState {}") + f.write_str("Gui {}") } }