webgl_rs/
buffer.rs

1//! WebGLBuffer and methods
2use glenum::{BufferBase, BufferKind};
3use rendering_context::WebGL2RenderingContext;
4use wasm_bindgen::prelude::*;
5
6impl WebGL2RenderingContext {
7    /// Creates a new `WebGLRSBuffer` object which is used for storing data such as vertices or colors.
8    pub fn create_buffer(&self) -> WebGLRSBuffer {
9        WebGLRSBuffer {
10            context: self,
11            inner: self._create_buffer(),
12        }
13    }
14}
15
16/// Buffer which is used for storing data
17///
18/// The `WebGLBuffer` interface is part of the WebGL API and represents an opaque buffer object
19/// storing data such as vertices or colors.
20#[derive(Clone)]
21pub struct WebGLRSBuffer<'ctx> {
22    context: &'ctx WebGL2RenderingContext,
23    inner: WebGLBuffer,
24}
25
26impl<'ctx> WebGLRSBuffer<'ctx> {
27    /// Deletes this `WebGLRSBuffer`
28    pub fn delete(self) {
29        self.context._delete_buffer(self.inner);
30    }
31
32    /// Returns true is the `WebGLRSBuffer` object is valid.
33    pub fn is_valid(&self) -> bool {
34        self.context._is_buffer(&self.inner)
35    }
36
37    /// Binds the buffer to a given target
38    ///
39    /// # Arguments
40    /// * `target` - an enum specifying the binding point.
41    pub fn bind(&self, target: BufferKind) {
42        self.context._bind_buffer(target, &self.inner);
43    }
44
45    /// Binds the `WebGLRSBuffer` to a given binding point (target) at a given index.
46    ///
47    /// # Arguments
48    /// * `target` - an enum specifying the target for the bind operation.
49    /// * `index` - the index of the target.
50    pub fn bind_base(&self, target: BufferBase, index: u32) {
51        self.context._bind_buffer_base(target, index, &self.inner);
52    }
53
54    /// Binds a range of the `WebGLRSBuffer` to a given binding point (target) at a given index.
55    ///
56    /// # Arguments
57    /// * `target` - an enum specifying the target for the bind operation.
58    /// * `index` - the index of the target.
59    /// * `offset` - the starting offset.
60    /// * `size` - the amount of data that can be read from the buffer.
61    pub fn bind_range(&self, target: BufferBase, index: u32, offset: u32, size: u32) {
62        self.context
63            ._bind_buffer_range(target, index, &self.inner, offset, size);
64    }
65}
66
67/// WebGLBuffer bindings
68#[wasm_bindgen]
69#[derive(Clone, Copy)]
70extern "C" {
71    #[derive(Clone)]
72    type WebGLBuffer;
73    /// Binding for `WebGLRenderingContext.createBuffer()`
74    #[wasm_bindgen(method, js_name = createBuffer)]
75    fn _create_buffer(this: &WebGL2RenderingContext) -> WebGLBuffer;
76
77    /// Binding for `WebGLRenderingContext.deleteBuffer()`
78    #[wasm_bindgen(method, js_name = deleteBuffer)]
79    fn _delete_buffer(this: &WebGL2RenderingContext, buffer: WebGLBuffer);
80
81    /// Binding for  `WebGLRenderingContext.isBuffer()`
82    #[wasm_bindgen(method, js_name = isBuffer)]
83    fn _is_buffer(this: &WebGL2RenderingContext, buffer: &WebGLBuffer) -> bool;
84
85    /// Binding for  `WebGLRenderingContext.bindBuffer()`
86    #[wasm_bindgen(method, js_name = bindBuffer)]
87    fn _bind_buffer(this: &WebGL2RenderingContext, target: BufferKind, buffer: &WebGLBuffer);
88
89    /// Binding for `WebGL2RenderingContext.bindBufferBase()`
90    #[wasm_bindgen(method, js_name = bindBufferBase)]
91    fn _bind_buffer_base(
92        this: &WebGL2RenderingContext,
93        target: BufferBase,
94        index: u32,
95        buffer: &WebGLBuffer,
96    );
97
98    /// Binding `WebGL2RenderingContext.bindBufferRange()`
99    #[wasm_bindgen(method, js_name = bindBufferRange)]
100    fn _bind_buffer_range(
101        this: &WebGL2RenderingContext,
102        target: BufferBase,
103        index: u32,
104        buffer: &WebGLBuffer,
105        offset: u32,
106        size: u32,
107    );
108}