1use alloc::{borrow::Cow, string::ToString, sync::Arc, vec::Vec};
2use core::{any::Any, convert::Infallible, marker::PhantomData};
3use std::io::Write as _;
4
5use crate::{
6 command::{
7 ArcCommand, ArcComputeCommand, ArcRayTracingCommand, ArcReferences, ArcRenderCommand,
8 BasePass, ColorAttachments, Command, ComputeCommand, PassTimestampWrites,
9 PointerReferences, RayTracingCommand, RenderCommand, RenderPassColorAttachment,
10 ResolvedRenderPassDepthStencilAttachment,
11 },
12 device::trace::{Data, DataKind, TraceRayTracingPipelineDescriptor},
13 id::{markers, PointerId},
14 storage::StorageItem,
15};
16
17use super::{
18 Action, TraceBindGroupDescriptor, TraceComputePipelineDescriptor,
19 TraceGeneralRenderPipelineDescriptor, FILE_NAME,
20};
21
22pub(crate) fn new_render_bundle_encoder_descriptor(
23 label: crate::Label<'_>,
24 context: &crate::device::RenderPassContext,
25 depth_read_only: bool,
26 stencil_read_only: bool,
27) -> crate::command::RenderBundleEncoderDescriptor<'static> {
28 crate::command::RenderBundleEncoderDescriptor {
29 label: label.map(|l| Cow::from(l.to_string())),
30 color_formats: Cow::from(context.attachments.colors.to_vec()),
31 depth_stencil: context.attachments.depth_stencil.map(|format| {
32 wgt::RenderBundleDepthStencil {
33 format,
34 depth_read_only,
35 stencil_read_only,
36 }
37 }),
38 sample_count: context.sample_count,
39 multiview: context.multiview_mask,
40 }
41}
42
43pub trait Trace: Any + Send + Sync {
44 fn make_binary(&mut self, kind: DataKind, data: &[u8]) -> Data;
45
46 fn make_string(&mut self, kind: DataKind, data: &str) -> Data;
47
48 fn add(&mut self, action: Action<'_, PointerReferences>)
49 where
50 for<'a> Action<'a, PointerReferences>: serde::Serialize;
51}
52
53#[derive(Debug)]
54pub struct DiskTrace {
55 path: std::path::PathBuf,
56 file: std::fs::File,
57 config: ron::ser::PrettyConfig,
58 data_id: usize,
59}
60
61impl DiskTrace {
62 pub fn new(path: std::path::PathBuf) -> Result<Self, std::io::Error> {
63 log::debug!("Tracing into '{path:?}'");
64 let mut file = std::fs::File::create(path.join(FILE_NAME))?;
65 file.write_all(b"[\n")?;
66 Ok(Self {
67 path,
68 file,
69 config: ron::ser::PrettyConfig::default(),
70 data_id: 0,
71 })
72 }
73}
74
75impl Trace for DiskTrace {
76 fn make_binary(&mut self, kind: DataKind, data: &[u8]) -> Data {
81 self.data_id += 1;
82 let name = std::format!("data{}.{}", self.data_id, kind);
83 let _ = std::fs::write(self.path.join(&name), data);
84 Data::File(name)
85 }
86
87 fn make_string(&mut self, kind: DataKind, data: &str) -> Data {
92 self.make_binary(kind, data.as_bytes())
93 }
94
95 fn add(&mut self, action: Action<'_, PointerReferences>)
96 where
97 for<'a> Action<'a, PointerReferences>: serde::Serialize,
98 {
99 match ron::ser::to_string_pretty(&action, self.config.clone()) {
100 Ok(string) => {
101 let _ = writeln!(self.file, "{string},");
102 }
103 Err(e) => {
104 log::warn!("RON serialization failure: {e:?}");
105 }
106 }
107 }
108}
109
110impl Drop for DiskTrace {
111 fn drop(&mut self) {
112 let _ = self.file.write_all(b"]");
113 }
114}
115
116#[derive(Default)]
117pub struct MemoryTrace {
118 actions: Vec<Action<'static, PointerReferences>>,
119}
120
121impl MemoryTrace {
122 pub fn new() -> Self {
123 Self::default()
124 }
125
126 pub fn actions(&self) -> &[Action<'static, PointerReferences>] {
127 &self.actions
128 }
129}
130
131impl Trace for MemoryTrace {
132 fn make_binary(&mut self, kind: DataKind, data: &[u8]) -> Data {
137 Data::Binary(kind, data.to_vec())
138 }
139
140 fn make_string(&mut self, kind: DataKind, data: &str) -> Data {
145 Data::String(kind, data.to_string())
146 }
147
148 fn add(&mut self, action: Action<'_, PointerReferences>)
149 where
150 for<'a> Action<'a, PointerReferences>: serde::Serialize,
151 {
152 self.actions.push(action_to_owned(action))
153 }
154}
155
156pub(crate) trait IntoTrace {
157 type Output;
158 fn into_trace(self) -> Self::Output;
159
160 fn to_trace(&self) -> Self::Output
161 where
162 Self: Sized + Clone,
163 {
164 self.clone().into_trace()
165 }
166}
167
168impl<T: StorageItem> IntoTrace for Arc<T> {
169 type Output = PointerId<T::Marker>;
170 fn into_trace(self) -> Self::Output {
171 self.to_trace()
172 }
173
174 fn to_trace(&self) -> Self::Output {
175 PointerId::from(self)
176 }
177}
178
179pub(crate) unsafe fn to_trace<T: StorageItem>(t: &T) -> PointerId<T::Marker> {
181 PointerId::PointerId(
182 #[expect(trivial_casts)]
183 core::num::NonZeroUsize::new(t as *const T as usize).unwrap(),
184 PhantomData,
185 )
186}
187
188impl IntoTrace for ArcCommand {
189 type Output = Command<PointerReferences>;
190 fn into_trace(self) -> Self::Output {
191 match self {
192 ArcCommand::CopyBufferToBuffer {
193 src,
194 src_offset,
195 dst,
196 dst_offset,
197 size,
198 } => Command::CopyBufferToBuffer {
199 src: src.to_trace(),
200 src_offset,
201 dst: dst.to_trace(),
202 dst_offset,
203 size,
204 },
205 ArcCommand::CopyBufferToTexture { src, dst, size } => Command::CopyBufferToTexture {
206 src: src.into_trace(),
207 dst: dst.into_trace(),
208 size,
209 },
210 ArcCommand::CopyTextureToBuffer { src, dst, size } => Command::CopyTextureToBuffer {
211 src: src.into_trace(),
212 dst: dst.into_trace(),
213 size,
214 },
215 ArcCommand::CopyTextureToTexture { src, dst, size } => Command::CopyTextureToTexture {
216 src: src.into_trace(),
217 dst: dst.into_trace(),
218 size,
219 },
220 ArcCommand::ClearBuffer { dst, offset, size } => Command::ClearBuffer {
221 dst: dst.to_trace(),
222 offset,
223 size,
224 },
225 ArcCommand::ClearTexture {
226 dst,
227 subresource_range,
228 } => Command::ClearTexture {
229 dst: dst.to_trace(),
230 subresource_range,
231 },
232 ArcCommand::WriteTimestamp {
233 query_set,
234 query_index,
235 } => Command::WriteTimestamp {
236 query_set: query_set.to_trace(),
237 query_index,
238 },
239 ArcCommand::ResolveQuerySet {
240 query_set,
241 start_query,
242 query_count,
243 destination,
244 destination_offset,
245 } => Command::ResolveQuerySet {
246 query_set: query_set.to_trace(),
247 start_query,
248 query_count,
249 destination: destination.to_trace(),
250 destination_offset,
251 },
252 ArcCommand::PushDebugGroup(label) => Command::PushDebugGroup(label),
253 ArcCommand::PopDebugGroup => Command::PopDebugGroup,
254 ArcCommand::InsertDebugMarker(label) => Command::InsertDebugMarker(label),
255 ArcCommand::RunComputePass {
256 pass,
257 timestamp_writes,
258 } => Command::RunComputePass {
259 pass: pass.into_trace(),
260 timestamp_writes: timestamp_writes.map(|tw| tw.into_trace()),
261 },
262 ArcCommand::RunRenderPass {
263 pass,
264 color_attachments,
265 depth_stencil_attachment,
266 timestamp_writes,
267 occlusion_query_set,
268 multiview_mask,
269 } => Command::RunRenderPass {
270 pass: pass.into_trace(),
271 color_attachments: color_attachments.into_trace(),
272 depth_stencil_attachment: depth_stencil_attachment.map(|d| d.into_trace()),
273 timestamp_writes: timestamp_writes.map(|tw| tw.into_trace()),
274 occlusion_query_set: occlusion_query_set.map(|q| q.to_trace()),
275 multiview_mask,
276 },
277 ArcCommand::RunRayTracingPass { pass } => Command::RunRayTracingPass {
278 pass: pass.into_trace(),
279 },
280 ArcCommand::BuildAccelerationStructures { blas, tlas } => {
281 Command::BuildAccelerationStructures {
282 blas: blas.into_iter().map(|b| b.into_trace()).collect(),
283 tlas: tlas.into_iter().map(|b| b.into_trace()).collect(),
284 }
285 }
286 ArcCommand::TransitionResources {
287 buffer_transitions: _,
288 texture_transitions: _,
289 } => {
290 panic!("TransitionResources cannot be converted to Command");
293 }
294 }
295 }
296}
297
298impl<T: IntoTrace> IntoTrace for wgt::TexelCopyBufferInfo<T> {
299 type Output = wgt::TexelCopyBufferInfo<T::Output>;
300 fn into_trace(self) -> Self::Output {
301 wgt::TexelCopyBufferInfo {
302 buffer: self.buffer.into_trace(),
303 layout: self.layout,
304 }
305 }
306}
307
308impl<T: IntoTrace> IntoTrace for wgt::TexelCopyTextureInfo<T> {
309 type Output = wgt::TexelCopyTextureInfo<T::Output>;
310 fn into_trace(self) -> Self::Output {
311 wgt::TexelCopyTextureInfo {
312 texture: self.texture.into_trace(),
313 mip_level: self.mip_level,
314 origin: self.origin,
315 aspect: self.aspect,
316 }
317 }
318}
319
320impl IntoTrace for PassTimestampWrites {
321 type Output = PassTimestampWrites<PointerId<markers::QuerySet>>;
322 fn into_trace(self) -> Self::Output {
323 PassTimestampWrites {
324 query_set: self.query_set.into_trace(),
325 beginning_of_pass_write_index: self.beginning_of_pass_write_index,
326 end_of_pass_write_index: self.end_of_pass_write_index,
327 }
328 }
329}
330
331impl IntoTrace for ColorAttachments {
332 type Output = ColorAttachments<PointerId<markers::TextureView>>;
333 fn into_trace(self) -> Self::Output {
334 self.into_iter()
335 .map(|opt| {
336 opt.map(|att| RenderPassColorAttachment {
337 view: att.view.into_trace(),
338 depth_slice: att.depth_slice,
339 resolve_target: att.resolve_target.map(|r| r.into_trace()),
340 load_op: att.load_op,
341 store_op: att.store_op,
342 })
343 })
344 .collect()
345 }
346}
347
348impl<TV: IntoTrace> IntoTrace for ResolvedRenderPassDepthStencilAttachment<TV> {
349 type Output = ResolvedRenderPassDepthStencilAttachment<TV::Output>;
350 fn into_trace(self) -> Self::Output {
351 ResolvedRenderPassDepthStencilAttachment {
352 view: self.view.into_trace(),
353 depth: self.depth,
354 stencil: self.stencil,
355 }
356 }
357}
358
359impl IntoTrace for crate::ray_tracing::OwnedBlasBuildEntry<ArcReferences> {
360 type Output = crate::ray_tracing::OwnedBlasBuildEntry<PointerReferences>;
361 fn into_trace(self) -> Self::Output {
362 crate::ray_tracing::OwnedBlasBuildEntry {
363 blas: self.blas.into_trace(),
364 geometries: self.geometries.into_trace(),
365 }
366 }
367}
368
369impl IntoTrace for crate::ray_tracing::OwnedBlasGeometries<ArcReferences> {
370 type Output = crate::ray_tracing::OwnedBlasGeometries<PointerReferences>;
371 fn into_trace(self) -> Self::Output {
372 match self {
373 crate::ray_tracing::OwnedBlasGeometries::TriangleGeometries(geos) => {
374 crate::ray_tracing::OwnedBlasGeometries::TriangleGeometries(
375 geos.into_iter().map(|g| g.into_trace()).collect(),
376 )
377 }
378 crate::ray_tracing::OwnedBlasGeometries::AabbGeometries(geos) => {
379 crate::ray_tracing::OwnedBlasGeometries::AabbGeometries(
380 geos.into_iter().map(|g| g.into_trace()).collect(),
381 )
382 }
383 }
384 }
385}
386
387impl IntoTrace for crate::ray_tracing::OwnedBlasTriangleGeometry<ArcReferences> {
388 type Output = crate::ray_tracing::OwnedBlasTriangleGeometry<PointerReferences>;
389 fn into_trace(self) -> Self::Output {
390 crate::ray_tracing::OwnedBlasTriangleGeometry {
391 size: self.size,
392 vertex_buffer: self.vertex_buffer.into_trace(),
393 index_buffer: self.index_buffer.map(|b| b.into_trace()),
394 transform_buffer: self.transform_buffer.map(|b| b.into_trace()),
395 first_vertex: self.first_vertex,
396 vertex_stride: self.vertex_stride,
397 first_index: self.first_index,
398 transform_buffer_offset: self.transform_buffer_offset,
399 }
400 }
401}
402
403impl IntoTrace for crate::ray_tracing::OwnedBlasAabbGeometry<ArcReferences> {
404 type Output = crate::ray_tracing::OwnedBlasAabbGeometry<PointerReferences>;
405 fn into_trace(self) -> Self::Output {
406 crate::ray_tracing::OwnedBlasAabbGeometry {
407 size: self.size,
408 stride: self.stride,
409 aabb_buffer: self.aabb_buffer.into_trace(),
410 primitive_offset: self.primitive_offset,
411 }
412 }
413}
414
415impl IntoTrace for crate::ray_tracing::OwnedTlasPackage<ArcReferences> {
416 type Output = crate::ray_tracing::OwnedTlasPackage<PointerReferences>;
417 fn into_trace(self) -> Self::Output {
418 crate::ray_tracing::OwnedTlasPackage {
419 tlas: self.tlas.into_trace(),
420 instances: self
421 .instances
422 .into_iter()
423 .map(|opt| opt.map(|inst| inst.into_trace()))
424 .collect(),
425 lowest_unmodified: self.lowest_unmodified,
426 }
427 }
428}
429
430impl IntoTrace for crate::ray_tracing::OwnedTlasInstance<ArcReferences> {
431 type Output = crate::ray_tracing::OwnedTlasInstance<PointerReferences>;
432 fn into_trace(self) -> Self::Output {
433 crate::ray_tracing::OwnedTlasInstance {
434 blas: self.blas.into_trace(),
435 transform: self.transform,
436 custom_data: self.custom_data,
437 mask: self.mask,
438 intersection_index: self.intersection_index,
439 flags: self.flags,
440 }
441 }
442}
443
444impl<C: IntoTrace> IntoTrace for BasePass<C, Infallible> {
445 type Output = BasePass<C::Output, Infallible>;
446
447 fn into_trace(self) -> Self::Output {
448 BasePass {
449 label: self.label,
450 error: self.error,
451 commands: self
452 .commands
453 .into_iter()
454 .map(|cmd| cmd.into_trace())
455 .collect(),
456 dynamic_offsets: self.dynamic_offsets,
457 string_data: self.string_data,
458 }
459 }
460}
461
462impl IntoTrace for ArcComputeCommand {
463 type Output = ComputeCommand<PointerReferences>;
464 fn into_trace(self) -> Self::Output {
465 use ComputeCommand as C;
466 match self {
467 C::SetBindGroup {
468 index,
469 num_dynamic_offsets,
470 bind_group,
471 } => C::SetBindGroup {
472 index,
473 num_dynamic_offsets,
474 bind_group: bind_group.map(|bg| bg.into_trace()),
475 },
476 C::SetPipeline(id) => C::SetPipeline(id.into_trace()),
477 C::SetImmediate { offset, data } => C::SetImmediate { offset, data },
478 C::DispatchWorkgroups(groups) => C::DispatchWorkgroups(groups),
479 C::DispatchWorkgroupsIndirect { buffer, offset } => C::DispatchWorkgroupsIndirect {
480 buffer: buffer.into_trace(),
481 offset,
482 },
483 C::PushDebugGroup { color, len } => C::PushDebugGroup { color, len },
484 C::PopDebugGroup => C::PopDebugGroup,
485 C::InsertDebugMarker { color, len } => C::InsertDebugMarker { color, len },
486 C::WriteTimestamp {
487 query_set,
488 query_index,
489 } => C::WriteTimestamp {
490 query_set: query_set.into_trace(),
491 query_index,
492 },
493 C::BeginPipelineStatisticsQuery {
494 query_set,
495 query_index,
496 } => C::BeginPipelineStatisticsQuery {
497 query_set: query_set.into_trace(),
498 query_index,
499 },
500 C::EndPipelineStatisticsQuery => C::EndPipelineStatisticsQuery,
501 C::TransitionResources {
502 buffer_transitions,
503 texture_transitions,
504 } => C::TransitionResources {
505 buffer_transitions: buffer_transitions
506 .into_iter()
507 .map(|buffer_transition| wgt::BufferTransition {
508 buffer: buffer_transition.buffer.into_trace(),
509 state: buffer_transition.state,
510 })
511 .collect(),
512 texture_transitions: texture_transitions
513 .into_iter()
514 .map(|texture_transition| wgt::TextureTransition {
515 texture: texture_transition.texture.into_trace(),
516 selector: texture_transition.selector,
517 state: texture_transition.state,
518 })
519 .collect(),
520 },
521 }
522 }
523}
524
525impl IntoTrace for ArcRenderCommand {
526 type Output = RenderCommand<PointerReferences>;
527 fn into_trace(self) -> Self::Output {
528 use RenderCommand as C;
529 match self {
530 C::SetBindGroup {
531 index,
532 num_dynamic_offsets,
533 bind_group,
534 } => C::SetBindGroup {
535 index,
536 num_dynamic_offsets,
537 bind_group: bind_group.map(|bg| bg.into_trace()),
538 },
539 C::SetPipeline(id) => C::SetPipeline(id.into_trace()),
540 C::SetIndexBuffer {
541 buffer,
542 index_format,
543 offset,
544 size,
545 } => C::SetIndexBuffer {
546 buffer: buffer.into_trace(),
547 index_format,
548 offset,
549 size,
550 },
551 C::SetVertexBuffer {
552 slot,
553 buffer,
554 offset,
555 size,
556 } => C::SetVertexBuffer {
557 slot,
558 buffer: buffer.into_trace(),
559 offset,
560 size,
561 },
562 C::SetBlendConstant(color) => C::SetBlendConstant(color),
563 C::SetStencilReference(val) => C::SetStencilReference(val),
564 C::SetViewport {
565 rect,
566 depth_min,
567 depth_max,
568 } => C::SetViewport {
569 rect,
570 depth_min,
571 depth_max,
572 },
573 C::SetScissor(rect) => C::SetScissor(rect),
574 C::SetImmediate { offset, data } => C::SetImmediate { offset, data },
575 C::Draw {
576 vertex_count,
577 instance_count,
578 first_vertex,
579 first_instance,
580 } => C::Draw {
581 vertex_count,
582 instance_count,
583 first_vertex,
584 first_instance,
585 },
586 C::DrawIndexed {
587 index_count,
588 instance_count,
589 first_index,
590 base_vertex,
591 first_instance,
592 } => C::DrawIndexed {
593 index_count,
594 instance_count,
595 first_index,
596 base_vertex,
597 first_instance,
598 },
599 C::DrawMeshTasks {
600 group_count_x,
601 group_count_y,
602 group_count_z,
603 } => C::DrawMeshTasks {
604 group_count_x,
605 group_count_y,
606 group_count_z,
607 },
608 C::DrawIndirect {
609 buffer,
610 offset,
611 count,
612 family,
613 vertex_or_index_limit,
614 instance_limit,
615 } => C::DrawIndirect {
616 buffer: buffer.into_trace(),
617 offset,
618 count,
619 family,
620 vertex_or_index_limit,
621 instance_limit,
622 },
623 C::MultiDrawIndirectCount {
624 buffer,
625 offset,
626 count_buffer,
627 count_buffer_offset,
628 max_count,
629 family,
630 } => C::MultiDrawIndirectCount {
631 buffer: buffer.into_trace(),
632 offset,
633 count_buffer: count_buffer.into_trace(),
634 count_buffer_offset,
635 max_count,
636 family,
637 },
638 C::PushDebugGroup { color, len } => C::PushDebugGroup { color, len },
639 C::PopDebugGroup => C::PopDebugGroup,
640 C::InsertDebugMarker { color, len } => C::InsertDebugMarker { color, len },
641 C::WriteTimestamp {
642 query_set,
643 query_index,
644 } => C::WriteTimestamp {
645 query_set: query_set.into_trace(),
646 query_index,
647 },
648 C::BeginOcclusionQuery { query_index } => C::BeginOcclusionQuery { query_index },
649 C::EndOcclusionQuery => C::EndOcclusionQuery,
650 C::BeginPipelineStatisticsQuery {
651 query_set,
652 query_index,
653 } => C::BeginPipelineStatisticsQuery {
654 query_set: query_set.into_trace(),
655 query_index,
656 },
657 C::EndPipelineStatisticsQuery => C::EndPipelineStatisticsQuery,
658 C::ExecuteBundle(bundle) => C::ExecuteBundle(bundle.into_trace()),
659 }
660 }
661}
662
663impl IntoTrace for ArcRayTracingCommand {
664 type Output = RayTracingCommand<PointerReferences>;
665 fn into_trace(self) -> Self::Output {
666 use RayTracingCommand as C;
667 match self {
668 C::SetBindGroup {
669 index,
670 num_dynamic_offsets,
671 bind_group,
672 } => C::SetBindGroup {
673 index,
674 num_dynamic_offsets,
675 bind_group: bind_group.map(|bg| bg.into_trace()),
676 },
677 C::SetPipeline(id) => C::SetPipeline(id.into_trace()),
678 C::SetImmediate { offset, data } => C::SetImmediate { offset, data },
679 C::TraceRays(groups) => C::TraceRays(groups),
680 C::TraceRaysIndirect { buffer, offset } => C::TraceRaysIndirect {
681 buffer: buffer.into_trace(),
682 offset,
683 },
684 C::PushDebugGroup { color, len } => C::PushDebugGroup { color, len },
685 C::PopDebugGroup => C::PopDebugGroup,
686 C::InsertDebugMarker { color, len } => C::InsertDebugMarker { color, len },
687 }
688 }
689}
690
691impl IntoTrace for crate::binding_model::PipelineLayoutDescriptor<'_> {
692 type Output = crate::binding_model::PipelineLayoutDescriptor<
693 'static,
694 PointerId<markers::BindGroupLayout>,
695 >;
696 fn into_trace(self) -> Self::Output {
697 crate::binding_model::PipelineLayoutDescriptor {
698 label: self.label.map(|l| Cow::Owned(l.into_owned())),
699 bind_group_layouts: self
700 .bind_group_layouts
701 .iter()
702 .map(|bgl| bgl.to_trace())
703 .collect(),
704 immediate_size: self.immediate_size,
705 }
706 }
707}
708
709impl<'a> IntoTrace for &'_ crate::binding_model::BindGroupDescriptor<'a> {
710 type Output = TraceBindGroupDescriptor<'a>;
711
712 fn into_trace(self) -> Self::Output {
713 use crate::binding_model::{BindGroupEntry, BindingResource, BufferBinding};
714 TraceBindGroupDescriptor {
715 label: self.label.clone(),
716 layout: self.layout.to_trace(),
717 entries: Cow::Owned(
718 self.entries
719 .iter()
720 .map(|entry| {
721 let resource = match &entry.resource {
722 BindingResource::Buffer(buffer_binding) => {
723 BindingResource::Buffer(BufferBinding {
724 buffer: buffer_binding.buffer.to_trace(),
725 offset: buffer_binding.offset,
726 size: buffer_binding.size,
727 })
728 }
729 BindingResource::BufferArray(buffer_bindings) => {
730 let resolved_buffers: Vec<_> = buffer_bindings
731 .iter()
732 .map(|bb| BufferBinding {
733 buffer: bb.buffer.to_trace(),
734 offset: bb.offset,
735 size: bb.size,
736 })
737 .collect();
738 BindingResource::BufferArray(Cow::Owned(resolved_buffers))
739 }
740 BindingResource::Sampler(sampler_id) => {
741 BindingResource::Sampler(sampler_id.to_trace())
742 }
743 BindingResource::SamplerArray(sampler_ids) => {
744 let resolved: Vec<_> =
745 sampler_ids.iter().map(|id| id.to_trace()).collect();
746 BindingResource::SamplerArray(Cow::Owned(resolved))
747 }
748 BindingResource::TextureView(texture_view_id) => {
749 BindingResource::TextureView(texture_view_id.to_trace())
750 }
751 BindingResource::TextureViewArray(texture_view_ids) => {
752 let resolved: Vec<_> =
753 texture_view_ids.iter().map(|id| id.to_trace()).collect();
754 BindingResource::TextureViewArray(Cow::Owned(resolved))
755 }
756 BindingResource::AccelerationStructure(tlas_id) => {
757 BindingResource::AccelerationStructure(tlas_id.to_trace())
758 }
759 BindingResource::AccelerationStructureArray(tlas_ids) => {
760 let resolved: Vec<_> =
761 tlas_ids.iter().map(|id| id.to_trace()).collect();
762 BindingResource::AccelerationStructureArray(Cow::Owned(resolved))
763 }
764 BindingResource::ExternalTexture(external_texture_id) => {
765 BindingResource::ExternalTexture(external_texture_id.to_trace())
766 }
767 };
768 BindGroupEntry {
769 binding: entry.binding,
770 resource,
771 }
772 })
773 .collect(),
774 ),
775 }
776 }
777}
778
779impl<'a> IntoTrace for crate::pipeline::ResolvedGeneralRenderPipelineDescriptor<'a> {
780 type Output = TraceGeneralRenderPipelineDescriptor<'a>;
781
782 fn into_trace(self) -> Self::Output {
783 TraceGeneralRenderPipelineDescriptor {
784 label: self.label,
785 layout: self.layout.into_trace(),
786 vertex: self.vertex.into_trace(),
787 primitive: self.primitive,
788 depth_stencil: self.depth_stencil,
789 multisample: self.multisample,
790 fragment: self.fragment.map(|f| f.into_trace()),
791 multiview_mask: self.multiview_mask,
792 cache: self.cache.map(|c| c.into_trace()),
793 }
794 }
795}
796
797impl<'a> IntoTrace for crate::pipeline::ComputePipelineDescriptor<'a> {
798 type Output = TraceComputePipelineDescriptor<'a>;
799
800 fn into_trace(self) -> Self::Output {
801 TraceComputePipelineDescriptor {
802 label: self.label,
803 layout: self.layout.into_trace(),
804 stage: self.stage.into_trace(),
805 cache: self.cache.map(|c| c.into_trace()),
806 }
807 }
808}
809
810impl<'a> IntoTrace for crate::ray_tracing_pipeline::RayTracingPipelineDescriptor<'a> {
811 type Output = TraceRayTracingPipelineDescriptor<'a>;
812
813 fn into_trace(self) -> Self::Output {
814 TraceRayTracingPipelineDescriptor {
815 label: self.label,
816 layout: self.layout.into_trace(),
817 ray_generation: self.ray_generation.into_trace(),
818 miss: self.miss.into_trace(),
819 intersections: self
820 .intersections
821 .into_iter()
822 .map(|intersection| match intersection {
823 crate::ray_tracing_pipeline::RayTracingIntersectionDescriptor::Triangle {
824 closest_hit,
825 any_hit,
826 } => crate::ray_tracing_pipeline::RayTracingIntersectionDescriptor::Triangle {
827 closest_hit: closest_hit.into_trace(),
828 any_hit: any_hit.map(|a| a.into_trace()),
829 },
830 })
831 .collect(),
832 cache: self.cache.map(|c| c.into_trace()),
833 max_recursion_depth: self.max_recursion_depth,
834 }
835 }
836}
837
838impl<'a> IntoTrace for crate::pipeline::ProgrammableStageDescriptor<'a> {
839 type Output =
840 crate::pipeline::ProgrammableStageDescriptor<'a, PointerId<markers::ShaderModule>>;
841 fn into_trace(self) -> Self::Output {
842 crate::pipeline::ProgrammableStageDescriptor {
843 module: self.module.into_trace(),
844 entry_point: self.entry_point,
845 constants: self.constants,
846 zero_initialize_workgroup_memory: self.zero_initialize_workgroup_memory,
847 }
848 }
849}
850
851impl<'a> IntoTrace
852 for crate::pipeline::RenderPipelineVertexProcessor<'a, Arc<crate::pipeline::ShaderModule>>
853{
854 type Output =
855 crate::pipeline::RenderPipelineVertexProcessor<'a, PointerId<markers::ShaderModule>>;
856 fn into_trace(self) -> Self::Output {
857 match self {
858 crate::pipeline::RenderPipelineVertexProcessor::Vertex(vertex) => {
859 crate::pipeline::RenderPipelineVertexProcessor::Vertex(vertex.into_trace())
860 }
861 crate::pipeline::RenderPipelineVertexProcessor::Mesh(task, mesh) => {
862 crate::pipeline::RenderPipelineVertexProcessor::Mesh(
863 task.map(|t| t.into_trace()),
864 mesh.into_trace(),
865 )
866 }
867 }
868 }
869}
870
871impl<'a> IntoTrace for crate::pipeline::TaskState<'a> {
872 type Output = crate::pipeline::TaskState<'a, PointerId<markers::ShaderModule>>;
873 fn into_trace(self) -> Self::Output {
874 crate::pipeline::TaskState {
875 stage: self.stage.into_trace(),
876 }
877 }
878}
879
880impl<'a> IntoTrace for crate::pipeline::MeshState<'a> {
881 type Output = crate::pipeline::MeshState<'a, PointerId<markers::ShaderModule>>;
882 fn into_trace(self) -> Self::Output {
883 crate::pipeline::MeshState {
884 stage: self.stage.into_trace(),
885 }
886 }
887}
888
889impl<'a> IntoTrace for crate::pipeline::VertexState<'a> {
890 type Output = crate::pipeline::VertexState<'a, PointerId<markers::ShaderModule>>;
891 fn into_trace(self) -> Self::Output {
892 crate::pipeline::VertexState {
893 stage: self.stage.into_trace(),
894 buffers: self.buffers,
895 }
896 }
897}
898
899impl<'a> IntoTrace for crate::pipeline::FragmentState<'a> {
900 type Output = crate::pipeline::FragmentState<'a, PointerId<markers::ShaderModule>>;
901 fn into_trace(self) -> Self::Output {
902 crate::pipeline::FragmentState {
903 stage: self.stage.into_trace(),
904 targets: self.targets,
905 }
906 }
907}
908
909impl<T: IntoTrace> IntoTrace for Option<T> {
910 type Output = Option<T::Output>;
911 fn into_trace(self) -> Self::Output {
912 self.map(|v| v.into_trace())
913 }
914}
915
916impl<'a, BGLE> IntoTrace for crate::binding_model::BindGroupLayoutDescriptor<'a, BGLE>
917where
918 BGLE: Copy,
919 BGLE: Into<crate::binding_model::BindGroupLayoutEntry>,
920{
921 type Output = crate::binding_model::BindGroupLayoutDescriptor<
922 'static,
923 crate::binding_model::BindGroupLayoutEntry,
924 >;
925
926 fn into_trace(self) -> Self::Output {
927 crate::binding_model::BindGroupLayoutDescriptor {
928 label: self.label.map(|l| Cow::Owned(l.into_owned())),
929 entries: Cow::Owned(self.entries.iter().map(|&e| e.into()).collect()),
930 }
931 }
932}
933
934fn action_to_owned(action: Action<'_, PointerReferences>) -> Action<'static, PointerReferences> {
938 use Action as A;
939 match action {
940 A::Init { desc, backend } => A::Init {
941 desc: desc.map_label(owned_label),
942 backend,
943 },
944 A::ConfigureSurface(surface, config) => A::ConfigureSurface(surface, config),
945 A::CreateBuffer(buffer, desc) => A::CreateBuffer(buffer, desc.map_label(owned_label)),
946 A::DestroyBuffer(buffer) => A::DestroyBuffer(buffer),
947 A::DropBuffer(buffer) => A::DropBuffer(buffer),
948 A::DestroyTexture(texture) => A::DestroyTexture(texture),
949 A::DropTexture(texture) => A::DropTexture(texture),
950 A::DropTextureView(texture_view) => A::DropTextureView(texture_view),
951 A::DestroyExternalTexture(external_texture) => A::DestroyExternalTexture(external_texture),
952 A::DropExternalTexture(external_texture) => A::DropExternalTexture(external_texture),
953 A::DropSampler(sampler) => A::DropSampler(sampler),
954 A::GetSurfaceTexture { id, parent } => A::GetSurfaceTexture { id, parent },
955 A::Present(surface) => A::Present(surface),
956 A::DiscardSurfaceTexture(surface) => A::DiscardSurfaceTexture(surface),
957 A::ReleaseSurfaceTexture(surface) => A::ReleaseSurfaceTexture(surface),
958 A::DropBindGroupLayout(layout) => A::DropBindGroupLayout(layout),
959 A::GetRenderPipelineBindGroupLayout {
960 id,
961 pipeline,
962 index,
963 } => A::GetRenderPipelineBindGroupLayout {
964 id,
965 pipeline,
966 index,
967 },
968 A::GetComputePipelineBindGroupLayout {
969 id,
970 pipeline,
971 index,
972 } => A::GetComputePipelineBindGroupLayout {
973 id,
974 pipeline,
975 index,
976 },
977 A::GetRayTracingPipelineBindGroupLayout {
978 id,
979 pipeline,
980 index,
981 } => A::GetRayTracingPipelineBindGroupLayout {
982 id,
983 pipeline,
984 index,
985 },
986 A::DropPipelineLayout(layout) => A::DropPipelineLayout(layout),
987 A::DropBindGroup(bind_group) => A::DropBindGroup(bind_group),
988 A::DropShaderModule(shader_module) => A::DropShaderModule(shader_module),
989 A::DropComputePipeline(pipeline) => A::DropComputePipeline(pipeline),
990 A::DropRenderPipeline(pipeline) => A::DropRenderPipeline(pipeline),
991 A::DropRayTracingPipeline(pipeline) => A::DropRayTracingPipeline(pipeline),
992 A::DropPipelineCache(cache) => A::DropPipelineCache(cache),
993 A::DropRenderBundle(render_bundle) => A::DropRenderBundle(render_bundle),
994 A::DestroyQuerySet(query_set) => A::DestroyQuerySet(query_set),
995 A::DropQuerySet(query_set) => A::DropQuerySet(query_set),
996 A::WriteBuffer {
997 id,
998 data,
999 offset,
1000 size,
1001 queued,
1002 } => A::WriteBuffer {
1003 id,
1004 data,
1005 offset,
1006 size,
1007 queued,
1008 },
1009 A::WriteTexture {
1010 to,
1011 data,
1012 layout,
1013 size,
1014 } => A::WriteTexture {
1015 to,
1016 data,
1017 layout,
1018 size,
1019 },
1020 A::Submit(index, commands) => A::Submit(index, commands),
1021 A::FailedCommands {
1022 commands,
1023 failed_at_submit,
1024 error,
1025 } => A::FailedCommands {
1026 commands,
1027 failed_at_submit,
1028 error,
1029 },
1030 A::DropBlas(blas) => A::DropBlas(blas),
1031 A::DropTlas(tlas) => A::DropTlas(tlas),
1032
1033 A::CreateTexture(id, desc) => A::CreateTexture(id, desc.map_label(owned_label)),
1034 A::CreateTextureError(id, desc) => A::CreateTextureError(id, desc.map_label(owned_label)),
1035 A::CreateTextureView { id, parent, desc } => A::CreateTextureView {
1036 id,
1037 parent,
1038 desc: crate::resource::TextureViewDescriptor {
1039 label: owned_label(&desc.label),
1040 format: desc.format,
1041 dimension: desc.dimension,
1042 usage: desc.usage,
1043 range: desc.range,
1044 swizzle: desc.swizzle,
1045 },
1046 },
1047 A::CreateExternalTexture { id, desc, planes } => A::CreateExternalTexture {
1048 id,
1049 desc: desc.map_label(owned_label),
1050 planes,
1051 },
1052 A::CreateSampler(id, desc) => A::CreateSampler(
1053 id,
1054 crate::resource::SamplerDescriptor {
1055 label: owned_label(&desc.label),
1056 address_modes: desc.address_modes,
1057 mag_filter: desc.mag_filter,
1058 min_filter: desc.min_filter,
1059 mipmap_filter: desc.mipmap_filter,
1060 lod_min_clamp: desc.lod_min_clamp,
1061 lod_max_clamp: desc.lod_max_clamp,
1062 compare: desc.compare,
1063 anisotropy_clamp: desc.anisotropy_clamp,
1064 border_color: desc.border_color,
1065 },
1066 ),
1067 A::CreateBindGroupLayout(id, desc) => A::CreateBindGroupLayout(
1068 id,
1069 crate::binding_model::BindGroupLayoutDescriptor {
1070 label: owned_label(&desc.label),
1071 entries: Cow::Owned(desc.entries.into_owned()),
1072 },
1073 ),
1074 A::CreatePipelineLayout(id, desc) => A::CreatePipelineLayout(
1075 id,
1076 crate::binding_model::PipelineLayoutDescriptor {
1077 label: owned_label(&desc.label),
1078 bind_group_layouts: Cow::Owned(desc.bind_group_layouts.into_owned()),
1079 immediate_size: desc.immediate_size,
1080 },
1081 ),
1082 A::CreateBindGroup(id, desc) => A::CreateBindGroup(
1083 id,
1084 crate::binding_model::BindGroupDescriptor {
1085 label: owned_label(&desc.label),
1086 layout: desc.layout,
1087 entries: desc
1088 .entries
1089 .iter()
1090 .map(|e| crate::binding_model::BindGroupEntry {
1091 binding: e.binding,
1092 resource: match &e.resource {
1093 crate::binding_model::BindingResource::Buffer(buffer_binding) => {
1094 crate::binding_model::BindingResource::Buffer(
1095 buffer_binding.clone(),
1096 )
1097 }
1098 crate::binding_model::BindingResource::BufferArray(cow) => {
1099 crate::binding_model::BindingResource::BufferArray(Cow::Owned(
1100 cow.clone().into_owned(),
1101 ))
1102 }
1103 crate::binding_model::BindingResource::Sampler(sampler) => {
1104 crate::binding_model::BindingResource::Sampler(*sampler)
1105 }
1106 crate::binding_model::BindingResource::SamplerArray(cow) => {
1107 crate::binding_model::BindingResource::SamplerArray(Cow::Owned(
1108 cow.clone().into_owned(),
1109 ))
1110 }
1111 crate::binding_model::BindingResource::TextureView(texture_view) => {
1112 crate::binding_model::BindingResource::TextureView(*texture_view)
1113 }
1114 crate::binding_model::BindingResource::TextureViewArray(cow) => {
1115 crate::binding_model::BindingResource::TextureViewArray(Cow::Owned(
1116 cow.clone().into_owned(),
1117 ))
1118 }
1119 crate::binding_model::BindingResource::AccelerationStructure(
1120 acceleration_structure,
1121 ) => crate::binding_model::BindingResource::AccelerationStructure(
1122 *acceleration_structure,
1123 ),
1124 crate::binding_model::BindingResource::AccelerationStructureArray(
1125 cow,
1126 ) => crate::binding_model::BindingResource::AccelerationStructureArray(
1127 Cow::Owned(cow.clone().into_owned()),
1128 ),
1129 crate::binding_model::BindingResource::ExternalTexture(
1130 external_texture,
1131 ) => crate::binding_model::BindingResource::ExternalTexture(
1132 *external_texture,
1133 ),
1134 },
1135 })
1136 .collect(),
1137 },
1138 ),
1139 A::CreateShaderModule { id, desc, data } => A::CreateShaderModule {
1140 id,
1141 desc: crate::pipeline::ShaderModuleDescriptor {
1142 label: owned_label(&desc.label),
1143 runtime_checks: desc.runtime_checks,
1144 },
1145 data,
1146 },
1147 A::CreateShaderModulePassthrough {
1148 id,
1149 data,
1150 label,
1151 entry_points,
1152 } => A::CreateShaderModulePassthrough {
1153 id,
1154 data,
1155 label: owned_label(&label),
1156 entry_points: entry_points
1157 .iter()
1158 .map(|ep| wgt::PassthroughShaderEntryPoint {
1159 name: Cow::Owned(ep.name.to_string()),
1160 workgroup_size: ep.workgroup_size,
1161 })
1162 .collect(),
1163 },
1164 A::CreateComputePipeline { id, desc } => A::CreateComputePipeline {
1165 id,
1166 desc: crate::pipeline::ComputePipelineDescriptor {
1167 label: owned_label(&desc.label),
1168 layout: desc.layout,
1169 stage: owned_stage(desc.stage),
1170 cache: desc.cache,
1171 },
1172 },
1173 A::CreateGeneralRenderPipeline { id, desc } => A::CreateGeneralRenderPipeline {
1174 id,
1175 desc: crate::pipeline::GeneralRenderPipelineDescriptor {
1176 label: owned_label(&desc.label),
1177 layout: desc.layout,
1178 vertex: match desc.vertex {
1179 crate::pipeline::RenderPipelineVertexProcessor::Vertex(
1180 crate::pipeline::VertexState { stage, buffers },
1181 ) => crate::pipeline::RenderPipelineVertexProcessor::Vertex(
1182 crate::pipeline::VertexState {
1183 stage: owned_stage(stage),
1184 buffers: buffers
1185 .iter()
1186 .map(|b| {
1187 b.clone().map(|buffer| crate::pipeline::VertexBufferLayout {
1188 array_stride: buffer.array_stride,
1189 step_mode: buffer.step_mode,
1190 attributes: Cow::Owned(buffer.attributes.into_owned()),
1191 })
1192 })
1193 .collect(),
1194 },
1195 ),
1196 crate::pipeline::RenderPipelineVertexProcessor::Mesh(task, mesh) => {
1197 crate::pipeline::RenderPipelineVertexProcessor::Mesh(
1198 task.map(|t| crate::pipeline::TaskState {
1199 stage: owned_stage(t.stage),
1200 }),
1201 crate::pipeline::MeshState {
1202 stage: owned_stage(mesh.stage),
1203 },
1204 )
1205 }
1206 },
1207 primitive: desc.primitive,
1208 depth_stencil: desc.depth_stencil,
1209 multisample: desc.multisample,
1210 fragment: desc.fragment.map(|f| crate::pipeline::FragmentState {
1211 stage: owned_stage(f.stage),
1212 targets: Cow::Owned(f.targets.into_owned()),
1213 }),
1214 multiview_mask: desc.multiview_mask,
1215 cache: desc.cache,
1216 },
1217 },
1218 A::CreateRayTracingPipeline { id, desc } => A::CreateRayTracingPipeline {
1219 id,
1220 desc: crate::ray_tracing_pipeline::RayTracingPipelineDescriptor {
1221 label: owned_label(&desc.label),
1222 layout: desc.layout,
1223 ray_generation: owned_stage(desc.ray_generation),
1224 miss: owned_stage(desc.miss),
1225 intersections: desc
1226 .intersections
1227 .into_iter()
1228 .map(|i| match i {
1229 crate::ray_tracing_pipeline::RayTracingIntersectionDescriptor::Triangle {
1230 closest_hit,
1231 any_hit,
1232 } => crate::ray_tracing_pipeline::RayTracingIntersectionDescriptor::Triangle {
1233 closest_hit: owned_stage(closest_hit),
1234 any_hit: any_hit.map(owned_stage),
1235 },
1236 })
1237 .collect::<Vec<_>>(),
1238 max_recursion_depth: desc.max_recursion_depth,
1239 cache: desc.cache,
1240 },
1241 },
1242 A::CreatePipelineCache { id, desc } => A::CreatePipelineCache {
1243 id,
1244 desc: crate::pipeline::PipelineCacheDescriptor {
1245 label: owned_label(&desc.label),
1246 data: desc.data.map(|d| Cow::Owned(d.to_vec())),
1247 fallback: desc.fallback,
1248 },
1249 },
1250 A::CreateRenderBundle { id, desc, base } => A::CreateRenderBundle {
1251 id,
1252 desc: crate::command::RenderBundleEncoderDescriptor {
1253 label: owned_label(&desc.label),
1254 color_formats: Cow::Owned(desc.color_formats.into_owned()),
1255 depth_stencil: desc.depth_stencil,
1256 sample_count: desc.sample_count,
1257 multiview: desc.multiview,
1258 },
1259 base,
1260 },
1261 A::CreateQuerySet { id, desc } => A::CreateQuerySet {
1262 id,
1263 desc: desc.map_label(owned_label),
1264 },
1265 A::CreateBlas { id, desc, sizes } => A::CreateBlas {
1266 id,
1267 desc: desc.map_label(owned_label),
1268 sizes,
1269 },
1270 A::CreateTlas { id, desc } => A::CreateTlas {
1271 id,
1272 desc: desc.map_label(owned_label),
1273 },
1274 }
1275}
1276
1277fn owned_stage<SM>(
1278 stage: crate::pipeline::ProgrammableStageDescriptor<'_, SM>,
1279) -> crate::pipeline::ProgrammableStageDescriptor<'static, SM> {
1280 crate::pipeline::ProgrammableStageDescriptor {
1281 module: stage.module,
1282 entry_point: owned_label(&stage.entry_point),
1283 constants: stage.constants,
1284 zero_initialize_workgroup_memory: stage.zero_initialize_workgroup_memory,
1285 }
1286}
1287
1288fn owned_label(l: &Option<Cow<'_, str>>) -> Option<Cow<'static, str>> {
1289 l.as_ref().map(|l| Cow::Owned(l.to_string()))
1290}