Skip to main content

wgpu_hal/gles/
queue.rs

1use alloc::sync::Arc;
2use alloc::vec;
3use core::sync::atomic::Ordering;
4
5use arrayvec::ArrayVec;
6use glow::HasContext;
7
8use super::{conv::is_layered_target, Command as C, PrivateCapabilities};
9
10const DEBUG_ID: u32 = 0;
11
12fn extract_marker<'a>(data: &'a [u8], range: &core::ops::Range<u32>) -> &'a str {
13    core::str::from_utf8(&data[range.start as usize..range.end as usize]).unwrap()
14}
15
16fn to_debug_str(s: &str) -> &str {
17    // The spec mentions that if the length given to debug functions is negative,
18    // the implementation will access the ptr and look for a null that terminates
19    // the string but some implementations will try to access the ptr even if the
20    // length is 0.
21    if s.is_empty() {
22        "<empty>"
23    } else {
24        s
25    }
26}
27
28fn get_2d_target(target: u32, array_layer: u32) -> u32 {
29    const CUBEMAP_FACES: [u32; 6] = [
30        glow::TEXTURE_CUBE_MAP_POSITIVE_X,
31        glow::TEXTURE_CUBE_MAP_NEGATIVE_X,
32        glow::TEXTURE_CUBE_MAP_POSITIVE_Y,
33        glow::TEXTURE_CUBE_MAP_NEGATIVE_Y,
34        glow::TEXTURE_CUBE_MAP_POSITIVE_Z,
35        glow::TEXTURE_CUBE_MAP_NEGATIVE_Z,
36    ];
37
38    match target {
39        glow::TEXTURE_2D => target,
40        glow::TEXTURE_CUBE_MAP => CUBEMAP_FACES[array_layer as usize],
41        _ => unreachable!(),
42    }
43}
44
45fn get_z_offset(target: u32, base: &crate::TextureCopyBase) -> u32 {
46    match target {
47        glow::TEXTURE_2D_ARRAY | glow::TEXTURE_CUBE_MAP_ARRAY => base.array_layer,
48        glow::TEXTURE_3D => base.origin.z,
49        _ => unreachable!(),
50    }
51}
52
53impl super::Queue {
54    /// Performs a manual shader clear, used as a workaround for a clearing bug on mesa
55    unsafe fn perform_shader_clear(&self, gl: &glow::Context, draw_buffer: u32, color: [f32; 4]) {
56        let shader_clear = self
57            .shader_clear_program
58            .as_ref()
59            .expect("shader_clear_program should always be set if the workaround is enabled");
60        unsafe { gl.use_program(Some(shader_clear.program)) };
61        unsafe {
62            gl.uniform_4_f32(
63                Some(&shader_clear.color_uniform_location),
64                color[0],
65                color[1],
66                color[2],
67                color[3],
68            )
69        };
70        unsafe { gl.disable(glow::DEPTH_TEST) };
71        unsafe { gl.disable(glow::STENCIL_TEST) };
72        unsafe { gl.disable(glow::SCISSOR_TEST) };
73        unsafe { gl.disable(glow::BLEND) };
74        unsafe { gl.disable(glow::CULL_FACE) };
75        unsafe { gl.draw_buffers(&[glow::COLOR_ATTACHMENT0 + draw_buffer]) };
76        unsafe { gl.draw_arrays(glow::TRIANGLES, 0, 3) };
77
78        let draw_buffer_count = self.draw_buffer_count.load(Ordering::Relaxed);
79        if draw_buffer_count != 0 {
80            // Reset the draw buffers to what they were before the clear
81            let indices = (0..draw_buffer_count as u32)
82                .map(|i| glow::COLOR_ATTACHMENT0 + i)
83                .collect::<ArrayVec<_, { crate::MAX_COLOR_ATTACHMENTS }>>();
84            unsafe { gl.draw_buffers(&indices) };
85        }
86    }
87
88    unsafe fn reset_state(&self, gl: &glow::Context) {
89        unsafe { gl.use_program(None) };
90        unsafe { gl.bind_framebuffer(glow::FRAMEBUFFER, None) };
91        unsafe { gl.disable(glow::DEPTH_TEST) };
92        unsafe { gl.disable(glow::STENCIL_TEST) };
93        unsafe { gl.disable(glow::SCISSOR_TEST) };
94        unsafe { gl.disable(glow::BLEND) };
95        unsafe { gl.disable(glow::CULL_FACE) };
96        unsafe { gl.disable(glow::POLYGON_OFFSET_FILL) };
97        unsafe { gl.disable(glow::SAMPLE_ALPHA_TO_COVERAGE) };
98        if self.features.contains(wgt::Features::DEPTH_CLIP_CONTROL) {
99            unsafe { gl.disable(glow::DEPTH_CLAMP) };
100        }
101
102        unsafe { gl.bind_buffer(glow::ELEMENT_ARRAY_BUFFER, None) };
103        let mut current_index_buffer = self.current_index_buffer.lock();
104        *current_index_buffer = None;
105    }
106
107    unsafe fn set_attachment(
108        &self,
109        gl: &glow::Context,
110        fbo_target: u32,
111        attachment: u32,
112        view: &super::TextureView,
113        depth_slice: Option<u32>,
114        sample_count: u32,
115    ) {
116        match view.inner {
117            super::TextureInner::Renderbuffer { raw } => {
118                unsafe {
119                    gl.framebuffer_renderbuffer(
120                        fbo_target,
121                        attachment,
122                        glow::RENDERBUFFER,
123                        Some(raw),
124                    )
125                };
126            }
127            super::TextureInner::DefaultRenderbuffer => panic!("Unexpected default RBO"),
128            super::TextureInner::Texture { raw, target } => {
129                let num_layers = view.array_layers.end - view.array_layers.start;
130                if num_layers > 1 {
131                    #[cfg(webgl)]
132                    unsafe {
133                        gl.framebuffer_texture_multiview_ovr(
134                            fbo_target,
135                            attachment,
136                            Some(raw),
137                            view.mip_levels.start as i32,
138                            view.array_layers.start as i32,
139                            num_layers as i32,
140                        )
141                    };
142                } else if is_layered_target(target) {
143                    let layer = if target == glow::TEXTURE_3D {
144                        depth_slice.unwrap() as i32
145                    } else {
146                        view.array_layers.start as i32
147                    };
148                    unsafe {
149                        gl.framebuffer_texture_layer(
150                            fbo_target,
151                            attachment,
152                            Some(raw),
153                            view.mip_levels.start as i32,
154                            layer,
155                        )
156                    };
157                } else {
158                    unsafe {
159                        assert_eq!(view.mip_levels.len(), 1);
160                        if sample_count != 1 {
161                            gl.framebuffer_texture_2d_multisample(
162                                fbo_target,
163                                attachment,
164                                get_2d_target(target, view.array_layers.start),
165                                Some(raw),
166                                view.mip_levels.start as i32,
167                                sample_count as i32,
168                            )
169                        } else {
170                            gl.framebuffer_texture_2d(
171                                fbo_target,
172                                attachment,
173                                get_2d_target(target, view.array_layers.start),
174                                Some(raw),
175                                view.mip_levels.start as i32,
176                            )
177                        }
178                    };
179                }
180            }
181            #[cfg(webgl)]
182            super::TextureInner::ExternalFramebuffer { ref inner } => unsafe {
183                gl.bind_external_framebuffer(glow::FRAMEBUFFER, inner);
184            },
185            #[cfg(native)]
186            super::TextureInner::ExternalNativeFramebuffer { ref inner } => unsafe {
187                gl.bind_framebuffer(glow::FRAMEBUFFER, Some(*inner));
188            },
189        }
190    }
191
192    unsafe fn process(
193        &self,
194        gl: &glow::Context,
195        command: &C,
196        #[cfg_attr(target_family = "wasm", allow(unused))] data_bytes: &[u8],
197        queries: &[glow::Query],
198    ) {
199        match *command {
200            C::Draw {
201                topology,
202                first_vertex,
203                vertex_count,
204                instance_count,
205                first_instance,
206                ref first_instance_location,
207            } => {
208                let supports_full_instancing = self
209                    .shared
210                    .private_caps
211                    .contains(PrivateCapabilities::FULLY_FEATURED_INSTANCING);
212
213                if supports_full_instancing {
214                    unsafe {
215                        gl.draw_arrays_instanced_base_instance(
216                            topology,
217                            first_vertex as i32,
218                            vertex_count as i32,
219                            instance_count as i32,
220                            first_instance,
221                        )
222                    }
223                } else {
224                    unsafe {
225                        gl.uniform_1_u32(first_instance_location.as_ref(), first_instance);
226                    }
227
228                    // Don't use `gl.draw_arrays` for `instance_count == 1`.
229                    // Angle has a bug where it doesn't consider the instance divisor when `DYNAMIC_DRAW` is used in `draw_arrays`.
230                    // See https://github.com/gfx-rs/wgpu/issues/3578
231                    unsafe {
232                        gl.draw_arrays_instanced(
233                            topology,
234                            first_vertex as i32,
235                            vertex_count as i32,
236                            instance_count as i32,
237                        )
238                    }
239                };
240            }
241            C::DrawIndexed {
242                topology,
243                index_type,
244                index_count,
245                index_offset,
246                base_vertex,
247                first_instance,
248                instance_count,
249                ref first_instance_location,
250            } => {
251                let supports_full_instancing = self
252                    .shared
253                    .private_caps
254                    .contains(PrivateCapabilities::FULLY_FEATURED_INSTANCING);
255
256                if supports_full_instancing {
257                    unsafe {
258                        gl.draw_elements_instanced_base_vertex_base_instance(
259                            topology,
260                            index_count as i32,
261                            index_type,
262                            index_offset as i32,
263                            instance_count as i32,
264                            base_vertex,
265                            first_instance,
266                        )
267                    }
268                } else {
269                    unsafe { gl.uniform_1_u32(first_instance_location.as_ref(), first_instance) };
270
271                    if base_vertex == 0 {
272                        unsafe {
273                            // Don't use `gl.draw_elements`/`gl.draw_elements_base_vertex` for `instance_count == 1`.
274                            // Angle has a bug where it doesn't consider the instance divisor when `DYNAMIC_DRAW` is used in `gl.draw_elements`/`gl.draw_elements_base_vertex`.
275                            // See https://github.com/gfx-rs/wgpu/issues/3578
276                            gl.draw_elements_instanced(
277                                topology,
278                                index_count as i32,
279                                index_type,
280                                index_offset as i32,
281                                instance_count as i32,
282                            )
283                        }
284                    } else {
285                        // If we've gotten here, wgpu-core has already validated that this function exists via the DownlevelFlags::BASE_VERTEX feature.
286                        unsafe {
287                            gl.draw_elements_instanced_base_vertex(
288                                topology,
289                                index_count as _,
290                                index_type,
291                                index_offset as i32,
292                                instance_count as i32,
293                                base_vertex,
294                            )
295                        }
296                    }
297                }
298            }
299            C::DrawIndirect {
300                topology,
301                indirect_buf,
302                indirect_offset,
303                ref first_instance_location,
304            } => {
305                unsafe { gl.uniform_1_u32(first_instance_location.as_ref(), 0) };
306
307                unsafe { gl.bind_buffer(glow::DRAW_INDIRECT_BUFFER, Some(indirect_buf)) };
308                unsafe { gl.draw_arrays_indirect_offset(topology, indirect_offset as i32) };
309            }
310            C::DrawIndexedIndirect {
311                topology,
312                index_type,
313                indirect_buf,
314                indirect_offset,
315                ref first_instance_location,
316            } => {
317                unsafe { gl.uniform_1_u32(first_instance_location.as_ref(), 0) };
318
319                unsafe { gl.bind_buffer(glow::DRAW_INDIRECT_BUFFER, Some(indirect_buf)) };
320                unsafe {
321                    gl.draw_elements_indirect_offset(topology, index_type, indirect_offset as i32)
322                };
323            }
324            C::Dispatch(group_counts) => {
325                unsafe { gl.dispatch_compute(group_counts[0], group_counts[1], group_counts[2]) };
326            }
327            C::DispatchIndirect {
328                indirect_buf,
329                indirect_offset,
330            } => {
331                unsafe { gl.bind_buffer(glow::DISPATCH_INDIRECT_BUFFER, Some(indirect_buf)) };
332                unsafe { gl.dispatch_compute_indirect(indirect_offset as i32) };
333            }
334            C::ClearBuffer {
335                ref dst,
336                dst_target,
337                ref range,
338            } => match dst.raw {
339                Some(buffer) => {
340                    // When `INDEX_BUFFER_ROLE_CHANGE` isn't available, we can't copy into the
341                    // index buffer from the zero buffer. This would fail in Chrome with the
342                    // following message:
343                    //
344                    // > Cannot copy into an element buffer destination from a non-element buffer
345                    // > source
346                    //
347                    // Instead, we'll upload zeroes into the buffer.
348                    let can_use_zero_buffer = self
349                        .shared
350                        .private_caps
351                        .contains(PrivateCapabilities::INDEX_BUFFER_ROLE_CHANGE)
352                        || dst_target != glow::ELEMENT_ARRAY_BUFFER;
353
354                    if can_use_zero_buffer {
355                        unsafe { gl.bind_buffer(glow::COPY_READ_BUFFER, Some(self.zero_buffer)) };
356                        unsafe { gl.bind_buffer(dst_target, Some(buffer)) };
357                        let mut dst_offset = range.start;
358                        while dst_offset < range.end {
359                            let size = (range.end - dst_offset).min(super::ZERO_BUFFER_SIZE as u64);
360                            unsafe {
361                                gl.copy_buffer_sub_data(
362                                    glow::COPY_READ_BUFFER,
363                                    dst_target,
364                                    0,
365                                    dst_offset as i32,
366                                    size as i32,
367                                )
368                            };
369                            dst_offset += size;
370                        }
371                    } else {
372                        unsafe { gl.bind_buffer(dst_target, Some(buffer)) };
373                        let zeroes = vec![0u8; (range.end - range.start) as usize];
374                        unsafe {
375                            gl.buffer_sub_data_u8_slice(dst_target, range.start as i32, &zeroes)
376                        };
377                    }
378                }
379                None => {
380                    let mut map_state = dst.map_state.lock();
381                    map_state.data.as_mut().unwrap().as_mut_slice()
382                        [range.start as usize..range.end as usize]
383                        .fill(0);
384                }
385            },
386            C::CopyBufferToBuffer {
387                ref src,
388                src_target,
389                ref dst,
390                dst_target,
391                copy,
392            } => {
393                let copy_src_target = glow::COPY_READ_BUFFER;
394                let is_index_buffer_only_element_dst = !self
395                    .shared
396                    .private_caps
397                    .contains(PrivateCapabilities::INDEX_BUFFER_ROLE_CHANGE)
398                    && dst_target == glow::ELEMENT_ARRAY_BUFFER
399                    || src_target == glow::ELEMENT_ARRAY_BUFFER;
400
401                // WebGL not allowed to copy data from other targets to element buffer and can't copy element data to other buffers
402                let copy_dst_target = if is_index_buffer_only_element_dst {
403                    glow::ELEMENT_ARRAY_BUFFER
404                } else {
405                    glow::COPY_WRITE_BUFFER
406                };
407                let size = copy.size.get() as usize;
408                match (src.raw, dst.raw) {
409                    (Some(ref src), Some(ref dst)) => {
410                        unsafe { gl.bind_buffer(copy_src_target, Some(*src)) };
411                        unsafe { gl.bind_buffer(copy_dst_target, Some(*dst)) };
412                        unsafe {
413                            gl.copy_buffer_sub_data(
414                                copy_src_target,
415                                copy_dst_target,
416                                copy.src_offset as _,
417                                copy.dst_offset as _,
418                                copy.size.get() as _,
419                            )
420                        };
421                    }
422                    (Some(src), None) => {
423                        let mut map_state = dst.map_state.lock();
424                        let dst_data = &mut map_state.data.as_mut().unwrap().as_mut_slice()
425                            [copy.dst_offset as usize..copy.dst_offset as usize + size];
426
427                        unsafe { gl.bind_buffer(copy_src_target, Some(src)) };
428                        unsafe {
429                            self.shared.get_buffer_sub_data(
430                                gl,
431                                copy_src_target,
432                                copy.src_offset as i32,
433                                dst_data,
434                            )
435                        };
436                    }
437                    (None, Some(dst)) => {
438                        let map_state = src.map_state.lock();
439                        let src_data = &map_state.data.as_ref().unwrap().as_slice()
440                            [copy.src_offset as usize..copy.src_offset as usize + size];
441                        unsafe { gl.bind_buffer(copy_dst_target, Some(dst)) };
442                        unsafe {
443                            gl.buffer_sub_data_u8_slice(
444                                copy_dst_target,
445                                copy.dst_offset as i32,
446                                src_data,
447                            )
448                        };
449                    }
450                    (None, None) => {
451                        todo!()
452                    }
453                }
454                unsafe { gl.bind_buffer(copy_src_target, None) };
455                if is_index_buffer_only_element_dst {
456                    unsafe {
457                        gl.bind_buffer(
458                            glow::ELEMENT_ARRAY_BUFFER,
459                            *self.current_index_buffer.lock(),
460                        )
461                    };
462                } else {
463                    unsafe { gl.bind_buffer(copy_dst_target, None) };
464                }
465            }
466            #[cfg(webgl)]
467            C::CopyExternalImageToTexture {
468                ref src,
469                dst,
470                dst_target,
471                dst_format,
472                dst_premultiplication,
473                ref copy,
474            } => {
475                const UNPACK_FLIP_Y_WEBGL: u32 =
476                    web_sys::WebGl2RenderingContext::UNPACK_FLIP_Y_WEBGL;
477                const UNPACK_PREMULTIPLY_ALPHA_WEBGL: u32 =
478                    web_sys::WebGl2RenderingContext::UNPACK_PREMULTIPLY_ALPHA_WEBGL;
479
480                unsafe {
481                    if src.flip_y {
482                        gl.pixel_store_bool(UNPACK_FLIP_Y_WEBGL, true);
483                    }
484                    if dst_premultiplication {
485                        gl.pixel_store_bool(UNPACK_PREMULTIPLY_ALPHA_WEBGL, true);
486                    }
487                }
488
489                unsafe { gl.bind_texture(dst_target, Some(dst)) };
490                let format_desc = self.shared.describe_texture_format(dst_format);
491                if is_layered_target(dst_target) {
492                    let z_offset = get_z_offset(dst_target, &copy.dst_base);
493
494                    match src.source {
495                        wgt::ExternalImageSource::ImageBitmap(ref b) => unsafe {
496                            gl.tex_sub_image_3d_with_image_bitmap(
497                                dst_target,
498                                copy.dst_base.mip_level as i32,
499                                copy.dst_base.origin.x as i32,
500                                copy.dst_base.origin.y as i32,
501                                z_offset as i32,
502                                copy.size.width as i32,
503                                copy.size.height as i32,
504                                copy.size.depth as i32,
505                                format_desc.external,
506                                format_desc.data_type,
507                                b,
508                            );
509                        },
510                        wgt::ExternalImageSource::HTMLImageElement(ref i) => unsafe {
511                            gl.tex_sub_image_3d_with_html_image_element(
512                                dst_target,
513                                copy.dst_base.mip_level as i32,
514                                copy.dst_base.origin.x as i32,
515                                copy.dst_base.origin.y as i32,
516                                z_offset as i32,
517                                copy.size.width as i32,
518                                copy.size.height as i32,
519                                copy.size.depth as i32,
520                                format_desc.external,
521                                format_desc.data_type,
522                                i,
523                            );
524                        },
525                        wgt::ExternalImageSource::HTMLVideoElement(ref v) => unsafe {
526                            gl.tex_sub_image_3d_with_html_video_element(
527                                dst_target,
528                                copy.dst_base.mip_level as i32,
529                                copy.dst_base.origin.x as i32,
530                                copy.dst_base.origin.y as i32,
531                                z_offset as i32,
532                                copy.size.width as i32,
533                                copy.size.height as i32,
534                                copy.size.depth as i32,
535                                format_desc.external,
536                                format_desc.data_type,
537                                v,
538                            );
539                        },
540                        wgt::ExternalImageSource::VideoFrame(ref v) => unsafe {
541                            gl.tex_sub_image_3d_with_video_frame(
542                                dst_target,
543                                copy.dst_base.mip_level as i32,
544                                copy.dst_base.origin.x as i32,
545                                copy.dst_base.origin.y as i32,
546                                z_offset as i32,
547                                copy.size.width as i32,
548                                copy.size.height as i32,
549                                copy.size.depth as i32,
550                                format_desc.external,
551                                format_desc.data_type,
552                                v,
553                            )
554                        },
555                        wgt::ExternalImageSource::ImageData(ref i) => unsafe {
556                            gl.tex_sub_image_3d_with_image_data(
557                                dst_target,
558                                copy.dst_base.mip_level as i32,
559                                copy.dst_base.origin.x as i32,
560                                copy.dst_base.origin.y as i32,
561                                z_offset as i32,
562                                copy.size.width as i32,
563                                copy.size.height as i32,
564                                copy.size.depth as i32,
565                                format_desc.external,
566                                format_desc.data_type,
567                                i,
568                            );
569                        },
570                        wgt::ExternalImageSource::HTMLCanvasElement(ref c) => unsafe {
571                            gl.tex_sub_image_3d_with_html_canvas_element(
572                                dst_target,
573                                copy.dst_base.mip_level as i32,
574                                copy.dst_base.origin.x as i32,
575                                copy.dst_base.origin.y as i32,
576                                z_offset as i32,
577                                copy.size.width as i32,
578                                copy.size.height as i32,
579                                copy.size.depth as i32,
580                                format_desc.external,
581                                format_desc.data_type,
582                                c,
583                            );
584                        },
585                        wgt::ExternalImageSource::OffscreenCanvas(ref c) => unsafe {
586                            // WebGL2's `texSubImage3D` accepts any `TexImageSource`,
587                            // including `OffscreenCanvas`, but web-sys 0.3.x generates
588                            // no typed overload for it. Re-wrap the same JS object as
589                            // `HtmlCanvasElement` — a type-erased pass-through, not a
590                            // conversion; the browser dispatches on the real object.
591                            // Tracked in wasm-bindgen PR#5312.
592                            use wasm_bindgen::JsCast as _;
593                            gl.tex_sub_image_3d_with_html_canvas_element(
594                                dst_target,
595                                copy.dst_base.mip_level as i32,
596                                copy.dst_base.origin.x as i32,
597                                copy.dst_base.origin.y as i32,
598                                z_offset as i32,
599                                copy.size.width as i32,
600                                copy.size.height as i32,
601                                copy.size.depth as i32,
602                                format_desc.external,
603                                format_desc.data_type,
604                                c.unchecked_ref(),
605                            );
606                        },
607                    }
608                } else {
609                    let dst_target = get_2d_target(dst_target, copy.dst_base.array_layer);
610
611                    match src.source {
612                        wgt::ExternalImageSource::ImageBitmap(ref b) => unsafe {
613                            gl.tex_sub_image_2d_with_image_bitmap_and_width_and_height(
614                                dst_target,
615                                copy.dst_base.mip_level as i32,
616                                copy.dst_base.origin.x as i32,
617                                copy.dst_base.origin.y as i32,
618                                copy.size.width as i32,
619                                copy.size.height as i32,
620                                format_desc.external,
621                                format_desc.data_type,
622                                b,
623                            );
624                        },
625                        wgt::ExternalImageSource::HTMLImageElement(ref i) => unsafe {
626                            gl.tex_sub_image_2d_with_html_image_and_width_and_height(
627                                dst_target,
628                                copy.dst_base.mip_level as i32,
629                                copy.dst_base.origin.x as i32,
630                                copy.dst_base.origin.y as i32,
631                                copy.size.width as i32,
632                                copy.size.height as i32,
633                                format_desc.external,
634                                format_desc.data_type,
635                                i,
636                            )
637                        },
638                        wgt::ExternalImageSource::HTMLVideoElement(ref v) => unsafe {
639                            gl.tex_sub_image_2d_with_html_video_and_width_and_height(
640                                dst_target,
641                                copy.dst_base.mip_level as i32,
642                                copy.dst_base.origin.x as i32,
643                                copy.dst_base.origin.y as i32,
644                                copy.size.width as i32,
645                                copy.size.height as i32,
646                                format_desc.external,
647                                format_desc.data_type,
648                                v,
649                            )
650                        },
651                        wgt::ExternalImageSource::VideoFrame(ref v) => unsafe {
652                            gl.tex_sub_image_2d_with_video_frame_and_width_and_height(
653                                dst_target,
654                                copy.dst_base.mip_level as i32,
655                                copy.dst_base.origin.x as i32,
656                                copy.dst_base.origin.y as i32,
657                                copy.size.width as i32,
658                                copy.size.height as i32,
659                                format_desc.external,
660                                format_desc.data_type,
661                                v,
662                            )
663                        },
664                        wgt::ExternalImageSource::ImageData(ref i) => unsafe {
665                            gl.tex_sub_image_2d_with_image_data_and_width_and_height(
666                                dst_target,
667                                copy.dst_base.mip_level as i32,
668                                copy.dst_base.origin.x as i32,
669                                copy.dst_base.origin.y as i32,
670                                copy.size.width as i32,
671                                copy.size.height as i32,
672                                format_desc.external,
673                                format_desc.data_type,
674                                i,
675                            );
676                        },
677                        wgt::ExternalImageSource::HTMLCanvasElement(ref c) => unsafe {
678                            gl.tex_sub_image_2d_with_html_canvas_and_width_and_height(
679                                dst_target,
680                                copy.dst_base.mip_level as i32,
681                                copy.dst_base.origin.x as i32,
682                                copy.dst_base.origin.y as i32,
683                                copy.size.width as i32,
684                                copy.size.height as i32,
685                                format_desc.external,
686                                format_desc.data_type,
687                                c,
688                            )
689                        },
690                        wgt::ExternalImageSource::OffscreenCanvas(ref c) => unsafe {
691                            // Same `TexImageSource` pass-through as the 3D path above:
692                            // web-sys 0.3.x has no `OffscreenCanvas` overload, so the
693                            // handle rides the `HtmlCanvasElement` binding unchanged.
694                            use wasm_bindgen::JsCast as _;
695                            gl.tex_sub_image_2d_with_html_canvas_and_width_and_height(
696                                dst_target,
697                                copy.dst_base.mip_level as i32,
698                                copy.dst_base.origin.x as i32,
699                                copy.dst_base.origin.y as i32,
700                                copy.size.width as i32,
701                                copy.size.height as i32,
702                                format_desc.external,
703                                format_desc.data_type,
704                                c.unchecked_ref(),
705                            )
706                        },
707                    }
708                }
709
710                unsafe {
711                    if src.flip_y {
712                        gl.pixel_store_bool(UNPACK_FLIP_Y_WEBGL, false);
713                    }
714                    if dst_premultiplication {
715                        gl.pixel_store_bool(UNPACK_PREMULTIPLY_ALPHA_WEBGL, false);
716                    }
717                }
718            }
719            C::CopyTextureToTexture {
720                src,
721                src_target,
722                dst,
723                dst_target,
724                ref copy,
725            } => {
726                unsafe { gl.bind_framebuffer(glow::READ_FRAMEBUFFER, Some(self.copy_fbo)) };
727                unsafe { gl.bind_texture(dst_target, Some(dst)) };
728
729                // The read framebuffer holds a single 2D slice at a time, so a copy of
730                // depth > 1 is issued one slice per iteration.
731                for z in 0..copy.size.depth {
732                    if is_layered_target(src_target) {
733                        //TODO: handle GLES without framebuffer_texture_3d
734                        unsafe {
735                            gl.framebuffer_texture_layer(
736                                glow::READ_FRAMEBUFFER,
737                                glow::COLOR_ATTACHMENT0,
738                                Some(src),
739                                copy.src_base.mip_level as i32,
740                                (get_z_offset(src_target, &copy.src_base) + z) as i32,
741                            )
742                        };
743                    } else {
744                        unsafe {
745                            gl.framebuffer_texture_2d(
746                                glow::READ_FRAMEBUFFER,
747                                glow::COLOR_ATTACHMENT0,
748                                get_2d_target(src_target, copy.src_base.array_layer + z),
749                                Some(src),
750                                copy.src_base.mip_level as i32,
751                            )
752                        };
753                    }
754
755                    if is_layered_target(dst_target) {
756                        unsafe {
757                            gl.copy_tex_sub_image_3d(
758                                dst_target,
759                                copy.dst_base.mip_level as i32,
760                                copy.dst_base.origin.x as i32,
761                                copy.dst_base.origin.y as i32,
762                                (get_z_offset(dst_target, &copy.dst_base) + z) as i32,
763                                copy.src_base.origin.x as i32,
764                                copy.src_base.origin.y as i32,
765                                copy.size.width as i32,
766                                copy.size.height as i32,
767                            )
768                        };
769                    } else {
770                        unsafe {
771                            gl.copy_tex_sub_image_2d(
772                                get_2d_target(dst_target, copy.dst_base.array_layer + z),
773                                copy.dst_base.mip_level as i32,
774                                copy.dst_base.origin.x as i32,
775                                copy.dst_base.origin.y as i32,
776                                copy.src_base.origin.x as i32,
777                                copy.src_base.origin.y as i32,
778                                copy.size.width as i32,
779                                copy.size.height as i32,
780                            )
781                        };
782                    }
783                }
784            }
785            C::CopyBufferToTexture {
786                ref src,
787                src_target: _,
788                dst,
789                dst_target,
790                dst_format,
791                ref copy,
792            } => {
793                let (block_width, block_height) = dst_format.block_dimensions();
794                let block_size = dst_format.block_copy_size(None).unwrap();
795                let format_desc = self.shared.describe_texture_format(dst_format);
796                let row_texels = copy
797                    .buffer_layout
798                    .bytes_per_row
799                    .map_or(0, |bpr| block_width * bpr / block_size);
800                let column_texels = copy
801                    .buffer_layout
802                    .rows_per_image
803                    .map_or(0, |rpi| block_height * rpi);
804
805                unsafe { gl.bind_texture(dst_target, Some(dst)) };
806                unsafe { gl.pixel_store_i32(glow::UNPACK_ROW_LENGTH, row_texels as i32) };
807                unsafe { gl.pixel_store_i32(glow::UNPACK_IMAGE_HEIGHT, column_texels as i32) };
808                let mut unbind_unpack_buffer = false;
809                if !dst_format.is_compressed() {
810                    let map_state;
811                    let unpack_data = match src.raw {
812                        Some(buffer) => {
813                            unsafe { gl.bind_buffer(glow::PIXEL_UNPACK_BUFFER, Some(buffer)) };
814                            unbind_unpack_buffer = true;
815                            glow::PixelUnpackData::BufferOffset(copy.buffer_layout.offset as u32)
816                        }
817                        None => {
818                            map_state = src.map_state.lock();
819                            let src_data = &map_state.data.as_ref().unwrap().as_slice()
820                                [copy.buffer_layout.offset as usize..];
821                            glow::PixelUnpackData::Slice(Some(src_data))
822                        }
823                    };
824                    if is_layered_target(dst_target) {
825                        unsafe {
826                            gl.tex_sub_image_3d(
827                                dst_target,
828                                copy.texture_base.mip_level as i32,
829                                copy.texture_base.origin.x as i32,
830                                copy.texture_base.origin.y as i32,
831                                get_z_offset(dst_target, &copy.texture_base) as i32,
832                                copy.size.width as i32,
833                                copy.size.height as i32,
834                                copy.size.depth as i32,
835                                format_desc.external,
836                                format_desc.data_type,
837                                unpack_data,
838                            )
839                        };
840                    } else {
841                        unsafe {
842                            gl.tex_sub_image_2d(
843                                get_2d_target(dst_target, copy.texture_base.array_layer),
844                                copy.texture_base.mip_level as i32,
845                                copy.texture_base.origin.x as i32,
846                                copy.texture_base.origin.y as i32,
847                                copy.size.width as i32,
848                                copy.size.height as i32,
849                                format_desc.external,
850                                format_desc.data_type,
851                                unpack_data,
852                            )
853                        };
854                    }
855                } else {
856                    let bytes_per_row = copy
857                        .buffer_layout
858                        .bytes_per_row
859                        .unwrap_or(copy.size.width * block_size);
860                    let minimum_rows_per_image = copy.size.height.div_ceil(block_height);
861                    let rows_per_image = copy
862                        .buffer_layout
863                        .rows_per_image
864                        .unwrap_or(minimum_rows_per_image);
865
866                    let bytes_per_image = bytes_per_row * rows_per_image;
867                    let minimum_bytes_per_image = bytes_per_row * minimum_rows_per_image;
868                    let bytes_in_upload =
869                        (bytes_per_image * (copy.size.depth - 1)) + minimum_bytes_per_image;
870                    let offset = copy.buffer_layout.offset as u32;
871
872                    let map_state;
873                    let unpack_data = match src.raw {
874                        Some(buffer) => {
875                            unsafe { gl.bind_buffer(glow::PIXEL_UNPACK_BUFFER, Some(buffer)) };
876                            unbind_unpack_buffer = true;
877                            glow::CompressedPixelUnpackData::BufferRange(
878                                offset..offset + bytes_in_upload,
879                            )
880                        }
881                        None => {
882                            map_state = src.map_state.lock();
883                            let src_data = &map_state.data.as_ref().unwrap().as_slice()
884                                [(offset as usize)..(offset + bytes_in_upload) as usize];
885                            glow::CompressedPixelUnpackData::Slice(src_data)
886                        }
887                    };
888
889                    if is_layered_target(dst_target) {
890                        unsafe {
891                            gl.compressed_tex_sub_image_3d(
892                                dst_target,
893                                copy.texture_base.mip_level as i32,
894                                copy.texture_base.origin.x as i32,
895                                copy.texture_base.origin.y as i32,
896                                get_z_offset(dst_target, &copy.texture_base) as i32,
897                                copy.size.width as i32,
898                                copy.size.height as i32,
899                                copy.size.depth as i32,
900                                format_desc.internal,
901                                unpack_data,
902                            )
903                        };
904                    } else {
905                        unsafe {
906                            gl.compressed_tex_sub_image_2d(
907                                get_2d_target(dst_target, copy.texture_base.array_layer),
908                                copy.texture_base.mip_level as i32,
909                                copy.texture_base.origin.x as i32,
910                                copy.texture_base.origin.y as i32,
911                                copy.size.width as i32,
912                                copy.size.height as i32,
913                                format_desc.internal,
914                                unpack_data,
915                            )
916                        };
917                    }
918                }
919                if unbind_unpack_buffer {
920                    unsafe { gl.bind_buffer(glow::PIXEL_UNPACK_BUFFER, None) };
921                }
922            }
923            C::CopyTextureToBuffer {
924                src,
925                src_target,
926                src_format,
927                ref dst,
928                dst_target: _,
929                ref copy,
930            } => {
931                let block_size = src_format.block_copy_size(None).unwrap();
932                if src_format.is_compressed() {
933                    log::error!("Not implemented yet: compressed texture copy to buffer");
934                    return;
935                }
936                if src_target == glow::TEXTURE_CUBE_MAP
937                    || src_target == glow::TEXTURE_CUBE_MAP_ARRAY
938                {
939                    log::error!("Not implemented yet: cubemap texture copy to buffer");
940                    return;
941                }
942                let format_desc = self.shared.describe_texture_format(src_format);
943                let row_texels = copy
944                    .buffer_layout
945                    .bytes_per_row
946                    .map_or(copy.size.width, |bpr| bpr / block_size);
947                let column_texels = copy
948                    .buffer_layout
949                    .rows_per_image
950                    .unwrap_or(copy.size.height);
951
952                unsafe { gl.bind_framebuffer(glow::READ_FRAMEBUFFER, Some(self.copy_fbo)) };
953
954                let read_pixels = |offset| {
955                    let mut map_state;
956                    let unpack_data = match dst.raw {
957                        Some(buffer) => {
958                            unsafe { gl.pixel_store_i32(glow::PACK_ROW_LENGTH, row_texels as i32) };
959                            unsafe { gl.bind_buffer(glow::PIXEL_PACK_BUFFER, Some(buffer)) };
960                            glow::PixelPackData::BufferOffset(offset as u32)
961                        }
962                        None => {
963                            map_state = dst.map_state.lock();
964                            let dst_data = &mut map_state.data.as_mut().unwrap().as_mut_slice()
965                                [offset as usize..];
966                            glow::PixelPackData::Slice(Some(dst_data))
967                        }
968                    };
969                    unsafe {
970                        gl.read_pixels(
971                            copy.texture_base.origin.x as i32,
972                            copy.texture_base.origin.y as i32,
973                            copy.size.width as i32,
974                            copy.size.height as i32,
975                            format_desc.external,
976                            format_desc.data_type,
977                            unpack_data,
978                        )
979                    };
980                };
981
982                match src_target {
983                    glow::TEXTURE_2D => {
984                        unsafe {
985                            gl.framebuffer_texture_2d(
986                                glow::READ_FRAMEBUFFER,
987                                glow::COLOR_ATTACHMENT0,
988                                src_target,
989                                Some(src),
990                                copy.texture_base.mip_level as i32,
991                            )
992                        };
993                        read_pixels(copy.buffer_layout.offset);
994                    }
995                    glow::TEXTURE_2D_ARRAY => {
996                        unsafe {
997                            gl.framebuffer_texture_layer(
998                                glow::READ_FRAMEBUFFER,
999                                glow::COLOR_ATTACHMENT0,
1000                                Some(src),
1001                                copy.texture_base.mip_level as i32,
1002                                copy.texture_base.array_layer as i32,
1003                            )
1004                        };
1005                        read_pixels(copy.buffer_layout.offset);
1006                    }
1007                    glow::TEXTURE_3D => {
1008                        for z in copy.texture_base.origin.z..copy.size.depth {
1009                            unsafe {
1010                                gl.framebuffer_texture_layer(
1011                                    glow::READ_FRAMEBUFFER,
1012                                    glow::COLOR_ATTACHMENT0,
1013                                    Some(src),
1014                                    copy.texture_base.mip_level as i32,
1015                                    z as i32,
1016                                )
1017                            };
1018                            let offset = copy.buffer_layout.offset
1019                                + (z * block_size * row_texels * column_texels) as u64;
1020                            read_pixels(offset);
1021                        }
1022                    }
1023                    glow::TEXTURE_CUBE_MAP | glow::TEXTURE_CUBE_MAP_ARRAY => unimplemented!(),
1024                    _ => unreachable!(),
1025                }
1026            }
1027            C::SetIndexBuffer(buffer) => {
1028                unsafe { gl.bind_buffer(glow::ELEMENT_ARRAY_BUFFER, Some(buffer)) };
1029                let mut current_index_buffer = self.current_index_buffer.lock();
1030                *current_index_buffer = Some(buffer);
1031            }
1032            C::BeginQuery(query, target) => {
1033                unsafe { gl.begin_query(target, query) };
1034            }
1035            C::EndQuery(target) => {
1036                unsafe { gl.end_query(target) };
1037            }
1038            C::TimestampQuery(query) => {
1039                unsafe { gl.query_counter(query, glow::TIMESTAMP) };
1040            }
1041            C::CopyQueryResults {
1042                ref query_range,
1043                ref dst,
1044                dst_target,
1045                dst_offset,
1046            } => {
1047                if self
1048                    .shared
1049                    .private_caps
1050                    .contains(PrivateCapabilities::QUERY_BUFFERS)
1051                    && dst.raw.is_some()
1052                {
1053                    unsafe {
1054                        // We're assuming that the only relevant queries are 8 byte timestamps or
1055                        // occlusion tests.
1056                        let query_size = 8;
1057
1058                        let query_range_size = query_size * query_range.len();
1059
1060                        let buffer = gl.create_buffer().ok();
1061                        gl.bind_buffer(glow::QUERY_BUFFER, buffer);
1062                        gl.buffer_data_size(
1063                            glow::QUERY_BUFFER,
1064                            query_range_size as _,
1065                            glow::STREAM_COPY,
1066                        );
1067
1068                        for (i, &query) in queries
1069                            [query_range.start as usize..query_range.end as usize]
1070                            .iter()
1071                            .enumerate()
1072                        {
1073                            gl.get_query_parameter_u64_with_offset(
1074                                query,
1075                                glow::QUERY_RESULT,
1076                                query_size * i,
1077                            )
1078                        }
1079                        gl.bind_buffer(dst_target, dst.raw);
1080                        gl.copy_buffer_sub_data(
1081                            glow::QUERY_BUFFER,
1082                            dst_target,
1083                            0,
1084                            dst_offset as _,
1085                            query_range_size as _,
1086                        );
1087                        if let Some(buffer) = buffer {
1088                            gl.delete_buffer(buffer)
1089                        }
1090                    }
1091                } else {
1092                    let mut temp_query_results = self.temp_query_results.lock();
1093                    temp_query_results.clear();
1094                    for &query in
1095                        queries[query_range.start as usize..query_range.end as usize].iter()
1096                    {
1097                        let mut result: u64 = 0;
1098                        unsafe {
1099                            if self
1100                                .shared
1101                                .private_caps
1102                                .contains(PrivateCapabilities::QUERY_64BIT)
1103                            {
1104                                let result: *mut u64 = &mut result;
1105                                gl.get_query_parameter_u64_with_offset(
1106                                    query,
1107                                    glow::QUERY_RESULT,
1108                                    result as usize,
1109                                )
1110                            } else {
1111                                result =
1112                                    gl.get_query_parameter_u32(query, glow::QUERY_RESULT) as u64;
1113                            }
1114                        };
1115                        temp_query_results.push(result);
1116                    }
1117                    let query_data = bytemuck::cast_slice(&temp_query_results);
1118                    match dst.raw {
1119                        Some(buffer) => {
1120                            unsafe { gl.bind_buffer(dst_target, Some(buffer)) };
1121                            unsafe {
1122                                gl.buffer_sub_data_u8_slice(
1123                                    dst_target,
1124                                    dst_offset as i32,
1125                                    query_data,
1126                                )
1127                            };
1128                        }
1129                        None => {
1130                            let mut map_state = dst.map_state.lock();
1131                            let data = map_state.data.as_mut().unwrap();
1132                            let len = query_data.len().min(data.len());
1133                            data[..len].copy_from_slice(&query_data[..len]);
1134                        }
1135                    }
1136                }
1137            }
1138            C::ResetFramebuffer { is_default } => {
1139                if is_default {
1140                    unsafe { gl.bind_framebuffer(glow::DRAW_FRAMEBUFFER, None) };
1141                } else {
1142                    unsafe { gl.bind_framebuffer(glow::DRAW_FRAMEBUFFER, Some(self.draw_fbo)) };
1143                    // Android Emulator's GL client tracks texture and renderbuffer attachments
1144                    // separately, so detach both types before reusing the framebuffer.
1145                    unsafe {
1146                        gl.framebuffer_renderbuffer(
1147                            glow::DRAW_FRAMEBUFFER,
1148                            glow::DEPTH_STENCIL_ATTACHMENT,
1149                            glow::RENDERBUFFER,
1150                            None,
1151                        );
1152                        gl.framebuffer_texture_2d(
1153                            glow::DRAW_FRAMEBUFFER,
1154                            glow::DEPTH_STENCIL_ATTACHMENT,
1155                            glow::TEXTURE_2D,
1156                            None,
1157                            0,
1158                        )
1159                    };
1160                    for i in 0..self.shared.limits.max_color_attachments {
1161                        let target = glow::COLOR_ATTACHMENT0 + i;
1162                        unsafe {
1163                            gl.framebuffer_renderbuffer(
1164                                glow::DRAW_FRAMEBUFFER,
1165                                target,
1166                                glow::RENDERBUFFER,
1167                                None,
1168                            );
1169                            gl.framebuffer_texture_2d(
1170                                glow::DRAW_FRAMEBUFFER,
1171                                target,
1172                                glow::TEXTURE_2D,
1173                                None,
1174                                0,
1175                            )
1176                        };
1177                    }
1178                }
1179                unsafe { gl.color_mask(true, true, true, true) };
1180                unsafe { gl.depth_mask(true) };
1181                unsafe { gl.stencil_mask(!0) };
1182                unsafe { gl.disable(glow::DEPTH_TEST) };
1183                unsafe { gl.disable(glow::STENCIL_TEST) };
1184                unsafe { gl.disable(glow::SCISSOR_TEST) };
1185            }
1186            C::BindAttachment {
1187                attachment,
1188                ref view,
1189                depth_slice,
1190                sample_count,
1191            } => {
1192                unsafe {
1193                    self.set_attachment(
1194                        gl,
1195                        glow::DRAW_FRAMEBUFFER,
1196                        attachment,
1197                        view,
1198                        depth_slice,
1199                        sample_count,
1200                    )
1201                };
1202            }
1203            C::ResolveAttachment {
1204                attachment,
1205                ref dst,
1206                ref size,
1207            } => {
1208                unsafe { gl.bind_framebuffer(glow::READ_FRAMEBUFFER, Some(self.draw_fbo)) };
1209                unsafe { gl.read_buffer(attachment) };
1210                unsafe { gl.bind_framebuffer(glow::DRAW_FRAMEBUFFER, Some(self.copy_fbo)) };
1211                unsafe {
1212                    self.set_attachment(
1213                        gl,
1214                        glow::DRAW_FRAMEBUFFER,
1215                        glow::COLOR_ATTACHMENT0,
1216                        dst,
1217                        None,
1218                        1,
1219                    )
1220                };
1221                unsafe {
1222                    gl.blit_framebuffer(
1223                        0,
1224                        0,
1225                        size.width as i32,
1226                        size.height as i32,
1227                        0,
1228                        0,
1229                        size.width as i32,
1230                        size.height as i32,
1231                        glow::COLOR_BUFFER_BIT,
1232                        glow::NEAREST,
1233                    )
1234                };
1235                unsafe { gl.bind_framebuffer(glow::READ_FRAMEBUFFER, None) };
1236                unsafe { gl.bind_framebuffer(glow::DRAW_FRAMEBUFFER, Some(self.draw_fbo)) };
1237            }
1238            C::InvalidateAttachments(ref list) => {
1239                if self
1240                    .shared
1241                    .private_caps
1242                    .contains(PrivateCapabilities::INVALIDATE_FRAMEBUFFER)
1243                {
1244                    unsafe { gl.invalidate_framebuffer(glow::DRAW_FRAMEBUFFER, list) };
1245                }
1246            }
1247            C::SetDrawColorBuffers(count) => {
1248                self.draw_buffer_count.store(count, Ordering::Relaxed);
1249                let indices = (0..count as u32)
1250                    .map(|i| glow::COLOR_ATTACHMENT0 + i)
1251                    .collect::<ArrayVec<_, { crate::MAX_COLOR_ATTACHMENTS }>>();
1252                unsafe { gl.draw_buffers(&indices) };
1253            }
1254            C::ClearColorF {
1255                draw_buffer,
1256                ref color,
1257                is_srgb,
1258            } => {
1259                if self
1260                    .shared
1261                    .workarounds
1262                    .contains(super::Workarounds::MESA_I915_SRGB_SHADER_CLEAR)
1263                    && is_srgb
1264                {
1265                    unsafe { self.perform_shader_clear(gl, draw_buffer, *color) };
1266                } else {
1267                    unsafe { gl.clear_buffer_f32_slice(glow::COLOR, draw_buffer, color) };
1268                }
1269            }
1270            C::ClearColorU(draw_buffer, ref color) => {
1271                unsafe { gl.clear_buffer_u32_slice(glow::COLOR, draw_buffer, color) };
1272            }
1273            C::ClearColorI(draw_buffer, ref color) => {
1274                unsafe { gl.clear_buffer_i32_slice(glow::COLOR, draw_buffer, color) };
1275            }
1276            C::ClearDepth(depth) => {
1277                // Prefer `clear` as `clear_buffer` functions have issues on Sandy Bridge
1278                // on Windows.
1279                unsafe {
1280                    gl.clear_depth_f32(depth);
1281                    gl.clear(glow::DEPTH_BUFFER_BIT);
1282                }
1283            }
1284            C::ClearStencil(value) => {
1285                // Prefer `clear` as `clear_buffer` functions have issues on Sandy Bridge
1286                // on Windows.
1287                unsafe {
1288                    gl.clear_stencil(value as i32);
1289                    gl.clear(glow::STENCIL_BUFFER_BIT);
1290                }
1291            }
1292            C::ClearDepthAndStencil(depth, stencil_value) => {
1293                // Prefer `clear` as `clear_buffer` functions have issues on Sandy Bridge
1294                // on Windows.
1295                unsafe {
1296                    gl.clear_depth_f32(depth);
1297                    gl.clear_stencil(stencil_value as i32);
1298                    gl.clear(glow::DEPTH_BUFFER_BIT | glow::STENCIL_BUFFER_BIT);
1299                }
1300            }
1301            C::BufferBarrier(raw, usage) => {
1302                let mut flags = 0;
1303                if usage.contains(wgt::BufferUses::VERTEX) {
1304                    flags |= glow::VERTEX_ATTRIB_ARRAY_BARRIER_BIT;
1305                    unsafe { gl.bind_buffer(glow::ARRAY_BUFFER, Some(raw)) };
1306                    unsafe { gl.vertex_attrib_pointer_f32(0, 1, glow::BYTE, true, 0, 0) };
1307                }
1308                if usage.contains(wgt::BufferUses::INDEX) {
1309                    flags |= glow::ELEMENT_ARRAY_BARRIER_BIT;
1310                    unsafe { gl.bind_buffer(glow::ELEMENT_ARRAY_BUFFER, Some(raw)) };
1311                }
1312                if usage.contains(wgt::BufferUses::UNIFORM) {
1313                    flags |= glow::UNIFORM_BARRIER_BIT;
1314                }
1315                if usage.contains(wgt::BufferUses::INDIRECT) {
1316                    flags |= glow::COMMAND_BARRIER_BIT;
1317                    unsafe { gl.bind_buffer(glow::DRAW_INDIRECT_BUFFER, Some(raw)) };
1318                }
1319                if usage.contains(wgt::BufferUses::COPY_SRC) {
1320                    flags |= glow::PIXEL_BUFFER_BARRIER_BIT;
1321                    unsafe { gl.bind_buffer(glow::PIXEL_UNPACK_BUFFER, Some(raw)) };
1322                }
1323                if usage.contains(wgt::BufferUses::COPY_DST) {
1324                    flags |= glow::PIXEL_BUFFER_BARRIER_BIT;
1325                    unsafe { gl.bind_buffer(glow::PIXEL_PACK_BUFFER, Some(raw)) };
1326                }
1327                if usage.intersects(wgt::BufferUses::MAP_READ | wgt::BufferUses::MAP_WRITE) {
1328                    flags |= glow::BUFFER_UPDATE_BARRIER_BIT;
1329                }
1330                if usage.intersects(
1331                    wgt::BufferUses::STORAGE_READ_ONLY | wgt::BufferUses::STORAGE_READ_WRITE,
1332                ) {
1333                    flags |= glow::SHADER_STORAGE_BARRIER_BIT;
1334                }
1335                unsafe { gl.memory_barrier(flags) };
1336            }
1337            // because `STORAGE_WRITE_ONLY` and `STORAGE_READ_WRITE` are only states
1338            // we can transit from due OpenGL memory barriers are used to make _subsequent_
1339            // operations see changes from the _shader_ side. We filter out usage changes that are
1340            // does not comes from the shader side in `transition_textures`
1341            C::TextureBarrier(usage) => {
1342                let mut flags = 0;
1343                if usage.contains(wgt::TextureUses::RESOURCE) {
1344                    flags |= glow::TEXTURE_FETCH_BARRIER_BIT;
1345                }
1346                if usage.intersects(
1347                    wgt::TextureUses::STORAGE_READ_ONLY
1348                        | wgt::TextureUses::STORAGE_WRITE_ONLY
1349                        | wgt::TextureUses::STORAGE_READ_WRITE,
1350                ) {
1351                    flags |= glow::SHADER_IMAGE_ACCESS_BARRIER_BIT;
1352                }
1353                if usage.intersects(wgt::TextureUses::COPY_SRC) {
1354                    flags |= glow::PIXEL_BUFFER_BARRIER_BIT;
1355                }
1356                if usage.contains(wgt::TextureUses::COPY_DST) {
1357                    flags |= glow::TEXTURE_UPDATE_BARRIER_BIT;
1358                }
1359                if usage.intersects(
1360                    wgt::TextureUses::COLOR_TARGET
1361                        | wgt::TextureUses::DEPTH_READ
1362                        | wgt::TextureUses::DEPTH_WRITE
1363                        | wgt::TextureUses::STENCIL_READ
1364                        | wgt::TextureUses::STENCIL_WRITE,
1365                ) {
1366                    flags |= glow::FRAMEBUFFER_BARRIER_BIT;
1367                }
1368                unsafe { gl.memory_barrier(flags) };
1369            }
1370            C::SetViewport {
1371                ref rect,
1372                ref depth,
1373            } => {
1374                unsafe { gl.viewport(rect.x, rect.y, rect.w, rect.h) };
1375                unsafe { gl.depth_range_f32(depth.start, depth.end) };
1376            }
1377            C::SetScissor(ref rect) => {
1378                unsafe { gl.scissor(rect.x, rect.y, rect.w, rect.h) };
1379                unsafe { gl.enable(glow::SCISSOR_TEST) };
1380            }
1381            C::SetStencilFunc {
1382                face,
1383                function,
1384                reference,
1385                read_mask,
1386            } => {
1387                unsafe { gl.stencil_func_separate(face, function, reference as i32, read_mask) };
1388            }
1389            C::SetStencilOps {
1390                face,
1391                write_mask,
1392                ref ops,
1393            } => {
1394                unsafe { gl.stencil_mask_separate(face, write_mask) };
1395                unsafe { gl.stencil_op_separate(face, ops.fail, ops.depth_fail, ops.pass) };
1396            }
1397            C::SetVertexAttribute {
1398                buffer,
1399                ref buffer_desc,
1400                attribute_desc: ref vat,
1401            } => {
1402                unsafe { gl.bind_buffer(glow::ARRAY_BUFFER, buffer) };
1403                unsafe { gl.enable_vertex_attrib_array(vat.location) };
1404
1405                if buffer.is_none() {
1406                    match vat.format_desc.attrib_kind {
1407                        super::VertexAttribKind::Float => unsafe {
1408                            gl.vertex_attrib_format_f32(
1409                                vat.location,
1410                                vat.format_desc.element_count,
1411                                vat.format_desc.element_format,
1412                                true, // always normalized
1413                                vat.offset,
1414                            )
1415                        },
1416                        super::VertexAttribKind::Integer => unsafe {
1417                            gl.vertex_attrib_format_i32(
1418                                vat.location,
1419                                vat.format_desc.element_count,
1420                                vat.format_desc.element_format,
1421                                vat.offset,
1422                            )
1423                        },
1424                    }
1425
1426                    //Note: there is apparently a bug on AMD 3500U:
1427                    // this call is ignored if the current array is disabled.
1428                    unsafe { gl.vertex_attrib_binding(vat.location, vat.buffer_index) };
1429                } else {
1430                    match vat.format_desc.attrib_kind {
1431                        super::VertexAttribKind::Float => unsafe {
1432                            gl.vertex_attrib_pointer_f32(
1433                                vat.location,
1434                                vat.format_desc.element_count,
1435                                vat.format_desc.element_format,
1436                                true, // always normalized
1437                                buffer_desc.stride as i32,
1438                                vat.offset as i32,
1439                            )
1440                        },
1441                        super::VertexAttribKind::Integer => unsafe {
1442                            gl.vertex_attrib_pointer_i32(
1443                                vat.location,
1444                                vat.format_desc.element_count,
1445                                vat.format_desc.element_format,
1446                                buffer_desc.stride as i32,
1447                                vat.offset as i32,
1448                            )
1449                        },
1450                    }
1451                    unsafe { gl.vertex_attrib_divisor(vat.location, buffer_desc.step as u32) };
1452                }
1453            }
1454            C::UnsetVertexAttribute(location) => {
1455                unsafe { gl.disable_vertex_attrib_array(location) };
1456            }
1457            C::SetVertexBuffer {
1458                index,
1459                ref buffer,
1460                ref buffer_desc,
1461            } => {
1462                unsafe { gl.vertex_binding_divisor(index, buffer_desc.step as u32) };
1463                unsafe {
1464                    gl.bind_vertex_buffer(
1465                        index,
1466                        Some(buffer.raw),
1467                        buffer.offset as i32,
1468                        buffer_desc.stride as i32,
1469                    )
1470                };
1471            }
1472            C::SetDepth(ref depth) => {
1473                unsafe { gl.depth_func(depth.function) };
1474                unsafe { gl.depth_mask(depth.mask) };
1475            }
1476            C::SetDepthBias(bias) => {
1477                if bias.is_enabled() {
1478                    unsafe { gl.enable(glow::POLYGON_OFFSET_FILL) };
1479                    unsafe { gl.polygon_offset(bias.slope_scale, bias.constant as f32) };
1480                } else {
1481                    unsafe { gl.disable(glow::POLYGON_OFFSET_FILL) };
1482                }
1483            }
1484            C::ConfigureDepthStencil(aspects) => {
1485                if aspects.contains(crate::FormatAspects::DEPTH) {
1486                    unsafe { gl.enable(glow::DEPTH_TEST) };
1487                } else {
1488                    unsafe { gl.disable(glow::DEPTH_TEST) };
1489                }
1490                if aspects.contains(crate::FormatAspects::STENCIL) {
1491                    unsafe { gl.enable(glow::STENCIL_TEST) };
1492                } else {
1493                    unsafe { gl.disable(glow::STENCIL_TEST) };
1494                }
1495            }
1496            C::SetAlphaToCoverage(enabled) => {
1497                if enabled {
1498                    unsafe { gl.enable(glow::SAMPLE_ALPHA_TO_COVERAGE) };
1499                } else {
1500                    unsafe { gl.disable(glow::SAMPLE_ALPHA_TO_COVERAGE) };
1501                }
1502            }
1503            C::SetProgram(program) => {
1504                unsafe { gl.use_program(Some(program)) };
1505            }
1506            C::SetPrimitive(ref state) => {
1507                unsafe { gl.front_face(state.front_face) };
1508                if state.cull_face != 0 {
1509                    unsafe { gl.enable(glow::CULL_FACE) };
1510                    unsafe { gl.cull_face(state.cull_face) };
1511                } else {
1512                    unsafe { gl.disable(glow::CULL_FACE) };
1513                }
1514                if self.features.contains(wgt::Features::DEPTH_CLIP_CONTROL) {
1515                    //Note: this is a bit tricky, since we are controlling the clip, not the clamp.
1516                    if state.unclipped_depth {
1517                        unsafe { gl.enable(glow::DEPTH_CLAMP) };
1518                    } else {
1519                        unsafe { gl.disable(glow::DEPTH_CLAMP) };
1520                    }
1521                }
1522                // POLYGON_MODE_LINE also implies POLYGON_MODE_POINT
1523                if self.features.contains(wgt::Features::POLYGON_MODE_LINE) {
1524                    unsafe { gl.polygon_mode(glow::FRONT_AND_BACK, state.polygon_mode) };
1525                }
1526            }
1527            C::SetBlendConstant(c) => {
1528                unsafe { gl.blend_color(c[0], c[1], c[2], c[3]) };
1529            }
1530            C::SetColorTarget {
1531                draw_buffer_index,
1532                desc: super::ColorTargetDesc { mask, ref blend },
1533            } => {
1534                use wgt::ColorWrites as Cw;
1535                if let Some(index) = draw_buffer_index {
1536                    unsafe {
1537                        gl.color_mask_draw_buffer(
1538                            index,
1539                            mask.contains(Cw::RED),
1540                            mask.contains(Cw::GREEN),
1541                            mask.contains(Cw::BLUE),
1542                            mask.contains(Cw::ALPHA),
1543                        )
1544                    };
1545                    if let Some(ref blend) = *blend {
1546                        unsafe { gl.enable_draw_buffer(glow::BLEND, index) };
1547                        if blend.color != blend.alpha {
1548                            unsafe {
1549                                gl.blend_equation_separate_draw_buffer(
1550                                    index,
1551                                    blend.color.equation,
1552                                    blend.alpha.equation,
1553                                )
1554                            };
1555                            unsafe {
1556                                gl.blend_func_separate_draw_buffer(
1557                                    index,
1558                                    blend.color.src,
1559                                    blend.color.dst,
1560                                    blend.alpha.src,
1561                                    blend.alpha.dst,
1562                                )
1563                            };
1564                        } else {
1565                            unsafe { gl.blend_equation_draw_buffer(index, blend.color.equation) };
1566                            unsafe {
1567                                gl.blend_func_draw_buffer(index, blend.color.src, blend.color.dst)
1568                            };
1569                        }
1570                    } else {
1571                        unsafe { gl.disable_draw_buffer(glow::BLEND, index) };
1572                    }
1573                } else {
1574                    unsafe {
1575                        gl.color_mask(
1576                            mask.contains(Cw::RED),
1577                            mask.contains(Cw::GREEN),
1578                            mask.contains(Cw::BLUE),
1579                            mask.contains(Cw::ALPHA),
1580                        )
1581                    };
1582                    if let Some(ref blend) = *blend {
1583                        unsafe { gl.enable(glow::BLEND) };
1584                        if blend.color != blend.alpha {
1585                            unsafe {
1586                                gl.blend_equation_separate(
1587                                    blend.color.equation,
1588                                    blend.alpha.equation,
1589                                )
1590                            };
1591                            unsafe {
1592                                gl.blend_func_separate(
1593                                    blend.color.src,
1594                                    blend.color.dst,
1595                                    blend.alpha.src,
1596                                    blend.alpha.dst,
1597                                )
1598                            };
1599                        } else {
1600                            unsafe { gl.blend_equation(blend.color.equation) };
1601                            unsafe { gl.blend_func(blend.color.src, blend.color.dst) };
1602                        }
1603                    } else {
1604                        unsafe { gl.disable(glow::BLEND) };
1605                    }
1606                }
1607            }
1608            C::BindBuffer {
1609                target,
1610                slot,
1611                buffer,
1612                offset,
1613                size,
1614            } => {
1615                unsafe { gl.bind_buffer_range(target, slot, Some(buffer), offset, size) };
1616            }
1617            C::BindSampler(texture_index, sampler) => {
1618                unsafe { gl.bind_sampler(texture_index, sampler) };
1619            }
1620            C::BindTexture {
1621                slot,
1622                texture,
1623                target,
1624                aspects,
1625                ref mip_levels,
1626            } => {
1627                unsafe { gl.active_texture(glow::TEXTURE0 + slot) };
1628                unsafe { gl.bind_texture(target, Some(texture)) };
1629
1630                unsafe {
1631                    gl.tex_parameter_i32(target, glow::TEXTURE_BASE_LEVEL, mip_levels.start as i32)
1632                };
1633                unsafe {
1634                    gl.tex_parameter_i32(
1635                        target,
1636                        glow::TEXTURE_MAX_LEVEL,
1637                        (mip_levels.end - 1) as i32,
1638                    )
1639                };
1640
1641                let version = gl.version();
1642                let is_min_es_3_1 = version.is_embedded && (version.major, version.minor) >= (3, 1);
1643                let is_min_4_3 = !version.is_embedded && (version.major, version.minor) >= (4, 3);
1644                if is_min_es_3_1 || is_min_4_3 {
1645                    let mode = match aspects {
1646                        crate::FormatAspects::DEPTH => Some(glow::DEPTH_COMPONENT),
1647                        crate::FormatAspects::STENCIL => Some(glow::STENCIL_INDEX),
1648                        _ => None,
1649                    };
1650                    if let Some(mode) = mode {
1651                        unsafe {
1652                            gl.tex_parameter_i32(
1653                                target,
1654                                glow::DEPTH_STENCIL_TEXTURE_MODE,
1655                                mode as _,
1656                            )
1657                        };
1658                    }
1659                }
1660            }
1661            C::BindImage { slot, ref binding } => {
1662                unsafe {
1663                    gl.bind_image_texture(
1664                        slot,
1665                        Some(binding.raw),
1666                        binding.mip_level as i32,
1667                        binding.array_layer.is_none(),
1668                        binding.array_layer.unwrap_or_default() as i32,
1669                        binding.access,
1670                        binding.format,
1671                    )
1672                };
1673            }
1674            C::InsertDebugMarker(ref range) => {
1675                let marker = extract_marker(data_bytes, range);
1676                unsafe {
1677                    if self
1678                        .shared
1679                        .private_caps
1680                        .contains(PrivateCapabilities::DEBUG_FNS)
1681                    {
1682                        gl.debug_message_insert(
1683                            glow::DEBUG_SOURCE_APPLICATION,
1684                            glow::DEBUG_TYPE_MARKER,
1685                            DEBUG_ID,
1686                            glow::DEBUG_SEVERITY_NOTIFICATION,
1687                            to_debug_str(marker),
1688                        )
1689                    }
1690                };
1691            }
1692            C::PushDebugGroup(ref range) => {
1693                let marker = extract_marker(data_bytes, range);
1694                unsafe {
1695                    if self
1696                        .shared
1697                        .private_caps
1698                        .contains(PrivateCapabilities::DEBUG_FNS)
1699                    {
1700                        gl.push_debug_group(
1701                            glow::DEBUG_SOURCE_APPLICATION,
1702                            DEBUG_ID,
1703                            to_debug_str(marker),
1704                        )
1705                    }
1706                };
1707            }
1708            C::PopDebugGroup => {
1709                unsafe {
1710                    if self
1711                        .shared
1712                        .private_caps
1713                        .contains(PrivateCapabilities::DEBUG_FNS)
1714                    {
1715                        gl.pop_debug_group()
1716                    }
1717                };
1718            }
1719            C::SetImmediates {
1720                ref uniform,
1721                offset,
1722            } => {
1723                fn get_data<T, const COUNT: usize>(data: &[u8], offset: u32) -> [T; COUNT]
1724                where
1725                    [T; COUNT]: bytemuck::AnyBitPattern,
1726                {
1727                    let data_required = size_of::<T>() * COUNT;
1728                    let raw = &data[(offset as usize)..][..data_required];
1729                    bytemuck::pod_read_unaligned(raw)
1730                }
1731
1732                let location = Some(&uniform.location);
1733                use nt::glsl::{GlslScalar, GlslUniformType, GlslVectorSize};
1734                match uniform.ty {
1735                    //
1736                    // --- Float 1-4 Component ---
1737                    //
1738                    GlslUniformType::Scalar(GlslScalar::F32) => {
1739                        let data = get_data::<f32, 1>(data_bytes, offset)[0];
1740                        unsafe { gl.uniform_1_f32(location, data) };
1741                    }
1742                    GlslUniformType::Vector {
1743                        size: GlslVectorSize::Bi,
1744                        scalar: GlslScalar::F32,
1745                    } => {
1746                        let data = &get_data::<f32, 2>(data_bytes, offset);
1747                        unsafe { gl.uniform_2_f32_slice(location, data) };
1748                    }
1749                    GlslUniformType::Vector {
1750                        size: GlslVectorSize::Tri,
1751                        scalar: GlslScalar::F32,
1752                    } => {
1753                        let data = &get_data::<f32, 3>(data_bytes, offset);
1754                        unsafe { gl.uniform_3_f32_slice(location, data) };
1755                    }
1756                    GlslUniformType::Vector {
1757                        size: GlslVectorSize::Quad,
1758                        scalar: GlslScalar::F32,
1759                    } => {
1760                        let data = &get_data::<f32, 4>(data_bytes, offset);
1761                        unsafe { gl.uniform_4_f32_slice(location, data) };
1762                    }
1763
1764                    //
1765                    // --- Int 1-4 Component ---
1766                    //
1767                    GlslUniformType::Scalar(GlslScalar::I32) => {
1768                        let data = get_data::<i32, 1>(data_bytes, offset)[0];
1769                        unsafe { gl.uniform_1_i32(location, data) };
1770                    }
1771                    GlslUniformType::Vector {
1772                        size: GlslVectorSize::Bi,
1773                        scalar: GlslScalar::I32,
1774                    } => {
1775                        let data = &get_data::<i32, 2>(data_bytes, offset);
1776                        unsafe { gl.uniform_2_i32_slice(location, data) };
1777                    }
1778                    GlslUniformType::Vector {
1779                        size: GlslVectorSize::Tri,
1780                        scalar: GlslScalar::I32,
1781                    } => {
1782                        let data = &get_data::<i32, 3>(data_bytes, offset);
1783                        unsafe { gl.uniform_3_i32_slice(location, data) };
1784                    }
1785                    GlslUniformType::Vector {
1786                        size: GlslVectorSize::Quad,
1787                        scalar: GlslScalar::I32,
1788                    } => {
1789                        let data = &get_data::<i32, 4>(data_bytes, offset);
1790                        unsafe { gl.uniform_4_i32_slice(location, data) };
1791                    }
1792
1793                    //
1794                    // --- Uint 1-4 Component ---
1795                    //
1796                    GlslUniformType::Scalar(GlslScalar::U32) => {
1797                        let data = get_data::<u32, 1>(data_bytes, offset)[0];
1798                        unsafe { gl.uniform_1_u32(location, data) };
1799                    }
1800                    GlslUniformType::Vector {
1801                        size: GlslVectorSize::Bi,
1802                        scalar: GlslScalar::U32,
1803                    } => {
1804                        let data = &get_data::<u32, 2>(data_bytes, offset);
1805                        unsafe { gl.uniform_2_u32_slice(location, data) };
1806                    }
1807                    GlslUniformType::Vector {
1808                        size: GlslVectorSize::Tri,
1809                        scalar: GlslScalar::U32,
1810                    } => {
1811                        let data = &get_data::<u32, 3>(data_bytes, offset);
1812                        unsafe { gl.uniform_3_u32_slice(location, data) };
1813                    }
1814                    GlslUniformType::Vector {
1815                        size: GlslVectorSize::Quad,
1816                        scalar: GlslScalar::U32,
1817                    } => {
1818                        let data = &get_data::<u32, 4>(data_bytes, offset);
1819                        unsafe { gl.uniform_4_u32_slice(location, data) };
1820                    }
1821
1822                    //
1823                    // --- Matrix 2xR ---
1824                    //
1825                    GlslUniformType::Matrix {
1826                        columns: GlslVectorSize::Bi,
1827                        rows: GlslVectorSize::Bi,
1828                        scalar: GlslScalar::F32,
1829                    } => {
1830                        let data = &get_data::<f32, 4>(data_bytes, offset);
1831                        unsafe { gl.uniform_matrix_2_f32_slice(location, false, data) };
1832                    }
1833                    GlslUniformType::Matrix {
1834                        columns: GlslVectorSize::Bi,
1835                        rows: GlslVectorSize::Tri,
1836                        scalar: GlslScalar::F32,
1837                    } => {
1838                        // repack 2 vec3s into 6 values.
1839                        let unpacked_data = &get_data::<f32, 8>(data_bytes, offset);
1840                        #[rustfmt::skip]
1841                        let packed_data = [
1842                            unpacked_data[0], unpacked_data[1], unpacked_data[2],
1843                            unpacked_data[4], unpacked_data[5], unpacked_data[6],
1844                        ];
1845                        unsafe { gl.uniform_matrix_2x3_f32_slice(location, false, &packed_data) };
1846                    }
1847                    GlslUniformType::Matrix {
1848                        columns: GlslVectorSize::Bi,
1849                        rows: GlslVectorSize::Quad,
1850                        scalar: GlslScalar::F32,
1851                    } => {
1852                        let data = &get_data::<f32, 8>(data_bytes, offset);
1853                        unsafe { gl.uniform_matrix_2x4_f32_slice(location, false, data) };
1854                    }
1855
1856                    //
1857                    // --- Matrix 3xR ---
1858                    //
1859                    GlslUniformType::Matrix {
1860                        columns: GlslVectorSize::Tri,
1861                        rows: GlslVectorSize::Bi,
1862                        scalar: GlslScalar::F32,
1863                    } => {
1864                        let data = &get_data::<f32, 6>(data_bytes, offset);
1865                        unsafe { gl.uniform_matrix_3x2_f32_slice(location, false, data) };
1866                    }
1867                    GlslUniformType::Matrix {
1868                        columns: GlslVectorSize::Tri,
1869                        rows: GlslVectorSize::Tri,
1870                        scalar: GlslScalar::F32,
1871                    } => {
1872                        // repack 3 vec3s into 9 values.
1873                        let unpacked_data = &get_data::<f32, 12>(data_bytes, offset);
1874                        #[rustfmt::skip]
1875                        let packed_data = [
1876                            unpacked_data[0], unpacked_data[1], unpacked_data[2],
1877                            unpacked_data[4], unpacked_data[5], unpacked_data[6],
1878                            unpacked_data[8], unpacked_data[9], unpacked_data[10],
1879                        ];
1880                        unsafe { gl.uniform_matrix_3_f32_slice(location, false, &packed_data) };
1881                    }
1882                    GlslUniformType::Matrix {
1883                        columns: GlslVectorSize::Tri,
1884                        rows: GlslVectorSize::Quad,
1885                        scalar: GlslScalar::F32,
1886                    } => {
1887                        let data = &get_data::<f32, 12>(data_bytes, offset);
1888                        unsafe { gl.uniform_matrix_3x4_f32_slice(location, false, data) };
1889                    }
1890
1891                    //
1892                    // --- Matrix 4xR ---
1893                    //
1894                    GlslUniformType::Matrix {
1895                        columns: GlslVectorSize::Quad,
1896                        rows: GlslVectorSize::Bi,
1897                        scalar: GlslScalar::F32,
1898                    } => {
1899                        let data = &get_data::<f32, 8>(data_bytes, offset);
1900                        unsafe { gl.uniform_matrix_4x2_f32_slice(location, false, data) };
1901                    }
1902                    GlslUniformType::Matrix {
1903                        columns: GlslVectorSize::Quad,
1904                        rows: GlslVectorSize::Tri,
1905                        scalar: GlslScalar::F32,
1906                    } => {
1907                        // repack 4 vec3s into 12 values.
1908                        let unpacked_data = &get_data::<f32, 16>(data_bytes, offset);
1909                        #[rustfmt::skip]
1910                        let packed_data = [
1911                            unpacked_data[0], unpacked_data[1], unpacked_data[2],
1912                            unpacked_data[4], unpacked_data[5], unpacked_data[6],
1913                            unpacked_data[8], unpacked_data[9], unpacked_data[10],
1914                            unpacked_data[12], unpacked_data[13], unpacked_data[14],
1915                        ];
1916                        unsafe { gl.uniform_matrix_4x3_f32_slice(location, false, &packed_data) };
1917                    }
1918                    GlslUniformType::Matrix {
1919                        columns: GlslVectorSize::Quad,
1920                        rows: GlslVectorSize::Quad,
1921                        scalar: GlslScalar::F32,
1922                    } => {
1923                        let data = &get_data::<f32, 16>(data_bytes, offset);
1924                        unsafe { gl.uniform_matrix_4_f32_slice(location, false, data) };
1925                    }
1926                    _ => panic!("Unsupported uniform datatype: {:?}!", uniform.ty),
1927                }
1928            }
1929            C::SetClipDistances {
1930                old_count,
1931                new_count,
1932            } => {
1933                // Disable clip planes that are no longer active
1934                for i in new_count..old_count {
1935                    unsafe { gl.disable(glow::CLIP_DISTANCE0 + i) };
1936                }
1937
1938                // Enable clip planes that are now active
1939                for i in old_count..new_count {
1940                    unsafe { gl.enable(glow::CLIP_DISTANCE0 + i) };
1941                }
1942            }
1943        }
1944    }
1945}
1946
1947impl crate::Queue for super::Queue {
1948    type A = super::Api;
1949
1950    unsafe fn submit(
1951        &self,
1952        command_buffers: &[&super::CommandBuffer],
1953        _surface_textures: &[&super::Texture],
1954        (signal_fence, signal_value): (&super::Fence, crate::FenceValue),
1955    ) -> Result<(), crate::DeviceError> {
1956        let shared = Arc::clone(&self.shared);
1957        let gl = &shared.context.lock();
1958        for cmd_buf in command_buffers.iter() {
1959            // The command encoder assumes a default state when encoding the command buffer.
1960            // Always reset the state between command_buffers to reflect this assumption. Do
1961            // this at the beginning of the loop in case something outside of wgpu modified
1962            // this state prior to commit.
1963            //
1964            // An empty command buffer executes no commands, so it neither depends on nor
1965            // changes any GL state; the next non-empty command buffer still resets the
1966            // state before running its own commands.
1967            if !cmd_buf.commands.is_empty() {
1968                unsafe { self.reset_state(gl) };
1969            }
1970            if let Some(ref label) = cmd_buf.label {
1971                if self
1972                    .shared
1973                    .private_caps
1974                    .contains(PrivateCapabilities::DEBUG_FNS)
1975                {
1976                    unsafe {
1977                        gl.push_debug_group(
1978                            glow::DEBUG_SOURCE_APPLICATION,
1979                            DEBUG_ID,
1980                            to_debug_str(label),
1981                        )
1982                    };
1983                }
1984            }
1985
1986            for command in cmd_buf.commands.iter() {
1987                unsafe { self.process(gl, command, &cmd_buf.data_bytes, &cmd_buf.queries) };
1988            }
1989
1990            if cmd_buf.label.is_some()
1991                && self
1992                    .shared
1993                    .private_caps
1994                    .contains(PrivateCapabilities::DEBUG_FNS)
1995            {
1996                unsafe { gl.pop_debug_group() };
1997            }
1998        }
1999
2000        signal_fence.maintain(gl);
2001        signal_fence.signal(gl, signal_value)?;
2002
2003        // This is extremely important. If we don't flush, the above fences may never
2004        // be signaled, particularly in headless contexts. Headed contexts will
2005        // often flush every so often, but headless contexts may not.
2006        unsafe { gl.flush() };
2007
2008        Ok(())
2009    }
2010
2011    unsafe fn present(
2012        &self,
2013        surface: &super::Surface,
2014        texture: super::Texture,
2015        damage_rects: &[wgt::Rect<u32>],
2016    ) -> Result<(), crate::SurfaceError> {
2017        unsafe { surface.present(texture, &self.shared.context, damage_rects) }
2018    }
2019
2020    unsafe fn get_timestamp_period(&self) -> f32 {
2021        1.0
2022    }
2023
2024    unsafe fn wait_for_idle(&self) -> Result<(), crate::DeviceError> {
2025        let gl = &self.shared.context.lock();
2026        unsafe { gl.finish() };
2027        Ok(())
2028    }
2029}
2030
2031#[cfg(send_sync)]
2032unsafe impl Sync for super::Queue {}
2033#[cfg(send_sync)]
2034unsafe impl Send for super::Queue {}