Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Artur Sapek committed Jan 1, 2020
1 parent 26de688 commit 8279691
Show file tree
Hide file tree
Showing 13 changed files with 336 additions and 10 deletions.
18 changes: 18 additions & 0 deletions core/src/geometry.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/// Hai
#[repr(C)]
#[derive(Copy, Clone, Debug)]
pub struct Vertex2D {
/// Hai
pub position: [f32; 2],
/// Hai
pub color: [f32; 4],
}

/// Hai
#[derive(Clone, Debug)]
pub struct Geometry2D {
/// Hai
pub vertices: Vec<Vertex2D>,
/// Hai
pub indices: Vec<u16>,
}
2 changes: 2 additions & 0 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ mod length;
mod point;
mod rectangle;
mod vector;
mod geometry;

pub use align::{Align, HorizontalAlignment, VerticalAlignment};
pub use background::Background;
Expand All @@ -32,6 +33,7 @@ pub use length::Length;
pub use point::Point;
pub use rectangle::Rectangle;
pub use vector::Vector;
pub use geometry::{Vertex2D, Geometry2D};

#[cfg(feature = "command")]
mod command;
Expand Down
35 changes: 29 additions & 6 deletions examples/custom_widget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ mod circle {
// if you wish to, by creating your own `Renderer` trait, which could be
// implemented by `iced_wgpu` and other renderers.
use iced_native::{
layout, Background, Color, Element, Hasher, Layout, Length,
MouseCursor, Point, Size, Widget,
layout, Background, Color, Element, Geometry2D, Hasher, Layout, Length,
MouseCursor, Point, Size, Vertex2D, Widget,
};
use iced_wgpu::{Primitive, Renderer};

Expand Down Expand Up @@ -57,11 +57,34 @@ mod circle {
layout: Layout<'_>,
_cursor_position: Point,
) -> (Primitive, MouseCursor) {
let bounds = layout.bounds();

dbg!(bounds);

(
Primitive::Quad {
bounds: layout.bounds(),
background: Background::Color(Color::BLACK),
border_radius: self.radius,
Primitive::Geometry2D {
bounds,
geometry: Geometry2D {
vertices: vec![
Vertex2D {
position: [bounds.x, bounds.y],
color: [1.0, 0.0, 0.0, 1.0],
},
Vertex2D {
position: [bounds.x + bounds.width, bounds.y + bounds.height],
color: [0.0, 1.0, 0.0, 1.0],
},
Vertex2D {
position: [bounds.x, bounds.y + bounds.height],
color: [0.0, 0.0, 1.0, 1.0],
},
Vertex2D {
position: [bounds.x + bounds.width, bounds.y],
color: [0.0, 1.0, 1.0, 1.0],
},
],
indices: vec![0, 1, 2, 0, 1, 3],
},
},
MouseCursor::OutOfBounds,
)
Expand Down
2 changes: 1 addition & 1 deletion native/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ mod user_interface;

pub use iced_core::{
Align, Background, Color, Command, Font, HorizontalAlignment, Length,
Point, Rectangle, Vector, VerticalAlignment,
Point, Rectangle, Vector, VerticalAlignment, Geometry2D, Vertex2D,
};

pub use clipboard::Clipboard;
Expand Down
Empty file added native/src/widget/geometry.rs
Empty file.
224 changes: 224 additions & 0 deletions wgpu/src/geometry.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,224 @@
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>,
) {
println!("draw 2d geom");

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,
);


println!("Drawing indexed geometry2d {:?}", geom.indices.len());

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,
}
}
}
1 change: 1 addition & 0 deletions wgpu/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
9 changes: 8 additions & 1 deletion wgpu/src/primitive.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use iced_native::{
image, svg, Background, Color, Font, HorizontalAlignment, Rectangle,
Vector, VerticalAlignment,
Vector, VerticalAlignment, Geometry2D,
};

/// A rendering primitive.
Expand Down Expand Up @@ -63,6 +63,13 @@ pub enum Primitive {
/// The content of the clip
content: Box<Primitive>,
},
/// A low-level geometry primitive
Geometry2D {
/// The bounds of the geometry
bounds: Rectangle,
/// The vertices of the geometry
geometry: Geometry2D,
},
}

impl Default for Primitive {
Expand Down
Loading

0 comments on commit 8279691

Please sign in to comment.