Skip to content
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

Add option to animate materials in many_cubes #17927

Merged
merged 2 commits into from
Feb 18, 2025

Conversation

DGriffin91
Copy link
Contributor

This adds an option to animate the materials in the many_cubes stress test. Each material instance base_color is varied each frame.

This has been tested in conjunction with the --vary-material-data-per-instance and --material-texture-count options.

If --vary-material-data-per-instance is not used it will just update the single material, otherwise it will update all of them. If --material-texture-count is used the base_color is multiplied with the texture so the effect is still visible.

Because this test is focused on the performance of updating material data and not the performance of bevy's color system it uses its own function (fast_hue_to_rgb) to quickly set the hue. This appeared to be around 8x faster than using base_color.set_hue(hue) in the tight loop.

@IceSentry IceSentry added A-Rendering Drawing game state to the screen C-Performance A change motivated by improving speed, memory usage or compile times S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Feb 18, 2025
@superdump superdump added this pull request to the merge queue Feb 18, 2025
Merged via the queue into bevyengine:main with commit c818c92 Feb 18, 2025
32 checks passed
@TimJentzsch TimJentzsch added the A-Animation Make things move and change over time label Feb 19, 2025
github-merge-queue bot pushed a commit that referenced this pull request Feb 20, 2025
# Objective

Fix incorrect mesh culling where objects (particularly directional
shadows) were being incorrectly culled during the early preprocessing
phase. The issue manifested specifically on Apple M1 GPUs but not on
newer devices like the M4. The bug was in the
`view_frustum_intersects_obb` function, where including the w component
(plane distance) in the dot product calculations led to false positive
culling results. This caused objects to be incorrectly culled before
shadow casting could begin.

## Issue Details
The problem of missing shadows is reproducible on Apple M1 GPUs as of
this commit (bisected):

```
00722b8 Make indirect drawing opt-out instead of opt-in, enabling multidraw by default. (#16757)
```

and as recent as this commit:

```
c818c92 Add option to animate materials in many_cubes (#17927)
```

- The frustum culling calculation incorrectly included the w component
(plane distance) when transforming basis vectors
- The relative radius calculation should only consider directional
transformation (xyz), not positional information (w)
- This caused false positive culling specifically on M1 devices likely
due to different device-specific floating-point behavior
- When objects were incorrectly culled, `early_instance_count` never
incremented, leading to missing geometry in the shadow pass

## Testing

- Tested on M1 and M4 devices to verify the fix
- Verified shadows and geometry render correctly on both platforms
- Confirmed the solution matches the existing Rust implementation's
behavior for calculating the relative radius:
https://github.com/bevyengine/bevy/blob/c818c92143e56ef3b51836af423319a5a61b15ad/crates/bevy_render/src/primitives/mod.rs#L77-L87
- The fix resolves a mathematical error in the frustum culling
calculation while maintaining correct culling behavior across all
platforms.

---

## Showcase

`c818c9214`
<img width="1284" alt="c818c9214"
src="https://github.com/user-attachments/assets/fe1c7ea9-b13d-422e-b12d-f1cd74475213"
/>

`mate-h/frustum-cull-fix`
<img width="1283" alt="frustum-cull-fix"
src="https://github.com/user-attachments/assets/8a9ccb2a-64b6-4d5e-a17d-ac4798da5b51"
/>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Animation Make things move and change over time A-Rendering Drawing game state to the screen C-Performance A change motivated by improving speed, memory usage or compile times S-Needs-Review Needs reviewer attention (from anyone!) to move forward
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

4 participants