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>;
24// Fix for DX12 backend in wgpu which appears to only support struct immediates
25// wgpu::backend::wgpu_core: Shader translation error for stage ShaderStages(FRAGMENT): HLSL: Unimplemented("immediate 'offset' has non-struct type; tracked by: https://github.com/gfx-rs/wgpu/issues/5683")
26struct BindlessOffset {
27 offset: u32,
28};
29var<immediate> offset: BindlessOffset;
31#else //BINDLESS
32@group(1) @binding(0) var image_texture: texture_2d<f32>;
33@group(1) @binding(1) var image_sampler: sampler;
34#endif // BINDLESS
36// 0-1 linear from 0-1 sRGB gamma.
37fn linear_from_gamma_rgb(srgb: vec3<f32>) -> vec3<f32> {
38 let cutoff = srgb < vec3<f32>(0.04045);
39 let lower = srgb / vec3<f32>(12.92);
40 let higher = pow((srgb + vec3<f32>(0.055)) / vec3<f32>(1.055), vec3<f32>(2.4));
41 return select(higher, lower, cutoff);
44// 0-1 sRGB gamma from 0-1 linear.
45fn gamma_from_linear_rgb(rgb: vec3<f32>) -> vec3<f32> {
46 let cutoff = rgb < vec3<f32>(0.0031308);
47 let lower = rgb * vec3<f32>(12.92);
48 let higher = vec3<f32>(1.055) * pow(rgb, vec3<f32>(1.0 / 2.4)) - vec3<f32>(0.055);
49 return select(higher, lower, cutoff);
52// 0-1 sRGBA gamma from 0-1 linear.
53fn gamma_from_linear_rgba(linear_rgba: vec4<f32>) -> vec4<f32> {
54 return vec4<f32>(gamma_from_linear_rgb(linear_rgba.rgb), linear_rgba.a);
57@vertex
58fn vs_main(in: VertexInput) -> VertexOutput {
59 let position = in.position * transform.scale + transform.translation;
60 // Not sure why Egui does vertex color interpolation in sRGB but here we do it the same way as well.
61 return VertexOutput(vec4<f32>(position, 0.0, 1.0), in.color, in.uv);
64@fragment
65fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
66 #ifdef BINDLESS
67 let image_texture = image_texture[offset.offset];
68 let image_sampler = image_sampler[offset.offset];
69 #endif
71 // Quoting the Egui's glsl shader:
72 // "We multiply the colors in gamma space, because that's the only way to get text to look right."
73 let texture_color_linear = textureSample(image_texture, image_sampler, in.uv);
74 let texture_color_gamma = gamma_from_linear_rgba(texture_color_linear);
75 let color_gamma = texture_color_gamma * in.color;
77 return vec4<f32>(linear_from_gamma_rgb(color_gamma.rgb), color_gamma.a);
Contents