diff --git a/crates/bevy_gizmos/src/lib.rs b/crates/bevy_gizmos/src/lib.rs index f9512bc7ab..4eeeea508d 100755 --- a/crates/bevy_gizmos/src/lib.rs +++ b/crates/bevy_gizmos/src/lib.rs @@ -124,7 +124,7 @@ use { }, renderer::RenderDevice, sync_world::{MainEntity, TemporaryRenderEntity}, - Extract, ExtractSchedule, Render, RenderApp, RenderSystems, + Extract, ExtractSchedule, Render, RenderApp, RenderStartup, RenderSystems, }, bytemuck::cast_slice, }; @@ -176,6 +176,8 @@ impl Plugin for GizmoPlugin { #[cfg(feature = "bevy_render")] if let Some(render_app) = app.get_sub_app_mut(RenderApp) { + render_app.add_systems(RenderStartup, init_line_gizmo_uniform_bind_group_layout); + render_app.add_systems( Render, prepare_line_gizmo_bind_group.in_set(RenderSystems::PrepareBindGroups), @@ -199,26 +201,6 @@ impl Plugin for GizmoPlugin { tracing::warn!("bevy_render feature is enabled but RenderApp was not detected. Are you sure you loaded GizmoPlugin after RenderPlugin?"); } } - - #[cfg(feature = "bevy_render")] - fn finish(&self, app: &mut App) { - let Some(render_app) = app.get_sub_app_mut(RenderApp) else { - return; - }; - - let render_device = render_app.world().resource::(); - let line_layout = render_device.create_bind_group_layout( - "LineGizmoUniform layout", - &BindGroupLayoutEntries::single( - ShaderStages::VERTEX, - uniform_buffer::(true), - ), - ); - - render_app.insert_resource(LineGizmoUniformBindgroupLayout { - layout: line_layout, - }); - } } /// A extension trait adding `App::init_gizmo_group` and `App::insert_gizmo_config`. @@ -415,6 +397,24 @@ fn update_gizmo_meshes( } } +#[cfg(feature = "bevy_render")] +fn init_line_gizmo_uniform_bind_group_layout( + mut commands: Commands, + render_device: Res, +) { + let line_layout = render_device.create_bind_group_layout( + "LineGizmoUniform layout", + &BindGroupLayoutEntries::single( + ShaderStages::VERTEX, + uniform_buffer::(true), + ), + ); + + commands.insert_resource(LineGizmoUniformBindgroupLayout { + layout: line_layout, + }); +} + #[cfg(feature = "bevy_render")] fn extract_gizmo_data( mut commands: Commands, diff --git a/crates/bevy_gizmos/src/pipeline_2d.rs b/crates/bevy_gizmos/src/pipeline_2d.rs index 15857022f3..128ecca883 100644 --- a/crates/bevy_gizmos/src/pipeline_2d.rs +++ b/crates/bevy_gizmos/src/pipeline_2d.rs @@ -1,23 +1,21 @@ use crate::{ config::{GizmoLineJoint, GizmoLineStyle, GizmoMeshConfig}, - line_gizmo_vertex_buffer_layouts, line_joint_gizmo_vertex_buffer_layouts, DrawLineGizmo, - DrawLineJointGizmo, GizmoRenderSystems, GpuLineGizmo, LineGizmoUniformBindgroupLayout, - SetLineGizmoBindGroup, + init_line_gizmo_uniform_bind_group_layout, line_gizmo_vertex_buffer_layouts, + line_joint_gizmo_vertex_buffer_layouts, DrawLineGizmo, DrawLineJointGizmo, GizmoRenderSystems, + GpuLineGizmo, LineGizmoUniformBindgroupLayout, SetLineGizmoBindGroup, }; use bevy_app::{App, Plugin}; -use bevy_asset::{load_embedded_asset, Handle}; +use bevy_asset::{load_embedded_asset, AssetServer, Handle}; use bevy_core_pipeline::core_2d::{Transparent2d, CORE_2D_DEPTH_FORMAT}; use bevy_ecs::{ prelude::Entity, resource::Resource, schedule::IntoScheduleConfigs, - system::{Query, Res, ResMut}, - world::{FromWorld, World}, + system::{Commands, Query, Res, ResMut}, }; use bevy_image::BevyDefault as _; use bevy_math::FloatOrd; -use bevy_render::sync_world::MainEntity; use bevy_render::{ render_asset::{prepare_assets, RenderAssets}, render_phase::{ @@ -28,6 +26,7 @@ use bevy_render::{ view::{ExtractedView, Msaa, RenderLayers, ViewTarget}, Render, RenderApp, RenderSystems, }; +use bevy_render::{sync_world::MainEntity, RenderStartup}; use bevy_sprite::{Mesh2dPipeline, Mesh2dPipelineKey, SetMesh2dViewBindGroup}; use bevy_utils::default; use tracing::error; @@ -55,6 +54,10 @@ impl Plugin for LineGizmo2dPlugin { bevy_sprite::queue_material2d_meshes::, ), ) + .add_systems( + RenderStartup, + init_line_gizmo_pipelines.after(init_line_gizmo_uniform_bind_group_layout), + ) .add_systems( Render, (queue_line_gizmos_2d, queue_line_joint_gizmos_2d) @@ -62,15 +65,6 @@ impl Plugin for LineGizmo2dPlugin { .after(prepare_assets::), ); } - - fn finish(&self, app: &mut App) { - let Some(render_app) = app.get_sub_app_mut(RenderApp) else { - return; - }; - - render_app.init_resource::(); - render_app.init_resource::(); - } } #[derive(Clone, Resource)] @@ -80,17 +74,22 @@ struct LineGizmoPipeline { shader: Handle, } -impl FromWorld for LineGizmoPipeline { - fn from_world(render_world: &mut World) -> Self { - LineGizmoPipeline { - mesh_pipeline: render_world.resource::().clone(), - uniform_layout: render_world - .resource::() - .layout - .clone(), - shader: load_embedded_asset!(render_world, "lines.wgsl"), - } - } +fn init_line_gizmo_pipelines( + mut commands: Commands, + mesh_2d_pipeline: Res, + uniform_bind_group_layout: Res, + asset_server: Res, +) { + commands.insert_resource(LineGizmoPipeline { + mesh_pipeline: mesh_2d_pipeline.clone(), + uniform_layout: uniform_bind_group_layout.layout.clone(), + shader: load_embedded_asset!(asset_server.as_ref(), "lines.wgsl"), + }); + commands.insert_resource(LineJointGizmoPipeline { + mesh_pipeline: mesh_2d_pipeline.clone(), + uniform_layout: uniform_bind_group_layout.layout.clone(), + shader: load_embedded_asset!(asset_server.as_ref(), "line_joints.wgsl"), + }); } #[derive(PartialEq, Eq, Hash, Clone)] @@ -178,19 +177,6 @@ struct LineJointGizmoPipeline { shader: Handle, } -impl FromWorld for LineJointGizmoPipeline { - fn from_world(render_world: &mut World) -> Self { - LineJointGizmoPipeline { - mesh_pipeline: render_world.resource::().clone(), - uniform_layout: render_world - .resource::() - .layout - .clone(), - shader: load_embedded_asset!(render_world, "line_joints.wgsl"), - } - } -} - #[derive(PartialEq, Eq, Hash, Clone)] struct LineJointGizmoPipelineKey { mesh_key: Mesh2dPipelineKey, diff --git a/crates/bevy_gizmos/src/pipeline_3d.rs b/crates/bevy_gizmos/src/pipeline_3d.rs index ddf01994a9..66f2050e55 100644 --- a/crates/bevy_gizmos/src/pipeline_3d.rs +++ b/crates/bevy_gizmos/src/pipeline_3d.rs @@ -1,11 +1,11 @@ use crate::{ config::{GizmoLineJoint, GizmoLineStyle, GizmoMeshConfig}, - line_gizmo_vertex_buffer_layouts, line_joint_gizmo_vertex_buffer_layouts, DrawLineGizmo, - DrawLineJointGizmo, GizmoRenderSystems, GpuLineGizmo, LineGizmoUniformBindgroupLayout, - SetLineGizmoBindGroup, + init_line_gizmo_uniform_bind_group_layout, line_gizmo_vertex_buffer_layouts, + line_joint_gizmo_vertex_buffer_layouts, DrawLineGizmo, DrawLineJointGizmo, GizmoRenderSystems, + GpuLineGizmo, LineGizmoUniformBindgroupLayout, SetLineGizmoBindGroup, }; use bevy_app::{App, Plugin}; -use bevy_asset::{load_embedded_asset, Handle}; +use bevy_asset::{load_embedded_asset, AssetServer, Handle}; use bevy_core_pipeline::{ core_3d::{Transparent3d, CORE_3D_DEPTH_FORMAT}, oit::OrderIndependentTransparencySettings, @@ -17,12 +17,10 @@ use bevy_ecs::{ query::Has, resource::Resource, schedule::IntoScheduleConfigs, - system::{Query, Res, ResMut}, - world::{FromWorld, World}, + system::{Commands, Query, Res, ResMut}, }; use bevy_image::BevyDefault as _; use bevy_pbr::{MeshPipeline, MeshPipelineKey, SetMeshViewBindGroup}; -use bevy_render::sync_world::MainEntity; use bevy_render::{ render_asset::{prepare_assets, RenderAssets}, render_phase::{ @@ -33,6 +31,7 @@ use bevy_render::{ view::{ExtractedView, Msaa, RenderLayers, ViewTarget}, Render, RenderApp, RenderSystems, }; +use bevy_render::{sync_world::MainEntity, RenderStartup}; use bevy_utils::default; use tracing::error; @@ -53,6 +52,10 @@ impl Plugin for LineGizmo3dPlugin { Render, GizmoRenderSystems::QueueLineGizmos3d.in_set(RenderSystems::Queue), ) + .add_systems( + RenderStartup, + init_line_gizmo_pipelines.after(init_line_gizmo_uniform_bind_group_layout), + ) .add_systems( Render, (queue_line_gizmos_3d, queue_line_joint_gizmos_3d) @@ -60,15 +63,6 @@ impl Plugin for LineGizmo3dPlugin { .after(prepare_assets::), ); } - - fn finish(&self, app: &mut App) { - let Some(render_app) = app.get_sub_app_mut(RenderApp) else { - return; - }; - - render_app.init_resource::(); - render_app.init_resource::(); - } } #[derive(Clone, Resource)] @@ -78,17 +72,22 @@ struct LineGizmoPipeline { shader: Handle, } -impl FromWorld for LineGizmoPipeline { - fn from_world(render_world: &mut World) -> Self { - LineGizmoPipeline { - mesh_pipeline: render_world.resource::().clone(), - uniform_layout: render_world - .resource::() - .layout - .clone(), - shader: load_embedded_asset!(render_world, "lines.wgsl"), - } - } +fn init_line_gizmo_pipelines( + mut commands: Commands, + mesh_pipeline: Res, + uniform_bind_group_layout: Res, + asset_server: Res, +) { + commands.insert_resource(LineGizmoPipeline { + mesh_pipeline: mesh_pipeline.clone(), + uniform_layout: uniform_bind_group_layout.layout.clone(), + shader: load_embedded_asset!(asset_server.as_ref(), "lines.wgsl"), + }); + commands.insert_resource(LineJointGizmoPipeline { + mesh_pipeline: mesh_pipeline.clone(), + uniform_layout: uniform_bind_group_layout.layout.clone(), + shader: load_embedded_asset!(asset_server.as_ref(), "line_joints.wgsl"), + }); } #[derive(PartialEq, Eq, Hash, Clone)] @@ -173,19 +172,6 @@ struct LineJointGizmoPipeline { shader: Handle, } -impl FromWorld for LineJointGizmoPipeline { - fn from_world(render_world: &mut World) -> Self { - LineJointGizmoPipeline { - mesh_pipeline: render_world.resource::().clone(), - uniform_layout: render_world - .resource::() - .layout - .clone(), - shader: load_embedded_asset!(render_world, "line_joints.wgsl"), - } - } -} - #[derive(PartialEq, Eq, Hash, Clone)] struct LineJointGizmoPipelineKey { view_key: MeshPipelineKey,