bevy/crates/bevy_render/src
Nicola Papale 79021c78c6
Fix perf degradation on web builds (#11227)
# Objective

- Since #10702, the way bevy updates the window leads to major slowdowns
as seen in
    - #11122 
    - #11220
- Slow is bad, furthermore, _very_ slow is _very_ bad. We should fix
this issue.

## Solution

- Move the app update code into the `Event::WindowEvent { event:
WindowEvent::RedrawRequested }` branch of the event loop.
- Run `window.request_redraw()` When `runner_state.redraw_requested`
- Instead of swapping `ControlFlow` between `Poll` and `Wait`, we always
keep it at `Wait`, and use `window.request_redraw()` to schedule an
immediate call to the event loop.
- `runner_state.redraw_requested` is set to `true` when
`UpdateMode::Continuous` and when a `RequestRedraw` event is received.
- Extract the redraw code into a separate function, because otherwise
I'd go crazy with the indentation level.
- Fix #11122.

## Testing

I tested the WASM builds as follow:

```sh
cargo run -p build-wasm-example -- --api webgl2 bevymark
python -m http.server --directory examples/wasm/ 8080
# Open browser at http://localhost:8080
```

On main, even spawning a couple sprites is super choppy. Even if it says
"300 FPS". While on this branch, it is smooth as butter.

I also found that it fixes all choppiness on window resize (tested on
Linux/X11). This was another issue from #10702 IIRC.

So here is what I tested:

- On `wasm`: `many_foxes` and `bevymark`, with `argh::from_env()`
commented out, otherwise we get a cryptic error.
- Both with `PresentMode::AutoVsync` and `PresentMode::AutoNoVsync`
  - On main, it is consistently choppy.
- With this PR, the visible frame rate is consistent with the diagnostic
numbers
- On native (linux/x11) I ran similar tests, making sure that
`AutoVsync` limits to monitor framerate, and `AutoNoVsync` doesn't.

## Future work

Code could be improved, I wanted a quick solution easy to review, but we
really need to make the code more accessible.

- #9768
- ~~**`WinitSettings::desktop_app()` is completely borked.**~~ actually
broken on main as well

### Review guide

Consider enable the non-whitespace diff to see the _real_ change set.
2024-01-06 19:40:13 +00:00
..
batching Rename WorldQueryData & WorldQueryFilter to QueryData & QueryFilter (#10779) 2023-12-12 19:45:50 +00:00
camera Explain Camera physical size is in pixel (#11189) 2024-01-04 18:28:36 +00:00
color Replace deprecated elements (#10991) 2023-12-16 02:25:12 +00:00
mesh assets should be kept on CPU by default (#11212) 2024-01-05 05:53:47 +00:00
primitives Add a method to compute a bounding box enclosing a set of points (#9630) 2023-08-31 01:33:13 +00:00
render_graph Reorder impl to be the same as the trait (#11076) 2023-12-24 17:43:55 +00:00
render_phase Replace or document ignored doctests (#11040) 2024-01-01 16:50:56 +00:00
render_resource Unload render assets from RAM (#10520) 2024-01-03 03:31:04 +00:00
renderer Fix perf degradation on web builds (#11227) 2024-01-06 19:40:13 +00:00
texture assets should be kept on CPU by default (#11212) 2024-01-05 05:53:47 +00:00
view Unload render assets from RAM (#10520) 2024-01-03 03:31:04 +00:00
extract_component.rs Rename WorldQueryData & WorldQueryFilter to QueryData & QueryFilter (#10779) 2023-12-12 19:45:50 +00:00
extract_instances.rs Rename WorldQueryData & WorldQueryFilter to QueryData & QueryFilter (#10779) 2023-12-12 19:45:50 +00:00
extract_param.rs Replace or document ignored doctests (#11040) 2024-01-01 16:50:56 +00:00
extract_resource.rs Allow optional extraction of resources from the main world (#10109) 2023-10-14 16:07:49 +00:00
globals.rs Unify FixedTime and Time while fixing several problems (#8964) 2023-10-16 01:57:55 +00:00
globals.wgsl
gpu_component_array_buffer.rs Reorder render sets, refactor bevy_sprite to take advantage (#9236) 2023-08-27 14:33:49 +00:00
instance_index.wgsl
lib.rs Keep track of when a texture is first cleared (#10325) 2023-12-31 00:37:37 +00:00
maths.wgsl Use instancing for sprites (#9597) 2023-09-02 18:03:19 +00:00
pipelined_rendering.rs Remove unnecessary path prefixes (#10749) 2023-11-28 23:43:40 +00:00
render_asset.rs assets should be kept on CPU by default (#11212) 2024-01-05 05:53:47 +00:00
settings.rs Update to wgpu 0.18 (#10266) 2023-12-14 02:45:47 +00:00
spatial_bundle.rs Implement Clone for VisibilityBundle and SpatialBundle (#10394) 2023-11-07 21:25:00 +00:00