Skip to main content

wgpu_core/command/
render.rs

1use alloc::{borrow::Cow, boxed::Box, sync::Arc, vec::Vec};
2use core::{convert::Infallible, fmt, num::NonZeroU32, ops::Range, str};
3use smallvec::SmallVec;
4
5use arrayvec::ArrayVec;
6use thiserror::Error;
7use wgt::{
8    error::{ErrorType, WebGpuError},
9    BufferAddress, BufferUsages, Color, DynamicOffset, IndexFormat, InstanceFlags, TextureSelector,
10    TextureUsages, TextureViewDimension, VertexStepMode,
11};
12
13use crate::{
14    api_log,
15    binding_model::{BindError, BindGroup, ImmediateUploadError},
16    command::{
17        bind::Binder,
18        draw::validate_mesh_draw_multiview,
19        memory_init::{fixup_discarded_surfaces, SurfacesInDiscardState, TextureSurfaceDiscard},
20        pass::{self, flush_bindings_helper, ImmediateState},
21        pass_base, pass_try,
22        query::{
23            end_occlusion_query, end_pipeline_statistics_query, record_pass_timestamp_writes,
24            validate_and_begin_occlusion_query, validate_and_begin_pipeline_statistics_query,
25            QueryResetMap, QuerySetWrites,
26        },
27        render_command::ArcRenderCommand,
28        ArcCommand, BasePass, BindGroupStateChange, CommandBufferTextureMemoryActions,
29        CommandEncoder, CommandEncoderError, DebugGroupError, DrawCommandFamily, DrawError,
30        DrawKind, EncoderStateError, EncodingState, ExecutionError, InnerCommandEncoder,
31        MapPassErr, PassErrorScope, PassStateError, PassTimestampWrites, QueryUseError, Rect,
32        RenderBundle, RenderCommandError, StateChange, TimestampWritesError,
33    },
34    device::{
35        AttachmentData, Device, DeviceError, MissingDownlevelFlags, MissingFeatures,
36        RenderPassCompatibilityError, RenderPassContext,
37    },
38    hal_label, id, impl_resource_type,
39    init_tracker::{MemoryInitKind, TextureInitRange, TextureInitTrackerAction},
40    pipeline::{PipelineFlags, RenderPipeline, VertexStep},
41    resource::{
42        Buffer, DestroyedResourceError, InvalidOrDestroyedResourceError, InvalidResourceError,
43        Labeled, MissingBufferUsageError, MissingTextureUsageError, ParentDevice, QuerySet,
44        RawResourceAccess, ResourceErrorIdent, Texture, TextureView,
45        TextureViewNotRenderableReason,
46    },
47    snatch::SnatchGuard,
48    track::{ResourceUsageCompatibilityError, Tracker, UsageScope},
49    validation::{self, WorkgroupSizeCheck},
50    Label,
51};
52
53#[cfg(feature = "serde")]
54use serde::Deserialize;
55#[cfg(feature = "serde")]
56use serde::Serialize;
57
58pub use wgt::{LoadOp, StoreOp};
59
60fn load_hal_ops<V>(load: LoadOp<V>) -> hal::AttachmentOps {
61    match load {
62        LoadOp::Load => hal::AttachmentOps::LOAD,
63        LoadOp::Clear(_) => hal::AttachmentOps::LOAD_CLEAR,
64        LoadOp::DontCare(_) => hal::AttachmentOps::LOAD_DONT_CARE,
65    }
66}
67
68fn store_hal_ops(store: StoreOp) -> hal::AttachmentOps {
69    match store {
70        StoreOp::Store => hal::AttachmentOps::STORE,
71        StoreOp::Discard => hal::AttachmentOps::STORE_DISCARD,
72    }
73}
74
75// Stencil clear and reference value should take the LSBs.
76fn convert_stencil_value(value: u32, format: Option<wgt::TextureFormat>) -> u32 {
77    let Some(format) = format else {
78        return value;
79    };
80    let Some(stencil_format) = format.aspect_specific_format(wgt::TextureAspect::StencilOnly)
81    else {
82        return value;
83    };
84    // Currently only 8-bit stencil formats are supported
85    assert_eq!(stencil_format, wgt::TextureFormat::Stencil8);
86    value & 255
87}
88
89/// Describes an individual channel within a render pass, such as color, depth, or stencil.
90///
91/// A channel must either be read-only, or it must specify both load and store
92/// operations. See [`ResolvedPassChannel`] for a validated version.
93#[repr(C)]
94#[derive(Clone, Debug, Eq, PartialEq)]
95#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
96pub struct PassChannel<V> {
97    /// Operation to perform to the output attachment at the start of a
98    /// renderpass.
99    ///
100    /// This must be clear if it is the first renderpass rendering to a swap
101    /// chain image.
102    pub load_op: Option<LoadOp<V>>,
103    /// Operation to perform to the output attachment at the end of a renderpass.
104    pub store_op: Option<StoreOp>,
105    /// If true, the relevant channel is not changed by a renderpass, and the
106    /// corresponding attachment can be used inside the pass by other read-only
107    /// usages.
108    pub read_only: bool,
109}
110
111impl<V: Copy + Default> PassChannel<Option<V>> {
112    fn resolve(
113        &self,
114        instance_flags: InstanceFlags,
115        handle_clear: impl Fn(Option<V>) -> Result<V, AttachmentError>,
116    ) -> Result<ResolvedPassChannel<V>, AttachmentError> {
117        if self.read_only {
118            if self.load_op.is_some() {
119                return Err(AttachmentError::ReadOnlyWithLoad);
120            }
121            if self.store_op.is_some() {
122                return Err(AttachmentError::ReadOnlyWithStore);
123            }
124            Ok(ResolvedPassChannel::ReadOnly)
125        } else {
126            Ok(ResolvedPassChannel::Operational(wgt::Operations {
127                load: match self.load_op.ok_or(AttachmentError::NoLoad)? {
128                    LoadOp::Clear(clear_value) => LoadOp::Clear(handle_clear(clear_value)?),
129                    LoadOp::DontCare(token) => {
130                        if instance_flags.contains(InstanceFlags::STRICT_WEBGPU_COMPLIANCE) {
131                            return Err(AttachmentError::LoadOpDontCareUnderStrictWebgpuCompliance);
132                        }
133                        LoadOp::DontCare(token)
134                    }
135                    LoadOp::Load => LoadOp::Load,
136                },
137                store: self.store_op.ok_or(AttachmentError::NoStore)?,
138            }))
139        }
140    }
141}
142
143/// Describes an individual channel within a render pass, such as color, depth, or stencil.
144///
145/// Unlike [`PassChannel`], this version uses the Rust type system to guarantee
146/// a valid specification.
147#[derive(Clone, Debug)]
148#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
149pub enum ResolvedPassChannel<V> {
150    ReadOnly,
151    Operational(wgt::Operations<V>),
152}
153
154impl<V: Copy + Default> ResolvedPassChannel<V> {
155    fn load_op(&self) -> LoadOp<V> {
156        match self {
157            ResolvedPassChannel::ReadOnly => LoadOp::Load,
158            ResolvedPassChannel::Operational(wgt::Operations { load, .. }) => *load,
159        }
160    }
161
162    fn store_op(&self) -> StoreOp {
163        match self {
164            ResolvedPassChannel::ReadOnly => StoreOp::Store,
165            ResolvedPassChannel::Operational(wgt::Operations { store, .. }) => *store,
166        }
167    }
168
169    fn clear_value(&self) -> V {
170        match self {
171            Self::Operational(wgt::Operations {
172                load: LoadOp::Clear(clear_value),
173                ..
174            }) => *clear_value,
175            _ => Default::default(),
176        }
177    }
178
179    fn is_readonly(&self) -> bool {
180        matches!(self, Self::ReadOnly)
181    }
182
183    fn hal_ops(&self) -> hal::AttachmentOps {
184        load_hal_ops(self.load_op()) | store_hal_ops(self.store_op())
185    }
186}
187
188/// Describes a color attachment to a render pass.
189#[repr(C)]
190#[derive(Clone, Debug, PartialEq)]
191#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
192pub struct RenderPassColorAttachment<TV = Arc<TextureView>> {
193    /// The view to use as an attachment.
194    pub view: TV,
195    /// The depth slice index of a 3D view. It must not be provided if the view is not 3D.
196    pub depth_slice: Option<u32>,
197    /// The view that will receive the resolved output if multisampling is used.
198    pub resolve_target: Option<TV>,
199    /// Operation to perform to the output attachment at the start of a
200    /// renderpass.
201    ///
202    /// This must be clear if it is the first renderpass rendering to a swap
203    /// chain image.
204    pub load_op: LoadOp<Color>,
205    /// Operation to perform to the output attachment at the end of a renderpass.
206    pub store_op: StoreOp,
207}
208
209// Avoid allocation in the common case that there is only one color attachment,
210// but don't bloat `ArcCommand::RunRenderPass` excessively.
211pub type ColorAttachments<TV = Arc<TextureView>> =
212    SmallVec<[Option<RenderPassColorAttachment<TV>>; 1]>;
213
214impl RenderPassColorAttachment {
215    fn hal_ops(&self) -> hal::AttachmentOps {
216        load_hal_ops(self.load_op) | store_hal_ops(self.store_op)
217    }
218
219    fn clear_value(&self) -> Color {
220        match self.load_op {
221            LoadOp::Clear(clear_value) => clear_value,
222            LoadOp::DontCare(_) | LoadOp::Load => Color::default(),
223        }
224    }
225}
226
227/// Describes a depth/stencil attachment to a render pass.
228///
229/// This version uses the unvalidated [`PassChannel`].
230#[repr(C)]
231#[derive(Clone, Debug, PartialEq)]
232#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
233pub struct RenderPassDepthStencilAttachment<TV> {
234    /// The view to use as an attachment.
235    pub view: TV,
236    /// What operations will be performed on the depth part of the attachment.
237    pub depth: PassChannel<Option<f32>>,
238    /// What operations will be performed on the stencil part of the attachment.
239    pub stencil: PassChannel<Option<u32>>,
240}
241
242/// Describes a depth/stencil attachment to a render pass.
243///
244/// This version uses the validated [`ResolvedPassChannel`].
245#[derive(Clone, Debug)]
246#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
247pub struct ResolvedRenderPassDepthStencilAttachment<TV> {
248    /// The view to use as an attachment.
249    pub view: TV,
250    /// What operations will be performed on the depth part of the attachment.
251    pub depth: ResolvedPassChannel<f32>,
252    /// What operations will be performed on the stencil part of the attachment.
253    pub stencil: ResolvedPassChannel<u32>,
254}
255
256/// Describes the attachments of a render pass.
257#[derive(Clone, Default)]
258pub struct ResolvedRenderPassDescriptor<'a> {
259    pub label: Label<'a>,
260    /// The color attachments of the render pass.
261    pub color_attachments: Cow<'a, [Option<RenderPassColorAttachment<Arc<TextureView>>>]>,
262    /// The depth and stencil attachment of the render pass, if any.
263    pub depth_stencil_attachment: Option<RenderPassDepthStencilAttachment<Arc<TextureView>>>,
264    /// Defines where and when timestamp values will be written for this pass.
265    pub timestamp_writes: Option<PassTimestampWrites<Arc<QuerySet>>>,
266    /// Defines where the occlusion query results will be stored for this pass.
267    pub occlusion_query_set: Option<Arc<QuerySet>>,
268    /// The multiview array layers that will be used
269    pub multiview_mask: Option<NonZeroU32>,
270}
271
272/// Describes the attachments of a render pass.
273struct ArcRenderPassDescriptor<'a> {
274    pub label: &'a Label<'a>,
275    /// The color attachments of the render pass.
276    pub color_attachments:
277        ArrayVec<Option<RenderPassColorAttachment>, { hal::MAX_COLOR_ATTACHMENTS }>,
278    /// The depth and stencil attachment of the render pass, if any.
279    pub depth_stencil_attachment:
280        Option<ResolvedRenderPassDepthStencilAttachment<Arc<TextureView>>>,
281    /// Defines where and when timestamp values will be written for this pass.
282    pub timestamp_writes: Option<PassTimestampWrites>,
283    /// Defines where the occlusion query results will be stored for this pass.
284    pub occlusion_query_set: Option<Arc<QuerySet>>,
285    /// The multiview array layers that will be used
286    pub multiview_mask: Option<NonZeroU32>,
287}
288
289pub type RenderBasePass = BasePass<ArcRenderCommand, RenderPassError>;
290
291/// A pass's [encoder state](https://www.w3.org/TR/webgpu/#encoder-state) and
292/// its validity are two distinct conditions, i.e., the full matrix of
293/// (open, ended) x (valid, invalid) is possible.
294///
295/// The presence or absence of the `parent` `Option` indicates the pass's state.
296/// The presence or absence of an error in `base.error` indicates the pass's
297/// validity.
298pub struct RenderPass {
299    /// All pass data & records is stored here.
300    base: BasePass<ArcRenderCommand, RenderPassError>,
301
302    /// Parent command encoder that this pass records commands into.
303    ///
304    /// If this is `Some`, then the pass is in WebGPU's "open" state. If it is
305    /// `None`, then the pass is in the "ended" state.
306    /// See <https://www.w3.org/TR/webgpu/#encoder-state>
307    parent: Option<Arc<CommandEncoder>>,
308
309    device: Arc<Device>,
310
311    color_attachments: ArrayVec<Option<RenderPassColorAttachment>, { hal::MAX_COLOR_ATTACHMENTS }>,
312    depth_stencil_attachment: Option<ResolvedRenderPassDepthStencilAttachment<Arc<TextureView>>>,
313    timestamp_writes: Option<PassTimestampWrites>,
314    occlusion_query_set: Option<Arc<QuerySet>>,
315    multiview_mask: Option<NonZeroU32>,
316
317    // Resource binding dedupe state.
318    current_bind_groups: BindGroupStateChange,
319    current_pipeline: StateChange<Arc<RenderPipeline>>,
320}
321
322impl_resource_type!(RenderPass);
323
324impl crate::storage::StorageItem for RenderPass {
325    type Marker = id::markers::RenderPassEncoder;
326}
327
328impl RenderPass {
329    /// If the parent command encoder is invalid, the returned pass will be invalid.
330    fn new(parent: Arc<CommandEncoder>, desc: ArcRenderPassDescriptor) -> Self {
331        let ArcRenderPassDescriptor {
332            label,
333            timestamp_writes,
334            color_attachments,
335            depth_stencil_attachment,
336            occlusion_query_set,
337            multiview_mask,
338        } = desc;
339
340        Self {
341            base: BasePass::new(label),
342            device: parent.device().clone(),
343            parent: Some(parent),
344            color_attachments,
345            depth_stencil_attachment,
346            timestamp_writes,
347            occlusion_query_set,
348            multiview_mask,
349
350            current_bind_groups: BindGroupStateChange::new(),
351            current_pipeline: StateChange::new(),
352        }
353    }
354
355    fn new_invalid(parent: Arc<CommandEncoder>, label: &Label, err: RenderPassError) -> Self {
356        Self {
357            base: BasePass::new_invalid(label, err),
358            device: parent.device().clone(),
359            parent: Some(parent),
360            color_attachments: ArrayVec::new(),
361            depth_stencil_attachment: None,
362            timestamp_writes: None,
363            occlusion_query_set: None,
364            multiview_mask: None,
365            current_bind_groups: BindGroupStateChange::new(),
366            current_pipeline: StateChange::new(),
367        }
368    }
369
370    #[inline]
371    pub fn label(&self) -> Option<&str> {
372        self.base.label.as_deref()
373    }
374
375    pub fn device(&self) -> &Arc<Device> {
376        &self.device
377    }
378}
379
380impl fmt::Debug for RenderPass {
381    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
382        f.debug_struct("RenderPass")
383            .field("label", &self.label())
384            .field("color_attachments", &self.color_attachments)
385            .field("depth_stencil_target", &self.depth_stencil_attachment)
386            .field("command count", &self.base.commands.len())
387            .field("dynamic offset count", &self.base.dynamic_offsets.len())
388            .field("multiview mask", &self.multiview_mask)
389            .finish()
390    }
391}
392
393#[derive(Debug, PartialEq)]
394enum OptionalState {
395    Unused,
396    Required,
397    Set,
398}
399
400impl OptionalState {
401    fn require(&mut self, require: bool) {
402        if require && *self == Self::Unused {
403            *self = Self::Required;
404        }
405    }
406}
407
408#[derive(Debug, Default)]
409struct IndexState {
410    buffer_format: Option<IndexFormat>,
411    limit: u64,
412}
413
414impl IndexState {
415    fn update_buffer(&mut self, range: Range<BufferAddress>, format: IndexFormat) {
416        self.buffer_format = Some(format);
417        let shift = match format {
418            IndexFormat::Uint16 => 1,
419            IndexFormat::Uint32 => 2,
420        };
421        self.limit = (range.end - range.start) >> shift;
422    }
423
424    fn reset(&mut self) {
425        self.buffer_format = None;
426        self.limit = 0;
427    }
428}
429
430#[derive(Debug, Default)]
431pub(crate) struct VertexLimits {
432    /// Length of the shortest vertex rate vertex buffer
433    pub(crate) vertex_limit: u64,
434    /// Buffer slot which the shortest vertex rate vertex buffer is bound to
435    vertex_limit_slot: u32,
436    /// Length of the shortest instance rate vertex buffer
437    pub(crate) instance_limit: u64,
438    /// Buffer slot which the shortest instance rate vertex buffer is bound to
439    instance_limit_slot: u32,
440}
441
442impl VertexLimits {
443    pub(crate) fn new(
444        buffer_sizes: impl ExactSizeIterator<Item = Option<BufferAddress>>,
445        pipeline_steps: &[Option<VertexStep>],
446    ) -> Self {
447        // Implements the validation from https://gpuweb.github.io/gpuweb/#dom-gpurendercommandsmixin-draw
448        // Except that the formula is shuffled to extract the number of vertices in order
449        // to carry the bulk of the computation when changing states instead of when producing
450        // draws. Draw calls tend to happen at a higher frequency. Here we determine vertex
451        // limits that can be cheaply checked for each draw call.
452
453        let mut vertex_limit = u64::MAX;
454        let mut vertex_limit_slot = 0;
455        let mut instance_limit = u64::MAX;
456        let mut instance_limit_slot = 0;
457
458        for (idx, (buffer_size, step)) in buffer_sizes.zip(pipeline_steps).enumerate() {
459            let Some(step) = step else {
460                continue;
461            };
462
463            let Some(buffer_size) = buffer_size else {
464                // Missing required vertex buffer
465                return Self::default();
466            };
467
468            let limit = if buffer_size < step.last_stride {
469                // The buffer cannot fit the last vertex.
470                0
471            } else {
472                if step.stride == 0 {
473                    // We already checked that the last stride fits, the same
474                    // vertex will be repeated so this slot can accommodate any number of
475                    // vertices.
476                    continue;
477                }
478
479                // The general case.
480                (buffer_size - step.last_stride) / step.stride + 1
481            };
482
483            match step.mode {
484                VertexStepMode::Vertex => {
485                    if limit < vertex_limit {
486                        vertex_limit = limit;
487                        vertex_limit_slot = idx as _;
488                    }
489                }
490                VertexStepMode::Instance => {
491                    if limit < instance_limit {
492                        instance_limit = limit;
493                        instance_limit_slot = idx as _;
494                    }
495                }
496            }
497        }
498
499        Self {
500            vertex_limit,
501            vertex_limit_slot,
502            instance_limit,
503            instance_limit_slot,
504        }
505    }
506
507    pub(crate) fn validate_vertex_limit(
508        &self,
509        first_vertex: u32,
510        vertex_count: u32,
511    ) -> Result<(), DrawError> {
512        let last_vertex = first_vertex as u64 + vertex_count as u64;
513        let vertex_limit = self.vertex_limit;
514        if last_vertex > vertex_limit {
515            return Err(DrawError::VertexBeyondLimit {
516                last_vertex,
517                vertex_limit,
518                slot: self.vertex_limit_slot,
519            });
520        }
521
522        Ok(())
523    }
524
525    pub(crate) fn validate_instance_limit(
526        &self,
527        first_instance: u32,
528        instance_count: u32,
529    ) -> Result<(), DrawError> {
530        let last_instance = first_instance as u64 + instance_count as u64;
531        let instance_limit = self.instance_limit;
532        if last_instance > instance_limit {
533            return Err(DrawError::InstanceBeyondLimit {
534                last_instance,
535                instance_limit,
536                slot: self.instance_limit_slot,
537            });
538        }
539
540        Ok(())
541    }
542}
543
544/// State of a single vertex buffer slot.
545#[derive(Debug)]
546pub(crate) struct VertexSlot {
547    pub(crate) buffer: Arc<Buffer>,
548    pub(crate) range: Range<BufferAddress>,
549    pub(crate) is_dirty: bool,
550}
551
552/// Vertex buffer tracking state, shared between render passes and render bundles.
553///
554/// Tracks which vertex buffer slots are set, and caches the vertex and instance limits
555/// derived from those buffers and the current pipeline, avoiding recomputation on each draw.
556#[derive(Debug, Default)]
557pub(crate) struct VertexState {
558    slots: [Option<VertexSlot>; hal::MAX_VERTEX_BUFFERS],
559    pub(crate) limits: VertexLimits,
560}
561
562impl VertexState {
563    /// Set a vertex buffer slot, marking it dirty.
564    pub(crate) fn set_buffer(
565        &mut self,
566        slot: usize,
567        buffer: Arc<Buffer>,
568        range: Range<BufferAddress>,
569    ) {
570        self.slots[slot] = Some(VertexSlot {
571            buffer,
572            range,
573            is_dirty: true,
574        });
575    }
576
577    /// Clear a vertex buffer slot.
578    pub(crate) fn clear_buffer(&mut self, slot: usize) {
579        self.slots[slot] = None;
580    }
581
582    /// Recompute the cached vertex and instance limits based on the current slots and pipeline.
583    pub(crate) fn update_limits(&mut self, pipeline_steps: &[Option<VertexStep>]) {
584        self.limits = VertexLimits::new(
585            self.slots
586                .iter()
587                .map(|s| s.as_ref().map(|s| s.range.end - s.range.start)),
588            pipeline_steps,
589        );
590    }
591
592    fn last_assigned_index(&self) -> Option<usize> {
593        self.slots
594            .iter()
595            .enumerate()
596            .filter_map(|(i, s)| s.as_ref().map(|_| i))
597            .next_back()
598    }
599
600    pub(super) fn validate(
601        &self,
602        pipeline: &RenderPipeline,
603        binder: &Binder,
604    ) -> Result<(), DrawError> {
605        // Check all needed vertex buffers have been bound
606        for index in pipeline
607            .vertex_steps
608            .iter()
609            .enumerate()
610            .filter_map(|(index, step)| step.map(|_| index))
611        {
612            if self.slots[index].is_none() {
613                return Err(DrawError::MissingVertexBuffer {
614                    pipeline: pipeline.error_ident(),
615                    index,
616                });
617            }
618        }
619
620        let bind_group_space_used = binder.last_assigned_index().map_or(0, |i| i + 1);
621        let vertex_buffer_space_used = self.last_assigned_index().map_or(0, |i| i + 1);
622
623        let bind_groups_plus_vertex_buffers =
624            u32::try_from(bind_group_space_used + vertex_buffer_space_used).unwrap();
625        if bind_groups_plus_vertex_buffers
626            > pipeline.device.limits.max_bind_groups_plus_vertex_buffers
627        {
628            return Err(DrawError::TooManyBindGroupsPlusVertexBuffers {
629                given: bind_groups_plus_vertex_buffers,
630                limit: pipeline.device.limits.max_bind_groups_plus_vertex_buffers,
631            });
632        }
633
634        Ok(())
635    }
636
637    /// Call `f` for each dirty slot with `(slot_index, buffer, offset, size)` and mark them clean.
638    pub(crate) fn flush<F>(&mut self, mut f: F)
639    where
640        F: FnMut(u32, &Arc<Buffer>, BufferAddress, BufferAddress),
641    {
642        for (i, slot) in self.slots.iter_mut().enumerate() {
643            let Some(slot) = slot.as_mut() else { continue };
644            if !slot.is_dirty {
645                continue;
646            }
647            slot.is_dirty = false;
648            let size = slot.range.end - slot.range.start;
649            f(i as u32, &slot.buffer, slot.range.start, size);
650        }
651    }
652}
653
654struct State<'scope, 'snatch_guard, 'cmd_enc> {
655    pipeline_flags: PipelineFlags,
656    blend_constant: OptionalState,
657    stencil_reference: u32,
658    pipeline: Option<Arc<RenderPipeline>>,
659    index: IndexState,
660    vertex: VertexState,
661
662    info: RenderPassInfo,
663
664    pass: pass::PassState<'scope, 'snatch_guard, 'cmd_enc>,
665
666    active_occlusion_query: Option<(Arc<QuerySet>, u32)>,
667    active_pipeline_statistics_query: Option<(Arc<QuerySet>, u32)>,
668}
669
670impl<'scope, 'snatch_guard, 'cmd_enc> State<'scope, 'snatch_guard, 'cmd_enc> {
671    fn is_ready(&self, family: DrawCommandFamily) -> Result<(), DrawError> {
672        if let Some(pipeline) = self.pipeline.as_ref() {
673            self.pass.binder.check_compatibility(pipeline.as_ref())?;
674            self.pass.binder.check_late_buffer_bindings()?;
675
676            if self.blend_constant == OptionalState::Required {
677                return Err(DrawError::MissingBlendConstant);
678            }
679
680            self.vertex.validate(pipeline.as_ref(), &self.pass.binder)?;
681
682            if family == DrawCommandFamily::DrawIndexed {
683                // Pipeline expects an index buffer
684                // We have a buffer bound
685                let buffer_index_format = self
686                    .index
687                    .buffer_format
688                    .ok_or(DrawError::MissingIndexBuffer)?;
689
690                if pipeline.topology.is_strip()
691                    && pipeline.strip_index_format != Some(buffer_index_format)
692                {
693                    return Err(DrawError::UnmatchedStripIndexFormat {
694                        pipeline: pipeline.error_ident(),
695                        strip_index_format: pipeline.strip_index_format,
696                        buffer_format: buffer_index_format,
697                    });
698                }
699            }
700            if (family == DrawCommandFamily::DrawMeshTasks) != pipeline.is_mesh {
701                return Err(DrawError::WrongPipelineType {
702                    wanted_mesh_pipeline: !pipeline.is_mesh,
703                });
704            }
705            if !self
706                .pass
707                .immediate_state
708                .immediate_slots_set
709                .contains(pipeline.immediate_slots_required)
710            {
711                return Err(DrawError::MissingImmediateData {
712                    missing: pipeline
713                        .immediate_slots_required
714                        .difference(self.pass.immediate_state.immediate_slots_set),
715                });
716            }
717            Ok(())
718        } else {
719            Err(DrawError::MissingPipeline(pass::MissingPipeline))
720        }
721    }
722
723    fn flush_immediates(&mut self) {
724        let pipeline = self.pipeline.as_ref().unwrap();
725        let layout = pipeline.layout().unwrap();
726        self.pass
727            .immediate_state
728            .flush_immediates(layout, self.pass.base.raw_encoder);
729    }
730
731    /// Flush binding state in preparation for a draw call.
732    ///
733    /// See the compute pass version for an explanation of some ways that
734    /// `flush_bindings` differs between the two types of passes.
735    fn flush_bindings(&mut self) -> Result<(), RenderPassErrorInner> {
736        flush_bindings_helper(&mut self.pass)?;
737        Ok(())
738    }
739
740    /// Reset the `RenderBundle`-related states.
741    fn reset_bundle(&mut self) {
742        self.pass.binder.reset();
743        self.pipeline = None;
744        self.index.reset();
745        self.vertex = Default::default();
746        self.pass.immediate_state.immediate_slots_set = Default::default();
747    }
748
749    /// Flush dirty vertex buffer slots to the HAL encoder in preparation for a draw call.
750    fn flush_vertex_buffers(&mut self) -> Result<(), RenderPassErrorInner> {
751        let vertex = &mut self.vertex;
752        let raw_encoder: &mut dyn hal::DynCommandEncoder = self.pass.base.raw_encoder;
753        let snatch_guard = self.pass.base.snatch_guard;
754        let mut result = Ok(());
755        vertex.flush(|slot, buffer, offset, size| {
756            if result.is_err() {
757                return;
758            }
759            match buffer.try_raw(snatch_guard) {
760                Ok(raw) => unsafe {
761                    // SAFETY: The offset and size were validated in set_vertex_buffer.
762                    raw_encoder.set_vertex_buffer(
763                        slot,
764                        hal::BufferBinding::new_unchecked(raw, offset, size),
765                    );
766                },
767                Err(e) => result = Err(e.into()),
768            }
769        });
770        result
771    }
772}
773
774/// Describes an attachment location in words.
775///
776/// Can be used as "the {loc} has..." or "{loc} has..."
777#[derive(Debug, Copy, Clone)]
778pub enum AttachmentErrorLocation {
779    Color { index: usize, resolve: bool },
780    Depth,
781}
782
783impl fmt::Display for AttachmentErrorLocation {
784    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
785        match *self {
786            AttachmentErrorLocation::Color {
787                index,
788                resolve: false,
789            } => write!(f, "color attachment at index {index}'s texture view"),
790            AttachmentErrorLocation::Color {
791                index,
792                resolve: true,
793            } => write!(
794                f,
795                "color attachment at index {index}'s resolve texture view"
796            ),
797            AttachmentErrorLocation::Depth => write!(f, "depth attachment's texture view"),
798        }
799    }
800}
801
802#[derive(Clone, Debug, Error)]
803#[non_exhaustive]
804pub enum ColorAttachmentError {
805    #[error("Attachment format {0:?} is not a color format")]
806    InvalidFormat(wgt::TextureFormat),
807    #[error("The number of color attachments {given} exceeds the limit {limit}")]
808    TooMany { given: usize, limit: usize },
809    #[error("The total number of bytes per sample in color attachments {total} exceeds the limit {limit}")]
810    TooManyBytesPerSample { total: u32, limit: u32 },
811    #[error("Depth slice must be less than {limit} but is {given}")]
812    DepthSliceLimit { given: u32, limit: u32 },
813    #[error("Color attachment's view is 3D and requires depth slice to be provided")]
814    MissingDepthSlice,
815    #[error("Depth slice was provided but the color attachment's view is not 3D")]
816    UnneededDepthSlice,
817    #[error("{view}'s subresource at mip {mip_level} and depth/array layer {depth_or_array_layer} is already attached to this render pass")]
818    SubresourceOverlap {
819        view: ResourceErrorIdent,
820        mip_level: u32,
821        depth_or_array_layer: u32,
822    },
823    #[error(
824        "Color attachment with `TRANSIENT_ATTACHMENT` usage can only be used with \
825        `LoadOp::Clear` or `LoadOp::DontCare` (if it is available) and  `StoreOp::Discard`. Operations `{0:?}` were provided"
826    )]
827    InvalidTransientAttachmentOp((LoadOp<Color>, StoreOp)),
828    #[error("Color attachment's load op is `LoadOp::DontCare` but `InstanceFlags::STRICT_WEBGPU_COMPLIANCE` is set")]
829    LoadOpDontCareUnderStrictWebgpuCompliance,
830}
831
832impl WebGpuError for ColorAttachmentError {
833    fn webgpu_error_type(&self) -> ErrorType {
834        ErrorType::Validation
835    }
836}
837
838#[derive(Clone, Debug, Error)]
839#[non_exhaustive]
840pub enum AttachmentError {
841    #[error("The format of the depth-stencil attachment ({0:?}) is not a depth-or-stencil format")]
842    InvalidDepthStencilAttachmentFormat(wgt::TextureFormat),
843    #[error(
844        "Depth attachment with `TRANSIENT_ATTACHMENT` usage can only be used with \
845        `LoadOp::Clear` or `LoadOp::DontCare` (if it is available) and  `StoreOp::Discard`. Operations `{0:?}` were provided"
846    )]
847    InvalidTransientDepthAttachmentOps((LoadOp<Option<f32>>, StoreOp)),
848    #[error("Depth attachment with `TRANSIENT_ATTACHMENT` usage cannot be read-only")]
849    ReadOnlyTransientDepthAttachment,
850    #[error(
851        "Stencil attachment with `TRANSIENT_ATTACHMENT` usage can only be used with \
852        `LoadOp::Clear` or `LoadOp::DontCare` (if it is available) and  `StoreOp::Discard`. Operations `{0:?}` were provided"
853    )]
854    InvalidTransientStencilAttachmentOps((LoadOp<Option<u32>>, StoreOp)),
855    #[error("Stencil attachment with `TRANSIENT_ATTACHMENT` usage cannot be read-only")]
856    ReadOnlyTransientStencilAttachment,
857    #[error("LoadOp must be None for read-only attachments")]
858    ReadOnlyWithLoad,
859    #[error("StoreOp must be None for read-only attachments")]
860    ReadOnlyWithStore,
861    #[error("Depth `LoadOp` and `StoreOp` (`{ops:?}`) must be `None` for attachments (`{format:?}`) without depth aspect")]
862    DepthOpsWithoutAspect {
863        format: wgt::TextureFormat,
864        ops: (Option<LoadOp<Option<f32>>>, Option<StoreOp>),
865    },
866    #[error("Stencil `LoadOp` and `StoreOp` (`{ops:?}`) must be `None` for attachments (`{format:?}`) without stencil aspect")]
867    StencilOpsWithoutAspect {
868        format: wgt::TextureFormat,
869        ops: (Option<LoadOp<Option<u32>>>, Option<StoreOp>),
870    },
871    #[error("Attachment without load")]
872    NoLoad,
873    #[error("Attachment without store")]
874    NoStore,
875    #[error("LoadOp is `Clear` but no clear value was provided")]
876    NoClearValue,
877    #[error("Clear value ({0}) must be between 0.0 and 1.0, inclusive")]
878    ClearValueOutOfRange(f32),
879    #[error("Load op is `DontCare` but `InstanceFlags::STRICT_WEBGPU_COMPLIANCE` is set")]
880    LoadOpDontCareUnderStrictWebgpuCompliance,
881}
882
883impl WebGpuError for AttachmentError {
884    fn webgpu_error_type(&self) -> ErrorType {
885        ErrorType::Validation
886    }
887}
888
889/// Error encountered when performing a render pass.
890#[derive(Clone, Debug, Error)]
891pub enum RenderPassErrorInner {
892    #[error(transparent)]
893    Device(#[from] DeviceError),
894    #[error(transparent)]
895    ColorAttachment(#[from] ColorAttachmentError),
896    #[error(transparent)]
897    InvalidAttachment(#[from] AttachmentError),
898    #[error(transparent)]
899    EncoderState(#[from] EncoderStateError),
900    #[error("Parent encoder is invalid")]
901    InvalidParentEncoder,
902    #[error(transparent)]
903    DebugGroupError(#[from] DebugGroupError),
904    #[error("The format of the {location} ({format:?}) is not resolvable")]
905    UnsupportedResolveTargetFormat {
906        location: AttachmentErrorLocation,
907        format: wgt::TextureFormat,
908    },
909    #[error("The {location} is not valid, because the texture has `TRANSIENT_ATTACHMENT` usage")]
910    InvalidTransientResolveTarget { location: AttachmentErrorLocation },
911    #[error("No color attachments or depth attachments were provided, at least one attachment of any kind must be provided")]
912    MissingAttachments,
913    #[error("The {location} is not renderable:")]
914    TextureViewIsNotRenderable {
915        location: AttachmentErrorLocation,
916        #[source]
917        reason: TextureViewNotRenderableReason,
918    },
919    #[error("Attachments have differing sizes: the {expected_location} has extent {expected_extent:?} but is followed by the {actual_location} which has {actual_extent:?}")]
920    AttachmentsDimensionMismatch {
921        expected_location: AttachmentErrorLocation,
922        expected_extent: wgt::Extent3d,
923        actual_location: AttachmentErrorLocation,
924        actual_extent: wgt::Extent3d,
925    },
926    #[error("Attachments have differing sample counts: the {expected_location} has count {expected_samples:?} but is followed by the {actual_location} which has count {actual_samples:?}")]
927    AttachmentSampleCountMismatch {
928        expected_location: AttachmentErrorLocation,
929        expected_samples: u32,
930        actual_location: AttachmentErrorLocation,
931        actual_samples: u32,
932    },
933    #[error("The resolve source, {location}, must be multi-sampled (has {src} samples) while the resolve destination must not be multisampled (has {dst} samples)")]
934    InvalidResolveSampleCounts {
935        location: AttachmentErrorLocation,
936        src: u32,
937        dst: u32,
938    },
939    #[error(
940        "Resource source, {location}, format ({src:?}) must match the resolve destination format ({dst:?})"
941    )]
942    MismatchedResolveTextureFormat {
943        location: AttachmentErrorLocation,
944        src: wgt::TextureFormat,
945        dst: wgt::TextureFormat,
946    },
947    #[error("Unable to clear non-present/read-only depth")]
948    InvalidDepthOps,
949    #[error("Unable to clear non-present/read-only stencil")]
950    InvalidStencilOps,
951    #[error(transparent)]
952    MissingFeatures(#[from] MissingFeatures),
953    #[error(transparent)]
954    MissingDownlevelFlags(#[from] MissingDownlevelFlags),
955    #[error("Indirect draw count of {count_bytes} bytes starting at {begin_count_offset} would overrun buffer of size {count_buffer_size}")]
956    IndirectCountBufferOverrun {
957        count_bytes: u64,
958        begin_count_offset: u64,
959        count_buffer_size: u64,
960    },
961    #[error(transparent)]
962    ResourceUsageCompatibility(#[from] ResourceUsageCompatibilityError),
963    #[error("Render bundle has incompatible targets, {0}")]
964    IncompatibleBundleTargets(#[from] RenderPassCompatibilityError),
965    #[error(
966        "Render bundle has incompatible read-only flags: \
967             bundle has flags depth = {bundle_depth} and stencil = {bundle_stencil}, \
968             while the pass has flags depth = {pass_depth} and stencil = {pass_stencil}. \
969             Read-only renderpasses are only compatible with read-only bundles for that aspect."
970    )]
971    IncompatibleBundleReadOnlyDepthStencil {
972        pass_depth: bool,
973        pass_stencil: bool,
974        bundle_depth: bool,
975        bundle_stencil: bool,
976    },
977    #[error(transparent)]
978    RenderCommand(#[from] RenderCommandError),
979    #[error(transparent)]
980    Draw(#[from] DrawError),
981    #[error(transparent)]
982    Bind(#[from] BindError),
983    #[error(transparent)]
984    QueryUse(#[from] QueryUseError),
985    #[error("Multiview layer count must match")]
986    MultiViewMismatch,
987    #[error(
988        "Multiview pass texture views with more than one array layer must have D2Array dimension"
989    )]
990    MultiViewDimensionMismatch,
991    #[error("Multiview view count limit violated")]
992    TooManyMultiviewViews,
993    #[error("missing occlusion query set")]
994    MissingOcclusionQuerySet,
995    #[error(transparent)]
996    DestroyedResource(#[from] DestroyedResourceError),
997    #[error("The compute pass has already been ended and no further commands can be recorded")]
998    PassEnded,
999    #[error(transparent)]
1000    InvalidResource(#[from] InvalidResourceError),
1001    #[error(transparent)]
1002    TimestampWrites(#[from] TimestampWritesError),
1003}
1004
1005impl From<InvalidOrDestroyedResourceError> for RenderPassErrorInner {
1006    fn from(error: InvalidOrDestroyedResourceError) -> Self {
1007        match error {
1008            InvalidOrDestroyedResourceError::InvalidResource(e) => Self::InvalidResource(e),
1009            InvalidOrDestroyedResourceError::DestroyedResource(e) => Self::DestroyedResource(e),
1010        }
1011    }
1012}
1013
1014impl From<MissingBufferUsageError> for RenderPassErrorInner {
1015    fn from(error: MissingBufferUsageError) -> Self {
1016        Self::RenderCommand(error.into())
1017    }
1018}
1019
1020impl From<MissingTextureUsageError> for RenderPassErrorInner {
1021    fn from(error: MissingTextureUsageError) -> Self {
1022        Self::RenderCommand(error.into())
1023    }
1024}
1025
1026impl From<pass::BindGroupIndexOutOfRange> for RenderPassErrorInner {
1027    fn from(error: pass::BindGroupIndexOutOfRange) -> Self {
1028        Self::RenderCommand(RenderCommandError::BindGroupIndexOutOfRange(error))
1029    }
1030}
1031
1032impl From<pass::MissingPipeline> for RenderPassErrorInner {
1033    fn from(error: pass::MissingPipeline) -> Self {
1034        Self::Draw(DrawError::MissingPipeline(error))
1035    }
1036}
1037
1038impl From<ImmediateUploadError> for RenderPassErrorInner {
1039    fn from(error: ImmediateUploadError) -> Self {
1040        Self::RenderCommand(error.into())
1041    }
1042}
1043
1044/// Error encountered when performing a render pass.
1045#[derive(Clone, Debug, Error)]
1046#[error("{scope}")]
1047pub struct RenderPassError {
1048    pub scope: PassErrorScope,
1049    #[source]
1050    pub(super) inner: Box<RenderPassErrorInner>,
1051}
1052
1053impl<E: Into<RenderPassErrorInner>> MapPassErr<RenderPassError> for E {
1054    fn map_pass_err(self, scope: PassErrorScope) -> RenderPassError {
1055        RenderPassError {
1056            scope,
1057            inner: Box::new(self.into()),
1058        }
1059    }
1060}
1061
1062impl WebGpuError for RenderPassError {
1063    fn webgpu_error_type(&self) -> ErrorType {
1064        match self.inner.as_ref() {
1065            RenderPassErrorInner::Device(e) => e.webgpu_error_type(),
1066            RenderPassErrorInner::ColorAttachment(e) => e.webgpu_error_type(),
1067            RenderPassErrorInner::EncoderState(e) => e.webgpu_error_type(),
1068            RenderPassErrorInner::DebugGroupError(e) => e.webgpu_error_type(),
1069            RenderPassErrorInner::MissingFeatures(e) => e.webgpu_error_type(),
1070            RenderPassErrorInner::MissingDownlevelFlags(e) => e.webgpu_error_type(),
1071            RenderPassErrorInner::RenderCommand(e) => e.webgpu_error_type(),
1072            RenderPassErrorInner::Draw(e) => e.webgpu_error_type(),
1073            RenderPassErrorInner::Bind(e) => e.webgpu_error_type(),
1074            RenderPassErrorInner::QueryUse(e) => e.webgpu_error_type(),
1075            RenderPassErrorInner::DestroyedResource(e) => e.webgpu_error_type(),
1076            RenderPassErrorInner::InvalidResource(e) => e.webgpu_error_type(),
1077            RenderPassErrorInner::IncompatibleBundleTargets(e) => e.webgpu_error_type(),
1078            RenderPassErrorInner::InvalidAttachment(e) => e.webgpu_error_type(),
1079            RenderPassErrorInner::TimestampWrites(e) => e.webgpu_error_type(),
1080
1081            RenderPassErrorInner::InvalidParentEncoder
1082            | RenderPassErrorInner::UnsupportedResolveTargetFormat { .. }
1083            | RenderPassErrorInner::InvalidTransientResolveTarget { .. }
1084            | RenderPassErrorInner::MissingAttachments
1085            | RenderPassErrorInner::TextureViewIsNotRenderable { .. }
1086            | RenderPassErrorInner::AttachmentsDimensionMismatch { .. }
1087            | RenderPassErrorInner::AttachmentSampleCountMismatch { .. }
1088            | RenderPassErrorInner::InvalidResolveSampleCounts { .. }
1089            | RenderPassErrorInner::MismatchedResolveTextureFormat { .. }
1090            | RenderPassErrorInner::InvalidDepthOps
1091            | RenderPassErrorInner::InvalidStencilOps
1092            | RenderPassErrorInner::IndirectCountBufferOverrun { .. }
1093            | RenderPassErrorInner::ResourceUsageCompatibility(..)
1094            | RenderPassErrorInner::IncompatibleBundleReadOnlyDepthStencil { .. }
1095            | RenderPassErrorInner::MultiViewMismatch
1096            | RenderPassErrorInner::MultiViewDimensionMismatch
1097            | RenderPassErrorInner::TooManyMultiviewViews
1098            | RenderPassErrorInner::MissingOcclusionQuerySet
1099            | RenderPassErrorInner::PassEnded => ErrorType::Validation,
1100        }
1101    }
1102}
1103
1104struct RenderAttachment {
1105    texture: Arc<Texture>,
1106    selector: TextureSelector,
1107    usage: wgt::TextureUses,
1108}
1109
1110impl TextureView {
1111    fn to_render_attachment(&self, usage: wgt::TextureUses) -> RenderAttachment {
1112        RenderAttachment {
1113            texture: self.parent.clone(),
1114            selector: self.selector.clone(),
1115            usage,
1116        }
1117    }
1118}
1119
1120const MAX_TOTAL_ATTACHMENTS: usize = hal::MAX_COLOR_ATTACHMENTS + hal::MAX_COLOR_ATTACHMENTS + 1;
1121type AttachmentDataVec<T> = ArrayVec<T, MAX_TOTAL_ATTACHMENTS>;
1122
1123struct RenderPassInfo {
1124    context: RenderPassContext,
1125    /// All render attachments, including depth/stencil
1126    render_attachments: AttachmentDataVec<RenderAttachment>,
1127    is_depth_read_only: bool,
1128    is_stencil_read_only: bool,
1129    extent: wgt::Extent3d,
1130
1131    divergent_discarded_depth_stencil_aspect: Option<(wgt::TextureAspect, Arc<TextureView>)>,
1132    multiview_mask: Option<NonZeroU32>,
1133}
1134
1135impl RenderPassInfo {
1136    fn add_pass_texture_init_actions<V>(
1137        load_op: LoadOp<V>,
1138        store_op: StoreOp,
1139        texture_memory_actions: &mut CommandBufferTextureMemoryActions,
1140        view: &TextureView,
1141        depth_slice: Option<u32>,
1142        pending_discard_init_fixups: &mut SurfacesInDiscardState,
1143    ) {
1144        match (
1145            depth_slice,
1146            view.parent.desc.dimension == wgt::TextureDimension::D3,
1147        ) {
1148            (Some(_), true) | (None, false) => {}
1149            _ => unreachable!("3D textures, but not any other kind, must specify a depth slice"),
1150        }
1151        // 3D textures with more than one depth slice are a special case,
1152        // because we don't track initialization status per slice.
1153        let partial_z = view.parent.desc.dimension == wgt::TextureDimension::D3
1154            && view
1155                .parent
1156                .desc
1157                .mip_level_size(view.selector.mips.start)
1158                .is_some_and(|dim| dim.depth_or_array_layers > 1);
1159
1160        if matches!(load_op, LoadOp::Load) {
1161            // Record the initialization action for the texture. Simultaneously, collect a
1162            // list of any ranges of the texture that were discarded within the current
1163            // command buffer, for initialization by `fixup_discarded_surfaces`.
1164            // (The analogous case for texture copies is in `handle_texture_init`.)
1165            //
1166            // Even when the target is a depth slice of a 3D texture, this is an action
1167            // for the entire mip. In most cases that is what we want (if there is any
1168            // live data in the mip at the end of a command buffer, we must have the
1169            // entire thing initialized). The exception is Load+Discard of one slice in an
1170            // uninitialized mip. In that case, we could initialize only one slice, and then
1171            // leave the entire mip uninitialized at the end of the command buffer. But we
1172            // don't optimize that, because it is a lot of complexity for a case that
1173            // doesn't seem very useful (if the render targets are only used transiently,
1174            // why is it important that they are volume slices?).
1175            pending_discard_init_fixups.extend(texture_memory_actions.register_init_action(
1176                &TextureInitTrackerAction {
1177                    texture: view.parent.clone(),
1178                    range: TextureInitRange::from(view),
1179                    // Note that this is needed for `Load` even if the target has `StoreOp::Discard`.
1180                    kind: MemoryInitKind::NeedsInitializedMemory,
1181                },
1182                depth_slice.map(|slice| slice..slice + 1),
1183            ));
1184        } else if store_op == StoreOp::Store {
1185            if partial_z {
1186                // We must initialize the entire mip due to init tracking granularity.
1187                pending_discard_init_fixups.extend(texture_memory_actions.register_init_action(
1188                    &TextureInitTrackerAction {
1189                        texture: view.parent.clone(),
1190                        range: TextureInitRange::from(view),
1191                        kind: MemoryInitKind::NeedsInitializedMemory,
1192                    },
1193                    depth_slice.map(|slice| slice..slice + 1),
1194                ));
1195            } else {
1196                // Clear + Store
1197                texture_memory_actions
1198                    .register_implicit_init(&view.parent, TextureInitRange::from(view));
1199            }
1200        }
1201        if store_op == StoreOp::Discard {
1202            // The discard happens at the *end* of a pass, but recording the
1203            // discard right away is fine, since attachments can't be used
1204            // for any other purpose during the pass.
1205            //
1206            // Discards are usually deferred as long as possible (i.e. until the
1207            // next use of the subresource). But discarded depth slices of 3D
1208            // textures will be reinitialized at the end of the command buffer
1209            // (by [`BakedCommands::initialize_discarded_depth_slices`]),
1210            // because the primary init tracker does not track individual slices.
1211            texture_memory_actions.discard(TextureSurfaceDiscard {
1212                texture: view.parent.clone(),
1213                mip_level: view.selector.mips.start,
1214                layer_or_depth_slice: depth_slice.unwrap_or(view.selector.layers.start),
1215            });
1216        }
1217    }
1218
1219    fn start(
1220        device: &Arc<Device>,
1221        hal_label: Option<&str>,
1222        color_attachments: &[Option<RenderPassColorAttachment>],
1223        mut depth_stencil_attachment: Option<
1224            ResolvedRenderPassDepthStencilAttachment<Arc<TextureView>>,
1225        >,
1226        mut timestamp_writes: Option<PassTimestampWrites>,
1227        mut occlusion_query_set: Option<Arc<QuerySet>>,
1228        encoder: &mut dyn hal::DynCommandEncoder,
1229        trackers: &mut Tracker,
1230        texture_memory_actions: &mut CommandBufferTextureMemoryActions,
1231        pending_query_resets: &mut QueryResetMap,
1232        pending_discard_init_fixups: &mut SurfacesInDiscardState,
1233        snatch_guard: &SnatchGuard<'_>,
1234        query_set_writes: &mut QuerySetWrites,
1235        multiview_mask: Option<NonZeroU32>,
1236    ) -> Result<Self, RenderPassErrorInner> {
1237        profiling::scope!("RenderPassInfo::start");
1238
1239        // We default to false intentionally, even if depth-stencil isn't used at all.
1240        // This allows us to use the primary raw pipeline in `RenderPipeline`,
1241        // instead of the special read-only one, which would be `None`.
1242        let mut is_depth_read_only = false;
1243        let mut is_stencil_read_only = false;
1244
1245        let mut render_attachments = AttachmentDataVec::<RenderAttachment>::new();
1246        let mut divergent_discarded_depth_stencil_aspect = None;
1247
1248        let mut attachment_location = AttachmentErrorLocation::Color {
1249            index: usize::MAX,
1250            resolve: false,
1251        };
1252        let mut extent = None;
1253        let mut sample_count = 0;
1254
1255        let mut detected_multiview: Option<Option<NonZeroU32>> = None;
1256
1257        let mut check_multiview = |view: &TextureView| {
1258            // Get the multiview configuration for this texture view
1259            let layers = view.selector.layers.end - view.selector.layers.start;
1260            let this_multiview = if layers >= 2 {
1261                // Trivially proven by the if above
1262                Some(unsafe { NonZeroU32::new_unchecked(layers) })
1263            } else {
1264                None
1265            };
1266
1267            // Make sure that if this view is a multiview, it is set to be an array
1268            if this_multiview.is_some() && view.desc.dimension != TextureViewDimension::D2Array {
1269                return Err(RenderPassErrorInner::MultiViewDimensionMismatch);
1270            }
1271
1272            // Validate matching first, or store the first one
1273            if let Some(multiview) = detected_multiview {
1274                if multiview != this_multiview {
1275                    return Err(RenderPassErrorInner::MultiViewMismatch);
1276                }
1277            } else {
1278                // Multiview is only supported if the feature is enabled
1279                if let Some(this_multiview) = this_multiview {
1280                    device.require_features(wgt::Features::MULTIVIEW)?;
1281                    if this_multiview.get() > device.limits.max_multiview_view_count {
1282                        return Err(RenderPassErrorInner::TooManyMultiviewViews);
1283                    }
1284                }
1285
1286                detected_multiview = Some(this_multiview);
1287            }
1288
1289            Ok(())
1290        };
1291        let mut add_view = |view: &TextureView, location| {
1292            let render_extent = view.state()?.render_extent.map_err(|reason| {
1293                RenderPassErrorInner::TextureViewIsNotRenderable { location, reason }
1294            })?;
1295            if let Some(ex) = extent {
1296                if ex != render_extent {
1297                    return Err(RenderPassErrorInner::AttachmentsDimensionMismatch {
1298                        expected_location: attachment_location,
1299                        expected_extent: ex,
1300                        actual_location: location,
1301                        actual_extent: render_extent,
1302                    });
1303                }
1304            } else {
1305                extent = Some(render_extent);
1306            }
1307            if sample_count == 0 {
1308                sample_count = view.samples;
1309            } else if sample_count != view.samples {
1310                return Err(RenderPassErrorInner::AttachmentSampleCountMismatch {
1311                    expected_location: attachment_location,
1312                    expected_samples: sample_count,
1313                    actual_location: location,
1314                    actual_samples: view.samples,
1315                });
1316            }
1317            attachment_location = location;
1318            Ok(())
1319        };
1320
1321        let mut depth_stencil = None;
1322
1323        if let Some(at) = depth_stencil_attachment.as_ref() {
1324            let view = &at.view;
1325            check_multiview(view)?;
1326            add_view(view, AttachmentErrorLocation::Depth)?;
1327
1328            let ds_aspects = view.desc.aspects();
1329
1330            if !ds_aspects.contains(hal::FormatAspects::STENCIL)
1331                || (at.stencil.load_op().eq_variant(at.depth.load_op())
1332                    && at.stencil.store_op() == at.depth.store_op())
1333            {
1334                Self::add_pass_texture_init_actions(
1335                    at.depth.load_op(),
1336                    at.depth.store_op(),
1337                    texture_memory_actions,
1338                    view,
1339                    None,
1340                    pending_discard_init_fixups,
1341                );
1342            } else if !ds_aspects.contains(hal::FormatAspects::DEPTH) {
1343                Self::add_pass_texture_init_actions(
1344                    at.stencil.load_op(),
1345                    at.stencil.store_op(),
1346                    texture_memory_actions,
1347                    view,
1348                    None,
1349                    pending_discard_init_fixups,
1350                );
1351            } else {
1352                // This is the only place (anywhere in wgpu) where Stencil &
1353                // Depth init state can diverge.
1354                //
1355                // To save us the overhead of tracking init state of texture
1356                // aspects everywhere, we're going to cheat a little bit in
1357                // order to keep the init state of both Stencil and Depth
1358                // aspects in sync. The expectation is that we hit this path
1359                // extremely rarely!
1360                //
1361                // Diverging LoadOp, i.e. Load + Clear:
1362                //
1363                // Record MemoryInitKind::NeedsInitializedMemory for the entire
1364                // surface, a bit wasteful due to redundancy with Clear, but no
1365                // negative effect!
1366                //
1367                // Rationale: If the loaded channel is uninitialized it needs
1368                // clearing, the cleared channel doesn't care. (If everything is
1369                // already initialized nothing special happens)
1370                //
1371                // (possible minor optimization: Clear caused by
1372                // NeedsInitializedMemory should know that it doesn't need to
1373                // clear the aspect that was set to C)
1374                let need_init_beforehand =
1375                    at.depth.load_op() == LoadOp::Load || at.stencil.load_op() == LoadOp::Load;
1376                if need_init_beforehand {
1377                    pending_discard_init_fixups.extend(
1378                        texture_memory_actions.register_init_action(
1379                            &TextureInitTrackerAction {
1380                                texture: view.parent.clone(),
1381                                range: TextureInitRange::from(view.as_ref()),
1382                                kind: MemoryInitKind::NeedsInitializedMemory,
1383                            },
1384                            None, // depth/stencil textures are never 3D
1385                        ),
1386                    );
1387                }
1388
1389                // Diverging Store, i.e. Discard + Store:
1390                //
1391                // Immediately zero out channel that is set to discard after
1392                // we're done with the render pass. This allows us to set the
1393                // entire surface to MemoryInitKind::ImplicitlyInitialized (if
1394                // it isn't already set to NeedsInitializedMemory).
1395                //
1396                // (possible optimization: Delay and potentially drop this zeroing)
1397                if at.depth.store_op() != at.stencil.store_op() {
1398                    if !need_init_beforehand {
1399                        texture_memory_actions.register_implicit_init(
1400                            &view.parent,
1401                            TextureInitRange::from(view.as_ref()),
1402                        );
1403                    }
1404                    divergent_discarded_depth_stencil_aspect = Some((
1405                        if at.depth.store_op() == StoreOp::Discard {
1406                            wgt::TextureAspect::DepthOnly
1407                        } else {
1408                            wgt::TextureAspect::StencilOnly
1409                        },
1410                        view.clone(),
1411                    ));
1412                } else if at.depth.store_op() == StoreOp::Discard {
1413                    // Both are discarded using the regular path.
1414                    texture_memory_actions.discard(TextureSurfaceDiscard {
1415                        texture: view.parent.clone(),
1416                        mip_level: view.selector.mips.start,
1417                        layer_or_depth_slice: view.selector.layers.start,
1418                    });
1419                }
1420            }
1421
1422            is_depth_read_only = at.depth.is_readonly();
1423            is_stencil_read_only = at.stencil.is_readonly();
1424
1425            let usage = 'b: {
1426                if device
1427                    .downlevel
1428                    .flags
1429                    .contains(wgt::DownlevelFlags::READ_ONLY_DEPTH_STENCIL)
1430                {
1431                    let depth_stencil_uses = match (is_depth_read_only, is_stencil_read_only) {
1432                        (true, true) => {
1433                            wgt::TextureUses::DEPTH_READ | wgt::TextureUses::STENCIL_READ
1434                        }
1435                        (true, false) => {
1436                            wgt::TextureUses::DEPTH_READ | wgt::TextureUses::STENCIL_WRITE
1437                        }
1438                        (false, true) => {
1439                            wgt::TextureUses::DEPTH_WRITE | wgt::TextureUses::STENCIL_READ
1440                        }
1441                        (false, false) => {
1442                            break 'b wgt::TextureUses::DEPTH_WRITE
1443                                | wgt::TextureUses::STENCIL_WRITE;
1444                        }
1445                    };
1446                    // If the texture supports TEXTURE_BINDING, it can be used as a shader
1447                    // resource and a read-only depth attachment simultaneously. But if it
1448                    // doesn't support TEXTURE_BINDING, don't attempt to transition it to a
1449                    // shader resource state, because DX12 will raise an error.
1450                    if view.desc.usage.contains(TextureUsages::TEXTURE_BINDING) {
1451                        depth_stencil_uses | wgt::TextureUses::RESOURCE
1452                    } else {
1453                        depth_stencil_uses
1454                    }
1455                } else {
1456                    wgt::TextureUses::DEPTH_WRITE | wgt::TextureUses::STENCIL_WRITE
1457                }
1458            };
1459            render_attachments.push(view.to_render_attachment(usage));
1460
1461            depth_stencil = Some(hal::DepthStencilAttachment {
1462                target: hal::Attachment {
1463                    view: view.try_raw(snatch_guard)?,
1464                    usage,
1465                },
1466                depth_ops: at.depth.hal_ops(),
1467                stencil_ops: at.stencil.hal_ops(),
1468                clear_value: (at.depth.clear_value(), at.stencil.clear_value()),
1469                depth_read_only: is_depth_read_only,
1470                stencil_read_only: is_stencil_read_only,
1471            });
1472        }
1473
1474        let mut attachment_set = crate::FastHashSet::default();
1475
1476        let mut color_attachments_hal =
1477            ArrayVec::<Option<hal::ColorAttachment<_>>, { hal::MAX_COLOR_ATTACHMENTS }>::new();
1478        for (index, attachment) in color_attachments.iter().enumerate() {
1479            let at = if let Some(attachment) = attachment.as_ref() {
1480                attachment
1481            } else {
1482                color_attachments_hal.push(None);
1483                continue;
1484            };
1485            let color_view: &TextureView = &at.view;
1486            color_view.same_device(device)?;
1487            check_multiview(color_view)?;
1488            add_view(
1489                color_view,
1490                AttachmentErrorLocation::Color {
1491                    index,
1492                    resolve: false,
1493                },
1494            )?;
1495
1496            if !color_view.desc.aspects().intersects(
1497                hal::FormatAspects::COLOR
1498                    | hal::FormatAspects::PLANE_0
1499                    | hal::FormatAspects::PLANE_1
1500                    | hal::FormatAspects::PLANE_2,
1501            ) {
1502                return Err(RenderPassErrorInner::ColorAttachment(
1503                    ColorAttachmentError::InvalidFormat(color_view.desc.format),
1504                ));
1505            }
1506
1507            if color_view.desc.dimension == TextureViewDimension::D3 {
1508                if let Some(depth_slice) = at.depth_slice {
1509                    let mip = color_view.desc.range.base_mip_level;
1510                    let mip_size = color_view
1511                        .parent
1512                        .desc
1513                        .size
1514                        .mip_level_size(mip, color_view.parent.desc.dimension);
1515                    let limit = mip_size.depth_or_array_layers;
1516                    if depth_slice >= limit {
1517                        return Err(RenderPassErrorInner::ColorAttachment(
1518                            ColorAttachmentError::DepthSliceLimit {
1519                                given: depth_slice,
1520                                limit,
1521                            },
1522                        ));
1523                    }
1524                } else {
1525                    return Err(RenderPassErrorInner::ColorAttachment(
1526                        ColorAttachmentError::MissingDepthSlice,
1527                    ));
1528                }
1529            } else if at.depth_slice.is_some() {
1530                return Err(RenderPassErrorInner::ColorAttachment(
1531                    ColorAttachmentError::UnneededDepthSlice,
1532                ));
1533            }
1534
1535            validation::validate_color_attachment_bytes_per_sample(
1536                color_attachments
1537                    .iter()
1538                    .flatten()
1539                    .map(|at| at.view.desc.format),
1540                device.limits.max_color_attachment_bytes_per_sample,
1541            )
1542            .map_err(RenderPassErrorInner::ColorAttachment)?;
1543
1544            fn check_attachment_overlap(
1545                attachment_set: &mut crate::FastHashSet<(crate::track::TrackerIndex, u32, u32)>,
1546                view: &TextureView,
1547                depth_slice: Option<u32>,
1548            ) -> Result<(), ColorAttachmentError> {
1549                let mut insert = |slice| {
1550                    let mip_level = view.desc.range.base_mip_level;
1551                    if attachment_set.insert((
1552                        view.parent.tracking_data.tracker_index(),
1553                        mip_level,
1554                        slice,
1555                    )) {
1556                        Ok(())
1557                    } else {
1558                        Err(ColorAttachmentError::SubresourceOverlap {
1559                            view: view.error_ident(),
1560                            mip_level,
1561                            depth_or_array_layer: slice,
1562                        })
1563                    }
1564                };
1565                match view.desc.dimension {
1566                    TextureViewDimension::D2 => {
1567                        insert(view.desc.range.base_array_layer)?;
1568                    }
1569                    TextureViewDimension::D2Array => {
1570                        for layer in view.selector.layers.clone() {
1571                            insert(layer)?;
1572                        }
1573                    }
1574                    TextureViewDimension::D3 => {
1575                        insert(depth_slice.unwrap())?;
1576                    }
1577                    _ => unreachable!(),
1578                };
1579                Ok(())
1580            }
1581
1582            check_attachment_overlap(&mut attachment_set, color_view, at.depth_slice)?;
1583
1584            Self::add_pass_texture_init_actions(
1585                at.load_op,
1586                at.store_op,
1587                texture_memory_actions,
1588                color_view,
1589                at.depth_slice,
1590                pending_discard_init_fixups,
1591            );
1592            render_attachments
1593                .push(color_view.to_render_attachment(wgt::TextureUses::COLOR_TARGET));
1594
1595            let mut hal_resolve_target = None;
1596            if let Some(resolve_view) = &at.resolve_target {
1597                resolve_view.same_device(device)?;
1598                check_multiview(resolve_view)?;
1599
1600                check_attachment_overlap(&mut attachment_set, resolve_view, None)?;
1601
1602                let resolve_location = AttachmentErrorLocation::Color {
1603                    index,
1604                    resolve: true,
1605                };
1606
1607                let render_extent = resolve_view.state()?.render_extent.map_err(|reason| {
1608                    RenderPassErrorInner::TextureViewIsNotRenderable {
1609                        location: resolve_location,
1610                        reason,
1611                    }
1612                })?;
1613                if color_view.state()?.render_extent.unwrap() != render_extent {
1614                    return Err(RenderPassErrorInner::AttachmentsDimensionMismatch {
1615                        expected_location: attachment_location,
1616                        expected_extent: extent.unwrap_or_default(),
1617                        actual_location: resolve_location,
1618                        actual_extent: render_extent,
1619                    });
1620                }
1621                if color_view.samples == 1 || resolve_view.samples != 1 {
1622                    return Err(RenderPassErrorInner::InvalidResolveSampleCounts {
1623                        location: resolve_location,
1624                        src: color_view.samples,
1625                        dst: resolve_view.samples,
1626                    });
1627                }
1628                if color_view.desc.format != resolve_view.desc.format {
1629                    return Err(RenderPassErrorInner::MismatchedResolveTextureFormat {
1630                        location: resolve_location,
1631                        src: color_view.desc.format,
1632                        dst: resolve_view.desc.format,
1633                    });
1634                }
1635                if !resolve_view
1636                    .format_features
1637                    .flags
1638                    .contains(wgt::TextureFormatFeatureFlags::MULTISAMPLE_RESOLVE)
1639                {
1640                    return Err(RenderPassErrorInner::UnsupportedResolveTargetFormat {
1641                        location: resolve_location,
1642                        format: resolve_view.desc.format,
1643                    });
1644                }
1645                if resolve_view
1646                    .desc
1647                    .usage
1648                    .contains(TextureUsages::TRANSIENT_ATTACHMENT)
1649                {
1650                    return Err(RenderPassErrorInner::InvalidTransientResolveTarget {
1651                        location: resolve_location,
1652                    });
1653                }
1654
1655                texture_memory_actions.register_implicit_init(
1656                    &resolve_view.parent,
1657                    TextureInitRange::from(resolve_view.as_ref()),
1658                );
1659                render_attachments
1660                    .push(resolve_view.to_render_attachment(wgt::TextureUses::COLOR_TARGET));
1661
1662                hal_resolve_target = Some(hal::Attachment {
1663                    view: resolve_view.try_raw(snatch_guard)?,
1664                    usage: wgt::TextureUses::COLOR_TARGET,
1665                });
1666            }
1667
1668            color_attachments_hal.push(Some(hal::ColorAttachment {
1669                target: hal::Attachment {
1670                    view: color_view.try_raw(snatch_guard)?,
1671                    usage: wgt::TextureUses::COLOR_TARGET,
1672                },
1673                depth_slice: at.depth_slice,
1674                resolve_target: hal_resolve_target,
1675                ops: at.hal_ops(),
1676                clear_value: at.clear_value(),
1677            }));
1678        }
1679
1680        let extent = extent.ok_or(RenderPassErrorInner::MissingAttachments)?;
1681
1682        let detected_multiview =
1683            detected_multiview.expect("Multiview was not detected, no attachments");
1684        if let Some(mask) = multiview_mask {
1685            // 0x01 will have msb 0
1686            let mask_msb = 31 - mask.leading_zeros();
1687            let detected_mv = detected_multiview.map(NonZeroU32::get).unwrap_or(1);
1688            if mask_msb >= detected_mv {
1689                return Err(RenderPassErrorInner::MultiViewMismatch);
1690            }
1691            if mask.get() != (1 << detected_mv) - 1 {
1692                device.require_features(wgt::Features::SELECTIVE_MULTIVIEW)?;
1693            }
1694        }
1695
1696        let attachment_formats = AttachmentData {
1697            colors: color_attachments
1698                .iter()
1699                .map(|at| at.as_ref().map(|at| at.view.desc.format))
1700                .collect(),
1701            resolves: color_attachments
1702                .iter()
1703                .filter_map(|at| {
1704                    at.as_ref().and_then(|at| {
1705                        at.resolve_target
1706                            .as_ref()
1707                            .map(|resolve| resolve.desc.format)
1708                    })
1709                })
1710                .collect(),
1711            depth_stencil: depth_stencil_attachment
1712                .as_ref()
1713                .map(|at| at.view.desc.format),
1714        };
1715
1716        let context = RenderPassContext {
1717            attachments: attachment_formats,
1718            sample_count,
1719            multiview_mask,
1720        };
1721
1722        let timestamp_writes_hal = if let Some(tw) = timestamp_writes.as_ref() {
1723            let query_set = &tw.query_set;
1724            query_set.same_device(device)?;
1725
1726            if let Some(index) = tw.beginning_of_pass_write_index {
1727                pending_query_resets.use_query_set(query_set, index);
1728            }
1729            if let Some(index) = tw.end_of_pass_write_index {
1730                pending_query_resets.use_query_set(query_set, index);
1731            }
1732
1733            record_pass_timestamp_writes(tw, query_set_writes);
1734
1735            Some(hal::PassTimestampWrites {
1736                query_set: query_set.try_raw(snatch_guard)?,
1737                beginning_of_pass_write_index: tw.beginning_of_pass_write_index,
1738                end_of_pass_write_index: tw.end_of_pass_write_index,
1739            })
1740        } else {
1741            None
1742        };
1743
1744        let occlusion_query_set_hal = if let Some(query_set) = occlusion_query_set.as_ref() {
1745            query_set.same_device(device)?;
1746            Some(query_set.try_raw(snatch_guard)?)
1747        } else {
1748            None
1749        };
1750
1751        let hal_desc = hal::RenderPassDescriptor {
1752            label: hal_label,
1753            extent,
1754            sample_count,
1755            color_attachments: &color_attachments_hal,
1756            depth_stencil_attachment: depth_stencil,
1757            multiview_mask,
1758            timestamp_writes: timestamp_writes_hal,
1759            occlusion_query_set: occlusion_query_set_hal,
1760        };
1761        unsafe {
1762            encoder
1763                .begin_render_pass(&hal_desc)
1764                .map_err(|e| device.handle_hal_error(e))?;
1765        };
1766        drop(color_attachments_hal); // Drop, so we can consume `color_attachments` for the tracker.
1767
1768        // Can't borrow the tracker more than once, so have to add to the tracker after the `begin_render_pass` hal call.
1769        if let Some(tw) = timestamp_writes.take() {
1770            trackers.query_sets.insert_single(tw.query_set);
1771        };
1772        if let Some(occlusion_query_set) = occlusion_query_set.take() {
1773            trackers.query_sets.insert_single(occlusion_query_set);
1774        };
1775        if let Some(at) = depth_stencil_attachment.take() {
1776            trackers.views.insert_single(at.view.clone());
1777        }
1778        for at in color_attachments.iter().flatten() {
1779            trackers.views.insert_single(at.view.clone());
1780            if let Some(resolve_target) = at.resolve_target.clone() {
1781                trackers.views.insert_single(resolve_target);
1782            }
1783        }
1784
1785        Ok(Self {
1786            context,
1787            render_attachments,
1788            is_depth_read_only,
1789            is_stencil_read_only,
1790            extent,
1791            divergent_discarded_depth_stencil_aspect,
1792            multiview_mask,
1793        })
1794    }
1795
1796    fn finish(
1797        self,
1798        device: &Device,
1799        raw: &mut dyn hal::DynCommandEncoder,
1800        snatch_guard: &SnatchGuard,
1801        scope: &mut UsageScope<'_>,
1802        instance_flags: InstanceFlags,
1803    ) -> Result<(), RenderPassErrorInner> {
1804        profiling::scope!("RenderPassInfo::finish");
1805        unsafe {
1806            raw.end_render_pass();
1807        }
1808
1809        for ra in self.render_attachments {
1810            let texture = &ra.texture;
1811            texture.check_usage(TextureUsages::RENDER_ATTACHMENT)?;
1812
1813            // the tracker set of the pass is always in "extend" mode
1814            unsafe {
1815                scope
1816                    .textures
1817                    .merge_single(texture, Some(ra.selector.clone()), ra.usage)?
1818            };
1819        }
1820
1821        // If one aspect of a combined depth/stencil format was discarded, we
1822        // clear that aspect immediately to keep the init status of the aspects
1823        // in sync. We do this so we don't need to track init state for depth/
1824        // stencil aspects individually. We can't initialize the entire texture,
1825        // because the user may genuinely want to preserve the data in the
1826        // aspect that was not discarded.
1827        if let Some((aspect, view)) = self.divergent_discarded_depth_stencil_aspect {
1828            let (depth_ops, stencil_ops) = if aspect == wgt::TextureAspect::DepthOnly {
1829                (
1830                    hal::AttachmentOps::LOAD_CLEAR | hal::AttachmentOps::STORE, // clear depth
1831                    hal::AttachmentOps::LOAD | hal::AttachmentOps::STORE,       // unchanged stencil
1832                )
1833            } else {
1834                (
1835                    hal::AttachmentOps::LOAD | hal::AttachmentOps::STORE, // unchanged stencil
1836                    hal::AttachmentOps::LOAD_CLEAR | hal::AttachmentOps::STORE, // clear depth
1837                )
1838            };
1839            let desc = hal::RenderPassDescriptor::<'_, _, dyn hal::DynTextureView> {
1840                label: hal_label(
1841                    Some("(wgpu internal) Zero init discarded depth/stencil aspect"),
1842                    instance_flags,
1843                ),
1844                extent: view.state()?.render_extent.unwrap(),
1845                sample_count: view.samples,
1846                color_attachments: &[],
1847                depth_stencil_attachment: Some(hal::DepthStencilAttachment {
1848                    target: hal::Attachment {
1849                        view: view.try_raw(snatch_guard)?,
1850                        usage: wgt::TextureUses::DEPTH_WRITE | wgt::TextureUses::STENCIL_WRITE,
1851                    },
1852                    depth_ops,
1853                    stencil_ops,
1854                    clear_value: (0.0, 0),
1855                    depth_read_only: false,
1856                    stencil_read_only: false,
1857                }),
1858                multiview_mask: self.multiview_mask,
1859                timestamp_writes: None,
1860                occlusion_query_set: None,
1861            };
1862            unsafe {
1863                raw.begin_render_pass(&desc)
1864                    .map_err(|e| device.handle_hal_error(e))?;
1865                raw.end_render_pass();
1866            }
1867        }
1868
1869        Ok(())
1870    }
1871}
1872
1873fn check_transient_attachment_ops<V>(load_op: LoadOp<V>, store_op: StoreOp) -> bool {
1874    matches!(
1875        (load_op, store_op),
1876        (LoadOp::Clear(_) | LoadOp::DontCare(_), StoreOp::Discard)
1877    )
1878}
1879
1880impl CommandEncoder {
1881    fn begin_render_pass_inner(
1882        self: &Arc<Self>,
1883        desc: ResolvedRenderPassDescriptor<'_>,
1884    ) -> (RenderPass, Option<CommandEncoderError>) {
1885        use EncoderStateError as SErr;
1886
1887        fn fill_arc_desc(
1888            desc: ResolvedRenderPassDescriptor<'_>,
1889            arc_desc: &mut ArcRenderPassDescriptor,
1890            device: &Device,
1891        ) -> Result<(), RenderPassErrorInner> {
1892            device.check_is_valid()?;
1893
1894            let max_color_attachments = device.limits.max_color_attachments as usize;
1895            if desc.color_attachments.len() > max_color_attachments {
1896                return Err(RenderPassErrorInner::ColorAttachment(
1897                    ColorAttachmentError::TooMany {
1898                        given: desc.color_attachments.len(),
1899                        limit: max_color_attachments,
1900                    },
1901                ));
1902            }
1903
1904            for color_attachment in desc.color_attachments.iter() {
1905                if let Some(RenderPassColorAttachment {
1906                    view,
1907                    depth_slice,
1908                    resolve_target,
1909                    load_op,
1910                    store_op,
1911                }) = color_attachment
1912                {
1913                    view.check_valid()?;
1914                    view.same_device(device)?;
1915                    if matches!(*load_op, LoadOp::DontCare(..))
1916                        && device
1917                            .instance_flags
1918                            .contains(InstanceFlags::STRICT_WEBGPU_COMPLIANCE)
1919                    {
1920                        return Err(RenderPassErrorInner::ColorAttachment(
1921                            ColorAttachmentError::LoadOpDontCareUnderStrictWebgpuCompliance,
1922                        ));
1923                    }
1924
1925                    if view
1926                        .desc
1927                        .usage
1928                        .contains(TextureUsages::TRANSIENT_ATTACHMENT)
1929                        && !check_transient_attachment_ops(*load_op, *store_op)
1930                    {
1931                        return Err(RenderPassErrorInner::ColorAttachment(
1932                            ColorAttachmentError::InvalidTransientAttachmentOp((
1933                                *load_op, *store_op,
1934                            )),
1935                        ));
1936                    }
1937
1938                    let resolve_target = if let Some(resolve_target) = resolve_target {
1939                        resolve_target.check_valid()?;
1940                        resolve_target.same_device(device)?;
1941
1942                        Some(resolve_target)
1943                    } else {
1944                        None
1945                    };
1946
1947                    arc_desc
1948                        .color_attachments
1949                        .push(Some(RenderPassColorAttachment {
1950                            view: Arc::clone(view),
1951                            depth_slice: *depth_slice,
1952                            resolve_target: resolve_target.map(Arc::clone),
1953                            load_op: *load_op,
1954                            store_op: *store_op,
1955                        }));
1956                } else {
1957                    arc_desc.color_attachments.push(None);
1958                }
1959            }
1960
1961            // https://gpuweb.github.io/gpuweb/#abstract-opdef-gpurenderpassdepthstencilattachment-gpurenderpassdepthstencilattachment-valid-usage
1962            arc_desc.depth_stencil_attachment = if let Some(depth_stencil_attachment) =
1963                desc.depth_stencil_attachment
1964            {
1965                let view = depth_stencil_attachment.view;
1966                view.check_valid()?;
1967                view.same_device(device)?;
1968
1969                let format = view.desc.format;
1970                if !format.is_depth_stencil_format() {
1971                    return Err(RenderPassErrorInner::InvalidAttachment(
1972                        AttachmentError::InvalidDepthStencilAttachmentFormat(view.desc.format),
1973                    ));
1974                }
1975
1976                #[allow(
1977                    clippy::collapsible_match,
1978                    reason = "Match guards change which attachment error is returned."
1979                )]
1980                if view
1981                    .desc
1982                    .usage
1983                    .contains(TextureUsages::TRANSIENT_ATTACHMENT)
1984                {
1985                    // We raise errors here for transient depth/stencil attachments if they
1986                    // are read only, or use unsupported ops. `resolve` (called below)
1987                    // validates that operations are specified iff the attachment is not
1988                    // read-only.
1989                    if format.has_depth_aspect() {
1990                        match depth_stencil_attachment.depth {
1991                            PassChannel {
1992                                load_op: Some(load_op),
1993                                store_op: Some(store_op),
1994                                read_only: _,
1995                            } => {
1996                                if !check_transient_attachment_ops(load_op, store_op) {
1997                                    return Err(RenderPassErrorInner::InvalidAttachment(
1998                                        AttachmentError::InvalidTransientDepthAttachmentOps((
1999                                            load_op, store_op,
2000                                        )),
2001                                    ));
2002                                }
2003                            }
2004                            PassChannel {
2005                                read_only: true, ..
2006                            } => {
2007                                return Err(RenderPassErrorInner::InvalidAttachment(
2008                                    AttachmentError::ReadOnlyTransientDepthAttachment,
2009                                ))
2010                            }
2011                            _ => {}
2012                        }
2013                    }
2014
2015                    if format.has_stencil_aspect() {
2016                        match depth_stencil_attachment.stencil {
2017                            PassChannel {
2018                                load_op: Some(load_op),
2019                                store_op: Some(store_op),
2020                                read_only: _,
2021                            } => {
2022                                if !check_transient_attachment_ops(load_op, store_op) {
2023                                    return Err(RenderPassErrorInner::InvalidAttachment(
2024                                        AttachmentError::InvalidTransientStencilAttachmentOps((
2025                                            load_op, store_op,
2026                                        )),
2027                                    ));
2028                                }
2029                            }
2030                            PassChannel {
2031                                read_only: true, ..
2032                            } => {
2033                                return Err(RenderPassErrorInner::InvalidAttachment(
2034                                    AttachmentError::ReadOnlyTransientStencilAttachment,
2035                                ))
2036                            }
2037                            _ => {}
2038                        }
2039                    }
2040                }
2041
2042                Some(ResolvedRenderPassDepthStencilAttachment {
2043                    view,
2044                    depth: if format.has_depth_aspect() {
2045                        depth_stencil_attachment
2046                            .depth
2047                            .resolve(device.instance_flags, |clear| {
2048                                if let Some(clear) = clear {
2049                                    // If this.depthLoadOp is "clear", this.depthClearValue must be provided and must be between 0.0 and 1.0, inclusive.
2050                                    if !(0.0..=1.0).contains(&clear) {
2051                                        Err(AttachmentError::ClearValueOutOfRange(clear))
2052                                    } else {
2053                                        Ok(clear)
2054                                    }
2055                                } else {
2056                                    Err(AttachmentError::NoClearValue)
2057                                }
2058                            })?
2059                    } else {
2060                        if depth_stencil_attachment.depth.load_op.is_some()
2061                            || depth_stencil_attachment.depth.store_op.is_some()
2062                        {
2063                            return Err(RenderPassErrorInner::InvalidAttachment(
2064                                AttachmentError::DepthOpsWithoutAspect {
2065                                    format,
2066                                    ops: (
2067                                        depth_stencil_attachment.depth.load_op,
2068                                        depth_stencil_attachment.depth.store_op,
2069                                    ),
2070                                },
2071                            ));
2072                        }
2073                        ResolvedPassChannel::ReadOnly
2074                    },
2075                    stencil: if format.has_stencil_aspect() {
2076                        depth_stencil_attachment.stencil.resolve(
2077                            device.instance_flags,
2078                            |clear| {
2079                                Ok(convert_stencil_value(
2080                                    clear.unwrap_or_default(),
2081                                    Some(format),
2082                                ))
2083                            },
2084                        )?
2085                    } else {
2086                        if depth_stencil_attachment.stencil.load_op.is_some()
2087                            || depth_stencil_attachment.stencil.store_op.is_some()
2088                        {
2089                            return Err(RenderPassErrorInner::InvalidAttachment(
2090                                AttachmentError::StencilOpsWithoutAspect {
2091                                    format,
2092                                    ops: (
2093                                        depth_stencil_attachment.stencil.load_op,
2094                                        depth_stencil_attachment.stencil.store_op,
2095                                    ),
2096                                },
2097                            ));
2098                        }
2099                        ResolvedPassChannel::ReadOnly
2100                    },
2101                })
2102            } else {
2103                None
2104            };
2105
2106            arc_desc.timestamp_writes = desc
2107                .timestamp_writes
2108                .map(|tw| {
2109                    CommandEncoder::validate_pass_timestamp_writes::<RenderPassErrorInner>(
2110                        device, &tw,
2111                    )
2112                })
2113                .transpose()?;
2114
2115            arc_desc.occlusion_query_set =
2116                if let Some(occlusion_query_set) = desc.occlusion_query_set {
2117                    occlusion_query_set.check_is_valid()?;
2118                    occlusion_query_set.same_device(device)?;
2119
2120                    if !matches!(occlusion_query_set.desc.ty, wgt::QueryType::Occlusion) {
2121                        return Err(QueryUseError::IncompatibleType {
2122                            set_type: occlusion_query_set.desc.ty.into(),
2123                            query_type: super::SimplifiedQueryType::Occlusion,
2124                        }
2125                        .into());
2126                    }
2127
2128                    Some(occlusion_query_set)
2129                } else {
2130                    None
2131                };
2132
2133            arc_desc.multiview_mask = desc.multiview_mask;
2134
2135            Ok(())
2136        }
2137
2138        let scope = PassErrorScope::Pass;
2139        let mut cmd_buf_data = self.data.lock();
2140
2141        match cmd_buf_data.lock_encoder() {
2142            Ok(()) => {
2143                drop(cmd_buf_data);
2144                let label = desc.label.clone();
2145                let mut arc_desc = ArcRenderPassDescriptor {
2146                    label: &label,
2147                    timestamp_writes: None,
2148                    color_attachments: ArrayVec::new(),
2149                    depth_stencil_attachment: None,
2150                    occlusion_query_set: None,
2151                    multiview_mask: None,
2152                };
2153                match fill_arc_desc(desc, &mut arc_desc, &self.device) {
2154                    Ok(()) => (RenderPass::new(self.clone(), arc_desc), None),
2155                    Err(err) => (
2156                        RenderPass::new_invalid(self.clone(), &label, err.map_pass_err(scope)),
2157                        None,
2158                    ),
2159                }
2160            }
2161            Err(err @ SErr::Locked) => {
2162                // Attempting to open a new pass while the encoder is locked
2163                // invalidates the encoder, but does not generate a validation
2164                // error.
2165                cmd_buf_data.invalidate(err.clone());
2166                drop(cmd_buf_data);
2167                (
2168                    RenderPass::new_invalid(self.clone(), &desc.label, err.map_pass_err(scope)),
2169                    None,
2170                )
2171            }
2172            Err(err @ (SErr::Ended | SErr::Submitted)) => {
2173                // Attempting to open a new pass after the encode has ended
2174                // generates an immediate validation error.
2175                drop(cmd_buf_data);
2176                (
2177                    RenderPass::new_invalid(
2178                        self.clone(),
2179                        &desc.label,
2180                        err.clone().map_pass_err(scope),
2181                    ),
2182                    Some(err.into()),
2183                )
2184            }
2185            Err(err @ SErr::Invalid) => {
2186                // Passes can be opened even on an invalid encoder. Such passes
2187                // are even valid, but since there's no visible side-effect of
2188                // the pass being valid and there's no point in storing recorded
2189                // commands that will ultimately be discarded, we open an
2190                // invalid pass to save that work.
2191                drop(cmd_buf_data);
2192                (
2193                    RenderPass::new_invalid(self.clone(), &desc.label, err.map_pass_err(scope)),
2194                    None,
2195                )
2196            }
2197            Err(SErr::Unlocked) => {
2198                unreachable!("lock_encoder cannot fail due to the encoder being unlocked")
2199            }
2200        }
2201    }
2202
2203    pub fn begin_render_pass(
2204        self: &Arc<Self>,
2205        desc: ResolvedRenderPassDescriptor<'_>,
2206    ) -> RenderPass {
2207        let (pass, err) = self.begin_render_pass_inner(desc);
2208        if let Some(err) = err {
2209            self.device
2210                .handle_error(err, pass.label(), "CommandEncoder::begin_render_pass");
2211        }
2212        pass
2213    }
2214}
2215
2216impl RenderPass {
2217    fn end_inner(&mut self) -> Result<(), EncoderStateError> {
2218        profiling::scope!(
2219            "CommandEncoder::run_render_pass {}",
2220            self.base.label.as_deref().unwrap_or("")
2221        );
2222
2223        let cmd_enc = self.parent.take().ok_or(EncoderStateError::Ended)?;
2224        let mut cmd_buf_data = cmd_enc.data.lock();
2225
2226        cmd_buf_data.unlock_encoder()?;
2227
2228        let base = self.base.take();
2229
2230        if let Err(RenderPassError { inner, scope: _ }) = &base {
2231            if let RenderPassErrorInner::EncoderState(
2232                err @ (EncoderStateError::Locked | EncoderStateError::Ended),
2233            ) = inner.as_ref()
2234            {
2235                // Most encoding errors are detected and raised within `finish()`.
2236                //
2237                // However, we raise a validation error here if the pass was opened
2238                // within another pass, or on a finished encoder. The latter is
2239                // particularly important, because in that case reporting errors via
2240                // `CommandEncoder::finish` is not possible.
2241                return Err(err.clone());
2242            }
2243        }
2244
2245        cmd_buf_data.push_with(|| -> Result<_, RenderPassError> {
2246            Ok(ArcCommand::RunRenderPass {
2247                pass: base?,
2248                color_attachments: SmallVec::from(self.color_attachments.as_slice()),
2249                depth_stencil_attachment: self.depth_stencil_attachment.take(),
2250                timestamp_writes: self.timestamp_writes.take(),
2251                occlusion_query_set: self.occlusion_query_set.take(),
2252                multiview_mask: self.multiview_mask,
2253            })
2254        })
2255    }
2256
2257    pub fn end(&mut self) {
2258        if let Err(err) = self.end_inner() {
2259            self.device
2260                .handle_error(err, self.label(), "RenderPass::end");
2261        }
2262    }
2263}
2264
2265pub(super) fn encode_render_pass(
2266    parent_state: &mut EncodingState<InnerCommandEncoder>,
2267    mut base: BasePass<ArcRenderCommand, Infallible>,
2268    color_attachments: ColorAttachments<Arc<TextureView>>,
2269    mut depth_stencil_attachment: Option<
2270        ResolvedRenderPassDepthStencilAttachment<Arc<TextureView>>,
2271    >,
2272    mut timestamp_writes: Option<PassTimestampWrites>,
2273    occlusion_query_set: Option<Arc<QuerySet>>,
2274    multiview_mask: Option<NonZeroU32>,
2275) -> Result<(), RenderPassError> {
2276    let pass_scope = PassErrorScope::Pass;
2277
2278    let device = parent_state.device;
2279
2280    let mut indirect_draw_validation_batcher = crate::indirect_validation::DrawBatcher::new();
2281
2282    // We automatically keep extending command buffers over time, and because
2283    // we want to insert a command buffer _before_ what we're about to record,
2284    // we need to make sure to close the previous one.
2285    parent_state
2286        .raw_encoder
2287        .close_if_open()
2288        .map_pass_err(pass_scope)?;
2289    let raw_encoder = parent_state
2290        .raw_encoder
2291        .open_pass(base.label.as_deref())
2292        .map_pass_err(pass_scope)?;
2293
2294    let (scope, pending_discard_init_fixups, mut pending_query_resets) = {
2295        let mut pending_query_resets = QueryResetMap::new();
2296        let mut pending_discard_init_fixups = SurfacesInDiscardState::new();
2297
2298        let info = RenderPassInfo::start(
2299            device,
2300            hal_label(base.label.as_deref(), device.instance_flags),
2301            &color_attachments,
2302            depth_stencil_attachment.take(),
2303            timestamp_writes.take(),
2304            // Still needed down the line.
2305            // TODO(wumpf): by restructuring the code, we could get rid of some of this Arc clone.
2306            occlusion_query_set.clone(),
2307            raw_encoder,
2308            parent_state.tracker,
2309            parent_state.texture_memory_actions,
2310            &mut pending_query_resets,
2311            &mut pending_discard_init_fixups,
2312            parent_state.snatch_guard,
2313            parent_state.query_set_writes,
2314            multiview_mask,
2315        )
2316        .map_pass_err(pass_scope)?;
2317
2318        let indices = &device.tracker_indices;
2319        parent_state
2320            .tracker
2321            .buffers
2322            .set_size(indices.buffers.size());
2323        parent_state
2324            .tracker
2325            .textures
2326            .set_size(indices.textures.size());
2327
2328        let mut debug_scope_depth = 0;
2329
2330        let mut state = State {
2331            pipeline_flags: PipelineFlags::empty(),
2332            blend_constant: OptionalState::Unused,
2333            stencil_reference: 0,
2334            pipeline: None,
2335            index: IndexState::default(),
2336            vertex: VertexState::default(),
2337
2338            info,
2339
2340            pass: pass::PassState {
2341                base: EncodingState {
2342                    device,
2343                    raw_encoder,
2344                    tracker: parent_state.tracker,
2345                    buffer_memory_init_actions: parent_state.buffer_memory_init_actions,
2346                    texture_memory_actions: parent_state.texture_memory_actions,
2347                    as_actions: parent_state.as_actions,
2348                    temp_resources: parent_state.temp_resources,
2349                    indirect_draw_validation_resources: parent_state
2350                        .indirect_draw_validation_resources,
2351                    snatch_guard: parent_state.snatch_guard,
2352                    debug_scope_depth: &mut debug_scope_depth,
2353                    query_set_writes: parent_state.query_set_writes,
2354                    deferred_query_set_resolves: parent_state.deferred_query_set_resolves,
2355                },
2356                pending_discard_init_fixups,
2357                scope: device.new_usage_scope(),
2358                binder: Binder::new(),
2359
2360                temp_offsets: Vec::new(),
2361                dynamic_offset_count: 0,
2362
2363                string_offset: 0,
2364
2365                immediate_state: ImmediateState::default(),
2366            },
2367
2368            active_occlusion_query: None,
2369            active_pipeline_statistics_query: None,
2370        };
2371
2372        for command in base.commands.drain(..) {
2373            match command {
2374                ArcRenderCommand::SetBindGroup {
2375                    index,
2376                    num_dynamic_offsets,
2377                    bind_group,
2378                } => {
2379                    let scope = PassErrorScope::SetBindGroup;
2380                    pass::set_bind_group::<RenderPassErrorInner>(
2381                        &mut state.pass,
2382                        device,
2383                        &base.dynamic_offsets,
2384                        index,
2385                        num_dynamic_offsets,
2386                        bind_group,
2387                        true,
2388                    )
2389                    .map_pass_err(scope)?;
2390                }
2391                ArcRenderCommand::SetPipeline(pipeline) => {
2392                    let scope = PassErrorScope::SetPipelineRender;
2393                    set_pipeline(&mut state, device, pipeline).map_pass_err(scope)?;
2394                }
2395                ArcRenderCommand::SetIndexBuffer {
2396                    buffer,
2397                    index_format,
2398                    offset,
2399                    size,
2400                } => {
2401                    let scope = PassErrorScope::SetIndexBuffer;
2402                    set_index_buffer(&mut state, device, buffer, index_format, offset, size)
2403                        .map_pass_err(scope)?;
2404                }
2405                ArcRenderCommand::SetVertexBuffer {
2406                    slot,
2407                    buffer,
2408                    offset,
2409                    size,
2410                } => {
2411                    let scope = PassErrorScope::SetVertexBuffer;
2412                    set_vertex_buffer(&mut state, device, slot, buffer, offset, size)
2413                        .map_pass_err(scope)?;
2414                }
2415                ArcRenderCommand::SetBlendConstant(ref color) => {
2416                    set_blend_constant(&mut state, color);
2417                }
2418                ArcRenderCommand::SetStencilReference(value) => {
2419                    set_stencil_reference(&mut state, value);
2420                }
2421                ArcRenderCommand::SetViewport {
2422                    rect,
2423                    depth_min,
2424                    depth_max,
2425                } => {
2426                    let scope = PassErrorScope::SetViewport;
2427                    set_viewport(&mut state, rect, depth_min, depth_max).map_pass_err(scope)?;
2428                }
2429                ArcRenderCommand::SetImmediate { offset, data } => {
2430                    let scope = PassErrorScope::SetImmediate;
2431                    state
2432                        .pass
2433                        .immediate_state
2434                        .set_immediates::<RenderPassErrorInner>(
2435                            &state.pass.base.device.limits,
2436                            offset,
2437                            &data,
2438                        )
2439                        .map_pass_err(scope)?;
2440                }
2441                ArcRenderCommand::SetScissor(rect) => {
2442                    let scope = PassErrorScope::SetScissorRect;
2443                    set_scissor(&mut state, rect).map_pass_err(scope)?;
2444                }
2445                ArcRenderCommand::Draw {
2446                    vertex_count,
2447                    instance_count,
2448                    first_vertex,
2449                    first_instance,
2450                } => {
2451                    let scope = PassErrorScope::Draw {
2452                        kind: DrawKind::Draw,
2453                        family: DrawCommandFamily::Draw,
2454                    };
2455                    draw(
2456                        &mut state,
2457                        vertex_count,
2458                        instance_count,
2459                        first_vertex,
2460                        first_instance,
2461                    )
2462                    .map_pass_err(scope)?;
2463                }
2464                ArcRenderCommand::DrawIndexed {
2465                    index_count,
2466                    instance_count,
2467                    first_index,
2468                    base_vertex,
2469                    first_instance,
2470                } => {
2471                    let scope = PassErrorScope::Draw {
2472                        kind: DrawKind::Draw,
2473                        family: DrawCommandFamily::DrawIndexed,
2474                    };
2475                    draw_indexed(
2476                        &mut state,
2477                        index_count,
2478                        instance_count,
2479                        first_index,
2480                        base_vertex,
2481                        first_instance,
2482                    )
2483                    .map_pass_err(scope)?;
2484                }
2485                ArcRenderCommand::DrawMeshTasks {
2486                    group_count_x,
2487                    group_count_y,
2488                    group_count_z,
2489                } => {
2490                    let scope = PassErrorScope::Draw {
2491                        kind: DrawKind::Draw,
2492                        family: DrawCommandFamily::DrawMeshTasks,
2493                    };
2494                    draw_mesh_tasks(&mut state, group_count_x, group_count_y, group_count_z)
2495                        .map_pass_err(scope)?;
2496                }
2497                ArcRenderCommand::DrawIndirect {
2498                    buffer,
2499                    offset,
2500                    count,
2501                    family,
2502
2503                    vertex_or_index_limit: _,
2504                    instance_limit: _,
2505                } => {
2506                    let scope = PassErrorScope::Draw {
2507                        kind: if count != 1 {
2508                            DrawKind::MultiDrawIndirect
2509                        } else {
2510                            DrawKind::DrawIndirect
2511                        },
2512                        family,
2513                    };
2514                    multi_draw_indirect(
2515                        &mut state,
2516                        &mut indirect_draw_validation_batcher,
2517                        device,
2518                        buffer,
2519                        offset,
2520                        count,
2521                        family,
2522                    )
2523                    .map_pass_err(scope)?;
2524                }
2525                ArcRenderCommand::MultiDrawIndirectCount {
2526                    buffer,
2527                    offset,
2528                    count_buffer,
2529                    count_buffer_offset,
2530                    max_count,
2531                    family,
2532                } => {
2533                    let scope = PassErrorScope::Draw {
2534                        kind: DrawKind::MultiDrawIndirectCount,
2535                        family,
2536                    };
2537                    multi_draw_indirect_count(
2538                        &mut state,
2539                        device,
2540                        buffer,
2541                        offset,
2542                        count_buffer,
2543                        count_buffer_offset,
2544                        max_count,
2545                        family,
2546                    )
2547                    .map_pass_err(scope)?;
2548                }
2549                ArcRenderCommand::PushDebugGroup { color: _, len } => {
2550                    pass::push_debug_group(&mut state.pass, &base.string_data, len);
2551                }
2552                ArcRenderCommand::PopDebugGroup => {
2553                    let scope = PassErrorScope::PopDebugGroup;
2554                    pass::pop_debug_group::<RenderPassErrorInner>(&mut state.pass)
2555                        .map_pass_err(scope)?;
2556                }
2557                ArcRenderCommand::InsertDebugMarker { color: _, len } => {
2558                    pass::insert_debug_marker(&mut state.pass, &base.string_data, len);
2559                }
2560                ArcRenderCommand::WriteTimestamp {
2561                    query_set,
2562                    query_index,
2563                } => {
2564                    let scope = PassErrorScope::WriteTimestamp;
2565                    pass::write_timestamp::<RenderPassErrorInner>(
2566                        &mut state.pass,
2567                        device,
2568                        Some(&mut pending_query_resets),
2569                        query_set,
2570                        query_index,
2571                    )
2572                    .map_pass_err(scope)?;
2573                }
2574                ArcRenderCommand::BeginOcclusionQuery { query_index } => {
2575                    api_log!("RenderPass::begin_occlusion_query {query_index}");
2576                    let scope = PassErrorScope::BeginOcclusionQuery;
2577
2578                    let query_set = occlusion_query_set
2579                        .clone()
2580                        .ok_or(RenderPassErrorInner::MissingOcclusionQuerySet)
2581                        .map_pass_err(scope)?;
2582
2583                    validate_and_begin_occlusion_query(
2584                        query_set,
2585                        state.pass.base.raw_encoder,
2586                        &mut state.pass.base.tracker.query_sets,
2587                        query_index,
2588                        Some(&mut pending_query_resets),
2589                        &mut state.active_occlusion_query,
2590                        state.pass.base.snatch_guard,
2591                    )
2592                    .map_pass_err(scope)?;
2593                }
2594                ArcRenderCommand::EndOcclusionQuery => {
2595                    api_log!("RenderPass::end_occlusion_query");
2596                    let scope = PassErrorScope::EndOcclusionQuery;
2597
2598                    end_occlusion_query(
2599                        state.pass.base.raw_encoder,
2600                        &mut state.active_occlusion_query,
2601                        state.pass.base.snatch_guard,
2602                        state.pass.base.query_set_writes,
2603                    )
2604                    .map_pass_err(scope)?;
2605                }
2606                ArcRenderCommand::BeginPipelineStatisticsQuery {
2607                    query_set,
2608                    query_index,
2609                } => {
2610                    api_log!(
2611                        "RenderPass::begin_pipeline_statistics_query {query_index} {}",
2612                        query_set.error_ident()
2613                    );
2614                    let scope = PassErrorScope::BeginPipelineStatisticsQuery;
2615
2616                    validate_and_begin_pipeline_statistics_query(
2617                        query_set,
2618                        state.pass.base.raw_encoder,
2619                        &mut state.pass.base.tracker.query_sets,
2620                        device,
2621                        query_index,
2622                        Some(&mut pending_query_resets),
2623                        &mut state.active_pipeline_statistics_query,
2624                        state.pass.base.snatch_guard,
2625                    )
2626                    .map_pass_err(scope)?;
2627                }
2628                ArcRenderCommand::EndPipelineStatisticsQuery => {
2629                    api_log!("RenderPass::end_pipeline_statistics_query");
2630                    let scope = PassErrorScope::EndPipelineStatisticsQuery;
2631
2632                    end_pipeline_statistics_query(
2633                        state.pass.base.raw_encoder,
2634                        &mut state.active_pipeline_statistics_query,
2635                        state.pass.base.snatch_guard,
2636                        state.pass.base.query_set_writes,
2637                    )
2638                    .map_pass_err(scope)?;
2639                }
2640                ArcRenderCommand::ExecuteBundle(bundle) => {
2641                    let scope = PassErrorScope::ExecuteBundle;
2642                    execute_bundle(
2643                        &mut state,
2644                        &mut indirect_draw_validation_batcher,
2645                        device,
2646                        bundle,
2647                    )
2648                    .map_pass_err(scope)?;
2649                }
2650            }
2651        }
2652
2653        if *state.pass.base.debug_scope_depth > 0 {
2654            Err(
2655                RenderPassErrorInner::DebugGroupError(DebugGroupError::MissingPop)
2656                    .map_pass_err(pass_scope),
2657            )?;
2658        }
2659        if state.active_occlusion_query.is_some() {
2660            Err(RenderPassErrorInner::QueryUse(QueryUseError::MissingEnd {
2661                query_type: super::SimplifiedQueryType::Occlusion,
2662            })
2663            .map_pass_err(pass_scope))?;
2664        }
2665        if state.active_pipeline_statistics_query.is_some() {
2666            Err(RenderPassErrorInner::QueryUse(QueryUseError::MissingEnd {
2667                query_type: super::SimplifiedQueryType::PipelineStatistics,
2668            })
2669            .map_pass_err(pass_scope))?;
2670        }
2671
2672        state
2673            .info
2674            .finish(
2675                device,
2676                state.pass.base.raw_encoder,
2677                state.pass.base.snatch_guard,
2678                &mut state.pass.scope,
2679                device.instance_flags,
2680            )
2681            .map_pass_err(pass_scope)?;
2682
2683        let trackers = state.pass.scope;
2684
2685        let pending_discard_init_fixups = state.pass.pending_discard_init_fixups;
2686
2687        parent_state.raw_encoder.close().map_pass_err(pass_scope)?;
2688        (trackers, pending_discard_init_fixups, pending_query_resets)
2689    };
2690
2691    let encoder = &mut parent_state.raw_encoder;
2692    let tracker = &mut parent_state.tracker;
2693
2694    {
2695        let transit = encoder
2696            .open_pass(hal_label(
2697                Some("(wgpu internal) Pre Pass"),
2698                device.instance_flags,
2699            ))
2700            .map_pass_err(pass_scope)?;
2701
2702        // If this pass reads any surfaces that were discarded by a previous
2703        // pass in the same command buffer, initialize them.
2704        fixup_discarded_surfaces(
2705            pending_discard_init_fixups.into_iter(),
2706            transit,
2707            &mut tracker.textures,
2708            device,
2709            parent_state.snatch_guard,
2710        );
2711
2712        pending_query_resets
2713            .reset_queries(transit, parent_state.snatch_guard)
2714            .map_pass_err(pass_scope)?;
2715
2716        CommandEncoder::insert_barriers_from_scope(
2717            transit,
2718            tracker,
2719            &scope,
2720            parent_state.snatch_guard,
2721        );
2722
2723        if let Some(ref indirect_validation) = device.indirect_validation {
2724            indirect_validation
2725                .draw
2726                .inject_validation_pass(
2727                    device,
2728                    parent_state.snatch_guard,
2729                    parent_state.indirect_draw_validation_resources,
2730                    parent_state.temp_resources,
2731                    transit,
2732                    indirect_draw_validation_batcher,
2733                )
2734                .map_pass_err(pass_scope)?;
2735        }
2736
2737        // Backends may have deferred setup work for the pass's indirect
2738        // multi-draws (e.g. Metal indirect-command-buffer generation); encode
2739        // it after indirect validation so it reads validated draw arguments.
2740        unsafe { transit.encode_deferred_multi_draws() };
2741    }
2742
2743    encoder.close_and_swap().map_pass_err(pass_scope)?;
2744
2745    Ok(())
2746}
2747
2748fn set_pipeline(
2749    state: &mut State,
2750    device: &Arc<Device>,
2751    pipeline: Arc<RenderPipeline>,
2752) -> Result<(), RenderPassErrorInner> {
2753    api_log!("RenderPass::set_pipeline {}", pipeline.error_ident());
2754
2755    state.pipeline = Some(pipeline.clone());
2756
2757    let pipeline = state
2758        .pass
2759        .base
2760        .tracker
2761        .render_pipelines
2762        .insert_single(pipeline)
2763        .clone();
2764
2765    pipeline.same_device(device)?;
2766
2767    state
2768        .info
2769        .context
2770        .check_compatible(&pipeline.pass_context, pipeline.as_ref())
2771        .map_err(RenderCommandError::IncompatiblePipelineTargets)?;
2772
2773    state.pipeline_flags = pipeline.flags;
2774
2775    if pipeline.flags.contains(PipelineFlags::WRITES_DEPTH) && state.info.is_depth_read_only {
2776        return Err(RenderCommandError::IncompatibleDepthAccess(pipeline.error_ident()).into());
2777    }
2778    if pipeline.flags.contains(PipelineFlags::WRITES_STENCIL) && state.info.is_stencil_read_only {
2779        return Err(RenderCommandError::IncompatibleStencilAccess(pipeline.error_ident()).into());
2780    }
2781
2782    state
2783        .blend_constant
2784        .require(pipeline.flags.contains(PipelineFlags::BLEND_CONSTANT));
2785
2786    unsafe {
2787        state
2788            .pass
2789            .base
2790            .raw_encoder
2791            .set_render_pipeline(pipeline.raw()?);
2792    }
2793
2794    if pipeline.flags.contains(PipelineFlags::STENCIL_REFERENCE) {
2795        unsafe {
2796            state
2797                .pass
2798                .base
2799                .raw_encoder
2800                .set_stencil_reference(state.stencil_reference);
2801        }
2802    }
2803
2804    // Rebind resource
2805    pass::change_pipeline_layout::<RenderPassErrorInner>(
2806        &mut state.pass,
2807        pipeline.layout()?,
2808        &pipeline.late_sized_buffer_groups,
2809    )?;
2810
2811    // Update vertex buffer limits.
2812    state.vertex.update_limits(&pipeline.vertex_steps);
2813    Ok(())
2814}
2815
2816// This function is duplicative of `bundle::set_index_buffer`.
2817fn set_index_buffer(
2818    state: &mut State,
2819    device: &Arc<Device>,
2820    buffer: Arc<Buffer>,
2821    index_format: IndexFormat,
2822    offset: u64,
2823    size: Option<BufferAddress>,
2824) -> Result<(), RenderPassErrorInner> {
2825    api_log!("RenderPass::set_index_buffer {}", buffer.error_ident());
2826
2827    state
2828        .pass
2829        .scope
2830        .buffers
2831        .merge_single(&buffer, wgt::BufferUses::INDEX)?;
2832
2833    buffer.same_device(device)?;
2834
2835    buffer.check_usage(BufferUsages::INDEX)?;
2836
2837    if !offset.is_multiple_of(u64::from(index_format.byte_size())) {
2838        return Err(RenderCommandError::UnalignedIndexBuffer {
2839            offset,
2840            alignment: index_format.byte_size() as usize,
2841        }
2842        .into());
2843    }
2844
2845    let range = buffer
2846        .resolve_vertex_or_index_binding_range(offset, size)
2847        .map_err(RenderCommandError::from)?;
2848
2849    let raw = buffer.try_raw(state.pass.base.snatch_guard)?;
2850
2851    state.index.update_buffer(range.clone(), index_format);
2852
2853    state.pass.base.buffer_memory_init_actions.extend(
2854        buffer.initialization_status.read().create_action(
2855            &buffer,
2856            range.clone(),
2857            MemoryInitKind::NeedsInitializedMemory,
2858        ),
2859    );
2860
2861    unsafe {
2862        // SAFETY: The binding range was validated by `resolve_vertex_or_index_binding_range`.
2863        hal::DynCommandEncoder::set_index_buffer(
2864            state.pass.base.raw_encoder,
2865            hal::BufferBinding::new_unchecked(raw, range.start, range.end - range.start),
2866            index_format,
2867        );
2868    }
2869    Ok(())
2870}
2871
2872// This function is duplicative of `render::set_vertex_buffer`.
2873fn set_vertex_buffer(
2874    state: &mut State,
2875    device: &Arc<Device>,
2876    slot: u32,
2877    buffer: Option<Arc<Buffer>>,
2878    offset: u64,
2879    size: Option<BufferAddress>,
2880) -> Result<(), RenderPassErrorInner> {
2881    if let Some(ref buffer) = buffer {
2882        api_log!(
2883            "RenderPass::set_vertex_buffer {slot} {}",
2884            buffer.error_ident()
2885        );
2886    } else {
2887        api_log!("RenderPass::set_vertex_buffer {slot} None");
2888    }
2889
2890    let max_vertex_buffers = state.pass.base.device.limits.max_vertex_buffers;
2891    if slot >= max_vertex_buffers {
2892        return Err(RenderCommandError::VertexBufferIndexOutOfRange {
2893            index: slot,
2894            max: max_vertex_buffers,
2895        }
2896        .into());
2897    }
2898
2899    if let Some(buffer) = buffer {
2900        buffer.same_device(device)?;
2901        buffer.check_usage(BufferUsages::VERTEX)?;
2902
2903        if !offset.is_multiple_of(wgt::VERTEX_ALIGNMENT) {
2904            return Err(RenderCommandError::UnalignedVertexBuffer { slot, offset }.into());
2905        }
2906        let range = buffer
2907            .resolve_vertex_or_index_binding_range(offset, size)
2908            .map_err(RenderCommandError::from)?;
2909
2910        state
2911            .pass
2912            .scope
2913            .buffers
2914            .merge_single(&buffer, wgt::BufferUses::VERTEX)?;
2915
2916        state.pass.base.buffer_memory_init_actions.extend(
2917            buffer.initialization_status.read().create_action(
2918                &buffer,
2919                range.clone(),
2920                MemoryInitKind::NeedsInitializedMemory,
2921            ),
2922        );
2923
2924        state
2925            .vertex
2926            .set_buffer(slot as usize, buffer, range.clone());
2927        if let Some(pipeline) = state.pipeline.as_ref() {
2928            state.vertex.update_limits(&pipeline.vertex_steps);
2929        }
2930    } else {
2931        if offset != 0 {
2932            return Err(RenderCommandError::from(
2933                crate::binding_model::BindingError::UnbindingVertexBufferOffsetNotZero {
2934                    slot,
2935                    offset,
2936                },
2937            )
2938            .into());
2939        }
2940        match size {
2941            Some(size) if size != 0 => {
2942                return Err(RenderCommandError::from(
2943                    crate::binding_model::BindingError::UnbindingVertexBufferSizeNotZero {
2944                        slot,
2945                        size,
2946                    },
2947                )
2948                .into());
2949            }
2950            _ => {}
2951        }
2952
2953        state.vertex.clear_buffer(slot as usize);
2954        if let Some(pipeline) = state.pipeline.as_ref() {
2955            state.vertex.update_limits(&pipeline.vertex_steps);
2956        }
2957    }
2958
2959    Ok(())
2960}
2961
2962fn set_blend_constant(state: &mut State, color: &Color) {
2963    api_log!("RenderPass::set_blend_constant");
2964
2965    state.blend_constant = OptionalState::Set;
2966    let array = [
2967        color.r as f32,
2968        color.g as f32,
2969        color.b as f32,
2970        color.a as f32,
2971    ];
2972    unsafe {
2973        state.pass.base.raw_encoder.set_blend_constants(&array);
2974    }
2975}
2976
2977fn set_stencil_reference(state: &mut State, value: u32) {
2978    api_log!("RenderPass::set_stencil_reference {value}");
2979
2980    state.stencil_reference = value;
2981    if state
2982        .pipeline_flags
2983        .contains(PipelineFlags::STENCIL_REFERENCE)
2984    {
2985        unsafe {
2986            state.pass.base.raw_encoder.set_stencil_reference(value);
2987        }
2988    }
2989}
2990
2991fn set_viewport(
2992    state: &mut State,
2993    rect: Rect<f32>,
2994    depth_min: f32,
2995    depth_max: f32,
2996) -> Result<(), RenderPassErrorInner> {
2997    api_log!("RenderPass::set_viewport {rect:?}");
2998
2999    if rect.w < 0.0
3000        || rect.h < 0.0
3001        || rect.w > state.pass.base.device.limits.max_texture_dimension_2d as f32
3002        || rect.h > state.pass.base.device.limits.max_texture_dimension_2d as f32
3003    {
3004        return Err(RenderCommandError::InvalidViewportRectSize {
3005            w: rect.w,
3006            h: rect.h,
3007            max: state.pass.base.device.limits.max_texture_dimension_2d,
3008        }
3009        .into());
3010    }
3011
3012    let max_viewport_range = state.pass.base.device.limits.max_texture_dimension_2d as f32 * 2.0;
3013
3014    if rect.x < -max_viewport_range
3015        || rect.y < -max_viewport_range
3016        || rect.x + rect.w > max_viewport_range - 1.0
3017        || rect.y + rect.h > max_viewport_range - 1.0
3018    {
3019        return Err(RenderCommandError::InvalidViewportRectPosition {
3020            rect,
3021            min: -max_viewport_range,
3022            max: max_viewport_range - 1.0,
3023        }
3024        .into());
3025    }
3026    if !(0.0..=1.0).contains(&depth_min)
3027        || !(0.0..=1.0).contains(&depth_max)
3028        || depth_min > depth_max
3029    {
3030        return Err(RenderCommandError::InvalidViewportDepth(depth_min, depth_max).into());
3031    }
3032    let r = hal::Rect {
3033        x: rect.x,
3034        y: rect.y,
3035        w: rect.w,
3036        h: rect.h,
3037    };
3038    unsafe {
3039        state
3040            .pass
3041            .base
3042            .raw_encoder
3043            .set_viewport(&r, depth_min..depth_max);
3044    }
3045    Ok(())
3046}
3047
3048fn set_scissor(state: &mut State, rect: Rect<u32>) -> Result<(), RenderPassErrorInner> {
3049    api_log!("RenderPass::set_scissor_rect {rect:?}");
3050
3051    if rect.x.saturating_add(rect.w) > state.info.extent.width
3052        || rect.y.saturating_add(rect.h) > state.info.extent.height
3053    {
3054        return Err(RenderCommandError::InvalidScissorRect(rect, state.info.extent).into());
3055    }
3056    let r = hal::Rect {
3057        x: rect.x,
3058        y: rect.y,
3059        w: rect.w,
3060        h: rect.h,
3061    };
3062    unsafe {
3063        state.pass.base.raw_encoder.set_scissor_rect(&r);
3064    }
3065    Ok(())
3066}
3067
3068fn draw(
3069    state: &mut State,
3070    vertex_count: u32,
3071    instance_count: u32,
3072    first_vertex: u32,
3073    first_instance: u32,
3074) -> Result<(), RenderPassErrorInner> {
3075    api_log!("RenderPass::draw {vertex_count} {instance_count} {first_vertex} {first_instance}");
3076
3077    state.is_ready(DrawCommandFamily::Draw)?;
3078    state.flush_vertex_buffers()?;
3079    state.flush_bindings()?;
3080    state.flush_immediates();
3081
3082    state
3083        .vertex
3084        .limits
3085        .validate_vertex_limit(first_vertex, vertex_count)?;
3086    state
3087        .vertex
3088        .limits
3089        .validate_instance_limit(first_instance, instance_count)?;
3090
3091    unsafe {
3092        if instance_count > 0 && vertex_count > 0 {
3093            state.pass.base.raw_encoder.draw(
3094                first_vertex,
3095                vertex_count,
3096                first_instance,
3097                instance_count,
3098            );
3099        }
3100    }
3101    Ok(())
3102}
3103
3104fn draw_indexed(
3105    state: &mut State,
3106    index_count: u32,
3107    instance_count: u32,
3108    first_index: u32,
3109    base_vertex: i32,
3110    first_instance: u32,
3111) -> Result<(), RenderPassErrorInner> {
3112    api_log!("RenderPass::draw_indexed {index_count} {instance_count} {first_index} {base_vertex} {first_instance}");
3113
3114    state.is_ready(DrawCommandFamily::DrawIndexed)?;
3115    state.flush_vertex_buffers()?;
3116    state.flush_bindings()?;
3117    state.flush_immediates();
3118
3119    let last_index = first_index as u64 + index_count as u64;
3120    let index_limit = state.index.limit;
3121    if last_index > index_limit {
3122        return Err(DrawError::IndexBeyondLimit {
3123            last_index,
3124            index_limit,
3125        }
3126        .into());
3127    }
3128    state
3129        .vertex
3130        .limits
3131        .validate_instance_limit(first_instance, instance_count)?;
3132
3133    unsafe {
3134        if instance_count > 0 && index_count > 0 {
3135            state.pass.base.raw_encoder.draw_indexed(
3136                first_index,
3137                index_count,
3138                base_vertex,
3139                first_instance,
3140                instance_count,
3141            );
3142        }
3143    }
3144    Ok(())
3145}
3146
3147fn draw_mesh_tasks(
3148    state: &mut State,
3149    group_count_x: u32,
3150    group_count_y: u32,
3151    group_count_z: u32,
3152) -> Result<(), RenderPassErrorInner> {
3153    api_log!("RenderPass::draw_mesh_tasks {group_count_x} {group_count_y} {group_count_z}");
3154
3155    state.is_ready(DrawCommandFamily::DrawMeshTasks)?;
3156
3157    state.flush_bindings()?;
3158    state.flush_immediates();
3159    validate_mesh_draw_multiview(state.pass.base.device, state.info.multiview_mask)?;
3160
3161    let limits = &state.pass.base.device.limits;
3162    let (groups_size_limit, max_groups) = if state.pipeline.as_ref().unwrap().has_task_shader {
3163        (
3164            limits.max_task_workgroups_per_dimension,
3165            limits.max_task_workgroup_total_count,
3166        )
3167    } else {
3168        (
3169            limits.max_mesh_workgroups_per_dimension,
3170            limits.max_mesh_workgroup_total_count,
3171        )
3172    };
3173
3174    let total_count = WorkgroupSizeCheck {
3175        dimensions: &[group_count_x, group_count_y, group_count_z],
3176        per_dimension_limits: &[groups_size_limit, groups_size_limit, groups_size_limit],
3177        per_dimension_limits_desc: "max_task_mesh_workgroups_per_dimension",
3178
3179        total_limit: max_groups,
3180        total_limit_desc: "max_task_mesh_workgroup_total_count",
3181    }
3182    .check_and_compute_total_invocations()
3183    .map_err(|err| RenderPassErrorInner::Draw(err.into()))?;
3184
3185    unsafe {
3186        if total_count > 0 {
3187            state.pass.base.raw_encoder.draw_mesh_tasks(
3188                group_count_x,
3189                group_count_y,
3190                group_count_z,
3191            );
3192        }
3193    }
3194    Ok(())
3195}
3196
3197fn multi_draw_indirect(
3198    state: &mut State,
3199    indirect_draw_validation_batcher: &mut crate::indirect_validation::DrawBatcher,
3200    device: &Arc<Device>,
3201    indirect_buffer: Arc<Buffer>,
3202    offset: u64,
3203    count: u32,
3204    family: DrawCommandFamily,
3205) -> Result<(), RenderPassErrorInner> {
3206    api_log!(
3207        "RenderPass::draw_indirect (family:{family:?}) {} {offset} {count:?}",
3208        indirect_buffer.error_ident()
3209    );
3210
3211    state.is_ready(family)?;
3212    state.flush_vertex_buffers()?;
3213    state.flush_bindings()?;
3214    state.flush_immediates();
3215
3216    if family == DrawCommandFamily::DrawMeshTasks {
3217        validate_mesh_draw_multiview(state.pass.base.device, state.info.multiview_mask)?;
3218    }
3219
3220    state
3221        .pass
3222        .base
3223        .device
3224        .require_downlevel_flags(wgt::DownlevelFlags::INDIRECT_EXECUTION)?;
3225
3226    indirect_buffer.same_device(device)?;
3227    indirect_buffer.check_usage(BufferUsages::INDIRECT)?;
3228    indirect_buffer.check_destroyed(state.pass.base.snatch_guard)?;
3229
3230    if !offset.is_multiple_of(4) {
3231        return Err(RenderCommandError::UnalignedIndirectBufferOffset(offset).into());
3232    }
3233
3234    let stride = get_src_stride_of_indirect_args(family);
3235    let args_size = match stride.checked_mul(u64::from(count)) {
3236        Some(sz) if sz <= indirect_buffer.size && indirect_buffer.size - sz >= offset => sz,
3237        args_size => {
3238            return Err(RenderCommandError::IndirectBufferOverrun {
3239                count,
3240                offset,
3241                args_size: args_size.unwrap_or(u64::MAX),
3242                buffer_size: indirect_buffer.size,
3243            }
3244            .into());
3245        }
3246    };
3247
3248    state.pass.base.buffer_memory_init_actions.extend(
3249        indirect_buffer.initialization_status.read().create_action(
3250            &indirect_buffer,
3251            offset..offset + args_size,
3252            MemoryInitKind::NeedsInitializedMemory,
3253        ),
3254    );
3255
3256    fn draw(
3257        raw_encoder: &mut dyn hal::DynCommandEncoder,
3258        family: DrawCommandFamily,
3259        indirect_buffer: &dyn hal::DynBuffer,
3260        offset: u64,
3261        count: u32,
3262    ) {
3263        match family {
3264            DrawCommandFamily::Draw => unsafe {
3265                raw_encoder.draw_indirect(indirect_buffer, offset, count);
3266            },
3267            DrawCommandFamily::DrawIndexed => unsafe {
3268                raw_encoder.draw_indexed_indirect(indirect_buffer, offset, count);
3269            },
3270            DrawCommandFamily::DrawMeshTasks => unsafe {
3271                raw_encoder.draw_mesh_tasks_indirect(indirect_buffer, offset, count);
3272            },
3273        }
3274    }
3275
3276    if state.pass.base.device.indirect_validation.is_some()
3277        && family != DrawCommandFamily::DrawMeshTasks
3278    {
3279        state
3280            .pass
3281            .scope
3282            .buffers
3283            .merge_single(&indirect_buffer, wgt::BufferUses::STORAGE_READ_ONLY)?;
3284
3285        struct DrawData {
3286            buffer_index: usize,
3287            offset: u64,
3288            count: u32,
3289        }
3290
3291        struct DrawContext<'a> {
3292            raw_encoder: &'a mut dyn hal::DynCommandEncoder,
3293            device: &'a Device,
3294
3295            indirect_draw_validation_resources: &'a mut crate::indirect_validation::DrawResources,
3296            indirect_draw_validation_batcher: &'a mut crate::indirect_validation::DrawBatcher,
3297
3298            indirect_buffer: Arc<Buffer>,
3299            family: DrawCommandFamily,
3300            vertex_or_index_limit: u64,
3301            instance_limit: u64,
3302        }
3303
3304        impl<'a> DrawContext<'a> {
3305            fn add(&mut self, offset: u64) -> Result<DrawData, DeviceError> {
3306                let (dst_resource_index, dst_offset) = self.indirect_draw_validation_batcher.add(
3307                    self.indirect_draw_validation_resources,
3308                    self.device,
3309                    &self.indirect_buffer,
3310                    offset,
3311                    self.family,
3312                    self.vertex_or_index_limit,
3313                    self.instance_limit,
3314                )?;
3315                Ok(DrawData {
3316                    buffer_index: dst_resource_index,
3317                    offset: dst_offset,
3318                    count: 1,
3319                })
3320            }
3321            fn draw(&mut self, draw_data: DrawData) {
3322                let dst_buffer = self
3323                    .indirect_draw_validation_resources
3324                    .get_dst_buffer(draw_data.buffer_index);
3325                draw(
3326                    self.raw_encoder,
3327                    self.family,
3328                    dst_buffer,
3329                    draw_data.offset,
3330                    draw_data.count,
3331                );
3332            }
3333        }
3334
3335        let mut draw_ctx = DrawContext {
3336            raw_encoder: state.pass.base.raw_encoder,
3337            device: state.pass.base.device,
3338            indirect_draw_validation_resources: state.pass.base.indirect_draw_validation_resources,
3339            indirect_draw_validation_batcher,
3340            indirect_buffer,
3341            family,
3342            vertex_or_index_limit: if family == DrawCommandFamily::DrawIndexed {
3343                state.index.limit
3344            } else {
3345                state.vertex.limits.vertex_limit
3346            },
3347            instance_limit: state.vertex.limits.instance_limit,
3348        };
3349
3350        let mut current_draw_data = draw_ctx.add(offset)?;
3351
3352        for i in 1..count {
3353            let draw_data = draw_ctx.add(offset + stride * i as u64)?;
3354
3355            if draw_data.buffer_index == current_draw_data.buffer_index {
3356                #[cfg(debug_assertions)]
3357                {
3358                    let dst_stride =
3359                        get_dst_stride_of_indirect_args(state.pass.base.device.backend(), family);
3360                    debug_assert_eq!(
3361                        draw_data.offset,
3362                        current_draw_data.offset + dst_stride * current_draw_data.count as u64
3363                    );
3364                }
3365                current_draw_data.count += 1;
3366            } else {
3367                draw_ctx.draw(current_draw_data);
3368                current_draw_data = draw_data;
3369            }
3370        }
3371
3372        draw_ctx.draw(current_draw_data);
3373    } else {
3374        state
3375            .pass
3376            .scope
3377            .buffers
3378            .merge_single(&indirect_buffer, wgt::BufferUses::INDIRECT)?;
3379
3380        draw(
3381            state.pass.base.raw_encoder,
3382            family,
3383            indirect_buffer.try_raw(state.pass.base.snatch_guard)?,
3384            offset,
3385            count,
3386        );
3387    };
3388
3389    Ok(())
3390}
3391
3392fn multi_draw_indirect_count(
3393    state: &mut State,
3394    device: &Arc<Device>,
3395    indirect_buffer: Arc<Buffer>,
3396    offset: u64,
3397    count_buffer: Arc<Buffer>,
3398    count_buffer_offset: u64,
3399    max_count: u32,
3400    family: DrawCommandFamily,
3401) -> Result<(), RenderPassErrorInner> {
3402    api_log!(
3403        "RenderPass::multi_draw_indirect_count (family:{family:?}) {} {offset} {} {count_buffer_offset:?} {max_count:?}",
3404        indirect_buffer.error_ident(),
3405        count_buffer.error_ident()
3406    );
3407
3408    state.is_ready(family)?;
3409    state.flush_vertex_buffers()?;
3410    state.flush_bindings()?;
3411    state.flush_immediates();
3412
3413    if family == DrawCommandFamily::DrawMeshTasks {
3414        validate_mesh_draw_multiview(state.pass.base.device, state.info.multiview_mask)?;
3415    }
3416
3417    let stride = get_src_stride_of_indirect_args(family);
3418
3419    state
3420        .pass
3421        .base
3422        .device
3423        .require_features(wgt::Features::MULTI_DRAW_INDIRECT_COUNT)?;
3424    state
3425        .pass
3426        .base
3427        .device
3428        .require_downlevel_flags(wgt::DownlevelFlags::INDIRECT_EXECUTION)?;
3429
3430    indirect_buffer.same_device(device)?;
3431    count_buffer.same_device(device)?;
3432
3433    state
3434        .pass
3435        .scope
3436        .buffers
3437        .merge_single(&indirect_buffer, wgt::BufferUses::INDIRECT)?;
3438
3439    indirect_buffer.check_usage(BufferUsages::INDIRECT)?;
3440    let indirect_raw = indirect_buffer.try_raw(state.pass.base.snatch_guard)?;
3441
3442    state
3443        .pass
3444        .scope
3445        .buffers
3446        .merge_single(&count_buffer, wgt::BufferUses::INDIRECT)?;
3447
3448    count_buffer.check_usage(BufferUsages::INDIRECT)?;
3449    let count_raw = count_buffer.try_raw(state.pass.base.snatch_guard)?;
3450
3451    if !offset.is_multiple_of(4) {
3452        return Err(RenderCommandError::UnalignedIndirectBufferOffset(offset).into());
3453    }
3454
3455    let args_size = match stride.checked_mul(u64::from(max_count)) {
3456        Some(sz) if sz <= indirect_buffer.size && indirect_buffer.size - sz >= offset => sz,
3457        args_size => {
3458            return Err(RenderCommandError::IndirectBufferOverrun {
3459                count: 1,
3460                offset,
3461                args_size: args_size.unwrap_or(u64::MAX),
3462                buffer_size: indirect_buffer.size,
3463            }
3464            .into());
3465        }
3466    };
3467
3468    state.pass.base.buffer_memory_init_actions.extend(
3469        indirect_buffer.initialization_status.read().create_action(
3470            &indirect_buffer,
3471            offset..offset + args_size,
3472            MemoryInitKind::NeedsInitializedMemory,
3473        ),
3474    );
3475
3476    let begin_count_offset = count_buffer_offset;
3477    let count_bytes = 4;
3478    if count_buffer.size < count_bytes || count_buffer.size - count_bytes < count_buffer_offset {
3479        return Err(RenderPassErrorInner::IndirectCountBufferOverrun {
3480            begin_count_offset,
3481            count_bytes: 4,
3482            count_buffer_size: count_buffer.size,
3483        });
3484    }
3485    state.pass.base.buffer_memory_init_actions.extend(
3486        count_buffer.initialization_status.read().create_action(
3487            &count_buffer,
3488            count_buffer_offset..count_buffer_offset + count_bytes,
3489            MemoryInitKind::NeedsInitializedMemory,
3490        ),
3491    );
3492
3493    match family {
3494        DrawCommandFamily::Draw => unsafe {
3495            state.pass.base.raw_encoder.draw_indirect_count(
3496                indirect_raw,
3497                offset,
3498                count_raw,
3499                count_buffer_offset,
3500                max_count,
3501            );
3502        },
3503        DrawCommandFamily::DrawIndexed => unsafe {
3504            state.pass.base.raw_encoder.draw_indexed_indirect_count(
3505                indirect_raw,
3506                offset,
3507                count_raw,
3508                count_buffer_offset,
3509                max_count,
3510            );
3511        },
3512        DrawCommandFamily::DrawMeshTasks => unsafe {
3513            state.pass.base.raw_encoder.draw_mesh_tasks_indirect_count(
3514                indirect_raw,
3515                offset,
3516                count_raw,
3517                count_buffer_offset,
3518                max_count,
3519            );
3520        },
3521    }
3522    Ok(())
3523}
3524
3525fn execute_bundle(
3526    state: &mut State,
3527    indirect_draw_validation_batcher: &mut crate::indirect_validation::DrawBatcher,
3528    device: &Arc<Device>,
3529    bundle: Arc<RenderBundle>,
3530) -> Result<(), RenderPassErrorInner> {
3531    api_log!("RenderPass::execute_bundle {}", bundle.error_ident());
3532
3533    let bundle = state.pass.base.tracker.bundles.insert_single(bundle);
3534
3535    let bundle_state = bundle.state()?;
3536    bundle.same_device(device)?;
3537
3538    state
3539        .info
3540        .context
3541        .check_compatible(&bundle_state.context, bundle.as_ref())
3542        .map_err(RenderPassErrorInner::IncompatibleBundleTargets)?;
3543
3544    if (state.info.is_depth_read_only && !bundle.is_depth_read_only)
3545        || (state.info.is_stencil_read_only && !bundle.is_stencil_read_only)
3546    {
3547        return Err(
3548            RenderPassErrorInner::IncompatibleBundleReadOnlyDepthStencil {
3549                pass_depth: state.info.is_depth_read_only,
3550                pass_stencil: state.info.is_stencil_read_only,
3551                bundle_depth: bundle.is_depth_read_only,
3552                bundle_stencil: bundle.is_stencil_read_only,
3553            },
3554        );
3555    }
3556
3557    state.pass.base.buffer_memory_init_actions.extend(
3558        bundle
3559            .buffer_memory_init_actions
3560            .iter()
3561            .filter_map(|action| {
3562                action
3563                    .buffer
3564                    .initialization_status
3565                    .read()
3566                    .check_action(action)
3567            }),
3568    );
3569    for action in bundle.texture_memory_init_actions.iter() {
3570        state.pass.pending_discard_init_fixups.extend(
3571            state
3572                .pass
3573                .base
3574                .texture_memory_actions
3575                .register_init_action(action, None),
3576        );
3577    }
3578
3579    unsafe {
3580        bundle.execute(
3581            state.pass.base.raw_encoder,
3582            state.pass.base.indirect_draw_validation_resources,
3583            indirect_draw_validation_batcher,
3584            state.pass.base.snatch_guard,
3585        )
3586    }
3587    .map_err(|e| match e {
3588        ExecutionError::Device(e) => RenderPassErrorInner::Device(e),
3589        ExecutionError::DestroyedResource(e) => {
3590            RenderPassErrorInner::RenderCommand(RenderCommandError::DestroyedResource(e))
3591        }
3592        ExecutionError::InvalidResource(e) => {
3593            RenderPassErrorInner::RenderCommand(RenderCommandError::InvalidResource(e))
3594        }
3595    })?;
3596
3597    unsafe {
3598        state.pass.scope.merge_render_bundle(&bundle_state.used)?;
3599    };
3600    state.reset_bundle();
3601    Ok(())
3602}
3603
3604// Recording a render pass.
3605//
3606// The only error that should be returned from these methods is
3607// `EncoderStateError::Ended`, when the pass has already ended and an immediate
3608// validation error is raised.
3609//
3610// All other errors should be stored in the pass for later reporting when
3611// `CommandEncoder.finish()` is called.
3612//
3613// The `pass_try!` macro should be used to handle errors appropriately. Note
3614// that the `pass_try!` and `pass_base!` macros may return early from the
3615// function that invokes them, like the `?` operator.
3616impl RenderPass {
3617    fn set_bind_group_inner(
3618        &mut self,
3619        index: u32,
3620        bind_group: Option<Arc<BindGroup>>,
3621        offsets: &[DynamicOffset],
3622    ) -> Result<(), PassStateError> {
3623        let scope = PassErrorScope::SetBindGroup;
3624
3625        // This statement will return an error if the pass is ended. It's
3626        // important the error check comes before the early-out for
3627        // `set_and_check_redundant`.
3628        let base = pass_base!(self, scope);
3629
3630        if self.current_bind_groups.set_and_check_redundant(
3631            &bind_group,
3632            index,
3633            &mut base.dynamic_offsets,
3634            offsets,
3635        ) {
3636            return Ok(());
3637        }
3638
3639        let bind_group = if let Some(bind_group) = bind_group {
3640            pass_try!(base, scope, bind_group.check_is_valid());
3641            Some(bind_group)
3642        } else {
3643            None
3644        };
3645
3646        base.commands.push(ArcRenderCommand::SetBindGroup {
3647            index,
3648            num_dynamic_offsets: offsets.len(),
3649            bind_group,
3650        });
3651
3652        Ok(())
3653    }
3654
3655    pub fn set_bind_group(
3656        &mut self,
3657        index: u32,
3658        bind_group: Option<Arc<BindGroup>>,
3659        offsets: &[DynamicOffset],
3660    ) {
3661        if let Err(err) = self.set_bind_group_inner(index, bind_group, offsets) {
3662            self.device
3663                .handle_error(err, self.label(), "RenderPass::set_bind_group");
3664        }
3665    }
3666
3667    fn set_pipeline_inner(&mut self, pipeline: Arc<RenderPipeline>) -> Result<(), PassStateError> {
3668        let scope = PassErrorScope::SetPipelineRender;
3669
3670        let redundant = self.current_pipeline.set_and_check_redundant(&pipeline);
3671
3672        // This statement will return an error if the pass is ended.
3673        // Its important the error check comes before the early-out for `redundant`.
3674        let base = pass_base!(self, scope);
3675
3676        if redundant {
3677            return Ok(());
3678        }
3679
3680        pass_try!(base, scope, pipeline.check_valid());
3681
3682        base.commands.push(ArcRenderCommand::SetPipeline(pipeline));
3683
3684        Ok(())
3685    }
3686
3687    pub fn set_pipeline(&mut self, pipeline: Arc<RenderPipeline>) {
3688        if let Err(err) = self.set_pipeline_inner(pipeline) {
3689            self.device
3690                .handle_error(err, self.label(), "RenderPass::set_pipeline");
3691        }
3692    }
3693
3694    fn set_index_buffer_inner(
3695        &mut self,
3696        buffer: Arc<Buffer>,
3697        index_format: IndexFormat,
3698        offset: BufferAddress,
3699        size: Option<BufferAddress>,
3700    ) -> Result<(), PassStateError> {
3701        let scope = PassErrorScope::SetIndexBuffer;
3702        let base = pass_base!(self, scope);
3703
3704        pass_try!(base, scope, buffer.check_is_valid());
3705
3706        base.commands.push(ArcRenderCommand::SetIndexBuffer {
3707            buffer,
3708            index_format,
3709            offset,
3710            size,
3711        });
3712
3713        Ok(())
3714    }
3715
3716    pub fn set_index_buffer(
3717        &mut self,
3718        buffer: Arc<Buffer>,
3719        index_format: IndexFormat,
3720        offset: BufferAddress,
3721        size: Option<BufferAddress>,
3722    ) {
3723        if let Err(err) = self.set_index_buffer_inner(buffer, index_format, offset, size) {
3724            self.device
3725                .handle_error(err, self.label(), "RenderPass::set_index_buffer");
3726        }
3727    }
3728
3729    fn set_vertex_buffer_inner(
3730        &mut self,
3731        slot: u32,
3732        buffer: Option<Arc<Buffer>>,
3733        offset: BufferAddress,
3734        size: Option<BufferAddress>,
3735    ) -> Result<(), PassStateError> {
3736        let scope = PassErrorScope::SetVertexBuffer;
3737        let base = pass_base!(self, scope);
3738
3739        let buffer = if let Some(buffer) = buffer {
3740            pass_try!(base, scope, buffer.check_is_valid());
3741            Some(buffer)
3742        } else {
3743            None
3744        };
3745
3746        base.commands.push(ArcRenderCommand::SetVertexBuffer {
3747            slot,
3748            buffer,
3749            offset,
3750            size,
3751        });
3752
3753        Ok(())
3754    }
3755
3756    pub fn set_vertex_buffer(
3757        &mut self,
3758        slot: u32,
3759        buffer: Option<Arc<Buffer>>,
3760        offset: BufferAddress,
3761        size: Option<BufferAddress>,
3762    ) {
3763        if let Err(err) = self.set_vertex_buffer_inner(slot, buffer, offset, size) {
3764            self.device
3765                .handle_error(err, self.label(), "RenderPass::set_vertex_buffer");
3766        }
3767    }
3768
3769    fn set_blend_constant_inner(&mut self, color: Color) -> Result<(), PassStateError> {
3770        let scope = PassErrorScope::SetBlendConstant;
3771        let base = pass_base!(self, scope);
3772
3773        base.commands
3774            .push(ArcRenderCommand::SetBlendConstant(color));
3775
3776        Ok(())
3777    }
3778
3779    pub fn set_blend_constant(&mut self, color: Color) {
3780        if let Err(err) = self.set_blend_constant_inner(color) {
3781            self.device
3782                .handle_error(err, self.label(), "RenderPass::set_blend_constant");
3783        }
3784    }
3785
3786    fn set_stencil_reference_inner(&mut self, value: u32) -> Result<(), PassStateError> {
3787        let scope = PassErrorScope::SetStencilReference;
3788        let base = pass_base!(self, scope);
3789        let value = convert_stencil_value(
3790            value,
3791            self.depth_stencil_attachment
3792                .as_ref()
3793                .map(|at| at.view.desc.format),
3794        );
3795        base.commands
3796            .push(ArcRenderCommand::SetStencilReference(value));
3797
3798        Ok(())
3799    }
3800
3801    pub fn set_stencil_reference(&mut self, value: u32) {
3802        if let Err(err) = self.set_stencil_reference_inner(value) {
3803            self.device
3804                .handle_error(err, self.label(), "RenderPass::set_stencil_reference");
3805        }
3806    }
3807
3808    fn set_viewport_inner(
3809        &mut self,
3810        x: f32,
3811        y: f32,
3812        w: f32,
3813        h: f32,
3814        depth_min: f32,
3815        depth_max: f32,
3816    ) -> Result<(), PassStateError> {
3817        let scope = PassErrorScope::SetViewport;
3818        let base = pass_base!(self, scope);
3819
3820        base.commands.push(ArcRenderCommand::SetViewport {
3821            rect: Rect { x, y, w, h },
3822            depth_min,
3823            depth_max,
3824        });
3825
3826        Ok(())
3827    }
3828
3829    pub fn set_viewport(&mut self, x: f32, y: f32, w: f32, h: f32, depth_min: f32, depth_max: f32) {
3830        if let Err(err) = self.set_viewport_inner(x, y, w, h, depth_min, depth_max) {
3831            self.device
3832                .handle_error(err, self.label(), "RenderPass::set_viewport");
3833        }
3834    }
3835
3836    fn set_scissor_rect_inner(
3837        &mut self,
3838        x: u32,
3839        y: u32,
3840        w: u32,
3841        h: u32,
3842    ) -> Result<(), PassStateError> {
3843        let scope = PassErrorScope::SetScissorRect;
3844        let base = pass_base!(self, scope);
3845
3846        base.commands
3847            .push(ArcRenderCommand::SetScissor(Rect { x, y, w, h }));
3848
3849        Ok(())
3850    }
3851
3852    pub fn set_scissor_rect(&mut self, x: u32, y: u32, w: u32, h: u32) {
3853        if let Err(err) = self.set_scissor_rect_inner(x, y, w, h) {
3854            self.device
3855                .handle_error(err, self.label(), "RenderPass::set_scissor_rect");
3856        }
3857    }
3858
3859    fn set_immediates_inner(&mut self, offset: u32, data: &[u8]) -> Result<(), PassStateError> {
3860        let scope = PassErrorScope::SetImmediate;
3861        let base = pass_base!(self, scope);
3862
3863        pass_try!(
3864            base,
3865            scope,
3866            pass::validate_immediates_alignment(offset, data.len())
3867        );
3868
3869        base.commands.push(ArcRenderCommand::SetImmediate {
3870            offset,
3871            data: data
3872                .chunks_exact(size_of::<u32>())
3873                .map(|ck| u32::from_le_bytes(ck.try_into().unwrap()))
3874                .collect(),
3875        });
3876
3877        Ok(())
3878    }
3879
3880    pub fn set_immediates(&mut self, offset: u32, data: &[u8]) {
3881        if let Err(err) = self.set_immediates_inner(offset, data) {
3882            self.device
3883                .handle_error(err, self.label(), "RenderPass::set_immediates");
3884        }
3885    }
3886
3887    fn draw_inner(
3888        &mut self,
3889        vertex_count: u32,
3890        instance_count: u32,
3891        first_vertex: u32,
3892        first_instance: u32,
3893    ) -> Result<(), PassStateError> {
3894        let scope = PassErrorScope::Draw {
3895            kind: DrawKind::Draw,
3896            family: DrawCommandFamily::Draw,
3897        };
3898        let base = pass_base!(self, scope);
3899
3900        base.commands.push(ArcRenderCommand::Draw {
3901            vertex_count,
3902            instance_count,
3903            first_vertex,
3904            first_instance,
3905        });
3906
3907        Ok(())
3908    }
3909
3910    pub fn draw(
3911        &mut self,
3912        vertex_count: u32,
3913        instance_count: u32,
3914        first_vertex: u32,
3915        first_instance: u32,
3916    ) {
3917        if let Err(err) =
3918            self.draw_inner(vertex_count, instance_count, first_vertex, first_instance)
3919        {
3920            self.device
3921                .handle_error(err, self.label(), "RenderPass::draw");
3922        }
3923    }
3924
3925    fn draw_indexed_inner(
3926        &mut self,
3927        index_count: u32,
3928        instance_count: u32,
3929        first_index: u32,
3930        base_vertex: i32,
3931        first_instance: u32,
3932    ) -> Result<(), PassStateError> {
3933        let scope = PassErrorScope::Draw {
3934            kind: DrawKind::Draw,
3935            family: DrawCommandFamily::DrawIndexed,
3936        };
3937        let base = pass_base!(self, scope);
3938
3939        base.commands.push(ArcRenderCommand::DrawIndexed {
3940            index_count,
3941            instance_count,
3942            first_index,
3943            base_vertex,
3944            first_instance,
3945        });
3946
3947        Ok(())
3948    }
3949
3950    pub fn draw_indexed(
3951        &mut self,
3952        index_count: u32,
3953        instance_count: u32,
3954        first_index: u32,
3955        base_vertex: i32,
3956        first_instance: u32,
3957    ) {
3958        if let Err(err) = self.draw_indexed_inner(
3959            index_count,
3960            instance_count,
3961            first_index,
3962            base_vertex,
3963            first_instance,
3964        ) {
3965            self.device
3966                .handle_error(err, self.label(), "RenderPass::draw_indexed");
3967        }
3968    }
3969
3970    fn draw_mesh_tasks_inner(
3971        &mut self,
3972        group_count_x: u32,
3973        group_count_y: u32,
3974        group_count_z: u32,
3975    ) -> Result<(), RenderPassError> {
3976        let scope = PassErrorScope::Draw {
3977            kind: DrawKind::Draw,
3978            family: DrawCommandFamily::DrawMeshTasks,
3979        };
3980        let base = pass_base!(self, scope);
3981
3982        base.commands.push(ArcRenderCommand::DrawMeshTasks {
3983            group_count_x,
3984            group_count_y,
3985            group_count_z,
3986        });
3987        Ok(())
3988    }
3989
3990    pub fn draw_mesh_tasks(&mut self, group_count_x: u32, group_count_y: u32, group_count_z: u32) {
3991        if let Err(err) = self.draw_mesh_tasks_inner(group_count_x, group_count_y, group_count_z) {
3992            self.device
3993                .handle_error(err, self.label(), "RenderPass::draw_mesh_tasks");
3994        }
3995    }
3996
3997    fn draw_indirect_inner(
3998        &mut self,
3999        buffer: Arc<Buffer>,
4000        offset: BufferAddress,
4001    ) -> Result<(), PassStateError> {
4002        let scope = PassErrorScope::Draw {
4003            kind: DrawKind::DrawIndirect,
4004            family: DrawCommandFamily::Draw,
4005        };
4006        let base = pass_base!(self, scope);
4007
4008        pass_try!(base, scope, buffer.check_is_valid());
4009
4010        base.commands.push(ArcRenderCommand::DrawIndirect {
4011            buffer,
4012            offset,
4013            count: 1,
4014            family: DrawCommandFamily::Draw,
4015
4016            vertex_or_index_limit: None,
4017            instance_limit: None,
4018        });
4019
4020        Ok(())
4021    }
4022
4023    pub fn draw_indirect(&mut self, buffer: Arc<Buffer>, offset: BufferAddress) {
4024        if let Err(err) = self.draw_indirect_inner(buffer, offset) {
4025            self.device
4026                .handle_error(err, self.label(), "RenderPass::draw_indirect");
4027        }
4028    }
4029
4030    fn draw_indexed_indirect_inner(
4031        &mut self,
4032        buffer: Arc<Buffer>,
4033        offset: BufferAddress,
4034    ) -> Result<(), PassStateError> {
4035        let scope = PassErrorScope::Draw {
4036            kind: DrawKind::DrawIndirect,
4037            family: DrawCommandFamily::DrawIndexed,
4038        };
4039        let base = pass_base!(self, scope);
4040
4041        pass_try!(base, scope, buffer.check_is_valid());
4042
4043        base.commands.push(ArcRenderCommand::DrawIndirect {
4044            buffer,
4045            offset,
4046            count: 1,
4047            family: DrawCommandFamily::DrawIndexed,
4048
4049            vertex_or_index_limit: None,
4050            instance_limit: None,
4051        });
4052
4053        Ok(())
4054    }
4055
4056    pub fn draw_indexed_indirect(&mut self, buffer: Arc<Buffer>, offset: BufferAddress) {
4057        if let Err(err) = self.draw_indexed_indirect_inner(buffer, offset) {
4058            self.device
4059                .handle_error(err, self.label(), "RenderPass::draw_indexed_indirect");
4060        }
4061    }
4062
4063    fn draw_mesh_tasks_indirect_inner(
4064        &mut self,
4065        buffer: Arc<Buffer>,
4066        offset: BufferAddress,
4067    ) -> Result<(), RenderPassError> {
4068        let scope = PassErrorScope::Draw {
4069            kind: DrawKind::DrawIndirect,
4070            family: DrawCommandFamily::DrawMeshTasks,
4071        };
4072        let base = pass_base!(self, scope);
4073
4074        pass_try!(base, scope, buffer.check_is_valid());
4075
4076        base.commands.push(ArcRenderCommand::DrawIndirect {
4077            buffer,
4078            offset,
4079            count: 1,
4080            family: DrawCommandFamily::DrawMeshTasks,
4081
4082            vertex_or_index_limit: None,
4083            instance_limit: None,
4084        });
4085
4086        Ok(())
4087    }
4088
4089    pub fn draw_mesh_tasks_indirect(&mut self, buffer: Arc<Buffer>, offset: BufferAddress) {
4090        if let Err(err) = self.draw_mesh_tasks_indirect_inner(buffer, offset) {
4091            self.device
4092                .handle_error(err, self.label(), "RenderPass::draw_mesh_tasks_indirect");
4093        }
4094    }
4095
4096    fn multi_draw_indirect_inner(
4097        &mut self,
4098        buffer: Arc<Buffer>,
4099        offset: BufferAddress,
4100        count: u32,
4101    ) -> Result<(), PassStateError> {
4102        let scope = PassErrorScope::Draw {
4103            kind: DrawKind::MultiDrawIndirect,
4104            family: DrawCommandFamily::Draw,
4105        };
4106        let base = pass_base!(self, scope);
4107
4108        pass_try!(base, scope, buffer.check_is_valid());
4109
4110        base.commands.push(ArcRenderCommand::DrawIndirect {
4111            buffer,
4112            offset,
4113            count,
4114            family: DrawCommandFamily::Draw,
4115
4116            vertex_or_index_limit: None,
4117            instance_limit: None,
4118        });
4119
4120        Ok(())
4121    }
4122
4123    pub fn multi_draw_indirect(&mut self, buffer: Arc<Buffer>, offset: BufferAddress, count: u32) {
4124        if let Err(err) = self.multi_draw_indirect_inner(buffer, offset, count) {
4125            self.device
4126                .handle_error(err, self.label(), "RenderPass::multi_draw_indirect");
4127        }
4128    }
4129
4130    fn multi_draw_indexed_indirect_inner(
4131        &mut self,
4132        buffer: Arc<Buffer>,
4133        offset: BufferAddress,
4134        count: u32,
4135    ) -> Result<(), PassStateError> {
4136        let scope = PassErrorScope::Draw {
4137            kind: DrawKind::MultiDrawIndirect,
4138            family: DrawCommandFamily::DrawIndexed,
4139        };
4140        let base = pass_base!(self, scope);
4141
4142        pass_try!(base, scope, buffer.check_is_valid());
4143
4144        base.commands.push(ArcRenderCommand::DrawIndirect {
4145            buffer,
4146            offset,
4147            count,
4148            family: DrawCommandFamily::DrawIndexed,
4149
4150            vertex_or_index_limit: None,
4151            instance_limit: None,
4152        });
4153
4154        Ok(())
4155    }
4156
4157    pub fn multi_draw_indexed_indirect(
4158        &mut self,
4159        buffer: Arc<Buffer>,
4160        offset: BufferAddress,
4161        count: u32,
4162    ) {
4163        if let Err(err) = self.multi_draw_indexed_indirect_inner(buffer, offset, count) {
4164            self.device
4165                .handle_error(err, self.label(), "RenderPass::multi_draw_indexed_indirect");
4166        }
4167    }
4168
4169    fn multi_draw_mesh_tasks_indirect_inner(
4170        &mut self,
4171        buffer: Arc<Buffer>,
4172        offset: BufferAddress,
4173        count: u32,
4174    ) -> Result<(), RenderPassError> {
4175        let scope = PassErrorScope::Draw {
4176            kind: DrawKind::MultiDrawIndirect,
4177            family: DrawCommandFamily::DrawMeshTasks,
4178        };
4179        let base = pass_base!(self, scope);
4180
4181        pass_try!(base, scope, buffer.check_is_valid());
4182
4183        base.commands.push(ArcRenderCommand::DrawIndirect {
4184            buffer,
4185            offset,
4186            count,
4187            family: DrawCommandFamily::DrawMeshTasks,
4188
4189            vertex_or_index_limit: None,
4190            instance_limit: None,
4191        });
4192
4193        Ok(())
4194    }
4195
4196    pub fn multi_draw_mesh_tasks_indirect(
4197        &mut self,
4198        buffer: Arc<Buffer>,
4199        offset: BufferAddress,
4200        count: u32,
4201    ) {
4202        if let Err(err) = self.multi_draw_mesh_tasks_indirect_inner(buffer, offset, count) {
4203            self.device.handle_error(
4204                err,
4205                self.label(),
4206                "RenderPass::multi_draw_mesh_tasks_indirect",
4207            );
4208        }
4209    }
4210
4211    fn multi_draw_indirect_count_inner(
4212        &mut self,
4213        buffer: Arc<Buffer>,
4214        offset: BufferAddress,
4215        count_buffer: Arc<Buffer>,
4216        count_buffer_offset: BufferAddress,
4217        max_count: u32,
4218    ) -> Result<(), PassStateError> {
4219        let scope = PassErrorScope::Draw {
4220            kind: DrawKind::MultiDrawIndirectCount,
4221            family: DrawCommandFamily::Draw,
4222        };
4223        let base = pass_base!(self, scope);
4224        pass_try!(base, scope, buffer.check_is_valid());
4225        pass_try!(base, scope, count_buffer.check_is_valid());
4226
4227        base.commands
4228            .push(ArcRenderCommand::MultiDrawIndirectCount {
4229                buffer,
4230                offset,
4231                count_buffer,
4232                count_buffer_offset,
4233                max_count,
4234                family: DrawCommandFamily::Draw,
4235            });
4236
4237        Ok(())
4238    }
4239
4240    pub fn multi_draw_indirect_count(
4241        &mut self,
4242        buffer: Arc<Buffer>,
4243        offset: BufferAddress,
4244        count_buffer: Arc<Buffer>,
4245        count_buffer_offset: BufferAddress,
4246        max_count: u32,
4247    ) {
4248        if let Err(err) = self.multi_draw_indirect_count_inner(
4249            buffer,
4250            offset,
4251            count_buffer,
4252            count_buffer_offset,
4253            max_count,
4254        ) {
4255            self.device
4256                .handle_error(err, self.label(), "RenderPass::multi_draw_indirect_count");
4257        }
4258    }
4259
4260    fn multi_draw_indexed_indirect_count_inner(
4261        &mut self,
4262        buffer: Arc<Buffer>,
4263        offset: BufferAddress,
4264        count_buffer: Arc<Buffer>,
4265        count_buffer_offset: BufferAddress,
4266        max_count: u32,
4267    ) -> Result<(), PassStateError> {
4268        let scope = PassErrorScope::Draw {
4269            kind: DrawKind::MultiDrawIndirectCount,
4270            family: DrawCommandFamily::DrawIndexed,
4271        };
4272        let base = pass_base!(self, scope);
4273
4274        pass_try!(base, scope, buffer.check_is_valid());
4275        pass_try!(base, scope, count_buffer.check_is_valid());
4276
4277        base.commands
4278            .push(ArcRenderCommand::MultiDrawIndirectCount {
4279                buffer,
4280                offset,
4281                count_buffer,
4282                count_buffer_offset,
4283                max_count,
4284                family: DrawCommandFamily::DrawIndexed,
4285            });
4286
4287        Ok(())
4288    }
4289
4290    pub fn multi_draw_indexed_indirect_count(
4291        &mut self,
4292        buffer: Arc<Buffer>,
4293        offset: BufferAddress,
4294        count_buffer: Arc<Buffer>,
4295        count_buffer_offset: BufferAddress,
4296        max_count: u32,
4297    ) {
4298        if let Err(err) = self.multi_draw_indexed_indirect_count_inner(
4299            buffer,
4300            offset,
4301            count_buffer,
4302            count_buffer_offset,
4303            max_count,
4304        ) {
4305            self.device.handle_error(
4306                err,
4307                self.label(),
4308                "RenderPass::multi_draw_indexed_indirect_count",
4309            );
4310        }
4311    }
4312
4313    fn multi_draw_mesh_tasks_indirect_count_inner(
4314        &mut self,
4315        buffer: Arc<Buffer>,
4316        offset: BufferAddress,
4317        count_buffer: Arc<Buffer>,
4318        count_buffer_offset: BufferAddress,
4319        max_count: u32,
4320    ) -> Result<(), RenderPassError> {
4321        let scope = PassErrorScope::Draw {
4322            kind: DrawKind::MultiDrawIndirectCount,
4323            family: DrawCommandFamily::DrawMeshTasks,
4324        };
4325        let base = pass_base!(self, scope);
4326
4327        pass_try!(base, scope, buffer.check_is_valid());
4328        pass_try!(base, scope, count_buffer.check_is_valid());
4329
4330        base.commands
4331            .push(ArcRenderCommand::MultiDrawIndirectCount {
4332                buffer,
4333                offset,
4334                count_buffer,
4335                count_buffer_offset,
4336                max_count,
4337                family: DrawCommandFamily::DrawMeshTasks,
4338            });
4339
4340        Ok(())
4341    }
4342
4343    pub fn multi_draw_mesh_tasks_indirect_count(
4344        &mut self,
4345        buffer: Arc<Buffer>,
4346        offset: BufferAddress,
4347        count_buffer: Arc<Buffer>,
4348        count_buffer_offset: BufferAddress,
4349        max_count: u32,
4350    ) {
4351        if let Err(err) = self.multi_draw_mesh_tasks_indirect_count_inner(
4352            buffer,
4353            offset,
4354            count_buffer,
4355            count_buffer_offset,
4356            max_count,
4357        ) {
4358            self.device.handle_error(
4359                err,
4360                self.label(),
4361                "RenderPass::multi_draw_mesh_tasks_indirect_count",
4362            );
4363        }
4364    }
4365
4366    fn push_debug_group_inner(&mut self, label: &str, color: u32) -> Result<(), PassStateError> {
4367        let base = pass_base!(self, PassErrorScope::PushDebugGroup);
4368
4369        let bytes = label.as_bytes();
4370        base.string_data.extend_from_slice(bytes);
4371
4372        base.commands.push(ArcRenderCommand::PushDebugGroup {
4373            color,
4374            len: bytes.len(),
4375        });
4376
4377        Ok(())
4378    }
4379
4380    pub fn push_debug_group(&mut self, label: &str, color: u32) {
4381        if let Err(err) = self.push_debug_group_inner(label, color) {
4382            self.device
4383                .handle_error(err, self.label(), "RenderPass::push_debug_group");
4384        }
4385    }
4386
4387    fn pop_debug_group_inner(&mut self) -> Result<(), PassStateError> {
4388        let base = pass_base!(self, PassErrorScope::PopDebugGroup);
4389
4390        base.commands.push(ArcRenderCommand::PopDebugGroup);
4391
4392        Ok(())
4393    }
4394
4395    pub fn pop_debug_group(&mut self) {
4396        if let Err(err) = self.pop_debug_group_inner() {
4397            self.device
4398                .handle_error(err, self.label(), "RenderPass::pop_debug_group");
4399        }
4400    }
4401
4402    fn insert_debug_marker_inner(&mut self, label: &str, color: u32) -> Result<(), PassStateError> {
4403        let base = pass_base!(self, PassErrorScope::InsertDebugMarker);
4404
4405        let bytes = label.as_bytes();
4406        base.string_data.extend_from_slice(bytes);
4407
4408        base.commands.push(ArcRenderCommand::InsertDebugMarker {
4409            color,
4410            len: bytes.len(),
4411        });
4412
4413        Ok(())
4414    }
4415
4416    pub fn insert_debug_marker(&mut self, label: &str, color: u32) {
4417        if let Err(err) = self.insert_debug_marker_inner(label, color) {
4418            self.device
4419                .handle_error(err, self.label(), "RenderPass::insert_debug_marker");
4420        }
4421    }
4422
4423    fn write_timestamp_inner(
4424        &mut self,
4425        query_set: Arc<QuerySet>,
4426        query_index: u32,
4427    ) -> Result<(), PassStateError> {
4428        let scope = PassErrorScope::WriteTimestamp;
4429        let base = pass_base!(self, scope);
4430
4431        pass_try!(base, scope, query_set.check_is_valid());
4432        base.commands.push(ArcRenderCommand::WriteTimestamp {
4433            query_set,
4434            query_index,
4435        });
4436
4437        Ok(())
4438    }
4439
4440    pub fn write_timestamp(&mut self, query_set: Arc<QuerySet>, query_index: u32) {
4441        if let Err(err) = self.write_timestamp_inner(query_set, query_index) {
4442            self.device
4443                .handle_error(err, self.label(), "RenderPass::write_timestamp");
4444        }
4445    }
4446
4447    fn begin_occlusion_query_inner(&mut self, query_index: u32) -> Result<(), PassStateError> {
4448        let scope = PassErrorScope::BeginOcclusionQuery;
4449        let base = pass_base!(self, scope);
4450
4451        base.commands
4452            .push(ArcRenderCommand::BeginOcclusionQuery { query_index });
4453
4454        Ok(())
4455    }
4456
4457    pub fn begin_occlusion_query(&mut self, query_index: u32) {
4458        if let Err(err) = self.begin_occlusion_query_inner(query_index) {
4459            self.device
4460                .handle_error(err, self.label(), "RenderPass::begin_occlusion_query");
4461        }
4462    }
4463
4464    fn end_occlusion_query_inner(&mut self) -> Result<(), PassStateError> {
4465        let scope = PassErrorScope::EndOcclusionQuery;
4466        let base = pass_base!(self, scope);
4467
4468        base.commands.push(ArcRenderCommand::EndOcclusionQuery);
4469
4470        Ok(())
4471    }
4472
4473    pub fn end_occlusion_query(&mut self) {
4474        if let Err(err) = self.end_occlusion_query_inner() {
4475            self.device
4476                .handle_error(err, self.label(), "RenderPass::end_occlusion_query");
4477        }
4478    }
4479
4480    fn begin_pipeline_statistics_query_inner(
4481        &mut self,
4482        query_set: Arc<QuerySet>,
4483        query_index: u32,
4484    ) -> Result<(), PassStateError> {
4485        let scope = PassErrorScope::BeginPipelineStatisticsQuery;
4486        let base = pass_base!(self, scope);
4487
4488        pass_try!(base, scope, query_set.check_is_valid());
4489        base.commands
4490            .push(ArcRenderCommand::BeginPipelineStatisticsQuery {
4491                query_set,
4492                query_index,
4493            });
4494
4495        Ok(())
4496    }
4497
4498    pub fn begin_pipeline_statistics_query(&mut self, query_set: Arc<QuerySet>, query_index: u32) {
4499        if let Err(err) = self.begin_pipeline_statistics_query_inner(query_set, query_index) {
4500            self.device.handle_error(
4501                err,
4502                self.label(),
4503                "RenderPass::begin_pipeline_statistics_query",
4504            );
4505        }
4506    }
4507
4508    fn end_pipeline_statistics_query_inner(&mut self) -> Result<(), PassStateError> {
4509        let scope = PassErrorScope::EndPipelineStatisticsQuery;
4510        let base = pass_base!(self, scope);
4511
4512        base.commands
4513            .push(ArcRenderCommand::EndPipelineStatisticsQuery);
4514
4515        Ok(())
4516    }
4517
4518    pub fn end_pipeline_statistics_query(&mut self) {
4519        if let Err(err) = self.end_pipeline_statistics_query_inner() {
4520            self.device.handle_error(
4521                err,
4522                self.label(),
4523                "RenderPass::end_pipeline_statistics_query",
4524            );
4525        }
4526    }
4527
4528    fn execute_bundles_inner(
4529        &mut self,
4530        render_bundles: &[Arc<RenderBundle>],
4531    ) -> Result<(), PassStateError> {
4532        let scope = PassErrorScope::ExecuteBundle;
4533        let base = pass_base!(self, scope);
4534
4535        for bundle in render_bundles {
4536            pass_try!(base, scope, bundle.check_is_valid());
4537
4538            base.commands
4539                .push(ArcRenderCommand::ExecuteBundle(bundle.clone()));
4540        }
4541        self.current_pipeline.reset();
4542        self.current_bind_groups.reset();
4543
4544        Ok(())
4545    }
4546
4547    pub fn execute_bundles(&mut self, render_bundles: &[Arc<RenderBundle>]) {
4548        if let Err(err) = self.execute_bundles_inner(render_bundles) {
4549            self.device
4550                .handle_error(err, self.label(), "RenderPass::execute_bundles");
4551        }
4552    }
4553}
4554
4555pub(crate) const fn get_src_stride_of_indirect_args(family: DrawCommandFamily) -> u64 {
4556    match family {
4557        DrawCommandFamily::Draw => size_of::<wgt::DrawIndirectArgs>() as u64,
4558        DrawCommandFamily::DrawIndexed => size_of::<wgt::DrawIndexedIndirectArgs>() as u64,
4559        DrawCommandFamily::DrawMeshTasks => size_of::<wgt::DispatchIndirectArgs>() as u64,
4560    }
4561}
4562
4563pub(crate) const fn get_dst_stride_of_indirect_args(
4564    backend: wgt::Backend,
4565    family: DrawCommandFamily,
4566) -> u64 {
4567    // space for D3D12 special constants
4568    let extra = if matches!(backend, wgt::Backend::Dx12) {
4569        3 * size_of::<u32>() as u64
4570    } else {
4571        0
4572    };
4573    extra + get_src_stride_of_indirect_args(family)
4574}