Skip to content

Commit

Permalink
Merge pull request #1241 from hannobraun/render
Browse files Browse the repository at this point in the history
Make some cleanups in rendering and GUI code
  • Loading branch information
hannobraun authored Oct 20, 2022
2 parents 23de8d4 + 08b9a73 commit 9063149
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 107 deletions.
45 changes: 9 additions & 36 deletions crates/fj-viewer/src/graphics/drawables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,48 +19,21 @@ 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 }
}

pub fn draw(
&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,
},
),
});

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(..),
Expand Down
99 changes: 42 additions & 57 deletions crates/fj-viewer/src/graphics/renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,37 +223,50 @@ impl Renderer {
&wgpu::CommandEncoderDescriptor { label: None },
);

self.clear_views(&mut encoder, &color_view);

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,
);
// Need this block here, as a render pass only takes effect once it's
// dropped.
{
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,
},
),
});
render_pass.set_bind_group(0, &self.bind_group, &[]);

let drawables = Drawables::new(&self.geometries, &self.pipelines);

if config.draw_model {
drawables.model.draw(&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(&mut render_pass);
}
if config.draw_debug {
drawables.lines.draw(&mut render_pass);
}
}
}
};

gui.draw(
&self.device,
Expand Down Expand Up @@ -300,34 +313,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)
Expand Down
21 changes: 7 additions & 14 deletions crates/fj-viewer/src/gui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
//! <https://github.com/gfx-rs/wgpu/issues/1492>
use fj_interop::status_report::StatusReport;
use fj_math::Aabb;
use fj_math::{Aabb, Scalar};

use crate::graphics::DrawConfig;

Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -286,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 {}")
}
}

Expand Down

0 comments on commit 9063149

Please sign in to comment.