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
|
#![allow(non_upper_case_globals)]
use crate::api::event::create_and_queue;
use crate::api::icd::*;
use crate::api::types::*;
use crate::api::util::*;
use crate::core::context::Context;
use crate::core::device::*;
use crate::core::format::*;
use crate::core::memory::*;
use crate::*;
use mesa_rust_util::properties::Properties;
use mesa_rust_util::ptr::*;
use rusticl_opencl_gen::*;
use std::alloc;
use std::alloc::Layout;
use std::cmp::Ordering;
use std::mem;
use std::os::raw::c_void;
use std::ptr;
use std::slice;
use std::sync::Arc;
fn validate_mem_flags(flags: cl_mem_flags, images: bool) -> CLResult<()> {
let mut valid_flags = cl_bitfield::from(
CL_MEM_READ_WRITE | CL_MEM_WRITE_ONLY | CL_MEM_READ_ONLY | CL_MEM_KERNEL_READ_AND_WRITE,
);
if !images {
valid_flags |= cl_bitfield::from(
CL_MEM_USE_HOST_PTR
| CL_MEM_ALLOC_HOST_PTR
| CL_MEM_COPY_HOST_PTR
| CL_MEM_HOST_WRITE_ONLY
| CL_MEM_HOST_READ_ONLY
| CL_MEM_HOST_NO_ACCESS,
);
}
let read_write_group =
cl_bitfield::from(CL_MEM_READ_WRITE | CL_MEM_WRITE_ONLY | CL_MEM_READ_ONLY);
let alloc_host_group = cl_bitfield::from(CL_MEM_ALLOC_HOST_PTR | CL_MEM_USE_HOST_PTR);
let copy_host_group = cl_bitfield::from(CL_MEM_COPY_HOST_PTR | CL_MEM_USE_HOST_PTR);
let host_read_write_group =
cl_bitfield::from(CL_MEM_HOST_WRITE_ONLY | CL_MEM_HOST_READ_ONLY | CL_MEM_HOST_NO_ACCESS);
if (flags & !valid_flags != 0)
|| (flags & read_write_group).count_ones() > 1
|| (flags & alloc_host_group).count_ones() > 1
|| (flags & copy_host_group).count_ones() > 1
|| (flags & host_read_write_group).count_ones() > 1
{
return Err(CL_INVALID_VALUE);
}
Ok(())
}
fn validate_map_flags_common(map_flags: cl_mem_flags) -> CLResult<()> {
// CL_INVALID_VALUE ... if values specified in map_flags are not valid.
let valid_flags =
cl_bitfield::from(CL_MAP_READ | CL_MAP_WRITE | CL_MAP_WRITE_INVALIDATE_REGION);
let read_write_group = cl_bitfield::from(CL_MAP_READ | CL_MAP_WRITE);
let invalidate_group = cl_bitfield::from(CL_MAP_WRITE_INVALIDATE_REGION);
if (map_flags & !valid_flags != 0)
|| ((map_flags & read_write_group != 0) && (map_flags & invalidate_group != 0))
{
return Err(CL_INVALID_VALUE);
}
Ok(())
}
fn validate_map_flags(m: &Mem, map_flags: cl_mem_flags) -> CLResult<()> {
validate_map_flags_common(map_flags)?;
// CL_INVALID_OPERATION if buffer has been created with CL_MEM_HOST_WRITE_ONLY or
// CL_MEM_HOST_NO_ACCESS and CL_MAP_READ is set in map_flags
if bit_check(m.flags, CL_MEM_HOST_WRITE_ONLY | CL_MEM_HOST_NO_ACCESS) &&
bit_check(map_flags, CL_MAP_READ) ||
// or if buffer has been created with CL_MEM_HOST_READ_ONLY or CL_MEM_HOST_NO_ACCESS and
// CL_MAP_WRITE or CL_MAP_WRITE_INVALIDATE_REGION is set in map_flags.
bit_check(m.flags, CL_MEM_HOST_READ_ONLY | CL_MEM_HOST_NO_ACCESS) &&
bit_check(map_flags, CL_MAP_WRITE | CL_MAP_WRITE_INVALIDATE_REGION)
{
return Err(CL_INVALID_OPERATION);
}
Ok(())
}
fn filter_image_access_flags(flags: cl_mem_flags) -> cl_mem_flags {
flags
& (CL_MEM_READ_WRITE | CL_MEM_WRITE_ONLY | CL_MEM_READ_ONLY | CL_MEM_KERNEL_READ_AND_WRITE)
as cl_mem_flags
}
fn inherit_mem_flags(mut flags: cl_mem_flags, mem: &Mem) -> cl_mem_flags {
let read_write_mask = cl_bitfield::from(
CL_MEM_READ_WRITE |
CL_MEM_WRITE_ONLY |
CL_MEM_READ_ONLY |
// not in spec, but...
CL_MEM_KERNEL_READ_AND_WRITE,
);
let host_ptr_mask =
cl_bitfield::from(CL_MEM_USE_HOST_PTR | CL_MEM_ALLOC_HOST_PTR | CL_MEM_COPY_HOST_PTR);
let host_mask =
cl_bitfield::from(CL_MEM_HOST_WRITE_ONLY | CL_MEM_HOST_READ_ONLY | CL_MEM_HOST_NO_ACCESS);
// For CL_MEM_OBJECT_IMAGE1D_BUFFER image type, or an image created from another memory object
// (image or buffer)...
//
// ... if the CL_MEM_READ_WRITE, CL_MEM_READ_ONLY or CL_MEM_WRITE_ONLY values are not
// specified in flags, they are inherited from the corresponding memory access qualifiers
// associated with mem_object. ...
if flags & read_write_mask == 0 {
flags |= mem.flags & read_write_mask;
}
// ... The CL_MEM_USE_HOST_PTR, CL_MEM_ALLOC_HOST_PTR and CL_MEM_COPY_HOST_PTR values cannot
// be specified in flags but are inherited from the corresponding memory access qualifiers
// associated with mem_object. ...
flags &= !host_ptr_mask;
flags |= mem.flags & host_ptr_mask;
// ... If the CL_MEM_HOST_WRITE_ONLY, CL_MEM_HOST_READ_ONLY or CL_MEM_HOST_NO_ACCESS values
// are not specified in flags, they are inherited from the corresponding memory access
// qualifiers associated with mem_object.
if flags & host_mask == 0 {
flags |= mem.flags & host_mask;
}
flags
}
fn image_type_valid(image_type: cl_mem_object_type) -> bool {
CL_IMAGE_TYPES.contains(&image_type)
}
fn validate_addressing_mode(addressing_mode: cl_addressing_mode) -> CLResult<()> {
match addressing_mode {
CL_ADDRESS_NONE
| CL_ADDRESS_CLAMP_TO_EDGE
| CL_ADDRESS_CLAMP
| CL_ADDRESS_REPEAT
| CL_ADDRESS_MIRRORED_REPEAT => Ok(()),
_ => Err(CL_INVALID_VALUE),
}
}
fn validate_filter_mode(filter_mode: cl_filter_mode) -> CLResult<()> {
match filter_mode {
CL_FILTER_NEAREST | CL_FILTER_LINEAR => Ok(()),
_ => Err(CL_INVALID_VALUE),
}
}
fn validate_host_ptr(host_ptr: *mut ::std::os::raw::c_void, flags: cl_mem_flags) -> CLResult<()> {
// CL_INVALID_HOST_PTR if host_ptr is NULL and CL_MEM_USE_HOST_PTR or CL_MEM_COPY_HOST_PTR are
// set in flags
if host_ptr.is_null()
&& flags & (cl_mem_flags::from(CL_MEM_USE_HOST_PTR | CL_MEM_COPY_HOST_PTR)) != 0
{
return Err(CL_INVALID_HOST_PTR);
}
// or if host_ptr is not NULL but CL_MEM_COPY_HOST_PTR or CL_MEM_USE_HOST_PTR are not set in
// flags.
if !host_ptr.is_null()
&& flags & (cl_mem_flags::from(CL_MEM_USE_HOST_PTR | CL_MEM_COPY_HOST_PTR)) == 0
{
return Err(CL_INVALID_HOST_PTR);
}
Ok(())
}
fn validate_matching_buffer_flags(mem: &Mem, flags: cl_mem_flags) -> CLResult<()> {
// CL_INVALID_VALUE if an image is being created from another memory object (buffer or image)
// under one of the following circumstances:
//
// 1) mem_object was created with CL_MEM_WRITE_ONLY and
// flags specifies CL_MEM_READ_WRITE or CL_MEM_READ_ONLY,
if bit_check(mem.flags, CL_MEM_WRITE_ONLY) && bit_check(flags, CL_MEM_READ_WRITE | CL_MEM_READ_ONLY) ||
// 2) mem_object was created with CL_MEM_READ_ONLY and
// flags specifies CL_MEM_READ_WRITE or CL_MEM_WRITE_ONLY,
bit_check(mem.flags, CL_MEM_READ_ONLY) && bit_check(flags, CL_MEM_READ_WRITE | CL_MEM_WRITE_ONLY) ||
// 3) flags specifies CL_MEM_USE_HOST_PTR or CL_MEM_ALLOC_HOST_PTR or CL_MEM_COPY_HOST_PTR.
bit_check(flags, CL_MEM_USE_HOST_PTR | CL_MEM_ALLOC_HOST_PTR | CL_MEM_COPY_HOST_PTR) ||
// CL_INVALID_VALUE if an image is being created from another memory object (buffer or image)
// and mem_object was created with CL_MEM_HOST_WRITE_ONLY and flags specifies CL_MEM_HOST_READ_ONLY
bit_check(mem.flags, CL_MEM_HOST_WRITE_ONLY) && bit_check(flags, CL_MEM_HOST_READ_ONLY) ||
// or if mem_object was created with CL_MEM_HOST_READ_ONLY and flags specifies CL_MEM_HOST_WRITE_ONLY
bit_check(mem.flags, CL_MEM_HOST_READ_ONLY) && bit_check(flags, CL_MEM_HOST_WRITE_ONLY) ||
// or if mem_object was created with CL_MEM_HOST_NO_ACCESS and_flags_ specifies CL_MEM_HOST_READ_ONLY or CL_MEM_HOST_WRITE_ONLY.
bit_check(mem.flags, CL_MEM_HOST_NO_ACCESS) && bit_check(flags, CL_MEM_HOST_READ_ONLY | CL_MEM_HOST_WRITE_ONLY)
{
return Err(CL_INVALID_VALUE);
}
Ok(())
}
impl CLInfo<cl_mem_info> for cl_mem {
fn query(&self, q: cl_mem_info, _: &[u8]) -> CLResult<Vec<u8>> {
let mem = self.get_ref()?;
Ok(match *q {
CL_MEM_ASSOCIATED_MEMOBJECT => {
let ptr = match mem.parent.as_ref() {
// Note we use as_ptr here which doesn't increase the reference count.
Some(parent) => Arc::as_ptr(parent),
None => ptr::null(),
};
cl_prop::<cl_mem>(cl_mem::from_ptr(ptr))
}
CL_MEM_CONTEXT => {
// Note we use as_ptr here which doesn't increase the reference count.
let ptr = Arc::as_ptr(&mem.context);
cl_prop::<cl_context>(cl_context::from_ptr(ptr))
}
CL_MEM_FLAGS => cl_prop::<cl_mem_flags>(mem.flags),
// TODO debugging feature
CL_MEM_MAP_COUNT => cl_prop::<cl_uint>(0),
CL_MEM_HOST_PTR => cl_prop::<*mut c_void>(mem.host_ptr),
CL_MEM_OFFSET => cl_prop::<usize>(mem.offset),
CL_MEM_PROPERTIES => cl_prop::<&Vec<cl_mem_properties>>(&mem.props),
CL_MEM_REFERENCE_COUNT => cl_prop::<cl_uint>(self.refcnt()?),
CL_MEM_SIZE => cl_prop::<usize>(mem.size),
CL_MEM_TYPE => cl_prop::<cl_mem_object_type>(mem.mem_type),
CL_MEM_USES_SVM_POINTER | CL_MEM_USES_SVM_POINTER_ARM => {
cl_prop::<cl_bool>(mem.is_svm().into())
}
_ => return Err(CL_INVALID_VALUE),
})
}
}
pub fn create_buffer_with_properties(
context: cl_context,
properties: *const cl_mem_properties,
flags: cl_mem_flags,
size: usize,
host_ptr: *mut ::std::os::raw::c_void,
) -> CLResult<cl_mem> {
let c = context.get_arc()?;
// CL_INVALID_VALUE if values specified in flags are not valid as defined in the Memory Flags table.
validate_mem_flags(flags, false)?;
// CL_INVALID_BUFFER_SIZE if size is 0
if size == 0 {
return Err(CL_INVALID_BUFFER_SIZE);
}
// ... or if size is greater than CL_DEVICE_MAX_MEM_ALLOC_SIZE for all devices in context.
if checked_compare(size, Ordering::Greater, c.max_mem_alloc()) {
return Err(CL_INVALID_BUFFER_SIZE);
}
validate_host_ptr(host_ptr, flags)?;
let props = Properties::from_ptr_raw(properties);
// CL_INVALID_PROPERTY if a property name in properties is not a supported property name, if
// the value specified for a supported property name is not valid, or if the same property name
// is specified more than once.
if props.len() > 1 {
// we don't support any properties besides the 0 property
return Err(CL_INVALID_PROPERTY);
}
Ok(cl_mem::from_arc(Mem::new_buffer(
c, flags, size, host_ptr, props,
)?))
}
pub fn create_buffer(
context: cl_context,
flags: cl_mem_flags,
size: usize,
host_ptr: *mut ::std::os::raw::c_void,
) -> CLResult<cl_mem> {
create_buffer_with_properties(context, ptr::null(), flags, size, host_ptr)
}
pub fn create_sub_buffer(
buffer: cl_mem,
mut flags: cl_mem_flags,
buffer_create_type: cl_buffer_create_type,
buffer_create_info: *const ::std::os::raw::c_void,
) -> CLResult<cl_mem> {
let b = buffer.get_arc()?;
// CL_INVALID_MEM_OBJECT if buffer ... is a sub-buffer object.
if b.parent.is_some() {
return Err(CL_INVALID_MEM_OBJECT);
}
validate_matching_buffer_flags(&b, flags)?;
flags = inherit_mem_flags(flags, &b);
validate_mem_flags(flags, false)?;
let (offset, size) = match buffer_create_type {
CL_BUFFER_CREATE_TYPE_REGION => {
// buffer_create_info is a pointer to a cl_buffer_region structure specifying a region of
// the buffer.
// CL_INVALID_VALUE if value(s) specified in buffer_create_info (for a given
// buffer_create_type) is not valid or if buffer_create_info is NULL.
let region = unsafe { buffer_create_info.cast::<cl_buffer_region>().as_ref() }
.ok_or(CL_INVALID_VALUE)?;
// CL_INVALID_BUFFER_SIZE if the size field of the cl_buffer_region structure passed in
// buffer_create_info is 0.
if region.size == 0 {
return Err(CL_INVALID_BUFFER_SIZE);
}
// CL_INVALID_VALUE if the region specified by the cl_buffer_region structure passed in
// buffer_create_info is out of bounds in buffer.
if region.origin + region.size > b.size {
return Err(CL_INVALID_VALUE);
}
(region.origin, region.size)
}
// CL_INVALID_VALUE if the value specified in buffer_create_type is not valid.
_ => return Err(CL_INVALID_VALUE),
};
Ok(cl_mem::from_arc(Mem::new_sub_buffer(
b, flags, offset, size,
)))
// TODO
// CL_MISALIGNED_SUB_BUFFER_OFFSET if there are no devices in context associated with buffer for which the origin field of the cl_buffer_region structure passed in buffer_create_info is aligned to the CL_DEVICE_MEM_BASE_ADDR_ALIGN value.
}
pub fn set_mem_object_destructor_callback(
memobj: cl_mem,
pfn_notify: Option<MemCB>,
user_data: *mut ::std::os::raw::c_void,
) -> CLResult<()> {
let m = memobj.get_ref()?;
// CL_INVALID_VALUE if pfn_notify is NULL.
if pfn_notify.is_none() {
return Err(CL_INVALID_VALUE);
}
m.cbs
.lock()
.unwrap()
.push(cl_closure!(|m| pfn_notify(m, user_data)));
Ok(())
}
fn validate_image_format<'a>(
image_format: *const cl_image_format,
) -> CLResult<(&'a cl_image_format, u8)> {
// CL_INVALID_IMAGE_FORMAT_DESCRIPTOR ... if image_format is NULL.
let format = unsafe { image_format.as_ref() }.ok_or(CL_INVALID_IMAGE_FORMAT_DESCRIPTOR)?;
let pixel_size = format
.pixel_size()
.ok_or(CL_INVALID_IMAGE_FORMAT_DESCRIPTOR)?;
// special validation
let valid_combination = match format.image_channel_data_type {
CL_UNORM_SHORT_565 | CL_UNORM_SHORT_555 | CL_UNORM_INT_101010 => {
[CL_RGB, CL_RGBx].contains(&format.image_channel_data_type)
}
CL_UNORM_INT_101010_2 => format.image_channel_data_type == CL_RGBA,
_ => true,
};
if !valid_combination {
return Err(CL_INVALID_IMAGE_FORMAT_DESCRIPTOR);
}
Ok((format, pixel_size))
}
fn validate_image_desc(
image_desc: *const cl_image_desc,
host_ptr: *mut ::std::os::raw::c_void,
elem_size: usize,
devs: &[Arc<Device>],
) -> CLResult<(cl_image_desc, Option<Arc<Mem>>)> {
// CL_INVALID_IMAGE_DESCRIPTOR if values specified in image_desc are not valid
const err: cl_int = CL_INVALID_IMAGE_DESCRIPTOR;
// CL_INVALID_IMAGE_DESCRIPTOR ... if image_desc is NULL.
let mut desc = *unsafe { image_desc.as_ref() }.ok_or(err)?;
// image_type describes the image type and must be either CL_MEM_OBJECT_IMAGE1D,
// CL_MEM_OBJECT_IMAGE1D_BUFFER, CL_MEM_OBJECT_IMAGE1D_ARRAY, CL_MEM_OBJECT_IMAGE2D,
// CL_MEM_OBJECT_IMAGE2D_ARRAY, or CL_MEM_OBJECT_IMAGE3D.
if !CL_IMAGE_TYPES.contains(&desc.image_type) {
return Err(err);
}
let (dims, array) = desc.type_info();
// image_width is the width of the image in pixels. For a 2D image and image array, the image
// width must be a value ≥ 1 and ≤ CL_DEVICE_IMAGE2D_MAX_WIDTH. For a 3D image, the image width
// must be a value ≥ 1 and ≤ CL_DEVICE_IMAGE3D_MAX_WIDTH. For a 1D image buffer, the image width
// must be a value ≥ 1 and ≤ CL_DEVICE_IMAGE_MAX_BUFFER_SIZE. For a 1D image and 1D image array,
// the image width must be a value ≥ 1 and ≤ CL_DEVICE_IMAGE2D_MAX_WIDTH.
//
// image_height is the height of the image in pixels. This is only used if the image is a 2D or
// 3D image, or a 2D image array. For a 2D image or image array, the image height must be a
// value ≥ 1 and ≤ CL_DEVICE_IMAGE2D_MAX_HEIGHT. For a 3D image, the image height must be a
// value ≥ 1 and ≤ CL_DEVICE_IMAGE3D_MAX_HEIGHT.
//
// image_depth is the depth of the image in pixels. This is only used if the image is a 3D image
// and must be a value ≥ 1 and ≤ CL_DEVICE_IMAGE3D_MAX_DEPTH.
if desc.image_width < 1
|| desc.image_height < 1 && dims >= 2
|| desc.image_depth < 1 && dims >= 3
|| desc.image_array_size < 1 && array
{
return Err(err);
}
let max_size = if dims == 3 {
devs.iter().map(|d| d.image_3d_size()).min()
} else if desc.image_type == CL_MEM_OBJECT_IMAGE1D_BUFFER {
devs.iter().map(|d| d.image_buffer_size()).min()
} else {
devs.iter().map(|d| d.image_2d_size()).min()
}
.unwrap();
let max_array = devs.iter().map(|d| d.image_array_size()).min().unwrap();
// CL_INVALID_IMAGE_SIZE if image dimensions specified in image_desc exceed the maximum image
// dimensions described in the Device Queries table for all devices in context.
if desc.image_width > max_size
|| desc.image_height > max_size && dims >= 2
|| desc.image_depth > max_size && dims >= 3
|| desc.image_array_size > max_array && array
{
return Err(CL_INVALID_IMAGE_SIZE);
}
// num_mip_levels and num_samples must be 0.
if desc.num_mip_levels != 0 || desc.num_samples != 0 {
return Err(err);
}
// mem_object may refer to a valid buffer or image memory object. mem_object can be a buffer
// memory object if image_type is CL_MEM_OBJECT_IMAGE1D_BUFFER or CL_MEM_OBJECT_IMAGE2D.
// mem_object can be an image object if image_type is CL_MEM_OBJECT_IMAGE2D. Otherwise it must
// be NULL.
//
// TODO: cl_khr_image2d_from_buffer is an optional feature
let p = unsafe { &desc.anon_1.mem_object };
let parent = if !p.is_null() {
let p = p.get_arc()?;
if !match desc.image_type {
CL_MEM_OBJECT_IMAGE1D_BUFFER => p.is_buffer(),
CL_MEM_OBJECT_IMAGE2D => {
(p.is_buffer() && devs.iter().any(|d| d.image2d_from_buffer_supported()))
|| p.mem_type == CL_MEM_OBJECT_IMAGE2D
}
_ => false,
} {
return Err(CL_INVALID_OPERATION);
}
Some(p)
} else {
None
};
// image_row_pitch is the scan-line pitch in bytes. This must be 0 if host_ptr is NULL and can
// be either 0 or ≥ image_width × size of element in bytes if host_ptr is not NULL. If host_ptr
// is not NULL and image_row_pitch = 0, image_row_pitch is calculated as image_width × size of
// element in bytes. If image_row_pitch is not 0, it must be a multiple of the image element
// size in bytes. For a 2D image created from a buffer, the pitch specified (or computed if
// pitch specified is 0) must be a multiple of the maximum of the
// CL_DEVICE_IMAGE_PITCH_ALIGNMENT value for all devices in the context associated with the
// buffer specified by mem_object that support images.
//
// image_slice_pitch is the size in bytes of each 2D slice in the 3D image or the size in bytes
// of each image in a 1D or 2D image array. This must be 0 if host_ptr is NULL. If host_ptr is
// not NULL, image_slice_pitch can be either 0 or ≥ image_row_pitch × image_height for a 2D
// image array or 3D image and can be either 0 or ≥ image_row_pitch for a 1D image array. If
// host_ptr is not NULL and image_slice_pitch = 0, image_slice_pitch is calculated as
// image_row_pitch × image_height for a 2D image array or 3D image and image_row_pitch for a 1D
// image array. If image_slice_pitch is not 0, it must be a multiple of the image_row_pitch.
let has_buf_parent = parent.as_ref().map_or(false, |p| p.is_buffer());
if host_ptr.is_null() {
if (desc.image_row_pitch != 0 || desc.image_slice_pitch != 0) && !has_buf_parent {
return Err(err);
}
if desc.image_row_pitch == 0 {
desc.image_row_pitch = desc.image_width * elem_size;
}
if desc.image_slice_pitch == 0 {
desc.image_slice_pitch = desc.image_row_pitch * desc.image_height;
}
if has_buf_parent {
let pitch_alignment = devs
.iter()
.map(|d| d.image_pitch_alignment())
.max()
.unwrap() as usize;
if desc.image_row_pitch % (pitch_alignment * elem_size) != 0 {
return Err(err);
}
}
} else {
if desc.image_row_pitch == 0 {
desc.image_row_pitch = desc.image_width * elem_size;
} else if desc.image_row_pitch % elem_size != 0 {
return Err(err);
}
if dims == 3 || array {
let valid_slice_pitch =
desc.image_row_pitch * if dims == 1 { 1 } else { desc.image_height };
if desc.image_slice_pitch == 0 {
desc.image_slice_pitch = valid_slice_pitch;
} else if desc.image_slice_pitch < valid_slice_pitch
|| desc.image_slice_pitch % desc.image_row_pitch != 0
{
return Err(err);
}
}
}
Ok((desc, parent))
}
fn validate_image_bounds(i: &Mem, origin: CLVec<usize>, region: CLVec<usize>) -> CLResult<()> {
let dims = i.image_desc.dims_with_array();
let bound = region + origin;
if bound > i.image_desc.size() {
return Err(CL_INVALID_VALUE);
}
// If image is a 2D image object, origin[2] must be 0. If image is a 1D image or 1D image buffer
// object, origin[1] and origin[2] must be 0. If image is a 1D image array object, origin[2]
// must be 0.
if dims < 3 && origin[2] != 0 || dims < 2 && origin[1] != 0 {
return Err(CL_INVALID_VALUE);
}
// If image is a 2D image object, region[2] must be 1. If image is a 1D image or 1D image buffer
// object, region[1] and region[2] must be 1. If image is a 1D image array object, region[2]
// must be 1. The values in region cannot be 0.
if dims < 3 && region[2] != 1 || dims < 2 && region[1] != 1 || region.contains(&0) {
return Err(CL_INVALID_VALUE);
}
Ok(())
}
fn desc_eq_no_buffer(a: &cl_image_desc, b: &cl_image_desc) -> bool {
a.image_type == b.image_type
&& a.image_width == b.image_width
&& a.image_height == b.image_height
&& a.image_depth == b.image_depth
&& a.image_array_size == b.image_array_size
&& a.image_row_pitch == b.image_row_pitch
&& a.image_slice_pitch == b.image_slice_pitch
&& a.num_mip_levels == b.num_mip_levels
&& a.num_samples == b.num_samples
}
fn validate_buffer(
desc: &cl_image_desc,
mut flags: cl_mem_flags,
format: &cl_image_format,
host_ptr: *mut ::std::os::raw::c_void,
elem_size: usize,
) -> CLResult<cl_mem_flags> {
// CL_INVALID_IMAGE_DESCRIPTOR if values specified in image_desc are not valid
const err: cl_int = CL_INVALID_IMAGE_DESCRIPTOR;
let mem_object = unsafe { desc.anon_1.mem_object };
// mem_object may refer to a valid buffer or image memory object. mem_object can be a buffer
// memory object if image_type is CL_MEM_OBJECT_IMAGE1D_BUFFER or CL_MEM_OBJECT_IMAGE2D
// mem_object can be an image object if image_type is CL_MEM_OBJECT_IMAGE2D. Otherwise it must
// be NULL. The image pixels are taken from the memory objects data store. When the contents of
// the specified memory objects data store are modified, those changes are reflected in the
// contents of the image object and vice-versa at corresponding synchronization points.
if !mem_object.is_null() {
let mem = mem_object.get_ref()?;
match mem.mem_type {
CL_MEM_OBJECT_BUFFER => {
match desc.image_type {
// For a 1D image buffer created from a buffer object, the image_width × size of
// element in bytes must be ≤ size of the buffer object.
CL_MEM_OBJECT_IMAGE1D_BUFFER => {
if desc.image_width * elem_size > mem.size {
return Err(err);
}
}
// For a 2D image created from a buffer object, the image_row_pitch × image_height
// must be ≤ size of the buffer object specified by mem_object.
CL_MEM_OBJECT_IMAGE2D => {
//TODO
//• CL_INVALID_IMAGE_FORMAT_DESCRIPTOR if a 2D image is created from a buffer and the row pitch and base address alignment does not follow the rules described for creating a 2D image from a buffer.
if desc.image_row_pitch * desc.image_height > mem.size {
return Err(err);
}
}
_ => return Err(err),
}
}
// For an image object created from another image object, the values specified in the
// image descriptor except for mem_object must match the image descriptor information
// associated with mem_object.
CL_MEM_OBJECT_IMAGE2D => {
if desc.image_type != mem.mem_type || !desc_eq_no_buffer(desc, &mem.image_desc) {
return Err(err);
}
// CL_INVALID_IMAGE_FORMAT_DESCRIPTOR if a 2D image is created from a 2D image object
// and the rules described above are not followed.
// Creating a 2D image object from another 2D image object creates a new 2D image
// object that shares the image data store with mem_object but views the pixels in the
// image with a different image channel order. Restrictions are:
//
// The image channel data type specified in image_format must match the image channel
// data type associated with mem_object.
if format.image_channel_data_type != mem.image_format.image_channel_data_type {
return Err(CL_INVALID_IMAGE_FORMAT_DESCRIPTOR);
}
// The image channel order specified in image_format must be compatible with the image
// channel order associated with mem_object. Compatible image channel orders are:
if format.image_channel_order != mem.image_format.image_channel_order {
// in image_format | in mem_object:
// CL_sBGRA | CL_BGRA
// CL_BGRA | CL_sBGRA
// CL_sRGBA | CL_RGBA
// CL_RGBA | CL_sRGBA
// CL_sRGB | CL_RGB
// CL_RGB | CL_sRGB
// CL_sRGBx | CL_RGBx
// CL_RGBx | CL_sRGBx
// CL_DEPTH | CL_R
match (
format.image_channel_order,
mem.image_format.image_channel_order,
) {
(CL_sBGRA, CL_BGRA)
| (CL_BGRA, CL_sBGRA)
| (CL_sRGBA, CL_RGBA)
| (CL_RGBA, CL_sRGBA)
| (CL_sRGB, CL_RGB)
| (CL_RGB, CL_sRGB)
| (CL_sRGBx, CL_RGBx)
| (CL_RGBx, CL_sRGBx)
| (CL_DEPTH, CL_R) => (),
_ => return Err(CL_INVALID_IMAGE_FORMAT_DESCRIPTOR),
}
}
}
_ => return Err(err),
}
// If the buffer object specified by mem_object was created with CL_MEM_USE_HOST_PTR, the
// host_ptr specified to clCreateBuffer or clCreateBufferWithProperties must be aligned to
// the maximum of the CL_DEVICE_IMAGE_BASE_ADDRESS_ALIGNMENT value for all devices in the
// context associated with the buffer specified by mem_object that support images.
if mem.flags & CL_MEM_USE_HOST_PTR as cl_mem_flags != 0 {
for dev in &mem.context.devs {
let addr_alignment = dev.image_base_address_alignment();
if addr_alignment == 0 {
return Err(CL_INVALID_OPERATION);
} else if !is_alligned(host_ptr, addr_alignment as usize) {
return Err(err);
}
}
}
validate_matching_buffer_flags(mem, flags)?;
flags = inherit_mem_flags(flags, mem);
// implied by spec
} else if desc.image_type == CL_MEM_OBJECT_IMAGE1D_BUFFER {
return Err(err);
}
Ok(flags)
}
impl CLInfo<cl_image_info> for cl_mem {
fn query(&self, q: cl_image_info, _: &[u8]) -> CLResult<Vec<u8>> {
let mem = self.get_ref()?;
Ok(match *q {
CL_IMAGE_ARRAY_SIZE => cl_prop::<usize>(mem.image_desc.image_array_size),
CL_IMAGE_BUFFER => cl_prop::<cl_mem>(unsafe { mem.image_desc.anon_1.buffer }),
CL_IMAGE_DEPTH => cl_prop::<usize>(mem.image_desc.image_depth),
CL_IMAGE_ELEMENT_SIZE => cl_prop::<usize>(mem.image_elem_size.into()),
CL_IMAGE_FORMAT => cl_prop::<cl_image_format>(mem.image_format),
CL_IMAGE_HEIGHT => cl_prop::<usize>(mem.image_desc.image_height),
CL_IMAGE_NUM_MIP_LEVELS => cl_prop::<cl_uint>(mem.image_desc.num_mip_levels),
CL_IMAGE_NUM_SAMPLES => cl_prop::<cl_uint>(mem.image_desc.num_samples),
CL_IMAGE_ROW_PITCH => cl_prop::<usize>(mem.image_desc.image_row_pitch),
CL_IMAGE_SLICE_PITCH => cl_prop::<usize>(mem.image_desc.image_slice_pitch),
CL_IMAGE_WIDTH => cl_prop::<usize>(mem.image_desc.image_width),
_ => return Err(CL_INVALID_VALUE),
})
}
}
pub fn create_image_with_properties(
context: cl_context,
properties: *const cl_mem_properties,
mut flags: cl_mem_flags,
image_format: *const cl_image_format,
image_desc: *const cl_image_desc,
host_ptr: *mut ::std::os::raw::c_void,
) -> CLResult<cl_mem> {
let c = context.get_arc()?;
// CL_INVALID_OPERATION if there are no devices in context that support images (i.e.
// CL_DEVICE_IMAGE_SUPPORT specified in the Device Queries table is CL_FALSE).
c.devs
.iter()
.find(|d| d.image_supported())
.ok_or(CL_INVALID_OPERATION)?;
let (format, elem_size) = validate_image_format(image_format)?;
let (desc, parent) = validate_image_desc(image_desc, host_ptr, elem_size.into(), &c.devs)?;
// validate host_ptr before merging flags
validate_host_ptr(host_ptr, flags)?;
flags = validate_buffer(&desc, flags, format, host_ptr, elem_size.into())?;
// For all image types except CL_MEM_OBJECT_IMAGE1D_BUFFER, if the value specified for flags is 0, the
// default is used which is CL_MEM_READ_WRITE.
if flags == 0 && desc.image_type != CL_MEM_OBJECT_IMAGE1D_BUFFER {
flags = CL_MEM_READ_WRITE.into();
}
validate_mem_flags(flags, false)?;
let filtered_flags = filter_image_access_flags(flags);
// CL_IMAGE_FORMAT_NOT_SUPPORTED if there are no devices in context that support image_format.
c.devs
.iter()
.filter_map(|d| d.formats.get(format))
.filter_map(|f| f.get(&desc.image_type))
.find(|f| *f & filtered_flags == filtered_flags)
.ok_or(CL_IMAGE_FORMAT_NOT_SUPPORTED)?;
let props = Properties::from_ptr_raw(properties);
// CL_INVALID_PROPERTY if a property name in properties is not a supported property name, if
// the value specified for a supported property name is not valid, or if the same property name
// is specified more than once.
if props.len() > 1 {
// we don't support any properties besides the 0 property
return Err(CL_INVALID_PROPERTY);
}
Ok(cl_mem::from_arc(Mem::new_image(
c,
parent,
desc.image_type,
flags,
format,
desc,
elem_size,
host_ptr,
props,
)?))
}
pub fn create_image(
context: cl_context,
flags: cl_mem_flags,
image_format: *const cl_image_format,
image_desc: *const cl_image_desc,
host_ptr: *mut ::std::os::raw::c_void,
) -> CLResult<cl_mem> {
create_image_with_properties(
context,
ptr::null(),
flags,
image_format,
image_desc,
host_ptr,
)
}
pub fn create_image_2d(
context: cl_context,
flags: cl_mem_flags,
image_format: *const cl_image_format,
image_width: usize,
image_height: usize,
image_row_pitch: usize,
host_ptr: *mut ::std::os::raw::c_void,
) -> CLResult<cl_mem> {
let image_desc = cl_image_desc {
image_type: CL_MEM_OBJECT_IMAGE2D,
image_width: image_width,
image_height: image_height,
image_row_pitch: image_row_pitch,
..Default::default()
};
create_image(context, flags, image_format, &image_desc, host_ptr)
}
pub fn create_image_3d(
context: cl_context,
flags: cl_mem_flags,
image_format: *const cl_image_format,
image_width: usize,
image_height: usize,
image_depth: usize,
image_row_pitch: usize,
image_slice_pitch: usize,
host_ptr: *mut ::std::os::raw::c_void,
) -> CLResult<cl_mem> {
let image_desc = cl_image_desc {
image_type: CL_MEM_OBJECT_IMAGE3D,
image_width: image_width,
image_height: image_height,
image_depth: image_depth,
image_row_pitch: image_row_pitch,
image_slice_pitch: image_slice_pitch,
..Default::default()
};
create_image(context, flags, image_format, &image_desc, host_ptr)
}
pub fn get_supported_image_formats(
context: cl_context,
flags: cl_mem_flags,
image_type: cl_mem_object_type,
num_entries: cl_uint,
image_formats: *mut cl_image_format,
num_image_formats: *mut cl_uint,
) -> CLResult<()> {
let c = context.get_ref()?;
// CL_INVALID_VALUE if flags
validate_mem_flags(flags, true)?;
// or image_type are not valid
if !image_type_valid(image_type) {
return Err(CL_INVALID_VALUE);
}
// CL_INVALID_VALUE ... if num_entries is 0 and image_formats is not NULL.
if num_entries == 0 && !image_formats.is_null() {
return Err(CL_INVALID_VALUE);
}
let mut res = Vec::<cl_image_format>::new();
let filtered_flags = filter_image_access_flags(flags);
for dev in &c.devs {
for f in &dev.formats {
let s = f.1.get(&image_type).unwrap_or(&0);
if filtered_flags & s == filtered_flags {
res.push(*f.0);
}
}
}
res.sort();
res.dedup();
num_image_formats.write_checked(res.len() as cl_uint);
unsafe { image_formats.copy_checked(res.as_ptr(), res.len()) };
Ok(())
}
impl CLInfo<cl_sampler_info> for cl_sampler {
fn query(&self, q: cl_sampler_info, _: &[u8]) -> CLResult<Vec<u8>> {
let sampler = self.get_ref()?;
Ok(match q {
CL_SAMPLER_ADDRESSING_MODE => cl_prop::<cl_addressing_mode>(sampler.addressing_mode),
CL_SAMPLER_CONTEXT => {
// Note we use as_ptr here which doesn't increase the reference count.
let ptr = Arc::as_ptr(&sampler.context);
cl_prop::<cl_context>(cl_context::from_ptr(ptr))
}
CL_SAMPLER_FILTER_MODE => cl_prop::<cl_filter_mode>(sampler.filter_mode),
CL_SAMPLER_NORMALIZED_COORDS => cl_prop::<bool>(sampler.normalized_coords),
CL_SAMPLER_REFERENCE_COUNT => cl_prop::<cl_uint>(self.refcnt()?),
CL_SAMPLER_PROPERTIES => {
cl_prop::<&Option<Properties<cl_sampler_properties>>>(&sampler.props)
}
// CL_INVALID_VALUE if param_name is not one of the supported values
_ => return Err(CL_INVALID_VALUE),
})
}
}
fn create_sampler_impl(
context: cl_context,
normalized_coords: cl_bool,
addressing_mode: cl_addressing_mode,
filter_mode: cl_filter_mode,
props: Option<Properties<cl_sampler_properties>>,
) -> CLResult<cl_sampler> {
let c = context.get_arc()?;
// CL_INVALID_OPERATION if images are not supported by any device associated with context (i.e.
// CL_DEVICE_IMAGE_SUPPORT specified in the Device Queries table is CL_FALSE).
c.devs
.iter()
.find(|d| d.image_supported())
.ok_or(CL_INVALID_OPERATION)?;
// CL_INVALID_VALUE if addressing_mode, filter_mode, normalized_coords or a combination of these
// arguements are not valid.
validate_addressing_mode(addressing_mode)?;
validate_filter_mode(filter_mode)?;
let sampler = Sampler::new(
c,
check_cl_bool(normalized_coords).ok_or(CL_INVALID_VALUE)?,
addressing_mode,
filter_mode,
props,
);
Ok(cl_sampler::from_arc(sampler))
}
pub fn create_sampler(
context: cl_context,
normalized_coords: cl_bool,
addressing_mode: cl_addressing_mode,
filter_mode: cl_filter_mode,
) -> CLResult<cl_sampler> {
create_sampler_impl(
context,
normalized_coords,
addressing_mode,
filter_mode,
None,
)
}
pub fn create_sampler_with_properties(
context: cl_context,
sampler_properties: *const cl_sampler_properties,
) -> CLResult<cl_sampler> {
let mut normalized_coords = CL_TRUE;
let mut addressing_mode = CL_ADDRESS_CLAMP;
let mut filter_mode = CL_FILTER_NEAREST;
// CL_INVALID_VALUE if the same property name is specified more than once.
let sampler_properties = if sampler_properties.is_null() {
None
} else {
let sampler_properties =
Properties::from_ptr(sampler_properties).ok_or(CL_INVALID_VALUE)?;
for p in &sampler_properties.props {
match p.0 as u32 {
CL_SAMPLER_ADDRESSING_MODE => addressing_mode = p.1 as u32,
CL_SAMPLER_FILTER_MODE => filter_mode = p.1 as u32,
CL_SAMPLER_NORMALIZED_COORDS => normalized_coords = p.1 as u32,
// CL_INVALID_VALUE if the property name in sampler_properties is not a supported
// property name
_ => return Err(CL_INVALID_VALUE),
}
}
Some(sampler_properties)
};
create_sampler_impl(
context,
normalized_coords,
addressing_mode,
filter_mode,
sampler_properties,
)
}
pub fn enqueue_read_buffer(
command_queue: cl_command_queue,
buffer: cl_mem,
blocking_read: cl_bool,
offset: usize,
cb: usize,
ptr: *mut ::std::os::raw::c_void,
num_events_in_wait_list: cl_uint,
event_wait_list: *const cl_event,
event: *mut cl_event,
) -> CLResult<()> {
let q = command_queue.get_arc()?;
let b = buffer.get_arc()?;
let block = check_cl_bool(blocking_read).ok_or(CL_INVALID_VALUE)?;
let evs = event_list_from_cl(&q, num_events_in_wait_list, event_wait_list)?;
// CL_INVALID_VALUE if the region being read or written specified by (offset, size) is out of
// bounds or if ptr is a NULL value.
if offset + cb > b.size || ptr.is_null() {
return Err(CL_INVALID_VALUE);
}
// CL_INVALID_CONTEXT if the context associated with command_queue and buffer are not the same
if b.context != q.context {
return Err(CL_INVALID_CONTEXT);
}
// CL_EXEC_STATUS_ERROR_FOR_EVENTS_IN_WAIT_LIST if the read and write operations are blocking
// and the execution status of any of the events in event_wait_list is a negative integer value.
if block && evs.iter().any(|e| e.is_error()) {
return Err(CL_EXEC_STATUS_ERROR_FOR_EVENTS_IN_WAIT_LIST);
}
// CL_INVALID_OPERATION if clEnqueueReadBuffer is called on buffer which has been created with
// CL_MEM_HOST_WRITE_ONLY or CL_MEM_HOST_NO_ACCESS.
if bit_check(b.flags, CL_MEM_HOST_WRITE_ONLY | CL_MEM_HOST_NO_ACCESS) {
return Err(CL_INVALID_OPERATION);
}
create_and_queue(
q,
CL_COMMAND_READ_BUFFER,
evs,
event,
block,
Box::new(move |q, ctx| b.read_to_user(q, ctx, offset, ptr, cb)),
)
// TODO
// CL_MISALIGNED_SUB_BUFFER_OFFSET if buffer is a sub-buffer object and offset specified when the sub-buffer object is created is not aligned to CL_DEVICE_MEM_BASE_ADDR_ALIGN value for device associated with queue.
}
pub fn enqueue_write_buffer(
command_queue: cl_command_queue,
buffer: cl_mem,
blocking_write: cl_bool,
offset: usize,
cb: usize,
ptr: *const ::std::os::raw::c_void,
num_events_in_wait_list: cl_uint,
event_wait_list: *const cl_event,
event: *mut cl_event,
) -> CLResult<()> {
let q = command_queue.get_arc()?;
let b = buffer.get_arc()?;
let block = check_cl_bool(blocking_write).ok_or(CL_INVALID_VALUE)?;
let evs = event_list_from_cl(&q, num_events_in_wait_list, event_wait_list)?;
// CL_INVALID_VALUE if the region being read or written specified by (offset, size) is out of
// bounds or if ptr is a NULL value.
if offset + cb > b.size || ptr.is_null() {
return Err(CL_INVALID_VALUE);
}
// CL_INVALID_CONTEXT if the context associated with command_queue and buffer are not the same
if b.context != q.context {
return Err(CL_INVALID_CONTEXT);
}
// CL_EXEC_STATUS_ERROR_FOR_EVENTS_IN_WAIT_LIST if the read and write operations are blocking
// and the execution status of any of the events in event_wait_list is a negative integer value.
if block && evs.iter().any(|e| e.is_error()) {
return Err(CL_EXEC_STATUS_ERROR_FOR_EVENTS_IN_WAIT_LIST);
}
// CL_INVALID_OPERATION if clEnqueueWriteBuffer is called on buffer which has been created with
// CL_MEM_HOST_READ_ONLY or CL_MEM_HOST_NO_ACCESS.
if bit_check(b.flags, CL_MEM_HOST_READ_ONLY | CL_MEM_HOST_NO_ACCESS) {
return Err(CL_INVALID_OPERATION);
}
create_and_queue(
q,
CL_COMMAND_WRITE_BUFFER,
evs,
event,
block,
Box::new(move |q, ctx| b.write_from_user(q, ctx, offset, ptr, cb)),
)
// TODO
// CL_MISALIGNED_SUB_BUFFER_OFFSET if buffer is a sub-buffer object and offset specified when the sub-buffer object is created is not aligned to CL_DEVICE_MEM_BASE_ADDR_ALIGN value for device associated with queue.
}
pub fn enqueue_copy_buffer(
command_queue: cl_command_queue,
src_buffer: cl_mem,
dst_buffer: cl_mem,
src_offset: usize,
dst_offset: usize,
size: usize,
num_events_in_wait_list: cl_uint,
event_wait_list: *const cl_event,
event: *mut cl_event,
) -> CLResult<()> {
let q = command_queue.get_arc()?;
let src = src_buffer.get_arc()?;
let dst = dst_buffer.get_arc()?;
let evs = event_list_from_cl(&q, num_events_in_wait_list, event_wait_list)?;
// CL_INVALID_CONTEXT if the context associated with command_queue, src_buffer and dst_buffer
// are not the same
if q.context != src.context || q.context != dst.context {
return Err(CL_INVALID_CONTEXT);
}
// CL_INVALID_VALUE if src_offset, dst_offset, size, src_offset + size or dst_offset + size
// require accessing elements outside the src_buffer and dst_buffer buffer objects respectively.
if src_offset + size > src.size || dst_offset + size > dst.size {
return Err(CL_INVALID_VALUE);
}
// CL_MEM_COPY_OVERLAP if src_buffer and dst_buffer are the same buffer or sub-buffer object
// and the source and destination regions overlap or if src_buffer and dst_buffer are different
// sub-buffers of the same associated buffer object and they overlap. The regions overlap if
// src_offset ≤ dst_offset ≤ src_offset + size - 1 or if dst_offset ≤ src_offset ≤ dst_offset + size - 1.
if src.has_same_parent(&dst) {
let src_offset = src_offset + src.offset;
let dst_offset = dst_offset + dst.offset;
if (src_offset <= dst_offset && dst_offset < src_offset + size)
|| (dst_offset <= src_offset && src_offset < dst_offset + size)
{
return Err(CL_MEM_COPY_OVERLAP);
}
}
create_and_queue(
q,
CL_COMMAND_COPY_BUFFER,
evs,
event,
false,
Box::new(move |q, ctx| {
src.copy_to(
q,
ctx,
&dst,
CLVec::new([src_offset, 0, 0]),
CLVec::new([dst_offset, 0, 0]),
&CLVec::new([size, 1, 1]),
)
}),
)
// TODO
//• CL_MISALIGNED_SUB_BUFFER_OFFSET if src_buffer is a sub-buffer object and offset specified when the sub-buffer object is created is not aligned to CL_DEVICE_MEM_BASE_ADDR_ALIGN value for device associated with queue.
//• CL_MISALIGNED_SUB_BUFFER_OFFSET if dst_buffer is a sub-buffer object and offset specified when the sub-buffer object is created is not aligned to CL_DEVICE_MEM_BASE_ADDR_ALIGN value for device associated with queue.
//• CL_MEM_OBJECT_ALLOCATION_FAILURE if there is a failure to allocate memory for data store associated with src_buffer or dst_buffer.
}
pub fn enqueue_read_buffer_rect(
command_queue: cl_command_queue,
buffer: cl_mem,
blocking_read: cl_bool,
buffer_origin: *const usize,
host_origin: *const usize,
region: *const usize,
mut buffer_row_pitch: usize,
mut buffer_slice_pitch: usize,
mut host_row_pitch: usize,
mut host_slice_pitch: usize,
ptr: *mut ::std::os::raw::c_void,
num_events_in_wait_list: cl_uint,
event_wait_list: *const cl_event,
event: *mut cl_event,
) -> CLResult<()> {
let block = check_cl_bool(blocking_read).ok_or(CL_INVALID_VALUE)?;
let q = command_queue.get_arc()?;
let buf = buffer.get_arc()?;
let evs = event_list_from_cl(&q, num_events_in_wait_list, event_wait_list)?;
// CL_INVALID_OPERATION if clEnqueueReadBufferRect is called on buffer which has been created
// with CL_MEM_HOST_WRITE_ONLY or CL_MEM_HOST_NO_ACCESS.
if bit_check(buf.flags, CL_MEM_HOST_WRITE_ONLY | CL_MEM_HOST_NO_ACCESS) {
return Err(CL_INVALID_OPERATION);
}
// CL_EXEC_STATUS_ERROR_FOR_EVENTS_IN_WAIT_LIST if the read and write operations are blocking
// and the execution status of any of the events in event_wait_list is a negative integer value.
if block && evs.iter().any(|e| e.is_error()) {
return Err(CL_EXEC_STATUS_ERROR_FOR_EVENTS_IN_WAIT_LIST);
}
// CL_INVALID_VALUE if buffer_origin, host_origin, or region is NULL.
if buffer_origin.is_null() ||
host_origin.is_null() ||
region.is_null() ||
// CL_INVALID_VALUE if ptr is NULL.
ptr.is_null()
{
return Err(CL_INVALID_VALUE);
}
let r = unsafe { CLVec::from_raw(region) };
let buf_ori = unsafe { CLVec::from_raw(buffer_origin) };
let host_ori = unsafe { CLVec::from_raw(host_origin) };
// CL_INVALID_VALUE if any region array element is 0.
if r.contains(&0) ||
// CL_INVALID_VALUE if buffer_row_pitch is not 0 and is less than region[0].
buffer_row_pitch != 0 && buffer_row_pitch < r[0] ||
// CL_INVALID_VALUE if host_row_pitch is not 0 and is less than region[0].
host_row_pitch != 0 && host_row_pitch < r[0]
{
return Err(CL_INVALID_VALUE);
}
// If buffer_row_pitch is 0, buffer_row_pitch is computed as region[0].
if buffer_row_pitch == 0 {
buffer_row_pitch = r[0];
}
// If host_row_pitch is 0, host_row_pitch is computed as region[0].
if host_row_pitch == 0 {
host_row_pitch = r[0];
}
// CL_INVALID_VALUE if buffer_slice_pitch is not 0 and is less than region[1] × buffer_row_pitch and not a multiple of buffer_row_pitch.
if buffer_slice_pitch != 0 && buffer_slice_pitch < r[1] * buffer_row_pitch && buffer_slice_pitch % buffer_row_pitch != 0 ||
// CL_INVALID_VALUE if host_slice_pitch is not 0 and is less than region[1] × host_row_pitch and not a multiple of host_row_pitch.
host_slice_pitch != 0 && host_slice_pitch < r[1] * host_row_pitch && host_slice_pitch % host_row_pitch != 0
{
return Err(CL_INVALID_VALUE);
}
// If buffer_slice_pitch is 0, buffer_slice_pitch is computed as region[1] × buffer_row_pitch.
if buffer_slice_pitch == 0 {
buffer_slice_pitch = r[1] * buffer_row_pitch;
}
// If host_slice_pitch is 0, host_slice_pitch is computed as region[1] × host_row_pitch.
if host_slice_pitch == 0 {
host_slice_pitch = r[1] * host_row_pitch
}
// CL_INVALID_VALUE if the region being read or written specified by (buffer_origin, region,
// buffer_row_pitch, buffer_slice_pitch) is out of bounds.
if CLVec::calc_size(r + buf_ori, [1, buffer_row_pitch, buffer_slice_pitch]) > buf.size {
return Err(CL_INVALID_VALUE);
}
// CL_INVALID_CONTEXT if the context associated with command_queue and buffer are not the same
if q.context != buf.context {
return Err(CL_INVALID_CONTEXT);
}
create_and_queue(
q,
CL_COMMAND_READ_BUFFER_RECT,
evs,
event,
block,
Box::new(move |q, ctx| {
buf.read_to_user_rect(
ptr,
q,
ctx,
&r,
&buf_ori,
buffer_row_pitch,
buffer_slice_pitch,
&host_ori,
host_row_pitch,
host_slice_pitch,
)
}),
)
// TODO
// CL_MISALIGNED_SUB_BUFFER_OFFSET if buffer is a sub-buffer object and offset specified when the sub-buffer object is created is not aligned to CL_DEVICE_MEM_BASE_ADDR_ALIGN value for device associated with queue.
}
pub fn enqueue_write_buffer_rect(
command_queue: cl_command_queue,
buffer: cl_mem,
blocking_write: cl_bool,
buffer_origin: *const usize,
host_origin: *const usize,
region: *const usize,
mut buffer_row_pitch: usize,
mut buffer_slice_pitch: usize,
mut host_row_pitch: usize,
mut host_slice_pitch: usize,
ptr: *const ::std::os::raw::c_void,
num_events_in_wait_list: cl_uint,
event_wait_list: *const cl_event,
event: *mut cl_event,
) -> CLResult<()> {
let block = check_cl_bool(blocking_write).ok_or(CL_INVALID_VALUE)?;
let q = command_queue.get_arc()?;
let buf = buffer.get_arc()?;
let evs = event_list_from_cl(&q, num_events_in_wait_list, event_wait_list)?;
// CL_INVALID_OPERATION if clEnqueueWriteBufferRect is called on buffer which has been created
// with CL_MEM_HOST_READ_ONLY or CL_MEM_HOST_NO_ACCESS.
if bit_check(buf.flags, CL_MEM_HOST_READ_ONLY | CL_MEM_HOST_NO_ACCESS) {
return Err(CL_INVALID_OPERATION);
}
// CL_EXEC_STATUS_ERROR_FOR_EVENTS_IN_WAIT_LIST if the read and write operations are blocking
// and the execution status of any of the events in event_wait_list is a negative integer value.
if block && evs.iter().any(|e| e.is_error()) {
return Err(CL_EXEC_STATUS_ERROR_FOR_EVENTS_IN_WAIT_LIST);
}
// CL_INVALID_VALUE if buffer_origin, host_origin, or region is NULL.
if buffer_origin.is_null() ||
host_origin.is_null() ||
region.is_null() ||
// CL_INVALID_VALUE if ptr is NULL.
ptr.is_null()
{
return Err(CL_INVALID_VALUE);
}
let r = unsafe { CLVec::from_raw(region) };
let buf_ori = unsafe { CLVec::from_raw(buffer_origin) };
let host_ori = unsafe { CLVec::from_raw(host_origin) };
// CL_INVALID_VALUE if any region array element is 0.
if r.contains(&0) ||
// CL_INVALID_VALUE if buffer_row_pitch is not 0 and is less than region[0].
buffer_row_pitch != 0 && buffer_row_pitch < r[0] ||
// CL_INVALID_VALUE if host_row_pitch is not 0 and is less than region[0].
host_row_pitch != 0 && host_row_pitch < r[0]
{
return Err(CL_INVALID_VALUE);
}
// If buffer_row_pitch is 0, buffer_row_pitch is computed as region[0].
if buffer_row_pitch == 0 {
buffer_row_pitch = r[0];
}
// If host_row_pitch is 0, host_row_pitch is computed as region[0].
if host_row_pitch == 0 {
host_row_pitch = r[0];
}
// CL_INVALID_VALUE if buffer_slice_pitch is not 0 and is less than region[1] × buffer_row_pitch and not a multiple of buffer_row_pitch.
if buffer_slice_pitch != 0 && buffer_slice_pitch < r[1] * buffer_row_pitch && buffer_slice_pitch % buffer_row_pitch != 0 ||
// CL_INVALID_VALUE if host_slice_pitch is not 0 and is less than region[1] × host_row_pitch and not a multiple of host_row_pitch.
host_slice_pitch != 0 && host_slice_pitch < r[1] * host_row_pitch && host_slice_pitch % host_row_pitch != 0
{
return Err(CL_INVALID_VALUE);
}
// If buffer_slice_pitch is 0, buffer_slice_pitch is computed as region[1] × buffer_row_pitch.
if buffer_slice_pitch == 0 {
buffer_slice_pitch = r[1] * buffer_row_pitch;
}
// If host_slice_pitch is 0, host_slice_pitch is computed as region[1] × host_row_pitch.
if host_slice_pitch == 0 {
host_slice_pitch = r[1] * host_row_pitch
}
// CL_INVALID_VALUE if the region being read or written specified by (buffer_origin, region,
// buffer_row_pitch, buffer_slice_pitch) is out of bounds.
if CLVec::calc_size(r + buf_ori, [1, buffer_row_pitch, buffer_slice_pitch]) > buf.size {
return Err(CL_INVALID_VALUE);
}
// CL_INVALID_CONTEXT if the context associated with command_queue and buffer are not the same
if q.context != buf.context {
return Err(CL_INVALID_CONTEXT);
}
create_and_queue(
q,
CL_COMMAND_WRITE_BUFFER_RECT,
evs,
event,
block,
Box::new(move |q, ctx| {
buf.write_from_user_rect(
ptr,
q,
ctx,
&r,
&host_ori,
host_row_pitch,
host_slice_pitch,
&buf_ori,
buffer_row_pitch,
buffer_slice_pitch,
)
}),
)
// TODO
// CL_MISALIGNED_SUB_BUFFER_OFFSET if buffer is a sub-buffer object and offset specified when the sub-buffer object is created is not aligned to CL_DEVICE_MEM_BASE_ADDR_ALIGN value for device associated with queue.
}
pub fn enqueue_copy_buffer_rect(
command_queue: cl_command_queue,
src_buffer: cl_mem,
dst_buffer: cl_mem,
src_origin: *const usize,
dst_origin: *const usize,
region: *const usize,
mut src_row_pitch: usize,
mut src_slice_pitch: usize,
mut dst_row_pitch: usize,
mut dst_slice_pitch: usize,
num_events_in_wait_list: cl_uint,
event_wait_list: *const cl_event,
event: *mut cl_event,
) -> CLResult<()> {
let q = command_queue.get_arc()?;
let src = src_buffer.get_arc()?;
let dst = dst_buffer.get_arc()?;
let evs = event_list_from_cl(&q, num_events_in_wait_list, event_wait_list)?;
// CL_INVALID_VALUE if src_origin, dst_origin, or region is NULL.
if src_origin.is_null() || dst_origin.is_null() || region.is_null() {
return Err(CL_INVALID_VALUE);
}
let r = unsafe { CLVec::from_raw(region) };
let src_ori = unsafe { CLVec::from_raw(src_origin) };
let dst_ori = unsafe { CLVec::from_raw(dst_origin) };
// CL_INVALID_VALUE if any region array element is 0.
if r.contains(&0) ||
// CL_INVALID_VALUE if src_row_pitch is not 0 and is less than region[0].
src_row_pitch != 0 && src_row_pitch < r[0] ||
// CL_INVALID_VALUE if dst_row_pitch is not 0 and is less than region[0].
dst_row_pitch != 0 && dst_row_pitch < r[0]
{
return Err(CL_INVALID_VALUE);
}
// If src_row_pitch is 0, src_row_pitch is computed as region[0].
if src_row_pitch == 0 {
src_row_pitch = r[0];
}
// If dst_row_pitch is 0, dst_row_pitch is computed as region[0].
if dst_row_pitch == 0 {
dst_row_pitch = r[0];
}
// CL_INVALID_VALUE if src_slice_pitch is not 0 and is less than region[1] × src_row_pitch
if src_slice_pitch != 0 && src_slice_pitch < r[1] * src_row_pitch ||
// CL_INVALID_VALUE if dst_slice_pitch is not 0 and is less than region[1] × dst_row_pitch
dst_slice_pitch != 0 && dst_slice_pitch < r[1] * dst_row_pitch ||
// if src_slice_pitch is not 0 and is not a multiple of src_row_pitch.
src_slice_pitch != 0 && src_slice_pitch % src_row_pitch != 0 ||
// if dst_slice_pitch is not 0 and is not a multiple of dst_row_pitch.
dst_slice_pitch != 0 && dst_slice_pitch % dst_row_pitch != 0
{
return Err(CL_INVALID_VALUE);
}
// If src_slice_pitch is 0, src_slice_pitch is computed as region[1] × src_row_pitch.
if src_slice_pitch == 0 {
src_slice_pitch = r[1] * src_row_pitch;
}
// If dst_slice_pitch is 0, dst_slice_pitch is computed as region[1] × dst_row_pitch.
if dst_slice_pitch == 0 {
dst_slice_pitch = r[1] * dst_row_pitch;
}
// CL_INVALID_VALUE if src_buffer and dst_buffer are the same buffer object and src_slice_pitch
// is not equal to dst_slice_pitch and src_row_pitch is not equal to dst_row_pitch.
if src_buffer == dst_buffer
&& src_slice_pitch != dst_slice_pitch
&& src_row_pitch != dst_row_pitch
{
return Err(CL_INVALID_VALUE);
}
// CL_INVALID_VALUE if (src_origin, region, src_row_pitch, src_slice_pitch) or (dst_origin,
// region, dst_row_pitch, dst_slice_pitch) require accessing elements outside the src_buffer
// and dst_buffer buffer objects respectively.
if CLVec::calc_size(r + src_ori, [1, src_row_pitch, src_slice_pitch]) > src.size
|| CLVec::calc_size(r + dst_ori, [1, dst_row_pitch, dst_slice_pitch]) > dst.size
{
return Err(CL_INVALID_VALUE);
}
// CL_MEM_COPY_OVERLAP if src_buffer and dst_buffer are the same buffer or sub-buffer object and
// the source and destination regions overlap or if src_buffer and dst_buffer are different
// sub-buffers of the same associated buffer object and they overlap.
if src.has_same_parent(&dst)
&& check_copy_overlap(
&src_ori,
src.offset,
&dst_ori,
dst.offset,
&r,
src_row_pitch,
src_slice_pitch,
)
{
return Err(CL_MEM_COPY_OVERLAP);
}
// CL_INVALID_CONTEXT if the context associated with command_queue, src_buffer and dst_buffer
// are not the same
if src.context != q.context || dst.context != q.context {
return Err(CL_INVALID_CONTEXT);
}
create_and_queue(
q,
CL_COMMAND_COPY_BUFFER_RECT,
evs,
event,
false,
Box::new(move |q, ctx| {
src.copy_to_rect(
&dst,
q,
ctx,
&r,
&src_ori,
src_row_pitch,
src_slice_pitch,
&dst_ori,
dst_row_pitch,
dst_slice_pitch,
)
}),
)
// TODO
// CL_MISALIGNED_SUB_BUFFER_OFFSET if src_buffer is a sub-buffer object and offset specified when the sub-buffer object is created is not aligned to CL_DEVICE_MEM_BASE_ADDR_ALIGN value for device associated with queue.
}
pub fn enqueue_fill_buffer(
command_queue: cl_command_queue,
buffer: cl_mem,
pattern: *const ::std::os::raw::c_void,
pattern_size: usize,
offset: usize,
size: usize,
num_events_in_wait_list: cl_uint,
event_wait_list: *const cl_event,
event: *mut cl_event,
) -> CLResult<()> {
let q = command_queue.get_arc()?;
let b = buffer.get_arc()?;
let evs = event_list_from_cl(&q, num_events_in_wait_list, event_wait_list)?;
// CL_INVALID_VALUE if offset or offset + size require accessing elements outside the buffer
// buffer object respectively.
if offset + size > b.size {
return Err(CL_INVALID_VALUE);
}
// CL_INVALID_VALUE if pattern is NULL or if pattern_size is 0 or if pattern_size is not one of
// { 1, 2, 4, 8, 16, 32, 64, 128 }.
if pattern.is_null() || pattern_size.count_ones() != 1 || pattern_size > 128 {
return Err(CL_INVALID_VALUE);
}
// CL_INVALID_VALUE if offset and size are not a multiple of pattern_size.
if offset % pattern_size != 0 || size % pattern_size != 0 {
return Err(CL_INVALID_VALUE);
}
// CL_INVALID_CONTEXT if the context associated with command_queue and buffer are not the same
if b.context != q.context {
return Err(CL_INVALID_CONTEXT);
}
// we have to copy memory
let pattern = unsafe { slice::from_raw_parts(pattern.cast(), pattern_size).to_vec() };
create_and_queue(
q,
CL_COMMAND_FILL_BUFFER,
evs,
event,
false,
Box::new(move |q, ctx| b.fill(q, ctx, &pattern, offset, size)),
)
// TODO
//• CL_MISALIGNED_SUB_BUFFER_OFFSET if buffer is a sub-buffer object and offset specified when the sub-buffer object is created is not aligned to CL_DEVICE_MEM_BASE_ADDR_ALIGN value for device associated with queue.
//• CL_MEM_OBJECT_ALLOCATION_FAILURE if there is a failure to allocate memory for data store associated with buffer.
}
pub fn enqueue_map_buffer(
command_queue: cl_command_queue,
buffer: cl_mem,
blocking_map: cl_bool,
map_flags: cl_map_flags,
offset: usize,
size: usize,
num_events_in_wait_list: cl_uint,
event_wait_list: *const cl_event,
event: *mut cl_event,
) -> CLResult<*mut c_void> {
let q = command_queue.get_arc()?;
let b = buffer.get_arc()?;
let block = check_cl_bool(blocking_map).ok_or(CL_INVALID_VALUE)?;
let evs = event_list_from_cl(&q, num_events_in_wait_list, event_wait_list)?;
validate_map_flags(&b, map_flags)?;
// CL_INVALID_VALUE if region being mapped given by (offset, size) is out of bounds or if size
// is 0
if offset + size > b.size || size == 0 {
return Err(CL_INVALID_VALUE);
}
// CL_EXEC_STATUS_ERROR_FOR_EVENTS_IN_WAIT_LIST if the map operation is blocking and the
// execution status of any of the events in event_wait_list is a negative integer value.
if block && evs.iter().any(|e| e.is_error()) {
return Err(CL_EXEC_STATUS_ERROR_FOR_EVENTS_IN_WAIT_LIST);
}
// CL_INVALID_CONTEXT if context associated with command_queue and buffer are not the same
if b.context != q.context {
return Err(CL_INVALID_CONTEXT);
}
let ptr = b.map_buffer(&q, offset, size)?;
create_and_queue(
q,
CL_COMMAND_MAP_BUFFER,
evs,
event,
block,
Box::new(move |q, ctx| b.sync_shadow_buffer(q, ctx, ptr)),
)?;
Ok(ptr)
// TODO
// CL_MISALIGNED_SUB_BUFFER_OFFSET if buffer is a sub-buffer object and offset specified when the sub-buffer object is created is not aligned to CL_DEVICE_MEM_BASE_ADDR_ALIGN value for the device associated with queue. This error code is missing before version 1.1.
// CL_MAP_FAILURE if there is a failure to map the requested region into the host address space. This error cannot occur for buffer objects created with CL_MEM_USE_HOST_PTR or CL_MEM_ALLOC_HOST_PTR.
// CL_INVALID_OPERATION if mapping would lead to overlapping regions being mapped for writing.
}
pub fn enqueue_read_image(
command_queue: cl_command_queue,
image: cl_mem,
blocking_read: cl_bool,
origin: *const usize,
region: *const usize,
mut row_pitch: usize,
mut slice_pitch: usize,
ptr: *mut ::std::os::raw::c_void,
num_events_in_wait_list: cl_uint,
event_wait_list: *const cl_event,
event: *mut cl_event,
) -> CLResult<()> {
let q = command_queue.get_arc()?;
let i = image.get_arc()?;
let block = check_cl_bool(blocking_read).ok_or(CL_INVALID_VALUE)?;
let evs = event_list_from_cl(&q, num_events_in_wait_list, event_wait_list)?;
let pixel_size = i.image_format.pixel_size().unwrap() as usize;
// CL_INVALID_CONTEXT if the context associated with command_queue and image are not the same
if i.context != q.context {
return Err(CL_INVALID_CONTEXT);
}
// CL_INVALID_OPERATION if clEnqueueReadImage is called on image which has been created with
// CL_MEM_HOST_WRITE_ONLY or CL_MEM_HOST_NO_ACCESS.
if bit_check(i.flags, CL_MEM_HOST_WRITE_ONLY | CL_MEM_HOST_NO_ACCESS) {
return Err(CL_INVALID_OPERATION);
}
// CL_INVALID_VALUE if origin or region is NULL.
// CL_INVALID_VALUE if ptr is NULL.
if origin.is_null() || region.is_null() || ptr.is_null() {
return Err(CL_INVALID_VALUE);
}
// CL_INVALID_VALUE if image is a 1D or 2D image and slice_pitch or input_slice_pitch is not 0.
if !i.image_desc.has_slice() && slice_pitch != 0 {
return Err(CL_INVALID_VALUE);
}
let r = unsafe { CLVec::from_raw(region) };
let o = unsafe { CLVec::from_raw(origin) };
// CL_INVALID_VALUE if the region being read or written specified by origin and region is out of
// bounds.
// CL_INVALID_VALUE if values in origin and region do not follow rules described in the argument
// description for origin and region.
validate_image_bounds(&i, o, r)?;
// If row_pitch (or input_row_pitch) is set to 0, the appropriate row pitch is calculated based
// on the size of each element in bytes multiplied by width.
if row_pitch == 0 {
row_pitch = r[0] * pixel_size;
}
// If slice_pitch (or input_slice_pitch) is set to 0, the appropriate slice pitch is calculated
// based on the row_pitch × height.
if slice_pitch == 0 {
slice_pitch = row_pitch * r[1];
}
create_and_queue(
q,
CL_COMMAND_READ_IMAGE,
evs,
event,
block,
Box::new(move |q, ctx| {
i.read_to_user_rect(
ptr,
q,
ctx,
&r,
&o,
i.image_desc.image_row_pitch,
i.image_desc.image_slice_pitch,
&CLVec::default(),
row_pitch,
slice_pitch,
)
}),
)
//• CL_INVALID_IMAGE_SIZE if image dimensions (image width, height, specified or compute row and/or slice pitch) for image are not supported by device associated with queue.
//• CL_IMAGE_FORMAT_NOT_SUPPORTED if image format (image channel order and data type) for image are not supported by device associated with queue.
//• CL_INVALID_OPERATION if the device associated with command_queue does not support images (i.e. CL_DEVICE_IMAGE_SUPPORT specified in the Device Queries table is CL_FALSE).
//• CL_EXEC_STATUS_ERROR_FOR_EVENTS_IN_WAIT_LIST if the read and write operations are blocking and the execution status of any of the events in event_wait_list is a negative integer value.
}
pub fn enqueue_write_image(
command_queue: cl_command_queue,
image: cl_mem,
blocking_write: cl_bool,
origin: *const usize,
region: *const usize,
mut row_pitch: usize,
mut slice_pitch: usize,
ptr: *const ::std::os::raw::c_void,
num_events_in_wait_list: cl_uint,
event_wait_list: *const cl_event,
event: *mut cl_event,
) -> CLResult<()> {
let q = command_queue.get_arc()?;
let i = image.get_arc()?;
let block = check_cl_bool(blocking_write).ok_or(CL_INVALID_VALUE)?;
let evs = event_list_from_cl(&q, num_events_in_wait_list, event_wait_list)?;
let pixel_size = i.image_format.pixel_size().unwrap() as usize;
// CL_INVALID_CONTEXT if the context associated with command_queue and image are not the same
if i.context != q.context {
return Err(CL_INVALID_CONTEXT);
}
// CL_INVALID_OPERATION if clEnqueueWriteImage is called on image which has been created with
// CL_MEM_HOST_READ_ONLY or CL_MEM_HOST_NO_ACCESS.
if bit_check(i.flags, CL_MEM_HOST_READ_ONLY | CL_MEM_HOST_NO_ACCESS) {
return Err(CL_INVALID_OPERATION);
}
// CL_INVALID_VALUE if origin or region is NULL.
// CL_INVALID_VALUE if ptr is NULL.
if origin.is_null() || region.is_null() || ptr.is_null() {
return Err(CL_INVALID_VALUE);
}
// CL_INVALID_VALUE if image is a 1D or 2D image and slice_pitch or input_slice_pitch is not 0.
if !i.image_desc.has_slice() && slice_pitch != 0 {
return Err(CL_INVALID_VALUE);
}
let r = unsafe { CLVec::from_raw(region) };
let o = unsafe { CLVec::from_raw(origin) };
// CL_INVALID_VALUE if the region being read or written specified by origin and region is out of
// bounds.
// CL_INVALID_VALUE if values in origin and region do not follow rules described in the argument
// description for origin and region.
validate_image_bounds(&i, o, r)?;
// If row_pitch (or input_row_pitch) is set to 0, the appropriate row pitch is calculated based
// on the size of each element in bytes multiplied by width.
if row_pitch == 0 {
row_pitch = r[0] * pixel_size;
}
// If slice_pitch (or input_slice_pitch) is set to 0, the appropriate slice pitch is calculated
// based on the row_pitch × height.
if slice_pitch == 0 {
slice_pitch = row_pitch * r[1];
}
create_and_queue(
q,
CL_COMMAND_WRITE_BUFFER_RECT,
evs,
event,
block,
Box::new(move |q, ctx| {
i.write_from_user_rect(
ptr,
q,
ctx,
&r,
&CLVec::default(),
row_pitch,
slice_pitch,
&o,
i.image_desc.image_row_pitch,
i.image_desc.image_slice_pitch,
)
}),
)
//• CL_INVALID_IMAGE_SIZE if image dimensions (image width, height, specified or compute row and/or slice pitch) for image are not supported by device associated with queue.
//• CL_IMAGE_FORMAT_NOT_SUPPORTED if image format (image channel order and data type) for image are not supported by device associated with queue.
//• CL_INVALID_OPERATION if the device associated with command_queue does not support images (i.e. CL_DEVICE_IMAGE_SUPPORT specified in the Device Queries table is CL_FALSE).
//• CL_EXEC_STATUS_ERROR_FOR_EVENTS_IN_WAIT_LIST if the read and write operations are blocking and the execution status of any of the events in event_wait_list is a negative integer value.
}
pub fn enqueue_copy_image(
command_queue: cl_command_queue,
src_image: cl_mem,
dst_image: cl_mem,
src_origin: *const usize,
dst_origin: *const usize,
region: *const usize,
num_events_in_wait_list: cl_uint,
event_wait_list: *const cl_event,
event: *mut cl_event,
) -> CLResult<()> {
let q = command_queue.get_arc()?;
let src_image = src_image.get_arc()?;
let dst_image = dst_image.get_arc()?;
let evs = event_list_from_cl(&q, num_events_in_wait_list, event_wait_list)?;
// CL_INVALID_CONTEXT if the context associated with command_queue, src_image and dst_image are not the same
if src_image.context != q.context || dst_image.context != q.context {
return Err(CL_INVALID_CONTEXT);
}
// CL_IMAGE_FORMAT_MISMATCH if src_image and dst_image do not use the same image format.
if src_image.image_format != dst_image.image_format {
return Err(CL_IMAGE_FORMAT_MISMATCH);
}
// CL_INVALID_VALUE if src_origin, dst_origin, or region is NULL.
if src_origin.is_null() || dst_origin.is_null() || region.is_null() {
return Err(CL_INVALID_VALUE);
}
let region = unsafe { CLVec::from_raw(region) };
let dst_origin = unsafe { CLVec::from_raw(dst_origin) };
let src_origin = unsafe { CLVec::from_raw(src_origin) };
// CL_INVALID_VALUE if the 2D or 3D rectangular region specified by src_origin and
// src_origin + region refers to a region outside src_image, or if the 2D or 3D rectangular
// region specified by dst_origin and dst_origin + region refers to a region outside dst_image.
// CL_INVALID_VALUE if values in src_origin, dst_origin and region do not follow rules described
// in the argument description for src_origin, dst_origin and region.
validate_image_bounds(&src_image, src_origin, region)?;
validate_image_bounds(&dst_image, dst_origin, region)?;
create_and_queue(
q,
CL_COMMAND_COPY_IMAGE,
evs,
event,
false,
Box::new(move |q, ctx| {
src_image.copy_to(q, ctx, &dst_image, src_origin, dst_origin, ®ion)
}),
)
//• CL_INVALID_IMAGE_SIZE if image dimensions (image width, height, specified or compute row and/or slice pitch) for src_image or dst_image are not supported by device associated with queue.
//• CL_IMAGE_FORMAT_NOT_SUPPORTED if image format (image channel order and data type) for src_image or dst_image are not supported by device associated with queue.
//• CL_INVALID_OPERATION if the device associated with command_queue does not support images (i.e. CL_DEVICE_IMAGE_SUPPORT specified in the Device Queries table is CL_FALSE).
//• CL_MEM_COPY_OVERLAP if src_image and dst_image are the same image object and the source and destination regions overlap.
}
pub fn enqueue_fill_image(
command_queue: cl_command_queue,
image: cl_mem,
fill_color: *const ::std::os::raw::c_void,
origin: *const [usize; 3],
region: *const [usize; 3],
num_events_in_wait_list: cl_uint,
event_wait_list: *const cl_event,
event: *mut cl_event,
) -> CLResult<()> {
let q = command_queue.get_arc()?;
let i = image.get_arc()?;
let evs = event_list_from_cl(&q, num_events_in_wait_list, event_wait_list)?;
// CL_INVALID_CONTEXT if the context associated with command_queue and image are not the same
if i.context != q.context {
return Err(CL_INVALID_CONTEXT);
}
// CL_INVALID_VALUE if fill_color is NULL.
// CL_INVALID_VALUE if origin or region is NULL.
if fill_color.is_null() || origin.is_null() || region.is_null() {
return Err(CL_INVALID_VALUE);
}
let region = unsafe { CLVec::from_raw(region.cast()) };
let origin = unsafe { CLVec::from_raw(origin.cast()) };
// CL_INVALID_VALUE if the region being filled as specified by origin and region is out of
// bounds.
// CL_INVALID_VALUE if values in origin and region do not follow rules described in the argument
// description for origin and region.
validate_image_bounds(&i, origin, region)?;
// we have to copy memory and it's always a 4 component int value
// TODO but not for CL_DEPTH
let fill_color = unsafe { slice::from_raw_parts(fill_color.cast(), 4).to_vec() };
create_and_queue(
q,
CL_COMMAND_FILL_BUFFER,
evs,
event,
false,
Box::new(move |q, ctx| i.fill_image(q, ctx, &fill_color, &origin, ®ion)),
)
//• CL_INVALID_IMAGE_SIZE if image dimensions (image width, height, specified or compute row and/or slice pitch) for image are not supported by device associated with queue.
//• CL_IMAGE_FORMAT_NOT_SUPPORTED if image format (image channel order and data type) for
//image are not supported by device associated with queue.
}
pub fn enqueue_copy_buffer_to_image(
command_queue: cl_command_queue,
src_buffer: cl_mem,
dst_image: cl_mem,
src_offset: usize,
dst_origin: *const usize,
region: *const usize,
num_events_in_wait_list: cl_uint,
event_wait_list: *const cl_event,
event: *mut cl_event,
) -> CLResult<()> {
let q = command_queue.get_arc()?;
let src = src_buffer.get_arc()?;
let dst = dst_image.get_arc()?;
let evs = event_list_from_cl(&q, num_events_in_wait_list, event_wait_list)?;
// CL_INVALID_CONTEXT if the context associated with command_queue, src_buffer and dst_image
// are not the same
if q.context != src.context || q.context != dst.context {
return Err(CL_INVALID_CONTEXT);
}
// CL_INVALID_VALUE if dst_origin or region is NULL.
if dst_origin.is_null() || region.is_null() {
return Err(CL_INVALID_VALUE);
}
let region = unsafe { CLVec::from_raw(region) };
let src_origin = CLVec::new([src_offset, 0, 0]);
let dst_origin = unsafe { CLVec::from_raw(dst_origin) };
// CL_INVALID_VALUE if values in dst_origin and region do not follow rules described in the
// argument description for dst_origin and region.
// CL_INVALID_VALUE if the 1D, 2D or 3D rectangular region specified by dst_origin and
// dst_origin + region refer to a region outside dst_image,
validate_image_bounds(&dst, dst_origin, region)?;
create_and_queue(
q,
CL_COMMAND_COPY_BUFFER_TO_IMAGE,
evs,
event,
false,
Box::new(move |q, ctx| src.copy_to(q, ctx, &dst, src_origin, dst_origin, ®ion)),
)
//• CL_INVALID_MEM_OBJECT if src_buffer is not a valid buffer object or dst_image is not a valid image object or if dst_image is a 1D image buffer object created from src_buffer.
//• CL_INVALID_VALUE ... if the region specified by src_offset and src_offset + src_cb refer to a region outside src_buffer.
//• CL_MISALIGNED_SUB_BUFFER_OFFSET if src_buffer is a sub-buffer object and offset specified when the sub-buffer object is created is not aligned to CL_DEVICE_MEM_BASE_ADDR_ALIGN value for device associated with queue.
//• CL_INVALID_IMAGE_SIZE if image dimensions (image width, height, specified or compute row and/or slice pitch) for dst_image are not supported by device associated with queue.
//• CL_IMAGE_FORMAT_NOT_SUPPORTED if image format (image channel order and data type) for dst_image are not supported by device associated with queue.
//• CL_MEM_OBJECT_ALLOCATION_FAILURE if there is a failure to allocate memory for data store associated with src_buffer or dst_image.
//• CL_INVALID_OPERATION if the device associated with command_queue does not support images (i.e. CL_DEVICE_IMAGE_SUPPORT specified in the Device Queries table is CL_FALSE).
}
pub fn enqueue_copy_image_to_buffer(
command_queue: cl_command_queue,
src_image: cl_mem,
dst_buffer: cl_mem,
src_origin: *const usize,
region: *const usize,
dst_offset: usize,
num_events_in_wait_list: cl_uint,
event_wait_list: *const cl_event,
event: *mut cl_event,
) -> CLResult<()> {
let q = command_queue.get_arc()?;
let src = src_image.get_arc()?;
let dst = dst_buffer.get_arc()?;
let evs = event_list_from_cl(&q, num_events_in_wait_list, event_wait_list)?;
// CL_INVALID_CONTEXT if the context associated with command_queue, src_image and dst_buffer
// are not the same
if q.context != src.context || q.context != dst.context {
return Err(CL_INVALID_CONTEXT);
}
// CL_INVALID_VALUE if src_origin or region is NULL.
if src_origin.is_null() || region.is_null() {
return Err(CL_INVALID_VALUE);
}
let region = unsafe { CLVec::from_raw(region) };
let src_origin = unsafe { CLVec::from_raw(src_origin) };
let dst_origin = CLVec::new([dst_offset, 0, 0]);
// CL_INVALID_VALUE if values in src_origin and region do not follow rules described in the
// argument description for src_origin and region.
// CL_INVALID_VALUE if the 1D, 2D or 3D rectangular region specified by src_origin and
// src_origin + region refers to a region outside src_image, or if the region specified by
// dst_offset and dst_offset + dst_cb to a region outside dst_buffer.
validate_image_bounds(&src, src_origin, region)?;
create_and_queue(
q,
CL_COMMAND_COPY_IMAGE_TO_BUFFER,
evs,
event,
false,
Box::new(move |q, ctx| src.copy_to(q, ctx, &dst, src_origin, dst_origin, ®ion)),
)
//• CL_INVALID_MEM_OBJECT if src_image is not a valid image object or dst_buffer is not a valid buffer object or if src_image is a 1D image buffer object created from dst_buffer.
//• CL_INVALID_VALUE ... if the region specified by dst_offset and dst_offset + dst_cb to a region outside dst_buffer.
//• CL_MISALIGNED_SUB_BUFFER_OFFSET if dst_buffer is a sub-buffer object and offset specified when the sub-buffer object is created is not aligned to CL_DEVICE_MEM_BASE_ADDR_ALIGN value for device associated with queue. This error code is missing before version 1.1.
//• CL_INVALID_IMAGE_SIZE if image dimensions (image width, height, specified or compute row and/or slice pitch) for src_image are not supported by device associated with queue.
//• CL_IMAGE_FORMAT_NOT_SUPPORTED if image format (image channel order and data type) for src_image are not supported by device associated with queue.
//• CL_MEM_OBJECT_ALLOCATION_FAILURE if there is a failure to allocate memory for data store associated with src_image or dst_buffer.
//• CL_INVALID_OPERATION if the device associated with command_queue does not support images (i.e. CL_DEVICE_IMAGE_SUPPORT specified in the Device Queries table is CL_FALSE).
}
pub fn enqueue_map_image(
command_queue: cl_command_queue,
image: cl_mem,
blocking_map: cl_bool,
map_flags: cl_map_flags,
origin: *const usize,
region: *const usize,
image_row_pitch: *mut usize,
image_slice_pitch: *mut usize,
num_events_in_wait_list: cl_uint,
event_wait_list: *const cl_event,
event: *mut cl_event,
) -> CLResult<*mut ::std::os::raw::c_void> {
let q = command_queue.get_arc()?;
let i = image.get_arc()?;
let block = check_cl_bool(blocking_map).ok_or(CL_INVALID_VALUE)?;
let evs = event_list_from_cl(&q, num_events_in_wait_list, event_wait_list)?;
// CL_INVALID_VALUE ... or if values specified in map_flags are not valid.
validate_map_flags(&i, map_flags)?;
// CL_INVALID_CONTEXT if context associated with command_queue and image are not the same
if i.context != q.context {
return Err(CL_INVALID_CONTEXT);
}
// CL_INVALID_VALUE if origin or region is NULL.
// CL_INVALID_VALUE if image_row_pitch is NULL.
if origin.is_null() || region.is_null() || image_row_pitch.is_null() {
return Err(CL_INVALID_VALUE);
}
let region = unsafe { CLVec::from_raw(region) };
let origin = unsafe { CLVec::from_raw(origin) };
// CL_INVALID_VALUE if region being mapped given by (origin, origin + region) is out of bounds
// CL_INVALID_VALUE if values in origin and region do not follow rules described in the argument
// description for origin and region.
validate_image_bounds(&i, origin, region)?;
let mut dummy_slice_pitch: usize = 0;
let image_slice_pitch = if image_slice_pitch.is_null() {
// CL_INVALID_VALUE if image is a 3D image, 1D or 2D image array object and
// image_slice_pitch is NULL.
if i.image_desc.is_array() || i.image_desc.image_type == CL_MEM_OBJECT_IMAGE3D {
return Err(CL_INVALID_VALUE);
}
&mut dummy_slice_pitch
} else {
unsafe { image_slice_pitch.as_mut().unwrap() }
};
let ptr = i.map_image(
&q,
&origin,
®ion,
unsafe { image_row_pitch.as_mut().unwrap() },
image_slice_pitch,
)?;
create_and_queue(
q.clone(),
CL_COMMAND_MAP_IMAGE,
evs,
event,
block,
Box::new(move |q, ctx| i.sync_shadow_image(q, ctx, ptr)),
)?;
Ok(ptr)
//• CL_INVALID_IMAGE_SIZE if image dimensions (image width, height, specified or compute row and/or slice pitch) for image are not supported by device associated with queue.
//• CL_IMAGE_FORMAT_NOT_SUPPORTED if image format (image channel order and data type) for image are not supported by device associated with queue.
//• CL_MAP_FAILURE if there is a failure to map the requested region into the host address space. This error cannot occur for image objects created with CL_MEM_USE_HOST_PTR or CL_MEM_ALLOC_HOST_PTR.
//• CL_EXEC_STATUS_ERROR_FOR_EVENTS_IN_WAIT_LIST if the map operation is blocking and the execution status of any of the events in event_wait_list is a negative integer value.
//• CL_INVALID_OPERATION if the device associated with command_queue does not support images (i.e. CL_DEVICE_IMAGE_SUPPORT specified in the Device Queries table is CL_FALSE).
//• CL_INVALID_OPERATION if mapping would lead to overlapping regions being mapped for writing.
}
pub fn enqueue_unmap_mem_object(
command_queue: cl_command_queue,
memobj: cl_mem,
mapped_ptr: *mut ::std::os::raw::c_void,
num_events_in_wait_list: cl_uint,
event_wait_list: *const cl_event,
event: *mut cl_event,
) -> CLResult<()> {
let q = command_queue.get_arc()?;
let m = memobj.get_arc()?;
let evs = event_list_from_cl(&q, num_events_in_wait_list, event_wait_list)?;
// CL_INVALID_CONTEXT if context associated with command_queue and memobj are not the same
if q.context != m.context {
return Err(CL_INVALID_CONTEXT);
}
// CL_INVALID_VALUE if mapped_ptr is not a valid pointer returned by clEnqueueMapBuffer or
// clEnqueueMapImage for memobj.
if !m.is_mapped_ptr(mapped_ptr) {
return Err(CL_INVALID_VALUE);
}
create_and_queue(
q,
CL_COMMAND_UNMAP_MEM_OBJECT,
evs,
event,
false,
Box::new(move |q, ctx| m.unmap(q, ctx, mapped_ptr)),
)
}
pub fn enqueue_migrate_mem_objects(
command_queue: cl_command_queue,
num_mem_objects: cl_uint,
mem_objects: *const cl_mem,
flags: cl_mem_migration_flags,
num_events_in_wait_list: cl_uint,
event_wait_list: *const cl_event,
event: *mut cl_event,
) -> CLResult<()> {
let q = command_queue.get_arc()?;
let evs = event_list_from_cl(&q, num_events_in_wait_list, event_wait_list)?;
let bufs = cl_mem::get_arc_vec_from_arr(mem_objects, num_mem_objects)?;
// CL_INVALID_VALUE if num_mem_objects is zero or if mem_objects is NULL.
if bufs.is_empty() {
return Err(CL_INVALID_VALUE);
}
// CL_INVALID_CONTEXT if the context associated with command_queue and memory objects in
// mem_objects are not the same
if bufs.iter().any(|b| b.context != q.context) {
return Err(CL_INVALID_CONTEXT);
}
// CL_INVALID_VALUE if flags is not 0 or is not any of the values described in the table above.
if flags != 0
&& bit_check(
flags,
!(CL_MIGRATE_MEM_OBJECT_HOST | CL_MIGRATE_MEM_OBJECT_CONTENT_UNDEFINED),
)
{
return Err(CL_INVALID_VALUE);
}
// we should do something, but it's legal to not do anything at all
create_and_queue(
q,
CL_COMMAND_MIGRATE_MEM_OBJECTS,
evs,
event,
false,
Box::new(|_, _| Ok(())),
)
//• CL_MEM_OBJECT_ALLOCATION_FAILURE if there is a failure to allocate memory for the specified set of memory objects in mem_objects.
}
impl CLInfo<cl_pipe_info> for cl_mem {
fn query(&self, _q: cl_pipe_info, _: &[u8]) -> CLResult<Vec<u8>> {
// CL_INVALID_MEM_OBJECT if pipe is a not a valid pipe object.
Err(CL_INVALID_MEM_OBJECT)
}
}
pub fn svm_alloc(
context: cl_context,
flags: cl_svm_mem_flags,
size: usize,
mut alignment: cl_uint,
) -> CLResult<*mut c_void> {
// clSVMAlloc will fail if
// context is not a valid context
let c = context.get_ref()?;
// or no devices in context support SVM.
if !c.has_svm_devs() {
return Err(CL_INVALID_OPERATION);
}
// flags does not contain CL_MEM_SVM_FINE_GRAIN_BUFFER but does contain CL_MEM_SVM_ATOMICS.
if !bit_check(flags, CL_MEM_SVM_FINE_GRAIN_BUFFER) && bit_check(flags, CL_MEM_SVM_ATOMICS) {
return Err(CL_INVALID_VALUE);
}
// size is 0 or > CL_DEVICE_MAX_MEM_ALLOC_SIZE value for any device in context.
if size == 0 || checked_compare(size, Ordering::Greater, c.max_mem_alloc()) {
return Err(CL_INVALID_VALUE);
}
if alignment == 0 {
alignment = mem::size_of::<[u64; 16]>() as cl_uint;
}
// alignment is not a power of two
if !alignment.is_power_of_two() {
return Err(CL_INVALID_VALUE);
}
let layout;
let ptr;
// SAFETY: we already verify the parameters to from_size_align above and layout is of non zero
// size
unsafe {
layout = Layout::from_size_align_unchecked(size, alignment as usize);
ptr = alloc::alloc(layout);
}
if ptr.is_null() {
return Err(CL_OUT_OF_HOST_MEMORY);
}
c.add_svm_ptr(ptr.cast(), layout);
Ok(ptr.cast())
// Values specified in flags do not follow rules described for supported values in the SVM Memory Flags table.
// CL_MEM_SVM_FINE_GRAIN_BUFFER or CL_MEM_SVM_ATOMICS is specified in flags and these are not supported by at least one device in context.
// The values specified in flags are not valid, i.e. don’t match those defined in the SVM Memory Flags table.
// the OpenCL implementation cannot support the specified alignment for at least one device in context.
// There was a failure to allocate resources.
}
fn svm_free_impl(c: &Context, svm_pointer: *mut c_void) {
if let Some(layout) = c.remove_svm_ptr(svm_pointer) {
// SAFETY: we make sure that svm_pointer is a valid allocation and reuse the same layout
// from the allocation
unsafe {
alloc::dealloc(svm_pointer.cast(), layout);
}
}
}
pub fn svm_free(context: cl_context, svm_pointer: *mut c_void) -> CLResult<()> {
let c = context.get_ref()?;
svm_free_impl(c, svm_pointer);
Ok(())
}
fn enqueue_svm_free_impl(
command_queue: cl_command_queue,
num_svm_pointers: cl_uint,
svm_pointers: *mut *mut c_void,
pfn_free_func: Option<SVMFreeCb>,
user_data: *mut c_void,
num_events_in_wait_list: cl_uint,
event_wait_list: *const cl_event,
event: *mut cl_event,
cmd_type: cl_command_type,
) -> CLResult<()> {
let q = command_queue.get_arc()?;
let evs = event_list_from_cl(&q, num_events_in_wait_list, event_wait_list)?;
// CL_INVALID_VALUE if num_svm_pointers is 0 and svm_pointers is non-NULL, or if svm_pointers is
// NULL and num_svm_pointers is not 0.
if num_svm_pointers == 0 && !svm_pointers.is_null()
|| num_svm_pointers != 0 && svm_pointers.is_null()
{
return Err(CL_INVALID_VALUE);
}
// CL_INVALID_OPERATION if the device associated with command queue does not support SVM.
if !q.device.svm_supported() {
return Err(CL_INVALID_OPERATION);
}
create_and_queue(
q,
cmd_type,
evs,
event,
false,
Box::new(move |q, _| {
if let Some(cb) = pfn_free_func {
// SAFETY: it's undefined behavior if the application screws up
unsafe {
cb(command_queue, num_svm_pointers, svm_pointers, user_data);
}
} else {
// SAFETY: num_svm_pointers specifies the amount of elements in svm_pointers
let svm_pointers =
unsafe { slice::from_raw_parts(svm_pointers, num_svm_pointers as usize) };
for &ptr in svm_pointers {
svm_free_impl(&q.context, ptr);
}
}
Ok(())
}),
)
}
pub fn enqueue_svm_free(
command_queue: cl_command_queue,
num_svm_pointers: cl_uint,
svm_pointers: *mut *mut c_void,
pfn_free_func: Option<SVMFreeCb>,
user_data: *mut c_void,
num_events_in_wait_list: cl_uint,
event_wait_list: *const cl_event,
event: *mut cl_event,
) -> CLResult<()> {
enqueue_svm_free_impl(
command_queue,
num_svm_pointers,
svm_pointers,
pfn_free_func,
user_data,
num_events_in_wait_list,
event_wait_list,
event,
CL_COMMAND_SVM_FREE,
)
}
pub fn enqueue_svm_free_arm(
command_queue: cl_command_queue,
num_svm_pointers: cl_uint,
svm_pointers: *mut *mut c_void,
pfn_free_func: Option<SVMFreeCb>,
user_data: *mut c_void,
num_events_in_wait_list: cl_uint,
event_wait_list: *const cl_event,
event: *mut cl_event,
) -> CLResult<()> {
enqueue_svm_free_impl(
command_queue,
num_svm_pointers,
svm_pointers,
pfn_free_func,
user_data,
num_events_in_wait_list,
event_wait_list,
event,
CL_COMMAND_SVM_FREE_ARM,
)
}
fn enqueue_svm_memcpy_impl(
command_queue: cl_command_queue,
blocking_copy: cl_bool,
dst_ptr: *mut c_void,
src_ptr: *const c_void,
size: usize,
num_events_in_wait_list: cl_uint,
event_wait_list: *const cl_event,
event: *mut cl_event,
cmd_type: cl_command_type,
) -> CLResult<()> {
let q = command_queue.get_arc()?;
let evs = event_list_from_cl(&q, num_events_in_wait_list, event_wait_list)?;
let block = check_cl_bool(blocking_copy).ok_or(CL_INVALID_VALUE)?;
// CL_INVALID_OPERATION if the device associated with command queue does not support SVM.
if !q.device.svm_supported() {
return Err(CL_INVALID_OPERATION);
}
// CL_INVALID_VALUE if dst_ptr or src_ptr is NULL.
if dst_ptr.is_null() || src_ptr.is_null() {
return Err(CL_INVALID_VALUE);
}
// CL_MEM_COPY_OVERLAP if the values specified for dst_ptr, src_ptr and size result in an
// overlapping copy.
let dst_ptr_addr = dst_ptr as usize;
let src_ptr_addr = src_ptr as usize;
if (src_ptr_addr <= dst_ptr_addr && dst_ptr_addr < src_ptr_addr + size)
|| (dst_ptr_addr <= src_ptr_addr && src_ptr_addr < dst_ptr_addr + size)
{
return Err(CL_MEM_COPY_OVERLAP);
}
create_and_queue(
q,
cmd_type,
evs,
event,
block,
Box::new(move |_, _| {
// SAFETY: We check for overlapping copies already and alignment doesn't matter for void
// pointers. And we also trust applications to provide properly allocated memory regions
// and if not it's all undefined anyway.
unsafe {
ptr::copy_nonoverlapping(src_ptr, dst_ptr, size);
}
Ok(())
}),
)
}
pub fn enqueue_svm_memcpy(
command_queue: cl_command_queue,
blocking_copy: cl_bool,
dst_ptr: *mut c_void,
src_ptr: *const c_void,
size: usize,
num_events_in_wait_list: cl_uint,
event_wait_list: *const cl_event,
event: *mut cl_event,
) -> CLResult<()> {
enqueue_svm_memcpy_impl(
command_queue,
blocking_copy,
dst_ptr,
src_ptr,
size,
num_events_in_wait_list,
event_wait_list,
event,
CL_COMMAND_SVM_MEMCPY,
)
}
pub fn enqueue_svm_memcpy_arm(
command_queue: cl_command_queue,
blocking_copy: cl_bool,
dst_ptr: *mut c_void,
src_ptr: *const c_void,
size: usize,
num_events_in_wait_list: cl_uint,
event_wait_list: *const cl_event,
event: *mut cl_event,
) -> CLResult<()> {
enqueue_svm_memcpy_impl(
command_queue,
blocking_copy,
dst_ptr,
src_ptr,
size,
num_events_in_wait_list,
event_wait_list,
event,
CL_COMMAND_SVM_MEMCPY_ARM,
)
}
fn enqueue_svm_mem_fill_impl(
command_queue: cl_command_queue,
svm_ptr: *mut ::std::os::raw::c_void,
pattern: *const ::std::os::raw::c_void,
pattern_size: usize,
size: usize,
num_events_in_wait_list: cl_uint,
event_wait_list: *const cl_event,
event: *mut cl_event,
cmd_type: cl_command_type,
) -> CLResult<()> {
let q = command_queue.get_arc()?;
let evs = event_list_from_cl(&q, num_events_in_wait_list, event_wait_list)?;
let svm_ptr_addr = svm_ptr as usize;
// CL_INVALID_OPERATION if the device associated with command queue does not support SVM.
if !q.device.svm_supported() {
return Err(CL_INVALID_OPERATION);
}
// CL_INVALID_VALUE if svm_ptr is NULL.
if svm_ptr.is_null() {
return Err(CL_INVALID_VALUE);
}
// CL_INVALID_VALUE if svm_ptr is not aligned to pattern_size bytes.
if svm_ptr_addr & (pattern_size - 1) != 0 {
return Err(CL_INVALID_VALUE);
}
// CL_INVALID_VALUE if pattern is NULL or if pattern_size is 0 or if pattern_size is not one of
// {1, 2, 4, 8, 16, 32, 64, 128}.
if pattern.is_null()
|| pattern_size == 0
|| !pattern_size.is_power_of_two()
|| pattern_size > 128
{
return Err(CL_INVALID_VALUE);
}
// CL_INVALID_VALUE if size is not a multiple of pattern_size.
if size % pattern_size != 0 {
return Err(CL_INVALID_VALUE);
}
create_and_queue(
q,
cmd_type,
evs,
event,
false,
Box::new(move |_, _| {
let mut offset = 0;
while offset < size {
// SAFETY: pointer are either valid or undefined behavior
unsafe {
ptr::copy(pattern, svm_ptr.add(offset), pattern_size);
}
offset += pattern_size;
}
Ok(())
}),
)
}
pub fn enqueue_svm_mem_fill(
command_queue: cl_command_queue,
svm_ptr: *mut ::std::os::raw::c_void,
pattern: *const ::std::os::raw::c_void,
pattern_size: usize,
size: usize,
num_events_in_wait_list: cl_uint,
event_wait_list: *const cl_event,
event: *mut cl_event,
) -> CLResult<()> {
enqueue_svm_mem_fill_impl(
command_queue,
svm_ptr,
pattern,
pattern_size,
size,
num_events_in_wait_list,
event_wait_list,
event,
CL_COMMAND_SVM_MEMFILL,
)
}
pub fn enqueue_svm_mem_fill_arm(
command_queue: cl_command_queue,
svm_ptr: *mut ::std::os::raw::c_void,
pattern: *const ::std::os::raw::c_void,
pattern_size: usize,
size: usize,
num_events_in_wait_list: cl_uint,
event_wait_list: *const cl_event,
event: *mut cl_event,
) -> CLResult<()> {
enqueue_svm_mem_fill_impl(
command_queue,
svm_ptr,
pattern,
pattern_size,
size,
num_events_in_wait_list,
event_wait_list,
event,
CL_COMMAND_SVM_MEMFILL_ARM,
)
}
fn enqueue_svm_map_impl(
command_queue: cl_command_queue,
blocking_map: cl_bool,
flags: cl_map_flags,
svm_ptr: *mut ::std::os::raw::c_void,
size: usize,
num_events_in_wait_list: cl_uint,
event_wait_list: *const cl_event,
event: *mut cl_event,
cmd_type: cl_command_type,
) -> CLResult<()> {
let q = command_queue.get_arc()?;
let evs = event_list_from_cl(&q, num_events_in_wait_list, event_wait_list)?;
let block = check_cl_bool(blocking_map).ok_or(CL_INVALID_VALUE)?;
// CL_INVALID_OPERATION if the device associated with command queue does not support SVM.
if !q.device.svm_supported() {
return Err(CL_INVALID_OPERATION);
}
// CL_INVALID_VALUE if svm_ptr is NULL.
if svm_ptr.is_null() {
return Err(CL_INVALID_VALUE);
}
// CL_INVALID_VALUE if size is 0 ...
if size == 0 {
return Err(CL_INVALID_VALUE);
}
// ... or if values specified in map_flags are not valid.
validate_map_flags_common(flags)?;
create_and_queue(q, cmd_type, evs, event, block, Box::new(|_, _| Ok(())))
}
pub fn enqueue_svm_map(
command_queue: cl_command_queue,
blocking_map: cl_bool,
flags: cl_map_flags,
svm_ptr: *mut ::std::os::raw::c_void,
size: usize,
num_events_in_wait_list: cl_uint,
event_wait_list: *const cl_event,
event: *mut cl_event,
) -> CLResult<()> {
enqueue_svm_map_impl(
command_queue,
blocking_map,
flags,
svm_ptr,
size,
num_events_in_wait_list,
event_wait_list,
event,
CL_COMMAND_SVM_MAP,
)
}
pub fn enqueue_svm_map_arm(
command_queue: cl_command_queue,
blocking_map: cl_bool,
flags: cl_map_flags,
svm_ptr: *mut ::std::os::raw::c_void,
size: usize,
num_events_in_wait_list: cl_uint,
event_wait_list: *const cl_event,
event: *mut cl_event,
) -> CLResult<()> {
enqueue_svm_map_impl(
command_queue,
blocking_map,
flags,
svm_ptr,
size,
num_events_in_wait_list,
event_wait_list,
event,
CL_COMMAND_SVM_MAP_ARM,
)
}
fn enqueue_svm_unmap_impl(
command_queue: cl_command_queue,
svm_ptr: *mut ::std::os::raw::c_void,
num_events_in_wait_list: cl_uint,
event_wait_list: *const cl_event,
event: *mut cl_event,
cmd_type: cl_command_type,
) -> CLResult<()> {
let q = command_queue.get_arc()?;
let evs = event_list_from_cl(&q, num_events_in_wait_list, event_wait_list)?;
// CL_INVALID_OPERATION if the device associated with command queue does not support SVM.
if !q.device.svm_supported() {
return Err(CL_INVALID_OPERATION);
}
// CL_INVALID_VALUE if svm_ptr is NULL.
if svm_ptr.is_null() {
return Err(CL_INVALID_VALUE);
}
create_and_queue(q, cmd_type, evs, event, false, Box::new(|_, _| Ok(())))
}
pub fn enqueue_svm_unmap(
command_queue: cl_command_queue,
svm_ptr: *mut ::std::os::raw::c_void,
num_events_in_wait_list: cl_uint,
event_wait_list: *const cl_event,
event: *mut cl_event,
) -> CLResult<()> {
enqueue_svm_unmap_impl(
command_queue,
svm_ptr,
num_events_in_wait_list,
event_wait_list,
event,
CL_COMMAND_SVM_UNMAP,
)
}
pub fn enqueue_svm_unmap_arm(
command_queue: cl_command_queue,
svm_ptr: *mut ::std::os::raw::c_void,
num_events_in_wait_list: cl_uint,
event_wait_list: *const cl_event,
event: *mut cl_event,
) -> CLResult<()> {
enqueue_svm_unmap_impl(
command_queue,
svm_ptr,
num_events_in_wait_list,
event_wait_list,
event,
CL_COMMAND_SVM_UNMAP_ARM,
)
}
pub fn enqueue_svm_migrate_mem(
command_queue: cl_command_queue,
num_svm_pointers: cl_uint,
svm_pointers: *mut *const ::std::os::raw::c_void,
sizes: *const usize,
flags: cl_mem_migration_flags,
num_events_in_wait_list: cl_uint,
event_wait_list: *const cl_event,
event: *mut cl_event,
) -> CLResult<()> {
let q = command_queue.get_arc()?;
let evs = event_list_from_cl(&q, num_events_in_wait_list, event_wait_list)?;
// CL_INVALID_OPERATION if the device associated with command queue does not support SVM.
if !q.device.svm_supported() {
return Err(CL_INVALID_OPERATION);
}
// CL_INVALID_VALUE if num_svm_pointers is zero or svm_pointers is NULL.
if num_svm_pointers == 0 || svm_pointers.is_null() {
return Err(CL_INVALID_VALUE);
}
let num_svm_pointers = num_svm_pointers as usize;
// SAFETY: Just hoping the application is alright.
let mut svm_pointers =
unsafe { slice::from_raw_parts(svm_pointers, num_svm_pointers) }.to_owned();
// if sizes is NULL, every allocation containing the pointers need to be migrated
let mut sizes = if sizes.is_null() {
vec![0; num_svm_pointers]
} else {
unsafe { slice::from_raw_parts(sizes, num_svm_pointers) }.to_owned()
};
// CL_INVALID_VALUE if sizes[i] is non-zero range [svm_pointers[i], svm_pointers[i]+sizes[i]) is
// not contained within an existing clSVMAlloc allocation.
for (ptr, size) in svm_pointers.iter_mut().zip(&mut sizes) {
if let Some((alloc, layout)) = q.context.find_svm_alloc(ptr.cast()) {
let ptr_addr = *ptr as usize;
let alloc_addr = alloc as usize;
// if the offset + size is bigger than the allocation we are out of bounds
if (ptr_addr - alloc_addr) + *size <= layout.size() {
// if the size is 0, the entire allocation should be migrated
if *size == 0 {
*ptr = alloc.cast();
*size = layout.size();
}
continue;
}
}
return Err(CL_INVALID_VALUE);
}
let to_device = !bit_check(flags, CL_MIGRATE_MEM_OBJECT_HOST);
let content_undefined = bit_check(flags, CL_MIGRATE_MEM_OBJECT_CONTENT_UNDEFINED);
create_and_queue(
q,
CL_COMMAND_SVM_MIGRATE_MEM,
evs,
event,
false,
Box::new(move |_, ctx| {
ctx.svm_migrate(&svm_pointers, &sizes, to_device, content_undefined);
Ok(())
}),
)
}
|