ash/
prelude.rs

1use std::convert::TryInto;
2#[cfg(feature = "debug")]
3use std::fmt;
4use std::mem;
5
6use crate::vk;
7pub type VkResult<T> = Result<T, vk::Result>;
8
9impl vk::Result {
10    #[inline]
11    pub fn result(self) -> VkResult<()> {
12        self.result_with_success(())
13    }
14
15    #[inline]
16    pub fn result_with_success<T>(self, v: T) -> VkResult<T> {
17        match self {
18            Self::SUCCESS => Ok(v),
19            _ => Err(self),
20        }
21    }
22
23    #[inline]
24    pub unsafe fn assume_init_on_success<T>(self, v: mem::MaybeUninit<T>) -> VkResult<T> {
25        self.result().map(move |()| v.assume_init())
26    }
27}
28
29/// Repeatedly calls `f` until it does not return [`vk::Result::INCOMPLETE`] anymore, ensuring all
30/// available data has been read into the vector.
31///
32/// See for example [`vkEnumerateInstanceExtensionProperties`]: the number of available items may
33/// change between calls; [`vk::Result::INCOMPLETE`] is returned when the count increased (and the
34/// vector is not large enough after querying the initial size), requiring Ash to try again.
35///
36/// [`vkEnumerateInstanceExtensionProperties`]: https://www.khronos.org/registry/vulkan/specs/1.3-extensions/man/html/vkEnumerateInstanceExtensionProperties.html
37pub(crate) unsafe fn read_into_uninitialized_vector<N: Copy + Default + TryInto<usize>, T>(
38    f: impl Fn(&mut N, *mut T) -> vk::Result,
39) -> VkResult<Vec<T>>
40where
41    <N as TryInto<usize>>::Error: std::fmt::Debug,
42{
43    loop {
44        let mut count = N::default();
45        f(&mut count, std::ptr::null_mut()).result()?;
46        let mut data =
47            Vec::with_capacity(count.try_into().expect("`N` failed to convert to `usize`"));
48
49        let err_code = f(&mut count, data.as_mut_ptr());
50        if err_code != vk::Result::INCOMPLETE {
51            err_code.result()?;
52            data.set_len(count.try_into().expect("`N` failed to convert to `usize`"));
53            break Ok(data);
54        }
55    }
56}
57
58/// Repeatedly calls `f` until it does not return [`vk::Result::INCOMPLETE`] anymore, ensuring all
59/// available data has been read into the vector.
60///
61/// Items in the target vector are [`default()`][Default::default()]-initialized which is required
62/// for [`vk::BaseOutStructure`]-like structs where [`vk::BaseOutStructure::s_type`] needs to be a
63/// valid type and [`vk::BaseOutStructure::p_next`] a valid or [`null`][std::ptr::null_mut()]
64/// pointer.
65///
66/// See for example [`vkEnumerateInstanceExtensionProperties`]: the number of available items may
67/// change between calls; [`vk::Result::INCOMPLETE`] is returned when the count increased (and the
68/// vector is not large enough after querying the initial size), requiring Ash to try again.
69///
70/// [`vkEnumerateInstanceExtensionProperties`]: https://www.khronos.org/registry/vulkan/specs/1.3-extensions/man/html/vkEnumerateInstanceExtensionProperties.html
71pub(crate) unsafe fn read_into_defaulted_vector<
72    N: Copy + Default + TryInto<usize>,
73    T: Default + Clone,
74>(
75    f: impl Fn(&mut N, *mut T) -> vk::Result,
76) -> VkResult<Vec<T>>
77where
78    <N as TryInto<usize>>::Error: std::fmt::Debug,
79{
80    loop {
81        let mut count = N::default();
82        f(&mut count, std::ptr::null_mut()).result()?;
83        let mut data =
84            vec![Default::default(); count.try_into().expect("`N` failed to convert to `usize`")];
85
86        let err_code = f(&mut count, data.as_mut_ptr());
87        if err_code != vk::Result::INCOMPLETE {
88            data.set_len(count.try_into().expect("`N` failed to convert to `usize`"));
89            break err_code.result_with_success(data);
90        }
91    }
92}
93
94#[cfg(feature = "debug")]
95pub(crate) fn debug_flags<Value: Into<u64> + Copy>(
96    f: &mut fmt::Formatter,
97    known: &[(Value, &'static str)],
98    value: Value,
99) -> fmt::Result {
100    let mut first = true;
101    let mut accum = value.into();
102    for &(bit, name) in known {
103        let bit = bit.into();
104        if bit != 0 && accum & bit == bit {
105            if !first {
106                f.write_str(" | ")?;
107            }
108            f.write_str(name)?;
109            first = false;
110            accum &= !bit;
111        }
112    }
113    if accum != 0 {
114        if !first {
115            f.write_str(" | ")?;
116        }
117        write!(f, "{accum:b}")?;
118    }
119    Ok(())
120}