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 canvas_traits::webgl::WebGLVersion;
6use dom_struct::dom_struct;
7
8use super::{WebGLExtension, WebGLExtensionSpec, WebGLExtensions};
9use crate::dom::bindings::codegen::Bindings::ANGLEInstancedArraysBinding::{
10    ANGLEInstancedArraysConstants, ANGLEInstancedArraysMethods,
11};
12use crate::dom::bindings::reflector::{DomGlobal, Reflector, reflect_dom_object};
13use crate::dom::bindings::root::{Dom, DomRoot};
14use crate::dom::webgl::webglrenderingcontext::WebGLRenderingContext;
15use crate::script_runtime::CanGc;
16
17#[dom_struct]
18pub(crate) struct ANGLEInstancedArrays {
19    reflector_: Reflector,
20    ctx: Dom<WebGLRenderingContext>,
21}
22
23impl ANGLEInstancedArrays {
24    fn new_inherited(ctx: &WebGLRenderingContext) -> Self {
25        Self {
26            reflector_: Reflector::new(),
27            ctx: Dom::from_ref(ctx),
28        }
29    }
30}
31
32impl WebGLExtension for ANGLEInstancedArrays {
33    type Extension = Self;
34
35    fn new(ctx: &WebGLRenderingContext, can_gc: CanGc) -> DomRoot<Self> {
36        reflect_dom_object(
37            Box::new(ANGLEInstancedArrays::new_inherited(ctx)),
38            &*ctx.global(),
39            can_gc,
40        )
41    }
42
43    fn spec() -> WebGLExtensionSpec {
44        WebGLExtensionSpec::Specific(WebGLVersion::WebGL1)
45    }
46
47    fn is_supported(ext: &WebGLExtensions) -> bool {
48        ext.supports_any_gl_extension(&[
49            "GL_ANGLE_instanced_arrays",
50            "GL_ARB_instanced_arrays",
51            "GL_EXT_instanced_arrays",
52            "GL_NV_instanced_arrays",
53        ])
54    }
55
56    fn enable(ext: &WebGLExtensions) {
57        ext.enable_get_vertex_attrib_name(
58            ANGLEInstancedArraysConstants::VERTEX_ATTRIB_ARRAY_DIVISOR_ANGLE,
59        );
60    }
61
62    fn name() -> &'static str {
63        "ANGLE_instanced_arrays"
64    }
65}
66
67impl ANGLEInstancedArraysMethods<crate::DomTypeHolder> for ANGLEInstancedArrays {
68    // https://www.khronos.org/registry/webgl/extensions/ANGLE_instanced_arrays/
69    fn DrawArraysInstancedANGLE(&self, mode: u32, first: i32, count: i32, primcount: i32) {
70        handle_potential_webgl_error!(
71            self.ctx,
72            self.ctx
73                .draw_arrays_instanced(mode, first, count, primcount)
74        )
75    }
76
77    // https://www.khronos.org/registry/webgl/extensions/ANGLE_instanced_arrays/
78    fn DrawElementsInstancedANGLE(
79        &self,
80        mode: u32,
81        count: i32,
82        type_: u32,
83        offset: i64,
84        primcount: i32,
85    ) {
86        handle_potential_webgl_error!(
87            self.ctx,
88            self.ctx
89                .draw_elements_instanced(mode, count, type_, offset, primcount)
90        )
91    }
92
93    fn VertexAttribDivisorANGLE(&self, index: u32, divisor: u32) {
94        self.ctx.vertex_attrib_divisor(index, divisor);
95    }
96}