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