From 5a5ca34b5fcab9266359d3f0885782969f8c058e Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sat, 5 Oct 2019 19:22:51 +0200 Subject: Modularize `iced_wgpu` --- wgpu/src/renderer.rs | 167 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 167 insertions(+) create mode 100644 wgpu/src/renderer.rs (limited to 'wgpu/src/renderer.rs') diff --git a/wgpu/src/renderer.rs b/wgpu/src/renderer.rs new file mode 100644 index 00000000..df8887e3 --- /dev/null +++ b/wgpu/src/renderer.rs @@ -0,0 +1,167 @@ +use crate::Primitive; +use iced_native::{renderer::Debugger, Color, Layout, Point, Widget}; + +use raw_window_handle::HasRawWindowHandle; +use wgpu::{ + Adapter, CommandEncoderDescriptor, Device, DeviceDescriptor, Extensions, + Instance, Limits, PowerPreference, RequestAdapterOptions, Surface, + SwapChain, SwapChainDescriptor, TextureFormat, TextureUsage, +}; +use wgpu_glyph::{GlyphBrush, GlyphBrushBuilder, Section}; + +use std::{cell::RefCell, rc::Rc}; + +mod button; +mod checkbox; +mod column; +mod image; +mod radio; +mod row; +mod slider; +mod text; + +pub struct Renderer { + instance: Instance, + surface: Surface, + adapter: Adapter, + device: Device, + glyph_brush: Rc>>, +} + +pub struct Target { + width: u16, + height: u16, + swap_chain: SwapChain, +} + +impl Renderer { + pub fn new(window: &W) -> Self { + let instance = Instance::new(); + + let adapter = instance.request_adapter(&RequestAdapterOptions { + power_preference: PowerPreference::LowPower, + }); + + let mut device = adapter.request_device(&DeviceDescriptor { + extensions: Extensions { + anisotropic_filtering: false, + }, + limits: Limits { max_bind_groups: 1 }, + }); + + let surface = instance.create_surface(window.raw_window_handle()); + + // TODO: Think about font loading strategy + // Loading system fonts with fallback may be a good idea + let font: &[u8] = + include_bytes!("../../examples/tour/resources/Roboto-Regular.ttf"); + + let glyph_brush = GlyphBrushBuilder::using_font_bytes(font) + .build(&mut device, TextureFormat::Bgra8UnormSrgb); + + Self { + instance, + surface, + adapter, + device, + glyph_brush: Rc::new(RefCell::new(glyph_brush)), + } + } + + pub fn target(&self, width: u16, height: u16) -> Target { + Target { + width, + height, + swap_chain: self.device.create_swap_chain( + &self.surface, + &SwapChainDescriptor { + usage: TextureUsage::OUTPUT_ATTACHMENT, + format: TextureFormat::Bgra8UnormSrgb, + width: u32::from(width), + height: u32::from(height), + present_mode: wgpu::PresentMode::Vsync, + }, + ), + } + } + + pub fn draw(&mut self, target: &mut Target, primitive: &Primitive) { + let frame = target.swap_chain.get_next_texture(); + + let mut encoder = self + .device + .create_command_encoder(&CommandEncoderDescriptor { todo: 0 }); + + let _ = encoder.begin_render_pass(&wgpu::RenderPassDescriptor { + color_attachments: &[wgpu::RenderPassColorAttachmentDescriptor { + attachment: &frame.view, + resolve_target: None, + load_op: wgpu::LoadOp::Clear, + store_op: wgpu::StoreOp::Store, + clear_color: wgpu::Color { + r: 1.0, + g: 1.0, + b: 1.0, + a: 1.0, + }, + }], + depth_stencil_attachment: None, + }); + + self.draw_primitive(primitive); + + self.glyph_brush + .borrow_mut() + .draw_queued( + &mut self.device, + &mut encoder, + &frame.view, + u32::from(target.width), + u32::from(target.height), + ) + .expect("Draw text"); + + self.device.get_queue().submit(&[encoder.finish()]); + } + + fn draw_primitive(&mut self, primitive: &Primitive) { + match primitive { + Primitive::None => {} + Primitive::Group { primitives } => { + // TODO: Inspect a bit and regroup (?) + for primitive in primitives { + self.draw_primitive(primitive) + } + } + Primitive::Text { + content, + bounds, + size, + } => self.glyph_brush.borrow_mut().queue(Section { + text: &content, + screen_position: (bounds.x, bounds.y), + bounds: (bounds.width, bounds.height), + scale: wgpu_glyph::Scale { x: *size, y: *size }, + ..Default::default() + }), + } + } +} + +impl iced_native::Renderer for Renderer { + // TODO: Add `MouseCursor` here (?) + type Primitive = Primitive; +} + +impl Debugger for Renderer { + fn explain( + &mut self, + widget: &dyn Widget, + layout: Layout<'_>, + cursor_position: Point, + _color: Color, + ) -> Self::Primitive { + // TODO: Include a bordered box to display layout bounds + widget.draw(self, layout, cursor_position) + } +} -- cgit From 7765e6da50a3448501ee1b639e580c94a4fbda8a Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sun, 6 Oct 2019 19:22:25 +0200 Subject: Draft `Box` primitive --- wgpu/src/renderer.rs | 3 +++ 1 file changed, 3 insertions(+) (limited to 'wgpu/src/renderer.rs') diff --git a/wgpu/src/renderer.rs b/wgpu/src/renderer.rs index df8887e3..56986bd1 100644 --- a/wgpu/src/renderer.rs +++ b/wgpu/src/renderer.rs @@ -144,6 +144,9 @@ impl Renderer { scale: wgpu_glyph::Scale { x: *size, y: *size }, ..Default::default() }), + Primitive::Box { bounds, background } => { + // TODO: Batch boxes and draw them all at once + } } } } -- cgit From 5286ef36b6a5eb6846b5675a7a4aced72601df3b Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sun, 6 Oct 2019 20:06:13 +0200 Subject: Make `tour` a simple example instead of a crate --- 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 56986bd1..f8f000f0 100644 --- a/wgpu/src/renderer.rs +++ b/wgpu/src/renderer.rs @@ -54,7 +54,7 @@ impl Renderer { // TODO: Think about font loading strategy // Loading system fonts with fallback may be a good idea let font: &[u8] = - include_bytes!("../../examples/tour/resources/Roboto-Regular.ttf"); + include_bytes!("../../examples/resources/Roboto-Regular.ttf"); let glyph_brush = GlyphBrushBuilder::using_font_bytes(font) .build(&mut device, TextureFormat::Bgra8UnormSrgb); -- cgit From 70c17b053b10741f6018b2559bb46c5f289cadb9 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Mon, 7 Oct 2019 02:17:40 +0200 Subject: Rename `Box` primitive to `Quad` --- 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 f8f000f0..5db47a8e 100644 --- a/wgpu/src/renderer.rs +++ b/wgpu/src/renderer.rs @@ -144,8 +144,8 @@ impl Renderer { scale: wgpu_glyph::Scale { x: *size, y: *size }, ..Default::default() }), - Primitive::Box { bounds, background } => { - // TODO: Batch boxes and draw them all at once + Primitive::Quad { bounds, background } => { + // TODO: Batch quads and draw them all at once } } } -- cgit From c9510db551893775d3233340dd114d971e24323a Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Mon, 7 Oct 2019 03:56:16 +0200 Subject: Render colored quads --- wgpu/src/renderer.rs | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) (limited to 'wgpu/src/renderer.rs') diff --git a/wgpu/src/renderer.rs b/wgpu/src/renderer.rs index 5db47a8e..8e69a91a 100644 --- a/wgpu/src/renderer.rs +++ b/wgpu/src/renderer.rs @@ -1,4 +1,4 @@ -use crate::Primitive; +use crate::{quad, Background, Primitive, Quad, Transformation}; use iced_native::{renderer::Debugger, Color, Layout, Point, Widget}; use raw_window_handle::HasRawWindowHandle; @@ -25,12 +25,16 @@ pub struct Renderer { surface: Surface, adapter: Adapter, device: Device, + quad_pipeline: quad::Pipeline, + + quads: Vec, glyph_brush: Rc>>, } pub struct Target { width: u16, height: u16, + transformation: Transformation, swap_chain: SwapChain, } @@ -59,11 +63,16 @@ impl Renderer { let glyph_brush = GlyphBrushBuilder::using_font_bytes(font) .build(&mut device, TextureFormat::Bgra8UnormSrgb); + let quad_pipeline = quad::Pipeline::new(&mut device); + Self { instance, surface, adapter, device, + quad_pipeline, + + quads: Vec::new(), glyph_brush: Rc::new(RefCell::new(glyph_brush)), } } @@ -72,6 +81,7 @@ impl Renderer { Target { width, height, + transformation: Transformation::orthographic(width, height), swap_chain: self.device.create_swap_chain( &self.surface, &SwapChainDescriptor { @@ -110,6 +120,16 @@ impl Renderer { self.draw_primitive(primitive); + self.quad_pipeline.draw( + &mut self.device, + &mut encoder, + &self.quads, + target.transformation, + &frame.view, + ); + + self.quads.clear(); + self.glyph_brush .borrow_mut() .draw_queued( @@ -145,7 +165,13 @@ impl Renderer { ..Default::default() }), Primitive::Quad { bounds, background } => { - // TODO: Batch quads and draw them all at once + self.quads.push(Quad { + position: [bounds.x, bounds.y], + scale: [bounds.width, bounds.height], + color: match background { + Background::Color(color) => color.into_linear(), + }, + }); } } } -- cgit From c9da3a10d9c4fcc9504b25eed873708406e3a9c7 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Mon, 7 Oct 2019 04:05:40 +0200 Subject: Use `log` crate instead of `dbg!` --- wgpu/src/renderer.rs | 2 ++ 1 file changed, 2 insertions(+) (limited to 'wgpu/src/renderer.rs') diff --git a/wgpu/src/renderer.rs b/wgpu/src/renderer.rs index 8e69a91a..926f12ae 100644 --- a/wgpu/src/renderer.rs +++ b/wgpu/src/renderer.rs @@ -96,6 +96,8 @@ impl Renderer { } pub fn draw(&mut self, target: &mut Target, primitive: &Primitive) { + log::debug!("Drawing"); + let frame = target.swap_chain.get_next_texture(); let mut encoder = self -- cgit From 0995950526bb605ddef5621c6e0590bb3232c1cb Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Mon, 7 Oct 2019 19:50:04 +0200 Subject: Use latest `wgpu` --- wgpu/src/renderer.rs | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) (limited to 'wgpu/src/renderer.rs') diff --git a/wgpu/src/renderer.rs b/wgpu/src/renderer.rs index 926f12ae..9883943b 100644 --- a/wgpu/src/renderer.rs +++ b/wgpu/src/renderer.rs @@ -3,8 +3,8 @@ use iced_native::{renderer::Debugger, Color, Layout, Point, Widget}; use raw_window_handle::HasRawWindowHandle; use wgpu::{ - Adapter, CommandEncoderDescriptor, Device, DeviceDescriptor, Extensions, - Instance, Limits, PowerPreference, RequestAdapterOptions, Surface, + Adapter, BackendBit, CommandEncoderDescriptor, Device, DeviceDescriptor, + Extensions, Limits, PowerPreference, Queue, RequestAdapterOptions, Surface, SwapChain, SwapChainDescriptor, TextureFormat, TextureUsage, }; use wgpu_glyph::{GlyphBrush, GlyphBrushBuilder, Section}; @@ -21,10 +21,10 @@ mod slider; mod text; pub struct Renderer { - instance: Instance, surface: Surface, adapter: Adapter, device: Device, + queue: Queue, quad_pipeline: quad::Pipeline, quads: Vec, @@ -40,20 +40,20 @@ pub struct Target { impl Renderer { pub fn new(window: &W) -> Self { - let instance = Instance::new(); - - let adapter = instance.request_adapter(&RequestAdapterOptions { + let adapter = Adapter::request(&RequestAdapterOptions { power_preference: PowerPreference::LowPower, - }); + backends: BackendBit::all(), + }) + .expect("Request adapter"); - let mut device = adapter.request_device(&DeviceDescriptor { + let (mut device, queue) = adapter.request_device(&DeviceDescriptor { extensions: Extensions { anisotropic_filtering: false, }, limits: Limits { max_bind_groups: 1 }, }); - let surface = instance.create_surface(window.raw_window_handle()); + let surface = Surface::create(window); // TODO: Think about font loading strategy // Loading system fonts with fallback may be a good idea @@ -66,10 +66,10 @@ impl Renderer { let quad_pipeline = quad::Pipeline::new(&mut device); Self { - instance, surface, adapter, device, + queue, quad_pipeline, quads: Vec::new(), @@ -143,7 +143,7 @@ impl Renderer { ) .expect("Draw text"); - self.device.get_queue().submit(&[encoder.finish()]); + self.queue.submit(&[encoder.finish()]); } fn draw_primitive(&mut self, primitive: &Primitive) { -- cgit From 10e10e5e06841574425d2633f1c2916733f7b4ff Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 8 Oct 2019 03:13:41 +0200 Subject: Make `iced_core::Button` customizable Now it supports: - Any kind of content - Custom border radius - Custom background --- wgpu/src/renderer.rs | 76 +++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 66 insertions(+), 10 deletions(-) (limited to 'wgpu/src/renderer.rs') diff --git a/wgpu/src/renderer.rs b/wgpu/src/renderer.rs index 9883943b..ae5692e3 100644 --- a/wgpu/src/renderer.rs +++ b/wgpu/src/renderer.rs @@ -1,5 +1,7 @@ -use crate::{quad, Background, Primitive, Quad, Transformation}; -use iced_native::{renderer::Debugger, Color, Layout, Point, Widget}; +use crate::{quad, Primitive, Quad, Transformation}; +use iced_native::{ + renderer::Debugger, Background, Color, Layout, Point, Widget, +}; use raw_window_handle::HasRawWindowHandle; use wgpu::{ @@ -159,20 +161,74 @@ impl Renderer { content, bounds, size, - } => self.glyph_brush.borrow_mut().queue(Section { - text: &content, - screen_position: (bounds.x, bounds.y), - bounds: (bounds.width, bounds.height), - scale: wgpu_glyph::Scale { x: *size, y: *size }, - ..Default::default() - }), - Primitive::Quad { bounds, background } => { + color, + horizontal_alignment, + vertical_alignment, + } => { + let x = match horizontal_alignment { + iced_native::text::HorizontalAlignment::Left => bounds.x, + iced_native::text::HorizontalAlignment::Center => { + bounds.x + bounds.width / 2.0 + } + iced_native::text::HorizontalAlignment::Right => { + bounds.x + bounds.width + } + }; + + let y = match vertical_alignment { + iced_native::text::VerticalAlignment::Top => bounds.y, + iced_native::text::VerticalAlignment::Center => { + bounds.y + bounds.height / 2.0 + } + iced_native::text::VerticalAlignment::Bottom => { + bounds.y + bounds.height + } + }; + + self.glyph_brush.borrow_mut().queue(Section { + text: &content, + screen_position: (x, y), + bounds: (bounds.width, bounds.height), + scale: wgpu_glyph::Scale { x: *size, y: *size }, + color: color.into_linear(), + layout: wgpu_glyph::Layout::default() + .h_align(match horizontal_alignment { + iced_native::text::HorizontalAlignment::Left => { + wgpu_glyph::HorizontalAlign::Left + } + iced_native::text::HorizontalAlignment::Center => { + wgpu_glyph::HorizontalAlign::Center + } + iced_native::text::HorizontalAlignment::Right => { + wgpu_glyph::HorizontalAlign::Right + } + }) + .v_align(match vertical_alignment { + iced_native::text::VerticalAlignment::Top => { + wgpu_glyph::VerticalAlign::Top + } + iced_native::text::VerticalAlignment::Center => { + wgpu_glyph::VerticalAlign::Center + } + iced_native::text::VerticalAlignment::Bottom => { + wgpu_glyph::VerticalAlign::Bottom + } + }), + ..Default::default() + }) + } + Primitive::Quad { + bounds, + background, + border_radius, + } => { self.quads.push(Quad { position: [bounds.x, bounds.y], scale: [bounds.width, bounds.height], color: match background { Background::Color(color) => color.into_linear(), }, + border_radius: u32::from(*border_radius), }); } } -- cgit From a92a0b73ed7ed935df762d06c4249894fd35b227 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 9 Oct 2019 05:36:49 +0200 Subject: Move `winit` logic from `iced` to `iced_winit` - Added new `renderer::Windowed` trait. This shoud allow users to easily try different renderers by simply changing one line. - Renamed `UserInterface` traits to `Application`, as the `run` method takes total control of the current thread. - Moved `MouseCursor` back to `iced_native`. The new `renderer::Windowed` trait returns one on `draw`. - Split `iced_native` renderer in multiple modules, for consistency. --- wgpu/src/renderer.rs | 36 +++++++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) (limited to 'wgpu/src/renderer.rs') diff --git a/wgpu/src/renderer.rs b/wgpu/src/renderer.rs index ae5692e3..036efd27 100644 --- a/wgpu/src/renderer.rs +++ b/wgpu/src/renderer.rs @@ -1,6 +1,7 @@ use crate::{quad, Primitive, Quad, Transformation}; use iced_native::{ - renderer::Debugger, Background, Color, Layout, Point, Widget, + renderer::Debugger, renderer::Windowed, Background, Color, Layout, + MouseCursor, Point, Widget, }; use raw_window_handle::HasRawWindowHandle; @@ -41,7 +42,7 @@ pub struct Target { } impl Renderer { - pub fn new(window: &W) -> Self { + fn new(window: &W) -> Self { let adapter = Adapter::request(&RequestAdapterOptions { power_preference: PowerPreference::LowPower, backends: BackendBit::all(), @@ -79,7 +80,7 @@ impl Renderer { } } - pub fn target(&self, width: u16, height: u16) -> Target { + fn target(&self, width: u16, height: u16) -> Target { Target { width, height, @@ -97,7 +98,11 @@ impl Renderer { } } - pub fn draw(&mut self, target: &mut Target, primitive: &Primitive) { + fn draw( + &mut self, + target: &mut Target, + primitive: &Primitive, + ) -> MouseCursor { log::debug!("Drawing"); let frame = target.swap_chain.get_next_texture(); @@ -146,8 +151,9 @@ impl Renderer { .expect("Draw text"); self.queue.submit(&[encoder.finish()]); - } + MouseCursor::OutOfBounds + } fn draw_primitive(&mut self, primitive: &Primitive) { match primitive { Primitive::None => {} @@ -240,6 +246,26 @@ impl iced_native::Renderer for Renderer { type Primitive = Primitive; } +impl Windowed for Renderer { + type Target = Target; + + fn new(window: &W) -> Self { + Self::new(window) + } + + fn target(&self, width: u16, height: u16) -> Target { + self.target(width, height) + } + + fn draw( + &mut self, + target: &mut Target, + primitive: &Primitive, + ) -> MouseCursor { + self.draw(target, primitive) + } +} + impl Debugger for Renderer { fn explain( &mut self, -- cgit From 8846a239cf14edd464b1d09f6d6d57ad9b5c9fc7 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 11 Oct 2019 22:15:39 +0200 Subject: Rename `Renderer::Primitive` to `Renderer::Output` --- wgpu/src/renderer.rs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'wgpu/src/renderer.rs') diff --git a/wgpu/src/renderer.rs b/wgpu/src/renderer.rs index 036efd27..cdb44554 100644 --- a/wgpu/src/renderer.rs +++ b/wgpu/src/renderer.rs @@ -100,8 +100,8 @@ impl Renderer { fn draw( &mut self, - target: &mut Target, primitive: &Primitive, + target: &mut Target, ) -> MouseCursor { log::debug!("Drawing"); @@ -154,6 +154,7 @@ impl Renderer { MouseCursor::OutOfBounds } + fn draw_primitive(&mut self, primitive: &Primitive) { match primitive { Primitive::None => {} @@ -243,7 +244,7 @@ impl Renderer { impl iced_native::Renderer for Renderer { // TODO: Add `MouseCursor` here (?) - type Primitive = Primitive; + type Output = Primitive; } impl Windowed for Renderer { @@ -259,10 +260,10 @@ impl Windowed for Renderer { fn draw( &mut self, + output: &Self::Output, target: &mut Target, - primitive: &Primitive, ) -> MouseCursor { - self.draw(target, primitive) + self.draw(output, target) } } @@ -273,7 +274,7 @@ impl Debugger for Renderer { layout: Layout<'_>, cursor_position: Point, _color: Color, - ) -> Self::Primitive { + ) -> Self::Output { // TODO: Include a bordered box to display layout bounds widget.draw(self, layout, cursor_position) } -- cgit From a031a6f2130b3913a2419e4cea859c22aa388213 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 11 Oct 2019 23:30:53 +0200 Subject: Handle mouse cursor in `iced_wgpu` --- wgpu/src/renderer.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) (limited to 'wgpu/src/renderer.rs') diff --git a/wgpu/src/renderer.rs b/wgpu/src/renderer.rs index cdb44554..8d54b2c7 100644 --- a/wgpu/src/renderer.rs +++ b/wgpu/src/renderer.rs @@ -100,7 +100,7 @@ impl Renderer { fn draw( &mut self, - primitive: &Primitive, + (primitive, mouse_cursor): &(Primitive, MouseCursor), target: &mut Target, ) -> MouseCursor { log::debug!("Drawing"); @@ -152,7 +152,7 @@ impl Renderer { self.queue.submit(&[encoder.finish()]); - MouseCursor::OutOfBounds + *mouse_cursor } fn draw_primitive(&mut self, primitive: &Primitive) { @@ -243,8 +243,7 @@ impl Renderer { } impl iced_native::Renderer for Renderer { - // TODO: Add `MouseCursor` here (?) - type Output = Primitive; + type Output = (Primitive, MouseCursor); } impl Windowed for Renderer { -- cgit From 734e80dea6cd39923aaa0d2f27c7368c9cbc5d62 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sun, 13 Oct 2019 18:22:26 +0200 Subject: Draft `Debugger` implementation in `iced_wgpu` --- wgpu/src/renderer.rs | 33 ++++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) (limited to 'wgpu/src/renderer.rs') diff --git a/wgpu/src/renderer.rs b/wgpu/src/renderer.rs index 8d54b2c7..8930e9df 100644 --- a/wgpu/src/renderer.rs +++ b/wgpu/src/renderer.rs @@ -272,9 +272,36 @@ impl Debugger for Renderer { widget: &dyn Widget, layout: Layout<'_>, cursor_position: Point, - _color: Color, + color: Color, ) -> Self::Output { - // TODO: Include a bordered box to display layout bounds - widget.draw(self, layout, cursor_position) + let mut primitives = Vec::new(); + let (primitive, cursor) = widget.draw(self, layout, cursor_position); + + explain_layout(layout, color, &mut primitives); + primitives.push(primitive); + + (Primitive::Group { primitives }, cursor) + } +} + +fn explain_layout( + layout: Layout, + color: Color, + primitives: &mut Vec, +) { + // TODO: Draw borders instead + primitives.push(Primitive::Quad { + bounds: layout.bounds(), + background: Background::Color(Color { + r: 0.0, + g: 0.0, + b: 0.0, + a: 0.05, + }), + border_radius: 0, + }); + + for child in layout.children() { + explain_layout(child, color, primitives); } } -- cgit From 38b6c84e7761c049b17d178deb9c866386a53946 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 23 Oct 2019 01:21:23 +0200 Subject: Implement basic image rendering in `iced_wgpu` --- wgpu/src/renderer.rs | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) (limited to 'wgpu/src/renderer.rs') diff --git a/wgpu/src/renderer.rs b/wgpu/src/renderer.rs index 8930e9df..ab6f744f 100644 --- a/wgpu/src/renderer.rs +++ b/wgpu/src/renderer.rs @@ -1,4 +1,4 @@ -use crate::{quad, Primitive, Quad, Transformation}; +use crate::{quad, Image, Primitive, Quad, Transformation}; use iced_native::{ renderer::Debugger, renderer::Windowed, Background, Color, Layout, MouseCursor, Point, Widget, @@ -29,8 +29,10 @@ pub struct Renderer { device: Device, queue: Queue, quad_pipeline: quad::Pipeline, + image_pipeline: crate::image::Pipeline, quads: Vec, + images: Vec, glyph_brush: Rc>>, } @@ -67,6 +69,7 @@ impl Renderer { .build(&mut device, TextureFormat::Bgra8UnormSrgb); let quad_pipeline = quad::Pipeline::new(&mut device); + let image_pipeline = crate::image::Pipeline::new(&mut device); Self { surface, @@ -74,8 +77,10 @@ impl Renderer { device, queue, quad_pipeline, + image_pipeline, quads: Vec::new(), + images: Vec::new(), glyph_brush: Rc::new(RefCell::new(glyph_brush)), } } @@ -139,6 +144,16 @@ impl Renderer { self.quads.clear(); + self.image_pipeline.draw( + &mut self.device, + &mut encoder, + &self.images, + target.transformation, + &frame.view, + ); + + self.images.clear(); + self.glyph_brush .borrow_mut() .draw_queued( @@ -238,6 +253,13 @@ impl Renderer { border_radius: u32::from(*border_radius), }); } + Primitive::Image { path, bounds } => { + self.images.push(Image { + path: path.clone(), + position: [bounds.x, bounds.y], + scale: [bounds.width, bounds.height], + }); + } } } } -- cgit