From b9a9576207ddfc7afd89da30b7cfc7ca0d7e335c Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 6 Jan 2023 23:29:38 +0100 Subject: Remove `iced_glow`, `glyph-brush`, and `wgpu_glyph` dependencies --- examples/integration/.gitignore | 2 + examples/integration/Cargo.toml | 22 +++ examples/integration/README.md | 36 ++++ examples/integration/index.html | 21 +++ examples/integration/src/controls.rs | 112 ++++++++++++ examples/integration/src/main.rs | 289 ++++++++++++++++++++++++++++++ examples/integration/src/scene.rs | 102 +++++++++++ examples/integration/src/shader/frag.wgsl | 4 + examples/integration/src/shader/vert.wgsl | 6 + 9 files changed, 594 insertions(+) create mode 100644 examples/integration/.gitignore create mode 100644 examples/integration/Cargo.toml create mode 100644 examples/integration/README.md create mode 100644 examples/integration/index.html 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.wgsl create mode 100644 examples/integration/src/shader/vert.wgsl (limited to 'examples/integration') diff --git a/examples/integration/.gitignore b/examples/integration/.gitignore new file mode 100644 index 00000000..e188dc28 --- /dev/null +++ b/examples/integration/.gitignore @@ -0,0 +1,2 @@ +*.wasm +*.js diff --git a/examples/integration/Cargo.toml b/examples/integration/Cargo.toml new file mode 100644 index 00000000..200306aa --- /dev/null +++ b/examples/integration/Cargo.toml @@ -0,0 +1,22 @@ +[package] +name = "integration" +version = "0.1.0" +authors = ["Héctor Ramón Jiménez "] +edition = "2021" +publish = false + +[dependencies] +iced_winit = { path = "../../winit" } +iced_wgpu = { path = "../../wgpu", features = ["webgl"] } +env_logger = "0.8" + +[target.'cfg(target_arch = "wasm32")'.dependencies] +console_error_panic_hook = "0.1.7" +console_log = "0.2.0" +log = "0.4" +wasm-bindgen = "0.2" +web-sys = { version = "0.3", features = ["Element", "HtmlCanvasElement", "Window", "Document"] } +# This dependency a little bit quirky, it is deep in the tree and without `js` feature it +# refuses to work with `wasm32-unknown-unknown target`. Unfortunately, we need this patch +# to make it work +getrandom = { version = "0.2", features = ["js"] } diff --git a/examples/integration/README.md b/examples/integration/README.md new file mode 100644 index 00000000..ece9ba1e --- /dev/null +++ b/examples/integration/README.md @@ -0,0 +1,36 @@ +## `wgpu` integration + +A demonstration of how to integrate Iced in an existing [`wgpu`] application. + +The __[`main`]__ file contains all the code of the example. + +
+ + + +
+ +You can run it with `cargo run`: +``` +cargo run --package integration_wgpu +``` + +### How to run this example with WebGL backend +NOTE: Currently, WebGL backend is is still experimental, so expect bugs. + +```sh +# 0. Install prerequisites +cargo install wasm-bindgen-cli https +# 1. cd to the current folder +# 2. Compile wasm module +cargo build -p integration_wgpu --target wasm32-unknown-unknown +# 3. Invoke wasm-bindgen +wasm-bindgen ../../target/wasm32-unknown-unknown/debug/integration_wgpu.wasm --out-dir . --target web --no-typescript +# 4. run http server +http +# 5. Open 127.0.0.1:8000 in browser +``` + + +[`main`]: src/main.rs +[`wgpu`]: https://github.com/gfx-rs/wgpu diff --git a/examples/integration/index.html b/examples/integration/index.html new file mode 100644 index 00000000..920bc4a0 --- /dev/null +++ b/examples/integration/index.html @@ -0,0 +1,21 @@ + + + + + Iced - wgpu + WebGL integration + + +

integration_wgpu

+ + + + + diff --git a/examples/integration/src/controls.rs b/examples/integration/src/controls.rs new file mode 100644 index 00000000..533cb6e2 --- /dev/null +++ b/examples/integration/src/controls.rs @@ -0,0 +1,112 @@ +use iced_wgpu::Renderer; +use iced_winit::widget::{slider, text_input, Column, Row, Text}; +use iced_winit::{Alignment, Color, Command, Element, Length, Program}; + +pub struct Controls { + background_color: Color, + text: String, +} + +#[derive(Debug, Clone)] +pub enum Message { + BackgroundColorChanged(Color), + TextChanged(String), +} + +impl Controls { + pub fn new() -> Controls { + Controls { + background_color: Color::BLACK, + text: Default::default(), + } + } + + pub fn background_color(&self) -> Color { + self.background_color + } +} + +impl Program for Controls { + type Renderer = Renderer; + type Message = Message; + + fn update(&mut self, message: Message) -> Command { + match message { + Message::BackgroundColorChanged(color) => { + self.background_color = color; + } + Message::TextChanged(text) => { + self.text = text; + } + } + + Command::none() + } + + fn view(&self) -> Element { + let background_color = self.background_color; + let text = &self.text; + + let sliders = Row::new() + .width(500) + .spacing(20) + .push( + slider(0.0..=1.0, background_color.r, move |r| { + Message::BackgroundColorChanged(Color { + r, + ..background_color + }) + }) + .step(0.01), + ) + .push( + slider(0.0..=1.0, background_color.g, move |g| { + Message::BackgroundColorChanged(Color { + g, + ..background_color + }) + }) + .step(0.01), + ) + .push( + slider(0.0..=1.0, background_color.b, move |b| { + Message::BackgroundColorChanged(Color { + b, + ..background_color + }) + }) + .step(0.01), + ); + + Row::new() + .width(Length::Fill) + .height(Length::Fill) + .align_items(Alignment::End) + .push( + Column::new() + .width(Length::Fill) + .align_items(Alignment::End) + .push( + Column::new() + .padding(10) + .spacing(10) + .push( + Text::new("Background color") + .style(Color::WHITE), + ) + .push(sliders) + .push( + Text::new(format!("{background_color:?}")) + .size(14) + .style(Color::WHITE), + ) + .push(text_input( + "Placeholder", + text, + Message::TextChanged, + )), + ), + ) + .into() + } +} diff --git a/examples/integration/src/main.rs b/examples/integration/src/main.rs new file mode 100644 index 00000000..2a56b6fa --- /dev/null +++ b/examples/integration/src/main.rs @@ -0,0 +1,289 @@ +mod controls; +mod scene; + +use controls::Controls; +use scene::Scene; + +use iced_wgpu::{wgpu, Backend, Renderer, Settings, Viewport}; +use iced_winit::{ + conversion, futures, program, renderer, winit, Clipboard, Color, Debug, + Size, +}; + +use winit::{ + dpi::PhysicalPosition, + event::{Event, ModifiersState, WindowEvent}, + event_loop::{ControlFlow, EventLoop}, +}; + +#[cfg(target_arch = "wasm32")] +use wasm_bindgen::JsCast; +#[cfg(target_arch = "wasm32")] +use web_sys::HtmlCanvasElement; +#[cfg(target_arch = "wasm32")] +use winit::platform::web::WindowBuilderExtWebSys; + +pub fn main() { + #[cfg(target_arch = "wasm32")] + let canvas_element = { + console_log::init_with_level(log::Level::Debug) + .expect("could not initialize logger"); + std::panic::set_hook(Box::new(console_error_panic_hook::hook)); + + web_sys::window() + .and_then(|win| win.document()) + .and_then(|doc| doc.get_element_by_id("iced_canvas")) + .and_then(|element| element.dyn_into::().ok()) + .expect("Canvas with id `iced_canvas` is missing") + }; + #[cfg(not(target_arch = "wasm32"))] + env_logger::init(); + + // Initialize winit + let event_loop = EventLoop::new(); + + #[cfg(target_arch = "wasm32")] + let window = winit::window::WindowBuilder::new() + .with_canvas(Some(canvas_element)) + .build(&event_loop) + .expect("Failed to build winit window"); + + #[cfg(not(target_arch = "wasm32"))] + let window = winit::window::Window::new(&event_loop).unwrap(); + + let physical_size = window.inner_size(); + let mut viewport = Viewport::with_physical_size( + Size::new(physical_size.width, physical_size.height), + window.scale_factor(), + ); + let mut cursor_position = PhysicalPosition::new(-1.0, -1.0); + let mut modifiers = ModifiersState::default(); + let mut clipboard = Clipboard::connect(&window); + + // Initialize wgpu + + #[cfg(target_arch = "wasm32")] + let default_backend = wgpu::Backends::GL; + #[cfg(not(target_arch = "wasm32"))] + let default_backend = wgpu::Backends::PRIMARY; + + let backend = + wgpu::util::backend_bits_from_env().unwrap_or(default_backend); + + let instance = wgpu::Instance::new(backend); + let surface = unsafe { instance.create_surface(&window) }; + + let (format, (device, queue)) = futures::executor::block_on(async { + let adapter = wgpu::util::initialize_adapter_from_env_or_default( + &instance, + backend, + Some(&surface), + ) + .await + .expect("No suitable GPU adapters found on the system!"); + + let adapter_features = adapter.features(); + + #[cfg(target_arch = "wasm32")] + let needed_limits = wgpu::Limits::downlevel_webgl2_defaults() + .using_resolution(adapter.limits()); + + #[cfg(not(target_arch = "wasm32"))] + let needed_limits = wgpu::Limits::default(); + + ( + surface + .get_supported_formats(&adapter) + .first() + .copied() + .expect("Get preferred format"), + adapter + .request_device( + &wgpu::DeviceDescriptor { + label: None, + features: adapter_features & wgpu::Features::default(), + limits: needed_limits, + }, + None, + ) + .await + .expect("Request device"), + ) + }); + + surface.configure( + &device, + &wgpu::SurfaceConfiguration { + usage: wgpu::TextureUsages::RENDER_ATTACHMENT, + format, + width: physical_size.width, + height: physical_size.height, + present_mode: wgpu::PresentMode::AutoVsync, + alpha_mode: wgpu::CompositeAlphaMode::Auto, + }, + ); + + let mut resized = false; + + // Initialize staging belt + let mut staging_belt = wgpu::util::StagingBelt::new(5 * 1024); + + // Initialize scene and GUI controls + let scene = Scene::new(&device, format); + let controls = Controls::new(); + + // Initialize iced + let mut debug = Debug::new(); + let mut renderer = + Renderer::new(Backend::new(&device, Settings::default(), format)); + + let mut state = program::State::new( + controls, + viewport.logical_size(), + &mut renderer, + &mut debug, + ); + + // 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::WindowEvent { event, .. } => { + match event { + WindowEvent::CursorMoved { position, .. } => { + cursor_position = position; + } + WindowEvent::ModifiersChanged(new_modifiers) => { + modifiers = new_modifiers; + } + WindowEvent::Resized(_) => { + 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, + ) { + state.queue_event(event); + } + } + Event::MainEventsCleared => { + // If there are events pending + if !state.is_queue_empty() { + // We update iced + let _ = state.update( + viewport.logical_size(), + conversion::cursor_position( + cursor_position, + viewport.scale_factor(), + ), + &mut renderer, + &iced_wgpu::Theme::Dark, + &renderer::Style { text_color: Color::WHITE }, + &mut clipboard, + &mut debug, + ); + + // and request a redraw + window.request_redraw(); + } + } + Event::RedrawRequested(_) => { + if resized { + let size = window.inner_size(); + + viewport = Viewport::with_physical_size( + Size::new(size.width, size.height), + window.scale_factor(), + ); + + surface.configure( + &device, + &wgpu::SurfaceConfiguration { + format, + usage: wgpu::TextureUsages::RENDER_ATTACHMENT, + width: size.width, + height: size.height, + present_mode: wgpu::PresentMode::AutoVsync, + alpha_mode: wgpu::CompositeAlphaMode::Auto + }, + ); + + resized = false; + } + + match surface.get_current_texture() { + Ok(frame) => { + let mut encoder = device.create_command_encoder( + &wgpu::CommandEncoderDescriptor { label: None }, + ); + + let program = state.program(); + + let view = frame.texture.create_view(&wgpu::TextureViewDescriptor::default()); + + { + // We clear the frame + let mut render_pass = scene.clear( + &view, + &mut encoder, + program.background_color(), + ); + + // Draw the scene + scene.draw(&mut render_pass); + } + + // And then iced on top + renderer.with_primitives(|backend, primitive| { + backend.present( + &device, + &mut staging_belt, + &mut encoder, + &view, + primitive, + &viewport, + &debug.overlay(), + ); + }); + + // Then we submit the work + staging_belt.finish(); + queue.submit(Some(encoder.finish())); + frame.present(); + + // Update the mouse cursor + window.set_cursor_icon( + iced_winit::conversion::mouse_interaction( + state.mouse_interaction(), + ), + ); + + // And recall staging buffers + staging_belt.recall(); + + } + Err(error) => match error { + wgpu::SurfaceError::OutOfMemory => { + panic!("Swapchain error: {error}. Rendering cannot continue.") + } + _ => { + // Try rendering again next frame. + window.request_redraw(); + } + }, + } + } + _ => {} + } + }) +} diff --git a/examples/integration/src/scene.rs b/examples/integration/src/scene.rs new file mode 100644 index 00000000..3e41fbda --- /dev/null +++ b/examples/integration/src/scene.rs @@ -0,0 +1,102 @@ +use iced_wgpu::wgpu; +use iced_winit::Color; + +pub struct Scene { + pipeline: wgpu::RenderPipeline, +} + +impl Scene { + pub fn new( + device: &wgpu::Device, + texture_format: wgpu::TextureFormat, + ) -> Scene { + let pipeline = build_pipeline(device, texture_format); + + Scene { pipeline } + } + + pub fn clear<'a>( + &self, + target: &'a wgpu::TextureView, + encoder: &'a mut wgpu::CommandEncoder, + background_color: Color, + ) -> wgpu::RenderPass<'a> { + encoder.begin_render_pass(&wgpu::RenderPassDescriptor { + label: None, + color_attachments: &[Some(wgpu::RenderPassColorAttachment { + view: target, + resolve_target: None, + ops: wgpu::Operations { + load: wgpu::LoadOp::Clear({ + let [r, g, b, a] = background_color.into_linear(); + + wgpu::Color { + r: r as f64, + g: g as f64, + b: b as f64, + a: a as f64, + } + }), + store: true, + }, + })], + depth_stencil_attachment: None, + }) + } + + pub fn draw<'a>(&'a self, render_pass: &mut wgpu::RenderPass<'a>) { + render_pass.set_pipeline(&self.pipeline); + render_pass.draw(0..3, 0..1); + } +} + +fn build_pipeline( + device: &wgpu::Device, + texture_format: wgpu::TextureFormat, +) -> wgpu::RenderPipeline { + let (vs_module, fs_module) = ( + device.create_shader_module(wgpu::include_wgsl!("shader/vert.wgsl")), + device.create_shader_module(wgpu::include_wgsl!("shader/frag.wgsl")), + ); + + let pipeline_layout = + device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { + label: None, + push_constant_ranges: &[], + bind_group_layouts: &[], + }); + + device.create_render_pipeline(&wgpu::RenderPipelineDescriptor { + label: None, + layout: Some(&pipeline_layout), + vertex: wgpu::VertexState { + module: &vs_module, + entry_point: "main", + buffers: &[], + }, + fragment: Some(wgpu::FragmentState { + module: &fs_module, + entry_point: "main", + targets: &[Some(wgpu::ColorTargetState { + format: texture_format, + blend: Some(wgpu::BlendState { + color: wgpu::BlendComponent::REPLACE, + alpha: wgpu::BlendComponent::REPLACE, + }), + write_mask: wgpu::ColorWrites::ALL, + })], + }), + primitive: wgpu::PrimitiveState { + topology: wgpu::PrimitiveTopology::TriangleList, + front_face: wgpu::FrontFace::Ccw, + ..Default::default() + }, + depth_stencil: None, + multisample: wgpu::MultisampleState { + count: 1, + mask: !0, + alpha_to_coverage_enabled: false, + }, + multiview: None, + }) +} diff --git a/examples/integration/src/shader/frag.wgsl b/examples/integration/src/shader/frag.wgsl new file mode 100644 index 00000000..cf27bb56 --- /dev/null +++ b/examples/integration/src/shader/frag.wgsl @@ -0,0 +1,4 @@ +@fragment +fn main() -> @location(0) vec4 { + return vec4(1.0, 0.0, 0.0, 1.0); +} diff --git a/examples/integration/src/shader/vert.wgsl b/examples/integration/src/shader/vert.wgsl new file mode 100644 index 00000000..e353e6ba --- /dev/null +++ b/examples/integration/src/shader/vert.wgsl @@ -0,0 +1,6 @@ +@vertex +fn main(@builtin(vertex_index) in_vertex_index: u32) -> @builtin(position) vec4 { + let x = f32(1 - i32(in_vertex_index)) * 0.5; + let y = f32(1 - i32(in_vertex_index & 1u) * 2) * 0.5; + return vec4(x, y, 0.0, 1.0); +} -- cgit From baf51a8fcffc78e4ca20f7dcbba18ca3655f2840 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 31 Jan 2023 06:29:21 +0100 Subject: Draft `glyphon` implementation of text pipeline for `iced_wgpu` --- examples/integration/src/main.rs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'examples/integration') diff --git a/examples/integration/src/main.rs b/examples/integration/src/main.rs index 2a56b6fa..69f6961e 100644 --- a/examples/integration/src/main.rs +++ b/examples/integration/src/main.rs @@ -134,8 +134,12 @@ pub fn main() { // Initialize iced let mut debug = Debug::new(); - let mut renderer = - Renderer::new(Backend::new(&device, Settings::default(), format)); + let mut renderer = Renderer::new(Backend::new( + &device, + &queue, + Settings::default(), + format, + )); let mut state = program::State::new( controls, @@ -247,6 +251,7 @@ pub fn main() { renderer.with_primitives(|backend, primitive| { backend.present( &device, + &queue, &mut staging_belt, &mut encoder, &view, -- cgit From b8c1809ea101cece6943432fd3597f785c39af09 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 7 Feb 2023 23:55:16 +0100 Subject: Refactor `triangle::Pipeline` into `prepare` and `render` architecture And get rid of the staging belt! :tada: --- examples/integration/src/main.rs | 9 --------- 1 file changed, 9 deletions(-) (limited to 'examples/integration') diff --git a/examples/integration/src/main.rs b/examples/integration/src/main.rs index 69f6961e..5af30ab1 100644 --- a/examples/integration/src/main.rs +++ b/examples/integration/src/main.rs @@ -125,9 +125,6 @@ pub fn main() { let mut resized = false; - // Initialize staging belt - let mut staging_belt = wgpu::util::StagingBelt::new(5 * 1024); - // Initialize scene and GUI controls let scene = Scene::new(&device, format); let controls = Controls::new(); @@ -252,7 +249,6 @@ pub fn main() { backend.present( &device, &queue, - &mut staging_belt, &mut encoder, &view, primitive, @@ -262,7 +258,6 @@ pub fn main() { }); // Then we submit the work - staging_belt.finish(); queue.submit(Some(encoder.finish())); frame.present(); @@ -272,10 +267,6 @@ pub fn main() { state.mouse_interaction(), ), ); - - // And recall staging buffers - staging_belt.recall(); - } Err(error) => match error { wgpu::SurfaceError::OutOfMemory => { -- cgit From 730d6a07564d014c470e02f233394ec98325d463 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 8 Feb 2023 00:47:16 +0100 Subject: Reuse a `RenderPass` as much as possible in `iced_wgpu` --- examples/integration/src/main.rs | 1 + 1 file changed, 1 insertion(+) (limited to 'examples/integration') diff --git a/examples/integration/src/main.rs b/examples/integration/src/main.rs index 5af30ab1..a7a90ced 100644 --- a/examples/integration/src/main.rs +++ b/examples/integration/src/main.rs @@ -250,6 +250,7 @@ pub fn main() { &device, &queue, &mut encoder, + None, &view, primitive, &viewport, -- cgit From 3a0d34c0240f4421737a6a08761f99d6f8140d02 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sat, 4 Mar 2023 05:37:11 +0100 Subject: Create `iced_widget` subcrate and re-organize the whole codebase --- examples/integration/Cargo.toml | 1 + examples/integration/src/controls.rs | 10 ++-- examples/integration/src/main.rs | 91 +++++++++++++++++++----------------- examples/integration/src/scene.rs | 2 +- 4 files changed, 56 insertions(+), 48 deletions(-) (limited to 'examples/integration') diff --git a/examples/integration/Cargo.toml b/examples/integration/Cargo.toml index 200306aa..60390242 100644 --- a/examples/integration/Cargo.toml +++ b/examples/integration/Cargo.toml @@ -8,6 +8,7 @@ publish = false [dependencies] iced_winit = { path = "../../winit" } iced_wgpu = { path = "../../wgpu", features = ["webgl"] } +iced_widget = { path = "../../widget" } env_logger = "0.8" [target.'cfg(target_arch = "wasm32")'.dependencies] diff --git a/examples/integration/src/controls.rs b/examples/integration/src/controls.rs index 533cb6e2..16e21709 100644 --- a/examples/integration/src/controls.rs +++ b/examples/integration/src/controls.rs @@ -1,6 +1,8 @@ use iced_wgpu::Renderer; -use iced_winit::widget::{slider, text_input, Column, Row, Text}; -use iced_winit::{Alignment, Color, Command, Element, Length, Program}; +use iced_widget::{slider, text_input, Column, Row, Text}; +use iced_winit::core::{Alignment, Color, Element, Length}; +use iced_winit::native::{Command, Program}; +use iced_winit::style::Theme; pub struct Controls { background_color: Color, @@ -27,7 +29,7 @@ impl Controls { } impl Program for Controls { - type Renderer = Renderer; + type Renderer = Renderer; type Message = Message; fn update(&mut self, message: Message) -> Command { @@ -43,7 +45,7 @@ impl Program for Controls { Command::none() } - fn view(&self) -> Element { + fn view(&self) -> Element> { let background_color = self.background_color; let text = &self.text; diff --git a/examples/integration/src/main.rs b/examples/integration/src/main.rs index a7a90ced..c1f1f076 100644 --- a/examples/integration/src/main.rs +++ b/examples/integration/src/main.rs @@ -4,11 +4,14 @@ mod scene; use controls::Controls; use scene::Scene; -use iced_wgpu::{wgpu, Backend, Renderer, Settings, Viewport}; -use iced_winit::{ - conversion, futures, program, renderer, winit, Clipboard, Color, Debug, - Size, -}; +use iced_wgpu::graphics::Viewport; +use iced_wgpu::{wgpu, Backend, Renderer, Settings}; +use iced_winit::core::renderer; +use iced_winit::core::{Color, Size}; +use iced_winit::native::program; +use iced_winit::native::Debug; +use iced_winit::style::Theme; +use iced_winit::{conversion, futures, winit, Clipboard}; use winit::{ dpi::PhysicalPosition, @@ -73,43 +76,45 @@ pub fn main() { let instance = wgpu::Instance::new(backend); let surface = unsafe { instance.create_surface(&window) }; - let (format, (device, queue)) = futures::executor::block_on(async { - let adapter = wgpu::util::initialize_adapter_from_env_or_default( - &instance, - backend, - Some(&surface), - ) - .await - .expect("No suitable GPU adapters found on the system!"); - - let adapter_features = adapter.features(); - - #[cfg(target_arch = "wasm32")] - let needed_limits = wgpu::Limits::downlevel_webgl2_defaults() - .using_resolution(adapter.limits()); - - #[cfg(not(target_arch = "wasm32"))] - let needed_limits = wgpu::Limits::default(); - - ( - surface - .get_supported_formats(&adapter) - .first() - .copied() - .expect("Get preferred format"), - adapter - .request_device( - &wgpu::DeviceDescriptor { - label: None, - features: adapter_features & wgpu::Features::default(), - limits: needed_limits, - }, - None, - ) - .await - .expect("Request device"), - ) - }); + let (format, (device, queue)) = + futures::futures::executor::block_on(async { + let adapter = wgpu::util::initialize_adapter_from_env_or_default( + &instance, + backend, + Some(&surface), + ) + .await + .expect("No suitable GPU adapters found on the system!"); + + let adapter_features = adapter.features(); + + #[cfg(target_arch = "wasm32")] + let needed_limits = wgpu::Limits::downlevel_webgl2_defaults() + .using_resolution(adapter.limits()); + + #[cfg(not(target_arch = "wasm32"))] + let needed_limits = wgpu::Limits::default(); + + ( + surface + .get_supported_formats(&adapter) + .first() + .copied() + .expect("Get preferred format"), + adapter + .request_device( + &wgpu::DeviceDescriptor { + label: None, + features: adapter_features + & wgpu::Features::default(), + limits: needed_limits, + }, + None, + ) + .await + .expect("Request device"), + ) + }); surface.configure( &device, @@ -188,7 +193,7 @@ pub fn main() { viewport.scale_factor(), ), &mut renderer, - &iced_wgpu::Theme::Dark, + &Theme::Dark, &renderer::Style { text_color: Color::WHITE }, &mut clipboard, &mut debug, diff --git a/examples/integration/src/scene.rs b/examples/integration/src/scene.rs index 3e41fbda..90c7efbf 100644 --- a/examples/integration/src/scene.rs +++ b/examples/integration/src/scene.rs @@ -1,5 +1,5 @@ use iced_wgpu::wgpu; -use iced_winit::Color; +use iced_winit::core::Color; pub struct Scene { pipeline: wgpu::RenderPipeline, -- cgit From 99e0a71504456976ba88040f5d1d3bbc347694ea Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sun, 5 Mar 2023 06:35:20 +0100 Subject: Rename `iced_native` to `iced_runtime` --- examples/integration/src/controls.rs | 2 +- examples/integration/src/main.rs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) (limited to 'examples/integration') diff --git a/examples/integration/src/controls.rs b/examples/integration/src/controls.rs index 16e21709..5849f730 100644 --- a/examples/integration/src/controls.rs +++ b/examples/integration/src/controls.rs @@ -1,7 +1,7 @@ use iced_wgpu::Renderer; use iced_widget::{slider, text_input, Column, Row, Text}; use iced_winit::core::{Alignment, Color, Element, Length}; -use iced_winit::native::{Command, Program}; +use iced_winit::runtime::{Command, Program}; use iced_winit::style::Theme; pub struct Controls { diff --git a/examples/integration/src/main.rs b/examples/integration/src/main.rs index c1f1f076..9707eda7 100644 --- a/examples/integration/src/main.rs +++ b/examples/integration/src/main.rs @@ -8,8 +8,8 @@ use iced_wgpu::graphics::Viewport; use iced_wgpu::{wgpu, Backend, Renderer, Settings}; use iced_winit::core::renderer; use iced_winit::core::{Color, Size}; -use iced_winit::native::program; -use iced_winit::native::Debug; +use iced_winit::runtime::program; +use iced_winit::runtime::Debug; use iced_winit::style::Theme; use iced_winit::{conversion, futures, winit, Clipboard}; -- cgit From 06bbcc310e6e759a0839df6ca391ea5e0f0ee609 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sun, 5 Mar 2023 06:40:20 +0100 Subject: Move `webgl` feature selection for `wgpu` into `iced_wgpu` --- examples/integration/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'examples/integration') diff --git a/examples/integration/Cargo.toml b/examples/integration/Cargo.toml index 60390242..f429977f 100644 --- a/examples/integration/Cargo.toml +++ b/examples/integration/Cargo.toml @@ -7,7 +7,7 @@ publish = false [dependencies] iced_winit = { path = "../../winit" } -iced_wgpu = { path = "../../wgpu", features = ["webgl"] } +iced_wgpu = { path = "../../wgpu" } iced_widget = { path = "../../widget" } env_logger = "0.8" -- cgit From d3900e067361c82fd857fc92b81284146140bc3b Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 7 Mar 2023 06:41:41 +0100 Subject: Enable renderer backends in `integration` example --- examples/integration/Cargo.toml | 1 + 1 file changed, 1 insertion(+) (limited to 'examples/integration') diff --git a/examples/integration/Cargo.toml b/examples/integration/Cargo.toml index f429977f..f6863cd3 100644 --- a/examples/integration/Cargo.toml +++ b/examples/integration/Cargo.toml @@ -9,6 +9,7 @@ publish = false iced_winit = { path = "../../winit" } iced_wgpu = { path = "../../wgpu" } iced_widget = { path = "../../widget" } +iced_renderer = { path = "../../renderer", features = ["wgpu", "tiny-skia"] } env_logger = "0.8" [target.'cfg(target_arch = "wasm32")'.dependencies] -- cgit From c0431aedd3bbef4161456f2fa5f29866e8f17fc5 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sat, 8 Apr 2023 04:47:05 +0200 Subject: Update `wgpu` and `cosmic-text` --- examples/integration/src/main.rs | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) (limited to 'examples/integration') diff --git a/examples/integration/src/main.rs b/examples/integration/src/main.rs index 9707eda7..949a726a 100644 --- a/examples/integration/src/main.rs +++ b/examples/integration/src/main.rs @@ -26,18 +26,16 @@ use web_sys::HtmlCanvasElement; #[cfg(target_arch = "wasm32")] use winit::platform::web::WindowBuilderExtWebSys; -pub fn main() { +pub fn main() -> Result<(), Box> { #[cfg(target_arch = "wasm32")] let canvas_element = { - console_log::init_with_level(log::Level::Debug) - .expect("could not initialize logger"); + console_log::init_with_level(log::Level::Debug)?; std::panic::set_hook(Box::new(console_error_panic_hook::hook)); web_sys::window() .and_then(|win| win.document()) .and_then(|doc| doc.get_element_by_id("iced_canvas")) - .and_then(|element| element.dyn_into::().ok()) - .expect("Canvas with id `iced_canvas` is missing") + .and_then(|element| element.dyn_into::().ok())? }; #[cfg(not(target_arch = "wasm32"))] env_logger::init(); @@ -48,8 +46,7 @@ pub fn main() { #[cfg(target_arch = "wasm32")] let window = winit::window::WindowBuilder::new() .with_canvas(Some(canvas_element)) - .build(&event_loop) - .expect("Failed to build winit window"); + .build(&event_loop)?; #[cfg(not(target_arch = "wasm32"))] let window = winit::window::Window::new(&event_loop).unwrap(); @@ -73,8 +70,11 @@ pub fn main() { let backend = wgpu::util::backend_bits_from_env().unwrap_or(default_backend); - let instance = wgpu::Instance::new(backend); - let surface = unsafe { instance.create_surface(&window) }; + let instance = wgpu::Instance::new(wgpu::InstanceDescriptor { + backends: backend, + ..Default::default() + }); + let surface = unsafe { instance.create_surface(&window) }?; let (format, (device, queue)) = futures::futures::executor::block_on(async { @@ -84,7 +84,7 @@ pub fn main() { Some(&surface), ) .await - .expect("No suitable GPU adapters found on the system!"); + .expect("Create adapter"); let adapter_features = adapter.features(); @@ -97,7 +97,8 @@ pub fn main() { ( surface - .get_supported_formats(&adapter) + .get_capabilities(&adapter) + .formats .first() .copied() .expect("Get preferred format"), @@ -125,6 +126,7 @@ pub fn main() { height: physical_size.height, present_mode: wgpu::PresentMode::AutoVsync, alpha_mode: wgpu::CompositeAlphaMode::Auto, + view_formats: vec![], }, ); @@ -220,7 +222,8 @@ pub fn main() { width: size.width, height: size.height, present_mode: wgpu::PresentMode::AutoVsync, - alpha_mode: wgpu::CompositeAlphaMode::Auto + alpha_mode: wgpu::CompositeAlphaMode::Auto, + view_formats: vec![], }, ); -- cgit From 88d3247717b6deb38d500c8c1b1b1f32d6df9991 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 27 Apr 2023 15:17:06 +0200 Subject: Fix build of `integration` example for Wasm target --- examples/integration/src/main.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'examples/integration') diff --git a/examples/integration/src/main.rs b/examples/integration/src/main.rs index 98d2bc59..d9aae7b9 100644 --- a/examples/integration/src/main.rs +++ b/examples/integration/src/main.rs @@ -36,7 +36,8 @@ pub fn main() -> Result<(), Box> { web_sys::window() .and_then(|win| win.document()) .and_then(|doc| doc.get_element_by_id("iced_canvas")) - .and_then(|element| element.dyn_into::().ok())? + .and_then(|element| element.dyn_into::().ok()) + .expect("Get canvas element") }; #[cfg(not(target_arch = "wasm32"))] env_logger::init(); -- cgit From dd04c0b070b60b15293892e2a7c284787d3d63b1 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 10 May 2023 22:21:31 +0200 Subject: Bundle `tiny-skia` backend together with `iced_renderer` --- examples/integration/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'examples/integration') diff --git a/examples/integration/Cargo.toml b/examples/integration/Cargo.toml index 2ab5d75f..22914742 100644 --- a/examples/integration/Cargo.toml +++ b/examples/integration/Cargo.toml @@ -9,7 +9,7 @@ publish = false iced_winit = { path = "../../winit" } iced_wgpu = { path = "../../wgpu" } iced_widget = { path = "../../widget" } -iced_renderer = { path = "../../renderer", features = ["wgpu", "tiny-skia"] } +iced_renderer = { path = "../../renderer", features = ["wgpu"] } env_logger = "0.10" [target.'cfg(target_arch = "wasm32")'.dependencies] -- cgit From aba98e49654852281ed17bedd1becac6f9db8700 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 8 Jun 2023 20:35:40 +0200 Subject: Extend cursor availability to the shell level --- examples/integration/src/main.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'examples/integration') diff --git a/examples/integration/src/main.rs b/examples/integration/src/main.rs index c935aca7..daecfeb2 100644 --- a/examples/integration/src/main.rs +++ b/examples/integration/src/main.rs @@ -6,6 +6,7 @@ use scene::Scene; use iced_wgpu::graphics::Viewport; use iced_wgpu::{wgpu, Backend, Renderer, Settings}; +use iced_winit::core::mouse; use iced_winit::core::renderer; use iced_winit::core::{Color, Size}; use iced_winit::runtime::program; @@ -194,10 +195,10 @@ pub fn main() -> Result<(), Box> { // We update iced let _ = state.update( viewport.logical_size(), - conversion::cursor_position( + mouse::Cursor::Available(conversion::cursor_position( cursor_position, viewport.scale_factor(), - ), + )), &mut renderer, &Theme::Dark, &renderer::Style { text_color: Color::WHITE }, -- cgit From 733c2bd9f594a21ce20444e8edcb4c5f88118e0a Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 8 Jun 2023 20:45:48 +0200 Subject: Use `mouse::Cursor` in `integration` example --- examples/integration/src/main.rs | 42 ++++++++++++++++++++++++++-------------- 1 file changed, 27 insertions(+), 15 deletions(-) (limited to 'examples/integration') diff --git a/examples/integration/src/main.rs b/examples/integration/src/main.rs index daecfeb2..342d4c69 100644 --- a/examples/integration/src/main.rs +++ b/examples/integration/src/main.rs @@ -15,7 +15,6 @@ use iced_winit::style::Theme; use iced_winit::{conversion, futures, winit, Clipboard}; use winit::{ - dpi::PhysicalPosition, event::{Event, ModifiersState, WindowEvent}, event_loop::{ControlFlow, EventLoop}, }; @@ -40,6 +39,7 @@ pub fn main() -> Result<(), Box> { .and_then(|element| element.dyn_into::().ok()) .expect("Get canvas element") }; + #[cfg(not(target_arch = "wasm32"))] env_logger::init(); @@ -59,7 +59,7 @@ pub fn main() -> Result<(), Box> { Size::new(physical_size.width, physical_size.height), window.scale_factor(), ); - let mut cursor_position = PhysicalPosition::new(-1.0, -1.0); + let mut cursor_position = None; let mut modifiers = ModifiersState::default(); let mut clipboard = Clipboard::connect(&window); @@ -166,7 +166,7 @@ pub fn main() -> Result<(), Box> { Event::WindowEvent { event, .. } => { match event { WindowEvent::CursorMoved { position, .. } => { - cursor_position = position; + cursor_position = Some(position); } WindowEvent::ModifiersChanged(new_modifiers) => { modifiers = new_modifiers; @@ -195,13 +195,20 @@ pub fn main() -> Result<(), Box> { // We update iced let _ = state.update( viewport.logical_size(), - mouse::Cursor::Available(conversion::cursor_position( - cursor_position, - viewport.scale_factor(), - )), + cursor_position + .map(|p| { + conversion::cursor_position( + p, + viewport.scale_factor(), + ) + }) + .map(mouse::Cursor::Available) + .unwrap_or(mouse::Cursor::Unavailable), &mut renderer, &Theme::Dark, - &renderer::Style { text_color: Color::WHITE }, + &renderer::Style { + text_color: Color::WHITE, + }, &mut clipboard, &mut debug, ); @@ -243,7 +250,9 @@ pub fn main() -> Result<(), Box> { let program = state.program(); - let view = frame.texture.create_view(&wgpu::TextureViewDescriptor::default()); + let view = frame.texture.create_view( + &wgpu::TextureViewDescriptor::default(), + ); { // We clear the frame @@ -276,15 +285,18 @@ pub fn main() -> Result<(), Box> { frame.present(); // Update the mouse cursor - window.set_cursor_icon( - iced_winit::conversion::mouse_interaction( - state.mouse_interaction(), - ), - ); + window.set_cursor_icon( + iced_winit::conversion::mouse_interaction( + state.mouse_interaction(), + ), + ); } Err(error) => match error { wgpu::SurfaceError::OutOfMemory => { - panic!("Swapchain error: {error}. Rendering cannot continue.") + panic!( + "Swapchain error: {error}. \ + Rendering cannot continue." + ) } _ => { // Try rendering again next frame. -- cgit