-
Notifications
You must be signed in to change notification settings - Fork 473
/
main.rs
217 lines (199 loc) · 7.12 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/// Minimal example for getting sdl2 and wgpu working together with raw-window-handle.
extern crate pollster;
extern crate sdl2;
extern crate wgpu;
use std::borrow::Cow;
use std::collections::HashMap;
use wgpu::SurfaceError;
use sdl2::event::{Event, WindowEvent};
use sdl2::keyboard::Keycode;
fn main() -> Result<(), String> {
// Show logs from wgpu
env_logger::init();
let sdl_context = sdl2::init()?;
let video_subsystem = sdl_context.video()?;
let window = video_subsystem
.window("Raw Window Handle Example", 800, 600)
.position_centered()
.resizable()
.metal_view()
.build()
.map_err(|e| e.to_string())?;
let (width, height) = window.size();
let instance = wgpu::Instance::new(wgpu::InstanceDescriptor {
backends: wgpu::Backends::PRIMARY,
dx12_shader_compiler: Default::default(),
..Default::default()
});
let surface = unsafe {
match instance
.create_surface_unsafe(wgpu::SurfaceTargetUnsafe::from_window(&window).unwrap())
{
Ok(s) => s,
Err(e) => return Err(e.to_string()),
}
};
let adapter_opt = pollster::block_on(instance.request_adapter(&wgpu::RequestAdapterOptions {
power_preference: wgpu::PowerPreference::HighPerformance,
force_fallback_adapter: false,
compatible_surface: Some(&surface),
}));
let adapter = match adapter_opt {
Some(a) => a,
None => return Err(String::from("No adapter found")),
};
let (device, queue) = match pollster::block_on(adapter.request_device(
&wgpu::DeviceDescriptor {
required_limits: wgpu::Limits::default(),
label: Some("device"),
required_features: wgpu::Features::empty(),
},
None,
)) {
Ok(a) => a,
Err(e) => return Err(e.to_string()),
};
let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
label: Some("shader"),
source: wgpu::ShaderSource::Wgsl(Cow::Borrowed(include_str!("shader.wgsl"))),
});
let bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
entries: &[],
label: Some("bind_group_layout"),
});
let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
layout: &bind_group_layout,
entries: &[],
label: Some("bind_group"),
});
let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
bind_group_layouts: &[&bind_group_layout],
label: None,
push_constant_ranges: &[],
});
let compilation_options = wgpu::PipelineCompilationOptions {
constants: &HashMap::new(),
zero_initialize_workgroup_memory: true,
};
let render_pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
layout: Some(&pipeline_layout),
vertex: wgpu::VertexState {
buffers: &[],
module: &shader,
entry_point: "vs_main",
compilation_options: compilation_options.clone(),
},
fragment: Some(wgpu::FragmentState {
targets: &[Some(wgpu::ColorTargetState {
format: wgpu::TextureFormat::Bgra8UnormSrgb,
blend: None,
write_mask: wgpu::ColorWrites::ALL,
})],
module: &shader,
entry_point: "fs_main",
compilation_options: compilation_options.clone(),
}),
primitive: wgpu::PrimitiveState {
topology: wgpu::PrimitiveTopology::TriangleList,
strip_index_format: None,
front_face: wgpu::FrontFace::Ccw,
cull_mode: Some(wgpu::Face::Front),
unclipped_depth: false,
polygon_mode: wgpu::PolygonMode::Fill,
conservative: false,
},
depth_stencil: None,
label: None,
multisample: wgpu::MultisampleState {
count: 1,
mask: !0,
alpha_to_coverage_enabled: false,
},
multiview: None,
});
let surface_caps = surface.get_capabilities(&adapter);
let surface_format = surface_caps
.formats
.iter()
.copied()
.find(|f| f.is_srgb())
.unwrap_or(surface_caps.formats[0]);
let mut config = wgpu::SurfaceConfiguration {
usage: wgpu::TextureUsages::RENDER_ATTACHMENT,
format: surface_format,
width,
height,
present_mode: wgpu::PresentMode::Fifo,
alpha_mode: wgpu::CompositeAlphaMode::Auto,
view_formats: Vec::default(),
desired_maximum_frame_latency: 2,
};
surface.configure(&device, &config);
let mut event_pump = sdl_context.event_pump()?;
'running: loop {
for event in event_pump.poll_iter() {
match event {
Event::Window {
window_id,
win_event: WindowEvent::SizeChanged(width, height),
..
} if window_id == window.id() => {
config.width = width as u32;
config.height = height as u32;
surface.configure(&device, &config);
}
Event::Quit { .. }
| Event::KeyDown {
keycode: Some(Keycode::Escape),
..
} => {
break 'running;
}
e => {
dbg!(e);
}
}
}
let frame = match surface.get_current_texture() {
Ok(frame) => frame,
Err(err) => {
let reason = match err {
SurfaceError::Timeout => "Timeout",
SurfaceError::Outdated => "Outdated",
SurfaceError::Lost => "Lost",
SurfaceError::OutOfMemory => "OutOfMemory",
};
println!("Failed to get current surface texture! Reason: {}", reason);
continue 'running;
}
};
let output = frame
.texture
.create_view(&wgpu::TextureViewDescriptor::default());
let mut encoder = device.create_command_encoder(&wgpu::CommandEncoderDescriptor {
label: Some("command_encoder"),
});
{
let mut rpass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
color_attachments: &[Some(wgpu::RenderPassColorAttachment {
view: &output,
resolve_target: None,
ops: wgpu::Operations {
load: wgpu::LoadOp::Clear(wgpu::Color::GREEN),
store: wgpu::StoreOp::Store,
},
})],
depth_stencil_attachment: None,
label: None,
timestamp_writes: None,
occlusion_query_set: None,
});
rpass.set_pipeline(&render_pipeline);
rpass.set_bind_group(0, &bind_group, &[]);
rpass.draw(0..3, 0..1);
}
queue.submit([encoder.finish()]);
frame.present();
}
Ok(())
}