vulkan_rs 1.0.62

Vulkan bindings for the rust programming language.
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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
/*
**  Copyright (c) 2016, Christoph Hommelsheim
**  All rights reserved.
**
**  Redistribution and use in source and binary forms, with or without
**  modification, are permitted provided that the following conditions are met:
**
**  * Redistributions of source code must retain the above copyright notice, this
**    list of conditions and the following disclaimer.
**
**  * Redistributions in binary form must reproduce the above copyright notice,
**    this list of conditions and the following disclaimer in the documentation
**    and/or other materials provided with the distribution.
**
**  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
**  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
**  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
**  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
**  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
**  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
**  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
**  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
**  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
**  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
**
*/

//! utilities

use std::fmt;
use std::ops;
use std::collections::BTreeSet;
use std::borrow::Cow;
use std::os::raw;
use std::ffi::CStr;
use types;

/// Holds a compressed version triple.
///
/// - Bits 0 (LSB) to 11: patch version
/// - Bits 12 to 21: minor version
/// - Bits 22 to 31: major version
#[repr(C)]
#[derive(Copy,Clone,PartialEq,Eq,PartialOrd,Ord,Hash)]
pub struct VkVersion(pub u32);

impl VkVersion {
    #[inline]
    pub fn new(major: u32, minor: u32, patch: u32) -> VkVersion {
        // TODO: make `const fn` when feature stabilized
        VkVersion((major << 22) | (minor << 12) | patch)
    }
    #[inline]
    pub fn major(self) -> u32 {
        self.0 >> 22
    }
    #[inline]
    pub fn minor(self) -> u32 {
        (self.0 >> 12) & 0x3ff
    }
    #[inline]
    pub fn patch(self) -> u32 {
        self.0 & 0xfff
    }
}

impl fmt::Display for VkVersion {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}.{}.{}", self.major(), self.minor(), self.patch())
    }
}
impl fmt::Debug for VkVersion {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "VkVersion({}.{}.{})", self.major(), self.minor(), self.patch())
    }
}
impl fmt::LowerHex for VkVersion {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{:x}", self.0)
    }
}
impl fmt::UpperHex for VkVersion {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{:X}", self.0)
    }
}

impl Into<u32> for VkVersion {
    #[inline]
    fn into(self) -> u32 {
        self.0
    }
}

impl From<u32> for VkVersion {
    #[inline]
    fn from(value: u32) -> VkVersion {
        VkVersion(value)
    }
}

/// Base-type for a dispatchable object handle.
///
/// The only dispatchable handle types are those related to device and instance management.
#[repr(C)]
#[derive(Copy,Clone,PartialEq,Eq)]
pub struct VkDispatchableHandle{
    value: usize,
}

/// Base-type for a non-dispatchable object handle.
///
/// Most Vulkan handle types, are non-dispatchable.
#[repr(C)]
#[derive(Copy,Clone,PartialEq,Eq)]
pub struct VkNonDispatchableHandle {
    value: u64,
}

impl VkDispatchableHandle {
    pub const NULL : VkDispatchableHandle = VkDispatchableHandle { value: 0 };
}
impl VkNonDispatchableHandle {
    pub const NULL : VkNonDispatchableHandle = VkNonDispatchableHandle { value: 0 };
}

impl fmt::Debug for VkDispatchableHandle {
    #[inline]
    fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
        write!(f, "`H:{:#x}`", self.value)
    }
}
impl fmt::Display for VkDispatchableHandle {
    #[inline]
    fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
        write!(f, "`H:{:#x}`", self.value)
    }
}
impl fmt::Pointer for VkDispatchableHandle {
    #[inline]
    fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
        write!(f, "{:#x}", self.value)
    }
}
impl fmt::LowerHex for VkDispatchableHandle {
    #[inline]
    fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
        write!(f, "{:x}", self.value)
    }
}
impl fmt::UpperHex for VkDispatchableHandle {
    #[inline]
    fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
        write!(f, "{:X}", self.value)
    }
}

impl fmt::Debug for VkNonDispatchableHandle {
    #[inline]
    fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
        write!(f, "`N:{:#x}`", self.value)
    }
}
impl fmt::Display for VkNonDispatchableHandle {
    #[inline]
    fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
        write!(f, "`N:{:#x}`", self.value)
    }
}
impl fmt::Pointer for VkNonDispatchableHandle {
    #[inline]
    fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
        write!(f, "{:#x}", self.value)
    }
}
impl fmt::LowerHex for VkNonDispatchableHandle {
    #[inline]
    fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
        write!(f, "{:x}", self.value)
    }
}
impl fmt::UpperHex for VkNonDispatchableHandle {
    #[inline]
    fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
        write!(f, "{:X}", self.value)
    }
}

impl VkNullHandle for VkDispatchableHandle {
    const NULL : VkDispatchableHandle = VkDispatchableHandle::NULL;

    #[inline]
    fn null() -> VkDispatchableHandle {
        VkDispatchableHandle::NULL
    }
}

impl VkNullHandle for VkNonDispatchableHandle {
    const NULL : VkNonDispatchableHandle = VkNonDispatchableHandle::NULL;

    #[inline]
    fn null() -> VkNonDispatchableHandle {
        VkNonDispatchableHandle::NULL
    }
}

impl Default for VkDispatchableHandle {
    #[inline]
    fn default() -> VkDispatchableHandle {
        VkDispatchableHandle::NULL
    }
}

impl Default for VkNonDispatchableHandle {
    #[inline]
    fn default() -> VkNonDispatchableHandle {
        VkNonDispatchableHandle::NULL
    }
}

pub trait VkDestroyableHandle: VkNullHandle {
    type Owner: Default+Copy+Clone;
    fn destroy(self, owner: Self::Owner, p_allocator: Option<&types::VkAllocationCallbacks>);
}

pub type VkError = ::types::VkResult;

impl VkError {
    #[inline]
    pub fn is_success(self) -> bool{
        return (self as i32) >= 0;
    }
    #[inline]
    pub fn is_error(self) -> bool {
        return (self as i32) < 0;
    }

    #[inline]
    pub fn into_result(self) -> VkResultObj {
        if self.is_error() {
            Err(self)
        } else {
            Ok(self)
        }
    }

    #[inline]
    pub fn from_result(result: VkResultObj) -> Self {
        match result {
            Ok(r) | Err(r) => r,
        }
    }
}

impl fmt::Display for VkError {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{:?}", *self)
    }
}
impl fmt::LowerHex for VkError {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{:x}", *self as u32)
    }
}
impl fmt::UpperHex for VkError {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{:X}", *self as u32)
    }
}

// impl error::Error for VkError {
//     #[inline]
//     fn description(&self) -> &str {
//         return get_VkResult_description(self.0);
//     }
// }

impl Into<String> for VkError {
    #[inline]
    fn into(self) -> String {
        format!("{:?}", self)
    }
}

impl Into<::std::io::Error> for VkError {
    #[inline]
    fn into(self) -> ::std::io::Error {
        ::std::io::Error::new(::std::io::ErrorKind::Other, format!("{:?}", self))
    }
}

impl Into<VkResultObj> for VkError {
    #[inline]
    fn into(self) -> VkResultObj {
        if self.is_error() {
            Err(self)
        } else {
            Ok(self)
        }
    }
}

impl Into<VkResultObj<()>> for VkError {
    #[inline]
    fn into(self) -> VkResultObj<()> {
        if self != ::types::VK_SUCCESS {
            Err(self)
        } else {
            Ok(())
        }
    }
}

impl From<VkResultObj> for VkError {
    #[inline]
    fn from(result: VkResultObj) -> VkError {
        match result {
            Ok(r) | Err(r) => r,
        }
    }
}

impl From<VkResultObj<()>> for VkError {
    #[inline]
    fn from(result: VkResultObj<()>) -> VkError {
        match result {
            Ok(()) => ::types::VK_SUCCESS,
            Err(r) => r,
        }
    }
}

#[cfg_attr(feature="nightly", feature(try_trait))]
#[cfg(feature="nightly")]
impl ops::Try for VkError {
    type Ok = VkError;
    type Error = VkError;

    #[inline]
    fn into_result(self) -> Result<VkError,VkError> {
        if self.is_error() {
            Err(self)
        } else {
            Ok(self)
        }
    }

    #[inline]
    fn from_ok(v: VkError) -> VkError {
        v
    }

    #[inline]
    fn from_error(v: VkError) -> VkError {
        v
    }
}


/// A `std::result::Result` wrapper for `VkResult`.
pub type VkResultObj<T=::types::VkResult> = Result<T, VkError>;

pub use std::ptr::null_mut as vk_null;

/// Support trait for the `vk_null_handle()` function
pub trait VkNullHandle: Sized+PartialEq+Eq+Copy+Clone+Sized {

    const NULL : Self;

    /// Returns a reserved non-valid object handle.
    #[inline]
    fn null() -> Self {
        Self::NULL
    }

    /// tests if the handle is the NULL_HANDLE
    #[inline]
    fn is_null(self) -> bool {
        self == Self::NULL
    }
}

/// Returns a reserved non-valid object handle.
#[inline]
pub fn vk_null_handle<T>() -> T where T: VkNullHandle {
    T::NULL
}

pub unsafe fn extensions_list_to_set<'l>(p: *const *const raw::c_char, len: u32) -> BTreeSet<Cow<'l, str>> {
    let mut extensions : BTreeSet<Cow<str>> = BTreeSet::new();
    if !p.is_null() {
        for ext in ::std::slice::from_raw_parts(p, len as usize) {
            if ext.is_null() {
                break;
            }
            extensions.insert(CStr::from_ptr(*ext).to_string_lossy());
        }
    }
    extensions
}

pub trait VkFlagBits: Copy + Clone + 'static {
    const NONE_VALUE : u32 = 0;
    const ALL_VALUE : u32;
    const NONE : VkFlags<Self> = VkFlags::NONE;
    const ALL : VkFlags<Self> = VkFlags::ALL;

    fn value(self) -> u32;
    fn from_value(value: u32) -> Option<Self>;
    #[inline]
    fn flags(self) -> VkFlags<Self> {
        VkFlags::one(self)
    }
}

#[derive(Copy,Clone,PartialEq,Eq,PartialOrd,Ord,Hash,Debug)]
pub enum VkVoid{}

impl VkFlagBits for VkVoid {
    const ALL_VALUE : u32 = 0;
    #[inline]
    fn value(self) -> u32 {
        unreachable!()
    }
    #[inline]
    fn from_value(_: u32) -> Option<VkVoid> {
        None
    }
}

#[repr(C)]
#[derive(Copy,Clone,PartialEq,Eq,PartialOrd,Ord,Hash)]
pub struct VkFlags<E:VkFlagBits=VkVoid>(u32, ::std::marker::PhantomData<E>);

impl<E:VkFlagBits> VkFlags<E> {

    pub const NONE : VkFlags<E> = VkFlags(E::NONE_VALUE, ::std::marker::PhantomData);
    pub const ALL : VkFlags<E> = VkFlags(E::ALL_VALUE, ::std::marker::PhantomData);

    #[inline]
    pub fn none() -> VkFlags<E> {
        Self::NONE
    }

    #[inline]
    pub fn one(e: E) -> VkFlags<E> {
        VkFlags(e.value(), ::std::marker::PhantomData)
    }

    #[inline]
    pub fn all() -> VkFlags<E> {
        Self::ALL
    }

    #[inline]
    pub fn is_empty(self) -> bool {
        self.0 == 0
    }

    #[inline]
    pub fn is_not_empty(self) -> bool {
        self.0 != 0
    }

    #[inline]
    pub fn contains(self, other: E) -> bool {
        self.0 & other.value() != 0
    }

    #[inline]
    pub fn contains_one_of(self, other: Self) -> bool {
        self.0 & other.0 != 0
    }

    #[inline]
    pub fn contains_all_of(self, other: Self) -> bool {
        self.0 & other.0 == other.0
    }
}

impl<E:VkFlagBits> Default for VkFlags<E> {
    #[inline]
    fn default() -> VkFlags<E> {
        VkFlags::NONE
    }
}

impl<E:VkFlagBits> From<E> for VkFlags<E> {
    #[inline]
    fn from(e: E) -> VkFlags<E> {
        VkFlags::one(e)
    }
}

impl<E:VkFlagBits> ops::BitAnd<E> for VkFlags<E> {
    type Output = VkFlags<E>;
    #[inline]
    fn bitand(self, rhs: E) -> VkFlags<E> {
        VkFlags(self.0 & rhs.value(), ::std::marker::PhantomData)
    }
}
impl<E:VkFlagBits> ops::BitOr<E> for VkFlags<E> {
    type Output = VkFlags<E>;
    #[inline]
    fn bitor(self, rhs: E) -> VkFlags<E> {
        VkFlags(self.0 | rhs.value(), ::std::marker::PhantomData)
    }
}
impl<E:VkFlagBits> ops::BitAnd<VkFlags<E>> for VkFlags<E> {
    type Output = VkFlags<E>;
    #[inline]
    fn bitand(self, rhs: VkFlags<E>) -> VkFlags<E> {
        VkFlags(self.0 & rhs.0, ::std::marker::PhantomData)
    }
}
impl<E:VkFlagBits> ops::BitOr<VkFlags<E>> for VkFlags<E> {
    type Output = VkFlags<E>;
    #[inline]
    fn bitor(self, rhs: VkFlags<E>) -> VkFlags<E> {
        VkFlags(self.0 | rhs.0, ::std::marker::PhantomData)
    }
}

impl<E:VkFlagBits+fmt::Debug> fmt::Debug for VkFlags<E> {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        if self.is_empty() {
            write!(f, "VkFlags[]")
        } else {
            write!(f, "VkFlags[{}]", self)
        }
    }
}
impl<E:VkFlagBits+fmt::Debug> fmt::Display for VkFlags<E> {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        if self.is_empty() {
            write!(f, "(None)")
        } else {
            let value = self.0 & E::ALL_VALUE;
            let mut i = 1;
            let mut n = 0;
            loop {
                if (value & i) != 0 {
                    if let Some(e) = E::from_value(i) {
                        if n > 0 {
                            write!(f, "|")?;
                        }
                        write!(f, "{:?}", e)?;
                        n += 1;
                    }
                }
                i = i << 1;
                if i==0 || i>E::ALL_VALUE {
                    break;
                }
            }
            Ok(())
        }
    }
}
impl<E:VkFlagBits> fmt::LowerHex for VkFlags<E> {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{:x}", self.0)
    }
}
impl<E:VkFlagBits> fmt::UpperHex for VkFlags<E> {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{:X}", self.0)
    }
}
impl<E:VkFlagBits> fmt::Octal for VkFlags<E> {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{:o}", self.0)
    }
}
impl<E:VkFlagBits> fmt::Binary for VkFlags<E> {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{:b}", self.0)
    }
}