kazan 0.3.9+1.4.359

Vulkan bindings for Rust
Documentation
//! <https://registry.khronos.org/vulkan/specs/latest/man/html/VK_KHR_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_KHR_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/VkCalibratedTimestampInfoKHR.html>
    #[repr(C)]
    #[derive(Copy, Clone)]
    #[must_use]
    pub struct CalibratedTimestampInfoKHR<'a> {
        pub s_type: StructureType,
        pub p_next: *const c_void,
        pub time_domain: TimeDomainKHR,
        pub _marker: PhantomData<&'a ()>,
    }

    #[cfg(feature = "debug")]
    impl fmt::Debug for CalibratedTimestampInfoKHR<'_> {
        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
            f.debug_struct("CalibratedTimestampInfoKHR")
                .field("s_type", &self.s_type)
                .field("p_next", &self.p_next)
                .field("time_domain", &self.time_domain)
                .finish()
        }
    }

    unsafe impl<'a> TaggedStructure<'a> for CalibratedTimestampInfoKHR<'a> {
        const STRUCTURE_TYPE: StructureType = StructureType::CALIBRATED_TIMESTAMP_INFO_KHR;
    }

    impl Default for CalibratedTimestampInfoKHR<'_> {
        fn default() -> Self {
            Self {
                s_type: Self::STRUCTURE_TYPE,
                p_next: ptr::null(),
                time_domain: Default::default(),
                _marker: PhantomData,
            }
        }
    }

    impl<'a> CalibratedTimestampInfoKHR<'a> {
        #[inline]
        pub fn time_domain(mut self, time_domain: TimeDomainKHR) -> Self {
            self.time_domain = time_domain;
            self
        }
    }

    /// <https://registry.khronos.org/vulkan/specs/latest/man/html/VkTimeDomainKHR.html>
    #[repr(transparent)]
    #[derive(Copy, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
    pub struct TimeDomainKHR(i32);

    impl TimeDomainKHR {
        #[inline]
        pub const fn from_raw(x: i32) -> Self {
            Self(x)
        }
        #[inline]
        pub const fn as_raw(self) -> i32 {
            self.0
        }

        pub const DEVICE_KHR: Self = Self(0);
        pub const CLOCK_MONOTONIC_KHR: Self = Self(1);
        pub const CLOCK_MONOTONIC_RAW_KHR: Self = Self(2);
        pub const QUERY_PERFORMANCE_COUNTER_KHR: Self = Self(3);

        // VK_EXT_calibrated_timestamps
        pub const DEVICE_EXT: Self = Self::DEVICE_KHR;
        pub const CLOCK_MONOTONIC_EXT: Self = Self::CLOCK_MONOTONIC_KHR;
        pub const CLOCK_MONOTONIC_RAW_EXT: Self = Self::CLOCK_MONOTONIC_RAW_KHR;
        pub const QUERY_PERFORMANCE_COUNTER_EXT: Self = Self::QUERY_PERFORMANCE_COUNTER_KHR;

        // VK_EXT_present_timing
        pub const PRESENT_STAGE_LOCAL_EXT: Self = Self(1000208000);
        pub const SWAPCHAIN_LOCAL_EXT: Self = Self(1000208001);
    }

    impl fmt::Debug for TimeDomainKHR {
        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
            let name = match *self {
                Self::DEVICE_KHR => Some("DEVICE_KHR"),
                Self::CLOCK_MONOTONIC_KHR => Some("CLOCK_MONOTONIC_KHR"),
                Self::CLOCK_MONOTONIC_RAW_KHR => Some("CLOCK_MONOTONIC_RAW_KHR"),
                Self::QUERY_PERFORMANCE_COUNTER_KHR => Some("QUERY_PERFORMANCE_COUNTER_KHR"),
                Self::PRESENT_STAGE_LOCAL_EXT => Some("PRESENT_STAGE_LOCAL_EXT"),
                Self::SWAPCHAIN_LOCAL_EXT => Some("SWAPCHAIN_LOCAL_EXT"),
                _ => None,
            };
            if let Some(name) = name {
                f.write_str(name)
            } else {
                self.0.fmt(f)
            }
        }
    }

    /// <https://registry.khronos.org/vulkan/specs/latest/man/html/vkGetPhysicalDeviceCalibrateableTimeDomainsKHR.html>
    pub type PFN_vkGetPhysicalDeviceCalibrateableTimeDomainsKHR =
        unsafe extern "system" fn(
            physical_device: PhysicalDevice,
            p_time_domain_count: *mut u32,
            p_time_domains: *mut TimeDomainKHR,
        ) -> vk::Result;
    /// <https://registry.khronos.org/vulkan/specs/latest/man/html/vkGetCalibratedTimestampsKHR.html>
    pub type PFN_vkGetCalibratedTimestampsKHR = unsafe extern "system" fn(
        device: Device,
        timestamp_count: u32,
        p_timestamp_infos: *const CalibratedTimestampInfoKHR<'_>,
        p_timestamps: *mut u64,
        p_max_deviation: *mut u64,
    ) -> vk::Result;
}

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

    pub type VkCalibratedTimestampInfoKHR = CalibratedTimestampInfoKHR<'static>;
    pub type VkTimeDomainKHR = TimeDomainKHR;
    impl CalibratedTimestampInfoKHR<'_> {
        #[inline]
        pub unsafe fn drop_lifetime_for_ffi(&self) -> &VkCalibratedTimestampInfoKHR {
            unsafe { core::mem::transmute(self) }
        }
    }
}

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"vkGetPhysicalDeviceCalibrateableTimeDomainsKHR")
                        .ok_or(MissingEntryPointError)?,
                ),
            })
        }
    }
}

impl InstanceFn {
    /// <https://registry.khronos.org/vulkan/specs/latest/man/html/vkGetPhysicalDeviceCalibrateableTimeDomainsKHR.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"vkGetCalibratedTimestampsKHR").ok_or(MissingEntryPointError)?,
                ),
            })
        }
    }
}

impl DeviceFn {
    /// <https://registry.khronos.org/vulkan/specs/latest/man/html/vkGetCalibratedTimestampsKHR.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),
            }
        }
    }
}