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

[Merged by Bors] - Add DrawFunctionsInternals::id() #6745

Closed
wants to merge 3 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
15 changes: 3 additions & 12 deletions crates/bevy_pbr/src/material.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,18 +347,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