metal-rust 1.0.0

Safe Rust interfaces for Apple Metal
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
//! Safe argument-buffer and indirect-command encoding APIs.

use super::{
    AccelerationStructure, ArgumentEncoder, Buffer, ComputePipelineState, CullMode, DepthClipMode,
    DepthStencilState, IndexType, IndirectCommandBuffer, IndirectComputeCommand,
    IndirectRenderCommand, IntersectionFunctionTable, PrimitiveType, Region, RenderPipelineState,
    ResourceID, SamplerState, Size, Texture, TriangleFillMode, VisibleFunctionTable, Winding,
};
use crate::Error;

enum RecordedIndirectRenderOperation<'a> {
    Reset,
    SetBarrier,
    ClearBarrier,
    DrawPatches {
        number_of_patch_control_points: usize,
        patch_start: usize,
        patch_count: usize,
        patch_index_buffer: Option<&'a Buffer>,
        patch_index_buffer_offset: usize,
        instance_count: usize,
        base_instance: usize,
        tessellation_factor_buffer: &'a Buffer,
        tessellation_factor_buffer_offset: usize,
        instance_stride: usize,
    },
    DrawIndexedPatches {
        number_of_patch_control_points: usize,
        patch_start: usize,
        patch_count: usize,
        patch_index_buffer: Option<&'a Buffer>,
        patch_index_buffer_offset: usize,
        control_point_index_buffer: &'a Buffer,
        control_point_index_buffer_offset: usize,
        instance_count: usize,
        base_instance: usize,
        tessellation_factor_buffer: &'a Buffer,
        tessellation_factor_buffer_offset: usize,
        instance_stride: usize,
    },
}

#[derive(Clone, Copy)]
enum RecordedIndirectComputeOperation {
    Reset,
    SetBarrier,
    ClearBarrier,
}

/// A safe command-list builder for one indirect render command.
#[derive(Default)]
pub struct IndirectRenderCommandRecording<'a> {
    operations: Vec<RecordedIndirectRenderOperation<'a>>,
}

impl<'a> IndirectRenderCommandRecording<'a> {
    /// Records a reset of the selected indirect render command.
    pub fn reset(&mut self) {
        self.operations.push(RecordedIndirectRenderOperation::Reset);
    }

    /// Records insertion of a barrier before later operations.
    pub fn set_barrier(&mut self) {
        self.operations
            .push(RecordedIndirectRenderOperation::SetBarrier);
    }

    /// Records removal of the currently configured barrier.
    pub fn clear_barrier(&mut self) {
        self.operations
            .push(RecordedIndirectRenderOperation::ClearBarrier);
    }

    /// Records a non-indexed patch draw whose buffers are checked when applied.
    #[allow(clippy::too_many_arguments)]
    pub fn draw_patches(
        &mut self,
        number_of_patch_control_points: usize,
        patch_start: usize,
        patch_count: usize,
        patch_index_buffer: Option<&'a Buffer>,
        patch_index_buffer_offset: usize,
        instance_count: usize,
        base_instance: usize,
        tessellation_factor_buffer: &'a Buffer,
        tessellation_factor_buffer_offset: usize,
        instance_stride: usize,
    ) {
        self.operations
            .push(RecordedIndirectRenderOperation::DrawPatches {
                number_of_patch_control_points,
                patch_start,
                patch_count,
                patch_index_buffer,
                patch_index_buffer_offset,
                instance_count,
                base_instance,
                tessellation_factor_buffer,
                tessellation_factor_buffer_offset,
                instance_stride,
            });
    }

    /// Records an indexed patch draw whose buffers are checked when applied.
    #[allow(clippy::too_many_arguments)]
    pub fn draw_indexed_patches(
        &mut self,
        number_of_patch_control_points: usize,
        patch_start: usize,
        patch_count: usize,
        patch_index_buffer: Option<&'a Buffer>,
        patch_index_buffer_offset: usize,
        control_point_index_buffer: &'a Buffer,
        control_point_index_buffer_offset: usize,
        instance_count: usize,
        base_instance: usize,
        tessellation_factor_buffer: &'a Buffer,
        tessellation_factor_buffer_offset: usize,
        instance_stride: usize,
    ) {
        self.operations
            .push(RecordedIndirectRenderOperation::DrawIndexedPatches {
                number_of_patch_control_points,
                patch_start,
                patch_count,
                patch_index_buffer,
                patch_index_buffer_offset,
                control_point_index_buffer,
                control_point_index_buffer_offset,
                instance_count,
                base_instance,
                tessellation_factor_buffer,
                tessellation_factor_buffer_offset,
                instance_stride,
            });
    }
}

/// A safe command-list builder for one indirect compute command.
#[derive(Default)]
pub struct IndirectComputeCommandRecording {
    operations: Vec<RecordedIndirectComputeOperation>,
}

impl IndirectComputeCommandRecording {
    /// Records a reset of the selected indirect compute command.
    pub fn reset(&mut self) {
        self.operations
            .push(RecordedIndirectComputeOperation::Reset);
    }

    /// Records insertion of a barrier before later operations.
    pub fn set_barrier(&mut self) {
        self.operations
            .push(RecordedIndirectComputeOperation::SetBarrier);
    }

    /// Records removal of the currently configured barrier.
    pub fn clear_barrier(&mut self) {
        self.operations
            .push(RecordedIndirectComputeOperation::ClearBarrier);
    }
}

impl IndirectCommandBuffer {
    /// Returns the stable GPU resource identifier after an availability check.
    pub fn gpu_resource_id(&self) -> Result<ResourceID, Error> {
        self.inner.gpu_resource_id().map_err(Error::from_ffi)
    }

    /// Builds and applies operations to a checked indirect render command.
    pub fn with_indirect_render_command<R>(
        &self,
        command_index: usize,
        body: impl for<'recording> FnOnce(&mut IndirectRenderCommandRecording<'recording>) -> R,
    ) -> Result<R, Error> {
        let mut recording = IndirectRenderCommandRecording::default();
        let result = body(&mut recording);
        self.inner
            .with_indirect_render_command(command_index, |inner| {
                for operation in recording.operations {
                    match operation {
                        RecordedIndirectRenderOperation::Reset => inner.reset()?,
                        RecordedIndirectRenderOperation::SetBarrier => inner.set_barrier()?,
                        RecordedIndirectRenderOperation::ClearBarrier => inner.clear_barrier()?,
                        RecordedIndirectRenderOperation::DrawPatches {
                            number_of_patch_control_points,
                            patch_start,
                            patch_count,
                            patch_index_buffer,
                            patch_index_buffer_offset,
                            instance_count,
                            base_instance,
                            tessellation_factor_buffer,
                            tessellation_factor_buffer_offset,
                            instance_stride,
                        } => inner.draw_patches(
                            number_of_patch_control_points,
                            patch_start,
                            patch_count,
                            patch_index_buffer.map(|value| &value.inner),
                            patch_index_buffer_offset,
                            instance_count,
                            base_instance,
                            &tessellation_factor_buffer.inner,
                            tessellation_factor_buffer_offset,
                            instance_stride,
                        )?,
                        RecordedIndirectRenderOperation::DrawIndexedPatches {
                            number_of_patch_control_points,
                            patch_start,
                            patch_count,
                            patch_index_buffer,
                            patch_index_buffer_offset,
                            control_point_index_buffer,
                            control_point_index_buffer_offset,
                            instance_count,
                            base_instance,
                            tessellation_factor_buffer,
                            tessellation_factor_buffer_offset,
                            instance_stride,
                        } => inner.draw_indexed_patches(
                            number_of_patch_control_points,
                            patch_start,
                            patch_count,
                            patch_index_buffer.map(|value| &value.inner),
                            patch_index_buffer_offset,
                            &control_point_index_buffer.inner,
                            control_point_index_buffer_offset,
                            instance_count,
                            base_instance,
                            &tessellation_factor_buffer.inner,
                            tessellation_factor_buffer_offset,
                            instance_stride,
                        )?,
                    }
                }
                Ok(())
            })
            .map_err(Error::from_ffi)?
            .map_err(Error::from_ffi)?;
        Ok(result)
    }

    /// Builds and applies operations to a checked indirect compute command.
    pub fn with_indirect_compute_command<R>(
        &self,
        command_index: usize,
        body: impl FnOnce(&mut IndirectComputeCommandRecording) -> R,
    ) -> Result<R, Error> {
        let mut recording = IndirectComputeCommandRecording::default();
        let result = body(&mut recording);
        self.inner
            .with_indirect_compute_command(command_index, |inner| {
                for operation in recording.operations {
                    match operation {
                        RecordedIndirectComputeOperation::Reset => inner.reset()?,
                        RecordedIndirectComputeOperation::SetBarrier => inner.set_barrier()?,
                        RecordedIndirectComputeOperation::ClearBarrier => inner.clear_barrier()?,
                    }
                }
                Ok(())
            })
            .map_err(Error::from_ffi)?
            .map_err(Error::from_ffi)?;
        Ok(result)
    }

    /// Resets a checked half-open range of commands.
    pub fn reset_commands(&self, range: std::ops::Range<usize>) -> Result<(), Error> {
        self.inner.reset_commands(range).map_err(Error::from_ffi)
    }
}

impl ArgumentEncoder {
    /// Creates a nested argument encoder for the member at `index`.
    pub fn new_argument_encoder(&self, index: usize) -> Result<Self, Error> {
        self.inner
            .new_argument_encoder(index)
            .map(Self::from_ffi)
            .map_err(Error::from_ffi)
    }

    /// Selects a checked byte offset in `buffer` as the argument destination.
    pub fn set_argument_buffer(&self, buffer: &Buffer, offset: usize) -> Result<(), Error> {
        self.inner
            .set_argument_buffer(&buffer.inner, offset)
            .map_err(Error::from_ffi)
    }

    /// Selects one array element in a checked buffer-backed argument array.
    pub fn set_argument_buffer_element(
        &self,
        buffer: &Buffer,
        start_offset: usize,
        array_element: usize,
    ) -> Result<(), Error> {
        self.inner
            .set_argument_buffer_element(&buffer.inner, start_offset, array_element)
            .map_err(Error::from_ffi)
    }

    /// Builds initialized bytes in a scoped slice and writes them to a verified
    /// member of the currently selected CPU-visible argument buffer.
    pub fn with_constant_data<R>(
        &self,
        index: usize,
        argument_buffer: &Buffer,
        argument_buffer_offset: usize,
        member_range: std::ops::Range<usize>,
        body: impl for<'data> FnOnce(&'data mut [u8]) -> R,
    ) -> Result<R, Error> {
        self.inner
            .with_constant_data(
                index,
                &argument_buffer.inner,
                argument_buffer_offset,
                member_range,
                body,
            )
            .map_err(Error::from_ffi)
    }

    /// Binds an optional buffer at an argument index with a checked byte offset.
    pub fn set_buffer(
        &self,
        buffer: Option<&Buffer>,
        offset: usize,
        index: usize,
    ) -> Result<(), Error> {
        self.inner
            .set_buffer(buffer.map(|value| &value.inner), offset, index)
            .map_err(Error::from_ffi)
    }

    /// Safely binds a slice of optional buffers and offsets at consecutive indices.
    pub fn set_buffers(
        &self,
        bindings: &[(Option<&Buffer>, usize)],
        start_index: usize,
    ) -> Result<(), Error> {
        let ffi = bindings
            .iter()
            .map(|(buffer, offset)| (buffer.map(|value| &value.inner), *offset))
            .collect::<Vec<_>>();
        self.inner
            .set_buffers(&ffi, start_index)
            .map_err(Error::from_ffi)
    }

    /// Binds or clears the texture at an argument index.
    pub fn set_texture(&self, texture: Option<&Texture>, index: usize) -> Result<(), Error> {
        self.inner
            .set_texture(texture.map(|value| &value.inner), index)
            .map_err(Error::from_ffi)
    }

    /// Safely binds a slice of optional textures at consecutive indices.
    pub fn set_textures(
        &self,
        textures: &[Option<&Texture>],
        start_index: usize,
    ) -> Result<(), Error> {
        let ffi = textures
            .iter()
            .map(|texture| texture.map(|value| &value.inner))
            .collect::<Vec<_>>();
        self.inner
            .set_textures(&ffi, start_index)
            .map_err(Error::from_ffi)
    }

    /// Binds or clears the acceleration structure at an argument index.
    pub fn set_acceleration_structure(
        &self,
        value: Option<&AccelerationStructure>,
        index: usize,
    ) -> Result<(), Error> {
        self.inner
            .set_acceleration_structure(value.map(|value| &value.inner), index)
            .map_err(Error::from_ffi)
    }

    /// Binds or clears the compute pipeline state at an argument index.
    pub fn set_compute_pipeline_state(
        &self,
        value: Option<&ComputePipelineState>,
        index: usize,
    ) -> Result<(), Error> {
        self.inner
            .set_compute_pipeline_state(value.map(|value| &value.inner), index)
            .map_err(Error::from_ffi)
    }

    /// Safely binds a slice of compute pipeline states at consecutive indices.
    pub fn set_compute_pipeline_states(
        &self,
        values: &[Option<&ComputePipelineState>],
        start_index: usize,
    ) -> Result<(), Error> {
        let ffi = values
            .iter()
            .map(|value| value.map(|value| &value.inner))
            .collect::<Vec<_>>();
        self.inner
            .set_compute_pipeline_states(&ffi, start_index)
            .map_err(Error::from_ffi)
    }

    /// Binds or clears the depth-stencil state at an argument index.
    pub fn set_depth_stencil_state(
        &self,
        value: Option<&DepthStencilState>,
        index: usize,
    ) -> Result<(), Error> {
        self.inner
            .set_depth_stencil_state(value.map(|value| &value.inner), index)
            .map_err(Error::from_ffi)
    }

    /// Safely binds a slice of depth-stencil states at consecutive indices.
    pub fn set_depth_stencil_states(
        &self,
        values: &[Option<&DepthStencilState>],
        start_index: usize,
    ) -> Result<(), Error> {
        let ffi = values
            .iter()
            .map(|value| value.map(|value| &value.inner))
            .collect::<Vec<_>>();
        self.inner
            .set_depth_stencil_states(&ffi, start_index)
            .map_err(Error::from_ffi)
    }

    /// Binds or clears the indirect command buffer at an argument index.
    pub fn set_indirect_command_buffer(
        &self,
        value: Option<&IndirectCommandBuffer>,
        index: usize,
    ) -> Result<(), Error> {
        self.inner
            .set_indirect_command_buffer(value.map(|value| &value.inner), index)
            .map_err(Error::from_ffi)
    }

    /// Safely binds a slice of indirect command buffers at consecutive indices.
    pub fn set_indirect_command_buffers(
        &self,
        values: &[Option<&IndirectCommandBuffer>],
        start_index: usize,
    ) -> Result<(), Error> {
        let ffi = values
            .iter()
            .map(|value| value.map(|value| &value.inner))
            .collect::<Vec<_>>();
        self.inner
            .set_indirect_command_buffers(&ffi, start_index)
            .map_err(Error::from_ffi)
    }

    /// Binds or clears the intersection function table at an argument index.
    pub fn set_intersection_function_table(
        &self,
        value: Option<&IntersectionFunctionTable>,
        index: usize,
    ) -> Result<(), Error> {
        self.inner
            .set_intersection_function_table(value.map(|value| &value.inner), index)
            .map_err(Error::from_ffi)
    }

    /// Safely binds intersection function tables at consecutive argument indices.
    pub fn set_intersection_function_tables(
        &self,
        values: &[Option<&IntersectionFunctionTable>],
        start_index: usize,
    ) -> Result<(), Error> {
        let ffi = values
            .iter()
            .map(|value| value.map(|value| &value.inner))
            .collect::<Vec<_>>();
        self.inner
            .set_intersection_function_tables(&ffi, start_index)
            .map_err(Error::from_ffi)
    }

    /// Binds or clears the render pipeline state at an argument index.
    pub fn set_render_pipeline_state(
        &self,
        value: Option<&RenderPipelineState>,
        index: usize,
    ) -> Result<(), Error> {
        self.inner
            .set_render_pipeline_state(value.map(|value| &value.inner), index)
            .map_err(Error::from_ffi)
    }

    /// Safely binds a slice of render pipeline states at consecutive indices.
    pub fn set_render_pipeline_states(
        &self,
        values: &[Option<&RenderPipelineState>],
        start_index: usize,
    ) -> Result<(), Error> {
        let ffi = values
            .iter()
            .map(|value| value.map(|value| &value.inner))
            .collect::<Vec<_>>();
        self.inner
            .set_render_pipeline_states(&ffi, start_index)
            .map_err(Error::from_ffi)
    }

    /// Binds or clears the sampler state at an argument index.
    pub fn set_sampler_state(
        &self,
        value: Option<&SamplerState>,
        index: usize,
    ) -> Result<(), Error> {
        self.inner
            .set_sampler_state(value.map(|value| &value.inner), index)
            .map_err(Error::from_ffi)
    }

    /// Safely binds a slice of sampler states at consecutive indices.
    pub fn set_sampler_states(
        &self,
        values: &[Option<&SamplerState>],
        start_index: usize,
    ) -> Result<(), Error> {
        let ffi = values
            .iter()
            .map(|value| value.map(|value| &value.inner))
            .collect::<Vec<_>>();
        self.inner
            .set_sampler_states(&ffi, start_index)
            .map_err(Error::from_ffi)
    }

    /// Binds or clears the visible function table at an argument index.
    pub fn set_visible_function_table(
        &self,
        value: Option<&VisibleFunctionTable>,
        index: usize,
    ) -> Result<(), Error> {
        self.inner
            .set_visible_function_table(value.map(|value| &value.inner), index)
            .map_err(Error::from_ffi)
    }

    /// Safely binds visible function tables at consecutive argument indices.
    pub fn set_visible_function_tables(
        &self,
        values: &[Option<&VisibleFunctionTable>],
        start_index: usize,
    ) -> Result<(), Error> {
        let ffi = values
            .iter()
            .map(|value| value.map(|value| &value.inner))
            .collect::<Vec<_>>();
        self.inner
            .set_visible_function_tables(&ffi, start_index)
            .map_err(Error::from_ffi)
    }
}

impl IndirectRenderCommand {
    /// Resets the indirect render command to its initial empty state.
    pub fn reset(&self) -> Result<(), Error> {
        self.inner.reset().map_err(Error::from_ffi)
    }
    /// Inserts a barrier before later operations in this indirect command.
    pub fn set_barrier(&self) -> Result<(), Error> {
        self.inner.set_barrier().map_err(Error::from_ffi)
    }
    /// Removes the barrier previously configured on this indirect command.
    pub fn clear_barrier(&self) -> Result<(), Error> {
        self.inner.clear_barrier().map_err(Error::from_ffi)
    }

    /// Binds an optional vertex buffer after validating its byte offset.
    pub fn set_vertex_buffer(
        &self,
        buffer: Option<&Buffer>,
        offset: usize,
        index: usize,
    ) -> Result<(), Error> {
        self.inner
            .set_vertex_buffer(buffer.map(|value| &value.inner), offset, index)
            .map_err(Error::from_ffi)
    }

    /// Binds an optional vertex buffer with a checked dynamic attribute stride.
    pub fn set_vertex_buffer_with_stride(
        &self,
        buffer: Option<&Buffer>,
        offset: usize,
        stride: usize,
        index: usize,
    ) -> Result<(), Error> {
        self.inner
            .set_vertex_buffer_with_stride(
                buffer.map(|value| &value.inner),
                offset,
                Some(stride),
                index,
            )
            .map_err(Error::from_ffi)
    }

    /// Binds an optional fragment buffer after validating its byte offset.
    pub fn set_fragment_buffer(
        &self,
        buffer: Option<&Buffer>,
        offset: usize,
        index: usize,
    ) -> Result<(), Error> {
        self.inner
            .set_fragment_buffer(buffer.map(|value| &value.inner), offset, index)
            .map_err(Error::from_ffi)
    }

    /// Binds an optional mesh-stage buffer after validating its byte offset.
    pub fn set_mesh_buffer(
        &self,
        buffer: Option<&Buffer>,
        offset: usize,
        index: usize,
    ) -> Result<(), Error> {
        self.inner
            .set_mesh_buffer(buffer.map(|value| &value.inner), offset, index)
            .map_err(Error::from_ffi)
    }

    /// Binds an optional object-stage buffer after validating its byte offset.
    pub fn set_object_buffer(
        &self,
        buffer: Option<&Buffer>,
        offset: usize,
        index: usize,
    ) -> Result<(), Error> {
        self.inner
            .set_object_buffer(buffer.map(|value| &value.inner), offset, index)
            .map_err(Error::from_ffi)
    }

    /// Selects the render pipeline state used by this indirect command.
    pub fn set_render_pipeline_state(&self, state: &RenderPipelineState) -> Result<(), Error> {
        self.inner
            .set_render_pipeline_state(&state.inner)
            .map_err(Error::from_ffi)
    }

    /// Selects or clears the depth-stencil state used by this indirect command.
    pub fn set_depth_stencil_state(&self, state: Option<&DepthStencilState>) -> Result<(), Error> {
        self.inner
            .set_depth_stencil_state(state.map(|value| &value.inner))
            .map_err(Error::from_ffi)
    }

    /// Sets a validated triangle culling mode.
    pub fn set_cull_mode(&self, value: CullMode) -> Result<(), Error> {
        self.inner.set_cull_mode(value).map_err(Error::from_ffi)
    }
    /// Sets a validated depth clipping mode.
    pub fn set_depth_clip_mode(&self, value: DepthClipMode) -> Result<(), Error> {
        self.inner
            .set_depth_clip_mode(value)
            .map_err(Error::from_ffi)
    }
    /// Sets the winding used to identify front-facing primitives.
    pub fn set_front_facing_winding(&self, value: Winding) -> Result<(), Error> {
        self.inner
            .set_front_facing_winding(value)
            .map_err(Error::from_ffi)
    }
    /// Sets a validated triangle fill mode.
    pub fn set_triangle_fill_mode(&self, value: TriangleFillMode) -> Result<(), Error> {
        self.inner
            .set_triangle_fill_mode(value)
            .map_err(Error::from_ffi)
    }
    /// Sets finite depth-bias parameters for subsequent indirect draws.
    pub fn set_depth_bias(
        &self,
        depth_bias: f32,
        slope_scale: f32,
        clamp: f32,
    ) -> Result<(), Error> {
        self.inner
            .set_depth_bias(depth_bias, slope_scale, clamp)
            .map_err(Error::from_ffi)
    }
    /// Sets object-stage threadgroup memory length for one binding index.
    pub fn set_object_threadgroup_memory_length(
        &self,
        length: usize,
        index: usize,
    ) -> Result<(), Error> {
        self.inner
            .set_object_threadgroup_memory_length(length, index)
            .map_err(Error::from_ffi)
    }

    /// Encodes a non-indexed draw after checking vertex and instance ranges.
    pub fn draw_primitives(
        &self,
        primitive_type: PrimitiveType,
        vertex_start: usize,
        vertex_count: usize,
        instance_count: usize,
        base_instance: usize,
    ) -> Result<(), Error> {
        self.inner
            .draw_primitives(
                primitive_type,
                vertex_start,
                vertex_count,
                instance_count,
                base_instance,
            )
            .map_err(Error::from_ffi)
    }

    #[allow(clippy::too_many_arguments)]
    /// Encodes an indexed draw after validating the index-buffer byte range.
    pub fn draw_indexed_primitives(
        &self,
        primitive_type: PrimitiveType,
        index_count: usize,
        index_type: IndexType,
        index_buffer: &Buffer,
        index_buffer_offset: usize,
        instance_count: usize,
        base_vertex: isize,
        base_instance: usize,
    ) -> Result<(), Error> {
        self.inner
            .draw_indexed_primitives(
                primitive_type,
                index_count,
                index_type,
                &index_buffer.inner,
                index_buffer_offset,
                instance_count,
                base_vertex,
                base_instance,
            )
            .map_err(Error::from_ffi)
    }

    /// Encodes a mesh draw using checked non-zero threadgroup dimensions.
    pub fn draw_mesh_threadgroups(
        &self,
        groups: Size,
        object_threads: Size,
        mesh_threads: Size,
    ) -> Result<(), Error> {
        self.inner
            .draw_mesh_threadgroups(groups, object_threads, mesh_threads)
            .map_err(Error::from_ffi)
    }

    /// Encodes a mesh draw using checked non-zero thread-grid dimensions.
    pub fn draw_mesh_threads(
        &self,
        threads: Size,
        object_threads: Size,
        mesh_threads: Size,
    ) -> Result<(), Error> {
        self.inner
            .draw_mesh_threads(threads, object_threads, mesh_threads)
            .map_err(Error::from_ffi)
    }
}

impl IndirectComputeCommand {
    /// Resets the indirect compute command to its initial empty state.
    pub fn reset(&self) -> Result<(), Error> {
        self.inner.reset().map_err(Error::from_ffi)
    }
    /// Inserts a barrier before later operations in this indirect command.
    pub fn set_barrier(&self) -> Result<(), Error> {
        self.inner.set_barrier().map_err(Error::from_ffi)
    }
    /// Removes the barrier previously configured on this indirect command.
    pub fn clear_barrier(&self) -> Result<(), Error> {
        self.inner.clear_barrier().map_err(Error::from_ffi)
    }

    /// Selects the compute pipeline state used by this indirect command.
    pub fn set_compute_pipeline_state(&self, state: &ComputePipelineState) -> Result<(), Error> {
        self.inner
            .set_compute_pipeline_state(&state.inner)
            .map_err(Error::from_ffi)
    }

    /// Binds an optional kernel buffer after validating its byte offset.
    pub fn set_kernel_buffer(
        &self,
        buffer: Option<&Buffer>,
        offset: usize,
        index: usize,
    ) -> Result<(), Error> {
        self.inner
            .set_kernel_buffer(buffer.map(|value| &value.inner), offset, index)
            .map_err(Error::from_ffi)
    }

    /// Binds an optional kernel buffer with a checked dynamic attribute stride.
    pub fn set_kernel_buffer_with_stride(
        &self,
        buffer: Option<&Buffer>,
        offset: usize,
        stride: usize,
        index: usize,
    ) -> Result<(), Error> {
        self.inner
            .set_kernel_buffer_with_stride(
                buffer.map(|value| &value.inner),
                offset,
                Some(stride),
                index,
            )
            .map_err(Error::from_ffi)
    }

    /// Sets checked non-zero imageblock dimensions.
    pub fn set_imageblock_size(&self, width: usize, height: usize) -> Result<(), Error> {
        self.inner
            .set_imageblock_size(width, height)
            .map_err(Error::from_ffi)
    }
    /// Sets a non-empty stage-in region after checking coordinate overflow.
    pub fn set_stage_in_region(&self, region: Region) -> Result<(), Error> {
        self.inner
            .set_stage_in_region(region)
            .map_err(Error::from_ffi)
    }
    /// Sets threadgroup memory length for one binding index.
    pub fn set_threadgroup_memory_length(&self, length: usize, index: usize) -> Result<(), Error> {
        self.inner
            .set_threadgroup_memory_length(length, index)
            .map_err(Error::from_ffi)
    }
    /// Encodes concurrent threadgroup dispatch with non-zero dimensions.
    pub fn concurrent_dispatch_threadgroups(
        &self,
        groups: Size,
        threads_per_group: Size,
    ) -> Result<(), Error> {
        self.inner
            .concurrent_dispatch_threadgroups(groups, threads_per_group)
            .map_err(Error::from_ffi)
    }
    /// Encodes concurrent thread dispatch with non-zero dimensions.
    pub fn concurrent_dispatch_threads(
        &self,
        threads: Size,
        threads_per_group: Size,
    ) -> Result<(), Error> {
        self.inner
            .concurrent_dispatch_threads(threads, threads_per_group)
            .map_err(Error::from_ffi)
    }
}