Implement Geometry2D primitive
This commit is contained in:
parent
26de688e68
commit
0d620b7701
13 changed files with 528 additions and 4 deletions
215
wgpu/src/geometry.rs
Normal file
215
wgpu/src/geometry.rs
Normal file
|
|
@ -0,0 +1,215 @@
|
|||
use crate::Transformation;
|
||||
use iced_native::{Geometry2D, Rectangle, Vertex2D};
|
||||
use std::mem;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Pipeline {
|
||||
pipeline: wgpu::RenderPipeline,
|
||||
constants: wgpu::BindGroup,
|
||||
constants_buffer: wgpu::Buffer,
|
||||
}
|
||||
|
||||
impl Pipeline {
|
||||
pub fn new(device: &mut wgpu::Device) -> Pipeline {
|
||||
let constant_layout =
|
||||
device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
|
||||
bindings: &[wgpu::BindGroupLayoutBinding {
|
||||
binding: 0,
|
||||
visibility: wgpu::ShaderStage::VERTEX,
|
||||
ty: wgpu::BindingType::UniformBuffer { dynamic: false },
|
||||
}],
|
||||
});
|
||||
|
||||
let constants_buffer = device
|
||||
.create_buffer_mapped(
|
||||
1,
|
||||
wgpu::BufferUsage::UNIFORM | wgpu::BufferUsage::COPY_DST,
|
||||
)
|
||||
.fill_from_slice(&[Uniforms::default()]);
|
||||
|
||||
let constant_bind_group =
|
||||
device.create_bind_group(&wgpu::BindGroupDescriptor {
|
||||
layout: &constant_layout,
|
||||
bindings: &[wgpu::Binding {
|
||||
binding: 0,
|
||||
resource: wgpu::BindingResource::Buffer {
|
||||
buffer: &constants_buffer,
|
||||
range: 0..std::mem::size_of::<Uniforms>() as u64,
|
||||
},
|
||||
}],
|
||||
});
|
||||
|
||||
let layout =
|
||||
device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
|
||||
bind_group_layouts: &[&constant_layout],
|
||||
});
|
||||
|
||||
let vs = include_bytes!("shader/geom2d.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"),
|
||||
);
|
||||
|
||||
let fs = include_bytes!("shader/geom2d.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"),
|
||||
);
|
||||
|
||||
let pipeline =
|
||||
device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
|
||||
layout: &layout,
|
||||
vertex_stage: wgpu::ProgrammableStageDescriptor {
|
||||
module: &vs_module,
|
||||
entry_point: "main",
|
||||
},
|
||||
fragment_stage: Some(wgpu::ProgrammableStageDescriptor {
|
||||
module: &fs_module,
|
||||
entry_point: "main",
|
||||
}),
|
||||
rasterization_state: Some(wgpu::RasterizationStateDescriptor {
|
||||
front_face: wgpu::FrontFace::Cw,
|
||||
cull_mode: wgpu::CullMode::None,
|
||||
depth_bias: 0,
|
||||
depth_bias_slope_scale: 0.0,
|
||||
depth_bias_clamp: 0.0,
|
||||
}),
|
||||
primitive_topology: wgpu::PrimitiveTopology::TriangleList,
|
||||
color_states: &[wgpu::ColorStateDescriptor {
|
||||
format: wgpu::TextureFormat::Bgra8UnormSrgb,
|
||||
color_blend: wgpu::BlendDescriptor {
|
||||
src_factor: wgpu::BlendFactor::SrcAlpha,
|
||||
dst_factor: wgpu::BlendFactor::OneMinusSrcAlpha,
|
||||
operation: wgpu::BlendOperation::Add,
|
||||
},
|
||||
alpha_blend: wgpu::BlendDescriptor {
|
||||
src_factor: wgpu::BlendFactor::One,
|
||||
dst_factor: wgpu::BlendFactor::OneMinusSrcAlpha,
|
||||
operation: wgpu::BlendOperation::Add,
|
||||
},
|
||||
write_mask: wgpu::ColorWrite::ALL,
|
||||
}],
|
||||
depth_stencil_state: None,
|
||||
index_format: wgpu::IndexFormat::Uint16,
|
||||
vertex_buffers: &[wgpu::VertexBufferDescriptor {
|
||||
stride: mem::size_of::<Vertex2D>() as u64,
|
||||
step_mode: wgpu::InputStepMode::Vertex,
|
||||
attributes: &[
|
||||
// Position
|
||||
wgpu::VertexAttributeDescriptor {
|
||||
shader_location: 0,
|
||||
format: wgpu::VertexFormat::Float2,
|
||||
offset: 0,
|
||||
},
|
||||
// Color
|
||||
wgpu::VertexAttributeDescriptor {
|
||||
shader_location: 1,
|
||||
format: wgpu::VertexFormat::Float4,
|
||||
offset: 4 * 2,
|
||||
},
|
||||
],
|
||||
}],
|
||||
sample_count: 1,
|
||||
sample_mask: !0,
|
||||
alpha_to_coverage_enabled: false,
|
||||
});
|
||||
|
||||
Pipeline {
|
||||
pipeline,
|
||||
constants: constant_bind_group,
|
||||
constants_buffer,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn draw(
|
||||
&mut self,
|
||||
device: &mut wgpu::Device,
|
||||
encoder: &mut wgpu::CommandEncoder,
|
||||
target: &wgpu::TextureView,
|
||||
transformation: Transformation,
|
||||
scale: f32,
|
||||
geometries: &Vec<Geometry2D>,
|
||||
bounds: Rectangle<u32>,
|
||||
) {
|
||||
let uniforms = Uniforms {
|
||||
transform: transformation.into(),
|
||||
scale,
|
||||
};
|
||||
|
||||
let constants_buffer = device
|
||||
.create_buffer_mapped(1, wgpu::BufferUsage::COPY_SRC)
|
||||
.fill_from_slice(&[uniforms]);
|
||||
|
||||
encoder.copy_buffer_to_buffer(
|
||||
&constants_buffer,
|
||||
0,
|
||||
&self.constants_buffer,
|
||||
0,
|
||||
std::mem::size_of::<Uniforms>() as u64,
|
||||
);
|
||||
|
||||
for geom in geometries {
|
||||
let vertices_buffer = device
|
||||
.create_buffer_mapped(
|
||||
geom.vertices.len(),
|
||||
wgpu::BufferUsage::VERTEX,
|
||||
)
|
||||
.fill_from_slice(&geom.vertices);
|
||||
|
||||
let indices_buffer = device
|
||||
.create_buffer_mapped(
|
||||
geom.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,
|
||||
});
|
||||
|
||||
render_pass.set_pipeline(&self.pipeline);
|
||||
render_pass.set_bind_group(0, &self.constants, &[]);
|
||||
render_pass.set_index_buffer(&indices_buffer, 0);
|
||||
render_pass.set_vertex_buffers(0, &[(&vertices_buffer, 0)]);
|
||||
render_pass.set_scissor_rect(
|
||||
bounds.x,
|
||||
bounds.y,
|
||||
bounds.width,
|
||||
bounds.height,
|
||||
);
|
||||
|
||||
render_pass.draw_indexed(0..geom.indices.len() as u32, 0, 0..1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
struct Uniforms {
|
||||
transform: [f32; 16],
|
||||
scale: f32,
|
||||
}
|
||||
|
||||
impl Default for Uniforms {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
transform: *Transformation::identity().as_ref(),
|
||||
scale: 1.0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -30,6 +30,7 @@ mod quad;
|
|||
mod renderer;
|
||||
mod text;
|
||||
mod transformation;
|
||||
mod geometry;
|
||||
|
||||
pub(crate) use crate::image::Image;
|
||||
pub(crate) use quad::Quad;
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
use iced_native::{
|
||||
image, svg, Background, Color, Font, HorizontalAlignment, Rectangle,
|
||||
Vector, VerticalAlignment,
|
||||
Vector, VerticalAlignment, Geometry2D,
|
||||
};
|
||||
|
||||
/// A rendering primitive.
|
||||
|
|
@ -63,6 +63,11 @@ 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,
|
||||
},
|
||||
}
|
||||
|
||||
impl Default for Primitive {
|
||||
|
|
|
|||
|
|
@ -1,7 +1,10 @@
|
|||
use crate::{image, quad, text, Image, Primitive, Quad, Transformation};
|
||||
use crate::{
|
||||
geometry, image, quad, text, Image, Primitive, Quad, Transformation,
|
||||
};
|
||||
use iced_native::{
|
||||
renderer::{Debugger, Windowed},
|
||||
Background, Color, Layout, MouseCursor, Point, Rectangle, Vector, Widget,
|
||||
Background, Color, Geometry2D, Layout, MouseCursor, Point, Rectangle,
|
||||
Vector, Widget,
|
||||
};
|
||||
|
||||
use wgpu::{
|
||||
|
|
@ -24,6 +27,7 @@ pub struct Renderer {
|
|||
quad_pipeline: quad::Pipeline,
|
||||
image_pipeline: crate::image::Pipeline,
|
||||
text_pipeline: text::Pipeline,
|
||||
geometry_pipeline: crate::geometry::Pipeline,
|
||||
}
|
||||
|
||||
struct Layer<'a> {
|
||||
|
|
@ -31,6 +35,7 @@ struct Layer<'a> {
|
|||
offset: Vector<u32>,
|
||||
quads: Vec<Quad>,
|
||||
images: Vec<Image>,
|
||||
geometries: Vec<Geometry2D>,
|
||||
text: Vec<wgpu_glyph::Section<'a>>,
|
||||
}
|
||||
|
||||
|
|
@ -42,6 +47,7 @@ impl<'a> Layer<'a> {
|
|||
quads: Vec::new(),
|
||||
images: Vec::new(),
|
||||
text: Vec::new(),
|
||||
geometries: Vec::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -64,6 +70,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);
|
||||
|
||||
Self {
|
||||
device,
|
||||
|
|
@ -71,6 +78,7 @@ impl Renderer {
|
|||
quad_pipeline,
|
||||
image_pipeline,
|
||||
text_pipeline,
|
||||
geometry_pipeline,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -244,6 +252,9 @@ impl Renderer {
|
|||
scale: [bounds.width, bounds.height],
|
||||
});
|
||||
}
|
||||
Primitive::Geometry2D { geometry } => {
|
||||
layer.geometries.push(geometry.clone());
|
||||
}
|
||||
Primitive::Clip {
|
||||
bounds,
|
||||
offset,
|
||||
|
|
@ -401,6 +412,24 @@ 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,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
8
wgpu/src/shader/geom2d.frag
Normal file
8
wgpu/src/shader/geom2d.frag
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
#version 450
|
||||
|
||||
layout(location = 0) in vec4 i_Color;
|
||||
layout(location = 0) out vec4 o_Color;
|
||||
|
||||
void main() {
|
||||
o_Color = i_Color;
|
||||
}
|
||||
BIN
wgpu/src/shader/geom2d.frag.spv
Normal file
BIN
wgpu/src/shader/geom2d.frag.spv
Normal file
Binary file not shown.
17
wgpu/src/shader/geom2d.vert
Normal file
17
wgpu/src/shader/geom2d.vert
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
#version 450
|
||||
|
||||
layout(location = 0) in vec2 i_Position;
|
||||
layout(location = 1) in vec4 i_Color;
|
||||
|
||||
layout(location = 0) out vec4 o_Color;
|
||||
|
||||
layout (set = 0, binding = 0) uniform Globals {
|
||||
mat4 u_Transform;
|
||||
float u_Scale;
|
||||
};
|
||||
|
||||
void main() {
|
||||
vec2 p_Position = i_Position * u_Scale;
|
||||
gl_Position = u_Transform * vec4(p_Position, 0.0, 1.0);
|
||||
o_Color = i_Color;
|
||||
}
|
||||
BIN
wgpu/src/shader/geom2d.vert.spv
Normal file
BIN
wgpu/src/shader/geom2d.vert.spv
Normal file
Binary file not shown.
Loading…
Add table
Add a link
Reference in a new issue