Skip to content

Fix: Provide CPU mesh processing with MaterialBindingId #19083

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 6 commits into from
May 12, 2025
Merged
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
40 changes: 35 additions & 5 deletions crates/bevy_pbr/src/render/mesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -837,12 +837,33 @@ pub struct RenderMeshInstanceGpuQueues(Parallel<RenderMeshInstanceGpuQueue>);
pub struct MeshesToReextractNextFrame(MainEntityHashSet);

impl RenderMeshInstanceShared {
fn from_components(
/// A gpu builder will provide the mesh instance id
/// during [`RenderMeshInstanceGpuBuilder::update`].
fn for_gpu_building(
previous_transform: Option<&PreviousGlobalTransform>,
mesh: &Mesh3d,
tag: Option<&MeshTag>,
not_shadow_caster: bool,
no_automatic_batching: bool,
) -> Self {
Self::for_cpu_building(
previous_transform,
mesh,
tag,
default(),
not_shadow_caster,
no_automatic_batching,
)
}

/// The cpu builder does not have an equivalent [`RenderMeshInstanceGpuBuilder::update`].
fn for_cpu_building(
previous_transform: Option<&PreviousGlobalTransform>,
mesh: &Mesh3d,
tag: Option<&MeshTag>,
material_bindings_index: MaterialBindingId,
not_shadow_caster: bool,
no_automatic_batching: bool,
) -> Self {
let mut mesh_instance_flags = RenderMeshInstanceFlags::empty();
mesh_instance_flags.set(RenderMeshInstanceFlags::SHADOW_CASTER, !not_shadow_caster);
Expand All @@ -858,8 +879,7 @@ impl RenderMeshInstanceShared {
RenderMeshInstanceShared {
mesh_asset_id: mesh.id(),
flags: mesh_instance_flags,
// This gets filled in later, during `RenderMeshGpuBuilder::update`.
material_bindings_index: default(),
material_bindings_index,
lightmap_slab_index: None,
tag: tag.map_or(0, |i| **i),
}
Expand Down Expand Up @@ -1309,6 +1329,8 @@ pub type ExtractMeshesSet = MeshExtractionSystems;
/// [`MeshUniform`] building.
pub fn extract_meshes_for_cpu_building(
mut render_mesh_instances: ResMut<RenderMeshInstances>,
mesh_material_ids: Res<RenderMaterialInstances>,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: these appear to be correctly ordered via the constraints on late_sweep_material_instances.

render_material_bindings: Res<RenderMaterialBindings>,
render_visibility_ranges: Res<RenderVisibilityRanges>,
mut render_mesh_instance_queues: Local<Parallel<Vec<(Entity, RenderMeshInstanceCpu)>>>,
meshes_query: Extract<
Expand Down Expand Up @@ -1362,10 +1384,18 @@ pub fn extract_meshes_for_cpu_building(
transmitted_receiver,
);

let shared = RenderMeshInstanceShared::from_components(
let mesh_material = mesh_material_ids.mesh_material(MainEntity::from(entity));

let material_bindings_index = render_material_bindings
.get(&mesh_material)
.copied()
.unwrap_or_default();

let shared = RenderMeshInstanceShared::for_cpu_building(
previous_transform,
mesh,
tag,
material_bindings_index,
not_shadow_caster,
no_automatic_batching,
);
Expand Down Expand Up @@ -1570,7 +1600,7 @@ fn extract_mesh_for_gpu_building(
transmitted_receiver,
);

let shared = RenderMeshInstanceShared::from_components(
let shared = RenderMeshInstanceShared::for_gpu_building(
previous_transform,
mesh,
tag,
Expand Down