Write docs for iced_wgpu

This commit is contained in:
Héctor Ramón Jiménez 2019-11-22 22:14:24 +01:00
parent fa227255b0
commit 6a0e442ad6
7 changed files with 74 additions and 16 deletions

View File

@ -1,11 +1,9 @@
use crate::Transformation; use crate::Transformation;
use iced_native::Rectangle; use iced_native::Rectangle;
use std::cell::RefCell; use std::{cell::RefCell, collections::HashMap, mem, rc::Rc};
use std::collections::HashMap;
use std::mem;
use std::rc::Rc;
#[derive(Debug)]
pub struct Pipeline { pub struct Pipeline {
cache: RefCell<HashMap<String, Memory>>, cache: RefCell<HashMap<String, Memory>>,
@ -207,7 +205,8 @@ impl Pipeline {
if !self.cache.borrow().contains_key(path) { if !self.cache.borrow().contains_key(path) {
let image = image::open(path).expect("Load image").to_bgra(); let image = image::open(path).expect("Load image").to_bgra();
self.cache let _ = self
.cache
.borrow_mut() .borrow_mut()
.insert(path.to_string(), Memory::Host { image }); .insert(path.to_string(), Memory::Host { image });
} }
@ -308,6 +307,7 @@ impl Pipeline {
} }
} }
#[derive(Debug)]
enum Memory { enum Memory {
Host { Host {
image: image::ImageBuffer<image::Bgra<u8>, Vec<u8>>, image: image::ImageBuffer<image::Bgra<u8>, Vec<u8>>,

View File

@ -1,3 +1,29 @@
//! A [`wgpu`] renderer for [`iced_native`].
//!
//! ![`iced_wgpu` crate graph](https://github.com/hecrj/iced/blob/cae26cb7bc627f4a5b3bcf1cd023a0c552e8c65e/docs/graphs/wgpu.png?raw=true)
//!
//! For now, it is the default renderer of [Iced] in native platforms.
//!
//! [`wgpu`] supports most modern graphics backends: Vulkan, Metal, DX11, and
//! DX12 (OpenGL and WebGL are still WIP). Additionally, it will support the
//! incoming [WebGPU API].
//!
//! Currently, `iced_wgpu` supports the following primitives:
//! - Text, which is rendered using [`wgpu_glyph`]. No shaping at all.
//! - Quads or rectangles, with rounded borders and a solid background color.
//! - Images, lazily loaded from the filesystem.
//! - Clip areas, useful to implement scrollables or hide overflowing content.
//!
//! [Iced]: https://github.com/hecrj/iced
//! [`iced_native`]: https://github.com/hecrj/iced/tree/master/native
//! [`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_debug_implementations)]
#![deny(unused_results)]
#![deny(unsafe_code)]
#![deny(rust_2018_idioms)]
mod image; mod image;
mod primitive; mod primitive;
mod quad; mod quad;

View File

@ -3,33 +3,56 @@ use iced_native::{
VerticalAlignment, VerticalAlignment,
}; };
/// A rendering primitive.
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub enum Primitive { pub enum Primitive {
/// An empty primitive
None, None,
/// A group of primitives
Group { Group {
/// The primitives of the group
primitives: Vec<Primitive>, primitives: Vec<Primitive>,
}, },
/// A text primitive
Text { Text {
/// The contents of the text
content: String, content: String,
/// The bounds of the text
bounds: Rectangle, bounds: Rectangle,
/// The color of the text
color: Color, color: Color,
/// The size of the text
size: f32, size: f32,
/// The font of the text
font: Font, font: Font,
/// The horizontal alignment of the text
horizontal_alignment: HorizontalAlignment, horizontal_alignment: HorizontalAlignment,
/// The vertical alignment of the text
vertical_alignment: VerticalAlignment, vertical_alignment: VerticalAlignment,
}, },
/// A quad primitive
Quad { Quad {
/// The bounds of the quad
bounds: Rectangle, bounds: Rectangle,
/// The background of the quad
background: Background, background: Background,
/// The border radius of the quad
border_radius: u16, border_radius: u16,
}, },
/// An image primitive
Image { Image {
/// The path of the image
path: String, path: String,
/// The bounds of the image
bounds: Rectangle, bounds: Rectangle,
}, },
/// A clip primitive
Clip { Clip {
/// The bounds of the clip
bounds: Rectangle, bounds: Rectangle,
/// The offset transformation of the clip
offset: Vector<u32>, offset: Vector<u32>,
/// The content of the clip
content: Box<Primitive>, content: Box<Primitive>,
}, },
} }

View File

@ -3,6 +3,7 @@ use iced_native::Rectangle;
use std::mem; use std::mem;
#[derive(Debug)]
pub struct Pipeline { pub struct Pipeline {
pipeline: wgpu::RenderPipeline, pipeline: wgpu::RenderPipeline,
constants: wgpu::BindGroup, constants: wgpu::BindGroup,

View File

@ -14,6 +14,10 @@ mod widget;
pub use target::Target; pub use target::Target;
/// A [`wgpu`] renderer.
///
/// [`wgpu`]: https://github.com/gfx-rs/wgpu-rs
#[derive(Debug)]
pub struct Renderer { pub struct Renderer {
device: Device, device: Device,
queue: Queue, queue: Queue,
@ -22,7 +26,7 @@ pub struct Renderer {
text_pipeline: text::Pipeline, text_pipeline: text::Pipeline,
} }
pub struct Layer<'a> { struct Layer<'a> {
bounds: Rectangle<u32>, bounds: Rectangle<u32>,
offset: Vector<u32>, offset: Vector<u32>,
quads: Vec<Quad>, quads: Vec<Quad>,
@ -304,7 +308,7 @@ impl Renderer {
&mut self, &mut self,
dpi: f32, dpi: f32,
transformation: Transformation, transformation: Transformation,
layer: &Layer, layer: &Layer<'_>,
encoder: &mut wgpu::CommandEncoder, encoder: &mut wgpu::CommandEncoder,
target: &wgpu::TextureView, target: &wgpu::TextureView,
) { ) {

View File

@ -2,6 +2,8 @@ use crate::{Renderer, Transformation};
use raw_window_handle::HasRawWindowHandle; use raw_window_handle::HasRawWindowHandle;
/// A rendering target.
#[derive(Debug)]
pub struct Target { pub struct Target {
surface: wgpu::Surface, surface: wgpu::Surface,
width: u16, width: u16,
@ -12,19 +14,19 @@ pub struct Target {
} }
impl Target { impl Target {
pub fn dimensions(&self) -> (u16, u16) { pub(crate) fn dimensions(&self) -> (u16, u16) {
(self.width, self.height) (self.width, self.height)
} }
pub fn dpi(&self) -> f32 { pub(crate) fn dpi(&self) -> f32 {
self.dpi self.dpi
} }
pub fn transformation(&self) -> Transformation { pub(crate) fn transformation(&self) -> Transformation {
self.transformation self.transformation
} }
pub fn next_frame(&mut self) -> wgpu::SwapChainOutput { pub(crate) fn next_frame(&mut self) -> wgpu::SwapChainOutput<'_> {
self.swap_chain.get_next_texture() self.swap_chain.get_next_texture()
} }
} }

View File

@ -2,8 +2,7 @@ mod font;
use crate::Transformation; use crate::Transformation;
use std::cell::RefCell; use std::{cell::RefCell, collections::HashMap};
use std::collections::HashMap;
pub const BUILTIN_ICONS: iced_native::Font = iced_native::Font::External { pub const BUILTIN_ICONS: iced_native::Font = iced_native::Font::External {
name: "iced_wgpu icons", name: "iced_wgpu icons",
@ -12,6 +11,7 @@ pub const BUILTIN_ICONS: iced_native::Font = iced_native::Font::External {
pub const CHECKMARK_ICON: char = '\u{F00C}'; pub const CHECKMARK_ICON: char = '\u{F00C}';
#[derive(Debug)]
pub struct Pipeline { pub struct Pipeline {
draw_brush: RefCell<wgpu_glyph::GlyphBrush<'static, ()>>, draw_brush: RefCell<wgpu_glyph::GlyphBrush<'static, ()>>,
draw_font_map: RefCell<HashMap<String, wgpu_glyph::FontId>>, draw_font_map: RefCell<HashMap<String, wgpu_glyph::FontId>>,
@ -56,7 +56,7 @@ impl Pipeline {
wgpu_glyph::FontId(0) wgpu_glyph::FontId(0)
} }
pub fn queue(&mut self, section: wgpu_glyph::Section) { pub fn queue(&mut self, section: wgpu_glyph::Section<'_>) {
self.draw_brush.borrow_mut().queue(section); self.draw_brush.borrow_mut().queue(section);
} }
@ -134,7 +134,8 @@ impl Pipeline {
// it uses a lifetimed `GlyphCalculatorGuard` with side-effects on drop. // it uses a lifetimed `GlyphCalculatorGuard` with side-effects on drop.
// This makes stuff quite inconvenient. A manual method for trimming the // This makes stuff quite inconvenient. A manual method for trimming the
// cache would make our lives easier. // cache would make our lives easier.
self.measure_brush let _ = self
.measure_brush
.borrow_mut() .borrow_mut()
.process_queued(|_, _| {}, |_| {}) .process_queued(|_, _| {}, |_| {})
.expect("Trim text measurements"); .expect("Trim text measurements");
@ -154,7 +155,8 @@ impl Pipeline {
let font_id = let font_id =
self.draw_brush.borrow_mut().add_font_bytes(bytes); self.draw_brush.borrow_mut().add_font_bytes(bytes);
self.draw_font_map let _ = self
.draw_font_map
.borrow_mut() .borrow_mut()
.insert(String::from(name), font_id); .insert(String::from(name), font_id);