Rename Geometry2D to Mesh2D and move it to iced_wgpu

This commit is contained in:
Héctor Ramón Jiménez 2020-01-02 19:25:00 +01:00
parent 0d620b7701
commit 5ca98b113e
12 changed files with 184 additions and 198 deletions

View file

@ -24,13 +24,14 @@
#![deny(unused_results)]
#![deny(unsafe_code)]
#![deny(rust_2018_idioms)]
pub mod triangle;
mod image;
mod primitive;
mod quad;
mod renderer;
mod text;
mod transformation;
mod geometry;
pub(crate) use crate::image::Image;
pub(crate) use quad::Quad;

View file

@ -1,8 +1,11 @@
use iced_native::{
image, svg, Background, Color, Font, HorizontalAlignment, Rectangle,
Vector, VerticalAlignment, Geometry2D,
Vector, VerticalAlignment,
};
use crate::triangle;
use std::sync::Arc;
/// A rendering primitive.
#[derive(Debug, Clone)]
pub enum Primitive {
@ -63,11 +66,10 @@ pub enum Primitive {
/// The content of the clip
content: Box<Primitive>,
},
/// A low-level geometry primitive
Geometry2D {
/// The vertices and indices of the geometry
geometry: Geometry2D,
},
/// A low-level primitive to render a mesh of triangles.
///
/// It can be used to render many kinds of geometry freely.
Mesh2D(Arc<triangle::Mesh2D>),
}
impl Default for Primitive {

View file

@ -1,12 +1,11 @@
use crate::{
geometry, image, quad, text, Image, Primitive, Quad, Transformation,
image, quad, text, triangle, Image, Primitive, Quad, Transformation,
};
use iced_native::{
renderer::{Debugger, Windowed},
Background, Color, Geometry2D, Layout, MouseCursor, Point, Rectangle,
Vector, Widget,
Background, Color, Layout, MouseCursor, Point, Rectangle, Vector, Widget,
};
use std::sync::Arc;
use wgpu::{
Adapter, BackendBit, CommandEncoderDescriptor, Device, DeviceDescriptor,
Extensions, Limits, PowerPreference, Queue, RequestAdapterOptions,
@ -27,7 +26,7 @@ pub struct Renderer {
quad_pipeline: quad::Pipeline,
image_pipeline: crate::image::Pipeline,
text_pipeline: text::Pipeline,
geometry_pipeline: crate::geometry::Pipeline,
triangle_pipeline: crate::triangle::Pipeline,
}
struct Layer<'a> {
@ -35,7 +34,7 @@ struct Layer<'a> {
offset: Vector<u32>,
quads: Vec<Quad>,
images: Vec<Image>,
geometries: Vec<Geometry2D>,
meshes: Vec<Arc<triangle::Mesh2D>>,
text: Vec<wgpu_glyph::Section<'a>>,
}
@ -47,7 +46,7 @@ impl<'a> Layer<'a> {
quads: Vec::new(),
images: Vec::new(),
text: Vec::new(),
geometries: Vec::new(),
meshes: Vec::new(),
}
}
}
@ -70,7 +69,7 @@ impl Renderer {
let text_pipeline = text::Pipeline::new(&mut device);
let quad_pipeline = quad::Pipeline::new(&mut device);
let image_pipeline = crate::image::Pipeline::new(&mut device);
let geometry_pipeline = geometry::Pipeline::new(&mut device);
let triangle_pipeline = triangle::Pipeline::new(&mut device);
Self {
device,
@ -78,7 +77,7 @@ impl Renderer {
quad_pipeline,
image_pipeline,
text_pipeline,
geometry_pipeline,
triangle_pipeline,
}
}
@ -252,8 +251,8 @@ impl Renderer {
scale: [bounds.width, bounds.height],
});
}
Primitive::Geometry2D { geometry } => {
layer.geometries.push(geometry.clone());
Primitive::Mesh2D(mesh) => {
layer.meshes.push(mesh.clone());
}
Primitive::Clip {
bounds,
@ -333,6 +332,24 @@ impl Renderer {
) {
let bounds = layer.bounds * dpi;
if layer.meshes.len() > 0 {
let translated = transformation
* Transformation::translate(
-(layer.offset.x as f32) * dpi,
-(layer.offset.y as f32) * dpi,
);
self.triangle_pipeline.draw(
&mut self.device,
encoder,
target,
translated,
dpi,
&layer.meshes,
bounds,
);
}
if layer.quads.len() > 0 {
self.quad_pipeline.draw(
&mut self.device,
@ -412,24 +429,6 @@ impl Renderer {
},
);
}
if layer.geometries.len() > 0 {
let translated = transformation
* Transformation::translate(
-(layer.offset.x as f32) * dpi,
-(layer.offset.y as f32) * dpi,
);
self.geometry_pipeline.draw(
&mut self.device,
encoder,
target,
translated,
dpi,
&layer.geometries,
bounds,
);
}
}
}

View file

@ -1,9 +1,10 @@
//! Draw meshes of triangles.
use crate::Transformation;
use iced_native::{Geometry2D, Rectangle, Vertex2D};
use std::mem;
use iced_native::Rectangle;
use std::{mem, sync::Arc};
#[derive(Debug)]
pub struct Pipeline {
pub(crate) struct Pipeline {
pipeline: wgpu::RenderPipeline,
constants: wgpu::BindGroup,
constants_buffer: wgpu::Buffer,
@ -44,16 +45,16 @@ impl Pipeline {
bind_group_layouts: &[&constant_layout],
});
let vs = include_bytes!("shader/geom2d.vert.spv");
let vs = include_bytes!("shader/triangle.vert.spv");
let vs_module = device.create_shader_module(
&wgpu::read_spirv(std::io::Cursor::new(&vs[..]))
.expect("Read quad vertex shader as SPIR-V"),
.expect("Read triangle vertex shader as SPIR-V"),
);
let fs = include_bytes!("shader/geom2d.frag.spv");
let fs = include_bytes!("shader/triangle.frag.spv");
let fs_module = device.create_shader_module(
&wgpu::read_spirv(std::io::Cursor::new(&fs[..]))
.expect("Read quad fragment shader as SPIR-V"),
.expect("Read triangle fragment shader as SPIR-V"),
);
let pipeline =
@ -128,7 +129,7 @@ impl Pipeline {
target: &wgpu::TextureView,
transformation: Transformation,
scale: f32,
geometries: &Vec<Geometry2D>,
meshes: &Vec<Arc<Mesh2D>>,
bounds: Rectangle<u32>,
) {
let uniforms = Uniforms {
@ -148,39 +149,39 @@ impl Pipeline {
std::mem::size_of::<Uniforms>() as u64,
);
for geom in geometries {
let mut render_pass =
encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
color_attachments: &[
wgpu::RenderPassColorAttachmentDescriptor {
attachment: target,
resolve_target: None,
load_op: wgpu::LoadOp::Load,
store_op: wgpu::StoreOp::Store,
clear_color: wgpu::Color {
r: 0.0,
g: 0.0,
b: 0.0,
a: 0.0,
},
},
],
depth_stencil_attachment: None,
});
for mesh in meshes {
let vertices_buffer = device
.create_buffer_mapped(
geom.vertices.len(),
mesh.vertices.len(),
wgpu::BufferUsage::VERTEX,
)
.fill_from_slice(&geom.vertices);
.fill_from_slice(&mesh.vertices);
let indices_buffer = device
.create_buffer_mapped(
geom.indices.len(),
mesh.indices.len(),
wgpu::BufferUsage::INDEX,
)
.fill_from_slice(&geom.indices);
let mut render_pass =
encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
color_attachments: &[
wgpu::RenderPassColorAttachmentDescriptor {
attachment: target,
resolve_target: None,
load_op: wgpu::LoadOp::Load,
store_op: wgpu::StoreOp::Store,
clear_color: wgpu::Color {
r: 0.0,
g: 0.0,
b: 0.0,
a: 0.0,
},
},
],
depth_stencil_attachment: None,
});
.fill_from_slice(&mesh.indices);
render_pass.set_pipeline(&self.pipeline);
render_pass.set_bind_group(0, &self.constants, &[]);
@ -193,7 +194,7 @@ impl Pipeline {
bounds.height,
);
render_pass.draw_indexed(0..geom.indices.len() as u32, 0, 0..1);
render_pass.draw_indexed(0..mesh.indices.len() as u32, 0, 0..1);
}
}
}
@ -213,3 +214,26 @@ impl Default for Uniforms {
}
}
}
/// A two-dimensional vertex with some color in __linear__ RGBA.
#[repr(C)]
#[derive(Copy, Clone, Debug)]
pub struct Vertex2D {
/// The vertex position
pub position: [f32; 2],
/// The vertex color in __linear__ RGBA.
pub color: [f32; 4],
}
/// A set of [`Vertex2D`] and indices representing a list of triangles.
///
/// [`Vertex2D`]: struct.Vertex2D.html
#[derive(Clone, Debug)]
pub struct Mesh2D {
/// The vertices of the mesh
pub vertices: Vec<Vertex2D>,
/// 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<u16>,
}