Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix initialization panic with GL backend #2015

Merged
merged 6 commits into from
Sep 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions crates/fj-viewer/src/graphics/drawables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ use super::{

pub struct Drawables<'r> {
pub model: Drawable<'r>,
pub mesh: Drawable<'r>,
pub mesh: Option<Drawable<'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 = Drawable::new(&geometries.mesh, &pipelines.mesh);
let mesh = pipelines
.mesh
.as_ref()
.map(|pipeline| Drawable::new(&geometries.mesh, pipeline));

Self { model, mesh }
}
Expand Down
44 changes: 22 additions & 22 deletions crates/fj-viewer/src/graphics/pipelines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ use super::{
#[derive(Debug)]
pub struct Pipelines {
pub model: Pipeline,
pub mesh: Pipeline,
pub lines: Pipeline,
pub mesh: Option<Pipeline>,
}

impl Pipelines {
pub fn new(
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 {
Expand All @@ -28,32 +28,32 @@ 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: Pipeline::new(
let model = Pipeline::new(
device,
&pipeline_layout,
shaders.model(),
wgpu::PrimitiveTopology::TriangleList,
wgpu::PolygonMode::Fill,
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,
),
lines: Pipeline::new(
device,
&pipeline_layout,
shaders.lines(),
wgpu::PrimitiveTopology::LineList,
wgpu::PolygonMode::Line,
color_format,
),
}
))
} else {
None
};

Self { model, mesh }
}
}

Expand Down
15 changes: 5 additions & 10 deletions crates/fj-viewer/src/graphics/renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ use super::{
#[derive(Debug)]
pub struct Renderer {
surface: wgpu::Surface,
features: wgpu::Features,
device: wgpu::Device,
queue: wgpu::Queue,

Expand Down Expand Up @@ -188,14 +187,13 @@ 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);

Ok(Self {
surface,
features,
device,
queue,

Expand Down Expand Up @@ -311,8 +309,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 config.draw_mesh {
drawable.draw(&mut render_pass);
}
}
}

Expand Down Expand Up @@ -376,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
Expand Down
7 changes: 0 additions & 7 deletions crates/fj-viewer/src/graphics/shader.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,3 @@ fn frag_mesh(in: VertexOutput) -> FragmentOutput {
out.color = vec4<f32>(1.0 - in.color.rgb, in.color.a);
return out;
}

@fragment
fn frag_lines(in: VertexOutput) -> FragmentOutput {
var out: FragmentOutput;
out.color = vec4<f32>(in.color.rgb, in.color.a);
return out;
}
7 changes: 0 additions & 7 deletions crates/fj-viewer/src/graphics/shaders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
4 changes: 1 addition & 3 deletions crates/fj-viewer/src/viewer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down