diff options
Diffstat (limited to '')
-rw-r--r-- | wgpu/src/image.rs | 159 | ||||
-rw-r--r-- | wgpu/src/image/atlas.rs | 38 | ||||
-rw-r--r-- | wgpu/src/image/vector.rs | 4 |
3 files changed, 100 insertions, 101 deletions
diff --git a/wgpu/src/image.rs b/wgpu/src/image.rs index d3603676..7145f3f3 100644 --- a/wgpu/src/image.rs +++ b/wgpu/src/image.rs @@ -12,6 +12,7 @@ use atlas::Atlas; use iced_native::Rectangle; use std::cell::RefCell; use std::mem; +use zerocopy::AsBytes; #[cfg(feature = "image")] use iced_native::image; @@ -49,21 +50,22 @@ impl Pipeline { mipmap_filter: wgpu::FilterMode::Linear, lod_min_clamp: -100.0, lod_max_clamp: 100.0, - compare_function: wgpu::CompareFunction::Always, + compare: wgpu::CompareFunction::Always, }); let constant_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { + label: None, bindings: &[ - wgpu::BindGroupLayoutBinding { + wgpu::BindGroupLayoutEntry { binding: 0, visibility: wgpu::ShaderStage::VERTEX, ty: wgpu::BindingType::UniformBuffer { dynamic: false }, }, - wgpu::BindGroupLayoutBinding { + wgpu::BindGroupLayoutEntry { binding: 1, visibility: wgpu::ShaderStage::FRAGMENT, - ty: wgpu::BindingType::Sampler, + ty: wgpu::BindingType::Sampler { comparison: false }, }, ], }); @@ -72,15 +74,14 @@ impl Pipeline { transform: Transformation::identity().into(), }; - let uniforms_buffer = device - .create_buffer_mapped( - 1, - wgpu::BufferUsage::UNIFORM | wgpu::BufferUsage::COPY_DST, - ) - .fill_from_slice(&[uniforms]); + let uniforms_buffer = device.create_buffer_with_data( + uniforms.as_bytes(), + wgpu::BufferUsage::UNIFORM | wgpu::BufferUsage::COPY_DST, + ); let constant_bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor { + label: None, layout: &constant_layout, bindings: &[ wgpu::Binding { @@ -99,12 +100,14 @@ impl Pipeline { let texture_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { - bindings: &[wgpu::BindGroupLayoutBinding { + label: None, + bindings: &[wgpu::BindGroupLayoutEntry { binding: 0, visibility: wgpu::ShaderStage::FRAGMENT, ty: wgpu::BindingType::SampledTexture { - multisampled: false, dimension: wgpu::TextureViewDimension::D2, + component_type: wgpu::TextureComponentType::Float, + multisampled: false, }, }], }); @@ -160,63 +163,68 @@ impl Pipeline { write_mask: wgpu::ColorWrite::ALL, }], depth_stencil_state: None, - index_format: wgpu::IndexFormat::Uint16, - vertex_buffers: &[ - wgpu::VertexBufferDescriptor { - stride: mem::size_of::<Vertex>() as u64, - step_mode: wgpu::InputStepMode::Vertex, - attributes: &[wgpu::VertexAttributeDescriptor { - shader_location: 0, - format: wgpu::VertexFormat::Float2, - offset: 0, - }], - }, - wgpu::VertexBufferDescriptor { - stride: mem::size_of::<Instance>() as u64, - step_mode: wgpu::InputStepMode::Instance, - attributes: &[ - wgpu::VertexAttributeDescriptor { - shader_location: 1, + vertex_state: wgpu::VertexStateDescriptor { + index_format: wgpu::IndexFormat::Uint16, + vertex_buffers: &[ + wgpu::VertexBufferDescriptor { + stride: mem::size_of::<Vertex>() as u64, + step_mode: wgpu::InputStepMode::Vertex, + attributes: &[wgpu::VertexAttributeDescriptor { + shader_location: 0, format: wgpu::VertexFormat::Float2, offset: 0, - }, - wgpu::VertexAttributeDescriptor { - shader_location: 2, - format: wgpu::VertexFormat::Float2, - offset: 4 * 2, - }, - wgpu::VertexAttributeDescriptor { - shader_location: 3, - format: wgpu::VertexFormat::Float2, - offset: 4 * 4, - }, - wgpu::VertexAttributeDescriptor { - shader_location: 4, - format: wgpu::VertexFormat::Float2, - offset: 4 * 6, - }, - wgpu::VertexAttributeDescriptor { - shader_location: 5, - format: wgpu::VertexFormat::Uint, - offset: 4 * 8, - }, - ], - }, - ], + }], + }, + wgpu::VertexBufferDescriptor { + stride: mem::size_of::<Instance>() as u64, + step_mode: wgpu::InputStepMode::Instance, + attributes: &[ + wgpu::VertexAttributeDescriptor { + shader_location: 1, + format: wgpu::VertexFormat::Float2, + offset: 0, + }, + wgpu::VertexAttributeDescriptor { + shader_location: 2, + format: wgpu::VertexFormat::Float2, + offset: 4 * 2, + }, + wgpu::VertexAttributeDescriptor { + shader_location: 3, + format: wgpu::VertexFormat::Float2, + offset: 4 * 4, + }, + wgpu::VertexAttributeDescriptor { + shader_location: 4, + format: wgpu::VertexFormat::Float2, + offset: 4 * 6, + }, + wgpu::VertexAttributeDescriptor { + shader_location: 5, + format: wgpu::VertexFormat::Uint, + offset: 4 * 8, + }, + ], + }, + ], + }, sample_count: 1, sample_mask: !0, alpha_to_coverage_enabled: false, }); - let vertices = device - .create_buffer_mapped(QUAD_VERTS.len(), wgpu::BufferUsage::VERTEX) - .fill_from_slice(&QUAD_VERTS); + let vertices = device.create_buffer_with_data( + QUAD_VERTS.as_bytes(), + wgpu::BufferUsage::VERTEX, + ); - let indices = device - .create_buffer_mapped(QUAD_INDICES.len(), wgpu::BufferUsage::INDEX) - .fill_from_slice(&QUAD_INDICES); + let indices = device.create_buffer_with_data( + QUAD_INDICES.as_bytes(), + wgpu::BufferUsage::INDEX, + ); let instances = device.create_buffer(&wgpu::BufferDescriptor { + label: None, size: mem::size_of::<Instance>() as u64 * Instance::MAX as u64, usage: wgpu::BufferUsage::VERTEX | wgpu::BufferUsage::COPY_DST, }); @@ -224,6 +232,7 @@ impl Pipeline { let texture_atlas = Atlas::new(device); let texture = device.create_bind_group(&wgpu::BindGroupDescriptor { + label: None, layout: &texture_layout, bindings: &[wgpu::Binding { binding: 0, @@ -327,6 +336,7 @@ impl Pipeline { self.texture = device.create_bind_group(&wgpu::BindGroupDescriptor { + label: None, layout: &self.texture_layout, bindings: &[wgpu::Binding { binding: 0, @@ -339,11 +349,13 @@ impl Pipeline { self.texture_version = texture_version; } - let uniforms_buffer = device - .create_buffer_mapped(1, wgpu::BufferUsage::COPY_SRC) - .fill_from_slice(&[Uniforms { + let uniforms_buffer = device.create_buffer_with_data( + Uniforms { transform: transformation.into(), - }]); + } + .as_bytes(), + wgpu::BufferUsage::COPY_SRC, + ); encoder.copy_buffer_to_buffer( &uniforms_buffer, @@ -353,9 +365,10 @@ impl Pipeline { std::mem::size_of::<Uniforms>() as u64, ); - let instances_buffer = device - .create_buffer_mapped(instances.len(), wgpu::BufferUsage::COPY_SRC) - .fill_from_slice(&instances); + let instances_buffer = device.create_buffer_with_data( + instances.as_bytes(), + wgpu::BufferUsage::COPY_SRC, + ); let mut i = 0; let total = instances.len(); @@ -394,11 +407,9 @@ impl Pipeline { render_pass.set_pipeline(&self.pipeline); render_pass.set_bind_group(0, &self.constants, &[]); render_pass.set_bind_group(1, &self.texture, &[]); - render_pass.set_index_buffer(&self.indices, 0); - render_pass.set_vertex_buffers( - 0, - &[(&self.vertices, 0), (&self.instances, 0)], - ); + render_pass.set_index_buffer(&self.indices, 0, 0); + render_pass.set_vertex_buffer(0, &self.vertices, 0, 0); + render_pass.set_vertex_buffer(1, &self.instances, 0, 0); render_pass.set_scissor_rect( bounds.x, @@ -441,7 +452,7 @@ pub enum Handle { } #[repr(C)] -#[derive(Clone, Copy)] +#[derive(Clone, Copy, AsBytes)] pub struct Vertex { _position: [f32; 2], } @@ -464,7 +475,7 @@ const QUAD_VERTS: [Vertex; 4] = [ ]; #[repr(C)] -#[derive(Debug, Clone, Copy)] +#[derive(Debug, Clone, Copy, AsBytes)] struct Instance { _position: [f32; 2], _size: [f32; 2], @@ -478,7 +489,7 @@ impl Instance { } #[repr(C)] -#[derive(Debug, Clone, Copy)] +#[derive(Debug, Clone, Copy, AsBytes)] struct Uniforms { transform: [f32; 16], } diff --git a/wgpu/src/image/atlas.rs b/wgpu/src/image/atlas.rs index 86a5ff49..3ce3ce8b 100644 --- a/wgpu/src/image/atlas.rs +++ b/wgpu/src/image/atlas.rs @@ -28,6 +28,7 @@ impl Atlas { }; let texture = device.create_texture(&wgpu::TextureDescriptor { + label: None, size: extent, array_layer_count: 2, mip_level_count: 1, @@ -56,17 +57,14 @@ impl Atlas { self.layers.len() } - pub fn upload<C>( + pub fn upload( &mut self, width: u32, height: u32, - data: &[C], + data: &[u8], device: &wgpu::Device, encoder: &mut wgpu::CommandEncoder, - ) -> Option<Entry> - where - C: Copy + 'static, - { + ) -> Option<Entry> { let entry = { let current_size = self.layers.len(); let entry = self.allocate(width, height)?; @@ -80,9 +78,8 @@ impl Atlas { log::info!("Allocated atlas entry: {:?}", entry); - let buffer = device - .create_buffer_mapped(data.len(), wgpu::BufferUsage::COPY_SRC) - .fill_from_slice(data); + let buffer = + device.create_buffer_with_data(data, wgpu::BufferUsage::COPY_SRC); match &entry { Entry::Contiguous(allocation) => { @@ -274,18 +271,14 @@ impl Atlas { wgpu::BufferCopyView { buffer, offset: offset as u64, - row_pitch: 4 * image_width, - image_height, + bytes_per_row: 4 * image_width, + rows_per_image: image_height, }, wgpu::TextureCopyView { texture: &self.texture, array_layer: layer as u32, mip_level: 0, - origin: wgpu::Origin3d { - x: x as f32, - y: y as f32, - z: 0.0, - }, + origin: wgpu::Origin3d { x, y, z: 0 }, }, extent, ); @@ -302,6 +295,7 @@ impl Atlas { } let new_texture = device.create_texture(&wgpu::TextureDescriptor { + label: None, size: wgpu::Extent3d { width: SIZE, height: SIZE, @@ -331,21 +325,13 @@ impl Atlas { texture: &self.texture, array_layer: i as u32, mip_level: 0, - origin: wgpu::Origin3d { - x: 0.0, - y: 0.0, - z: 0.0, - }, + origin: wgpu::Origin3d { x: 0, y: 0, z: 0 }, }, wgpu::TextureCopyView { texture: &new_texture, array_layer: i as u32, mip_level: 0, - origin: wgpu::Origin3d { - x: 0.0, - y: 0.0, - z: 0.0, - }, + origin: wgpu::Origin3d { x: 0, y: 0, z: 0 }, }, wgpu::Extent3d { width: SIZE, diff --git a/wgpu/src/image/vector.rs b/wgpu/src/image/vector.rs index b6776827..7648aa7e 100644 --- a/wgpu/src/image/vector.rs +++ b/wgpu/src/image/vector.rs @@ -2,6 +2,8 @@ use crate::image::atlas::{self, Atlas}; use iced_native::svg; use std::collections::{HashMap, HashSet}; +use zerocopy::AsBytes; + pub enum Svg { Loaded(resvg::usvg::Tree), NotFound, @@ -117,7 +119,7 @@ impl Cache { let allocation = texture_atlas.upload( width, height, - canvas.get_data(), + canvas.get_data().as_bytes(), device, encoder, )?; |