Skip to content

Commit

Permalink
Converted exclusive systems to parallel systems wherever possible (be…
Browse files Browse the repository at this point in the history
…vyengine#2774)

Closes bevyengine#2767.

Converted:
- `play_queued_audio_system`
- `change_window`
  • Loading branch information
SarthakSingh31 authored and exjam committed May 22, 2022
1 parent 92aea3c commit 2e0c4ae
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 34 deletions.
19 changes: 8 additions & 11 deletions crates/bevy_audio/src/audio_output.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{Audio, AudioSource, Decodable};
use bevy_asset::{Asset, Assets};
use bevy_ecs::world::World;
use bevy_ecs::system::{NonSend, Res, ResMut};
use bevy_reflect::TypeUuid;
use bevy_utils::tracing::warn;
use rodio::{OutputStream, OutputStreamHandle, Sink, Source};
Expand Down Expand Up @@ -85,16 +85,13 @@ where
}

/// Plays audio currently queued in the [`Audio`] resource through the [`AudioOutput`] resource
pub fn play_queued_audio_system<Source: Asset>(world: &mut World)
where
Source: Decodable,
{
let world = world.cell();
let audio_output = world.get_non_send::<AudioOutput<Source>>().unwrap();
let mut audio = world.get_resource_mut::<Audio<Source>>().unwrap();
let mut sinks = world.get_resource_mut::<Assets<AudioSink>>().unwrap();

if let Some(audio_sources) = world.get_resource::<Assets<Source>>() {
pub fn play_queued_audio_system<Source: Asset + Decodable>(
audio_output: NonSend<AudioOutput<Source>>,
audio_sources: Option<Res<Assets<Source>>>,
mut audio: ResMut<Audio<Source>>,
mut sinks: ResMut<Assets<AudioSink>>,
) {
if let Some(audio_sources) = audio_sources {
audio_output.try_play_queued(&*audio_sources, &mut *audio, &mut *sinks);
};
}
Expand Down
3 changes: 1 addition & 2 deletions crates/bevy_audio/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ pub use audio_source::*;

use bevy_app::prelude::*;
use bevy_asset::AddAsset;
use bevy_ecs::system::IntoExclusiveSystem;

/// Adds support for audio playback to a Bevy Application
///
Expand All @@ -60,7 +59,7 @@ impl Plugin for AudioPlugin {
.init_resource::<Audio<AudioSource>>()
.add_system_to_stage(
CoreStage::PostUpdate,
play_queued_audio_system::<AudioSource>.exclusive_system(),
play_queued_audio_system::<AudioSource>,
);

#[cfg(any(feature = "mp3", feature = "flac", feature = "wav", feature = "vorbis"))]
Expand Down
5 changes: 4 additions & 1 deletion crates/bevy_pbr/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ pub use material::*;
pub use pbr_material::*;
pub use render::*;

use bevy_window::ModifiesWindows;

pub mod prelude {
#[doc(hidden)]
pub use crate::{
Expand Down Expand Up @@ -86,7 +88,8 @@ impl Plugin for PbrPlugin {
CoreStage::PostUpdate,
assign_lights_to_clusters
.label(SimulationLightSystems::AssignLightsToClusters)
.after(TransformSystem::TransformPropagate),
.after(TransformSystem::TransformPropagate)
.after(ModifiesWindows),
)
.add_system_to_stage(
CoreStage::PostUpdate,
Expand Down
6 changes: 4 additions & 2 deletions crates/bevy_render/src/camera/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ use crate::{
view::{ComputedVisibility, Visibility, VisibleEntities},
};
use bevy_app::{App, CoreStage, Plugin};
use bevy_ecs::schedule::ParallelSystemDescriptorCoercion;
use bevy_window::ModifiesWindows;

#[derive(Default)]
pub struct CameraPlugin;
Expand All @@ -32,11 +34,11 @@ impl Plugin for CameraPlugin {
.register_type::<Camera2d>()
.add_system_to_stage(
CoreStage::PostUpdate,
crate::camera::camera_system::<OrthographicProjection>,
crate::camera::camera_system::<OrthographicProjection>.after(ModifiesWindows),
)
.add_system_to_stage(
CoreStage::PostUpdate,
crate::camera::camera_system::<PerspectiveProjection>,
crate::camera::camera_system::<PerspectiveProjection>.after(ModifiesWindows),
)
.add_plugin(CameraTypePlugin::<Camera3d>::default())
.add_plugin(CameraTypePlugin::<Camera2d>::default());
Expand Down
3 changes: 2 additions & 1 deletion crates/bevy_text/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ use bevy_asset::AddAsset;
use bevy_ecs::{entity::Entity, schedule::ParallelSystemDescriptorCoercion};
use bevy_render::{RenderApp, RenderStage};
use bevy_sprite::SpriteSystem;
use bevy_window::ModifiesWindows;

pub type DefaultTextPipeline = TextPipeline<Entity>;

Expand All @@ -46,7 +47,7 @@ impl Plugin for TextPlugin {
.register_type::<HorizontalAlign>()
.init_asset_loader::<FontLoader>()
.insert_resource(DefaultTextPipeline::default())
.add_system_to_stage(CoreStage::PostUpdate, text2d_system);
.add_system_to_stage(CoreStage::PostUpdate, text2d_system.after(ModifiesWindows));

if let Ok(render_app) = app.get_sub_app_mut(RenderApp) {
render_app.add_system_to_stage(
Expand Down
8 changes: 6 additions & 2 deletions crates/bevy_ui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ use bevy_ecs::schedule::{ParallelSystemDescriptorCoercion, SystemLabel};
use bevy_input::InputSystem;
use bevy_math::Rect;
use bevy_transform::TransformSystem;
use bevy_window::ModifiesWindows;
use update::{ui_z_system, update_clipping_system};

use crate::prelude::CameraUi;
Expand Down Expand Up @@ -84,7 +85,9 @@ impl Plugin for UiPlugin {
// add these stages to front because these must run before transform update systems
.add_system_to_stage(
CoreStage::PostUpdate,
widget::text_system.before(UiSystem::Flex),
widget::text_system
.before(UiSystem::Flex)
.after(ModifiesWindows),
)
.add_system_to_stage(
CoreStage::PostUpdate,
Expand All @@ -94,7 +97,8 @@ impl Plugin for UiPlugin {
CoreStage::PostUpdate,
flex_node_system
.label(UiSystem::Flex)
.before(TransformSystem::TransformPropagate),
.before(TransformSystem::TransformPropagate)
.after(ModifiesWindows),
)
.add_system_to_stage(
CoreStage::PostUpdate,
Expand Down
5 changes: 4 additions & 1 deletion crates/bevy_window/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub mod prelude {
}

use bevy_app::prelude::*;
use bevy_ecs::event::Events;
use bevy_ecs::{event::Events, schedule::SystemLabel};

pub struct WindowPlugin {
pub add_primary_window: bool,
Expand Down Expand Up @@ -74,3 +74,6 @@ impl Plugin for WindowPlugin {
}
}
}

#[derive(Debug, Hash, PartialEq, Eq, Clone, SystemLabel)]
pub struct ModifiesWindows;
26 changes: 12 additions & 14 deletions crates/bevy_winit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ pub use winit_windows::*;

use bevy_app::{App, AppExit, CoreStage, Plugin};
use bevy_ecs::{
event::{Events, ManualEventReader},
system::IntoExclusiveSystem,
event::{EventWriter, Events, ManualEventReader},
schedule::ParallelSystemDescriptorCoercion,
system::{NonSend, ResMut},
world::World,
};
use bevy_math::{ivec2, DVec2, Vec2};
Expand All @@ -22,9 +23,9 @@ use bevy_utils::{
Instant,
};
use bevy_window::{
CreateWindow, CursorEntered, CursorLeft, CursorMoved, FileDragAndDrop, ReceivedCharacter,
RequestRedraw, WindowBackendScaleFactorChanged, WindowCloseRequested, WindowCreated,
WindowFocused, WindowMoved, WindowResized, WindowScaleFactorChanged, Windows,
CreateWindow, CursorEntered, CursorLeft, CursorMoved, FileDragAndDrop, ModifiesWindows,
ReceivedCharacter, RequestRedraw, WindowBackendScaleFactorChanged, WindowCloseRequested,
WindowCreated, WindowFocused, WindowMoved, WindowResized, WindowScaleFactorChanged, Windows,
};
use winit::{
dpi::PhysicalPosition,
Expand All @@ -42,18 +43,18 @@ impl Plugin for WinitPlugin {
app.init_non_send_resource::<WinitWindows>()
.init_resource::<WinitSettings>()
.set_runner(winit_runner)
.add_system_to_stage(CoreStage::PostUpdate, change_window.exclusive_system());
.add_system_to_stage(CoreStage::PostUpdate, change_window.label(ModifiesWindows));
let event_loop = EventLoop::new();
handle_initial_window_events(&mut app.world, &event_loop);
app.insert_non_send_resource(event_loop);
}
}

fn change_window(world: &mut World) {
let world = world.cell();
let winit_windows = world.get_non_send::<WinitWindows>().unwrap();
let mut windows = world.get_resource_mut::<Windows>().unwrap();

fn change_window(
winit_windows: NonSend<WinitWindows>,
mut windows: ResMut<Windows>,
mut window_dpi_changed_events: EventWriter<WindowScaleFactorChanged>,
) {
for bevy_window in windows.iter_mut() {
let id = bevy_window.id();
for command in bevy_window.drain_commands() {
Expand Down Expand Up @@ -88,9 +89,6 @@ fn change_window(world: &mut World) {
window.set_title(&title);
}
bevy_window::WindowCommand::SetScaleFactor { scale_factor } => {
let mut window_dpi_changed_events = world
.get_resource_mut::<Events<WindowScaleFactorChanged>>()
.unwrap();
window_dpi_changed_events.send(WindowScaleFactorChanged { id, scale_factor });
}
bevy_window::WindowCommand::SetResolution {
Expand Down

0 comments on commit 2e0c4ae

Please sign in to comment.