diff options
author | 2024-01-19 20:54:09 +0100 | |
---|---|---|
committer | 2024-01-19 20:54:09 +0100 | |
commit | b3e3f6e3c9fc6879e6681810f54d7eaa7c0f3d30 (patch) | |
tree | ef1a846a756fadce7df07732538192ee4f4ce6cf /wgpu/src/shader | |
parent | 7ae7fcb89855002519bab752fd3686106ce448db (diff) | |
parent | 0c7f6e4b34391c709aa4c333c4a9cc10e607f6c4 (diff) | |
download | iced-b3e3f6e3c9fc6879e6681810f54d7eaa7c0f3d30.tar.gz iced-b3e3f6e3c9fc6879e6681810f54d7eaa7c0f3d30.tar.bz2 iced-b3e3f6e3c9fc6879e6681810f54d7eaa7c0f3d30.zip |
Merge pull request #2099 from jim-ec/master
Compute vertex positions in the shader
Diffstat (limited to 'wgpu/src/shader')
-rw-r--r-- | wgpu/src/shader/image.wgsl | 18 | ||||
-rw-r--r-- | wgpu/src/shader/quad/gradient.wgsl | 24 | ||||
-rw-r--r-- | wgpu/src/shader/quad/solid.wgsl | 16 | ||||
-rw-r--r-- | wgpu/src/shader/vertex.wgsl | 7 |
4 files changed, 37 insertions, 28 deletions
diff --git a/wgpu/src/shader/image.wgsl b/wgpu/src/shader/image.wgsl index 5e22cdf4..7b2e5238 100644 --- a/wgpu/src/shader/image.wgsl +++ b/wgpu/src/shader/image.wgsl @@ -7,12 +7,12 @@ struct Globals { @group(1) @binding(0) var u_texture: texture_2d_array<f32>; struct VertexInput { - @location(0) v_pos: vec2<f32>, - @location(1) pos: vec2<f32>, - @location(2) scale: vec2<f32>, - @location(3) atlas_pos: vec2<f32>, - @location(4) atlas_scale: vec2<f32>, - @location(5) layer: i32, + @builtin(vertex_index) vertex_index: u32, + @location(0) pos: vec2<f32>, + @location(1) scale: vec2<f32>, + @location(2) atlas_pos: vec2<f32>, + @location(3) atlas_scale: vec2<f32>, + @location(4) layer: i32, } struct VertexOutput { @@ -25,7 +25,9 @@ struct VertexOutput { fn vs_main(input: VertexInput) -> VertexOutput { var out: VertexOutput; - out.uv = vec2<f32>(input.v_pos * input.atlas_scale + input.atlas_pos); + let v_pos = vertex_position(input.vertex_index); + + out.uv = vec2<f32>(v_pos * input.atlas_scale + input.atlas_pos); out.layer = f32(input.layer); var transform: mat4x4<f32> = mat4x4<f32>( @@ -35,7 +37,7 @@ fn vs_main(input: VertexInput) -> VertexOutput { vec4<f32>(input.pos, 0.0, 1.0) ); - out.position = globals.transform * transform * vec4<f32>(input.v_pos, 0.0, 1.0); + out.position = globals.transform * transform * vec4<f32>(v_pos, 0.0, 1.0); return out; } diff --git a/wgpu/src/shader/quad/gradient.wgsl b/wgpu/src/shader/quad/gradient.wgsl index 0754e97f..4ad2fea8 100644 --- a/wgpu/src/shader/quad/gradient.wgsl +++ b/wgpu/src/shader/quad/gradient.wgsl @@ -1,15 +1,15 @@ struct GradientVertexInput { - @location(0) v_pos: vec2<f32>, - @location(1) @interpolate(flat) colors_1: vec4<u32>, - @location(2) @interpolate(flat) colors_2: vec4<u32>, - @location(3) @interpolate(flat) colors_3: vec4<u32>, - @location(4) @interpolate(flat) colors_4: vec4<u32>, - @location(5) @interpolate(flat) offsets: vec4<u32>, - @location(6) direction: vec4<f32>, - @location(7) position_and_scale: vec4<f32>, - @location(8) border_color: vec4<f32>, - @location(9) border_radius: vec4<f32>, - @location(10) border_width: f32, + @builtin(vertex_index) vertex_index: u32, + @location(0) @interpolate(flat) colors_1: vec4<u32>, + @location(1) @interpolate(flat) colors_2: vec4<u32>, + @location(2) @interpolate(flat) colors_3: vec4<u32>, + @location(3) @interpolate(flat) colors_4: vec4<u32>, + @location(4) @interpolate(flat) offsets: vec4<u32>, + @location(5) direction: vec4<f32>, + @location(6) position_and_scale: vec4<f32>, + @location(7) border_color: vec4<f32>, + @location(8) border_radius: vec4<f32>, + @location(9) border_width: f32, } struct GradientVertexOutput { @@ -48,7 +48,7 @@ fn gradient_vs_main(input: GradientVertexInput) -> GradientVertexOutput { vec4<f32>(pos - vec2<f32>(0.5, 0.5), 0.0, 1.0) ); - out.position = globals.transform * transform * vec4<f32>(input.v_pos, 0.0, 1.0); + out.position = globals.transform * transform * vec4<f32>(vertex_position(input.vertex_index), 0.0, 1.0); out.colors_1 = input.colors_1; out.colors_2 = input.colors_2; out.colors_3 = input.colors_3; diff --git a/wgpu/src/shader/quad/solid.wgsl b/wgpu/src/shader/quad/solid.wgsl index ebd6d877..f84dd7ab 100644 --- a/wgpu/src/shader/quad/solid.wgsl +++ b/wgpu/src/shader/quad/solid.wgsl @@ -1,11 +1,11 @@ struct SolidVertexInput { - @location(0) v_pos: vec2<f32>, - @location(1) color: vec4<f32>, - @location(2) pos: vec2<f32>, - @location(3) scale: vec2<f32>, - @location(4) border_color: vec4<f32>, - @location(5) border_radius: vec4<f32>, - @location(6) border_width: f32, + @builtin(vertex_index) vertex_index: u32, + @location(0) color: vec4<f32>, + @location(1) pos: vec2<f32>, + @location(2) scale: vec2<f32>, + @location(3) border_color: vec4<f32>, + @location(4) border_radius: vec4<f32>, + @location(5) border_width: f32, } struct SolidVertexOutput { @@ -40,7 +40,7 @@ fn solid_vs_main(input: SolidVertexInput) -> SolidVertexOutput { vec4<f32>(pos - vec2<f32>(0.5, 0.5), 0.0, 1.0) ); - out.position = globals.transform * transform * vec4<f32>(input.v_pos, 0.0, 1.0); + out.position = globals.transform * transform * vec4<f32>(vertex_position(input.vertex_index), 0.0, 1.0); out.color = input.color; out.border_color = input.border_color; out.pos = pos; diff --git a/wgpu/src/shader/vertex.wgsl b/wgpu/src/shader/vertex.wgsl new file mode 100644 index 00000000..e6af6fc0 --- /dev/null +++ b/wgpu/src/shader/vertex.wgsl @@ -0,0 +1,7 @@ +// Compute the normalized quad coordinates based on the vertex index. +fn vertex_position(vertex_index: u32) -> vec2<f32> { + // #: 0 1 2 3 4 5 + // x: 1 1 0 0 0 1 + // y: 1 0 0 0 1 1 + return vec2<f32>((vec2(1u, 2u) + vertex_index) % vec2(6u) < vec2(3u)); +} |