Skip to content

Commit dc22c54

Browse files
committed
cleanup
1 parent 792fc38 commit dc22c54

File tree

7 files changed

+64
-63
lines changed

7 files changed

+64
-63
lines changed

crates/bevy_app/src/app.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ pub struct App {
6767
/// the application's event loop and advancing the [`Schedule`].
6868
/// Typically, it is not configured manually, but set by one of Bevy's built-in plugins.
6969
/// See `bevy::winit::WinitPlugin` and [`ScheduleRunnerPlugin`](crate::schedule_runner::ScheduleRunnerPlugin).
70-
pub runner: Box<dyn Fn(App) + Send + Sync>, // send/sync bound is only required to make App Send/Sync
70+
pub runner: Box<dyn Fn(App) + Send + Sync>, // Send + Sync bound is only required to make App Send + Sync
7171
/// A container of [`Stage`]s set to be run in a linear order.
7272
pub schedule: Schedule,
7373
sub_apps: HashMap<AppLabelId, SubApp>,
@@ -89,7 +89,7 @@ impl Debug for App {
8989
pub struct SubApp {
9090
app: App,
9191
extract: Box<dyn Fn(&mut World, &mut App) + Send + Sync>, // Send + Sync bound is only required to make SubApp send sync
92-
runner: Box<dyn Fn(&mut App) + Send + Sync>, // this send sync bound is required since we're actually sending this function to another thread
92+
runner: Box<dyn Fn(&mut App) + Send + Sync>, // this Send + Sync bound is required since we're running this function on another thread
9393
}
9494

9595
impl SubApp {
@@ -161,7 +161,7 @@ impl App {
161161
/// See [`add_sub_app`](Self::add_sub_app) and [`run_once`](Schedule::run_once) for more details.
162162
pub fn update(&mut self) {
163163
#[cfg(feature = "trace")]
164-
let _bevy_frame_update_span = info_span!("main_app").entered();
164+
let _bevy_frame_update_span = info_span!("main app").entered();
165165
self.schedule.run(&mut self.world);
166166
for sub_app in self.sub_apps.values_mut() {
167167
sub_app.extract(&mut self.world);
@@ -997,15 +997,15 @@ impl App {
997997

998998
/// Adds an [`App`] as a child of the current one.
999999
///
1000-
/// The provided function `f` is called by the [`update`](Self::update) method. The [`World`]
1000+
/// The provided functions `extract` and `runner` are normally called by the [`update`](Self::update) method. The [`World`]
10011001
/// parameter represents the main app world, while the [`App`] parameter is just a mutable
10021002
/// reference to the `SubApp` itself.
10031003
pub fn add_sub_app(
10041004
&mut self,
10051005
label: impl AppLabel,
10061006
mut app: App,
1007-
sub_app_extract: impl Fn(&mut World, &mut App) + 'static + Send + Sync,
1008-
sub_app_runner: impl Fn(&mut App) + 'static + Send + Sync,
1007+
extract: impl Fn(&mut World, &mut App) + 'static + Send + Sync,
1008+
runner: impl Fn(&mut App) + 'static + Send + Sync,
10091009
) -> &mut Self {
10101010
if let Some(executor) = self.world.get_resource::<MainThreadExecutor>() {
10111011
app.world.insert_resource(executor.clone());
@@ -1014,8 +1014,8 @@ impl App {
10141014
label.as_label(),
10151015
SubApp {
10161016
app,
1017-
extract: Box::new(sub_app_extract),
1018-
runner: Box::new(sub_app_runner),
1017+
extract: Box::new(extract),
1018+
runner: Box::new(runner),
10191019
},
10201020
);
10211021
self
@@ -1055,12 +1055,12 @@ impl App {
10551055
}
10561056
}
10571057

1058-
/// inserts an existing sub app into the app
1058+
/// Inserts an existing sub app into the app
10591059
pub fn insert_sub_app(&mut self, label: impl AppLabel, sub_app: SubApp) {
10601060
self.sub_apps.insert(label.as_label(), sub_app);
10611061
}
10621062

1063-
/// remove a sub app from the app
1063+
/// Removes a sub app from the app. Returns None if the label doesn't exist.
10641064
pub fn remove_sub_app(&mut self, label: impl AppLabel) -> Option<SubApp> {
10651065
self.sub_apps.remove(&label.as_label())
10661066
}

crates/bevy_ecs/src/schedule/executor_parallel.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use fixedbitset::FixedBitSet;
1818
#[cfg(test)]
1919
use scheduling_event::*;
2020

21-
///
21+
/// Newtyped [`ThreadExecutor`] [`Resource`] that is used to run systems on the main thread
2222
#[derive(Resource, Default)]
2323
pub struct MainThreadExecutor(pub Arc<ThreadExecutor>);
2424

crates/bevy_render/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,4 @@ basis-universal = { version = "0.2.0", optional = true }
7676
encase = { version = "0.4", features = ["glam"] }
7777
# For wgpu profiling using tracing. Use `RUST_LOG=info` to also capture the wgpu spans.
7878
profiling = { version = "1", features = ["profile-with-tracing"], optional = true }
79-
async-channel = "1.4"
79+
async-channel = "1.4"

crates/bevy_render/src/lib.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ use std::{
6363

6464
/// Contains the default Bevy rendering backend based on wgpu.
6565
pub struct RenderPlugin {
66+
/// Pipelined rendering runs the rendering simultaneously with the main app.
67+
/// Use this to turn pipelined rendering on or off. By default it's on in native
68+
/// environments and off in wasm.
6669
pub use_pipelined_rendering: bool,
6770
}
6871

@@ -237,7 +240,7 @@ impl Plugin for RenderPlugin {
237240

238241
app.add_sub_app(RenderApp, render_app, move |app_world, render_app| {
239242
#[cfg(feature = "trace")]
240-
let _render_span = bevy_utils::tracing::info_span!("extract").entered();
243+
let _render_span = bevy_utils::tracing::info_span!("extract main to render app").entered();
241244
{
242245
#[cfg(feature = "trace")]
243246
let _stage_span =
@@ -272,6 +275,8 @@ impl Plugin for RenderPlugin {
272275
extract(app_world, render_app);
273276
}
274277
}, |render_app| {
278+
#[cfg(feature = "trace")]
279+
let _render_span = bevy_utils::tracing::info_span!("render app").entered();
275280
{
276281
#[cfg(feature = "trace")]
277282
let _stage_span =
+32-38
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use async_channel::{Receiver, Sender};
2+
23
use bevy_app::{App, SubApp};
34
use bevy_ecs::{
45
schedule::MainThreadExecutor,
@@ -7,22 +8,19 @@ use bevy_ecs::{
78
};
89
use bevy_tasks::ComputeTaskPool;
910

10-
#[cfg(feature = "trace")]
11-
use bevy_utils::tracing::Instrument;
12-
1311
use crate::{PipelinedRenderingApp, RenderApp};
1412

15-
/// Resource to be used for pipelined rendering for sending the render app from the main thread to the rendering thread
13+
/// Resource for pipelined rendering to send the render app from the main thread to the rendering thread
1614
#[derive(Resource)]
1715
pub struct MainToRenderAppSender(pub Sender<SubApp>);
1816

19-
/// Resource used by pipelined rendering to send the render app from the render thread to the main thread
17+
/// Resource for pipelined rendering to send the render app from the render thread to the main thread
2018
#[derive(Resource)]
2119
pub struct RenderToMainAppReceiver(pub Receiver<SubApp>);
2220

23-
/// sets up the render thread and insert resource into the main app for controlling the render thread
21+
/// Sets up the render thread and inserts resources into the main app used for controlling the render thread
22+
/// This does nothing if pipelined rendering is not enabled.
2423
pub fn setup_rendering(app: &mut App) {
25-
// skip this if pipelined rendering is not enabled
2624
if app.get_sub_app(PipelinedRenderingApp).is_err() {
2725
return;
2826
}
@@ -36,42 +34,38 @@ pub fn setup_rendering(app: &mut App) {
3634
app.insert_resource(MainToRenderAppSender(app_to_render_sender));
3735
app.insert_resource(RenderToMainAppReceiver(render_to_app_receiver));
3836

39-
let render_task = async move {
40-
loop {
41-
// TODO: exit loop when app is exited
42-
let recv_task = app_to_render_receiver.recv();
43-
let mut sub_app = recv_task.await.unwrap();
44-
sub_app.run();
45-
render_to_app_sender.send(sub_app).await.unwrap();
46-
}
47-
};
48-
#[cfg(feature = "trace")]
49-
let span = bevy_utils::tracing::info_span!("render app");
50-
#[cfg(feature = "trace")]
51-
let render_task = render_task.instrument(span);
52-
ComputeTaskPool::get().spawn(render_task).detach();
37+
ComputeTaskPool::get()
38+
.spawn(async move {
39+
loop {
40+
// TODO: exit loop when app is exited
41+
let recv_task = app_to_render_receiver.recv();
42+
let mut sub_app = recv_task.await.unwrap();
43+
sub_app.run();
44+
render_to_app_sender.send(sub_app).await.unwrap();
45+
}
46+
})
47+
.detach();
5348
}
5449

50+
/// This function is used for synchronizing the main app with the render world.
51+
/// Do not call this function if pipelined rendering is not setup.
5552
pub fn update_rendering(app_world: &mut World) {
56-
// wait to get the render app back to signal that rendering is finished
57-
let mut render_app = app_world
58-
.resource_scope(|world, main_thread_executor: Mut<MainThreadExecutor>| {
59-
ComputeTaskPool::get()
60-
.scope(Some(main_thread_executor.0.clone()), |s| {
61-
s.spawn(async {
62-
let receiver = world.get_resource::<RenderToMainAppReceiver>().unwrap();
63-
let recv = receiver.0.recv();
64-
recv.await.unwrap()
65-
});
66-
})
67-
.pop()
68-
})
69-
.unwrap();
53+
app_world.resource_scope(|world, main_thread_executor: Mut<MainThreadExecutor>| {
54+
// we use a scope here to run any main thread tasks that the render world still needs to run
55+
// while we wait for the render world to be received.
56+
let mut render_app = ComputeTaskPool::get()
57+
.scope(Some(main_thread_executor.0.clone()), |s| {
58+
s.spawn(async {
59+
let receiver = world.get_resource::<RenderToMainAppReceiver>().unwrap();
60+
receiver.0.recv().await.unwrap()
61+
});
62+
})
63+
.pop()
64+
.unwrap();
7065

71-
render_app.extract(app_world);
66+
render_app.extract(world);
7267

73-
app_world.resource_scope(|_world, sender: Mut<MainToRenderAppSender>| {
68+
let sender = world.get_resource::<MainToRenderAppSender>().unwrap();
7469
sender.0.send_blocking(render_app).unwrap();
7570
});
76-
// frame pacing plugin should run here somehow. i.e. after rendering, but before input handling
7771
}

crates/bevy_tasks/src/main_thread_executor.rs

+10-8
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,17 @@ impl ThreadExecutor {
3131

3232
/// Gets the `[MainThreadSpawner]` for the thread executor.
3333
/// Use this to spawn tasks that run on the thread this was instatiated on.
34-
pub fn spawner(&self) -> MainThreadSpawner<'static> {
35-
MainThreadSpawner(self.executor.clone())
34+
pub fn spawner(&self) -> ThreadSpawner<'static> {
35+
ThreadSpawner(self.executor.clone())
3636
}
3737

3838
/// Gets the `[MainThreadTicker]` for this executor.
3939
/// Use this to tick the executor.
4040
/// It only returns the ticker if it's on the thread the executor was created on
4141
/// and returns `None` otherwise.
42-
pub fn ticker(&self) -> Option<MainThreadTicker> {
42+
pub fn ticker(&self) -> Option<ThreadTicker> {
4343
if thread::current().id() == self.thread_id {
44-
return Some(MainThreadTicker {
44+
return Some(ThreadTicker {
4545
executor: self.executor.clone(),
4646
_marker: PhantomData::default(),
4747
});
@@ -50,22 +50,24 @@ impl ThreadExecutor {
5050
}
5151
}
5252

53+
/// Used to spawn on the [`ThreadExecutor`]
5354
#[derive(Debug)]
54-
pub struct MainThreadSpawner<'a>(Arc<Executor<'a>>);
55-
impl<'a> MainThreadSpawner<'a> {
55+
pub struct ThreadSpawner<'a>(Arc<Executor<'a>>);
56+
impl<'a> ThreadSpawner<'a> {
5657
/// Spawn a task on the main thread
5758
pub fn spawn<T: Send + 'a>(&self, future: impl Future<Output = T> + Send + 'a) -> Task<T> {
5859
self.0.spawn(future)
5960
}
6061
}
6162

63+
/// Used to tick the [`ThreadExecutor`]
6264
#[derive(Debug)]
63-
pub struct MainThreadTicker {
65+
pub struct ThreadTicker {
6466
executor: Arc<Executor<'static>>,
6567
// make type not send or sync
6668
_marker: PhantomData<*const ()>,
6769
}
68-
impl MainThreadTicker {
70+
impl ThreadTicker {
6971
/// Tick the main thread executor.
7072
/// This needs to be called manually on the thread if it is not being used with
7173
/// a `[TaskPool::scope]`.

crates/bevy_tasks/src/task_pool.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use concurrent_queue::ConcurrentQueue;
1010
use futures_lite::{future, FutureExt};
1111

1212
use crate::Task;
13-
use crate::{main_thread_executor::MainThreadSpawner, ThreadExecutor};
13+
use crate::{main_thread_executor::ThreadSpawner, ThreadExecutor};
1414

1515
/// Used to create a [`TaskPool`]
1616
#[derive(Debug, Default, Clone)]
@@ -157,7 +157,7 @@ impl TaskPool {
157157
///
158158
/// This is similar to `rayon::scope` and `crossbeam::scope`
159159
///
160-
/// The `thread_executor` optional parameter can be used to pass a `[ThreadExecutor]` to
160+
/// The `thread_executor` optional parameter can be used to pass a [`ThreadExecutor`] to
161161
/// spawn tasks on when calling `spawn_on_scope`. This can be useful for spawning tasks that
162162
/// must run on the main thread. If `None` is passed then `spawn_on_scope` runs tasks on
163163
/// the thread `scope` is run on.
@@ -257,7 +257,7 @@ impl TaskPool {
257257
Arc::new(ThreadExecutor::new())
258258
};
259259
let thread_spawner = thread_executor.spawner();
260-
let thread_spawner: MainThreadSpawner<'env> = unsafe { mem::transmute(thread_spawner) };
260+
let thread_spawner: ThreadSpawner<'env> = unsafe { mem::transmute(thread_spawner) };
261261
let spawned: ConcurrentQueue<async_executor::Task<T>> = ConcurrentQueue::unbounded();
262262
let spawned_ref: &'env ConcurrentQueue<async_executor::Task<T>> =
263263
unsafe { mem::transmute(&spawned) };
@@ -371,7 +371,7 @@ impl Drop for TaskPool {
371371
#[derive(Debug)]
372372
pub struct Scope<'scope, 'env: 'scope, T> {
373373
executor: &'scope async_executor::Executor<'scope>,
374-
thread_spawner: MainThreadSpawner<'scope>,
374+
thread_spawner: ThreadSpawner<'scope>,
375375
spawned: &'scope ConcurrentQueue<async_executor::Task<T>>,
376376
// make `Scope` invariant over 'scope and 'env
377377
scope: PhantomData<&'scope mut &'scope ()>,

0 commit comments

Comments
 (0)