display_rect/
display_rect.rs1use std::iter::once;
2
3use wgpu::{Color, CommandEncoder, RenderPass, RenderPipeline, TextureView, VertexAttribute};
4
5use wgpu_noboiler::app::{AppCreator, AppData};
6use wgpu_noboiler::buffer::BufferCreator;
7use wgpu_noboiler::render_pass::RenderPassCreator;
8use wgpu_noboiler::render_pipeline::RenderPipelineCreator;
9use wgpu_noboiler::vertex::Vertex;
10
11#[repr(C)]
12#[derive(Copy, Clone, Debug, bytemuck::Pod, bytemuck::Zeroable)]
13struct ColoredVertex {
14 position: [f32; 3],
15 color: [f32; 3],
16}
17
18impl Vertex<2> for ColoredVertex {
19 const ATTRIBS: [VertexAttribute; 2] = wgpu::vertex_attr_array![0 => Float32x3, 1 => Float32x3];
20}
21
22fn main() {
23 AppCreator::new(())
24 .init(init)
25 .render(render)
26 .title("Rect")
27 .resizable(false)
28 .run();
29}
30
31fn render(app_data: &AppData, _: &mut (), mut encoder: CommandEncoder, view: TextureView) {
32 let vertex_buffer = BufferCreator::vertex(&app_data.device)
33 .data(vec![
34 ColoredVertex {
35 position: [-0.5, 0.5, 0.0],
36 color: [0.2, 0.0, 0.3],
37 },
38 ColoredVertex {
39 position: [-0.5, -0.5, 0.0],
40 color: [0.2, 0.0, 0.3],
41 },
42 ColoredVertex {
43 position: [0.5, 0.5, 0.0],
44 color: [0.2, 0.0, 0.3],
45 },
46 ColoredVertex {
47 position: [0.5, -0.5, 0.0],
48 color: [0.2, 0.0, 0.3],
49 },
50 ])
51 .build();
52
53 let indices_buffer = BufferCreator::indices(&app_data.device)
54 .data(vec![0, 1, 2, 2, 1, 3])
55 .build();
56
57 {
58 let mut render_pass: RenderPass = RenderPassCreator::new(&view)
59 .clear_color(Color::BLACK)
60 .build(&mut encoder);
61
62 render_pass.set_pipeline(app_data.render_pipelines.get(0).unwrap());
63
64 render_pass.set_vertex_buffer(0, vertex_buffer.slice());
65 render_pass.set_index_buffer(indices_buffer.slice(), wgpu::IndexFormat::Uint32);
66
67 render_pass.draw_indexed(0..indices_buffer.size(), 0, 0..1);
68 }
69
70 app_data.queue.submit(once(encoder.finish()));
71}
72
73fn init(app_data: &AppData, _state: &mut (), vec: &mut Vec<RenderPipeline>) {
74 let render_pipeline = RenderPipelineCreator::from_shader_file(
75 "examples/shaderBasicColor.wgsl",
76 &app_data.device,
77 &app_data.config,
78 )
79 .add_vertex_buffer(ColoredVertex::descriptor())
80 .build();
81
82 vec.push(render_pipeline);
83}