forked from gfx-rs/wgpu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
149 lines (130 loc) · 5.41 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
extern crate wgpu;
extern crate wgpu_native;
fn main() {
let instance = wgpu::Instance::new();
let adapter = instance.get_adapter(&wgpu::AdapterDescriptor {
power_preference: wgpu::PowerPreference::LowPower,
});
let device = adapter.create_device(&wgpu::DeviceDescriptor {
extensions: wgpu::Extensions {
anisotropic_filtering: false,
},
});
let vs_bytes = include_bytes!("./../data/hello_triangle.vert.spv");
let vs_module = device.create_shader_module(vs_bytes);
let fs_bytes = include_bytes!("./../data/hello_triangle.frag.spv");
let fs_module = device.create_shader_module(fs_bytes);
let vertex_binding_layout = wgpu::BindGroupLayoutBinding {
binding: 0,
visibility: wgpu::ShaderStageFlags::VERTEX,
ty: wgpu::BindingType::UniformBuffer,
};
let bind_group_layout =
device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { bindings: &[vertex_binding_layout] });
let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
bind_group_layouts: &[&bind_group_layout],
});
let size = std::mem::size_of::<[u32; 2]>() as u32 * 3;
let vertex_buffer_descriptor = wgpu::BufferDescriptor {
size,
usage: wgpu_native::BufferUsageFlags::UNIFORM,
};
let vertex_buffer = device.create_buffer(&vertex_buffer_descriptor);
let vertex_binding = wgpu::Binding {
binding: 0,
resource: wgpu::BindingResource::Buffer(wgpu::BufferBinding{
buffer: &vertex_buffer,
offset: 0,
size,
}),
};
let bind_group =
device.create_bind_group(&wgpu::BindGroupDescriptor { layout: &bind_group_layout, bindings: &[vertex_binding] });
let blend_state0 = device.create_blend_state(&wgpu::BlendStateDescriptor::REPLACE);
let depth_stencil_state =
device.create_depth_stencil_state(&wgpu::DepthStencilStateDescriptor::IGNORE);
let render_pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
layout: &pipeline_layout,
stages: &[
wgpu::PipelineStageDescriptor {
module: &vs_module,
stage: wgpu::ShaderStage::Vertex,
entry_point: "main",
},
wgpu::PipelineStageDescriptor {
module: &fs_module,
stage: wgpu::ShaderStage::Fragment,
entry_point: "main",
},
],
primitive_topology: wgpu::PrimitiveTopology::TriangleList,
attachments_state: wgpu::AttachmentsState {
color_attachments: &[wgpu::Attachment {
format: wgpu::TextureFormat::B8g8r8a8Unorm,
samples: 1,
}],
depth_stencil_attachment: None,
},
blend_states: &[&blend_state0],
depth_stencil_state: &depth_stencil_state,
});
#[cfg(feature = "winit")]
{
use wgpu_native::winit::{ControlFlow, Event, ElementState, EventsLoop, KeyboardInput, Window, WindowEvent, VirtualKeyCode};
let mut events_loop = EventsLoop::new();
let window = Window::new(&events_loop).unwrap();
let size = window
.get_inner_size()
.unwrap()
.to_physical(window.get_hidpi_factor());
let surface = instance.create_surface(&window);
let swap_chain = device.create_swap_chain(&surface, &wgpu::SwapChainDescriptor {
usage: wgpu::TextureUsageFlags::OUTPUT_ATTACHMENT | wgpu::TextureUsageFlags::PRESENT,
format: wgpu::TextureFormat::B8g8r8a8Unorm,
width: size.width as u32,
height: size.height as u32,
});
events_loop.run_forever(|event| {
match event {
Event::WindowEvent { event, .. } => match event {
WindowEvent::KeyboardInput {
input: KeyboardInput { virtual_keycode: Some(code), state: ElementState::Pressed, .. },
..
} => match code {
VirtualKeyCode::Escape => {
return ControlFlow::Break
}
_ => {}
}
WindowEvent::CloseRequested => {
return ControlFlow::Break
}
_ => {}
}
_ => {}
}
let (_, view) = swap_chain.get_next_texture();
let mut cmd_buf = device.create_command_buffer(&wgpu::CommandBufferDescriptor { todo: 0 });
{
let mut rpass = cmd_buf.begin_render_pass(&wgpu::RenderPassDescriptor {
color_attachments: &[wgpu::RenderPassColorAttachmentDescriptor {
attachment: &view,
load_op: wgpu::LoadOp::Clear,
store_op: wgpu::StoreOp::Store,
clear_color: wgpu::Color::GREEN,
}],
depth_stencil_attachment: None,
});
rpass.set_pipeline(&render_pipeline);
//rpass.set_bind_group(0, &bind_group);
rpass.draw(0..3, 0..1);
rpass.end_pass();
}
device
.get_queue()
.submit(&[cmd_buf]);
swap_chain.present();
ControlFlow::Continue
});
}
}