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
use state::StateRef;
use gl::{self, types::*};
use std::time::Duration;
pub struct Fence{
id: GLsync,
gl: StateRef,
}
impl Drop for Fence{
fn drop(&mut self){
unsafe{ self.gl.DeleteSync(self.id) }
}
}
impl Fence{
pub(crate) fn new(gl: &StateRef) -> Fence{
Fence{
id: unsafe{ gl.FenceSync(gl::SYNC_GPU_COMMANDS_COMPLETE, 0) },
gl: gl.clone(),
}
}
pub fn wait(self) -> ::Result<()>{
let mut flags = 0;
let mut duration = 0;
loop {
let ret = unsafe{ self.gl.ClientWaitSync(self.id, flags, duration) };
if ret == gl::ALREADY_SIGNALED || ret == gl::CONDITION_SATISFIED {
return Ok(())
}
if ret == gl::WAIT_FAILED {
return Err(::Error::from(::ErrorKind::WaitFailed))
}
flags = gl::SYNC_FLUSH_COMMANDS_BIT;
duration = 10_000;
}
}
pub fn try_wait(&mut self, flags: GLenum, duration: Duration) -> ::Result<()>{
let duration = duration.as_nanos();
let ret = unsafe{ self.gl.ClientWaitSync(self.id, flags, duration as u64) };
if ret == gl::ALREADY_SIGNALED || ret == gl::CONDITION_SATISFIED {
Ok(())
}else if ret == gl::WAIT_FAILED {
Err(::Error::from(::ErrorKind::WaitFailed))
}else{
return Err(::Error::from(::ErrorKind::NotReady))
}
}
}