Decouple Mesh primitives from main Primitive type

This commit is contained in:
Héctor Ramón Jiménez 2023-06-29 07:48:03 +02:00
parent 2128472c2a
commit fa5650cfd1
No known key found for this signature in database
GPG key ID: 140CC052C94F138E
15 changed files with 248 additions and 205 deletions

View file

@ -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)

View file

@ -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

View file

@ -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;

75
graphics/src/mesh.rs Normal file
View file

@ -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,
}

View file

@ -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 {}