Skip to main content

script/dom/webgl/extensions/ext/
angleinstancedarrays.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5use dom_struct::dom_struct;
6use js::context::JSContext;
7use script_bindings::reflector::{Reflector, reflect_dom_object_with_cx};
8use servo_canvas_traits::webgl::WebGLVersion;
9
10use super::{WebGLExtension, WebGLExtensionSpec, WebGLExtensions};
11use crate::dom::bindings::codegen::Bindings::ANGLEInstancedArraysBinding::{
12    ANGLEInstancedArraysConstants, ANGLEInstancedArraysMethods,
13};
14use crate::dom::bindings::reflector::DomGlobal;
15use crate::dom::bindings::root::{Dom, DomRoot};
16use crate::dom::webgl::webglrenderingcontext::WebGLRenderingContext;
17
18#[dom_struct]
19pub(crate) struct ANGLEInstancedArrays {
20    reflector_: Reflector,
21    ctx: Dom<WebGLRenderingContext>,
22}
23
24impl ANGLEInstancedArrays {
25    fn new_inherited(ctx: &WebGLRenderingContext) -> Self {
26        Self {
27            reflector_: Reflector::new(),
28            ctx: Dom::from_ref(ctx),
29        }
30    }
31}
32
33impl WebGLExtension for ANGLEInstancedArrays {
34    type Extension = Self;
35
36    fn new(cx: &mut JSContext, ctx: &WebGLRenderingContext) -> DomRoot<Self> {
37        reflect_dom_object_with_cx(
38            Box::new(ANGLEInstancedArrays::new_inherited(ctx)),
39            &*ctx.global(),
40            cx,
41        )
42    }
43
44    fn spec() -> WebGLExtensionSpec {
45        WebGLExtensionSpec::Specific(WebGLVersion::WebGL1)
46    }
47
48    fn is_supported(ext: &WebGLExtensions) -> bool {
49        ext.supports_any_gl_extension(&[
50            "GL_ANGLE_instanced_arrays",
51            "GL_ARB_instanced_arrays",
52            "GL_EXT_instanced_arrays",
53            "GL_NV_instanced_arrays",
54        ])
55    }
56
57    fn enable(ext: &WebGLExtensions) {
58        ext.enable_get_vertex_attrib_name(
59            ANGLEInstancedArraysConstants::VERTEX_ATTRIB_ARRAY_DIVISOR_ANGLE,
60        );
61    }
62
63    fn name() -> &'static str {
64        "ANGLE_instanced_arrays"
65    }
66}
67
68impl ANGLEInstancedArraysMethods<crate::DomTypeHolder> for ANGLEInstancedArrays {
69    /// <https://www.khronos.org/registry/webgl/extensions/ANGLE_instanced_arrays/>
70    fn DrawArraysInstancedANGLE(
71        &self,
72        cx: &mut JSContext,
73        mode: u32,
74        first: i32,
75        count: i32,
76        primcount: i32,
77    ) {
78        handle_potential_webgl_error!(
79            self.ctx,
80            self.ctx
81                .draw_arrays_instanced(cx, mode, first, count, primcount)
82        )
83    }
84
85    /// <https://www.khronos.org/registry/webgl/extensions/ANGLE_instanced_arrays/>
86    fn DrawElementsInstancedANGLE(
87        &self,
88        cx: &mut JSContext,
89        mode: u32,
90        count: i32,
91        type_: u32,
92        offset: i64,
93        primcount: i32,
94    ) {
95        handle_potential_webgl_error!(
96            self.ctx,
97            self.ctx
98                .draw_elements_instanced(cx, mode, count, type_, offset, primcount)
99        )
100    }
101
102    fn VertexAttribDivisorANGLE(&self, cx: &mut JSContext, index: u32, divisor: u32) {
103        self.ctx.vertex_attrib_divisor(cx, index, divisor);
104    }
105}