1use crate::{
2 binding_model::BindError,
3 command::{
4 self,
5 bind::Binder,
6 end_pipeline_statistics_query,
7 memory_init::{fixup_discarded_surfaces, SurfacesInDiscardState},
8 BasePass, BasePassRef, BindGroupStateChange, CommandBuffer, CommandEncoderError,
9 CommandEncoderStatus, DrawError, ExecutionError, MapPassErr, PassErrorScope, QueryResetMap,
10 QueryUseError, RenderCommand, RenderCommandError, StateChange,
11 },
12 device::{
13 AttachmentData, Device, MissingDownlevelFlags, MissingFeatures,
14 RenderPassCompatibilityCheckType, RenderPassCompatibilityError, RenderPassContext,
15 },
16 error::{ErrorFormatter, PrettyError},
17 global::Global,
18 hal_api::HalApi,
19 hub::Token,
20 id,
21 identity::GlobalIdentityHandlerFactory,
22 init_tracker::{MemoryInitKind, TextureInitRange, TextureInitTrackerAction},
23 pipeline::{self, PipelineFlags},
24 resource::{self, Buffer, Texture, TextureView, TextureViewNotRenderableReason},
25 storage::Storage,
26 track::{TextureSelector, UsageConflict, UsageScope},
27 validation::{
28 check_buffer_usage, check_texture_usage, MissingBufferUsageError, MissingTextureUsageError,
29 },
30 Label, Stored,
31};
32
33use arrayvec::ArrayVec;
34use hal::CommandEncoder as _;
35use thiserror::Error;
36use wgt::{
37 BufferAddress, BufferSize, BufferUsages, Color, IndexFormat, TextureUsages,
38 TextureViewDimension, VertexStepMode,
39};
40
41#[cfg(any(feature = "serial-pass", feature = "replay"))]
42use serde::Deserialize;
43#[cfg(any(feature = "serial-pass", feature = "trace"))]
44use serde::Serialize;
45
46use std::{borrow::Cow, fmt, iter, marker::PhantomData, mem, num::NonZeroU32, ops::Range, str};
47
48use super::{memory_init::TextureSurfaceDiscard, CommandBufferTextureMemoryActions};
49
50#[repr(C)]
52#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq)]
53#[cfg_attr(any(feature = "serial-pass", feature = "trace"), derive(Serialize))]
54#[cfg_attr(any(feature = "serial-pass", feature = "replay"), derive(Deserialize))]
55#[cfg_attr(feature = "serde", serde(rename_all = "kebab-case"))]
56pub enum LoadOp {
57 Clear = 0,
59 Load = 1,
61}
62
63#[repr(C)]
65#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq)]
66#[cfg_attr(any(feature = "serial-pass", feature = "trace"), derive(Serialize))]
67#[cfg_attr(any(feature = "serial-pass", feature = "replay"), derive(Deserialize))]
68#[cfg_attr(feature = "serde", serde(rename_all = "kebab-case"))]
69pub enum StoreOp {
70 Discard = 0,
74 Store = 1,
76}
77
78#[repr(C)]
80#[derive(Clone, Debug, Eq, PartialEq)]
81#[cfg_attr(any(feature = "serial-pass", feature = "trace"), derive(Serialize))]
82#[cfg_attr(any(feature = "serial-pass", feature = "replay"), derive(Deserialize))]
83pub struct PassChannel<V> {
84 pub load_op: LoadOp,
90 pub store_op: StoreOp,
92 pub clear_value: V,
95 pub read_only: bool,
99}
100
101impl<V> PassChannel<V> {
102 fn hal_ops(&self) -> hal::AttachmentOps {
103 let mut ops = hal::AttachmentOps::empty();
104 match self.load_op {
105 LoadOp::Load => ops |= hal::AttachmentOps::LOAD,
106 LoadOp::Clear => (),
107 };
108 match self.store_op {
109 StoreOp::Store => ops |= hal::AttachmentOps::STORE,
110 StoreOp::Discard => (),
111 };
112 ops
113 }
114}
115
116#[repr(C)]
118#[derive(Clone, Debug, PartialEq)]
119#[cfg_attr(any(feature = "serial-pass", feature = "trace"), derive(Serialize))]
120#[cfg_attr(any(feature = "serial-pass", feature = "replay"), derive(Deserialize))]
121pub struct RenderPassColorAttachment {
122 pub view: id::TextureViewId,
124 pub resolve_target: Option<id::TextureViewId>,
126 pub channel: PassChannel<Color>,
128}
129
130#[repr(C)]
132#[derive(Clone, Debug, PartialEq)]
133#[cfg_attr(any(feature = "serial-pass", feature = "trace"), derive(Serialize))]
134#[cfg_attr(any(feature = "serial-pass", feature = "replay"), derive(Deserialize))]
135pub struct RenderPassDepthStencilAttachment {
136 pub view: id::TextureViewId,
138 pub depth: PassChannel<f32>,
140 pub stencil: PassChannel<u32>,
142}
143
144impl RenderPassDepthStencilAttachment {
145 fn depth_stencil_read_only(
154 &self,
155 aspects: hal::FormatAspects,
156 ) -> Result<(bool, bool), RenderPassErrorInner> {
157 let mut depth_read_only = true;
158 let mut stencil_read_only = true;
159
160 if aspects.contains(hal::FormatAspects::DEPTH) {
161 if self.depth.read_only
162 && (self.depth.load_op, self.depth.store_op) != (LoadOp::Load, StoreOp::Store)
163 {
164 return Err(RenderPassErrorInner::InvalidDepthOps);
165 }
166 depth_read_only = self.depth.read_only;
167 }
168
169 if aspects.contains(hal::FormatAspects::STENCIL) {
170 if self.stencil.read_only
171 && (self.stencil.load_op, self.stencil.store_op) != (LoadOp::Load, StoreOp::Store)
172 {
173 return Err(RenderPassErrorInner::InvalidStencilOps);
174 }
175 stencil_read_only = self.stencil.read_only;
176 }
177
178 Ok((depth_read_only, stencil_read_only))
179 }
180}
181
182#[derive(Clone, Debug, Default, PartialEq)]
184pub struct RenderPassDescriptor<'a> {
185 pub label: Label<'a>,
186 pub color_attachments: Cow<'a, [Option<RenderPassColorAttachment>]>,
188 pub depth_stencil_attachment: Option<&'a RenderPassDepthStencilAttachment>,
190}
191
192#[cfg_attr(feature = "serial-pass", derive(Deserialize, Serialize))]
193pub struct RenderPass {
194 base: BasePass<RenderCommand>,
195 parent_id: id::CommandEncoderId,
196 color_targets: ArrayVec<Option<RenderPassColorAttachment>, { hal::MAX_COLOR_ATTACHMENTS }>,
197 depth_stencil_target: Option<RenderPassDepthStencilAttachment>,
198
199 #[cfg_attr(feature = "serial-pass", serde(skip))]
201 current_bind_groups: BindGroupStateChange,
202 #[cfg_attr(feature = "serial-pass", serde(skip))]
203 current_pipeline: StateChange<id::RenderPipelineId>,
204}
205
206impl RenderPass {
207 pub fn new(parent_id: id::CommandEncoderId, desc: &RenderPassDescriptor) -> Self {
208 Self {
209 base: BasePass::new(&desc.label),
210 parent_id,
211 color_targets: desc.color_attachments.iter().cloned().collect(),
212 depth_stencil_target: desc.depth_stencil_attachment.cloned(),
213
214 current_bind_groups: BindGroupStateChange::new(),
215 current_pipeline: StateChange::new(),
216 }
217 }
218
219 pub fn parent_id(&self) -> id::CommandEncoderId {
220 self.parent_id
221 }
222
223 #[cfg(feature = "trace")]
224 pub fn into_command(self) -> crate::device::trace::Command {
225 crate::device::trace::Command::RunRenderPass {
226 base: self.base,
227 target_colors: self.color_targets.into_iter().collect(),
228 target_depth_stencil: self.depth_stencil_target,
229 }
230 }
231
232 pub fn set_index_buffer(
233 &mut self,
234 buffer_id: id::BufferId,
235 index_format: IndexFormat,
236 offset: BufferAddress,
237 size: Option<BufferSize>,
238 ) {
239 self.base.commands.push(RenderCommand::SetIndexBuffer {
240 buffer_id,
241 index_format,
242 offset,
243 size,
244 });
245 }
246}
247
248impl fmt::Debug for RenderPass {
249 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
250 f.debug_struct("RenderPass")
251 .field("encoder_id", &self.parent_id)
252 .field("color_targets", &self.color_targets)
253 .field("depth_stencil_target", &self.depth_stencil_target)
254 .field("command count", &self.base.commands.len())
255 .field("dynamic offset count", &self.base.dynamic_offsets.len())
256 .field(
257 "push constant u32 count",
258 &self.base.push_constant_data.len(),
259 )
260 .finish()
261 }
262}
263
264#[derive(Debug, PartialEq)]
265enum OptionalState {
266 Unused,
267 Required,
268 Set,
269}
270
271impl OptionalState {
272 fn require(&mut self, require: bool) {
273 if require && *self == Self::Unused {
274 *self = Self::Required;
275 }
276 }
277}
278
279#[derive(Debug, Default)]
280struct IndexState {
281 bound_buffer_view: Option<(id::Valid<id::BufferId>, Range<BufferAddress>)>,
282 format: Option<IndexFormat>,
283 pipeline_format: Option<IndexFormat>,
284 limit: u32,
285}
286
287impl IndexState {
288 fn update_limit(&mut self) {
289 self.limit = match self.bound_buffer_view {
290 Some((_, ref range)) => {
291 let format = self
292 .format
293 .expect("IndexState::update_limit must be called after a index buffer is set");
294 let shift = match format {
295 IndexFormat::Uint16 => 1,
296 IndexFormat::Uint32 => 2,
297 };
298 ((range.end - range.start) >> shift) as u32
299 }
300 None => 0,
301 }
302 }
303
304 fn reset(&mut self) {
305 self.bound_buffer_view = None;
306 self.limit = 0;
307 }
308}
309
310#[derive(Clone, Copy, Debug)]
311struct VertexBufferState {
312 total_size: BufferAddress,
313 step: pipeline::VertexStep,
314 bound: bool,
315}
316
317impl VertexBufferState {
318 const EMPTY: Self = Self {
319 total_size: 0,
320 step: pipeline::VertexStep {
321 stride: 0,
322 mode: VertexStepMode::Vertex,
323 },
324 bound: false,
325 };
326}
327
328#[derive(Debug, Default)]
329struct VertexState {
330 inputs: ArrayVec<VertexBufferState, { hal::MAX_VERTEX_BUFFERS }>,
331 vertex_limit: u32,
333 vertex_limit_slot: u32,
335 instance_limit: u32,
337 instance_limit_slot: u32,
339 buffers_required: u32,
341}
342
343impl VertexState {
344 fn update_limits(&mut self) {
345 self.vertex_limit = u32::MAX;
346 self.instance_limit = u32::MAX;
347 for (idx, vbs) in self.inputs.iter().enumerate() {
348 if vbs.step.stride == 0 || !vbs.bound {
349 continue;
350 }
351 let limit = (vbs.total_size / vbs.step.stride) as u32;
352 match vbs.step.mode {
353 VertexStepMode::Vertex => {
354 if limit < self.vertex_limit {
355 self.vertex_limit = limit;
356 self.vertex_limit_slot = idx as _;
357 }
358 }
359 VertexStepMode::Instance => {
360 if limit < self.instance_limit {
361 self.instance_limit = limit;
362 self.instance_limit_slot = idx as _;
363 }
364 }
365 }
366 }
367 }
368
369 fn reset(&mut self) {
370 self.inputs.clear();
371 self.vertex_limit = 0;
372 self.instance_limit = 0;
373 }
374}
375
376#[derive(Debug)]
377struct State {
378 pipeline_flags: PipelineFlags,
379 binder: Binder,
380 blend_constant: OptionalState,
381 stencil_reference: u32,
382 pipeline: Option<id::RenderPipelineId>,
383 index: IndexState,
384 vertex: VertexState,
385 debug_scope_depth: u32,
386}
387
388impl State {
389 fn is_ready(&self, indexed: bool) -> Result<(), DrawError> {
390 let vertex_buffer_count = self.vertex.inputs.iter().take_while(|v| v.bound).count() as u32;
392 if vertex_buffer_count < self.vertex.buffers_required {
394 return Err(DrawError::MissingVertexBuffer {
395 index: vertex_buffer_count,
396 });
397 }
398
399 let bind_mask = self.binder.invalid_mask();
400 if bind_mask != 0 {
401 return Err(DrawError::IncompatibleBindGroup {
403 index: bind_mask.trailing_zeros(),
404 });
405 }
406 if self.pipeline.is_none() {
407 return Err(DrawError::MissingPipeline);
408 }
409 if self.blend_constant == OptionalState::Required {
410 return Err(DrawError::MissingBlendConstant);
411 }
412
413 if indexed {
414 if let Some(pipeline_index_format) = self.index.pipeline_format {
416 let buffer_index_format = self.index.format.ok_or(DrawError::MissingIndexBuffer)?;
418
419 if pipeline_index_format != buffer_index_format {
421 return Err(DrawError::UnmatchedIndexFormats {
422 pipeline: pipeline_index_format,
423 buffer: buffer_index_format,
424 });
425 }
426 }
427 }
428
429 self.binder.check_late_buffer_bindings()?;
430
431 Ok(())
432 }
433
434 fn reset_bundle(&mut self) {
436 self.binder.reset();
437 self.pipeline = None;
438 self.index.reset();
439 self.vertex.reset();
440 }
441}
442
443#[derive(Debug, Copy, Clone)]
447pub enum AttachmentErrorLocation {
448 Color { index: usize, resolve: bool },
449 Depth,
450}
451
452impl fmt::Display for AttachmentErrorLocation {
453 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
454 match *self {
455 AttachmentErrorLocation::Color {
456 index,
457 resolve: false,
458 } => write!(f, "color attachment at index {index}'s texture view"),
459 AttachmentErrorLocation::Color {
460 index,
461 resolve: true,
462 } => write!(
463 f,
464 "color attachment at index {index}'s resolve texture view"
465 ),
466 AttachmentErrorLocation::Depth => write!(f, "depth attachment's texture view"),
467 }
468 }
469}
470
471#[derive(Clone, Debug, Error)]
472#[non_exhaustive]
473pub enum ColorAttachmentError {
474 #[error("Attachment format {0:?} is not a color format")]
475 InvalidFormat(wgt::TextureFormat),
476 #[error("The number of color attachments {given} exceeds the limit {limit}")]
477 TooMany { given: usize, limit: usize },
478}
479
480#[derive(Clone, Debug, Error)]
482pub enum RenderPassErrorInner {
483 #[error(transparent)]
484 ColorAttachment(#[from] ColorAttachmentError),
485 #[error(transparent)]
486 Encoder(#[from] CommandEncoderError),
487 #[error("Attachment texture view {0:?} is invalid")]
488 InvalidAttachment(id::TextureViewId),
489 #[error("The format of the depth-stencil attachment ({0:?}) is not a depth-stencil format")]
490 InvalidDepthStencilAttachmentFormat(wgt::TextureFormat),
491 #[error("The format of the {location} ({format:?}) is not resolvable")]
492 UnsupportedResolveTargetFormat {
493 location: AttachmentErrorLocation,
494 format: wgt::TextureFormat,
495 },
496 #[error("No color attachments or depth attachments were provided, at least one attachment of any kind must be provided")]
497 MissingAttachments,
498 #[error("The {location} is not renderable:")]
499 TextureViewIsNotRenderable {
500 location: AttachmentErrorLocation,
501 #[source]
502 reason: TextureViewNotRenderableReason,
503 },
504 #[error("Attachments have differing sizes: the {expected_location} has extent {expected_extent:?} but is followed by the {actual_location} which has {actual_extent:?}")]
505 AttachmentsDimensionMismatch {
506 expected_location: AttachmentErrorLocation,
507 expected_extent: wgt::Extent3d,
508 actual_location: AttachmentErrorLocation,
509 actual_extent: wgt::Extent3d,
510 },
511 #[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:?}")]
512 AttachmentSampleCountMismatch {
513 expected_location: AttachmentErrorLocation,
514 expected_samples: u32,
515 actual_location: AttachmentErrorLocation,
516 actual_samples: u32,
517 },
518 #[error("The resolve source, {location}, must be multi-sampled (has {src} samples) while the resolve destination must not be multisampled (has {dst} samples)")]
519 InvalidResolveSampleCounts {
520 location: AttachmentErrorLocation,
521 src: u32,
522 dst: u32,
523 },
524 #[error(
525 "Resource source, {location}, format ({src:?}) must match the resolve destination format ({dst:?})"
526 )]
527 MismatchedResolveTextureFormat {
528 location: AttachmentErrorLocation,
529 src: wgt::TextureFormat,
530 dst: wgt::TextureFormat,
531 },
532 #[error("Surface texture is dropped before the render pass is finished")]
533 SurfaceTextureDropped,
534 #[error("Not enough memory left")]
535 OutOfMemory,
536 #[error("Unable to clear non-present/read-only depth")]
537 InvalidDepthOps,
538 #[error("Unable to clear non-present/read-only stencil")]
539 InvalidStencilOps,
540 #[error("Setting `values_offset` to be `None` is only for internal use in render bundles")]
541 InvalidValuesOffset,
542 #[error(transparent)]
543 MissingFeatures(#[from] MissingFeatures),
544 #[error(transparent)]
545 MissingDownlevelFlags(#[from] MissingDownlevelFlags),
546 #[error("Indirect draw uses bytes {offset}..{end_offset} {} which overruns indirect buffer of size {buffer_size}",
547 count.map_or_else(String::new, |v| format!("(using count {v})")))]
548 IndirectBufferOverrun {
549 count: Option<NonZeroU32>,
550 offset: u64,
551 end_offset: u64,
552 buffer_size: u64,
553 },
554 #[error("Indirect draw uses bytes {begin_count_offset}..{end_count_offset} which overruns indirect buffer of size {count_buffer_size}")]
555 IndirectCountBufferOverrun {
556 begin_count_offset: u64,
557 end_count_offset: u64,
558 count_buffer_size: u64,
559 },
560 #[error("Cannot pop debug group, because number of pushed debug groups is zero")]
561 InvalidPopDebugGroup,
562 #[error(transparent)]
563 ResourceUsageConflict(#[from] UsageConflict),
564 #[error("Render bundle has incompatible targets, {0}")]
565 IncompatibleBundleTargets(#[from] RenderPassCompatibilityError),
566 #[error(
567 "Render bundle has incompatible read-only flags: \
568 bundle has flags depth = {bundle_depth} and stencil = {bundle_stencil}, \
569 while the pass has flags depth = {pass_depth} and stencil = {pass_stencil}. \
570 Read-only renderpasses are only compatible with read-only bundles for that aspect."
571 )]
572 IncompatibleBundleReadOnlyDepthStencil {
573 pass_depth: bool,
574 pass_stencil: bool,
575 bundle_depth: bool,
576 bundle_stencil: bool,
577 },
578 #[error(transparent)]
579 RenderCommand(#[from] RenderCommandError),
580 #[error(transparent)]
581 Draw(#[from] DrawError),
582 #[error(transparent)]
583 Bind(#[from] BindError),
584 #[error(transparent)]
585 QueryUse(#[from] QueryUseError),
586 #[error("Multiview layer count must match")]
587 MultiViewMismatch,
588 #[error(
589 "Multiview pass texture views with more than one array layer must have D2Array dimension"
590 )]
591 MultiViewDimensionMismatch,
592}
593
594impl PrettyError for RenderPassErrorInner {
595 fn fmt_pretty(&self, fmt: &mut ErrorFormatter) {
596 fmt.error(self);
597 if let Self::InvalidAttachment(id) = *self {
598 fmt.texture_view_label_with_key(&id, "attachment");
599 };
600 }
601}
602
603impl From<MissingBufferUsageError> for RenderPassErrorInner {
604 fn from(error: MissingBufferUsageError) -> Self {
605 Self::RenderCommand(error.into())
606 }
607}
608
609impl From<MissingTextureUsageError> for RenderPassErrorInner {
610 fn from(error: MissingTextureUsageError) -> Self {
611 Self::RenderCommand(error.into())
612 }
613}
614
615#[derive(Clone, Debug, Error)]
617#[error("{scope}")]
618pub struct RenderPassError {
619 pub scope: PassErrorScope,
620 #[source]
621 inner: RenderPassErrorInner,
622}
623impl PrettyError for RenderPassError {
624 fn fmt_pretty(&self, fmt: &mut ErrorFormatter) {
625 fmt.error(self);
628 self.scope.fmt_pretty(fmt);
629 }
630}
631
632impl<T, E> MapPassErr<T, RenderPassError> for Result<T, E>
633where
634 E: Into<RenderPassErrorInner>,
635{
636 fn map_pass_err(self, scope: PassErrorScope) -> Result<T, RenderPassError> {
637 self.map_err(|inner| RenderPassError {
638 scope,
639 inner: inner.into(),
640 })
641 }
642}
643
644struct RenderAttachment<'a> {
645 texture_id: &'a Stored<id::TextureId>,
646 selector: &'a TextureSelector,
647 usage: hal::TextureUses,
648}
649
650impl<A: hal::Api> TextureView<A> {
651 fn to_render_attachment(&self, usage: hal::TextureUses) -> RenderAttachment {
652 RenderAttachment {
653 texture_id: &self.parent_id,
654 selector: &self.selector,
655 usage,
656 }
657 }
658}
659
660const MAX_TOTAL_ATTACHMENTS: usize = hal::MAX_COLOR_ATTACHMENTS + hal::MAX_COLOR_ATTACHMENTS + 1;
661type AttachmentDataVec<T> = ArrayVec<T, MAX_TOTAL_ATTACHMENTS>;
662
663struct RenderPassInfo<'a, A: HalApi> {
664 context: RenderPassContext,
665 usage_scope: UsageScope<A>,
666 render_attachments: AttachmentDataVec<RenderAttachment<'a>>,
668 is_depth_read_only: bool,
669 is_stencil_read_only: bool,
670 extent: wgt::Extent3d,
671 _phantom: PhantomData<A>,
672
673 pending_discard_init_fixups: SurfacesInDiscardState,
674 divergent_discarded_depth_stencil_aspect: Option<(wgt::TextureAspect, &'a TextureView<A>)>,
675 multiview: Option<NonZeroU32>,
676}
677
678impl<'a, A: HalApi> RenderPassInfo<'a, A> {
679 fn add_pass_texture_init_actions<V>(
680 channel: &PassChannel<V>,
681 texture_memory_actions: &mut CommandBufferTextureMemoryActions,
682 view: &TextureView<A>,
683 texture_guard: &Storage<Texture<A>, id::TextureId>,
684 pending_discard_init_fixups: &mut SurfacesInDiscardState,
685 ) {
686 if channel.load_op == LoadOp::Load {
687 pending_discard_init_fixups.extend(texture_memory_actions.register_init_action(
688 &TextureInitTrackerAction {
689 id: view.parent_id.value.0,
690 range: TextureInitRange::from(view.selector.clone()),
691 kind: MemoryInitKind::NeedsInitializedMemory,
693 },
694 texture_guard,
695 ));
696 } else if channel.store_op == StoreOp::Store {
697 texture_memory_actions.register_implicit_init(
699 view.parent_id.value,
700 TextureInitRange::from(view.selector.clone()),
701 texture_guard,
702 );
703 }
704 if channel.store_op == StoreOp::Discard {
705 texture_memory_actions.discard(TextureSurfaceDiscard {
709 texture: view.parent_id.value.0,
710 mip_level: view.selector.mips.start,
711 layer: view.selector.layers.start,
712 });
713 }
714 }
715
716 fn start(
717 device: &Device<A>,
718 label: Option<&str>,
719 color_attachments: &[Option<RenderPassColorAttachment>],
720 depth_stencil_attachment: Option<&RenderPassDepthStencilAttachment>,
721 cmd_buf: &mut CommandBuffer<A>,
722 view_guard: &'a Storage<TextureView<A>, id::TextureViewId>,
723 buffer_guard: &'a Storage<Buffer<A>, id::BufferId>,
724 texture_guard: &'a Storage<Texture<A>, id::TextureId>,
725 ) -> Result<Self, RenderPassErrorInner> {
726 profiling::scope!("RenderPassInfo::start");
727
728 let mut is_depth_read_only = false;
732 let mut is_stencil_read_only = false;
733
734 let mut render_attachments = AttachmentDataVec::<RenderAttachment>::new();
735 let mut discarded_surfaces = AttachmentDataVec::new();
736 let mut pending_discard_init_fixups = SurfacesInDiscardState::new();
737 let mut divergent_discarded_depth_stencil_aspect = None;
738
739 let mut attachment_location = AttachmentErrorLocation::Color {
740 index: usize::MAX,
741 resolve: false,
742 };
743 let mut extent = None;
744 let mut sample_count = 0;
745
746 let mut detected_multiview: Option<Option<NonZeroU32>> = None;
747
748 let mut check_multiview = |view: &TextureView<A>| {
749 let layers = view.selector.layers.end - view.selector.layers.start;
751 let this_multiview = if layers >= 2 {
752 Some(unsafe { NonZeroU32::new_unchecked(layers) })
754 } else {
755 None
756 };
757
758 if this_multiview.is_some() && view.desc.dimension != TextureViewDimension::D2Array {
760 return Err(RenderPassErrorInner::MultiViewDimensionMismatch);
761 }
762
763 if let Some(multiview) = detected_multiview {
765 if multiview != this_multiview {
766 return Err(RenderPassErrorInner::MultiViewMismatch);
767 }
768 } else {
769 if this_multiview.is_some() {
771 device.require_features(wgt::Features::MULTIVIEW)?;
772 }
773
774 detected_multiview = Some(this_multiview);
775 }
776
777 Ok(())
778 };
779 let mut add_view = |view: &TextureView<A>, location| {
780 let render_extent = view.render_extent.map_err(|reason| {
781 RenderPassErrorInner::TextureViewIsNotRenderable { location, reason }
782 })?;
783 if let Some(ex) = extent {
784 if ex != render_extent {
785 return Err(RenderPassErrorInner::AttachmentsDimensionMismatch {
786 expected_location: attachment_location,
787 expected_extent: ex,
788 actual_location: location,
789 actual_extent: render_extent,
790 });
791 }
792 } else {
793 extent = Some(render_extent);
794 }
795 if sample_count == 0 {
796 sample_count = view.samples;
797 } else if sample_count != view.samples {
798 return Err(RenderPassErrorInner::AttachmentSampleCountMismatch {
799 expected_location: attachment_location,
800 expected_samples: sample_count,
801 actual_location: location,
802 actual_samples: view.samples,
803 });
804 }
805 attachment_location = location;
806 Ok(())
807 };
808
809 let mut colors =
810 ArrayVec::<Option<hal::ColorAttachment<A>>, { hal::MAX_COLOR_ATTACHMENTS }>::new();
811 let mut depth_stencil = None;
812
813 if let Some(at) = depth_stencil_attachment {
814 let view: &TextureView<A> = cmd_buf
815 .trackers
816 .views
817 .add_single(view_guard, at.view)
818 .ok_or(RenderPassErrorInner::InvalidAttachment(at.view))?;
819 check_multiview(view)?;
820 add_view(view, AttachmentErrorLocation::Depth)?;
821
822 let ds_aspects = view.desc.aspects();
823 if ds_aspects.contains(hal::FormatAspects::COLOR) {
824 return Err(RenderPassErrorInner::InvalidDepthStencilAttachmentFormat(
825 view.desc.format,
826 ));
827 }
828
829 if !ds_aspects.contains(hal::FormatAspects::STENCIL)
830 || (at.stencil.load_op == at.depth.load_op
831 && at.stencil.store_op == at.depth.store_op)
832 {
833 Self::add_pass_texture_init_actions(
834 &at.depth,
835 &mut cmd_buf.texture_memory_actions,
836 view,
837 texture_guard,
838 &mut pending_discard_init_fixups,
839 );
840 } else if !ds_aspects.contains(hal::FormatAspects::DEPTH) {
841 Self::add_pass_texture_init_actions(
842 &at.stencil,
843 &mut cmd_buf.texture_memory_actions,
844 view,
845 texture_guard,
846 &mut pending_discard_init_fixups,
847 );
848 } else {
849 let need_init_beforehand =
871 at.depth.load_op == LoadOp::Load || at.stencil.load_op == LoadOp::Load;
872 if need_init_beforehand {
873 pending_discard_init_fixups.extend(
874 cmd_buf.texture_memory_actions.register_init_action(
875 &TextureInitTrackerAction {
876 id: view.parent_id.value.0,
877 range: TextureInitRange::from(view.selector.clone()),
878 kind: MemoryInitKind::NeedsInitializedMemory,
879 },
880 texture_guard,
881 ),
882 );
883 }
884
885 if at.depth.store_op != at.stencil.store_op {
894 if !need_init_beforehand {
895 cmd_buf.texture_memory_actions.register_implicit_init(
896 view.parent_id.value,
897 TextureInitRange::from(view.selector.clone()),
898 texture_guard,
899 );
900 }
901 divergent_discarded_depth_stencil_aspect = Some((
902 if at.depth.store_op == StoreOp::Discard {
903 wgt::TextureAspect::DepthOnly
904 } else {
905 wgt::TextureAspect::StencilOnly
906 },
907 view,
908 ));
909 } else if at.depth.store_op == StoreOp::Discard {
910 discarded_surfaces.push(TextureSurfaceDiscard {
912 texture: view.parent_id.value.0,
913 mip_level: view.selector.mips.start,
914 layer: view.selector.layers.start,
915 });
916 }
917 }
918
919 (is_depth_read_only, is_stencil_read_only) = at.depth_stencil_read_only(ds_aspects)?;
920
921 let usage = if is_depth_read_only && is_stencil_read_only {
922 hal::TextureUses::DEPTH_STENCIL_READ | hal::TextureUses::RESOURCE
923 } else {
924 hal::TextureUses::DEPTH_STENCIL_WRITE
925 };
926 render_attachments.push(view.to_render_attachment(usage));
927
928 depth_stencil = Some(hal::DepthStencilAttachment {
929 target: hal::Attachment {
930 view: &view.raw,
931 usage,
932 },
933 depth_ops: at.depth.hal_ops(),
934 stencil_ops: at.stencil.hal_ops(),
935 clear_value: (at.depth.clear_value, at.stencil.clear_value),
936 });
937 }
938
939 for (index, attachment) in color_attachments.iter().enumerate() {
940 let at = if let Some(attachment) = attachment.as_ref() {
941 attachment
942 } else {
943 colors.push(None);
944 continue;
945 };
946 let color_view: &TextureView<A> = cmd_buf
947 .trackers
948 .views
949 .add_single(view_guard, at.view)
950 .ok_or(RenderPassErrorInner::InvalidAttachment(at.view))?;
951 check_multiview(color_view)?;
952 add_view(
953 color_view,
954 AttachmentErrorLocation::Color {
955 index,
956 resolve: false,
957 },
958 )?;
959
960 if !color_view
961 .desc
962 .aspects()
963 .contains(hal::FormatAspects::COLOR)
964 {
965 return Err(RenderPassErrorInner::ColorAttachment(
966 ColorAttachmentError::InvalidFormat(color_view.desc.format),
967 ));
968 }
969
970 Self::add_pass_texture_init_actions(
971 &at.channel,
972 &mut cmd_buf.texture_memory_actions,
973 color_view,
974 texture_guard,
975 &mut pending_discard_init_fixups,
976 );
977 render_attachments
978 .push(color_view.to_render_attachment(hal::TextureUses::COLOR_TARGET));
979
980 let mut hal_resolve_target = None;
981 if let Some(resolve_target) = at.resolve_target {
982 let resolve_view: &TextureView<A> = cmd_buf
983 .trackers
984 .views
985 .add_single(view_guard, resolve_target)
986 .ok_or(RenderPassErrorInner::InvalidAttachment(resolve_target))?;
987
988 check_multiview(resolve_view)?;
989
990 let resolve_location = AttachmentErrorLocation::Color {
991 index,
992 resolve: true,
993 };
994
995 let render_extent = resolve_view.render_extent.map_err(|reason| {
996 RenderPassErrorInner::TextureViewIsNotRenderable {
997 location: resolve_location,
998 reason,
999 }
1000 })?;
1001 if color_view.render_extent.unwrap() != render_extent {
1002 return Err(RenderPassErrorInner::AttachmentsDimensionMismatch {
1003 expected_location: attachment_location,
1004 expected_extent: extent.unwrap_or_default(),
1005 actual_location: resolve_location,
1006 actual_extent: render_extent,
1007 });
1008 }
1009 if color_view.samples == 1 || resolve_view.samples != 1 {
1010 return Err(RenderPassErrorInner::InvalidResolveSampleCounts {
1011 location: resolve_location,
1012 src: color_view.samples,
1013 dst: resolve_view.samples,
1014 });
1015 }
1016 if color_view.desc.format != resolve_view.desc.format {
1017 return Err(RenderPassErrorInner::MismatchedResolveTextureFormat {
1018 location: resolve_location,
1019 src: color_view.desc.format,
1020 dst: resolve_view.desc.format,
1021 });
1022 }
1023 if !resolve_view
1024 .format_features
1025 .flags
1026 .contains(wgt::TextureFormatFeatureFlags::MULTISAMPLE_RESOLVE)
1027 {
1028 return Err(RenderPassErrorInner::UnsupportedResolveTargetFormat {
1029 location: resolve_location,
1030 format: resolve_view.desc.format,
1031 });
1032 }
1033
1034 cmd_buf.texture_memory_actions.register_implicit_init(
1035 resolve_view.parent_id.value,
1036 TextureInitRange::from(resolve_view.selector.clone()),
1037 texture_guard,
1038 );
1039 render_attachments
1040 .push(resolve_view.to_render_attachment(hal::TextureUses::COLOR_TARGET));
1041
1042 hal_resolve_target = Some(hal::Attachment {
1043 view: &resolve_view.raw,
1044 usage: hal::TextureUses::COLOR_TARGET,
1045 });
1046 }
1047
1048 colors.push(Some(hal::ColorAttachment {
1049 target: hal::Attachment {
1050 view: &color_view.raw,
1051 usage: hal::TextureUses::COLOR_TARGET,
1052 },
1053 resolve_target: hal_resolve_target,
1054 ops: at.channel.hal_ops(),
1055 clear_value: at.channel.clear_value,
1056 }));
1057 }
1058
1059 let extent = extent.ok_or(RenderPassErrorInner::MissingAttachments)?;
1060 let multiview = detected_multiview.expect("Multiview was not detected, no attachments");
1061
1062 let view_data = AttachmentData {
1063 colors: color_attachments
1064 .iter()
1065 .map(|at| at.as_ref().map(|at| view_guard.get(at.view).unwrap()))
1066 .collect(),
1067 resolves: color_attachments
1068 .iter()
1069 .filter_map(|at| match *at {
1070 Some(RenderPassColorAttachment {
1071 resolve_target: Some(resolve),
1072 ..
1073 }) => Some(view_guard.get(resolve).unwrap()),
1074 _ => None,
1075 })
1076 .collect(),
1077 depth_stencil: depth_stencil_attachment.map(|at| view_guard.get(at.view).unwrap()),
1078 };
1079
1080 let context = RenderPassContext {
1081 attachments: view_data.map(|view| view.desc.format),
1082 sample_count,
1083 multiview,
1084 };
1085
1086 let hal_desc = hal::RenderPassDescriptor {
1087 label,
1088 extent,
1089 sample_count,
1090 color_attachments: &colors,
1091 depth_stencil_attachment: depth_stencil,
1092 multiview,
1093 };
1094 unsafe {
1095 cmd_buf.encoder.raw.begin_render_pass(&hal_desc);
1096 };
1097
1098 Ok(Self {
1099 context,
1100 usage_scope: UsageScope::new(buffer_guard, texture_guard),
1101 render_attachments,
1102 is_depth_read_only,
1103 is_stencil_read_only,
1104 extent,
1105 _phantom: PhantomData,
1106 pending_discard_init_fixups,
1107 divergent_discarded_depth_stencil_aspect,
1108 multiview,
1109 })
1110 }
1111
1112 fn finish(
1113 mut self,
1114 raw: &mut A::CommandEncoder,
1115 texture_guard: &Storage<Texture<A>, id::TextureId>,
1116 ) -> Result<(UsageScope<A>, SurfacesInDiscardState), RenderPassErrorInner> {
1117 profiling::scope!("RenderPassInfo::finish");
1118 unsafe {
1119 raw.end_render_pass();
1120 }
1121
1122 for ra in self.render_attachments {
1123 if !texture_guard.contains(ra.texture_id.value.0) {
1124 return Err(RenderPassErrorInner::SurfaceTextureDropped);
1125 }
1126 let texture = &texture_guard[ra.texture_id.value];
1127 check_texture_usage(texture.desc.usage, TextureUsages::RENDER_ATTACHMENT)?;
1128
1129 unsafe {
1131 self.usage_scope
1132 .textures
1133 .merge_single(
1134 texture_guard,
1135 ra.texture_id.value,
1136 Some(ra.selector.clone()),
1137 &ra.texture_id.ref_count,
1138 ra.usage,
1139 )
1140 .map_err(UsageConflict::from)?
1141 };
1142 }
1143
1144 if let Some((aspect, view)) = self.divergent_discarded_depth_stencil_aspect {
1154 let (depth_ops, stencil_ops) = if aspect == wgt::TextureAspect::DepthOnly {
1155 (
1156 hal::AttachmentOps::STORE, hal::AttachmentOps::LOAD | hal::AttachmentOps::STORE, )
1159 } else {
1160 (
1161 hal::AttachmentOps::LOAD | hal::AttachmentOps::STORE, hal::AttachmentOps::STORE, )
1164 };
1165 let desc = hal::RenderPassDescriptor {
1166 label: Some("(wgpu internal) Zero init discarded depth/stencil aspect"),
1167 extent: view.render_extent.unwrap(),
1168 sample_count: view.samples,
1169 color_attachments: &[],
1170 depth_stencil_attachment: Some(hal::DepthStencilAttachment {
1171 target: hal::Attachment {
1172 view: &view.raw,
1173 usage: hal::TextureUses::DEPTH_STENCIL_WRITE,
1174 },
1175 depth_ops,
1176 stencil_ops,
1177 clear_value: (0.0, 0),
1178 }),
1179 multiview: self.multiview,
1180 };
1181 unsafe {
1182 raw.begin_render_pass(&desc);
1183 raw.end_render_pass();
1184 }
1185 }
1186
1187 Ok((self.usage_scope, self.pending_discard_init_fixups))
1188 }
1189}
1190
1191impl<G: GlobalIdentityHandlerFactory> Global<G> {
1194 pub fn command_encoder_run_render_pass<A: HalApi>(
1195 &self,
1196 encoder_id: id::CommandEncoderId,
1197 pass: &RenderPass,
1198 ) -> Result<(), RenderPassError> {
1199 self.command_encoder_run_render_pass_impl::<A>(
1200 encoder_id,
1201 pass.base.as_ref(),
1202 &pass.color_targets,
1203 pass.depth_stencil_target.as_ref(),
1204 )
1205 }
1206
1207 #[doc(hidden)]
1208 pub fn command_encoder_run_render_pass_impl<A: HalApi>(
1209 &self,
1210 encoder_id: id::CommandEncoderId,
1211 base: BasePassRef<RenderCommand>,
1212 color_attachments: &[Option<RenderPassColorAttachment>],
1213 depth_stencil_attachment: Option<&RenderPassDepthStencilAttachment>,
1214 ) -> Result<(), RenderPassError> {
1215 profiling::scope!("CommandEncoder::run_render_pass");
1216 let init_scope = PassErrorScope::Pass(encoder_id);
1217
1218 let hub = A::hub(self);
1219 let mut token = Token::root();
1220 let (device_guard, mut token) = hub.devices.read(&mut token);
1221
1222 let (scope, query_reset_state, pending_discard_init_fixups) = {
1223 let (mut cmb_guard, mut token) = hub.command_buffers.write(&mut token);
1224
1225 let cmd_buf: &mut CommandBuffer<A> =
1228 CommandBuffer::get_encoder_mut(&mut *cmb_guard, encoder_id)
1229 .map_pass_err(init_scope)?;
1230
1231 cmd_buf.encoder.close();
1235 cmd_buf.status = CommandEncoderStatus::Error;
1237
1238 #[cfg(feature = "trace")]
1239 if let Some(ref mut list) = cmd_buf.commands {
1240 list.push(crate::device::trace::Command::RunRenderPass {
1241 base: BasePass::from_ref(base),
1242 target_colors: color_attachments.to_vec(),
1243 target_depth_stencil: depth_stencil_attachment.cloned(),
1244 });
1245 }
1246
1247 let device = &device_guard[cmd_buf.device_id.value];
1248 cmd_buf.encoder.open_pass(base.label);
1249
1250 let (bundle_guard, mut token) = hub.render_bundles.read(&mut token);
1251 let (pipeline_layout_guard, mut token) = hub.pipeline_layouts.read(&mut token);
1252 let (bind_group_guard, mut token) = hub.bind_groups.read(&mut token);
1253 let (render_pipeline_guard, mut token) = hub.render_pipelines.read(&mut token);
1254 let (query_set_guard, mut token) = hub.query_sets.read(&mut token);
1255 let (buffer_guard, mut token) = hub.buffers.read(&mut token);
1256 let (texture_guard, mut token) = hub.textures.read(&mut token);
1257 let (view_guard, _) = hub.texture_views.read(&mut token);
1258
1259 log::trace!(
1260 "Encoding render pass begin in command buffer {:?}",
1261 encoder_id
1262 );
1263
1264 let mut info = RenderPassInfo::start(
1265 device,
1266 base.label,
1267 color_attachments,
1268 depth_stencil_attachment,
1269 cmd_buf,
1270 &*view_guard,
1271 &*buffer_guard,
1272 &*texture_guard,
1273 )
1274 .map_pass_err(init_scope)?;
1275
1276 cmd_buf.trackers.set_size(
1277 Some(&*buffer_guard),
1278 Some(&*texture_guard),
1279 Some(&*view_guard),
1280 None,
1281 Some(&*bind_group_guard),
1282 None,
1283 Some(&*render_pipeline_guard),
1284 Some(&*bundle_guard),
1285 Some(&*query_set_guard),
1286 );
1287
1288 let raw = &mut cmd_buf.encoder.raw;
1289
1290 let mut state = State {
1291 pipeline_flags: PipelineFlags::empty(),
1292 binder: Binder::new(),
1293 blend_constant: OptionalState::Unused,
1294 stencil_reference: 0,
1295 pipeline: None,
1296 index: IndexState::default(),
1297 vertex: VertexState::default(),
1298 debug_scope_depth: 0,
1299 };
1300 let mut temp_offsets = Vec::new();
1301 let mut dynamic_offset_count = 0;
1302 let mut string_offset = 0;
1303 let mut active_query = None;
1304 let mut query_reset_state = QueryResetMap::new();
1305
1306 for command in base.commands {
1307 match *command {
1308 RenderCommand::SetBindGroup {
1309 index,
1310 num_dynamic_offsets,
1311 bind_group_id,
1312 } => {
1313 let scope = PassErrorScope::SetBindGroup(bind_group_id);
1314 let max_bind_groups = device.limits.max_bind_groups;
1315 if index >= max_bind_groups {
1316 return Err(RenderCommandError::BindGroupIndexOutOfRange {
1317 index,
1318 max: max_bind_groups,
1319 })
1320 .map_pass_err(scope);
1321 }
1322
1323 temp_offsets.clear();
1324 temp_offsets.extend_from_slice(
1325 &base.dynamic_offsets[dynamic_offset_count
1326 ..dynamic_offset_count + (num_dynamic_offsets as usize)],
1327 );
1328 dynamic_offset_count += num_dynamic_offsets as usize;
1329
1330 let bind_group: &crate::binding_model::BindGroup<A> = cmd_buf
1331 .trackers
1332 .bind_groups
1333 .add_single(&*bind_group_guard, bind_group_id)
1334 .ok_or(RenderCommandError::InvalidBindGroup(bind_group_id))
1335 .map_pass_err(scope)?;
1336 bind_group
1337 .validate_dynamic_bindings(index, &temp_offsets, &cmd_buf.limits)
1338 .map_pass_err(scope)?;
1339
1340 unsafe {
1342 info.usage_scope
1343 .merge_bind_group(&*texture_guard, &bind_group.used)
1344 .map_pass_err(scope)?;
1345 }
1346 cmd_buf.buffer_memory_init_actions.extend(
1350 bind_group.used_buffer_ranges.iter().filter_map(|action| {
1351 match buffer_guard.get(action.id) {
1352 Ok(buffer) => buffer.initialization_status.check_action(action),
1353 Err(_) => None,
1354 }
1355 }),
1356 );
1357 for action in bind_group.used_texture_ranges.iter() {
1358 info.pending_discard_init_fixups.extend(
1359 cmd_buf
1360 .texture_memory_actions
1361 .register_init_action(action, &texture_guard),
1362 );
1363 }
1364
1365 let pipeline_layout_id = state.binder.pipeline_layout_id;
1366 let entries = state.binder.assign_group(
1367 index as usize,
1368 id::Valid(bind_group_id),
1369 bind_group,
1370 &temp_offsets,
1371 );
1372 if !entries.is_empty() {
1373 let pipeline_layout =
1374 &pipeline_layout_guard[pipeline_layout_id.unwrap()].raw;
1375 for (i, e) in entries.iter().enumerate() {
1376 let raw_bg =
1377 &bind_group_guard[e.group_id.as_ref().unwrap().value].raw;
1378
1379 unsafe {
1380 raw.set_bind_group(
1381 pipeline_layout,
1382 index + i as u32,
1383 raw_bg,
1384 &e.dynamic_offsets,
1385 );
1386 }
1387 }
1388 }
1389 }
1390 RenderCommand::SetPipeline(pipeline_id) => {
1391 let scope = PassErrorScope::SetPipelineRender(pipeline_id);
1392 state.pipeline = Some(pipeline_id);
1393
1394 let pipeline: &pipeline::RenderPipeline<A> = cmd_buf
1395 .trackers
1396 .render_pipelines
1397 .add_single(&*render_pipeline_guard, pipeline_id)
1398 .ok_or(RenderCommandError::InvalidPipeline(pipeline_id))
1399 .map_pass_err(scope)?;
1400
1401 info.context
1402 .check_compatible(
1403 &pipeline.pass_context,
1404 RenderPassCompatibilityCheckType::RenderPipeline,
1405 )
1406 .map_err(RenderCommandError::IncompatiblePipelineTargets)
1407 .map_pass_err(scope)?;
1408
1409 state.pipeline_flags = pipeline.flags;
1410
1411 if (pipeline.flags.contains(PipelineFlags::WRITES_DEPTH)
1412 && info.is_depth_read_only)
1413 || (pipeline.flags.contains(PipelineFlags::WRITES_STENCIL)
1414 && info.is_stencil_read_only)
1415 {
1416 return Err(RenderCommandError::IncompatiblePipelineRods)
1417 .map_pass_err(scope);
1418 }
1419
1420 state
1421 .blend_constant
1422 .require(pipeline.flags.contains(PipelineFlags::BLEND_CONSTANT));
1423
1424 unsafe {
1425 raw.set_render_pipeline(&pipeline.raw);
1426 }
1427
1428 if pipeline.flags.contains(PipelineFlags::STENCIL_REFERENCE) {
1429 unsafe {
1430 raw.set_stencil_reference(state.stencil_reference);
1431 }
1432 }
1433
1434 if state.binder.pipeline_layout_id != Some(pipeline.layout_id.value) {
1436 let pipeline_layout = &pipeline_layout_guard[pipeline.layout_id.value];
1437
1438 let (start_index, entries) = state.binder.change_pipeline_layout(
1439 &*pipeline_layout_guard,
1440 pipeline.layout_id.value,
1441 &pipeline.late_sized_buffer_groups,
1442 );
1443 if !entries.is_empty() {
1444 for (i, e) in entries.iter().enumerate() {
1445 let raw_bg =
1446 &bind_group_guard[e.group_id.as_ref().unwrap().value].raw;
1447
1448 unsafe {
1449 raw.set_bind_group(
1450 &pipeline_layout.raw,
1451 start_index as u32 + i as u32,
1452 raw_bg,
1453 &e.dynamic_offsets,
1454 );
1455 }
1456 }
1457 }
1458
1459 let non_overlapping = super::bind::compute_nonoverlapping_ranges(
1461 &pipeline_layout.push_constant_ranges,
1462 );
1463 for range in non_overlapping {
1464 let offset = range.range.start;
1465 let size_bytes = range.range.end - offset;
1466 super::push_constant_clear(
1467 offset,
1468 size_bytes,
1469 |clear_offset, clear_data| unsafe {
1470 raw.set_push_constants(
1471 &pipeline_layout.raw,
1472 range.stages,
1473 clear_offset,
1474 clear_data,
1475 );
1476 },
1477 );
1478 }
1479 }
1480
1481 state.index.pipeline_format = pipeline.strip_index_format;
1482
1483 let vertex_steps_len = pipeline.vertex_steps.len();
1484 state.vertex.buffers_required = vertex_steps_len as u32;
1485
1486 while state.vertex.inputs.len() < vertex_steps_len {
1492 state.vertex.inputs.push(VertexBufferState::EMPTY);
1493 }
1494
1495 let mut steps = pipeline.vertex_steps.iter();
1497 for input in state.vertex.inputs.iter_mut() {
1498 input.step = steps.next().cloned().unwrap_or_default();
1499 }
1500
1501 state.vertex.update_limits();
1503 }
1504 RenderCommand::SetIndexBuffer {
1505 buffer_id,
1506 index_format,
1507 offset,
1508 size,
1509 } => {
1510 let scope = PassErrorScope::SetIndexBuffer(buffer_id);
1511 let buffer: &Buffer<A> = info
1512 .usage_scope
1513 .buffers
1514 .merge_single(&*buffer_guard, buffer_id, hal::BufferUses::INDEX)
1515 .map_pass_err(scope)?;
1516 check_buffer_usage(buffer.usage, BufferUsages::INDEX)
1517 .map_pass_err(scope)?;
1518 let buf_raw = buffer
1519 .raw
1520 .as_ref()
1521 .ok_or(RenderCommandError::DestroyedBuffer(buffer_id))
1522 .map_pass_err(scope)?;
1523
1524 let end = match size {
1525 Some(s) => offset + s.get(),
1526 None => buffer.size,
1527 };
1528 state.index.bound_buffer_view = Some((id::Valid(buffer_id), offset..end));
1529
1530 state.index.format = Some(index_format);
1531 state.index.update_limit();
1532
1533 cmd_buf.buffer_memory_init_actions.extend(
1534 buffer.initialization_status.create_action(
1535 buffer_id,
1536 offset..end,
1537 MemoryInitKind::NeedsInitializedMemory,
1538 ),
1539 );
1540
1541 let bb = hal::BufferBinding {
1542 buffer: buf_raw,
1543 offset,
1544 size,
1545 };
1546 unsafe {
1547 raw.set_index_buffer(bb, index_format);
1548 }
1549 }
1550 RenderCommand::SetVertexBuffer {
1551 slot,
1552 buffer_id,
1553 offset,
1554 size,
1555 } => {
1556 let scope = PassErrorScope::SetVertexBuffer(buffer_id);
1557 let buffer: &Buffer<A> = info
1558 .usage_scope
1559 .buffers
1560 .merge_single(&*buffer_guard, buffer_id, hal::BufferUses::VERTEX)
1561 .map_pass_err(scope)?;
1562 check_buffer_usage(buffer.usage, BufferUsages::VERTEX)
1563 .map_pass_err(scope)?;
1564 let buf_raw = buffer
1565 .raw
1566 .as_ref()
1567 .ok_or(RenderCommandError::DestroyedBuffer(buffer_id))
1568 .map_pass_err(scope)?;
1569
1570 let empty_slots =
1571 (1 + slot as usize).saturating_sub(state.vertex.inputs.len());
1572 state
1573 .vertex
1574 .inputs
1575 .extend(iter::repeat(VertexBufferState::EMPTY).take(empty_slots));
1576 let vertex_state = &mut state.vertex.inputs[slot as usize];
1577 vertex_state.total_size = match size {
1579 Some(s) => s.get(),
1580 None => buffer.size - offset,
1581 };
1582 vertex_state.bound = true;
1583
1584 cmd_buf.buffer_memory_init_actions.extend(
1585 buffer.initialization_status.create_action(
1586 buffer_id,
1587 offset..(offset + vertex_state.total_size),
1588 MemoryInitKind::NeedsInitializedMemory,
1589 ),
1590 );
1591
1592 let bb = hal::BufferBinding {
1593 buffer: buf_raw,
1594 offset,
1595 size,
1596 };
1597 unsafe {
1598 raw.set_vertex_buffer(slot, bb);
1599 }
1600 state.vertex.update_limits();
1601 }
1602 RenderCommand::SetBlendConstant(ref color) => {
1603 state.blend_constant = OptionalState::Set;
1604 let array = [
1605 color.r as f32,
1606 color.g as f32,
1607 color.b as f32,
1608 color.a as f32,
1609 ];
1610 unsafe {
1611 raw.set_blend_constants(&array);
1612 }
1613 }
1614 RenderCommand::SetStencilReference(value) => {
1615 state.stencil_reference = value;
1616 if state
1617 .pipeline_flags
1618 .contains(PipelineFlags::STENCIL_REFERENCE)
1619 {
1620 unsafe {
1621 raw.set_stencil_reference(value);
1622 }
1623 }
1624 }
1625 RenderCommand::SetViewport {
1626 ref rect,
1627 depth_min,
1628 depth_max,
1629 } => {
1630 let scope = PassErrorScope::SetViewport;
1631 if rect.w <= 0.0 || rect.h <= 0.0 {
1632 return Err(RenderCommandError::InvalidViewportDimension(
1633 rect.w, rect.h,
1634 ))
1635 .map_pass_err(scope);
1636 }
1637 if !(0.0..=1.0).contains(&depth_min) || !(0.0..=1.0).contains(&depth_max) {
1638 return Err(RenderCommandError::InvalidViewportDepth(
1639 depth_min, depth_max,
1640 ))
1641 .map_pass_err(scope);
1642 }
1643 let r = hal::Rect {
1644 x: rect.x,
1645 y: rect.y,
1646 w: rect.w,
1647 h: rect.h,
1648 };
1649 unsafe {
1650 raw.set_viewport(&r, depth_min..depth_max);
1651 }
1652 }
1653 RenderCommand::SetPushConstant {
1654 stages,
1655 offset,
1656 size_bytes,
1657 values_offset,
1658 } => {
1659 let scope = PassErrorScope::SetPushConstant;
1660 let values_offset = values_offset
1661 .ok_or(RenderPassErrorInner::InvalidValuesOffset)
1662 .map_pass_err(scope)?;
1663
1664 let end_offset_bytes = offset + size_bytes;
1665 let values_end_offset =
1666 (values_offset + size_bytes / wgt::PUSH_CONSTANT_ALIGNMENT) as usize;
1667 let data_slice =
1668 &base.push_constant_data[(values_offset as usize)..values_end_offset];
1669
1670 let pipeline_layout_id = state
1671 .binder
1672 .pipeline_layout_id
1673 .ok_or(DrawError::MissingPipeline)
1674 .map_pass_err(scope)?;
1675 let pipeline_layout = &pipeline_layout_guard[pipeline_layout_id];
1676
1677 pipeline_layout
1678 .validate_push_constant_ranges(stages, offset, end_offset_bytes)
1679 .map_err(RenderCommandError::from)
1680 .map_pass_err(scope)?;
1681
1682 unsafe {
1683 raw.set_push_constants(&pipeline_layout.raw, stages, offset, data_slice)
1684 }
1685 }
1686 RenderCommand::SetScissor(ref rect) => {
1687 let scope = PassErrorScope::SetScissorRect;
1688 if rect.x + rect.w > info.extent.width
1689 || rect.y + rect.h > info.extent.height
1690 {
1691 return Err(RenderCommandError::InvalidScissorRect(*rect, info.extent))
1692 .map_pass_err(scope);
1693 }
1694 let r = hal::Rect {
1695 x: rect.x,
1696 y: rect.y,
1697 w: rect.w,
1698 h: rect.h,
1699 };
1700 unsafe {
1701 raw.set_scissor_rect(&r);
1702 }
1703 }
1704 RenderCommand::Draw {
1705 vertex_count,
1706 instance_count,
1707 first_vertex,
1708 first_instance,
1709 } => {
1710 let indexed = false;
1711 let scope = PassErrorScope::Draw {
1712 indexed,
1713 indirect: false,
1714 pipeline: state.pipeline,
1715 };
1716 state.is_ready(indexed).map_pass_err(scope)?;
1717
1718 let last_vertex = first_vertex + vertex_count;
1719 let vertex_limit = state.vertex.vertex_limit;
1720 if last_vertex > vertex_limit {
1721 return Err(DrawError::VertexBeyondLimit {
1722 last_vertex,
1723 vertex_limit,
1724 slot: state.vertex.vertex_limit_slot,
1725 })
1726 .map_pass_err(scope);
1727 }
1728 let last_instance = first_instance + instance_count;
1729 let instance_limit = state.vertex.instance_limit;
1730 if last_instance > instance_limit {
1731 return Err(DrawError::InstanceBeyondLimit {
1732 last_instance,
1733 instance_limit,
1734 slot: state.vertex.instance_limit_slot,
1735 })
1736 .map_pass_err(scope);
1737 }
1738
1739 unsafe {
1740 raw.draw(first_vertex, vertex_count, first_instance, instance_count);
1741 }
1742 }
1743 RenderCommand::DrawIndexed {
1744 index_count,
1745 instance_count,
1746 first_index,
1747 base_vertex,
1748 first_instance,
1749 } => {
1750 let indexed = true;
1751 let scope = PassErrorScope::Draw {
1752 indexed,
1753 indirect: false,
1754 pipeline: state.pipeline,
1755 };
1756 state.is_ready(indexed).map_pass_err(scope)?;
1757
1758 let last_index = first_index + index_count;
1761 let index_limit = state.index.limit;
1762 if last_index > index_limit {
1763 return Err(DrawError::IndexBeyondLimit {
1764 last_index,
1765 index_limit,
1766 })
1767 .map_pass_err(scope);
1768 }
1769 let last_instance = first_instance + instance_count;
1770 let instance_limit = state.vertex.instance_limit;
1771 if last_instance > instance_limit {
1772 return Err(DrawError::InstanceBeyondLimit {
1773 last_instance,
1774 instance_limit,
1775 slot: state.vertex.instance_limit_slot,
1776 })
1777 .map_pass_err(scope);
1778 }
1779
1780 unsafe {
1781 raw.draw_indexed(
1782 first_index,
1783 index_count,
1784 base_vertex,
1785 first_instance,
1786 instance_count,
1787 );
1788 }
1789 }
1790 RenderCommand::MultiDrawIndirect {
1791 buffer_id,
1792 offset,
1793 count,
1794 indexed,
1795 } => {
1796 let scope = PassErrorScope::Draw {
1797 indexed,
1798 indirect: true,
1799 pipeline: state.pipeline,
1800 };
1801 state.is_ready(indexed).map_pass_err(scope)?;
1802
1803 let stride = match indexed {
1804 false => mem::size_of::<wgt::DrawIndirectArgs>(),
1805 true => mem::size_of::<wgt::DrawIndexedIndirectArgs>(),
1806 };
1807
1808 if count.is_some() {
1809 device
1810 .require_features(wgt::Features::MULTI_DRAW_INDIRECT)
1811 .map_pass_err(scope)?;
1812 }
1813 device
1814 .require_downlevel_flags(wgt::DownlevelFlags::INDIRECT_EXECUTION)
1815 .map_pass_err(scope)?;
1816
1817 let indirect_buffer: &Buffer<A> = info
1818 .usage_scope
1819 .buffers
1820 .merge_single(&*buffer_guard, buffer_id, hal::BufferUses::INDIRECT)
1821 .map_pass_err(scope)?;
1822 check_buffer_usage(indirect_buffer.usage, BufferUsages::INDIRECT)
1823 .map_pass_err(scope)?;
1824 let indirect_raw = indirect_buffer
1825 .raw
1826 .as_ref()
1827 .ok_or(RenderCommandError::DestroyedBuffer(buffer_id))
1828 .map_pass_err(scope)?;
1829
1830 let actual_count = count.map_or(1, |c| c.get());
1831
1832 let end_offset = offset + stride as u64 * actual_count as u64;
1833 if end_offset > indirect_buffer.size {
1834 return Err(RenderPassErrorInner::IndirectBufferOverrun {
1835 count,
1836 offset,
1837 end_offset,
1838 buffer_size: indirect_buffer.size,
1839 })
1840 .map_pass_err(scope);
1841 }
1842
1843 cmd_buf.buffer_memory_init_actions.extend(
1844 indirect_buffer.initialization_status.create_action(
1845 buffer_id,
1846 offset..end_offset,
1847 MemoryInitKind::NeedsInitializedMemory,
1848 ),
1849 );
1850
1851 match indexed {
1852 false => unsafe {
1853 raw.draw_indirect(indirect_raw, offset, actual_count);
1854 },
1855 true => unsafe {
1856 raw.draw_indexed_indirect(indirect_raw, offset, actual_count);
1857 },
1858 }
1859 }
1860 RenderCommand::MultiDrawIndirectCount {
1861 buffer_id,
1862 offset,
1863 count_buffer_id,
1864 count_buffer_offset,
1865 max_count,
1866 indexed,
1867 } => {
1868 let scope = PassErrorScope::Draw {
1869 indexed,
1870 indirect: true,
1871 pipeline: state.pipeline,
1872 };
1873 state.is_ready(indexed).map_pass_err(scope)?;
1874
1875 let stride = match indexed {
1876 false => mem::size_of::<wgt::DrawIndirectArgs>(),
1877 true => mem::size_of::<wgt::DrawIndexedIndirectArgs>(),
1878 } as u64;
1879
1880 device
1881 .require_features(wgt::Features::MULTI_DRAW_INDIRECT_COUNT)
1882 .map_pass_err(scope)?;
1883 device
1884 .require_downlevel_flags(wgt::DownlevelFlags::INDIRECT_EXECUTION)
1885 .map_pass_err(scope)?;
1886
1887 let indirect_buffer: &Buffer<A> = info
1888 .usage_scope
1889 .buffers
1890 .merge_single(&*buffer_guard, buffer_id, hal::BufferUses::INDIRECT)
1891 .map_pass_err(scope)?;
1892 check_buffer_usage(indirect_buffer.usage, BufferUsages::INDIRECT)
1893 .map_pass_err(scope)?;
1894 let indirect_raw = indirect_buffer
1895 .raw
1896 .as_ref()
1897 .ok_or(RenderCommandError::DestroyedBuffer(buffer_id))
1898 .map_pass_err(scope)?;
1899
1900 let count_buffer: &Buffer<A> = info
1901 .usage_scope
1902 .buffers
1903 .merge_single(
1904 &*buffer_guard,
1905 count_buffer_id,
1906 hal::BufferUses::INDIRECT,
1907 )
1908 .map_pass_err(scope)?;
1909 check_buffer_usage(count_buffer.usage, BufferUsages::INDIRECT)
1910 .map_pass_err(scope)?;
1911 let count_raw = count_buffer
1912 .raw
1913 .as_ref()
1914 .ok_or(RenderCommandError::DestroyedBuffer(count_buffer_id))
1915 .map_pass_err(scope)?;
1916
1917 let end_offset = offset + stride * max_count as u64;
1918 if end_offset > indirect_buffer.size {
1919 return Err(RenderPassErrorInner::IndirectBufferOverrun {
1920 count: None,
1921 offset,
1922 end_offset,
1923 buffer_size: indirect_buffer.size,
1924 })
1925 .map_pass_err(scope);
1926 }
1927 cmd_buf.buffer_memory_init_actions.extend(
1928 indirect_buffer.initialization_status.create_action(
1929 buffer_id,
1930 offset..end_offset,
1931 MemoryInitKind::NeedsInitializedMemory,
1932 ),
1933 );
1934
1935 let begin_count_offset = count_buffer_offset;
1936 let end_count_offset = count_buffer_offset + 4;
1937 if end_count_offset > count_buffer.size {
1938 return Err(RenderPassErrorInner::IndirectCountBufferOverrun {
1939 begin_count_offset,
1940 end_count_offset,
1941 count_buffer_size: count_buffer.size,
1942 })
1943 .map_pass_err(scope);
1944 }
1945 cmd_buf.buffer_memory_init_actions.extend(
1946 count_buffer.initialization_status.create_action(
1947 count_buffer_id,
1948 count_buffer_offset..end_count_offset,
1949 MemoryInitKind::NeedsInitializedMemory,
1950 ),
1951 );
1952
1953 match indexed {
1954 false => unsafe {
1955 raw.draw_indirect_count(
1956 indirect_raw,
1957 offset,
1958 count_raw,
1959 count_buffer_offset,
1960 max_count,
1961 );
1962 },
1963 true => unsafe {
1964 raw.draw_indexed_indirect_count(
1965 indirect_raw,
1966 offset,
1967 count_raw,
1968 count_buffer_offset,
1969 max_count,
1970 );
1971 },
1972 }
1973 }
1974 RenderCommand::PushDebugGroup { color: _, len } => {
1975 state.debug_scope_depth += 1;
1976 let label =
1977 str::from_utf8(&base.string_data[string_offset..string_offset + len])
1978 .unwrap();
1979 string_offset += len;
1980 unsafe {
1981 raw.begin_debug_marker(label);
1982 }
1983 }
1984 RenderCommand::PopDebugGroup => {
1985 let scope = PassErrorScope::PopDebugGroup;
1986 if state.debug_scope_depth == 0 {
1987 return Err(RenderPassErrorInner::InvalidPopDebugGroup)
1988 .map_pass_err(scope);
1989 }
1990 state.debug_scope_depth -= 1;
1991 unsafe {
1992 raw.end_debug_marker();
1993 }
1994 }
1995 RenderCommand::InsertDebugMarker { color: _, len } => {
1996 let label =
1997 str::from_utf8(&base.string_data[string_offset..string_offset + len])
1998 .unwrap();
1999 string_offset += len;
2000 unsafe {
2001 raw.insert_debug_marker(label);
2002 }
2003 }
2004 RenderCommand::WriteTimestamp {
2005 query_set_id,
2006 query_index,
2007 } => {
2008 let scope = PassErrorScope::WriteTimestamp;
2009
2010 device
2011 .require_features(wgt::Features::TIMESTAMP_QUERY_INSIDE_PASSES)
2012 .map_pass_err(scope)?;
2013
2014 let query_set: &resource::QuerySet<A> = cmd_buf
2015 .trackers
2016 .query_sets
2017 .add_single(&*query_set_guard, query_set_id)
2018 .ok_or(RenderCommandError::InvalidQuerySet(query_set_id))
2019 .map_pass_err(scope)?;
2020
2021 query_set
2022 .validate_and_write_timestamp(
2023 raw,
2024 query_set_id,
2025 query_index,
2026 Some(&mut query_reset_state),
2027 )
2028 .map_pass_err(scope)?;
2029 }
2030 RenderCommand::BeginPipelineStatisticsQuery {
2031 query_set_id,
2032 query_index,
2033 } => {
2034 let scope = PassErrorScope::BeginPipelineStatisticsQuery;
2035
2036 let query_set: &resource::QuerySet<A> = cmd_buf
2037 .trackers
2038 .query_sets
2039 .add_single(&*query_set_guard, query_set_id)
2040 .ok_or(RenderCommandError::InvalidQuerySet(query_set_id))
2041 .map_pass_err(scope)?;
2042
2043 query_set
2044 .validate_and_begin_pipeline_statistics_query(
2045 raw,
2046 query_set_id,
2047 query_index,
2048 Some(&mut query_reset_state),
2049 &mut active_query,
2050 )
2051 .map_pass_err(scope)?;
2052 }
2053 RenderCommand::EndPipelineStatisticsQuery => {
2054 let scope = PassErrorScope::EndPipelineStatisticsQuery;
2055
2056 end_pipeline_statistics_query(raw, &*query_set_guard, &mut active_query)
2057 .map_pass_err(scope)?;
2058 }
2059 RenderCommand::ExecuteBundle(bundle_id) => {
2060 let scope = PassErrorScope::ExecuteBundle;
2061 let bundle: &command::RenderBundle<A> = cmd_buf
2062 .trackers
2063 .bundles
2064 .add_single(&*bundle_guard, bundle_id)
2065 .ok_or(RenderCommandError::InvalidRenderBundle(bundle_id))
2066 .map_pass_err(scope)?;
2067
2068 info.context
2069 .check_compatible(
2070 &bundle.context,
2071 RenderPassCompatibilityCheckType::RenderBundle,
2072 )
2073 .map_err(RenderPassErrorInner::IncompatibleBundleTargets)
2074 .map_pass_err(scope)?;
2075
2076 if (info.is_depth_read_only && !bundle.is_depth_read_only)
2077 || (info.is_stencil_read_only && !bundle.is_stencil_read_only)
2078 {
2079 return Err(
2080 RenderPassErrorInner::IncompatibleBundleReadOnlyDepthStencil {
2081 pass_depth: info.is_depth_read_only,
2082 pass_stencil: info.is_stencil_read_only,
2083 bundle_depth: bundle.is_depth_read_only,
2084 bundle_stencil: bundle.is_stencil_read_only,
2085 },
2086 )
2087 .map_pass_err(scope);
2088 }
2089
2090 cmd_buf.buffer_memory_init_actions.extend(
2091 bundle
2092 .buffer_memory_init_actions
2093 .iter()
2094 .filter_map(|action| match buffer_guard.get(action.id) {
2095 Ok(buffer) => buffer.initialization_status.check_action(action),
2096 Err(_) => None,
2097 }),
2098 );
2099 for action in bundle.texture_memory_init_actions.iter() {
2100 info.pending_discard_init_fixups.extend(
2101 cmd_buf
2102 .texture_memory_actions
2103 .register_init_action(action, &texture_guard),
2104 );
2105 }
2106
2107 unsafe {
2108 bundle.execute(
2109 raw,
2110 &*pipeline_layout_guard,
2111 &*bind_group_guard,
2112 &*render_pipeline_guard,
2113 &*buffer_guard,
2114 )
2115 }
2116 .map_err(|e| match e {
2117 ExecutionError::DestroyedBuffer(id) => {
2118 RenderCommandError::DestroyedBuffer(id)
2119 }
2120 ExecutionError::Unimplemented(what) => {
2121 RenderCommandError::Unimplemented(what)
2122 }
2123 })
2124 .map_pass_err(scope)?;
2125
2126 unsafe {
2127 info.usage_scope
2128 .merge_render_bundle(&*texture_guard, &bundle.used)
2129 .map_pass_err(scope)?;
2130 cmd_buf
2131 .trackers
2132 .add_from_render_bundle(&bundle.used)
2133 .map_pass_err(scope)?;
2134 };
2135 state.reset_bundle();
2136 }
2137 }
2138 }
2139
2140 log::trace!("Merging renderpass into cmd_buf {:?}", encoder_id);
2141 let (trackers, pending_discard_init_fixups) =
2142 info.finish(raw, &*texture_guard).map_pass_err(init_scope)?;
2143
2144 cmd_buf.encoder.close();
2145 (trackers, query_reset_state, pending_discard_init_fixups)
2146 };
2147
2148 let (mut cmb_guard, mut token) = hub.command_buffers.write(&mut token);
2149 let (query_set_guard, mut token) = hub.query_sets.read(&mut token);
2150 let (buffer_guard, mut token) = hub.buffers.read(&mut token);
2151 let (texture_guard, _) = hub.textures.read(&mut token);
2152
2153 let cmd_buf = cmb_guard.get_mut(encoder_id).unwrap();
2154 {
2155 let transit = cmd_buf.encoder.open();
2156
2157 fixup_discarded_surfaces(
2158 pending_discard_init_fixups.into_iter(),
2159 transit,
2160 &texture_guard,
2161 &mut cmd_buf.trackers.textures,
2162 &device_guard[cmd_buf.device_id.value],
2163 );
2164
2165 query_reset_state
2166 .reset_queries(
2167 transit,
2168 &query_set_guard,
2169 cmd_buf.device_id.value.0.backend(),
2170 )
2171 .map_err(RenderCommandError::InvalidQuerySet)
2172 .map_pass_err(PassErrorScope::QueryReset)?;
2173
2174 super::CommandBuffer::insert_barriers_from_scope(
2175 transit,
2176 &mut cmd_buf.trackers,
2177 &scope,
2178 &*buffer_guard,
2179 &*texture_guard,
2180 );
2181 }
2182
2183 cmd_buf.status = CommandEncoderStatus::Recording;
2184 cmd_buf.encoder.close_and_swap();
2185
2186 Ok(())
2187 }
2188}
2189
2190pub mod render_ffi {
2191 use super::{
2192 super::{Rect, RenderCommand},
2193 RenderPass,
2194 };
2195 use crate::{id, RawString};
2196 use std::{convert::TryInto, ffi, num::NonZeroU32, slice};
2197 use wgt::{BufferAddress, BufferSize, Color, DynamicOffset, IndexFormat};
2198
2199 #[no_mangle]
2204 pub unsafe extern "C" fn wgpu_render_pass_set_bind_group(
2205 pass: &mut RenderPass,
2206 index: u32,
2207 bind_group_id: id::BindGroupId,
2208 offsets: *const DynamicOffset,
2209 offset_length: usize,
2210 ) {
2211 let redundant = unsafe {
2212 pass.current_bind_groups.set_and_check_redundant(
2213 bind_group_id,
2214 index,
2215 &mut pass.base.dynamic_offsets,
2216 offsets,
2217 offset_length,
2218 )
2219 };
2220
2221 if redundant {
2222 return;
2223 }
2224
2225 pass.base.commands.push(RenderCommand::SetBindGroup {
2226 index,
2227 num_dynamic_offsets: offset_length.try_into().unwrap(),
2228 bind_group_id,
2229 });
2230 }
2231
2232 #[no_mangle]
2233 pub extern "C" fn wgpu_render_pass_set_pipeline(
2234 pass: &mut RenderPass,
2235 pipeline_id: id::RenderPipelineId,
2236 ) {
2237 if pass.current_pipeline.set_and_check_redundant(pipeline_id) {
2238 return;
2239 }
2240
2241 pass.base
2242 .commands
2243 .push(RenderCommand::SetPipeline(pipeline_id));
2244 }
2245
2246 #[no_mangle]
2247 pub extern "C" fn wgpu_render_pass_set_vertex_buffer(
2248 pass: &mut RenderPass,
2249 slot: u32,
2250 buffer_id: id::BufferId,
2251 offset: BufferAddress,
2252 size: Option<BufferSize>,
2253 ) {
2254 pass.base.commands.push(RenderCommand::SetVertexBuffer {
2255 slot,
2256 buffer_id,
2257 offset,
2258 size,
2259 });
2260 }
2261
2262 #[no_mangle]
2263 pub extern "C" fn wgpu_render_pass_set_index_buffer(
2264 pass: &mut RenderPass,
2265 buffer: id::BufferId,
2266 index_format: IndexFormat,
2267 offset: BufferAddress,
2268 size: Option<BufferSize>,
2269 ) {
2270 pass.set_index_buffer(buffer, index_format, offset, size);
2271 }
2272
2273 #[no_mangle]
2274 pub extern "C" fn wgpu_render_pass_set_blend_constant(pass: &mut RenderPass, color: &Color) {
2275 pass.base
2276 .commands
2277 .push(RenderCommand::SetBlendConstant(*color));
2278 }
2279
2280 #[no_mangle]
2281 pub extern "C" fn wgpu_render_pass_set_stencil_reference(pass: &mut RenderPass, value: u32) {
2282 pass.base
2283 .commands
2284 .push(RenderCommand::SetStencilReference(value));
2285 }
2286
2287 #[no_mangle]
2288 pub extern "C" fn wgpu_render_pass_set_viewport(
2289 pass: &mut RenderPass,
2290 x: f32,
2291 y: f32,
2292 w: f32,
2293 h: f32,
2294 depth_min: f32,
2295 depth_max: f32,
2296 ) {
2297 pass.base.commands.push(RenderCommand::SetViewport {
2298 rect: Rect { x, y, w, h },
2299 depth_min,
2300 depth_max,
2301 });
2302 }
2303
2304 #[no_mangle]
2305 pub extern "C" fn wgpu_render_pass_set_scissor_rect(
2306 pass: &mut RenderPass,
2307 x: u32,
2308 y: u32,
2309 w: u32,
2310 h: u32,
2311 ) {
2312 pass.base
2313 .commands
2314 .push(RenderCommand::SetScissor(Rect { x, y, w, h }));
2315 }
2316
2317 #[no_mangle]
2322 pub unsafe extern "C" fn wgpu_render_pass_set_push_constants(
2323 pass: &mut RenderPass,
2324 stages: wgt::ShaderStages,
2325 offset: u32,
2326 size_bytes: u32,
2327 data: *const u8,
2328 ) {
2329 assert_eq!(
2330 offset & (wgt::PUSH_CONSTANT_ALIGNMENT - 1),
2331 0,
2332 "Push constant offset must be aligned to 4 bytes."
2333 );
2334 assert_eq!(
2335 size_bytes & (wgt::PUSH_CONSTANT_ALIGNMENT - 1),
2336 0,
2337 "Push constant size must be aligned to 4 bytes."
2338 );
2339 let data_slice = unsafe { slice::from_raw_parts(data, size_bytes as usize) };
2340 let value_offset = pass.base.push_constant_data.len().try_into().expect(
2341 "Ran out of push constant space. Don't set 4gb of push constants per RenderPass.",
2342 );
2343
2344 pass.base.push_constant_data.extend(
2345 data_slice
2346 .chunks_exact(wgt::PUSH_CONSTANT_ALIGNMENT as usize)
2347 .map(|arr| u32::from_ne_bytes([arr[0], arr[1], arr[2], arr[3]])),
2348 );
2349
2350 pass.base.commands.push(RenderCommand::SetPushConstant {
2351 stages,
2352 offset,
2353 size_bytes,
2354 values_offset: Some(value_offset),
2355 });
2356 }
2357
2358 #[no_mangle]
2359 pub extern "C" fn wgpu_render_pass_draw(
2360 pass: &mut RenderPass,
2361 vertex_count: u32,
2362 instance_count: u32,
2363 first_vertex: u32,
2364 first_instance: u32,
2365 ) {
2366 pass.base.commands.push(RenderCommand::Draw {
2367 vertex_count,
2368 instance_count,
2369 first_vertex,
2370 first_instance,
2371 });
2372 }
2373
2374 #[no_mangle]
2375 pub extern "C" fn wgpu_render_pass_draw_indexed(
2376 pass: &mut RenderPass,
2377 index_count: u32,
2378 instance_count: u32,
2379 first_index: u32,
2380 base_vertex: i32,
2381 first_instance: u32,
2382 ) {
2383 pass.base.commands.push(RenderCommand::DrawIndexed {
2384 index_count,
2385 instance_count,
2386 first_index,
2387 base_vertex,
2388 first_instance,
2389 });
2390 }
2391
2392 #[no_mangle]
2393 pub extern "C" fn wgpu_render_pass_draw_indirect(
2394 pass: &mut RenderPass,
2395 buffer_id: id::BufferId,
2396 offset: BufferAddress,
2397 ) {
2398 pass.base.commands.push(RenderCommand::MultiDrawIndirect {
2399 buffer_id,
2400 offset,
2401 count: None,
2402 indexed: false,
2403 });
2404 }
2405
2406 #[no_mangle]
2407 pub extern "C" fn wgpu_render_pass_draw_indexed_indirect(
2408 pass: &mut RenderPass,
2409 buffer_id: id::BufferId,
2410 offset: BufferAddress,
2411 ) {
2412 pass.base.commands.push(RenderCommand::MultiDrawIndirect {
2413 buffer_id,
2414 offset,
2415 count: None,
2416 indexed: true,
2417 });
2418 }
2419
2420 #[no_mangle]
2421 pub extern "C" fn wgpu_render_pass_multi_draw_indirect(
2422 pass: &mut RenderPass,
2423 buffer_id: id::BufferId,
2424 offset: BufferAddress,
2425 count: u32,
2426 ) {
2427 pass.base.commands.push(RenderCommand::MultiDrawIndirect {
2428 buffer_id,
2429 offset,
2430 count: NonZeroU32::new(count),
2431 indexed: false,
2432 });
2433 }
2434
2435 #[no_mangle]
2436 pub extern "C" fn wgpu_render_pass_multi_draw_indexed_indirect(
2437 pass: &mut RenderPass,
2438 buffer_id: id::BufferId,
2439 offset: BufferAddress,
2440 count: u32,
2441 ) {
2442 pass.base.commands.push(RenderCommand::MultiDrawIndirect {
2443 buffer_id,
2444 offset,
2445 count: NonZeroU32::new(count),
2446 indexed: true,
2447 });
2448 }
2449
2450 #[no_mangle]
2451 pub extern "C" fn wgpu_render_pass_multi_draw_indirect_count(
2452 pass: &mut RenderPass,
2453 buffer_id: id::BufferId,
2454 offset: BufferAddress,
2455 count_buffer_id: id::BufferId,
2456 count_buffer_offset: BufferAddress,
2457 max_count: u32,
2458 ) {
2459 pass.base
2460 .commands
2461 .push(RenderCommand::MultiDrawIndirectCount {
2462 buffer_id,
2463 offset,
2464 count_buffer_id,
2465 count_buffer_offset,
2466 max_count,
2467 indexed: false,
2468 });
2469 }
2470
2471 #[no_mangle]
2472 pub extern "C" fn wgpu_render_pass_multi_draw_indexed_indirect_count(
2473 pass: &mut RenderPass,
2474 buffer_id: id::BufferId,
2475 offset: BufferAddress,
2476 count_buffer_id: id::BufferId,
2477 count_buffer_offset: BufferAddress,
2478 max_count: u32,
2479 ) {
2480 pass.base
2481 .commands
2482 .push(RenderCommand::MultiDrawIndirectCount {
2483 buffer_id,
2484 offset,
2485 count_buffer_id,
2486 count_buffer_offset,
2487 max_count,
2488 indexed: true,
2489 });
2490 }
2491
2492 #[no_mangle]
2497 pub unsafe extern "C" fn wgpu_render_pass_push_debug_group(
2498 pass: &mut RenderPass,
2499 label: RawString,
2500 color: u32,
2501 ) {
2502 let bytes = unsafe { ffi::CStr::from_ptr(label) }.to_bytes();
2503 pass.base.string_data.extend_from_slice(bytes);
2504
2505 pass.base.commands.push(RenderCommand::PushDebugGroup {
2506 color,
2507 len: bytes.len(),
2508 });
2509 }
2510
2511 #[no_mangle]
2512 pub extern "C" fn wgpu_render_pass_pop_debug_group(pass: &mut RenderPass) {
2513 pass.base.commands.push(RenderCommand::PopDebugGroup);
2514 }
2515
2516 #[no_mangle]
2521 pub unsafe extern "C" fn wgpu_render_pass_insert_debug_marker(
2522 pass: &mut RenderPass,
2523 label: RawString,
2524 color: u32,
2525 ) {
2526 let bytes = unsafe { ffi::CStr::from_ptr(label) }.to_bytes();
2527 pass.base.string_data.extend_from_slice(bytes);
2528
2529 pass.base.commands.push(RenderCommand::InsertDebugMarker {
2530 color,
2531 len: bytes.len(),
2532 });
2533 }
2534
2535 #[no_mangle]
2536 pub extern "C" fn wgpu_render_pass_write_timestamp(
2537 pass: &mut RenderPass,
2538 query_set_id: id::QuerySetId,
2539 query_index: u32,
2540 ) {
2541 pass.base.commands.push(RenderCommand::WriteTimestamp {
2542 query_set_id,
2543 query_index,
2544 });
2545 }
2546
2547 #[no_mangle]
2548 pub extern "C" fn wgpu_render_pass_begin_pipeline_statistics_query(
2549 pass: &mut RenderPass,
2550 query_set_id: id::QuerySetId,
2551 query_index: u32,
2552 ) {
2553 pass.base
2554 .commands
2555 .push(RenderCommand::BeginPipelineStatisticsQuery {
2556 query_set_id,
2557 query_index,
2558 });
2559 }
2560
2561 #[no_mangle]
2562 pub extern "C" fn wgpu_render_pass_end_pipeline_statistics_query(pass: &mut RenderPass) {
2563 pass.base
2564 .commands
2565 .push(RenderCommand::EndPipelineStatisticsQuery);
2566 }
2567
2568 #[no_mangle]
2573 pub unsafe extern "C" fn wgpu_render_pass_execute_bundles(
2574 pass: &mut RenderPass,
2575 render_bundle_ids: *const id::RenderBundleId,
2576 render_bundle_ids_length: usize,
2577 ) {
2578 for &bundle_id in
2579 unsafe { slice::from_raw_parts(render_bundle_ids, render_bundle_ids_length) }
2580 {
2581 pass.base
2582 .commands
2583 .push(RenderCommand::ExecuteBundle(bundle_id));
2584 }
2585 pass.current_pipeline.reset();
2586 pass.current_bind_groups.reset();
2587 }
2588}