metal-rust-ffi 1.0.0

Audited Objective-C interoperability boundary for metal-rust
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
//! Audited parallel-render and resource-state command encoders.

use crate::ThreadBound;
use crate::foundation::Error;
use crate::metal::generated_object_types::metal::{
    BlitPassSampleBufferAttachmentDescriptor, BlitPassSampleBufferAttachmentDescriptorArray,
    CommandEncoder, Fence, ResourceStatePassDescriptor,
    ResourceStatePassSampleBufferAttachmentDescriptor,
    ResourceStatePassSampleBufferAttachmentDescriptorArray,
};
use crate::metal::generated_value_types::{
    SparseTextureMappingMode, Stages, StoreAction, StoreActionOptions,
};
use crate::metal::{
    CommandBuffer, Device, Origin, Region, RenderCommandEncoder, RenderPassDescriptor, Texture,
    TextureType,
};
use objc2::rc::Retained;
use objc2::runtime::{NSObjectProtocol, ProtocolObject};
use objc2::{msg_send, sel};
use objc2_foundation::NSString;
use objc2_metal::{
    MTLCommandBuffer, MTLCommandEncoder, MTLOrigin, MTLParallelRenderCommandEncoder, MTLRegion,
    MTLResourceStateCommandEncoder, MTLSparseTextureMappingMode, MTLStoreAction,
    MTLStoreActionOptions, MTLTexture,
};
use std::cell::Cell;
use std::ptr::NonNull;

const MAX_COLOR_ATTACHMENTS: usize = 8;
const MAX_PASS_SAMPLE_BUFFER_ATTACHMENTS: usize = 4;

fn object_responds(object: &objc2::runtime::AnyObject, selector: objc2::runtime::Sel) -> bool {
    // SAFETY: every Objective-C object implements respondsToSelector:, and the
    // selector and bool use their runtime-declared ABI representations.
    unsafe { msg_send![object, respondsToSelector: selector] }
}

/// A checked index into a pass sample-buffer attachment array.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct PassSampleBufferAttachmentIndex(u8);

impl PassSampleBufferAttachmentIndex {
    /// First pass sample-buffer attachment slot.
    pub const ZERO: Self = Self(0);
    /// Second pass sample-buffer attachment slot.
    pub const ONE: Self = Self(1);
    /// Third pass sample-buffer attachment slot.
    pub const TWO: Self = Self(2);
    /// Fourth pass sample-buffer attachment slot.
    pub const THREE: Self = Self(3);

    const fn as_usize(self) -> usize {
        self.0 as usize
    }
}

impl TryFrom<usize> for PassSampleBufferAttachmentIndex {
    type Error = Error;

    fn try_from(value: usize) -> Result<Self, Self::Error> {
        if value >= MAX_PASS_SAMPLE_BUFFER_ATTACHMENTS {
            return Err(Error::invalid_argument(
                "pass sample-buffer attachment index must be below 4",
            ));
        }
        Ok(Self(value as u8))
    }
}

impl BlitPassSampleBufferAttachmentDescriptorArray {
    /// Returns one typed attachment from a checked slot.
    pub fn attachment(
        &self,
        index: PassSampleBufferAttachmentIndex,
    ) -> Result<BlitPassSampleBufferAttachmentDescriptor, Error> {
        if !object_responds(self.as_inner(), sel!(objectAtIndexedSubscript:)) {
            return Err(Error::unsupported(
                "MTLBlitPassSampleBufferAttachmentDescriptorArray::object is unavailable",
            ));
        }
        // SAFETY: selector availability was checked and the newtype limits the
        // index to Metal's four pass sample-buffer attachment slots.
        let value: Option<Retained<objc2::runtime::AnyObject>> =
            unsafe { msg_send![self.as_inner(), objectAtIndexedSubscript: index.as_usize()] };
        value
            .map(BlitPassSampleBufferAttachmentDescriptor::from_inner)
            .ok_or_else(|| Error::unsupported("Metal returned no blit-pass attachment"))
    }

    /// Replaces or clears one typed attachment in a checked slot.
    pub fn set_attachment(
        &self,
        index: PassSampleBufferAttachmentIndex,
        attachment: Option<&BlitPassSampleBufferAttachmentDescriptor>,
    ) -> Result<(), Error> {
        if !object_responds(self.as_inner(), sel!(setObject:atIndexedSubscript:)) {
            return Err(Error::unsupported(
                "MTLBlitPassSampleBufferAttachmentDescriptorArray::setObject is unavailable",
            ));
        }
        // SAFETY: selector availability was checked, the index is constrained
        // to the four valid slots, and the typed wrapper owns the attachment.
        unsafe {
            let _: () = msg_send![
                self.as_inner(),
                setObject: attachment.map(BlitPassSampleBufferAttachmentDescriptor::as_inner),
                atIndexedSubscript: index.as_usize()
            ];
        }
        Ok(())
    }
}

impl ResourceStatePassSampleBufferAttachmentDescriptorArray {
    /// Returns one typed attachment from a checked slot.
    pub fn attachment(
        &self,
        index: PassSampleBufferAttachmentIndex,
    ) -> Result<ResourceStatePassSampleBufferAttachmentDescriptor, Error> {
        if !object_responds(self.as_inner(), sel!(objectAtIndexedSubscript:)) {
            return Err(Error::unsupported(
                "MTLResourceStatePassSampleBufferAttachmentDescriptorArray::object is unavailable",
            ));
        }
        // SAFETY: selector availability was checked and the newtype limits the
        // index to Metal's four pass sample-buffer attachment slots.
        let value: Option<Retained<objc2::runtime::AnyObject>> =
            unsafe { msg_send![self.as_inner(), objectAtIndexedSubscript: index.as_usize()] };
        value
            .map(ResourceStatePassSampleBufferAttachmentDescriptor::from_inner)
            .ok_or_else(|| Error::unsupported("Metal returned no resource-state attachment"))
    }

    /// Replaces or clears one typed attachment in a checked slot.
    pub fn set_attachment(
        &self,
        index: PassSampleBufferAttachmentIndex,
        attachment: Option<&ResourceStatePassSampleBufferAttachmentDescriptor>,
    ) -> Result<(), Error> {
        if !object_responds(self.as_inner(), sel!(setObject:atIndexedSubscript:)) {
            return Err(Error::unsupported(
                "MTLResourceStatePassSampleBufferAttachmentDescriptorArray::setObject is unavailable",
            ));
        }
        // SAFETY: selector availability was checked, the index is constrained
        // to the four valid slots, and the typed wrapper owns the attachment.
        unsafe {
            let _: () = msg_send![
                self.as_inner(),
                setObject: attachment.map(ResourceStatePassSampleBufferAttachmentDescriptor::as_inner),
                atIndexedSubscript: index.as_usize()
            ];
        }
        Ok(())
    }
}

impl CommandEncoder {
    /// Encodes a cross-pass queue-stage barrier using validated stage masks.
    pub fn barrier_after_queue_stages(
        &self,
        after_queue_stages: Stages,
        before_stages: Stages,
    ) -> Result<(), Error> {
        if !after_queue_stages.is_valid() || !before_stages.is_valid() {
            return Err(Error::invalid_argument(
                "queue-stage barrier masks contain undeclared bits",
            ));
        }
        if !object_responds(self.as_inner(), sel!(barrierAfterQueueStages:beforeStages:)) {
            return Err(Error::unsupported(
                "MTLCommandEncoder::barrierAfterQueueStages is unavailable",
            ));
        }
        // SAFETY: selector availability and both option masks were checked;
        // Stages has Metal's NSUInteger representation.
        unsafe {
            let _: () = msg_send![
                self.as_inner(),
                barrierAfterQueueStages: after_queue_stages.as_raw(),
                beforeStages: before_stages.as_raw()
            ];
        }
        Ok(())
    }
}

fn validate_store_action(value: StoreAction) -> Result<(), Error> {
    if !value.is_valid() || value.as_raw() == StoreAction::StoreActionUnknown.as_raw() {
        return Err(Error::invalid_argument(
            "parallel render store action must be a declared non-unknown value",
        ));
    }
    Ok(())
}

fn validate_store_action_options(value: StoreActionOptions) -> Result<(), Error> {
    if !value.is_valid() {
        return Err(Error::invalid_argument(
            "parallel render store-action options contain undeclared bits",
        ));
    }
    Ok(())
}

fn texture_slice_count(texture: &Texture) -> Result<usize, Error> {
    let (_, array_length, _, _) = texture.layout();
    let count = match texture.texture_type()? {
        TextureType::Cube => 6,
        TextureType::CubeArray => array_length
            .checked_mul(6)
            .ok_or_else(|| Error::invalid_argument("cube-array slice count overflows"))?,
        TextureType::D1Array | TextureType::D2Array | TextureType::D2MultisampleArray => {
            array_length
        }
        _ => 1,
    };
    Ok(count.max(1))
}

fn validate_sparse_region(
    texture: &Texture,
    region: Region,
    mip_level: usize,
    slice: usize,
) -> Result<(), Error> {
    if !texture.is_sparse() {
        return Err(Error::invalid_argument(
            "sparse mapping operations require a sparse texture",
        ));
    }
    let (_, _, mip_count, _) = texture.layout();
    if mip_level >= mip_count || slice >= texture_slice_count(texture)? {
        return Err(Error::invalid_argument(
            "sparse texture mip level or slice is out of bounds",
        ));
    }
    if region.size.width == 0 || region.size.height == 0 || region.size.depth == 0 {
        return Err(Error::invalid_argument(
            "sparse texture mapping region must be non-empty",
        ));
    }
    let width = (texture.width() >> mip_level.min(usize::BITS as usize - 1)).max(1);
    let height = (texture.height() >> mip_level.min(usize::BITS as usize - 1)).max(1);
    let depth = (texture.inner.depth() >> mip_level.min(usize::BITS as usize - 1)).max(1);
    let end_x = region.origin.x.checked_add(region.size.width);
    let end_y = region.origin.y.checked_add(region.size.height);
    let end_z = region.origin.z.checked_add(region.size.depth);
    if end_x.is_none_or(|end| end > width)
        || end_y.is_none_or(|end| end > height)
        || end_z.is_none_or(|end| end > depth)
    {
        return Err(Error::invalid_argument(
            "sparse texture mapping region is out of bounds",
        ));
    }
    Ok(())
}

/// One checked region in a batched sparse-texture mapping operation.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct SparseTextureMapping {
    /// Texture region whose tiles are mapped or unmapped.
    pub region: Region,
    /// Mipmap level containing the region.
    pub mip_level: usize,
    /// Array or cube-face slice containing the region.
    pub slice: usize,
}

/// A parallel render encoder borrowing its command buffer until encoding ends.
pub struct ParallelRenderCommandEncoder<'a> {
    inner: Retained<ProtocolObject<dyn MTLParallelRenderCommandEncoder>>,
    command_buffer: &'a mut CommandBuffer,
    ended: bool,
    _thread_bound: ThreadBound,
}

impl<'a> ParallelRenderCommandEncoder<'a> {
    fn new(
        inner: Retained<ProtocolObject<dyn MTLParallelRenderCommandEncoder>>,
        command_buffer: &'a mut CommandBuffer,
    ) -> Self {
        Self {
            inner,
            command_buffer,
            ended: false,
            _thread_bound: ThreadBound::new(),
        }
    }

    /// Creates one render encoder whose borrow prevents ending the parallel pass.
    pub fn render_encoder<'encoder>(
        &'encoder mut self,
    ) -> Result<RenderCommandEncoder<'encoder>, Error> {
        self.inner
            .renderCommandEncoder()
            .map(|inner| RenderCommandEncoder::new(inner, &mut *self.command_buffer))
            .ok_or_else(|| Error::unsupported("Metal could not create a parallel render encoder"))
    }

    /// Returns the device executing this encoder.
    #[must_use]
    pub fn device(&self) -> Device {
        Device::from_inner(self.inner.device())
    }

    /// Inserts a copied Rust debug signpost.
    pub fn insert_debug_signpost(&self, value: &str) {
        self.inner.insertDebugSignpost(&NSString::from_str(value));
    }

    /// Pushes a copied Rust debug group.
    pub fn push_debug_group(&self, value: &str) {
        self.inner.pushDebugGroup(&NSString::from_str(value));
    }

    /// Pops the current debug group.
    pub fn pop_debug_group(&self) {
        self.inner.popDebugGroup();
    }

    /// Finalizes the store action for a checked color attachment.
    pub fn set_color_store_action(&self, index: usize, action: StoreAction) -> Result<(), Error> {
        if index >= MAX_COLOR_ATTACHMENTS {
            return Err(Error::invalid_argument(
                "color attachment index must be below 8",
            ));
        }
        validate_store_action(action)?;
        if !self
            .inner
            .respondsToSelector(sel!(setColorStoreAction:atIndex:))
        {
            return Err(Error::unsupported(
                "MTLParallelRenderCommandEncoder::setColorStoreAction is unavailable",
            ));
        }
        // SAFETY: the selector was checked, the attachment index is below
        // Metal's fixed color-attachment limit, and the action is declared.
        unsafe {
            self.inner
                .setColorStoreAction_atIndex(MTLStoreAction(action.as_raw()), index);
        }
        Ok(())
    }

    /// Finalizes the depth store action.
    pub fn set_depth_store_action(&self, action: StoreAction) -> Result<(), Error> {
        validate_store_action(action)?;
        if !self.inner.respondsToSelector(sel!(setDepthStoreAction:)) {
            return Err(Error::unsupported(
                "MTLParallelRenderCommandEncoder::setDepthStoreAction is unavailable",
            ));
        }
        self.inner
            .setDepthStoreAction(MTLStoreAction(action.as_raw()));
        Ok(())
    }

    /// Finalizes the stencil store action.
    pub fn set_stencil_store_action(&self, action: StoreAction) -> Result<(), Error> {
        validate_store_action(action)?;
        if !self.inner.respondsToSelector(sel!(setStencilStoreAction:)) {
            return Err(Error::unsupported(
                "MTLParallelRenderCommandEncoder::setStencilStoreAction is unavailable",
            ));
        }
        self.inner
            .setStencilStoreAction(MTLStoreAction(action.as_raw()));
        Ok(())
    }

    /// Sets options for a checked color attachment's store action.
    pub fn set_color_store_action_options(
        &self,
        index: usize,
        options: StoreActionOptions,
    ) -> Result<(), Error> {
        if index >= MAX_COLOR_ATTACHMENTS {
            return Err(Error::invalid_argument(
                "color attachment index must be below 8",
            ));
        }
        validate_store_action_options(options)?;
        if !self
            .inner
            .respondsToSelector(sel!(setColorStoreActionOptions:atIndex:))
        {
            return Err(Error::unsupported(
                "MTLParallelRenderCommandEncoder::setColorStoreActionOptions is unavailable",
            ));
        }
        // SAFETY: the selector was checked, the attachment index is below
        // Metal's fixed limit, and only declared option bits are passed.
        unsafe {
            self.inner
                .setColorStoreActionOptions_atIndex(MTLStoreActionOptions(options.as_raw()), index);
        }
        Ok(())
    }

    /// Sets depth store-action options.
    pub fn set_depth_store_action_options(&self, options: StoreActionOptions) -> Result<(), Error> {
        validate_store_action_options(options)?;
        if !self
            .inner
            .respondsToSelector(sel!(setDepthStoreActionOptions:))
        {
            return Err(Error::unsupported(
                "MTLParallelRenderCommandEncoder::setDepthStoreActionOptions is unavailable",
            ));
        }
        self.inner
            .setDepthStoreActionOptions(MTLStoreActionOptions(options.as_raw()));
        Ok(())
    }

    /// Sets stencil store-action options.
    pub fn set_stencil_store_action_options(
        &self,
        options: StoreActionOptions,
    ) -> Result<(), Error> {
        validate_store_action_options(options)?;
        if !self
            .inner
            .respondsToSelector(sel!(setStencilStoreActionOptions:))
        {
            return Err(Error::unsupported(
                "MTLParallelRenderCommandEncoder::setStencilStoreActionOptions is unavailable",
            ));
        }
        self.inner
            .setStencilStoreActionOptions(MTLStoreActionOptions(options.as_raw()));
        Ok(())
    }

    /// Ends encoding explicitly. Dropping the encoder also ends encoding.
    pub fn end_encoding(mut self) {
        if !self.ended {
            self.inner.endEncoding();
            self.ended = true;
        }
    }
}

impl Drop for ParallelRenderCommandEncoder<'_> {
    fn drop(&mut self) {
        if !self.ended {
            self.inner.endEncoding();
            self.ended = true;
        }
    }
}

/// A resource-state encoder borrowing its command buffer until encoding ends.
pub struct ResourceStateCommandEncoder<'a> {
    inner: Retained<ProtocolObject<dyn MTLResourceStateCommandEncoder>>,
    _command_buffer: &'a mut CommandBuffer,
    fence_updated: Cell<bool>,
    ended: bool,
    _thread_bound: ThreadBound,
}

impl<'a> ResourceStateCommandEncoder<'a> {
    fn new(
        inner: Retained<ProtocolObject<dyn MTLResourceStateCommandEncoder>>,
        command_buffer: &'a mut CommandBuffer,
    ) -> Self {
        Self {
            inner,
            _command_buffer: command_buffer,
            fence_updated: Cell::new(false),
            ended: false,
            _thread_bound: ThreadBound::new(),
        }
    }

    /// Returns the device executing this encoder.
    #[must_use]
    pub fn device(&self) -> Device {
        Device::from_inner(self.inner.device())
    }

    /// Inserts a copied Rust debug signpost.
    pub fn insert_debug_signpost(&self, value: &str) {
        self.inner.insertDebugSignpost(&NSString::from_str(value));
    }

    /// Pushes a copied Rust debug group.
    pub fn push_debug_group(&self, value: &str) {
        self.inner.pushDebugGroup(&NSString::from_str(value));
    }

    /// Pops the current debug group.
    pub fn pop_debug_group(&self) {
        self.inner.popDebugGroup();
    }

    /// Updates one checked sparse-texture mapping.
    pub fn update_texture_mapping(
        &self,
        texture: &Texture,
        mode: SparseTextureMappingMode,
        mapping: SparseTextureMapping,
    ) -> Result<(), Error> {
        if !mode.is_valid() {
            return Err(Error::invalid_argument(
                "sparse texture mapping mode is not declared by Metal",
            ));
        }
        validate_sparse_region(texture, mapping.region, mapping.mip_level, mapping.slice)?;
        if !self
            .inner
            .respondsToSelector(sel!(updateTextureMapping:mode:region:mipLevel:slice:))
        {
            return Err(Error::unsupported(
                "MTLResourceStateCommandEncoder::updateTextureMapping is unavailable",
            ));
        }
        // SAFETY: selector availability and all texture coordinates were
        // checked; the safe wrappers keep the texture and encoder retained.
        unsafe {
            self.inner.updateTextureMapping_mode_region_mipLevel_slice(
                &texture.inner,
                MTLSparseTextureMappingMode(mode.as_raw()),
                MTLRegion {
                    origin: mapping.region.origin.into(),
                    size: mapping.region.size.into(),
                },
                mapping.mip_level,
                mapping.slice,
            );
        }
        Ok(())
    }

    /// Updates checked sparse-texture mappings from a Rust slice.
    pub fn update_texture_mappings(
        &self,
        texture: &Texture,
        mode: SparseTextureMappingMode,
        mappings: &[SparseTextureMapping],
    ) -> Result<(), Error> {
        if mappings.is_empty() {
            return Ok(());
        }
        if !mode.is_valid() {
            return Err(Error::invalid_argument(
                "sparse texture mapping mode is not declared by Metal",
            ));
        }
        for mapping in mappings {
            validate_sparse_region(texture, mapping.region, mapping.mip_level, mapping.slice)?;
        }
        if !self.inner.respondsToSelector(
            sel!(updateTextureMappings:mode:regions:mipLevels:slices:numRegions:),
        ) {
            return Err(Error::unsupported(
                "MTLResourceStateCommandEncoder::updateTextureMappings is unavailable",
            ));
        }
        let mut regions = mappings
            .iter()
            .map(|mapping| MTLRegion {
                origin: mapping.region.origin.into(),
                size: mapping.region.size.into(),
            })
            .collect::<Vec<_>>();
        let mut mip_levels = mappings
            .iter()
            .map(|mapping| mapping.mip_level)
            .collect::<Vec<_>>();
        let mut slices = mappings
            .iter()
            .map(|mapping| mapping.slice)
            .collect::<Vec<_>>();
        // SAFETY: all three non-empty vectors have exactly `mappings.len()`
        // initialized elements and remain alive throughout the synchronous
        // Objective-C call. Every coordinate was validated above.
        unsafe {
            self.inner
                .updateTextureMappings_mode_regions_mipLevels_slices_numRegions(
                    &texture.inner,
                    MTLSparseTextureMappingMode(mode.as_raw()),
                    NonNull::new_unchecked(regions.as_mut_ptr()),
                    NonNull::new_unchecked(mip_levels.as_mut_ptr()),
                    NonNull::new_unchecked(slices.as_mut_ptr()),
                    mappings.len(),
                );
        }
        Ok(())
    }

    /// Safely substitutes native indirect-buffer mappings with checked direct
    /// mapping records.
    ///
    /// An arbitrary GPU buffer can contain an unbounded mapping count that a
    /// safe caller cannot validate at encoding time. This substitute preserves
    /// the mapping result without exposing that native unsafe precondition.
    pub fn update_texture_mapping_indirect(
        &self,
        texture: &Texture,
        mode: SparseTextureMappingMode,
        mappings: &[SparseTextureMapping],
    ) -> Result<(), Error> {
        self.update_texture_mappings(texture, mode, mappings)
    }

    /// Moves checked sparse mappings between compatible sparse textures.
    #[allow(clippy::too_many_arguments)]
    pub fn move_texture_mappings(
        &self,
        source: &Texture,
        source_slice: usize,
        source_level: usize,
        source_region: Region,
        destination: &Texture,
        destination_slice: usize,
        destination_level: usize,
        destination_origin: Origin,
    ) -> Result<(), Error> {
        validate_sparse_region(source, source_region, source_level, source_slice)?;
        let destination_region = Region::new(destination_origin, source_region.size);
        validate_sparse_region(
            destination,
            destination_region,
            destination_level,
            destination_slice,
        )?;
        if source.texture_type()? != destination.texture_type()?
            || source.layout().3 != destination.layout().3
            || source.usage() != destination.usage()
        {
            return Err(Error::invalid_argument(
                "sparse mapping move requires compatible texture types, samples, and usage",
            ));
        }
        if !self.inner.respondsToSelector(sel!(moveTextureMappingsFromTexture:sourceSlice:sourceLevel:sourceOrigin:sourceSize:toTexture:destinationSlice:destinationLevel:destinationOrigin:)) {
            return Err(Error::unsupported(
                "MTLResourceStateCommandEncoder::moveTextureMappings is unavailable",
            ));
        }
        // SAFETY: selector availability and both source and destination
        // coordinate ranges were checked; all resource wrappers remain alive.
        unsafe {
            self.inner.moveTextureMappingsFromTexture_sourceSlice_sourceLevel_sourceOrigin_sourceSize_toTexture_destinationSlice_destinationLevel_destinationOrigin(
                &source.inner,
                source_slice,
                source_level,
                MTLOrigin::from(source_region.origin),
                source_region.size.into(),
                &destination.inner,
                destination_slice,
                destination_level,
                MTLOrigin::from(destination_origin),
            );
        }
        Ok(())
    }

    /// Updates a fence after all resource-state work encoded so far.
    pub fn update_fence(&self, fence: &Fence) -> Result<(), Error> {
        if !self.inner.respondsToSelector(sel!(updateFence:)) {
            return Err(Error::unsupported(
                "MTLResourceStateCommandEncoder::updateFence is unavailable",
            ));
        }
        // SAFETY: selector availability was checked and the generated Fence
        // wrapper owns an object declared to conform to MTLFence.
        unsafe {
            let _: () = msg_send![&*self.inner, updateFence: fence.as_inner()];
        }
        self.fence_updated.set(true);
        Ok(())
    }

    /// Waits for a fence before subsequent resource-state work.
    pub fn wait_for_fence(&self, fence: &Fence) -> Result<(), Error> {
        if self.fence_updated.get() {
            return Err(Error::invalid_argument(
                "a resource-state encoder cannot wait after updating a fence",
            ));
        }
        if !self.inner.respondsToSelector(sel!(waitForFence:)) {
            return Err(Error::unsupported(
                "MTLResourceStateCommandEncoder::waitForFence is unavailable",
            ));
        }
        // SAFETY: selector availability was checked and the generated Fence
        // wrapper owns an object declared to conform to MTLFence.
        unsafe {
            let _: () = msg_send![&*self.inner, waitForFence: fence.as_inner()];
        }
        Ok(())
    }

    /// Ends encoding explicitly. Dropping the encoder also ends encoding.
    pub fn end_encoding(mut self) {
        if !self.ended {
            self.inner.endEncoding();
            self.ended = true;
        }
    }
}

impl Drop for ResourceStateCommandEncoder<'_> {
    fn drop(&mut self) {
        if !self.ended {
            self.inner.endEncoding();
            self.ended = true;
        }
    }
}

impl CommandBuffer {
    /// Begins a parallel render encoder with an exclusive command-buffer borrow.
    pub fn parallel_render_encoder<'a>(
        &'a mut self,
        descriptor: &RenderPassDescriptor,
    ) -> Result<ParallelRenderCommandEncoder<'a>, Error> {
        if !self
            .inner
            .respondsToSelector(sel!(parallelRenderCommandEncoderWithDescriptor:))
        {
            return Err(Error::unsupported(
                "MTLCommandBuffer::parallelRenderCommandEncoder is unavailable",
            ));
        }
        self.inner
            .parallelRenderCommandEncoderWithDescriptor(&descriptor.inner)
            .map(|inner| ParallelRenderCommandEncoder::new(inner, self))
            .ok_or_else(|| Error::unsupported("Metal could not create a parallel render encoder"))
    }

    /// Begins a resource-state encoder with an exclusive command-buffer borrow.
    pub fn resource_state_encoder<'a>(
        &'a mut self,
    ) -> Result<ResourceStateCommandEncoder<'a>, Error> {
        if !self
            .inner
            .respondsToSelector(sel!(resourceStateCommandEncoder))
        {
            return Err(Error::unsupported(
                "MTLCommandBuffer::resourceStateCommandEncoder is unavailable",
            ));
        }
        self.inner
            .resourceStateCommandEncoder()
            .map(|inner| ResourceStateCommandEncoder::new(inner, self))
            .ok_or_else(|| Error::unsupported("Metal could not create a resource-state encoder"))
    }

    /// Begins a descriptor-configured resource-state encoder.
    pub fn resource_state_encoder_with_descriptor<'a>(
        &'a mut self,
        descriptor: &ResourceStatePassDescriptor,
    ) -> Result<ResourceStateCommandEncoder<'a>, Error> {
        if !self
            .inner
            .respondsToSelector(sel!(resourceStateCommandEncoderWithDescriptor:))
        {
            return Err(Error::unsupported(
                "MTLCommandBuffer::resourceStateCommandEncoderWithDescriptor is unavailable",
            ));
        }
        // SAFETY: selector availability was checked; the generated descriptor
        // owns an MTLResourceStatePassDescriptor, and the returned protocol
        // object remains inside an exclusive command-buffer borrow.
        let inner: Option<Retained<ProtocolObject<dyn MTLResourceStateCommandEncoder>>> = unsafe {
            msg_send![&*self.inner, resourceStateCommandEncoderWithDescriptor: descriptor.as_inner()]
        };
        inner
            .map(|inner| ResourceStateCommandEncoder::new(inner, self))
            .ok_or_else(|| Error::unsupported("Metal could not create a resource-state encoder"))
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn parallel_store_action_rejects_unknown() {
        assert!(validate_store_action(StoreAction::StoreActionStore).is_ok());
        assert!(validate_store_action(StoreAction::StoreActionUnknown).is_err());
    }

    #[test]
    fn parallel_store_options_accept_declared_bits() {
        assert!(validate_store_action_options(StoreActionOptions::StoreActionOptionNone).is_ok());
        assert!(
            validate_store_action_options(
                StoreActionOptions::StoreActionOptionCustomSamplePositions
            )
            .is_ok()
        );
    }

    #[test]
    fn pass_attachment_indices_are_bounded() {
        assert_eq!(
            PassSampleBufferAttachmentIndex::try_from(3).expect("fourth slot should be valid"),
            PassSampleBufferAttachmentIndex::THREE
        );
        assert!(PassSampleBufferAttachmentIndex::try_from(4).is_err());
    }
}