webgl_rs/
texture.rs

1//! WebGLTexture and methods
2use glenum::{Attachment, FramebufferKind, TextureBindPoint, TextureKind};
3use rendering_context::WebGL2RenderingContext;
4use wasm_bindgen::prelude::*;
5
6impl WebGL2RenderingContext {
7    /// Creates and initializes a WebGLRSTexture
8    pub fn create_texture(&self) -> WebGLRSTexture {
9        WebGLRSTexture {
10            context: self,
11            inner: self._create_texture(),
12        }
13    }
14}
15
16/// The WebGLTexture interface is part of the WebGL API and represents an opaque texture object providing
17/// storage and state for texturing operations.
18#[derive(Clone)]
19pub struct WebGLRSTexture<'ctx> {
20    context: &'ctx WebGL2RenderingContext,
21    inner: WebGLTexture,
22}
23
24impl<'ctx> WebGLRSTexture<'ctx> {
25    /// Deletes the `WebGLRSTexture` object.
26    pub fn delete(self) {
27        self.context._delete_texture(self.inner);
28    }
29
30    /// Binds the `WebGLRSTexture` to a target
31    ///
32    /// # Arguments
33    /// * `target` - specifying the binding point.
34    pub fn bind(&self, target: TextureKind) {
35        self.context._bind_texture(target, &self.inner);
36    }
37
38    /// Returns true if the `WebGLRSTexture` is valid and false otherwise.
39    pub fn is_valid(&self) -> bool {
40        self.context._is_texture(&self.inner)
41    }
42
43    /// Attaches this `WebGLRSTexture` object to a framebuffer.
44    ///
45    /// # Arguments
46    /// * `target` - specifying the binding point.
47    /// * `attachment` - specifying the attachment point for the texture.
48    /// * `tex_target` - specifying the texture target.
49    /// * `level` - specifying the mipmap level of the texture image to attach.
50    pub fn attach_framebuffer(
51        &self,
52        target: FramebufferKind,
53        attachment: Attachment,
54        tex_target: TextureBindPoint,
55        level: i32,
56    ) {
57        self.context
58            ._framebuffer_texture_2d(target, attachment, tex_target, &self.inner, level);
59    }
60
61    /// Attaches a single layer of this `WebGLRSTexture` object to a framebuffer.TextureBindPoint
62    ///
63    /// # Arguments
64    /// * `target` - specifying the binding point.
65    /// * `attachment` - specifying the attachment point for the texture.
66    /// * `level` - specifying the mipmap level of the texture image to attach.
67    /// * `layer` - specifying the layer of the texture image to attach.
68    pub fn attach_layer_framebuffer(
69        &self,
70        target: FramebufferKind,
71        attachment: Attachment,
72        level: i32,
73        layer: i32,
74    ) {
75        self.context
76            ._framebuffer_texture_layer(target, attachment, &self.inner, level, layer);
77    }
78}
79
80/// Bindings for WebGLTexture
81#[wasm_bindgen]
82#[derive(Clone, Copy)]
83extern "C" {
84    #[derive(Clone)]
85    type WebGLTexture;
86    /// Binding for `WebGLRenderingContext.createTexture()`.
87    #[wasm_bindgen(method, js_name = createTexture)]
88    fn _create_texture(this: &WebGL2RenderingContext) -> WebGLTexture;
89
90    /// Binding for `WebGLRenderingContext.bindTexture()`
91    #[wasm_bindgen(method, js_name = bindTexture)]
92    fn _bind_texture(this: &WebGL2RenderingContext, target: TextureKind, texture: &WebGLTexture);
93
94    /// Binding for `WebGLRenderingContext.deleteTexture()`
95    #[wasm_bindgen(method, js_name = deleteTexture)]
96    fn _delete_texture(this: &WebGL2RenderingContext, texture: WebGLTexture);
97
98    /// Binding for `WebGLRenderingContext.isTexture()`
99    #[wasm_bindgen(method, js_name = isTexture)]
100    fn _is_texture(this: &WebGL2RenderingContext, texture: &WebGLTexture) -> bool;
101
102    /// Binding for `WebGLRenderingContext.framebufferTexture2D()`
103    #[wasm_bindgen(method, js_name = framebufferTexture2D)]
104    fn _framebuffer_texture_2d(
105        this: &WebGL2RenderingContext,
106        target: FramebufferKind,
107        attachment: Attachment,
108        textarget: TextureBindPoint,
109        texture: &WebGLTexture,
110        level: i32,
111    );
112
113    /// Binding for `WebGL2RenderingContext.framebufferTextureLayer()`
114    #[wasm_bindgen(method, js_name = framebufferTextureLayer)]
115    fn _framebuffer_texture_layer(
116        this: &WebGL2RenderingContext,
117        target: FramebufferKind,
118        attachment: Attachment,
119        texture: &WebGLTexture,
120        level: i32,
121        layer: i32,
122    );
123}