bevy/assets/shaders
Robert Swain c1a5428f8e
Work around naga/wgpu WGSL instance_index -> GLSL gl_InstanceID bug on WebGL2 (#9383)
naga and wgpu should polyfill WGSL instance_index functionality where it
is not available in GLSL. Until that is done, we can work around it in
bevy using a push constant which is converted to a uniform by naga and
wgpu.

# Objective

- Fixes #9375 

## Solution

- Use a push constant to pass in the base instance to the shader on
WebGL2 so that base instance + gl_InstanceID is used to correctly
represent the instance index.

## TODO

- [ ] Benchmark vs per-object dynamic offset MeshUniform as this will
now push a uniform value per-draw as well as update the dynamic offset
per-batch.
- [x] Test on DX12 AMD/NVIDIA to check that this PR does not regress any
problems that were observed there. (@Elabajaba @robtfm were testing that
last time - help appreciated. <3 )

---

## Changelog

- Added: `bevy_render::instance_index` shader import which includes a
workaround for the lack of a WGSL `instance_index` polyfill for WebGL2
in naga and wgpu for the time being. It uses a push_constant which gets
converted to a plain uniform by naga and wgpu.

## Migration Guide

Shader code before:

```
struct Vertex {
    @builtin(instance_index) instance_index: u32,
...
}

@vertex
fn vertex(vertex_no_morph: Vertex) -> VertexOutput {
...

    var model = mesh[vertex_no_morph.instance_index].model;
```

After:

```
#import bevy_render::instance_index

struct Vertex {
    @builtin(instance_index) instance_index: u32,
...
}

@vertex
fn vertex(vertex_no_morph: Vertex) -> VertexOutput {
...

    var model = mesh[bevy_render::instance_index::get_instance_index(vertex_no_morph.instance_index)].model;
```
2023-08-09 18:38:45 +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 Work around naga/wgpu WGSL instance_index -> GLSL gl_InstanceID bug on WebGL2 (#9383) 2023-08-09 18:38:45 +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 Use GpuArrayBuffer for MeshUniform (#9254) 2023-07-30 13:17:08 +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