summaryrefslogtreecommitdiffstats
path: root/graphics
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón Jiménez <hector0193@gmail.com>2023-06-29 07:48:03 +0200
committerLibravatar Héctor Ramón Jiménez <hector0193@gmail.com>2023-06-29 07:48:03 +0200
commitfa5650cfd1115e6ccec2ad795cf58fd970d5b43c (patch)
tree90fa0706c8d28547fd4dae1000161082b52b9b27 /graphics
parent2128472c2a8afcb59927712497c4f613612e9dcc (diff)
downloadiced-fa5650cfd1115e6ccec2ad795cf58fd970d5b43c.tar.gz
iced-fa5650cfd1115e6ccec2ad795cf58fd970d5b43c.tar.bz2
iced-fa5650cfd1115e6ccec2ad795cf58fd970d5b43c.zip
Decouple `Mesh` primitives from main `Primitive` type
Diffstat (limited to '')
-rw-r--r--graphics/src/damage.rs3
-rw-r--r--graphics/src/gradient.rs3
-rw-r--r--graphics/src/lib.rs2
-rw-r--r--graphics/src/mesh.rs75
-rw-r--r--graphics/src/primitive.rs69
5 files changed, 80 insertions, 72 deletions
diff --git a/graphics/src/damage.rs b/graphics/src/damage.rs
index 1add6707..be17b935 100644
--- a/graphics/src/damage.rs
+++ b/graphics/src/damage.rs
@@ -43,9 +43,6 @@ impl<T: Damage> Damage for Primitive<T> {
| Self::Image { bounds, .. }
| Self::Svg { bounds, .. } => bounds.expand(1.0),
Self::Clip { bounds, .. } => bounds.expand(1.0),
- Self::SolidMesh { size, .. } | Self::GradientMesh { size, .. } => {
- Rectangle::with_size(*size)
- }
Self::Group { primitives } => primitives
.iter()
.map(Self::bounds)
diff --git a/graphics/src/gradient.rs b/graphics/src/gradient.rs
index 3f5d0509..4db565d8 100644
--- a/graphics/src/gradient.rs
+++ b/graphics/src/gradient.rs
@@ -7,6 +7,7 @@ use crate::color;
use crate::core::gradient::ColorStop;
use crate::core::{self, Color, Point, Rectangle};
+use bytemuck::{Pod, Zeroable};
use half::f16;
use std::cmp::Ordering;
@@ -135,7 +136,7 @@ impl Linear {
}
/// Packed [`Gradient`] data for use in shader code.
-#[derive(Debug, Copy, Clone, PartialEq)]
+#[derive(Debug, Copy, Clone, PartialEq, Zeroable, Pod)]
#[repr(C)]
pub struct Packed {
// 8 colors, each channel = 16 bit float, 2 colors packed into 1 u32
diff --git a/graphics/src/lib.rs b/graphics/src/lib.rs
index 055a9216..cef36003 100644
--- a/graphics/src/lib.rs
+++ b/graphics/src/lib.rs
@@ -31,6 +31,7 @@ pub mod color;
pub mod compositor;
pub mod damage;
pub mod gradient;
+pub mod mesh;
pub mod primitive;
pub mod renderer;
@@ -46,6 +47,7 @@ pub use compositor::Compositor;
pub use damage::Damage;
pub use error::Error;
pub use gradient::Gradient;
+pub use mesh::Mesh;
pub use primitive::Primitive;
pub use renderer::Renderer;
pub use transformation::Transformation;
diff --git a/graphics/src/mesh.rs b/graphics/src/mesh.rs
new file mode 100644
index 00000000..6ca7f79f
--- /dev/null
+++ b/graphics/src/mesh.rs
@@ -0,0 +1,75 @@
+use crate::color;
+use crate::core::{Rectangle, Size};
+use crate::gradient;
+use crate::Damage;
+
+use bytemuck::{Pod, Zeroable};
+
+/// A low-level primitive to render a mesh of triangles.
+#[derive(Debug, Clone, PartialEq)]
+pub enum Mesh {
+ /// A mesh with a solid color.
+ Solid {
+ /// The vertices and indices of the mesh.
+ buffers: Indexed<SolidVertex2D>,
+
+ /// The size of the drawable region of the mesh.
+ ///
+ /// Any geometry that falls out of this region will be clipped.
+ size: Size,
+ },
+ /// A mesh with a gradient.
+ Gradient {
+ /// The vertices and indices of the mesh.
+ buffers: Indexed<GradientVertex2D>,
+
+ /// The size of the drawable region of the mesh.
+ ///
+ /// Any geometry that falls out of this region will be clipped.
+ size: Size,
+ },
+}
+
+impl Damage for Mesh {
+ fn bounds(&self) -> Rectangle {
+ match self {
+ Self::Solid { size, .. } | Self::Gradient { size, .. } => {
+ Rectangle::with_size(*size)
+ }
+ }
+ }
+}
+
+/// A set of [`Vertex2D`] and indices representing a list of triangles.
+#[derive(Clone, Debug, PartialEq, Eq)]
+pub struct Indexed<T> {
+ /// The vertices of the mesh
+ pub vertices: Vec<T>,
+
+ /// 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<u32>,
+}
+
+/// A two-dimensional vertex with a color.
+#[derive(Copy, Clone, Debug, PartialEq, Zeroable, Pod)]
+#[repr(C)]
+pub struct SolidVertex2D {
+ /// The vertex position in 2D space.
+ pub position: [f32; 2],
+
+ /// The color of the vertex in __linear__ RGBA.
+ pub color: color::Packed,
+}
+
+/// A vertex which contains 2D position & packed gradient data.
+#[derive(Copy, Clone, Debug, PartialEq, Zeroable, Pod)]
+#[repr(C)]
+pub struct GradientVertex2D {
+ /// The vertex position in 2D space.
+ pub position: [f32; 2],
+
+ /// The packed vertex data of the gradient.
+ pub gradient: gradient::Packed,
+}
diff --git a/graphics/src/primitive.rs b/graphics/src/primitive.rs
index f8b005ad..7592a410 100644
--- a/graphics/src/primitive.rs
+++ b/graphics/src/primitive.rs
@@ -1,13 +1,10 @@
//! Draw using different graphical primitives.
-use crate::color;
use crate::core::alignment;
use crate::core::image;
use crate::core::svg;
use crate::core::text;
-use crate::core::{Background, Color, Font, Rectangle, Size, Vector};
-use crate::gradient;
+use crate::core::{Background, Color, Font, Rectangle, Vector};
-use bytemuck::{Pod, Zeroable};
use std::sync::Arc;
/// A rendering primitive.
@@ -65,30 +62,6 @@ pub enum Primitive<T> {
/// The bounds of the viewport
bounds: Rectangle,
},
- /// A low-level primitive to render a mesh of triangles with a solid color.
- ///
- /// It can be used to render many kinds of geometry freely.
- SolidMesh {
- /// The vertices and indices of the mesh.
- buffers: Mesh2D<ColoredVertex2D>,
-
- /// The size of the drawable region of the mesh.
- ///
- /// Any geometry that falls out of this region will be clipped.
- size: Size,
- },
- /// A low-level primitive to render a mesh of triangles with a gradient.
- ///
- /// It can be used to render many kinds of geometry freely.
- GradientMesh {
- /// The vertices and indices of the mesh.
- buffers: Mesh2D<GradientVertex2D>,
-
- /// The size of the drawable region of the mesh.
- ///
- /// Any geometry that falls out of this region will be clipped.
- size: Size,
- },
/// A group of primitives
Group {
/// The primitives of the group
@@ -143,43 +116,3 @@ impl<T> Primitive<T> {
}
}
}
-
-/// A set of [`Vertex2D`] and indices representing a list of triangles.
-#[derive(Clone, Debug, PartialEq, Eq)]
-pub struct Mesh2D<T> {
- /// The vertices of the mesh
- pub vertices: Vec<T>,
-
- /// 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<u32>,
-}
-
-/// A two-dimensional vertex with a color.
-#[derive(Copy, Clone, Debug, PartialEq, Zeroable, Pod)]
-#[repr(C)]
-pub struct ColoredVertex2D {
- /// The vertex position in 2D space.
- pub position: [f32; 2],
-
- /// The color of the vertex in __linear__ RGBA.
- pub color: color::Packed,
-}
-
-/// A vertex which contains 2D position & packed gradient data.
-#[derive(Copy, Clone, Debug, PartialEq)]
-#[repr(C)]
-pub struct GradientVertex2D {
- /// The vertex position in 2D space.
- pub position: [f32; 2],
-
- /// The packed vertex data of the gradient.
- pub gradient: gradient::Packed,
-}
-
-#[allow(unsafe_code)]
-unsafe impl Zeroable for GradientVertex2D {}
-
-#[allow(unsafe_code)]
-unsafe impl Pod for GradientVertex2D {}