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

Replace unwraps with expects in bevy_pbr #4046

Closed
wants to merge 14 commits into from
Closed
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
11 changes: 7 additions & 4 deletions crates/bevy_pbr/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,21 +182,24 @@ impl Plugin for PbrPlugin {
let mut graph = render_app.world.resource_mut::<RenderGraph>();
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 from shadow pass to main pass.");
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 to 3d graph for shadow pass view entity input.");
}
}
19 changes: 13 additions & 6 deletions crates/bevy_pbr/src/material.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,11 @@ impl<M: SpecializedMaterial> SpecializedMeshPipeline for MaterialPipeline<M> {
}

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(),
Expand Down Expand Up @@ -289,8 +293,11 @@ impl<M: SpecializedMaterial, const I: usize> 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),
Expand Down Expand Up @@ -326,15 +333,15 @@ pub fn queue_material_meshes<M: SpecializedMaterial>(
let draw_opaque_pbr = opaque_draw_functions
.read()
.get_id::<DrawMaterial<M>>()
.unwrap();
.expect("Could not get `DrawMaterial` for `Opaque3d`");
let draw_alpha_mask_pbr = alpha_mask_draw_functions
.read()
.get_id::<DrawMaterial<M>>()
.unwrap();
.expect("Could not get `DrawMaterial` for `AlphaMask3d`");
let draw_transparent_pbr = transparent_draw_functions
.read()
.get_id::<DrawMaterial<M>>()
.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);
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_pbr/src/pbr_material.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}
Expand Down
33 changes: 22 additions & 11 deletions crates/bevy_pbr/src/render/light.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<RenderDevice>().unwrap();
let render_device = world
.get_resource::<RenderDevice>()
.expect("Could not find `RenderDevice` in `World`.");

let view_layout = render_device.create_bind_group_layout(&BindGroupLayoutDescriptor {
entries: &[
Expand All @@ -184,7 +186,9 @@ impl FromWorld for ShadowPipeline {
label: Some("shadow_view_layout"),
});

let mesh_pipeline = world.get_resource::<MeshPipeline>().unwrap();
let mesh_pipeline = world
.get_resource::<MeshPipeline>()
.expect("Could not find `MeshPipeline` in `World`.");

ShadowPipeline {
view_layout,
Expand Down Expand Up @@ -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.")
omarbassam88 marked this conversation as resolved.
Show resolved Hide resolved
})
}

pub fn primitive_topology(&self) -> PrimitiveTopology {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -1050,10 +1056,11 @@ pub fn queue_shadows(
let draw_shadow_mesh = shadow_draw_functions
.read()
.get_id::<DrawShadowMesh>()
.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)
Expand Down Expand Up @@ -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: &[],
Expand All @@ -1194,7 +1201,9 @@ impl Node for ShadowPassNode {
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);
}
}
Expand All @@ -1221,14 +1230,16 @@ impl<const I: usize> EntityRenderCommand for SetShadowViewBindGroup<I> {
(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 get shadow view bind group."),
&[view_uniform_offset.offset],
);

Expand Down
33 changes: 25 additions & 8 deletions crates/bevy_pbr/src/render/mesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,10 @@ impl FromWorld for MeshPipeline {
std::num::NonZeroU32::new(
image.texture_descriptor.size.width * format_size as u32,
)
.unwrap(),
.unwrap_or_else(|| {
panic!("Could not calculate texture `bytes_per_row` to be `NonZeroU32` from width {:?} * format size {}.",
image.texture_descriptor.size.width, format_size)
}),
),
rows_per_image: None,
},
Expand Down Expand Up @@ -380,7 +383,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 {
Expand All @@ -391,7 +395,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 {
Expand Down Expand Up @@ -589,14 +597,18 @@ pub fn queue_mesh_view_bind_groups(
resource: view_cluster_bindings
.cluster_light_index_lists
.binding()
.unwrap(),
.expect(
"Could not create `cluster_light_index_lists` `BindGroupEntry`.",
),
},
BindGroupEntry {
binding: 8,
resource: view_cluster_bindings
.cluster_offsets_and_counts
.binding()
.unwrap(),
.expect(
"Could not create `cluster_offsets_and_counts` `BindGroupEntry`.",
),
},
],
label: Some("mesh_view_bind_group"),
Expand Down Expand Up @@ -624,7 +636,8 @@ impl<const I: usize> EntityRenderCommand for SetMeshViewBindGroup<I> {
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 find view entity.");
pass.set_bind_group(
I,
&mesh_view_bind_group.value,
Expand All @@ -648,7 +661,9 @@ impl<const I: usize> EntityRenderCommand for SetMeshBindGroup<I> {
(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 find mesh entity to get mesh index.");
pass.set_bind_group(
I,
&mesh_bind_group.into_inner().value,
Expand All @@ -668,7 +683,9 @@ 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 find mesh entity to 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 {
Expand Down
15 changes: 12 additions & 3 deletions crates/bevy_pbr/src/wireframe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,18 @@ impl SpecializedMeshPipeline for WireframePipeline {
) -> Result<RenderPipelineDescriptor, SpecializedMeshPipelineError> {
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)
}
}
Expand All @@ -121,7 +130,7 @@ fn queue_wireframes(
let draw_custom = opaque_3d_draw_functions
.read()
.get_id::<DrawWireframes>()
.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();
Expand Down