diff --git a/crates/bevy_pbr/src/material.rs b/crates/bevy_pbr/src/material.rs index 53aa8d7bc8ac6..4a9a99dc9ab54 100644 --- a/crates/bevy_pbr/src/material.rs +++ b/crates/bevy_pbr/src/material.rs @@ -283,12 +283,7 @@ where .add_render_command::>() .add_render_command::>() .init_resource::>>() - .add_systems( - ExtractSchedule, - extract_mesh_materials:: - .before(extract_meshes_for_cpu_building) - .before(extract_meshes_for_gpu_building), - ) + .add_systems(ExtractSchedule, extract_mesh_materials::) .add_systems( Render, queue_material_meshes:: diff --git a/crates/bevy_pbr/src/render/mesh.rs b/crates/bevy_pbr/src/render/mesh.rs index 35edf83b944f8..347f3efabd308 100644 --- a/crates/bevy_pbr/src/render/mesh.rs +++ b/crates/bevy_pbr/src/render/mesh.rs @@ -28,7 +28,7 @@ use bevy_render::{ camera::Camera, mesh::*, primitives::Aabb, - render_asset::{ExtractAssetsSet, RenderAssets}, + render_asset::RenderAssets, render_phase::{ BinnedRenderPhasePlugin, PhaseItem, PhaseItemExtraIndex, RenderCommand, RenderCommandResult, SortedRenderPhasePlugin, TrackedRenderPass, @@ -44,6 +44,7 @@ use bevy_render::{ }; use bevy_transform::components::GlobalTransform; use bevy_utils::{ + default, hashbrown::hash_map::Entry, tracing::{error, warn}, HashMap, Parallel, @@ -208,8 +209,7 @@ impl Plugin for MeshRenderPlugin { .add_systems( ExtractSchedule, extract_meshes_for_gpu_building - .in_set(ExtractMeshesSet) - .after(ExtractAssetsSet), + .in_set(ExtractMeshesSet), ) .add_systems( Render, @@ -237,9 +237,7 @@ impl Plugin for MeshRenderPlugin { .insert_resource(cpu_batched_instance_buffer) .add_systems( ExtractSchedule, - extract_meshes_for_cpu_building - .in_set(ExtractMeshesSet) - .after(ExtractAssetsSet), + extract_meshes_for_cpu_building.in_set(ExtractMeshesSet), ) .add_systems( Render, @@ -612,7 +610,6 @@ impl RenderMeshInstanceShared { fn from_components( previous_transform: Option<&PreviousGlobalTransform>, mesh: &Mesh3d, - material_bindings_index: MaterialBindingId, not_shadow_caster: bool, no_automatic_batching: bool, ) -> Self { @@ -630,7 +627,8 @@ impl RenderMeshInstanceShared { RenderMeshInstanceShared { mesh_asset_id: mesh.id(), flags: mesh_instance_flags, - material_bindings_index, + // This gets filled in later, during `RenderMeshGpuBuilder::update`. + material_bindings_index: default(), } } @@ -887,13 +885,15 @@ impl RenderMeshInstanceGpuQueue { impl RenderMeshInstanceGpuBuilder { /// Flushes this mesh instance to the [`RenderMeshInstanceGpu`] and /// [`MeshInputUniform`] tables, replacing the existing entry if applicable. + #[allow(clippy::too_many_arguments)] fn update( - self, + mut self, entity: MainEntity, render_mesh_instances: &mut MainEntityHashMap, current_input_buffer: &mut InstanceInputUniformBuffer, previous_input_buffer: &mut InstanceInputUniformBuffer, mesh_allocator: &MeshAllocator, + mesh_material_ids: &RenderMeshMaterialIds, skin_indices: &SkinIndices, ) -> u32 { let first_vertex_index = match mesh_allocator.mesh_vertex_slice(&self.shared.mesh_asset_id) @@ -911,6 +911,10 @@ impl RenderMeshInstanceGpuBuilder { None => u32::MAX, }; + // Look up the material index. + let mesh_material_binding_id = mesh_material_ids.mesh_material_binding_id(entity); + self.shared.material_bindings_index = mesh_material_binding_id; + // Create the mesh input uniform. let mut mesh_input_uniform = MeshInputUniform { world_from_local: self.world_from_local.to_transpose(), @@ -1046,7 +1050,6 @@ pub struct ExtractMeshesSet; pub fn extract_meshes_for_cpu_building( mut render_mesh_instances: ResMut, render_visibility_ranges: Res, - mesh_material_ids: Res, mut render_mesh_instance_queues: Local>>, meshes_query: Extract< Query<( @@ -1084,9 +1087,6 @@ pub fn extract_meshes_for_cpu_building( return; } - let mesh_material_binding_id = - mesh_material_ids.mesh_material_binding_id(MainEntity::from(entity)); - let mut lod_index = None; if visibility_range { lod_index = render_visibility_ranges.lod_index_for_entity(entity.into()); @@ -1103,7 +1103,6 @@ pub fn extract_meshes_for_cpu_building( let shared = RenderMeshInstanceShared::from_components( previous_transform, mesh, - mesh_material_binding_id, not_shadow_caster, no_automatic_batching, ); @@ -1155,7 +1154,6 @@ pub fn extract_meshes_for_cpu_building( pub fn extract_meshes_for_gpu_building( mut render_mesh_instances: ResMut, render_visibility_ranges: Res, - mesh_material_ids: Res, mut render_mesh_instance_queues: ResMut, changed_meshes_query: Extract< Query< @@ -1235,9 +1233,6 @@ pub fn extract_meshes_for_gpu_building( return; } - let mesh_material_binding_id = - mesh_material_ids.mesh_material_binding_id(MainEntity::from(entity)); - let mut lod_index = None; if visibility_range { lod_index = render_visibility_ranges.lod_index_for_entity(entity.into()); @@ -1254,7 +1249,6 @@ pub fn extract_meshes_for_gpu_building( let shared = RenderMeshInstanceShared::from_components( previous_transform, mesh, - mesh_material_binding_id, not_shadow_caster, no_automatic_batching, ); @@ -1337,6 +1331,7 @@ fn set_mesh_motion_vector_flags( /// Creates the [`RenderMeshInstanceGpu`]s and [`MeshInputUniform`]s when GPU /// mesh uniforms are built. +#[allow(clippy::too_many_arguments)] pub fn collect_meshes_for_gpu_building( render_mesh_instances: ResMut, batched_instance_buffers: ResMut< @@ -1345,6 +1340,7 @@ pub fn collect_meshes_for_gpu_building( mut mesh_culling_data_buffer: ResMut, mut render_mesh_instance_queues: ResMut, mesh_allocator: Res, + mesh_material_ids: Res, skin_indices: Res, ) { let RenderMeshInstances::GpuBuilding(ref mut render_mesh_instances) = @@ -1381,6 +1377,7 @@ pub fn collect_meshes_for_gpu_building( current_input_buffer, previous_input_buffer, &mesh_allocator, + &mesh_material_ids, &skin_indices, ); } @@ -1405,6 +1402,7 @@ pub fn collect_meshes_for_gpu_building( current_input_buffer, previous_input_buffer, &mesh_allocator, + &mesh_material_ids, &skin_indices, ); mesh_culling_builder