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
//! Vulkan Synchronization Primitives(Fence, Semaphore, Event)

#![cfg_attr(not(feature = "Implements"), allow(dead_code))]

use vk::*;
#[cfg(feature = "Implements")] use VkHandle;
#[cfg(feature = "Implements")] use {VkResultHandler, VkResultBox};

/// Opaque handle to a fence object
pub struct Fence(pub VkFence, ::Device);
/// Opaque handle to a semaphore object
pub struct Semaphore(pub VkSemaphore, ::Device);
/// Opaque handle to a event object
pub struct Event(pub VkEvent, ::Device);

#[cfg(feature = "Implements")] DeviceChildCommonDrop!{
	for Fence[vkDestroyFence], Semaphore[vkDestroySemaphore], Event[vkDestroyEvent]
}
impl ::VkHandle for Fence { type Handle = VkFence; fn native_ptr(&self) -> VkFence { self.0 } }
impl ::VkHandle for Semaphore { type Handle = VkSemaphore; fn native_ptr(&self) -> VkSemaphore { self.0 } }
impl ::VkHandle for Event { type Handle = VkEvent; fn native_ptr(&self) -> VkEvent { self.0 } }
impl ::DeviceChild for Fence { fn device(&self) -> &::Device { &self.1 } }
impl ::DeviceChild for Semaphore { fn device(&self) -> &::Device { &self.1 } }
impl ::DeviceChild for Event { fn device(&self) -> &::Device { &self.1 } }

/// Following methods are enabled with [feature = "Implements"]
#[cfg(feature = "Implements")]
impl Fence
{
	/// Create a new fence object
	/// # Failures
	/// On failure, this command returns
	/// 
	/// * `VK_ERROR_OUT_OF_HOST_MEMORY`
	/// * `VK_ERROR_OUT_OF_DEVICE_MEMORY`
	pub fn new(device: &::Device, signaled: bool) -> ::Result<Self>
	{
		let mut h = VK_NULL_HANDLE as _;
		let flags = if signaled { ::vk::VK_FENCE_CREATE_SIGNALED_BIT } else { 0 };
		unsafe { vkCreateFence(device.native_ptr(), &VkFenceCreateInfo { flags, .. Default::default() }, ::std::ptr::null(), &mut h) }
			.into_result().map(|_| Fence(h, device.clone()))
	}
}
/// Following methods are enabled with [feature = "Implements"]
#[cfg(feature = "Implements")]
impl Semaphore
{
	/// Create a new queue semaphore object
	/// # Failures
	/// On failure, this command returns
	/// 
	/// * `VK_ERROR_OUT_OF_HOST_MEMORY`
	/// * `VK_ERROR_OUT_OF_DEVICE_MEMORY`
	pub fn new(device: &::Device) -> ::Result<Self>
	{
		let mut h = VK_NULL_HANDLE as _;
		unsafe { vkCreateSemaphore(device.native_ptr(), &Default::default(), ::std::ptr::null(), &mut h) }
			.into_result().map(|_| Semaphore(h, device.clone()))
	}
}
/// Following methods are enabled with [feature = "Implements"]
#[cfg(feature = "Implements")]
impl Event
{
	/// Create a new event object
	/// # Failures
	/// On failure, this command returns
	/// 
	/// * `VK_ERROR_OUT_OF_HOST_MEMORY`
	/// * `VK_ERROR_OUT_OF_DEVICE_MEMORY`
	pub fn new(device: &::Device) -> ::Result<Self>
	{
		let mut h = VK_NULL_HANDLE as _;
		unsafe { vkCreateEvent(device.native_ptr(), &Default::default(), ::std::ptr::null(), &mut h) }
			.into_result().map(|_| Event(h, device.clone()))
	}
}

/// Following methods are enabled with [feature = "Implements"]
#[cfg(feature = "Implements")]
impl Fence
{
	/// Wait for one or more fences to become signaled, returns `Ok(true)` if operation is timed out
	/// # Failures
	/// On failure, this command returns
	/// 
	/// * `VK_ERROR_OUT_OF_HOST_MEMORY`
	/// * `VK_ERROR_OUT_OF_DEVICE_MEMORY`
	/// * `VK_ERROR_DEVICE_LOST`
	pub fn wait_multiple(objects: &[&Self], wait_all: bool, timeout: Option<u64>) -> ::Result<bool>
	{
		let objects_ptr = objects.iter().map(|x| x.0).collect::<Vec<_>>();
		let vr = unsafe { ::vk::vkWaitForFences(objects[0].1.native_ptr(), objects_ptr.len() as _, objects_ptr.as_ptr(), wait_all as _, timeout.unwrap_or(::std::u64::MAX)) };
		match vr { ::vk::VK_SUCCESS => Ok(false), ::vk::VK_TIMEOUT => Ok(true), _ => Err(VkResultBox(vr)) }
	}
	/// Wait for a fence to become signaled, returns `Ok(true)` if operation is timed out
	/// # Failures
	/// On failure, this command returns
	/// 
	/// * `VK_ERROR_OUT_OF_HOST_MEMORY`
	/// * `VK_ERROR_OUT_OF_DEVICE_MEMORY`
	/// * `VK_ERROR_DEVICE_LOST`
	pub fn wait_timeout(&self, timeout: u64) -> ::Result<bool>
	{
		let vr = unsafe { ::vk::vkWaitForFences(self.1.native_ptr(), 1, &self.0, false as _, timeout) };
		match vr { ::vk::VK_SUCCESS => Ok(false), ::vk::VK_TIMEOUT => Ok(true), _ => Err(VkResultBox(vr)) }
	}
	/// Resets one or more fence objects
	/// # Failures
	/// On failure, this command returns
	/// 
	/// * `VK_ERROR_OUT_OF_HOST_MEMORY`
	/// * `VK_ERROR_OUT_OF_DEVICE_MEMORY`
	pub fn reset_multiple(objects: &[&Self]) -> ::Result<()>
	{
		let objects_ptr = objects.iter().map(|x| x.0).collect::<Vec<_>>();
		unsafe { ::vk::vkResetFences(objects[0].1.native_ptr(), objects_ptr.len() as _, objects_ptr.as_ptr()) }.into_result()
	}
	/// Resets a fence object
	/// # Failures
	/// On failure, this command returns
	/// 
	/// * `VK_ERROR_OUT_OF_HOST_MEMORY`
	/// * `VK_ERROR_OUT_OF_DEVICE_MEMORY`
	pub fn reset(&self) -> ::Result<()> { unsafe { ::vk::vkResetFences(self.1.native_ptr(), 1, &self.0) }.into_result() }
}
/// Following methods are enabled with [feature = "Implements"]
#[cfg(feature = "Implements")]
impl Event
{
	/// Set an event to signaled state
	/// # Failures
	/// On failure, this command returns
	/// 
	/// * `VK_ERROR_OUT_OF_HOST_MEMORY`
	/// * `VK_ERROR_OUT_OF_DEVICE_MEMORY`
	pub fn set(&self) -> ::Result<()> { unsafe { ::vk::vkSetEvent(self.1.native_ptr(), self.0) }.into_result() }
	/// Reset an event to non-signaled state
	/// # Failures
	/// On failure, this command returns
	/// 
	/// * `VK_ERROR_OUT_OF_HOST_MEMORY`
	/// * `VK_ERROR_OUT_OF_DEVICE_MEMORY`
	pub fn reset(&self) -> ::Result<()> { unsafe { ::vk::vkResetEvent(self.1.native_ptr(), self.0) }.into_result() }
}

#[cfg(feature = "Implements")]
pub trait Status
{
	/// [feature = "Implements"] Retrieve the status(whether is signaled or not) of a synchronize object
	/// # Failures
	/// On failure, this command returns
	/// 
	/// * `VK_ERROR_OUT_OF_HOST_MEMORY`
	/// * `VK_ERROR_OUT_OF_DEVICE_MEMORY`
	/// * `VK_ERROR_DEVICE_LOST`
	fn status(&self) -> ::Result<bool>;
}
#[cfg(feature = "Implements")]
impl Status for Fence
{
	fn status(&self) -> ::Result<bool>
	{
		let vr = unsafe { ::vk::vkGetFenceStatus(self.1.native_ptr(), self.0) };
		match vr { ::vk::VK_SUCCESS => Ok(true), ::vk::VK_NOT_READY => Ok(false), _ => Err(VkResultBox(vr)) }
	}
}
#[cfg(feature = "Implements")]
impl Status for Event
{
	fn status(&self) -> ::Result<bool>
	{
		let vr = unsafe { ::vk::vkGetEventStatus(self.1.native_ptr(), self.0) };
		match vr { ::vk::VK_EVENT_SET => Ok(true), ::vk::VK_EVENT_RESET => Ok(false), _ => Err(VkResultBox(vr)) }
	}
}
#[cfg(feature = "Implements")]
impl ::Waitable for Fence
{
	/// [feature = "Implements"] Wait for a fence to become signaled
	/// # Failures
	/// On failure, this command returns
	/// 
	/// * `VK_ERROR_OUT_OF_HOST_MEMORY`
	/// * `VK_ERROR_OUT_OF_DEVICE_MEMORY`
	/// * `VK_ERROR_DEVICE_LOST`
	fn wait(&self) -> ::Result<()> { self.wait_timeout(::std::u64::MAX).map(|_| ()) }
}

unsafe impl Send for Fence {}
unsafe impl Sync for Fence {}