Skip to main content

wgpu_hal/gles/
device.rs

1use alloc::{
2    borrow::{Cow, ToOwned},
3    boxed::Box,
4    format,
5    string::String,
6    string::ToString as _,
7    sync::Arc,
8    vec,
9    vec::Vec,
10};
11use core::{
12    cmp::max,
13    convert::TryInto,
14    num::NonZeroU32,
15    ptr,
16    sync::atomic::{AtomicUsize, Ordering},
17};
18
19use arrayvec::ArrayVec;
20use glow::HasContext;
21use naga::FastHashMap;
22use parking_lot::MutexGuard;
23
24use super::{conv, lock, MaybeMutex, PrivateCapabilities};
25use crate::auxil::map_naga_stage;
26use crate::TlasInstance;
27
28type ShaderStage<'a> = (
29    naga::ShaderStage,
30    &'a crate::ProgrammableStage<'a, super::ShaderModule>,
31);
32type NameBindingMap = FastHashMap<String, (super::BindingRegister, u8)>;
33
34struct CompilationContext<'a> {
35    layout: &'a super::PipelineLayout,
36    sampler_map: &'a mut super::SamplerBindMap,
37    name_binding_map: &'a mut NameBindingMap,
38    immediates_items: &'a mut Vec<naga::back::glsl::ImmediateItem>,
39    multiview_mask: Option<NonZeroU32>,
40    clip_distance_count: &'a mut u32,
41    /// Whether the vertex stage writes `gl_PointSize`: only a pipeline that
42    /// draws points needs it, and WebGL counts it against the varyings.
43    force_point_size: bool,
44}
45
46impl CompilationContext<'_> {
47    fn consume_reflection(
48        self,
49        gl: &glow::Context,
50        module: &naga::Module,
51        ep_info: &naga::valid::FunctionInfo,
52        reflection_info: naga::back::glsl::ReflectionInfo,
53        naga_stage: naga::ShaderStage,
54        program: glow::Program,
55    ) {
56        for (handle, var) in module.global_variables.iter() {
57            if ep_info[handle].is_empty() {
58                continue;
59            }
60            let register = match var.space {
61                naga::AddressSpace::Uniform => super::BindingRegister::UniformBuffers,
62                naga::AddressSpace::Storage { .. } => super::BindingRegister::StorageBuffers,
63                _ => continue,
64            };
65
66            let br = var.binding.as_ref().unwrap();
67            let slot = self.layout.get_slot(br);
68
69            let name = match reflection_info.uniforms.get(&handle) {
70                Some(name) => name.clone(),
71                None => continue,
72            };
73            log::trace!(
74                "Rebind buffer: {:?} -> {}, register={:?}, slot={}",
75                var.name.as_ref(),
76                &name,
77                register,
78                slot
79            );
80            self.name_binding_map.insert(name, (register, slot));
81        }
82
83        for (name, mapping) in reflection_info.texture_mapping {
84            let var = &module.global_variables[mapping.texture];
85            let register = match module.types[var.ty].inner {
86                naga::TypeInner::Image {
87                    class: naga::ImageClass::Storage { .. },
88                    ..
89                } => super::BindingRegister::Images,
90                _ => super::BindingRegister::Textures,
91            };
92
93            let tex_br = var.binding.as_ref().unwrap();
94            let texture_linear_index = self.layout.get_slot(tex_br);
95
96            self.name_binding_map
97                .insert(name, (register, texture_linear_index));
98            if let Some(sampler_handle) = mapping.sampler {
99                let sam_br = module.global_variables[sampler_handle]
100                    .binding
101                    .as_ref()
102                    .unwrap();
103                let sampler_linear_index = self.layout.get_slot(sam_br);
104                self.sampler_map[texture_linear_index as usize] = Some(sampler_linear_index);
105            }
106        }
107
108        for (name, location) in reflection_info.varying {
109            assert_eq!(location.index, 0);
110            bind_varying(gl, program, naga_stage, &name, location.location);
111        }
112
113        *self.immediates_items = reflection_info.immediates_items;
114
115        if naga_stage == naga::ShaderStage::Vertex {
116            *self.clip_distance_count = reflection_info.clip_distance_count;
117        }
118    }
119
120    /// Binds the names a template's GLSL gives its resources, as
121    /// `consume_reflection` binds those of GLSL naga writes.
122    fn consume_template(
123        self,
124        gl: &glow::Context,
125        template: &crate::GlslTemplate,
126        program: glow::Program,
127    ) {
128        for block in template.blocks {
129            let register = if block.storage {
130                super::BindingRegister::StorageBuffers
131            } else {
132                super::BindingRegister::UniformBuffers
133            };
134            let slot = self.layout.get_slot(&block.binding);
135            self.name_binding_map
136                .insert(block.name.to_owned(), (register, slot));
137        }
138        for texture in template.textures {
139            let register = if texture.storage {
140                super::BindingRegister::Images
141            } else {
142                super::BindingRegister::Textures
143            };
144            let texture_linear_index = self.layout.get_slot(&texture.binding);
145            self.name_binding_map
146                .insert(texture.name.to_owned(), (register, texture_linear_index));
147            if let Some(sampler) = texture.sampler {
148                self.sampler_map[texture_linear_index as usize] =
149                    Some(self.layout.get_slot(&sampler));
150            }
151        }
152        for &(name, location) in template.varyings {
153            bind_varying(gl, program, template.stage, name, location);
154        }
155        if template.stage == naga::ShaderStage::Vertex {
156            *self.clip_distance_count = template.clip_distance_count;
157        }
158    }
159}
160
161/// Binds a vertex input or fragment output `name` to its `location`.
162fn bind_varying(
163    gl: &glow::Context,
164    program: glow::Program,
165    naga_stage: naga::ShaderStage,
166    name: &str,
167    location: u32,
168) {
169    match naga_stage {
170        naga::ShaderStage::Vertex => unsafe { gl.bind_attrib_location(program, location, name) },
171        naga::ShaderStage::Fragment => unsafe {
172            gl.bind_frag_data_location(program, location, name)
173        },
174        naga::ShaderStage::Compute => {}
175        naga::ShaderStage::Task
176        | naga::ShaderStage::Mesh
177        | naga::ShaderStage::RayGeneration
178        | naga::ShaderStage::AnyHit
179        | naga::ShaderStage::ClosestHit
180        | naga::ShaderStage::Miss => unreachable!(),
181    }
182}
183
184impl super::Device {
185    /// Reads the link of every program whose link no submission read yet,
186    /// and returns whether there was one. Each read waits for the GPU to
187    /// reach that program's link; called once it has, between frames, the
188    /// reads cost only their replies there instead of in the first frame
189    /// that draws with the programs. In Chrome on a Huawei Mate 20 X that
190    /// frame read 22 links built ahead of it in 113 ms.
191    pub fn read_program_links(&self) -> bool {
192        if self.shared.unread_links.load(Ordering::Relaxed) == 0 {
193            return false;
194        }
195        let gl = &self.shared.context.lock();
196        for program in self.shared.program_cache.lock().values().flatten() {
197            // SAFETY: the context is current while `gl` holds its lock.
198            drop(unsafe { program.read_link(gl, &self.shared.unread_links) });
199        }
200        true
201    }
202
203    /// # Safety
204    ///
205    /// - `name` must be created respecting `desc`
206    /// - `name` must be a texture
207    /// - If `drop_callback` is [`None`], wgpu-hal will take ownership of the texture. If
208    ///   `drop_callback` is [`Some`], the texture must be valid until the callback is called.
209    #[cfg(any(native, Emscripten))]
210    pub unsafe fn texture_from_raw(
211        &self,
212        name: NonZeroU32,
213        desc: &crate::TextureDescriptor,
214        drop_callback: Option<crate::DropCallback>,
215    ) -> super::Texture {
216        super::Texture {
217            inner: super::TextureInner::Texture {
218                raw: glow::NativeTexture(name),
219                target: super::Texture::get_info_from_desc(desc),
220            },
221            drop_guard: crate::DropGuard::from_option(drop_callback),
222            mip_level_count: desc.mip_level_count,
223            array_layer_count: desc.array_layer_count(),
224            format: desc.format,
225            format_desc: self.shared.describe_texture_format(desc.format),
226            copy_size: desc.copy_extent(),
227        }
228    }
229
230    /// # Safety
231    ///
232    /// - `name` must be created respecting `desc`
233    /// - `name` must be a renderbuffer
234    /// - If `drop_callback` is [`None`], wgpu-hal will take ownership of the renderbuffer. If
235    ///   `drop_callback` is [`Some`], the renderbuffer must be valid until the callback is called.
236    #[cfg(any(native, Emscripten))]
237    pub unsafe fn texture_from_raw_renderbuffer(
238        &self,
239        name: NonZeroU32,
240        desc: &crate::TextureDescriptor,
241        drop_callback: Option<crate::DropCallback>,
242    ) -> super::Texture {
243        super::Texture {
244            inner: super::TextureInner::Renderbuffer {
245                raw: glow::NativeRenderbuffer(name),
246            },
247            drop_guard: crate::DropGuard::from_option(drop_callback),
248            mip_level_count: desc.mip_level_count,
249            array_layer_count: desc.array_layer_count(),
250            format: desc.format,
251            format_desc: self.shared.describe_texture_format(desc.format),
252            copy_size: desc.copy_extent(),
253        }
254    }
255
256    /// # Safety
257    ///
258    /// - `name` must be a non-zero GL buffer name created respecting `desc`.
259    /// - The buffer's storage size must be at least `desc.size`.
260    /// - If `desc.usage` includes [`BufferUses::MAP_READ`](wgt::BufferUses::MAP_READ) or
261    ///   [`BufferUses::MAP_WRITE`](wgt::BufferUses::MAP_WRITE), the GL buffer must have
262    ///   been allocated with `glBufferStorage` (or equivalent) using flags compatible
263    ///   with persistent mapping (`GL_MAP_PERSISTENT_BIT` plus matching read/write/coherent
264    ///   bits). Buffers created with the legacy `glBufferData` family cannot be mapped
265    ///   through this path.
266    /// - If `drop_callback` is [`None`], wgpu-hal will take ownership of the buffer and
267    ///   call `glDeleteBuffers` on it. If `drop_callback` is [`Some`], the buffer must
268    ///   remain valid until the callback is invoked.
269    #[cfg(any(native, Emscripten))]
270    pub unsafe fn buffer_from_raw(
271        &self,
272        name: NonZeroU32,
273        desc: &crate::BufferDescriptor,
274        drop_callback: Option<crate::DropCallback>,
275    ) -> super::Buffer {
276        let target = if desc.usage.contains(wgt::BufferUses::INDEX) {
277            glow::ELEMENT_ARRAY_BUFFER
278        } else {
279            glow::ARRAY_BUFFER
280        };
281
282        let is_host_visible = desc
283            .usage
284            .intersects(wgt::BufferUses::MAP_READ | wgt::BufferUses::MAP_WRITE);
285        let is_coherent = desc
286            .memory_flags
287            .contains(crate::MemoryFlags::PREFER_COHERENT);
288
289        let mut map_flags = 0;
290        if desc.usage.contains(wgt::BufferUses::MAP_READ) {
291            map_flags |= glow::MAP_READ_BIT;
292        }
293        if desc.usage.contains(wgt::BufferUses::MAP_WRITE) {
294            map_flags |= glow::MAP_WRITE_BIT;
295        }
296        if is_host_visible {
297            map_flags |= glow::MAP_PERSISTENT_BIT;
298            if is_coherent {
299                map_flags |= glow::MAP_COHERENT_BIT;
300            }
301        }
302        if !is_coherent && desc.usage.contains(wgt::BufferUses::MAP_WRITE) {
303            map_flags |= glow::MAP_FLUSH_EXPLICIT_BIT;
304        }
305
306        self.counters.buffers.add(1);
307
308        super::Buffer {
309            raw: Some(glow::NativeBuffer(name)),
310            target,
311            size: desc.size,
312            map_flags,
313            map_state: Arc::new(MaybeMutex::new(super::BufferMapState {
314                mapped: false,
315                data: None,
316                offset_of_current_mapping: 0,
317            })),
318            drop_guard: crate::DropGuard::from_option(drop_callback).map(Arc::new),
319        }
320    }
321
322    unsafe fn compile_shader(
323        gl: &glow::Context,
324        shader: &str,
325        naga_stage: naga::ShaderStage,
326        #[cfg_attr(target_arch = "wasm32", allow(unused))] label: Option<&str>,
327    ) -> Result<(glow::Shader, naga::ShaderStage), crate::PipelineError> {
328        let target = match naga_stage {
329            naga::ShaderStage::Vertex => glow::VERTEX_SHADER,
330            naga::ShaderStage::Fragment => glow::FRAGMENT_SHADER,
331            naga::ShaderStage::Compute => glow::COMPUTE_SHADER,
332            naga::ShaderStage::Task
333            | naga::ShaderStage::Mesh
334            | naga::ShaderStage::RayGeneration
335            | naga::ShaderStage::AnyHit
336            | naga::ShaderStage::ClosestHit
337            | naga::ShaderStage::Miss => unreachable!(),
338        };
339
340        let raw = unsafe { gl.create_shader(target) }.unwrap();
341        #[cfg(native)]
342        if gl.supports_debug() {
343            let name = raw.0.get();
344            unsafe { gl.object_label(glow::SHADER, name, label) };
345        }
346
347        unsafe { gl.shader_source(raw, shader) };
348        unsafe { gl.compile_shader(raw) };
349
350        log::debug!("\tCompiled shader {raw:?}");
351
352        // The status is read only when the program fails to link (see
353        // `link_failure`): on the web each read waits for a round trip to
354        // the GPU process.
355        Ok((raw, naga_stage))
356    }
357
358    /// Why `program`, built from `shaders`, failed to link: the first shader
359    /// that failed to compile, or the program's own log.
360    unsafe fn link_failure(
361        gl: &glow::Context,
362        program: glow::Program,
363        shaders: &[(glow::Shader, naga::ShaderStage)],
364        has_stages: wgt::ShaderStages,
365    ) -> crate::PipelineError {
366        for &(shader, naga_stage) in shaders {
367            if !unsafe { gl.get_shader_compile_status(shader) } {
368                let msg = unsafe { gl.get_shader_info_log(shader) };
369                log::error!("\tShader compilation failed: {msg}");
370                return crate::PipelineError::Linkage(map_naga_stage(naga_stage), msg);
371            }
372        }
373        crate::PipelineError::Linkage(has_stages, unsafe { gl.get_program_info_log(program) })
374    }
375
376    fn create_shader(
377        gl: &glow::Context,
378        naga_stage: naga::ShaderStage,
379        stage: &crate::ProgrammableStage<super::ShaderModule>,
380        context: CompilationContext,
381        program: glow::Program,
382    ) -> Result<(glow::Shader, naga::ShaderStage), crate::PipelineError> {
383        let source = 'outer: {
384            use naga::back::glsl;
385            let pipeline_options = glsl::PipelineOptions {
386                shader_stage: naga_stage,
387                entry_point: stage.entry_point.to_owned(),
388                multiview: context
389                    .multiview_mask
390                    .map(|a| NonZeroU32::new(a.get().count_ones()).unwrap()),
391            };
392
393            let naga = match stage.module.source {
394                super::ShaderModuleSource::Naga(ref naga) => naga,
395                super::ShaderModuleSource::Passthrough { ref source } => {
396                    break 'outer Cow::Borrowed(source);
397                }
398                super::ShaderModuleSource::Templates(templates) => {
399                    let template = templates
400                        .iter()
401                        .find(|template| {
402                            template.stage == naga_stage
403                                && template.entry_point == stage.entry_point
404                        })
405                        .ok_or(crate::PipelineError::EntryPoint(naga_stage))?;
406                    let stages = map_naga_stage(naga_stage);
407                    if context.multiview_mask.is_some()
408                        || (context.force_point_size && naga_stage == naga::ShaderStage::Vertex)
409                    {
410                        return Err(crate::PipelineError::Linkage(
411                            stages,
412                            "a GLSL template neither draws multiview nor writes the point size"
413                                .to_owned(),
414                        ));
415                    }
416                    let glsl = template
417                        .expand(stage.constants)
418                        .map_err(|msg| crate::PipelineError::PipelineConstants(stages, msg))?;
419                    context.consume_template(gl, template, program);
420                    break 'outer Cow::Owned(glsl);
421                }
422            };
423
424            Cow::Owned(Self::naga_glsl(
425                gl,
426                naga,
427                (naga_stage, stage),
428                context,
429                program,
430                &pipeline_options,
431            )?)
432        };
433
434        unsafe { Self::compile_shader(gl, &source, naga_stage, stage.module.label.as_deref()) }
435    }
436
437    /// The GLSL naga writes for a stage of a module it holds as IR. The
438    /// built-in shaders take GLSL templates, so this stays out of
439    /// [`Self::create_shader`]: a WebAssembly page compiles a function whole at
440    /// its first call, and every pipeline's first call would compile naga's
441    /// writer with it.
442    #[inline(never)]
443    fn naga_glsl(
444        gl: &glow::Context,
445        naga: &crate::NagaShader,
446        (naga_stage, stage): (naga::ShaderStage, &crate::ProgrammableStage<super::ShaderModule>),
447        context: CompilationContext,
448        program: glow::Program,
449        pipeline_options: &naga::back::glsl::PipelineOptions,
450    ) -> Result<String, crate::PipelineError> {
451        use naga::back::glsl;
452        let (module, info) = naga::back::pipeline_constants::process_overrides(
453            &naga.module,
454            &naga.info,
455            Some((naga_stage, stage.entry_point)),
456            stage.constants,
457        )
458        .map_err(|e| {
459            let msg = format!("{e}");
460            crate::PipelineError::PipelineConstants(map_naga_stage(naga_stage), msg)
461        })?;
462        let (module, info) = crate::auxil::prune::prune_decided_branches(module, info)
463            .map_err(|msg| {
464                crate::PipelineError::PipelineConstants(map_naga_stage(naga_stage), msg)
465            })?;
466
467        let entry_point_index = module
468            .entry_points
469            .iter()
470            .position(|ep| ep.name.as_str() == stage.entry_point)
471            .ok_or(crate::PipelineError::EntryPoint(naga_stage))?;
472
473        use naga::proc::BoundsCheckPolicy;
474        // The image bounds checks require the TEXTURE_LEVELS feature available in GL core 4.3+.
475        let version = gl.version();
476        let image_check = if !version.is_embedded && (version.major, version.minor) >= (4, 3) {
477            BoundsCheckPolicy::ReadZeroSkipWrite
478        } else {
479            BoundsCheckPolicy::Unchecked
480        };
481
482        // Other bounds check are either provided by glsl or not implemented yet.
483        let policies = naga::proc::BoundsCheckPolicies {
484            index: BoundsCheckPolicy::Unchecked,
485            buffer: BoundsCheckPolicy::Unchecked,
486            image_load: image_check,
487            binding_array: BoundsCheckPolicy::Unchecked,
488        };
489
490        let mut output = String::new();
491        let force_point_size =
492            context.force_point_size && naga_stage == naga::ShaderStage::Vertex;
493        let needs_temp_options = stage.zero_initialize_workgroup_memory
494            != context.layout.naga_options.zero_initialize_workgroup_memory
495            || force_point_size;
496        let mut temp_options;
497        let naga_options = if needs_temp_options {
498            // We use a conditional here, as cloning the naga_options could be expensive
499            // That is, we want to avoid doing that unless we cannot avoid it
500            temp_options = context.layout.naga_options.clone();
501            temp_options.zero_initialize_workgroup_memory =
502                stage.zero_initialize_workgroup_memory;
503            temp_options
504                .writer_flags
505                .set(glsl::WriterFlags::FORCE_POINT_SIZE, force_point_size);
506            &temp_options
507        } else {
508            &context.layout.naga_options
509        };
510        let mut writer = glsl::Writer::new(
511            &mut output,
512            &module,
513            &info,
514            naga_options,
515            pipeline_options,
516            policies,
517        )
518        .map_err(|e| {
519            let msg = format!("{e}");
520            crate::PipelineError::Linkage(map_naga_stage(naga_stage), msg)
521        })?;
522
523        let reflection_info = writer.write().map_err(|e| {
524            let msg = format!("{e}");
525            crate::PipelineError::Linkage(map_naga_stage(naga_stage), msg)
526        })?;
527
528        log::debug!("Naga generated shader:\n{output}");
529
530        context.consume_reflection(
531            gl,
532            &module,
533            info.get_entry_point(entry_point_index),
534            reflection_info,
535            naga_stage,
536            program,
537        );
538        Ok(output)
539    }
540
541    unsafe fn create_pipeline<'a>(
542        &self,
543        gl: &glow::Context,
544        shaders: ArrayVec<ShaderStage<'a>, { crate::MAX_CONCURRENT_SHADER_STAGES }>,
545        layout: &super::PipelineLayout,
546        #[cfg_attr(target_arch = "wasm32", allow(unused))] label: Option<&str>,
547        multiview_mask: Option<NonZeroU32>,
548        force_point_size: bool,
549    ) -> Result<Arc<super::PipelineInner>, crate::PipelineError> {
550        let mut program_stages = ArrayVec::new();
551        let group_to_binding_to_slot = layout
552            .group_infos
553            .iter()
554            .map(|group| group.as_ref().map(|group| group.binding_to_slot.clone()))
555            .collect::<Vec<_>>();
556        for &(naga_stage, stage) in &shaders {
557            program_stages.push(super::ProgramStage {
558                naga_stage: naga_stage.to_owned(),
559                shader_id: stage.module.id,
560                entry_point: stage.entry_point.to_owned(),
561                zero_initialize_workgroup_memory: stage.zero_initialize_workgroup_memory,
562                constant_hash: Self::create_constant_hash(stage),
563            });
564        }
565        let mut guard = self
566            .shared
567            .program_cache
568            .try_lock()
569            .expect("Couldn't acquire program_cache lock");
570        // This guard ensures that we can't accidentally destroy a program whilst we're about to reuse it
571        // The only place that destroys a pipeline is also locking on `program_cache`
572        let program = guard
573            .entry(super::ProgramCacheKey {
574                stages: program_stages,
575                group_to_binding_to_slot: group_to_binding_to_slot.into_boxed_slice(),
576                force_point_size,
577            })
578            .or_insert_with(|| {
579                let created = unsafe {
580                    Self::create_program(
581                        gl,
582                        shaders,
583                        layout,
584                        label,
585                        multiview_mask,
586                        force_point_size,
587                        self.shared.shading_language_version,
588                        self.shared.private_caps,
589                    )
590                };
591                if created.is_ok() {
592                    self.shared.unread_links.fetch_add(1, Ordering::Relaxed);
593                }
594                created
595            })
596            .to_owned()?;
597        drop(guard);
598
599        Ok(program)
600    }
601
602    fn create_constant_hash(stage: &crate::ProgrammableStage<super::ShaderModule>) -> Vec<u8> {
603        let mut buf: Vec<u8> = Vec::new();
604
605        for (key, value) in stage.constants.iter() {
606            buf.extend_from_slice(key.as_bytes());
607            buf.extend_from_slice(&value.to_ne_bytes());
608        }
609
610        buf
611    }
612
613    unsafe fn create_program<'a>(
614        gl: &glow::Context,
615        shaders: ArrayVec<ShaderStage<'a>, { crate::MAX_CONCURRENT_SHADER_STAGES }>,
616        layout: &super::PipelineLayout,
617        #[cfg_attr(target_arch = "wasm32", allow(unused))] label: Option<&str>,
618        multiview_mask: Option<NonZeroU32>,
619        force_point_size: bool,
620        glsl_version: naga::back::glsl::Version,
621        private_caps: PrivateCapabilities,
622    ) -> Result<Arc<super::PipelineInner>, crate::PipelineError> {
623        let glsl_version = match glsl_version {
624            naga::back::glsl::Version::Embedded { version, .. } => format!("{version} es"),
625            naga::back::glsl::Version::Desktop(version) => format!("{version}"),
626        };
627        let program = unsafe { gl.create_program() }.unwrap();
628        #[cfg(native)]
629        if let Some(label) = label {
630            if private_caps.contains(PrivateCapabilities::DEBUG_FNS) {
631                let name = program.0.get();
632                unsafe { gl.object_label(glow::PROGRAM, name, Some(label)) };
633            }
634        }
635
636        let mut name_binding_map = NameBindingMap::default();
637        let mut immediates_items = ArrayVec::<_, { crate::MAX_CONCURRENT_SHADER_STAGES }>::new();
638        let mut sampler_map = [None; super::MAX_TEXTURE_SLOTS];
639        let mut has_stages = wgt::ShaderStages::empty();
640        let mut shaders_to_delete = ArrayVec::<_, { crate::MAX_CONCURRENT_SHADER_STAGES }>::new();
641        let mut clip_distance_count = 0;
642
643        for &(naga_stage, stage) in &shaders {
644            has_stages |= map_naga_stage(naga_stage);
645            let pc_item = {
646                immediates_items.push(Vec::new());
647                immediates_items.last_mut().unwrap()
648            };
649            let context = CompilationContext {
650                layout,
651                sampler_map: &mut sampler_map,
652                name_binding_map: &mut name_binding_map,
653                immediates_items: pc_item,
654                multiview_mask,
655                clip_distance_count: &mut clip_distance_count,
656                force_point_size,
657            };
658
659            let shader = Self::create_shader(gl, naga_stage, stage, context, program)?;
660            shaders_to_delete.push(shader);
661        }
662
663        // Create empty fragment shader if only vertex shader is present
664        if has_stages == wgt::ShaderStages::VERTEX {
665            let shader_src = format!("#version {glsl_version}\n void main(void) {{}}",);
666            log::debug!("Only vertex shader is present. Creating an empty fragment shader",);
667            let shader = unsafe {
668                Self::compile_shader(
669                    gl,
670                    &shader_src,
671                    naga::ShaderStage::Fragment,
672                    Some("(wgpu internal) dummy fragment shader"),
673                )
674            }?;
675            shaders_to_delete.push(shader);
676        }
677
678        for &(shader, _) in shaders_to_delete.iter() {
679            unsafe { gl.attach_shader(program, shader) };
680        }
681        unsafe { gl.link_program(program) };
682        // WebGL keeps commands in the page's buffer until a flush or a read
683        // sends them: without one the GPU process starts this link only when
684        // the queue reads it.
685        #[cfg(webgl)]
686        unsafe {
687            gl.flush()
688        };
689
690        log::debug!("\tLinked program {program:?}");
691
692        // Nothing is read back here: on the web each read waits for the GPU
693        // process to compile and link the program, which it does one
694        // program at a time. The queue reads the link at the program's
695        // first use, so the caller records and submits work meanwhile.
696        Ok(Arc::new(super::PipelineInner {
697            program,
698            sampler_map,
699            clip_distance_count,
700            link: parking_lot::Mutex::new(super::ProgramLink {
701                pending: Some(Box::new(PendingLink {
702                    shaders: shaders_to_delete,
703                    has_stages,
704                    name_binding_map,
705                    immediates_items,
706                    remap_bindings: !private_caps
707                        .contains(PrivateCapabilities::SHADER_BINDING_LAYOUT),
708                })),
709                linked: None,
710            }),
711        }))
712    }
713}
714
715/// What reading a program's link needs: its shaders, kept until then for
716/// the log of one that failed, and the names its uniforms are found by.
717#[derive(Debug)]
718pub(super) struct PendingLink {
719    shaders: ArrayVec<(glow::Shader, naga::ShaderStage), { crate::MAX_CONCURRENT_SHADER_STAGES }>,
720    has_stages: wgt::ShaderStages,
721    name_binding_map: NameBindingMap,
722    immediates_items:
723        ArrayVec<Vec<naga::back::glsl::ImmediateItem>, { crate::MAX_CONCURRENT_SHADER_STAGES }>,
724    /// Whether the program's bindings are set here: GLSL ES 3.00 cannot
725    /// name them in the shader.
726    remap_bindings: bool,
727}
728
729impl PendingLink {
730    /// Reads `program`'s link and sets the bindings the shader could not
731    /// name. A program that failed to link logs why and sets nothing; its
732    /// draws then fail as GL fails them.
733    unsafe fn finish(self, gl: &glow::Context, program: glow::Program) -> super::LinkedProgram {
734        let linked_ok = unsafe { gl.get_program_link_status(program) };
735        let failure = (!linked_ok).then(|| unsafe {
736            super::Device::link_failure(gl, program, &self.shaders, self.has_stages)
737        });
738        unsafe { self.delete_shaders(gl) };
739        if let Some(error) = failure {
740            log::error!("A render pipeline's program did not link: {error}");
741            return super::LinkedProgram::default();
742        }
743        if log::log_enabled!(log::Level::Debug) {
744            let msg = unsafe { gl.get_program_info_log(program) };
745            if !msg.is_empty() {
746                log::debug!("\tLink message: {msg}");
747            }
748        }
749
750        if self.remap_bindings {
751            // We can't remap storage buffers this way.
752            unsafe { gl.use_program(Some(program)) };
753            for (ref name, (register, slot)) in self.name_binding_map {
754                log::trace!("Get binding {name:?} from program {program:?}");
755                match register {
756                    super::BindingRegister::UniformBuffers => {
757                        match unsafe { gl.get_uniform_block_index(program, name) } {
758                            Some(index) => {
759                                log::trace!("\tBinding slot {slot} to block index {index}");
760                                unsafe { gl.uniform_block_binding(program, index, slot as _) };
761                            }
762                            None => log::error!("No uniform block {name} in program {program:?}"),
763                        }
764                    }
765                    super::BindingRegister::StorageBuffers => {
766                        log::error!("Unable to re-map shader storage block {name}");
767                    }
768                    super::BindingRegister::Textures | super::BindingRegister::Images => {
769                        let location = unsafe { gl.get_uniform_location(program, name) };
770                        unsafe { gl.uniform_1_i32(location.as_ref(), slot as _) };
771                    }
772                }
773            }
774        }
775
776        let mut immediates_descs = ArrayVec::new();
777        for item in self.immediates_items.iter().flatten() {
778            let location = unsafe { gl.get_uniform_location(program, &item.access_path) };
779
780            log::trace!(
781                "immediate data item: name={}, ty={:?}, offset={}, location={:?}",
782                item.access_path,
783                item.ty,
784                item.offset,
785                location,
786            );
787
788            if let Some(location) = location {
789                immediates_descs.push(super::ImmediateDesc {
790                    location,
791                    offset: item.offset,
792                    ty: item.ty,
793                });
794            }
795        }
796
797        let first_instance_location = if self.has_stages.contains(wgt::ShaderStages::VERTEX) {
798            // If this returns none (the uniform isn't active), that's fine, we just won't set it.
799            unsafe { gl.get_uniform_location(program, naga::back::glsl::FIRST_INSTANCE_BINDING) }
800        } else {
801            None
802        };
803
804        super::LinkedProgram {
805            first_instance_location,
806            immediates_descs,
807        }
808    }
809
810    unsafe fn delete_shaders(&self, gl: &glow::Context) {
811        for &(shader, _) in self.shaders.iter() {
812            unsafe { gl.delete_shader(shader) };
813        }
814    }
815}
816
817impl super::PipelineInner {
818    /// Reads the program's link once, when a submission first uses it.
819    /// `unread` counts the programs whose link no command read yet.
820    pub(super) unsafe fn read_link(
821        &self,
822        gl: &glow::Context,
823        unread: &AtomicUsize,
824    ) -> MutexGuard<'_, super::ProgramLink> {
825        let mut link = self.link.lock();
826        if let Some(pending) = link.pending.take() {
827            unread.fetch_sub(1, Ordering::Relaxed);
828            link.linked = Some(Arc::new(unsafe { pending.finish(gl, self.program) }));
829        }
830        link
831    }
832
833    /// What the linked program tells, read at its first use.
834    pub(super) unsafe fn linked(
835        &self,
836        gl: &glow::Context,
837        unread: &AtomicUsize,
838    ) -> Arc<super::LinkedProgram> {
839        unsafe { self.read_link(gl, unread) }
840            .linked
841            .clone()
842            .unwrap_or_default()
843    }
844
845    /// Deletes the program, with its shaders when no command used it.
846    unsafe fn delete(&self, gl: &glow::Context, unread: &AtomicUsize) {
847        if let Some(pending) = self.link.lock().pending.take() {
848            unread.fetch_sub(1, Ordering::Relaxed);
849            unsafe { pending.delete_shaders(gl) };
850        }
851        unsafe { gl.delete_program(self.program) };
852    }
853}
854
855impl crate::Device for super::Device {
856    type A = super::Api;
857
858    unsafe fn create_buffer(
859        &self,
860        desc: &crate::BufferDescriptor,
861    ) -> Result<super::Buffer, crate::DeviceError> {
862        let target = if desc.usage.contains(wgt::BufferUses::INDEX) {
863            glow::ELEMENT_ARRAY_BUFFER
864        } else {
865            glow::ARRAY_BUFFER
866        };
867
868        let emulate_map = self
869            .shared
870            .workarounds
871            .contains(super::Workarounds::EMULATE_BUFFER_MAP)
872            || !self
873                .shared
874                .private_caps
875                .contains(PrivateCapabilities::BUFFER_ALLOCATION);
876
877        if emulate_map && desc.usage.intersects(wgt::BufferUses::MAP_WRITE) {
878            return Ok(super::Buffer {
879                raw: None,
880                target,
881                size: desc.size,
882                map_flags: 0,
883                map_state: Arc::new(MaybeMutex::new(super::BufferMapState {
884                    mapped: false,
885                    data: Some(vec![0; desc.size as usize]),
886                    offset_of_current_mapping: 0,
887                })),
888                drop_guard: None,
889            });
890        }
891
892        let gl = &self.shared.context.lock();
893
894        let target = if desc.usage.contains(wgt::BufferUses::INDEX) {
895            glow::ELEMENT_ARRAY_BUFFER
896        } else {
897            glow::ARRAY_BUFFER
898        };
899
900        let is_host_visible = desc
901            .usage
902            .intersects(wgt::BufferUses::MAP_READ | wgt::BufferUses::MAP_WRITE);
903        let is_coherent = desc
904            .memory_flags
905            .contains(crate::MemoryFlags::PREFER_COHERENT);
906
907        let mut map_flags = 0;
908        if desc.usage.contains(wgt::BufferUses::MAP_READ) {
909            map_flags |= glow::MAP_READ_BIT;
910        }
911        if desc.usage.contains(wgt::BufferUses::MAP_WRITE) {
912            map_flags |= glow::MAP_WRITE_BIT;
913        }
914
915        let raw = Some(unsafe { gl.create_buffer() }.map_err(|_| crate::DeviceError::OutOfMemory)?);
916        unsafe { gl.bind_buffer(target, raw) };
917        let raw_size = desc
918            .size
919            .try_into()
920            .map_err(|_| crate::DeviceError::OutOfMemory)?;
921
922        if self
923            .shared
924            .private_caps
925            .contains(PrivateCapabilities::BUFFER_ALLOCATION)
926        {
927            if is_host_visible {
928                map_flags |= glow::MAP_PERSISTENT_BIT;
929                if is_coherent {
930                    map_flags |= glow::MAP_COHERENT_BIT;
931                }
932            }
933            // TODO: may also be required for other calls involving `buffer_sub_data_u8_slice` (e.g. copy buffer to buffer and clear buffer)
934            if desc.usage.intersects(wgt::BufferUses::QUERY_RESOLVE) {
935                map_flags |= glow::DYNAMIC_STORAGE_BIT;
936            }
937            unsafe { gl.buffer_storage(target, raw_size, None, map_flags) };
938        } else {
939            assert!(!is_coherent);
940            let usage = if is_host_visible {
941                if desc.usage.contains(wgt::BufferUses::MAP_READ) {
942                    glow::STREAM_READ
943                } else {
944                    glow::DYNAMIC_DRAW
945                }
946            } else {
947                // Even if the usage doesn't contain SRC_READ, we update it internally at least once
948                // Some vendors take usage very literally and STATIC_DRAW will freeze us with an empty buffer
949                // https://github.com/gfx-rs/wgpu/issues/3371
950                glow::DYNAMIC_DRAW
951            };
952            unsafe { gl.buffer_data_size(target, raw_size, usage) };
953        }
954
955        unsafe { gl.bind_buffer(target, None) };
956
957        if !is_coherent && desc.usage.contains(wgt::BufferUses::MAP_WRITE) {
958            map_flags |= glow::MAP_FLUSH_EXPLICIT_BIT;
959        }
960        //TODO: do we need `glow::MAP_UNSYNCHRONIZED_BIT`?
961
962        #[cfg(native)]
963        if let Some(label) = desc.label {
964            if self
965                .shared
966                .private_caps
967                .contains(PrivateCapabilities::DEBUG_FNS)
968            {
969                let name = raw.map_or(0, |buf| buf.0.get());
970                unsafe { gl.object_label(glow::BUFFER, name, Some(label)) };
971            }
972        }
973
974        let data = if emulate_map && desc.usage.contains(wgt::BufferUses::MAP_READ) {
975            Some(vec![0; desc.size as usize])
976        } else {
977            None
978        };
979
980        self.counters.buffers.add(1);
981
982        Ok(super::Buffer {
983            raw,
984            target,
985            size: desc.size,
986            map_flags,
987            map_state: Arc::new(MaybeMutex::new(super::BufferMapState {
988                mapped: false,
989                data,
990                offset_of_current_mapping: 0,
991            })),
992            drop_guard: None,
993        })
994    }
995
996    unsafe fn destroy_buffer(&self, buffer: super::Buffer) {
997        if buffer.drop_guard.is_none() {
998            if let Some(raw) = buffer.raw {
999                let gl = &self.shared.context.lock();
1000                unsafe { gl.delete_buffer(raw) };
1001            }
1002        }
1003
1004        // For clarity, we explicitly drop the drop guard. Although this has no real semantic effect as the
1005        // end of the scope will drop the drop guard since this function takes ownership of the buffer.
1006        drop(buffer.drop_guard);
1007
1008        self.counters.buffers.sub(1);
1009    }
1010
1011    unsafe fn add_raw_buffer(&self, _buffer: &super::Buffer) {
1012        self.counters.buffers.add(1);
1013    }
1014
1015    unsafe fn map_buffer(
1016        &self,
1017        buffer: &super::Buffer,
1018        range: crate::MemoryRange,
1019    ) -> Result<crate::BufferMapping, crate::DeviceError> {
1020        let is_coherent = buffer.map_flags & glow::MAP_COHERENT_BIT != 0;
1021        let ptr = match buffer.raw {
1022            None => {
1023                let mut map_state = lock(&buffer.map_state);
1024                let vec = map_state.data.as_mut().unwrap();
1025                let slice = &mut vec.as_mut_slice()[range.start as usize..range.end as usize];
1026                slice.as_mut_ptr()
1027            }
1028            Some(raw) => {
1029                let gl = &self.shared.context.lock();
1030                unsafe { gl.bind_buffer(buffer.target, Some(raw)) };
1031                let mut map_state = lock(&buffer.map_state);
1032                let ptr = if let Some(map_read_allocation) = map_state.data.as_mut() {
1033                    let slice = map_read_allocation.as_mut_slice();
1034                    unsafe { self.shared.get_buffer_sub_data(gl, buffer.target, 0, slice) };
1035                    slice.as_mut_ptr()
1036                } else {
1037                    map_state.offset_of_current_mapping = range.start;
1038                    // glMapBufferRange throws an error if length is 0.
1039                    // We want to allow mapping 0-sized buffer slices, so perform a workaround
1040                    // if the range length is 0. The resulting pointer must never be dereferenced.
1041                    let range_start: i32 = range
1042                        .start
1043                        .try_into()
1044                        .expect("Buffer range invalid for GLES");
1045                    let range_length: i32 = (range.end - range.start)
1046                        .try_into()
1047                        .expect("Buffer range invalid for GLES");
1048                    if range_length != 0 {
1049                        map_state.mapped = true;
1050                        unsafe {
1051                            gl.map_buffer_range(
1052                                buffer.target,
1053                                range_start,
1054                                range_length,
1055                                buffer.map_flags,
1056                            )
1057                        }
1058                    } else {
1059                        ptr::dangling_mut()
1060                    }
1061                };
1062                unsafe { gl.bind_buffer(buffer.target, None) };
1063                ptr
1064            }
1065        };
1066        Ok(crate::BufferMapping {
1067            ptr: ptr::NonNull::new(ptr).ok_or(crate::DeviceError::Lost)?,
1068            is_coherent,
1069        })
1070    }
1071    unsafe fn unmap_buffer(&self, buffer: &super::Buffer) {
1072        let gl = &self.shared.context.lock();
1073        let mut map_state = lock(&buffer.map_state);
1074        if core::mem::replace(&mut map_state.mapped, false) {
1075            if let Some(raw) = buffer.raw {
1076                if map_state.data.is_none() {
1077                    unsafe { gl.bind_buffer(buffer.target, Some(raw)) };
1078                    unsafe { gl.unmap_buffer(buffer.target) };
1079                    unsafe { gl.bind_buffer(buffer.target, None) };
1080                    map_state.offset_of_current_mapping = 0;
1081                }
1082            }
1083        }
1084    }
1085    unsafe fn flush_mapped_ranges<I>(&self, buffer: &super::Buffer, ranges: I)
1086    where
1087        I: Iterator<Item = crate::MemoryRange>,
1088    {
1089        let gl = &self.shared.context.lock();
1090        let map_state = lock(&buffer.map_state);
1091        if map_state.mapped {
1092            if let Some(raw) = buffer.raw {
1093                if map_state.data.is_none() {
1094                    unsafe { gl.bind_buffer(buffer.target, Some(raw)) };
1095                    for range in ranges {
1096                        let offset_of_current_mapping = map_state.offset_of_current_mapping;
1097                        unsafe {
1098                            gl.flush_mapped_buffer_range(
1099                                buffer.target,
1100                                (range.start - offset_of_current_mapping) as i32,
1101                                (range.end - range.start) as i32,
1102                            )
1103                        };
1104                    }
1105                }
1106            }
1107        }
1108    }
1109    unsafe fn invalidate_mapped_ranges<I>(&self, _buffer: &super::Buffer, _ranges: I) {
1110        //TODO: do we need to do anything?
1111    }
1112
1113    unsafe fn create_texture(
1114        &self,
1115        desc: &crate::TextureDescriptor,
1116    ) -> Result<super::Texture, crate::DeviceError> {
1117        let gl = &self.shared.context.lock();
1118
1119        let render_usage = wgt::TextureUses::COLOR_TARGET
1120            | wgt::TextureUses::DEPTH_STENCIL_WRITE
1121            | wgt::TextureUses::DEPTH_STENCIL_READ
1122            | wgt::TextureUses::TRANSIENT;
1123        let format_desc = self.shared.describe_texture_format(desc.format);
1124
1125        let inner = if render_usage.contains(desc.usage)
1126            && desc.dimension == wgt::TextureDimension::D2
1127            && desc.size.depth_or_array_layers == 1
1128        {
1129            let raw = unsafe { gl.create_renderbuffer().unwrap() };
1130            unsafe { gl.bind_renderbuffer(glow::RENDERBUFFER, Some(raw)) };
1131            if desc.sample_count > 1 {
1132                unsafe {
1133                    gl.renderbuffer_storage_multisample(
1134                        glow::RENDERBUFFER,
1135                        desc.sample_count as i32,
1136                        format_desc.internal,
1137                        desc.size.width as i32,
1138                        desc.size.height as i32,
1139                    )
1140                };
1141            } else {
1142                unsafe {
1143                    gl.renderbuffer_storage(
1144                        glow::RENDERBUFFER,
1145                        format_desc.internal,
1146                        desc.size.width as i32,
1147                        desc.size.height as i32,
1148                    )
1149                };
1150            }
1151
1152            #[cfg(native)]
1153            if let Some(label) = desc.label {
1154                if self
1155                    .shared
1156                    .private_caps
1157                    .contains(PrivateCapabilities::DEBUG_FNS)
1158                {
1159                    let name = raw.0.get();
1160                    unsafe { gl.object_label(glow::RENDERBUFFER, name, Some(label)) };
1161                }
1162            }
1163
1164            unsafe { gl.bind_renderbuffer(glow::RENDERBUFFER, None) };
1165            super::TextureInner::Renderbuffer { raw }
1166        } else {
1167            let raw = unsafe { gl.create_texture().unwrap() };
1168            let target = super::Texture::get_info_from_desc(desc);
1169
1170            unsafe { gl.bind_texture(target, Some(raw)) };
1171            //Note: this has to be done before defining the storage!
1172            match desc.format.sample_type(None, Some(self.shared.features)) {
1173                Some(
1174                    wgt::TextureSampleType::Float { filterable: false }
1175                    | wgt::TextureSampleType::Uint
1176                    | wgt::TextureSampleType::Sint,
1177                ) => {
1178                    // reset default filtering mode
1179                    unsafe {
1180                        gl.tex_parameter_i32(target, glow::TEXTURE_MIN_FILTER, glow::NEAREST as i32)
1181                    };
1182                    unsafe {
1183                        gl.tex_parameter_i32(target, glow::TEXTURE_MAG_FILTER, glow::NEAREST as i32)
1184                    };
1185                }
1186                _ => {}
1187            }
1188
1189            if conv::is_layered_target(target) {
1190                unsafe {
1191                    if self
1192                        .shared
1193                        .private_caps
1194                        .contains(PrivateCapabilities::TEXTURE_STORAGE)
1195                    {
1196                        gl.tex_storage_3d(
1197                            target,
1198                            desc.mip_level_count as i32,
1199                            format_desc.internal,
1200                            desc.size.width as i32,
1201                            desc.size.height as i32,
1202                            desc.size.depth_or_array_layers as i32,
1203                        )
1204                    } else if target == glow::TEXTURE_3D {
1205                        let mut width = desc.size.width;
1206                        let mut height = desc.size.height;
1207                        let mut depth = desc.size.depth_or_array_layers;
1208                        for i in 0..desc.mip_level_count {
1209                            gl.tex_image_3d(
1210                                target,
1211                                i as i32,
1212                                format_desc.internal as i32,
1213                                width as i32,
1214                                height as i32,
1215                                depth as i32,
1216                                0,
1217                                format_desc.external,
1218                                format_desc.data_type,
1219                                glow::PixelUnpackData::Slice(None),
1220                            );
1221                            width = max(1, width / 2);
1222                            height = max(1, height / 2);
1223                            depth = max(1, depth / 2);
1224                        }
1225                    } else {
1226                        let mut width = desc.size.width;
1227                        let mut height = desc.size.height;
1228                        for i in 0..desc.mip_level_count {
1229                            gl.tex_image_3d(
1230                                target,
1231                                i as i32,
1232                                format_desc.internal as i32,
1233                                width as i32,
1234                                height as i32,
1235                                desc.size.depth_or_array_layers as i32,
1236                                0,
1237                                format_desc.external,
1238                                format_desc.data_type,
1239                                glow::PixelUnpackData::Slice(None),
1240                            );
1241                            width = max(1, width / 2);
1242                            height = max(1, height / 2);
1243                        }
1244                    }
1245                };
1246            } else if desc.sample_count > 1 {
1247                unsafe {
1248                    gl.tex_storage_2d_multisample(
1249                        target,
1250                        desc.sample_count as i32,
1251                        format_desc.internal,
1252                        desc.size.width as i32,
1253                        desc.size.height as i32,
1254                        true,
1255                    )
1256                };
1257            } else {
1258                unsafe {
1259                    if self
1260                        .shared
1261                        .private_caps
1262                        .contains(PrivateCapabilities::TEXTURE_STORAGE)
1263                    {
1264                        gl.tex_storage_2d(
1265                            target,
1266                            desc.mip_level_count as i32,
1267                            format_desc.internal,
1268                            desc.size.width as i32,
1269                            desc.size.height as i32,
1270                        )
1271                    } else if target == glow::TEXTURE_CUBE_MAP {
1272                        let mut width = desc.size.width;
1273                        let mut height = desc.size.height;
1274                        for i in 0..desc.mip_level_count {
1275                            for face in [
1276                                glow::TEXTURE_CUBE_MAP_POSITIVE_X,
1277                                glow::TEXTURE_CUBE_MAP_NEGATIVE_X,
1278                                glow::TEXTURE_CUBE_MAP_POSITIVE_Y,
1279                                glow::TEXTURE_CUBE_MAP_NEGATIVE_Y,
1280                                glow::TEXTURE_CUBE_MAP_POSITIVE_Z,
1281                                glow::TEXTURE_CUBE_MAP_NEGATIVE_Z,
1282                            ] {
1283                                gl.tex_image_2d(
1284                                    face,
1285                                    i as i32,
1286                                    format_desc.internal as i32,
1287                                    width as i32,
1288                                    height as i32,
1289                                    0,
1290                                    format_desc.external,
1291                                    format_desc.data_type,
1292                                    glow::PixelUnpackData::Slice(None),
1293                                );
1294                            }
1295                            width = max(1, width / 2);
1296                            height = max(1, height / 2);
1297                        }
1298                    } else {
1299                        let mut width = desc.size.width;
1300                        let mut height = desc.size.height;
1301                        for i in 0..desc.mip_level_count {
1302                            gl.tex_image_2d(
1303                                target,
1304                                i as i32,
1305                                format_desc.internal as i32,
1306                                width as i32,
1307                                height as i32,
1308                                0,
1309                                format_desc.external,
1310                                format_desc.data_type,
1311                                glow::PixelUnpackData::Slice(None),
1312                            );
1313                            width = max(1, width / 2);
1314                            height = max(1, height / 2);
1315                        }
1316                    }
1317                };
1318            }
1319
1320            #[cfg(native)]
1321            if let Some(label) = desc.label {
1322                if self
1323                    .shared
1324                    .private_caps
1325                    .contains(PrivateCapabilities::DEBUG_FNS)
1326                {
1327                    let name = raw.0.get();
1328                    unsafe { gl.object_label(glow::TEXTURE, name, Some(label)) };
1329                }
1330            }
1331
1332            unsafe { gl.bind_texture(target, None) };
1333            super::TextureInner::Texture { raw, target }
1334        };
1335
1336        self.counters.textures.add(1);
1337
1338        Ok(super::Texture {
1339            inner,
1340            drop_guard: None,
1341            mip_level_count: desc.mip_level_count,
1342            array_layer_count: desc.array_layer_count(),
1343            format: desc.format,
1344            format_desc,
1345            copy_size: desc.copy_extent(),
1346        })
1347    }
1348
1349    unsafe fn destroy_texture(&self, texture: super::Texture) {
1350        if texture.drop_guard.is_none() {
1351            let gl = &self.shared.context.lock();
1352            match texture.inner {
1353                super::TextureInner::Renderbuffer { raw, .. } => {
1354                    unsafe { gl.delete_renderbuffer(raw) };
1355                }
1356                super::TextureInner::DefaultRenderbuffer => {}
1357                super::TextureInner::Texture { raw, .. } => {
1358                    unsafe { gl.delete_texture(raw) };
1359                }
1360                #[cfg(webgl)]
1361                super::TextureInner::ExternalFramebuffer { .. } => {}
1362                #[cfg(native)]
1363                super::TextureInner::ExternalNativeFramebuffer { .. } => {}
1364            }
1365        }
1366
1367        // For clarity, we explicitly drop the drop guard. Although this has no real semantic effect as the
1368        // end of the scope will drop the drop guard since this function takes ownership of the texture.
1369        drop(texture.drop_guard);
1370
1371        self.counters.textures.sub(1);
1372    }
1373
1374    unsafe fn add_raw_texture(&self, _texture: &super::Texture) {
1375        self.counters.textures.add(1);
1376    }
1377
1378    unsafe fn create_texture_view(
1379        &self,
1380        texture: &super::Texture,
1381        desc: &crate::TextureViewDescriptor,
1382    ) -> Result<super::TextureView, crate::DeviceError> {
1383        self.counters.texture_views.add(1);
1384        Ok(super::TextureView {
1385            //TODO: use `conv::map_view_dimension(desc.dimension)`?
1386            inner: texture.inner.clone(),
1387            aspects: crate::FormatAspects::new(texture.format, desc.range.aspect),
1388            mip_levels: desc.range.mip_range(texture.mip_level_count),
1389            array_layers: desc.range.layer_range(texture.array_layer_count),
1390            format: texture.format,
1391        })
1392    }
1393
1394    unsafe fn destroy_texture_view(&self, _view: super::TextureView) {
1395        self.counters.texture_views.sub(1);
1396    }
1397
1398    unsafe fn create_sampler(
1399        &self,
1400        desc: &crate::SamplerDescriptor,
1401    ) -> Result<super::Sampler, crate::DeviceError> {
1402        let gl = &self.shared.context.lock();
1403
1404        let raw = unsafe { gl.create_sampler().unwrap() };
1405
1406        let (min, mag) =
1407            conv::map_filter_modes(desc.min_filter, desc.mag_filter, desc.mipmap_filter);
1408
1409        unsafe { gl.sampler_parameter_i32(raw, glow::TEXTURE_MIN_FILTER, min as i32) };
1410        unsafe { gl.sampler_parameter_i32(raw, glow::TEXTURE_MAG_FILTER, mag as i32) };
1411
1412        unsafe {
1413            gl.sampler_parameter_i32(
1414                raw,
1415                glow::TEXTURE_WRAP_S,
1416                conv::map_address_mode(desc.address_modes[0]) as i32,
1417            )
1418        };
1419        unsafe {
1420            gl.sampler_parameter_i32(
1421                raw,
1422                glow::TEXTURE_WRAP_T,
1423                conv::map_address_mode(desc.address_modes[1]) as i32,
1424            )
1425        };
1426        unsafe {
1427            gl.sampler_parameter_i32(
1428                raw,
1429                glow::TEXTURE_WRAP_R,
1430                conv::map_address_mode(desc.address_modes[2]) as i32,
1431            )
1432        };
1433
1434        if let Some(border_color) = desc.border_color {
1435            let border = match border_color {
1436                wgt::SamplerBorderColor::TransparentBlack | wgt::SamplerBorderColor::Zero => {
1437                    [0.0; 4]
1438                }
1439                wgt::SamplerBorderColor::OpaqueBlack => [0.0, 0.0, 0.0, 1.0],
1440                wgt::SamplerBorderColor::OpaqueWhite => [1.0; 4],
1441            };
1442            unsafe { gl.sampler_parameter_f32_slice(raw, glow::TEXTURE_BORDER_COLOR, &border) };
1443        }
1444
1445        unsafe { gl.sampler_parameter_f32(raw, glow::TEXTURE_MIN_LOD, desc.lod_clamp.start) };
1446        unsafe { gl.sampler_parameter_f32(raw, glow::TEXTURE_MAX_LOD, desc.lod_clamp.end) };
1447
1448        // If clamp is not 1, we know anisotropy is supported up to 16x
1449        if desc.anisotropy_clamp != 1 {
1450            unsafe {
1451                gl.sampler_parameter_i32(
1452                    raw,
1453                    glow::TEXTURE_MAX_ANISOTROPY,
1454                    desc.anisotropy_clamp as i32,
1455                )
1456            };
1457        }
1458
1459        //set_param_float(glow::TEXTURE_LOD_BIAS, info.lod_bias.0);
1460
1461        if let Some(compare) = desc.compare {
1462            unsafe {
1463                gl.sampler_parameter_i32(
1464                    raw,
1465                    glow::TEXTURE_COMPARE_MODE,
1466                    glow::COMPARE_REF_TO_TEXTURE as i32,
1467                )
1468            };
1469            unsafe {
1470                gl.sampler_parameter_i32(
1471                    raw,
1472                    glow::TEXTURE_COMPARE_FUNC,
1473                    conv::map_compare_func(compare) as i32,
1474                )
1475            };
1476        }
1477
1478        #[cfg(native)]
1479        if let Some(label) = desc.label {
1480            if self
1481                .shared
1482                .private_caps
1483                .contains(PrivateCapabilities::DEBUG_FNS)
1484            {
1485                let name = raw.0.get();
1486                unsafe { gl.object_label(glow::SAMPLER, name, Some(label)) };
1487            }
1488        }
1489
1490        self.counters.samplers.add(1);
1491
1492        Ok(super::Sampler { raw })
1493    }
1494
1495    unsafe fn destroy_sampler(&self, sampler: super::Sampler) {
1496        let gl = &self.shared.context.lock();
1497        unsafe { gl.delete_sampler(sampler.raw) };
1498        self.counters.samplers.sub(1);
1499    }
1500
1501    unsafe fn create_command_encoder(
1502        &self,
1503        _desc: &crate::CommandEncoderDescriptor<super::Queue>,
1504    ) -> Result<super::CommandEncoder, crate::DeviceError> {
1505        self.counters.command_encoders.add(1);
1506
1507        Ok(super::CommandEncoder {
1508            cmd_buffer: super::CommandBuffer::default(),
1509            state: Default::default(),
1510            private_caps: self.shared.private_caps,
1511            counters: Arc::clone(&self.counters),
1512        })
1513    }
1514
1515    unsafe fn create_bind_group_layout(
1516        &self,
1517        desc: &crate::BindGroupLayoutDescriptor,
1518    ) -> Result<super::BindGroupLayout, crate::DeviceError> {
1519        self.counters.bind_group_layouts.add(1);
1520        Ok(super::BindGroupLayout {
1521            entries: Arc::from(desc.entries),
1522        })
1523    }
1524
1525    unsafe fn destroy_bind_group_layout(&self, _bg_layout: super::BindGroupLayout) {
1526        self.counters.bind_group_layouts.sub(1);
1527    }
1528
1529    unsafe fn create_pipeline_layout(
1530        &self,
1531        desc: &crate::PipelineLayoutDescriptor<super::BindGroupLayout>,
1532    ) -> Result<super::PipelineLayout, crate::DeviceError> {
1533        use naga::back::glsl;
1534
1535        let mut group_infos = Vec::with_capacity(desc.bind_group_layouts.len());
1536        let mut num_samplers = 0u8;
1537        let mut num_textures = 0u8;
1538        let mut num_images = 0u8;
1539        let mut num_uniform_buffers = 0u8;
1540        let mut num_storage_buffers = 0u8;
1541
1542        let mut writer_flags = glsl::WriterFlags::ADJUST_COORDINATE_SPACE;
1543        writer_flags.set(
1544            glsl::WriterFlags::TEXTURE_SHADOW_LOD,
1545            self.shared
1546                .private_caps
1547                .contains(PrivateCapabilities::SHADER_TEXTURE_SHADOW_LOD),
1548        );
1549        writer_flags.set(
1550            glsl::WriterFlags::DRAW_PARAMETERS,
1551            self.shared
1552                .private_caps
1553                .contains(PrivateCapabilities::FULLY_FEATURED_INSTANCING),
1554        );
1555        // A point list pipeline writes the point size (`create_shader`); no
1556        // other one does, as WebGL counts `gl_PointSize` against the 15
1557        // varying vectors a program may link with.
1558        let mut binding_map = glsl::BindingMap::default();
1559
1560        for (group_index, bg_layout) in desc.bind_group_layouts.iter().enumerate() {
1561            let Some(bg_layout) = bg_layout else {
1562                group_infos.push(None);
1563                continue;
1564            };
1565
1566            // create a vector with the size enough to hold all the bindings, filled with `!0`
1567            let mut binding_to_slot = vec![
1568                !0;
1569                bg_layout
1570                    .entries
1571                    .iter()
1572                    .map(|b| b.binding)
1573                    .max()
1574                    .map_or(0, |idx| idx as usize + 1)
1575            ]
1576            .into_boxed_slice();
1577
1578            for entry in bg_layout.entries.iter() {
1579                let counter = match entry.ty {
1580                    wgt::BindingType::Sampler { .. } => &mut num_samplers,
1581                    wgt::BindingType::Texture { .. } => &mut num_textures,
1582                    wgt::BindingType::StorageTexture { .. } => &mut num_images,
1583                    wgt::BindingType::Buffer {
1584                        ty: wgt::BufferBindingType::Uniform,
1585                        ..
1586                    } => &mut num_uniform_buffers,
1587                    wgt::BindingType::Buffer {
1588                        ty: wgt::BufferBindingType::Storage { .. },
1589                        ..
1590                    } => &mut num_storage_buffers,
1591                    wgt::BindingType::AccelerationStructure { .. } => unimplemented!(),
1592                    wgt::BindingType::ExternalTexture => unimplemented!(),
1593                };
1594
1595                binding_to_slot[entry.binding as usize] = *counter;
1596                let br = naga::ResourceBinding {
1597                    group: group_index as u32,
1598                    binding: entry.binding,
1599                };
1600                binding_map.insert(br, *counter);
1601                *counter += entry.count.map_or(1, |c| c.get() as u8);
1602            }
1603
1604            group_infos.push(Some(super::BindGroupLayoutInfo {
1605                entries: Arc::clone(&bg_layout.entries),
1606                binding_to_slot,
1607            }));
1608        }
1609
1610        self.counters.pipeline_layouts.add(1);
1611
1612        Ok(super::PipelineLayout {
1613            group_infos: group_infos.into_boxed_slice(),
1614            naga_options: glsl::Options {
1615                version: self.shared.shading_language_version,
1616                writer_flags,
1617                binding_map,
1618                zero_initialize_workgroup_memory: true,
1619            },
1620        })
1621    }
1622
1623    unsafe fn destroy_pipeline_layout(&self, _pipeline_layout: super::PipelineLayout) {
1624        self.counters.pipeline_layouts.sub(1);
1625    }
1626
1627    unsafe fn create_bind_group(
1628        &self,
1629        desc: &crate::BindGroupDescriptor<
1630            super::BindGroupLayout,
1631            super::Buffer,
1632            super::Sampler,
1633            super::TextureView,
1634            super::AccelerationStructure,
1635        >,
1636    ) -> Result<super::BindGroup, crate::DeviceError> {
1637        let mut contents = Vec::new();
1638
1639        let layout_and_entry_iter = desc.entries.iter().map(|entry| {
1640            let layout = desc
1641                .layout
1642                .entries
1643                .iter()
1644                .find(|layout_entry| layout_entry.binding == entry.binding)
1645                .expect("internal error: no layout entry found with binding slot");
1646            (entry, layout)
1647        });
1648        for (entry, layout) in layout_and_entry_iter {
1649            let binding = match layout.ty {
1650                wgt::BindingType::Buffer { .. } => {
1651                    let bb = &desc.buffers[entry.resource_index as usize];
1652                    super::RawBinding::Buffer {
1653                        raw: bb.buffer.raw.unwrap(),
1654                        offset: bb.offset as i32,
1655                        size: match bb.size {
1656                            Some(s) => s.get() as i32,
1657                            None => (bb.buffer.size - bb.offset) as i32,
1658                        },
1659                    }
1660                }
1661                wgt::BindingType::Sampler { .. } => {
1662                    let sampler = desc.samplers[entry.resource_index as usize];
1663                    super::RawBinding::Sampler(sampler.raw)
1664                }
1665                wgt::BindingType::Texture { view_dimension, .. } => {
1666                    let view = desc.textures[entry.resource_index as usize].view;
1667                    if view.array_layers.start != 0 {
1668                        log::error!("Unable to create a sampled texture binding for non-zero array layer.\n{}",
1669                            "This is an implementation problem of wgpu-hal/gles backend.")
1670                    }
1671                    let (raw, target) = view.inner.as_native();
1672
1673                    super::Texture::log_failing_target_heuristics(view_dimension, target);
1674
1675                    super::RawBinding::Texture {
1676                        raw,
1677                        target,
1678                        aspects: view.aspects,
1679                        mip_levels: view.mip_levels.clone(),
1680                    }
1681                }
1682                wgt::BindingType::StorageTexture {
1683                    access,
1684                    format,
1685                    view_dimension,
1686                } => {
1687                    let view = desc.textures[entry.resource_index as usize].view;
1688                    let format_desc = self.shared.describe_texture_format(format);
1689                    let (raw, _target) = view.inner.as_native();
1690                    super::RawBinding::Image(super::ImageBinding {
1691                        raw,
1692                        mip_level: view.mip_levels.start,
1693                        array_layer: match view_dimension {
1694                            wgt::TextureViewDimension::D2Array
1695                            | wgt::TextureViewDimension::CubeArray => None,
1696                            _ => Some(view.array_layers.start),
1697                        },
1698                        access: conv::map_storage_access(access),
1699                        format: format_desc.internal,
1700                    })
1701                }
1702                wgt::BindingType::AccelerationStructure { .. } => unimplemented!(),
1703                wgt::BindingType::ExternalTexture => unimplemented!(),
1704            };
1705            contents.push(binding);
1706        }
1707
1708        self.counters.bind_groups.add(1);
1709
1710        Ok(super::BindGroup {
1711            contents: contents.into_boxed_slice(),
1712        })
1713    }
1714
1715    unsafe fn destroy_bind_group(&self, _group: super::BindGroup) {
1716        self.counters.bind_groups.sub(1);
1717    }
1718
1719    unsafe fn create_shader_module(
1720        &self,
1721        desc: &crate::ShaderModuleDescriptor,
1722        shader: crate::ShaderInput,
1723    ) -> Result<super::ShaderModule, crate::ShaderError> {
1724        self.counters.shader_modules.add(1);
1725
1726        Ok(super::ShaderModule {
1727            source: match shader {
1728                crate::ShaderInput::Naga(naga) => super::ShaderModuleSource::Naga(naga),
1729                // The backend doesn't yet expose this feature so it should be fine
1730                crate::ShaderInput::Glsl { shader, .. } => super::ShaderModuleSource::Passthrough {
1731                    source: shader.to_owned(),
1732                },
1733                crate::ShaderInput::GlslTemplates(templates) => {
1734                    super::ShaderModuleSource::Templates(templates)
1735                }
1736                crate::ShaderInput::SpirV(_)
1737                | crate::ShaderInput::MetalLib { .. }
1738                | crate::ShaderInput::Msl { .. }
1739                | crate::ShaderInput::Dxil { .. }
1740                | crate::ShaderInput::Hlsl { .. } => {
1741                    unreachable!()
1742                }
1743            },
1744            label: desc.label.map(|str| str.to_string()),
1745            id: self.shared.next_shader_id.fetch_add(1, Ordering::Relaxed),
1746        })
1747    }
1748
1749    unsafe fn destroy_shader_module(&self, _module: super::ShaderModule) {
1750        self.counters.shader_modules.sub(1);
1751    }
1752
1753    unsafe fn create_render_pipeline(
1754        &self,
1755        desc: &crate::RenderPipelineDescriptor<
1756            super::PipelineLayout,
1757            super::ShaderModule,
1758            super::PipelineCache,
1759        >,
1760    ) -> Result<super::RenderPipeline, crate::PipelineError> {
1761        let (vertex_stage, vertex_buffers) = match &desc.vertex_processor {
1762            crate::VertexProcessor::Standard {
1763                vertex_buffers,
1764                ref vertex_stage,
1765            } => (vertex_stage, vertex_buffers),
1766            crate::VertexProcessor::Mesh { .. } => unreachable!(),
1767        };
1768        let gl = &self.shared.context.lock();
1769        let mut shaders = ArrayVec::new();
1770        shaders.push((naga::ShaderStage::Vertex, vertex_stage));
1771        if let Some(ref fs) = desc.fragment_stage {
1772            shaders.push((naga::ShaderStage::Fragment, fs));
1773        }
1774        let points = desc.primitive.topology == wgt::PrimitiveTopology::PointList;
1775        let inner = unsafe {
1776            self.create_pipeline(
1777                gl,
1778                shaders,
1779                desc.layout,
1780                desc.label,
1781                desc.multiview_mask,
1782                points,
1783            )
1784        }?;
1785
1786        let (vertex_buffers, vertex_attributes) = {
1787            let mut buffers = Vec::new();
1788            let mut attributes = Vec::new();
1789            for (index, vb_layout) in vertex_buffers.iter().enumerate() {
1790                let vb_desc = if let Some(vb_layout) = vb_layout {
1791                    for vat in vb_layout.attributes.iter() {
1792                        let format_desc = conv::describe_vertex_format(vat.format);
1793                        attributes.push(super::AttributeDesc {
1794                            location: vat.shader_location,
1795                            offset: vat.offset as u32,
1796                            buffer_index: index as u32,
1797                            format_desc,
1798                        });
1799                    }
1800                    Some(super::VertexBufferDesc {
1801                        step: vb_layout.step_mode,
1802                        stride: vb_layout.array_stride as u32,
1803                    })
1804                } else {
1805                    None
1806                };
1807                buffers.push(vb_desc);
1808            }
1809            (buffers.into_boxed_slice(), attributes.into_boxed_slice())
1810        };
1811
1812        let color_targets = {
1813            let mut targets = Vec::new();
1814            for ct in desc.color_targets.iter().filter_map(|at| at.as_ref()) {
1815                targets.push(super::ColorTargetDesc {
1816                    mask: ct.write_mask,
1817                    blend: ct.blend.as_ref().map(conv::map_blend),
1818                });
1819            }
1820            //Note: if any of the states are different, and `INDEPENDENT_BLEND` flag
1821            // is not exposed, then this pipeline will not bind correctly.
1822            targets.into_boxed_slice()
1823        };
1824
1825        self.counters.render_pipelines.add(1);
1826
1827        Ok(super::RenderPipeline {
1828            inner,
1829            primitive: desc.primitive,
1830            vertex_buffers,
1831            vertex_attributes,
1832            color_targets,
1833            depth: desc.depth_stencil.as_ref().map(|ds| super::DepthState {
1834                function: conv::map_compare_func(ds.depth_compare.unwrap_or_default()),
1835                mask: ds.depth_write_enabled.unwrap_or_default(),
1836            }),
1837            depth_bias: desc
1838                .depth_stencil
1839                .as_ref()
1840                .map(|ds| ds.bias)
1841                .unwrap_or_default(),
1842            stencil: desc
1843                .depth_stencil
1844                .as_ref()
1845                .map(|ds| conv::map_stencil(&ds.stencil)),
1846            alpha_to_coverage_enabled: desc.multisample.alpha_to_coverage_enabled,
1847        })
1848    }
1849
1850    unsafe fn destroy_render_pipeline(&self, pipeline: super::RenderPipeline) {
1851        // If the pipeline only has 2 strong references remaining, they're `pipeline` and `program_cache`
1852        // This is safe to assume as long as:
1853        // - `RenderPipeline` can't be cloned
1854        // - The only place that we can get a new reference is during `program_cache.lock()`
1855        if Arc::strong_count(&pipeline.inner) == 2 {
1856            let gl = &self.shared.context.lock();
1857            let mut program_cache = self.shared.program_cache.lock();
1858            program_cache.retain(|_, v| match *v {
1859                Ok(ref p) => p.program != pipeline.inner.program,
1860                Err(_) => false,
1861            });
1862            unsafe { pipeline.inner.delete(gl, &self.shared.unread_links) };
1863        }
1864
1865        self.counters.render_pipelines.sub(1);
1866    }
1867
1868    unsafe fn create_compute_pipeline(
1869        &self,
1870        desc: &crate::ComputePipelineDescriptor<
1871            super::PipelineLayout,
1872            super::ShaderModule,
1873            super::PipelineCache,
1874        >,
1875    ) -> Result<super::ComputePipeline, crate::PipelineError> {
1876        let gl = &self.shared.context.lock();
1877        let mut shaders = ArrayVec::new();
1878        shaders.push((naga::ShaderStage::Compute, &desc.stage));
1879        let inner =
1880            unsafe { self.create_pipeline(gl, shaders, desc.layout, desc.label, None, false) }?;
1881
1882        self.counters.compute_pipelines.add(1);
1883
1884        Ok(super::ComputePipeline { inner })
1885    }
1886
1887    unsafe fn destroy_compute_pipeline(&self, pipeline: super::ComputePipeline) {
1888        // If the pipeline only has 2 strong references remaining, they're `pipeline` and `program_cache``
1889        // This is safe to assume as long as:
1890        // - `ComputePipeline` can't be cloned
1891        // - The only place that we can get a new reference is during `program_cache.lock()`
1892        if Arc::strong_count(&pipeline.inner) == 2 {
1893            let gl = &self.shared.context.lock();
1894            let mut program_cache = self.shared.program_cache.lock();
1895            program_cache.retain(|_, v| match *v {
1896                Ok(ref p) => p.program != pipeline.inner.program,
1897                Err(_) => false,
1898            });
1899            unsafe { pipeline.inner.delete(gl, &self.shared.unread_links) };
1900        }
1901
1902        self.counters.compute_pipelines.sub(1);
1903    }
1904
1905    unsafe fn create_ray_tracing_pipeline(
1906        &self,
1907        _desc: &crate::RayTracingPipelineDescriptor<
1908            super::PipelineLayout,
1909            super::ShaderModule,
1910            super::PipelineCache,
1911        >,
1912    ) -> Result<super::RayTracingPipeline, crate::PipelineError> {
1913        unimplemented!("Ray tracing is unsupported on GL")
1914    }
1915
1916    unsafe fn destroy_ray_tracing_pipeline(&self, _pipeline: super::RayTracingPipeline) {
1917        unimplemented!("Ray tracing is unsupported on GL")
1918    }
1919
1920    unsafe fn get_raytracing_pipeline_group_data(
1921        &self,
1922        _pipeline: &super::RayTracingPipeline,
1923        _groups: core::ops::Range<u32>,
1924    ) -> Result<Vec<u8>, crate::DeviceError> {
1925        unimplemented!("Ray tracing is unsupported on GL")
1926    }
1927
1928    unsafe fn create_pipeline_cache(
1929        &self,
1930        _: &crate::PipelineCacheDescriptor<'_>,
1931    ) -> Result<super::PipelineCache, crate::PipelineCacheError> {
1932        // Even though the cache doesn't do anything, we still return something here
1933        // as the least bad option
1934        Ok(super::PipelineCache)
1935    }
1936    unsafe fn destroy_pipeline_cache(&self, _: super::PipelineCache) {}
1937
1938    #[cfg_attr(target_arch = "wasm32", allow(unused))]
1939    unsafe fn create_query_set(
1940        &self,
1941        desc: &wgt::QuerySetDescriptor<crate::Label>,
1942    ) -> Result<super::QuerySet, crate::DeviceError> {
1943        let gl = &self.shared.context.lock();
1944
1945        let mut queries = Vec::with_capacity(desc.count as usize);
1946        for _ in 0..desc.count {
1947            let query =
1948                unsafe { gl.create_query() }.map_err(|_| crate::DeviceError::OutOfMemory)?;
1949
1950            // We aren't really able to, in general, label queries.
1951            //
1952            // We could take a timestamp here to "initialize" the query,
1953            // but that's a bit of a hack, and we don't want to insert
1954            // random timestamps into the command stream of we don't have to.
1955
1956            queries.push(query);
1957        }
1958
1959        self.counters.query_sets.add(1);
1960
1961        Ok(super::QuerySet {
1962            queries: queries.into_boxed_slice(),
1963            target: match desc.ty {
1964                wgt::QueryType::Occlusion => glow::ANY_SAMPLES_PASSED_CONSERVATIVE,
1965                wgt::QueryType::Timestamp => glow::TIMESTAMP,
1966                _ => unimplemented!(),
1967            },
1968        })
1969    }
1970
1971    unsafe fn destroy_query_set(&self, set: super::QuerySet) {
1972        let gl = &self.shared.context.lock();
1973        for &query in set.queries.iter() {
1974            unsafe { gl.delete_query(query) };
1975        }
1976        self.counters.query_sets.sub(1);
1977    }
1978
1979    unsafe fn create_fence(&self) -> Result<super::Fence, crate::DeviceError> {
1980        self.counters.fences.add(1);
1981        Ok(super::Fence::new(&self.shared.options))
1982    }
1983
1984    unsafe fn destroy_fence(&self, fence: super::Fence) {
1985        let gl = &self.shared.context.lock();
1986        fence.destroy(gl);
1987        self.counters.fences.sub(1);
1988    }
1989
1990    unsafe fn get_fence_value(
1991        &self,
1992        fence: &super::Fence,
1993    ) -> Result<crate::FenceValue, crate::DeviceError> {
1994        #[cfg_attr(target_arch = "wasm32", allow(clippy::needless_borrow))]
1995        Ok(fence.get_latest(&self.shared.context.lock()))
1996    }
1997    unsafe fn wait(
1998        &self,
1999        fence: &super::Fence,
2000        wait_value: crate::FenceValue,
2001        timeout: Option<core::time::Duration>,
2002    ) -> Result<bool, crate::DeviceError> {
2003        if fence.satisfied(wait_value) {
2004            return Ok(true);
2005        }
2006
2007        let gl = &self.shared.context.lock();
2008        // MAX_CLIENT_WAIT_TIMEOUT_WEBGL is:
2009        // - 1s in Gecko https://searchfox.org/mozilla-central/rev/754074e05178e017ef6c3d8e30428ffa8f1b794d/dom/canvas/WebGLTypes.h#1386
2010        // - 0 in WebKit https://github.com/WebKit/WebKit/blob/4ef90d4672ca50267c0971b85db403d9684508ea/Source/WebCore/html/canvas/WebGL2RenderingContext.cpp#L110
2011        // - 0 in Chromium https://source.chromium.org/chromium/chromium/src/+/main:third_party/blink/renderer/modules/webgl/webgl2_rendering_context_base.cc;l=112;drc=a3cb0ac4c71ec04abfeaed199e5d63230eca2551
2012        let timeout_ns = if cfg!(any(webgl, Emscripten)) {
2013            0
2014        } else {
2015            timeout
2016                .map(|t| t.as_nanos().min(u32::MAX as u128) as u32)
2017                .unwrap_or(u32::MAX)
2018        };
2019        fence.wait(gl, wait_value, timeout_ns)
2020    }
2021
2022    unsafe fn start_graphics_debugger_capture(&self) -> bool {
2023        #[cfg(all(native, feature = "renderdoc"))]
2024        return unsafe {
2025            self.render_doc
2026                .start_frame_capture(self.shared.context.raw_context(), ptr::null_mut())
2027        };
2028        #[allow(unreachable_code)]
2029        false
2030    }
2031    unsafe fn stop_graphics_debugger_capture(&self) {
2032        #[cfg(all(native, feature = "renderdoc"))]
2033        unsafe {
2034            self.render_doc
2035                .end_frame_capture(ptr::null_mut(), ptr::null_mut())
2036        }
2037    }
2038    unsafe fn create_acceleration_structure(
2039        &self,
2040        _desc: &crate::AccelerationStructureDescriptor,
2041    ) -> Result<super::AccelerationStructure, crate::DeviceError> {
2042        unimplemented!()
2043    }
2044    unsafe fn get_acceleration_structure_build_sizes<'a>(
2045        &self,
2046        _desc: &crate::GetAccelerationStructureBuildSizesDescriptor<'a, super::Buffer>,
2047    ) -> crate::AccelerationStructureBuildSizes {
2048        unimplemented!()
2049    }
2050    unsafe fn get_acceleration_structure_device_address(
2051        &self,
2052        _acceleration_structure: &super::AccelerationStructure,
2053    ) -> wgt::BufferAddress {
2054        unimplemented!()
2055    }
2056    unsafe fn destroy_acceleration_structure(
2057        &self,
2058        _acceleration_structure: super::AccelerationStructure,
2059    ) {
2060    }
2061
2062    fn tlas_instance_to_bytes(&self, _instance: TlasInstance) -> Vec<u8> {
2063        unimplemented!()
2064    }
2065
2066    fn get_internal_counters(&self) -> wgt::HalCounters {
2067        self.counters.as_ref().clone()
2068    }
2069
2070    fn check_if_oom(&self) -> Result<(), crate::DeviceError> {
2071        Ok(())
2072    }
2073}
2074
2075#[cfg(send_sync)]
2076unsafe impl Sync for super::Device {}
2077#[cfg(send_sync)]
2078unsafe impl Send for super::Device {}