webgl_rs/
sync.rs

1//! WebGLSync and methods
2use glenum::{GPUState, SyncParameter, SyncStatus, WaitStatus};
3use rendering_context::WebGL2RenderingContext;
4use wasm_bindgen::prelude::*;
5
6impl WebGL2RenderingContext {
7    /// Creates a new `WebGLRSSync` object and inserts it into the GL command stream.
8    ///
9    /// # Arguments
10    /// * `conditions` - specifying the condition that must be met to set the sync object's state to.
11    /// * `flags` - specifying a bitwise combination of flags controlling the behavior of the sync object.
12    ///         Must be 0 (exists for extensions only).
13    pub fn fence_sync(&self, conditions: GPUState, flags: u32) -> WebGLRSSync {
14        WebGLRSSync {
15            context: self,
16            inner: self._fence_sync(conditions, flags),
17        }
18    }
19}
20
21#[derive(Clone)]
22pub struct WebGLRSSync<'ctx> {
23    context: &'ctx WebGL2RenderingContext,
24    inner: WebGLSync,
25}
26
27impl<'ctx> WebGLRSSync<'ctx> {
28    /// Deletes this `WebGLRSSync` object
29    pub fn delete(self) {
30        self.context._delete_sync(self.inner);
31    }
32
33    /// Returns true if this is a valid `WebGLRSSync` object.  
34    pub fn is_valid(&self) -> bool {
35        self.context._is_sync(&self.inner)
36    }
37
38    /// Blocks and waits for this `WebGLRSSync` object to become signaled or a given timeout to be passed.
39    ///
40    /// # Arguments
41    /// * `flags` - specifying a bitwise combination of flags controlling the flushing behavior. May be gl.SYNC_FLUSH_COMMANDS_BIT.
42    /// * `timeout` - specifying a timeout (in nanoseconds) for which to wait for the sync object to become signaled. Must not be larger than gl.MAX_CLIENT_WAIT_TIMEOUT_WEBGL.
43    pub fn client_wait(&self, flags: u32, timeout: i64) -> WaitStatus {
44        self.context._client_wait_sync(&self.inner, flags, timeout)
45    }
46
47    /// Returns immediately, but waits on the GL server until the `WebGLRSSync` object is signaled.
48    ///
49    /// The method is a no-op in the absence of the possibility of synchronizing between multiple GL contexts.
50    /// # Arguments
51    /// * `flags` - specifying a bitwise combination of flags controlling the flushing behavior. May be gl.SYNC_FLUSH_COMMANDS_BIT.
52    /// * `timeout` - specifying a timeout (in nanoseconds) for which to wait for the sync object to become signaled. Must not be larger than gl.MAX_CLIENT_WAIT_TIMEOUT_WEBGL.
53    // FIXME: timeout must be gl.TIMEOUT_IGNORED.
54    pub fn wait(&self, flags: u32, timeout: i64) {
55        self.context._wait_sync(&self.inner, flags, timeout);
56    }
57
58    /// Returns the status of this `WebGLRSSync` object.
59    pub fn status(&self) -> SyncStatus {
60        self.context
61            ._get_sync_parameter_status(&self.inner, SyncParameter::Status)
62    }
63}
64
65/// Bindings for WebGLSync
66#[wasm_bindgen]
67#[derive(Clone, Copy)]
68extern "C" {
69    #[derive(Clone)]
70    pub type WebGLSync;
71
72    /// Binding for `WebGL2RenderingContext.fenceSync()`
73    #[wasm_bindgen(method, js_name = fenceSync)]
74    fn _fence_sync(this: &WebGL2RenderingContext, conditions: GPUState, flags: u32) -> WebGLSync;
75
76    /// Binding for `WebGL2RenderingContext.isSync()` method of the WebGL 2 API
77    #[wasm_bindgen(method, js_name = isSync)]
78    fn _is_sync(this: &WebGL2RenderingContext, sync: &WebGLSync) -> bool;
79
80    /// Binding for `WebGL2RenderingContext.deleteSync()`
81    #[wasm_bindgen(method, js_name = deleteSync)]
82    fn _delete_sync(this: &WebGL2RenderingContext, sync: WebGLSync);
83
84    /// Binding for `WebGL2RenderingContext.clientWaitSync()`
85    #[wasm_bindgen(method, js_name = clientWaitSync)]
86    fn _client_wait_sync(
87        this: &WebGL2RenderingContext,
88        sync: &WebGLSync,
89        flags: u32,
90        timeout: i64,
91    ) -> WaitStatus;
92
93    /// Binding for `WebGL2RenderingContext.waitSync()`
94    #[wasm_bindgen(method, js_name = waitSync)]
95    fn _wait_sync(this: &WebGL2RenderingContext, sync: &WebGLSync, flags: u32, timeout: i64);
96
97    /// Binding for `WebGL2RenderingContext.getSyncParameter()` when asking for status
98    #[wasm_bindgen(method, js_name = getSyncParameter)]
99    fn _get_sync_parameter_status(
100        this: &WebGL2RenderingContext,
101        sync: &WebGLSync,
102        pname: SyncParameter,
103    ) -> SyncStatus;
104}