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
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
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
//! Audited Metal blit encoder operations.

use crate::ThreadBound;
use crate::foundation::Error;
use crate::metal::generated_object_types::metal::{
    CounterSampleBuffer, Fence, IndirectCommandBuffer, Tensor,
};
use crate::metal::generated_value_types::{BlitOption, TensorPlaneType};
use crate::metal::{
    Buffer, BufferReadback, CommandBuffer, PixelFormat, Region, ResourceOptions, StorageMode,
    TensorLayout, Texture, TextureReadback, TextureType,
};
use objc2::rc::{Allocated, Retained};
use objc2::runtime::{AnyClass, AnyObject, NSObjectProtocol, ProtocolObject};
use objc2::{msg_send, sel};
use objc2_foundation::{NSData, NSRange};
use objc2_metal::{MTLBlitCommandEncoder, MTLCommandEncoder, MTLDevice, MTLTexture as _};
use std::marker::PhantomData;

/// A blit encoder borrowing its command buffer until `end_encoding`.
pub struct BlitCommandEncoder<'a> {
    inner: Retained<ProtocolObject<dyn MTLBlitCommandEncoder>>,
    _command_buffer: PhantomData<&'a mut CommandBuffer>,
    submission_id: u64,
    ended: bool,
    _thread_bound: ThreadBound,
}

impl<'a> BlitCommandEncoder<'a> {
    pub(super) fn new(
        inner: Retained<ProtocolObject<dyn MTLBlitCommandEncoder>>,
        submission_id: u64,
        _command_buffer: &'a mut CommandBuffer,
    ) -> Self {
        Self {
            inner,
            _command_buffer: PhantomData,
            submission_id,
            ended: false,
            _thread_bound: ThreadBound::new(),
        }
    }

    /// Encodes a copy into an internal shared staging buffer.
    pub fn read_buffer(
        &self,
        source: &Buffer,
        source_offset: usize,
        size: usize,
    ) -> Result<BufferReadback, Error> {
        let source_end = source_offset
            .checked_add(size)
            .ok_or_else(|| Error::invalid_argument("readback range overflow"))?;
        if source_end > source.length() {
            return Err(Error::invalid_argument("readback range is out of bounds"));
        }
        let inner = self
            .inner
            .device()
            .newBufferWithLength_options(size, ResourceOptions::SHARED.as_objc())
            .ok_or_else(|| Error::unsupported("Metal could not allocate a staging buffer"))?;
        let buffer = Buffer::new(inner, StorageMode::Shared);
        self.copy_buffer(source, source_offset, &buffer, 0, size)?;
        Ok(BufferReadback {
            buffer,
            submission_id: self.submission_id,
            length: size,
        })
    }

    /// Encodes a checked level-zero 2D texture readback through staging.
    pub fn read_texture(&self, source: &Texture, region: Region) -> Result<TextureReadback, Error> {
        if region.size.depth != 1 || region.origin.z != 0 {
            return Err(Error::invalid_argument(
                "2D texture readback requires z=0 and depth=1",
            ));
        }
        let end_x = region
            .origin
            .x
            .checked_add(region.size.width)
            .ok_or_else(|| Error::invalid_argument("texture readback x range overflow"))?;
        let end_y = region
            .origin
            .y
            .checked_add(region.size.height)
            .ok_or_else(|| Error::invalid_argument("texture readback y range overflow"))?;
        if region.size.width == 0
            || region.size.height == 0
            || end_x > source.width()
            || end_y > source.height()
        {
            return Err(Error::invalid_argument(
                "texture readback region is empty or out of bounds",
            ));
        }
        let pixel_format = source.pixel_format();
        let bytes_per_pixel = match pixel_format {
            PixelFormat::RGBA8_UNORM
            | PixelFormat::BGRA8_UNORM
            | PixelFormat::RGBA8_UNORM_SRGB
            | PixelFormat::BGRA8_UNORM_SRGB
            | PixelFormat::RG16_FLOAT
            | PixelFormat::DEPTH32_FLOAT => 4_usize,
            PixelFormat::R16_FLOAT => 2,
            PixelFormat::RGBA16_FLOAT => 8,
            _ => {
                return Err(Error::unsupported(
                    "texture readback format has no audited byte layout",
                ));
            }
        };
        let unaligned_row = region
            .size
            .width
            .checked_mul(bytes_per_pixel)
            .ok_or_else(|| Error::invalid_argument("texture row size overflow"))?;
        let alignment = self
            .inner
            .device()
            .minimumTextureBufferAlignmentForPixelFormat(pixel_format.as_objc())
            .max(1);
        let bytes_per_row = unaligned_row
            .checked_add(alignment - 1)
            .map(|value| value / alignment * alignment)
            .ok_or_else(|| Error::invalid_argument("aligned texture row size overflow"))?;
        let length = bytes_per_row
            .checked_mul(region.size.height)
            .ok_or_else(|| Error::invalid_argument("texture staging size overflow"))?;
        let inner = self
            .inner
            .device()
            .newBufferWithLength_options(length, ResourceOptions::SHARED.as_objc())
            .ok_or_else(|| Error::unsupported("Metal could not allocate texture staging"))?;
        let buffer = Buffer::new(inner, StorageMode::Shared);
        // SAFETY: source bounds, staging length, pixel byte layout, row
        // alignment, origin, and size were all checked above; both retained
        // resources remain alive through command encoding and submission.
        unsafe {
            self.inner.copyFromTexture_sourceSlice_sourceLevel_sourceOrigin_sourceSize_toBuffer_destinationOffset_destinationBytesPerRow_destinationBytesPerImage(
                &source.inner,
                0,
                0,
                region.origin.into(),
                region.size.into(),
                &buffer.inner,
                0,
                bytes_per_row,
                length,
            );
        }
        Ok(TextureReadback {
            buffer,
            submission_id: self.submission_id,
            length,
            bytes_per_row,
            width: region.size.width,
            height: region.size.height,
        })
    }

    /// Copies a checked byte range between two buffers.
    pub fn copy_buffer(
        &self,
        source: &Buffer,
        source_offset: usize,
        destination: &Buffer,
        destination_offset: usize,
        size: usize,
    ) -> Result<(), Error> {
        let source_end = source_offset
            .checked_add(size)
            .ok_or_else(|| Error::invalid_argument("source copy range overflow"))?;
        let destination_end = destination_offset
            .checked_add(size)
            .ok_or_else(|| Error::invalid_argument("destination copy range overflow"))?;
        if source_end > source.length() || destination_end > destination.length() {
            return Err(Error::invalid_argument(
                "buffer copy range is out of bounds",
            ));
        }
        if size == 0 {
            return Ok(());
        }
        // SAFETY: both buffers are retained for the encoder lifetime, and the
        // checked offsets and size are inside their Metal allocations.
        unsafe {
            self.inner
                .copyFromBuffer_sourceOffset_toBuffer_destinationOffset_size(
                    &source.inner,
                    source_offset,
                    &destination.inner,
                    destination_offset,
                    size,
                );
        }
        Ok(())
    }

    /// Copies a checked texture region between matching textures.
    #[allow(clippy::too_many_arguments)]
    pub fn copy_texture_region(
        &self,
        source: &Texture,
        source_slice: usize,
        source_level: usize,
        source_region: Region,
        destination: &Texture,
        destination_slice: usize,
        destination_level: usize,
        destination_origin: crate::metal::Origin,
    ) -> Result<(), Error> {
        validate_texture_region(source, source_slice, source_level, source_region)?;
        validate_texture_region(
            destination,
            destination_slice,
            destination_level,
            Region::new(destination_origin, source_region.size),
        )?;
        if source.pixel_format() != destination.pixel_format()
            || source.layout().3 != destination.layout().3
        {
            return Err(Error::invalid_argument(
                "texture-region copy requires matching pixel formats and sample counts",
            ));
        }
        // SAFETY: source and destination mip/slice regions, formats, and
        // sample counts were checked and both retained textures outlive this
        // command encoding operation.
        unsafe {
            self.inner.copyFromTexture_sourceSlice_sourceLevel_sourceOrigin_sourceSize_toTexture_destinationSlice_destinationLevel_destinationOrigin(
                &source.inner,
                source_slice,
                source_level,
                source_region.origin.into(),
                source_region.size.into(),
                &destination.inner,
                destination_slice,
                destination_level,
                destination_origin.into(),
            );
        }
        Ok(())
    }

    /// Copies a checked linear buffer layout into a texture region.
    #[allow(clippy::too_many_arguments)]
    pub fn copy_buffer_to_texture(
        &self,
        source: &Buffer,
        source_offset: usize,
        source_bytes_per_row: usize,
        source_bytes_per_image: usize,
        source_size: crate::metal::Size,
        destination: &Texture,
        destination_slice: usize,
        destination_level: usize,
        destination_origin: crate::metal::Origin,
    ) -> Result<(), Error> {
        let region = Region::new(destination_origin, source_size);
        validate_texture_region(destination, destination_slice, destination_level, region)?;
        validate_linear_texture_layout(
            &self.inner,
            source,
            source_offset,
            source_bytes_per_row,
            source_bytes_per_image,
            source_size,
            destination.pixel_format(),
        )?;
        // SAFETY: the destination region and complete source buffer byte
        // footprint, including row/image strides and required alignment, were
        // checked above. Both retained resources remain alive for submission.
        unsafe {
            self.inner.copyFromBuffer_sourceOffset_sourceBytesPerRow_sourceBytesPerImage_sourceSize_toTexture_destinationSlice_destinationLevel_destinationOrigin(
                &source.inner,
                source_offset,
                source_bytes_per_row,
                source_bytes_per_image,
                source_size.into(),
                &destination.inner,
                destination_slice,
                destination_level,
                destination_origin.into(),
            );
        }
        Ok(())
    }

    /// Safe substitute for the option-bearing buffer-to-texture overload.
    #[allow(clippy::too_many_arguments)]
    pub fn copy_buffer_to_texture_with_options(
        &self,
        source: &Buffer,
        source_offset: usize,
        source_bytes_per_row: usize,
        source_bytes_per_image: usize,
        source_size: crate::metal::Size,
        destination: &Texture,
        destination_slice: usize,
        destination_level: usize,
        destination_origin: crate::metal::Origin,
        options: BlitOption,
    ) -> Result<(), Error> {
        if !options.is_valid() {
            return Err(Error::invalid_argument("blit options contain unknown bits"));
        }
        if options.as_raw() != BlitOption::BlitOptionNone.as_raw() {
            return Err(Error::unsupported(
                "depth/stencil and row-linear PVRTC copies require an unaudited byte layout",
            ));
        }
        self.copy_buffer_to_texture(
            source,
            source_offset,
            source_bytes_per_row,
            source_bytes_per_image,
            source_size,
            destination,
            destination_slice,
            destination_level,
            destination_origin,
        )
    }

    /// Copies a checked texture region into a linear buffer layout.
    #[allow(clippy::too_many_arguments)]
    pub fn copy_texture_to_buffer(
        &self,
        source: &Texture,
        source_slice: usize,
        source_level: usize,
        source_region: Region,
        destination: &Buffer,
        destination_offset: usize,
        destination_bytes_per_row: usize,
        destination_bytes_per_image: usize,
    ) -> Result<(), Error> {
        validate_texture_region(source, source_slice, source_level, source_region)?;
        validate_linear_texture_layout(
            &self.inner,
            destination,
            destination_offset,
            destination_bytes_per_row,
            destination_bytes_per_image,
            source_region.size,
            source.pixel_format(),
        )?;
        // SAFETY: the source region and complete destination buffer byte
        // footprint, including row/image strides and required alignment, were
        // checked above. Both retained resources remain alive for submission.
        unsafe {
            self.inner.copyFromTexture_sourceSlice_sourceLevel_sourceOrigin_sourceSize_toBuffer_destinationOffset_destinationBytesPerRow_destinationBytesPerImage(
                &source.inner,
                source_slice,
                source_level,
                source_region.origin.into(),
                source_region.size.into(),
                &destination.inner,
                destination_offset,
                destination_bytes_per_row,
                destination_bytes_per_image,
            );
        }
        Ok(())
    }

    /// Safe substitute for the option-bearing texture-to-buffer overload.
    #[allow(clippy::too_many_arguments)]
    pub fn copy_texture_to_buffer_with_options(
        &self,
        source: &Texture,
        source_slice: usize,
        source_level: usize,
        source_region: Region,
        destination: &Buffer,
        destination_offset: usize,
        destination_bytes_per_row: usize,
        destination_bytes_per_image: usize,
        options: BlitOption,
    ) -> Result<(), Error> {
        if !options.is_valid() {
            return Err(Error::invalid_argument("blit options contain unknown bits"));
        }
        if options.as_raw() != BlitOption::BlitOptionNone.as_raw() {
            return Err(Error::unsupported(
                "depth/stencil and row-linear PVRTC copies require an unaudited byte layout",
            ));
        }
        self.copy_texture_to_buffer(
            source,
            source_slice,
            source_level,
            source_region,
            destination,
            destination_offset,
            destination_bytes_per_row,
            destination_bytes_per_image,
        )
    }

    /// Copies a checked tensor slice, including safe reshape copies.
    #[allow(clippy::too_many_arguments)]
    pub fn copy_tensor(
        &self,
        source: &Tensor,
        source_layout: &TensorLayout,
        source_origin: &[usize],
        source_dimensions: &[usize],
        destination: &Tensor,
        destination_layout: &TensorLayout,
        destination_origin: &[usize],
        destination_dimensions: &[usize],
    ) -> Result<(), Error> {
        validate_tensor_layout(source, source_layout)?;
        validate_tensor_layout(destination, destination_layout)?;
        validate_tensor_slice(source_layout, source_origin, source_dimensions)?;
        validate_tensor_slice(
            destination_layout,
            destination_origin,
            destination_dimensions,
        )?;
        if source_layout.data_type() != destination_layout.data_type()
            || checked_element_count(source_dimensions)?
                != checked_element_count(destination_dimensions)?
        {
            return Err(Error::invalid_argument(
                "tensor copy requires matching data types and element counts",
            ));
        }
        let source_origin = ObjectiveCTensorExtents::new(source_origin)?;
        let source_dimensions = ObjectiveCTensorExtents::new(source_dimensions)?;
        let destination_origin = ObjectiveCTensorExtents::new(destination_origin)?;
        let destination_dimensions = ObjectiveCTensorExtents::new(destination_dimensions)?;
        if !self.inner.respondsToSelector(
            sel!(copyFromTensor:sourceOrigin:sourceDimensions:toTensor:destinationOrigin:destinationDimensions:),
        ) {
            return Err(Error::unsupported("tensor blit copies are unavailable"));
        }
        // SAFETY: runtime layouts match checked Rust layout proofs; origins,
        // extents, types, element counts, and all retained arguments were checked.
        unsafe {
            let _: () = msg_send![
                &*self.inner,
                copyFromTensor: source.as_inner(),
                sourceOrigin: &*source_origin.inner,
                sourceDimensions: &*source_dimensions.inner,
                toTensor: destination.as_inner(),
                destinationOrigin: &*destination_origin.inner,
                destinationDimensions: &*destination_dimensions.inner
            ];
        }
        Ok(())
    }

    /// Safe data-plane substitute for the plane-selecting tensor overload.
    #[allow(clippy::too_many_arguments)]
    pub fn copy_tensor_plane(
        &self,
        source: &Tensor,
        source_layout: &TensorLayout,
        source_origin: &[usize],
        source_dimensions: &[usize],
        source_plane: TensorPlaneType,
        destination: &Tensor,
        destination_layout: &TensorLayout,
        destination_origin: &[usize],
        destination_dimensions: &[usize],
        destination_plane: TensorPlaneType,
    ) -> Result<(), Error> {
        if !source_plane.is_valid() || !destination_plane.is_valid() {
            return Err(Error::invalid_argument("tensor plane is invalid"));
        }
        let data = TensorPlaneType::TensorPlaneTypeData.as_raw();
        if source_plane.as_raw() != data || destination_plane.as_raw() != data {
            return Err(Error::unsupported(
                "scale-plane blits require an audited auxiliary-plane layout",
            ));
        }
        self.copy_tensor(
            source,
            source_layout,
            source_origin,
            source_dimensions,
            destination,
            destination_layout,
            destination_origin,
            destination_dimensions,
        )
    }

    /// Copies checked whole mip levels across a checked slice range.
    #[allow(clippy::too_many_arguments)]
    pub fn copy_texture_subresources(
        &self,
        source: &Texture,
        source_slice: usize,
        source_level: usize,
        destination: &Texture,
        destination_slice: usize,
        destination_level: usize,
        slice_count: usize,
        level_count: usize,
    ) -> Result<(), Error> {
        if slice_count == 0 || level_count == 0 {
            return Err(Error::invalid_argument(
                "texture slice and level counts must be non-zero",
            ));
        }
        let source_slice_end = source_slice
            .checked_add(slice_count)
            .ok_or_else(|| Error::invalid_argument("source texture slice range overflow"))?;
        let destination_slice_end = destination_slice
            .checked_add(slice_count)
            .ok_or_else(|| Error::invalid_argument("destination texture slice range overflow"))?;
        let source_level_end = source_level
            .checked_add(level_count)
            .ok_or_else(|| Error::invalid_argument("source texture level range overflow"))?;
        let destination_level_end = destination_level
            .checked_add(level_count)
            .ok_or_else(|| Error::invalid_argument("destination texture level range overflow"))?;
        let (_, _, source_levels, source_samples) = source.layout();
        let (_, _, destination_levels, destination_samples) = destination.layout();
        if source_slice_end > texture_slice_count(source)?
            || destination_slice_end > texture_slice_count(destination)?
            || source_level_end > source_levels
            || destination_level_end > destination_levels
            || source_samples != 1
            || destination_samples != 1
            || source.pixel_format() != destination.pixel_format()
            || source.texture_type()? != destination.texture_type()?
        {
            return Err(Error::invalid_argument(
                "texture subresource ranges or layouts are incompatible",
            ));
        }
        for relative_level in 0..level_count {
            if mip_extent(source, source_level + relative_level)?
                != mip_extent(destination, destination_level + relative_level)?
            {
                return Err(Error::invalid_argument(
                    "texture mip dimensions are incompatible",
                ));
            }
        }
        // SAFETY: non-empty slice/level ranges, every mip extent, texture type,
        // sample count, and pixel format were checked for compatibility.
        unsafe {
            self.inner.copyFromTexture_sourceSlice_sourceLevel_toTexture_destinationSlice_destinationLevel_sliceCount_levelCount(
                &source.inner,
                source_slice,
                source_level,
                &destination.inner,
                destination_slice,
                destination_level,
                slice_count,
                level_count,
            );
        }
        Ok(())
    }

    /// Fills a checked buffer range with a byte value.
    pub fn fill_buffer(
        &self,
        buffer: &Buffer,
        range: std::ops::Range<usize>,
        value: u8,
    ) -> Result<(), Error> {
        if range.start > range.end || range.end > buffer.length() {
            return Err(Error::invalid_argument(
                "buffer fill range is out of bounds",
            ));
        }
        if !range.is_empty() {
            self.inner.fillBuffer_range_value(
                &buffer.inner,
                NSRange::new(range.start, range.len()),
                value,
            );
        }
        Ok(())
    }

    /// Copies an entire compatible texture.
    pub fn copy_texture(&self, source: &Texture, destination: &Texture) -> Result<(), Error> {
        if source.width() != destination.width()
            || source.height() != destination.height()
            || source.layout() != destination.layout()
            || source.texture_type()? != destination.texture_type()?
            || source.pixel_format() != destination.pixel_format()
        {
            return Err(Error::invalid_argument(
                "texture copy requires identical type, layout, dimensions, and pixel format",
            ));
        }
        // SAFETY: both textures are retained and their dimensions and pixel
        // formats were checked for whole-texture copy compatibility.
        unsafe {
            self.inner
                .copyFromTexture_toTexture(&source.inner, &destination.inner)
        };
        Ok(())
    }

    /// Generates mipmaps for a texture with more than one mip level.
    pub fn generate_mipmaps(&self, texture: &Texture) -> Result<(), Error> {
        if texture.inner.mipmapLevelCount() <= 1 {
            return Err(Error::invalid_argument(
                "mipmap generation requires more than one mip level",
            ));
        }
        self.inner.generateMipmapsForTexture(&texture.inner);
        Ok(())
    }

    /// Synchronizes one texture slice and mip level for managed CPU access.
    pub fn synchronize_texture(
        &self,
        texture: &Texture,
        slice: usize,
        level: usize,
    ) -> Result<(), Error> {
        if texture.storage_mode() != StorageMode::Managed {
            return Err(Error::invalid_argument(
                "only managed textures require explicit synchronization",
            ));
        }
        validate_texture_subresource(texture, slice, level)?;
        // SAFETY: the texture is retained and both the slice and mip level are
        // within its declared ranges.
        unsafe {
            self.inner
                .synchronizeTexture_slice_level(&texture.inner, slice, level)
        };
        Ok(())
    }

    /// Synchronizes a managed buffer for subsequent CPU access.
    pub fn synchronize_buffer(&self, buffer: &Buffer) -> Result<(), Error> {
        if buffer.storage_mode() != StorageMode::Managed {
            return Err(Error::invalid_argument(
                "only managed buffers require explicit synchronization",
            ));
        }
        // SAFETY: MTLBuffer inherits MTLResource, the retained buffer remains
        // live, and its managed storage mode was checked above.
        unsafe {
            let _: () = objc2::msg_send![&*self.inner, synchronizeResource: buffer.as_any_object()];
        }
        Ok(())
    }

    /// Synchronizes an entire managed texture through MTLResource semantics.
    pub fn synchronize_texture_resource(&self, texture: &Texture) -> Result<(), Error> {
        if texture.storage_mode() != StorageMode::Managed {
            return Err(Error::invalid_argument(
                "only managed textures require explicit synchronization",
            ));
        }
        // SAFETY: MTLTexture inherits MTLResource, the retained texture remains
        // live, and its managed storage mode was checked above.
        unsafe {
            let _: () = msg_send![&*self.inner, synchronizeResource: texture.as_any_object()];
        }
        Ok(())
    }

    /// Optimizes a checked texture slice and mip level for CPU access.
    pub fn optimize_texture_slice_for_cpu(
        &self,
        texture: &Texture,
        slice: usize,
        level: usize,
    ) -> Result<(), Error> {
        validate_texture_subresource(texture, slice, level)?;
        if !self
            .inner
            .respondsToSelector(sel!(optimizeContentsForCPUAccess:slice:level:))
        {
            return Err(Error::unsupported(
                "per-subresource CPU texture optimization is unavailable",
            ));
        }
        // SAFETY: selector availability and subresource bounds were checked.
        unsafe {
            self.inner
                .optimizeContentsForCPUAccess_slice_level(&texture.inner, slice, level)
        };
        Ok(())
    }

    /// Optimizes a checked texture slice and mip level for GPU access.
    pub fn optimize_texture_slice_for_gpu(
        &self,
        texture: &Texture,
        slice: usize,
        level: usize,
    ) -> Result<(), Error> {
        validate_texture_subresource(texture, slice, level)?;
        if !self
            .inner
            .respondsToSelector(sel!(optimizeContentsForGPUAccess:slice:level:))
        {
            return Err(Error::unsupported(
                "per-subresource GPU texture optimization is unavailable",
            ));
        }
        // SAFETY: selector availability and subresource bounds were checked.
        unsafe {
            self.inner
                .optimizeContentsForGPUAccess_slice_level(&texture.inner, slice, level)
        };
        Ok(())
    }

    /// Resets a checked range in an indirect command buffer.
    pub fn reset_indirect_commands(
        &self,
        buffer: &IndirectCommandBuffer,
        range: std::ops::Range<usize>,
    ) -> Result<(), Error> {
        validate_indirect_range(buffer, &range)?;
        if !self
            .inner
            .respondsToSelector(sel!(resetCommandsInBuffer:withRange:))
        {
            return Err(Error::unsupported("indirect command reset is unavailable"));
        }
        // SAFETY: the wrapper is type-checked as MTLIndirectCommandBuffer, the
        // selector is present, and the range is within its reported size.
        unsafe {
            let _: () = objc2::msg_send![
                &*self.inner,
                resetCommandsInBuffer: buffer.as_inner(),
                withRange: NSRange::new(range.start, range.len())
            ];
        }
        Ok(())
    }

    /// Copies a checked range between indirect command buffers.
    pub fn copy_indirect_commands(
        &self,
        source: &IndirectCommandBuffer,
        source_range: std::ops::Range<usize>,
        destination: &IndirectCommandBuffer,
        destination_index: usize,
    ) -> Result<(), Error> {
        validate_indirect_range(source, &source_range)?;
        let destination_end = destination_index
            .checked_add(source_range.len())
            .ok_or_else(|| Error::invalid_argument("indirect command destination overflow"))?;
        if destination_end > indirect_size(destination)? {
            return Err(Error::invalid_argument(
                "indirect command destination range is out of bounds",
            ));
        }
        if !self.inner.respondsToSelector(
            sel!(copyIndirectCommandBuffer:sourceRange:destination:destinationIndex:),
        ) {
            return Err(Error::unsupported(
                "indirect command buffer copy is unavailable",
            ));
        }
        // SAFETY: both wrappers are type-checked as indirect command buffers,
        // selector availability was checked, and both ranges are in bounds.
        unsafe {
            let _: () = objc2::msg_send![
                &*self.inner,
                copyIndirectCommandBuffer: source.as_inner(),
                sourceRange: NSRange::new(source_range.start, source_range.len()),
                destination: destination.as_inner(),
                destinationIndex: destination_index
            ];
        }
        Ok(())
    }

    /// Optimizes a checked indirect command range for execution.
    pub fn optimize_indirect_commands(
        &self,
        buffer: &IndirectCommandBuffer,
        range: std::ops::Range<usize>,
    ) -> Result<(), Error> {
        validate_indirect_range(buffer, &range)?;
        if !self
            .inner
            .respondsToSelector(sel!(optimizeIndirectCommandBuffer:withRange:))
        {
            return Err(Error::unsupported(
                "indirect command optimization is unavailable",
            ));
        }
        // SAFETY: the wrapper type, selector availability, and range were checked.
        unsafe {
            let _: () = objc2::msg_send![
                &*self.inner,
                optimizeIndirectCommandBuffer: buffer.as_inner(),
                withRange: NSRange::new(range.start, range.len())
            ];
        }
        Ok(())
    }

    /// Samples counters at a checked index.
    pub fn sample_counters(
        &self,
        sample_buffer: &CounterSampleBuffer,
        sample_index: usize,
        barrier: bool,
    ) -> Result<(), Error> {
        let sample_count = sample_buffer.sample_count()?;
        if sample_index >= sample_count {
            return Err(Error::invalid_argument(
                "counter sample index is out of bounds",
            ));
        }
        if !self
            .inner
            .respondsToSelector(sel!(sampleCountersInBuffer:atSampleIndex:withBarrier:))
        {
            return Err(Error::unsupported("blit counter sampling is unavailable"));
        }
        // SAFETY: the wrapper is type-checked as MTLCounterSampleBuffer, the
        // selector exists, and sample_index is inside sampleCount.
        unsafe {
            let _: () = objc2::msg_send![
                &*self.inner,
                sampleCountersInBuffer: sample_buffer.as_inner(),
                atSampleIndex: sample_index,
                withBarrier: barrier
            ];
        }
        Ok(())
    }

    /// Resolves a checked counter range into submission-bound staging.
    ///
    /// The shared sample buffer's CPU resolve result is used only to obtain the
    /// framework-defined exact byte length. Counter contents are returned only
    /// through `CompletedCommandBuffer::resolve_buffer` for this submission.
    pub fn read_counters(
        &self,
        sample_buffer: &CounterSampleBuffer,
        range: std::ops::Range<usize>,
    ) -> Result<BufferReadback, Error> {
        let sample_count = sample_buffer.sample_count()?;
        if range.start >= range.end || range.end > sample_count {
            return Err(Error::invalid_argument(
                "counter resolve range is empty or out of bounds",
            ));
        }
        let object = sample_buffer.as_inner();
        // SAFETY: NSObject respondsToSelector: uses the selector/bool ABI.
        let can_measure: bool =
            unsafe { msg_send![object, respondsToSelector: sel!(resolveCounterRange:)] };
        if !can_measure
            || !self.inner.respondsToSelector(
                sel!(resolveCounters:inRange:destinationBuffer:destinationOffset:),
            )
        {
            return Err(Error::unsupported("counter resolving is unavailable"));
        }
        // SAFETY: range is inside sampleCount; this API returns owned NSData
        // for shared counter buffers. Its contents are discarded and only its
        // exact framework-defined byte length is used for staging allocation.
        let measured: Option<Retained<NSData>> = unsafe {
            msg_send![object, resolveCounterRange: NSRange::new(range.start, range.len())]
        };
        let length = measured
            .map(|data| data.length())
            .filter(|&length| length != 0)
            .ok_or_else(|| {
                Error::unsupported("counter byte layout cannot be measured for this sample buffer")
            })?;
        let inner = self
            .inner
            .device()
            .newBufferWithLength_options(length, ResourceOptions::SHARED.as_objc())
            .ok_or_else(|| Error::unsupported("Metal could not allocate counter staging"))?;
        let buffer = Buffer::new(inner, StorageMode::Shared);
        // SAFETY: sample range bounds and exact resolved byte length were
        // obtained from Metal; destination offset is zero and the staging
        // buffer has exactly that retained allocation.
        unsafe {
            let _: () = msg_send![
                &*self.inner,
                resolveCounters: object,
                inRange: NSRange::new(range.start, range.len()),
                destinationBuffer: buffer.as_any_object(),
                destinationOffset: 0usize
            ];
        }
        Ok(BufferReadback {
            buffer,
            submission_id: self.submission_id,
            length,
        })
    }

    /// Resets sparse texture access counters for a checked region.
    pub fn reset_texture_access_counters(
        &self,
        texture: &Texture,
        region: Region,
        level: usize,
        slice: usize,
    ) -> Result<(), Error> {
        if !texture.is_sparse() {
            return Err(Error::invalid_argument(
                "texture access counters require a sparse texture",
            ));
        }
        validate_sparse_tile_region(&self.inner, texture, slice, level, region)?;
        if !self
            .inner
            .respondsToSelector(sel!(resetTextureAccessCounters:region:mipLevel:slice:))
        {
            return Err(Error::unsupported(
                "sparse texture access counters are unavailable",
            ));
        }
        // SAFETY: selector availability, sparse-texture requirement, and the
        // complete mip/slice region were checked above.
        unsafe {
            self.inner.resetTextureAccessCounters_region_mipLevel_slice(
                &texture.inner,
                region.into(),
                level,
                slice,
            );
        }
        Ok(())
    }

    /// Copies sparse-tile access counters into submission-bound staging.
    pub fn read_texture_access_counters(
        &self,
        texture: &Texture,
        region: Region,
        level: usize,
        slice: usize,
        reset_counters: bool,
    ) -> Result<BufferReadback, Error> {
        if !texture.is_sparse() {
            return Err(Error::invalid_argument(
                "texture access counters require a sparse texture",
            ));
        }
        validate_sparse_tile_region(&self.inner, texture, slice, level, region)?;
        if !self.inner.respondsToSelector(sel!(getTextureAccessCounters:region:mipLevel:slice:resetCounters:countersBuffer:countersBufferOffset:)) {
            return Err(Error::unsupported(
                "sparse texture access counters are unavailable",
            ));
        }
        // Apple defines one row-major uint32 counter per requested sparse tile.
        let length = region
            .size
            .width
            .checked_mul(region.size.height)
            .and_then(|value| value.checked_mul(region.size.depth))
            .and_then(|value| value.checked_mul(std::mem::size_of::<u32>()))
            .ok_or_else(|| Error::invalid_argument("texture counter staging size overflow"))?;
        let inner = self
            .inner
            .device()
            .newBufferWithLength_options(length, ResourceOptions::SHARED.as_objc())
            .ok_or_else(|| Error::unsupported("Metal could not allocate counter staging"))?;
        let buffer = Buffer::new(inner, StorageMode::Shared);
        // SAFETY: sparse tile-region bounds were validated, the selector is
        // present, and staging contains exactly one u32 per requested tile.
        unsafe {
            self.inner.getTextureAccessCounters_region_mipLevel_slice_resetCounters_countersBuffer_countersBufferOffset(
                &texture.inner,
                region.into(),
                level,
                slice,
                reset_counters,
                &buffer.inner,
                0,
            );
        }
        Ok(BufferReadback {
            buffer,
            submission_id: self.submission_id,
            length,
        })
    }

    /// Updates a fence after all prior blit commands.
    pub fn update_fence(&self, fence: &Fence) -> Result<(), Error> {
        if !self.inner.respondsToSelector(sel!(updateFence:)) {
            return Err(Error::unsupported("blit fences are unavailable"));
        }
        // SAFETY: Fence is a type-checked retained MTLFence wrapper and the
        // selector was checked before sending it.
        unsafe {
            let _: () = objc2::msg_send![&*self.inner, updateFence: fence.as_inner()];
        }
        Ok(())
    }

    /// Waits for a fence before subsequent blit commands.
    pub fn wait_for_fence(&self, fence: &Fence) -> Result<(), Error> {
        if !self.inner.respondsToSelector(sel!(waitForFence:)) {
            return Err(Error::unsupported("blit fences are unavailable"));
        }
        // SAFETY: Fence is a type-checked retained MTLFence wrapper and the
        // selector was checked before sending it.
        unsafe {
            let _: () = objc2::msg_send![&*self.inner, waitForFence: fence.as_inner()];
        }
        Ok(())
    }

    /// Optimizes an entire texture for CPU access when supported.
    pub fn optimize_texture_for_cpu(&self, texture: &Texture) -> Result<(), Error> {
        if !self
            .inner
            .respondsToSelector(sel!(optimizeContentsForCPUAccess:))
        {
            return Err(Error::unsupported(
                "CPU texture-content optimization is unavailable",
            ));
        }
        // SAFETY: selector availability was checked and the retained texture is
        // valid for the duration of encoding.
        unsafe { self.inner.optimizeContentsForCPUAccess(&texture.inner) };
        Ok(())
    }

    /// Optimizes an entire texture for GPU access when supported.
    pub fn optimize_texture_for_gpu(&self, texture: &Texture) -> Result<(), Error> {
        if !self
            .inner
            .respondsToSelector(sel!(optimizeContentsForGPUAccess:))
        {
            return Err(Error::unsupported(
                "GPU texture-content optimization is unavailable",
            ));
        }
        self.inner.optimizeContentsForGPUAccess(&texture.inner);
        Ok(())
    }

    /// Ends encoding and releases the borrow of the command buffer.
    pub fn end_encoding(mut self) {
        self.inner.endEncoding();
        self.ended = true;
    }
}

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

fn validate_texture_subresource(
    texture: &Texture,
    slice: usize,
    level: usize,
) -> Result<(), Error> {
    let (_, _, levels, samples) = texture.layout();
    if level >= levels || slice >= texture_slice_count(texture)? {
        return Err(Error::invalid_argument(
            "texture slice or mip level is out of bounds",
        ));
    }
    if samples > 1 {
        return Err(Error::unsupported(
            "multisample texture blits are not exposed by this safe path",
        ));
    }
    Ok(())
}

fn validate_texture_region(
    texture: &Texture,
    slice: usize,
    level: usize,
    region: Region,
) -> Result<(), Error> {
    validate_texture_subresource(texture, slice, level)?;
    if region.size.width == 0 || region.size.height == 0 || region.size.depth == 0 {
        return Err(Error::invalid_argument(
            "texture copy region must be non-empty",
        ));
    }
    let (width, height, depth) = mip_extent(texture, level)?;
    let end_x = region
        .origin
        .x
        .checked_add(region.size.width)
        .ok_or_else(|| Error::invalid_argument("texture x range overflow"))?;
    let end_y = region
        .origin
        .y
        .checked_add(region.size.height)
        .ok_or_else(|| Error::invalid_argument("texture y range overflow"))?;
    let end_z = region
        .origin
        .z
        .checked_add(region.size.depth)
        .ok_or_else(|| Error::invalid_argument("texture z range overflow"))?;
    if end_x > width || end_y > height || end_z > depth {
        return Err(Error::invalid_argument(
            "texture copy region is out of mip bounds",
        ));
    }
    Ok(())
}

fn validate_sparse_tile_region(
    encoder: &ProtocolObject<dyn MTLBlitCommandEncoder>,
    texture: &Texture,
    slice: usize,
    level: usize,
    region: Region,
) -> Result<(), Error> {
    validate_texture_subresource(texture, slice, level)?;
    if region.size.width == 0 || region.size.height == 0 || region.size.depth == 0 {
        return Err(Error::invalid_argument(
            "sparse tile region must be non-empty",
        ));
    }
    if !encoder
        .device()
        .respondsToSelector(sel!(sparseTileSizeWithTextureType:pixelFormat:sampleCount:))
    {
        return Err(Error::unsupported("sparse tile geometry is unavailable"));
    }
    // SAFETY: selector availability was checked and all value arguments come
    // directly from a retained texture with checked enum representations.
    let tile = unsafe {
        encoder
            .device()
            .sparseTileSizeWithTextureType_pixelFormat_sampleCount(
                texture.texture_type()?.as_objc(),
                texture.pixel_format().as_objc(),
                texture.layout().3,
            )
    };
    if tile.width == 0 || tile.height == 0 || tile.depth == 0 {
        return Err(Error::unsupported(
            "Metal returned invalid sparse tile geometry",
        ));
    }
    let (width, height, depth) = mip_extent(texture, level)?;
    let tiles = (
        width.div_ceil(tile.width),
        height.div_ceil(tile.height),
        depth.div_ceil(tile.depth),
    );
    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 > tiles.0)
        || end_y.is_none_or(|end| end > tiles.1)
        || end_z.is_none_or(|end| end > tiles.2)
    {
        return Err(Error::invalid_argument(
            "sparse tile region is out of mip bounds",
        ));
    }
    Ok(())
}

fn mip_extent(texture: &Texture, level: usize) -> Result<(usize, usize, usize), Error> {
    let (depth, _, _, _) = texture.layout();
    let shift = u32::try_from(level)
        .map_err(|_| Error::invalid_argument("texture mip level cannot be represented"))?;
    let width = texture.width().checked_shr(shift).unwrap_or(0).max(1);
    let height = texture.height().checked_shr(shift).unwrap_or(0).max(1);
    let depth = if texture.texture_type()? == TextureType::D3 {
        depth.checked_shr(shift).unwrap_or(0).max(1)
    } else {
        1
    };
    Ok((width, height, depth))
}

fn audited_bytes_per_pixel(pixel_format: PixelFormat) -> Result<usize, Error> {
    match pixel_format {
        PixelFormat::RGBA8_UNORM
        | PixelFormat::BGRA8_UNORM
        | PixelFormat::RGBA8_UNORM_SRGB
        | PixelFormat::BGRA8_UNORM_SRGB
        | PixelFormat::RG16_FLOAT
        | PixelFormat::DEPTH32_FLOAT => Ok(4),
        PixelFormat::R16_FLOAT => Ok(2),
        PixelFormat::RGBA16_FLOAT => Ok(8),
        _ => Err(Error::unsupported(
            "linear texture copy format has no audited byte layout",
        )),
    }
}

fn validate_linear_texture_layout(
    encoder: &ProtocolObject<dyn MTLBlitCommandEncoder>,
    buffer: &Buffer,
    offset: usize,
    bytes_per_row: usize,
    bytes_per_image: usize,
    size: crate::metal::Size,
    pixel_format: PixelFormat,
) -> Result<(), Error> {
    let bytes_per_pixel = audited_bytes_per_pixel(pixel_format)?;
    let active_row_bytes = size
        .width
        .checked_mul(bytes_per_pixel)
        .ok_or_else(|| Error::invalid_argument("active texture row size overflow"))?;
    let minimum_image_bytes = bytes_per_row
        .checked_mul(size.height)
        .ok_or_else(|| Error::invalid_argument("texture image stride overflow"))?;
    if bytes_per_row < active_row_bytes || bytes_per_image < minimum_image_bytes {
        return Err(Error::invalid_argument(
            "texture row or image stride is too small",
        ));
    }
    let alignment = encoder
        .device()
        .minimumLinearTextureAlignmentForPixelFormat(pixel_format.as_objc())
        .max(1);
    if !offset.is_multiple_of(alignment) || !bytes_per_row.is_multiple_of(alignment) {
        return Err(Error::invalid_argument(
            "texture buffer offset and row stride do not meet Metal alignment",
        ));
    }
    let required = offset
        .checked_add(
            bytes_per_image
                .checked_mul(size.depth.saturating_sub(1))
                .ok_or_else(|| Error::invalid_argument("texture depth footprint overflow"))?,
        )
        .and_then(|value| {
            value.checked_add(bytes_per_row.checked_mul(size.height.saturating_sub(1))?)
        })
        .and_then(|value| value.checked_add(active_row_bytes))
        .ok_or_else(|| Error::invalid_argument("texture buffer footprint overflow"))?;
    if required > buffer.length() {
        return Err(Error::invalid_argument(
            "texture buffer footprint is out of bounds",
        ));
    }
    Ok(())
}

fn indirect_size(buffer: &IndirectCommandBuffer) -> Result<usize, Error> {
    buffer.size()
}

fn validate_indirect_range(
    buffer: &IndirectCommandBuffer,
    range: &std::ops::Range<usize>,
) -> Result<(), Error> {
    if range.start > range.end || range.end > indirect_size(buffer)? {
        return Err(Error::invalid_argument(
            "indirect command range is out of bounds",
        ));
    }
    Ok(())
}

struct ObjectiveCTensorExtents {
    inner: Retained<AnyObject>,
}

impl ObjectiveCTensorExtents {
    fn new(values: &[usize]) -> Result<Self, Error> {
        if values.len() > crate::metal::MAX_TENSOR_RANK
            || values.iter().any(|&value| value > isize::MAX as usize)
        {
            return Err(Error::invalid_argument(
                "tensor extents exceed Metal's rank or integer limits",
            ));
        }
        let class = AnyClass::get(c"MTLTensorExtents")
            .ok_or_else(|| Error::unsupported("MTLTensorExtents is unavailable"))?;
        // SAFETY: the class identity is checked before querying its initializer.
        let available: bool =
            unsafe { msg_send![class, instancesRespondToSelector: sel!(initWithRank:values:)] };
        if !available {
            return Err(Error::unsupported(
                "MTLTensorExtents initializer is unavailable",
            ));
        }
        let signed: Vec<isize> = values.iter().map(|&value| value as isize).collect();
        // SAFETY: alloc is sent to the checked MTLTensorExtents class.
        let allocated: Allocated<AnyObject> = unsafe { msg_send![class, alloc] };
        let pointer = if signed.is_empty() {
            std::ptr::null()
        } else {
            signed.as_ptr()
        };
        // SAFETY: pointer covers exactly rank NSInteger elements for this call;
        // MTLTensorExtents copies the input during initialization.
        let inner: Option<Retained<AnyObject>> =
            unsafe { msg_send![allocated, initWithRank: signed.len(), values: pointer] };
        inner
            .map(|inner| Self { inner })
            .ok_or_else(|| Error::invalid_argument("Metal rejected tensor extents"))
    }
}

fn read_tensor_extents(object: &AnyObject) -> Result<Vec<usize>, Error> {
    // SAFETY: NSObject respondsToSelector: uses the declared selector/bool ABI.
    let has_rank: bool = unsafe { msg_send![object, respondsToSelector: sel!(rank)] };
    // SAFETY: NSObject respondsToSelector: uses the declared selector/bool ABI.
    let has_extent: bool =
        unsafe { msg_send![object, respondsToSelector: sel!(extentAtDimensionIndex:)] };
    if !has_rank || !has_extent {
        return Err(Error::unsupported(
            "tensor extent inspection is unavailable",
        ));
    }
    // SAFETY: rank selector and NSUInteger ABI were checked above.
    let rank: usize = unsafe { msg_send![object, rank] };
    if rank > crate::metal::MAX_TENSOR_RANK {
        return Err(Error::unsupported("Metal returned an invalid tensor rank"));
    }
    let mut values = Vec::with_capacity(rank);
    for index in 0..rank {
        // SAFETY: extent selector exists and index is strictly below rank.
        let value: isize = unsafe { msg_send![object, extentAtDimensionIndex: index] };
        values.push(
            usize::try_from(value)
                .map_err(|_| Error::unsupported("Metal returned a negative tensor extent"))?,
        );
    }
    Ok(values)
}

fn validate_tensor_layout(tensor: &Tensor, layout: &TensorLayout) -> Result<(), Error> {
    if tensor.data_type()? != layout.data_type() {
        return Err(Error::invalid_argument(
            "tensor data type does not match its layout proof",
        ));
    }
    let dimensions = tensor
        .dimensions()?
        .ok_or_else(|| Error::unsupported("tensor dimensions are unavailable"))?;
    let strides = tensor
        .strides()?
        .ok_or_else(|| Error::unsupported("tensor strides are unavailable"))?;
    if read_tensor_extents(dimensions.as_inner())? != layout.dimensions().as_slice()
        || read_tensor_extents(strides.as_inner())? != layout.strides().as_slice()
    {
        return Err(Error::invalid_argument(
            "tensor runtime layout does not match its Rust proof",
        ));
    }
    Ok(())
}

fn validate_tensor_slice(
    layout: &TensorLayout,
    origin: &[usize],
    dimensions: &[usize],
) -> Result<(), Error> {
    let rank = layout.dimensions().rank();
    if origin.len() != rank || dimensions.len() != rank || dimensions.contains(&0) {
        return Err(Error::invalid_argument(
            "tensor slice origin and dimensions must match rank and be non-zero",
        ));
    }
    for ((&origin, &size), &extent) in origin
        .iter()
        .zip(dimensions)
        .zip(layout.dimensions().as_slice())
    {
        let end = origin
            .checked_add(size)
            .ok_or_else(|| Error::invalid_argument("tensor slice range overflow"))?;
        if end > extent {
            return Err(Error::invalid_argument(
                "tensor slice is out of layout bounds",
            ));
        }
    }
    Ok(())
}

fn checked_element_count(dimensions: &[usize]) -> Result<usize, Error> {
    dimensions.iter().try_fold(1_usize, |count, &dimension| {
        count
            .checked_mul(dimension)
            .ok_or_else(|| Error::invalid_argument("tensor slice element count overflow"))
    })
}

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

#[cfg(test)]
mod tests {
    use super::*;
    use crate::metal::generated_value_types::TensorDataType;

    #[test]
    fn tensor_slice_bounds_are_checked() {
        let layout = TensorLayout::dense(&[4, 3], TensorDataType::TensorDataTypeFloat32).unwrap();
        assert!(validate_tensor_slice(&layout, &[1, 1], &[3, 2]).is_ok());
        assert!(validate_tensor_slice(&layout, &[2, 1], &[3, 2]).is_err());
        assert!(validate_tensor_slice(&layout, &[0], &[1]).is_err());
        assert!(validate_tensor_slice(&layout, &[0, 0], &[4, 0]).is_err());
    }

    #[test]
    fn tensor_element_count_rejects_overflow() {
        assert_eq!(checked_element_count(&[2, 3, 4]).unwrap(), 24);
        assert!(checked_element_count(&[usize::MAX, 2]).is_err());
    }
}