diff options
author | 2023-09-24 15:24:08 +0200 | |
---|---|---|
committer | 2023-09-24 15:24:08 +0200 | |
commit | 5fb877ab5984dd1c4a3f3dcccf87103393da4e0c (patch) | |
tree | efb995792bf06a33ab3c7910824dab707e9e489c /wgpu/src/image.rs | |
parent | e197abe0aae659742532ff2e2985afc97f041d2a (diff) | |
download | iced-5fb877ab5984dd1c4a3f3dcccf87103393da4e0c.tar.gz iced-5fb877ab5984dd1c4a3f3dcccf87103393da4e0c.tar.bz2 iced-5fb877ab5984dd1c4a3f3dcccf87103393da4e0c.zip |
Compute vertex position for image shader
Diffstat (limited to 'wgpu/src/image.rs')
-rw-r--r-- | wgpu/src/image.rs | 95 |
1 files changed, 18 insertions, 77 deletions
diff --git a/wgpu/src/image.rs b/wgpu/src/image.rs index 553ba330..384138a2 100644 --- a/wgpu/src/image.rs +++ b/wgpu/src/image.rs @@ -35,8 +35,6 @@ pub struct Pipeline { vector_cache: RefCell<vector::Cache>, 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::<Vertex>() 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::<Instance>() 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::<Instance>() 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); } @@ -491,29 +455,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 { _position: [f32; 2], |