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
3231#[inline(never)]
3232fn multi_draw_indirect(
3233 state: &mut State,
3234 indirect_draw_validation_batcher: &mut crate::indirect_validation::DrawBatcher,
3235 device: &Arc<Device>,
3236 indirect_buffer: Arc<Buffer>,
3237 offset: u64,
3238 count: u32,
3239 family: DrawCommandFamily,
3240) -> Result<(), RenderPassErrorInner> {
3241 api_log!(
3242 "RenderPass::draw_indirect (family:{family:?}) {} {offset} {count:?}",
3243 indirect_buffer.error_ident()
3244 );
3245
3246 state.is_ready(family)?;
3247 state.flush_vertex_buffers()?;
3248 state.flush_bindings()?;
3249
3250 if family == DrawCommandFamily::DrawMeshTasks {
3251 validate_mesh_draw_multiview(state)?;
3252 }
3253
3254 state
3255 .pass
3256 .base
3257 .device
3258 .require_downlevel_flags(wgt::DownlevelFlags::INDIRECT_EXECUTION)?;
3259
3260 indirect_buffer.same_device(device)?;
3261 indirect_buffer.check_usage(BufferUsages::INDIRECT)?;
3262 indirect_buffer.check_destroyed(state.pass.base.snatch_guard)?;
3263
3264 if !offset.is_multiple_of(4) {
3265 return Err(RenderPassErrorInner::UnalignedIndirectBufferOffset(offset));
3266 }
3267
3268 let stride = get_src_stride_of_indirect_args(family);
3269 let args_size = match stride.checked_mul(u64::from(count)) {
3270 Some(sz) if sz <= indirect_buffer.size && indirect_buffer.size - sz >= offset => sz,
3271 args_size => {
3272 return Err(RenderPassErrorInner::IndirectBufferOverrun {
3273 count,
3274 offset,
3275 args_size: args_size.unwrap_or(u64::MAX),
3276 buffer_size: indirect_buffer.size,
3277 });
3278 }
3279 };
3280
3281 state.pass.base.buffer_memory_init_actions.extend(
3282 indirect_buffer.initialization_status.read().create_action(
3283 &indirect_buffer,
3284 offset..offset + args_size,
3285 MemoryInitKind::NeedsInitializedMemory,
3286 ),
3287 );
3288
3289 fn draw(
3290 raw_encoder: &mut dyn hal::DynCommandEncoder,
3291 family: DrawCommandFamily,
3292 indirect_buffer: &dyn hal::DynBuffer,
3293 offset: u64,
3294 count: u32,
3295 ) {
3296 match family {
3297 DrawCommandFamily::Draw => unsafe {
3298 raw_encoder.draw_indirect(indirect_buffer, offset, count);
3299 },
3300 DrawCommandFamily::DrawIndexed => unsafe {
3301 raw_encoder.draw_indexed_indirect(indirect_buffer, offset, count);
3302 },
3303 DrawCommandFamily::DrawMeshTasks => unsafe {
3304 raw_encoder.draw_mesh_tasks_indirect(indirect_buffer, offset, count);
3305 },
3306 }
3307 }
3308
3309 if state.pass.base.device.indirect_validation.is_some()
3310 && family != DrawCommandFamily::DrawMeshTasks
3311 {
3312 state
3313 .pass
3314 .scope
3315 .buffers
3316 .merge_single(&indirect_buffer, wgt::BufferUses::STORAGE_READ_ONLY)?;
3317
3318 struct DrawData {
3319 buffer_index: usize,
3320 offset: u64,
3321 count: u32,
3322 }
3323
3324 struct DrawContext<'a> {
3325 raw_encoder: &'a mut dyn hal::DynCommandEncoder,
3326 device: &'a Device,
3327
3328 indirect_draw_validation_resources: &'a mut crate::indirect_validation::DrawResources,
3329 indirect_draw_validation_batcher: &'a mut crate::indirect_validation::DrawBatcher,
3330
3331 indirect_buffer: Arc<Buffer>,
3332 family: DrawCommandFamily,
3333 vertex_or_index_limit: u64,
3334 instance_limit: u64,
3335 }
3336
3337 impl<'a> DrawContext<'a> {
3338 fn add(&mut self, offset: u64) -> Result<DrawData, DeviceError> {
3339 let (dst_resource_index, dst_offset) = self.indirect_draw_validation_batcher.add(
3340 self.indirect_draw_validation_resources,
3341 self.device,
3342 &self.indirect_buffer,
3343 offset,
3344 self.family,
3345 self.vertex_or_index_limit,
3346 self.instance_limit,
3347 )?;
3348 Ok(DrawData {
3349 buffer_index: dst_resource_index,
3350 offset: dst_offset,
3351 count: 1,
3352 })
3353 }
3354 fn draw(&mut self, draw_data: DrawData) {
3355 let dst_buffer = self
3356 .indirect_draw_validation_resources
3357 .get_dst_buffer(draw_data.buffer_index);
3358 draw(
3359 self.raw_encoder,
3360 self.family,
3361 dst_buffer,
3362 draw_data.offset,
3363 draw_data.count,
3364 );
3365 }
3366 }
3367
3368 let mut draw_ctx = DrawContext {
3369 raw_encoder: state.pass.base.raw_encoder,
3370 device: state.pass.base.device,
3371 indirect_draw_validation_resources: state.pass.base.indirect_draw_validation_resources,
3372 indirect_draw_validation_batcher,
3373 indirect_buffer,
3374 family,
3375 vertex_or_index_limit: if family == DrawCommandFamily::DrawIndexed {
3376 state.index.limit
3377 } else {
3378 state.vertex.limits.vertex_limit
3379 },
3380 instance_limit: state.vertex.limits.instance_limit,
3381 };
3382
3383 let mut current_draw_data = draw_ctx.add(offset)?;
3384
3385 for i in 1..count {
3386 let draw_data = draw_ctx.add(offset + stride * i as u64)?;
3387
3388 if draw_data.buffer_index == current_draw_data.buffer_index {
3389 #[cfg(debug_assertions)]
3390 {
3391 let dst_stride =
3392 get_dst_stride_of_indirect_args(state.pass.base.device.backend(), family);
3393 debug_assert_eq!(
3394 draw_data.offset,
3395 current_draw_data.offset + dst_stride * current_draw_data.count as u64
3396 );
3397 }
3398 current_draw_data.count += 1;
3399 } else {
3400 draw_ctx.draw(current_draw_data);
3401 current_draw_data = draw_data;
3402 }
3403 }
3404
3405 draw_ctx.draw(current_draw_data);
3406 } else {
3407 state
3408 .pass
3409 .scope
3410 .buffers
3411 .merge_single(&indirect_buffer, wgt::BufferUses::INDIRECT)?;
3412
3413 draw(
3414 state.pass.base.raw_encoder,
3415 family,
3416 indirect_buffer.try_raw(state.pass.base.snatch_guard)?,
3417 offset,
3418 count,
3419 );
3420 };
3421
3422 Ok(())
3423}
3424
3425#[inline(never)]
3426fn multi_draw_indirect_count(
3427 state: &mut State,
3428 device: &Arc<Device>,
3429 indirect_buffer: Arc<Buffer>,
3430 offset: u64,
3431 count_buffer: Arc<Buffer>,
3432 count_buffer_offset: u64,
3433 max_count: u32,
3434 family: DrawCommandFamily,
3435) -> Result<(), RenderPassErrorInner> {
3436 api_log!(
3437 "RenderPass::multi_draw_indirect_count (family:{family:?}) {} {offset} {} {count_buffer_offset:?} {max_count:?}",
3438 indirect_buffer.error_ident(),
3439 count_buffer.error_ident()
3440 );
3441
3442 state.is_ready(family)?;
3443 state.flush_vertex_buffers()?;
3444 state.flush_bindings()?;
3445
3446 if family == DrawCommandFamily::DrawMeshTasks {
3447 validate_mesh_draw_multiview(state)?;
3448 }
3449
3450 let stride = get_src_stride_of_indirect_args(family);
3451
3452 state
3453 .pass
3454 .base
3455 .device
3456 .require_features(wgt::Features::MULTI_DRAW_INDIRECT_COUNT)?;
3457 state
3458 .pass
3459 .base
3460 .device
3461 .require_downlevel_flags(wgt::DownlevelFlags::INDIRECT_EXECUTION)?;
3462
3463 indirect_buffer.same_device(device)?;
3464 count_buffer.same_device(device)?;
3465
3466 state
3467 .pass
3468 .scope
3469 .buffers
3470 .merge_single(&indirect_buffer, wgt::BufferUses::INDIRECT)?;
3471
3472 indirect_buffer.check_usage(BufferUsages::INDIRECT)?;
3473 let indirect_raw = indirect_buffer.try_raw(state.pass.base.snatch_guard)?;
3474
3475 state
3476 .pass
3477 .scope
3478 .buffers
3479 .merge_single(&count_buffer, wgt::BufferUses::INDIRECT)?;
3480
3481 count_buffer.check_usage(BufferUsages::INDIRECT)?;
3482 let count_raw = count_buffer.try_raw(state.pass.base.snatch_guard)?;
3483
3484 if !offset.is_multiple_of(4) {
3485 return Err(RenderPassErrorInner::UnalignedIndirectBufferOffset(offset));
3486 }
3487
3488 let args_size = match stride.checked_mul(u64::from(max_count)) {
3489 Some(sz) if sz <= indirect_buffer.size && indirect_buffer.size - sz >= offset => sz,
3490 args_size => {
3491 return Err(RenderPassErrorInner::IndirectBufferOverrun {
3492 count: 1,
3493 offset,
3494 args_size: args_size.unwrap_or(u64::MAX),
3495 buffer_size: indirect_buffer.size,
3496 });
3497 }
3498 };
3499
3500 state.pass.base.buffer_memory_init_actions.extend(
3501 indirect_buffer.initialization_status.read().create_action(
3502 &indirect_buffer,
3503 offset..offset + args_size,
3504 MemoryInitKind::NeedsInitializedMemory,
3505 ),
3506 );
3507
3508 let begin_count_offset = count_buffer_offset;
3509 let count_bytes = 4;
3510 if count_buffer.size < count_bytes || count_buffer.size - count_bytes < count_buffer_offset {
3511 return Err(RenderPassErrorInner::IndirectCountBufferOverrun {
3512 begin_count_offset,
3513 count_bytes: 4,
3514 count_buffer_size: count_buffer.size,
3515 });
3516 }
3517 state.pass.base.buffer_memory_init_actions.extend(
3518 count_buffer.initialization_status.read().create_action(
3519 &count_buffer,
3520 count_buffer_offset..count_buffer_offset + count_bytes,
3521 MemoryInitKind::NeedsInitializedMemory,
3522 ),
3523 );
3524
3525 match family {
3526 DrawCommandFamily::Draw => unsafe {
3527 state.pass.base.raw_encoder.draw_indirect_count(
3528 indirect_raw,
3529 offset,
3530 count_raw,
3531 count_buffer_offset,
3532 max_count,
3533 );
3534 },
3535 DrawCommandFamily::DrawIndexed => unsafe {
3536 state.pass.base.raw_encoder.draw_indexed_indirect_count(
3537 indirect_raw,
3538 offset,
3539 count_raw,
3540 count_buffer_offset,
3541 max_count,
3542 );
3543 },
3544 DrawCommandFamily::DrawMeshTasks => unsafe {
3545 state.pass.base.raw_encoder.draw_mesh_tasks_indirect_count(
3546 indirect_raw,
3547 offset,
3548 count_raw,
3549 count_buffer_offset,
3550 max_count,
3551 );
3552 },
3553 }
3554 Ok(())
3555}
3556
3557fn execute_bundle(
3558 state: &mut State,
3559 indirect_draw_validation_batcher: &mut crate::indirect_validation::DrawBatcher,
3560 device: &Arc<Device>,
3561 bundle: Arc<super::RenderBundle>,
3562) -> Result<(), RenderPassErrorInner> {
3563 api_log!("RenderPass::execute_bundle {}", bundle.error_ident());
3564
3565 let bundle = state.pass.base.tracker.bundles.insert_single(bundle);
3566
3567 bundle.same_device(device)?;
3568
3569 state
3570 .info
3571 .context
3572 .check_compatible(&bundle.context, bundle.as_ref())
3573 .map_err(RenderPassErrorInner::IncompatibleBundleTargets)?;
3574
3575 if (state.info.is_depth_read_only && !bundle.is_depth_read_only)
3576 || (state.info.is_stencil_read_only && !bundle.is_stencil_read_only)
3577 {
3578 return Err(
3579 RenderPassErrorInner::IncompatibleBundleReadOnlyDepthStencil {
3580 pass_depth: state.info.is_depth_read_only,
3581 pass_stencil: state.info.is_stencil_read_only,
3582 bundle_depth: bundle.is_depth_read_only,
3583 bundle_stencil: bundle.is_stencil_read_only,
3584 },
3585 );
3586 }
3587
3588 state.pass.base.buffer_memory_init_actions.extend(
3589 bundle
3590 .buffer_memory_init_actions
3591 .iter()
3592 .filter_map(|action| {
3593 action
3594 .buffer
3595 .initialization_status
3596 .read()
3597 .check_action(action)
3598 }),
3599 );
3600 for action in bundle.texture_memory_init_actions.iter() {
3601 state.pass.pending_discard_init_fixups.extend(
3602 state
3603 .pass
3604 .base
3605 .texture_memory_actions
3606 .register_init_action(action),
3607 );
3608 }
3609
3610 unsafe {
3611 bundle.execute(
3612 state.pass.base.raw_encoder,
3613 state.pass.base.indirect_draw_validation_resources,
3614 indirect_draw_validation_batcher,
3615 state.pass.base.snatch_guard,
3616 )
3617 }
3618 .map_err(|e| match e {
3619 ExecutionError::Device(e) => RenderPassErrorInner::Device(e),
3620 ExecutionError::DestroyedResource(e) => {
3621 RenderPassErrorInner::RenderCommand(RenderCommandError::DestroyedResource(e))
3622 }
3623 ExecutionError::Unimplemented(what) => {
3624 RenderPassErrorInner::RenderCommand(RenderCommandError::Unimplemented(what))
3625 }
3626 })?;
3627
3628 unsafe {
3629 state.pass.scope.merge_render_bundle(&bundle.used)?;
3630 };
3631 state.reset_bundle();
3632 Ok(())
3633}
3634
3635impl Global {
3648 pub fn render_pass_set_bind_group(
3649 &self,
3650 pass: &mut RenderPass,
3651 index: u32,
3652 bind_group_id: Option<id::BindGroupId>,
3653 offsets: &[DynamicOffset],
3654 ) -> Result<(), PassStateError> {
3655 let scope = PassErrorScope::SetBindGroup;
3656
3657 let base = pass_base!(pass, scope);
3661
3662 if pass.current_bind_groups.set_and_check_redundant(
3663 bind_group_id,
3664 index,
3665 &mut base.dynamic_offsets,
3666 offsets,
3667 ) {
3668 return Ok(());
3669 }
3670
3671 let mut bind_group = None;
3672 if let Some(bind_group_id) = bind_group_id {
3673 let hub = &self.hub;
3674 bind_group = Some(pass_try!(
3675 base,
3676 scope,
3677 hub.bind_groups.get(bind_group_id).get(),
3678 ));
3679 }
3680
3681 base.commands.push(ArcRenderCommand::SetBindGroup {
3682 index,
3683 num_dynamic_offsets: offsets.len(),
3684 bind_group,
3685 });
3686
3687 Ok(())
3688 }
3689
3690 pub fn render_pass_set_bind_group_with_id(
3691 &self,
3692 pass: id::RenderPassEncoderId,
3693 index: u32,
3694 bind_group_id: Option<id::BindGroupId>,
3695 offsets: &[DynamicOffset],
3696 ) -> Result<(), PassStateError> {
3697 let pass = self.hub.render_passes.get(pass);
3698 let mut pass = pass
3699 .try_lock()
3700 .expect("RenderPasses should not be used concurrently");
3701 self.render_pass_set_bind_group(&mut pass, index, bind_group_id, offsets)
3702 }
3703
3704 pub fn render_pass_set_pipeline(
3705 &self,
3706 pass: &mut RenderPass,
3707 pipeline_id: id::RenderPipelineId,
3708 ) -> Result<(), PassStateError> {
3709 let scope = PassErrorScope::SetPipelineRender;
3710
3711 let redundant = pass.current_pipeline.set_and_check_redundant(pipeline_id);
3712
3713 let base = pass_base!(pass, scope);
3716
3717 if redundant {
3718 return Ok(());
3719 }
3720
3721 let hub = &self.hub;
3722 let pipeline = hub.render_pipelines.get(pipeline_id);
3723 pass_try!(base, scope, pipeline.check_valid());
3724
3725 base.commands.push(ArcRenderCommand::SetPipeline(pipeline));
3726
3727 Ok(())
3728 }
3729
3730 pub fn render_pass_set_pipeline_with_id(
3731 &self,
3732 pass: id::RenderPassEncoderId,
3733 pipeline_id: id::RenderPipelineId,
3734 ) -> Result<(), PassStateError> {
3735 let pass = self.hub.render_passes.get(pass);
3736 let mut pass = pass
3737 .try_lock()
3738 .expect("RenderPasses should not be used concurrently");
3739 self.render_pass_set_pipeline(&mut pass, pipeline_id)
3740 }
3741
3742 pub fn render_pass_set_index_buffer(
3743 &self,
3744 pass: &mut RenderPass,
3745 buffer_id: id::BufferId,
3746 index_format: IndexFormat,
3747 offset: BufferAddress,
3748 size: Option<BufferSize>,
3749 ) -> Result<(), PassStateError> {
3750 let scope = PassErrorScope::SetIndexBuffer;
3751 let base = pass_base!(pass, scope);
3752
3753 base.commands.push(ArcRenderCommand::SetIndexBuffer {
3754 buffer: pass_try!(base, scope, self.resolve_buffer_id(buffer_id)),
3755 index_format,
3756 offset,
3757 size,
3758 });
3759
3760 Ok(())
3761 }
3762
3763 pub fn render_pass_set_index_buffer_with_id(
3764 &self,
3765 pass: id::RenderPassEncoderId,
3766 buffer_id: id::BufferId,
3767 index_format: IndexFormat,
3768 offset: BufferAddress,
3769 size: Option<BufferSize>,
3770 ) -> Result<(), PassStateError> {
3771 let pass = self.hub.render_passes.get(pass);
3772 let mut pass = pass
3773 .try_lock()
3774 .expect("RenderPasses should not be used concurrently");
3775 self.render_pass_set_index_buffer(&mut pass, buffer_id, index_format, offset, size)
3776 }
3777
3778 pub fn render_pass_set_vertex_buffer(
3779 &self,
3780 pass: &mut RenderPass,
3781 slot: u32,
3782 buffer_id: Option<id::BufferId>,
3783 offset: BufferAddress,
3784 size: Option<BufferSize>,
3785 ) -> Result<(), PassStateError> {
3786 let scope = PassErrorScope::SetVertexBuffer;
3787 let base = pass_base!(pass, scope);
3788
3789 let buffer = if let Some(buffer_id) = buffer_id {
3790 Some(pass_try!(base, scope, self.resolve_buffer_id(buffer_id)))
3791 } else {
3792 None
3793 };
3794
3795 base.commands.push(ArcRenderCommand::SetVertexBuffer {
3796 slot,
3797 buffer,
3798 offset,
3799 size,
3800 });
3801
3802 Ok(())
3803 }
3804
3805 pub fn render_pass_set_vertex_buffer_with_id(
3806 &self,
3807 pass: id::RenderPassEncoderId,
3808 slot: u32,
3809 buffer_id: Option<id::BufferId>,
3810 offset: BufferAddress,
3811 size: Option<BufferSize>,
3812 ) -> Result<(), PassStateError> {
3813 let pass = self.hub.render_passes.get(pass);
3814 let mut pass = pass
3815 .try_lock()
3816 .expect("RenderPasses should not be used concurrently");
3817 self.render_pass_set_vertex_buffer(&mut pass, slot, buffer_id, offset, size)
3818 }
3819
3820 pub fn render_pass_set_blend_constant(
3821 &self,
3822 pass: &mut RenderPass,
3823 color: Color,
3824 ) -> Result<(), PassStateError> {
3825 let scope = PassErrorScope::SetBlendConstant;
3826 let base = pass_base!(pass, scope);
3827
3828 base.commands
3829 .push(ArcRenderCommand::SetBlendConstant(color));
3830
3831 Ok(())
3832 }
3833
3834 pub fn render_pass_set_blend_constant_with_id(
3835 &self,
3836 pass: id::RenderPassEncoderId,
3837 color: Color,
3838 ) -> Result<(), PassStateError> {
3839 let pass = self.hub.render_passes.get(pass);
3840 let mut pass = pass
3841 .try_lock()
3842 .expect("RenderPasses should not be used concurrently");
3843 self.render_pass_set_blend_constant(&mut pass, color)
3844 }
3845
3846 pub fn render_pass_set_stencil_reference(
3847 &self,
3848 pass: &mut RenderPass,
3849 value: u32,
3850 ) -> Result<(), PassStateError> {
3851 let scope = PassErrorScope::SetStencilReference;
3852 let base = pass_base!(pass, scope);
3853 let value = convert_stencil_value(
3854 value,
3855 pass.depth_stencil_attachment
3856 .as_ref()
3857 .map(|at| at.view.desc.format),
3858 );
3859 base.commands
3860 .push(ArcRenderCommand::SetStencilReference(value));
3861
3862 Ok(())
3863 }
3864
3865 pub fn render_pass_set_stencil_reference_with_id(
3866 &self,
3867 pass: id::RenderPassEncoderId,
3868 value: u32,
3869 ) -> Result<(), PassStateError> {
3870 let pass = self.hub.render_passes.get(pass);
3871 let mut pass = pass
3872 .try_lock()
3873 .expect("RenderPasses should not be used concurrently");
3874 self.render_pass_set_stencil_reference(&mut pass, value)
3875 }
3876
3877 pub fn render_pass_set_viewport(
3878 &self,
3879 pass: &mut RenderPass,
3880 x: f32,
3881 y: f32,
3882 w: f32,
3883 h: f32,
3884 depth_min: f32,
3885 depth_max: f32,
3886 ) -> Result<(), PassStateError> {
3887 let scope = PassErrorScope::SetViewport;
3888 let base = pass_base!(pass, scope);
3889
3890 base.commands.push(ArcRenderCommand::SetViewport {
3891 rect: Rect { x, y, w, h },
3892 depth_min,
3893 depth_max,
3894 });
3895
3896 Ok(())
3897 }
3898
3899 pub fn render_pass_set_viewport_with_id(
3900 &self,
3901 pass: id::RenderPassEncoderId,
3902 x: f32,
3903 y: f32,
3904 w: f32,
3905 h: f32,
3906 depth_min: f32,
3907 depth_max: f32,
3908 ) -> Result<(), PassStateError> {
3909 let pass = self.hub.render_passes.get(pass);
3910 let mut pass = pass
3911 .try_lock()
3912 .expect("RenderPasses should not be used concurrently");
3913 self.render_pass_set_viewport(&mut pass, x, y, w, h, depth_min, depth_max)
3914 }
3915
3916 pub fn render_pass_set_scissor_rect(
3917 &self,
3918 pass: &mut RenderPass,
3919 x: u32,
3920 y: u32,
3921 w: u32,
3922 h: u32,
3923 ) -> Result<(), PassStateError> {
3924 let scope = PassErrorScope::SetScissorRect;
3925 let base = pass_base!(pass, scope);
3926
3927 base.commands
3928 .push(ArcRenderCommand::SetScissor(Rect { x, y, w, h }));
3929
3930 Ok(())
3931 }
3932
3933 pub fn render_pass_set_scissor_rect_with_id(
3934 &self,
3935 pass: id::RenderPassEncoderId,
3936 x: u32,
3937 y: u32,
3938 w: u32,
3939 h: u32,
3940 ) -> Result<(), PassStateError> {
3941 let pass = self.hub.render_passes.get(pass);
3942 let mut pass = pass
3943 .try_lock()
3944 .expect("RenderPasses should not be used concurrently");
3945 self.render_pass_set_scissor_rect(&mut pass, x, y, w, h)
3946 }
3947
3948 pub fn render_pass_set_immediates(
3949 &self,
3950 pass: &mut RenderPass,
3951 offset: u32,
3952 data: &[u8],
3953 ) -> Result<(), PassStateError> {
3954 let scope = PassErrorScope::SetImmediate;
3955 let base = pass_base!(pass, scope);
3956
3957 let size_bytes = pass_try!(
3958 base,
3959 scope,
3960 u32::try_from(data.len()).map_err(|_| ImmediateUploadError::ImmediateOutOfMemory)
3961 );
3962 pass_try!(
3963 base,
3964 scope,
3965 pass::validate_immediates_alignment(offset, size_bytes)
3966 );
3967
3968 let values_offset = base.immediates_data.len().try_into().unwrap();
3969
3970 base.immediates_data.extend(
3971 data.chunks_exact(wgt::IMMEDIATE_DATA_ALIGNMENT as usize)
3972 .map(|arr| u32::from_ne_bytes([arr[0], arr[1], arr[2], arr[3]])),
3973 );
3974
3975 base.commands.push(ArcRenderCommand::SetImmediate {
3976 offset,
3977 size_bytes: data.len() as u32,
3978 values_offset: Some(values_offset),
3979 });
3980
3981 Ok(())
3982 }
3983
3984 pub fn render_pass_set_immediates_with_id(
3985 &self,
3986 pass: id::RenderPassEncoderId,
3987 offset: u32,
3988 data: &[u8],
3989 ) -> Result<(), PassStateError> {
3990 let pass = self.hub.render_passes.get(pass);
3991 let mut pass = pass
3992 .try_lock()
3993 .expect("RenderPasses should not be used concurrently");
3994 self.render_pass_set_immediates(&mut pass, offset, data)
3995 }
3996
3997 pub fn render_pass_draw(
3998 &self,
3999 pass: &mut RenderPass,
4000 vertex_count: u32,
4001 instance_count: u32,
4002 first_vertex: u32,
4003 first_instance: u32,
4004 ) -> Result<(), PassStateError> {
4005 let scope = PassErrorScope::Draw {
4006 kind: DrawKind::Draw,
4007 family: DrawCommandFamily::Draw,
4008 };
4009 let base = pass_base!(pass, scope);
4010
4011 base.commands.push(ArcRenderCommand::Draw {
4012 vertex_count,
4013 instance_count,
4014 first_vertex,
4015 first_instance,
4016 });
4017
4018 Ok(())
4019 }
4020
4021 pub fn render_pass_draw_with_id(
4022 &self,
4023 pass: id::RenderPassEncoderId,
4024 vertex_count: u32,
4025 instance_count: u32,
4026 first_vertex: u32,
4027 first_instance: u32,
4028 ) -> Result<(), PassStateError> {
4029 let pass = self.hub.render_passes.get(pass);
4030 let mut pass = pass
4031 .try_lock()
4032 .expect("RenderPasses should not be used concurrently");
4033 self.render_pass_draw(
4034 &mut pass,
4035 vertex_count,
4036 instance_count,
4037 first_vertex,
4038 first_instance,
4039 )
4040 }
4041
4042 pub fn render_pass_draw_indexed(
4043 &self,
4044 pass: &mut RenderPass,
4045 index_count: u32,
4046 instance_count: u32,
4047 first_index: u32,
4048 base_vertex: i32,
4049 first_instance: u32,
4050 ) -> Result<(), PassStateError> {
4051 let scope = PassErrorScope::Draw {
4052 kind: DrawKind::Draw,
4053 family: DrawCommandFamily::DrawIndexed,
4054 };
4055 let base = pass_base!(pass, scope);
4056
4057 base.commands.push(ArcRenderCommand::DrawIndexed {
4058 index_count,
4059 instance_count,
4060 first_index,
4061 base_vertex,
4062 first_instance,
4063 });
4064
4065 Ok(())
4066 }
4067
4068 pub fn render_pass_draw_indexed_with_id(
4069 &self,
4070 pass: id::RenderPassEncoderId,
4071 index_count: u32,
4072 instance_count: u32,
4073 first_index: u32,
4074 base_vertex: i32,
4075 first_instance: u32,
4076 ) -> Result<(), PassStateError> {
4077 let pass = self.hub.render_passes.get(pass);
4078 let mut pass = pass
4079 .try_lock()
4080 .expect("RenderPasses should not be used concurrently");
4081 self.render_pass_draw_indexed(
4082 &mut pass,
4083 index_count,
4084 instance_count,
4085 first_index,
4086 base_vertex,
4087 first_instance,
4088 )
4089 }
4090
4091 pub fn render_pass_draw_mesh_tasks(
4092 &self,
4093 pass: &mut RenderPass,
4094 group_count_x: u32,
4095 group_count_y: u32,
4096 group_count_z: u32,
4097 ) -> Result<(), RenderPassError> {
4098 let scope = PassErrorScope::Draw {
4099 kind: DrawKind::Draw,
4100 family: DrawCommandFamily::DrawMeshTasks,
4101 };
4102 let base = pass_base!(pass, scope);
4103
4104 base.commands.push(ArcRenderCommand::DrawMeshTasks {
4105 group_count_x,
4106 group_count_y,
4107 group_count_z,
4108 });
4109 Ok(())
4110 }
4111
4112 pub fn render_pass_draw_indirect(
4113 &self,
4114 pass: &mut RenderPass,
4115 buffer_id: id::BufferId,
4116 offset: BufferAddress,
4117 ) -> Result<(), PassStateError> {
4118 let scope = PassErrorScope::Draw {
4119 kind: DrawKind::DrawIndirect,
4120 family: DrawCommandFamily::Draw,
4121 };
4122 let base = pass_base!(pass, scope);
4123
4124 base.commands.push(ArcRenderCommand::DrawIndirect {
4125 buffer: pass_try!(base, scope, self.resolve_buffer_id(buffer_id)),
4126 offset,
4127 count: 1,
4128 family: DrawCommandFamily::Draw,
4129
4130 vertex_or_index_limit: None,
4131 instance_limit: None,
4132 });
4133
4134 Ok(())
4135 }
4136
4137 pub fn render_pass_draw_indirect_with_id(
4138 &self,
4139 pass: id::RenderPassEncoderId,
4140 buffer_id: id::BufferId,
4141 offset: BufferAddress,
4142 ) -> Result<(), PassStateError> {
4143 let pass = self.hub.render_passes.get(pass);
4144 let mut pass = pass
4145 .try_lock()
4146 .expect("RenderPasses should not be used concurrently");
4147 self.render_pass_draw_indirect(&mut pass, buffer_id, offset)
4148 }
4149
4150 pub fn render_pass_draw_indexed_indirect(
4151 &self,
4152 pass: &mut RenderPass,
4153 buffer_id: id::BufferId,
4154 offset: BufferAddress,
4155 ) -> Result<(), PassStateError> {
4156 let scope = PassErrorScope::Draw {
4157 kind: DrawKind::DrawIndirect,
4158 family: DrawCommandFamily::DrawIndexed,
4159 };
4160 let base = pass_base!(pass, scope);
4161
4162 base.commands.push(ArcRenderCommand::DrawIndirect {
4163 buffer: pass_try!(base, scope, self.resolve_buffer_id(buffer_id)),
4164 offset,
4165 count: 1,
4166 family: DrawCommandFamily::DrawIndexed,
4167
4168 vertex_or_index_limit: None,
4169 instance_limit: None,
4170 });
4171
4172 Ok(())
4173 }
4174
4175 pub fn render_pass_draw_indexed_indirect_with_id(
4176 &self,
4177 pass: id::RenderPassEncoderId,
4178 buffer_id: id::BufferId,
4179 offset: BufferAddress,
4180 ) -> Result<(), PassStateError> {
4181 let pass = self.hub.render_passes.get(pass);
4182 let mut pass = pass
4183 .try_lock()
4184 .expect("RenderPasses should not be used concurrently");
4185 self.render_pass_draw_indexed_indirect(&mut pass, buffer_id, offset)
4186 }
4187
4188 pub fn render_pass_draw_mesh_tasks_indirect(
4189 &self,
4190 pass: &mut RenderPass,
4191 buffer_id: id::BufferId,
4192 offset: BufferAddress,
4193 ) -> Result<(), RenderPassError> {
4194 let scope = PassErrorScope::Draw {
4195 kind: DrawKind::DrawIndirect,
4196 family: DrawCommandFamily::DrawMeshTasks,
4197 };
4198 let base = pass_base!(pass, scope);
4199
4200 base.commands.push(ArcRenderCommand::DrawIndirect {
4201 buffer: pass_try!(base, scope, self.resolve_buffer_id(buffer_id)),
4202 offset,
4203 count: 1,
4204 family: DrawCommandFamily::DrawMeshTasks,
4205
4206 vertex_or_index_limit: None,
4207 instance_limit: None,
4208 });
4209
4210 Ok(())
4211 }
4212
4213 pub fn render_pass_multi_draw_indirect(
4214 &self,
4215 pass: &mut RenderPass,
4216 buffer_id: id::BufferId,
4217 offset: BufferAddress,
4218 count: u32,
4219 ) -> Result<(), PassStateError> {
4220 let scope = PassErrorScope::Draw {
4221 kind: DrawKind::MultiDrawIndirect,
4222 family: DrawCommandFamily::Draw,
4223 };
4224 let base = pass_base!(pass, scope);
4225
4226 base.commands.push(ArcRenderCommand::DrawIndirect {
4227 buffer: pass_try!(base, scope, self.resolve_buffer_id(buffer_id)),
4228 offset,
4229 count,
4230 family: DrawCommandFamily::Draw,
4231
4232 vertex_or_index_limit: None,
4233 instance_limit: None,
4234 });
4235
4236 Ok(())
4237 }
4238
4239 pub fn render_pass_multi_draw_indexed_indirect(
4240 &self,
4241 pass: &mut RenderPass,
4242 buffer_id: id::BufferId,
4243 offset: BufferAddress,
4244 count: u32,
4245 ) -> Result<(), PassStateError> {
4246 let scope = PassErrorScope::Draw {
4247 kind: DrawKind::MultiDrawIndirect,
4248 family: DrawCommandFamily::DrawIndexed,
4249 };
4250 let base = pass_base!(pass, scope);
4251
4252 base.commands.push(ArcRenderCommand::DrawIndirect {
4253 buffer: pass_try!(base, scope, self.resolve_buffer_id(buffer_id)),
4254 offset,
4255 count,
4256 family: DrawCommandFamily::DrawIndexed,
4257
4258 vertex_or_index_limit: None,
4259 instance_limit: None,
4260 });
4261
4262 Ok(())
4263 }
4264
4265 pub fn render_pass_multi_draw_mesh_tasks_indirect(
4266 &self,
4267 pass: &mut RenderPass,
4268 buffer_id: id::BufferId,
4269 offset: BufferAddress,
4270 count: u32,
4271 ) -> Result<(), RenderPassError> {
4272 let scope = PassErrorScope::Draw {
4273 kind: DrawKind::MultiDrawIndirect,
4274 family: DrawCommandFamily::DrawMeshTasks,
4275 };
4276 let base = pass_base!(pass, scope);
4277
4278 base.commands.push(ArcRenderCommand::DrawIndirect {
4279 buffer: pass_try!(base, scope, self.resolve_buffer_id(buffer_id)),
4280 offset,
4281 count,
4282 family: DrawCommandFamily::DrawMeshTasks,
4283
4284 vertex_or_index_limit: None,
4285 instance_limit: None,
4286 });
4287
4288 Ok(())
4289 }
4290
4291 pub fn render_pass_multi_draw_indirect_count(
4292 &self,
4293 pass: &mut RenderPass,
4294 buffer_id: id::BufferId,
4295 offset: BufferAddress,
4296 count_buffer_id: id::BufferId,
4297 count_buffer_offset: BufferAddress,
4298 max_count: u32,
4299 ) -> Result<(), PassStateError> {
4300 let scope = PassErrorScope::Draw {
4301 kind: DrawKind::MultiDrawIndirectCount,
4302 family: DrawCommandFamily::Draw,
4303 };
4304 let base = pass_base!(pass, scope);
4305
4306 base.commands
4307 .push(ArcRenderCommand::MultiDrawIndirectCount {
4308 buffer: pass_try!(base, scope, self.resolve_buffer_id(buffer_id)),
4309 offset,
4310 count_buffer: pass_try!(base, scope, self.resolve_buffer_id(count_buffer_id)),
4311 count_buffer_offset,
4312 max_count,
4313 family: DrawCommandFamily::Draw,
4314 });
4315
4316 Ok(())
4317 }
4318
4319 pub fn render_pass_multi_draw_indexed_indirect_count(
4320 &self,
4321 pass: &mut RenderPass,
4322 buffer_id: id::BufferId,
4323 offset: BufferAddress,
4324 count_buffer_id: id::BufferId,
4325 count_buffer_offset: BufferAddress,
4326 max_count: u32,
4327 ) -> Result<(), PassStateError> {
4328 let scope = PassErrorScope::Draw {
4329 kind: DrawKind::MultiDrawIndirectCount,
4330 family: DrawCommandFamily::DrawIndexed,
4331 };
4332 let base = pass_base!(pass, scope);
4333
4334 base.commands
4335 .push(ArcRenderCommand::MultiDrawIndirectCount {
4336 buffer: pass_try!(base, scope, self.resolve_buffer_id(buffer_id)),
4337 offset,
4338 count_buffer: pass_try!(base, scope, self.resolve_buffer_id(count_buffer_id)),
4339 count_buffer_offset,
4340 max_count,
4341 family: DrawCommandFamily::DrawIndexed,
4342 });
4343
4344 Ok(())
4345 }
4346
4347 pub fn render_pass_multi_draw_mesh_tasks_indirect_count(
4348 &self,
4349 pass: &mut RenderPass,
4350 buffer_id: id::BufferId,
4351 offset: BufferAddress,
4352 count_buffer_id: id::BufferId,
4353 count_buffer_offset: BufferAddress,
4354 max_count: u32,
4355 ) -> Result<(), RenderPassError> {
4356 let scope = PassErrorScope::Draw {
4357 kind: DrawKind::MultiDrawIndirectCount,
4358 family: DrawCommandFamily::DrawMeshTasks,
4359 };
4360 let base = pass_base!(pass, scope);
4361
4362 base.commands
4363 .push(ArcRenderCommand::MultiDrawIndirectCount {
4364 buffer: pass_try!(base, scope, self.resolve_buffer_id(buffer_id)),
4365 offset,
4366 count_buffer: pass_try!(base, scope, self.resolve_buffer_id(count_buffer_id)),
4367 count_buffer_offset,
4368 max_count,
4369 family: DrawCommandFamily::DrawMeshTasks,
4370 });
4371
4372 Ok(())
4373 }
4374
4375 pub fn render_pass_push_debug_group(
4376 &self,
4377 pass: &mut RenderPass,
4378 label: &str,
4379 color: u32,
4380 ) -> Result<(), PassStateError> {
4381 let base = pass_base!(pass, PassErrorScope::PushDebugGroup);
4382
4383 let bytes = label.as_bytes();
4384 base.string_data.extend_from_slice(bytes);
4385
4386 base.commands.push(ArcRenderCommand::PushDebugGroup {
4387 color,
4388 len: bytes.len(),
4389 });
4390
4391 Ok(())
4392 }
4393
4394 pub fn render_pass_push_debug_group_with_id(
4395 &self,
4396 pass: id::RenderPassEncoderId,
4397 label: &str,
4398 color: u32,
4399 ) -> Result<(), PassStateError> {
4400 let pass = self.hub.render_passes.get(pass);
4401 let mut pass = pass
4402 .try_lock()
4403 .expect("RenderPasses should not be used concurrently");
4404 self.render_pass_push_debug_group(&mut pass, label, color)
4405 }
4406
4407 pub fn render_pass_pop_debug_group(&self, pass: &mut RenderPass) -> Result<(), PassStateError> {
4408 let base = pass_base!(pass, PassErrorScope::PopDebugGroup);
4409
4410 base.commands.push(ArcRenderCommand::PopDebugGroup);
4411
4412 Ok(())
4413 }
4414
4415 pub fn render_pass_pop_debug_group_with_id(
4416 &self,
4417 pass: id::RenderPassEncoderId,
4418 ) -> Result<(), PassStateError> {
4419 let pass = self.hub.render_passes.get(pass);
4420 let mut pass = pass
4421 .try_lock()
4422 .expect("RenderPasses should not be used concurrently");
4423 self.render_pass_pop_debug_group(&mut pass)
4424 }
4425
4426 pub fn render_pass_insert_debug_marker(
4427 &self,
4428 pass: &mut RenderPass,
4429 label: &str,
4430 color: u32,
4431 ) -> Result<(), PassStateError> {
4432 let base = pass_base!(pass, PassErrorScope::InsertDebugMarker);
4433
4434 let bytes = label.as_bytes();
4435 base.string_data.extend_from_slice(bytes);
4436
4437 base.commands.push(ArcRenderCommand::InsertDebugMarker {
4438 color,
4439 len: bytes.len(),
4440 });
4441
4442 Ok(())
4443 }
4444
4445 pub fn render_pass_insert_debug_marker_with_id(
4446 &self,
4447 pass: id::RenderPassEncoderId,
4448 label: &str,
4449 color: u32,
4450 ) -> Result<(), PassStateError> {
4451 let pass = self.hub.render_passes.get(pass);
4452 let mut pass = pass
4453 .try_lock()
4454 .expect("RenderPasses should not be used concurrently");
4455 self.render_pass_insert_debug_marker(&mut pass, label, color)
4456 }
4457
4458 pub fn render_pass_write_timestamp(
4459 &self,
4460 pass: &mut RenderPass,
4461 query_set_id: id::QuerySetId,
4462 query_index: u32,
4463 ) -> Result<(), PassStateError> {
4464 let scope = PassErrorScope::WriteTimestamp;
4465 let base = pass_base!(pass, scope);
4466
4467 base.commands.push(ArcRenderCommand::WriteTimestamp {
4468 query_set: pass_try!(base, scope, self.resolve_query_set(query_set_id)),
4469 query_index,
4470 });
4471
4472 Ok(())
4473 }
4474
4475 pub fn render_pass_write_timestamp_with_id(
4476 &self,
4477 pass: id::RenderPassEncoderId,
4478 query_set_id: id::QuerySetId,
4479 query_index: u32,
4480 ) -> Result<(), PassStateError> {
4481 let pass = self.hub.render_passes.get(pass);
4482 let mut pass = pass
4483 .try_lock()
4484 .expect("RenderPasses should not be used concurrently");
4485 self.render_pass_write_timestamp(&mut pass, query_set_id, query_index)
4486 }
4487
4488 pub fn render_pass_begin_occlusion_query(
4489 &self,
4490 pass: &mut RenderPass,
4491 query_index: u32,
4492 ) -> Result<(), PassStateError> {
4493 let scope = PassErrorScope::BeginOcclusionQuery;
4494 let base = pass_base!(pass, scope);
4495
4496 base.commands
4497 .push(ArcRenderCommand::BeginOcclusionQuery { query_index });
4498
4499 Ok(())
4500 }
4501
4502 pub fn render_pass_begin_occlusion_query_with_id(
4503 &self,
4504 pass: id::RenderPassEncoderId,
4505 query_index: u32,
4506 ) -> Result<(), PassStateError> {
4507 let pass = self.hub.render_passes.get(pass);
4508 let mut pass = pass
4509 .try_lock()
4510 .expect("RenderPasses should not be used concurrently");
4511 self.render_pass_begin_occlusion_query(&mut pass, query_index)
4512 }
4513
4514 pub fn render_pass_end_occlusion_query(
4515 &self,
4516 pass: &mut RenderPass,
4517 ) -> Result<(), PassStateError> {
4518 let scope = PassErrorScope::EndOcclusionQuery;
4519 let base = pass_base!(pass, scope);
4520
4521 base.commands.push(ArcRenderCommand::EndOcclusionQuery);
4522
4523 Ok(())
4524 }
4525
4526 pub fn render_pass_end_occlusion_query_with_id(
4527 &self,
4528 pass: id::RenderPassEncoderId,
4529 ) -> Result<(), PassStateError> {
4530 let pass = self.hub.render_passes.get(pass);
4531 let mut pass = pass
4532 .try_lock()
4533 .expect("RenderPasses should not be used concurrently");
4534 self.render_pass_end_occlusion_query(&mut pass)
4535 }
4536
4537 pub fn render_pass_begin_pipeline_statistics_query(
4538 &self,
4539 pass: &mut RenderPass,
4540 query_set_id: id::QuerySetId,
4541 query_index: u32,
4542 ) -> Result<(), PassStateError> {
4543 let scope = PassErrorScope::BeginPipelineStatisticsQuery;
4544 let base = pass_base!(pass, scope);
4545
4546 base.commands
4547 .push(ArcRenderCommand::BeginPipelineStatisticsQuery {
4548 query_set: pass_try!(base, scope, self.resolve_query_set(query_set_id)),
4549 query_index,
4550 });
4551
4552 Ok(())
4553 }
4554
4555 pub fn render_pass_begin_pipeline_statistics_query_with_id(
4556 &self,
4557 pass: id::RenderPassEncoderId,
4558 query_set_id: id::QuerySetId,
4559 query_index: u32,
4560 ) -> Result<(), PassStateError> {
4561 let pass = self.hub.render_passes.get(pass);
4562 let mut pass = pass
4563 .try_lock()
4564 .expect("RenderPasses should not be used concurrently");
4565 self.render_pass_begin_pipeline_statistics_query(&mut pass, query_set_id, query_index)
4566 }
4567
4568 pub fn render_pass_end_pipeline_statistics_query(
4569 &self,
4570 pass: &mut RenderPass,
4571 ) -> Result<(), PassStateError> {
4572 let scope = PassErrorScope::EndPipelineStatisticsQuery;
4573 let base = pass_base!(pass, scope);
4574
4575 base.commands
4576 .push(ArcRenderCommand::EndPipelineStatisticsQuery);
4577
4578 Ok(())
4579 }
4580
4581 pub fn render_pass_end_pipeline_statistics_query_with_id(
4582 &self,
4583 pass: id::RenderPassEncoderId,
4584 ) -> Result<(), PassStateError> {
4585 let pass = self.hub.render_passes.get(pass);
4586 let mut pass = pass
4587 .try_lock()
4588 .expect("RenderPasses should not be used concurrently");
4589 self.render_pass_end_pipeline_statistics_query(&mut pass)
4590 }
4591
4592 pub fn render_pass_execute_bundles(
4593 &self,
4594 pass: &mut RenderPass,
4595 render_bundle_ids: &[id::RenderBundleId],
4596 ) -> Result<(), PassStateError> {
4597 let scope = PassErrorScope::ExecuteBundle;
4598 let base = pass_base!(pass, scope);
4599
4600 let hub = &self.hub;
4601 let bundles = hub.render_bundles.read();
4602
4603 for &bundle_id in render_bundle_ids {
4604 let bundle = pass_try!(base, scope, bundles.get(bundle_id).get());
4605
4606 base.commands.push(ArcRenderCommand::ExecuteBundle(bundle));
4607 }
4608 pass.current_pipeline.reset();
4609 pass.current_bind_groups.reset();
4610
4611 Ok(())
4612 }
4613
4614 pub fn render_pass_execute_bundles_with_id(
4615 &self,
4616 pass: id::RenderPassEncoderId,
4617 render_bundle_ids: &[id::RenderBundleId],
4618 ) -> Result<(), PassStateError> {
4619 let pass = self.hub.render_passes.get(pass);
4620 let mut pass = pass
4621 .try_lock()
4622 .expect("RenderPasses should not be used concurrently");
4623 self.render_pass_execute_bundles(&mut pass, render_bundle_ids)
4624 }
4625}
4626
4627pub(crate) const fn get_src_stride_of_indirect_args(family: DrawCommandFamily) -> u64 {
4628 match family {
4629 DrawCommandFamily::Draw => size_of::<wgt::DrawIndirectArgs>() as u64,
4630 DrawCommandFamily::DrawIndexed => size_of::<wgt::DrawIndexedIndirectArgs>() as u64,
4631 DrawCommandFamily::DrawMeshTasks => size_of::<wgt::DispatchIndirectArgs>() as u64,
4632 }
4633}
4634
4635pub(crate) const fn get_dst_stride_of_indirect_args(
4636 backend: wgt::Backend,
4637 family: DrawCommandFamily,
4638) -> u64 {
4639 let extra = if matches!(backend, wgt::Backend::Dx12) {
4641 3 * size_of::<u32>() as u64
4642 } else {
4643 0
4644 };
4645 extra + get_src_stride_of_indirect_args(family)
4646}