Added support for gradients as background variants + other optimizations.

This commit is contained in:
Bingus 2023-05-11 09:12:06 -07:00
parent 669f7cc74b
commit 6551a0b2ab
No known key found for this signature in database
GPG key ID: 5F84D2AA40A9F170
41 changed files with 1658 additions and 1489 deletions

View file

@ -1,5 +1,5 @@
//! A collection of triangle primitives.
use crate::core::{Gradient, Point, Rectangle};
use crate::core::{Point, Rectangle};
use crate::graphics::primitive;
/// A mesh of triangles.
@ -22,13 +22,10 @@ pub enum Mesh<'a> {
origin: Point,
/// The vertex and index buffers of the [`Mesh`].
buffers: &'a primitive::Mesh2D<primitive::Vertex2D>,
buffers: &'a primitive::Mesh2D<primitive::GradientVertex2D>,
/// The clipping bounds of the [`Mesh`].
clip_bounds: Rectangle<f32>,
/// The gradient to apply to the [`Mesh`].
gradient: &'a Gradient,
},
}
@ -65,9 +62,15 @@ pub struct AttributeCount {
/// The total amount of solid vertices.
pub solid_vertices: usize,
/// The total amount of solid meshes.
pub solids: usize,
/// The total amount of gradient vertices.
pub gradient_vertices: usize,
/// The total amount of gradient meshes.
pub gradients: usize,
/// The total amount of indices.
pub indices: usize,
}
@ -79,10 +82,12 @@ pub fn attribute_count_of<'a>(meshes: &'a [Mesh<'a>]) -> AttributeCount {
.fold(AttributeCount::default(), |mut count, mesh| {
match mesh {
Mesh::Solid { buffers, .. } => {
count.solids += 1;
count.solid_vertices += buffers.vertices.len();
count.indices += buffers.indices.len();
}
Mesh::Gradient { buffers, .. } => {
count.gradients += 1;
count.gradient_vertices += buffers.vertices.len();
count.indices += buffers.indices.len();
}

View file

@ -1,7 +1,9 @@
/// A colored rectangle with a border.
///
/// This type can be directly uploaded to GPU memory.
#[derive(Debug, Clone, Copy)]
//! A rectangle with certain styled properties.
use bytemuck::{Pod, Zeroable};
/// The properties of a quad.
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
#[repr(C)]
pub struct Quad {
/// The position of the [`Quad`].
@ -10,21 +12,40 @@ pub struct Quad {
/// 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`].
/// The border radii of the [`Quad`].
pub border_radius: [f32; 4],
/// The border width of the [`Quad`].
pub border_width: f32,
}
#[allow(unsafe_code)]
unsafe impl bytemuck::Zeroable for Quad {}
/// A quad filled with a solid color.
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
#[repr(C)]
pub struct Solid {
/// The background color data of the quad.
pub color: [f32; 4],
/// The [`Quad`] data of the [`Solid`].
pub quad: Quad,
}
/// A quad filled with interpolated colors.
#[derive(Clone, Copy, Debug)]
#[repr(C)]
pub struct Gradient {
/// The background gradient data of the quad.
pub gradient: [f32; 44],
/// The [`Quad`] data of the [`Gradient`].
pub quad: Quad,
}
#[allow(unsafe_code)]
unsafe impl bytemuck::Pod for Quad {}
unsafe impl Pod for Gradient {}
#[allow(unsafe_code)]
unsafe impl Zeroable for Gradient {}