forked from bevyengine/bevy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improve shader import model (bevyengine#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.
- Loading branch information
Showing
69 changed files
with
950 additions
and
3,144 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,51 @@ | ||
#import bevy_pbr::mesh_view_bindings | ||
#import bevy_pbr::mesh_bindings | ||
|
||
#import bevy_pbr::pbr_types | ||
#import bevy_pbr::utils | ||
#import bevy_pbr::clustered_forward | ||
#import bevy_pbr::lighting | ||
#import bevy_pbr::shadows | ||
#import bevy_pbr::fog | ||
#import bevy_pbr::pbr_functions | ||
#import bevy_pbr::pbr_ambient | ||
#import bevy_pbr::mesh_vertex_output MeshVertexOutput | ||
#import bevy_pbr::mesh_view_bindings view | ||
#import bevy_pbr::pbr_types STANDARD_MATERIAL_FLAGS_DOUBLE_SIDED_BIT | ||
#import bevy_core_pipeline::tonemapping tone_mapping | ||
#import bevy_pbr::pbr_functions as fns | ||
|
||
@group(1) @binding(0) | ||
var my_array_texture: texture_2d_array<f32>; | ||
@group(1) @binding(1) | ||
var my_array_texture_sampler: sampler; | ||
|
||
struct FragmentInput { | ||
@builtin(front_facing) is_front: bool, | ||
@builtin(position) frag_coord: vec4<f32>, | ||
#import bevy_pbr::mesh_vertex_output | ||
}; | ||
|
||
@fragment | ||
fn fragment(in: FragmentInput) -> @location(0) vec4<f32> { | ||
let layer = i32(in.world_position.x) & 0x3; | ||
fn fragment( | ||
@builtin(front_facing) is_front: bool, | ||
mesh: MeshVertexOutput, | ||
) -> @location(0) vec4<f32> { | ||
let layer = i32(mesh.world_position.x) & 0x3; | ||
|
||
// Prepare a 'processed' StandardMaterial by sampling all textures to resolve | ||
// the material members | ||
var pbr_input: PbrInput = pbr_input_new(); | ||
var pbr_input: fns::PbrInput = fns::pbr_input_new(); | ||
|
||
pbr_input.material.base_color = textureSample(my_array_texture, my_array_texture_sampler, in.uv, layer); | ||
pbr_input.material.base_color = textureSample(my_array_texture, my_array_texture_sampler, mesh.uv, layer); | ||
#ifdef VERTEX_COLORS | ||
pbr_input.material.base_color = pbr_input.material.base_color * in.color; | ||
pbr_input.material.base_color = pbr_input.material.base_color * mesh.color; | ||
#endif | ||
|
||
pbr_input.frag_coord = in.frag_coord; | ||
pbr_input.world_position = in.world_position; | ||
pbr_input.world_normal = prepare_world_normal( | ||
in.world_normal, | ||
pbr_input.frag_coord = mesh.position; | ||
pbr_input.world_position = mesh.world_position; | ||
pbr_input.world_normal = fns::prepare_world_normal( | ||
mesh.world_normal, | ||
(pbr_input.material.flags & STANDARD_MATERIAL_FLAGS_DOUBLE_SIDED_BIT) != 0u, | ||
in.is_front, | ||
is_front, | ||
); | ||
|
||
pbr_input.is_orthographic = view.projection[3].w == 1.0; | ||
|
||
pbr_input.N = apply_normal_mapping( | ||
pbr_input.N = fns::apply_normal_mapping( | ||
pbr_input.material.flags, | ||
pbr_input.world_normal, | ||
mesh.world_normal, | ||
#ifdef VERTEX_TANGENTS | ||
#ifdef STANDARDMATERIAL_NORMAL_MAP | ||
in.world_tangent, | ||
mesh.world_tangent, | ||
#endif | ||
#endif | ||
in.uv, | ||
mesh.uv, | ||
); | ||
pbr_input.V = calculate_view(in.world_position, pbr_input.is_orthographic); | ||
pbr_input.V = fns::calculate_view(mesh.world_position, pbr_input.is_orthographic); | ||
|
||
return tone_mapping(pbr(pbr_input)); | ||
return tone_mapping(fns::pbr(pbr_input), view.color_grading); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.