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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

dsgallups
Copy link

@dsgallups dsgallups commented May 5, 2025

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 :)

Copy link
Contributor

github-actions bot commented May 5, 2025

Welcome, new contributor!

Please make sure you've read our contributing guide and we look forward to reviewing your pull request shortly ✨

@dsgallups dsgallups changed the title Fix: Provide CPU processing with MaterialBindingId Fix: Provide CPU mesh processing with MaterialBindingId May 5, 2025
@alice-i-cecile alice-i-cecile added this to the 0.16.1 milestone May 5, 2025
@alice-i-cecile alice-i-cecile added C-Bug An unexpected or incorrect behavior A-Rendering Drawing game state to the screen O-WebGL2 Specific to the WebGL2 render API labels May 5, 2025
@alice-i-cecile alice-i-cecile requested a review from mockersf May 5, 2025 19:26
@alice-i-cecile alice-i-cecile added S-Needs-Review Needs reviewer attention (from anyone!) to move forward P-High This is particularly urgent, and deserves immediate attention labels May 5, 2025
.unwrap_or_default()
})
.unwrap_or_default();

let shared = RenderMeshInstanceShared::from_components(
Copy link
Contributor

Choose a reason for hiding this comment

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

Instead of supercharging the from_components method with the material_bindings_index, it may be clearer to introduce a new method (for_cpu_building or from_components_and_material_binding_index or with_material_binding_index, ...) that takes the material_bindings_index as an additional parameter, and use this new method here.
The previous from_components would just internally call this new method and give it default() for the material_bindings_index. This version of from_components could be renamed to for_gpu_building or without_material_binding_index.

This way the comment about overriding the binding index would have its own place in the without_material_binding_index method specifically.

Copy link
Author

Choose a reason for hiding this comment

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

Ok, I've followed these comments. Can you verify this is what you described? Thanks!

dsgallups and others added 3 commits May 6, 2025 12:19
Using @Henauxg's request to create a new constructor for `RenderMeshInstanceShared`.

The `RenderMeshInstanceShare::from_components` now calls this method with default index id.
Added comments that appear correct on the surface. I'm *pretty sure* this is accurate.
Comment on lines +864 to +869
/// Unlike the gpu builder, the cpu builder does not have an equivalent
/// [`RenderMeshGpuBuilder::update`]. Therefore, we need to have
/// the [`MaterialBindingId`] ahead of time.
///
/// Otherwise, a mesh with a handle to a base color texture will all appear identical
/// based on the most recently inserted mesh.
Copy link
Author

Choose a reason for hiding this comment

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

Need a verification that this comment is true. That there really isn't an equivalent...and if there isn't, well...I need to find out

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't really think we need to document what will happen if this code doesn't work. In general, I think these comments might be saying a bit too much. It's important to document on the gpu side that this will be updated later but its's clear from the usage at the call site that we retrieve the binding id ahead of time.

Comment on lines +1395 to +1402
let material_bindings_index = (mesh_material != DUMMY_MESH_MATERIAL.untyped())
.then(|| {
render_material_bindings
.get(&mesh_material)
.copied()
.unwrap_or_default()
})
.unwrap_or_default();
Copy link
Author

Choose a reason for hiding this comment

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

should we be unwrap_or_defaulting here?

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not really sure what the check for DUMMY_MESH_MATERIAL is doing here, is it's presumably already effectively covered by the unwrap_or_default when you call get on render_material_bindings -- if a dummy asset is being used we'll default there, which is the right call.

Copy link
Contributor

@tychedelia tychedelia left a comment

Choose a reason for hiding this comment

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

Just some small comments, otherwise lgtm!

/// during `RenderMeshGpuBuilder::update`.
///
/// Therefore, this does not need to be queried.
#[inline]
Copy link
Contributor

Choose a reason for hiding this comment

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

Comment on lines +864 to +869
/// Unlike the gpu builder, the cpu builder does not have an equivalent
/// [`RenderMeshGpuBuilder::update`]. Therefore, we need to have
/// the [`MaterialBindingId`] ahead of time.
///
/// Otherwise, a mesh with a handle to a base color texture will all appear identical
/// based on the most recently inserted mesh.
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't really think we need to document what will happen if this code doesn't work. In general, I think these comments might be saying a bit too much. It's important to document on the gpu side that this will be updated later but its's clear from the usage at the call site that we retrieve the binding id ahead of time.

Comment on lines +1395 to +1402
let material_bindings_index = (mesh_material != DUMMY_MESH_MATERIAL.untyped())
.then(|| {
render_material_bindings
.get(&mesh_material)
.copied()
.unwrap_or_default()
})
.unwrap_or_default();
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not really sure what the check for DUMMY_MESH_MATERIAL is doing here, is it's presumably already effectively covered by the unwrap_or_default when you call get on render_material_bindings -- if a dummy asset is being used we'll default there, which is the right call.

@@ -837,12 +837,43 @@ pub struct RenderMeshInstanceGpuQueues(Parallel<RenderMeshInstanceGpuQueue>);
pub struct MeshesToReextractNextFrame(MainEntityHashSet);

impl RenderMeshInstanceShared {
fn from_components(
/// A gpu builder will provide the mesh instance id
/// during `RenderMeshGpuBuilder::update`.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
/// during `RenderMeshGpuBuilder::update`.
/// during `RenderMeshInstanceGpuBuilder::update`.

@@ -1305,6 +1335,8 @@ pub struct ExtractMeshesSet;
/// [`MeshUniform`] building.
pub fn extract_meshes_for_cpu_building(
mut render_mesh_instances: ResMut<RenderMeshInstances>,
mesh_material_ids: Res<RenderMaterialInstances>,
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Rendering Drawing game state to the screen C-Bug An unexpected or incorrect behavior O-WebGL2 Specific to the WebGL2 render API P-High This is particularly urgent, and deserves immediate attention S-Needs-Review Needs reviewer attention (from anyone!) to move forward
Projects
Status: No status
Development

Successfully merging this pull request may close these issues.

WebGPU Example 3D Rendering / Texture has error in the WebGL2 version
4 participants