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::set_ray_tracing_pipeline]
254    unsafe fn set_ray_tracing_pipeline(&mut self, pipeline: &dyn DynRayTracingPipeline);
255
256    /// Dynamic version of [CommandEncoder::build_acceleration_structures]
257    unsafe fn build_acceleration_structures<'a>(
258        &mut self,
259        descriptors: &'a [BuildAccelerationStructureDescriptor<
260            'a,
261            dyn DynBuffer,
262            dyn DynAccelerationStructure,
263        >],
264    );
265    /// Dynamic version of [CommandEncoder::place_acceleration_structure_barrier]
266    unsafe fn place_acceleration_structure_barrier(
267        &mut self,
268        barrier: AccelerationStructureBarrier,
269    );
270    /// Dynamic version of [CommandEncoder::copy_acceleration_structure_to_acceleration_structure]
271    unsafe fn copy_acceleration_structure_to_acceleration_structure(
272        &mut self,
273        src: &dyn DynAccelerationStructure,
274        dst: &dyn DynAccelerationStructure,
275        copy: wgt::AccelerationStructureCopy,
276    );
277    /// Dynamic version of [CommandEncoder::read_acceleration_structure_compact_size]
278    unsafe fn read_acceleration_structure_compact_size(
279        &mut self,
280        acceleration_structure: &dyn DynAccelerationStructure,
281        buf: &dyn DynBuffer,
282    );
283    /// Dynamic version of [CommandEncoder::set_acceleration_structure_dependencies]
284    unsafe fn set_acceleration_structure_dependencies(
285        &self,
286        command_buffers: &[Box<dyn DynCommandBuffer>],
287        dependencies: &[&dyn DynAccelerationStructure],
288    );
289}
290
291impl<C: CommandEncoder + DynResource> DynCommandEncoder for C {
292    unsafe fn begin_encoding(&mut self, label: Label) -> Result<(), DeviceError> {
293        unsafe { C::begin_encoding(self, label) }
294    }
295
296    unsafe fn discard_encoding(&mut self) {
297        unsafe { C::discard_encoding(self) }
298    }
299
300    unsafe fn end_encoding(&mut self) -> Result<Box<dyn DynCommandBuffer>, DeviceError> {
301        unsafe { C::end_encoding(self) }.map(|cb| {
302            let boxed_command_buffer: Box<<C::A as Api>::CommandBuffer> = Box::new(cb);
303            let boxed_command_buffer: Box<dyn DynCommandBuffer> = boxed_command_buffer;
304            boxed_command_buffer
305        })
306    }
307
308    unsafe fn reset_all(&mut self, command_buffers: Vec<Box<dyn DynCommandBuffer>>) {
309        unsafe { C::reset_all(self, command_buffers.into_iter().map(|cb| cb.unbox())) }
310    }
311
312    unsafe fn transition_buffers(&mut self, barriers: &[BufferBarrier<'_, dyn DynBuffer>]) {
313        let barriers = barriers.iter().map(|barrier| BufferBarrier {
314            buffer: barrier.buffer.expect_downcast_ref(),
315            usage: barrier.usage.clone(),
316        });
317        unsafe { self.transition_buffers(barriers) };
318    }
319
320    unsafe fn transition_textures(&mut self, barriers: &[TextureBarrier<'_, dyn DynTexture>]) {
321        let barriers = barriers.iter().map(|barrier| TextureBarrier {
322            texture: barrier.texture.expect_downcast_ref(),
323            usage: barrier.usage.clone(),
324            range: barrier.range,
325            queue_family_ownership_transfer: barrier.queue_family_ownership_transfer,
326        });
327        unsafe { self.transition_textures(barriers) };
328    }
329
330    unsafe fn clear_buffer(&mut self, buffer: &dyn DynBuffer, range: MemoryRange) {
331        let buffer = buffer.expect_downcast_ref();
332        unsafe { C::clear_buffer(self, buffer, range) };
333    }
334
335    unsafe fn copy_buffer_to_buffer(
336        &mut self,
337        src: &dyn DynBuffer,
338        dst: &dyn DynBuffer,
339        regions: &[BufferCopy],
340    ) {
341        let src = src.expect_downcast_ref();
342        let dst = dst.expect_downcast_ref();
343        unsafe {
344            C::copy_buffer_to_buffer(self, src, dst, regions.iter().copied());
345        }
346    }
347
348    unsafe fn copy_texture_to_texture(
349        &mut self,
350        src: &dyn DynTexture,
351        src_usage: wgt::TextureUses,
352        dst: &dyn DynTexture,
353        regions: &[TextureCopy],
354    ) {
355        let src = src.expect_downcast_ref();
356        let dst = dst.expect_downcast_ref();
357        unsafe {
358            C::copy_texture_to_texture(self, src, src_usage, dst, regions.iter().cloned());
359        }
360    }
361
362    unsafe fn copy_buffer_to_texture(
363        &mut self,
364        src: &dyn DynBuffer,
365        dst: &dyn DynTexture,
366        regions: &[BufferTextureCopy],
367    ) {
368        let src = src.expect_downcast_ref();
369        let dst = dst.expect_downcast_ref();
370        unsafe {
371            C::copy_buffer_to_texture(self, src, dst, regions.iter().cloned());
372        }
373    }
374
375    unsafe fn copy_texture_to_buffer(
376        &mut self,
377        src: &dyn DynTexture,
378        src_usage: wgt::TextureUses,
379        dst: &dyn DynBuffer,
380        regions: &[BufferTextureCopy],
381    ) {
382        let src = src.expect_downcast_ref();
383        let dst = dst.expect_downcast_ref();
384        unsafe {
385            C::copy_texture_to_buffer(self, src, src_usage, dst, regions.iter().cloned());
386        }
387    }
388
389    unsafe fn set_bind_group(
390        &mut self,
391        layout: &dyn DynPipelineLayout,
392        index: u32,
393        group: &dyn DynBindGroup,
394        dynamic_offsets: &[wgt::DynamicOffset],
395    ) {
396        let layout = layout.expect_downcast_ref();
397        let group = group.expect_downcast_ref();
398        unsafe { C::set_bind_group(self, layout, index, group, dynamic_offsets) };
399    }
400
401    unsafe fn set_immediates(
402        &mut self,
403        layout: &dyn DynPipelineLayout,
404        offset_bytes: u32,
405        data: &[u32],
406    ) {
407        let layout = layout.expect_downcast_ref();
408        unsafe { C::set_immediates(self, layout, offset_bytes, data) };
409    }
410
411    unsafe fn insert_debug_marker(&mut self, label: &str) {
412        unsafe {
413            C::insert_debug_marker(self, label);
414        }
415    }
416
417    unsafe fn begin_debug_marker(&mut self, group_label: &str) {
418        unsafe {
419            C::begin_debug_marker(self, group_label);
420        }
421    }
422
423    unsafe fn end_debug_marker(&mut self) {
424        unsafe {
425            C::end_debug_marker(self);
426        }
427    }
428
429    unsafe fn begin_query(&mut self, set: &dyn DynQuerySet, index: u32) {
430        let set = set.expect_downcast_ref();
431        unsafe { C::begin_query(self, set, index) };
432    }
433
434    unsafe fn end_query(&mut self, set: &dyn DynQuerySet, index: u32) {
435        let set = set.expect_downcast_ref();
436        unsafe { C::end_query(self, set, index) };
437    }
438
439    unsafe fn write_timestamp(&mut self, set: &dyn DynQuerySet, index: u32) {
440        let set = set.expect_downcast_ref();
441        unsafe { C::write_timestamp(self, set, index) };
442    }
443
444    unsafe fn reset_queries(&mut self, set: &dyn DynQuerySet, range: Range<u32>) {
445        let set = set.expect_downcast_ref();
446        unsafe { C::reset_queries(self, set, range) };
447    }
448
449    unsafe fn copy_query_results(
450        &mut self,
451        set: &dyn DynQuerySet,
452        range: Range<u32>,
453        buffer: &dyn DynBuffer,
454        offset: wgt::BufferAddress,
455        stride: wgt::BufferSize,
456    ) {
457        let set = set.expect_downcast_ref();
458        let buffer = buffer.expect_downcast_ref();
459        unsafe { C::copy_query_results(self, set, range, buffer, offset, stride) };
460    }
461
462    unsafe fn begin_render_pass(
463        &mut self,
464        desc: &RenderPassDescriptor<dyn DynQuerySet, dyn DynTextureView>,
465    ) -> Result<(), DeviceError> {
466        let color_attachments = desc
467            .color_attachments
468            .iter()
469            .map(|attachment| {
470                attachment
471                    .as_ref()
472                    .map(|attachment| attachment.expect_downcast())
473            })
474            .collect::<Vec<_>>();
475
476        let desc: RenderPassDescriptor<<C::A as Api>::QuerySet, <C::A as Api>::TextureView> =
477            RenderPassDescriptor {
478                label: desc.label,
479                extent: desc.extent,
480                sample_count: desc.sample_count,
481                color_attachments: &color_attachments,
482                depth_stencil_attachment: desc
483                    .depth_stencil_attachment
484                    .as_ref()
485                    .map(|ds| ds.expect_downcast()),
486                multiview_mask: desc.multiview_mask,
487                timestamp_writes: desc
488                    .timestamp_writes
489                    .as_ref()
490                    .map(|writes| writes.expect_downcast()),
491                occlusion_query_set: desc
492                    .occlusion_query_set
493                    .map(|set| set.expect_downcast_ref()),
494            };
495        unsafe { C::begin_render_pass(self, &desc) }
496    }
497
498    unsafe fn end_render_pass(&mut self) {
499        unsafe {
500            C::end_render_pass(self);
501        }
502    }
503
504    unsafe fn set_viewport(&mut self, rect: &Rect<f32>, depth_range: Range<f32>) {
505        unsafe {
506            C::set_viewport(self, rect, depth_range);
507        }
508    }
509
510    unsafe fn set_scissor_rect(&mut self, rect: &Rect<u32>) {
511        unsafe {
512            C::set_scissor_rect(self, rect);
513        }
514    }
515
516    unsafe fn set_stencil_reference(&mut self, value: u32) {
517        unsafe {
518            C::set_stencil_reference(self, value);
519        }
520    }
521
522    unsafe fn set_blend_constants(&mut self, color: &[f32; 4]) {
523        unsafe { C::set_blend_constants(self, color) };
524    }
525
526    unsafe fn draw(
527        &mut self,
528        first_vertex: u32,
529        vertex_count: u32,
530        first_instance: u32,
531        instance_count: u32,
532    ) {
533        unsafe {
534            C::draw(
535                self,
536                first_vertex,
537                vertex_count,
538                first_instance,
539                instance_count,
540            )
541        };
542    }
543
544    unsafe fn draw_indexed(
545        &mut self,
546        first_index: u32,
547        index_count: u32,
548        base_vertex: i32,
549        first_instance: u32,
550        instance_count: u32,
551    ) {
552        unsafe {
553            C::draw_indexed(
554                self,
555                first_index,
556                index_count,
557                base_vertex,
558                first_instance,
559                instance_count,
560            )
561        };
562    }
563
564    unsafe fn draw_mesh_tasks(
565        &mut self,
566        group_count_x: u32,
567        group_count_y: u32,
568        group_count_z: u32,
569    ) {
570        unsafe { C::draw_mesh_tasks(self, group_count_x, group_count_y, group_count_z) };
571    }
572
573    unsafe fn draw_indirect(
574        &mut self,
575        buffer: &dyn DynBuffer,
576        offset: wgt::BufferAddress,
577        draw_count: u32,
578    ) {
579        let buffer = buffer.expect_downcast_ref();
580        unsafe { C::draw_indirect(self, buffer, offset, draw_count) };
581    }
582
583    unsafe fn draw_indexed_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_indexed_indirect(self, buffer, offset, draw_count) };
591    }
592
593    unsafe fn draw_mesh_tasks_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_mesh_tasks_indirect(self, buffer, offset, draw_count) };
601    }
602
603    unsafe fn draw_indirect_count(
604        &mut self,
605        buffer: &dyn DynBuffer,
606        offset: wgt::BufferAddress,
607        count_buffer: &dyn DynBuffer,
608        count_offset: wgt::BufferAddress,
609        max_count: u32,
610    ) {
611        let buffer = buffer.expect_downcast_ref();
612        let count_buffer = count_buffer.expect_downcast_ref();
613        unsafe {
614            C::draw_indirect_count(self, buffer, offset, count_buffer, count_offset, max_count)
615        };
616    }
617
618    unsafe fn draw_indexed_indirect_count(
619        &mut self,
620        buffer: &dyn DynBuffer,
621        offset: wgt::BufferAddress,
622        count_buffer: &dyn DynBuffer,
623        count_offset: wgt::BufferAddress,
624        max_count: u32,
625    ) {
626        let buffer = buffer.expect_downcast_ref();
627        let count_buffer = count_buffer.expect_downcast_ref();
628        unsafe {
629            C::draw_indexed_indirect_count(
630                self,
631                buffer,
632                offset,
633                count_buffer,
634                count_offset,
635                max_count,
636            )
637        };
638    }
639
640    unsafe fn draw_mesh_tasks_indirect_count(
641        &mut self,
642        buffer: &dyn DynBuffer,
643        offset: wgt::BufferAddress,
644        count_buffer: &dyn DynBuffer,
645        count_offset: wgt::BufferAddress,
646        max_count: u32,
647    ) {
648        let buffer = buffer.expect_downcast_ref();
649        let count_buffer = count_buffer.expect_downcast_ref();
650        unsafe {
651            C::draw_mesh_tasks_indirect_count(
652                self,
653                buffer,
654                offset,
655                count_buffer,
656                count_offset,
657                max_count,
658            )
659        };
660    }
661
662    unsafe fn encode_deferred_multi_draws(&mut self) {
663        unsafe { C::encode_deferred_multi_draws(self) };
664    }
665
666    unsafe fn begin_compute_pass(&mut self, desc: &ComputePassDescriptor<dyn DynQuerySet>) {
667        let desc = ComputePassDescriptor {
668            label: desc.label,
669            timestamp_writes: desc
670                .timestamp_writes
671                .as_ref()
672                .map(|writes| writes.expect_downcast()),
673        };
674        unsafe { C::begin_compute_pass(self, &desc) };
675    }
676
677    unsafe fn end_compute_pass(&mut self) {
678        unsafe { C::end_compute_pass(self) };
679    }
680
681    unsafe fn set_compute_pipeline(&mut self, pipeline: &dyn DynComputePipeline) {
682        let pipeline = pipeline.expect_downcast_ref();
683        unsafe { C::set_compute_pipeline(self, pipeline) };
684    }
685
686    unsafe fn dispatch_workgroups(&mut self, count: [u32; 3]) {
687        unsafe { C::dispatch_workgroups(self, count) };
688    }
689
690    unsafe fn dispatch_workgroups_indirect(
691        &mut self,
692        buffer: &dyn DynBuffer,
693        offset: wgt::BufferAddress,
694    ) {
695        let buffer = buffer.expect_downcast_ref();
696        unsafe { C::dispatch_workgroups_indirect(self, buffer, offset) };
697    }
698
699    unsafe fn set_render_pipeline(&mut self, pipeline: &dyn DynRenderPipeline) {
700        let pipeline = pipeline.expect_downcast_ref();
701        unsafe { C::set_render_pipeline(self, pipeline) };
702    }
703
704    unsafe fn set_index_buffer<'a>(
705        &mut self,
706        binding: BufferBinding<'a, dyn DynBuffer, wgt::BufferAddress>,
707        format: wgt::IndexFormat,
708    ) {
709        let binding = binding.expect_downcast();
710        unsafe { self.set_index_buffer(binding, format) };
711    }
712
713    unsafe fn set_vertex_buffer<'a>(
714        &mut self,
715        index: u32,
716        binding: BufferBinding<'a, dyn DynBuffer, wgt::BufferAddress>,
717    ) {
718        let binding = binding.expect_downcast();
719        unsafe { self.set_vertex_buffer(index, binding) };
720    }
721
722    unsafe fn begin_ray_tracing_pass(&mut self, desc: &RayTracingPassDescriptor) {
723        let desc = RayTracingPassDescriptor { label: desc.label };
724        unsafe { C::begin_ray_tracing_pass(self, &desc) };
725    }
726
727    unsafe fn end_ray_tracing_pass(&mut self) {
728        unsafe { C::end_ray_tracing_pass(self) };
729    }
730
731    unsafe fn set_ray_tracing_pipeline(&mut self, pipeline: &dyn DynRayTracingPipeline) {
732        let pipeline = pipeline.expect_downcast_ref();
733        unsafe { C::set_ray_tracing_pipeline(self, pipeline) };
734    }
735
736    unsafe fn trace_rays<'a>(
737        &mut self,
738        count: [u32; 3],
739        ray_generation_group_data: crate::PipelineGroupData<'a, dyn DynBuffer>,
740        miss_group_data: crate::PipelineGroupData<'a, dyn DynBuffer>,
741        intersection_group_data: crate::PipelineGroupData<'a, dyn DynBuffer>,
742    ) {
743        let downcast_group_data =
744            |data: crate::PipelineGroupData<'a, dyn DynBuffer>| crate::PipelineGroupData {
745                buffer: data.buffer.expect_downcast_ref(),
746                offset: data.offset,
747                stride: data.stride,
748                count: data.count,
749            };
750
751        unsafe {
752            C::trace_rays(
753                self,
754                count,
755                downcast_group_data(ray_generation_group_data),
756                downcast_group_data(miss_group_data),
757                downcast_group_data(intersection_group_data),
758            );
759        }
760    }
761
762    unsafe fn build_acceleration_structures<'a>(
763        &mut self,
764        descriptors: &'a [BuildAccelerationStructureDescriptor<
765            'a,
766            dyn DynBuffer,
767            dyn DynAccelerationStructure,
768        >],
769    ) {
770        // Need to collect entries here so we can reference them in the descriptor.
771        // TODO: API should be redesigned to avoid this and other descriptor copies that happen due to the dyn api.
772        let descriptor_entries = descriptors
773            .iter()
774            .map(|d| d.entries.expect_downcast())
775            .collect::<Vec<_>>();
776        let descriptors = descriptors
777            .iter()
778            .zip(descriptor_entries.iter())
779            .map(|(d, entries)| BuildAccelerationStructureDescriptor::<
780                <C::A as Api>::Buffer,
781                <C::A as Api>::AccelerationStructure,
782            > {
783                entries,
784                mode: d.mode,
785                flags: d.flags,
786                source_acceleration_structure: d
787                    .source_acceleration_structure
788                    .map(|a| a.expect_downcast_ref()),
789                destination_acceleration_structure: d
790                    .destination_acceleration_structure
791                    .expect_downcast_ref(),
792                scratch_buffer: d.scratch_buffer.expect_downcast_ref(),
793                scratch_buffer_offset: d.scratch_buffer_offset,
794            });
795        unsafe { C::build_acceleration_structures(self, descriptors.len() as _, descriptors) };
796    }
797
798    unsafe fn place_acceleration_structure_barrier(
799        &mut self,
800        barrier: AccelerationStructureBarrier,
801    ) {
802        unsafe { C::place_acceleration_structure_barrier(self, barrier) };
803    }
804
805    unsafe fn copy_acceleration_structure_to_acceleration_structure(
806        &mut self,
807        src: &dyn DynAccelerationStructure,
808        dst: &dyn DynAccelerationStructure,
809        copy: wgt::AccelerationStructureCopy,
810    ) {
811        let src = src.expect_downcast_ref();
812        let dst = dst.expect_downcast_ref();
813        unsafe { C::copy_acceleration_structure_to_acceleration_structure(self, src, dst, copy) };
814    }
815    unsafe fn read_acceleration_structure_compact_size(
816        &mut self,
817        acceleration_structure: &dyn DynAccelerationStructure,
818        buf: &dyn DynBuffer,
819    ) {
820        let acceleration_structure = acceleration_structure.expect_downcast_ref();
821        let buf = buf.expect_downcast_ref();
822        unsafe { C::read_acceleration_structure_compact_size(self, acceleration_structure, buf) }
823    }
824
825    unsafe fn set_acceleration_structure_dependencies(
826        &self,
827        command_buffers: &[Box<dyn DynCommandBuffer>],
828        dependencies: &[&dyn DynAccelerationStructure],
829    ) {
830        let command_buffers: Vec<&<C::A as Api>::CommandBuffer> = command_buffers
831            .iter()
832            .map(|command_buffer| command_buffer.expect_downcast_ref())
833            .collect();
834        let dependencies: Vec<&<C::A as Api>::AccelerationStructure> = dependencies
835            .iter()
836            .map(|dependency| dependency.expect_downcast_ref())
837            .collect();
838        unsafe { C::set_acceleration_structure_dependencies(&command_buffers, &dependencies) }
839    }
840}
841
842impl<'a> PassTimestampWrites<'a, dyn DynQuerySet> {
843    pub fn expect_downcast<B: DynQuerySet>(&self) -> PassTimestampWrites<'a, B> {
844        PassTimestampWrites {
845            query_set: self.query_set.expect_downcast_ref(),
846            beginning_of_pass_write_index: self.beginning_of_pass_write_index,
847            end_of_pass_write_index: self.end_of_pass_write_index,
848        }
849    }
850}
851
852impl<'a> Attachment<'a, dyn DynTextureView> {
853    pub fn expect_downcast<B: DynTextureView>(&self) -> Attachment<'a, B> {
854        Attachment {
855            view: self.view.expect_downcast_ref(),
856            usage: self.usage,
857        }
858    }
859}
860
861impl<'a> ColorAttachment<'a, dyn DynTextureView> {
862    pub fn expect_downcast<B: DynTextureView>(&self) -> ColorAttachment<'a, B> {
863        ColorAttachment {
864            target: self.target.expect_downcast(),
865            depth_slice: self.depth_slice,
866            resolve_target: self.resolve_target.as_ref().map(|rt| rt.expect_downcast()),
867            ops: self.ops,
868            clear_value: self.clear_value,
869        }
870    }
871}
872
873impl<'a> DepthStencilAttachment<'a, dyn DynTextureView> {
874    pub fn expect_downcast<B: DynTextureView>(&self) -> DepthStencilAttachment<'a, B> {
875        DepthStencilAttachment {
876            target: self.target.expect_downcast(),
877            depth_ops: self.depth_ops,
878            stencil_ops: self.stencil_ops,
879            clear_value: self.clear_value,
880            depth_read_only: self.depth_read_only,
881            stencil_read_only: self.stencil_read_only,
882        }
883    }
884}