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
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
|
public module core;
// Slang `core` library
// Aliases for base types
/// @category scalar_types Scalar types
typedef half float16_t;
/// @category scalar_types
typedef float float32_t;
/// @category scalar_types
typedef double float64_t;
/// @category scalar_types
typedef int int32_t;
/// @category scalar_types
typedef uint uint32_t;
/// @category scalar_types
typedef uintptr_t size_t;
/// @category scalar_types
typedef uintptr_t usize_t;
/// @category scalar_types
typedef intptr_t ssize_t;
// Modifier for variables that must resolve to compile-time constants
// as part of translation.
syntax constexpr : ConstExprModifier;
// Modifier for variables that should have writes be made
// visible at the global-memory scope
syntax globallycoherent : GloballyCoherentModifier;
/// Modifier to disable inteprolation and force per-vertex passing of a varying attribute.
///
/// When a varying attribute passed to the fragment shader is marked `pervertex`, it will
/// not be interpolated during rasterization (similar to `nointerpolate` attributes).
/// Unlike a plain `nointerpolate` attribute, this modifier indicates that the attribute
/// should *only* be acccessed through the `GetAttributeAtVertex()` operation, so access its
/// distinct per-vertex values.
///
syntax pervertex : PerVertexModifier;
/// Modifier to indicate a buffer or texture element type is
/// backed by data in an unsigned normalized format.
///
/// The `unorm` modifier is only valid on `float` and `vector`s
/// with `float` elements.
///
/// This modifier does not affect the semantics of any variable,
/// parameter, or field that uses it. The semantics of a `float`
/// or vector are the same with or without `unorm`.
///
/// The `unorm` modifier can be used for the element type of a
/// buffer or texture, to indicate that the data that is bound
/// to that buffer or texture is in a matching normalized format.
/// Some platforms may require a `unorm` qualifier for such buffers
/// and textures, and others may operate correctly without it.
///
syntax unorm : UNormModifier;
/// Modifier to indicate a buffer or texture element type is
/// backed by data in an signed normalized format.
///
/// The `snorm` modifier is only valid on `float` and `vector`s
/// with `float` elements.
///
/// This modifier does not affect the semantics of any variable,
/// parameter, or field that uses it. The semantics of a `float`
/// or vector are the same with or without `snorm`.
///
/// The `snorm` modifier can be used for the element type of a
/// buffer or texture, to indicate that the data that is bound
/// to that buffer or texture is in a matching normalized format.
/// Some platforms may require a `unorm` qualifier for such buffers
/// and textures, and others may operate correctly without it.
///
syntax snorm : SNormModifier;
/// Modifier to indicate that a function name should not be mangled
/// by the Slang compiler.
///
/// The `__extern_cpp` modifier makes a symbol to have unmangled
/// name in source/output C++ code.
///
syntax __extern_cpp : ExternCppModifier;
/// Represents types that can be initialized with a default constructor.
__magic_type(DefaultInitializableType)
interface IDefaultInitializable
{
__builtin_requirement($((int)BuiltinRequirementKind::DefaultInitializableConstructor))
__init();
}
/// Represents types that can be compared.
/// Implemented by all builtin integer and floating point scalar or vector types.
interface IComparable
{
/// Returns true if two values of the conforming type are equal.
__builtin_requirement($((int)BuiltinRequirementKind::Equals))
bool equals(This other);
/// Returns true if `this` is less than `other`.
__builtin_requirement($((int)BuiltinRequirementKind::LessThan))
bool lessThan(This other);
/// Returns true if `this` is less than or equal to `other`.
__builtin_requirement($((int)BuiltinRequirementKind::LessThanOrEquals))
bool lessThanOrEquals(This other);
}
/// Represents types that has a defined range of values.
/// Implemented by all builtin integer and floating point types.
interface IRangedValue
{
/// The maximum value that an instance of the type can hold.
/// This is a constant value specific to the type.
static const This maxValue;
/// The minimum value that an instance of the type can hold.
/// This is a constant value specific to the type.
static const This minValue;
}
__attributeTarget(DeclBase)
attribute_syntax [TreatAsDifferentiable] : TreatAsDifferentiableAttribute;
/// Represents types that provide arithmetic operations.
interface IArithmetic : IComparable
{
/// Adds two values of the conforming type.
/// @param other The value to add to `this`.
/// @return The sum of `this` and `other`.
This add(This other);
/// Subtracts one value of the conforming type from another.
/// @param other The value to subtract from `this`.
/// @return The difference of `this` and `other`.
This sub(This other);
/// Multiplies two values of the conforming type.
/// @param other The value to multiply with `this`.
/// @return The product of `this` and `other`.
This mul(This other);
/// Divides one value of the conforming type by another.
/// @param other The value by which to divide `this`.
/// @return The quotient of `this` divided by `other`.
This div(This other);
/// Computes the remainder of division of one value of the conforming type by another.
/// @param other The divisor used to divide `this`.
/// @return The remainder of `this` divided by `other`.
This mod(This other);
/// Negates a value of the conforming type.
/// @return The negation of `this`.
This neg();
__init(int val);
/// Initialize from the same type.
__init(This value);
}
/// Represents types that provide logical operations.
interface ILogical : IComparable
{
/// Shifts the bits of this value to the left by the specified number of positions.
__builtin_requirement($((int)BuiltinRequirementKind::Shl))
This shl(int value);
/// Shifts the bits of this value to the right by the specified number of positions.
__builtin_requirement($((int)BuiltinRequirementKind::Shr))
This shr(int value);
/// Performs a bitwise AND operation on this value with another value of the same type.
__builtin_requirement($((int)BuiltinRequirementKind::BitAnd))
This bitAnd(This other);
/// Performs a bitwise OR operation on this value with another value of the same type.
__builtin_requirement($((int)BuiltinRequirementKind::BitOr))
This bitOr(This other);
/// Performs a bitwise XOR operation on this value with another value of the same type.
__builtin_requirement($((int)BuiltinRequirementKind::BitXor))
This bitXor(This other);
/// Performs a bitwise NOT operation on this value, flipping all bits.
__builtin_requirement($((int)BuiltinRequirementKind::BitNot))
This bitNot();
/// Performs a logical AND operation on this value with another value of the same type.
__builtin_requirement($((int)BuiltinRequirementKind::And))
This and(This other);
/// Performs a logical OR operation on this value with another value of the same type.
__builtin_requirement($((int)BuiltinRequirementKind::Or))
This or(This other);
/// Performs a logical NOT operation on this value, returning the logical negation.
__builtin_requirement($((int)BuiltinRequirementKind::Not))
This not();
__builtin_requirement($((int)BuiltinRequirementKind::InitLogicalFromInt))
__init(int val);
}
/// Represents a type that can be used for integer arithmetic operations.
///
/// Implemented by builtin scalar types: `int`, `uint`, `int64_t`, `uint64_t`, `int8_t`, `uint8_t`, `int16_t`, `uint16_t`.
///
/// Also implemented by `vector<T, N>` where `T` is one of the above scalar types.
///
/// @remarks This interface can be used to define generic functions that work with integer-like types. See example below.
/// @example The following code defines a generic function that computes `a*b+1`, where `a`, `b` can be any integer scalar or vector types.
/// ```csharp
/// T compute<T:IInteger>(T a, T b)
/// {
/// return a * b + T(1);
/// }
///
/// RWStructuredBuffer<int> outputBuffer;
///
/// [numthreads(1,1,1)]
/// void test()
/// {
/// int a = 2;
/// int b = 3;
/// outputBuffer[0] = compute(a, b); // result = 2*3 + 1 = 7
///
/// int16_t2 a2 = int16_t2(2, 3);
/// int16_t2 b2 = int16_t2(4, 5);
/// // result2 = int16_t2(2*4 + 1, 3*5 + 1) = int16_t2(9, 16)
/// int16_t2 result2 = compute<int16_t2>(a2, b2);
/// outputBuffer[1] = result2.x;
/// }
/// ```
///
interface IInteger : IArithmetic, ILogical
{
/// Converts the value of the current instance to an `int`.
/// @return The converted `int` value.
int toInt();
/// Converts the value of the current instance to an `int64_t`.
/// @return The converted `int64_t` value.
int64_t toInt64();
/// Converts the value of the current instance to an `uint`.
/// @return The converted `uint` value.
uint toUInt();
/// Converts the value of the current instance to an `uint64_t`.
/// @return The converted `uint64_t` value.
uint64_t toUInt64();
__init(int val);
__init(int64_t val);
}
/// Represents a type that can be used for floating point arithmetic operations.
///
/// Implemented by builtin scalar types: `float`, `half`, `double`.
///
/// Also implemented by `vector<T, N>` where `T` is one of the above scalar types.
///
/// @remarks This interface can be used to define generic functions that work with floating-point-like types. See example below.
/// @example The following code defines a generic function that computes `a*b+1`, where `a`, `b` can be any floating point scalar or vector types.
/// ```csharp
/// T compute<T:IFloat>(T a, T b)
/// {
/// return a * b + T(1.0f);
/// }
///
/// RWStructuredBuffer<float> outputBuffer;
///
/// [numthreads(1,1,1)]
/// void test()
/// {
/// float a = 2.0;
/// float b = 3.0;
/// outputBuffer[0] = compute(a, b); // result = 2.0*3.0 + 1.0 = 7.0
///
/// half2 a2 = half2(2.0h, 3.0h);
/// half2 b2 = half2(4.0h, 5.0h);
/// // result2 = half2(2*4 + 1, 3*5 + 1) = half2(9, 16)
/// half2 result2 = compute(a2, b2);
/// outputBuffer[1] = result2.x;
/// }
/// ```
///
interface IFloat : IArithmetic, IDifferentiable
{
/// Initializes a value of the conforming type form a `float`.
[TreatAsDifferentiable]
__init(float value);
/// Converts a value of the conforming type into a `float`.
[TreatAsDifferentiable]
float toFloat();
/// Computes the arithmetic sum of two values of the conforming type.
[TreatAsDifferentiable]
This add(This other);
/// Computes the arithmetic subtraction from values of the conforming type.
[TreatAsDifferentiable]
This sub(This other);
/// Computes the arithmetic multiplication from values of the conforming type.
[TreatAsDifferentiable]
This mul(This other);
/// Computes the arithmetic division from values of the conforming type.
[TreatAsDifferentiable]
This div(This other);
/// Computes the arithmetic remainder from values of the conforming type.
[TreatAsDifferentiable]
This mod(This other);
/// Computes the arithmetic negation of the conforming type.
[TreatAsDifferentiable]
This neg();
[TreatAsDifferentiable]
__init(This value);
/// Multiplies a value of the conforming type by a floating point scale factor..
[TreatAsDifferentiable]
This scale<T:__BuiltinFloatingPointType>(T scale);
}
/// A type that can be used as an operand for builtins
[sealed]
[builtin]
interface __BuiltinType
{
}
/// A type that can be used for arithmetic operations. For defining generic functions that work with builtin scalar arithmetic types only.
/// Builtin types implementing this interface: `int`, `uint`, `int64_t`, `uint64_t`, `int8_t`, `uint8_t`, `int16_t`, `uint16_t`, `float`, `half`, `double`.
/// To define generic functions that work with both scalar and vector types, use `IInteger` or `IFloat` instead.
[sealed]
[builtin]
interface __BuiltinArithmeticType : __BuiltinType, IArithmetic
{
}
/// A type that can be used for logical/bitwise operations. For defining generic functions that work with builtin scalar logical types only.
/// Builtin types implementing this interface: `bool`, `int8_t`, `uint8_t`, `int16_t`, `uint16_t`, `int`, `uint`, `int64_t`, `uint64_t`.
[sealed]
[builtin]
interface __BuiltinLogicalType : __BuiltinType, ILogical
{
}
/// Represents builtin types that logically have a sign (positive/negative/zero).
[sealed]
[builtin]
interface __BuiltinSignedArithmeticType : __BuiltinArithmeticType {}
/// Represents builtin types that can represent integers.
[sealed]
[builtin]
interface __BuiltinIntegerType : __BuiltinArithmeticType, IInteger
{}
/// Represents a `int` or `uint` type.
[sealed]
[builtin]
interface __BuiltinInt32Type : __BuiltinIntegerType
{}
/// Represents a `int64_t` or `uint64_t` type.
[sealed]
[builtin]
interface __BuiltinInt64Type : __BuiltinIntegerType
{}
/// Represents builtin types that can represent a real number.
[sealed]
[builtin]
interface __BuiltinRealType : __BuiltinSignedArithmeticType {}
__attributeTarget(AggTypeDecl)
attribute_syntax [__NonCopyableType] : NonCopyableTypeAttribute;
__attributeTarget(FunctionDeclBase)
attribute_syntax [__NoSideEffect] : NoSideEffectAttribute;
/// Marks a function as being differentiable in forward-mode.
///
/// See the user guide [section on forward-mode differentiation](https://shader-slang.org/slang/user-guide/autodiff.html#invoking-auto-diff-in-slang) for more
///
/// If used on a function that has a definition (i.e. a function body),Slang will use
/// automatic-differentiation to generate a forward-mode derivative of this function,
/// unless an implementation is provided by the user via `[ForwardDerivative(fn)]`
///
/// If used on an interface requirement, the signature of the requirement is modified to
/// include forward-differentiability. Any satisfying method must also be forward-differentiable,
/// or provide an appropriate derivative implementation.
/// See the user guide [section on auto-diff for interfaces](https://shader-slang.org/slang/user-guide/autodiff.html##using-auto-diff-with-interface-requirements-and-interface-types) for more
///
__attributeTarget(FunctionDeclBase)
attribute_syntax [ForwardDifferentiable] : ForwardDifferentiableAttribute;
/// Marks a function as being differentiable for backward-mode auto-diff.
/// Note that in the current implementation, this implies that the method
/// is also forward differentiable.
///
/// This attribute is equivalent to using `[Differentiable]`
///
__attributeTarget(FunctionDeclBase)
attribute_syntax [BackwardDifferentiable(order:int = 0)] : BackwardDifferentiableAttribute;
/// Marks a function as being differentiable for both
/// forward and backward mode auto-diff.
///
/// This attribute is equivalent to using `[Differentiable]`
///
/// See the user guide [section on auto-diff invocation](https://shader-slang.org/slang/user-guide/autodiff.html#invoking-auto-diff-in-slang) for more.
///
/// If used on a function that has a definition (i.e. a function body), Slang will use
/// automatic-differentiation to generate the derivative implementations for this function,
/// unless an implementation is provided by the user via `[ForwardDerivative(fn)]` and/or `[BackwardDerivative(fn)]`
///
/// If used on an interface requirement, the signature of the requirement is modified to
/// include differentiability. Any satisfying method must also be differentiable,
/// or provide appropriate derivative implementations.
/// See the user guide [section on auto-diff for interfaces](https://shader-slang.org/slang/user-guide/autodiff.html##using-auto-diff-with-interface-requirements-and-interface-types) for more
///
__attributeTarget(FunctionDeclBase)
attribute_syntax [Differentiable(order:int = 0)] : BackwardDifferentiableAttribute;
__intrinsic_op($(kIROp_RequirePrelude))
void __requirePrelude(constexpr String preludeText);
__intrinsic_op($(kIROp_RequireTargetExtension))
void __requireTargetExtension(constexpr String preludeText);
/// @experimetal
/// Perform a compile-time condition check and emit a compile-time error if the condition is false.
/// @param condition The compile-time condition to check.
/// @param errorMessage The error message to emit if the condition is false.
__intrinsic_op($(kIROp_StaticAssert))
void static_assert(constexpr bool condition, NativeString errorMessage);
/// Represents a 'value' type that is differentiable for the purposes of automatic differentiation.
///
/// See the auto-diff user guide section for an introduction to
/// differentiable value types (https://shader-slang.org/slang/user-guide/autodiff.html#differentiable-value-types)
///
/// #### Builtin Differentiable Value Types
/// The following built-in types are differentiable:
/// - Scalars: `float`, `double` and `half`.
/// - Vector/Matrix: `vector` and `matrix` of `float`, `double` and `half` types.
/// - Arrays: `T[n]` is differentiable if `T` is differentiable.
/// - Tuples: `Tuple<each T>` is differentiable if `T` is differentiable.
///
/// The `IDifferentiable` interface requires the following definitions (which can be auto-generated by the compiler for most scenarios)
/// ```csharp
/// interface IDifferentiable
/// {
/// associatedtype Differential : IDifferentiable
/// where Differential.Differential == Differential;
///
/// static Differential dzero();
///
/// static Differential dadd(Differential, Differential);
/// }
/// ```
///
/// As defined by the `IDifferentiable` interface, a differentiable type must have a
/// `Differential` associated type that stores the derivative of the value.
/// A further requirement is that the type of the second-order derivative must be the same
/// `Differential` type. In another words, given a type `T`, `T.Differential` can be different
/// from `T`, but `T.Differential.Differential` must equal to `T.Differential`.
///
/// In addition, a differentiable type must define the `zero` value of its derivative,
/// and how to add two derivative values together. These function are used during reverse-mode
/// auto-diff, to initialize and accumulate derivatives of the given type.
///
/// #### Automatic Fulfillment of `IDifferentiable` Requirements
/// Assume the user has defined the following type:
///
/// ```csharp
/// struct MyRay
/// {
/// float3 origin;
/// float3 dir;
/// int nonDifferentiablePayload;
/// }
/// ```
///
/// The type can be made differentiable by adding `IDifferentiable` conformance:
/// ```csharp
/// struct MyRay : IDifferentiable
/// {
/// float3 origin;
/// float3 dir;
/// int nonDifferentiablePayload;
/// }
/// ```
///
/// Note that this code does not provide any explicit implementation of the `IDifferentiable` requirements. In this case the compiler will automatically synthesize all the requirements. This should provide the desired behavior most of the time. The procedure for synthesizing the interface implementation is as follows:
/// 1. A new type is generated that stores the `Differential` of all differentiable fields. This new type itself will conform to the `IDifferentiable` interface, and it will be used to satisfy the `Differential` associated type requirement.
/// 2. Each differential field will be associated to its corresponding field in the newly synthesized `Differential` type.
/// 3. The `zero` value of the differential type is made from the `zero` value of each field in the differential type.
/// 4. The `dadd` method invokes the `dadd` operations for each field whose type conforms to `IDifferentiable`.
/// 5. If the synthesized `Differential` type contains exactly the same fields as the original type, and the type of each field is the same as the original field type, then the original type itself will be used as the `Differential` type instead of creating a new type to satisfy the `Differential` associated type requirement. This means that all the synthesized `Differential` type use itself to meet its own `IDifferentiable` requirements.
///
/// #### Manual fulfilment of `IDifferentiable` requirements
/// In rare cases where more control is desired, the user can manually provide the implementation.
/// To do so, we will first define the `Differential` type for `MyRay`, and use it to fulfill
/// the `Differential` requirement in `MyRay`:
/// ```csharp
/// struct MyRayDifferential
/// {
/// float3 d_origin;
/// float3 d_dir;
/// }
///
/// struct MyRay : IDifferentiable
/// {
/// // Specify that `MyRay.Differential` is `MyRayDifferential`.
/// typealias Differential = MyRayDifferential;
///
/// // Specify that the derivative for `origin` will be stored in `MayRayDifferential.d_origin`.
/// [DerivativeMember(MayRayDifferential.d_origin)]
/// float3 origin;
///
/// // Specify that the derivative for `dir` will be stored in `MayRayDifferential.d_dir`.
/// [DerivativeMember(MayRayDifferential.d_dir)]
/// float3 dir;
///
/// // This is a non-differentiable field so we don't put any attributes on it.
/// int nonDifferentiablePayload;
///
/// // Define zero derivative.
/// static MyRayDifferential dzero()
/// {
/// return {float3(0.0), float3(0.0)};
/// }
///
/// // Define the add operation of two derivatives.
/// static MyRayDifferential dadd(MyRayDifferential v1, MyRayDifferential v2)
/// {
/// MyRayDifferential result;
/// result.d_origin = v1.d_origin + v2.d_origin;
/// result.d_dir = v1.d_dir + v2.d_dir;
/// return result;
/// }
/// }
/// ```
///
/// Note that for each struct field that is differentiable, we need to use the `[DerivativeMember]` attribute to associate it with the
/// corresponding field in the `Differential` type, so the compiler knows how to access the derivative for the field.
///
/// However, there is still a missing piece in the above code: we also need to make `MyRayDifferential` conform to `IDifferentiable` because it is required that the `Differential` of a type must itself be `Differential`. Again we can use automatic fulfillment by simply adding `IDifferentiable` conformance to `MyRayDifferential`:
/// ```csharp
/// struct MyRayDifferential : IDifferentiable
/// {
/// float3 d_origin;
/// float3 d_dir;
/// }
/// ```
/// In this case, since all fields in `MyRayDifferential` are differentiable, and the `Differential` of each field is the same as the original type of each field (i.e. `float3.Differential == float3` as defined in the core module), the compiler will automatically use the type itself as its own `Differential`, making `MyRayDifferential` suitable for use as `Differential` of `MyRay`.
///
/// We can also choose to manually implement `IDifferentiable` interface for `MyRayDifferential` as in the following code:
///
/// ```csharp
/// struct MyRayDifferential : IDifferentiable
/// {
/// typealias Differential = MyRayDifferential;
///
/// [DerivativeMember(MyRayDifferential.d_origin)]
/// float3 d_origin;
///
/// [DerivativeMember(MyRayDifferential.d_dir)]
/// float3 d_dir;
///
/// static MyRayDifferential dzero()
/// {
/// return {float3(0.0), float3(0.0)};
/// }
///
/// static MyRayDifferential dadd(MyRayDifferential v1, MyRayDifferential v2)
/// {
/// MyRayDifferential result;
/// result.d_origin = v1.d_origin + v2.d_origin;
/// result.d_dir = v1.d_dir + v2.d_dir;
/// return result;
/// }
/// }
/// ```
/// In this specific case, the automatically generated `IDifferentiable`
/// implementation will be exactly the same as the manually written code listed above.
///
///
__magic_type(DifferentiableType)
[KnownBuiltin($( (int)KnownBuiltinDeclName::IDifferentiable))]
interface IDifferentiable
{
// Note: the compiler implementation requires the `Differential` associated type to be defined
// before anything else.
__builtin_requirement($((int)BuiltinRequirementKind::DifferentialType))
associatedtype Differential : IDifferentiable;
/// Returns a zero-initialized value of the differential type.
__builtin_requirement($((int)BuiltinRequirementKind::DZeroFunc))
static Differential dzero();
/// Adds two differential values and returns the result.
__builtin_requirement($((int)BuiltinRequirementKind::DAddFunc))
static Differential dadd(Differential, Differential);
/// Multiplies a scalar value of a built-in real type with a differential value and returns the result.
__builtin_requirement($((int)BuiltinRequirementKind::DMulFunc))
__generic<T : __BuiltinRealType>
static Differential dmul(T, Differential);
};
/// @experimental
///
/// The `IDifferentiablePtrType` interface requires the following definitions.
///
/// ```csharp
/// interface IDifferentiablePtrType
/// {
/// associatedtype Differential : IDifferentiablePtrType
/// where Differential.Differential == Differential;
/// }
/// ```
///
/// Types that conform to this interface can be used with `DifferentialPtrPair<T>`
/// to pass the derivative components to calls to `fwd_diff(fn)` or `bwd_diff(fn)`
///
/// See the auto-diff user guide for more details (https://shader-slang.org/slang/user-guide/autodiff.html#differentiable-ptr-types)
///
/// @remarks Support for this interface is still experimental and subject to change.
///
__magic_type(DifferentiablePtrType)
[KnownBuiltin($( (int)KnownBuiltinDeclName::IDifferentiablePtr))]
interface IDifferentiablePtrType
{
__builtin_requirement($((int)BuiltinRequirementKind::DifferentialPtrType))
associatedtype Differential : IDifferentiablePtrType;
};
/// `DifferentialPair<T>` is a built-in type that carries both the original and derivative value of a term.
/// It is defined as follows:
/// ```csharp
/// struct DifferentialPair<T : IDifferentiable> : IDifferentiable
/// {
/// typealias Differential = DifferentialPair<T.Differential>;
/// property T p {get;}
/// property T.Differential d {get;}
/// static Differential dzero();
/// static Differential dadd(Differential a, Differential b);
/// }
/// ```
///
/// Differential pairs can be created via constructor or through the `diffPair()` operation
/// ```csharp
/// DifferentialPair<float> dpa = DifferentialPair<float>(1.0f, 2.0f);
/// DifferentialPair<float> dpa = diffPair(1.0f, 2.0f);
/// ```
/// Note that derivative pairs are used to pass derivatives into and out of auto-diff functions.
/// See documentation on `fwd_diff` and `bwd_diff` operators for more information.
///
__generic<T : IDifferentiable>
__magic_type(DifferentialPairType)
__intrinsic_type($(kIROp_DifferentialPairUserCodeType))
struct DifferentialPair : IDifferentiable
{
typedef DifferentialPair<T.Differential> Differential;
typedef T.Differential DifferentialElementType;
__intrinsic_op($(kIROp_MakeDifferentialPairUserCode))
__init(T _primal, T.Differential _differential);
property p : T
{
__intrinsic_op($(kIROp_DifferentialPairGetPrimalUserCode))
get;
}
property v : T
{
__intrinsic_op($(kIROp_DifferentialPairGetPrimalUserCode))
get;
}
property d : T.Differential
{
__intrinsic_op($(kIROp_DifferentialPairGetDifferentialUserCode))
get;
}
[__unsafeForceInlineEarly]
T.Differential getDifferential()
{
return d;
}
[__unsafeForceInlineEarly]
T getPrimal()
{
return p;
}
[__unsafeForceInlineEarly]
static Differential dzero()
{
return Differential(T.dzero(), T.Differential.dzero());
}
[__unsafeForceInlineEarly]
static Differential dadd(Differential a, Differential b)
{
return Differential(
T.dadd(
a.p,
b.p
),
T.Differential.dadd(a.d, b.d));
}
__generic<U : __BuiltinRealType>
[__unsafeForceInlineEarly]
static Differential dmul(U a, Differential b)
{
return Differential(
T.dmul<U>(a, b.p),
T.Differential.dmul<U>(a, b.d));
}
};
/// @experimental
/// `DifferentialPtrPair<T>` is a built-in type that carries both the original and differential of a
/// pointer-like object.
/// `T` must conform to `IDifferentiablePtrType`
///
/// It is defined as follows:
/// ```csharp
/// struct DifferentialPtrPair<T : IDifferentiablePtrType> : IDifferentiablePtrType
/// {
/// typealias Differential = DifferentialPtrPair<T.Differential>;
/// property T p {get;}
/// property T.Differential d {get;}
/// }
/// ```
/// @remarks
/// Differential ptr pairs can be created via constructor.
/// ```csharp
/// struct DPtrFloat : IDifferentialPtrType
/// {
/// typealias Differential = DPtrFloat;
/// float* ptr;
/// };
///
/// DifferentialPtrPair<DPtrFloat> dpa =
/// DifferentialPtrPair<float>({&outputBuffer[0]}, {&outputBuffer[1]});
/// ```
/// Note that derivative ptr pairs are used to pass derivatives into and out of auto-diff functions.
/// See documentation on `fwd_diff` and `bwd_diff` operators for more information.
///
///
__generic<T : IDifferentiablePtrType>
__magic_type(DifferentialPtrPairType)
__intrinsic_type($(kIROp_DifferentialPtrPairType))
struct DifferentialPtrPair : IDifferentiablePtrType
{
typedef DifferentialPtrPair<T.Differential> Differential;
typedef T.Differential DifferentialElementType;
__intrinsic_op($(kIROp_MakeDifferentialPtrPair))
__init(T _primal, T.Differential _differential);
property p : T
{
__intrinsic_op($(kIROp_DifferentialPtrPairGetPrimal))
get;
}
property v : T
{
__intrinsic_op($(kIROp_DifferentialPtrPairGetPrimal))
get;
}
property d : T.Differential
{
__intrinsic_op($(kIROp_DifferentialPtrPairGetDifferential))
get;
}
};
/// Represents builtin floating point scalar types.
/// To define generic functions that work with both scalar and vector types, use `IFloat` instead.
///
/// Implemented by `float`, `half` and `double` types.
[sealed]
[builtin]
[TreatAsDifferentiable]
interface __BuiltinFloatingPointType : __BuiltinRealType, IFloat
{
/// Get the value of the mathematical constant pi in this type.
[Differentiable]
static This getPi();
}
//@ hidden:
// A type resulting from an `enum` declaration.
[builtin]
__magic_type(EnumTypeType)
interface __EnumType : ILogical
{
// The type of tags for this `enum`
//
// Note: using `__Tag` instead of `Tag` to avoid any
// conflict if a user had an `enum` case called `Tag`
associatedtype __Tag : __BuiltinIntegerType;
__builtin_requirement($((int)BuiltinRequirementKind::InitLogicalFromInt))
__init(__Tag value);
};
//@public:
/// Represents types that provide a subscript operator so that they can be used like an immutable array.
/// @param T The element type returned by the subscript operator.
/// @remarks This interface is implemented by `Array`, `vector`, `matrix`, `StructuredBuffer` and `RWStructuredBuffer` types.
/// @example The follow example shows how to define a generic function that computes the sum of all elements in an array-like type.
/// ```csharp
/// T sum<T:IFloat, U:IArray<T>>(U array)
/// {
/// T result = T(0);
/// for (int i = 0; i < array.getCount(); i++)
/// {
/// result = result + array[i];
/// }
/// return result;
/// }
///
/// RWStructuredBuffer<float> outputBuffer;
///
/// [numthreads(1, 1, 1)]
/// void computeMain(int3 dispatchThreadID : SV_DispatchThreadID)
/// {
/// float arr[3] = { 1.0, 2.0, 3.0 };
/// float4 v = float4(1.0, 2.0, 3.0, 4.0);
/// float2x2 m = float2x2(1.0, 2.0, 3.0, 4.0);
///
/// outputBuffer[0] = sum(arr); // 6.0
///
/// // treat `v` as array of `float`.
/// outputBuffer[1] = sum(v); // 10.0
///
/// // treat `m` as array of `float2`.
/// float2 innerSum = sum(m);
/// outputBuffer[2] = sum(innerSum); // 10.0
/// }
/// ```
interface IArray<T>
{
/// Returns the number of elements in the conforming type.
int getCount();
/// The subscript operator to be provided by a conforming type.
__subscript(int index) -> T
{
get;
}
}
/// Represents types that provide a subscript operator so that they can be used like a mutable array.
/// @param T The element type returned by the subscript operator.
/// @remarks This interface is implemented by `Array`, `vector`, `matrix`, `StructuredBuffer` and `RWStructuredBuffer` types.
/// @example The follow example shows how to define a generic function uses the `IRWArray` interface to mutate an array-like value.
/// ```csharp
/// void writeToArray<U, T : IRWArray<U>>(inout T array, int index, U value) { array[index] = value; }
/// void writeToBuffer<U, T : IRWArray<U>>(T array, int index, U value) { array[index] = value; }
/// U readFromArray<U, T:IArray<U>>(T array, int index) { return array[index]; }
///
/// //TEST_INPUT:ubuffer(data=[0 0 0 0], stride=4):out,name=outputBuffer
/// RWStructuredBuffer<float> outputBuffer;
///
/// [numthreads(1, 1, 1)]
/// void computeMain(int3 dispatchThreadID: SV_DispatchThreadID)
/// {
/// float arr[3] = { 1.0, 2.0, 3.0 };
/// float4 v = float4(1.0, 2.0, 3.0, 4.0);
/// float2x2 m = float2x2(1.0, 2.0, 3.0, 4.0);
///
/// // treat `outputBuffer` as a mutable array of `float`.
/// writeToBuffer(outputBuffer, 0, 1.0f);
///
/// // treat `arr` as a mutable array of `float`.
/// writeToArray(arr, 0, 4.0f);
/// outputBuffer[1] = readFromArray(arr, 0); // 4.0
///
/// // treat `v` as a mutable array of `float`.
/// writeToArray(v, 3, 3.0f);
/// outputBuffer[2] = readFromArray(v, 3); // 3.0
///
/// // treat `m` as a mutable array of `float2`.
/// writeToArray(m, 1, float2(10.0f, 20.0f));
/// outputBuffer[3] = readFromArray(m, 1).x + readFromArray(m, 1).y; // 30.0
///
/// writeToBuffer(outputBuffer, 0, readFromArray(outputBuffer, 0));
/// }
/// ```
interface IRWArray<T> : IArray<T>
{
/// The subscript operator to be provided by a conforming type. Provides both a `get` and a `set` accessor.
__subscript(int index)->T
{
get;
set;
}
}
// The "comma operator" is effectively just a generic function that returns its second
// argument. The left-to-right evaluation order guaranteed by Slang then ensures that
// `left` is evaluated before `right`.
//
//@hidden:
__generic<T,U>
[__unsafeForceInlineEarly]
U operator,(T left, U right)
{
return right;
}
// The ternary `?:` operator does not short-circuit in HLSL, and Slang no longer
// follow that definition for the scalar condition overload, so this declaration just serves
// for type-checking purpose only.
//@hidden:
__generic<T> __intrinsic_op(select) T operator?: (bool condition, T ifTrue, T ifFalse);
//@hidden:
__generic<T, let N : int> __intrinsic_op(select) vector<T,N> operator?:(vector<bool,N> condition, vector<T,N> ifTrue, vector<T,N> ifFalse);
// Users are advised to use `select` instead if non-short-circuiting behavior is intended.
//@public:
__generic<T> __intrinsic_op(select) T select(bool condition, T ifTrue, T ifFalse);
__generic<T, let N : int> __intrinsic_op(select) vector<T,N> select(vector<bool,N> condition, vector<T,N> ifTrue, vector<T,N> ifFalse);
[require(hlsl)]
__generic<T, let N : int, let M : int> __intrinsic_op(select) matrix<T,N,M> __hlsl_select(matrix<bool,N,M> condition, matrix<T,N,M> ifTrue, matrix<T,N,M> ifFalse);
__generic<T, let N : int, let M : int>
matrix<T,N,M> select(matrix<bool,N,M> condition, matrix<T,N,M> ifTrue, matrix<T,N,M> ifFalse)
{
__target_switch
{
case hlsl:
return __hlsl_select(condition, ifTrue, ifFalse);
default:
matrix<T,N,M> result;
[[unroll]]
for (uint32_t i = 0; i < N; i++)
result[i] = select(condition[i], ifTrue[i], ifFalse[i]);
return result;
}
}
__generic<T, let N : int, let M : int>
matrix<T,N,M> operator?:(matrix<bool,N,M> condition, matrix<T,N,M> ifTrue, matrix<T,N,M> ifFalse)
{
return select(condition, ifTrue, ifFalse);
}
[ForceInline]
__generic<T> Optional<T> select(bool condition, __none_t ifTrue, T ifFalse)
{
return select(condition, Optional<T>(none), Optional<T>(ifFalse));
}
[ForceInline]
__generic<T> Optional<T> select(bool condition, T ifTrue, __none_t ifFalse)
{
return select(condition, Optional<T>(ifTrue), Optional<T>(none));
}
// Allow real-number types to be cast into each other
//@hidden:
__intrinsic_op($(kIROp_FloatCast))
T __realCast<T : __BuiltinRealType, U : __BuiltinRealType>(U val);
//@hidden:
__intrinsic_op($(kIROp_CastIntToFloat))
T __realCast<T : __BuiltinRealType, U : __BuiltinIntegerType>(U val);
//@hidden:
__intrinsic_op($(kIROp_IntCast))
T __intCast<T : __BuiltinType, U : __BuiltinType>(U val);
//@hidden:
__intrinsic_op($(kIROp_CastIntToFloat))
T __castIntToFloat<T:__BuiltinType, U:__BuiltinType>(U v);
//@hidden:
${{{{
// We are going to use code generation to produce the
// declarations for all of our base types.
static const int kBaseTypeCount = sizeof(kBaseTypes) / sizeof(kBaseTypes[0]);
for (int tt = 0; tt < kBaseTypeCount; ++tt)
{
}}}}
__builtin_type($(int(kBaseTypes[tt].tag)))
struct $(kBaseTypes[tt].name)
: __BuiltinType
${{{{
switch (kBaseTypes[tt].tag)
{
case BaseType::Half:
case BaseType::Float:
case BaseType::Double:
}}}}
, __BuiltinFloatingPointType
, __BuiltinRealType
, __BuiltinSignedArithmeticType
, __BuiltinArithmeticType
${{{{
break;
case BaseType::Int8:
case BaseType::Int16:
case BaseType::Int:
case BaseType::Int64:
case BaseType::IntPtr:
}}}}
, __BuiltinSignedArithmeticType
${{{{
; // fall through
case BaseType::UInt8:
case BaseType::UInt16:
case BaseType::UInt:
case BaseType::UInt64:
case BaseType::UIntPtr:
}}}}
, __BuiltinArithmeticType
, __BuiltinIntegerType
${{{{
if (kBaseTypes[tt].tag == BaseType::Int || kBaseTypes[tt].tag == BaseType::UInt)
}}}}
, __BuiltinInt32Type
${{{{
if (kBaseTypes[tt].tag == BaseType::Int64 || kBaseTypes[tt].tag == BaseType::UInt64)
}}}}
, __BuiltinInt64Type
${{{{
; // fall through
case BaseType::Bool:
}}}}
, __BuiltinLogicalType
${{{{
break;
default:
break;
}
}}}}
{
${{{{
// Declare initializers to convert from various other types
for (int ss = 0; ss < kBaseTypeCount; ++ss)
{
// Don't allow conversion to or from `void`
if (kBaseTypes[tt].tag == BaseType::Void)
continue;
if (kBaseTypes[ss].tag == BaseType::Void)
continue;
// We need to emit a modifier so that the semantic-checking
// layer will know it can use these operations for implicit
// conversion.
ConversionCost conversionCost = getBaseTypeConversionCost(
kBaseTypes[tt],
kBaseTypes[ss]);
IROp intrinsicOpCode = getBaseTypeConversionOp(
kBaseTypes[tt],
kBaseTypes[ss]);
BuiltinConversionKind builtinConversionKind = kBuiltinConversion_Unknown;
if (kBaseTypes[tt].tag == BaseType::Double &&
kBaseTypes[ss].tag == BaseType::Float)
builtinConversionKind = kBuiltinConversion_FloatToDouble;
const char* attrib = "";
if ((kBaseTypes[tt].flags & kBaseTypes[ss].flags & FLOAT_MASK) != 0)
attrib = "[TreatAsDifferentiable]";
}}}}
$(attrib)
__intrinsic_op($(intrinsicOpCode))
__implicit_conversion($(conversionCost), $(builtinConversionKind))
__init($(kBaseTypes[ss].name) value);
${{{{
}
// Integer type implementations.
switch (kBaseTypes[tt].tag)
{
case BaseType::Bool:
}}}}
[__unsafeForceInlineEarly] __intrinsic_op($(kIROp_Eql)) bool equals(This other);
[__unsafeForceInlineEarly] __intrinsic_op($(kIROp_Less)) bool lessThan(This other);
[__unsafeForceInlineEarly] __intrinsic_op($(kIROp_Leq)) bool lessThanOrEquals(This other);
[__unsafeForceInlineEarly] This shl(int other) { return __intCast<This>(__shl(__intCast<int>(this), other)); }
[__unsafeForceInlineEarly] This shr(int other) { return __intCast<This>(__shr(__intCast<int>(this), other)); }
[__unsafeForceInlineEarly] This bitAnd(This other) { return __intCast<This>(__and(__intCast<int>(this), __intCast<int>(other))); }
[__unsafeForceInlineEarly] This bitOr(This other) { return __intCast<This>(__or(__intCast<int>(this), __intCast<int>(other))); }
[__unsafeForceInlineEarly] __intrinsic_op($(kIROp_And)) This and(This other);
[__unsafeForceInlineEarly] __intrinsic_op($(kIROp_Or)) This or(This other);
[__unsafeForceInlineEarly] This bitXor(This other) { return __intCast<This>(__xor(__intCast<int>(this), __intCast<int>(other))); }
[__unsafeForceInlineEarly] This bitNot() { return __intCast<This>(__not(__intCast<int>(this))); }
[__unsafeForceInlineEarly] __intrinsic_op($(kIROp_Not)) This not();
${{{{
break;
case BaseType::UInt8:
case BaseType::UInt16:
case BaseType::UInt:
case BaseType::UInt64:
case BaseType::Int8:
case BaseType::Int16:
case BaseType::Int:
case BaseType::Int64:
case BaseType::IntPtr:
case BaseType::UIntPtr:
}}}}
// If this is a basic integer type, then define explicit
// initializers that take a value of an `enum` type.
//
// TODO: This should actually be restricted, so that this
// only applies `where T.__Tag == Self`, but we don't have
// the needed features in our type system to implement
// that constraint right now.
//
__generic<T:__EnumType>
__intrinsic_op($(kIROp_IntCast))
__init(T value);
// Implementation of the `IInteger` interface.
__intrinsic_op($(kIROp_Less)) bool lessThan(This other);
__intrinsic_op($(kIROp_Leq)) bool lessThanOrEquals(This other);
__intrinsic_op($(kIROp_Eql)) bool equals(This other);
__intrinsic_op($(kIROp_Add)) This add(This other);
__intrinsic_op($(kIROp_Sub)) This sub(This other);
__intrinsic_op($(kIROp_Mul)) This mul(This other);
__intrinsic_op($(kIROp_Div)) This div(This other);
__intrinsic_op($(kIROp_IRem)) This mod(This other);
__intrinsic_op($(kIROp_Neg)) This neg();
__intrinsic_op($(kIROp_Lsh)) This shl(int other);
__intrinsic_op($(kIROp_Rsh)) This shr(int other);
__intrinsic_op($(kIROp_BitAnd)) This bitAnd(This other);
__intrinsic_op($(kIROp_BitOr)) This bitOr(This other);
[__unsafeForceInlineEarly] This and(This other) {return __intCast<This>(and(__intCast<bool>(this), __intCast<bool>(other))); }
[__unsafeForceInlineEarly] This or(This other) {return __intCast<This>(__intCast<bool>(this) || __intCast<bool>(other)); }
__intrinsic_op($(kIROp_BitXor)) This bitXor(This other);
__intrinsic_op($(kIROp_BitNot)) This bitNot();
[__unsafeForceInlineEarly] This not() {return __intCast<This>(!__intCast<bool>(this)); }
__intrinsic_op($(kIROp_IntCast)) int toInt();
__intrinsic_op($(kIROp_IntCast)) int64_t toInt64();
__intrinsic_op($(kIROp_IntCast)) uint toUInt();
__intrinsic_op($(kIROp_IntCast)) uint64_t toUInt64();
${{{{
break;
default:
break;
}
// If this is a floating-point type, then we need to
// implement the `IFloat` interface, which defines the basic `getPi()`
// function that is used to implement generic versions of `degrees()` and
// `radians()`.
//
switch (kBaseTypes[tt].tag)
{
default:
break;
case BaseType::Half:
case BaseType::Float:
case BaseType::Double:
}}}}
[Differentiable]
static $(kBaseTypes[tt].name) getPi() { return $(kBaseTypes[tt].name)(3.14159265358979323846264338328); }
__intrinsic_op($(kIROp_Less)) bool lessThan(This other);
__intrinsic_op($(kIROp_Leq)) bool lessThanOrEquals(This other);
__intrinsic_op($(kIROp_Eql)) bool equals(This other);
__intrinsic_op($(kIROp_Add)) This add(This other);
__intrinsic_op($(kIROp_Sub)) This sub(This other);
__intrinsic_op($(kIROp_Mul)) This mul(This other);
__intrinsic_op($(kIROp_Div)) This div(This other);
__intrinsic_op($(kIROp_FRem)) This mod(This other);
__intrinsic_op($(kIROp_Neg)) This neg();
__intrinsic_op($(kIROp_FloatCast)) float toFloat();
[__unsafeForceInlineEarly] This scale<T:__BuiltinFloatingPointType>(T s) { return __mul(this, __realCast<This>(s)); }
typedef $(kBaseTypes[tt].name) Differential;
[__unsafeForceInlineEarly]
[BackwardDifferentiable]
static Differential dzero()
{
return Differential(0);
}
[__unsafeForceInlineEarly]
[BackwardDifferentiable]
static Differential dadd(Differential a, Differential b)
{
return a + b;
}
__generic<U : __BuiltinRealType>
[__unsafeForceInlineEarly]
[BackwardDifferentiable]
static Differential dmul(U a, Differential b)
{
return __realCast<Differential, U>(a) * b;
}
${{{{
break;
}
// If this is the `void` type, then we want to allow
// explicit conversion to it from any other type, using
// `(void) someExpression`.
//
if( kBaseTypes[tt].tag == BaseType::Void )
{
}}}}
__generic<T>
[__readNone]
__intrinsic_op($(kIROp_CastToVoid))
__init(T value)
{}
${{{{
}
}}}}
}
${{{{
}
// Declare built-in pointer type
// (eventually we can have the traditional syntax sugar for this)
}}}}
//@hidden:
__magic_type(NullPtrType)
struct NullPtr
{
};
//@hidden:
__magic_type(NoneType)
__intrinsic_type($(kIROp_VoidType))
struct __none_t
{
};
/// @category misc_types
__magic_enum(AddressSpace)
enum AddressSpace : uint64_t
{
Device = $((uint64_t)AddressSpace::UserPointer),
GroupShared = $((uint64_t)AddressSpace::GroupShared),
VaryingInput = $((uint64_t)AddressSpace::Input),
};
/// @category misc_types
__magic_enum(MemoryScope)
enum MemoryScope : int32_t
{
CrossDevice = $((int32_t)MemoryScope::CrossDevice),
Device = $((int32_t)MemoryScope::Device),
Workgroup = $((int32_t)MemoryScope::Workgroup),
Subgroup = $((int32_t)MemoryScope::Subgroup),
Invocation = $((int32_t)MemoryScope::Invocation),
QueueFamily = $((int32_t)MemoryScope::QueueFamily),
}
/// @category misc_types
__magic_enum(AccessQualifier)
enum Access : uint64_t
{
ReadWrite = $((uint64_t)AccessQualifier::ReadWrite),
Read = $((uint64_t)AccessQualifier::Read),
}
//@public:
/// Represents a pointer type.
/// @param T The type of the value pointed to.
/// @remarks `T* val` is equivalent to `Ptr<T> val`.
__magic_type(PtrType)
__intrinsic_type($(kIROp_PtrType))
struct Ptr<
T,
Access access = Access::ReadWrite,
AddressSpace addrSpace = AddressSpace::Device>
{
// A user is allowed to explicitly cast between any pointer type of
// the same address space
__intrinsic_op($(kIROp_BitCast))
__init<U, Access accessOther>(Ptr<U, accessOther, addrSpace> ptr);
__intrinsic_op($(kIROp_CastIntToPtr))
__init(uint64_t val);
__intrinsic_op($(kIROp_CastIntToPtr))
__init(int64_t val);
/// Subscript a pointer to get a reference to a value.
///
/// The pointer must reference a memory location where
/// N >= 0 values of type `T` are stored sequentially,
/// in a layout consistent with an array `T[N]`;
/// otherwise, this operation has undefined behavior.
///
/// The `index` parameter must satisfy 0 <= `index` <= N;
/// otherwise, this operation has undefined behavior.
///
__generic<TInt : __BuiltinIntegerType>
__subscript(TInt index) -> T
{
//
// TODO(tfoley): The `Ptr` type's subscript operation
// currently provides a `ref` accessor (which returns a
// reference that can be used for reading or writing),
// independent of the `Access` that is specified by the
// generic arguments of a particular pointer type. In
// practice, this means that subscripting a read-only
// pointer yields a readable *and* writable reference.
//
// Previous versions of the code module attempted to
// address the problem by providing a `get` accessor
// on all platforms, and then use an `extension` to
// introduce a `ref` accessor only to pointers that
// are known to be mutable. That approach does not work,
// however, because it is necessary that subscripting
// a pointer yields a reference (l-value), whether or not
// the reference is read-only.
//
// A different solution to the problem is needed, whether
// by splitting up read-only and read-write pointers as
// distinct types (as many languages do), or by allowing
// the subscript operation to explicitly return a reference
// with the same access as the pointer itself.
//
__intrinsic_op($(kIROp_GetOffsetPtr))
[nonmutating]
ref;
}
};
//@hidden:
__intrinsic_op($(kIROp_AlignedAttr))
void __align_attr(int alignment);
__intrinsic_op($(kIROp_Load))
T __load_aligned<T, U>(T* ptr, U alignmentAttr);
__intrinsic_op($(kIROp_Store))
void __store_aligned<T, U>(T* ptr, T value, U alignmentAttr);
//@public:
/// Load a value from a pointer with a known alignment.
/// Aligned loads are more efficient than unaligned loads on some platforms.
/// @param alignment The alignment of the load operation.
/// @param ptr The pointer to load from.
/// @return The value loaded from the pointer.
/// @remarks When targeting SPIRV, this function maps to an `OpLoad` instruction with the `Aligned` memory operand.
/// The functions maps to normal load operation on other targets.
///
[__NoSideEffect]
[ForceInline]
T loadAligned<int alignment, T>(T* ptr)
{
return __load_aligned(ptr, __align_attr(alignment));
}
/// Store a value to a pointer with a known alignment.
/// Aligned stores are more efficient than unaligned stores on some platforms.
/// @param alignment The alignment of the store operation.
/// @param ptr The pointer to store value to.
/// @param value The value to store.
/// @remarks When targeting SPIRV, this function maps to an `OpStore` instruction with the `Aligned` memory operand.
/// The functions maps to normal store operation on other targets.
///
[ForceInline]
void storeAligned<int alignment, T>(T* ptr, T value)
{
__store_aligned(ptr, value, __align_attr(alignment));
}
${{{
StringBuilder ptrTypeParameterListBuilder;
ptrTypeParameterListBuilder << "T, Access access, AddressSpace addrSpace";
String ptrTypeParameterList = ptrTypeParameterListBuilder.toString();
StringBuilder ptrArgListBuilder;
ptrArgListBuilder << "T, access, addrSpace";
String ptrArgList = ptrArgListBuilder.toString();
StringBuilder fullPtrTypeBuilder;
fullPtrTypeBuilder << "Ptr<" << ptrArgList << ">";
String fullPtrType = fullPtrTypeBuilder.toString();
}}}
//@hidden:
__intrinsic_op($(kIROp_Load))
T __load<$(ptrTypeParameterList)>($(fullPtrType) ptr);
__intrinsic_op($(kIROp_Store))
void __store<$(ptrTypeParameterList)>($(fullPtrType) ptr, T val);
__intrinsic_op($(kIROp_GetElementPtr))
$(fullPtrType) __getElementPtr<$(ptrTypeParameterList), TIndex : __BuiltinIntegerType>($(fullPtrType) ptr, TIndex index);
__intrinsic_op($(kIROp_GetOffsetPtr))
$(fullPtrType) __getOffsetPtr<$(ptrTypeParameterList), TIndex : __BuiltinIntegerType>($(fullPtrType) ptr, TIndex index);
__generic<$(ptrTypeParameterList)>
__intrinsic_op($(kIROp_Less))
bool operator <($(fullPtrType) p1, $(fullPtrType) p2);
__generic<$(ptrTypeParameterList)>
__intrinsic_op($(kIROp_Leq))
bool operator <=($(fullPtrType) p1, $(fullPtrType) p2);
__generic<$(ptrTypeParameterList)>
__intrinsic_op($(kIROp_Greater))
bool operator>($(fullPtrType) p1, $(fullPtrType) p2);
__generic<$(ptrTypeParameterList)>
__intrinsic_op($(kIROp_Geq))
bool operator >=($(fullPtrType) p1, $(fullPtrType) p2);
__generic<$(ptrTypeParameterList)>
__intrinsic_op($(kIROp_Neq))
bool operator !=($(fullPtrType) p1, $(fullPtrType) p2);
__generic<$(ptrTypeParameterList)>
__intrinsic_op($(kIROp_Eql))
bool operator ==($(fullPtrType) p1, $(fullPtrType) p2);
//@public:
extension bool : IRangedValue
{
__generic<$(ptrTypeParameterList)>
__implicit_conversion($(kConversionCost_PtrToBool))
__intrinsic_op($(kIROp_CastPtrToBool))
__init($(fullPtrType) ptr);
__generic<T : __EnumType>
__implicit_conversion($(kConversionCost_IntegerTruncate))
[__unsafeForceInlineEarly]
__init(T v)
{
return __slang_noop_cast<T.__Tag>(v) != __intCast<T.__Tag>(0);
}
static const bool maxValue = true;
static const bool minValue = false;
}
extension uint64_t : IRangedValue
{
__generic<$(ptrTypeParameterList)>
__intrinsic_op($(kIROp_CastPtrToInt))
__init($(fullPtrType) ptr);
static const uint64_t maxValue = 0xFFFFFFFFFFFFFFFFULL;
static const uint64_t minValue = 0;
}
extension int64_t : IRangedValue
{
__generic<$(ptrTypeParameterList)>
__intrinsic_op($(kIROp_CastPtrToInt))
__init($(fullPtrType) ptr);
static const int64_t maxValue = 0x7FFFFFFFFFFFFFFFLL;
static const int64_t minValue = -0x8000000000000000LL;
}
extension intptr_t : IRangedValue
{
__generic<$(ptrTypeParameterList)>
__intrinsic_op($(kIROp_CastPtrToInt))
__init($(fullPtrType) ptr);
static const intptr_t maxValue = $(SLANG_PROCESSOR_X86_64?"0x7FFFFFFFFFFFFFFFz":"0x7FFFFFFFz");
static const intptr_t minValue = $(SLANG_PROCESSOR_X86_64?"0x8000000000000000z":"0x80000000z");
static const int size = $(SLANG_PROCESSOR_X86_64?"8":"4");
}
extension uintptr_t : IRangedValue
{
__generic<$(ptrTypeParameterList)>
__intrinsic_op($(kIROp_CastPtrToInt))
__init($(fullPtrType) ptr);
static const uintptr_t maxValue = $(SLANG_PROCESSOR_X86_64?"0xFFFFFFFFFFFFFFFFz":"0xFFFFFFFFz");
static const uintptr_t minValue = 0z;
static const int size = $(SLANG_PROCESSOR_X86_64?"8":"4");
}
//@hidden:
__magic_type(ExplicitRefType)
__intrinsic_type($(kIROp_RefType))
struct Ref<
T,
Access access = Access::ReadWrite,
AddressSpace addrSpace = AddressSpace::Device>
{};
__magic_type(OutParamType)
__intrinsic_type($(kIROp_OutType))
struct OutParam<T>
{};
__magic_type(InOutParamType)
__intrinsic_type($(kIROp_InOutType))
struct InOutParam<T>
{};
__magic_type(RefParamType)
__intrinsic_type($(kIROp_RefType))
struct RefParam<T>
{};
__magic_type(ConstRefParamType)
__intrinsic_type($(kIROp_ConstRefType))
struct ConstRefParam<T>
{};
// __Addr<T> is AddressSpace::Generic since Slang will specalize & validate the address-space
// internally to a concrete address-space.
typealias __Addr<T> = Ptr<T, Access::ReadWrite, (AddressSpace)$((uint64_t)AddressSpace::Generic)>;
//@public:
/// `Optional<T>` is a type with one extra value than `T`, this extra value is
/// used to represent a "missing" or "invalid" `T`. This extra value is called
/// `none`.
///
/// `none` can be compared against with the operators `==` or `!=`.
///
/// An `Optional<T>` value can also be implicitly or explicitly constructed from
/// a value of type `T`
///
/// `Optional<T>` values can be deconstructed with `if(let myT = myOptionalT) ...`
/// where this branch will only be taken if `myOptionalT` contains a value
/// of type `T` (to be put into scope as `myT`).
__generic<T>
__magic_type(OptionalType)
__intrinsic_type($(kIROp_OptionalType))
struct Optional : IDefaultInitializable
{
/// Return `true` iff this `Optional` contains a value of type `T`
property bool hasValue
{
__intrinsic_op($(kIROp_OptionalHasValue))
get;
}
/// If this `Optional` contains a value of type `T` return that.
property T value
{
__intrinsic_op($(kIROp_GetOptionalValue))
get;
}
__implicit_conversion($(kConversionCost_ValToOptional))
__intrinsic_op($(kIROp_MakeOptionalValue))
__init(T val);
[__unsafeForceInlineEarly]
__init() { this = none; }
};
//@hidden:
__generic<T>
[__unsafeForceInlineEarly]
bool operator==(Optional<T> val, __none_t noneVal)
{
return !val.hasValue;
}
__generic<T>
[__unsafeForceInlineEarly]
bool operator!=(Optional<T> val, __none_t noneVal)
{
return val.hasValue;
}
__generic<T>
[__unsafeForceInlineEarly]
bool operator==(__none_t noneVal, Optional<T> val)
{
return !val.hasValue;
}
__generic<T>
[__unsafeForceInlineEarly]
bool operator!=(__none_t noneVal, Optional<T> val)
{
return val.hasValue;
}
struct Conditional<T, bool hasValue>
{
internal T storage[hasValue];
__implicit_conversion($(kConversionCost_ValToOptional))
[__unsafeForceInlineEarly]
__init(T val) { if (hasValue) storage[0] = val;}
[__unsafeForceInlineEarly]
public Optional<T> get()
{
if (hasValue)
{
return Optional<T>(storage[0]);
}
else
{
return none;
}
}
[__unsafeForceInlineEarly]
[mutating]
public void set(T value)
{
if (hasValue)
storage[0] = value;
}
}
extension<T> Optional<T>
{
__implicit_conversion($(kConversionCost_ImplicitDereference))
__generic<bool condHasValue>
[__unsafeForceInlineEarly]
__init(Conditional<T, condHasValue> condVal)
{
if (condHasValue)
this = Optional<T>(condVal.storage[0]);
else
this = none;
}
}
//@public:
/// A variadic generic storing the product of several types.
///
/// The member names of individual values are `_0`, `_1`, `_2`, ...
///
/// New tuples can also be constructed by swizzling an existing tuple with the
/// concatenation of these names, for example `x._2_1_0` will return the first
/// three members of the tuple `x` in reverse order.
///
/// When all tuple elements conform to `IComparable` tuples can themselves be
/// compared according to a lexicographic ordering.
///
/// The number of elements in a tuple is given by the `countof` function.
__generic<each T>
__magic_type(TupleType)
struct Tuple
{
__intrinsic_op($(kIROp_MakeTuple))
__init(expand each T);
}
/// Construct a tuple from several values in order
__intrinsic_op($(kIROp_MakeTuple))
Tuple<T> makeTuple<each T>(T v);
/// Return a tuple containing the values of both input tuples in order
Tuple<T, U> concat<each T, each U>(Tuple<T> t, Tuple<U> u)
{
return makeTuple(expand each t, expand each u);
}
//@hidden:
[__unsafeForceInlineEarly]
bool __assign(inout bool v, bool newVal)
{
v = newVal;
return newVal;
}
[__unsafeForceInlineEarly]
void __tupleLessKernel<T : IComparable>(inout bool result, inout bool exit, T a, T b)
{
if (!exit)
{
if (a.lessThan(b))
{
result = true;
exit = true;
}
else if (!a.equals(b))
{
exit = true;
}
}
}
[__unsafeForceInlineEarly]
void __tupleGreaterKernel<T : IComparable>(inout bool result, inout bool exit, T a, T b)
{
if (!exit)
{
if (!a.lessThanOrEquals(b))
{
result = true;
exit = true;
}
else if (!a.equals(b))
{
exit = true;
}
}
}
//@public:
__generic<each T : IComparable>
extension Tuple<T> : IComparable
{
bool lessThan(Tuple<T> other)
{
bool result = false;
bool exit = false;
expand __tupleLessKernel(result, exit, each this, each other);
return result;
}
bool lessThanOrEquals(Tuple<T> other)
{
bool result = false;
bool exit = false;
expand __tupleGreaterKernel(result, exit, each this, each other);
return !result;
}
bool equals(Tuple<T> other)
{
bool result = true;
expand result && __assign(result, result && (each this).equals(each other));
return result;
}
}
/// Represents an interface for a mutating function that can take multiple parameters.
/// The function allows to modify the state of the object it belongs to.
interface IMutatingFunc<TR, each TP>
{
/// Defines a mutating function that takes multiple parameters and returns a result of type `TR`.
/// This function can modify the state of the object it is called on.
[mutating]
TR operator()(expand each TP p);
}
/// Represents an interface for a function that can take multiple parameters.
/// This interface inherits from `IMutatingFunc` but is used for non-mutating functions.
interface IFunc<TR, each TP> : IMutatingFunc<TR, expand each TP>
{
/// Defines a non-mutating function that takes multiple parameters and returns a result of type `TR`.
TR operator()(expand each TP p);
}
/// Represents an interface for a mutating function that can take multiple differentiable parameters.
/// The function allows to modify the state of the object it belongs to and supports differentiation.
interface IDifferentiableMutatingFunc<TR : IDifferentiable, each TP : IDifferentiable> : IMutatingFunc<TR, expand each TP>
{
/// Defines a mutating function that takes multiple differentiable parameters and returns a differentiable result of type `TR`.
/// This function can modify the state of the object it is called on and supports automatic differentiation.
[Differentiable]
[mutating]
TR operator()(expand each TP p);
}
/// Represents an interface for a function that can take multiple differentiable parameters and supports differentiation.
/// This interface inherits from both `IFunc` and `IDifferentiableMutatingFunc` but is used for non-mutating differentiable functions.
interface IDifferentiableFunc<TR : IDifferentiable, each TP : IDifferentiable> : IFunc<TR, expand each TP>, IDifferentiableMutatingFunc<TR, expand each TP>
{
/// Defines a non-mutating function that takes multiple differentiable parameters and returns a differentiable result of type `TR`.
/// This function supports automatic differentiation.
[Differentiable]
TR operator()(expand each TP p);
}
//@hidden:
__generic<T>
__magic_type(NativeRefType)
__intrinsic_type($(kIROp_NativePtrType))
struct NativeRef
{
__intrinsic_op($(kIROp_GetNativePtr))
__init(T val);
};
__generic<T>
__intrinsic_op($(kIROp_ManagedPtrAttach))
void __managed_ptr_attach(__ref T val, NativeRef<T> nativeVal);
__generic<T>
[__unsafeForceInlineEarly]
T __attachToNativeRef(NativeRef<T> nativeVal)
{
T result;
__managed_ptr_attach(result, nativeVal);
return result;
}
//@public:
/// Represents a string.
/// This type can only be used on the CPU target.
__magic_type(StringType)
__intrinsic_type($(kIROp_StringType))
struct String
{
[require(cpp)]
__intrinsic_op($(kIROp_MakeString))
__init(int val);
[require(cpp)]
__intrinsic_op($(kIROp_MakeString))
__init(uint val);
[require(cpp)]
__intrinsic_op($(kIROp_MakeString))
__init(int64_t val);
[require(cpp)]
__intrinsic_op($(kIROp_MakeString))
__init(uint64_t val);
[require(cpp)]
__intrinsic_op($(kIROp_MakeString))
__init(float val);
[require(cpp)]
__intrinsic_op($(kIROp_MakeString))
__init(double val);
[require(cpp)]
__implicit_conversion($(kConversionCost_None))
__intrinsic_op($(kIROp_MakeString))
__init(NativeString value);
/// Returns the length of the string.
[require(cpp)]
int64_t getLength();
/// Returns the length of the string.
property int length
{
get { return (int)getLength(); }
}
};
/// @category misc_types
typedef String string;
/// @category misc_types
__magic_type(NativeStringType)
__intrinsic_type($(kIROp_NativeStringType))
struct NativeString
{
[require(cpp)]
int getLength()
{
__target_switch
{
case cpp: __intrinsic_asm "int(strlen($0))";
}
}
[require(cpp)]
Ptr<void> getBuffer()
{
__target_switch
{
case cpp: __intrinsic_asm "(void*)((const char*)($0))";
}
}
property int length { [__unsafeForceInlineEarly] get{return getLength();} }
__implicit_conversion($(kConversionCost_None))
__intrinsic_op($(kIROp_GetNativeStr))
__init(String value);
__init() { this = NativeString(""); }
};
extension<Access access> Ptr<void, access>
{
__implicit_conversion($(kConversionCost_PtrToVoidPtr))
[__unsafeForceInlineEarly]
__init(NativeString nativeStr) { this = Ptr<void, access>(nativeStr.getBuffer()); }
__generic<$(ptrTypeParameterList)>
__intrinsic_op($(kIROp_BitCast))
__implicit_conversion($(kConversionCost_PtrToVoidPtr))
__init($(fullPtrType) ptr);
__generic<T>
__intrinsic_op($(kIROp_BitCast))
__implicit_conversion($(kConversionCost_PtrToVoidPtr))
__init(NativeRef<T> ptr);
}
//@hidden:
__magic_type(DynamicType)
__intrinsic_type($(kIROp_DynamicType))
struct __Dynamic
{};
//@public:
extension half : IRangedValue
{
static const half maxValue = half(65504);
static const half minValue = half(-65504);
}
extension float : IRangedValue
{
static const float maxValue = 340282346638528859811704183484516925440.0f;
static const float minValue = -340282346638528859811704183484516925440.0f;
}
extension double : IRangedValue
{
static const double maxValue = bit_cast<double>(0x7fefffffffffffffULL);
static const double minValue = bit_cast<double>(0xffefffffffffffffULL);
}
extension int : IRangedValue
{
static const int maxValue = 2147483647;
static const int minValue = -2147483648;
}
extension uint : IRangedValue
{
static const uint maxValue = 4294967295;
static const uint minValue = 0;
}
extension int8_t : IRangedValue
{
static const int8_t maxValue = 127;
static const int8_t minValue = -128;
}
extension uint8_t : IRangedValue
{
static const uint8_t maxValue = 255;
static const uint8_t minValue = 0;
}
extension uint16_t : IRangedValue
{
static const uint16_t maxValue = 65535;
static const uint16_t minValue = 0;
}
extension int16_t : IRangedValue
{
static const int16_t maxValue = 32767;
static const int16_t minValue = -32768;
}
__generic<T, let N:int>
__magic_type(ArrayExpressionType)
struct Array : IRWArray<T>
{
__intrinsic_op($(kIROp_GetArrayLength))
int getCount();
}
/// @category math_types Math types
/// An `N` component vector with elements of type `T`.
__generic<T = float, let N : int = 4>
__magic_type(VectorExpressionType)
struct vector : IRWArray<T>
{
/// The element type of the vector
typedef T Element;
/// Initialize a vector where all elements have the same scalar `value`.
[TreatAsDifferentiable]
__implicit_conversion($(kConversionCost_ScalarToVector))
__intrinsic_op($(kIROp_MakeVectorFromScalar))
__init(T value);
/// Initialize a vector from a value of the same type
// TODO: we should revise semantic checking so this kind of "identity" conversion is not required
__intrinsic_op(0)
[TreatAsDifferentiable]
__init(vector<T,N> value);
[ForceInline]
int getCount() { return N; }
}
//@hidden:
static const int kRowMajorMatrixLayout = $(SLANG_MATRIX_LAYOUT_ROW_MAJOR);
static const int kColumnMajorMatrixLayout = $(SLANG_MATRIX_LAYOUT_COLUMN_MAJOR);
//@public:
/// A matrix with `R` rows and `C` columns, with elements of type `T`.
/// @category math_types Math types
__generic<T = float, let R : int = 4, let C : int = 4, let L : int = $(SLANG_MATRIX_LAYOUT_MODE_UNKNOWN)>
__magic_type(MatrixExpressionType)
struct matrix : IRWArray<vector<T,C>>
{
__intrinsic_op($(kIROp_MakeMatrixFromScalar))
__implicit_conversion($(kConversionCost_ScalarToMatrix))
[TreatAsDifferentiable]
__init(T val);
/// Initialize a vector from a value of the same type
// TODO: we should revise semantic checking so this kind of "identity" conversion is not required
__intrinsic_op(0)
[TreatAsDifferentiable]
__init(This value);
[ForceInline]
int getCount() { return R; }
}
//@hidden:
__intrinsic_op($(kIROp_Eql))
vector<bool, N> __vectorEql<T, let N : int>(vector<T, N> left, vector<T, N> right);
//@public:
__generic<T:__BuiltinIntegerType, let N : int>
extension vector<T,N> : IInteger
{
[__unsafeForceInlineEarly] bool lessThan(This other) { return this[0] < other[0]; }
[__unsafeForceInlineEarly] bool lessThanOrEquals(This other) { return this[0] <= other[0]; }
[__unsafeForceInlineEarly] bool equals(This other) { return all(__vectorEql(this, other)); }
__intrinsic_op($(kIROp_Add)) This add(This other);
__intrinsic_op($(kIROp_Sub)) This sub(This other);
__intrinsic_op($(kIROp_Mul)) This mul(This other);
__intrinsic_op($(kIROp_Div)) This div(This other);
__intrinsic_op($(kIROp_FRem)) This mod(This other);
__intrinsic_op($(kIROp_Neg)) This neg();
__intrinsic_op($(kIROp_Lsh))
This shl(int value);
__intrinsic_op($(kIROp_Rsh))
This shr(int value);
__intrinsic_op($(kIROp_BitAnd))
This bitAnd(This other);
__intrinsic_op($(kIROp_BitOr))
This bitOr(This other);
__intrinsic_op($(kIROp_BitXor))
This bitXor(This other);
__intrinsic_op($(kIROp_BitNot))
This bitNot();
__intrinsic_op($(kIROp_And))
This and(This other);
__intrinsic_op($(kIROp_Or))
This or(This other);
__intrinsic_op($(kIROp_Not))
This not();
[__unsafeForceInlineEarly] int toInt() { return __intCast<int>(this[0]); }
[__unsafeForceInlineEarly] int64_t toInt64() { return __intCast<int64_t>(this[0]); }
[__unsafeForceInlineEarly] uint toUInt() { return __intCast<uint>(this[0]); }
[__unsafeForceInlineEarly] uint64_t toUInt64() { return __intCast<uint64_t>(this[0]); }
[OverloadRank(-1)]
[__unsafeForceInlineEarly] __init(int v) { this = vector<T,N>(T(v)); }
[OverloadRank(-1)]
[__unsafeForceInlineEarly] __init(int64_t v) { this = vector<T,N>(T(v)); }
[OverloadRank(-1)]
__implicit_conversion($(kConversionCost_Default))
__intrinsic_op($(kIROp_CastFloatToInt))
__generic<U:__BuiltinFloatingPointType>
__init(vector<U, N> other);
[OverloadRank(-1)]
__intrinsic_op($(kIROp_IntCast))
__generic<U:__BuiltinIntegerType>
__init(vector<U, N> other);
}
__generic<T:__BuiltinFloatingPointType, let N : int>
extension vector<T,N> : IFloat
{
[__unsafeForceInlineEarly] bool lessThan(This other) { return this[0] < other[0]; }
[__unsafeForceInlineEarly] bool lessThanOrEquals(This other) { return this[0] <= other[0]; }
[__unsafeForceInlineEarly] bool equals(This other) { return all(__vectorEql(this, other)); }
__intrinsic_op($(kIROp_Add)) This add(This other);
__intrinsic_op($(kIROp_Sub)) This sub(This other);
__intrinsic_op($(kIROp_Mul)) This mul(This other);
__intrinsic_op($(kIROp_Div)) This div(This other);
__intrinsic_op($(kIROp_FRem)) This mod(This other);
__intrinsic_op($(kIROp_Neg)) This neg();
[__unsafeForceInlineEarly] This scale<T1:__BuiltinFloatingPointType>(T1 s) { return this.mul(__realCast<T>(s)); }
[__unsafeForceInlineEarly] float toFloat() { return __realCast<float>(this[0]); }
[OverloadRank(-1)]
__implicit_conversion($(kConversionCost_Default))
__intrinsic_op($(kIROp_FloatCast))
__generic<U:__BuiltinFloatingPointType>
__init(vector<U, N> other);
[OverloadRank(-1)]
__intrinsic_op($(kIROp_CastIntToFloat))
__generic<U:__BuiltinIntegerType>
__init(vector<U, N> other);
[OverloadRank(-1)]
[__unsafeForceInlineEarly] __init(int v) { this = vector<T,N>(T(v)); }
[OverloadRank(-1)]
[__unsafeForceInlineEarly] __init(float v) { this = vector<T,N>(T(v)); }
}
__intrinsic_op($(kIROp_Add))
T __internal_add<T>(T a, T b);
__intrinsic_op($(kIROp_Mul))
T __internal_mul<T, U>(U a, T b);
__generic<T:IDifferentiable, let N : int>
extension vector<T,N> : IDifferentiable
{
// IDifferentiable
typedef vector<T, N> Differential;
[__unsafeForceInlineEarly]
[BackwardDifferentiable]
static Differential dzero()
{
return Differential(__slang_noop_cast<T>(T.dzero()));
}
[__unsafeForceInlineEarly]
[BackwardDifferentiable]
static Differential dadd(Differential a, Differential b)
{
return __internal_add(a, b);
}
__generic<U : __BuiltinRealType>
[__unsafeForceInlineEarly]
[BackwardDifferentiable]
static Differential dmul(U a, Differential b)
{
return __internal_mul(__realCast<float>(a), b);
}
}
__generic<T:__BuiltinFloatingPointType, let N : int, let M : int, let L : int>
extension matrix<T,N,M,L> : IFloat
{
[TreatAsDifferentiable][__unsafeForceInlineEarly] bool lessThan(This other) { return this < other; }
[TreatAsDifferentiable][__unsafeForceInlineEarly] bool lessThanOrEquals(This other) { return this <= other; }
[TreatAsDifferentiable][__unsafeForceInlineEarly] bool equals(This other) { return all(this == other); }
[TreatAsDifferentiable] __intrinsic_op($(kIROp_Add)) This add(This other);
[TreatAsDifferentiable] __intrinsic_op($(kIROp_Sub)) This sub(This other);
[TreatAsDifferentiable] __intrinsic_op($(kIROp_Mul))This mul(This other);
[TreatAsDifferentiable] __intrinsic_op($(kIROp_Div)) This div(This other);
[TreatAsDifferentiable] __intrinsic_op($(kIROp_FRem)) This mod(This other);
[TreatAsDifferentiable] __intrinsic_op($(kIROp_Neg)) This neg();
[TreatAsDifferentiable][__unsafeForceInlineEarly] This scale<T1:__BuiltinFloatingPointType>(T1 s) { return this.mul(__realCast<T>(s)); }
[TreatAsDifferentiable][__unsafeForceInlineEarly] float toFloat() { return __realCast<float>(this[0][0]); }
[OverloadRank(-1)]
[TreatAsDifferentiable]
[__unsafeForceInlineEarly]
__implicit_conversion($(kConversionCost_ScalarIntegerToFloatMatrix))
__init(int v) { this = matrix<T,N,M>(T(v)); }
[OverloadRank(-1)]
[TreatAsDifferentiable]
[__unsafeForceInlineEarly]
__implicit_conversion($(kConversionCost_ScalarToMatrix))
__init(float v) { this = matrix<T,N,M>(T(v)); }
}
__generic<T:IDifferentiable, let N : int, let M : int, let L : int>
extension matrix<T,N,M,L> : IDifferentiable
{
// IDifferentiable.
typedef matrix<T, N,M,L> Differential;
[__unsafeForceInlineEarly]
[BackwardDifferentiable]
static Differential dzero()
{
return matrix<T, N,M,L>(__slang_noop_cast<T>(T.dzero()));
}
[__unsafeForceInlineEarly]
[BackwardDifferentiable]
static Differential dadd(Differential a, Differential b)
{
return __internal_add(a, b);
}
__generic<U : __BuiltinRealType>
[__unsafeForceInlineEarly]
[BackwardDifferentiable]
static Differential dmul(U a, Differential b)
{
return __internal_mul(__realCast<float>(a), b);
}
}
__generic<let R : int, let C : int, let L : int>
extension matrix<int16_t,R,C,L>
{
typealias T = int16_t;
__implicit_conversion($(kConversionCost_IntegerTruncate))
__init(int value) { this = matrix<T,R,C,L>(T(value)); }
}
//@hidden:
__intrinsic_op(makeVector)
__generic<T, let N:int>
vector<T,N*2> __makeVector(vector<T,N> vec1, vector<T,N> vec2);
//@public:
__generic<T>
extension vector<T, 4>
{
__generic<let L : int>
[__unsafeForceInlineEarly]
__init(matrix<T, 2, 2, L> value)
{
this = __makeVector(value[0], value[1]);
}
}
__generic<T, let L : int>
extension matrix<T, 2, 2, L>
{
[__unsafeForceInlineEarly]
__init(vector<T, 4> value)
{
this[0] = value.xy;
this[1] = value.zw;
}
}
//@hidden:
${{{{
static const struct {
char const* name;
char const* glslPrefix;
} kTypes[] =
{
{"half", "f16"},
{"float", ""},
{"double", "d"},
{"float16_t", "f16"},
{"float32_t", "f32"},
{"float64_t", "f64"},
{"int8_t", "i8"},
{"int16_t", "i16"},
{"int32_t", "i32"},
{"int", "i"},
{"int64_t", "i64"},
{"uint8_t", "u8"},
{"uint16_t", "u16"},
{"uint32_t", "u32"},
{"uint", "u"},
{"uint64_t", "u64"},
{"bool", "b"},
};
static const int kTypeCount = sizeof(kTypes) / sizeof(kTypes[0]);
for (int tt = 0; tt < kTypeCount; ++tt)
{
// Declare HLSL vector types
for (int ii = 1; ii <= 4; ++ii)
{
sb << "typedef vector<" << kTypes[tt].name << "," << ii << "> " << kTypes[tt].name << ii << ";\n";
}
// Declare HLSL matrix types
for (int rr = 1; rr <= 4; ++rr)
for (int cc = 1; cc <= 4; ++cc)
{
sb << "typedef matrix<" << kTypes[tt].name << "," << rr << "," << cc << "> " << kTypes[tt].name << rr << "x" << cc << ";\n";
}
}
sb << "\n\n";
sb << "// Cast from vector<T,1> to T, which is a scalar type\n";
for (int tt = 0; tt < kBaseTypeCount; ++tt)
{
if(kBaseTypes[tt].tag == BaseType::Void) continue;
const char* tname = kBaseTypes[tt].name;
sb << "extension " << tname << "\n";
sb << "{\n";
sb << " __implicit_conversion(" << kConversionCost_OneVectorToScalar << ")\n";
sb << " __init(vector<" << tname << ",1> v) { this = v[0]; }\n";
sb << "}\n";
}
// Declare additional built-in generic types
}}}}
//@ public:
__generic<T, L:IBufferDataLayout = DefaultDataLayout>
__intrinsic_type($(kIROp_ConstantBufferType))
__magic_type(ConstantBufferType)
struct ConstantBuffer {}
///@category texture_types
__generic<T>
__intrinsic_type($(kIROp_TextureBufferType))
__magic_type(TextureBufferType)
struct TextureBuffer {}
__generic<T>
__intrinsic_type($(kIROp_ParameterBlockType))
__magic_type(ParameterBlockType)
struct ParameterBlock {}
/// @category stage_io
__generic<T, let MAX_VERTS : uint>
__magic_type(VerticesType)
__intrinsic_type($(kIROp_VerticesType))
[__NonCopyableType]
struct OutputVertices
{
__intrinsic_op($(kIROp_MetalSetVertex))
static void _metalSetVertex(uint index, T val);
__intrinsic_op($(kIROp_MeshOutputSet))
static void _setVertex(This v, uint index, T val);
__subscript(uint index) -> T
{
// TODO: Make sure this remains write only, we can't do this with just
// a 'set' operation as it's legal to only write to part of the output
// buffer, or part of the output buffer at a time.
[mutating]
[require(glsl_hlsl_metal_spirv, meshshading)]
set
{
__target_switch
{
case metal: _metalSetVertex(index, newValue);
case glsl: _setVertex(this, index, newValue);
case hlsl: _setVertex(this, index, newValue);
case spirv: _setVertex(this, index, newValue);
}
}
//
// If a 'OutputVertices[index]' is referred to by a '__ref', call 'kIROp_MeshOutputRef(index)'
[require(glsl_hlsl_spirv, meshshading)]
__intrinsic_op($(kIROp_MeshOutputRef))
ref;
}
};
/// @category stage_io
__generic<T, let MAX_PRIMITIVES : uint>
__magic_type(IndicesType)
__intrinsic_type($(kIROp_IndicesType))
[__NonCopyableType]
struct OutputIndices
{
__intrinsic_op($(kIROp_MetalSetIndices))
static void __metalSetIndices(uint index, T val);
// for some reason only here in the indices array it uses the return value as an actual
// operand, while the others use the value of the instruction (the return value) to access
// the type of the vertex, as when using the ref there is no third operand
__intrinsic_op($(kIROp_MeshOutputSet))
static void __setIndices(This v, uint index, T val);
__subscript(uint index) -> T
{
[mutating]
[require(glsl_hlsl_metal_spirv, meshshading)]
set
{
__target_switch
{
case metal: __metalSetIndices(index, newValue);
case glsl: __setIndices(this, index, newValue);
case hlsl: __setIndices(this, index, newValue);
case spirv: __setIndices(this, index, newValue);
}
}
// If a 'OutputIndices[index]' is referred to by a '__ref', call 'kIROp_MeshOutputRef(index)'
[require(glsl_hlsl_metal_spirv, meshshading)]
__intrinsic_op($(kIROp_MeshOutputRef))
ref;
}
};
/// @category stage_io
__generic<T, let MAX_PRIMITIVES : uint>
__magic_type(PrimitivesType)
__intrinsic_type($(kIROp_PrimitivesType))
[__NonCopyableType]
struct OutputPrimitives
{
__intrinsic_op($(kIROp_MetalSetPrimitive))
static void __metalSetPrimitive(uint index, T val);
__intrinsic_op($(kIROp_MeshOutputSet))
static void __setPrimitive(This v, uint index, T val);
__subscript(uint index)->T
{
[mutating]
[require(glsl_hlsl_metal_spirv, meshshading)]
set
{
__target_switch
{
case metal: __metalSetPrimitive(index, newValue);
case glsl: __setPrimitive(this, index, newValue);
case hlsl: __setPrimitive(this, index, newValue);
case spirv: __setPrimitive(this, index, newValue);
}
}
// If a 'OutputPrimitives[index]' is referred to by a '__ref', call 'kIROp_MeshOutputRef(index)'
[require(glsl_hlsl_spirv, meshshading)]
__intrinsic_op($(kIROp_MeshOutputRef))
ref;
}
};
//@ public:
// Need to add constructors to the types above
__generic<T> __extension vector<T, 2>
{
__intrinsic_op($(kIROp_MakeVector))
__init(T x, T y);
}
__generic<T> __extension vector<T, 3>
{
__intrinsic_op($(kIROp_MakeVector))
__init(T x, T y, T z);
[__unsafeForceInlineEarly]
__intrinsic_op($(kIROp_MakeVector))
__init(vector<T,2> xy, T z);
[__unsafeForceInlineEarly]
__intrinsic_op($(kIROp_MakeVector))
__init(T x, vector<T,2> yz);
}
__generic<T> __extension vector<T, 4>
{
__intrinsic_op($(kIROp_MakeVector))
__init(T x, T y, T z, T w);
[__unsafeForceInlineEarly]
__intrinsic_op($(kIROp_MakeVector))
__init(vector<T,2> xy, T z, T w);
[__unsafeForceInlineEarly]
__intrinsic_op($(kIROp_MakeVector))
__init(T x, vector<T,2> yz, T w);
[__unsafeForceInlineEarly]
__intrinsic_op($(kIROp_MakeVector))
__init(T x, T y, vector<T,2> zw);
[__unsafeForceInlineEarly]
__intrinsic_op($(kIROp_MakeVector))
__init(vector<T,2> xy, vector<T,2> zw);
[__unsafeForceInlineEarly]
__intrinsic_op($(kIROp_MakeVector))
__init(vector<T,3> xyz, T w);
[__unsafeForceInlineEarly]
__intrinsic_op($(kIROp_MakeVector))
__init(T x, vector<T,3> yzw);
}
${{{{
for( int R = 1; R <= 4; ++R )
for( int C = 1; C <= 4; ++C )
{
sb << "__generic<T, let L:int> __extension matrix<T, " << R << "," << C << ", L>\n{\n";
// initialize from R*C scalars
if (R == 1 || C == 1)
sb << "[require(hlsl)]\n";
sb << "__intrinsic_op(" << int(kIROp_MakeMatrix) << ") __init(";
for( int ii = 0; ii < R; ++ii )
for( int jj = 0; jj < C; ++jj )
{
if ((ii+jj) != 0) sb << ", ";
sb << "T m" << ii << jj;
}
sb << ");\n";
// Initialize from R C-vectors
if (R == 1 || C == 1)
sb << "[require(hlsl)]\n";
sb << "__intrinsic_op(" << int(kIROp_MakeMatrix) << ") __init(";
for (int ii = 0; ii < R; ++ii)
{
if(ii != 0) sb << ", ";
sb << "vector<T," << C << "> row" << ii;
}
sb << ");\n";
// initialize from a matrix of larger size
for(int rr = R; rr <= 4; ++rr)
for( int cc = C; cc <= 4; ++cc )
{
if(rr == R && cc == C) continue;
if (R == 1 || C == 1)
sb << "[require(hlsl)]\n";
sb << "__intrinsic_op(" << int(kIROp_MatrixReshape) << ") __init(matrix<T," << rr << "," << cc << ", L> value);\n";
}
// Initialize from matrix and vector
if (R > 2)
{
sb << "__init(matrix<T," << (R - 1) << "," << C << "> m, vector<T," << C << "> row" << (R - 1) << ") ";
sb << "{ this = This(";
for (int ii = 0; ii < R - 1; ++ii)
{
sb << "m[" << ii << "], ";
}
sb << "row" << (R - 1) << "); }\n";
}
sb << "}\n";
}
}}}}
//@hidden:
__intrinsic_op($(kIROp_BuiltinCast))
internal T __builtin_cast<T, U>(U u);
// If T is implicitly convertible to U, then vector<T,N> is implicitly convertible to vector<U,N>.
__generic<ToType, let N : int> extension vector<ToType,N>
{
__implicit_conversion(constraint)
__intrinsic_op(BuiltinCast)
__init<FromType>(vector<FromType,N> value) where ToType(FromType) implicit;
__implicit_conversion(constraint+)
[__unsafeForceInlineEarly]
[__readNone]
[TreatAsDifferentiable]
__init<FromType>(FromType value) where ToType(FromType) implicit
{
this = __builtin_cast<vector<ToType,N>>(vector<FromType,N>(value));
}
}
// If T is implicitly convertible to U, then matrix<T,R,C,L> is implicitly convertible to matrix<U,R,C,L>.
__generic<ToType, let R : int, let C : int, let L : int> extension matrix<ToType,R,C,L>
{
__implicit_conversion(constraint)
__intrinsic_op(BuiltinCast)
__init<FromType>(matrix<FromType,R,C,L> value) where ToType(FromType) implicit;
}
//@ hidden:
__generic<T, U>
__intrinsic_op(0)
T __slang_noop_cast(U u);
//@ public:
/// Sampling state for filtered texture fetches.
/// @category sampler_types Sampler types
__magic_type(SamplerStateType, $(int(SamplerStateFlavor::SamplerState)))
__intrinsic_type($(kIROp_SamplerStateType))
struct SamplerState
{
}
/// Sampling state for filtered texture fetches that include a comparison operation before filtering.
/// @category sampler_types
__magic_type(SamplerStateType, $(int(SamplerStateFlavor::SamplerComparisonState)))
__intrinsic_type($(kIROp_SamplerComparisonStateType))
struct SamplerComparisonState
{
}
//@ hidden:
${{{{
for (auto op : intrinsicUnaryOps)
{
for (auto type : kBaseTypes)
{
if ((type.flags & op.flags) == 0)
continue;
char const* resultType = type.name;
if (op.flags & BOOL_RESULT) resultType = "bool";
// scalar version
sb << "__prefix __intrinsic_op(" << int(op.opCode) << ") " << resultType << " operator" << op.opName << "(" << type.name << " value);\n";
sb << "__intrinsic_op(" << int(op.opCode) << ") " << resultType << " __" << op.funcName << "(" << type.name << " value);\n";
// vector version
sb << "__generic<let N : int> ";
sb << "__prefix __intrinsic_op(" << int(op.opCode) << ") vector<" << resultType << ",N> operator" << op.opName << "(" << "vector<" << type.name << ",N> value);\n";
// matrix version
sb << "__generic<let N : int, let M : int> ";
sb << "__prefix __intrinsic_op(" << int(op.opCode) << ") matrix<" << resultType << ",N,M> operator" << op.opName << "(" << "matrix<" << type.name << ",N,M> value);\n";
}
// Synthesize generic versions
if(op.interface)
{
char const* resultType = "T";
if (op.flags & BOOL_RESULT) resultType = "bool";
// scalar version
sb << "__generic<T : " << op.interface << ">\n";
sb << "[OverloadRank(10)]";
sb << "__prefix __intrinsic_op(" << int(op.opCode) << ") " << resultType << " operator" << op.opName << "(" << "T value);\n";
// vector version
sb << "__generic<T : " << op.interface << ", let N : int> ";
sb << "[OverloadRank(10)]";
sb << "__prefix __intrinsic_op(" << int(op.opCode) << ") vector<" << resultType << ",N> operator" << op.opName << "(vector<T,N> value);\n";
// matrix version
sb << "__generic<T : " << op.interface << ", let N : int, let M : int> ";
sb << "[OverloadRank(10)]";
sb << "__prefix __intrinsic_op(" << int(op.opCode) << ") matrix<" << resultType << ",N,M> operator" << op.opName << "(matrix<T,N,M> value);\n";
}
}
}}}}
// Only ReadWrite is an L-value.
__generic<T, Access access, AddressSpace addrSpace>
__intrinsic_op(0)
__prefix Ref<T, access, addrSpace> operator*(Ptr<T, access, addrSpace> value);
// TODO: [require(cpu)]. This cannot be done yet since this change breaks slangpy
__generic<T>
__intrinsic_op(0)
[KnownBuiltin( $((int)KnownBuiltinDeclName::OperatorAddressOf))]
[require(cpp_cuda_spirv)]
__prefix Ptr<T, Access::ReadWrite, AddressSpace::Device> operator&(__ref T value);
__generic<$(ptrTypeParameterList), TInt : __BuiltinIntegerType>
__intrinsic_op($(kIROp_GetOffsetPtr))
$(fullPtrType) operator+($(fullPtrType) value, TInt offset);
__generic<$(ptrTypeParameterList), TInt : __BuiltinIntegerType>
[__unsafeForceInlineEarly]
$(fullPtrType) operator-($(fullPtrType) value, TInt offset)
{
return __getOffsetPtr(value, -offset);
}
__generic<T : IArithmetic>
[__unsafeForceInlineEarly]
__prefix T operator+(T value)
{ return value; }
__generic<T : __BuiltinArithmeticType, let N : int>
[__unsafeForceInlineEarly]
__prefix vector<T,N> operator+(vector<T,N> value)
{ return value; }
__generic<T : __BuiltinArithmeticType, let R : int, let C : int>
[__unsafeForceInlineEarly]
__prefix matrix<T,R,C> operator+(matrix<T,R,C> value)
{ return value; }
${{{{
static const struct IncDecOpInfo
{
char const* name;
char const* binOp;
} kIncDecOps[] =
{
{ "++", "+" },
{ "--", "-" },
};
static const struct IncDecOpFixity
{
char const* qual;
char const* bodyPrefix;
char const* returnVal;
} kIncDecFixities[] =
{
{ "__prefix", "", "value" },
{ "__postfix", " let result = value;", "result" },
};
for(auto op : kIncDecOps)
for(auto fixity : kIncDecFixities)
{
}}}}
$(fixity.qual)
__generic<T : __BuiltinArithmeticType>
[__unsafeForceInlineEarly]
T operator$(op.name)( in out T value)
{ $(fixity.bodyPrefix) value = value $(op.binOp) __builtin_cast<T>(1); return $(fixity.returnVal); }
$(fixity.qual)
__generic<T : __BuiltinArithmeticType, let N : int>
[__unsafeForceInlineEarly]
vector<T,N> operator$(op.name)(in out vector<T,N> value)
{$(fixity.bodyPrefix) value = value $(op.binOp) __builtin_cast<T>(1); return $(fixity.returnVal); }
$(fixity.qual)
__generic<T : __BuiltinArithmeticType, let R : int, let C : int, let L : int>
[__unsafeForceInlineEarly]
matrix<T,R,C> operator$(op.name)(in out matrix<T,R,C,L> value)
{$(fixity.bodyPrefix) value = value $(op.binOp) __builtin_cast<T>(1); return $(fixity.returnVal); }
$(fixity.qual)
__generic<$(ptrTypeParameterList)>
[__unsafeForceInlineEarly]
$(fullPtrType) operator$(op.name)(in out $(fullPtrType) value)
{$(fixity.bodyPrefix) value = value $(op.binOp) 1; return $(fixity.returnVal); }
${{{{
}
for (auto op : intrinsicBinaryOps)
{
for (auto type : kBaseTypes)
{
if ((type.flags & op.flags) == 0)
continue;
char const* leftType = type.name;
char const* rightType = leftType;
char const* resultType = leftType;
if (op.flags & BOOL_RESULT) resultType = "bool";
// TODO: We should handle a `SHIFT` flag on the op
// by changing `rightType` to `int` in order to
// account for the fact that the shift amount should
// always have a fixed type independent of the LHS.
//
// (It is unclear why this change hadn't been made
// already, so it is possible that such a change
// breaks overload resolution or other parts of
// the compiler)
// scalar version
sb << "__intrinsic_op(" << int(op.opCode) << ") " << resultType << " operator" << op.opName << "(" << leftType << " left, " << rightType << " right);\n";
sb << "__intrinsic_op(" << int(op.opCode) << ") " << resultType << " __" << op.funcName << "(" << leftType << " left, " << rightType << " right);\n";
// vector version
sb << "__generic<let N : int> ";
sb << "__intrinsic_op(" << int(op.opCode) << ") vector<" << resultType << ",N> operator" << op.opName << "(vector<" << leftType << ",N> left, vector<" << rightType << ",N> right);\n";
// matrix version
sb << "__generic<let N : int, let M : int> ";
sb << "__intrinsic_op(" << int(op.opCode) << ") matrix<" << resultType << ",N,M> operator" << op.opName << "(matrix<" << leftType << ",N,M> left, matrix<" << rightType << ",N,M> right);\n";
// We currently synthesize addiitonal overloads
// for the case where one or the other operand
// is a scalar. This choice serves a few purposes:
//
// 1. It avoids introducing scalar-to-vector or
// scalar-to-matrix promotions before the operator,
// which might allow some back ends to produce
// more optimal code.
//
// 2. It avoids concerns about making overload resolution
// and the inference rules for `N` and `M` able to
// handle the mixed vector/scalar or matrix/scalar case.
//
// 3. Having explicit overloads for the matrix/scalar cases
// here means that we do *not* need to support a general
// implicit conversion from scalars to matrices, unless
// we decide we want to.
//
// Note: Case (2) of the motivation shouldn't really apply
// any more, because we end up having to support similar
// inteference for built-in binary math functions where
// vectors and scalars might be combined (and where defining
// additional overloads to cover all the combinations doesn't
// seem practical or desirable).
//
// TODO: We should consider whether dropping these extra
// overloads is possible and worth it. The optimization
// concern (1) could possibly be addressed in specific
// back-ends. The issue (3) about not wanting to support
// implicit scalar-to-matrix conversion may be moot if
// we end up needing to support mixed scalar/matrix input
// for builtin in non-operator functions anyway.
// scalar-vector and scalar-matrix
sb << "__generic<let N : int> ";
sb << "__intrinsic_op(" << int(op.opCode) << ") vector<" << resultType << ",N> operator" << op.opName << "(" << leftType << " left, vector<" << rightType << ",N> right);\n";
sb << "__generic<let N : int, let M : int> ";
sb << "__intrinsic_op(" << int(op.opCode) << ") matrix<" << resultType << ",N,M> operator" << op.opName << "(" << leftType << " left, matrix<" << rightType << ",N,M> right);\n";
// vector-scalar and matrix-scalar
sb << "__generic<let N : int> ";
sb << "__intrinsic_op(" << int(op.opCode) << ") vector<" << resultType << ",N> operator" << op.opName << "(vector<" << leftType << ",N> left, " << rightType << " right);\n";
sb << "__generic<let N : int, let M : int> ";
sb << "__intrinsic_op(" << int(op.opCode) << ") matrix<" << resultType << ",N,M> operator" << op.opName << "(matrix<" << leftType << ",N,M> left, " << rightType << " right);\n";
}
// Synthesize generic versions
if(op.interface)
{
char const* leftType = "T";
char const* rightType = leftType;
char const* resultType = leftType;
if (op.flags & BOOL_RESULT) resultType = "bool";
// TODO: handle `SHIFT`
// scalar version
sb << "__generic<T : " << op.interface << ">\n";
sb << "[OverloadRank(10)]";
sb << "__intrinsic_op(" << int(op.opCode) << ") " << resultType << " operator" << op.opName << "(" << leftType << " left, " << rightType << " right);\n";
// vector version
sb << "__generic<T : " << op.interface << ", let N : int> ";
sb << "[OverloadRank(10)]";
sb << "__intrinsic_op(" << int(op.opCode) << ") vector<" << resultType << ",N> operator" << op.opName << "(vector<" << leftType << ",N> left, vector<" << rightType << ",N> right);\n";
// matrix version
sb << "__generic<T : " << op.interface << ", let N : int, let M : int> ";
sb << "[OverloadRank(10)]";
sb << "__intrinsic_op(" << int(op.opCode) << ") matrix<" << resultType << ",N,M> operator" << op.opName << "(matrix<" << leftType << ",N,M> left, matrix<" << rightType << ",N,M> right);\n";
// scalar-vector and scalar-matrix
sb << "__generic<T : " << op.interface << ", let N : int> ";
sb << "[OverloadRank(10)]";
sb << "__intrinsic_op(" << int(op.opCode) << ") vector<" << resultType << ",N> operator" << op.opName << "(" << leftType << " left, vector<" << rightType << ",N> right);\n";
sb << "__generic<T : " << op.interface << ", let N : int, let M : int> ";
sb << "[OverloadRank(10)]";
sb << "__intrinsic_op(" << int(op.opCode) << ") matrix<" << resultType << ",N,M> operator" << op.opName << "(" << leftType << " left, matrix<" << rightType << ",N,M> right);\n";
// vector-scalar and matrix-scalar
sb << "__generic<T : " << op.interface << ", let N : int> ";
sb << "[OverloadRank(10)]";
sb << "__intrinsic_op(" << int(op.opCode) << ") vector<" << resultType << ",N> operator" << op.opName << "(vector<" << leftType << ",N> left, " << rightType << " right);\n";
sb << "__generic<T : " << op.interface << ", let N : int, let M : int> ";
sb << "[OverloadRank(10)]";
sb << "__intrinsic_op(" << int(op.opCode) << ") matrix<" << resultType << ",N,M> operator" << op.opName << "(matrix<" << leftType << ",N,M> left, " << rightType << " right);\n";
}
}
// We will declare the shift operations entirely as generics
// rather than try to handle all the pairings of left-hand
// and right-hand side types.
//
static const struct ShiftOpInfo
{
char const* name;
char const* funcName;
int op;
} kShiftOps[] =
{
{ "<<", "shl", kIROp_Lsh },
{ ">>", "shr", kIROp_Rsh },
};
for(auto info : kShiftOps) {
}}}}
__generic<L: __BuiltinIntegerType, R: __BuiltinIntegerType>
__intrinsic_op($(info.op))
L operator$(info.name)(L left, R right);
__generic<L: __BuiltinIntegerType, R: __BuiltinIntegerType>
__intrinsic_op($(info.op))
L __$(info.funcName)(L left, R right);
__generic<L: __BuiltinIntegerType, R: __BuiltinIntegerType>
[__unsafeForceInlineEarly]
L operator$(info.name)=(in out L left, R right)
{
left = left $(info.name) right;
return left;
}
__generic<L: __BuiltinIntegerType, R: __BuiltinIntegerType, let N : int>
__intrinsic_op($(info.op))
vector<L,N> operator$(info.name)(vector<L,N> left, vector<R,N> right);
__generic<L: __BuiltinIntegerType, R: __BuiltinIntegerType, let N : int>
[__unsafeForceInlineEarly]
vector<L,N> operator$(info.name)=(in out vector<L,N> left, vector<R,N> right)
{
left = left $(info.name) right;
return left;
}
__generic<L: __BuiltinIntegerType, R: __BuiltinIntegerType, let N : int, let M : int>
__intrinsic_op($(info.op))
matrix<L,N,M> operator$(info.name)(matrix<L,N,M> left, matrix<R,N,M> right);
__generic<L: __BuiltinIntegerType, R: __BuiltinIntegerType, let N : int, let M : int, let Layout : int>
[__unsafeForceInlineEarly]
matrix<L, N, M> operator$(info.name)=(in out matrix<L, N, M, Layout> left, matrix<R, N, M> right)
{
left = left $(info.name) right;
return left;
}
__generic<L: __BuiltinIntegerType, R: __BuiltinIntegerType, let N : int>
__intrinsic_op($(info.op))
vector<L,N> operator$(info.name)(L left, vector<R,N> right);
__generic<L: __BuiltinIntegerType, R: __BuiltinIntegerType, let N : int, let M : int>
__intrinsic_op($(info.op))
matrix<L,N,M> operator$(info.name)(L left, matrix<R,N,M> right);
__generic<L: __BuiltinIntegerType, R: __BuiltinIntegerType, let N : int>
__intrinsic_op($(info.op))
vector<L,N> operator$(info.name)(vector<L,N> left, R right);
__generic<L: __BuiltinIntegerType, R: __BuiltinIntegerType, let N : int>
[__unsafeForceInlineEarly]
vector<L, N> operator$(info.name)=(in out vector<L, N> left, R right)
{
left = left $(info.name) right;
return left;
}
__generic<L: __BuiltinIntegerType, R: __BuiltinIntegerType, let N : int, let M : int>
__intrinsic_op($(info.op))
matrix<L,N,M> operator$(info.name)(matrix<L,N,M> left, R right);
__generic<L: __BuiltinIntegerType, R: __BuiltinIntegerType, let N : int, let M : int, let Layout : int>
[__unsafeForceInlineEarly]
matrix<L,N,M> operator$(info.name)=(in out matrix<L,N,M, Layout> left, R right)
{
left = left $(info.name) right;
return left;
}
${{{{
}
static const struct CompoundBinaryOpInfo
{
char const* name;
char const* interface;
} kCompoundBinaryOps[] =
{
{ "+", "__BuiltinArithmeticType" },
{ "-", "__BuiltinArithmeticType" },
{ "*", "__BuiltinArithmeticType" },
{ "/", "__BuiltinArithmeticType" },
{ "%", "__BuiltinIntegerType" },
{ "%", "__BuiltinFloatingPointType" },
{ "&", "__BuiltinLogicalType" },
{ "|", "__BuiltinLogicalType" },
{ "^", "__BuiltinLogicalType" },
};
for( auto op : kCompoundBinaryOps )
{
}}}}
__generic<T : $(op.interface)>
[__unsafeForceInlineEarly]
T operator$(op.name)=(in out T left, T right)
{
left = left $(op.name) right;
return left;
}
__generic<T : $(op.interface), let N : int>
[__unsafeForceInlineEarly]
vector<T,N> operator$(op.name)=(in out vector<T,N> left, vector<T,N> right)
{
left = left $(op.name) right;
return left;
}
__generic<T : $(op.interface), let N : int>
[__unsafeForceInlineEarly]
vector<T,N> operator$(op.name)=(in out vector<T,N> left, T right)
{
left = left $(op.name) right;
return left;
}
__generic<T : $(op.interface), let R : int, let C : int, let Layout : int>
[__unsafeForceInlineEarly]
matrix<T,R,C> operator$(op.name)=(in out matrix<T,R,C,Layout> left, matrix<T,R,C> right)
{
left = left $(op.name) right;
return left;
}
__generic<T : $(op.interface), let R : int, let C : int, let Layout : int>
[__unsafeForceInlineEarly]
matrix<T,R,C> operator$(op.name)=(in out matrix<T,R,C, Layout> left, T right)
{
left = left $(op.name) right;
return left;
}
${{{{
}
}}}}
//@ public:
/// Bit cast between types. `T` and `U` must have the same size.
/// They can be any scalar, vector, matrix, struct or array types.
/// @category conversion
__generic<T, U>
[__unsafeForceInlineEarly]
__intrinsic_op($(kIROp_BitCast))
T bit_cast(U value);
// Create Existential object
__generic<T, U>
[__unsafeForceInlineEarly]
__intrinsic_op($(kIROp_CreateExistentialObject))
T createDynamicObject(uint typeId, U value);
/// Reinterpret type `U` as type `T`. `T` and `U`
/// can be any scalar, vector, matrix, struct or array types.
/// @category conversion
__generic<T, U>
[__unsafeForceInlineEarly]
__intrinsic_op($(kIROp_Reinterpret))
T reinterpret(U value);
/// `bitfieldInsert` inserts the bits least significant bits of `insert` into base at `offset` offset.
/// The returned value will have bits [offset, offset + bits + 1] taken from [0, bits - 1] of `insert`
/// and all other bits taken directly from the corresponding bits of `base`.
__generic<T>
[__readNone]
[__unsafeForceInlineEarly]
__intrinsic_op($(kIROp_BitfieldInsert))
T bitfieldInsert(T base, T insert, uint offset, uint bits);
/// `bitfieldExtract` extracts a subset of the bits of `value` and
/// returns it in the least significant bits of the result. The range of bits extracted is [offset, offset + bits - 1].
__generic<T>
[__readNone]
[__unsafeForceInlineEarly]
__intrinsic_op($(kIROp_BitfieldExtract))
T bitfieldExtract(T value, uint offset, uint bits);
/// Use an otherwise unused value
/// This can be used to silence the warning about returning before initializing an out paramter.
__generic<T>
[__readNone]
[ForceInline]
__intrinsic_op($(kIROp_Unmodified))
void unused(inout T){}
// This can be used to silence the warning about not writing to an inout parameter.
__generic<T>
[__readNone]
[ForceInline]
__intrinsic_op($(kIROp_Unmodified))
void unmodified(out T){}
// Specialized function
/// Given a string returns an integer hash of that string.
__intrinsic_op($(kIROp_GetStringHash))
int getStringHash(String string);
/// Use will produce a syntax error in downstream compiler
/// Useful for testing diagnostics around compilation errors of downstream compiler
/// It 'returns' an int so can be used in expressions without the front end complaining.
[require(cpp_cuda_glsl_hlsl)]
int __SyntaxError()
{
__target_switch
{
case cpp: __intrinsic_asm " @ ";
case cuda: __intrinsic_asm " @ ";
case glsl: __intrinsic_asm " @ ";
case hlsl: __intrinsic_asm " @ ";
}
}
//@ hidden:
/// For downstream compilers that allow sizeof/alignof/offsetof
/// Can't be called in the C/C++ style. Need to use __size_of<some_type>() as opposed to sizeof(some_type).
__generic<T>
[__readNone]
[require(cpp_cuda)]
int __sizeOf()
{
__intrinsic_asm "sizeof($[0])", T;
}
__generic<T>
[__readNone]
[require(cpp_cuda)]
int __sizeOf(T v)
{
__target_switch
{
case cpp: __intrinsic_asm "sizeof($T0)";
case cuda: __intrinsic_asm "sizeof($T0)";
}
}
__generic<T>
[__readNone]
[require(cpp_cuda)]
int __alignOf()
{
__target_switch
{
case cuda :
case cpp :
__intrinsic_asm "alignof($[0])", T;
}
}
__generic<T>
[__readNone]
[require(cpp_cuda)]
int __alignOf(T v)
{
__target_switch
{
case cpp: __intrinsic_asm "alignof($T0)";
case cuda: __intrinsic_asm "alignof($T0)";
}
}
// It would be nice to have offsetof equivalent, but it's not clear how that would work in terms of the Slang language.
// Here we allow calculating the offset of a field in bytes from an *instance* of the type.
__generic<T,F>
[__readNone]
[require(cpp_cuda)]
int __offsetOf(in T t, in F field)
{
__target_switch
{
case cpp: __intrinsic_asm "int(((char*)&($1)) - ((char*)&($0))";
case cuda: __intrinsic_asm "int(((char*)&($1)) - ((char*)&($0)))";
}
}
/// Mark beginning of "interlocked" operations in a fragment shader.
[require(glsl_spirv, GL_ARB_fragment_shader_interlock, fragment)]
__intrinsic_op($(kIROp_BeginFragmentShaderInterlock))
void beginInvocationInterlock();
/// Mark end of "interlocked" operations in a fragment shader.
[require(glsl_spirv, GL_ARB_fragment_shader_interlock, fragment)]
__intrinsic_op($(kIROp_EndFragmentShaderInterlock))
void endInvocationInterlock();
// Operators to apply to `enum` types
//@ hidden:
__generic<E : __EnumType>
__intrinsic_op($(kIROp_Eql))
bool operator==(E left, E right);
__generic<E : __EnumType>
__intrinsic_op($(kIROp_Neq))
bool operator!=(E left, E right);
//@ hidden:
// public interfaces for generic arithmetic types.
__generic<T : IComparable>
[__unsafeForceInlineEarly]
[OverloadRank(-10)]
bool operator<(T v0, T v1)
{
return v0.lessThan(v1);
}
__generic<T : IComparable>
[__unsafeForceInlineEarly]
[OverloadRank(-10)]
bool operator>(T v0, T v1)
{
return v1.lessThan(v0);
}
__generic<T : IComparable>
[__unsafeForceInlineEarly]
[OverloadRank(-10)]
bool operator ==(T v0, T v1)
{
return v0.equals(v1);
}
__generic<T : IComparable>
[__unsafeForceInlineEarly]
[OverloadRank(-10)]
bool operator >=(T v0, T v1)
{
return v1.lessThanOrEquals(v0);
}
__generic<T : IComparable>
[__unsafeForceInlineEarly]
[OverloadRank(-10)]
bool operator <=(T v0, T v1)
{
return v0.lessThanOrEquals(v1);
}
__generic<T : IComparable>
[__unsafeForceInlineEarly]
[OverloadRank(-10)]
bool operator !=(T v0, T v1)
{
return !v0.equals(v1);
}
${{{{
const char* arithmeticInterfaces[] = {"IArithmetic", "IFloat"};
const char* attribs[] = {"", "[TreatAsDifferentiable]"};
for (Index i = 0; i < 2; i++) {
const auto interfaceName = arithmeticInterfaces[i];
const auto attrib = attribs[i];
Index overloadRank = i - 3;
}}}}
$(attrib)
__generic<T : $(interfaceName)>
[__unsafeForceInlineEarly]
[OverloadRank($(overloadRank))]
T operator +(T v0, T v1)
{
return v0.add(v1);
}
$(attrib)
__generic<T : $(interfaceName)>
[__unsafeForceInlineEarly]
[OverloadRank($(overloadRank))]
T operator -(T v0, T v1)
{
return v0.sub(v1);
}
$(attrib)
__generic<T : $(interfaceName)>
[__unsafeForceInlineEarly]
[OverloadRank($(overloadRank))]
T operator *(T v0, T v1)
{
return v0.mul(v1);
}
$(attrib)
__generic<T : $(interfaceName)>
[__unsafeForceInlineEarly]
[OverloadRank($(overloadRank))]
T operator /(T v0, T v1)
{
return v0.div(v1);
}
$(attrib)
__generic<T : $(interfaceName)>
[__unsafeForceInlineEarly]
[OverloadRank($(overloadRank))]
T operator %(T v0, T v1)
{
return v0.mod(v1);
}
$(attrib)
__generic<T : $(interfaceName)>
[__unsafeForceInlineEarly]
[OverloadRank($(overloadRank))]
__prefix T operator -(T v0)
{
return v0.neg();
}
${{{{
} // foreach ["IArithmetic", "IFloat"]
}}}}
__generic<T : ILogical>
[__unsafeForceInlineEarly]
[OverloadRank(-10)]
T operator &(T v0, T v1)
{
return v0.bitAnd(v1);
}
__generic<T : ILogical>
[__unsafeForceInlineEarly]
[OverloadRank(-10)]
T operator &&(T v0, T v1)
{
return v0.and(v1);
}
[__unsafeForceInlineEarly]
[OverloadRank(-10)]
bool and(bool v0, bool v1)
{
return __and(v0, v1);
}
[__unsafeForceInlineEarly]
[OverloadRank(-10)]
__intrinsic_op($(kIROp_And))
vector<bool, N> and<let N : int>(vector<bool, N> v0, vector<bool, N> v1);
[__unsafeForceInlineEarly]
[OverloadRank(-10)]
vector<bool, N> and<let N : int>(bool b, vector<bool, N> v)
{
return and(vector<bool, N>(b), v);
}
[__unsafeForceInlineEarly]
[OverloadRank(-10)]
vector<bool, N> and<let N : int>(vector<bool, N> v, bool b)
{
return and(v, vector<bool, N>(b));
}
[__unsafeForceInlineEarly]
[OverloadRank(-10)]
bool or(bool v0, bool v1)
{
return __or(v0, v1);
}
[__unsafeForceInlineEarly]
[OverloadRank(-10)]
__intrinsic_op($(kIROp_Or))
vector<bool, N> or<let N : int>(vector<bool, N> v0, vector<bool, N> v1);
[__unsafeForceInlineEarly]
[OverloadRank(-10)]
vector<bool, N> or<let N : int>(bool b, vector<bool, N> v)
{
return or(vector<bool, N>(b), v);
}
[__unsafeForceInlineEarly]
[OverloadRank(-10)]
vector<bool, N> or<let N : int>(vector<bool, N> v, bool b)
{
return or(v, vector<bool, N>(b));
}
__generic<T : ILogical>
[__unsafeForceInlineEarly]
[OverloadRank(-10)]
T operator |(T v0, T v1)
{
return v0.bitOr(v1);
}
__generic<T : ILogical>
[__unsafeForceInlineEarly]
[OverloadRank(-10)]
T operator ||(T v0, T v1)
{
return v0.or(v1);
}
__generic<T : ILogical>
[__unsafeForceInlineEarly]
[OverloadRank(-10)]
T operator ^(T v0, T v1)
{
return v0.bitXor(v1);
}
__generic<T : ILogical>
[__unsafeForceInlineEarly]
[OverloadRank(-10)]
__prefix T operator ~(T v0)
{
return v0.bitNot();
}
__generic<T : ILogical>
[__unsafeForceInlineEarly]
[OverloadRank(-10)]
__prefix T operator !(T v0)
{
return v0.not();
}
// The operator overloads defined above already allows Enum types to be used
// in logical operators, but we still provide overloads for __EnumTypes and map
// them directly to intrinsic op to allow constant propagation at AST level to
// work on enum types.
__generic<T : __EnumType>
[__unsafeForceInlineEarly]
__intrinsic_op($(kIROp_BitAnd))
T operator &(T v0, T v1);
__generic<T : __EnumType>
[__unsafeForceInlineEarly]
__intrinsic_op($(kIROp_BitOr))
T operator |(T v0, T v1);
__generic<T : __EnumType>
[__unsafeForceInlineEarly]
__intrinsic_op($(kIROp_BitNot))
__prefix T operator ~(T v0);
// IR level type traits.
__generic<T>
__intrinsic_op($(kIROp_Undefined))
T __declVal();
__generic<T>
__intrinsic_op($(kIROp_DefaultConstruct))
T __default();
__generic<T, U>
__intrinsic_op($(kIROp_TypeEquals))
bool __type_equals_impl(T t, U u);
__generic<T, U>
[__unsafeForceInlineEarly]
bool __type_equals(T t, U u)
{
return __type_equals_impl(__declVal<T>(), __declVal<U>());
}
__generic<T, U>
[__unsafeForceInlineEarly]
bool __type_equals()
{
return __type_equals_impl(__declVal<T>(), __declVal<U>());
}
__generic<T>
__intrinsic_op($(kIROp_IsBool))
bool __isBool_impl(T t);
__generic<T>
[__unsafeForceInlineEarly]
bool __isBool()
{
return __isBool_impl(__declVal<T>());
}
__generic<T>
__intrinsic_op($(kIROp_IsInt))
bool __isInt_impl(T t);
__generic<T>
[__unsafeForceInlineEarly]
bool __isInt()
{
return __isInt_impl(__declVal<T>());
}
__generic<T>
__intrinsic_op($(kIROp_IsFloat))
bool __isFloat_impl(T t);
__generic<T>
__intrinsic_op($(kIROp_IsHalf))
bool __isHalf_impl(T t);
__generic<T>
[__unsafeForceInlineEarly]
bool __isFloat()
{
return __isFloat_impl(__declVal<T>());
}
__generic<T>
[__unsafeForceInlineEarly]
bool __isHalf()
{
return __isHalf_impl(__declVal<T>());
}
__generic<T>
__intrinsic_op($(kIROp_IsUnsignedInt))
bool __isUnsignedInt_impl(T t);
__generic<T>
[__unsafeForceInlineEarly]
bool __isUnsignedInt()
{
return __isUnsignedInt_impl(__declVal<T>());
}
__generic<T>
__intrinsic_op($(kIROp_IsSignedInt))
bool __isSignedInt_impl(T t);
__generic<T>
[__unsafeForceInlineEarly]
bool __isSignedInt()
{
return __isSignedInt_impl(__declVal<T>());
}
__generic<T>
__intrinsic_op($(kIROp_IsVector))
bool __isVector_impl(T t);
__generic<T>
[__unsafeForceInlineEarly]
bool __isVector()
{
return __isVector_impl(__declVal<T>());
}
__generic<T>
__intrinsic_op($(kIROp_GetNaturalStride))
int __naturalStrideOf_impl(T v);
__generic<T>
[__unsafeForceInlineEarly]
int __naturalStrideOf()
{
return __naturalStrideOf_impl(__declVal<T>());
}
__intrinsic_op($(kIROp_AlignOf))
int __alignOf_intrinsic_impl<T>(T t);
[ForceInline]
int __alignOf_intrinsic<T>()
{
return __alignOf_intrinsic_impl<T>(__default<T>());
}
[__unsafeForceInlineEarly]
int32_t __elemToByteOffset<T>(int32_t elemOffset)
{
return elemOffset * __naturalStrideOf<T>();
}
[__unsafeForceInlineEarly]
int32_t __byteToElemOffset<T>(int32_t byteOffset)
{
return byteOffset / __naturalStrideOf<T>();
}
__intrinsic_op($(kIROp_TreatAsDynamicUniform))
T asDynamicUniform<T>(T v);
__generic<T>
__intrinsic_op( $(kIROp_GetLegalizedSPIRVGlobalParamAddr))
__Addr<T> __getLegalizedSPIRVGlobalParamAddr(T val);
__intrinsic_op($(kIROp_RequireComputeDerivative))
void __requireComputeDerivative();
__intrinsic_op($(kIROp_RequireMaximallyReconverges))
void __requireMaximallyReconverges();
__intrinsic_op($(kIROp_RequireQuadDerivatives))
void __requireQuadDerivatives();
//@ public:
/// @category misc_types
enum MemoryOrder
{
/// No memory operation ordering constraints
Relaxed = $(kIRMemoryOrder_Relaxed),
/// Ensures that all subsequent memory operations in the same thread are not reordered before it
Acquire = $(kIRMemoryOrder_Acquire),
/// Ensures that all prior memory operations in the same thread are not reordered after it
Release = $(kIRMemoryOrder_Release),
/// Combines both acquire and release semantics
AcquireRelease = $(kIRMemoryOrder_AcquireRelease),
/// Provides the strongest ordering: total order exists between all SeqCst operations
SeqCst = $(kIRMemoryOrder_SeqCst),
}
/// Represents types that can be used in any atomic operations.
/// Implemented by builtin scalar types: `int`, `uint`, `int64_t`, `uint64_t`, `int8_t`, `uint8_t`, `int16_t`, `uint16_t`, `float`, `double` and `half`.
[sealed] interface IAtomicable {}
/// Represents types that can be used in atomic arithmetic operations.
/// Implemented by builtin scalar types: `int`, `uint`, `int64_t`, `uint64_t`, `int8_t`, `uint8_t`, `int16_t`, `uint16_t`, `float`, `double` and `half`.
[sealed] interface IArithmeticAtomicable : IAtomicable, IArithmetic {}
/// Represents types that can be used in atomic bit operations.
/// Implemented by builtin scalar types: `int`, `uint`, `int64_t`, `uint64_t`, `int8_t`, `uint8_t`, `int16_t`, `uint16_t`.
[sealed] interface IBitAtomicable : IArithmeticAtomicable, IInteger {}
extension int : IBitAtomicable {}
extension uint : IBitAtomicable {}
extension int64_t : IBitAtomicable {}
extension uint64_t : IBitAtomicable {}
extension double : IArithmeticAtomicable {}
extension float : IArithmeticAtomicable {}
extension half : IArithmeticAtomicable {}
/// A wrapper for `IAtomicable` types to introduce atomic load and store
/// operations. Values of this type are to be stored in buffers or groupshared
/// memory.
///
/// All operations take a `MemoryOrder` parameter which influenced the
/// semantics of the performed operation
///
/// All operations take place at the device scope.
///
/// Operators `+=`, `-=`, `&=`, `|=`, `^=`, `++`, `--` are overloaded to
/// operate on `Atomic<T>`.
__magic_type(AtomicType)
__intrinsic_type($(kIROp_AtomicType))
[require(cuda_glsl_hlsl_metal_spirv_wgsl)]
struct Atomic<T : IAtomicable>
{
/// Atomically load the stored `T` value
__intrinsic_op($(kIROp_AtomicLoad))
[__ref] T load(MemoryOrder order = MemoryOrder.Relaxed);
/// Atomically store a new `T` value
__intrinsic_op($(kIROp_AtomicStore))
[__ref] void store(T newValue, MemoryOrder order = MemoryOrder.Relaxed);
/// Atomically replace the stored `T` value with a new `T` value and return
/// replaced value
__intrinsic_op($(kIROp_AtomicExchange))
[__ref] T exchange(T newValue, MemoryOrder order = MemoryOrder.Relaxed); // returns old value
/// Atomically replace and return the stored `T` value with a new `T` value
/// only if the stored value is equal to the specified comparison value.
///
/// If the comparison value is equal to the stored value, then the
/// `successOrder` `MemoryOrder` is used, otherwise the `failOrder`
/// `MemoryOrder` is used.
///
/// `successOrder` must be at least as strong as `failOrder`
///
/// `failOrder` must not be `MemoryOrder.Release` or `MemoryOrder.AcquireRelease`
__intrinsic_op($(kIROp_AtomicCompareExchange))
[__ref] T compareExchange(
T compareValue,
T newValue,
MemoryOrder successOrder = MemoryOrder.Relaxed,
MemoryOrder failOrder = MemoryOrder.Relaxed);
}
/// These additional members are only available when `T` conforms to `IArithmeticAtomicable`.
extension<T : IArithmeticAtomicable> Atomic<T>
{
/// Atomically adds the given value to the stored value and returns the
/// original stored value.
__intrinsic_op($(kIROp_AtomicAdd))
[__ref] T add(T value, MemoryOrder order = MemoryOrder.Relaxed);
/// Atomically subtracts the given value from the stored value and returns
/// the original stored value.
__intrinsic_op($(kIROp_AtomicSub))
[__ref] T sub(T value, MemoryOrder order = MemoryOrder.Relaxed);
/// Atomically computes the maximum of the stored value and the given
/// value, storing the result and returning the original stored value.
__intrinsic_op($(kIROp_AtomicMax))
[__ref] T max(T value, MemoryOrder order = MemoryOrder.Relaxed);
/// Atomically computes the minimum of the stored value and the given
/// value, storing the result and returning the original stored value.
__intrinsic_op($(kIROp_AtomicMin))
[__ref] T min(T value, MemoryOrder order = MemoryOrder.Relaxed);
}
/// These additional members are only available when `T` conforms to `IBitAtomicable`.
extension<T : IBitAtomicable> Atomic<T>
{
/// Atomically performs a bitwise AND operation between the stored value
/// and the given value, storing the result and returning the original
/// stored value.
__intrinsic_op($(kIROp_AtomicAnd))
[__ref] T and(T value, MemoryOrder order = MemoryOrder.Relaxed);
/// Atomically performs a bitwise OR operation between the stored value and
/// the given value, storing the result and returning the original stored
/// value.
__intrinsic_op($(kIROp_AtomicOr))
[__ref] T or(T value, MemoryOrder order = MemoryOrder.Relaxed);
/// Atomically performs a bitwise XOR operation between the stored value
/// and the given value, storing the result and returning the original
/// stored value.
__intrinsic_op($(kIROp_AtomicXor))
[__ref] T xor(T value, MemoryOrder order = MemoryOrder.Relaxed);
/// Atomically increments the stored value and returns the original stored
/// value.
__intrinsic_op($(kIROp_AtomicInc))
[__ref] T increment(MemoryOrder order = MemoryOrder.Relaxed);
/// Atomically decrements the stored value and returns the original stored
/// value.
__intrinsic_op($(kIROp_AtomicDec))
[__ref] T decrement(MemoryOrder order = MemoryOrder.Relaxed);
}
//@ hidden:
__generic<T : IArithmeticAtomicable>
[ForceInline]
T operator +=(__ref Atomic<T> v, T value)
{
return v.add(value) + value;
}
__generic<T : IArithmeticAtomicable>
[ForceInline]
T operator -=(__ref Atomic<T> v, T value)
{
return v.sub(value) - value;
}
__generic<T : IBitAtomicable>
[ForceInline]
T operator &=(__ref Atomic<T> v, T value)
{
return v.and(value) & value;
}
__generic<T : IBitAtomicable>
[ForceInline]
T operator |=(__ref Atomic<T> v, T value)
{
return v.or(value) | value;
}
__generic<T : IBitAtomicable>
[ForceInline]
T operator ^=(__ref Atomic<T> v, T value)
{
return v.xor(value) ^ value;
}
__generic<T : IBitAtomicable>
[ForceInline]
__prefix T operator ++(__ref Atomic<T> v)
{
return v.increment() + T(1);
}
__generic<T : IBitAtomicable>
[ForceInline]
__postfix T operator ++(__ref Atomic<T> v)
{
return v.increment();
}
__generic<T : IBitAtomicable>
[ForceInline]
__prefix T operator --(__ref Atomic<T> v)
{
return v.decrement() - T(1);
}
__generic<T : IBitAtomicable>
[ForceInline]
__postfix T operator --(__ref Atomic<T> v)
{
return v.decrement();
}
// Binding Attributes
//@public:
/// Declare the Vulkan binding location of a global shader variable.
/// @param binding The binding location.
/// @param set The descriptor set index of the binding.
__attributeTarget(DeclBase)
attribute_syntax [vk_binding(binding: int, set: int = 0)] : GLSLBindingAttribute;
/// Declare the Vulkan binding location of a global shader variable.
/// @param binding The binding location.
/// @param set The descriptor set index of the binding.
__attributeTarget(DeclBase)
attribute_syntax [gl_binding(binding: int, set: int = 0)] : GLSLBindingAttribute;
/// Mark a global variable as a Vulkan shader record.
__attributeTarget(VarDeclBase)
attribute_syntax [vk_shader_record] : ShaderRecordAttribute;
/// Mark a global variable as a Vulkan shader record.
__attributeTarget(VarDeclBase)
attribute_syntax [shader_record] : ShaderRecordAttribute;
/// Mark a global variable as a Vulkan push constant.
__attributeTarget(VarDeclBase)
attribute_syntax [vk_push_constant] : PushConstantAttribute;
/// Mark a global variable as a Vulkan push constant.
__attributeTarget(VarDeclBase)
attribute_syntax [push_constant] : PushConstantAttribute;
/// Mark a global variable as a Vulkan specialization constant.
__attributeTarget(VarDeclBase)
attribute_syntax[vk_specialization_constant] : SpecializationConstantAttribute;
/// Mark a global variable as a Vulkan specialization constant.
__attributeTarget(VarDeclBase)
attribute_syntax[SpecializationConstant] : SpecializationConstantAttribute;
/// Mark a global variable as a Vulkan specialization constant.
/// @param location The index of the specialization constant.
__attributeTarget(VarDeclBase)
attribute_syntax[vk_constant_id(location: int)] : VkConstantIdAttribute;
/// Declare the Vulkan location of a global variable.
__attributeTarget(VarDeclBase)
attribute_syntax [vk_location(location : int)] : GLSLLocationAttribute;
/// Declare the Vulkan binding index of a global variable.
__attributeTarget(VarDeclBase)
attribute_syntax [vk_index(index : int)] : GLSLIndexAttribute;
/// Declare offset for a struct field. Applies to all kinds of structs when targeting Vulkan.
/// Applies only to structs that are directly used as interface blocks (such as push constants and uniforms)
/// when targeting GLSL, as GLSL does not support the `offset` qualifier on regular structs.
/// This attribute has no effect on other targets.
__attributeTarget(VarDeclBase)
attribute_syntax [vk_offset(index: int)] : VkStructOffsetAttribute;
/// @deprecated
/// Use `spirv_asm` instead for inline SPIR-V assembly.
__attributeTarget(FuncDecl)
attribute_syntax [vk_spirv_instruction(op : int, set : String = "")] : SPIRVInstructionOpAttribute;
/// Declare the Vulkan input attachment index of a global variable.
__attributeTarget(VarDeclBase)
attribute_syntax [vk_input_attachment_index(location : int)] : GLSLInputAttachmentIndexLayoutAttribute;
/// @internal
__attributeTarget(FuncDecl)
attribute_syntax [spv_target_env_1_3] : SPIRVTargetEnv13Attribute;
/// @internal
__attributeTarget(VarDeclBase)
attribute_syntax [disable_array_flattening] : DisableArrayFlatteningAttribute;
/// Marks a enum type as an unscoped enum. The enum cases of an unscoped enum are not scoped within the enum type, and can be
/// referenced directly without the enum type name.
__attributeTarget(EnumDecl)
attribute_syntax [UnscopedEnum] : UnscopedEnumAttribute;
/// Marks a enum type as a bit flag enum. Bit flag enums will have their enum cases assigned to powers of 2 instead of consecutive integers.
///
__attributeTarget(EnumDecl)
attribute_syntax[Flags] : FlagsAttribute;
// Statement Attributes
/// A hint to the downstream compiler to unroll the loop until the specified number of iterations reached.
/// This attribute does not affect Slang compiler's behavior.
/// To unroll a loop in the Slang compiler before emitting target code, use the `[ForceUnroll]` attribute.
__attributeTarget(LoopStmt)
attribute_syntax [unroll(count: int = 0)] : UnrollAttribute;
/// Instructs the Slang compiler to unroll the loop until the specified number of iterations before
/// emiting targett code.
/// @param count The maximun number of iterations to unroll the loop.
__attributeTarget(LoopStmt)
attribute_syntax [ForceUnroll(count: int = 0)] : ForceUnrollAttribute;
/// A hint to the downstream compiler to preserve a loop.
__attributeTarget(LoopStmt)
attribute_syntax [loop] : LoopAttribute;
/// Used on loop statements as a hint to the downstream compiler to perform less aggressive optimization on the loop
/// in favor of faster compilation time.
/// This attribute has no effect on targets other than HLSL.
__attributeTarget(LoopStmt)
attribute_syntax [fastopt] : FastOptAttribute;
/// A hint to the downstream compiler that UAV conditions are allowed in the loop.
/// This attribute has no effect on targets other than HLSL.
/// @deprecated
__attributeTarget(LoopStmt)
attribute_syntax [allow_uav_condition] : AllowUAVConditionAttribute;
__attributeTarget(LoopStmt)
attribute_syntax [MaxIters(count)] : MaxItersAttribute;
/// A hint to the downstream compiler to flatten an if statement.
__attributeTarget(IfStmt)
attribute_syntax [flatten] : FlattenAttribute;
/// A hint to the downstream compiler to preserve the branching behavior of an if statement.
__attributeTarget(IfStmt)
__attributeTarget(SwitchStmt)
attribute_syntax [branch] : BranchAttribute;
/// A hint to the downstream compiler to preserve the `switch` statement as is.
/// This attribute has no effect on targets other than HLSL.
__attributeTarget(SwitchStmt)
attribute_syntax [forcecase] : ForceCaseAttribute;
/// A hint to the downstream compiler to translate the `switch` statement into subroutine calls.
/// This attribute has no effect on targets other than HLSL.
__attributeTarget(SwitchStmt)
attribute_syntax [call] : CallAttribute;
// Entry-point Attributes
// All Stages
/// Marks a function as a shader entry point.
/// @param stage The stage of the shader. Must be one of "vertex", "fragment", "compute", "geometry", "hull", "domain", "raygeneration",
// "intersection", "anyhit", "closesthit", "miss", "callable", "task", "mesh".
__attributeTarget(FuncDecl)
attribute_syntax [shader(stage)] : EntryPointAttribute;
/// Marks a function as a shader entry point.
/// @param stage The stage of the shader. Must be one of "vertex", "fragment", "compute", "geometry", "hull", "domain", "raygeneration",
// "intersection", "anyhit", "closesthit", "miss", "callable", "task", "mesh".
__attributeTarget(FuncDecl)
attribute_syntax [Shader(stage)] : EntryPointAttribute;
// Hull Shader
/// Used on an hull shader entrypoint to declare the upperbound of the tessellation factor that the hull shader can return.
/// @param factor The maximum tessellation factor the hull shader can return.
__attributeTarget(FuncDecl)
attribute_syntax [maxtessfactor(factor: float)] : MaxTessFactorAttribute;
/// Used on an hull shader entrypoint to declare the number of control points the hull shader will produce per thread.
/// @param count The number of control points the hull shader will produce per thread.
/// @remarks The attribute indicates be the number of times the hull shader function will be executed.
__attributeTarget(FuncDecl)
attribute_syntax [outputcontrolpoints(count: int)] : OutputControlPointsAttribute;
/// Used on an hull shader entrypoint to declare the output primitive type of the tessellator.
/// @param topology The output primitive type, must be one of "point", "line", "triangle_cw", and "triangle_ccw".
__attributeTarget(FuncDecl)
attribute_syntax [outputtopology(topology)] : OutputTopologyAttribute;
/// Used on an hull shader entrypoint to specify the patch type used by the hull shader.
/// @param mode The patch type used by the hull shader. Valid values are "tri", "quad" and "isoline".
__attributeTarget(FuncDecl)
attribute_syntax [partitioning(mode)] : PartitioningAttribute;
/// Used on a hull shader entrypoint to specify the associated function that computes the patch constant data.
/// @param name The name of the function (in string literal) that computes the patch constant data.
__attributeTarget(FuncDecl)
attribute_syntax [patchconstantfunc(name)] : PatchConstantFuncAttribute;
// Hull/Domain Shader
/// Used on an hull shader entrypoint to specify the patch type used by the hull shader.
/// @param patchType The patch type used by the hull shader. Valid values are "tri", "quad" and "isoline".
__attributeTarget(FuncDecl)
attribute_syntax [domain(patchType)] : DomainAttribute;
// Geometry Shader
/// Used on a geometry shader entry point to specify the maximum number of vertices that the geometry shader can output.
/// @param count The maximum number of vertices that the geometry shader can output.
__attributeTarget(FuncDecl)
attribute_syntax [maxvertexcount(count: int)] : MaxVertexCountAttribute;
/// Used on a geometry shader entry point to specify the number of instances to execute for each input primitive.
/// @param count The number of instances to execute for each input primitive.
/// @remarks When using this attribute, a geometry shader can declare a parameter with `SV_GSInstanceID` semantic to get the instance index.
__attributeTarget(FuncDecl)
attribute_syntax [instance(count: int)] : InstanceAttribute;
// Fragment ("Pixel") Shader
/// Used on a fragment shader entry point to specify that early depth stencil testing can be enabled.
__attributeTarget(FuncDecl)
attribute_syntax [earlydepthstencil] : EarlyDepthStencilAttribute;
// Compute Shader
/// Specifies the size of the thread group a compute shader.
/// @param x The number of threads in the x dimension of a thread group.
/// @param y The number of threads in the y dimension of a thread group.
/// @param z The number of threads in the z dimension of a thread group.
__attributeTarget(FuncDecl)
attribute_syntax [numthreads(x: int, y: int = 1, z: int = 1)] : NumThreadsAttribute;
/// Specifies the size of the thread group a compute shader.
/// @param x The number of threads in the x dimension of a thread group.
/// @param y The number of threads in the y dimension of a thread group.
/// @param z The number of threads in the z dimension of a thread group.
__attributeTarget(FuncDecl)
attribute_syntax [NumThreads(x: int, y: int = 1, z: int = 1)] : NumThreadsAttribute;
/// Indicate a compute shader entry point is only compatible with the specified wave size.
/// @param numLanes The wave size this shader entrypoint is compatible with. Must be one of 4, 8, 16, 32, 64, 128.
__attributeTarget(FuncDecl)
attribute_syntax [WaveSize(numLanes: int)] : WaveSizeAttribute;
//@hidden:
__attributeTarget(VarDeclBase)
attribute_syntax [__vulkanRayPayload(location : int = -1)] : VulkanRayPayloadAttribute;
__attributeTarget(VarDeclBase)
attribute_syntax [__vulkanCallablePayload(location : int = -1)] : VulkanCallablePayloadAttribute;
__attributeTarget(VarDeclBase)
attribute_syntax [__vulkanHitObjectAttributes(location : int = -1)] : VulkanHitObjectAttributesAttribute;
__attributeTarget(VarDeclBase)
attribute_syntax [__vulkanHitAttributes] : VulkanHitAttributesAttribute;
//@public:
/// Mark a function or a property or subscript accessor as mutating. A mutating function receives the implicit `this` parameter
/// as an `inout` parameter, so that mutations to members access from `this` argument will be visible to the caller.
///
/// @remarks
/// By default, Slang treats all member functions as non-mutating. For example, consider the following function:
/// ```csharp
/// struct S
/// {
/// int x;
/// void foo()
/// {
/// x = 1; // error: `x` is not an l-value.
/// }
/// }
///
/// ```
/// The line `x = 1` will lead to a compile time error because by-default, all member methods in Slang are non-mutating. To
/// allow `foo` to modify `x`, you can use `[mutating]` to mark the function as such:
/// ```csharp
/// struct S
/// {
/// int x;
/// [mutating]
/// void foo()
/// {
/// x = 1; // ok
/// }
/// }
/// ```
/// @see `[nonmutating]`.
///
__attributeTarget(FunctionDeclBase)
attribute_syntax [mutating] : MutatingAttribute;
/// Marks a function or a property and subscript accessor as non-mutating. A non-mutating function receives the implicit `this` parameter
/// as an `in` parameter, so mutations to members accessed from `this` argument will be prohibited by the compiler.
/// @remarks
/// Member functions of a type are non-mutating by default, so this attribute is not necessary in most cases.
/// However, the `set` accessor of a property or subscript is mutating by default, and you can use `[nonmutating]` to mark it as non-mutating.
/// For example:
/// ```csharp
/// struct S
/// {
/// int* ptr_x;
/// property x : int
/// {
/// get { return *ptr_x; }
///
/// [nonmutating]
/// set { *ptr_x = value; }
/// }
/// }
/// uniform S s; // `s` is not mutable.
/// void test() { s.x = 1; } // OK, because the `set` accessor is non-mutating.
/// ```
/// In the above example, the property `x` reads and writes to a memory location pointed to by `ptr_x`. Therefore, the `set` accessor is not actually
/// modifying any field of `S`, and does not need to take `this` as an `inout` parameter. Using `[nonmutating]` here on the set accessor will allow
/// it to be called with a non-mutating value of `S`.
/// @see `[mutating]`.
///
__attributeTarget(AccessorDecl)
attribute_syntax [nonmutating] : NonmutatingAttribute;
/// @internal
/// Marks a member function to make `this` argument passed by const reference.
__attributeTarget(FunctionDeclBase)
attribute_syntax [constref] : ConstRefAttribute;
/// @internal
/// Marks a member function to make `this` argument passed by reference.
__attributeTarget(FunctionDeclBase)
attribute_syntax [__ref] : RefAttribute;
/// @internal
/// Indicates that a function computes its result as a function of its arguments without loading/storing any memory or other state.
///
/// This is equivalent to the LLVM `readnone` function attribute.
__attributeTarget(FunctionDeclBase)
attribute_syntax [__readNone] : ReadNoneAttribute;
/// Represents the applicable target for an attribute.
enum _AttributeTargets
{
Struct = $((int) UserDefinedAttributeTargets::Struct), /// Struct types.
Var = $((int) UserDefinedAttributeTargets::Var), /// Global and local variables and constants.
Function = $((int) UserDefinedAttributeTargets::Function), /// Functions or member functions.
Param = $((int) UserDefinedAttributeTargets::Param), /// Function parameters.
};
/// Mark a struct type as a user defined attribute type.
/// @param target The type of declarations to which the attribute can be applied.
__attributeTarget(StructDecl)
attribute_syntax [__AttributeUsage(target : _AttributeTargets)] : AttributeUsageAttribute;
/// Specify the storage format of a read-write texture. Can only be used on a texture typed struct field or global parameter.
/// @param format The storage format of the texture.
/// @see Please refer to `_Texture` for a complete list of allowed format strings.
__attributeTarget(VarDeclBase)
attribute_syntax [format(format : String)] : FormatAttribute;
/// Specify the storage format of a read-write texture. Can only be used on a texture typed struct field or global parameter.
/// This is an alias of the `[format]` attribute.
/// @param format The storage format of the texture.
/// @see Please refer to `_Texture` for a complete list of allowed format strings.
__attributeTarget(VarDeclBase)
attribute_syntax [vk_image_format(format : String)] : FormatAttribute;
__attributeTarget(Decl)
attribute_syntax [allow(diagnostic: String)] : AllowAttribute;
/// Mark declaration to require a specific target capability.
/// @param capability The required capability.
__attributeTarget(Decl)
attribute_syntax [require(capability)] : RequireCapabilityAttribute;
// Linking
__attributeTarget(Decl)
attribute_syntax [__extern] : ExternAttribute;
__attributeTarget(FunctionDeclBase)
attribute_syntax [__unsafeForceInlineEarly] : UnsafeForceInlineEarlyAttribute;
/// Perform inlining of the function at the call site during Slang compilation.
/// @remarks By default Slang does not inline user defined functions, and will preserve the function call hierarchy in the generated code.
/// Use this attribute on a function to force the Slang compiler to inline the function before emitting target code.
__attributeTarget(FunctionDeclBase)
attribute_syntax [ForceInline] : ForceInlineAttribute;
/// @internal
/// Specify the overload rank of a function for overload resolution.
__attributeTarget(FunctionDeclBase)
attribute_syntax [OverloadRank] : OverloadRankAttribute;
/// @experimental
/// Mark a function as imported from a DLL. Valid only on CPU host targets.
__attributeTarget(FuncDecl)
attribute_syntax [DllImport(modulePath: String)] : DllImportAttribute;
/// @experimental
/// Mark a function to be exported as a DLL symbol. Valid only on CPU host targets.
__attributeTarget(FuncDecl)
attribute_syntax [DllExport] : DllExportAttribute;
/// @experimental
/// Mark a function as a pytorch kernel entrypoint.
__attributeTarget(FuncDecl)
attribute_syntax [TorchEntryPoint] : TorchEntryPointAttribute;
/// @experimental
/// Mark a function for export as a CUDA device function. Valid only on CUDA target.
__attributeTarget(FuncDecl)
attribute_syntax [CudaDeviceExport] : CudaDeviceExportAttribute;
/// @experimental
/// Mark a function for export as a CUDA host function. Valid only on CUDA target.
__attributeTarget(FuncDecl)
attribute_syntax [CudaHost] : CudaHostAttribute;
/// @experimental
/// Mark a function for export as a CUDA kernel function. Valid only on CUDA target.
__attributeTarget(FuncDecl)
attribute_syntax [CudaKernel] : CudaKernelAttribute;
/// @experimental
/// Mark a function for export as a CUDA device function. Valid only on CUDA target.
__attributeTarget(FuncDecl)
attribute_syntax [CUDADeviceExport] : CudaDeviceExportAttribute;
/// @experimental
/// Mark a function for export as a CUDA host function. Valid only on CUDA target.
__attributeTarget(FuncDecl)
attribute_syntax [CUDAHost] : CudaHostAttribute;
/// @experimental
/// Mark a function for export as a CUDA kernel function. Valid only on CUDA target.
__attributeTarget(FuncDecl)
attribute_syntax [CUDAKernel] : CudaKernelAttribute;
/// @experimental
/// Mark a function for automatic pytorch binding.
__attributeTarget(FuncDecl)
attribute_syntax [AutoPyBindCUDA] : AutoPyBindCudaAttribute;
/// @experimental
/// Mark a type for export to slang-torch.
__attributeTarget(AggTypeDecl)
attribute_syntax [PyExport(name: String)] : PyExportAttribute;
/// @experimental
/// Mark an interface as a COM interface. Valid only on CPU target.
/// @param guid The GUID of the COM interface.
__attributeTarget(InterfaceDecl)
attribute_syntax [COM(guid: String)] : ComInterfaceAttribute;
// Inheritance Control
/// @experimental
/// Mark a type as sealed, preventing it from being inherited from or implemented by types defined in other modules.
/// @see `[open]`.
__attributeTarget(AggTypeDecl)
attribute_syntax [sealed] : SealedAttribute;
/// @experimental
/// Mark a type as open, allowing it from being inherited from or implemented by types defined in other modules.
/// This is the default behavior.
/// @see `[sealed]`.
__attributeTarget(AggTypeDecl)
attribute_syntax [open] : OpenAttribute;
/// @experimental
/// Mark an interface type to allow dynmaic dispatch, and declare the maximum size in bytes that an implementation type
/// of the interface can have.
__attributeTarget(InterfaceDecl)
attribute_syntax [anyValueSize(size:int)] : AnyValueSizeAttribute;
/// @experimental
/// Mark an interface type for specialization only. Such interface types cannot be used for dynamic dispatch.
__attributeTarget(InterfaceDecl)
attribute_syntax [Specialize] : SpecializeAttribute;
/// @internal
/// Marks a declaration as a builtin declaration.
__attributeTarget(DeclBase)
attribute_syntax [builtin] : BuiltinAttribute;
// @hidden:
__attributeTarget(DeclBase)
attribute_syntax[__AutoDiffBuiltin] : AutoDiffBuiltinAttribute;
__attributeTarget(DeclBase)
attribute_syntax [__requiresNVAPI] : RequiresNVAPIAttribute;
// @public:
/// Mark a type to require a target specific prelude.
/// The prelude will be included in the generated code for the specified target if the resulting code uses
/// the marked type.
__attributeTarget(AggTypeDecl)
attribute_syntax[RequirePrelude(target, prelude:String)] : RequirePreludeAttribute;
__attributeTarget(DeclBase)
attribute_syntax [__AlwaysFoldIntoUseSiteAttribute] : AlwaysFoldIntoUseSiteAttribute;
/// Inform the downstream compiler to not inline the function.
__attributeTarget(FunctionDeclBase)
attribute_syntax [noinline] : NoInlineAttribute;
/// Mark a declaration as deprecated.
/// @param message The diagnostic message to show when the declaration is used.
__attributeTarget(DeclBase)
attribute_syntax [deprecated(message: String)] : DeprecatedAttribute;
/// Controls the behavior of the compiler when a differentiable function is detected to have side-effects.
/// @category misc_types
enum SideEffectBehavior
{
/// Causes a warning if the method is detected to have side-effects
Warn = 0,
/// Suppresses the warning
Allow = 1
};
/// Mark a differentiable function to prefer recomputation over checkpointing when a value computed in the primal pass is needed
/// during backward derivative propagation.
__attributeTarget(FunctionDeclBase)
attribute_syntax[PreferRecompute(behavior: SideEffectBehavior = SideEffectBehavior.Warn)] : PreferRecomputeAttribute;
/// Mark a differentiable function to prefer checkpointing over recomputation when a value computed in the primal pass is needed
/// during backward derivative propagation.
__attributeTarget(FunctionDeclBase)
attribute_syntax [PreferCheckpoint] : PreferCheckpointAttribute;
// @hidden:
__attributeTarget(DeclBase)
attribute_syntax [KnownBuiltin(name : int)] : KnownBuiltinAttribute;
__attributeTarget(FunctionDeclBase)
attribute_syntax [__GLSLRequireShaderInputParameter(parameterNumber:int)] : GLSLRequireShaderInputParameterAttribute;
__attributeTarget(FuncDecl)
attribute_syntax [noRefInline] : NoRefInlineAttribute;
// @public:
/// Mark a function's return value as non-uniform.
__attributeTarget(FunctionDeclBase)
attribute_syntax [NonUniformReturn] : NonDynamicUniformAttribute;
/// Mark a compute shader entry point to allow it to use implicit derivatives.
/// @remarks This attributes causes Slang to emit `DerivativeGroupQuadsNV` execution mode when producing SPIR-V. The attribute has no
/// effect on other targets.
__attributeTarget(FuncDecl)
attribute_syntax [DerivativeGroupQuad] : DerivativeGroupQuadAttribute;
/// Mark a compute shader entry point to allow it to use implicit derivatives.
/// @remarks This attributes causes Slang to emit `DerivativeGroupLinearNV` execution mode when producing SPIR-V. The attribute has no
/// effect on other targets.
__attributeTarget(FuncDecl)
attribute_syntax [DerivativeGroupLinear] : DerivativeGroupLinearAttribute;
/// Emits `MaximallyReconvergesKHR` execution mode when producing SPIR-V.
/// This attribute has no effect on other targets.
__attributeTarget(FuncDecl)
attribute_syntax [MaximallyReconverges] : MaximallyReconvergesAttribute;
/// Emits `QuadDerivativesKHR` execution mode when producing SPIR-V.
/// This attribute has no effect on other targets.
__attributeTarget(FuncDecl)
attribute_syntax [QuadDerivatives] : QuadDerivativesAttribute;
/// Emits `RequireFullQuadsKHR` execution mode when producing SPIR-V.
/// This attribute has no effect on other targets.
__attributeTarget(FuncDecl)
attribute_syntax [RequireFullQuads] : RequireFullQuadsAttribute;
__generic<T>
typealias NodePayloadPtr = Ptr<T, Access::ReadWrite, (AddressSpace)$((uint64_t)AddressSpace::NodePayloadAMDX)>;
__attributeTarget(StructDecl)
attribute_syntax [raypayload] : RayPayloadAttribute;
|