Decouple Mesh primitives from main Primitive type
This commit is contained in:
parent
2128472c2a
commit
fa5650cfd1
15 changed files with 248 additions and 205 deletions
|
|
@ -6,8 +6,8 @@ use crate::graphics::geometry::{
|
|||
LineCap, LineDash, LineJoin, Path, Stroke, Style, Text,
|
||||
};
|
||||
use crate::graphics::gradient::{self, Gradient};
|
||||
use crate::graphics::primitive;
|
||||
use crate::Primitive;
|
||||
use crate::graphics::mesh::{self, Mesh};
|
||||
use crate::primitive::{self, Primitive};
|
||||
|
||||
use lyon::geom::euclid;
|
||||
use lyon::tessellation;
|
||||
|
|
@ -25,8 +25,8 @@ pub struct Frame {
|
|||
}
|
||||
|
||||
enum Buffer {
|
||||
Solid(tessellation::VertexBuffers<primitive::ColoredVertex2D, u32>),
|
||||
Gradient(tessellation::VertexBuffers<primitive::GradientVertex2D, u32>),
|
||||
Solid(tessellation::VertexBuffers<mesh::SolidVertex2D, u32>),
|
||||
Gradient(tessellation::VertexBuffers<mesh::GradientVertex2D, u32>),
|
||||
}
|
||||
|
||||
struct BufferStack {
|
||||
|
|
@ -464,24 +464,28 @@ impl Frame {
|
|||
match buffer {
|
||||
Buffer::Solid(buffer) => {
|
||||
if !buffer.indices.is_empty() {
|
||||
self.primitives.push(Primitive::SolidMesh {
|
||||
buffers: primitive::Mesh2D {
|
||||
vertices: buffer.vertices,
|
||||
indices: buffer.indices,
|
||||
},
|
||||
size: self.size,
|
||||
})
|
||||
self.primitives.push(Primitive::Custom(
|
||||
primitive::Custom::Mesh(Mesh::Solid {
|
||||
buffers: mesh::Indexed {
|
||||
vertices: buffer.vertices,
|
||||
indices: buffer.indices,
|
||||
},
|
||||
size: self.size,
|
||||
}),
|
||||
))
|
||||
}
|
||||
}
|
||||
Buffer::Gradient(buffer) => {
|
||||
if !buffer.indices.is_empty() {
|
||||
self.primitives.push(Primitive::GradientMesh {
|
||||
buffers: primitive::Mesh2D {
|
||||
vertices: buffer.vertices,
|
||||
indices: buffer.indices,
|
||||
},
|
||||
size: self.size,
|
||||
})
|
||||
self.primitives.push(Primitive::Custom(
|
||||
primitive::Custom::Mesh(Mesh::Gradient {
|
||||
buffers: mesh::Indexed {
|
||||
vertices: buffer.vertices,
|
||||
indices: buffer.indices,
|
||||
},
|
||||
size: self.size,
|
||||
}),
|
||||
))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -495,32 +499,32 @@ struct GradientVertex2DBuilder {
|
|||
gradient: gradient::Packed,
|
||||
}
|
||||
|
||||
impl tessellation::FillVertexConstructor<primitive::GradientVertex2D>
|
||||
impl tessellation::FillVertexConstructor<mesh::GradientVertex2D>
|
||||
for GradientVertex2DBuilder
|
||||
{
|
||||
fn new_vertex(
|
||||
&mut self,
|
||||
vertex: tessellation::FillVertex<'_>,
|
||||
) -> primitive::GradientVertex2D {
|
||||
) -> mesh::GradientVertex2D {
|
||||
let position = vertex.position();
|
||||
|
||||
primitive::GradientVertex2D {
|
||||
mesh::GradientVertex2D {
|
||||
position: [position.x, position.y],
|
||||
gradient: self.gradient,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl tessellation::StrokeVertexConstructor<primitive::GradientVertex2D>
|
||||
impl tessellation::StrokeVertexConstructor<mesh::GradientVertex2D>
|
||||
for GradientVertex2DBuilder
|
||||
{
|
||||
fn new_vertex(
|
||||
&mut self,
|
||||
vertex: tessellation::StrokeVertex<'_, '_>,
|
||||
) -> primitive::GradientVertex2D {
|
||||
) -> mesh::GradientVertex2D {
|
||||
let position = vertex.position();
|
||||
|
||||
primitive::GradientVertex2D {
|
||||
mesh::GradientVertex2D {
|
||||
position: [position.x, position.y],
|
||||
gradient: self.gradient,
|
||||
}
|
||||
|
|
@ -529,32 +533,32 @@ impl tessellation::StrokeVertexConstructor<primitive::GradientVertex2D>
|
|||
|
||||
struct TriangleVertex2DBuilder(color::Packed);
|
||||
|
||||
impl tessellation::FillVertexConstructor<primitive::ColoredVertex2D>
|
||||
impl tessellation::FillVertexConstructor<mesh::SolidVertex2D>
|
||||
for TriangleVertex2DBuilder
|
||||
{
|
||||
fn new_vertex(
|
||||
&mut self,
|
||||
vertex: tessellation::FillVertex<'_>,
|
||||
) -> primitive::ColoredVertex2D {
|
||||
) -> mesh::SolidVertex2D {
|
||||
let position = vertex.position();
|
||||
|
||||
primitive::ColoredVertex2D {
|
||||
mesh::SolidVertex2D {
|
||||
position: [position.x, position.y],
|
||||
color: self.0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl tessellation::StrokeVertexConstructor<primitive::ColoredVertex2D>
|
||||
impl tessellation::StrokeVertexConstructor<mesh::SolidVertex2D>
|
||||
for TriangleVertex2DBuilder
|
||||
{
|
||||
fn new_vertex(
|
||||
&mut self,
|
||||
vertex: tessellation::StrokeVertex<'_, '_>,
|
||||
) -> primitive::ColoredVertex2D {
|
||||
) -> mesh::SolidVertex2D {
|
||||
let position = vertex.position();
|
||||
|
||||
primitive::ColoredVertex2D {
|
||||
mesh::SolidVertex2D {
|
||||
position: [position.x, position.y],
|
||||
color: self.0,
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,10 +11,11 @@ pub use text::Text;
|
|||
use crate::core;
|
||||
use crate::core::alignment;
|
||||
use crate::core::{Color, Font, Point, Rectangle, Size, Vector};
|
||||
use crate::graphics;
|
||||
use crate::graphics::color;
|
||||
use crate::graphics::Viewport;
|
||||
use crate::primitive::{self, Primitive};
|
||||
use crate::quad::{self, Quad};
|
||||
use crate::Primitive;
|
||||
|
||||
/// A group of primitives that should be clipped together.
|
||||
#[derive(Debug)]
|
||||
|
|
@ -180,40 +181,6 @@ impl<'a> Layer<'a> {
|
|||
bounds: *bounds + translation,
|
||||
});
|
||||
}
|
||||
Primitive::SolidMesh { buffers, size } => {
|
||||
let layer = &mut layers[current_layer];
|
||||
|
||||
let bounds = Rectangle::new(
|
||||
Point::new(translation.x, translation.y),
|
||||
*size,
|
||||
);
|
||||
|
||||
// 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),
|
||||
buffers,
|
||||
clip_bounds,
|
||||
});
|
||||
}
|
||||
}
|
||||
Primitive::GradientMesh { buffers, size } => {
|
||||
let layer = &mut layers[current_layer];
|
||||
|
||||
let bounds = Rectangle::new(
|
||||
Point::new(translation.x, translation.y),
|
||||
*size,
|
||||
);
|
||||
|
||||
// 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),
|
||||
buffers,
|
||||
clip_bounds,
|
||||
});
|
||||
}
|
||||
}
|
||||
Primitive::Group { primitives } => {
|
||||
// TODO: Inspect a bit and regroup (?)
|
||||
for primitive in primitives {
|
||||
|
|
@ -263,7 +230,54 @@ impl<'a> Layer<'a> {
|
|||
current_layer,
|
||||
);
|
||||
}
|
||||
Primitive::Custom(()) => {}
|
||||
Primitive::Custom(custom) => match custom {
|
||||
primitive::Custom::Mesh(mesh) => match mesh {
|
||||
graphics::Mesh::Solid { buffers, size } => {
|
||||
let layer = &mut layers[current_layer];
|
||||
|
||||
let bounds = Rectangle::new(
|
||||
Point::new(translation.x, translation.y),
|
||||
*size,
|
||||
);
|
||||
|
||||
// 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,
|
||||
),
|
||||
buffers,
|
||||
clip_bounds,
|
||||
});
|
||||
}
|
||||
}
|
||||
graphics::Mesh::Gradient { buffers, size } => {
|
||||
let layer = &mut layers[current_layer];
|
||||
|
||||
let bounds = Rectangle::new(
|
||||
Point::new(translation.x, translation.y),
|
||||
*size,
|
||||
);
|
||||
|
||||
// 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,
|
||||
),
|
||||
buffers,
|
||||
clip_bounds,
|
||||
});
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
//! A collection of triangle primitives.
|
||||
use crate::core::{Point, Rectangle};
|
||||
use crate::graphics::primitive;
|
||||
use crate::graphics::mesh;
|
||||
|
||||
/// A mesh of triangles.
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
|
|
@ -11,7 +11,7 @@ pub enum Mesh<'a> {
|
|||
origin: Point,
|
||||
|
||||
/// The vertex and index buffers of the [`Mesh`].
|
||||
buffers: &'a primitive::Mesh2D<primitive::ColoredVertex2D>,
|
||||
buffers: &'a mesh::Indexed<mesh::SolidVertex2D>,
|
||||
|
||||
/// The clipping bounds of the [`Mesh`].
|
||||
clip_bounds: Rectangle<f32>,
|
||||
|
|
@ -22,7 +22,7 @@ pub enum Mesh<'a> {
|
|||
origin: Point,
|
||||
|
||||
/// The vertex and index buffers of the [`Mesh`].
|
||||
buffers: &'a primitive::Mesh2D<primitive::GradientVertex2D>,
|
||||
buffers: &'a mesh::Indexed<mesh::GradientVertex2D>,
|
||||
|
||||
/// The clipping bounds of the [`Mesh`].
|
||||
clip_bounds: Rectangle<f32>,
|
||||
|
|
|
|||
|
|
@ -38,6 +38,7 @@
|
|||
#![allow(clippy::inherent_to_string, clippy::type_complexity)]
|
||||
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
|
||||
pub mod layer;
|
||||
pub mod primitive;
|
||||
pub mod settings;
|
||||
pub mod window;
|
||||
|
||||
|
|
@ -47,7 +48,6 @@ pub mod geometry;
|
|||
mod backend;
|
||||
mod buffer;
|
||||
mod color;
|
||||
mod primitive;
|
||||
mod quad;
|
||||
mod text;
|
||||
mod triangle;
|
||||
|
|
|
|||
|
|
@ -1,3 +1,17 @@
|
|||
use crate::core::Rectangle;
|
||||
use crate::graphics::{Damage, Mesh};
|
||||
|
||||
pub type Primitive = crate::graphics::Primitive<Custom>;
|
||||
|
||||
pub type Custom = ();
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub enum Custom {
|
||||
Mesh(Mesh),
|
||||
}
|
||||
|
||||
impl Damage for Custom {
|
||||
fn bounds(&self) -> Rectangle {
|
||||
match self {
|
||||
Self::Mesh(mesh) => mesh.bounds(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -393,7 +393,7 @@ impl Uniforms {
|
|||
}
|
||||
|
||||
mod solid {
|
||||
use crate::graphics::primitive;
|
||||
use crate::graphics::mesh;
|
||||
use crate::graphics::Antialiasing;
|
||||
use crate::triangle;
|
||||
use crate::Buffer;
|
||||
|
|
@ -406,7 +406,7 @@ mod solid {
|
|||
|
||||
#[derive(Debug)]
|
||||
pub struct Layer {
|
||||
pub vertices: Buffer<primitive::ColoredVertex2D>,
|
||||
pub vertices: Buffer<mesh::SolidVertex2D>,
|
||||
pub uniforms: Buffer<triangle::Uniforms>,
|
||||
pub constants: wgpu::BindGroup,
|
||||
}
|
||||
|
|
@ -493,38 +493,40 @@ mod solid {
|
|||
),
|
||||
});
|
||||
|
||||
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: "solid_vs_main",
|
||||
buffers: &[wgpu::VertexBufferLayout {
|
||||
array_stride: std::mem::size_of::<
|
||||
primitive::ColoredVertex2D,
|
||||
>()
|
||||
as u64,
|
||||
step_mode: wgpu::VertexStepMode::Vertex,
|
||||
attributes: &wgpu::vertex_attr_array!(
|
||||
// Position
|
||||
0 => Float32x2,
|
||||
// Color
|
||||
1 => Float32x4,
|
||||
),
|
||||
}],
|
||||
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: "solid_vs_main",
|
||||
buffers: &[wgpu::VertexBufferLayout {
|
||||
array_stride: std::mem::size_of::<
|
||||
mesh::SolidVertex2D,
|
||||
>(
|
||||
)
|
||||
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: "solid_fs_main",
|
||||
targets: &[triangle::fragment_target(format)],
|
||||
}),
|
||||
primitive: triangle::primitive_state(),
|
||||
depth_stencil: None,
|
||||
multisample: triangle::multisample_state(antialiasing),
|
||||
multiview: None,
|
||||
},
|
||||
fragment: Some(wgpu::FragmentState {
|
||||
module: &shader,
|
||||
entry_point: "solid_fs_main",
|
||||
targets: &[triangle::fragment_target(format)],
|
||||
}),
|
||||
primitive: triangle::primitive_state(),
|
||||
depth_stencil: None,
|
||||
multisample: triangle::multisample_state(antialiasing),
|
||||
multiview: None,
|
||||
},
|
||||
);
|
||||
);
|
||||
|
||||
Self {
|
||||
pipeline,
|
||||
|
|
@ -535,7 +537,8 @@ mod solid {
|
|||
}
|
||||
|
||||
mod gradient {
|
||||
use crate::graphics::{primitive, Antialiasing};
|
||||
use crate::graphics::mesh;
|
||||
use crate::graphics::Antialiasing;
|
||||
use crate::triangle;
|
||||
use crate::Buffer;
|
||||
|
||||
|
|
@ -547,7 +550,7 @@ mod gradient {
|
|||
|
||||
#[derive(Debug)]
|
||||
pub struct Layer {
|
||||
pub vertices: Buffer<primitive::GradientVertex2D>,
|
||||
pub vertices: Buffer<mesh::GradientVertex2D>,
|
||||
pub uniforms: Buffer<triangle::Uniforms>,
|
||||
pub constants: wgpu::BindGroup,
|
||||
}
|
||||
|
|
@ -645,7 +648,7 @@ mod gradient {
|
|||
entry_point: "gradient_vs_main",
|
||||
buffers: &[wgpu::VertexBufferLayout {
|
||||
array_stride: std::mem::size_of::<
|
||||
primitive::GradientVertex2D,
|
||||
mesh::GradientVertex2D,
|
||||
>()
|
||||
as u64,
|
||||
step_mode: wgpu::VertexStepMode::Vertex,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue