Micro-optimize queue_material_meshes, primarily to remove bit manipulation. (#12791)

This commit makes the following optimizations:

## `MeshPipelineKey`/`BaseMeshPipelineKey` split

`MeshPipelineKey` has been split into `BaseMeshPipelineKey`, which lives
in `bevy_render` and `MeshPipelineKey`, which lives in `bevy_pbr`.
Conceptually, `BaseMeshPipelineKey` is a superclass of
`MeshPipelineKey`. For `BaseMeshPipelineKey`, the bits start at the
highest (most significant) bit and grow downward toward the lowest bit;
for `MeshPipelineKey`, the bits start at the lowest bit and grow upward
toward the highest bit. This prevents them from colliding.

The goal of this is to avoid having to reassemble bits of the pipeline
key for every mesh every frame. Instead, we can just use a bitwise or
operation to combine the pieces that make up a `MeshPipelineKey`.

## `specialize_slow`

Previously, all of `specialize()` was marked as `#[inline]`. This
bloated `queue_material_meshes` unnecessarily, as a large chunk of it
ended up being a slow path that was rarely hit. This commit refactors
the function to move the slow path to `specialize_slow()`.

Together, these two changes shave about 5% off `queue_material_meshes`:

![Screenshot 2024-03-29
130002](https://github.com/bevyengine/bevy/assets/157897/a7e5a994-a807-4328-b314-9003429dcdd2)

## Migration Guide

- The `primitive_topology` field on `GpuMesh` is now an accessor method:
`GpuMesh::primitive_topology()`.
- For performance reasons, `MeshPipelineKey` has been split into
`BaseMeshPipelineKey`, which lives in `bevy_render`, and
`MeshPipelineKey`, which lives in `bevy_pbr`. These two should be
combined with bitwise-or to produce the final `MeshPipelineKey`.
This commit is contained in:
Patrick Walton 2024-04-01 16:58:53 -05:00 committed by GitHub
parent c8aa3ac7d1
commit 37522fd0ae
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 195 additions and 108 deletions

View file

@ -50,6 +50,7 @@ serde = { version = "1", features = ["derive", "rc"] }
bincode = "1" bincode = "1"
range-alloc = "0.1" range-alloc = "0.1"
nonmax = "0.5" nonmax = "0.5"
static_assertions = "1"
[lints] [lints]
workspace = true workspace = true

View file

@ -661,25 +661,9 @@ pub fn queue_material_meshes<M: Material>(
continue; continue;
}; };
let forward = match material.properties.render_method { let mut mesh_key = view_key
OpaqueRendererMethod::Forward => true, | MeshPipelineKey::from_bits_retain(mesh.key_bits.bits())
OpaqueRendererMethod::Deferred => false, | material.properties.mesh_pipeline_key_bits;
OpaqueRendererMethod::Auto => unreachable!(),
};
let mut mesh_key = view_key;
mesh_key |= MeshPipelineKey::from_primitive_topology(mesh.primitive_topology);
if mesh.morph_targets.is_some() {
mesh_key |= MeshPipelineKey::MORPH_TARGETS;
}
if material.properties.reads_view_transmission_texture {
mesh_key |= MeshPipelineKey::READS_VIEW_TRANSMISSION_TEXTURE;
}
mesh_key |= alpha_mode_pipeline_key(material.properties.alpha_mode);
let lightmap_image = render_lightmaps let lightmap_image = render_lightmaps
.render_lightmaps .render_lightmaps
@ -724,7 +708,7 @@ pub fn queue_material_meshes<M: Material>(
batch_range: 0..1, batch_range: 0..1,
dynamic_offset: None, dynamic_offset: None,
}); });
} else if forward { } else if material.properties.render_method == OpaqueRendererMethod::Forward {
let bin_key = Opaque3dBinKey { let bin_key = Opaque3dBinKey {
draw_function: draw_opaque_pbr, draw_function: draw_opaque_pbr,
pipeline: pipeline_id, pipeline: pipeline_id,
@ -748,7 +732,7 @@ pub fn queue_material_meshes<M: Material>(
batch_range: 0..1, batch_range: 0..1,
dynamic_offset: None, dynamic_offset: None,
}); });
} else if forward { } else if material.properties.render_method == OpaqueRendererMethod::Forward {
let bin_key = OpaqueNoLightmap3dBinKey { let bin_key = OpaqueNoLightmap3dBinKey {
draw_function: draw_alpha_mask_pbr, draw_function: draw_alpha_mask_pbr,
pipeline: pipeline_id, pipeline: pipeline_id,
@ -823,7 +807,7 @@ impl DefaultOpaqueRendererMethod {
/// bandwidth usage which can be unsuitable for low end mobile or other bandwidth-constrained devices. /// bandwidth usage which can be unsuitable for low end mobile or other bandwidth-constrained devices.
/// ///
/// If a material indicates `OpaqueRendererMethod::Auto`, `DefaultOpaqueRendererMethod` will be used. /// If a material indicates `OpaqueRendererMethod::Auto`, `DefaultOpaqueRendererMethod` will be used.
#[derive(Default, Clone, Copy, Debug, Reflect)] #[derive(Default, Clone, Copy, Debug, PartialEq, Reflect)]
pub enum OpaqueRendererMethod { pub enum OpaqueRendererMethod {
#[default] #[default]
Forward, Forward,
@ -838,6 +822,11 @@ pub struct MaterialProperties {
pub render_method: OpaqueRendererMethod, pub render_method: OpaqueRendererMethod,
/// The [`AlphaMode`] of this material. /// The [`AlphaMode`] of this material.
pub alpha_mode: AlphaMode, pub alpha_mode: AlphaMode,
/// The bits in the [`MeshPipelineKey`] for this material.
///
/// These are precalculated so that we can just "or" them together in
/// [`queue_material_meshes`].
pub mesh_pipeline_key_bits: MeshPipelineKey,
/// Add a bias to the view depth of the mesh which can be used to force a specific render order /// Add a bias to the view depth of the mesh which can be used to force a specific render order
/// for meshes with equal depth, to avoid z-fighting. /// for meshes with equal depth, to avoid z-fighting.
/// The bias is in depth-texture units so large values may be needed to overcome small depth differences. /// The bias is in depth-texture units so large values may be needed to overcome small depth differences.
@ -1061,6 +1050,14 @@ fn prepare_material<M: Material>(
OpaqueRendererMethod::Deferred => OpaqueRendererMethod::Deferred, OpaqueRendererMethod::Deferred => OpaqueRendererMethod::Deferred,
OpaqueRendererMethod::Auto => default_opaque_render_method, OpaqueRendererMethod::Auto => default_opaque_render_method,
}; };
let mut mesh_pipeline_key_bits = MeshPipelineKey::empty();
mesh_pipeline_key_bits.set(
MeshPipelineKey::READS_VIEW_TRANSMISSION_TEXTURE,
material.reads_view_transmission_texture(),
);
mesh_pipeline_key_bits.insert(alpha_mode_pipeline_key(material.alpha_mode()));
Ok(PreparedMaterial { Ok(PreparedMaterial {
bindings: prepared.bindings, bindings: prepared.bindings,
bind_group: prepared.bind_group, bind_group: prepared.bind_group,
@ -1068,8 +1065,10 @@ fn prepare_material<M: Material>(
properties: MaterialProperties { properties: MaterialProperties {
alpha_mode: material.alpha_mode(), alpha_mode: material.alpha_mode(),
depth_bias: material.depth_bias(), depth_bias: material.depth_bias(),
reads_view_transmission_texture: material.reads_view_transmission_texture(), reads_view_transmission_texture: mesh_pipeline_key_bits
.contains(MeshPipelineKey::READS_VIEW_TRANSMISSION_TEXTURE),
render_method: method, render_method: method,
mesh_pipeline_key_bits,
}, },
}) })
} }

View file

@ -792,11 +792,8 @@ pub fn queue_prepass_material_meshes<M: Material>(
continue; continue;
}; };
let mut mesh_key = let mut mesh_key = view_key | MeshPipelineKey::from_bits_retain(mesh.key_bits.bits());
MeshPipelineKey::from_primitive_topology(mesh.primitive_topology) | view_key;
if mesh.morph_targets.is_some() {
mesh_key |= MeshPipelineKey::MORPH_TARGETS;
}
let alpha_mode = material.properties.alpha_mode; let alpha_mode = material.properties.alpha_mode;
match alpha_mode { match alpha_mode {
AlphaMode::Opaque => {} AlphaMode::Opaque => {}

View file

@ -1644,6 +1644,10 @@ pub fn queue_shadows<M: Material>(
}; };
// NOTE: Lights with shadow mapping disabled will have no visible entities // NOTE: Lights with shadow mapping disabled will have no visible entities
// so no meshes will be queued // so no meshes will be queued
let mut light_key = MeshPipelineKey::DEPTH_PREPASS;
light_key.set(MeshPipelineKey::DEPTH_CLAMP_ORTHO, is_directional_light);
for entity in visible_entities.iter().copied() { for entity in visible_entities.iter().copied() {
let Some(mesh_instance) = render_mesh_instances.get(&entity) else { let Some(mesh_instance) = render_mesh_instances.get(&entity) else {
continue; continue;
@ -1662,14 +1666,7 @@ pub fn queue_shadows<M: Material>(
}; };
let mut mesh_key = let mut mesh_key =
MeshPipelineKey::from_primitive_topology(mesh.primitive_topology) light_key | MeshPipelineKey::from_bits_retain(mesh.key_bits.bits());
| MeshPipelineKey::DEPTH_PREPASS;
if mesh.morph_targets.is_some() {
mesh_key |= MeshPipelineKey::MORPH_TARGETS;
}
if is_directional_light {
mesh_key |= MeshPipelineKey::DEPTH_CLAMP_ORTHO;
}
// Even though we don't use the lightmap in the shadow map, the // Even though we don't use the lightmap in the shadow map, the
// `SetMeshBindGroup` render command will bind the data for it. So // `SetMeshBindGroup` render command will bind the data for it. So

View file

@ -31,6 +31,7 @@ use bevy_utils::{tracing::error, Entry, HashMap, Parallel};
#[cfg(debug_assertions)] #[cfg(debug_assertions)]
use bevy_utils::warn_once; use bevy_utils::warn_once;
use static_assertions::const_assert_eq;
use crate::render::{ use crate::render::{
morph::{ morph::{
@ -508,7 +509,13 @@ bitflags::bitflags! {
// NOTE: Apparently quadro drivers support up to 64x MSAA. // NOTE: Apparently quadro drivers support up to 64x MSAA.
/// MSAA uses the highest 3 bits for the MSAA log2(sample count) to support up to 128x MSAA. /// MSAA uses the highest 3 bits for the MSAA log2(sample count) to support up to 128x MSAA.
pub struct MeshPipelineKey: u32 { pub struct MeshPipelineKey: u32 {
// Nothing
const NONE = 0; const NONE = 0;
// Inherited bits
const MORPH_TARGETS = BaseMeshPipelineKey::MORPH_TARGETS.bits();
// Flag bits
const HDR = 1 << 0; const HDR = 1 << 0;
const TONEMAP_IN_SHADER = 1 << 1; const TONEMAP_IN_SHADER = 1 << 1;
const DEBAND_DITHER = 1 << 2; const DEBAND_DITHER = 1 << 2;
@ -522,17 +529,18 @@ bitflags::bitflags! {
const SCREEN_SPACE_AMBIENT_OCCLUSION = 1 << 9; const SCREEN_SPACE_AMBIENT_OCCLUSION = 1 << 9;
const DEPTH_CLAMP_ORTHO = 1 << 10; const DEPTH_CLAMP_ORTHO = 1 << 10;
const TEMPORAL_JITTER = 1 << 11; const TEMPORAL_JITTER = 1 << 11;
const MORPH_TARGETS = 1 << 12; const READS_VIEW_TRANSMISSION_TEXTURE = 1 << 12;
const READS_VIEW_TRANSMISSION_TEXTURE = 1 << 13; const LIGHTMAPPED = 1 << 13;
const LIGHTMAPPED = 1 << 14; const IRRADIANCE_VOLUME = 1 << 14;
const IRRADIANCE_VOLUME = 1 << 15; const LAST_FLAG = Self::IRRADIANCE_VOLUME.bits();
// Bitfields
const BLEND_RESERVED_BITS = Self::BLEND_MASK_BITS << Self::BLEND_SHIFT_BITS; // ← Bitmask reserving bits for the blend state const BLEND_RESERVED_BITS = Self::BLEND_MASK_BITS << Self::BLEND_SHIFT_BITS; // ← Bitmask reserving bits for the blend state
const BLEND_OPAQUE = 0 << Self::BLEND_SHIFT_BITS; // ← Values are just sequential within the mask, and can range from 0 to 3 const BLEND_OPAQUE = 0 << Self::BLEND_SHIFT_BITS; // ← Values are just sequential within the mask, and can range from 0 to 3
const BLEND_PREMULTIPLIED_ALPHA = 1 << Self::BLEND_SHIFT_BITS; // const BLEND_PREMULTIPLIED_ALPHA = 1 << Self::BLEND_SHIFT_BITS; //
const BLEND_MULTIPLY = 2 << Self::BLEND_SHIFT_BITS; // ← We still have room for one more value without adding more bits const BLEND_MULTIPLY = 2 << Self::BLEND_SHIFT_BITS; // ← We still have room for one more value without adding more bits
const BLEND_ALPHA = 3 << Self::BLEND_SHIFT_BITS; const BLEND_ALPHA = 3 << Self::BLEND_SHIFT_BITS;
const MSAA_RESERVED_BITS = Self::MSAA_MASK_BITS << Self::MSAA_SHIFT_BITS; const MSAA_RESERVED_BITS = Self::MSAA_MASK_BITS << Self::MSAA_SHIFT_BITS;
const PRIMITIVE_TOPOLOGY_RESERVED_BITS = Self::PRIMITIVE_TOPOLOGY_MASK_BITS << Self::PRIMITIVE_TOPOLOGY_SHIFT_BITS;
const TONEMAP_METHOD_RESERVED_BITS = Self::TONEMAP_METHOD_MASK_BITS << Self::TONEMAP_METHOD_SHIFT_BITS; const TONEMAP_METHOD_RESERVED_BITS = Self::TONEMAP_METHOD_MASK_BITS << Self::TONEMAP_METHOD_SHIFT_BITS;
const TONEMAP_METHOD_NONE = 0 << Self::TONEMAP_METHOD_SHIFT_BITS; const TONEMAP_METHOD_NONE = 0 << Self::TONEMAP_METHOD_SHIFT_BITS;
const TONEMAP_METHOD_REINHARD = 1 << Self::TONEMAP_METHOD_SHIFT_BITS; const TONEMAP_METHOD_REINHARD = 1 << Self::TONEMAP_METHOD_SHIFT_BITS;
@ -556,36 +564,38 @@ bitflags::bitflags! {
const SCREEN_SPACE_SPECULAR_TRANSMISSION_MEDIUM = 1 << Self::SCREEN_SPACE_SPECULAR_TRANSMISSION_SHIFT_BITS; const SCREEN_SPACE_SPECULAR_TRANSMISSION_MEDIUM = 1 << Self::SCREEN_SPACE_SPECULAR_TRANSMISSION_SHIFT_BITS;
const SCREEN_SPACE_SPECULAR_TRANSMISSION_HIGH = 2 << Self::SCREEN_SPACE_SPECULAR_TRANSMISSION_SHIFT_BITS; const SCREEN_SPACE_SPECULAR_TRANSMISSION_HIGH = 2 << Self::SCREEN_SPACE_SPECULAR_TRANSMISSION_SHIFT_BITS;
const SCREEN_SPACE_SPECULAR_TRANSMISSION_ULTRA = 3 << Self::SCREEN_SPACE_SPECULAR_TRANSMISSION_SHIFT_BITS; const SCREEN_SPACE_SPECULAR_TRANSMISSION_ULTRA = 3 << Self::SCREEN_SPACE_SPECULAR_TRANSMISSION_SHIFT_BITS;
const ALL_RESERVED_BITS =
Self::BLEND_RESERVED_BITS.bits() |
Self::MSAA_RESERVED_BITS.bits() |
Self::TONEMAP_METHOD_RESERVED_BITS.bits() |
Self::SHADOW_FILTER_METHOD_RESERVED_BITS.bits() |
Self::VIEW_PROJECTION_RESERVED_BITS.bits() |
Self::SCREEN_SPACE_SPECULAR_TRANSMISSION_RESERVED_BITS.bits();
} }
} }
impl MeshPipelineKey { impl MeshPipelineKey {
const MSAA_MASK_BITS: u32 = 0b111; const MSAA_MASK_BITS: u32 = 0b111;
const MSAA_SHIFT_BITS: u32 = 32 - Self::MSAA_MASK_BITS.count_ones(); const MSAA_SHIFT_BITS: u32 = Self::LAST_FLAG.bits().trailing_zeros();
const PRIMITIVE_TOPOLOGY_MASK_BITS: u32 = 0b111;
const PRIMITIVE_TOPOLOGY_SHIFT_BITS: u32 =
Self::MSAA_SHIFT_BITS - Self::PRIMITIVE_TOPOLOGY_MASK_BITS.count_ones();
const BLEND_MASK_BITS: u32 = 0b11; const BLEND_MASK_BITS: u32 = 0b11;
const BLEND_SHIFT_BITS: u32 = const BLEND_SHIFT_BITS: u32 = Self::MSAA_MASK_BITS.count_ones() + Self::MSAA_SHIFT_BITS;
Self::PRIMITIVE_TOPOLOGY_SHIFT_BITS - Self::BLEND_MASK_BITS.count_ones();
const TONEMAP_METHOD_MASK_BITS: u32 = 0b111; const TONEMAP_METHOD_MASK_BITS: u32 = 0b111;
const TONEMAP_METHOD_SHIFT_BITS: u32 = const TONEMAP_METHOD_SHIFT_BITS: u32 =
Self::BLEND_SHIFT_BITS - Self::TONEMAP_METHOD_MASK_BITS.count_ones(); Self::BLEND_MASK_BITS.count_ones() + Self::BLEND_SHIFT_BITS;
const SHADOW_FILTER_METHOD_MASK_BITS: u32 = 0b11; const SHADOW_FILTER_METHOD_MASK_BITS: u32 = 0b11;
const SHADOW_FILTER_METHOD_SHIFT_BITS: u32 = const SHADOW_FILTER_METHOD_SHIFT_BITS: u32 =
Self::TONEMAP_METHOD_SHIFT_BITS - Self::SHADOW_FILTER_METHOD_MASK_BITS.count_ones(); Self::TONEMAP_METHOD_MASK_BITS.count_ones() + Self::TONEMAP_METHOD_SHIFT_BITS;
const VIEW_PROJECTION_MASK_BITS: u32 = 0b11; const VIEW_PROJECTION_MASK_BITS: u32 = 0b11;
const VIEW_PROJECTION_SHIFT_BITS: u32 = const VIEW_PROJECTION_SHIFT_BITS: u32 =
Self::SHADOW_FILTER_METHOD_SHIFT_BITS - Self::VIEW_PROJECTION_MASK_BITS.count_ones(); Self::SHADOW_FILTER_METHOD_MASK_BITS.count_ones() + Self::SHADOW_FILTER_METHOD_SHIFT_BITS;
const SCREEN_SPACE_SPECULAR_TRANSMISSION_MASK_BITS: u32 = 0b11; const SCREEN_SPACE_SPECULAR_TRANSMISSION_MASK_BITS: u32 = 0b11;
const SCREEN_SPACE_SPECULAR_TRANSMISSION_SHIFT_BITS: u32 = Self::VIEW_PROJECTION_SHIFT_BITS const SCREEN_SPACE_SPECULAR_TRANSMISSION_SHIFT_BITS: u32 =
- Self::SCREEN_SPACE_SPECULAR_TRANSMISSION_MASK_BITS.count_ones(); Self::VIEW_PROJECTION_MASK_BITS.count_ones() + Self::VIEW_PROJECTION_SHIFT_BITS;
pub fn from_msaa_samples(msaa_samples: u32) -> Self { pub fn from_msaa_samples(msaa_samples: u32) -> Self {
let msaa_bits = let msaa_bits =
@ -607,14 +617,15 @@ impl MeshPipelineKey {
pub fn from_primitive_topology(primitive_topology: PrimitiveTopology) -> Self { pub fn from_primitive_topology(primitive_topology: PrimitiveTopology) -> Self {
let primitive_topology_bits = ((primitive_topology as u32) let primitive_topology_bits = ((primitive_topology as u32)
& Self::PRIMITIVE_TOPOLOGY_MASK_BITS) & BaseMeshPipelineKey::PRIMITIVE_TOPOLOGY_MASK_BITS)
<< Self::PRIMITIVE_TOPOLOGY_SHIFT_BITS; << BaseMeshPipelineKey::PRIMITIVE_TOPOLOGY_SHIFT_BITS;
Self::from_bits_retain(primitive_topology_bits) Self::from_bits_retain(primitive_topology_bits)
} }
pub fn primitive_topology(&self) -> PrimitiveTopology { pub fn primitive_topology(&self) -> PrimitiveTopology {
let primitive_topology_bits = (self.bits() >> Self::PRIMITIVE_TOPOLOGY_SHIFT_BITS) let primitive_topology_bits = (self.bits()
& Self::PRIMITIVE_TOPOLOGY_MASK_BITS; >> BaseMeshPipelineKey::PRIMITIVE_TOPOLOGY_SHIFT_BITS)
& BaseMeshPipelineKey::PRIMITIVE_TOPOLOGY_MASK_BITS;
match primitive_topology_bits { match primitive_topology_bits {
x if x == PrimitiveTopology::PointList as u32 => PrimitiveTopology::PointList, x if x == PrimitiveTopology::PointList as u32 => PrimitiveTopology::PointList,
x if x == PrimitiveTopology::LineList as u32 => PrimitiveTopology::LineList, x if x == PrimitiveTopology::LineList as u32 => PrimitiveTopology::LineList,
@ -626,6 +637,13 @@ impl MeshPipelineKey {
} }
} }
// Ensure that we didn't overflow the number of bits available in `MeshPipelineKey`.
const_assert_eq!(
(((MeshPipelineKey::LAST_FLAG.bits() << 1) - 1) | MeshPipelineKey::ALL_RESERVED_BITS.bits())
& BaseMeshPipelineKey::all().bits(),
0
);
fn is_skinned(layout: &MeshVertexBufferLayoutRef) -> bool { fn is_skinned(layout: &MeshVertexBufferLayoutRef) -> bool {
layout.0.contains(Mesh::ATTRIBUTE_JOINT_INDEX) layout.0.contains(Mesh::ATTRIBUTE_JOINT_INDEX)
&& layout.0.contains(Mesh::ATTRIBUTE_JOINT_WEIGHT) && layout.0.contains(Mesh::ATTRIBUTE_JOINT_WEIGHT)

View file

@ -1,6 +1,7 @@
mod conversions; mod conversions;
pub mod skinning; pub mod skinning;
use bevy_transform::components::Transform; use bevy_transform::components::Transform;
use bitflags::bitflags;
pub use wgpu::PrimitiveTopology; pub use wgpu::PrimitiveTopology;
use crate::{ use crate::{
@ -1393,6 +1394,43 @@ impl From<&Indices> for IndexFormat {
} }
} }
bitflags! {
/// Our base mesh pipeline key bits start from the highest bit and go
/// downward. The PBR mesh pipeline key bits start from the lowest bit and
/// go upward. This allows the PBR bits in the downstream crate `bevy_pbr`
/// to coexist in the same field without any shifts.
#[derive(Clone, Debug)]
pub struct BaseMeshPipelineKey: u32 {
const MORPH_TARGETS = 1 << 31;
}
}
impl BaseMeshPipelineKey {
pub const PRIMITIVE_TOPOLOGY_MASK_BITS: u32 = 0b111;
pub const PRIMITIVE_TOPOLOGY_SHIFT_BITS: u32 =
31 - Self::PRIMITIVE_TOPOLOGY_MASK_BITS.count_ones();
pub fn from_primitive_topology(primitive_topology: PrimitiveTopology) -> Self {
let primitive_topology_bits = ((primitive_topology as u32)
& Self::PRIMITIVE_TOPOLOGY_MASK_BITS)
<< Self::PRIMITIVE_TOPOLOGY_SHIFT_BITS;
Self::from_bits_retain(primitive_topology_bits)
}
pub fn primitive_topology(&self) -> PrimitiveTopology {
let primitive_topology_bits = (self.bits() >> Self::PRIMITIVE_TOPOLOGY_SHIFT_BITS)
& Self::PRIMITIVE_TOPOLOGY_MASK_BITS;
match primitive_topology_bits {
x if x == PrimitiveTopology::PointList as u32 => PrimitiveTopology::PointList,
x if x == PrimitiveTopology::LineList as u32 => PrimitiveTopology::LineList,
x if x == PrimitiveTopology::LineStrip as u32 => PrimitiveTopology::LineStrip,
x if x == PrimitiveTopology::TriangleList as u32 => PrimitiveTopology::TriangleList,
x if x == PrimitiveTopology::TriangleStrip as u32 => PrimitiveTopology::TriangleStrip,
_ => PrimitiveTopology::default(),
}
}
}
/// The GPU-representation of a [`Mesh`]. /// The GPU-representation of a [`Mesh`].
/// Consists of a vertex data buffer and an optional index data buffer. /// Consists of a vertex data buffer and an optional index data buffer.
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
@ -1402,10 +1440,17 @@ pub struct GpuMesh {
pub vertex_count: u32, pub vertex_count: u32,
pub morph_targets: Option<TextureView>, pub morph_targets: Option<TextureView>,
pub buffer_info: GpuBufferInfo, pub buffer_info: GpuBufferInfo,
pub primitive_topology: PrimitiveTopology, pub key_bits: BaseMeshPipelineKey,
pub layout: MeshVertexBufferLayoutRef, pub layout: MeshVertexBufferLayoutRef,
} }
impl GpuMesh {
#[inline]
pub fn primitive_topology(&self) -> PrimitiveTopology {
self.key_bits.primitive_topology()
}
}
/// The index/vertex buffer info of a [`GpuMesh`]. /// The index/vertex buffer info of a [`GpuMesh`].
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub enum GpuBufferInfo { pub enum GpuBufferInfo {
@ -1461,11 +1506,17 @@ impl RenderAsset for Mesh {
let mesh_vertex_buffer_layout = let mesh_vertex_buffer_layout =
self.get_mesh_vertex_buffer_layout(mesh_vertex_buffer_layouts); self.get_mesh_vertex_buffer_layout(mesh_vertex_buffer_layouts);
let mut key_bits = BaseMeshPipelineKey::from_primitive_topology(self.primitive_topology());
key_bits.set(
BaseMeshPipelineKey::MORPH_TARGETS,
self.morph_targets.is_some(),
);
Ok(GpuMesh { Ok(GpuMesh {
vertex_buffer, vertex_buffer,
vertex_count: self.count_vertices() as u32, vertex_count: self.count_vertices() as u32,
buffer_info, buffer_info,
primitive_topology: self.primitive_topology(), key_bits,
layout: mesh_vertex_buffer_layout, layout: mesh_vertex_buffer_layout,
morph_targets: self morph_targets: self
.morph_targets .morph_targets

View file

@ -8,6 +8,7 @@ use crate::{
}, },
}; };
use bevy_ecs::system::Resource; use bevy_ecs::system::Resource;
use bevy_utils::hashbrown::hash_map::VacantEntry;
use bevy_utils::{default, hashbrown::hash_map::RawEntryMut, tracing::error, Entry, HashMap}; use bevy_utils::{default, hashbrown::hash_map::RawEntryMut, tracing::error, Entry, HashMap};
use std::{fmt::Debug, hash::Hash}; use std::{fmt::Debug, hash::Hash};
use thiserror::Error; use thiserror::Error;
@ -84,9 +85,14 @@ pub trait SpecializedMeshPipeline {
#[derive(Resource)] #[derive(Resource)]
pub struct SpecializedMeshPipelines<S: SpecializedMeshPipeline> { pub struct SpecializedMeshPipelines<S: SpecializedMeshPipeline> {
mesh_layout_cache: HashMap<(MeshVertexBufferLayoutRef, S::Key), CachedRenderPipelineId>, mesh_layout_cache: HashMap<(MeshVertexBufferLayoutRef, S::Key), CachedRenderPipelineId>,
vertex_layout_cache: HashMap<VertexBufferLayout, HashMap<S::Key, CachedRenderPipelineId>>, vertex_layout_cache: VertexLayoutCache<S>,
} }
pub type VertexLayoutCache<S> = HashMap<
VertexBufferLayout,
HashMap<<S as SpecializedMeshPipeline>::Key, CachedRenderPipelineId>,
>;
impl<S: SpecializedMeshPipeline> Default for SpecializedMeshPipelines<S> { impl<S: SpecializedMeshPipeline> Default for SpecializedMeshPipelines<S> {
fn default() -> Self { fn default() -> Self {
Self { Self {
@ -105,55 +111,72 @@ impl<S: SpecializedMeshPipeline> SpecializedMeshPipelines<S> {
key: S::Key, key: S::Key,
layout: &MeshVertexBufferLayoutRef, layout: &MeshVertexBufferLayoutRef,
) -> Result<CachedRenderPipelineId, SpecializedMeshPipelineError> { ) -> Result<CachedRenderPipelineId, SpecializedMeshPipelineError> {
match self.mesh_layout_cache.entry((layout.clone(), key.clone())) { return match self.mesh_layout_cache.entry((layout.clone(), key.clone())) {
Entry::Occupied(entry) => Ok(*entry.into_mut()), Entry::Occupied(entry) => Ok(*entry.into_mut()),
Entry::Vacant(entry) => { Entry::Vacant(entry) => specialize_slow(
let descriptor = specialize_pipeline &mut self.vertex_layout_cache,
.specialize(key.clone(), layout) cache,
.map_err(|mut err| { specialize_pipeline,
{ key,
let SpecializedMeshPipelineError::MissingVertexAttribute(err) = layout,
&mut err; entry,
err.pipeline_type = Some(std::any::type_name::<S>()); ),
} };
err
})?; #[cold]
// Different MeshVertexBufferLayouts can produce the same final VertexBufferLayout fn specialize_slow<S>(
// We want compatible vertex buffer layouts to use the same pipelines, so we must "deduplicate" them vertex_layout_cache: &mut VertexLayoutCache<S>,
let layout_map = match self cache: &PipelineCache,
.vertex_layout_cache specialize_pipeline: &S,
.raw_entry_mut() key: S::Key,
.from_key(&descriptor.vertex.buffers[0]) layout: &MeshVertexBufferLayoutRef,
{ entry: VacantEntry<(MeshVertexBufferLayoutRef, S::Key), CachedRenderPipelineId>,
RawEntryMut::Occupied(entry) => entry.into_mut(), ) -> Result<CachedRenderPipelineId, SpecializedMeshPipelineError>
RawEntryMut::Vacant(entry) => { where
entry S: SpecializedMeshPipeline,
.insert(descriptor.vertex.buffers[0].clone(), Default::default()) {
.1 let descriptor = specialize_pipeline
.specialize(key.clone(), layout)
.map_err(|mut err| {
{
let SpecializedMeshPipelineError::MissingVertexAttribute(err) = &mut err;
err.pipeline_type = Some(std::any::type_name::<S>());
} }
}; err
Ok(*entry.insert(match layout_map.entry(key) { })?;
Entry::Occupied(entry) => { // Different MeshVertexBufferLayouts can produce the same final VertexBufferLayout
if cfg!(debug_assertions) { // We want compatible vertex buffer layouts to use the same pipelines, so we must "deduplicate" them
let stored_descriptor = let layout_map = match vertex_layout_cache
cache.get_render_pipeline_descriptor(*entry.get()); .raw_entry_mut()
if stored_descriptor != &descriptor { .from_key(&descriptor.vertex.buffers[0])
error!( {
"The cached pipeline descriptor for {} is not \ RawEntryMut::Occupied(entry) => entry.into_mut(),
equal to the generated descriptor for the given key. \ RawEntryMut::Vacant(entry) => {
This means the SpecializePipeline implementation uses \ entry
unused' MeshVertexBufferLayout information to specialize \ .insert(descriptor.vertex.buffers[0].clone(), Default::default())
the pipeline. This is not allowed because it would invalidate \ .1
the pipeline cache.", }
std::any::type_name::<S>() };
); Ok(*entry.insert(match layout_map.entry(key) {
} Entry::Occupied(entry) => {
if cfg!(debug_assertions) {
let stored_descriptor = cache.get_render_pipeline_descriptor(*entry.get());
if stored_descriptor != &descriptor {
error!(
"The cached pipeline descriptor for {} is not \
equal to the generated descriptor for the given key. \
This means the SpecializePipeline implementation uses \
unused' MeshVertexBufferLayout information to specialize \
the pipeline. This is not allowed because it would invalidate \
the pipeline cache.",
std::any::type_name::<S>()
);
} }
*entry.into_mut()
} }
Entry::Vacant(entry) => *entry.insert(cache.queue_render_pipeline(descriptor)), *entry.into_mut()
})) }
} Entry::Vacant(entry) => *entry.insert(cache.queue_render_pipeline(descriptor)),
}))
} }
} }
} }

View file

@ -426,7 +426,7 @@ pub fn queue_material2d_meshes<M: Material2d>(
continue; continue;
}; };
let mesh_key = let mesh_key =
view_key | Mesh2dPipelineKey::from_primitive_topology(mesh.primitive_topology); view_key | Mesh2dPipelineKey::from_primitive_topology(mesh.primitive_topology());
let pipeline_id = pipelines.specialize( let pipeline_id = pipelines.specialize(
&pipeline_cache, &pipeline_cache,

View file

@ -383,7 +383,7 @@ pub fn queue_colored_mesh2d(
let mut mesh2d_key = mesh_key; let mut mesh2d_key = mesh_key;
if let Some(mesh) = render_meshes.get(mesh2d_handle) { if let Some(mesh) = render_meshes.get(mesh2d_handle) {
mesh2d_key |= mesh2d_key |=
Mesh2dPipelineKey::from_primitive_topology(mesh.primitive_topology); Mesh2dPipelineKey::from_primitive_topology(mesh.primitive_topology());
} }
let pipeline_id = let pipeline_id =

View file

@ -133,7 +133,8 @@ fn queue_custom(
let Some(mesh) = meshes.get(mesh_instance.mesh_asset_id) else { let Some(mesh) = meshes.get(mesh_instance.mesh_asset_id) else {
continue; continue;
}; };
let key = view_key | MeshPipelineKey::from_primitive_topology(mesh.primitive_topology); let key =
view_key | MeshPipelineKey::from_primitive_topology(mesh.primitive_topology());
let pipeline = pipelines let pipeline = pipelines
.specialize(&pipeline_cache, &custom_pipeline, key, &mesh.layout) .specialize(&pipeline_cache, &custom_pipeline, key, &mesh.layout)
.unwrap(); .unwrap();