From 00a8a167122301983753a2f4b43d136c79a7d5cb Mon Sep 17 00:00:00 2001 From: shan Date: Thu, 29 Sep 2022 10:52:58 -0700 Subject: Adds linear gradient support to 2D meshes in the canvas widget. --- Cargo.toml | 1 + examples/arc/src/main.rs | 14 +- examples/clock/src/main.rs | 54 ++- examples/geometry/src/main.rs | 41 +- examples/modern_art/Cargo.toml | 10 + examples/modern_art/src/main.rs | 141 ++++++ examples/solar_system/src/main.rs | 13 +- glow/src/backend.rs | 7 +- glow/src/shader/common/gradient.frag | 48 ++ glow/src/shader/common/triangle.frag | 4 +- glow/src/shader/common/triangle.vert | 6 +- glow/src/triangle.rs | 228 +++++----- glow/src/triangle/gradient.rs | 189 ++++++++ glow/src/triangle/solid.rs | 67 +++ glow/src/window/compositor.rs | 3 +- graphics/Cargo.toml | 2 +- graphics/src/gradient.rs | 23 + graphics/src/layer.rs | 47 +- graphics/src/lib.rs | 2 + graphics/src/primitive.rs | 5 +- graphics/src/shader.rs | 42 ++ graphics/src/transformation.rs | 8 +- graphics/src/triangle.rs | 18 +- graphics/src/widget/canvas.rs | 6 +- graphics/src/widget/canvas/fill.rs | 33 +- graphics/src/widget/canvas/frame.rs | 111 ++--- graphics/src/widget/canvas/gradient.rs | 21 + graphics/src/widget/canvas/gradient/linear.rs | 73 ++++ graphics/src/widget/canvas/stroke.rs | 26 +- wgpu/Cargo.toml | 7 + wgpu/src/backend.rs | 11 +- wgpu/src/buffers.rs | 3 + wgpu/src/buffers/buffer.rs | 91 ++++ wgpu/src/buffers/dynamic_buffers.rs | 202 +++++++++ wgpu/src/lib.rs | 1 + wgpu/src/shader/triangle_gradient.wgsl | 83 ++++ wgpu/src/shader/triangle_solid.wgsl | 18 + wgpu/src/triangle.rs | 605 +++++++++++--------------- wgpu/src/triangle/gradient.rs | 265 +++++++++++ wgpu/src/triangle/solid.rs | 169 +++++++ 40 files changed, 2043 insertions(+), 655 deletions(-) create mode 100644 examples/modern_art/Cargo.toml create mode 100644 examples/modern_art/src/main.rs create mode 100644 glow/src/shader/common/gradient.frag create mode 100644 glow/src/triangle/gradient.rs create mode 100644 glow/src/triangle/solid.rs create mode 100644 graphics/src/gradient.rs create mode 100644 graphics/src/shader.rs create mode 100644 graphics/src/widget/canvas/gradient.rs create mode 100644 graphics/src/widget/canvas/gradient/linear.rs create mode 100644 wgpu/src/buffers.rs create mode 100644 wgpu/src/buffers/buffer.rs create mode 100644 wgpu/src/buffers/dynamic_buffers.rs create mode 100644 wgpu/src/shader/triangle_gradient.wgsl create mode 100644 wgpu/src/shader/triangle_solid.wgsl create mode 100644 wgpu/src/triangle/gradient.rs create mode 100644 wgpu/src/triangle/solid.rs diff --git a/Cargo.toml b/Cargo.toml index 725baecc..d8f5eccb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -72,6 +72,7 @@ members = [ "examples/geometry", "examples/integration_opengl", "examples/integration_wgpu", + "examples/modern_art", "examples/pane_grid", "examples/pick_list", "examples/pokedex", diff --git a/examples/arc/src/main.rs b/examples/arc/src/main.rs index 0c619dc9..6029a69c 100644 --- a/examples/arc/src/main.rs +++ b/examples/arc/src/main.rs @@ -2,7 +2,7 @@ use std::{f32::consts::PI, time::Instant}; use iced::executor; use iced::widget::canvas::{ - self, Cache, Canvas, Cursor, Geometry, Path, Stroke, + self, Cache, Canvas, Cursor, Geometry, Path, Stroke, StrokeStyle, }; use iced::{ Application, Command, Element, Length, Point, Rectangle, Settings, @@ -52,11 +52,6 @@ impl Application for Arc { Command::none() } - fn subscription(&self) -> Subscription { - iced::time::every(std::time::Duration::from_millis(10)) - .map(|_| Message::Tick) - } - fn view(&self) -> Element { Canvas::new(self) .width(Length::Fill) @@ -67,6 +62,11 @@ impl Application for Arc { fn theme(&self) -> Theme { Theme::Dark } + + fn subscription(&self) -> Subscription { + iced::time::every(std::time::Duration::from_millis(10)) + .map(|_| Message::Tick) + } } impl canvas::Program for Arc { @@ -114,7 +114,7 @@ impl canvas::Program for Arc { frame.stroke( &path, Stroke { - color: palette.text, + style: StrokeStyle::Solid(palette.text), width: 10.0, ..Stroke::default() }, diff --git a/examples/clock/src/main.rs b/examples/clock/src/main.rs index 8818fb54..51f25a3f 100644 --- a/examples/clock/src/main.rs +++ b/examples/clock/src/main.rs @@ -1,5 +1,7 @@ use iced::executor; -use iced::widget::canvas::{Cache, Cursor, Geometry, LineCap, Path, Stroke}; +use iced::widget::canvas::{ + Cache, Cursor, Geometry, LineCap, Path, Stroke, StrokeStyle, +}; use iced::widget::{canvas, container}; use iced::{ Application, Color, Command, Element, Length, Point, Rectangle, Settings, @@ -24,9 +26,9 @@ enum Message { } impl Application for Clock { + type Executor = executor::Default; type Message = Message; type Theme = Theme; - type Executor = executor::Default; type Flags = (); fn new(_flags: ()) -> (Self, Command) { @@ -59,15 +61,6 @@ impl Application for Clock { Command::none() } - fn subscription(&self) -> Subscription { - iced::time::every(std::time::Duration::from_millis(500)).map(|_| { - Message::Tick( - time::OffsetDateTime::now_local() - .unwrap_or_else(|_| time::OffsetDateTime::now_utc()), - ) - }) - } - fn view(&self) -> Element { let canvas = canvas(self as &Self) .width(Length::Fill) @@ -79,6 +72,15 @@ impl Application for Clock { .padding(20) .into() } + + fn subscription(&self) -> Subscription { + iced::time::every(std::time::Duration::from_millis(500)).map(|_| { + Message::Tick( + time::OffsetDateTime::now_local() + .unwrap_or_else(|_| time::OffsetDateTime::now_utc()), + ) + }) + } } impl canvas::Program for Clock { @@ -104,33 +106,41 @@ impl canvas::Program for Clock { let long_hand = Path::line(Point::ORIGIN, Point::new(0.0, -0.8 * radius)); - let thin_stroke = Stroke { - width: radius / 100.0, - color: Color::WHITE, - line_cap: LineCap::Round, - ..Stroke::default() + let width = radius / 100.0; + + let thin_stroke = || -> Stroke { + Stroke { + width, + style: StrokeStyle::Solid(Color::WHITE), + line_cap: LineCap::Round, + ..Stroke::default() + } }; - let wide_stroke = Stroke { - width: thin_stroke.width * 3.0, - ..thin_stroke + let wide_stroke = || -> Stroke { + Stroke { + width: width * 3.0, + style: StrokeStyle::Solid(Color::WHITE), + line_cap: LineCap::Round, + ..Stroke::default() + } }; frame.translate(Vector::new(center.x, center.y)); frame.with_save(|frame| { frame.rotate(hand_rotation(self.now.hour(), 12)); - frame.stroke(&short_hand, wide_stroke); + frame.stroke(&short_hand, wide_stroke()); }); frame.with_save(|frame| { frame.rotate(hand_rotation(self.now.minute(), 60)); - frame.stroke(&long_hand, wide_stroke); + frame.stroke(&long_hand, wide_stroke()); }); frame.with_save(|frame| { frame.rotate(hand_rotation(self.now.second(), 60)); - frame.stroke(&long_hand, thin_stroke); + frame.stroke(&long_hand, thin_stroke()); }) }); diff --git a/examples/geometry/src/main.rs b/examples/geometry/src/main.rs index d8b99ab3..a8ce26f8 100644 --- a/examples/geometry/src/main.rs +++ b/examples/geometry/src/main.rs @@ -1,6 +1,9 @@ //! This example showcases a simple native custom widget that renders using //! arbitrary low-level geometry. +//! +//TODO need to update this now that vertex data doesn't contain color data mod rainbow { + use iced::Color; // For now, to implement a custom native widget you will need to add // `iced_native` and `iced_wgpu` to your dependencies. // @@ -12,6 +15,7 @@ mod rainbow { // implemented by `iced_wgpu` and other renderers. use iced_graphics::renderer::{self, Renderer}; use iced_graphics::{Backend, Primitive}; + use iced_graphics::shader::Shader; use iced_native::widget::{self, Widget}; use iced_native::{ @@ -63,20 +67,20 @@ mod rainbow { cursor_position: Point, _viewport: &Rectangle, ) { - use iced_graphics::triangle::{Mesh2D, Vertex2D}; + use iced_graphics::triangle::{Mesh2D, Shader, Vertex2D}; use iced_native::Renderer as _; let b = layout.bounds(); // R O Y G B I V - let color_r = [1.0, 0.0, 0.0, 1.0]; - let color_o = [1.0, 0.5, 0.0, 1.0]; - let color_y = [1.0, 1.0, 0.0, 1.0]; - let color_g = [0.0, 1.0, 0.0, 1.0]; - let color_gb = [0.0, 1.0, 0.5, 1.0]; - let color_b = [0.0, 0.2, 1.0, 1.0]; - let color_i = [0.5, 0.0, 1.0, 1.0]; - let color_v = [0.75, 0.0, 0.5, 1.0]; + // let color_r = [1.0, 0.0, 0.0, 1.0]; + // let color_o = [1.0, 0.5, 0.0, 1.0]; + // let color_y = [1.0, 1.0, 0.0, 1.0]; + // let color_g = [0.0, 1.0, 0.0, 1.0]; + // let color_gb = [0.0, 1.0, 0.5, 1.0]; + // let color_b = [0.0, 0.2, 1.0, 1.0]; + // let color_i = [0.5, 0.0, 1.0, 1.0]; + // let color_v = [0.75, 0.0, 0.5, 1.0]; let posn_center = { if b.contains(cursor_position) { @@ -101,39 +105,39 @@ mod rainbow { vertices: vec![ Vertex2D { position: posn_center, - color: [1.0, 1.0, 1.0, 1.0], + // color: [1.0, 1.0, 1.0, 1.0], }, Vertex2D { position: posn_tl, - color: color_r, + // color: color_r, }, Vertex2D { position: posn_t, - color: color_o, + // color: color_o, }, Vertex2D { position: posn_tr, - color: color_y, + // color: color_y, }, Vertex2D { position: posn_r, - color: color_g, + // color: color_g, }, Vertex2D { position: posn_br, - color: color_gb, + // color: color_gb, }, Vertex2D { position: posn_b, - color: color_b, + // color: color_b, }, Vertex2D { position: posn_bl, - color: color_i, + // color: color_i, }, Vertex2D { position: posn_l, - color: color_v, + // color: color_v, }, ], indices: vec![ @@ -147,6 +151,7 @@ mod rainbow { 0, 8, 1, // L ], }, + shader: Shader::Solid(Color::BLACK), }; renderer.with_translation(Vector::new(b.x, b.y), |renderer| { diff --git a/examples/modern_art/Cargo.toml b/examples/modern_art/Cargo.toml new file mode 100644 index 00000000..4995e9a7 --- /dev/null +++ b/examples/modern_art/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "modern_art" +version = "0.1.0" +authors = ["Bingus "] +edition = "2021" +publish = false + +[dependencies] +iced = { path = "../..", features = ["canvas", "tokio", "debug"] } +rand = "0.8.5" \ No newline at end of file diff --git a/examples/modern_art/src/main.rs b/examples/modern_art/src/main.rs new file mode 100644 index 00000000..17149cbe --- /dev/null +++ b/examples/modern_art/src/main.rs @@ -0,0 +1,141 @@ +use rand::{Rng, thread_rng}; +use crate::canvas::{Cursor, FillStyle, Geometry, Gradient}; +use iced::widget::canvas::{Cache, Fill, Frame}; +use iced::widget::{canvas, Canvas}; +use iced::Settings; +use iced::{ + executor, Application, Color, Command, Element, Length, Point, Rectangle, + Renderer, Size, Theme, +}; + +fn main() -> iced::Result { + ModernArt::run(Settings { + antialiasing: true, + ..Settings::default() + }) +} + +#[derive(Debug, Clone, Copy)] +enum Message {} + +struct ModernArt { + cache: Cache, +} + +impl Application for ModernArt { + type Executor = executor::Default; + type Message = Message; + type Theme = Theme; + type Flags = (); + + fn new(_flags: Self::Flags) -> (Self, Command) { + ( + ModernArt { + cache: Default::default(), + }, + Command::none(), + ) + } + + fn title(&self) -> String { + String::from("Modern Art") + } + + fn update(&mut self, _message: Self::Message) -> Command { + Command::none() + } + + fn view(&self) -> Element<'_, Self::Message, Renderer> { + Canvas::new(self) + .width(Length::Fill) + .height(Length::Fill) + .into() + } +} + +impl canvas::Program for ModernArt { + type State = (); + + fn draw( + &self, + _state: &Self::State, + _theme: &Theme, + bounds: Rectangle, + _cursor: Cursor, + ) -> Vec { + let geometry = self.cache.draw(bounds.size(), |frame| { + let num_squares = thread_rng().gen_range(0..1200); + + let mut i = 0; + while i <= num_squares { + generate_box(frame, bounds.size()); + i += 1; + } + }); + + vec![geometry] + } +} + +fn generate_box(frame: &mut Frame, bounds: Size) -> bool { + let solid = rand::random::(); + + let random_color = || -> Color { + Color::from_rgb( + thread_rng().gen_range(0.0..1.0), + thread_rng().gen_range(0.0..1.0), + thread_rng().gen_range(0.0..1.0), + ) + }; + + let gradient = |top_left: Point, bottom_right: Point| -> Gradient { + let mut builder = Gradient::linear(top_left, bottom_right); + let stops = thread_rng().gen_range(1..64u32); + + let mut i = 0; + while i <= stops { + builder = builder.add_stop( + i as f32 / stops as f32, + random_color() + ); + i += 1; + } + + builder.build().unwrap() + }; + + let top_left = Point::new( + thread_rng().gen_range(0.0..bounds.width), + thread_rng().gen_range(0.0..bounds.height) + ); + + let size = Size::new( + thread_rng().gen_range(50.0..200.0), + thread_rng().gen_range(50.0..200.0), + ); + + if solid { + frame.fill_rectangle( + top_left, + size, + Fill { + style: FillStyle::Solid(random_color()), + .. Default::default() + } + ); + } else { + frame.fill_rectangle( + top_left, + size, + Fill { + style: FillStyle::Gradient(&gradient( + top_left, + Point::new(top_left.x + size.width, top_left.y + size.height) + )), + .. Default::default() + } + ); + }; + + solid +} \ No newline at end of file diff --git a/examples/solar_system/src/main.rs b/examples/solar_system/src/main.rs index c59d73a8..fcd20561 100644 --- a/examples/solar_system/src/main.rs +++ b/examples/solar_system/src/main.rs @@ -19,6 +19,7 @@ use iced::{ }; use std::time::Instant; +use crate::canvas::StrokeStyle; pub fn main() -> iced::Result { SolarSystem::run(Settings { @@ -37,9 +38,9 @@ enum Message { } impl Application for SolarSystem { + type Executor = executor::Default; type Message = Message; type Theme = Theme; - type Executor = executor::Default; type Flags = (); fn new(_flags: ()) -> (Self, Command) { @@ -65,10 +66,6 @@ impl Application for SolarSystem { Command::none() } - fn subscription(&self) -> Subscription { - time::every(std::time::Duration::from_millis(10)).map(Message::Tick) - } - fn view(&self) -> Element { canvas(&self.state) .width(Length::Fill) @@ -86,6 +83,10 @@ impl Application for SolarSystem { text_color: Color::WHITE, }) } + + fn subscription(&self) -> Subscription { + time::every(std::time::Duration::from_millis(10)).map(Message::Tick) + } } #[derive(Debug)] @@ -178,8 +179,8 @@ impl canvas::Program for State { frame.stroke( &orbit, Stroke { + style: StrokeStyle::Solid(Color::from_rgba8(0, 153, 255, 0.1)), width: 1.0, - color: Color::from_rgba8(0, 153, 255, 0.1), line_dash: canvas::LineDash { offset: 0, segments: &[3.0, 6.0], diff --git a/glow/src/backend.rs b/glow/src/backend.rs index 78d4229e..6fc4fb38 100644 --- a/glow/src/backend.rs +++ b/glow/src/backend.rs @@ -1,7 +1,6 @@ -use crate::program; +use crate::{program, triangle}; use crate::quad; use crate::text; -use crate::triangle; use crate::{Settings, Transformation, Viewport}; use iced_graphics::backend; @@ -100,16 +99,16 @@ impl Backend { ); } - if !layer.meshes.is_empty() { + if !layer.meshes.0.is_empty() { let scaled = transformation * Transformation::scale(scale_factor, scale_factor); self.triangle_pipeline.draw( + &layer.meshes, gl, target_height, scaled, scale_factor, - &layer.meshes, ); } diff --git a/glow/src/shader/common/gradient.frag b/glow/src/shader/common/gradient.frag new file mode 100644 index 00000000..588f63e0 --- /dev/null +++ b/glow/src/shader/common/gradient.frag @@ -0,0 +1,48 @@ +// GLSL does not support dynamically sized arrays without SSBOs +#define MAX_STOPS 64 + +#ifdef GL_ES +#ifdef GL_FRAGMENT_PRECISION_HIGH +precision highp float; +#else +precision mediump float; +#endif +#endif + +#ifdef HIGHER_THAN_300 +layout (location = 0) out vec4 fragColor; +#define gl_FragColor fragColor +#endif + +in vec2 raw_position; + +uniform vec2 gradient_start; +uniform vec2 gradient_end; + +uniform uint color_stops_size; +uniform float color_stop_offsets[MAX_STOPS]; +uniform vec4 color_stop_colors[MAX_STOPS]; + +void main() { + vec2 gradient_vec = vec2(gradient_end - gradient_start); + vec2 current_vec = vec2(raw_position.xy - gradient_start); + vec2 unit = normalize(gradient_vec); + float coord_offset = dot(unit, current_vec) / length(gradient_vec); + + for (uint i = 0; i < color_stops_size - 1; i++) { + float stop_offset = color_stop_offsets[i]; + float next_stop_offset = color_stop_offsets[i + 1]; + + if (stop_offset <= coord_offset && coord_offset <= next_stop_offset) { + fragColor = mix(color_stop_colors[i], color_stop_colors[i+1], smoothstep( + stop_offset, + next_stop_offset, + coord_offset + )); + } else if (coord_offset < color_stop_offsets[0]) { + fragColor = color_stop_colors[0]; + } else if (coord_offset > color_stop_offsets[color_stops_size - 1]) { + fragColor = color_stop_colors[color_stops_size - 1]; + } + } +} \ No newline at end of file diff --git a/glow/src/shader/common/triangle.frag b/glow/src/shader/common/triangle.frag index e8689f2e..0ee65239 100644 --- a/glow/src/shader/common/triangle.frag +++ b/glow/src/shader/common/triangle.frag @@ -11,8 +11,8 @@ out vec4 fragColor; #define gl_FragColor fragColor #endif -in vec4 v_Color; +uniform vec4 color; void main() { - gl_FragColor = v_Color; + fragColor = color; } \ No newline at end of file diff --git a/glow/src/shader/common/triangle.vert b/glow/src/shader/common/triangle.vert index d0494a5f..09a4e324 100644 --- a/glow/src/shader/common/triangle.vert +++ b/glow/src/shader/common/triangle.vert @@ -1,11 +1,9 @@ uniform mat4 u_Transform; in vec2 i_Position; -in vec4 i_Color; - -out vec4 v_Color; +out vec2 raw_position; void main() { gl_Position = u_Transform * vec4(i_Position, 0.0, 1.0); - v_Color = i_Color; + raw_position = i_Position; } \ No newline at end of file diff --git a/glow/src/triangle.rs b/glow/src/triangle.rs index ae4f83ef..7d0e14c7 100644 --- a/glow/src/triangle.rs +++ b/glow/src/triangle.rs @@ -1,66 +1,41 @@ -//! Draw meshes of triangles. +//! Draw meshes of triangle. +mod gradient; +mod solid; + use crate::program::{self, Shader}; use crate::Transformation; use glow::HasContext; -use iced_graphics::layer; +use iced_graphics::layer::{Mesh, Meshes}; +use iced_graphics::shader; use std::marker::PhantomData; +use crate::triangle::gradient::GradientProgram; +use crate::triangle::solid::SolidProgram; pub use iced_graphics::triangle::{Mesh2D, Vertex2D}; -const VERTEX_BUFFER_SIZE: usize = 10_000; -const INDEX_BUFFER_SIZE: usize = 10_000; - #[derive(Debug)] pub(crate) struct Pipeline { - program: ::Program, vertex_array: ::VertexArray, vertices: Buffer, indices: Buffer, - transform_location: ::UniformLocation, current_transform: Transformation, + programs: TrianglePrograms, } -impl Pipeline { - pub fn new( - gl: &glow::Context, - shader_version: &program::Version, - ) -> Pipeline { - let program = unsafe { - let vertex_shader = Shader::vertex( - gl, - shader_version, - include_str!("shader/common/triangle.vert"), - ); - let fragment_shader = Shader::fragment( - gl, - shader_version, - include_str!("shader/common/triangle.frag"), - ); - - program::create( - gl, - &[vertex_shader, fragment_shader], - &[(0, "i_Position"), (1, "i_Color")], - ) - }; - - let transform_location = - unsafe { gl.get_uniform_location(program, "u_Transform") } - .expect("Get transform location"); - - unsafe { - gl.use_program(Some(program)); - - let transform: [f32; 16] = Transformation::identity().into(); - gl.uniform_matrix_4_f32_slice( - Some(&transform_location), - false, - &transform, - ); +#[derive(Debug)] +struct TrianglePrograms { + solid: TriangleProgram, + gradient: TriangleProgram, +} - gl.use_program(None); - } +#[derive(Debug)] +enum TriangleProgram { + Solid(SolidProgram), + Gradient(GradientProgram), +} +impl Pipeline { + pub fn new(gl: &glow::Context, shader_version: &program::Version) -> Self { let vertex_array = unsafe { gl.create_vertex_array().expect("Create vertex array") }; @@ -73,7 +48,7 @@ impl Pipeline { gl, glow::ARRAY_BUFFER, glow::DYNAMIC_DRAW, - VERTEX_BUFFER_SIZE, + std::mem::size_of::() as usize, ) }; @@ -82,7 +57,7 @@ impl Pipeline { gl, glow::ELEMENT_ARRAY_BUFFER, glow::DYNAMIC_DRAW, - INDEX_BUFFER_SIZE, + std::mem::size_of::() as usize, ) }; @@ -92,58 +67,45 @@ impl Pipeline { gl.enable_vertex_attrib_array(0); gl.vertex_attrib_pointer_f32(0, 2, glow::FLOAT, false, stride, 0); - gl.enable_vertex_attrib_array(1); - gl.vertex_attrib_pointer_f32( - 1, - 4, - glow::FLOAT, - false, - stride, - 4 * 2, - ); - gl.bind_vertex_array(None); - } + }; - Pipeline { - program, + Self { vertex_array, vertices, indices, - transform_location, current_transform: Transformation::identity(), + programs: TrianglePrograms { + solid: TriangleProgram::Solid(SolidProgram::new( + gl, + shader_version, + )), + gradient: TriangleProgram::Gradient(GradientProgram::new( + gl, + shader_version, + )), + }, } } pub fn draw( &mut self, + meshes: &Meshes<'_>, gl: &glow::Context, target_height: u32, transformation: Transformation, scale_factor: f32, - meshes: &[layer::Mesh<'_>], ) { unsafe { gl.enable(glow::MULTISAMPLE); gl.enable(glow::SCISSOR_TEST); - gl.use_program(Some(self.program)); - gl.bind_vertex_array(Some(self.vertex_array)); + gl.bind_vertex_array(Some(self.vertex_array)) } - // This looks a bit crazy, but we are just counting how many vertices - // and indices we will need to handle. - // TODO: Improve readability - let (total_vertices, total_indices) = meshes - .iter() - .map(|layer::Mesh { buffers, .. }| { - (buffers.vertices.len(), buffers.indices.len()) - }) - .fold((0, 0), |(total_v, total_i), (v, i)| { - (total_v + v, total_i + i) - }); - - // Then we ensure the current buffers are big enough, resizing if - // necessary + //count the total number of vertices & indices we need to handle for all meshes + let (total_vertices, total_indices) = meshes.attribute_count(); + + // Then we ensure the current attribute buffers are big enough, resizing if necessary unsafe { self.vertices.bind(gl, total_vertices); self.indices.bind(gl, total_indices); @@ -153,7 +115,7 @@ impl Pipeline { let mut last_vertex = 0; let mut last_index = 0; - for layer::Mesh { buffers, .. } in meshes { + for Mesh { buffers, .. } in meshes.0.iter() { unsafe { gl.buffer_sub_data_u8_slice( glow::ARRAY_BUFFER, @@ -176,11 +138,12 @@ impl Pipeline { let mut last_vertex = 0; let mut last_index = 0; - for layer::Mesh { + for Mesh { buffers, origin, clip_bounds, - } in meshes + shader, + } in meshes.0.iter() { let transform = transformation * Transformation::translate(origin.x, origin.y); @@ -188,17 +151,6 @@ impl Pipeline { let clip_bounds = (*clip_bounds * scale_factor).snap(); unsafe { - if self.current_transform != transform { - let matrix: [f32; 16] = transform.into(); - gl.uniform_matrix_4_f32_slice( - Some(&self.transform_location), - false, - &matrix, - ); - - self.current_transform = transform; - } - gl.scissor( clip_bounds.x as i32, (target_height - (clip_bounds.y + clip_bounds.height)) @@ -207,6 +159,15 @@ impl Pipeline { clip_bounds.height as i32, ); + let t = if self.current_transform != transform { + self.current_transform = transform; + Some(transform) + } else { + None + }; + + self.use_with_shader(gl, shader, t); + gl.draw_elements_base_vertex( glow::TRIANGLES, buffers.indices.len() as i32, @@ -222,34 +183,79 @@ impl Pipeline { unsafe { gl.bind_vertex_array(None); - gl.use_program(None); gl.disable(glow::SCISSOR_TEST); gl.disable(glow::MULTISAMPLE); } } -} -#[repr(C)] -#[derive(Debug, Clone, Copy)] -struct Uniforms { - transform: [f32; 16], + fn use_with_shader( + &mut self, + gl: &glow::Context, + shader: &shader::Shader, + transform: Option, + ) { + match shader { + shader::Shader::Solid(color) => { + if let TriangleProgram::Solid(solid_program) = + &mut self.programs.solid + { + unsafe { gl.use_program(Some(solid_program.program)) } + solid_program.set_uniforms(gl, color, transform); + } + } + shader::Shader::Gradient(gradient) => { + if let TriangleProgram::Gradient(gradient_program) = + &mut self.programs.gradient + { + unsafe { gl.use_program(Some(gradient_program.program)) } + gradient_program.set_uniforms(gl, gradient, transform); + } + } + } + } } -unsafe impl bytemuck::Zeroable for Uniforms {} -unsafe impl bytemuck::Pod for Uniforms {} - -impl Default for Uniforms { - fn default() -> Self { - Self { - transform: *Transformation::identity().as_ref(), - } +/// A simple shader program. Uses [`triangle.vert`] for its vertex shader and only binds position +/// attribute location. +pub(super) fn simple_triangle_program( + gl: &glow::Context, + shader_version: &program::Version, + fragment_shader: &'static str, +) -> ::Program { + unsafe { + let vertex_shader = Shader::vertex( + gl, + shader_version, + include_str!("shader/common/triangle.vert"), + ); + + let fragment_shader = + Shader::fragment(gl, shader_version, fragment_shader); + + program::create( + gl, + &[vertex_shader, fragment_shader], + &[(0, "i_Position")], + ) } } -impl From for Uniforms { - fn from(transformation: Transformation) -> Uniforms { - Self { - transform: transformation.into(), +pub(super) fn update_transform( + gl: &glow::Context, + program: ::Program, + transform: Option +) { + if let Some(t) = transform { + let transform_location = + unsafe { gl.get_uniform_location(program, "u_Transform") } + .expect("Get transform location."); + + unsafe { + gl.uniform_matrix_4_f32_slice( + Some(&transform_location), + false, + t.as_ref(), + ); } } } diff --git a/glow/src/triangle/gradient.rs b/glow/src/triangle/gradient.rs new file mode 100644 index 00000000..d1b10d77 --- /dev/null +++ b/glow/src/triangle/gradient.rs @@ -0,0 +1,189 @@ +use crate::program::Version; +use crate::triangle::{simple_triangle_program, update_transform}; +use glow::{Context, HasContext, NativeProgram}; +use iced_graphics::gradient::Gradient; +use iced_graphics::widget::canvas::gradient::Linear; +use iced_graphics::Transformation; + +#[derive(Debug)] +pub(super) struct GradientProgram { + pub(super) program: ::Program, + pub(super) uniform_data: GradientUniformData, +} + +impl GradientProgram { + pub(super) fn new(gl: &Context, shader_version: &Version) -> Self { + let program = simple_triangle_program( + gl, + shader_version, + include_str!("../shader/common/gradient.frag"), + ); + + Self { + program, + uniform_data: GradientUniformData::new(gl, program), + } + } + + pub(super) fn set_uniforms<'a>( + &mut self, + gl: &Context, + gradient: &Gradient, + transform: Option, + ) { + update_transform(gl, self.program, transform); + + if &self.uniform_data.current_gradient != gradient { + match gradient { + Gradient::Linear(linear) => { + let gradient_start: [f32; 2] = (linear.start).into(); + let gradient_end: [f32; 2] = (linear.end).into(); + + unsafe { + gl.uniform_2_f32( + Some( + &self + .uniform_data + .uniform_locations + .gradient_start_location, + ), + gradient_start[0], + gradient_start[1], + ); + + gl.uniform_2_f32( + Some( + &self + .uniform_data + .uniform_locations + .gradient_end_location, + ), + gradient_end[0], + gradient_end[1], + ); + + gl.uniform_1_u32( + Some( + &self + .uniform_data + .uniform_locations + .color_stops_size_location, + ), + linear.color_stops.len() as u32, + ); + + for (index, stop) in + linear.color_stops.iter().enumerate() + { + gl.uniform_1_f32( + Some( + &self + .uniform_data + .uniform_locations + .color_stops_locations[index] + .offset, + ), + stop.offset, + ); + + gl.uniform_4_f32( + Some( + &self + .uniform_data + .uniform_locations + .color_stops_locations[index] + .color, + ), + stop.color.r, + stop.color.g, + stop.color.b, + stop.color.a, + ); + } + } + } + } + + self.uniform_data.current_gradient = gradient.clone(); + } + } +} + +#[derive(Debug)] +pub(super) struct GradientUniformData { + current_gradient: Gradient, + uniform_locations: GradientUniformLocations, +} + +#[derive(Debug)] +struct GradientUniformLocations { + gradient_start_location: ::UniformLocation, + gradient_end_location: ::UniformLocation, + color_stops_size_location: ::UniformLocation, + //currently the maximum number of stops is 64 due to needing to allocate the + //memory for the array of stops with a const value in GLSL + color_stops_locations: [ColorStopLocation; 64], +} + +#[derive(Copy, Debug, Clone)] +struct ColorStopLocation { + color: ::UniformLocation, + offset: ::UniformLocation, +} + +impl GradientUniformData { + fn new(gl: &Context, program: NativeProgram) -> Self { + let gradient_start_location = + unsafe { gl.get_uniform_location(program, "gradient_start") } + .expect("Gradient - Get gradient_start."); + + let gradient_end_location = + unsafe { gl.get_uniform_location(program, "gradient_end") } + .expect("Gradient - Get gradient_end."); + + let color_stops_size_location = + unsafe { gl.get_uniform_location(program, "color_stops_size") } + .expect("Gradient - Get color_stops_size."); + + let color_stops_locations: [ColorStopLocation; 64] = + core::array::from_fn(|index| { + let offset = unsafe { + gl.get_uniform_location( + program, + &format!("color_stop_offsets[{}]", index), + ) + } + .expect(&format!( + "Gradient - Color stop offset with index {}", + index + )); + + let color = unsafe { + gl.get_uniform_location( + program, + &format!("color_stop_colors[{}]", index), + ) + } + .expect(&format!( + "Gradient - Color stop colors with index {}", + index + )); + + ColorStopLocation { color, offset } + }); + + GradientUniformData { + current_gradient: Gradient::Linear(Linear { + start: Default::default(), + end: Default::default(), + color_stops: vec![], + }), + uniform_locations: GradientUniformLocations { + gradient_start_location, + gradient_end_location, + color_stops_size_location, + color_stops_locations, + }, + } + } +} diff --git a/glow/src/triangle/solid.rs b/glow/src/triangle/solid.rs new file mode 100644 index 00000000..3a33cea8 --- /dev/null +++ b/glow/src/triangle/solid.rs @@ -0,0 +1,67 @@ +use crate::program::Version; +use crate::triangle::{simple_triangle_program, update_transform}; +use crate::Color; +use glow::{Context, HasContext, NativeProgram}; +use iced_graphics::Transformation; + +#[derive(Debug)] +pub struct SolidProgram { + pub(crate) program: ::Program, + pub(crate) uniform_data: SolidUniformData, +} + +impl SolidProgram { + pub fn new(gl: &Context, shader_version: &Version) -> Self { + let program = simple_triangle_program( + gl, + shader_version, + include_str!("../shader/common/triangle.frag"), + ); + + Self { + program, + uniform_data: SolidUniformData::new(gl, program), + } + } + + pub fn set_uniforms<'a>( + &mut self, + gl: &Context, + color: &Color, + transform: Option, + ) { + update_transform(gl, self.program, transform); + + if &self.uniform_data.color != color { + unsafe { + gl.uniform_4_f32( + Some(&self.uniform_data.color_location), + color.r, + color.g, + color.b, + color.a, + ); + } + + self.uniform_data.color = *color; + } + } +} + +#[derive(Debug)] +pub(crate) struct SolidUniformData { + pub color: Color, + pub color_location: ::UniformLocation, +} + +impl SolidUniformData { + fn new(gl: &Context, program: NativeProgram) -> Self { + Self { + color: Color::TRANSPARENT, + color_location: unsafe { + gl.get_uniform_location(program, "color") + } + .expect("Solid - Color uniform location."), + } + } +} diff --git a/glow/src/window/compositor.rs b/glow/src/window/compositor.rs index f6afaa68..e9cf6015 100644 --- a/glow/src/window/compositor.rs +++ b/glow/src/window/compositor.rs @@ -26,8 +26,7 @@ impl iced_graphics::window::GLCompositor for Compositor { log::info!("{:#?}", settings); let version = gl.version(); - log::info!("Version: {:?}", version); - log::info!("Embedded: {}", version.is_embedded); + log::info!("OpenGL version: {:?} (Embedded: {}", version, version.is_embedded); let renderer = gl.get_parameter_string(glow::RENDERER); log::info!("Renderer: {}", renderer); diff --git a/graphics/Cargo.toml b/graphics/Cargo.toml index 49d4d9c6..ff6fcd4a 100644 --- a/graphics/Cargo.toml +++ b/graphics/Cargo.toml @@ -19,7 +19,7 @@ font-icons = [] opengl = [] [dependencies] -glam = "0.10" +glam = "0.21.3" raw-window-handle = "0.4" thiserror = "1.0" diff --git a/graphics/src/gradient.rs b/graphics/src/gradient.rs new file mode 100644 index 00000000..4d1eff62 --- /dev/null +++ b/graphics/src/gradient.rs @@ -0,0 +1,23 @@ +//! For creating a Gradient. + +use iced_native::Color; +use crate::widget::canvas::gradient::Linear; + +#[derive(Debug, Clone, PartialEq)] +/// A fill which transitions colors progressively along a direction, either linearly, radially, +/// or conically. +pub enum Gradient { + /// A linear gradient interpolates colors along a direction from its [`start`] to its [`end`] + /// point. + Linear(Linear), +} + + +#[derive(Debug, Clone, Copy, PartialEq)] +/// A point along the gradient vector where the specified [`color`] is unmixed. +pub struct ColorStop { + /// Offset along the gradient vector. + pub offset: f32, + /// The color of the gradient at the specified [`offset`]. + pub color: Color, +} \ No newline at end of file diff --git a/graphics/src/layer.rs b/graphics/src/layer.rs index af545713..b7731922 100644 --- a/graphics/src/layer.rs +++ b/graphics/src/layer.rs @@ -7,9 +7,10 @@ use crate::{ use iced_native::image; use iced_native::svg; +use crate::shader::Shader; /// A group of primitives that should be clipped together. -#[derive(Debug, Clone)] +#[derive(Debug)] pub struct Layer<'a> { /// The clipping bounds of the [`Layer`]. pub bounds: Rectangle, @@ -18,7 +19,7 @@ pub struct Layer<'a> { pub quads: Vec, /// The triangle meshes of the [`Layer`]. - pub meshes: Vec>, + pub meshes: Meshes<'a>, /// The text of the [`Layer`]. pub text: Vec>, @@ -33,7 +34,7 @@ impl<'a> Layer<'a> { Self { bounds, quads: Vec::new(), - meshes: Vec::new(), + meshes: Meshes(Vec::new()), text: Vec::new(), images: Vec::new(), } @@ -159,7 +160,11 @@ impl<'a> Layer<'a> { border_color: border_color.into_linear(), }); } - Primitive::Mesh2D { buffers, size } => { + Primitive::Mesh2D { + buffers, + size, + shader, + } => { let layer = &mut layers[current_layer]; let bounds = Rectangle::new( @@ -169,11 +174,14 @@ impl<'a> Layer<'a> { // Only draw visible content if let Some(clip_bounds) = layer.bounds.intersection(&bounds) { - layer.meshes.push(Mesh { - origin: Point::new(translation.x, translation.y), - buffers, - clip_bounds, - }); + layer.meshes.0.push( + Mesh { + origin: Point::new(translation.x, translation.y), + buffers, + clip_bounds, + shader, + } + ); } } Primitive::Clip { bounds, content } => { @@ -270,6 +278,9 @@ pub struct Mesh<'a> { /// The clipping bounds of the [`Mesh`]. pub clip_bounds: Rectangle, + + /// The shader of the [`Mesh`]. + pub shader: &'a Shader, } /// A paragraph of text. @@ -323,3 +334,21 @@ unsafe impl bytemuck::Zeroable for Quad {} #[allow(unsafe_code)] unsafe impl bytemuck::Pod for Quad {} + +#[derive(Debug)] +/// A collection of meshes. +pub struct Meshes<'a>(pub Vec>); + +impl<'a> Meshes<'a> { + /// Returns the number of total vertices & total indices of all [`Mesh`]es. + pub fn attribute_count(&self) -> (usize, usize) { + self.0 + .iter() + .map(|Mesh { buffers, .. }| { + (buffers.vertices.len(), buffers.indices.len()) + }) + .fold((0, 0), |(total_v, total_i), (v, i)| { + (total_v + v, total_i + i) + }) + } +} \ No newline at end of file diff --git a/graphics/src/lib.rs b/graphics/src/lib.rs index 11082472..ce9b1b07 100644 --- a/graphics/src/lib.rs +++ b/graphics/src/lib.rs @@ -35,6 +35,8 @@ pub mod renderer; pub mod triangle; pub mod widget; pub mod window; +pub mod shader; +pub mod gradient; pub use antialiasing::Antialiasing; pub use backend::Backend; diff --git a/graphics/src/primitive.rs b/graphics/src/primitive.rs index 5f7a344d..4f79a74c 100644 --- a/graphics/src/primitive.rs +++ b/graphics/src/primitive.rs @@ -2,7 +2,7 @@ use iced_native::image; use iced_native::svg; use iced_native::{Background, Color, Font, Rectangle, Size, Vector}; -use crate::alignment; +use crate::{alignment, shader}; use crate::triangle; use std::sync::Arc; @@ -88,6 +88,9 @@ pub enum Primitive { /// /// Any geometry that falls out of this region will be clipped. size: Size, + + /// The shader of the mesh + shader: shader::Shader, }, /// A cached primitive. /// diff --git a/graphics/src/shader.rs b/graphics/src/shader.rs new file mode 100644 index 00000000..b9071c74 --- /dev/null +++ b/graphics/src/shader.rs @@ -0,0 +1,42 @@ +//! Supported shaders; + +use crate::{Color, widget}; +use crate::gradient::Gradient; +use crate::widget::canvas::{FillStyle, StrokeStyle}; + +#[derive(Debug, Clone)] +/// Supported shaders for primitives. +pub enum Shader { + /// Fill a primitive with a solid color. + Solid(Color), + /// Fill a primitive with an interpolated color. + Gradient(Gradient) +} + +impl <'a> Into for StrokeStyle<'a> { + fn into(self) -> Shader { + match self { + StrokeStyle::Solid(color) => Shader::Solid(color), + StrokeStyle::Gradient(gradient) => gradient.clone().into() + } + } +} + +impl <'a> Into for FillStyle<'a> { + fn into(self) -> Shader { + match self { + FillStyle::Solid(color) => Shader::Solid(color), + FillStyle::Gradient(gradient) => gradient.clone().into() + } + } +} + +impl <'a> Into for widget::canvas::Gradient { + fn into(self) -> Shader { + match self { + widget::canvas::Gradient::Linear(linear) => { + Shader::Gradient(Gradient::Linear(linear)) + } + } + } +} \ No newline at end of file diff --git a/graphics/src/transformation.rs b/graphics/src/transformation.rs index 2a19caed..03f453db 100644 --- a/graphics/src/transformation.rs +++ b/graphics/src/transformation.rs @@ -8,7 +8,7 @@ pub struct Transformation(Mat4); impl Transformation { /// Get the identity transformation. pub fn identity() -> Transformation { - Transformation(Mat4::identity()) + Transformation(Mat4::IDENTITY) } /// Creates an orthographic projection. @@ -51,3 +51,9 @@ impl From for [f32; 16] { *t.as_ref() } } + +impl Into for Transformation { + fn into(self) -> Mat4 { + self.0 + } +} \ No newline at end of file diff --git a/graphics/src/triangle.rs b/graphics/src/triangle.rs index 05028f51..92709fe2 100644 --- a/graphics/src/triangle.rs +++ b/graphics/src/triangle.rs @@ -6,20 +6,22 @@ use bytemuck::{Pod, Zeroable}; pub struct Mesh2D { /// The vertices of the mesh pub vertices: Vec, - /// The list of vertex indices that defines the triangles of the mesh. - /// - /// Therefore, this list should always have a length that is a multiple of - /// 3. pub indices: Vec, } -/// A two-dimensional vertex with some color in __linear__ RGBA. +/// A two-dimensional vertex. #[derive(Copy, Clone, Debug, Zeroable, Pod)] #[repr(C)] pub struct Vertex2D { - /// The vertex position + /// The vertex position in 2D space. pub position: [f32; 2], - /// The vertex color in __linear__ RGBA. - pub color: [f32; 4], +} + +/// Convert from lyon's position data to Iced's Vertex2D type. +impl Vertex2D { + /// Converts from [`lyon::math::Point`] to [`Vertex2D`]. Used for generating primitives. + pub fn from(points: Vec) -> Vec { + points.iter().map(|p| Vertex2D { position: [p.x, p.y]}).collect() + } } diff --git a/graphics/src/widget/canvas.rs b/graphics/src/widget/canvas.rs index 88403fd7..09aad98d 100644 --- a/graphics/src/widget/canvas.rs +++ b/graphics/src/widget/canvas.rs @@ -5,6 +5,7 @@ //! and more! pub mod event; +pub mod gradient; pub mod path; mod cache; @@ -19,12 +20,13 @@ mod text; pub use cache::Cache; pub use cursor::Cursor; pub use event::Event; -pub use fill::{Fill, FillRule}; +pub use fill::{Fill, FillRule, FillStyle}; pub use frame::Frame; pub use geometry::Geometry; +pub use gradient::Gradient; pub use path::Path; pub use program::Program; -pub use stroke::{LineCap, LineDash, LineJoin, Stroke}; +pub use stroke::{LineCap, LineDash, LineJoin, Stroke, StrokeStyle}; pub use text::Text; use crate::{Backend, Primitive, Renderer}; diff --git a/graphics/src/widget/canvas/fill.rs b/graphics/src/widget/canvas/fill.rs index 56495435..02d2311f 100644 --- a/graphics/src/widget/canvas/fill.rs +++ b/graphics/src/widget/canvas/fill.rs @@ -1,12 +1,14 @@ use iced_native::Color; +use crate::widget::canvas::Gradient; + /// The style used to fill geometry. -#[derive(Debug, Clone, Copy)] -pub struct Fill { - /// The color used to fill geometry. +#[derive(Debug, Clone)] +pub struct Fill<'a> { + /// The color or gradient of the fill. /// - /// By default, it is set to `BLACK`. - pub color: Color, + /// By default, it is set to [`FillStyle::Solid`] `BLACK`. + pub style: FillStyle<'a>, /// The fill rule defines how to determine what is inside and what is /// outside of a shape. @@ -19,24 +21,33 @@ pub struct Fill { pub rule: FillRule, } -impl Default for Fill { - fn default() -> Fill { +impl <'a> Default for Fill<'a> { + fn default() -> Fill<'a> { Fill { - color: Color::BLACK, + style: FillStyle::Solid(Color::BLACK), rule: FillRule::NonZero, } } } -impl From for Fill { - fn from(color: Color) -> Fill { +impl<'a> From for Fill<'a> { + fn from(color: Color) -> Fill<'a> { Fill { - color, + style: FillStyle::Solid(color), ..Fill::default() } } } +/// The color or gradient of a [`Fill`]. +#[derive(Debug, Clone)] +pub enum FillStyle<'a> { + /// A solid color + Solid(Color), + /// A color gradient + Gradient(&'a Gradient), +} + /// The fill rule defines how to determine what is inside and what is outside of /// a shape. /// diff --git a/graphics/src/widget/canvas/frame.rs b/graphics/src/widget/canvas/frame.rs index 516539ca..df2db98f 100644 --- a/graphics/src/widget/canvas/frame.rs +++ b/graphics/src/widget/canvas/frame.rs @@ -3,11 +3,13 @@ use std::borrow::Cow; use iced_native::{Point, Rectangle, Size, Vector}; use crate::triangle; -use crate::widget::canvas::path; -use crate::widget::canvas::{Fill, Geometry, Path, Stroke, Text}; +use crate::widget::canvas::{Fill, Geometry, Path, Stroke, Text, path}; use crate::Primitive; +use crate::triangle::{Vertex2D}; +use crate::shader::Shader; use lyon::tessellation; +use lyon::tessellation::geometry_builder::Positions; /// The frame of a [`Canvas`]. /// @@ -15,7 +17,7 @@ use lyon::tessellation; #[allow(missing_debug_implementations)] pub struct Frame { size: Size, - buffers: lyon::tessellation::VertexBuffers, + buffers: Vec<(tessellation::VertexBuffers, Shader)>, primitives: Vec, transforms: Transforms, fill_tessellator: tessellation::FillTessellator, @@ -42,7 +44,7 @@ impl Frame { pub fn new(size: Size) -> Frame { Frame { size, - buffers: lyon::tessellation::VertexBuffers::new(), + buffers: Vec::new(), primitives: Vec::new(), transforms: Transforms { previous: Vec::new(), @@ -82,18 +84,18 @@ impl Frame { /// Draws the given [`Path`] on the [`Frame`] by filling it with the /// provided style. - pub fn fill(&mut self, path: &Path, fill: impl Into) { - let Fill { color, rule } = fill.into(); + pub fn fill<'a>(&mut self, path: &Path, fill: impl Into>) { + let Fill { style, rule } = fill.into(); - let mut buffers = tessellation::BuffersBuilder::new( - &mut self.buffers, - FillVertex(color.into_linear()), - ); + let mut buf = tessellation::VertexBuffers::new(); + + let mut buffers = + tessellation::BuffersBuilder::new(&mut buf, Positions); let options = tessellation::FillOptions::default().with_fill_rule(rule.into()); - let result = if self.transforms.current.is_identity { + if self.transforms.current.is_identity { self.fill_tessellator.tessellate_path( path.raw(), &options, @@ -107,25 +109,24 @@ impl Frame { &options, &mut buffers, ) - }; + }.expect("Tessellate path."); - result.expect("Tessellate path"); + self.buffers.push((buf, style.into())) } /// Draws an axis-aligned rectangle given its top-left corner coordinate and /// its `Size` on the [`Frame`] by filling it with the provided style. - pub fn fill_rectangle( + pub fn fill_rectangle<'a>( &mut self, top_left: Point, size: Size, - fill: impl Into, + fill: impl Into>, ) { - let Fill { color, rule } = fill.into(); + let Fill { style, rule } = fill.into(); + + let mut buf = tessellation::VertexBuffers::new(); - let mut buffers = tessellation::BuffersBuilder::new( - &mut self.buffers, - FillVertex(color.into_linear()), - ); + let mut buffers = tessellation::BuffersBuilder::new(&mut buf, Positions); let top_left = self.transforms.current.raw.transform_point( @@ -147,6 +148,8 @@ impl Frame { &mut buffers, ) .expect("Fill rectangle"); + + self.buffers.push((buf, style.into())) } /// Draws the stroke of the given [`Path`] on the [`Frame`] with the @@ -154,10 +157,9 @@ impl Frame { pub fn stroke<'a>(&mut self, path: &Path, stroke: impl Into>) { let stroke = stroke.into(); - let mut buffers = tessellation::BuffersBuilder::new( - &mut self.buffers, - StrokeVertex(stroke.color.into_linear()), - ); + let mut buf = tessellation::VertexBuffers::new(); + + let mut buffers = tessellation::BuffersBuilder::new(&mut buf, Positions); let mut options = tessellation::StrokeOptions::default(); options.line_width = stroke.width; @@ -171,7 +173,7 @@ impl Frame { Cow::Owned(path::dashed(path, stroke.line_dash)) }; - let result = if self.transforms.current.is_identity { + if self.transforms.current.is_identity { self.stroke_tessellator.tessellate_path( path.raw(), &options, @@ -185,9 +187,9 @@ impl Frame { &options, &mut buffers, ) - }; + }.expect("Stroke path"); - result.expect("Stroke path"); + self.buffers.push((buf, stroke.style.into())) } /// Draws the characters of the given [`Text`] on the [`Frame`], filling @@ -331,52 +333,19 @@ impl Frame { } fn into_primitives(mut self) -> Vec { - if !self.buffers.indices.is_empty() { - self.primitives.push(Primitive::Mesh2D { - buffers: triangle::Mesh2D { - vertices: self.buffers.vertices, - indices: self.buffers.indices, - }, - size: self.size, - }); + if !self.primitives.is_empty() { + for (buffer, shader) in self.buffers { + self.primitives.push(Primitive::Mesh2D { + buffers: triangle::Mesh2D { + vertices: Vertex2D::from(buffer.vertices), + indices: buffer.indices + }, + size: self.size, + shader + }) + } } self.primitives } } - -struct FillVertex([f32; 4]); - -impl lyon::tessellation::FillVertexConstructor - for FillVertex -{ - fn new_vertex( - &mut self, - vertex: lyon::tessellation::FillVertex<'_>, - ) -> triangle::Vertex2D { - let position = vertex.position(); - - triangle::Vertex2D { - position: [position.x, position.y], - color: self.0, - } - } -} - -struct StrokeVertex([f32; 4]); - -impl lyon::tessellation::StrokeVertexConstructor - for StrokeVertex -{ - fn new_vertex( - &mut self, - vertex: lyon::tessellation::StrokeVertex<'_, '_>, - ) -> triangle::Vertex2D { - let position = vertex.position(); - - triangle::Vertex2D { - position: [position.x, position.y], - color: self.0, - } - } -} diff --git a/graphics/src/widget/canvas/gradient.rs b/graphics/src/widget/canvas/gradient.rs new file mode 100644 index 00000000..7d2daabc --- /dev/null +++ b/graphics/src/widget/canvas/gradient.rs @@ -0,0 +1,21 @@ +//! Define a color gradient. +use iced_native::Point; + +pub mod linear; + +pub use linear::Linear; + +/// A gradient that can be used in the style of [`super::Fill`] or [`super::Stroke`]. +#[derive(Debug, Clone)] +pub enum Gradient { + /// A linear gradient + Linear(Linear), + //TODO: radial, conical +} + +impl Gradient { + /// Creates a new linear [`linear::Builder`]. + pub fn linear(start: Point, end: Point) -> linear::Builder { + linear::Builder::new(start, end) + } +} \ No newline at end of file diff --git a/graphics/src/widget/canvas/gradient/linear.rs b/graphics/src/widget/canvas/gradient/linear.rs new file mode 100644 index 00000000..37533e19 --- /dev/null +++ b/graphics/src/widget/canvas/gradient/linear.rs @@ -0,0 +1,73 @@ +//! A linear color gradient. +use iced_native::{Color, Point}; + +use crate::gradient::ColorStop; + +use super::Gradient; + +/// A linear gradient that can be used in the style of [`super::Fill`] or [`super::Stroke`]. +#[derive(Debug, Clone, PartialEq)] +pub struct Linear { + /// The point where the linear gradient begins. + pub start: Point, + /// The point where the linear gradient ends. + pub end: Point, + /// [`ColorStop`]s along the linear gradient path. + pub color_stops: Vec, +} + +/// A [`Linear`] builder. +#[derive(Debug)] +pub struct Builder { + start: Point, + end: Point, + stops: Vec<(f32, Color)>, + valid: bool, +} + +impl Builder { + /// Creates a new [`Builder`]. + pub fn new(start: Point, end: Point) -> Self { + Self { + start, + end, + stops: vec![], + valid: true, + } + } + + /// Adds a new stop, defined by an offset and a color, to the gradient. + /// + /// `offset` must be between `0.0` and `1.0`. + pub fn add_stop(mut self, offset: f32, color: Color) -> Self { + if !(0.0..=1.0).contains(&offset) { + self.valid = false; + } + + self.stops.push((offset, color)); + self + } + + /// Builds the linear [`Gradient`] of this [`Builder`]. + /// + /// Returns `None` if no stops were added to the builder or + /// if stops not between 0.0 and 1.0 were added. + pub fn build(self) -> Option { + if self.stops.is_empty() || !self.valid { + return None; + } + + Some(Gradient::Linear(Linear { + start: self.start, + end: self.end, + color_stops: self + .stops + .into_iter() + .map(|f| ColorStop { + offset: f.0, + color: f.1, + }) + .collect(), + })) + } +} diff --git a/graphics/src/widget/canvas/stroke.rs b/graphics/src/widget/canvas/stroke.rs index 6accc2fb..c319b398 100644 --- a/graphics/src/widget/canvas/stroke.rs +++ b/graphics/src/widget/canvas/stroke.rs @@ -1,10 +1,14 @@ use iced_native::Color; +use crate::widget::canvas::Gradient; + /// The style of a stroke. -#[derive(Debug, Clone, Copy)] +#[derive(Debug, Clone)] pub struct Stroke<'a> { - /// The color of the stroke. - pub color: Color, + /// The color or gradient of the stroke. + /// + /// By default, it is set to [`StrokeStyle::Solid`] `BLACK`. + pub style: StrokeStyle<'a>, /// The distance between the two edges of the stroke. pub width: f32, /// The shape to be used at the end of open subpaths when they are stroked. @@ -19,7 +23,10 @@ pub struct Stroke<'a> { impl<'a> Stroke<'a> { /// Sets the color of the [`Stroke`]. pub fn with_color(self, color: Color) -> Self { - Stroke { color, ..self } + Stroke { + style: StrokeStyle::Solid(color), + ..self + } } /// Sets the width of the [`Stroke`]. @@ -41,7 +48,7 @@ impl<'a> Stroke<'a> { impl<'a> Default for Stroke<'a> { fn default() -> Self { Stroke { - color: Color::BLACK, + style: StrokeStyle::Solid(Color::BLACK), width: 1.0, line_cap: LineCap::default(), line_join: LineJoin::default(), @@ -50,6 +57,15 @@ impl<'a> Default for Stroke<'a> { } } +/// The color or gradient of a [`Stroke`]. +#[derive(Debug, Clone, Copy)] +pub enum StrokeStyle<'a> { + /// A solid color + Solid(Color), + /// A color gradient + Gradient(&'a Gradient), +} + /// The shape used at the end of open subpaths when they are stroked. #[derive(Debug, Clone, Copy)] pub enum LineCap { diff --git a/wgpu/Cargo.toml b/wgpu/Cargo.toml index 586f97d3..7174f80c 100644 --- a/wgpu/Cargo.toml +++ b/wgpu/Cargo.toml @@ -69,6 +69,13 @@ optional = true version = "0.6" optional = true +[dependencies.encase] +version = "0.3.0" +features = ["glam"] + +[dependencies.glam] +version = "0.21.3" + [package.metadata.docs.rs] rustdoc-args = ["--cfg", "docsrs"] all-features = true diff --git a/wgpu/src/backend.rs b/wgpu/src/backend.rs index 8c875254..fd688004 100644 --- a/wgpu/src/backend.rs +++ b/wgpu/src/backend.rs @@ -94,8 +94,7 @@ impl Backend { staging_belt, encoder, frame, - target_size.width, - target_size.height, + target_size ); } @@ -112,8 +111,7 @@ impl Backend { staging_belt: &mut wgpu::util::StagingBelt, encoder: &mut wgpu::CommandEncoder, target: &wgpu::TextureView, - target_width: u32, - target_height: u32, + target_size: Size, ) { let bounds = (layer.bounds * scale_factor).snap(); @@ -134,7 +132,7 @@ impl Backend { ); } - if !layer.meshes.is_empty() { + if !layer.meshes.0.is_empty() { let scaled = transformation * Transformation::scale(scale_factor, scale_factor); @@ -143,8 +141,7 @@ impl Backend { staging_belt, encoder, target, - target_width, - target_height, + target_size, scaled, scale_factor, &layer.meshes, diff --git a/wgpu/src/buffers.rs b/wgpu/src/buffers.rs new file mode 100644 index 00000000..f94d175d --- /dev/null +++ b/wgpu/src/buffers.rs @@ -0,0 +1,3 @@ +//! Utilities for buffer operations. +pub mod buffer; +pub mod dynamic_buffers; \ No newline at end of file diff --git a/wgpu/src/buffers/buffer.rs b/wgpu/src/buffers/buffer.rs new file mode 100644 index 00000000..dae3b038 --- /dev/null +++ b/wgpu/src/buffers/buffer.rs @@ -0,0 +1,91 @@ +//! Utilities for static buffer operations. + +/// A generic buffer struct useful for items which have no alignment requirements +/// (e.g. Vertex, Index buffers) and are set once and never changed until destroyed. +/// +/// This buffer is mapped to the GPU on creation, so must be initialized with the correct capacity. +#[derive(Debug)] +pub(crate) struct StaticBuffer { + //stored sequentially per mesh iteration + offsets: Vec, + gpu: wgpu::Buffer, + //the static size of the buffer + size: wgpu::BufferAddress, +} + +impl StaticBuffer { + pub fn new( + device: &wgpu::Device, + label: &'static str, + size: u64, + usage: wgpu::BufferUsages, + total_offsets: usize, + ) -> Self { + Self { + offsets: Vec::with_capacity(total_offsets), + gpu: device.create_buffer(&wgpu::BufferDescriptor { + label: Some(label), + size, + usage, + mapped_at_creation: true, + }), + size, + } + } + + /// Resolves pending write operations & unmaps buffer from host memory. + pub fn flush(&self) { + (&self.gpu).unmap(); + } + + /// Returns whether or not the buffer needs to be recreated. This can happen whenever the mesh + /// data is re-submitted. + pub fn needs_recreate(&self, new_size: usize) -> bool { + self.size != new_size as u64 + } + + /// Writes the current vertex data to the gpu buffer with a memcpy & stores its offset. + pub fn write(&mut self, offset: u64, content: &[u8]) { + //offset has to be divisible by 8 for alignment reasons + let actual_offset = if offset % 8 != 0 { + offset + 4 + } else { + offset + }; + + let mut buffer = self + .gpu + .slice(actual_offset..(actual_offset + content.len() as u64)) + .get_mapped_range_mut(); + buffer.copy_from_slice(content); + self.offsets.push(actual_offset); + } + + fn offset_at(&self, index: usize) -> &wgpu::BufferAddress { + self.offsets + .get(index) + .expect(&format!("Offset index {} is not in range.", index)) + } + + /// Returns the slice calculated from the offset stored at the given index. + /// e.g. to calculate the slice for the 2nd mesh in the layer, this would be the offset at index + /// 1 that we stored earlier when writing. + pub fn slice_from_index( + &self, + index: usize, + ) -> wgpu::BufferSlice<'_> { + self.gpu.slice(self.offset_at(index)..) + } +} + +/// Returns true if the current buffer doesn't exist & needs to be created, or if it's too small +/// for the new content. +pub(crate) fn needs_recreate( + buffer: &Option, + new_size: usize, +) -> bool { + match buffer { + None => true, + Some(buf) => buf.needs_recreate(new_size), + } +} diff --git a/wgpu/src/buffers/dynamic_buffers.rs b/wgpu/src/buffers/dynamic_buffers.rs new file mode 100644 index 00000000..d81529ce --- /dev/null +++ b/wgpu/src/buffers/dynamic_buffers.rs @@ -0,0 +1,202 @@ +//! Utilities for uniform buffer operations. +use encase::private::WriteInto; +use encase::ShaderType; +use std::marker::PhantomData; + +// Currently supported dynamic buffers. +enum DynamicBufferType { + Uniform(encase::DynamicUniformBuffer>), + Storage(encase::DynamicStorageBuffer>), +} + +impl DynamicBufferType { + /// Writes the current value to its CPU buffer with proper alignment. + pub(super) fn write( + &mut self, + value: &T, + ) -> wgpu::DynamicOffset { + match self { + DynamicBufferType::Uniform(buf) => buf + .write(value) + .expect("Error when writing to dynamic uniform buffer.") + as u32, + DynamicBufferType::Storage(buf) => buf + .write(value) + .expect("Error when writing to dynamic storage buffer.") + as u32, + } + } + + /// Returns bytearray of aligned CPU buffer. + pub(super) fn get_ref(&self) -> &Vec { + match self { + DynamicBufferType::Uniform(buf) => buf.as_ref(), + DynamicBufferType::Storage(buf) => buf.as_ref(), + } + } + + /// Resets the CPU buffer. + pub(super) fn clear(&mut self) { + match self { + DynamicBufferType::Uniform(buf) => { + buf.as_mut().clear(); + buf.set_offset(0); + } + DynamicBufferType::Storage(buf) => { + buf.as_mut().clear(); + buf.set_offset(0); + } + } + } +} + +//TODO think about making cpu & gpu buffers optional +pub(crate) struct DynamicBuffer { + offsets: Vec, + cpu: DynamicBufferType, + gpu: wgpu::Buffer, + label: &'static str, + size: u64, + _data: PhantomData, +} + +impl DynamicBuffer { + /// Creates a new dynamic uniform buffer. + pub fn uniform(device: &wgpu::Device, label: &'static str) -> Self { + DynamicBuffer::new( + device, + DynamicBufferType::Uniform(encase::DynamicUniformBuffer::new( + Vec::new(), + )), + label, + wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST, + ) + } + + /// Creates a new dynamic storage buffer. + pub fn storage(device: &wgpu::Device, label: &'static str) -> Self { + DynamicBuffer::new( + device, + DynamicBufferType::Storage(encase::DynamicStorageBuffer::new( + Vec::new(), + )), + label, + wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_DST, + ) + } + + fn new( + device: &wgpu::Device, + dynamic_buffer_type: DynamicBufferType, + label: &'static str, + usage: wgpu::BufferUsages, + ) -> Self { + let initial_size = u64::from(T::min_size()); + + Self { + offsets: Vec::new(), + cpu: dynamic_buffer_type, + gpu: DynamicBuffer::::create_gpu_buffer( + device, + label, + usage, + initial_size, + ), + label, + size: initial_size, + _data: Default::default(), + } + } + + fn create_gpu_buffer( + device: &wgpu::Device, + label: &'static str, + usage: wgpu::BufferUsages, + size: u64, + ) -> wgpu::Buffer { + device.create_buffer(&wgpu::BufferDescriptor { + label: Some(label), + size, + usage, + mapped_at_creation: false, + }) + } + + /// Write a new value to the CPU buffer with proper alignment. Stores the returned offset value + /// in the buffer for future use. + pub fn push(&mut self, value: &T) { + //this write operation on the buffer will adjust for uniform alignment requirements + let offset = self.cpu.write(value); + self.offsets.push(offset as u32); + } + + /// Resize buffer contents if necessary. This will re-create the GPU buffer if current size is + /// less than the newly computed size from the CPU buffer. + pub fn resize(&mut self, device: &wgpu::Device) -> bool { + let new_size = self.cpu.get_ref().len() as u64; + + if self.size < new_size { + let usages = match self.cpu { + DynamicBufferType::Uniform(_) => { + wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST + } + DynamicBufferType::Storage(_) => { + wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_DST + } + }; + + //Re-create the GPU buffer since it needs to be resized. + self.gpu = DynamicBuffer::::create_gpu_buffer( + device, self.label, usages, new_size, + ); + self.size = new_size; + true + } else { + false + } + } + + /// Write the contents of this dynamic buffer to the GPU via staging belt command. + pub fn write( + &mut self, + device: &wgpu::Device, + staging_belt: &mut wgpu::util::StagingBelt, + encoder: &mut wgpu::CommandEncoder, + ) { + let size = self.cpu.get_ref().len(); + + if let Some(buffer_size) = wgpu::BufferSize::new(size as u64) { + let mut buffer = staging_belt.write_buffer( + encoder, + &self.gpu, + 0, + buffer_size, + device, + ); + + buffer.copy_from_slice(self.cpu.get_ref()); + } + } + + // Gets the aligned offset at the given index from the CPU buffer. + pub fn offset_at_index(&self, index: usize) -> wgpu::DynamicOffset { + let offset = self + .offsets + .get(index) + .expect(&format!("Index {} not found in offsets.", index)) + .clone(); + + offset + } + + /// Returns a reference to the GPU buffer. + pub fn raw(&self) -> &wgpu::Buffer { + &self.gpu + } + + /// Reset the buffer. + pub fn clear(&mut self) { + self.offsets.clear(); + self.cpu.clear(); + } +} diff --git a/wgpu/src/lib.rs b/wgpu/src/lib.rs index 3a98c6bd..42cf712e 100644 --- a/wgpu/src/lib.rs +++ b/wgpu/src/lib.rs @@ -41,6 +41,7 @@ pub mod settings; pub mod triangle; pub mod window; +pub mod buffers; mod backend; mod quad; diff --git a/wgpu/src/shader/triangle_gradient.wgsl b/wgpu/src/shader/triangle_gradient.wgsl new file mode 100644 index 00000000..cb35b61c --- /dev/null +++ b/wgpu/src/shader/triangle_gradient.wgsl @@ -0,0 +1,83 @@ +// uniforms +struct GradientUniforms { + transform: mat4x4, + @size(16) start: vec2, + @size(16) end: vec2, + @size(16) start_stop: i32, + @size(16) end_stop: i32, +} + +struct Stop { + color: vec4, + offset: f32, +}; + +@group(0) @binding(0) +var gradient_uniforms: GradientUniforms; + +@group(0) @binding(1) +var color_stops: array; + +struct VertexOutput { + @builtin(position) position: vec4, + @location(0) raw_position: vec2 +} + +@vertex +fn vs_main(@location(0) input: vec2) -> VertexOutput { + var output: VertexOutput; + output.position = gradient_uniforms.transform * vec4(input.xy, 0.0, 1.0); + output.raw_position = input; + + return output; +} + +@fragment +fn fs_gradient(input: VertexOutput) -> @location(0) vec4 { + let v1 = gradient_uniforms.end - gradient_uniforms.start; + let v2 = input.raw_position.xy - gradient_uniforms.start; + let unit = normalize(v1); + let offset = dot(unit, v2) / length(v1); + + let min_stop = color_stops[gradient_uniforms.start_stop]; + let max_stop = color_stops[gradient_uniforms.end_stop]; + + var color: vec4; + + if (offset <= min_stop.offset) { + color = min_stop.color; + } else if (offset >= max_stop.offset) { + color = max_stop.color; + } else { + var min = min_stop; + var max = max_stop; + var min_index = gradient_uniforms.start_stop; + var max_index = gradient_uniforms.end_stop; + + loop { + if (min_index >= max_index - 1) { + break; + } + + let index = min_index + (max_index - min_index) / 2; + + let stop = color_stops[index]; + + if (offset <= stop.offset) { + max = stop; + max_index = index; + } else { + min = stop; + min_index = index; + } + } + + color = mix(min.color, max.color, smoothstep( + min.offset, + max.offset, + offset + )); + } + + return color; +} \ No newline at end of file diff --git a/wgpu/src/shader/triangle_solid.wgsl b/wgpu/src/shader/triangle_solid.wgsl new file mode 100644 index 00000000..126eceaa --- /dev/null +++ b/wgpu/src/shader/triangle_solid.wgsl @@ -0,0 +1,18 @@ +// uniforms +struct SolidUniforms { + transform: mat4x4, + color: vec4 +} + +@group(0) @binding(0) +var solid_uniforms: SolidUniforms; + +@vertex +fn vs_main(@location(0) input: vec2) -> @builtin(position) vec4 { + return solid_uniforms.transform * vec4(input.xy, 0.0, 1.0); +} + +@fragment +fn fs_solid() -> @location(0) vec4 { + return solid_uniforms.color; +} \ No newline at end of file diff --git a/wgpu/src/triangle.rs b/wgpu/src/triangle.rs index fd06dddf..d632c26c 100644 --- a/wgpu/src/triangle.rs +++ b/wgpu/src/triangle.rs @@ -1,429 +1,308 @@ //! Draw meshes of triangles. use crate::{settings, Transformation}; -use iced_graphics::layer; +use core::fmt; +use std::fmt::Formatter; -use bytemuck::{Pod, Zeroable}; -use std::mem; +use iced_graphics::layer::Meshes; +use iced_graphics::shader::Shader; +use iced_graphics::Size; +use crate::buffers::buffer::{needs_recreate, StaticBuffer}; +use crate::triangle::gradient::GradientPipeline; +use crate::triangle::solid::SolidPipeline; pub use iced_graphics::triangle::{Mesh2D, Vertex2D}; +mod gradient; mod msaa; +mod solid; -const UNIFORM_BUFFER_SIZE: usize = 50; -const VERTEX_BUFFER_SIZE: usize = 10_000; -const INDEX_BUFFER_SIZE: usize = 10_000; - +/// Triangle pipeline for all mesh layers in a [`iced_graphics::Canvas`] widget. #[derive(Debug)] pub(crate) struct Pipeline { - pipeline: wgpu::RenderPipeline, blit: Option, - constants_layout: wgpu::BindGroupLayout, - constants: wgpu::BindGroup, - uniforms_buffer: Buffer, - vertex_buffer: Buffer, - index_buffer: Buffer, + // these are optional so we don't allocate any memory to the GPU if + // application has no triangle meshes. + vertex_buffer: Option, + index_buffer: Option, + pipelines: TrianglePipelines, } -#[derive(Debug)] -struct Buffer { - label: &'static str, - raw: wgpu::Buffer, - size: usize, - usage: wgpu::BufferUsages, - _type: std::marker::PhantomData, +/// Supported triangle pipelines for different fills. Both use the same vertex shader. +pub(crate) struct TrianglePipelines { + solid: SolidPipeline, + gradient: GradientPipeline, } -impl Buffer { - pub fn new( - label: &'static str, - device: &wgpu::Device, - size: usize, - usage: wgpu::BufferUsages, - ) -> Self { - let raw = device.create_buffer(&wgpu::BufferDescriptor { - label: Some(label), - size: (std::mem::size_of::() * size) as u64, - usage, - mapped_at_creation: false, - }); - - Buffer { - label, - raw, - size, - usage, - _type: std::marker::PhantomData, - } +impl fmt::Debug for TrianglePipelines { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + f.debug_struct("TrianglePipelines").finish() } +} - pub fn expand(&mut self, device: &wgpu::Device, size: usize) -> bool { - let needs_resize = self.size < size; - - if needs_resize { - self.raw = device.create_buffer(&wgpu::BufferDescriptor { - label: Some(self.label), - size: (std::mem::size_of::() * size) as u64, - usage: self.usage, - mapped_at_creation: false, - }); - - self.size = size; - } +impl TrianglePipelines { + /// Resets each pipeline's buffers. + fn clear(&mut self) { + self.solid.buffer.clear(); + self.gradient.uniform_buffer.clear(); + self.gradient.storage_buffer.clear(); + } - needs_resize + /// Writes the contents of each pipeline's CPU buffer to the GPU, resizing the GPU buffer + /// beforehand if necessary. + fn write( + &mut self, + device: &wgpu::Device, + staging_belt: &mut wgpu::util::StagingBelt, + encoder: &mut wgpu::CommandEncoder, + ) { + self.solid.write(device, staging_belt, encoder); + self.gradient.write(device, staging_belt, encoder); } } impl Pipeline { + /// Creates supported GL programs, listed in [TrianglePipelines]. pub fn new( device: &wgpu::Device, format: wgpu::TextureFormat, antialiasing: Option, ) -> Pipeline { - let constants_layout = - device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { - label: Some("iced_wgpu::triangle uniforms layout"), - entries: &[wgpu::BindGroupLayoutEntry { - binding: 0, - visibility: wgpu::ShaderStages::VERTEX, - ty: wgpu::BindingType::Buffer { - ty: wgpu::BufferBindingType::Uniform, - has_dynamic_offset: true, - min_binding_size: wgpu::BufferSize::new( - mem::size_of::() as u64, - ), - }, - count: None, - }], - }); - - let constants_buffer = Buffer::new( - "iced_wgpu::triangle uniforms buffer", - device, - UNIFORM_BUFFER_SIZE, - wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST, - ); - - let constant_bind_group = - device.create_bind_group(&wgpu::BindGroupDescriptor { - label: Some("iced_wgpu::triangle uniforms bind group"), - layout: &constants_layout, - entries: &[wgpu::BindGroupEntry { - binding: 0, - resource: wgpu::BindingResource::Buffer( - wgpu::BufferBinding { - buffer: &constants_buffer.raw, - offset: 0, - size: wgpu::BufferSize::new(std::mem::size_of::< - Uniforms, - >( - ) - as u64), - }, - ), - }], - }); - - let layout = - device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { - label: Some("iced_wgpu::triangle pipeline layout"), - push_constant_ranges: &[], - bind_group_layouts: &[&constants_layout], - }); - - let shader = - device.create_shader_module(wgpu::ShaderModuleDescriptor { - label: Some("iced_wgpu::triangle::shader"), - source: wgpu::ShaderSource::Wgsl(std::borrow::Cow::Borrowed( - include_str!("shader/triangle.wgsl"), - )), - }); - - let pipeline = - device.create_render_pipeline(&wgpu::RenderPipelineDescriptor { - label: Some("iced_wgpu::triangle pipeline"), - layout: Some(&layout), - vertex: wgpu::VertexState { - module: &shader, - entry_point: "vs_main", - buffers: &[wgpu::VertexBufferLayout { - array_stride: mem::size_of::() as u64, - step_mode: wgpu::VertexStepMode::Vertex, - attributes: &wgpu::vertex_attr_array!( - // Position - 0 => Float32x2, - // Color - 1 => Float32x4, - ), - }], - }, - fragment: Some(wgpu::FragmentState { - module: &shader, - entry_point: "fs_main", - targets: &[Some(wgpu::ColorTargetState { - format, - blend: Some(wgpu::BlendState::ALPHA_BLENDING), - write_mask: wgpu::ColorWrites::ALL, - })], - }), - primitive: wgpu::PrimitiveState { - topology: wgpu::PrimitiveTopology::TriangleList, - front_face: wgpu::FrontFace::Cw, - ..Default::default() - }, - depth_stencil: None, - multisample: wgpu::MultisampleState { - count: antialiasing.map(|a| a.sample_count()).unwrap_or(1), - mask: !0, - alpha_to_coverage_enabled: false, - }, - multiview: None, - }); - Pipeline { - pipeline, blit: antialiasing.map(|a| msaa::Blit::new(device, format, a)), - constants_layout, - constants: constant_bind_group, - uniforms_buffer: constants_buffer, - vertex_buffer: Buffer::new( - "iced_wgpu::triangle vertex buffer", - device, - VERTEX_BUFFER_SIZE, - wgpu::BufferUsages::VERTEX | wgpu::BufferUsages::COPY_DST, - ), - index_buffer: Buffer::new( - "iced_wgpu::triangle index buffer", - device, - INDEX_BUFFER_SIZE, - wgpu::BufferUsages::INDEX | wgpu::BufferUsages::COPY_DST, - ), + vertex_buffer: None, + index_buffer: None, + pipelines: TrianglePipelines { + solid: SolidPipeline::new(device, format, antialiasing), + gradient: GradientPipeline::new(device, format, antialiasing), + }, } } + /// Draws the contents of the current layer's meshes to the [target]. pub fn draw( &mut self, device: &wgpu::Device, staging_belt: &mut wgpu::util::StagingBelt, encoder: &mut wgpu::CommandEncoder, target: &wgpu::TextureView, - target_width: u32, - target_height: u32, + target_size: Size, transformation: Transformation, scale_factor: f32, - meshes: &[layer::Mesh<'_>], + meshes: &Meshes<'_>, ) { - // This looks a bit crazy, but we are just counting how many vertices - // and indices we will need to handle. - // TODO: Improve readability - let (total_vertices, total_indices) = meshes - .iter() - .map(|layer::Mesh { buffers, .. }| { - (buffers.vertices.len(), buffers.indices.len()) - }) - .fold((0, 0), |(total_v, total_i), (v, i)| { - (total_v + v, total_i + i) - }); - - // Then we ensure the current buffers are big enough, resizing if - // necessary - let _ = self.vertex_buffer.expand(device, total_vertices); - let _ = self.index_buffer.expand(device, total_indices); - - // If the uniforms buffer is resized, then we need to recreate its - // bind group. - if self.uniforms_buffer.expand(device, meshes.len()) { - self.constants = - device.create_bind_group(&wgpu::BindGroupDescriptor { - label: Some("iced_wgpu::triangle uniforms buffer"), - layout: &self.constants_layout, - entries: &[wgpu::BindGroupEntry { - binding: 0, - resource: wgpu::BindingResource::Buffer( - wgpu::BufferBinding { - buffer: &self.uniforms_buffer.raw, - offset: 0, - size: wgpu::BufferSize::new( - std::mem::size_of::() as u64, - ), - }, - ), - }], - }); + //count the total number of vertices & indices we need to handle + let (total_vertices, total_indices) = meshes.attribute_count(); + println!("total vertices: {}, total indices: {}", total_vertices, total_indices); + + //Only create buffers if they need to be re-sized or don't exist + if needs_recreate(&self.vertex_buffer, total_vertices) { + //mapped to GPU at creation with total vertices + self.vertex_buffer = Some(StaticBuffer::new( + device, + "iced_wgpu::triangle vertex buffer", + //TODO: a more reasonable default to prevent frequent resizing calls + // before this was 10_000 + (std::mem::size_of::() * total_vertices) as u64, + wgpu::BufferUsages::VERTEX, + meshes.0.len(), + )) } - let mut uniforms: Vec = Vec::with_capacity(meshes.len()); - let mut offsets: Vec<( - wgpu::BufferAddress, - wgpu::BufferAddress, - usize, - )> = Vec::with_capacity(meshes.len()); - let mut last_vertex = 0; - let mut last_index = 0; - - // We upload everything upfront - for mesh in meshes { - let transform = (transformation - * Transformation::translate(mesh.origin.x, mesh.origin.y)) - .into(); - - let vertices = bytemuck::cast_slice(&mesh.buffers.vertices); - let indices = bytemuck::cast_slice(&mesh.buffers.indices); - - if let (Some(vertices_size), Some(indices_size)) = ( - wgpu::BufferSize::new(vertices.len() as u64), - wgpu::BufferSize::new(indices.len() as u64), - ) { - { - let mut vertex_buffer = staging_belt.write_buffer( - encoder, - &self.vertex_buffer.raw, - (std::mem::size_of::() * last_vertex) as u64, - vertices_size, - device, - ); + if needs_recreate(&self.index_buffer, total_indices) { + //mapped to GPU at creation with total indices + self.index_buffer = Some(StaticBuffer::new( + device, + "iced_wgpu::triangle index buffer", + //TODO: a more reasonable default to prevent frequent resizing calls + // before this was 10_000 + (std::mem::size_of::() * total_indices) as u64, + wgpu::BufferUsages::INDEX, + meshes.0.len(), + )); + } - vertex_buffer.copy_from_slice(vertices); + if let Some(vertex_buffer) = &mut self.vertex_buffer { + if let Some(index_buffer) = &mut self.index_buffer { + let mut offset_v = 0; + let mut offset_i = 0; + //TODO: store this more efficiently + let mut indices_lengths = Vec::with_capacity(meshes.0.len()); + + //iterate through meshes to write all attribute data + for mesh in meshes.0.iter() { + let transform = transformation + * Transformation::translate( + mesh.origin.x, + mesh.origin.y, + ); + + println!("Mesh attribute data: Vertex: {:?}, Index: {:?}", mesh.buffers.vertices, mesh.buffers.indices); + + let vertices = bytemuck::cast_slice(&mesh.buffers.vertices); + let indices = bytemuck::cast_slice(&mesh.buffers.indices); + + //TODO: it's (probably) more efficient to reduce this write command and + // iterate first and then upload + println!("vertex buffer len: {}, index length: {}", vertices.len(), indices.len()); + vertex_buffer.write(offset_v, vertices); + index_buffer.write(offset_i, indices); + + offset_v += vertices.len() as u64; + offset_i += indices.len() as u64; + indices_lengths.push(mesh.buffers.indices.len()); + + match mesh.shader { + Shader::Solid(color) => { + self.pipelines.solid.push(transform, color); + } + Shader::Gradient(gradient) => { + self.pipelines.gradient.push(transform, gradient); + } + } } + //done writing to gpu buffer, unmap from host memory since we don't need it + //anymore + vertex_buffer.flush(); + index_buffer.flush(); + + //resize & memcpy uniforms from CPU buffers to GPU buffers for all pipelines + self.pipelines.write(device, staging_belt, encoder); + + //configure the render pass now that the data is uploaded to the GPU { - let mut index_buffer = staging_belt.write_buffer( - encoder, - &self.index_buffer.raw, - (std::mem::size_of::() * last_index) as u64, - indices_size, - device, + //configure antialiasing pass + let (attachment, resolve_target, load) = + if let Some(blit) = &mut self.blit { + let (attachment, resolve_target) = blit.targets( + device, + target_size.width, + target_size.height, + ); + + ( + attachment, + Some(resolve_target), + wgpu::LoadOp::Clear(wgpu::Color::TRANSPARENT), + ) + } else { + (target, None, wgpu::LoadOp::Load) + }; + + let mut render_pass = encoder.begin_render_pass( + &wgpu::RenderPassDescriptor { + label: Some("iced_wgpu::triangle render pass"), + color_attachments: &[Some( + wgpu::RenderPassColorAttachment { + view: attachment, + resolve_target, + ops: wgpu::Operations { load, store: true }, + }, + )], + depth_stencil_attachment: None, + }, ); - index_buffer.copy_from_slice(indices); + //TODO: do this a better way; store it in the respective pipelines perhaps + // to be more readable + let mut num_solids = 0; + let mut num_gradients = 0; + + //TODO: try to avoid this extra iteration if possible + for index in 0..meshes.0.len() { + let clip_bounds = + (meshes.0[index].clip_bounds * scale_factor).snap(); + + render_pass.set_scissor_rect( + clip_bounds.x, + clip_bounds.y, + clip_bounds.width, + clip_bounds.height, + ); + + match meshes.0[index].shader { + Shader::Solid(_) => { + self.pipelines.solid.configure_render_pass( + &mut render_pass, + num_solids, + ); + num_solids += 1; + } + Shader::Gradient(_) => { + self.pipelines.gradient.configure_render_pass( + &mut render_pass, + num_gradients, + ); + num_gradients += 1; + } + } + + render_pass.set_index_buffer( + index_buffer.slice_from_index::(index), + wgpu::IndexFormat::Uint32, + ); + + render_pass.set_vertex_buffer( + 0, + vertex_buffer.slice_from_index::(index), + ); + + render_pass.draw_indexed( + 0..(indices_lengths[index] as u32), + 0, + 0..1, + ); + } } - - uniforms.push(transform); - offsets.push(( - last_vertex as u64, - last_index as u64, - mesh.buffers.indices.len(), - )); - - last_vertex += mesh.buffers.vertices.len(); - last_index += mesh.buffers.indices.len(); - } - } - - let uniforms = bytemuck::cast_slice(&uniforms); - - if let Some(uniforms_size) = - wgpu::BufferSize::new(uniforms.len() as u64) - { - let mut uniforms_buffer = staging_belt.write_buffer( - encoder, - &self.uniforms_buffer.raw, - 0, - uniforms_size, - device, - ); - - uniforms_buffer.copy_from_slice(uniforms); - } - - { - let (attachment, resolve_target, load) = - if let Some(blit) = &mut self.blit { - let (attachment, resolve_target) = - blit.targets(device, target_width, target_height); - - ( - attachment, - Some(resolve_target), - wgpu::LoadOp::Clear(wgpu::Color::TRANSPARENT), - ) - } else { - (target, None, wgpu::LoadOp::Load) - }; - - let mut render_pass = - encoder.begin_render_pass(&wgpu::RenderPassDescriptor { - label: Some("iced_wgpu::triangle render pass"), - color_attachments: &[Some( - wgpu::RenderPassColorAttachment { - view: attachment, - resolve_target, - ops: wgpu::Operations { load, store: true }, - }, - )], - depth_stencil_attachment: None, - }); - - render_pass.set_pipeline(&self.pipeline); - - for (i, (vertex_offset, index_offset, indices)) in - offsets.into_iter().enumerate() - { - let clip_bounds = (meshes[i].clip_bounds * scale_factor).snap(); - - render_pass.set_scissor_rect( - clip_bounds.x, - clip_bounds.y, - clip_bounds.width, - clip_bounds.height, - ); - - render_pass.set_bind_group( - 0, - &self.constants, - &[(std::mem::size_of::() * i) as u32], - ); - - render_pass.set_index_buffer( - self.index_buffer - .raw - .slice(index_offset * mem::size_of::() as u64..), - wgpu::IndexFormat::Uint32, - ); - - render_pass.set_vertex_buffer( - 0, - self.vertex_buffer.raw.slice( - vertex_offset * mem::size_of::() as u64.., - ), - ); - - render_pass.draw_indexed(0..indices as u32, 0, 0..1); } } if let Some(blit) = &mut self.blit { blit.draw(encoder, target); } + + //cleanup + self.pipelines.clear(); } } -#[repr(C)] -#[derive(Debug, Clone, Copy, Zeroable, Pod)] -struct Uniforms { - transform: [f32; 16], - // We need to align this to 256 bytes to please `wgpu`... - // TODO: Be smarter and stop wasting memory! - _padding_a: [f32; 32], - _padding_b: [f32; 16], +//utility functions for individual pipelines with shared functionality +fn vertex_buffer_layout<'a>() -> wgpu::VertexBufferLayout<'a> { + wgpu::VertexBufferLayout { + array_stride: std::mem::size_of::() as u64, + step_mode: wgpu::VertexStepMode::Vertex, + attributes: &[wgpu::VertexAttribute { + format: wgpu::VertexFormat::Float32x2, + offset: 0, + shader_location: 0, + }], + } } -impl Default for Uniforms { - fn default() -> Self { - Self { - transform: *Transformation::identity().as_ref(), - _padding_a: [0.0; 32], - _padding_b: [0.0; 16], - } +fn default_fragment_target( + texture_format: wgpu::TextureFormat, +) -> Option { + Some(wgpu::ColorTargetState { + format: texture_format, + blend: Some(wgpu::BlendState::ALPHA_BLENDING), + write_mask: wgpu::ColorWrites::ALL, + }) +} + +fn default_triangle_primitive_state() -> wgpu::PrimitiveState { + wgpu::PrimitiveState { + topology: wgpu::PrimitiveTopology::TriangleList, + front_face: wgpu::FrontFace::Cw, + ..Default::default() } } -impl From for Uniforms { - fn from(transformation: Transformation) -> Uniforms { - Self { - transform: transformation.into(), - _padding_a: [0.0; 32], - _padding_b: [0.0; 16], - } +fn default_multisample_state( + antialiasing: Option, +) -> wgpu::MultisampleState { + wgpu::MultisampleState { + count: antialiasing.map(|a| a.sample_count()).unwrap_or(1), + mask: !0, + alpha_to_coverage_enabled: false, } } diff --git a/wgpu/src/triangle/gradient.rs b/wgpu/src/triangle/gradient.rs new file mode 100644 index 00000000..471b204c --- /dev/null +++ b/wgpu/src/triangle/gradient.rs @@ -0,0 +1,265 @@ +use crate::buffers::dynamic_buffers::DynamicBuffer; +use crate::settings; +use crate::triangle::{ + default_fragment_target, default_multisample_state, + default_triangle_primitive_state, vertex_buffer_layout, +}; +use encase::ShaderType; +use glam::{Vec2, Vec4}; +use iced_graphics::gradient::Gradient; +use iced_graphics::Transformation; + +pub(super) struct GradientPipeline { + pipeline: wgpu::RenderPipeline, + pub(super) uniform_buffer: DynamicBuffer, + pub(super) storage_buffer: DynamicBuffer, + color_stop_offset: i32, + //Need to store these and then write them all at once + //or else they will be padded to 256 and cause gaps in the storage buffer + color_stops_pending_write: GradientStorage, + bind_group_layout: wgpu::BindGroupLayout, + bind_group: wgpu::BindGroup, +} + +//TODO I can tightly pack this by rearranging/consolidating some fields +#[derive(Debug, ShaderType)] +pub(super) struct GradientUniforms { + transform: glam::Mat4, + start: Vec2, + #[align(16)] + end: Vec2, + #[align(16)] + start_stop: i32, + #[align(16)] + end_stop: i32, +} + +#[derive(Debug, ShaderType)] +pub(super) struct ColorStop { + color: Vec4, + offset: f32, +} + +#[derive(ShaderType)] +pub(super) struct GradientStorage { + #[size(runtime)] + pub color_stops: Vec, +} + +impl GradientPipeline { + /// Creates a new [GradientPipeline] using `triangle_gradient.wgsl` shader. + pub(super) fn new( + device: &wgpu::Device, + format: wgpu::TextureFormat, + antialiasing: Option, + ) -> Self { + let uniform_buffer = DynamicBuffer::uniform( + device, + "iced_wgpu::triangle [GRADIENT] uniforms", + ); + + //TODO: With a WASM target storage buffers are not supported. Will need to use UBOs & static + // sized array (64 on OpenGL side right now) to make gradients work + let storage_buffer = DynamicBuffer::storage( + device, + "iced_wgpu::triangle [GRADIENT] storage", + ); + + let bind_group_layout = + device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { + label: Some("iced_wgpu::triangle [GRADIENT] bind group layout"), + entries: &[ + wgpu::BindGroupLayoutEntry { + binding: 0, + visibility: wgpu::ShaderStages::VERTEX_FRAGMENT, + ty: wgpu::BindingType::Buffer { + ty: wgpu::BufferBindingType::Uniform, + has_dynamic_offset: true, + min_binding_size: Some(GradientUniforms::min_size()), + }, + count: None, + }, + wgpu::BindGroupLayoutEntry { + binding: 1, + visibility: wgpu::ShaderStages::FRAGMENT, + ty: wgpu::BindingType::Buffer { + ty: wgpu::BufferBindingType::Storage { + read_only: true, + }, + has_dynamic_offset: false, + min_binding_size: Some(GradientStorage::min_size()), + }, + count: None, + }, + ], + }); + + let bind_group = GradientPipeline::bind_group( + device, + uniform_buffer.raw(), + storage_buffer.raw(), + &bind_group_layout, + ); + + let layout = + device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { + label: Some("iced_wgpu::triangle [GRADIENT] pipeline layout"), + bind_group_layouts: &[&bind_group_layout], + push_constant_ranges: &[], + }); + + let shader = + device.create_shader_module(wgpu::ShaderModuleDescriptor { + label: Some( + "iced_wgpu::triangle [GRADIENT] create shader module", + ), + source: wgpu::ShaderSource::Wgsl(std::borrow::Cow::Borrowed( + include_str!("../shader/triangle_gradient.wgsl"), + )), + }); + + let pipeline = + device.create_render_pipeline(&wgpu::RenderPipelineDescriptor { + label: Some("iced_wgpu::triangle [GRADIENT] pipeline"), + layout: Some(&layout), + vertex: wgpu::VertexState { + module: &shader, + entry_point: "vs_main", + buffers: &[vertex_buffer_layout()], + }, + fragment: Some(wgpu::FragmentState { + module: &shader, + entry_point: "fs_gradient", + targets: &[default_fragment_target(format)], + }), + primitive: default_triangle_primitive_state(), + depth_stencil: None, + multisample: default_multisample_state(antialiasing), + multiview: None, + }); + + Self { + pipeline, + uniform_buffer, + storage_buffer, + color_stop_offset: 0, + color_stops_pending_write: GradientStorage { color_stops: vec![] }, + bind_group_layout, + bind_group, + } + } + + /// Pushes a new gradient uniform to the CPU buffer. + pub fn push(&mut self, transform: Transformation, gradient: &Gradient) { + match gradient { + Gradient::Linear(linear) => { + let start_offset = self.color_stop_offset; + let end_offset = + (linear.color_stops.len() as i32) + start_offset - 1; + + self.uniform_buffer.push(&GradientUniforms { + transform: transform.into(), + start: Vec2::new(linear.start.x, linear.start.y), + end: Vec2::new(linear.end.x, linear.end.y), + start_stop: start_offset, + end_stop: end_offset, + }); + + self.color_stop_offset = end_offset + 1; + + let stops: Vec = linear + .color_stops + .iter() + .map(|stop| ColorStop { + offset: stop.offset, + color: Vec4::new( + stop.color.r, + stop.color.g, + stop.color.b, + stop.color.a, + ), + }) + .collect(); + + self.color_stops_pending_write.color_stops.extend(stops); + } + } + } + + fn bind_group( + device: &wgpu::Device, + uniform_buffer: &wgpu::Buffer, + storage_buffer: &wgpu::Buffer, + layout: &wgpu::BindGroupLayout, + ) -> wgpu::BindGroup { + device.create_bind_group(&wgpu::BindGroupDescriptor { + label: Some("iced_wgpu::triangle [GRADIENT] bind group"), + layout, + entries: &[ + wgpu::BindGroupEntry { + binding: 0, + resource: wgpu::BindingResource::Buffer( + wgpu::BufferBinding { + buffer: uniform_buffer, + offset: 0, + size: Some(GradientUniforms::min_size()) + } + ) + }, + wgpu::BindGroupEntry { + binding: 1, + resource: storage_buffer.as_entire_binding() + }, + ], + }) + } + + /// Writes the contents of the gradient CPU buffer to the GPU buffer, resizing the GPU buffer + /// beforehand if necessary. + pub fn write( + &mut self, + device: &wgpu::Device, + staging_belt: &mut wgpu::util::StagingBelt, + encoder: &mut wgpu::CommandEncoder, + ) { + //first write the pending color stops to the CPU buffer + self.storage_buffer.push(&self.color_stops_pending_write); + + //resize buffers if needed + let uniforms_resized = self.uniform_buffer.resize(device); + let storage_resized = self.storage_buffer.resize(device); + + if uniforms_resized || storage_resized { + //recreate bind groups if any buffers were resized + self.bind_group = GradientPipeline::bind_group( + device, + self.uniform_buffer.raw(), + self.storage_buffer.raw(), + &self.bind_group_layout, + ); + } + + //write to GPU + self.uniform_buffer.write(device, staging_belt, encoder); + self.storage_buffer.write(device, staging_belt, encoder); + + //cleanup + self.color_stop_offset = 0; + self.color_stops_pending_write.color_stops.clear(); + } + + /// Configures the current render pass to draw the gradient at its offset stored in the + /// [DynamicBuffer] at [index]. + pub fn configure_render_pass<'a>( + &'a self, + render_pass: &mut wgpu::RenderPass<'a>, + index: usize, + ) { + render_pass.set_pipeline(&self.pipeline); + render_pass.set_bind_group( + 0, + &self.bind_group, + &[self.uniform_buffer.offset_at_index(index)], + ); + } +} diff --git a/wgpu/src/triangle/solid.rs b/wgpu/src/triangle/solid.rs new file mode 100644 index 00000000..a3cbd72b --- /dev/null +++ b/wgpu/src/triangle/solid.rs @@ -0,0 +1,169 @@ +use crate::buffers::dynamic_buffers::DynamicBuffer; +use crate::triangle::{ + default_fragment_target, default_multisample_state, + default_triangle_primitive_state, vertex_buffer_layout, +}; +use crate::{settings, Color}; +use encase::ShaderType; +use glam::Vec4; +use iced_graphics::Transformation; + +pub(super) struct SolidPipeline { + pipeline: wgpu::RenderPipeline, + pub(super) buffer: DynamicBuffer, + bind_group_layout: wgpu::BindGroupLayout, + bind_group: wgpu::BindGroup, +} + +#[derive(Debug, Clone, Copy, ShaderType)] +pub(super) struct SolidUniforms { + transform: glam::Mat4, + color: Vec4, +} + +impl SolidUniforms { + pub fn new(transform: Transformation, color: Color) -> Self { + Self { + transform: transform.into(), + color: Vec4::new(color.r, color.g, color.b, color.a), + } + } +} + +impl SolidPipeline { + /// Creates a new [SolidPipeline] using `triangle_solid.wgsl` shader. + pub fn new( + device: &wgpu::Device, + format: wgpu::TextureFormat, + antialiasing: Option, + ) -> Self { + let buffer = DynamicBuffer::uniform( + device, + "iced_wgpu::triangle [SOLID] uniforms", + ); + + let bind_group_layout = + device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { + label: Some("iced_wgpu::triangle [SOLID] bind group layout"), + entries: &[wgpu::BindGroupLayoutEntry { + binding: 0, + visibility: wgpu::ShaderStages::VERTEX_FRAGMENT, + ty: wgpu::BindingType::Buffer { + ty: wgpu::BufferBindingType::Uniform, + has_dynamic_offset: true, + min_binding_size: Some(SolidUniforms::min_size()), + }, + count: None, + }], + }); + + let bind_group = SolidPipeline::bind_group( + device, + &buffer.raw(), + &bind_group_layout, + ); + + let layout = + device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { + label: Some("iced_wgpu::triangle [SOLID] pipeline layout"), + bind_group_layouts: &[&bind_group_layout], + push_constant_ranges: &[], + }); + + let shader = + device.create_shader_module(wgpu::ShaderModuleDescriptor { + label: Some("iced_wgpu::triangle [SOLID] create shader module"), + source: wgpu::ShaderSource::Wgsl(std::borrow::Cow::Borrowed( + include_str!("../shader/triangle_solid.wgsl"), + )), + }); + + let pipeline = + device.create_render_pipeline(&wgpu::RenderPipelineDescriptor { + label: Some("iced_wgpu::triangle [SOLID] pipeline"), + layout: Some(&layout), + vertex: wgpu::VertexState { + module: &shader, + entry_point: "vs_main", + buffers: &[vertex_buffer_layout()], + }, + fragment: Some(wgpu::FragmentState { + module: &shader, + entry_point: "fs_solid", + targets: &[default_fragment_target(format)], + }), + primitive: default_triangle_primitive_state(), + depth_stencil: None, + multisample: default_multisample_state(antialiasing), + multiview: None, + }); + + Self { + pipeline, + buffer, + bind_group_layout, + bind_group, + } + } + + fn bind_group( + device: &wgpu::Device, + buffer: &wgpu::Buffer, + layout: &wgpu::BindGroupLayout, + ) -> wgpu::BindGroup { + device.create_bind_group(&wgpu::BindGroupDescriptor { + label: Some("iced_wgpu::triangle [SOLID] bind group"), + layout, + entries: &[wgpu::BindGroupEntry { + binding: 0, + resource: wgpu::BindingResource::Buffer(wgpu::BufferBinding { + buffer, + offset: 0, + size: Some(SolidUniforms::min_size()), + }), + }], + }) + } + + /// Pushes a new solid uniform to the CPU buffer. + pub fn push(&mut self, transform: Transformation, color: &Color) { + self.buffer.push(&SolidUniforms::new(transform, *color)); + } + + /// Writes the contents of the solid CPU buffer to the GPU buffer, resizing the GPU buffer + /// beforehand if necessary. + pub fn write( + &mut self, + device: &wgpu::Device, + staging_belt: &mut wgpu::util::StagingBelt, + encoder: &mut wgpu::CommandEncoder, + ) { + let uniforms_resized = self.buffer.resize(device); + + if uniforms_resized { + self.bind_group = SolidPipeline::bind_group( + device, + self.buffer.raw(), + &self.bind_group_layout, + ) + } + + self.buffer.write(device, staging_belt, encoder); + } + + /// Configures the current render pass to draw the solid at its offset stored in the + /// [DynamicBuffer] at [index]. + pub fn configure_render_pass<'a>( + &'a self, + render_pass: &mut wgpu::RenderPass<'a>, + index: usize, + ) { + render_pass.set_pipeline(&self.pipeline); + + render_pass.set_bind_group( + 0, + &self.bind_group, + &[self.buffer.offset_at_index(index)], + ); + } +} \ No newline at end of file -- cgit From 40f45d7b7e35dd4937abe6b5ce16b6256b4f1eeb Mon Sep 17 00:00:00 2001 From: shan Date: Thu, 29 Sep 2022 10:52:58 -0700 Subject: Adds linear gradient support to 2D meshes in the canvas widget. --- Cargo.toml | 1 + examples/arc/src/main.rs | 14 +- examples/clock/src/main.rs | 54 ++- examples/geometry/src/main.rs | 41 +- examples/modern_art/Cargo.toml | 10 + examples/modern_art/src/main.rs | 141 ++++++ examples/solar_system/src/main.rs | 13 +- glow/src/backend.rs | 7 +- glow/src/shader/common/gradient.frag | 48 ++ glow/src/shader/common/triangle.frag | 4 +- glow/src/shader/common/triangle.vert | 6 +- glow/src/triangle.rs | 228 +++++----- glow/src/triangle/gradient.rs | 189 ++++++++ glow/src/triangle/solid.rs | 67 +++ glow/src/window/compositor.rs | 3 +- graphics/Cargo.toml | 2 +- graphics/src/gradient.rs | 23 + graphics/src/layer.rs | 47 +- graphics/src/lib.rs | 2 + graphics/src/primitive.rs | 5 +- graphics/src/shader.rs | 42 ++ graphics/src/transformation.rs | 8 +- graphics/src/triangle.rs | 18 +- graphics/src/widget/canvas.rs | 6 +- graphics/src/widget/canvas/fill.rs | 33 +- graphics/src/widget/canvas/frame.rs | 113 ++--- graphics/src/widget/canvas/gradient.rs | 21 + graphics/src/widget/canvas/gradient/linear.rs | 73 ++++ graphics/src/widget/canvas/stroke.rs | 26 +- wgpu/Cargo.toml | 7 + wgpu/src/backend.rs | 11 +- wgpu/src/buffers.rs | 3 + wgpu/src/buffers/buffer.rs | 91 ++++ wgpu/src/buffers/dynamic_buffers.rs | 202 +++++++++ wgpu/src/lib.rs | 1 + wgpu/src/shader/triangle_gradient.wgsl | 83 ++++ wgpu/src/shader/triangle_solid.wgsl | 18 + wgpu/src/triangle.rs | 605 +++++++++++--------------- wgpu/src/triangle/gradient.rs | 265 +++++++++++ wgpu/src/triangle/solid.rs | 169 +++++++ 40 files changed, 2043 insertions(+), 657 deletions(-) create mode 100644 examples/modern_art/Cargo.toml create mode 100644 examples/modern_art/src/main.rs create mode 100644 glow/src/shader/common/gradient.frag create mode 100644 glow/src/triangle/gradient.rs create mode 100644 glow/src/triangle/solid.rs create mode 100644 graphics/src/gradient.rs create mode 100644 graphics/src/shader.rs create mode 100644 graphics/src/widget/canvas/gradient.rs create mode 100644 graphics/src/widget/canvas/gradient/linear.rs create mode 100644 wgpu/src/buffers.rs create mode 100644 wgpu/src/buffers/buffer.rs create mode 100644 wgpu/src/buffers/dynamic_buffers.rs create mode 100644 wgpu/src/shader/triangle_gradient.wgsl create mode 100644 wgpu/src/shader/triangle_solid.wgsl create mode 100644 wgpu/src/triangle/gradient.rs create mode 100644 wgpu/src/triangle/solid.rs diff --git a/Cargo.toml b/Cargo.toml index 725baecc..d8f5eccb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -72,6 +72,7 @@ members = [ "examples/geometry", "examples/integration_opengl", "examples/integration_wgpu", + "examples/modern_art", "examples/pane_grid", "examples/pick_list", "examples/pokedex", diff --git a/examples/arc/src/main.rs b/examples/arc/src/main.rs index 0c619dc9..6029a69c 100644 --- a/examples/arc/src/main.rs +++ b/examples/arc/src/main.rs @@ -2,7 +2,7 @@ use std::{f32::consts::PI, time::Instant}; use iced::executor; use iced::widget::canvas::{ - self, Cache, Canvas, Cursor, Geometry, Path, Stroke, + self, Cache, Canvas, Cursor, Geometry, Path, Stroke, StrokeStyle, }; use iced::{ Application, Command, Element, Length, Point, Rectangle, Settings, @@ -52,11 +52,6 @@ impl Application for Arc { Command::none() } - fn subscription(&self) -> Subscription { - iced::time::every(std::time::Duration::from_millis(10)) - .map(|_| Message::Tick) - } - fn view(&self) -> Element { Canvas::new(self) .width(Length::Fill) @@ -67,6 +62,11 @@ impl Application for Arc { fn theme(&self) -> Theme { Theme::Dark } + + fn subscription(&self) -> Subscription { + iced::time::every(std::time::Duration::from_millis(10)) + .map(|_| Message::Tick) + } } impl canvas::Program for Arc { @@ -114,7 +114,7 @@ impl canvas::Program for Arc { frame.stroke( &path, Stroke { - color: palette.text, + style: StrokeStyle::Solid(palette.text), width: 10.0, ..Stroke::default() }, diff --git a/examples/clock/src/main.rs b/examples/clock/src/main.rs index 8818fb54..51f25a3f 100644 --- a/examples/clock/src/main.rs +++ b/examples/clock/src/main.rs @@ -1,5 +1,7 @@ use iced::executor; -use iced::widget::canvas::{Cache, Cursor, Geometry, LineCap, Path, Stroke}; +use iced::widget::canvas::{ + Cache, Cursor, Geometry, LineCap, Path, Stroke, StrokeStyle, +}; use iced::widget::{canvas, container}; use iced::{ Application, Color, Command, Element, Length, Point, Rectangle, Settings, @@ -24,9 +26,9 @@ enum Message { } impl Application for Clock { + type Executor = executor::Default; type Message = Message; type Theme = Theme; - type Executor = executor::Default; type Flags = (); fn new(_flags: ()) -> (Self, Command) { @@ -59,15 +61,6 @@ impl Application for Clock { Command::none() } - fn subscription(&self) -> Subscription { - iced::time::every(std::time::Duration::from_millis(500)).map(|_| { - Message::Tick( - time::OffsetDateTime::now_local() - .unwrap_or_else(|_| time::OffsetDateTime::now_utc()), - ) - }) - } - fn view(&self) -> Element { let canvas = canvas(self as &Self) .width(Length::Fill) @@ -79,6 +72,15 @@ impl Application for Clock { .padding(20) .into() } + + fn subscription(&self) -> Subscription { + iced::time::every(std::time::Duration::from_millis(500)).map(|_| { + Message::Tick( + time::OffsetDateTime::now_local() + .unwrap_or_else(|_| time::OffsetDateTime::now_utc()), + ) + }) + } } impl canvas::Program for Clock { @@ -104,33 +106,41 @@ impl canvas::Program for Clock { let long_hand = Path::line(Point::ORIGIN, Point::new(0.0, -0.8 * radius)); - let thin_stroke = Stroke { - width: radius / 100.0, - color: Color::WHITE, - line_cap: LineCap::Round, - ..Stroke::default() + let width = radius / 100.0; + + let thin_stroke = || -> Stroke { + Stroke { + width, + style: StrokeStyle::Solid(Color::WHITE), + line_cap: LineCap::Round, + ..Stroke::default() + } }; - let wide_stroke = Stroke { - width: thin_stroke.width * 3.0, - ..thin_stroke + let wide_stroke = || -> Stroke { + Stroke { + width: width * 3.0, + style: StrokeStyle::Solid(Color::WHITE), + line_cap: LineCap::Round, + ..Stroke::default() + } }; frame.translate(Vector::new(center.x, center.y)); frame.with_save(|frame| { frame.rotate(hand_rotation(self.now.hour(), 12)); - frame.stroke(&short_hand, wide_stroke); + frame.stroke(&short_hand, wide_stroke()); }); frame.with_save(|frame| { frame.rotate(hand_rotation(self.now.minute(), 60)); - frame.stroke(&long_hand, wide_stroke); + frame.stroke(&long_hand, wide_stroke()); }); frame.with_save(|frame| { frame.rotate(hand_rotation(self.now.second(), 60)); - frame.stroke(&long_hand, thin_stroke); + frame.stroke(&long_hand, thin_stroke()); }) }); diff --git a/examples/geometry/src/main.rs b/examples/geometry/src/main.rs index d8b99ab3..a8ce26f8 100644 --- a/examples/geometry/src/main.rs +++ b/examples/geometry/src/main.rs @@ -1,6 +1,9 @@ //! This example showcases a simple native custom widget that renders using //! arbitrary low-level geometry. +//! +//TODO need to update this now that vertex data doesn't contain color data mod rainbow { + use iced::Color; // For now, to implement a custom native widget you will need to add // `iced_native` and `iced_wgpu` to your dependencies. // @@ -12,6 +15,7 @@ mod rainbow { // implemented by `iced_wgpu` and other renderers. use iced_graphics::renderer::{self, Renderer}; use iced_graphics::{Backend, Primitive}; + use iced_graphics::shader::Shader; use iced_native::widget::{self, Widget}; use iced_native::{ @@ -63,20 +67,20 @@ mod rainbow { cursor_position: Point, _viewport: &Rectangle, ) { - use iced_graphics::triangle::{Mesh2D, Vertex2D}; + use iced_graphics::triangle::{Mesh2D, Shader, Vertex2D}; use iced_native::Renderer as _; let b = layout.bounds(); // R O Y G B I V - let color_r = [1.0, 0.0, 0.0, 1.0]; - let color_o = [1.0, 0.5, 0.0, 1.0]; - let color_y = [1.0, 1.0, 0.0, 1.0]; - let color_g = [0.0, 1.0, 0.0, 1.0]; - let color_gb = [0.0, 1.0, 0.5, 1.0]; - let color_b = [0.0, 0.2, 1.0, 1.0]; - let color_i = [0.5, 0.0, 1.0, 1.0]; - let color_v = [0.75, 0.0, 0.5, 1.0]; + // let color_r = [1.0, 0.0, 0.0, 1.0]; + // let color_o = [1.0, 0.5, 0.0, 1.0]; + // let color_y = [1.0, 1.0, 0.0, 1.0]; + // let color_g = [0.0, 1.0, 0.0, 1.0]; + // let color_gb = [0.0, 1.0, 0.5, 1.0]; + // let color_b = [0.0, 0.2, 1.0, 1.0]; + // let color_i = [0.5, 0.0, 1.0, 1.0]; + // let color_v = [0.75, 0.0, 0.5, 1.0]; let posn_center = { if b.contains(cursor_position) { @@ -101,39 +105,39 @@ mod rainbow { vertices: vec![ Vertex2D { position: posn_center, - color: [1.0, 1.0, 1.0, 1.0], + // color: [1.0, 1.0, 1.0, 1.0], }, Vertex2D { position: posn_tl, - color: color_r, + // color: color_r, }, Vertex2D { position: posn_t, - color: color_o, + // color: color_o, }, Vertex2D { position: posn_tr, - color: color_y, + // color: color_y, }, Vertex2D { position: posn_r, - color: color_g, + // color: color_g, }, Vertex2D { position: posn_br, - color: color_gb, + // color: color_gb, }, Vertex2D { position: posn_b, - color: color_b, + // color: color_b, }, Vertex2D { position: posn_bl, - color: color_i, + // color: color_i, }, Vertex2D { position: posn_l, - color: color_v, + // color: color_v, }, ], indices: vec![ @@ -147,6 +151,7 @@ mod rainbow { 0, 8, 1, // L ], }, + shader: Shader::Solid(Color::BLACK), }; renderer.with_translation(Vector::new(b.x, b.y), |renderer| { diff --git a/examples/modern_art/Cargo.toml b/examples/modern_art/Cargo.toml new file mode 100644 index 00000000..4995e9a7 --- /dev/null +++ b/examples/modern_art/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "modern_art" +version = "0.1.0" +authors = ["Bingus "] +edition = "2021" +publish = false + +[dependencies] +iced = { path = "../..", features = ["canvas", "tokio", "debug"] } +rand = "0.8.5" \ No newline at end of file diff --git a/examples/modern_art/src/main.rs b/examples/modern_art/src/main.rs new file mode 100644 index 00000000..17149cbe --- /dev/null +++ b/examples/modern_art/src/main.rs @@ -0,0 +1,141 @@ +use rand::{Rng, thread_rng}; +use crate::canvas::{Cursor, FillStyle, Geometry, Gradient}; +use iced::widget::canvas::{Cache, Fill, Frame}; +use iced::widget::{canvas, Canvas}; +use iced::Settings; +use iced::{ + executor, Application, Color, Command, Element, Length, Point, Rectangle, + Renderer, Size, Theme, +}; + +fn main() -> iced::Result { + ModernArt::run(Settings { + antialiasing: true, + ..Settings::default() + }) +} + +#[derive(Debug, Clone, Copy)] +enum Message {} + +struct ModernArt { + cache: Cache, +} + +impl Application for ModernArt { + type Executor = executor::Default; + type Message = Message; + type Theme = Theme; + type Flags = (); + + fn new(_flags: Self::Flags) -> (Self, Command) { + ( + ModernArt { + cache: Default::default(), + }, + Command::none(), + ) + } + + fn title(&self) -> String { + String::from("Modern Art") + } + + fn update(&mut self, _message: Self::Message) -> Command { + Command::none() + } + + fn view(&self) -> Element<'_, Self::Message, Renderer> { + Canvas::new(self) + .width(Length::Fill) + .height(Length::Fill) + .into() + } +} + +impl canvas::Program for ModernArt { + type State = (); + + fn draw( + &self, + _state: &Self::State, + _theme: &Theme, + bounds: Rectangle, + _cursor: Cursor, + ) -> Vec { + let geometry = self.cache.draw(bounds.size(), |frame| { + let num_squares = thread_rng().gen_range(0..1200); + + let mut i = 0; + while i <= num_squares { + generate_box(frame, bounds.size()); + i += 1; + } + }); + + vec![geometry] + } +} + +fn generate_box(frame: &mut Frame, bounds: Size) -> bool { + let solid = rand::random::(); + + let random_color = || -> Color { + Color::from_rgb( + thread_rng().gen_range(0.0..1.0), + thread_rng().gen_range(0.0..1.0), + thread_rng().gen_range(0.0..1.0), + ) + }; + + let gradient = |top_left: Point, bottom_right: Point| -> Gradient { + let mut builder = Gradient::linear(top_left, bottom_right); + let stops = thread_rng().gen_range(1..64u32); + + let mut i = 0; + while i <= stops { + builder = builder.add_stop( + i as f32 / stops as f32, + random_color() + ); + i += 1; + } + + builder.build().unwrap() + }; + + let top_left = Point::new( + thread_rng().gen_range(0.0..bounds.width), + thread_rng().gen_range(0.0..bounds.height) + ); + + let size = Size::new( + thread_rng().gen_range(50.0..200.0), + thread_rng().gen_range(50.0..200.0), + ); + + if solid { + frame.fill_rectangle( + top_left, + size, + Fill { + style: FillStyle::Solid(random_color()), + .. Default::default() + } + ); + } else { + frame.fill_rectangle( + top_left, + size, + Fill { + style: FillStyle::Gradient(&gradient( + top_left, + Point::new(top_left.x + size.width, top_left.y + size.height) + )), + .. Default::default() + } + ); + }; + + solid +} \ No newline at end of file diff --git a/examples/solar_system/src/main.rs b/examples/solar_system/src/main.rs index c59d73a8..fcd20561 100644 --- a/examples/solar_system/src/main.rs +++ b/examples/solar_system/src/main.rs @@ -19,6 +19,7 @@ use iced::{ }; use std::time::Instant; +use crate::canvas::StrokeStyle; pub fn main() -> iced::Result { SolarSystem::run(Settings { @@ -37,9 +38,9 @@ enum Message { } impl Application for SolarSystem { + type Executor = executor::Default; type Message = Message; type Theme = Theme; - type Executor = executor::Default; type Flags = (); fn new(_flags: ()) -> (Self, Command) { @@ -65,10 +66,6 @@ impl Application for SolarSystem { Command::none() } - fn subscription(&self) -> Subscription { - time::every(std::time::Duration::from_millis(10)).map(Message::Tick) - } - fn view(&self) -> Element { canvas(&self.state) .width(Length::Fill) @@ -86,6 +83,10 @@ impl Application for SolarSystem { text_color: Color::WHITE, }) } + + fn subscription(&self) -> Subscription { + time::every(std::time::Duration::from_millis(10)).map(Message::Tick) + } } #[derive(Debug)] @@ -178,8 +179,8 @@ impl canvas::Program for State { frame.stroke( &orbit, Stroke { + style: StrokeStyle::Solid(Color::from_rgba8(0, 153, 255, 0.1)), width: 1.0, - color: Color::from_rgba8(0, 153, 255, 0.1), line_dash: canvas::LineDash { offset: 0, segments: &[3.0, 6.0], diff --git a/glow/src/backend.rs b/glow/src/backend.rs index 78d4229e..6fc4fb38 100644 --- a/glow/src/backend.rs +++ b/glow/src/backend.rs @@ -1,7 +1,6 @@ -use crate::program; +use crate::{program, triangle}; use crate::quad; use crate::text; -use crate::triangle; use crate::{Settings, Transformation, Viewport}; use iced_graphics::backend; @@ -100,16 +99,16 @@ impl Backend { ); } - if !layer.meshes.is_empty() { + if !layer.meshes.0.is_empty() { let scaled = transformation * Transformation::scale(scale_factor, scale_factor); self.triangle_pipeline.draw( + &layer.meshes, gl, target_height, scaled, scale_factor, - &layer.meshes, ); } diff --git a/glow/src/shader/common/gradient.frag b/glow/src/shader/common/gradient.frag new file mode 100644 index 00000000..588f63e0 --- /dev/null +++ b/glow/src/shader/common/gradient.frag @@ -0,0 +1,48 @@ +// GLSL does not support dynamically sized arrays without SSBOs +#define MAX_STOPS 64 + +#ifdef GL_ES +#ifdef GL_FRAGMENT_PRECISION_HIGH +precision highp float; +#else +precision mediump float; +#endif +#endif + +#ifdef HIGHER_THAN_300 +layout (location = 0) out vec4 fragColor; +#define gl_FragColor fragColor +#endif + +in vec2 raw_position; + +uniform vec2 gradient_start; +uniform vec2 gradient_end; + +uniform uint color_stops_size; +uniform float color_stop_offsets[MAX_STOPS]; +uniform vec4 color_stop_colors[MAX_STOPS]; + +void main() { + vec2 gradient_vec = vec2(gradient_end - gradient_start); + vec2 current_vec = vec2(raw_position.xy - gradient_start); + vec2 unit = normalize(gradient_vec); + float coord_offset = dot(unit, current_vec) / length(gradient_vec); + + for (uint i = 0; i < color_stops_size - 1; i++) { + float stop_offset = color_stop_offsets[i]; + float next_stop_offset = color_stop_offsets[i + 1]; + + if (stop_offset <= coord_offset && coord_offset <= next_stop_offset) { + fragColor = mix(color_stop_colors[i], color_stop_colors[i+1], smoothstep( + stop_offset, + next_stop_offset, + coord_offset + )); + } else if (coord_offset < color_stop_offsets[0]) { + fragColor = color_stop_colors[0]; + } else if (coord_offset > color_stop_offsets[color_stops_size - 1]) { + fragColor = color_stop_colors[color_stops_size - 1]; + } + } +} \ No newline at end of file diff --git a/glow/src/shader/common/triangle.frag b/glow/src/shader/common/triangle.frag index e8689f2e..0ee65239 100644 --- a/glow/src/shader/common/triangle.frag +++ b/glow/src/shader/common/triangle.frag @@ -11,8 +11,8 @@ out vec4 fragColor; #define gl_FragColor fragColor #endif -in vec4 v_Color; +uniform vec4 color; void main() { - gl_FragColor = v_Color; + fragColor = color; } \ No newline at end of file diff --git a/glow/src/shader/common/triangle.vert b/glow/src/shader/common/triangle.vert index d0494a5f..09a4e324 100644 --- a/glow/src/shader/common/triangle.vert +++ b/glow/src/shader/common/triangle.vert @@ -1,11 +1,9 @@ uniform mat4 u_Transform; in vec2 i_Position; -in vec4 i_Color; - -out vec4 v_Color; +out vec2 raw_position; void main() { gl_Position = u_Transform * vec4(i_Position, 0.0, 1.0); - v_Color = i_Color; + raw_position = i_Position; } \ No newline at end of file diff --git a/glow/src/triangle.rs b/glow/src/triangle.rs index ae4f83ef..7d0e14c7 100644 --- a/glow/src/triangle.rs +++ b/glow/src/triangle.rs @@ -1,66 +1,41 @@ -//! Draw meshes of triangles. +//! Draw meshes of triangle. +mod gradient; +mod solid; + use crate::program::{self, Shader}; use crate::Transformation; use glow::HasContext; -use iced_graphics::layer; +use iced_graphics::layer::{Mesh, Meshes}; +use iced_graphics::shader; use std::marker::PhantomData; +use crate::triangle::gradient::GradientProgram; +use crate::triangle::solid::SolidProgram; pub use iced_graphics::triangle::{Mesh2D, Vertex2D}; -const VERTEX_BUFFER_SIZE: usize = 10_000; -const INDEX_BUFFER_SIZE: usize = 10_000; - #[derive(Debug)] pub(crate) struct Pipeline { - program: ::Program, vertex_array: ::VertexArray, vertices: Buffer, indices: Buffer, - transform_location: ::UniformLocation, current_transform: Transformation, + programs: TrianglePrograms, } -impl Pipeline { - pub fn new( - gl: &glow::Context, - shader_version: &program::Version, - ) -> Pipeline { - let program = unsafe { - let vertex_shader = Shader::vertex( - gl, - shader_version, - include_str!("shader/common/triangle.vert"), - ); - let fragment_shader = Shader::fragment( - gl, - shader_version, - include_str!("shader/common/triangle.frag"), - ); - - program::create( - gl, - &[vertex_shader, fragment_shader], - &[(0, "i_Position"), (1, "i_Color")], - ) - }; - - let transform_location = - unsafe { gl.get_uniform_location(program, "u_Transform") } - .expect("Get transform location"); - - unsafe { - gl.use_program(Some(program)); - - let transform: [f32; 16] = Transformation::identity().into(); - gl.uniform_matrix_4_f32_slice( - Some(&transform_location), - false, - &transform, - ); +#[derive(Debug)] +struct TrianglePrograms { + solid: TriangleProgram, + gradient: TriangleProgram, +} - gl.use_program(None); - } +#[derive(Debug)] +enum TriangleProgram { + Solid(SolidProgram), + Gradient(GradientProgram), +} +impl Pipeline { + pub fn new(gl: &glow::Context, shader_version: &program::Version) -> Self { let vertex_array = unsafe { gl.create_vertex_array().expect("Create vertex array") }; @@ -73,7 +48,7 @@ impl Pipeline { gl, glow::ARRAY_BUFFER, glow::DYNAMIC_DRAW, - VERTEX_BUFFER_SIZE, + std::mem::size_of::() as usize, ) }; @@ -82,7 +57,7 @@ impl Pipeline { gl, glow::ELEMENT_ARRAY_BUFFER, glow::DYNAMIC_DRAW, - INDEX_BUFFER_SIZE, + std::mem::size_of::() as usize, ) }; @@ -92,58 +67,45 @@ impl Pipeline { gl.enable_vertex_attrib_array(0); gl.vertex_attrib_pointer_f32(0, 2, glow::FLOAT, false, stride, 0); - gl.enable_vertex_attrib_array(1); - gl.vertex_attrib_pointer_f32( - 1, - 4, - glow::FLOAT, - false, - stride, - 4 * 2, - ); - gl.bind_vertex_array(None); - } + }; - Pipeline { - program, + Self { vertex_array, vertices, indices, - transform_location, current_transform: Transformation::identity(), + programs: TrianglePrograms { + solid: TriangleProgram::Solid(SolidProgram::new( + gl, + shader_version, + )), + gradient: TriangleProgram::Gradient(GradientProgram::new( + gl, + shader_version, + )), + }, } } pub fn draw( &mut self, + meshes: &Meshes<'_>, gl: &glow::Context, target_height: u32, transformation: Transformation, scale_factor: f32, - meshes: &[layer::Mesh<'_>], ) { unsafe { gl.enable(glow::MULTISAMPLE); gl.enable(glow::SCISSOR_TEST); - gl.use_program(Some(self.program)); - gl.bind_vertex_array(Some(self.vertex_array)); + gl.bind_vertex_array(Some(self.vertex_array)) } - // This looks a bit crazy, but we are just counting how many vertices - // and indices we will need to handle. - // TODO: Improve readability - let (total_vertices, total_indices) = meshes - .iter() - .map(|layer::Mesh { buffers, .. }| { - (buffers.vertices.len(), buffers.indices.len()) - }) - .fold((0, 0), |(total_v, total_i), (v, i)| { - (total_v + v, total_i + i) - }); - - // Then we ensure the current buffers are big enough, resizing if - // necessary + //count the total number of vertices & indices we need to handle for all meshes + let (total_vertices, total_indices) = meshes.attribute_count(); + + // Then we ensure the current attribute buffers are big enough, resizing if necessary unsafe { self.vertices.bind(gl, total_vertices); self.indices.bind(gl, total_indices); @@ -153,7 +115,7 @@ impl Pipeline { let mut last_vertex = 0; let mut last_index = 0; - for layer::Mesh { buffers, .. } in meshes { + for Mesh { buffers, .. } in meshes.0.iter() { unsafe { gl.buffer_sub_data_u8_slice( glow::ARRAY_BUFFER, @@ -176,11 +138,12 @@ impl Pipeline { let mut last_vertex = 0; let mut last_index = 0; - for layer::Mesh { + for Mesh { buffers, origin, clip_bounds, - } in meshes + shader, + } in meshes.0.iter() { let transform = transformation * Transformation::translate(origin.x, origin.y); @@ -188,17 +151,6 @@ impl Pipeline { let clip_bounds = (*clip_bounds * scale_factor).snap(); unsafe { - if self.current_transform != transform { - let matrix: [f32; 16] = transform.into(); - gl.uniform_matrix_4_f32_slice( - Some(&self.transform_location), - false, - &matrix, - ); - - self.current_transform = transform; - } - gl.scissor( clip_bounds.x as i32, (target_height - (clip_bounds.y + clip_bounds.height)) @@ -207,6 +159,15 @@ impl Pipeline { clip_bounds.height as i32, ); + let t = if self.current_transform != transform { + self.current_transform = transform; + Some(transform) + } else { + None + }; + + self.use_with_shader(gl, shader, t); + gl.draw_elements_base_vertex( glow::TRIANGLES, buffers.indices.len() as i32, @@ -222,34 +183,79 @@ impl Pipeline { unsafe { gl.bind_vertex_array(None); - gl.use_program(None); gl.disable(glow::SCISSOR_TEST); gl.disable(glow::MULTISAMPLE); } } -} -#[repr(C)] -#[derive(Debug, Clone, Copy)] -struct Uniforms { - transform: [f32; 16], + fn use_with_shader( + &mut self, + gl: &glow::Context, + shader: &shader::Shader, + transform: Option, + ) { + match shader { + shader::Shader::Solid(color) => { + if let TriangleProgram::Solid(solid_program) = + &mut self.programs.solid + { + unsafe { gl.use_program(Some(solid_program.program)) } + solid_program.set_uniforms(gl, color, transform); + } + } + shader::Shader::Gradient(gradient) => { + if let TriangleProgram::Gradient(gradient_program) = + &mut self.programs.gradient + { + unsafe { gl.use_program(Some(gradient_program.program)) } + gradient_program.set_uniforms(gl, gradient, transform); + } + } + } + } } -unsafe impl bytemuck::Zeroable for Uniforms {} -unsafe impl bytemuck::Pod for Uniforms {} - -impl Default for Uniforms { - fn default() -> Self { - Self { - transform: *Transformation::identity().as_ref(), - } +/// A simple shader program. Uses [`triangle.vert`] for its vertex shader and only binds position +/// attribute location. +pub(super) fn simple_triangle_program( + gl: &glow::Context, + shader_version: &program::Version, + fragment_shader: &'static str, +) -> ::Program { + unsafe { + let vertex_shader = Shader::vertex( + gl, + shader_version, + include_str!("shader/common/triangle.vert"), + ); + + let fragment_shader = + Shader::fragment(gl, shader_version, fragment_shader); + + program::create( + gl, + &[vertex_shader, fragment_shader], + &[(0, "i_Position")], + ) } } -impl From for Uniforms { - fn from(transformation: Transformation) -> Uniforms { - Self { - transform: transformation.into(), +pub(super) fn update_transform( + gl: &glow::Context, + program: ::Program, + transform: Option +) { + if let Some(t) = transform { + let transform_location = + unsafe { gl.get_uniform_location(program, "u_Transform") } + .expect("Get transform location."); + + unsafe { + gl.uniform_matrix_4_f32_slice( + Some(&transform_location), + false, + t.as_ref(), + ); } } } diff --git a/glow/src/triangle/gradient.rs b/glow/src/triangle/gradient.rs new file mode 100644 index 00000000..d1b10d77 --- /dev/null +++ b/glow/src/triangle/gradient.rs @@ -0,0 +1,189 @@ +use crate::program::Version; +use crate::triangle::{simple_triangle_program, update_transform}; +use glow::{Context, HasContext, NativeProgram}; +use iced_graphics::gradient::Gradient; +use iced_graphics::widget::canvas::gradient::Linear; +use iced_graphics::Transformation; + +#[derive(Debug)] +pub(super) struct GradientProgram { + pub(super) program: ::Program, + pub(super) uniform_data: GradientUniformData, +} + +impl GradientProgram { + pub(super) fn new(gl: &Context, shader_version: &Version) -> Self { + let program = simple_triangle_program( + gl, + shader_version, + include_str!("../shader/common/gradient.frag"), + ); + + Self { + program, + uniform_data: GradientUniformData::new(gl, program), + } + } + + pub(super) fn set_uniforms<'a>( + &mut self, + gl: &Context, + gradient: &Gradient, + transform: Option, + ) { + update_transform(gl, self.program, transform); + + if &self.uniform_data.current_gradient != gradient { + match gradient { + Gradient::Linear(linear) => { + let gradient_start: [f32; 2] = (linear.start).into(); + let gradient_end: [f32; 2] = (linear.end).into(); + + unsafe { + gl.uniform_2_f32( + Some( + &self + .uniform_data + .uniform_locations + .gradient_start_location, + ), + gradient_start[0], + gradient_start[1], + ); + + gl.uniform_2_f32( + Some( + &self + .uniform_data + .uniform_locations + .gradient_end_location, + ), + gradient_end[0], + gradient_end[1], + ); + + gl.uniform_1_u32( + Some( + &self + .uniform_data + .uniform_locations + .color_stops_size_location, + ), + linear.color_stops.len() as u32, + ); + + for (index, stop) in + linear.color_stops.iter().enumerate() + { + gl.uniform_1_f32( + Some( + &self + .uniform_data + .uniform_locations + .color_stops_locations[index] + .offset, + ), + stop.offset, + ); + + gl.uniform_4_f32( + Some( + &self + .uniform_data + .uniform_locations + .color_stops_locations[index] + .color, + ), + stop.color.r, + stop.color.g, + stop.color.b, + stop.color.a, + ); + } + } + } + } + + self.uniform_data.current_gradient = gradient.clone(); + } + } +} + +#[derive(Debug)] +pub(super) struct GradientUniformData { + current_gradient: Gradient, + uniform_locations: GradientUniformLocations, +} + +#[derive(Debug)] +struct GradientUniformLocations { + gradient_start_location: ::UniformLocation, + gradient_end_location: ::UniformLocation, + color_stops_size_location: ::UniformLocation, + //currently the maximum number of stops is 64 due to needing to allocate the + //memory for the array of stops with a const value in GLSL + color_stops_locations: [ColorStopLocation; 64], +} + +#[derive(Copy, Debug, Clone)] +struct ColorStopLocation { + color: ::UniformLocation, + offset: ::UniformLocation, +} + +impl GradientUniformData { + fn new(gl: &Context, program: NativeProgram) -> Self { + let gradient_start_location = + unsafe { gl.get_uniform_location(program, "gradient_start") } + .expect("Gradient - Get gradient_start."); + + let gradient_end_location = + unsafe { gl.get_uniform_location(program, "gradient_end") } + .expect("Gradient - Get gradient_end."); + + let color_stops_size_location = + unsafe { gl.get_uniform_location(program, "color_stops_size") } + .expect("Gradient - Get color_stops_size."); + + let color_stops_locations: [ColorStopLocation; 64] = + core::array::from_fn(|index| { + let offset = unsafe { + gl.get_uniform_location( + program, + &format!("color_stop_offsets[{}]", index), + ) + } + .expect(&format!( + "Gradient - Color stop offset with index {}", + index + )); + + let color = unsafe { + gl.get_uniform_location( + program, + &format!("color_stop_colors[{}]", index), + ) + } + .expect(&format!( + "Gradient - Color stop colors with index {}", + index + )); + + ColorStopLocation { color, offset } + }); + + GradientUniformData { + current_gradient: Gradient::Linear(Linear { + start: Default::default(), + end: Default::default(), + color_stops: vec![], + }), + uniform_locations: GradientUniformLocations { + gradient_start_location, + gradient_end_location, + color_stops_size_location, + color_stops_locations, + }, + } + } +} diff --git a/glow/src/triangle/solid.rs b/glow/src/triangle/solid.rs new file mode 100644 index 00000000..3a33cea8 --- /dev/null +++ b/glow/src/triangle/solid.rs @@ -0,0 +1,67 @@ +use crate::program::Version; +use crate::triangle::{simple_triangle_program, update_transform}; +use crate::Color; +use glow::{Context, HasContext, NativeProgram}; +use iced_graphics::Transformation; + +#[derive(Debug)] +pub struct SolidProgram { + pub(crate) program: ::Program, + pub(crate) uniform_data: SolidUniformData, +} + +impl SolidProgram { + pub fn new(gl: &Context, shader_version: &Version) -> Self { + let program = simple_triangle_program( + gl, + shader_version, + include_str!("../shader/common/triangle.frag"), + ); + + Self { + program, + uniform_data: SolidUniformData::new(gl, program), + } + } + + pub fn set_uniforms<'a>( + &mut self, + gl: &Context, + color: &Color, + transform: Option, + ) { + update_transform(gl, self.program, transform); + + if &self.uniform_data.color != color { + unsafe { + gl.uniform_4_f32( + Some(&self.uniform_data.color_location), + color.r, + color.g, + color.b, + color.a, + ); + } + + self.uniform_data.color = *color; + } + } +} + +#[derive(Debug)] +pub(crate) struct SolidUniformData { + pub color: Color, + pub color_location: ::UniformLocation, +} + +impl SolidUniformData { + fn new(gl: &Context, program: NativeProgram) -> Self { + Self { + color: Color::TRANSPARENT, + color_location: unsafe { + gl.get_uniform_location(program, "color") + } + .expect("Solid - Color uniform location."), + } + } +} diff --git a/glow/src/window/compositor.rs b/glow/src/window/compositor.rs index f6afaa68..e9cf6015 100644 --- a/glow/src/window/compositor.rs +++ b/glow/src/window/compositor.rs @@ -26,8 +26,7 @@ impl iced_graphics::window::GLCompositor for Compositor { log::info!("{:#?}", settings); let version = gl.version(); - log::info!("Version: {:?}", version); - log::info!("Embedded: {}", version.is_embedded); + log::info!("OpenGL version: {:?} (Embedded: {}", version, version.is_embedded); let renderer = gl.get_parameter_string(glow::RENDERER); log::info!("Renderer: {}", renderer); diff --git a/graphics/Cargo.toml b/graphics/Cargo.toml index 49d4d9c6..ff6fcd4a 100644 --- a/graphics/Cargo.toml +++ b/graphics/Cargo.toml @@ -19,7 +19,7 @@ font-icons = [] opengl = [] [dependencies] -glam = "0.10" +glam = "0.21.3" raw-window-handle = "0.4" thiserror = "1.0" diff --git a/graphics/src/gradient.rs b/graphics/src/gradient.rs new file mode 100644 index 00000000..4d1eff62 --- /dev/null +++ b/graphics/src/gradient.rs @@ -0,0 +1,23 @@ +//! For creating a Gradient. + +use iced_native::Color; +use crate::widget::canvas::gradient::Linear; + +#[derive(Debug, Clone, PartialEq)] +/// A fill which transitions colors progressively along a direction, either linearly, radially, +/// or conically. +pub enum Gradient { + /// A linear gradient interpolates colors along a direction from its [`start`] to its [`end`] + /// point. + Linear(Linear), +} + + +#[derive(Debug, Clone, Copy, PartialEq)] +/// A point along the gradient vector where the specified [`color`] is unmixed. +pub struct ColorStop { + /// Offset along the gradient vector. + pub offset: f32, + /// The color of the gradient at the specified [`offset`]. + pub color: Color, +} \ No newline at end of file diff --git a/graphics/src/layer.rs b/graphics/src/layer.rs index af545713..b7731922 100644 --- a/graphics/src/layer.rs +++ b/graphics/src/layer.rs @@ -7,9 +7,10 @@ use crate::{ use iced_native::image; use iced_native::svg; +use crate::shader::Shader; /// A group of primitives that should be clipped together. -#[derive(Debug, Clone)] +#[derive(Debug)] pub struct Layer<'a> { /// The clipping bounds of the [`Layer`]. pub bounds: Rectangle, @@ -18,7 +19,7 @@ pub struct Layer<'a> { pub quads: Vec, /// The triangle meshes of the [`Layer`]. - pub meshes: Vec>, + pub meshes: Meshes<'a>, /// The text of the [`Layer`]. pub text: Vec>, @@ -33,7 +34,7 @@ impl<'a> Layer<'a> { Self { bounds, quads: Vec::new(), - meshes: Vec::new(), + meshes: Meshes(Vec::new()), text: Vec::new(), images: Vec::new(), } @@ -159,7 +160,11 @@ impl<'a> Layer<'a> { border_color: border_color.into_linear(), }); } - Primitive::Mesh2D { buffers, size } => { + Primitive::Mesh2D { + buffers, + size, + shader, + } => { let layer = &mut layers[current_layer]; let bounds = Rectangle::new( @@ -169,11 +174,14 @@ impl<'a> Layer<'a> { // Only draw visible content if let Some(clip_bounds) = layer.bounds.intersection(&bounds) { - layer.meshes.push(Mesh { - origin: Point::new(translation.x, translation.y), - buffers, - clip_bounds, - }); + layer.meshes.0.push( + Mesh { + origin: Point::new(translation.x, translation.y), + buffers, + clip_bounds, + shader, + } + ); } } Primitive::Clip { bounds, content } => { @@ -270,6 +278,9 @@ pub struct Mesh<'a> { /// The clipping bounds of the [`Mesh`]. pub clip_bounds: Rectangle, + + /// The shader of the [`Mesh`]. + pub shader: &'a Shader, } /// A paragraph of text. @@ -323,3 +334,21 @@ unsafe impl bytemuck::Zeroable for Quad {} #[allow(unsafe_code)] unsafe impl bytemuck::Pod for Quad {} + +#[derive(Debug)] +/// A collection of meshes. +pub struct Meshes<'a>(pub Vec>); + +impl<'a> Meshes<'a> { + /// Returns the number of total vertices & total indices of all [`Mesh`]es. + pub fn attribute_count(&self) -> (usize, usize) { + self.0 + .iter() + .map(|Mesh { buffers, .. }| { + (buffers.vertices.len(), buffers.indices.len()) + }) + .fold((0, 0), |(total_v, total_i), (v, i)| { + (total_v + v, total_i + i) + }) + } +} \ No newline at end of file diff --git a/graphics/src/lib.rs b/graphics/src/lib.rs index 11082472..ce9b1b07 100644 --- a/graphics/src/lib.rs +++ b/graphics/src/lib.rs @@ -35,6 +35,8 @@ pub mod renderer; pub mod triangle; pub mod widget; pub mod window; +pub mod shader; +pub mod gradient; pub use antialiasing::Antialiasing; pub use backend::Backend; diff --git a/graphics/src/primitive.rs b/graphics/src/primitive.rs index 5f7a344d..4f79a74c 100644 --- a/graphics/src/primitive.rs +++ b/graphics/src/primitive.rs @@ -2,7 +2,7 @@ use iced_native::image; use iced_native::svg; use iced_native::{Background, Color, Font, Rectangle, Size, Vector}; -use crate::alignment; +use crate::{alignment, shader}; use crate::triangle; use std::sync::Arc; @@ -88,6 +88,9 @@ pub enum Primitive { /// /// Any geometry that falls out of this region will be clipped. size: Size, + + /// The shader of the mesh + shader: shader::Shader, }, /// A cached primitive. /// diff --git a/graphics/src/shader.rs b/graphics/src/shader.rs new file mode 100644 index 00000000..b9071c74 --- /dev/null +++ b/graphics/src/shader.rs @@ -0,0 +1,42 @@ +//! Supported shaders; + +use crate::{Color, widget}; +use crate::gradient::Gradient; +use crate::widget::canvas::{FillStyle, StrokeStyle}; + +#[derive(Debug, Clone)] +/// Supported shaders for primitives. +pub enum Shader { + /// Fill a primitive with a solid color. + Solid(Color), + /// Fill a primitive with an interpolated color. + Gradient(Gradient) +} + +impl <'a> Into for StrokeStyle<'a> { + fn into(self) -> Shader { + match self { + StrokeStyle::Solid(color) => Shader::Solid(color), + StrokeStyle::Gradient(gradient) => gradient.clone().into() + } + } +} + +impl <'a> Into for FillStyle<'a> { + fn into(self) -> Shader { + match self { + FillStyle::Solid(color) => Shader::Solid(color), + FillStyle::Gradient(gradient) => gradient.clone().into() + } + } +} + +impl <'a> Into for widget::canvas::Gradient { + fn into(self) -> Shader { + match self { + widget::canvas::Gradient::Linear(linear) => { + Shader::Gradient(Gradient::Linear(linear)) + } + } + } +} \ No newline at end of file diff --git a/graphics/src/transformation.rs b/graphics/src/transformation.rs index 2a19caed..03f453db 100644 --- a/graphics/src/transformation.rs +++ b/graphics/src/transformation.rs @@ -8,7 +8,7 @@ pub struct Transformation(Mat4); impl Transformation { /// Get the identity transformation. pub fn identity() -> Transformation { - Transformation(Mat4::identity()) + Transformation(Mat4::IDENTITY) } /// Creates an orthographic projection. @@ -51,3 +51,9 @@ impl From for [f32; 16] { *t.as_ref() } } + +impl Into for Transformation { + fn into(self) -> Mat4 { + self.0 + } +} \ No newline at end of file diff --git a/graphics/src/triangle.rs b/graphics/src/triangle.rs index 05028f51..92709fe2 100644 --- a/graphics/src/triangle.rs +++ b/graphics/src/triangle.rs @@ -6,20 +6,22 @@ use bytemuck::{Pod, Zeroable}; pub struct Mesh2D { /// The vertices of the mesh pub vertices: Vec, - /// The list of vertex indices that defines the triangles of the mesh. - /// - /// Therefore, this list should always have a length that is a multiple of - /// 3. pub indices: Vec, } -/// A two-dimensional vertex with some color in __linear__ RGBA. +/// A two-dimensional vertex. #[derive(Copy, Clone, Debug, Zeroable, Pod)] #[repr(C)] pub struct Vertex2D { - /// The vertex position + /// The vertex position in 2D space. pub position: [f32; 2], - /// The vertex color in __linear__ RGBA. - pub color: [f32; 4], +} + +/// Convert from lyon's position data to Iced's Vertex2D type. +impl Vertex2D { + /// Converts from [`lyon::math::Point`] to [`Vertex2D`]. Used for generating primitives. + pub fn from(points: Vec) -> Vec { + points.iter().map(|p| Vertex2D { position: [p.x, p.y]}).collect() + } } diff --git a/graphics/src/widget/canvas.rs b/graphics/src/widget/canvas.rs index 88403fd7..09aad98d 100644 --- a/graphics/src/widget/canvas.rs +++ b/graphics/src/widget/canvas.rs @@ -5,6 +5,7 @@ //! and more! pub mod event; +pub mod gradient; pub mod path; mod cache; @@ -19,12 +20,13 @@ mod text; pub use cache::Cache; pub use cursor::Cursor; pub use event::Event; -pub use fill::{Fill, FillRule}; +pub use fill::{Fill, FillRule, FillStyle}; pub use frame::Frame; pub use geometry::Geometry; +pub use gradient::Gradient; pub use path::Path; pub use program::Program; -pub use stroke::{LineCap, LineDash, LineJoin, Stroke}; +pub use stroke::{LineCap, LineDash, LineJoin, Stroke, StrokeStyle}; pub use text::Text; use crate::{Backend, Primitive, Renderer}; diff --git a/graphics/src/widget/canvas/fill.rs b/graphics/src/widget/canvas/fill.rs index 56495435..02d2311f 100644 --- a/graphics/src/widget/canvas/fill.rs +++ b/graphics/src/widget/canvas/fill.rs @@ -1,12 +1,14 @@ use iced_native::Color; +use crate::widget::canvas::Gradient; + /// The style used to fill geometry. -#[derive(Debug, Clone, Copy)] -pub struct Fill { - /// The color used to fill geometry. +#[derive(Debug, Clone)] +pub struct Fill<'a> { + /// The color or gradient of the fill. /// - /// By default, it is set to `BLACK`. - pub color: Color, + /// By default, it is set to [`FillStyle::Solid`] `BLACK`. + pub style: FillStyle<'a>, /// The fill rule defines how to determine what is inside and what is /// outside of a shape. @@ -19,24 +21,33 @@ pub struct Fill { pub rule: FillRule, } -impl Default for Fill { - fn default() -> Fill { +impl <'a> Default for Fill<'a> { + fn default() -> Fill<'a> { Fill { - color: Color::BLACK, + style: FillStyle::Solid(Color::BLACK), rule: FillRule::NonZero, } } } -impl From for Fill { - fn from(color: Color) -> Fill { +impl<'a> From for Fill<'a> { + fn from(color: Color) -> Fill<'a> { Fill { - color, + style: FillStyle::Solid(color), ..Fill::default() } } } +/// The color or gradient of a [`Fill`]. +#[derive(Debug, Clone)] +pub enum FillStyle<'a> { + /// A solid color + Solid(Color), + /// A color gradient + Gradient(&'a Gradient), +} + /// The fill rule defines how to determine what is inside and what is outside of /// a shape. /// diff --git a/graphics/src/widget/canvas/frame.rs b/graphics/src/widget/canvas/frame.rs index 516539ca..8845bc6a 100644 --- a/graphics/src/widget/canvas/frame.rs +++ b/graphics/src/widget/canvas/frame.rs @@ -3,11 +3,13 @@ use std::borrow::Cow; use iced_native::{Point, Rectangle, Size, Vector}; use crate::triangle; -use crate::widget::canvas::path; -use crate::widget::canvas::{Fill, Geometry, Path, Stroke, Text}; +use crate::widget::canvas::{path, Fill, Geometry, Path, Stroke, Text}; use crate::Primitive; +use crate::shader::Shader; +use crate::triangle::Vertex2D; use lyon::tessellation; +use lyon::tessellation::geometry_builder::Positions; /// The frame of a [`Canvas`]. /// @@ -15,7 +17,7 @@ use lyon::tessellation; #[allow(missing_debug_implementations)] pub struct Frame { size: Size, - buffers: lyon::tessellation::VertexBuffers, + buffers: Vec<(tessellation::VertexBuffers, Shader)>, primitives: Vec, transforms: Transforms, fill_tessellator: tessellation::FillTessellator, @@ -42,7 +44,7 @@ impl Frame { pub fn new(size: Size) -> Frame { Frame { size, - buffers: lyon::tessellation::VertexBuffers::new(), + buffers: Vec::new(), primitives: Vec::new(), transforms: Transforms { previous: Vec::new(), @@ -82,18 +84,18 @@ impl Frame { /// Draws the given [`Path`] on the [`Frame`] by filling it with the /// provided style. - pub fn fill(&mut self, path: &Path, fill: impl Into) { - let Fill { color, rule } = fill.into(); + pub fn fill<'a>(&mut self, path: &Path, fill: impl Into>) { + let Fill { style, rule } = fill.into(); - let mut buffers = tessellation::BuffersBuilder::new( - &mut self.buffers, - FillVertex(color.into_linear()), - ); + let mut buf = tessellation::VertexBuffers::new(); + + let mut buffers = + tessellation::BuffersBuilder::new(&mut buf, Positions); let options = tessellation::FillOptions::default().with_fill_rule(rule.into()); - let result = if self.transforms.current.is_identity { + if self.transforms.current.is_identity { self.fill_tessellator.tessellate_path( path.raw(), &options, @@ -107,25 +109,24 @@ impl Frame { &options, &mut buffers, ) - }; + }.expect("Tessellate path."); - result.expect("Tessellate path"); + self.buffers.push((buf, style.into())) } /// Draws an axis-aligned rectangle given its top-left corner coordinate and /// its `Size` on the [`Frame`] by filling it with the provided style. - pub fn fill_rectangle( + pub fn fill_rectangle<'a>( &mut self, top_left: Point, size: Size, - fill: impl Into, + fill: impl Into>, ) { - let Fill { color, rule } = fill.into(); + let Fill { style, rule } = fill.into(); + + let mut buf = tessellation::VertexBuffers::new(); - let mut buffers = tessellation::BuffersBuilder::new( - &mut self.buffers, - FillVertex(color.into_linear()), - ); + let mut buffers = tessellation::BuffersBuilder::new(&mut buf, Positions); let top_left = self.transforms.current.raw.transform_point( @@ -147,6 +148,8 @@ impl Frame { &mut buffers, ) .expect("Fill rectangle"); + + self.buffers.push((buf, style.into())) } /// Draws the stroke of the given [`Path`] on the [`Frame`] with the @@ -154,10 +157,9 @@ impl Frame { pub fn stroke<'a>(&mut self, path: &Path, stroke: impl Into>) { let stroke = stroke.into(); - let mut buffers = tessellation::BuffersBuilder::new( - &mut self.buffers, - StrokeVertex(stroke.color.into_linear()), - ); + let mut buf = tessellation::VertexBuffers::new(); + + let mut buffers = tessellation::BuffersBuilder::new(&mut buf, Positions); let mut options = tessellation::StrokeOptions::default(); options.line_width = stroke.width; @@ -171,7 +173,7 @@ impl Frame { Cow::Owned(path::dashed(path, stroke.line_dash)) }; - let result = if self.transforms.current.is_identity { + if self.transforms.current.is_identity { self.stroke_tessellator.tessellate_path( path.raw(), &options, @@ -185,9 +187,9 @@ impl Frame { &options, &mut buffers, ) - }; + }.expect("Stroke path"); - result.expect("Stroke path"); + self.buffers.push((buf, stroke.style.into())) } /// Draws the characters of the given [`Text`] on the [`Frame`], filling @@ -206,8 +208,6 @@ impl Frame { /// /// [`Canvas`]: crate::widget::Canvas pub fn fill_text(&mut self, text: impl Into) { - use std::f32; - let text = text.into(); let position = if self.transforms.current.is_identity { @@ -331,52 +331,19 @@ impl Frame { } fn into_primitives(mut self) -> Vec { - if !self.buffers.indices.is_empty() { - self.primitives.push(Primitive::Mesh2D { - buffers: triangle::Mesh2D { - vertices: self.buffers.vertices, - indices: self.buffers.indices, - }, - size: self.size, - }); + for (buffer, shader) in self.buffers { + if !buffer.indices.is_empty() { + self.primitives.push(Primitive::Mesh2D { + buffers: triangle::Mesh2D { + vertices: Vertex2D::from(buffer.vertices), + indices: buffer.indices, + }, + size: self.size, + shader, + }) + } } self.primitives } } - -struct FillVertex([f32; 4]); - -impl lyon::tessellation::FillVertexConstructor - for FillVertex -{ - fn new_vertex( - &mut self, - vertex: lyon::tessellation::FillVertex<'_>, - ) -> triangle::Vertex2D { - let position = vertex.position(); - - triangle::Vertex2D { - position: [position.x, position.y], - color: self.0, - } - } -} - -struct StrokeVertex([f32; 4]); - -impl lyon::tessellation::StrokeVertexConstructor - for StrokeVertex -{ - fn new_vertex( - &mut self, - vertex: lyon::tessellation::StrokeVertex<'_, '_>, - ) -> triangle::Vertex2D { - let position = vertex.position(); - - triangle::Vertex2D { - position: [position.x, position.y], - color: self.0, - } - } -} diff --git a/graphics/src/widget/canvas/gradient.rs b/graphics/src/widget/canvas/gradient.rs new file mode 100644 index 00000000..7d2daabc --- /dev/null +++ b/graphics/src/widget/canvas/gradient.rs @@ -0,0 +1,21 @@ +//! Define a color gradient. +use iced_native::Point; + +pub mod linear; + +pub use linear::Linear; + +/// A gradient that can be used in the style of [`super::Fill`] or [`super::Stroke`]. +#[derive(Debug, Clone)] +pub enum Gradient { + /// A linear gradient + Linear(Linear), + //TODO: radial, conical +} + +impl Gradient { + /// Creates a new linear [`linear::Builder`]. + pub fn linear(start: Point, end: Point) -> linear::Builder { + linear::Builder::new(start, end) + } +} \ No newline at end of file diff --git a/graphics/src/widget/canvas/gradient/linear.rs b/graphics/src/widget/canvas/gradient/linear.rs new file mode 100644 index 00000000..37533e19 --- /dev/null +++ b/graphics/src/widget/canvas/gradient/linear.rs @@ -0,0 +1,73 @@ +//! A linear color gradient. +use iced_native::{Color, Point}; + +use crate::gradient::ColorStop; + +use super::Gradient; + +/// A linear gradient that can be used in the style of [`super::Fill`] or [`super::Stroke`]. +#[derive(Debug, Clone, PartialEq)] +pub struct Linear { + /// The point where the linear gradient begins. + pub start: Point, + /// The point where the linear gradient ends. + pub end: Point, + /// [`ColorStop`]s along the linear gradient path. + pub color_stops: Vec, +} + +/// A [`Linear`] builder. +#[derive(Debug)] +pub struct Builder { + start: Point, + end: Point, + stops: Vec<(f32, Color)>, + valid: bool, +} + +impl Builder { + /// Creates a new [`Builder`]. + pub fn new(start: Point, end: Point) -> Self { + Self { + start, + end, + stops: vec![], + valid: true, + } + } + + /// Adds a new stop, defined by an offset and a color, to the gradient. + /// + /// `offset` must be between `0.0` and `1.0`. + pub fn add_stop(mut self, offset: f32, color: Color) -> Self { + if !(0.0..=1.0).contains(&offset) { + self.valid = false; + } + + self.stops.push((offset, color)); + self + } + + /// Builds the linear [`Gradient`] of this [`Builder`]. + /// + /// Returns `None` if no stops were added to the builder or + /// if stops not between 0.0 and 1.0 were added. + pub fn build(self) -> Option { + if self.stops.is_empty() || !self.valid { + return None; + } + + Some(Gradient::Linear(Linear { + start: self.start, + end: self.end, + color_stops: self + .stops + .into_iter() + .map(|f| ColorStop { + offset: f.0, + color: f.1, + }) + .collect(), + })) + } +} diff --git a/graphics/src/widget/canvas/stroke.rs b/graphics/src/widget/canvas/stroke.rs index 6accc2fb..c319b398 100644 --- a/graphics/src/widget/canvas/stroke.rs +++ b/graphics/src/widget/canvas/stroke.rs @@ -1,10 +1,14 @@ use iced_native::Color; +use crate::widget::canvas::Gradient; + /// The style of a stroke. -#[derive(Debug, Clone, Copy)] +#[derive(Debug, Clone)] pub struct Stroke<'a> { - /// The color of the stroke. - pub color: Color, + /// The color or gradient of the stroke. + /// + /// By default, it is set to [`StrokeStyle::Solid`] `BLACK`. + pub style: StrokeStyle<'a>, /// The distance between the two edges of the stroke. pub width: f32, /// The shape to be used at the end of open subpaths when they are stroked. @@ -19,7 +23,10 @@ pub struct Stroke<'a> { impl<'a> Stroke<'a> { /// Sets the color of the [`Stroke`]. pub fn with_color(self, color: Color) -> Self { - Stroke { color, ..self } + Stroke { + style: StrokeStyle::Solid(color), + ..self + } } /// Sets the width of the [`Stroke`]. @@ -41,7 +48,7 @@ impl<'a> Stroke<'a> { impl<'a> Default for Stroke<'a> { fn default() -> Self { Stroke { - color: Color::BLACK, + style: StrokeStyle::Solid(Color::BLACK), width: 1.0, line_cap: LineCap::default(), line_join: LineJoin::default(), @@ -50,6 +57,15 @@ impl<'a> Default for Stroke<'a> { } } +/// The color or gradient of a [`Stroke`]. +#[derive(Debug, Clone, Copy)] +pub enum StrokeStyle<'a> { + /// A solid color + Solid(Color), + /// A color gradient + Gradient(&'a Gradient), +} + /// The shape used at the end of open subpaths when they are stroked. #[derive(Debug, Clone, Copy)] pub enum LineCap { diff --git a/wgpu/Cargo.toml b/wgpu/Cargo.toml index 586f97d3..7174f80c 100644 --- a/wgpu/Cargo.toml +++ b/wgpu/Cargo.toml @@ -69,6 +69,13 @@ optional = true version = "0.6" optional = true +[dependencies.encase] +version = "0.3.0" +features = ["glam"] + +[dependencies.glam] +version = "0.21.3" + [package.metadata.docs.rs] rustdoc-args = ["--cfg", "docsrs"] all-features = true diff --git a/wgpu/src/backend.rs b/wgpu/src/backend.rs index 8c875254..fd688004 100644 --- a/wgpu/src/backend.rs +++ b/wgpu/src/backend.rs @@ -94,8 +94,7 @@ impl Backend { staging_belt, encoder, frame, - target_size.width, - target_size.height, + target_size ); } @@ -112,8 +111,7 @@ impl Backend { staging_belt: &mut wgpu::util::StagingBelt, encoder: &mut wgpu::CommandEncoder, target: &wgpu::TextureView, - target_width: u32, - target_height: u32, + target_size: Size, ) { let bounds = (layer.bounds * scale_factor).snap(); @@ -134,7 +132,7 @@ impl Backend { ); } - if !layer.meshes.is_empty() { + if !layer.meshes.0.is_empty() { let scaled = transformation * Transformation::scale(scale_factor, scale_factor); @@ -143,8 +141,7 @@ impl Backend { staging_belt, encoder, target, - target_width, - target_height, + target_size, scaled, scale_factor, &layer.meshes, diff --git a/wgpu/src/buffers.rs b/wgpu/src/buffers.rs new file mode 100644 index 00000000..f94d175d --- /dev/null +++ b/wgpu/src/buffers.rs @@ -0,0 +1,3 @@ +//! Utilities for buffer operations. +pub mod buffer; +pub mod dynamic_buffers; \ No newline at end of file diff --git a/wgpu/src/buffers/buffer.rs b/wgpu/src/buffers/buffer.rs new file mode 100644 index 00000000..dae3b038 --- /dev/null +++ b/wgpu/src/buffers/buffer.rs @@ -0,0 +1,91 @@ +//! Utilities for static buffer operations. + +/// A generic buffer struct useful for items which have no alignment requirements +/// (e.g. Vertex, Index buffers) and are set once and never changed until destroyed. +/// +/// This buffer is mapped to the GPU on creation, so must be initialized with the correct capacity. +#[derive(Debug)] +pub(crate) struct StaticBuffer { + //stored sequentially per mesh iteration + offsets: Vec, + gpu: wgpu::Buffer, + //the static size of the buffer + size: wgpu::BufferAddress, +} + +impl StaticBuffer { + pub fn new( + device: &wgpu::Device, + label: &'static str, + size: u64, + usage: wgpu::BufferUsages, + total_offsets: usize, + ) -> Self { + Self { + offsets: Vec::with_capacity(total_offsets), + gpu: device.create_buffer(&wgpu::BufferDescriptor { + label: Some(label), + size, + usage, + mapped_at_creation: true, + }), + size, + } + } + + /// Resolves pending write operations & unmaps buffer from host memory. + pub fn flush(&self) { + (&self.gpu).unmap(); + } + + /// Returns whether or not the buffer needs to be recreated. This can happen whenever the mesh + /// data is re-submitted. + pub fn needs_recreate(&self, new_size: usize) -> bool { + self.size != new_size as u64 + } + + /// Writes the current vertex data to the gpu buffer with a memcpy & stores its offset. + pub fn write(&mut self, offset: u64, content: &[u8]) { + //offset has to be divisible by 8 for alignment reasons + let actual_offset = if offset % 8 != 0 { + offset + 4 + } else { + offset + }; + + let mut buffer = self + .gpu + .slice(actual_offset..(actual_offset + content.len() as u64)) + .get_mapped_range_mut(); + buffer.copy_from_slice(content); + self.offsets.push(actual_offset); + } + + fn offset_at(&self, index: usize) -> &wgpu::BufferAddress { + self.offsets + .get(index) + .expect(&format!("Offset index {} is not in range.", index)) + } + + /// Returns the slice calculated from the offset stored at the given index. + /// e.g. to calculate the slice for the 2nd mesh in the layer, this would be the offset at index + /// 1 that we stored earlier when writing. + pub fn slice_from_index( + &self, + index: usize, + ) -> wgpu::BufferSlice<'_> { + self.gpu.slice(self.offset_at(index)..) + } +} + +/// Returns true if the current buffer doesn't exist & needs to be created, or if it's too small +/// for the new content. +pub(crate) fn needs_recreate( + buffer: &Option, + new_size: usize, +) -> bool { + match buffer { + None => true, + Some(buf) => buf.needs_recreate(new_size), + } +} diff --git a/wgpu/src/buffers/dynamic_buffers.rs b/wgpu/src/buffers/dynamic_buffers.rs new file mode 100644 index 00000000..d81529ce --- /dev/null +++ b/wgpu/src/buffers/dynamic_buffers.rs @@ -0,0 +1,202 @@ +//! Utilities for uniform buffer operations. +use encase::private::WriteInto; +use encase::ShaderType; +use std::marker::PhantomData; + +// Currently supported dynamic buffers. +enum DynamicBufferType { + Uniform(encase::DynamicUniformBuffer>), + Storage(encase::DynamicStorageBuffer>), +} + +impl DynamicBufferType { + /// Writes the current value to its CPU buffer with proper alignment. + pub(super) fn write( + &mut self, + value: &T, + ) -> wgpu::DynamicOffset { + match self { + DynamicBufferType::Uniform(buf) => buf + .write(value) + .expect("Error when writing to dynamic uniform buffer.") + as u32, + DynamicBufferType::Storage(buf) => buf + .write(value) + .expect("Error when writing to dynamic storage buffer.") + as u32, + } + } + + /// Returns bytearray of aligned CPU buffer. + pub(super) fn get_ref(&self) -> &Vec { + match self { + DynamicBufferType::Uniform(buf) => buf.as_ref(), + DynamicBufferType::Storage(buf) => buf.as_ref(), + } + } + + /// Resets the CPU buffer. + pub(super) fn clear(&mut self) { + match self { + DynamicBufferType::Uniform(buf) => { + buf.as_mut().clear(); + buf.set_offset(0); + } + DynamicBufferType::Storage(buf) => { + buf.as_mut().clear(); + buf.set_offset(0); + } + } + } +} + +//TODO think about making cpu & gpu buffers optional +pub(crate) struct DynamicBuffer { + offsets: Vec, + cpu: DynamicBufferType, + gpu: wgpu::Buffer, + label: &'static str, + size: u64, + _data: PhantomData, +} + +impl DynamicBuffer { + /// Creates a new dynamic uniform buffer. + pub fn uniform(device: &wgpu::Device, label: &'static str) -> Self { + DynamicBuffer::new( + device, + DynamicBufferType::Uniform(encase::DynamicUniformBuffer::new( + Vec::new(), + )), + label, + wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST, + ) + } + + /// Creates a new dynamic storage buffer. + pub fn storage(device: &wgpu::Device, label: &'static str) -> Self { + DynamicBuffer::new( + device, + DynamicBufferType::Storage(encase::DynamicStorageBuffer::new( + Vec::new(), + )), + label, + wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_DST, + ) + } + + fn new( + device: &wgpu::Device, + dynamic_buffer_type: DynamicBufferType, + label: &'static str, + usage: wgpu::BufferUsages, + ) -> Self { + let initial_size = u64::from(T::min_size()); + + Self { + offsets: Vec::new(), + cpu: dynamic_buffer_type, + gpu: DynamicBuffer::::create_gpu_buffer( + device, + label, + usage, + initial_size, + ), + label, + size: initial_size, + _data: Default::default(), + } + } + + fn create_gpu_buffer( + device: &wgpu::Device, + label: &'static str, + usage: wgpu::BufferUsages, + size: u64, + ) -> wgpu::Buffer { + device.create_buffer(&wgpu::BufferDescriptor { + label: Some(label), + size, + usage, + mapped_at_creation: false, + }) + } + + /// Write a new value to the CPU buffer with proper alignment. Stores the returned offset value + /// in the buffer for future use. + pub fn push(&mut self, value: &T) { + //this write operation on the buffer will adjust for uniform alignment requirements + let offset = self.cpu.write(value); + self.offsets.push(offset as u32); + } + + /// Resize buffer contents if necessary. This will re-create the GPU buffer if current size is + /// less than the newly computed size from the CPU buffer. + pub fn resize(&mut self, device: &wgpu::Device) -> bool { + let new_size = self.cpu.get_ref().len() as u64; + + if self.size < new_size { + let usages = match self.cpu { + DynamicBufferType::Uniform(_) => { + wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST + } + DynamicBufferType::Storage(_) => { + wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_DST + } + }; + + //Re-create the GPU buffer since it needs to be resized. + self.gpu = DynamicBuffer::::create_gpu_buffer( + device, self.label, usages, new_size, + ); + self.size = new_size; + true + } else { + false + } + } + + /// Write the contents of this dynamic buffer to the GPU via staging belt command. + pub fn write( + &mut self, + device: &wgpu::Device, + staging_belt: &mut wgpu::util::StagingBelt, + encoder: &mut wgpu::CommandEncoder, + ) { + let size = self.cpu.get_ref().len(); + + if let Some(buffer_size) = wgpu::BufferSize::new(size as u64) { + let mut buffer = staging_belt.write_buffer( + encoder, + &self.gpu, + 0, + buffer_size, + device, + ); + + buffer.copy_from_slice(self.cpu.get_ref()); + } + } + + // Gets the aligned offset at the given index from the CPU buffer. + pub fn offset_at_index(&self, index: usize) -> wgpu::DynamicOffset { + let offset = self + .offsets + .get(index) + .expect(&format!("Index {} not found in offsets.", index)) + .clone(); + + offset + } + + /// Returns a reference to the GPU buffer. + pub fn raw(&self) -> &wgpu::Buffer { + &self.gpu + } + + /// Reset the buffer. + pub fn clear(&mut self) { + self.offsets.clear(); + self.cpu.clear(); + } +} diff --git a/wgpu/src/lib.rs b/wgpu/src/lib.rs index 3a98c6bd..42cf712e 100644 --- a/wgpu/src/lib.rs +++ b/wgpu/src/lib.rs @@ -41,6 +41,7 @@ pub mod settings; pub mod triangle; pub mod window; +pub mod buffers; mod backend; mod quad; diff --git a/wgpu/src/shader/triangle_gradient.wgsl b/wgpu/src/shader/triangle_gradient.wgsl new file mode 100644 index 00000000..cb35b61c --- /dev/null +++ b/wgpu/src/shader/triangle_gradient.wgsl @@ -0,0 +1,83 @@ +// uniforms +struct GradientUniforms { + transform: mat4x4, + @size(16) start: vec2, + @size(16) end: vec2, + @size(16) start_stop: i32, + @size(16) end_stop: i32, +} + +struct Stop { + color: vec4, + offset: f32, +}; + +@group(0) @binding(0) +var gradient_uniforms: GradientUniforms; + +@group(0) @binding(1) +var color_stops: array; + +struct VertexOutput { + @builtin(position) position: vec4, + @location(0) raw_position: vec2 +} + +@vertex +fn vs_main(@location(0) input: vec2) -> VertexOutput { + var output: VertexOutput; + output.position = gradient_uniforms.transform * vec4(input.xy, 0.0, 1.0); + output.raw_position = input; + + return output; +} + +@fragment +fn fs_gradient(input: VertexOutput) -> @location(0) vec4 { + let v1 = gradient_uniforms.end - gradient_uniforms.start; + let v2 = input.raw_position.xy - gradient_uniforms.start; + let unit = normalize(v1); + let offset = dot(unit, v2) / length(v1); + + let min_stop = color_stops[gradient_uniforms.start_stop]; + let max_stop = color_stops[gradient_uniforms.end_stop]; + + var color: vec4; + + if (offset <= min_stop.offset) { + color = min_stop.color; + } else if (offset >= max_stop.offset) { + color = max_stop.color; + } else { + var min = min_stop; + var max = max_stop; + var min_index = gradient_uniforms.start_stop; + var max_index = gradient_uniforms.end_stop; + + loop { + if (min_index >= max_index - 1) { + break; + } + + let index = min_index + (max_index - min_index) / 2; + + let stop = color_stops[index]; + + if (offset <= stop.offset) { + max = stop; + max_index = index; + } else { + min = stop; + min_index = index; + } + } + + color = mix(min.color, max.color, smoothstep( + min.offset, + max.offset, + offset + )); + } + + return color; +} \ No newline at end of file diff --git a/wgpu/src/shader/triangle_solid.wgsl b/wgpu/src/shader/triangle_solid.wgsl new file mode 100644 index 00000000..126eceaa --- /dev/null +++ b/wgpu/src/shader/triangle_solid.wgsl @@ -0,0 +1,18 @@ +// uniforms +struct SolidUniforms { + transform: mat4x4, + color: vec4 +} + +@group(0) @binding(0) +var solid_uniforms: SolidUniforms; + +@vertex +fn vs_main(@location(0) input: vec2) -> @builtin(position) vec4 { + return solid_uniforms.transform * vec4(input.xy, 0.0, 1.0); +} + +@fragment +fn fs_solid() -> @location(0) vec4 { + return solid_uniforms.color; +} \ No newline at end of file diff --git a/wgpu/src/triangle.rs b/wgpu/src/triangle.rs index fd06dddf..d632c26c 100644 --- a/wgpu/src/triangle.rs +++ b/wgpu/src/triangle.rs @@ -1,429 +1,308 @@ //! Draw meshes of triangles. use crate::{settings, Transformation}; -use iced_graphics::layer; +use core::fmt; +use std::fmt::Formatter; -use bytemuck::{Pod, Zeroable}; -use std::mem; +use iced_graphics::layer::Meshes; +use iced_graphics::shader::Shader; +use iced_graphics::Size; +use crate::buffers::buffer::{needs_recreate, StaticBuffer}; +use crate::triangle::gradient::GradientPipeline; +use crate::triangle::solid::SolidPipeline; pub use iced_graphics::triangle::{Mesh2D, Vertex2D}; +mod gradient; mod msaa; +mod solid; -const UNIFORM_BUFFER_SIZE: usize = 50; -const VERTEX_BUFFER_SIZE: usize = 10_000; -const INDEX_BUFFER_SIZE: usize = 10_000; - +/// Triangle pipeline for all mesh layers in a [`iced_graphics::Canvas`] widget. #[derive(Debug)] pub(crate) struct Pipeline { - pipeline: wgpu::RenderPipeline, blit: Option, - constants_layout: wgpu::BindGroupLayout, - constants: wgpu::BindGroup, - uniforms_buffer: Buffer, - vertex_buffer: Buffer, - index_buffer: Buffer, + // these are optional so we don't allocate any memory to the GPU if + // application has no triangle meshes. + vertex_buffer: Option, + index_buffer: Option, + pipelines: TrianglePipelines, } -#[derive(Debug)] -struct Buffer { - label: &'static str, - raw: wgpu::Buffer, - size: usize, - usage: wgpu::BufferUsages, - _type: std::marker::PhantomData, +/// Supported triangle pipelines for different fills. Both use the same vertex shader. +pub(crate) struct TrianglePipelines { + solid: SolidPipeline, + gradient: GradientPipeline, } -impl Buffer { - pub fn new( - label: &'static str, - device: &wgpu::Device, - size: usize, - usage: wgpu::BufferUsages, - ) -> Self { - let raw = device.create_buffer(&wgpu::BufferDescriptor { - label: Some(label), - size: (std::mem::size_of::() * size) as u64, - usage, - mapped_at_creation: false, - }); - - Buffer { - label, - raw, - size, - usage, - _type: std::marker::PhantomData, - } +impl fmt::Debug for TrianglePipelines { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + f.debug_struct("TrianglePipelines").finish() } +} - pub fn expand(&mut self, device: &wgpu::Device, size: usize) -> bool { - let needs_resize = self.size < size; - - if needs_resize { - self.raw = device.create_buffer(&wgpu::BufferDescriptor { - label: Some(self.label), - size: (std::mem::size_of::() * size) as u64, - usage: self.usage, - mapped_at_creation: false, - }); - - self.size = size; - } +impl TrianglePipelines { + /// Resets each pipeline's buffers. + fn clear(&mut self) { + self.solid.buffer.clear(); + self.gradient.uniform_buffer.clear(); + self.gradient.storage_buffer.clear(); + } - needs_resize + /// Writes the contents of each pipeline's CPU buffer to the GPU, resizing the GPU buffer + /// beforehand if necessary. + fn write( + &mut self, + device: &wgpu::Device, + staging_belt: &mut wgpu::util::StagingBelt, + encoder: &mut wgpu::CommandEncoder, + ) { + self.solid.write(device, staging_belt, encoder); + self.gradient.write(device, staging_belt, encoder); } } impl Pipeline { + /// Creates supported GL programs, listed in [TrianglePipelines]. pub fn new( device: &wgpu::Device, format: wgpu::TextureFormat, antialiasing: Option, ) -> Pipeline { - let constants_layout = - device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { - label: Some("iced_wgpu::triangle uniforms layout"), - entries: &[wgpu::BindGroupLayoutEntry { - binding: 0, - visibility: wgpu::ShaderStages::VERTEX, - ty: wgpu::BindingType::Buffer { - ty: wgpu::BufferBindingType::Uniform, - has_dynamic_offset: true, - min_binding_size: wgpu::BufferSize::new( - mem::size_of::() as u64, - ), - }, - count: None, - }], - }); - - let constants_buffer = Buffer::new( - "iced_wgpu::triangle uniforms buffer", - device, - UNIFORM_BUFFER_SIZE, - wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST, - ); - - let constant_bind_group = - device.create_bind_group(&wgpu::BindGroupDescriptor { - label: Some("iced_wgpu::triangle uniforms bind group"), - layout: &constants_layout, - entries: &[wgpu::BindGroupEntry { - binding: 0, - resource: wgpu::BindingResource::Buffer( - wgpu::BufferBinding { - buffer: &constants_buffer.raw, - offset: 0, - size: wgpu::BufferSize::new(std::mem::size_of::< - Uniforms, - >( - ) - as u64), - }, - ), - }], - }); - - let layout = - device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { - label: Some("iced_wgpu::triangle pipeline layout"), - push_constant_ranges: &[], - bind_group_layouts: &[&constants_layout], - }); - - let shader = - device.create_shader_module(wgpu::ShaderModuleDescriptor { - label: Some("iced_wgpu::triangle::shader"), - source: wgpu::ShaderSource::Wgsl(std::borrow::Cow::Borrowed( - include_str!("shader/triangle.wgsl"), - )), - }); - - let pipeline = - device.create_render_pipeline(&wgpu::RenderPipelineDescriptor { - label: Some("iced_wgpu::triangle pipeline"), - layout: Some(&layout), - vertex: wgpu::VertexState { - module: &shader, - entry_point: "vs_main", - buffers: &[wgpu::VertexBufferLayout { - array_stride: mem::size_of::() as u64, - step_mode: wgpu::VertexStepMode::Vertex, - attributes: &wgpu::vertex_attr_array!( - // Position - 0 => Float32x2, - // Color - 1 => Float32x4, - ), - }], - }, - fragment: Some(wgpu::FragmentState { - module: &shader, - entry_point: "fs_main", - targets: &[Some(wgpu::ColorTargetState { - format, - blend: Some(wgpu::BlendState::ALPHA_BLENDING), - write_mask: wgpu::ColorWrites::ALL, - })], - }), - primitive: wgpu::PrimitiveState { - topology: wgpu::PrimitiveTopology::TriangleList, - front_face: wgpu::FrontFace::Cw, - ..Default::default() - }, - depth_stencil: None, - multisample: wgpu::MultisampleState { - count: antialiasing.map(|a| a.sample_count()).unwrap_or(1), - mask: !0, - alpha_to_coverage_enabled: false, - }, - multiview: None, - }); - Pipeline { - pipeline, blit: antialiasing.map(|a| msaa::Blit::new(device, format, a)), - constants_layout, - constants: constant_bind_group, - uniforms_buffer: constants_buffer, - vertex_buffer: Buffer::new( - "iced_wgpu::triangle vertex buffer", - device, - VERTEX_BUFFER_SIZE, - wgpu::BufferUsages::VERTEX | wgpu::BufferUsages::COPY_DST, - ), - index_buffer: Buffer::new( - "iced_wgpu::triangle index buffer", - device, - INDEX_BUFFER_SIZE, - wgpu::BufferUsages::INDEX | wgpu::BufferUsages::COPY_DST, - ), + vertex_buffer: None, + index_buffer: None, + pipelines: TrianglePipelines { + solid: SolidPipeline::new(device, format, antialiasing), + gradient: GradientPipeline::new(device, format, antialiasing), + }, } } + /// Draws the contents of the current layer's meshes to the [target]. pub fn draw( &mut self, device: &wgpu::Device, staging_belt: &mut wgpu::util::StagingBelt, encoder: &mut wgpu::CommandEncoder, target: &wgpu::TextureView, - target_width: u32, - target_height: u32, + target_size: Size, transformation: Transformation, scale_factor: f32, - meshes: &[layer::Mesh<'_>], + meshes: &Meshes<'_>, ) { - // This looks a bit crazy, but we are just counting how many vertices - // and indices we will need to handle. - // TODO: Improve readability - let (total_vertices, total_indices) = meshes - .iter() - .map(|layer::Mesh { buffers, .. }| { - (buffers.vertices.len(), buffers.indices.len()) - }) - .fold((0, 0), |(total_v, total_i), (v, i)| { - (total_v + v, total_i + i) - }); - - // Then we ensure the current buffers are big enough, resizing if - // necessary - let _ = self.vertex_buffer.expand(device, total_vertices); - let _ = self.index_buffer.expand(device, total_indices); - - // If the uniforms buffer is resized, then we need to recreate its - // bind group. - if self.uniforms_buffer.expand(device, meshes.len()) { - self.constants = - device.create_bind_group(&wgpu::BindGroupDescriptor { - label: Some("iced_wgpu::triangle uniforms buffer"), - layout: &self.constants_layout, - entries: &[wgpu::BindGroupEntry { - binding: 0, - resource: wgpu::BindingResource::Buffer( - wgpu::BufferBinding { - buffer: &self.uniforms_buffer.raw, - offset: 0, - size: wgpu::BufferSize::new( - std::mem::size_of::() as u64, - ), - }, - ), - }], - }); + //count the total number of vertices & indices we need to handle + let (total_vertices, total_indices) = meshes.attribute_count(); + println!("total vertices: {}, total indices: {}", total_vertices, total_indices); + + //Only create buffers if they need to be re-sized or don't exist + if needs_recreate(&self.vertex_buffer, total_vertices) { + //mapped to GPU at creation with total vertices + self.vertex_buffer = Some(StaticBuffer::new( + device, + "iced_wgpu::triangle vertex buffer", + //TODO: a more reasonable default to prevent frequent resizing calls + // before this was 10_000 + (std::mem::size_of::() * total_vertices) as u64, + wgpu::BufferUsages::VERTEX, + meshes.0.len(), + )) } - let mut uniforms: Vec = Vec::with_capacity(meshes.len()); - let mut offsets: Vec<( - wgpu::BufferAddress, - wgpu::BufferAddress, - usize, - )> = Vec::with_capacity(meshes.len()); - let mut last_vertex = 0; - let mut last_index = 0; - - // We upload everything upfront - for mesh in meshes { - let transform = (transformation - * Transformation::translate(mesh.origin.x, mesh.origin.y)) - .into(); - - let vertices = bytemuck::cast_slice(&mesh.buffers.vertices); - let indices = bytemuck::cast_slice(&mesh.buffers.indices); - - if let (Some(vertices_size), Some(indices_size)) = ( - wgpu::BufferSize::new(vertices.len() as u64), - wgpu::BufferSize::new(indices.len() as u64), - ) { - { - let mut vertex_buffer = staging_belt.write_buffer( - encoder, - &self.vertex_buffer.raw, - (std::mem::size_of::() * last_vertex) as u64, - vertices_size, - device, - ); + if needs_recreate(&self.index_buffer, total_indices) { + //mapped to GPU at creation with total indices + self.index_buffer = Some(StaticBuffer::new( + device, + "iced_wgpu::triangle index buffer", + //TODO: a more reasonable default to prevent frequent resizing calls + // before this was 10_000 + (std::mem::size_of::() * total_indices) as u64, + wgpu::BufferUsages::INDEX, + meshes.0.len(), + )); + } - vertex_buffer.copy_from_slice(vertices); + if let Some(vertex_buffer) = &mut self.vertex_buffer { + if let Some(index_buffer) = &mut self.index_buffer { + let mut offset_v = 0; + let mut offset_i = 0; + //TODO: store this more efficiently + let mut indices_lengths = Vec::with_capacity(meshes.0.len()); + + //iterate through meshes to write all attribute data + for mesh in meshes.0.iter() { + let transform = transformation + * Transformation::translate( + mesh.origin.x, + mesh.origin.y, + ); + + println!("Mesh attribute data: Vertex: {:?}, Index: {:?}", mesh.buffers.vertices, mesh.buffers.indices); + + let vertices = bytemuck::cast_slice(&mesh.buffers.vertices); + let indices = bytemuck::cast_slice(&mesh.buffers.indices); + + //TODO: it's (probably) more efficient to reduce this write command and + // iterate first and then upload + println!("vertex buffer len: {}, index length: {}", vertices.len(), indices.len()); + vertex_buffer.write(offset_v, vertices); + index_buffer.write(offset_i, indices); + + offset_v += vertices.len() as u64; + offset_i += indices.len() as u64; + indices_lengths.push(mesh.buffers.indices.len()); + + match mesh.shader { + Shader::Solid(color) => { + self.pipelines.solid.push(transform, color); + } + Shader::Gradient(gradient) => { + self.pipelines.gradient.push(transform, gradient); + } + } } + //done writing to gpu buffer, unmap from host memory since we don't need it + //anymore + vertex_buffer.flush(); + index_buffer.flush(); + + //resize & memcpy uniforms from CPU buffers to GPU buffers for all pipelines + self.pipelines.write(device, staging_belt, encoder); + + //configure the render pass now that the data is uploaded to the GPU { - let mut index_buffer = staging_belt.write_buffer( - encoder, - &self.index_buffer.raw, - (std::mem::size_of::() * last_index) as u64, - indices_size, - device, + //configure antialiasing pass + let (attachment, resolve_target, load) = + if let Some(blit) = &mut self.blit { + let (attachment, resolve_target) = blit.targets( + device, + target_size.width, + target_size.height, + ); + + ( + attachment, + Some(resolve_target), + wgpu::LoadOp::Clear(wgpu::Color::TRANSPARENT), + ) + } else { + (target, None, wgpu::LoadOp::Load) + }; + + let mut render_pass = encoder.begin_render_pass( + &wgpu::RenderPassDescriptor { + label: Some("iced_wgpu::triangle render pass"), + color_attachments: &[Some( + wgpu::RenderPassColorAttachment { + view: attachment, + resolve_target, + ops: wgpu::Operations { load, store: true }, + }, + )], + depth_stencil_attachment: None, + }, ); - index_buffer.copy_from_slice(indices); + //TODO: do this a better way; store it in the respective pipelines perhaps + // to be more readable + let mut num_solids = 0; + let mut num_gradients = 0; + + //TODO: try to avoid this extra iteration if possible + for index in 0..meshes.0.len() { + let clip_bounds = + (meshes.0[index].clip_bounds * scale_factor).snap(); + + render_pass.set_scissor_rect( + clip_bounds.x, + clip_bounds.y, + clip_bounds.width, + clip_bounds.height, + ); + + match meshes.0[index].shader { + Shader::Solid(_) => { + self.pipelines.solid.configure_render_pass( + &mut render_pass, + num_solids, + ); + num_solids += 1; + } + Shader::Gradient(_) => { + self.pipelines.gradient.configure_render_pass( + &mut render_pass, + num_gradients, + ); + num_gradients += 1; + } + } + + render_pass.set_index_buffer( + index_buffer.slice_from_index::(index), + wgpu::IndexFormat::Uint32, + ); + + render_pass.set_vertex_buffer( + 0, + vertex_buffer.slice_from_index::(index), + ); + + render_pass.draw_indexed( + 0..(indices_lengths[index] as u32), + 0, + 0..1, + ); + } } - - uniforms.push(transform); - offsets.push(( - last_vertex as u64, - last_index as u64, - mesh.buffers.indices.len(), - )); - - last_vertex += mesh.buffers.vertices.len(); - last_index += mesh.buffers.indices.len(); - } - } - - let uniforms = bytemuck::cast_slice(&uniforms); - - if let Some(uniforms_size) = - wgpu::BufferSize::new(uniforms.len() as u64) - { - let mut uniforms_buffer = staging_belt.write_buffer( - encoder, - &self.uniforms_buffer.raw, - 0, - uniforms_size, - device, - ); - - uniforms_buffer.copy_from_slice(uniforms); - } - - { - let (attachment, resolve_target, load) = - if let Some(blit) = &mut self.blit { - let (attachment, resolve_target) = - blit.targets(device, target_width, target_height); - - ( - attachment, - Some(resolve_target), - wgpu::LoadOp::Clear(wgpu::Color::TRANSPARENT), - ) - } else { - (target, None, wgpu::LoadOp::Load) - }; - - let mut render_pass = - encoder.begin_render_pass(&wgpu::RenderPassDescriptor { - label: Some("iced_wgpu::triangle render pass"), - color_attachments: &[Some( - wgpu::RenderPassColorAttachment { - view: attachment, - resolve_target, - ops: wgpu::Operations { load, store: true }, - }, - )], - depth_stencil_attachment: None, - }); - - render_pass.set_pipeline(&self.pipeline); - - for (i, (vertex_offset, index_offset, indices)) in - offsets.into_iter().enumerate() - { - let clip_bounds = (meshes[i].clip_bounds * scale_factor).snap(); - - render_pass.set_scissor_rect( - clip_bounds.x, - clip_bounds.y, - clip_bounds.width, - clip_bounds.height, - ); - - render_pass.set_bind_group( - 0, - &self.constants, - &[(std::mem::size_of::() * i) as u32], - ); - - render_pass.set_index_buffer( - self.index_buffer - .raw - .slice(index_offset * mem::size_of::() as u64..), - wgpu::IndexFormat::Uint32, - ); - - render_pass.set_vertex_buffer( - 0, - self.vertex_buffer.raw.slice( - vertex_offset * mem::size_of::() as u64.., - ), - ); - - render_pass.draw_indexed(0..indices as u32, 0, 0..1); } } if let Some(blit) = &mut self.blit { blit.draw(encoder, target); } + + //cleanup + self.pipelines.clear(); } } -#[repr(C)] -#[derive(Debug, Clone, Copy, Zeroable, Pod)] -struct Uniforms { - transform: [f32; 16], - // We need to align this to 256 bytes to please `wgpu`... - // TODO: Be smarter and stop wasting memory! - _padding_a: [f32; 32], - _padding_b: [f32; 16], +//utility functions for individual pipelines with shared functionality +fn vertex_buffer_layout<'a>() -> wgpu::VertexBufferLayout<'a> { + wgpu::VertexBufferLayout { + array_stride: std::mem::size_of::() as u64, + step_mode: wgpu::VertexStepMode::Vertex, + attributes: &[wgpu::VertexAttribute { + format: wgpu::VertexFormat::Float32x2, + offset: 0, + shader_location: 0, + }], + } } -impl Default for Uniforms { - fn default() -> Self { - Self { - transform: *Transformation::identity().as_ref(), - _padding_a: [0.0; 32], - _padding_b: [0.0; 16], - } +fn default_fragment_target( + texture_format: wgpu::TextureFormat, +) -> Option { + Some(wgpu::ColorTargetState { + format: texture_format, + blend: Some(wgpu::BlendState::ALPHA_BLENDING), + write_mask: wgpu::ColorWrites::ALL, + }) +} + +fn default_triangle_primitive_state() -> wgpu::PrimitiveState { + wgpu::PrimitiveState { + topology: wgpu::PrimitiveTopology::TriangleList, + front_face: wgpu::FrontFace::Cw, + ..Default::default() } } -impl From for Uniforms { - fn from(transformation: Transformation) -> Uniforms { - Self { - transform: transformation.into(), - _padding_a: [0.0; 32], - _padding_b: [0.0; 16], - } +fn default_multisample_state( + antialiasing: Option, +) -> wgpu::MultisampleState { + wgpu::MultisampleState { + count: antialiasing.map(|a| a.sample_count()).unwrap_or(1), + mask: !0, + alpha_to_coverage_enabled: false, } } diff --git a/wgpu/src/triangle/gradient.rs b/wgpu/src/triangle/gradient.rs new file mode 100644 index 00000000..471b204c --- /dev/null +++ b/wgpu/src/triangle/gradient.rs @@ -0,0 +1,265 @@ +use crate::buffers::dynamic_buffers::DynamicBuffer; +use crate::settings; +use crate::triangle::{ + default_fragment_target, default_multisample_state, + default_triangle_primitive_state, vertex_buffer_layout, +}; +use encase::ShaderType; +use glam::{Vec2, Vec4}; +use iced_graphics::gradient::Gradient; +use iced_graphics::Transformation; + +pub(super) struct GradientPipeline { + pipeline: wgpu::RenderPipeline, + pub(super) uniform_buffer: DynamicBuffer, + pub(super) storage_buffer: DynamicBuffer, + color_stop_offset: i32, + //Need to store these and then write them all at once + //or else they will be padded to 256 and cause gaps in the storage buffer + color_stops_pending_write: GradientStorage, + bind_group_layout: wgpu::BindGroupLayout, + bind_group: wgpu::BindGroup, +} + +//TODO I can tightly pack this by rearranging/consolidating some fields +#[derive(Debug, ShaderType)] +pub(super) struct GradientUniforms { + transform: glam::Mat4, + start: Vec2, + #[align(16)] + end: Vec2, + #[align(16)] + start_stop: i32, + #[align(16)] + end_stop: i32, +} + +#[derive(Debug, ShaderType)] +pub(super) struct ColorStop { + color: Vec4, + offset: f32, +} + +#[derive(ShaderType)] +pub(super) struct GradientStorage { + #[size(runtime)] + pub color_stops: Vec, +} + +impl GradientPipeline { + /// Creates a new [GradientPipeline] using `triangle_gradient.wgsl` shader. + pub(super) fn new( + device: &wgpu::Device, + format: wgpu::TextureFormat, + antialiasing: Option, + ) -> Self { + let uniform_buffer = DynamicBuffer::uniform( + device, + "iced_wgpu::triangle [GRADIENT] uniforms", + ); + + //TODO: With a WASM target storage buffers are not supported. Will need to use UBOs & static + // sized array (64 on OpenGL side right now) to make gradients work + let storage_buffer = DynamicBuffer::storage( + device, + "iced_wgpu::triangle [GRADIENT] storage", + ); + + let bind_group_layout = + device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { + label: Some("iced_wgpu::triangle [GRADIENT] bind group layout"), + entries: &[ + wgpu::BindGroupLayoutEntry { + binding: 0, + visibility: wgpu::ShaderStages::VERTEX_FRAGMENT, + ty: wgpu::BindingType::Buffer { + ty: wgpu::BufferBindingType::Uniform, + has_dynamic_offset: true, + min_binding_size: Some(GradientUniforms::min_size()), + }, + count: None, + }, + wgpu::BindGroupLayoutEntry { + binding: 1, + visibility: wgpu::ShaderStages::FRAGMENT, + ty: wgpu::BindingType::Buffer { + ty: wgpu::BufferBindingType::Storage { + read_only: true, + }, + has_dynamic_offset: false, + min_binding_size: Some(GradientStorage::min_size()), + }, + count: None, + }, + ], + }); + + let bind_group = GradientPipeline::bind_group( + device, + uniform_buffer.raw(), + storage_buffer.raw(), + &bind_group_layout, + ); + + let layout = + device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { + label: Some("iced_wgpu::triangle [GRADIENT] pipeline layout"), + bind_group_layouts: &[&bind_group_layout], + push_constant_ranges: &[], + }); + + let shader = + device.create_shader_module(wgpu::ShaderModuleDescriptor { + label: Some( + "iced_wgpu::triangle [GRADIENT] create shader module", + ), + source: wgpu::ShaderSource::Wgsl(std::borrow::Cow::Borrowed( + include_str!("../shader/triangle_gradient.wgsl"), + )), + }); + + let pipeline = + device.create_render_pipeline(&wgpu::RenderPipelineDescriptor { + label: Some("iced_wgpu::triangle [GRADIENT] pipeline"), + layout: Some(&layout), + vertex: wgpu::VertexState { + module: &shader, + entry_point: "vs_main", + buffers: &[vertex_buffer_layout()], + }, + fragment: Some(wgpu::FragmentState { + module: &shader, + entry_point: "fs_gradient", + targets: &[default_fragment_target(format)], + }), + primitive: default_triangle_primitive_state(), + depth_stencil: None, + multisample: default_multisample_state(antialiasing), + multiview: None, + }); + + Self { + pipeline, + uniform_buffer, + storage_buffer, + color_stop_offset: 0, + color_stops_pending_write: GradientStorage { color_stops: vec![] }, + bind_group_layout, + bind_group, + } + } + + /// Pushes a new gradient uniform to the CPU buffer. + pub fn push(&mut self, transform: Transformation, gradient: &Gradient) { + match gradient { + Gradient::Linear(linear) => { + let start_offset = self.color_stop_offset; + let end_offset = + (linear.color_stops.len() as i32) + start_offset - 1; + + self.uniform_buffer.push(&GradientUniforms { + transform: transform.into(), + start: Vec2::new(linear.start.x, linear.start.y), + end: Vec2::new(linear.end.x, linear.end.y), + start_stop: start_offset, + end_stop: end_offset, + }); + + self.color_stop_offset = end_offset + 1; + + let stops: Vec = linear + .color_stops + .iter() + .map(|stop| ColorStop { + offset: stop.offset, + color: Vec4::new( + stop.color.r, + stop.color.g, + stop.color.b, + stop.color.a, + ), + }) + .collect(); + + self.color_stops_pending_write.color_stops.extend(stops); + } + } + } + + fn bind_group( + device: &wgpu::Device, + uniform_buffer: &wgpu::Buffer, + storage_buffer: &wgpu::Buffer, + layout: &wgpu::BindGroupLayout, + ) -> wgpu::BindGroup { + device.create_bind_group(&wgpu::BindGroupDescriptor { + label: Some("iced_wgpu::triangle [GRADIENT] bind group"), + layout, + entries: &[ + wgpu::BindGroupEntry { + binding: 0, + resource: wgpu::BindingResource::Buffer( + wgpu::BufferBinding { + buffer: uniform_buffer, + offset: 0, + size: Some(GradientUniforms::min_size()) + } + ) + }, + wgpu::BindGroupEntry { + binding: 1, + resource: storage_buffer.as_entire_binding() + }, + ], + }) + } + + /// Writes the contents of the gradient CPU buffer to the GPU buffer, resizing the GPU buffer + /// beforehand if necessary. + pub fn write( + &mut self, + device: &wgpu::Device, + staging_belt: &mut wgpu::util::StagingBelt, + encoder: &mut wgpu::CommandEncoder, + ) { + //first write the pending color stops to the CPU buffer + self.storage_buffer.push(&self.color_stops_pending_write); + + //resize buffers if needed + let uniforms_resized = self.uniform_buffer.resize(device); + let storage_resized = self.storage_buffer.resize(device); + + if uniforms_resized || storage_resized { + //recreate bind groups if any buffers were resized + self.bind_group = GradientPipeline::bind_group( + device, + self.uniform_buffer.raw(), + self.storage_buffer.raw(), + &self.bind_group_layout, + ); + } + + //write to GPU + self.uniform_buffer.write(device, staging_belt, encoder); + self.storage_buffer.write(device, staging_belt, encoder); + + //cleanup + self.color_stop_offset = 0; + self.color_stops_pending_write.color_stops.clear(); + } + + /// Configures the current render pass to draw the gradient at its offset stored in the + /// [DynamicBuffer] at [index]. + pub fn configure_render_pass<'a>( + &'a self, + render_pass: &mut wgpu::RenderPass<'a>, + index: usize, + ) { + render_pass.set_pipeline(&self.pipeline); + render_pass.set_bind_group( + 0, + &self.bind_group, + &[self.uniform_buffer.offset_at_index(index)], + ); + } +} diff --git a/wgpu/src/triangle/solid.rs b/wgpu/src/triangle/solid.rs new file mode 100644 index 00000000..a3cbd72b --- /dev/null +++ b/wgpu/src/triangle/solid.rs @@ -0,0 +1,169 @@ +use crate::buffers::dynamic_buffers::DynamicBuffer; +use crate::triangle::{ + default_fragment_target, default_multisample_state, + default_triangle_primitive_state, vertex_buffer_layout, +}; +use crate::{settings, Color}; +use encase::ShaderType; +use glam::Vec4; +use iced_graphics::Transformation; + +pub(super) struct SolidPipeline { + pipeline: wgpu::RenderPipeline, + pub(super) buffer: DynamicBuffer, + bind_group_layout: wgpu::BindGroupLayout, + bind_group: wgpu::BindGroup, +} + +#[derive(Debug, Clone, Copy, ShaderType)] +pub(super) struct SolidUniforms { + transform: glam::Mat4, + color: Vec4, +} + +impl SolidUniforms { + pub fn new(transform: Transformation, color: Color) -> Self { + Self { + transform: transform.into(), + color: Vec4::new(color.r, color.g, color.b, color.a), + } + } +} + +impl SolidPipeline { + /// Creates a new [SolidPipeline] using `triangle_solid.wgsl` shader. + pub fn new( + device: &wgpu::Device, + format: wgpu::TextureFormat, + antialiasing: Option, + ) -> Self { + let buffer = DynamicBuffer::uniform( + device, + "iced_wgpu::triangle [SOLID] uniforms", + ); + + let bind_group_layout = + device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { + label: Some("iced_wgpu::triangle [SOLID] bind group layout"), + entries: &[wgpu::BindGroupLayoutEntry { + binding: 0, + visibility: wgpu::ShaderStages::VERTEX_FRAGMENT, + ty: wgpu::BindingType::Buffer { + ty: wgpu::BufferBindingType::Uniform, + has_dynamic_offset: true, + min_binding_size: Some(SolidUniforms::min_size()), + }, + count: None, + }], + }); + + let bind_group = SolidPipeline::bind_group( + device, + &buffer.raw(), + &bind_group_layout, + ); + + let layout = + device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { + label: Some("iced_wgpu::triangle [SOLID] pipeline layout"), + bind_group_layouts: &[&bind_group_layout], + push_constant_ranges: &[], + }); + + let shader = + device.create_shader_module(wgpu::ShaderModuleDescriptor { + label: Some("iced_wgpu::triangle [SOLID] create shader module"), + source: wgpu::ShaderSource::Wgsl(std::borrow::Cow::Borrowed( + include_str!("../shader/triangle_solid.wgsl"), + )), + }); + + let pipeline = + device.create_render_pipeline(&wgpu::RenderPipelineDescriptor { + label: Some("iced_wgpu::triangle [SOLID] pipeline"), + layout: Some(&layout), + vertex: wgpu::VertexState { + module: &shader, + entry_point: "vs_main", + buffers: &[vertex_buffer_layout()], + }, + fragment: Some(wgpu::FragmentState { + module: &shader, + entry_point: "fs_solid", + targets: &[default_fragment_target(format)], + }), + primitive: default_triangle_primitive_state(), + depth_stencil: None, + multisample: default_multisample_state(antialiasing), + multiview: None, + }); + + Self { + pipeline, + buffer, + bind_group_layout, + bind_group, + } + } + + fn bind_group( + device: &wgpu::Device, + buffer: &wgpu::Buffer, + layout: &wgpu::BindGroupLayout, + ) -> wgpu::BindGroup { + device.create_bind_group(&wgpu::BindGroupDescriptor { + label: Some("iced_wgpu::triangle [SOLID] bind group"), + layout, + entries: &[wgpu::BindGroupEntry { + binding: 0, + resource: wgpu::BindingResource::Buffer(wgpu::BufferBinding { + buffer, + offset: 0, + size: Some(SolidUniforms::min_size()), + }), + }], + }) + } + + /// Pushes a new solid uniform to the CPU buffer. + pub fn push(&mut self, transform: Transformation, color: &Color) { + self.buffer.push(&SolidUniforms::new(transform, *color)); + } + + /// Writes the contents of the solid CPU buffer to the GPU buffer, resizing the GPU buffer + /// beforehand if necessary. + pub fn write( + &mut self, + device: &wgpu::Device, + staging_belt: &mut wgpu::util::StagingBelt, + encoder: &mut wgpu::CommandEncoder, + ) { + let uniforms_resized = self.buffer.resize(device); + + if uniforms_resized { + self.bind_group = SolidPipeline::bind_group( + device, + self.buffer.raw(), + &self.bind_group_layout, + ) + } + + self.buffer.write(device, staging_belt, encoder); + } + + /// Configures the current render pass to draw the solid at its offset stored in the + /// [DynamicBuffer] at [index]. + pub fn configure_render_pass<'a>( + &'a self, + render_pass: &mut wgpu::RenderPass<'a>, + index: usize, + ) { + render_pass.set_pipeline(&self.pipeline); + + render_pass.set_bind_group( + 0, + &self.bind_group, + &[self.buffer.offset_at_index(index)], + ); + } +} \ No newline at end of file -- cgit From 734557bda5924e563a9f0b39aca37d5953fcda5c Mon Sep 17 00:00:00 2001 From: shan Date: Thu, 29 Sep 2022 14:01:57 -0700 Subject: Fixed issue where stops could be declared out of order in the builder but must be sorted before being passed to shader. --- graphics/src/widget/canvas/gradient/linear.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/graphics/src/widget/canvas/gradient/linear.rs b/graphics/src/widget/canvas/gradient/linear.rs index 37533e19..1dc7e3a8 100644 --- a/graphics/src/widget/canvas/gradient/linear.rs +++ b/graphics/src/widget/canvas/gradient/linear.rs @@ -57,17 +57,17 @@ impl Builder { return None; } + let mut stops: Vec = self.stops.clone().into_iter().map(|f| ColorStop { + offset: f.0, + color: f.1 + }).collect(); + + stops.sort_by(|a, b| a.offset.partial_cmp(&b.offset).unwrap()); + Some(Gradient::Linear(Linear { start: self.start, end: self.end, - color_stops: self - .stops - .into_iter() - .map(|f| ColorStop { - offset: f.0, - color: f.1, - }) - .collect(), + color_stops: stops })) } } -- cgit From 0f434c74d68d32ecbf2362d1edbac66976dcd8ab Mon Sep 17 00:00:00 2001 From: shan Date: Thu, 29 Sep 2022 16:11:05 -0700 Subject: Removed some leftover debugging. --- wgpu/src/triangle.rs | 4 ---- 1 file changed, 4 deletions(-) diff --git a/wgpu/src/triangle.rs b/wgpu/src/triangle.rs index d632c26c..f1770e9a 100644 --- a/wgpu/src/triangle.rs +++ b/wgpu/src/triangle.rs @@ -92,7 +92,6 @@ impl Pipeline { ) { //count the total number of vertices & indices we need to handle let (total_vertices, total_indices) = meshes.attribute_count(); - println!("total vertices: {}, total indices: {}", total_vertices, total_indices); //Only create buffers if they need to be re-sized or don't exist if needs_recreate(&self.vertex_buffer, total_vertices) { @@ -136,14 +135,11 @@ impl Pipeline { mesh.origin.y, ); - println!("Mesh attribute data: Vertex: {:?}, Index: {:?}", mesh.buffers.vertices, mesh.buffers.indices); - let vertices = bytemuck::cast_slice(&mesh.buffers.vertices); let indices = bytemuck::cast_slice(&mesh.buffers.indices); //TODO: it's (probably) more efficient to reduce this write command and // iterate first and then upload - println!("vertex buffer len: {}, index length: {}", vertices.len(), indices.len()); vertex_buffer.write(offset_v, vertices); index_buffer.write(offset_i, indices); -- cgit From e25f3d3dea2d2c1ba7d2778948443df2745084be Mon Sep 17 00:00:00 2001 From: shan Date: Thu, 29 Sep 2022 17:13:32 -0700 Subject: Fixed issue where OpenGL would not render both shaders at once under certain circumstances. --- examples/modern_art/src/main.rs | 2 +- glow/src/triangle.rs | 52 +++++++++++++++++------------------------ 2 files changed, 23 insertions(+), 31 deletions(-) diff --git a/examples/modern_art/src/main.rs b/examples/modern_art/src/main.rs index 17149cbe..7b04b581 100644 --- a/examples/modern_art/src/main.rs +++ b/examples/modern_art/src/main.rs @@ -90,7 +90,7 @@ fn generate_box(frame: &mut Frame, bounds: Size) -> bool { let gradient = |top_left: Point, bottom_right: Point| -> Gradient { let mut builder = Gradient::linear(top_left, bottom_right); - let stops = thread_rng().gen_range(1..64u32); + let stops = thread_rng().gen_range(1..10u32); let mut i = 0; while i <= stops { diff --git a/glow/src/triangle.rs b/glow/src/triangle.rs index 7d0e14c7..85d873fe 100644 --- a/glow/src/triangle.rs +++ b/glow/src/triangle.rs @@ -24,14 +24,8 @@ pub(crate) struct Pipeline { #[derive(Debug)] struct TrianglePrograms { - solid: TriangleProgram, - gradient: TriangleProgram, -} - -#[derive(Debug)] -enum TriangleProgram { - Solid(SolidProgram), - Gradient(GradientProgram), + solid: SolidProgram, + gradient: GradientProgram, } impl Pipeline { @@ -76,14 +70,8 @@ impl Pipeline { indices, current_transform: Transformation::identity(), programs: TrianglePrograms { - solid: TriangleProgram::Solid(SolidProgram::new( - gl, - shader_version, - )), - gradient: TriangleProgram::Gradient(GradientProgram::new( - gl, - shader_version, - )), + solid: SolidProgram::new(gl, shader_version), + gradient: GradientProgram::new(gl, shader_version), }, } } @@ -138,16 +126,21 @@ impl Pipeline { let mut last_vertex = 0; let mut last_index = 0; - for Mesh { + for (index, Mesh { buffers, origin, clip_bounds, shader, - } in meshes.0.iter() + }) in meshes.0.iter().enumerate() { let transform = transformation * Transformation::translate(origin.x, origin.y); + if index == 0 { + //set initial transform uniform for both programs + self.programs.set_transforms(gl, transform); + } + let clip_bounds = (*clip_bounds * scale_factor).snap(); unsafe { @@ -196,25 +189,24 @@ impl Pipeline { ) { match shader { shader::Shader::Solid(color) => { - if let TriangleProgram::Solid(solid_program) = - &mut self.programs.solid - { - unsafe { gl.use_program(Some(solid_program.program)) } - solid_program.set_uniforms(gl, color, transform); - } + unsafe { gl.use_program(Some(self.programs.solid.program)) } + self.programs.solid.set_uniforms(gl, color, transform); } shader::Shader::Gradient(gradient) => { - if let TriangleProgram::Gradient(gradient_program) = - &mut self.programs.gradient - { - unsafe { gl.use_program(Some(gradient_program.program)) } - gradient_program.set_uniforms(gl, gradient, transform); - } + unsafe { gl.use_program(Some(self.programs.gradient.program)) } + self.programs.gradient.set_uniforms(gl, gradient, transform); } } } } +impl TrianglePrograms { + pub fn set_transforms(&self, gl: &glow::Context, transform: Transformation) { + update_transform(gl, self.solid.program, Some(transform)); + update_transform(gl, self.gradient.program, Some(transform)); + } +} + /// A simple shader program. Uses [`triangle.vert`] for its vertex shader and only binds position /// attribute location. pub(super) fn simple_triangle_program( -- cgit From 5d0fffc626928177239336757507b986b081b878 Mon Sep 17 00:00:00 2001 From: shan Date: Fri, 30 Sep 2022 10:27:00 -0700 Subject: Fixed some importing issues since you can use a Shader::Gradient outside a Canvas widget, where it was previously only accessible. --- examples/modern_art/Cargo.toml | 1 + examples/modern_art/src/main.rs | 3 +- graphics/src/gradient.rs | 92 +++++++++++++++++++++++++-- graphics/src/shader.rs | 25 +------- graphics/src/triangle.rs | 10 +-- graphics/src/widget/canvas.rs | 16 ++--- graphics/src/widget/canvas/fill.rs | 13 +++- graphics/src/widget/canvas/frame.rs | 7 +- graphics/src/widget/canvas/gradient.rs | 21 ------ graphics/src/widget/canvas/gradient/linear.rs | 73 --------------------- graphics/src/widget/canvas/stroke.rs | 13 +++- 11 files changed, 126 insertions(+), 148 deletions(-) delete mode 100644 graphics/src/widget/canvas/gradient.rs delete mode 100644 graphics/src/widget/canvas/gradient/linear.rs diff --git a/examples/modern_art/Cargo.toml b/examples/modern_art/Cargo.toml index 4995e9a7..8242f7e4 100644 --- a/examples/modern_art/Cargo.toml +++ b/examples/modern_art/Cargo.toml @@ -7,4 +7,5 @@ publish = false [dependencies] iced = { path = "../..", features = ["canvas", "tokio", "debug"] } +iced_graphics = { path = "../../graphics" } rand = "0.8.5" \ No newline at end of file diff --git a/examples/modern_art/src/main.rs b/examples/modern_art/src/main.rs index 7b04b581..c7945012 100644 --- a/examples/modern_art/src/main.rs +++ b/examples/modern_art/src/main.rs @@ -1,5 +1,5 @@ use rand::{Rng, thread_rng}; -use crate::canvas::{Cursor, FillStyle, Geometry, Gradient}; +use crate::canvas::{Cursor, FillStyle, Geometry}; use iced::widget::canvas::{Cache, Fill, Frame}; use iced::widget::{canvas, Canvas}; use iced::Settings; @@ -7,6 +7,7 @@ use iced::{ executor, Application, Color, Command, Element, Length, Point, Rectangle, Renderer, Size, Theme, }; +use iced_graphics::gradient::Gradient; fn main() -> iced::Result { ModernArt::run(Settings { diff --git a/graphics/src/gradient.rs b/graphics/src/gradient.rs index 4d1eff62..fa57842b 100644 --- a/graphics/src/gradient.rs +++ b/graphics/src/gradient.rs @@ -1,18 +1,17 @@ //! For creating a Gradient. - use iced_native::Color; -use crate::widget::canvas::gradient::Linear; +use crate::gradient::linear::Linear; +use crate::Point; #[derive(Debug, Clone, PartialEq)] -/// A fill which transitions colors progressively along a direction, either linearly, radially, -/// or conically. +/// A fill which transitions colors progressively along a direction, either linearly, radially (TBD), +/// or conically (TBD). pub enum Gradient { /// A linear gradient interpolates colors along a direction from its [`start`] to its [`end`] /// point. Linear(Linear), } - #[derive(Debug, Clone, Copy, PartialEq)] /// A point along the gradient vector where the specified [`color`] is unmixed. pub struct ColorStop { @@ -20,4 +19,85 @@ pub struct ColorStop { pub offset: f32, /// The color of the gradient at the specified [`offset`]. pub color: Color, -} \ No newline at end of file +} + +impl Gradient { + /// Creates a new linear [`linear::Builder`]. + pub fn linear(start: Point, end: Point) -> linear::Builder { + linear::Builder::new(start, end) + } +} + +/// Linear gradient builder & definition. +pub mod linear { + use crate::gradient::{ColorStop, Gradient}; + use crate::{Color, Point}; + + /// A linear gradient that can be used in the style of [`super::Fill`] or [`super::Stroke`]. + #[derive(Debug, Clone, PartialEq)] + pub struct Linear { + /// The point where the linear gradient begins. + pub start: Point, + /// The point where the linear gradient ends. + pub end: Point, + /// [`ColorStop`]s along the linear gradient path. + pub color_stops: Vec, + } + + /// A [`Linear`] builder. + #[derive(Debug)] + pub struct Builder { + start: Point, + end: Point, + stops: Vec<(f32, Color)>, + valid: bool, + } + + impl Builder { + /// Creates a new [`Builder`]. + pub fn new(start: Point, end: Point) -> Self { + Self { + start, + end, + stops: vec![], + valid: true, + } + } + + /// Adds a new stop, defined by an offset and a color, to the gradient. + /// + /// `offset` must be between `0.0` and `1.0`. + pub fn add_stop(mut self, offset: f32, color: Color) -> Self { + if !(0.0..=1.0).contains(&offset) { + self.valid = false; + } + + self.stops.push((offset, color)); + self + } + + /// Builds the linear [`Gradient`] of this [`Builder`]. + /// + /// Returns `None` if no stops were added to the builder or + /// if stops not between 0.0 and 1.0 were added. + pub fn build(self) -> Option { + if self.stops.is_empty() || !self.valid { + return None; + } + + let mut stops: Vec = self.stops.clone().into_iter().map(|f| ColorStop { + offset: f.0, + color: f.1 + }).collect(); + + stops.sort_by(|a, b| a.offset.partial_cmp(&b.offset).unwrap()); + + Some(Gradient::Linear(Linear { + start: self.start, + end: self.end, + color_stops: stops + })) + } + } +} + diff --git a/graphics/src/shader.rs b/graphics/src/shader.rs index b9071c74..69679e9b 100644 --- a/graphics/src/shader.rs +++ b/graphics/src/shader.rs @@ -1,8 +1,7 @@ //! Supported shaders; -use crate::{Color, widget}; +use crate::Color; use crate::gradient::Gradient; -use crate::widget::canvas::{FillStyle, StrokeStyle}; #[derive(Debug, Clone)] /// Supported shaders for primitives. @@ -13,28 +12,10 @@ pub enum Shader { Gradient(Gradient) } -impl <'a> Into for StrokeStyle<'a> { +impl <'a> Into for Gradient { fn into(self) -> Shader { match self { - StrokeStyle::Solid(color) => Shader::Solid(color), - StrokeStyle::Gradient(gradient) => gradient.clone().into() - } - } -} - -impl <'a> Into for FillStyle<'a> { - fn into(self) -> Shader { - match self { - FillStyle::Solid(color) => Shader::Solid(color), - FillStyle::Gradient(gradient) => gradient.clone().into() - } - } -} - -impl <'a> Into for widget::canvas::Gradient { - fn into(self) -> Shader { - match self { - widget::canvas::Gradient::Linear(linear) => { + Gradient::Linear(linear) => { Shader::Gradient(Gradient::Linear(linear)) } } diff --git a/graphics/src/triangle.rs b/graphics/src/triangle.rs index 92709fe2..8f82c5df 100644 --- a/graphics/src/triangle.rs +++ b/graphics/src/triangle.rs @@ -16,12 +16,4 @@ pub struct Mesh2D { pub struct Vertex2D { /// The vertex position in 2D space. pub position: [f32; 2], -} - -/// Convert from lyon's position data to Iced's Vertex2D type. -impl Vertex2D { - /// Converts from [`lyon::math::Point`] to [`Vertex2D`]. Used for generating primitives. - pub fn from(points: Vec) -> Vec { - points.iter().map(|p| Vertex2D { position: [p.x, p.y]}).collect() - } -} +} \ No newline at end of file diff --git a/graphics/src/widget/canvas.rs b/graphics/src/widget/canvas.rs index 09aad98d..95c962af 100644 --- a/graphics/src/widget/canvas.rs +++ b/graphics/src/widget/canvas.rs @@ -5,7 +5,6 @@ //! and more! pub mod event; -pub mod gradient; pub mod path; mod cache; @@ -23,7 +22,6 @@ pub use event::Event; pub use fill::{Fill, FillRule, FillStyle}; pub use frame::Frame; pub use geometry::Geometry; -pub use gradient::Gradient; pub use path::Path; pub use program::Program; pub use stroke::{LineCap, LineDash, LineJoin, Stroke, StrokeStyle}; @@ -47,16 +45,12 @@ use std::marker::PhantomData; /// If you want to get a quick overview, here's how we can draw a simple circle: /// /// ```no_run -/// # mod iced { -/// # pub mod widget { -/// # pub use iced_graphics::widget::canvas; -/// # } -/// # pub use iced_native::{Color, Rectangle, Theme}; -/// # } -/// use iced::widget::canvas::{self, Canvas, Cursor, Fill, Frame, Geometry, Path, Program}; -/// use iced::{Color, Rectangle, Theme}; -/// /// // First, we define the data we need for drawing +/// use iced_graphics::{Color, Rectangle}; +/// use iced_graphics::widget::Canvas; +/// use iced_graphics::widget::canvas::{Cursor, Frame, Geometry, Path, Program}; +/// use iced_style::Theme; +/// /// #[derive(Debug)] /// struct Circle { /// radius: f32, diff --git a/graphics/src/widget/canvas/fill.rs b/graphics/src/widget/canvas/fill.rs index 02d2311f..60029e03 100644 --- a/graphics/src/widget/canvas/fill.rs +++ b/graphics/src/widget/canvas/fill.rs @@ -1,6 +1,6 @@ use iced_native::Color; - -use crate::widget::canvas::Gradient; +use crate::gradient::Gradient; +use crate::shader::Shader; /// The style used to fill geometry. #[derive(Debug, Clone)] @@ -48,6 +48,15 @@ pub enum FillStyle<'a> { Gradient(&'a Gradient), } +impl <'a> Into for FillStyle<'a> { + fn into(self) -> Shader { + match self { + FillStyle::Solid(color) => Shader::Solid(color), + FillStyle::Gradient(gradient) => gradient.clone().into() + } + } +} + /// The fill rule defines how to determine what is inside and what is outside of /// a shape. /// diff --git a/graphics/src/widget/canvas/frame.rs b/graphics/src/widget/canvas/frame.rs index 8845bc6a..30239b2a 100644 --- a/graphics/src/widget/canvas/frame.rs +++ b/graphics/src/widget/canvas/frame.rs @@ -335,7 +335,7 @@ impl Frame { if !buffer.indices.is_empty() { self.primitives.push(Primitive::Mesh2D { buffers: triangle::Mesh2D { - vertices: Vertex2D::from(buffer.vertices), + vertices: vertices_from(buffer.vertices), indices: buffer.indices, }, size: self.size, @@ -347,3 +347,8 @@ impl Frame { self.primitives } } + +/// Converts from [`lyon::math::Point`] to [`Vertex2D`]. Used for generating primitives. +fn vertices_from(points: Vec) -> Vec { + points.iter().map(|p| Vertex2D { position: [p.x, p.y]}).collect() +} \ No newline at end of file diff --git a/graphics/src/widget/canvas/gradient.rs b/graphics/src/widget/canvas/gradient.rs deleted file mode 100644 index 7d2daabc..00000000 --- a/graphics/src/widget/canvas/gradient.rs +++ /dev/null @@ -1,21 +0,0 @@ -//! Define a color gradient. -use iced_native::Point; - -pub mod linear; - -pub use linear::Linear; - -/// A gradient that can be used in the style of [`super::Fill`] or [`super::Stroke`]. -#[derive(Debug, Clone)] -pub enum Gradient { - /// A linear gradient - Linear(Linear), - //TODO: radial, conical -} - -impl Gradient { - /// Creates a new linear [`linear::Builder`]. - pub fn linear(start: Point, end: Point) -> linear::Builder { - linear::Builder::new(start, end) - } -} \ No newline at end of file diff --git a/graphics/src/widget/canvas/gradient/linear.rs b/graphics/src/widget/canvas/gradient/linear.rs deleted file mode 100644 index 1dc7e3a8..00000000 --- a/graphics/src/widget/canvas/gradient/linear.rs +++ /dev/null @@ -1,73 +0,0 @@ -//! A linear color gradient. -use iced_native::{Color, Point}; - -use crate::gradient::ColorStop; - -use super::Gradient; - -/// A linear gradient that can be used in the style of [`super::Fill`] or [`super::Stroke`]. -#[derive(Debug, Clone, PartialEq)] -pub struct Linear { - /// The point where the linear gradient begins. - pub start: Point, - /// The point where the linear gradient ends. - pub end: Point, - /// [`ColorStop`]s along the linear gradient path. - pub color_stops: Vec, -} - -/// A [`Linear`] builder. -#[derive(Debug)] -pub struct Builder { - start: Point, - end: Point, - stops: Vec<(f32, Color)>, - valid: bool, -} - -impl Builder { - /// Creates a new [`Builder`]. - pub fn new(start: Point, end: Point) -> Self { - Self { - start, - end, - stops: vec![], - valid: true, - } - } - - /// Adds a new stop, defined by an offset and a color, to the gradient. - /// - /// `offset` must be between `0.0` and `1.0`. - pub fn add_stop(mut self, offset: f32, color: Color) -> Self { - if !(0.0..=1.0).contains(&offset) { - self.valid = false; - } - - self.stops.push((offset, color)); - self - } - - /// Builds the linear [`Gradient`] of this [`Builder`]. - /// - /// Returns `None` if no stops were added to the builder or - /// if stops not between 0.0 and 1.0 were added. - pub fn build(self) -> Option { - if self.stops.is_empty() || !self.valid { - return None; - } - - let mut stops: Vec = self.stops.clone().into_iter().map(|f| ColorStop { - offset: f.0, - color: f.1 - }).collect(); - - stops.sort_by(|a, b| a.offset.partial_cmp(&b.offset).unwrap()); - - Some(Gradient::Linear(Linear { - start: self.start, - end: self.end, - color_stops: stops - })) - } -} diff --git a/graphics/src/widget/canvas/stroke.rs b/graphics/src/widget/canvas/stroke.rs index c319b398..ed82f189 100644 --- a/graphics/src/widget/canvas/stroke.rs +++ b/graphics/src/widget/canvas/stroke.rs @@ -1,6 +1,6 @@ use iced_native::Color; - -use crate::widget::canvas::Gradient; +use crate::gradient::Gradient; +use crate::shader::Shader; /// The style of a stroke. #[derive(Debug, Clone)] @@ -66,6 +66,15 @@ pub enum StrokeStyle<'a> { Gradient(&'a Gradient), } +impl <'a> Into for StrokeStyle<'a> { + fn into(self) -> Shader { + match self { + StrokeStyle::Solid(color) => Shader::Solid(color), + StrokeStyle::Gradient(gradient) => gradient.clone().into() + } + } +} + /// The shape used at the end of open subpaths when they are stroked. #[derive(Debug, Clone, Copy)] pub enum LineCap { -- cgit From 6e7b3ced0b1daf368e44e181ecdb4ae529877eb6 Mon Sep 17 00:00:00 2001 From: shan Date: Tue, 4 Oct 2022 18:24:46 -0700 Subject: Reworked wgpu buffers, updated glow side to have proper transform location storage, attempting to fix visibility modifiers, implemented some of the feedback received in initial PR. --- examples/arc/src/main.rs | 4 +- examples/clock/src/main.rs | 6 +- examples/modern_art/src/main.rs | 7 +- examples/solar_system/src/main.rs | 4 +- glow/src/backend.rs | 2 +- glow/src/shader/common/gradient.frag | 1 + glow/src/triangle.rs | 126 +++++---------- glow/src/triangle/gradient.rs | 92 ++++++----- glow/src/triangle/solid.rs | 62 ++++--- graphics/src/gradient.rs | 2 +- graphics/src/layer.rs | 32 ++-- graphics/src/widget/canvas.rs | 4 +- graphics/src/widget/canvas/fill.rs | 14 +- graphics/src/widget/canvas/stroke.rs | 14 +- wgpu/src/backend.rs | 2 +- wgpu/src/buffers/buffer.rs | 141 +++++++++------- wgpu/src/buffers/dynamic_buffers.rs | 3 +- wgpu/src/triangle.rs | 303 +++++++++++++++++------------------ wgpu/src/triangle/gradient.rs | 6 +- wgpu/src/triangle/solid.rs | 15 +- 20 files changed, 417 insertions(+), 423 deletions(-) diff --git a/examples/arc/src/main.rs b/examples/arc/src/main.rs index 6029a69c..bc7e49c6 100644 --- a/examples/arc/src/main.rs +++ b/examples/arc/src/main.rs @@ -2,7 +2,7 @@ use std::{f32::consts::PI, time::Instant}; use iced::executor; use iced::widget::canvas::{ - self, Cache, Canvas, Cursor, Geometry, Path, Stroke, StrokeStyle, + self, Cache, Canvas, Cursor, Geometry, Path, Stroke, Style, }; use iced::{ Application, Command, Element, Length, Point, Rectangle, Settings, @@ -114,7 +114,7 @@ impl canvas::Program for Arc { frame.stroke( &path, Stroke { - style: StrokeStyle::Solid(palette.text), + style: Style::Solid(palette.text), width: 10.0, ..Stroke::default() }, diff --git a/examples/clock/src/main.rs b/examples/clock/src/main.rs index 51f25a3f..06ed44f0 100644 --- a/examples/clock/src/main.rs +++ b/examples/clock/src/main.rs @@ -1,6 +1,6 @@ use iced::executor; use iced::widget::canvas::{ - Cache, Cursor, Geometry, LineCap, Path, Stroke, StrokeStyle, + Cache, Cursor, Geometry, LineCap, Path, Stroke, Style, }; use iced::widget::{canvas, container}; use iced::{ @@ -111,7 +111,7 @@ impl canvas::Program for Clock { let thin_stroke = || -> Stroke { Stroke { width, - style: StrokeStyle::Solid(Color::WHITE), + style: Style::Solid(Color::WHITE), line_cap: LineCap::Round, ..Stroke::default() } @@ -120,7 +120,7 @@ impl canvas::Program for Clock { let wide_stroke = || -> Stroke { Stroke { width: width * 3.0, - style: StrokeStyle::Solid(Color::WHITE), + style: Style::Solid(Color::WHITE), line_cap: LineCap::Round, ..Stroke::default() } diff --git a/examples/modern_art/src/main.rs b/examples/modern_art/src/main.rs index c7945012..238c9a0f 100644 --- a/examples/modern_art/src/main.rs +++ b/examples/modern_art/src/main.rs @@ -1,5 +1,5 @@ use rand::{Rng, thread_rng}; -use crate::canvas::{Cursor, FillStyle, Geometry}; +use crate::canvas::{Cursor, Geometry}; use iced::widget::canvas::{Cache, Fill, Frame}; use iced::widget::{canvas, Canvas}; use iced::Settings; @@ -8,6 +8,7 @@ use iced::{ Renderer, Size, Theme, }; use iced_graphics::gradient::Gradient; +use iced_graphics::widget::canvas::Style; fn main() -> iced::Result { ModernArt::run(Settings { @@ -120,7 +121,7 @@ fn generate_box(frame: &mut Frame, bounds: Size) -> bool { top_left, size, Fill { - style: FillStyle::Solid(random_color()), + style: Style::Solid(random_color()), .. Default::default() } ); @@ -129,7 +130,7 @@ fn generate_box(frame: &mut Frame, bounds: Size) -> bool { top_left, size, Fill { - style: FillStyle::Gradient(&gradient( + style: Style::Gradient(&gradient( top_left, Point::new(top_left.x + size.width, top_left.y + size.height) )), diff --git a/examples/solar_system/src/main.rs b/examples/solar_system/src/main.rs index fcd20561..8d713ce0 100644 --- a/examples/solar_system/src/main.rs +++ b/examples/solar_system/src/main.rs @@ -19,7 +19,7 @@ use iced::{ }; use std::time::Instant; -use crate::canvas::StrokeStyle; +use crate::canvas::Style; pub fn main() -> iced::Result { SolarSystem::run(Settings { @@ -179,7 +179,7 @@ impl canvas::Program for State { frame.stroke( &orbit, Stroke { - style: StrokeStyle::Solid(Color::from_rgba8(0, 153, 255, 0.1)), + style: Style::Solid(Color::from_rgba8(0, 153, 255, 0.1)), width: 1.0, line_dash: canvas::LineDash { offset: 0, diff --git a/glow/src/backend.rs b/glow/src/backend.rs index 6fc4fb38..7333d513 100644 --- a/glow/src/backend.rs +++ b/glow/src/backend.rs @@ -99,7 +99,7 @@ impl Backend { ); } - if !layer.meshes.0.is_empty() { + if !layer.meshes.is_empty() { let scaled = transformation * Transformation::scale(scale_factor, scale_factor); diff --git a/glow/src/shader/common/gradient.frag b/glow/src/shader/common/gradient.frag index 588f63e0..1afb557d 100644 --- a/glow/src/shader/common/gradient.frag +++ b/glow/src/shader/common/gradient.frag @@ -23,6 +23,7 @@ uniform uint color_stops_size; uniform float color_stop_offsets[MAX_STOPS]; uniform vec4 color_stop_colors[MAX_STOPS]; +//TODO: rewrite without branching to make ALUs happy void main() { vec2 gradient_vec = vec2(gradient_end - gradient_start); vec2 current_vec = vec2(raw_position.xy - gradient_start); diff --git a/glow/src/triangle.rs b/glow/src/triangle.rs index 85d873fe..f16f8af4 100644 --- a/glow/src/triangle.rs +++ b/glow/src/triangle.rs @@ -2,23 +2,22 @@ mod gradient; mod solid; -use crate::program::{self, Shader}; -use crate::Transformation; +use crate::{program, Transformation}; use glow::HasContext; -use iced_graphics::layer::{Mesh, Meshes}; +use iced_graphics::layer::{attribute_count_of, Mesh}; use iced_graphics::shader; use std::marker::PhantomData; use crate::triangle::gradient::GradientProgram; use crate::triangle::solid::SolidProgram; pub use iced_graphics::triangle::{Mesh2D, Vertex2D}; +use shader::Shader; #[derive(Debug)] pub(crate) struct Pipeline { vertex_array: ::VertexArray, vertices: Buffer, indices: Buffer, - current_transform: Transformation, programs: TrianglePrograms, } @@ -68,7 +67,6 @@ impl Pipeline { vertex_array, vertices, indices, - current_transform: Transformation::identity(), programs: TrianglePrograms { solid: SolidProgram::new(gl, shader_version), gradient: GradientProgram::new(gl, shader_version), @@ -78,7 +76,7 @@ impl Pipeline { pub fn draw( &mut self, - meshes: &Meshes<'_>, + meshes: &[Mesh<'_>], gl: &glow::Context, target_height: u32, transformation: Transformation, @@ -90,8 +88,8 @@ impl Pipeline { gl.bind_vertex_array(Some(self.vertex_array)) } - //count the total number of vertices & indices we need to handle for all meshes - let (total_vertices, total_indices) = meshes.attribute_count(); + //count the total amount of vertices & indices we need to handle + let (total_vertices, total_indices) = attribute_count_of(meshes); // Then we ensure the current attribute buffers are big enough, resizing if necessary unsafe { @@ -100,25 +98,25 @@ impl Pipeline { } // We upload all the vertices and indices upfront - let mut last_vertex = 0; - let mut last_index = 0; + let mut vertex_offset = 0; + let mut index_offset = 0; - for Mesh { buffers, .. } in meshes.0.iter() { + for mesh in meshes { unsafe { gl.buffer_sub_data_u8_slice( glow::ARRAY_BUFFER, - (last_vertex * std::mem::size_of::()) as i32, - bytemuck::cast_slice(&buffers.vertices), + (vertex_offset * std::mem::size_of::()) as i32, + bytemuck::cast_slice(&mesh.buffers.vertices), ); gl.buffer_sub_data_u8_slice( glow::ELEMENT_ARRAY_BUFFER, - (last_index * std::mem::size_of::()) as i32, - bytemuck::cast_slice(&buffers.indices), + (index_offset * std::mem::size_of::()) as i32, + bytemuck::cast_slice(&mesh.buffers.indices), ); - last_vertex += buffers.vertices.len(); - last_index += buffers.indices.len(); + vertex_offset += mesh.buffers.vertices.len(); + index_offset += mesh.buffers.indices.len(); } } @@ -126,22 +124,11 @@ impl Pipeline { let mut last_vertex = 0; let mut last_index = 0; - for (index, Mesh { - buffers, - origin, - clip_bounds, - shader, - }) in meshes.0.iter().enumerate() - { - let transform = - transformation * Transformation::translate(origin.x, origin.y); - - if index == 0 { - //set initial transform uniform for both programs - self.programs.set_transforms(gl, transform); - } + for mesh in meshes { + let transform = transformation + * Transformation::translate(mesh.origin.x, mesh.origin.y); - let clip_bounds = (*clip_bounds * scale_factor).snap(); + let clip_bounds = (mesh.clip_bounds * scale_factor).snap(); unsafe { gl.scissor( @@ -152,25 +139,25 @@ impl Pipeline { clip_bounds.height as i32, ); - let t = if self.current_transform != transform { - self.current_transform = transform; - Some(transform) - } else { - None - }; - - self.use_with_shader(gl, shader, t); + match mesh.shader { + Shader::Solid(color) => { + self.programs.solid.use_program(gl, &color, &transform); + } + Shader::Gradient(gradient) => { + self.programs.gradient.use_program(gl, &gradient, &transform); + } + } gl.draw_elements_base_vertex( glow::TRIANGLES, - buffers.indices.len() as i32, + mesh.buffers.indices.len() as i32, glow::UNSIGNED_INT, (last_index * std::mem::size_of::()) as i32, last_vertex as i32, ); - last_vertex += buffers.vertices.len(); - last_index += buffers.indices.len(); + last_vertex += mesh.buffers.vertices.len(); + last_index += mesh.buffers.indices.len(); } } @@ -180,31 +167,6 @@ impl Pipeline { gl.disable(glow::MULTISAMPLE); } } - - fn use_with_shader( - &mut self, - gl: &glow::Context, - shader: &shader::Shader, - transform: Option, - ) { - match shader { - shader::Shader::Solid(color) => { - unsafe { gl.use_program(Some(self.programs.solid.program)) } - self.programs.solid.set_uniforms(gl, color, transform); - } - shader::Shader::Gradient(gradient) => { - unsafe { gl.use_program(Some(self.programs.gradient.program)) } - self.programs.gradient.set_uniforms(gl, gradient, transform); - } - } - } -} - -impl TrianglePrograms { - pub fn set_transforms(&self, gl: &glow::Context, transform: Transformation) { - update_transform(gl, self.solid.program, Some(transform)); - update_transform(gl, self.gradient.program, Some(transform)); - } } /// A simple shader program. Uses [`triangle.vert`] for its vertex shader and only binds position @@ -215,14 +177,14 @@ pub(super) fn simple_triangle_program( fragment_shader: &'static str, ) -> ::Program { unsafe { - let vertex_shader = Shader::vertex( + let vertex_shader = program::Shader::vertex( gl, shader_version, include_str!("shader/common/triangle.vert"), ); let fragment_shader = - Shader::fragment(gl, shader_version, fragment_shader); + program::Shader::fragment(gl, shader_version, fragment_shader); program::create( gl, @@ -232,23 +194,17 @@ pub(super) fn simple_triangle_program( } } -pub(super) fn update_transform( +pub fn set_transform( gl: &glow::Context, - program: ::Program, - transform: Option + location: ::UniformLocation, + transform: Transformation, ) { - if let Some(t) = transform { - let transform_location = - unsafe { gl.get_uniform_location(program, "u_Transform") } - .expect("Get transform location."); - - unsafe { - gl.uniform_matrix_4_f32_slice( - Some(&transform_location), - false, - t.as_ref(), - ); - } + unsafe { + gl.uniform_matrix_4_f32_slice( + Some(&location), + false, + transform.as_ref() + ); } } diff --git a/glow/src/triangle/gradient.rs b/glow/src/triangle/gradient.rs index d1b10d77..547871e2 100644 --- a/glow/src/triangle/gradient.rs +++ b/glow/src/triangle/gradient.rs @@ -1,18 +1,42 @@ use crate::program::Version; -use crate::triangle::{simple_triangle_program, update_transform}; +use crate::triangle::{simple_triangle_program, set_transform}; use glow::{Context, HasContext, NativeProgram}; +use iced_graphics::gradient::Linear; use iced_graphics::gradient::Gradient; -use iced_graphics::widget::canvas::gradient::Linear; use iced_graphics::Transformation; #[derive(Debug)] -pub(super) struct GradientProgram { - pub(super) program: ::Program, - pub(super) uniform_data: GradientUniformData, +pub struct GradientProgram { + pub program: ::Program, + pub uniform_data: GradientUniformData, +} + +#[derive(Debug)] +pub struct GradientUniformData { + gradient: Gradient, + transform: Transformation, + uniform_locations: GradientUniformLocations, +} + +#[derive(Debug)] +struct GradientUniformLocations { + gradient_start_location: ::UniformLocation, + gradient_end_location: ::UniformLocation, + color_stops_size_location: ::UniformLocation, + //currently the maximum number of stops is 64 due to needing to allocate the + //memory for the array of stops with a const value in GLSL + color_stops_locations: [ColorStopLocation; 64], + transform_location: ::UniformLocation, +} + +#[derive(Copy, Debug, Clone)] +struct ColorStopLocation { + color: ::UniformLocation, + offset: ::UniformLocation, } impl GradientProgram { - pub(super) fn new(gl: &Context, shader_version: &Version) -> Self { + pub fn new(gl: &Context, shader_version: &Version) -> Self { let program = simple_triangle_program( gl, shader_version, @@ -25,15 +49,17 @@ impl GradientProgram { } } - pub(super) fn set_uniforms<'a>( + pub fn write_uniforms( &mut self, gl: &Context, gradient: &Gradient, - transform: Option, + transform: &Transformation, ) { - update_transform(gl, self.program, transform); + if transform != &self.uniform_data.transform { + set_transform(gl, self.uniform_data.uniform_locations.transform_location, *transform); + } - if &self.uniform_data.current_gradient != gradient { + if &self.uniform_data.gradient != gradient { match gradient { Gradient::Linear(linear) => { let gradient_start: [f32; 2] = (linear.start).into(); @@ -104,31 +130,17 @@ impl GradientProgram { } } - self.uniform_data.current_gradient = gradient.clone(); + self.uniform_data.gradient = gradient.clone(); } } -} - -#[derive(Debug)] -pub(super) struct GradientUniformData { - current_gradient: Gradient, - uniform_locations: GradientUniformLocations, -} -#[derive(Debug)] -struct GradientUniformLocations { - gradient_start_location: ::UniformLocation, - gradient_end_location: ::UniformLocation, - color_stops_size_location: ::UniformLocation, - //currently the maximum number of stops is 64 due to needing to allocate the - //memory for the array of stops with a const value in GLSL - color_stops_locations: [ColorStopLocation; 64], -} + pub fn use_program(&mut self, gl: &glow::Context, gradient: &Gradient, transform: &Transformation) { + unsafe { + gl.use_program(Some(self.program)) + } -#[derive(Copy, Debug, Clone)] -struct ColorStopLocation { - color: ::UniformLocation, - offset: ::UniformLocation, + self.write_uniforms(gl, gradient, transform); + } } impl GradientUniformData { @@ -153,10 +165,7 @@ impl GradientUniformData { &format!("color_stop_offsets[{}]", index), ) } - .expect(&format!( - "Gradient - Color stop offset with index {}", - index - )); + .expect("Gradient - Color stop offset location."); let color = unsafe { gl.get_uniform_location( @@ -164,25 +173,28 @@ impl GradientUniformData { &format!("color_stop_colors[{}]", index), ) } - .expect(&format!( - "Gradient - Color stop colors with index {}", - index - )); + .expect("Gradient - Color stop color location."); ColorStopLocation { color, offset } }); + let transform_location = + unsafe { gl.get_uniform_location(program, "u_Transform") } + .expect("Get transform location."); + GradientUniformData { - current_gradient: Gradient::Linear(Linear { + gradient: Gradient::Linear(Linear { start: Default::default(), end: Default::default(), color_stops: vec![], }), + transform: Transformation::identity(), uniform_locations: GradientUniformLocations { gradient_start_location, gradient_end_location, color_stops_size_location, color_stops_locations, + transform_location, }, } } diff --git a/glow/src/triangle/solid.rs b/glow/src/triangle/solid.rs index 3a33cea8..d5b73eb9 100644 --- a/glow/src/triangle/solid.rs +++ b/glow/src/triangle/solid.rs @@ -1,13 +1,38 @@ use crate::program::Version; -use crate::triangle::{simple_triangle_program, update_transform}; +use crate::triangle::{set_transform, simple_triangle_program}; use crate::Color; use glow::{Context, HasContext, NativeProgram}; use iced_graphics::Transformation; #[derive(Debug)] pub struct SolidProgram { - pub(crate) program: ::Program, - pub(crate) uniform_data: SolidUniformData, + program: ::Program, + uniform_data: SolidUniformData, +} + +#[derive(Debug)] +pub(crate) struct SolidUniformData { + pub color: Color, + pub color_location: ::UniformLocation, + pub transform: Transformation, + pub transform_location: ::UniformLocation, +} + +impl SolidUniformData { + fn new(gl: &Context, program: NativeProgram) -> Self { + Self { + color: Color::TRANSPARENT, + color_location: unsafe { + gl.get_uniform_location(program, "color") + } + .expect("Solid - Color uniform location."), + transform: Transformation::identity(), + transform_location: unsafe { + gl.get_uniform_location(program, "u_Transform") + } + .expect("Get transform location."), + } + } } impl SolidProgram { @@ -24,15 +49,17 @@ impl SolidProgram { } } - pub fn set_uniforms<'a>( + pub fn write_uniforms( &mut self, gl: &Context, color: &Color, - transform: Option, + transform: &Transformation, ) { - update_transform(gl, self.program, transform); + if transform != &self.uniform_data.transform { + set_transform(gl, self.uniform_data.transform_location, *transform) + } - if &self.uniform_data.color != color { + if color != &self.uniform_data.color { unsafe { gl.uniform_4_f32( Some(&self.uniform_data.color_location), @@ -46,22 +73,11 @@ impl SolidProgram { self.uniform_data.color = *color; } } -} - -#[derive(Debug)] -pub(crate) struct SolidUniformData { - pub color: Color, - pub color_location: ::UniformLocation, -} -impl SolidUniformData { - fn new(gl: &Context, program: NativeProgram) -> Self { - Self { - color: Color::TRANSPARENT, - color_location: unsafe { - gl.get_uniform_location(program, "color") - } - .expect("Solid - Color uniform location."), + pub fn use_program(&mut self, gl: &glow::Context, color: &Color, transform: &Transformation) { + unsafe { + gl.use_program(Some(self.program)) } + self.write_uniforms(gl, color, transform) } -} +} \ No newline at end of file diff --git a/graphics/src/gradient.rs b/graphics/src/gradient.rs index fa57842b..0c394e8b 100644 --- a/graphics/src/gradient.rs +++ b/graphics/src/gradient.rs @@ -1,6 +1,6 @@ //! For creating a Gradient. use iced_native::Color; -use crate::gradient::linear::Linear; +pub use crate::gradient::linear::Linear; use crate::Point; #[derive(Debug, Clone, PartialEq)] diff --git a/graphics/src/layer.rs b/graphics/src/layer.rs index b7731922..096c50dc 100644 --- a/graphics/src/layer.rs +++ b/graphics/src/layer.rs @@ -19,7 +19,7 @@ pub struct Layer<'a> { pub quads: Vec, /// The triangle meshes of the [`Layer`]. - pub meshes: Meshes<'a>, + pub meshes: Vec>, /// The text of the [`Layer`]. pub text: Vec>, @@ -34,7 +34,7 @@ impl<'a> Layer<'a> { Self { bounds, quads: Vec::new(), - meshes: Meshes(Vec::new()), + meshes: Vec::new(), text: Vec::new(), images: Vec::new(), } @@ -174,7 +174,7 @@ impl<'a> Layer<'a> { // Only draw visible content if let Some(clip_bounds) = layer.bounds.intersection(&bounds) { - layer.meshes.0.push( + layer.meshes.push( Mesh { origin: Point::new(translation.x, translation.y), buffers, @@ -335,20 +335,14 @@ unsafe impl bytemuck::Zeroable for Quad {} #[allow(unsafe_code)] unsafe impl bytemuck::Pod for Quad {} -#[derive(Debug)] -/// A collection of meshes. -pub struct Meshes<'a>(pub Vec>); - -impl<'a> Meshes<'a> { - /// Returns the number of total vertices & total indices of all [`Mesh`]es. - pub fn attribute_count(&self) -> (usize, usize) { - self.0 - .iter() - .map(|Mesh { buffers, .. }| { - (buffers.vertices.len(), buffers.indices.len()) - }) - .fold((0, 0), |(total_v, total_i), (v, i)| { - (total_v + v, total_i + i) - }) - } +/// Returns the number of total vertices & total indices of all [`Mesh`]es. +pub fn attribute_count_of<'a>(meshes: &'a [Mesh<'a>]) -> (usize, usize) { + meshes + .iter() + .map(|Mesh { buffers, .. }| { + (buffers.vertices.len(), buffers.indices.len()) + }) + .fold((0, 0), |(total_v, total_i), (v, i)| { + (total_v + v, total_i + i) + }) } \ No newline at end of file diff --git a/graphics/src/widget/canvas.rs b/graphics/src/widget/canvas.rs index 95c962af..f6929e97 100644 --- a/graphics/src/widget/canvas.rs +++ b/graphics/src/widget/canvas.rs @@ -19,12 +19,12 @@ mod text; pub use cache::Cache; pub use cursor::Cursor; pub use event::Event; -pub use fill::{Fill, FillRule, FillStyle}; +pub use fill::{Fill, FillRule, Style}; pub use frame::Frame; pub use geometry::Geometry; pub use path::Path; pub use program::Program; -pub use stroke::{LineCap, LineDash, LineJoin, Stroke, StrokeStyle}; +pub use stroke::{LineCap, LineDash, LineJoin, Stroke}; pub use text::Text; use crate::{Backend, Primitive, Renderer}; diff --git a/graphics/src/widget/canvas/fill.rs b/graphics/src/widget/canvas/fill.rs index 60029e03..6f10505c 100644 --- a/graphics/src/widget/canvas/fill.rs +++ b/graphics/src/widget/canvas/fill.rs @@ -8,7 +8,7 @@ pub struct Fill<'a> { /// The color or gradient of the fill. /// /// By default, it is set to [`FillStyle::Solid`] `BLACK`. - pub style: FillStyle<'a>, + pub style: Style<'a>, /// The fill rule defines how to determine what is inside and what is /// outside of a shape. @@ -24,7 +24,7 @@ pub struct Fill<'a> { impl <'a> Default for Fill<'a> { fn default() -> Fill<'a> { Fill { - style: FillStyle::Solid(Color::BLACK), + style: Style::Solid(Color::BLACK), rule: FillRule::NonZero, } } @@ -33,7 +33,7 @@ impl <'a> Default for Fill<'a> { impl<'a> From for Fill<'a> { fn from(color: Color) -> Fill<'a> { Fill { - style: FillStyle::Solid(color), + style: Style::Solid(color), ..Fill::default() } } @@ -41,18 +41,18 @@ impl<'a> From for Fill<'a> { /// The color or gradient of a [`Fill`]. #[derive(Debug, Clone)] -pub enum FillStyle<'a> { +pub enum Style<'a> { /// A solid color Solid(Color), /// A color gradient Gradient(&'a Gradient), } -impl <'a> Into for FillStyle<'a> { +impl <'a> Into for Style<'a> { fn into(self) -> Shader { match self { - FillStyle::Solid(color) => Shader::Solid(color), - FillStyle::Gradient(gradient) => gradient.clone().into() + Style::Solid(color) => Shader::Solid(color), + Style::Gradient(gradient) => gradient.clone().into() } } } diff --git a/graphics/src/widget/canvas/stroke.rs b/graphics/src/widget/canvas/stroke.rs index ed82f189..7ce5ff1d 100644 --- a/graphics/src/widget/canvas/stroke.rs +++ b/graphics/src/widget/canvas/stroke.rs @@ -8,7 +8,7 @@ pub struct Stroke<'a> { /// The color or gradient of the stroke. /// /// By default, it is set to [`StrokeStyle::Solid`] `BLACK`. - pub style: StrokeStyle<'a>, + pub style: Style<'a>, /// The distance between the two edges of the stroke. pub width: f32, /// The shape to be used at the end of open subpaths when they are stroked. @@ -24,7 +24,7 @@ impl<'a> Stroke<'a> { /// Sets the color of the [`Stroke`]. pub fn with_color(self, color: Color) -> Self { Stroke { - style: StrokeStyle::Solid(color), + style: Style::Solid(color), ..self } } @@ -48,7 +48,7 @@ impl<'a> Stroke<'a> { impl<'a> Default for Stroke<'a> { fn default() -> Self { Stroke { - style: StrokeStyle::Solid(Color::BLACK), + style: Style::Solid(Color::BLACK), width: 1.0, line_cap: LineCap::default(), line_join: LineJoin::default(), @@ -59,18 +59,18 @@ impl<'a> Default for Stroke<'a> { /// The color or gradient of a [`Stroke`]. #[derive(Debug, Clone, Copy)] -pub enum StrokeStyle<'a> { +pub enum Style<'a> { /// A solid color Solid(Color), /// A color gradient Gradient(&'a Gradient), } -impl <'a> Into for StrokeStyle<'a> { +impl <'a> Into for Style<'a> { fn into(self) -> Shader { match self { - StrokeStyle::Solid(color) => Shader::Solid(color), - StrokeStyle::Gradient(gradient) => gradient.clone().into() + Style::Solid(color) => Shader::Solid(color), + Style::Gradient(gradient) => gradient.clone().into() } } } diff --git a/wgpu/src/backend.rs b/wgpu/src/backend.rs index fd688004..9295a491 100644 --- a/wgpu/src/backend.rs +++ b/wgpu/src/backend.rs @@ -132,7 +132,7 @@ impl Backend { ); } - if !layer.meshes.0.is_empty() { + if !layer.meshes.is_empty() { let scaled = transformation * Transformation::scale(scale_factor, scale_factor); diff --git a/wgpu/src/buffers/buffer.rs b/wgpu/src/buffers/buffer.rs index dae3b038..a44120d3 100644 --- a/wgpu/src/buffers/buffer.rs +++ b/wgpu/src/buffers/buffer.rs @@ -1,91 +1,124 @@ //! Utilities for static buffer operations. +use bytemuck::{Pod, Zeroable}; +use std::marker::PhantomData; +use std::mem; + +//128 triangles/indices +const DEFAULT_STATIC_BUFFER_COUNT: wgpu::BufferAddress = 128; /// A generic buffer struct useful for items which have no alignment requirements /// (e.g. Vertex, Index buffers) and are set once and never changed until destroyed. -/// -/// This buffer is mapped to the GPU on creation, so must be initialized with the correct capacity. #[derive(Debug)] -pub(crate) struct StaticBuffer { - //stored sequentially per mesh iteration +pub(crate) struct StaticBuffer { + //stored sequentially per mesh iteration; refers to the offset index in the GPU buffer offsets: Vec, + label: &'static str, + usages: wgpu::BufferUsages, gpu: wgpu::Buffer, //the static size of the buffer size: wgpu::BufferAddress, + _data: PhantomData, } -impl StaticBuffer { +impl StaticBuffer { + /// Initialize a new static buffer. pub fn new( device: &wgpu::Device, label: &'static str, - size: u64, - usage: wgpu::BufferUsages, - total_offsets: usize, + usages: wgpu::BufferUsages, ) -> Self { + let size = (mem::size_of::() as u64) * DEFAULT_STATIC_BUFFER_COUNT; + Self { - offsets: Vec::with_capacity(total_offsets), - gpu: device.create_buffer(&wgpu::BufferDescriptor { - label: Some(label), - size, - usage, - mapped_at_creation: true, - }), + offsets: Vec::new(), + label, + usages, + gpu: Self::gpu_buffer(device, label, size, usages), size, + _data: Default::default(), } } - /// Resolves pending write operations & unmaps buffer from host memory. - pub fn flush(&self) { - (&self.gpu).unmap(); + fn gpu_buffer( + device: &wgpu::Device, + label: &'static str, + size: wgpu::BufferAddress, + usage: wgpu::BufferUsages, + ) -> wgpu::Buffer { + device.create_buffer(&wgpu::BufferDescriptor { + label: Some(label), + size, + usage, + mapped_at_creation: false, + }) } - /// Returns whether or not the buffer needs to be recreated. This can happen whenever the mesh - /// data is re-submitted. - pub fn needs_recreate(&self, new_size: usize) -> bool { - self.size != new_size as u64 - } + /// Returns whether or not the buffer needs to be recreated. This can happen whenever mesh data + /// changes & a redraw is requested. + pub fn recreate_if_needed( + &mut self, + device: &wgpu::Device, + new_count: usize, + ) -> bool { + let size = + wgpu::BufferAddress::from((mem::size_of::() * new_count) as u64); - /// Writes the current vertex data to the gpu buffer with a memcpy & stores its offset. - pub fn write(&mut self, offset: u64, content: &[u8]) { - //offset has to be divisible by 8 for alignment reasons - let actual_offset = if offset % 8 != 0 { - offset + 4 + if self.size <= size { + self.offsets.clear(); + self.size = size; + self.gpu = Self::gpu_buffer(device, self.label, size, self.usages); + true } else { - offset - }; + false + } + } - let mut buffer = self - .gpu - .slice(actual_offset..(actual_offset + content.len() as u64)) - .get_mapped_range_mut(); - buffer.copy_from_slice(content); - self.offsets.push(actual_offset); + /// Writes the current vertex data to the gpu buffer if it is currently writable with a memcpy & + /// stores its offset. + /// + /// This will return either the offset of the written bytes, or `None` if the GPU buffer is not + /// currently writable. + pub fn write( + &mut self, + device: &wgpu::Device, + staging_belt: &mut wgpu::util::StagingBelt, + encoder: &mut wgpu::CommandEncoder, + offset: u64, + content: &[T], + ) -> u64 { + let bytes = bytemuck::cast_slice(content); + let bytes_size = bytes.len() as u64; + + if let Some(buffer_size) = wgpu::BufferSize::new(bytes_size as u64) { + //offset has to be divisible by 8 for alignment reasons + let actual_offset = if offset % 8 != 0 { offset + 4 } else { offset }; + + let mut buffer = staging_belt.write_buffer( + encoder, + &self.gpu, + actual_offset, + buffer_size, + device, + ); + + buffer.copy_from_slice(bytes); + + self.offsets.push(actual_offset); + } + + bytes_size } fn offset_at(&self, index: usize) -> &wgpu::BufferAddress { self.offsets .get(index) - .expect(&format!("Offset index {} is not in range.", index)) + .expect("Offset at index does not exist.") } /// Returns the slice calculated from the offset stored at the given index. - /// e.g. to calculate the slice for the 2nd mesh in the layer, this would be the offset at index + /// e.g. to calculate the slice for the 2nd mesh in the layer, this would be the offset at index /// 1 that we stored earlier when writing. - pub fn slice_from_index( - &self, - index: usize, - ) -> wgpu::BufferSlice<'_> { + pub fn slice_from_index(&self, index: usize) -> wgpu::BufferSlice<'_> { self.gpu.slice(self.offset_at(index)..) } } - -/// Returns true if the current buffer doesn't exist & needs to be created, or if it's too small -/// for the new content. -pub(crate) fn needs_recreate( - buffer: &Option, - new_size: usize, -) -> bool { - match buffer { - None => true, - Some(buf) => buf.needs_recreate(new_size), - } -} diff --git a/wgpu/src/buffers/dynamic_buffers.rs b/wgpu/src/buffers/dynamic_buffers.rs index d81529ce..75cc202c 100644 --- a/wgpu/src/buffers/dynamic_buffers.rs +++ b/wgpu/src/buffers/dynamic_buffers.rs @@ -50,7 +50,6 @@ impl DynamicBufferType { } } -//TODO think about making cpu & gpu buffers optional pub(crate) struct DynamicBuffer { offsets: Vec, cpu: DynamicBufferType, @@ -183,7 +182,7 @@ impl DynamicBuffer { let offset = self .offsets .get(index) - .expect(&format!("Index {} not found in offsets.", index)) + .expect("Index not found in offsets.") .clone(); offset diff --git a/wgpu/src/triangle.rs b/wgpu/src/triangle.rs index f1770e9a..df5e3132 100644 --- a/wgpu/src/triangle.rs +++ b/wgpu/src/triangle.rs @@ -3,11 +3,11 @@ use crate::{settings, Transformation}; use core::fmt; use std::fmt::Formatter; -use iced_graphics::layer::Meshes; +use iced_graphics::layer::{attribute_count_of, Mesh}; use iced_graphics::shader::Shader; use iced_graphics::Size; -use crate::buffers::buffer::{needs_recreate, StaticBuffer}; +use crate::buffers::buffer::StaticBuffer; use crate::triangle::gradient::GradientPipeline; use crate::triangle::solid::SolidPipeline; pub use iced_graphics::triangle::{Mesh2D, Vertex2D}; @@ -20,10 +20,9 @@ mod solid; #[derive(Debug)] pub(crate) struct Pipeline { blit: Option, - // these are optional so we don't allocate any memory to the GPU if - // application has no triangle meshes. - vertex_buffer: Option, - index_buffer: Option, + vertex_buffer: StaticBuffer, + index_buffer: StaticBuffer, + index_strides: Vec, pipelines: TrianglePipelines, } @@ -69,8 +68,17 @@ impl Pipeline { ) -> Pipeline { Pipeline { blit: antialiasing.map(|a| msaa::Blit::new(device, format, a)), - vertex_buffer: None, - index_buffer: None, + vertex_buffer: StaticBuffer::new( + device, + "iced_wgpu::triangle vertex buffer", + wgpu::BufferUsages::VERTEX | wgpu::BufferUsages::COPY_DST, + ), + index_buffer: StaticBuffer::new( + device, + "iced_wgpu::triangle vertex buffer", + wgpu::BufferUsages::INDEX | wgpu::BufferUsages::COPY_DST, + ), + index_strides: Vec::new(), pipelines: TrianglePipelines { solid: SolidPipeline::new(device, format, antialiasing), gradient: GradientPipeline::new(device, format, antialiasing), @@ -88,177 +96,152 @@ impl Pipeline { target_size: Size, transformation: Transformation, scale_factor: f32, - meshes: &Meshes<'_>, + meshes: &[Mesh<'_>], ) { - //count the total number of vertices & indices we need to handle - let (total_vertices, total_indices) = meshes.attribute_count(); + //count the total amount of vertices & indices we need to handle + let (total_vertices, total_indices) = attribute_count_of(meshes); + + // Then we ensure the current attribute buffers are big enough, resizing if necessary + // with wgpu this means recreating the buffer. + + //We are not currently using the return value of these functions as we have no system in + //place to calculate mesh diff, or to know whether or not that would be more performant for + //the majority of use cases. Therefore we will write GPU data every frame (for now). + let _ = self.vertex_buffer.recreate_if_needed(device, total_vertices); + let _ = self.index_buffer.recreate_if_needed(device, total_indices); + + //prepare dynamic buffers & data store for writing + self.index_strides.clear(); + self.pipelines.clear(); + + let mut vertex_offset = 0; + let mut index_offset = 0; - //Only create buffers if they need to be re-sized or don't exist - if needs_recreate(&self.vertex_buffer, total_vertices) { - //mapped to GPU at creation with total vertices - self.vertex_buffer = Some(StaticBuffer::new( + for mesh in meshes { + let transform = transformation + * Transformation::translate(mesh.origin.x, mesh.origin.y); + + //write to both buffers + let new_vertex_offset = self.vertex_buffer.write( device, - "iced_wgpu::triangle vertex buffer", - //TODO: a more reasonable default to prevent frequent resizing calls - // before this was 10_000 - (std::mem::size_of::() * total_vertices) as u64, - wgpu::BufferUsages::VERTEX, - meshes.0.len(), - )) - } + staging_belt, + encoder, + vertex_offset, + &mesh.buffers.vertices, + ); - if needs_recreate(&self.index_buffer, total_indices) { - //mapped to GPU at creation with total indices - self.index_buffer = Some(StaticBuffer::new( + let new_index_offset = self.index_buffer.write( device, - "iced_wgpu::triangle index buffer", - //TODO: a more reasonable default to prevent frequent resizing calls - // before this was 10_000 - (std::mem::size_of::() * total_indices) as u64, - wgpu::BufferUsages::INDEX, - meshes.0.len(), - )); - } + staging_belt, + encoder, + index_offset, + &mesh.buffers.indices, + ); - if let Some(vertex_buffer) = &mut self.vertex_buffer { - if let Some(index_buffer) = &mut self.index_buffer { - let mut offset_v = 0; - let mut offset_i = 0; - //TODO: store this more efficiently - let mut indices_lengths = Vec::with_capacity(meshes.0.len()); - - //iterate through meshes to write all attribute data - for mesh in meshes.0.iter() { - let transform = transformation - * Transformation::translate( - mesh.origin.x, - mesh.origin.y, - ); + vertex_offset = vertex_offset + new_vertex_offset; + index_offset = index_offset + new_index_offset; - let vertices = bytemuck::cast_slice(&mesh.buffers.vertices); - let indices = bytemuck::cast_slice(&mesh.buffers.indices); - - //TODO: it's (probably) more efficient to reduce this write command and - // iterate first and then upload - vertex_buffer.write(offset_v, vertices); - index_buffer.write(offset_i, indices); - - offset_v += vertices.len() as u64; - offset_i += indices.len() as u64; - indices_lengths.push(mesh.buffers.indices.len()); - - match mesh.shader { - Shader::Solid(color) => { - self.pipelines.solid.push(transform, color); - } - Shader::Gradient(gradient) => { - self.pipelines.gradient.push(transform, gradient); - } - } + self.index_strides.push(mesh.buffers.indices.len() as u32); + + //push uniform data to CPU buffers + match mesh.shader { + Shader::Solid(color) => { + self.pipelines.solid.push(transform, color); } + Shader::Gradient(gradient) => { + self.pipelines.gradient.push(transform, gradient); + } + } + } - //done writing to gpu buffer, unmap from host memory since we don't need it - //anymore - vertex_buffer.flush(); - index_buffer.flush(); - - //resize & memcpy uniforms from CPU buffers to GPU buffers for all pipelines - self.pipelines.write(device, staging_belt, encoder); - - //configure the render pass now that the data is uploaded to the GPU - { - //configure antialiasing pass - let (attachment, resolve_target, load) = - if let Some(blit) = &mut self.blit { - let (attachment, resolve_target) = blit.targets( - device, - target_size.width, - target_size.height, - ); - - ( - attachment, - Some(resolve_target), - wgpu::LoadOp::Clear(wgpu::Color::TRANSPARENT), - ) - } else { - (target, None, wgpu::LoadOp::Load) - }; - - let mut render_pass = encoder.begin_render_pass( - &wgpu::RenderPassDescriptor { - label: Some("iced_wgpu::triangle render pass"), - color_attachments: &[Some( - wgpu::RenderPassColorAttachment { - view: attachment, - resolve_target, - ops: wgpu::Operations { load, store: true }, - }, - )], - depth_stencil_attachment: None, + //write uniform data to GPU + self.pipelines.write(device, staging_belt, encoder); + + //configure the render pass now that the data is uploaded to the GPU + { + //configure antialiasing pass + let (attachment, resolve_target, load) = if let Some(blit) = + &mut self.blit + { + let (attachment, resolve_target) = + blit.targets(device, target_size.width, target_size.height); + + ( + attachment, + Some(resolve_target), + wgpu::LoadOp::Clear(wgpu::Color::TRANSPARENT), + ) + } else { + (target, None, wgpu::LoadOp::Load) + }; + + let mut render_pass = + encoder.begin_render_pass(&wgpu::RenderPassDescriptor { + label: Some("iced_wgpu::triangle render pass"), + color_attachments: &[Some( + wgpu::RenderPassColorAttachment { + view: attachment, + resolve_target, + ops: wgpu::Operations { load, store: true }, }, - ); - - //TODO: do this a better way; store it in the respective pipelines perhaps - // to be more readable - let mut num_solids = 0; - let mut num_gradients = 0; - - //TODO: try to avoid this extra iteration if possible - for index in 0..meshes.0.len() { - let clip_bounds = - (meshes.0[index].clip_bounds * scale_factor).snap(); - - render_pass.set_scissor_rect( - clip_bounds.x, - clip_bounds.y, - clip_bounds.width, - clip_bounds.height, - ); - - match meshes.0[index].shader { - Shader::Solid(_) => { - self.pipelines.solid.configure_render_pass( - &mut render_pass, - num_solids, - ); - num_solids += 1; - } - Shader::Gradient(_) => { - self.pipelines.gradient.configure_render_pass( - &mut render_pass, - num_gradients, - ); - num_gradients += 1; - } - } - - render_pass.set_index_buffer( - index_buffer.slice_from_index::(index), - wgpu::IndexFormat::Uint32, + )], + depth_stencil_attachment: None, + }); + + //TODO I can't figure out a clean way to encapsulate these into their appropriate + // structs without displeasing the borrow checker due to the lifetime requirements of + // render_pass & using a mutable reference to each pipeline in a loop... + let mut num_solids = 0; + let mut num_gradients = 0; + + for (index, mesh) in meshes.iter().enumerate() { + let clip_bounds = (mesh.clip_bounds * scale_factor).snap(); + + render_pass.set_scissor_rect( + clip_bounds.x, + clip_bounds.y, + clip_bounds.width, + clip_bounds.height, + ); + + match mesh.shader { + Shader::Solid(_) => { + self.pipelines.solid.configure_render_pass( + &mut render_pass, + num_solids, ); - - render_pass.set_vertex_buffer( - 0, - vertex_buffer.slice_from_index::(index), - ); - - render_pass.draw_indexed( - 0..(indices_lengths[index] as u32), - 0, - 0..1, + num_solids += 1; + } + Shader::Gradient(_) => { + self.pipelines.gradient.configure_render_pass( + &mut render_pass, + num_gradients, ); + num_gradients += 1; } - } + }; + + render_pass.set_vertex_buffer( + 0, + self.vertex_buffer.slice_from_index(index), + ); + + render_pass.set_index_buffer( + self.index_buffer.slice_from_index(index), + wgpu::IndexFormat::Uint32, + ); + + render_pass.draw_indexed( + 0..(self.index_strides[index] as u32), + 0, + 0..1, + ); } } if let Some(blit) = &mut self.blit { blit.draw(encoder, target); } - - //cleanup - self.pipelines.clear(); } } diff --git a/wgpu/src/triangle/gradient.rs b/wgpu/src/triangle/gradient.rs index 471b204c..15b6b7e0 100644 --- a/wgpu/src/triangle/gradient.rs +++ b/wgpu/src/triangle/gradient.rs @@ -253,13 +253,13 @@ impl GradientPipeline { pub fn configure_render_pass<'a>( &'a self, render_pass: &mut wgpu::RenderPass<'a>, - index: usize, + count: usize, ) { render_pass.set_pipeline(&self.pipeline); render_pass.set_bind_group( 0, &self.bind_group, - &[self.uniform_buffer.offset_at_index(index)], - ); + &[self.uniform_buffer.offset_at_index(count)], + ) } } diff --git a/wgpu/src/triangle/solid.rs b/wgpu/src/triangle/solid.rs index a3cbd72b..e7e9098a 100644 --- a/wgpu/src/triangle/solid.rs +++ b/wgpu/src/triangle/solid.rs @@ -8,15 +8,15 @@ use encase::ShaderType; use glam::Vec4; use iced_graphics::Transformation; -pub(super) struct SolidPipeline { +pub struct SolidPipeline { pipeline: wgpu::RenderPipeline, - pub(super) buffer: DynamicBuffer, + pub(crate) buffer: DynamicBuffer, bind_group_layout: wgpu::BindGroupLayout, bind_group: wgpu::BindGroup, } #[derive(Debug, Clone, Copy, ShaderType)] -pub(super) struct SolidUniforms { +pub struct SolidUniforms { transform: glam::Mat4, color: Vec4, } @@ -156,14 +156,13 @@ impl SolidPipeline { pub fn configure_render_pass<'a>( &'a self, render_pass: &mut wgpu::RenderPass<'a>, - index: usize, + count: usize, ) { render_pass.set_pipeline(&self.pipeline); - render_pass.set_bind_group( 0, &self.bind_group, - &[self.buffer.offset_at_index(index)], - ); + &[self.buffer.offset_at_index(count)], + ) } -} \ No newline at end of file +} -- cgit From 30432cbade3d9b25c4df62656a7494db3f4ea82a Mon Sep 17 00:00:00 2001 From: shan Date: Wed, 5 Oct 2022 10:49:58 -0700 Subject: Readjusted namespaces, removed Geometry example as it's no longer relevant. --- Cargo.toml | 1 - examples/arc/src/main.rs | 4 +- examples/geometry/Cargo.toml | 11 -- examples/geometry/README.md | 18 --- examples/geometry/src/main.rs | 222 ----------------------------------- examples/modern_art/src/main.rs | 9 +- glow/src/triangle.rs | 10 +- graphics/src/gradient.rs | 75 +----------- graphics/src/gradient/linear.rs | 71 +++++++++++ graphics/src/layer.rs | 111 ++---------------- graphics/src/layer/image.rs | 23 ++++ graphics/src/layer/mesh.rs | 39 ++++++ graphics/src/layer/quad.rs | 30 +++++ graphics/src/layer/text.rs | 26 ++++ graphics/src/lib.rs | 1 - graphics/src/primitive.rs | 5 +- graphics/src/shader.rs | 23 ---- graphics/src/widget/canvas.rs | 8 +- graphics/src/widget/canvas/fill.rs | 16 +-- graphics/src/widget/canvas/frame.rs | 32 +++-- graphics/src/widget/canvas/stroke.rs | 10 +- wgpu/src/buffers.rs | 124 ++++++++++++++++++- wgpu/src/buffers/buffer.rs | 124 ------------------- wgpu/src/buffers/dynamic.rs | 201 +++++++++++++++++++++++++++++++ wgpu/src/buffers/dynamic_buffers.rs | 201 ------------------------------- wgpu/src/triangle.rs | 22 ++-- wgpu/src/triangle/gradient.rs | 2 +- wgpu/src/triangle/solid.rs | 2 +- 28 files changed, 595 insertions(+), 826 deletions(-) delete mode 100644 examples/geometry/Cargo.toml delete mode 100644 examples/geometry/README.md delete mode 100644 examples/geometry/src/main.rs create mode 100644 graphics/src/gradient/linear.rs create mode 100644 graphics/src/layer/image.rs create mode 100644 graphics/src/layer/mesh.rs create mode 100644 graphics/src/layer/quad.rs create mode 100644 graphics/src/layer/text.rs delete mode 100644 graphics/src/shader.rs delete mode 100644 wgpu/src/buffers/buffer.rs create mode 100644 wgpu/src/buffers/dynamic.rs delete mode 100644 wgpu/src/buffers/dynamic_buffers.rs diff --git a/Cargo.toml b/Cargo.toml index d8f5eccb..e4754782 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -69,7 +69,6 @@ members = [ "examples/events", "examples/exit", "examples/game_of_life", - "examples/geometry", "examples/integration_opengl", "examples/integration_wgpu", "examples/modern_art", diff --git a/examples/arc/src/main.rs b/examples/arc/src/main.rs index bc7e49c6..7b6ea0e1 100644 --- a/examples/arc/src/main.rs +++ b/examples/arc/src/main.rs @@ -2,7 +2,7 @@ use std::{f32::consts::PI, time::Instant}; use iced::executor; use iced::widget::canvas::{ - self, Cache, Canvas, Cursor, Geometry, Path, Stroke, Style, + self, stroke, Cache, Canvas, Cursor, Geometry, Path, Stroke, }; use iced::{ Application, Command, Element, Length, Point, Rectangle, Settings, @@ -114,7 +114,7 @@ impl canvas::Program for Arc { frame.stroke( &path, Stroke { - style: Style::Solid(palette.text), + style: stroke::Style::Solid(palette.text), width: 10.0, ..Stroke::default() }, diff --git a/examples/geometry/Cargo.toml b/examples/geometry/Cargo.toml deleted file mode 100644 index 22ede0e0..00000000 --- a/examples/geometry/Cargo.toml +++ /dev/null @@ -1,11 +0,0 @@ -[package] -name = "geometry" -version = "0.1.0" -authors = ["Héctor Ramón Jiménez "] -edition = "2021" -publish = false - -[dependencies] -iced = { path = "../.." } -iced_native = { path = "../../native" } -iced_graphics = { path = "../../graphics" } diff --git a/examples/geometry/README.md b/examples/geometry/README.md deleted file mode 100644 index 4d5c81cb..00000000 --- a/examples/geometry/README.md +++ /dev/null @@ -1,18 +0,0 @@ -## Geometry - -A custom widget showcasing how to draw geometry with the `Mesh2D` primitive in [`iced_wgpu`](../../wgpu). - -The __[`main`]__ file contains all the code of the example. - - - -You can run it with `cargo run`: -``` -cargo run --package geometry -``` - -[`main`]: src/main.rs diff --git a/examples/geometry/src/main.rs b/examples/geometry/src/main.rs deleted file mode 100644 index a8ce26f8..00000000 --- a/examples/geometry/src/main.rs +++ /dev/null @@ -1,222 +0,0 @@ -//! This example showcases a simple native custom widget that renders using -//! arbitrary low-level geometry. -//! -//TODO need to update this now that vertex data doesn't contain color data -mod rainbow { - use iced::Color; - // For now, to implement a custom native widget you will need to add - // `iced_native` and `iced_wgpu` to your dependencies. - // - // Then, you simply need to define your widget type and implement the - // `iced_native::Widget` trait with the `iced_wgpu::Renderer`. - // - // Of course, you can choose to make the implementation renderer-agnostic, - // if you wish to, by creating your own `Renderer` trait, which could be - // implemented by `iced_wgpu` and other renderers. - use iced_graphics::renderer::{self, Renderer}; - use iced_graphics::{Backend, Primitive}; - use iced_graphics::shader::Shader; - - use iced_native::widget::{self, Widget}; - use iced_native::{ - layout, Element, Layout, Length, Point, Rectangle, Size, Vector, - }; - - #[derive(Default)] - pub struct Rainbow; - - impl Rainbow { - pub fn new() -> Self { - Self - } - } - - pub fn rainbow() -> Rainbow { - Rainbow - } - - impl Widget> for Rainbow - where - B: Backend, - { - fn width(&self) -> Length { - Length::Fill - } - - fn height(&self) -> Length { - Length::Shrink - } - - fn layout( - &self, - _renderer: &Renderer, - limits: &layout::Limits, - ) -> layout::Node { - let size = limits.width(Length::Fill).resolve(Size::ZERO); - - layout::Node::new(Size::new(size.width, size.width)) - } - - fn draw( - &self, - _tree: &widget::Tree, - renderer: &mut Renderer, - _theme: &T, - _style: &renderer::Style, - layout: Layout<'_>, - cursor_position: Point, - _viewport: &Rectangle, - ) { - use iced_graphics::triangle::{Mesh2D, Shader, Vertex2D}; - use iced_native::Renderer as _; - - let b = layout.bounds(); - - // R O Y G B I V - // let color_r = [1.0, 0.0, 0.0, 1.0]; - // let color_o = [1.0, 0.5, 0.0, 1.0]; - // let color_y = [1.0, 1.0, 0.0, 1.0]; - // let color_g = [0.0, 1.0, 0.0, 1.0]; - // let color_gb = [0.0, 1.0, 0.5, 1.0]; - // let color_b = [0.0, 0.2, 1.0, 1.0]; - // let color_i = [0.5, 0.0, 1.0, 1.0]; - // let color_v = [0.75, 0.0, 0.5, 1.0]; - - let posn_center = { - if b.contains(cursor_position) { - [cursor_position.x - b.x, cursor_position.y - b.y] - } else { - [b.width / 2.0, b.height / 2.0] - } - }; - - let posn_tl = [0.0, 0.0]; - let posn_t = [b.width / 2.0, 0.0]; - let posn_tr = [b.width, 0.0]; - let posn_r = [b.width, b.height / 2.0]; - let posn_br = [b.width, b.height]; - let posn_b = [(b.width / 2.0), b.height]; - let posn_bl = [0.0, b.height]; - let posn_l = [0.0, b.height / 2.0]; - - let mesh = Primitive::Mesh2D { - size: b.size(), - buffers: Mesh2D { - vertices: vec![ - Vertex2D { - position: posn_center, - // color: [1.0, 1.0, 1.0, 1.0], - }, - Vertex2D { - position: posn_tl, - // color: color_r, - }, - Vertex2D { - position: posn_t, - // color: color_o, - }, - Vertex2D { - position: posn_tr, - // color: color_y, - }, - Vertex2D { - position: posn_r, - // color: color_g, - }, - Vertex2D { - position: posn_br, - // color: color_gb, - }, - Vertex2D { - position: posn_b, - // color: color_b, - }, - Vertex2D { - position: posn_bl, - // color: color_i, - }, - Vertex2D { - position: posn_l, - // color: color_v, - }, - ], - indices: vec![ - 0, 1, 2, // TL - 0, 2, 3, // T - 0, 3, 4, // TR - 0, 4, 5, // R - 0, 5, 6, // BR - 0, 6, 7, // B - 0, 7, 8, // BL - 0, 8, 1, // L - ], - }, - shader: Shader::Solid(Color::BLACK), - }; - - renderer.with_translation(Vector::new(b.x, b.y), |renderer| { - renderer.draw_primitive(mesh); - }); - } - } - - impl<'a, Message, B, T> From for Element<'a, Message, Renderer> - where - B: Backend, - { - fn from(rainbow: Rainbow) -> Self { - Self::new(rainbow) - } - } -} - -use iced::widget::{column, container, scrollable}; -use iced::{Alignment, Element, Length, Sandbox, Settings}; -use rainbow::rainbow; - -pub fn main() -> iced::Result { - Example::run(Settings::default()) -} - -struct Example; - -impl Sandbox for Example { - type Message = (); - - fn new() -> Self { - Example - } - - fn title(&self) -> String { - String::from("Custom 2D geometry - Iced") - } - - fn update(&mut self, _: ()) {} - - fn view(&self) -> Element<()> { - let content = column![ - rainbow(), - "In this example we draw a custom widget Rainbow, using \ - the Mesh2D primitive. This primitive supplies a list of \ - triangles, expressed as vertices and indices.", - "Move your cursor over it, and see the center vertex \ - follow you!", - "Every Vertex2D defines its own color. You could use the \ - Mesh2D primitive to render virtually any two-dimensional \ - geometry for your widget.", - ] - .padding(20) - .spacing(20) - .max_width(500) - .align_items(Alignment::Start); - - let scrollable = - scrollable(container(content).width(Length::Fill).center_x()); - - container(scrollable) - .width(Length::Fill) - .height(Length::Fill) - .center_y() - .into() - } -} diff --git a/examples/modern_art/src/main.rs b/examples/modern_art/src/main.rs index 238c9a0f..14e117b3 100644 --- a/examples/modern_art/src/main.rs +++ b/examples/modern_art/src/main.rs @@ -1,14 +1,13 @@ use rand::{Rng, thread_rng}; use crate::canvas::{Cursor, Geometry}; -use iced::widget::canvas::{Cache, Fill, Frame}; +use iced::widget::canvas::{Cache, Fill, Frame, Gradient}; use iced::widget::{canvas, Canvas}; use iced::Settings; use iced::{ executor, Application, Color, Command, Element, Length, Point, Rectangle, Renderer, Size, Theme, }; -use iced_graphics::gradient::Gradient; -use iced_graphics::widget::canvas::Style; +use iced_graphics::widget::canvas::fill; fn main() -> iced::Result { ModernArt::run(Settings { @@ -121,7 +120,7 @@ fn generate_box(frame: &mut Frame, bounds: Size) -> bool { top_left, size, Fill { - style: Style::Solid(random_color()), + style: fill::Style::Solid(random_color()), .. Default::default() } ); @@ -130,7 +129,7 @@ fn generate_box(frame: &mut Frame, bounds: Size) -> bool { top_left, size, Fill { - style: Style::Gradient(&gradient( + style: fill::Style::Gradient(&gradient( top_left, Point::new(top_left.x + size.width, top_left.y + size.height) )), diff --git a/glow/src/triangle.rs b/glow/src/triangle.rs index f16f8af4..5e58f4e4 100644 --- a/glow/src/triangle.rs +++ b/glow/src/triangle.rs @@ -5,13 +5,13 @@ mod solid; use crate::{program, Transformation}; use glow::HasContext; use iced_graphics::layer::{attribute_count_of, Mesh}; -use iced_graphics::shader; use std::marker::PhantomData; +use iced_graphics::layer; use crate::triangle::gradient::GradientProgram; use crate::triangle::solid::SolidProgram; pub use iced_graphics::triangle::{Mesh2D, Vertex2D}; -use shader::Shader; +use layer::mesh; #[derive(Debug)] pub(crate) struct Pipeline { @@ -139,11 +139,11 @@ impl Pipeline { clip_bounds.height as i32, ); - match mesh.shader { - Shader::Solid(color) => { + match mesh.style { + mesh::Style::Solid(color) => { self.programs.solid.use_program(gl, &color, &transform); } - Shader::Gradient(gradient) => { + mesh::Style::Gradient(gradient) => { self.programs.gradient.use_program(gl, &gradient, &transform); } } diff --git a/graphics/src/gradient.rs b/graphics/src/gradient.rs index 0c394e8b..33453c67 100644 --- a/graphics/src/gradient.rs +++ b/graphics/src/gradient.rs @@ -1,4 +1,6 @@ //! For creating a Gradient. +mod linear; + use iced_native::Color; pub use crate::gradient::linear::Linear; use crate::Point; @@ -28,76 +30,3 @@ impl Gradient { } } -/// Linear gradient builder & definition. -pub mod linear { - use crate::gradient::{ColorStop, Gradient}; - use crate::{Color, Point}; - - /// A linear gradient that can be used in the style of [`super::Fill`] or [`super::Stroke`]. - #[derive(Debug, Clone, PartialEq)] - pub struct Linear { - /// The point where the linear gradient begins. - pub start: Point, - /// The point where the linear gradient ends. - pub end: Point, - /// [`ColorStop`]s along the linear gradient path. - pub color_stops: Vec, - } - - /// A [`Linear`] builder. - #[derive(Debug)] - pub struct Builder { - start: Point, - end: Point, - stops: Vec<(f32, Color)>, - valid: bool, - } - - impl Builder { - /// Creates a new [`Builder`]. - pub fn new(start: Point, end: Point) -> Self { - Self { - start, - end, - stops: vec![], - valid: true, - } - } - - /// Adds a new stop, defined by an offset and a color, to the gradient. - /// - /// `offset` must be between `0.0` and `1.0`. - pub fn add_stop(mut self, offset: f32, color: Color) -> Self { - if !(0.0..=1.0).contains(&offset) { - self.valid = false; - } - - self.stops.push((offset, color)); - self - } - - /// Builds the linear [`Gradient`] of this [`Builder`]. - /// - /// Returns `None` if no stops were added to the builder or - /// if stops not between 0.0 and 1.0 were added. - pub fn build(self) -> Option { - if self.stops.is_empty() || !self.valid { - return None; - } - - let mut stops: Vec = self.stops.clone().into_iter().map(|f| ColorStop { - offset: f.0, - color: f.1 - }).collect(); - - stops.sort_by(|a, b| a.offset.partial_cmp(&b.offset).unwrap()); - - Some(Gradient::Linear(Linear { - start: self.start, - end: self.end, - color_stops: stops - })) - } - } -} - diff --git a/graphics/src/gradient/linear.rs b/graphics/src/gradient/linear.rs new file mode 100644 index 00000000..00f94adc --- /dev/null +++ b/graphics/src/gradient/linear.rs @@ -0,0 +1,71 @@ +//! Linear gradient builder & definition. + +use crate::gradient::{ColorStop, Gradient}; +use crate::{Color, Point}; + +/// A linear gradient that can be used in the style of [`super::Fill`] or [`super::Stroke`]. +#[derive(Debug, Clone, PartialEq)] +pub struct Linear { + /// The point where the linear gradient begins. + pub start: Point, + /// The point where the linear gradient ends. + pub end: Point, + /// [`ColorStop`]s along the linear gradient path. + pub color_stops: Vec, +} + +/// A [`Linear`] builder. +#[derive(Debug)] +pub struct Builder { + start: Point, + end: Point, + stops: Vec<(f32, Color)>, + valid: bool, +} + +impl Builder { + /// Creates a new [`Builder`]. + pub fn new(start: Point, end: Point) -> Self { + Self { + start, + end, + stops: vec![], + valid: true, + } + } + + /// Adds a new stop, defined by an offset and a color, to the gradient. + /// + /// `offset` must be between `0.0` and `1.0`. + pub fn add_stop(mut self, offset: f32, color: Color) -> Self { + if !(0.0..=1.0).contains(&offset) { + self.valid = false; + } + + self.stops.push((offset, color)); + self + } + + /// Builds the linear [`Gradient`] of this [`Builder`]. + /// + /// Returns `None` if no stops were added to the builder or + /// if stops not between 0.0 and 1.0 were added. + pub fn build(self) -> Option { + if self.stops.is_empty() || !self.valid { + return None; + } + + let mut stops: Vec = self.stops.clone().into_iter().map(|f| ColorStop { + offset: f.0, + color: f.1 + }).collect(); + + stops.sort_by(|a, b| a.offset.partial_cmp(&b.offset).unwrap()); + + Some(Gradient::Linear(Linear { + start: self.start, + end: self.end, + color_stops: stops + })) + } +} \ No newline at end of file diff --git a/graphics/src/layer.rs b/graphics/src/layer.rs index 096c50dc..65e70cb3 100644 --- a/graphics/src/layer.rs +++ b/graphics/src/layer.rs @@ -1,13 +1,17 @@ //! Organize rendering primitives into a flattened list of layers. +pub mod mesh; +mod quad; +mod text; +mod image; + use crate::alignment; -use crate::triangle; use crate::{ Background, Font, Point, Primitive, Rectangle, Size, Vector, Viewport, }; - -use iced_native::image; -use iced_native::svg; -use crate::shader::Shader; +pub use crate::layer::image::Image; +pub use crate::layer::mesh::Mesh; +pub use crate::layer::quad::Quad; +pub use crate::layer::text::Text; /// A group of primitives that should be clipped together. #[derive(Debug)] @@ -163,7 +167,7 @@ impl<'a> Layer<'a> { Primitive::Mesh2D { buffers, size, - shader, + style, } => { let layer = &mut layers[current_layer]; @@ -179,7 +183,7 @@ impl<'a> Layer<'a> { origin: Point::new(translation.x, translation.y), buffers, clip_bounds, - shader, + style, } ); } @@ -242,99 +246,6 @@ impl<'a> Layer<'a> { } } -/// A colored rectangle with a border. -/// -/// This type can be directly uploaded to GPU memory. -#[derive(Debug, Clone, Copy)] -#[repr(C)] -pub struct Quad { - /// The position of the [`Quad`]. - pub position: [f32; 2], - - /// The size of the [`Quad`]. - pub size: [f32; 2], - - /// The color of the [`Quad`], in __linear RGB__. - pub color: [f32; 4], - - /// The border color of the [`Quad`], in __linear RGB__. - pub border_color: [f32; 4], - - /// The border radius of the [`Quad`]. - pub border_radius: f32, - - /// The border width of the [`Quad`]. - pub border_width: f32, -} - -/// A mesh of triangles. -#[derive(Debug, Clone, Copy)] -pub struct Mesh<'a> { - /// The origin of the vertices of the [`Mesh`]. - pub origin: Point, - - /// The vertex and index buffers of the [`Mesh`]. - pub buffers: &'a triangle::Mesh2D, - - /// The clipping bounds of the [`Mesh`]. - pub clip_bounds: Rectangle, - - /// The shader of the [`Mesh`]. - pub shader: &'a Shader, -} - -/// A paragraph of text. -#[derive(Debug, Clone, Copy)] -pub struct Text<'a> { - /// The content of the [`Text`]. - pub content: &'a str, - - /// The layout bounds of the [`Text`]. - pub bounds: Rectangle, - - /// The color of the [`Text`], in __linear RGB_. - pub color: [f32; 4], - - /// The size of the [`Text`]. - pub size: f32, - - /// The font of the [`Text`]. - pub font: Font, - - /// The horizontal alignment of the [`Text`]. - pub horizontal_alignment: alignment::Horizontal, - - /// The vertical alignment of the [`Text`]. - pub vertical_alignment: alignment::Vertical, -} - -/// A raster or vector image. -#[derive(Debug, Clone)] -pub enum Image { - /// A raster image. - Raster { - /// The handle of a raster image. - handle: image::Handle, - - /// The bounds of the image. - bounds: Rectangle, - }, - /// A vector image. - Vector { - /// The handle of a vector image. - handle: svg::Handle, - - /// The bounds of the image. - bounds: Rectangle, - }, -} - -#[allow(unsafe_code)] -unsafe impl bytemuck::Zeroable for Quad {} - -#[allow(unsafe_code)] -unsafe impl bytemuck::Pod for Quad {} - /// Returns the number of total vertices & total indices of all [`Mesh`]es. pub fn attribute_count_of<'a>(meshes: &'a [Mesh<'a>]) -> (usize, usize) { meshes diff --git a/graphics/src/layer/image.rs b/graphics/src/layer/image.rs new file mode 100644 index 00000000..387b60ed --- /dev/null +++ b/graphics/src/layer/image.rs @@ -0,0 +1,23 @@ +use iced_native::{image, svg}; +use crate::Rectangle; + +/// A raster or vector image. +#[derive(Debug, Clone)] +pub enum Image { + /// A raster image. + Raster { + /// The handle of a raster image. + handle: image::Handle, + + /// The bounds of the image. + bounds: Rectangle, + }, + /// A vector image. + Vector { + /// The handle of a vector image. + handle: svg::Handle, + + /// The bounds of the image. + bounds: Rectangle, + }, +} \ No newline at end of file diff --git a/graphics/src/layer/mesh.rs b/graphics/src/layer/mesh.rs new file mode 100644 index 00000000..a025675a --- /dev/null +++ b/graphics/src/layer/mesh.rs @@ -0,0 +1,39 @@ +//! A collection of triangle primitives. + +use crate::{Color, Point, Rectangle, triangle}; +use crate::gradient::Gradient; + +/// A mesh of triangles. +#[derive(Debug, Clone, Copy)] +pub struct Mesh<'a> { + /// The origin of the vertices of the [`Mesh`]. + pub origin: Point, + + /// The vertex and index buffers of the [`Mesh`]. + pub buffers: &'a triangle::Mesh2D, + + /// The clipping bounds of the [`Mesh`]. + pub clip_bounds: Rectangle, + + /// The shader of the [`Mesh`]. + pub style: &'a Style, +} + +#[derive(Debug, Clone)] +/// Supported shaders for primitives. +pub enum Style { + /// Fill a primitive with a solid color. + Solid(Color), + /// Fill a primitive with an interpolated color. + Gradient(Gradient) +} + +impl <'a> Into