1mod allocator;
12mod bind;
13mod bundle;
14mod clear;
15mod compute;
16mod compute_command;
17mod draw;
18mod encoder;
19mod encoder_command;
20mod memory_init;
21mod pass;
22mod query;
23mod ray_tracing;
24mod ray_tracing_pass;
25mod ray_tracing_pass_commands;
26mod render;
27mod render_command;
28mod timestamp_writes;
29mod transfer;
30mod transition_resources;
31
32use alloc::{borrow::ToOwned as _, boxed::Box, string::String, sync::Arc, vec::Vec};
33use core::convert::Infallible;
34use core::mem::{self, ManuallyDrop};
35use core::{ops, panic};
36
37#[cfg(feature = "serde")]
38pub(crate) use self::encoder_command::serde_object_reference_struct;
39#[cfg(any(feature = "trace", feature = "replay"))]
40#[doc(hidden)]
41pub use self::encoder_command::PointerReferences;
42pub use self::{
46 bundle::{
47 CreateRenderBundleError, ExecutionError, RenderBundle, RenderBundleDescriptor,
48 RenderBundleEncoder, RenderBundleEncoderDescriptor, RenderBundleError,
49 RenderBundleErrorInner,
50 },
51 clear::ClearError,
52 compute::{
53 ComputeBasePass, ComputePass, ComputePassDescriptor, ComputePassError,
54 ComputePassErrorInner, DispatchError,
55 },
56 compute_command::ArcComputeCommand,
57 draw::{DrawError, Rect, RenderCommandError},
58 encoder_command::{ArcCommand, ArcReferences, Command, ReferenceType},
59 query::{QueryError, QueryUseError, ResolveError, SimplifiedQueryType},
60 ray_tracing_pass::{
61 RayTracingBasePass, RayTracingPass, RayTracingPassDescriptor, RayTracingPassError,
62 RayTracingPassErrorInner, TraceRayError,
63 },
64 ray_tracing_pass_commands::ArcRayTracingCommand,
65 render::{
66 AttachmentError, AttachmentErrorLocation, ColorAttachmentError, ColorAttachments, LoadOp,
67 PassChannel, RenderBasePass, RenderPass, RenderPassColorAttachment,
68 RenderPassDepthStencilAttachment, RenderPassError, RenderPassErrorInner,
69 ResolvedPassChannel, ResolvedRenderPassDepthStencilAttachment,
70 ResolvedRenderPassDescriptor, StoreOp,
71 },
72 render_command::ArcRenderCommand,
73 transfer::{CopySide, TransferError},
74 transition_resources::TransitionResourcesError,
75};
76pub(crate) use self::{
77 clear::clear_texture,
78 encoder::EncodingState,
79 memory_init::CommandBufferTextureMemoryActions,
80 render::{get_dst_stride_of_indirect_args, get_src_stride_of_indirect_args, VertexState},
81 transfer::{
82 validate_linear_texture_data, validate_texture_buffer_copy,
83 validate_texture_copy_dst_format, validate_texture_copy_range, CopyRange,
84 },
85};
86
87pub(crate) use allocator::CommandAllocator;
88
89pub use self::{
91 compute_command::ComputeCommand, ray_tracing_pass_commands::RayTracingCommand,
92 render_command::RenderCommand,
93};
94
95pub use timestamp_writes::PassTimestampWrites;
96
97use crate::binding_model::{BindGroup, BindingError};
98use crate::device::queue::TempResource;
99use crate::device::{Device, DeviceError, MissingFeatures};
100use crate::lock::{rank, Mutex};
101use crate::snatch::SnatchGuard;
102
103use crate::init_tracker::BufferInitTrackerAction;
104use crate::ray_tracing::{AsAction, BuildAccelerationStructureError};
105use crate::resource::{
106 DestroyedResourceError, InvalidOrDestroyedResourceError, InvalidResourceError, Labeled,
107 ParentDevice as _, QuerySet,
108};
109use crate::track::{DeviceTracker, ResourceUsageCompatibilityError, Tracker, UsageScope};
110use crate::{api_log, resource_log, Label};
111use crate::{hal_label, LabelHelpers};
112
113use wgt::error::{ErrorType, WebGpuError};
114
115use thiserror::Error;
116
117pub(crate) struct EncoderErrorState {
118 error: CommandEncoderError,
119
120 #[cfg(feature = "trace")]
121 trace_commands: Option<Vec<Command<PointerReferences>>>,
122}
123
124fn make_error_state<E: Into<CommandEncoderError>>(error: E) -> CommandEncoderStatus {
134 CommandEncoderStatus::Error(EncoderErrorState {
135 error: error.into(),
136
137 #[cfg(feature = "trace")]
138 trace_commands: None,
139 })
140}
141
142pub(crate) enum CommandEncoderStatus {
148 Recording(CommandBufferMutable),
157
158 Locked(CommandBufferMutable),
167
168 Consumed,
169
170 Finished(CommandBufferMutable),
183
184 Error(EncoderErrorState),
189
190 Transitioning,
193}
194
195impl CommandEncoderStatus {
196 #[doc(hidden)]
197 fn replay(&mut self, commands: Vec<Command<ArcReferences>>) {
198 let Self::Recording(cmd_buf_data) = self else {
199 panic!("encoder should be in the recording state");
200 };
201 cmd_buf_data.commands.extend(commands);
202 }
203
204 fn push_with<F: FnOnce() -> Result<ArcCommand, E>, E: Clone + Into<CommandEncoderError>>(
220 &mut self,
221 f: F,
222 ) -> Result<(), EncoderStateError> {
223 match self {
224 Self::Recording(cmd_buf_data) => {
225 cmd_buf_data.encoder.api.set(EncodingApi::Wgpu);
226 match f() {
227 Ok(cmd) => cmd_buf_data.commands.push(cmd),
228 Err(err) => {
229 self.invalidate(err);
230 }
231 }
232 Ok(())
233 }
234 Self::Locked(_) => {
235 self.invalidate(EncoderStateError::Locked);
238 Ok(())
239 }
240 Self::Finished(_) => Err(self.invalidate(EncoderStateError::Ended)),
243 Self::Consumed => Err(EncoderStateError::Ended),
244 Self::Error(_) => Ok(()),
247 Self::Transitioning => unreachable!(),
248 }
249 }
250
251 fn with_buffer<
265 F: FnOnce(&mut CommandBufferMutable) -> Result<(), E>,
266 E: Clone + Into<CommandEncoderError>,
267 >(
268 &mut self,
269 api: EncodingApi,
270 f: F,
271 ) -> Result<(), EncoderStateError> {
272 match self {
273 Self::Recording(inner) => {
274 inner.encoder.api.set(api);
275 RecordingGuard { inner: self }.record(f);
276 Ok(())
277 }
278 Self::Locked(_) => {
279 self.invalidate(EncoderStateError::Locked);
282 Ok(())
283 }
284 Self::Finished(_) => Err(self.invalidate(EncoderStateError::Ended)),
287 Self::Consumed => Err(EncoderStateError::Ended),
288 Self::Error(_) => Ok(()),
291 Self::Transitioning => unreachable!(),
292 }
293 }
294
295 pub(crate) fn record_as_hal_mut<T, F: FnOnce(Option<&mut CommandBufferMutable>) -> T>(
303 &mut self,
304 f: F,
305 ) -> T {
306 match self {
307 Self::Recording(inner) => {
308 inner.encoder.api.set(EncodingApi::Raw);
309 RecordingGuard { inner: self }.record_as_hal_mut(f)
310 }
311 Self::Locked(_) => {
312 self.invalidate(EncoderStateError::Locked);
313 f(None)
314 }
315 Self::Finished(_) => {
316 self.invalidate(EncoderStateError::Ended);
317 f(None)
318 }
319 Self::Consumed => f(None),
320 Self::Error(_) => f(None),
321 Self::Transitioning => unreachable!(),
322 }
323 }
324
325 fn lock_encoder(&mut self) -> Result<(), EncoderStateError> {
331 match mem::replace(self, Self::Transitioning) {
332 Self::Recording(inner) => {
333 *self = Self::Locked(inner);
334 Ok(())
335 }
336 st @ Self::Finished(_) => {
337 *self = st;
341 Err(EncoderStateError::Ended)
342 }
343 Self::Locked(_) => Err(self.invalidate(EncoderStateError::Locked)),
344 st @ Self::Consumed => {
345 *self = st;
346 Err(EncoderStateError::Ended)
347 }
348 st @ Self::Error(_) => {
349 *self = st;
350 Err(EncoderStateError::Invalid)
351 }
352 Self::Transitioning => unreachable!(),
353 }
354 }
355
356 fn unlock_encoder(&mut self) -> Result<(), EncoderStateError> {
366 match mem::replace(self, Self::Transitioning) {
367 Self::Locked(inner) => {
368 *self = Self::Recording(inner);
369 Ok(())
370 }
371 st @ Self::Finished(_) => {
372 *self = st;
373 Err(EncoderStateError::Ended)
374 }
375 Self::Recording(_) => {
376 *self = make_error_state(EncoderStateError::Unlocked);
377 Err(EncoderStateError::Unlocked)
378 }
379 st @ Self::Consumed => {
380 *self = st;
381 Err(EncoderStateError::Ended)
382 }
383 st @ Self::Error(_) => {
384 *self = st;
387 Ok(())
388 }
389 Self::Transitioning => unreachable!(),
390 }
391 }
392
393 fn finish(&mut self) -> Self {
394 match mem::replace(self, Self::Consumed) {
397 Self::Recording(inner) => {
398 if inner.encoder.api != EncodingApi::Raw {
401 assert!(!inner.encoder.is_open);
402 }
403 Self::Finished(inner)
404 }
405 Self::Consumed | Self::Finished(_) => make_error_state(EncoderStateError::Ended),
406 Self::Locked(_) => make_error_state(EncoderStateError::Locked),
407 st @ Self::Error(_) => st,
408 Self::Transitioning => unreachable!(),
409 }
410 }
411
412 fn invalidate<E: Clone + Into<CommandEncoderError>>(&mut self, err: E) -> E {
420 #[cfg(feature = "trace")]
421 let trace_commands = match self {
422 Self::Recording(cmd_buf_data) => Some(
423 mem::take(&mut cmd_buf_data.commands)
424 .into_iter()
425 .map(crate::device::trace::IntoTrace::into_trace)
426 .collect(),
427 ),
428 _ => None,
429 };
430
431 let enc_err = err.clone().into();
432 api_log!("Invalidating command encoder: {enc_err:?}");
433 *self = Self::Error(EncoderErrorState {
434 error: enc_err,
435 #[cfg(feature = "trace")]
436 trace_commands,
437 });
438 err
439 }
440}
441
442pub(crate) struct RecordingGuard<'a> {
455 inner: &'a mut CommandEncoderStatus,
456}
457
458impl<'a> RecordingGuard<'a> {
459 pub(crate) fn mark_successful(self) {
460 mem::forget(self)
461 }
462
463 fn record<
464 F: FnOnce(&mut CommandBufferMutable) -> Result<(), E>,
465 E: Clone + Into<CommandEncoderError>,
466 >(
467 mut self,
468 f: F,
469 ) {
470 match f(&mut self) {
471 Ok(()) => self.mark_successful(),
472 Err(err) => {
473 self.inner.invalidate(err);
474 }
475 }
476 }
477
478 pub(crate) fn record_as_hal_mut<T, F: FnOnce(Option<&mut CommandBufferMutable>) -> T>(
481 mut self,
482 f: F,
483 ) -> T {
484 let res = f(Some(&mut self));
485 self.mark_successful();
486 res
487 }
488}
489
490impl<'a> Drop for RecordingGuard<'a> {
491 fn drop(&mut self) {
492 if matches!(*self.inner, CommandEncoderStatus::Error(_)) {
493 return;
495 }
496 self.inner.invalidate(EncoderStateError::Invalid);
497 }
498}
499
500impl<'a> ops::Deref for RecordingGuard<'a> {
501 type Target = CommandBufferMutable;
502
503 fn deref(&self) -> &Self::Target {
504 match &*self.inner {
505 CommandEncoderStatus::Recording(command_buffer_mutable) => command_buffer_mutable,
506 _ => unreachable!(),
507 }
508 }
509}
510
511impl<'a> ops::DerefMut for RecordingGuard<'a> {
512 fn deref_mut(&mut self) -> &mut Self::Target {
513 match self.inner {
514 CommandEncoderStatus::Recording(command_buffer_mutable) => command_buffer_mutable,
515 _ => unreachable!(),
516 }
517 }
518}
519
520pub struct CommandEncoder {
521 pub(crate) device: Arc<Device>,
522
523 pub(crate) label: String,
524
525 pub(crate) data: Mutex<CommandEncoderStatus>,
527}
528
529crate::impl_resource_type!(CommandEncoder);
530crate::impl_labeled!(CommandEncoder);
531crate::impl_parent_device!(CommandEncoder);
532crate::impl_storage_item!(CommandEncoder);
533
534impl Drop for CommandEncoder {
535 #[allow(trivial_casts)]
536 fn drop(&mut self) {
537 profiling::scope!("CommandEncoder::drop");
538 api_log!("CommandEncoder::drop {:?}", self as *const _);
539 resource_log!("Drop {}", self.error_ident());
540 }
541}
542
543#[derive(Copy, Clone, Debug, Eq, PartialEq)]
547pub enum EncodingApi {
548 Wgpu,
550
551 Raw,
553
554 Undecided,
556
557 InternalUse,
559}
560
561impl EncodingApi {
562 pub(crate) fn set(&mut self, api: EncodingApi) {
563 match *self {
564 EncodingApi::Undecided => {
565 *self = api;
566 }
567 self_api if self_api != api => {
568 panic!("Mixing the wgpu encoding API with the raw encoding API is not permitted");
569 }
570 _ => {}
571 }
572 }
573}
574
575pub(crate) struct InnerCommandEncoder {
591 pub(crate) raw: ManuallyDrop<Box<dyn hal::DynCommandEncoder>>,
599
600 pub(crate) list: Vec<Box<dyn hal::DynCommandBuffer>>,
612
613 pub(crate) device: Arc<Device>,
614
615 pub(crate) is_open: bool,
622
623 pub(crate) api: EncodingApi,
629
630 pub(crate) label: String,
631}
632
633impl InnerCommandEncoder {
634 fn close_and_swap(&mut self) -> Result<(), DeviceError> {
666 self.close_and_insert_at(self.list.len() - 1)
667 }
668
669 pub(crate) fn close_and_push_front(&mut self) -> Result<(), DeviceError> {
686 self.close_and_insert_at(0)
687 }
688
689 pub(crate) fn close_and_insert_at(&mut self, index: usize) -> Result<(), DeviceError> {
706 assert!(self.is_open);
707 self.is_open = false;
708
709 let cmd_buf =
710 unsafe { self.raw.end_encoding() }.map_err(|e| self.device.handle_hal_error(e))?;
711 self.list.insert(index, cmd_buf);
712
713 Ok(())
714 }
715
716 pub(crate) fn close(&mut self) -> Result<(), DeviceError> {
727 assert!(self.is_open);
728 self.is_open = false;
729
730 let cmd_buf =
731 unsafe { self.raw.end_encoding() }.map_err(|e| self.device.handle_hal_error(e))?;
732 self.list.push(cmd_buf);
733
734 Ok(())
735 }
736
737 fn close_if_open(&mut self) -> Result<(), DeviceError> {
748 if self.is_open {
749 self.is_open = false;
750 let cmd_buf =
751 unsafe { self.raw.end_encoding() }.map_err(|e| self.device.handle_hal_error(e))?;
752 self.list.push(cmd_buf);
753 }
754
755 Ok(())
756 }
757
758 fn open_if_closed(&mut self) -> Result<&mut dyn hal::DynCommandEncoder, DeviceError> {
764 if !self.is_open {
765 let hal_label = hal_label(Some(self.label.as_str()), self.device.instance_flags);
766 unsafe { self.raw.begin_encoding(hal_label) }
767 .map_err(|e| self.device.handle_hal_error(e))?;
768 self.is_open = true;
769 }
770
771 Ok(self.raw.as_mut())
772 }
773
774 pub(crate) fn open(&mut self) -> Result<&mut dyn hal::DynCommandEncoder, DeviceError> {
778 if !self.is_open {
779 let hal_label = hal_label(Some(self.label.as_str()), self.device.instance_flags);
780 unsafe { self.raw.begin_encoding(hal_label) }
781 .map_err(|e| self.device.handle_hal_error(e))?;
782 self.is_open = true;
783 }
784
785 Ok(self.raw.as_mut())
786 }
787
788 pub(crate) fn open_pass(
797 &mut self,
798 label: Option<&str>,
799 ) -> Result<&mut dyn hal::DynCommandEncoder, DeviceError> {
800 assert!(!self.is_open);
801
802 let hal_label = hal_label(label, self.device.instance_flags);
803 unsafe { self.raw.begin_encoding(hal_label) }
804 .map_err(|e| self.device.handle_hal_error(e))?;
805 self.is_open = true;
806
807 Ok(self.raw.as_mut())
808 }
809}
810
811impl Drop for InnerCommandEncoder {
812 fn drop(&mut self) {
813 if self.is_open {
814 unsafe { self.raw.discard_encoding() };
815 }
816 unsafe {
817 self.raw.reset_all(mem::take(&mut self.list));
818 }
819 let raw = unsafe { ManuallyDrop::take(&mut self.raw) };
821 self.device.command_allocator.release_encoder(raw);
822 }
823}
824
825pub(crate) struct BakedCommands {
828 pub(crate) encoder: InnerCommandEncoder,
829 pub(crate) trackers: Tracker,
830 pub(crate) temp_resources: Vec<TempResource>,
831 pub(crate) indirect_draw_validation_resources: crate::indirect_validation::DrawResources,
832 buffer_memory_init_actions: Vec<BufferInitTrackerAction>,
833 texture_memory_actions: CommandBufferTextureMemoryActions,
834 pub(crate) query_set_writes: query::QuerySetWrites,
835 pub(crate) deferred_query_set_resolves: Vec<query::DeferredQuerySetResolve>,
836}
837
838pub struct CommandBufferMutable {
840 pub(crate) encoder: InnerCommandEncoder,
845
846 pub(crate) trackers: Tracker,
848
849 buffer_memory_init_actions: Vec<BufferInitTrackerAction>,
856 texture_memory_actions: CommandBufferTextureMemoryActions,
857
858 as_actions: Vec<AsAction>,
859 temp_resources: Vec<TempResource>,
860
861 indirect_draw_validation_resources: crate::indirect_validation::DrawResources,
862
863 pub(crate) commands: Vec<Command<ArcReferences>>,
864
865 #[cfg(feature = "trace")]
868 pub(crate) trace_commands: Option<Vec<Command<PointerReferences>>>,
869
870 pub(crate) query_set_writes: query::QuerySetWrites,
872 pub(crate) deferred_query_set_resolves: Vec<query::DeferredQuerySetResolve>,
874}
875
876impl CommandBufferMutable {
877 pub(crate) fn into_baked_commands(self) -> BakedCommands {
878 BakedCommands {
879 encoder: self.encoder,
880 trackers: self.trackers,
881 temp_resources: self.temp_resources,
882 indirect_draw_validation_resources: self.indirect_draw_validation_resources,
883 buffer_memory_init_actions: self.buffer_memory_init_actions,
884 texture_memory_actions: self.texture_memory_actions,
885 query_set_writes: self.query_set_writes,
886 deferred_query_set_resolves: self.deferred_query_set_resolves,
887 }
888 }
889}
890
891pub struct CommandBuffer {
897 pub(crate) device: Arc<Device>,
898 label: String,
900
901 pub(crate) data: Mutex<CommandEncoderStatus>,
903}
904
905impl Drop for CommandBuffer {
906 #[allow(trivial_casts)]
907 fn drop(&mut self) {
908 profiling::scope!("CommandBuffer::drop");
909 api_log!("CommandBuffer::drop {:?}", self as *const _);
910 resource_log!("Drop {}", self.error_ident());
911 }
912}
913
914impl CommandEncoder {
915 pub(crate) fn new(
916 encoder: Box<dyn hal::DynCommandEncoder>,
917 device: &Arc<Device>,
918 label: &Label,
919 ) -> Self {
920 CommandEncoder {
921 device: device.clone(),
922 label: label.to_string(),
923 data: Mutex::new(
924 rank::COMMAND_BUFFER_DATA,
925 CommandEncoderStatus::Recording(CommandBufferMutable {
926 encoder: InnerCommandEncoder {
927 raw: ManuallyDrop::new(encoder),
928 list: Vec::new(),
929 device: device.clone(),
930 is_open: false,
931 api: EncodingApi::Undecided,
932 label: label.to_string(),
933 },
934 trackers: Tracker::new(
935 device.ordered_buffer_usages,
936 device.ordered_texture_usages,
937 ),
938 buffer_memory_init_actions: Default::default(),
939 texture_memory_actions: Default::default(),
940 as_actions: Default::default(),
941 temp_resources: Default::default(),
942 indirect_draw_validation_resources:
943 crate::indirect_validation::DrawResources::new(device.clone()),
944 commands: Vec::new(),
945 query_set_writes: Default::default(),
946 deferred_query_set_resolves: Default::default(),
947 #[cfg(feature = "trace")]
948 trace_commands: if device.trace.lock().is_some() {
949 Some(Vec::new())
950 } else {
951 None
952 },
953 }),
954 ),
955 }
956 }
957
958 pub(crate) fn new_invalid(
959 device: &Arc<Device>,
960 label: &Label,
961 err: CommandEncoderError,
962 ) -> Arc<Self> {
963 Arc::new(CommandEncoder {
964 device: device.clone(),
965 label: label.to_string(),
966 data: Mutex::new(rank::COMMAND_BUFFER_DATA, make_error_state(err)),
967 })
968 }
969
970 pub(crate) fn validate_pass_timestamp_writes<E>(
971 device: &Device,
972 timestamp_writes: &PassTimestampWrites<Arc<QuerySet>>,
973 ) -> Result<PassTimestampWrites, E>
974 where
975 E: From<TimestampWritesError>
976 + From<QueryUseError>
977 + From<DeviceError>
978 + From<MissingFeatures>
979 + From<InvalidResourceError>,
980 {
981 let &PassTimestampWrites {
982 ref query_set,
983 beginning_of_pass_write_index,
984 end_of_pass_write_index,
985 } = timestamp_writes;
986
987 device.require_features(wgt::Features::TIMESTAMP_QUERY)?;
988
989 query_set.check_is_valid()?;
990 query_set.same_device(device)?;
991
992 for idx in [beginning_of_pass_write_index, end_of_pass_write_index]
993 .into_iter()
994 .flatten()
995 {
996 query_set.validate_query(SimplifiedQueryType::Timestamp, idx, None)?;
997 }
998
999 if let Some((begin, end)) = beginning_of_pass_write_index.zip(end_of_pass_write_index) {
1000 if begin == end {
1001 return Err(TimestampWritesError::IndicesEqual { idx: begin }.into());
1002 }
1003 }
1004
1005 if beginning_of_pass_write_index
1006 .or(end_of_pass_write_index)
1007 .is_none()
1008 {
1009 return Err(TimestampWritesError::IndicesMissing.into());
1010 }
1011
1012 Ok(PassTimestampWrites {
1013 query_set: query_set.clone(),
1014 beginning_of_pass_write_index,
1015 end_of_pass_write_index,
1016 })
1017 }
1018
1019 pub(crate) fn insert_barriers_from_tracker(
1020 raw: &mut dyn hal::DynCommandEncoder,
1021 base: &mut Tracker,
1022 head: &Tracker,
1023 snatch_guard: &SnatchGuard,
1024 ) {
1025 profiling::scope!("insert_barriers");
1026
1027 base.buffers.set_from_tracker(&head.buffers);
1028 base.textures.set_from_tracker(&head.textures);
1029
1030 Self::drain_barriers(raw, base, snatch_guard);
1031 }
1032
1033 pub(crate) fn insert_barriers_from_scope(
1034 raw: &mut dyn hal::DynCommandEncoder,
1035 base: &mut Tracker,
1036 head: &UsageScope,
1037 snatch_guard: &SnatchGuard,
1038 ) {
1039 profiling::scope!("insert_barriers");
1040
1041 base.buffers.set_from_usage_scope(&head.buffers);
1042 base.textures.set_from_usage_scope(&head.textures);
1043
1044 Self::drain_barriers(raw, base, snatch_guard);
1045 }
1046
1047 pub(crate) fn drain_barriers(
1048 raw: &mut dyn hal::DynCommandEncoder,
1049 base: &mut Tracker,
1050 snatch_guard: &SnatchGuard,
1051 ) {
1052 profiling::scope!("drain_barriers");
1053
1054 let buffer_barriers = base
1055 .buffers
1056 .drain_transitions(snatch_guard)
1057 .collect::<Vec<_>>();
1058 let (transitions, textures) = base.textures.drain_transitions(snatch_guard);
1059 let texture_barriers = transitions
1060 .into_iter()
1061 .enumerate()
1062 .map(|(i, p)| p.into_hal(textures[i].unwrap().raw()))
1063 .collect::<Vec<_>>();
1064
1065 unsafe {
1066 raw.transition_buffers(&buffer_barriers);
1067 raw.transition_textures(&texture_barriers);
1068 }
1069 }
1070
1071 pub(crate) fn insert_barriers_from_device_tracker(
1072 raw: &mut dyn hal::DynCommandEncoder,
1073 base: &mut DeviceTracker,
1074 head: &Tracker,
1075 snatch_guard: &SnatchGuard,
1076 ) {
1077 profiling::scope!("insert_barriers_from_device_tracker");
1078
1079 let buffer_barriers = base
1080 .buffers
1081 .set_from_tracker_and_drain_transitions(&head.buffers, snatch_guard)
1082 .collect::<Vec<_>>();
1083
1084 let texture_barriers = base
1085 .textures
1086 .set_from_tracker_and_drain_transitions(&head.textures, snatch_guard)
1087 .collect::<Vec<_>>();
1088
1089 unsafe {
1090 raw.transition_buffers(&buffer_barriers);
1091 raw.transition_textures(&texture_barriers);
1092 }
1093 }
1094
1095 fn encode_commands(
1096 device: &Arc<Device>,
1097 cmd_buf_data: &mut CommandBufferMutable,
1098 ) -> Result<(), CommandEncoderError> {
1099 device.check_is_valid()?;
1100 let snatch_guard = device.snatchable_lock.read();
1101 let mut debug_scope_depth = 0;
1102
1103 if cmd_buf_data.encoder.api == EncodingApi::Raw {
1104 assert!(cmd_buf_data.commands.is_empty());
1107 }
1108
1109 let commands = mem::take(&mut cmd_buf_data.commands);
1110
1111 #[cfg(feature = "trace")]
1112 if device.trace.lock().is_some() {
1113 cmd_buf_data.trace_commands = Some(
1114 commands
1115 .iter()
1116 .map(crate::device::trace::IntoTrace::to_trace)
1117 .collect(),
1118 );
1119 }
1120
1121 for command in commands {
1122 if matches!(
1123 command,
1124 ArcCommand::RunRenderPass { .. }
1125 | ArcCommand::RunComputePass { .. }
1126 | ArcCommand::ResolveQuerySet { .. }
1127 | ArcCommand::RunRayTracingPass { .. }
1128 ) {
1129 let mut state = EncodingState {
1135 device,
1136 raw_encoder: &mut cmd_buf_data.encoder,
1137 tracker: &mut cmd_buf_data.trackers,
1138 buffer_memory_init_actions: &mut cmd_buf_data.buffer_memory_init_actions,
1139 texture_memory_actions: &mut cmd_buf_data.texture_memory_actions,
1140 as_actions: &mut cmd_buf_data.as_actions,
1141 temp_resources: &mut cmd_buf_data.temp_resources,
1142 indirect_draw_validation_resources: &mut cmd_buf_data
1143 .indirect_draw_validation_resources,
1144 snatch_guard: &snatch_guard,
1145 debug_scope_depth: &mut debug_scope_depth,
1146 query_set_writes: &mut cmd_buf_data.query_set_writes,
1147 deferred_query_set_resolves: &mut cmd_buf_data.deferred_query_set_resolves,
1148 };
1149
1150 match command {
1151 ArcCommand::RunRenderPass {
1152 pass,
1153 color_attachments,
1154 depth_stencil_attachment,
1155 timestamp_writes,
1156 occlusion_query_set,
1157 multiview_mask,
1158 } => {
1159 api_log!(
1160 "Begin encoding render pass with '{}' label",
1161 pass.label.as_deref().unwrap_or("")
1162 );
1163 let res = render::encode_render_pass(
1164 &mut state,
1165 pass,
1166 color_attachments,
1167 depth_stencil_attachment,
1168 timestamp_writes,
1169 occlusion_query_set,
1170 multiview_mask,
1171 );
1172 match res.as_ref() {
1173 Err(err) => {
1174 api_log!("Finished encoding render pass ({err:?})")
1175 }
1176 Ok(_) => {
1177 api_log!("Finished encoding render pass (success)")
1178 }
1179 }
1180 res?;
1181 }
1182 ArcCommand::RunComputePass {
1183 pass,
1184 timestamp_writes,
1185 } => {
1186 api_log!(
1187 "Begin encoding compute pass with '{}' label",
1188 pass.label.as_deref().unwrap_or("")
1189 );
1190 let res = compute::encode_compute_pass(&mut state, pass, timestamp_writes);
1191 match res.as_ref() {
1192 Err(err) => {
1193 api_log!("Finished encoding compute pass ({err:?})")
1194 }
1195 Ok(_) => {
1196 api_log!("Finished encoding compute pass (success)")
1197 }
1198 }
1199 res?;
1200 }
1201 ArcCommand::ResolveQuerySet {
1202 query_set,
1203 start_query,
1204 query_count,
1205 destination,
1206 destination_offset,
1207 } => {
1208 query::resolve_query_set(
1209 &mut state,
1210 query_set,
1211 start_query,
1212 query_count,
1213 destination,
1214 destination_offset,
1215 )?;
1216 }
1217 ArcCommand::RunRayTracingPass { pass } => {
1218 api_log!(
1219 "Begin encoding ray tracing pass with '{}' label",
1220 pass.label.as_deref().unwrap_or("")
1221 );
1222 let res = ray_tracing_pass::encode_ray_tracing_pass(&mut state, pass);
1223 match res.as_ref() {
1224 Err(err) => {
1225 api_log!("Finished encoding ray tracing pass ({err:?})")
1226 }
1227 Ok(_) => {
1228 api_log!("Finished encoding ray tracing pass (success)")
1229 }
1230 }
1231 res?;
1232 }
1233 _ => unreachable!(),
1234 }
1235 } else {
1236 let raw_encoder = cmd_buf_data.encoder.open_if_closed()?;
1242 let mut state = EncodingState {
1243 device,
1244 raw_encoder,
1245 tracker: &mut cmd_buf_data.trackers,
1246 buffer_memory_init_actions: &mut cmd_buf_data.buffer_memory_init_actions,
1247 texture_memory_actions: &mut cmd_buf_data.texture_memory_actions,
1248 as_actions: &mut cmd_buf_data.as_actions,
1249 temp_resources: &mut cmd_buf_data.temp_resources,
1250 indirect_draw_validation_resources: &mut cmd_buf_data
1251 .indirect_draw_validation_resources,
1252 snatch_guard: &snatch_guard,
1253 debug_scope_depth: &mut debug_scope_depth,
1254 query_set_writes: &mut cmd_buf_data.query_set_writes,
1255 deferred_query_set_resolves: &mut cmd_buf_data.deferred_query_set_resolves,
1256 };
1257 match command {
1258 ArcCommand::CopyBufferToBuffer {
1259 src,
1260 src_offset,
1261 dst,
1262 dst_offset,
1263 size,
1264 } => {
1265 transfer::copy_buffer_to_buffer(
1266 &mut state, &src, src_offset, &dst, dst_offset, size,
1267 )?;
1268 }
1269 ArcCommand::CopyBufferToTexture { src, dst, size } => {
1270 transfer::copy_buffer_to_texture(&mut state, &src, &dst, &size)?;
1271 }
1272 ArcCommand::CopyTextureToBuffer { src, dst, size } => {
1273 transfer::copy_texture_to_buffer(&mut state, &src, &dst, &size)?;
1274 }
1275 ArcCommand::CopyTextureToTexture { src, dst, size } => {
1276 transfer::copy_texture_to_texture(&mut state, &src, &dst, &size)?;
1277 }
1278 ArcCommand::ClearBuffer { dst, offset, size } => {
1279 clear::clear_buffer(&mut state, dst, offset, size)?;
1280 }
1281 ArcCommand::ClearTexture {
1282 dst,
1283 subresource_range,
1284 } => {
1285 clear::clear_texture_cmd(&mut state, dst, &subresource_range)?;
1286 }
1287 ArcCommand::WriteTimestamp {
1288 query_set,
1289 query_index,
1290 } => {
1291 query::write_timestamp(&mut state, query_set, query_index)?;
1292 }
1293 ArcCommand::PushDebugGroup(label) => {
1294 push_debug_group(&mut state, &label)?;
1295 }
1296 ArcCommand::PopDebugGroup => {
1297 pop_debug_group(&mut state)?;
1298 }
1299 ArcCommand::InsertDebugMarker(label) => {
1300 insert_debug_marker(&mut state, &label)?;
1301 }
1302 ArcCommand::BuildAccelerationStructures { blas, tlas } => {
1303 ray_tracing::build_acceleration_structures(&mut state, blas, tlas)?;
1304 }
1305 ArcCommand::TransitionResources {
1306 buffer_transitions,
1307 texture_transitions,
1308 } => {
1309 transition_resources::transition_resources(
1310 &mut state,
1311 buffer_transitions,
1312 texture_transitions,
1313 )?;
1314 }
1315 ArcCommand::RunComputePass { .. }
1316 | ArcCommand::RunRenderPass { .. }
1317 | ArcCommand::ResolveQuerySet { .. }
1318 | ArcCommand::RunRayTracingPass { .. } => {
1319 unreachable!()
1320 }
1321 }
1322 }
1323 }
1324
1325 if debug_scope_depth > 0 {
1326 Err(CommandEncoderError::DebugGroupError(
1327 DebugGroupError::MissingPop,
1328 ))?;
1329 }
1330
1331 cmd_buf_data.encoder.close_if_open()?;
1333
1334 Ok(())
1338 }
1339
1340 fn finish_inner(
1343 self: &Arc<Self>,
1344 desc: &wgt::CommandBufferDescriptor<Label>,
1345 ) -> (Arc<CommandBuffer>, Option<CommandEncoderError>) {
1346 profiling::scope!("CommandEncoder::finish");
1347
1348 let status = self.data.lock().finish();
1349
1350 let res = match status {
1351 CommandEncoderStatus::Finished(mut cmd_buf_data) => {
1352 match Self::encode_commands(&self.device, &mut cmd_buf_data) {
1353 Ok(()) => Ok(cmd_buf_data),
1354 Err(error) => Err(EncoderErrorState {
1355 error,
1356 #[cfg(feature = "trace")]
1357 trace_commands: mem::take(&mut cmd_buf_data.trace_commands),
1358 }),
1359 }
1360 }
1361 CommandEncoderStatus::Error(error_state) => Err(error_state),
1362 _ => unreachable!(),
1363 };
1364
1365 let (data, error) = match res {
1366 Err(EncoderErrorState {
1367 error,
1368 #[cfg(feature = "trace")]
1369 trace_commands,
1370 }) => {
1371 #[cfg(feature = "trace")]
1375 if let Some(trace) = self.device.trace.lock().as_mut() {
1376 use alloc::string::ToString;
1377
1378 trace.add(crate::device::trace::Action::FailedCommands {
1379 commands: trace_commands,
1380 failed_at_submit: None,
1381 error: error.to_string(),
1382 });
1383 }
1384
1385 if error.is_destroyed_error() {
1386 (make_error_state(error), None)
1389 } else {
1390 (make_error_state(error.clone()), Some(error))
1391 }
1392 }
1393
1394 Ok(data) => (CommandEncoderStatus::Finished(data), None),
1395 };
1396
1397 let cmd_buf = Arc::new(CommandBuffer {
1398 device: self.device.clone(),
1399 label: desc.label.to_string(),
1400 data: Mutex::new(rank::COMMAND_BUFFER_DATA, data),
1401 });
1402
1403 (cmd_buf, error)
1404 }
1405
1406 pub fn finish(
1407 self: &Arc<Self>,
1408 desc: &wgt::CommandBufferDescriptor<Label>,
1409 ) -> Arc<CommandBuffer> {
1410 let (cmd_buf, error) = self.finish_inner(desc);
1411 if let Some(error) = error {
1412 self.device
1413 .handle_error(error, Some(self.label()), "CommandEncoder::finish");
1414 }
1415 cmd_buf
1416 }
1417}
1418
1419impl CommandBuffer {
1420 #[doc(hidden)]
1426 pub fn from_trace(device: &Arc<Device>, commands: Vec<Command<ArcReferences>>) -> Arc<Self> {
1427 let encoder = device.create_command_encoder(&wgt::CommandEncoderDescriptor { label: None });
1428 let mut cmd_enc_status = encoder.data.lock();
1429 cmd_enc_status.replay(commands);
1430 drop(cmd_enc_status);
1431
1432 encoder.finish(&wgt::CommandBufferDescriptor { label: None })
1433 }
1434
1435 pub fn take_finished(&self) -> Result<CommandBufferMutable, CommandEncoderError> {
1436 use CommandEncoderStatus as St;
1437 match mem::replace(
1438 &mut *self.data.lock(),
1439 make_error_state(EncoderStateError::Submitted),
1440 ) {
1441 St::Finished(command_buffer_mutable) => Ok(command_buffer_mutable),
1442 St::Error(EncoderErrorState {
1443 #[cfg(feature = "trace")]
1444 trace_commands: _,
1445 error,
1446 }) => Err(error),
1447 St::Recording(_) | St::Locked(_) | St::Consumed | St::Transitioning => unreachable!(),
1448 }
1449 }
1450}
1451
1452crate::impl_resource_type!(CommandBuffer);
1453crate::impl_labeled!(CommandBuffer);
1454crate::impl_parent_device!(CommandBuffer);
1455crate::impl_storage_item!(CommandBuffer);
1456
1457#[doc(hidden)]
1469#[derive(Debug, Clone)]
1470#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
1471pub struct BasePass<C, E> {
1472 pub label: Option<String>,
1473
1474 #[cfg_attr(feature = "serde", serde(skip, default = "Option::default"))]
1482 pub error: Option<E>,
1483
1484 pub commands: Vec<C>,
1490
1491 pub dynamic_offsets: Vec<wgt::DynamicOffset>,
1496
1497 pub string_data: Vec<u8>,
1502}
1503
1504impl<C: Clone, E: Clone> BasePass<C, E> {
1505 fn new(label: &Label) -> Self {
1506 Self {
1507 label: label.as_deref().map(str::to_owned),
1508 error: None,
1509 commands: Vec::new(),
1510 dynamic_offsets: Vec::new(),
1511 string_data: Vec::new(),
1512 }
1513 }
1514
1515 fn new_invalid(label: &Label, err: E) -> Self {
1516 Self {
1517 label: label.as_deref().map(str::to_owned),
1518 error: Some(err),
1519 commands: Vec::new(),
1520 dynamic_offsets: Vec::new(),
1521 string_data: Vec::new(),
1522 }
1523 }
1524
1525 fn take(&mut self) -> Result<BasePass<C, Infallible>, E> {
1532 match self.error.as_ref() {
1533 Some(err) => Err(err.clone()),
1534 None => Ok(BasePass {
1535 label: self.label.clone(),
1536 error: None,
1537 commands: mem::take(&mut self.commands),
1538 dynamic_offsets: mem::take(&mut self.dynamic_offsets),
1539 string_data: mem::take(&mut self.string_data),
1540 }),
1541 }
1542 }
1543}
1544
1545macro_rules! pass_base {
1568 ($pass:expr, $scope:expr $(,)?) => {
1569 match (&$pass.parent, &$pass.base.error) {
1570 (&None, _) => return Err(EncoderStateError::Ended).map_pass_err($scope),
1572 (&Some(_), &Some(_)) => return Ok(()),
1574 (&Some(_), &None) => &mut $pass.base,
1576 }
1577 };
1578}
1579pub(crate) use pass_base;
1580
1581macro_rules! pass_try {
1593 ($base:expr, $scope:expr, $res:expr $(,)?) => {
1594 match $res.map_pass_err($scope) {
1595 Ok(val) => val,
1596 Err(err) => {
1597 $base.error.get_or_insert(err);
1598 return Ok(());
1599 }
1600 }
1601 };
1602}
1603pub(crate) use pass_try;
1604
1605#[derive(Clone, Debug, Error)]
1610#[non_exhaustive]
1611pub enum EncoderStateError {
1612 #[error("Encoder is invalid")]
1617 Invalid,
1618
1619 #[error("Encoding must not have ended")]
1622 Ended,
1623
1624 #[error("Encoder is locked by a previously created render/compute pass. Before recording any new commands, the pass must be ended.")]
1630 Locked,
1631
1632 #[error(
1635 "Encoder is not currently locked. A pass can only be ended while the encoder is locked."
1636 )]
1637 Unlocked,
1638
1639 #[error("This command buffer has already been submitted.")]
1644 Submitted,
1645}
1646
1647impl WebGpuError for EncoderStateError {
1648 fn webgpu_error_type(&self) -> ErrorType {
1649 match self {
1650 EncoderStateError::Invalid
1651 | EncoderStateError::Ended
1652 | EncoderStateError::Locked
1653 | EncoderStateError::Unlocked
1654 | EncoderStateError::Submitted => ErrorType::Validation,
1655 }
1656 }
1657}
1658
1659#[derive(Clone, Debug, Error)]
1660#[non_exhaustive]
1661pub enum CommandEncoderError {
1662 #[error(transparent)]
1663 State(#[from] EncoderStateError),
1664 #[error(transparent)]
1665 Device(#[from] DeviceError),
1666 #[error(transparent)]
1667 InvalidResource(#[from] InvalidResourceError),
1668 #[error(transparent)]
1669 DestroyedResource(#[from] DestroyedResourceError),
1670 #[error(transparent)]
1671 ResourceUsage(#[from] ResourceUsageCompatibilityError),
1672 #[error(transparent)]
1673 DebugGroupError(#[from] DebugGroupError),
1674 #[error(transparent)]
1675 MissingFeatures(#[from] MissingFeatures),
1676 #[error(transparent)]
1677 Transfer(#[from] TransferError),
1678 #[error(transparent)]
1679 Clear(#[from] ClearError),
1680 #[error(transparent)]
1681 Query(#[from] QueryError),
1682 #[error(transparent)]
1683 BuildAccelerationStructure(#[from] BuildAccelerationStructureError),
1684 #[error(transparent)]
1685 TransitionResources(#[from] TransitionResourcesError),
1686 #[error(transparent)]
1687 ComputePass(#[from] ComputePassError),
1688 #[error(transparent)]
1689 RenderPass(#[from] RenderPassError),
1690 #[error(transparent)]
1691 RayTracingPass(#[from] RayTracingPassError),
1692}
1693
1694impl From<InvalidOrDestroyedResourceError> for CommandEncoderError {
1695 fn from(err: InvalidOrDestroyedResourceError) -> Self {
1696 match err {
1697 InvalidOrDestroyedResourceError::InvalidResource(e) => Self::InvalidResource(e),
1698 InvalidOrDestroyedResourceError::DestroyedResource(e) => Self::DestroyedResource(e),
1699 }
1700 }
1701}
1702
1703impl CommandEncoderError {
1704 fn is_destroyed_error(&self) -> bool {
1705 matches!(
1706 self,
1707 Self::DestroyedResource(_)
1708 | Self::Clear(ClearError::DestroyedResource(_))
1709 | Self::Query(QueryError::DestroyedResource(_))
1710 | Self::ComputePass(ComputePassError {
1711 inner: ComputePassErrorInner::DestroyedResource(_),
1712 ..
1713 })
1714 ) || if let Self::RenderPass(pass_error) = self {
1715 matches!(
1716 pass_error.inner.as_ref(),
1717 RenderPassErrorInner::DestroyedResource(_)
1718 | RenderPassErrorInner::RenderCommand(RenderCommandError::DestroyedResource(_))
1719 | RenderPassErrorInner::RenderCommand(RenderCommandError::BindingError(
1720 BindingError::DestroyedResource(_),
1721 ))
1722 )
1723 } else {
1724 false
1725 }
1726 }
1727}
1728
1729impl WebGpuError for CommandEncoderError {
1730 fn webgpu_error_type(&self) -> ErrorType {
1731 match self {
1732 Self::Device(e) => e.webgpu_error_type(),
1733 Self::InvalidResource(e) => e.webgpu_error_type(),
1734 Self::DebugGroupError(e) => e.webgpu_error_type(),
1735 Self::MissingFeatures(e) => e.webgpu_error_type(),
1736 Self::State(e) => e.webgpu_error_type(),
1737 Self::DestroyedResource(e) => e.webgpu_error_type(),
1738 Self::Transfer(e) => e.webgpu_error_type(),
1739 Self::Clear(e) => e.webgpu_error_type(),
1740 Self::Query(e) => e.webgpu_error_type(),
1741 Self::BuildAccelerationStructure(e) => e.webgpu_error_type(),
1742 Self::TransitionResources(e) => e.webgpu_error_type(),
1743 Self::ResourceUsage(e) => e.webgpu_error_type(),
1744 Self::ComputePass(e) => e.webgpu_error_type(),
1745 Self::RenderPass(e) => e.webgpu_error_type(),
1746 Self::RayTracingPass(e) => e.webgpu_error_type(),
1747 }
1748 }
1749}
1750
1751#[derive(Clone, Debug, Error)]
1752#[non_exhaustive]
1753pub enum DebugGroupError {
1754 #[error("Cannot pop debug group, because number of pushed debug groups is zero")]
1755 InvalidPop,
1756 #[error("A debug group was not popped before the encoder was finished")]
1757 MissingPop,
1758}
1759
1760impl WebGpuError for DebugGroupError {
1761 fn webgpu_error_type(&self) -> ErrorType {
1762 match self {
1763 Self::InvalidPop | Self::MissingPop => ErrorType::Validation,
1764 }
1765 }
1766}
1767
1768#[derive(Clone, Debug, Error)]
1769#[non_exhaustive]
1770pub enum TimestampWritesError {
1771 #[error(
1772 "begin and end indices of pass timestamp writes are both set to {idx}, which is not allowed"
1773 )]
1774 IndicesEqual { idx: u32 },
1775 #[error("no begin or end indices were specified for pass timestamp writes, expected at least one to be set")]
1776 IndicesMissing,
1777}
1778
1779impl WebGpuError for TimestampWritesError {
1780 fn webgpu_error_type(&self) -> ErrorType {
1781 match self {
1782 Self::IndicesEqual { .. } | Self::IndicesMissing => ErrorType::Validation,
1783 }
1784 }
1785}
1786
1787impl CommandEncoder {
1788 fn push_debug_group_inner(self: &Arc<Self>, label: &str) -> Result<(), EncoderStateError> {
1789 profiling::scope!("CommandEncoder::push_debug_group");
1790 api_log!("CommandEncoder::push_debug_group {label}");
1791
1792 let mut cmd_buf_data = self.data.lock();
1793
1794 cmd_buf_data.push_with(|| -> Result<_, CommandEncoderError> {
1795 Ok(ArcCommand::PushDebugGroup(label.to_owned()))
1796 })
1797 }
1798
1799 pub fn push_debug_group(self: &Arc<Self>, label: &str) {
1800 if let Err(err) = self.push_debug_group_inner(label) {
1801 self.device
1802 .handle_error(err, Some(self.label()), "CommandEncoder::push_debug_group");
1803 }
1804 }
1805
1806 fn insert_debug_marker_inner(self: &Arc<Self>, label: &str) -> Result<(), EncoderStateError> {
1807 profiling::scope!("CommandEncoder::insert_debug_marker");
1808 api_log!("CommandEncoder::insert_debug_marker {label}");
1809
1810 let mut cmd_buf_data = self.data.lock();
1811
1812 cmd_buf_data.push_with(|| -> Result<_, CommandEncoderError> {
1813 Ok(ArcCommand::InsertDebugMarker(label.to_owned()))
1814 })
1815 }
1816
1817 pub fn insert_debug_marker(self: &Arc<Self>, label: &str) {
1818 if let Err(err) = self.insert_debug_marker_inner(label) {
1819 self.device.handle_error(
1820 err,
1821 Some(self.label()),
1822 "CommandEncoder::insert_debug_marker",
1823 );
1824 }
1825 }
1826
1827 fn pop_debug_group_inner(self: &Arc<Self>) -> Result<(), EncoderStateError> {
1828 profiling::scope!("CommandEncoder::pop_debug_marker");
1829 api_log!("CommandEncoder::pop_debug_group");
1830
1831 let mut cmd_buf_data = self.data.lock();
1832
1833 cmd_buf_data
1834 .push_with(|| -> Result<_, CommandEncoderError> { Ok(ArcCommand::PopDebugGroup) })
1835 }
1836
1837 pub fn pop_debug_group(self: &Arc<Self>) {
1838 if let Err(err) = self.pop_debug_group_inner() {
1839 self.device
1840 .handle_error(err, Some(self.label()), "CommandEncoder::pop_debug_group");
1841 }
1842 }
1843}
1844
1845pub(crate) fn push_debug_group(
1846 state: &mut EncodingState,
1847 label: &str,
1848) -> Result<(), CommandEncoderError> {
1849 *state.debug_scope_depth += 1;
1850
1851 if !state
1852 .device
1853 .instance_flags
1854 .contains(wgt::InstanceFlags::DISCARD_HAL_LABELS)
1855 {
1856 unsafe { state.raw_encoder.begin_debug_marker(label) };
1857 }
1858
1859 Ok(())
1860}
1861
1862pub(crate) fn insert_debug_marker(
1863 state: &mut EncodingState,
1864 label: &str,
1865) -> Result<(), CommandEncoderError> {
1866 if !state
1867 .device
1868 .instance_flags
1869 .contains(wgt::InstanceFlags::DISCARD_HAL_LABELS)
1870 {
1871 unsafe { state.raw_encoder.insert_debug_marker(label) };
1872 }
1873
1874 Ok(())
1875}
1876
1877pub(crate) fn pop_debug_group(state: &mut EncodingState) -> Result<(), CommandEncoderError> {
1878 if *state.debug_scope_depth == 0 {
1879 return Err(DebugGroupError::InvalidPop.into());
1880 }
1881 *state.debug_scope_depth -= 1;
1882
1883 if !state
1884 .device
1885 .instance_flags
1886 .contains(wgt::InstanceFlags::DISCARD_HAL_LABELS)
1887 {
1888 unsafe { state.raw_encoder.end_debug_marker() };
1889 }
1890
1891 Ok(())
1892}
1893
1894#[derive(Debug, Copy, Clone)]
1895struct StateChange<T> {
1896 last_state: Option<T>,
1897}
1898
1899impl<T: Clone + PartialEq_> StateChange<T> {
1900 const fn new() -> Self {
1901 Self { last_state: None }
1902 }
1903
1904 fn set_and_check_redundant(&mut self, new_state: &T) -> bool {
1905 let already_set = self.last_state.as_ref().is_some_and(|s| s.eq(new_state));
1906 if !already_set {
1907 self.last_state = Some(new_state.clone());
1908 }
1909 already_set
1910 }
1911
1912 fn reset(&mut self) {
1913 self.last_state = None;
1914 }
1915}
1916
1917impl<T: Clone + PartialEq_> Default for StateChange<T> {
1918 fn default() -> Self {
1919 Self::new()
1920 }
1921}
1922
1923trait PartialEq_ {
1924 fn eq(&self, other: &Self) -> bool;
1925}
1926
1927impl<T> PartialEq_ for Arc<T> {
1928 fn eq(&self, other: &Self) -> bool {
1929 Arc::ptr_eq(self, other)
1930 }
1931}
1932
1933impl<T: PartialEq_> PartialEq_ for Option<T> {
1934 fn eq(&self, other: &Self) -> bool {
1935 match (self, other) {
1936 (Some(a), Some(b)) => a.eq(b),
1937 (None, None) => true,
1938 _ => false,
1939 }
1940 }
1941}
1942
1943#[derive(Debug)]
1944struct BindGroupStateChange {
1945 last_states: [StateChange<Option<Arc<BindGroup>>>; hal::MAX_BIND_GROUPS],
1946}
1947
1948impl BindGroupStateChange {
1949 fn new() -> Self {
1950 Self {
1951 last_states: [const { StateChange::new() }; hal::MAX_BIND_GROUPS],
1952 }
1953 }
1954
1955 fn set_and_check_redundant(
1956 &mut self,
1957 bind_group: &Option<Arc<BindGroup>>,
1958 index: u32,
1959 dynamic_offsets: &mut Vec<u32>,
1960 offsets: &[wgt::DynamicOffset],
1961 ) -> bool {
1962 if offsets.is_empty() {
1964 if let Some(current_bind_group) = self.last_states.get_mut(index as usize) {
1967 if current_bind_group.set_and_check_redundant(bind_group) {
1969 return true;
1970 }
1971 }
1972 } else {
1973 if let Some(current_bind_group) = self.last_states.get_mut(index as usize) {
1977 current_bind_group.reset();
1978 }
1979 dynamic_offsets.extend_from_slice(offsets);
1980 }
1981 false
1982 }
1983 fn reset(&mut self) {
1984 self.last_states = [const { StateChange::new() }; hal::MAX_BIND_GROUPS];
1985 }
1986}
1987
1988impl Default for BindGroupStateChange {
1989 fn default() -> Self {
1990 Self::new()
1991 }
1992}
1993
1994trait MapPassErr<T> {
1996 fn map_pass_err(self, scope: PassErrorScope) -> T;
1997}
1998
1999impl<T, E, F> MapPassErr<Result<T, F>> for Result<T, E>
2000where
2001 E: MapPassErr<F>,
2002{
2003 fn map_pass_err(self, scope: PassErrorScope) -> Result<T, F> {
2004 self.map_err(|err| err.map_pass_err(scope))
2005 }
2006}
2007
2008impl MapPassErr<PassStateError> for EncoderStateError {
2009 fn map_pass_err(self, scope: PassErrorScope) -> PassStateError {
2010 PassStateError { scope, inner: self }
2011 }
2012}
2013
2014#[derive(Clone, Copy, Debug)]
2015pub enum DrawKind {
2016 Draw,
2017 DrawIndirect,
2018 MultiDrawIndirect,
2019 MultiDrawIndirectCount,
2020}
2021
2022#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
2024#[derive(Clone, Copy, Debug, PartialEq, Eq)]
2025pub enum DrawCommandFamily {
2026 Draw,
2027 DrawIndexed,
2028 DrawMeshTasks,
2029}
2030
2031#[derive(Clone, Copy, Debug, Error)]
2041pub enum PassErrorScope {
2042 #[error("In a bundle parameter")]
2045 Bundle,
2046 #[error("In a pass parameter")]
2047 Pass,
2048 #[error("In a set_bind_group command")]
2049 SetBindGroup,
2050 #[error("In a set_pipeline command")]
2051 SetPipelineRender,
2052 #[error("In a set_pipeline command")]
2053 SetPipelineCompute,
2054 #[error("In a set_pipeline command")]
2055 SetPipelineRayTracing,
2056 #[error("In a set_immediates command")]
2057 SetImmediate,
2058 #[error("In a set_vertex_buffer command")]
2059 SetVertexBuffer,
2060 #[error("In a set_index_buffer command")]
2061 SetIndexBuffer,
2062 #[error("In a set_blend_constant command")]
2063 SetBlendConstant,
2064 #[error("In a set_stencil_reference command")]
2065 SetStencilReference,
2066 #[error("In a set_viewport command")]
2067 SetViewport,
2068 #[error("In a set_scissor_rect command")]
2069 SetScissorRect,
2070 #[error("In a draw command, kind: {kind:?}")]
2071 Draw {
2072 kind: DrawKind,
2073 family: DrawCommandFamily,
2074 },
2075 #[error("In a write_timestamp command")]
2076 WriteTimestamp,
2077 #[error("In a begin_occlusion_query command")]
2078 BeginOcclusionQuery,
2079 #[error("In a end_occlusion_query command")]
2080 EndOcclusionQuery,
2081 #[error("In a begin_pipeline_statistics_query command")]
2082 BeginPipelineStatisticsQuery,
2083 #[error("In a end_pipeline_statistics_query command")]
2084 EndPipelineStatisticsQuery,
2085 #[error("In a transition_resources command")]
2086 TransitionResources,
2087 #[error("In a execute_bundle command")]
2088 ExecuteBundle,
2089 #[error("In a dispatch command, indirect:{indirect}")]
2090 Dispatch { indirect: bool },
2091 #[error("In a push_debug_group command")]
2092 PushDebugGroup,
2093 #[error("In a pop_debug_group command")]
2094 PopDebugGroup,
2095 #[error("In a insert_debug_marker command")]
2096 InsertDebugMarker,
2097 #[error("In a trace rays command")]
2098 TraceRays,
2099}
2100
2101#[derive(Clone, Debug, Error)]
2103#[error("{scope}")]
2104pub struct PassStateError {
2105 pub scope: PassErrorScope,
2106 #[source]
2107 pub(super) inner: EncoderStateError,
2108}
2109
2110impl WebGpuError for PassStateError {
2111 fn webgpu_error_type(&self) -> ErrorType {
2112 let Self { scope: _, inner } = self;
2113 inner.webgpu_error_type()
2114 }
2115}