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