Skip to main content

wgpu_hal/dynamic/
command.rs

1use alloc::{boxed::Box, vec::Vec};
2use core::ops::Range;
3
4use crate::{
5    AccelerationStructureBarrier, Api, Attachment, BufferBarrier, BufferBinding, BufferCopy,
6    BufferTextureCopy, BuildAccelerationStructureDescriptor, ColorAttachment, CommandEncoder,
7    ComputePassDescriptor, DepthStencilAttachment, DeviceError, DynRayTracingPipeline, Label,
8    MemoryRange, PassTimestampWrites, RayTracingPassDescriptor, Rect, RenderPassDescriptor,
9    TextureBarrier, TextureCopy,
10};
11
12use super::{
13    DynAccelerationStructure, DynBindGroup, DynBuffer, DynCommandBuffer, DynComputePipeline,
14    DynPipelineLayout, DynQuerySet, DynRenderPipeline, DynResource, DynResourceExt as _,
15    DynTexture, DynTextureView,
16};
17
18pub trait DynCommandEncoder: DynResource + core::fmt::Debug {
19    /// Dynamic version of [CommandEncoder::begin_encoding]
20    unsafe fn begin_encoding(&mut self, label: Label) -> Result<(), DeviceError>;
21
22    /// Dynamic version of [CommandEncoder::discard_encoding]
23    unsafe fn discard_encoding(&mut self);
24
25    /// Dynamic version of [CommandEncoder::end_encoding]
26    unsafe fn end_encoding(&mut self) -> Result<Box<dyn DynCommandBuffer>, DeviceError>;
27
28    /// Dynamic version of [CommandEncoder::reset_all]
29    unsafe fn reset_all(&mut self, command_buffers: Vec<Box<dyn DynCommandBuffer>>);
30
31    /// Dynamic version of [CommandEncoder::transition_buffers]
32    unsafe fn transition_buffers(&mut self, barriers: &[BufferBarrier<'_, dyn DynBuffer>]);
33    /// Dynamic version of [CommandEncoder::transition_textures]
34    unsafe fn transition_textures(&mut self, barriers: &[TextureBarrier<'_, dyn DynTexture>]);
35
36    /// Dynamic version of [CommandEncoder::clear_buffer]
37    unsafe fn clear_buffer(&mut self, buffer: &dyn DynBuffer, range: MemoryRange);
38
39    /// Dynamic version of [CommandEncoder::copy_buffer_to_buffer]
40    unsafe fn copy_buffer_to_buffer(
41        &mut self,
42        src: &dyn DynBuffer,
43        dst: &dyn DynBuffer,
44        regions: &[BufferCopy],
45    );
46
47    /// Dynamic version of [CommandEncoder::copy_texture_to_texture]
48    unsafe fn copy_texture_to_texture(
49        &mut self,
50        src: &dyn DynTexture,
51        src_usage: wgt::TextureUses,
52        dst: &dyn DynTexture,
53        regions: &[TextureCopy],
54    );
55
56    /// Dynamic version of [CommandEncoder::copy_buffer_to_texture]
57    unsafe fn copy_buffer_to_texture(
58        &mut self,
59        src: &dyn DynBuffer,
60        dst: &dyn DynTexture,
61        regions: &[BufferTextureCopy],
62    );
63
64    /// Dynamic version of [CommandEncoder::copy_texture_to_buffer]
65    unsafe fn copy_texture_to_buffer(
66        &mut self,
67        src: &dyn DynTexture,
68        src_usage: wgt::TextureUses,
69        dst: &dyn DynBuffer,
70        regions: &[BufferTextureCopy],
71    );
72
73    /// Dynamic version of [CommandEncoder::set_bind_group]
74    unsafe fn set_bind_group(
75        &mut self,
76        layout: &dyn DynPipelineLayout,
77        index: u32,
78        group: &dyn DynBindGroup,
79        dynamic_offsets: &[wgt::DynamicOffset],
80    );
81
82    /// Dynamic version of [CommandEncoder::set_immediates]
83    unsafe fn set_immediates(
84        &mut self,
85        layout: &dyn DynPipelineLayout,
86        offset_bytes: u32,
87        data: &[u32],
88    );
89
90    /// Dynamic version of [CommandEncoder::insert_debug_marker]
91    unsafe fn insert_debug_marker(&mut self, label: &str);
92    /// Dynamic version of [CommandEncoder::begin_debug_marker]
93    unsafe fn begin_debug_marker(&mut self, group_label: &str);
94    /// Dynamic version of [CommandEncoder::end_debug_marker]
95    unsafe fn end_debug_marker(&mut self);
96
97    /// Dynamic version of [CommandEncoder::begin_query]
98    unsafe fn begin_query(&mut self, set: &dyn DynQuerySet, index: u32);
99    /// Dynamic version of [CommandEncoder::end_query]
100    unsafe fn end_query(&mut self, set: &dyn DynQuerySet, index: u32);
101    /// Dynamic version of [CommandEncoder::write_timestamp]
102    unsafe fn write_timestamp(&mut self, set: &dyn DynQuerySet, index: u32);
103    /// Dynamic version of [CommandEncoder::reset_queries]
104    unsafe fn reset_queries(&mut self, set: &dyn DynQuerySet, range: Range<u32>);
105    /// Dynamic version of [CommandEncoder::copy_query_results]
106    unsafe fn copy_query_results(
107        &mut self,
108        set: &dyn DynQuerySet,
109        range: Range<u32>,
110        buffer: &dyn DynBuffer,
111        offset: wgt::BufferAddress,
112        stride: wgt::BufferSize,
113    );
114
115    /// Dynamic version of [CommandEncoder::begin_render_pass]
116    unsafe fn begin_render_pass(
117        &mut self,
118        desc: &RenderPassDescriptor<dyn DynQuerySet, dyn DynTextureView>,
119    ) -> Result<(), DeviceError>;
120    /// Dynamic version of [CommandEncoder::end_render_pass]
121    unsafe fn end_render_pass(&mut self);
122
123    /// Dynamic version of [CommandEncoder::set_render_pipeline]
124    unsafe fn set_render_pipeline(&mut self, pipeline: &dyn DynRenderPipeline);
125
126    /// Dynamic version of [CommandEncoder::set_index_buffer]
127    unsafe fn set_index_buffer<'a>(
128        &mut self,
129        binding: BufferBinding<'a, dyn DynBuffer, wgt::BufferAddress>,
130        format: wgt::IndexFormat,
131    );
132
133    /// Dynamic version of [CommandEncoder::set_vertex_buffer]
134    unsafe fn set_vertex_buffer<'a>(
135        &mut self,
136        index: u32,
137        binding: BufferBinding<'a, dyn DynBuffer, wgt::BufferAddress>,
138    );
139    /// Dynamic version of [CommandEncoder::set_viewport]
140    unsafe fn set_viewport(&mut self, rect: &Rect<f32>, depth_range: Range<f32>);
141    /// Dynamic version of [CommandEncoder::set_scissor_rect]
142    unsafe fn set_scissor_rect(&mut self, rect: &Rect<u32>);
143    /// Dynamic version of [CommandEncoder::set_stencil_reference]
144    unsafe fn set_stencil_reference(&mut self, value: u32);
145    /// Dynamic version of [CommandEncoder::set_blend_constants]
146    unsafe fn set_blend_constants(&mut self, color: &[f32; 4]);
147
148    /// Dynamic version of [CommandEncoder::draw]
149    unsafe fn draw(
150        &mut self,
151        first_vertex: u32,
152        vertex_count: u32,
153        first_instance: u32,
154        instance_count: u32,
155    );
156    /// Dynamic version of [CommandEncoder::draw_indexed]
157    unsafe fn draw_indexed(
158        &mut self,
159        first_index: u32,
160        index_count: u32,
161        base_vertex: i32,
162        first_instance: u32,
163        instance_count: u32,
164    );
165    /// Dynamic version of [CommandEncoder::draw_mesh_tasks]
166    unsafe fn draw_mesh_tasks(
167        &mut self,
168        group_count_x: u32,
169        group_count_y: u32,
170        group_count_z: u32,
171    );
172    /// Dynamic version of [CommandEncoder::draw_indirect]
173    unsafe fn draw_indirect(
174        &mut self,
175        buffer: &dyn DynBuffer,
176        offset: wgt::BufferAddress,
177        draw_count: u32,
178    );
179    /// Dynamic version of [CommandEncoder::draw_indexed_indirect]
180    unsafe fn draw_indexed_indirect(
181        &mut self,
182        buffer: &dyn DynBuffer,
183        offset: wgt::BufferAddress,
184        draw_count: u32,
185    );
186    /// Dynamic version of [CommandEncoder::draw_mesh_tasks_indirect]
187    unsafe fn draw_mesh_tasks_indirect(
188        &mut self,
189        buffer: &dyn DynBuffer,
190        offset: wgt::BufferAddress,
191        draw_count: u32,
192    );
193    /// Dynamic version of [CommandEncoder::draw_indirect_count]
194    unsafe fn draw_indirect_count(
195        &mut self,
196        buffer: &dyn DynBuffer,
197        offset: wgt::BufferAddress,
198        count_buffer: &dyn DynBuffer,
199        count_offset: wgt::BufferAddress,
200        max_count: u32,
201    );
202    /// Dynamic version of [CommandEncoder::draw_indexed_indirect_count]
203    unsafe fn draw_indexed_indirect_count(
204        &mut self,
205        buffer: &dyn DynBuffer,
206        offset: wgt::BufferAddress,
207        count_buffer: &dyn DynBuffer,
208        count_offset: wgt::BufferAddress,
209        max_count: u32,
210    );
211    /// Dynamic version of [CommandEncoder::draw_mesh_tasks_indirect_count]
212    unsafe fn draw_mesh_tasks_indirect_count(
213        &mut self,
214        buffer: &dyn DynBuffer,
215        offset: wgt::BufferAddress,
216        count_buffer: &dyn DynBuffer,
217        count_offset: wgt::BufferAddress,
218        max_count: u32,
219    );
220    unsafe fn encode_deferred_multi_draws(&mut self);
221
222    /// Dynamic version of [CommandEncoder::begin_compute_pass]
223    unsafe fn begin_compute_pass(&mut self, desc: &ComputePassDescriptor<dyn DynQuerySet>);
224    /// Dynamic version of [CommandEncoder::end_compute_pass]
225    unsafe fn end_compute_pass(&mut self);
226
227    /// Dynamic version of [CommandEncoder::set_compute_pipeline]
228    unsafe fn set_compute_pipeline(&mut self, pipeline: &dyn DynComputePipeline);
229
230    /// Dynamic version of [CommandEncoder::dispatch_workgroups]
231    unsafe fn dispatch_workgroups(&mut self, count: [u32; 3]);
232    /// Dynamic version of [CommandEncoder::dispatch_workgroups_indirect]
233    unsafe fn dispatch_workgroups_indirect(
234        &mut self,
235        buffer: &dyn DynBuffer,
236        offset: wgt::BufferAddress,
237    );
238
239    /// Dynamic version of [CommandEncoder::begin_ray_tracing_pass]
240    unsafe fn begin_ray_tracing_pass(&mut self, desc: &RayTracingPassDescriptor);
241    /// Dynamic version of [CommandEncoder::end_ray_tracing_pass]
242    unsafe fn end_ray_tracing_pass(&mut self);
243
244    /// Dynamic version of [CommandEncoder::trace_rays]
245    unsafe fn trace_rays(
246        &mut self,
247        count: [u32; 3],
248        ray_generation_group_data: crate::PipelineGroupData<dyn DynBuffer>,
249        miss_group_data: crate::PipelineGroupData<dyn DynBuffer>,
250        intersection_group_data: crate::PipelineGroupData<dyn DynBuffer>,
251    );
252
253    /// Dynamic version of [CommandEncoder::trace_rays_indirect]
254    unsafe fn trace_rays_indirect(
255        &mut self,
256        buffer: &dyn DynBuffer,
257        offset: wgt::BufferAddress,
258        ray_generation_group_data: crate::PipelineGroupData<dyn DynBuffer>,
259        miss_group_data: crate::PipelineGroupData<dyn DynBuffer>,
260        intersection_group_data: crate::PipelineGroupData<dyn DynBuffer>,
261    );
262
263    /// Dynamic version of [CommandEncoder::set_ray_tracing_pipeline]
264    unsafe fn set_ray_tracing_pipeline(&mut self, pipeline: &dyn DynRayTracingPipeline);
265
266    /// Dynamic version of [CommandEncoder::build_acceleration_structures]
267    unsafe fn build_acceleration_structures<'a>(
268        &mut self,
269        descriptors: &'a [BuildAccelerationStructureDescriptor<
270            'a,
271            dyn DynBuffer,
272            dyn DynAccelerationStructure,
273        >],
274    );
275    /// Dynamic version of [CommandEncoder::place_acceleration_structure_barrier]
276    unsafe fn place_acceleration_structure_barrier(
277        &mut self,
278        barrier: AccelerationStructureBarrier,
279    );
280    /// Dynamic version of [CommandEncoder::copy_acceleration_structure_to_acceleration_structure]
281    unsafe fn copy_acceleration_structure_to_acceleration_structure(
282        &mut self,
283        src: &dyn DynAccelerationStructure,
284        dst: &dyn DynAccelerationStructure,
285        copy: wgt::AccelerationStructureCopy,
286    );
287    /// Dynamic version of [CommandEncoder::read_acceleration_structure_compact_size]
288    unsafe fn read_acceleration_structure_compact_size(
289        &mut self,
290        acceleration_structure: &dyn DynAccelerationStructure,
291        buf: &dyn DynBuffer,
292    );
293    /// Dynamic version of [CommandEncoder::set_acceleration_structure_dependencies]
294    unsafe fn set_acceleration_structure_dependencies(
295        &self,
296        command_buffers: &[Box<dyn DynCommandBuffer>],
297        dependencies: &[&dyn DynAccelerationStructure],
298    );
299}
300
301impl<C: CommandEncoder + DynResource> DynCommandEncoder for C {
302    unsafe fn begin_encoding(&mut self, label: Label) -> Result<(), DeviceError> {
303        unsafe { C::begin_encoding(self, label) }
304    }
305
306    unsafe fn discard_encoding(&mut self) {
307        unsafe { C::discard_encoding(self) }
308    }
309
310    unsafe fn end_encoding(&mut self) -> Result<Box<dyn DynCommandBuffer>, DeviceError> {
311        unsafe { C::end_encoding(self) }.map(|cb| {
312            let boxed_command_buffer: Box<<C::A as Api>::CommandBuffer> = Box::new(cb);
313            let boxed_command_buffer: Box<dyn DynCommandBuffer> = boxed_command_buffer;
314            boxed_command_buffer
315        })
316    }
317
318    unsafe fn reset_all(&mut self, command_buffers: Vec<Box<dyn DynCommandBuffer>>) {
319        unsafe { C::reset_all(self, command_buffers.into_iter().map(|cb| cb.unbox())) }
320    }
321
322    unsafe fn transition_buffers(&mut self, barriers: &[BufferBarrier<'_, dyn DynBuffer>]) {
323        let barriers = barriers.iter().map(|barrier| BufferBarrier {
324            buffer: barrier.buffer.expect_downcast_ref(),
325            usage: barrier.usage.clone(),
326        });
327        unsafe { self.transition_buffers(barriers) };
328    }
329
330    unsafe fn transition_textures(&mut self, barriers: &[TextureBarrier<'_, dyn DynTexture>]) {
331        let barriers = barriers.iter().map(|barrier| TextureBarrier {
332            texture: barrier.texture.expect_downcast_ref(),
333            usage: barrier.usage.clone(),
334            range: barrier.range,
335            queue_family_ownership_transfer: barrier.queue_family_ownership_transfer,
336        });
337        unsafe { self.transition_textures(barriers) };
338    }
339
340    unsafe fn clear_buffer(&mut self, buffer: &dyn DynBuffer, range: MemoryRange) {
341        let buffer = buffer.expect_downcast_ref();
342        unsafe { C::clear_buffer(self, buffer, range) };
343    }
344
345    unsafe fn copy_buffer_to_buffer(
346        &mut self,
347        src: &dyn DynBuffer,
348        dst: &dyn DynBuffer,
349        regions: &[BufferCopy],
350    ) {
351        let src = src.expect_downcast_ref();
352        let dst = dst.expect_downcast_ref();
353        unsafe {
354            C::copy_buffer_to_buffer(self, src, dst, regions.iter().copied());
355        }
356    }
357
358    unsafe fn copy_texture_to_texture(
359        &mut self,
360        src: &dyn DynTexture,
361        src_usage: wgt::TextureUses,
362        dst: &dyn DynTexture,
363        regions: &[TextureCopy],
364    ) {
365        let src = src.expect_downcast_ref();
366        let dst = dst.expect_downcast_ref();
367        unsafe {
368            C::copy_texture_to_texture(self, src, src_usage, dst, regions.iter().cloned());
369        }
370    }
371
372    unsafe fn copy_buffer_to_texture(
373        &mut self,
374        src: &dyn DynBuffer,
375        dst: &dyn DynTexture,
376        regions: &[BufferTextureCopy],
377    ) {
378        let src = src.expect_downcast_ref();
379        let dst = dst.expect_downcast_ref();
380        unsafe {
381            C::copy_buffer_to_texture(self, src, dst, regions.iter().cloned());
382        }
383    }
384
385    unsafe fn copy_texture_to_buffer(
386        &mut self,
387        src: &dyn DynTexture,
388        src_usage: wgt::TextureUses,
389        dst: &dyn DynBuffer,
390        regions: &[BufferTextureCopy],
391    ) {
392        let src = src.expect_downcast_ref();
393        let dst = dst.expect_downcast_ref();
394        unsafe {
395            C::copy_texture_to_buffer(self, src, src_usage, dst, regions.iter().cloned());
396        }
397    }
398
399    unsafe fn set_bind_group(
400        &mut self,
401        layout: &dyn DynPipelineLayout,
402        index: u32,
403        group: &dyn DynBindGroup,
404        dynamic_offsets: &[wgt::DynamicOffset],
405    ) {
406        let layout = layout.expect_downcast_ref();
407        let group = group.expect_downcast_ref();
408        unsafe { C::set_bind_group(self, layout, index, group, dynamic_offsets) };
409    }
410
411    unsafe fn set_immediates(
412        &mut self,
413        layout: &dyn DynPipelineLayout,
414        offset_bytes: u32,
415        data: &[u32],
416    ) {
417        let layout = layout.expect_downcast_ref();
418        unsafe { C::set_immediates(self, layout, offset_bytes, data) };
419    }
420
421    unsafe fn insert_debug_marker(&mut self, label: &str) {
422        unsafe {
423            C::insert_debug_marker(self, label);
424        }
425    }
426
427    unsafe fn begin_debug_marker(&mut self, group_label: &str) {
428        unsafe {
429            C::begin_debug_marker(self, group_label);
430        }
431    }
432
433    unsafe fn end_debug_marker(&mut self) {
434        unsafe {
435            C::end_debug_marker(self);
436        }
437    }
438
439    unsafe fn begin_query(&mut self, set: &dyn DynQuerySet, index: u32) {
440        let set = set.expect_downcast_ref();
441        unsafe { C::begin_query(self, set, index) };
442    }
443
444    unsafe fn end_query(&mut self, set: &dyn DynQuerySet, index: u32) {
445        let set = set.expect_downcast_ref();
446        unsafe { C::end_query(self, set, index) };
447    }
448
449    unsafe fn write_timestamp(&mut self, set: &dyn DynQuerySet, index: u32) {
450        let set = set.expect_downcast_ref();
451        unsafe { C::write_timestamp(self, set, index) };
452    }
453
454    unsafe fn reset_queries(&mut self, set: &dyn DynQuerySet, range: Range<u32>) {
455        let set = set.expect_downcast_ref();
456        unsafe { C::reset_queries(self, set, range) };
457    }
458
459    unsafe fn copy_query_results(
460        &mut self,
461        set: &dyn DynQuerySet,
462        range: Range<u32>,
463        buffer: &dyn DynBuffer,
464        offset: wgt::BufferAddress,
465        stride: wgt::BufferSize,
466    ) {
467        let set = set.expect_downcast_ref();
468        let buffer = buffer.expect_downcast_ref();
469        unsafe { C::copy_query_results(self, set, range, buffer, offset, stride) };
470    }
471
472    unsafe fn begin_render_pass(
473        &mut self,
474        desc: &RenderPassDescriptor<dyn DynQuerySet, dyn DynTextureView>,
475    ) -> Result<(), DeviceError> {
476        let color_attachments = desc
477            .color_attachments
478            .iter()
479            .map(|attachment| {
480                attachment
481                    .as_ref()
482                    .map(|attachment| attachment.expect_downcast())
483            })
484            .collect::<Vec<_>>();
485
486        let desc: RenderPassDescriptor<<C::A as Api>::QuerySet, <C::A as Api>::TextureView> =
487            RenderPassDescriptor {
488                label: desc.label,
489                extent: desc.extent,
490                sample_count: desc.sample_count,
491                color_attachments: &color_attachments,
492                depth_stencil_attachment: desc
493                    .depth_stencil_attachment
494                    .as_ref()
495                    .map(|ds| ds.expect_downcast()),
496                multiview_mask: desc.multiview_mask,
497                timestamp_writes: desc
498                    .timestamp_writes
499                    .as_ref()
500                    .map(|writes| writes.expect_downcast()),
501                occlusion_query_set: desc
502                    .occlusion_query_set
503                    .map(|set| set.expect_downcast_ref()),
504            };
505        unsafe { C::begin_render_pass(self, &desc) }
506    }
507
508    unsafe fn end_render_pass(&mut self) {
509        unsafe {
510            C::end_render_pass(self);
511        }
512    }
513
514    unsafe fn set_viewport(&mut self, rect: &Rect<f32>, depth_range: Range<f32>) {
515        unsafe {
516            C::set_viewport(self, rect, depth_range);
517        }
518    }
519
520    unsafe fn set_scissor_rect(&mut self, rect: &Rect<u32>) {
521        unsafe {
522            C::set_scissor_rect(self, rect);
523        }
524    }
525
526    unsafe fn set_stencil_reference(&mut self, value: u32) {
527        unsafe {
528            C::set_stencil_reference(self, value);
529        }
530    }
531
532    unsafe fn set_blend_constants(&mut self, color: &[f32; 4]) {
533        unsafe { C::set_blend_constants(self, color) };
534    }
535
536    unsafe fn draw(
537        &mut self,
538        first_vertex: u32,
539        vertex_count: u32,
540        first_instance: u32,
541        instance_count: u32,
542    ) {
543        unsafe {
544            C::draw(
545                self,
546                first_vertex,
547                vertex_count,
548                first_instance,
549                instance_count,
550            )
551        };
552    }
553
554    unsafe fn draw_indexed(
555        &mut self,
556        first_index: u32,
557        index_count: u32,
558        base_vertex: i32,
559        first_instance: u32,
560        instance_count: u32,
561    ) {
562        unsafe {
563            C::draw_indexed(
564                self,
565                first_index,
566                index_count,
567                base_vertex,
568                first_instance,
569                instance_count,
570            )
571        };
572    }
573
574    unsafe fn draw_mesh_tasks(
575        &mut self,
576        group_count_x: u32,
577        group_count_y: u32,
578        group_count_z: u32,
579    ) {
580        unsafe { C::draw_mesh_tasks(self, group_count_x, group_count_y, group_count_z) };
581    }
582
583    unsafe fn draw_indirect(
584        &mut self,
585        buffer: &dyn DynBuffer,
586        offset: wgt::BufferAddress,
587        draw_count: u32,
588    ) {
589        let buffer = buffer.expect_downcast_ref();
590        unsafe { C::draw_indirect(self, buffer, offset, draw_count) };
591    }
592
593    unsafe fn draw_indexed_indirect(
594        &mut self,
595        buffer: &dyn DynBuffer,
596        offset: wgt::BufferAddress,
597        draw_count: u32,
598    ) {
599        let buffer = buffer.expect_downcast_ref();
600        unsafe { C::draw_indexed_indirect(self, buffer, offset, draw_count) };
601    }
602
603    unsafe fn draw_mesh_tasks_indirect(
604        &mut self,
605        buffer: &dyn DynBuffer,
606        offset: wgt::BufferAddress,
607        draw_count: u32,
608    ) {
609        let buffer = buffer.expect_downcast_ref();
610        unsafe { C::draw_mesh_tasks_indirect(self, buffer, offset, draw_count) };
611    }
612
613    unsafe fn draw_indirect_count(
614        &mut self,
615        buffer: &dyn DynBuffer,
616        offset: wgt::BufferAddress,
617        count_buffer: &dyn DynBuffer,
618        count_offset: wgt::BufferAddress,
619        max_count: u32,
620    ) {
621        let buffer = buffer.expect_downcast_ref();
622        let count_buffer = count_buffer.expect_downcast_ref();
623        unsafe {
624            C::draw_indirect_count(self, buffer, offset, count_buffer, count_offset, max_count)
625        };
626    }
627
628    unsafe fn draw_indexed_indirect_count(
629        &mut self,
630        buffer: &dyn DynBuffer,
631        offset: wgt::BufferAddress,
632        count_buffer: &dyn DynBuffer,
633        count_offset: wgt::BufferAddress,
634        max_count: u32,
635    ) {
636        let buffer = buffer.expect_downcast_ref();
637        let count_buffer = count_buffer.expect_downcast_ref();
638        unsafe {
639            C::draw_indexed_indirect_count(
640                self,
641                buffer,
642                offset,
643                count_buffer,
644                count_offset,
645                max_count,
646            )
647        };
648    }
649
650    unsafe fn draw_mesh_tasks_indirect_count(
651        &mut self,
652        buffer: &dyn DynBuffer,
653        offset: wgt::BufferAddress,
654        count_buffer: &dyn DynBuffer,
655        count_offset: wgt::BufferAddress,
656        max_count: u32,
657    ) {
658        let buffer = buffer.expect_downcast_ref();
659        let count_buffer = count_buffer.expect_downcast_ref();
660        unsafe {
661            C::draw_mesh_tasks_indirect_count(
662                self,
663                buffer,
664                offset,
665                count_buffer,
666                count_offset,
667                max_count,
668            )
669        };
670    }
671
672    unsafe fn encode_deferred_multi_draws(&mut self) {
673        unsafe { C::encode_deferred_multi_draws(self) };
674    }
675
676    unsafe fn begin_compute_pass(&mut self, desc: &ComputePassDescriptor<dyn DynQuerySet>) {
677        let desc = ComputePassDescriptor {
678            label: desc.label,
679            timestamp_writes: desc
680                .timestamp_writes
681                .as_ref()
682                .map(|writes| writes.expect_downcast()),
683        };
684        unsafe { C::begin_compute_pass(self, &desc) };
685    }
686
687    unsafe fn end_compute_pass(&mut self) {
688        unsafe { C::end_compute_pass(self) };
689    }
690
691    unsafe fn set_compute_pipeline(&mut self, pipeline: &dyn DynComputePipeline) {
692        let pipeline = pipeline.expect_downcast_ref();
693        unsafe { C::set_compute_pipeline(self, pipeline) };
694    }
695
696    unsafe fn dispatch_workgroups(&mut self, count: [u32; 3]) {
697        unsafe { C::dispatch_workgroups(self, count) };
698    }
699
700    unsafe fn dispatch_workgroups_indirect(
701        &mut self,
702        buffer: &dyn DynBuffer,
703        offset: wgt::BufferAddress,
704    ) {
705        let buffer = buffer.expect_downcast_ref();
706        unsafe { C::dispatch_workgroups_indirect(self, buffer, offset) };
707    }
708
709    unsafe fn set_render_pipeline(&mut self, pipeline: &dyn DynRenderPipeline) {
710        let pipeline = pipeline.expect_downcast_ref();
711        unsafe { C::set_render_pipeline(self, pipeline) };
712    }
713
714    unsafe fn set_index_buffer<'a>(
715        &mut self,
716        binding: BufferBinding<'a, dyn DynBuffer, wgt::BufferAddress>,
717        format: wgt::IndexFormat,
718    ) {
719        let binding = binding.expect_downcast();
720        unsafe { self.set_index_buffer(binding, format) };
721    }
722
723    unsafe fn set_vertex_buffer<'a>(
724        &mut self,
725        index: u32,
726        binding: BufferBinding<'a, dyn DynBuffer, wgt::BufferAddress>,
727    ) {
728        let binding = binding.expect_downcast();
729        unsafe { self.set_vertex_buffer(index, binding) };
730    }
731
732    unsafe fn begin_ray_tracing_pass(&mut self, desc: &RayTracingPassDescriptor) {
733        let desc = RayTracingPassDescriptor { label: desc.label };
734        unsafe { C::begin_ray_tracing_pass(self, &desc) };
735    }
736
737    unsafe fn end_ray_tracing_pass(&mut self) {
738        unsafe { C::end_ray_tracing_pass(self) };
739    }
740
741    unsafe fn set_ray_tracing_pipeline(&mut self, pipeline: &dyn DynRayTracingPipeline) {
742        let pipeline = pipeline.expect_downcast_ref();
743        unsafe { C::set_ray_tracing_pipeline(self, pipeline) };
744    }
745
746    unsafe fn trace_rays<'a>(
747        &mut self,
748        count: [u32; 3],
749        ray_generation_group_data: crate::PipelineGroupData<'a, dyn DynBuffer>,
750        miss_group_data: crate::PipelineGroupData<'a, dyn DynBuffer>,
751        intersection_group_data: crate::PipelineGroupData<'a, dyn DynBuffer>,
752    ) {
753        let downcast_group_data =
754            |data: crate::PipelineGroupData<'a, dyn DynBuffer>| crate::PipelineGroupData {
755                buffer: data.buffer.expect_downcast_ref(),
756                offset: data.offset,
757                stride: data.stride,
758                count: data.count,
759            };
760
761        unsafe {
762            C::trace_rays(
763                self,
764                count,
765                downcast_group_data(ray_generation_group_data),
766                downcast_group_data(miss_group_data),
767                downcast_group_data(intersection_group_data),
768            );
769        }
770    }
771
772    unsafe fn trace_rays_indirect<'a>(
773        &mut self,
774        buffer: &dyn DynBuffer,
775        offset: wgt::BufferAddress,
776        ray_generation_group_data: crate::PipelineGroupData<'a, dyn DynBuffer>,
777        miss_group_data: crate::PipelineGroupData<'a, dyn DynBuffer>,
778        intersection_group_data: crate::PipelineGroupData<'a, dyn DynBuffer>,
779    ) {
780        let downcast_group_data =
781            |data: crate::PipelineGroupData<'a, dyn DynBuffer>| crate::PipelineGroupData {
782                buffer: data.buffer.expect_downcast_ref(),
783                offset: data.offset,
784                stride: data.stride,
785                count: data.count,
786            };
787
788        let buffer = buffer.expect_downcast_ref();
789        unsafe {
790            C::trace_rays_indirect(
791                self,
792                buffer,
793                offset,
794                downcast_group_data(ray_generation_group_data),
795                downcast_group_data(miss_group_data),
796                downcast_group_data(intersection_group_data),
797            );
798        }
799    }
800
801    unsafe fn build_acceleration_structures<'a>(
802        &mut self,
803        descriptors: &'a [BuildAccelerationStructureDescriptor<
804            'a,
805            dyn DynBuffer,
806            dyn DynAccelerationStructure,
807        >],
808    ) {
809        // Need to collect entries here so we can reference them in the descriptor.
810        // TODO: API should be redesigned to avoid this and other descriptor copies that happen due to the dyn api.
811        let descriptor_entries = descriptors
812            .iter()
813            .map(|d| d.entries.expect_downcast())
814            .collect::<Vec<_>>();
815        let descriptors = descriptors
816            .iter()
817            .zip(descriptor_entries.iter())
818            .map(|(d, entries)| BuildAccelerationStructureDescriptor::<
819                <C::A as Api>::Buffer,
820                <C::A as Api>::AccelerationStructure,
821            > {
822                entries,
823                mode: d.mode,
824                flags: d.flags,
825                source_acceleration_structure: d
826                    .source_acceleration_structure
827                    .map(|a| a.expect_downcast_ref()),
828                destination_acceleration_structure: d
829                    .destination_acceleration_structure
830                    .expect_downcast_ref(),
831                scratch_buffer: d.scratch_buffer.expect_downcast_ref(),
832                scratch_buffer_offset: d.scratch_buffer_offset,
833            });
834        unsafe { C::build_acceleration_structures(self, descriptors.len() as _, descriptors) };
835    }
836
837    unsafe fn place_acceleration_structure_barrier(
838        &mut self,
839        barrier: AccelerationStructureBarrier,
840    ) {
841        unsafe { C::place_acceleration_structure_barrier(self, barrier) };
842    }
843
844    unsafe fn copy_acceleration_structure_to_acceleration_structure(
845        &mut self,
846        src: &dyn DynAccelerationStructure,
847        dst: &dyn DynAccelerationStructure,
848        copy: wgt::AccelerationStructureCopy,
849    ) {
850        let src = src.expect_downcast_ref();
851        let dst = dst.expect_downcast_ref();
852        unsafe { C::copy_acceleration_structure_to_acceleration_structure(self, src, dst, copy) };
853    }
854    unsafe fn read_acceleration_structure_compact_size(
855        &mut self,
856        acceleration_structure: &dyn DynAccelerationStructure,
857        buf: &dyn DynBuffer,
858    ) {
859        let acceleration_structure = acceleration_structure.expect_downcast_ref();
860        let buf = buf.expect_downcast_ref();
861        unsafe { C::read_acceleration_structure_compact_size(self, acceleration_structure, buf) }
862    }
863
864    unsafe fn set_acceleration_structure_dependencies(
865        &self,
866        command_buffers: &[Box<dyn DynCommandBuffer>],
867        dependencies: &[&dyn DynAccelerationStructure],
868    ) {
869        let command_buffers: Vec<&<C::A as Api>::CommandBuffer> = command_buffers
870            .iter()
871            .map(|command_buffer| command_buffer.expect_downcast_ref())
872            .collect();
873        let dependencies: Vec<&<C::A as Api>::AccelerationStructure> = dependencies
874            .iter()
875            .map(|dependency| dependency.expect_downcast_ref())
876            .collect();
877        unsafe { C::set_acceleration_structure_dependencies(&command_buffers, &dependencies) }
878    }
879}
880
881impl<'a> PassTimestampWrites<'a, dyn DynQuerySet> {
882    pub fn expect_downcast<B: DynQuerySet>(&self) -> PassTimestampWrites<'a, B> {
883        PassTimestampWrites {
884            query_set: self.query_set.expect_downcast_ref(),
885            beginning_of_pass_write_index: self.beginning_of_pass_write_index,
886            end_of_pass_write_index: self.end_of_pass_write_index,
887        }
888    }
889}
890
891impl<'a> Attachment<'a, dyn DynTextureView> {
892    pub fn expect_downcast<B: DynTextureView>(&self) -> Attachment<'a, B> {
893        Attachment {
894            view: self.view.expect_downcast_ref(),
895            usage: self.usage,
896        }
897    }
898}
899
900impl<'a> ColorAttachment<'a, dyn DynTextureView> {
901    pub fn expect_downcast<B: DynTextureView>(&self) -> ColorAttachment<'a, B> {
902        ColorAttachment {
903            target: self.target.expect_downcast(),
904            depth_slice: self.depth_slice,
905            resolve_target: self.resolve_target.as_ref().map(|rt| rt.expect_downcast()),
906            ops: self.ops,
907            clear_value: self.clear_value,
908        }
909    }
910}
911
912impl<'a> DepthStencilAttachment<'a, dyn DynTextureView> {
913    pub fn expect_downcast<B: DynTextureView>(&self) -> DepthStencilAttachment<'a, B> {
914        DepthStencilAttachment {
915            target: self.target.expect_downcast(),
916            depth_ops: self.depth_ops,
917            stencil_ops: self.stencil_ops,
918            clear_value: self.clear_value,
919            depth_read_only: self.depth_read_only,
920            stencil_read_only: self.stencil_read_only,
921        }
922    }
923}