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                    // A link puts every uniform block on binding point 0 and
757                    // every sampler on texture unit 0, so a binding at slot 0
758                    // needs no call. A program's first block lookup waits for
759                    // a reply from the GPU process; one whose blocks all sit
760                    // at slot 0 waits for none. On WebGL in Chrome on a
761                    // Huawei Mate 20 X, 7 of the 17 programs that the
762                    // gauntlet's first frame reads are such programs.
763                    super::BindingRegister::UniformBuffers
764                    | super::BindingRegister::Textures
765                    | super::BindingRegister::Images
766                        if slot == 0 => {}
767                    super::BindingRegister::UniformBuffers => {
768                        match unsafe { gl.get_uniform_block_index(program, name) } {
769                            Some(index) => {
770                                log::trace!("\tBinding slot {slot} to block index {index}");
771                                unsafe { gl.uniform_block_binding(program, index, slot as _) };
772                            }
773                            None => log::error!("No uniform block {name} in program {program:?}"),
774                        }
775                    }
776                    super::BindingRegister::StorageBuffers => {
777                        log::error!("Unable to re-map shader storage block {name}");
778                    }
779                    super::BindingRegister::Textures | super::BindingRegister::Images => {
780                        let location = unsafe { gl.get_uniform_location(program, name) };
781                        unsafe { gl.uniform_1_i32(location.as_ref(), slot as _) };
782                    }
783                }
784            }
785        }
786
787        let mut immediates_descs = ArrayVec::new();
788        for item in self.immediates_items.iter().flatten() {
789            let location = unsafe { gl.get_uniform_location(program, &item.access_path) };
790
791            log::trace!(
792                "immediate data item: name={}, ty={:?}, offset={}, location={:?}",
793                item.access_path,
794                item.ty,
795                item.offset,
796                location,
797            );
798
799            if let Some(location) = location {
800                immediates_descs.push(super::ImmediateDesc {
801                    location,
802                    offset: item.offset,
803                    ty: item.ty,
804                });
805            }
806        }
807
808        let first_instance_location = if self.has_stages.contains(wgt::ShaderStages::VERTEX) {
809            // If this returns none (the uniform isn't active), that's fine, we just won't set it.
810            unsafe { gl.get_uniform_location(program, naga::back::glsl::FIRST_INSTANCE_BINDING) }
811        } else {
812            None
813        };
814
815        super::LinkedProgram {
816            first_instance_location,
817            immediates_descs,
818        }
819    }
820
821    unsafe fn delete_shaders(&self, gl: &glow::Context) {
822        for &(shader, _) in self.shaders.iter() {
823            unsafe { gl.delete_shader(shader) };
824        }
825    }
826}
827
828impl super::PipelineInner {
829    /// Reads the program's link once, when a submission first uses it.
830    /// `unread` counts the programs whose link no command read yet.
831    pub(super) unsafe fn read_link(
832        &self,
833        gl: &glow::Context,
834        unread: &AtomicUsize,
835    ) -> MutexGuard<'_, super::ProgramLink> {
836        let mut link = self.link.lock();
837        if let Some(pending) = link.pending.take() {
838            unread.fetch_sub(1, Ordering::Relaxed);
839            link.linked = Some(Arc::new(unsafe { pending.finish(gl, self.program) }));
840        }
841        link
842    }
843
844    /// What the linked program tells, read at its first use.
845    pub(super) unsafe fn linked(
846        &self,
847        gl: &glow::Context,
848        unread: &AtomicUsize,
849    ) -> Arc<super::LinkedProgram> {
850        unsafe { self.read_link(gl, unread) }
851            .linked
852            .clone()
853            .unwrap_or_default()
854    }
855
856    /// Deletes the program, with its shaders when no command used it.
857    unsafe fn delete(&self, gl: &glow::Context, unread: &AtomicUsize) {
858        if let Some(pending) = self.link.lock().pending.take() {
859            unread.fetch_sub(1, Ordering::Relaxed);
860            unsafe { pending.delete_shaders(gl) };
861        }
862        unsafe { gl.delete_program(self.program) };
863    }
864}
865
866impl crate::Device for super::Device {
867    type A = super::Api;
868
869    unsafe fn create_buffer(
870        &self,
871        desc: &crate::BufferDescriptor,
872    ) -> Result<super::Buffer, crate::DeviceError> {
873        let target = if desc.usage.contains(wgt::BufferUses::INDEX) {
874            glow::ELEMENT_ARRAY_BUFFER
875        } else {
876            glow::ARRAY_BUFFER
877        };
878
879        let emulate_map = self
880            .shared
881            .workarounds
882            .contains(super::Workarounds::EMULATE_BUFFER_MAP)
883            || !self
884                .shared
885                .private_caps
886                .contains(PrivateCapabilities::BUFFER_ALLOCATION);
887
888        if emulate_map && desc.usage.intersects(wgt::BufferUses::MAP_WRITE) {
889            return Ok(super::Buffer {
890                raw: None,
891                target,
892                size: desc.size,
893                map_flags: 0,
894                map_state: Arc::new(MaybeMutex::new(super::BufferMapState {
895                    mapped: false,
896                    data: Some(vec![0; desc.size as usize]),
897                    offset_of_current_mapping: 0,
898                })),
899                drop_guard: None,
900            });
901        }
902
903        let gl = &self.shared.context.lock();
904
905        let target = if desc.usage.contains(wgt::BufferUses::INDEX) {
906            glow::ELEMENT_ARRAY_BUFFER
907        } else {
908            glow::ARRAY_BUFFER
909        };
910
911        let is_host_visible = desc
912            .usage
913            .intersects(wgt::BufferUses::MAP_READ | wgt::BufferUses::MAP_WRITE);
914        let is_coherent = desc
915            .memory_flags
916            .contains(crate::MemoryFlags::PREFER_COHERENT);
917
918        let mut map_flags = 0;
919        if desc.usage.contains(wgt::BufferUses::MAP_READ) {
920            map_flags |= glow::MAP_READ_BIT;
921        }
922        if desc.usage.contains(wgt::BufferUses::MAP_WRITE) {
923            map_flags |= glow::MAP_WRITE_BIT;
924        }
925
926        let raw = Some(unsafe { gl.create_buffer() }.map_err(|_| crate::DeviceError::OutOfMemory)?);
927        unsafe { gl.bind_buffer(target, raw) };
928        let raw_size = desc
929            .size
930            .try_into()
931            .map_err(|_| crate::DeviceError::OutOfMemory)?;
932
933        if self
934            .shared
935            .private_caps
936            .contains(PrivateCapabilities::BUFFER_ALLOCATION)
937        {
938            if is_host_visible {
939                map_flags |= glow::MAP_PERSISTENT_BIT;
940                if is_coherent {
941                    map_flags |= glow::MAP_COHERENT_BIT;
942                }
943            }
944            // TODO: may also be required for other calls involving `buffer_sub_data_u8_slice` (e.g. copy buffer to buffer and clear buffer)
945            if desc.usage.intersects(wgt::BufferUses::QUERY_RESOLVE) {
946                map_flags |= glow::DYNAMIC_STORAGE_BIT;
947            }
948            unsafe { gl.buffer_storage(target, raw_size, None, map_flags) };
949        } else {
950            assert!(!is_coherent);
951            let usage = if is_host_visible {
952                if desc.usage.contains(wgt::BufferUses::MAP_READ) {
953                    glow::STREAM_READ
954                } else {
955                    glow::DYNAMIC_DRAW
956                }
957            } else {
958                // Even if the usage doesn't contain SRC_READ, we update it internally at least once
959                // Some vendors take usage very literally and STATIC_DRAW will freeze us with an empty buffer
960                // https://github.com/gfx-rs/wgpu/issues/3371
961                glow::DYNAMIC_DRAW
962            };
963            unsafe { gl.buffer_data_size(target, raw_size, usage) };
964        }
965
966        unsafe { gl.bind_buffer(target, None) };
967
968        if !is_coherent && desc.usage.contains(wgt::BufferUses::MAP_WRITE) {
969            map_flags |= glow::MAP_FLUSH_EXPLICIT_BIT;
970        }
971        //TODO: do we need `glow::MAP_UNSYNCHRONIZED_BIT`?
972
973        #[cfg(native)]
974        if let Some(label) = desc.label {
975            if self
976                .shared
977                .private_caps
978                .contains(PrivateCapabilities::DEBUG_FNS)
979            {
980                let name = raw.map_or(0, |buf| buf.0.get());
981                unsafe { gl.object_label(glow::BUFFER, name, Some(label)) };
982            }
983        }
984
985        let data = if emulate_map && desc.usage.contains(wgt::BufferUses::MAP_READ) {
986            Some(vec![0; desc.size as usize])
987        } else {
988            None
989        };
990
991        self.counters.buffers.add(1);
992
993        Ok(super::Buffer {
994            raw,
995            target,
996            size: desc.size,
997            map_flags,
998            map_state: Arc::new(MaybeMutex::new(super::BufferMapState {
999                mapped: false,
1000                data,
1001                offset_of_current_mapping: 0,
1002            })),
1003            drop_guard: None,
1004        })
1005    }
1006
1007    unsafe fn destroy_buffer(&self, buffer: super::Buffer) {
1008        if buffer.drop_guard.is_none() {
1009            if let Some(raw) = buffer.raw {
1010                let gl = &self.shared.context.lock();
1011                unsafe { gl.delete_buffer(raw) };
1012            }
1013        }
1014
1015        // For clarity, we explicitly drop the drop guard. Although this has no real semantic effect as the
1016        // end of the scope will drop the drop guard since this function takes ownership of the buffer.
1017        drop(buffer.drop_guard);
1018
1019        self.counters.buffers.sub(1);
1020    }
1021
1022    unsafe fn add_raw_buffer(&self, _buffer: &super::Buffer) {
1023        self.counters.buffers.add(1);
1024    }
1025
1026    unsafe fn map_buffer(
1027        &self,
1028        buffer: &super::Buffer,
1029        range: crate::MemoryRange,
1030    ) -> Result<crate::BufferMapping, crate::DeviceError> {
1031        let is_coherent = buffer.map_flags & glow::MAP_COHERENT_BIT != 0;
1032        let ptr = match buffer.raw {
1033            None => {
1034                let mut map_state = lock(&buffer.map_state);
1035                let vec = map_state.data.as_mut().unwrap();
1036                let slice = &mut vec.as_mut_slice()[range.start as usize..range.end as usize];
1037                slice.as_mut_ptr()
1038            }
1039            Some(raw) => {
1040                let gl = &self.shared.context.lock();
1041                unsafe { gl.bind_buffer(buffer.target, Some(raw)) };
1042                let mut map_state = lock(&buffer.map_state);
1043                let ptr = if let Some(map_read_allocation) = map_state.data.as_mut() {
1044                    let slice = map_read_allocation.as_mut_slice();
1045                    unsafe { self.shared.get_buffer_sub_data(gl, buffer.target, 0, slice) };
1046                    slice.as_mut_ptr()
1047                } else {
1048                    map_state.offset_of_current_mapping = range.start;
1049                    // glMapBufferRange throws an error if length is 0.
1050                    // We want to allow mapping 0-sized buffer slices, so perform a workaround
1051                    // if the range length is 0. The resulting pointer must never be dereferenced.
1052                    let range_start: i32 = range
1053                        .start
1054                        .try_into()
1055                        .expect("Buffer range invalid for GLES");
1056                    let range_length: i32 = (range.end - range.start)
1057                        .try_into()
1058                        .expect("Buffer range invalid for GLES");
1059                    if range_length != 0 {
1060                        map_state.mapped = true;
1061                        unsafe {
1062                            gl.map_buffer_range(
1063                                buffer.target,
1064                                range_start,
1065                                range_length,
1066                                buffer.map_flags,
1067                            )
1068                        }
1069                    } else {
1070                        ptr::dangling_mut()
1071                    }
1072                };
1073                unsafe { gl.bind_buffer(buffer.target, None) };
1074                ptr
1075            }
1076        };
1077        Ok(crate::BufferMapping {
1078            ptr: ptr::NonNull::new(ptr).ok_or(crate::DeviceError::Lost)?,
1079            is_coherent,
1080        })
1081    }
1082    unsafe fn unmap_buffer(&self, buffer: &super::Buffer) {
1083        let gl = &self.shared.context.lock();
1084        let mut map_state = lock(&buffer.map_state);
1085        if core::mem::replace(&mut map_state.mapped, false) {
1086            if let Some(raw) = buffer.raw {
1087                if map_state.data.is_none() {
1088                    unsafe { gl.bind_buffer(buffer.target, Some(raw)) };
1089                    unsafe { gl.unmap_buffer(buffer.target) };
1090                    unsafe { gl.bind_buffer(buffer.target, None) };
1091                    map_state.offset_of_current_mapping = 0;
1092                }
1093            }
1094        }
1095    }
1096    unsafe fn flush_mapped_ranges<I>(&self, buffer: &super::Buffer, ranges: I)
1097    where
1098        I: Iterator<Item = crate::MemoryRange>,
1099    {
1100        let gl = &self.shared.context.lock();
1101        let map_state = lock(&buffer.map_state);
1102        if map_state.mapped {
1103            if let Some(raw) = buffer.raw {
1104                if map_state.data.is_none() {
1105                    unsafe { gl.bind_buffer(buffer.target, Some(raw)) };
1106                    for range in ranges {
1107                        let offset_of_current_mapping = map_state.offset_of_current_mapping;
1108                        unsafe {
1109                            gl.flush_mapped_buffer_range(
1110                                buffer.target,
1111                                (range.start - offset_of_current_mapping) as i32,
1112                                (range.end - range.start) as i32,
1113                            )
1114                        };
1115                    }
1116                }
1117            }
1118        }
1119    }
1120    unsafe fn invalidate_mapped_ranges<I>(&self, _buffer: &super::Buffer, _ranges: I) {
1121        //TODO: do we need to do anything?
1122    }
1123
1124    unsafe fn create_texture(
1125        &self,
1126        desc: &crate::TextureDescriptor,
1127    ) -> Result<super::Texture, crate::DeviceError> {
1128        let gl = &self.shared.context.lock();
1129
1130        let render_usage = wgt::TextureUses::COLOR_TARGET
1131            | wgt::TextureUses::DEPTH_STENCIL_WRITE
1132            | wgt::TextureUses::DEPTH_STENCIL_READ
1133            | wgt::TextureUses::TRANSIENT;
1134        let format_desc = self.shared.describe_texture_format(desc.format);
1135
1136        let inner = if render_usage.contains(desc.usage)
1137            && desc.dimension == wgt::TextureDimension::D2
1138            && desc.size.depth_or_array_layers == 1
1139        {
1140            let raw = unsafe { gl.create_renderbuffer().unwrap() };
1141            unsafe { gl.bind_renderbuffer(glow::RENDERBUFFER, Some(raw)) };
1142            if desc.sample_count > 1 {
1143                unsafe {
1144                    gl.renderbuffer_storage_multisample(
1145                        glow::RENDERBUFFER,
1146                        desc.sample_count as i32,
1147                        format_desc.internal,
1148                        desc.size.width as i32,
1149                        desc.size.height as i32,
1150                    )
1151                };
1152            } else {
1153                unsafe {
1154                    gl.renderbuffer_storage(
1155                        glow::RENDERBUFFER,
1156                        format_desc.internal,
1157                        desc.size.width as i32,
1158                        desc.size.height as i32,
1159                    )
1160                };
1161            }
1162
1163            #[cfg(native)]
1164            if let Some(label) = desc.label {
1165                if self
1166                    .shared
1167                    .private_caps
1168                    .contains(PrivateCapabilities::DEBUG_FNS)
1169                {
1170                    let name = raw.0.get();
1171                    unsafe { gl.object_label(glow::RENDERBUFFER, name, Some(label)) };
1172                }
1173            }
1174
1175            unsafe { gl.bind_renderbuffer(glow::RENDERBUFFER, None) };
1176            super::TextureInner::Renderbuffer { raw }
1177        } else {
1178            let raw = unsafe { gl.create_texture().unwrap() };
1179            let target = super::Texture::get_info_from_desc(desc);
1180
1181            unsafe { gl.bind_texture(target, Some(raw)) };
1182            //Note: this has to be done before defining the storage!
1183            match desc.format.sample_type(None, Some(self.shared.features)) {
1184                Some(
1185                    wgt::TextureSampleType::Float { filterable: false }
1186                    | wgt::TextureSampleType::Uint
1187                    | wgt::TextureSampleType::Sint,
1188                ) => {
1189                    // reset default filtering mode
1190                    unsafe {
1191                        gl.tex_parameter_i32(target, glow::TEXTURE_MIN_FILTER, glow::NEAREST as i32)
1192                    };
1193                    unsafe {
1194                        gl.tex_parameter_i32(target, glow::TEXTURE_MAG_FILTER, glow::NEAREST as i32)
1195                    };
1196                }
1197                _ => {}
1198            }
1199
1200            if conv::is_layered_target(target) {
1201                unsafe {
1202                    if self
1203                        .shared
1204                        .private_caps
1205                        .contains(PrivateCapabilities::TEXTURE_STORAGE)
1206                    {
1207                        gl.tex_storage_3d(
1208                            target,
1209                            desc.mip_level_count as i32,
1210                            format_desc.internal,
1211                            desc.size.width as i32,
1212                            desc.size.height as i32,
1213                            desc.size.depth_or_array_layers as i32,
1214                        )
1215                    } else if target == glow::TEXTURE_3D {
1216                        let mut width = desc.size.width;
1217                        let mut height = desc.size.height;
1218                        let mut depth = desc.size.depth_or_array_layers;
1219                        for i in 0..desc.mip_level_count {
1220                            gl.tex_image_3d(
1221                                target,
1222                                i as i32,
1223                                format_desc.internal as i32,
1224                                width as i32,
1225                                height as i32,
1226                                depth as i32,
1227                                0,
1228                                format_desc.external,
1229                                format_desc.data_type,
1230                                glow::PixelUnpackData::Slice(None),
1231                            );
1232                            width = max(1, width / 2);
1233                            height = max(1, height / 2);
1234                            depth = max(1, depth / 2);
1235                        }
1236                    } else {
1237                        let mut width = desc.size.width;
1238                        let mut height = desc.size.height;
1239                        for i in 0..desc.mip_level_count {
1240                            gl.tex_image_3d(
1241                                target,
1242                                i as i32,
1243                                format_desc.internal as i32,
1244                                width as i32,
1245                                height as i32,
1246                                desc.size.depth_or_array_layers as i32,
1247                                0,
1248                                format_desc.external,
1249                                format_desc.data_type,
1250                                glow::PixelUnpackData::Slice(None),
1251                            );
1252                            width = max(1, width / 2);
1253                            height = max(1, height / 2);
1254                        }
1255                    }
1256                };
1257            } else if desc.sample_count > 1 {
1258                unsafe {
1259                    gl.tex_storage_2d_multisample(
1260                        target,
1261                        desc.sample_count as i32,
1262                        format_desc.internal,
1263                        desc.size.width as i32,
1264                        desc.size.height as i32,
1265                        true,
1266                    )
1267                };
1268            } else {
1269                unsafe {
1270                    if self
1271                        .shared
1272                        .private_caps
1273                        .contains(PrivateCapabilities::TEXTURE_STORAGE)
1274                    {
1275                        gl.tex_storage_2d(
1276                            target,
1277                            desc.mip_level_count as i32,
1278                            format_desc.internal,
1279                            desc.size.width as i32,
1280                            desc.size.height as i32,
1281                        )
1282                    } else if target == glow::TEXTURE_CUBE_MAP {
1283                        let mut width = desc.size.width;
1284                        let mut height = desc.size.height;
1285                        for i in 0..desc.mip_level_count {
1286                            for face in [
1287                                glow::TEXTURE_CUBE_MAP_POSITIVE_X,
1288                                glow::TEXTURE_CUBE_MAP_NEGATIVE_X,
1289                                glow::TEXTURE_CUBE_MAP_POSITIVE_Y,
1290                                glow::TEXTURE_CUBE_MAP_NEGATIVE_Y,
1291                                glow::TEXTURE_CUBE_MAP_POSITIVE_Z,
1292                                glow::TEXTURE_CUBE_MAP_NEGATIVE_Z,
1293                            ] {
1294                                gl.tex_image_2d(
1295                                    face,
1296                                    i as i32,
1297                                    format_desc.internal as i32,
1298                                    width as i32,
1299                                    height as i32,
1300                                    0,
1301                                    format_desc.external,
1302                                    format_desc.data_type,
1303                                    glow::PixelUnpackData::Slice(None),
1304                                );
1305                            }
1306                            width = max(1, width / 2);
1307                            height = max(1, height / 2);
1308                        }
1309                    } else {
1310                        let mut width = desc.size.width;
1311                        let mut height = desc.size.height;
1312                        for i in 0..desc.mip_level_count {
1313                            gl.tex_image_2d(
1314                                target,
1315                                i as i32,
1316                                format_desc.internal as i32,
1317                                width as i32,
1318                                height as i32,
1319                                0,
1320                                format_desc.external,
1321                                format_desc.data_type,
1322                                glow::PixelUnpackData::Slice(None),
1323                            );
1324                            width = max(1, width / 2);
1325                            height = max(1, height / 2);
1326                        }
1327                    }
1328                };
1329            }
1330
1331            #[cfg(native)]
1332            if let Some(label) = desc.label {
1333                if self
1334                    .shared
1335                    .private_caps
1336                    .contains(PrivateCapabilities::DEBUG_FNS)
1337                {
1338                    let name = raw.0.get();
1339                    unsafe { gl.object_label(glow::TEXTURE, name, Some(label)) };
1340                }
1341            }
1342
1343            unsafe { gl.bind_texture(target, None) };
1344            super::TextureInner::Texture { raw, target }
1345        };
1346
1347        self.counters.textures.add(1);
1348
1349        Ok(super::Texture {
1350            inner,
1351            drop_guard: None,
1352            mip_level_count: desc.mip_level_count,
1353            array_layer_count: desc.array_layer_count(),
1354            format: desc.format,
1355            format_desc,
1356            copy_size: desc.copy_extent(),
1357        })
1358    }
1359
1360    unsafe fn destroy_texture(&self, texture: super::Texture) {
1361        if texture.drop_guard.is_none() {
1362            let gl = &self.shared.context.lock();
1363            match texture.inner {
1364                super::TextureInner::Renderbuffer { raw, .. } => {
1365                    unsafe { gl.delete_renderbuffer(raw) };
1366                }
1367                super::TextureInner::DefaultRenderbuffer => {}
1368                super::TextureInner::Texture { raw, .. } => {
1369                    unsafe { gl.delete_texture(raw) };
1370                }
1371                #[cfg(webgl)]
1372                super::TextureInner::ExternalFramebuffer { .. } => {}
1373                #[cfg(native)]
1374                super::TextureInner::ExternalNativeFramebuffer { .. } => {}
1375            }
1376        }
1377
1378        // For clarity, we explicitly drop the drop guard. Although this has no real semantic effect as the
1379        // end of the scope will drop the drop guard since this function takes ownership of the texture.
1380        drop(texture.drop_guard);
1381
1382        self.counters.textures.sub(1);
1383    }
1384
1385    unsafe fn add_raw_texture(&self, _texture: &super::Texture) {
1386        self.counters.textures.add(1);
1387    }
1388
1389    unsafe fn create_texture_view(
1390        &self,
1391        texture: &super::Texture,
1392        desc: &crate::TextureViewDescriptor,
1393    ) -> Result<super::TextureView, crate::DeviceError> {
1394        self.counters.texture_views.add(1);
1395        Ok(super::TextureView {
1396            //TODO: use `conv::map_view_dimension(desc.dimension)`?
1397            inner: texture.inner.clone(),
1398            aspects: crate::FormatAspects::new(texture.format, desc.range.aspect),
1399            mip_levels: desc.range.mip_range(texture.mip_level_count),
1400            array_layers: desc.range.layer_range(texture.array_layer_count),
1401            format: texture.format,
1402        })
1403    }
1404
1405    unsafe fn destroy_texture_view(&self, _view: super::TextureView) {
1406        self.counters.texture_views.sub(1);
1407    }
1408
1409    unsafe fn create_sampler(
1410        &self,
1411        desc: &crate::SamplerDescriptor,
1412    ) -> Result<super::Sampler, crate::DeviceError> {
1413        let gl = &self.shared.context.lock();
1414
1415        let raw = unsafe { gl.create_sampler().unwrap() };
1416
1417        let (min, mag) =
1418            conv::map_filter_modes(desc.min_filter, desc.mag_filter, desc.mipmap_filter);
1419
1420        unsafe { gl.sampler_parameter_i32(raw, glow::TEXTURE_MIN_FILTER, min as i32) };
1421        unsafe { gl.sampler_parameter_i32(raw, glow::TEXTURE_MAG_FILTER, mag as i32) };
1422
1423        unsafe {
1424            gl.sampler_parameter_i32(
1425                raw,
1426                glow::TEXTURE_WRAP_S,
1427                conv::map_address_mode(desc.address_modes[0]) as i32,
1428            )
1429        };
1430        unsafe {
1431            gl.sampler_parameter_i32(
1432                raw,
1433                glow::TEXTURE_WRAP_T,
1434                conv::map_address_mode(desc.address_modes[1]) as i32,
1435            )
1436        };
1437        unsafe {
1438            gl.sampler_parameter_i32(
1439                raw,
1440                glow::TEXTURE_WRAP_R,
1441                conv::map_address_mode(desc.address_modes[2]) as i32,
1442            )
1443        };
1444
1445        if let Some(border_color) = desc.border_color {
1446            let border = match border_color {
1447                wgt::SamplerBorderColor::TransparentBlack | wgt::SamplerBorderColor::Zero => {
1448                    [0.0; 4]
1449                }
1450                wgt::SamplerBorderColor::OpaqueBlack => [0.0, 0.0, 0.0, 1.0],
1451                wgt::SamplerBorderColor::OpaqueWhite => [1.0; 4],
1452            };
1453            unsafe { gl.sampler_parameter_f32_slice(raw, glow::TEXTURE_BORDER_COLOR, &border) };
1454        }
1455
1456        unsafe { gl.sampler_parameter_f32(raw, glow::TEXTURE_MIN_LOD, desc.lod_clamp.start) };
1457        unsafe { gl.sampler_parameter_f32(raw, glow::TEXTURE_MAX_LOD, desc.lod_clamp.end) };
1458
1459        // If clamp is not 1, we know anisotropy is supported up to 16x
1460        if desc.anisotropy_clamp != 1 {
1461            unsafe {
1462                gl.sampler_parameter_i32(
1463                    raw,
1464                    glow::TEXTURE_MAX_ANISOTROPY,
1465                    desc.anisotropy_clamp as i32,
1466                )
1467            };
1468        }
1469
1470        //set_param_float(glow::TEXTURE_LOD_BIAS, info.lod_bias.0);
1471
1472        if let Some(compare) = desc.compare {
1473            unsafe {
1474                gl.sampler_parameter_i32(
1475                    raw,
1476                    glow::TEXTURE_COMPARE_MODE,
1477                    glow::COMPARE_REF_TO_TEXTURE as i32,
1478                )
1479            };
1480            unsafe {
1481                gl.sampler_parameter_i32(
1482                    raw,
1483                    glow::TEXTURE_COMPARE_FUNC,
1484                    conv::map_compare_func(compare) as i32,
1485                )
1486            };
1487        }
1488
1489        #[cfg(native)]
1490        if let Some(label) = desc.label {
1491            if self
1492                .shared
1493                .private_caps
1494                .contains(PrivateCapabilities::DEBUG_FNS)
1495            {
1496                let name = raw.0.get();
1497                unsafe { gl.object_label(glow::SAMPLER, name, Some(label)) };
1498            }
1499        }
1500
1501        self.counters.samplers.add(1);
1502
1503        Ok(super::Sampler { raw })
1504    }
1505
1506    unsafe fn destroy_sampler(&self, sampler: super::Sampler) {
1507        let gl = &self.shared.context.lock();
1508        unsafe { gl.delete_sampler(sampler.raw) };
1509        self.counters.samplers.sub(1);
1510    }
1511
1512    unsafe fn create_command_encoder(
1513        &self,
1514        _desc: &crate::CommandEncoderDescriptor<super::Queue>,
1515    ) -> Result<super::CommandEncoder, crate::DeviceError> {
1516        self.counters.command_encoders.add(1);
1517
1518        Ok(super::CommandEncoder {
1519            cmd_buffer: super::CommandBuffer::default(),
1520            state: Default::default(),
1521            private_caps: self.shared.private_caps,
1522            counters: Arc::clone(&self.counters),
1523        })
1524    }
1525
1526    unsafe fn create_bind_group_layout(
1527        &self,
1528        desc: &crate::BindGroupLayoutDescriptor,
1529    ) -> Result<super::BindGroupLayout, crate::DeviceError> {
1530        self.counters.bind_group_layouts.add(1);
1531        Ok(super::BindGroupLayout {
1532            entries: Arc::from(desc.entries),
1533        })
1534    }
1535
1536    unsafe fn destroy_bind_group_layout(&self, _bg_layout: super::BindGroupLayout) {
1537        self.counters.bind_group_layouts.sub(1);
1538    }
1539
1540    unsafe fn create_pipeline_layout(
1541        &self,
1542        desc: &crate::PipelineLayoutDescriptor<super::BindGroupLayout>,
1543    ) -> Result<super::PipelineLayout, crate::DeviceError> {
1544        use naga::back::glsl;
1545
1546        let mut group_infos = Vec::with_capacity(desc.bind_group_layouts.len());
1547        let mut num_samplers = 0u8;
1548        let mut num_textures = 0u8;
1549        let mut num_images = 0u8;
1550        let mut num_uniform_buffers = 0u8;
1551        let mut num_storage_buffers = 0u8;
1552
1553        let mut writer_flags = glsl::WriterFlags::ADJUST_COORDINATE_SPACE;
1554        writer_flags.set(
1555            glsl::WriterFlags::TEXTURE_SHADOW_LOD,
1556            self.shared
1557                .private_caps
1558                .contains(PrivateCapabilities::SHADER_TEXTURE_SHADOW_LOD),
1559        );
1560        writer_flags.set(
1561            glsl::WriterFlags::DRAW_PARAMETERS,
1562            self.shared
1563                .private_caps
1564                .contains(PrivateCapabilities::FULLY_FEATURED_INSTANCING),
1565        );
1566        // A point list pipeline writes the point size (`create_shader`); no
1567        // other one does, as WebGL counts `gl_PointSize` against the 15
1568        // varying vectors a program may link with.
1569        let mut binding_map = glsl::BindingMap::default();
1570
1571        for (group_index, bg_layout) in desc.bind_group_layouts.iter().enumerate() {
1572            let Some(bg_layout) = bg_layout else {
1573                group_infos.push(None);
1574                continue;
1575            };
1576
1577            // create a vector with the size enough to hold all the bindings, filled with `!0`
1578            let mut binding_to_slot = vec![
1579                !0;
1580                bg_layout
1581                    .entries
1582                    .iter()
1583                    .map(|b| b.binding)
1584                    .max()
1585                    .map_or(0, |idx| idx as usize + 1)
1586            ]
1587            .into_boxed_slice();
1588
1589            for entry in bg_layout.entries.iter() {
1590                let counter = match entry.ty {
1591                    wgt::BindingType::Sampler { .. } => &mut num_samplers,
1592                    wgt::BindingType::Texture { .. } => &mut num_textures,
1593                    wgt::BindingType::StorageTexture { .. } => &mut num_images,
1594                    wgt::BindingType::Buffer {
1595                        ty: wgt::BufferBindingType::Uniform,
1596                        ..
1597                    } => &mut num_uniform_buffers,
1598                    wgt::BindingType::Buffer {
1599                        ty: wgt::BufferBindingType::Storage { .. },
1600                        ..
1601                    } => &mut num_storage_buffers,
1602                    wgt::BindingType::AccelerationStructure { .. } => unimplemented!(),
1603                    wgt::BindingType::ExternalTexture => unimplemented!(),
1604                };
1605
1606                binding_to_slot[entry.binding as usize] = *counter;
1607                let br = naga::ResourceBinding {
1608                    group: group_index as u32,
1609                    binding: entry.binding,
1610                };
1611                binding_map.insert(br, *counter);
1612                *counter += entry.count.map_or(1, |c| c.get() as u8);
1613            }
1614
1615            group_infos.push(Some(super::BindGroupLayoutInfo {
1616                entries: Arc::clone(&bg_layout.entries),
1617                binding_to_slot,
1618            }));
1619        }
1620
1621        self.counters.pipeline_layouts.add(1);
1622
1623        Ok(super::PipelineLayout {
1624            group_infos: group_infos.into_boxed_slice(),
1625            naga_options: glsl::Options {
1626                version: self.shared.shading_language_version,
1627                writer_flags,
1628                binding_map,
1629                zero_initialize_workgroup_memory: true,
1630            },
1631        })
1632    }
1633
1634    unsafe fn destroy_pipeline_layout(&self, _pipeline_layout: super::PipelineLayout) {
1635        self.counters.pipeline_layouts.sub(1);
1636    }
1637
1638    unsafe fn create_bind_group(
1639        &self,
1640        desc: &crate::BindGroupDescriptor<
1641            super::BindGroupLayout,
1642            super::Buffer,
1643            super::Sampler,
1644            super::TextureView,
1645            super::AccelerationStructure,
1646        >,
1647    ) -> Result<super::BindGroup, crate::DeviceError> {
1648        let mut contents = Vec::new();
1649
1650        let layout_and_entry_iter = desc.entries.iter().map(|entry| {
1651            let layout = desc
1652                .layout
1653                .entries
1654                .iter()
1655                .find(|layout_entry| layout_entry.binding == entry.binding)
1656                .expect("internal error: no layout entry found with binding slot");
1657            (entry, layout)
1658        });
1659        for (entry, layout) in layout_and_entry_iter {
1660            let binding = match layout.ty {
1661                wgt::BindingType::Buffer { .. } => {
1662                    let bb = &desc.buffers[entry.resource_index as usize];
1663                    super::RawBinding::Buffer {
1664                        raw: bb.buffer.raw.unwrap(),
1665                        offset: bb.offset as i32,
1666                        size: match bb.size {
1667                            Some(s) => s.get() as i32,
1668                            None => (bb.buffer.size - bb.offset) as i32,
1669                        },
1670                    }
1671                }
1672                wgt::BindingType::Sampler { .. } => {
1673                    let sampler = desc.samplers[entry.resource_index as usize];
1674                    super::RawBinding::Sampler(sampler.raw)
1675                }
1676                wgt::BindingType::Texture { view_dimension, .. } => {
1677                    let view = desc.textures[entry.resource_index as usize].view;
1678                    if view.array_layers.start != 0 {
1679                        log::error!("Unable to create a sampled texture binding for non-zero array layer.\n{}",
1680                            "This is an implementation problem of wgpu-hal/gles backend.")
1681                    }
1682                    let (raw, target) = view.inner.as_native();
1683
1684                    super::Texture::log_failing_target_heuristics(view_dimension, target);
1685
1686                    super::RawBinding::Texture {
1687                        raw,
1688                        target,
1689                        aspects: view.aspects,
1690                        mip_levels: view.mip_levels.clone(),
1691                    }
1692                }
1693                wgt::BindingType::StorageTexture {
1694                    access,
1695                    format,
1696                    view_dimension,
1697                } => {
1698                    let view = desc.textures[entry.resource_index as usize].view;
1699                    let format_desc = self.shared.describe_texture_format(format);
1700                    let (raw, _target) = view.inner.as_native();
1701                    super::RawBinding::Image(super::ImageBinding {
1702                        raw,
1703                        mip_level: view.mip_levels.start,
1704                        array_layer: match view_dimension {
1705                            wgt::TextureViewDimension::D2Array
1706                            | wgt::TextureViewDimension::CubeArray => None,
1707                            _ => Some(view.array_layers.start),
1708                        },
1709                        access: conv::map_storage_access(access),
1710                        format: format_desc.internal,
1711                    })
1712                }
1713                wgt::BindingType::AccelerationStructure { .. } => unimplemented!(),
1714                wgt::BindingType::ExternalTexture => unimplemented!(),
1715            };
1716            contents.push(binding);
1717        }
1718
1719        self.counters.bind_groups.add(1);
1720
1721        Ok(super::BindGroup {
1722            contents: contents.into_boxed_slice(),
1723        })
1724    }
1725
1726    unsafe fn destroy_bind_group(&self, _group: super::BindGroup) {
1727        self.counters.bind_groups.sub(1);
1728    }
1729
1730    unsafe fn create_shader_module(
1731        &self,
1732        desc: &crate::ShaderModuleDescriptor,
1733        shader: crate::ShaderInput,
1734    ) -> Result<super::ShaderModule, crate::ShaderError> {
1735        self.counters.shader_modules.add(1);
1736
1737        Ok(super::ShaderModule {
1738            source: match shader {
1739                crate::ShaderInput::Naga(naga) => super::ShaderModuleSource::Naga(naga),
1740                // The backend doesn't yet expose this feature so it should be fine
1741                crate::ShaderInput::Glsl { shader, .. } => super::ShaderModuleSource::Passthrough {
1742                    source: shader.to_owned(),
1743                },
1744                crate::ShaderInput::GlslTemplates(templates) => {
1745                    super::ShaderModuleSource::Templates(templates)
1746                }
1747                crate::ShaderInput::SpirV(_)
1748                | crate::ShaderInput::MetalLib { .. }
1749                | crate::ShaderInput::Msl { .. }
1750                | crate::ShaderInput::Dxil { .. }
1751                | crate::ShaderInput::Hlsl { .. } => {
1752                    unreachable!()
1753                }
1754            },
1755            label: desc.label.map(|str| str.to_string()),
1756            id: self.shared.next_shader_id.fetch_add(1, Ordering::Relaxed),
1757        })
1758    }
1759
1760    unsafe fn destroy_shader_module(&self, _module: super::ShaderModule) {
1761        self.counters.shader_modules.sub(1);
1762    }
1763
1764    unsafe fn create_render_pipeline(
1765        &self,
1766        desc: &crate::RenderPipelineDescriptor<
1767            super::PipelineLayout,
1768            super::ShaderModule,
1769            super::PipelineCache,
1770        >,
1771    ) -> Result<super::RenderPipeline, crate::PipelineError> {
1772        let (vertex_stage, vertex_buffers) = match &desc.vertex_processor {
1773            crate::VertexProcessor::Standard {
1774                vertex_buffers,
1775                ref vertex_stage,
1776            } => (vertex_stage, vertex_buffers),
1777            crate::VertexProcessor::Mesh { .. } => unreachable!(),
1778        };
1779        let gl = &self.shared.context.lock();
1780        let mut shaders = ArrayVec::new();
1781        shaders.push((naga::ShaderStage::Vertex, vertex_stage));
1782        if let Some(ref fs) = desc.fragment_stage {
1783            shaders.push((naga::ShaderStage::Fragment, fs));
1784        }
1785        let points = desc.primitive.topology == wgt::PrimitiveTopology::PointList;
1786        let inner = unsafe {
1787            self.create_pipeline(
1788                gl,
1789                shaders,
1790                desc.layout,
1791                desc.label,
1792                desc.multiview_mask,
1793                points,
1794            )
1795        }?;
1796
1797        let (vertex_buffers, vertex_attributes) = {
1798            let mut buffers = Vec::new();
1799            let mut attributes = Vec::new();
1800            for (index, vb_layout) in vertex_buffers.iter().enumerate() {
1801                let vb_desc = if let Some(vb_layout) = vb_layout {
1802                    for vat in vb_layout.attributes.iter() {
1803                        let format_desc = conv::describe_vertex_format(vat.format);
1804                        attributes.push(super::AttributeDesc {
1805                            location: vat.shader_location,
1806                            offset: vat.offset as u32,
1807                            buffer_index: index as u32,
1808                            format_desc,
1809                        });
1810                    }
1811                    Some(super::VertexBufferDesc {
1812                        step: vb_layout.step_mode,
1813                        stride: vb_layout.array_stride as u32,
1814                    })
1815                } else {
1816                    None
1817                };
1818                buffers.push(vb_desc);
1819            }
1820            (buffers.into_boxed_slice(), attributes.into_boxed_slice())
1821        };
1822
1823        let color_targets = {
1824            let mut targets = Vec::new();
1825            for ct in desc.color_targets.iter().filter_map(|at| at.as_ref()) {
1826                targets.push(super::ColorTargetDesc {
1827                    mask: ct.write_mask,
1828                    blend: ct.blend.as_ref().map(conv::map_blend),
1829                });
1830            }
1831            //Note: if any of the states are different, and `INDEPENDENT_BLEND` flag
1832            // is not exposed, then this pipeline will not bind correctly.
1833            targets.into_boxed_slice()
1834        };
1835
1836        self.counters.render_pipelines.add(1);
1837
1838        Ok(super::RenderPipeline {
1839            inner,
1840            primitive: desc.primitive,
1841            vertex_buffers,
1842            vertex_attributes,
1843            color_targets,
1844            depth: desc.depth_stencil.as_ref().map(|ds| super::DepthState {
1845                function: conv::map_compare_func(ds.depth_compare.unwrap_or_default()),
1846                mask: ds.depth_write_enabled.unwrap_or_default(),
1847            }),
1848            depth_bias: desc
1849                .depth_stencil
1850                .as_ref()
1851                .map(|ds| ds.bias)
1852                .unwrap_or_default(),
1853            stencil: desc
1854                .depth_stencil
1855                .as_ref()
1856                .map(|ds| conv::map_stencil(&ds.stencil)),
1857            alpha_to_coverage_enabled: desc.multisample.alpha_to_coverage_enabled,
1858        })
1859    }
1860
1861    unsafe fn destroy_render_pipeline(&self, pipeline: super::RenderPipeline) {
1862        // If the pipeline only has 2 strong references remaining, they're `pipeline` and `program_cache`
1863        // This is safe to assume as long as:
1864        // - `RenderPipeline` can't be cloned
1865        // - The only place that we can get a new reference is during `program_cache.lock()`
1866        if Arc::strong_count(&pipeline.inner) == 2 {
1867            let gl = &self.shared.context.lock();
1868            let mut program_cache = self.shared.program_cache.lock();
1869            program_cache.retain(|_, v| match *v {
1870                Ok(ref p) => p.program != pipeline.inner.program,
1871                Err(_) => false,
1872            });
1873            unsafe { pipeline.inner.delete(gl, &self.shared.unread_links) };
1874        }
1875
1876        self.counters.render_pipelines.sub(1);
1877    }
1878
1879    unsafe fn create_compute_pipeline(
1880        &self,
1881        desc: &crate::ComputePipelineDescriptor<
1882            super::PipelineLayout,
1883            super::ShaderModule,
1884            super::PipelineCache,
1885        >,
1886    ) -> Result<super::ComputePipeline, crate::PipelineError> {
1887        let gl = &self.shared.context.lock();
1888        let mut shaders = ArrayVec::new();
1889        shaders.push((naga::ShaderStage::Compute, &desc.stage));
1890        let inner =
1891            unsafe { self.create_pipeline(gl, shaders, desc.layout, desc.label, None, false) }?;
1892
1893        self.counters.compute_pipelines.add(1);
1894
1895        Ok(super::ComputePipeline { inner })
1896    }
1897
1898    unsafe fn destroy_compute_pipeline(&self, pipeline: super::ComputePipeline) {
1899        // If the pipeline only has 2 strong references remaining, they're `pipeline` and `program_cache``
1900        // This is safe to assume as long as:
1901        // - `ComputePipeline` can't be cloned
1902        // - The only place that we can get a new reference is during `program_cache.lock()`
1903        if Arc::strong_count(&pipeline.inner) == 2 {
1904            let gl = &self.shared.context.lock();
1905            let mut program_cache = self.shared.program_cache.lock();
1906            program_cache.retain(|_, v| match *v {
1907                Ok(ref p) => p.program != pipeline.inner.program,
1908                Err(_) => false,
1909            });
1910            unsafe { pipeline.inner.delete(gl, &self.shared.unread_links) };
1911        }
1912
1913        self.counters.compute_pipelines.sub(1);
1914    }
1915
1916    unsafe fn create_ray_tracing_pipeline(
1917        &self,
1918        _desc: &crate::RayTracingPipelineDescriptor<
1919            super::PipelineLayout,
1920            super::ShaderModule,
1921            super::PipelineCache,
1922        >,
1923    ) -> Result<super::RayTracingPipeline, crate::PipelineError> {
1924        unimplemented!("Ray tracing is unsupported on GL")
1925    }
1926
1927    unsafe fn destroy_ray_tracing_pipeline(&self, _pipeline: super::RayTracingPipeline) {
1928        unimplemented!("Ray tracing is unsupported on GL")
1929    }
1930
1931    unsafe fn get_raytracing_pipeline_group_data(
1932        &self,
1933        _pipeline: &super::RayTracingPipeline,
1934        _groups: core::ops::Range<u32>,
1935    ) -> Result<Vec<u8>, crate::DeviceError> {
1936        unimplemented!("Ray tracing is unsupported on GL")
1937    }
1938
1939    unsafe fn create_pipeline_cache(
1940        &self,
1941        _: &crate::PipelineCacheDescriptor<'_>,
1942    ) -> Result<super::PipelineCache, crate::PipelineCacheError> {
1943        // Even though the cache doesn't do anything, we still return something here
1944        // as the least bad option
1945        Ok(super::PipelineCache)
1946    }
1947    unsafe fn destroy_pipeline_cache(&self, _: super::PipelineCache) {}
1948
1949    #[cfg_attr(target_arch = "wasm32", allow(unused))]
1950    unsafe fn create_query_set(
1951        &self,
1952        desc: &wgt::QuerySetDescriptor<crate::Label>,
1953    ) -> Result<super::QuerySet, crate::DeviceError> {
1954        let gl = &self.shared.context.lock();
1955
1956        let mut queries = Vec::with_capacity(desc.count as usize);
1957        for _ in 0..desc.count {
1958            let query =
1959                unsafe { gl.create_query() }.map_err(|_| crate::DeviceError::OutOfMemory)?;
1960
1961            // We aren't really able to, in general, label queries.
1962            //
1963            // We could take a timestamp here to "initialize" the query,
1964            // but that's a bit of a hack, and we don't want to insert
1965            // random timestamps into the command stream of we don't have to.
1966
1967            queries.push(query);
1968        }
1969
1970        self.counters.query_sets.add(1);
1971
1972        Ok(super::QuerySet {
1973            queries: queries.into_boxed_slice(),
1974            target: match desc.ty {
1975                wgt::QueryType::Occlusion => glow::ANY_SAMPLES_PASSED_CONSERVATIVE,
1976                wgt::QueryType::Timestamp => glow::TIMESTAMP,
1977                _ => unimplemented!(),
1978            },
1979        })
1980    }
1981
1982    unsafe fn destroy_query_set(&self, set: super::QuerySet) {
1983        let gl = &self.shared.context.lock();
1984        for &query in set.queries.iter() {
1985            unsafe { gl.delete_query(query) };
1986        }
1987        self.counters.query_sets.sub(1);
1988    }
1989
1990    unsafe fn create_fence(&self) -> Result<super::Fence, crate::DeviceError> {
1991        self.counters.fences.add(1);
1992        Ok(super::Fence::new(&self.shared.options))
1993    }
1994
1995    unsafe fn destroy_fence(&self, fence: super::Fence) {
1996        let gl = &self.shared.context.lock();
1997        fence.destroy(gl);
1998        self.counters.fences.sub(1);
1999    }
2000
2001    unsafe fn get_fence_value(
2002        &self,
2003        fence: &super::Fence,
2004    ) -> Result<crate::FenceValue, crate::DeviceError> {
2005        #[cfg_attr(target_arch = "wasm32", allow(clippy::needless_borrow))]
2006        Ok(fence.get_latest(&self.shared.context.lock()))
2007    }
2008    unsafe fn wait(
2009        &self,
2010        fence: &super::Fence,
2011        wait_value: crate::FenceValue,
2012        timeout: Option<core::time::Duration>,
2013    ) -> Result<bool, crate::DeviceError> {
2014        if fence.satisfied(wait_value) {
2015            return Ok(true);
2016        }
2017
2018        let gl = &self.shared.context.lock();
2019        // MAX_CLIENT_WAIT_TIMEOUT_WEBGL is:
2020        // - 1s in Gecko https://searchfox.org/mozilla-central/rev/754074e05178e017ef6c3d8e30428ffa8f1b794d/dom/canvas/WebGLTypes.h#1386
2021        // - 0 in WebKit https://github.com/WebKit/WebKit/blob/4ef90d4672ca50267c0971b85db403d9684508ea/Source/WebCore/html/canvas/WebGL2RenderingContext.cpp#L110
2022        // - 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
2023        let timeout_ns = if cfg!(any(webgl, Emscripten)) {
2024            0
2025        } else {
2026            timeout
2027                .map(|t| t.as_nanos().min(u32::MAX as u128) as u32)
2028                .unwrap_or(u32::MAX)
2029        };
2030        fence.wait(gl, wait_value, timeout_ns)
2031    }
2032
2033    unsafe fn start_graphics_debugger_capture(&self) -> bool {
2034        #[cfg(all(native, feature = "renderdoc"))]
2035        return unsafe {
2036            self.render_doc
2037                .start_frame_capture(self.shared.context.raw_context(), ptr::null_mut())
2038        };
2039        #[allow(unreachable_code)]
2040        false
2041    }
2042    unsafe fn stop_graphics_debugger_capture(&self) {
2043        #[cfg(all(native, feature = "renderdoc"))]
2044        unsafe {
2045            self.render_doc
2046                .end_frame_capture(ptr::null_mut(), ptr::null_mut())
2047        }
2048    }
2049    unsafe fn create_acceleration_structure(
2050        &self,
2051        _desc: &crate::AccelerationStructureDescriptor,
2052    ) -> Result<super::AccelerationStructure, crate::DeviceError> {
2053        unimplemented!()
2054    }
2055    unsafe fn get_acceleration_structure_build_sizes<'a>(
2056        &self,
2057        _desc: &crate::GetAccelerationStructureBuildSizesDescriptor<'a, super::Buffer>,
2058    ) -> crate::AccelerationStructureBuildSizes {
2059        unimplemented!()
2060    }
2061    unsafe fn get_acceleration_structure_device_address(
2062        &self,
2063        _acceleration_structure: &super::AccelerationStructure,
2064    ) -> wgt::BufferAddress {
2065        unimplemented!()
2066    }
2067    unsafe fn destroy_acceleration_structure(
2068        &self,
2069        _acceleration_structure: super::AccelerationStructure,
2070    ) {
2071    }
2072
2073    fn tlas_instance_to_bytes(&self, _instance: TlasInstance) -> Vec<u8> {
2074        unimplemented!()
2075    }
2076
2077    fn get_internal_counters(&self) -> wgt::HalCounters {
2078        self.counters.as_ref().clone()
2079    }
2080
2081    fn check_if_oom(&self) -> Result<(), crate::DeviceError> {
2082        Ok(())
2083    }
2084}
2085
2086#[cfg(send_sync)]
2087unsafe impl Sync for super::Device {}
2088#[cfg(send_sync)]
2089unsafe impl Send for super::Device {}