Skip to content

Commit

Permalink
Add DrawFunctionsInternals::id() (bevyengine#6745)
Browse files Browse the repository at this point in the history
# Objective

- Every usage of `DrawFunctionsInternals::get_id()` was followed by a `.unwrap()`. which just adds boilerplate.

## Solution

- Introduce a fallible version of `DrawFunctionsInternals::get_id()` and use it where possible.
- I also took the opportunity to improve the error message a little in the case where it fails.

---

## Changelog

- Added `DrawFunctionsInternals::id()`
  • Loading branch information
IceSentry authored and alradish committed Jan 22, 2023
1 parent e0e481f commit e99ca5b
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 34 deletions.
15 changes: 3 additions & 12 deletions crates/bevy_pbr/src/material.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,18 +343,9 @@ pub fn queue_material_meshes<M: Material>(
mut transparent_phase,
) in &mut views
{
let draw_opaque_pbr = opaque_draw_functions
.read()
.get_id::<DrawMaterial<M>>()
.unwrap();
let draw_alpha_mask_pbr = alpha_mask_draw_functions
.read()
.get_id::<DrawMaterial<M>>()
.unwrap();
let draw_transparent_pbr = transparent_draw_functions
.read()
.get_id::<DrawMaterial<M>>()
.unwrap();
let draw_opaque_pbr = opaque_draw_functions.read().id::<DrawMaterial<M>>();
let draw_alpha_mask_pbr = alpha_mask_draw_functions.read().id::<DrawMaterial<M>>();
let draw_transparent_pbr = transparent_draw_functions.read().id::<DrawMaterial<M>>();

let mut view_key =
MeshPipelineKey::from_msaa_samples(msaa.samples) | MeshPipelineKey::from_hdr(view.hdr);
Expand Down
5 changes: 1 addition & 4 deletions crates/bevy_pbr/src/render/light.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1635,10 +1635,7 @@ pub fn queue_shadows(
spot_light_entities: Query<&VisibleEntities, With<ExtractedPointLight>>,
) {
for view_lights in &view_lights {
let draw_shadow_mesh = shadow_draw_functions
.read()
.get_id::<DrawShadowMesh>()
.unwrap();
let draw_shadow_mesh = shadow_draw_functions.read().id::<DrawShadowMesh>();
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();
Expand Down
5 changes: 1 addition & 4 deletions crates/bevy_pbr/src/wireframe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,7 @@ fn queue_wireframes(
)>,
mut views: Query<(&ExtractedView, &VisibleEntities, &mut RenderPhase<Opaque3d>)>,
) {
let draw_custom = opaque_3d_draw_functions
.read()
.get_id::<DrawWireframes>()
.unwrap();
let draw_custom = opaque_3d_draw_functions.read().id::<DrawWireframes>();
let msaa_key = MeshPipelineKey::from_msaa_samples(msaa.samples);
for (view, visible_entities, mut opaque_phase) in &mut views {
let rangefinder = view.rangefinder3d();
Expand Down
16 changes: 16 additions & 0 deletions crates/bevy_render/src/render_phase/draw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,22 @@ impl<P: PhaseItem> DrawFunctionsInternal<P> {
pub fn get_id<T: 'static>(&self) -> Option<DrawFunctionId> {
self.indices.get(&TypeId::of::<T>()).copied()
}

/// Retrieves the id of the [`Draw`] function corresponding to their associated type `T`.
///
/// Fallible wrapper for [`Self::get_id()`]
///
/// ## Panics
/// If the id doesn't exist it will panic
pub fn id<T: 'static>(&self) -> DrawFunctionId {
self.get_id::<T>().unwrap_or_else(|| {
panic!(
"Draw function {} not found for {}",
std::any::type_name::<T>(),
std::any::type_name::<P>()
)
})
}
}

/// Stores all draw functions for the [`PhaseItem`] type hidden behind a reader-writer lock.
Expand Down
5 changes: 1 addition & 4 deletions crates/bevy_sprite/src/mesh2d/material.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,10 +320,7 @@ pub fn queue_material2d_meshes<M: Material2d>(
}

for (view, visible_entities, tonemapping, mut transparent_phase) in &mut views {
let draw_transparent_pbr = transparent_draw_functions
.read()
.get_id::<DrawMaterial2d<M>>()
.unwrap();
let draw_transparent_pbr = transparent_draw_functions.read().id::<DrawMaterial2d<M>>();

let mut view_key = Mesh2dPipelineKey::from_msaa_samples(msaa.samples)
| Mesh2dPipelineKey::from_hdr(view.hdr);
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_sprite/src/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ pub fn queue_sprites(
layout: &sprite_pipeline.view_layout,
}));

let draw_sprite_function = draw_functions.read().get_id::<DrawSprite>().unwrap();
let draw_sprite_function = draw_functions.read().id::<DrawSprite>();

// Vertex buffer indices
let mut index = 0;
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_ui/src/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,7 @@ pub fn queue_uinodes(
label: Some("ui_view_bind_group"),
layout: &ui_pipeline.view_layout,
}));
let draw_ui_function = draw_functions.read().get_id::<DrawUi>().unwrap();
let draw_ui_function = draw_functions.read().id::<DrawUi>();
for (view, mut transparent_phase) in &mut views {
let pipeline = pipelines.specialize(
&mut pipeline_cache,
Expand Down
5 changes: 1 addition & 4 deletions examples/2d/mesh2d_manual.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,10 +327,7 @@ pub fn queue_colored_mesh2d(
}
// Iterate each view (a camera is a view)
for (visible_entities, mut transparent_phase, view) in &mut views {
let draw_colored_mesh2d = transparent_draw_functions
.read()
.get_id::<DrawColoredMesh2d>()
.unwrap();
let draw_colored_mesh2d = transparent_draw_functions.read().id::<DrawColoredMesh2d>();

let mesh_key = Mesh2dPipelineKey::from_msaa_samples(msaa.samples)
| Mesh2dPipelineKey::from_hdr(view.hdr);
Expand Down
5 changes: 1 addition & 4 deletions examples/shader/shader_instancing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,7 @@ fn queue_custom(
material_meshes: Query<(Entity, &MeshUniform, &Handle<Mesh>), With<InstanceMaterialData>>,
mut views: Query<(&ExtractedView, &mut RenderPhase<Transparent3d>)>,
) {
let draw_custom = transparent_3d_draw_functions
.read()
.get_id::<DrawCustom>()
.unwrap();
let draw_custom = transparent_3d_draw_functions.read().id::<DrawCustom>();

let msaa_key = MeshPipelineKey::from_msaa_samples(msaa.samples);

Expand Down

0 comments on commit e99ca5b

Please sign in to comment.