Skip to content

Commit

Permalink
Resolve some unwraps
Browse files Browse the repository at this point in the history
  • Loading branch information
maxammann committed Apr 9, 2023
1 parent 4710740 commit fd7e522
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 11 deletions.
2 changes: 2 additions & 0 deletions maplibre/src/render/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use crate::render::graph::RenderGraphError;
pub enum RenderError {
#[error("error in surface")]
Surface(#[from] wgpu::SurfaceError),
#[error("error during surface creation")]
CreateSurfaceError(#[from] wgpu::CreateSurfaceError),
#[error("error in render graph")]
Graph(#[from] RenderGraphError),
#[error("error while requesting device")]
Expand Down
19 changes: 12 additions & 7 deletions maplibre/src/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,15 @@ impl RenderResources {
}
}

pub fn recreate_surface<MW>(&mut self, window: &MW, instance: &wgpu::Instance)
pub fn recreate_surface<MW>(
&mut self,
window: &MW,
instance: &wgpu::Instance,
) -> Result<(), RenderError>
where
MW: MapWindow + HeadedMapWindow,
{
self.surface.recreate::<MW>(window, instance);
self.surface.recreate::<MW>(window, instance)
}

pub fn surface(&self) -> &Surface {
Expand Down Expand Up @@ -165,7 +169,7 @@ impl Renderer {
dx12_shader_compiler: Default::default(),
});

let surface: wgpu::Surface = unsafe { instance.create_surface(window.raw()).unwrap() };
let surface: wgpu::Surface = unsafe { instance.create_surface(window.raw())? };

let (adapter, device, queue) = Self::request_device(
&instance,
Expand Down Expand Up @@ -480,13 +484,14 @@ mod tests {
let render_state = RenderResources::new(Surface::from_image(
&device,
&HeadlessMapWindow {
size: WindowSize::new(100, 100).unwrap(),
size: WindowSize::new(100, 100).expect("invalid headless map size"),
},
&RendererSettings::default(),
));

let world = World::default();
RenderGraphRunner::run(&graph, &device, &queue, &render_state, &world).unwrap();
RenderGraphRunner::run(&graph, &device, &queue, &render_state, &world)
.expect("failed to run graph runner");
}
}

Expand Down Expand Up @@ -546,7 +551,7 @@ impl<E: Environment> Plugin<E> for RenderPlugin {
// Edges
draw_graph
.add_node_edge(input_node_id, draw_graph::node::MAIN_PASS)
.unwrap();
.expect("main pass or draw node does not exist");

graph.add_sub_graph(draw_graph::NAME, draw_graph);
graph.add_node(main_graph::node::MAIN_PASS_DEPENDENCIES, EmptyNode);
Expand All @@ -556,7 +561,7 @@ impl<E: Environment> Plugin<E> for RenderPlugin {
main_graph::node::MAIN_PASS_DEPENDENCIES,
main_graph::node::MAIN_PASS_DRIVER,
)
.unwrap();
.expect("main pass driver or dependencies do not exist");

// render graph dependency
resources.init::<RenderPhase<LayerItem>>();
Expand Down
19 changes: 15 additions & 4 deletions maplibre/src/render/resource/surface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::{mem::size_of, num::NonZeroU32, sync::Arc};

use wgpu::TextureFormatFeatures;

use crate::render::error::RenderError;
use crate::{
render::{
eventually::HasChanged,
Expand Down Expand Up @@ -68,11 +69,16 @@ impl WindowHead {
self.surface.configure(device, &surface_config);
}

pub fn recreate_surface<MW>(&mut self, window: &MW, instance: &wgpu::Instance)
pub fn recreate_surface<MW>(
&mut self,
window: &MW,
instance: &wgpu::Instance,
) -> Result<(), RenderError>
where
MW: MapWindow + HeadedMapWindow,
{
self.surface = unsafe { instance.create_surface(window.raw()).unwrap() };
self.surface = unsafe { instance.create_surface(window.raw())? };
Ok(())
}

pub fn surface(&self) -> &wgpu::Surface {
Expand Down Expand Up @@ -305,18 +311,23 @@ impl Surface {
}
}

pub fn recreate<MW>(&mut self, window: &MW, instance: &wgpu::Instance)
pub fn recreate<MW>(
&mut self,
window: &MW,
instance: &wgpu::Instance,
) -> Result<(), RenderError>
where
MW: MapWindow + HeadedMapWindow,
{
match &mut self.head {
Head::Headed(window_head) => {
if window_head.has_changed(&(self.size.width(), self.size.height())) {
window_head.recreate_surface(window, instance);
window_head.recreate_surface(window, instance)?;
}
}
Head::Headless(_) => {}
}
Ok(())
}

pub fn head(&self) -> &Head {
Expand Down

0 comments on commit fd7e522

Please sign in to comment.