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
|
|
|
// TODO use common view binding
|
|
|
|
#import bevy_render::view View
|
|
|
|
|
2023-09-19 22:17:44 +00:00
|
|
|
@group(0) @binding(0) var<uniform> view: View;
|
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
|
|
|
|
2023-03-20 20:57:54 +00:00
|
|
|
|
2023-06-13 06:49:47 +00:00
|
|
|
struct LineGizmoUniform {
|
|
|
|
line_width: f32,
|
|
|
|
depth_bias: f32,
|
|
|
|
#ifdef SIXTEEN_BYTE_ALIGNMENT
|
|
|
|
// WebGL2 structs must be 16 byte aligned.
|
|
|
|
_padding: vec2<f32>,
|
|
|
|
#endif
|
2023-03-20 20:57:54 +00:00
|
|
|
}
|
|
|
|
|
2023-09-19 22:17:44 +00:00
|
|
|
@group(1) @binding(0) var<uniform> line_gizmo: LineGizmoUniform;
|
2023-06-13 06:49:47 +00:00
|
|
|
|
|
|
|
struct VertexInput {
|
|
|
|
@location(0) position_a: vec3<f32>,
|
|
|
|
@location(1) position_b: vec3<f32>,
|
|
|
|
@location(2) color_a: vec4<f32>,
|
|
|
|
@location(3) color_b: vec4<f32>,
|
|
|
|
@builtin(vertex_index) index: u32,
|
|
|
|
};
|
|
|
|
|
2023-03-20 20:57:54 +00:00
|
|
|
struct VertexOutput {
|
2023-06-13 06:49:47 +00:00
|
|
|
@builtin(position) clip_position: vec4<f32>,
|
2023-03-20 20:57:54 +00:00
|
|
|
@location(0) color: vec4<f32>,
|
2023-06-13 06:49:47 +00:00
|
|
|
};
|
2023-03-20 20:57:54 +00:00
|
|
|
|
2023-06-13 06:49:47 +00:00
|
|
|
@vertex
|
|
|
|
fn vertex(vertex: VertexInput) -> VertexOutput {
|
|
|
|
var positions = array<vec3<f32>, 6>(
|
|
|
|
vec3(0., -0.5, 0.),
|
|
|
|
vec3(0., -0.5, 1.),
|
|
|
|
vec3(0., 0.5, 1.),
|
|
|
|
vec3(0., -0.5, 0.),
|
|
|
|
vec3(0., 0.5, 1.),
|
|
|
|
vec3(0., 0.5, 0.)
|
|
|
|
);
|
|
|
|
let position = positions[vertex.index];
|
|
|
|
|
|
|
|
// algorithm based on https://wwwtyro.net/2019/11/18/instanced-lines.html
|
2023-08-17 20:09:19 +00:00
|
|
|
var clip_a = view.view_proj * vec4(vertex.position_a, 1.);
|
|
|
|
var clip_b = view.view_proj * vec4(vertex.position_b, 1.);
|
|
|
|
|
|
|
|
// Manual near plane clipping to avoid errors when doing the perspective divide inside this shader.
|
|
|
|
clip_a = clip_near_plane(clip_a, clip_b);
|
|
|
|
clip_b = clip_near_plane(clip_b, clip_a);
|
|
|
|
|
2023-06-13 06:49:47 +00:00
|
|
|
let clip = mix(clip_a, clip_b, position.z);
|
|
|
|
|
|
|
|
let resolution = view.viewport.zw;
|
|
|
|
let screen_a = resolution * (0.5 * clip_a.xy / clip_a.w + 0.5);
|
|
|
|
let screen_b = resolution * (0.5 * clip_b.xy / clip_b.w + 0.5);
|
|
|
|
|
|
|
|
let x_basis = normalize(screen_a - screen_b);
|
|
|
|
let y_basis = vec2(-x_basis.y, x_basis.x);
|
|
|
|
|
|
|
|
var color = mix(vertex.color_a, vertex.color_b, position.z);
|
|
|
|
|
|
|
|
var line_width = line_gizmo.line_width;
|
|
|
|
var alpha = 1.;
|
|
|
|
|
|
|
|
#ifdef PERSPECTIVE
|
|
|
|
line_width /= clip.w;
|
2023-03-29 18:05:03 +00:00
|
|
|
#endif
|
2023-03-20 20:57:54 +00:00
|
|
|
|
2023-06-13 06:49:47 +00:00
|
|
|
// Line thinness fade from https://acegikmo.com/shapes/docs/#anti-aliasing
|
2023-07-31 18:57:59 +00:00
|
|
|
if line_width > 0.0 && line_width < 1. {
|
2023-06-13 06:49:47 +00:00
|
|
|
color.a *= line_width;
|
|
|
|
line_width = 1.;
|
|
|
|
}
|
|
|
|
|
|
|
|
let offset = line_width * (position.x * x_basis + position.y * y_basis);
|
|
|
|
let screen = mix(screen_a, screen_b, position.z) + offset;
|
2023-03-20 20:57:54 +00:00
|
|
|
|
2023-06-13 06:49:47 +00:00
|
|
|
var depth: f32;
|
|
|
|
if line_gizmo.depth_bias >= 0. {
|
|
|
|
depth = clip.z * (1. - line_gizmo.depth_bias);
|
|
|
|
} else {
|
|
|
|
let epsilon = 4.88e-04;
|
|
|
|
// depth * (clip.w / depth)^-depth_bias. So that when -depth_bias is 1.0, this is equal to clip.w
|
|
|
|
// and when equal to 0.0, it is exactly equal to depth.
|
2023-07-31 18:57:59 +00:00
|
|
|
// the epsilon is here to prevent the depth from exceeding clip.w when -depth_bias = 1.0
|
2023-07-10 00:11:51 +00:00
|
|
|
// clip.w represents the near plane in homogeneous clip space in bevy, having a depth
|
2023-06-13 06:49:47 +00:00
|
|
|
// of this value means nothing can be in front of this
|
2023-07-31 18:57:59 +00:00
|
|
|
// The reason this uses an exponential function is that it makes it much easier for the
|
2023-07-10 00:11:51 +00:00
|
|
|
// user to chose a value that is convenient for them
|
2023-06-13 06:49:47 +00:00
|
|
|
depth = clip.z * exp2(-line_gizmo.depth_bias * log2(clip.w / clip.z - epsilon));
|
|
|
|
}
|
2023-03-20 20:57:54 +00:00
|
|
|
|
2023-06-13 06:49:47 +00:00
|
|
|
var clip_position = vec4(clip.w * ((2. * screen) / resolution - 1.), depth, clip.w);
|
|
|
|
|
|
|
|
return VertexOutput(clip_position, color);
|
2023-03-20 20:57:54 +00:00
|
|
|
}
|
|
|
|
|
2023-08-17 20:09:19 +00:00
|
|
|
fn clip_near_plane(a: vec4<f32>, b: vec4<f32>) -> vec4<f32> {
|
|
|
|
// Move a if a is behind the near plane and b is in front.
|
|
|
|
if a.z > a.w && b.z <= b.w {
|
|
|
|
// Interpolate a towards b until it's at the near plane.
|
|
|
|
let distance_a = a.z - a.w;
|
|
|
|
let distance_b = b.z - b.w;
|
|
|
|
let t = distance_a / (distance_a - distance_b);
|
|
|
|
return a + (b - a) * t;
|
|
|
|
}
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
2023-06-13 06:49:47 +00:00
|
|
|
struct FragmentInput {
|
|
|
|
@location(0) color: vec4<f32>,
|
|
|
|
};
|
2023-03-20 20:57:54 +00:00
|
|
|
|
2023-06-13 06:49:47 +00:00
|
|
|
struct FragmentOutput {
|
|
|
|
@location(0) color: vec4<f32>,
|
|
|
|
};
|
2023-03-20 20:57:54 +00:00
|
|
|
|
2023-06-13 06:49:47 +00:00
|
|
|
@fragment
|
|
|
|
fn fragment(in: FragmentInput) -> FragmentOutput {
|
|
|
|
return FragmentOutput(in.color);
|
2023-03-20 20:57:54 +00:00
|
|
|
}
|