kazan 0.3.9+1.4.359

Vulkan bindings for Rust
Documentation
//! <https://registry.khronos.org/vulkan/specs/latest/man/html/VK_EXT_calibrated_timestamps.html>
#![allow(unused_imports)]
use crate::{vk::Result as VkResult, vk::*, *};
use core::ffi::{CStr, c_char, c_int, c_void};
use core::mem::transmute;
use core::ptr;

pub const EXTENSION_NAME: &CStr = c"VK_EXT_calibrated_timestamps";

pub(super) mod defs {
    #![allow(non_camel_case_types, unused_imports)]
    use crate::{vk::*, *};
    use core::ffi::{CStr, c_char, c_int, c_void};
    use core::fmt;
    use core::marker::PhantomData;
    use core::ptr;

    /// <https://registry.khronos.org/vulkan/specs/latest/man/html/VkTimeDomainEXT.html>
    pub type TimeDomainEXT = TimeDomainKHR;
    /// <https://registry.khronos.org/vulkan/specs/latest/man/html/VkCalibratedTimestampInfoEXT.html>
    pub type CalibratedTimestampInfoEXT<'a> = CalibratedTimestampInfoKHR<'a>;
    pub type PFN_vkGetPhysicalDeviceCalibrateableTimeDomainsEXT =
        PFN_vkGetPhysicalDeviceCalibrateableTimeDomainsKHR;
    pub type PFN_vkGetCalibratedTimestampsEXT = PFN_vkGetCalibratedTimestampsKHR;
}

#[cfg(feature = "ffi")]
pub(super) mod ffi {
    #![allow(non_camel_case_types)]
    use super::defs::*;

    pub type VkTimeDomainEXT = TimeDomainEXT;
    pub type VkCalibratedTimestampInfoEXT = CalibratedTimestampInfoEXT<'static>;
}

pub struct InstanceFn {
    get_physical_device_calibrateable_time_domains:
        PFN_vkGetPhysicalDeviceCalibrateableTimeDomainsKHR,
}

impl LoadInstanceFn for InstanceFn {
    unsafe fn load_with(
        load: impl Fn(&CStr) -> Option<PFN_vkVoidFunction>,
    ) -> core::result::Result<Self, MissingEntryPointError> {
        unsafe {
            Ok(Self {
                get_physical_device_calibrateable_time_domains: transmute(
                    load(c"vkGetPhysicalDeviceCalibrateableTimeDomainsEXT")
                        .ok_or(MissingEntryPointError)?,
                ),
            })
        }
    }
}

impl InstanceFn {
    /// <https://registry.khronos.org/vulkan/specs/latest/man/html/vkGetPhysicalDeviceCalibrateableTimeDomainsEXT.html>
    #[inline]
    pub unsafe fn get_physical_device_calibrateable_time_domains(
        &self,
        physical_device: PhysicalDevice,
        mut time_domains: impl EnumerateInto<TimeDomainKHR>,
    ) -> crate::Result<()> {
        unsafe {
            let call = |time_domain_count, time_domains| {
                let result = (self.get_physical_device_calibrateable_time_domains)(
                    physical_device,
                    time_domain_count,
                    time_domains as _,
                );

                match result {
                    VkResult::SUCCESS => Ok(()),
                    VkResult::INCOMPLETE => Ok(()),
                    err => Err(err),
                }
            };
            let mut len = 0;
            call(&mut len, std::ptr::null_mut())?;
            let capacity = len.try_into().expect("failed to convert `N` to usize");
            let time_domains_buf = time_domains.reserve(capacity);
            len = time_domains_buf.len().try_into().unwrap();
            let result = call(&mut len, time_domains_buf.as_mut_ptr() as *mut _)?;
            time_domains.set_len(len.try_into().unwrap());
            Ok(result)
        }
    }
}

pub struct DeviceFn {
    get_calibrated_timestamps: PFN_vkGetCalibratedTimestampsKHR,
}

impl LoadDeviceFn for DeviceFn {
    unsafe fn load_with(
        load: impl Fn(&CStr) -> Option<PFN_vkVoidFunction>,
    ) -> core::result::Result<Self, MissingEntryPointError> {
        unsafe {
            Ok(Self {
                get_calibrated_timestamps: transmute(
                    load(c"vkGetCalibratedTimestampsEXT").ok_or(MissingEntryPointError)?,
                ),
            })
        }
    }
}

impl DeviceFn {
    /// <https://registry.khronos.org/vulkan/specs/latest/man/html/vkGetCalibratedTimestampsEXT.html>
    #[inline]
    pub unsafe fn get_calibrated_timestamps(
        &self,
        device: Device,
        timestamp_infos: &[CalibratedTimestampInfoKHR<'_>],
        timestamps: &mut [u64],
    ) -> crate::Result<u64> {
        unsafe {
            let mut max_deviation = core::mem::MaybeUninit::uninit();
            assert_eq!(timestamps.len(), timestamp_infos.len());
            let result = (self.get_calibrated_timestamps)(
                device,
                timestamp_infos.len().try_into().unwrap(),
                timestamp_infos.as_ptr() as _,
                timestamps.as_mut_ptr() as _,
                max_deviation.as_mut_ptr(),
            );

            match result {
                VkResult::SUCCESS => Ok(max_deviation.assume_init()),
                err => Err(err),
            }
        }
    }
}