wgpu_core/device/
resource.rs

1use alloc::{
2    borrow::Cow,
3    boxed::Box,
4    string::{String, ToString as _},
5    sync::{Arc, Weak},
6    vec::Vec,
7};
8use core::{
9    fmt,
10    mem::{self, ManuallyDrop},
11    num::NonZeroU32,
12    sync::atomic::{AtomicBool, Ordering},
13};
14use hal::ShouldBeNonZeroExt;
15
16use arrayvec::ArrayVec;
17use bitflags::Flags;
18use smallvec::SmallVec;
19use wgt::{
20    math::align_to, DeviceLostReason, TextureFormat, TextureSampleType, TextureSelector,
21    TextureViewDimension,
22};
23
24#[cfg(feature = "trace")]
25use crate::device::trace;
26use crate::{
27    api_log,
28    binding_model::{
29        self, BindGroup, BindGroupLateBufferBindingInfo, BindGroupLayout,
30        BindGroupLayoutEntryError, CreateBindGroupError, CreateBindGroupLayoutError,
31    },
32    command, conv,
33    device::{
34        bgl, create_validator, features_to_naga_capabilities, life::WaitIdleError, map_buffer,
35        AttachmentData, DeviceLostInvocation, HostMap, MissingDownlevelFlags, MissingFeatures,
36        RenderPassContext,
37    },
38    hal_label,
39    init_tracker::{
40        BufferInitTracker, BufferInitTrackerAction, MemoryInitKind, TextureInitRange,
41        TextureInitTrackerAction,
42    },
43    instance::{Adapter, RequestDeviceError},
44    lock::{rank, Mutex, RwLock},
45    pipeline::{self, ColorStateError},
46    pool::ResourcePool,
47    present,
48    resource::{
49        self, Buffer, ExternalTexture, Fallible, Labeled, ParentDevice, QuerySet,
50        RawResourceAccess, Sampler, StagingBuffer, Texture, TextureView,
51        TextureViewNotRenderableReason, Tlas, TrackingData,
52    },
53    resource_log,
54    snatch::{SnatchGuard, SnatchLock, Snatchable},
55    timestamp_normalization::TIMESTAMP_NORMALIZATION_BUFFER_USES,
56    track::{BindGroupStates, DeviceTracker, TrackerIndexAllocators, UsageScope, UsageScopePool},
57    validation::{self, check_color_attachment_count, PassthroughInterface, ShaderMetaData},
58    weak_vec::WeakVec,
59    FastHashMap, LabelHelpers, OnceCellOrLock,
60};
61
62use super::{
63    queue::Queue, DeviceDescriptor, DeviceError, DeviceLostClosure, UserClosures,
64    ENTRYPOINT_FAILURE_ERROR, ZERO_BUFFER_SIZE,
65};
66
67#[cfg(supports_64bit_atomics)]
68use core::sync::atomic::AtomicU64;
69#[cfg(not(supports_64bit_atomics))]
70use portable_atomic::AtomicU64;
71
72pub(crate) struct CommandIndices {
73    /// The index of the last command submission that was attempted.
74    ///
75    /// Note that `fence` may never be signalled with this value, if the command
76    /// submission failed. If you need to wait for everything running on a
77    /// `Queue` to complete, wait for [`last_successful_submission_index`].
78    ///
79    /// [`last_successful_submission_index`]: Device::last_successful_submission_index
80    pub(crate) active_submission_index: hal::FenceValue,
81    pub(crate) next_acceleration_structure_build_command_index: u64,
82}
83
84/// Parameters provided to shaders via a uniform buffer of the type
85/// [`NagaExternalTextureParams`], describing an [`ExternalTexture`] resource
86/// binding.
87///
88/// [`NagaExternalTextureParams`]: naga::SpecialTypes::external_texture_params
89/// [`ExternalTexture`]: binding_model::BindingResource::ExternalTexture
90#[repr(C)]
91#[derive(Copy, Clone, bytemuck::Zeroable, bytemuck::Pod)]
92pub struct ExternalTextureParams {
93    /// 4x4 column-major matrix with which to convert sampled YCbCr values
94    /// to RGBA.
95    ///
96    /// This is ignored when `num_planes` is 1.
97    pub yuv_conversion_matrix: [f32; 16],
98
99    /// 3x3 column-major matrix to transform linear RGB values in the source
100    /// color space to linear RGB values in the destination color space. In
101    /// combination with [`Self::src_transfer_function`] and
102    /// [`Self::dst_transfer_function`] this can be used to ensure that
103    /// [`ImageSample`] and [`ImageLoad`] operations return values in the
104    /// desired destination color space rather than the source color space of
105    /// the underlying planes.
106    ///
107    /// Includes a padding element after each column.
108    ///
109    /// [`ImageSample`]: naga::ir::Expression::ImageSample
110    /// [`ImageLoad`]: naga::ir::Expression::ImageLoad
111    pub gamut_conversion_matrix: [f32; 12],
112
113    /// Transfer function for the source color space. The *inverse* of this
114    /// will be applied to decode non-linear RGB to linear RGB in the source
115    /// color space.
116    pub src_transfer_function: wgt::ExternalTextureTransferFunction,
117
118    /// Transfer function for the destination color space. This will be applied
119    /// to encode linear RGB to non-linear RGB in the destination color space.
120    pub dst_transfer_function: wgt::ExternalTextureTransferFunction,
121
122    /// Transform to apply to [`ImageSample`] coordinates.
123    ///
124    /// This is a 3x2 column-major matrix representing an affine transform from
125    /// normalized texture coordinates to the normalized coordinates that should
126    /// be sampled from the external texture's underlying plane(s).
127    ///
128    /// This transform may scale, translate, flip, and rotate in 90-degree
129    /// increments, but the result of transforming the rectangle (0,0)..(1,1)
130    /// must be an axis-aligned rectangle that falls within the bounds of
131    /// (0,0)..(1,1).
132    ///
133    /// [`ImageSample`]: naga::ir::Expression::ImageSample
134    pub sample_transform: [f32; 6],
135
136    /// Transform to apply to [`ImageLoad`] coordinates.
137    ///
138    /// This is a 3x2 column-major matrix representing an affine transform from
139    /// non-normalized texel coordinates to the non-normalized coordinates of
140    /// the texel that should be loaded from the external texture's underlying
141    /// plane 0. For planes 1 and 2, if present, plane 0's coordinates are
142    /// scaled according to the textures' relative sizes.
143    ///
144    /// This transform may scale, translate, flip, and rotate in 90-degree
145    /// increments, but the result of transforming the rectangle (0,0)..[`size`]
146    /// must be an axis-aligned rectangle that falls within the bounds of
147    /// (0,0)..[`size`].
148    ///
149    /// [`ImageLoad`]: naga::ir::Expression::ImageLoad
150    /// [`size`]: Self::size
151    pub load_transform: [f32; 6],
152
153    /// Size of the external texture.
154    ///
155    /// This is the value that should be returned by size queries in shader
156    /// code; it does not necessarily match the dimensions of the underlying
157    /// texture(s). As a special case, if this is `[0, 0]`, the actual size of
158    /// plane 0 should be used instead.
159    ///
160    /// This must be consistent with [`sample_transform`]: it should be the size
161    /// in texels of the rectangle covered by the square (0,0)..(1,1) after
162    /// [`sample_transform`] has been applied to it.
163    ///
164    /// [`sample_transform`]: Self::sample_transform
165    pub size: [u32; 2],
166
167    /// Number of planes. 1 indicates a single RGBA plane. 2 indicates a Y
168    /// plane and an interleaved CbCr plane. 3 indicates separate Y, Cb, and Cr
169    /// planes.
170    pub num_planes: u32,
171    // Ensure the size of this struct matches the type generated by Naga.
172    pub _padding: [u8; 4],
173}
174
175impl ExternalTextureParams {
176    pub fn from_desc<L>(desc: &wgt::ExternalTextureDescriptor<L>) -> Self {
177        let gamut_conversion_matrix = [
178            desc.gamut_conversion_matrix[0],
179            desc.gamut_conversion_matrix[1],
180            desc.gamut_conversion_matrix[2],
181            0.0, // padding
182            desc.gamut_conversion_matrix[3],
183            desc.gamut_conversion_matrix[4],
184            desc.gamut_conversion_matrix[5],
185            0.0, // padding
186            desc.gamut_conversion_matrix[6],
187            desc.gamut_conversion_matrix[7],
188            desc.gamut_conversion_matrix[8],
189            0.0, // padding
190        ];
191
192        Self {
193            yuv_conversion_matrix: desc.yuv_conversion_matrix,
194            gamut_conversion_matrix,
195            src_transfer_function: desc.src_transfer_function,
196            dst_transfer_function: desc.dst_transfer_function,
197            size: [desc.width, desc.height],
198            sample_transform: desc.sample_transform,
199            load_transform: desc.load_transform,
200            num_planes: desc.num_planes() as u32,
201            _padding: Default::default(),
202        }
203    }
204}
205
206/// Structure describing a logical device. Some members are internally mutable,
207/// stored behind mutexes.
208pub struct Device {
209    raw: Box<dyn hal::DynDevice>,
210    pub(crate) adapter: Arc<Adapter>,
211    pub(crate) queue: OnceCellOrLock<Weak<Queue>>,
212    pub(crate) zero_buffer: ManuallyDrop<Box<dyn hal::DynBuffer>>,
213    pub(crate) empty_bgl: ManuallyDrop<Box<dyn hal::DynBindGroupLayout>>,
214    /// The `label` from the descriptor used to create the resource.
215    label: String,
216
217    pub(crate) command_allocator: command::CommandAllocator,
218
219    pub(crate) command_indices: RwLock<CommandIndices>,
220
221    /// The index of the last successful submission to this device's
222    /// [`hal::Queue`].
223    ///
224    /// Unlike [`active_submission_index`], which is incremented each time
225    /// submission is attempted, this is updated only when submission succeeds,
226    /// so waiting for this value won't hang waiting for work that was never
227    /// submitted.
228    ///
229    /// [`active_submission_index`]: CommandIndices::active_submission_index
230    pub(crate) last_successful_submission_index: hal::AtomicFenceValue,
231
232    pub(crate) fence: ManuallyDrop<Box<dyn hal::DynFence>>,
233    pub(crate) snatchable_lock: SnatchLock,
234
235    /// Is this device valid? Valid is closely associated with "lose the device",
236    /// which can be triggered by various methods, including at the end of device
237    /// destroy, and by any GPU errors that cause us to no longer trust the state
238    /// of the device. Ideally we would like to fold valid into the storage of
239    /// the device itself (for example as an Error enum), but unfortunately we
240    /// need to continue to be able to retrieve the device in poll_devices to
241    /// determine if it can be dropped. If our internal accesses of devices were
242    /// done through ref-counted references and external accesses checked for
243    /// Error enums, we wouldn't need this. For now, we need it. All the call
244    /// sites where we check it are areas that should be revisited if we start
245    /// using ref-counted references for internal access.
246    pub(crate) valid: AtomicBool,
247
248    /// Closure to be called on "lose the device". This is invoked directly by
249    /// device.lose or by the UserCallbacks returned from maintain when the device
250    /// has been destroyed and its queues are empty.
251    pub(crate) device_lost_closure: Mutex<Option<DeviceLostClosure>>,
252
253    /// Stores the state of buffers and textures.
254    pub(crate) trackers: Mutex<DeviceTracker>,
255    pub(crate) tracker_indices: TrackerIndexAllocators,
256    /// Pool of bind group layouts, allowing deduplication.
257    pub(crate) bgl_pool: ResourcePool<bgl::EntryMap, BindGroupLayout>,
258    pub(crate) alignments: hal::Alignments,
259    pub(crate) limits: wgt::Limits,
260    pub(crate) features: wgt::Features,
261    pub(crate) downlevel: wgt::DownlevelCapabilities,
262    /// Buffer uses listed here, are expected to be ordered by the underlying hardware.
263    /// If a usage is ordered, then if the buffer state doesn't change between draw calls,
264    /// there are no barriers needed for synchronization.
265    /// See the implementations of [`hal::Adapter::get_ordered_buffer_usages`] for hardware specific info
266    pub(crate) ordered_buffer_usages: wgt::BufferUses,
267    /// Texture uses listed here, are expected to be ordered by the underlying hardware.
268    /// If a usage is ordered, then if the buffer state doesn't change between draw calls,
269    /// there are no barriers needed for synchronization.
270    /// See the implementations of [`hal::Adapter::get_ordered_texture_usages`] for hardware specific info
271    pub(crate) ordered_texture_usages: wgt::TextureUses,
272    pub(crate) instance_flags: wgt::InstanceFlags,
273    pub(crate) deferred_destroy: Mutex<Vec<DeferredDestroy>>,
274    pub(crate) usage_scopes: UsageScopePool,
275    pub(crate) indirect_validation: Option<crate::indirect_validation::IndirectValidation>,
276    // Optional so that we can late-initialize this after the queue is created.
277    pub(crate) timestamp_normalizer:
278        OnceCellOrLock<crate::timestamp_normalization::TimestampNormalizer>,
279    /// Uniform buffer containing [`ExternalTextureParams`] with values such
280    /// that a [`TextureView`] bound to a [`wgt::BindingType::ExternalTexture`]
281    /// binding point will be rendered correctly. Intended to be used as the
282    /// [`hal::ExternalTextureBinding::params`] field.
283    pub(crate) default_external_texture_params_buffer: ManuallyDrop<Box<dyn hal::DynBuffer>>,
284    // needs to be dropped last
285    #[cfg(feature = "trace")]
286    pub(crate) trace: Mutex<Option<Box<dyn trace::Trace + Send + Sync + 'static>>>,
287}
288
289pub(crate) enum DeferredDestroy {
290    TextureViews(WeakVec<TextureView>),
291    BindGroups(WeakVec<BindGroup>),
292}
293
294impl fmt::Debug for Device {
295    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
296        f.debug_struct("Device")
297            .field("label", &self.label())
298            .field("limits", &self.limits)
299            .field("features", &self.features)
300            .field("downlevel", &self.downlevel)
301            .finish()
302    }
303}
304
305impl Drop for Device {
306    fn drop(&mut self) {
307        resource_log!("Drop {}", self.error_ident());
308
309        // SAFETY: We are in the Drop impl and we don't use self.zero_buffer anymore after this
310        // point.
311        let zero_buffer = unsafe { ManuallyDrop::take(&mut self.zero_buffer) };
312        // SAFETY: We are in the Drop impl and we don't use self.empty_bgl anymore after this point.
313        let empty_bgl = unsafe { ManuallyDrop::take(&mut self.empty_bgl) };
314        // SAFETY: We are in the Drop impl and we don't use
315        // self.default_external_texture_params_buffer anymore after this point.
316        let default_external_texture_params_buffer =
317            unsafe { ManuallyDrop::take(&mut self.default_external_texture_params_buffer) };
318        // SAFETY: We are in the Drop impl and we don't use self.fence anymore after this point.
319        let fence = unsafe { ManuallyDrop::take(&mut self.fence) };
320        if let Some(indirect_validation) = self.indirect_validation.take() {
321            indirect_validation.dispose(self.raw.as_ref());
322        }
323        if let Some(timestamp_normalizer) = self.timestamp_normalizer.take() {
324            timestamp_normalizer.dispose(self.raw.as_ref());
325        }
326        unsafe {
327            self.raw.destroy_buffer(zero_buffer);
328            self.raw.destroy_bind_group_layout(empty_bgl);
329            self.raw
330                .destroy_buffer(default_external_texture_params_buffer);
331            self.raw.destroy_fence(fence);
332        }
333    }
334}
335
336impl Device {
337    pub(crate) fn raw(&self) -> &dyn hal::DynDevice {
338        self.raw.as_ref()
339    }
340    pub(crate) fn require_features(&self, feature: wgt::Features) -> Result<(), MissingFeatures> {
341        if self.features.contains(feature) {
342            Ok(())
343        } else {
344            Err(MissingFeatures(feature))
345        }
346    }
347
348    pub(crate) fn require_downlevel_flags(
349        &self,
350        flags: wgt::DownlevelFlags,
351    ) -> Result<(), MissingDownlevelFlags> {
352        if self.downlevel.flags.contains(flags) {
353            Ok(())
354        } else {
355            Err(MissingDownlevelFlags(flags))
356        }
357    }
358
359    /// # Safety
360    ///
361    /// - See [wgpu::Device::start_graphics_debugger_capture][api] for details the safety.
362    ///
363    /// [api]: ../../wgpu/struct.Device.html#method.start_graphics_debugger_capture
364    pub unsafe fn start_graphics_debugger_capture(&self) {
365        api_log!("Device::start_graphics_debugger_capture");
366
367        if !self.is_valid() {
368            return;
369        }
370        unsafe { self.raw().start_graphics_debugger_capture() };
371    }
372
373    /// # Safety
374    ///
375    /// - See [wgpu::Device::stop_graphics_debugger_capture][api] for details the safety.
376    ///
377    /// [api]: ../../wgpu/struct.Device.html#method.stop_graphics_debugger_capture
378    pub unsafe fn stop_graphics_debugger_capture(&self) {
379        api_log!("Device::stop_graphics_debugger_capture");
380
381        if !self.is_valid() {
382            return;
383        }
384        unsafe { self.raw().stop_graphics_debugger_capture() };
385    }
386}
387
388impl Device {
389    pub(crate) fn new(
390        raw_device: Box<dyn hal::DynDevice>,
391        adapter: &Arc<Adapter>,
392        desc: &DeviceDescriptor,
393        instance_flags: wgt::InstanceFlags,
394    ) -> Result<Self, DeviceError> {
395        #[cfg(not(feature = "trace"))]
396        match &desc.trace {
397            wgt::Trace::Off => {}
398            _ => {
399                log::error!("wgpu-core feature 'trace' is not enabled");
400            }
401        };
402        #[cfg(feature = "trace")]
403        let trace: Option<Box<dyn trace::Trace + Send + Sync + 'static>> = match &desc.trace {
404            wgt::Trace::Off => None,
405            wgt::Trace::Directory(dir) => match trace::DiskTrace::new(dir.clone()) {
406                Ok(mut trace) => {
407                    trace::Trace::add(
408                        &mut trace,
409                        trace::Action::Init {
410                            desc: wgt::DeviceDescriptor {
411                                trace: wgt::Trace::Off,
412                                ..desc.clone()
413                            },
414                            backend: adapter.backend(),
415                        },
416                    );
417                    Some(Box::new(trace))
418                }
419                Err(e) => {
420                    log::error!("Unable to start a trace in '{dir:?}': {e}");
421                    None
422                }
423            },
424            wgt::Trace::Memory => {
425                let mut trace = trace::MemoryTrace::new();
426                trace::Trace::add(
427                    &mut trace,
428                    trace::Action::Init {
429                        desc: wgt::DeviceDescriptor {
430                            trace: wgt::Trace::Off,
431                            ..desc.clone()
432                        },
433                        backend: adapter.backend(),
434                    },
435                );
436                Some(Box::new(trace))
437            }
438            // The enum is non_exhaustive, so we must have a fallback arm (that should be
439            // unreachable in practice).
440            t => {
441                log::error!("unimplemented wgpu_types::Trace variant {t:?}");
442                None
443            }
444        };
445
446        let ordered_buffer_usages = adapter.raw.adapter.get_ordered_buffer_usages();
447        let ordered_texture_usages = adapter.raw.adapter.get_ordered_texture_usages();
448
449        let fence = unsafe { raw_device.create_fence() }.map_err(DeviceError::from_hal)?;
450
451        let command_allocator = command::CommandAllocator::new();
452
453        let rt_uses = if desc
454            .required_features
455            .intersects(wgt::Features::EXPERIMENTAL_RAY_QUERY)
456        {
457            wgt::BufferUses::TOP_LEVEL_ACCELERATION_STRUCTURE_INPUT
458        } else {
459            wgt::BufferUses::empty()
460        };
461
462        // Create zeroed buffer used for texture clears (and raytracing if required).
463        let zero_buffer = unsafe {
464            raw_device.create_buffer(&hal::BufferDescriptor {
465                label: hal_label(Some("(wgpu internal) zero init buffer"), instance_flags),
466                size: ZERO_BUFFER_SIZE,
467                usage: wgt::BufferUses::COPY_SRC | wgt::BufferUses::COPY_DST | rt_uses,
468                memory_flags: hal::MemoryFlags::empty(),
469            })
470        }
471        .map_err(DeviceError::from_hal)?;
472
473        let empty_bgl = unsafe {
474            raw_device.create_bind_group_layout(&hal::BindGroupLayoutDescriptor {
475                label: None,
476                flags: hal::BindGroupLayoutFlags::empty(),
477                entries: &[],
478            })
479        }
480        .map_err(DeviceError::from_hal)?;
481
482        let default_external_texture_params_buffer = unsafe {
483            raw_device.create_buffer(&hal::BufferDescriptor {
484                label: hal_label(
485                    Some("(wgpu internal) default external texture params buffer"),
486                    instance_flags,
487                ),
488                size: size_of::<ExternalTextureParams>() as _,
489                usage: wgt::BufferUses::COPY_DST | wgt::BufferUses::UNIFORM,
490                memory_flags: hal::MemoryFlags::empty(),
491            })
492        }
493        .map_err(DeviceError::from_hal)?;
494
495        // Cloned as we need them below anyway.
496        let alignments = adapter.raw.capabilities.alignments.clone();
497        let downlevel = adapter.raw.capabilities.downlevel.clone();
498        let limits = &adapter.raw.capabilities.limits;
499
500        let enable_indirect_validation = instance_flags
501            .contains(wgt::InstanceFlags::VALIDATION_INDIRECT_CALL)
502            && downlevel.flags.contains(
503                wgt::DownlevelFlags::INDIRECT_EXECUTION | wgt::DownlevelFlags::COMPUTE_SHADERS,
504            )
505            && limits.max_storage_buffers_per_shader_stage >= 2;
506
507        let indirect_validation = if enable_indirect_validation {
508            Some(crate::indirect_validation::IndirectValidation::new(
509                raw_device.as_ref(),
510                &desc.required_limits,
511                &desc.required_features,
512                instance_flags,
513                adapter.backend(),
514            )?)
515        } else {
516            None
517        };
518
519        Ok(Self {
520            raw: raw_device,
521            adapter: adapter.clone(),
522            queue: OnceCellOrLock::new(),
523            zero_buffer: ManuallyDrop::new(zero_buffer),
524            empty_bgl: ManuallyDrop::new(empty_bgl),
525            default_external_texture_params_buffer: ManuallyDrop::new(
526                default_external_texture_params_buffer,
527            ),
528            label: desc.label.to_string(),
529            command_allocator,
530            command_indices: RwLock::new(
531                rank::DEVICE_COMMAND_INDICES,
532                CommandIndices {
533                    active_submission_index: 0,
534                    // By starting at one, we can put the result in a NonZeroU64.
535                    next_acceleration_structure_build_command_index: 1,
536                },
537            ),
538            last_successful_submission_index: AtomicU64::new(0),
539            fence: ManuallyDrop::new(fence),
540            snatchable_lock: unsafe { SnatchLock::new(rank::DEVICE_SNATCHABLE_LOCK) },
541            valid: AtomicBool::new(true),
542            device_lost_closure: Mutex::new(rank::DEVICE_LOST_CLOSURE, None),
543            trackers: Mutex::new(
544                rank::DEVICE_TRACKERS,
545                DeviceTracker::new(ordered_buffer_usages, ordered_texture_usages),
546            ),
547            tracker_indices: TrackerIndexAllocators::new(),
548            bgl_pool: ResourcePool::new(),
549            #[cfg(feature = "trace")]
550            trace: Mutex::new(rank::DEVICE_TRACE, trace),
551            alignments,
552            limits: desc.required_limits.clone(),
553            features: desc.required_features,
554            downlevel,
555            ordered_buffer_usages,
556            ordered_texture_usages,
557            instance_flags,
558            deferred_destroy: Mutex::new(rank::DEVICE_DEFERRED_DESTROY, Vec::new()),
559            usage_scopes: Mutex::new(rank::DEVICE_USAGE_SCOPES, Default::default()),
560            timestamp_normalizer: OnceCellOrLock::new(),
561            indirect_validation,
562        })
563    }
564
565    /// Initializes [`Device::default_external_texture_params_buffer`] with
566    /// required values such that a [`TextureView`] bound to a
567    /// [`wgt::BindingType::ExternalTexture`] binding point will be rendered
568    /// correctly.
569    fn init_default_external_texture_params_buffer(self: &Arc<Self>) -> Result<(), DeviceError> {
570        let data = ExternalTextureParams {
571            #[rustfmt::skip]
572            yuv_conversion_matrix: [
573                1.0, 0.0, 0.0, 0.0,
574                0.0, 1.0, 0.0, 0.0,
575                0.0, 0.0, 1.0, 0.0,
576                0.0, 0.0, 0.0, 1.0,
577            ],
578            #[rustfmt::skip]
579            gamut_conversion_matrix: [
580                1.0, 0.0, 0.0, /* padding */ 0.0,
581                0.0, 1.0, 0.0, /* padding */ 0.0,
582                0.0, 0.0, 1.0, /* padding */ 0.0,
583            ],
584            src_transfer_function: Default::default(),
585            dst_transfer_function: Default::default(),
586            size: [0, 0],
587            #[rustfmt::skip]
588            sample_transform: [
589                1.0, 0.0,
590                0.0, 1.0,
591                0.0, 0.0
592            ],
593            #[rustfmt::skip]
594            load_transform: [
595                1.0, 0.0,
596                0.0, 1.0,
597                0.0, 0.0
598            ],
599            num_planes: 1,
600            _padding: Default::default(),
601        };
602        let mut staging_buffer =
603            StagingBuffer::new(self, wgt::BufferSize::new(size_of_val(&data) as _).unwrap())?;
604        staging_buffer.write(bytemuck::bytes_of(&data));
605        let staging_buffer = staging_buffer.flush();
606
607        let params_buffer = self.default_external_texture_params_buffer.as_ref();
608        let queue = self.get_queue().unwrap();
609        let mut pending_writes = queue.pending_writes.lock();
610
611        unsafe {
612            pending_writes
613                .command_encoder
614                .transition_buffers(&[hal::BufferBarrier {
615                    buffer: params_buffer,
616                    usage: hal::StateTransition {
617                        from: wgt::BufferUses::MAP_WRITE,
618                        to: wgt::BufferUses::COPY_DST,
619                    },
620                }]);
621            pending_writes.command_encoder.copy_buffer_to_buffer(
622                staging_buffer.raw(),
623                params_buffer,
624                &[hal::BufferCopy {
625                    src_offset: 0,
626                    dst_offset: 0,
627                    size: staging_buffer.size,
628                }],
629            );
630            pending_writes.consume(staging_buffer);
631            pending_writes
632                .command_encoder
633                .transition_buffers(&[hal::BufferBarrier {
634                    buffer: params_buffer,
635                    usage: hal::StateTransition {
636                        from: wgt::BufferUses::COPY_DST,
637                        to: wgt::BufferUses::UNIFORM,
638                    },
639                }]);
640        }
641
642        Ok(())
643    }
644
645    pub fn late_init_resources_with_queue(self: &Arc<Self>) -> Result<(), RequestDeviceError> {
646        let queue = self.get_queue().unwrap();
647
648        let timestamp_normalizer = crate::timestamp_normalization::TimestampNormalizer::new(
649            self,
650            queue.get_timestamp_period(),
651        )?;
652
653        self.timestamp_normalizer
654            .set(timestamp_normalizer)
655            .unwrap_or_else(|_| panic!("Called late_init_resources_with_queue twice"));
656
657        self.init_default_external_texture_params_buffer()?;
658
659        Ok(())
660    }
661
662    /// Returns the backend this device is using.
663    pub fn backend(&self) -> wgt::Backend {
664        self.adapter.backend()
665    }
666
667    pub fn is_valid(&self) -> bool {
668        self.valid.load(Ordering::Acquire)
669    }
670
671    pub fn check_is_valid(&self) -> Result<(), DeviceError> {
672        if self.is_valid() {
673            Ok(())
674        } else {
675            Err(DeviceError::Lost)
676        }
677    }
678
679    /// Stop tracing and return the trace object.
680    ///
681    /// This is mostly useful for in-memory traces.
682    #[cfg(feature = "trace")]
683    pub fn take_trace(&self) -> Option<Box<dyn trace::Trace + Send + Sync + 'static>> {
684        self.trace.lock().take()
685    }
686
687    /// Checks that we are operating within the memory budget reported by the native APIs.
688    ///
689    /// If we are not, the device gets invalidated.
690    ///
691    /// The budget might fluctuate over the lifetime of the application, so it should be checked
692    /// somewhat frequently.
693    pub fn lose_if_oom(&self) {
694        let _ = self
695            .raw()
696            .check_if_oom()
697            .map_err(|e| self.handle_hal_error(e));
698    }
699
700    pub fn handle_hal_error(&self, error: hal::DeviceError) -> DeviceError {
701        match error {
702            hal::DeviceError::OutOfMemory
703            | hal::DeviceError::Lost
704            | hal::DeviceError::Unexpected => {
705                self.lose(&error.to_string());
706            }
707        }
708        DeviceError::from_hal(error)
709    }
710
711    pub fn handle_hal_error_with_nonfatal_oom(&self, error: hal::DeviceError) -> DeviceError {
712        match error {
713            hal::DeviceError::OutOfMemory => DeviceError::from_hal(error),
714            error => self.handle_hal_error(error),
715        }
716    }
717
718    /// Run some destroy operations that were deferred.
719    ///
720    /// Destroying the resources requires taking a write lock on the device's snatch lock,
721    /// so a good reason for deferring resource destruction is when we don't know for sure
722    /// how risky it is to take the lock (typically, it shouldn't be taken from the drop
723    /// implementation of a reference-counted structure).
724    /// The snatch lock must not be held while this function is called.
725    pub(crate) fn deferred_resource_destruction(&self) {
726        // Note that the deferred_destroy list may contain duplicate entries.
727        let deferred_destroy = mem::take(&mut *self.deferred_destroy.lock());
728        for item in deferred_destroy {
729            match item {
730                DeferredDestroy::TextureViews(views) => {
731                    for view in views {
732                        let Some(view) = view.upgrade() else {
733                            continue;
734                        };
735                        let Some(raw_view) = view.raw.snatch(&mut self.snatchable_lock.write())
736                        else {
737                            continue;
738                        };
739
740                        resource_log!("Destroy raw {}", view.error_ident());
741
742                        unsafe {
743                            self.raw().destroy_texture_view(raw_view);
744                        }
745                    }
746                }
747                DeferredDestroy::BindGroups(bind_groups) => {
748                    for bind_group in bind_groups {
749                        let Some(bind_group) = bind_group.upgrade() else {
750                            continue;
751                        };
752                        let Some(raw_bind_group) =
753                            bind_group.raw.snatch(&mut self.snatchable_lock.write())
754                        else {
755                            continue;
756                        };
757
758                        resource_log!("Destroy raw {}", bind_group.error_ident());
759
760                        unsafe {
761                            self.raw().destroy_bind_group(raw_bind_group);
762                        }
763                    }
764                }
765            }
766        }
767    }
768
769    pub fn get_queue(&self) -> Option<Arc<Queue>> {
770        self.queue.get().as_ref()?.upgrade()
771    }
772
773    pub fn set_queue(&self, queue: &Arc<Queue>) {
774        assert!(self.queue.set(Arc::downgrade(queue)).is_ok());
775    }
776
777    pub fn poll(
778        &self,
779        poll_type: wgt::PollType<crate::SubmissionIndex>,
780    ) -> Result<wgt::PollStatus, WaitIdleError> {
781        let (user_closures, result) = self.poll_and_return_closures(poll_type);
782        user_closures.fire();
783        result
784    }
785
786    /// Poll the device, returning any `UserClosures` that need to be executed.
787    ///
788    /// The caller must invoke the `UserClosures` even if this function returns
789    /// an error. This is an internal helper, used by `Device::poll` and
790    /// `Global::poll_all_devices`, so that `poll_all_devices` can invoke
791    /// closures once after all devices have been polled.
792    pub(crate) fn poll_and_return_closures(
793        &self,
794        poll_type: wgt::PollType<crate::SubmissionIndex>,
795    ) -> (UserClosures, Result<wgt::PollStatus, WaitIdleError>) {
796        let snatch_guard = self.snatchable_lock.read();
797        let maintain_result = self.maintain(poll_type, snatch_guard);
798
799        self.lose_if_oom();
800
801        // Some deferred destroys are scheduled in maintain so run this right after
802        // to avoid holding on to them until the next device poll.
803        self.deferred_resource_destruction();
804
805        maintain_result
806    }
807
808    /// Check the current status of the GPU and process any submissions that have
809    /// finished.
810    ///
811    /// The `poll_type` argument tells if this function should wait for a particular
812    /// submission index to complete, or if it should just poll the current status.
813    ///
814    /// This will process _all_ completed submissions, even if the caller only asked
815    /// us to poll to a given submission index.
816    ///
817    /// Return a pair `(closures, result)`, where:
818    ///
819    /// - `closures` is a list of callbacks that need to be invoked informing the user
820    ///   about various things occurring. These happen and should be handled even if
821    ///   this function returns an error, hence they are outside of the result.
822    ///
823    /// - `results` is a boolean indicating the result of the wait operation, including
824    ///   if there was a timeout or a validation error.
825    pub(crate) fn maintain<'this>(
826        &'this self,
827        poll_type: wgt::PollType<crate::SubmissionIndex>,
828        snatch_guard: SnatchGuard,
829    ) -> (UserClosures, Result<wgt::PollStatus, WaitIdleError>) {
830        profiling::scope!("Device::maintain");
831
832        let mut user_closures = UserClosures::default();
833
834        // If a wait was requested, determine which submission index to wait for.
835        let wait_submission_index = match poll_type {
836            wgt::PollType::Wait {
837                submission_index: Some(submission_index),
838                ..
839            } => {
840                let last_successful_submission_index = self
841                    .last_successful_submission_index
842                    .load(Ordering::Acquire);
843
844                if submission_index > last_successful_submission_index {
845                    let result = Err(WaitIdleError::WrongSubmissionIndex(
846                        submission_index,
847                        last_successful_submission_index,
848                    ));
849
850                    return (user_closures, result);
851                }
852
853                Some(submission_index)
854            }
855            wgt::PollType::Wait {
856                submission_index: None,
857                ..
858            } => Some(
859                self.last_successful_submission_index
860                    .load(Ordering::Acquire),
861            ),
862            wgt::PollType::Poll => None,
863        };
864
865        // Wait for the submission index if requested.
866        if let Some(target_submission_index) = wait_submission_index {
867            log::trace!("Device::maintain: waiting for submission index {target_submission_index}");
868
869            let wait_timeout = match poll_type {
870                wgt::PollType::Wait { timeout, .. } => timeout,
871                wgt::PollType::Poll => unreachable!(
872                    "`wait_submission_index` index for poll type `Poll` should be None"
873                ),
874            };
875
876            let wait_result = unsafe {
877                self.raw()
878                    .wait(self.fence.as_ref(), target_submission_index, wait_timeout)
879            };
880
881            // This error match is only about `DeviceErrors`. At this stage we do not care if
882            // the wait succeeded or not, and the `Ok(bool)`` variant is ignored.
883            if let Err(e) = wait_result {
884                let hal_error: WaitIdleError = self.handle_hal_error(e).into();
885                return (user_closures, Err(hal_error));
886            }
887        }
888
889        // Get the currently finished submission index. This may be higher than the requested
890        // wait, or it may be less than the requested wait if the wait failed.
891        let fence_value_result = unsafe { self.raw().get_fence_value(self.fence.as_ref()) };
892        let current_finished_submission = match fence_value_result {
893            Ok(fence_value) => fence_value,
894            Err(e) => {
895                let hal_error: WaitIdleError = self.handle_hal_error(e).into();
896                return (user_closures, Err(hal_error));
897            }
898        };
899
900        // Prevent new commands from being submitted as we want to act on `queue_empty`.
901        let command_indices = self.command_indices.read();
902        // Check that the device is valid. This is combined with queue empty to decide whether
903        // to destroy all resources. Queue.submit blocks on command indices being writable
904        // and rejects if invalid so if the device in now invalid, and all submissions are
905        // finished, there will be no more submissions.
906        let device_valid = self.is_valid();
907        drop(command_indices);
908
909        // Maintain all finished submissions on the queue, updating the relevant user closures and
910        // collecting if the queue is empty.
911        //
912        // We don't use the result of the wait here, as we want to progress forward as far as
913        // possible and the wait could have been for submissions that finished long ago.
914        let mut queue_empty = false;
915        if let Some(queue) = self.get_queue() {
916            let queue_result = queue.maintain(current_finished_submission, &snatch_guard);
917            (
918                user_closures.submissions,
919                user_closures.mappings,
920                user_closures.blas_compact_ready,
921                queue_empty,
922            ) = queue_result;
923            // DEADLOCK PREVENTION: We must drop `snatch_guard` before `queue` goes out of scope.
924            //
925            // `Queue::drop` acquires the snatch guard. If we still hold it when `queue` is dropped
926            // at the end of this block, we would deadlock. This can happen in the following
927            // scenario:
928            //
929            // - Thread A calls `Device::maintain` while Thread B holds the last strong ref to the
930            //   queue.
931            // - Thread A calls `self.get_queue()`, obtaining a new strong ref, and enters this
932            //   branch.
933            // - Thread B drops its strong ref, making Thread A's ref the last one.
934            // - When `queue` goes out of scope here, `Queue::drop` runs and tries to acquire the
935            //   snatch guard — but Thread A (this thread) still holds it, causing a deadlock.
936            drop(snatch_guard);
937        } else {
938            drop(snatch_guard);
939        };
940
941        // Based on the queue empty status, and the current finished submission index, determine
942        // the result of the poll.
943        let result = if queue_empty {
944            if let Some(wait_submission_index) = wait_submission_index {
945                // Assert to ensure that if we received a queue empty status, the fence shows the
946                // correct value. This is defensive, as this should never be hit.
947                assert!(
948                    current_finished_submission >= wait_submission_index,
949                    concat!(
950                        "If the queue is empty, the current submission index ",
951                        "({}) should be at least the wait submission index ({})",
952                    ),
953                    current_finished_submission,
954                    wait_submission_index,
955                );
956            }
957
958            Ok(wgt::PollStatus::QueueEmpty)
959        } else if let Some(wait_submission_index) = wait_submission_index {
960            // This is theoretically possible to succeed more than checking on the poll result
961            // as submissions could have finished in the time between the timeout resolving,
962            // the thread getting scheduled again, and us checking the fence value.
963            if current_finished_submission >= wait_submission_index {
964                Ok(wgt::PollStatus::WaitSucceeded)
965            } else {
966                Err(WaitIdleError::Timeout)
967            }
968        } else {
969            Ok(wgt::PollStatus::Poll)
970        };
971
972        // Detect if we have been destroyed and now need to lose the device.
973        //
974        // If we are invalid (set at start of destroy) and our queue is empty,
975        // and we have a DeviceLostClosure, return the closure to be called by
976        // our caller. This will complete the steps for both destroy and for
977        // "lose the device".
978        let mut should_release_gpu_resource = false;
979        if !device_valid && queue_empty {
980            // We can release gpu resources associated with this device (but not
981            // while holding the life_tracker lock).
982            should_release_gpu_resource = true;
983
984            // If we have a DeviceLostClosure, build an invocation with the
985            // reason DeviceLostReason::Destroyed and no message.
986            if let Some(device_lost_closure) = self.device_lost_closure.lock().take() {
987                user_closures
988                    .device_lost_invocations
989                    .push(DeviceLostInvocation {
990                        closure: device_lost_closure,
991                        reason: DeviceLostReason::Destroyed,
992                        message: String::new(),
993                    });
994            }
995        }
996
997        if should_release_gpu_resource {
998            self.release_gpu_resources();
999        }
1000
1001        (user_closures, result)
1002    }
1003
1004    pub fn create_buffer(
1005        self: &Arc<Self>,
1006        desc: &resource::BufferDescriptor,
1007    ) -> Result<Arc<Buffer>, resource::CreateBufferError> {
1008        self.check_is_valid()?;
1009
1010        if desc.size > self.limits.max_buffer_size {
1011            return Err(resource::CreateBufferError::MaxBufferSize {
1012                requested: desc.size,
1013                maximum: self.limits.max_buffer_size,
1014            });
1015        }
1016
1017        if desc
1018            .usage
1019            .intersects(wgt::BufferUsages::BLAS_INPUT | wgt::BufferUsages::TLAS_INPUT)
1020        {
1021            self.require_features(wgt::Features::EXPERIMENTAL_RAY_QUERY)?;
1022        }
1023
1024        if desc.usage.contains(wgt::BufferUsages::INDEX)
1025            && desc.usage.contains(
1026                wgt::BufferUsages::VERTEX
1027                    | wgt::BufferUsages::UNIFORM
1028                    | wgt::BufferUsages::INDIRECT
1029                    | wgt::BufferUsages::STORAGE,
1030            )
1031        {
1032            self.require_downlevel_flags(wgt::DownlevelFlags::UNRESTRICTED_INDEX_BUFFER)?;
1033        }
1034
1035        if desc.usage.is_empty() || desc.usage.contains_unknown_bits() {
1036            return Err(resource::CreateBufferError::InvalidUsage(desc.usage));
1037        }
1038
1039        if !self
1040            .features
1041            .contains(wgt::Features::MAPPABLE_PRIMARY_BUFFERS)
1042        {
1043            use wgt::BufferUsages as Bu;
1044            let write_mismatch = desc.usage.contains(Bu::MAP_WRITE)
1045                && !(Bu::MAP_WRITE | Bu::COPY_SRC).contains(desc.usage);
1046            let read_mismatch = desc.usage.contains(Bu::MAP_READ)
1047                && !(Bu::MAP_READ | Bu::COPY_DST).contains(desc.usage);
1048            if write_mismatch || read_mismatch {
1049                return Err(resource::CreateBufferError::UsageMismatch(desc.usage));
1050            }
1051        }
1052
1053        let mut usage = conv::map_buffer_usage(desc.usage);
1054
1055        if desc.usage.contains(wgt::BufferUsages::INDIRECT) {
1056            self.require_downlevel_flags(wgt::DownlevelFlags::INDIRECT_EXECUTION)?;
1057            // We are going to be reading from it, internally;
1058            // when validating the content of the buffer
1059            usage |= wgt::BufferUses::STORAGE_READ_ONLY | wgt::BufferUses::STORAGE_READ_WRITE;
1060        }
1061
1062        if desc.usage.contains(wgt::BufferUsages::QUERY_RESOLVE) {
1063            usage |= TIMESTAMP_NORMALIZATION_BUFFER_USES;
1064        }
1065
1066        if desc.mapped_at_creation {
1067            if !desc.size.is_multiple_of(wgt::COPY_BUFFER_ALIGNMENT) {
1068                return Err(resource::CreateBufferError::UnalignedSize);
1069            }
1070            if !desc.usage.contains(wgt::BufferUsages::MAP_WRITE) {
1071                // we are going to be copying into it, internally
1072                usage |= wgt::BufferUses::COPY_DST;
1073            }
1074        } else {
1075            // We are required to zero out (initialize) all memory. This is done
1076            // on demand using clear_buffer which requires write transfer usage!
1077            usage |= wgt::BufferUses::COPY_DST;
1078        }
1079
1080        let actual_size = if desc.size == 0 {
1081            wgt::COPY_BUFFER_ALIGNMENT
1082        } else if desc.usage.contains(wgt::BufferUsages::VERTEX) {
1083            // Bumping the size by 1 so that we can bind an empty range at the
1084            // end of the buffer.
1085            desc.size + 1
1086        } else {
1087            desc.size
1088        };
1089        let clear_remainder = actual_size % wgt::COPY_BUFFER_ALIGNMENT;
1090        let aligned_size = if clear_remainder != 0 {
1091            actual_size + wgt::COPY_BUFFER_ALIGNMENT - clear_remainder
1092        } else {
1093            actual_size
1094        };
1095
1096        let hal_desc = hal::BufferDescriptor {
1097            label: desc.label.to_hal(self.instance_flags),
1098            size: aligned_size,
1099            usage,
1100            memory_flags: hal::MemoryFlags::empty(),
1101        };
1102        let buffer = unsafe { self.raw().create_buffer(&hal_desc) }
1103            .map_err(|e| self.handle_hal_error_with_nonfatal_oom(e))?;
1104
1105        let timestamp_normalization_bind_group = Snatchable::new(unsafe {
1106            // SAFETY: The size passed here must not overflow the buffer.
1107            self.timestamp_normalizer
1108                .get()
1109                .unwrap()
1110                .create_normalization_bind_group(
1111                    self,
1112                    &*buffer,
1113                    desc.label.as_deref(),
1114                    wgt::BufferSize::new(hal_desc.size).unwrap(),
1115                    desc.usage,
1116                )
1117        }?);
1118
1119        let indirect_validation_bind_groups =
1120            self.create_indirect_validation_bind_groups(buffer.as_ref(), desc.size, desc.usage)?;
1121
1122        let buffer = Buffer {
1123            raw: Snatchable::new(buffer),
1124            device: self.clone(),
1125            usage: desc.usage,
1126            size: desc.size,
1127            initialization_status: RwLock::new(
1128                rank::BUFFER_INITIALIZATION_STATUS,
1129                BufferInitTracker::new(aligned_size),
1130            ),
1131            map_state: Mutex::new(rank::BUFFER_MAP_STATE, resource::BufferMapState::Idle),
1132            label: desc.label.to_string(),
1133            tracking_data: TrackingData::new(self.tracker_indices.buffers.clone()),
1134            bind_groups: Mutex::new(rank::BUFFER_BIND_GROUPS, WeakVec::new()),
1135            timestamp_normalization_bind_group,
1136            indirect_validation_bind_groups,
1137        };
1138
1139        let buffer = Arc::new(buffer);
1140
1141        let buffer_use = if !desc.mapped_at_creation {
1142            wgt::BufferUses::empty()
1143        } else if desc.usage.contains(wgt::BufferUsages::MAP_WRITE) {
1144            // buffer is mappable, so we are just doing that at start
1145            let map_size = buffer.size;
1146            let mapping = if map_size == 0 {
1147                hal::BufferMapping {
1148                    ptr: core::ptr::NonNull::dangling(),
1149                    is_coherent: true,
1150                }
1151            } else {
1152                let snatch_guard: SnatchGuard = self.snatchable_lock.read();
1153                map_buffer(&buffer, 0, map_size, HostMap::Write, &snatch_guard)?
1154            };
1155            *buffer.map_state.lock() = resource::BufferMapState::Active {
1156                mapping,
1157                range: 0..map_size,
1158                host: HostMap::Write,
1159            };
1160            wgt::BufferUses::MAP_WRITE
1161        } else {
1162            let mut staging_buffer =
1163                StagingBuffer::new(self, wgt::BufferSize::new(aligned_size).unwrap())?;
1164
1165            // Zero initialize memory and then mark the buffer as initialized
1166            // (it's guaranteed that this is the case by the time the buffer is usable)
1167            staging_buffer.write_zeros();
1168            buffer.initialization_status.write().drain(0..aligned_size);
1169
1170            *buffer.map_state.lock() = resource::BufferMapState::Init { staging_buffer };
1171            wgt::BufferUses::COPY_DST
1172        };
1173
1174        self.trackers
1175            .lock()
1176            .buffers
1177            .insert_single(&buffer, buffer_use);
1178
1179        Ok(buffer)
1180    }
1181
1182    #[cfg(feature = "replay")]
1183    pub fn set_buffer_data(
1184        self: &Arc<Self>,
1185        buffer: &Arc<Buffer>,
1186        offset: wgt::BufferAddress,
1187        data: &[u8],
1188    ) -> resource::BufferAccessResult {
1189        use crate::resource::RawResourceAccess;
1190
1191        let device = &buffer.device;
1192
1193        device.check_is_valid()?;
1194        buffer.check_usage(wgt::BufferUsages::MAP_WRITE)?;
1195
1196        let last_submission = device
1197            .get_queue()
1198            .and_then(|queue| queue.lock_life().get_buffer_latest_submission_index(buffer));
1199
1200        if let Some(last_submission) = last_submission {
1201            device.wait_for_submit(last_submission)?;
1202        }
1203
1204        let snatch_guard = device.snatchable_lock.read();
1205        let raw_buf = buffer.try_raw(&snatch_guard)?;
1206
1207        if offset > buffer.size {
1208            return Err(resource::BufferAccessError::OutOfBoundsStartOffsetOverrun {
1209                index: offset,
1210                max: buffer.size,
1211            });
1212        } else if buffer.size - offset < u64::try_from(data.len()).unwrap() {
1213            return Err(resource::BufferAccessError::OutOfBoundsEndOffsetOverrun {
1214                index: offset,
1215                size: u64::try_from(data.len()).unwrap(),
1216                max: buffer.size,
1217            });
1218        }
1219
1220        let mapping = unsafe {
1221            device
1222                .raw()
1223                .map_buffer(raw_buf, offset..offset + u64::try_from(data.len()).unwrap())
1224        }
1225        .map_err(|e| device.handle_hal_error(e))?;
1226
1227        unsafe { core::ptr::copy_nonoverlapping(data.as_ptr(), mapping.ptr.as_ptr(), data.len()) };
1228
1229        if !mapping.is_coherent {
1230            #[allow(clippy::single_range_in_vec_init)]
1231            unsafe {
1232                device
1233                    .raw()
1234                    .flush_mapped_ranges(raw_buf, &[offset..offset + data.len() as u64])
1235            };
1236        }
1237
1238        unsafe { device.raw().unmap_buffer(raw_buf) };
1239
1240        Ok(())
1241    }
1242
1243    pub(crate) fn create_texture_from_hal(
1244        self: &Arc<Self>,
1245        hal_texture: Box<dyn hal::DynTexture>,
1246        desc: &resource::TextureDescriptor,
1247        initial_state: wgt::TextureUses,
1248    ) -> Result<Arc<Texture>, resource::CreateTextureError> {
1249        let format_features = self
1250            .describe_format_features(desc.format)
1251            .map_err(|error| resource::CreateTextureError::MissingFeatures(desc.format, error))?;
1252
1253        unsafe { self.raw().add_raw_texture(&*hal_texture) };
1254
1255        let texture = Texture::new(
1256            self,
1257            resource::TextureInner::Native { raw: hal_texture },
1258            conv::map_texture_usage(desc.usage, desc.format.into(), format_features.flags),
1259            desc,
1260            format_features,
1261            resource::TextureClearMode::None,
1262            false,
1263        );
1264
1265        let texture = Arc::new(texture);
1266
1267        self.trackers
1268            .lock()
1269            .textures
1270            .insert_single(&texture, initial_state);
1271
1272        Ok(texture)
1273    }
1274
1275    /// # Safety
1276    ///
1277    /// - `hal_buffer` must have been created on this device.
1278    /// - `hal_buffer` must have been created respecting `desc` (in particular, the size).
1279    /// - `hal_buffer` must be initialized.
1280    /// - `hal_buffer` must not have zero size.
1281    pub(crate) unsafe fn create_buffer_from_hal(
1282        self: &Arc<Self>,
1283        hal_buffer: Box<dyn hal::DynBuffer>,
1284        desc: &resource::BufferDescriptor,
1285    ) -> (Fallible<Buffer>, Option<resource::CreateBufferError>) {
1286        let timestamp_normalization_bind_group = unsafe {
1287            match self
1288                .timestamp_normalizer
1289                .get()
1290                .unwrap()
1291                .create_normalization_bind_group(
1292                    self,
1293                    &*hal_buffer,
1294                    desc.label.as_deref(),
1295                    wgt::BufferSize::new(desc.size).unwrap(),
1296                    desc.usage,
1297                ) {
1298                Ok(bg) => Snatchable::new(bg),
1299                Err(e) => {
1300                    return (
1301                        Fallible::Invalid(Arc::new(desc.label.to_string())),
1302                        Some(e.into()),
1303                    )
1304                }
1305            }
1306        };
1307
1308        let indirect_validation_bind_groups = match self.create_indirect_validation_bind_groups(
1309            hal_buffer.as_ref(),
1310            desc.size,
1311            desc.usage,
1312        ) {
1313            Ok(ok) => ok,
1314            Err(e) => return (Fallible::Invalid(Arc::new(desc.label.to_string())), Some(e)),
1315        };
1316
1317        unsafe { self.raw().add_raw_buffer(&*hal_buffer) };
1318
1319        let buffer = Buffer {
1320            raw: Snatchable::new(hal_buffer),
1321            device: self.clone(),
1322            usage: desc.usage,
1323            size: desc.size,
1324            initialization_status: RwLock::new(
1325                rank::BUFFER_INITIALIZATION_STATUS,
1326                BufferInitTracker::new(0),
1327            ),
1328            map_state: Mutex::new(rank::BUFFER_MAP_STATE, resource::BufferMapState::Idle),
1329            label: desc.label.to_string(),
1330            tracking_data: TrackingData::new(self.tracker_indices.buffers.clone()),
1331            bind_groups: Mutex::new(rank::BUFFER_BIND_GROUPS, WeakVec::new()),
1332            timestamp_normalization_bind_group,
1333            indirect_validation_bind_groups,
1334        };
1335
1336        let buffer = Arc::new(buffer);
1337
1338        self.trackers
1339            .lock()
1340            .buffers
1341            .insert_single(&buffer, wgt::BufferUses::empty());
1342
1343        (Fallible::Valid(buffer), None)
1344    }
1345
1346    fn create_indirect_validation_bind_groups(
1347        &self,
1348        raw_buffer: &dyn hal::DynBuffer,
1349        buffer_size: u64,
1350        usage: wgt::BufferUsages,
1351    ) -> Result<Snatchable<crate::indirect_validation::BindGroups>, resource::CreateBufferError>
1352    {
1353        if !usage.contains(wgt::BufferUsages::INDIRECT) {
1354            return Ok(Snatchable::empty());
1355        }
1356
1357        let Some(ref indirect_validation) = self.indirect_validation else {
1358            return Ok(Snatchable::empty());
1359        };
1360
1361        let bind_groups = crate::indirect_validation::BindGroups::new(
1362            indirect_validation,
1363            self,
1364            buffer_size,
1365            raw_buffer,
1366        )
1367        .map_err(resource::CreateBufferError::IndirectValidationBindGroup)?;
1368
1369        if let Some(bind_groups) = bind_groups {
1370            Ok(Snatchable::new(bind_groups))
1371        } else {
1372            Ok(Snatchable::empty())
1373        }
1374    }
1375
1376    pub fn create_texture(
1377        self: &Arc<Self>,
1378        desc: &resource::TextureDescriptor,
1379    ) -> Result<Arc<Texture>, resource::CreateTextureError> {
1380        use resource::{CreateTextureError, TextureDimensionError};
1381
1382        self.check_is_valid()?;
1383
1384        if desc.usage.is_empty() || desc.usage.contains_unknown_bits() {
1385            return Err(CreateTextureError::InvalidUsage(desc.usage));
1386        }
1387
1388        conv::check_texture_dimension_size(
1389            desc.dimension,
1390            desc.size,
1391            desc.sample_count,
1392            &self.limits,
1393        )?;
1394
1395        if desc.dimension != wgt::TextureDimension::D2 {
1396            // Depth textures can only be 2D
1397            if desc.format.is_depth_stencil_format() {
1398                return Err(CreateTextureError::InvalidDepthDimension(
1399                    desc.dimension,
1400                    desc.format,
1401                ));
1402            }
1403        }
1404
1405        if desc.dimension != wgt::TextureDimension::D2
1406            && desc.dimension != wgt::TextureDimension::D3
1407        {
1408            // Compressed textures can only be 2D or 3D
1409            if desc.format.is_compressed() {
1410                return Err(CreateTextureError::InvalidCompressedDimension(
1411                    desc.dimension,
1412                    desc.format,
1413                ));
1414            }
1415
1416            // Renderable textures can only be 2D or 3D
1417            if desc.usage.contains(wgt::TextureUsages::RENDER_ATTACHMENT) {
1418                return Err(CreateTextureError::InvalidDimensionUsages(
1419                    wgt::TextureUsages::RENDER_ATTACHMENT,
1420                    desc.dimension,
1421                ));
1422            }
1423        }
1424
1425        if desc.format.is_compressed() {
1426            let (block_width, block_height) = desc.format.block_dimensions();
1427
1428            if !desc.size.width.is_multiple_of(block_width) {
1429                return Err(CreateTextureError::InvalidDimension(
1430                    TextureDimensionError::NotMultipleOfBlockWidth {
1431                        width: desc.size.width,
1432                        block_width,
1433                        format: desc.format,
1434                    },
1435                ));
1436            }
1437
1438            if !desc.size.height.is_multiple_of(block_height) {
1439                return Err(CreateTextureError::InvalidDimension(
1440                    TextureDimensionError::NotMultipleOfBlockHeight {
1441                        height: desc.size.height,
1442                        block_height,
1443                        format: desc.format,
1444                    },
1445                ));
1446            }
1447
1448            if desc.dimension == wgt::TextureDimension::D3 {
1449                // Only BCn formats with Sliced 3D feature can be used for 3D textures
1450                if desc.format.is_bcn() {
1451                    self.require_features(wgt::Features::TEXTURE_COMPRESSION_BC_SLICED_3D)
1452                        .map_err(|error| CreateTextureError::MissingFeatures(desc.format, error))?;
1453                } else if desc.format.is_astc() {
1454                    self.require_features(wgt::Features::TEXTURE_COMPRESSION_ASTC_SLICED_3D)
1455                        .map_err(|error| CreateTextureError::MissingFeatures(desc.format, error))?;
1456                } else {
1457                    return Err(CreateTextureError::InvalidCompressedDimension(
1458                        desc.dimension,
1459                        desc.format,
1460                    ));
1461                }
1462            }
1463        }
1464
1465        let mips = desc.mip_level_count;
1466        let max_levels_allowed = desc.size.max_mips(desc.dimension).min(hal::MAX_MIP_LEVELS);
1467        if mips == 0 || mips > max_levels_allowed {
1468            return Err(CreateTextureError::InvalidMipLevelCount {
1469                requested: mips,
1470                maximum: max_levels_allowed,
1471            });
1472        }
1473
1474        {
1475            let (mut width_multiple, mut height_multiple) = desc.format.size_multiple_requirement();
1476
1477            if desc.format.is_multi_planar_format() {
1478                // TODO(https://github.com/gfx-rs/wgpu/issues/8491): fix
1479                // `mip_level_size` calculation for these formats and relax this
1480                // restriction.
1481                width_multiple <<= desc.mip_level_count.saturating_sub(1);
1482                height_multiple <<= desc.mip_level_count.saturating_sub(1);
1483            }
1484
1485            if !desc.size.width.is_multiple_of(width_multiple) {
1486                return Err(CreateTextureError::InvalidDimension(
1487                    TextureDimensionError::WidthNotMultipleOf {
1488                        width: desc.size.width,
1489                        multiple: width_multiple,
1490                        format: desc.format,
1491                    },
1492                ));
1493            }
1494
1495            if !desc.size.height.is_multiple_of(height_multiple) {
1496                return Err(CreateTextureError::InvalidDimension(
1497                    TextureDimensionError::HeightNotMultipleOf {
1498                        height: desc.size.height,
1499                        multiple: height_multiple,
1500                        format: desc.format,
1501                    },
1502                ));
1503            }
1504        }
1505
1506        if desc.usage.contains(wgt::TextureUsages::TRANSIENT) {
1507            if !desc.usage.contains(wgt::TextureUsages::RENDER_ATTACHMENT) {
1508                return Err(CreateTextureError::InvalidUsage(
1509                    wgt::TextureUsages::TRANSIENT,
1510                ));
1511            }
1512            let extra_usage =
1513                desc.usage - wgt::TextureUsages::TRANSIENT - wgt::TextureUsages::RENDER_ATTACHMENT;
1514            if !extra_usage.is_empty() {
1515                return Err(CreateTextureError::IncompatibleUsage(
1516                    wgt::TextureUsages::TRANSIENT,
1517                    extra_usage,
1518                ));
1519            }
1520        }
1521
1522        let format_features = self
1523            .describe_format_features(desc.format)
1524            .map_err(|error| CreateTextureError::MissingFeatures(desc.format, error))?;
1525
1526        if desc.sample_count > 1 {
1527            // <https://www.w3.org/TR/2025/CRD-webgpu-20251120/#:~:text=If%20descriptor%2EsampleCount%20%3E%201>
1528            //
1529            // Note that there are also some checks related to the sample count
1530            // in [`conv::check_texture_dimension_size`].
1531
1532            if desc.mip_level_count != 1 {
1533                return Err(CreateTextureError::InvalidMipLevelCount {
1534                    requested: desc.mip_level_count,
1535                    maximum: 1,
1536                });
1537            }
1538
1539            if desc.size.depth_or_array_layers != 1
1540                && !self.features.contains(wgt::Features::MULTISAMPLE_ARRAY)
1541            {
1542                return Err(CreateTextureError::InvalidDimension(
1543                    TextureDimensionError::MultisampledDepthOrArrayLayer(
1544                        desc.size.depth_or_array_layers,
1545                    ),
1546                ));
1547            }
1548
1549            if desc.usage.contains(wgt::TextureUsages::STORAGE_BINDING) {
1550                return Err(CreateTextureError::InvalidMultisampledStorageBinding);
1551            }
1552
1553            if !desc.usage.contains(wgt::TextureUsages::RENDER_ATTACHMENT) {
1554                return Err(CreateTextureError::MultisampledNotRenderAttachment);
1555            }
1556
1557            if !format_features.flags.intersects(
1558                wgt::TextureFormatFeatureFlags::MULTISAMPLE_X4
1559                    | wgt::TextureFormatFeatureFlags::MULTISAMPLE_X2
1560                    | wgt::TextureFormatFeatureFlags::MULTISAMPLE_X8
1561                    | wgt::TextureFormatFeatureFlags::MULTISAMPLE_X16,
1562            ) {
1563                return Err(CreateTextureError::InvalidMultisampledFormat(desc.format));
1564            }
1565
1566            if !format_features
1567                .flags
1568                .sample_count_supported(desc.sample_count)
1569            {
1570                return Err(CreateTextureError::InvalidSampleCount(
1571                    desc.sample_count,
1572                    desc.format,
1573                    desc.format
1574                        .guaranteed_format_features(self.features)
1575                        .flags
1576                        .supported_sample_counts(),
1577                    self.adapter
1578                        .get_texture_format_features(desc.format)
1579                        .flags
1580                        .supported_sample_counts(),
1581                ));
1582            };
1583        }
1584
1585        let missing_allowed_usages = match desc.format.planes() {
1586            Some(planes) => {
1587                let mut planes_usages = wgt::TextureUsages::all();
1588                for plane in 0..planes {
1589                    let aspect = wgt::TextureAspect::from_plane(plane).unwrap();
1590                    let format = desc.format.aspect_specific_format(aspect).unwrap();
1591                    let format_features = self
1592                        .describe_format_features(format)
1593                        .map_err(|error| CreateTextureError::MissingFeatures(desc.format, error))?;
1594
1595                    planes_usages &= format_features.allowed_usages;
1596                }
1597
1598                desc.usage - planes_usages
1599            }
1600            None => desc.usage - format_features.allowed_usages,
1601        };
1602
1603        if !missing_allowed_usages.is_empty() {
1604            // detect downlevel incompatibilities
1605            let wgpu_allowed_usages = desc
1606                .format
1607                .guaranteed_format_features(self.features)
1608                .allowed_usages;
1609            let wgpu_missing_usages = desc.usage - wgpu_allowed_usages;
1610            return Err(CreateTextureError::InvalidFormatUsages(
1611                missing_allowed_usages,
1612                desc.format,
1613                wgpu_missing_usages.is_empty(),
1614            ));
1615        }
1616
1617        let mut hal_view_formats = Vec::new();
1618        for format in desc.view_formats.iter() {
1619            if desc.format == *format {
1620                continue;
1621            }
1622            if desc.format.remove_srgb_suffix() != format.remove_srgb_suffix() {
1623                return Err(CreateTextureError::InvalidViewFormat(*format, desc.format));
1624            }
1625            hal_view_formats.push(*format);
1626        }
1627        if !hal_view_formats.is_empty() {
1628            self.require_downlevel_flags(wgt::DownlevelFlags::VIEW_FORMATS)?;
1629        }
1630
1631        let hal_usage = conv::map_texture_usage_for_texture(desc, &format_features);
1632
1633        let hal_desc = hal::TextureDescriptor {
1634            label: desc.label.to_hal(self.instance_flags),
1635            size: desc.size,
1636            mip_level_count: desc.mip_level_count,
1637            sample_count: desc.sample_count,
1638            dimension: desc.dimension,
1639            format: desc.format,
1640            usage: hal_usage,
1641            memory_flags: hal::MemoryFlags::empty(),
1642            view_formats: hal_view_formats,
1643        };
1644
1645        let raw_texture = unsafe { self.raw().create_texture(&hal_desc) }
1646            .map_err(|e| self.handle_hal_error_with_nonfatal_oom(e))?;
1647
1648        let clear_mode = if hal_usage
1649            .intersects(wgt::TextureUses::DEPTH_STENCIL_WRITE | wgt::TextureUses::COLOR_TARGET)
1650            && desc.dimension == wgt::TextureDimension::D2
1651        {
1652            let (is_color, usage) = if desc.format.is_depth_stencil_format() {
1653                (false, wgt::TextureUses::DEPTH_STENCIL_WRITE)
1654            } else {
1655                (true, wgt::TextureUses::COLOR_TARGET)
1656            };
1657
1658            let clear_label = hal_label(
1659                Some("(wgpu internal) clear texture view"),
1660                self.instance_flags,
1661            );
1662
1663            let mut clear_views = SmallVec::new();
1664            for mip_level in 0..desc.mip_level_count {
1665                for array_layer in 0..desc.size.depth_or_array_layers {
1666                    macro_rules! push_clear_view {
1667                        ($format:expr, $aspect:expr) => {
1668                            let desc = hal::TextureViewDescriptor {
1669                                label: clear_label,
1670                                format: $format,
1671                                dimension: TextureViewDimension::D2,
1672                                usage,
1673                                range: wgt::ImageSubresourceRange {
1674                                    aspect: $aspect,
1675                                    base_mip_level: mip_level,
1676                                    mip_level_count: Some(1),
1677                                    base_array_layer: array_layer,
1678                                    array_layer_count: Some(1),
1679                                },
1680                            };
1681                            clear_views.push(ManuallyDrop::new(
1682                                unsafe {
1683                                    self.raw().create_texture_view(raw_texture.as_ref(), &desc)
1684                                }
1685                                .map_err(|e| self.handle_hal_error(e))?,
1686                            ));
1687                        };
1688                    }
1689
1690                    if let Some(planes) = desc.format.planes() {
1691                        for plane in 0..planes {
1692                            let aspect = wgt::TextureAspect::from_plane(plane).unwrap();
1693                            let format = desc.format.aspect_specific_format(aspect).unwrap();
1694                            push_clear_view!(format, aspect);
1695                        }
1696                    } else {
1697                        push_clear_view!(desc.format, wgt::TextureAspect::All);
1698                    }
1699                }
1700            }
1701            resource::TextureClearMode::RenderPass {
1702                clear_views,
1703                is_color,
1704            }
1705        } else {
1706            resource::TextureClearMode::BufferCopy
1707        };
1708
1709        let texture = Texture::new(
1710            self,
1711            resource::TextureInner::Native { raw: raw_texture },
1712            hal_usage,
1713            desc,
1714            format_features,
1715            clear_mode,
1716            true,
1717        );
1718
1719        let texture = Arc::new(texture);
1720
1721        self.trackers
1722            .lock()
1723            .textures
1724            .insert_single(&texture, wgt::TextureUses::UNINITIALIZED);
1725
1726        Ok(texture)
1727    }
1728
1729    pub fn create_texture_view(
1730        self: &Arc<Self>,
1731        texture: &Arc<Texture>,
1732        desc: &resource::TextureViewDescriptor,
1733    ) -> Result<Arc<TextureView>, resource::CreateTextureViewError> {
1734        self.check_is_valid()?;
1735
1736        let snatch_guard = texture.device.snatchable_lock.read();
1737
1738        let texture_raw = texture.try_raw(&snatch_guard)?;
1739
1740        // resolve TextureViewDescriptor defaults
1741        // https://gpuweb.github.io/gpuweb/#abstract-opdef-resolving-gputextureviewdescriptor-defaults
1742        let resolved_format = desc.format.unwrap_or_else(|| {
1743            texture
1744                .desc
1745                .format
1746                .aspect_specific_format(desc.range.aspect)
1747                .unwrap_or(texture.desc.format)
1748        });
1749
1750        let resolved_dimension = desc
1751            .dimension
1752            .unwrap_or_else(|| match texture.desc.dimension {
1753                wgt::TextureDimension::D1 => TextureViewDimension::D1,
1754                wgt::TextureDimension::D2 => {
1755                    if texture.desc.array_layer_count() == 1 {
1756                        TextureViewDimension::D2
1757                    } else {
1758                        TextureViewDimension::D2Array
1759                    }
1760                }
1761                wgt::TextureDimension::D3 => TextureViewDimension::D3,
1762            });
1763
1764        let resolved_mip_level_count = desc.range.mip_level_count.unwrap_or_else(|| {
1765            texture
1766                .desc
1767                .mip_level_count
1768                .saturating_sub(desc.range.base_mip_level)
1769        });
1770
1771        let resolved_array_layer_count =
1772            desc.range
1773                .array_layer_count
1774                .unwrap_or_else(|| match resolved_dimension {
1775                    TextureViewDimension::D1
1776                    | TextureViewDimension::D2
1777                    | TextureViewDimension::D3 => 1,
1778                    TextureViewDimension::Cube => 6,
1779                    TextureViewDimension::D2Array | TextureViewDimension::CubeArray => texture
1780                        .desc
1781                        .array_layer_count()
1782                        .saturating_sub(desc.range.base_array_layer),
1783                });
1784
1785        let resolved_usage = {
1786            let usage = desc.usage.unwrap_or(wgt::TextureUsages::empty());
1787            if usage.is_empty() {
1788                texture.desc.usage
1789            } else if texture.desc.usage.contains(usage) {
1790                usage
1791            } else {
1792                return Err(resource::CreateTextureViewError::InvalidTextureViewUsage {
1793                    view: usage,
1794                    texture: texture.desc.usage,
1795                });
1796            }
1797        };
1798
1799        let format_features = self.describe_format_features(resolved_format)?;
1800        let allowed_format_usages = format_features.allowed_usages;
1801        if resolved_usage.contains(wgt::TextureUsages::RENDER_ATTACHMENT)
1802            && !allowed_format_usages.contains(wgt::TextureUsages::RENDER_ATTACHMENT)
1803        {
1804            return Err(
1805                resource::CreateTextureViewError::TextureViewFormatNotRenderable(resolved_format),
1806            );
1807        }
1808
1809        if resolved_usage.contains(wgt::TextureUsages::STORAGE_BINDING)
1810            && !allowed_format_usages.contains(wgt::TextureUsages::STORAGE_BINDING)
1811        {
1812            return Err(
1813                resource::CreateTextureViewError::TextureViewFormatNotStorage(resolved_format),
1814            );
1815        }
1816
1817        // validate TextureViewDescriptor
1818
1819        let aspects = hal::FormatAspects::new(texture.desc.format, desc.range.aspect);
1820        if aspects.is_empty() {
1821            return Err(resource::CreateTextureViewError::InvalidAspect {
1822                texture_format: texture.desc.format,
1823                requested_aspect: desc.range.aspect,
1824            });
1825        }
1826
1827        let format_is_good = if desc.range.aspect == wgt::TextureAspect::All {
1828            resolved_format == texture.desc.format
1829                || texture.desc.view_formats.contains(&resolved_format)
1830        } else {
1831            Some(resolved_format)
1832                == texture
1833                    .desc
1834                    .format
1835                    .aspect_specific_format(desc.range.aspect)
1836        };
1837        if !format_is_good {
1838            return Err(resource::CreateTextureViewError::FormatReinterpretation {
1839                texture: texture.desc.format,
1840                view: resolved_format,
1841            });
1842        }
1843
1844        // check if multisampled texture is seen as anything but 2D
1845        if texture.desc.sample_count > 1 && resolved_dimension != TextureViewDimension::D2 {
1846            // Multisample is allowed on 2D arrays, only if explicitly supported
1847            let multisample_array_exception = resolved_dimension == TextureViewDimension::D2Array
1848                && self.features.contains(wgt::Features::MULTISAMPLE_ARRAY);
1849
1850            if !multisample_array_exception {
1851                return Err(
1852                    resource::CreateTextureViewError::InvalidMultisampledTextureViewDimension(
1853                        resolved_dimension,
1854                    ),
1855                );
1856            }
1857        }
1858
1859        // check if the dimension is compatible with the texture
1860        if texture.desc.dimension != resolved_dimension.compatible_texture_dimension() {
1861            return Err(
1862                resource::CreateTextureViewError::InvalidTextureViewDimension {
1863                    view: resolved_dimension,
1864                    texture: texture.desc.dimension,
1865                },
1866            );
1867        }
1868
1869        match resolved_dimension {
1870            TextureViewDimension::D1 | TextureViewDimension::D2 | TextureViewDimension::D3 => {
1871                if resolved_array_layer_count != 1 {
1872                    return Err(resource::CreateTextureViewError::InvalidArrayLayerCount {
1873                        requested: resolved_array_layer_count,
1874                        dim: resolved_dimension,
1875                    });
1876                }
1877            }
1878            TextureViewDimension::Cube => {
1879                if resolved_array_layer_count != 6 {
1880                    return Err(
1881                        resource::CreateTextureViewError::InvalidCubemapTextureDepth {
1882                            depth: resolved_array_layer_count,
1883                        },
1884                    );
1885                }
1886            }
1887            TextureViewDimension::CubeArray => {
1888                if !resolved_array_layer_count.is_multiple_of(6) {
1889                    return Err(
1890                        resource::CreateTextureViewError::InvalidCubemapArrayTextureDepth {
1891                            depth: resolved_array_layer_count,
1892                        },
1893                    );
1894                }
1895            }
1896            _ => {}
1897        }
1898
1899        match resolved_dimension {
1900            TextureViewDimension::Cube | TextureViewDimension::CubeArray => {
1901                if texture.desc.size.width != texture.desc.size.height {
1902                    return Err(resource::CreateTextureViewError::InvalidCubeTextureViewSize);
1903                }
1904            }
1905            _ => {}
1906        }
1907
1908        if resolved_mip_level_count == 0 {
1909            return Err(resource::CreateTextureViewError::ZeroMipLevelCount);
1910        }
1911
1912        let mip_level_end = desc
1913            .range
1914            .base_mip_level
1915            .saturating_add(resolved_mip_level_count);
1916
1917        let level_end = texture.desc.mip_level_count;
1918        if mip_level_end > level_end {
1919            return Err(resource::CreateTextureViewError::TooManyMipLevels {
1920                base_mip_level: desc.range.base_mip_level,
1921                mip_level_count: resolved_mip_level_count,
1922                total: level_end,
1923            });
1924        }
1925
1926        if resolved_array_layer_count == 0 {
1927            return Err(resource::CreateTextureViewError::ZeroArrayLayerCount);
1928        }
1929
1930        let array_layer_end = desc
1931            .range
1932            .base_array_layer
1933            .saturating_add(resolved_array_layer_count);
1934
1935        let layer_end = texture.desc.array_layer_count();
1936        if array_layer_end > layer_end {
1937            return Err(resource::CreateTextureViewError::TooManyArrayLayers {
1938                base_array_layer: desc.range.base_array_layer,
1939                array_layer_count: resolved_array_layer_count,
1940                total: layer_end,
1941            });
1942        };
1943
1944        // https://gpuweb.github.io/gpuweb/#abstract-opdef-renderable-texture-view
1945        let render_extent = 'error: {
1946            if !resolved_usage.contains(wgt::TextureUsages::RENDER_ATTACHMENT) {
1947                break 'error Err(TextureViewNotRenderableReason::Usage(resolved_usage));
1948            }
1949
1950            let allowed_view_dimensions = [
1951                TextureViewDimension::D2,
1952                TextureViewDimension::D2Array,
1953                TextureViewDimension::D3,
1954            ];
1955            if !allowed_view_dimensions.contains(&resolved_dimension) {
1956                break 'error Err(TextureViewNotRenderableReason::Dimension(
1957                    resolved_dimension,
1958                ));
1959            }
1960
1961            if resolved_mip_level_count != 1 {
1962                break 'error Err(TextureViewNotRenderableReason::MipLevelCount(
1963                    resolved_mip_level_count,
1964                ));
1965            }
1966
1967            if resolved_array_layer_count != 1
1968                && !(self.features.contains(wgt::Features::MULTIVIEW))
1969            {
1970                break 'error Err(TextureViewNotRenderableReason::ArrayLayerCount(
1971                    resolved_array_layer_count,
1972                ));
1973            }
1974
1975            if !texture.desc.format.is_multi_planar_format()
1976                && aspects != hal::FormatAspects::from(texture.desc.format)
1977            {
1978                break 'error Err(TextureViewNotRenderableReason::Aspects(aspects));
1979            }
1980
1981            Ok(texture
1982                .desc
1983                .compute_render_extent(desc.range.base_mip_level, desc.range.aspect.to_plane()))
1984        };
1985
1986        // filter the usages based on the other criteria
1987        let usage = {
1988            let resolved_hal_usage = conv::map_texture_usage(
1989                resolved_usage,
1990                resolved_format.into(),
1991                format_features.flags,
1992            );
1993            let mask_copy = !(wgt::TextureUses::COPY_SRC | wgt::TextureUses::COPY_DST);
1994            let mask_dimension = match resolved_dimension {
1995                TextureViewDimension::Cube | TextureViewDimension::CubeArray => {
1996                    wgt::TextureUses::RESOURCE
1997                }
1998                TextureViewDimension::D3 => {
1999                    wgt::TextureUses::RESOURCE
2000                        | wgt::TextureUses::STORAGE_READ_ONLY
2001                        | wgt::TextureUses::STORAGE_WRITE_ONLY
2002                        | wgt::TextureUses::STORAGE_READ_WRITE
2003                }
2004                _ => wgt::TextureUses::all(),
2005            };
2006            let mask_mip_level = if resolved_mip_level_count == 1 {
2007                wgt::TextureUses::all()
2008            } else {
2009                wgt::TextureUses::RESOURCE
2010            };
2011            resolved_hal_usage & mask_copy & mask_dimension & mask_mip_level
2012        };
2013
2014        // use the combined depth-stencil format for the view
2015        let format = if resolved_format.is_depth_stencil_component(texture.desc.format) {
2016            texture.desc.format
2017        } else {
2018            resolved_format
2019        };
2020
2021        let resolved_range = wgt::ImageSubresourceRange {
2022            aspect: desc.range.aspect,
2023            base_mip_level: desc.range.base_mip_level,
2024            mip_level_count: Some(resolved_mip_level_count),
2025            base_array_layer: desc.range.base_array_layer,
2026            array_layer_count: Some(resolved_array_layer_count),
2027        };
2028
2029        let hal_desc = hal::TextureViewDescriptor {
2030            label: desc.label.to_hal(self.instance_flags),
2031            format,
2032            dimension: resolved_dimension,
2033            usage,
2034            range: resolved_range,
2035        };
2036
2037        let raw = unsafe { self.raw().create_texture_view(texture_raw, &hal_desc) }
2038            .map_err(|e| self.handle_hal_error(e))?;
2039
2040        let selector = TextureSelector {
2041            mips: desc.range.base_mip_level..mip_level_end,
2042            layers: desc.range.base_array_layer..array_layer_end,
2043        };
2044
2045        let view = TextureView {
2046            raw: Snatchable::new(raw),
2047            parent: texture.clone(),
2048            device: self.clone(),
2049            desc: resource::HalTextureViewDescriptor {
2050                texture_format: texture.desc.format,
2051                format: resolved_format,
2052                dimension: resolved_dimension,
2053                usage: resolved_usage,
2054                range: resolved_range,
2055            },
2056            format_features: texture.format_features,
2057            render_extent,
2058            samples: texture.desc.sample_count,
2059            selector,
2060            label: desc.label.to_string(),
2061        };
2062
2063        let view = Arc::new(view);
2064
2065        {
2066            let mut views = texture.views.lock();
2067            views.push(Arc::downgrade(&view));
2068        }
2069
2070        Ok(view)
2071    }
2072
2073    pub fn create_external_texture(
2074        self: &Arc<Self>,
2075        desc: &resource::ExternalTextureDescriptor,
2076        planes: &[Arc<TextureView>],
2077    ) -> Result<Arc<ExternalTexture>, resource::CreateExternalTextureError> {
2078        use resource::CreateExternalTextureError;
2079        self.require_features(wgt::Features::EXTERNAL_TEXTURE)?;
2080        self.check_is_valid()?;
2081
2082        if desc.num_planes() != planes.len() {
2083            return Err(CreateExternalTextureError::IncorrectPlaneCount {
2084                format: desc.format,
2085                expected: desc.num_planes(),
2086                provided: planes.len(),
2087            });
2088        }
2089
2090        let planes = planes
2091            .iter()
2092            .enumerate()
2093            .map(|(i, plane)| {
2094                if plane.samples != 1 {
2095                    return Err(CreateExternalTextureError::InvalidPlaneMultisample(
2096                        plane.samples,
2097                    ));
2098                }
2099
2100                let sample_type = plane
2101                    .desc
2102                    .format
2103                    .sample_type(Some(plane.desc.range.aspect), Some(self.features))
2104                    .unwrap();
2105                if !matches!(sample_type, TextureSampleType::Float { filterable: true }) {
2106                    return Err(CreateExternalTextureError::InvalidPlaneSampleType {
2107                        format: plane.desc.format,
2108                        sample_type,
2109                    });
2110                }
2111
2112                if plane.desc.dimension != TextureViewDimension::D2 {
2113                    return Err(CreateExternalTextureError::InvalidPlaneDimension(
2114                        plane.desc.dimension,
2115                    ));
2116                }
2117
2118                let expected_components = match desc.format {
2119                    wgt::ExternalTextureFormat::Rgba => 4,
2120                    wgt::ExternalTextureFormat::Nv12 => match i {
2121                        0 => 1,
2122                        1 => 2,
2123                        _ => unreachable!(),
2124                    },
2125                    wgt::ExternalTextureFormat::Yu12 => 1,
2126                };
2127                if plane.desc.format.components() != expected_components {
2128                    return Err(CreateExternalTextureError::InvalidPlaneFormat {
2129                        format: desc.format,
2130                        plane: i,
2131                        expected: expected_components,
2132                        provided: plane.desc.format,
2133                    });
2134                }
2135
2136                plane.check_usage(wgt::TextureUsages::TEXTURE_BINDING)?;
2137                Ok(plane.clone())
2138            })
2139            .collect::<Result<_, _>>()?;
2140
2141        let params_data = ExternalTextureParams::from_desc(desc);
2142        let label = desc.label.as_ref().map(|l| alloc::format!("{l} params"));
2143        let params_desc = resource::BufferDescriptor {
2144            label: label.map(Cow::Owned),
2145            size: size_of_val(&params_data) as wgt::BufferAddress,
2146            usage: wgt::BufferUsages::UNIFORM | wgt::BufferUsages::COPY_DST,
2147            mapped_at_creation: false,
2148        };
2149        let params = self.create_buffer(&params_desc)?;
2150        self.get_queue().unwrap().write_buffer(
2151            params.clone(),
2152            0,
2153            bytemuck::bytes_of(&params_data),
2154        )?;
2155
2156        let external_texture = ExternalTexture {
2157            device: self.clone(),
2158            planes,
2159            params,
2160            label: desc.label.to_string(),
2161            tracking_data: TrackingData::new(self.tracker_indices.external_textures.clone()),
2162        };
2163        let external_texture = Arc::new(external_texture);
2164
2165        Ok(external_texture)
2166    }
2167
2168    pub fn create_sampler(
2169        self: &Arc<Self>,
2170        desc: &resource::SamplerDescriptor,
2171    ) -> Result<Arc<Sampler>, resource::CreateSamplerError> {
2172        self.check_is_valid()?;
2173
2174        if desc
2175            .address_modes
2176            .iter()
2177            .any(|am| am == &wgt::AddressMode::ClampToBorder)
2178        {
2179            self.require_features(wgt::Features::ADDRESS_MODE_CLAMP_TO_BORDER)?;
2180        }
2181
2182        if desc.border_color == Some(wgt::SamplerBorderColor::Zero) {
2183            self.require_features(wgt::Features::ADDRESS_MODE_CLAMP_TO_ZERO)?;
2184        }
2185
2186        if desc.lod_min_clamp < 0.0 {
2187            return Err(resource::CreateSamplerError::InvalidLodMinClamp(
2188                desc.lod_min_clamp,
2189            ));
2190        }
2191        if desc.lod_max_clamp < desc.lod_min_clamp {
2192            return Err(resource::CreateSamplerError::InvalidLodMaxClamp {
2193                lod_min_clamp: desc.lod_min_clamp,
2194                lod_max_clamp: desc.lod_max_clamp,
2195            });
2196        }
2197
2198        if desc.anisotropy_clamp < 1 {
2199            return Err(resource::CreateSamplerError::InvalidAnisotropy(
2200                desc.anisotropy_clamp,
2201            ));
2202        }
2203
2204        if desc.anisotropy_clamp != 1 {
2205            if !matches!(desc.min_filter, wgt::FilterMode::Linear) {
2206                return Err(
2207                    resource::CreateSamplerError::InvalidFilterModeWithAnisotropy {
2208                        filter_type: resource::SamplerFilterErrorType::MinFilter,
2209                        filter_mode: desc.min_filter,
2210                        anisotropic_clamp: desc.anisotropy_clamp,
2211                    },
2212                );
2213            }
2214            if !matches!(desc.mag_filter, wgt::FilterMode::Linear) {
2215                return Err(
2216                    resource::CreateSamplerError::InvalidFilterModeWithAnisotropy {
2217                        filter_type: resource::SamplerFilterErrorType::MagFilter,
2218                        filter_mode: desc.mag_filter,
2219                        anisotropic_clamp: desc.anisotropy_clamp,
2220                    },
2221                );
2222            }
2223            if !matches!(desc.mipmap_filter, wgt::MipmapFilterMode::Linear) {
2224                return Err(
2225                    resource::CreateSamplerError::InvalidMipmapFilterModeWithAnisotropy {
2226                        filter_type: resource::SamplerFilterErrorType::MipmapFilter,
2227                        filter_mode: desc.mipmap_filter,
2228                        anisotropic_clamp: desc.anisotropy_clamp,
2229                    },
2230                );
2231            }
2232        }
2233
2234        let anisotropy_clamp = if self
2235            .downlevel
2236            .flags
2237            .contains(wgt::DownlevelFlags::ANISOTROPIC_FILTERING)
2238        {
2239            // Clamp anisotropy clamp to [1, 16] per the wgpu-hal interface
2240            desc.anisotropy_clamp.min(16)
2241        } else {
2242            // If it isn't supported, set this unconditionally to 1
2243            1
2244        };
2245
2246        //TODO: check for wgt::DownlevelFlags::COMPARISON_SAMPLERS
2247
2248        let hal_desc = hal::SamplerDescriptor {
2249            label: desc.label.to_hal(self.instance_flags),
2250            address_modes: desc.address_modes,
2251            mag_filter: desc.mag_filter,
2252            min_filter: desc.min_filter,
2253            mipmap_filter: desc.mipmap_filter,
2254            lod_clamp: desc.lod_min_clamp..desc.lod_max_clamp,
2255            compare: desc.compare,
2256            anisotropy_clamp,
2257            border_color: desc.border_color,
2258        };
2259
2260        let raw = unsafe { self.raw().create_sampler(&hal_desc) }
2261            .map_err(|e| self.handle_hal_error_with_nonfatal_oom(e))?;
2262
2263        let sampler = Sampler {
2264            raw: ManuallyDrop::new(raw),
2265            device: self.clone(),
2266            label: desc.label.to_string(),
2267            tracking_data: TrackingData::new(self.tracker_indices.samplers.clone()),
2268            comparison: desc.compare.is_some(),
2269            filtering: desc.min_filter == wgt::FilterMode::Linear
2270                || desc.mag_filter == wgt::FilterMode::Linear
2271                || desc.mipmap_filter == wgt::MipmapFilterMode::Linear,
2272        };
2273
2274        let sampler = Arc::new(sampler);
2275
2276        Ok(sampler)
2277    }
2278
2279    pub fn create_shader_module<'a>(
2280        self: &Arc<Self>,
2281        desc: &pipeline::ShaderModuleDescriptor<'a>,
2282        source: pipeline::ShaderModuleSource<'a>,
2283    ) -> Result<Arc<pipeline::ShaderModule>, pipeline::CreateShaderModuleError> {
2284        self.check_is_valid()?;
2285
2286        let (module, source) = match source {
2287            #[cfg(feature = "wgsl")]
2288            pipeline::ShaderModuleSource::Wgsl(code) => {
2289                profiling::scope!("naga::front::wgsl::parse");
2290                let capabilities =
2291                    features_to_naga_capabilities(self.features, self.downlevel.flags);
2292                let mut options = naga::front::wgsl::Options::new();
2293                options.capabilities = capabilities;
2294                let mut frontend = naga::front::wgsl::Frontend::new_with_options(options);
2295                let module = frontend.parse(&code).map_err(|inner| {
2296                    pipeline::CreateShaderModuleError::Parsing(naga::error::ShaderError {
2297                        source: code.to_string(),
2298                        label: desc.label.as_ref().map(|l| l.to_string()),
2299                        inner: Box::new(inner),
2300                    })
2301                })?;
2302                (Cow::Owned(module), code.into_owned())
2303            }
2304            #[cfg(feature = "spirv")]
2305            pipeline::ShaderModuleSource::SpirV(spv, options) => {
2306                let parser = naga::front::spv::Frontend::new(spv.iter().cloned(), &options);
2307                profiling::scope!("naga::front::spv::Frontend");
2308                let module = parser.parse().map_err(|inner| {
2309                    pipeline::CreateShaderModuleError::ParsingSpirV(naga::error::ShaderError {
2310                        source: String::new(),
2311                        label: desc.label.as_ref().map(|l| l.to_string()),
2312                        inner: Box::new(inner),
2313                    })
2314                })?;
2315                (Cow::Owned(module), String::new())
2316            }
2317            #[cfg(feature = "glsl")]
2318            pipeline::ShaderModuleSource::Glsl(code, options) => {
2319                let mut parser = naga::front::glsl::Frontend::default();
2320                profiling::scope!("naga::front::glsl::Frontend.parse");
2321                let module = parser.parse(&options, &code).map_err(|inner| {
2322                    pipeline::CreateShaderModuleError::ParsingGlsl(naga::error::ShaderError {
2323                        source: code.to_string(),
2324                        label: desc.label.as_ref().map(|l| l.to_string()),
2325                        inner: Box::new(inner),
2326                    })
2327                })?;
2328                (Cow::Owned(module), code.into_owned())
2329            }
2330            pipeline::ShaderModuleSource::Naga(module) => (module, String::new()),
2331            pipeline::ShaderModuleSource::Dummy(_) => panic!("found `ShaderModuleSource::Dummy`"),
2332        };
2333        for (_, var) in module.global_variables.iter() {
2334            match var.binding {
2335                Some(br) if br.group >= self.limits.max_bind_groups => {
2336                    return Err(pipeline::CreateShaderModuleError::InvalidGroupIndex {
2337                        bind: br,
2338                        group: br.group,
2339                        limit: self.limits.max_bind_groups,
2340                    });
2341                }
2342                _ => continue,
2343            };
2344        }
2345
2346        profiling::scope!("naga::validate");
2347        let debug_source =
2348            if self.instance_flags.contains(wgt::InstanceFlags::DEBUG) && !source.is_empty() {
2349                Some(hal::DebugSource {
2350                    file_name: Cow::Owned(
2351                        desc.label
2352                            .as_ref()
2353                            .map_or("shader".to_string(), |l| l.to_string()),
2354                    ),
2355                    source_code: Cow::Owned(source.clone()),
2356                })
2357            } else {
2358                None
2359            };
2360
2361        let info = create_validator(
2362            self.features,
2363            self.downlevel.flags,
2364            naga::valid::ValidationFlags::all(),
2365        )
2366        .validate(&module)
2367        .map_err(|inner| {
2368            pipeline::CreateShaderModuleError::Validation(naga::error::ShaderError {
2369                source,
2370                label: desc.label.as_ref().map(|l| l.to_string()),
2371                inner: Box::new(inner),
2372            })
2373        })?;
2374
2375        let interface = validation::Interface::new(&module, &info, self.limits.clone());
2376        let hal_shader = hal::ShaderInput::Naga(hal::NagaShader {
2377            module,
2378            info,
2379            debug_source,
2380        });
2381        let hal_desc = hal::ShaderModuleDescriptor {
2382            label: desc.label.to_hal(self.instance_flags),
2383            runtime_checks: desc.runtime_checks,
2384        };
2385        let raw = match unsafe { self.raw().create_shader_module(&hal_desc, hal_shader) } {
2386            Ok(raw) => raw,
2387            Err(error) => {
2388                return Err(match error {
2389                    hal::ShaderError::Device(error) => {
2390                        pipeline::CreateShaderModuleError::Device(self.handle_hal_error(error))
2391                    }
2392                    hal::ShaderError::Compilation(ref msg) => {
2393                        log::error!("Shader error: {msg}");
2394                        pipeline::CreateShaderModuleError::Generation
2395                    }
2396                })
2397            }
2398        };
2399
2400        let module = pipeline::ShaderModule {
2401            raw: ManuallyDrop::new(raw),
2402            device: self.clone(),
2403            interface: ShaderMetaData::Interface(interface),
2404            label: desc.label.to_string(),
2405        };
2406
2407        let module = Arc::new(module);
2408
2409        Ok(module)
2410    }
2411
2412    /// Not a public API. For use by `player` only.
2413    #[allow(unused_unsafe)]
2414    #[doc(hidden)]
2415    pub unsafe fn create_shader_module_passthrough<'a>(
2416        self: &Arc<Self>,
2417        descriptor: &pipeline::ShaderModuleDescriptorPassthrough<'a>,
2418    ) -> Result<Arc<pipeline::ShaderModule>, pipeline::CreateShaderModuleError> {
2419        self.check_is_valid()?;
2420        self.require_features(wgt::Features::PASSTHROUGH_SHADERS)?;
2421
2422        // Mainly important for GLSL or SPIR-V or DXIL, which each take exactly 1 entry point.
2423        if (descriptor.dxil.is_some() || descriptor.glsl.is_some())
2424            && descriptor.entry_points.len() != 1
2425        {
2426            return Err(pipeline::CreateShaderModuleError::IncorrectPassthroughEntryPointCount);
2427        }
2428
2429        let entry_point_hashmap = || {
2430            descriptor
2431                .entry_points
2432                .iter()
2433                .map(|e| (e.name.to_string(), e.workgroup_size))
2434                .collect()
2435        };
2436
2437        let hal_shader = match self.backend() {
2438            wgt::Backend::Vulkan => hal::ShaderInput::SpirV(
2439                descriptor
2440                    .spirv
2441                    .as_ref()
2442                    .ok_or(pipeline::CreateShaderModuleError::NotCompiledForBackend)?,
2443            ),
2444            wgt::Backend::Dx12 => {
2445                if let Some(dxil) = &descriptor.dxil {
2446                    hal::ShaderInput::Dxil { shader: dxil }
2447                } else if let Some(hlsl) = &descriptor.hlsl {
2448                    hal::ShaderInput::Hlsl { shader: hlsl }
2449                } else {
2450                    return Err(pipeline::CreateShaderModuleError::NotCompiledForBackend);
2451                }
2452            }
2453            wgt::Backend::Metal => {
2454                if let Some(metallib) = &descriptor.metallib {
2455                    hal::ShaderInput::MetalLib {
2456                        file: metallib,
2457                        num_workgroups: entry_point_hashmap(),
2458                    }
2459                } else if let Some(msl) = &descriptor.msl {
2460                    hal::ShaderInput::Msl {
2461                        shader: msl,
2462                        num_workgroups: entry_point_hashmap(),
2463                    }
2464                } else {
2465                    return Err(pipeline::CreateShaderModuleError::NotCompiledForBackend);
2466                }
2467            }
2468            wgt::Backend::Gl => hal::ShaderInput::Glsl {
2469                shader: descriptor
2470                    .glsl
2471                    .as_ref()
2472                    .ok_or(pipeline::CreateShaderModuleError::NotCompiledForBackend)?,
2473            },
2474            wgt::Backend::Noop => {
2475                return Err(pipeline::CreateShaderModuleError::NotCompiledForBackend)
2476            }
2477            wgt::Backend::BrowserWebGpu => unreachable!(),
2478        };
2479
2480        let hal_desc = hal::ShaderModuleDescriptor {
2481            label: descriptor.label.to_hal(self.instance_flags),
2482            runtime_checks: wgt::ShaderRuntimeChecks::unchecked(),
2483        };
2484
2485        let raw = match unsafe { self.raw().create_shader_module(&hal_desc, hal_shader) } {
2486            Ok(raw) => raw,
2487            Err(error) => {
2488                return Err(match error {
2489                    hal::ShaderError::Device(error) => {
2490                        pipeline::CreateShaderModuleError::Device(self.handle_hal_error(error))
2491                    }
2492                    hal::ShaderError::Compilation(ref msg) => {
2493                        log::error!("Shader error: {msg}");
2494                        pipeline::CreateShaderModuleError::Generation
2495                    }
2496                })
2497            }
2498        };
2499
2500        let module = pipeline::ShaderModule {
2501            raw: ManuallyDrop::new(raw),
2502            device: self.clone(),
2503            interface: ShaderMetaData::Passthrough(PassthroughInterface {
2504                entry_point_names: descriptor
2505                    .entry_points
2506                    .iter()
2507                    .map(|e| e.name.to_string())
2508                    .collect(),
2509            }),
2510            label: descriptor.label.to_string(),
2511        };
2512
2513        Ok(Arc::new(module))
2514    }
2515
2516    pub(crate) fn create_command_encoder(
2517        self: &Arc<Self>,
2518        label: &crate::Label,
2519    ) -> Result<Arc<command::CommandEncoder>, DeviceError> {
2520        self.check_is_valid()?;
2521
2522        let queue = self.get_queue().unwrap();
2523
2524        let encoder = self
2525            .command_allocator
2526            .acquire_encoder(self.raw(), queue.raw())
2527            .map_err(|e| self.handle_hal_error(e))?;
2528
2529        let cmd_enc = command::CommandEncoder::new(encoder, self, label);
2530
2531        let cmd_enc = Arc::new(cmd_enc);
2532
2533        Ok(cmd_enc)
2534    }
2535
2536    /// Generate information about late-validated buffer bindings for pipelines.
2537    //TODO: should this be combined with `get_introspection_bind_group_layouts` in some way?
2538    fn make_late_sized_buffer_groups(
2539        shader_binding_sizes: &FastHashMap<naga::ResourceBinding, wgt::BufferSize>,
2540        layout: &binding_model::PipelineLayout,
2541    ) -> ArrayVec<pipeline::LateSizedBufferGroup, { hal::MAX_BIND_GROUPS }> {
2542        // Given the shader-required binding sizes and the pipeline layout,
2543        // return the filtered list of them in the layout order,
2544        // removing those with given `min_binding_size`.
2545        layout
2546            .bind_group_layouts
2547            .iter()
2548            .enumerate()
2549            .map(|(group_index, bgl)| {
2550                let Some(bgl) = bgl else {
2551                    return pipeline::LateSizedBufferGroup::default();
2552                };
2553
2554                let shader_sizes = bgl
2555                    .entries
2556                    .values()
2557                    .filter_map(|entry| match entry.ty {
2558                        wgt::BindingType::Buffer {
2559                            min_binding_size: None,
2560                            ..
2561                        } => {
2562                            let rb = naga::ResourceBinding {
2563                                group: group_index as u32,
2564                                binding: entry.binding,
2565                            };
2566                            let shader_size =
2567                                shader_binding_sizes.get(&rb).map_or(0, |nz| nz.get());
2568                            Some(shader_size)
2569                        }
2570                        _ => None,
2571                    })
2572                    .collect();
2573                pipeline::LateSizedBufferGroup { shader_sizes }
2574            })
2575            .collect()
2576    }
2577
2578    pub fn create_bind_group_layout(
2579        self: &Arc<Self>,
2580        desc: &binding_model::BindGroupLayoutDescriptor,
2581    ) -> Result<Arc<BindGroupLayout>, CreateBindGroupLayoutError> {
2582        self.check_is_valid()?;
2583
2584        let entry_map = bgl::EntryMap::from_entries(&desc.entries)?;
2585
2586        let bgl_result = self.bgl_pool.get_or_init(entry_map, |entry_map| {
2587            let bgl =
2588                self.create_bind_group_layout_internal(&desc.label, entry_map, bgl::Origin::Pool)?;
2589            bgl.exclusive_pipeline
2590                .set(binding_model::ExclusivePipeline::None)
2591                .unwrap();
2592            Ok(bgl)
2593        });
2594
2595        match bgl_result {
2596            Ok(layout) => Ok(layout),
2597            Err(e) => Err(e),
2598        }
2599    }
2600
2601    fn create_bind_group_layout_internal(
2602        self: &Arc<Self>,
2603        label: &crate::Label,
2604        entry_map: bgl::EntryMap,
2605        origin: bgl::Origin,
2606    ) -> Result<Arc<BindGroupLayout>, CreateBindGroupLayoutError> {
2607        #[derive(PartialEq)]
2608        enum WritableStorage {
2609            Yes,
2610            No,
2611        }
2612
2613        for entry in entry_map.values() {
2614            if entry.binding >= self.limits.max_bindings_per_bind_group {
2615                return Err(CreateBindGroupLayoutError::InvalidBindingIndex {
2616                    binding: entry.binding,
2617                    maximum: self.limits.max_bindings_per_bind_group,
2618                });
2619            }
2620
2621            use wgt::BindingType as Bt;
2622
2623            let mut required_features = wgt::Features::empty();
2624            let mut required_downlevel_flags = wgt::DownlevelFlags::empty();
2625            let (array_feature, writable_storage) = match entry.ty {
2626                Bt::Buffer {
2627                    ty: wgt::BufferBindingType::Uniform,
2628                    has_dynamic_offset: false,
2629                    min_binding_size: _,
2630                } => (
2631                    Some(wgt::Features::BUFFER_BINDING_ARRAY),
2632                    WritableStorage::No,
2633                ),
2634                Bt::Buffer {
2635                    ty: wgt::BufferBindingType::Uniform,
2636                    has_dynamic_offset: true,
2637                    min_binding_size: _,
2638                } => (
2639                    Some(wgt::Features::BUFFER_BINDING_ARRAY),
2640                    WritableStorage::No,
2641                ),
2642                Bt::Buffer {
2643                    ty: wgt::BufferBindingType::Storage { read_only },
2644                    ..
2645                } => (
2646                    Some(
2647                        wgt::Features::BUFFER_BINDING_ARRAY
2648                            | wgt::Features::STORAGE_RESOURCE_BINDING_ARRAY,
2649                    ),
2650                    match read_only {
2651                        true => WritableStorage::No,
2652                        false => WritableStorage::Yes,
2653                    },
2654                ),
2655                Bt::Sampler { .. } => (
2656                    Some(wgt::Features::TEXTURE_BINDING_ARRAY),
2657                    WritableStorage::No,
2658                ),
2659                Bt::Texture {
2660                    multisampled: true,
2661                    sample_type: TextureSampleType::Float { filterable: true },
2662                    ..
2663                } => {
2664                    return Err(CreateBindGroupLayoutError::Entry {
2665                        binding: entry.binding,
2666                        error:
2667                            BindGroupLayoutEntryError::SampleTypeFloatFilterableBindingMultisampled,
2668                    });
2669                }
2670                Bt::Texture {
2671                    multisampled,
2672                    view_dimension,
2673                    ..
2674                } => {
2675                    if multisampled && view_dimension != TextureViewDimension::D2 {
2676                        return Err(CreateBindGroupLayoutError::Entry {
2677                            binding: entry.binding,
2678                            error: BindGroupLayoutEntryError::Non2DMultisampled(view_dimension),
2679                        });
2680                    }
2681
2682                    (
2683                        Some(wgt::Features::TEXTURE_BINDING_ARRAY),
2684                        WritableStorage::No,
2685                    )
2686                }
2687                Bt::StorageTexture {
2688                    access,
2689                    view_dimension,
2690                    format,
2691                } => {
2692                    use wgt::{StorageTextureAccess as Access, TextureFormatFeatureFlags as Flags};
2693
2694                    match view_dimension {
2695                        TextureViewDimension::Cube | TextureViewDimension::CubeArray => {
2696                            return Err(CreateBindGroupLayoutError::Entry {
2697                                binding: entry.binding,
2698                                error: BindGroupLayoutEntryError::StorageTextureCube,
2699                            })
2700                        }
2701                        _ => (),
2702                    }
2703                    match access {
2704                        wgt::StorageTextureAccess::Atomic
2705                            if !self.features.contains(wgt::Features::TEXTURE_ATOMIC) =>
2706                        {
2707                            return Err(CreateBindGroupLayoutError::Entry {
2708                                binding: entry.binding,
2709                                error: BindGroupLayoutEntryError::StorageTextureAtomic,
2710                            });
2711                        }
2712                        _ => (),
2713                    }
2714
2715                    let format_features =
2716                        self.describe_format_features(format).map_err(|error| {
2717                            CreateBindGroupLayoutError::Entry {
2718                                binding: entry.binding,
2719                                error: BindGroupLayoutEntryError::MissingFeatures(error),
2720                            }
2721                        })?;
2722
2723                    let required_feature_flag = match access {
2724                        Access::WriteOnly => Flags::STORAGE_WRITE_ONLY,
2725                        Access::ReadOnly => Flags::STORAGE_READ_ONLY,
2726                        Access::ReadWrite => Flags::STORAGE_READ_WRITE,
2727                        Access::Atomic => Flags::STORAGE_ATOMIC,
2728                    };
2729
2730                    if !format_features.flags.contains(required_feature_flag) {
2731                        return Err(
2732                            CreateBindGroupLayoutError::UnsupportedStorageTextureAccess {
2733                                binding: entry.binding,
2734                                access,
2735                                format,
2736                            },
2737                        );
2738                    }
2739
2740                    (
2741                        Some(
2742                            wgt::Features::TEXTURE_BINDING_ARRAY
2743                                | wgt::Features::STORAGE_RESOURCE_BINDING_ARRAY,
2744                        ),
2745                        match access {
2746                            wgt::StorageTextureAccess::WriteOnly => WritableStorage::Yes,
2747                            wgt::StorageTextureAccess::ReadOnly => WritableStorage::No,
2748                            wgt::StorageTextureAccess::ReadWrite => WritableStorage::Yes,
2749                            wgt::StorageTextureAccess::Atomic => {
2750                                required_features |= wgt::Features::TEXTURE_ATOMIC;
2751                                WritableStorage::Yes
2752                            }
2753                        },
2754                    )
2755                }
2756                Bt::AccelerationStructure { vertex_return } => {
2757                    self.require_features(wgt::Features::EXPERIMENTAL_RAY_QUERY)
2758                        .map_err(|e| CreateBindGroupLayoutError::Entry {
2759                            binding: entry.binding,
2760                            error: e.into(),
2761                        })?;
2762                    if vertex_return {
2763                        self.require_features(wgt::Features::EXPERIMENTAL_RAY_HIT_VERTEX_RETURN)
2764                            .map_err(|e| CreateBindGroupLayoutError::Entry {
2765                                binding: entry.binding,
2766                                error: e.into(),
2767                            })?;
2768                    }
2769                    (
2770                        Some(wgt::Features::ACCELERATION_STRUCTURE_BINDING_ARRAY),
2771                        WritableStorage::No,
2772                    )
2773                }
2774                Bt::ExternalTexture => {
2775                    self.require_features(wgt::Features::EXTERNAL_TEXTURE)
2776                        .map_err(|e| CreateBindGroupLayoutError::Entry {
2777                            binding: entry.binding,
2778                            error: e.into(),
2779                        })?;
2780                    (None, WritableStorage::No)
2781                }
2782            };
2783
2784            // Validate the count parameter
2785            if entry.count.is_some() {
2786                required_features |= array_feature
2787                    .ok_or(BindGroupLayoutEntryError::ArrayUnsupported)
2788                    .map_err(|error| CreateBindGroupLayoutError::Entry {
2789                        binding: entry.binding,
2790                        error,
2791                    })?;
2792            }
2793
2794            if entry.visibility.contains_unknown_bits() {
2795                return Err(CreateBindGroupLayoutError::InvalidVisibility(
2796                    entry.visibility,
2797                ));
2798            }
2799
2800            if entry.visibility.contains(wgt::ShaderStages::VERTEX) {
2801                if writable_storage == WritableStorage::Yes {
2802                    required_features |= wgt::Features::VERTEX_WRITABLE_STORAGE;
2803                }
2804                if let Bt::Buffer {
2805                    ty: wgt::BufferBindingType::Storage { .. },
2806                    ..
2807                } = entry.ty
2808                {
2809                    required_downlevel_flags |= wgt::DownlevelFlags::VERTEX_STORAGE;
2810                }
2811            }
2812            if writable_storage == WritableStorage::Yes
2813                && entry.visibility.contains(wgt::ShaderStages::FRAGMENT)
2814            {
2815                required_downlevel_flags |= wgt::DownlevelFlags::FRAGMENT_WRITABLE_STORAGE;
2816            }
2817
2818            self.require_features(required_features)
2819                .map_err(BindGroupLayoutEntryError::MissingFeatures)
2820                .map_err(|error| CreateBindGroupLayoutError::Entry {
2821                    binding: entry.binding,
2822                    error,
2823                })?;
2824            self.require_downlevel_flags(required_downlevel_flags)
2825                .map_err(BindGroupLayoutEntryError::MissingDownlevelFlags)
2826                .map_err(|error| CreateBindGroupLayoutError::Entry {
2827                    binding: entry.binding,
2828                    error,
2829                })?;
2830        }
2831
2832        let bgl_flags = conv::bind_group_layout_flags(self.features);
2833
2834        let hal_bindings = entry_map.values().copied().collect::<Vec<_>>();
2835        let hal_desc = hal::BindGroupLayoutDescriptor {
2836            label: label.to_hal(self.instance_flags),
2837            flags: bgl_flags,
2838            entries: &hal_bindings,
2839        };
2840
2841        let mut count_validator = binding_model::BindingTypeMaxCountValidator::default();
2842        for entry in entry_map.values() {
2843            count_validator.add_binding(entry);
2844        }
2845        // If a single bind group layout violates limits, the pipeline layout is
2846        // definitely going to violate limits too, lets catch it now.
2847        count_validator
2848            .validate(&self.limits)
2849            .map_err(CreateBindGroupLayoutError::TooManyBindings)?;
2850
2851        // Validate that binding arrays don't conflict with dynamic offsets.
2852        count_validator.validate_binding_arrays()?;
2853
2854        let raw = unsafe { self.raw().create_bind_group_layout(&hal_desc) }
2855            .map_err(|e| self.handle_hal_error(e))?;
2856
2857        let bgl = BindGroupLayout {
2858            raw: binding_model::RawBindGroupLayout::Owning(ManuallyDrop::new(raw)),
2859            device: self.clone(),
2860            entries: entry_map,
2861            origin,
2862            exclusive_pipeline: OnceCellOrLock::new(),
2863            binding_count_validator: count_validator,
2864            label: label.to_string(),
2865        };
2866
2867        let bgl = Arc::new(bgl);
2868
2869        Ok(bgl)
2870    }
2871
2872    fn create_buffer_binding<'a>(
2873        &self,
2874        bb: &'a binding_model::ResolvedBufferBinding,
2875        binding: u32,
2876        decl: &wgt::BindGroupLayoutEntry,
2877        buffer_init_actions: &mut Vec<BufferInitTrackerAction>,
2878        dynamic_binding_info: &mut Vec<binding_model::BindGroupDynamicBindingData>,
2879        late_buffer_binding_sizes: &mut FastHashMap<u32, wgt::BufferSize>,
2880        used: &mut BindGroupStates,
2881        snatch_guard: &'a SnatchGuard<'a>,
2882    ) -> Result<hal::BufferBinding<'a, dyn hal::DynBuffer>, CreateBindGroupError> {
2883        use crate::binding_model::CreateBindGroupError as Error;
2884
2885        let (binding_ty, dynamic, min_size) = match decl.ty {
2886            wgt::BindingType::Buffer {
2887                ty,
2888                has_dynamic_offset,
2889                min_binding_size,
2890            } => (ty, has_dynamic_offset, min_binding_size),
2891            _ => {
2892                return Err(Error::WrongBindingType {
2893                    binding,
2894                    actual: decl.ty,
2895                    expected: "UniformBuffer, StorageBuffer or ReadonlyStorageBuffer",
2896                })
2897            }
2898        };
2899
2900        let (pub_usage, internal_use, range_limit) = match binding_ty {
2901            wgt::BufferBindingType::Uniform => (
2902                wgt::BufferUsages::UNIFORM,
2903                wgt::BufferUses::UNIFORM,
2904                self.limits.max_uniform_buffer_binding_size,
2905            ),
2906            wgt::BufferBindingType::Storage { read_only } => (
2907                wgt::BufferUsages::STORAGE,
2908                if read_only {
2909                    wgt::BufferUses::STORAGE_READ_ONLY
2910                } else {
2911                    wgt::BufferUses::STORAGE_READ_WRITE
2912                },
2913                self.limits.max_storage_buffer_binding_size,
2914            ),
2915        };
2916
2917        let (align, align_limit_name) =
2918            binding_model::buffer_binding_type_alignment(&self.limits, binding_ty);
2919        if !bb.offset.is_multiple_of(align as u64) {
2920            return Err(Error::UnalignedBufferOffset(
2921                bb.offset,
2922                align_limit_name,
2923                align,
2924            ));
2925        }
2926
2927        let buffer = &bb.buffer;
2928
2929        used.buffers.insert_single(buffer.clone(), internal_use);
2930
2931        buffer.same_device(self)?;
2932
2933        buffer.check_usage(pub_usage)?;
2934
2935        let req_size = match bb.size.map(wgt::BufferSize::new) {
2936            // Requested a non-zero size
2937            Some(non_zero @ Some(_)) => non_zero,
2938            // Requested size not specified
2939            None => None,
2940            // Requested zero size
2941            Some(None) => return Err(CreateBindGroupError::BindingZeroSize(buffer.error_ident())),
2942        };
2943        let (bb, bind_size) = buffer.binding(bb.offset, req_size, snatch_guard)?;
2944
2945        if matches!(binding_ty, wgt::BufferBindingType::Storage { .. })
2946            && bind_size % u64::from(wgt::STORAGE_BINDING_SIZE_ALIGNMENT) != 0
2947        {
2948            return Err(Error::UnalignedEffectiveBufferBindingSizeForStorage {
2949                alignment: wgt::STORAGE_BINDING_SIZE_ALIGNMENT,
2950                size: bind_size,
2951            });
2952        }
2953
2954        let bind_end = bb.offset + bind_size;
2955
2956        if bind_size > range_limit {
2957            return Err(Error::BufferRangeTooLarge {
2958                binding,
2959                given: bind_size,
2960                limit: range_limit,
2961            });
2962        }
2963
2964        // Record binding info for validating dynamic offsets
2965        if dynamic {
2966            dynamic_binding_info.push(binding_model::BindGroupDynamicBindingData {
2967                binding_idx: binding,
2968                buffer_size: buffer.size,
2969                binding_range: bb.offset..bind_end,
2970                maximum_dynamic_offset: buffer.size - bind_end,
2971                binding_type: binding_ty,
2972            });
2973        }
2974
2975        if let Some(non_zero) = min_size {
2976            let min_size = non_zero.get();
2977            if min_size > bind_size {
2978                return Err(Error::BindingSizeTooSmall {
2979                    buffer: buffer.error_ident(),
2980                    actual: bind_size,
2981                    min: min_size,
2982                });
2983            }
2984        } else {
2985            let late_size = wgt::BufferSize::new(bind_size)
2986                .ok_or_else(|| Error::BindingZeroSize(buffer.error_ident()))?;
2987            late_buffer_binding_sizes.insert(binding, late_size);
2988        }
2989
2990        // This was checked against the device's alignment requirements above,
2991        // which should always be a multiple of `COPY_BUFFER_ALIGNMENT`.
2992        assert_eq!(bb.offset % wgt::COPY_BUFFER_ALIGNMENT, 0);
2993
2994        let init_range = if dynamic {
2995            // We don't know what part of the buffer will be bound, so require that it
2996            // is fully initialized.
2997            0..buffer.size
2998        } else {
2999            // `wgpu_hal` only restricts shader access to bound buffer regions with
3000            // a certain resolution. For the sake of lazy initialization, round up
3001            // the size of the bound range to reflect how much of the buffer is
3002            // actually going to be visible to the shader.
3003            let bounds_check_alignment = binding_model::buffer_binding_type_bounds_check_alignment(
3004                &self.alignments,
3005                binding_ty,
3006            );
3007            let visible_size = align_to(bind_size, bounds_check_alignment);
3008
3009            bb.offset..bb.offset + visible_size
3010        };
3011
3012        buffer_init_actions.extend(buffer.initialization_status.read().create_action(
3013            buffer,
3014            init_range,
3015            MemoryInitKind::NeedsInitializedMemory,
3016        ));
3017
3018        Ok(bb)
3019    }
3020
3021    fn create_sampler_binding<'a>(
3022        &self,
3023        used: &mut BindGroupStates,
3024        binding: u32,
3025        decl: &wgt::BindGroupLayoutEntry,
3026        sampler: &'a Arc<Sampler>,
3027    ) -> Result<&'a dyn hal::DynSampler, CreateBindGroupError> {
3028        use crate::binding_model::CreateBindGroupError as Error;
3029
3030        used.samplers.insert_single(sampler.clone());
3031
3032        sampler.same_device(self)?;
3033
3034        match decl.ty {
3035            wgt::BindingType::Sampler(ty) => {
3036                let (allowed_filtering, allowed_comparison) = match ty {
3037                    wgt::SamplerBindingType::Filtering => (None, false),
3038                    wgt::SamplerBindingType::NonFiltering => (Some(false), false),
3039                    wgt::SamplerBindingType::Comparison => (None, true),
3040                };
3041                if let Some(allowed_filtering) = allowed_filtering {
3042                    if allowed_filtering != sampler.filtering {
3043                        return Err(Error::WrongSamplerFiltering {
3044                            binding,
3045                            layout_flt: allowed_filtering,
3046                            sampler_flt: sampler.filtering,
3047                        });
3048                    }
3049                }
3050                if allowed_comparison != sampler.comparison {
3051                    return Err(Error::WrongSamplerComparison {
3052                        binding,
3053                        layout_cmp: allowed_comparison,
3054                        sampler_cmp: sampler.comparison,
3055                    });
3056                }
3057            }
3058            _ => {
3059                return Err(Error::WrongBindingType {
3060                    binding,
3061                    actual: decl.ty,
3062                    expected: "Sampler",
3063                })
3064            }
3065        }
3066
3067        Ok(sampler.raw())
3068    }
3069
3070    fn create_texture_binding<'a>(
3071        &self,
3072        binding: u32,
3073        decl: &wgt::BindGroupLayoutEntry,
3074        view: &'a Arc<TextureView>,
3075        used: &mut BindGroupStates,
3076        texture_init_actions: &mut Vec<TextureInitTrackerAction>,
3077        snatch_guard: &'a SnatchGuard<'a>,
3078    ) -> Result<hal::TextureBinding<'a, dyn hal::DynTextureView>, CreateBindGroupError> {
3079        view.same_device(self)?;
3080
3081        let internal_use = self.texture_use_parameters(
3082            binding,
3083            decl,
3084            view,
3085            "SampledTexture, ReadonlyStorageTexture or WriteonlyStorageTexture",
3086        )?;
3087
3088        used.views.insert_single(view.clone(), internal_use);
3089
3090        let texture = &view.parent;
3091
3092        texture_init_actions.push(TextureInitTrackerAction {
3093            texture: texture.clone(),
3094            range: TextureInitRange {
3095                mip_range: view.desc.range.mip_range(texture.desc.mip_level_count),
3096                layer_range: view
3097                    .desc
3098                    .range
3099                    .layer_range(texture.desc.array_layer_count()),
3100            },
3101            kind: MemoryInitKind::NeedsInitializedMemory,
3102        });
3103
3104        Ok(hal::TextureBinding {
3105            view: view.try_raw(snatch_guard)?,
3106            usage: internal_use,
3107        })
3108    }
3109
3110    fn create_tlas_binding<'a>(
3111        self: &Arc<Self>,
3112        used: &mut BindGroupStates,
3113        binding: u32,
3114        decl: &wgt::BindGroupLayoutEntry,
3115        tlas: &'a Arc<Tlas>,
3116        snatch_guard: &'a SnatchGuard<'a>,
3117    ) -> Result<&'a dyn hal::DynAccelerationStructure, CreateBindGroupError> {
3118        use crate::binding_model::CreateBindGroupError as Error;
3119
3120        used.acceleration_structures.insert_single(tlas.clone());
3121
3122        tlas.same_device(self)?;
3123
3124        match decl.ty {
3125            wgt::BindingType::AccelerationStructure { vertex_return } => {
3126                if vertex_return
3127                    && !tlas.flags.contains(
3128                        wgpu_types::AccelerationStructureFlags::ALLOW_RAY_HIT_VERTEX_RETURN,
3129                    )
3130                {
3131                    return Err(Error::MissingTLASVertexReturn { binding });
3132                }
3133            }
3134            _ => {
3135                return Err(Error::WrongBindingType {
3136                    binding,
3137                    actual: decl.ty,
3138                    expected: "Tlas",
3139                });
3140            }
3141        }
3142
3143        Ok(tlas.try_raw(snatch_guard)?)
3144    }
3145
3146    fn create_external_texture_binding<'a>(
3147        &'a self,
3148        binding: u32,
3149        decl: &wgt::BindGroupLayoutEntry,
3150        external_texture: &'a Arc<ExternalTexture>,
3151        used: &mut BindGroupStates,
3152        snatch_guard: &'a SnatchGuard,
3153    ) -> Result<
3154        hal::ExternalTextureBinding<'a, dyn hal::DynBuffer, dyn hal::DynTextureView>,
3155        CreateBindGroupError,
3156    > {
3157        use crate::binding_model::CreateBindGroupError as Error;
3158
3159        external_texture.same_device(self)?;
3160
3161        used.external_textures
3162            .insert_single(external_texture.clone());
3163
3164        match decl.ty {
3165            wgt::BindingType::ExternalTexture => {}
3166            _ => {
3167                return Err(Error::WrongBindingType {
3168                    binding,
3169                    actual: decl.ty,
3170                    expected: "ExternalTexture",
3171                });
3172            }
3173        }
3174
3175        let planes = (0..3)
3176            .map(|i| {
3177                // We always need 3 bindings. If we have fewer than 3 planes
3178                // just bind plane 0 multiple times. The shader will only
3179                // sample from valid planes anyway.
3180                let plane = external_texture
3181                    .planes
3182                    .get(i)
3183                    .unwrap_or(&external_texture.planes[0]);
3184                let internal_use = wgt::TextureUses::RESOURCE;
3185                used.views.insert_single(plane.clone(), internal_use);
3186                let view = plane.try_raw(snatch_guard)?;
3187                Ok(hal::TextureBinding {
3188                    view,
3189                    usage: internal_use,
3190                })
3191            })
3192            // We can remove this intermediate Vec by using
3193            // array::try_from_fn() above, once it stabilizes.
3194            .collect::<Result<Vec<_>, Error>>()?;
3195        let planes = planes.try_into().unwrap();
3196
3197        used.buffers
3198            .insert_single(external_texture.params.clone(), wgt::BufferUses::UNIFORM);
3199        let params = external_texture.params.binding(0, None, snatch_guard)?.0;
3200
3201        Ok(hal::ExternalTextureBinding { planes, params })
3202    }
3203
3204    fn create_external_texture_binding_from_view<'a>(
3205        &'a self,
3206        binding: u32,
3207        decl: &wgt::BindGroupLayoutEntry,
3208        view: &'a Arc<TextureView>,
3209        used: &mut BindGroupStates,
3210        snatch_guard: &'a SnatchGuard,
3211    ) -> Result<
3212        hal::ExternalTextureBinding<'a, dyn hal::DynBuffer, dyn hal::DynTextureView>,
3213        CreateBindGroupError,
3214    > {
3215        use crate::binding_model::CreateBindGroupError as Error;
3216
3217        view.same_device(self)?;
3218
3219        let internal_use = self.texture_use_parameters(binding, decl, view, "SampledTexture")?;
3220        used.views.insert_single(view.clone(), internal_use);
3221
3222        match decl.ty {
3223            wgt::BindingType::ExternalTexture => {}
3224            _ => {
3225                return Err(Error::WrongBindingType {
3226                    binding,
3227                    actual: decl.ty,
3228                    expected: "ExternalTexture",
3229                });
3230            }
3231        }
3232
3233        // We need 3 bindings, so just repeat the same texture view 3 times.
3234        let planes = [
3235            hal::TextureBinding {
3236                view: view.try_raw(snatch_guard)?,
3237                usage: internal_use,
3238            },
3239            hal::TextureBinding {
3240                view: view.try_raw(snatch_guard)?,
3241                usage: internal_use,
3242            },
3243            hal::TextureBinding {
3244                view: view.try_raw(snatch_guard)?,
3245                usage: internal_use,
3246            },
3247        ];
3248        let params = hal::BufferBinding::new_unchecked(
3249            self.default_external_texture_params_buffer.as_ref(),
3250            0,
3251            None,
3252        );
3253
3254        Ok(hal::ExternalTextureBinding { planes, params })
3255    }
3256
3257    // This function expects the provided bind group layout to be resolved
3258    // (not passing a duplicate) beforehand.
3259    pub fn create_bind_group(
3260        self: &Arc<Self>,
3261        desc: binding_model::ResolvedBindGroupDescriptor,
3262    ) -> Result<Arc<BindGroup>, CreateBindGroupError> {
3263        use crate::binding_model::{CreateBindGroupError as Error, ResolvedBindingResource as Br};
3264
3265        let layout = desc.layout;
3266
3267        self.check_is_valid()?;
3268        layout.same_device(self)?;
3269
3270        {
3271            // Check that the number of entries in the descriptor matches
3272            // the number of entries in the layout.
3273            let actual = desc.entries.len();
3274            let expected = layout.entries.len();
3275            if actual != expected {
3276                return Err(Error::BindingsNumMismatch { expected, actual });
3277            }
3278        }
3279
3280        // TODO: arrayvec/smallvec, or re-use allocations
3281        // Record binding info for dynamic offset validation
3282        let mut dynamic_binding_info = Vec::new();
3283        // Map of binding -> shader reflected size
3284        //Note: we can't collect into a vector right away because
3285        // it needs to be in BGL iteration order, not BG entry order.
3286        let mut late_buffer_binding_sizes = FastHashMap::default();
3287        // fill out the descriptors
3288        let mut used = BindGroupStates::new();
3289
3290        let mut buffer_init_actions = Vec::new();
3291        let mut texture_init_actions = Vec::new();
3292        let mut hal_entries = Vec::with_capacity(desc.entries.len());
3293        let mut hal_buffers = Vec::new();
3294        let mut hal_samplers = Vec::new();
3295        let mut hal_textures = Vec::new();
3296        let mut hal_tlas_s = Vec::new();
3297        let mut hal_external_textures = Vec::new();
3298        let snatch_guard = self.snatchable_lock.read();
3299        for entry in desc.entries.iter() {
3300            let binding = entry.binding;
3301            // Find the corresponding declaration in the layout
3302            let decl = layout
3303                .entries
3304                .get(binding)
3305                .ok_or(Error::MissingBindingDeclaration(binding))?;
3306            let (res_index, count) = match entry.resource {
3307                Br::Buffer(ref bb) => {
3308                    let bb = self.create_buffer_binding(
3309                        bb,
3310                        binding,
3311                        decl,
3312                        &mut buffer_init_actions,
3313                        &mut dynamic_binding_info,
3314                        &mut late_buffer_binding_sizes,
3315                        &mut used,
3316                        &snatch_guard,
3317                    )?;
3318
3319                    let res_index = hal_buffers.len();
3320                    hal_buffers.push(bb);
3321                    (res_index, 1)
3322                }
3323                Br::BufferArray(ref bindings_array) => {
3324                    let num_bindings = bindings_array.len();
3325                    Self::check_array_binding(self.features, decl.count, num_bindings)?;
3326
3327                    let res_index = hal_buffers.len();
3328                    for bb in bindings_array.iter() {
3329                        let bb = self.create_buffer_binding(
3330                            bb,
3331                            binding,
3332                            decl,
3333                            &mut buffer_init_actions,
3334                            &mut dynamic_binding_info,
3335                            &mut late_buffer_binding_sizes,
3336                            &mut used,
3337                            &snatch_guard,
3338                        )?;
3339                        hal_buffers.push(bb);
3340                    }
3341                    (res_index, num_bindings)
3342                }
3343                Br::Sampler(ref sampler) => {
3344                    let sampler = self.create_sampler_binding(&mut used, binding, decl, sampler)?;
3345
3346                    let res_index = hal_samplers.len();
3347                    hal_samplers.push(sampler);
3348                    (res_index, 1)
3349                }
3350                Br::SamplerArray(ref samplers) => {
3351                    let num_bindings = samplers.len();
3352                    Self::check_array_binding(self.features, decl.count, num_bindings)?;
3353
3354                    let res_index = hal_samplers.len();
3355                    for sampler in samplers.iter() {
3356                        let sampler =
3357                            self.create_sampler_binding(&mut used, binding, decl, sampler)?;
3358
3359                        hal_samplers.push(sampler);
3360                    }
3361
3362                    (res_index, num_bindings)
3363                }
3364                Br::TextureView(ref view) => match decl.ty {
3365                    wgt::BindingType::ExternalTexture => {
3366                        let et = self.create_external_texture_binding_from_view(
3367                            binding,
3368                            decl,
3369                            view,
3370                            &mut used,
3371                            &snatch_guard,
3372                        )?;
3373                        let res_index = hal_external_textures.len();
3374                        hal_external_textures.push(et);
3375                        (res_index, 1)
3376                    }
3377                    _ => {
3378                        let tb = self.create_texture_binding(
3379                            binding,
3380                            decl,
3381                            view,
3382                            &mut used,
3383                            &mut texture_init_actions,
3384                            &snatch_guard,
3385                        )?;
3386                        let res_index = hal_textures.len();
3387                        hal_textures.push(tb);
3388                        (res_index, 1)
3389                    }
3390                },
3391                Br::TextureViewArray(ref views) => {
3392                    let num_bindings = views.len();
3393                    Self::check_array_binding(self.features, decl.count, num_bindings)?;
3394
3395                    let res_index = hal_textures.len();
3396                    for view in views.iter() {
3397                        let tb = self.create_texture_binding(
3398                            binding,
3399                            decl,
3400                            view,
3401                            &mut used,
3402                            &mut texture_init_actions,
3403                            &snatch_guard,
3404                        )?;
3405
3406                        hal_textures.push(tb);
3407                    }
3408
3409                    (res_index, num_bindings)
3410                }
3411                Br::AccelerationStructure(ref tlas) => {
3412                    let tlas =
3413                        self.create_tlas_binding(&mut used, binding, decl, tlas, &snatch_guard)?;
3414                    let res_index = hal_tlas_s.len();
3415                    hal_tlas_s.push(tlas);
3416                    (res_index, 1)
3417                }
3418                Br::AccelerationStructureArray(ref tlas_array) => {
3419                    // Feature validation for TLAS binding arrays happens at bind group layout
3420                    // creation time (mirroring other binding-array resource types). By the time we
3421                    // get here, `decl.count` has already been validated against device features.
3422                    let num_bindings = tlas_array.len();
3423                    Self::check_array_binding(self.features, decl.count, num_bindings)?;
3424
3425                    let res_index = hal_tlas_s.len();
3426                    for tlas in tlas_array.iter() {
3427                        let tlas = self.create_tlas_binding(
3428                            &mut used,
3429                            binding,
3430                            decl,
3431                            tlas,
3432                            &snatch_guard,
3433                        )?;
3434                        hal_tlas_s.push(tlas);
3435                    }
3436                    (res_index, num_bindings)
3437                }
3438                Br::ExternalTexture(ref et) => {
3439                    let et = self.create_external_texture_binding(
3440                        binding,
3441                        decl,
3442                        et,
3443                        &mut used,
3444                        &snatch_guard,
3445                    )?;
3446                    let res_index = hal_external_textures.len();
3447                    hal_external_textures.push(et);
3448                    (res_index, 1)
3449                }
3450            };
3451
3452            hal_entries.push(hal::BindGroupEntry {
3453                binding,
3454                resource_index: res_index as u32,
3455                count: count as u32,
3456            });
3457        }
3458
3459        used.optimize();
3460
3461        hal_entries.sort_by_key(|entry| entry.binding);
3462        for (a, b) in hal_entries.iter().zip(hal_entries.iter().skip(1)) {
3463            if a.binding == b.binding {
3464                return Err(Error::DuplicateBinding(a.binding));
3465            }
3466        }
3467
3468        dynamic_binding_info.sort_by_key(|i| i.binding_idx);
3469
3470        let hal_desc = hal::BindGroupDescriptor {
3471            label: desc.label.to_hal(self.instance_flags),
3472            layout: layout.raw(),
3473            entries: &hal_entries,
3474            buffers: &hal_buffers,
3475            samplers: &hal_samplers,
3476            textures: &hal_textures,
3477            acceleration_structures: &hal_tlas_s,
3478            external_textures: &hal_external_textures,
3479        };
3480        let raw = unsafe { self.raw().create_bind_group(&hal_desc) }
3481            .map_err(|e| self.handle_hal_error(e))?;
3482
3483        // collect in the order of BGL iteration
3484        let late_buffer_binding_infos = layout
3485            .entries
3486            .indices()
3487            .flat_map(|binding| {
3488                let size = late_buffer_binding_sizes.get(&binding).cloned()?;
3489                Some(BindGroupLateBufferBindingInfo {
3490                    binding_index: binding,
3491                    size,
3492                })
3493            })
3494            .collect();
3495
3496        let bind_group = BindGroup {
3497            raw: Snatchable::new(raw),
3498            device: self.clone(),
3499            layout,
3500            label: desc.label.to_string(),
3501            tracking_data: TrackingData::new(self.tracker_indices.bind_groups.clone()),
3502            used,
3503            buffer_init_actions,
3504            texture_init_actions,
3505            dynamic_binding_info,
3506            late_buffer_binding_infos,
3507        };
3508
3509        let bind_group = Arc::new(bind_group);
3510
3511        let weak_ref = Arc::downgrade(&bind_group);
3512        for texture in bind_group.used.views.used_textures() {
3513            let mut bind_groups = texture.bind_groups.lock();
3514            bind_groups.push(weak_ref.clone());
3515        }
3516        for buffer in bind_group.used.buffers.used_resources() {
3517            let mut bind_groups = buffer.bind_groups.lock();
3518            bind_groups.push(weak_ref.clone());
3519        }
3520
3521        Ok(bind_group)
3522    }
3523
3524    fn check_array_binding(
3525        features: wgt::Features,
3526        count: Option<NonZeroU32>,
3527        num_bindings: usize,
3528    ) -> Result<(), CreateBindGroupError> {
3529        use super::binding_model::CreateBindGroupError as Error;
3530
3531        if let Some(count) = count {
3532            let count = count.get() as usize;
3533            if count < num_bindings {
3534                return Err(Error::BindingArrayPartialLengthMismatch {
3535                    actual: num_bindings,
3536                    expected: count,
3537                });
3538            }
3539            if count != num_bindings
3540                && !features.contains(wgt::Features::PARTIALLY_BOUND_BINDING_ARRAY)
3541            {
3542                return Err(Error::BindingArrayLengthMismatch {
3543                    actual: num_bindings,
3544                    expected: count,
3545                });
3546            }
3547            if num_bindings == 0 {
3548                return Err(Error::BindingArrayZeroLength);
3549            }
3550        } else {
3551            return Err(Error::SingleBindingExpected);
3552        };
3553
3554        Ok(())
3555    }
3556
3557    fn texture_use_parameters(
3558        &self,
3559        binding: u32,
3560        decl: &wgt::BindGroupLayoutEntry,
3561        view: &TextureView,
3562        expected: &'static str,
3563    ) -> Result<wgt::TextureUses, CreateBindGroupError> {
3564        use crate::binding_model::CreateBindGroupError as Error;
3565        if view
3566            .desc
3567            .aspects()
3568            .contains(hal::FormatAspects::DEPTH | hal::FormatAspects::STENCIL)
3569        {
3570            return Err(Error::DepthStencilAspect);
3571        }
3572        match decl.ty {
3573            wgt::BindingType::Texture {
3574                sample_type,
3575                view_dimension,
3576                multisampled,
3577            } => {
3578                use wgt::TextureSampleType as Tst;
3579                if multisampled != (view.samples != 1) {
3580                    return Err(Error::InvalidTextureMultisample {
3581                        binding,
3582                        layout_multisampled: multisampled,
3583                        view_samples: view.samples,
3584                    });
3585                }
3586                let compat_sample_type = view
3587                    .desc
3588                    .format
3589                    .sample_type(Some(view.desc.range.aspect), Some(self.features))
3590                    .unwrap();
3591                match (sample_type, compat_sample_type) {
3592                    (Tst::Uint, Tst::Uint) |
3593                        (Tst::Sint, Tst::Sint) |
3594                        (Tst::Depth, Tst::Depth) |
3595                        // if we expect non-filterable, accept anything float
3596                        (Tst::Float { filterable: false }, Tst::Float { .. }) |
3597                        // if we expect filterable, require it
3598                        (Tst::Float { filterable: true }, Tst::Float { filterable: true }) |
3599                        // if we expect non-filterable, also accept depth
3600                        (Tst::Float { filterable: false }, Tst::Depth) => {}
3601                    // if we expect filterable, also accept Float that is defined as
3602                    // unfilterable if filterable feature is explicitly enabled (only hit
3603                    // if wgt::Features::TEXTURE_ADAPTER_SPECIFIC_FORMAT_FEATURES is
3604                    // enabled)
3605                    (Tst::Float { filterable: true }, Tst::Float { .. })
3606                        if view.format_features.flags
3607                            .contains(wgt::TextureFormatFeatureFlags::FILTERABLE) => {}
3608                    _ => {
3609                        return Err(Error::InvalidTextureSampleType {
3610                            binding,
3611                            layout_sample_type: sample_type,
3612                            view_format: view.desc.format,
3613                            view_sample_type: compat_sample_type,
3614                        })
3615                    }
3616                }
3617                if view_dimension != view.desc.dimension {
3618                    return Err(Error::InvalidTextureDimension {
3619                        binding,
3620                        layout_dimension: view_dimension,
3621                        view_dimension: view.desc.dimension,
3622                    });
3623                }
3624                view.check_usage(wgt::TextureUsages::TEXTURE_BINDING)?;
3625                Ok(wgt::TextureUses::RESOURCE)
3626            }
3627            wgt::BindingType::StorageTexture {
3628                access,
3629                format,
3630                view_dimension,
3631            } => {
3632                if format != view.desc.format {
3633                    return Err(Error::InvalidStorageTextureFormat {
3634                        binding,
3635                        layout_format: format,
3636                        view_format: view.desc.format,
3637                    });
3638                }
3639                if view_dimension != view.desc.dimension {
3640                    return Err(Error::InvalidTextureDimension {
3641                        binding,
3642                        layout_dimension: view_dimension,
3643                        view_dimension: view.desc.dimension,
3644                    });
3645                }
3646
3647                let mip_level_count = view.selector.mips.end - view.selector.mips.start;
3648                if mip_level_count != 1 {
3649                    return Err(Error::InvalidStorageTextureMipLevelCount {
3650                        binding,
3651                        mip_level_count,
3652                    });
3653                }
3654
3655                view.check_usage(wgt::TextureUsages::STORAGE_BINDING)?;
3656
3657                Ok(match access {
3658                    wgt::StorageTextureAccess::ReadOnly => wgt::TextureUses::STORAGE_READ_ONLY,
3659                    wgt::StorageTextureAccess::WriteOnly => wgt::TextureUses::STORAGE_WRITE_ONLY,
3660                    wgt::StorageTextureAccess::ReadWrite => wgt::TextureUses::STORAGE_READ_WRITE,
3661                    wgt::StorageTextureAccess::Atomic => wgt::TextureUses::STORAGE_ATOMIC,
3662                })
3663            }
3664            wgt::BindingType::ExternalTexture => {
3665                if view.desc.dimension != TextureViewDimension::D2 {
3666                    return Err(Error::InvalidTextureDimension {
3667                        binding,
3668                        layout_dimension: TextureViewDimension::D2,
3669                        view_dimension: view.desc.dimension,
3670                    });
3671                }
3672                let mip_level_count = view.selector.mips.end - view.selector.mips.start;
3673                if mip_level_count != 1 {
3674                    return Err(Error::InvalidExternalTextureMipLevelCount {
3675                        binding,
3676                        mip_level_count,
3677                    });
3678                }
3679                if view.desc.format != TextureFormat::Rgba8Unorm
3680                    && view.desc.format != TextureFormat::Bgra8Unorm
3681                    && view.desc.format != TextureFormat::Rgba16Float
3682                {
3683                    return Err(Error::InvalidExternalTextureFormat {
3684                        binding,
3685                        format: view.desc.format,
3686                    });
3687                }
3688                if view.samples != 1 {
3689                    return Err(Error::InvalidTextureMultisample {
3690                        binding,
3691                        layout_multisampled: false,
3692                        view_samples: view.samples,
3693                    });
3694                }
3695
3696                view.check_usage(wgt::TextureUsages::TEXTURE_BINDING)?;
3697                Ok(wgt::TextureUses::RESOURCE)
3698            }
3699            _ => Err(Error::WrongBindingType {
3700                binding,
3701                actual: decl.ty,
3702                expected,
3703            }),
3704        }
3705    }
3706
3707    pub fn create_pipeline_layout(
3708        self: &Arc<Self>,
3709        desc: &binding_model::ResolvedPipelineLayoutDescriptor,
3710    ) -> Result<Arc<binding_model::PipelineLayout>, binding_model::CreatePipelineLayoutError> {
3711        self.create_pipeline_layout_impl(desc, false)
3712    }
3713
3714    fn create_pipeline_layout_impl(
3715        self: &Arc<Self>,
3716        desc: &binding_model::ResolvedPipelineLayoutDescriptor,
3717        ignore_exclusive_pipeline_check: bool,
3718    ) -> Result<Arc<binding_model::PipelineLayout>, binding_model::CreatePipelineLayoutError> {
3719        use crate::binding_model::CreatePipelineLayoutError as Error;
3720
3721        self.check_is_valid()?;
3722
3723        let bind_group_layouts_count = desc.bind_group_layouts.len();
3724        let device_max_bind_groups = self.limits.max_bind_groups as usize;
3725        if bind_group_layouts_count > device_max_bind_groups {
3726            return Err(Error::TooManyGroups {
3727                actual: bind_group_layouts_count,
3728                max: device_max_bind_groups,
3729            });
3730        }
3731
3732        if desc.immediate_size != 0 {
3733            self.require_features(wgt::Features::IMMEDIATES)?;
3734        }
3735        if self.limits.max_immediate_size < desc.immediate_size {
3736            return Err(Error::ImmediateRangeTooLarge {
3737                size: desc.immediate_size,
3738                max: self.limits.max_immediate_size,
3739            });
3740        }
3741        if !desc
3742            .immediate_size
3743            .is_multiple_of(wgt::IMMEDIATE_DATA_ALIGNMENT)
3744        {
3745            return Err(Error::MisalignedImmediateSize {
3746                size: desc.immediate_size,
3747            });
3748        }
3749
3750        let mut count_validator = binding_model::BindingTypeMaxCountValidator::default();
3751
3752        for (index, bgl) in desc.bind_group_layouts.iter().enumerate() {
3753            let Some(bgl) = bgl else {
3754                continue;
3755            };
3756
3757            bgl.same_device(self)?;
3758
3759            if !ignore_exclusive_pipeline_check {
3760                let exclusive_pipeline = bgl.exclusive_pipeline.get().unwrap();
3761                if !matches!(exclusive_pipeline, binding_model::ExclusivePipeline::None) {
3762                    return Err(Error::BglHasExclusivePipeline {
3763                        index,
3764                        pipeline: alloc::format!("{exclusive_pipeline}"),
3765                    });
3766                }
3767            }
3768
3769            count_validator.merge(&bgl.binding_count_validator);
3770        }
3771
3772        count_validator
3773            .validate(&self.limits)
3774            .map_err(Error::TooManyBindings)?;
3775
3776        let get_bgl_iter = || {
3777            desc.bind_group_layouts
3778                .iter()
3779                .map(|bgl| bgl.as_ref().filter(|bgl| !bgl.entries.is_empty()))
3780        };
3781
3782        let bind_group_layouts = get_bgl_iter()
3783            .map(|bgl| bgl.cloned())
3784            .collect::<ArrayVec<_, { hal::MAX_BIND_GROUPS }>>();
3785
3786        let raw_bind_group_layouts = get_bgl_iter()
3787            .map(|bgl| bgl.map(|bgl| bgl.raw()))
3788            .collect::<ArrayVec<_, { hal::MAX_BIND_GROUPS }>>();
3789
3790        let additional_flags = if self.indirect_validation.is_some() {
3791            hal::PipelineLayoutFlags::INDIRECT_BUILTIN_UPDATE
3792        } else {
3793            hal::PipelineLayoutFlags::empty()
3794        };
3795
3796        let hal_desc = hal::PipelineLayoutDescriptor {
3797            label: desc.label.to_hal(self.instance_flags),
3798            flags: hal::PipelineLayoutFlags::FIRST_VERTEX_INSTANCE
3799                | hal::PipelineLayoutFlags::NUM_WORK_GROUPS
3800                | additional_flags,
3801            bind_group_layouts: &raw_bind_group_layouts,
3802            immediate_size: desc.immediate_size,
3803        };
3804
3805        let raw = unsafe { self.raw().create_pipeline_layout(&hal_desc) }
3806            .map_err(|e| self.handle_hal_error(e))?;
3807
3808        drop(raw_bind_group_layouts);
3809
3810        let layout = binding_model::PipelineLayout {
3811            raw: ManuallyDrop::new(raw),
3812            device: self.clone(),
3813            label: desc.label.to_string(),
3814            bind_group_layouts,
3815            immediate_size: desc.immediate_size,
3816        };
3817
3818        let layout = Arc::new(layout);
3819
3820        Ok(layout)
3821    }
3822
3823    fn create_derived_pipeline_layout(
3824        self: &Arc<Self>,
3825        mut derived_group_layouts: Box<ArrayVec<bgl::EntryMap, { hal::MAX_BIND_GROUPS }>>,
3826        immediate_size: u32,
3827    ) -> Result<Arc<binding_model::PipelineLayout>, pipeline::ImplicitLayoutError> {
3828        while derived_group_layouts
3829            .last()
3830            .is_some_and(|map| map.is_empty())
3831        {
3832            derived_group_layouts.pop();
3833        }
3834
3835        let mut unique_bind_group_layouts = FastHashMap::default();
3836
3837        let bind_group_layouts = derived_group_layouts
3838            .into_iter()
3839            .map(|mut bgl_entry_map| {
3840                if bgl_entry_map.is_empty() {
3841                    return Ok(None);
3842                }
3843
3844                bgl_entry_map.sort();
3845                match unique_bind_group_layouts.entry(bgl_entry_map) {
3846                    hashbrown::hash_map::Entry::Occupied(v) => Ok(Some(Arc::clone(v.get()))),
3847                    hashbrown::hash_map::Entry::Vacant(e) => {
3848                        match self.create_bind_group_layout_internal(
3849                            &None,
3850                            e.key().clone(),
3851                            bgl::Origin::Derived,
3852                        ) {
3853                            Ok(bgl) => {
3854                                e.insert(bgl.clone());
3855                                Ok(Some(bgl))
3856                            }
3857                            Err(e) => Err(e),
3858                        }
3859                    }
3860                }
3861            })
3862            .collect::<Result<Vec<_>, _>>()?;
3863
3864        let layout_desc = binding_model::ResolvedPipelineLayoutDescriptor {
3865            label: None,
3866            bind_group_layouts: Cow::Owned(bind_group_layouts),
3867            immediate_size,
3868        };
3869
3870        let layout = self.create_pipeline_layout_impl(&layout_desc, true)?;
3871        Ok(layout)
3872    }
3873
3874    pub fn create_compute_pipeline(
3875        self: &Arc<Self>,
3876        desc: pipeline::ResolvedComputePipelineDescriptor,
3877    ) -> Result<Arc<pipeline::ComputePipeline>, pipeline::CreateComputePipelineError> {
3878        self.check_is_valid()?;
3879
3880        self.require_downlevel_flags(wgt::DownlevelFlags::COMPUTE_SHADERS)?;
3881
3882        let shader_module = desc.stage.module;
3883
3884        shader_module.same_device(self)?;
3885
3886        let is_auto_layout = desc.layout.is_none();
3887
3888        // Get the pipeline layout from the desc if it is provided.
3889        let pipeline_layout = match desc.layout {
3890            Some(pipeline_layout) => {
3891                pipeline_layout.same_device(self)?;
3892                Some(pipeline_layout)
3893            }
3894            None => None,
3895        };
3896
3897        let mut binding_layout_source = match pipeline_layout {
3898            Some(pipeline_layout) => validation::BindingLayoutSource::Provided(pipeline_layout),
3899            None => validation::BindingLayoutSource::new_derived(&self.limits),
3900        };
3901        let mut shader_binding_sizes = FastHashMap::default();
3902        let io = validation::StageIo::default();
3903
3904        let final_entry_point_name;
3905
3906        {
3907            let stage = validation::ShaderStageForValidation::Compute;
3908
3909            final_entry_point_name = shader_module.finalize_entry_point_name(
3910                stage.to_naga(),
3911                desc.stage.entry_point.as_ref().map(|ep| ep.as_ref()),
3912            )?;
3913
3914            if let Some(interface) = shader_module.interface.interface() {
3915                let _ = interface.check_stage(
3916                    &mut binding_layout_source,
3917                    &mut shader_binding_sizes,
3918                    &final_entry_point_name,
3919                    stage,
3920                    io,
3921                    None,
3922                )?;
3923            }
3924        }
3925
3926        let pipeline_layout = match binding_layout_source {
3927            validation::BindingLayoutSource::Provided(pipeline_layout) => pipeline_layout,
3928            validation::BindingLayoutSource::Derived(entries) => {
3929                let immediate_size = shader_module
3930                    .interface
3931                    .interface()
3932                    .map_or(0, |i| i.immediate_size);
3933                self.create_derived_pipeline_layout(entries, immediate_size)?
3934            }
3935        };
3936
3937        let late_sized_buffer_groups =
3938            Device::make_late_sized_buffer_groups(&shader_binding_sizes, &pipeline_layout);
3939
3940        let cache = match desc.cache {
3941            Some(cache) => {
3942                cache.same_device(self)?;
3943                Some(cache)
3944            }
3945            None => None,
3946        };
3947
3948        let pipeline_desc = hal::ComputePipelineDescriptor {
3949            label: desc.label.to_hal(self.instance_flags),
3950            layout: pipeline_layout.raw(),
3951            stage: hal::ProgrammableStage {
3952                module: shader_module.raw(),
3953                entry_point: final_entry_point_name.as_ref(),
3954                constants: &desc.stage.constants,
3955                zero_initialize_workgroup_memory: desc.stage.zero_initialize_workgroup_memory,
3956            },
3957            cache: cache.as_ref().map(|it| it.raw()),
3958        };
3959
3960        let raw =
3961            unsafe { self.raw().create_compute_pipeline(&pipeline_desc) }.map_err(
3962                |err| match err {
3963                    hal::PipelineError::Device(error) => {
3964                        pipeline::CreateComputePipelineError::Device(self.handle_hal_error(error))
3965                    }
3966                    hal::PipelineError::Linkage(_stages, msg) => {
3967                        pipeline::CreateComputePipelineError::Internal(msg)
3968                    }
3969                    hal::PipelineError::EntryPoint(_stage) => {
3970                        pipeline::CreateComputePipelineError::Internal(
3971                            ENTRYPOINT_FAILURE_ERROR.to_string(),
3972                        )
3973                    }
3974                    hal::PipelineError::PipelineConstants(_stages, msg) => {
3975                        pipeline::CreateComputePipelineError::PipelineConstants(msg)
3976                    }
3977                },
3978            )?;
3979
3980        let immediate_slots_required =
3981            shader_module
3982                .interface
3983                .interface()
3984                .map_or(Default::default(), |iface| {
3985                    iface.immediate_slots_required(
3986                        naga::ShaderStage::Compute,
3987                        &final_entry_point_name,
3988                    )
3989                });
3990
3991        let pipeline = pipeline::ComputePipeline {
3992            raw: ManuallyDrop::new(raw),
3993            layout: pipeline_layout,
3994            device: self.clone(),
3995            _shader_module: shader_module,
3996            late_sized_buffer_groups,
3997            immediate_slots_required,
3998            label: desc.label.to_string(),
3999            tracking_data: TrackingData::new(self.tracker_indices.compute_pipelines.clone()),
4000        };
4001
4002        let pipeline = Arc::new(pipeline);
4003
4004        if is_auto_layout {
4005            for bgl in pipeline.layout.bind_group_layouts.iter() {
4006                let Some(bgl) = bgl else {
4007                    continue;
4008                };
4009
4010                // `bind_group_layouts` might contain duplicate entries, so we need to ignore the
4011                // result.
4012                let _ = bgl.exclusive_pipeline.set((&pipeline).into());
4013            }
4014        }
4015
4016        Ok(pipeline)
4017    }
4018
4019    pub fn create_render_pipeline(
4020        self: &Arc<Self>,
4021        desc: pipeline::ResolvedGeneralRenderPipelineDescriptor,
4022    ) -> Result<Arc<pipeline::RenderPipeline>, pipeline::CreateRenderPipelineError> {
4023        use wgt::TextureFormatFeatureFlags as Tfff;
4024
4025        self.check_is_valid()?;
4026
4027        let mut shader_binding_sizes = FastHashMap::default();
4028
4029        let color_targets = desc
4030            .fragment
4031            .as_ref()
4032            .map_or(&[][..], |fragment| &fragment.targets);
4033        let depth_stencil_state = desc.depth_stencil.as_ref();
4034
4035        check_color_attachment_count(color_targets.len(), self.limits.max_color_attachments)?;
4036
4037        {
4038            let cts: ArrayVec<_, { hal::MAX_COLOR_ATTACHMENTS }> =
4039                color_targets.iter().filter_map(|x| x.as_ref()).collect();
4040            if !cts.is_empty() && {
4041                let first = &cts[0];
4042                cts[1..]
4043                    .iter()
4044                    .any(|ct| ct.write_mask != first.write_mask || ct.blend != first.blend)
4045            } {
4046                self.require_downlevel_flags(wgt::DownlevelFlags::INDEPENDENT_BLEND)?;
4047            }
4048        }
4049
4050        let mut io = validation::StageIo::default();
4051        let mut validated_stages = wgt::ShaderStages::empty();
4052
4053        let mut vertex_steps;
4054        let mut hal_vertex_buffer_layouts;
4055        let mut total_attributes;
4056        let mut dual_source_blending = false;
4057        let mut has_depth_attachment = false;
4058        if let pipeline::RenderPipelineVertexProcessor::Vertex(ref vertex) = desc.vertex {
4059            if vertex.buffers.len() > self.limits.max_vertex_buffers as usize {
4060                return Err(pipeline::CreateRenderPipelineError::TooManyVertexBuffers {
4061                    given: vertex.buffers.len() as u32,
4062                    limit: self.limits.max_vertex_buffers,
4063                });
4064            }
4065
4066            vertex_steps = Vec::with_capacity(vertex.buffers.len());
4067            hal_vertex_buffer_layouts = Vec::with_capacity(vertex.buffers.len());
4068            total_attributes = 0;
4069            for (i, vb_state) in vertex.buffers.iter().enumerate() {
4070                let Some(vb_state) = vb_state else {
4071                    vertex_steps.push(None);
4072                    hal_vertex_buffer_layouts.push(None);
4073                    continue;
4074                };
4075
4076                // https://gpuweb.github.io/gpuweb/#abstract-opdef-validating-gpuvertexbufferlayout
4077
4078                if vb_state.array_stride > self.limits.max_vertex_buffer_array_stride as u64 {
4079                    return Err(pipeline::CreateRenderPipelineError::VertexStrideTooLarge {
4080                        index: i as u32,
4081                        given: vb_state.array_stride as u32,
4082                        limit: self.limits.max_vertex_buffer_array_stride,
4083                    });
4084                }
4085                if vb_state.array_stride % wgt::VERTEX_ALIGNMENT != 0 {
4086                    return Err(pipeline::CreateRenderPipelineError::UnalignedVertexStride {
4087                        index: i as u32,
4088                        stride: vb_state.array_stride,
4089                    });
4090                }
4091
4092                let max_stride = if vb_state.array_stride == 0 {
4093                    self.limits.max_vertex_buffer_array_stride as u64
4094                } else {
4095                    vb_state.array_stride
4096                };
4097                let mut last_stride = 0;
4098                for attribute in vb_state.attributes.iter() {
4099                    let attribute_stride = attribute.offset + attribute.format.size();
4100                    if attribute_stride > max_stride {
4101                        return Err(
4102                            pipeline::CreateRenderPipelineError::VertexAttributeStrideTooLarge {
4103                                location: attribute.shader_location,
4104                                given: attribute_stride as u32,
4105                                limit: max_stride as u32,
4106                            },
4107                        );
4108                    }
4109
4110                    let required_offset_alignment = attribute.format.size().min(4);
4111                    if attribute.offset % required_offset_alignment != 0 {
4112                        return Err(
4113                            pipeline::CreateRenderPipelineError::InvalidVertexAttributeOffset {
4114                                location: attribute.shader_location,
4115                                offset: attribute.offset,
4116                            },
4117                        );
4118                    }
4119
4120                    if attribute.shader_location >= self.limits.max_vertex_attributes {
4121                        return Err(
4122                            pipeline::CreateRenderPipelineError::VertexAttributeLocationTooLarge {
4123                                given: attribute.shader_location,
4124                                limit: self.limits.max_vertex_attributes,
4125                            },
4126                        );
4127                    }
4128
4129                    last_stride = last_stride.max(attribute_stride);
4130                }
4131
4132                vertex_steps.push(Some(pipeline::VertexStep {
4133                    stride: vb_state.array_stride,
4134                    last_stride,
4135                    mode: vb_state.step_mode,
4136                }));
4137                hal_vertex_buffer_layouts.push(if vb_state.attributes.is_empty() {
4138                    None
4139                } else {
4140                    Some(hal::VertexBufferLayout {
4141                        array_stride: vb_state.array_stride,
4142                        step_mode: vb_state.step_mode,
4143                        attributes: vb_state.attributes.as_ref(),
4144                    })
4145                });
4146
4147                for attribute in vb_state.attributes.iter() {
4148                    if attribute.offset >= 0x10000000 {
4149                        return Err(
4150                            pipeline::CreateRenderPipelineError::InvalidVertexAttributeOffset {
4151                                location: attribute.shader_location,
4152                                offset: attribute.offset,
4153                            },
4154                        );
4155                    }
4156
4157                    if let wgt::VertexFormat::Float64
4158                    | wgt::VertexFormat::Float64x2
4159                    | wgt::VertexFormat::Float64x3
4160                    | wgt::VertexFormat::Float64x4 = attribute.format
4161                    {
4162                        self.require_features(wgt::Features::VERTEX_ATTRIBUTE_64BIT)?;
4163                    }
4164
4165                    let previous = io.varyings.insert(
4166                        attribute.shader_location,
4167                        validation::InterfaceVar::vertex_attribute(attribute.format),
4168                    );
4169
4170                    if previous.is_some() {
4171                        return Err(pipeline::CreateRenderPipelineError::ShaderLocationClash(
4172                            attribute.shader_location,
4173                        ));
4174                    }
4175                }
4176                total_attributes += vb_state.attributes.len();
4177            }
4178
4179            if total_attributes > self.limits.max_vertex_attributes as usize {
4180                return Err(
4181                    pipeline::CreateRenderPipelineError::TooManyVertexAttributes {
4182                        given: total_attributes as u32,
4183                        limit: self.limits.max_vertex_attributes,
4184                    },
4185                );
4186            }
4187        } else {
4188            vertex_steps = Vec::new();
4189            hal_vertex_buffer_layouts = Vec::new();
4190        };
4191
4192        if desc.primitive.strip_index_format.is_some() && !desc.primitive.topology.is_strip() {
4193            return Err(
4194                pipeline::CreateRenderPipelineError::StripIndexFormatForNonStripTopology {
4195                    strip_index_format: desc.primitive.strip_index_format,
4196                    topology: desc.primitive.topology,
4197                },
4198            );
4199        }
4200
4201        if desc.primitive.unclipped_depth {
4202            self.require_features(wgt::Features::DEPTH_CLIP_CONTROL)?;
4203        }
4204
4205        if desc.primitive.polygon_mode == wgt::PolygonMode::Line {
4206            self.require_features(wgt::Features::POLYGON_MODE_LINE)?;
4207        }
4208        if desc.primitive.polygon_mode == wgt::PolygonMode::Point {
4209            self.require_features(wgt::Features::POLYGON_MODE_POINT)?;
4210        }
4211
4212        if desc.primitive.conservative {
4213            self.require_features(wgt::Features::CONSERVATIVE_RASTERIZATION)?;
4214        }
4215
4216        if desc.primitive.conservative && desc.primitive.polygon_mode != wgt::PolygonMode::Fill {
4217            return Err(
4218                pipeline::CreateRenderPipelineError::ConservativeRasterizationNonFillPolygonMode,
4219            );
4220        }
4221
4222        let mut target_specified = false;
4223
4224        for (i, cs) in color_targets.iter().enumerate() {
4225            if let Some(cs) = cs.as_ref() {
4226                target_specified = true;
4227                let error = 'error: {
4228                    // This is expected to be the operative check for illegal write mask
4229                    // values (larger than 15), because WebGPU requires that it be validated
4230                    // on the device timeline.
4231                    if cs.write_mask.contains_unknown_bits() {
4232                        break 'error Some(ColorStateError::InvalidWriteMask(cs.write_mask));
4233                    }
4234
4235                    let format_features = self.describe_format_features(cs.format)?;
4236                    if !format_features
4237                        .allowed_usages
4238                        .contains(wgt::TextureUsages::RENDER_ATTACHMENT)
4239                    {
4240                        break 'error Some(ColorStateError::FormatNotRenderable(cs.format));
4241                    }
4242                    if cs.blend.is_some() && !format_features.flags.contains(Tfff::BLENDABLE) {
4243                        break 'error Some(ColorStateError::FormatNotBlendable(cs.format));
4244                    }
4245                    if !hal::FormatAspects::from(cs.format).contains(hal::FormatAspects::COLOR) {
4246                        break 'error Some(ColorStateError::FormatNotColor(cs.format));
4247                    }
4248
4249                    if desc.multisample.count > 1
4250                        && !format_features
4251                            .flags
4252                            .sample_count_supported(desc.multisample.count)
4253                    {
4254                        break 'error Some(ColorStateError::InvalidSampleCount(
4255                            desc.multisample.count,
4256                            cs.format,
4257                            cs.format
4258                                .guaranteed_format_features(self.features)
4259                                .flags
4260                                .supported_sample_counts(),
4261                            self.adapter
4262                                .get_texture_format_features(cs.format)
4263                                .flags
4264                                .supported_sample_counts(),
4265                        ));
4266                    }
4267
4268                    if let Some(blend_mode) = cs.blend {
4269                        for component in [&blend_mode.color, &blend_mode.alpha] {
4270                            for factor in [component.src_factor, component.dst_factor] {
4271                                if factor.ref_second_blend_source() {
4272                                    self.require_features(wgt::Features::DUAL_SOURCE_BLENDING)?;
4273                                    if i == 0 {
4274                                        dual_source_blending = true;
4275                                    } else {
4276                                        break 'error Some(
4277                                            ColorStateError::BlendFactorOnUnsupportedTarget {
4278                                                factor,
4279                                                target: i as u32,
4280                                            },
4281                                        );
4282                                    }
4283                                }
4284
4285                                if [wgt::BlendOperation::Min, wgt::BlendOperation::Max]
4286                                    .contains(&component.operation)
4287                                    && factor != wgt::BlendFactor::One
4288                                {
4289                                    break 'error Some(ColorStateError::InvalidMinMaxBlendFactor {
4290                                        factor,
4291                                        target: i as u32,
4292                                    });
4293                                }
4294                            }
4295                        }
4296                    }
4297
4298                    break 'error None;
4299                };
4300                if let Some(e) = error {
4301                    return Err(pipeline::CreateRenderPipelineError::ColorState(i as u8, e));
4302                }
4303            }
4304        }
4305
4306        if dual_source_blending && color_targets.len() > 1 {
4307            return Err(
4308                pipeline::CreateRenderPipelineError::DualSourceBlendingWithMultipleColorTargets {
4309                    count: color_targets.len(),
4310                },
4311            );
4312        }
4313
4314        validation::validate_color_attachment_bytes_per_sample(
4315            color_targets.iter().flatten().map(|cs| cs.format),
4316            self.limits.max_color_attachment_bytes_per_sample,
4317        )
4318        .map_err(pipeline::CreateRenderPipelineError::ColorAttachment)?;
4319
4320        if let Some(ds) = depth_stencil_state {
4321            // See <https://gpuweb.github.io/gpuweb/#abstract-opdef-validating-gpudepthstencilstate>.
4322            target_specified = true;
4323            let error = 'error: {
4324                if !ds.format.is_depth_stencil_format() {
4325                    // This error case is not redundant with the aspect check below when
4326                    // neither depth nor stencil is enabled at all.
4327                    break 'error Some(pipeline::DepthStencilStateError::FormatNotDepthOrStencil(
4328                        ds.format,
4329                    ));
4330                }
4331
4332                let format_features = self.describe_format_features(ds.format)?;
4333                if !format_features
4334                    .allowed_usages
4335                    .contains(wgt::TextureUsages::RENDER_ATTACHMENT)
4336                {
4337                    break 'error Some(pipeline::DepthStencilStateError::FormatNotRenderable(
4338                        ds.format,
4339                    ));
4340                }
4341
4342                let aspect = hal::FormatAspects::from(ds.format);
4343                if aspect.contains(hal::FormatAspects::DEPTH) {
4344                    has_depth_attachment = true;
4345                } else if ds.is_depth_enabled() {
4346                    break 'error Some(pipeline::DepthStencilStateError::FormatNotDepth(ds.format));
4347                }
4348                if has_depth_attachment {
4349                    let Some(depth_write_enabled) = ds.depth_write_enabled else {
4350                        break 'error Some(
4351                            pipeline::DepthStencilStateError::MissingDepthWriteEnabled(ds.format),
4352                        );
4353                    };
4354
4355                    let depth_compare_required = depth_write_enabled
4356                        || ds.stencil.front.depth_fail_op != wgt::StencilOperation::Keep
4357                        || ds.stencil.back.depth_fail_op != wgt::StencilOperation::Keep;
4358                    if depth_compare_required && ds.depth_compare.is_none() {
4359                        break 'error Some(pipeline::DepthStencilStateError::MissingDepthCompare(
4360                            ds.format,
4361                        ));
4362                    }
4363                }
4364
4365                if ds.stencil.is_enabled() && !aspect.contains(hal::FormatAspects::STENCIL) {
4366                    break 'error Some(pipeline::DepthStencilStateError::FormatNotStencil(
4367                        ds.format,
4368                    ));
4369                }
4370                if desc.multisample.count > 1
4371                    && !format_features
4372                        .flags
4373                        .sample_count_supported(desc.multisample.count)
4374                {
4375                    break 'error Some(pipeline::DepthStencilStateError::InvalidSampleCount(
4376                        desc.multisample.count,
4377                        ds.format,
4378                        ds.format
4379                            .guaranteed_format_features(self.features)
4380                            .flags
4381                            .supported_sample_counts(),
4382                        self.adapter
4383                            .get_texture_format_features(ds.format)
4384                            .flags
4385                            .supported_sample_counts(),
4386                    ));
4387                }
4388
4389                break 'error None;
4390            };
4391            if let Some(e) = error {
4392                return Err(pipeline::CreateRenderPipelineError::DepthStencilState(e));
4393            }
4394
4395            if ds.bias.clamp != 0.0 {
4396                self.require_downlevel_flags(wgt::DownlevelFlags::DEPTH_BIAS_CLAMP)?;
4397            }
4398
4399            if (ds.bias.is_enabled() || ds.bias.clamp != 0.0)
4400                && !desc.primitive.topology.is_triangles()
4401            {
4402                return Err(pipeline::CreateRenderPipelineError::DepthStencilState(
4403                    pipeline::DepthStencilStateError::DepthBiasWithIncompatibleTopology(
4404                        desc.primitive.topology,
4405                    ),
4406                ));
4407            }
4408        }
4409
4410        if !target_specified {
4411            return Err(pipeline::CreateRenderPipelineError::NoTargetSpecified);
4412        }
4413
4414        let is_auto_layout = desc.layout.is_none();
4415
4416        // Get the pipeline layout from the desc if it is provided.
4417        let pipeline_layout = match desc.layout {
4418            Some(pipeline_layout) => {
4419                pipeline_layout.same_device(self)?;
4420                Some(pipeline_layout)
4421            }
4422            None => None,
4423        };
4424
4425        let mut binding_layout_source = match pipeline_layout {
4426            Some(pipeline_layout) => validation::BindingLayoutSource::Provided(pipeline_layout),
4427            None => validation::BindingLayoutSource::new_derived(&self.limits),
4428        };
4429
4430        let samples = {
4431            let sc = desc.multisample.count;
4432            if sc == 0 || sc > 32 || !sc.is_power_of_two() {
4433                return Err(pipeline::CreateRenderPipelineError::InvalidSampleCount(sc));
4434            }
4435            sc
4436        };
4437
4438        let mut vertex_stage = None;
4439        let mut task_stage = None;
4440        let mut mesh_stage = None;
4441        let mut _vertex_entry_point_name = String::new();
4442        let mut _task_entry_point_name = String::new();
4443        let mut _mesh_entry_point_name = String::new();
4444        let mut immediate_slots_required = naga::valid::ImmediateSlots::default();
4445        match desc.vertex {
4446            pipeline::RenderPipelineVertexProcessor::Vertex(ref vertex) => {
4447                vertex_stage = {
4448                    let stage_desc = &vertex.stage;
4449                    let stage = validation::ShaderStageForValidation::Vertex {
4450                        topology: desc.primitive.topology,
4451                        compare_function: desc.depth_stencil.as_ref().and_then(|d| d.depth_compare),
4452                    };
4453                    let stage_bit = stage.to_wgt_bit();
4454
4455                    let vertex_shader_module = &stage_desc.module;
4456                    vertex_shader_module.same_device(self)?;
4457
4458                    let stage_err = |error| pipeline::CreateRenderPipelineError::Stage {
4459                        stage: stage_bit,
4460                        error,
4461                    };
4462
4463                    _vertex_entry_point_name = vertex_shader_module
4464                        .finalize_entry_point_name(
4465                            stage.to_naga(),
4466                            stage_desc.entry_point.as_ref().map(|ep| ep.as_ref()),
4467                        )
4468                        .map_err(stage_err)?;
4469
4470                    if let Some(interface) = vertex_shader_module.interface.interface() {
4471                        immediate_slots_required |= interface
4472                            .immediate_slots_required(stage.to_naga(), &_vertex_entry_point_name);
4473                        io = interface
4474                            .check_stage(
4475                                &mut binding_layout_source,
4476                                &mut shader_binding_sizes,
4477                                &_vertex_entry_point_name,
4478                                stage,
4479                                io,
4480                                Some(desc.primitive.topology),
4481                            )
4482                            .map_err(stage_err)?;
4483                        validated_stages |= stage_bit;
4484                    }
4485                    Some(hal::ProgrammableStage {
4486                        module: vertex_shader_module.raw(),
4487                        entry_point: &_vertex_entry_point_name,
4488                        constants: &stage_desc.constants,
4489                        zero_initialize_workgroup_memory: stage_desc
4490                            .zero_initialize_workgroup_memory,
4491                    })
4492                };
4493            }
4494            pipeline::RenderPipelineVertexProcessor::Mesh(ref task, ref mesh) => {
4495                self.require_features(wgt::Features::EXPERIMENTAL_MESH_SHADER)?;
4496
4497                task_stage = if let Some(task) = task {
4498                    let stage_desc = &task.stage;
4499                    let stage = validation::ShaderStageForValidation::Task;
4500                    let stage_bit = stage.to_wgt_bit();
4501                    let task_shader_module = &stage_desc.module;
4502                    task_shader_module.same_device(self)?;
4503
4504                    let stage_err = |error| pipeline::CreateRenderPipelineError::Stage {
4505                        stage: stage_bit,
4506                        error,
4507                    };
4508
4509                    _task_entry_point_name = task_shader_module
4510                        .finalize_entry_point_name(
4511                            stage.to_naga(),
4512                            stage_desc.entry_point.as_ref().map(|ep| ep.as_ref()),
4513                        )
4514                        .map_err(stage_err)?;
4515
4516                    if let Some(interface) = task_shader_module.interface.interface() {
4517                        immediate_slots_required |= interface
4518                            .immediate_slots_required(stage.to_naga(), &_task_entry_point_name);
4519                        io = interface
4520                            .check_stage(
4521                                &mut binding_layout_source,
4522                                &mut shader_binding_sizes,
4523                                &_task_entry_point_name,
4524                                stage,
4525                                io,
4526                                Some(desc.primitive.topology),
4527                            )
4528                            .map_err(stage_err)?;
4529                        validated_stages |= stage_bit;
4530                    }
4531                    Some(hal::ProgrammableStage {
4532                        module: task_shader_module.raw(),
4533                        entry_point: &_task_entry_point_name,
4534                        constants: &stage_desc.constants,
4535                        zero_initialize_workgroup_memory: stage_desc
4536                            .zero_initialize_workgroup_memory,
4537                    })
4538                } else {
4539                    None
4540                };
4541                mesh_stage = {
4542                    let stage_desc = &mesh.stage;
4543                    let stage = validation::ShaderStageForValidation::Mesh;
4544                    let stage_bit = stage.to_wgt_bit();
4545                    let mesh_shader_module = &stage_desc.module;
4546                    mesh_shader_module.same_device(self)?;
4547
4548                    let stage_err = |error| pipeline::CreateRenderPipelineError::Stage {
4549                        stage: stage_bit,
4550                        error,
4551                    };
4552
4553                    _mesh_entry_point_name = mesh_shader_module
4554                        .finalize_entry_point_name(
4555                            stage.to_naga(),
4556                            stage_desc.entry_point.as_ref().map(|ep| ep.as_ref()),
4557                        )
4558                        .map_err(stage_err)?;
4559
4560                    if let Some(interface) = mesh_shader_module.interface.interface() {
4561                        immediate_slots_required |= interface
4562                            .immediate_slots_required(stage.to_naga(), &_mesh_entry_point_name);
4563                        io = interface
4564                            .check_stage(
4565                                &mut binding_layout_source,
4566                                &mut shader_binding_sizes,
4567                                &_mesh_entry_point_name,
4568                                stage,
4569                                io,
4570                                Some(desc.primitive.topology),
4571                            )
4572                            .map_err(stage_err)?;
4573                        validated_stages |= stage_bit;
4574                    }
4575                    Some(hal::ProgrammableStage {
4576                        module: mesh_shader_module.raw(),
4577                        entry_point: &_mesh_entry_point_name,
4578                        constants: &stage_desc.constants,
4579                        zero_initialize_workgroup_memory: stage_desc
4580                            .zero_initialize_workgroup_memory,
4581                    })
4582                };
4583            }
4584        }
4585
4586        let fragment_entry_point_name;
4587        let fragment_stage = match desc.fragment {
4588            Some(ref fragment_state) => {
4589                let stage = validation::ShaderStageForValidation::Fragment {
4590                    dual_source_blending,
4591                    has_depth_attachment,
4592                };
4593                let stage_bit = stage.to_wgt_bit();
4594
4595                let shader_module = &fragment_state.stage.module;
4596                shader_module.same_device(self)?;
4597
4598                let stage_err = |error| pipeline::CreateRenderPipelineError::Stage {
4599                    stage: stage_bit,
4600                    error,
4601                };
4602
4603                fragment_entry_point_name = shader_module
4604                    .finalize_entry_point_name(
4605                        stage.to_naga(),
4606                        fragment_state
4607                            .stage
4608                            .entry_point
4609                            .as_ref()
4610                            .map(|ep| ep.as_ref()),
4611                    )
4612                    .map_err(stage_err)?;
4613
4614                if let Some(interface) = shader_module.interface.interface() {
4615                    immediate_slots_required |= interface
4616                        .immediate_slots_required(stage.to_naga(), &fragment_entry_point_name);
4617                    io = interface
4618                        .check_stage(
4619                            &mut binding_layout_source,
4620                            &mut shader_binding_sizes,
4621                            &fragment_entry_point_name,
4622                            stage,
4623                            io,
4624                            Some(desc.primitive.topology),
4625                        )
4626                        .map_err(stage_err)?;
4627                    validated_stages |= stage_bit;
4628                }
4629
4630                Some(hal::ProgrammableStage {
4631                    module: shader_module.raw(),
4632                    entry_point: &fragment_entry_point_name,
4633                    constants: &fragment_state.stage.constants,
4634                    zero_initialize_workgroup_memory: fragment_state
4635                        .stage
4636                        .zero_initialize_workgroup_memory,
4637                })
4638            }
4639            None => None,
4640        };
4641
4642        if validated_stages.contains(wgt::ShaderStages::FRAGMENT) {
4643            for (i, output) in io.varyings.iter() {
4644                match color_targets.get(*i as usize) {
4645                    Some(Some(state)) => {
4646                        validation::check_texture_format(state.format, &output.ty).map_err(
4647                            |pipeline| {
4648                                pipeline::CreateRenderPipelineError::ColorState(
4649                                    *i as u8,
4650                                    ColorStateError::IncompatibleFormat {
4651                                        pipeline,
4652                                        shader: output.ty,
4653                                    },
4654                                )
4655                            },
4656                        )?;
4657                    }
4658                    _ => {
4659                        log::debug!(
4660                            "The fragment stage {:?} output @location({}) values are ignored",
4661                            fragment_stage
4662                                .as_ref()
4663                                .map_or("", |stage| stage.entry_point),
4664                            i
4665                        );
4666                    }
4667                }
4668            }
4669        }
4670        let last_stage = match desc.fragment {
4671            Some(_) => wgt::ShaderStages::FRAGMENT,
4672            None => wgt::ShaderStages::VERTEX,
4673        };
4674        if is_auto_layout && !validated_stages.contains(last_stage) {
4675            return Err(pipeline::ImplicitLayoutError::ReflectionError(last_stage).into());
4676        }
4677
4678        let pipeline_layout = match binding_layout_source {
4679            validation::BindingLayoutSource::Provided(pipeline_layout) => pipeline_layout,
4680            validation::BindingLayoutSource::Derived(entries) => {
4681                let immediate_size = {
4682                    let immediate_size_of = |sm: &pipeline::ShaderModule| {
4683                        sm.interface.interface().map(|i| i.immediate_size)
4684                    };
4685                    let vertex = match desc.vertex {
4686                        pipeline::RenderPipelineVertexProcessor::Vertex(ref v) => {
4687                            immediate_size_of(&v.stage.module)
4688                        }
4689                        pipeline::RenderPipelineVertexProcessor::Mesh(ref task, ref mesh) => task
4690                            .as_ref()
4691                            .and_then(|t| immediate_size_of(&t.stage.module))
4692                            .max(immediate_size_of(&mesh.stage.module)),
4693                    };
4694                    let fragment = desc
4695                        .fragment
4696                        .as_ref()
4697                        .and_then(|f| immediate_size_of(&f.stage.module));
4698                    vertex.max(fragment).unwrap_or(0)
4699                };
4700                self.create_derived_pipeline_layout(entries, immediate_size)?
4701            }
4702        };
4703
4704        if let pipeline::RenderPipelineVertexProcessor::Vertex(ref vertex) = desc.vertex {
4705            let bind_groups_plus_vertex_buffers =
4706                u32::try_from(pipeline_layout.bind_group_layouts.len() + vertex.buffers.len())
4707                    .unwrap();
4708            if bind_groups_plus_vertex_buffers > self.limits.max_bind_groups_plus_vertex_buffers {
4709                return Err(
4710                    pipeline::CreateRenderPipelineError::TooManyBindGroupsPlusVertexBuffers {
4711                        given: bind_groups_plus_vertex_buffers,
4712                        limit: self.limits.max_bind_groups_plus_vertex_buffers,
4713                    },
4714                );
4715            }
4716        }
4717
4718        // Multiview is only supported if the feature is enabled
4719        if let Some(mv_mask) = desc.multiview_mask {
4720            self.require_features(wgt::Features::MULTIVIEW)?;
4721            if !(mv_mask.get() + 1).is_power_of_two() {
4722                self.require_features(wgt::Features::SELECTIVE_MULTIVIEW)?;
4723            }
4724        }
4725
4726        if !self
4727            .downlevel
4728            .flags
4729            .contains(wgt::DownlevelFlags::BUFFER_BINDINGS_NOT_16_BYTE_ALIGNED)
4730        {
4731            for (binding, size) in shader_binding_sizes.iter() {
4732                if size.get() % 16 != 0 {
4733                    return Err(pipeline::CreateRenderPipelineError::UnalignedShader {
4734                        binding: binding.binding,
4735                        group: binding.group,
4736                        size: size.get(),
4737                    });
4738                }
4739            }
4740        }
4741
4742        let late_sized_buffer_groups =
4743            Device::make_late_sized_buffer_groups(&shader_binding_sizes, &pipeline_layout);
4744
4745        let cache = match desc.cache {
4746            Some(cache) => {
4747                cache.same_device(self)?;
4748                Some(cache)
4749            }
4750            None => None,
4751        };
4752
4753        let is_mesh = mesh_stage.is_some();
4754        let has_task_shader = task_stage.is_some();
4755        let raw = {
4756            let pipeline_desc = hal::RenderPipelineDescriptor {
4757                label: desc.label.to_hal(self.instance_flags),
4758                layout: pipeline_layout.raw(),
4759                vertex_processor: match vertex_stage {
4760                    Some(vertex_stage) => hal::VertexProcessor::Standard {
4761                        vertex_buffers: &hal_vertex_buffer_layouts,
4762                        vertex_stage,
4763                    },
4764                    None => hal::VertexProcessor::Mesh {
4765                        task_stage,
4766                        mesh_stage: mesh_stage.unwrap(),
4767                    },
4768                },
4769                primitive: desc.primitive,
4770                depth_stencil: desc.depth_stencil.clone(),
4771                multisample: desc.multisample,
4772                fragment_stage,
4773                color_targets,
4774                multiview_mask: desc.multiview_mask,
4775                cache: cache.as_ref().map(|it| it.raw()),
4776            };
4777            unsafe { self.raw().create_render_pipeline(&pipeline_desc) }.map_err(
4778                |err| match err {
4779                    hal::PipelineError::Device(error) => {
4780                        pipeline::CreateRenderPipelineError::Device(self.handle_hal_error(error))
4781                    }
4782                    hal::PipelineError::Linkage(stage, msg) => {
4783                        pipeline::CreateRenderPipelineError::Internal { stage, error: msg }
4784                    }
4785                    hal::PipelineError::EntryPoint(stage) => {
4786                        pipeline::CreateRenderPipelineError::Internal {
4787                            stage: hal::auxil::map_naga_stage(stage),
4788                            error: ENTRYPOINT_FAILURE_ERROR.to_string(),
4789                        }
4790                    }
4791                    hal::PipelineError::PipelineConstants(stage, error) => {
4792                        pipeline::CreateRenderPipelineError::PipelineConstants { stage, error }
4793                    }
4794                },
4795            )?
4796        };
4797
4798        let pass_context = RenderPassContext {
4799            attachments: AttachmentData {
4800                colors: color_targets
4801                    .iter()
4802                    .map(|state| state.as_ref().map(|s| s.format))
4803                    .collect(),
4804                resolves: ArrayVec::new(),
4805                depth_stencil: depth_stencil_state.as_ref().map(|state| state.format),
4806            },
4807            sample_count: samples,
4808            multiview_mask: desc.multiview_mask,
4809        };
4810
4811        let mut flags = pipeline::PipelineFlags::empty();
4812        for state in color_targets.iter().filter_map(|s| s.as_ref()) {
4813            if let Some(ref bs) = state.blend {
4814                if bs.color.uses_constant() | bs.alpha.uses_constant() {
4815                    flags |= pipeline::PipelineFlags::BLEND_CONSTANT;
4816                }
4817            }
4818        }
4819        if let Some(ds) = depth_stencil_state.as_ref() {
4820            if ds.stencil.is_enabled() && ds.stencil.needs_ref_value() {
4821                flags |= pipeline::PipelineFlags::STENCIL_REFERENCE;
4822            }
4823            if !ds.is_depth_read_only() {
4824                flags |= pipeline::PipelineFlags::WRITES_DEPTH;
4825            }
4826            if !ds.is_stencil_read_only(desc.primitive.cull_mode) {
4827                flags |= pipeline::PipelineFlags::WRITES_STENCIL;
4828            }
4829        }
4830        let shader_modules = {
4831            let mut shader_modules = ArrayVec::new();
4832            match desc.vertex {
4833                pipeline::RenderPipelineVertexProcessor::Vertex(vertex) => {
4834                    shader_modules.push(vertex.stage.module)
4835                }
4836                pipeline::RenderPipelineVertexProcessor::Mesh(task, mesh) => {
4837                    if let Some(task) = task {
4838                        shader_modules.push(task.stage.module);
4839                    }
4840                    shader_modules.push(mesh.stage.module);
4841                }
4842            }
4843            shader_modules.extend(desc.fragment.map(|f| f.stage.module));
4844            shader_modules
4845        };
4846
4847        let pipeline = pipeline::RenderPipeline {
4848            raw: ManuallyDrop::new(raw),
4849            layout: pipeline_layout,
4850            device: self.clone(),
4851            pass_context,
4852            _shader_modules: shader_modules,
4853            flags,
4854            topology: desc.primitive.topology,
4855            strip_index_format: desc.primitive.strip_index_format,
4856            vertex_steps,
4857            late_sized_buffer_groups,
4858            immediate_slots_required,
4859            label: desc.label.to_string(),
4860            tracking_data: TrackingData::new(self.tracker_indices.render_pipelines.clone()),
4861            is_mesh,
4862            has_task_shader,
4863        };
4864
4865        let pipeline = Arc::new(pipeline);
4866
4867        if is_auto_layout {
4868            for bgl in pipeline.layout.bind_group_layouts.iter() {
4869                let Some(bgl) = bgl else {
4870                    continue;
4871                };
4872
4873                // `bind_group_layouts` might contain duplicate entries, so we need to ignore the
4874                // result.
4875                let _ = bgl.exclusive_pipeline.set((&pipeline).into());
4876            }
4877        }
4878
4879        Ok(pipeline)
4880    }
4881
4882    /// # Safety
4883    /// The `data` field on `desc` must have previously been returned from
4884    /// [`crate::global::Global::pipeline_cache_get_data`]
4885    pub unsafe fn create_pipeline_cache(
4886        self: &Arc<Self>,
4887        desc: &pipeline::PipelineCacheDescriptor,
4888    ) -> Result<Arc<pipeline::PipelineCache>, pipeline::CreatePipelineCacheError> {
4889        use crate::pipeline_cache;
4890
4891        self.check_is_valid()?;
4892
4893        self.require_features(wgt::Features::PIPELINE_CACHE)?;
4894        let data = if let Some((data, validation_key)) = desc
4895            .data
4896            .as_ref()
4897            .zip(self.raw().pipeline_cache_validation_key())
4898        {
4899            let data = pipeline_cache::validate_pipeline_cache(
4900                data,
4901                &self.adapter.raw.info,
4902                validation_key,
4903            );
4904            match data {
4905                Ok(data) => Some(data),
4906                Err(e) if e.was_avoidable() || !desc.fallback => return Err(e.into()),
4907                // If the error was unavoidable and we are asked to fallback, do so
4908                Err(_) => None,
4909            }
4910        } else {
4911            None
4912        };
4913        let cache_desc = hal::PipelineCacheDescriptor {
4914            data,
4915            label: desc.label.to_hal(self.instance_flags),
4916        };
4917        let raw = match unsafe { self.raw().create_pipeline_cache(&cache_desc) } {
4918            Ok(raw) => raw,
4919            Err(e) => match e {
4920                hal::PipelineCacheError::Device(e) => return Err(self.handle_hal_error(e).into()),
4921            },
4922        };
4923        let cache = pipeline::PipelineCache {
4924            device: self.clone(),
4925            label: desc.label.to_string(),
4926            // This would be none in the error condition, which we don't implement yet
4927            raw: ManuallyDrop::new(raw),
4928        };
4929
4930        let cache = Arc::new(cache);
4931
4932        Ok(cache)
4933    }
4934
4935    fn get_texture_format_features(&self, format: TextureFormat) -> wgt::TextureFormatFeatures {
4936        // Variant of adapter.get_texture_format_features that takes device features into account
4937        use wgt::TextureFormatFeatureFlags as tfsc;
4938        let mut format_features = self.adapter.get_texture_format_features(format);
4939        if (format == TextureFormat::R32Float
4940            || format == TextureFormat::Rg32Float
4941            || format == TextureFormat::Rgba32Float)
4942            && !self.features.contains(wgt::Features::FLOAT32_FILTERABLE)
4943        {
4944            format_features.flags.set(tfsc::FILTERABLE, false);
4945        }
4946        format_features
4947    }
4948
4949    pub(crate) fn describe_format_features(
4950        &self,
4951        format: TextureFormat,
4952    ) -> Result<wgt::TextureFormatFeatures, MissingFeatures> {
4953        self.require_features(format.required_features())?;
4954
4955        let using_device_features = self
4956            .features
4957            .contains(wgt::Features::TEXTURE_ADAPTER_SPECIFIC_FORMAT_FEATURES);
4958        // If we're running downlevel, we need to manually ask the backend what
4959        // we can use as we can't trust WebGPU.
4960        let downlevel = !self
4961            .downlevel
4962            .flags
4963            .contains(wgt::DownlevelFlags::WEBGPU_TEXTURE_FORMAT_SUPPORT);
4964
4965        if using_device_features || downlevel {
4966            Ok(self.get_texture_format_features(format))
4967        } else {
4968            Ok(format.guaranteed_format_features(self.features))
4969        }
4970    }
4971
4972    #[cfg(feature = "replay")]
4973    pub(crate) fn wait_for_submit(
4974        &self,
4975        submission_index: crate::SubmissionIndex,
4976    ) -> Result<(), DeviceError> {
4977        let last_done_index = unsafe { self.raw().get_fence_value(self.fence.as_ref()) }
4978            .map_err(|e| self.handle_hal_error(e))?;
4979        if last_done_index < submission_index {
4980            unsafe { self.raw().wait(self.fence.as_ref(), submission_index, None) }
4981                .map_err(|e| self.handle_hal_error(e))?;
4982            if let Some(queue) = self.get_queue() {
4983                let closures = queue.lock_life().triage_submissions(submission_index);
4984                assert!(
4985                    closures.is_empty(),
4986                    "wait_for_submit is not expected to work with closures"
4987                );
4988            }
4989        }
4990        Ok(())
4991    }
4992
4993    pub fn create_query_set(
4994        self: &Arc<Self>,
4995        desc: &resource::QuerySetDescriptor,
4996    ) -> Result<Arc<QuerySet>, resource::CreateQuerySetError> {
4997        use resource::CreateQuerySetError as Error;
4998
4999        self.check_is_valid()?;
5000
5001        match desc.ty {
5002            wgt::QueryType::Occlusion => {}
5003            wgt::QueryType::Timestamp => {
5004                self.require_features(wgt::Features::TIMESTAMP_QUERY)?;
5005            }
5006            wgt::QueryType::PipelineStatistics(..) => {
5007                self.require_features(wgt::Features::PIPELINE_STATISTICS_QUERY)?;
5008            }
5009        }
5010
5011        if desc.count == 0 {
5012            return Err(Error::ZeroCount);
5013        }
5014
5015        if desc.count > wgt::QUERY_SET_MAX_QUERIES {
5016            return Err(Error::TooManyQueries {
5017                count: desc.count,
5018                maximum: wgt::QUERY_SET_MAX_QUERIES,
5019            });
5020        }
5021
5022        let hal_desc = desc.map_label(|label| label.to_hal(self.instance_flags));
5023
5024        let raw = unsafe { self.raw().create_query_set(&hal_desc) }
5025            .map_err(|e| self.handle_hal_error_with_nonfatal_oom(e))?;
5026
5027        let query_set = QuerySet {
5028            raw: ManuallyDrop::new(raw),
5029            device: self.clone(),
5030            label: desc.label.to_string(),
5031            tracking_data: TrackingData::new(self.tracker_indices.query_sets.clone()),
5032            desc: desc.map_label(|_| ()),
5033        };
5034
5035        let query_set = Arc::new(query_set);
5036
5037        Ok(query_set)
5038    }
5039
5040    pub fn configure_surface(
5041        self: &Arc<Self>,
5042        surface: &crate::instance::Surface,
5043        config: &wgt::SurfaceConfiguration<Vec<TextureFormat>>,
5044    ) -> Option<present::ConfigureSurfaceError> {
5045        use present::ConfigureSurfaceError as E;
5046        profiling::scope!("surface_configure");
5047
5048        fn validate_surface_configuration(
5049            config: &mut hal::SurfaceConfiguration,
5050            caps: &hal::SurfaceCapabilities,
5051            max_texture_dimension_2d: u32,
5052        ) -> Result<(), E> {
5053            let width = config.extent.width;
5054            let height = config.extent.height;
5055
5056            if width > max_texture_dimension_2d || height > max_texture_dimension_2d {
5057                return Err(E::TooLarge {
5058                    width,
5059                    height,
5060                    max_texture_dimension_2d,
5061                });
5062            }
5063
5064            if !caps.present_modes.contains(&config.present_mode) {
5065                // Automatic present mode checks.
5066                //
5067                // The "Automatic" modes are never supported by the backends.
5068                let fallbacks = match config.present_mode {
5069                    wgt::PresentMode::AutoVsync => {
5070                        &[wgt::PresentMode::FifoRelaxed, wgt::PresentMode::Fifo][..]
5071                    }
5072                    // Always end in FIFO to make sure it's always supported
5073                    wgt::PresentMode::AutoNoVsync => &[
5074                        wgt::PresentMode::Immediate,
5075                        wgt::PresentMode::Mailbox,
5076                        wgt::PresentMode::Fifo,
5077                    ][..],
5078                    _ => {
5079                        return Err(E::UnsupportedPresentMode {
5080                            requested: config.present_mode,
5081                            available: caps.present_modes.clone(),
5082                        });
5083                    }
5084                };
5085
5086                let new_mode = fallbacks
5087                    .iter()
5088                    .copied()
5089                    .find(|fallback| caps.present_modes.contains(fallback))
5090                    .unwrap_or_else(|| {
5091                        unreachable!(
5092                            "Fallback system failed to choose present mode. \
5093                            This is a bug. Mode: {:?}, Options: {:?}",
5094                            config.present_mode, &caps.present_modes
5095                        );
5096                    });
5097
5098                api_log!(
5099                    "Automatically choosing presentation mode by rule {:?}. Chose {new_mode:?}",
5100                    config.present_mode
5101                );
5102                config.present_mode = new_mode;
5103            }
5104            if !caps.formats.contains(&config.format) {
5105                return Err(E::UnsupportedFormat {
5106                    requested: config.format,
5107                    available: caps.formats.clone(),
5108                });
5109            }
5110            if !caps
5111                .composite_alpha_modes
5112                .contains(&config.composite_alpha_mode)
5113            {
5114                let new_alpha_mode = 'alpha: {
5115                    // Automatic alpha mode checks.
5116                    let fallbacks = match config.composite_alpha_mode {
5117                        wgt::CompositeAlphaMode::Auto => &[
5118                            wgt::CompositeAlphaMode::Opaque,
5119                            wgt::CompositeAlphaMode::Inherit,
5120                        ][..],
5121                        _ => {
5122                            return Err(E::UnsupportedAlphaMode {
5123                                requested: config.composite_alpha_mode,
5124                                available: caps.composite_alpha_modes.clone(),
5125                            });
5126                        }
5127                    };
5128
5129                    for &fallback in fallbacks {
5130                        if caps.composite_alpha_modes.contains(&fallback) {
5131                            break 'alpha fallback;
5132                        }
5133                    }
5134
5135                    unreachable!(
5136                        "Fallback system failed to choose alpha mode. This is a bug. \
5137                                  AlphaMode: {:?}, Options: {:?}",
5138                        config.composite_alpha_mode, &caps.composite_alpha_modes
5139                    );
5140                };
5141
5142                api_log!(
5143                    "Automatically choosing alpha mode by rule {:?}. Chose {new_alpha_mode:?}",
5144                    config.composite_alpha_mode
5145                );
5146                config.composite_alpha_mode = new_alpha_mode;
5147            }
5148            if !caps.usage.contains(config.usage) {
5149                return Err(E::UnsupportedUsage {
5150                    requested: config.usage,
5151                    available: caps.usage,
5152                });
5153            }
5154            if width == 0 || height == 0 {
5155                return Err(E::ZeroArea);
5156            }
5157            Ok(())
5158        }
5159
5160        log::debug!("configuring surface with {config:?}");
5161
5162        let error = 'error: {
5163            // User callbacks must not be called while we are holding locks.
5164            let user_callbacks;
5165            {
5166                if let Err(e) = self.check_is_valid() {
5167                    break 'error e.into();
5168                }
5169
5170                let caps = match surface.get_capabilities(&self.adapter) {
5171                    Ok(caps) => caps,
5172                    Err(_) => break 'error E::UnsupportedQueueFamily,
5173                };
5174
5175                let mut hal_view_formats = Vec::new();
5176                for format in config.view_formats.iter() {
5177                    if *format == config.format {
5178                        continue;
5179                    }
5180                    if !caps.formats.contains(&config.format) {
5181                        break 'error E::UnsupportedFormat {
5182                            requested: config.format,
5183                            available: caps.formats,
5184                        };
5185                    }
5186                    if config.format.remove_srgb_suffix() != format.remove_srgb_suffix() {
5187                        break 'error E::InvalidViewFormat(*format, config.format);
5188                    }
5189                    hal_view_formats.push(*format);
5190                }
5191
5192                if !hal_view_formats.is_empty() {
5193                    if let Err(missing_flag) =
5194                        self.require_downlevel_flags(wgt::DownlevelFlags::SURFACE_VIEW_FORMATS)
5195                    {
5196                        break 'error E::MissingDownlevelFlags(missing_flag);
5197                    }
5198                }
5199
5200                let maximum_frame_latency = config.desired_maximum_frame_latency.clamp(
5201                    *caps.maximum_frame_latency.start(),
5202                    *caps.maximum_frame_latency.end(),
5203                );
5204                let mut hal_config = hal::SurfaceConfiguration {
5205                    maximum_frame_latency,
5206                    present_mode: config.present_mode,
5207                    composite_alpha_mode: config.alpha_mode,
5208                    format: config.format,
5209                    extent: wgt::Extent3d {
5210                        width: config.width,
5211                        height: config.height,
5212                        depth_or_array_layers: 1,
5213                    },
5214                    usage: conv::map_texture_usage(
5215                        config.usage,
5216                        hal::FormatAspects::COLOR,
5217                        wgt::TextureFormatFeatureFlags::STORAGE_READ_ONLY
5218                            | wgt::TextureFormatFeatureFlags::STORAGE_WRITE_ONLY
5219                            | wgt::TextureFormatFeatureFlags::STORAGE_READ_WRITE,
5220                    ),
5221                    view_formats: hal_view_formats,
5222                };
5223
5224                if let Err(error) = validate_surface_configuration(
5225                    &mut hal_config,
5226                    &caps,
5227                    self.limits.max_texture_dimension_2d,
5228                ) {
5229                    break 'error error;
5230                }
5231
5232                // Wait for all work to finish before configuring the surface.
5233                let snatch_guard = self.snatchable_lock.read();
5234
5235                let maintain_result;
5236                (user_callbacks, maintain_result) =
5237                    self.maintain(wgt::PollType::wait_indefinitely(), snatch_guard);
5238
5239                match maintain_result {
5240                    // We're happy
5241                    Ok(wgt::PollStatus::QueueEmpty) => {}
5242                    Ok(wgt::PollStatus::WaitSucceeded) => {
5243                        // After the wait, the queue should be empty. It can only be non-empty
5244                        // if another thread is submitting at the same time.
5245                        break 'error E::GpuWaitTimeout;
5246                    }
5247                    Ok(wgt::PollStatus::Poll) => {
5248                        unreachable!("Cannot get a Poll result from a Wait action.")
5249                    }
5250                    Err(WaitIdleError::Timeout) if cfg!(target_arch = "wasm32") => {
5251                        // On wasm, you cannot actually successfully wait for the surface.
5252                        // However WebGL does not actually require you do this, so ignoring
5253                        // the failure is totally fine. See
5254                        // https://github.com/gfx-rs/wgpu/issues/7363
5255                    }
5256                    Err(e) => {
5257                        break 'error e.into();
5258                    }
5259                }
5260
5261                // All textures must be destroyed before the surface can be re-configured.
5262                if let Some(present) = surface.presentation.lock().take() {
5263                    if present.acquired_texture.is_some() {
5264                        break 'error E::PreviousOutputExists;
5265                    }
5266                }
5267
5268                // TODO: Texture views may still be alive that point to the texture.
5269                // this will allow the user to render to the surface texture, long after
5270                // it has been removed.
5271                //
5272                // https://github.com/gfx-rs/wgpu/issues/4105
5273
5274                let surface_raw = surface.raw(self.backend()).unwrap();
5275                match unsafe { surface_raw.configure(self.raw(), &hal_config) } {
5276                    Ok(()) => (),
5277                    Err(error) => {
5278                        break 'error match error {
5279                            hal::SurfaceError::Outdated
5280                            | hal::SurfaceError::Lost
5281                            | hal::SurfaceError::Occluded
5282                            | hal::SurfaceError::Timeout => E::InvalidSurface,
5283                            hal::SurfaceError::Device(error) => {
5284                                E::Device(self.handle_hal_error(error))
5285                            }
5286                            hal::SurfaceError::Other(message) => {
5287                                log::error!("surface configuration failed: {message}");
5288                                E::InvalidSurface
5289                            }
5290                        }
5291                    }
5292                }
5293
5294                let mut presentation = surface.presentation.lock();
5295                *presentation = Some(present::Presentation {
5296                    device: Arc::clone(self),
5297                    config: config.clone(),
5298                    acquired_texture: None,
5299                });
5300            }
5301
5302            user_callbacks.fire();
5303            return None;
5304        };
5305
5306        Some(error)
5307    }
5308
5309    fn lose(&self, message: &str) {
5310        // Follow the steps at https://gpuweb.github.io/gpuweb/#lose-the-device.
5311
5312        // Mark the device explicitly as invalid. This is checked in various
5313        // places to prevent new work from being submitted.
5314        self.valid.store(false, Ordering::Release);
5315
5316        // 1) Resolve the GPUDevice device.lost promise.
5317        if let Some(device_lost_closure) = self.device_lost_closure.lock().take() {
5318            device_lost_closure(DeviceLostReason::Unknown, message.to_string());
5319        }
5320
5321        // 2) Complete any outstanding mapAsync() steps.
5322        // 3) Complete any outstanding onSubmittedWorkDone() steps.
5323
5324        // These parts are passively accomplished by setting valid to false,
5325        // since that will prevent any new work from being added to the queues.
5326        // Future calls to poll_devices will continue to check the work queues
5327        // until they are cleared, and then drop the device.
5328    }
5329
5330    fn release_gpu_resources(&self) {
5331        // This is called when the device is lost, which makes every associated
5332        // resource invalid and unusable. This is an opportunity to release all of
5333        // the underlying gpu resources, even though the objects remain visible to
5334        // the user agent. We purge this memory naturally when resources have been
5335        // moved into the appropriate buckets, so this function just needs to
5336        // initiate movement into those buckets, and it can do that by calling
5337        // "destroy" on all the resources we know about.
5338
5339        // During these iterations, we discard all errors. We don't care!
5340        let trackers = self.trackers.lock();
5341        for buffer in trackers.buffers.used_resources() {
5342            if let Some(buffer) = Weak::upgrade(buffer) {
5343                buffer.destroy();
5344            }
5345        }
5346        for texture in trackers.textures.used_resources() {
5347            if let Some(texture) = Weak::upgrade(texture) {
5348                texture.destroy();
5349            }
5350        }
5351    }
5352
5353    pub(crate) fn new_usage_scope(&self) -> UsageScope<'_> {
5354        UsageScope::new_pooled(
5355            &self.usage_scopes,
5356            &self.tracker_indices,
5357            self.ordered_buffer_usages,
5358            self.ordered_texture_usages,
5359        )
5360    }
5361
5362    pub fn get_hal_counters(&self) -> wgt::HalCounters {
5363        self.raw().get_internal_counters()
5364    }
5365
5366    pub fn generate_allocator_report(&self) -> Option<wgt::AllocatorReport> {
5367        self.raw().generate_allocator_report()
5368    }
5369}
5370
5371crate::impl_resource_type!(Device);
5372crate::impl_labeled!(Device);
5373crate::impl_storage_item!(Device);