# 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.
74 lines
1.6 KiB
WebGPU Shading Language
74 lines
1.6 KiB
WebGPU Shading Language
#import bevy_sprite::mesh2d_functions as mesh_functions
|
|
#import bevy_sprite::mesh2d_bindings mesh
|
|
#import bevy_sprite::mesh2d_vertex_output MeshVertexOutput
|
|
#import bevy_sprite::mesh2d_view_bindings view
|
|
|
|
#ifdef TONEMAP_IN_SHADER
|
|
#import bevy_core_pipeline::tonemapping
|
|
#endif
|
|
|
|
struct Vertex {
|
|
#ifdef VERTEX_POSITIONS
|
|
@location(0) position: vec3<f32>,
|
|
#endif
|
|
#ifdef VERTEX_NORMALS
|
|
@location(1) normal: vec3<f32>,
|
|
#endif
|
|
#ifdef VERTEX_UVS
|
|
@location(2) uv: vec2<f32>,
|
|
#endif
|
|
#ifdef VERTEX_TANGENTS
|
|
@location(3) tangent: vec4<f32>,
|
|
#endif
|
|
#ifdef VERTEX_COLORS
|
|
@location(4) color: vec4<f32>,
|
|
#endif
|
|
};
|
|
|
|
@vertex
|
|
fn vertex(vertex: Vertex) -> MeshVertexOutput {
|
|
var out: MeshVertexOutput;
|
|
#ifdef VERTEX_UVS
|
|
out.uv = vertex.uv;
|
|
#endif
|
|
|
|
#ifdef VERTEX_POSITIONS
|
|
out.world_position = mesh_functions::mesh2d_position_local_to_world(
|
|
mesh.model,
|
|
vec4<f32>(vertex.position, 1.0)
|
|
);
|
|
out.position = mesh_functions::mesh2d_position_world_to_clip(out.world_position);
|
|
#endif
|
|
|
|
#ifdef VERTEX_NORMALS
|
|
out.world_normal = mesh_functions::mesh2d_normal_local_to_world(vertex.normal);
|
|
#endif
|
|
|
|
#ifdef VERTEX_TANGENTS
|
|
out.world_tangent = mesh_functions::mesh2d_tangent_local_to_world(
|
|
mesh.model,
|
|
vertex.tangent
|
|
);
|
|
#endif
|
|
|
|
#ifdef VERTEX_COLORS
|
|
out.color = vertex.color;
|
|
#endif
|
|
return out;
|
|
}
|
|
|
|
@fragment
|
|
fn fragment(
|
|
in: MeshVertexOutput,
|
|
) -> @location(0) vec4<f32> {
|
|
#ifdef VERTEX_COLORS
|
|
var color = in.color;
|
|
#ifdef TONEMAP_IN_SHADER
|
|
color = bevy_core_pipeline::tonemapping::tone_mapping(color, view.color_grading);
|
|
#endif
|
|
return color;
|
|
#else
|
|
return vec4<f32>(1.0, 0.0, 1.0, 1.0);
|
|
#endif
|
|
}
|