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
use std::{ffi::CString, mem, ptr};

use sys::platform::*;

use crate::*;

impl Instance {
    /// Set the debug name of this `Instance`, if `XR_EXT_debug_utils` is loaded
    #[inline]
    pub fn set_name(&mut self, name: &str) -> Result<()> {
        if let Some(fp) = self.exts().ext_debug_utils.as_ref() {
            let name = CString::new(name).unwrap();
            let info = sys::DebugUtilsObjectNameInfoEXT {
                ty: sys::DebugUtilsObjectNameInfoEXT::TYPE,
                next: ptr::null(),
                object_type: ObjectType::INSTANCE,
                object_handle: self.as_raw().into_raw(),
                object_name: name.as_ptr(),
            };
            unsafe {
                cvt((fp.set_debug_utils_object_name)(self.as_raw(), &info))?;
            }
        }
        Ok(())
    }

    #[inline]
    pub fn properties(&self) -> Result<InstanceProperties> {
        unsafe {
            let mut p = sys::InstanceProperties {
                ty: sys::InstanceProperties::TYPE,
                ..mem::zeroed()
            };
            cvt((self.fp().get_instance_properties)(self.as_raw(), &mut p))?;
            Ok(InstanceProperties {
                runtime_version: p.runtime_version,
                runtime_name: fixed_str(&p.runtime_name).into(),
            })
        }
    }

    #[inline]
    pub fn result_to_string(&self, result: sys::Result) -> Result<String> {
        unsafe {
            let mut s = [0; sys::MAX_RESULT_STRING_SIZE];
            cvt((self.fp().result_to_string)(
                self.as_raw(),
                result,
                s.as_mut_ptr(),
            ))?;
            Ok(fixed_str(&s).into())
        }
    }

    #[inline]
    pub fn structure_type_to_string(&self, ty: StructureType) -> Result<String> {
        unsafe {
            let mut s = [0; sys::MAX_STRUCTURE_NAME_SIZE];
            cvt((self.fp().structure_type_to_string)(
                self.as_raw(),
                ty,
                s.as_mut_ptr(),
            ))?;
            Ok(fixed_str(&s).into())
        }
    }

    #[inline]
    pub fn system(&self, form_factor: FormFactor) -> Result<SystemId> {
        let info = sys::SystemGetInfo {
            ty: sys::SystemGetInfo::TYPE,
            next: ptr::null_mut(),
            form_factor,
        };
        let mut out = SystemId::NULL;
        unsafe {
            cvt((self.fp().get_system)(self.as_raw(), &info, &mut out))?;
        }
        Ok(out)
    }

    #[inline]
    pub fn system_properties(&self, system: SystemId) -> Result<SystemProperties> {
        unsafe {
            let mut p = sys::SystemProperties {
                ty: sys::SystemProperties::TYPE,
                ..mem::zeroed()
            };
            cvt((self.fp().get_system_properties)(
                self.as_raw(),
                system,
                &mut p,
            ))?;
            Ok(SystemProperties {
                system_id: p.system_id,
                vendor_id: p.vendor_id,
                system_name: fixed_str(&p.system_name).into(),
                graphics_properties: p.graphics_properties,
                tracking_properties: SystemTrackingProperties {
                    orientation_tracking: p.tracking_properties.orientation_tracking.into(),
                    position_tracking: p.tracking_properties.position_tracking.into(),
                },
            })
        }
    }

    /// Construct a `Path` from a string
    ///
    /// # Safety
    ///
    /// A returned `Path` must not used with any other instance.
    #[inline]
    pub unsafe fn string_to_path(&self, string: &str) -> Result<Path> {
        let string = CString::new(string).map_err(|_| sys::Result::ERROR_PATH_FORMAT_INVALID)?;
        let mut out = Path::NULL;
        cvt((self.fp().string_to_path)(
            self.as_raw(),
            string.as_ptr(),
            &mut out,
        ))?;
        Ok(out)
    }

    #[inline]
    pub fn path_to_string(&self, path: Path) -> Result<String> {
        get_str(|input, output, buf| unsafe {
            (self.fp().path_to_string)(self.as_raw(), path, input, output, buf)
        })
    }

    /// Identify the Vulkan instance extensions required by a system
    ///
    /// Returns a space-delimited list of Vulkan instance extension names.
    #[inline]
    pub fn vulkan_instance_extensions(&self, system: SystemId) -> Result<String> {
        get_str(|input, output, buf| unsafe {
            (self.vulkan().get_vulkan_instance_extensions)(
                self.as_raw(),
                system,
                input,
                output,
                buf,
            )
        })
    }

    /// Identify the Vulkan device extensions required by a system
    ///
    /// Returns a space-delimited list of Vulkan device extension names.
    #[inline]
    pub fn vulkan_device_extensions(&self, system: SystemId) -> Result<String> {
        get_str(|input, output, buf| unsafe {
            (self.vulkan().get_vulkan_device_extensions)(self.as_raw(), system, input, output, buf)
        })
    }

    /// Identify the Vulkan graphics device to use for a system
    #[inline]
    pub fn vulkan_graphics_device(
        &self,
        system: SystemId,
        vk_instance: VkInstance,
    ) -> Result<VkPhysicalDevice> {
        let mut out = ptr::null();
        unsafe {
            cvt((self.vulkan().get_vulkan_graphics_device)(
                self.as_raw(),
                system,
                vk_instance,
                &mut out,
            ))?;
        }
        Ok(out)
    }

    /// Query graphics API version requirements
    pub fn graphics_requirements<G: Graphics>(&self, system: SystemId) -> Result<G::Requirements> {
        G::requirements(self, system)
    }

    /// Create a session for a particular graphics API
    ///
    /// # Safety
    ///
    /// The requirements documented by the graphics API extension must be respected. Among other
    /// requirements, `info` must contain valid handles, and certain operations must be externally
    /// synchronized.
    #[inline]
    pub unsafe fn create_session<G: Graphics>(
        &self,
        system: SystemId,
        info: &G::SessionCreateInfo,
    ) -> Result<(Session<G>, FrameStream<G>)> {
        let handle = G::create_session(self, system, info)?;
        Ok(Session::from_raw(self.clone(), handle))
    }

    /// Create a session without graphics support
    #[inline]
    pub fn create_session_headless(&self, system: SystemId) -> Result<Session<Headless>> {
        unsafe {
            Ok(self.create_session(system, &())?.0)
        }
    }

    /// Get the next event, if available
    ///
    /// Returns immediately regardless of whether an event was available.
    #[inline]
    pub fn poll_event<'a>(&self, storage: &'a mut EventDataBuffer) -> Result<Option<Event<'a>>> {
        unsafe {
            // Work around a shortcoming in NLL as of 2019-03-22
            let storage: *mut EventDataBuffer = storage;
            loop {
                let status = cvt((self.fp().poll_event)(self.as_raw(), &mut (*storage).inner))?;
                if status == sys::Result::EVENT_UNAVAILABLE {
                    return Ok(None);
                }
                debug_assert_eq!(status, sys::Result::SUCCESS);
                if let x @ Some(_) = Event::from_raw(&(*storage).inner) {
                    return Ok(x);
                }
            }
        }
    }

    /// Enumerates the supported view configuration types
    #[inline]
    pub fn enumerate_view_configurations(
        &self,
        system: SystemId,
    ) -> Result<Vec<ViewConfigurationType>> {
        get_arr(|cap, count, buf| unsafe {
            (self.fp().enumerate_view_configurations)(self.as_raw(), system, cap, count, buf)
        })
    }

    /// Query properties of an individual view configuration
    #[inline]
    pub fn view_configuration_properties(
        &self,
        system: SystemId,
        ty: ViewConfigurationType,
    ) -> Result<ViewConfigurationProperties> {
        let mut out;
        unsafe {
            out = sys::ViewConfigurationProperties {
                ty: sys::ViewConfigurationProperties::TYPE,
                next: ptr::null_mut(),
                ..mem::uninitialized()
            };
            cvt((self.fp().get_view_configuration_properties)(
                self.as_raw(),
                system,
                ty,
                &mut out,
            ))?;
        }
        Ok(ViewConfigurationProperties {
            view_configuration_type: out.view_configuration_type,
            fov_mutable: out.fov_mutable != sys::FALSE,
        })
    }

    #[inline]
    pub fn enumerate_view_configuration_views(
        &self,
        system: SystemId,
        ty: ViewConfigurationType,
    ) -> Result<Vec<ViewConfigurationView>> {
        let views = get_arr_init(
            unsafe {
                sys::ViewConfigurationView {
                    ty: sys::ViewConfigurationView::TYPE,
                    next: ptr::null_mut(),
                    ..mem::uninitialized()
                }
            },
            |capacity, count, buf| unsafe {
                (self.fp().enumerate_view_configuration_views)(
                    self.as_raw(),
                    system,
                    ty,
                    capacity,
                    count,
                    buf as *mut _,
                )
            },
        )?;
        Ok(views
            .into_iter()
            .map(|x| ViewConfigurationView {
                recommended_image_rect_width: x.recommended_image_rect_width,
                max_image_rect_width: x.max_image_rect_width,
                recommended_image_rect_height: x.recommended_image_rect_height,
                max_image_rect_height: x.max_image_rect_height,
                recommended_swapchain_sample_count: x.recommended_swapchain_sample_count,
                max_swapchain_sample_count: x.max_swapchain_sample_count,
            })
            .collect())
    }

    #[inline]
    pub fn enumerate_environment_blend_modes(
        &self,
        system: SystemId,
    ) -> Result<Vec<EnvironmentBlendMode>> {
        get_arr(|cap, count, buf| unsafe {
            (self.fp().enumerate_environment_blend_modes)(self.as_raw(), system, cap, count, buf)
        })
    }

    /// Obtain the current `Time`
    ///
    /// Requires KHR_convert_timespec_time
    #[inline]
    pub fn now(&self) -> Result<Time> {
        unsafe {
            let mut now = mem::uninitialized();
            libc::clock_gettime(libc::CLOCK_MONOTONIC, &mut now);
            let mut out = mem::uninitialized();
            cvt((self.exts().khr_convert_timespec_time.as_ref().expect("KHR_convert_timespec_time not loaded")
                 .convert_timespec_time_to_time)(self.as_raw(), &now, &mut out))?;
            Ok(out)
        }
    }

    //
    // Internal helpers
    //

    pub(crate) fn vulkan(&self) -> &raw::VulkanEnableKHR {
        self.exts().khr_vulkan_enable.as_ref().expect("KHR_vulkan_enable not loaded")
    }
    pub(crate) fn opengl(&self) -> &raw::OpenglEnableKHR {
        self.exts().khr_opengl_enable.as_ref().expect("KHR_opengl_enable not loaded")
    }
}

#[derive(Debug, Clone)]
pub struct InstanceProperties {
    pub runtime_version: u32,
    pub runtime_name: String,
}

#[derive(Clone)]
pub struct SystemProperties {
    pub system_id: SystemId,
    pub vendor_id: u32,
    pub system_name: String,
    pub graphics_properties: SystemGraphicsProperties,
    pub tracking_properties: SystemTrackingProperties,
}

#[derive(Debug, Copy, Clone)]
pub struct SystemTrackingProperties {
    pub orientation_tracking: bool,
    pub position_tracking: bool,
}

#[derive(Debug, Copy, Clone)]
pub struct ViewConfigurationProperties {
    pub view_configuration_type: ViewConfigurationType,
    pub fov_mutable: bool,
}

#[derive(Debug, Copy, Clone)]
pub struct ViewConfigurationView {
    pub recommended_image_rect_width: u32,
    pub max_image_rect_width: u32,
    pub recommended_image_rect_height: u32,
    pub max_image_rect_height: u32,
    pub recommended_swapchain_sample_count: u32,
    pub max_swapchain_sample_count: u32,
}

pub struct EventDataBuffer {
    inner: sys::EventDataBuffer,
}

impl EventDataBuffer {
    pub fn new() -> Self {
        Self {
            inner: sys::EventDataBuffer {
                ty: sys::EventDataBuffer::TYPE,
                next: ptr::null_mut(),
                ..unsafe { mem::uninitialized() }
            }
        }
    }
}