Skip to content

Commit

Permalink
Remove Handle<T> trait implementations that are dependent on `Compo…
Browse files Browse the repository at this point in the history
…nent` (#15749)

# Objective

- Another step towards #15716
- Remove trait implementations that are dependent on `Handle<T>` being a
`Component`

## Solution

- Remove unused `ExtractComponent` trait implementation for `Handle<T>`
- Remove unused `ExtractInstance` trait implementation for `AssetId`
- Although the `ExtractInstance` trait wasn't used, the `AssetId`s were
being stored inside of `ExtractedInstances` which has an
`ExtractInstance` trait bound on its contents.
I've upgraded the `RenderMaterialInstances` type alias to be its own
resource, identical to `ExtractedInstances<AssetId<M>>` to get around
that with minimal breakage.
## Testing

Tested `many_cubes`, rendering did not explode
  • Loading branch information
tim-blackbird authored Oct 9, 2024
1 parent 123a19a commit e19c53e
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 30 deletions.
14 changes: 11 additions & 3 deletions crates/bevy_pbr/src/material.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ use bevy_core_pipeline::{
};
use bevy_derive::{Deref, DerefMut};
use bevy_ecs::{
entity::EntityHashMap,
prelude::*,
system::{lifetimeless::SRes, SystemParamItem},
};
use bevy_reflect::std_traits::ReflectDefault;
use bevy_reflect::Reflect;
use bevy_render::{
camera::TemporalJitter,
extract_instances::ExtractedInstances,
extract_resource::ExtractResource,
mesh::{Mesh3d, MeshVertexBufferLayoutRef, RenderMesh},
render_asset::{PrepareAssetError, RenderAsset, RenderAssetPlugin, RenderAssets},
Expand Down Expand Up @@ -276,7 +276,7 @@ where
if let Some(render_app) = app.get_sub_app_mut(RenderApp) {
render_app
.init_resource::<DrawFunctions<Shadow>>()
.init_resource::<ExtractedInstances<AssetId<M>>>()
.init_resource::<RenderMaterialInstances<M>>()
.add_render_command::<Shadow, DrawPrepass<M>>()
.add_render_command::<Transmissive3d, DrawMaterial<M>>()
.add_render_command::<Transparent3d, DrawMaterial<M>>()
Expand Down Expand Up @@ -490,7 +490,15 @@ impl<P: PhaseItem, M: Material, const I: usize> RenderCommand<P> for SetMaterial
}
}

pub type RenderMaterialInstances<M> = ExtractedInstances<AssetId<M>>;
/// Stores all extracted instances of a [`Material`] in the render world.
#[derive(Resource, Deref, DerefMut)]
pub struct RenderMaterialInstances<M: Material>(pub EntityHashMap<AssetId<M>>);

impl<M: Material> Default for RenderMaterialInstances<M> {
fn default() -> Self {
Self(Default::default())
}
}

pub const fn alpha_mode_pipeline_key(alpha_mode: AlphaMode, msaa: &Msaa) -> MeshPipelineKey {
match alpha_mode {
Expand Down
13 changes: 0 additions & 13 deletions crates/bevy_render/src/extract_component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@ use crate::{
Extract, ExtractSchedule, Render, RenderApp, RenderSet,
};
use bevy_app::{App, Plugin};
use bevy_asset::{Asset, Handle};
use bevy_ecs::{
component::Component,
prelude::*,
query::{QueryFilter, QueryItem, ReadOnlyQueryData},
system::lifetimeless::Read,
};
use core::{marker::PhantomData, ops::Deref};

Expand Down Expand Up @@ -197,17 +195,6 @@ impl<C: ExtractComponent> Plugin for ExtractComponentPlugin<C> {
}
}

impl<T: Asset> ExtractComponent for Handle<T> {
type QueryData = Read<Handle<T>>;
type QueryFilter = ();
type Out = Handle<T>;

#[inline]
fn extract_component(handle: QueryItem<'_, Self::QueryData>) -> Option<Self::Out> {
Some(handle.clone_weak())
}
}

/// This system extracts all components of the corresponding [`ExtractComponent`], for entities that are synced via [`crate::sync_world::SyncToRenderWorld`].
fn extract_components<C: ExtractComponent>(
mut commands: Commands,
Expand Down
15 changes: 1 addition & 14 deletions crates/bevy_render/src/extract_instances.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@
use core::marker::PhantomData;

use bevy_app::{App, Plugin};
use bevy_asset::{Asset, AssetId, Handle};
use bevy_derive::{Deref, DerefMut};
use bevy_ecs::{
entity::EntityHashMap,
prelude::Entity,
query::{QueryFilter, QueryItem, ReadOnlyQueryData},
system::{lifetimeless::Read, Query, ResMut, Resource},
system::{Query, ResMut, Resource},
};

use crate::{prelude::ViewVisibility, Extract, ExtractSchedule, RenderApp};
Expand Down Expand Up @@ -134,15 +133,3 @@ fn extract_visible<EI>(
}
}
}

impl<A> ExtractInstance for AssetId<A>
where
A: Asset,
{
type QueryData = Read<Handle<A>>;
type QueryFilter = ();

fn extract(item: QueryItem<'_, Self::QueryData>) -> Option<Self> {
Some(item.id())
}
}

0 comments on commit e19c53e

Please sign in to comment.