Skip to content

Commit 9f94f7e

Browse files
committed
Example showing how to use AsyncComputeTaskPool and Tasks (bevyengine#2180)
1 parent bec323e commit 9f94f7e

File tree

3 files changed

+134
-0
lines changed

3 files changed

+134
-0
lines changed

Cargo.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ anyhow = "1.0"
8787
rand = "0.8.0"
8888
ron = "0.6.2"
8989
serde = {version = "1", features = ["derive"]}
90+
# Needed to poll Task examples
91+
futures-lite = "1.11.3"
9092

9193
[[example]]
9294
name = "hello_world"
@@ -232,6 +234,11 @@ path = "examples/asset/custom_asset_io.rs"
232234
name = "hot_asset_reloading"
233235
path = "examples/asset/hot_asset_reloading.rs"
234236

237+
# Async Tasks
238+
[[example]]
239+
name = "async_compute"
240+
path = "examples/async_tasks/async_compute.rs"
241+
235242
# Audio
236243
[[example]]
237244
name = "audio"

examples/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ git checkout v0.4.0
4141
- [3D Rendering](#3d-rendering)
4242
- [Application](#application)
4343
- [Assets](#assets)
44+
- [Async Tasks](#async-tasks)
4445
- [Audio](#audio)
4546
- [Diagnostics](#diagnostics)
4647
- [ECS (Entity Component System)](#ecs-entity-component-system)
@@ -131,6 +132,12 @@ Example | File | Description
131132
`custom_asset_io` | [`asset/custom_asset_io.rs`](./asset/custom_asset_io.rs) | Implements a custom asset io loader
132133
`hot_asset_reloading` | [`asset/hot_asset_reloading.rs`](./asset/hot_asset_reloading.rs) | Demonstrates automatic reloading of assets when modified on disk
133134

135+
## Async Tasks
136+
137+
Example | File | Description
138+
--- | --- | ---
139+
`async_compute` | [`async_tasks/async_compute.rs`](async_tasks/async_compute.rs) | How to use `AsyncComputeTaskPool` to complete longer running tasks
140+
134141
## Audio
135142

136143
Example | File | Description

examples/async_tasks/async_compute.rs

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
use bevy::{
2+
prelude::*,
3+
tasks::{AsyncComputeTaskPool, Task},
4+
};
5+
use futures_lite::future;
6+
use rand::Rng;
7+
use std::time::{Duration, Instant};
8+
9+
/// This example shows how to use the ECS and the AsyncComputeTaskPool
10+
/// to spawn, poll, and complete tasks across systems and system ticks.
11+
fn main() {
12+
App::build()
13+
.insert_resource(Msaa { samples: 4 })
14+
.add_plugins(DefaultPlugins)
15+
.add_startup_system(setup_env.system())
16+
.add_startup_system(add_assets.system())
17+
.add_startup_system(spawn_tasks.system())
18+
.add_system(handle_tasks.system())
19+
.run();
20+
}
21+
22+
// Number of cubes to spawn across the x, y, and z axis
23+
const NUM_CUBES: u32 = 6;
24+
25+
struct BoxMeshHandle(Handle<Mesh>);
26+
struct BoxMaterialHandle(Handle<StandardMaterial>);
27+
28+
/// Startup system which runs only once and generates our Box Mesh
29+
/// and Box Material assets, adds them to their respective Asset
30+
/// Resources, and stores their handles as resources so we can access
31+
/// them later when we're ready to render our Boxes
32+
fn add_assets(
33+
mut commands: Commands,
34+
mut meshes: ResMut<Assets<Mesh>>,
35+
mut materials: ResMut<Assets<StandardMaterial>>,
36+
) {
37+
let box_mesh_handle = meshes.add(Mesh::from(shape::Cube { size: 0.25 }));
38+
commands.insert_resource(BoxMeshHandle(box_mesh_handle));
39+
40+
let box_material_handle = materials.add(Color::rgb(1.0, 0.2, 0.3).into());
41+
commands.insert_resource(BoxMaterialHandle(box_material_handle));
42+
}
43+
44+
/// This system generates tasks simulating computationally intensive
45+
/// work that potentially spans multiple frames/ticks. A separate
46+
/// system, handle_tasks, will poll the spawned tasks on subsequent
47+
/// frames/ticks, and use the results to spawn cubes
48+
fn spawn_tasks(mut commands: Commands, thread_pool: Res<AsyncComputeTaskPool>) {
49+
for x in 0..NUM_CUBES {
50+
for y in 0..NUM_CUBES {
51+
for z in 0..NUM_CUBES {
52+
// Spawn new task on the AsyncComputeTaskPool
53+
let task = thread_pool.spawn(async move {
54+
let mut rng = rand::thread_rng();
55+
let start_time = Instant::now();
56+
let duration = Duration::from_secs_f32(rng.gen_range(0.05..0.2));
57+
while Instant::now() - start_time < duration {
58+
// Spinning for 'duration', simulating doing hard
59+
// compute work generating translation coords!
60+
}
61+
62+
// Such hard work, all done!
63+
Transform::from_translation(Vec3::new(x as f32, y as f32, z as f32))
64+
});
65+
66+
// Spawn new entity and add our new task as a component
67+
commands.spawn().insert(task);
68+
}
69+
}
70+
}
71+
}
72+
73+
/// This system queries for entities that have our Task<Transform> component. It polls the
74+
/// tasks to see if they're complete. If the task is complete it takes the result, adds a
75+
/// new PbrBundle of components to the entity using the result from the task's work, and
76+
/// removes the task component from the entity.
77+
fn handle_tasks(
78+
mut commands: Commands,
79+
mut transform_tasks: Query<(Entity, &mut Task<Transform>)>,
80+
box_mesh_handle: Res<BoxMeshHandle>,
81+
box_material_handle: Res<BoxMaterialHandle>,
82+
) {
83+
for (entity, mut task) in transform_tasks.iter_mut() {
84+
if let Some(transform) = future::block_on(future::poll_once(&mut *task)) {
85+
// Add our new PbrBundle of components to our tagged entity
86+
commands.entity(entity).insert_bundle(PbrBundle {
87+
mesh: box_mesh_handle.0.clone(),
88+
material: box_material_handle.0.clone(),
89+
transform,
90+
..Default::default()
91+
});
92+
93+
// Task is complete, so remove task component from entity
94+
commands.entity(entity).remove::<Task<Transform>>();
95+
}
96+
}
97+
}
98+
99+
/// This system is only used to setup light and camera for the environment
100+
fn setup_env(mut commands: Commands) {
101+
// Used to center camera on spawned cubes
102+
let offset = if NUM_CUBES % 2 == 0 {
103+
(NUM_CUBES / 2) as f32 - 0.5
104+
} else {
105+
(NUM_CUBES / 2) as f32
106+
};
107+
108+
// lights
109+
commands.spawn_bundle(PointLightBundle {
110+
transform: Transform::from_translation(Vec3::new(4.0, 12.0, 15.0)),
111+
..Default::default()
112+
});
113+
114+
// camera
115+
commands.spawn_bundle(PerspectiveCameraBundle {
116+
transform: Transform::from_translation(Vec3::new(offset, offset, 15.0))
117+
.looking_at(Vec3::new(offset, offset, 0.0), Vec3::Y),
118+
..Default::default()
119+
});
120+
}

0 commit comments

Comments
 (0)