diff options
author | 2024-02-20 03:56:30 +0100 | |
---|---|---|
committer | 2024-02-20 03:56:30 +0100 | |
commit | 698b9001b12bfb23fe1eccece62eae6417bea18a (patch) | |
tree | 3db828501fedac26342c41fcec05d0e9a7477f0f | |
parent | 0fe265adb0a97e64b0c83cbb19c7d0179bb60975 (diff) | |
parent | 35e2049ee668791f67b5325fc156b8c042d26d49 (diff) | |
download | iced-698b9001b12bfb23fe1eccece62eae6417bea18a.tar.gz iced-698b9001b12bfb23fe1eccece62eae6417bea18a.tar.bz2 iced-698b9001b12bfb23fe1eccece62eae6417bea18a.zip |
Merge pull request #2271 from iced-rs/fix/wasm
Fix WebAssembly platform
-rw-r--r-- | Cargo.toml | 4 | ||||
-rw-r--r-- | examples/tour/index.html | 4 | ||||
-rw-r--r-- | graphics/Cargo.toml | 1 | ||||
-rw-r--r-- | graphics/fonts/FiraSans-Regular.ttf | bin | 0 -> 440984 bytes | |||
-rw-r--r-- | graphics/src/text.rs | 13 | ||||
-rw-r--r-- | renderer/Cargo.toml | 1 | ||||
-rw-r--r-- | wgpu/src/quad/gradient.rs | 206 | ||||
-rw-r--r-- | wgpu/src/window/compositor.rs | 4 | ||||
-rw-r--r-- | winit/src/application.rs | 4 |
9 files changed, 142 insertions, 95 deletions
@@ -18,7 +18,7 @@ all-features = true maintenance = { status = "actively-developed" } [features] -default = ["wgpu"] +default = ["wgpu", "fira-sans"] # Enable the `wgpu` GPU-accelerated renderer backend wgpu = ["iced_renderer/wgpu", "iced_widget/wgpu"] # Enables the `Image` widget @@ -53,6 +53,8 @@ highlighter = ["iced_highlighter"] multi-window = ["iced_winit/multi-window"] # Enables the advanced module advanced = [] +# Enables embedding Fira Sans as the default font on Wasm builds +fira-sans = ["iced_renderer/fira-sans"] [dependencies] iced_core.workspace = true diff --git a/examples/tour/index.html b/examples/tour/index.html index c64af912..09d03367 100644 --- a/examples/tour/index.html +++ b/examples/tour/index.html @@ -1,12 +1,12 @@ <!DOCTYPE html> -<html lang="en"> +<html lang="en" style="height: 100%"> <head> <meta charset="utf-8" content="text/html; charset=utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Tour - Iced</title> <base data-trunk-public-url /> </head> -<body> +<body style="height: 100%; margin: 0"> <link data-trunk rel="rust" href="Cargo.toml" data-wasm-opt="z" data-bin="tour" /> </body> </html> diff --git a/graphics/Cargo.toml b/graphics/Cargo.toml index 907f3705..0ee6ff47 100644 --- a/graphics/Cargo.toml +++ b/graphics/Cargo.toml @@ -18,6 +18,7 @@ all-features = true geometry = ["lyon_path"] image = ["dep:image", "kamadak-exif"] web-colors = [] +fira-sans = [] [dependencies] iced_core.workspace = true diff --git a/graphics/fonts/FiraSans-Regular.ttf b/graphics/fonts/FiraSans-Regular.ttf Binary files differnew file mode 100644 index 00000000..6f806474 --- /dev/null +++ b/graphics/fonts/FiraSans-Regular.ttf diff --git a/graphics/src/text.rs b/graphics/src/text.rs index 217a23e2..0310ead7 100644 --- a/graphics/src/text.rs +++ b/graphics/src/text.rs @@ -17,6 +17,15 @@ use once_cell::sync::OnceCell; use std::borrow::Cow; use std::sync::{Arc, RwLock, Weak}; +/// The regular variant of the [Fira Sans] font. +/// +/// It is loaded as part of the default fonts in Wasm builds. +/// +/// [Fira Sans]: https://mozilla.github.io/Fira/ +#[cfg(all(target_arch = "wasm32", feature = "fira-sans"))] +pub const FIRA_SANS_REGULAR: &'static [u8] = + include_bytes!("../fonts/FiraSans-Regular.ttf").as_slice(); + /// Returns the global [`FontSystem`]. pub fn font_system() -> &'static RwLock<FontSystem> { static FONT_SYSTEM: OnceCell<RwLock<FontSystem>> = OnceCell::new(); @@ -27,6 +36,10 @@ pub fn font_system() -> &'static RwLock<FontSystem> { cosmic_text::fontdb::Source::Binary(Arc::new( include_bytes!("../fonts/Iced-Icons.ttf").as_slice(), )), + #[cfg(all(target_arch = "wasm32", feature = "fira-sans"))] + cosmic_text::fontdb::Source::Binary(Arc::new( + include_bytes!("../fonts/FiraSans-Regular.ttf").as_slice(), + )), ]), version: Version::default(), }) diff --git a/renderer/Cargo.toml b/renderer/Cargo.toml index a159978c..5cce2427 100644 --- a/renderer/Cargo.toml +++ b/renderer/Cargo.toml @@ -18,6 +18,7 @@ geometry = ["iced_graphics/geometry", "iced_tiny_skia/geometry", "iced_wgpu?/geo tracing = ["iced_wgpu?/tracing"] web-colors = ["iced_wgpu?/web-colors"] webgl = ["iced_wgpu?/webgl"] +fira-sans = ["iced_graphics/fira-sans"] [dependencies] iced_graphics.workspace = true diff --git a/wgpu/src/quad/gradient.rs b/wgpu/src/quad/gradient.rs index 60b170cc..560fcad2 100644 --- a/wgpu/src/quad/gradient.rs +++ b/wgpu/src/quad/gradient.rs @@ -1,4 +1,3 @@ -use crate::graphics::color; use crate::graphics::gradient; use crate::quad::{self, Quad}; use crate::Buffer; @@ -59,106 +58,128 @@ impl Layer { #[derive(Debug)] pub struct Pipeline { + #[cfg(not(target_arch = "wasm32"))] pipeline: wgpu::RenderPipeline, } impl Pipeline { + #[allow(unused_variables)] pub fn new( device: &wgpu::Device, format: wgpu::TextureFormat, constants_layout: &wgpu::BindGroupLayout, ) -> Self { - let layout = - device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { - label: Some("iced_wgpu.quad.gradient.pipeline"), - push_constant_ranges: &[], - bind_group_layouts: &[constants_layout], - }); - - let shader = - device.create_shader_module(wgpu::ShaderModuleDescriptor { - label: Some("iced_wgpu.quad.gradient.shader"), - source: wgpu::ShaderSource::Wgsl(std::borrow::Cow::Borrowed( - if color::GAMMA_CORRECTION { - concat!( - include_str!("../shader/quad.wgsl"), - "\n", - include_str!("../shader/vertex.wgsl"), - "\n", - include_str!("../shader/quad/gradient.wgsl"), - "\n", - include_str!("../shader/color/oklab.wgsl") - ) - } else { - concat!( - include_str!("../shader/quad.wgsl"), - "\n", - include_str!("../shader/vertex.wgsl"), - "\n", - include_str!("../shader/quad/gradient.wgsl"), - "\n", - include_str!("../shader/color/linear_rgb.wgsl") - ) - }, - )), - }); - - let pipeline = - device.create_render_pipeline(&wgpu::RenderPipelineDescriptor { - label: Some("iced_wgpu.quad.gradient.pipeline"), - layout: Some(&layout), - vertex: wgpu::VertexState { - module: &shader, - entry_point: "gradient_vs_main", - buffers: &[wgpu::VertexBufferLayout { - array_stride: std::mem::size_of::<Gradient>() as u64, - step_mode: wgpu::VertexStepMode::Instance, - attributes: &wgpu::vertex_attr_array!( - // Colors 1-2 - 0 => Uint32x4, - // Colors 3-4 - 1 => Uint32x4, - // Colors 5-6 - 2 => Uint32x4, - // Colors 7-8 - 3 => Uint32x4, - // Offsets 1-8 - 4 => Uint32x4, - // Direction - 5 => Float32x4, - // Position & Scale - 6 => Float32x4, - // Border color - 7 => Float32x4, - // Border radius - 8 => Float32x4, - // Border width - 9 => Float32 - ), - }], - }, - fragment: Some(wgpu::FragmentState { - module: &shader, - entry_point: "gradient_fs_main", - targets: &quad::color_target_state(format), - }), - primitive: wgpu::PrimitiveState { - topology: wgpu::PrimitiveTopology::TriangleList, - front_face: wgpu::FrontFace::Cw, - ..Default::default() + #[cfg(not(target_arch = "wasm32"))] + { + use crate::graphics::color; + + let layout = device.create_pipeline_layout( + &wgpu::PipelineLayoutDescriptor { + label: Some("iced_wgpu.quad.gradient.pipeline"), + push_constant_ranges: &[], + bind_group_layouts: &[constants_layout], }, - depth_stencil: None, - multisample: wgpu::MultisampleState { - count: 1, - mask: !0, - alpha_to_coverage_enabled: false, + ); + + let shader = + device.create_shader_module(wgpu::ShaderModuleDescriptor { + label: Some("iced_wgpu.quad.gradient.shader"), + source: wgpu::ShaderSource::Wgsl( + std::borrow::Cow::Borrowed( + if color::GAMMA_CORRECTION { + concat!( + include_str!("../shader/quad.wgsl"), + "\n", + include_str!("../shader/vertex.wgsl"), + "\n", + include_str!( + "../shader/quad/gradient.wgsl" + ), + "\n", + include_str!("../shader/color/oklab.wgsl") + ) + } else { + concat!( + include_str!("../shader/quad.wgsl"), + "\n", + include_str!("../shader/vertex.wgsl"), + "\n", + include_str!( + "../shader/quad/gradient.wgsl" + ), + "\n", + include_str!( + "../shader/color/linear_rgb.wgsl" + ) + ) + }, + ), + ), + }); + + let pipeline = device.create_render_pipeline( + &wgpu::RenderPipelineDescriptor { + label: Some("iced_wgpu.quad.gradient.pipeline"), + layout: Some(&layout), + vertex: wgpu::VertexState { + module: &shader, + entry_point: "gradient_vs_main", + buffers: &[wgpu::VertexBufferLayout { + array_stride: std::mem::size_of::<Gradient>() + as u64, + step_mode: wgpu::VertexStepMode::Instance, + attributes: &wgpu::vertex_attr_array!( + // Colors 1-2 + 0 => Uint32x4, + // Colors 3-4 + 1 => Uint32x4, + // Colors 5-6 + 2 => Uint32x4, + // Colors 7-8 + 3 => Uint32x4, + // Offsets 1-8 + 4 => Uint32x4, + // Direction + 5 => Float32x4, + // Position & Scale + 6 => Float32x4, + // Border color + 7 => Float32x4, + // Border radius + 8 => Float32x4, + // Border width + 9 => Float32 + ), + }], + }, + fragment: Some(wgpu::FragmentState { + module: &shader, + entry_point: "gradient_fs_main", + targets: &quad::color_target_state(format), + }), + primitive: wgpu::PrimitiveState { + topology: wgpu::PrimitiveTopology::TriangleList, + front_face: wgpu::FrontFace::Cw, + ..Default::default() + }, + depth_stencil: None, + multisample: wgpu::MultisampleState { + count: 1, + mask: !0, + alpha_to_coverage_enabled: false, + }, + multiview: None, }, - multiview: None, - }); + ); - Self { pipeline } + Self { pipeline } + } + + #[cfg(target_arch = "wasm32")] + Self {} } + #[allow(unused_variables)] pub fn render<'a>( &'a self, render_pass: &mut wgpu::RenderPass<'a>, @@ -169,10 +190,13 @@ impl Pipeline { #[cfg(feature = "tracing")] let _ = tracing::info_span!("Wgpu::Quad::Gradient", "DRAW").entered(); - render_pass.set_pipeline(&self.pipeline); - render_pass.set_bind_group(0, constants, &[]); - render_pass.set_vertex_buffer(0, layer.instances.slice(..)); + #[cfg(not(target_arch = "wasm32"))] + { + render_pass.set_pipeline(&self.pipeline); + render_pass.set_bind_group(0, constants, &[]); + render_pass.set_vertex_buffer(0, layer.instances.slice(..)); - render_pass.draw(0..6, range.start as u32..range.end as u32); + render_pass.draw(0..6, range.start as u32..range.end as u32); + } } } diff --git a/wgpu/src/window/compositor.rs b/wgpu/src/window/compositor.rs index 7ac017d9..0a5d2c8f 100644 --- a/wgpu/src/window/compositor.rs +++ b/wgpu/src/window/compositor.rs @@ -257,7 +257,9 @@ impl graphics::Compositor for Compositor { .create_surface(window) .expect("Create surface"); - self.configure_surface(&mut surface, width, height); + if width > 0 && height > 0 { + self.configure_surface(&mut surface, width, height); + } surface } diff --git a/winit/src/application.rs b/winit/src/application.rs index 0c596b3f..1fd51d82 100644 --- a/winit/src/application.rs +++ b/winit/src/application.rs @@ -159,6 +159,10 @@ where use winit::platform::web::WindowExtWebSys; let canvas = window.canvas().expect("Get window canvas"); + let _ = canvas.set_attribute( + "style", + "display: block; width: 100%; height: 100%", + ); let window = web_sys::window().unwrap(); let document = window.document().unwrap(); |