Add DrawFunctionsInternals::id() (#6745)

# 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()`
This commit is contained in:
IceSentry 2022-11-28 13:54:13 +00:00
parent d79888bdae
commit f119d9df8e
9 changed files with 26 additions and 34 deletions

View file

@ -343,18 +343,9 @@ pub fn queue_material_meshes<M: Material>(
mut transparent_phase, mut transparent_phase,
) in &mut views ) in &mut views
{ {
let draw_opaque_pbr = opaque_draw_functions let draw_opaque_pbr = opaque_draw_functions.read().id::<DrawMaterial<M>>();
.read() let draw_alpha_mask_pbr = alpha_mask_draw_functions.read().id::<DrawMaterial<M>>();
.get_id::<DrawMaterial<M>>() let draw_transparent_pbr = transparent_draw_functions.read().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 mut view_key = let mut view_key =
MeshPipelineKey::from_msaa_samples(msaa.samples) | MeshPipelineKey::from_hdr(view.hdr); MeshPipelineKey::from_msaa_samples(msaa.samples) | MeshPipelineKey::from_hdr(view.hdr);

View file

@ -1635,10 +1635,7 @@ pub fn queue_shadows(
spot_light_entities: Query<&VisibleEntities, With<ExtractedPointLight>>, spot_light_entities: Query<&VisibleEntities, With<ExtractedPointLight>>,
) { ) {
for view_lights in &view_lights { for view_lights in &view_lights {
let draw_shadow_mesh = shadow_draw_functions let draw_shadow_mesh = shadow_draw_functions.read().id::<DrawShadowMesh>();
.read()
.get_id::<DrawShadowMesh>()
.unwrap();
for view_light_entity in view_lights.lights.iter().copied() { for view_light_entity in view_lights.lights.iter().copied() {
let (light_entity, mut shadow_phase) = let (light_entity, mut shadow_phase) =
view_light_shadow_phases.get_mut(view_light_entity).unwrap(); view_light_shadow_phases.get_mut(view_light_entity).unwrap();

View file

@ -116,10 +116,7 @@ fn queue_wireframes(
)>, )>,
mut views: Query<(&ExtractedView, &VisibleEntities, &mut RenderPhase<Opaque3d>)>, mut views: Query<(&ExtractedView, &VisibleEntities, &mut RenderPhase<Opaque3d>)>,
) { ) {
let draw_custom = opaque_3d_draw_functions let draw_custom = opaque_3d_draw_functions.read().id::<DrawWireframes>();
.read()
.get_id::<DrawWireframes>()
.unwrap();
let msaa_key = MeshPipelineKey::from_msaa_samples(msaa.samples); let msaa_key = MeshPipelineKey::from_msaa_samples(msaa.samples);
for (view, visible_entities, mut opaque_phase) in &mut views { for (view, visible_entities, mut opaque_phase) in &mut views {
let rangefinder = view.rangefinder3d(); let rangefinder = view.rangefinder3d();

View file

@ -97,6 +97,22 @@ impl<P: PhaseItem> DrawFunctionsInternal<P> {
pub fn get_id<T: 'static>(&self) -> Option<DrawFunctionId> { pub fn get_id<T: 'static>(&self) -> Option<DrawFunctionId> {
self.indices.get(&TypeId::of::<T>()).copied() 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. /// Stores all draw functions for the [`PhaseItem`] type hidden behind a reader-writer lock.

View file

@ -320,10 +320,7 @@ pub fn queue_material2d_meshes<M: Material2d>(
} }
for (view, visible_entities, tonemapping, mut transparent_phase) in &mut views { for (view, visible_entities, tonemapping, mut transparent_phase) in &mut views {
let draw_transparent_pbr = transparent_draw_functions let draw_transparent_pbr = transparent_draw_functions.read().id::<DrawMaterial2d<M>>();
.read()
.get_id::<DrawMaterial2d<M>>()
.unwrap();
let mut view_key = Mesh2dPipelineKey::from_msaa_samples(msaa.samples) let mut view_key = Mesh2dPipelineKey::from_msaa_samples(msaa.samples)
| Mesh2dPipelineKey::from_hdr(view.hdr); | Mesh2dPipelineKey::from_hdr(view.hdr);

View file

@ -488,7 +488,7 @@ pub fn queue_sprites(
layout: &sprite_pipeline.view_layout, 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 // Vertex buffer indices
let mut index = 0; let mut index = 0;

View file

@ -588,7 +588,7 @@ pub fn queue_uinodes(
label: Some("ui_view_bind_group"), label: Some("ui_view_bind_group"),
layout: &ui_pipeline.view_layout, 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 { for (view, mut transparent_phase) in &mut views {
let pipeline = pipelines.specialize( let pipeline = pipelines.specialize(
&mut pipeline_cache, &mut pipeline_cache,

View file

@ -327,10 +327,7 @@ pub fn queue_colored_mesh2d(
} }
// Iterate each view (a camera is a view) // Iterate each view (a camera is a view)
for (visible_entities, mut transparent_phase, view) in &mut views { for (visible_entities, mut transparent_phase, view) in &mut views {
let draw_colored_mesh2d = transparent_draw_functions let draw_colored_mesh2d = transparent_draw_functions.read().id::<DrawColoredMesh2d>();
.read()
.get_id::<DrawColoredMesh2d>()
.unwrap();
let mesh_key = Mesh2dPipelineKey::from_msaa_samples(msaa.samples) let mesh_key = Mesh2dPipelineKey::from_msaa_samples(msaa.samples)
| Mesh2dPipelineKey::from_hdr(view.hdr); | Mesh2dPipelineKey::from_hdr(view.hdr);

View file

@ -109,10 +109,7 @@ fn queue_custom(
material_meshes: Query<(Entity, &MeshUniform, &Handle<Mesh>), With<InstanceMaterialData>>, material_meshes: Query<(Entity, &MeshUniform, &Handle<Mesh>), With<InstanceMaterialData>>,
mut views: Query<(&ExtractedView, &mut RenderPhase<Transparent3d>)>, mut views: Query<(&ExtractedView, &mut RenderPhase<Transparent3d>)>,
) { ) {
let draw_custom = transparent_3d_draw_functions let draw_custom = transparent_3d_draw_functions.read().id::<DrawCustom>();
.read()
.get_id::<DrawCustom>()
.unwrap();
let msaa_key = MeshPipelineKey::from_msaa_samples(msaa.samples); let msaa_key = MeshPipelineKey::from_msaa_samples(msaa.samples);