From 4ac455a5b9d27e49321ffc89d310377cfa8b4e88 Mon Sep 17 00:00:00 2001 From: Lukas Kalbertodt Date: Sat, 20 Jan 2024 13:52:58 +0100 Subject: [PATCH] Clean WGSL shader code using newer features It looks like this code has been written quite a while ago, presumably when the capabilities of naga regarding WGSL were quite limited yet. I changed some stuff to what I would perceive as more idiomatic. I removed a bunch of type annotations, used built-in type aliases, and even used abstract types new with wgpu 0.19. --- src/shader/glyph.wgsl | 56 +++++++++++++++++++++---------------------- 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/src/shader/glyph.wgsl b/src/shader/glyph.wgsl index c8a5910..d804da3 100644 --- a/src/shader/glyph.wgsl +++ b/src/shader/glyph.wgsl @@ -8,62 +8,62 @@ struct Globals { struct VertexInput { @builtin(vertex_index) vertex_index: u32, - @location(0) left_top: vec3, - @location(1) right_bottom: vec2, - @location(2) tex_left_top: vec2, - @location(3) tex_right_bottom: vec2, - @location(4) color: vec4, + @location(0) left_top: vec3f, + @location(1) right_bottom: vec2f, + @location(2) tex_left_top: vec2f, + @location(3) tex_right_bottom: vec2f, + @location(4) color: vec4f, } struct VertexOutput { - @builtin(position) position: vec4, - @location(0) f_tex_pos: vec2, - @location(1) f_color: vec4, + @builtin(position) position: vec4f, + @location(0) f_tex_pos: vec2f, + @location(1) f_color: vec4f, } @vertex fn vs_main(input: VertexInput) -> VertexOutput { var out: VertexOutput; - var pos: vec2 = vec2(0.0, 0.0); - var left: f32 = input.left_top.x; - var right: f32 = input.right_bottom.x; - var top: f32 = input.left_top.y; - var bottom: f32 = input.right_bottom.y; + var pos = vec2f(0, 0); + let left = input.left_top.x; + let right = input.right_bottom.x; + let top = input.left_top.y; + let bottom = input.right_bottom.y; - switch (i32(input.vertex_index)) { - case 0: { - pos = vec2(left, top); + switch input.vertex_index { + case 0u: { + pos = vec2(left, top); out.f_tex_pos = input.tex_left_top; } - case 1: { - pos = vec2(right, top); - out.f_tex_pos = vec2(input.tex_right_bottom.x, input.tex_left_top.y); + case 1u: { + pos = vec2(right, top); + out.f_tex_pos = vec2(input.tex_right_bottom.x, input.tex_left_top.y); } - case 2: { - pos = vec2(left, bottom); - out.f_tex_pos = vec2(input.tex_left_top.x, input.tex_right_bottom.y); + case 2u: { + pos = vec2(left, bottom); + out.f_tex_pos = vec2(input.tex_left_top.x, input.tex_right_bottom.y); } - case 3: { - pos = vec2(right, bottom); + case 3u: { + pos = vec2(right, bottom); out.f_tex_pos = input.tex_right_bottom; } default: {} } out.f_color = input.color; - out.position = globals.transform * vec4(pos, input.left_top.z, 1.0); + out.position = globals.transform * vec4(pos, input.left_top.z, 1.0); return out; } @fragment -fn fs_main(input: VertexOutput) -> @location(0) vec4 { - var alpha: f32 = textureSample(font_tex, font_sampler, input.f_tex_pos).r; +fn fs_main(input: VertexOutput) -> @location(0) vec4f { + var alpha = textureSample(font_tex, font_sampler, input.f_tex_pos).r; if (alpha <= 0.0) { discard; } - return input.f_color * vec4(1.0, 1.0, 1.0, alpha); + return input.f_color * vec4f(1.0, 1.0, 1.0, alpha); }