From 40f45d7b7e35dd4937abe6b5ce16b6256b4f1eeb Mon Sep 17 00:00:00 2001 From: shan Date: Thu, 29 Sep 2022 10:52:58 -0700 Subject: Adds linear gradient support to 2D meshes in the canvas widget. --- graphics/src/layer.rs | 47 ++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 38 insertions(+), 9 deletions(-) (limited to 'graphics/src/layer.rs') diff --git a/graphics/src/layer.rs b/graphics/src/layer.rs index af545713..b7731922 100644 --- a/graphics/src/layer.rs +++ b/graphics/src/layer.rs @@ -7,9 +7,10 @@ use crate::{ use iced_native::image; use iced_native::svg; +use crate::shader::Shader; /// A group of primitives that should be clipped together. -#[derive(Debug, Clone)] +#[derive(Debug)] pub struct Layer<'a> { /// The clipping bounds of the [`Layer`]. pub bounds: Rectangle, @@ -18,7 +19,7 @@ pub struct Layer<'a> { pub quads: Vec, /// The triangle meshes of the [`Layer`]. - pub meshes: Vec>, + pub meshes: Meshes<'a>, /// The text of the [`Layer`]. pub text: Vec>, @@ -33,7 +34,7 @@ impl<'a> Layer<'a> { Self { bounds, quads: Vec::new(), - meshes: Vec::new(), + meshes: Meshes(Vec::new()), text: Vec::new(), images: Vec::new(), } @@ -159,7 +160,11 @@ impl<'a> Layer<'a> { border_color: border_color.into_linear(), }); } - Primitive::Mesh2D { buffers, size } => { + Primitive::Mesh2D { + buffers, + size, + shader, + } => { let layer = &mut layers[current_layer]; let bounds = Rectangle::new( @@ -169,11 +174,14 @@ impl<'a> Layer<'a> { // Only draw visible content if let Some(clip_bounds) = layer.bounds.intersection(&bounds) { - layer.meshes.push(Mesh { - origin: Point::new(translation.x, translation.y), - buffers, - clip_bounds, - }); + layer.meshes.0.push( + Mesh { + origin: Point::new(translation.x, translation.y), + buffers, + clip_bounds, + shader, + } + ); } } Primitive::Clip { bounds, content } => { @@ -270,6 +278,9 @@ pub struct Mesh<'a> { /// The clipping bounds of the [`Mesh`]. pub clip_bounds: Rectangle, + + /// The shader of the [`Mesh`]. + pub shader: &'a Shader, } /// A paragraph of text. @@ -323,3 +334,21 @@ unsafe impl bytemuck::Zeroable for Quad {} #[allow(unsafe_code)] unsafe impl bytemuck::Pod for Quad {} + +#[derive(Debug)] +/// A collection of meshes. +pub struct Meshes<'a>(pub Vec>); + +impl<'a> Meshes<'a> { + /// Returns the number of total vertices & total indices of all [`Mesh`]es. + pub fn attribute_count(&self) -> (usize, usize) { + self.0 + .iter() + .map(|Mesh { buffers, .. }| { + (buffers.vertices.len(), buffers.indices.len()) + }) + .fold((0, 0), |(total_v, total_i), (v, i)| { + (total_v + v, total_i + i) + }) + } +} \ No newline at end of file -- cgit From 6e7b3ced0b1daf368e44e181ecdb4ae529877eb6 Mon Sep 17 00:00:00 2001 From: shan Date: Tue, 4 Oct 2022 18:24:46 -0700 Subject: Reworked wgpu buffers, updated glow side to have proper transform location storage, attempting to fix visibility modifiers, implemented some of the feedback received in initial PR. --- graphics/src/layer.rs | 32 +++++++++++++------------------- 1 file changed, 13 insertions(+), 19 deletions(-) (limited to 'graphics/src/layer.rs') diff --git a/graphics/src/layer.rs b/graphics/src/layer.rs index b7731922..096c50dc 100644 --- a/graphics/src/layer.rs +++ b/graphics/src/layer.rs @@ -19,7 +19,7 @@ pub struct Layer<'a> { pub quads: Vec, /// The triangle meshes of the [`Layer`]. - pub meshes: Meshes<'a>, + pub meshes: Vec>, /// The text of the [`Layer`]. pub text: Vec>, @@ -34,7 +34,7 @@ impl<'a> Layer<'a> { Self { bounds, quads: Vec::new(), - meshes: Meshes(Vec::new()), + meshes: Vec::new(), text: Vec::new(), images: Vec::new(), } @@ -174,7 +174,7 @@ impl<'a> Layer<'a> { // Only draw visible content if let Some(clip_bounds) = layer.bounds.intersection(&bounds) { - layer.meshes.0.push( + layer.meshes.push( Mesh { origin: Point::new(translation.x, translation.y), buffers, @@ -335,20 +335,14 @@ unsafe impl bytemuck::Zeroable for Quad {} #[allow(unsafe_code)] unsafe impl bytemuck::Pod for Quad {} -#[derive(Debug)] -/// A collection of meshes. -pub struct Meshes<'a>(pub Vec>); - -impl<'a> Meshes<'a> { - /// Returns the number of total vertices & total indices of all [`Mesh`]es. - pub fn attribute_count(&self) -> (usize, usize) { - self.0 - .iter() - .map(|Mesh { buffers, .. }| { - (buffers.vertices.len(), buffers.indices.len()) - }) - .fold((0, 0), |(total_v, total_i), (v, i)| { - (total_v + v, total_i + i) - }) - } +/// Returns the number of total vertices & total indices of all [`Mesh`]es. +pub fn attribute_count_of<'a>(meshes: &'a [Mesh<'a>]) -> (usize, usize) { + meshes + .iter() + .map(|Mesh { buffers, .. }| { + (buffers.vertices.len(), buffers.indices.len()) + }) + .fold((0, 0), |(total_v, total_i), (v, i)| { + (total_v + v, total_i + i) + }) } \ No newline at end of file -- cgit From 30432cbade3d9b25c4df62656a7494db3f4ea82a Mon Sep 17 00:00:00 2001 From: shan Date: Wed, 5 Oct 2022 10:49:58 -0700 Subject: Readjusted namespaces, removed Geometry example as it's no longer relevant. --- graphics/src/layer.rs | 111 +++++--------------------------------------------- 1 file changed, 11 insertions(+), 100 deletions(-) (limited to 'graphics/src/layer.rs') diff --git a/graphics/src/layer.rs b/graphics/src/layer.rs index 096c50dc..65e70cb3 100644 --- a/graphics/src/layer.rs +++ b/graphics/src/layer.rs @@ -1,13 +1,17 @@ //! Organize rendering primitives into a flattened list of layers. +pub mod mesh; +mod quad; +mod text; +mod image; + use crate::alignment; -use crate::triangle; use crate::{ Background, Font, Point, Primitive, Rectangle, Size, Vector, Viewport, }; - -use iced_native::image; -use iced_native::svg; -use crate::shader::Shader; +pub use crate::layer::image::Image; +pub use crate::layer::mesh::Mesh; +pub use crate::layer::quad::Quad; +pub use crate::layer::text::Text; /// A group of primitives that should be clipped together. #[derive(Debug)] @@ -163,7 +167,7 @@ impl<'a> Layer<'a> { Primitive::Mesh2D { buffers, size, - shader, + style, } => { let layer = &mut layers[current_layer]; @@ -179,7 +183,7 @@ impl<'a> Layer<'a> { origin: Point::new(translation.x, translation.y), buffers, clip_bounds, - shader, + style, } ); } @@ -242,99 +246,6 @@ impl<'a> Layer<'a> { } } -/// A colored rectangle with a border. -/// -/// This type can be directly uploaded to GPU memory. -#[derive(Debug, Clone, Copy)] -#[repr(C)] -pub struct Quad { - /// The position of the [`Quad`]. - pub position: [f32; 2], - - /// The size of the [`Quad`]. - pub size: [f32; 2], - - /// The color of the [`Quad`], in __linear RGB__. - pub color: [f32; 4], - - /// The border color of the [`Quad`], in __linear RGB__. - pub border_color: [f32; 4], - - /// The border radius of the [`Quad`]. - pub border_radius: f32, - - /// The border width of the [`Quad`]. - pub border_width: f32, -} - -/// A mesh of triangles. -#[derive(Debug, Clone, Copy)] -pub struct Mesh<'a> { - /// The origin of the vertices of the [`Mesh`]. - pub origin: Point, - - /// The vertex and index buffers of the [`Mesh`]. - pub buffers: &'a triangle::Mesh2D, - - /// The clipping bounds of the [`Mesh`]. - pub clip_bounds: Rectangle, - - /// The shader of the [`Mesh`]. - pub shader: &'a Shader, -} - -/// A paragraph of text. -#[derive(Debug, Clone, Copy)] -pub struct Text<'a> { - /// The content of the [`Text`]. - pub content: &'a str, - - /// The layout bounds of the [`Text`]. - pub bounds: Rectangle, - - /// The color of the [`Text`], in __linear RGB_. - pub color: [f32; 4], - - /// The size of the [`Text`]. - pub size: f32, - - /// The font of the [`Text`]. - pub font: Font, - - /// The horizontal alignment of the [`Text`]. - pub horizontal_alignment: alignment::Horizontal, - - /// The vertical alignment of the [`Text`]. - pub vertical_alignment: alignment::Vertical, -} - -/// A raster or vector image. -#[derive(Debug, Clone)] -pub enum Image { - /// A raster image. - Raster { - /// The handle of a raster image. - handle: image::Handle, - - /// The bounds of the image. - bounds: Rectangle, - }, - /// A vector image. - Vector { - /// The handle of a vector image. - handle: svg::Handle, - - /// The bounds of the image. - bounds: Rectangle, - }, -} - -#[allow(unsafe_code)] -unsafe impl bytemuck::Zeroable for Quad {} - -#[allow(unsafe_code)] -unsafe impl bytemuck::Pod for Quad {} - /// Returns the number of total vertices & total indices of all [`Mesh`]es. pub fn attribute_count_of<'a>(meshes: &'a [Mesh<'a>]) -> (usize, usize) { meshes -- cgit From cb7c4676543cd508dfae8d4dcbd9cc8b61b1a94e Mon Sep 17 00:00:00 2001 From: shan Date: Thu, 6 Oct 2022 07:28:05 -0700 Subject: Fixed lint issues & cleaned up some documentation. --- graphics/src/layer.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'graphics/src/layer.rs') diff --git a/graphics/src/layer.rs b/graphics/src/layer.rs index 65e70cb3..08f07c0e 100644 --- a/graphics/src/layer.rs +++ b/graphics/src/layer.rs @@ -256,4 +256,4 @@ pub fn attribute_count_of<'a>(meshes: &'a [Mesh<'a>]) -> (usize, usize) { .fold((0, 0), |(total_v, total_i), (v, i)| { (total_v + v, total_i + i) }) -} \ No newline at end of file +} -- cgit From 215e6c95be7370bdd29c75b904463f06f942b7c0 Mon Sep 17 00:00:00 2001 From: shan Date: Fri, 7 Oct 2022 13:21:32 -0700 Subject: More import adjusting. --- graphics/src/layer.rs | 14 +------------- 1 file changed, 1 insertion(+), 13 deletions(-) (limited to 'graphics/src/layer.rs') diff --git a/graphics/src/layer.rs b/graphics/src/layer.rs index 08f07c0e..56a42a9d 100644 --- a/graphics/src/layer.rs +++ b/graphics/src/layer.rs @@ -244,16 +244,4 @@ impl<'a> Layer<'a> { } } } -} - -/// Returns the number of total vertices & total indices of all [`Mesh`]es. -pub fn attribute_count_of<'a>(meshes: &'a [Mesh<'a>]) -> (usize, usize) { - meshes - .iter() - .map(|Mesh { buffers, .. }| { - (buffers.vertices.len(), buffers.indices.len()) - }) - .fold((0, 0), |(total_v, total_i), (v, i)| { - (total_v + v, total_i + i) - }) -} +} \ No newline at end of file -- cgit From b95745340441835bd25b5cadc2342254631f8c05 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 3 Nov 2022 04:35:16 +0100 Subject: Run `cargo fmt` --- graphics/src/layer.rs | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) (limited to 'graphics/src/layer.rs') diff --git a/graphics/src/layer.rs b/graphics/src/layer.rs index 56a42a9d..40e006a2 100644 --- a/graphics/src/layer.rs +++ b/graphics/src/layer.rs @@ -1,17 +1,17 @@ //! Organize rendering primitives into a flattened list of layers. +mod image; pub mod mesh; mod quad; mod text; -mod image; use crate::alignment; -use crate::{ - Background, Font, Point, Primitive, Rectangle, Size, Vector, Viewport, -}; pub use crate::layer::image::Image; pub use crate::layer::mesh::Mesh; pub use crate::layer::quad::Quad; pub use crate::layer::text::Text; +use crate::{ + Background, Font, Point, Primitive, Rectangle, Size, Vector, Viewport, +}; /// A group of primitives that should be clipped together. #[derive(Debug)] @@ -178,14 +178,12 @@ impl<'a> Layer<'a> { // Only draw visible content if let Some(clip_bounds) = layer.bounds.intersection(&bounds) { - layer.meshes.push( - Mesh { - origin: Point::new(translation.x, translation.y), - buffers, - clip_bounds, - style, - } - ); + layer.meshes.push(Mesh { + origin: Point::new(translation.x, translation.y), + buffers, + clip_bounds, + style, + }); } } Primitive::Clip { bounds, content } => { @@ -244,4 +242,4 @@ impl<'a> Layer<'a> { } } } -} \ No newline at end of file +} -- cgit From 610dae487725a1ea392c726a8b88601da4d08166 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 3 Nov 2022 05:22:58 +0100 Subject: Refactor exports in `graphics::layer` --- graphics/src/layer.rs | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) (limited to 'graphics/src/layer.rs') diff --git a/graphics/src/layer.rs b/graphics/src/layer.rs index 40e006a2..e95934b0 100644 --- a/graphics/src/layer.rs +++ b/graphics/src/layer.rs @@ -1,14 +1,16 @@ //! Organize rendering primitives into a flattened list of layers. mod image; -pub mod mesh; mod quad; mod text; +pub mod mesh; + +pub use image::Image; +pub use mesh::Mesh; +pub use quad::Quad; +pub use text::Text; + use crate::alignment; -pub use crate::layer::image::Image; -pub use crate::layer::mesh::Mesh; -pub use crate::layer::quad::Quad; -pub use crate::layer::text::Text; use crate::{ Background, Font, Point, Primitive, Rectangle, Size, Vector, Viewport, }; -- cgit