summaryrefslogtreecommitdiffstats
path: root/wgpu
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón <hector@hecrj.dev>2024-02-02 14:54:56 +0100
committerLibravatar GitHub <noreply@github.com>2024-02-02 14:54:56 +0100
commitaea172543cb49f1f1e3625f60b49336f59e26c00 (patch)
treee99e7a55873678ac37fd695a0f46c1350b30dd2f /wgpu
parent759f0e922598504705b543185bc7140a652b726a (diff)
parentb3adf3184594c9bf60e0548a0362d30c512f3966 (diff)
downloadiced-aea172543cb49f1f1e3625f60b49336f59e26c00.tar.gz
iced-aea172543cb49f1f1e3625f60b49336f59e26c00.tar.bz2
iced-aea172543cb49f1f1e3625f60b49336f59e26c00.zip
Merge pull request #2120 from iced-rs/transform-primitive
`Transform` primitive
Diffstat (limited to '')
-rw-r--r--wgpu/src/backend.rs12
-rw-r--r--wgpu/src/geometry.rs12
-rw-r--r--wgpu/src/image.rs3
-rw-r--r--wgpu/src/layer.rs89
-rw-r--r--wgpu/src/layer/mesh.rs17
-rw-r--r--wgpu/src/layer/text.rs10
-rw-r--r--wgpu/src/quad.rs6
-rw-r--r--wgpu/src/text.rs33
-rw-r--r--wgpu/src/triangle.rs10
9 files changed, 96 insertions, 96 deletions
diff --git a/wgpu/src/backend.rs b/wgpu/src/backend.rs
index 25134d68..77b6fa83 100644
--- a/wgpu/src/backend.rs
+++ b/wgpu/src/backend.rs
@@ -1,7 +1,7 @@
-use crate::core::{Color, Size};
+use crate::core::{Color, Size, Transformation};
use crate::graphics::backend;
use crate::graphics::color;
-use crate::graphics::{Transformation, Viewport};
+use crate::graphics::Viewport;
use crate::primitive::pipeline;
use crate::primitive::{self, Primitive};
use crate::quad;
@@ -147,8 +147,8 @@ impl Backend {
}
if !layer.meshes.is_empty() {
- let scaled = transformation
- * Transformation::scale(scale_factor, scale_factor);
+ let scaled =
+ transformation * Transformation::scale(scale_factor);
self.triangle_pipeline.prepare(
device,
@@ -161,8 +161,8 @@ impl Backend {
#[cfg(any(feature = "image", feature = "svg"))]
{
if !layer.images.is_empty() {
- let scaled = transformation
- * Transformation::scale(scale_factor, scale_factor);
+ let scaled =
+ transformation * Transformation::scale(scale_factor);
self.image_pipeline.prepare(
device,
diff --git a/wgpu/src/geometry.rs b/wgpu/src/geometry.rs
index a4d4fb1f..8cfcfff0 100644
--- a/wgpu/src/geometry.rs
+++ b/wgpu/src/geometry.rs
@@ -1,6 +1,6 @@
//! Build and draw geometry.
use crate::core::text::LineHeight;
-use crate::core::{Pixels, Point, Rectangle, Size, Vector};
+use crate::core::{Pixels, Point, Rectangle, Size, Transformation, Vector};
use crate::graphics::color;
use crate::graphics::geometry::fill::{self, Fill};
use crate::graphics::geometry::{
@@ -435,7 +435,7 @@ impl Frame {
pub fn clip(&mut self, frame: Frame, at: Point) {
let size = frame.size();
let primitives = frame.into_primitives();
- let translation = Vector::new(at.x, at.y);
+ let transformation = Transformation::translate(at.x, at.y);
let (text, meshes) = primitives
.into_iter()
@@ -443,12 +443,12 @@ impl Frame {
self.primitives.push(Primitive::Group {
primitives: vec![
- Primitive::Translate {
- translation,
+ Primitive::Transform {
+ transformation,
content: Box::new(Primitive::Group { primitives: meshes }),
},
- Primitive::Translate {
- translation,
+ Primitive::Transform {
+ transformation,
content: Box::new(Primitive::Clip {
bounds: Rectangle::with_size(size),
content: Box::new(Primitive::Group {
diff --git a/wgpu/src/image.rs b/wgpu/src/image.rs
index 1e5d3ee0..06c22870 100644
--- a/wgpu/src/image.rs
+++ b/wgpu/src/image.rs
@@ -8,8 +8,7 @@ mod vector;
use atlas::Atlas;
-use crate::core::{Rectangle, Size};
-use crate::graphics::Transformation;
+use crate::core::{Rectangle, Size, Transformation};
use crate::layer;
use crate::Buffer;
diff --git a/wgpu/src/layer.rs b/wgpu/src/layer.rs
index e213c95f..cc767c25 100644
--- a/wgpu/src/layer.rs
+++ b/wgpu/src/layer.rs
@@ -12,7 +12,9 @@ pub use text::Text;
use crate::core;
use crate::core::alignment;
-use crate::core::{Color, Font, Pixels, Point, Rectangle, Size, Vector};
+use crate::core::{
+ Color, Font, Pixels, Point, Rectangle, Size, Transformation, Vector,
+};
use crate::graphics;
use crate::graphics::color;
use crate::graphics::Viewport;
@@ -104,7 +106,7 @@ impl<'a> Layer<'a> {
for primitive in primitives {
Self::process_primitive(
&mut layers,
- Vector::new(0.0, 0.0),
+ Transformation::IDENTITY,
primitive,
0,
);
@@ -115,7 +117,7 @@ impl<'a> Layer<'a> {
fn process_primitive(
layers: &mut Vec<Self>,
- translation: Vector,
+ transformation: Transformation,
primitive: &'a Primitive,
current_layer: usize,
) {
@@ -130,9 +132,10 @@ impl<'a> Layer<'a> {
layer.text.push(Text::Paragraph {
paragraph: paragraph.clone(),
- position: *position + translation,
+ position: *position,
color: *color,
- clip_bounds: *clip_bounds + translation,
+ clip_bounds: *clip_bounds,
+ transformation,
});
}
Primitive::Editor {
@@ -145,9 +148,10 @@ impl<'a> Layer<'a> {
layer.text.push(Text::Editor {
editor: editor.clone(),
- position: *position + translation,
+ position: *position,
color: *color,
- clip_bounds: *clip_bounds + translation,
+ clip_bounds: *clip_bounds,
+ transformation,
});
}
Primitive::Text {
@@ -166,31 +170,24 @@ impl<'a> Layer<'a> {
layer.text.push(Text::Cached(text::Cached {
content,
- bounds: *bounds + translation,
- size: *size,
+ bounds: *bounds + transformation.translation(),
+ size: *size * transformation.scale_factor(),
line_height: *line_height,
color: *color,
font: *font,
horizontal_alignment: *horizontal_alignment,
vertical_alignment: *vertical_alignment,
shaping: *shaping,
- clip_bounds: *clip_bounds + translation,
+ clip_bounds: *clip_bounds * transformation,
}));
}
- graphics::Primitive::RawText(graphics::text::Raw {
- buffer,
- position,
- color,
- clip_bounds,
- }) => {
+ graphics::Primitive::RawText(raw) => {
let layer = &mut layers[current_layer];
- layer.text.push(Text::Raw(graphics::text::Raw {
- buffer: buffer.clone(),
- position: *position + translation,
- color: *color,
- clip_bounds: *clip_bounds + translation,
- }));
+ layer.text.push(Text::Raw {
+ raw: raw.clone(),
+ transformation,
+ });
}
Primitive::Quad {
bounds,
@@ -199,12 +196,10 @@ impl<'a> Layer<'a> {
shadow,
} => {
let layer = &mut layers[current_layer];
+ let bounds = *bounds * transformation;
let quad = Quad {
- position: [
- bounds.x + translation.x,
- bounds.y + translation.y,
- ],
+ position: [bounds.x, bounds.y],
size: [bounds.width, bounds.height],
border_color: color::pack(border.color),
border_radius: border.radius.into(),
@@ -226,7 +221,7 @@ impl<'a> Layer<'a> {
layer.images.push(Image::Raster {
handle: handle.clone(),
filter_method: *filter_method,
- bounds: *bounds + translation,
+ bounds: *bounds * transformation,
});
}
Primitive::Svg {
@@ -239,7 +234,7 @@ impl<'a> Layer<'a> {
layer.images.push(Image::Vector {
handle: handle.clone(),
color: *color,
- bounds: *bounds + translation,
+ bounds: *bounds * transformation,
});
}
Primitive::Group { primitives } => {
@@ -247,7 +242,7 @@ impl<'a> Layer<'a> {
for primitive in primitives {
Self::process_primitive(
layers,
- translation,
+ transformation,
primitive,
current_layer,
);
@@ -255,7 +250,7 @@ impl<'a> Layer<'a> {
}
Primitive::Clip { bounds, content } => {
let layer = &mut layers[current_layer];
- let translated_bounds = *bounds + translation;
+ let translated_bounds = *bounds * transformation;
// Only draw visible content
if let Some(clip_bounds) =
@@ -266,19 +261,19 @@ impl<'a> Layer<'a> {
Self::process_primitive(
layers,
- translation,
+ transformation,
content,
layers.len() - 1,
);
}
}
- Primitive::Translate {
- translation: new_translation,
+ Primitive::Transform {
+ transformation: new_transformation,
content,
} => {
Self::process_primitive(
layers,
- translation + *new_translation,
+ transformation * *new_transformation,
content,
current_layer,
);
@@ -286,7 +281,7 @@ impl<'a> Layer<'a> {
Primitive::Cache { content } => {
Self::process_primitive(
layers,
- translation,
+ transformation,
content,
current_layer,
);
@@ -296,20 +291,15 @@ impl<'a> Layer<'a> {
graphics::Mesh::Solid { buffers, size } => {
let layer = &mut layers[current_layer];
- let bounds = Rectangle::new(
- Point::new(translation.x, translation.y),
- *size,
- );
+ let bounds =
+ Rectangle::with_size(*size) * transformation;
// Only draw visible content
if let Some(clip_bounds) =
layer.bounds.intersection(&bounds)
{
layer.meshes.push(Mesh::Solid {
- origin: Point::new(
- translation.x,
- translation.y,
- ),
+ transformation,
buffers,
clip_bounds,
});
@@ -318,20 +308,15 @@ impl<'a> Layer<'a> {
graphics::Mesh::Gradient { buffers, size } => {
let layer = &mut layers[current_layer];
- let bounds = Rectangle::new(
- Point::new(translation.x, translation.y),
- *size,
- );
+ let bounds =
+ Rectangle::with_size(*size) * transformation;
// Only draw visible content
if let Some(clip_bounds) =
layer.bounds.intersection(&bounds)
{
layer.meshes.push(Mesh::Gradient {
- origin: Point::new(
- translation.x,
- translation.y,
- ),
+ transformation,
buffers,
clip_bounds,
});
@@ -340,7 +325,7 @@ impl<'a> Layer<'a> {
},
primitive::Custom::Pipeline(pipeline) => {
let layer = &mut layers[current_layer];
- let bounds = pipeline.bounds + translation;
+ let bounds = pipeline.bounds * transformation;
if let Some(clip_bounds) =
layer.bounds.intersection(&bounds)
diff --git a/wgpu/src/layer/mesh.rs b/wgpu/src/layer/mesh.rs
index 7c6206cd..5ed7c654 100644
--- a/wgpu/src/layer/mesh.rs
+++ b/wgpu/src/layer/mesh.rs
@@ -1,5 +1,5 @@
//! A collection of triangle primitives.
-use crate::core::{Point, Rectangle};
+use crate::core::{Rectangle, Transformation};
use crate::graphics::mesh;
/// A mesh of triangles.
@@ -7,8 +7,8 @@ use crate::graphics::mesh;
pub enum Mesh<'a> {
/// A mesh of triangles with a solid color.
Solid {
- /// The origin of the vertices of the [`Mesh`].
- origin: Point,
+ /// The [`Transformation`] for the vertices of the [`Mesh`].
+ transformation: Transformation,
/// The vertex and index buffers of the [`Mesh`].
buffers: &'a mesh::Indexed<mesh::SolidVertex2D>,
@@ -18,8 +18,8 @@ pub enum Mesh<'a> {
},
/// A mesh of triangles with a gradient color.
Gradient {
- /// The origin of the vertices of the [`Mesh`].
- origin: Point,
+ /// The [`Transformation`] for the vertices of the [`Mesh`].
+ transformation: Transformation,
/// The vertex and index buffers of the [`Mesh`].
buffers: &'a mesh::Indexed<mesh::GradientVertex2D>,
@@ -31,11 +31,10 @@ pub enum Mesh<'a> {
impl Mesh<'_> {
/// Returns the origin of the [`Mesh`].
- pub fn origin(&self) -> Point {
+ pub fn transformation(&self) -> Transformation {
match self {
- Self::Solid { origin, .. } | Self::Gradient { origin, .. } => {
- *origin
- }
+ Self::Solid { transformation, .. }
+ | Self::Gradient { transformation, .. } => *transformation,
}
}
diff --git a/wgpu/src/layer/text.rs b/wgpu/src/layer/text.rs
index 37ee5247..b3a00130 100644
--- a/wgpu/src/layer/text.rs
+++ b/wgpu/src/layer/text.rs
@@ -1,6 +1,6 @@
use crate::core::alignment;
use crate::core::text;
-use crate::core::{Color, Font, Pixels, Point, Rectangle};
+use crate::core::{Color, Font, Pixels, Point, Rectangle, Transformation};
use crate::graphics;
use crate::graphics::text::editor;
use crate::graphics::text::paragraph;
@@ -15,6 +15,7 @@ pub enum Text<'a> {
position: Point,
color: Color,
clip_bounds: Rectangle,
+ transformation: Transformation,
},
/// An editor.
#[allow(missing_docs)]
@@ -23,11 +24,16 @@ pub enum Text<'a> {
position: Point,
color: Color,
clip_bounds: Rectangle,
+ transformation: Transformation,
},
/// Some cached text.
Cached(Cached<'a>),
/// Some raw text.
- Raw(graphics::text::Raw),
+ #[allow(missing_docs)]
+ Raw {
+ raw: graphics::text::Raw,
+ transformation: Transformation,
+ },
}
#[derive(Debug, Clone)]
diff --git a/wgpu/src/quad.rs b/wgpu/src/quad.rs
index cda1bec9..b932f54f 100644
--- a/wgpu/src/quad.rs
+++ b/wgpu/src/quad.rs
@@ -4,9 +4,9 @@ mod solid;
use gradient::Gradient;
use solid::Solid;
-use crate::core::{Background, Rectangle};
+use crate::core::{Background, Rectangle, Transformation};
+use crate::graphics;
use crate::graphics::color;
-use crate::graphics::{self, Transformation};
use bytemuck::{Pod, Zeroable};
@@ -319,7 +319,7 @@ impl Uniforms {
impl Default for Uniforms {
fn default() -> Self {
Self {
- transform: *Transformation::identity().as_ref(),
+ transform: *Transformation::IDENTITY.as_ref(),
scale: 1.0,
_padding: [0.0; 3],
}
diff --git a/wgpu/src/text.rs b/wgpu/src/text.rs
index dca09cb8..6fa1922d 100644
--- a/wgpu/src/text.rs
+++ b/wgpu/src/text.rs
@@ -1,5 +1,5 @@
use crate::core::alignment;
-use crate::core::{Rectangle, Size};
+use crate::core::{Rectangle, Size, Transformation};
use crate::graphics::color;
use crate::graphics::text::cache::{self, Cache};
use crate::graphics::text::{font_system, to_color, Editor, Paragraph};
@@ -109,7 +109,9 @@ impl Pipeline {
Some(Allocation::Cache(key))
}
- Text::Raw(text) => text.buffer.upgrade().map(Allocation::Raw),
+ Text::Raw { raw, .. } => {
+ raw.buffer.upgrade().map(Allocation::Raw)
+ }
})
.collect();
@@ -124,11 +126,13 @@ impl Pipeline {
vertical_alignment,
color,
clip_bounds,
+ transformation,
) = match section {
Text::Paragraph {
position,
color,
clip_bounds,
+ transformation,
..
} => {
use crate::core::text::Paragraph as _;
@@ -145,12 +149,14 @@ impl Pipeline {
paragraph.vertical_alignment(),
*color,
*clip_bounds,
+ *transformation,
)
}
Text::Editor {
position,
color,
clip_bounds,
+ transformation,
..
} => {
use crate::core::text::Editor as _;
@@ -167,6 +173,7 @@ impl Pipeline {
alignment::Vertical::Top,
*color,
*clip_bounds,
+ *transformation,
)
}
Text::Cached(text) => {
@@ -186,9 +193,13 @@ impl Pipeline {
text.vertical_alignment,
text.color,
text.clip_bounds,
+ Transformation::IDENTITY,
)
}
- Text::Raw(text) => {
+ Text::Raw {
+ raw,
+ transformation,
+ } => {
let Some(Allocation::Raw(buffer)) = allocation else {
return None;
};
@@ -198,18 +209,19 @@ impl Pipeline {
(
buffer.as_ref(),
Rectangle::new(
- text.position,
+ raw.position,
Size::new(width, height),
),
alignment::Horizontal::Left,
alignment::Vertical::Top,
- text.color,
- text.clip_bounds,
+ raw.color,
+ raw.clip_bounds,
+ *transformation,
)
}
};
- let bounds = bounds * scale_factor;
+ let bounds = bounds * transformation * scale_factor;
let left = match horizontal_alignment {
alignment::Horizontal::Left => bounds.x,
@@ -227,14 +239,15 @@ impl Pipeline {
alignment::Vertical::Bottom => bounds.y - bounds.height,
};
- let clip_bounds =
- layer_bounds.intersection(&(clip_bounds * scale_factor))?;
+ let clip_bounds = layer_bounds.intersection(
+ &(clip_bounds * transformation * scale_factor),
+ )?;
Some(glyphon::TextArea {
buffer,
left,
top,
- scale: scale_factor,
+ scale: scale_factor * transformation.scale_factor(),
bounds: glyphon::TextBounds {
left: clip_bounds.x as i32,
top: clip_bounds.y as i32,
diff --git a/wgpu/src/triangle.rs b/wgpu/src/triangle.rs
index 69270a73..2bb6f307 100644
--- a/wgpu/src/triangle.rs
+++ b/wgpu/src/triangle.rs
@@ -1,8 +1,8 @@
//! Draw meshes of triangles.
mod msaa;
-use crate::core::Size;
-use crate::graphics::{Antialiasing, Transformation};
+use crate::core::{Size, Transformation};
+use crate::graphics::Antialiasing;
use crate::layer::mesh::{self, Mesh};
use crate::Buffer;
@@ -98,12 +98,10 @@ impl Layer {
let mut index_offset = 0;
for mesh in meshes {
- let origin = mesh.origin();
let indices = mesh.indices();
- let uniforms = Uniforms::new(
- transformation * Transformation::translate(origin.x, origin.y),
- );
+ let uniforms =
+ Uniforms::new(transformation * mesh.transformation());
index_offset +=
self.index_buffer.write(queue, index_offset, indices);