summaryrefslogtreecommitdiff
path: root/contrib/SDL-3.2.8/src/render/metal/SDL_render_metal.m
blob: 63d9770d5025a48ebbe0a536a67e3fc5b2c46709 (plain)
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
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
/*
  Simple DirectMedia Layer
  Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>

  This software is provided 'as-is', without any express or implied
  warranty.  In no event will the authors be held liable for any damages
  arising from the use of this software.

  Permission is granted to anyone to use this software for any purpose,
  including commercial applications, and to alter it and redistribute it
  freely, subject to the following restrictions:

  1. The origin of this software must not be misrepresented; you must not
     claim that you wrote the original software. If you use this software
     in a product, an acknowledgment in the product documentation would be
     appreciated but is not required.
  2. Altered source versions must be plainly marked as such, and must not be
     misrepresented as being the original software.
  3. This notice may not be removed or altered from any source distribution.
*/
#include "SDL_internal.h"

#ifdef SDL_VIDEO_RENDER_METAL

#include "../SDL_sysrender.h"
#include "../../video/SDL_pixels_c.h"

#import <CoreVideo/CoreVideo.h>
#import <Metal/Metal.h>
#import <QuartzCore/CAMetalLayer.h>

#ifdef SDL_VIDEO_DRIVER_COCOA
#import <AppKit/NSWindow.h>
#import <AppKit/NSView.h>
#endif
#ifdef SDL_VIDEO_DRIVER_UIKIT
#import <UIKit/UIKit.h>
#endif

// Regenerate these with build-metal-shaders.sh
#ifdef SDL_PLATFORM_MACOS
#include "SDL_shaders_metal_macos.h"
#elif defined(SDL_PLATFORM_TVOS)
#if TARGET_OS_SIMULATOR
#include "SDL_shaders_metal_tvsimulator.h"
#else
#include "SDL_shaders_metal_tvos.h"
#endif
#else
#if TARGET_OS_SIMULATOR
#include "SDL_shaders_metal_iphonesimulator.h"
#else
#include "SDL_shaders_metal_ios.h"
#endif
#endif

// Apple Metal renderer implementation

// macOS requires constants in a buffer to have a 256 byte alignment.
// Use native type alignments from https://developer.apple.com/metal/Metal-Shading-Language-Specification.pdf
#if defined(SDL_PLATFORM_MACOS) || TARGET_OS_SIMULATOR || TARGET_OS_MACCATALYST
#define CONSTANT_ALIGN(x) (256)
#else
#define CONSTANT_ALIGN(x) (x < 4 ? 4 : x)
#endif

#define DEVICE_ALIGN(x) (x < 4 ? 4 : x)

#define ALIGN_CONSTANTS(align, size) ((size + CONSTANT_ALIGN(align) - 1) & (~(CONSTANT_ALIGN(align) - 1)))

static const size_t CONSTANTS_OFFSET_INVALID = 0xFFFFFFFF;
static const size_t CONSTANTS_OFFSET_IDENTITY = 0;
static const size_t CONSTANTS_OFFSET_HALF_PIXEL_TRANSFORM = ALIGN_CONSTANTS(16, CONSTANTS_OFFSET_IDENTITY + sizeof(float) * 16);
static const size_t CONSTANTS_OFFSET_DECODE_BT601_LIMITED = ALIGN_CONSTANTS(16, CONSTANTS_OFFSET_HALF_PIXEL_TRANSFORM + sizeof(float) * 16);
static const size_t CONSTANTS_OFFSET_DECODE_BT601_FULL = ALIGN_CONSTANTS(16, CONSTANTS_OFFSET_DECODE_BT601_LIMITED + sizeof(float) * 4 * 4);
static const size_t CONSTANTS_OFFSET_DECODE_BT709_LIMITED = ALIGN_CONSTANTS(16, CONSTANTS_OFFSET_DECODE_BT601_FULL + sizeof(float) * 4 * 4);
static const size_t CONSTANTS_OFFSET_DECODE_BT709_FULL = ALIGN_CONSTANTS(16, CONSTANTS_OFFSET_DECODE_BT709_LIMITED + sizeof(float) * 4 * 4);
static const size_t CONSTANTS_OFFSET_DECODE_BT2020_LIMITED = ALIGN_CONSTANTS(16, CONSTANTS_OFFSET_DECODE_BT709_FULL + sizeof(float) * 4 * 4);
static const size_t CONSTANTS_OFFSET_DECODE_BT2020_FULL = ALIGN_CONSTANTS(16, CONSTANTS_OFFSET_DECODE_BT2020_LIMITED + sizeof(float) * 4 * 4);
static const size_t CONSTANTS_LENGTH = CONSTANTS_OFFSET_DECODE_BT2020_FULL + sizeof(float) * 4 * 4;

// Sampler types
typedef enum
{
    SDL_METAL_SAMPLER_NEAREST_CLAMP,
    SDL_METAL_SAMPLER_NEAREST_WRAP,
    SDL_METAL_SAMPLER_LINEAR_CLAMP,
    SDL_METAL_SAMPLER_LINEAR_WRAP,
    SDL_NUM_METAL_SAMPLERS
} SDL_METAL_sampler_type;

typedef enum SDL_MetalVertexFunction
{
    SDL_METAL_VERTEX_SOLID,
    SDL_METAL_VERTEX_COPY,
} SDL_MetalVertexFunction;

typedef enum SDL_MetalFragmentFunction
{
    SDL_METAL_FRAGMENT_SOLID = 0,
    SDL_METAL_FRAGMENT_COPY,
    SDL_METAL_FRAGMENT_YUV,
    SDL_METAL_FRAGMENT_NV12,
    SDL_METAL_FRAGMENT_COUNT,
} SDL_MetalFragmentFunction;

typedef struct METAL_PipelineState
{
    SDL_BlendMode blendMode;
    void *pipe;
} METAL_PipelineState;

typedef struct METAL_PipelineCache
{
    METAL_PipelineState *states;
    int count;
    SDL_MetalVertexFunction vertexFunction;
    SDL_MetalFragmentFunction fragmentFunction;
    MTLPixelFormat renderTargetFormat;
    const char *label;
} METAL_PipelineCache;

/* Each shader combination used by drawing functions has a separate pipeline
 * cache, and we have a separate list of caches for each render target pixel
 * format. This is more efficient than iterating over a global cache to find
 * the pipeline based on the specified shader combination and RT pixel format,
 * since we know what the RT pixel format is when we set the render target, and
 * we know what the shader combination is inside each drawing function's code. */
typedef struct METAL_ShaderPipelines
{
    MTLPixelFormat renderTargetFormat;
    METAL_PipelineCache caches[SDL_METAL_FRAGMENT_COUNT];
} METAL_ShaderPipelines;

@interface SDL3METAL_RenderData : NSObject
@property(nonatomic, retain) id<MTLDevice> mtldevice;
@property(nonatomic, retain) id<MTLCommandQueue> mtlcmdqueue;
@property(nonatomic, retain) id<MTLCommandBuffer> mtlcmdbuffer;
@property(nonatomic, retain) id<MTLRenderCommandEncoder> mtlcmdencoder;
@property(nonatomic, retain) id<MTLLibrary> mtllibrary;
@property(nonatomic, retain) id<CAMetalDrawable> mtlbackbuffer;
@property(nonatomic, retain) NSMutableArray<id<MTLSamplerState>> *mtlsamplers;
@property(nonatomic, retain) id<MTLBuffer> mtlbufconstants;
@property(nonatomic, retain) id<MTLBuffer> mtlbufquadindices;
@property(nonatomic, assign) SDL_MetalView mtlview;
@property(nonatomic, retain) CAMetalLayer *mtllayer;
@property(nonatomic, retain) MTLRenderPassDescriptor *mtlpassdesc;
@property(nonatomic, assign) METAL_ShaderPipelines *activepipelines;
@property(nonatomic, assign) METAL_ShaderPipelines *allpipelines;
@property(nonatomic, assign) int pipelinescount;
@end

@implementation SDL3METAL_RenderData
@end

@interface SDL3METAL_TextureData : NSObject
@property(nonatomic, retain) id<MTLTexture> mtltexture;
@property(nonatomic, retain) id<MTLTexture> mtltextureUv;
@property(nonatomic, assign) SDL_MetalFragmentFunction fragmentFunction;
#ifdef SDL_HAVE_YUV
@property(nonatomic, assign) BOOL yuv;
@property(nonatomic, assign) BOOL nv12;
@property(nonatomic, assign) size_t conversionBufferOffset;
#endif
@property(nonatomic, assign) BOOL hasdata;
@property(nonatomic, retain) id<MTLBuffer> lockedbuffer;
@property(nonatomic, assign) SDL_Rect lockedrect;
@end

@implementation SDL3METAL_TextureData
@end

static const MTLBlendOperation invalidBlendOperation = (MTLBlendOperation)0xFFFFFFFF;
static const MTLBlendFactor invalidBlendFactor = (MTLBlendFactor)0xFFFFFFFF;

static MTLBlendOperation GetBlendOperation(SDL_BlendOperation operation)
{
    switch (operation) {
    case SDL_BLENDOPERATION_ADD:
        return MTLBlendOperationAdd;
    case SDL_BLENDOPERATION_SUBTRACT:
        return MTLBlendOperationSubtract;
    case SDL_BLENDOPERATION_REV_SUBTRACT:
        return MTLBlendOperationReverseSubtract;
    case SDL_BLENDOPERATION_MINIMUM:
        return MTLBlendOperationMin;
    case SDL_BLENDOPERATION_MAXIMUM:
        return MTLBlendOperationMax;
    default:
        return invalidBlendOperation;
    }
}

static MTLBlendFactor GetBlendFactor(SDL_BlendFactor factor)
{
    switch (factor) {
    case SDL_BLENDFACTOR_ZERO:
        return MTLBlendFactorZero;
    case SDL_BLENDFACTOR_ONE:
        return MTLBlendFactorOne;
    case SDL_BLENDFACTOR_SRC_COLOR:
        return MTLBlendFactorSourceColor;
    case SDL_BLENDFACTOR_ONE_MINUS_SRC_COLOR:
        return MTLBlendFactorOneMinusSourceColor;
    case SDL_BLENDFACTOR_SRC_ALPHA:
        return MTLBlendFactorSourceAlpha;
    case SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA:
        return MTLBlendFactorOneMinusSourceAlpha;
    case SDL_BLENDFACTOR_DST_COLOR:
        return MTLBlendFactorDestinationColor;
    case SDL_BLENDFACTOR_ONE_MINUS_DST_COLOR:
        return MTLBlendFactorOneMinusDestinationColor;
    case SDL_BLENDFACTOR_DST_ALPHA:
        return MTLBlendFactorDestinationAlpha;
    case SDL_BLENDFACTOR_ONE_MINUS_DST_ALPHA:
        return MTLBlendFactorOneMinusDestinationAlpha;
    default:
        return invalidBlendFactor;
    }
}

static NSString *GetVertexFunctionName(SDL_MetalVertexFunction function)
{
    switch (function) {
    case SDL_METAL_VERTEX_SOLID:
        return @"SDL_Solid_vertex";
    case SDL_METAL_VERTEX_COPY:
        return @"SDL_Copy_vertex";
    default:
        return nil;
    }
}

static NSString *GetFragmentFunctionName(SDL_MetalFragmentFunction function)
{
    switch (function) {
    case SDL_METAL_FRAGMENT_SOLID:
        return @"SDL_Solid_fragment";
    case SDL_METAL_FRAGMENT_COPY:
        return @"SDL_Copy_fragment";
    case SDL_METAL_FRAGMENT_YUV:
        return @"SDL_YUV_fragment";
    case SDL_METAL_FRAGMENT_NV12:
        return @"SDL_NV12_fragment";
    default:
        return nil;
    }
}

static id<MTLRenderPipelineState> MakePipelineState(SDL3METAL_RenderData *data, METAL_PipelineCache *cache,
                                                    NSString *blendlabel, SDL_BlendMode blendmode)
{
    MTLRenderPipelineDescriptor *mtlpipedesc;
    MTLVertexDescriptor *vertdesc;
    MTLRenderPipelineColorAttachmentDescriptor *rtdesc;
    NSError *err = nil;
    id<MTLRenderPipelineState> state;
    METAL_PipelineState pipeline;
    METAL_PipelineState *states;

    id<MTLFunction> mtlvertfn = [data.mtllibrary newFunctionWithName:GetVertexFunctionName(cache->vertexFunction)];
    id<MTLFunction> mtlfragfn = [data.mtllibrary newFunctionWithName:GetFragmentFunctionName(cache->fragmentFunction)];
    SDL_assert(mtlvertfn != nil);
    SDL_assert(mtlfragfn != nil);

    mtlpipedesc = [[MTLRenderPipelineDescriptor alloc] init];
    mtlpipedesc.vertexFunction = mtlvertfn;
    mtlpipedesc.fragmentFunction = mtlfragfn;

    vertdesc = [MTLVertexDescriptor vertexDescriptor];

    switch (cache->vertexFunction) {
    case SDL_METAL_VERTEX_SOLID:
        // position (float2), color (float4)
        vertdesc.layouts[0].stride = sizeof(float) * 2 + sizeof(float) * 4;
        vertdesc.layouts[0].stepFunction = MTLVertexStepFunctionPerVertex;

        vertdesc.attributes[0].format = MTLVertexFormatFloat2;
        vertdesc.attributes[0].offset = 0;
        vertdesc.attributes[0].bufferIndex = 0;

        vertdesc.attributes[1].format = MTLVertexFormatFloat4;
        vertdesc.attributes[1].offset = sizeof(float) * 2;
        vertdesc.attributes[1].bufferIndex = 0;

        break;
    case SDL_METAL_VERTEX_COPY:
        // position (float2), color (float4), texcoord (float2)
        vertdesc.layouts[0].stride = sizeof(float) * 2 + sizeof(float) * 4 + sizeof(float) * 2;
        vertdesc.layouts[0].stepFunction = MTLVertexStepFunctionPerVertex;

        vertdesc.attributes[0].format = MTLVertexFormatFloat2;
        vertdesc.attributes[0].offset = 0;
        vertdesc.attributes[0].bufferIndex = 0;

        vertdesc.attributes[1].format = MTLVertexFormatFloat4;
        vertdesc.attributes[1].offset = sizeof(float) * 2;
        vertdesc.attributes[1].bufferIndex = 0;

        vertdesc.attributes[2].format = MTLVertexFormatFloat2;
        vertdesc.attributes[2].offset = sizeof(float) * 2 + sizeof(float) * 4;
        vertdesc.attributes[2].bufferIndex = 0;
        break;
    }

    mtlpipedesc.vertexDescriptor = vertdesc;

    rtdesc = mtlpipedesc.colorAttachments[0];
    rtdesc.pixelFormat = cache->renderTargetFormat;

    if (blendmode != SDL_BLENDMODE_NONE) {
        rtdesc.blendingEnabled = YES;
        rtdesc.sourceRGBBlendFactor = GetBlendFactor(SDL_GetBlendModeSrcColorFactor(blendmode));
        rtdesc.destinationRGBBlendFactor = GetBlendFactor(SDL_GetBlendModeDstColorFactor(blendmode));
        rtdesc.rgbBlendOperation = GetBlendOperation(SDL_GetBlendModeColorOperation(blendmode));
        rtdesc.sourceAlphaBlendFactor = GetBlendFactor(SDL_GetBlendModeSrcAlphaFactor(blendmode));
        rtdesc.destinationAlphaBlendFactor = GetBlendFactor(SDL_GetBlendModeDstAlphaFactor(blendmode));
        rtdesc.alphaBlendOperation = GetBlendOperation(SDL_GetBlendModeAlphaOperation(blendmode));
    } else {
        rtdesc.blendingEnabled = NO;
    }

    mtlpipedesc.label = [@(cache->label) stringByAppendingString:blendlabel];

    state = [data.mtldevice newRenderPipelineStateWithDescriptor:mtlpipedesc error:&err];
    SDL_assert(err == nil);

    pipeline.blendMode = blendmode;
    pipeline.pipe = (void *)CFBridgingRetain(state);

    states = SDL_realloc(cache->states, (cache->count + 1) * sizeof(pipeline));

    if (states) {
        states[cache->count++] = pipeline;
        cache->states = states;
        return (__bridge id<MTLRenderPipelineState>)pipeline.pipe;
    } else {
        CFBridgingRelease(pipeline.pipe);
        return NULL;
    }
}

static void MakePipelineCache(SDL3METAL_RenderData *data, METAL_PipelineCache *cache, const char *label,
                              MTLPixelFormat rtformat, SDL_MetalVertexFunction vertfn, SDL_MetalFragmentFunction fragfn)
{
    SDL_zerop(cache);

    cache->vertexFunction = vertfn;
    cache->fragmentFunction = fragfn;
    cache->renderTargetFormat = rtformat;
    cache->label = label;

    /* Create pipeline states for the default blend modes. Custom blend modes
     * will be added to the cache on-demand. */
    MakePipelineState(data, cache, @" (blend=none)", SDL_BLENDMODE_NONE);
    MakePipelineState(data, cache, @" (blend=blend)", SDL_BLENDMODE_BLEND);
    MakePipelineState(data, cache, @" (blend=add)", SDL_BLENDMODE_ADD);
    MakePipelineState(data, cache, @" (blend=mod)", SDL_BLENDMODE_MOD);
    MakePipelineState(data, cache, @" (blend=mul)", SDL_BLENDMODE_MUL);
}

static void DestroyPipelineCache(METAL_PipelineCache *cache)
{
    if (cache != NULL) {
        for (int i = 0; i < cache->count; i++) {
            CFBridgingRelease(cache->states[i].pipe);
        }

        SDL_free(cache->states);
    }
}

void MakeShaderPipelines(SDL3METAL_RenderData *data, METAL_ShaderPipelines *pipelines, MTLPixelFormat rtformat)
{
    SDL_zerop(pipelines);

    pipelines->renderTargetFormat = rtformat;

    MakePipelineCache(data, &pipelines->caches[SDL_METAL_FRAGMENT_SOLID], "SDL primitives pipeline", rtformat, SDL_METAL_VERTEX_SOLID, SDL_METAL_FRAGMENT_SOLID);
    MakePipelineCache(data, &pipelines->caches[SDL_METAL_FRAGMENT_COPY], "SDL copy pipeline", rtformat, SDL_METAL_VERTEX_COPY, SDL_METAL_FRAGMENT_COPY);
    MakePipelineCache(data, &pipelines->caches[SDL_METAL_FRAGMENT_YUV], "SDL YUV pipeline", rtformat, SDL_METAL_VERTEX_COPY, SDL_METAL_FRAGMENT_YUV);
    MakePipelineCache(data, &pipelines->caches[SDL_METAL_FRAGMENT_NV12], "SDL NV12 pipeline", rtformat, SDL_METAL_VERTEX_COPY, SDL_METAL_FRAGMENT_NV12);
}

static METAL_ShaderPipelines *ChooseShaderPipelines(SDL3METAL_RenderData *data, MTLPixelFormat rtformat)
{
    METAL_ShaderPipelines *allpipelines = data.allpipelines;
    int count = data.pipelinescount;

    for (int i = 0; i < count; i++) {
        if (allpipelines[i].renderTargetFormat == rtformat) {
            return &allpipelines[i];
        }
    }

    allpipelines = SDL_realloc(allpipelines, (count + 1) * sizeof(METAL_ShaderPipelines));

    if (allpipelines == NULL) {
        return NULL;
    }

    MakeShaderPipelines(data, &allpipelines[count], rtformat);

    data.allpipelines = allpipelines;
    data.pipelinescount = count + 1;

    return &data.allpipelines[count];
}

static void DestroyAllPipelines(METAL_ShaderPipelines *allpipelines, int count)
{
    if (allpipelines != NULL) {
        for (int i = 0; i < count; i++) {
            for (int cache = 0; cache < SDL_METAL_FRAGMENT_COUNT; cache++) {
                DestroyPipelineCache(&allpipelines[i].caches[cache]);
            }
        }

        SDL_free(allpipelines);
    }
}

static inline id<MTLRenderPipelineState> ChoosePipelineState(SDL3METAL_RenderData *data, METAL_ShaderPipelines *pipelines, SDL_MetalFragmentFunction fragfn, SDL_BlendMode blendmode)
{
    METAL_PipelineCache *cache = &pipelines->caches[fragfn];

    for (int i = 0; i < cache->count; i++) {
        if (cache->states[i].blendMode == blendmode) {
            return (__bridge id<MTLRenderPipelineState>)cache->states[i].pipe;
        }
    }

    return MakePipelineState(data, cache, [NSString stringWithFormat:@" (blend=custom 0x%x)", blendmode], blendmode);
}

static bool METAL_ActivateRenderCommandEncoder(SDL_Renderer *renderer, MTLLoadAction load, MTLClearColor *clear_color, id<MTLBuffer> vertex_buffer)
{
    SDL3METAL_RenderData *data = (__bridge SDL3METAL_RenderData *)renderer->internal;

    /* Our SetRenderTarget just signals that the next render operation should
     * set up a new render pass. This is where that work happens. */
    if (data.mtlcmdencoder == nil) {
        id<MTLTexture> mtltexture = nil;

        if (renderer->target != NULL) {
            SDL3METAL_TextureData *texdata = (__bridge SDL3METAL_TextureData *)renderer->target->internal;
            mtltexture = texdata.mtltexture;
        } else {
            if (data.mtlbackbuffer == nil) {
                /* The backbuffer's contents aren't guaranteed to persist after
                 * presenting, so we can leave it undefined when loading it. */
                data.mtlbackbuffer = [data.mtllayer nextDrawable];
                if (load == MTLLoadActionLoad) {
                    load = MTLLoadActionDontCare;
                }
            }
            if (data.mtlbackbuffer != nil) {
                mtltexture = data.mtlbackbuffer.texture;
            }
        }

        /* mtltexture can be nil here if macOS refused to give us a drawable,
           which apparently can happen for minimized windows, etc. */
        if (mtltexture == nil) {
            return false;
        }

        if (load == MTLLoadActionClear) {
            SDL_assert(clear_color != NULL);
            data.mtlpassdesc.colorAttachments[0].clearColor = *clear_color;
        }

        data.mtlpassdesc.colorAttachments[0].loadAction = load;
        data.mtlpassdesc.colorAttachments[0].texture = mtltexture;

        data.mtlcmdbuffer = [data.mtlcmdqueue commandBuffer];
        data.mtlcmdencoder = [data.mtlcmdbuffer renderCommandEncoderWithDescriptor:data.mtlpassdesc];

        if (data.mtlbackbuffer != nil && mtltexture == data.mtlbackbuffer.texture) {
            data.mtlcmdencoder.label = @"SDL metal renderer backbuffer";
        } else {
            data.mtlcmdencoder.label = @"SDL metal renderer render target";
        }

        /* Set up buffer bindings for positions, texcoords, and color once here,
         * the offsets are adjusted in the code that uses them. */
        if (vertex_buffer != nil) {
            [data.mtlcmdencoder setVertexBuffer:vertex_buffer offset:0 atIndex:0];
            [data.mtlcmdencoder setFragmentBuffer:vertex_buffer offset:0 atIndex:0];
        }

        data.activepipelines = ChooseShaderPipelines(data, mtltexture.pixelFormat);

        // make sure this has a definite place in the queue. This way it will
        //  execute reliably whether the app tries to make its own command buffers
        //  or whatever. This means we can _always_ batch rendering commands!
        [data.mtlcmdbuffer enqueue];
    }

    return true;
}

static void METAL_WindowEvent(SDL_Renderer *renderer, const SDL_WindowEvent *event)
{
}

static bool METAL_GetOutputSize(SDL_Renderer *renderer, int *w, int *h)
{
    @autoreleasepool {
        SDL3METAL_RenderData *data = (__bridge SDL3METAL_RenderData *)renderer->internal;
        if (w) {
            *w = (int)data.mtllayer.drawableSize.width;
        }
        if (h) {
            *h = (int)data.mtllayer.drawableSize.height;
        }
        return true;
    }
}

static bool METAL_SupportsBlendMode(SDL_Renderer *renderer, SDL_BlendMode blendMode)
{
    SDL_BlendFactor srcColorFactor = SDL_GetBlendModeSrcColorFactor(blendMode);
    SDL_BlendFactor srcAlphaFactor = SDL_GetBlendModeSrcAlphaFactor(blendMode);
    SDL_BlendOperation colorOperation = SDL_GetBlendModeColorOperation(blendMode);
    SDL_BlendFactor dstColorFactor = SDL_GetBlendModeDstColorFactor(blendMode);
    SDL_BlendFactor dstAlphaFactor = SDL_GetBlendModeDstAlphaFactor(blendMode);
    SDL_BlendOperation alphaOperation = SDL_GetBlendModeAlphaOperation(blendMode);

    if (GetBlendFactor(srcColorFactor) == invalidBlendFactor ||
        GetBlendFactor(srcAlphaFactor) == invalidBlendFactor ||
        GetBlendOperation(colorOperation) == invalidBlendOperation ||
        GetBlendFactor(dstColorFactor) == invalidBlendFactor ||
        GetBlendFactor(dstAlphaFactor) == invalidBlendFactor ||
        GetBlendOperation(alphaOperation) == invalidBlendOperation) {
        return false;
    }
    return true;
}

size_t GetBT601ConversionMatrix(SDL_Colorspace colorspace)
{
    switch (SDL_COLORSPACERANGE(colorspace)) {
    case SDL_COLOR_RANGE_LIMITED:
    case SDL_COLOR_RANGE_UNKNOWN:
        return CONSTANTS_OFFSET_DECODE_BT601_LIMITED;
    case SDL_COLOR_RANGE_FULL:
        return CONSTANTS_OFFSET_DECODE_BT601_FULL;
    default:
        break;
    }
    return 0;
}

size_t GetBT709ConversionMatrix(SDL_Colorspace colorspace)
{
    switch (SDL_COLORSPACERANGE(colorspace)) {
    case SDL_COLOR_RANGE_LIMITED:
    case SDL_COLOR_RANGE_UNKNOWN:
        return CONSTANTS_OFFSET_DECODE_BT709_LIMITED;
    case SDL_COLOR_RANGE_FULL:
        return CONSTANTS_OFFSET_DECODE_BT709_FULL;
    default:
        break;
    }
    return 0;
}

size_t GetBT2020ConversionMatrix(SDL_Colorspace colorspace)
{
    switch (SDL_COLORSPACERANGE(colorspace)) {
    case SDL_COLOR_RANGE_LIMITED:
    case SDL_COLOR_RANGE_UNKNOWN:
        return CONSTANTS_OFFSET_DECODE_BT2020_LIMITED;
    case SDL_COLOR_RANGE_FULL:
        return CONSTANTS_OFFSET_DECODE_BT2020_FULL;
    default:
        break;
    }
    return 0;
}

size_t GetYCbCRtoRGBConversionMatrix(SDL_Colorspace colorspace, int w, int h, int bits_per_pixel)
{
    const int YUV_SD_THRESHOLD = 576;

    switch (SDL_COLORSPACEMATRIX(colorspace)) {
    case SDL_MATRIX_COEFFICIENTS_BT470BG:
    case SDL_MATRIX_COEFFICIENTS_BT601:
        return GetBT601ConversionMatrix(colorspace);

    case SDL_MATRIX_COEFFICIENTS_BT709:
        return GetBT709ConversionMatrix(colorspace);

    case SDL_MATRIX_COEFFICIENTS_BT2020_NCL:
        return GetBT2020ConversionMatrix(colorspace);

    case SDL_MATRIX_COEFFICIENTS_UNSPECIFIED:
        switch (bits_per_pixel) {
        case 8:
            if (h <= YUV_SD_THRESHOLD) {
                return GetBT601ConversionMatrix(colorspace);
            } else {
                return GetBT709ConversionMatrix(colorspace);
            }
        case 10:
        case 16:
            return GetBT2020ConversionMatrix(colorspace);
        default:
            break;
        }
        break;
    default:
        break;
    }
    return 0;
}

static bool METAL_CreateTexture(SDL_Renderer *renderer, SDL_Texture *texture, SDL_PropertiesID create_props)
{
    @autoreleasepool {
        SDL3METAL_RenderData *data = (__bridge SDL3METAL_RenderData *)renderer->internal;
        MTLPixelFormat pixfmt;
        MTLTextureDescriptor *mtltexdesc;
        id<MTLTexture> mtltexture = nil, mtltextureUv = nil;
        SDL3METAL_TextureData *texturedata;
        CVPixelBufferRef pixelbuffer = nil;
        IOSurfaceRef surface = nil;

        pixelbuffer = SDL_GetPointerProperty(create_props, SDL_PROP_TEXTURE_CREATE_METAL_PIXELBUFFER_POINTER, nil);
        if (pixelbuffer) {
            surface = CVPixelBufferGetIOSurface(pixelbuffer);
            if (!surface) {
                return SDL_SetError("CVPixelBufferGetIOSurface() failed");
            }
        }

        switch (texture->format) {
        case SDL_PIXELFORMAT_ABGR8888:
            if (renderer->output_colorspace == SDL_COLORSPACE_SRGB_LINEAR) {
                pixfmt = MTLPixelFormatRGBA8Unorm_sRGB;
            } else {
                pixfmt = MTLPixelFormatRGBA8Unorm;
            }
            break;
        case SDL_PIXELFORMAT_ARGB8888:
            if (renderer->output_colorspace == SDL_COLORSPACE_SRGB_LINEAR) {
                pixfmt = MTLPixelFormatBGRA8Unorm_sRGB;
            } else {
                pixfmt = MTLPixelFormatBGRA8Unorm;
            }
            break;
        case SDL_PIXELFORMAT_ABGR2101010:
            pixfmt = MTLPixelFormatRGB10A2Unorm;
            break;
        case SDL_PIXELFORMAT_IYUV:
        case SDL_PIXELFORMAT_YV12:
        case SDL_PIXELFORMAT_NV12:
        case SDL_PIXELFORMAT_NV21:
            pixfmt = MTLPixelFormatR8Unorm;
            break;
        case SDL_PIXELFORMAT_P010:
            pixfmt = MTLPixelFormatR16Unorm;
            break;
        case SDL_PIXELFORMAT_RGBA64_FLOAT:
            pixfmt = MTLPixelFormatRGBA16Float;
            break;
        case SDL_PIXELFORMAT_RGBA128_FLOAT:
            pixfmt = MTLPixelFormatRGBA32Float;
            break;
        default:
            return SDL_SetError("Texture format %s not supported by Metal", SDL_GetPixelFormatName(texture->format));
        }

        mtltexdesc = [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:pixfmt
                                                                        width:(NSUInteger)texture->w
                                                                       height:(NSUInteger)texture->h
                                                                    mipmapped:NO];

        if (texture->access == SDL_TEXTUREACCESS_TARGET) {
            mtltexdesc.usage = MTLTextureUsageShaderRead | MTLTextureUsageRenderTarget;
        } else {
            mtltexdesc.usage = MTLTextureUsageShaderRead;
        }

        if (surface) {
            mtltexture = [data.mtldevice newTextureWithDescriptor:mtltexdesc iosurface:surface plane:0];
        } else {
            mtltexture = [data.mtldevice newTextureWithDescriptor:mtltexdesc];
        }
        if (mtltexture == nil) {
            return SDL_SetError("Texture allocation failed");
        }

        mtltextureUv = nil;
#ifdef SDL_HAVE_YUV
        BOOL yuv = (texture->format == SDL_PIXELFORMAT_IYUV || texture->format == SDL_PIXELFORMAT_YV12);
        BOOL nv12 = (texture->format == SDL_PIXELFORMAT_NV12 || texture->format == SDL_PIXELFORMAT_NV21 || texture->format == SDL_PIXELFORMAT_P010);

        if (yuv) {
            mtltexdesc.pixelFormat = MTLPixelFormatR8Unorm;
            mtltexdesc.width = (texture->w + 1) / 2;
            mtltexdesc.height = (texture->h + 1) / 2;
            mtltexdesc.textureType = MTLTextureType2DArray;
            mtltexdesc.arrayLength = 2;
        } else if (texture->format == SDL_PIXELFORMAT_P010) {
            mtltexdesc.pixelFormat = MTLPixelFormatRG16Unorm;
            mtltexdesc.width = (texture->w + 1) / 2;
            mtltexdesc.height = (texture->h + 1) / 2;
        } else if (nv12) {
            mtltexdesc.pixelFormat = MTLPixelFormatRG8Unorm;
            mtltexdesc.width = (texture->w + 1) / 2;
            mtltexdesc.height = (texture->h + 1) / 2;
        }

        if (yuv || nv12) {
            if (surface) {
                mtltextureUv = [data.mtldevice newTextureWithDescriptor:mtltexdesc iosurface:surface plane:1];
            } else {
                mtltextureUv = [data.mtldevice newTextureWithDescriptor:mtltexdesc];
            }
            if (mtltextureUv == nil) {
                return SDL_SetError("Texture allocation failed");
            }
        }
#endif // SDL_HAVE_YUV
        texturedata = [[SDL3METAL_TextureData alloc] init];
#ifdef SDL_HAVE_YUV
        if (yuv) {
            texturedata.fragmentFunction = SDL_METAL_FRAGMENT_YUV;
        } else if (nv12) {
            texturedata.fragmentFunction = SDL_METAL_FRAGMENT_NV12;
        } else
#endif
        {
            texturedata.fragmentFunction = SDL_METAL_FRAGMENT_COPY;
        }
        texturedata.mtltexture = mtltexture;
        texturedata.mtltextureUv = mtltextureUv;
#ifdef SDL_HAVE_YUV
        texturedata.yuv = yuv;
        texturedata.nv12 = nv12;
        if (yuv || nv12) {
            size_t offset = GetYCbCRtoRGBConversionMatrix(texture->colorspace, texture->w, texture->h, 8);
            if (offset == 0) {
                return SDL_SetError("Unsupported YUV colorspace");
            }
            texturedata.conversionBufferOffset = offset;
        }
#endif
        texture->internal = (void *)CFBridgingRetain(texturedata);

        return true;
    }
}

static void METAL_UploadTextureData(id<MTLTexture> texture, SDL_Rect rect, int slice,
                                    const void *pixels, int pitch)
{
    [texture replaceRegion:MTLRegionMake2D(rect.x, rect.y, rect.w, rect.h)
               mipmapLevel:0
                     slice:slice
                 withBytes:pixels
               bytesPerRow:pitch
             bytesPerImage:0];
}

static MTLStorageMode METAL_GetStorageMode(id<MTLResource> resource)
{
    return resource.storageMode;
}

static bool METAL_UpdateTextureInternal(SDL_Renderer *renderer, SDL3METAL_TextureData *texturedata,
                                       id<MTLTexture> texture, SDL_Rect rect, int slice,
                                       const void *pixels, int pitch)
{
    SDL3METAL_RenderData *data = (__bridge SDL3METAL_RenderData *)renderer->internal;
    SDL_Rect stagingrect = { 0, 0, rect.w, rect.h };
    MTLTextureDescriptor *desc;
    id<MTLTexture> stagingtex;
    id<MTLBlitCommandEncoder> blitcmd;

    /* If the texture is managed or shared and this is the first upload, we can
     * use replaceRegion to upload to it directly. Otherwise we upload the data
     * to a staging texture and copy that over. */
    if (!texturedata.hasdata && METAL_GetStorageMode(texture) != MTLStorageModePrivate) {
        METAL_UploadTextureData(texture, rect, slice, pixels, pitch);
        return true;
    }

    desc = [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:texture.pixelFormat
                                                              width:rect.w
                                                             height:rect.h
                                                          mipmapped:NO];

    if (desc == nil) {
        return SDL_OutOfMemory();
    }

    /* TODO: We could have a pool of textures or a MTLHeap we allocate from,
     * and release a staging texture back to the pool in the command buffer's
     * completion handler. */
    stagingtex = [data.mtldevice newTextureWithDescriptor:desc];
    if (stagingtex == nil) {
        return SDL_OutOfMemory();
    }

    METAL_UploadTextureData(stagingtex, stagingrect, 0, pixels, pitch);

    if (data.mtlcmdencoder != nil) {
        [data.mtlcmdencoder endEncoding];
        data.mtlcmdencoder = nil;
    }

    if (data.mtlcmdbuffer == nil) {
        data.mtlcmdbuffer = [data.mtlcmdqueue commandBuffer];
    }

    blitcmd = [data.mtlcmdbuffer blitCommandEncoder];

    [blitcmd copyFromTexture:stagingtex
                 sourceSlice:0
                 sourceLevel:0
                sourceOrigin:MTLOriginMake(0, 0, 0)
                  sourceSize:MTLSizeMake(rect.w, rect.h, 1)
                   toTexture:texture
            destinationSlice:slice
            destinationLevel:0
           destinationOrigin:MTLOriginMake(rect.x, rect.y, 0)];

    [blitcmd endEncoding];

    /* TODO: This isn't very efficient for the YUV formats, which call
     * UpdateTextureInternal multiple times in a row. */
    [data.mtlcmdbuffer commit];
    data.mtlcmdbuffer = nil;

    return true;
}

static bool METAL_UpdateTexture(SDL_Renderer *renderer, SDL_Texture *texture,
                               const SDL_Rect *rect, const void *pixels, int pitch)
{
    @autoreleasepool {
        SDL3METAL_TextureData *texturedata = (__bridge SDL3METAL_TextureData *)texture->internal;

        if (!METAL_UpdateTextureInternal(renderer, texturedata, texturedata.mtltexture, *rect, 0, pixels, pitch)) {
            return false;
        }
#ifdef SDL_HAVE_YUV
        if (texturedata.yuv) {
            int Uslice = texture->format == SDL_PIXELFORMAT_YV12 ? 1 : 0;
            int Vslice = texture->format == SDL_PIXELFORMAT_YV12 ? 0 : 1;
            int UVpitch = (pitch + 1) / 2;
            SDL_Rect UVrect = { rect->x / 2, rect->y / 2, (rect->w + 1) / 2, (rect->h + 1) / 2 };

            // Skip to the correct offset into the next texture
            pixels = (const void *)((const Uint8 *)pixels + rect->h * pitch);
            if (!METAL_UpdateTextureInternal(renderer, texturedata, texturedata.mtltextureUv, UVrect, Uslice, pixels, UVpitch)) {
                return false;
            }

            // Skip to the correct offset into the next texture
            pixels = (const void *)((const Uint8 *)pixels + UVrect.h * UVpitch);
            if (!METAL_UpdateTextureInternal(renderer, texturedata, texturedata.mtltextureUv, UVrect, Vslice, pixels, UVpitch)) {
                return false;
            }
        }

        if (texturedata.nv12) {
            SDL_Rect UVrect = { rect->x / 2, rect->y / 2, (rect->w + 1) / 2, (rect->h + 1) / 2 };
            int UVpitch = 2 * ((pitch + 1) / 2);

            // Skip to the correct offset into the next texture
            pixels = (const void *)((const Uint8 *)pixels + rect->h * pitch);
            if (!METAL_UpdateTextureInternal(renderer, texturedata, texturedata.mtltextureUv, UVrect, 0, pixels, UVpitch)) {
                return false;
            }
        }
#endif
        texturedata.hasdata = YES;

        return true;
    }
}

#ifdef SDL_HAVE_YUV
static bool METAL_UpdateTextureYUV(SDL_Renderer *renderer, SDL_Texture *texture,
                                  const SDL_Rect *rect,
                                  const Uint8 *Yplane, int Ypitch,
                                  const Uint8 *Uplane, int Upitch,
                                  const Uint8 *Vplane, int Vpitch)
{
    @autoreleasepool {
        SDL3METAL_TextureData *texturedata = (__bridge SDL3METAL_TextureData *)texture->internal;
        const int Uslice = 0;
        const int Vslice = 1;
        SDL_Rect UVrect = { rect->x / 2, rect->y / 2, (rect->w + 1) / 2, (rect->h + 1) / 2 };

        // Bail out if we're supposed to update an empty rectangle
        if (rect->w <= 0 || rect->h <= 0) {
            return true;
        }

        if (!METAL_UpdateTextureInternal(renderer, texturedata, texturedata.mtltexture, *rect, 0, Yplane, Ypitch)) {
            return false;
        }
        if (!METAL_UpdateTextureInternal(renderer, texturedata, texturedata.mtltextureUv, UVrect, Uslice, Uplane, Upitch)) {
            return false;
        }
        if (!METAL_UpdateTextureInternal(renderer, texturedata, texturedata.mtltextureUv, UVrect, Vslice, Vplane, Vpitch)) {
            return false;
        }

        texturedata.hasdata = YES;

        return true;
    }
}

static bool METAL_UpdateTextureNV(SDL_Renderer *renderer, SDL_Texture *texture,
                                 const SDL_Rect *rect,
                                 const Uint8 *Yplane, int Ypitch,
                                 const Uint8 *UVplane, int UVpitch)
{
    @autoreleasepool {
        SDL3METAL_TextureData *texturedata = (__bridge SDL3METAL_TextureData *)texture->internal;
        SDL_Rect UVrect = { rect->x / 2, rect->y / 2, (rect->w + 1) / 2, (rect->h + 1) / 2 };

        // Bail out if we're supposed to update an empty rectangle
        if (rect->w <= 0 || rect->h <= 0) {
            return true;
        }

        if (!METAL_UpdateTextureInternal(renderer, texturedata, texturedata.mtltexture, *rect, 0, Yplane, Ypitch)) {
            return false;
        }

        if (!METAL_UpdateTextureInternal(renderer, texturedata, texturedata.mtltextureUv, UVrect, 0, UVplane, UVpitch)) {
            return false;
        }

        texturedata.hasdata = YES;

        return true;
    }
}
#endif

static bool METAL_LockTexture(SDL_Renderer *renderer, SDL_Texture *texture,
                             const SDL_Rect *rect, void **pixels, int *pitch)
{
    @autoreleasepool {
        SDL3METAL_RenderData *data = (__bridge SDL3METAL_RenderData *)renderer->internal;
        SDL3METAL_TextureData *texturedata = (__bridge SDL3METAL_TextureData *)texture->internal;
        int buffersize = 0;
        id<MTLBuffer> lockedbuffer = nil;

        if (rect->w <= 0 || rect->h <= 0) {
            return SDL_SetError("Invalid rectangle dimensions for LockTexture.");
        }

        *pitch = SDL_BYTESPERPIXEL(texture->format) * rect->w;
#ifdef SDL_HAVE_YUV
        if (texturedata.yuv || texturedata.nv12) {
            buffersize = ((*pitch) * rect->h) + (2 * (*pitch + 1) / 2) * ((rect->h + 1) / 2);
        } else
#endif
        {
            buffersize = (*pitch) * rect->h;
        }

        lockedbuffer = [data.mtldevice newBufferWithLength:buffersize options:MTLResourceStorageModeShared];
        if (lockedbuffer == nil) {
            return SDL_OutOfMemory();
        }

        texturedata.lockedrect = *rect;
        texturedata.lockedbuffer = lockedbuffer;
        *pixels = [lockedbuffer contents];

        return true;
    }
}

static void METAL_UnlockTexture(SDL_Renderer *renderer, SDL_Texture *texture)
{
    @autoreleasepool {
        SDL3METAL_RenderData *data = (__bridge SDL3METAL_RenderData *)renderer->internal;
        SDL3METAL_TextureData *texturedata = (__bridge SDL3METAL_TextureData *)texture->internal;
        id<MTLBlitCommandEncoder> blitcmd;
        SDL_Rect rect = texturedata.lockedrect;
        int pitch = SDL_BYTESPERPIXEL(texture->format) * rect.w;
#ifdef SDL_HAVE_YUV
        SDL_Rect UVrect = { rect.x / 2, rect.y / 2, (rect.w + 1) / 2, (rect.h + 1) / 2 };
#endif

        if (texturedata.lockedbuffer == nil) {
            return;
        }

        if (data.mtlcmdencoder != nil) {
            [data.mtlcmdencoder endEncoding];
            data.mtlcmdencoder = nil;
        }

        if (data.mtlcmdbuffer == nil) {
            data.mtlcmdbuffer = [data.mtlcmdqueue commandBuffer];
        }

        blitcmd = [data.mtlcmdbuffer blitCommandEncoder];

        [blitcmd copyFromBuffer:texturedata.lockedbuffer
                   sourceOffset:0
              sourceBytesPerRow:pitch
            sourceBytesPerImage:0
                     sourceSize:MTLSizeMake(rect.w, rect.h, 1)
                      toTexture:texturedata.mtltexture
               destinationSlice:0
               destinationLevel:0
              destinationOrigin:MTLOriginMake(rect.x, rect.y, 0)];
#ifdef SDL_HAVE_YUV
        if (texturedata.yuv) {
            int Uslice = texture->format == SDL_PIXELFORMAT_YV12 ? 1 : 0;
            int Vslice = texture->format == SDL_PIXELFORMAT_YV12 ? 0 : 1;
            int UVpitch = (pitch + 1) / 2;

            [blitcmd copyFromBuffer:texturedata.lockedbuffer
                       sourceOffset:rect.h * pitch
                  sourceBytesPerRow:UVpitch
                sourceBytesPerImage:UVpitch * UVrect.h
                         sourceSize:MTLSizeMake(UVrect.w, UVrect.h, 1)
                          toTexture:texturedata.mtltextureUv
                   destinationSlice:Uslice
                   destinationLevel:0
                  destinationOrigin:MTLOriginMake(UVrect.x, UVrect.y, 0)];

            [blitcmd copyFromBuffer:texturedata.lockedbuffer
                       sourceOffset:(rect.h * pitch) + UVrect.h * UVpitch
                  sourceBytesPerRow:UVpitch
                sourceBytesPerImage:UVpitch * UVrect.h
                         sourceSize:MTLSizeMake(UVrect.w, UVrect.h, 1)
                          toTexture:texturedata.mtltextureUv
                   destinationSlice:Vslice
                   destinationLevel:0
                  destinationOrigin:MTLOriginMake(UVrect.x, UVrect.y, 0)];
        }

        if (texturedata.nv12) {
            int UVpitch = 2 * ((pitch + 1) / 2);

            [blitcmd copyFromBuffer:texturedata.lockedbuffer
                       sourceOffset:rect.h * pitch
                  sourceBytesPerRow:UVpitch
                sourceBytesPerImage:0
                         sourceSize:MTLSizeMake(UVrect.w, UVrect.h, 1)
                          toTexture:texturedata.mtltextureUv
                   destinationSlice:0
                   destinationLevel:0
                  destinationOrigin:MTLOriginMake(UVrect.x, UVrect.y, 0)];
        }
#endif
        [blitcmd endEncoding];

        [data.mtlcmdbuffer commit];
        data.mtlcmdbuffer = nil;

        texturedata.lockedbuffer = nil; // Retained property, so it calls release.
        texturedata.hasdata = YES;
    }
}

static void METAL_SetTextureScaleMode(SDL_Renderer *renderer, SDL_Texture *texture, SDL_ScaleMode scaleMode)
{
}

static bool METAL_SetRenderTarget(SDL_Renderer *renderer, SDL_Texture *texture)
{
    @autoreleasepool {
        SDL3METAL_RenderData *data = (__bridge SDL3METAL_RenderData *)renderer->internal;

        if (data.mtlcmdencoder) {
            /* End encoding for the previous render target so we can set up a new
             * render pass for this one. */
            [data.mtlcmdencoder endEncoding];
            [data.mtlcmdbuffer commit];

            data.mtlcmdencoder = nil;
            data.mtlcmdbuffer = nil;
        }

        /* We don't begin a new render pass right away - we delay it until an actual
         * draw or clear happens. That way we can use hardware clears when possible,
         * which are only available when beginning a new render pass. */
        return true;
    }
}

static bool METAL_QueueSetViewport(SDL_Renderer *renderer, SDL_RenderCommand *cmd)
{
    float projection[4][4]; // Prepare an orthographic projection
    const int w = cmd->data.viewport.rect.w;
    const int h = cmd->data.viewport.rect.h;
    const size_t matrixlen = sizeof(projection);
    float *matrix = (float *)SDL_AllocateRenderVertices(renderer, matrixlen, CONSTANT_ALIGN(16), &cmd->data.viewport.first);
    if (!matrix) {
        return false;
    }

    SDL_memset(projection, '\0', matrixlen);
    if (w && h) {
        projection[0][0] = 2.0f / w;
        projection[1][1] = -2.0f / h;
        projection[3][0] = -1.0f;
        projection[3][1] = 1.0f;
        projection[3][3] = 1.0f;
    }
    SDL_memcpy(matrix, projection, matrixlen);

    return true;
}

static bool METAL_QueueNoOp(SDL_Renderer *renderer, SDL_RenderCommand *cmd)
{
    return true; // nothing to do in this backend.
}

static bool METAL_QueueDrawPoints(SDL_Renderer *renderer, SDL_RenderCommand *cmd, const SDL_FPoint *points, int count)
{
    SDL_FColor color = cmd->data.draw.color;
    bool convert_color = SDL_RenderingLinearSpace(renderer);

    const size_t vertlen = (2 * sizeof(float) + 4 * sizeof(float)) * count;
    float *verts = (float *)SDL_AllocateRenderVertices(renderer, vertlen, DEVICE_ALIGN(8), &cmd->data.draw.first);
    if (!verts) {
        return false;
    }
    cmd->data.draw.count = count;

    if (convert_color) {
        SDL_ConvertToLinear(&color);
    }

    for (int i = 0; i < count; i++, points++) {
        *(verts++) = points->x;
        *(verts++) = points->y;
        *(verts++) = color.r;
        *(verts++) = color.g;
        *(verts++) = color.b;
        *(verts++) = color.a;
    }
    return true;
}

static bool METAL_QueueDrawLines(SDL_Renderer *renderer, SDL_RenderCommand *cmd, const SDL_FPoint *points, int count)
{
    SDL_FColor color = cmd->data.draw.color;
    bool convert_color = SDL_RenderingLinearSpace(renderer);
    size_t vertlen;
    float *verts;

    SDL_assert(count >= 2); // should have been checked at the higher level.

    vertlen = (2 * sizeof(float) + 4 * sizeof(float)) * count;
    verts = (float *)SDL_AllocateRenderVertices(renderer, vertlen, DEVICE_ALIGN(8), &cmd->data.draw.first);
    if (!verts) {
        return false;
    }
    cmd->data.draw.count = count;

    if (convert_color) {
        SDL_ConvertToLinear(&color);
    }

    for (int i = 0; i < count; i++, points++) {
        *(verts++) = points->x;
        *(verts++) = points->y;
        *(verts++) = color.r;
        *(verts++) = color.g;
        *(verts++) = color.b;
        *(verts++) = color.a;
    }

    /* If the line segment is completely horizontal or vertical,
       make it one pixel longer, to satisfy the diamond-exit rule.
       We should probably do this for diagonal lines too, but we'd have to
       do some trigonometry to figure out the correct pixel and generally
       when we have problems with pixel perfection, it's for straight lines
       that are missing a pixel that frames something and not arbitrary
       angles. Maybe !!! FIXME for later, though. */

    points -= 2; // update the last line.
    verts -= 2 + 1;

    {
        const float xstart = points[0].x;
        const float ystart = points[0].y;
        const float xend = points[1].x;
        const float yend = points[1].y;

        if (ystart == yend) { // horizontal line
            verts[0] += (xend > xstart) ? 1.0f : -1.0f;
        } else if (xstart == xend) { // vertical line
            verts[1] += (yend > ystart) ? 1.0f : -1.0f;
        }
    }

    return true;
}

static bool METAL_QueueGeometry(SDL_Renderer *renderer, SDL_RenderCommand *cmd, SDL_Texture *texture,
                               const float *xy, int xy_stride, const SDL_FColor *color, int color_stride, const float *uv, int uv_stride,
                               int num_vertices, const void *indices, int num_indices, int size_indices,
                               float scale_x, float scale_y)
{
    bool convert_color = SDL_RenderingLinearSpace(renderer);
    int count = indices ? num_indices : num_vertices;
    const size_t vertlen = (2 * sizeof(float) + 4 * sizeof(float) + (texture ? 2 : 0) * sizeof(float)) * count;
    float *verts = (float *)SDL_AllocateRenderVertices(renderer, vertlen, DEVICE_ALIGN(8), &cmd->data.draw.first);
    if (!verts) {
        return false;
    }

    cmd->data.draw.count = count;
    size_indices = indices ? size_indices : 0;

    for (int i = 0; i < count; i++) {
        int j;
        float *xy_;
        SDL_FColor col_;
        if (size_indices == 4) {
            j = ((const Uint32 *)indices)[i];
        } else if (size_indices == 2) {
            j = ((const Uint16 *)indices)[i];
        } else if (size_indices == 1) {
            j = ((const Uint8 *)indices)[i];
        } else {
            j = i;
        }

        xy_ = (float *)((char *)xy + j * xy_stride);

        *(verts++) = xy_[0] * scale_x;
        *(verts++) = xy_[1] * scale_y;

        col_ = *(SDL_FColor *)((char *)color + j * color_stride);

        if (convert_color) {
            SDL_ConvertToLinear(&col_);
        }

        *(verts++) = col_.r;
        *(verts++) = col_.g;
        *(verts++) = col_.b;
        *(verts++) = col_.a;

        if (texture) {
            float *uv_ = (float *)((char *)uv + j * uv_stride);
            *(verts++) = uv_[0];
            *(verts++) = uv_[1];
        }
    }

    return true;
}

// These should mirror the definitions in SDL_shaders_metal.metal
//static const float TONEMAP_NONE = 0;
//static const float TONEMAP_LINEAR = 1;
static const float TONEMAP_CHROME = 2;

//static const float TEXTURETYPE_NONE = 0;
static const float TEXTURETYPE_RGB = 1;
static const float TEXTURETYPE_NV12 = 2;
static const float TEXTURETYPE_NV21 = 3;
static const float TEXTURETYPE_YUV = 4;

//static const float INPUTTYPE_UNSPECIFIED = 0;
static const float INPUTTYPE_SRGB = 1;
static const float INPUTTYPE_SCRGB = 2;
static const float INPUTTYPE_HDR10 = 3;

typedef struct
{
    float scRGB_output;
    float texture_type;
    float input_type;
    float color_scale;

    float tonemap_method;
    float tonemap_factor1;
    float tonemap_factor2;
    float sdr_white_point;
} PixelShaderConstants;

typedef struct
{
    __unsafe_unretained id<MTLRenderPipelineState> pipeline;
    __unsafe_unretained id<MTLBuffer> vertex_buffer;
    size_t constants_offset;
    SDL_Texture *texture;
    bool cliprect_dirty;
    bool cliprect_enabled;
    SDL_Rect cliprect;
    bool viewport_dirty;
    SDL_Rect viewport;
    size_t projection_offset;
    bool shader_constants_dirty;
    PixelShaderConstants shader_constants;
} METAL_DrawStateCache;

static void SetupShaderConstants(SDL_Renderer *renderer, const SDL_RenderCommand *cmd, const SDL_Texture *texture, PixelShaderConstants *constants)
{
    float output_headroom;

    SDL_zerop(constants);

    constants->scRGB_output = (float)SDL_RenderingLinearSpace(renderer);
    constants->color_scale = cmd->data.draw.color_scale;

    if (texture) {
        switch (texture->format) {
        case SDL_PIXELFORMAT_YV12:
        case SDL_PIXELFORMAT_IYUV:
            constants->texture_type = TEXTURETYPE_YUV;
            break;
        case SDL_PIXELFORMAT_NV12:
            constants->texture_type = TEXTURETYPE_NV12;
            break;
        case SDL_PIXELFORMAT_NV21:
            constants->texture_type = TEXTURETYPE_NV21;
            break;
        case SDL_PIXELFORMAT_P010:
            constants->texture_type = TEXTURETYPE_NV12;
            break;
        default:
            constants->texture_type = TEXTURETYPE_RGB;
        }

        switch (SDL_COLORSPACETRANSFER(texture->colorspace)) {
        case SDL_TRANSFER_CHARACTERISTICS_LINEAR:
            constants->input_type = INPUTTYPE_SCRGB;
            break;
        case SDL_TRANSFER_CHARACTERISTICS_PQ:
            constants->input_type = INPUTTYPE_HDR10;
            break;
        default:
            constants->input_type = INPUTTYPE_SRGB;
            break;
        }

        constants->sdr_white_point = texture->SDR_white_point;

        if (renderer->target) {
            output_headroom = renderer->target->HDR_headroom;
        } else {
            output_headroom = renderer->HDR_headroom;
        }

        if (texture->HDR_headroom > output_headroom) {
            constants->tonemap_method = TONEMAP_CHROME;
            constants->tonemap_factor1 = (output_headroom / (texture->HDR_headroom * texture->HDR_headroom));
            constants->tonemap_factor2 = (1.0f / output_headroom);
        }
    }
}

static bool SetDrawState(SDL_Renderer *renderer, const SDL_RenderCommand *cmd, const SDL_MetalFragmentFunction shader, PixelShaderConstants *shader_constants, const size_t constants_offset, id<MTLBuffer> mtlbufvertex, METAL_DrawStateCache *statecache)
{
    SDL3METAL_RenderData *data = (__bridge SDL3METAL_RenderData *)renderer->internal;
    const SDL_BlendMode blend = cmd->data.draw.blend;
    size_t first = cmd->data.draw.first;
    id<MTLRenderPipelineState> newpipeline;
    PixelShaderConstants solid_constants;

    if (!METAL_ActivateRenderCommandEncoder(renderer, MTLLoadActionLoad, NULL, statecache->vertex_buffer)) {
        return false;
    }

    if (statecache->viewport_dirty) {
        MTLViewport viewport;
        viewport.originX = statecache->viewport.x;
        viewport.originY = statecache->viewport.y;
        viewport.width = statecache->viewport.w;
        viewport.height = statecache->viewport.h;
        viewport.znear = 0.0;
        viewport.zfar = 1.0;
        [data.mtlcmdencoder setViewport:viewport];
        [data.mtlcmdencoder setVertexBuffer:mtlbufvertex offset:statecache->projection_offset atIndex:2]; // projection
        statecache->viewport_dirty = false;
    }

    if (statecache->cliprect_dirty) {
        SDL_Rect output;
        SDL_Rect clip;
        if (statecache->cliprect_enabled) {
            clip = statecache->cliprect;
            clip.x += statecache->viewport.x;
            clip.y += statecache->viewport.y;
        } else {
            clip = statecache->viewport;
        }

        // Set Scissor Rect Validation: w/h must be <= render pass
        SDL_zero(output);

        if (renderer->target) {
            output.w = renderer->target->w;
            output.h = renderer->target->h;
        } else {
            METAL_GetOutputSize(renderer, &output.w, &output.h);
        }

        if (SDL_GetRectIntersection(&output, &clip, &clip)) {
            MTLScissorRect mtlrect;
            mtlrect.x = clip.x;
            mtlrect.y = clip.y;
            mtlrect.width = clip.w;
            mtlrect.height = clip.h;
            [data.mtlcmdencoder setScissorRect:mtlrect];
        }

        statecache->cliprect_dirty = false;
    }

    newpipeline = ChoosePipelineState(data, data.activepipelines, shader, blend);
    if (newpipeline != statecache->pipeline) {
        [data.mtlcmdencoder setRenderPipelineState:newpipeline];
        statecache->pipeline = newpipeline;
    }

    if (!shader_constants) {
        SetupShaderConstants(renderer, cmd, NULL, &solid_constants);
        shader_constants = &solid_constants;
    }

    if (statecache->shader_constants_dirty ||
        SDL_memcmp(shader_constants, &statecache->shader_constants, sizeof(*shader_constants)) != 0) {
        id<MTLBuffer> mtlbufconstants = [data.mtldevice newBufferWithLength:sizeof(*shader_constants) options:MTLResourceStorageModeShared];
        mtlbufconstants.label = @"SDL shader constants data";
        SDL_memcpy([mtlbufconstants contents], shader_constants, sizeof(*shader_constants));
        [data.mtlcmdencoder setFragmentBuffer:mtlbufconstants offset:0 atIndex:0];

        SDL_memcpy(&statecache->shader_constants, shader_constants, sizeof(*shader_constants));
        statecache->shader_constants_dirty = false;
    }

    if (constants_offset != statecache->constants_offset) {
        if (constants_offset != CONSTANTS_OFFSET_INVALID) {
            [data.mtlcmdencoder setVertexBuffer:data.mtlbufconstants offset:constants_offset atIndex:3];
        }
        statecache->constants_offset = constants_offset;
    }

    [data.mtlcmdencoder setVertexBufferOffset:first atIndex:0]; // position/texcoords
    return true;
}

static bool SetCopyState(SDL_Renderer *renderer, const SDL_RenderCommand *cmd, const size_t constants_offset,
                             id<MTLBuffer> mtlbufvertex, METAL_DrawStateCache *statecache)
{
    SDL3METAL_RenderData *data = (__bridge SDL3METAL_RenderData *)renderer->internal;
    SDL_Texture *texture = cmd->data.draw.texture;
    SDL3METAL_TextureData *texturedata = (__bridge SDL3METAL_TextureData *)texture->internal;
    PixelShaderConstants constants;

    SetupShaderConstants(renderer, cmd, texture, &constants);

    if (!SetDrawState(renderer, cmd, texturedata.fragmentFunction, &constants, constants_offset, mtlbufvertex, statecache)) {
        return false;
    }

    if (texture != statecache->texture) {
        id<MTLSamplerState> mtlsampler;

        if (texture->scaleMode == SDL_SCALEMODE_NEAREST) {
            switch (cmd->data.draw.texture_address_mode) {
            case SDL_TEXTURE_ADDRESS_CLAMP:
                mtlsampler = data.mtlsamplers[SDL_METAL_SAMPLER_NEAREST_CLAMP];
                break;
            case SDL_TEXTURE_ADDRESS_WRAP:
                mtlsampler = data.mtlsamplers[SDL_METAL_SAMPLER_NEAREST_WRAP];
                break;
            default:
                return SDL_SetError("Unknown texture address mode: %d", cmd->data.draw.texture_address_mode);
            }
        } else {
            switch (cmd->data.draw.texture_address_mode) {
            case SDL_TEXTURE_ADDRESS_CLAMP:
                mtlsampler = data.mtlsamplers[SDL_METAL_SAMPLER_LINEAR_CLAMP];
                break;
            case SDL_TEXTURE_ADDRESS_WRAP:
                mtlsampler = data.mtlsamplers[SDL_METAL_SAMPLER_LINEAR_WRAP];
                break;
            default:
                return SDL_SetError("Unknown texture address mode: %d", cmd->data.draw.texture_address_mode);
            }
        }
        [data.mtlcmdencoder setFragmentSamplerState:mtlsampler atIndex:0];

        [data.mtlcmdencoder setFragmentTexture:texturedata.mtltexture atIndex:0];
#ifdef SDL_HAVE_YUV
        if (texturedata.yuv || texturedata.nv12) {
            [data.mtlcmdencoder setFragmentTexture:texturedata.mtltextureUv atIndex:1];
            [data.mtlcmdencoder setFragmentBuffer:data.mtlbufconstants offset:texturedata.conversionBufferOffset atIndex:1];
        }
#endif
        statecache->texture = texture;
    }
    return true;
}

static void METAL_InvalidateCachedState(SDL_Renderer *renderer)
{
    // METAL_DrawStateCache only exists during a run of METAL_RunCommandQueue, so there's nothing to invalidate!
}

static bool METAL_RunCommandQueue(SDL_Renderer *renderer, SDL_RenderCommand *cmd, void *vertices, size_t vertsize)
{
    @autoreleasepool {
        SDL3METAL_RenderData *data = (__bridge SDL3METAL_RenderData *)renderer->internal;
        id<MTLBuffer> mtlbufvertex = nil;
        METAL_DrawStateCache statecache;
        SDL_zero(statecache);

        statecache.pipeline = nil;
        statecache.vertex_buffer = nil;
        statecache.constants_offset = CONSTANTS_OFFSET_INVALID;
        statecache.texture = NULL;
        statecache.shader_constants_dirty = true;
        statecache.cliprect_dirty = true;
        statecache.viewport_dirty = true;
        statecache.projection_offset = 0;

        // !!! FIXME: have a ring of pre-made MTLBuffers we cycle through? How expensive is creation?
        if (vertsize > 0) {
            /* We can memcpy to a shared buffer from the CPU and read it from the GPU
             * without any extra copying. It's a bit slower on macOS to read shared
             * data from the GPU than to read managed/private data, but we avoid the
             * cost of copying the data and the code's simpler. Apple's best
             * practices guide recommends this approach for streamed vertex data.
             */
            mtlbufvertex = [data.mtldevice newBufferWithLength:vertsize options:MTLResourceStorageModeShared];
            mtlbufvertex.label = @"SDL vertex data";
            SDL_memcpy([mtlbufvertex contents], vertices, vertsize);

            statecache.vertex_buffer = mtlbufvertex;
        }

        // If there's a command buffer here unexpectedly (app requested one?). Commit it so we can start fresh.
        [data.mtlcmdencoder endEncoding];
        [data.mtlcmdbuffer commit];
        data.mtlcmdencoder = nil;
        data.mtlcmdbuffer = nil;

        while (cmd) {
            switch (cmd->command) {
            case SDL_RENDERCMD_SETVIEWPORT:
            {
                SDL_memcpy(&statecache.viewport, &cmd->data.viewport.rect, sizeof(statecache.viewport));
                statecache.projection_offset = cmd->data.viewport.first;
                statecache.viewport_dirty = true;
                statecache.cliprect_dirty = true;
                break;
            }

            case SDL_RENDERCMD_SETCLIPRECT:
            {
                SDL_memcpy(&statecache.cliprect, &cmd->data.cliprect.rect, sizeof(statecache.cliprect));
                statecache.cliprect_enabled = cmd->data.cliprect.enabled;
                statecache.cliprect_dirty = true;
                break;
            }

            case SDL_RENDERCMD_SETDRAWCOLOR:
            {
                break;
            }

            case SDL_RENDERCMD_CLEAR:
            {
                /* If we're already encoding a command buffer, dump it without committing it. We'd just
                    clear all its work anyhow, and starting a new encoder will let us use a hardware clear
                    operation via MTLLoadActionClear. */
                if (data.mtlcmdencoder != nil) {
                    [data.mtlcmdencoder endEncoding];

                    // !!! FIXME: have to commit, or an uncommitted but enqueued buffer will prevent the frame from finishing.
                    [data.mtlcmdbuffer commit];
                    data.mtlcmdencoder = nil;
                    data.mtlcmdbuffer = nil;
                }

                // force all this state to be reconfigured on next command buffer.
                statecache.pipeline = nil;
                statecache.constants_offset = CONSTANTS_OFFSET_INVALID;
                statecache.texture = NULL;
                statecache.shader_constants_dirty = true;
                statecache.cliprect_dirty = true;
                statecache.viewport_dirty = true;

                {
                    bool convert_color = SDL_RenderingLinearSpace(renderer);
                    SDL_FColor color = cmd->data.color.color;
                    if (convert_color) {
                        SDL_ConvertToLinear(&color);
                    }
                    color.r *= cmd->data.color.color_scale;
                    color.g *= cmd->data.color.color_scale;
                    color.b *= cmd->data.color.color_scale;
                    MTLClearColor mtlcolor = MTLClearColorMake(color.r, color.g, color.b, color.a);

                    // get new command encoder, set up with an initial clear operation.
                    // (this might fail, and future draw operations will notice.)
                    METAL_ActivateRenderCommandEncoder(renderer, MTLLoadActionClear, &mtlcolor, mtlbufvertex);
                }
                break;
            }

            case SDL_RENDERCMD_DRAW_POINTS:
            case SDL_RENDERCMD_DRAW_LINES:
            {
                const size_t count = cmd->data.draw.count;
                const MTLPrimitiveType primtype = (cmd->command == SDL_RENDERCMD_DRAW_POINTS) ? MTLPrimitiveTypePoint : MTLPrimitiveTypeLineStrip;
                if (SetDrawState(renderer, cmd, SDL_METAL_FRAGMENT_SOLID, NULL, CONSTANTS_OFFSET_HALF_PIXEL_TRANSFORM, mtlbufvertex, &statecache)) {
                    [data.mtlcmdencoder drawPrimitives:primtype vertexStart:0 vertexCount:count];
                }
                break;
            }

            case SDL_RENDERCMD_FILL_RECTS: // unused
                break;

            case SDL_RENDERCMD_COPY: // unused
                break;

            case SDL_RENDERCMD_COPY_EX: // unused
                break;

            case SDL_RENDERCMD_GEOMETRY:
            {
                const size_t count = cmd->data.draw.count;
                SDL_Texture *texture = cmd->data.draw.texture;

                if (texture) {
                    if (SetCopyState(renderer, cmd, CONSTANTS_OFFSET_IDENTITY, mtlbufvertex, &statecache)) {
                        [data.mtlcmdencoder drawPrimitives:MTLPrimitiveTypeTriangle vertexStart:0 vertexCount:count];
                    }
                } else {
                    if (SetDrawState(renderer, cmd, SDL_METAL_FRAGMENT_SOLID, NULL, CONSTANTS_OFFSET_IDENTITY, mtlbufvertex, &statecache)) {
                        [data.mtlcmdencoder drawPrimitives:MTLPrimitiveTypeTriangle vertexStart:0 vertexCount:count];
                    }
                }
                break;
            }

            case SDL_RENDERCMD_NO_OP:
                break;
            }
            cmd = cmd->next;
        }

        return true;
    }
}

static SDL_Surface *METAL_RenderReadPixels(SDL_Renderer *renderer, const SDL_Rect *rect)
{
    @autoreleasepool {
        SDL3METAL_RenderData *data = (__bridge SDL3METAL_RenderData *)renderer->internal;
        id<MTLTexture> mtltexture;
        MTLRegion mtlregion;
        Uint32 format;
        SDL_Surface *surface;

        if (!METAL_ActivateRenderCommandEncoder(renderer, MTLLoadActionLoad, NULL, nil)) {
            SDL_SetError("Failed to activate render command encoder (is your window in the background?");
            return NULL;
        }

        [data.mtlcmdencoder endEncoding];
        mtltexture = data.mtlpassdesc.colorAttachments[0].texture;

#ifdef SDL_PLATFORM_MACOS
        /* on macOS with managed-storage textures, we need to tell the driver to
         * update the CPU-side copy of the texture data.
         * NOTE: Currently all of our textures are managed on macOS. We'll need some
         * extra copying for any private textures. */
        if (METAL_GetStorageMode(mtltexture) == MTLStorageModeManaged) {
            id<MTLBlitCommandEncoder> blit = [data.mtlcmdbuffer blitCommandEncoder];
            [blit synchronizeResource:mtltexture];
            [blit endEncoding];
        }
#endif

        /* Commit the current command buffer and wait until it's completed, to make
         * sure the GPU has finished rendering to it by the time we read it. */
        [data.mtlcmdbuffer commit];
        [data.mtlcmdbuffer waitUntilCompleted];
        data.mtlcmdencoder = nil;
        data.mtlcmdbuffer = nil;

        mtlregion = MTLRegionMake2D(rect->x, rect->y, rect->w, rect->h);

        switch (mtltexture.pixelFormat) {
        case MTLPixelFormatBGRA8Unorm:
        case MTLPixelFormatBGRA8Unorm_sRGB:
            format = SDL_PIXELFORMAT_ARGB8888;
            break;
        case MTLPixelFormatRGBA8Unorm:
        case MTLPixelFormatRGBA8Unorm_sRGB:
            format = SDL_PIXELFORMAT_ABGR8888;
            break;
        case MTLPixelFormatRGB10A2Unorm:
            format = SDL_PIXELFORMAT_ABGR2101010;
            break;
        case MTLPixelFormatRGBA16Float:
            format = SDL_PIXELFORMAT_RGBA64_FLOAT;
            break;
        default:
            SDL_SetError("Unknown framebuffer pixel format");
            return NULL;
        }
        surface = SDL_CreateSurface(rect->w, rect->h, format);
        if (surface) {
            [mtltexture getBytes:surface->pixels bytesPerRow:surface->pitch fromRegion:mtlregion mipmapLevel:0];
        }
        return surface;
    }
}

static bool METAL_RenderPresent(SDL_Renderer *renderer)
{
    @autoreleasepool {
        SDL3METAL_RenderData *data = (__bridge SDL3METAL_RenderData *)renderer->internal;
        bool ready = true;

        // If we don't have a command buffer, we can't present, so activate to get one.
        if (data.mtlcmdencoder == nil) {
            // We haven't even gotten a backbuffer yet? Load and clear it. Otherwise, load the existing data.
            if (data.mtlbackbuffer == nil) {
                float alpha = (SDL_GetWindowFlags(renderer->window) & SDL_WINDOW_TRANSPARENT) ? 0.0f : 1.0f;
                MTLClearColor color = MTLClearColorMake(0.0f, 0.0f, 0.0f, alpha);
                ready = METAL_ActivateRenderCommandEncoder(renderer, MTLLoadActionClear, &color, nil);
            } else {
                ready = METAL_ActivateRenderCommandEncoder(renderer, MTLLoadActionLoad, NULL, nil);
            }
        }

        [data.mtlcmdencoder endEncoding];

        // If we don't have a drawable to present, don't try to present it.
        //  But we'll still try to commit the command buffer in case it was already enqueued.
        if (ready) {
            SDL_assert(data.mtlbackbuffer != nil);
            [data.mtlcmdbuffer presentDrawable:data.mtlbackbuffer];
        }

        [data.mtlcmdbuffer commit];

        data.mtlcmdencoder = nil;
        data.mtlcmdbuffer = nil;
        data.mtlbackbuffer = nil;

        if (renderer->hidden || !ready) {
            return false;
        }
        return true;
    }
}

static void METAL_DestroyTexture(SDL_Renderer *renderer, SDL_Texture *texture)
{
    @autoreleasepool {
        CFBridgingRelease(texture->internal);
        texture->internal = NULL;
    }
}

static void METAL_DestroyRenderer(SDL_Renderer *renderer)
{
    @autoreleasepool {
        if (renderer->internal) {
            SDL3METAL_RenderData *data = CFBridgingRelease(renderer->internal);

            if (data.mtlcmdencoder != nil) {
                [data.mtlcmdencoder endEncoding];
            }

            DestroyAllPipelines(data.allpipelines, data.pipelinescount);

            /* Release the metal view instead of destroying it,
               in case we want to use it later (recreating the renderer)
             */
            // SDL_Metal_DestroyView(data.mtlview);
            CFBridgingRelease(data.mtlview);
        }
    }
}

static void *METAL_GetMetalLayer(SDL_Renderer *renderer)
{
    @autoreleasepool {
        SDL3METAL_RenderData *data = (__bridge SDL3METAL_RenderData *)renderer->internal;
        return (__bridge void *)data.mtllayer;
    }
}

static void *METAL_GetMetalCommandEncoder(SDL_Renderer *renderer)
{
    @autoreleasepool {
        // note that data.mtlcmdencoder can be nil if METAL_ActivateRenderCommandEncoder fails.
        //  Before SDL 2.0.18, it might have returned a non-nil encoding that might not have been
        //  usable for presentation. Check your return values!
        SDL3METAL_RenderData *data;
        METAL_ActivateRenderCommandEncoder(renderer, MTLLoadActionLoad, NULL, nil);
        data = (__bridge SDL3METAL_RenderData *)renderer->internal;
        return (__bridge void *)data.mtlcmdencoder;
    }
}

static bool METAL_SetVSync(SDL_Renderer *renderer, const int vsync)
{
#if defined(SDL_PLATFORM_MACOS) || TARGET_OS_MACCATALYST
    SDL3METAL_RenderData *data = (__bridge SDL3METAL_RenderData *)renderer->internal;
    switch (vsync) {
    case 0:
        data.mtllayer.displaySyncEnabled = NO;
        break;
    case 1:
        data.mtllayer.displaySyncEnabled = YES;
        break;
    default:
        return SDL_Unsupported();
    }
    return true;
#else
    switch (vsync) {
    case 1:
        return true;
    default:
        return SDL_Unsupported();
    }
#endif
}

static SDL_MetalView GetWindowView(SDL_Window *window)
{
#ifdef SDL_VIDEO_DRIVER_COCOA
    NSWindow *nswindow = (__bridge NSWindow *)SDL_GetPointerProperty(SDL_GetWindowProperties(window), SDL_PROP_WINDOW_COCOA_WINDOW_POINTER, NULL);
    NSInteger tag = (NSInteger)SDL_GetNumberProperty(SDL_GetWindowProperties(window), SDL_PROP_WINDOW_COCOA_METAL_VIEW_TAG_NUMBER, 0);
    if (nswindow && tag) {
        NSView *view = nswindow.contentView;
        if (view.subviews.count > 0) {
            view = view.subviews[0];
            if (view.tag == tag) {
                return (SDL_MetalView)CFBridgingRetain(view);
            }
        }
    }
#endif

#ifdef SDL_VIDEO_DRIVER_UIKIT
    UIWindow *uiwindow = (__bridge UIWindow *)SDL_GetPointerProperty(SDL_GetWindowProperties(window), SDL_PROP_WINDOW_UIKIT_WINDOW_POINTER, NULL);
    NSInteger tag = (NSInteger)SDL_GetNumberProperty(SDL_GetWindowProperties(window), SDL_PROP_WINDOW_UIKIT_METAL_VIEW_TAG_NUMBER, 0);
    if (uiwindow && tag) {
        UIView *view = uiwindow.rootViewController.view;
        if (view.tag == tag) {
            return (SDL_MetalView)CFBridgingRetain(view);
        }
    }
#endif

    return nil;
}

static bool METAL_CreateRenderer(SDL_Renderer *renderer, SDL_Window *window, SDL_PropertiesID create_props)
{
    @autoreleasepool {
        SDL3METAL_RenderData *data = NULL;
        id<MTLDevice> mtldevice = nil;
        SDL_MetalView view = NULL;
        CAMetalLayer *layer = nil;
        NSError *err = nil;
        dispatch_data_t mtllibdata;
        char *constantdata;
        int maxtexsize, quadcount = UINT16_MAX / 4;
        UInt16 *indexdata;
        size_t indicessize = sizeof(UInt16) * quadcount * 6;
        MTLSamplerDescriptor *samplerdesc;
        id<MTLCommandQueue> mtlcmdqueue;
        id<MTLLibrary> mtllibrary;
        id<MTLBuffer> mtlbufconstantstaging, mtlbufquadindicesstaging, mtlbufconstants, mtlbufquadindices;
        id<MTLCommandBuffer> cmdbuffer;
        id<MTLBlitCommandEncoder> blitcmd;
        bool scRGB_supported = false;

        // Note: matrices are column major.
        float identitytransform[16] = {
            1.0f,
            0.0f,
            0.0f,
            0.0f,
            0.0f,
            1.0f,
            0.0f,
            0.0f,
            0.0f,
            0.0f,
            1.0f,
            0.0f,
            0.0f,
            0.0f,
            0.0f,
            1.0f,
        };

        float halfpixeltransform[16] = {
            1.0f,
            0.0f,
            0.0f,
            0.0f,
            0.0f,
            1.0f,
            0.0f,
            0.0f,
            0.0f,
            0.0f,
            1.0f,
            0.0f,
            0.5f,
            0.5f,
            0.0f,
            1.0f,
        };

        const size_t YCbCr_shader_matrix_size = 4 * 4 * sizeof(float);

        SDL_SetupRendererColorspace(renderer, create_props);

#ifndef SDL_PLATFORM_TVOS
        if (@available(macos 10.11, iOS 16.0, *)) {
            scRGB_supported = true;
        }
#endif
        if (renderer->output_colorspace != SDL_COLORSPACE_SRGB) {
            if (renderer->output_colorspace == SDL_COLORSPACE_SRGB_LINEAR && scRGB_supported) {
                // This colorspace is supported
            } else {
                return SDL_SetError("Unsupported output colorspace");
            }
        }

#ifdef SDL_PLATFORM_MACOS
        if (SDL_GetHintBoolean(SDL_HINT_RENDER_METAL_PREFER_LOW_POWER_DEVICE, true)) {
            NSArray<id<MTLDevice>> *devices = MTLCopyAllDevices();

            for (id<MTLDevice> device in devices) {
                if (device.isLowPower) {
                    mtldevice = device;
                    break;
                }
            }
        }
#endif

        if (mtldevice == nil) {
            mtldevice = MTLCreateSystemDefaultDevice();
        }

        if (mtldevice == nil) {
            return SDL_SetError("Failed to obtain Metal device");
        }

        view = GetWindowView(window);
        if (view == nil) {
            view = SDL_Metal_CreateView(window);
        }

        if (view == NULL) {
            return false;
        }

        // !!! FIXME: error checking on all of this.
        data = [[SDL3METAL_RenderData alloc] init];

        if (data == nil) {
            /* Release the metal view instead of destroying it,
               in case we want to use it later (recreating the renderer)
             */
            // SDL_Metal_DestroyView(view);
            CFBridgingRelease(view);
            return SDL_SetError("SDL3METAL_RenderData alloc/init failed");
        }

        renderer->internal = (void *)CFBridgingRetain(data);
        METAL_InvalidateCachedState(renderer);
        renderer->window = window;

        data.mtlview = view;

#ifdef SDL_PLATFORM_MACOS
        layer = (CAMetalLayer *)[(__bridge NSView *)view layer];
#else
        layer = (CAMetalLayer *)[(__bridge UIView *)view layer];
#endif

#ifndef SDL_PLATFORM_TVOS
        if (renderer->output_colorspace == SDL_COLORSPACE_SRGB_LINEAR) {
            if (@available(macos 10.11, iOS 16.0, *)) {
                layer.wantsExtendedDynamicRangeContent = YES;
            } else {
                SDL_assert(!"Logic error, scRGB is not actually supported");
            }
            layer.pixelFormat = MTLPixelFormatRGBA16Float;

            const CFStringRef name = kCGColorSpaceExtendedLinearSRGB;
            CGColorSpaceRef colorspace = CGColorSpaceCreateWithName(name);
            layer.colorspace = colorspace;
            CGColorSpaceRelease(colorspace);
        }
#endif // !SDL_PLATFORM_TVOS

        layer.device = mtldevice;

        // Necessary for RenderReadPixels.
        layer.framebufferOnly = NO;

        data.mtldevice = layer.device;
        data.mtllayer = layer;
        mtlcmdqueue = [data.mtldevice newCommandQueue];
        data.mtlcmdqueue = mtlcmdqueue;
        data.mtlcmdqueue.label = @"SDL Metal Renderer";
        data.mtlpassdesc = [MTLRenderPassDescriptor renderPassDescriptor];

        // The compiled .metallib is embedded in a static array in a header file
        // but the original shader source code is in SDL_shaders_metal.metal.
        mtllibdata = dispatch_data_create(sdl_metallib, sdl_metallib_len, dispatch_get_global_queue(0, 0), ^{
                                                                          });
        mtllibrary = [data.mtldevice newLibraryWithData:mtllibdata error:&err];
        data.mtllibrary = mtllibrary;
        SDL_assert(err == nil);
        data.mtllibrary.label = @"SDL Metal renderer shader library";

        // Do some shader pipeline state loading up-front rather than on demand.
        data.pipelinescount = 0;
        data.allpipelines = NULL;
        ChooseShaderPipelines(data, MTLPixelFormatBGRA8Unorm);

        static struct
        {
            MTLSamplerMinMagFilter filter;
            MTLSamplerAddressMode address;
        } samplerParams[] = {
            { MTLSamplerMinMagFilterNearest, MTLSamplerAddressModeClampToEdge },
            { MTLSamplerMinMagFilterNearest, MTLSamplerAddressModeRepeat },
            { MTLSamplerMinMagFilterLinear, MTLSamplerAddressModeClampToEdge },
            { MTLSamplerMinMagFilterLinear, MTLSamplerAddressModeRepeat },
        };
        SDL_COMPILE_TIME_ASSERT(samplerParams_SIZE, SDL_arraysize(samplerParams) == SDL_NUM_METAL_SAMPLERS);

        data.mtlsamplers = [[NSMutableArray<id<MTLSamplerState>> alloc] init];
        samplerdesc = [[MTLSamplerDescriptor alloc] init];
        for (int i = 0; i < SDL_arraysize(samplerParams); ++i) {
            samplerdesc.minFilter = samplerParams[i].filter;
            samplerdesc.magFilter = samplerParams[i].filter;
            samplerdesc.sAddressMode = samplerParams[i].address;
            samplerdesc.tAddressMode = samplerParams[i].address;
            [data.mtlsamplers addObject:[data.mtldevice newSamplerStateWithDescriptor:samplerdesc]];
        }

        mtlbufconstantstaging = [data.mtldevice newBufferWithLength:CONSTANTS_LENGTH options:MTLResourceStorageModeShared];

        constantdata = [mtlbufconstantstaging contents];
        SDL_memcpy(constantdata + CONSTANTS_OFFSET_IDENTITY, identitytransform, sizeof(identitytransform));
        SDL_memcpy(constantdata + CONSTANTS_OFFSET_HALF_PIXEL_TRANSFORM, halfpixeltransform, sizeof(halfpixeltransform));
        SDL_memcpy(constantdata + CONSTANTS_OFFSET_DECODE_BT601_LIMITED, SDL_GetYCbCRtoRGBConversionMatrix(SDL_COLORSPACE_BT601_LIMITED, 0, 0, 8), YCbCr_shader_matrix_size);
        SDL_memcpy(constantdata + CONSTANTS_OFFSET_DECODE_BT601_FULL, SDL_GetYCbCRtoRGBConversionMatrix(SDL_COLORSPACE_BT601_FULL, 0, 0, 8), YCbCr_shader_matrix_size);
        SDL_memcpy(constantdata + CONSTANTS_OFFSET_DECODE_BT709_LIMITED, SDL_GetYCbCRtoRGBConversionMatrix(SDL_COLORSPACE_BT709_LIMITED, 0, 0, 8), YCbCr_shader_matrix_size);
        SDL_memcpy(constantdata + CONSTANTS_OFFSET_DECODE_BT709_FULL, SDL_GetYCbCRtoRGBConversionMatrix(SDL_COLORSPACE_BT709_FULL, 0, 0, 8), YCbCr_shader_matrix_size);
        SDL_memcpy(constantdata + CONSTANTS_OFFSET_DECODE_BT2020_LIMITED, SDL_GetYCbCRtoRGBConversionMatrix(SDL_COLORSPACE_BT2020_LIMITED, 0, 0, 10), YCbCr_shader_matrix_size);
        SDL_memcpy(constantdata + CONSTANTS_OFFSET_DECODE_BT2020_FULL, SDL_GetYCbCRtoRGBConversionMatrix(SDL_COLORSPACE_BT2020_FULL, 0, 0, 10), YCbCr_shader_matrix_size);

        mtlbufquadindicesstaging = [data.mtldevice newBufferWithLength:indicessize options:MTLResourceStorageModeShared];

        /* Quads in the following vertex order (matches the FillRects vertices):
         * 1---3
         * | \ |
         * 0---2
         */
        indexdata = [mtlbufquadindicesstaging contents];
        for (int i = 0; i < quadcount; i++) {
            indexdata[i * 6 + 0] = i * 4 + 0;
            indexdata[i * 6 + 1] = i * 4 + 1;
            indexdata[i * 6 + 2] = i * 4 + 2;

            indexdata[i * 6 + 3] = i * 4 + 2;
            indexdata[i * 6 + 4] = i * 4 + 1;
            indexdata[i * 6 + 5] = i * 4 + 3;
        }

        mtlbufconstants = [data.mtldevice newBufferWithLength:CONSTANTS_LENGTH options:MTLResourceStorageModePrivate];
        data.mtlbufconstants = mtlbufconstants;
        data.mtlbufconstants.label = @"SDL constant data";

        mtlbufquadindices = [data.mtldevice newBufferWithLength:indicessize options:MTLResourceStorageModePrivate];
        data.mtlbufquadindices = mtlbufquadindices;
        data.mtlbufquadindices.label = @"SDL quad index buffer";

        cmdbuffer = [data.mtlcmdqueue commandBuffer];
        blitcmd = [cmdbuffer blitCommandEncoder];

        [blitcmd copyFromBuffer:mtlbufconstantstaging sourceOffset:0 toBuffer:mtlbufconstants destinationOffset:0 size:CONSTANTS_LENGTH];
        [blitcmd copyFromBuffer:mtlbufquadindicesstaging sourceOffset:0 toBuffer:mtlbufquadindices destinationOffset:0 size:indicessize];

        [blitcmd endEncoding];
        [cmdbuffer commit];

        // !!! FIXME: force more clears here so all the drawables are sane to start, and our static buffers are definitely flushed.

        renderer->WindowEvent = METAL_WindowEvent;
        renderer->GetOutputSize = METAL_GetOutputSize;
        renderer->SupportsBlendMode = METAL_SupportsBlendMode;
        renderer->CreateTexture = METAL_CreateTexture;
        renderer->UpdateTexture = METAL_UpdateTexture;
#ifdef SDL_HAVE_YUV
        renderer->UpdateTextureYUV = METAL_UpdateTextureYUV;
        renderer->UpdateTextureNV = METAL_UpdateTextureNV;
#endif
        renderer->LockTexture = METAL_LockTexture;
        renderer->UnlockTexture = METAL_UnlockTexture;
        renderer->SetTextureScaleMode = METAL_SetTextureScaleMode;
        renderer->SetRenderTarget = METAL_SetRenderTarget;
        renderer->QueueSetViewport = METAL_QueueSetViewport;
        renderer->QueueSetDrawColor = METAL_QueueNoOp;
        renderer->QueueDrawPoints = METAL_QueueDrawPoints;
        renderer->QueueDrawLines = METAL_QueueDrawLines;
        renderer->QueueGeometry = METAL_QueueGeometry;
        renderer->InvalidateCachedState = METAL_InvalidateCachedState;
        renderer->RunCommandQueue = METAL_RunCommandQueue;
        renderer->RenderReadPixels = METAL_RenderReadPixels;
        renderer->RenderPresent = METAL_RenderPresent;
        renderer->DestroyTexture = METAL_DestroyTexture;
        renderer->DestroyRenderer = METAL_DestroyRenderer;
        renderer->SetVSync = METAL_SetVSync;
        renderer->GetMetalLayer = METAL_GetMetalLayer;
        renderer->GetMetalCommandEncoder = METAL_GetMetalCommandEncoder;

        renderer->name = METAL_RenderDriver.name;
        SDL_AddSupportedTextureFormat(renderer, SDL_PIXELFORMAT_ARGB8888);
        SDL_AddSupportedTextureFormat(renderer, SDL_PIXELFORMAT_ABGR8888);
        SDL_AddSupportedTextureFormat(renderer, SDL_PIXELFORMAT_ABGR2101010);
        SDL_AddSupportedTextureFormat(renderer, SDL_PIXELFORMAT_RGBA64_FLOAT);
        SDL_AddSupportedTextureFormat(renderer, SDL_PIXELFORMAT_RGBA128_FLOAT);
        SDL_AddSupportedTextureFormat(renderer, SDL_PIXELFORMAT_YV12);
        SDL_AddSupportedTextureFormat(renderer, SDL_PIXELFORMAT_IYUV);
        SDL_AddSupportedTextureFormat(renderer, SDL_PIXELFORMAT_NV12);
        SDL_AddSupportedTextureFormat(renderer, SDL_PIXELFORMAT_NV21);
        SDL_AddSupportedTextureFormat(renderer, SDL_PIXELFORMAT_P010);

#if defined(SDL_PLATFORM_MACOS) || TARGET_OS_MACCATALYST
        data.mtllayer.displaySyncEnabled = NO;
#endif

        // https://developer.apple.com/metal/Metal-Feature-Set-Tables.pdf
        maxtexsize = 4096;
#if defined(SDL_PLATFORM_MACOS) || TARGET_OS_MACCATALYST
        maxtexsize = 16384;
#elif defined(SDL_PLATFORM_TVOS)
        maxtexsize = 8192;
        if ([mtldevice supportsFeatureSet:MTLFeatureSet_tvOS_GPUFamily2_v1]) {
            maxtexsize = 16384;
        }
#else
        if ([mtldevice supportsFeatureSet:MTLFeatureSet_iOS_GPUFamily4_v1]) {
            maxtexsize = 16384;
        } else if ([mtldevice supportsFeatureSet:MTLFeatureSet_iOS_GPUFamily3_v1]) {
            maxtexsize = 16384;
        } else if ([mtldevice supportsFeatureSet:MTLFeatureSet_iOS_GPUFamily2_v2] || [mtldevice supportsFeatureSet:MTLFeatureSet_iOS_GPUFamily1_v2]) {
            maxtexsize = 8192;
        } else {
            maxtexsize = 4096;
        }
#endif

        SDL_SetNumberProperty(SDL_GetRendererProperties(renderer), SDL_PROP_RENDERER_MAX_TEXTURE_SIZE_NUMBER, maxtexsize);

        return true;
    }
}

SDL_RenderDriver METAL_RenderDriver = {
    METAL_CreateRenderer, "metal"
};

#endif // SDL_VIDEO_RENDER_METAL