ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/OpenCL/OpenCL.xs
Revision: 1.83
Committed: Mon Apr 7 22:36:54 2014 UTC (10 years, 3 months ago) by root
Branch: MAIN
CVS Tags: HEAD
Changes since 1.82: +5 -3 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 #include <stddef.h>
2
3 #include "EXTERN.h"
4 #include "perl.h"
5 #include "XSUB.h"
6
7 #define ECB_NO_THREADS 1
8 #define ECB_NO_LIBM 1
9 #include "ecb.h"
10
11 #define X_STACKSIZE sizeof (void *) * 512 * 1024 // 2-4mb should be enough, really
12 #include "xthread.h"
13 #include "schmorp.h"
14
15 #ifdef I_DLFCN
16 #include <dlfcn.h>
17 #endif
18
19 // how stupid is that, the 1.2 header files define CL_VERSION_1_1,
20 // but then fail to define the api functions unless you ALSO define
21 // this. This breaks 100% of the opencl 1.1 apps, for what reason?
22 // after all, the functions are deprecated, not removed.
23 // in addition, you cannot test for this in any future-proof way.
24 // each time a new opencl version comes out, you need to make a new
25 // release.
26 #define CL_USE_DEPRECATED_OPENCL_1_2_APIS /* just guessing, you stupid idiots */
27
28 #ifndef PREFER_1_1
29 #define PREFER_1_1 1
30 #endif
31
32 #if PREFER_1_1
33 #define CL_USE_DEPRECATED_OPENCL_1_1_APIS
34 #endif
35
36 #ifdef __APPLE__
37 #define CLHDR(name) <OpenCL/name>
38 #else
39 #define CLHDR(name) <CL/name>
40 #endif
41
42 #include CLHDR(opencl.h)
43
44 #if 0
45 #ifndef CL_VERSION_1_2
46 #include CLHDR(cl_d3d9.h)
47 #endif
48 #endif
49
50 #if _WIN32
51 #include CLHDR(cl_d3d10.h)
52 #if CL_VERSION_1_2
53 #include CLHDR(cl_d3d11.h)
54 #endif
55 #include CLHDR(cl_dx9_media_sharing.h)
56 #endif
57
58 #ifndef CL_VERSION_1_2
59 #undef PREFER_1_1
60 #define PREFER_1_1 1
61 #endif
62
63 // make sure all constants we might use are actually defined
64 #include "default.h"
65
66 typedef cl_platform_id OpenCL__Platform;
67 typedef cl_device_id OpenCL__Device;
68 typedef cl_device_id OpenCL__SubDevice;
69 typedef cl_context OpenCL__Context;
70 typedef cl_command_queue OpenCL__Queue;
71 typedef cl_mem OpenCL__Memory;
72 typedef cl_mem OpenCL__Buffer;
73 typedef cl_mem OpenCL__BufferObj;
74 typedef cl_mem OpenCL__Image;
75 typedef cl_mem OpenCL__Memory_ornull;
76 typedef cl_mem OpenCL__Buffer_ornull;
77 typedef cl_mem OpenCL__Image_ornull;
78 typedef cl_sampler OpenCL__Sampler;
79 typedef cl_program OpenCL__Program;
80 typedef cl_kernel OpenCL__Kernel;
81 typedef cl_event OpenCL__Event;
82 typedef cl_event OpenCL__UserEvent;
83
84 typedef struct mapped * OpenCL__Mapped;
85
86 static HV
87 *stash_platform,
88 *stash_device,
89 *stash_subdevice,
90 *stash_context,
91 *stash_queue,
92 *stash_program,
93 *stash_kernel,
94 *stash_sampler,
95 *stash_event,
96 *stash_userevent,
97 *stash_memory,
98 *stash_buffer,
99 *stash_bufferobj,
100 *stash_image,
101 *stash_image1d,
102 *stash_image1darray,
103 *stash_image1dbuffer,
104 *stash_image2d,
105 *stash_image2darray,
106 *stash_image3d,
107 *stash_mapped,
108 *stash_mappedbuffer,
109 *stash_mappedimage;
110
111 /*****************************************************************************/
112
113 // name must include a leading underscore
114 // all of this horrors would be unneceesary if somebody wrote a proper OpenGL module
115 // for perl. doh.
116 static void *
117 glsym (const char *name)
118 {
119 void *fun = 0;
120
121 #if defined I_DLFCN && defined RTLD_DEFAULT
122 fun = dlsym (RTLD_DEFAULT, name + 1);
123 if (!fun) fun = dlsym (RTLD_DEFAULT, name);
124
125 if (!fun)
126 {
127 static void *libgl;
128 static const char *glso[] = {
129 "libGL.so.1",
130 "libGL.so.3",
131 "libGL.so.4.0",
132 "libGL.so",
133 "/usr/lib/libGL.so",
134 "/usr/X11R6/lib/libGL.1.dylib"
135 };
136 int i;
137
138 for (i = 0; !libgl && i < sizeof (glso) / sizeof (glso [0]); ++i)
139 {
140 libgl = dlopen (glso [i], RTLD_LAZY);
141 if (libgl)
142 break;
143 }
144
145 if (libgl)
146 {
147 fun = dlsym (libgl, name + 1);
148 if (!fun) fun = dlsym (libgl, name);
149 }
150 }
151 #endif
152
153 return fun;
154 }
155
156 /*****************************************************************************/
157
158 /* up to two temporary buffers */
159 static void * ecb_noinline
160 tmpbuf (size_t size)
161 {
162 enum { buffers = 4 };
163 static int idx;
164 static void *buf [buffers];
165 static size_t len [buffers];
166
167 idx = (idx + 1) % buffers;
168
169 if (len [idx] < size)
170 {
171 free (buf [idx]);
172 len [idx] = ((size + 31) & ~4095) + 4096 - 32;
173 buf [idx] = malloc (len [idx]);
174 }
175
176 return buf [idx];
177 }
178
179 static const char * ecb_noinline
180 cv_get_name (CV *cv)
181 {
182 static char fullname [256];
183
184 GV *gv = CvGV (cv); // gv better be nonzero
185
186 HV *stash = GvSTASH (gv);
187 const char *hvname = HvNAME_get (stash); // stash also better be nonzero
188 const char *gvname = GvNAME (gv);
189
190 snprintf (fullname, sizeof (fullname), "%s::%s", hvname, gvname);
191 return fullname;
192 }
193
194 /*****************************************************************************/
195
196 typedef struct
197 {
198 IV iv;
199 const char *name;
200 #define const_iv(name) { (IV)CL_ ## name, # name },
201 } ivstr;
202
203 typedef struct
204 {
205 NV nv;
206 const char *name;
207 #define const_nv(name) { (NV)CL_ ## name, # name },
208 } nvstr;
209
210 static const char *
211 iv2str (IV value, const ivstr *base, int count, const char *fallback)
212 {
213 int i;
214 static char strbuf [32];
215
216 for (i = count; i--; )
217 if (base [i].iv == value)
218 return base [i].name;
219
220 snprintf (strbuf, sizeof (strbuf), fallback, (int)value);
221
222 return strbuf;
223 }
224
225 static const char *
226 enum2str (cl_uint value)
227 {
228 static const ivstr enumstr[] = {
229 #include "enumstr.h"
230 };
231
232 return iv2str (value, enumstr, sizeof (enumstr) / sizeof (enumstr [0]), "ENUM(0x%04x)");
233 }
234
235 static const char *
236 err2str (cl_int err)
237 {
238 static const ivstr errstr[] = {
239 #include "errstr.h"
240 };
241
242 return iv2str (err, errstr, sizeof (errstr) / sizeof (errstr [0]), "ERROR(%d)");
243 }
244
245 /*****************************************************************************/
246
247 static cl_int res;
248
249 #define FAIL(name) \
250 croak ("cl" # name ": %s", err2str (res));
251
252 #define NEED_SUCCESS(name,args) \
253 do { \
254 res = cl ## name args; \
255 \
256 if (res) \
257 FAIL (name); \
258 } while (0)
259
260 #define NEED_SUCCESS_ARG(retdecl, name, args) \
261 retdecl = cl ## name args; \
262 if (res) \
263 FAIL (name);
264
265 /*****************************************************************************/
266
267 static SV *
268 new_clobj (HV *stash, IV id)
269 {
270 return sv_2mortal (sv_bless (newRV_noinc (newSViv (id)), stash));
271 }
272
273 #define PUSH_CLOBJ(stash,id) PUSHs (new_clobj ((stash), (IV)(id)))
274 #define XPUSH_CLOBJ(stash,id) XPUSHs (new_clobj ((stash), (IV)(id)))
275
276 /* cl objects are either \$iv, or [$iv, ...] */
277 /* they can be upgraded at runtime to the array form */
278 static void * ecb_noinline
279 SvCLOBJ (CV *cv, const char *svname, SV *sv, const char *pkg)
280 {
281 // sv_derived_from is quite slow :(
282 if (SvROK (sv) && sv_derived_from (sv, pkg))
283 return (void *)SvIV (SvRV (sv));
284
285 croak ("%s: %s is not of type %s", cv_get_name (cv), svname, pkg);
286 }
287
288 // the "no-inherit" version of the above
289 static void * ecb_noinline
290 SvCLOBJ_ni (CV *cv, const char *svname, SV *sv, HV *stash)
291 {
292 if (SvROK (sv) && SvSTASH (SvRV (sv)) == stash)
293 return (void *)SvIV (SvRV (sv));
294
295 croak ("%s: %s is not of type %s", cv_get_name (cv), svname, HvNAME (stash));
296 }
297
298 /*****************************************************************************/
299
300 static cl_context_properties * ecb_noinline
301 SvCONTEXTPROPERTIES (CV *cv, const char *svname, SV *sv, cl_context_properties *extra, int extracount)
302 {
303 if (!sv || !SvOK (sv))
304 if (extra)
305 sv = sv_2mortal (newRV_noinc ((SV *)newAV ())); // slow, but rarely used hopefully
306 else
307 return 0;
308
309 if (SvROK (sv) && SvTYPE (SvRV (sv)) == SVt_PVAV)
310 {
311 AV *av = (AV *)SvRV (sv);
312 int i, len = av_len (av) + 1;
313 cl_context_properties *p = tmpbuf (sizeof (cl_context_properties) * (len + extracount + 1));
314 cl_context_properties *l = p;
315
316 if (len & 1)
317 croak ("%s: %s is not a property list (must contain an even number of elements)", cv_get_name (cv), svname);
318
319 while (extracount--)
320 *l++ = *extra++;
321
322 for (i = 0; i < len; i += 2)
323 {
324 cl_context_properties t = SvIV (*av_fetch (av, i , 0));
325 SV *p_sv = *av_fetch (av, i + 1, 0);
326 cl_context_properties v = SvIV (p_sv); // code below can override
327
328 switch (t)
329 {
330 case CL_CONTEXT_PLATFORM:
331 if (SvROK (p_sv))
332 v = (cl_context_properties)SvCLOBJ (cv, svname, p_sv, "OpenCL::Platform");
333 break;
334
335 case CL_GLX_DISPLAY_KHR:
336 if (!SvOK (p_sv))
337 {
338 void *func = glsym ("_glXGetCurrentDisplay");
339 if (func)
340 v = (cl_context_properties)((void *(*)(void))func)();
341 }
342 break;
343
344 case CL_GL_CONTEXT_KHR:
345 if (!SvOK (p_sv))
346 {
347 void *func = glsym ("_glXGetCurrentContext");
348 if (func)
349 v = (cl_context_properties)((void *(*)(void))func)();
350 }
351 break;
352
353 default:
354 /* unknown property, treat as int */
355 break;
356 }
357
358 *l++ = t;
359 *l++ = v;
360 }
361
362 *l = 0;
363
364 return p;
365 }
366
367 croak ("%s: %s is not a property list (either undef or [type => value, ...])", cv_get_name (cv), svname);
368 }
369
370 // parse an array of CLOBJ into a void ** array in C - works only for CLOBJs whose representation
371 // is a pointer (and only on well-behaved systems).
372 static void * ecb_noinline
373 object_list (CV *cv, int or_undef, const char *argname, SV *arg, const char *klass, cl_uint *rcount)
374 {
375 if (!SvROK (arg) || SvTYPE (SvRV (arg)) != SVt_PVAV)
376 croak ("%s: '%s' parameter must be %sa reference to an array of %s objects",
377 cv_get_name (cv), argname, or_undef ? "undef or " : "", klass);
378
379 AV *av = (AV *)SvRV (arg);
380 void **list = 0;
381 cl_uint count = av_len (av) + 1;
382
383 if (count)
384 {
385 list = tmpbuf (sizeof (*list) * count);
386 int i;
387 for (i = 0; i < count; ++i)
388 list [i] = SvCLOBJ (cv, argname, *av_fetch (av, i, 1), klass);
389 }
390
391 if (!count && !or_undef)
392 croak ("%s: '%s' must contain at least one %s object",
393 cv_get_name (cv), argname, klass);
394
395 *rcount = count;
396 return (void *)list;
397 }
398
399 /*****************************************************************************/
400 /* callback stuff */
401
402 /* default context callback, log to stderr */
403 static void CL_CALLBACK
404 context_default_notify (const char *msg, const void *info, size_t cb, void *data)
405 {
406 fprintf (stderr, "OpenCL Context Notify: %s\n", msg);
407 }
408
409 typedef struct
410 {
411 int free_cb;
412 void (*push)(void *data1, void *data2, void *data3);
413 } eq_vtbl;
414
415 typedef struct eq_item
416 {
417 struct eq_item *next;
418 eq_vtbl *vtbl;
419 SV *cb;
420 void *data1, *data2, *data3;
421 } eq_item;
422
423 static void (*eq_signal_func)(void *signal_arg, int value);
424 static void *eq_signal_arg;
425 static xmutex_t eq_lock = X_MUTEX_INIT;
426 static eq_item *eq_head, *eq_tail;
427
428 static void ecb_noinline
429 eq_enq (eq_vtbl *vtbl, SV *cb, void *data1, void *data2, void *data3)
430 {
431 eq_item *item = malloc (sizeof (eq_item));
432
433 item->next = 0;
434 item->vtbl = vtbl;
435 item->cb = cb;
436 item->data1 = data1;
437 item->data2 = data2;
438 item->data3 = data3;
439
440 X_LOCK (eq_lock);
441
442 *(eq_head ? &eq_tail->next : &eq_head) = item;
443 eq_tail = item;
444
445 X_UNLOCK (eq_lock);
446
447 eq_signal_func (eq_signal_arg, 0);
448 }
449
450 static eq_item *
451 eq_dec (void)
452 {
453 eq_item *res;
454
455 X_LOCK (eq_lock);
456
457 res = eq_head;
458 if (res)
459 eq_head = res->next;
460
461 X_UNLOCK (eq_lock);
462
463 return res;
464 }
465
466 static void
467 eq_poll (void)
468 {
469 eq_item *item;
470
471 while ((item = eq_dec ()))
472 {
473 ENTER;
474 SAVETMPS;
475
476 dSP;
477 PUSHMARK (SP);
478 EXTEND (SP, 2);
479
480 if (item->vtbl->free_cb)
481 sv_2mortal (item->cb);
482
483 PUTBACK;
484 item->vtbl->push (item->data1, item->data2, item->data3);
485
486 SV *cb = item->cb;
487 free (item);
488
489 call_sv (cb, G_DISCARD | G_VOID);
490
491 FREETMPS;
492 LEAVE;
493 }
494 }
495
496 static void
497 eq_poll_interrupt (pTHX_ void *c_arg, int value)
498 {
499 eq_poll ();
500 }
501
502 /*****************************************************************************/
503 /* context notify */
504
505 static void ecb_noinline
506 eq_context_push (void *data1, void *data2, void *data3)
507 {
508 dSP;
509 PUSHs (sv_2mortal (newSVpv (data1, 0)));
510 PUSHs (sv_2mortal (newSVpvn (data2, (STRLEN)data3)));
511 PUTBACK;
512
513 free (data1);
514 free (data2);
515 }
516
517 static eq_vtbl eq_context_vtbl = { 0, eq_context_push };
518
519 static void CL_CALLBACK
520 eq_context_notify (const char *msg, const void *pvt, size_t cb, void *user_data)
521 {
522 void *pvt_copy = malloc (cb);
523 memcpy (pvt_copy, pvt, cb);
524 eq_enq (&eq_context_vtbl, user_data, strdup (msg), pvt_copy, (void *)cb);
525 }
526
527 #define CONTEXT_NOTIFY_CALLBACK \
528 void (CL_CALLBACK *pfn_notify)(const char *, const void *, size_t, void *) = context_default_notify; \
529 void *user_data = 0; \
530 \
531 if (SvOK (notify)) \
532 { \
533 pfn_notify = eq_context_notify; \
534 user_data = s_get_cv (notify); \
535 }
536
537 static SV * ecb_noinline
538 new_clobj_context (cl_context ctx, void *user_data)
539 {
540 SV *sv = new_clobj (stash_context, (IV)ctx);
541
542 if (user_data)
543 sv_magicext (SvRV (sv), user_data, PERL_MAGIC_ext, 0, 0, 0);
544
545 return sv;
546 }
547
548 #define XPUSH_CLOBJ_CONTEXT XPUSHs (new_clobj_context (ctx, user_data));
549
550 /*****************************************************************************/
551 /* build/compile/link notify */
552
553 static void
554 eq_program_push (void *data1, void *data2, void *data3)
555 {
556 dSP;
557 PUSH_CLOBJ (stash_program, data1);
558 PUTBACK;
559 }
560
561 static eq_vtbl eq_program_vtbl = { 1, eq_program_push };
562
563 static void CL_CALLBACK
564 eq_program_notify (cl_program program, void *user_data)
565 {
566 clRetainProgram (program);
567
568 eq_enq (&eq_program_vtbl, user_data, (void *)program, 0, 0);
569 }
570
571 typedef void (CL_CALLBACK *program_callback)(cl_program program, void *user_data);
572
573 static program_callback ecb_noinline
574 make_program_callback (SV *notify, void **ruser_data)
575 {
576 if (SvOK (notify))
577 {
578 *ruser_data = SvREFCNT_inc (s_get_cv (notify));
579 return eq_program_notify;
580 }
581 else
582 {
583 *ruser_data = 0;
584 return 0;
585 }
586 }
587
588 struct build_args
589 {
590 cl_program program;
591 char *options;
592 void *user_data;
593 cl_uint num_devices;
594 };
595
596 X_THREAD_PROC (build_program_thread)
597 {
598 struct build_args *arg = thr_arg;
599
600 clBuildProgram (arg->program, arg->num_devices, arg->num_devices ? (void *)(arg + 1) : 0, arg->options, 0, 0);
601
602 if (arg->user_data)
603 eq_program_notify (arg->program, arg->user_data);
604 else
605 clReleaseProgram (arg->program);
606
607 free (arg->options);
608 free (arg);
609
610 return 0;
611 }
612
613 static void
614 build_program_async (cl_program program, cl_uint num_devices, const cl_device_id *device_list, const char *options, void *user_data)
615 {
616 struct build_args *arg = malloc (sizeof (struct build_args) + sizeof (*device_list) * num_devices);
617
618 arg->program = program;
619 arg->options = strdup (options);
620 arg->user_data = user_data;
621 arg->num_devices = num_devices;
622 memcpy (arg + 1, device_list, sizeof (*device_list) * num_devices);
623
624 xthread_t id;
625 xthread_create (&id, build_program_thread, arg);
626 }
627
628 /*****************************************************************************/
629 /* mem object destructor notify */
630
631 static void ecb_noinline
632 eq_destructor_push (void *data1, void *data2, void *data3)
633 {
634 }
635
636 static eq_vtbl eq_destructor_vtbl = { 0, eq_destructor_push };
637
638 static void CL_CALLBACK
639 eq_destructor_notify (cl_mem memobj, void *user_data)
640 {
641 eq_enq (&eq_destructor_vtbl, (SV *)user_data, (void *)memobj, 0, 0);
642 }
643
644 /*****************************************************************************/
645 /* event objects */
646
647 static void
648 eq_event_push (void *data1, void *data2, void *data3)
649 {
650 dSP;
651 PUSH_CLOBJ (stash_event, data1);
652 PUSHs (sv_2mortal (newSViv ((IV)data2)));
653 PUTBACK;
654 }
655
656 static eq_vtbl eq_event_vtbl = { 1, eq_event_push };
657
658 static void CL_CALLBACK
659 eq_event_notify (cl_event event, cl_int event_command_exec_status, void *user_data)
660 {
661 clRetainEvent (event);
662 eq_enq (&eq_event_vtbl, user_data, (void *)event, (void *)(IV)event_command_exec_status, 0);
663 }
664
665 /*****************************************************************************/
666 /* utilities for XS code */
667
668 static size_t
669 img_row_pitch (cl_mem img)
670 {
671 size_t res;
672 clGetImageInfo (img, CL_IMAGE_ROW_PITCH, sizeof (res), &res, 0);
673 return res;
674 }
675
676 static cl_event * ecb_noinline
677 event_list (CV *cv, SV **items, cl_uint *rcount, cl_event extra)
678 {
679 cl_uint count = *rcount;
680
681 if (count > 0x7fffffffU) // yeah, it's a hack - the caller might have underflowed
682 *rcount = count = 0;
683
684 if (!count && !extra)
685 return 0;
686
687 cl_event *list = tmpbuf (sizeof (cl_event) * (count + 1));
688 int i = 0;
689
690 while (count--)
691 if (SvOK (items [count]))
692 list [i++] = SvCLOBJ (cv, "wait_events", items [count], "OpenCL::Event");
693
694 if (extra)
695 list [i++] = extra;
696
697 *rcount = i;
698
699 return i ? list : 0;
700 }
701
702 #define EVENT_LIST(skip) \
703 cl_uint event_list_count = items - (skip); \
704 cl_event *event_list_ptr = event_list (cv, &ST (skip), &event_list_count, 0)
705
706 #define INFO(class) \
707 { \
708 size_t size; \
709 NEED_SUCCESS (Get ## class ## Info, (self, name, 0, 0, &size)); \
710 SV *sv = sv_2mortal (newSV (size)); \
711 SvUPGRADE (sv, SVt_PV); \
712 SvPOK_only (sv); \
713 SvCUR_set (sv, size); \
714 NEED_SUCCESS (Get ## class ## Info, (self, name, size, SvPVX (sv), 0)); \
715 XPUSHs (sv); \
716 }
717
718 /*****************************************************************************/
719 /* mapped_xxx */
720
721 static OpenCL__Mapped
722 SvMAPPED (SV *sv)
723 {
724 // no typechecking atm., keep your fingers crossed
725 return (OpenCL__Mapped)SvMAGIC (SvRV (sv))->mg_ptr;
726 }
727
728 struct mapped
729 {
730 cl_command_queue queue;
731 cl_mem memobj;
732 void *ptr;
733 size_t cb;
734 cl_event event;
735 size_t row_pitch;
736 size_t slice_pitch;
737
738 size_t element_size;
739 size_t width, height, depth;
740 };
741
742 static SV *
743 mapped_new (
744 HV *stash, cl_command_queue queue, cl_mem memobj, cl_map_flags flags,
745 void *ptr, size_t cb, cl_event ev,
746 size_t row_pitch, size_t slice_pitch, size_t element_size,
747 size_t width, size_t height, size_t depth
748 )
749 {
750 SV *data = newSV (0);
751 SvUPGRADE (data, SVt_PVMG);
752
753 OpenCL__Mapped mapped;
754 New (0, mapped, 1, struct mapped);
755
756 clRetainCommandQueue (queue);
757
758 mapped->queue = queue;
759 mapped->memobj = memobj;
760 mapped->ptr = ptr;
761 mapped->cb = cb;
762 mapped->event = ev;
763 mapped->row_pitch = row_pitch;
764 mapped->slice_pitch = slice_pitch;
765
766 mapped->element_size = element_size;
767 mapped->width = width;
768 mapped->height = height;
769 mapped->depth = depth;
770
771 sv_magicext (data, 0, PERL_MAGIC_ext, 0, (char *)mapped, 0);
772
773 if (SvLEN (data))
774 Safefree (data);
775
776 SvPVX (data) = (char *)ptr;
777 SvCUR_set (data, cb);
778 SvLEN_set (data, 0);
779 SvPOK_only (data);
780
781 SV *obj = sv_2mortal (sv_bless (newRV_noinc (data), stash));
782
783 if (!(flags & CL_MAP_WRITE))
784 SvREADONLY_on (data);
785
786 return obj;
787 }
788
789 static void
790 mapped_detach (SV *sv, OpenCL__Mapped mapped)
791 {
792 SV *data = SvRV (sv);
793
794 // the next check checks both before AND after detach, where SvPVX should STILL be 0
795 if (SvPVX (data) != (char *)mapped->ptr)
796 warn ("FATAL: OpenCL memory mapped scalar changed location, detected");
797 else
798 {
799 SvREADONLY_off (data);
800 SvCUR_set (data, 0);
801 SvPVX (data) = 0;
802 SvOK_off (data);
803 }
804
805 mapped->ptr = 0;
806 }
807
808 static void
809 mapped_unmap (CV *cv, SV *self, OpenCL__Mapped mapped, cl_command_queue queue, SV **wait_list, cl_uint event_list_count)
810 {
811 cl_event *event_list_ptr = event_list (cv, wait_list, &event_list_count, mapped->event);
812 cl_event ev;
813
814 NEED_SUCCESS (EnqueueUnmapMemObject, (queue, mapped->memobj, mapped->ptr, event_list_count, event_list_ptr, &ev));
815
816 clReleaseEvent (mapped->event);
817 mapped->event = ev;
818
819 mapped_detach (self, mapped);
820 }
821
822 static size_t
823 mapped_element_size (OpenCL__Mapped self)
824 {
825 if (!self->element_size)
826 clGetImageInfo (self->memobj, CL_IMAGE_ELEMENT_SIZE, sizeof (self->element_size), &self->element_size, 0);
827
828 return self->element_size;
829 }
830
831 /*****************************************************************************/
832
833 MODULE = OpenCL PACKAGE = OpenCL
834
835 PROTOTYPES: ENABLE
836
837 void
838 poll ()
839 CODE:
840 eq_poll ();
841
842 void
843 _eq_initialise (IV func, IV arg)
844 CODE:
845 eq_signal_func = (void (*)(void *, int))func;
846 eq_signal_arg = (void*)arg;
847
848 BOOT:
849 {
850 HV *stash = gv_stashpv ("OpenCL", 1);
851
852 static const ivstr *civ, const_iv[] = {
853 { sizeof (cl_char ), "SIZEOF_CHAR" },
854 { sizeof (cl_uchar ), "SIZEOF_UCHAR" },
855 { sizeof (cl_short ), "SIZEOF_SHORT" },
856 { sizeof (cl_ushort), "SIZEOF_USHORT" },
857 { sizeof (cl_int ), "SIZEOF_INT" },
858 { sizeof (cl_uint ), "SIZEOF_UINT" },
859 { sizeof (cl_long ), "SIZEOF_LONG" },
860 { sizeof (cl_ulong ), "SIZEOF_ULONG" },
861 { sizeof (cl_half ), "SIZEOF_HALF" },
862 { sizeof (cl_float ), "SIZEOF_FLOAT" },
863 { sizeof (cl_double), "SIZEOF_DOUBLE" },
864 { PREFER_1_1 , "PREFER_1_1" },
865 #include "constiv.h"
866 };
867
868 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ > const_iv; civ--)
869 newCONSTSUB (stash, (char *)civ[-1].name, newSViv (civ[-1].iv));
870
871 static const nvstr *cnv, const_nv[] = {
872 #include "constnv.h"
873 };
874
875 for (cnv = const_nv + sizeof (const_nv) / sizeof (const_nv [0]); cnv > const_nv; cnv--)
876 newCONSTSUB (stash, (char *)cnv[-1].name, newSVnv (cnv[-1].nv));
877
878 newCONSTSUB (stash, "NAN", newSVnv (CL_NAN)); // CL_NAN might be a function call
879
880 stash_platform = gv_stashpv ("OpenCL::Platform", GV_ADD);
881 stash_device = gv_stashpv ("OpenCL::Device", GV_ADD);
882 stash_subdevice = gv_stashpv ("OpenCL::SubDevice", GV_ADD);
883 stash_context = gv_stashpv ("OpenCL::Context", GV_ADD);
884 stash_queue = gv_stashpv ("OpenCL::Queue", GV_ADD);
885 stash_program = gv_stashpv ("OpenCL::Program", GV_ADD);
886 stash_kernel = gv_stashpv ("OpenCL::Kernel", GV_ADD);
887 stash_sampler = gv_stashpv ("OpenCL::Sampler", GV_ADD);
888 stash_event = gv_stashpv ("OpenCL::Event", GV_ADD);
889 stash_userevent = gv_stashpv ("OpenCL::UserEvent", GV_ADD);
890 stash_memory = gv_stashpv ("OpenCL::Memory", GV_ADD);
891 stash_buffer = gv_stashpv ("OpenCL::Buffer", GV_ADD);
892 stash_bufferobj = gv_stashpv ("OpenCL::BufferObj", GV_ADD);
893 stash_image = gv_stashpv ("OpenCL::Image", GV_ADD);
894 stash_image1d = gv_stashpv ("OpenCL::Image1D", GV_ADD);
895 stash_image1darray = gv_stashpv ("OpenCL::Image1DArray", GV_ADD);
896 stash_image1dbuffer = gv_stashpv ("OpenCL::Image1DBuffer", GV_ADD);
897 stash_image2d = gv_stashpv ("OpenCL::Image2D", GV_ADD);
898 stash_image2darray = gv_stashpv ("OpenCL::Image2DArray", GV_ADD);
899 stash_image3d = gv_stashpv ("OpenCL::Image3D", GV_ADD);
900 stash_mapped = gv_stashpv ("OpenCL::Mapped", GV_ADD);
901 stash_mappedbuffer = gv_stashpv ("OpenCL::MappedBuffer", GV_ADD);
902 stash_mappedimage = gv_stashpv ("OpenCL::MappedImage", GV_ADD);
903
904 sv_setiv (perl_get_sv ("OpenCL::POLL_FUNC", TRUE), (IV)eq_poll_interrupt);
905 }
906
907 cl_int
908 errno ()
909 CODE:
910 RETVAL = res;
911 OUTPUT:
912 RETVAL
913
914 const char *
915 err2str (cl_int err = res)
916
917 const char *
918 enum2str (cl_uint value)
919
920 void
921 platforms ()
922 PPCODE:
923 cl_platform_id *list;
924 cl_uint count;
925 int i;
926
927 NEED_SUCCESS (GetPlatformIDs, (0, 0, &count));
928 list = tmpbuf (sizeof (*list) * count);
929 NEED_SUCCESS (GetPlatformIDs, (count, list, 0));
930
931 EXTEND (SP, count);
932 for (i = 0; i < count; ++i)
933 PUSH_CLOBJ (stash_platform, list [i]);
934
935 void
936 context_from_type (cl_context_properties *properties = 0, cl_device_type type = CL_DEVICE_TYPE_DEFAULT, SV *notify = &PL_sv_undef)
937 PPCODE:
938 CONTEXT_NOTIFY_CALLBACK;
939 NEED_SUCCESS_ARG (cl_context ctx, CreateContextFromType, (properties, type, pfn_notify, user_data, &res));
940 XPUSH_CLOBJ_CONTEXT;
941
942 void
943 context (cl_context_properties *properties, SV *devices, SV *notify = &PL_sv_undef)
944 PPCODE:
945 cl_uint device_count;
946 cl_device_id *device_list = object_list (cv, 0, "devices", devices, "OpenCL::Device", &device_count);
947
948 CONTEXT_NOTIFY_CALLBACK;
949 NEED_SUCCESS_ARG (cl_context ctx, CreateContext, (properties, device_count, device_list, pfn_notify, user_data, &res));
950 XPUSH_CLOBJ_CONTEXT;
951
952 void
953 wait_for_events (...)
954 CODE:
955 EVENT_LIST (0);
956 NEED_SUCCESS (WaitForEvents, (event_list_count, event_list_ptr));
957
958 PROTOTYPES: DISABLE
959
960 MODULE = OpenCL PACKAGE = OpenCL::Platform
961
962 void
963 info (OpenCL::Platform self, cl_platform_info name)
964 PPCODE:
965 INFO (Platform)
966
967 void
968 unload_compiler (OpenCL::Platform self)
969 CODE:
970 #if CL_VERSION_1_2
971 clUnloadPlatformCompiler (self);
972 #endif
973
974 #BEGIN:platform
975
976 void
977 profile (OpenCL::Platform self)
978 ALIAS:
979 profile = CL_PLATFORM_PROFILE
980 version = CL_PLATFORM_VERSION
981 name = CL_PLATFORM_NAME
982 vendor = CL_PLATFORM_VENDOR
983 extensions = CL_PLATFORM_EXTENSIONS
984 PPCODE:
985 size_t size;
986 NEED_SUCCESS (GetPlatformInfo, (self, ix, 0, 0, &size));
987 char *value = tmpbuf (size);
988 NEED_SUCCESS (GetPlatformInfo, (self, ix, size, value, 0));
989 EXTEND (SP, 1);
990 const int i = 0;
991 PUSHs (sv_2mortal (newSVpv (value, 0)));
992
993 #END:platform
994
995 void
996 devices (OpenCL::Platform self, cl_device_type type = CL_DEVICE_TYPE_ALL)
997 PPCODE:
998 cl_device_id *list;
999 cl_uint count;
1000 int i;
1001
1002 NEED_SUCCESS (GetDeviceIDs, (self, type, 0, 0, &count));
1003 list = tmpbuf (sizeof (*list) * count);
1004 NEED_SUCCESS (GetDeviceIDs, (self, type, count, list, 0));
1005
1006 EXTEND (SP, count);
1007 for (i = 0; i < count; ++i)
1008 PUSH_CLOBJ (stash_device, list [i]);
1009
1010 void
1011 context (OpenCL::Platform self, SV *properties, SV *devices, SV *notify = &PL_sv_undef)
1012 PPCODE:
1013 cl_context_properties extra[] = { CL_CONTEXT_PLATFORM, (cl_context_properties)self };
1014 cl_context_properties *props = SvCONTEXTPROPERTIES (cv, "properties", properties, extra, 2);
1015
1016 cl_uint device_count;
1017 cl_device_id *device_list = object_list (cv, 0, "devices", devices, "OpenCL::Device", &device_count);
1018
1019 CONTEXT_NOTIFY_CALLBACK;
1020 NEED_SUCCESS_ARG (cl_context ctx, CreateContext, (props, device_count, device_list, pfn_notify, user_data, &res));
1021 XPUSH_CLOBJ_CONTEXT;
1022
1023 void
1024 context_from_type (OpenCL::Platform self, SV *properties = 0, cl_device_type type = CL_DEVICE_TYPE_DEFAULT, SV *notify = &PL_sv_undef)
1025 PPCODE:
1026 cl_context_properties extra[] = { CL_CONTEXT_PLATFORM, (cl_context_properties)self };
1027 cl_context_properties *props = SvCONTEXTPROPERTIES (cv, "properties", properties, extra, 2);
1028
1029 CONTEXT_NOTIFY_CALLBACK;
1030 NEED_SUCCESS_ARG (cl_context ctx, CreateContextFromType, (props, type, pfn_notify, user_data, &res));
1031 XPUSH_CLOBJ_CONTEXT;
1032
1033 MODULE = OpenCL PACKAGE = OpenCL::Device
1034
1035 void
1036 info (OpenCL::Device self, cl_device_info name)
1037 PPCODE:
1038 INFO (Device)
1039
1040 #if CL_VERSION_1_2
1041
1042 void
1043 sub_devices (OpenCL::Device self, SV *properties)
1044 PPCODE:
1045 if (!SvROK (properties) || SvTYPE (SvRV (properties)) != SVt_PVAV)
1046 croak ("OpenCL::Device::sub_devices: properties must be specified as reference to an array of property-value pairs");
1047
1048 properties = SvRV (properties);
1049
1050 cl_uint count = av_len ((AV *)properties) + 1;
1051 cl_device_partition_property *props = tmpbuf (sizeof (*props) * count + 1);
1052
1053 int i;
1054 for (i = 0; i < count; ++i)
1055 props [i] = (cl_device_partition_property)SvIV (*av_fetch ((AV *)properties, i, 0));
1056
1057 props [count] = 0;
1058
1059 cl_uint num_devices;
1060 NEED_SUCCESS (CreateSubDevices, (self, props, 0, 0, &num_devices));
1061 cl_device_id *list = tmpbuf (sizeof (*list) * num_devices);
1062 NEED_SUCCESS (CreateSubDevices, (self, props, num_devices, list, 0));
1063
1064 EXTEND (SP, num_devices);
1065 for (i = 0; i < count; ++i)
1066 PUSH_CLOBJ (stash_subdevice, list [i]);
1067
1068 #endif
1069
1070 #BEGIN:device
1071
1072 void
1073 type (OpenCL::Device self)
1074 ALIAS:
1075 type = CL_DEVICE_TYPE
1076 address_bits = CL_DEVICE_ADDRESS_BITS
1077 max_mem_alloc_size = CL_DEVICE_MAX_MEM_ALLOC_SIZE
1078 single_fp_config = CL_DEVICE_SINGLE_FP_CONFIG
1079 global_mem_cache_size = CL_DEVICE_GLOBAL_MEM_CACHE_SIZE
1080 global_mem_size = CL_DEVICE_GLOBAL_MEM_SIZE
1081 max_constant_buffer_size = CL_DEVICE_MAX_CONSTANT_BUFFER_SIZE
1082 local_mem_size = CL_DEVICE_LOCAL_MEM_SIZE
1083 execution_capabilities = CL_DEVICE_EXECUTION_CAPABILITIES
1084 properties = CL_DEVICE_QUEUE_PROPERTIES
1085 double_fp_config = CL_DEVICE_DOUBLE_FP_CONFIG
1086 half_fp_config = CL_DEVICE_HALF_FP_CONFIG
1087 PPCODE:
1088 cl_ulong value [1];
1089 NEED_SUCCESS (GetDeviceInfo, (self, ix, sizeof (value), value, 0));
1090 EXTEND (SP, 1);
1091 const int i = 0;
1092 PUSHs (sv_2mortal (newSVuv (value [i])));
1093
1094 void
1095 vendor_id (OpenCL::Device self)
1096 ALIAS:
1097 vendor_id = CL_DEVICE_VENDOR_ID
1098 max_compute_units = CL_DEVICE_MAX_COMPUTE_UNITS
1099 max_work_item_dimensions = CL_DEVICE_MAX_WORK_ITEM_DIMENSIONS
1100 preferred_vector_width_char = CL_DEVICE_PREFERRED_VECTOR_WIDTH_CHAR
1101 preferred_vector_width_short = CL_DEVICE_PREFERRED_VECTOR_WIDTH_SHORT
1102 preferred_vector_width_int = CL_DEVICE_PREFERRED_VECTOR_WIDTH_INT
1103 preferred_vector_width_long = CL_DEVICE_PREFERRED_VECTOR_WIDTH_LONG
1104 preferred_vector_width_float = CL_DEVICE_PREFERRED_VECTOR_WIDTH_FLOAT
1105 preferred_vector_width_double = CL_DEVICE_PREFERRED_VECTOR_WIDTH_DOUBLE
1106 max_clock_frequency = CL_DEVICE_MAX_CLOCK_FREQUENCY
1107 max_read_image_args = CL_DEVICE_MAX_READ_IMAGE_ARGS
1108 max_write_image_args = CL_DEVICE_MAX_WRITE_IMAGE_ARGS
1109 image_support = CL_DEVICE_IMAGE_SUPPORT
1110 max_samplers = CL_DEVICE_MAX_SAMPLERS
1111 mem_base_addr_align = CL_DEVICE_MEM_BASE_ADDR_ALIGN
1112 min_data_type_align_size = CL_DEVICE_MIN_DATA_TYPE_ALIGN_SIZE
1113 global_mem_cache_type = CL_DEVICE_GLOBAL_MEM_CACHE_TYPE
1114 global_mem_cacheline_size = CL_DEVICE_GLOBAL_MEM_CACHELINE_SIZE
1115 max_constant_args = CL_DEVICE_MAX_CONSTANT_ARGS
1116 local_mem_type = CL_DEVICE_LOCAL_MEM_TYPE
1117 preferred_vector_width_half = CL_DEVICE_PREFERRED_VECTOR_WIDTH_HALF
1118 native_vector_width_char = CL_DEVICE_NATIVE_VECTOR_WIDTH_CHAR
1119 native_vector_width_short = CL_DEVICE_NATIVE_VECTOR_WIDTH_SHORT
1120 native_vector_width_int = CL_DEVICE_NATIVE_VECTOR_WIDTH_INT
1121 native_vector_width_long = CL_DEVICE_NATIVE_VECTOR_WIDTH_LONG
1122 native_vector_width_float = CL_DEVICE_NATIVE_VECTOR_WIDTH_FLOAT
1123 native_vector_width_double = CL_DEVICE_NATIVE_VECTOR_WIDTH_DOUBLE
1124 native_vector_width_half = CL_DEVICE_NATIVE_VECTOR_WIDTH_HALF
1125 reference_count_ext = CL_DEVICE_REFERENCE_COUNT_EXT
1126 PPCODE:
1127 cl_uint value [1];
1128 NEED_SUCCESS (GetDeviceInfo, (self, ix, sizeof (value), value, 0));
1129 EXTEND (SP, 1);
1130 const int i = 0;
1131 PUSHs (sv_2mortal (newSVuv (value [i])));
1132
1133 void
1134 max_work_group_size (OpenCL::Device self)
1135 ALIAS:
1136 max_work_group_size = CL_DEVICE_MAX_WORK_GROUP_SIZE
1137 image2d_max_width = CL_DEVICE_IMAGE2D_MAX_WIDTH
1138 image2d_max_height = CL_DEVICE_IMAGE2D_MAX_HEIGHT
1139 image3d_max_width = CL_DEVICE_IMAGE3D_MAX_WIDTH
1140 image3d_max_height = CL_DEVICE_IMAGE3D_MAX_HEIGHT
1141 image3d_max_depth = CL_DEVICE_IMAGE3D_MAX_DEPTH
1142 max_parameter_size = CL_DEVICE_MAX_PARAMETER_SIZE
1143 profiling_timer_resolution = CL_DEVICE_PROFILING_TIMER_RESOLUTION
1144 PPCODE:
1145 size_t value [1];
1146 NEED_SUCCESS (GetDeviceInfo, (self, ix, sizeof (value), value, 0));
1147 EXTEND (SP, 1);
1148 const int i = 0;
1149 PUSHs (sv_2mortal (newSVuv (value [i])));
1150
1151 void
1152 max_work_item_sizes (OpenCL::Device self)
1153 PPCODE:
1154 size_t size;
1155 NEED_SUCCESS (GetDeviceInfo, (self, CL_DEVICE_MAX_WORK_ITEM_SIZES, 0, 0, &size));
1156 size_t *value = tmpbuf (size);
1157 NEED_SUCCESS (GetDeviceInfo, (self, CL_DEVICE_MAX_WORK_ITEM_SIZES, size, value, 0));
1158 int i, n = size / sizeof (*value);
1159 EXTEND (SP, n);
1160 for (i = 0; i < n; ++i)
1161 PUSHs (sv_2mortal (newSVuv (value [i])));
1162
1163 void
1164 error_correction_support (OpenCL::Device self)
1165 ALIAS:
1166 error_correction_support = CL_DEVICE_ERROR_CORRECTION_SUPPORT
1167 endian_little = CL_DEVICE_ENDIAN_LITTLE
1168 available = CL_DEVICE_AVAILABLE
1169 compiler_available = CL_DEVICE_COMPILER_AVAILABLE
1170 host_unified_memory = CL_DEVICE_HOST_UNIFIED_MEMORY
1171 PPCODE:
1172 cl_uint value [1];
1173 NEED_SUCCESS (GetDeviceInfo, (self, ix, sizeof (value), value, 0));
1174 EXTEND (SP, 1);
1175 const int i = 0;
1176 PUSHs (sv_2mortal (value [i] ? &PL_sv_yes : &PL_sv_no));
1177
1178 void
1179 platform (OpenCL::Device self)
1180 PPCODE:
1181 cl_platform_id value [1];
1182 NEED_SUCCESS (GetDeviceInfo, (self, CL_DEVICE_PLATFORM, sizeof (value), value, 0));
1183 EXTEND (SP, 1);
1184 const int i = 0;
1185 PUSH_CLOBJ (stash_platform, value [i]);
1186
1187 void
1188 name (OpenCL::Device self)
1189 ALIAS:
1190 name = CL_DEVICE_NAME
1191 vendor = CL_DEVICE_VENDOR
1192 driver_version = CL_DRIVER_VERSION
1193 profile = CL_DEVICE_PROFILE
1194 version = CL_DEVICE_VERSION
1195 extensions = CL_DEVICE_EXTENSIONS
1196 PPCODE:
1197 size_t size;
1198 NEED_SUCCESS (GetDeviceInfo, (self, ix, 0, 0, &size));
1199 char *value = tmpbuf (size);
1200 NEED_SUCCESS (GetDeviceInfo, (self, ix, size, value, 0));
1201 EXTEND (SP, 1);
1202 const int i = 0;
1203 PUSHs (sv_2mortal (newSVpv (value, 0)));
1204
1205 void
1206 parent_device_ext (OpenCL::Device self)
1207 PPCODE:
1208 cl_device_id value [1];
1209 NEED_SUCCESS (GetDeviceInfo, (self, CL_DEVICE_PARENT_DEVICE_EXT, sizeof (value), value, 0));
1210 EXTEND (SP, 1);
1211 const int i = 0;
1212 PUSH_CLOBJ (stash_device, value [i]);
1213
1214 void
1215 partition_types_ext (OpenCL::Device self)
1216 ALIAS:
1217 partition_types_ext = CL_DEVICE_PARTITION_TYPES_EXT
1218 affinity_domains_ext = CL_DEVICE_AFFINITY_DOMAINS_EXT
1219 partition_style_ext = CL_DEVICE_PARTITION_STYLE_EXT
1220 PPCODE:
1221 size_t size;
1222 NEED_SUCCESS (GetDeviceInfo, (self, ix, 0, 0, &size));
1223 cl_device_partition_property_ext *value = tmpbuf (size);
1224 NEED_SUCCESS (GetDeviceInfo, (self, ix, size, value, 0));
1225 int i, n = size / sizeof (*value);
1226 EXTEND (SP, n);
1227 for (i = 0; i < n; ++i)
1228 PUSHs (sv_2mortal (newSVuv (value [i])));
1229
1230 #END:device
1231
1232 MODULE = OpenCL PACKAGE = OpenCL::SubDevice
1233
1234 #if CL_VERSION_1_2
1235
1236 void
1237 DESTROY (OpenCL::SubDevice self)
1238 CODE:
1239 clReleaseDevice (self);
1240
1241 #endif
1242
1243 MODULE = OpenCL PACKAGE = OpenCL::Context
1244
1245 void
1246 DESTROY (OpenCL::Context self)
1247 CODE:
1248 clReleaseContext (self);
1249
1250 void
1251 info (OpenCL::Context self, cl_context_info name)
1252 PPCODE:
1253 INFO (Context)
1254
1255 void
1256 queue (OpenCL::Context self, OpenCL::Device device, cl_command_queue_properties properties = 0)
1257 PPCODE:
1258 NEED_SUCCESS_ARG (cl_command_queue queue, CreateCommandQueue, (self, device, properties, &res));
1259 XPUSH_CLOBJ (stash_queue, queue);
1260
1261 void
1262 user_event (OpenCL::Context self)
1263 PPCODE:
1264 NEED_SUCCESS_ARG (cl_event ev, CreateUserEvent, (self, &res));
1265 XPUSH_CLOBJ (stash_userevent, ev);
1266
1267 void
1268 buffer (OpenCL::Context self, cl_mem_flags flags, size_t len)
1269 PPCODE:
1270 if (flags & (CL_MEM_USE_HOST_PTR | CL_MEM_COPY_HOST_PTR))
1271 croak ("OpenCL::Context::buffer: cannot use/copy host ptr when no data is given, use $context->buffer_sv instead?");
1272
1273 NEED_SUCCESS_ARG (cl_mem mem, CreateBuffer, (self, flags, len, 0, &res));
1274 XPUSH_CLOBJ (stash_bufferobj, mem);
1275
1276 void
1277 buffer_sv (OpenCL::Context self, cl_mem_flags flags, SV *data)
1278 PPCODE:
1279 STRLEN len;
1280 char *ptr = SvOK (data) ? SvPVbyte (data, len) : 0;
1281 if (!(flags & (CL_MEM_USE_HOST_PTR | CL_MEM_COPY_HOST_PTR)))
1282 croak ("OpenCL::Context::buffer_sv: you have to specify use or copy host ptr when buffer data is given, use $context->buffer instead?");
1283 NEED_SUCCESS_ARG (cl_mem mem, CreateBuffer, (self, flags, len, ptr, &res));
1284 XPUSH_CLOBJ (stash_bufferobj, mem);
1285
1286 #if CL_VERSION_1_2
1287
1288 void
1289 image (OpenCL::Context self, cl_mem_flags flags, cl_channel_order channel_order, cl_channel_type channel_type, cl_mem_object_type type, size_t width, size_t height, size_t depth = 0, size_t array_size = 0, size_t row_pitch = 0, size_t slice_pitch = 0, cl_uint num_mip_level = 0, cl_uint num_samples = 0, SV *data = &PL_sv_undef)
1290 PPCODE:
1291 STRLEN len;
1292 char *ptr = SvOK (data) ? SvPVbyte (data, len) : 0;
1293 const cl_image_format format = { channel_order, channel_type };
1294 const cl_image_desc desc = {
1295 type,
1296 width, height, depth,
1297 array_size, row_pitch, slice_pitch,
1298 num_mip_level, num_samples,
1299 type == CL_MEM_OBJECT_IMAGE1D_BUFFER ? (cl_mem)SvCLOBJ (cv, "data", data, "OpenCL::Buffer") : 0
1300 };
1301 NEED_SUCCESS_ARG (cl_mem mem, CreateImage, (self, flags, &format, &desc, ptr, &res));
1302 HV *stash = stash_image;
1303 switch (type)
1304 {
1305 case CL_MEM_OBJECT_IMAGE1D_BUFFER: stash = stash_image1dbuffer; break;
1306 case CL_MEM_OBJECT_IMAGE1D: stash = stash_image1d; break;
1307 case CL_MEM_OBJECT_IMAGE1D_ARRAY: stash = stash_image2darray; break;
1308 case CL_MEM_OBJECT_IMAGE2D: stash = stash_image2d; break;
1309 case CL_MEM_OBJECT_IMAGE2D_ARRAY: stash = stash_image2darray; break;
1310 case CL_MEM_OBJECT_IMAGE3D: stash = stash_image3d; break;
1311 }
1312 XPUSH_CLOBJ (stash, mem);
1313
1314 #endif
1315
1316 void
1317 image2d (OpenCL::Context self, cl_mem_flags flags, cl_channel_order channel_order, cl_channel_type channel_type, size_t width, size_t height, size_t row_pitch = 0, SV *data = &PL_sv_undef)
1318 PPCODE:
1319 STRLEN len;
1320 char *ptr = SvOK (data) ? SvPVbyte (data, len) : 0;
1321 const cl_image_format format = { channel_order, channel_type };
1322 #if PREFER_1_1
1323 NEED_SUCCESS_ARG (cl_mem mem, CreateImage2D, (self, flags, &format, width, height, row_pitch, ptr, &res));
1324 #else
1325 const cl_image_desc desc = { CL_MEM_OBJECT_IMAGE2D, width, height, 0, 0, row_pitch, 0, 0, 0, 0 };
1326 NEED_SUCCESS_ARG (cl_mem mem, CreateImage, (self, flags, &format, &desc, ptr, &res));
1327 #endif
1328 XPUSH_CLOBJ (stash_image2d, mem);
1329
1330 void
1331 image3d (OpenCL::Context self, cl_mem_flags flags, cl_channel_order channel_order, cl_channel_type channel_type, size_t width, size_t height, size_t depth, size_t row_pitch = 0, size_t slice_pitch = 0, SV *data = &PL_sv_undef)
1332 PPCODE:
1333 STRLEN len;
1334 char *ptr = SvOK (data) ? SvPVbyte (data, len) : 0;
1335 const cl_image_format format = { channel_order, channel_type };
1336 #if PREFER_1_1
1337 NEED_SUCCESS_ARG (cl_mem mem, CreateImage3D, (self, flags, &format, width, height, depth, row_pitch, slice_pitch, ptr, &res));
1338 #else
1339 const cl_image_desc desc = { CL_MEM_OBJECT_IMAGE3D, width, height, depth, 0, row_pitch, slice_pitch, 0, 0, 0 };
1340 NEED_SUCCESS_ARG (cl_mem mem, CreateImage, (self, flags, &format, &desc, ptr, &res));
1341 #endif
1342 XPUSH_CLOBJ (stash_image3d, mem);
1343
1344 #if cl_apple_gl_sharing || cl_khr_gl_sharing
1345
1346 void
1347 gl_buffer (OpenCL::Context self, cl_mem_flags flags, cl_GLuint bufobj)
1348 PPCODE:
1349 NEED_SUCCESS_ARG (cl_mem mem, CreateFromGLBuffer, (self, flags, bufobj, &res));
1350 XPUSH_CLOBJ (stash_bufferobj, mem);
1351
1352 void
1353 gl_renderbuffer (OpenCL::Context self, cl_mem_flags flags, cl_GLuint renderbuffer)
1354 PPCODE:
1355 NEED_SUCCESS_ARG (cl_mem mem, CreateFromGLRenderbuffer, (self, flags, renderbuffer, &res));
1356 XPUSH_CLOBJ (stash_image2d, mem);
1357
1358 #if CL_VERSION_1_2
1359
1360 void
1361 gl_texture (OpenCL::Context self, cl_mem_flags flags, cl_GLenum target, cl_GLint miplevel, cl_GLuint texture)
1362 ALIAS:
1363 PPCODE:
1364 NEED_SUCCESS_ARG (cl_mem mem, CreateFromGLTexture, (self, flags, target, miplevel, texture, &res));
1365 cl_gl_object_type type;
1366 NEED_SUCCESS (GetGLObjectInfo, (mem, &type, 0)); // TODO: use target instead?
1367 HV *stash = stash_memory;
1368 switch (type)
1369 {
1370 case CL_GL_OBJECT_TEXTURE_BUFFER: stash = stash_image1dbuffer; break;
1371 case CL_GL_OBJECT_TEXTURE1D: stash = stash_image1d; break;
1372 case CL_GL_OBJECT_TEXTURE1D_ARRAY: stash = stash_image2darray; break;
1373 case CL_GL_OBJECT_TEXTURE2D: stash = stash_image2d; break;
1374 case CL_GL_OBJECT_TEXTURE2D_ARRAY: stash = stash_image2darray; break;
1375 case CL_GL_OBJECT_TEXTURE3D: stash = stash_image3d; break;
1376 }
1377 XPUSH_CLOBJ (stash, mem);
1378
1379 #endif
1380
1381 void
1382 gl_texture2d (OpenCL::Context self, cl_mem_flags flags, cl_GLenum target, cl_GLint miplevel, cl_GLuint texture)
1383 PPCODE:
1384 #if PREFER_1_1
1385 NEED_SUCCESS_ARG (cl_mem mem, CreateFromGLTexture2D, (self, flags, target, miplevel, texture, &res));
1386 #else
1387 NEED_SUCCESS_ARG (cl_mem mem, CreateFromGLTexture , (self, flags, target, miplevel, texture, &res));
1388 #endif
1389 XPUSH_CLOBJ (stash_image2d, mem);
1390
1391 void
1392 gl_texture3d (OpenCL::Context self, cl_mem_flags flags, cl_GLenum target, cl_GLint miplevel, cl_GLuint texture)
1393 PPCODE:
1394 #if PREFER_1_1
1395 NEED_SUCCESS_ARG (cl_mem mem, CreateFromGLTexture3D, (self, flags, target, miplevel, texture, &res));
1396 #else
1397 NEED_SUCCESS_ARG (cl_mem mem, CreateFromGLTexture , (self, flags, target, miplevel, texture, &res));
1398 #endif
1399 XPUSH_CLOBJ (stash_image3d, mem);
1400
1401 #endif
1402
1403 void
1404 supported_image_formats (OpenCL::Context self, cl_mem_flags flags, cl_mem_object_type image_type)
1405 PPCODE:
1406 {
1407 cl_uint count;
1408 cl_image_format *list;
1409 int i;
1410
1411 NEED_SUCCESS (GetSupportedImageFormats, (self, flags, image_type, 0, 0, &count));
1412 Newx (list, count, cl_image_format);
1413 NEED_SUCCESS (GetSupportedImageFormats, (self, flags, image_type, count, list, 0));
1414
1415 EXTEND (SP, count);
1416 for (i = 0; i < count; ++i)
1417 {
1418 AV *av = newAV ();
1419 av_store (av, 1, newSVuv (list [i].image_channel_data_type));
1420 av_store (av, 0, newSVuv (list [i].image_channel_order));
1421 PUSHs (sv_2mortal (newRV_noinc ((SV *)av)));
1422 }
1423 }
1424
1425 void
1426 sampler (OpenCL::Context self, cl_bool normalized_coords, cl_addressing_mode addressing_mode, cl_filter_mode filter_mode)
1427 PPCODE:
1428 NEED_SUCCESS_ARG (cl_sampler sampler, CreateSampler, (self, normalized_coords, addressing_mode, filter_mode, &res));
1429 XPUSH_CLOBJ (stash_sampler, sampler);
1430
1431 void
1432 program_with_source (OpenCL::Context self, SV *program)
1433 PPCODE:
1434 STRLEN len;
1435 size_t len2;
1436 const char *ptr = SvPVbyte (program, len);
1437
1438 len2 = len;
1439 NEED_SUCCESS_ARG (cl_program prog, CreateProgramWithSource, (self, 1, &ptr, &len2, &res));
1440 XPUSH_CLOBJ (stash_program, prog);
1441
1442 void
1443 program_with_binary (OpenCL::Context self, SV *devices, SV *binaries)
1444 PPCODE:
1445 cl_uint device_count;
1446 cl_device_id *device_list = object_list (cv, 0, "devices", devices, "OpenCL::Device", &device_count);
1447
1448 if (!SvROK (binaries) || SvTYPE (SvRV (binaries)) != SVt_PVAV)
1449 croak ("OpenCL::Context::program_with_binary: binaries must be specified as reference to an array of strings");
1450
1451 binaries = SvRV (binaries);
1452
1453 if (device_count != av_len ((AV *)binaries) + 1)
1454 croak ("OpenCL::Context::program_with_binary: differing numbers of devices and binaries are not allowed");
1455
1456 size_t *length_list = tmpbuf (sizeof (*length_list) * device_count);
1457 const unsigned char **binary_list = tmpbuf (sizeof (*binary_list) * device_count);
1458 cl_int *status_list = tmpbuf (sizeof (*status_list) * device_count);
1459
1460 int i;
1461 for (i = 0; i < device_count; ++i)
1462 {
1463 STRLEN len;
1464 binary_list [i] = (const unsigned char *)SvPVbyte (*av_fetch ((AV *)binaries, i, 0), len);
1465 length_list [i] = len;
1466 }
1467
1468 NEED_SUCCESS_ARG (cl_program prog, CreateProgramWithBinary, (self, device_count, device_list,
1469 length_list, binary_list,
1470 GIMME_V == G_ARRAY ? status_list : 0, &res));
1471
1472 EXTEND (SP, 2);
1473 PUSH_CLOBJ (stash_program, prog);
1474
1475 if (GIMME_V == G_ARRAY)
1476 {
1477 AV *av = newAV ();
1478 PUSHs (sv_2mortal (newRV_noinc ((SV *)av)));
1479
1480 for (i = device_count; i--; )
1481 av_store (av, i, newSViv (status_list [i]));
1482 }
1483
1484 #if CL_VERSION_1_2
1485
1486 void
1487 program_with_built_in_kernels (OpenCL::Context self, SV *devices, SV *kernel_names)
1488 PPCODE:
1489 cl_uint device_count;
1490 cl_device_id *device_list = object_list (cv, 0, "devices", devices, "OpenCL::Device", &device_count);
1491
1492 NEED_SUCCESS_ARG (cl_program prog, CreateProgramWithBuiltInKernels, (self, device_count, device_list, SvPVbyte_nolen (kernel_names), &res));
1493
1494 XPUSH_CLOBJ (stash_program, prog);
1495
1496 void
1497 link_program (OpenCL::Context self, SV *devices, SV *options, SV *programs, SV *notify = &PL_sv_undef)
1498 CODE:
1499 cl_uint device_count = 0;
1500 cl_device_id *device_list = 0;
1501
1502 if (SvOK (devices))
1503 device_list = object_list (cv, 1, "devices", devices, "OpenCL::Device", &device_count);
1504
1505 cl_uint program_count;
1506 cl_program *program_list = object_list (cv, 0, "programs", programs, "OpenCL::Program", &program_count);
1507
1508 void *user_data;
1509 program_callback pfn_notify = make_program_callback (notify, &user_data);
1510
1511 NEED_SUCCESS_ARG (cl_program prog, LinkProgram, (self, device_count, device_list, SvPVbyte_nolen (options),
1512 program_count, program_list, pfn_notify, user_data, &res));
1513
1514 XPUSH_CLOBJ (stash_program, prog);
1515
1516 #endif
1517
1518 #BEGIN:context
1519
1520 void
1521 reference_count (OpenCL::Context self)
1522 ALIAS:
1523 reference_count = CL_CONTEXT_REFERENCE_COUNT
1524 num_devices = CL_CONTEXT_NUM_DEVICES
1525 PPCODE:
1526 cl_uint value [1];
1527 NEED_SUCCESS (GetContextInfo, (self, ix, sizeof (value), value, 0));
1528 EXTEND (SP, 1);
1529 const int i = 0;
1530 PUSHs (sv_2mortal (newSVuv (value [i])));
1531
1532 void
1533 devices (OpenCL::Context self)
1534 PPCODE:
1535 size_t size;
1536 NEED_SUCCESS (GetContextInfo, (self, CL_CONTEXT_DEVICES, 0, 0, &size));
1537 cl_device_id *value = tmpbuf (size);
1538 NEED_SUCCESS (GetContextInfo, (self, CL_CONTEXT_DEVICES, size, value, 0));
1539 int i, n = size / sizeof (*value);
1540 EXTEND (SP, n);
1541 for (i = 0; i < n; ++i)
1542 PUSH_CLOBJ (stash_device, value [i]);
1543
1544 void
1545 properties (OpenCL::Context self)
1546 PPCODE:
1547 size_t size;
1548 NEED_SUCCESS (GetContextInfo, (self, CL_CONTEXT_PROPERTIES, 0, 0, &size));
1549 cl_context_properties *value = tmpbuf (size);
1550 NEED_SUCCESS (GetContextInfo, (self, CL_CONTEXT_PROPERTIES, size, value, 0));
1551 int i, n = size / sizeof (*value);
1552 EXTEND (SP, n);
1553 for (i = 0; i < n; ++i)
1554 PUSHs (sv_2mortal (newSVuv ((UV)value [i])));
1555
1556 #END:context
1557
1558 MODULE = OpenCL PACKAGE = OpenCL::Queue
1559
1560 void
1561 DESTROY (OpenCL::Queue self)
1562 CODE:
1563 clReleaseCommandQueue (self);
1564
1565 void
1566 read_buffer (OpenCL::Queue self, OpenCL::Buffer mem, cl_bool blocking, size_t offset, size_t len, SV *data, ...)
1567 ALIAS:
1568 enqueue_read_buffer = 0
1569 PPCODE:
1570 EVENT_LIST (6);
1571
1572 SvUPGRADE (data, SVt_PV);
1573 SvGROW (data, len);
1574 SvPOK_only (data);
1575 SvCUR_set (data, len);
1576
1577 cl_event ev = 0;
1578 NEED_SUCCESS (EnqueueReadBuffer, (self, mem, blocking, offset, len, SvPVX (data), event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0));
1579
1580 if (ev)
1581 XPUSH_CLOBJ (stash_event, ev);
1582
1583 void
1584 write_buffer (OpenCL::Queue self, OpenCL::Buffer mem, cl_bool blocking, size_t offset, SV *data, ...)
1585 ALIAS:
1586 enqueue_write_buffer = 0
1587 PPCODE:
1588 EVENT_LIST (5);
1589
1590 STRLEN len;
1591 char *ptr = SvPVbyte (data, len);
1592
1593 cl_event ev = 0;
1594 NEED_SUCCESS (EnqueueWriteBuffer, (self, mem, blocking, offset, len, ptr, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0));
1595
1596 if (ev)
1597 XPUSH_CLOBJ (stash_event, ev);
1598
1599 #if CL_VERSION_1_2
1600
1601 void
1602 fill_buffer (OpenCL::Queue self, OpenCL::Buffer mem, SV *data, size_t offset, size_t size, ...)
1603 ALIAS:
1604 enqueue_fill_buffer = 0
1605 PPCODE:
1606 EVENT_LIST (5);
1607
1608 STRLEN len;
1609 char *ptr = SvPVbyte (data, len);
1610
1611 cl_event ev = 0;
1612 NEED_SUCCESS (EnqueueFillBuffer, (self, mem, ptr, len, offset, size, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0));
1613
1614 if (ev)
1615 XPUSH_CLOBJ (stash_event, ev);
1616
1617 void
1618 fill_image (OpenCL::Queue self, OpenCL::Image img, NV r, NV g, NV b, NV a, size_t x, size_t y, size_t z, size_t width, size_t height, size_t depth, ...)
1619 ALIAS:
1620 enqueue_fill_image = 0
1621 PPCODE:
1622 EVENT_LIST (12);
1623
1624 const size_t origin [3] = { x, y, z };
1625 const size_t region [3] = { width, height, depth };
1626
1627 const cl_float c_f [4] = { r, g, b, a };
1628 const cl_uint c_u [4] = { r, g, b, a };
1629 const cl_int c_s [4] = { r, g, b, a };
1630 const void *c_fus [3] = { &c_f, &c_u, &c_s };
1631 static const char fus [] = { 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 1, 1, 1, 0, 0 };
1632 cl_image_format format;
1633 NEED_SUCCESS (GetImageInfo, (img, CL_IMAGE_FORMAT, sizeof (format), &format, 0));
1634 assert (sizeof (fus) == CL_FLOAT + 1 - CL_SNORM_INT8);
1635 if (format.image_channel_data_type < CL_SNORM_INT8 || CL_FLOAT < format.image_channel_data_type)
1636 croak ("enqueue_fill_image: image has unsupported channel type, only opencl 1.2 channel types supported.");
1637
1638 cl_event ev = 0;
1639 NEED_SUCCESS (EnqueueFillImage, (self, img, c_fus [fus [format.image_channel_data_type - CL_SNORM_INT8]],
1640 origin, region, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0));
1641
1642 if (ev)
1643 XPUSH_CLOBJ (stash_event, ev);
1644
1645 #endif
1646
1647 void
1648 copy_buffer (OpenCL::Queue self, OpenCL::Buffer src, OpenCL::Buffer dst, size_t src_offset, size_t dst_offset, size_t len, ...)
1649 ALIAS:
1650 enqueue_copy_buffer = 0
1651 PPCODE:
1652 EVENT_LIST (6);
1653
1654 cl_event ev = 0;
1655 NEED_SUCCESS (EnqueueCopyBuffer, (self, src, dst, src_offset, dst_offset, len, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0));
1656
1657 if (ev)
1658 XPUSH_CLOBJ (stash_event, ev);
1659
1660 void
1661 read_buffer_rect (OpenCL::Queue self, OpenCL::Memory buf, cl_bool blocking, size_t buf_x, size_t buf_y, size_t buf_z, size_t host_x, size_t host_y, size_t host_z, size_t width, size_t height, size_t depth, size_t buf_row_pitch, size_t buf_slice_pitch, size_t host_row_pitch, size_t host_slice_pitch, SV *data, ...)
1662 ALIAS:
1663 enqueue_read_buffer_rect = 0
1664 PPCODE:
1665 EVENT_LIST (17);
1666
1667 const size_t buf_origin [3] = { buf_x , buf_y , buf_z };
1668 const size_t host_origin[3] = { host_x, host_y, host_z };
1669 const size_t region[3] = { width, height, depth };
1670
1671 if (!buf_row_pitch)
1672 buf_row_pitch = region [0];
1673
1674 if (!buf_slice_pitch)
1675 buf_slice_pitch = region [1] * buf_row_pitch;
1676
1677 if (!host_row_pitch)
1678 host_row_pitch = region [0];
1679
1680 if (!host_slice_pitch)
1681 host_slice_pitch = region [1] * host_row_pitch;
1682
1683 size_t len = host_row_pitch * host_slice_pitch * region [2];
1684
1685 SvUPGRADE (data, SVt_PV);
1686 SvGROW (data, len);
1687 SvPOK_only (data);
1688 SvCUR_set (data, len);
1689
1690 cl_event ev = 0;
1691 NEED_SUCCESS (EnqueueReadBufferRect, (self, buf, blocking, buf_origin, host_origin, region, buf_row_pitch, buf_slice_pitch, host_row_pitch, host_slice_pitch, SvPVX (data), event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0));
1692
1693 if (ev)
1694 XPUSH_CLOBJ (stash_event, ev);
1695
1696 void
1697 write_buffer_rect (OpenCL::Queue self, OpenCL::Memory buf, cl_bool blocking, size_t buf_x, size_t buf_y, size_t buf_z, size_t host_x, size_t host_y, size_t host_z, size_t width, size_t height, size_t depth, size_t buf_row_pitch, size_t buf_slice_pitch, size_t host_row_pitch, size_t host_slice_pitch, SV *data, ...)
1698 ALIAS:
1699 enqueue_write_buffer_rect = 0
1700 PPCODE:
1701 EVENT_LIST (17);
1702
1703 const size_t buf_origin [3] = { buf_x , buf_y , buf_z };
1704 const size_t host_origin[3] = { host_x, host_y, host_z };
1705 const size_t region[3] = { width, height, depth };
1706 STRLEN len;
1707 char *ptr = SvPVbyte (data, len);
1708
1709 if (!buf_row_pitch)
1710 buf_row_pitch = region [0];
1711
1712 if (!buf_slice_pitch)
1713 buf_slice_pitch = region [1] * buf_row_pitch;
1714
1715 if (!host_row_pitch)
1716 host_row_pitch = region [0];
1717
1718 if (!host_slice_pitch)
1719 host_slice_pitch = region [1] * host_row_pitch;
1720
1721 size_t min_len = host_row_pitch * host_slice_pitch * region [2];
1722
1723 if (len < min_len)
1724 croak ("clEnqueueWriteImage: data string is shorter than what would be transferred");
1725
1726 cl_event ev = 0;
1727 NEED_SUCCESS (EnqueueWriteBufferRect, (self, buf, blocking, buf_origin, host_origin, region, buf_row_pitch, buf_slice_pitch, host_row_pitch, host_slice_pitch, ptr, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0));
1728
1729 if (ev)
1730 XPUSH_CLOBJ (stash_event, ev);
1731
1732 void
1733 copy_buffer_rect (OpenCL::Queue self, OpenCL::Buffer src, OpenCL::Buffer dst, size_t src_x, size_t src_y, size_t src_z, size_t dst_x, size_t dst_y, size_t dst_z, size_t width, size_t height, size_t depth, size_t src_row_pitch, size_t src_slice_pitch, size_t dst_row_pitch, size_t dst_slice_pitch, ...)
1734 ALIAS:
1735 enqueue_copy_buffer_rect = 0
1736 PPCODE:
1737 EVENT_LIST (16);
1738
1739 const size_t src_origin[3] = { src_x, src_y, src_z };
1740 const size_t dst_origin[3] = { dst_x, dst_y, dst_z };
1741 const size_t region[3] = { width, height, depth };
1742
1743 cl_event ev = 0;
1744 NEED_SUCCESS (EnqueueCopyBufferRect, (self, src, dst, src_origin, dst_origin, region, src_row_pitch, src_slice_pitch, dst_row_pitch, dst_slice_pitch, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0));
1745
1746 if (ev)
1747 XPUSH_CLOBJ (stash_event, ev);
1748
1749 void
1750 read_image (OpenCL::Queue self, OpenCL::Image src, cl_bool blocking, size_t src_x, size_t src_y, size_t src_z, size_t width, size_t height, size_t depth, size_t row_pitch, size_t slice_pitch, SV *data, ...)
1751 ALIAS:
1752 enqueue_read_image = 0
1753 PPCODE:
1754 EVENT_LIST (12);
1755
1756 const size_t src_origin[3] = { src_x, src_y, src_z };
1757 const size_t region[3] = { width, height, depth };
1758
1759 if (!row_pitch)
1760 row_pitch = img_row_pitch (src);
1761
1762 if (depth > 1 && !slice_pitch)
1763 slice_pitch = row_pitch * height;
1764
1765 size_t len = slice_pitch ? slice_pitch * depth : row_pitch * height;
1766
1767 SvUPGRADE (data, SVt_PV);
1768 SvGROW (data, len);
1769 SvPOK_only (data);
1770 SvCUR_set (data, len);
1771
1772 cl_event ev = 0;
1773 NEED_SUCCESS (EnqueueReadImage, (self, src, blocking, src_origin, region, row_pitch, slice_pitch, SvPVX (data), event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0));
1774
1775 if (ev)
1776 XPUSH_CLOBJ (stash_event, ev);
1777
1778 void
1779 write_image (OpenCL::Queue self, OpenCL::Image dst, cl_bool blocking, size_t dst_x, size_t dst_y, size_t dst_z, size_t width, size_t height, size_t depth, size_t row_pitch, size_t slice_pitch, SV *data, ...)
1780 ALIAS:
1781 enqueue_write_image = 0
1782 PPCODE:
1783 EVENT_LIST (12);
1784
1785 const size_t dst_origin[3] = { dst_x, dst_y, dst_z };
1786 const size_t region[3] = { width, height, depth };
1787 STRLEN len;
1788 char *ptr = SvPVbyte (data, len);
1789
1790 if (!row_pitch)
1791 row_pitch = img_row_pitch (dst);
1792
1793 if (depth > 1 && !slice_pitch)
1794 slice_pitch = row_pitch * height;
1795
1796 size_t min_len = slice_pitch ? slice_pitch * depth : row_pitch * height;
1797
1798 if (len < min_len)
1799 croak ("clEnqueueWriteImage: data string is shorter than what would be transferred");
1800
1801 cl_event ev = 0;
1802 NEED_SUCCESS (EnqueueWriteImage, (self, dst, blocking, dst_origin, region, row_pitch, slice_pitch, ptr, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0));
1803
1804 if (ev)
1805 XPUSH_CLOBJ (stash_event, ev);
1806
1807 void
1808 copy_image (OpenCL::Queue self, OpenCL::Image src, OpenCL::Image dst, size_t src_x, size_t src_y, size_t src_z, size_t dst_x, size_t dst_y, size_t dst_z, size_t width, size_t height, size_t depth, ...)
1809 ALIAS:
1810 enqueue_copy_image = 0
1811 PPCODE:
1812 EVENT_LIST (12);
1813
1814 const size_t src_origin[3] = { src_x, src_y, src_z };
1815 const size_t dst_origin[3] = { dst_x, dst_y, dst_z };
1816 const size_t region[3] = { width, height, depth };
1817
1818 cl_event ev = 0;
1819 NEED_SUCCESS (EnqueueCopyImage, (self, src, dst, src_origin, dst_origin, region, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0));
1820
1821 if (ev)
1822 XPUSH_CLOBJ (stash_event, ev);
1823
1824 void
1825 copy_image_to_buffer (OpenCL::Queue self, OpenCL::Image src, OpenCL::Buffer dst, size_t src_x, size_t src_y, size_t src_z, size_t width, size_t height, size_t depth, size_t dst_offset, ...)
1826 ALIAS:
1827 enqueue_copy_image_to_buffer = 0
1828 PPCODE:
1829 EVENT_LIST (10);
1830
1831 const size_t src_origin[3] = { src_x, src_y, src_z };
1832 const size_t region [3] = { width, height, depth };
1833
1834 cl_event ev = 0;
1835 NEED_SUCCESS (EnqueueCopyImageToBuffer, (self, src, dst, src_origin, region, dst_offset, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0));
1836
1837 if (ev)
1838 XPUSH_CLOBJ (stash_event, ev);
1839
1840 void
1841 copy_buffer_to_image (OpenCL::Queue self, OpenCL::Buffer src, OpenCL::Image dst, size_t src_offset, size_t dst_x, size_t dst_y, size_t dst_z, size_t width, size_t height, size_t depth, ...)
1842 ALIAS:
1843 enqueue_copy_buffer_to_image = 0
1844 PPCODE:
1845 EVENT_LIST (10);
1846
1847 const size_t dst_origin[3] = { dst_x, dst_y, dst_z };
1848 const size_t region [3] = { width, height, depth };
1849
1850 cl_event ev = 0;
1851 NEED_SUCCESS (EnqueueCopyBufferToImage, (self, src, dst, src_offset, dst_origin, region, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0));
1852
1853 if (ev)
1854 XPUSH_CLOBJ (stash_event, ev);
1855
1856 void
1857 map_buffer (OpenCL::Queue self, OpenCL::Buffer buf, cl_bool blocking = 1, cl_map_flags map_flags = CL_MAP_READ | CL_MAP_WRITE, size_t offset = 0, SV *cb_ = &PL_sv_undef, ...)
1858 ALIAS:
1859 enqueue_map_buffer = 0
1860 PPCODE:
1861 EVENT_LIST (6);
1862
1863 size_t cb = SvIV (cb_);
1864
1865 if (!SvOK (cb_))
1866 {
1867 NEED_SUCCESS (GetMemObjectInfo, (buf, CL_MEM_SIZE, sizeof (cb), &cb, 0));
1868 cb -= offset;
1869 }
1870
1871 cl_event ev;
1872 NEED_SUCCESS_ARG (void *ptr, EnqueueMapBuffer, (self, buf, blocking, map_flags, offset, cb, event_list_count, event_list_ptr, &ev, &res));
1873 XPUSHs (mapped_new (stash_mappedbuffer, self, buf, map_flags, ptr, cb, ev, 0, 0, 1, cb, 1, 1));
1874
1875 void
1876 map_image (OpenCL::Queue self, OpenCL::Image img, cl_bool blocking = 1, cl_map_flags map_flags = CL_MAP_READ | CL_MAP_WRITE, size_t x = 0, size_t y = 0, size_t z = 0, SV *width_ = &PL_sv_undef, SV *height_ = &PL_sv_undef, SV *depth_ = &PL_sv_undef, ...)
1877 ALIAS:
1878 enqueue_map_image = 0
1879 PPCODE:
1880 size_t width = SvIV (width_);
1881 if (!SvOK (width_))
1882 {
1883 NEED_SUCCESS (GetImageInfo, (img, CL_IMAGE_WIDTH, sizeof (width), &width, 0));
1884 width -= x;
1885 }
1886
1887 size_t height = SvIV (width_);
1888 if (!SvOK (height_))
1889 {
1890 NEED_SUCCESS (GetImageInfo, (img, CL_IMAGE_HEIGHT, sizeof (height), &height, 0));
1891 height -= y;
1892
1893 // stupid opencl returns 0 for depth, but requires 1 for 2d images
1894 if (!height)
1895 height = 1;
1896 }
1897
1898 size_t depth = SvIV (width_);
1899 if (!SvOK (depth_))
1900 {
1901 NEED_SUCCESS (GetImageInfo, (img, CL_IMAGE_DEPTH, sizeof (depth), &depth, 0));
1902 depth -= z;
1903
1904 // stupid opencl returns 0 for depth, but requires 1 for 2d images
1905 if (!depth)
1906 depth = 1;
1907 }
1908
1909 const size_t origin[3] = { x, y, z };
1910 const size_t region[3] = { width, height, depth };
1911 size_t row_pitch, slice_pitch;
1912 EVENT_LIST (10);
1913
1914 cl_event ev;
1915 NEED_SUCCESS_ARG (void *ptr, EnqueueMapImage, (self, img, blocking, map_flags, origin, region, &row_pitch, &slice_pitch, event_list_count, event_list_ptr, &ev, &res));
1916
1917 size_t cb = slice_pitch ? slice_pitch * region [2]
1918 : row_pitch ? row_pitch * region [1]
1919 : region [0];
1920
1921 XPUSHs (mapped_new (stash_mappedimage, self, img, map_flags, ptr, cb, ev, row_pitch, slice_pitch, 0, width, height, depth));
1922
1923 void
1924 unmap (OpenCL::Queue self, OpenCL::Mapped mapped, ...)
1925 PPCODE:
1926 mapped_unmap (cv, ST (1), mapped, self, &ST (2), items - 2);
1927 if (GIMME_V != G_VOID)
1928 {
1929 clRetainEvent (mapped->event);
1930 XPUSH_CLOBJ (stash_event, mapped->event);
1931 }
1932
1933 void
1934 task (OpenCL::Queue self, OpenCL::Kernel kernel, ...)
1935 ALIAS:
1936 enqueue_task = 0
1937 PPCODE:
1938 EVENT_LIST (2);
1939
1940 cl_event ev = 0;
1941 NEED_SUCCESS (EnqueueTask, (self, kernel, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0));
1942
1943 if (ev)
1944 XPUSH_CLOBJ (stash_event, ev);
1945
1946 void
1947 nd_range_kernel (OpenCL::Queue self, OpenCL::Kernel kernel, SV *global_work_offset, SV *global_work_size, SV *local_work_size = &PL_sv_undef, ...)
1948 ALIAS:
1949 enqueue_nd_range_kernel = 0
1950 PPCODE:
1951 EVENT_LIST (5);
1952
1953 size_t *gwo = 0, *gws, *lws = 0;
1954 int gws_len;
1955 size_t *lists;
1956 int i;
1957
1958 if (!SvROK (global_work_size) || SvTYPE (SvRV (global_work_size)) != SVt_PVAV)
1959 croak ("clEnqueueNDRangeKernel: global_work_size must be an array reference");
1960
1961 gws_len = AvFILLp (SvRV (global_work_size)) + 1;
1962
1963 lists = tmpbuf (sizeof (size_t) * 3 * gws_len);
1964
1965 gws = lists + gws_len * 0;
1966 for (i = 0; i < gws_len; ++i)
1967 {
1968 gws [i] = SvIV (AvARRAY (SvRV (global_work_size))[i]);
1969 // at least nvidia crashes for 0-sized work group sizes, work around
1970 if (!gws [i])
1971 croak ("clEnqueueNDRangeKernel: global_work_size[%d] is zero, must be non-zero", i);
1972 }
1973
1974 if (SvOK (global_work_offset))
1975 {
1976 if (!SvROK (global_work_offset) || SvTYPE (SvRV (global_work_offset)) != SVt_PVAV)
1977 croak ("clEnqueueNDRangeKernel: global_work_offset must be undef or an array reference");
1978
1979 if (AvFILLp (SvRV (global_work_size)) + 1 != gws_len)
1980 croak ("clEnqueueNDRangeKernel: global_work_offset must be undef or an array of same size as global_work_size");
1981
1982 gwo = lists + gws_len * 1;
1983 for (i = 0; i < gws_len; ++i)
1984 gwo [i] = SvIV (AvARRAY (SvRV (global_work_offset))[i]);
1985 }
1986
1987 if (SvOK (local_work_size))
1988 {
1989 if ((SvOK (local_work_size) && !SvROK (local_work_size)) || SvTYPE (SvRV (local_work_size)) != SVt_PVAV)
1990 croak ("clEnqueueNDRangeKernel: local_work_size must be undef or an array reference");
1991
1992 if (AvFILLp (SvRV (local_work_size)) + 1 != gws_len)
1993 croak ("clEnqueueNDRangeKernel: local_work_local must be undef or an array of same size as global_work_size");
1994
1995 lws = lists + gws_len * 2;
1996 for (i = 0; i < gws_len; ++i)
1997 {
1998 lws [i] = SvIV (AvARRAY (SvRV (local_work_size))[i]);
1999 // at least nvidia crashes for 0-sized work group sizes, work around
2000 if (!lws [i])
2001 croak ("clEnqueueNDRangeKernel: local_work_size[%d] is zero, must be non-zero", i);
2002 }
2003 }
2004
2005 cl_event ev = 0;
2006 NEED_SUCCESS (EnqueueNDRangeKernel, (self, kernel, gws_len, gwo, gws, lws, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0));
2007
2008 if (ev)
2009 XPUSH_CLOBJ (stash_event, ev);
2010
2011 #if CL_VERSION_1_2
2012
2013 void
2014 migrate_mem_objects (OpenCL::Queue self, SV *objects, cl_mem_migration_flags flags, ...)
2015 ALIAS:
2016 enqueue_migrate_mem_objects = 0
2017 PPCODE:
2018 EVENT_LIST (3);
2019
2020 cl_uint obj_count;
2021 cl_mem *obj_list = object_list (cv, 0, "objects", objects, "OpenCL::Memory", &obj_count);
2022
2023 cl_event ev = 0;
2024 NEED_SUCCESS (EnqueueMigrateMemObjects, (self, obj_count, obj_list, flags, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0));
2025
2026 if (ev)
2027 XPUSH_CLOBJ (stash_event, ev);
2028
2029 #endif
2030
2031 #if cl_apple_gl_sharing || cl_khr_gl_sharing
2032
2033 void
2034 acquire_gl_objects (OpenCL::Queue self, SV *objects, ...)
2035 ALIAS:
2036 release_gl_objects = 1
2037 enqueue_acquire_gl_objects = 0
2038 enqueue_release_gl_objects = 1
2039 PPCODE:
2040 EVENT_LIST (2);
2041
2042 cl_uint obj_count;
2043 cl_mem *obj_list = object_list (cv, 0, "objects", objects, "OpenCL::Memory", &obj_count);
2044
2045 cl_event ev = 0;
2046
2047 if (ix)
2048 NEED_SUCCESS (EnqueueReleaseGLObjects, (self, obj_count, obj_list, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0));
2049 else
2050 NEED_SUCCESS (EnqueueAcquireGLObjects, (self, obj_count, obj_list, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0));
2051
2052 if (ev)
2053 XPUSH_CLOBJ (stash_event, ev);
2054
2055 #endif
2056
2057 void
2058 wait_for_events (OpenCL::Queue self, ...)
2059 ALIAS:
2060 enqueue_wait_for_events = 0
2061 CODE:
2062 EVENT_LIST (1);
2063 #if PREFER_1_1
2064 NEED_SUCCESS (EnqueueWaitForEvents, (self, event_list_count, event_list_ptr));
2065 #else
2066 NEED_SUCCESS (EnqueueBarrierWithWaitList, (self, event_list_count, event_list_ptr, 0));
2067 #endif
2068
2069 void
2070 marker (OpenCL::Queue self, ...)
2071 ALIAS:
2072 enqueue_marker = 0
2073 PPCODE:
2074 EVENT_LIST (1);
2075 cl_event ev = 0;
2076 #if PREFER_1_1
2077 if (!event_list_count)
2078 NEED_SUCCESS (EnqueueMarker, (self, GIMME_V != G_VOID ? &ev : 0));
2079 else
2080 #if CL_VERSION_1_2
2081 NEED_SUCCESS (EnqueueMarkerWithWaitList, (self, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0));
2082 #else
2083 {
2084 NEED_SUCCESS (EnqueueWaitForEvents, (self, event_list_count, event_list_ptr)); // also a barrier
2085 NEED_SUCCESS (EnqueueMarker, (self, GIMME_V != G_VOID ? &ev : 0));
2086 }
2087 #endif
2088 #else
2089 NEED_SUCCESS (EnqueueMarkerWithWaitList, (self, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0));
2090 #endif
2091 if (ev)
2092 XPUSH_CLOBJ (stash_event, ev);
2093
2094 void
2095 barrier (OpenCL::Queue self, ...)
2096 ALIAS:
2097 enqueue_barrier = 0
2098 PPCODE:
2099 EVENT_LIST (1);
2100 cl_event ev = 0;
2101 #if PREFER_1_1
2102 if (!event_list_count && GIMME_V == G_VOID)
2103 NEED_SUCCESS (EnqueueBarrier, (self));
2104 else
2105 #if CL_VERSION_1_2
2106 NEED_SUCCESS (EnqueueBarrierWithWaitList, (self, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0));
2107 #else
2108 {
2109 if (event_list_count)
2110 NEED_SUCCESS (EnqueueWaitForEvents, (self, event_list_count, event_list_ptr));
2111
2112 if (GIMME_V != G_VOID)
2113 NEED_SUCCESS (EnqueueMarker, (self, &ev));
2114 }
2115 #endif
2116 #else
2117 NEED_SUCCESS (EnqueueBarrierWithWaitList, (self, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0));
2118 #endif
2119 if (ev)
2120 XPUSH_CLOBJ (stash_event, ev);
2121
2122 void
2123 flush (OpenCL::Queue self)
2124 CODE:
2125 NEED_SUCCESS (Flush, (self));
2126
2127 void
2128 finish (OpenCL::Queue self)
2129 CODE:
2130 NEED_SUCCESS (Finish, (self));
2131
2132 void
2133 info (OpenCL::Queue self, cl_command_queue_info name)
2134 PPCODE:
2135 INFO (CommandQueue)
2136
2137 #BEGIN:command_queue
2138
2139 void
2140 context (OpenCL::Queue self)
2141 PPCODE:
2142 cl_context value [1];
2143 NEED_SUCCESS (GetCommandQueueInfo, (self, CL_QUEUE_CONTEXT, sizeof (value), value, 0));
2144 EXTEND (SP, 1);
2145 const int i = 0;
2146 NEED_SUCCESS (RetainContext, (value [i]));
2147 PUSH_CLOBJ (stash_context, value [i]);
2148
2149 void
2150 device (OpenCL::Queue self)
2151 PPCODE:
2152 cl_device_id value [1];
2153 NEED_SUCCESS (GetCommandQueueInfo, (self, CL_QUEUE_DEVICE, sizeof (value), value, 0));
2154 EXTEND (SP, 1);
2155 const int i = 0;
2156 PUSH_CLOBJ (stash_device, value [i]);
2157
2158 void
2159 reference_count (OpenCL::Queue self)
2160 PPCODE:
2161 cl_uint value [1];
2162 NEED_SUCCESS (GetCommandQueueInfo, (self, CL_QUEUE_REFERENCE_COUNT, sizeof (value), value, 0));
2163 EXTEND (SP, 1);
2164 const int i = 0;
2165 PUSHs (sv_2mortal (newSVuv (value [i])));
2166
2167 void
2168 properties (OpenCL::Queue self)
2169 PPCODE:
2170 cl_ulong value [1];
2171 NEED_SUCCESS (GetCommandQueueInfo, (self, CL_QUEUE_PROPERTIES, sizeof (value), value, 0));
2172 EXTEND (SP, 1);
2173 const int i = 0;
2174 PUSHs (sv_2mortal (newSVuv (value [i])));
2175
2176 #END:command_queue
2177
2178 MODULE = OpenCL PACKAGE = OpenCL::Memory
2179
2180 void
2181 DESTROY (OpenCL::Memory self)
2182 CODE:
2183 clReleaseMemObject (self);
2184
2185 void
2186 info (OpenCL::Memory self, cl_mem_info name)
2187 PPCODE:
2188 INFO (MemObject)
2189
2190 void
2191 destructor_callback (OpenCL::Memory self, SV *notify)
2192 PPCODE:
2193 clSetMemObjectDestructorCallback (self, eq_destructor_notify, SvREFCNT_inc (s_get_cv (notify)));
2194
2195 #BEGIN:mem
2196
2197 void
2198 type (OpenCL::Memory self)
2199 ALIAS:
2200 type = CL_MEM_TYPE
2201 map_count = CL_MEM_MAP_COUNT
2202 reference_count = CL_MEM_REFERENCE_COUNT
2203 PPCODE:
2204 cl_uint value [1];
2205 NEED_SUCCESS (GetMemObjectInfo, (self, ix, sizeof (value), value, 0));
2206 EXTEND (SP, 1);
2207 const int i = 0;
2208 PUSHs (sv_2mortal (newSVuv (value [i])));
2209
2210 void
2211 flags (OpenCL::Memory self)
2212 PPCODE:
2213 cl_ulong value [1];
2214 NEED_SUCCESS (GetMemObjectInfo, (self, CL_MEM_FLAGS, sizeof (value), value, 0));
2215 EXTEND (SP, 1);
2216 const int i = 0;
2217 PUSHs (sv_2mortal (newSVuv (value [i])));
2218
2219 void
2220 size (OpenCL::Memory self)
2221 ALIAS:
2222 size = CL_MEM_SIZE
2223 offset = CL_MEM_OFFSET
2224 PPCODE:
2225 size_t value [1];
2226 NEED_SUCCESS (GetMemObjectInfo, (self, ix, sizeof (value), value, 0));
2227 EXTEND (SP, 1);
2228 const int i = 0;
2229 PUSHs (sv_2mortal (newSVuv (value [i])));
2230
2231 void
2232 host_ptr (OpenCL::Memory self)
2233 PPCODE:
2234 void * value [1];
2235 NEED_SUCCESS (GetMemObjectInfo, (self, CL_MEM_HOST_PTR, sizeof (value), value, 0));
2236 EXTEND (SP, 1);
2237 const int i = 0;
2238 PUSHs (sv_2mortal (newSVuv ((IV)(intptr_t)value [i])));
2239
2240 void
2241 context (OpenCL::Memory self)
2242 PPCODE:
2243 cl_context value [1];
2244 NEED_SUCCESS (GetMemObjectInfo, (self, CL_MEM_CONTEXT, sizeof (value), value, 0));
2245 EXTEND (SP, 1);
2246 const int i = 0;
2247 NEED_SUCCESS (RetainContext, (value [i]));
2248 PUSH_CLOBJ (stash_context, value [i]);
2249
2250 void
2251 associated_memobject (OpenCL::Memory self)
2252 PPCODE:
2253 cl_mem value [1];
2254 NEED_SUCCESS (GetMemObjectInfo, (self, CL_MEM_ASSOCIATED_MEMOBJECT, sizeof (value), value, 0));
2255 EXTEND (SP, 1);
2256 const int i = 0;
2257 NEED_SUCCESS (RetainMemObject, (value [i]));
2258 PUSH_CLOBJ (stash_memory, value [i]);
2259
2260 #END:mem
2261
2262 #if cl_apple_gl_sharing || cl_khr_gl_sharing
2263
2264 void
2265 gl_object_info (OpenCL::Memory self)
2266 PPCODE:
2267 cl_gl_object_type type;
2268 cl_GLuint name;
2269 NEED_SUCCESS (GetGLObjectInfo, (self, &type, &name));
2270 EXTEND (SP, 2);
2271 PUSHs (sv_2mortal (newSVuv (type)));
2272 PUSHs (sv_2mortal (newSVuv (name)));
2273
2274 #endif
2275
2276 MODULE = OpenCL PACKAGE = OpenCL::BufferObj
2277
2278 void
2279 sub_buffer_region (OpenCL::BufferObj self, cl_mem_flags flags, size_t origin, size_t size)
2280 PPCODE:
2281 if (flags & (CL_MEM_USE_HOST_PTR | CL_MEM_COPY_HOST_PTR | CL_MEM_ALLOC_HOST_PTR))
2282 croak ("clCreateSubBuffer: cannot use/copy/alloc host ptr, doesn't make sense, check your flags!");
2283
2284 cl_buffer_region crdata = { origin, size };
2285
2286 NEED_SUCCESS_ARG (cl_mem mem, CreateSubBuffer, (self, flags, CL_BUFFER_CREATE_TYPE_REGION, &crdata, &res));
2287 XPUSH_CLOBJ (stash_buffer, mem);
2288
2289 MODULE = OpenCL PACKAGE = OpenCL::Image
2290
2291 void
2292 image_info (OpenCL::Image self, cl_image_info name)
2293 PPCODE:
2294 INFO (Image)
2295
2296 void
2297 format (OpenCL::Image self)
2298 PPCODE:
2299 cl_image_format format;
2300 NEED_SUCCESS (GetImageInfo, (self, CL_IMAGE_FORMAT, sizeof (format), &format, 0));
2301 EXTEND (SP, 2);
2302 PUSHs (sv_2mortal (newSVuv (format.image_channel_order)));
2303 PUSHs (sv_2mortal (newSVuv (format.image_channel_data_type)));
2304
2305 #BEGIN:image
2306
2307 void
2308 element_size (OpenCL::Image self)
2309 ALIAS:
2310 element_size = CL_IMAGE_ELEMENT_SIZE
2311 row_pitch = CL_IMAGE_ROW_PITCH
2312 slice_pitch = CL_IMAGE_SLICE_PITCH
2313 width = CL_IMAGE_WIDTH
2314 height = CL_IMAGE_HEIGHT
2315 depth = CL_IMAGE_DEPTH
2316 PPCODE:
2317 size_t value [1];
2318 NEED_SUCCESS (GetImageInfo, (self, ix, sizeof (value), value, 0));
2319 EXTEND (SP, 1);
2320 const int i = 0;
2321 PUSHs (sv_2mortal (newSVuv (value [i])));
2322
2323 #END:image
2324
2325 #if cl_apple_gl_sharing || cl_khr_gl_sharing
2326
2327 #BEGIN:gl_texture
2328
2329 void
2330 target (OpenCL::Image self)
2331 PPCODE:
2332 cl_GLenum value [1];
2333 NEED_SUCCESS (GetGLTextureInfo, (self, CL_GL_TEXTURE_TARGET, sizeof (value), value, 0));
2334 EXTEND (SP, 1);
2335 const int i = 0;
2336 PUSHs (sv_2mortal (newSVuv (value [i])));
2337
2338 void
2339 gl_mipmap_level (OpenCL::Image self)
2340 PPCODE:
2341 cl_GLint value [1];
2342 NEED_SUCCESS (GetGLTextureInfo, (self, CL_GL_MIPMAP_LEVEL, sizeof (value), value, 0));
2343 EXTEND (SP, 1);
2344 const int i = 0;
2345 PUSHs (sv_2mortal (newSViv (value [i])));
2346
2347 #END:gl_texture
2348
2349 #endif
2350
2351 MODULE = OpenCL PACKAGE = OpenCL::Sampler
2352
2353 void
2354 DESTROY (OpenCL::Sampler self)
2355 CODE:
2356 clReleaseSampler (self);
2357
2358 void
2359 info (OpenCL::Sampler self, cl_sampler_info name)
2360 PPCODE:
2361 INFO (Sampler)
2362
2363 #BEGIN:sampler
2364
2365 void
2366 reference_count (OpenCL::Sampler self)
2367 ALIAS:
2368 reference_count = CL_SAMPLER_REFERENCE_COUNT
2369 normalized_coords = CL_SAMPLER_NORMALIZED_COORDS
2370 addressing_mode = CL_SAMPLER_ADDRESSING_MODE
2371 PPCODE:
2372 cl_uint value [1];
2373 NEED_SUCCESS (GetSamplerInfo, (self, ix, sizeof (value), value, 0));
2374 EXTEND (SP, 1);
2375 const int i = 0;
2376 PUSHs (sv_2mortal (newSVuv (value [i])));
2377
2378 void
2379 context (OpenCL::Sampler self)
2380 PPCODE:
2381 cl_context value [1];
2382 NEED_SUCCESS (GetSamplerInfo, (self, CL_SAMPLER_CONTEXT, sizeof (value), value, 0));
2383 EXTEND (SP, 1);
2384 const int i = 0;
2385 NEED_SUCCESS (RetainContext, (value [i]));
2386 PUSH_CLOBJ (stash_context, value [i]);
2387
2388 void
2389 filter_mode (OpenCL::Sampler self)
2390 PPCODE:
2391 cl_uint value [1];
2392 NEED_SUCCESS (GetSamplerInfo, (self, CL_SAMPLER_FILTER_MODE, sizeof (value), value, 0));
2393 EXTEND (SP, 1);
2394 const int i = 0;
2395 PUSHs (sv_2mortal (value [i] ? &PL_sv_yes : &PL_sv_no));
2396
2397 #END:sampler
2398
2399 MODULE = OpenCL PACKAGE = OpenCL::Program
2400
2401 void
2402 DESTROY (OpenCL::Program self)
2403 CODE:
2404 clReleaseProgram (self);
2405
2406 void
2407 build (OpenCL::Program self, SV *devices = &PL_sv_undef, SV *options = &PL_sv_undef, SV *notify = &PL_sv_undef)
2408 ALIAS:
2409 build_async = 1
2410 CODE:
2411 cl_uint device_count = 0;
2412 cl_device_id *device_list = 0;
2413
2414 if (SvOK (devices))
2415 device_list = object_list (cv, 1, "devices", devices, "OpenCL::Device", &device_count);
2416
2417 void *user_data;
2418 program_callback pfn_notify = make_program_callback (notify, &user_data);
2419
2420 if (ix)
2421 build_program_async (self, device_count, device_list, SvPVbyte_nolen (options), user_data);
2422 else
2423 NEED_SUCCESS (BuildProgram, (self, device_count, device_list, SvPVbyte_nolen (options), pfn_notify, user_data));
2424
2425 #if CL_VERSION_1_2
2426
2427 void
2428 compile (OpenCL::Program self, SV *devices, SV *options = &PL_sv_undef, SV *headers = &PL_sv_undef, SV *notify = &PL_sv_undef)
2429 CODE:
2430 cl_uint device_count = 0;
2431 cl_device_id *device_list = 0;
2432
2433 if (SvOK (devices))
2434 device_list = object_list (cv, 1, "devices", devices, "OpenCL::Device", &device_count);
2435
2436 cl_uint header_count = 0;
2437 cl_program *header_list = 0;
2438 const char **header_name = 0;
2439
2440 if (SvOK (headers))
2441 {
2442 if (!SvROK (devices) || SvTYPE (SvRV (devices)) != SVt_PVHV)
2443 croak ("clCompileProgram: headers must be undef or a hashref of name => OpenCL::Program pairs");
2444
2445 HV *hv = (HV *)SvRV (devices);
2446
2447 header_count = hv_iterinit (hv);
2448 header_list = tmpbuf (sizeof (*header_list) * header_count);
2449 header_name = tmpbuf (sizeof (*header_name) * header_count);
2450
2451 HE *he;
2452 int i = 0;
2453 while (he = hv_iternext (hv))
2454 {
2455 header_name [i] = SvPVbyte_nolen (HeSVKEY_force (he));
2456 header_list [i] = SvCLOBJ (cv, "headers", HeVAL (he), "OpenCL::Program");
2457 ++i;
2458 }
2459 }
2460
2461 void *user_data;
2462 program_callback pfn_notify = make_program_callback (notify, &user_data);
2463
2464 NEED_SUCCESS (CompileProgram, (self, device_count, device_list, SvPVbyte_nolen (options),
2465 header_count, header_list, header_name, pfn_notify, user_data));
2466
2467 #endif
2468
2469 void
2470 build_info (OpenCL::Program self, OpenCL::Device device, cl_program_build_info name)
2471 PPCODE:
2472 size_t size;
2473 NEED_SUCCESS (GetProgramBuildInfo, (self, device, name, 0, 0, &size));
2474 SV *sv = sv_2mortal (newSV (size));
2475 SvUPGRADE (sv, SVt_PV);
2476 SvPOK_only (sv);
2477 SvCUR_set (sv, size);
2478 NEED_SUCCESS (GetProgramBuildInfo, (self, device, name, size, SvPVX (sv), 0));
2479 XPUSHs (sv);
2480
2481 #BEGIN:program_build
2482
2483 void
2484 build_status (OpenCL::Program self, OpenCL::Device device)
2485 PPCODE:
2486 cl_int value [1];
2487 NEED_SUCCESS (GetProgramBuildInfo, (self, device, CL_PROGRAM_BUILD_STATUS, sizeof (value), value, 0));
2488 EXTEND (SP, 1);
2489 const int i = 0;
2490 PUSHs (sv_2mortal (newSViv (value [i])));
2491
2492 void
2493 build_options (OpenCL::Program self, OpenCL::Device device)
2494 ALIAS:
2495 build_options = CL_PROGRAM_BUILD_OPTIONS
2496 build_log = CL_PROGRAM_BUILD_LOG
2497 PPCODE:
2498 size_t size;
2499 NEED_SUCCESS (GetProgramBuildInfo, (self, device, ix, 0, 0, &size));
2500 char *value = tmpbuf (size);
2501 NEED_SUCCESS (GetProgramBuildInfo, (self, device, ix, size, value, 0));
2502 EXTEND (SP, 1);
2503 const int i = 0;
2504 PUSHs (sv_2mortal (newSVpv (value, 0)));
2505
2506 void
2507 binary_type (OpenCL::Program self, OpenCL::Device device)
2508 PPCODE:
2509 cl_uint value [1];
2510 NEED_SUCCESS (GetProgramBuildInfo, (self, device, CL_PROGRAM_BINARY_TYPE, sizeof (value), value, 0));
2511 EXTEND (SP, 1);
2512 const int i = 0;
2513 PUSHs (sv_2mortal (newSVuv ((UV)value [i])));
2514
2515 #END:program_build
2516
2517 void
2518 kernel (OpenCL::Program program, SV *function)
2519 PPCODE:
2520 NEED_SUCCESS_ARG (cl_kernel kernel, CreateKernel, (program, SvPVbyte_nolen (function), &res));
2521 XPUSH_CLOBJ (stash_kernel, kernel);
2522
2523 void
2524 kernels_in_program (OpenCL::Program program)
2525 PPCODE:
2526 cl_uint num_kernels;
2527 NEED_SUCCESS (CreateKernelsInProgram, (program, 0, 0, &num_kernels));
2528 cl_kernel *kernels = tmpbuf (sizeof (cl_kernel) * num_kernels);
2529 NEED_SUCCESS (CreateKernelsInProgram, (program, num_kernels, kernels, 0));
2530
2531 int i;
2532 EXTEND (SP, num_kernels);
2533 for (i = 0; i < num_kernels; ++i)
2534 PUSH_CLOBJ (stash_kernel, kernels [i]);
2535
2536 void
2537 info (OpenCL::Program self, cl_program_info name)
2538 PPCODE:
2539 INFO (Program)
2540
2541 void
2542 binaries (OpenCL::Program self)
2543 PPCODE:
2544 cl_uint n, i;
2545 size_t size;
2546
2547 NEED_SUCCESS (GetProgramInfo, (self, CL_PROGRAM_NUM_DEVICES , sizeof (n) , &n , 0));
2548 if (!n) XSRETURN_EMPTY;
2549
2550 size_t *sizes = tmpbuf (sizeof (*sizes) * n);
2551 NEED_SUCCESS (GetProgramInfo, (self, CL_PROGRAM_BINARY_SIZES, sizeof (*sizes) * n, sizes, &size));
2552 if (size != sizeof (*sizes) * n) XSRETURN_EMPTY;
2553 unsigned char **ptrs = tmpbuf (sizeof (*ptrs) * n);
2554
2555 EXTEND (SP, n);
2556 for (i = 0; i < n; ++i)
2557 {
2558 SV *sv = sv_2mortal (newSV (sizes [i]));
2559 SvUPGRADE (sv, SVt_PV);
2560 SvPOK_only (sv);
2561 SvCUR_set (sv, sizes [i]);
2562 ptrs [i] = (void *)SvPVX (sv);
2563 PUSHs (sv);
2564 }
2565
2566 NEED_SUCCESS (GetProgramInfo, (self, CL_PROGRAM_BINARIES , sizeof (*ptrs ) * n, ptrs , &size));
2567 if (size != sizeof (*ptrs) * n) XSRETURN_EMPTY;
2568
2569 #BEGIN:program
2570
2571 void
2572 reference_count (OpenCL::Program self)
2573 ALIAS:
2574 reference_count = CL_PROGRAM_REFERENCE_COUNT
2575 num_devices = CL_PROGRAM_NUM_DEVICES
2576 PPCODE:
2577 cl_uint value [1];
2578 NEED_SUCCESS (GetProgramInfo, (self, ix, sizeof (value), value, 0));
2579 EXTEND (SP, 1);
2580 const int i = 0;
2581 PUSHs (sv_2mortal (newSVuv (value [i])));
2582
2583 void
2584 context (OpenCL::Program self)
2585 PPCODE:
2586 cl_context value [1];
2587 NEED_SUCCESS (GetProgramInfo, (self, CL_PROGRAM_CONTEXT, sizeof (value), value, 0));
2588 EXTEND (SP, 1);
2589 const int i = 0;
2590 NEED_SUCCESS (RetainContext, (value [i]));
2591 PUSH_CLOBJ (stash_context, value [i]);
2592
2593 void
2594 devices (OpenCL::Program self)
2595 PPCODE:
2596 size_t size;
2597 NEED_SUCCESS (GetProgramInfo, (self, CL_PROGRAM_DEVICES, 0, 0, &size));
2598 cl_device_id *value = tmpbuf (size);
2599 NEED_SUCCESS (GetProgramInfo, (self, CL_PROGRAM_DEVICES, size, value, 0));
2600 int i, n = size / sizeof (*value);
2601 EXTEND (SP, n);
2602 for (i = 0; i < n; ++i)
2603 PUSH_CLOBJ (stash_device, value [i]);
2604
2605 void
2606 source (OpenCL::Program self)
2607 PPCODE:
2608 size_t size;
2609 NEED_SUCCESS (GetProgramInfo, (self, CL_PROGRAM_SOURCE, 0, 0, &size));
2610 char *value = tmpbuf (size);
2611 NEED_SUCCESS (GetProgramInfo, (self, CL_PROGRAM_SOURCE, size, value, 0));
2612 EXTEND (SP, 1);
2613 const int i = 0;
2614 PUSHs (sv_2mortal (newSVpv (value, 0)));
2615
2616 void
2617 binary_sizes (OpenCL::Program self)
2618 PPCODE:
2619 size_t size;
2620 NEED_SUCCESS (GetProgramInfo, (self, CL_PROGRAM_BINARY_SIZES, 0, 0, &size));
2621 size_t *value = tmpbuf (size);
2622 NEED_SUCCESS (GetProgramInfo, (self, CL_PROGRAM_BINARY_SIZES, size, value, 0));
2623 int i, n = size / sizeof (*value);
2624 EXTEND (SP, n);
2625 for (i = 0; i < n; ++i)
2626 PUSHs (sv_2mortal (newSVuv (value [i])));
2627
2628 #END:program
2629
2630 MODULE = OpenCL PACKAGE = OpenCL::Kernel
2631
2632 void
2633 DESTROY (OpenCL::Kernel self)
2634 CODE:
2635 clReleaseKernel (self);
2636
2637 void
2638 setf (OpenCL::Kernel self, const char *format, ...)
2639 CODE:
2640 int i;
2641 for (i = 2; ; ++i)
2642 {
2643 while (*format == ' ')
2644 ++format;
2645
2646 char type = *format++;
2647
2648 if (!type)
2649 break;
2650
2651 if (i >= items)
2652 croak ("OpenCL::Kernel::setf format string too long (not enough arguments)");
2653
2654 SV *sv = ST (i);
2655
2656 union
2657 {
2658 cl_char cc; cl_uchar cC; cl_short cs; cl_ushort cS;
2659 cl_int ci; cl_uint cI; cl_long cl; cl_ulong cL;
2660 cl_half ch; cl_float cf; cl_double cd;
2661 cl_mem cm;
2662 cl_sampler ca;
2663 size_t cz;
2664 cl_event ce;
2665 } arg;
2666 size_t size;
2667 int nullarg = 0;
2668
2669 switch (type)
2670 {
2671 case 'c': arg.cc = SvIV (sv); size = sizeof (arg.cc); break;
2672 case 'C': arg.cC = SvUV (sv); size = sizeof (arg.cC); break;
2673 case 's': arg.cs = SvIV (sv); size = sizeof (arg.cs); break;
2674 case 'S': arg.cS = SvUV (sv); size = sizeof (arg.cS); break;
2675 case 'i': arg.ci = SvIV (sv); size = sizeof (arg.ci); break;
2676 case 'I': arg.cI = SvUV (sv); size = sizeof (arg.cI); break;
2677 case 'l': arg.cl = SvIV (sv); size = sizeof (arg.cl); break;
2678 case 'L': arg.cL = SvUV (sv); size = sizeof (arg.cL); break;
2679
2680 case 'h': arg.ch = SvUV (sv); size = sizeof (arg.ch); break;
2681 case 'f': arg.cf = SvNV (sv); size = sizeof (arg.cf); break;
2682 case 'd': arg.cd = SvNV (sv); size = sizeof (arg.cd); break;
2683
2684 case 'z': nullarg = 1; size = SvIV (sv); break;
2685
2686 case 'm': nullarg = !SvOK (sv); arg.cm = SvCLOBJ (cv, "m", sv, "OpenCL::Memory" ); size = sizeof (arg.cm); break;
2687 case 'a': nullarg = !SvOK (sv); arg.ca = SvCLOBJ (cv, "a", sv, "OpenCL::Sampler"); size = sizeof (arg.ca); break;
2688 case 'e': nullarg = !SvOK (sv); arg.ca = SvCLOBJ (cv, "e", sv, "OpenCL::Event" ); size = sizeof (arg.ce); break;
2689
2690 default:
2691 croak ("OpenCL::Kernel::setf format character '%c' not supported", type);
2692 }
2693
2694 res = clSetKernelArg (self, i - 2, size, nullarg ? 0 : &arg);
2695 if (res)
2696 croak ("OpenCL::Kernel::setf kernel parameter '%c' (#%d): %s", type, i - 2, err2str (res));
2697 }
2698
2699 if (i != items)
2700 croak ("OpenCL::Kernel::setf format string too short (too many arguments)");
2701
2702 void
2703 set_char (OpenCL::Kernel self, cl_uint idx, cl_char value)
2704 CODE:
2705 clSetKernelArg (self, idx, sizeof (value), &value);
2706
2707 void
2708 set_uchar (OpenCL::Kernel self, cl_uint idx, cl_uchar value)
2709 CODE:
2710 clSetKernelArg (self, idx, sizeof (value), &value);
2711
2712 void
2713 set_short (OpenCL::Kernel self, cl_uint idx, cl_short value)
2714 CODE:
2715 clSetKernelArg (self, idx, sizeof (value), &value);
2716
2717 void
2718 set_ushort (OpenCL::Kernel self, cl_uint idx, cl_ushort value)
2719 CODE:
2720 clSetKernelArg (self, idx, sizeof (value), &value);
2721
2722 void
2723 set_int (OpenCL::Kernel self, cl_uint idx, cl_int value)
2724 CODE:
2725 clSetKernelArg (self, idx, sizeof (value), &value);
2726
2727 void
2728 set_uint (OpenCL::Kernel self, cl_uint idx, cl_uint value)
2729 CODE:
2730 clSetKernelArg (self, idx, sizeof (value), &value);
2731
2732 void
2733 set_long (OpenCL::Kernel self, cl_uint idx, cl_long value)
2734 CODE:
2735 clSetKernelArg (self, idx, sizeof (value), &value);
2736
2737 void
2738 set_ulong (OpenCL::Kernel self, cl_uint idx, cl_ulong value)
2739 CODE:
2740 clSetKernelArg (self, idx, sizeof (value), &value);
2741
2742 void
2743 set_half (OpenCL::Kernel self, cl_uint idx, cl_half value)
2744 CODE:
2745 clSetKernelArg (self, idx, sizeof (value), &value);
2746
2747 void
2748 set_float (OpenCL::Kernel self, cl_uint idx, cl_float value)
2749 CODE:
2750 clSetKernelArg (self, idx, sizeof (value), &value);
2751
2752 void
2753 set_double (OpenCL::Kernel self, cl_uint idx, cl_double value)
2754 CODE:
2755 clSetKernelArg (self, idx, sizeof (value), &value);
2756
2757 void
2758 set_memory (OpenCL::Kernel self, cl_uint idx, OpenCL::Memory_ornull value)
2759 CODE:
2760 clSetKernelArg (self, idx, sizeof (value), value ? &value : 0);
2761
2762 void
2763 set_buffer (OpenCL::Kernel self, cl_uint idx, OpenCL::Buffer_ornull value)
2764 CODE:
2765 clSetKernelArg (self, idx, sizeof (value), value ? &value : 0);
2766
2767 void
2768 set_image (OpenCL::Kernel self, cl_uint idx, OpenCL::Image_ornull value)
2769 CODE:
2770 clSetKernelArg (self, idx, sizeof (value), value ? &value : 0);
2771
2772 void
2773 set_sampler (OpenCL::Kernel self, cl_uint idx, OpenCL::Sampler value)
2774 CODE:
2775 clSetKernelArg (self, idx, sizeof (value), &value);
2776
2777 void
2778 set_local (OpenCL::Kernel self, cl_uint idx, size_t size)
2779 CODE:
2780 clSetKernelArg (self, idx, size, 0);
2781
2782 void
2783 set_event (OpenCL::Kernel self, cl_uint idx, OpenCL::Event value)
2784 CODE:
2785 clSetKernelArg (self, idx, sizeof (value), &value);
2786
2787 void
2788 info (OpenCL::Kernel self, cl_kernel_info name)
2789 PPCODE:
2790 INFO (Kernel)
2791
2792 #BEGIN:kernel
2793
2794 void
2795 function_name (OpenCL::Kernel self)
2796 PPCODE:
2797 size_t size;
2798 NEED_SUCCESS (GetKernelInfo, (self, CL_KERNEL_FUNCTION_NAME, 0, 0, &size));
2799 char *value = tmpbuf (size);
2800 NEED_SUCCESS (GetKernelInfo, (self, CL_KERNEL_FUNCTION_NAME, size, value, 0));
2801 EXTEND (SP, 1);
2802 const int i = 0;
2803 PUSHs (sv_2mortal (newSVpv (value, 0)));
2804
2805 void
2806 num_args (OpenCL::Kernel self)
2807 ALIAS:
2808 num_args = CL_KERNEL_NUM_ARGS
2809 reference_count = CL_KERNEL_REFERENCE_COUNT
2810 PPCODE:
2811 cl_uint value [1];
2812 NEED_SUCCESS (GetKernelInfo, (self, ix, sizeof (value), value, 0));
2813 EXTEND (SP, 1);
2814 const int i = 0;
2815 PUSHs (sv_2mortal (newSVuv (value [i])));
2816
2817 void
2818 context (OpenCL::Kernel self)
2819 PPCODE:
2820 cl_context value [1];
2821 NEED_SUCCESS (GetKernelInfo, (self, CL_KERNEL_CONTEXT, sizeof (value), value, 0));
2822 EXTEND (SP, 1);
2823 const int i = 0;
2824 NEED_SUCCESS (RetainContext, (value [i]));
2825 PUSH_CLOBJ (stash_context, value [i]);
2826
2827 void
2828 program (OpenCL::Kernel self)
2829 PPCODE:
2830 cl_program value [1];
2831 NEED_SUCCESS (GetKernelInfo, (self, CL_KERNEL_PROGRAM, sizeof (value), value, 0));
2832 EXTEND (SP, 1);
2833 const int i = 0;
2834 NEED_SUCCESS (RetainProgram, (value [i]));
2835 PUSH_CLOBJ (stash_program, value [i]);
2836
2837 #END:kernel
2838
2839 void
2840 work_group_info (OpenCL::Kernel self, OpenCL::Device device, cl_kernel_work_group_info name)
2841 PPCODE:
2842 size_t size;
2843 NEED_SUCCESS (GetKernelWorkGroupInfo, (self, device, name, 0, 0, &size));
2844 SV *sv = sv_2mortal (newSV (size));
2845 SvUPGRADE (sv, SVt_PV);
2846 SvPOK_only (sv);
2847 SvCUR_set (sv, size);
2848 NEED_SUCCESS (GetKernelWorkGroupInfo, (self, device, name, size, SvPVX (sv), 0));
2849 XPUSHs (sv);
2850
2851 #BEGIN:kernel_work_group
2852
2853 void
2854 work_group_size (OpenCL::Kernel self, OpenCL::Device device)
2855 ALIAS:
2856 work_group_size = CL_KERNEL_WORK_GROUP_SIZE
2857 preferred_work_group_size_multiple = CL_KERNEL_PREFERRED_WORK_GROUP_SIZE_MULTIPLE
2858 PPCODE:
2859 size_t value [1];
2860 NEED_SUCCESS (GetKernelWorkGroupInfo, (self, device, ix, sizeof (value), value, 0));
2861 EXTEND (SP, 1);
2862 const int i = 0;
2863 PUSHs (sv_2mortal (newSVuv (value [i])));
2864
2865 void
2866 compile_work_group_size (OpenCL::Kernel self, OpenCL::Device device)
2867 PPCODE:
2868 size_t size;
2869 NEED_SUCCESS (GetKernelWorkGroupInfo, (self, device, CL_KERNEL_COMPILE_WORK_GROUP_SIZE, 0, 0, &size));
2870 size_t *value = tmpbuf (size);
2871 NEED_SUCCESS (GetKernelWorkGroupInfo, (self, device, CL_KERNEL_COMPILE_WORK_GROUP_SIZE, size, value, 0));
2872 int i, n = size / sizeof (*value);
2873 EXTEND (SP, n);
2874 for (i = 0; i < n; ++i)
2875 PUSHs (sv_2mortal (newSVuv (value [i])));
2876
2877 void
2878 local_mem_size (OpenCL::Kernel self, OpenCL::Device device)
2879 ALIAS:
2880 local_mem_size = CL_KERNEL_LOCAL_MEM_SIZE
2881 private_mem_size = CL_KERNEL_PRIVATE_MEM_SIZE
2882 PPCODE:
2883 cl_ulong value [1];
2884 NEED_SUCCESS (GetKernelWorkGroupInfo, (self, device, ix, sizeof (value), value, 0));
2885 EXTEND (SP, 1);
2886 const int i = 0;
2887 PUSHs (sv_2mortal (newSVuv (value [i])));
2888
2889 #END:kernel_work_group
2890
2891 #if CL_VERSION_1_2
2892
2893 void
2894 arg_info (OpenCL::Kernel self, cl_uint idx, cl_kernel_arg_info name)
2895 PPCODE:
2896 size_t size;
2897 NEED_SUCCESS (GetKernelArgInfo, (self, idx, name, 0, 0, &size));
2898 SV *sv = sv_2mortal (newSV (size));
2899 SvUPGRADE (sv, SVt_PV);
2900 SvPOK_only (sv);
2901 SvCUR_set (sv, size);
2902 NEED_SUCCESS (GetKernelArgInfo, (self, idx, name, size, SvPVX (sv), 0));
2903 XPUSHs (sv);
2904
2905 #BEGIN:kernel_arg
2906
2907 void
2908 arg_address_qualifier (OpenCL::Kernel self, cl_uint idx)
2909 ALIAS:
2910 arg_address_qualifier = CL_KERNEL_ARG_ADDRESS_QUALIFIER
2911 arg_access_qualifier = CL_KERNEL_ARG_ACCESS_QUALIFIER
2912 PPCODE:
2913 cl_uint value [1];
2914 NEED_SUCCESS (GetKernelArgInfo, (self, idx, ix, sizeof (value), value, 0));
2915 EXTEND (SP, 1);
2916 const int i = 0;
2917 PUSHs (sv_2mortal (newSVuv (value [i])));
2918
2919 void
2920 arg_type_name (OpenCL::Kernel self, cl_uint idx)
2921 ALIAS:
2922 arg_type_name = CL_KERNEL_ARG_TYPE_NAME
2923 arg_name = CL_KERNEL_ARG_NAME
2924 PPCODE:
2925 size_t size;
2926 NEED_SUCCESS (GetKernelArgInfo, (self, idx, ix, 0, 0, &size));
2927 char *value = tmpbuf (size);
2928 NEED_SUCCESS (GetKernelArgInfo, (self, idx, ix, size, value, 0));
2929 EXTEND (SP, 1);
2930 const int i = 0;
2931 PUSHs (sv_2mortal (newSVpv (value, 0)));
2932
2933 void
2934 arg_type_qualifier (OpenCL::Kernel self, cl_uint idx)
2935 PPCODE:
2936 cl_ulong value [1];
2937 NEED_SUCCESS (GetKernelArgInfo, (self, idx, CL_KERNEL_ARG_TYPE_QUALIFIER, sizeof (value), value, 0));
2938 EXTEND (SP, 1);
2939 const int i = 0;
2940 PUSHs (sv_2mortal (newSVuv (value [i])));
2941
2942 #END:kernel_arg
2943
2944 #endif
2945
2946 MODULE = OpenCL PACKAGE = OpenCL::Event
2947
2948 void
2949 DESTROY (OpenCL::Event self)
2950 CODE:
2951 clReleaseEvent (self);
2952
2953 void
2954 wait (OpenCL::Event self)
2955 CODE:
2956 clWaitForEvents (1, &self);
2957
2958 void
2959 cb (OpenCL::Event self, cl_int command_exec_callback_type, SV *cb)
2960 CODE:
2961 clSetEventCallback (self, command_exec_callback_type, eq_event_notify, SvREFCNT_inc (s_get_cv (cb)));
2962
2963 void
2964 info (OpenCL::Event self, cl_event_info name)
2965 PPCODE:
2966 INFO (Event)
2967
2968 #BEGIN:event
2969
2970 void
2971 command_queue (OpenCL::Event self)
2972 PPCODE:
2973 cl_command_queue value [1];
2974 NEED_SUCCESS (GetEventInfo, (self, CL_EVENT_COMMAND_QUEUE, sizeof (value), value, 0));
2975 EXTEND (SP, 1);
2976 const int i = 0;
2977 NEED_SUCCESS (RetainCommandQueue, (value [i]));
2978 PUSH_CLOBJ (stash_queue, value [i]);
2979
2980 void
2981 command_type (OpenCL::Event self)
2982 ALIAS:
2983 command_type = CL_EVENT_COMMAND_TYPE
2984 reference_count = CL_EVENT_REFERENCE_COUNT
2985 command_execution_status = CL_EVENT_COMMAND_EXECUTION_STATUS
2986 PPCODE:
2987 cl_uint value [1];
2988 NEED_SUCCESS (GetEventInfo, (self, ix, sizeof (value), value, 0));
2989 EXTEND (SP, 1);
2990 const int i = 0;
2991 PUSHs (sv_2mortal (newSVuv (value [i])));
2992
2993 void
2994 context (OpenCL::Event self)
2995 PPCODE:
2996 cl_context value [1];
2997 NEED_SUCCESS (GetEventInfo, (self, CL_EVENT_CONTEXT, sizeof (value), value, 0));
2998 EXTEND (SP, 1);
2999 const int i = 0;
3000 NEED_SUCCESS (RetainContext, (value [i]));
3001 PUSH_CLOBJ (stash_context, value [i]);
3002
3003 #END:event
3004
3005 void
3006 profiling_info (OpenCL::Event self, cl_profiling_info name)
3007 PPCODE:
3008 INFO (EventProfiling)
3009
3010 #BEGIN:profiling
3011
3012 void
3013 profiling_command_queued (OpenCL::Event self)
3014 ALIAS:
3015 profiling_command_queued = CL_PROFILING_COMMAND_QUEUED
3016 profiling_command_submit = CL_PROFILING_COMMAND_SUBMIT
3017 profiling_command_start = CL_PROFILING_COMMAND_START
3018 profiling_command_end = CL_PROFILING_COMMAND_END
3019 PPCODE:
3020 cl_ulong value [1];
3021 NEED_SUCCESS (GetEventProfilingInfo, (self, ix, sizeof (value), value, 0));
3022 EXTEND (SP, 1);
3023 const int i = 0;
3024 PUSHs (sv_2mortal (newSVuv (value [i])));
3025
3026 #END:profiling
3027
3028 MODULE = OpenCL PACKAGE = OpenCL::UserEvent
3029
3030 void
3031 set_status (OpenCL::UserEvent self, cl_int execution_status)
3032 CODE:
3033 clSetUserEventStatus (self, execution_status);
3034
3035 MODULE = OpenCL PACKAGE = OpenCL::Mapped
3036
3037 void
3038 DESTROY (SV *self)
3039 CODE:
3040 OpenCL__Mapped mapped = SvMAPPED (self);
3041
3042 clEnqueueUnmapMemObject (mapped->queue, mapped->memobj, mapped->ptr, 1, &mapped->event, 0);
3043 mapped_detach (self, mapped);
3044
3045 clReleaseCommandQueue (mapped->queue);
3046 clReleaseEvent (mapped->event);
3047 Safefree (mapped);
3048
3049 void
3050 unmap (OpenCL::Mapped self, ...)
3051 CODE:
3052 mapped_unmap (cv, ST (0), self, self->queue, &ST (1), items - 1);
3053
3054 bool
3055 mapped (OpenCL::Mapped self)
3056 CODE:
3057 RETVAL = !!self->ptr;
3058 OUTPUT:
3059 RETVAL
3060
3061 void
3062 wait (OpenCL::Mapped self)
3063 PPCODE:
3064 if (self->event)
3065 NEED_SUCCESS (WaitForEvents, (1, &self->event));
3066
3067 void
3068 event (OpenCL::Mapped self)
3069 PPCODE:
3070 if (!self->event)
3071 XSRETURN_UNDEF;
3072
3073 clRetainEvent (self->event);
3074 XPUSH_CLOBJ (stash_event, self->event);
3075
3076 #define MAPPED_OFFSET_CB offsetof (struct mapped, cb)
3077 #define MAPPED_OFFSET_ROW_PITCH offsetof (struct mapped, row_pitch)
3078 #define MAPPED_OFFSET_SLICE_PITCH offsetof (struct mapped, slice_pitch)
3079 #define MAPPED_OFFSET_WIDTH offsetof (struct mapped, width)
3080 #define MAPPED_OFFSET_HEIGHT offsetof (struct mapped, height)
3081 #define MAPPED_OFFSET_DEPTH offsetof (struct mapped, depth)
3082
3083 IV
3084 size (OpenCL::Mapped self)
3085 ALIAS:
3086 size = MAPPED_OFFSET_CB
3087 row_pitch = MAPPED_OFFSET_ROW_PITCH
3088 slice_pitch = MAPPED_OFFSET_SLICE_PITCH
3089 width = MAPPED_OFFSET_WIDTH
3090 height = MAPPED_OFFSET_HEIGHT
3091 depth = MAPPED_OFFSET_DEPTH
3092 CODE:
3093 RETVAL = *(size_t *)((char *)self + ix);
3094 OUTPUT:
3095 RETVAL
3096
3097 IV
3098 ptr (OpenCL::Mapped self)
3099 CODE:
3100 RETVAL = PTR2IV (self->ptr);
3101 OUTPUT:
3102 RETVAL
3103
3104 void
3105 set (OpenCL::Mapped self, size_t offset, SV *data)
3106 CODE:
3107 STRLEN len;
3108 const char *ptr = SvPVbyte (data, len);
3109
3110 if (offset + len > self->cb)
3111 croak ("OpenCL::Mapped::set out of bound condition detected");
3112
3113 memcpy (offset + (char *)self->ptr, ptr, len);
3114
3115 void
3116 get_row (OpenCL::Mapped self, size_t count, size_t x = 0, size_t y = 0, size_t z = 0)
3117 PPCODE:
3118 if (!SvOK (ST (1)))
3119 count = self->width - x;
3120
3121 if (x + count > self->width)
3122 croak ("OpenCL::Mapped::get: x + count crosses a row boundary");
3123
3124 if (y >= self->height)
3125 croak ("OpenCL::Mapped::get: y coordinate out of bounds");
3126
3127 if (z >= self->depth)
3128 croak ("OpenCL::Mapped::get: z coordinate out of bounds");
3129
3130 size_t element = mapped_element_size (self);
3131
3132 count *= element;
3133 x *= element;
3134
3135 char *ptr = (char *)self->ptr + x + y * self->row_pitch + z * self->slice_pitch;
3136 XPUSHs (sv_2mortal (newSVpvn (ptr, count)));
3137
3138 void
3139 set_row (OpenCL::Mapped self, SV *data, size_t x = 0, size_t y = 0, size_t z = 0)
3140 PPCODE:
3141 STRLEN count;
3142 char *dataptr = SvPVbyte (data, count);
3143 size_t element = mapped_element_size (self);
3144
3145 x *= element;
3146
3147 if (x + count > self->width * element)
3148 croak ("OpenCL::Mapped::set: x + data size crosses a row boundary");
3149
3150 if (y >= self->height)
3151 croak ("OpenCL::Mapped::set: y coordinate out of bounds");
3152
3153 if (z >= self->depth)
3154 croak ("OpenCL::Mapped::set: z coordinate out of bounds");
3155
3156 char *ptr = (char *)self->ptr + x + y * self->row_pitch + z * self->slice_pitch;
3157 memcpy (ptr, dataptr, count);
3158
3159 MODULE = OpenCL PACKAGE = OpenCL::MappedBuffer
3160
3161 MODULE = OpenCL PACKAGE = OpenCL::MappedImage
3162
3163 IV
3164 element_size (OpenCL::Mapped self)
3165 CODE:
3166 RETVAL = mapped_element_size (self);
3167 OUTPUT:
3168 RETVAL
3169