bevy/crates/bevy_render/src
Robert Swain 0a11af9375
Reduce the size of MeshUniform to improve performance (#9416)
# Objective

- Significantly reduce the size of MeshUniform by only including
necessary data.

## Solution

Local to world, model transforms are affine. This means they only need a
4x3 matrix to represent them.

`MeshUniform` stores the current, and previous model transforms, and the
inverse transpose of the current model transform, all as 4x4 matrices.
Instead we can store the current, and previous model transforms as 4x3
matrices, and we only need the upper-left 3x3 part of the inverse
transpose of the current model transform. This change allows us to
reduce the serialized MeshUniform size from 208 bytes to 144 bytes,
which is over a 30% saving in data to serialize, and VRAM bandwidth and
space.

## Benchmarks

On an M1 Max, running `many_cubes -- sphere`, main is in yellow, this PR
is in red:
<img width="1484" alt="Screenshot 2023-08-11 at 02 36 43"
src="https://github.com/bevyengine/bevy/assets/302146/7d99c7b3-f2bb-4004-a8d0-4c00f755cb0d">
A reduction in frame time of ~14%.

---

## Changelog

- Changed: Redefined `MeshUniform` to improve performance by using 4x3
affine transforms and reconstructing 4x4 matrices in the shader. Helper
functions were added to `bevy_pbr::mesh_functions` to unpack the data.
`affine_to_square` converts the packed 4x3 in 3x4 matrix data to a 4x4
matrix. `mat2x4_f32_to_mat3x3` converts the 3x3 in mat2x4 + f32 matrix
data back into a 3x3.

## Migration Guide

Shader code before:
```
var model = mesh[instance_index].model;
```

Shader code after:
```
#import bevy_pbr::mesh_functions affine_to_square

var model = affine_to_square(mesh[instance_index].model);
```
2023-08-15 06:00:23 +00:00
..
camera Document when Camera::viewport_to_world and related methods return None (#8841) 2023-08-03 00:12:44 +00:00
color bevy_reflect: FromReflect Ergonomics Implementation (#6056) 2023-06-29 01:31:34 +00:00
mesh fix typo in a link - Mesh docs (#9329) 2023-08-01 21:03:16 +00:00
primitives Reduce the size of MeshUniform to improve performance (#9416) 2023-08-15 06:00:23 +00:00
render_graph Add ViewNode to simplify render node management (#8118) 2023-05-08 19:42:23 +00:00
render_phase Reduce the size of MeshUniform to improve performance (#9416) 2023-08-15 06:00:23 +00:00
render_resource include toplevel shader-associated defs (#9343) 2023-08-03 09:12:31 +00:00
renderer Refs #8975 -- Add return to RenderDevice::poll() (#8977) 2023-06-28 01:05:03 +00:00
texture allow asset loader pre-registration (#9429) 2023-08-14 21:27:51 +00:00
view Reduce the size of MeshUniform to improve performance (#9416) 2023-08-15 06:00:23 +00:00
extract_component.rs Schedule-First: the new and improved add_systems (#8079) 2023-03-18 01:45:34 +00:00
extract_param.rs Use UnsafeWorldCell to increase code quality for SystemParam (#8174) 2023-04-01 15:45:07 +00:00
extract_resource.rs Schedule-First: the new and improved add_systems (#8079) 2023-03-18 01:45:34 +00:00
globals.rs Webgpu support (#8336) 2023-05-04 22:07:57 +00:00
globals.wgsl Refactor Globals and View structs into separate shaders (#7512) 2023-02-11 17:55:18 +00:00
gpu_component_array_buffer.rs Use GpuArrayBuffer for MeshUniform (#9254) 2023-07-30 13:17:08 +00:00
instance_index.wgsl Work around naga/wgpu WGSL instance_index -> GLSL gl_InstanceID bug on WebGL2 (#9383) 2023-08-09 18:38:45 +00:00
lib.rs Work around naga/wgpu WGSL instance_index -> GLSL gl_InstanceID bug on WebGL2 (#9383) 2023-08-09 18:38:45 +00:00
pipelined_rendering.rs Stop using unwrap in the pipelined rendering thread (#9052) 2023-07-23 01:06:25 +00:00
render_asset.rs Schedule-First: the new and improved add_systems (#8079) 2023-03-18 01:45:34 +00:00
settings.rs Webgpu support (#8336) 2023-05-04 22:07:57 +00:00
spatial_bundle.rs enum Visibility component (#6320) 2022-12-25 00:39:29 +00:00