Skip to content

Commit

Permalink
update shader imports (bevyengine#10180)
Browse files Browse the repository at this point in the history
# Objective

- bump naga_oil to 0.10
- update shader imports to use rusty syntax

## Migration Guide

naga_oil 0.10 reworks the import mechanism to support more syntax to
make it more rusty, and test for item use before importing to determine
which imports are modules and which are items, which allows:

- use rust-style imports
```
#import bevy_pbr::{
    pbr_functions::{alpha_discard as discard, apply_pbr_lighting}, 
    mesh_bindings,
}
```

- import partial paths:
```
#import part::of::path
...
path::remainder::function();
```
which will call to `part::of::path::remainder::function`

- use fully qualified paths without importing:
```
// #import bevy_pbr::pbr_functions
bevy_pbr::pbr_functions::pbr()
```
- use imported items without qualifying
```
#import bevy_pbr::pbr_functions::pbr
// for backwards compatibility the old style is still supported:
// #import bevy_pbr::pbr_functions pbr
...
pbr()
```

- allows most imported items to end with `_` and numbers (naga_oil#30).
still doesn't allow struct members to end with `_` or numbers but it's
progress.

- the vast majority of existing shader code will work without changes,
but will emit "deprecated" warnings for old-style imports. these can be
suppressed with the `allow-deprecated` feature.

- partly breaks overrides (as far as i'm aware nobody uses these yet) -
now overrides will only be applied if the overriding module is added as
an additional import in the arguments to `Composer::make_naga_module` or
`Composer::add_composable_module`. this is necessary to support
determining whether imports are modules or items.
  • Loading branch information
robtfm authored Oct 21, 2023
1 parent 9b80205 commit 61bad4e
Show file tree
Hide file tree
Showing 67 changed files with 290 additions and 255 deletions.
6 changes: 4 additions & 2 deletions assets/shaders/animate_shader.wgsl
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// The time since startup data is in the globals binding which is part of the mesh_view_bindings import
#import bevy_pbr::mesh_view_bindings globals
#import bevy_pbr::forward_io VertexOutput
#import bevy_pbr::{
mesh_view_bindings::globals,
forward_io::VertexOutput,
}

fn oklab_to_linear_srgb(c: vec3<f32>) -> vec3<f32> {
let L = c.x;
Expand Down
12 changes: 7 additions & 5 deletions assets/shaders/array_texture.wgsl
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#import bevy_pbr::forward_io VertexOutput
#import bevy_pbr::mesh_view_bindings view
#import bevy_pbr::pbr_types STANDARD_MATERIAL_FLAGS_DOUBLE_SIDED_BIT, PbrInput, pbr_input_new
#import bevy_core_pipeline::tonemapping tone_mapping
#import bevy_pbr::pbr_functions as fns
#import bevy_pbr::{
forward_io::VertexOutput,
mesh_view_bindings::view,
pbr_types::{STANDARD_MATERIAL_FLAGS_DOUBLE_SIDED_BIT, PbrInput, pbr_input_new},
pbr_functions as fns,
}
#import bevy_core_pipeline::tonemapping::tone_mapping

@group(1) @binding(0) var my_array_texture: texture_2d_array<f32>;
@group(1) @binding(1) var my_array_texture_sampler: sampler;
Expand Down
2 changes: 1 addition & 1 deletion assets/shaders/cubemap_unlit.wgsl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#import bevy_pbr::forward_io VertexOutput
#import bevy_pbr::forward_io::VertexOutput

#ifdef CUBEMAP_ARRAY
@group(1) @binding(0) var base_color_texture: texture_cube_array<f32>;
Expand Down
7 changes: 4 additions & 3 deletions assets/shaders/custom_gltf_2d.wgsl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#import bevy_sprite::mesh2d_view_bindings globals
#import bevy_sprite::mesh2d_bindings mesh
#import bevy_sprite::mesh2d_functions get_model_matrix, mesh2d_position_local_to_clip
#import bevy_sprite::{
mesh2d_view_bindings::globals,
mesh2d_functions::{get_model_matrix, mesh2d_position_local_to_clip},
}

struct Vertex {
@builtin(instance_index) instance_index: u32,
Expand Down
4 changes: 2 additions & 2 deletions assets/shaders/custom_material.wgsl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#import bevy_pbr::forward_io VertexOutput
#import bevy_pbr::forward_io::VertexOutput
// we can import items from shader modules in the assets folder with a quoted path
#import "shaders/custom_material_import.wgsl" COLOR_MULTIPLIER
#import "shaders/custom_material_import.wgsl"::COLOR_MULTIPLIER

struct CustomMaterial {
color: vec4<f32>,
Expand Down
8 changes: 5 additions & 3 deletions assets/shaders/custom_material_screenspace_texture.wgsl
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#import bevy_pbr::mesh_view_bindings view
#import bevy_pbr::forward_io VertexOutput
#import bevy_pbr::utils coords_to_viewport_uv
#import bevy_pbr::{
mesh_view_bindings::view,
forward_io::VertexOutput,
utils::coords_to_viewport_uv,
}

@group(1) @binding(0) var texture: texture_2d<f32>;
@group(1) @binding(1) var texture_sampler: sampler;
Expand Down
3 changes: 1 addition & 2 deletions assets/shaders/custom_vertex_attribute.wgsl
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#import bevy_pbr::mesh_bindings mesh
#import bevy_pbr::mesh_functions get_model_matrix, mesh_position_local_to_clip
#import bevy_pbr::mesh_functions::{get_model_matrix, mesh_position_local_to_clip}

struct CustomMaterial {
color: vec4<f32>,
Expand Down
18 changes: 12 additions & 6 deletions assets/shaders/extended_material.wgsl
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
#import bevy_pbr::pbr_fragment pbr_input_from_standard_material
#import bevy_pbr::pbr_functions alpha_discard
#import bevy_pbr::{
pbr_fragment::pbr_input_from_standard_material,
pbr_functions::alpha_discard,
}

#ifdef PREPASS_PIPELINE
#import bevy_pbr::prepass_io VertexOutput, FragmentOutput
#import bevy_pbr::pbr_deferred_functions deferred_output
#import bevy_pbr::{
prepass_io::{VertexOutput, FragmentOutput},
pbr_deferred_functions::deferred_output,
}
#else
#import bevy_pbr::forward_io VertexOutput, FragmentOutput
#import bevy_pbr::pbr_functions apply_pbr_lighting, main_pass_post_lighting_processing
#import bevy_pbr::{
forward_io::{VertexOutput, FragmentOutput},
pbr_functions::{apply_pbr_lighting, main_pass_post_lighting_processing},
}
#endif

struct MyExtendedMaterial {
Expand Down
4 changes: 1 addition & 3 deletions assets/shaders/fallback_image_test.wgsl
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
#import bevy_pbr::mesh_view_bindings
#import bevy_pbr::mesh_bindings
#import bevy_pbr::forward_io VertexOutput
#import bevy_pbr::forward_io::VertexOutput

@group(1) @binding(0) var test_texture_1d: texture_1d<f32>;
@group(1) @binding(1) var test_texture_1d_sampler: sampler;
Expand Down
3 changes: 1 addition & 2 deletions assets/shaders/instancing.wgsl
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#import bevy_pbr::mesh_functions get_model_matrix, mesh_position_local_to_clip
#import bevy_pbr::mesh_bindings mesh
#import bevy_pbr::mesh_functions::{get_model_matrix, mesh_position_local_to_clip}

struct Vertex {
@location(0) position: vec3<f32>,
Expand Down
2 changes: 1 addition & 1 deletion assets/shaders/line_material.wgsl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#import bevy_pbr::forward_io VertexOutput
#import bevy_pbr::forward_io::VertexOutput

struct LineMaterial {
color: vec4<f32>,
Expand Down
4 changes: 1 addition & 3 deletions assets/shaders/post_processing.wgsl
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
// This shader computes the chromatic aberration effect

#import bevy_pbr::utils

// Since post processing is a fullscreen effect, we use the fullscreen vertex shader provided by bevy.
// This will import a vertex shader that renders a single fullscreen triangle.
//
Expand All @@ -20,7 +18,7 @@
// As you can see, the triangle ends up bigger than the screen.
//
// You don't need to worry about this too much since bevy will compute the correct UVs for you.
#import bevy_core_pipeline::fullscreen_vertex_shader FullscreenVertexOutput
#import bevy_core_pipeline::fullscreen_vertex_shader::FullscreenVertexOutput

@group(0) @binding(0) var screen_texture: texture_2d<f32>;
@group(0) @binding(1) var texture_sampler: sampler;
Expand Down
2 changes: 1 addition & 1 deletion assets/shaders/shader_defs.wgsl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#import bevy_pbr::forward_io VertexOutput
#import bevy_pbr::forward_io::VertexOutput

struct CustomMaterial {
color: vec4<f32>,
Expand Down
9 changes: 5 additions & 4 deletions assets/shaders/show_prepass.wgsl
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#import bevy_pbr::mesh_types
#import bevy_pbr::mesh_view_bindings globals
#import bevy_pbr::prepass_utils
#import bevy_pbr::forward_io VertexOutput
#import bevy_pbr::{
mesh_view_bindings::globals,
prepass_utils,
forward_io::VertexOutput,
}

struct ShowPrepassSettings {
show_depth: u32,
Expand Down
2 changes: 1 addition & 1 deletion assets/shaders/texture_binding_array.wgsl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#import bevy_pbr::forward_io VertexOutput
#import bevy_pbr::forward_io::VertexOutput

@group(1) @binding(0) var textures: binding_array<texture_2d<f32>>;
@group(1) @binding(1) var nearest_sampler: sampler;
Expand Down
13 changes: 7 additions & 6 deletions assets/shaders/tonemapping_test_patterns.wgsl
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#import bevy_pbr::mesh_view_bindings
#import bevy_pbr::mesh_bindings
#import bevy_pbr::forward_io VertexOutput
#import bevy_pbr::utils PI
#import bevy_pbr::{
mesh_view_bindings,
forward_io::VertexOutput,
utils::PI,
}

#ifdef TONEMAP_IN_SHADER
#import bevy_core_pipeline::tonemapping tone_mapping
#import bevy_core_pipeline::tonemapping::tone_mapping
#endif

// Sweep across hues on y axis with value from 0.0 to +15EV across x axis
Expand Down Expand Up @@ -55,7 +56,7 @@ fn fragment(
}
var color = vec4(out, 1.0);
#ifdef TONEMAP_IN_SHADER
color = tone_mapping(color, bevy_pbr::mesh_view_bindings::view.color_grading);
color = tone_mapping(color, mesh_view_bindings::view.color_grading);
#endif
return color;
}
2 changes: 1 addition & 1 deletion crates/bevy_core_pipeline/src/blit/blit.wgsl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#import bevy_core_pipeline::fullscreen_vertex_shader FullscreenVertexOutput
#import bevy_core_pipeline::fullscreen_vertex_shader::FullscreenVertexOutput

@group(0) @binding(0) var in_texture: texture_2d<f32>;
@group(0) @binding(1) var in_sampler: sampler;
Expand Down
2 changes: 0 additions & 2 deletions crates/bevy_core_pipeline/src/bloom/bloom.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
// * [COD] - Next Generation Post Processing in Call of Duty - http://www.iryoku.com/next-generation-post-processing-in-call-of-duty-advanced-warfare
// * [PBB] - Physically Based Bloom - https://learnopengl.com/Guest-Articles/2022/Phys.-Based-Bloom

#import bevy_core_pipeline::fullscreen_vertex_shader

struct BloomUniforms {
threshold_precomputations: vec4<f32>,
viewport: vec4<f32>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

#import bevy_core_pipeline::fullscreen_vertex_shader FullscreenVertexOutput
#import bevy_core_pipeline::fullscreen_vertex_shader::FullscreenVertexOutput

struct CASUniforms {
sharpness: f32,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#import bevy_pbr::utils
#import bevy_core_pipeline::fullscreen_vertex_shader FullscreenVertexOutput
#import bevy_core_pipeline::fullscreen_vertex_shader::FullscreenVertexOutput

@group(0) @binding(0)
var material_id_texture: texture_2d<u32>;
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_core_pipeline/src/fxaa/fxaa.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//
// Tweaks by mrDIMAS - https://github.com/FyroxEngine/Fyrox/blob/master/src/renderer/shaders/fxaa_fs.glsl

#import bevy_core_pipeline::fullscreen_vertex_shader FullscreenVertexOutput
#import bevy_core_pipeline::fullscreen_vertex_shader::FullscreenVertexOutput

@group(0) @binding(0) var screenTexture: texture_2d<f32>;
@group(0) @binding(1) var samp: sampler;
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_core_pipeline/src/skybox/skybox.wgsl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#import bevy_render::view View
#import bevy_pbr::utils coords_to_viewport_uv
#import bevy_render::view::View
#import bevy_pbr::utils::coords_to_viewport_uv

@group(0) @binding(0) var skybox: texture_cube<f32>;
@group(0) @binding(1) var skybox_sampler: sampler;
Expand Down
2 changes: 0 additions & 2 deletions crates/bevy_core_pipeline/src/taa/taa.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
const DEFAULT_HISTORY_BLEND_RATE: f32 = 0.1; // Default blend rate to use when no confidence in history
const MIN_HISTORY_BLEND_RATE: f32 = 0.015; // Minimum blend rate allowed, to ensure at least some of the current sample is used

#import bevy_core_pipeline::fullscreen_vertex_shader

@group(0) @binding(0) var view_target: texture_2d<f32>;
@group(0) @binding(1) var history: texture_2d<f32>;
@group(0) @binding(2) var motion_vectors: texture_2d<f32>;
Expand Down
12 changes: 6 additions & 6 deletions crates/bevy_core_pipeline/src/tonemapping/tonemapping.wgsl
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#define TONEMAPPING_PASS

#import bevy_core_pipeline::fullscreen_vertex_shader FullscreenVertexOutput
#import bevy_render::view View
#import bevy_core_pipeline::tonemapping tone_mapping, powsafe, screen_space_dither
#import bevy_render::view::View
#import bevy_core_pipeline::{
fullscreen_vertex_shader::FullscreenVertexOutput,
tonemapping::{tone_mapping, powsafe, screen_space_dither},
}

@group(0) @binding(0) var<uniform> view: View;

Expand All @@ -11,8 +13,6 @@
@group(0) @binding(3) var dt_lut_texture: texture_3d<f32>;
@group(0) @binding(4) var dt_lut_sampler: sampler;

#import bevy_core_pipeline::tonemapping

@fragment
fn fragment(in: FullscreenVertexOutput) -> @location(0) vec4<f32> {
let hdr_color = textureSample(hdr_texture, hdr_sampler, in.uv);
Expand All @@ -21,7 +21,7 @@ fn fragment(in: FullscreenVertexOutput) -> @location(0) vec4<f32> {

#ifdef DEBAND_DITHER
output_rgb = powsafe(output_rgb.rgb, 1.0 / 2.2);
output_rgb = output_rgb + bevy_core_pipeline::tonemapping::screen_space_dither(in.position.xy);
output_rgb = output_rgb + screen_space_dither(in.position.xy);
// This conversion back to linear space is required because our output texture format is
// SRGB; the GPU will assume our output is linear and will apply an SRGB conversion.
output_rgb = powsafe(output_rgb.rgb, 2.2);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#define_import_path bevy_core_pipeline::tonemapping

#import bevy_render::view View, ColorGrading
#import bevy_render::view::ColorGrading

// hack !! not sure what to do with this
#ifdef TONEMAPPING_PASS
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_gizmos/src/lines.wgsl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// TODO use common view binding
#import bevy_render::view View
#import bevy_render::view::View

@group(0) @binding(0) var<uniform> view: View;

Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_pbr/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ fixedbitset = "0.4"
# direct dependency required for derive macro
bytemuck = { version = "1", features = ["derive"] }
radsort = "0.1"
naga_oil = "0.9"
naga_oil = "0.10"
smallvec = "1.6"
thread_local = "1.0"
23 changes: 11 additions & 12 deletions crates/bevy_pbr/src/deferred/deferred_lighting.wgsl
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
#import bevy_pbr::prepass_utils
#import bevy_pbr::pbr_types STANDARD_MATERIAL_FLAGS_FOG_ENABLED_BIT, STANDARD_MATERIAL_FLAGS_UNLIT_BIT
#import bevy_pbr::pbr_functions as pbr_functions
#import bevy_pbr::pbr_deferred_types as deferred_types
#import bevy_pbr::pbr_deferred_functions pbr_input_from_deferred_gbuffer, unpack_unorm3x4_plus_unorm_20_
#import bevy_pbr::mesh_view_types FOG_MODE_OFF

#import bevy_pbr::mesh_view_bindings deferred_prepass_texture, fog, view, screen_space_ambient_occlusion_texture
#import bevy_core_pipeline::tonemapping screen_space_dither, powsafe, tone_mapping
#import bevy_pbr::{
prepass_utils,
pbr_types::STANDARD_MATERIAL_FLAGS_UNLIT_BIT,
pbr_functions,
pbr_deferred_functions::{pbr_input_from_deferred_gbuffer, unpack_unorm3x4_plus_unorm_20_},
mesh_view_bindings::deferred_prepass_texture,
}

#ifdef SCREEN_SPACE_AMBIENT_OCCLUSION
#import bevy_pbr::gtao_utils gtao_multibounce
#import bevy_pbr::mesh_view_bindings::screen_space_ambient_occlusion_texture
#import bevy_pbr::gtao_utils::gtao_multibounce
#endif

struct FullscreenVertexOutput {
Expand Down Expand Up @@ -48,10 +47,10 @@ fn fragment(in: FullscreenVertexOutput) -> @location(0) vec4<f32> {
let deferred_data = textureLoad(deferred_prepass_texture, vec2<i32>(frag_coord.xy), 0);

#ifdef WEBGL2
frag_coord.z = deferred_types::unpack_unorm3x4_plus_unorm_20_(deferred_data.b).w;
frag_coord.z = unpack_unorm3x4_plus_unorm_20_(deferred_data.b).w;
#else
#ifdef DEPTH_PREPASS
frag_coord.z = bevy_pbr::prepass_utils::prepass_depth(in.position, 0u);
frag_coord.z = prepass_utils::prepass_depth(in.position, 0u);
#endif
#endif

Expand Down
19 changes: 10 additions & 9 deletions crates/bevy_pbr/src/deferred/pbr_deferred_functions.wgsl
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
#define_import_path bevy_pbr::pbr_deferred_functions

#import bevy_pbr::pbr_types PbrInput, standard_material_new, STANDARD_MATERIAL_FLAGS_FOG_ENABLED_BIT, STANDARD_MATERIAL_FLAGS_UNLIT_BIT
#import bevy_pbr::pbr_deferred_types as deferred_types
#import bevy_pbr::pbr_functions as pbr_functions
#import bevy_pbr::rgb9e5 as rgb9e5
#import bevy_pbr::mesh_view_bindings as view_bindings
#import bevy_pbr::mesh_view_bindings view
#import bevy_pbr::utils octahedral_encode, octahedral_decode
#import bevy_pbr::prepass_io VertexOutput, FragmentOutput
#import bevy_pbr::{
pbr_types::{PbrInput, standard_material_new, STANDARD_MATERIAL_FLAGS_UNLIT_BIT},
pbr_deferred_types as deferred_types,
pbr_functions,
rgb9e5,
mesh_view_bindings::view,
utils::{octahedral_encode, octahedral_decode},
prepass_io::{VertexOutput, FragmentOutput},
}

#ifdef MOTION_VECTOR_PREPASS
#import bevy_pbr::pbr_prepass_functions calculate_motion_vector
#import bevy_pbr::pbr_prepass_functions::calculate_motion_vector
#endif

// ---------------------------
Expand Down
7 changes: 5 additions & 2 deletions crates/bevy_pbr/src/deferred/pbr_deferred_types.wgsl
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#define_import_path bevy_pbr::pbr_deferred_types
#import bevy_pbr::mesh_types MESH_FLAGS_SHADOW_RECEIVER_BIT
#import bevy_pbr::pbr_types STANDARD_MATERIAL_FLAGS_FOG_ENABLED_BIT, STANDARD_MATERIAL_FLAGS_UNLIT_BIT

#import bevy_pbr::{
mesh_types::MESH_FLAGS_SHADOW_RECEIVER_BIT,
pbr_types::{STANDARD_MATERIAL_FLAGS_FOG_ENABLED_BIT, STANDARD_MATERIAL_FLAGS_UNLIT_BIT},
}

// Maximum of 8 bits available
const DEFERRED_FLAGS_UNLIT_BIT: u32 = 1u;
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_pbr/src/environment_map/environment_map.wgsl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#define_import_path bevy_pbr::environment_map

#import bevy_pbr::mesh_view_bindings as bindings
#import bevy_pbr::mesh_view_bindings as bindings;

struct EnvironmentMapLight {
diffuse: vec3<f32>,
Expand Down
Loading

0 comments on commit 61bad4e

Please sign in to comment.