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
use std::mem;
use std::ptr;
use std::sync::Arc;
use OomError;
use Success;
use VulkanObject;
use check_errors;
use device::Device;
use device::DeviceOwned;
use vk;
#[derive(Debug)]
pub struct Event {
event: vk::Event,
device: Arc<Device>,
must_put_in_pool: bool,
}
impl Event {
pub fn from_pool(device: Arc<Device>) -> Result<Event, OomError> {
let maybe_raw_event = device.event_pool().lock().unwrap().pop();
match maybe_raw_event {
Some(raw_event) => {
unsafe {
let vk = device.pointers();
check_errors(vk.ResetEvent(device.internal_object(), raw_event))?;
}
Ok(Event {
event: raw_event,
device: device,
must_put_in_pool: true,
})
},
None => {
Event::alloc_impl(device, true)
},
}
}
#[inline]
pub fn alloc(device: Arc<Device>) -> Result<Event, OomError> {
Event::alloc_impl(device, false)
}
fn alloc_impl(device: Arc<Device>, must_put_in_pool: bool) -> Result<Event, OomError> {
let event = unsafe {
static mut INFOS: vk::EventCreateInfo = vk::EventCreateInfo {
sType: vk::STRUCTURE_TYPE_EVENT_CREATE_INFO,
pNext: 0 as *const _,
flags: 0,
};
let mut output = mem::uninitialized();
let vk = device.pointers();
check_errors(vk.CreateEvent(device.internal_object(),
&INFOS,
ptr::null(),
&mut output))?;
output
};
Ok(Event {
device: device,
event: event,
must_put_in_pool: must_put_in_pool,
})
}
#[inline]
pub fn signaled(&self) -> Result<bool, OomError> {
unsafe {
let vk = self.device.pointers();
let result = check_errors(vk.GetEventStatus(self.device.internal_object(),
self.event))?;
match result {
Success::EventSet => Ok(true),
Success::EventReset => Ok(false),
_ => unreachable!(),
}
}
}
#[inline]
pub fn set_raw(&mut self) -> Result<(), OomError> {
unsafe {
let vk = self.device.pointers();
check_errors(vk.SetEvent(self.device.internal_object(), self.event))?;
Ok(())
}
}
#[inline]
pub fn set(&mut self) {
self.set_raw().unwrap();
}
#[inline]
pub fn reset_raw(&mut self) -> Result<(), OomError> {
unsafe {
let vk = self.device.pointers();
check_errors(vk.ResetEvent(self.device.internal_object(), self.event))?;
Ok(())
}
}
#[inline]
pub fn reset(&mut self) {
self.reset_raw().unwrap();
}
}
unsafe impl DeviceOwned for Event {
#[inline]
fn device(&self) -> &Arc<Device> {
&self.device
}
}
unsafe impl VulkanObject for Event {
type Object = vk::Event;
const TYPE: vk::DebugReportObjectTypeEXT = vk::DEBUG_REPORT_OBJECT_TYPE_EVENT_EXT;
#[inline]
fn internal_object(&self) -> vk::Event {
self.event
}
}
impl Drop for Event {
#[inline]
fn drop(&mut self) {
unsafe {
if self.must_put_in_pool {
let raw_event = self.event;
self.device.event_pool().lock().unwrap().push(raw_event);
} else {
let vk = self.device.pointers();
vk.DestroyEvent(self.device.internal_object(), self.event, ptr::null());
}
}
}
}
#[cfg(test)]
mod tests {
use VulkanObject;
use sync::Event;
#[test]
fn event_create() {
let (device, _) = gfx_dev_and_queue!();
let event = Event::alloc(device).unwrap();
assert!(!event.signaled().unwrap());
}
#[test]
fn event_set() {
let (device, _) = gfx_dev_and_queue!();
let mut event = Event::alloc(device).unwrap();
assert!(!event.signaled().unwrap());
event.set();
assert!(event.signaled().unwrap());
}
#[test]
fn event_reset() {
let (device, _) = gfx_dev_and_queue!();
let mut event = Event::alloc(device).unwrap();
event.set();
assert!(event.signaled().unwrap());
event.reset();
assert!(!event.signaled().unwrap());
}
#[test]
fn event_pool() {
let (device, _) = gfx_dev_and_queue!();
assert_eq!(device.event_pool().lock().unwrap().len(), 0);
let event1_internal_obj = {
let event = Event::from_pool(device.clone()).unwrap();
assert_eq!(device.event_pool().lock().unwrap().len(), 0);
event.internal_object()
};
assert_eq!(device.event_pool().lock().unwrap().len(), 1);
let event2 = Event::from_pool(device.clone()).unwrap();
assert_eq!(device.event_pool().lock().unwrap().len(), 0);
assert_eq!(event2.internal_object(), event1_internal_obj);
}
}