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 script_bindings::reflector::{Reflector, reflect_dom_object};
7use servo_canvas_traits::webgl::WebGLVersion;
8
9use super::{WebGLExtension, WebGLExtensionSpec, WebGLExtensions};
10use crate::dom::bindings::codegen::Bindings::ANGLEInstancedArraysBinding::{
11    ANGLEInstancedArraysConstants, ANGLEInstancedArraysMethods,
12};
13use crate::dom::bindings::reflector::DomGlobal;
14use crate::dom::bindings::root::{Dom, DomRoot};
15use crate::dom::webgl::webglrenderingcontext::WebGLRenderingContext;
16use crate::script_runtime::CanGc;
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(ctx: &WebGLRenderingContext, can_gc: CanGc) -> DomRoot<Self> {
37        reflect_dom_object(
38            Box::new(ANGLEInstancedArrays::new_inherited(ctx)),
39            &*ctx.global(),
40            can_gc,
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(&self, mode: u32, first: i32, count: i32, primcount: i32) {
71        handle_potential_webgl_error!(
72            self.ctx,
73            self.ctx
74                .draw_arrays_instanced(mode, first, count, primcount)
75        )
76    }
77
78    /// <https://www.khronos.org/registry/webgl/extensions/ANGLE_instanced_arrays/>
79    fn DrawElementsInstancedANGLE(
80        &self,
81        mode: u32,
82        count: i32,
83        type_: u32,
84        offset: i64,
85        primcount: i32,
86    ) {
87        handle_potential_webgl_error!(
88            self.ctx,
89            self.ctx
90                .draw_elements_instanced(mode, count, type_, offset, primcount)
91        )
92    }
93
94    fn VertexAttribDivisorANGLE(&self, index: u32, divisor: u32) {
95        self.ctx.vertex_attrib_divisor(index, divisor);
96    }
97}