From 3f467d121229142177ef1b2f417fe87c7bf7fdf2 Mon Sep 17 00:00:00 2001 From: Jim Eckerlein Date: Sun, 24 Sep 2023 15:10:19 +0200 Subject: [PATCH 1/6] Compute vertex position in shader --- wgpu/src/quad.rs | 61 ---------------------------- wgpu/src/quad/gradient.rs | 64 +++++++++++++----------------- wgpu/src/quad/solid.rs | 47 ++++++++++------------ wgpu/src/shader/quad.wgsl | 8 ++++ wgpu/src/shader/quad/gradient.wgsl | 4 +- wgpu/src/shader/quad/solid.wgsl | 4 +- 6 files changed, 60 insertions(+), 128 deletions(-) diff --git a/wgpu/src/quad.rs b/wgpu/src/quad.rs index 37d0c623ab..4bcf9a6658 100644 --- a/wgpu/src/quad.rs +++ b/wgpu/src/quad.rs @@ -9,7 +9,6 @@ use crate::graphics::color; use crate::graphics::{self, Transformation}; use bytemuck::{Pod, Zeroable}; -use wgpu::util::DeviceExt; use std::mem; @@ -23,8 +22,6 @@ pub struct Pipeline { solid: solid::Pipeline, gradient: gradient::Pipeline, constant_layout: wgpu::BindGroupLayout, - vertices: wgpu::Buffer, - indices: wgpu::Buffer, layers: Vec, prepare_layer: usize, } @@ -48,23 +45,7 @@ impl Pipeline { }], }); - let vertices = - device.create_buffer_init(&wgpu::util::BufferInitDescriptor { - label: Some("iced_wgpu::quad vertex buffer"), - contents: bytemuck::cast_slice(&VERTICES), - usage: wgpu::BufferUsages::VERTEX, - }); - - let indices = - device.create_buffer_init(&wgpu::util::BufferInitDescriptor { - label: Some("iced_wgpu::quad index buffer"), - contents: bytemuck::cast_slice(&INDICES), - usage: wgpu::BufferUsages::INDEX, - }); - Self { - vertices, - indices, solid: solid::Pipeline::new(device, format, &constant_layout), gradient: gradient::Pipeline::new(device, format, &constant_layout), layers: Vec::new(), @@ -105,11 +86,6 @@ impl Pipeline { bounds.width, bounds.height, ); - render_pass.set_index_buffer( - self.indices.slice(..), - wgpu::IndexFormat::Uint16, - ); - render_pass.set_vertex_buffer(0, self.vertices.slice(..)); let mut solid_offset = 0; let mut gradient_offset = 0; @@ -311,43 +287,6 @@ fn color_target_state( })] } -#[repr(C)] -#[derive(Clone, Copy, bytemuck::Zeroable, bytemuck::Pod)] -pub struct Vertex { - _position: [f32; 2], -} - -impl Vertex { - fn buffer_layout<'a>() -> wgpu::VertexBufferLayout<'a> { - wgpu::VertexBufferLayout { - array_stride: mem::size_of::() as u64, - step_mode: wgpu::VertexStepMode::Vertex, - attributes: &[wgpu::VertexAttribute { - shader_location: 0, - format: wgpu::VertexFormat::Float32x2, - offset: 0, - }], - } - } -} - -const INDICES: [u16; 6] = [0, 1, 2, 0, 2, 3]; - -const VERTICES: [Vertex; 4] = [ - Vertex { - _position: [0.0, 0.0], - }, - Vertex { - _position: [1.0, 0.0], - }, - Vertex { - _position: [1.0, 1.0], - }, - Vertex { - _position: [0.0, 1.0], - }, -]; - #[repr(C)] #[derive(Debug, Clone, Copy, bytemuck::Zeroable, bytemuck::Pod)] struct Uniforms { diff --git a/wgpu/src/quad/gradient.rs b/wgpu/src/quad/gradient.rs index a8e83d01f1..312408b75b 100644 --- a/wgpu/src/quad/gradient.rs +++ b/wgpu/src/quad/gradient.rs @@ -106,36 +106,32 @@ impl Pipeline { vertex: wgpu::VertexState { module: &shader, entry_point: "gradient_vs_main", - buffers: &[ - quad::Vertex::buffer_layout(), - wgpu::VertexBufferLayout { - array_stride: std::mem::size_of::() - as u64, - step_mode: wgpu::VertexStepMode::Instance, - attributes: &wgpu::vertex_attr_array!( - // Colors 1-2 - 1 => Uint32x4, - // Colors 3-4 - 2 => Uint32x4, - // Colors 5-6 - 3 => Uint32x4, - // Colors 7-8 - 4 => Uint32x4, - // Offsets 1-8 - 5 => Uint32x4, - // Direction - 6 => Float32x4, - // Position & Scale - 7 => Float32x4, - // Border color - 8 => Float32x4, - // Border radius - 9 => Float32x4, - // Border width - 10 => Float32 - ), - }, - ], + buffers: &[wgpu::VertexBufferLayout { + array_stride: std::mem::size_of::() as u64, + step_mode: wgpu::VertexStepMode::Instance, + attributes: &wgpu::vertex_attr_array!( + // Colors 1-2 + 1 => Uint32x4, + // Colors 3-4 + 2 => Uint32x4, + // Colors 5-6 + 3 => Uint32x4, + // Colors 7-8 + 4 => Uint32x4, + // Offsets 1-8 + 5 => Uint32x4, + // Direction + 6 => Float32x4, + // Position & Scale + 7 => Float32x4, + // Border color + 8 => Float32x4, + // Border radius + 9 => Float32x4, + // Border width + 10 => Float32 + ), + }], }, fragment: Some(wgpu::FragmentState { module: &shader, @@ -171,12 +167,8 @@ impl Pipeline { render_pass.set_pipeline(&self.pipeline); render_pass.set_bind_group(0, constants, &[]); - render_pass.set_vertex_buffer(1, layer.instances.slice(..)); + render_pass.set_vertex_buffer(0, layer.instances.slice(..)); - render_pass.draw_indexed( - 0..quad::INDICES.len() as u32, - 0, - range.start as u32..range.end as u32, - ); + render_pass.draw(0..6, range.start as u32..range.end as u32); } } diff --git a/wgpu/src/quad/solid.rs b/wgpu/src/quad/solid.rs index 9bc6b4669b..bab7367df6 100644 --- a/wgpu/src/quad/solid.rs +++ b/wgpu/src/quad/solid.rs @@ -87,27 +87,24 @@ impl Pipeline { vertex: wgpu::VertexState { module: &shader, entry_point: "solid_vs_main", - buffers: &[ - quad::Vertex::buffer_layout(), - wgpu::VertexBufferLayout { - array_stride: std::mem::size_of::() as u64, - step_mode: wgpu::VertexStepMode::Instance, - attributes: &wgpu::vertex_attr_array!( - // Color - 1 => Float32x4, - // Position - 2 => Float32x2, - // Size - 3 => Float32x2, - // Border color - 4 => Float32x4, - // Border radius - 5 => Float32x4, - // Border width - 6 => Float32, - ), - }, - ], + buffers: &[wgpu::VertexBufferLayout { + array_stride: std::mem::size_of::() as u64, + step_mode: wgpu::VertexStepMode::Instance, + attributes: &wgpu::vertex_attr_array!( + // Color + 1 => Float32x4, + // Position + 2 => Float32x2, + // Size + 3 => Float32x2, + // Border color + 4 => Float32x4, + // Border radius + 5 => Float32x4, + // Border width + 6 => Float32, + ), + }], }, fragment: Some(wgpu::FragmentState { module: &shader, @@ -143,12 +140,8 @@ impl Pipeline { render_pass.set_pipeline(&self.pipeline); render_pass.set_bind_group(0, constants, &[]); - render_pass.set_vertex_buffer(1, layer.instances.slice(..)); + render_pass.set_vertex_buffer(0, layer.instances.slice(..)); - render_pass.draw_indexed( - 0..quad::INDICES.len() as u32, - 0, - range.start as u32..range.end as u32, - ); + render_pass.draw(0..6, range.start as u32..range.end as u32); } } diff --git a/wgpu/src/shader/quad.wgsl b/wgpu/src/shader/quad.wgsl index f919cfe24e..555e81bb97 100644 --- a/wgpu/src/shader/quad.wgsl +++ b/wgpu/src/shader/quad.wgsl @@ -37,3 +37,11 @@ fn select_border_radius(radi: vec4, position: vec2, center: vec2) rx = select(rx, ry, position.y > center.y); return rx; } + +// Compute the normalized quad coordinates based on the vertex index. +fn vertex_position(vertex_index: u32) -> vec2 { + // #: 0 1 2 3 4 5 + // x: 1 1 0 0 0 1 + // y: 1 0 0 0 1 1 + return vec2((vec2(1u, 2u) + vertex_index) % 6u < 3u); +} diff --git a/wgpu/src/shader/quad/gradient.wgsl b/wgpu/src/shader/quad/gradient.wgsl index 0754e97f62..36cae61c0a 100644 --- a/wgpu/src/shader/quad/gradient.wgsl +++ b/wgpu/src/shader/quad/gradient.wgsl @@ -1,5 +1,5 @@ struct GradientVertexInput { - @location(0) v_pos: vec2, + @builtin(vertex_index) vertex_index: u32, @location(1) @interpolate(flat) colors_1: vec4, @location(2) @interpolate(flat) colors_2: vec4, @location(3) @interpolate(flat) colors_3: vec4, @@ -48,7 +48,7 @@ fn gradient_vs_main(input: GradientVertexInput) -> GradientVertexOutput { vec4(pos - vec2(0.5, 0.5), 0.0, 1.0) ); - out.position = globals.transform * transform * vec4(input.v_pos, 0.0, 1.0); + out.position = globals.transform * transform * vec4(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 ebd6d87720..5fc5b13abd 100644 --- a/wgpu/src/shader/quad/solid.wgsl +++ b/wgpu/src/shader/quad/solid.wgsl @@ -1,5 +1,5 @@ struct SolidVertexInput { - @location(0) v_pos: vec2, + @builtin(vertex_index) vertex_index: u32, @location(1) color: vec4, @location(2) pos: vec2, @location(3) scale: vec2, @@ -40,7 +40,7 @@ fn solid_vs_main(input: SolidVertexInput) -> SolidVertexOutput { vec4(pos - vec2(0.5, 0.5), 0.0, 1.0) ); - out.position = globals.transform * transform * vec4(input.v_pos, 0.0, 1.0); + out.position = globals.transform * transform * vec4(vertex_position(input.vertex_index), 0.0, 1.0); out.color = input.color; out.border_color = input.border_color; out.pos = pos; From bcc55e6036df0a2f9bdc7a21bf6ac98c03dd29ae Mon Sep 17 00:00:00 2001 From: Jim Eckerlein Date: Sun, 24 Sep 2023 15:12:32 +0200 Subject: [PATCH 2/6] Reassign attribute locations --- wgpu/src/quad/gradient.rs | 20 ++++++++++---------- wgpu/src/quad/solid.rs | 12 ++++++------ wgpu/src/shader/quad/gradient.wgsl | 20 ++++++++++---------- wgpu/src/shader/quad/solid.wgsl | 12 ++++++------ 4 files changed, 32 insertions(+), 32 deletions(-) diff --git a/wgpu/src/quad/gradient.rs b/wgpu/src/quad/gradient.rs index 312408b75b..ff30f78f71 100644 --- a/wgpu/src/quad/gradient.rs +++ b/wgpu/src/quad/gradient.rs @@ -111,25 +111,25 @@ impl Pipeline { step_mode: wgpu::VertexStepMode::Instance, attributes: &wgpu::vertex_attr_array!( // Colors 1-2 - 1 => Uint32x4, + 0 => Uint32x4, // Colors 3-4 - 2 => Uint32x4, + 1 => Uint32x4, // Colors 5-6 - 3 => Uint32x4, + 2 => Uint32x4, // Colors 7-8 - 4 => Uint32x4, + 3 => Uint32x4, // Offsets 1-8 - 5 => Uint32x4, + 4 => Uint32x4, // Direction - 6 => Float32x4, + 5 => Float32x4, // Position & Scale - 7 => Float32x4, + 6 => Float32x4, // Border color - 8 => Float32x4, + 7 => Float32x4, // Border radius - 9 => Float32x4, + 8 => Float32x4, // Border width - 10 => Float32 + 9 => Float32 ), }], }, diff --git a/wgpu/src/quad/solid.rs b/wgpu/src/quad/solid.rs index bab7367df6..96e73ba82d 100644 --- a/wgpu/src/quad/solid.rs +++ b/wgpu/src/quad/solid.rs @@ -92,17 +92,17 @@ impl Pipeline { step_mode: wgpu::VertexStepMode::Instance, attributes: &wgpu::vertex_attr_array!( // Color - 1 => Float32x4, + 0 => Float32x4, // Position - 2 => Float32x2, + 1 => Float32x2, // Size - 3 => Float32x2, + 2 => Float32x2, // Border color - 4 => Float32x4, + 3 => Float32x4, // Border radius - 5 => Float32x4, + 4 => Float32x4, // Border width - 6 => Float32, + 5 => Float32, ), }], }, diff --git a/wgpu/src/shader/quad/gradient.wgsl b/wgpu/src/shader/quad/gradient.wgsl index 36cae61c0a..4ad2fea89e 100644 --- a/wgpu/src/shader/quad/gradient.wgsl +++ b/wgpu/src/shader/quad/gradient.wgsl @@ -1,15 +1,15 @@ struct GradientVertexInput { @builtin(vertex_index) vertex_index: u32, - @location(1) @interpolate(flat) colors_1: vec4, - @location(2) @interpolate(flat) colors_2: vec4, - @location(3) @interpolate(flat) colors_3: vec4, - @location(4) @interpolate(flat) colors_4: vec4, - @location(5) @interpolate(flat) offsets: vec4, - @location(6) direction: vec4, - @location(7) position_and_scale: vec4, - @location(8) border_color: vec4, - @location(9) border_radius: vec4, - @location(10) border_width: f32, + @location(0) @interpolate(flat) colors_1: vec4, + @location(1) @interpolate(flat) colors_2: vec4, + @location(2) @interpolate(flat) colors_3: vec4, + @location(3) @interpolate(flat) colors_4: vec4, + @location(4) @interpolate(flat) offsets: vec4, + @location(5) direction: vec4, + @location(6) position_and_scale: vec4, + @location(7) border_color: vec4, + @location(8) border_radius: vec4, + @location(9) border_width: f32, } struct GradientVertexOutput { diff --git a/wgpu/src/shader/quad/solid.wgsl b/wgpu/src/shader/quad/solid.wgsl index 5fc5b13abd..f84dd7abb7 100644 --- a/wgpu/src/shader/quad/solid.wgsl +++ b/wgpu/src/shader/quad/solid.wgsl @@ -1,11 +1,11 @@ struct SolidVertexInput { @builtin(vertex_index) vertex_index: u32, - @location(1) color: vec4, - @location(2) pos: vec2, - @location(3) scale: vec2, - @location(4) border_color: vec4, - @location(5) border_radius: vec4, - @location(6) border_width: f32, + @location(0) color: vec4, + @location(1) pos: vec2, + @location(2) scale: vec2, + @location(3) border_color: vec4, + @location(4) border_radius: vec4, + @location(5) border_width: f32, } struct SolidVertexOutput { From e197abe0aae659742532ff2e2985afc97f041d2a Mon Sep 17 00:00:00 2001 From: Jim Eckerlein Date: Sun, 24 Sep 2023 15:19:07 +0200 Subject: [PATCH 3/6] Move vertex position function into own file --- wgpu/src/quad/gradient.rs | 4 ++++ wgpu/src/quad/solid.rs | 2 ++ wgpu/src/shader/quad.wgsl | 8 -------- wgpu/src/shader/vertex.wgsl | 7 +++++++ 4 files changed, 13 insertions(+), 8 deletions(-) create mode 100644 wgpu/src/shader/vertex.wgsl diff --git a/wgpu/src/quad/gradient.rs b/wgpu/src/quad/gradient.rs index ff30f78f71..60b170cc73 100644 --- a/wgpu/src/quad/gradient.rs +++ b/wgpu/src/quad/gradient.rs @@ -83,6 +83,8 @@ impl Pipeline { concat!( include_str!("../shader/quad.wgsl"), "\n", + include_str!("../shader/vertex.wgsl"), + "\n", include_str!("../shader/quad/gradient.wgsl"), "\n", include_str!("../shader/color/oklab.wgsl") @@ -91,6 +93,8 @@ impl Pipeline { concat!( include_str!("../shader/quad.wgsl"), "\n", + include_str!("../shader/vertex.wgsl"), + "\n", include_str!("../shader/quad/gradient.wgsl"), "\n", include_str!("../shader/color/linear_rgb.wgsl") diff --git a/wgpu/src/quad/solid.rs b/wgpu/src/quad/solid.rs index 96e73ba82d..90e7f98e3d 100644 --- a/wgpu/src/quad/solid.rs +++ b/wgpu/src/quad/solid.rs @@ -75,6 +75,8 @@ impl Pipeline { concat!( include_str!("../shader/quad.wgsl"), "\n", + include_str!("../shader/vertex.wgsl"), + "\n", include_str!("../shader/quad/solid.wgsl"), ), )), diff --git a/wgpu/src/shader/quad.wgsl b/wgpu/src/shader/quad.wgsl index 555e81bb97..f919cfe24e 100644 --- a/wgpu/src/shader/quad.wgsl +++ b/wgpu/src/shader/quad.wgsl @@ -37,11 +37,3 @@ fn select_border_radius(radi: vec4, position: vec2, center: vec2) rx = select(rx, ry, position.y > center.y); return rx; } - -// Compute the normalized quad coordinates based on the vertex index. -fn vertex_position(vertex_index: u32) -> vec2 { - // #: 0 1 2 3 4 5 - // x: 1 1 0 0 0 1 - // y: 1 0 0 0 1 1 - return vec2((vec2(1u, 2u) + vertex_index) % 6u < 3u); -} diff --git a/wgpu/src/shader/vertex.wgsl b/wgpu/src/shader/vertex.wgsl new file mode 100644 index 0000000000..904f847054 --- /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 { + // #: 0 1 2 3 4 5 + // x: 1 1 0 0 0 1 + // y: 1 0 0 0 1 1 + return vec2((vec2(1u, 2u) + vertex_index) % 6u < 3u); +} From 5fb877ab5984dd1c4a3f3dcccf87103393da4e0c Mon Sep 17 00:00:00 2001 From: Jim Eckerlein Date: Sun, 24 Sep 2023 15:24:08 +0200 Subject: [PATCH 4/6] Compute vertex position for image shader --- wgpu/src/image.rs | 95 ++++++++------------------------------ wgpu/src/shader/image.wgsl | 8 ++-- 2 files changed, 23 insertions(+), 80 deletions(-) diff --git a/wgpu/src/image.rs b/wgpu/src/image.rs index 553ba33057..384138a22d 100644 --- a/wgpu/src/image.rs +++ b/wgpu/src/image.rs @@ -35,8 +35,6 @@ pub struct Pipeline { vector_cache: RefCell, pipeline: wgpu::RenderPipeline, - vertices: wgpu::Buffer, - indices: wgpu::Buffer, sampler: wgpu::Sampler, texture: wgpu::BindGroup, texture_version: usize, @@ -128,20 +126,14 @@ impl Layer { fn render<'a>(&'a self, render_pass: &mut wgpu::RenderPass<'a>) { render_pass.set_bind_group(0, &self.constants, &[]); - render_pass.set_vertex_buffer(1, self.instances.slice(..)); + render_pass.set_vertex_buffer(0, self.instances.slice(..)); - render_pass.draw_indexed( - 0..QUAD_INDICES.len() as u32, - 0, - 0..self.instance_count as u32, - ); + render_pass.draw(0..6, 0..self.instance_count as u32); } } impl Pipeline { pub fn new(device: &wgpu::Device, format: wgpu::TextureFormat) -> Self { - use wgpu::util::DeviceExt; - let sampler = device.create_sampler(&wgpu::SamplerDescriptor { address_mode_u: wgpu::AddressMode::ClampToEdge, address_mode_v: wgpu::AddressMode::ClampToEdge, @@ -207,7 +199,11 @@ impl Pipeline { device.create_shader_module(wgpu::ShaderModuleDescriptor { label: Some("iced_wgpu image shader"), source: wgpu::ShaderSource::Wgsl(std::borrow::Cow::Borrowed( - include_str!("shader/image.wgsl"), + concat!( + include_str!("shader/vertex.wgsl"), + "\n", + include_str!("shader/image.wgsl"), + ), )), }); @@ -218,28 +214,17 @@ impl Pipeline { vertex: wgpu::VertexState { module: &shader, entry_point: "vs_main", - buffers: &[ - wgpu::VertexBufferLayout { - array_stride: mem::size_of::() as u64, - step_mode: wgpu::VertexStepMode::Vertex, - attributes: &[wgpu::VertexAttribute { - shader_location: 0, - format: wgpu::VertexFormat::Float32x2, - offset: 0, - }], - }, - wgpu::VertexBufferLayout { - array_stride: mem::size_of::() as u64, - step_mode: wgpu::VertexStepMode::Instance, - attributes: &wgpu::vertex_attr_array!( - 1 => Float32x2, - 2 => Float32x2, - 3 => Float32x2, - 4 => Float32x2, - 5 => Sint32, - ), - }, - ], + buffers: &[wgpu::VertexBufferLayout { + array_stride: mem::size_of::() as u64, + step_mode: wgpu::VertexStepMode::Instance, + attributes: &wgpu::vertex_attr_array!( + 1 => Float32x2, + 2 => Float32x2, + 3 => Float32x2, + 4 => Float32x2, + 5 => Sint32, + ), + }], }, fragment: Some(wgpu::FragmentState { module: &shader, @@ -275,20 +260,6 @@ impl Pipeline { multiview: None, }); - let vertices = - device.create_buffer_init(&wgpu::util::BufferInitDescriptor { - label: Some("iced_wgpu::image vertex buffer"), - contents: bytemuck::cast_slice(&QUAD_VERTICES), - usage: wgpu::BufferUsages::VERTEX, - }); - - let indices = - device.create_buffer_init(&wgpu::util::BufferInitDescriptor { - label: Some("iced_wgpu::image index buffer"), - contents: bytemuck::cast_slice(&QUAD_INDICES), - usage: wgpu::BufferUsages::INDEX, - }); - let texture_atlas = Atlas::new(device); let texture = device.create_bind_group(&wgpu::BindGroupDescriptor { @@ -310,8 +281,6 @@ impl Pipeline { vector_cache: RefCell::new(vector::Cache::default()), pipeline, - vertices, - indices, sampler, texture, texture_version: texture_atlas.layer_count(), @@ -469,11 +438,6 @@ impl Pipeline { ); render_pass.set_bind_group(1, &self.texture, &[]); - render_pass.set_index_buffer( - self.indices.slice(..), - wgpu::IndexFormat::Uint16, - ); - render_pass.set_vertex_buffer(0, self.vertices.slice(..)); layer.render(render_pass); } @@ -490,29 +454,6 @@ impl Pipeline { } } -#[repr(C)] -#[derive(Clone, Copy, Zeroable, Pod)] -pub struct Vertex { - _position: [f32; 2], -} - -const QUAD_INDICES: [u16; 6] = [0, 1, 2, 0, 2, 3]; - -const QUAD_VERTICES: [Vertex; 4] = [ - Vertex { - _position: [0.0, 0.0], - }, - Vertex { - _position: [1.0, 0.0], - }, - Vertex { - _position: [1.0, 1.0], - }, - Vertex { - _position: [0.0, 1.0], - }, -]; - #[repr(C)] #[derive(Debug, Clone, Copy, Zeroable, Pod)] struct Instance { diff --git a/wgpu/src/shader/image.wgsl b/wgpu/src/shader/image.wgsl index 5e22cdf482..0c8b3bdb00 100644 --- a/wgpu/src/shader/image.wgsl +++ b/wgpu/src/shader/image.wgsl @@ -7,7 +7,7 @@ struct Globals { @group(1) @binding(0) var u_texture: texture_2d_array; struct VertexInput { - @location(0) v_pos: vec2, + @builtin(vertex_index) vertex_index: u32, @location(1) pos: vec2, @location(2) scale: vec2, @location(3) atlas_pos: vec2, @@ -25,7 +25,9 @@ struct VertexOutput { fn vs_main(input: VertexInput) -> VertexOutput { var out: VertexOutput; - out.uv = vec2(input.v_pos * input.atlas_scale + input.atlas_pos); + let v_pos = vertex_position(input.vertex_index); + + out.uv = vec2(v_pos * input.atlas_scale + input.atlas_pos); out.layer = f32(input.layer); var transform: mat4x4 = mat4x4( @@ -35,7 +37,7 @@ fn vs_main(input: VertexInput) -> VertexOutput { vec4(input.pos, 0.0, 1.0) ); - out.position = globals.transform * transform * vec4(input.v_pos, 0.0, 1.0); + out.position = globals.transform * transform * vec4(v_pos, 0.0, 1.0); return out; } From 41dec5bd203ff5b1574a33a17d5f7358ae1beea2 Mon Sep 17 00:00:00 2001 From: Jim Eckerlein Date: Sun, 24 Sep 2023 15:26:10 +0200 Subject: [PATCH 5/6] Reassign attribute locations for image shader --- wgpu/src/image.rs | 9 +++++++-- wgpu/src/shader/image.wgsl | 10 +++++----- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/wgpu/src/image.rs b/wgpu/src/image.rs index 384138a22d..36c1e22886 100644 --- a/wgpu/src/image.rs +++ b/wgpu/src/image.rs @@ -218,11 +218,16 @@ impl Pipeline { array_stride: mem::size_of::() as u64, step_mode: wgpu::VertexStepMode::Instance, attributes: &wgpu::vertex_attr_array!( + // Position + 0 => Float32x2, + // Scale 1 => Float32x2, + // Atlas position 2 => Float32x2, + // Atlas scale 3 => Float32x2, - 4 => Float32x2, - 5 => Sint32, + // Layer + 4 => Sint32, ), }], }, diff --git a/wgpu/src/shader/image.wgsl b/wgpu/src/shader/image.wgsl index 0c8b3bdb00..7b2e523855 100644 --- a/wgpu/src/shader/image.wgsl +++ b/wgpu/src/shader/image.wgsl @@ -8,11 +8,11 @@ struct Globals { struct VertexInput { @builtin(vertex_index) vertex_index: u32, - @location(1) pos: vec2, - @location(2) scale: vec2, - @location(3) atlas_pos: vec2, - @location(4) atlas_scale: vec2, - @location(5) layer: i32, + @location(0) pos: vec2, + @location(1) scale: vec2, + @location(2) atlas_pos: vec2, + @location(3) atlas_scale: vec2, + @location(4) layer: i32, } struct VertexOutput { From 0c7f6e4b34391c709aa4c333c4a9cc10e607f6c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Fri, 19 Jan 2024 20:43:18 +0100 Subject: [PATCH 6/6] Fix `vertex.wgsl` shader in `iced_wgpu` --- wgpu/src/shader/vertex.wgsl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wgpu/src/shader/vertex.wgsl b/wgpu/src/shader/vertex.wgsl index 904f847054..e6af6fc008 100644 --- a/wgpu/src/shader/vertex.wgsl +++ b/wgpu/src/shader/vertex.wgsl @@ -3,5 +3,5 @@ fn vertex_position(vertex_index: u32) -> vec2 { // #: 0 1 2 3 4 5 // x: 1 1 0 0 0 1 // y: 1 0 0 0 1 1 - return vec2((vec2(1u, 2u) + vertex_index) % 6u < 3u); + return vec2((vec2(1u, 2u) + vertex_index) % vec2(6u) < vec2(3u)); }