Skip to content

Use SmallVec instead of HashMap in MaterialProperties #19846

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 26 additions & 19 deletions crates/bevy_pbr/src/material.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1300,8 +1300,11 @@ pub struct MaterialProperties {
pub reads_view_transmission_texture: bool,
pub render_phase_type: RenderPhaseType,
pub material_layout: Option<BindGroupLayout>,
pub draw_functions: HashMap<InternedDrawFunctionLabel, DrawFunctionId>,
pub shaders: HashMap<InternedShaderLabel, Handle<Shader>>,
/// Backing array is a size of 4 because the `StandardMaterial` needs 4 draw functions by default
pub draw_functions: SmallVec<[(InternedDrawFunctionLabel, DrawFunctionId); 4]>,
/// Backing array is a size of 3 because the `StandardMaterial` has 3 custom shaders (`frag`, `prepass_frag`, `deferred_frag`) which is the
/// most common use case
pub shaders: SmallVec<[(InternedShaderLabel, Handle<Shader>); 3]>,
/// Whether this material *actually* uses bindless resources, taking the
/// platform support (or lack thereof) of bindless resources into account.
pub bindless: bool,
Expand All @@ -1320,27 +1323,31 @@ pub struct MaterialProperties {

impl MaterialProperties {
pub fn get_shader(&self, label: impl ShaderLabel) -> Option<Handle<Shader>> {
self.shaders.get(&label.intern()).cloned()
self.shaders
.iter()
.find(|(inner_label, _)| inner_label == &label.intern())
.map(|(_, shader)| shader)
.cloned()
}

pub fn add_shader(
&mut self,
label: impl ShaderLabel,
shader: Handle<Shader>,
) -> Option<Handle<Shader>> {
self.shaders.insert(label.intern(), shader)
pub fn add_shader(&mut self, label: impl ShaderLabel, shader: Handle<Shader>) {
self.shaders.push((label.intern(), shader));
}

pub fn get_draw_function(&self, label: impl DrawFunctionLabel) -> Option<DrawFunctionId> {
self.draw_functions.get(&label.intern()).copied()
self.draw_functions
.iter()
.find(|(inner_label, _)| inner_label == &label.intern())
.map(|(_, shader)| shader)
.cloned()
}

pub fn add_draw_function(
&mut self,
label: impl DrawFunctionLabel,
draw_function: DrawFunctionId,
) -> Option<DrawFunctionId> {
self.draw_functions.insert(label.intern(), draw_function)
) {
self.draw_functions.push((label.intern(), draw_function));
}
}

Expand Down Expand Up @@ -1472,27 +1479,27 @@ where
_ => None,
};

let mut draw_functions = HashMap::new();
draw_functions.insert(MaterialDrawFunction.intern(), draw_function_id);
let mut draw_functions = SmallVec::new();
draw_functions.push((MaterialDrawFunction.intern(), draw_function_id));
if let Some(prepass_draw_function_id) = prepass_draw_function_id {
draw_functions.insert(PrepassDrawFunction.intern(), prepass_draw_function_id);
draw_functions.push((PrepassDrawFunction.intern(), prepass_draw_function_id));
}
if let Some(deferred_draw_function_id) = deferred_draw_function_id {
draw_functions.insert(DeferredDrawFunction.intern(), deferred_draw_function_id);
draw_functions.push((DeferredDrawFunction.intern(), deferred_draw_function_id));
}
if let Some(shadow_draw_function_id) = shadow_draw_function_id {
draw_functions.insert(ShadowsDrawFunction.intern(), shadow_draw_function_id);
draw_functions.push((ShadowsDrawFunction.intern(), shadow_draw_function_id));
}

let mut shaders = HashMap::new();
let mut shaders = SmallVec::new();
let mut add_shader = |label: InternedShaderLabel, shader_ref: ShaderRef| {
let mayber_shader = match shader_ref {
ShaderRef::Default => None,
ShaderRef::Handle(handle) => Some(handle),
ShaderRef::Path(path) => Some(asset_server.load(path)),
};
if let Some(shader) = mayber_shader {
shaders.insert(label, shader);
shaders.push((label, shader));
}
};
add_shader(MaterialVertexShader.intern(), M::vertex_shader());
Expand Down
15 changes: 3 additions & 12 deletions crates/bevy_pbr/src/prepass/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -553,27 +553,18 @@ impl PrepassPipelineInternal {
|| emulate_unclipped_depth
|| (mesh_key.contains(MeshPipelineKey::MAY_DISCARD)
&& material_properties
.shaders
.get(&PrepassFragmentShader.intern())
.get_shader(PrepassFragmentShader)
.is_some());

let fragment = fragment_required.then(|| {
// Use the fragment shader from the material
let frag_shader_handle = if mesh_key.contains(MeshPipelineKey::DEFERRED_PREPASS) {
match material_properties
.shaders
.get(&DeferredFragmentShader.intern())
.cloned()
{
match material_properties.get_shader(DeferredFragmentShader) {
Some(frag_shader_handle) => frag_shader_handle,
None => self.default_prepass_shader.clone(),
}
} else {
match material_properties
.shaders
.get(&PrepassFragmentShader.intern())
.cloned()
{
match material_properties.get_shader(PrepassFragmentShader) {
Some(frag_shader_handle) => frag_shader_handle,
None => self.default_prepass_shader.clone(),
}
Expand Down