1use glenum::{Attachment, FramebufferKind, TextureBindPoint, TextureKind};
3use rendering_context::WebGL2RenderingContext;
4use wasm_bindgen::prelude::*;
5
6impl WebGL2RenderingContext {
7 pub fn create_texture(&self) -> WebGLRSTexture {
9 WebGLRSTexture {
10 context: self,
11 inner: self._create_texture(),
12 }
13 }
14}
15
16#[derive(Clone)]
19pub struct WebGLRSTexture<'ctx> {
20 context: &'ctx WebGL2RenderingContext,
21 inner: WebGLTexture,
22}
23
24impl<'ctx> WebGLRSTexture<'ctx> {
25 pub fn delete(self) {
27 self.context._delete_texture(self.inner);
28 }
29
30 pub fn bind(&self, target: TextureKind) {
35 self.context._bind_texture(target, &self.inner);
36 }
37
38 pub fn is_valid(&self) -> bool {
40 self.context._is_texture(&self.inner)
41 }
42
43 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 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#[wasm_bindgen]
82#[derive(Clone, Copy)]
83extern "C" {
84 #[derive(Clone)]
85 type WebGLTexture;
86 #[wasm_bindgen(method, js_name = createTexture)]
88 fn _create_texture(this: &WebGL2RenderingContext) -> WebGLTexture;
89
90 #[wasm_bindgen(method, js_name = bindTexture)]
92 fn _bind_texture(this: &WebGL2RenderingContext, target: TextureKind, texture: &WebGLTexture);
93
94 #[wasm_bindgen(method, js_name = deleteTexture)]
96 fn _delete_texture(this: &WebGL2RenderingContext, texture: WebGLTexture);
97
98 #[wasm_bindgen(method, js_name = isTexture)]
100 fn _is_texture(this: &WebGL2RenderingContext, texture: &WebGLTexture) -> bool;
101
102 #[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 #[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}