From 126133ead775fda064a6c23503e9a552a10dc2c5 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sat, 22 Feb 2020 18:03:49 +0100 Subject: Fix `Clip` primitive intersection in `iced_wgpu` --- wgpu/src/renderer.rs | 29 +++++++++++------------------ 1 file changed, 11 insertions(+), 18 deletions(-) (limited to 'wgpu/src/renderer.rs') diff --git a/wgpu/src/renderer.rs b/wgpu/src/renderer.rs index 29adcfb6..af61804e 100644 --- a/wgpu/src/renderer.rs +++ b/wgpu/src/renderer.rs @@ -240,25 +240,18 @@ impl Renderer { offset, content, } => { - let x = bounds.x - layer.offset.x as f32; - let y = bounds.y - layer.offset.y as f32; - let width = (bounds.width + x).min(bounds.width); - let height = (bounds.height + y).min(bounds.height); - - // Only draw visible content on-screen - // TODO: Also, check for parent layer bounds to avoid further - // drawing in some circumstances. - if width > 0.0 && height > 0.0 { - let clip_layer = Layer::new( - Rectangle { - x: x.max(0.0).floor() as u32, - y: y.max(0.0).floor() as u32, - width: width.ceil() as u32, - height: height.ceil() as u32, - }, - layer.offset + *offset, - ); + let layer_bounds: Rectangle = layer.bounds.into(); + let clip = Rectangle { + x: bounds.x - layer.offset.x as f32, + y: bounds.y - layer.offset.y as f32, + ..*bounds + }; + + // Only draw visible content + if let Some(clip_bounds) = layer_bounds.intersection(&clip) { + let clip_layer = + Layer::new(clip_bounds.into(), layer.offset + *offset); let new_layer = Layer::new(layer.bounds, layer.offset); layers.push(clip_layer); -- cgit From be14aca07506385a209e89cd99256744a7ec3c0f Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Mon, 24 Feb 2020 20:08:40 +0100 Subject: Make output format of `iced_wgpu` configurable --- wgpu/src/renderer.rs | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) (limited to 'wgpu/src/renderer.rs') diff --git a/wgpu/src/renderer.rs b/wgpu/src/renderer.rs index af61804e..f67dd1eb 100644 --- a/wgpu/src/renderer.rs +++ b/wgpu/src/renderer.rs @@ -48,11 +48,16 @@ impl Renderer { /// /// [`Renderer`]: struct.Renderer.html pub fn new(device: &mut wgpu::Device, settings: Settings) -> Self { - let text_pipeline = text::Pipeline::new(device, settings.default_font); - let quad_pipeline = quad::Pipeline::new(device); - let image_pipeline = crate::image::Pipeline::new(device); - let triangle_pipeline = - triangle::Pipeline::new(device, settings.antialiasing); + let text_pipeline = + text::Pipeline::new(device, settings.format, settings.default_font); + let quad_pipeline = quad::Pipeline::new(device, settings.format); + let image_pipeline = + crate::image::Pipeline::new(device, settings.format); + let triangle_pipeline = triangle::Pipeline::new( + device, + settings.format, + settings.antialiasing, + ); Self { quad_pipeline, -- cgit From 59d45a5440aaa46c7dc8f3dc70c8518167c10418 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 26 Feb 2020 12:34:34 +0100 Subject: Refactor texture atlas - Split into multiple modules - Rename some concepts - Change API details --- wgpu/src/renderer.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'wgpu/src/renderer.rs') diff --git a/wgpu/src/renderer.rs b/wgpu/src/renderer.rs index f67dd1eb..b5dce480 100644 --- a/wgpu/src/renderer.rs +++ b/wgpu/src/renderer.rs @@ -227,14 +227,14 @@ impl Renderer { layer.images.push(Image { handle: image::Handle::Raster(handle.clone()), position: [bounds.x, bounds.y], - scale: [bounds.width, bounds.height], + size: [bounds.width, bounds.height], }); } Primitive::Svg { handle, bounds } => { layer.images.push(Image { handle: image::Handle::Vector(handle.clone()), position: [bounds.x, bounds.y], - scale: [bounds.width, bounds.height], + size: [bounds.width, bounds.height], }); } Primitive::Mesh2D { origin, buffers } => { -- cgit From 4e7159c22c6be90f61aa715d5eb6811f805cb597 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 28 Feb 2020 14:38:42 +0100 Subject: Stop creating image pipeline when unnecessary --- wgpu/src/renderer.rs | 103 ++++++++++++++++++++++++++++++++------------------- 1 file changed, 64 insertions(+), 39 deletions(-) (limited to 'wgpu/src/renderer.rs') diff --git a/wgpu/src/renderer.rs b/wgpu/src/renderer.rs index b5dce480..d9ef9fc4 100644 --- a/wgpu/src/renderer.rs +++ b/wgpu/src/renderer.rs @@ -1,7 +1,11 @@ use crate::{ - image, quad, text, triangle, Defaults, Image, Primitive, Quad, Settings, - Target, Transformation, + quad, text, triangle, Defaults, Primitive, Quad, Settings, Target, + Transformation, }; + +#[cfg(any(feature = "image", feature = "svg"))] +use crate::{image, Image}; + use iced_native::{ layout, Background, Color, Layout, MouseCursor, Point, Rectangle, Vector, Widget, @@ -16,18 +20,22 @@ mod widget; #[derive(Debug)] pub struct Renderer { quad_pipeline: quad::Pipeline, - image_pipeline: image::Pipeline, text_pipeline: text::Pipeline, - triangle_pipeline: crate::triangle::Pipeline, + triangle_pipeline: triangle::Pipeline, + + #[cfg(any(feature = "image", feature = "svg"))] + image_pipeline: image::Pipeline, } struct Layer<'a> { bounds: Rectangle, offset: Vector, quads: Vec, - images: Vec, meshes: Vec<(Point, Arc)>, text: Vec>, + + #[cfg(any(feature = "image", feature = "svg"))] + images: Vec, } impl<'a> Layer<'a> { @@ -36,9 +44,11 @@ impl<'a> Layer<'a> { bounds, offset, quads: Vec::new(), - images: Vec::new(), text: Vec::new(), meshes: Vec::new(), + + #[cfg(any(feature = "image", feature = "svg"))] + images: Vec::new(), } } } @@ -51,19 +61,22 @@ impl Renderer { let text_pipeline = text::Pipeline::new(device, settings.format, settings.default_font); let quad_pipeline = quad::Pipeline::new(device, settings.format); - let image_pipeline = - crate::image::Pipeline::new(device, settings.format); let triangle_pipeline = triangle::Pipeline::new( device, settings.format, settings.antialiasing, ); + #[cfg(any(feature = "image", feature = "svg"))] + let image_pipeline = image::Pipeline::new(device, settings.format); + Self { quad_pipeline, - image_pipeline, text_pipeline, triangle_pipeline, + + #[cfg(any(feature = "image", feature = "svg"))] + image_pipeline, } } @@ -116,6 +129,7 @@ impl Renderer { ); } + #[cfg(any(feature = "image", feature = "svg"))] self.image_pipeline.trim_cache(); *mouse_cursor @@ -223,20 +237,6 @@ impl Renderer { border_color: border_color.into_linear(), }); } - Primitive::Image { handle, bounds } => { - layer.images.push(Image { - handle: image::Handle::Raster(handle.clone()), - position: [bounds.x, bounds.y], - size: [bounds.width, bounds.height], - }); - } - Primitive::Svg { handle, bounds } => { - layer.images.push(Image { - handle: image::Handle::Vector(handle.clone()), - position: [bounds.x, bounds.y], - size: [bounds.width, bounds.height], - }); - } Primitive::Mesh2D { origin, buffers } => { layer.meshes.push((*origin, buffers.clone())); } @@ -264,6 +264,28 @@ impl Renderer { layers.push(new_layer); } } + + #[cfg(feature = "image")] + Primitive::Image { handle, bounds } => { + layer.images.push(Image { + handle: image::Handle::Raster(handle.clone()), + position: [bounds.x, bounds.y], + size: [bounds.width, bounds.height], + }); + } + #[cfg(not(feature = "image"))] + Primitive::Image { .. } => {} + + #[cfg(feature = "svg")] + Primitive::Svg { handle, bounds } => { + layer.images.push(Image { + handle: image::Handle::Vector(handle.clone()), + position: [bounds.x, bounds.y], + size: [bounds.width, bounds.height], + }); + } + #[cfg(not(feature = "svg"))] + Primitive::Svg { .. } => {} } } @@ -346,23 +368,26 @@ impl Renderer { ); } - if layer.images.len() > 0 { - let translated_and_scaled = transformation - * Transformation::scale(scale_factor, scale_factor) - * Transformation::translate( - -(layer.offset.x as f32), - -(layer.offset.y as f32), + #[cfg(any(feature = "image", feature = "svg"))] + { + if layer.images.len() > 0 { + let translated_and_scaled = transformation + * Transformation::scale(scale_factor, scale_factor) + * Transformation::translate( + -(layer.offset.x as f32), + -(layer.offset.y as f32), + ); + + self.image_pipeline.draw( + device, + encoder, + &layer.images, + translated_and_scaled, + bounds, + target, + scale_factor, ); - - self.image_pipeline.draw( - device, - encoder, - &layer.images, - translated_and_scaled, - bounds, - target, - scale_factor, - ); + } } if layer.text.len() > 0 { -- cgit From 88d4cd097044077127e1b3aa8fcb04afed185491 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 28 Feb 2020 14:41:07 +0100 Subject: Remove unnecessary `pub(crate) use` --- wgpu/src/renderer.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'wgpu/src/renderer.rs') diff --git a/wgpu/src/renderer.rs b/wgpu/src/renderer.rs index d9ef9fc4..1da19b1a 100644 --- a/wgpu/src/renderer.rs +++ b/wgpu/src/renderer.rs @@ -4,7 +4,7 @@ use crate::{ }; #[cfg(any(feature = "image", feature = "svg"))] -use crate::{image, Image}; +use crate::image::{self, Image}; use iced_native::{ layout, Background, Color, Layout, MouseCursor, Point, Rectangle, Vector, -- cgit From b74e7e7353d69ffb54cf0c0f0574ea7abf0f3a68 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sat, 7 Mar 2020 23:45:54 +0100 Subject: Implement `Primitive::Cached` --- wgpu/src/renderer.rs | 112 ++++++++++++++++++++++++++++----------------------- 1 file changed, 62 insertions(+), 50 deletions(-) (limited to 'wgpu/src/renderer.rs') diff --git a/wgpu/src/renderer.rs b/wgpu/src/renderer.rs index 1da19b1a..c06af339 100644 --- a/wgpu/src/renderer.rs +++ b/wgpu/src/renderer.rs @@ -10,7 +10,6 @@ use iced_native::{ layout, Background, Color, Layout, MouseCursor, Point, Rectangle, Vector, Widget, }; -use std::sync::Arc; mod widget; @@ -29,9 +28,8 @@ pub struct Renderer { struct Layer<'a> { bounds: Rectangle, - offset: Vector, quads: Vec, - meshes: Vec<(Point, Arc)>, + meshes: Vec<(Point, &'a triangle::Mesh2D)>, text: Vec>, #[cfg(any(feature = "image", feature = "svg"))] @@ -39,10 +37,9 @@ struct Layer<'a> { } impl<'a> Layer<'a> { - pub fn new(bounds: Rectangle, offset: Vector) -> Self { + pub fn new(bounds: Rectangle) -> Self { Self { bounds, - offset, quads: Vec::new(), text: Vec::new(), meshes: Vec::new(), @@ -103,17 +100,14 @@ impl Renderer { let mut layers = Vec::new(); - layers.push(Layer::new( - Rectangle { - x: 0, - y: 0, - width: u32::from(width), - height: u32::from(height), - }, - Vector::new(0, 0), - )); - - self.draw_primitive(primitive, &mut layers); + layers.push(Layer::new(Rectangle { + x: 0, + y: 0, + width: u32::from(width), + height: u32::from(height), + })); + + self.draw_primitive(Vector::new(0.0, 0.0), primitive, &mut layers); self.draw_overlay(overlay, &mut layers); for layer in layers { @@ -137,17 +131,16 @@ impl Renderer { fn draw_primitive<'a>( &mut self, + translation: Vector, primitive: &'a Primitive, layers: &mut Vec>, ) { - let layer = layers.last_mut().unwrap(); - match primitive { Primitive::None => {} Primitive::Group { primitives } => { // TODO: Inspect a bit and regroup (?) for primitive in primitives { - self.draw_primitive(primitive, layers) + self.draw_primitive(translation, primitive, layers) } } Primitive::Text { @@ -179,12 +172,11 @@ impl Renderer { } }; + let layer = layers.last_mut().unwrap(); + layer.text.push(wgpu_glyph::Section { text: &content, - screen_position: ( - x - layer.offset.x as f32, - y - layer.offset.y as f32, - ), + screen_position: (x + translation.x, y + translation.y), bounds: (bounds.width, bounds.height), scale: wgpu_glyph::Scale { x: *size, y: *size }, color: color.into_linear(), @@ -222,11 +214,13 @@ impl Renderer { border_width, border_color, } => { - // TODO: Move some of this computations to the GPU (?) + let layer = layers.last_mut().unwrap(); + + // TODO: Move some of these computations to the GPU (?) layer.quads.push(Quad { position: [ - bounds.x - layer.offset.x as f32, - bounds.y - layer.offset.y as f32, + bounds.x + translation.x, + bounds.y + translation.y, ], scale: [bounds.width, bounds.height], color: match background { @@ -238,38 +232,59 @@ impl Renderer { }); } Primitive::Mesh2D { origin, buffers } => { - layer.meshes.push((*origin, buffers.clone())); + let layer = layers.last_mut().unwrap(); + + layer.meshes.push((*origin + translation, buffers)); } Primitive::Clip { bounds, offset, content, } => { + let layer = layers.last_mut().unwrap(); + let layer_bounds: Rectangle = layer.bounds.into(); let clip = Rectangle { - x: bounds.x - layer.offset.x as f32, - y: bounds.y - layer.offset.y as f32, + x: bounds.x + translation.x, + y: bounds.y + translation.y, ..*bounds }; // Only draw visible content if let Some(clip_bounds) = layer_bounds.intersection(&clip) { - let clip_layer = - Layer::new(clip_bounds.into(), layer.offset + *offset); - let new_layer = Layer::new(layer.bounds, layer.offset); + let clip_layer = Layer::new(clip_bounds.into()); + let new_layer = Layer::new(layer.bounds); layers.push(clip_layer); - self.draw_primitive(content, layers); + self.draw_primitive( + translation + - Vector::new(offset.x as f32, offset.y as f32), + content, + layers, + ); layers.push(new_layer); } } + Primitive::Cached { origin, cache } => { + self.draw_primitive( + translation + Vector::new(origin.x, origin.y), + &cache, + layers, + ); + } + #[cfg(feature = "image")] Primitive::Image { handle, bounds } => { + let layer = layers.last_mut().unwrap(); + layer.images.push(Image { handle: image::Handle::Raster(handle.clone()), - position: [bounds.x, bounds.y], + position: [ + bounds.x + translation.x, + bounds.y + translation.y, + ], size: [bounds.width, bounds.height], }); } @@ -278,9 +293,14 @@ impl Renderer { #[cfg(feature = "svg")] Primitive::Svg { handle, bounds } => { + let layer = layers.last_mut().unwrap(); + layer.images.push(Image { handle: image::Handle::Vector(handle.clone()), - position: [bounds.x, bounds.y], + position: [ + bounds.x + translation.x, + bounds.y + translation.y, + ], size: [bounds.width, bounds.height], }); } @@ -295,7 +315,7 @@ impl Renderer { layers: &mut Vec>, ) { let first = layers.first().unwrap(); - let mut overlay = Layer::new(first.bounds, Vector::new(0, 0)); + let mut overlay = Layer::new(first.bounds); let font_id = self.text_pipeline.overlay_font(); let scale = wgpu_glyph::Scale { x: 20.0, y: 20.0 }; @@ -337,12 +357,8 @@ impl Renderer { let bounds = layer.bounds * scale_factor; if layer.meshes.len() > 0 { - let translated = transformation - * Transformation::scale(scale_factor, scale_factor) - * Transformation::translate( - -(layer.offset.x as f32), - -(layer.offset.y as f32), - ); + let scaled = transformation + * Transformation::scale(scale_factor, scale_factor); self.triangle_pipeline.draw( device, @@ -350,7 +366,7 @@ impl Renderer { target, target_width, target_height, - translated, + scaled, &layer.meshes, bounds, ); @@ -371,18 +387,14 @@ impl Renderer { #[cfg(any(feature = "image", feature = "svg"))] { if layer.images.len() > 0 { - let translated_and_scaled = transformation - * Transformation::scale(scale_factor, scale_factor) - * Transformation::translate( - -(layer.offset.x as f32), - -(layer.offset.y as f32), - ); + let scaled = transformation + * Transformation::scale(scale_factor, scale_factor); self.image_pipeline.draw( device, encoder, &layer.images, - translated_and_scaled, + scaled, bounds, target, scale_factor, -- cgit