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
//! An OpenCL Image.
//!
//
// * NOTE: `Image` does not have the latest command builders nor does it have
// support for mapping yet. (TODO: implement)
//
use crate::core::{
self, AsMem, ImageChannelDataType, ImageChannelOrder, ImageDescriptor, ImageFormat,
ImageFormatParseResult, ImageInfo, ImageInfoResult, MapFlags, Mem as MemCore, MemCmdAll,
MemCmdRw, MemFlags, MemInfo, MemInfoResult, MemObjectType, OclPrm,
};
use crate::error::{Error as OclError, Result as OclResult};
use crate::standard::{
ClNullEventPtrEnum, ClWaitListPtrEnum, Context, HostSlice, QueCtx, Queue, SpatialDims,
};
use crate::MemMap;
use std;
use std::marker::PhantomData;
use std::mem;
use std::ops::{Deref, DerefMut};
#[cfg(not(feature = "opencl_vendor_mesa"))]
use crate::core::GlTextureTarget;
#[cfg(not(feature = "opencl_vendor_mesa"))]
use crate::ffi::{cl_GLint, cl_GLuint};
use crate::ffi::{cl_id3d11_texture2d, cl_id3d11_texture3d, cl_uint};
/// The type of operation to be performed by a command.
#[derive(Debug)]
pub enum ImageCmdKind<'c, T: 'c> {
Unspecified,
Read {
data: &'c mut [T],
},
Write {
data: &'c [T],
},
Map,
Fill {
color: T,
},
Copy {
dst_image: &'c MemCore,
dst_origin: [usize; 3],
},
CopyToBuffer {
buffer: &'c MemCore,
dst_offset: usize,
},
GLAcquire,
GLRelease,
D3D11Acquire,
D3D11Release,
}
impl<'c, T: 'c> ImageCmdKind<'c, T> {
fn is_unspec(&'c self) -> bool {
if let ImageCmdKind::Unspecified = *self {
true
} else {
false
}
}
}
/// An image command builder for enqueuing reads, writes, fills, and copies.
///
/// ## Examples
///
/// ```rust,ignore
///
/// // Copies one image to another:
/// src_image.cmd().copy(&dst_image, [0, 0, 0]).enq().unwrap();
///
/// // Writes from a vector to an image, waiting on an event:
/// image.write(&src_vec).ewait(&event).enq().unwrap();
///
/// // Reads from a image into a vector, waiting on an event list and
/// // filling a new empty event:
/// image.read(&dst_vec).ewait(&event_list).enew(&empty_event).enq().unwrap();
///
/// // Reads without blocking:
/// image.cmd().read_async(&dst_vec).enew(&empty_event).enq().unwrap();
///
/// ```
///
/// [FIXME]: Fills not yet implemented.
#[must_use = "commands do nothing unless enqueued"]
#[allow(dead_code)]
pub struct ImageCmd<'c, T: 'c> {
queue: Option<&'c Queue>,
obj_core: &'c MemCore,
block: bool,
origin: [usize; 3],
region: [usize; 3],
row_pitch_bytes: usize,
slc_pitch_bytes: usize,
kind: ImageCmdKind<'c, T>,
ewait: Option<ClWaitListPtrEnum<'c>>,
enew: Option<ClNullEventPtrEnum<'c>>,
mem_dims: [usize; 3],
ext_fns: Option<&'c core::ExtensionFunctions>,
}
/// [UNSTABLE]: All methods still in a state of adjustifulsomeness.
impl<'c, T: 'c + OclPrm> ImageCmd<'c, T> {
/// Returns a new image command builder associated with with the
/// memory object `obj_core` along with a default `queue` and `to_len`
/// (the length of the device side image).
fn new(
queue: Option<&'c Queue>,
obj_core: &'c MemCore,
dims: [usize; 3],
ext_fns: Option<&'c core::ExtensionFunctions>,
) -> ImageCmd<'c, T> {
ImageCmd {
queue,
obj_core,
block: true,
origin: [0, 0, 0],
region: dims,
row_pitch_bytes: 0,
slc_pitch_bytes: 0,
kind: ImageCmdKind::Unspecified,
ewait: None,
enew: None,
mem_dims: dims,
ext_fns,
}
}
/// Specifies that this command will be a blocking read operation.
///
/// After calling this method, the blocking state of this command will
/// be locked to true and a call to `::block` will cause a panic.
///
/// ## Panics
///
/// The command operation kind must not have already been specified.
///
/// ### More Information
///
/// See [SDK][read_image] docs for more details.
///
/// [read_image]: https://www.khronos.org/registry/OpenCL/sdk/1.2/docs/man/xhtml/clEnqueueReadImage.html
pub fn read<'d>(mut self, dst_data: &'d mut [T]) -> ImageCmd<'c, T>
where
'd: 'c,
{
assert!(
self.kind.is_unspec(),
"ocl::ImageCmd::read(): Operation kind \
already set for this command."
);
self.kind = ImageCmdKind::Read { data: dst_data };
self.block = true;
self
}
/// Specifies that this command will be a write operation.
///
/// ## Panics
///
/// The command operation kind must not have already been specified
///
/// ### More Information
///
/// See [SDK][read_buffer] docs for more details.
///
/// [read_buffer]: https://www.khronos.org/registry/OpenCL/sdk/1.2/docs/man/xhtml/clEnqueueReadBuffer.html
pub fn write<'d>(mut self, src_data: &'d [T]) -> ImageCmd<'c, T>
where
'd: 'c,
{
assert!(
self.kind.is_unspec(),
"ocl::ImageCmd::write(): Operation kind \
already set for this command."
);
self.kind = ImageCmdKind::Write { data: src_data };
self
}
/// Specifies that this command will be a map operation.
///
/// If `.block(..)` has been set it will be ignored. Non-blocking map
/// commands are enqueued using `::enq_async`.
///
/// ## Safety
///
/// The caller must ensure that only one mapping of a particular memory
/// region exists at a time.
///
/// ## Panics
///
/// The command operation kind must not have already been specified
///
/// ### More Information
///
/// See [SDK][map_image] docs for more details.
///
/// [map_image]: https://www.khronos.org/registry/OpenCL/sdk/1.2/docs/man/xhtml/clEnqueueMapImage.html
pub unsafe fn map(mut self) -> ImageMapCmd<'c, T> {
assert!(
self.kind.is_unspec(),
"ocl::BufferCmd::write(): Operation kind \
already set for this command."
);
self.kind = ImageCmdKind::Map;
unimplemented!();
// ImageMapCmd { cmd: self }
}
/// Specifies that this command will be a copy operation.
///
/// If `.block(..)` has been set it will be ignored.
///
/// ## Errors
///
/// If this is a rectangular copy, `dst_origin` and `len` must be zero.
///
/// ## Panics
///
/// The command operation kind must not have already been specified
///
pub fn copy<'d>(mut self, dst_image: &'d Image<T>, dst_origin: [usize; 3]) -> ImageCmd<'c, T>
where
'd: 'c,
{
assert!(
self.kind.is_unspec(),
"ocl::ImageCmd::copy(): Operation kind \
already set for this command."
);
self.kind = ImageCmdKind::Copy {
dst_image: dst_image.as_core(),
dst_origin,
};
self
}
/// Specifies that this command will be a copy to image.
///
/// If `.block(..)` has been set it will be ignored.
///
/// ## Panics
///
/// The command operation kind must not have already been specified
///
pub fn copy_to_buffer<'d>(mut self, buffer: &'d MemCore, dst_offset: usize) -> ImageCmd<'c, T>
where
'd: 'c,
{
assert!(
self.kind.is_unspec(),
"ocl::ImageCmd::copy_to_buffer(): Operation kind \
already set for this command."
);
self.kind = ImageCmdKind::CopyToBuffer { buffer, dst_offset };
self
}
/// Specifies that this command will acquire a GL buffer.
///
/// If `.block(..)` has been set it will be ignored.
///
/// ## Panics
///
/// The command operation kind must not have already been specified
///
pub fn gl_acquire(mut self) -> ImageCmd<'c, T> {
assert!(
self.kind.is_unspec(),
"ocl::ImageCmd::gl_acquire(): Operation kind \
already set for this command."
);
self.kind = ImageCmdKind::GLAcquire;
self
}
/// Specifies that this command will release a GL buffer.
///
/// If `.block(..)` has been set it will be ignored.
///
/// ## Panics
///
/// The command operation kind must not have already been specified
///
pub fn gl_release(mut self) -> ImageCmd<'c, T> {
assert!(
self.kind.is_unspec(),
"ocl::ImageCmd::gl_release(): Operation kind \
already set for this command."
);
self.kind = ImageCmdKind::GLRelease;
self
}
/// Specifies that this command will acquire a D3D11 buffer.
///
/// If `.block(..)` has been set it will be ignored.
///
/// ## Panics
///
/// The command operation kind must not have already been specified
///
pub fn d3d11_acquire(mut self) -> ImageCmd<'c, T> {
assert!(
self.kind.is_unspec(),
"ocl::ImageCmd::d3d11_acquire(): Operation kind \
already set for this command."
);
self.kind = ImageCmdKind::D3D11Acquire;
self
}
/// Specifies that this command will release a D3D11 buffer.
///
/// If `.block(..)` has been set it will be ignored.
///
/// ## Panics
///
/// The command operation kind must not have already been specified
///
pub fn d3d11_release(mut self) -> ImageCmd<'c, T> {
assert!(
self.kind.is_unspec(),
"ocl::ImageCmd::d3d11_release(): Operation kind \
already set for this command."
);
self.kind = ImageCmdKind::D3D11Release;
self
}
/// Specifies that this command will be a fill.
///
/// If `.block(..)` has been set it will be ignored.
///
/// ## Panics
///
/// The command operation kind must not have already been specified
///
pub fn fill(mut self, color: T) -> ImageCmd<'c, T> {
assert!(
self.kind.is_unspec(),
"ocl::ImageCmd::fill(): Operation kind \
already set for this command."
);
self.kind = ImageCmdKind::Fill { color };
self
}
/// Specifies a queue to use for this call only.
///
/// Overrides the image's default queue if one is set. If no default queue
/// is set, this method **must** be called before enqueuing the command.
pub fn queue(mut self, queue: &'c Queue) -> ImageCmd<'c, T> {
self.queue = Some(queue);
self
}
/// Specifies whether or not to block the current thread until completion.
///
/// Ignored if this is not a read or write operation.
///
/// Default is `block = true`.
///
/// ## Safety
///
/// When performing non-blocking reads or writes, the caller must ensure
/// that the data being read from or written to is not accessed improperly
/// until the command completes. Use events (`Event::wait_for`) or the
/// command queue (`Queue::finish`) to synchronize.
///
/// If possible, prefer instead to use [`::map`] with [`::enq_async`] for
/// optimal performance and data integrity.
///
/// [`::map`]: struct.ImageMapCmd.html
/// [`::enq_async`]: struct.ImageMapCmd.html#method.enq_async
///
pub unsafe fn block(mut self, block: bool) -> ImageCmd<'c, T> {
self.block = block;
self
}
/// Sets the three dimensional offset, the origin point, for an operation.
///
/// Defaults to [0, 0, 0] if not set.
///
/// ## Panics
///
/// The 'shape' may not have already been set to rectangular by the
/// `::rect` function.
pub fn origin<D>(mut self, origin: D) -> ImageCmd<'c, T>
where
D: Into<SpatialDims>,
{
self.origin = origin.into().to_offset().unwrap();
self
}
/// Sets the region size for an operation.
///
/// Defaults to the full region size of the image(s) as defined when first
/// created if not set.
///
/// ## Panics [TENATIVE]
///
/// Panics if the region is out of range on any of the three dimensions.
///
/// [FIXME]: Probably delay checking this until enq().
///
pub fn region<D>(mut self, region: D) -> ImageCmd<'c, T>
where
D: Into<SpatialDims>,
{
self.region = region.into().to_lens().unwrap();
self
}
/// Sets the row and slice pitch for a read or write operation in bytes.
///
/// `row_pitch_bytes`: Must be greater than or equal to the region width
/// in bytes (region[0] * sizeof(T)).
///
/// `slice_pitch: Must be greater than or equal to `row_pitch` * region
/// height in bytes (region[1] * sizeof(T)).
///
/// Only needs to be set if region has been set to something other than
/// the (default) image buffer size.
///
pub unsafe fn pitch_bytes(
mut self,
row_pitch_bytes: usize,
slc_pitch_bytes: usize,
) -> ImageCmd<'c, T> {
self.row_pitch_bytes = row_pitch_bytes;
self.slc_pitch_bytes = slc_pitch_bytes;
self
}
/// Specifies an event or list of events to wait on before the command
/// will run.
///
/// When events generated using the `::enew` method of **other**,
/// previously enqueued commands are passed here (either individually or
/// as part of an [`EventList`]), this command will not execute until
/// those commands have completed.
///
/// Using events can compliment the use of queues to order commands by
/// creating temporal dependencies between them (where commands in one
/// queue must wait for the completion of commands in another). Events can
/// also supplant queues altogether when, for example, using out-of-order
/// queues.
///
/// # Example
///
/// ```rust,ignore
/// // Create an event list:
/// let mut event_list = EventList::new();
/// // Enqueue a kernel on `queue_1`, creating an event representing the kernel
/// // command in our list:
/// kernel.cmd().queue(&queue_1).enew(&mut event_list).enq()?;
/// // Read from a buffer using `queue_2`, ensuring the read does not begin until
/// // after the kernel command has completed:
/// buffer.read(rwvec.clone()).queue(&queue_2).ewait(&event_list).enq_async()?;
/// ```
///
/// [`EventList`]: struct.EventList.html
pub fn ewait<'e, Ewl>(mut self, ewait: Ewl) -> ImageCmd<'c, T>
where
'e: 'c,
Ewl: Into<ClWaitListPtrEnum<'e>>,
{
self.ewait = Some(ewait.into());
self
}
/// Specifies the destination to store a new, optionally created event
/// associated with this command.
///
/// The destination can be a mutable reference to an empty event (created
/// using [`Event::empty`]) or a mutable reference to an event list.
///
/// After this command is enqueued, the event in the destination can be
/// passed to the `::ewait` method of another command. Doing so will cause
/// the other command to wait until this command has completed before
/// executing.
///
/// Using events can compliment the use of queues to order commands by
/// creating temporal dependencies between them (where commands in one
/// queue must wait for the completion of commands in another). Events can
/// also supplant queues altogether when, for example, using out-of-order
/// queues.
///
/// # Example
///
/// ```rust,ignore
/// // Create an event list:
/// let mut event = Event::empty();
/// // Enqueue a kernel on `queue_1`, creating an event representing the kernel
/// // command in our list:
/// kernel.cmd().queue(&queue_1).enew(&mut event).enq()?;
/// // Read from a buffer using `queue_2`, ensuring the read does not begin until
/// // after the kernel command has completed:
/// buffer.read(rwvec.clone()).queue(&queue_2).ewait(&event).enq_async()?;
/// ```
///
/// [`Event::empty`]: struct.Event.html#method.empty
pub fn enew<'e, En>(mut self, enew: En) -> ImageCmd<'c, T>
where
'e: 'c,
En: Into<ClNullEventPtrEnum<'e>>,
{
self.enew = Some(enew.into());
self
}
/// Enqueues this command.
///
/// * TODO: FOR COPY, FILL, AND COPYTOBUFFER -- ENSURE PITCHES ARE BOTH
/// UNSET.
pub fn enq(self) -> OclResult<()> {
let queue = match self.queue {
Some(q) => q,
None => return Err("ImageCmd::enq: No queue set.".into()),
};
match self.kind {
ImageCmdKind::Read { data } => {
unsafe { core::enqueue_read_image(queue, self.obj_core, self.block,
self.origin, self.region, self.row_pitch_bytes, self.slc_pitch_bytes, data, self.ewait,
self.enew) }
},
ImageCmdKind::Write { data } => {
unsafe {
core::enqueue_write_image(queue, self.obj_core, self.block,
self.origin, self.region, self.row_pitch_bytes, self.slc_pitch_bytes, data, self.ewait,
self.enew)
}
},
ImageCmdKind::Copy { dst_image, dst_origin } => {
core::enqueue_copy_image(queue, self.obj_core, dst_image, self.origin,
dst_origin, self.region, self.ewait, self.enew)
},
ImageCmdKind::CopyToBuffer { buffer, dst_offset } => {
core::enqueue_copy_image_to_buffer::<T, _, _, _>(queue, self.obj_core, buffer, self.origin,
self.region, dst_offset, self.ewait, self.enew)
},
#[cfg(not(feature="opencl_vendor_mesa"))]
ImageCmdKind::GLAcquire => {
// core::enqueue_acquire_gl_buffer(queue, self.obj_core, self.ewait, self.enew)
let buf_slc = unsafe { std::slice::from_raw_parts(self.obj_core, 1) };
core::enqueue_acquire_gl_objects(queue, buf_slc, self.ewait, self.enew)
},
#[cfg(not(feature="opencl_vendor_mesa"))]
ImageCmdKind::GLRelease => {
// core::enqueue_release_gl_buffer(queue, self.obj_core, self.ewait, self.enew)
let buf_slc = unsafe { std::slice::from_raw_parts(self.obj_core, 1) };
core::enqueue_release_gl_objects(queue, buf_slc, self.ewait, self.enew)
},
ImageCmdKind::D3D11Acquire => {
match self.ext_fns {
Some(fns) => {
let buf_slc = unsafe { std::slice::from_raw_parts(self.obj_core, 1) };
core::enqueue_acquire_d3d11_objects(queue, buf_slc, self.ewait, self.enew, fns)
}
None => Err("ocl::ImageCmd::enq(): The function pointer to clEnqueueAcquireD3D11Objects was not resolved.".into())
}
},
ImageCmdKind::D3D11Release => {
match self.ext_fns {
Some(fns) => {
let buf_slc = unsafe { std::slice::from_raw_parts(self.obj_core, 1) };
core::enqueue_release_d3d11_objects(queue, buf_slc, self.ewait, self.enew, fns)
}
None => Err("ocl::ImageCmd::enq(): The function pointer to clEnqueueReleaseD3D11Objects was not resolved.".into())
}
},
ImageCmdKind::Unspecified => Err("ocl::ImageCmd::enq(): No operation \
specified. Use '.read(...)', 'write(...)', etc. before calling '.enq()'.".into()),
_ => unimplemented!(),
}.map_err(OclError::from)
}
}
/// A buffer command builder used to enqueue maps.
///
/// See [SDK][map_buffer] docs for more details.
///
/// [map_buffer]: https://www.khronos.org/registry/OpenCL/sdk/1.2/docs/man/xhtml/clEnqueueMapBuffer.html
// const size_t * origin ,
// const size_t * region ,
// size_t *image_row_pitch ,
// size_t *image_slice_pitch ,
pub struct ImageMapCmd<'c, T>
where
T: 'c,
{
cmd: ImageCmd<'c, T>,
flags: Option<MapFlags>,
}
impl<'c, T> ImageMapCmd<'c, T>
where
T: OclPrm,
{
/// Specifies the flags to be used with this map command.
///
/// See [SDK] docs for more details.
///
/// [SDK]: https://www.khronos.org/registry/OpenCL/sdk/1.2/docs/man/xhtml/clEnqueueMapBuffer.html
pub fn flags(mut self, flags: MapFlags) -> ImageMapCmd<'c, T> {
self.flags = Some(flags);
self
}
/// Sets the three dimensional offset, the origin point, for an operation.
///
/// Defaults to [0, 0, 0] if not set.
///
/// ## Panics
///
/// The 'shape' may not have already been set to rectangular by the
/// `::rect` function.
pub fn origin(mut self, origin: [usize; 3]) -> ImageMapCmd<'c, T> {
self.cmd.origin = origin;
self
}
/// Sets the region size for an operation.
///
/// Defaults to the full region size of the image(s) as defined when first
/// created if not set.
///
/// ## Panics [TENATIVE]
///
/// Panics if the region is out of range on any of the three dimensions.
///
/// [FIXME]: Probably delay checking this until enq().
///
pub fn region(mut self, region: [usize; 3]) -> ImageMapCmd<'c, T> {
self.cmd.region = region;
self
}
/// Specifies an event or list of events to wait on before the command
/// will run.
///
/// When events generated using the `::enew` method of **other**,
/// previously enqueued commands are passed here (either individually or
/// as part of an [`EventList`]), this command will not execute until
/// those commands have completed.
///
/// Using events can compliment the use of queues to order commands by
/// creating temporal dependencies between them (where commands in one
/// queue must wait for the completion of commands in another). Events can
/// also supplant queues altogether when, for example, using out-of-order
/// queues.
///
/// # Example
///
/// ```rust,ignore
/// // Create an event list:
/// let mut event_list = EventList::new();
/// // Enqueue a kernel on `queue_1`, creating an event representing the kernel
/// // command in our list:
/// kernel.cmd().queue(&queue_1).enew(&mut event_list).enq()?;
/// // Read from a buffer using `queue_2`, ensuring the read does not begin until
/// // after the kernel command has completed:
/// buffer.read(rwvec.clone()).queue(&queue_2).ewait(&event_list).enq_async()?;
/// ```
///
/// [`EventList`]: struct.EventList.html
pub fn ewait<'e, Ewl>(mut self, ewait: Ewl) -> ImageMapCmd<'c, T>
where
'e: 'c,
Ewl: Into<ClWaitListPtrEnum<'e>>,
{
self.cmd.ewait = Some(ewait.into());
self
}
/// Specifies the destination to store a new, optionally created event
/// associated with this command.
///
/// The destination can be a mutable reference to an empty event (created
/// using [`Event::empty`]) or a mutable reference to an event list.
///
/// After this command is enqueued, the event in the destination can be
/// passed to the `::ewait` method of another command. Doing so will cause
/// the other command to wait until this command has completed before
/// executing.
///
/// Using events can compliment the use of queues to order commands by
/// creating temporal dependencies between them (where commands in one
/// queue must wait for the completion of commands in another). Events can
/// also supplant queues altogether when, for example, using out-of-order
/// queues.
///
/// # Example
///
/// ```rust,ignore
/// // Create an event list:
/// let mut event = Event::empty();
/// // Enqueue a kernel on `queue_1`, creating an event representing the kernel
/// // command in our list:
/// kernel.cmd().queue(&queue_1).enew(&mut event).enq()?;
/// // Read from a buffer using `queue_2`, ensuring the read does not begin until
/// // after the kernel command has completed:
/// buffer.read(rwvec.clone()).queue(&queue_2).ewait(&event).enq_async()?;
/// ```
///
/// [`Event::empty`]: struct.Event.html#method.empty
pub fn enew<'e, En>(mut self, enew: En) -> ImageMapCmd<'c, T>
where
'e: 'c,
En: Into<ClNullEventPtrEnum<'e>>,
{
self.cmd.enew = Some(enew.into());
self
}
/// Enqueues this command.
///
/// * TODO: FOR COPY, FILL, AND COPYTOBUFFER -- ENSURE PITCHES ARE BOTH UNSET.
#[allow(unused_variables, unreachable_code)]
pub fn enq(self) -> OclResult<MemMap<T>> {
let queue = match self.cmd.queue {
Some(q) => q,
None => return Err("ImageCmd::enq: No queue set.".into()),
};
let flags = self.flags.unwrap_or(MapFlags::empty());
match self.cmd.kind {
ImageCmdKind::Map => {
// try!(check_len(self.cmd.to_len, data.len(), offset));
let mut row_pitch_bytes = 0usize;
let mut slc_pitch_bytes = 0usize;
unsafe {
let mm_core = core::enqueue_map_image::<T, _, _, _>(
queue,
self.cmd.obj_core,
self.cmd.block,
flags,
self.cmd.origin,
self.cmd.region,
&mut row_pitch_bytes,
&mut slc_pitch_bytes,
self.cmd.ewait,
self.cmd.enew,
)?;
let len_bytes = if slc_pitch_bytes == 0 {
// 1D or 2D image.
unimplemented!();
} else {
// 1D image array, 2D image array, or 3D image.
unimplemented!();
};
// let unmap_event = None;
// * TODO: Create a special container for mapped images
// that can take into account row and slice pitch. It
// cannot deref into a &[T] as the size of rows (and
// slices) can vary with byte-sized precision.
// Ok(MemMap::new(mm_core, 0, unmap_event, self.cmd.obj_core.clone(),
// queue.core().clone()))
}
}
_ => unreachable!(),
}
}
}
/// A section of device memory which represents one or many images.
///
/// Use `::builder` for an easy way to create. [UNIMPLEMENTED]
///
#[derive(Clone, Debug)]
pub struct Image<T: OclPrm> {
obj_core: MemCore,
queue: Option<Queue>,
dims: SpatialDims,
pixel_element_len: usize,
extension_functions: Option<core::ExtensionFunctions>,
_pixel: PhantomData<T>,
}
impl<T: OclPrm> Image<T> {
/// Returns a list of supported image formats.
pub fn supported_formats(
context: &Context,
flags: MemFlags,
mem_obj_type: MemObjectType,
) -> OclResult<Vec<ImageFormatParseResult>> {
core::get_supported_image_formats(context, flags, mem_obj_type).map_err(OclError::from)
}
/// Returns an `ImageBuilder`. This is the recommended method to create
/// a new `Image`.
pub fn builder<'a>() -> ImageBuilder<'a, T> {
ImageBuilder::new()
}
/// Returns a new `Image`.
///
/// Prefer `::builder` to create a new image.
pub unsafe fn new<'o, Q>(
que_ctx: Q,
flags: MemFlags,
image_format: ImageFormat,
image_desc: ImageDescriptor,
host_data: Option<&[T]>,
) -> OclResult<Image<T>>
where
Q: Into<QueCtx<'o>>,
{
let que_ctx = que_ctx.into();
let context = que_ctx.context_cloned();
let device_versions = context.device_versions()?;
let obj_core = core::create_image(
&context,
flags,
&image_format,
&image_desc,
host_data,
Some(&device_versions),
)?;
let pixel_element_len = match core::get_image_info(&obj_core, ImageInfo::ElementSize)? {
ImageInfoResult::ElementSize(s) => s / mem::size_of::<T>(),
_ => {
return Err("ocl::Image::element_len(): \
Unexpected 'ImageInfoResult' variant."
.into())
}
};
let dims = [
image_desc.image_width,
image_desc.image_height,
image_desc.image_depth,
]
.into();
let new_img = Image {
obj_core,
queue: que_ctx.into(),
dims,
pixel_element_len,
extension_functions: None,
_pixel: PhantomData,
};
Ok(new_img)
}
/// Returns a new `Image` from an existant GL texture2D/3D.
///
/// Remember to specify the GL context when creating the CL context,
/// using `.properties(ocl_interop::get_properties_list())`
///
/// Don't forget to `.cmd().gl_acquire().enq()` before using it and
/// `.cmd().gl_release().enq()` after.
///
// [WORK IN PROGRESS]
#[cfg(not(feature = "opencl_vendor_mesa"))]
pub fn from_gl_texture<'o, Q>(
que_ctx: Q,
flags: MemFlags,
image_desc: ImageDescriptor,
texture_target: GlTextureTarget,
miplevel: cl_GLint,
texture: cl_GLuint,
) -> OclResult<Image<T>>
where
Q: Into<QueCtx<'o>>,
{
let que_ctx = que_ctx.into();
let context = que_ctx.context_cloned();
let device_versions = context.device_versions()?;
if texture_target == GlTextureTarget::GlTextureBuffer && miplevel != 0 {
return Err(
"If texture_target is GL_TEXTURE_BUFFER, miplevel must be 0.\
Implementations may return CL_INVALID_OPERATION for miplevel values > 0"
.into(),
);
}
let obj_core = unsafe {
core::create_from_gl_texture(
&context,
texture_target as u32,
miplevel,
texture,
flags,
Some(&device_versions),
)?
};
// FIXME can I do this from a GLTexture ?
let pixel_element_len = match core::get_image_info(&obj_core, ImageInfo::ElementSize)? {
ImageInfoResult::ElementSize(s) => s / mem::size_of::<T>(),
_ => {
return Err("ocl::Image::element_len(): Unexpected \
'ImageInfoResult' variant."
.into())
}
};
let dims = [
image_desc.image_width,
image_desc.image_height,
image_desc.image_depth,
]
.into();
let new_img = Image {
obj_core,
queue: que_ctx.into(),
dims,
pixel_element_len,
extension_functions: None,
_pixel: PhantomData,
};
Ok(new_img)
}
/// Returns a new `Image` from an existant renderbuffer.
///
/// Remember to specify the GL context when creating the CL context,
/// using `.properties(ocl_interop::get_properties_list())`
///
/// Don't forget to `.cmd().gl_acquire().enq()` before using it and
/// `.cmd().gl_release().enq()` after.
///
// [WORK IN PROGRESS]
#[cfg(not(feature = "opencl_vendor_mesa"))]
pub fn from_gl_renderbuffer<'o, Q>(
que_ctx: Q,
flags: MemFlags,
image_desc: ImageDescriptor,
renderbuffer: cl_GLuint,
) -> OclResult<Image<T>>
where
Q: Into<QueCtx<'o>>,
{
let que_ctx = que_ctx.into();
let context = que_ctx.context_cloned();
let obj_core = unsafe { core::create_from_gl_renderbuffer(&context, renderbuffer, flags)? };
// FIXME can I do this from a renderbuffer ?
let pixel_element_len = match core::get_image_info(&obj_core, ImageInfo::ElementSize)? {
ImageInfoResult::ElementSize(s) => s / mem::size_of::<T>(),
_ => {
return Err("ocl::Image::element_len(): \
Unexpected 'ImageInfoResult' variant."
.into())
}
};
let dims = [image_desc.image_width, image_desc.image_height].into();
let new_img = Image {
obj_core,
queue: que_ctx.into(),
dims,
pixel_element_len,
extension_functions: None,
_pixel: PhantomData,
};
Ok(new_img)
}
/// Returns a new `Image` from an existant ID3D11Texture2D.
///
/// Remember to specify the D3D11 device when creating the CL context,
/// using `.properties()` and `.set_property_value(ContextPropertyValue::D3d11DeviceKhr(<pointer to ID3D11Device>))`
///
/// Don't forget to `.cmd().d3d11_acquire().enq()` before using it and
/// `.cmd().d3d11_release().enq()` after.
///
pub fn from_d3d11_texture2d<'o, Q>(
que_ctx: Q,
flags: MemFlags,
image_desc: ImageDescriptor,
texture: cl_id3d11_texture2d,
subresource: u32,
) -> OclResult<Image<T>>
where
Q: Into<QueCtx<'o>>,
{
let que_ctx = que_ctx.into();
let context = que_ctx.context_cloned();
let device_versions = context.device_versions()?;
let extension_fns = match context.platform()? {
Some(platform) => core::ExtensionFunctions::resolve_all(*platform)?,
_ => {
return Err(
"ocl::Image::from_d3d11_texture2d(): Platform must be set in context.".into(),
)
}
};
let obj_core = unsafe {
core::create_from_d3d11_texture2d(
&context,
texture,
subresource as cl_uint,
flags,
Some(&device_versions),
&extension_fns,
)?
};
let pixel_element_len = match core::get_image_info(&obj_core, ImageInfo::ElementSize)? {
ImageInfoResult::ElementSize(s) => s / mem::size_of::<T>(),
_ => {
return Err("ocl::Image::element_len(): Unexpected \
'ImageInfoResult' variant."
.into())
}
};
let dims = [
image_desc.image_width,
image_desc.image_height,
image_desc.image_depth,
]
.into();
let new_img = Image {
obj_core,
queue: que_ctx.into(),
dims,
pixel_element_len,
extension_functions: Some(extension_fns),
_pixel: PhantomData,
};
Ok(new_img)
}
/// Returns a new `Image` from an existant ID3D11Texture2D.
///
/// Remember to specify the D3D11 device when creating the CL context,
/// using `.properties()` and `.set_property_value(ContextPropertyValue::D3d11DeviceKhr(<pointer to ID3D11Device>))`
///
/// Don't forget to `.cmd().d3d11_acquire().enq()` before using it and
/// `.cmd().d3d11_release().enq()` after.
///
pub fn from_d3d11_texture3d<'o, Q>(
que_ctx: Q,
flags: MemFlags,
image_desc: ImageDescriptor,
texture: cl_id3d11_texture3d,
subresource: u32,
) -> OclResult<Image<T>>
where
Q: Into<QueCtx<'o>>,
{
let que_ctx = que_ctx.into();
let context = que_ctx.context_cloned();
let device_versions = context.device_versions()?;
let extension_fns = match context.platform()? {
Some(platform) => core::ExtensionFunctions::resolve_all(*platform)?,
_ => {
return Err(
"ocl::Image::from_d3d11_texture3d(): Platform must be set in context.".into(),
)
}
};
let obj_core = unsafe {
core::create_from_d3d11_texture3d(
&context,
texture,
subresource as cl_uint,
flags,
Some(&device_versions),
&extension_fns,
)?
};
let pixel_element_len = match core::get_image_info(&obj_core, ImageInfo::ElementSize)? {
ImageInfoResult::ElementSize(s) => s / mem::size_of::<T>(),
_ => {
return Err("ocl::Image::element_len(): Unexpected \
'ImageInfoResult' variant."
.into())
}
};
let dims = [
image_desc.image_width,
image_desc.image_height,
image_desc.image_depth,
]
.into();
let new_img = Image {
obj_core,
queue: que_ctx.into(),
dims,
pixel_element_len,
extension_functions: Some(extension_fns),
_pixel: PhantomData,
};
Ok(new_img)
}
/// Returns an image command builder used to read, write, copy, etc.
///
/// Call `.enq()` to enqueue the command.
///
/// See the [command builder documentation](struct.ImageCmd)
/// for more details.
pub fn cmd(&self) -> ImageCmd<T> {
ImageCmd::new(
self.queue.as_ref(),
&self.obj_core,
self.dims.to_lens().expect("ocl::Image::cmd"),
self.extension_functions.as_ref(),
)
}
/// Returns an image command builder set to read.
///
/// Call `.enq()` to enqueue the command.
///
/// See the [command builder documentation](struct.ImageCmd#method.read)
/// for more details.
pub fn read<'c, 'd>(&'c self, data: &'d mut [T]) -> ImageCmd<'c, T>
where
'd: 'c,
{
self.cmd().read(data)
}
/// Returns an image command builder set to write.
///
/// Call `.enq()` to enqueue the command.
///
/// See the [command builder documentation](struct.ImageCmd#method.write)
/// for more details.
pub fn write<'c, 'd>(&'c self, data: &'d [T]) -> ImageCmd<'c, T>
where
'd: 'c,
{
self.cmd().write(data)
}
/// Returns a command builder used to map data for reading or writing.
///
/// Call `.enq()` to enqueue the command.
///
/// ## Safety
///
/// The caller must ensure that only one mapping of a particular memory
/// region exists at a time.
///
/// See the [command builder documentation](struct.ImageCmd#method.map)
/// for more details.
///
#[inline]
pub unsafe fn map<'c>(&'c self) -> ImageMapCmd<'c, T> {
unimplemented!();
// self.cmd().map()
}
// /// Specifies that this command will be a copy operation.
// ///
// /// Call `.enq()` to enqueue the command.
// ///
// /// See the [command builder documentation](struct.ImageCmd#method.copy)
// /// for more details.
// ///
// #[inline]
// pub fn copy<'c, M>(&'c self, dst_buffer: &'c M, dst_offset: Option<usize>, len: Option<usize>)
// -> BufferCmd<'c, T>
// where M: AsMem
// {
// self.cmd().copy(dst_buffer, dst_offset, len)
// }
/// Changes the default queue.
///
/// Returns a ref for chaining i.e.:
///
/// `image.set_default_queue(queue).write(....);`
///
/// [NOTE]: Even when used as above, the queue is changed permanently,
/// not just for the one call. Changing the queue is cheap so feel free
/// to change as often as needed.
///
/// The new queue must be associated with a valid device.
///
pub fn set_default_queue<'a>(&'a mut self, queue: Queue) -> &'a mut Image<T> {
// self.command_queue_obj_core = queue.core().clone();
self.queue = Some(queue);
self
}
/// Returns a reference to the default queue.
pub fn default_queue(&self) -> Option<&Queue> {
self.queue.as_ref()
}
/// Returns this image's dimensions.
pub fn dims(&self) -> &SpatialDims {
&self.dims
}
/// Returns the total number of pixels in this image.
pub fn pixel_count(&self) -> usize {
self.dims.to_len()
}
/// Returns the length of each pixel element.
pub fn pixel_element_len(&self) -> usize {
self.pixel_element_len
}
/// Returns the total number of pixel elements in this image. Equivalent to its length.
pub fn element_count(&self) -> usize {
self.pixel_count() * self.pixel_element_len()
}
/// Get information about this image.
pub fn info(&self, info_kind: ImageInfo) -> OclResult<ImageInfoResult> {
// match core::get_image_info(&self.obj_core, info_kind) {
// Ok(res) => res,
// Err(err) => ImageInfoResult::Error(Box::new(err)),
// }
core::get_image_info(&self.obj_core, info_kind).map_err(OclError::from)
}
/// Returns info about this image's memory.
pub fn mem_info(&self, info_kind: MemInfo) -> OclResult<MemInfoResult> {
// match core::get_mem_object_info(&self.obj_core, info_kind) {
// Ok(res) => res,
// Err(err) => MemInfoResult::Error(Box::new(err)),
// }
core::get_mem_object_info(&self.obj_core, info_kind).map_err(OclError::from)
}
/// Returns a reference to the core pointer wrapper, usable by functions in
/// the `core` module.
#[inline]
pub fn as_core(&self) -> &MemCore {
&self.obj_core
}
/// Format image info.
fn fmt_info(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
f.debug_struct("Image")
.field("ElementSize", &self.info(ImageInfo::ElementSize))
.field("RowPitch", &self.info(ImageInfo::RowPitch))
.field("SlicePitch", &self.info(ImageInfo::SlicePitch))
.field("Width", &self.info(ImageInfo::Width))
.field("Height", &self.info(ImageInfo::Height))
.field("Depth", &self.info(ImageInfo::Depth))
.field("ArraySize", &self.info(ImageInfo::ArraySize))
.field("Buffer", &self.info(ImageInfo::Buffer))
.field("NumMipLevels", &self.info(ImageInfo::NumMipLevels))
.field("NumSamples", &self.info(ImageInfo::NumSamples))
.finish()
}
/// Format image mem info.
fn fmt_mem_info(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
f.debug_struct("Mem")
.field("Type", &self.mem_info(MemInfo::Type))
.field("Flags", &self.mem_info(MemInfo::Flags))
.field("Size", &self.mem_info(MemInfo::Size))
.field("HostPtr", &self.mem_info(MemInfo::HostPtr))
.field("MapCount", &self.mem_info(MemInfo::MapCount))
.field("ReferenceCount", &self.mem_info(MemInfo::ReferenceCount))
.field("Context", &self.mem_info(MemInfo::Context))
.field(
"AssociatedMemobject",
&self.mem_info(MemInfo::AssociatedMemobject),
)
.field("Offset", &self.mem_info(MemInfo::Offset))
.finish()
}
}
impl<T: OclPrm> std::fmt::Display for Image<T> {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
self.fmt_info(f)?;
write!(f, " ")?;
self.fmt_mem_info(f)
}
}
impl<T: OclPrm> Deref for Image<T> {
type Target = MemCore;
fn deref(&self) -> &MemCore {
&self.obj_core
}
}
impl<T: OclPrm> DerefMut for Image<T> {
fn deref_mut(&mut self) -> &mut MemCore {
&mut self.obj_core
}
}
impl<T: OclPrm> AsMem<T> for Image<T> {
fn as_mem(&self) -> &MemCore {
&self.obj_core
}
}
unsafe impl<'a, T> MemCmdRw for Image<T> where T: OclPrm {}
unsafe impl<'a, T> MemCmdRw for &'a Image<T> where T: OclPrm {}
unsafe impl<'a, T> MemCmdRw for &'a mut Image<T> where T: OclPrm {}
unsafe impl<'a, T> MemCmdAll for Image<T> where T: OclPrm {}
unsafe impl<'a, T> MemCmdAll for &'a Image<T> where T: OclPrm {}
unsafe impl<'a, T> MemCmdAll for &'a mut Image<T> where T: OclPrm {}
/// A builder for `Image`.
#[must_use = "builders do nothing unless '::build' is called"]
pub struct ImageBuilder<'a, T>
where
T: 'a,
{
queue_option: Option<QueCtx<'a>>,
flags: MemFlags,
host_slice: HostSlice<'a, T>,
image_format: ImageFormat,
image_desc: ImageDescriptor,
_pixel: PhantomData<T>,
}
impl<'a, T> ImageBuilder<'a, T>
where
T: 'a + OclPrm,
{
/// Returns a new `ImageBuilder` with very basic defaults.
///
/// ## Defaults
///
/// * Flags:
///
/// ```rust,ignore
/// ocl::MEM_READ_WRITE
/// ```
///
/// * Image Format:
///
/// ```rust,ignore
/// ocl::ImageFormat {
/// channel_order: ocl::ImageChannelOrder::Rgba,
/// channel_data_type: ocl::ImageChannelDataType::SnormInt8,
/// }
/// ```
///
/// * Descriptor (stores everything else - width, height, pitch, etc.):
///
/// ```rust,ignore
/// ImageDescriptor::new(MemObjectType::Image1d, 0, 0, 0, 0, 0, 0, None)
/// ```
///
/// ## Reference
///
/// See the [official SDK documentation] for more information.
///
/// Some descriptions here are adapted from various SDK docs.
///
/// [official SDK docs]: https://www.khronos.org/registry/cl/sdk/1.2/docs/man/xhtml/clCreateImage.html
pub fn new() -> ImageBuilder<'a, T> {
ImageBuilder {
queue_option: None,
flags: core::MEM_READ_WRITE,
host_slice: HostSlice::None,
image_format: ImageFormat::new_rgba(),
image_desc: ImageDescriptor::new(MemObjectType::Image1d, 0, 0, 0, 0, 0, 0, None),
_pixel: PhantomData,
}
}
/// Sets the context with which to associate the buffer.
///
/// May not be used in combination with `::queue` (use one or the other).
pub fn context<'o>(mut self, context: &'o Context) -> ImageBuilder<'a, T>
where
'o: 'a,
{
assert!(self.queue_option.is_none());
self.queue_option = Some(QueCtx::Context(context));
self
}
/// Sets the default queue.
///
/// If this is set, the context associated with the `default_queue` will
/// be used when creating the buffer (use one or the other).
pub fn queue<'b>(mut self, default_queue: Queue) -> ImageBuilder<'a, T> {
assert!(self.queue_option.is_none());
self.queue_option = Some(QueCtx::Queue(default_queue));
self
}
/// Sets the flags used when creating the image.
///
/// Defaults to `flags::MEM_READ_WRITE` aka.
/// `MemFlags::new().read_write()` if this is not set. See the [SDK Docs]
/// for more information about flags. Note that the names of all flags in
/// this library have the `CL_` prefix removed for brevity.
///
/// ### Panics
///
/// Due to its unsafety, setting the
/// `MEM_USE_HOST_PTR`/`MemFlags::new()::use_host_ptr()` flag will cause a
/// panic. Use the `::use_host_slice` method instead.
///
/// [SDK Docs]: https://www.khronos.org/registry/cl/sdk/1.2/docs/man/xhtml/clCreateBuffer.html
pub fn flags(mut self, flags: MemFlags) -> ImageBuilder<'a, T> {
assert!(
!flags.contains(MemFlags::new().use_host_ptr()),
"The `ImageBuilder::flags` method may not be used to set the \
`MEM_USE_HOST_PTR` flag. Use the `::use_host_ptr` method instead."
);
self.flags = flags;
self
}
/// Specifies a region of host memory to use as storage for the image.
///
/// OpenCL implementations are allowed to cache the image contents
/// pointed to by `host_slice` in device memory. This cached copy can be
/// used when kernels are executed on a device.
///
/// The result of OpenCL commands that operate on multiple image objects
/// created with the same `host_slice` or overlapping host regions is
/// considered to be undefined
///
/// Refer to the [description of the alignment][align_rules] rules for
/// `host_slice` for memory objects (buffer and images) created using
/// this method.
///
/// Automatically sets the `flags::MEM_USE_HOST_PTR` aka.
/// `MemFlags::new().use_host_ptr()` flag.
///
/// ### Panics
///
/// `::copy_host_slice` or `::use_host_slice` must not have already been
/// called.
///
/// ### Safety
///
/// The caller must ensure that `host_slice` lives until the image is
/// destroyed. The caller must also ensure that only one image uses
/// `host_slice` and that it is not tampered with inappropriately.
///
/// [align_rules]: https://www.khronos.org/registry/OpenCL/sdk/1.2/docs/man/xhtml/dataTypes.html
pub unsafe fn use_host_slice<'d>(mut self, host_slice: &'d [T]) -> ImageBuilder<'a, T>
where
'd: 'a,
{
assert!(
self.host_slice.is_none(),
"ImageBuilder::use_host_slice: \
A host slice has already been specified."
);
self.host_slice = HostSlice::Use(host_slice);
self
}
/// Specifies a region of memory to copy into the image upon creation.
///
/// Automatically sets the `flags::MEM_COPY_HOST_PTR` aka.
/// `MemFlags::new().copy_host_ptr()` flag.
///
/// ### Panics
///
/// `::copy_host_slice` or `::use_host_slice` must not have already been
/// called.
///
pub fn copy_host_slice<'d>(mut self, host_slice: &'d [T]) -> ImageBuilder<'a, T>
where
'd: 'a,
{
assert!(
self.host_slice.is_none(),
"ImageBuilder::copy_host_slice: \
A host slice has already been specified."
);
self.host_slice = HostSlice::Copy(host_slice);
self
}
pub fn channel_order(mut self, order: ImageChannelOrder) -> ImageBuilder<'a, T> {
self.image_format.channel_order = order;
self
}
pub fn channel_data_type(mut self, data_type: ImageChannelDataType) -> ImageBuilder<'a, T> {
self.image_format.channel_data_type = data_type;
self
}
/// Sets the type of image (technically the type of memory buffer).
///
/// Describes the image type and must be either `Image1d`, `Image1dBuffer`,
/// `Image1dArray`, `Image2d`, `Image2dArray`, or `Image3d`.
///
pub fn image_type(mut self, image_type: MemObjectType) -> ImageBuilder<'a, T> {
self.image_desc.image_type = image_type;
self
}
/// The width, height, and depth of an image or image array:
///
/// Some notes adapted from SDK docs:
///
/// ## Width
///
/// The width of the image in pixels. For a 2D image and image array, the
/// image width must be ≤ `DeviceInfo::Image2dMaxWidth`. For a 3D image, the
/// image width must be ≤ `DeviceInfo::Image3dMaxWidth`. For a 1D image buffer,
/// the image width must be ≤ `DeviceInfo::ImageMaxBufferSize`. For a 1D image
/// and 1D image array, the image width must be ≤ `DeviceInfo::Image2dMaxWidth`.
///
/// ## Height
///
/// The height of the image in pixels. This is only used if the
/// image is a 2D, 3D or 2D image array. For a 2D image or image array, the
/// image height must be ≤ `DeviceInfo::Image2dMaxHeight`. For a 3D image, the
/// image height must be ≤ `DeviceInfo::Image3dMaxHeight`.
///
/// ## Depth
///
/// image_depth 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 ≤
/// `DeviceInfo::Image3dMaxDepth`.
///
/// ## Examples
///
/// * To set the dimensions of a 2d image use:
/// `SpatialDims::Two(width, height)`.
/// * To set the dimensions of a 2d image array use:
/// `SpatialDims::Three(width, height, array_length)`.
/// * To set the dimensions of a 3d image use:
/// `SpatialDims::Three(width, height, depth)`.
///
pub fn dims<D>(mut self, dims: D) -> ImageBuilder<'a, T>
where
D: Into<SpatialDims>,
{
let dims = dims.into().to_lens().unwrap();
self.image_desc.image_width = dims[0];
self.image_desc.image_height = dims[1];
self.image_desc.image_depth = dims[2];
self
}
/// Image array size.
///
/// The number of images in the image array. This is only used if the image is
/// a 1D or 2D image array. The values for image_array_size, if specified,
/// must be a value ≥ 1 and ≤ `DeviceInfo::ImageMaxArraySize`.
///
/// Note that reading and writing 2D image arrays from a kernel with
/// image_array_size = 1 may be lower performance than 2D images.
///
pub fn array_size(mut self, array_size: usize) -> ImageBuilder<'a, T> {
self.image_desc.image_array_size = array_size;
self
}
/// Image row pitch.
///
/// The scan-line pitch in bytes. This must be 0 if host data is `None` and
/// can be either 0 or ≥ image_width * size of element in bytes if host data
/// is not `None`. If host data is not `None` 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.
///
pub fn row_pitch_bytes(mut self, row_pitch: usize) -> ImageBuilder<'a, T> {
self.image_desc.image_row_pitch = row_pitch;
self
}
/// Image slice pitch.
///
/// 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 data is
/// `None`. If host data is not `None`, 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 data is not
/// `None` 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.
///
pub fn slc_pitch_bytes(mut self, slc_pitch: usize) -> ImageBuilder<'a, T> {
self.image_desc.image_slice_pitch = slc_pitch;
self
}
/// Buffer synchronization.
///
/// Refers to a valid buffer memory object if image_type is
/// `MemObjectType::Image1dBuffer`. Otherwise it must be `None` (default).
/// For a 1D image buffer object, the image pixels are taken from the buffer
/// object's data store. When the contents of a buffer object's data store are
/// modified, those changes are reflected in the contents of the 1D image
/// buffer object and vice-versa at corresponding sychronization points. The
/// image_width * size of element in bytes must be ≤ size of buffer object
/// data store.
///
pub fn buffer_sync(mut self, buffer: MemCore) -> ImageBuilder<'a, T> {
self.image_desc.buffer = Some(buffer);
self
}
/// Specifies the image pixel format.
///
/// If unspecified, defaults to:
///
/// ```rust,ignore
/// ImageFormat {
/// channel_order: ImageChannelOrder::Rgba,
/// channel_data_type: ImageChannelDataType::SnormInt8,
/// }
/// ```
pub fn image_format(mut self, image_format: ImageFormat) -> ImageBuilder<'a, T> {
self.image_format = image_format;
self
}
/// Specifies the image descriptor containing a number of important settings.
///
/// If unspecified (not recommended), defaults to:
///
/// ```rust,ignore
/// ImageDescriptor {
/// image_type: MemObjectType::Image1d,
/// image_width: 0,
/// image_height: 0,
/// image_depth: 0,
/// image_array_size: 0,
/// image_row_pitch: 0,
/// image_slice_pitch: 0,
/// num_mip_levels: 0,
/// num_samples: 0,
/// buffer: None,
/// }
/// ```
///
/// If you are unsure, just set the first four by using
/// `ImageDescriptor::new`. Ex.:
///
/// ```rust,ignore
/// ocl::Image::builder()
/// .image_desc(ocl::ImageDescriptor::new(
/// ocl::MemObjectType::Image2d, 1280, 800, 1))
/// ...
/// ...
/// .build()
/// ```
///
/// Setting this overwrites any previously set type, dimensions, array size, pitch, etc.
///
pub unsafe fn image_desc(mut self, image_desc: ImageDescriptor) -> ImageBuilder<'a, T> {
self.image_desc = image_desc;
self
}
/// Builds with no host side image data memory specified and returns a
/// new `Image`.
pub fn build(mut self) -> OclResult<Image<T>> {
let host_slice = match self.host_slice {
HostSlice::Use(hs) => {
self.flags.insert(MemFlags::new().use_host_ptr());
Some(hs)
}
HostSlice::Copy(hs) => {
self.flags.insert(MemFlags::new().copy_host_ptr());
Some(hs)
}
HostSlice::None => None,
};
match self.queue_option {
Some(qo) => unsafe {
Image::new(
qo,
self.flags,
self.image_format.clone(),
self.image_desc.clone(),
host_slice,
)
},
None => panic!(
"ocl::ImageBuilder::build: A context or default queue must be set \
with '.context(...)' or '.queue(...)'."
),
}
}
}