diff --git a/crates/bevy_pbr/src/material.rs b/crates/bevy_pbr/src/material.rs index 0b86cd95e1..17603704e6 100644 --- a/crates/bevy_pbr/src/material.rs +++ b/crates/bevy_pbr/src/material.rs @@ -37,6 +37,7 @@ use bevy_render::erased_render_asset::{ use bevy_render::mesh::mark_3d_meshes_as_changed_if_their_assets_changed; use bevy_render::render_asset::{prepare_assets, RenderAssets}; use bevy_render::renderer::RenderQueue; +use bevy_render::RenderStartup; use bevy_render::{ batching::gpu_preprocessing::GpuPreprocessingSupport, extract_resource::ExtractResource, @@ -371,44 +372,40 @@ where } if let Some(render_app) = app.get_sub_app_mut(RenderApp) { - render_app.add_systems( - ExtractSchedule, - ( - extract_mesh_materials::.in_set(MaterialExtractionSystems), - early_sweep_material_instances:: - .after(MaterialExtractionSystems) - .before(late_sweep_material_instances), - extract_entities_needs_specialization::.after(extract_cameras), - ), - ); - } - } - - fn finish(&self, app: &mut App) { - let Some(render_app) = app.get_sub_app_mut(RenderApp) else { - return; - }; - - render_app.world_mut().resource_scope( - |world, mut bind_group_allocators: Mut| { - let render_device = world.resource::(); - bind_group_allocators.insert( - TypeId::of::(), - MaterialBindGroupAllocator::new( - render_device, - M::label(), - material_uses_bindless_resources::(render_device) - .then(|| M::bindless_descriptor()) - .flatten(), - M::bind_group_layout(render_device), - M::bindless_slot_count(), + render_app + .add_systems(RenderStartup, setup_render_app::) + .add_systems( + ExtractSchedule, + ( + extract_mesh_materials::.in_set(MaterialExtractionSystems), + early_sweep_material_instances:: + .after(MaterialExtractionSystems) + .before(late_sweep_material_instances), + extract_entities_needs_specialization::.after(extract_cameras), ), ); - }, - ); + } } } +fn setup_render_app( + render_device: Res, + mut bind_group_allocators: ResMut, +) { + bind_group_allocators.insert( + TypeId::of::(), + MaterialBindGroupAllocator::new( + &render_device, + M::label(), + material_uses_bindless_resources::(&render_device) + .then(|| M::bindless_descriptor()) + .flatten(), + M::bind_group_layout(&render_device), + M::bindless_slot_count(), + ), + ); +} + /// A dummy [`AssetId`] that we use as a placeholder whenever a mesh doesn't /// have a material. ///