From 3d33632247f25784ae32a8c035865d62e9c3f0dc Mon Sep 17 00:00:00 2001 From: Omar Bassam Date: Sat, 26 Feb 2022 19:19:33 +0200 Subject: [PATCH] Replaced unwraps for bevy_pbr crate --- crates/bevy_pbr/src/lib.rs | 18 +++++++++----- crates/bevy_pbr/src/material.rs | 32 ++++++++++++++++++------- crates/bevy_pbr/src/pbr_material.rs | 2 +- crates/bevy_pbr/src/render/light.rs | 37 +++++++++++++++++++---------- crates/bevy_pbr/src/render/mesh.rs | 32 +++++++++++++++++-------- crates/bevy_pbr/src/wireframe.rs | 20 ++++++++++++---- 6 files changed, 99 insertions(+), 42 deletions(-) diff --git a/crates/bevy_pbr/src/lib.rs b/crates/bevy_pbr/src/lib.rs index f264d829d3802c..1f222dc25c94fd 100644 --- a/crates/bevy_pbr/src/lib.rs +++ b/crates/bevy_pbr/src/lib.rs @@ -125,7 +125,7 @@ impl Plugin for PbrPlugin { app.world .get_resource_mut::>() - .unwrap() + .expect("Could not find `Asset` of `StandardMaterial` in `World`.") .set_untracked( Handle::::default(), StandardMaterial { @@ -180,24 +180,30 @@ impl Plugin for PbrPlugin { let shadow_pass_node = ShadowPassNode::new(&mut render_app.world); render_app.add_render_command::(); - let mut graph = render_app.world.get_resource_mut::().unwrap(); + let mut graph = render_app + .world + .get_resource_mut::() + .expect("Could not find `RenderGraph` in `World`."); let draw_3d_graph = graph .get_sub_graph_mut(bevy_core_pipeline::draw_3d_graph::NAME) - .unwrap(); + .expect("Could not find a mutable 3d sub graph in `RenderGraph`."); draw_3d_graph.add_node(draw_3d_graph::node::SHADOW_PASS, shadow_pass_node); draw_3d_graph .add_node_edge( draw_3d_graph::node::SHADOW_PASS, bevy_core_pipeline::draw_3d_graph::node::MAIN_PASS, ) - .unwrap(); + .expect("Could not add node edge to 3d graph."); draw_3d_graph .add_slot_edge( - draw_3d_graph.input_node().unwrap().id, + draw_3d_graph + .input_node() + .expect("Could not get input node for 3d graph.") + .id, bevy_core_pipeline::draw_3d_graph::input::VIEW_ENTITY, draw_3d_graph::node::SHADOW_PASS, ShadowPassNode::IN_VIEW, ) - .unwrap(); + .expect("Could not add slot edge for 3d graph."); } } diff --git a/crates/bevy_pbr/src/material.rs b/crates/bevy_pbr/src/material.rs index d01e4f79a557ce..4eb8a17dfdbc8e 100644 --- a/crates/bevy_pbr/src/material.rs +++ b/crates/bevy_pbr/src/material.rs @@ -243,7 +243,11 @@ impl SpecializedMeshPipeline for MaterialPipeline { } if let Some(fragment_shader) = &self.fragment_shader { - descriptor.fragment.as_mut().unwrap().shader = fragment_shader.clone(); + descriptor + .fragment + .as_mut() + .expect("Could not mutate Fragment Shader.") + .shader = fragment_shader.clone(); } descriptor.layout = Some(vec![ self.mesh_pipeline.view_layout.clone(), @@ -258,12 +262,19 @@ impl SpecializedMeshPipeline for MaterialPipeline { impl FromWorld for MaterialPipeline { fn from_world(world: &mut World) -> Self { - let asset_server = world.get_resource::().unwrap(); - let render_device = world.get_resource::().unwrap(); + let asset_server = world + .get_resource::() + .expect("Could not find `AssetServer` in `World`."); + let render_device = world + .get_resource::() + .expect("Could not find `RenderDevice` in `World`."); let material_layout = M::bind_group_layout(render_device); MaterialPipeline { - mesh_pipeline: world.get_resource::().unwrap().clone(), + mesh_pipeline: world + .get_resource::() + .expect("Could not find `MeshPipeline` in `World`.") + .clone(), material_layout, vertex_shader: M::vertex_shader(asset_server), fragment_shader: M::fragment_shader(asset_server), @@ -289,8 +300,11 @@ impl EntityRenderCommand for SetMaterial (materials, query): SystemParamItem<'w, '_, Self::Param>, pass: &mut TrackedRenderPass<'w>, ) -> RenderCommandResult { - let material_handle = query.get(item).unwrap(); - let material = materials.into_inner().get(material_handle).unwrap(); + let material_handle = query.get(item).expect("Could not find entity."); + let material = materials + .into_inner() + .get(material_handle) + .expect("Could not load material."); pass.set_bind_group( I, M::bind_group(material), @@ -326,15 +340,15 @@ pub fn queue_material_meshes( let draw_opaque_pbr = opaque_draw_functions .read() .get_id::>() - .unwrap(); + .expect("Could not get `DrawMaterial` for `Opaque3d`"); let draw_alpha_mask_pbr = alpha_mask_draw_functions .read() .get_id::>() - .unwrap(); + .expect("Could not get `DrawMaterial` for `AlphaMask3d`"); let draw_transparent_pbr = transparent_draw_functions .read() .get_id::>() - .unwrap(); + .expect("Could not get `DrawMaterial` for `Transparent3d`"); let inverse_view_matrix = view.transform.compute_matrix().inverse(); let inverse_view_row_2 = inverse_view_matrix.row(2); diff --git a/crates/bevy_pbr/src/pbr_material.rs b/crates/bevy_pbr/src/pbr_material.rs index eaf45397a78a42..6311773a029db2 100644 --- a/crates/bevy_pbr/src/pbr_material.rs +++ b/crates/bevy_pbr/src/pbr_material.rs @@ -348,7 +348,7 @@ impl SpecializedMaterial for StandardMaterial { descriptor .fragment .as_mut() - .unwrap() + .expect("Could not mutate fragment shader for Normal map.") .shader_defs .push(String::from("STANDARDMATERIAL_NORMAL_MAP")); } diff --git a/crates/bevy_pbr/src/render/light.rs b/crates/bevy_pbr/src/render/light.rs index 562c9b6d2031c0..403dd08f0fe685 100644 --- a/crates/bevy_pbr/src/render/light.rs +++ b/crates/bevy_pbr/src/render/light.rs @@ -165,7 +165,9 @@ pub struct ShadowPipeline { impl FromWorld for ShadowPipeline { fn from_world(world: &mut World) -> Self { let world = world.cell(); - let render_device = world.get_resource::().unwrap(); + let render_device = world + .get_resource::() + .expect("Could not find `RenderDevice` in `World`."); let view_layout = render_device.create_bind_group_layout(&BindGroupLayoutDescriptor { entries: &[ @@ -184,7 +186,9 @@ impl FromWorld for ShadowPipeline { label: Some("shadow_view_layout"), }); - let mesh_pipeline = world.get_resource::().unwrap(); + let mesh_pipeline = world + .get_resource::() + .expect("Could not find `MeshPipeline` in `World`."); ShadowPipeline { view_layout, @@ -229,7 +233,9 @@ impl ShadowPipelineKey { let primitive_topology_bits = ((primitive_topology as u32) & Self::PRIMITIVE_TOPOLOGY_MASK_BITS) << Self::PRIMITIVE_TOPOLOGY_SHIFT_BITS; - Self::from_bits(primitive_topology_bits).unwrap() + Self::from_bits(primitive_topology_bits).unwrap_or_else(|| { + panic!("Could not create `ShadowPipelineKey` from {primitive_topology_bits} bits.") + }) } pub fn primitive_topology(&self) -> PrimitiveTopology { @@ -692,7 +698,7 @@ pub fn prepare_lights( let light_index = *global_light_meta .entity_to_index .get(&light_entity) - .unwrap(); + .expect("Could not get light index."); // ignore scale because we don't want to effectively scale light radius and range // by applying those as a view transform to shadow map rendering of objects // and ignore rotation because we want the shadow map projections to align with the axes @@ -1050,10 +1056,11 @@ pub fn queue_shadows( let draw_shadow_mesh = shadow_draw_functions .read() .get_id::() - .unwrap(); + .expect("Could not get `DrawShadowMesh` for `Shadow`."); for view_light_entity in view_lights.lights.iter().copied() { - let (light_entity, mut shadow_phase) = - view_light_shadow_phases.get_mut(view_light_entity).unwrap(); + let (light_entity, mut shadow_phase) = view_light_shadow_phases + .get_mut(view_light_entity) + .expect("Could not get light entity."); let visible_entities = match light_entity { LightEntity::Directional { light_entity } => directional_light_entities .get(*light_entity) @@ -1173,7 +1180,7 @@ impl Node for ShadowPassNode { let (view_light, shadow_phase) = self .view_light_query .get_manual(world, view_light_entity) - .unwrap(); + .expect("Could not get light entity from `World`."); let pass_descriptor = RenderPassDescriptor { label: Some(&view_light.pass_name), color_attachments: &[], @@ -1187,14 +1194,18 @@ impl Node for ShadowPassNode { }), }; - let draw_functions = world.get_resource::>().unwrap(); + let draw_functions = world + .get_resource::>() + .expect("Could not find `DrawFunctions` for `Shadow` in `World`."); let render_pass = render_context .command_encoder .begin_render_pass(&pass_descriptor); let mut draw_functions = draw_functions.write(); let mut tracked_pass = TrackedRenderPass::new(render_pass); for item in &shadow_phase.items { - let draw_function = draw_functions.get_mut(item.draw_function).unwrap(); + let draw_function = draw_functions + .get_mut(item.draw_function) + .expect("Could not get draw function for shadow phase item."); draw_function.draw(world, &mut tracked_pass, view_light_entity, item); } } @@ -1221,14 +1232,16 @@ impl EntityRenderCommand for SetShadowViewBindGroup { (light_meta, view_query): SystemParamItem<'w, '_, Self::Param>, pass: &mut TrackedRenderPass<'w>, ) -> RenderCommandResult { - let view_uniform_offset = view_query.get(view).unwrap(); + let view_uniform_offset = view_query + .get(view) + .expect("Could not get view uniform offset."); pass.set_bind_group( I, light_meta .into_inner() .shadow_view_bind_group .as_ref() - .unwrap(), + .expect("Could not bind shadow view group to render pass."), &[view_uniform_offset.offset], ); diff --git a/crates/bevy_pbr/src/render/mesh.rs b/crates/bevy_pbr/src/render/mesh.rs index 4c34a462eb0f73..e8dc1d1d6afbb2 100644 --- a/crates/bevy_pbr/src/render/mesh.rs +++ b/crates/bevy_pbr/src/render/mesh.rs @@ -165,7 +165,9 @@ pub struct MeshPipeline { impl FromWorld for MeshPipeline { fn from_world(world: &mut World) -> Self { - let render_device = world.get_resource::().unwrap(); + let render_device = world + .get_resource::() + .expect("Could not find `RenderDevice` in `World`."); let view_layout = render_device.create_bind_group_layout(&BindGroupLayoutDescriptor { entries: &[ // View @@ -301,7 +303,9 @@ impl FromWorld for MeshPipeline { let sampler = render_device.create_sampler(&image.sampler_descriptor); let format_size = image.texture_descriptor.format.pixel_size(); - let render_queue = world.get_resource_mut::().unwrap(); + let render_queue = world + .get_resource_mut::() + .expect("Could not find `RenderQueue` in `World`."); render_queue.write_texture( ImageCopyTexture { texture: &texture, @@ -316,7 +320,9 @@ impl FromWorld for MeshPipeline { std::num::NonZeroU32::new( image.texture_descriptor.size.width * format_size as u32, ) - .unwrap(), + .unwrap_or_else(|| { + panic!("Could not write texture with format size {format_size}.") + }), ), rows_per_image: None, }, @@ -380,7 +386,8 @@ impl MeshPipelineKey { pub fn from_msaa_samples(msaa_samples: u32) -> Self { let msaa_bits = ((msaa_samples - 1) & Self::MSAA_MASK_BITS) << Self::MSAA_SHIFT_BITS; - MeshPipelineKey::from_bits(msaa_bits).unwrap() + MeshPipelineKey::from_bits(msaa_bits) + .unwrap_or_else(|| panic!("Could not create `MeshPipelineKey` from {msaa_bits} bits.")) } pub fn msaa_samples(&self) -> u32 { @@ -391,7 +398,11 @@ impl MeshPipelineKey { let primitive_topology_bits = ((primitive_topology as u32) & Self::PRIMITIVE_TOPOLOGY_MASK_BITS) << Self::PRIMITIVE_TOPOLOGY_SHIFT_BITS; - MeshPipelineKey::from_bits(primitive_topology_bits).unwrap() + MeshPipelineKey::from_bits(primitive_topology_bits).unwrap_or_else(|| { + panic!( + "Could not create `MeshPipelineKey` from {primitive_topology_bits} topology bits." + ) + }) } pub fn primitive_topology(&self) -> PrimitiveTopology { @@ -589,14 +600,14 @@ pub fn queue_mesh_view_bind_groups( resource: view_cluster_bindings .cluster_light_index_lists .binding() - .unwrap(), + .expect("Could not create `BindGroupEntry`."), }, BindGroupEntry { binding: 8, resource: view_cluster_bindings .cluster_offsets_and_counts .binding() - .unwrap(), + .expect("Could not create `BindGroupEntry`."), }, ], label: Some("mesh_view_bind_group"), @@ -624,7 +635,8 @@ impl EntityRenderCommand for SetMeshViewBindGroup { view_query: SystemParamItem<'w, '_, Self::Param>, pass: &mut TrackedRenderPass<'w>, ) -> RenderCommandResult { - let (view_uniform, view_lights, mesh_view_bind_group) = view_query.get(view).unwrap(); + let (view_uniform, view_lights, mesh_view_bind_group) = + view_query.get(view).expect("Could not get view."); pass.set_bind_group( I, &mesh_view_bind_group.value, @@ -648,7 +660,7 @@ impl EntityRenderCommand for SetMeshBindGroup { (mesh_bind_group, mesh_query): SystemParamItem<'w, '_, Self::Param>, pass: &mut TrackedRenderPass<'w>, ) -> RenderCommandResult { - let mesh_index = mesh_query.get(item).unwrap(); + let mesh_index = mesh_query.get(item).expect("Could not get mesh index."); pass.set_bind_group( I, &mesh_bind_group.into_inner().value, @@ -668,7 +680,7 @@ impl EntityRenderCommand for DrawMesh { (meshes, mesh_query): SystemParamItem<'w, '_, Self::Param>, pass: &mut TrackedRenderPass<'w>, ) -> RenderCommandResult { - let mesh_handle = mesh_query.get(item).unwrap(); + let mesh_handle = mesh_query.get(item).expect("Could not get mesh handle."); if let Some(gpu_mesh) = meshes.into_inner().get(mesh_handle) { pass.set_vertex_buffer(0, gpu_mesh.vertex_buffer.slice(..)); match &gpu_mesh.buffer_info { diff --git a/crates/bevy_pbr/src/wireframe.rs b/crates/bevy_pbr/src/wireframe.rs index 7a65ae267ef99f..bacff996b43461 100644 --- a/crates/bevy_pbr/src/wireframe.rs +++ b/crates/bevy_pbr/src/wireframe.rs @@ -79,7 +79,10 @@ pub struct WireframePipeline { impl FromWorld for WireframePipeline { fn from_world(render_world: &mut World) -> Self { WireframePipeline { - mesh_pipeline: render_world.get_resource::().unwrap().clone(), + mesh_pipeline: render_world + .get_resource::() + .expect("Could not find `MeshPipeline` in `World`.") + .clone(), shader: WIREFRAME_SHADER_HANDLE.typed(), } } @@ -95,9 +98,18 @@ impl SpecializedMeshPipeline for WireframePipeline { ) -> Result { let mut descriptor = self.mesh_pipeline.specialize(key, layout)?; descriptor.vertex.shader = self.shader.clone_weak(); - descriptor.fragment.as_mut().unwrap().shader = self.shader.clone_weak(); + descriptor + .fragment + .as_mut() + .expect("Could not mutate fragment shader.") + .shader = self.shader.clone_weak(); descriptor.primitive.polygon_mode = PolygonMode::Line; - descriptor.depth_stencil.as_mut().unwrap().bias.slope_scale = 1.0; + descriptor + .depth_stencil + .as_mut() + .expect("Could not mutate depth stencil.") + .bias + .slope_scale = 1.0; Ok(descriptor) } } @@ -121,7 +133,7 @@ fn queue_wireframes( let draw_custom = opaque_3d_draw_functions .read() .get_id::() - .unwrap(); + .expect("Could not get `DrawWireframes` for `Opaque3d`."); let msaa_key = MeshPipelineKey::from_msaa_samples(msaa.samples); for (view, mut transparent_phase) in views.iter_mut() { let view_matrix = view.transform.compute_matrix();