Skip to main content

wgpu_core/command/
render.rs

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