Skip to content

Commit

Permalink
Replaced unwraps for bevy_pbr crate
Browse files Browse the repository at this point in the history
  • Loading branch information
omarbassam88 committed Feb 26, 2022
1 parent b697e73 commit cd070ad
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 42 deletions.
18 changes: 12 additions & 6 deletions crates/bevy_pbr/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ impl Plugin for PbrPlugin {

app.world
.get_resource_mut::<Assets<StandardMaterial>>()
.unwrap()
.expect("Could not find `Asset` of `StandardMaterial` in `World`.")
.set_untracked(
Handle::<StandardMaterial>::default(),
StandardMaterial {
Expand Down Expand Up @@ -180,24 +180,30 @@ impl Plugin for PbrPlugin {

let shadow_pass_node = ShadowPassNode::new(&mut render_app.world);
render_app.add_render_command::<Shadow, DrawShadowMesh>();
let mut graph = render_app.world.get_resource_mut::<RenderGraph>().unwrap();
let mut graph = render_app
.world
.get_resource_mut::<RenderGraph>()
.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.");
}
}
32 changes: 23 additions & 9 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 All @@ -258,12 +262,19 @@ impl<M: SpecializedMaterial> SpecializedMeshPipeline for MaterialPipeline<M> {

impl<M: SpecializedMaterial> FromWorld for MaterialPipeline<M> {
fn from_world(world: &mut World) -> Self {
let asset_server = world.get_resource::<AssetServer>().unwrap();
let render_device = world.get_resource::<RenderDevice>().unwrap();
let asset_server = world
.get_resource::<AssetServer>()
.expect("Could not find `AssetServer` in `World`.");
let render_device = world
.get_resource::<RenderDevice>()
.expect("Could not find `RenderDevice` in `World`.");
let material_layout = M::bind_group_layout(render_device);

MaterialPipeline {
mesh_pipeline: world.get_resource::<MeshPipeline>().unwrap().clone(),
mesh_pipeline: world
.get_resource::<MeshPipeline>()
.expect("Could not find `MeshPipeline` in `World`.")
.clone(),
material_layout,
vertex_shader: M::vertex_shader(asset_server),
fragment_shader: M::fragment_shader(asset_server),
Expand All @@ -289,8 +300,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 +340,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
37 changes: 25 additions & 12 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.")
})
}

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 @@ -1187,14 +1194,18 @@ impl Node for ShadowPassNode {
}),
};

let draw_functions = world.get_resource::<DrawFunctions<Shadow>>().unwrap();
let draw_functions = world
.get_resource::<DrawFunctions<Shadow>>()
.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);
}
}
Expand All @@ -1221,14 +1232,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 bind shadow view group to render pass."),
&[view_uniform_offset.offset],
);

Expand Down
32 changes: 22 additions & 10 deletions crates/bevy_pbr/src/render/mesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,9 @@ pub struct MeshPipeline {

impl FromWorld for MeshPipeline {
fn from_world(world: &mut World) -> Self {
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: &[
// View
Expand Down Expand Up @@ -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::<RenderQueue>().unwrap();
let render_queue = world
.get_resource_mut::<RenderQueue>()
.expect("Could not find `RenderQueue` in `World`.");
render_queue.write_texture(
ImageCopyTexture {
texture: &texture,
Expand All @@ -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,
},
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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"),
Expand Down Expand Up @@ -624,7 +635,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 get view.");
pass.set_bind_group(
I,
&mesh_view_bind_group.value,
Expand All @@ -648,7 +660,7 @@ 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 get mesh index.");
pass.set_bind_group(
I,
&mesh_bind_group.into_inner().value,
Expand All @@ -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 {
Expand Down
20 changes: 16 additions & 4 deletions crates/bevy_pbr/src/wireframe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<MeshPipeline>().unwrap().clone(),
mesh_pipeline: render_world
.get_resource::<MeshPipeline>()
.expect("Could not find `MeshPipeline` in `World`.")
.clone(),
shader: WIREFRAME_SHADER_HANDLE.typed(),
}
}
Expand All @@ -95,9 +98,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 +133,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

0 comments on commit cd070ad

Please sign in to comment.