This repository has been archived by the owner on Jun 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 184
/
Copy pathmain.rs
592 lines (545 loc) · 23.4 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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
use ash::{
version::{EntryV1_0, InstanceV1_0},
vk::{self, Handle},
};
use std::{borrow::Cow, convert::TryInto};
use wgpu::{
Adapter, Device, Extent3d, Instance, Queue, ShaderFlags, ShaderSource, TextureAspect,
TextureView, TextureViewDescriptor, TextureViewDimension,
};
use winit::{
event::{Event, WindowEvent},
event_loop::{ControlFlow, EventLoop},
window::Window,
};
fn main() {
let event_loop = EventLoop::new();
let window = winit::window::Window::new(&event_loop).unwrap();
pollster::block_on(run(event_loop, window));
}
async fn run(event_loop: EventLoop<()>, window: Window) {
let xr_entry = openxr::Entry::load().unwrap();
// Initialize OpenXR
let mut enabled_extensions = openxr::ExtensionSet::default();
// Note: At time of writing, Oculus does not support this extension, but it's usable through the
// OpenVR runtime instead on Oculus.
enabled_extensions.khr_vulkan_enable2 = true;
let xr_instance = xr_entry
.create_instance(
&openxr::ApplicationInfo {
application_name: "wgpu-hello-vr-triangle",
application_version: 0,
engine_name: "wgpu-hello-vr-triangle",
engine_version: 0,
},
&enabled_extensions,
&[],
)
.unwrap();
let instance_props = xr_instance.properties().unwrap();
println!(
"Loaded OpenXR runtime: {} {}",
instance_props.runtime_name, instance_props.runtime_version,
);
// Fetch a head mounted display to render to
let xr_system = xr_instance
.system(openxr::FormFactor::HEAD_MOUNTED_DISPLAY)
.unwrap();
let environment_blend_mode = xr_instance
.enumerate_environment_blend_modes(xr_system, VIEW_TYPE)
.unwrap()[0];
// Initialize graphics context
let (
vk_entry,
vk_instance,
vk_physical_device,
queue_family_index,
vk_device,
instance,
adapter,
device,
queue,
) = initialize_wgpu_openxr(&xr_instance, xr_system);
let surface = unsafe { instance.create_surface(&window) };
// Load the shaders from disk
let shader = device.create_shader_module(&wgpu::ShaderModuleDescriptor {
label: None,
source: ShaderSource::Wgsl(Cow::Borrowed(include_str!("shader.wgsl"))),
flags: ShaderFlags::all(),
});
let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
label: None,
bind_group_layouts: &[],
push_constant_ranges: &[],
});
let swapchain_format = adapter.get_swap_chain_preferred_format(&surface).unwrap();
let render_pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
label: None,
layout: Some(&pipeline_layout),
vertex: wgpu::VertexState {
module: &shader,
entry_point: "vs_main",
buffers: &[],
},
fragment: Some(wgpu::FragmentState {
module: &shader,
entry_point: "fs_main",
targets: &[swapchain_format.into()],
}),
primitive: wgpu::PrimitiveState::default(),
depth_stencil: None,
multisample: wgpu::MultisampleState::default(),
});
let size = window.inner_size();
let mut sc_desc = wgpu::SwapChainDescriptor {
usage: wgpu::TextureUsage::RENDER_ATTACHMENT,
format: swapchain_format,
width: size.width,
height: size.height,
present_mode: wgpu::PresentMode::Mailbox,
};
let mut swap_chain = device.create_swap_chain(&surface, &sc_desc);
// Start the OpenXR session
let (xr_session, mut frame_wait, mut frame_stream) = unsafe {
xr_instance
.create_session::<openxr::Vulkan>(
xr_system,
&openxr::vulkan::SessionCreateInfo {
instance: vk_instance.handle().as_raw() as _,
physical_device: vk_physical_device.as_raw() as _,
device: vk_device.handle().as_raw() as _,
queue_family_index,
queue_index: 0,
},
)
.unwrap()
};
// Create a room-scale reference space
let stage = xr_session
.create_reference_space(openxr::ReferenceSpaceType::STAGE, openxr::Posef::IDENTITY)
.unwrap();
let mut event_storage = openxr::EventDataBuffer::new();
let mut session_running = false;
let mut swapchain = None;
event_loop.run(move |event, _, control_flow| {
// Have the closure take ownership of the resources.
// `event_loop.run` never returns, therefore we must do this to ensure
// the resources are properly cleaned up.
let _ = (
&vk_entry,
&vk_instance,
&vk_device,
&instance,
&adapter,
&shader,
&pipeline_layout,
);
*control_flow = ControlFlow::Poll;
match event {
Event::WindowEvent {
event: WindowEvent::Resized(size),
..
} => {
// Recreate the swap chain with the new size
sc_desc.width = size.width;
sc_desc.height = size.height;
swap_chain = device.create_swap_chain(&surface, &sc_desc);
}
Event::RedrawRequested(_) => {
let frame = swap_chain
.get_current_frame()
.expect("Failed to acquire next swap chain texture")
.output;
let mut encoder =
device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: None });
{
let mut rpass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: None,
color_attachments: &[wgpu::RenderPassColorAttachment {
view: &frame.view,
resolve_target: None,
ops: wgpu::Operations {
load: wgpu::LoadOp::Clear(wgpu::Color::GREEN),
store: true,
},
}],
depth_stencil_attachment: None,
});
rpass.set_pipeline(&render_pipeline);
rpass.draw(0..3, 0..1);
}
queue.submit(Some(encoder.finish()));
}
Event::WindowEvent {
event: WindowEvent::CloseRequested,
..
} => *control_flow = ControlFlow::Exit,
Event::MainEventsCleared => {
// Handle OpenXR events
while let Some(event) = xr_instance.poll_event(&mut event_storage).unwrap() {
match event {
openxr::Event::SessionStateChanged(e) => {
// Session state change is where we can begin and end sessions, as well as
// find quit messages!
println!("Entered state {:?}", e.state());
match e.state() {
openxr::SessionState::READY => {
xr_session.begin(VIEW_TYPE).unwrap();
session_running = true;
}
openxr::SessionState::STOPPING => {
xr_session.end().unwrap();
session_running = false;
}
openxr::SessionState::EXITING
| openxr::SessionState::LOSS_PENDING => {
*control_flow = ControlFlow::Exit;
}
_ => {}
}
}
openxr::Event::InstanceLossPending(_) => {
*control_flow = ControlFlow::Exit;
}
openxr::Event::EventsLost(e) => {
println!("Lost {} OpenXR events", e.lost_event_count());
}
_ => {}
}
}
// Render to HMD only if we have an active session
if session_running {
// Block until the previous frame is finished displaying, and is ready for
// another one. Also returns a prediction of when the next frame will be
// displayed, for use with predicting locations of controllers, viewpoints, etc.
let xr_frame_state = frame_wait.wait().unwrap();
// Must be called before any rendering is done!
frame_stream.begin().unwrap();
// Only render if we should
if !xr_frame_state.should_render {
// Early bail
frame_stream
.end(
xr_frame_state.predicted_display_time,
environment_blend_mode,
&[],
)
.unwrap();
return;
}
// If we do not have a swapchain yet, create it
let (xr_swapchain, resolution, image_views) =
swapchain.get_or_insert_with(|| {
create_swapchain(&xr_instance, xr_system, &xr_session, &device)
});
// Check which image we need to render to and wait until the compositor is
// done with this image
let image_index = xr_swapchain.acquire_image().unwrap();
xr_swapchain.wait_image(openxr::Duration::INFINITE).unwrap();
let (left_view, right_view) = &image_views[image_index as usize];
// Render!
let mut encoder = device
.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: None });
{
let mut rpass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: None,
color_attachments: &[wgpu::RenderPassColorAttachment {
view: &left_view,
resolve_target: None,
ops: wgpu::Operations {
load: wgpu::LoadOp::Clear(wgpu::Color::GREEN),
store: true,
},
}],
depth_stencil_attachment: None,
});
rpass.set_pipeline(&render_pipeline);
rpass.draw(0..3, 0..1);
}
{
let mut rpass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: None,
color_attachments: &[wgpu::RenderPassColorAttachment {
view: &right_view,
resolve_target: None,
ops: wgpu::Operations {
load: wgpu::LoadOp::Clear(wgpu::Color::GREEN),
store: true,
},
}],
depth_stencil_attachment: None,
});
rpass.set_pipeline(&render_pipeline);
rpass.draw(0..3, 0..1);
}
// Fetch the view transforms. To minimize latency, we intentionally do this
// *after* recording commands to render the scene, i.e. at the last possible
// moment before rendering begins in earnest on the GPU. Uniforms dependent on
// this data can be sent to the GPU just-in-time by writing them to per-frame
// host-visible memory which the GPU will only read once the command buffer is
// submitted.
let (_, views) = xr_session
.locate_views(VIEW_TYPE, xr_frame_state.predicted_display_time, &stage)
.unwrap();
queue.submit(Some(encoder.finish()));
xr_swapchain.release_image().unwrap();
// End rendering and submit the images
let rect = openxr::Rect2Di {
offset: openxr::Offset2Di { x: 0, y: 0 },
extent: openxr::Extent2Di {
width: resolution.width as _,
height: resolution.height as _,
},
};
frame_stream
.end(
xr_frame_state.predicted_display_time,
environment_blend_mode,
&[&openxr::CompositionLayerProjection::new()
.space(&stage)
.views(&[
openxr::CompositionLayerProjectionView::new()
.pose(views[0].pose)
.fov(views[0].fov)
.sub_image(
openxr::SwapchainSubImage::new()
.swapchain(&xr_swapchain)
.image_array_index(0)
.image_rect(rect),
),
openxr::CompositionLayerProjectionView::new()
.pose(views[1].pose)
.fov(views[1].fov)
.sub_image(
openxr::SwapchainSubImage::new()
.swapchain(&xr_swapchain)
.image_array_index(1)
.image_rect(rect),
),
])],
)
.unwrap();
}
}
Event::LoopDestroyed => {
// TODO: Destroy first WGPU and then raw vulkan handles after
}
_ => {}
}
});
}
fn initialize_wgpu_openxr(
xr_instance: &openxr::Instance,
xr_system: openxr::SystemId,
) -> (
ash::Entry,
ash::Instance,
ash::vk::PhysicalDevice,
u32,
ash::Device,
Instance,
Adapter,
Device,
Queue,
) {
unsafe {
// This must always be called before vulkan init
let _requirements = xr_instance
.graphics_requirements::<openxr::Vulkan>(xr_system)
.unwrap();
// Initialize Vulkan instance
let vk_entry = ash::Entry::new().unwrap();
let vk_extensions = wgpu::Instance::required_vulkan_extensions(&vk_entry);
let mut extension_names_raw = vec![];
for extension in &vk_extensions {
extension_names_raw.push(extension.as_ptr());
}
let vk_target_version = vk::make_version(1, 1, 0);
let vk_app_info = vk::ApplicationInfo::builder()
.application_version(0)
.engine_version(0)
.api_version(vk_target_version);
let vk_instance = xr_instance
.create_vulkan_instance(
xr_system,
std::mem::transmute(vk_entry.static_fn().get_instance_proc_addr),
&vk::InstanceCreateInfo::builder()
.application_info(&vk_app_info)
.enabled_extension_names(&extension_names_raw) as *const _
as *const _,
)
.expect("XR error creating Vulkan instance")
.map_err(vk::Result::from_raw)
.expect("Vulkan error creating Vulkan instance");
let vk_instance = ash::Instance::load(
vk_entry.static_fn(),
vk::Instance::from_raw(vk_instance as _),
);
// Find the physical device we actually need to initialize with
let vk_physical_device = vk::PhysicalDevice::from_raw(
xr_instance
.vulkan_graphics_device(xr_system, vk_instance.handle().as_raw() as _)
.unwrap() as _,
);
let queue_family_index = vk_instance
.get_physical_device_queue_family_properties(vk_physical_device)
.into_iter()
.enumerate()
.find_map(|(queue_family_index, info)| {
if info.queue_flags.contains(vk::QueueFlags::GRAPHICS) {
Some(queue_family_index as u32)
} else {
None
}
})
.expect("Vulkan device has no graphics queue");
// Initialize WGPU instance using our Vulkan instance
let instance =
wgpu::Instance::new_raw_vulkan(vk_entry.clone(), vk_instance.clone(), vk_extensions);
let adapter = instance.adapter_from_raw_vulkan(vk_physical_device);
// Create the Vulkan logical device
let desc = wgpu::DeviceDescriptor {
label: None,
features: wgpu::Features::empty(),
limits: wgpu::Limits::default(),
};
let vk_device_extensions = adapter.required_vulkan_device_extensions(&desc);
let mut device_extension_names_raw = vec![];
for extension in &vk_device_extensions {
device_extension_names_raw.push(extension.as_ptr());
}
let queue_create_infos = [vk::DeviceQueueCreateInfo::builder()
.queue_family_index(queue_family_index)
.queue_priorities(&[1.0])
.build()];
let mut vulkan11_features = vk::PhysicalDeviceVulkan11Features {
multiview: vk::TRUE,
..Default::default()
};
let create_device_info = vk::DeviceCreateInfo::builder()
.queue_create_infos(&queue_create_infos)
.enabled_extension_names(&device_extension_names_raw)
.push_next(&mut vulkan11_features);
let vk_device_raw = xr_instance
.create_vulkan_device(
xr_system,
std::mem::transmute(vk_entry.static_fn().get_instance_proc_addr),
vk_physical_device.as_raw() as _,
&create_device_info as *const _ as *const _,
)
.expect("XR error creating Vulkan device")
.map_err(vk::Result::from_raw)
.expect("Vulkan error creating Vulkan device");
let vk_device = ash::Device::load(
vk_instance.fp_v1_0(),
vk::Device::from_raw(vk_device_raw as _),
);
// Initialize WGPU device using our Device instance
let (device, queue) =
adapter.device_from_raw_vulkan(vk_device.clone(), queue_family_index, &desc, None);
(
vk_entry,
vk_instance,
vk_physical_device,
queue_family_index,
vk_device,
instance,
adapter,
device,
queue,
)
}
}
fn create_swapchain(
xr_instance: &openxr::Instance,
xr_system: openxr::SystemId,
xr_session: &openxr::Session<openxr::Vulkan>,
device: &Device,
) -> (
openxr::Swapchain<openxr::Vulkan>,
vk::Extent2D,
Vec<(TextureView, TextureView)>,
) {
println!("Creating OpenXR swapchain");
// Fetch the views we need to render to (the eye screens on the HMD)
let views = xr_instance
.enumerate_view_configuration_views(xr_system, VIEW_TYPE)
.unwrap();
assert_eq!(views.len(), 2);
assert_eq!(views[0], views[1]);
// Create the OpenXR swapchain
let color_format = vk::Format::B8G8R8A8_SRGB;
let resolution = vk::Extent2D {
width: views[0].recommended_image_rect_width,
height: views[0].recommended_image_rect_height,
};
let xr_swapchain = xr_session
.create_swapchain(&openxr::SwapchainCreateInfo {
create_flags: openxr::SwapchainCreateFlags::EMPTY,
usage_flags: openxr::SwapchainUsageFlags::COLOR_ATTACHMENT
| openxr::SwapchainUsageFlags::SAMPLED,
format: color_format.clone().as_raw() as _,
sample_count: 1,
width: resolution.width,
height: resolution.height,
face_count: 1,
array_size: 2,
mip_count: 1,
})
.unwrap();
// Create image views for the swapchain
let image_views: Vec<_> = xr_swapchain
.enumerate_images()
.unwrap()
.into_iter()
.map(|image| {
// Create a WGPU image view for this image
// TODO: Right now we're using separate image views per eye, we need
// multiview support in WGPU
unsafe {
(
device.create_raw_vulkan_texture_view(
vk::Image::from_raw(image),
vk::ImageViewType::TYPE_2D,
&TextureViewDescriptor {
label: None,
format: Some(wgpu::TextureFormat::Bgra8UnormSrgb),
dimension: Some(TextureViewDimension::D2Array),
aspect: TextureAspect::All,
base_mip_level: 0,
mip_level_count: Some(1u32.try_into().unwrap()),
base_array_layer: 0,
array_layer_count: Some(1.try_into().unwrap()),
},
Extent3d {
width: resolution.width,
height: resolution.height,
depth_or_array_layers: 1,
},
),
device.create_raw_vulkan_texture_view(
vk::Image::from_raw(image),
vk::ImageViewType::TYPE_2D,
&TextureViewDescriptor {
label: None,
format: Some(wgpu::TextureFormat::Bgra8UnormSrgb),
dimension: Some(TextureViewDimension::D2Array),
aspect: TextureAspect::All,
base_mip_level: 0,
mip_level_count: Some(1u32.try_into().unwrap()),
base_array_layer: 1,
array_layer_count: Some(1.try_into().unwrap()),
},
Extent3d {
width: resolution.width,
height: resolution.height,
depth_or_array_layers: 1,
},
),
)
}
})
.collect();
(xr_swapchain, resolution, image_views)
}
const VIEW_TYPE: openxr::ViewConfigurationType = openxr::ViewConfigurationType::PRIMARY_STEREO;