Skip to content

Commit

Permalink
Implement batching for 2D and 3D meshes
Browse files Browse the repository at this point in the history
  • Loading branch information
superdump committed Sep 4, 2023
1 parent a166b65 commit 5468edc
Show file tree
Hide file tree
Showing 30 changed files with 884 additions and 209 deletions.
6 changes: 4 additions & 2 deletions assets/shaders/custom_gltf_2d.wgsl
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#import bevy_sprite::mesh2d_view_bindings globals
#import bevy_sprite::mesh2d_bindings mesh
#import bevy_sprite::mesh2d_functions mesh2d_position_local_to_clip
#import bevy_sprite::mesh2d_functions get_model_matrix, mesh2d_position_local_to_clip

struct Vertex {
@builtin(instance_index) instance_index: u32,
@location(0) position: vec3<f32>,
@location(1) color: vec4<f32>,
@location(2) barycentric: vec3<f32>,
Expand All @@ -17,7 +18,8 @@ struct VertexOutput {
@vertex
fn vertex(vertex: Vertex) -> VertexOutput {
var out: VertexOutput;
out.clip_position = mesh2d_position_local_to_clip(mesh.model, vec4<f32>(vertex.position, 1.0));
let model = get_model_matrix(vertex.instance_index);
out.clip_position = mesh2d_position_local_to_clip(model, vec4<f32>(vertex.position, 1.0));
out.color = vertex.color;
out.barycentric = vertex.barycentric;
return out;
Expand Down
1 change: 1 addition & 0 deletions crates/bevy_core_pipeline/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,4 @@ bevy_utils = { path = "../bevy_utils", version = "0.12.0-dev" }
serde = { version = "1", features = ["derive"] }
bitflags = "2.3"
radsort = "0.1"
nonmax = "0.5.3"
25 changes: 22 additions & 3 deletions crates/bevy_core_pipeline/src/core_2d/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ pub mod graph {
}
pub const CORE_2D: &str = graph::NAME;

use std::ops::Range;

pub use camera_2d::*;
pub use main_pass_2d_node::*;

Expand All @@ -36,6 +38,7 @@ use bevy_render::{
Extract, ExtractSchedule, Render, RenderApp, RenderSet,
};
use bevy_utils::FloatOrd;
use nonmax::NonMaxU32;

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

Expand Down Expand Up @@ -83,7 +86,8 @@ pub struct Transparent2d {
pub entity: Entity,
pub pipeline: CachedRenderPipelineId,
pub draw_function: DrawFunctionId,
pub batch_size: usize,
pub batch_range: Range<u32>,
pub dynamic_offset: Option<NonMaxU32>,
}

impl PhaseItem for Transparent2d {
Expand All @@ -110,8 +114,23 @@ impl PhaseItem for Transparent2d {
}

#[inline]
fn batch_size(&self) -> usize {
self.batch_size
fn batch_range(&self) -> &Range<u32> {
&self.batch_range
}

#[inline]
fn batch_range_mut(&mut self) -> &mut Range<u32> {
&mut self.batch_range
}

#[inline]
fn dynamic_offset(&self) -> Option<NonMaxU32> {
self.dynamic_offset
}

#[inline]
fn dynamic_offset_mut(&mut self) -> &mut Option<NonMaxU32> {
&mut self.dynamic_offset
}
}

Expand Down
69 changes: 59 additions & 10 deletions crates/bevy_core_pipeline/src/core_3d/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub mod graph {
}
pub const CORE_3D: &str = graph::NAME;

use std::cmp::Reverse;
use std::{cmp::Reverse, ops::Range};

pub use camera_3d::*;
pub use main_opaque_pass_3d_node::*;
Expand All @@ -51,6 +51,7 @@ use bevy_render::{
Extract, ExtractSchedule, Render, RenderApp, RenderSet,
};
use bevy_utils::{FloatOrd, HashMap};
use nonmax::NonMaxU32;

use crate::{
prepass::{
Expand Down Expand Up @@ -135,7 +136,8 @@ pub struct Opaque3d {
pub pipeline: CachedRenderPipelineId,
pub entity: Entity,
pub draw_function: DrawFunctionId,
pub batch_size: usize,
pub batch_range: Range<u32>,
pub dynamic_offset: Option<NonMaxU32>,
}

impl PhaseItem for Opaque3d {
Expand Down Expand Up @@ -164,8 +166,23 @@ impl PhaseItem for Opaque3d {
}

#[inline]
fn batch_size(&self) -> usize {
self.batch_size
fn batch_range(&self) -> &Range<u32> {
&self.batch_range
}

#[inline]
fn batch_range_mut(&mut self) -> &mut Range<u32> {
&mut self.batch_range
}

#[inline]
fn dynamic_offset(&self) -> Option<NonMaxU32> {
self.dynamic_offset
}

#[inline]
fn dynamic_offset_mut(&mut self) -> &mut Option<NonMaxU32> {
&mut self.dynamic_offset
}
}

Expand All @@ -181,7 +198,8 @@ pub struct AlphaMask3d {
pub pipeline: CachedRenderPipelineId,
pub entity: Entity,
pub draw_function: DrawFunctionId,
pub batch_size: usize,
pub batch_range: Range<u32>,
pub dynamic_offset: Option<NonMaxU32>,
}

impl PhaseItem for AlphaMask3d {
Expand Down Expand Up @@ -210,8 +228,23 @@ impl PhaseItem for AlphaMask3d {
}

#[inline]
fn batch_size(&self) -> usize {
self.batch_size
fn batch_range(&self) -> &Range<u32> {
&self.batch_range
}

#[inline]
fn batch_range_mut(&mut self) -> &mut Range<u32> {
&mut self.batch_range
}

#[inline]
fn dynamic_offset(&self) -> Option<NonMaxU32> {
self.dynamic_offset
}

#[inline]
fn dynamic_offset_mut(&mut self) -> &mut Option<NonMaxU32> {
&mut self.dynamic_offset
}
}

Expand All @@ -227,7 +260,8 @@ pub struct Transparent3d {
pub pipeline: CachedRenderPipelineId,
pub entity: Entity,
pub draw_function: DrawFunctionId,
pub batch_size: usize,
pub batch_range: Range<u32>,
pub dynamic_offset: Option<NonMaxU32>,
}

impl PhaseItem for Transparent3d {
Expand Down Expand Up @@ -255,8 +289,23 @@ impl PhaseItem for Transparent3d {
}

#[inline]
fn batch_size(&self) -> usize {
self.batch_size
fn batch_range(&self) -> &Range<u32> {
&self.batch_range
}

#[inline]
fn batch_range_mut(&mut self) -> &mut Range<u32> {
&mut self.batch_range
}

#[inline]
fn dynamic_offset(&self) -> Option<NonMaxU32> {
self.dynamic_offset
}

#[inline]
fn dynamic_offset_mut(&mut self) -> &mut Option<NonMaxU32> {
&mut self.dynamic_offset
}
}

Expand Down
47 changes: 46 additions & 1 deletion crates/bevy_core_pipeline/src/prepass/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
pub mod node;

use std::cmp::Reverse;
use std::{cmp::Reverse, ops::Range};

use bevy_ecs::prelude::*;
use bevy_reflect::Reflect;
Expand All @@ -37,6 +37,7 @@ use bevy_render::{
texture::CachedTexture,
};
use bevy_utils::FloatOrd;
use nonmax::NonMaxU32;

pub const DEPTH_PREPASS_FORMAT: TextureFormat = TextureFormat::Depth32Float;
pub const NORMAL_PREPASS_FORMAT: TextureFormat = TextureFormat::Rgb10a2Unorm;
Expand Down Expand Up @@ -83,6 +84,8 @@ pub struct Opaque3dPrepass {
pub entity: Entity,
pub pipeline_id: CachedRenderPipelineId,
pub draw_function: DrawFunctionId,
pub batch_range: Range<u32>,
pub dynamic_offset: Option<NonMaxU32>,
}

impl PhaseItem for Opaque3dPrepass {
Expand All @@ -109,6 +112,26 @@ impl PhaseItem for Opaque3dPrepass {
// Key negated to match reversed SortKey ordering
radsort::sort_by_key(items, |item| -item.distance);
}

#[inline]
fn batch_range(&self) -> &Range<u32> {
&self.batch_range
}

#[inline]
fn batch_range_mut(&mut self) -> &mut Range<u32> {
&mut self.batch_range
}

#[inline]
fn dynamic_offset(&self) -> Option<NonMaxU32> {
self.dynamic_offset
}

#[inline]
fn dynamic_offset_mut(&mut self) -> &mut Option<NonMaxU32> {
&mut self.dynamic_offset
}
}

impl CachedRenderPipelinePhaseItem for Opaque3dPrepass {
Expand All @@ -128,6 +151,8 @@ pub struct AlphaMask3dPrepass {
pub entity: Entity,
pub pipeline_id: CachedRenderPipelineId,
pub draw_function: DrawFunctionId,
pub batch_range: Range<u32>,
pub dynamic_offset: Option<NonMaxU32>,
}

impl PhaseItem for AlphaMask3dPrepass {
Expand All @@ -154,6 +179,26 @@ impl PhaseItem for AlphaMask3dPrepass {
// Key negated to match reversed SortKey ordering
radsort::sort_by_key(items, |item| -item.distance);
}

#[inline]
fn batch_range(&self) -> &Range<u32> {
&self.batch_range
}

#[inline]
fn batch_range_mut(&mut self) -> &mut Range<u32> {
&mut self.batch_range
}

#[inline]
fn dynamic_offset(&self) -> Option<NonMaxU32> {
self.dynamic_offset
}

#[inline]
fn dynamic_offset_mut(&mut self) -> &mut Option<NonMaxU32> {
&mut self.dynamic_offset
}
}

impl CachedRenderPipelinePhaseItem for AlphaMask3dPrepass {
Expand Down
3 changes: 2 additions & 1 deletion crates/bevy_gizmos/src/pipeline_2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,8 @@ fn queue_line_gizmos_2d(
draw_function,
pipeline,
sort_key: FloatOrd(f32::INFINITY),
batch_size: 1,
batch_range: 0..1,
dynamic_offset: None,
});
}
}
Expand Down
3 changes: 2 additions & 1 deletion crates/bevy_gizmos/src/pipeline_3d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,8 @@ fn queue_line_gizmos_3d(
draw_function,
pipeline,
distance: 0.,
batch_size: 1,
batch_range: 0..1,
dynamic_offset: None,
});
}
}
Expand Down
1 change: 1 addition & 0 deletions crates/bevy_pbr/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,4 @@ bytemuck = { version = "1", features = ["derive"] }
naga_oil = "0.8"
radsort = "0.1"
smallvec = "1.6"
nonmax = "0.5.3"
Loading

0 comments on commit 5468edc

Please sign in to comment.