summaryrefslogtreecommitdiffstats
path: root/examples/integration/src/scene.rs
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón Jiménez <hector0193@gmail.com>2020-02-09 05:32:56 +0100
committerLibravatar Héctor Ramón Jiménez <hector0193@gmail.com>2020-02-09 05:32:56 +0100
commit4d7979aa77e481a5f8ff2f22bc2665912617fc04 (patch)
tree767519bfaf64a8abf3b091d3ff7d66cc44c4d4ad /examples/integration/src/scene.rs
parenta244f9324305a2393da95be76085cba6a29b4b27 (diff)
downloadiced-4d7979aa77e481a5f8ff2f22bc2665912617fc04.tar.gz
iced-4d7979aa77e481a5f8ff2f22bc2665912617fc04.tar.bz2
iced-4d7979aa77e481a5f8ff2f22bc2665912617fc04.zip
Add `integration` example
It showcases how to integrate iced in an existing graphical application.
Diffstat (limited to '')
-rw-r--r--examples/integration/src/scene.rs119
1 files changed, 119 insertions, 0 deletions
diff --git a/examples/integration/src/scene.rs b/examples/integration/src/scene.rs
new file mode 100644
index 00000000..efb1921b
--- /dev/null
+++ b/examples/integration/src/scene.rs
@@ -0,0 +1,119 @@
+use iced_wgpu::wgpu;
+use iced_winit::Color;
+
+pub struct Scene {
+ pub background_color: Color,
+ pipeline: wgpu::RenderPipeline,
+ bind_group: wgpu::BindGroup,
+}
+
+impl Scene {
+ pub fn new(device: &wgpu::Device) -> Scene {
+ let (pipeline, bind_group) = build_pipeline(device);
+
+ Scene {
+ background_color: Color::BLACK,
+ pipeline,
+ bind_group,
+ }
+ }
+
+ pub fn draw(
+ &self,
+ encoder: &mut wgpu::CommandEncoder,
+ target: &wgpu::TextureView,
+ ) {
+ let mut rpass =
+ encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
+ color_attachments: &[
+ wgpu::RenderPassColorAttachmentDescriptor {
+ attachment: target,
+ resolve_target: None,
+ load_op: wgpu::LoadOp::Clear,
+ store_op: wgpu::StoreOp::Store,
+ clear_color: {
+ let [r, g, b, a] =
+ self.background_color.into_linear();
+
+ wgpu::Color {
+ r: r as f64,
+ g: g as f64,
+ b: b as f64,
+ a: a as f64,
+ }
+ },
+ },
+ ],
+ depth_stencil_attachment: None,
+ });
+
+ rpass.set_pipeline(&self.pipeline);
+ rpass.set_bind_group(0, &self.bind_group, &[]);
+ rpass.draw(0..3, 0..1);
+ }
+}
+
+fn build_pipeline(
+ device: &wgpu::Device,
+) -> (wgpu::RenderPipeline, wgpu::BindGroup) {
+ let vs = include_bytes!("shader/vert.spv");
+ let fs = include_bytes!("shader/frag.spv");
+
+ let vs_module = device.create_shader_module(
+ &wgpu::read_spirv(std::io::Cursor::new(&vs[..])).unwrap(),
+ );
+
+ let fs_module = device.create_shader_module(
+ &wgpu::read_spirv(std::io::Cursor::new(&fs[..])).unwrap(),
+ );
+
+ let bind_group_layout =
+ device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
+ bindings: &[],
+ });
+
+ let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
+ layout: &bind_group_layout,
+ bindings: &[],
+ });
+
+ let pipeline_layout =
+ device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
+ bind_group_layouts: &[&bind_group_layout],
+ });
+
+ let pipeline =
+ device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
+ layout: &pipeline_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::Ccw,
+ 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::REPLACE,
+ alpha_blend: wgpu::BlendDescriptor::REPLACE,
+ write_mask: wgpu::ColorWrite::ALL,
+ }],
+ depth_stencil_state: None,
+ index_format: wgpu::IndexFormat::Uint16,
+ vertex_buffers: &[],
+ sample_count: 1,
+ sample_mask: !0,
+ alpha_to_coverage_enabled: false,
+ });
+
+ (pipeline, bind_group)
+}