aboutsummaryrefslogtreecommitdiff
path: root/src/sync/semaphore/external_semaphore_handle_type.rs
blob: 35af2fa1d6d9ff8b2594629908ea46a0d22055a9 (plain)
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
// Copyright (c) 2021 The vulkano developers
// Licensed under the Apache License, Version 2.0
// <LICENSE-APACHE or
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT
// license <LICENSE-MIT or https://opensource.org/licenses/MIT>,
// at your option. All files in the project carrying such
// notice may not be copied, modified, or distributed except
// according to those terms.

use std::ops::BitOr;

/// Describes the handle type used for Vulkan external semaphore APIs. This is **not**
/// just a suggestion. Check out VkExternalSemaphoreHandleTypeFlagBits in the Vulkan
/// spec.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub struct ExternalSemaphoreHandleType {
    pub opaque_fd: bool,
    pub opaque_win32: bool,
    pub opaque_win32_kmt: bool,
    pub d3d12_fence: bool,
    pub sync_fd: bool,
}

impl ExternalSemaphoreHandleType {
    /// Builds a `ExternalSemaphoreHandleType` with all values set to false. Useful as a default value.
    ///
    /// # Example
    ///
    /// ```rust
    /// use vulkano::sync::ExternalSemaphoreHandleType as ExternalSemaphoreHandleType;
    ///
    /// let _handle_type = ExternalSemaphoreHandleType {
    ///     opaque_fd: true,
    ///     .. ExternalSemaphoreHandleType::none()
    /// };
    /// ```
    #[inline]
    pub fn none() -> ExternalSemaphoreHandleType {
        ExternalSemaphoreHandleType {
            opaque_fd: false,
            opaque_win32: false,
            opaque_win32_kmt: false,
            d3d12_fence: false,
            sync_fd: false,
        }
    }

    /// Builds an `ExternalSemaphoreHandleType` for a posix file descriptor.
    ///
    /// # Example
    ///
    /// ```rust
    /// use vulkano::sync::ExternalSemaphoreHandleType as ExternalSemaphoreHandleType;
    ///
    /// let _handle_type = ExternalSemaphoreHandleType::posix();
    /// ```
    #[inline]
    pub fn posix() -> ExternalSemaphoreHandleType {
        ExternalSemaphoreHandleType {
            opaque_fd: true,
            ..ExternalSemaphoreHandleType::none()
        }
    }
}

impl From<ExternalSemaphoreHandleType> for ash::vk::ExternalSemaphoreHandleTypeFlags {
    #[inline]
    fn from(val: ExternalSemaphoreHandleType) -> Self {
        let mut result = ash::vk::ExternalSemaphoreHandleTypeFlags::empty();
        if val.opaque_fd {
            result |= ash::vk::ExternalSemaphoreHandleTypeFlags::OPAQUE_FD;
        }
        if val.opaque_win32 {
            result |= ash::vk::ExternalSemaphoreHandleTypeFlags::OPAQUE_WIN32;
        }
        if val.opaque_win32_kmt {
            result |= ash::vk::ExternalSemaphoreHandleTypeFlags::OPAQUE_WIN32_KMT;
        }
        if val.d3d12_fence {
            result |= ash::vk::ExternalSemaphoreHandleTypeFlags::D3D12_FENCE;
        }
        if val.sync_fd {
            result |= ash::vk::ExternalSemaphoreHandleTypeFlags::SYNC_FD;
        }
        result
    }
}

impl BitOr for ExternalSemaphoreHandleType {
    type Output = Self;

    fn bitor(self, rhs: Self) -> Self {
        ExternalSemaphoreHandleType {
            opaque_fd: self.opaque_fd || rhs.opaque_fd,
            opaque_win32: self.opaque_win32 || rhs.opaque_win32,
            opaque_win32_kmt: self.opaque_win32_kmt || rhs.opaque_win32_kmt,
            d3d12_fence: self.d3d12_fence || rhs.d3d12_fence,
            sync_fd: self.sync_fd || rhs.sync_fd,
        }
    }
}