1use std::cell::Cell;
6use std::cmp;
7use std::ptr::{self, NonNull};
8#[cfg(feature = "webxr")]
9use std::rc::Rc;
10
11#[cfg(feature = "webgl_backtrace")]
12use backtrace::Backtrace;
13use bitflags::bitflags;
14use dom_struct::dom_struct;
15use euclid::default::{Point2D, Rect, Size2D};
16use js::jsapi::{JSContext, JSObject, Type};
17use js::jsval::{BooleanValue, DoubleValue, Int32Value, NullValue, ObjectValue, UInt32Value};
18use js::rust::{CustomAutoRooterGuard, MutableHandleValue};
19use js::typedarray::{
20 ArrayBufferView, CreateWith, Float32, Float32Array, Int32, Int32Array, TypedArray,
21 TypedArrayElementCreator, Uint32Array,
22};
23use pixels::{self, Alpha, PixelFormat, Snapshot, SnapshotPixelFormat};
24use script_bindings::conversions::SafeToJSValConvertible;
25use script_bindings::reflector::AssociatedMemory;
26use serde::{Deserialize, Serialize};
27use servo_base::generic_channel::GenericSharedMemory;
28use servo_base::{Epoch, generic_channel};
29use servo_canvas_traits::webgl::WebGLError::*;
30use servo_canvas_traits::webgl::{
31 AlphaTreatment, GLContextAttributes, GLLimits, GlType, Parameter, SizedDataType, TexDataType,
32 TexFormat, TexParameter, WebGLCommand, WebGLCommandBacktrace, WebGLContextId, WebGLError,
33 WebGLFramebufferBindingRequest, WebGLMsg, WebGLMsgSender, WebGLProgramId, WebGLResult,
34 WebGLSLVersion, WebGLVersion, YAxisTreatment, webgl_channel,
35};
36use servo_config::pref;
37use webrender_api::ImageKey;
38
39use crate::canvas_context::{CanvasContext, HTMLCanvasElementOrOffscreenCanvas};
40use crate::dom::bindings::cell::{DomRefCell, Ref, RefMut};
41use crate::dom::bindings::codegen::Bindings::ANGLEInstancedArraysBinding::ANGLEInstancedArraysConstants;
42use crate::dom::bindings::codegen::Bindings::EXTBlendMinmaxBinding::EXTBlendMinmaxConstants;
43use crate::dom::bindings::codegen::Bindings::OESVertexArrayObjectBinding::OESVertexArrayObjectConstants;
44use crate::dom::bindings::codegen::Bindings::WebGL2RenderingContextBinding::WebGL2RenderingContextConstants;
45use crate::dom::bindings::codegen::Bindings::WebGLRenderingContextBinding::{
46 TexImageSource, WebGLContextAttributes, WebGLRenderingContextConstants as constants,
47 WebGLRenderingContextMethods,
48};
49use crate::dom::bindings::codegen::UnionTypes::{
50 ArrayBufferViewOrArrayBuffer, Float32ArrayOrUnrestrictedFloatSequence,
51 HTMLCanvasElementOrOffscreenCanvas as RootedHTMLCanvasElementOrOffscreenCanvas,
52 Int32ArrayOrLongSequence,
53};
54use crate::dom::bindings::conversions::DerivedFrom;
55use crate::dom::bindings::error::{Error, ErrorResult, Fallible};
56use crate::dom::bindings::inheritance::Castable;
57use crate::dom::bindings::reflector::{DomGlobal, Reflector, reflect_dom_object};
58use crate::dom::bindings::root::{DomOnceCell, DomRoot, MutNullableDom};
59use crate::dom::bindings::str::DOMString;
60use crate::dom::event::{Event, EventBubbles, EventCancelable};
61#[cfg(feature = "webgl_backtrace")]
62use crate::dom::globalscope::GlobalScope;
63use crate::dom::node::NodeTraits;
64#[cfg(feature = "webxr")]
65use crate::dom::promise::Promise;
66use crate::dom::webgl::extensions::WebGLExtensions;
67use crate::dom::webgl::validations::WebGLValidator;
68use crate::dom::webgl::validations::tex_image_2d::{
69 CommonCompressedTexImage2DValidatorResult, CommonTexImage2DValidator,
70 CommonTexImage2DValidatorResult, CompressedTexImage2DValidator,
71 CompressedTexSubImage2DValidator, TexImage2DValidator, TexImage2DValidatorResult,
72};
73use crate::dom::webgl::validations::types::TexImageTarget;
74use crate::dom::webgl::vertexarrayobject::VertexAttribData;
75use crate::dom::webgl::webglactiveinfo::WebGLActiveInfo;
76use crate::dom::webgl::webglbuffer::WebGLBuffer;
77use crate::dom::webgl::webglcontextevent::WebGLContextEvent;
78use crate::dom::webgl::webglframebuffer::{
79 CompleteForRendering, WebGLFramebuffer, WebGLFramebufferAttachmentRoot,
80};
81use crate::dom::webgl::webglobject::WebGLObject;
82use crate::dom::webgl::webglprogram::WebGLProgram;
83use crate::dom::webgl::webglrenderbuffer::WebGLRenderbuffer;
84use crate::dom::webgl::webglshader::WebGLShader;
85use crate::dom::webgl::webglshaderprecisionformat::WebGLShaderPrecisionFormat;
86use crate::dom::webgl::webgltexture::{TexParameterValue, WebGLTexture};
87use crate::dom::webgl::webgluniformlocation::WebGLUniformLocation;
88use crate::dom::webgl::webglvertexarrayobject::WebGLVertexArrayObject;
89use crate::dom::webgl::webglvertexarrayobjectoes::WebGLVertexArrayObjectOES;
90use crate::dom::window::Window;
91use crate::script_runtime::{CanGc, JSContext as SafeJSContext};
92
93macro_rules! handle_object_deletion {
102 ($self_:expr, $binding:expr, $object:ident, $unbind_command:expr) => {
103 if let Some(bound_object) = $binding.get() {
104 if bound_object.id() == $object.id() {
105 $binding.set(None);
106 if let Some(command) = $unbind_command {
107 $self_.send_command(command);
108 }
109 }
110 }
111 };
112}
113
114fn has_invalid_blend_constants(arg1: u32, arg2: u32) -> bool {
115 match (arg1, arg2) {
116 (constants::CONSTANT_COLOR, constants::CONSTANT_ALPHA) => true,
117 (constants::ONE_MINUS_CONSTANT_COLOR, constants::ONE_MINUS_CONSTANT_ALPHA) => true,
118 (constants::ONE_MINUS_CONSTANT_COLOR, constants::CONSTANT_ALPHA) => true,
119 (constants::CONSTANT_COLOR, constants::ONE_MINUS_CONSTANT_ALPHA) => true,
120 (_, _) => false,
121 }
122}
123
124pub(crate) fn uniform_get<T, F>(triple: (&WebGLRenderingContext, WebGLProgramId, i32), f: F) -> T
125where
126 F: FnOnce(WebGLProgramId, i32, generic_channel::GenericSender<T>) -> WebGLCommand,
127 T: for<'de> Deserialize<'de> + Serialize,
128{
129 let (sender, receiver) = webgl_channel().unwrap();
130 triple.0.send_command(f(triple.1, triple.2, sender));
131 receiver.recv().unwrap()
132}
133
134#[expect(unsafe_code)]
135pub(crate) unsafe fn uniform_typed<T>(
136 cx: *mut JSContext,
137 value: &[T::Element],
138 mut retval: MutableHandleValue,
139) where
140 T: TypedArrayElementCreator,
141{
142 rooted!(in(cx) let mut rval = ptr::null_mut::<JSObject>());
143 unsafe {
144 <TypedArray<T, *mut JSObject>>::create(cx, CreateWith::Slice(value), rval.handle_mut())
145 }
146 .unwrap();
147 retval.set(ObjectValue(rval.get()));
148}
149
150#[derive(Clone, Copy, JSTraceable, MallocSizeOf)]
152pub(crate) struct TextureUnpacking(u8);
153
154bitflags! {
155 impl TextureUnpacking: u8 {
156 const FLIP_Y_AXIS = 0x01;
157 const PREMULTIPLY_ALPHA = 0x02;
158 const CONVERT_COLORSPACE = 0x04;
159 }
160}
161
162#[derive(Clone, Copy, Debug, JSTraceable, MallocSizeOf)]
163pub(crate) enum VertexAttrib {
164 Float(f32, f32, f32, f32),
165 Int(i32, i32, i32, i32),
166 Uint(u32, u32, u32, u32),
167}
168
169#[derive(Clone, Copy, Debug)]
170pub(crate) enum Operation {
171 Fallible,
172 Infallible,
173}
174
175#[derive(JSTraceable, MallocSizeOf)]
176struct DroppableWebGLRenderingContext {
177 #[no_trace]
178 webgl_sender: WebGLMsgSender,
179}
180
181impl Drop for DroppableWebGLRenderingContext {
182 fn drop(&mut self) {
183 let _ = self.webgl_sender.send_remove();
184 }
185}
186
187#[dom_struct(associated_memory)]
188pub(crate) struct WebGLRenderingContext {
189 reflector_: Reflector<AssociatedMemory>,
190 #[no_trace]
191 webgl_version: WebGLVersion,
192 #[no_trace]
193 glsl_version: WebGLSLVersion,
194 #[ignore_malloc_size_of = "Defined in surfman"]
195 #[no_trace]
196 limits: GLLimits,
197 canvas: HTMLCanvasElementOrOffscreenCanvas,
198 #[ignore_malloc_size_of = "Defined in servo_canvas_traits"]
199 #[no_trace]
200 last_error: Cell<Option<WebGLError>>,
201 texture_packing_alignment: Cell<u8>,
202 texture_unpacking_settings: Cell<TextureUnpacking>,
203 texture_unpacking_alignment: Cell<u32>,
205 bound_draw_framebuffer: MutNullableDom<WebGLFramebuffer>,
206 bound_read_framebuffer: MutNullableDom<WebGLFramebuffer>,
209 bound_renderbuffer: MutNullableDom<WebGLRenderbuffer>,
210 bound_buffer_array: MutNullableDom<WebGLBuffer>,
211 current_program: MutNullableDom<WebGLProgram>,
212 current_vertex_attribs: DomRefCell<Box<[VertexAttrib]>>,
213 #[ignore_malloc_size_of = "Because it's small"]
214 current_scissor: Cell<(i32, i32, u32, u32)>,
215 #[ignore_malloc_size_of = "Because it's small"]
216 current_clear_color: Cell<(f32, f32, f32, f32)>,
217 #[no_trace]
218 size: Cell<Size2D<u32>>,
219 extension_manager: WebGLExtensions,
220 capabilities: Capabilities,
221 default_vao: DomOnceCell<WebGLVertexArrayObjectOES>,
222 current_vao: MutNullableDom<WebGLVertexArrayObjectOES>,
223 default_vao_webgl2: DomOnceCell<WebGLVertexArrayObject>,
224 current_vao_webgl2: MutNullableDom<WebGLVertexArrayObject>,
225 textures: Textures,
226 #[no_trace]
227 api_type: GlType,
228 droppable: DroppableWebGLRenderingContext,
229}
230
231impl WebGLRenderingContext {
232 #[cfg_attr(crown, expect(crown::unrooted_must_root))]
233 pub(crate) fn new_inherited(
234 window: &Window,
235 canvas: HTMLCanvasElementOrOffscreenCanvas,
236 webgl_version: WebGLVersion,
237 size: Size2D<u32>,
238 attrs: GLContextAttributes,
239 ) -> Result<WebGLRenderingContext, String> {
240 if pref!(webgl_testing_context_creation_error) {
241 return Err("WebGL context creation error forced by pref `webgl.testing.context_creation_error`".into());
242 }
243
244 let webgl_chan = match window.webgl_chan() {
245 Some(chan) => chan,
246 None => return Err("WebGL initialization failed early on".into()),
247 };
248
249 let (sender, receiver) = webgl_channel().unwrap();
250 webgl_chan
251 .send(WebGLMsg::CreateContext(
252 window.webview_id().into(),
253 webgl_version,
254 size,
255 attrs,
256 sender,
257 ))
258 .unwrap();
259 let result = receiver.recv().unwrap();
260
261 result.map(|ctx_data| {
262 let max_combined_texture_image_units = ctx_data.limits.max_combined_texture_image_units;
263 let max_vertex_attribs = ctx_data.limits.max_vertex_attribs as usize;
264 Self {
265 reflector_: Reflector::new(),
266 webgl_version,
267 glsl_version: ctx_data.glsl_version,
268 limits: ctx_data.limits,
269 canvas,
270 last_error: Cell::new(None),
271 texture_packing_alignment: Cell::new(4),
272 texture_unpacking_settings: Cell::new(TextureUnpacking::CONVERT_COLORSPACE),
273 texture_unpacking_alignment: Cell::new(4),
274 bound_draw_framebuffer: MutNullableDom::new(None),
275 bound_read_framebuffer: MutNullableDom::new(None),
276 bound_buffer_array: MutNullableDom::new(None),
277 bound_renderbuffer: MutNullableDom::new(None),
278 current_program: MutNullableDom::new(None),
279 current_vertex_attribs: DomRefCell::new(
280 vec![VertexAttrib::Float(0f32, 0f32, 0f32, 1f32); max_vertex_attribs].into(),
281 ),
282 current_scissor: Cell::new((0, 0, size.width, size.height)),
283 size: Cell::new(size),
286 current_clear_color: Cell::new((0.0, 0.0, 0.0, 0.0)),
287 extension_manager: WebGLExtensions::new(
288 webgl_version,
289 ctx_data.api_type,
290 ctx_data.glsl_version,
291 ),
292 capabilities: Default::default(),
293 default_vao: Default::default(),
294 current_vao: Default::default(),
295 default_vao_webgl2: Default::default(),
296 current_vao_webgl2: Default::default(),
297 textures: Textures::new(max_combined_texture_image_units),
298 api_type: ctx_data.api_type,
299 droppable: DroppableWebGLRenderingContext {
300 webgl_sender: ctx_data.sender,
301 },
302 }
303 })
304 }
305
306 #[cfg_attr(crown, expect(crown::unrooted_must_root))]
307 pub(crate) fn new(
308 window: &Window,
309 canvas: &RootedHTMLCanvasElementOrOffscreenCanvas,
310 webgl_version: WebGLVersion,
311 size: Size2D<u32>,
312 attrs: GLContextAttributes,
313 can_gc: CanGc,
314 ) -> Option<DomRoot<WebGLRenderingContext>> {
315 match WebGLRenderingContext::new_inherited(
316 window,
317 HTMLCanvasElementOrOffscreenCanvas::from(canvas),
318 webgl_version,
319 size,
320 attrs,
321 ) {
322 Ok(ctx) => Some(reflect_dom_object(Box::new(ctx), window, can_gc)),
323 Err(msg) => {
324 error!("Couldn't create WebGLRenderingContext: {}", msg);
325 let event = WebGLContextEvent::new(
326 window,
327 atom!("webglcontextcreationerror"),
328 EventBubbles::DoesNotBubble,
329 EventCancelable::Cancelable,
330 DOMString::from(msg),
331 can_gc,
332 );
333 match canvas {
334 RootedHTMLCanvasElementOrOffscreenCanvas::HTMLCanvasElement(canvas) => {
335 event.upcast::<Event>().fire(canvas.upcast(), can_gc);
336 },
337 RootedHTMLCanvasElementOrOffscreenCanvas::OffscreenCanvas(canvas) => {
338 event.upcast::<Event>().fire(canvas.upcast(), can_gc);
339 },
340 }
341 None
342 },
343 }
344 }
345
346 pub(crate) fn set_image_key(&self, image_key: ImageKey) {
347 self.droppable.webgl_sender.set_image_key(image_key);
348 }
349
350 pub(crate) fn update_rendering(&self, canvas_epoch: Epoch) -> bool {
351 if !self.onscreen() {
352 return false;
353 }
354
355 let global = self.global();
356 let Some(window) = global.downcast::<Window>() else {
357 return false;
358 };
359
360 window
361 .webgl_chan()
362 .expect("Where's the WebGL channel?")
363 .send(WebGLMsg::SwapBuffers(
364 vec![self.context_id()],
365 Some(canvas_epoch),
366 0, ))
368 .is_ok()
369 }
370
371 pub(crate) fn webgl_version(&self) -> WebGLVersion {
372 self.webgl_version
373 }
374
375 pub(crate) fn limits(&self) -> &GLLimits {
376 &self.limits
377 }
378
379 pub(crate) fn texture_unpacking_alignment(&self) -> u32 {
380 self.texture_unpacking_alignment.get()
381 }
382
383 pub(crate) fn bound_draw_framebuffer(&self) -> Option<DomRoot<WebGLFramebuffer>> {
384 self.bound_draw_framebuffer.get()
385 }
386
387 pub(crate) fn current_vao(&self) -> DomRoot<WebGLVertexArrayObjectOES> {
388 self.current_vao.or_init(|| {
389 DomRoot::from_ref(
390 self.default_vao.init_once(|| {
391 WebGLVertexArrayObjectOES::new(self, None, CanGc::deprecated_note())
392 }),
393 )
394 })
395 }
396
397 pub(crate) fn current_vao_webgl2(&self) -> DomRoot<WebGLVertexArrayObject> {
398 self.current_vao_webgl2.or_init(|| {
399 DomRoot::from_ref(
400 self.default_vao_webgl2.init_once(|| {
401 WebGLVertexArrayObject::new(self, None, CanGc::deprecated_note())
402 }),
403 )
404 })
405 }
406
407 pub(crate) fn current_vertex_attribs(&self) -> RefMut<'_, Box<[VertexAttrib]>> {
408 self.current_vertex_attribs.borrow_mut()
409 }
410
411 #[inline]
412 pub(crate) fn sender(&self) -> &WebGLMsgSender {
413 &self.droppable.webgl_sender
414 }
415
416 #[inline]
417 pub(crate) fn send_with_fallibility(&self, command: WebGLCommand, fallibility: Operation) {
418 let result = self
419 .droppable
420 .webgl_sender
421 .send(command, capture_webgl_backtrace());
422 if matches!(fallibility, Operation::Infallible) {
423 result.expect("Operation failed");
424 }
425 }
426
427 #[inline]
428 pub(crate) fn send_command(&self, command: WebGLCommand) {
429 self.send_with_fallibility(command, Operation::Infallible);
430 }
431
432 pub(crate) fn send_command_ignored(&self, command: WebGLCommand) {
433 self.send_with_fallibility(command, Operation::Fallible);
434 }
435
436 pub(crate) fn webgl_error(&self, err: WebGLError) {
437 warn!(
439 "WebGL error: {:?}, previous error was {:?}",
440 err,
441 self.last_error.get()
442 );
443
444 if self.last_error.get().is_none() {
447 self.last_error.set(Some(err));
448 }
449 }
450
451 pub(crate) fn validate_framebuffer(&self) -> WebGLResult<()> {
470 match self.bound_draw_framebuffer.get() {
471 Some(fb) => match fb.check_status_for_rendering() {
472 CompleteForRendering::Complete => Ok(()),
473 CompleteForRendering::Incomplete => Err(InvalidFramebufferOperation),
474 CompleteForRendering::MissingColorAttachment => Err(InvalidOperation),
475 },
476 None => Ok(()),
477 }
478 }
479
480 pub(crate) fn validate_ownership<T>(&self, object: &T) -> WebGLResult<()>
481 where
482 T: DerivedFrom<WebGLObject>,
483 {
484 let Some(context) = object.upcast().context() else {
485 return Err(WebGLError::InvalidOperation);
486 };
487 if !std::ptr::eq(self, &*context) {
488 return Err(WebGLError::InvalidOperation);
489 }
490 Ok(())
491 }
492
493 pub(crate) fn with_location<F>(&self, location: Option<&WebGLUniformLocation>, f: F)
494 where
495 F: FnOnce(&WebGLUniformLocation) -> WebGLResult<()>,
496 {
497 let location = match location {
498 Some(loc) => loc,
499 None => return,
500 };
501 match self.current_program.get() {
502 Some(ref program)
503 if program.id() == location.program_id() &&
504 program.link_generation() == location.link_generation() => {},
505 _ => return self.webgl_error(InvalidOperation),
506 }
507 handle_potential_webgl_error!(self, f(location));
508 }
509
510 pub(crate) fn textures(&self) -> &Textures {
511 &self.textures
512 }
513
514 fn tex_parameter(&self, target: u32, param: u32, value: TexParameterValue) {
515 let texture_slot = handle_potential_webgl_error!(
516 self,
517 self.textures
518 .active_texture_slot(target, self.webgl_version()),
519 return
520 );
521 let texture =
522 handle_potential_webgl_error!(self, texture_slot.get().ok_or(InvalidOperation), return);
523
524 if !self
525 .extension_manager
526 .is_get_tex_parameter_name_enabled(param)
527 {
528 return self.webgl_error(InvalidEnum);
529 }
530
531 handle_potential_webgl_error!(self, texture.tex_parameter(param, value), return);
532
533 if target != constants::TEXTURE_2D {
535 return;
536 }
537
538 let target = TexImageTarget::Texture2D;
539 if let Some(info) = texture.image_info_for_target(&target, 0) {
540 self.validate_filterable_texture(
541 &texture,
542 target,
543 0,
544 info.internal_format(),
545 Size2D::new(info.width(), info.height()),
546 info.data_type().unwrap_or(TexDataType::UnsignedByte),
547 );
548 }
549 }
550
551 fn vertex_attrib(&self, indx: u32, x: f32, y: f32, z: f32, w: f32) {
552 if indx >= self.limits.max_vertex_attribs {
553 return self.webgl_error(InvalidValue);
554 }
555
556 match self.webgl_version() {
557 WebGLVersion::WebGL1 => self
558 .current_vao()
559 .set_vertex_attrib_type(indx, constants::FLOAT),
560 WebGLVersion::WebGL2 => self
561 .current_vao_webgl2()
562 .set_vertex_attrib_type(indx, constants::FLOAT),
563 };
564 self.current_vertex_attribs.borrow_mut()[indx as usize] = VertexAttrib::Float(x, y, z, w);
565
566 self.send_command(WebGLCommand::VertexAttrib(indx, x, y, z, w));
567 }
568
569 pub(crate) fn get_current_framebuffer_size(&self) -> Option<(i32, i32)> {
570 match self.bound_draw_framebuffer.get() {
571 Some(fb) => fb.size(),
572
573 None => Some((self.DrawingBufferWidth(), self.DrawingBufferHeight())),
575 }
576 }
577
578 pub(crate) fn get_texture_packing_alignment(&self) -> u8 {
579 self.texture_packing_alignment.get()
580 }
581
582 pub(crate) fn get_current_unpack_state(
583 &self,
584 premultiplied: Alpha,
585 ) -> (Option<AlphaTreatment>, YAxisTreatment) {
586 let settings = self.texture_unpacking_settings.get();
587 let dest_premultiplied = settings.contains(TextureUnpacking::PREMULTIPLY_ALPHA);
588
589 let alpha_treatment = match (premultiplied, dest_premultiplied) {
590 (Alpha::Premultiplied, false) => Some(AlphaTreatment::Unmultiply),
591 (Alpha::NotPremultiplied, true) => Some(AlphaTreatment::Premultiply),
592 _ => None,
593 };
594
595 let y_axis_treatment = if settings.contains(TextureUnpacking::FLIP_Y_AXIS) {
596 YAxisTreatment::Flipped
597 } else {
598 YAxisTreatment::AsIs
599 };
600
601 (alpha_treatment, y_axis_treatment)
602 }
603
604 fn validate_filterable_texture(
607 &self,
608 texture: &WebGLTexture,
609 target: TexImageTarget,
610 level: u32,
611 internal_format: TexFormat,
612 size: Size2D<u32>,
613 data_type: TexDataType,
614 ) -> bool {
615 if self
616 .extension_manager
617 .is_filterable(data_type.as_gl_constant()) ||
618 !texture.is_using_linear_filtering()
619 {
620 return true;
621 }
622
623 let data_type = TexDataType::UnsignedByte;
626 let expected_byte_length = size.area() * 4;
627 let mut pixels = vec![0u8; expected_byte_length as usize];
628 for rgba8 in pixels.chunks_mut(4) {
629 rgba8[3] = 255u8;
630 }
631
632 self.tex_image_2d(
636 texture,
637 target,
638 data_type,
639 internal_format,
640 internal_format.to_unsized(),
641 level,
642 0,
643 1,
644 size,
645 TexSource::Pixels(TexPixels::new(
646 GenericSharedMemory::from_bytes(&pixels),
647 size,
648 PixelFormat::RGBA8,
649 None,
650 YAxisTreatment::AsIs,
651 )),
652 );
653
654 false
655 }
656
657 fn validate_stencil_actions(&self, action: u32) -> bool {
658 matches!(
659 action,
660 0 | constants::KEEP |
661 constants::REPLACE |
662 constants::INCR |
663 constants::DECR |
664 constants::INVERT |
665 constants::INCR_WRAP |
666 constants::DECR_WRAP
667 )
668 }
669
670 pub(crate) fn get_image_pixels(&self, source: TexImageSource) -> Fallible<Option<TexPixels>> {
671 Ok(Some(match source {
672 TexImageSource::ImageBitmap(bitmap) => {
673 if !bitmap.origin_is_clean() {
674 return Err(Error::Security(None));
675 }
676
677 let Some(snapshot) = bitmap.bitmap_data().clone() else {
678 return Ok(None);
679 };
680
681 let snapshot = snapshot.to_shared();
682 let size = snapshot.size().cast();
683 let format = match snapshot.format() {
684 SnapshotPixelFormat::RGBA => PixelFormat::RGBA8,
685 SnapshotPixelFormat::BGRA => PixelFormat::BGRA8,
686 };
687
688 TexPixels::new(
695 snapshot.shared_memory(),
696 size,
697 format,
698 None,
699 YAxisTreatment::AsIs,
700 )
701 },
702 TexImageSource::ImageData(image_data) => {
703 let (alpha_treatment, y_axis_treatment) =
704 self.get_current_unpack_state(Alpha::NotPremultiplied);
705
706 TexPixels::new(
707 image_data.to_shared_memory(),
708 image_data.get_size(),
709 PixelFormat::RGBA8,
710 alpha_treatment,
711 y_axis_treatment,
712 )
713 },
714 TexImageSource::HTMLImageElement(image) => {
715 let document = match self.canvas {
716 HTMLCanvasElementOrOffscreenCanvas::HTMLCanvasElement(ref canvas) => {
717 canvas.owner_document()
718 },
719 HTMLCanvasElementOrOffscreenCanvas::OffscreenCanvas(ref _canvas) => {
720 return Ok(None);
722 },
723 };
724 if !image.same_origin(&document.origin()) {
725 return Err(Error::Security(None));
726 }
727
728 let Some(snapshot) = image.get_raster_image_data() else {
732 return Ok(None);
733 };
734
735 let snapshot = snapshot.to_shared();
736 let size = snapshot.size().cast();
737 let format: PixelFormat = match snapshot.format() {
738 SnapshotPixelFormat::RGBA => PixelFormat::RGBA8,
739 SnapshotPixelFormat::BGRA => PixelFormat::BGRA8,
740 };
741
742 let (alpha_treatment, y_axis_treatment) =
743 self.get_current_unpack_state(snapshot.alpha_mode().alpha());
744
745 TexPixels::new(
746 snapshot.shared_memory(),
747 size,
748 format,
749 alpha_treatment,
750 y_axis_treatment,
751 )
752 },
753 TexImageSource::HTMLCanvasElement(canvas) => {
757 if !canvas.origin_is_clean() {
758 return Err(Error::Security(None));
759 }
760
761 let Some(snapshot) = canvas.get_image_data() else {
762 return Ok(None);
763 };
764
765 let snapshot = snapshot.to_shared();
766 let size = snapshot.size().cast();
767 let format = match snapshot.format() {
768 SnapshotPixelFormat::RGBA => PixelFormat::RGBA8,
769 SnapshotPixelFormat::BGRA => PixelFormat::BGRA8,
770 };
771
772 let (alpha_treatment, y_axis_treatment) =
773 self.get_current_unpack_state(snapshot.alpha_mode().alpha());
774
775 TexPixels::new(
776 snapshot.shared_memory(),
777 size,
778 format,
779 alpha_treatment,
780 y_axis_treatment,
781 )
782 },
783 TexImageSource::HTMLVideoElement(video) => {
784 if !video.origin_is_clean() {
785 return Err(Error::Security(None));
786 }
787
788 let Some(snapshot) = video.get_current_frame_data() else {
789 return Ok(None);
790 };
791
792 let snapshot = snapshot.to_shared();
793 let size = snapshot.size().cast();
794 let format: PixelFormat = match snapshot.format() {
795 SnapshotPixelFormat::RGBA => PixelFormat::RGBA8,
796 SnapshotPixelFormat::BGRA => PixelFormat::BGRA8,
797 };
798
799 let (alpha_treatment, y_axis_treatment) =
800 self.get_current_unpack_state(snapshot.alpha_mode().alpha());
801
802 TexPixels::new(
803 snapshot.shared_memory(),
804 size,
805 format,
806 alpha_treatment,
807 y_axis_treatment,
808 )
809 },
810 }))
811 }
812
813 pub(crate) fn validate_tex_image_2d_data(
815 &self,
816 width: u32,
817 height: u32,
818 format: TexFormat,
819 data_type: TexDataType,
820 unpacking_alignment: u32,
821 data: Option<&ArrayBufferView>,
822 ) -> Result<u32, ()> {
823 let element_size = data_type.element_size();
824 let components_per_element = data_type.components_per_element();
825 let components = format.components();
826
827 let data_type_matches = data.as_ref().is_none_or(|buffer| {
835 Some(data_type.sized_data_type()) ==
836 array_buffer_type_to_sized_type(buffer.get_array_type()) &&
837 data_type.required_webgl_version() <= self.webgl_version()
838 });
839
840 if !data_type_matches {
841 self.webgl_error(InvalidOperation);
842 return Err(());
843 }
844
845 if height == 0 {
847 Ok(0)
848 } else {
849 let cpp = element_size * components / components_per_element;
854 let stride = (width * cpp + unpacking_alignment - 1) & !(unpacking_alignment - 1);
855 Ok(stride * (height - 1) + width * cpp)
856 }
857 }
858
859 #[allow(clippy::too_many_arguments)]
860 pub(crate) fn tex_image_2d(
861 &self,
862 texture: &WebGLTexture,
863 target: TexImageTarget,
864 data_type: TexDataType,
865 internal_format: TexFormat,
866 format: TexFormat,
867 level: u32,
868 _border: u32,
869 unpacking_alignment: u32,
870 size: Size2D<u32>,
871 source: TexSource,
872 ) {
873 handle_potential_webgl_error!(
875 self,
876 texture.initialize(
877 target,
878 size.width,
879 size.height,
880 1,
881 format,
882 level,
883 Some(data_type)
884 )
885 );
886
887 let internal_format = self
888 .extension_manager
889 .get_effective_tex_internal_format(internal_format, data_type.as_gl_constant());
890
891 let effective_data_type = self
892 .extension_manager
893 .effective_type(data_type.as_gl_constant());
894
895 match source {
896 TexSource::Pixels(pixels) => {
897 self.send_command(WebGLCommand::TexImage2D {
899 target: target.as_gl_constant(),
900 level,
901 internal_format,
902 size,
903 format,
904 data_type,
905 effective_data_type,
906 unpacking_alignment,
907 alpha_treatment: pixels.alpha_treatment,
908 y_axis_treatment: pixels.y_axis_treatment,
909 pixel_format: pixels.pixel_format,
910 data: pixels.data.into(),
911 });
912 },
913 TexSource::BufferOffset(offset) => {
914 self.send_command(WebGLCommand::TexImage2DPBO {
915 target: target.as_gl_constant(),
916 level,
917 internal_format,
918 size,
919 format,
920 effective_data_type,
921 unpacking_alignment,
922 offset,
923 });
924 },
925 }
926
927 if let Some(fb) = self.bound_draw_framebuffer.get() {
928 fb.invalidate_texture(texture);
929 }
930 }
931
932 #[allow(clippy::too_many_arguments)]
933 fn tex_sub_image_2d(
934 &self,
935 texture: DomRoot<WebGLTexture>,
936 target: TexImageTarget,
937 level: u32,
938 xoffset: i32,
939 yoffset: i32,
940 format: TexFormat,
941 data_type: TexDataType,
942 unpacking_alignment: u32,
943 pixels: TexPixels,
944 ) {
945 let image_info = match texture.image_info_for_target(&target, level) {
947 Some(info) => info,
948 None => return self.webgl_error(InvalidOperation),
949 };
950
951 if xoffset < 0 ||
956 (xoffset as u32 + pixels.size().width) > image_info.width() ||
957 yoffset < 0 ||
958 (yoffset as u32 + pixels.size().height) > image_info.height()
959 {
960 return self.webgl_error(InvalidValue);
961 }
962
963 debug_assert!(!format.is_sized());
965 if format != image_info.internal_format().to_unsized() {
966 return self.webgl_error(InvalidOperation);
967 }
968
969 if self.webgl_version() == WebGLVersion::WebGL1 &&
971 data_type != image_info.data_type().unwrap()
972 {
973 return self.webgl_error(InvalidOperation);
974 }
975
976 let effective_data_type = self
977 .extension_manager
978 .effective_type(data_type.as_gl_constant());
979
980 self.send_command(WebGLCommand::TexSubImage2D {
982 target: target.as_gl_constant(),
983 level,
984 xoffset,
985 yoffset,
986 size: pixels.size(),
987 format,
988 data_type,
989 effective_data_type,
990 unpacking_alignment,
991 alpha_treatment: pixels.alpha_treatment,
992 y_axis_treatment: pixels.y_axis_treatment,
993 pixel_format: pixels.pixel_format,
994 data: pixels.data.into(),
995 });
996 }
997
998 fn get_gl_extensions(&self) -> String {
999 let (sender, receiver) = webgl_channel().unwrap();
1000 self.send_command(WebGLCommand::GetExtensions(sender));
1001 receiver.recv().unwrap()
1002 }
1003
1004 pub(crate) fn draw_arrays_instanced(
1006 &self,
1007 mode: u32,
1008 first: i32,
1009 count: i32,
1010 primcount: i32,
1011 ) -> WebGLResult<()> {
1012 match mode {
1013 constants::POINTS |
1014 constants::LINE_STRIP |
1015 constants::LINE_LOOP |
1016 constants::LINES |
1017 constants::TRIANGLE_STRIP |
1018 constants::TRIANGLE_FAN |
1019 constants::TRIANGLES => {},
1020 _ => {
1021 return Err(InvalidEnum);
1022 },
1023 }
1024 if first < 0 || count < 0 || primcount < 0 {
1025 return Err(InvalidValue);
1026 }
1027
1028 let current_program = self.current_program.get().ok_or(InvalidOperation)?;
1029
1030 let required_len = if count > 0 {
1031 first
1032 .checked_add(count)
1033 .map(|len| len as u32)
1034 .ok_or(InvalidOperation)?
1035 } else {
1036 0
1037 };
1038
1039 match self.webgl_version() {
1040 WebGLVersion::WebGL1 => self.current_vao().validate_for_draw(
1041 required_len,
1042 primcount as u32,
1043 ¤t_program.active_attribs(),
1044 )?,
1045 WebGLVersion::WebGL2 => self.current_vao_webgl2().validate_for_draw(
1046 required_len,
1047 primcount as u32,
1048 ¤t_program.active_attribs(),
1049 )?,
1050 };
1051
1052 self.validate_framebuffer()?;
1053
1054 if count == 0 || primcount == 0 {
1055 return Ok(());
1056 }
1057
1058 self.send_command(if primcount == 1 {
1059 WebGLCommand::DrawArrays { mode, first, count }
1060 } else {
1061 WebGLCommand::DrawArraysInstanced {
1062 mode,
1063 first,
1064 count,
1065 primcount,
1066 }
1067 });
1068 self.mark_as_dirty();
1069 Ok(())
1070 }
1071
1072 pub(crate) fn draw_elements_instanced(
1074 &self,
1075 mode: u32,
1076 count: i32,
1077 type_: u32,
1078 offset: i64,
1079 primcount: i32,
1080 ) -> WebGLResult<()> {
1081 match mode {
1082 constants::POINTS |
1083 constants::LINE_STRIP |
1084 constants::LINE_LOOP |
1085 constants::LINES |
1086 constants::TRIANGLE_STRIP |
1087 constants::TRIANGLE_FAN |
1088 constants::TRIANGLES => {},
1089 _ => {
1090 return Err(InvalidEnum);
1091 },
1092 }
1093 if count < 0 || offset < 0 || primcount < 0 {
1094 return Err(InvalidValue);
1095 }
1096 let type_size = match type_ {
1097 constants::UNSIGNED_BYTE => 1,
1098 constants::UNSIGNED_SHORT => 2,
1099 constants::UNSIGNED_INT => match self.webgl_version() {
1100 WebGLVersion::WebGL1 if self.extension_manager.is_element_index_uint_enabled() => 4,
1101 WebGLVersion::WebGL2 => 4,
1102 _ => return Err(InvalidEnum),
1103 },
1104 _ => return Err(InvalidEnum),
1105 };
1106 if offset % type_size != 0 {
1107 return Err(InvalidOperation);
1108 }
1109
1110 let current_program = self.current_program.get().ok_or(InvalidOperation)?;
1111 let array_buffer = match self.webgl_version() {
1112 WebGLVersion::WebGL1 => self.current_vao().element_array_buffer().get(),
1113 WebGLVersion::WebGL2 => self.current_vao_webgl2().element_array_buffer().get(),
1114 }
1115 .ok_or(InvalidOperation)?;
1116
1117 if count > 0 && primcount > 0 {
1118 let val = offset as u64 + (count as u64 * type_size as u64);
1120 if val > array_buffer.capacity() as u64 {
1121 return Err(InvalidOperation);
1122 }
1123 }
1124
1125 match self.webgl_version() {
1127 WebGLVersion::WebGL1 => self.current_vao().validate_for_draw(
1128 0,
1129 primcount as u32,
1130 ¤t_program.active_attribs(),
1131 )?,
1132 WebGLVersion::WebGL2 => self.current_vao_webgl2().validate_for_draw(
1133 0,
1134 primcount as u32,
1135 ¤t_program.active_attribs(),
1136 )?,
1137 };
1138
1139 self.validate_framebuffer()?;
1140
1141 if count == 0 || primcount == 0 {
1142 return Ok(());
1143 }
1144
1145 let offset = offset as u32;
1146 self.send_command(if primcount == 1 {
1147 WebGLCommand::DrawElements {
1148 mode,
1149 count,
1150 type_,
1151 offset,
1152 }
1153 } else {
1154 WebGLCommand::DrawElementsInstanced {
1155 mode,
1156 count,
1157 type_,
1158 offset,
1159 primcount,
1160 }
1161 });
1162 self.mark_as_dirty();
1163 Ok(())
1164 }
1165
1166 pub(crate) fn vertex_attrib_divisor(&self, index: u32, divisor: u32) {
1167 if index >= self.limits.max_vertex_attribs {
1168 return self.webgl_error(InvalidValue);
1169 }
1170
1171 match self.webgl_version() {
1172 WebGLVersion::WebGL1 => self.current_vao().vertex_attrib_divisor(index, divisor),
1173 WebGLVersion::WebGL2 => self
1174 .current_vao_webgl2()
1175 .vertex_attrib_divisor(index, divisor),
1176 };
1177 self.send_command(WebGLCommand::VertexAttribDivisor { index, divisor });
1178 }
1179
1180 pub(crate) fn array_buffer(&self) -> Option<DomRoot<WebGLBuffer>> {
1181 self.bound_buffer_array.get()
1182 }
1183
1184 pub(crate) fn array_buffer_slot(&self) -> &MutNullableDom<WebGLBuffer> {
1185 &self.bound_buffer_array
1186 }
1187
1188 pub(crate) fn bound_buffer(&self, target: u32) -> WebGLResult<Option<DomRoot<WebGLBuffer>>> {
1189 match target {
1190 constants::ARRAY_BUFFER => Ok(self.bound_buffer_array.get()),
1191 constants::ELEMENT_ARRAY_BUFFER => Ok(self.current_vao().element_array_buffer().get()),
1192 _ => Err(WebGLError::InvalidEnum),
1193 }
1194 }
1195
1196 pub(crate) fn buffer_usage(&self, usage: u32) -> WebGLResult<u32> {
1197 match usage {
1198 constants::STREAM_DRAW | constants::STATIC_DRAW | constants::DYNAMIC_DRAW => Ok(usage),
1199 _ => Err(WebGLError::InvalidEnum),
1200 }
1201 }
1202
1203 pub(crate) fn create_vertex_array(&self) -> Option<DomRoot<WebGLVertexArrayObjectOES>> {
1204 let (sender, receiver) = webgl_channel().unwrap();
1205 self.send_command(WebGLCommand::CreateVertexArray(sender));
1206 receiver
1207 .recv()
1208 .unwrap()
1209 .map(|id| WebGLVertexArrayObjectOES::new(self, Some(id), CanGc::deprecated_note()))
1210 }
1211
1212 pub(crate) fn create_vertex_array_webgl2(&self) -> Option<DomRoot<WebGLVertexArrayObject>> {
1213 let (sender, receiver) = webgl_channel().unwrap();
1214 self.send_command(WebGLCommand::CreateVertexArray(sender));
1215 receiver
1216 .recv()
1217 .unwrap()
1218 .map(|id| WebGLVertexArrayObject::new(self, Some(id), CanGc::deprecated_note()))
1219 }
1220
1221 pub(crate) fn delete_vertex_array(&self, vao: Option<&WebGLVertexArrayObjectOES>) {
1222 if let Some(vao) = vao {
1223 handle_potential_webgl_error!(self, self.validate_ownership(vao), return);
1224 assert!(vao.id().is_some());
1226 if vao.is_deleted() {
1227 return;
1228 }
1229 if vao == &*self.current_vao() {
1230 self.current_vao.set(None);
1233 self.send_command(WebGLCommand::BindVertexArray(None));
1234 }
1235 vao.delete(Operation::Infallible);
1236 }
1237 }
1238
1239 pub(crate) fn delete_vertex_array_webgl2(&self, vao: Option<&WebGLVertexArrayObject>) {
1240 if let Some(vao) = vao {
1241 handle_potential_webgl_error!(self, self.validate_ownership(vao), return);
1242 assert!(vao.id().is_some());
1244 if vao.is_deleted() {
1245 return;
1246 }
1247 if vao == &*self.current_vao_webgl2() {
1248 self.current_vao_webgl2.set(None);
1251 self.send_command(WebGLCommand::BindVertexArray(None));
1252 }
1253 vao.delete(Operation::Infallible);
1254 }
1255 }
1256
1257 pub(crate) fn is_vertex_array(&self, vao: Option<&WebGLVertexArrayObjectOES>) -> bool {
1258 vao.is_some_and(|vao| {
1259 assert!(vao.id().is_some());
1261 self.validate_ownership(vao).is_ok() && vao.ever_bound() && !vao.is_deleted()
1262 })
1263 }
1264
1265 pub(crate) fn is_vertex_array_webgl2(&self, vao: Option<&WebGLVertexArrayObject>) -> bool {
1266 vao.is_some_and(|vao| {
1267 assert!(vao.id().is_some());
1269 self.validate_ownership(vao).is_ok() && vao.ever_bound() && !vao.is_deleted()
1270 })
1271 }
1272
1273 pub(crate) fn bind_vertex_array(&self, vao: Option<&WebGLVertexArrayObjectOES>) {
1274 if let Some(vao) = vao {
1275 assert!(vao.id().is_some());
1277 handle_potential_webgl_error!(self, self.validate_ownership(vao), return);
1278 if vao.is_deleted() {
1279 return self.webgl_error(InvalidOperation);
1280 }
1281 vao.set_ever_bound();
1282 }
1283 self.send_command(WebGLCommand::BindVertexArray(vao.and_then(|vao| vao.id())));
1284 self.current_vao.set(vao);
1287 }
1288
1289 pub(crate) fn bind_vertex_array_webgl2(&self, vao: Option<&WebGLVertexArrayObject>) {
1290 if let Some(vao) = vao {
1291 assert!(vao.id().is_some());
1293 handle_potential_webgl_error!(self, self.validate_ownership(vao), return);
1294 if vao.is_deleted() {
1295 return self.webgl_error(InvalidOperation);
1296 }
1297 vao.set_ever_bound();
1298 }
1299 self.send_command(WebGLCommand::BindVertexArray(vao.and_then(|vao| vao.id())));
1300 self.current_vao_webgl2.set(vao);
1303 }
1304
1305 fn validate_blend_mode(&self, mode: u32) -> WebGLResult<()> {
1306 match mode {
1307 constants::FUNC_ADD | constants::FUNC_SUBTRACT | constants::FUNC_REVERSE_SUBTRACT => {
1308 Ok(())
1309 },
1310 EXTBlendMinmaxConstants::MIN_EXT | EXTBlendMinmaxConstants::MAX_EXT
1311 if self.extension_manager.is_blend_minmax_enabled() =>
1312 {
1313 Ok(())
1314 },
1315 _ => Err(InvalidEnum),
1316 }
1317 }
1318
1319 pub(crate) fn initialize_framebuffer(&self, clear_bits: u32) {
1320 if clear_bits == 0 {
1321 return;
1322 }
1323 self.send_command(WebGLCommand::InitializeFramebuffer {
1324 color: clear_bits & constants::COLOR_BUFFER_BIT != 0,
1325 depth: clear_bits & constants::DEPTH_BUFFER_BIT != 0,
1326 stencil: clear_bits & constants::STENCIL_BUFFER_BIT != 0,
1327 });
1328 }
1329
1330 pub(crate) fn extension_manager(&self) -> &WebGLExtensions {
1331 &self.extension_manager
1332 }
1333
1334 #[expect(unsafe_code)]
1335 pub(crate) fn buffer_data(
1336 &self,
1337 target: u32,
1338 data: Option<ArrayBufferViewOrArrayBuffer>,
1339 usage: u32,
1340 bound_buffer: Option<DomRoot<WebGLBuffer>>,
1341 ) {
1342 let data = handle_potential_webgl_error!(self, data.ok_or(InvalidValue), return);
1343 let bound_buffer =
1344 handle_potential_webgl_error!(self, bound_buffer.ok_or(InvalidOperation), return);
1345
1346 let data = unsafe {
1347 match data {
1349 ArrayBufferViewOrArrayBuffer::ArrayBuffer(ref data) => data.as_slice(),
1350 ArrayBufferViewOrArrayBuffer::ArrayBufferView(ref data) => data.as_slice(),
1351 }
1352 };
1353 handle_potential_webgl_error!(self, bound_buffer.buffer_data(target, data, usage));
1354 }
1355
1356 pub(crate) fn buffer_data_(
1357 &self,
1358 target: u32,
1359 size: i64,
1360 usage: u32,
1361 bound_buffer: Option<DomRoot<WebGLBuffer>>,
1362 ) {
1363 let bound_buffer =
1364 handle_potential_webgl_error!(self, bound_buffer.ok_or(InvalidOperation), return);
1365
1366 if size < 0 {
1367 return self.webgl_error(InvalidValue);
1368 }
1369
1370 let data = vec![0u8; size as usize];
1373 handle_potential_webgl_error!(self, bound_buffer.buffer_data(target, &data, usage));
1374 }
1375
1376 #[expect(unsafe_code)]
1377 pub(crate) fn buffer_sub_data(
1378 &self,
1379 target: u32,
1380 offset: i64,
1381 data: ArrayBufferViewOrArrayBuffer,
1382 bound_buffer: Option<DomRoot<WebGLBuffer>>,
1383 ) {
1384 let bound_buffer =
1385 handle_potential_webgl_error!(self, bound_buffer.ok_or(InvalidOperation), return);
1386
1387 if offset < 0 {
1388 return self.webgl_error(InvalidValue);
1389 }
1390
1391 let data = unsafe {
1392 match data {
1394 ArrayBufferViewOrArrayBuffer::ArrayBuffer(ref data) => data.as_slice(),
1395 ArrayBufferViewOrArrayBuffer::ArrayBufferView(ref data) => data.as_slice(),
1396 }
1397 };
1398 if (offset as u64) + data.len() as u64 > bound_buffer.capacity() as u64 {
1399 return self.webgl_error(InvalidValue);
1400 }
1401 let (sender, receiver) = generic_channel::channel().unwrap();
1402 self.send_command(WebGLCommand::BufferSubData(
1403 target,
1404 offset as isize,
1405 receiver,
1406 ));
1407 let buffer = GenericSharedMemory::from_bytes(data);
1408 sender.send(buffer).unwrap();
1409 }
1410
1411 pub(crate) fn bind_buffer_maybe(
1412 &self,
1413 slot: &MutNullableDom<WebGLBuffer>,
1414 target: u32,
1415 buffer: Option<&WebGLBuffer>,
1416 ) {
1417 if let Some(buffer) = buffer {
1418 handle_potential_webgl_error!(self, self.validate_ownership(buffer), return);
1419
1420 if buffer.is_marked_for_deletion() {
1421 return self.webgl_error(InvalidOperation);
1422 }
1423 handle_potential_webgl_error!(self, buffer.set_target_maybe(target), return);
1424 buffer.increment_attached_counter();
1425 }
1426
1427 self.send_command(WebGLCommand::BindBuffer(target, buffer.map(|b| b.id())));
1428 if let Some(old) = slot.get() {
1429 old.decrement_attached_counter(Operation::Infallible);
1430 }
1431
1432 slot.set(buffer);
1433 }
1434
1435 pub(crate) fn current_program(&self) -> Option<DomRoot<WebGLProgram>> {
1436 self.current_program.get()
1437 }
1438
1439 pub(crate) fn uniform_check_program(
1440 &self,
1441 program: &WebGLProgram,
1442 location: &WebGLUniformLocation,
1443 ) -> WebGLResult<()> {
1444 self.validate_ownership(program)?;
1445
1446 if program.is_deleted() ||
1447 !program.is_linked() ||
1448 self.context_id() != location.context_id() ||
1449 program.id() != location.program_id() ||
1450 program.link_generation() != location.link_generation()
1451 {
1452 return Err(InvalidOperation);
1453 }
1454
1455 Ok(())
1456 }
1457
1458 fn uniform_vec_section_int(
1459 &self,
1460 vec: Int32ArrayOrLongSequence,
1461 offset: u32,
1462 length: u32,
1463 uniform_size: usize,
1464 uniform_location: &WebGLUniformLocation,
1465 ) -> WebGLResult<Vec<i32>> {
1466 let vec = match vec {
1467 Int32ArrayOrLongSequence::Int32Array(v) => v.to_vec(),
1468 Int32ArrayOrLongSequence::LongSequence(v) => v,
1469 };
1470 self.uniform_vec_section::<i32>(vec, offset, length, uniform_size, uniform_location)
1471 }
1472
1473 fn uniform_vec_section_float(
1474 &self,
1475 vec: Float32ArrayOrUnrestrictedFloatSequence,
1476 offset: u32,
1477 length: u32,
1478 uniform_size: usize,
1479 uniform_location: &WebGLUniformLocation,
1480 ) -> WebGLResult<Vec<f32>> {
1481 let vec = match vec {
1482 Float32ArrayOrUnrestrictedFloatSequence::Float32Array(v) => v.to_vec(),
1483 Float32ArrayOrUnrestrictedFloatSequence::UnrestrictedFloatSequence(v) => v,
1484 };
1485 self.uniform_vec_section::<f32>(vec, offset, length, uniform_size, uniform_location)
1486 }
1487
1488 pub(crate) fn uniform_vec_section<T: Clone>(
1489 &self,
1490 vec: Vec<T>,
1491 offset: u32,
1492 length: u32,
1493 uniform_size: usize,
1494 uniform_location: &WebGLUniformLocation,
1495 ) -> WebGLResult<Vec<T>> {
1496 let offset = offset as usize;
1497 if offset > vec.len() {
1498 return Err(InvalidValue);
1499 }
1500
1501 let length = if length > 0 {
1502 length as usize
1503 } else {
1504 vec.len() - offset
1505 };
1506 if offset + length > vec.len() {
1507 return Err(InvalidValue);
1508 }
1509
1510 let vec = if offset == 0 && length == vec.len() {
1511 vec
1512 } else {
1513 vec[offset..offset + length].to_vec()
1514 };
1515
1516 if vec.len() < uniform_size || vec.len() % uniform_size != 0 {
1517 return Err(InvalidValue);
1518 }
1519 if uniform_location.size().is_none() && vec.len() != uniform_size {
1520 return Err(InvalidOperation);
1521 }
1522
1523 Ok(vec)
1524 }
1525
1526 pub(crate) fn uniform_matrix_section(
1527 &self,
1528 vec: Float32ArrayOrUnrestrictedFloatSequence,
1529 offset: u32,
1530 length: u32,
1531 transpose: bool,
1532 uniform_size: usize,
1533 uniform_location: &WebGLUniformLocation,
1534 ) -> WebGLResult<Vec<f32>> {
1535 let vec = match vec {
1536 Float32ArrayOrUnrestrictedFloatSequence::Float32Array(v) => v.to_vec(),
1537 Float32ArrayOrUnrestrictedFloatSequence::UnrestrictedFloatSequence(v) => v,
1538 };
1539 if transpose {
1540 return Err(InvalidValue);
1541 }
1542 self.uniform_vec_section::<f32>(vec, offset, length, uniform_size, uniform_location)
1543 }
1544
1545 pub(crate) fn get_draw_framebuffer_slot(&self) -> &MutNullableDom<WebGLFramebuffer> {
1546 &self.bound_draw_framebuffer
1547 }
1548
1549 pub(crate) fn get_read_framebuffer_slot(&self) -> &MutNullableDom<WebGLFramebuffer> {
1550 &self.bound_read_framebuffer
1551 }
1552
1553 pub(crate) fn validate_new_framebuffer_binding(
1554 &self,
1555 framebuffer: Option<&WebGLFramebuffer>,
1556 ) -> WebGLResult<()> {
1557 if let Some(fb) = framebuffer {
1558 self.validate_ownership(fb)?;
1559 if fb.is_deleted() {
1560 return Err(InvalidOperation);
1566 }
1567 }
1568 Ok(())
1569 }
1570
1571 pub(crate) fn bind_framebuffer_to(
1572 &self,
1573 target: u32,
1574 framebuffer: Option<&WebGLFramebuffer>,
1575 slot: &MutNullableDom<WebGLFramebuffer>,
1576 ) {
1577 match framebuffer {
1578 Some(framebuffer) => framebuffer.bind(target),
1579 None => {
1580 let cmd =
1582 WebGLCommand::BindFramebuffer(target, WebGLFramebufferBindingRequest::Default);
1583 self.send_command(cmd);
1584 },
1585 }
1586 slot.set(framebuffer);
1587 }
1588
1589 pub(crate) fn renderbuffer_storage(
1590 &self,
1591 target: u32,
1592 samples: i32,
1593 internal_format: u32,
1594 width: i32,
1595 height: i32,
1596 ) {
1597 if target != constants::RENDERBUFFER {
1598 return self.webgl_error(InvalidEnum);
1599 }
1600
1601 let max = self.limits.max_renderbuffer_size;
1602
1603 if samples < 0 || width < 0 || width as u32 > max || height < 0 || height as u32 > max {
1604 return self.webgl_error(InvalidValue);
1605 }
1606
1607 let rb = handle_potential_webgl_error!(
1608 self,
1609 self.bound_renderbuffer.get().ok_or(InvalidOperation),
1610 return
1611 );
1612 handle_potential_webgl_error!(
1613 self,
1614 rb.storage(self.api_type, samples, internal_format, width, height)
1615 );
1616 if let Some(fb) = self.bound_draw_framebuffer.get() {
1617 fb.invalidate_renderbuffer(&rb);
1618 }
1619
1620 }
1622
1623 pub(crate) fn valid_color_attachment_enum(&self, attachment: u32) -> bool {
1624 let last_slot = constants::COLOR_ATTACHMENT0 + self.limits().max_color_attachments - 1;
1625 constants::COLOR_ATTACHMENT0 <= attachment && attachment <= last_slot
1626 }
1627
1628 #[allow(clippy::too_many_arguments)]
1629 pub(crate) fn compressed_tex_image_2d(
1630 &self,
1631 target: u32,
1632 level: i32,
1633 internal_format: u32,
1634 width: i32,
1635 height: i32,
1636 border: i32,
1637 data: &[u8],
1638 ) {
1639 let validator = CompressedTexImage2DValidator::new(
1640 self,
1641 target,
1642 level,
1643 width,
1644 height,
1645 border,
1646 internal_format,
1647 data.len(),
1648 );
1649 let CommonCompressedTexImage2DValidatorResult {
1650 texture,
1651 target,
1652 level,
1653 width,
1654 height,
1655 compression,
1656 } = match validator.validate() {
1657 Ok(result) => result,
1658 Err(_) => return,
1659 };
1660
1661 if texture.is_immutable() {
1662 return self.webgl_error(InvalidOperation);
1663 }
1664
1665 let size = Size2D::new(width, height);
1666 let data = GenericSharedMemory::from_bytes(data);
1667
1668 handle_potential_webgl_error!(
1669 self,
1670 texture.initialize(
1671 target,
1672 size.width,
1673 size.height,
1674 1,
1675 compression.format,
1676 level,
1677 Some(TexDataType::UnsignedByte)
1678 )
1679 );
1680
1681 self.send_command(WebGLCommand::CompressedTexImage2D {
1682 target: target.as_gl_constant(),
1683 level,
1684 internal_format,
1685 size: Size2D::new(width, height),
1686 data: data.into(),
1687 });
1688
1689 if let Some(fb) = self.bound_draw_framebuffer.get() {
1690 fb.invalidate_texture(&texture);
1691 }
1692 }
1693
1694 #[allow(clippy::too_many_arguments)]
1695 pub(crate) fn compressed_tex_sub_image_2d(
1696 &self,
1697 target: u32,
1698 level: i32,
1699 xoffset: i32,
1700 yoffset: i32,
1701 width: i32,
1702 height: i32,
1703 format: u32,
1704 data: &[u8],
1705 ) {
1706 let validator = CompressedTexSubImage2DValidator::new(
1707 self,
1708 target,
1709 level,
1710 xoffset,
1711 yoffset,
1712 width,
1713 height,
1714 format,
1715 data.len(),
1716 );
1717 let CommonCompressedTexImage2DValidatorResult {
1718 texture: _,
1719 target,
1720 level,
1721 width,
1722 height,
1723 ..
1724 } = match validator.validate() {
1725 Ok(result) => result,
1726 Err(_) => return,
1727 };
1728
1729 let data = GenericSharedMemory::from_bytes(data);
1730
1731 self.send_command(WebGLCommand::CompressedTexSubImage2D {
1732 target: target.as_gl_constant(),
1733 level: level as i32,
1734 xoffset,
1735 yoffset,
1736 size: Size2D::new(width, height),
1737 format,
1738 data: data.into(),
1739 });
1740 }
1741
1742 pub(crate) fn uniform1iv(
1743 &self,
1744 location: Option<&WebGLUniformLocation>,
1745 val: Int32ArrayOrLongSequence,
1746 src_offset: u32,
1747 src_length: u32,
1748 ) {
1749 self.with_location(location, |location| {
1750 match location.type_() {
1751 constants::BOOL |
1752 constants::INT |
1753 constants::SAMPLER_2D |
1754 WebGL2RenderingContextConstants::SAMPLER_2D_ARRAY |
1755 WebGL2RenderingContextConstants::SAMPLER_3D |
1756 constants::SAMPLER_CUBE => {},
1757 _ => return Err(InvalidOperation),
1758 }
1759
1760 let val = self.uniform_vec_section_int(val, src_offset, src_length, 1, location)?;
1761
1762 match location.type_() {
1763 constants::SAMPLER_2D |
1764 constants::SAMPLER_CUBE |
1765 WebGL2RenderingContextConstants::SAMPLER_2D_ARRAY |
1766 WebGL2RenderingContextConstants::SAMPLER_3D => {
1767 for &v in val
1768 .iter()
1769 .take(cmp::min(location.size().unwrap_or(1) as usize, val.len()))
1770 {
1771 if v < 0 || v as u32 >= self.limits.max_combined_texture_image_units {
1772 return Err(InvalidValue);
1773 }
1774 }
1775 },
1776 _ => {},
1777 }
1778 self.send_command(WebGLCommand::Uniform1iv(location.id(), val));
1779 Ok(())
1780 });
1781 }
1782
1783 pub(crate) fn uniform1fv(
1784 &self,
1785 location: Option<&WebGLUniformLocation>,
1786 val: Float32ArrayOrUnrestrictedFloatSequence,
1787 src_offset: u32,
1788 src_length: u32,
1789 ) {
1790 self.with_location(location, |location| {
1791 match location.type_() {
1792 constants::BOOL | constants::FLOAT => {},
1793 _ => return Err(InvalidOperation),
1794 }
1795 let val = self.uniform_vec_section_float(val, src_offset, src_length, 1, location)?;
1796 self.send_command(WebGLCommand::Uniform1fv(location.id(), val));
1797 Ok(())
1798 });
1799 }
1800
1801 pub(crate) fn uniform2fv(
1802 &self,
1803 location: Option<&WebGLUniformLocation>,
1804 val: Float32ArrayOrUnrestrictedFloatSequence,
1805 src_offset: u32,
1806 src_length: u32,
1807 ) {
1808 self.with_location(location, |location| {
1809 match location.type_() {
1810 constants::BOOL_VEC2 | constants::FLOAT_VEC2 => {},
1811 _ => return Err(InvalidOperation),
1812 }
1813 let val = self.uniform_vec_section_float(val, src_offset, src_length, 2, location)?;
1814 self.send_command(WebGLCommand::Uniform2fv(location.id(), val));
1815 Ok(())
1816 });
1817 }
1818
1819 pub(crate) fn uniform2iv(
1820 &self,
1821 location: Option<&WebGLUniformLocation>,
1822 val: Int32ArrayOrLongSequence,
1823 src_offset: u32,
1824 src_length: u32,
1825 ) {
1826 self.with_location(location, |location| {
1827 match location.type_() {
1828 constants::BOOL_VEC2 | constants::INT_VEC2 => {},
1829 _ => return Err(InvalidOperation),
1830 }
1831 let val = self.uniform_vec_section_int(val, src_offset, src_length, 2, location)?;
1832 self.send_command(WebGLCommand::Uniform2iv(location.id(), val));
1833 Ok(())
1834 });
1835 }
1836
1837 pub(crate) fn uniform3fv(
1838 &self,
1839 location: Option<&WebGLUniformLocation>,
1840 val: Float32ArrayOrUnrestrictedFloatSequence,
1841 src_offset: u32,
1842 src_length: u32,
1843 ) {
1844 self.with_location(location, |location| {
1845 match location.type_() {
1846 constants::BOOL_VEC3 | constants::FLOAT_VEC3 => {},
1847 _ => return Err(InvalidOperation),
1848 }
1849 let val = self.uniform_vec_section_float(val, src_offset, src_length, 3, location)?;
1850 self.send_command(WebGLCommand::Uniform3fv(location.id(), val));
1851 Ok(())
1852 });
1853 }
1854
1855 pub(crate) fn uniform3iv(
1856 &self,
1857 location: Option<&WebGLUniformLocation>,
1858 val: Int32ArrayOrLongSequence,
1859 src_offset: u32,
1860 src_length: u32,
1861 ) {
1862 self.with_location(location, |location| {
1863 match location.type_() {
1864 constants::BOOL_VEC3 | constants::INT_VEC3 => {},
1865 _ => return Err(InvalidOperation),
1866 }
1867 let val = self.uniform_vec_section_int(val, src_offset, src_length, 3, location)?;
1868 self.send_command(WebGLCommand::Uniform3iv(location.id(), val));
1869 Ok(())
1870 });
1871 }
1872
1873 pub(crate) fn uniform4iv(
1874 &self,
1875 location: Option<&WebGLUniformLocation>,
1876 val: Int32ArrayOrLongSequence,
1877 src_offset: u32,
1878 src_length: u32,
1879 ) {
1880 self.with_location(location, |location| {
1881 match location.type_() {
1882 constants::BOOL_VEC4 | constants::INT_VEC4 => {},
1883 _ => return Err(InvalidOperation),
1884 }
1885 let val = self.uniform_vec_section_int(val, src_offset, src_length, 4, location)?;
1886 self.send_command(WebGLCommand::Uniform4iv(location.id(), val));
1887 Ok(())
1888 });
1889 }
1890
1891 pub(crate) fn uniform4fv(
1892 &self,
1893 location: Option<&WebGLUniformLocation>,
1894 val: Float32ArrayOrUnrestrictedFloatSequence,
1895 src_offset: u32,
1896 src_length: u32,
1897 ) {
1898 self.with_location(location, |location| {
1899 match location.type_() {
1900 constants::BOOL_VEC4 | constants::FLOAT_VEC4 => {},
1901 _ => return Err(InvalidOperation),
1902 }
1903 let val = self.uniform_vec_section_float(val, src_offset, src_length, 4, location)?;
1904 self.send_command(WebGLCommand::Uniform4fv(location.id(), val));
1905 Ok(())
1906 });
1907 }
1908
1909 pub(crate) fn uniform_matrix_2fv(
1910 &self,
1911 location: Option<&WebGLUniformLocation>,
1912 transpose: bool,
1913 val: Float32ArrayOrUnrestrictedFloatSequence,
1914 src_offset: u32,
1915 src_length: u32,
1916 ) {
1917 self.with_location(location, |location| {
1918 match location.type_() {
1919 constants::FLOAT_MAT2 => {},
1920 _ => return Err(InvalidOperation),
1921 }
1922 let val =
1923 self.uniform_matrix_section(val, src_offset, src_length, transpose, 4, location)?;
1924 self.send_command(WebGLCommand::UniformMatrix2fv(location.id(), val));
1925 Ok(())
1926 });
1927 }
1928
1929 pub(crate) fn uniform_matrix_3fv(
1930 &self,
1931 location: Option<&WebGLUniformLocation>,
1932 transpose: bool,
1933 val: Float32ArrayOrUnrestrictedFloatSequence,
1934 src_offset: u32,
1935 src_length: u32,
1936 ) {
1937 self.with_location(location, |location| {
1938 match location.type_() {
1939 constants::FLOAT_MAT3 => {},
1940 _ => return Err(InvalidOperation),
1941 }
1942 let val =
1943 self.uniform_matrix_section(val, src_offset, src_length, transpose, 9, location)?;
1944 self.send_command(WebGLCommand::UniformMatrix3fv(location.id(), val));
1945 Ok(())
1946 });
1947 }
1948
1949 pub(crate) fn uniform_matrix_4fv(
1950 &self,
1951 location: Option<&WebGLUniformLocation>,
1952 transpose: bool,
1953 val: Float32ArrayOrUnrestrictedFloatSequence,
1954 src_offset: u32,
1955 src_length: u32,
1956 ) {
1957 self.with_location(location, |location| {
1958 match location.type_() {
1959 constants::FLOAT_MAT4 => {},
1960 _ => return Err(InvalidOperation),
1961 }
1962 let val =
1963 self.uniform_matrix_section(val, src_offset, src_length, transpose, 16, location)?;
1964 self.send_command(WebGLCommand::UniformMatrix4fv(location.id(), val));
1965 Ok(())
1966 });
1967 }
1968
1969 pub(crate) fn get_buffer_param(
1970 &self,
1971 buffer: Option<DomRoot<WebGLBuffer>>,
1972 parameter: u32,
1973 mut retval: MutableHandleValue,
1974 ) {
1975 let buffer = handle_potential_webgl_error!(
1976 self,
1977 buffer.ok_or(InvalidOperation),
1978 return retval.set(NullValue())
1979 );
1980
1981 retval.set(match parameter {
1982 constants::BUFFER_SIZE => Int32Value(buffer.capacity() as i32),
1983 constants::BUFFER_USAGE => Int32Value(buffer.usage() as i32),
1984 _ => {
1985 self.webgl_error(InvalidEnum);
1986 NullValue()
1987 },
1988 })
1989 }
1990}
1991
1992impl CanvasContext for WebGLRenderingContext {
1993 type ID = WebGLContextId;
1994
1995 fn context_id(&self) -> Self::ID {
1996 self.droppable.webgl_sender.context_id()
1997 }
1998
1999 fn canvas(&self) -> Option<RootedHTMLCanvasElementOrOffscreenCanvas> {
2000 Some(RootedHTMLCanvasElementOrOffscreenCanvas::from(&self.canvas))
2001 }
2002
2003 fn resize(&self) {
2004 let size = self.size().cast();
2005 let (sender, receiver) = webgl_channel().unwrap();
2006 self.droppable
2007 .webgl_sender
2008 .send_resize(size, sender)
2009 .unwrap();
2010 self.size.set(size);
2013 self.reflector_
2014 .update_memory_size(self, size.cast::<usize>().area() * 4);
2015
2016 if let Err(msg) = receiver.recv().unwrap() {
2017 error!("Error resizing WebGLContext: {}", msg);
2018 return;
2019 };
2020
2021 let color = self.current_clear_color.get();
2024 self.send_command(WebGLCommand::ClearColor(color.0, color.1, color.2, color.3));
2025
2026 let rect = self.current_scissor.get();
2031 self.send_command(WebGLCommand::Scissor(rect.0, rect.1, rect.2, rect.3));
2032
2033 if let Some(texture) = self
2038 .textures
2039 .active_texture_slot(constants::TEXTURE_2D, self.webgl_version())
2040 .unwrap()
2041 .get()
2042 {
2043 self.send_command(WebGLCommand::BindTexture(
2044 constants::TEXTURE_2D,
2045 Some(texture.id()),
2046 ));
2047 }
2048
2049 if let Some(fbo) = self.bound_draw_framebuffer.get() {
2053 let id = WebGLFramebufferBindingRequest::Explicit(fbo.id());
2054 self.send_command(WebGLCommand::BindFramebuffer(constants::FRAMEBUFFER, id));
2055 }
2056 }
2057
2058 fn reset_bitmap(&self) {
2059 warn!("The WebGLRenderingContext 'reset_bitmap' is not implemented yet");
2060 }
2061
2062 fn get_image_data(&self) -> Option<Snapshot> {
2069 handle_potential_webgl_error!(self, self.validate_framebuffer(), return None);
2070 let mut size = self.size().cast();
2071
2072 let (fb_width, fb_height) = handle_potential_webgl_error!(
2073 self,
2074 self.get_current_framebuffer_size().ok_or(InvalidOperation),
2075 return None
2076 );
2077 size.width = cmp::min(size.width, fb_width as u32);
2078 size.height = cmp::min(size.height, fb_height as u32);
2079
2080 let (sender, receiver) = generic_channel::channel().unwrap();
2081 self.send_command(WebGLCommand::ReadPixels(
2082 Rect::from_size(size),
2083 constants::RGBA,
2084 constants::UNSIGNED_BYTE,
2085 sender,
2086 ));
2087 let (data, alpha_mode) = receiver.recv().unwrap();
2088 Some(Snapshot::from_vec(
2089 size.cast(),
2090 SnapshotPixelFormat::RGBA,
2091 alpha_mode,
2092 data.to_vec(),
2093 ))
2094 }
2095
2096 fn mark_as_dirty(&self) {
2097 if self.bound_draw_framebuffer.get().is_some() {
2099 return;
2100 }
2101
2102 if self.global().as_window().in_immersive_xr_session() {
2105 return;
2106 }
2107
2108 self.canvas.mark_as_dirty();
2109 }
2110}
2111
2112#[cfg(not(feature = "webgl_backtrace"))]
2113#[inline]
2114pub(crate) fn capture_webgl_backtrace() -> WebGLCommandBacktrace {
2115 WebGLCommandBacktrace {}
2116}
2117
2118#[cfg(feature = "webgl_backtrace")]
2119#[cfg_attr(feature = "webgl_backtrace", expect(unsafe_code))]
2120pub(crate) fn capture_webgl_backtrace() -> WebGLCommandBacktrace {
2121 let bt = Backtrace::new();
2122 unsafe {
2123 capture_stack!(in(*GlobalScope::get_cx()) let stack);
2124 WebGLCommandBacktrace {
2125 backtrace: format!("{:?}", bt),
2126 js_backtrace: stack.and_then(|s| s.as_string(None, js::jsapi::StackFormat::Default)),
2127 }
2128 }
2129}
2130
2131impl WebGLRenderingContextMethods<crate::DomTypeHolder> for WebGLRenderingContext {
2132 fn Canvas(&self) -> RootedHTMLCanvasElementOrOffscreenCanvas {
2134 RootedHTMLCanvasElementOrOffscreenCanvas::from(&self.canvas)
2135 }
2136
2137 fn Flush(&self) {
2139 self.send_command(WebGLCommand::Flush);
2140 }
2141
2142 fn Finish(&self) {
2144 let (sender, receiver) = webgl_channel().unwrap();
2145 self.send_command(WebGLCommand::Finish(sender));
2146 receiver.recv().unwrap()
2147 }
2148
2149 fn DrawingBufferWidth(&self) -> i32 {
2151 let (sender, receiver) = webgl_channel().unwrap();
2152 self.send_command(WebGLCommand::DrawingBufferWidth(sender));
2153 receiver.recv().unwrap()
2154 }
2155
2156 fn DrawingBufferHeight(&self) -> i32 {
2158 let (sender, receiver) = webgl_channel().unwrap();
2159 self.send_command(WebGLCommand::DrawingBufferHeight(sender));
2160 receiver.recv().unwrap()
2161 }
2162
2163 fn GetBufferParameter(
2165 &self,
2166 _cx: SafeJSContext,
2167 target: u32,
2168 parameter: u32,
2169 mut retval: MutableHandleValue,
2170 ) {
2171 let buffer = handle_potential_webgl_error!(
2172 self,
2173 self.bound_buffer(target),
2174 return retval.set(NullValue())
2175 );
2176 self.get_buffer_param(buffer, parameter, retval)
2177 }
2178
2179 #[expect(unsafe_code)]
2180 fn GetParameter(&self, cx: SafeJSContext, parameter: u32, mut retval: MutableHandleValue) {
2182 if !self
2183 .extension_manager
2184 .is_get_parameter_name_enabled(parameter)
2185 {
2186 self.webgl_error(WebGLError::InvalidEnum);
2187 return retval.set(NullValue());
2188 }
2189
2190 match parameter {
2191 constants::ARRAY_BUFFER_BINDING => {
2192 self.bound_buffer_array
2193 .get()
2194 .safe_to_jsval(cx, retval, CanGc::deprecated_note());
2195 return;
2196 },
2197 constants::CURRENT_PROGRAM => {
2198 self.current_program
2199 .get()
2200 .safe_to_jsval(cx, retval, CanGc::deprecated_note());
2201 return;
2202 },
2203 constants::ELEMENT_ARRAY_BUFFER_BINDING => {
2204 let buffer = self.current_vao().element_array_buffer().get();
2205 buffer.safe_to_jsval(cx, retval, CanGc::deprecated_note());
2206 return;
2207 },
2208 constants::FRAMEBUFFER_BINDING => {
2209 self.bound_draw_framebuffer.get().safe_to_jsval(
2210 cx,
2211 retval,
2212 CanGc::deprecated_note(),
2213 );
2214 return;
2215 },
2216 constants::RENDERBUFFER_BINDING => {
2217 self.bound_renderbuffer
2218 .get()
2219 .safe_to_jsval(cx, retval, CanGc::deprecated_note());
2220 return;
2221 },
2222 constants::TEXTURE_BINDING_2D => {
2223 let texture = self
2224 .textures
2225 .active_texture_slot(constants::TEXTURE_2D, self.webgl_version())
2226 .unwrap()
2227 .get();
2228 texture.safe_to_jsval(cx, retval, CanGc::deprecated_note());
2229 return;
2230 },
2231 WebGL2RenderingContextConstants::TEXTURE_BINDING_2D_ARRAY => {
2232 let texture = self
2233 .textures
2234 .active_texture_slot(
2235 WebGL2RenderingContextConstants::TEXTURE_2D_ARRAY,
2236 self.webgl_version(),
2237 )
2238 .unwrap()
2239 .get();
2240 texture.safe_to_jsval(cx, retval, CanGc::deprecated_note());
2241 return;
2242 },
2243 WebGL2RenderingContextConstants::TEXTURE_BINDING_3D => {
2244 let texture = self
2245 .textures
2246 .active_texture_slot(
2247 WebGL2RenderingContextConstants::TEXTURE_3D,
2248 self.webgl_version(),
2249 )
2250 .unwrap()
2251 .get();
2252 texture.safe_to_jsval(cx, retval, CanGc::deprecated_note());
2253 return;
2254 },
2255 constants::TEXTURE_BINDING_CUBE_MAP => {
2256 let texture = self
2257 .textures
2258 .active_texture_slot(constants::TEXTURE_CUBE_MAP, self.webgl_version())
2259 .unwrap()
2260 .get();
2261 texture.safe_to_jsval(cx, retval, CanGc::deprecated_note());
2262 return;
2263 },
2264 OESVertexArrayObjectConstants::VERTEX_ARRAY_BINDING_OES => {
2265 let vao = self.current_vao.get().filter(|vao| vao.id().is_some());
2266 vao.safe_to_jsval(cx, retval, CanGc::deprecated_note());
2267 return;
2268 },
2269 constants::IMPLEMENTATION_COLOR_READ_FORMAT => {
2275 if self.validate_framebuffer().is_err() {
2276 self.webgl_error(InvalidOperation);
2277 return retval.set(NullValue());
2278 }
2279 return retval.set(Int32Value(constants::RGBA as i32));
2280 },
2281 constants::IMPLEMENTATION_COLOR_READ_TYPE => {
2282 if self.validate_framebuffer().is_err() {
2283 self.webgl_error(InvalidOperation);
2284 return retval.set(NullValue());
2285 }
2286 return retval.set(Int32Value(constants::UNSIGNED_BYTE as i32));
2287 },
2288 constants::COMPRESSED_TEXTURE_FORMATS => unsafe {
2289 let format_ids = self.extension_manager.get_tex_compression_ids();
2290
2291 rooted!(in(*cx) let mut rval = ptr::null_mut::<JSObject>());
2292 Uint32Array::create(*cx, CreateWith::Slice(&format_ids), rval.handle_mut())
2293 .unwrap();
2294 return retval.set(ObjectValue(rval.get()));
2295 },
2296 constants::VERSION => {
2297 "WebGL 1.0".safe_to_jsval(cx, retval, CanGc::deprecated_note());
2298 return;
2299 },
2300 constants::RENDERER | constants::VENDOR => {
2301 "Mozilla/Servo".safe_to_jsval(cx, retval, CanGc::deprecated_note());
2302 return;
2303 },
2304 constants::SHADING_LANGUAGE_VERSION => {
2305 "WebGL GLSL ES 1.0".safe_to_jsval(cx, retval, CanGc::deprecated_note());
2306 return;
2307 },
2308 constants::UNPACK_FLIP_Y_WEBGL => {
2309 let unpack = self.texture_unpacking_settings.get();
2310 retval.set(BooleanValue(unpack.contains(TextureUnpacking::FLIP_Y_AXIS)));
2311 return;
2312 },
2313 constants::UNPACK_PREMULTIPLY_ALPHA_WEBGL => {
2314 let unpack = self.texture_unpacking_settings.get();
2315 retval.set(BooleanValue(
2316 unpack.contains(TextureUnpacking::PREMULTIPLY_ALPHA),
2317 ));
2318 return;
2319 },
2320 constants::PACK_ALIGNMENT => {
2321 retval.set(UInt32Value(self.texture_packing_alignment.get() as u32));
2322 return;
2323 },
2324 constants::UNPACK_ALIGNMENT => {
2325 retval.set(UInt32Value(self.texture_unpacking_alignment.get()));
2326 return;
2327 },
2328 constants::UNPACK_COLORSPACE_CONVERSION_WEBGL => {
2329 let unpack = self.texture_unpacking_settings.get();
2330 retval.set(UInt32Value(
2331 if unpack.contains(TextureUnpacking::CONVERT_COLORSPACE) {
2332 constants::BROWSER_DEFAULT_WEBGL
2333 } else {
2334 constants::NONE
2335 },
2336 ));
2337 return;
2338 },
2339 _ => {},
2340 }
2341
2342 let limit = match parameter {
2345 constants::MAX_VERTEX_ATTRIBS => Some(self.limits.max_vertex_attribs),
2346 constants::MAX_TEXTURE_SIZE => Some(self.limits.max_tex_size),
2347 constants::MAX_CUBE_MAP_TEXTURE_SIZE => Some(self.limits.max_cube_map_tex_size),
2348 constants::MAX_COMBINED_TEXTURE_IMAGE_UNITS => {
2349 Some(self.limits.max_combined_texture_image_units)
2350 },
2351 constants::MAX_FRAGMENT_UNIFORM_VECTORS => {
2352 Some(self.limits.max_fragment_uniform_vectors)
2353 },
2354 constants::MAX_RENDERBUFFER_SIZE => Some(self.limits.max_renderbuffer_size),
2355 constants::MAX_TEXTURE_IMAGE_UNITS => Some(self.limits.max_texture_image_units),
2356 constants::MAX_VARYING_VECTORS => Some(self.limits.max_varying_vectors),
2357 constants::MAX_VERTEX_TEXTURE_IMAGE_UNITS => {
2358 Some(self.limits.max_vertex_texture_image_units)
2359 },
2360 constants::MAX_VERTEX_UNIFORM_VECTORS => Some(self.limits.max_vertex_uniform_vectors),
2361 _ => None,
2362 };
2363 if let Some(limit) = limit {
2364 retval.set(UInt32Value(limit));
2365 return;
2366 }
2367
2368 if let Ok(value) = self.capabilities.is_enabled(parameter) {
2369 retval.set(BooleanValue(value));
2370 return;
2371 }
2372
2373 match handle_potential_webgl_error!(
2374 self,
2375 Parameter::from_u32(parameter),
2376 return retval.set(NullValue())
2377 ) {
2378 Parameter::Bool(param) => {
2379 let (sender, receiver) = webgl_channel().unwrap();
2380 self.send_command(WebGLCommand::GetParameterBool(param, sender));
2381 retval.set(BooleanValue(receiver.recv().unwrap()))
2382 },
2383 Parameter::Bool4(param) => {
2384 let (sender, receiver) = webgl_channel().unwrap();
2385 self.send_command(WebGLCommand::GetParameterBool4(param, sender));
2386 receiver
2387 .recv()
2388 .unwrap()
2389 .safe_to_jsval(cx, retval, CanGc::deprecated_note());
2390 },
2391 Parameter::Int(param) => {
2392 let (sender, receiver) = webgl_channel().unwrap();
2393 self.send_command(WebGLCommand::GetParameterInt(param, sender));
2394 retval.set(Int32Value(receiver.recv().unwrap()))
2395 },
2396 Parameter::Int2(param) => unsafe {
2397 let (sender, receiver) = webgl_channel().unwrap();
2398 self.send_command(WebGLCommand::GetParameterInt2(param, sender));
2399 rooted!(in(*cx) let mut rval = ptr::null_mut::<JSObject>());
2400 Int32Array::create(
2401 *cx,
2402 CreateWith::Slice(&receiver.recv().unwrap()),
2403 rval.handle_mut(),
2404 )
2405 .unwrap();
2406 retval.set(ObjectValue(rval.get()))
2407 },
2408 Parameter::Int4(param) => unsafe {
2409 let (sender, receiver) = webgl_channel().unwrap();
2410 self.send_command(WebGLCommand::GetParameterInt4(param, sender));
2411 rooted!(in(*cx) let mut rval = ptr::null_mut::<JSObject>());
2412 Int32Array::create(
2413 *cx,
2414 CreateWith::Slice(&receiver.recv().unwrap()),
2415 rval.handle_mut(),
2416 )
2417 .unwrap();
2418 retval.set(ObjectValue(rval.get()))
2419 },
2420 Parameter::Float(param) => {
2421 let (sender, receiver) = webgl_channel().unwrap();
2422 self.send_command(WebGLCommand::GetParameterFloat(param, sender));
2423 retval.set(DoubleValue(receiver.recv().unwrap() as f64))
2424 },
2425 Parameter::Float2(param) => unsafe {
2426 let (sender, receiver) = webgl_channel().unwrap();
2427 self.send_command(WebGLCommand::GetParameterFloat2(param, sender));
2428 rooted!(in(*cx) let mut rval = ptr::null_mut::<JSObject>());
2429 Float32Array::create(
2430 *cx,
2431 CreateWith::Slice(&receiver.recv().unwrap()),
2432 rval.handle_mut(),
2433 )
2434 .unwrap();
2435 retval.set(ObjectValue(rval.get()))
2436 },
2437 Parameter::Float4(param) => unsafe {
2438 let (sender, receiver) = webgl_channel().unwrap();
2439 self.send_command(WebGLCommand::GetParameterFloat4(param, sender));
2440 rooted!(in(*cx) let mut rval = ptr::null_mut::<JSObject>());
2441 Float32Array::create(
2442 *cx,
2443 CreateWith::Slice(&receiver.recv().unwrap()),
2444 rval.handle_mut(),
2445 )
2446 .unwrap();
2447 retval.set(ObjectValue(rval.get()))
2448 },
2449 }
2450 }
2451
2452 fn GetTexParameter(
2454 &self,
2455 _cx: SafeJSContext,
2456 target: u32,
2457 pname: u32,
2458 mut retval: MutableHandleValue,
2459 ) {
2460 let texture_slot = handle_potential_webgl_error!(
2461 self,
2462 self.textures
2463 .active_texture_slot(target, self.webgl_version()),
2464 return retval.set(NullValue())
2465 );
2466 let texture = handle_potential_webgl_error!(
2467 self,
2468 texture_slot.get().ok_or(InvalidOperation),
2469 return retval.set(NullValue())
2470 );
2471
2472 if !self
2473 .extension_manager
2474 .is_get_tex_parameter_name_enabled(pname)
2475 {
2476 self.webgl_error(InvalidEnum);
2477 return retval.set(NullValue());
2478 }
2479
2480 match pname {
2481 constants::TEXTURE_MAG_FILTER => return retval.set(UInt32Value(texture.mag_filter())),
2482 constants::TEXTURE_MIN_FILTER => return retval.set(UInt32Value(texture.min_filter())),
2483 _ => {},
2484 }
2485
2486 let texparam = handle_potential_webgl_error!(
2487 self,
2488 TexParameter::from_u32(pname),
2489 return retval.set(NullValue())
2490 );
2491 if self.webgl_version() < texparam.required_webgl_version() {
2492 self.webgl_error(InvalidEnum);
2493 return retval.set(NullValue());
2494 }
2495
2496 if let Some(value) = texture.maybe_get_tex_parameter(texparam) {
2497 match value {
2498 TexParameterValue::Float(v) => retval.set(DoubleValue(v as f64)),
2499 TexParameterValue::Int(v) => retval.set(Int32Value(v)),
2500 TexParameterValue::Bool(v) => retval.set(BooleanValue(v)),
2501 }
2502 return;
2503 }
2504
2505 match texparam {
2506 TexParameter::Float(param) => {
2507 let (sender, receiver) = webgl_channel().unwrap();
2508 self.send_command(WebGLCommand::GetTexParameterFloat(target, param, sender));
2509 retval.set(DoubleValue(receiver.recv().unwrap() as f64))
2510 },
2511 TexParameter::Int(param) => {
2512 let (sender, receiver) = webgl_channel().unwrap();
2513 self.send_command(WebGLCommand::GetTexParameterInt(target, param, sender));
2514 retval.set(Int32Value(receiver.recv().unwrap()))
2515 },
2516 TexParameter::Bool(param) => {
2517 let (sender, receiver) = webgl_channel().unwrap();
2518 self.send_command(WebGLCommand::GetTexParameterBool(target, param, sender));
2519 retval.set(BooleanValue(receiver.recv().unwrap()))
2520 },
2521 }
2522 }
2523
2524 fn GetError(&self) -> u32 {
2526 let error_code = if let Some(error) = self.last_error.get() {
2527 match error {
2528 WebGLError::InvalidEnum => constants::INVALID_ENUM,
2529 WebGLError::InvalidFramebufferOperation => constants::INVALID_FRAMEBUFFER_OPERATION,
2530 WebGLError::InvalidValue => constants::INVALID_VALUE,
2531 WebGLError::InvalidOperation => constants::INVALID_OPERATION,
2532 WebGLError::OutOfMemory => constants::OUT_OF_MEMORY,
2533 WebGLError::ContextLost => constants::CONTEXT_LOST_WEBGL,
2534 }
2535 } else {
2536 constants::NO_ERROR
2537 };
2538 self.last_error.set(None);
2539 error_code
2540 }
2541
2542 fn GetContextAttributes(&self) -> Option<WebGLContextAttributes> {
2544 let (sender, receiver) = webgl_channel().unwrap();
2545
2546 let backtrace = capture_webgl_backtrace();
2548 if self
2549 .droppable
2550 .webgl_sender
2551 .send(WebGLCommand::GetContextAttributes(sender), backtrace)
2552 .is_err()
2553 {
2554 return None;
2555 }
2556
2557 let attrs = receiver.recv().unwrap();
2558
2559 Some(WebGLContextAttributes {
2560 alpha: attrs.alpha,
2561 antialias: attrs.antialias,
2562 depth: attrs.depth,
2563 failIfMajorPerformanceCaveat: false,
2564 preferLowPowerToHighPerformance: false,
2565 premultipliedAlpha: attrs.premultiplied_alpha,
2566 preserveDrawingBuffer: attrs.preserve_drawing_buffer,
2567 stencil: attrs.stencil,
2568 })
2569 }
2570
2571 fn IsContextLost(&self) -> bool {
2573 false
2574 }
2575
2576 fn GetSupportedExtensions(&self) -> Option<Vec<DOMString>> {
2578 self.extension_manager
2579 .init_once(|| self.get_gl_extensions());
2580 let extensions = self.extension_manager.get_supported_extensions();
2581 Some(
2582 extensions
2583 .iter()
2584 .map(|name| DOMString::from(*name))
2585 .collect(),
2586 )
2587 }
2588
2589 fn GetExtension(&self, _cx: SafeJSContext, name: DOMString) -> Option<NonNull<JSObject>> {
2591 self.extension_manager
2592 .init_once(|| self.get_gl_extensions());
2593 self.extension_manager.get_or_init_extension(&name, self)
2594 }
2595
2596 fn ActiveTexture(&self, texture: u32) {
2598 handle_potential_webgl_error!(self, self.textures.set_active_unit_enum(texture), return);
2599 self.send_command(WebGLCommand::ActiveTexture(texture));
2600 }
2601
2602 fn BlendColor(&self, r: f32, g: f32, b: f32, a: f32) {
2604 self.send_command(WebGLCommand::BlendColor(r, g, b, a));
2605 }
2606
2607 fn BlendEquation(&self, mode: u32) {
2609 handle_potential_webgl_error!(self, self.validate_blend_mode(mode), return);
2610 self.send_command(WebGLCommand::BlendEquation(mode))
2611 }
2612
2613 fn BlendEquationSeparate(&self, mode_rgb: u32, mode_alpha: u32) {
2615 handle_potential_webgl_error!(self, self.validate_blend_mode(mode_rgb), return);
2616 handle_potential_webgl_error!(self, self.validate_blend_mode(mode_alpha), return);
2617 self.send_command(WebGLCommand::BlendEquationSeparate(mode_rgb, mode_alpha));
2618 }
2619
2620 fn BlendFunc(&self, src_factor: u32, dest_factor: u32) {
2622 if has_invalid_blend_constants(src_factor, dest_factor) {
2628 return self.webgl_error(InvalidOperation);
2629 }
2630 if has_invalid_blend_constants(dest_factor, src_factor) {
2631 return self.webgl_error(InvalidOperation);
2632 }
2633
2634 self.send_command(WebGLCommand::BlendFunc(src_factor, dest_factor));
2635 }
2636
2637 fn BlendFuncSeparate(&self, src_rgb: u32, dest_rgb: u32, src_alpha: u32, dest_alpha: u32) {
2639 if has_invalid_blend_constants(src_rgb, dest_rgb) {
2645 return self.webgl_error(InvalidOperation);
2646 }
2647 if has_invalid_blend_constants(dest_rgb, src_rgb) {
2648 return self.webgl_error(InvalidOperation);
2649 }
2650
2651 self.send_command(WebGLCommand::BlendFuncSeparate(
2652 src_rgb, dest_rgb, src_alpha, dest_alpha,
2653 ));
2654 }
2655
2656 fn AttachShader(&self, program: &WebGLProgram, shader: &WebGLShader) {
2658 handle_potential_webgl_error!(self, self.validate_ownership(program), return);
2659 handle_potential_webgl_error!(self, self.validate_ownership(shader), return);
2660 handle_potential_webgl_error!(self, program.attach_shader(shader));
2661 }
2662
2663 fn DetachShader(&self, program: &WebGLProgram, shader: &WebGLShader) {
2665 handle_potential_webgl_error!(self, self.validate_ownership(program), return);
2666 handle_potential_webgl_error!(self, self.validate_ownership(shader), return);
2667 handle_potential_webgl_error!(self, program.detach_shader(shader));
2668 }
2669
2670 fn BindAttribLocation(&self, program: &WebGLProgram, index: u32, name: DOMString) {
2672 handle_potential_webgl_error!(self, self.validate_ownership(program), return);
2673 handle_potential_webgl_error!(self, program.bind_attrib_location(index, name));
2674 }
2675
2676 fn BindBuffer(&self, target: u32, buffer: Option<&WebGLBuffer>) {
2678 let current_vao;
2679 let slot = match target {
2680 constants::ARRAY_BUFFER => &self.bound_buffer_array,
2681 constants::ELEMENT_ARRAY_BUFFER => {
2682 current_vao = self.current_vao();
2683 current_vao.element_array_buffer()
2684 },
2685 _ => return self.webgl_error(InvalidEnum),
2686 };
2687 self.bind_buffer_maybe(slot, target, buffer);
2688 }
2689
2690 fn BindFramebuffer(&self, target: u32, framebuffer: Option<&WebGLFramebuffer>) {
2692 handle_potential_webgl_error!(
2693 self,
2694 self.validate_new_framebuffer_binding(framebuffer),
2695 return
2696 );
2697
2698 if target != constants::FRAMEBUFFER {
2699 return self.webgl_error(InvalidEnum);
2700 }
2701
2702 self.bind_framebuffer_to(target, framebuffer, &self.bound_draw_framebuffer)
2703 }
2704
2705 fn BindRenderbuffer(&self, target: u32, renderbuffer: Option<&WebGLRenderbuffer>) {
2707 if let Some(rb) = renderbuffer {
2708 handle_potential_webgl_error!(self, self.validate_ownership(rb), return);
2709 }
2710
2711 if target != constants::RENDERBUFFER {
2712 return self.webgl_error(InvalidEnum);
2713 }
2714
2715 match renderbuffer {
2716 Some(renderbuffer) if !renderbuffer.is_deleted() => {
2720 self.bound_renderbuffer.set(Some(renderbuffer));
2721 renderbuffer.bind(target);
2722 },
2723 _ => {
2724 if renderbuffer.is_some() {
2725 self.webgl_error(InvalidOperation);
2726 }
2727
2728 self.bound_renderbuffer.set(None);
2729 self.send_command(WebGLCommand::BindRenderbuffer(target, None));
2731 },
2732 }
2733 }
2734
2735 fn BindTexture(&self, target: u32, texture: Option<&WebGLTexture>) {
2737 if let Some(texture) = texture {
2738 handle_potential_webgl_error!(self, self.validate_ownership(texture), return);
2739 }
2740
2741 let texture_slot = handle_potential_webgl_error!(
2742 self,
2743 self.textures
2744 .active_texture_slot(target, self.webgl_version()),
2745 return
2746 );
2747
2748 if let Some(texture) = texture {
2749 handle_potential_webgl_error!(self, texture.bind(target), return);
2750 } else {
2751 self.send_command(WebGLCommand::BindTexture(target, None));
2752 }
2753 texture_slot.set(texture);
2754 }
2755
2756 fn GenerateMipmap(&self, target: u32) {
2758 let texture_slot = handle_potential_webgl_error!(
2759 self,
2760 self.textures
2761 .active_texture_slot(target, self.webgl_version()),
2762 return
2763 );
2764 let texture =
2765 handle_potential_webgl_error!(self, texture_slot.get().ok_or(InvalidOperation), return);
2766 handle_potential_webgl_error!(self, texture.generate_mipmap());
2767 }
2768
2769 fn BufferData_(&self, target: u32, data: Option<ArrayBufferViewOrArrayBuffer>, usage: u32) {
2771 let usage = handle_potential_webgl_error!(self, self.buffer_usage(usage), return);
2772 let bound_buffer = handle_potential_webgl_error!(self, self.bound_buffer(target), return);
2773 self.buffer_data(target, data, usage, bound_buffer)
2774 }
2775
2776 fn BufferData(&self, target: u32, size: i64, usage: u32) {
2778 let usage = handle_potential_webgl_error!(self, self.buffer_usage(usage), return);
2779 let bound_buffer = handle_potential_webgl_error!(self, self.bound_buffer(target), return);
2780 self.buffer_data_(target, size, usage, bound_buffer)
2781 }
2782
2783 fn BufferSubData(&self, target: u32, offset: i64, data: ArrayBufferViewOrArrayBuffer) {
2785 let bound_buffer = handle_potential_webgl_error!(self, self.bound_buffer(target), return);
2786 self.buffer_sub_data(target, offset, data, bound_buffer)
2787 }
2788
2789 #[expect(unsafe_code)]
2791 fn CompressedTexImage2D(
2792 &self,
2793 target: u32,
2794 level: i32,
2795 internal_format: u32,
2796 width: i32,
2797 height: i32,
2798 border: i32,
2799 data: CustomAutoRooterGuard<ArrayBufferView>,
2800 ) {
2801 let data = unsafe { data.as_slice() };
2802 self.compressed_tex_image_2d(target, level, internal_format, width, height, border, data)
2803 }
2804
2805 #[expect(unsafe_code)]
2807 fn CompressedTexSubImage2D(
2808 &self,
2809 target: u32,
2810 level: i32,
2811 xoffset: i32,
2812 yoffset: i32,
2813 width: i32,
2814 height: i32,
2815 format: u32,
2816 data: CustomAutoRooterGuard<ArrayBufferView>,
2817 ) {
2818 let data = unsafe { data.as_slice() };
2819 self.compressed_tex_sub_image_2d(
2820 target, level, xoffset, yoffset, width, height, format, data,
2821 )
2822 }
2823
2824 fn CopyTexImage2D(
2826 &self,
2827 target: u32,
2828 level: i32,
2829 internal_format: u32,
2830 x: i32,
2831 y: i32,
2832 width: i32,
2833 height: i32,
2834 border: i32,
2835 ) {
2836 handle_potential_webgl_error!(self, self.validate_framebuffer(), return);
2837
2838 let validator = CommonTexImage2DValidator::new(
2839 self,
2840 target,
2841 level,
2842 internal_format,
2843 width,
2844 height,
2845 border,
2846 );
2847 let CommonTexImage2DValidatorResult {
2848 texture,
2849 target,
2850 level,
2851 internal_format,
2852 width,
2853 height,
2854 border,
2855 } = match validator.validate() {
2856 Ok(result) => result,
2857 Err(_) => return,
2858 };
2859
2860 if texture.is_immutable() {
2861 return self.webgl_error(InvalidOperation);
2862 }
2863
2864 let framebuffer_format = match self.bound_draw_framebuffer.get() {
2865 Some(fb) => match fb.attachment(constants::COLOR_ATTACHMENT0) {
2866 Some(WebGLFramebufferAttachmentRoot::Renderbuffer(rb)) => {
2867 TexFormat::from_gl_constant(rb.internal_format())
2868 },
2869 Some(WebGLFramebufferAttachmentRoot::Texture(texture)) => texture
2870 .image_info_for_target(&target, 0)
2871 .map(|info| info.internal_format()),
2872 None => None,
2873 },
2874 None => {
2875 let attrs = self.GetContextAttributes().unwrap();
2876 Some(if attrs.alpha {
2877 TexFormat::RGBA
2878 } else {
2879 TexFormat::RGB
2880 })
2881 },
2882 };
2883
2884 let framebuffer_format = match framebuffer_format {
2885 Some(f) => f,
2886 None => {
2887 self.webgl_error(InvalidOperation);
2888 return;
2889 },
2890 };
2891
2892 match (framebuffer_format, internal_format) {
2893 (a, b) if a == b => (),
2894 (TexFormat::RGBA, TexFormat::RGB) => (),
2895 (TexFormat::RGBA, TexFormat::Alpha) => (),
2896 (TexFormat::RGBA, TexFormat::Luminance) => (),
2897 (TexFormat::RGBA, TexFormat::LuminanceAlpha) => (),
2898 (TexFormat::RGB, TexFormat::Luminance) => (),
2899 _ => {
2900 self.webgl_error(InvalidOperation);
2901 return;
2902 },
2903 }
2904
2905 handle_potential_webgl_error!(
2907 self,
2908 texture.initialize(target, width, height, 1, internal_format, level, None)
2909 );
2910
2911 let msg = WebGLCommand::CopyTexImage2D(
2912 target.as_gl_constant(),
2913 level as i32,
2914 internal_format.as_gl_constant(),
2915 x,
2916 y,
2917 width as i32,
2918 height as i32,
2919 border as i32,
2920 );
2921
2922 self.send_command(msg);
2923
2924 if let Some(framebuffer) = self.bound_draw_framebuffer.get() {
2925 framebuffer.invalidate_texture(&texture);
2926 }
2927 }
2928
2929 fn CopyTexSubImage2D(
2931 &self,
2932 target: u32,
2933 level: i32,
2934 xoffset: i32,
2935 yoffset: i32,
2936 x: i32,
2937 y: i32,
2938 width: i32,
2939 height: i32,
2940 ) {
2941 handle_potential_webgl_error!(self, self.validate_framebuffer(), return);
2942
2943 let validator = CommonTexImage2DValidator::new(
2946 self,
2947 target,
2948 level,
2949 TexFormat::RGBA.as_gl_constant(),
2950 width,
2951 height,
2952 0,
2953 );
2954 let CommonTexImage2DValidatorResult {
2955 texture,
2956 target,
2957 level,
2958 width,
2959 height,
2960 ..
2961 } = match validator.validate() {
2962 Ok(result) => result,
2963 Err(_) => return,
2964 };
2965
2966 let image_info = match texture.image_info_for_target(&target, level) {
2967 Some(info) => info,
2968 None => return self.webgl_error(InvalidOperation),
2969 };
2970
2971 if xoffset < 0 ||
2976 (xoffset as u32 + width) > image_info.width() ||
2977 yoffset < 0 ||
2978 (yoffset as u32 + height) > image_info.height()
2979 {
2980 self.webgl_error(InvalidValue);
2981 return;
2982 }
2983
2984 let msg = WebGLCommand::CopyTexSubImage2D(
2985 target.as_gl_constant(),
2986 level as i32,
2987 xoffset,
2988 yoffset,
2989 x,
2990 y,
2991 width as i32,
2992 height as i32,
2993 );
2994
2995 self.send_command(msg);
2996 }
2997
2998 fn Clear(&self, mask: u32) {
3000 handle_potential_webgl_error!(self, self.validate_framebuffer(), return);
3001 if mask &
3002 !(constants::DEPTH_BUFFER_BIT |
3003 constants::STENCIL_BUFFER_BIT |
3004 constants::COLOR_BUFFER_BIT) !=
3005 0
3006 {
3007 return self.webgl_error(InvalidValue);
3008 }
3009
3010 self.send_command(WebGLCommand::Clear(mask));
3011 self.mark_as_dirty();
3012 }
3013
3014 fn ClearColor(&self, red: f32, green: f32, blue: f32, alpha: f32) {
3016 self.current_clear_color.set((red, green, blue, alpha));
3017 self.send_command(WebGLCommand::ClearColor(red, green, blue, alpha));
3018 }
3019
3020 fn ClearDepth(&self, depth: f32) {
3022 self.send_command(WebGLCommand::ClearDepth(depth))
3023 }
3024
3025 fn ClearStencil(&self, stencil: i32) {
3027 self.send_command(WebGLCommand::ClearStencil(stencil))
3028 }
3029
3030 fn ColorMask(&self, r: bool, g: bool, b: bool, a: bool) {
3032 self.send_command(WebGLCommand::ColorMask(r, g, b, a))
3033 }
3034
3035 fn CullFace(&self, mode: u32) {
3037 match mode {
3038 constants::FRONT | constants::BACK | constants::FRONT_AND_BACK => {
3039 self.send_command(WebGLCommand::CullFace(mode))
3040 },
3041 _ => self.webgl_error(InvalidEnum),
3042 }
3043 }
3044
3045 fn FrontFace(&self, mode: u32) {
3047 match mode {
3048 constants::CW | constants::CCW => self.send_command(WebGLCommand::FrontFace(mode)),
3049 _ => self.webgl_error(InvalidEnum),
3050 }
3051 }
3052 fn DepthFunc(&self, func: u32) {
3054 match func {
3055 constants::NEVER |
3056 constants::LESS |
3057 constants::EQUAL |
3058 constants::LEQUAL |
3059 constants::GREATER |
3060 constants::NOTEQUAL |
3061 constants::GEQUAL |
3062 constants::ALWAYS => self.send_command(WebGLCommand::DepthFunc(func)),
3063 _ => self.webgl_error(InvalidEnum),
3064 }
3065 }
3066
3067 fn DepthMask(&self, flag: bool) {
3069 self.send_command(WebGLCommand::DepthMask(flag))
3070 }
3071
3072 fn DepthRange(&self, near: f32, far: f32) {
3074 if near > far {
3076 return self.webgl_error(InvalidOperation);
3077 }
3078 self.send_command(WebGLCommand::DepthRange(near, far))
3079 }
3080
3081 fn Enable(&self, cap: u32) {
3083 if handle_potential_webgl_error!(self, self.capabilities.set(cap, true), return) {
3084 self.send_command(WebGLCommand::Enable(cap));
3085 }
3086 }
3087
3088 fn Disable(&self, cap: u32) {
3090 if handle_potential_webgl_error!(self, self.capabilities.set(cap, false), return) {
3091 self.send_command(WebGLCommand::Disable(cap));
3092 }
3093 }
3094
3095 fn CompileShader(&self, shader: &WebGLShader) {
3097 handle_potential_webgl_error!(self, self.validate_ownership(shader), return);
3098 handle_potential_webgl_error!(
3099 self,
3100 shader.compile(
3101 self.api_type,
3102 self.webgl_version,
3103 self.glsl_version,
3104 &self.limits,
3105 &self.extension_manager,
3106 )
3107 )
3108 }
3109
3110 fn CreateBuffer(&self) -> Option<DomRoot<WebGLBuffer>> {
3112 WebGLBuffer::maybe_new(self, CanGc::deprecated_note())
3113 }
3114
3115 fn CreateFramebuffer(&self) -> Option<DomRoot<WebGLFramebuffer>> {
3117 WebGLFramebuffer::maybe_new(self, CanGc::deprecated_note())
3118 }
3119
3120 fn CreateRenderbuffer(&self) -> Option<DomRoot<WebGLRenderbuffer>> {
3122 WebGLRenderbuffer::maybe_new(self)
3123 }
3124
3125 fn CreateTexture(&self) -> Option<DomRoot<WebGLTexture>> {
3127 WebGLTexture::maybe_new(self)
3128 }
3129
3130 fn CreateProgram(&self) -> Option<DomRoot<WebGLProgram>> {
3132 WebGLProgram::maybe_new(self, CanGc::deprecated_note())
3133 }
3134
3135 fn CreateShader(&self, shader_type: u32) -> Option<DomRoot<WebGLShader>> {
3137 match shader_type {
3138 constants::VERTEX_SHADER | constants::FRAGMENT_SHADER => {},
3139 _ => {
3140 self.webgl_error(InvalidEnum);
3141 return None;
3142 },
3143 }
3144 WebGLShader::maybe_new(self, shader_type)
3145 }
3146
3147 fn DeleteBuffer(&self, buffer: Option<&WebGLBuffer>) {
3149 let buffer = match buffer {
3150 Some(buffer) => buffer,
3151 None => return,
3152 };
3153 handle_potential_webgl_error!(self, self.validate_ownership(buffer), return);
3154 if buffer.is_marked_for_deletion() {
3155 return;
3156 }
3157 self.current_vao().unbind_buffer(buffer);
3158 if self.bound_buffer_array.get().is_some_and(|b| buffer == &*b) {
3159 self.bound_buffer_array.set(None);
3160 buffer.decrement_attached_counter(Operation::Infallible);
3161 }
3162 buffer.mark_for_deletion(Operation::Infallible);
3163 }
3164
3165 fn DeleteFramebuffer(&self, framebuffer: Option<&WebGLFramebuffer>) {
3167 if let Some(framebuffer) = framebuffer {
3168 handle_potential_webgl_error!(self, framebuffer.validate_transparent(), return);
3172 handle_potential_webgl_error!(self, self.validate_ownership(framebuffer), return);
3173 handle_object_deletion!(
3174 self,
3175 self.bound_draw_framebuffer,
3176 framebuffer,
3177 Some(WebGLCommand::BindFramebuffer(
3178 framebuffer.target().unwrap(),
3179 WebGLFramebufferBindingRequest::Default
3180 ))
3181 );
3182 framebuffer.delete(Operation::Infallible)
3183 }
3184 }
3185
3186 fn DeleteRenderbuffer(&self, renderbuffer: Option<&WebGLRenderbuffer>) {
3188 if let Some(renderbuffer) = renderbuffer {
3189 handle_potential_webgl_error!(self, self.validate_ownership(renderbuffer), return);
3190 handle_object_deletion!(
3191 self,
3192 self.bound_renderbuffer,
3193 renderbuffer,
3194 Some(WebGLCommand::BindRenderbuffer(
3195 constants::RENDERBUFFER,
3196 None
3197 ))
3198 );
3199 renderbuffer.delete(Operation::Infallible)
3200 }
3201 }
3202
3203 fn DeleteTexture(&self, texture: Option<&WebGLTexture>) {
3205 if let Some(texture) = texture {
3206 handle_potential_webgl_error!(self, self.validate_ownership(texture), return);
3207
3208 let mut active_unit_enum = self.textures.active_unit_enum();
3217 for (unit_enum, slot) in self.textures.iter() {
3218 if let Some(target) = slot.unbind(texture) {
3219 if unit_enum != active_unit_enum {
3220 self.send_command(WebGLCommand::ActiveTexture(unit_enum));
3221 active_unit_enum = unit_enum;
3222 }
3223 self.send_command(WebGLCommand::BindTexture(target, None));
3224 }
3225 }
3226
3227 if active_unit_enum != self.textures.active_unit_enum() {
3229 self.send_command(WebGLCommand::ActiveTexture(
3230 self.textures.active_unit_enum(),
3231 ));
3232 }
3233
3234 texture.delete(Operation::Infallible)
3235 }
3236 }
3237
3238 fn DeleteProgram(&self, program: Option<&WebGLProgram>) {
3240 if let Some(program) = program {
3241 handle_potential_webgl_error!(self, self.validate_ownership(program), return);
3242 program.mark_for_deletion(Operation::Infallible)
3243 }
3244 }
3245
3246 fn DeleteShader(&self, shader: Option<&WebGLShader>) {
3248 if let Some(shader) = shader {
3249 handle_potential_webgl_error!(self, self.validate_ownership(shader), return);
3250 shader.mark_for_deletion(Operation::Infallible)
3251 }
3252 }
3253
3254 fn DrawArrays(&self, mode: u32, first: i32, count: i32) {
3256 handle_potential_webgl_error!(self, self.draw_arrays_instanced(mode, first, count, 1));
3257 }
3258
3259 fn DrawElements(&self, mode: u32, count: i32, type_: u32, offset: i64) {
3261 handle_potential_webgl_error!(
3262 self,
3263 self.draw_elements_instanced(mode, count, type_, offset, 1)
3264 );
3265 }
3266
3267 fn EnableVertexAttribArray(&self, attrib_id: u32) {
3269 if attrib_id >= self.limits.max_vertex_attribs {
3270 return self.webgl_error(InvalidValue);
3271 }
3272 match self.webgl_version() {
3273 WebGLVersion::WebGL1 => self
3274 .current_vao()
3275 .enabled_vertex_attrib_array(attrib_id, true),
3276 WebGLVersion::WebGL2 => self
3277 .current_vao_webgl2()
3278 .enabled_vertex_attrib_array(attrib_id, true),
3279 };
3280 self.send_command(WebGLCommand::EnableVertexAttribArray(attrib_id));
3281 }
3282
3283 fn DisableVertexAttribArray(&self, attrib_id: u32) {
3285 if attrib_id >= self.limits.max_vertex_attribs {
3286 return self.webgl_error(InvalidValue);
3287 }
3288 match self.webgl_version() {
3289 WebGLVersion::WebGL1 => self
3290 .current_vao()
3291 .enabled_vertex_attrib_array(attrib_id, false),
3292 WebGLVersion::WebGL2 => self
3293 .current_vao_webgl2()
3294 .enabled_vertex_attrib_array(attrib_id, false),
3295 };
3296 self.send_command(WebGLCommand::DisableVertexAttribArray(attrib_id));
3297 }
3298
3299 fn GetActiveUniform(
3301 &self,
3302 program: &WebGLProgram,
3303 index: u32,
3304 ) -> Option<DomRoot<WebGLActiveInfo>> {
3305 handle_potential_webgl_error!(self, self.validate_ownership(program), return None);
3306 match program.get_active_uniform(index, CanGc::deprecated_note()) {
3307 Ok(ret) => Some(ret),
3308 Err(e) => {
3309 self.webgl_error(e);
3310 None
3311 },
3312 }
3313 }
3314
3315 fn GetActiveAttrib(
3317 &self,
3318 program: &WebGLProgram,
3319 index: u32,
3320 ) -> Option<DomRoot<WebGLActiveInfo>> {
3321 handle_potential_webgl_error!(self, self.validate_ownership(program), return None);
3322 handle_potential_webgl_error!(
3323 self,
3324 program
3325 .get_active_attrib(index, CanGc::deprecated_note())
3326 .map(Some),
3327 None
3328 )
3329 }
3330
3331 fn GetAttribLocation(&self, program: &WebGLProgram, name: DOMString) -> i32 {
3333 handle_potential_webgl_error!(self, self.validate_ownership(program), return -1);
3334 handle_potential_webgl_error!(self, program.get_attrib_location(name), -1)
3335 }
3336
3337 fn GetFramebufferAttachmentParameter(
3339 &self,
3340 cx: SafeJSContext,
3341 target: u32,
3342 attachment: u32,
3343 pname: u32,
3344 mut retval: MutableHandleValue,
3345 ) {
3346 if let Some(fb) = self.bound_draw_framebuffer.get() {
3348 handle_potential_webgl_error!(
3351 self,
3352 fb.validate_transparent(),
3353 return retval.set(NullValue())
3354 );
3355 } else {
3356 self.webgl_error(InvalidOperation);
3357 return retval.set(NullValue());
3358 }
3359
3360 let target_matches = match target {
3362 constants::FRAMEBUFFER => true,
3365 _ => false,
3366 };
3367 let attachment_matches = match attachment {
3368 constants::COLOR_ATTACHMENT0 |
3371 constants::DEPTH_STENCIL_ATTACHMENT |
3372 constants::DEPTH_ATTACHMENT |
3373 constants::STENCIL_ATTACHMENT => true,
3374 _ => false,
3375 };
3376 let pname_matches = match pname {
3377 constants::FRAMEBUFFER_ATTACHMENT_OBJECT_NAME |
3387 constants::FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE |
3388 constants::FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE |
3389 constants::FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL => true,
3390 _ => false,
3391 };
3392
3393 let bound_attachment_matches = match self
3394 .bound_draw_framebuffer
3395 .get()
3396 .unwrap()
3397 .attachment(attachment)
3398 {
3399 Some(attachment_root) => match attachment_root {
3400 WebGLFramebufferAttachmentRoot::Renderbuffer(_) => matches!(
3401 pname,
3402 constants::FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE |
3403 constants::FRAMEBUFFER_ATTACHMENT_OBJECT_NAME
3404 ),
3405 WebGLFramebufferAttachmentRoot::Texture(_) => matches!(
3406 pname,
3407 constants::FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE |
3408 constants::FRAMEBUFFER_ATTACHMENT_OBJECT_NAME |
3409 constants::FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL |
3410 constants::FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE
3411 ),
3412 },
3413 _ => matches!(pname, constants::FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE),
3414 };
3415
3416 if !target_matches || !attachment_matches || !pname_matches || !bound_attachment_matches {
3417 self.webgl_error(InvalidEnum);
3418 return retval.set(NullValue());
3419 }
3420
3421 if pname == constants::FRAMEBUFFER_ATTACHMENT_OBJECT_NAME {
3428 let fb = self.bound_draw_framebuffer.get().unwrap();
3431 if let Some(webgl_attachment) = fb.attachment(attachment) {
3432 match webgl_attachment {
3433 WebGLFramebufferAttachmentRoot::Renderbuffer(rb) => {
3434 rb.safe_to_jsval(cx, retval, CanGc::deprecated_note());
3435 return;
3436 },
3437 WebGLFramebufferAttachmentRoot::Texture(texture) => {
3438 texture.safe_to_jsval(cx, retval, CanGc::deprecated_note());
3439 return;
3440 },
3441 }
3442 }
3443 self.webgl_error(InvalidEnum);
3444 return retval.set(NullValue());
3445 }
3446
3447 let (sender, receiver) = webgl_channel().unwrap();
3448 self.send_command(WebGLCommand::GetFramebufferAttachmentParameter(
3449 target, attachment, pname, sender,
3450 ));
3451
3452 retval.set(Int32Value(receiver.recv().unwrap()))
3453 }
3454
3455 fn GetRenderbufferParameter(
3457 &self,
3458 _cx: SafeJSContext,
3459 target: u32,
3460 pname: u32,
3461 mut retval: MutableHandleValue,
3462 ) {
3463 let target_matches = target == constants::RENDERBUFFER;
3466
3467 let pname_matches = matches!(
3468 pname,
3469 constants::RENDERBUFFER_WIDTH |
3470 constants::RENDERBUFFER_HEIGHT |
3471 constants::RENDERBUFFER_INTERNAL_FORMAT |
3472 constants::RENDERBUFFER_RED_SIZE |
3473 constants::RENDERBUFFER_GREEN_SIZE |
3474 constants::RENDERBUFFER_BLUE_SIZE |
3475 constants::RENDERBUFFER_ALPHA_SIZE |
3476 constants::RENDERBUFFER_DEPTH_SIZE |
3477 constants::RENDERBUFFER_STENCIL_SIZE
3478 );
3479
3480 if !target_matches || !pname_matches {
3481 self.webgl_error(InvalidEnum);
3482 return retval.set(NullValue());
3483 }
3484
3485 if self.bound_renderbuffer.get().is_none() {
3486 self.webgl_error(InvalidOperation);
3487 return retval.set(NullValue());
3488 }
3489
3490 let result = if pname == constants::RENDERBUFFER_INTERNAL_FORMAT {
3491 let rb = self.bound_renderbuffer.get().unwrap();
3492 rb.internal_format() as i32
3493 } else {
3494 let (sender, receiver) = webgl_channel().unwrap();
3495 self.send_command(WebGLCommand::GetRenderbufferParameter(
3496 target, pname, sender,
3497 ));
3498 receiver.recv().unwrap()
3499 };
3500
3501 retval.set(Int32Value(result))
3502 }
3503
3504 fn GetProgramInfoLog(&self, program: &WebGLProgram) -> Option<DOMString> {
3506 handle_potential_webgl_error!(self, self.validate_ownership(program), return None);
3507 match program.get_info_log() {
3508 Ok(value) => Some(DOMString::from(value)),
3509 Err(e) => {
3510 self.webgl_error(e);
3511 None
3512 },
3513 }
3514 }
3515
3516 fn GetProgramParameter(
3518 &self,
3519 _: SafeJSContext,
3520 program: &WebGLProgram,
3521 param: u32,
3522 mut retval: MutableHandleValue,
3523 ) {
3524 handle_potential_webgl_error!(
3525 self,
3526 self.validate_ownership(program),
3527 return retval.set(NullValue())
3528 );
3529 if program.is_deleted() {
3530 self.webgl_error(InvalidOperation);
3531 return retval.set(NullValue());
3532 }
3533 retval.set(match param {
3534 constants::DELETE_STATUS => BooleanValue(program.is_marked_for_deletion()),
3535 constants::LINK_STATUS => BooleanValue(program.is_linked()),
3536 constants::VALIDATE_STATUS => {
3537 let (sender, receiver) = webgl_channel().unwrap();
3540 self.send_command(WebGLCommand::GetProgramValidateStatus(program.id(), sender));
3541 BooleanValue(receiver.recv().unwrap())
3542 },
3543 constants::ATTACHED_SHADERS => {
3544 Int32Value(
3546 program
3547 .attached_shaders()
3548 .map(|shaders| shaders.len() as i32)
3549 .unwrap_or(0),
3550 )
3551 },
3552 constants::ACTIVE_ATTRIBUTES => Int32Value(program.active_attribs().len() as i32),
3553 constants::ACTIVE_UNIFORMS => Int32Value(program.active_uniforms().len() as i32),
3554 _ => {
3555 self.webgl_error(InvalidEnum);
3556 NullValue()
3557 },
3558 })
3559 }
3560
3561 fn GetShaderInfoLog(&self, shader: &WebGLShader) -> Option<DOMString> {
3563 handle_potential_webgl_error!(self, self.validate_ownership(shader), return None);
3564 Some(shader.info_log())
3565 }
3566
3567 fn GetShaderParameter(
3569 &self,
3570 _: SafeJSContext,
3571 shader: &WebGLShader,
3572 param: u32,
3573 mut retval: MutableHandleValue,
3574 ) {
3575 handle_potential_webgl_error!(
3576 self,
3577 self.validate_ownership(shader),
3578 return retval.set(NullValue())
3579 );
3580 if shader.is_deleted() {
3581 self.webgl_error(InvalidValue);
3582 return retval.set(NullValue());
3583 }
3584 retval.set(match param {
3585 constants::DELETE_STATUS => BooleanValue(shader.is_marked_for_deletion()),
3586 constants::COMPILE_STATUS => BooleanValue(shader.successfully_compiled()),
3587 constants::SHADER_TYPE => UInt32Value(shader.gl_type()),
3588 _ => {
3589 self.webgl_error(InvalidEnum);
3590 NullValue()
3591 },
3592 })
3593 }
3594
3595 fn GetShaderPrecisionFormat(
3597 &self,
3598 shader_type: u32,
3599 precision_type: u32,
3600 ) -> Option<DomRoot<WebGLShaderPrecisionFormat>> {
3601 match shader_type {
3602 constants::FRAGMENT_SHADER | constants::VERTEX_SHADER => (),
3603 _ => {
3604 self.webgl_error(InvalidEnum);
3605 return None;
3606 },
3607 }
3608
3609 match precision_type {
3610 constants::LOW_FLOAT |
3611 constants::MEDIUM_FLOAT |
3612 constants::HIGH_FLOAT |
3613 constants::LOW_INT |
3614 constants::MEDIUM_INT |
3615 constants::HIGH_INT => (),
3616 _ => {
3617 self.webgl_error(InvalidEnum);
3618 return None;
3619 },
3620 }
3621
3622 let (sender, receiver) = webgl_channel().unwrap();
3623 self.send_command(WebGLCommand::GetShaderPrecisionFormat(
3624 shader_type,
3625 precision_type,
3626 sender,
3627 ));
3628
3629 let (range_min, range_max, precision) = receiver.recv().unwrap();
3630 Some(WebGLShaderPrecisionFormat::new(
3631 self.global().as_window(),
3632 range_min,
3633 range_max,
3634 precision,
3635 CanGc::deprecated_note(),
3636 ))
3637 }
3638
3639 fn GetUniformLocation(
3641 &self,
3642 program: &WebGLProgram,
3643 name: DOMString,
3644 ) -> Option<DomRoot<WebGLUniformLocation>> {
3645 handle_potential_webgl_error!(self, self.validate_ownership(program), return None);
3646 handle_potential_webgl_error!(
3647 self,
3648 program.get_uniform_location(name, CanGc::deprecated_note()),
3649 None
3650 )
3651 }
3652
3653 #[expect(unsafe_code)]
3654 fn GetVertexAttrib(
3656 &self,
3657 cx: SafeJSContext,
3658 index: u32,
3659 param: u32,
3660 mut retval: MutableHandleValue,
3661 ) {
3662 let mut get_attrib = |data: Ref<'_, VertexAttribData>| {
3663 if param == constants::CURRENT_VERTEX_ATTRIB {
3664 let attrib = self.current_vertex_attribs.borrow()[index as usize];
3665 match attrib {
3666 VertexAttrib::Float(x, y, z, w) => {
3667 let value = [x, y, z, w];
3668 unsafe {
3669 rooted!(in(*cx) let mut result = ptr::null_mut::<JSObject>());
3670 Float32Array::create(
3671 *cx,
3672 CreateWith::Slice(&value),
3673 result.handle_mut(),
3674 )
3675 .unwrap();
3676 return retval.set(ObjectValue(result.get()));
3677 }
3678 },
3679 VertexAttrib::Int(x, y, z, w) => {
3680 let value = [x, y, z, w];
3681 unsafe {
3682 rooted!(in(*cx) let mut result = ptr::null_mut::<JSObject>());
3683 Int32Array::create(*cx, CreateWith::Slice(&value), result.handle_mut())
3684 .unwrap();
3685 return retval.set(ObjectValue(result.get()));
3686 }
3687 },
3688 VertexAttrib::Uint(x, y, z, w) => {
3689 let value = [x, y, z, w];
3690 unsafe {
3691 rooted!(in(*cx) let mut result = ptr::null_mut::<JSObject>());
3692 Uint32Array::create(
3693 *cx,
3694 CreateWith::Slice(&value),
3695 result.handle_mut(),
3696 )
3697 .unwrap();
3698 return retval.set(ObjectValue(result.get()));
3699 }
3700 },
3701 };
3702 }
3703 if !self
3704 .extension_manager
3705 .is_get_vertex_attrib_name_enabled(param)
3706 {
3707 self.webgl_error(WebGLError::InvalidEnum);
3708 return retval.set(NullValue());
3709 }
3710
3711 match param {
3712 constants::VERTEX_ATTRIB_ARRAY_ENABLED => {
3713 retval.set(BooleanValue(data.enabled_as_array))
3714 },
3715 constants::VERTEX_ATTRIB_ARRAY_SIZE => retval.set(Int32Value(data.size as i32)),
3716 constants::VERTEX_ATTRIB_ARRAY_TYPE => retval.set(Int32Value(data.type_ as i32)),
3717 constants::VERTEX_ATTRIB_ARRAY_NORMALIZED => {
3718 retval.set(BooleanValue(data.normalized))
3719 },
3720 constants::VERTEX_ATTRIB_ARRAY_STRIDE => retval.set(Int32Value(data.stride as i32)),
3721 constants::VERTEX_ATTRIB_ARRAY_BUFFER_BINDING => {
3722 if let Some(buffer) = data.buffer() {
3723 buffer.safe_to_jsval(cx, retval.reborrow(), CanGc::deprecated_note());
3724 } else {
3725 retval.set(NullValue());
3726 }
3727 },
3728 ANGLEInstancedArraysConstants::VERTEX_ATTRIB_ARRAY_DIVISOR_ANGLE => {
3729 retval.set(UInt32Value(data.divisor))
3730 },
3731 _ => {
3732 self.webgl_error(InvalidEnum);
3733 retval.set(NullValue())
3734 },
3735 }
3736 };
3737
3738 match self.webgl_version() {
3739 WebGLVersion::WebGL1 => {
3740 let current_vao = self.current_vao();
3741 let data = handle_potential_webgl_error!(
3742 self,
3743 current_vao.get_vertex_attrib(index).ok_or(InvalidValue),
3744 return retval.set(NullValue())
3745 );
3746 get_attrib(data)
3747 },
3748 WebGLVersion::WebGL2 => {
3749 let current_vao = self.current_vao_webgl2();
3750 let data = handle_potential_webgl_error!(
3751 self,
3752 current_vao.get_vertex_attrib(index).ok_or(InvalidValue),
3753 return retval.set(NullValue())
3754 );
3755 get_attrib(data)
3756 },
3757 }
3758 }
3759
3760 fn GetVertexAttribOffset(&self, index: u32, pname: u32) -> i64 {
3762 if pname != constants::VERTEX_ATTRIB_ARRAY_POINTER {
3763 self.webgl_error(InvalidEnum);
3764 return 0;
3765 }
3766 match self.webgl_version() {
3767 WebGLVersion::WebGL1 => {
3768 let current_vao = self.current_vao();
3769 let data = handle_potential_webgl_error!(
3770 self,
3771 current_vao.get_vertex_attrib(index).ok_or(InvalidValue),
3772 return 0
3773 );
3774 data.offset as i64
3775 },
3776 WebGLVersion::WebGL2 => {
3777 let current_vao = self.current_vao_webgl2();
3778 let data = handle_potential_webgl_error!(
3779 self,
3780 current_vao.get_vertex_attrib(index).ok_or(InvalidValue),
3781 return 0
3782 );
3783 data.offset as i64
3784 },
3785 }
3786 }
3787
3788 fn Hint(&self, target: u32, mode: u32) {
3790 if target != constants::GENERATE_MIPMAP_HINT &&
3791 !self.extension_manager.is_hint_target_enabled(target)
3792 {
3793 return self.webgl_error(InvalidEnum);
3794 }
3795
3796 match mode {
3797 constants::FASTEST | constants::NICEST | constants::DONT_CARE => (),
3798
3799 _ => return self.webgl_error(InvalidEnum),
3800 }
3801
3802 self.send_command(WebGLCommand::Hint(target, mode));
3803 }
3804
3805 fn IsBuffer(&self, buffer: Option<&WebGLBuffer>) -> bool {
3807 buffer.is_some_and(|buf| {
3808 self.validate_ownership(buf).is_ok() && buf.target().is_some() && !buf.is_deleted()
3809 })
3810 }
3811
3812 fn IsEnabled(&self, cap: u32) -> bool {
3814 handle_potential_webgl_error!(self, self.capabilities.is_enabled(cap), false)
3815 }
3816
3817 fn IsFramebuffer(&self, frame_buffer: Option<&WebGLFramebuffer>) -> bool {
3819 frame_buffer.is_some_and(|buf| {
3820 self.validate_ownership(buf).is_ok() && buf.target().is_some() && !buf.is_deleted()
3821 })
3822 }
3823
3824 fn IsProgram(&self, program: Option<&WebGLProgram>) -> bool {
3826 program.is_some_and(|p| self.validate_ownership(p).is_ok() && !p.is_deleted())
3827 }
3828
3829 fn IsRenderbuffer(&self, render_buffer: Option<&WebGLRenderbuffer>) -> bool {
3831 render_buffer.is_some_and(|buf| {
3832 self.validate_ownership(buf).is_ok() && buf.ever_bound() && !buf.is_deleted()
3833 })
3834 }
3835
3836 fn IsShader(&self, shader: Option<&WebGLShader>) -> bool {
3838 shader.is_some_and(|s| self.validate_ownership(s).is_ok() && !s.is_deleted())
3839 }
3840
3841 fn IsTexture(&self, texture: Option<&WebGLTexture>) -> bool {
3843 texture.is_some_and(|tex| {
3844 self.validate_ownership(tex).is_ok() && tex.target().is_some() && !tex.is_invalid()
3845 })
3846 }
3847
3848 fn LineWidth(&self, width: f32) {
3850 if width.is_nan() || width <= 0f32 {
3851 return self.webgl_error(InvalidValue);
3852 }
3853
3854 self.send_command(WebGLCommand::LineWidth(width))
3855 }
3856
3857 fn PixelStorei(&self, param_name: u32, param_value: i32) {
3862 let mut texture_settings = self.texture_unpacking_settings.get();
3863 match param_name {
3864 constants::UNPACK_FLIP_Y_WEBGL => {
3865 texture_settings.set(TextureUnpacking::FLIP_Y_AXIS, param_value != 0);
3866 },
3867 constants::UNPACK_PREMULTIPLY_ALPHA_WEBGL => {
3868 texture_settings.set(TextureUnpacking::PREMULTIPLY_ALPHA, param_value != 0);
3869 },
3870 constants::UNPACK_COLORSPACE_CONVERSION_WEBGL => {
3871 let convert = match param_value as u32 {
3872 constants::BROWSER_DEFAULT_WEBGL => true,
3873 constants::NONE => false,
3874 _ => return self.webgl_error(InvalidEnum),
3875 };
3876 texture_settings.set(TextureUnpacking::CONVERT_COLORSPACE, convert);
3877 },
3878 constants::UNPACK_ALIGNMENT => {
3879 match param_value {
3880 1 | 2 | 4 | 8 => (),
3881 _ => return self.webgl_error(InvalidValue),
3882 }
3883 self.texture_unpacking_alignment.set(param_value as u32);
3884 return;
3885 },
3886 constants::PACK_ALIGNMENT => {
3887 match param_value {
3888 1 | 2 | 4 | 8 => (),
3889 _ => return self.webgl_error(InvalidValue),
3890 }
3891 self.texture_packing_alignment.set(param_value as u8);
3895 return;
3896 },
3897 _ => return self.webgl_error(InvalidEnum),
3898 }
3899 self.texture_unpacking_settings.set(texture_settings);
3900 }
3901
3902 fn PolygonOffset(&self, factor: f32, units: f32) {
3904 self.send_command(WebGLCommand::PolygonOffset(factor, units))
3905 }
3906
3907 #[expect(unsafe_code)]
3909 fn ReadPixels(
3910 &self,
3911 x: i32,
3912 y: i32,
3913 width: i32,
3914 height: i32,
3915 format: u32,
3916 pixel_type: u32,
3917 mut pixels: CustomAutoRooterGuard<Option<ArrayBufferView>>,
3918 ) {
3919 handle_potential_webgl_error!(self, self.validate_framebuffer(), return);
3920
3921 let pixels =
3922 handle_potential_webgl_error!(self, pixels.as_mut().ok_or(InvalidValue), return);
3923
3924 if width < 0 || height < 0 {
3925 return self.webgl_error(InvalidValue);
3926 }
3927
3928 if format != constants::RGBA || pixel_type != constants::UNSIGNED_BYTE {
3929 return self.webgl_error(InvalidOperation);
3930 }
3931
3932 if pixels.get_array_type() != Type::Uint8 {
3933 return self.webgl_error(InvalidOperation);
3934 }
3935
3936 let (fb_width, fb_height) = handle_potential_webgl_error!(
3937 self,
3938 self.get_current_framebuffer_size().ok_or(InvalidOperation),
3939 return
3940 );
3941
3942 if width == 0 || height == 0 {
3943 return;
3944 }
3945
3946 let bytes_per_pixel = 4;
3947
3948 let row_len = handle_potential_webgl_error!(
3949 self,
3950 width.checked_mul(bytes_per_pixel).ok_or(InvalidOperation),
3951 return
3952 );
3953
3954 let pack_alignment = self.texture_packing_alignment.get() as i32;
3955 let dest_padding = match row_len % pack_alignment {
3956 0 => 0,
3957 remainder => pack_alignment - remainder,
3958 };
3959 let dest_stride = row_len + dest_padding;
3960
3961 let full_rows_len = handle_potential_webgl_error!(
3962 self,
3963 dest_stride.checked_mul(height - 1).ok_or(InvalidOperation),
3964 return
3965 );
3966 let required_dest_len = handle_potential_webgl_error!(
3967 self,
3968 full_rows_len.checked_add(row_len).ok_or(InvalidOperation),
3969 return
3970 );
3971
3972 let dest = unsafe { pixels.as_mut_slice() };
3973 if dest.len() < required_dest_len as usize {
3974 return self.webgl_error(InvalidOperation);
3975 }
3976
3977 let src_origin = Point2D::new(x, y);
3978 let src_size = Size2D::new(width as u32, height as u32);
3979 let fb_size = Size2D::new(fb_width as u32, fb_height as u32);
3980 let src_rect = match pixels::clip(src_origin, src_size.to_u32(), fb_size.to_u32()) {
3981 Some(rect) => rect,
3982 None => return,
3983 };
3984
3985 let src_rect = src_rect.to_u32();
3989
3990 let mut dest_offset = 0;
3991 if x < 0 {
3992 dest_offset += -x * bytes_per_pixel;
3993 }
3994 if y < 0 {
3995 dest_offset += -y * row_len;
3996 }
3997
3998 let (sender, receiver) = generic_channel::channel().unwrap();
3999 self.send_command(WebGLCommand::ReadPixels(
4000 src_rect, format, pixel_type, sender,
4001 ));
4002 let (src, _) = receiver.recv().unwrap();
4003
4004 let src_row_len = src_rect.size.width as usize * bytes_per_pixel as usize;
4005 for i in 0..src_rect.size.height {
4006 let dest_start = dest_offset as usize + i as usize * dest_stride as usize;
4007 let dest_end = dest_start + src_row_len;
4008 let src_start = i as usize * src_row_len;
4009 let src_end = src_start + src_row_len;
4010 dest[dest_start..dest_end].copy_from_slice(&src[src_start..src_end]);
4011 }
4012 }
4013
4014 fn SampleCoverage(&self, value: f32, invert: bool) {
4016 self.send_command(WebGLCommand::SampleCoverage(value, invert));
4017 }
4018
4019 fn Scissor(&self, x: i32, y: i32, width: i32, height: i32) {
4021 if width < 0 || height < 0 {
4022 return self.webgl_error(InvalidValue);
4023 }
4024
4025 let width = width as u32;
4026 let height = height as u32;
4027
4028 self.current_scissor.set((x, y, width, height));
4029 self.send_command(WebGLCommand::Scissor(x, y, width, height));
4030 }
4031
4032 fn StencilFunc(&self, func: u32, ref_: i32, mask: u32) {
4034 match func {
4035 constants::NEVER |
4036 constants::LESS |
4037 constants::EQUAL |
4038 constants::LEQUAL |
4039 constants::GREATER |
4040 constants::NOTEQUAL |
4041 constants::GEQUAL |
4042 constants::ALWAYS => self.send_command(WebGLCommand::StencilFunc(func, ref_, mask)),
4043 _ => self.webgl_error(InvalidEnum),
4044 }
4045 }
4046
4047 fn StencilFuncSeparate(&self, face: u32, func: u32, ref_: i32, mask: u32) {
4049 match face {
4050 constants::FRONT | constants::BACK | constants::FRONT_AND_BACK => (),
4051 _ => return self.webgl_error(InvalidEnum),
4052 }
4053
4054 match func {
4055 constants::NEVER |
4056 constants::LESS |
4057 constants::EQUAL |
4058 constants::LEQUAL |
4059 constants::GREATER |
4060 constants::NOTEQUAL |
4061 constants::GEQUAL |
4062 constants::ALWAYS => {
4063 self.send_command(WebGLCommand::StencilFuncSeparate(face, func, ref_, mask))
4064 },
4065 _ => self.webgl_error(InvalidEnum),
4066 }
4067 }
4068
4069 fn StencilMask(&self, mask: u32) {
4071 self.send_command(WebGLCommand::StencilMask(mask))
4072 }
4073
4074 fn StencilMaskSeparate(&self, face: u32, mask: u32) {
4076 match face {
4077 constants::FRONT | constants::BACK | constants::FRONT_AND_BACK => {
4078 self.send_command(WebGLCommand::StencilMaskSeparate(face, mask))
4079 },
4080 _ => self.webgl_error(InvalidEnum),
4081 };
4082 }
4083
4084 fn StencilOp(&self, fail: u32, zfail: u32, zpass: u32) {
4086 if self.validate_stencil_actions(fail) &&
4087 self.validate_stencil_actions(zfail) &&
4088 self.validate_stencil_actions(zpass)
4089 {
4090 self.send_command(WebGLCommand::StencilOp(fail, zfail, zpass));
4091 } else {
4092 self.webgl_error(InvalidEnum)
4093 }
4094 }
4095
4096 fn StencilOpSeparate(&self, face: u32, fail: u32, zfail: u32, zpass: u32) {
4098 match face {
4099 constants::FRONT | constants::BACK | constants::FRONT_AND_BACK => (),
4100 _ => return self.webgl_error(InvalidEnum),
4101 }
4102
4103 if self.validate_stencil_actions(fail) &&
4104 self.validate_stencil_actions(zfail) &&
4105 self.validate_stencil_actions(zpass)
4106 {
4107 self.send_command(WebGLCommand::StencilOpSeparate(face, fail, zfail, zpass))
4108 } else {
4109 self.webgl_error(InvalidEnum)
4110 }
4111 }
4112
4113 fn LinkProgram(&self, program: &WebGLProgram) {
4115 handle_potential_webgl_error!(self, self.validate_ownership(program), return);
4116 if program.is_deleted() {
4117 return self.webgl_error(InvalidValue);
4118 }
4119 handle_potential_webgl_error!(self, program.link());
4120 }
4121
4122 fn ShaderSource(&self, shader: &WebGLShader, source: DOMString) {
4124 handle_potential_webgl_error!(self, self.validate_ownership(shader), return);
4125 shader.set_source(source)
4126 }
4127
4128 fn GetShaderSource(&self, shader: &WebGLShader) -> Option<DOMString> {
4130 handle_potential_webgl_error!(self, self.validate_ownership(shader), return None);
4131 Some(shader.source())
4132 }
4133
4134 fn Uniform1f(&self, location: Option<&WebGLUniformLocation>, val: f32) {
4136 self.with_location(location, |location| {
4137 match location.type_() {
4138 constants::BOOL | constants::FLOAT => {},
4139 _ => return Err(InvalidOperation),
4140 }
4141 self.send_command(WebGLCommand::Uniform1f(location.id(), val));
4142 Ok(())
4143 });
4144 }
4145
4146 fn Uniform1i(&self, location: Option<&WebGLUniformLocation>, val: i32) {
4148 self.with_location(location, |location| {
4149 match location.type_() {
4150 constants::BOOL | constants::INT => {},
4151 constants::SAMPLER_2D |
4152 WebGL2RenderingContextConstants::SAMPLER_3D |
4153 WebGL2RenderingContextConstants::SAMPLER_2D_ARRAY |
4154 constants::SAMPLER_CUBE => {
4155 if val < 0 || val as u32 >= self.limits.max_combined_texture_image_units {
4156 return Err(InvalidValue);
4157 }
4158 },
4159 _ => return Err(InvalidOperation),
4160 }
4161 self.send_command(WebGLCommand::Uniform1i(location.id(), val));
4162 Ok(())
4163 });
4164 }
4165
4166 fn Uniform1iv(&self, location: Option<&WebGLUniformLocation>, val: Int32ArrayOrLongSequence) {
4168 self.uniform1iv(location, val, 0, 0)
4169 }
4170
4171 fn Uniform1fv(
4173 &self,
4174 location: Option<&WebGLUniformLocation>,
4175 val: Float32ArrayOrUnrestrictedFloatSequence,
4176 ) {
4177 self.uniform1fv(location, val, 0, 0)
4178 }
4179
4180 fn Uniform2f(&self, location: Option<&WebGLUniformLocation>, x: f32, y: f32) {
4182 self.with_location(location, |location| {
4183 match location.type_() {
4184 constants::BOOL_VEC2 | constants::FLOAT_VEC2 => {},
4185 _ => return Err(InvalidOperation),
4186 }
4187 self.send_command(WebGLCommand::Uniform2f(location.id(), x, y));
4188 Ok(())
4189 });
4190 }
4191
4192 fn Uniform2fv(
4194 &self,
4195 location: Option<&WebGLUniformLocation>,
4196 val: Float32ArrayOrUnrestrictedFloatSequence,
4197 ) {
4198 self.uniform2fv(location, val, 0, 0)
4199 }
4200
4201 fn Uniform2i(&self, location: Option<&WebGLUniformLocation>, x: i32, y: i32) {
4203 self.with_location(location, |location| {
4204 match location.type_() {
4205 constants::BOOL_VEC2 | constants::INT_VEC2 => {},
4206 _ => return Err(InvalidOperation),
4207 }
4208 self.send_command(WebGLCommand::Uniform2i(location.id(), x, y));
4209 Ok(())
4210 });
4211 }
4212
4213 fn Uniform2iv(&self, location: Option<&WebGLUniformLocation>, val: Int32ArrayOrLongSequence) {
4215 self.uniform2iv(location, val, 0, 0)
4216 }
4217
4218 fn Uniform3f(&self, location: Option<&WebGLUniformLocation>, x: f32, y: f32, z: f32) {
4220 self.with_location(location, |location| {
4221 match location.type_() {
4222 constants::BOOL_VEC3 | constants::FLOAT_VEC3 => {},
4223 _ => return Err(InvalidOperation),
4224 }
4225 self.send_command(WebGLCommand::Uniform3f(location.id(), x, y, z));
4226 Ok(())
4227 });
4228 }
4229
4230 fn Uniform3fv(
4232 &self,
4233 location: Option<&WebGLUniformLocation>,
4234 val: Float32ArrayOrUnrestrictedFloatSequence,
4235 ) {
4236 self.uniform3fv(location, val, 0, 0)
4237 }
4238
4239 fn Uniform3i(&self, location: Option<&WebGLUniformLocation>, x: i32, y: i32, z: i32) {
4241 self.with_location(location, |location| {
4242 match location.type_() {
4243 constants::BOOL_VEC3 | constants::INT_VEC3 => {},
4244 _ => return Err(InvalidOperation),
4245 }
4246 self.send_command(WebGLCommand::Uniform3i(location.id(), x, y, z));
4247 Ok(())
4248 });
4249 }
4250
4251 fn Uniform3iv(&self, location: Option<&WebGLUniformLocation>, val: Int32ArrayOrLongSequence) {
4253 self.uniform3iv(location, val, 0, 0)
4254 }
4255
4256 fn Uniform4i(&self, location: Option<&WebGLUniformLocation>, x: i32, y: i32, z: i32, w: i32) {
4258 self.with_location(location, |location| {
4259 match location.type_() {
4260 constants::BOOL_VEC4 | constants::INT_VEC4 => {},
4261 _ => return Err(InvalidOperation),
4262 }
4263 self.send_command(WebGLCommand::Uniform4i(location.id(), x, y, z, w));
4264 Ok(())
4265 });
4266 }
4267
4268 fn Uniform4iv(&self, location: Option<&WebGLUniformLocation>, val: Int32ArrayOrLongSequence) {
4270 self.uniform4iv(location, val, 0, 0)
4271 }
4272
4273 fn Uniform4f(&self, location: Option<&WebGLUniformLocation>, x: f32, y: f32, z: f32, w: f32) {
4275 self.with_location(location, |location| {
4276 match location.type_() {
4277 constants::BOOL_VEC4 | constants::FLOAT_VEC4 => {},
4278 _ => return Err(InvalidOperation),
4279 }
4280 self.send_command(WebGLCommand::Uniform4f(location.id(), x, y, z, w));
4281 Ok(())
4282 });
4283 }
4284
4285 fn Uniform4fv(
4287 &self,
4288 location: Option<&WebGLUniformLocation>,
4289 val: Float32ArrayOrUnrestrictedFloatSequence,
4290 ) {
4291 self.uniform4fv(location, val, 0, 0)
4292 }
4293
4294 fn UniformMatrix2fv(
4296 &self,
4297 location: Option<&WebGLUniformLocation>,
4298 transpose: bool,
4299 val: Float32ArrayOrUnrestrictedFloatSequence,
4300 ) {
4301 self.uniform_matrix_2fv(location, transpose, val, 0, 0)
4302 }
4303
4304 fn UniformMatrix3fv(
4306 &self,
4307 location: Option<&WebGLUniformLocation>,
4308 transpose: bool,
4309 val: Float32ArrayOrUnrestrictedFloatSequence,
4310 ) {
4311 self.uniform_matrix_3fv(location, transpose, val, 0, 0)
4312 }
4313
4314 fn UniformMatrix4fv(
4316 &self,
4317 location: Option<&WebGLUniformLocation>,
4318 transpose: bool,
4319 val: Float32ArrayOrUnrestrictedFloatSequence,
4320 ) {
4321 self.uniform_matrix_4fv(location, transpose, val, 0, 0)
4322 }
4323
4324 #[expect(unsafe_code)]
4326 fn GetUniform(
4327 &self,
4328 cx: SafeJSContext,
4329 program: &WebGLProgram,
4330 location: &WebGLUniformLocation,
4331 mut rval: MutableHandleValue,
4332 ) {
4333 handle_potential_webgl_error!(
4334 self,
4335 self.uniform_check_program(program, location),
4336 return rval.set(NullValue())
4337 );
4338
4339 let triple = (self, program.id(), location.id());
4340
4341 match location.type_() {
4342 constants::BOOL => rval.set(BooleanValue(uniform_get(
4343 triple,
4344 WebGLCommand::GetUniformBool,
4345 ))),
4346 constants::BOOL_VEC2 => uniform_get(triple, WebGLCommand::GetUniformBool2)
4347 .safe_to_jsval(cx, rval, CanGc::deprecated_note()),
4348 constants::BOOL_VEC3 => uniform_get(triple, WebGLCommand::GetUniformBool3)
4349 .safe_to_jsval(cx, rval, CanGc::deprecated_note()),
4350 constants::BOOL_VEC4 => uniform_get(triple, WebGLCommand::GetUniformBool4)
4351 .safe_to_jsval(cx, rval, CanGc::deprecated_note()),
4352 constants::INT |
4353 constants::SAMPLER_2D |
4354 constants::SAMPLER_CUBE |
4355 WebGL2RenderingContextConstants::SAMPLER_2D_ARRAY |
4356 WebGL2RenderingContextConstants::SAMPLER_3D => {
4357 rval.set(Int32Value(uniform_get(triple, WebGLCommand::GetUniformInt)))
4358 },
4359 constants::INT_VEC2 => unsafe {
4360 uniform_typed::<Int32>(
4361 *cx,
4362 &uniform_get(triple, WebGLCommand::GetUniformInt2),
4363 rval,
4364 )
4365 },
4366 constants::INT_VEC3 => unsafe {
4367 uniform_typed::<Int32>(
4368 *cx,
4369 &uniform_get(triple, WebGLCommand::GetUniformInt3),
4370 rval,
4371 )
4372 },
4373 constants::INT_VEC4 => unsafe {
4374 uniform_typed::<Int32>(
4375 *cx,
4376 &uniform_get(triple, WebGLCommand::GetUniformInt4),
4377 rval,
4378 )
4379 },
4380 constants::FLOAT => rval
4381 .set(DoubleValue(
4382 uniform_get(triple, WebGLCommand::GetUniformFloat) as f64,
4383 )),
4384 constants::FLOAT_VEC2 => unsafe {
4385 uniform_typed::<Float32>(
4386 *cx,
4387 &uniform_get(triple, WebGLCommand::GetUniformFloat2),
4388 rval,
4389 )
4390 },
4391 constants::FLOAT_VEC3 => unsafe {
4392 uniform_typed::<Float32>(
4393 *cx,
4394 &uniform_get(triple, WebGLCommand::GetUniformFloat3),
4395 rval,
4396 )
4397 },
4398 constants::FLOAT_VEC4 | constants::FLOAT_MAT2 => unsafe {
4399 uniform_typed::<Float32>(
4400 *cx,
4401 &uniform_get(triple, WebGLCommand::GetUniformFloat4),
4402 rval,
4403 )
4404 },
4405 constants::FLOAT_MAT3 => unsafe {
4406 uniform_typed::<Float32>(
4407 *cx,
4408 &uniform_get(triple, WebGLCommand::GetUniformFloat9),
4409 rval,
4410 )
4411 },
4412 constants::FLOAT_MAT4 => unsafe {
4413 uniform_typed::<Float32>(
4414 *cx,
4415 &uniform_get(triple, WebGLCommand::GetUniformFloat16),
4416 rval,
4417 )
4418 },
4419 _ => panic!("wrong uniform type"),
4420 }
4421 }
4422
4423 fn UseProgram(&self, program: Option<&WebGLProgram>) {
4425 if let Some(program) = program {
4426 handle_potential_webgl_error!(self, self.validate_ownership(program), return);
4427 if program.is_deleted() || !program.is_linked() {
4428 return self.webgl_error(InvalidOperation);
4429 }
4430 if program.is_in_use() {
4431 return;
4432 }
4433 program.in_use(true);
4434 }
4435 match self.current_program.get() {
4436 Some(ref current) if program != Some(&**current) => current.in_use(false),
4437 _ => {},
4438 }
4439 self.send_command(WebGLCommand::UseProgram(program.map(|p| p.id())));
4440 self.current_program.set(program);
4441 }
4442
4443 fn ValidateProgram(&self, program: &WebGLProgram) {
4445 handle_potential_webgl_error!(self, self.validate_ownership(program), return);
4446 if let Err(e) = program.validate() {
4447 self.webgl_error(e);
4448 }
4449 }
4450
4451 fn VertexAttrib1f(&self, indx: u32, x: f32) {
4453 self.vertex_attrib(indx, x, 0f32, 0f32, 1f32)
4454 }
4455
4456 fn VertexAttrib1fv(&self, indx: u32, v: Float32ArrayOrUnrestrictedFloatSequence) {
4458 let values = match v {
4459 Float32ArrayOrUnrestrictedFloatSequence::Float32Array(v) => v.to_vec(),
4460 Float32ArrayOrUnrestrictedFloatSequence::UnrestrictedFloatSequence(v) => v,
4461 };
4462 if values.is_empty() {
4463 return self.webgl_error(InvalidValue);
4465 }
4466 self.vertex_attrib(indx, values[0], 0f32, 0f32, 1f32);
4467 }
4468
4469 fn VertexAttrib2f(&self, indx: u32, x: f32, y: f32) {
4471 self.vertex_attrib(indx, x, y, 0f32, 1f32)
4472 }
4473
4474 fn VertexAttrib2fv(&self, indx: u32, v: Float32ArrayOrUnrestrictedFloatSequence) {
4476 let values = match v {
4477 Float32ArrayOrUnrestrictedFloatSequence::Float32Array(v) => v.to_vec(),
4478 Float32ArrayOrUnrestrictedFloatSequence::UnrestrictedFloatSequence(v) => v,
4479 };
4480 if values.len() < 2 {
4481 return self.webgl_error(InvalidValue);
4483 }
4484 self.vertex_attrib(indx, values[0], values[1], 0f32, 1f32);
4485 }
4486
4487 fn VertexAttrib3f(&self, indx: u32, x: f32, y: f32, z: f32) {
4489 self.vertex_attrib(indx, x, y, z, 1f32)
4490 }
4491
4492 fn VertexAttrib3fv(&self, indx: u32, v: Float32ArrayOrUnrestrictedFloatSequence) {
4494 let values = match v {
4495 Float32ArrayOrUnrestrictedFloatSequence::Float32Array(v) => v.to_vec(),
4496 Float32ArrayOrUnrestrictedFloatSequence::UnrestrictedFloatSequence(v) => v,
4497 };
4498 if values.len() < 3 {
4499 return self.webgl_error(InvalidValue);
4501 }
4502 self.vertex_attrib(indx, values[0], values[1], values[2], 1f32);
4503 }
4504
4505 fn VertexAttrib4f(&self, indx: u32, x: f32, y: f32, z: f32, w: f32) {
4507 self.vertex_attrib(indx, x, y, z, w)
4508 }
4509
4510 fn VertexAttrib4fv(&self, indx: u32, v: Float32ArrayOrUnrestrictedFloatSequence) {
4512 let values = match v {
4513 Float32ArrayOrUnrestrictedFloatSequence::Float32Array(v) => v.to_vec(),
4514 Float32ArrayOrUnrestrictedFloatSequence::UnrestrictedFloatSequence(v) => v,
4515 };
4516 if values.len() < 4 {
4517 return self.webgl_error(InvalidValue);
4519 }
4520 self.vertex_attrib(indx, values[0], values[1], values[2], values[3]);
4521 }
4522
4523 fn VertexAttribPointer(
4525 &self,
4526 index: u32,
4527 size: i32,
4528 type_: u32,
4529 normalized: bool,
4530 stride: i32,
4531 offset: i64,
4532 ) {
4533 let res = match self.webgl_version() {
4534 WebGLVersion::WebGL1 => self
4535 .current_vao()
4536 .vertex_attrib_pointer(index, size, type_, normalized, stride, offset),
4537 WebGLVersion::WebGL2 => self
4538 .current_vao_webgl2()
4539 .vertex_attrib_pointer(index, size, type_, normalized, stride, offset),
4540 };
4541 handle_potential_webgl_error!(self, res);
4542 }
4543
4544 fn Viewport(&self, x: i32, y: i32, width: i32, height: i32) {
4546 if width < 0 || height < 0 {
4547 return self.webgl_error(InvalidValue);
4548 }
4549
4550 self.send_command(WebGLCommand::SetViewport(x, y, width, height))
4551 }
4552
4553 #[expect(unsafe_code)]
4555 fn TexImage2D(
4556 &self,
4557 target: u32,
4558 level: i32,
4559 internal_format: i32,
4560 width: i32,
4561 height: i32,
4562 border: i32,
4563 format: u32,
4564 data_type: u32,
4565 pixels: CustomAutoRooterGuard<Option<ArrayBufferView>>,
4566 ) -> ErrorResult {
4567 if !self.extension_manager.is_tex_type_enabled(data_type) {
4568 self.webgl_error(InvalidEnum);
4569 return Ok(());
4570 }
4571
4572 let validator = TexImage2DValidator::new(
4573 self,
4574 target,
4575 level,
4576 internal_format as u32,
4577 width,
4578 height,
4579 border,
4580 format,
4581 data_type,
4582 );
4583
4584 let TexImage2DValidatorResult {
4585 texture,
4586 target,
4587 width,
4588 height,
4589 level,
4590 border,
4591 internal_format,
4592 format,
4593 data_type,
4594 } = match validator.validate() {
4595 Ok(result) => result,
4596 Err(_) => return Ok(()), };
4598
4599 if !internal_format.compatible_data_types().contains(&data_type) {
4600 return {
4601 self.webgl_error(InvalidOperation);
4602 Ok(())
4603 };
4604 }
4605 if texture.is_immutable() {
4606 return {
4607 self.webgl_error(InvalidOperation);
4608 Ok(())
4609 };
4610 }
4611
4612 let unpacking_alignment = self.texture_unpacking_alignment.get();
4613
4614 let expected_byte_length = match self.validate_tex_image_2d_data(
4615 width,
4616 height,
4617 format,
4618 data_type,
4619 unpacking_alignment,
4620 pixels.as_ref(),
4621 ) {
4622 Ok(byte_length) => byte_length,
4623 Err(()) => return Ok(()),
4624 };
4625
4626 let buff = match *pixels {
4629 None => GenericSharedMemory::from_bytes(&vec![0u8; expected_byte_length as usize]),
4630 Some(ref data) => GenericSharedMemory::from_bytes(unsafe { data.as_slice() }),
4631 };
4632
4633 if buff.len() < expected_byte_length as usize {
4640 return {
4641 self.webgl_error(InvalidOperation);
4642 Ok(())
4643 };
4644 }
4645
4646 let size = Size2D::new(width, height);
4647
4648 if !self.validate_filterable_texture(
4649 &texture,
4650 target,
4651 level,
4652 internal_format,
4653 size,
4654 data_type,
4655 ) {
4656 return Ok(());
4659 }
4660
4661 let size = Size2D::new(width, height);
4662
4663 let (alpha_treatment, y_axis_treatment) =
4664 self.get_current_unpack_state(Alpha::NotPremultiplied);
4665
4666 self.tex_image_2d(
4667 &texture,
4668 target,
4669 data_type,
4670 internal_format,
4671 format,
4672 level,
4673 border,
4674 unpacking_alignment,
4675 size,
4676 TexSource::Pixels(TexPixels::from_array(
4677 buff,
4678 size,
4679 alpha_treatment,
4680 y_axis_treatment,
4681 )),
4682 );
4683
4684 Ok(())
4685 }
4686
4687 fn TexImage2D_(
4689 &self,
4690 target: u32,
4691 level: i32,
4692 internal_format: i32,
4693 format: u32,
4694 data_type: u32,
4695 source: TexImageSource,
4696 ) -> ErrorResult {
4697 if !self.extension_manager.is_tex_type_enabled(data_type) {
4698 self.webgl_error(InvalidEnum);
4699 return Ok(());
4700 }
4701
4702 let pixels = match self.get_image_pixels(source)? {
4703 Some(pixels) => pixels,
4704 None => return Ok(()),
4705 };
4706
4707 let validator = TexImage2DValidator::new(
4708 self,
4709 target,
4710 level,
4711 internal_format as u32,
4712 pixels.size().width as i32,
4713 pixels.size().height as i32,
4714 0,
4715 format,
4716 data_type,
4717 );
4718
4719 let TexImage2DValidatorResult {
4720 texture,
4721 target,
4722 level,
4723 border,
4724 internal_format,
4725 format,
4726 data_type,
4727 ..
4728 } = match validator.validate() {
4729 Ok(result) => result,
4730 Err(_) => return Ok(()), };
4732
4733 if !internal_format.compatible_data_types().contains(&data_type) {
4734 return {
4735 self.webgl_error(InvalidOperation);
4736 Ok(())
4737 };
4738 }
4739 if texture.is_immutable() {
4740 return {
4741 self.webgl_error(InvalidOperation);
4742 Ok(())
4743 };
4744 }
4745
4746 if !self.validate_filterable_texture(
4747 &texture,
4748 target,
4749 level,
4750 internal_format,
4751 pixels.size(),
4752 data_type,
4753 ) {
4754 return Ok(());
4757 }
4758
4759 self.tex_image_2d(
4760 &texture,
4761 target,
4762 data_type,
4763 internal_format,
4764 format,
4765 level,
4766 border,
4767 1,
4768 pixels.size(),
4769 TexSource::Pixels(pixels),
4770 );
4771 Ok(())
4772 }
4773
4774 #[expect(unsafe_code)]
4776 fn TexSubImage2D(
4777 &self,
4778 target: u32,
4779 level: i32,
4780 xoffset: i32,
4781 yoffset: i32,
4782 width: i32,
4783 height: i32,
4784 format: u32,
4785 data_type: u32,
4786 pixels: CustomAutoRooterGuard<Option<ArrayBufferView>>,
4787 ) -> ErrorResult {
4788 let validator = TexImage2DValidator::new(
4789 self, target, level, format, width, height, 0, format, data_type,
4790 );
4791 let TexImage2DValidatorResult {
4792 texture,
4793 target,
4794 width,
4795 height,
4796 level,
4797 format,
4798 data_type,
4799 ..
4800 } = match validator.validate() {
4801 Ok(result) => result,
4802 Err(_) => return Ok(()), };
4804
4805 let unpacking_alignment = self.texture_unpacking_alignment.get();
4806
4807 let expected_byte_length = match self.validate_tex_image_2d_data(
4808 width,
4809 height,
4810 format,
4811 data_type,
4812 unpacking_alignment,
4813 pixels.as_ref(),
4814 ) {
4815 Ok(byte_length) => byte_length,
4816 Err(()) => return Ok(()),
4817 };
4818
4819 let buff = handle_potential_webgl_error!(
4820 self,
4821 pixels
4822 .as_ref()
4823 .map(|p| GenericSharedMemory::from_bytes(unsafe { p.as_slice() }))
4824 .ok_or(InvalidValue),
4825 return Ok(())
4826 );
4827
4828 if buff.len() < expected_byte_length as usize {
4835 return {
4836 self.webgl_error(InvalidOperation);
4837 Ok(())
4838 };
4839 }
4840
4841 let (alpha_treatment, y_axis_treatment) =
4842 self.get_current_unpack_state(Alpha::NotPremultiplied);
4843
4844 self.tex_sub_image_2d(
4845 texture,
4846 target,
4847 level,
4848 xoffset,
4849 yoffset,
4850 format,
4851 data_type,
4852 unpacking_alignment,
4853 TexPixels::from_array(
4854 buff,
4855 Size2D::new(width, height),
4856 alpha_treatment,
4857 y_axis_treatment,
4858 ),
4859 );
4860 Ok(())
4861 }
4862
4863 fn TexSubImage2D_(
4865 &self,
4866 target: u32,
4867 level: i32,
4868 xoffset: i32,
4869 yoffset: i32,
4870 format: u32,
4871 data_type: u32,
4872 source: TexImageSource,
4873 ) -> ErrorResult {
4874 let pixels = match self.get_image_pixels(source)? {
4875 Some(pixels) => pixels,
4876 None => return Ok(()),
4877 };
4878
4879 let validator = TexImage2DValidator::new(
4880 self,
4881 target,
4882 level,
4883 format,
4884 pixels.size().width as i32,
4885 pixels.size().height as i32,
4886 0,
4887 format,
4888 data_type,
4889 );
4890 let TexImage2DValidatorResult {
4891 texture,
4892 target,
4893 level,
4894 format,
4895 data_type,
4896 ..
4897 } = match validator.validate() {
4898 Ok(result) => result,
4899 Err(_) => return Ok(()), };
4901
4902 self.tex_sub_image_2d(
4903 texture, target, level, xoffset, yoffset, format, data_type, 1, pixels,
4904 );
4905 Ok(())
4906 }
4907
4908 fn TexParameterf(&self, target: u32, name: u32, value: f32) {
4910 self.tex_parameter(target, name, TexParameterValue::Float(value))
4911 }
4912
4913 fn TexParameteri(&self, target: u32, name: u32, value: i32) {
4915 self.tex_parameter(target, name, TexParameterValue::Int(value))
4916 }
4917
4918 fn CheckFramebufferStatus(&self, target: u32) -> u32 {
4920 if target != constants::FRAMEBUFFER {
4926 self.webgl_error(InvalidEnum);
4927 return 0;
4928 }
4929
4930 match self.bound_draw_framebuffer.get() {
4931 Some(fb) => fb.check_status(),
4932 None => constants::FRAMEBUFFER_COMPLETE,
4933 }
4934 }
4935
4936 fn RenderbufferStorage(&self, target: u32, internal_format: u32, width: i32, height: i32) {
4938 self.renderbuffer_storage(target, 0, internal_format, width, height)
4939 }
4940
4941 fn FramebufferRenderbuffer(
4943 &self,
4944 target: u32,
4945 attachment: u32,
4946 renderbuffertarget: u32,
4947 rb: Option<&WebGLRenderbuffer>,
4948 ) {
4949 if let Some(rb) = rb {
4950 handle_potential_webgl_error!(self, self.validate_ownership(rb), return);
4951 }
4952
4953 if target != constants::FRAMEBUFFER || renderbuffertarget != constants::RENDERBUFFER {
4954 return self.webgl_error(InvalidEnum);
4955 }
4956
4957 match self.bound_draw_framebuffer.get() {
4958 Some(fb) => handle_potential_webgl_error!(self, fb.renderbuffer(attachment, rb)),
4959 None => self.webgl_error(InvalidOperation),
4960 };
4961 }
4962
4963 fn FramebufferTexture2D(
4965 &self,
4966 target: u32,
4967 attachment: u32,
4968 textarget: u32,
4969 texture: Option<&WebGLTexture>,
4970 level: i32,
4971 ) {
4972 if let Some(texture) = texture {
4973 handle_potential_webgl_error!(self, self.validate_ownership(texture), return);
4974 }
4975
4976 if target != constants::FRAMEBUFFER {
4977 return self.webgl_error(InvalidEnum);
4978 }
4979
4980 if level != 0 {
4986 return self.webgl_error(InvalidValue);
4987 }
4988
4989 match self.bound_draw_framebuffer.get() {
4990 Some(fb) => handle_potential_webgl_error!(
4991 self,
4992 fb.texture2d(attachment, textarget, texture, level)
4993 ),
4994 None => self.webgl_error(InvalidOperation),
4995 };
4996 }
4997
4998 fn GetAttachedShaders(&self, program: &WebGLProgram) -> Option<Vec<DomRoot<WebGLShader>>> {
5000 handle_potential_webgl_error!(self, self.validate_ownership(program), return None);
5001 handle_potential_webgl_error!(self, program.attached_shaders().map(Some), None)
5002 }
5003
5004 #[cfg(feature = "webxr")]
5006 fn MakeXRCompatible(&self, can_gc: CanGc) -> Rc<Promise> {
5007 let p = Promise::new(&self.global(), can_gc);
5009 p.resolve_native(&(), can_gc);
5010 p
5011 }
5012}
5013
5014#[derive(Default, JSTraceable, MallocSizeOf)]
5015struct Capabilities {
5016 value: Cell<CapFlags>,
5017}
5018
5019impl Capabilities {
5020 fn set(&self, cap: u32, set: bool) -> WebGLResult<bool> {
5021 let cap = CapFlags::from_enum(cap)?;
5022 let mut value = self.value.get();
5023 if value.contains(cap) == set {
5024 return Ok(false);
5025 }
5026 value.set(cap, set);
5027 self.value.set(value);
5028 Ok(true)
5029 }
5030
5031 fn is_enabled(&self, cap: u32) -> WebGLResult<bool> {
5032 Ok(self.value.get().contains(CapFlags::from_enum(cap)?))
5033 }
5034}
5035
5036impl Default for CapFlags {
5037 fn default() -> Self {
5038 CapFlags::DITHER
5039 }
5040}
5041
5042macro_rules! capabilities {
5043 ($name:ident, $next:ident, $($rest:ident,)*) => {
5044 capabilities!($name, $next, $($rest,)* [$name = 1;]);
5045 };
5046 ($prev:ident, $name:ident, $($rest:ident,)* [$($tt:tt)*]) => {
5047 capabilities!($name, $($rest,)* [$($tt)* $name = Self::$prev.bits() << 1;]);
5048 };
5049 ($prev:ident, [$($name:ident = $value:expr;)*]) => {
5050 #[derive(Clone, Copy, JSTraceable, MallocSizeOf)]
5051 pub(crate) struct CapFlags(u16);
5052
5053 bitflags! {
5054 impl CapFlags: u16 {
5055 $(const $name = $value;)*
5056 }
5057 }
5058
5059 impl CapFlags {
5060 fn from_enum(cap: u32) -> WebGLResult<Self> {
5061 match cap {
5062 $(constants::$name => Ok(Self::$name),)*
5063 _ => Err(InvalidEnum),
5064 }
5065 }
5066 }
5067 };
5068}
5069
5070capabilities! {
5071 BLEND,
5072 CULL_FACE,
5073 DEPTH_TEST,
5074 DITHER,
5075 POLYGON_OFFSET_FILL,
5076 SAMPLE_ALPHA_TO_COVERAGE,
5077 SAMPLE_COVERAGE,
5078 SCISSOR_TEST,
5079 STENCIL_TEST,
5080}
5081
5082#[cfg_attr(crown, crown::unrooted_must_root_lint::must_root)]
5083#[derive(JSTraceable, MallocSizeOf)]
5084pub(crate) struct Textures {
5085 active_unit: Cell<u32>,
5086 units: Box<[TextureUnit]>,
5087}
5088
5089impl Textures {
5090 fn new(max_combined_textures: u32) -> Self {
5091 Self {
5092 active_unit: Default::default(),
5093 units: (0..max_combined_textures)
5094 .map(|_| Default::default())
5095 .collect::<Vec<_>>()
5096 .into(),
5097 }
5098 }
5099
5100 pub(crate) fn active_unit_enum(&self) -> u32 {
5101 self.active_unit.get() + constants::TEXTURE0
5102 }
5103
5104 fn set_active_unit_enum(&self, index: u32) -> WebGLResult<()> {
5105 if (constants::TEXTURE0..constants::TEXTURE0 + self.units.len() as u32).contains(&index) {
5106 self.active_unit.set(index - constants::TEXTURE0);
5107 Ok(())
5108 } else {
5109 Err(InvalidEnum)
5110 }
5111 }
5112
5113 pub(crate) fn active_texture_slot(
5114 &self,
5115 target: u32,
5116 webgl_version: WebGLVersion,
5117 ) -> WebGLResult<&MutNullableDom<WebGLTexture>> {
5118 let active_unit = self.active_unit();
5119 let is_webgl2 = webgl_version == WebGLVersion::WebGL2;
5120 match target {
5121 constants::TEXTURE_2D => Ok(&active_unit.tex_2d),
5122 constants::TEXTURE_CUBE_MAP => Ok(&active_unit.tex_cube_map),
5123 WebGL2RenderingContextConstants::TEXTURE_2D_ARRAY if is_webgl2 => {
5124 Ok(&active_unit.tex_2d_array)
5125 },
5126 WebGL2RenderingContextConstants::TEXTURE_3D if is_webgl2 => Ok(&active_unit.tex_3d),
5127 _ => Err(InvalidEnum),
5128 }
5129 }
5130
5131 pub(crate) fn active_texture_for_image_target(
5132 &self,
5133 target: TexImageTarget,
5134 ) -> Option<DomRoot<WebGLTexture>> {
5135 let active_unit = self.active_unit();
5136 match target {
5137 TexImageTarget::Texture2D => active_unit.tex_2d.get(),
5138 TexImageTarget::Texture2DArray => active_unit.tex_2d_array.get(),
5139 TexImageTarget::Texture3D => active_unit.tex_3d.get(),
5140 TexImageTarget::CubeMap |
5141 TexImageTarget::CubeMapPositiveX |
5142 TexImageTarget::CubeMapNegativeX |
5143 TexImageTarget::CubeMapPositiveY |
5144 TexImageTarget::CubeMapNegativeY |
5145 TexImageTarget::CubeMapPositiveZ |
5146 TexImageTarget::CubeMapNegativeZ => active_unit.tex_cube_map.get(),
5147 }
5148 }
5149
5150 fn active_unit(&self) -> &TextureUnit {
5151 &self.units[self.active_unit.get() as usize]
5152 }
5153
5154 fn iter(&self) -> impl Iterator<Item = (u32, &TextureUnit)> {
5155 self.units
5156 .iter()
5157 .enumerate()
5158 .map(|(index, unit)| (index as u32 + constants::TEXTURE0, unit))
5159 }
5160}
5161
5162#[cfg_attr(crown, crown::unrooted_must_root_lint::must_root)]
5163#[derive(Default, JSTraceable, MallocSizeOf)]
5164struct TextureUnit {
5165 tex_2d: MutNullableDom<WebGLTexture>,
5166 tex_cube_map: MutNullableDom<WebGLTexture>,
5167 tex_2d_array: MutNullableDom<WebGLTexture>,
5168 tex_3d: MutNullableDom<WebGLTexture>,
5169}
5170
5171impl TextureUnit {
5172 fn unbind(&self, texture: &WebGLTexture) -> Option<u32> {
5173 let fields = [
5174 (&self.tex_2d, constants::TEXTURE_2D),
5175 (&self.tex_cube_map, constants::TEXTURE_CUBE_MAP),
5176 (
5177 &self.tex_2d_array,
5178 WebGL2RenderingContextConstants::TEXTURE_2D_ARRAY,
5179 ),
5180 (&self.tex_3d, WebGL2RenderingContextConstants::TEXTURE_3D),
5181 ];
5182 for &(slot, target) in &fields {
5183 if slot.get().is_some_and(|t| texture == &*t) {
5184 slot.set(None);
5185 return Some(target);
5186 }
5187 }
5188 None
5189 }
5190}
5191
5192pub(crate) struct TexPixels {
5193 data: GenericSharedMemory,
5194 size: Size2D<u32>,
5195 pixel_format: Option<PixelFormat>,
5196 alpha_treatment: Option<AlphaTreatment>,
5197 y_axis_treatment: YAxisTreatment,
5198}
5199
5200impl TexPixels {
5201 fn new(
5202 data: GenericSharedMemory,
5203 size: Size2D<u32>,
5204 pixel_format: PixelFormat,
5205 alpha_treatment: Option<AlphaTreatment>,
5206 y_axis_treatment: YAxisTreatment,
5207 ) -> Self {
5208 Self {
5209 data,
5210 size,
5211 pixel_format: Some(pixel_format),
5212 alpha_treatment,
5213 y_axis_treatment,
5214 }
5215 }
5216
5217 pub(crate) fn from_array(
5218 data: GenericSharedMemory,
5219 size: Size2D<u32>,
5220 alpha_treatment: Option<AlphaTreatment>,
5221 y_axis_treatment: YAxisTreatment,
5222 ) -> Self {
5223 Self {
5224 data,
5225 size,
5226 pixel_format: None,
5227 alpha_treatment,
5228 y_axis_treatment,
5229 }
5230 }
5231
5232 pub(crate) fn size(&self) -> Size2D<u32> {
5233 self.size
5234 }
5235
5236 pub(crate) fn pixel_format(&self) -> Option<PixelFormat> {
5237 self.pixel_format
5238 }
5239
5240 pub(crate) fn alpha_treatment(&self) -> Option<AlphaTreatment> {
5241 self.alpha_treatment
5242 }
5243
5244 pub(crate) fn y_axis_treatment(&self) -> YAxisTreatment {
5245 self.y_axis_treatment
5246 }
5247
5248 pub(crate) fn into_shared_memory(self) -> GenericSharedMemory {
5249 self.data
5250 }
5251}
5252
5253pub(crate) enum TexSource {
5254 Pixels(TexPixels),
5255 BufferOffset(i64),
5256}
5257
5258fn array_buffer_type_to_sized_type(type_: Type) -> Option<SizedDataType> {
5259 match type_ {
5260 Type::Uint8 | Type::Uint8Clamped => Some(SizedDataType::Uint8),
5261 Type::Uint16 => Some(SizedDataType::Uint16),
5262 Type::Uint32 => Some(SizedDataType::Uint32),
5263 Type::Int8 => Some(SizedDataType::Int8),
5264 Type::Int16 => Some(SizedDataType::Int16),
5265 Type::Int32 => Some(SizedDataType::Int32),
5266 Type::Float32 => Some(SizedDataType::Float32),
5267 Type::Float16 |
5268 Type::Float64 |
5269 Type::BigInt64 |
5270 Type::BigUint64 |
5271 Type::MaxTypedArrayViewType |
5272 Type::Int64 |
5273 Type::Simd128 => None,
5274 }
5275}