Skip to content

Commit 7342d46

Browse files
committed
Use a sorted Map for vertex buffer attributes (#1796)
The `VertexBufferLayout` returned by `crates\bevy_render\src\mesh\mesh.rs:308` was unstable, because `HashMap.iter()` has a random order. This caused the pipeline_compiler to wrongly consider a specialization to be different (`crates\bevy_render\src\pipeline\pipeline_compiler.rs:123`), causing each mesh changed event to potentially result in a different `PipelineSpecialization`. This in turn caused `Draw` to emit a `set_pipeline` much more often than needed. This fix shaves off a `BindPipeline` and two `BindDescriptorSets` (for the Camera and for global renderresources) for every mesh after the first that can now use the same specialization, where it didn't before (which was random). `StableHashMap` was not a good replacement, because it isn't `Clone`, so instead I replaced it with a `BTreeMap` which is OK in this instance, because there shouldn't be many insertions on `Mesh.attributes` after the mesh is created.
1 parent 4c1099a commit 7342d46

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

crates/bevy_render/src/mesh/mesh.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use bevy_ecs::{
1313
};
1414
use bevy_math::*;
1515
use bevy_reflect::TypeUuid;
16-
use std::borrow::Cow;
16+
use std::{borrow::Cow, collections::BTreeMap};
1717

1818
use crate::pipeline::{InputStepMode, VertexAttribute, VertexBufferLayout};
1919
use bevy_utils::{HashMap, HashSet};
@@ -208,9 +208,11 @@ impl From<&Indices> for IndexFormat {
208208
#[uuid = "8ecbac0f-f545-4473-ad43-e1f4243af51e"]
209209
pub struct Mesh {
210210
primitive_topology: PrimitiveTopology,
211-
/// `bevy_utils::HashMap` with all defined vertex attributes (Positions, Normals, ...) for this
211+
/// `std::collections::BTreeMap` with all defined vertex attributes (Positions, Normals, ...) for this
212212
/// mesh. Attribute name maps to attribute values.
213-
attributes: HashMap<Cow<'static, str>, VertexAttributeValues>,
213+
/// Uses a BTreeMap because, unlike HashMap, it has a defined iteration order,
214+
/// which allows easy stable VertexBuffers (i.e. same buffer order)
215+
attributes: BTreeMap<Cow<'static, str>, VertexAttributeValues>,
214216
indices: Option<Indices>,
215217
}
216218

0 commit comments

Comments
 (0)