Skip to content

Commit

Permalink
Use multi_draw_indirect_count where available, in preparation for
Browse files Browse the repository at this point in the history
two-phase occlusion culling.

This commit allows Bevy to use `multi_draw_indirect_count` for
drawing meshes. The `multi_draw_indirect_count` feature works just like
`multi_draw_indirect`, but it takes the number of indirect parameters
from a GPU buffer rather than specifying it on the CPU.

Currently, the CPU constructs the list of indirect draw parameters with
the instance count for each batch set to zero, uploads the resulting
buffer to the GPU, and dispatches a compute shader that bumps the
instance count for each mesh that survives culling. Unfortunately, this
is inefficient when we support `multi_draw_indirect_count`. Draw
commands corresponding to meshes for which all instances were culled
will remain present in the list when calling
`multi_draw_indirect_count`, causing overhead. Proper use of
`multi_draw_indirect_count` requires eliminating these empty draw
commands.

To address this inefficiency, this PR makes Bevy fully construct the
indirect draw commands on the GPU instead of on the CPU. Instead of
writing instance counts to the draw command buffer, the mesh
preprocessing shader now writes them to a separate *indirect metadata
buffer*. A second compute dispatch known as the *build indirect
parameters* shader runs after mesh preprocessing and converts the
indirect draw metadata into actual indirect draw commands for the GPU.
The build indirect parameters shader operates on a batch at a time,
rather than an instance at a time, and as such each thread writes only 0
or 1 indirect draw parameters, simplifying the current logic in
`mesh_preprocessing`, which has to have special cases for the first mesh
in each batch. The build indirect parameters shader emits draw commands
in a tightly packed manner, enabling maximally efficient use of
`multi_draw_indirect_count`.

Along the way, this patch switches mesh preprocessing to dispatch one
compute invocation per render phase per view, instead of dispatching one
compute invocation per view. This is preparation for two-phase occlusion
culling, in which we will have two mesh preprocessing stages. In that
scenario, the first mesh preprocessing stage must only process opaque
and alpha tested objects, so the work items must be separated into those
that are opaque or alpha tested and those that aren't. Thus this PR
splits out the work items into a separate buffer for each phase. As this
patch rewrites so much of the mesh preprocessing infrastructure, it was
simpler to just fold the change into this patch instead of deferring it
to the forthcoming occlusion culling PR.

Finally, this patch changes mesh preprocessing so that it runs
separately for indexed and non-indexed meshes. This is because draw
commands for indexed and non-indexed meshes have different sizes and
layouts. *The existing code is actually broken for non-indexed meshes*,
as it attempts to overlay the indirect parameters for non-indexed meshes
on top of those for indexed meshes. Consequently, right now the
parameters will be read incorrectly when multiple non-indexed meshes are
multi-drawn together. *This is a bug fix* and, as with the change to
dispatch phases separately noted above, was easiest to include in this
patch as opposed to separately.
  • Loading branch information
pcwalton committed Jan 7, 2025
1 parent 5faff84 commit 22c95b3
Show file tree
Hide file tree
Showing 28 changed files with 1,991 additions and 592 deletions.
35 changes: 28 additions & 7 deletions crates/bevy_core_pipeline/src/core_2d/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ pub mod graph {
use core::ops::Range;

use bevy_asset::UntypedAssetId;
use bevy_render::batching::gpu_preprocessing::GpuPreprocessingMode;
use bevy_render::{
batching::gpu_preprocessing::GpuPreprocessingMode, render_phase::PhaseItemBatchSetKey,
};
use bevy_utils::HashMap;
pub use camera_2d::*;
pub use main_opaque_pass_2d_node::*;
Expand Down Expand Up @@ -129,7 +131,7 @@ pub struct Opaque2d {
///
/// Objects in a single batch set can potentially be multi-drawn together,
/// if it's enabled and the current platform supports it.
pub batch_set_key: (),
pub batch_set_key: BatchSetKey2d,
/// The key, which determines which can be batched.
pub bin_key: Opaque2dBinKey,
/// An entity from which data will be fetched, including the mesh if
Expand Down Expand Up @@ -195,7 +197,7 @@ impl PhaseItem for Opaque2d {
impl BinnedPhaseItem for Opaque2d {
// Since 2D meshes presently can't be multidrawn, the batch set key is
// irrelevant.
type BatchSetKey = ();
type BatchSetKey = BatchSetKey2d;

type BinKey = Opaque2dBinKey;

Expand All @@ -216,6 +218,20 @@ impl BinnedPhaseItem for Opaque2d {
}
}

/// 2D meshes aren't currently multi-drawn together, so this batch set key only
/// stores whether the mesh is indexed.
#[derive(Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Hash)]
pub struct BatchSetKey2d {
/// True if the mesh is indexed.
pub indexed: bool,
}

impl PhaseItemBatchSetKey for BatchSetKey2d {
fn indexed(&self) -> bool {
self.indexed
}
}

impl CachedRenderPipelinePhaseItem for Opaque2d {
#[inline]
fn cached_pipeline(&self) -> CachedRenderPipelineId {
Expand All @@ -229,7 +245,7 @@ pub struct AlphaMask2d {
///
/// Objects in a single batch set can potentially be multi-drawn together,
/// if it's enabled and the current platform supports it.
pub batch_set_key: (),
pub batch_set_key: BatchSetKey2d,
/// The key, which determines which can be batched.
pub bin_key: AlphaMask2dBinKey,
/// An entity from which data will be fetched, including the mesh if
Expand Down Expand Up @@ -294,9 +310,7 @@ impl PhaseItem for AlphaMask2d {
}

impl BinnedPhaseItem for AlphaMask2d {
// Since 2D meshes presently can't be multidrawn, the batch set key is
// irrelevant.
type BatchSetKey = ();
type BatchSetKey = BatchSetKey2d;

type BinKey = AlphaMask2dBinKey;

Expand Down Expand Up @@ -332,6 +346,9 @@ pub struct Transparent2d {
pub draw_function: DrawFunctionId,
pub batch_range: Range<u32>,
pub extra_index: PhaseItemExtraIndex,
/// Whether the mesh in question is indexed (uses an index buffer in
/// addition to its vertex buffer).
pub indexed: bool,
}

impl PhaseItem for Transparent2d {
Expand Down Expand Up @@ -384,6 +401,10 @@ impl SortedPhaseItem for Transparent2d {
// radsort is a stable radix sort that performed better than `slice::sort_by_key` or `slice::sort_unstable_by_key`.
radsort::sort_by_key(items, |item| item.sort_key().0);
}

fn indexed(&self) -> bool {
self.indexed
}
}

impl CachedRenderPipelinePhaseItem for Transparent2d {
Expand Down
23 changes: 23 additions & 0 deletions crates/bevy_core_pipeline/src/core_3d/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ use core::ops::Range;
use bevy_render::{
batching::gpu_preprocessing::{GpuPreprocessingMode, GpuPreprocessingSupport},
mesh::allocator::SlabId,
render_phase::PhaseItemBatchSetKey,
view::NoIndirectDrawing,
};
pub use camera_3d::*;
Expand Down Expand Up @@ -269,6 +270,12 @@ pub struct Opaque3dBatchSetKey {
pub lightmap_slab: Option<NonMaxU32>,
}

impl PhaseItemBatchSetKey for Opaque3dBatchSetKey {
fn indexed(&self) -> bool {
self.index_slab.is_some()
}
}

/// Data that must be identical in order to *batch* phase items together.
///
/// Note that a *batch set* (if multi-draw is in use) contains multiple batches.
Expand Down Expand Up @@ -430,6 +437,9 @@ pub struct Transmissive3d {
pub draw_function: DrawFunctionId,
pub batch_range: Range<u32>,
pub extra_index: PhaseItemExtraIndex,
/// Whether the mesh in question is indexed (uses an index buffer in
/// addition to its vertex buffer).
pub indexed: bool,
}

impl PhaseItem for Transmissive3d {
Expand Down Expand Up @@ -493,6 +503,11 @@ impl SortedPhaseItem for Transmissive3d {
fn sort(items: &mut [Self]) {
radsort::sort_by_key(items, |item| item.distance);
}

#[inline]
fn indexed(&self) -> bool {
self.indexed
}
}

impl CachedRenderPipelinePhaseItem for Transmissive3d {
Expand All @@ -509,6 +524,9 @@ pub struct Transparent3d {
pub draw_function: DrawFunctionId,
pub batch_range: Range<u32>,
pub extra_index: PhaseItemExtraIndex,
/// Whether the mesh in question is indexed (uses an index buffer in
/// addition to its vertex buffer).
pub indexed: bool,
}

impl PhaseItem for Transparent3d {
Expand Down Expand Up @@ -560,6 +578,11 @@ impl SortedPhaseItem for Transparent3d {
fn sort(items: &mut [Self]) {
radsort::sort_by_key(items, |item| item.distance);
}

#[inline]
fn indexed(&self) -> bool {
self.indexed
}
}

impl CachedRenderPipelinePhaseItem for Transparent3d {
Expand Down
7 changes: 7 additions & 0 deletions crates/bevy_core_pipeline/src/prepass/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ use bevy_ecs::prelude::*;
use bevy_math::Mat4;
use bevy_reflect::{std_traits::ReflectDefault, Reflect};
use bevy_render::mesh::allocator::SlabId;
use bevy_render::render_phase::PhaseItemBatchSetKey;
use bevy_render::sync_world::MainEntity;
use bevy_render::{
render_phase::{
Expand Down Expand Up @@ -184,6 +185,12 @@ pub struct OpaqueNoLightmap3dBatchSetKey {
pub index_slab: Option<SlabId>,
}

impl PhaseItemBatchSetKey for OpaqueNoLightmap3dBatchSetKey {
fn indexed(&self) -> bool {
self.index_slab.is_some()
}
}

// TODO: Try interning these.
/// The data used to bin each opaque 3D object in the prepass and deferred pass.
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
Expand Down
3 changes: 3 additions & 0 deletions crates/bevy_gizmos/src/pipeline_2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@ fn queue_line_gizmos_2d(
sort_key: FloatOrd(f32::INFINITY),
batch_range: 0..1,
extra_index: PhaseItemExtraIndex::None,
indexed: false,
});
}

Expand All @@ -360,6 +361,7 @@ fn queue_line_gizmos_2d(
sort_key: FloatOrd(f32::INFINITY),
batch_range: 0..1,
extra_index: PhaseItemExtraIndex::None,
indexed: false,
});
}
}
Expand Down Expand Up @@ -419,6 +421,7 @@ fn queue_line_joint_gizmos_2d(
sort_key: FloatOrd(f32::INFINITY),
batch_range: 0..1,
extra_index: PhaseItemExtraIndex::None,
indexed: false,
});
}
}
Expand Down
3 changes: 3 additions & 0 deletions crates/bevy_gizmos/src/pipeline_3d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,7 @@ fn queue_line_gizmos_3d(
distance: 0.,
batch_range: 0..1,
extra_index: PhaseItemExtraIndex::None,
indexed: true,
});
}

Expand All @@ -392,6 +393,7 @@ fn queue_line_gizmos_3d(
distance: 0.,
batch_range: 0..1,
extra_index: PhaseItemExtraIndex::None,
indexed: true,
});
}
}
Expand Down Expand Up @@ -488,6 +490,7 @@ fn queue_line_joint_gizmos_3d(
distance: 0.,
batch_range: 0..1,
extra_index: PhaseItemExtraIndex::None,
indexed: true,
});
}
}
Expand Down
2 changes: 2 additions & 0 deletions crates/bevy_pbr/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ pub mod graph {
GpuPreprocess,
/// Label for the screen space reflections pass.
ScreenSpaceReflections,
/// Label for the indirect parameters building pass.
BuildIndirectParametersNode,
}
}

Expand Down
12 changes: 7 additions & 5 deletions crates/bevy_pbr/src/material.rs
Original file line number Diff line number Diff line change
Expand Up @@ -855,6 +855,9 @@ pub fn queue_material_meshes<M: Material>(
}
};

// Fetch the slabs that this mesh resides in.
let (vertex_slab, index_slab) = mesh_allocator.mesh_slabs(&mesh_instance.mesh_asset_id);

match mesh_key
.intersection(MeshPipelineKey::BLEND_RESERVED_BITS | MeshPipelineKey::MAY_DISCARD)
{
Expand All @@ -869,13 +872,12 @@ pub fn queue_material_meshes<M: Material>(
distance,
batch_range: 0..1,
extra_index: PhaseItemExtraIndex::None,
indexed: index_slab.is_some(),
});
} else if material.properties.render_method == OpaqueRendererMethod::Forward {
let (vertex_slab, index_slab) =
mesh_allocator.mesh_slabs(&mesh_instance.mesh_asset_id);
let batch_set_key = Opaque3dBatchSetKey {
draw_function: draw_opaque_pbr,
pipeline: pipeline_id,
draw_function: draw_opaque_pbr,
material_bind_group_index: Some(material.binding.group.0),
vertex_slab: vertex_slab.unwrap_or_default(),
index_slab,
Expand Down Expand Up @@ -908,10 +910,9 @@ pub fn queue_material_meshes<M: Material>(
distance,
batch_range: 0..1,
extra_index: PhaseItemExtraIndex::None,
indexed: index_slab.is_some(),
});
} else if material.properties.render_method == OpaqueRendererMethod::Forward {
let (vertex_slab, index_slab) =
mesh_allocator.mesh_slabs(&mesh_instance.mesh_asset_id);
let batch_set_key = OpaqueNoLightmap3dBatchSetKey {
draw_function: draw_alpha_mask_pbr,
pipeline: pipeline_id,
Expand Down Expand Up @@ -943,6 +944,7 @@ pub fn queue_material_meshes<M: Material>(
distance,
batch_range: 0..1,
extra_index: PhaseItemExtraIndex::None,
indexed: index_slab.is_some(),
});
}
}
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_pbr/src/prepass/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -959,13 +959,13 @@ pub fn queue_prepass_material_meshes<M: Material>(
}
};

let (vertex_slab, index_slab) = mesh_allocator.mesh_slabs(&mesh_instance.mesh_asset_id);

match mesh_key
.intersection(MeshPipelineKey::BLEND_RESERVED_BITS | MeshPipelineKey::MAY_DISCARD)
{
MeshPipelineKey::BLEND_OPAQUE | MeshPipelineKey::BLEND_ALPHA_TO_COVERAGE => {
if deferred {
let (vertex_slab, index_slab) =
mesh_allocator.mesh_slabs(&mesh_instance.mesh_asset_id);
opaque_deferred_phase.as_mut().unwrap().add(
OpaqueNoLightmap3dBatchSetKey {
draw_function: opaque_draw_deferred,
Expand Down
Loading

0 comments on commit 22c95b3

Please sign in to comment.