bevy/assets/shaders
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
..
animate_shader.wgsl improve shader import model (#5703) 2023-06-27 00:29:22 +00:00
array_texture.wgsl fix prepass normal_mapping (#8978) 2023-06-29 00:28:34 +00:00
cubemap_unlit.wgsl improve shader import model (#5703) 2023-06-27 00:29:22 +00:00
custom_gltf_2d.wgsl improve shader import model (#5703) 2023-06-27 00:29:22 +00:00
custom_material.frag improve shader import model (#5703) 2023-06-27 00:29:22 +00:00
custom_material.vert custom_material.vert: gl_InstanceIndex includes gl_BaseInstance (#9326) 2023-08-01 08:02:16 +00:00
custom_material.wgsl improve shader import model (#5703) 2023-06-27 00:29:22 +00:00
custom_material_screenspace_texture.wgsl improve shader import model (#5703) 2023-06-27 00:29:22 +00:00
custom_vertex_attribute.wgsl Reduce the size of MeshUniform to improve performance (#9416) 2023-08-15 06:00:23 +00:00
fallback_image_test.wgsl Fix fallback_image example (#8968) 2023-06-27 18:08:02 +00:00
game_of_life.wgsl Remove unused code in game of life shader (#5349) 2022-07-17 15:24:24 +00:00
instancing.wgsl Reduce the size of MeshUniform to improve performance (#9416) 2023-08-15 06:00:23 +00:00
line_material.wgsl improve shader import model (#5703) 2023-06-27 00:29:22 +00:00
post_processing.wgsl Fix post_processing example on webgl2 (#9361) 2023-08-04 17:44:29 +00:00
shader_defs.wgsl improve shader import model (#5703) 2023-06-27 00:29:22 +00:00
show_prepass.wgsl improve shader import model (#5703) 2023-06-27 00:29:22 +00:00
texture_binding_array.wgsl improve shader import model (#5703) 2023-06-27 00:29:22 +00:00
tonemapping_test_patterns.wgsl Include tone_mapping fn in tonemapping_test_patterns (#9084) 2023-07-31 18:59:04 +00:00