From 7eeb292adfacd9311a420ac3ea225e2261a8f8d3 Mon Sep 17 00:00:00 2001 From: mitchmindtree Date: Sat, 11 Jun 2022 21:52:06 +1000 Subject: [PATCH] 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. --- egui-wgpu/src/egui.wgsl | 55 +++++++++++++++++------------------------ 1 file changed, 23 insertions(+), 32 deletions(-) diff --git a/egui-wgpu/src/egui.wgsl b/egui-wgpu/src/egui.wgsl index a3b4f3b60..3a6ea07bc 100644 --- a/egui-wgpu/src/egui.wgsl +++ b/egui-wgpu/src/egui.wgsl @@ -19,6 +19,25 @@ fn linear_from_srgb(srgb: vec3) -> vec3 { return select(higher, lower, cutoff); } +// [u8; 4] SRGB as u32 -> [r, g, b, a] +fn unpack_color(color: u32) -> vec4 { + return vec4( + f32(color & 255u), + f32((color >> 8u) & 255u), + f32((color >> 16u) & 255u), + f32((color >> 24u) & 255u), + ); +} + +fn position_from_screen(screen_pos: vec2) -> vec4 { + return vec4( + 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)]] fn vs_main( [[location(0)]] a_pos: vec2, @@ -27,23 +46,9 @@ fn vs_main( ) -> VertexOutput { var out: VertexOutput; out.tex_coord = a_tex_coord; - - // [u8; 4] SRGB as u32 -> [r, g, b, a] - let color = vec4( - f32(a_color & 255u), - f32((a_color >> 8u) & 255u), - f32((a_color >> 16u) & 255u), - f32((a_color >> 24u) & 255u), - ); + let color = unpack_color(a_color); out.color = vec4(linear_from_srgb(color.rgb), color.a / 255.0); - - out.position = vec4( - 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, - ); - + out.position = position_from_screen(a_pos); return out; } @@ -55,23 +60,9 @@ fn vs_conv_main( ) -> VertexOutput { var out: VertexOutput; out.tex_coord = a_tex_coord; - - // [u8; 4] SRGB as u32 -> [r, g, b, a] - let color = vec4( - f32(a_color & 255u), - f32((a_color >> 8u) & 255u), - f32((a_color >> 16u) & 255u), - f32((a_color >> 24u) & 255u), - ); + let color = unpack_color(a_color); out.color = vec4(color.rgba / 255.0); - - out.position = vec4( - 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, - ); - + out.position = position_from_screen(a_pos); return out; }