Skip to content

Commit eb0f4f7

Browse files
dsgallupsHenauxgtychedelia
authored
Fix: Provide CPU mesh processing with MaterialBindingId (#19083)
# Objective Fixes #19027 ## Solution Query for the material binding id if using fallback CPU processing ## Testing I've honestly no clue how to test for this, and I imagine that this isn't entirely failsafe :( but would highly appreciate a suggestion! To verify this works, please run the the texture.rs example using WebGL 2. Additionally, I'm extremely naive about the nuances of pbr. This PR is essentially to kinda *get the ball rolling* of sorts. Thanks :) --------- Co-authored-by: Gilles Henaux <ghx_github_priv@fastmail.com> Co-authored-by: charlotte <charlotte.c.mcelwain@gmail.com>
1 parent ddc9f41 commit eb0f4f7

File tree

1 file changed

+35
-5
lines changed

1 file changed

+35
-5
lines changed

crates/bevy_pbr/src/render/mesh.rs

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -837,12 +837,33 @@ pub struct RenderMeshInstanceGpuQueues(Parallel<RenderMeshInstanceGpuQueue>);
837837
pub struct MeshesToReextractNextFrame(MainEntityHashSet);
838838

839839
impl RenderMeshInstanceShared {
840-
fn from_components(
840+
/// A gpu builder will provide the mesh instance id
841+
/// during [`RenderMeshInstanceGpuBuilder::update`].
842+
fn for_gpu_building(
841843
previous_transform: Option<&PreviousGlobalTransform>,
842844
mesh: &Mesh3d,
843845
tag: Option<&MeshTag>,
844846
not_shadow_caster: bool,
845847
no_automatic_batching: bool,
848+
) -> Self {
849+
Self::for_cpu_building(
850+
previous_transform,
851+
mesh,
852+
tag,
853+
default(),
854+
not_shadow_caster,
855+
no_automatic_batching,
856+
)
857+
}
858+
859+
/// The cpu builder does not have an equivalent [`RenderMeshInstanceGpuBuilder::update`].
860+
fn for_cpu_building(
861+
previous_transform: Option<&PreviousGlobalTransform>,
862+
mesh: &Mesh3d,
863+
tag: Option<&MeshTag>,
864+
material_bindings_index: MaterialBindingId,
865+
not_shadow_caster: bool,
866+
no_automatic_batching: bool,
846867
) -> Self {
847868
let mut mesh_instance_flags = RenderMeshInstanceFlags::empty();
848869
mesh_instance_flags.set(RenderMeshInstanceFlags::SHADOW_CASTER, !not_shadow_caster);
@@ -858,8 +879,7 @@ impl RenderMeshInstanceShared {
858879
RenderMeshInstanceShared {
859880
mesh_asset_id: mesh.id(),
860881
flags: mesh_instance_flags,
861-
// This gets filled in later, during `RenderMeshGpuBuilder::update`.
862-
material_bindings_index: default(),
882+
material_bindings_index,
863883
lightmap_slab_index: None,
864884
tag: tag.map_or(0, |i| **i),
865885
}
@@ -1309,6 +1329,8 @@ pub type ExtractMeshesSet = MeshExtractionSystems;
13091329
/// [`MeshUniform`] building.
13101330
pub fn extract_meshes_for_cpu_building(
13111331
mut render_mesh_instances: ResMut<RenderMeshInstances>,
1332+
mesh_material_ids: Res<RenderMaterialInstances>,
1333+
render_material_bindings: Res<RenderMaterialBindings>,
13121334
render_visibility_ranges: Res<RenderVisibilityRanges>,
13131335
mut render_mesh_instance_queues: Local<Parallel<Vec<(Entity, RenderMeshInstanceCpu)>>>,
13141336
meshes_query: Extract<
@@ -1362,10 +1384,18 @@ pub fn extract_meshes_for_cpu_building(
13621384
transmitted_receiver,
13631385
);
13641386

1365-
let shared = RenderMeshInstanceShared::from_components(
1387+
let mesh_material = mesh_material_ids.mesh_material(MainEntity::from(entity));
1388+
1389+
let material_bindings_index = render_material_bindings
1390+
.get(&mesh_material)
1391+
.copied()
1392+
.unwrap_or_default();
1393+
1394+
let shared = RenderMeshInstanceShared::for_cpu_building(
13661395
previous_transform,
13671396
mesh,
13681397
tag,
1398+
material_bindings_index,
13691399
not_shadow_caster,
13701400
no_automatic_batching,
13711401
);
@@ -1570,7 +1600,7 @@ fn extract_mesh_for_gpu_building(
15701600
transmitted_receiver,
15711601
);
15721602

1573-
let shared = RenderMeshInstanceShared::from_components(
1603+
let shared = RenderMeshInstanceShared::for_gpu_building(
15741604
previous_transform,
15751605
mesh,
15761606
tag,

0 commit comments

Comments
 (0)