diff --git a/crates/bevy_pbr/src/render/mesh.rs b/crates/bevy_pbr/src/render/mesh.rs index ceebb03e665c0..f3116f797dfca 100644 --- a/crates/bevy_pbr/src/render/mesh.rs +++ b/crates/bevy_pbr/src/render/mesh.rs @@ -370,12 +370,15 @@ bitflags::bitflags! { const VERTEX_TANGENTS = (1 << 0); const TRANSPARENT_MAIN_PASS = (1 << 1); const MSAA_RESERVED_BITS = MeshPipelineKey::MSAA_MASK_BITS << MeshPipelineKey::MSAA_SHIFT_BITS; + const PRIMITIVE_TOPOLOGY_RESERVED_BITS = MeshPipelineKey::PRIMITIVE_TOPOLOGY_MASK_BITS << MeshPipelineKey::PRIMITIVE_TOPOLOGY_SHIFT_BITS; } } impl MeshPipelineKey { const MSAA_MASK_BITS: u32 = 0b111111; const MSAA_SHIFT_BITS: u32 = 32 - 6; + const PRIMITIVE_TOPOLOGY_MASK_BITS: u32 = 0b111; + const PRIMITIVE_TOPOLOGY_SHIFT_BITS: u32 = Self::MSAA_SHIFT_BITS - 3; pub fn from_msaa_samples(msaa_samples: u32) -> Self { let msaa_bits = ((msaa_samples - 1) & Self::MSAA_MASK_BITS) << Self::MSAA_SHIFT_BITS; @@ -385,6 +388,26 @@ impl MeshPipelineKey { pub fn msaa_samples(&self) -> u32 { ((self.bits >> Self::MSAA_SHIFT_BITS) & Self::MSAA_MASK_BITS) + 1 } + + 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; + MeshPipelineKey::from_bits(primitive_topology_bits).unwrap() + } + + 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(), + } + } } impl SpecializedPipeline for MeshPipeline { @@ -496,7 +519,7 @@ impl SpecializedPipeline for MeshPipeline { polygon_mode: PolygonMode::Fill, clamp_depth: false, conservative: false, - topology: PrimitiveTopology::TriangleList, + topology: key.primitive_topology(), strip_index_format: None, }, depth_stencil: Some(DepthStencilState { diff --git a/crates/bevy_pbr/src/render/mod.rs b/crates/bevy_pbr/src/render/mod.rs index 090a2a978435c..2bad31a8e9f0e 100644 --- a/crates/bevy_pbr/src/render/mod.rs +++ b/crates/bevy_pbr/src/render/mod.rs @@ -269,6 +269,8 @@ pub fn queue_meshes( if mesh.has_tangents { pbr_key.mesh_key |= MeshPipelineKey::VERTEX_TANGENTS; } + pbr_key.mesh_key |= + MeshPipelineKey::from_primitive_topology(mesh.primitive_topology); } if let AlphaMode::Blend = material.alpha_mode { diff --git a/crates/bevy_render/src/mesh/mesh/mod.rs b/crates/bevy_render/src/mesh/mesh/mod.rs index 1c82fdf1eed53..1fda3f0ebf36b 100644 --- a/crates/bevy_render/src/mesh/mesh/mod.rs +++ b/crates/bevy_render/src/mesh/mesh/mod.rs @@ -593,6 +593,7 @@ pub struct GpuMesh { pub vertex_buffer: Buffer, pub index_info: Option, pub has_tangents: bool, + pub primitive_topology: PrimitiveTopology, } /// The index info of a [`GpuMesh`]. @@ -640,6 +641,7 @@ impl RenderAsset for Mesh { vertex_buffer, index_info, has_tangents: mesh.attributes.contains_key(Mesh::ATTRIBUTE_TANGENT), + primitive_topology: mesh.primitive_topology(), }) } }