diff options
| author | 2024-05-23 13:29:45 +0200 | |
|---|---|---|
| committer | 2024-05-23 13:29:45 +0200 | |
| commit | d8ba6b0673a33724a177f3a1ba59705527280142 (patch) | |
| tree | 89482c8d1e3a03e00b3a8151abbb81e30ae5898c /wgpu/src/quad | |
| parent | 72ed8bcc8def9956e25f3720a3095fc96bb2eef0 (diff) | |
| parent | 468794d918eb06c1dbebb33c32b10017ad335f05 (diff) | |
| download | iced-d8ba6b0673a33724a177f3a1ba59705527280142.tar.gz iced-d8ba6b0673a33724a177f3a1ba59705527280142.tar.bz2 iced-d8ba6b0673a33724a177f3a1ba59705527280142.zip | |
Merge branch 'master' into feat/text-macro
Diffstat (limited to '')
| -rw-r--r-- | wgpu/src/quad.rs | 118 | ||||
| -rw-r--r-- | wgpu/src/quad/gradient.rs | 5 | ||||
| -rw-r--r-- | wgpu/src/quad/solid.rs | 5 | 
3 files changed, 78 insertions, 50 deletions
| diff --git a/wgpu/src/quad.rs b/wgpu/src/quad.rs index b932f54f..de432d2f 100644 --- a/wgpu/src/quad.rs +++ b/wgpu/src/quad.rs @@ -12,11 +12,37 @@ use bytemuck::{Pod, Zeroable};  use std::mem; -#[cfg(feature = "tracing")] -use tracing::info_span; -  const INITIAL_INSTANCES: usize = 2_000; +/// The properties of a quad. +#[derive(Clone, Copy, Debug, Pod, Zeroable)] +#[repr(C)] +pub struct Quad { +    /// The position of the [`Quad`]. +    pub position: [f32; 2], + +    /// The size of the [`Quad`]. +    pub size: [f32; 2], + +    /// The border color of the [`Quad`], in __linear RGB__. +    pub border_color: color::Packed, + +    /// The border radii of the [`Quad`]. +    pub border_radius: [f32; 4], + +    /// The border width of the [`Quad`]. +    pub border_width: f32, + +    /// The shadow color of the [`Quad`]. +    pub shadow_color: color::Packed, + +    /// The shadow offset of the [`Quad`]. +    pub shadow_offset: [f32; 2], + +    /// The shadow blur radius of the [`Quad`]. +    pub shadow_blur_radius: f32, +} +  #[derive(Debug)]  pub struct Pipeline {      solid: solid::Pipeline, @@ -57,7 +83,8 @@ impl Pipeline {      pub fn prepare(          &mut self,          device: &wgpu::Device, -        queue: &wgpu::Queue, +        encoder: &mut wgpu::CommandEncoder, +        belt: &mut wgpu::util::StagingBelt,          quads: &Batch,          transformation: Transformation,          scale: f32, @@ -67,7 +94,7 @@ impl Pipeline {          }          let layer = &mut self.layers[self.prepare_layer]; -        layer.prepare(device, queue, quads, transformation, scale); +        layer.prepare(device, encoder, belt, quads, transformation, scale);          self.prepare_layer += 1;      } @@ -123,7 +150,7 @@ impl Pipeline {  }  #[derive(Debug)] -struct Layer { +pub struct Layer {      constants: wgpu::BindGroup,      constants_buffer: wgpu::Buffer,      solid: solid::Layer, @@ -162,56 +189,46 @@ impl Layer {      pub fn prepare(          &mut self,          device: &wgpu::Device, -        queue: &wgpu::Queue, +        encoder: &mut wgpu::CommandEncoder, +        belt: &mut wgpu::util::StagingBelt,          quads: &Batch,          transformation: Transformation,          scale: f32,      ) { -        #[cfg(feature = "tracing")] -        let _ = info_span!("Wgpu::Quad", "PREPARE").entered(); +        self.update(device, encoder, belt, transformation, scale); +        if !quads.solids.is_empty() { +            self.solid.prepare(device, encoder, belt, &quads.solids); +        } + +        if !quads.gradients.is_empty() { +            self.gradient +                .prepare(device, encoder, belt, &quads.gradients); +        } +    } + +    pub fn update( +        &mut self, +        device: &wgpu::Device, +        encoder: &mut wgpu::CommandEncoder, +        belt: &mut wgpu::util::StagingBelt, +        transformation: Transformation, +        scale: f32, +    ) {          let uniforms = Uniforms::new(transformation, scale); +        let bytes = bytemuck::bytes_of(&uniforms); -        queue.write_buffer( +        belt.write_buffer( +            encoder,              &self.constants_buffer,              0, -            bytemuck::bytes_of(&uniforms), -        ); - -        self.solid.prepare(device, queue, &quads.solids); -        self.gradient.prepare(device, queue, &quads.gradients); +            (bytes.len() as u64).try_into().expect("Sized uniforms"), +            device, +        ) +        .copy_from_slice(bytes);      }  } -/// The properties of a quad. -#[derive(Clone, Copy, Debug, Pod, Zeroable)] -#[repr(C)] -pub struct Quad { -    /// The position of the [`Quad`]. -    pub position: [f32; 2], - -    /// The size of the [`Quad`]. -    pub size: [f32; 2], - -    /// The border color of the [`Quad`], in __linear RGB__. -    pub border_color: color::Packed, - -    /// The border radii of the [`Quad`]. -    pub border_radius: [f32; 4], - -    /// The border width of the [`Quad`]. -    pub border_width: f32, - -    /// The shadow color of the [`Quad`]. -    pub shadow_color: [f32; 4], - -    /// The shadow offset of the [`Quad`]. -    pub shadow_offset: [f32; 2], - -    /// The shadow blur radius of the [`Quad`]. -    pub shadow_blur_radius: f32, -} -  /// A group of [`Quad`]s rendered together.  #[derive(Default, Debug)]  pub struct Batch { @@ -221,10 +238,13 @@ pub struct Batch {      /// The gradient quads of the [`Layer`].      gradients: Vec<Gradient>, -    /// The quad order of the [`Layer`]; stored as a tuple of the quad type & its count. -    order: Vec<(Kind, usize)>, +    /// The quad order of the [`Layer`]. +    order: Order,  } +/// The quad order of a [`Layer`]; stored as a tuple of the quad type & its count. +type Order = Vec<(Kind, usize)>; +  impl Batch {      /// Returns true if there are no quads of any type in [`Quads`].      pub fn is_empty(&self) -> bool { @@ -264,6 +284,12 @@ impl Batch {              }          }      } + +    pub fn clear(&mut self) { +        self.solids.clear(); +        self.gradients.clear(); +        self.order.clear(); +    }  }  #[derive(Debug, Copy, Clone, PartialEq, Eq)] diff --git a/wgpu/src/quad/gradient.rs b/wgpu/src/quad/gradient.rs index 560fcad2..5b32c52a 100644 --- a/wgpu/src/quad/gradient.rs +++ b/wgpu/src/quad/gradient.rs @@ -46,11 +46,12 @@ impl Layer {      pub fn prepare(          &mut self,          device: &wgpu::Device, -        queue: &wgpu::Queue, +        encoder: &mut wgpu::CommandEncoder, +        belt: &mut wgpu::util::StagingBelt,          instances: &[Gradient],      ) {          let _ = self.instances.resize(device, instances.len()); -        let _ = self.instances.write(queue, 0, instances); +        let _ = self.instances.write(device, encoder, belt, 0, instances);          self.instance_count = instances.len();      } diff --git a/wgpu/src/quad/solid.rs b/wgpu/src/quad/solid.rs index 771eee34..1cead367 100644 --- a/wgpu/src/quad/solid.rs +++ b/wgpu/src/quad/solid.rs @@ -40,11 +40,12 @@ impl Layer {      pub fn prepare(          &mut self,          device: &wgpu::Device, -        queue: &wgpu::Queue, +        encoder: &mut wgpu::CommandEncoder, +        belt: &mut wgpu::util::StagingBelt,          instances: &[Solid],      ) {          let _ = self.instances.resize(device, instances.len()); -        let _ = self.instances.write(queue, 0, instances); +        let _ = self.instances.write(device, encoder, belt, 0, instances);          self.instance_count = instances.len();      } | 
