Search across packages, versions, files, declarations, and comments. Use multiple terms to narrow results (AND), and add @vertex, @fragment, or @compute to filter by shader stage.
1struct Transform {
2 scale: vec2<f32>,
3 translation: vec2<f32>,
4}
6struct VertexInput {
7 @location(0) position: vec2<f32>,
8 @location(1) uv: vec2<f32>,
9 @location(2) color: vec4<f32>,
12struct VertexOutput {
13 @builtin(position) position: vec4<f32>,
14 @location(0) color: vec4<f32>,
15 @location(1) uv: vec2<f32>,
18@group(0) @binding(0) var<uniform> transform: Transform;
19@group(1) @binding(0) var image_texture: texture_2d<f32>;
20@group(1) @binding(1) var image_sampler: sampler;
22// 0-1 linear from 0-1 sRGB gamma.
23fn linear_from_gamma_rgb(srgb: vec3<f32>) -> vec3<f32> {
24 let cutoff = srgb < vec3<f32>(0.04045);
25 let lower = srgb / vec3<f32>(12.92);
26 let higher = pow((srgb + vec3<f32>(0.055)) / vec3<f32>(1.055), vec3<f32>(2.4));
27 return select(higher, lower, cutoff);
30// 0-1 sRGB gamma from 0-1 linear.
31fn gamma_from_linear_rgb(rgb: vec3<f32>) -> vec3<f32> {
32 let cutoff = rgb < vec3<f32>(0.0031308);
33 let lower = rgb * vec3<f32>(12.92);
34 let higher = vec3<f32>(1.055) * pow(rgb, vec3<f32>(1.0 / 2.4)) - vec3<f32>(0.055);
35 return select(higher, lower, cutoff);
38// 0-1 sRGBA gamma from 0-1 linear.
39fn gamma_from_linear_rgba(linear_rgba: vec4<f32>) -> vec4<f32> {
40 return vec4<f32>(gamma_from_linear_rgb(linear_rgba.rgb), linear_rgba.a);
43@vertex
44fn vs_main(in: VertexInput) -> VertexOutput {
45 let position = in.position * transform.scale + transform.translation;
46 // Not sure why Egui does vertex color interpolation in sRGB but here we do it the same way as well.
47 return VertexOutput(vec4<f32>(position, 0.0, 1.0), in.color, in.uv);
50@fragment
51fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
52 let texture_color_linear = textureSample(image_texture, image_sampler, in.uv);
53 // We un-premultiply Egui-managed textures on CPU, because Bevy doesn't premultiply it's own images, so here we pre-multiply everything.
54 let texture_color_linear_premultiplied = vec4<f32>(texture_color_linear.rgb * texture_color_linear.a, texture_color_linear.a);
55 let texture_color_gamma_premultiplied = gamma_from_linear_rgba(texture_color_linear_premultiplied);
57 // Quoting the Egui's glsl shader:
58 // "We multiply the colors in gamma space, because that's the only way to get text to look right."
59 let color_gamma = texture_color_gamma_premultiplied * in.color;
61 return vec4<f32>(linear_from_gamma_rgb(color_gamma.rgb), color_gamma.a);
Contents