Skip to content

Commit 42ffd8d

Browse files
committed
Implement batching for 2D and 3D meshes
1 parent a166b65 commit 42ffd8d

File tree

28 files changed

+851
-194
lines changed

28 files changed

+851
-194
lines changed

crates/bevy_core_pipeline/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,4 @@ bevy_utils = { path = "../bevy_utils", version = "0.12.0-dev" }
3333
serde = { version = "1", features = ["derive"] }
3434
bitflags = "2.3"
3535
radsort = "0.1"
36+
nonmax = "0.5.3"

crates/bevy_core_pipeline/src/core_2d/mod.rs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ pub mod graph {
1919
}
2020
pub const CORE_2D: &str = graph::NAME;
2121

22+
use std::ops::Range;
23+
2224
pub use camera_2d::*;
2325
pub use main_pass_2d_node::*;
2426

@@ -36,6 +38,7 @@ use bevy_render::{
3638
Extract, ExtractSchedule, Render, RenderApp, RenderSet,
3739
};
3840
use bevy_utils::FloatOrd;
41+
use nonmax::NonMaxU32;
3942

4043
use crate::{tonemapping::TonemappingNode, upscaling::UpscalingNode};
4144

@@ -83,7 +86,8 @@ pub struct Transparent2d {
8386
pub entity: Entity,
8487
pub pipeline: CachedRenderPipelineId,
8588
pub draw_function: DrawFunctionId,
86-
pub batch_size: usize,
89+
pub batch_range: Range<u32>,
90+
pub dynamic_offset: Option<NonMaxU32>,
8791
}
8892

8993
impl PhaseItem for Transparent2d {
@@ -110,8 +114,23 @@ impl PhaseItem for Transparent2d {
110114
}
111115

112116
#[inline]
113-
fn batch_size(&self) -> usize {
114-
self.batch_size
117+
fn batch_range(&self) -> &Range<u32> {
118+
&self.batch_range
119+
}
120+
121+
#[inline]
122+
fn batch_range_mut(&mut self) -> &mut Range<u32> {
123+
&mut self.batch_range
124+
}
125+
126+
#[inline]
127+
fn dynamic_offset(&self) -> Option<NonMaxU32> {
128+
self.dynamic_offset
129+
}
130+
131+
#[inline]
132+
fn dynamic_offset_mut(&mut self) -> &mut Option<NonMaxU32> {
133+
&mut self.dynamic_offset
115134
}
116135
}
117136

crates/bevy_core_pipeline/src/core_3d/mod.rs

Lines changed: 59 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ pub mod graph {
2424
}
2525
pub const CORE_3D: &str = graph::NAME;
2626

27-
use std::cmp::Reverse;
27+
use std::{cmp::Reverse, ops::Range};
2828

2929
pub use camera_3d::*;
3030
pub use main_opaque_pass_3d_node::*;
@@ -51,6 +51,7 @@ use bevy_render::{
5151
Extract, ExtractSchedule, Render, RenderApp, RenderSet,
5252
};
5353
use bevy_utils::{FloatOrd, HashMap};
54+
use nonmax::NonMaxU32;
5455

5556
use crate::{
5657
prepass::{
@@ -135,7 +136,8 @@ pub struct Opaque3d {
135136
pub pipeline: CachedRenderPipelineId,
136137
pub entity: Entity,
137138
pub draw_function: DrawFunctionId,
138-
pub batch_size: usize,
139+
pub batch_range: Range<u32>,
140+
pub dynamic_offset: Option<NonMaxU32>,
139141
}
140142

141143
impl PhaseItem for Opaque3d {
@@ -164,8 +166,23 @@ impl PhaseItem for Opaque3d {
164166
}
165167

166168
#[inline]
167-
fn batch_size(&self) -> usize {
168-
self.batch_size
169+
fn batch_range(&self) -> &Range<u32> {
170+
&self.batch_range
171+
}
172+
173+
#[inline]
174+
fn batch_range_mut(&mut self) -> &mut Range<u32> {
175+
&mut self.batch_range
176+
}
177+
178+
#[inline]
179+
fn dynamic_offset(&self) -> Option<NonMaxU32> {
180+
self.dynamic_offset
181+
}
182+
183+
#[inline]
184+
fn dynamic_offset_mut(&mut self) -> &mut Option<NonMaxU32> {
185+
&mut self.dynamic_offset
169186
}
170187
}
171188

@@ -181,7 +198,8 @@ pub struct AlphaMask3d {
181198
pub pipeline: CachedRenderPipelineId,
182199
pub entity: Entity,
183200
pub draw_function: DrawFunctionId,
184-
pub batch_size: usize,
201+
pub batch_range: Range<u32>,
202+
pub dynamic_offset: Option<NonMaxU32>,
185203
}
186204

187205
impl PhaseItem for AlphaMask3d {
@@ -210,8 +228,23 @@ impl PhaseItem for AlphaMask3d {
210228
}
211229

212230
#[inline]
213-
fn batch_size(&self) -> usize {
214-
self.batch_size
231+
fn batch_range(&self) -> &Range<u32> {
232+
&self.batch_range
233+
}
234+
235+
#[inline]
236+
fn batch_range_mut(&mut self) -> &mut Range<u32> {
237+
&mut self.batch_range
238+
}
239+
240+
#[inline]
241+
fn dynamic_offset(&self) -> Option<NonMaxU32> {
242+
self.dynamic_offset
243+
}
244+
245+
#[inline]
246+
fn dynamic_offset_mut(&mut self) -> &mut Option<NonMaxU32> {
247+
&mut self.dynamic_offset
215248
}
216249
}
217250

@@ -227,7 +260,8 @@ pub struct Transparent3d {
227260
pub pipeline: CachedRenderPipelineId,
228261
pub entity: Entity,
229262
pub draw_function: DrawFunctionId,
230-
pub batch_size: usize,
263+
pub batch_range: Range<u32>,
264+
pub dynamic_offset: Option<NonMaxU32>,
231265
}
232266

233267
impl PhaseItem for Transparent3d {
@@ -255,8 +289,23 @@ impl PhaseItem for Transparent3d {
255289
}
256290

257291
#[inline]
258-
fn batch_size(&self) -> usize {
259-
self.batch_size
292+
fn batch_range(&self) -> &Range<u32> {
293+
&self.batch_range
294+
}
295+
296+
#[inline]
297+
fn batch_range_mut(&mut self) -> &mut Range<u32> {
298+
&mut self.batch_range
299+
}
300+
301+
#[inline]
302+
fn dynamic_offset(&self) -> Option<NonMaxU32> {
303+
self.dynamic_offset
304+
}
305+
306+
#[inline]
307+
fn dynamic_offset_mut(&mut self) -> &mut Option<NonMaxU32> {
308+
&mut self.dynamic_offset
260309
}
261310
}
262311

crates/bevy_core_pipeline/src/prepass/mod.rs

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
2828
pub mod node;
2929

30-
use std::cmp::Reverse;
30+
use std::{cmp::Reverse, ops::Range};
3131

3232
use bevy_ecs::prelude::*;
3333
use bevy_reflect::Reflect;
@@ -37,6 +37,7 @@ use bevy_render::{
3737
texture::CachedTexture,
3838
};
3939
use bevy_utils::FloatOrd;
40+
use nonmax::NonMaxU32;
4041

4142
pub const DEPTH_PREPASS_FORMAT: TextureFormat = TextureFormat::Depth32Float;
4243
pub const NORMAL_PREPASS_FORMAT: TextureFormat = TextureFormat::Rgb10a2Unorm;
@@ -83,6 +84,8 @@ pub struct Opaque3dPrepass {
8384
pub entity: Entity,
8485
pub pipeline_id: CachedRenderPipelineId,
8586
pub draw_function: DrawFunctionId,
87+
pub batch_range: Range<u32>,
88+
pub dynamic_offset: Option<NonMaxU32>,
8689
}
8790

8891
impl PhaseItem for Opaque3dPrepass {
@@ -109,6 +112,26 @@ impl PhaseItem for Opaque3dPrepass {
109112
// Key negated to match reversed SortKey ordering
110113
radsort::sort_by_key(items, |item| -item.distance);
111114
}
115+
116+
#[inline]
117+
fn batch_range(&self) -> &Range<u32> {
118+
&self.batch_range
119+
}
120+
121+
#[inline]
122+
fn batch_range_mut(&mut self) -> &mut Range<u32> {
123+
&mut self.batch_range
124+
}
125+
126+
#[inline]
127+
fn dynamic_offset(&self) -> Option<NonMaxU32> {
128+
self.dynamic_offset
129+
}
130+
131+
#[inline]
132+
fn dynamic_offset_mut(&mut self) -> &mut Option<NonMaxU32> {
133+
&mut self.dynamic_offset
134+
}
112135
}
113136

114137
impl CachedRenderPipelinePhaseItem for Opaque3dPrepass {
@@ -128,6 +151,8 @@ pub struct AlphaMask3dPrepass {
128151
pub entity: Entity,
129152
pub pipeline_id: CachedRenderPipelineId,
130153
pub draw_function: DrawFunctionId,
154+
pub batch_range: Range<u32>,
155+
pub dynamic_offset: Option<NonMaxU32>,
131156
}
132157

133158
impl PhaseItem for AlphaMask3dPrepass {
@@ -154,6 +179,26 @@ impl PhaseItem for AlphaMask3dPrepass {
154179
// Key negated to match reversed SortKey ordering
155180
radsort::sort_by_key(items, |item| -item.distance);
156181
}
182+
183+
#[inline]
184+
fn batch_range(&self) -> &Range<u32> {
185+
&self.batch_range
186+
}
187+
188+
#[inline]
189+
fn batch_range_mut(&mut self) -> &mut Range<u32> {
190+
&mut self.batch_range
191+
}
192+
193+
#[inline]
194+
fn dynamic_offset(&self) -> Option<NonMaxU32> {
195+
self.dynamic_offset
196+
}
197+
198+
#[inline]
199+
fn dynamic_offset_mut(&mut self) -> &mut Option<NonMaxU32> {
200+
&mut self.dynamic_offset
201+
}
157202
}
158203

159204
impl CachedRenderPipelinePhaseItem for AlphaMask3dPrepass {

crates/bevy_gizmos/src/pipeline_2d.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,8 @@ fn queue_line_gizmos_2d(
178178
draw_function,
179179
pipeline,
180180
sort_key: FloatOrd(f32::INFINITY),
181-
batch_size: 1,
181+
batch_range: 0..1,
182+
dynamic_offset: None,
182183
});
183184
}
184185
}

crates/bevy_gizmos/src/pipeline_3d.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,8 @@ fn queue_line_gizmos_3d(
192192
draw_function,
193193
pipeline,
194194
distance: 0.,
195-
batch_size: 1,
195+
batch_range: 0..1,
196+
dynamic_offset: None,
196197
});
197198
}
198199
}

crates/bevy_pbr/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,4 @@ bytemuck = { version = "1", features = ["derive"] }
3333
naga_oil = "0.8"
3434
radsort = "0.1"
3535
smallvec = "1.6"
36+
nonmax = "0.5.3"

0 commit comments

Comments
 (0)