glium/context/
uuid.rs

1use super::Context;
2
3/// Describes an error preventing the retrieval of the uuid.
4#[derive(Debug, Clone, Copy, PartialEq, Eq)]
5pub enum UuidError {
6    /// EXT_external_objects is not supported by the driver
7    ExtensionNotPresent,
8}
9
10impl std::fmt::Display for UuidError {
11    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
12        let desc = match self {
13            UuidError::ExtensionNotPresent => "EXT_external_objects is not supported by the driver",
14        };
15
16        f.write_str(desc)
17    }
18}
19
20impl std::error::Error for UuidError {}
21
22impl Context {
23    /// Returns the UUID of the driver currently being used by this context.
24    ///
25    /// Useful to ensure compatibility when sharing resources with an external API.
26    pub fn driver_uuid(&self) -> Result<[u8; 16], UuidError> {
27        if !self.extensions.gl_ext_semaphore && !self.extensions.gl_ext_memory_object {
28            return Err(UuidError::ExtensionNotPresent);
29        }
30        let mut data = [0u8; 16];
31        unsafe {
32            self.gl.GetUnsignedBytevEXT(crate::gl::DRIVER_UUID_EXT, data.as_mut_ptr())
33        };
34        Ok(data)
35    }
36
37    /// Returns the UUIDs of the devices being used by this context.
38    ///
39    /// Useful to ensure compatibility when sharing resources with an external API.
40    pub fn device_uuids(&self) -> Result<Vec<[u8; 16]>, UuidError> {
41        if !self.extensions.gl_ext_semaphore && !self.extensions.gl_ext_memory_object {
42            return Err(UuidError::ExtensionNotPresent);
43        }
44        let mut n = std::mem::MaybeUninit::<i32>::uninit();
45        let n = unsafe {
46            self.gl.GetIntegerv(crate::gl::NUM_DEVICE_UUIDS_EXT, n.as_mut_ptr());
47            n.assume_init()
48        };
49
50        let mut res = Vec::with_capacity(n as usize);
51        for i in 0..n {
52            let mut data = [0u8; 16];
53            unsafe {
54                self.gl.GetUnsignedBytei_vEXT(
55                    crate::gl::DEVICE_UUID_EXT,
56                    i as u32,
57                    data.as_mut_ptr(),
58                )
59            };
60            res.push(data);
61        }
62
63        Ok(res)
64    }
65}