From 60bab6f42757a701e528c248658c5aa4c288cd51 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Thu, 7 Sep 2023 18:02:38 +0200 Subject: [PATCH 1/6] Refactor to prepare for follow-on change --- crates/fj-viewer/src/graphics/drawables.rs | 4 ++-- crates/fj-viewer/src/graphics/renderer.rs | 6 ++++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/crates/fj-viewer/src/graphics/drawables.rs b/crates/fj-viewer/src/graphics/drawables.rs index 4c46f955d..9598a785b 100644 --- a/crates/fj-viewer/src/graphics/drawables.rs +++ b/crates/fj-viewer/src/graphics/drawables.rs @@ -5,13 +5,13 @@ use super::{ pub struct Drawables<'r> { pub model: Drawable<'r>, - pub mesh: Drawable<'r>, + pub mesh: Option>, } impl<'r> Drawables<'r> { pub fn new(geometries: &'r Geometries, pipelines: &'r Pipelines) -> Self { let model = Drawable::new(&geometries.mesh, &pipelines.model); - let mesh = Drawable::new(&geometries.mesh, &pipelines.mesh); + let mesh = Some(Drawable::new(&geometries.mesh, &pipelines.mesh)); Self { model, mesh } } diff --git a/crates/fj-viewer/src/graphics/renderer.rs b/crates/fj-viewer/src/graphics/renderer.rs index 02b68257e..2802a489c 100644 --- a/crates/fj-viewer/src/graphics/renderer.rs +++ b/crates/fj-viewer/src/graphics/renderer.rs @@ -311,8 +311,10 @@ impl Renderer { drawables.model.draw(&mut render_pass); } - if self.is_line_drawing_available() && config.draw_mesh { - drawables.mesh.draw(&mut render_pass); + if let Some(drawable) = drawables.mesh { + if self.is_line_drawing_available() && config.draw_mesh { + drawable.draw(&mut render_pass); + } } } From 74ab305b0446efb0bbede1250b894311d60e8e1c Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Thu, 7 Sep 2023 18:04:01 +0200 Subject: [PATCH 2/6] Refactor to prepare for follow-on change --- crates/fj-viewer/src/graphics/drawables.rs | 5 ++++- crates/fj-viewer/src/graphics/pipelines.rs | 6 +++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/crates/fj-viewer/src/graphics/drawables.rs b/crates/fj-viewer/src/graphics/drawables.rs index 9598a785b..40a0eb43f 100644 --- a/crates/fj-viewer/src/graphics/drawables.rs +++ b/crates/fj-viewer/src/graphics/drawables.rs @@ -11,7 +11,10 @@ pub struct Drawables<'r> { impl<'r> Drawables<'r> { pub fn new(geometries: &'r Geometries, pipelines: &'r Pipelines) -> Self { let model = Drawable::new(&geometries.mesh, &pipelines.model); - let mesh = Some(Drawable::new(&geometries.mesh, &pipelines.mesh)); + let mesh = pipelines + .mesh + .as_ref() + .map(|pipeline| Drawable::new(&geometries.mesh, pipeline)); Self { model, mesh } } diff --git a/crates/fj-viewer/src/graphics/pipelines.rs b/crates/fj-viewer/src/graphics/pipelines.rs index fb64e3cb8..82878c675 100644 --- a/crates/fj-viewer/src/graphics/pipelines.rs +++ b/crates/fj-viewer/src/graphics/pipelines.rs @@ -9,7 +9,7 @@ use super::{ #[derive(Debug)] pub struct Pipelines { pub model: Pipeline, - pub mesh: Pipeline, + pub mesh: Option, pub lines: Pipeline, } @@ -37,14 +37,14 @@ impl Pipelines { wgpu::PolygonMode::Fill, color_format, ), - mesh: Pipeline::new( + mesh: Some(Pipeline::new( device, &pipeline_layout, shaders.mesh(), wgpu::PrimitiveTopology::TriangleList, wgpu::PolygonMode::Line, color_format, - ), + )), lines: Pipeline::new( device, &pipeline_layout, From e15608f4908bdbcddd2e5f3bab27e367d30bb959 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Thu, 7 Sep 2023 18:04:58 +0200 Subject: [PATCH 3/6] Remove unused code --- crates/fj-viewer/src/graphics/pipelines.rs | 9 --------- crates/fj-viewer/src/graphics/shader.wgsl | 7 ------- crates/fj-viewer/src/graphics/shaders.rs | 7 ------- 3 files changed, 23 deletions(-) diff --git a/crates/fj-viewer/src/graphics/pipelines.rs b/crates/fj-viewer/src/graphics/pipelines.rs index 82878c675..bcabcf13a 100644 --- a/crates/fj-viewer/src/graphics/pipelines.rs +++ b/crates/fj-viewer/src/graphics/pipelines.rs @@ -10,7 +10,6 @@ use super::{ pub struct Pipelines { pub model: Pipeline, pub mesh: Option, - pub lines: Pipeline, } impl Pipelines { @@ -45,14 +44,6 @@ impl Pipelines { wgpu::PolygonMode::Line, color_format, )), - lines: Pipeline::new( - device, - &pipeline_layout, - shaders.lines(), - wgpu::PrimitiveTopology::LineList, - wgpu::PolygonMode::Line, - color_format, - ), } } } diff --git a/crates/fj-viewer/src/graphics/shader.wgsl b/crates/fj-viewer/src/graphics/shader.wgsl index 7a03144b1..d171ff502 100644 --- a/crates/fj-viewer/src/graphics/shader.wgsl +++ b/crates/fj-viewer/src/graphics/shader.wgsl @@ -56,10 +56,3 @@ fn frag_mesh(in: VertexOutput) -> FragmentOutput { out.color = vec4(1.0 - in.color.rgb, in.color.a); return out; } - -@fragment -fn frag_lines(in: VertexOutput) -> FragmentOutput { - var out: FragmentOutput; - out.color = vec4(in.color.rgb, in.color.a); - return out; -} diff --git a/crates/fj-viewer/src/graphics/shaders.rs b/crates/fj-viewer/src/graphics/shaders.rs index dc4aab79e..b228edfd2 100644 --- a/crates/fj-viewer/src/graphics/shaders.rs +++ b/crates/fj-viewer/src/graphics/shaders.rs @@ -28,13 +28,6 @@ impl Shaders { frag_entry: "frag_mesh", } } - - pub fn lines(&self) -> Shader { - Shader { - module: &self.0, - frag_entry: "frag_lines", - } - } } #[derive(Clone, Copy)] From 97201b74ba24bd9dfa7224c50c34bff6d2ec9c4c Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Thu, 7 Sep 2023 18:05:53 +0200 Subject: [PATCH 4/6] Refactor to prepare for follow-on change --- crates/fj-viewer/src/graphics/pipelines.rs | 37 +++++++++++----------- 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/crates/fj-viewer/src/graphics/pipelines.rs b/crates/fj-viewer/src/graphics/pipelines.rs index bcabcf13a..b1751e063 100644 --- a/crates/fj-viewer/src/graphics/pipelines.rs +++ b/crates/fj-viewer/src/graphics/pipelines.rs @@ -27,24 +27,25 @@ impl Pipelines { let shaders = Shaders::new(device); - Self { - model: Pipeline::new( - device, - &pipeline_layout, - shaders.model(), - wgpu::PrimitiveTopology::TriangleList, - wgpu::PolygonMode::Fill, - color_format, - ), - mesh: Some(Pipeline::new( - device, - &pipeline_layout, - shaders.mesh(), - wgpu::PrimitiveTopology::TriangleList, - wgpu::PolygonMode::Line, - color_format, - )), - } + let model = Pipeline::new( + device, + &pipeline_layout, + shaders.model(), + wgpu::PrimitiveTopology::TriangleList, + wgpu::PolygonMode::Fill, + color_format, + ); + + let mesh = Some(Pipeline::new( + device, + &pipeline_layout, + shaders.mesh(), + wgpu::PrimitiveTopology::TriangleList, + wgpu::PolygonMode::Line, + color_format, + )); + + Self { model, mesh } } } From cdbb6bba394e438125167e2f0a0aa6495af269c7 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Thu, 7 Sep 2023 18:08:41 +0200 Subject: [PATCH 5/6] Fix initialization panic with GL backend --- crates/fj-viewer/src/graphics/pipelines.rs | 24 ++++++++++++++-------- crates/fj-viewer/src/graphics/renderer.rs | 2 +- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/crates/fj-viewer/src/graphics/pipelines.rs b/crates/fj-viewer/src/graphics/pipelines.rs index b1751e063..4fb75dfa4 100644 --- a/crates/fj-viewer/src/graphics/pipelines.rs +++ b/crates/fj-viewer/src/graphics/pipelines.rs @@ -17,6 +17,7 @@ impl Pipelines { device: &wgpu::Device, bind_group_layout: &wgpu::BindGroupLayout, color_format: wgpu::TextureFormat, + features: wgpu::Features, ) -> Self { let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { @@ -36,14 +37,21 @@ impl Pipelines { color_format, ); - let mesh = Some(Pipeline::new( - device, - &pipeline_layout, - shaders.mesh(), - wgpu::PrimitiveTopology::TriangleList, - wgpu::PolygonMode::Line, - color_format, - )); + let mesh = if features.contains(wgpu::Features::POLYGON_MODE_LINE) { + // We need this feature, otherwise initializing the pipeline will + // panic. + + Some(Pipeline::new( + device, + &pipeline_layout, + shaders.mesh(), + wgpu::PrimitiveTopology::TriangleList, + wgpu::PolygonMode::Line, + color_format, + )) + } else { + None + }; Self { model, mesh } } diff --git a/crates/fj-viewer/src/graphics/renderer.rs b/crates/fj-viewer/src/graphics/renderer.rs index 2802a489c..0a26dc690 100644 --- a/crates/fj-viewer/src/graphics/renderer.rs +++ b/crates/fj-viewer/src/graphics/renderer.rs @@ -188,7 +188,7 @@ impl Renderer { let geometries = Geometries::new(&device, &Vertices::empty()); let pipelines = - Pipelines::new(&device, &bind_group_layout, color_format); + Pipelines::new(&device, &bind_group_layout, color_format, features); let navigation_cube_renderer = NavigationCubeRenderer::new(&device, &queue, &surface_config); From 0aa8f82e17be2ccec7fec3ea3be33ec063982cf1 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Thu, 7 Sep 2023 18:15:08 +0200 Subject: [PATCH 6/6] Remove redundant code --- crates/fj-viewer/src/graphics/renderer.rs | 9 +-------- crates/fj-viewer/src/viewer.rs | 4 +--- 2 files changed, 2 insertions(+), 11 deletions(-) diff --git a/crates/fj-viewer/src/graphics/renderer.rs b/crates/fj-viewer/src/graphics/renderer.rs index 0a26dc690..9a65512df 100644 --- a/crates/fj-viewer/src/graphics/renderer.rs +++ b/crates/fj-viewer/src/graphics/renderer.rs @@ -20,7 +20,6 @@ use super::{ #[derive(Debug)] pub struct Renderer { surface: wgpu::Surface, - features: wgpu::Features, device: wgpu::Device, queue: wgpu::Queue, @@ -195,7 +194,6 @@ impl Renderer { Ok(Self { surface, - features, device, queue, @@ -312,7 +310,7 @@ impl Renderer { } if let Some(drawable) = drawables.mesh { - if self.is_line_drawing_available() && config.draw_mesh { + if config.draw_mesh { drawable.draw(&mut render_pass); } } @@ -378,11 +376,6 @@ impl Renderer { texture.create_view(&wgpu::TextureViewDescriptor::default()) } - - /// 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) - } } /// Error describing the set of render surface initialization errors diff --git a/crates/fj-viewer/src/viewer.rs b/crates/fj-viewer/src/viewer.rs index 3f9ea1898..06a367592 100644 --- a/crates/fj-viewer/src/viewer.rs +++ b/crates/fj-viewer/src/viewer.rs @@ -47,9 +47,7 @@ impl Viewer { /// Toggle the "draw mesh" setting pub fn toggle_draw_mesh(&mut self) { - if self.renderer.is_line_drawing_available() { - self.draw_config.draw_mesh = !self.draw_config.draw_mesh; - } + self.draw_config.draw_mesh = !self.draw_config.draw_mesh; } /// Handle the model being updated