From f1e20a61f16388ed4d2dac734bab30d67bbd84b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Sun, 9 Feb 2020 03:25:13 +0100 Subject: [PATCH 01/12] Allow `iced_wgpu` to render to any `TextureView` --- native/src/window.rs | 4 +- native/src/window/backend.rs | 55 ++++++++++++++++ native/src/window/renderer.rs | 73 ++++++++++----------- src/application.rs | 2 +- wgpu/src/lib.rs | 10 ++- wgpu/src/renderer.rs | 116 ++++++++++------------------------ wgpu/src/renderer/target.rs | 91 -------------------------- wgpu/src/target.rs | 8 +++ wgpu/src/viewport.rs | 32 ++++++++++ wgpu/src/window.rs | 5 ++ wgpu/src/window/backend.rs | 99 +++++++++++++++++++++++++++++ wgpu/src/window/swap_chain.rs | 49 ++++++++++++++ winit/src/application.rs | 39 +++++++----- 13 files changed, 349 insertions(+), 234 deletions(-) create mode 100644 native/src/window/backend.rs delete mode 100644 wgpu/src/renderer/target.rs create mode 100644 wgpu/src/target.rs create mode 100644 wgpu/src/viewport.rs create mode 100644 wgpu/src/window.rs create mode 100644 wgpu/src/window/backend.rs create mode 100644 wgpu/src/window/swap_chain.rs diff --git a/native/src/window.rs b/native/src/window.rs index db9226dc06..4dcae62f42 100644 --- a/native/src/window.rs +++ b/native/src/window.rs @@ -1,6 +1,6 @@ //! Build window-based GUI applications. +mod backend; mod event; -mod renderer; +pub use backend::Backend; pub use event::Event; -pub use renderer::{Renderer, Target}; diff --git a/native/src/window/backend.rs b/native/src/window/backend.rs new file mode 100644 index 0000000000..690dbdab1e --- /dev/null +++ b/native/src/window/backend.rs @@ -0,0 +1,55 @@ +use crate::MouseCursor; + +use raw_window_handle::HasRawWindowHandle; + +/// A graphics backend that can render to windows. +pub trait Backend: Sized { + /// The settings of the backend. + type Settings: Default; + + /// The iced renderer of the backend. + type Renderer: crate::Renderer; + + /// The surface of the backend. + type Surface; + + /// The swap chain of the backend. + type SwapChain; + + /// Creates a new [`Backend`] and an associated iced renderer. + /// + /// [`Backend`]: trait.Backend.html + fn new(settings: Self::Settings) -> (Self, Self::Renderer); + + /// Crates a new [`Surface`] for the given window. + /// + /// [`Surface`]: #associatedtype.Surface + fn create_surface( + &mut self, + window: &W, + ) -> Self::Surface; + + /// Crates a new [`SwapChain`] for the given [`Surface`]. + /// + /// [`SwapChain`]: #associatedtype.SwapChain + /// [`Surface`]: #associatedtype.Surface + fn create_swap_chain( + &mut self, + surface: &Self::Surface, + width: u32, + height: u32, + scale_factor: f64, + ) -> Self::SwapChain; + + /// Draws the output primitives to the next frame of the given [`SwapChain`]. + /// + /// [`SwapChain`]: #associatedtype.SwapChain + /// [`Surface`]: #associatedtype.Surface + fn draw>( + &mut self, + renderer: &mut Self::Renderer, + swap_chain: &mut Self::SwapChain, + output: &::Output, + overlay: &[T], + ) -> MouseCursor; +} diff --git a/native/src/window/renderer.rs b/native/src/window/renderer.rs index a3cbb8ceac..b0cc01344d 100644 --- a/native/src/window/renderer.rs +++ b/native/src/window/renderer.rs @@ -2,57 +2,54 @@ use crate::MouseCursor; use raw_window_handle::HasRawWindowHandle; -/// A renderer that can target windows. -pub trait Renderer: crate::Renderer + Sized { - /// The settings of the renderer. +/// A graphics backend that can render to windows. +pub trait Backend: Sized { + /// The settings of the backend. type Settings: Default; - /// The type of target. - type Target: Target; + /// The iced renderer of the backend. + type Renderer: crate::Renderer; - /// Creates a new window [`Renderer`]. + /// The surface of the backend. + type Surface; + + /// The target of the backend. + type Target; + + /// Creates a new [`Gpu`] and an associated iced renderer. /// - /// [`Renderer`]: trait.Renderer.html - fn new(settings: Self::Settings) -> Self; + /// [`Gpu`]: trait.Gpu.html + fn new(settings: Self::Settings) -> (Self, Self::Renderer); - /// Performs the drawing operations described in the output on the given - /// target. + /// Crates a new [`Surface`] for the given window. /// - /// The overlay can be a bunch of debug text logs. It should be rendered on - /// top of the GUI on most scenarios. - fn draw>( + /// [`Surface`]: #associatedtype.Surface + fn create_surface( &mut self, - output: &Self::Output, - overlay: &[T], - target: &mut Self::Target, - ) -> MouseCursor; -} - -/// A rendering target. -pub trait Target { - /// The renderer of this target. - type Renderer; + window: &W, + ) -> Self::Surface; - /// Creates a new rendering [`Target`] from the given window handle, width, - /// height and dpi factor. + /// Crates a new [`Target`] for the given [`Surface`]. /// - /// [`Target`]: trait.Target.html - fn new( - window: &W, + /// [`Target`]: #associatedtype.Target + /// [`Surface`]: #associatedtype.Surface + fn create_target( + &mut self, + surface: &Self::Surface, width: u32, height: u32, scale_factor: f64, - renderer: &Self::Renderer, - ) -> Self; + ) -> Self::Target; - /// Resizes the current [`Target`]. + /// Draws the output primitives to the given [`Target`]. /// - /// [`Target`]: trait.Target.html - fn resize( + /// [`Target`]: #associatedtype.Target + /// [`Surface`]: #associatedtype.Surface + fn draw>( &mut self, - width: u32, - height: u32, - scale_factor: f64, - renderer: &Self::Renderer, - ); + renderer: &mut Self::Renderer, + target: &mut Self::Target, + output: &::Output, + overlay: &[T], + ) -> MouseCursor; } diff --git a/src/application.rs b/src/application.rs index 926a2986b9..0a4b6d9e47 100644 --- a/src/application.rs +++ b/src/application.rs @@ -193,7 +193,7 @@ impl iced_winit::Application for Instance where A: Application, { - type Renderer = iced_wgpu::Renderer; + type Backend = iced_wgpu::window::Backend; type Executor = A::Executor; type Message = A::Message; diff --git a/wgpu/src/lib.rs b/wgpu/src/lib.rs index b8cd3fce91..9c1739b234 100644 --- a/wgpu/src/lib.rs +++ b/wgpu/src/lib.rs @@ -19,7 +19,7 @@ //! [`wgpu`]: https://github.com/gfx-rs/wgpu-rs //! [WebGPU API]: https://gpuweb.github.io/gpuweb/ //! [`wgpu_glyph`]: https://github.com/hecrj/wgpu_glyph -#![deny(missing_docs)] +//#![deny(missing_docs)] #![deny(missing_debug_implementations)] #![deny(unused_results)] #![forbid(unsafe_code)] @@ -27,19 +27,25 @@ pub mod defaults; pub mod triangle; pub mod widget; +pub mod window; mod image; mod primitive; mod quad; mod renderer; mod settings; +mod target; mod text; mod transformation; +mod viewport; pub use defaults::Defaults; pub use primitive::Primitive; -pub use renderer::{Renderer, Target}; +pub use renderer::Renderer; pub use settings::Settings; +pub use target::Target; +pub use viewport::Viewport; + #[doc(no_inline)] pub use widget::*; diff --git a/wgpu/src/renderer.rs b/wgpu/src/renderer.rs index 93d2bb1375..a99080f40a 100644 --- a/wgpu/src/renderer.rs +++ b/wgpu/src/renderer.rs @@ -1,29 +1,20 @@ use crate::{ image, quad, text, triangle, Defaults, Image, Primitive, Quad, Settings, - Transformation, + Target, Transformation, }; use iced_native::{ - layout, window, Background, Color, Layout, MouseCursor, Point, Rectangle, - Vector, Widget, + layout, Background, Color, Layout, MouseCursor, Point, Rectangle, Vector, + Widget, }; use std::sync::Arc; -use wgpu::{ - Adapter, BackendBit, CommandEncoderDescriptor, Device, DeviceDescriptor, - Extensions, Limits, PowerPreference, Queue, RequestAdapterOptions, -}; -mod target; mod widget; -pub use target::Target; - /// A [`wgpu`] renderer. /// /// [`wgpu`]: https://github.com/gfx-rs/wgpu-rs #[derive(Debug)] pub struct Renderer { - device: Device, - queue: Queue, quad_pipeline: quad::Pipeline, image_pipeline: image::Pipeline, text_pipeline: text::Pipeline, @@ -53,29 +44,16 @@ impl<'a> Layer<'a> { } impl Renderer { - fn new(settings: Settings) -> Self { - let adapter = Adapter::request(&RequestAdapterOptions { - power_preference: PowerPreference::Default, - backends: BackendBit::all(), - }) - .expect("Request adapter"); - - let (mut device, queue) = adapter.request_device(&DeviceDescriptor { - extensions: Extensions { - anisotropic_filtering: false, - }, - limits: Limits { max_bind_groups: 2 }, - }); - - let text_pipeline = - text::Pipeline::new(&mut device, settings.default_font); - let quad_pipeline = quad::Pipeline::new(&mut device); - let image_pipeline = crate::image::Pipeline::new(&mut device); - let triangle_pipeline = triangle::Pipeline::new(&mut device); + /// Creates a new [`Renderer`]. + /// + /// [`Renderer`]: struct.Renderer.html + pub fn new(settings: Settings, device: &mut wgpu::Device) -> 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); Self { - device, - queue, quad_pipeline, image_pipeline, text_pipeline, @@ -83,38 +61,25 @@ impl Renderer { } } - fn draw>( + /// Draws the provided primitives in the given [`Target`]. + /// + /// The text provided as overlay will be renderer on top of the primitives. + /// This is useful for rendering debug information. + /// + /// [`Target`]: struct.Target.html + pub fn draw>( &mut self, + device: &mut wgpu::Device, + encoder: &mut wgpu::CommandEncoder, + target: Target<'_>, (primitive, mouse_cursor): &(Primitive, MouseCursor), overlay: &[T], - target: &mut Target, ) -> MouseCursor { log::debug!("Drawing"); - let (width, height) = target.dimensions(); - let scale_factor = target.scale_factor(); - let transformation = target.transformation(); - let frame = target.next_frame(); - - 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, - }); + let (width, height) = target.viewport.dimensions(); + let scale_factor = target.viewport.scale_factor(); + let transformation = target.viewport.transformation(); let mut layers = Vec::new(); @@ -133,15 +98,15 @@ impl Renderer { for layer in layers { self.flush( + device, scale_factor, transformation, &layer, - &mut encoder, - &frame.view, + encoder, + target.texture, ); } - self.queue.submit(&[encoder.finish()]); self.image_pipeline.trim_cache(); *mouse_cursor @@ -336,6 +301,7 @@ impl Renderer { fn flush( &mut self, + device: &mut wgpu::Device, scale_factor: f32, transformation: Transformation, layer: &Layer<'_>, @@ -352,7 +318,7 @@ impl Renderer { ); self.triangle_pipeline.draw( - &mut self.device, + device, encoder, target, translated, @@ -364,7 +330,7 @@ impl Renderer { if layer.quads.len() > 0 { self.quad_pipeline.draw( - &mut self.device, + device, encoder, &layer.quads, transformation, @@ -383,7 +349,7 @@ impl Renderer { ); self.image_pipeline.draw( - &mut self.device, + device, encoder, &layer.images, translated_and_scaled, @@ -429,7 +395,7 @@ impl Renderer { } self.text_pipeline.draw_queued( - &mut self.device, + device, encoder, target, transformation, @@ -461,24 +427,6 @@ impl iced_native::Renderer for Renderer { } } -impl window::Renderer for Renderer { - type Settings = Settings; - type Target = Target; - - fn new(settings: Settings) -> Self { - Self::new(settings) - } - - fn draw>( - &mut self, - output: &Self::Output, - overlay: &[T], - target: &mut Target, - ) -> MouseCursor { - self.draw(output, overlay, target) - } -} - impl layout::Debugger for Renderer { fn explain( &mut self, diff --git a/wgpu/src/renderer/target.rs b/wgpu/src/renderer/target.rs deleted file mode 100644 index 2097497612..0000000000 --- a/wgpu/src/renderer/target.rs +++ /dev/null @@ -1,91 +0,0 @@ -use crate::{Renderer, Transformation}; -use iced_native::window; - -use raw_window_handle::HasRawWindowHandle; - -/// A rendering target. -#[derive(Debug)] -pub struct Target { - surface: wgpu::Surface, - width: u32, - height: u32, - scale_factor: f32, - transformation: Transformation, - swap_chain: wgpu::SwapChain, -} - -impl Target { - pub(crate) fn dimensions(&self) -> (u32, u32) { - (self.width, self.height) - } - - pub(crate) fn scale_factor(&self) -> f32 { - self.scale_factor - } - - pub(crate) fn transformation(&self) -> Transformation { - self.transformation - } - - pub(crate) fn next_frame(&mut self) -> wgpu::SwapChainOutput<'_> { - self.swap_chain.get_next_texture() - } -} - -impl window::Target for Target { - type Renderer = Renderer; - - fn new( - window: &W, - width: u32, - height: u32, - scale_factor: f64, - renderer: &Renderer, - ) -> Target { - let surface = wgpu::Surface::create(window); - let swap_chain = - new_swap_chain(&surface, width, height, &renderer.device); - - Target { - surface, - width, - height, - scale_factor: scale_factor as f32, - transformation: Transformation::orthographic(width, height), - swap_chain, - } - } - - fn resize( - &mut self, - width: u32, - height: u32, - scale_factor: f64, - renderer: &Renderer, - ) { - self.width = width; - self.height = height; - self.scale_factor = scale_factor as f32; - self.transformation = Transformation::orthographic(width, height); - self.swap_chain = - new_swap_chain(&self.surface, width, height, &renderer.device); - } -} - -fn new_swap_chain( - surface: &wgpu::Surface, - width: u32, - height: u32, - device: &wgpu::Device, -) -> wgpu::SwapChain { - device.create_swap_chain( - &surface, - &wgpu::SwapChainDescriptor { - usage: wgpu::TextureUsage::OUTPUT_ATTACHMENT, - format: wgpu::TextureFormat::Bgra8UnormSrgb, - width, - height, - present_mode: wgpu::PresentMode::Vsync, - }, - ) -} diff --git a/wgpu/src/target.rs b/wgpu/src/target.rs new file mode 100644 index 0000000000..544e83d196 --- /dev/null +++ b/wgpu/src/target.rs @@ -0,0 +1,8 @@ +use crate::Viewport; + +/// A rendering target. +#[derive(Debug)] +pub struct Target<'a> { + pub texture: &'a wgpu::TextureView, + pub viewport: &'a Viewport, +} diff --git a/wgpu/src/viewport.rs b/wgpu/src/viewport.rs new file mode 100644 index 0000000000..a4bcb704c7 --- /dev/null +++ b/wgpu/src/viewport.rs @@ -0,0 +1,32 @@ +use crate::Transformation; + +#[derive(Debug)] +pub struct Viewport { + width: u32, + height: u32, + scale_factor: f32, + transformation: Transformation, +} + +impl Viewport { + pub fn new(width: u32, height: u32, scale_factor: f64) -> Viewport { + Viewport { + width, + height, + scale_factor: scale_factor as f32, + transformation: Transformation::orthographic(width, height), + } + } + + pub fn dimensions(&self) -> (u32, u32) { + (self.width, self.height) + } + + pub fn scale_factor(&self) -> f32 { + self.scale_factor + } + + pub(crate) fn transformation(&self) -> Transformation { + self.transformation + } +} diff --git a/wgpu/src/window.rs b/wgpu/src/window.rs new file mode 100644 index 0000000000..97cac9b7b6 --- /dev/null +++ b/wgpu/src/window.rs @@ -0,0 +1,5 @@ +mod backend; +mod swap_chain; + +pub use backend::Backend; +pub use swap_chain::SwapChain; diff --git a/wgpu/src/window/backend.rs b/wgpu/src/window/backend.rs new file mode 100644 index 0000000000..fda34a0aa9 --- /dev/null +++ b/wgpu/src/window/backend.rs @@ -0,0 +1,99 @@ +use crate::{window::SwapChain, Renderer, Settings, Target}; + +use iced_native::MouseCursor; +use raw_window_handle::HasRawWindowHandle; + +#[derive(Debug)] +pub struct Backend { + device: wgpu::Device, + queue: wgpu::Queue, +} + +impl iced_native::window::Backend for Backend { + type Settings = Settings; + type Renderer = Renderer; + type Surface = wgpu::Surface; + type SwapChain = SwapChain; + + fn new(settings: Self::Settings) -> (Backend, Renderer) { + let adapter = wgpu::Adapter::request(&wgpu::RequestAdapterOptions { + power_preference: wgpu::PowerPreference::Default, + backends: wgpu::BackendBit::all(), + }) + .expect("Request adapter"); + + let (mut device, queue) = + adapter.request_device(&wgpu::DeviceDescriptor { + extensions: wgpu::Extensions { + anisotropic_filtering: false, + }, + limits: wgpu::Limits { max_bind_groups: 2 }, + }); + + let renderer = Renderer::new(settings, &mut device); + + (Backend { device, queue }, renderer) + } + + fn create_surface( + &mut self, + window: &W, + ) -> wgpu::Surface { + wgpu::Surface::create(window) + } + + fn create_swap_chain( + &mut self, + surface: &Self::Surface, + width: u32, + height: u32, + scale_factor: f64, + ) -> SwapChain { + SwapChain::new(&self.device, surface, width, height, scale_factor) + } + + fn draw>( + &mut self, + renderer: &mut Self::Renderer, + swap_chain: &mut SwapChain, + output: &::Output, + overlay: &[T], + ) -> MouseCursor { + let (frame, viewport) = swap_chain.next_frame(); + + let mut encoder = self.device.create_command_encoder( + &wgpu::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, + }); + + let mouse_cursor = renderer.draw( + &mut self.device, + &mut encoder, + Target { + texture: &frame.view, + viewport, + }, + output, + overlay, + ); + + self.queue.submit(&[encoder.finish()]); + + mouse_cursor + } +} diff --git a/wgpu/src/window/swap_chain.rs b/wgpu/src/window/swap_chain.rs new file mode 100644 index 0000000000..46aaa8699e --- /dev/null +++ b/wgpu/src/window/swap_chain.rs @@ -0,0 +1,49 @@ +use crate::Viewport; + +/// The rendering target of a window. +/// +/// It represents a series of virtual framebuffers with a scale factor. +#[derive(Debug)] +pub struct SwapChain { + raw: wgpu::SwapChain, + viewport: Viewport, +} + +impl SwapChain {} + +impl SwapChain { + pub fn new( + device: &wgpu::Device, + surface: &wgpu::Surface, + width: u32, + height: u32, + scale_factor: f64, + ) -> SwapChain { + SwapChain { + raw: new_swap_chain(surface, width, height, device), + viewport: Viewport::new(width, height, scale_factor), + } + } + + pub fn next_frame(&mut self) -> (wgpu::SwapChainOutput<'_>, &Viewport) { + (self.raw.get_next_texture(), &self.viewport) + } +} + +fn new_swap_chain( + surface: &wgpu::Surface, + width: u32, + height: u32, + device: &wgpu::Device, +) -> wgpu::SwapChain { + device.create_swap_chain( + &surface, + &wgpu::SwapChainDescriptor { + usage: wgpu::TextureUsage::OUTPUT_ATTACHMENT, + format: wgpu::TextureFormat::Bgra8UnormSrgb, + width, + height, + present_mode: wgpu::PresentMode::Vsync, + }, + ) +} diff --git a/winit/src/application.rs b/winit/src/application.rs index 3c0332ed1d..0d90525ae8 100644 --- a/winit/src/application.rs +++ b/winit/src/application.rs @@ -14,10 +14,10 @@ use crate::{ /// An [`Application`](trait.Application.html) can execute asynchronous actions /// by returning a [`Command`](struct.Command.html) in some of its methods. pub trait Application: Sized { - /// The renderer to use to draw the [`Application`]. + /// The graphics backend to use to draw the [`Application`]. /// /// [`Application`]: trait.Application.html - type Renderer: window::Renderer; + type Backend: window::Backend; /// The [`Executor`] that will run commands and subscriptions. /// @@ -75,7 +75,9 @@ pub trait Application: Sized { /// These widgets can produce __messages__ based on user interaction. /// /// [`Application`]: trait.Application.html - fn view(&mut self) -> Element<'_, Self::Message, Self::Renderer>; + fn view( + &mut self, + ) -> Element<'_, Self::Message, ::Renderer>; /// Returns the current [`Application`] mode. /// @@ -99,11 +101,11 @@ pub trait Application: Sized { /// [`Application`]: trait.Application.html fn run( settings: Settings, - renderer_settings: ::Settings, + backend_settings: ::Settings, ) where Self: 'static, { - use window::{Renderer as _, Target as _}; + use window::Backend as _; use winit::{ event::{self, WindowEvent}, event_loop::{ControlFlow, EventLoop}, @@ -162,17 +164,18 @@ pub trait Application: Sized { let mut resized = false; let clipboard = Clipboard::new(&window); - let mut renderer = Self::Renderer::new(renderer_settings); + let (mut backend, mut renderer) = Self::Backend::new(backend_settings); - let mut target = { + let surface = backend.create_surface(&window); + + let mut swap_chain = { let physical_size = size.physical(); - ::Target::new( - &window, + backend.create_swap_chain( + &surface, physical_size.width, physical_size.height, size.scale_factor(), - &renderer, ) }; @@ -306,18 +309,22 @@ pub trait Application: Sized { if resized { let physical_size = size.physical(); - target.resize( + swap_chain = backend.create_swap_chain( + &surface, physical_size.width, physical_size.height, size.scale_factor(), - &renderer, ); resized = false; } - let new_mouse_cursor = - renderer.draw(&primitive, &debug.overlay(), &mut target); + let new_mouse_cursor = backend.draw( + &mut renderer, + &mut swap_chain, + &primitive, + &debug.overlay(), + ); debug.render_finished(); @@ -451,10 +458,10 @@ pub trait Application: Sized { fn build_user_interface<'a, A: Application>( application: &'a mut A, cache: Cache, - renderer: &mut A::Renderer, + renderer: &mut ::Renderer, size: winit::dpi::LogicalSize, debug: &mut Debug, -) -> UserInterface<'a, A::Message, A::Renderer> { +) -> UserInterface<'a, A::Message, ::Renderer> { debug.view_started(); let view = application.view(); debug.view_finished(); From 8edb04fddd55519ebebeee32a79dac99eef9e174 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Sun, 9 Feb 2020 03:26:40 +0100 Subject: [PATCH 02/12] Remove unused `window::renderer` module --- native/src/window/renderer.rs | 55 ----------------------------------- 1 file changed, 55 deletions(-) delete mode 100644 native/src/window/renderer.rs diff --git a/native/src/window/renderer.rs b/native/src/window/renderer.rs deleted file mode 100644 index b0cc01344d..0000000000 --- a/native/src/window/renderer.rs +++ /dev/null @@ -1,55 +0,0 @@ -use crate::MouseCursor; - -use raw_window_handle::HasRawWindowHandle; - -/// A graphics backend that can render to windows. -pub trait Backend: Sized { - /// The settings of the backend. - type Settings: Default; - - /// The iced renderer of the backend. - type Renderer: crate::Renderer; - - /// The surface of the backend. - type Surface; - - /// The target of the backend. - type Target; - - /// Creates a new [`Gpu`] and an associated iced renderer. - /// - /// [`Gpu`]: trait.Gpu.html - fn new(settings: Self::Settings) -> (Self, Self::Renderer); - - /// Crates a new [`Surface`] for the given window. - /// - /// [`Surface`]: #associatedtype.Surface - fn create_surface( - &mut self, - window: &W, - ) -> Self::Surface; - - /// Crates a new [`Target`] for the given [`Surface`]. - /// - /// [`Target`]: #associatedtype.Target - /// [`Surface`]: #associatedtype.Surface - fn create_target( - &mut self, - surface: &Self::Surface, - width: u32, - height: u32, - scale_factor: f64, - ) -> Self::Target; - - /// Draws the output primitives to the given [`Target`]. - /// - /// [`Target`]: #associatedtype.Target - /// [`Surface`]: #associatedtype.Surface - fn draw>( - &mut self, - renderer: &mut Self::Renderer, - target: &mut Self::Target, - output: &::Output, - overlay: &[T], - ) -> MouseCursor; -} From 8f0b59a4b28bee028a879b0705eeeaa0b2e82df6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Sun, 9 Feb 2020 03:36:59 +0100 Subject: [PATCH 03/12] Remove `scale_factor` from `iced_wgpu::Viewport` --- native/src/window/backend.rs | 2 +- wgpu/src/renderer.rs | 3 ++- wgpu/src/viewport.rs | 11 ++++------- wgpu/src/window/backend.rs | 5 +++-- wgpu/src/window/swap_chain.rs | 3 +-- winit/src/application.rs | 3 +-- 6 files changed, 12 insertions(+), 15 deletions(-) diff --git a/native/src/window/backend.rs b/native/src/window/backend.rs index 690dbdab1e..3bc691cdef 100644 --- a/native/src/window/backend.rs +++ b/native/src/window/backend.rs @@ -38,7 +38,6 @@ pub trait Backend: Sized { surface: &Self::Surface, width: u32, height: u32, - scale_factor: f64, ) -> Self::SwapChain; /// Draws the output primitives to the next frame of the given [`SwapChain`]. @@ -50,6 +49,7 @@ pub trait Backend: Sized { renderer: &mut Self::Renderer, swap_chain: &mut Self::SwapChain, output: &::Output, + scale_factor: f64, overlay: &[T], ) -> MouseCursor; } diff --git a/wgpu/src/renderer.rs b/wgpu/src/renderer.rs index a99080f40a..904e3c6564 100644 --- a/wgpu/src/renderer.rs +++ b/wgpu/src/renderer.rs @@ -73,12 +73,13 @@ impl Renderer { encoder: &mut wgpu::CommandEncoder, target: Target<'_>, (primitive, mouse_cursor): &(Primitive, MouseCursor), + scale_factor: f64, overlay: &[T], ) -> MouseCursor { log::debug!("Drawing"); let (width, height) = target.viewport.dimensions(); - let scale_factor = target.viewport.scale_factor(); + let scale_factor = scale_factor as f32; let transformation = target.viewport.transformation(); let mut layers = Vec::new(); diff --git a/wgpu/src/viewport.rs b/wgpu/src/viewport.rs index a4bcb704c7..66242468ff 100644 --- a/wgpu/src/viewport.rs +++ b/wgpu/src/viewport.rs @@ -1,31 +1,28 @@ use crate::Transformation; +/// A viewing region for displaying computer graphics. #[derive(Debug)] pub struct Viewport { width: u32, height: u32, - scale_factor: f32, transformation: Transformation, } impl Viewport { - pub fn new(width: u32, height: u32, scale_factor: f64) -> Viewport { + /// Creates a new [`Viewport`] with the given dimensions. + pub fn new(width: u32, height: u32) -> Viewport { Viewport { width, height, - scale_factor: scale_factor as f32, transformation: Transformation::orthographic(width, height), } } + /// Returns the dimensions of the [`Viewport`]. pub fn dimensions(&self) -> (u32, u32) { (self.width, self.height) } - pub fn scale_factor(&self) -> f32 { - self.scale_factor - } - pub(crate) fn transformation(&self) -> Transformation { self.transformation } diff --git a/wgpu/src/window/backend.rs b/wgpu/src/window/backend.rs index fda34a0aa9..0e4f02c94f 100644 --- a/wgpu/src/window/backend.rs +++ b/wgpu/src/window/backend.rs @@ -47,9 +47,8 @@ impl iced_native::window::Backend for Backend { surface: &Self::Surface, width: u32, height: u32, - scale_factor: f64, ) -> SwapChain { - SwapChain::new(&self.device, surface, width, height, scale_factor) + SwapChain::new(&self.device, surface, width, height) } fn draw>( @@ -57,6 +56,7 @@ impl iced_native::window::Backend for Backend { renderer: &mut Self::Renderer, swap_chain: &mut SwapChain, output: &::Output, + scale_factor: f64, overlay: &[T], ) -> MouseCursor { let (frame, viewport) = swap_chain.next_frame(); @@ -89,6 +89,7 @@ impl iced_native::window::Backend for Backend { viewport, }, output, + scale_factor, overlay, ); diff --git a/wgpu/src/window/swap_chain.rs b/wgpu/src/window/swap_chain.rs index 46aaa8699e..3760e8a260 100644 --- a/wgpu/src/window/swap_chain.rs +++ b/wgpu/src/window/swap_chain.rs @@ -17,11 +17,10 @@ impl SwapChain { surface: &wgpu::Surface, width: u32, height: u32, - scale_factor: f64, ) -> SwapChain { SwapChain { raw: new_swap_chain(surface, width, height, device), - viewport: Viewport::new(width, height, scale_factor), + viewport: Viewport::new(width, height), } } diff --git a/winit/src/application.rs b/winit/src/application.rs index 0d90525ae8..22d1afabec 100644 --- a/winit/src/application.rs +++ b/winit/src/application.rs @@ -175,7 +175,6 @@ pub trait Application: Sized { &surface, physical_size.width, physical_size.height, - size.scale_factor(), ) }; @@ -313,7 +312,6 @@ pub trait Application: Sized { &surface, physical_size.width, physical_size.height, - size.scale_factor(), ); resized = false; @@ -323,6 +321,7 @@ pub trait Application: Sized { &mut renderer, &mut swap_chain, &primitive, + size.scale_factor(), &debug.overlay(), ); From 9a73c3a88d92262b4e59c1f061b1c56e533e2b0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Sun, 9 Feb 2020 03:44:16 +0100 Subject: [PATCH 04/12] Write documentation for new `iced_wgpu` types --- wgpu/src/lib.rs | 2 +- wgpu/src/target.rs | 6 ++++++ wgpu/src/window.rs | 1 + wgpu/src/window/backend.rs | 1 + wgpu/src/window/swap_chain.rs | 7 +++++++ 5 files changed, 16 insertions(+), 1 deletion(-) diff --git a/wgpu/src/lib.rs b/wgpu/src/lib.rs index 9c1739b234..b38da06062 100644 --- a/wgpu/src/lib.rs +++ b/wgpu/src/lib.rs @@ -19,7 +19,7 @@ //! [`wgpu`]: https://github.com/gfx-rs/wgpu-rs //! [WebGPU API]: https://gpuweb.github.io/gpuweb/ //! [`wgpu_glyph`]: https://github.com/hecrj/wgpu_glyph -//#![deny(missing_docs)] +#![deny(missing_docs)] #![deny(missing_debug_implementations)] #![deny(unused_results)] #![forbid(unsafe_code)] diff --git a/wgpu/src/target.rs b/wgpu/src/target.rs index 544e83d196..1e72c0c3fa 100644 --- a/wgpu/src/target.rs +++ b/wgpu/src/target.rs @@ -3,6 +3,12 @@ use crate::Viewport; /// A rendering target. #[derive(Debug)] pub struct Target<'a> { + /// The texture where graphics will be rendered. pub texture: &'a wgpu::TextureView, + + /// The viewport of the target. + /// + /// Most of the time, you will want this to match the dimensions of the + /// texture. pub viewport: &'a Viewport, } diff --git a/wgpu/src/window.rs b/wgpu/src/window.rs index 97cac9b7b6..b7adad82e7 100644 --- a/wgpu/src/window.rs +++ b/wgpu/src/window.rs @@ -1,3 +1,4 @@ +//! Display rendering results on windows. mod backend; mod swap_chain; diff --git a/wgpu/src/window/backend.rs b/wgpu/src/window/backend.rs index 0e4f02c94f..87779bea9a 100644 --- a/wgpu/src/window/backend.rs +++ b/wgpu/src/window/backend.rs @@ -3,6 +3,7 @@ use crate::{window::SwapChain, Renderer, Settings, Target}; use iced_native::MouseCursor; use raw_window_handle::HasRawWindowHandle; +/// A window graphics backend for iced powered by `wgpu`. #[derive(Debug)] pub struct Backend { device: wgpu::Device, diff --git a/wgpu/src/window/swap_chain.rs b/wgpu/src/window/swap_chain.rs index 3760e8a260..6f545fceac 100644 --- a/wgpu/src/window/swap_chain.rs +++ b/wgpu/src/window/swap_chain.rs @@ -12,6 +12,9 @@ pub struct SwapChain { impl SwapChain {} impl SwapChain { + /// Creates a new [`SwapChain`] for the given surface. + /// + /// [`SwapChain`]: struct.SwapChain.html pub fn new( device: &wgpu::Device, surface: &wgpu::Surface, @@ -24,6 +27,10 @@ impl SwapChain { } } + /// Returns the next frame of the [`SwapChain`] alongside its [`Viewport`]. + /// + /// [`SwapChain`]: struct.SwapChain.html + /// [`Viewport`]: ../struct.Viewport.html pub fn next_frame(&mut self) -> (wgpu::SwapChainOutput<'_>, &Viewport) { (self.raw.get_next_texture(), &self.viewport) } From 3efede26627121138316e8375a594979c92a9a43 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Sun, 9 Feb 2020 05:23:21 +0100 Subject: [PATCH 05/12] Add `conversion::window_event` to `iced_winit` --- winit/src/conversion.rs | 92 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 90 insertions(+), 2 deletions(-) diff --git a/winit/src/conversion.rs b/winit/src/conversion.rs index af0c4c9ff0..b6a0b64b4d 100644 --- a/winit/src/conversion.rs +++ b/winit/src/conversion.rs @@ -4,12 +4,90 @@ //! [`iced_native`]: https://github.com/hecrj/iced/tree/master/native use crate::{ input::{ - keyboard::{KeyCode, ModifiersState}, + keyboard::{self, KeyCode, ModifiersState}, mouse, ButtonState, }, - Mode, MouseCursor, + window, Event, Mode, MouseCursor, }; +/// Converts a winit window event into an iced event. +pub fn window_event( + event: winit::event::WindowEvent<'_>, + scale_factor: f64, + modifiers: winit::event::ModifiersState, +) -> Option { + use winit::event::WindowEvent; + + match event { + WindowEvent::Resized(new_size) => { + let logical_size = new_size.to_logical(scale_factor); + + Some(Event::Window(window::Event::Resized { + width: logical_size.width, + height: logical_size.height, + })) + } + WindowEvent::CursorMoved { position, .. } => { + let position = position.to_logical::(scale_factor); + + Some(Event::Mouse(mouse::Event::CursorMoved { + x: position.x as f32, + y: position.y as f32, + })) + } + WindowEvent::MouseInput { button, state, .. } => { + Some(Event::Mouse(mouse::Event::Input { + button: mouse_button(button), + state: button_state(state), + })) + } + WindowEvent::MouseWheel { delta, .. } => match delta { + winit::event::MouseScrollDelta::LineDelta(delta_x, delta_y) => { + Some(Event::Mouse(mouse::Event::WheelScrolled { + delta: mouse::ScrollDelta::Lines { + x: delta_x, + y: delta_y, + }, + })) + } + winit::event::MouseScrollDelta::PixelDelta(position) => { + Some(Event::Mouse(mouse::Event::WheelScrolled { + delta: mouse::ScrollDelta::Pixels { + x: position.x as f32, + y: position.y as f32, + }, + })) + } + }, + WindowEvent::ReceivedCharacter(c) if !is_private_use_character(c) => { + Some(Event::Keyboard(keyboard::Event::CharacterReceived(c))) + } + WindowEvent::KeyboardInput { + input: + winit::event::KeyboardInput { + virtual_keycode: Some(virtual_keycode), + state, + .. + }, + .. + } => Some(Event::Keyboard(keyboard::Event::Input { + key_code: key_code(virtual_keycode), + state: button_state(state), + modifiers: modifiers_state(modifiers), + })), + WindowEvent::HoveredFile(path) => { + Some(Event::Window(window::Event::FileHovered(path))) + } + WindowEvent::DroppedFile(path) => { + Some(Event::Window(window::Event::FileDropped(path))) + } + WindowEvent::HoveredFileCancelled => { + Some(Event::Window(window::Event::FilesHoveredLeft)) + } + _ => None, + } +} + /// Converts a [`Mode`] to a [`winit`] fullscreen mode. /// /// [`Mode`]: @@ -254,3 +332,13 @@ pub fn key_code(virtual_keycode: winit::event::VirtualKeyCode) -> KeyCode { winit::event::VirtualKeyCode::Cut => KeyCode::Cut, } } + +// As defined in: http://www.unicode.org/faq/private_use.html +pub(crate) fn is_private_use_character(c: char) -> bool { + match c { + '\u{E000}'..='\u{F8FF}' + | '\u{F0000}'..='\u{FFFFD}' + | '\u{100000}'..='\u{10FFFD}' => true, + _ => false, + } +} From ce6806bbf4fce198453e13da7cb38d62fd1739f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Sun, 9 Feb 2020 05:24:54 +0100 Subject: [PATCH 06/12] Remove event conversion from `iced_winit::Application` --- native/src/user_interface.rs | 4 +- winit/src/application.rs | 145 ++++++++--------------------------- 2 files changed, 34 insertions(+), 115 deletions(-) diff --git a/native/src/user_interface.rs b/native/src/user_interface.rs index 53b36240be..376cd2f48a 100644 --- a/native/src/user_interface.rs +++ b/native/src/user_interface.rs @@ -174,9 +174,9 @@ where /// ``` pub fn update( &mut self, - renderer: &Renderer, - clipboard: Option<&dyn Clipboard>, events: impl IntoIterator, + clipboard: Option<&dyn Clipboard>, + renderer: &Renderer, ) -> Vec { let mut messages = Vec::new(); diff --git a/winit/src/application.rs b/winit/src/application.rs index 22d1afabec..35a36434c1 100644 --- a/winit/src/application.rs +++ b/winit/src/application.rs @@ -1,8 +1,7 @@ use crate::{ - conversion, - input::{keyboard, mouse}, - window, Cache, Clipboard, Command, Debug, Element, Event, Executor, Mode, - MouseCursor, Proxy, Runtime, Settings, Size, Subscription, UserInterface, + conversion, size::Size, window, Cache, Clipboard, Command, Debug, Element, + Executor, Mode, MouseCursor, Proxy, Runtime, Settings, Subscription, + UserInterface, }; /// An interactive, native cross-platform application. @@ -200,8 +199,7 @@ pub trait Application: Sized { event_loop.run(move |event, _, control_flow| match event { event::Event::MainEventsCleared => { - if events.is_empty() && external_messages.is_empty() && !resized - { + if events.is_empty() && external_messages.is_empty() { return; } @@ -225,11 +223,11 @@ pub trait Application: Sized { .for_each(|event| runtime.broadcast(event)); let mut messages = user_interface.update( - &renderer, + events.drain(..), clipboard .as_ref() .map(|c| c as &dyn iced_native::Clipboard), - events.drain(..), + &renderer, ); messages.extend(external_messages.drain(..)); debug.event_processing_finished(); @@ -341,106 +339,37 @@ pub trait Application: Sized { event::Event::WindowEvent { event: window_event, .. - } => match window_event { - WindowEvent::Resized(new_size) => { - size = Size::new(new_size, size.scale_factor()); - - events.push(Event::Window(window::Event::Resized { - width: size.logical().width.round() as u32, - height: size.logical().height.round() as u32, - })); - - resized = true; - } - WindowEvent::CloseRequested => { - *control_flow = ControlFlow::Exit; - } - WindowEvent::CursorMoved { position, .. } => { - let position = - position.to_logical::(size.scale_factor()); - - events.push(Event::Mouse(mouse::Event::CursorMoved { - x: position.x as f32, - y: position.y as f32, - })); - } - WindowEvent::MouseInput { button, state, .. } => { - events.push(Event::Mouse(mouse::Event::Input { - button: conversion::mouse_button(button), - state: conversion::button_state(state), - })); - } - WindowEvent::MouseWheel { delta, .. } => match delta { - winit::event::MouseScrollDelta::LineDelta( - delta_x, - delta_y, - ) => { - events.push(Event::Mouse( - mouse::Event::WheelScrolled { - delta: mouse::ScrollDelta::Lines { - x: delta_x, - y: delta_y, - }, - }, - )); + } => { + match window_event { + WindowEvent::Resized(new_size) => { + size = Size::new(new_size, window.scale_factor()); + resized = true; } - winit::event::MouseScrollDelta::PixelDelta(position) => { - events.push(Event::Mouse( - mouse::Event::WheelScrolled { - delta: mouse::ScrollDelta::Pixels { - x: position.x as f32, - y: position.y as f32, - }, - }, - )); + WindowEvent::CloseRequested => { + *control_flow = ControlFlow::Exit; } - }, - WindowEvent::ReceivedCharacter(c) - if !is_private_use_character(c) => - { - events.push(Event::Keyboard( - keyboard::Event::CharacterReceived(c), - )); + #[cfg(feature = "debug")] + WindowEvent::KeyboardInput { + input: + winit::event::KeyboardInput { + virtual_keycode: + Some(winit::event::VirtualKeyCode::F12), + state: winit::event::ElementState::Pressed, + .. + }, + .. + } => debug.toggle(), + _ => {} } - WindowEvent::KeyboardInput { - input: - winit::event::KeyboardInput { - virtual_keycode: Some(virtual_keycode), - state, - .. - }, - .. - } => { - match (virtual_keycode, state) { - ( - winit::event::VirtualKeyCode::F12, - winit::event::ElementState::Pressed, - ) => debug.toggle(), - _ => {} - } - events.push(Event::Keyboard(keyboard::Event::Input { - key_code: conversion::key_code(virtual_keycode), - state: conversion::button_state(state), - modifiers: conversion::modifiers_state(modifiers), - })); - } - WindowEvent::HoveredFile(path) => { - events - .push(Event::Window(window::Event::FileHovered(path))); - } - WindowEvent::DroppedFile(path) => { - events - .push(Event::Window(window::Event::FileDropped(path))); - } - WindowEvent::HoveredFileCancelled => { - events.push(Event::Window(window::Event::FilesHoveredLeft)); - } - WindowEvent::ScaleFactorChanged { scale_factor, .. } => { - size = Size::new(size.physical(), scale_factor); + if let Some(event) = conversion::window_event( + window_event, + size.scale_factor(), + modifiers, + ) { + events.push(event); } - _ => {} - }, + } event::Event::DeviceEvent { event: event::DeviceEvent::ModifiersChanged(new_modifiers), .. @@ -479,13 +408,3 @@ fn build_user_interface<'a, A: Application>( user_interface } - -// As defined in: http://www.unicode.org/faq/private_use.html -fn is_private_use_character(c: char) -> bool { - match c { - '\u{E000}'..='\u{F8FF}' - | '\u{F0000}'..='\u{FFFFD}' - | '\u{100000}'..='\u{10FFFD}' => true, - _ => false, - } -} From fbfd76023b3756a39dbff75019c346c594c49cba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Sun, 9 Feb 2020 05:26:20 +0100 Subject: [PATCH 07/12] Remove dead code in `iced_winit::debug::Null` --- winit/src/debug/null.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/winit/src/debug/null.rs b/winit/src/debug/null.rs index 9c809dd49a..2a9430cdc1 100644 --- a/winit/src/debug/null.rs +++ b/winit/src/debug/null.rs @@ -6,8 +6,6 @@ impl Debug { Self } - pub fn toggle(&mut self) {} - pub fn startup_started(&mut self) {} pub fn startup_finished(&mut self) {} From 5f89fab9d7575900b02e70f6d64cb3d8fbd45753 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Sun, 9 Feb 2020 05:31:42 +0100 Subject: [PATCH 08/12] Improve consistency in new `iced_wgpu` types --- wgpu/src/lib.rs | 2 ++ wgpu/src/renderer.rs | 2 +- wgpu/src/window/backend.rs | 2 +- 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/wgpu/src/lib.rs b/wgpu/src/lib.rs index b38da06062..e4834818e7 100644 --- a/wgpu/src/lib.rs +++ b/wgpu/src/lib.rs @@ -39,6 +39,8 @@ mod text; mod transformation; mod viewport; +pub use wgpu; + pub use defaults::Defaults; pub use primitive::Primitive; pub use renderer::Renderer; diff --git a/wgpu/src/renderer.rs b/wgpu/src/renderer.rs index 904e3c6564..e93090b802 100644 --- a/wgpu/src/renderer.rs +++ b/wgpu/src/renderer.rs @@ -47,7 +47,7 @@ impl Renderer { /// Creates a new [`Renderer`]. /// /// [`Renderer`]: struct.Renderer.html - pub fn new(settings: Settings, device: &mut wgpu::Device) -> Self { + 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); diff --git a/wgpu/src/window/backend.rs b/wgpu/src/window/backend.rs index 87779bea9a..6f8a0bb0e5 100644 --- a/wgpu/src/window/backend.rs +++ b/wgpu/src/window/backend.rs @@ -31,7 +31,7 @@ impl iced_native::window::Backend for Backend { limits: wgpu::Limits { max_bind_groups: 2 }, }); - let renderer = Renderer::new(settings, &mut device); + let renderer = Renderer::new(&mut device, settings); (Backend { device, queue }, renderer) } From a244f9324305a2393da95be76085cba6a29b4b27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Sun, 9 Feb 2020 05:31:55 +0100 Subject: [PATCH 09/12] Avoid `Size` name collision in`iced_winit` --- winit/src/lib.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/winit/src/lib.rs b/winit/src/lib.rs index 225907a47f..f99e12903e 100644 --- a/winit/src/lib.rs +++ b/winit/src/lib.rs @@ -50,4 +50,3 @@ pub use settings::Settings; use debug::Debug; use proxy::Proxy; -use size::Size; From 4d7979aa77e481a5f8ff2f22bc2665912617fc04 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Sun, 9 Feb 2020 05:32:56 +0100 Subject: [PATCH 10/12] Add `integration` example It showcases how to integrate iced in an existing graphical application. --- Cargo.toml | 1 + examples/integration/Cargo.toml | 11 ++ examples/integration/src/controls.rs | 102 ++++++++++++ examples/integration/src/main.rs | 204 +++++++++++++++++++++++ examples/integration/src/scene.rs | 119 +++++++++++++ examples/integration/src/shader/frag.spv | Bin 0 -> 352 bytes examples/integration/src/shader/vert.spv | Bin 0 -> 904 bytes 7 files changed, 437 insertions(+) create mode 100644 examples/integration/Cargo.toml create mode 100644 examples/integration/src/controls.rs create mode 100644 examples/integration/src/main.rs create mode 100644 examples/integration/src/scene.rs create mode 100644 examples/integration/src/shader/frag.spv create mode 100644 examples/integration/src/shader/vert.spv diff --git a/Cargo.toml b/Cargo.toml index 39c5957a02..11cca8b38a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -40,6 +40,7 @@ members = [ "examples/custom_widget", "examples/events", "examples/geometry", + "examples/integration", "examples/pokedex", "examples/progress_bar", "examples/stopwatch", diff --git a/examples/integration/Cargo.toml b/examples/integration/Cargo.toml new file mode 100644 index 0000000000..afc2c791dc --- /dev/null +++ b/examples/integration/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "integration" +version = "0.1.0" +authors = ["Héctor Ramón Jiménez "] +edition = "2018" +publish = false + +[dependencies] +iced_winit = { path = "../../winit" } +iced_wgpu = { path = "../../wgpu" } +env_logger = "0.7" diff --git a/examples/integration/src/controls.rs b/examples/integration/src/controls.rs new file mode 100644 index 0000000000..0457a058ce --- /dev/null +++ b/examples/integration/src/controls.rs @@ -0,0 +1,102 @@ +use crate::Scene; + +use iced_wgpu::Renderer; +use iced_winit::{ + slider, Align, Color, Column, Element, Length, Row, Slider, Text, +}; + +pub struct Controls { + sliders: [slider::State; 3], +} + +#[derive(Debug)] +pub enum Message { + BackgroundColorChanged(Color), +} + +impl Controls { + pub fn new() -> Controls { + Controls { + sliders: Default::default(), + } + } + + pub fn update(&self, message: Message, scene: &mut Scene) { + match message { + Message::BackgroundColorChanged(color) => { + scene.background_color = color; + } + } + } + + pub fn view<'a>( + &'a mut self, + scene: &Scene, + ) -> Element<'a, Message, Renderer> { + let [r, g, b] = &mut self.sliders; + let background_color = scene.background_color; + + let sliders = Row::new() + .width(Length::Units(500)) + .spacing(20) + .push(Slider::new( + r, + 0.0..=1.0, + scene.background_color.r, + move |r| { + Message::BackgroundColorChanged(Color { + r, + ..background_color + }) + }, + )) + .push(Slider::new( + g, + 0.0..=1.0, + scene.background_color.g, + move |g| { + Message::BackgroundColorChanged(Color { + g, + ..background_color + }) + }, + )) + .push(Slider::new( + b, + 0.0..=1.0, + scene.background_color.b, + move |b| { + Message::BackgroundColorChanged(Color { + b, + ..background_color + }) + }, + )); + + Row::new() + .width(Length::Fill) + .height(Length::Fill) + .align_items(Align::End) + .push( + Column::new() + .width(Length::Fill) + .align_items(Align::End) + .push( + Column::new() + .padding(10) + .spacing(10) + .push( + Text::new("Background color") + .color(Color::WHITE), + ) + .push(sliders) + .push( + Text::new(format!("{:?}", background_color)) + .size(14) + .color(Color::WHITE), + ), + ), + ) + .into() + } +} diff --git a/examples/integration/src/main.rs b/examples/integration/src/main.rs new file mode 100644 index 0000000000..ed36f73624 --- /dev/null +++ b/examples/integration/src/main.rs @@ -0,0 +1,204 @@ +mod controls; +mod scene; + +use controls::Controls; +use scene::Scene; + +use iced_wgpu::{ + wgpu, window::SwapChain, Primitive, Renderer, Settings, Target, +}; +use iced_winit::{winit, Cache, Clipboard, MouseCursor, Size, UserInterface}; + +use winit::{ + event::{DeviceEvent, Event, ModifiersState, WindowEvent}, + event_loop::{ControlFlow, EventLoop}, +}; + +pub fn main() { + env_logger::init(); + + // Initialize winit + let event_loop = EventLoop::new(); + let window = winit::window::Window::new(&event_loop).unwrap(); + let mut logical_size = + window.inner_size().to_logical(window.scale_factor()); + let mut modifiers = ModifiersState::default(); + + // Initialize WGPU + let adapter = wgpu::Adapter::request(&wgpu::RequestAdapterOptions { + power_preference: wgpu::PowerPreference::Default, + backends: wgpu::BackendBit::PRIMARY, + }) + .expect("Request adapter"); + + let (mut device, mut queue) = + adapter.request_device(&wgpu::DeviceDescriptor { + extensions: wgpu::Extensions { + anisotropic_filtering: false, + }, + limits: wgpu::Limits::default(), + }); + + let surface = wgpu::Surface::create(&window); + + let mut swap_chain = { + let size = window.inner_size(); + + SwapChain::new(&device, &surface, size.width, size.height) + }; + let mut resized = false; + + // Initialize iced + let mut events = Vec::new(); + let mut cache = Some(Cache::default()); + let mut renderer = Renderer::new(&mut device, Settings::default()); + let mut output = (Primitive::None, MouseCursor::OutOfBounds); + let clipboard = Clipboard::new(&window); + + // Initialize scene and GUI controls + let mut scene = Scene::new(&device); + let mut controls = Controls::new(); + + // Run event loop + event_loop.run(move |event, _, control_flow| { + // You should change this if you want to render continuosly + *control_flow = ControlFlow::Wait; + + match event { + Event::DeviceEvent { + event: DeviceEvent::ModifiersChanged(new_modifiers), + .. + } => { + modifiers = new_modifiers; + } + Event::WindowEvent { event, .. } => { + match event { + WindowEvent::Resized(new_size) => { + logical_size = + new_size.to_logical(window.scale_factor()); + resized = true; + } + WindowEvent::CloseRequested => { + *control_flow = ControlFlow::Exit; + } + _ => {} + } + + // Map window event to iced event + if let Some(event) = iced_winit::conversion::window_event( + event, + window.scale_factor(), + modifiers, + ) { + events.push(event); + } + } + Event::MainEventsCleared => { + // If no relevant events happened, we can simply skip this + if events.is_empty() { + return; + } + + // We need to: + // 1. Process events of our user interface. + // 2. Update state as a result of any interaction. + // 3. Generate a new output for our renderer. + + // First, we build our user interface. + let mut user_interface = UserInterface::build( + controls.view(&scene), + Size::new(logical_size.width, logical_size.height), + cache.take().unwrap(), + &mut renderer, + ); + + // Then, we process the events, obtaining messages in return. + let messages = user_interface.update( + events.drain(..), + clipboard.as_ref().map(|c| c as _), + &renderer, + ); + + let user_interface = if messages.is_empty() { + // If there are no messages, no interactions we care about have + // happened. We can simply leave our user interface as it is. + user_interface + } else { + // If there are messages, we need to update our state + // accordingly and rebuild our user interface. + // We can only do this if we drop our user interface first + // by turning it into its cache. + cache = Some(user_interface.into_cache()); + + // In this example, `Controls` is the only part that cares + // about messages, so updating our state is pretty + // straightforward. + for message in messages { + controls.update(message, &mut scene); + } + + // Once the state has been changed, we rebuild our updated + // user interface. + UserInterface::build( + controls.view(&scene), + Size::new(logical_size.width, logical_size.height), + cache.take().unwrap(), + &mut renderer, + ) + }; + + // Finally, we just need to draw a new output for our renderer, + output = user_interface.draw(&mut renderer); + + // update our cache, + cache = Some(user_interface.into_cache()); + + // and request a redraw + window.request_redraw(); + } + Event::RedrawRequested(_) => { + if resized { + let size = window.inner_size(); + + swap_chain = SwapChain::new( + &device, + &surface, + size.width, + size.height, + ); + } + + let (frame, viewport) = swap_chain.next_frame(); + + let mut encoder = device.create_command_encoder( + &wgpu::CommandEncoderDescriptor { todo: 0 }, + ); + + // We draw the scene first + scene.draw(&mut encoder, &frame.view); + + // And then iced on top + let mouse_cursor = renderer.draw( + &mut device, + &mut encoder, + Target { + texture: &frame.view, + viewport, + }, + &output, + window.scale_factor(), + &["Some debug information!"], + ); + + // Then we submit the work + queue.submit(&[encoder.finish()]); + + // And update the mouse cursor + window.set_cursor_icon(iced_winit::conversion::mouse_cursor( + mouse_cursor, + )); + } + _ => {} + } + }) +} diff --git a/examples/integration/src/scene.rs b/examples/integration/src/scene.rs new file mode 100644 index 0000000000..efb1921b11 --- /dev/null +++ b/examples/integration/src/scene.rs @@ -0,0 +1,119 @@ +use iced_wgpu::wgpu; +use iced_winit::Color; + +pub struct Scene { + pub background_color: Color, + pipeline: wgpu::RenderPipeline, + bind_group: wgpu::BindGroup, +} + +impl Scene { + pub fn new(device: &wgpu::Device) -> Scene { + let (pipeline, bind_group) = build_pipeline(device); + + Scene { + background_color: Color::BLACK, + pipeline, + bind_group, + } + } + + pub fn draw( + &self, + encoder: &mut wgpu::CommandEncoder, + target: &wgpu::TextureView, + ) { + let mut rpass = + encoder.begin_render_pass(&wgpu::RenderPassDescriptor { + color_attachments: &[ + wgpu::RenderPassColorAttachmentDescriptor { + attachment: target, + resolve_target: None, + load_op: wgpu::LoadOp::Clear, + store_op: wgpu::StoreOp::Store, + clear_color: { + let [r, g, b, a] = + self.background_color.into_linear(); + + wgpu::Color { + r: r as f64, + g: g as f64, + b: b as f64, + a: a as f64, + } + }, + }, + ], + depth_stencil_attachment: None, + }); + + rpass.set_pipeline(&self.pipeline); + rpass.set_bind_group(0, &self.bind_group, &[]); + rpass.draw(0..3, 0..1); + } +} + +fn build_pipeline( + device: &wgpu::Device, +) -> (wgpu::RenderPipeline, wgpu::BindGroup) { + let vs = include_bytes!("shader/vert.spv"); + let fs = include_bytes!("shader/frag.spv"); + + let vs_module = device.create_shader_module( + &wgpu::read_spirv(std::io::Cursor::new(&vs[..])).unwrap(), + ); + + let fs_module = device.create_shader_module( + &wgpu::read_spirv(std::io::Cursor::new(&fs[..])).unwrap(), + ); + + let bind_group_layout = + device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { + bindings: &[], + }); + + let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor { + layout: &bind_group_layout, + bindings: &[], + }); + + let pipeline_layout = + device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { + bind_group_layouts: &[&bind_group_layout], + }); + + let pipeline = + device.create_render_pipeline(&wgpu::RenderPipelineDescriptor { + layout: &pipeline_layout, + vertex_stage: wgpu::ProgrammableStageDescriptor { + module: &vs_module, + entry_point: "main", + }, + fragment_stage: Some(wgpu::ProgrammableStageDescriptor { + module: &fs_module, + entry_point: "main", + }), + rasterization_state: Some(wgpu::RasterizationStateDescriptor { + front_face: wgpu::FrontFace::Ccw, + cull_mode: wgpu::CullMode::None, + depth_bias: 0, + depth_bias_slope_scale: 0.0, + depth_bias_clamp: 0.0, + }), + primitive_topology: wgpu::PrimitiveTopology::TriangleList, + color_states: &[wgpu::ColorStateDescriptor { + format: wgpu::TextureFormat::Bgra8UnormSrgb, + color_blend: wgpu::BlendDescriptor::REPLACE, + alpha_blend: wgpu::BlendDescriptor::REPLACE, + write_mask: wgpu::ColorWrite::ALL, + }], + depth_stencil_state: None, + index_format: wgpu::IndexFormat::Uint16, + vertex_buffers: &[], + sample_count: 1, + sample_mask: !0, + alpha_to_coverage_enabled: false, + }); + + (pipeline, bind_group) +} diff --git a/examples/integration/src/shader/frag.spv b/examples/integration/src/shader/frag.spv new file mode 100644 index 0000000000000000000000000000000000000000..9d6807c91430f6ae007915b29659cb2b46d46580 GIT binary patch literal 352 zcmYk2yNbeK6ohBZ##`1Kf_CC!=^_>?f}n+kjh#;**a&Jsv%SyeQ`rc9^XtN%Fga%? zb1o#x^$OC5iaZs)qM;m$lqetaYQ0$xF4=A{8V~KLDhft>YIr`$#&Nqp`9lfR@mOOx zRm4Q;MnjDH-y5UYpZ;dc^pKt@wZdVoV?s3PcK>&nq)@l0^Jrbr#_v7mp55hLB>PJ1 yvp4rlYkj!AWguM_e>@3X`k~8xU%(#ghU*FU`WEkTc-|c)CeLBs;l2ldZVH~`)e-Cf literal 0 HcmV?d00001 diff --git a/examples/integration/src/shader/vert.spv b/examples/integration/src/shader/vert.spv new file mode 100644 index 0000000000000000000000000000000000000000..0cabc9c038cf708ce9de14e58428d8dd888be5d7 GIT binary patch literal 904 zcmYk4OD}^_5QdM|SNBWZFU3c(K*R!(2x3(di;BcnNE>NVZ9*58{+YzDvXOY6?;_5Z zH#76jJ9FktTP#!uLg)*FeA^*C}75TOvB2rC)1~zIs6h_4Cc}M zVq@R4n)Q;4VTQ17$Nr~T>^|gXiM=NVtR(Da@hHB>-eXvMJA?XhYz#c=jVFNpD#Vjm zYvjyZTLzvyo=U^dJ$9jX8mN)aH8c2}i(E5HASZHe4&R!TDY~IrGa{_vP%yD_QJp`8+Xx2p<>bH}QP0HOIWc-MD*?{5MR5^)7q+A5eQn> Date: Sun, 9 Feb 2020 05:57:15 +0100 Subject: [PATCH 11/12] Fix documentation examples of `UserInterface` --- native/src/user_interface.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/native/src/user_interface.rs b/native/src/user_interface.rs index 376cd2f48a..08914bed37 100644 --- a/native/src/user_interface.rs +++ b/native/src/user_interface.rs @@ -162,7 +162,7 @@ where /// ); /// /// // Update the user interface - /// let messages = user_interface.update(&renderer, None, events.drain(..)); + /// let messages = user_interface.update(events.drain(..), None, &renderer); /// /// cache = user_interface.into_cache(); /// @@ -246,7 +246,7 @@ where /// &mut renderer, /// ); /// - /// let messages = user_interface.update(&renderer, None, events.drain(..)); + /// let messages = user_interface.update(events.drain(..), None, &renderer); /// /// // Draw the user interface /// let mouse_cursor = user_interface.draw(&mut renderer); From 4337daddb2a02a2c60dfc5beb896e3059588312a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Mon, 10 Feb 2020 19:38:22 +0100 Subject: [PATCH 12/12] Add `integration` example to examples `README` --- examples/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/examples/README.md b/examples/README.md index 8f2eba3ea6..a9ab546f40 100644 --- a/examples/README.md +++ b/examples/README.md @@ -74,6 +74,7 @@ A bunch of simpler examples exist: - [`custom_widget`](custom_widget), a demonstration of how to build a custom widget that draws a circle. - [`events`](events), a log of native events displayed using a conditional `Subscription`. - [`geometry`](geometry), a custom widget showcasing how to draw geometry with the `Mesh2D` primitive in [`iced_wgpu`](../wgpu). +- [`integration`](integration), a demonstration of how to integrate Iced in an existing graphical application. - [`pokedex`](pokedex), an application that displays a random Pokédex entry (sprite included!) by using the [PokéAPI]. - [`progress_bar`](progress_bar), a simple progress bar that can be filled by using a slider. - [`stopwatch`](stopwatch), a watch with start/stop and reset buttons showcasing how to listen to time.