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;
20#ifdef BINDLESS
21@group(1) @binding(0) var image_texture: binding_array<texture_2d<f32>>;
22@group(1) @binding(1) var image_sampler: binding_array<sampler>;
23var<push_constant> offset: u32;
25#else //BINDLESS
26@group(1) @binding(0) var image_texture: texture_2d<f32>;
27@group(1) @binding(1) var image_sampler: sampler;
28#endif // BINDLESS
30// 0-1 linear from 0-1 sRGB gamma.
31fn linear_from_gamma_rgb(srgb: vec3<f32>) -> vec3<f32> {
32 let cutoff = srgb < vec3<f32>(0.04045);
33 let lower = srgb / vec3<f32>(12.92);
34 let higher = pow((srgb + vec3<f32>(0.055)) / vec3<f32>(1.055), vec3<f32>(2.4));
35 return select(higher, lower, cutoff);
38// 0-1 sRGB gamma from 0-1 linear.
39fn gamma_from_linear_rgb(rgb: vec3<f32>) -> vec3<f32> {
40 let cutoff = rgb < vec3<f32>(0.0031308);
41 let lower = rgb * vec3<f32>(12.92);
42 let higher = vec3<f32>(1.055) * pow(rgb, vec3<f32>(1.0 / 2.4)) - vec3<f32>(0.055);
43 return select(higher, lower, cutoff);
46// 0-1 sRGBA gamma from 0-1 linear.
47fn gamma_from_linear_rgba(linear_rgba: vec4<f32>) -> vec4<f32> {
48 return vec4<f32>(gamma_from_linear_rgb(linear_rgba.rgb), linear_rgba.a);
51@vertex
52fn vs_main(in: VertexInput) -> VertexOutput {
53 let position = in.position * transform.scale + transform.translation;
54 // Not sure why Egui does vertex color interpolation in sRGB but here we do it the same way as well.
55 return VertexOutput(vec4<f32>(position, 0.0, 1.0), in.color, in.uv);
58@fragment
59fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
60 #ifdef BINDLESS
61 let image_texture = image_texture[offset];
62 let image_sampler = image_sampler[offset];
63 #endif
65 let texture_color_linear = textureSample(image_texture, image_sampler, in.uv);
66 // We un-premultiply Egui-managed textures on CPU, because Bevy doesn't premultiply it's own images, so here we pre-multiply everything.
67 let texture_color_linear_premultiplied = vec4<f32>(texture_color_linear.rgb * texture_color_linear.a, texture_color_linear.a);
68 let texture_color_gamma_premultiplied = gamma_from_linear_rgba(texture_color_linear_premultiplied);
70 // Quoting the Egui's glsl shader:
71 // "We multiply the colors in gamma space, because that's the only way to get text to look right."
72 let color_gamma = texture_color_gamma_premultiplied * in.color;
74 return vec4<f32>(linear_from_gamma_rgb(color_gamma.rgb), color_gamma.a);
Contents