2023-10-21 11:51:58 +00:00
|
|
|
#import bevy_pbr::mesh_functions::{get_model_matrix, mesh_position_local_to_clip}
|
2022-06-14 00:32:33 +00:00
|
|
|
|
2022-01-05 19:43:11 +00:00
|
|
|
struct Vertex {
|
2022-07-14 21:17:16 +00:00
|
|
|
@location(0) position: vec3<f32>,
|
|
|
|
@location(1) normal: vec3<f32>,
|
|
|
|
@location(2) uv: vec2<f32>,
|
2022-01-05 19:43:11 +00:00
|
|
|
|
2022-07-14 21:17:16 +00:00
|
|
|
@location(3) i_pos_scale: vec4<f32>,
|
|
|
|
@location(4) i_color: vec4<f32>,
|
2022-01-05 19:43:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct VertexOutput {
|
2022-07-14 21:17:16 +00:00
|
|
|
@builtin(position) clip_position: vec4<f32>,
|
|
|
|
@location(0) color: vec4<f32>,
|
2022-01-05 19:43:11 +00:00
|
|
|
};
|
|
|
|
|
2022-07-14 21:17:16 +00:00
|
|
|
@vertex
|
2022-01-05 19:43:11 +00:00
|
|
|
fn vertex(vertex: Vertex) -> VertexOutput {
|
|
|
|
let position = vertex.position * vertex.i_pos_scale.w + vertex.i_pos_scale.xyz;
|
|
|
|
var out: VertexOutput;
|
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
|
|
|
// NOTE: Passing 0 as the instance_index to get_model_matrix() is a hack
|
|
|
|
// for this example as the instance_index builtin would map to the wrong
|
|
|
|
// index in the Mesh array. This index could be passed in via another
|
|
|
|
// uniform instead but it's unnecessary for the example.
|
improve shader import model (#5703)
# Objective
operate on naga IR directly to improve handling of shader modules.
- give codespan reporting into imported modules
- allow glsl to be used from wgsl and vice-versa
the ultimate objective is to make it possible to
- provide user hooks for core shader functions (to modify light
behaviour within the standard pbr pipeline, for example)
- make automatic binding slot allocation possible
but ... since this is already big, adds some value and (i think) is at
feature parity with the existing code, i wanted to push this now.
## Solution
i made a crate called naga_oil (https://github.com/robtfm/naga_oil -
unpublished for now, could be part of bevy) which manages modules by
- building each module independantly to naga IR
- creating "header" files for each supported language, which are used to
build dependent modules/shaders
- make final shaders by combining the shader IR with the IR for imported
modules
then integrated this into bevy, replacing some of the existing shader
processing stuff. also reworked examples to reflect this.
## Migration Guide
shaders that don't use `#import` directives should work without changes.
the most notable user-facing difference is that imported
functions/variables/etc need to be qualified at point of use, and
there's no "leakage" of visible stuff into your shader scope from the
imports of your imports, so if you used things imported by your imports,
you now need to import them directly and qualify them.
the current strategy of including/'spreading' `mesh_vertex_output`
directly into a struct doesn't work any more, so these need to be
modified as per the examples (e.g. color_material.wgsl, or many others).
mesh data is assumed to be in bindgroup 2 by default, if mesh data is
bound into bindgroup 1 instead then the shader def `MESH_BINDGROUP_1`
needs to be added to the pipeline shader_defs.
2023-06-27 00:29:22 +00:00
|
|
|
out.clip_position = mesh_position_local_to_clip(
|
2023-08-15 07:28:45 +00:00
|
|
|
get_model_matrix(0u),
|
improve shader import model (#5703)
# Objective
operate on naga IR directly to improve handling of shader modules.
- give codespan reporting into imported modules
- allow glsl to be used from wgsl and vice-versa
the ultimate objective is to make it possible to
- provide user hooks for core shader functions (to modify light
behaviour within the standard pbr pipeline, for example)
- make automatic binding slot allocation possible
but ... since this is already big, adds some value and (i think) is at
feature parity with the existing code, i wanted to push this now.
## Solution
i made a crate called naga_oil (https://github.com/robtfm/naga_oil -
unpublished for now, could be part of bevy) which manages modules by
- building each module independantly to naga IR
- creating "header" files for each supported language, which are used to
build dependent modules/shaders
- make final shaders by combining the shader IR with the IR for imported
modules
then integrated this into bevy, replacing some of the existing shader
processing stuff. also reworked examples to reflect this.
## Migration Guide
shaders that don't use `#import` directives should work without changes.
the most notable user-facing difference is that imported
functions/variables/etc need to be qualified at point of use, and
there's no "leakage" of visible stuff into your shader scope from the
imports of your imports, so if you used things imported by your imports,
you now need to import them directly and qualify them.
the current strategy of including/'spreading' `mesh_vertex_output`
directly into a struct doesn't work any more, so these need to be
modified as per the examples (e.g. color_material.wgsl, or many others).
mesh data is assumed to be in bindgroup 2 by default, if mesh data is
bound into bindgroup 1 instead then the shader def `MESH_BINDGROUP_1`
needs to be added to the pipeline shader_defs.
2023-06-27 00:29:22 +00:00
|
|
|
vec4<f32>(position, 1.0)
|
|
|
|
);
|
2022-01-05 19:43:11 +00:00
|
|
|
out.color = vertex.i_color;
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2022-07-14 21:17:16 +00:00
|
|
|
@fragment
|
|
|
|
fn fragment(in: VertexOutput) -> @location(0) vec4<f32> {
|
2022-01-05 19:43:11 +00:00
|
|
|
return in.color;
|
|
|
|
}
|