Browse Source

Refactor common code in `egui-wgpu` shader entry points (#1730)

Creates `unpack_color` and `position_from_screen` functions to share
common logic between the alternative vertex shader entry points.
pull/1739/head
mitchmindtree 2 years ago
committed by GitHub
parent
commit
7eeb292adf
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 55
      egui-wgpu/src/egui.wgsl

55
egui-wgpu/src/egui.wgsl

@ -19,6 +19,25 @@ fn linear_from_srgb(srgb: vec3<f32>) -> vec3<f32> {
return select(higher, lower, cutoff); return select(higher, lower, cutoff);
} }
// [u8; 4] SRGB as u32 -> [r, g, b, a]
fn unpack_color(color: u32) -> vec4<f32> {
return vec4<f32>(
f32(color & 255u),
f32((color >> 8u) & 255u),
f32((color >> 16u) & 255u),
f32((color >> 24u) & 255u),
);
}
fn position_from_screen(screen_pos: vec2<f32>) -> vec4<f32> {
return vec4<f32>(
2.0 * screen_pos.x / r_locals.screen_size.x - 1.0,
1.0 - 2.0 * screen_pos.y / r_locals.screen_size.y,
0.0,
1.0,
);
}
[[stage(vertex)]] [[stage(vertex)]]
fn vs_main( fn vs_main(
[[location(0)]] a_pos: vec2<f32>, [[location(0)]] a_pos: vec2<f32>,
@ -27,23 +46,9 @@ fn vs_main(
) -> VertexOutput { ) -> VertexOutput {
var out: VertexOutput; var out: VertexOutput;
out.tex_coord = a_tex_coord; out.tex_coord = a_tex_coord;
let color = unpack_color(a_color);
// [u8; 4] SRGB as u32 -> [r, g, b, a]
let color = vec4<f32>(
f32(a_color & 255u),
f32((a_color >> 8u) & 255u),
f32((a_color >> 16u) & 255u),
f32((a_color >> 24u) & 255u),
);
out.color = vec4<f32>(linear_from_srgb(color.rgb), color.a / 255.0); out.color = vec4<f32>(linear_from_srgb(color.rgb), color.a / 255.0);
out.position = position_from_screen(a_pos);
out.position = vec4<f32>(
2.0 * a_pos.x / r_locals.screen_size.x - 1.0,
1.0 - 2.0 * a_pos.y / r_locals.screen_size.y,
0.0,
1.0,
);
return out; return out;
} }
@ -55,23 +60,9 @@ fn vs_conv_main(
) -> VertexOutput { ) -> VertexOutput {
var out: VertexOutput; var out: VertexOutput;
out.tex_coord = a_tex_coord; out.tex_coord = a_tex_coord;
let color = unpack_color(a_color);
// [u8; 4] SRGB as u32 -> [r, g, b, a]
let color = vec4<f32>(
f32(a_color & 255u),
f32((a_color >> 8u) & 255u),
f32((a_color >> 16u) & 255u),
f32((a_color >> 24u) & 255u),
);
out.color = vec4<f32>(color.rgba / 255.0); out.color = vec4<f32>(color.rgba / 255.0);
out.position = position_from_screen(a_pos);
out.position = vec4<f32>(
2.0 * a_pos.x / r_locals.screen_size.x - 1.0,
1.0 - 2.0 * a_pos.y / r_locals.screen_size.y,
0.0,
1.0,
);
return out; return out;
} }

Loading…
Cancel
Save