1use crate::binding_model::BindGroup;
2use crate::command::{
3 validate_and_begin_occlusion_query, validate_and_begin_pipeline_statistics_query,
4};
5use crate::init_tracker::BufferInitTrackerAction;
6use crate::pipeline::RenderPipeline;
7use crate::resource::InvalidResourceError;
8use crate::snatch::SnatchGuard;
9use crate::{
10 api_log,
11 binding_model::BindError,
12 command::{
13 bind::Binder,
14 end_occlusion_query, end_pipeline_statistics_query,
15 memory_init::{fixup_discarded_surfaces, SurfacesInDiscardState},
16 ArcPassTimestampWrites, BasePass, BindGroupStateChange, CommandBuffer, CommandEncoderError,
17 CommandEncoderStatus, DrawError, ExecutionError, MapPassErr, PassErrorScope,
18 PassTimestampWrites, QueryUseError, RenderCommandError, StateChange,
19 },
20 device::{
21 AttachmentData, Device, DeviceError, MissingDownlevelFlags, MissingFeatures,
22 RenderPassCompatibilityError, RenderPassContext,
23 },
24 global::Global,
25 hal_label, id,
26 init_tracker::{MemoryInitKind, TextureInitRange, TextureInitTrackerAction},
27 pipeline::{self, PipelineFlags},
28 resource::{
29 DestroyedResourceError, Labeled, MissingBufferUsageError, MissingTextureUsageError,
30 ParentDevice, QuerySet, Texture, TextureView, TextureViewNotRenderableReason,
31 },
32 track::{ResourceUsageCompatibilityError, TextureSelector, Tracker, UsageScope},
33 Label,
34};
35
36use arrayvec::ArrayVec;
37use thiserror::Error;
38use wgt::{
39 BufferAddress, BufferSize, BufferUsages, Color, DynamicOffset, IndexFormat, ShaderStages,
40 TextureUsages, TextureViewDimension, VertexStepMode,
41};
42
43#[cfg(feature = "serde")]
44use serde::Deserialize;
45#[cfg(feature = "serde")]
46use serde::Serialize;
47
48use std::sync::Arc;
49use std::{borrow::Cow, fmt, iter, mem::size_of, num::NonZeroU32, ops::Range, str};
50
51use super::render_command::ArcRenderCommand;
52use super::{
53 memory_init::TextureSurfaceDiscard, CommandBufferTextureMemoryActions, CommandEncoder,
54 QueryResetMap,
55};
56use super::{DrawKind, Rect};
57
58#[repr(C)]
60#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq)]
61#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
62#[cfg_attr(feature = "serde", serde(rename_all = "kebab-case"))]
63pub enum LoadOp {
64 Clear = 0,
66 Load = 1,
68}
69
70#[repr(C)]
72#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq)]
73#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
74#[cfg_attr(feature = "serde", serde(rename_all = "kebab-case"))]
75pub enum StoreOp {
76 Discard = 0,
80 Store = 1,
82}
83
84#[repr(C)]
86#[derive(Clone, Debug, Eq, PartialEq)]
87#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
88pub struct PassChannel<V> {
89 pub load_op: LoadOp,
95 pub store_op: StoreOp,
97 pub clear_value: V,
100 pub read_only: bool,
104}
105
106impl<V> PassChannel<V> {
107 fn hal_ops(&self) -> hal::AttachmentOps {
108 let mut ops = hal::AttachmentOps::empty();
109 match self.load_op {
110 LoadOp::Load => ops |= hal::AttachmentOps::LOAD,
111 LoadOp::Clear => (),
112 };
113 match self.store_op {
114 StoreOp::Store => ops |= hal::AttachmentOps::STORE,
115 StoreOp::Discard => (),
116 };
117 ops
118 }
119}
120
121#[repr(C)]
123#[derive(Clone, Debug, PartialEq)]
124#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
125pub struct RenderPassColorAttachment {
126 pub view: id::TextureViewId,
128 pub resolve_target: Option<id::TextureViewId>,
130 pub channel: PassChannel<Color>,
132}
133
134#[derive(Debug)]
136struct ArcRenderPassColorAttachment {
137 pub view: Arc<TextureView>,
139 pub resolve_target: Option<Arc<TextureView>>,
141 pub channel: PassChannel<Color>,
143}
144
145#[repr(C)]
147#[derive(Clone, Debug, PartialEq)]
148#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
149pub struct RenderPassDepthStencilAttachment {
150 pub view: id::TextureViewId,
152 pub depth: PassChannel<f32>,
154 pub stencil: PassChannel<u32>,
156}
157#[derive(Debug)]
159pub struct ArcRenderPassDepthStencilAttachment {
160 pub view: Arc<TextureView>,
162 pub depth: PassChannel<f32>,
164 pub stencil: PassChannel<u32>,
166}
167
168impl ArcRenderPassDepthStencilAttachment {
169 fn depth_stencil_read_only(
178 &self,
179 aspects: hal::FormatAspects,
180 ) -> Result<(bool, bool), RenderPassErrorInner> {
181 let mut depth_read_only = true;
182 let mut stencil_read_only = true;
183
184 if aspects.contains(hal::FormatAspects::DEPTH) {
185 if self.depth.read_only
186 && (self.depth.load_op, self.depth.store_op) != (LoadOp::Load, StoreOp::Store)
187 {
188 return Err(RenderPassErrorInner::InvalidDepthOps);
189 }
190 depth_read_only = self.depth.read_only;
191 }
192
193 if aspects.contains(hal::FormatAspects::STENCIL) {
194 if self.stencil.read_only
195 && (self.stencil.load_op, self.stencil.store_op) != (LoadOp::Load, StoreOp::Store)
196 {
197 return Err(RenderPassErrorInner::InvalidStencilOps);
198 }
199 stencil_read_only = self.stencil.read_only;
200 }
201
202 Ok((depth_read_only, stencil_read_only))
203 }
204}
205
206#[derive(Clone, Debug, Default, PartialEq)]
208pub struct RenderPassDescriptor<'a> {
209 pub label: Label<'a>,
210 pub color_attachments: Cow<'a, [Option<RenderPassColorAttachment>]>,
212 pub depth_stencil_attachment: Option<&'a RenderPassDepthStencilAttachment>,
214 pub timestamp_writes: Option<&'a PassTimestampWrites>,
216 pub occlusion_query_set: Option<id::QuerySetId>,
218}
219
220struct ArcRenderPassDescriptor<'a> {
222 pub label: &'a Label<'a>,
223 pub color_attachments:
225 ArrayVec<Option<ArcRenderPassColorAttachment>, { hal::MAX_COLOR_ATTACHMENTS }>,
226 pub depth_stencil_attachment: Option<ArcRenderPassDepthStencilAttachment>,
228 pub timestamp_writes: Option<ArcPassTimestampWrites>,
230 pub occlusion_query_set: Option<Arc<QuerySet>>,
232}
233
234pub struct RenderPass {
235 base: Option<BasePass<ArcRenderCommand>>,
240
241 parent: Option<Arc<CommandBuffer>>,
245
246 color_attachments:
247 ArrayVec<Option<ArcRenderPassColorAttachment>, { hal::MAX_COLOR_ATTACHMENTS }>,
248 depth_stencil_attachment: Option<ArcRenderPassDepthStencilAttachment>,
249 timestamp_writes: Option<ArcPassTimestampWrites>,
250 occlusion_query_set: Option<Arc<QuerySet>>,
251
252 current_bind_groups: BindGroupStateChange,
254 current_pipeline: StateChange<id::RenderPipelineId>,
255}
256
257impl RenderPass {
258 fn new(parent: Option<Arc<CommandBuffer>>, desc: ArcRenderPassDescriptor) -> Self {
260 let ArcRenderPassDescriptor {
261 label,
262 timestamp_writes,
263 color_attachments,
264 depth_stencil_attachment,
265 occlusion_query_set,
266 } = desc;
267
268 Self {
269 base: Some(BasePass::new(label)),
270 parent,
271 color_attachments,
272 depth_stencil_attachment,
273 timestamp_writes,
274 occlusion_query_set,
275
276 current_bind_groups: BindGroupStateChange::new(),
277 current_pipeline: StateChange::new(),
278 }
279 }
280
281 #[inline]
282 pub fn label(&self) -> Option<&str> {
283 self.base.as_ref().and_then(|base| base.label.as_deref())
284 }
285
286 fn base_mut<'a>(
287 &'a mut self,
288 scope: PassErrorScope,
289 ) -> Result<&'a mut BasePass<ArcRenderCommand>, RenderPassError> {
290 self.base
291 .as_mut()
292 .ok_or(RenderPassErrorInner::PassEnded)
293 .map_pass_err(scope)
294 }
295}
296
297impl fmt::Debug for RenderPass {
298 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
299 f.debug_struct("RenderPass")
300 .field("label", &self.label())
301 .field("color_attachments", &self.color_attachments)
302 .field("depth_stencil_target", &self.depth_stencil_attachment)
303 .field(
304 "command count",
305 &self.base.as_ref().map_or(0, |base| base.commands.len()),
306 )
307 .field(
308 "dynamic offset count",
309 &self
310 .base
311 .as_ref()
312 .map_or(0, |base| base.dynamic_offsets.len()),
313 )
314 .field(
315 "push constant u32 count",
316 &self
317 .base
318 .as_ref()
319 .map_or(0, |base| base.push_constant_data.len()),
320 )
321 .finish()
322 }
323}
324
325#[derive(Debug, PartialEq)]
326enum OptionalState {
327 Unused,
328 Required,
329 Set,
330}
331
332impl OptionalState {
333 fn require(&mut self, require: bool) {
334 if require && *self == Self::Unused {
335 *self = Self::Required;
336 }
337 }
338}
339
340#[derive(Debug, Default)]
341struct IndexState {
342 buffer_format: Option<IndexFormat>,
343 limit: u64,
344}
345
346impl IndexState {
347 fn update_buffer(&mut self, range: Range<BufferAddress>, format: IndexFormat) {
348 self.buffer_format = Some(format);
349 let shift = match format {
350 IndexFormat::Uint16 => 1,
351 IndexFormat::Uint32 => 2,
352 };
353 self.limit = (range.end - range.start) >> shift;
354 }
355
356 fn reset(&mut self) {
357 self.buffer_format = None;
358 self.limit = 0;
359 }
360}
361
362#[derive(Clone, Copy, Debug)]
363struct VertexBufferState {
364 total_size: BufferAddress,
365 step: pipeline::VertexStep,
366 bound: bool,
367}
368
369impl VertexBufferState {
370 const EMPTY: Self = Self {
371 total_size: 0,
372 step: pipeline::VertexStep {
373 stride: 0,
374 last_stride: 0,
375 mode: VertexStepMode::Vertex,
376 },
377 bound: false,
378 };
379}
380
381#[derive(Debug, Default)]
382struct VertexState {
383 inputs: ArrayVec<VertexBufferState, { hal::MAX_VERTEX_BUFFERS }>,
384 vertex_limit: u64,
386 vertex_limit_slot: u32,
388 instance_limit: u64,
390 instance_limit_slot: u32,
392}
393
394impl VertexState {
395 fn update_limits(&mut self) {
396 self.vertex_limit = u32::MAX as u64;
402 self.instance_limit = u32::MAX as u64;
403 for (idx, vbs) in self.inputs.iter().enumerate() {
404 if !vbs.bound {
405 continue;
406 }
407
408 let limit = if vbs.total_size < vbs.step.last_stride {
409 0
411 } else {
412 if vbs.step.stride == 0 {
413 continue;
417 }
418
419 (vbs.total_size - vbs.step.last_stride) / vbs.step.stride + 1
421 };
422
423 match vbs.step.mode {
424 VertexStepMode::Vertex => {
425 if limit < self.vertex_limit {
426 self.vertex_limit = limit;
427 self.vertex_limit_slot = idx as _;
428 }
429 }
430 VertexStepMode::Instance => {
431 if limit < self.instance_limit {
432 self.instance_limit = limit;
433 self.instance_limit_slot = idx as _;
434 }
435 }
436 }
437 }
438 }
439
440 fn reset(&mut self) {
441 self.inputs.clear();
442 self.vertex_limit = 0;
443 self.instance_limit = 0;
444 }
445}
446
447struct State<'scope, 'snatch_guard, 'cmd_buf, 'raw_encoder> {
448 pipeline_flags: PipelineFlags,
449 binder: Binder,
450 blend_constant: OptionalState,
451 stencil_reference: u32,
452 pipeline: Option<Arc<RenderPipeline>>,
453 index: IndexState,
454 vertex: VertexState,
455 debug_scope_depth: u32,
456
457 info: RenderPassInfo<'scope>,
458
459 snatch_guard: &'snatch_guard SnatchGuard<'snatch_guard>,
460
461 device: &'cmd_buf Arc<Device>,
462
463 raw_encoder: &'raw_encoder mut dyn hal::DynCommandEncoder,
464
465 tracker: &'cmd_buf mut Tracker,
466 buffer_memory_init_actions: &'cmd_buf mut Vec<BufferInitTrackerAction>,
467 texture_memory_actions: &'cmd_buf mut CommandBufferTextureMemoryActions,
468
469 temp_offsets: Vec<u32>,
470 dynamic_offset_count: usize,
471 string_offset: usize,
472
473 active_occlusion_query: Option<(Arc<QuerySet>, u32)>,
474 active_pipeline_statistics_query: Option<(Arc<QuerySet>, u32)>,
475}
476
477impl<'scope, 'snatch_guard, 'cmd_buf, 'raw_encoder>
478 State<'scope, 'snatch_guard, 'cmd_buf, 'raw_encoder>
479{
480 fn is_ready(&self, indexed: bool) -> Result<(), DrawError> {
481 if let Some(pipeline) = self.pipeline.as_ref() {
482 self.binder.check_compatibility(pipeline.as_ref())?;
483 self.binder.check_late_buffer_bindings()?;
484
485 if self.blend_constant == OptionalState::Required {
486 return Err(DrawError::MissingBlendConstant);
487 }
488
489 let vertex_buffer_count =
491 self.vertex.inputs.iter().take_while(|v| v.bound).count() as u32;
492 if vertex_buffer_count < pipeline.vertex_steps.len() as u32 {
494 return Err(DrawError::MissingVertexBuffer {
495 pipeline: pipeline.error_ident(),
496 index: vertex_buffer_count,
497 });
498 }
499
500 if indexed {
501 if let Some(pipeline_index_format) = pipeline.strip_index_format {
503 let buffer_index_format = self
505 .index
506 .buffer_format
507 .ok_or(DrawError::MissingIndexBuffer)?;
508
509 if pipeline_index_format != buffer_index_format {
511 return Err(DrawError::UnmatchedIndexFormats {
512 pipeline: pipeline.error_ident(),
513 pipeline_format: pipeline_index_format,
514 buffer_format: buffer_index_format,
515 });
516 }
517 }
518 }
519 Ok(())
520 } else {
521 Err(DrawError::MissingPipeline)
522 }
523 }
524
525 fn reset_bundle(&mut self) {
527 self.binder.reset();
528 self.pipeline = None;
529 self.index.reset();
530 self.vertex.reset();
531 }
532}
533
534#[derive(Debug, Copy, Clone)]
538pub enum AttachmentErrorLocation {
539 Color { index: usize, resolve: bool },
540 Depth,
541}
542
543impl fmt::Display for AttachmentErrorLocation {
544 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
545 match *self {
546 AttachmentErrorLocation::Color {
547 index,
548 resolve: false,
549 } => write!(f, "color attachment at index {index}'s texture view"),
550 AttachmentErrorLocation::Color {
551 index,
552 resolve: true,
553 } => write!(
554 f,
555 "color attachment at index {index}'s resolve texture view"
556 ),
557 AttachmentErrorLocation::Depth => write!(f, "depth attachment's texture view"),
558 }
559 }
560}
561
562#[derive(Clone, Debug, Error)]
563#[non_exhaustive]
564pub enum ColorAttachmentError {
565 #[error("Attachment format {0:?} is not a color format")]
566 InvalidFormat(wgt::TextureFormat),
567 #[error("The number of color attachments {given} exceeds the limit {limit}")]
568 TooMany { given: usize, limit: usize },
569 #[error("The total number of bytes per sample in color attachments {total} exceeds the limit {limit}")]
570 TooManyBytesPerSample { total: u32, limit: u32 },
571}
572
573#[derive(Clone, Debug, Error)]
575pub enum RenderPassErrorInner {
576 #[error(transparent)]
577 Device(DeviceError),
578 #[error(transparent)]
579 ColorAttachment(#[from] ColorAttachmentError),
580 #[error(transparent)]
581 Encoder(#[from] CommandEncoderError),
582 #[error("Parent encoder is invalid")]
583 InvalidParentEncoder,
584 #[error("The format of the depth-stencil attachment ({0:?}) is not a depth-stencil format")]
585 InvalidDepthStencilAttachmentFormat(wgt::TextureFormat),
586 #[error("The format of the {location} ({format:?}) is not resolvable")]
587 UnsupportedResolveTargetFormat {
588 location: AttachmentErrorLocation,
589 format: wgt::TextureFormat,
590 },
591 #[error("No color attachments or depth attachments were provided, at least one attachment of any kind must be provided")]
592 MissingAttachments,
593 #[error("The {location} is not renderable:")]
594 TextureViewIsNotRenderable {
595 location: AttachmentErrorLocation,
596 #[source]
597 reason: TextureViewNotRenderableReason,
598 },
599 #[error("Attachments have differing sizes: the {expected_location} has extent {expected_extent:?} but is followed by the {actual_location} which has {actual_extent:?}")]
600 AttachmentsDimensionMismatch {
601 expected_location: AttachmentErrorLocation,
602 expected_extent: wgt::Extent3d,
603 actual_location: AttachmentErrorLocation,
604 actual_extent: wgt::Extent3d,
605 },
606 #[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:?}")]
607 AttachmentSampleCountMismatch {
608 expected_location: AttachmentErrorLocation,
609 expected_samples: u32,
610 actual_location: AttachmentErrorLocation,
611 actual_samples: u32,
612 },
613 #[error("The resolve source, {location}, must be multi-sampled (has {src} samples) while the resolve destination must not be multisampled (has {dst} samples)")]
614 InvalidResolveSampleCounts {
615 location: AttachmentErrorLocation,
616 src: u32,
617 dst: u32,
618 },
619 #[error(
620 "Resource source, {location}, format ({src:?}) must match the resolve destination format ({dst:?})"
621 )]
622 MismatchedResolveTextureFormat {
623 location: AttachmentErrorLocation,
624 src: wgt::TextureFormat,
625 dst: wgt::TextureFormat,
626 },
627 #[error("Surface texture is dropped before the render pass is finished")]
628 SurfaceTextureDropped,
629 #[error("Not enough memory left for render pass")]
630 OutOfMemory,
631 #[error("Unable to clear non-present/read-only depth")]
632 InvalidDepthOps,
633 #[error("Unable to clear non-present/read-only stencil")]
634 InvalidStencilOps,
635 #[error("Setting `values_offset` to be `None` is only for internal use in render bundles")]
636 InvalidValuesOffset,
637 #[error(transparent)]
638 MissingFeatures(#[from] MissingFeatures),
639 #[error(transparent)]
640 MissingDownlevelFlags(#[from] MissingDownlevelFlags),
641 #[error("Indirect buffer offset {0:?} is not a multiple of 4")]
642 UnalignedIndirectBufferOffset(BufferAddress),
643 #[error("Indirect draw uses bytes {offset}..{end_offset} {} which overruns indirect buffer of size {buffer_size}",
644 count.map_or_else(String::new, |v| format!("(using count {v})")))]
645 IndirectBufferOverrun {
646 count: Option<NonZeroU32>,
647 offset: u64,
648 end_offset: u64,
649 buffer_size: u64,
650 },
651 #[error("Indirect draw uses bytes {begin_count_offset}..{end_count_offset} which overruns indirect buffer of size {count_buffer_size}")]
652 IndirectCountBufferOverrun {
653 begin_count_offset: u64,
654 end_count_offset: u64,
655 count_buffer_size: u64,
656 },
657 #[error("Cannot pop debug group, because number of pushed debug groups is zero")]
658 InvalidPopDebugGroup,
659 #[error(transparent)]
660 ResourceUsageCompatibility(#[from] ResourceUsageCompatibilityError),
661 #[error("Render bundle has incompatible targets, {0}")]
662 IncompatibleBundleTargets(#[from] RenderPassCompatibilityError),
663 #[error(
664 "Render bundle has incompatible read-only flags: \
665 bundle has flags depth = {bundle_depth} and stencil = {bundle_stencil}, \
666 while the pass has flags depth = {pass_depth} and stencil = {pass_stencil}. \
667 Read-only renderpasses are only compatible with read-only bundles for that aspect."
668 )]
669 IncompatibleBundleReadOnlyDepthStencil {
670 pass_depth: bool,
671 pass_stencil: bool,
672 bundle_depth: bool,
673 bundle_stencil: bool,
674 },
675 #[error(transparent)]
676 RenderCommand(#[from] RenderCommandError),
677 #[error(transparent)]
678 Draw(#[from] DrawError),
679 #[error(transparent)]
680 Bind(#[from] BindError),
681 #[error("Push constant offset must be aligned to 4 bytes")]
682 PushConstantOffsetAlignment,
683 #[error("Push constant size must be aligned to 4 bytes")]
684 PushConstantSizeAlignment,
685 #[error("Ran out of push constant space. Don't set 4gb of push constants per ComputePass.")]
686 PushConstantOutOfMemory,
687 #[error(transparent)]
688 QueryUse(#[from] QueryUseError),
689 #[error("Multiview layer count must match")]
690 MultiViewMismatch,
691 #[error(
692 "Multiview pass texture views with more than one array layer must have D2Array dimension"
693 )]
694 MultiViewDimensionMismatch,
695 #[error("missing occlusion query set")]
696 MissingOcclusionQuerySet,
697 #[error(transparent)]
698 DestroyedResource(#[from] DestroyedResourceError),
699 #[error("The compute pass has already been ended and no further commands can be recorded")]
700 PassEnded,
701 #[error(transparent)]
702 InvalidResource(#[from] InvalidResourceError),
703}
704
705impl From<MissingBufferUsageError> for RenderPassErrorInner {
706 fn from(error: MissingBufferUsageError) -> Self {
707 Self::RenderCommand(error.into())
708 }
709}
710
711impl From<MissingTextureUsageError> for RenderPassErrorInner {
712 fn from(error: MissingTextureUsageError) -> Self {
713 Self::RenderCommand(error.into())
714 }
715}
716
717impl From<DeviceError> for RenderPassErrorInner {
718 fn from(error: DeviceError) -> Self {
719 Self::Device(error)
720 }
721}
722
723#[derive(Clone, Debug, Error)]
725#[error("{scope}")]
726pub struct RenderPassError {
727 pub scope: PassErrorScope,
728 #[source]
729 pub(super) inner: RenderPassErrorInner,
730}
731
732impl<T, E> MapPassErr<T, RenderPassError> for Result<T, E>
733where
734 E: Into<RenderPassErrorInner>,
735{
736 fn map_pass_err(self, scope: PassErrorScope) -> Result<T, RenderPassError> {
737 self.map_err(|inner| RenderPassError {
738 scope,
739 inner: inner.into(),
740 })
741 }
742}
743
744struct RenderAttachment {
745 texture: Arc<Texture>,
746 selector: TextureSelector,
747 usage: hal::TextureUses,
748}
749
750impl TextureView {
751 fn to_render_attachment(&self, usage: hal::TextureUses) -> RenderAttachment {
752 RenderAttachment {
753 texture: self.parent.clone(),
754 selector: self.selector.clone(),
755 usage,
756 }
757 }
758}
759
760const MAX_TOTAL_ATTACHMENTS: usize = hal::MAX_COLOR_ATTACHMENTS + hal::MAX_COLOR_ATTACHMENTS + 1;
761type AttachmentDataVec<T> = ArrayVec<T, MAX_TOTAL_ATTACHMENTS>;
762
763struct RenderPassInfo<'d> {
764 context: RenderPassContext,
765 usage_scope: UsageScope<'d>,
766 render_attachments: AttachmentDataVec<RenderAttachment>,
768 is_depth_read_only: bool,
769 is_stencil_read_only: bool,
770 extent: wgt::Extent3d,
771
772 pending_discard_init_fixups: SurfacesInDiscardState,
773 divergent_discarded_depth_stencil_aspect: Option<(wgt::TextureAspect, Arc<TextureView>)>,
774 multiview: Option<NonZeroU32>,
775}
776
777impl<'d> RenderPassInfo<'d> {
778 fn add_pass_texture_init_actions<V>(
779 channel: &PassChannel<V>,
780 texture_memory_actions: &mut CommandBufferTextureMemoryActions,
781 view: &TextureView,
782 pending_discard_init_fixups: &mut SurfacesInDiscardState,
783 ) {
784 if channel.load_op == LoadOp::Load {
785 pending_discard_init_fixups.extend(texture_memory_actions.register_init_action(
786 &TextureInitTrackerAction {
787 texture: view.parent.clone(),
788 range: TextureInitRange::from(view.selector.clone()),
789 kind: MemoryInitKind::NeedsInitializedMemory,
791 },
792 ));
793 } else if channel.store_op == StoreOp::Store {
794 texture_memory_actions.register_implicit_init(
796 &view.parent,
797 TextureInitRange::from(view.selector.clone()),
798 );
799 }
800 if channel.store_op == StoreOp::Discard {
801 texture_memory_actions.discard(TextureSurfaceDiscard {
805 texture: view.parent.clone(),
806 mip_level: view.selector.mips.start,
807 layer: view.selector.layers.start,
808 });
809 }
810 }
811
812 fn start(
813 device: &'d Arc<Device>,
814 hal_label: Option<&str>,
815 color_attachments: ArrayVec<
816 Option<ArcRenderPassColorAttachment>,
817 { hal::MAX_COLOR_ATTACHMENTS },
818 >,
819 mut depth_stencil_attachment: Option<ArcRenderPassDepthStencilAttachment>,
820 mut timestamp_writes: Option<ArcPassTimestampWrites>,
821 mut occlusion_query_set: Option<Arc<QuerySet>>,
822 encoder: &mut CommandEncoder,
823 trackers: &mut Tracker,
824 texture_memory_actions: &mut CommandBufferTextureMemoryActions,
825 pending_query_resets: &mut QueryResetMap,
826 snatch_guard: &SnatchGuard<'_>,
827 ) -> Result<Self, RenderPassErrorInner> {
828 profiling::scope!("RenderPassInfo::start");
829
830 let mut is_depth_read_only = false;
834 let mut is_stencil_read_only = false;
835
836 let mut render_attachments = AttachmentDataVec::<RenderAttachment>::new();
837 let mut discarded_surfaces = AttachmentDataVec::new();
838 let mut pending_discard_init_fixups = SurfacesInDiscardState::new();
839 let mut divergent_discarded_depth_stencil_aspect = None;
840
841 let mut attachment_location = AttachmentErrorLocation::Color {
842 index: usize::MAX,
843 resolve: false,
844 };
845 let mut extent = None;
846 let mut sample_count = 0;
847
848 let mut detected_multiview: Option<Option<NonZeroU32>> = None;
849
850 let mut check_multiview = |view: &TextureView| {
851 let layers = view.selector.layers.end - view.selector.layers.start;
853 let this_multiview = if layers >= 2 {
854 Some(unsafe { NonZeroU32::new_unchecked(layers) })
856 } else {
857 None
858 };
859
860 if this_multiview.is_some() && view.desc.dimension != TextureViewDimension::D2Array {
862 return Err(RenderPassErrorInner::MultiViewDimensionMismatch);
863 }
864
865 if let Some(multiview) = detected_multiview {
867 if multiview != this_multiview {
868 return Err(RenderPassErrorInner::MultiViewMismatch);
869 }
870 } else {
871 if this_multiview.is_some() {
873 device.require_features(wgt::Features::MULTIVIEW)?;
874 }
875
876 detected_multiview = Some(this_multiview);
877 }
878
879 Ok(())
880 };
881 let mut add_view = |view: &TextureView, location| {
882 let render_extent = view.render_extent.map_err(|reason| {
883 RenderPassErrorInner::TextureViewIsNotRenderable { location, reason }
884 })?;
885 if let Some(ex) = extent {
886 if ex != render_extent {
887 return Err(RenderPassErrorInner::AttachmentsDimensionMismatch {
888 expected_location: attachment_location,
889 expected_extent: ex,
890 actual_location: location,
891 actual_extent: render_extent,
892 });
893 }
894 } else {
895 extent = Some(render_extent);
896 }
897 if sample_count == 0 {
898 sample_count = view.samples;
899 } else if sample_count != view.samples {
900 return Err(RenderPassErrorInner::AttachmentSampleCountMismatch {
901 expected_location: attachment_location,
902 expected_samples: sample_count,
903 actual_location: location,
904 actual_samples: view.samples,
905 });
906 }
907 attachment_location = location;
908 Ok(())
909 };
910
911 let mut depth_stencil = None;
912
913 if let Some(at) = depth_stencil_attachment.as_ref() {
914 let view = &at.view;
915 view.same_device(device)?;
916 check_multiview(view)?;
917 add_view(view, AttachmentErrorLocation::Depth)?;
918
919 let ds_aspects = view.desc.aspects();
920 if ds_aspects.contains(hal::FormatAspects::COLOR) {
921 return Err(RenderPassErrorInner::InvalidDepthStencilAttachmentFormat(
922 view.desc.format,
923 ));
924 }
925
926 if !ds_aspects.contains(hal::FormatAspects::STENCIL)
927 || (at.stencil.load_op == at.depth.load_op
928 && at.stencil.store_op == at.depth.store_op)
929 {
930 Self::add_pass_texture_init_actions(
931 &at.depth,
932 texture_memory_actions,
933 view,
934 &mut pending_discard_init_fixups,
935 );
936 } else if !ds_aspects.contains(hal::FormatAspects::DEPTH) {
937 Self::add_pass_texture_init_actions(
938 &at.stencil,
939 texture_memory_actions,
940 view,
941 &mut pending_discard_init_fixups,
942 );
943 } else {
944 let need_init_beforehand =
966 at.depth.load_op == LoadOp::Load || at.stencil.load_op == LoadOp::Load;
967 if need_init_beforehand {
968 pending_discard_init_fixups.extend(
969 texture_memory_actions.register_init_action(&TextureInitTrackerAction {
970 texture: view.parent.clone(),
971 range: TextureInitRange::from(view.selector.clone()),
972 kind: MemoryInitKind::NeedsInitializedMemory,
973 }),
974 );
975 }
976
977 if at.depth.store_op != at.stencil.store_op {
986 if !need_init_beforehand {
987 texture_memory_actions.register_implicit_init(
988 &view.parent,
989 TextureInitRange::from(view.selector.clone()),
990 );
991 }
992 divergent_discarded_depth_stencil_aspect = Some((
993 if at.depth.store_op == StoreOp::Discard {
994 wgt::TextureAspect::DepthOnly
995 } else {
996 wgt::TextureAspect::StencilOnly
997 },
998 view.clone(),
999 ));
1000 } else if at.depth.store_op == StoreOp::Discard {
1001 discarded_surfaces.push(TextureSurfaceDiscard {
1003 texture: view.parent.clone(),
1004 mip_level: view.selector.mips.start,
1005 layer: view.selector.layers.start,
1006 });
1007 }
1008 }
1009
1010 (is_depth_read_only, is_stencil_read_only) = at.depth_stencil_read_only(ds_aspects)?;
1011
1012 let usage = if is_depth_read_only
1013 && is_stencil_read_only
1014 && device
1015 .downlevel
1016 .flags
1017 .contains(wgt::DownlevelFlags::READ_ONLY_DEPTH_STENCIL)
1018 {
1019 hal::TextureUses::DEPTH_STENCIL_READ | hal::TextureUses::RESOURCE
1020 } else {
1021 hal::TextureUses::DEPTH_STENCIL_WRITE
1022 };
1023 render_attachments.push(view.to_render_attachment(usage));
1024
1025 depth_stencil = Some(hal::DepthStencilAttachment {
1026 target: hal::Attachment {
1027 view: view.try_raw(snatch_guard)?,
1028 usage,
1029 },
1030 depth_ops: at.depth.hal_ops(),
1031 stencil_ops: at.stencil.hal_ops(),
1032 clear_value: (at.depth.clear_value, at.stencil.clear_value),
1033 });
1034 }
1035
1036 let mut color_attachments_hal =
1037 ArrayVec::<Option<hal::ColorAttachment<_>>, { hal::MAX_COLOR_ATTACHMENTS }>::new();
1038 for (index, attachment) in color_attachments.iter().enumerate() {
1039 let at = if let Some(attachment) = attachment.as_ref() {
1040 attachment
1041 } else {
1042 color_attachments_hal.push(None);
1043 continue;
1044 };
1045 let color_view: &TextureView = &at.view;
1046 color_view.same_device(device)?;
1047 check_multiview(color_view)?;
1048 add_view(
1049 color_view,
1050 AttachmentErrorLocation::Color {
1051 index,
1052 resolve: false,
1053 },
1054 )?;
1055
1056 if !color_view
1057 .desc
1058 .aspects()
1059 .contains(hal::FormatAspects::COLOR)
1060 {
1061 return Err(RenderPassErrorInner::ColorAttachment(
1062 ColorAttachmentError::InvalidFormat(color_view.desc.format),
1063 ));
1064 }
1065
1066 Self::add_pass_texture_init_actions(
1067 &at.channel,
1068 texture_memory_actions,
1069 color_view,
1070 &mut pending_discard_init_fixups,
1071 );
1072 render_attachments
1073 .push(color_view.to_render_attachment(hal::TextureUses::COLOR_TARGET));
1074
1075 let mut hal_resolve_target = None;
1076 if let Some(resolve_view) = &at.resolve_target {
1077 resolve_view.same_device(device)?;
1078 check_multiview(resolve_view)?;
1079
1080 let resolve_location = AttachmentErrorLocation::Color {
1081 index,
1082 resolve: true,
1083 };
1084
1085 let render_extent = resolve_view.render_extent.map_err(|reason| {
1086 RenderPassErrorInner::TextureViewIsNotRenderable {
1087 location: resolve_location,
1088 reason,
1089 }
1090 })?;
1091 if color_view.render_extent.unwrap() != render_extent {
1092 return Err(RenderPassErrorInner::AttachmentsDimensionMismatch {
1093 expected_location: attachment_location,
1094 expected_extent: extent.unwrap_or_default(),
1095 actual_location: resolve_location,
1096 actual_extent: render_extent,
1097 });
1098 }
1099 if color_view.samples == 1 || resolve_view.samples != 1 {
1100 return Err(RenderPassErrorInner::InvalidResolveSampleCounts {
1101 location: resolve_location,
1102 src: color_view.samples,
1103 dst: resolve_view.samples,
1104 });
1105 }
1106 if color_view.desc.format != resolve_view.desc.format {
1107 return Err(RenderPassErrorInner::MismatchedResolveTextureFormat {
1108 location: resolve_location,
1109 src: color_view.desc.format,
1110 dst: resolve_view.desc.format,
1111 });
1112 }
1113 if !resolve_view
1114 .format_features
1115 .flags
1116 .contains(wgt::TextureFormatFeatureFlags::MULTISAMPLE_RESOLVE)
1117 {
1118 return Err(RenderPassErrorInner::UnsupportedResolveTargetFormat {
1119 location: resolve_location,
1120 format: resolve_view.desc.format,
1121 });
1122 }
1123
1124 texture_memory_actions.register_implicit_init(
1125 &resolve_view.parent,
1126 TextureInitRange::from(resolve_view.selector.clone()),
1127 );
1128 render_attachments
1129 .push(resolve_view.to_render_attachment(hal::TextureUses::COLOR_TARGET));
1130
1131 hal_resolve_target = Some(hal::Attachment {
1132 view: resolve_view.try_raw(snatch_guard)?,
1133 usage: hal::TextureUses::COLOR_TARGET,
1134 });
1135 }
1136
1137 color_attachments_hal.push(Some(hal::ColorAttachment {
1138 target: hal::Attachment {
1139 view: color_view.try_raw(snatch_guard)?,
1140 usage: hal::TextureUses::COLOR_TARGET,
1141 },
1142 resolve_target: hal_resolve_target,
1143 ops: at.channel.hal_ops(),
1144 clear_value: at.channel.clear_value,
1145 }));
1146 }
1147
1148 let extent = extent.ok_or(RenderPassErrorInner::MissingAttachments)?;
1149 let multiview = detected_multiview.expect("Multiview was not detected, no attachments");
1150
1151 let attachment_formats = AttachmentData {
1152 colors: color_attachments
1153 .iter()
1154 .map(|at| at.as_ref().map(|at| at.view.desc.format))
1155 .collect(),
1156 resolves: color_attachments
1157 .iter()
1158 .filter_map(|at| {
1159 at.as_ref().and_then(|at| {
1160 at.resolve_target
1161 .as_ref()
1162 .map(|resolve| resolve.desc.format)
1163 })
1164 })
1165 .collect(),
1166 depth_stencil: depth_stencil_attachment
1167 .as_ref()
1168 .map(|at| at.view.desc.format),
1169 };
1170
1171 let context = RenderPassContext {
1172 attachments: attachment_formats,
1173 sample_count,
1174 multiview,
1175 };
1176
1177 let timestamp_writes_hal = if let Some(tw) = timestamp_writes.as_ref() {
1178 let query_set = &tw.query_set;
1179 query_set.same_device(device)?;
1180
1181 if let Some(index) = tw.beginning_of_pass_write_index {
1182 pending_query_resets.use_query_set(query_set, index);
1183 }
1184 if let Some(index) = tw.end_of_pass_write_index {
1185 pending_query_resets.use_query_set(query_set, index);
1186 }
1187
1188 Some(hal::PassTimestampWrites {
1189 query_set: query_set.raw(),
1190 beginning_of_pass_write_index: tw.beginning_of_pass_write_index,
1191 end_of_pass_write_index: tw.end_of_pass_write_index,
1192 })
1193 } else {
1194 None
1195 };
1196
1197 let occlusion_query_set_hal = if let Some(query_set) = occlusion_query_set.as_ref() {
1198 query_set.same_device(device)?;
1199 Some(query_set.raw())
1200 } else {
1201 None
1202 };
1203
1204 let hal_desc = hal::RenderPassDescriptor {
1205 label: hal_label,
1206 extent,
1207 sample_count,
1208 color_attachments: &color_attachments_hal,
1209 depth_stencil_attachment: depth_stencil,
1210 multiview,
1211 timestamp_writes: timestamp_writes_hal,
1212 occlusion_query_set: occlusion_query_set_hal,
1213 };
1214 unsafe {
1215 encoder.raw.begin_render_pass(&hal_desc);
1216 };
1217 drop(color_attachments_hal); if let Some(tw) = timestamp_writes.take() {
1221 trackers.query_sets.insert_single(tw.query_set);
1222 };
1223 if let Some(occlusion_query_set) = occlusion_query_set.take() {
1224 trackers.query_sets.insert_single(occlusion_query_set);
1225 };
1226 if let Some(at) = depth_stencil_attachment.take() {
1227 trackers.views.insert_single(at.view.clone());
1228 }
1229 for at in color_attachments.into_iter().flatten() {
1230 trackers.views.insert_single(at.view.clone());
1231 if let Some(resolve_target) = at.resolve_target {
1232 trackers.views.insert_single(resolve_target);
1233 }
1234 }
1235
1236 Ok(Self {
1237 context,
1238 usage_scope: device.new_usage_scope(),
1239 render_attachments,
1240 is_depth_read_only,
1241 is_stencil_read_only,
1242 extent,
1243 pending_discard_init_fixups,
1244 divergent_discarded_depth_stencil_aspect,
1245 multiview,
1246 })
1247 }
1248
1249 fn finish(
1250 mut self,
1251 raw: &mut dyn hal::DynCommandEncoder,
1252 snatch_guard: &SnatchGuard,
1253 ) -> Result<(UsageScope<'d>, SurfacesInDiscardState), RenderPassErrorInner> {
1254 profiling::scope!("RenderPassInfo::finish");
1255 unsafe {
1256 raw.end_render_pass();
1257 }
1258
1259 for ra in self.render_attachments {
1260 let texture = &ra.texture;
1261 texture.check_usage(TextureUsages::RENDER_ATTACHMENT)?;
1262
1263 unsafe {
1265 self.usage_scope.textures.merge_single(
1266 texture,
1267 Some(ra.selector.clone()),
1268 ra.usage,
1269 )?
1270 };
1271 }
1272
1273 if let Some((aspect, view)) = self.divergent_discarded_depth_stencil_aspect {
1283 let (depth_ops, stencil_ops) = if aspect == wgt::TextureAspect::DepthOnly {
1284 (
1285 hal::AttachmentOps::STORE, hal::AttachmentOps::LOAD | hal::AttachmentOps::STORE, )
1288 } else {
1289 (
1290 hal::AttachmentOps::LOAD | hal::AttachmentOps::STORE, hal::AttachmentOps::STORE, )
1293 };
1294 let desc = hal::RenderPassDescriptor::<'_, _, dyn hal::DynTextureView> {
1295 label: Some("(wgpu internal) Zero init discarded depth/stencil aspect"),
1296 extent: view.render_extent.unwrap(),
1297 sample_count: view.samples,
1298 color_attachments: &[],
1299 depth_stencil_attachment: Some(hal::DepthStencilAttachment {
1300 target: hal::Attachment {
1301 view: view.try_raw(snatch_guard)?,
1302 usage: hal::TextureUses::DEPTH_STENCIL_WRITE,
1303 },
1304 depth_ops,
1305 stencil_ops,
1306 clear_value: (0.0, 0),
1307 }),
1308 multiview: self.multiview,
1309 timestamp_writes: None,
1310 occlusion_query_set: None,
1311 };
1312 unsafe {
1313 raw.begin_render_pass(&desc);
1314 raw.end_render_pass();
1315 }
1316 }
1317
1318 Ok((self.usage_scope, self.pending_discard_init_fixups))
1319 }
1320}
1321
1322impl Global {
1323 pub fn command_encoder_create_render_pass(
1330 &self,
1331 encoder_id: id::CommandEncoderId,
1332 desc: &RenderPassDescriptor<'_>,
1333 ) -> (RenderPass, Option<CommandEncoderError>) {
1334 fn fill_arc_desc(
1335 hub: &crate::hub::Hub,
1336 desc: &RenderPassDescriptor<'_>,
1337 arc_desc: &mut ArcRenderPassDescriptor,
1338 device: &Device,
1339 ) -> Result<(), CommandEncoderError> {
1340 let query_sets = hub.query_sets.read();
1341 let texture_views = hub.texture_views.read();
1342
1343 let max_color_attachments = device.limits.max_color_attachments as usize;
1344 if desc.color_attachments.len() > max_color_attachments {
1345 return Err(CommandEncoderError::InvalidColorAttachment(
1346 ColorAttachmentError::TooMany {
1347 given: desc.color_attachments.len(),
1348 limit: max_color_attachments,
1349 },
1350 ));
1351 }
1352
1353 for color_attachment in desc.color_attachments.iter() {
1354 if let Some(RenderPassColorAttachment {
1355 view: view_id,
1356 resolve_target,
1357 channel,
1358 }) = color_attachment
1359 {
1360 let view = texture_views.get(*view_id).get()?;
1361
1362 let resolve_target = if let Some(resolve_target_id) = resolve_target {
1363 let rt_arc = texture_views.get(*resolve_target_id).get()?;
1364
1365 Some(rt_arc)
1366 } else {
1367 None
1368 };
1369
1370 arc_desc
1371 .color_attachments
1372 .push(Some(ArcRenderPassColorAttachment {
1373 view,
1374 resolve_target,
1375 channel: channel.clone(),
1376 }));
1377 } else {
1378 arc_desc.color_attachments.push(None);
1379 }
1380 }
1381
1382 arc_desc.depth_stencil_attachment =
1383 if let Some(depth_stencil_attachment) = desc.depth_stencil_attachment {
1384 let view = texture_views.get(depth_stencil_attachment.view).get()?;
1385
1386 Some(ArcRenderPassDepthStencilAttachment {
1387 view,
1388 depth: depth_stencil_attachment.depth.clone(),
1389 stencil: depth_stencil_attachment.stencil.clone(),
1390 })
1391 } else {
1392 None
1393 };
1394
1395 arc_desc.timestamp_writes = if let Some(tw) = desc.timestamp_writes {
1396 let query_set = query_sets.get(tw.query_set).get()?;
1397
1398 Some(ArcPassTimestampWrites {
1399 query_set,
1400 beginning_of_pass_write_index: tw.beginning_of_pass_write_index,
1401 end_of_pass_write_index: tw.end_of_pass_write_index,
1402 })
1403 } else {
1404 None
1405 };
1406
1407 arc_desc.occlusion_query_set =
1408 if let Some(occlusion_query_set) = desc.occlusion_query_set {
1409 let query_set = query_sets.get(occlusion_query_set).get()?;
1410
1411 Some(query_set)
1412 } else {
1413 None
1414 };
1415
1416 Ok(())
1417 }
1418
1419 let hub = &self.hub;
1420 let mut arc_desc = ArcRenderPassDescriptor {
1421 label: &desc.label,
1422 timestamp_writes: None,
1423 color_attachments: ArrayVec::new(),
1424 depth_stencil_attachment: None,
1425 occlusion_query_set: None,
1426 };
1427
1428 let make_err = |e, arc_desc| (RenderPass::new(None, arc_desc), Some(e));
1429
1430 let cmd_buf = hub.command_buffers.get(encoder_id.into_command_buffer_id());
1431
1432 match cmd_buf
1433 .try_get()
1434 .map_err(|e| e.into())
1435 .and_then(|mut cmd_buf_data| cmd_buf_data.lock_encoder())
1436 {
1437 Ok(_) => {}
1438 Err(e) => return make_err(e, arc_desc),
1439 };
1440
1441 let err = fill_arc_desc(hub, desc, &mut arc_desc, &cmd_buf.device).err();
1442
1443 (RenderPass::new(Some(cmd_buf), arc_desc), err)
1444 }
1445
1446 #[doc(hidden)]
1449 #[cfg(any(feature = "serde", feature = "replay"))]
1450 pub fn render_pass_end_with_unresolved_commands(
1451 &self,
1452 encoder_id: id::CommandEncoderId,
1453 base: BasePass<super::RenderCommand>,
1454 color_attachments: &[Option<RenderPassColorAttachment>],
1455 depth_stencil_attachment: Option<&RenderPassDepthStencilAttachment>,
1456 timestamp_writes: Option<&PassTimestampWrites>,
1457 occlusion_query_set: Option<id::QuerySetId>,
1458 ) -> Result<(), RenderPassError> {
1459 let pass_scope = PassErrorScope::Pass;
1460
1461 #[cfg(feature = "trace")]
1462 {
1463 let cmd_buf = self
1464 .hub
1465 .command_buffers
1466 .get(encoder_id.into_command_buffer_id());
1467 let mut cmd_buf_data = cmd_buf.try_get().map_pass_err(pass_scope)?;
1468
1469 if let Some(ref mut list) = cmd_buf_data.commands {
1470 list.push(crate::device::trace::Command::RunRenderPass {
1471 base: BasePass {
1472 label: base.label.clone(),
1473 commands: base.commands.clone(),
1474 dynamic_offsets: base.dynamic_offsets.clone(),
1475 string_data: base.string_data.clone(),
1476 push_constant_data: base.push_constant_data.clone(),
1477 },
1478 target_colors: color_attachments.to_vec(),
1479 target_depth_stencil: depth_stencil_attachment.cloned(),
1480 timestamp_writes: timestamp_writes.cloned(),
1481 occlusion_query_set_id: occlusion_query_set,
1482 });
1483 }
1484 }
1485
1486 let BasePass {
1487 label,
1488 commands,
1489 dynamic_offsets,
1490 string_data,
1491 push_constant_data,
1492 } = base;
1493
1494 let (mut render_pass, encoder_error) = self.command_encoder_create_render_pass(
1495 encoder_id,
1496 &RenderPassDescriptor {
1497 label: label.as_deref().map(Cow::Borrowed),
1498 color_attachments: Cow::Borrowed(color_attachments),
1499 depth_stencil_attachment,
1500 timestamp_writes,
1501 occlusion_query_set,
1502 },
1503 );
1504 if let Some(err) = encoder_error {
1505 return Err(RenderPassError {
1506 scope: pass_scope,
1507 inner: err.into(),
1508 });
1509 };
1510
1511 render_pass.base = Some(BasePass {
1512 label,
1513 commands: super::RenderCommand::resolve_render_command_ids(&self.hub, &commands)?,
1514 dynamic_offsets,
1515 string_data,
1516 push_constant_data,
1517 });
1518
1519 self.render_pass_end(&mut render_pass)
1520 }
1521
1522 pub fn render_pass_end(&self, pass: &mut RenderPass) -> Result<(), RenderPassError> {
1523 let pass_scope = PassErrorScope::Pass;
1524
1525 let cmd_buf = pass
1526 .parent
1527 .as_ref()
1528 .ok_or(RenderPassErrorInner::InvalidParentEncoder)
1529 .map_pass_err(pass_scope)?;
1530
1531 let base = pass
1532 .base
1533 .take()
1534 .ok_or(RenderPassErrorInner::PassEnded)
1535 .map_pass_err(pass_scope)?;
1536
1537 profiling::scope!(
1538 "CommandEncoder::run_render_pass {}",
1539 base.label.as_deref().unwrap_or("")
1540 );
1541
1542 let mut cmd_buf_data = cmd_buf.try_get().map_pass_err(pass_scope)?;
1543 cmd_buf_data.unlock_encoder().map_pass_err(pass_scope)?;
1544 let cmd_buf_data = &mut *cmd_buf_data;
1545
1546 let device = &cmd_buf.device;
1547 let snatch_guard = &device.snatchable_lock.read();
1548
1549 let hal_label = hal_label(base.label.as_deref(), device.instance_flags);
1550
1551 let (scope, pending_discard_init_fixups) = {
1552 device.check_is_valid().map_pass_err(pass_scope)?;
1553
1554 let encoder = &mut cmd_buf_data.encoder;
1555 let status = &mut cmd_buf_data.status;
1556 let tracker = &mut cmd_buf_data.trackers;
1557 let buffer_memory_init_actions = &mut cmd_buf_data.buffer_memory_init_actions;
1558 let texture_memory_actions = &mut cmd_buf_data.texture_memory_actions;
1559 let pending_query_resets = &mut cmd_buf_data.pending_query_resets;
1560
1561 encoder.close(&cmd_buf.device).map_pass_err(pass_scope)?;
1565 *status = CommandEncoderStatus::Error;
1567 encoder
1568 .open_pass(hal_label, &cmd_buf.device)
1569 .map_pass_err(pass_scope)?;
1570
1571 let info = RenderPassInfo::start(
1572 device,
1573 hal_label,
1574 pass.color_attachments.take(),
1575 pass.depth_stencil_attachment.take(),
1576 pass.timestamp_writes.take(),
1577 pass.occlusion_query_set.clone(),
1580 encoder,
1581 tracker,
1582 texture_memory_actions,
1583 pending_query_resets,
1584 snatch_guard,
1585 )
1586 .map_pass_err(pass_scope)?;
1587
1588 let indices = &device.tracker_indices;
1589 tracker.buffers.set_size(indices.buffers.size());
1590 tracker.textures.set_size(indices.textures.size());
1591
1592 let mut state = State {
1593 pipeline_flags: PipelineFlags::empty(),
1594 binder: Binder::new(),
1595 blend_constant: OptionalState::Unused,
1596 stencil_reference: 0,
1597 pipeline: None,
1598 index: IndexState::default(),
1599 vertex: VertexState::default(),
1600 debug_scope_depth: 0,
1601
1602 info,
1603
1604 snatch_guard,
1605
1606 device,
1607 raw_encoder: encoder.raw.as_mut(),
1608 tracker,
1609 buffer_memory_init_actions,
1610 texture_memory_actions,
1611
1612 temp_offsets: Vec::new(),
1613 dynamic_offset_count: 0,
1614 string_offset: 0,
1615
1616 active_occlusion_query: None,
1617 active_pipeline_statistics_query: None,
1618 };
1619
1620 for command in base.commands {
1621 match command {
1622 ArcRenderCommand::SetBindGroup {
1623 index,
1624 num_dynamic_offsets,
1625 bind_group,
1626 } => {
1627 let scope = PassErrorScope::SetBindGroup;
1628 set_bind_group(
1629 &mut state,
1630 cmd_buf,
1631 &base.dynamic_offsets,
1632 index,
1633 num_dynamic_offsets,
1634 bind_group,
1635 )
1636 .map_pass_err(scope)?;
1637 }
1638 ArcRenderCommand::SetPipeline(pipeline) => {
1639 let scope = PassErrorScope::SetPipelineRender;
1640 set_pipeline(&mut state, cmd_buf, pipeline).map_pass_err(scope)?;
1641 }
1642 ArcRenderCommand::SetIndexBuffer {
1643 buffer,
1644 index_format,
1645 offset,
1646 size,
1647 } => {
1648 let scope = PassErrorScope::SetIndexBuffer;
1649 set_index_buffer(&mut state, cmd_buf, buffer, index_format, offset, size)
1650 .map_pass_err(scope)?;
1651 }
1652 ArcRenderCommand::SetVertexBuffer {
1653 slot,
1654 buffer,
1655 offset,
1656 size,
1657 } => {
1658 let scope = PassErrorScope::SetVertexBuffer;
1659 set_vertex_buffer(&mut state, cmd_buf, slot, buffer, offset, size)
1660 .map_pass_err(scope)?;
1661 }
1662 ArcRenderCommand::SetBlendConstant(ref color) => {
1663 set_blend_constant(&mut state, color);
1664 }
1665 ArcRenderCommand::SetStencilReference(value) => {
1666 set_stencil_reference(&mut state, value);
1667 }
1668 ArcRenderCommand::SetViewport {
1669 rect,
1670 depth_min,
1671 depth_max,
1672 } => {
1673 let scope = PassErrorScope::SetViewport;
1674 set_viewport(&mut state, rect, depth_min, depth_max).map_pass_err(scope)?;
1675 }
1676 ArcRenderCommand::SetPushConstant {
1677 stages,
1678 offset,
1679 size_bytes,
1680 values_offset,
1681 } => {
1682 let scope = PassErrorScope::SetPushConstant;
1683 set_push_constant(
1684 &mut state,
1685 &base.push_constant_data,
1686 stages,
1687 offset,
1688 size_bytes,
1689 values_offset,
1690 )
1691 .map_pass_err(scope)?;
1692 }
1693 ArcRenderCommand::SetScissor(rect) => {
1694 let scope = PassErrorScope::SetScissorRect;
1695 set_scissor(&mut state, rect).map_pass_err(scope)?;
1696 }
1697 ArcRenderCommand::Draw {
1698 vertex_count,
1699 instance_count,
1700 first_vertex,
1701 first_instance,
1702 } => {
1703 let scope = PassErrorScope::Draw {
1704 kind: DrawKind::Draw,
1705 indexed: false,
1706 };
1707 draw(
1708 &mut state,
1709 vertex_count,
1710 instance_count,
1711 first_vertex,
1712 first_instance,
1713 )
1714 .map_pass_err(scope)?;
1715 }
1716 ArcRenderCommand::DrawIndexed {
1717 index_count,
1718 instance_count,
1719 first_index,
1720 base_vertex,
1721 first_instance,
1722 } => {
1723 let scope = PassErrorScope::Draw {
1724 kind: DrawKind::Draw,
1725 indexed: true,
1726 };
1727 draw_indexed(
1728 &mut state,
1729 index_count,
1730 instance_count,
1731 first_index,
1732 base_vertex,
1733 first_instance,
1734 )
1735 .map_pass_err(scope)?;
1736 }
1737 ArcRenderCommand::MultiDrawIndirect {
1738 buffer,
1739 offset,
1740 count,
1741 indexed,
1742 } => {
1743 let scope = PassErrorScope::Draw {
1744 kind: if count.is_some() {
1745 DrawKind::MultiDrawIndirect
1746 } else {
1747 DrawKind::DrawIndirect
1748 },
1749 indexed,
1750 };
1751 multi_draw_indirect(&mut state, cmd_buf, buffer, offset, count, indexed)
1752 .map_pass_err(scope)?;
1753 }
1754 ArcRenderCommand::MultiDrawIndirectCount {
1755 buffer,
1756 offset,
1757 count_buffer,
1758 count_buffer_offset,
1759 max_count,
1760 indexed,
1761 } => {
1762 let scope = PassErrorScope::Draw {
1763 kind: DrawKind::MultiDrawIndirectCount,
1764 indexed,
1765 };
1766 multi_draw_indirect_count(
1767 &mut state,
1768 cmd_buf,
1769 buffer,
1770 offset,
1771 count_buffer,
1772 count_buffer_offset,
1773 max_count,
1774 indexed,
1775 )
1776 .map_pass_err(scope)?;
1777 }
1778 ArcRenderCommand::PushDebugGroup { color: _, len } => {
1779 push_debug_group(&mut state, &base.string_data, len);
1780 }
1781 ArcRenderCommand::PopDebugGroup => {
1782 let scope = PassErrorScope::PopDebugGroup;
1783 pop_debug_group(&mut state).map_pass_err(scope)?;
1784 }
1785 ArcRenderCommand::InsertDebugMarker { color: _, len } => {
1786 insert_debug_marker(&mut state, &base.string_data, len);
1787 }
1788 ArcRenderCommand::WriteTimestamp {
1789 query_set,
1790 query_index,
1791 } => {
1792 let scope = PassErrorScope::WriteTimestamp;
1793 write_timestamp(
1794 &mut state,
1795 cmd_buf,
1796 &mut cmd_buf_data.pending_query_resets,
1797 query_set,
1798 query_index,
1799 )
1800 .map_pass_err(scope)?;
1801 }
1802 ArcRenderCommand::BeginOcclusionQuery { query_index } => {
1803 api_log!("RenderPass::begin_occlusion_query {query_index}");
1804 let scope = PassErrorScope::BeginOcclusionQuery;
1805
1806 let query_set = pass
1807 .occlusion_query_set
1808 .clone()
1809 .ok_or(RenderPassErrorInner::MissingOcclusionQuerySet)
1810 .map_pass_err(scope)?;
1811
1812 validate_and_begin_occlusion_query(
1813 query_set,
1814 state.raw_encoder,
1815 &mut state.tracker.query_sets,
1816 query_index,
1817 Some(&mut cmd_buf_data.pending_query_resets),
1818 &mut state.active_occlusion_query,
1819 )
1820 .map_pass_err(scope)?;
1821 }
1822 ArcRenderCommand::EndOcclusionQuery => {
1823 api_log!("RenderPass::end_occlusion_query");
1824 let scope = PassErrorScope::EndOcclusionQuery;
1825
1826 end_occlusion_query(state.raw_encoder, &mut state.active_occlusion_query)
1827 .map_pass_err(scope)?;
1828 }
1829 ArcRenderCommand::BeginPipelineStatisticsQuery {
1830 query_set,
1831 query_index,
1832 } => {
1833 api_log!(
1834 "RenderPass::begin_pipeline_statistics_query {query_index} {}",
1835 query_set.error_ident()
1836 );
1837 let scope = PassErrorScope::BeginPipelineStatisticsQuery;
1838
1839 validate_and_begin_pipeline_statistics_query(
1840 query_set,
1841 state.raw_encoder,
1842 &mut state.tracker.query_sets,
1843 cmd_buf.as_ref(),
1844 query_index,
1845 Some(&mut cmd_buf_data.pending_query_resets),
1846 &mut state.active_pipeline_statistics_query,
1847 )
1848 .map_pass_err(scope)?;
1849 }
1850 ArcRenderCommand::EndPipelineStatisticsQuery => {
1851 api_log!("RenderPass::end_pipeline_statistics_query");
1852 let scope = PassErrorScope::EndPipelineStatisticsQuery;
1853
1854 end_pipeline_statistics_query(
1855 state.raw_encoder,
1856 &mut state.active_pipeline_statistics_query,
1857 )
1858 .map_pass_err(scope)?;
1859 }
1860 ArcRenderCommand::ExecuteBundle(bundle) => {
1861 let scope = PassErrorScope::ExecuteBundle;
1862 execute_bundle(&mut state, cmd_buf, bundle).map_pass_err(scope)?;
1863 }
1864 }
1865 }
1866
1867 let (trackers, pending_discard_init_fixups) = state
1868 .info
1869 .finish(state.raw_encoder, state.snatch_guard)
1870 .map_pass_err(pass_scope)?;
1871
1872 encoder.close(&cmd_buf.device).map_pass_err(pass_scope)?;
1873 (trackers, pending_discard_init_fixups)
1874 };
1875
1876 let encoder = &mut cmd_buf_data.encoder;
1877 let status = &mut cmd_buf_data.status;
1878 let tracker = &mut cmd_buf_data.trackers;
1879
1880 {
1881 let transit = encoder.open(&cmd_buf.device).map_pass_err(pass_scope)?;
1882
1883 fixup_discarded_surfaces(
1884 pending_discard_init_fixups.into_iter(),
1885 transit,
1886 &mut tracker.textures,
1887 &cmd_buf.device,
1888 snatch_guard,
1889 );
1890
1891 cmd_buf_data.pending_query_resets.reset_queries(transit);
1892
1893 CommandBuffer::insert_barriers_from_scope(transit, tracker, &scope, snatch_guard);
1894 }
1895
1896 *status = CommandEncoderStatus::Recording;
1897 encoder
1898 .close_and_swap(&cmd_buf.device)
1899 .map_pass_err(pass_scope)?;
1900
1901 Ok(())
1902 }
1903}
1904
1905fn set_bind_group(
1906 state: &mut State,
1907 cmd_buf: &Arc<CommandBuffer>,
1908 dynamic_offsets: &[DynamicOffset],
1909 index: u32,
1910 num_dynamic_offsets: usize,
1911 bind_group: Option<Arc<BindGroup>>,
1912) -> Result<(), RenderPassErrorInner> {
1913 if bind_group.is_none() {
1914 api_log!("RenderPass::set_bind_group {index} None");
1915 } else {
1916 api_log!(
1917 "RenderPass::set_bind_group {index} {}",
1918 bind_group.as_ref().unwrap().error_ident()
1919 );
1920 }
1921
1922 let max_bind_groups = state.device.limits.max_bind_groups;
1923 if index >= max_bind_groups {
1924 return Err(RenderCommandError::BindGroupIndexOutOfRange {
1925 index,
1926 max: max_bind_groups,
1927 }
1928 .into());
1929 }
1930
1931 state.temp_offsets.clear();
1932 state.temp_offsets.extend_from_slice(
1933 &dynamic_offsets
1934 [state.dynamic_offset_count..state.dynamic_offset_count + num_dynamic_offsets],
1935 );
1936 state.dynamic_offset_count += num_dynamic_offsets;
1937
1938 if bind_group.is_none() {
1939 return Ok(());
1941 }
1942
1943 let bind_group = bind_group.unwrap();
1944 let bind_group = state.tracker.bind_groups.insert_single(bind_group);
1945
1946 bind_group.same_device_as(cmd_buf.as_ref())?;
1947
1948 bind_group.validate_dynamic_bindings(index, &state.temp_offsets)?;
1949
1950 unsafe {
1952 state.info.usage_scope.merge_bind_group(&bind_group.used)?;
1953 }
1954 state
1958 .buffer_memory_init_actions
1959 .extend(bind_group.used_buffer_ranges.iter().filter_map(|action| {
1960 action
1961 .buffer
1962 .initialization_status
1963 .read()
1964 .check_action(action)
1965 }));
1966 for action in bind_group.used_texture_ranges.iter() {
1967 state
1968 .info
1969 .pending_discard_init_fixups
1970 .extend(state.texture_memory_actions.register_init_action(action));
1971 }
1972
1973 let pipeline_layout = state.binder.pipeline_layout.clone();
1974 let entries = state
1975 .binder
1976 .assign_group(index as usize, bind_group, &state.temp_offsets);
1977 if !entries.is_empty() && pipeline_layout.is_some() {
1978 let pipeline_layout = pipeline_layout.as_ref().unwrap().raw();
1979 for (i, e) in entries.iter().enumerate() {
1980 if let Some(group) = e.group.as_ref() {
1981 let raw_bg = group.try_raw(state.snatch_guard)?;
1982 unsafe {
1983 state.raw_encoder.set_bind_group(
1984 pipeline_layout,
1985 index + i as u32,
1986 Some(raw_bg),
1987 &e.dynamic_offsets,
1988 );
1989 }
1990 }
1991 }
1992 }
1993 Ok(())
1994}
1995
1996fn set_pipeline(
1997 state: &mut State,
1998 cmd_buf: &Arc<CommandBuffer>,
1999 pipeline: Arc<RenderPipeline>,
2000) -> Result<(), RenderPassErrorInner> {
2001 api_log!("RenderPass::set_pipeline {}", pipeline.error_ident());
2002
2003 state.pipeline = Some(pipeline.clone());
2004
2005 let pipeline = state.tracker.render_pipelines.insert_single(pipeline);
2006
2007 pipeline.same_device_as(cmd_buf.as_ref())?;
2008
2009 state
2010 .info
2011 .context
2012 .check_compatible(&pipeline.pass_context, pipeline.as_ref())
2013 .map_err(RenderCommandError::IncompatiblePipelineTargets)?;
2014
2015 state.pipeline_flags = pipeline.flags;
2016
2017 if pipeline.flags.contains(PipelineFlags::WRITES_DEPTH) && state.info.is_depth_read_only {
2018 return Err(RenderCommandError::IncompatibleDepthAccess(pipeline.error_ident()).into());
2019 }
2020 if pipeline.flags.contains(PipelineFlags::WRITES_STENCIL) && state.info.is_stencil_read_only {
2021 return Err(RenderCommandError::IncompatibleStencilAccess(pipeline.error_ident()).into());
2022 }
2023
2024 state
2025 .blend_constant
2026 .require(pipeline.flags.contains(PipelineFlags::BLEND_CONSTANT));
2027
2028 unsafe {
2029 state.raw_encoder.set_render_pipeline(pipeline.raw());
2030 }
2031
2032 if pipeline.flags.contains(PipelineFlags::STENCIL_REFERENCE) {
2033 unsafe {
2034 state
2035 .raw_encoder
2036 .set_stencil_reference(state.stencil_reference);
2037 }
2038 }
2039
2040 if state.binder.pipeline_layout.is_none()
2042 || !state
2043 .binder
2044 .pipeline_layout
2045 .as_ref()
2046 .unwrap()
2047 .is_equal(&pipeline.layout)
2048 {
2049 let (start_index, entries) = state
2050 .binder
2051 .change_pipeline_layout(&pipeline.layout, &pipeline.late_sized_buffer_groups);
2052 if !entries.is_empty() {
2053 for (i, e) in entries.iter().enumerate() {
2054 if let Some(group) = e.group.as_ref() {
2055 let raw_bg = group.try_raw(state.snatch_guard)?;
2056 unsafe {
2057 state.raw_encoder.set_bind_group(
2058 pipeline.layout.raw(),
2059 start_index as u32 + i as u32,
2060 Some(raw_bg),
2061 &e.dynamic_offsets,
2062 );
2063 }
2064 }
2065 }
2066 }
2067
2068 let non_overlapping =
2070 super::bind::compute_nonoverlapping_ranges(&pipeline.layout.push_constant_ranges);
2071 for range in non_overlapping {
2072 let offset = range.range.start;
2073 let size_bytes = range.range.end - offset;
2074 super::push_constant_clear(offset, size_bytes, |clear_offset, clear_data| unsafe {
2075 state.raw_encoder.set_push_constants(
2076 pipeline.layout.raw(),
2077 range.stages,
2078 clear_offset,
2079 clear_data,
2080 );
2081 });
2082 }
2083 }
2084
2085 while state.vertex.inputs.len() < pipeline.vertex_steps.len() {
2091 state.vertex.inputs.push(VertexBufferState::EMPTY);
2092 }
2093
2094 let mut steps = pipeline.vertex_steps.iter();
2096 for input in state.vertex.inputs.iter_mut() {
2097 input.step = steps.next().cloned().unwrap_or_default();
2098 }
2099
2100 state.vertex.update_limits();
2102 Ok(())
2103}
2104
2105fn set_index_buffer(
2106 state: &mut State,
2107 cmd_buf: &Arc<CommandBuffer>,
2108 buffer: Arc<crate::resource::Buffer>,
2109 index_format: IndexFormat,
2110 offset: u64,
2111 size: Option<BufferSize>,
2112) -> Result<(), RenderPassErrorInner> {
2113 api_log!("RenderPass::set_index_buffer {}", buffer.error_ident());
2114
2115 state
2116 .info
2117 .usage_scope
2118 .buffers
2119 .merge_single(&buffer, hal::BufferUses::INDEX)?;
2120
2121 buffer.same_device_as(cmd_buf.as_ref())?;
2122
2123 buffer.check_usage(BufferUsages::INDEX)?;
2124 let buf_raw = buffer.try_raw(state.snatch_guard)?;
2125
2126 let end = match size {
2127 Some(s) => offset + s.get(),
2128 None => buffer.size,
2129 };
2130 state.index.update_buffer(offset..end, index_format);
2131
2132 state
2133 .buffer_memory_init_actions
2134 .extend(buffer.initialization_status.read().create_action(
2135 &buffer,
2136 offset..end,
2137 MemoryInitKind::NeedsInitializedMemory,
2138 ));
2139
2140 let bb = hal::BufferBinding {
2141 buffer: buf_raw,
2142 offset,
2143 size,
2144 };
2145 unsafe {
2146 hal::DynCommandEncoder::set_index_buffer(state.raw_encoder, bb, index_format);
2147 }
2148 Ok(())
2149}
2150
2151fn set_vertex_buffer(
2152 state: &mut State,
2153 cmd_buf: &Arc<CommandBuffer>,
2154 slot: u32,
2155 buffer: Arc<crate::resource::Buffer>,
2156 offset: u64,
2157 size: Option<BufferSize>,
2158) -> Result<(), RenderPassErrorInner> {
2159 api_log!(
2160 "RenderPass::set_vertex_buffer {slot} {}",
2161 buffer.error_ident()
2162 );
2163
2164 state
2165 .info
2166 .usage_scope
2167 .buffers
2168 .merge_single(&buffer, hal::BufferUses::VERTEX)?;
2169
2170 buffer.same_device_as(cmd_buf.as_ref())?;
2171
2172 let max_vertex_buffers = state.device.limits.max_vertex_buffers;
2173 if slot >= max_vertex_buffers {
2174 return Err(RenderCommandError::VertexBufferIndexOutOfRange {
2175 index: slot,
2176 max: max_vertex_buffers,
2177 }
2178 .into());
2179 }
2180
2181 buffer.check_usage(BufferUsages::VERTEX)?;
2182 let buf_raw = buffer.try_raw(state.snatch_guard)?;
2183
2184 let empty_slots = (1 + slot as usize).saturating_sub(state.vertex.inputs.len());
2185 state
2186 .vertex
2187 .inputs
2188 .extend(iter::repeat(VertexBufferState::EMPTY).take(empty_slots));
2189 let vertex_state = &mut state.vertex.inputs[slot as usize];
2190 vertex_state.total_size = match size {
2192 Some(s) => s.get(),
2193 None => buffer.size - offset,
2194 };
2195 vertex_state.bound = true;
2196
2197 state
2198 .buffer_memory_init_actions
2199 .extend(buffer.initialization_status.read().create_action(
2200 &buffer,
2201 offset..(offset + vertex_state.total_size),
2202 MemoryInitKind::NeedsInitializedMemory,
2203 ));
2204
2205 let bb = hal::BufferBinding {
2206 buffer: buf_raw,
2207 offset,
2208 size,
2209 };
2210 unsafe {
2211 hal::DynCommandEncoder::set_vertex_buffer(state.raw_encoder, slot, bb);
2212 }
2213 state.vertex.update_limits();
2214 Ok(())
2215}
2216
2217fn set_blend_constant(state: &mut State, color: &Color) {
2218 api_log!("RenderPass::set_blend_constant");
2219
2220 state.blend_constant = OptionalState::Set;
2221 let array = [
2222 color.r as f32,
2223 color.g as f32,
2224 color.b as f32,
2225 color.a as f32,
2226 ];
2227 unsafe {
2228 state.raw_encoder.set_blend_constants(&array);
2229 }
2230}
2231
2232fn set_stencil_reference(state: &mut State, value: u32) {
2233 api_log!("RenderPass::set_stencil_reference {value}");
2234
2235 state.stencil_reference = value;
2236 if state
2237 .pipeline_flags
2238 .contains(PipelineFlags::STENCIL_REFERENCE)
2239 {
2240 unsafe {
2241 state.raw_encoder.set_stencil_reference(value);
2242 }
2243 }
2244}
2245
2246fn set_viewport(
2247 state: &mut State,
2248 rect: Rect<f32>,
2249 depth_min: f32,
2250 depth_max: f32,
2251) -> Result<(), RenderPassErrorInner> {
2252 api_log!("RenderPass::set_viewport {rect:?}");
2253 if rect.x < 0.0
2254 || rect.y < 0.0
2255 || rect.w <= 0.0
2256 || rect.h <= 0.0
2257 || rect.x + rect.w > state.info.extent.width as f32
2258 || rect.y + rect.h > state.info.extent.height as f32
2259 {
2260 return Err(RenderCommandError::InvalidViewportRect(rect, state.info.extent).into());
2261 }
2262 if !(0.0..=1.0).contains(&depth_min) || !(0.0..=1.0).contains(&depth_max) {
2263 return Err(RenderCommandError::InvalidViewportDepth(depth_min, depth_max).into());
2264 }
2265 let r = hal::Rect {
2266 x: rect.x,
2267 y: rect.y,
2268 w: rect.w,
2269 h: rect.h,
2270 };
2271 unsafe {
2272 state.raw_encoder.set_viewport(&r, depth_min..depth_max);
2273 }
2274 Ok(())
2275}
2276
2277fn set_push_constant(
2278 state: &mut State,
2279 push_constant_data: &[u32],
2280 stages: ShaderStages,
2281 offset: u32,
2282 size_bytes: u32,
2283 values_offset: Option<u32>,
2284) -> Result<(), RenderPassErrorInner> {
2285 api_log!("RenderPass::set_push_constants");
2286
2287 let values_offset = values_offset.ok_or(RenderPassErrorInner::InvalidValuesOffset)?;
2288
2289 let end_offset_bytes = offset + size_bytes;
2290 let values_end_offset = (values_offset + size_bytes / wgt::PUSH_CONSTANT_ALIGNMENT) as usize;
2291 let data_slice = &push_constant_data[(values_offset as usize)..values_end_offset];
2292
2293 let pipeline_layout = state
2294 .binder
2295 .pipeline_layout
2296 .as_ref()
2297 .ok_or(DrawError::MissingPipeline)?;
2298
2299 pipeline_layout
2300 .validate_push_constant_ranges(stages, offset, end_offset_bytes)
2301 .map_err(RenderCommandError::from)?;
2302
2303 unsafe {
2304 state
2305 .raw_encoder
2306 .set_push_constants(pipeline_layout.raw(), stages, offset, data_slice)
2307 }
2308 Ok(())
2309}
2310
2311fn set_scissor(state: &mut State, rect: Rect<u32>) -> Result<(), RenderPassErrorInner> {
2312 api_log!("RenderPass::set_scissor_rect {rect:?}");
2313
2314 if rect.x + rect.w > state.info.extent.width || rect.y + rect.h > state.info.extent.height {
2315 return Err(RenderCommandError::InvalidScissorRect(rect, state.info.extent).into());
2316 }
2317 let r = hal::Rect {
2318 x: rect.x,
2319 y: rect.y,
2320 w: rect.w,
2321 h: rect.h,
2322 };
2323 unsafe {
2324 state.raw_encoder.set_scissor_rect(&r);
2325 }
2326 Ok(())
2327}
2328
2329fn draw(
2330 state: &mut State,
2331 vertex_count: u32,
2332 instance_count: u32,
2333 first_vertex: u32,
2334 first_instance: u32,
2335) -> Result<(), DrawError> {
2336 api_log!("RenderPass::draw {vertex_count} {instance_count} {first_vertex} {first_instance}");
2337
2338 state.is_ready(false)?;
2339
2340 let last_vertex = first_vertex as u64 + vertex_count as u64;
2341 let vertex_limit = state.vertex.vertex_limit;
2342 if last_vertex > vertex_limit {
2343 return Err(DrawError::VertexBeyondLimit {
2344 last_vertex,
2345 vertex_limit,
2346 slot: state.vertex.vertex_limit_slot,
2347 });
2348 }
2349 let last_instance = first_instance as u64 + instance_count as u64;
2350 let instance_limit = state.vertex.instance_limit;
2351 if last_instance > instance_limit {
2352 return Err(DrawError::InstanceBeyondLimit {
2353 last_instance,
2354 instance_limit,
2355 slot: state.vertex.instance_limit_slot,
2356 });
2357 }
2358
2359 unsafe {
2360 if instance_count > 0 && vertex_count > 0 {
2361 state
2362 .raw_encoder
2363 .draw(first_vertex, vertex_count, first_instance, instance_count);
2364 }
2365 }
2366 Ok(())
2367}
2368
2369fn draw_indexed(
2370 state: &mut State,
2371 index_count: u32,
2372 instance_count: u32,
2373 first_index: u32,
2374 base_vertex: i32,
2375 first_instance: u32,
2376) -> Result<(), DrawError> {
2377 api_log!("RenderPass::draw_indexed {index_count} {instance_count} {first_index} {base_vertex} {first_instance}");
2378
2379 state.is_ready(true)?;
2380
2381 let last_index = first_index as u64 + index_count as u64;
2382 let index_limit = state.index.limit;
2383 if last_index > index_limit {
2384 return Err(DrawError::IndexBeyondLimit {
2385 last_index,
2386 index_limit,
2387 });
2388 }
2389 let last_instance = first_instance as u64 + instance_count as u64;
2390 let instance_limit = state.vertex.instance_limit;
2391 if last_instance > instance_limit {
2392 return Err(DrawError::InstanceBeyondLimit {
2393 last_instance,
2394 instance_limit,
2395 slot: state.vertex.instance_limit_slot,
2396 });
2397 }
2398
2399 unsafe {
2400 if instance_count > 0 && index_count > 0 {
2401 state.raw_encoder.draw_indexed(
2402 first_index,
2403 index_count,
2404 base_vertex,
2405 first_instance,
2406 instance_count,
2407 );
2408 }
2409 }
2410 Ok(())
2411}
2412
2413fn multi_draw_indirect(
2414 state: &mut State,
2415 cmd_buf: &Arc<CommandBuffer>,
2416 indirect_buffer: Arc<crate::resource::Buffer>,
2417 offset: u64,
2418 count: Option<NonZeroU32>,
2419 indexed: bool,
2420) -> Result<(), RenderPassErrorInner> {
2421 api_log!(
2422 "RenderPass::draw_indirect (indexed:{indexed}) {} {offset} {count:?}",
2423 indirect_buffer.error_ident()
2424 );
2425
2426 state.is_ready(indexed)?;
2427
2428 let stride = match indexed {
2429 false => size_of::<wgt::DrawIndirectArgs>(),
2430 true => size_of::<wgt::DrawIndexedIndirectArgs>(),
2431 };
2432
2433 if count.is_some() {
2434 state
2435 .device
2436 .require_features(wgt::Features::MULTI_DRAW_INDIRECT)?;
2437 }
2438 state
2439 .device
2440 .require_downlevel_flags(wgt::DownlevelFlags::INDIRECT_EXECUTION)?;
2441
2442 indirect_buffer.same_device_as(cmd_buf.as_ref())?;
2443
2444 state
2445 .info
2446 .usage_scope
2447 .buffers
2448 .merge_single(&indirect_buffer, hal::BufferUses::INDIRECT)?;
2449
2450 indirect_buffer.check_usage(BufferUsages::INDIRECT)?;
2451 let indirect_raw = indirect_buffer.try_raw(state.snatch_guard)?;
2452
2453 let actual_count = count.map_or(1, |c| c.get());
2454
2455 if offset % 4 != 0 {
2456 return Err(RenderPassErrorInner::UnalignedIndirectBufferOffset(offset));
2457 }
2458
2459 let end_offset = offset + stride as u64 * actual_count as u64;
2460 if end_offset > indirect_buffer.size {
2461 return Err(RenderPassErrorInner::IndirectBufferOverrun {
2462 count,
2463 offset,
2464 end_offset,
2465 buffer_size: indirect_buffer.size,
2466 });
2467 }
2468
2469 state.buffer_memory_init_actions.extend(
2470 indirect_buffer.initialization_status.read().create_action(
2471 &indirect_buffer,
2472 offset..end_offset,
2473 MemoryInitKind::NeedsInitializedMemory,
2474 ),
2475 );
2476
2477 match indexed {
2478 false => unsafe {
2479 state
2480 .raw_encoder
2481 .draw_indirect(indirect_raw, offset, actual_count);
2482 },
2483 true => unsafe {
2484 state
2485 .raw_encoder
2486 .draw_indexed_indirect(indirect_raw, offset, actual_count);
2487 },
2488 }
2489 Ok(())
2490}
2491
2492fn multi_draw_indirect_count(
2493 state: &mut State,
2494 cmd_buf: &Arc<CommandBuffer>,
2495 indirect_buffer: Arc<crate::resource::Buffer>,
2496 offset: u64,
2497 count_buffer: Arc<crate::resource::Buffer>,
2498 count_buffer_offset: u64,
2499 max_count: u32,
2500 indexed: bool,
2501) -> Result<(), RenderPassErrorInner> {
2502 api_log!(
2503 "RenderPass::multi_draw_indirect_count (indexed:{indexed}) {} {offset} {} {count_buffer_offset:?} {max_count:?}",
2504 indirect_buffer.error_ident(),
2505 count_buffer.error_ident()
2506 );
2507
2508 state.is_ready(indexed)?;
2509
2510 let stride = match indexed {
2511 false => size_of::<wgt::DrawIndirectArgs>(),
2512 true => size_of::<wgt::DrawIndexedIndirectArgs>(),
2513 } as u64;
2514
2515 state
2516 .device
2517 .require_features(wgt::Features::MULTI_DRAW_INDIRECT_COUNT)?;
2518 state
2519 .device
2520 .require_downlevel_flags(wgt::DownlevelFlags::INDIRECT_EXECUTION)?;
2521
2522 indirect_buffer.same_device_as(cmd_buf.as_ref())?;
2523 count_buffer.same_device_as(cmd_buf.as_ref())?;
2524
2525 state
2526 .info
2527 .usage_scope
2528 .buffers
2529 .merge_single(&indirect_buffer, hal::BufferUses::INDIRECT)?;
2530
2531 indirect_buffer.check_usage(BufferUsages::INDIRECT)?;
2532 let indirect_raw = indirect_buffer.try_raw(state.snatch_guard)?;
2533
2534 state
2535 .info
2536 .usage_scope
2537 .buffers
2538 .merge_single(&count_buffer, hal::BufferUses::INDIRECT)?;
2539
2540 count_buffer.check_usage(BufferUsages::INDIRECT)?;
2541 let count_raw = count_buffer.try_raw(state.snatch_guard)?;
2542
2543 if offset % 4 != 0 {
2544 return Err(RenderPassErrorInner::UnalignedIndirectBufferOffset(offset));
2545 }
2546
2547 let end_offset = offset + stride * max_count as u64;
2548 if end_offset > indirect_buffer.size {
2549 return Err(RenderPassErrorInner::IndirectBufferOverrun {
2550 count: None,
2551 offset,
2552 end_offset,
2553 buffer_size: indirect_buffer.size,
2554 });
2555 }
2556 state.buffer_memory_init_actions.extend(
2557 indirect_buffer.initialization_status.read().create_action(
2558 &indirect_buffer,
2559 offset..end_offset,
2560 MemoryInitKind::NeedsInitializedMemory,
2561 ),
2562 );
2563
2564 let begin_count_offset = count_buffer_offset;
2565 let end_count_offset = count_buffer_offset + 4;
2566 if end_count_offset > count_buffer.size {
2567 return Err(RenderPassErrorInner::IndirectCountBufferOverrun {
2568 begin_count_offset,
2569 end_count_offset,
2570 count_buffer_size: count_buffer.size,
2571 });
2572 }
2573 state.buffer_memory_init_actions.extend(
2574 count_buffer.initialization_status.read().create_action(
2575 &count_buffer,
2576 count_buffer_offset..end_count_offset,
2577 MemoryInitKind::NeedsInitializedMemory,
2578 ),
2579 );
2580
2581 match indexed {
2582 false => unsafe {
2583 state.raw_encoder.draw_indirect_count(
2584 indirect_raw,
2585 offset,
2586 count_raw,
2587 count_buffer_offset,
2588 max_count,
2589 );
2590 },
2591 true => unsafe {
2592 state.raw_encoder.draw_indexed_indirect_count(
2593 indirect_raw,
2594 offset,
2595 count_raw,
2596 count_buffer_offset,
2597 max_count,
2598 );
2599 },
2600 }
2601 Ok(())
2602}
2603
2604fn push_debug_group(state: &mut State, string_data: &[u8], len: usize) {
2605 state.debug_scope_depth += 1;
2606 if !state
2607 .device
2608 .instance_flags
2609 .contains(wgt::InstanceFlags::DISCARD_HAL_LABELS)
2610 {
2611 let label =
2612 str::from_utf8(&string_data[state.string_offset..state.string_offset + len]).unwrap();
2613
2614 api_log!("RenderPass::push_debug_group {label:?}");
2615 unsafe {
2616 state.raw_encoder.begin_debug_marker(label);
2617 }
2618 }
2619 state.string_offset += len;
2620}
2621
2622fn pop_debug_group(state: &mut State) -> Result<(), RenderPassErrorInner> {
2623 api_log!("RenderPass::pop_debug_group");
2624
2625 if state.debug_scope_depth == 0 {
2626 return Err(RenderPassErrorInner::InvalidPopDebugGroup);
2627 }
2628 state.debug_scope_depth -= 1;
2629 if !state
2630 .device
2631 .instance_flags
2632 .contains(wgt::InstanceFlags::DISCARD_HAL_LABELS)
2633 {
2634 unsafe {
2635 state.raw_encoder.end_debug_marker();
2636 }
2637 }
2638 Ok(())
2639}
2640
2641fn insert_debug_marker(state: &mut State, string_data: &[u8], len: usize) {
2642 if !state
2643 .device
2644 .instance_flags
2645 .contains(wgt::InstanceFlags::DISCARD_HAL_LABELS)
2646 {
2647 let label =
2648 str::from_utf8(&string_data[state.string_offset..state.string_offset + len]).unwrap();
2649 api_log!("RenderPass::insert_debug_marker {label:?}");
2650 unsafe {
2651 state.raw_encoder.insert_debug_marker(label);
2652 }
2653 }
2654 state.string_offset += len;
2655}
2656
2657fn write_timestamp(
2658 state: &mut State,
2659 cmd_buf: &CommandBuffer,
2660 pending_query_resets: &mut QueryResetMap,
2661 query_set: Arc<QuerySet>,
2662 query_index: u32,
2663) -> Result<(), RenderPassErrorInner> {
2664 api_log!(
2665 "RenderPass::write_timestamps {query_index} {}",
2666 query_set.error_ident()
2667 );
2668
2669 query_set.same_device_as(cmd_buf)?;
2670
2671 state
2672 .device
2673 .require_features(wgt::Features::TIMESTAMP_QUERY_INSIDE_PASSES)?;
2674
2675 let query_set = state.tracker.query_sets.insert_single(query_set);
2676
2677 query_set.validate_and_write_timestamp(
2678 state.raw_encoder,
2679 query_index,
2680 Some(pending_query_resets),
2681 )?;
2682 Ok(())
2683}
2684
2685fn execute_bundle(
2686 state: &mut State,
2687 cmd_buf: &Arc<CommandBuffer>,
2688 bundle: Arc<super::RenderBundle>,
2689) -> Result<(), RenderPassErrorInner> {
2690 api_log!("RenderPass::execute_bundle {}", bundle.error_ident());
2691
2692 let bundle = state.tracker.bundles.insert_single(bundle);
2693
2694 bundle.same_device_as(cmd_buf.as_ref())?;
2695
2696 state
2697 .info
2698 .context
2699 .check_compatible(&bundle.context, bundle.as_ref())
2700 .map_err(RenderPassErrorInner::IncompatibleBundleTargets)?;
2701
2702 if (state.info.is_depth_read_only && !bundle.is_depth_read_only)
2703 || (state.info.is_stencil_read_only && !bundle.is_stencil_read_only)
2704 {
2705 return Err(
2706 RenderPassErrorInner::IncompatibleBundleReadOnlyDepthStencil {
2707 pass_depth: state.info.is_depth_read_only,
2708 pass_stencil: state.info.is_stencil_read_only,
2709 bundle_depth: bundle.is_depth_read_only,
2710 bundle_stencil: bundle.is_stencil_read_only,
2711 },
2712 );
2713 }
2714
2715 state
2716 .buffer_memory_init_actions
2717 .extend(
2718 bundle
2719 .buffer_memory_init_actions
2720 .iter()
2721 .filter_map(|action| {
2722 action
2723 .buffer
2724 .initialization_status
2725 .read()
2726 .check_action(action)
2727 }),
2728 );
2729 for action in bundle.texture_memory_init_actions.iter() {
2730 state
2731 .info
2732 .pending_discard_init_fixups
2733 .extend(state.texture_memory_actions.register_init_action(action));
2734 }
2735
2736 unsafe { bundle.execute(state.raw_encoder, state.snatch_guard) }.map_err(|e| match e {
2737 ExecutionError::DestroyedResource(e) => RenderCommandError::DestroyedResource(e),
2738 ExecutionError::Unimplemented(what) => RenderCommandError::Unimplemented(what),
2739 })?;
2740
2741 unsafe {
2742 state.info.usage_scope.merge_render_bundle(&bundle.used)?;
2743 };
2744 state.reset_bundle();
2745 Ok(())
2746}
2747
2748impl Global {
2749 fn resolve_render_pass_buffer_id(
2750 &self,
2751 scope: PassErrorScope,
2752 buffer_id: id::Id<id::markers::Buffer>,
2753 ) -> Result<Arc<crate::resource::Buffer>, RenderPassError> {
2754 let hub = &self.hub;
2755 let buffer = hub.buffers.get(buffer_id).get().map_pass_err(scope)?;
2756
2757 Ok(buffer)
2758 }
2759
2760 fn resolve_render_pass_query_set(
2761 &self,
2762 scope: PassErrorScope,
2763 query_set_id: id::Id<id::markers::QuerySet>,
2764 ) -> Result<Arc<QuerySet>, RenderPassError> {
2765 let hub = &self.hub;
2766 let query_set = hub.query_sets.get(query_set_id).get().map_pass_err(scope)?;
2767
2768 Ok(query_set)
2769 }
2770
2771 pub fn render_pass_set_bind_group(
2772 &self,
2773 pass: &mut RenderPass,
2774 index: u32,
2775 bind_group_id: Option<id::BindGroupId>,
2776 offsets: &[DynamicOffset],
2777 ) -> Result<(), RenderPassError> {
2778 let scope = PassErrorScope::SetBindGroup;
2779 let base = pass
2780 .base
2781 .as_mut()
2782 .ok_or(RenderPassErrorInner::PassEnded)
2783 .map_pass_err(scope)?;
2784
2785 if pass.current_bind_groups.set_and_check_redundant(
2786 bind_group_id,
2787 index,
2788 &mut base.dynamic_offsets,
2789 offsets,
2790 ) {
2791 return Ok(());
2793 }
2794
2795 let mut bind_group = None;
2796 if bind_group_id.is_some() {
2797 let bind_group_id = bind_group_id.unwrap();
2798
2799 let hub = &self.hub;
2800 let bg = hub
2801 .bind_groups
2802 .get(bind_group_id)
2803 .get()
2804 .map_pass_err(scope)?;
2805 bind_group = Some(bg);
2806 }
2807
2808 base.commands.push(ArcRenderCommand::SetBindGroup {
2809 index,
2810 num_dynamic_offsets: offsets.len(),
2811 bind_group,
2812 });
2813
2814 Ok(())
2815 }
2816
2817 pub fn render_pass_set_pipeline(
2818 &self,
2819 pass: &mut RenderPass,
2820 pipeline_id: id::RenderPipelineId,
2821 ) -> Result<(), RenderPassError> {
2822 let scope = PassErrorScope::SetPipelineRender;
2823
2824 let redundant = pass.current_pipeline.set_and_check_redundant(pipeline_id);
2825 let base = pass.base_mut(scope)?;
2826
2827 if redundant {
2828 return Ok(());
2830 }
2831
2832 let hub = &self.hub;
2833 let pipeline = hub
2834 .render_pipelines
2835 .get(pipeline_id)
2836 .get()
2837 .map_pass_err(scope)?;
2838
2839 base.commands.push(ArcRenderCommand::SetPipeline(pipeline));
2840
2841 Ok(())
2842 }
2843
2844 pub fn render_pass_set_index_buffer(
2845 &self,
2846 pass: &mut RenderPass,
2847 buffer_id: id::BufferId,
2848 index_format: IndexFormat,
2849 offset: BufferAddress,
2850 size: Option<BufferSize>,
2851 ) -> Result<(), RenderPassError> {
2852 let scope = PassErrorScope::SetIndexBuffer;
2853 let base = pass.base_mut(scope)?;
2854
2855 base.commands.push(ArcRenderCommand::SetIndexBuffer {
2856 buffer: self.resolve_render_pass_buffer_id(scope, buffer_id)?,
2857 index_format,
2858 offset,
2859 size,
2860 });
2861
2862 Ok(())
2863 }
2864
2865 pub fn render_pass_set_vertex_buffer(
2866 &self,
2867 pass: &mut RenderPass,
2868 slot: u32,
2869 buffer_id: id::BufferId,
2870 offset: BufferAddress,
2871 size: Option<BufferSize>,
2872 ) -> Result<(), RenderPassError> {
2873 let scope = PassErrorScope::SetVertexBuffer;
2874 let base = pass.base_mut(scope)?;
2875
2876 base.commands.push(ArcRenderCommand::SetVertexBuffer {
2877 slot,
2878 buffer: self.resolve_render_pass_buffer_id(scope, buffer_id)?,
2879 offset,
2880 size,
2881 });
2882
2883 Ok(())
2884 }
2885
2886 pub fn render_pass_set_blend_constant(
2887 &self,
2888 pass: &mut RenderPass,
2889 color: Color,
2890 ) -> Result<(), RenderPassError> {
2891 let scope = PassErrorScope::SetBlendConstant;
2892 let base = pass.base_mut(scope)?;
2893
2894 base.commands
2895 .push(ArcRenderCommand::SetBlendConstant(color));
2896
2897 Ok(())
2898 }
2899
2900 pub fn render_pass_set_stencil_reference(
2901 &self,
2902 pass: &mut RenderPass,
2903 value: u32,
2904 ) -> Result<(), RenderPassError> {
2905 let scope = PassErrorScope::SetStencilReference;
2906 let base = pass.base_mut(scope)?;
2907
2908 base.commands
2909 .push(ArcRenderCommand::SetStencilReference(value));
2910
2911 Ok(())
2912 }
2913
2914 pub fn render_pass_set_viewport(
2915 &self,
2916 pass: &mut RenderPass,
2917 x: f32,
2918 y: f32,
2919 w: f32,
2920 h: f32,
2921 depth_min: f32,
2922 depth_max: f32,
2923 ) -> Result<(), RenderPassError> {
2924 let scope = PassErrorScope::SetViewport;
2925 let base = pass.base_mut(scope)?;
2926
2927 base.commands.push(ArcRenderCommand::SetViewport {
2928 rect: Rect { x, y, w, h },
2929 depth_min,
2930 depth_max,
2931 });
2932
2933 Ok(())
2934 }
2935
2936 pub fn render_pass_set_scissor_rect(
2937 &self,
2938 pass: &mut RenderPass,
2939 x: u32,
2940 y: u32,
2941 w: u32,
2942 h: u32,
2943 ) -> Result<(), RenderPassError> {
2944 let scope = PassErrorScope::SetScissorRect;
2945 let base = pass.base_mut(scope)?;
2946
2947 base.commands
2948 .push(ArcRenderCommand::SetScissor(Rect { x, y, w, h }));
2949
2950 Ok(())
2951 }
2952
2953 pub fn render_pass_set_push_constants(
2954 &self,
2955 pass: &mut RenderPass,
2956 stages: ShaderStages,
2957 offset: u32,
2958 data: &[u8],
2959 ) -> Result<(), RenderPassError> {
2960 let scope = PassErrorScope::SetPushConstant;
2961 let base = pass.base_mut(scope)?;
2962
2963 if offset & (wgt::PUSH_CONSTANT_ALIGNMENT - 1) != 0 {
2964 return Err(RenderPassErrorInner::PushConstantOffsetAlignment).map_pass_err(scope);
2965 }
2966 if data.len() as u32 & (wgt::PUSH_CONSTANT_ALIGNMENT - 1) != 0 {
2967 return Err(RenderPassErrorInner::PushConstantSizeAlignment).map_pass_err(scope);
2968 }
2969
2970 let value_offset = base
2971 .push_constant_data
2972 .len()
2973 .try_into()
2974 .map_err(|_| RenderPassErrorInner::PushConstantOutOfMemory)
2975 .map_pass_err(scope)?;
2976
2977 base.push_constant_data.extend(
2978 data.chunks_exact(wgt::PUSH_CONSTANT_ALIGNMENT as usize)
2979 .map(|arr| u32::from_ne_bytes([arr[0], arr[1], arr[2], arr[3]])),
2980 );
2981
2982 base.commands.push(ArcRenderCommand::SetPushConstant {
2983 stages,
2984 offset,
2985 size_bytes: data.len() as u32,
2986 values_offset: Some(value_offset),
2987 });
2988
2989 Ok(())
2990 }
2991
2992 pub fn render_pass_draw(
2993 &self,
2994 pass: &mut RenderPass,
2995 vertex_count: u32,
2996 instance_count: u32,
2997 first_vertex: u32,
2998 first_instance: u32,
2999 ) -> Result<(), RenderPassError> {
3000 let scope = PassErrorScope::Draw {
3001 kind: DrawKind::Draw,
3002 indexed: false,
3003 };
3004 let base = pass.base_mut(scope)?;
3005
3006 base.commands.push(ArcRenderCommand::Draw {
3007 vertex_count,
3008 instance_count,
3009 first_vertex,
3010 first_instance,
3011 });
3012
3013 Ok(())
3014 }
3015
3016 pub fn render_pass_draw_indexed(
3017 &self,
3018 pass: &mut RenderPass,
3019 index_count: u32,
3020 instance_count: u32,
3021 first_index: u32,
3022 base_vertex: i32,
3023 first_instance: u32,
3024 ) -> Result<(), RenderPassError> {
3025 let scope = PassErrorScope::Draw {
3026 kind: DrawKind::Draw,
3027 indexed: true,
3028 };
3029 let base = pass.base_mut(scope)?;
3030
3031 base.commands.push(ArcRenderCommand::DrawIndexed {
3032 index_count,
3033 instance_count,
3034 first_index,
3035 base_vertex,
3036 first_instance,
3037 });
3038
3039 Ok(())
3040 }
3041
3042 pub fn render_pass_draw_indirect(
3043 &self,
3044 pass: &mut RenderPass,
3045 buffer_id: id::BufferId,
3046 offset: BufferAddress,
3047 ) -> Result<(), RenderPassError> {
3048 let scope = PassErrorScope::Draw {
3049 kind: DrawKind::DrawIndirect,
3050 indexed: false,
3051 };
3052 let base = pass.base_mut(scope)?;
3053
3054 base.commands.push(ArcRenderCommand::MultiDrawIndirect {
3055 buffer: self.resolve_render_pass_buffer_id(scope, buffer_id)?,
3056 offset,
3057 count: None,
3058 indexed: false,
3059 });
3060
3061 Ok(())
3062 }
3063
3064 pub fn render_pass_draw_indexed_indirect(
3065 &self,
3066 pass: &mut RenderPass,
3067 buffer_id: id::BufferId,
3068 offset: BufferAddress,
3069 ) -> Result<(), RenderPassError> {
3070 let scope = PassErrorScope::Draw {
3071 kind: DrawKind::DrawIndirect,
3072 indexed: true,
3073 };
3074 let base = pass.base_mut(scope)?;
3075
3076 base.commands.push(ArcRenderCommand::MultiDrawIndirect {
3077 buffer: self.resolve_render_pass_buffer_id(scope, buffer_id)?,
3078 offset,
3079 count: None,
3080 indexed: true,
3081 });
3082
3083 Ok(())
3084 }
3085
3086 pub fn render_pass_multi_draw_indirect(
3087 &self,
3088 pass: &mut RenderPass,
3089 buffer_id: id::BufferId,
3090 offset: BufferAddress,
3091 count: u32,
3092 ) -> Result<(), RenderPassError> {
3093 let scope = PassErrorScope::Draw {
3094 kind: DrawKind::MultiDrawIndirect,
3095 indexed: false,
3096 };
3097 let base = pass.base_mut(scope)?;
3098
3099 base.commands.push(ArcRenderCommand::MultiDrawIndirect {
3100 buffer: self.resolve_render_pass_buffer_id(scope, buffer_id)?,
3101 offset,
3102 count: NonZeroU32::new(count),
3103 indexed: false,
3104 });
3105
3106 Ok(())
3107 }
3108
3109 pub fn render_pass_multi_draw_indexed_indirect(
3110 &self,
3111 pass: &mut RenderPass,
3112 buffer_id: id::BufferId,
3113 offset: BufferAddress,
3114 count: u32,
3115 ) -> Result<(), RenderPassError> {
3116 let scope = PassErrorScope::Draw {
3117 kind: DrawKind::MultiDrawIndirect,
3118 indexed: true,
3119 };
3120 let base = pass.base_mut(scope)?;
3121
3122 base.commands.push(ArcRenderCommand::MultiDrawIndirect {
3123 buffer: self.resolve_render_pass_buffer_id(scope, buffer_id)?,
3124 offset,
3125 count: NonZeroU32::new(count),
3126 indexed: true,
3127 });
3128
3129 Ok(())
3130 }
3131
3132 pub fn render_pass_multi_draw_indirect_count(
3133 &self,
3134 pass: &mut RenderPass,
3135 buffer_id: id::BufferId,
3136 offset: BufferAddress,
3137 count_buffer_id: id::BufferId,
3138 count_buffer_offset: BufferAddress,
3139 max_count: u32,
3140 ) -> Result<(), RenderPassError> {
3141 let scope = PassErrorScope::Draw {
3142 kind: DrawKind::MultiDrawIndirectCount,
3143 indexed: false,
3144 };
3145 let base = pass.base_mut(scope)?;
3146
3147 base.commands
3148 .push(ArcRenderCommand::MultiDrawIndirectCount {
3149 buffer: self.resolve_render_pass_buffer_id(scope, buffer_id)?,
3150 offset,
3151 count_buffer: self.resolve_render_pass_buffer_id(scope, count_buffer_id)?,
3152 count_buffer_offset,
3153 max_count,
3154 indexed: false,
3155 });
3156
3157 Ok(())
3158 }
3159
3160 pub fn render_pass_multi_draw_indexed_indirect_count(
3161 &self,
3162 pass: &mut RenderPass,
3163 buffer_id: id::BufferId,
3164 offset: BufferAddress,
3165 count_buffer_id: id::BufferId,
3166 count_buffer_offset: BufferAddress,
3167 max_count: u32,
3168 ) -> Result<(), RenderPassError> {
3169 let scope = PassErrorScope::Draw {
3170 kind: DrawKind::MultiDrawIndirectCount,
3171 indexed: true,
3172 };
3173 let base = pass.base_mut(scope)?;
3174
3175 base.commands
3176 .push(ArcRenderCommand::MultiDrawIndirectCount {
3177 buffer: self.resolve_render_pass_buffer_id(scope, buffer_id)?,
3178 offset,
3179 count_buffer: self.resolve_render_pass_buffer_id(scope, count_buffer_id)?,
3180 count_buffer_offset,
3181 max_count,
3182 indexed: true,
3183 });
3184
3185 Ok(())
3186 }
3187
3188 pub fn render_pass_push_debug_group(
3189 &self,
3190 pass: &mut RenderPass,
3191 label: &str,
3192 color: u32,
3193 ) -> Result<(), RenderPassError> {
3194 let base = pass.base_mut(PassErrorScope::PushDebugGroup)?;
3195
3196 let bytes = label.as_bytes();
3197 base.string_data.extend_from_slice(bytes);
3198
3199 base.commands.push(ArcRenderCommand::PushDebugGroup {
3200 color,
3201 len: bytes.len(),
3202 });
3203
3204 Ok(())
3205 }
3206
3207 pub fn render_pass_pop_debug_group(
3208 &self,
3209 pass: &mut RenderPass,
3210 ) -> Result<(), RenderPassError> {
3211 let base = pass.base_mut(PassErrorScope::PopDebugGroup)?;
3212
3213 base.commands.push(ArcRenderCommand::PopDebugGroup);
3214
3215 Ok(())
3216 }
3217
3218 pub fn render_pass_insert_debug_marker(
3219 &self,
3220 pass: &mut RenderPass,
3221 label: &str,
3222 color: u32,
3223 ) -> Result<(), RenderPassError> {
3224 let base = pass.base_mut(PassErrorScope::InsertDebugMarker)?;
3225
3226 let bytes = label.as_bytes();
3227 base.string_data.extend_from_slice(bytes);
3228
3229 base.commands.push(ArcRenderCommand::InsertDebugMarker {
3230 color,
3231 len: bytes.len(),
3232 });
3233
3234 Ok(())
3235 }
3236
3237 pub fn render_pass_write_timestamp(
3238 &self,
3239 pass: &mut RenderPass,
3240 query_set_id: id::QuerySetId,
3241 query_index: u32,
3242 ) -> Result<(), RenderPassError> {
3243 let scope = PassErrorScope::WriteTimestamp;
3244 let base = pass.base_mut(scope)?;
3245
3246 base.commands.push(ArcRenderCommand::WriteTimestamp {
3247 query_set: self.resolve_render_pass_query_set(scope, query_set_id)?,
3248 query_index,
3249 });
3250
3251 Ok(())
3252 }
3253
3254 pub fn render_pass_begin_occlusion_query(
3255 &self,
3256 pass: &mut RenderPass,
3257 query_index: u32,
3258 ) -> Result<(), RenderPassError> {
3259 let scope = PassErrorScope::BeginOcclusionQuery;
3260 let base = pass.base_mut(scope)?;
3261
3262 base.commands
3263 .push(ArcRenderCommand::BeginOcclusionQuery { query_index });
3264
3265 Ok(())
3266 }
3267
3268 pub fn render_pass_end_occlusion_query(
3269 &self,
3270 pass: &mut RenderPass,
3271 ) -> Result<(), RenderPassError> {
3272 let scope = PassErrorScope::EndOcclusionQuery;
3273 let base = pass.base_mut(scope)?;
3274
3275 base.commands.push(ArcRenderCommand::EndOcclusionQuery);
3276
3277 Ok(())
3278 }
3279
3280 pub fn render_pass_begin_pipeline_statistics_query(
3281 &self,
3282 pass: &mut RenderPass,
3283 query_set_id: id::QuerySetId,
3284 query_index: u32,
3285 ) -> Result<(), RenderPassError> {
3286 let scope = PassErrorScope::BeginPipelineStatisticsQuery;
3287 let base = pass.base_mut(scope)?;
3288
3289 base.commands
3290 .push(ArcRenderCommand::BeginPipelineStatisticsQuery {
3291 query_set: self.resolve_render_pass_query_set(scope, query_set_id)?,
3292 query_index,
3293 });
3294
3295 Ok(())
3296 }
3297
3298 pub fn render_pass_end_pipeline_statistics_query(
3299 &self,
3300 pass: &mut RenderPass,
3301 ) -> Result<(), RenderPassError> {
3302 let scope = PassErrorScope::EndPipelineStatisticsQuery;
3303 let base = pass.base_mut(scope)?;
3304
3305 base.commands
3306 .push(ArcRenderCommand::EndPipelineStatisticsQuery);
3307
3308 Ok(())
3309 }
3310
3311 pub fn render_pass_execute_bundles(
3312 &self,
3313 pass: &mut RenderPass,
3314 render_bundle_ids: &[id::RenderBundleId],
3315 ) -> Result<(), RenderPassError> {
3316 let scope = PassErrorScope::ExecuteBundle;
3317 let base = pass.base_mut(scope)?;
3318
3319 let hub = &self.hub;
3320 let bundles = hub.render_bundles.read();
3321
3322 for &bundle_id in render_bundle_ids {
3323 let bundle = bundles.get(bundle_id).get().map_pass_err(scope)?;
3324
3325 base.commands.push(ArcRenderCommand::ExecuteBundle(bundle));
3326 }
3327 pass.current_pipeline.reset();
3328 pass.current_bind_groups.reset();
3329
3330 Ok(())
3331 }
3332}