ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/OpenCL/OpenCL.xs
Revision: 1.51
Committed: Tue Apr 24 22:45:38 2012 UTC (12 years ago) by root
Branch: MAIN
Changes since 1.50: +381 -76 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.1 #include "EXTERN.h"
2     #include "perl.h"
3     #include "XSUB.h"
4    
5 root 1.51 #define X_STACKSIZE sizeof (void *) * 512 * 1024 // 2-4mb should be enough, really
6     #include "xthread.h"
7     #include "schmorp.h"
8    
9 root 1.30 #ifdef I_DLFCN
10     #include <dlfcn.h>
11     #endif
12    
13 root 1.37 // how stupid is that, the 1.2 header files define CL_VERSION_1_1,
14     // but then fail to define the api functions unless you ALSO define
15     // this. This breaks 100% of the opencl 1.1 apps, for what reason?
16     // after all, the functions are deprecated, not removed.
17     // in addition, you cannot test for this in any future-proof way.
18     // each time a new opencl version comes out, you need to make a new
19     // release.
20     #define CL_USE_DEPRECATED_OPENCL_1_2_APIS /* just guessing, you stupid idiots */
21    
22 root 1.45 #ifndef PREFER_1_1
23 root 1.44 #define PREFER_1_1 1
24     #endif
25    
26     #if PREFER_1_1
27     #define CL_USE_DEPRECATED_OPENCL_1_1_APIS
28     #endif
29    
30 root 1.19 #ifdef __APPLE__
31     #include <OpenCL/opencl.h>
32     #else
33     #include <CL/opencl.h>
34     #endif
35 root 1.1
36 root 1.44 #ifndef CL_VERSION_1_2
37     #undef PREFER_1_1
38     #define PREFER_1_1 1
39 root 1.38 #endif
40    
41 root 1.16 typedef cl_platform_id OpenCL__Platform;
42     typedef cl_device_id OpenCL__Device;
43     typedef cl_context OpenCL__Context;
44     typedef cl_command_queue OpenCL__Queue;
45     typedef cl_mem OpenCL__Memory;
46     typedef cl_mem OpenCL__Buffer;
47 root 1.18 typedef cl_mem OpenCL__BufferObj;
48 root 1.16 typedef cl_mem OpenCL__Image;
49     typedef cl_mem OpenCL__Image2D;
50     typedef cl_mem OpenCL__Image3D;
51     typedef cl_mem OpenCL__Memory_ornull;
52     typedef cl_mem OpenCL__Buffer_ornull;
53     typedef cl_mem OpenCL__Image_ornull;
54     typedef cl_mem OpenCL__Image2D_ornull;
55     typedef cl_mem OpenCL__Image3D_ornull;
56     typedef cl_sampler OpenCL__Sampler;
57     typedef cl_program OpenCL__Program;
58     typedef cl_kernel OpenCL__Kernel;
59     typedef cl_event OpenCL__Event;
60     typedef cl_event OpenCL__UserEvent;
61 root 1.5
62 root 1.7 typedef SV *FUTURE;
63    
64 root 1.5 /*****************************************************************************/
65    
66 root 1.30 // name must include a leading underscore
67 root 1.32 // all of this horrors would be unneceesary if somebody wrote a proper OpenGL module
68     // for perl. doh.
69 root 1.30 static void *
70 root 1.32 glsym (const char *name)
71 root 1.30 {
72 root 1.32 void *fun = 0;
73    
74 root 1.30 #if defined I_DLFCN && defined RTLD_DEFAULT
75 root 1.32 fun = dlsym (RTLD_DEFAULT, name + 1);
76     if (!fun) fun = dlsym (RTLD_DEFAULT, name);
77    
78     if (!fun)
79     {
80     static void *libgl;
81     static const char *glso[] = {
82     "libGL.so.1",
83     "libGL.so.3",
84     "libGL.so.4.0",
85     "libGL.so",
86     "/usr/lib/libGL.so",
87     "/usr/X11R6/lib/libGL.1.dylib"
88     };
89     int i;
90    
91     for (i = 0; !libgl && i < sizeof (glso) / sizeof (glso [0]); ++i)
92     {
93     libgl = dlopen (glso [i], RTLD_LAZY);
94     if (libgl)
95     break;
96     }
97    
98     if (libgl)
99     {
100     fun = dlsym (libgl, name + 1);
101     if (!fun) fun = dlsym (libgl, name);
102     }
103     }
104 root 1.30 #endif
105 root 1.32
106     return fun;
107 root 1.30 }
108    
109     /*****************************************************************************/
110    
111 root 1.5 /* up to two temporary buffers */
112     static void *
113     tmpbuf (size_t size)
114     {
115 root 1.24 enum { buffers = 3 };
116 root 1.5 static int idx;
117 root 1.24 static void *buf [buffers];
118     static size_t len [buffers];
119 root 1.5
120 root 1.37 idx = (idx + 1) % buffers;
121 root 1.5
122     if (len [idx] < size)
123     {
124     free (buf [idx]);
125     len [idx] = ((size + 31) & ~4095) + 4096 - 32;
126     buf [idx] = malloc (len [idx]);
127     }
128    
129     return buf [idx];
130     }
131    
132     /*****************************************************************************/
133 root 1.1
134 root 1.3 typedef struct
135     {
136 root 1.1 IV iv;
137     const char *name;
138 root 1.3 #define const_iv(name) { (IV)CL_ ## name, # name },
139     } ivstr;
140 root 1.1
141     static const char *
142 root 1.3 iv2str (IV value, const ivstr *base, int count, const char *fallback)
143 root 1.1 {
144     int i;
145 root 1.3 static char strbuf [32];
146    
147     for (i = count; i--; )
148     if (base [i].iv == value)
149     return base [i].name;
150    
151     snprintf (strbuf, sizeof (strbuf), fallback, (int)value);
152    
153     return strbuf;
154     }
155    
156     static const char *
157     enum2str (cl_uint value)
158     {
159     static const ivstr enumstr[] = {
160     #include "enumstr.h"
161     };
162 root 1.1
163 root 1.3 return iv2str (value, enumstr, sizeof (enumstr) / sizeof (enumstr [0]), "ENUM(0x%04x)");
164     }
165 root 1.1
166 root 1.3 static const char *
167     err2str (cl_int err)
168     {
169     static const ivstr errstr[] = {
170     #include "errstr.h"
171     };
172 root 1.1
173 root 1.3 return iv2str (err, errstr, sizeof (errstr) / sizeof (errstr [0]), "ERROR(%d)");
174 root 1.1 }
175    
176 root 1.5 /*****************************************************************************/
177    
178 root 1.8 static cl_int res;
179 root 1.5
180 root 1.8 #define FAIL(name) \
181     croak ("cl" # name ": %s", err2str (res));
182 root 1.1
183     #define NEED_SUCCESS(name,args) \
184     do { \
185 root 1.8 res = cl ## name args; \
186 root 1.1 \
187     if (res) \
188 root 1.8 FAIL (name); \
189 root 1.1 } while (0)
190    
191 root 1.8 #define NEED_SUCCESS_ARG(retdecl, name, args) \
192     retdecl = cl ## name args; \
193     if (res) \
194     FAIL (name);
195    
196 root 1.5 /*****************************************************************************/
197    
198 root 1.24 static cl_context_properties *
199     SvCONTEXTPROPERTIES (const char *func, const char *svname, SV *sv, cl_context_properties *extra, int extracount)
200     {
201     if (!sv || !SvOK (sv))
202     if (extra)
203     sv = sv_2mortal (newRV_noinc ((SV *)newAV ())); // slow, but rarely used hopefully
204     else
205     return 0;
206    
207     if (SvROK (sv) && SvTYPE (SvRV (sv)) == SVt_PVAV)
208     {
209     AV *av = (AV *)SvRV (sv);
210 root 1.28 int i, len = av_len (av) + 1;
211 root 1.24 cl_context_properties *p = tmpbuf (sizeof (cl_context_properties) * (len + extracount + 1));
212     cl_context_properties *l = p;
213    
214     if (len & 1)
215     croak ("%s: %s is not a property list (must be even number of elements)", func, svname);
216    
217     while (extracount--)
218     *l++ = *extra++;
219    
220 root 1.29 for (i = 0; i < len; i += 2)
221 root 1.24 {
222 root 1.29 cl_context_properties t = SvIV (*av_fetch (av, i , 0));
223     SV *p_sv = *av_fetch (av, i + 1, 0);
224 root 1.32 cl_context_properties v = SvIV (p_sv); // code below can override
225 root 1.24
226     switch (t)
227     {
228 root 1.32 case CL_GLX_DISPLAY_KHR:
229     if (!SvOK (p_sv))
230     {
231     void *func = glsym ("_glXGetCurrentDisplay");
232     if (func)
233     v = (cl_context_properties)((void *(*)(void))func)();
234     }
235     break;
236    
237     case CL_GL_CONTEXT_KHR:
238     if (!SvOK (p_sv))
239     {
240     void *func = glsym ("_glXGetCurrentContext");
241     if (func)
242     v = (cl_context_properties)((void *(*)(void))func)();
243     }
244     break;
245    
246 root 1.24 default:
247     /* unknown property, treat as int */
248     break;
249     }
250    
251     *l++ = t;
252     *l++ = v;
253     }
254    
255     *l = 0;
256    
257     return p;
258     }
259    
260     croak ("%s: %s is not a property list (either undef or [type => value, ...])", func, svname);
261     }
262    
263     /*****************************************************************************/
264    
265 root 1.51 #define NEW_CLOBJ(class,ptr) sv_setref_pv (sv_newmortal (), class, ptr)
266     #define PUSH_CLOBJ(class,ptr) PUSHs (NEW_CLOBJ (class, ptr))
267     #define XPUSH_CLOBJ(class,ptr) XPUSHs (NEW_CLOBJ (class, ptr))
268    
269     /* cl objects are either \$iv, or [$iv, ...] */
270     /* they can be upgraded at runtime to the array form */
271     static void *
272     SvCLOBJ (const char *func, const char *svname, SV *sv, const char *pkg)
273     {
274     if (SvROK (sv) && sv_derived_from (sv, pkg))
275     {
276     SV *rv = SvRV (sv);
277    
278     if (SvTYPE (rv) == SVt_PVAV)
279     rv = AvARRAY (rv)[0];
280    
281     return (void *)SvIV (SvRV (sv));
282     }
283    
284     croak ("%s: %s is not of type %s", func, svname, pkg);
285     }
286    
287     static void
288     CLOBJ_push (SV *self, SV *data)
289     {
290     SV *rv = SvRV (self);
291    
292     if (SvTYPE (rv) != SVt_PVAV)
293     {
294     AV *av = newAV ();
295     av_push (av, rv);
296     rv = (SV *)av;
297     SvRV_set (self, rv);
298     }
299    
300     av_push ((AV *)rv, data);
301     }
302    
303     static SV *
304     sv_struct (STRLEN size)
305     {
306     SV *sv = newSV (size);
307     SvPOK_only (sv);
308     return sv;
309     }
310    
311     static void *
312     CLOBJ_push_struct (SV *self, STRLEN size)
313     {
314     SV *sv = sv_struct (size);
315     CLOBJ_push (self, sv);
316     return SvPVX (sv);
317     }
318    
319     /*****************************************************************************/
320     /* callback stuff */
321    
322     /* default context callback, log to stderr */
323     static void CL_CALLBACK
324     context_default_notify (const char *msg, const void *info, size_t cb, void *data)
325     {
326     fprintf (stderr, "OpenCL Context Notify: %s\n", msg);
327     }
328    
329     typedef struct
330     {
331     int free_cb;
332     void (*push)(void *data1, void *data2, void *data3);
333     } eq_vtbl;
334    
335     typedef struct eq_item
336     {
337     struct eq_item *next;
338     eq_vtbl *vtbl;
339     SV *cb;
340     void *data1, *data2, *data3;
341     } eq_item;
342    
343     static void (*eq_signal_func)(void *signal_arg, int value);
344     static void *eq_signal_arg;
345     static xmutex_t eq_lock = X_MUTEX_INIT;
346     static eq_item *eq_head, *eq_tail;
347    
348     static void
349     eq_enq (eq_vtbl *vtbl, SV *cb, void *data1, void *data2, void *data3)
350     {
351     eq_item *item = malloc (sizeof (eq_item));
352    
353     printf ("enq(%p,%p,%p,%p,%p)\n", vtbl, cb, data1, data2, data3);//D
354    
355     item->next = 0;
356     item->vtbl = vtbl;
357     item->cb = cb;
358     item->data1 = data1;
359     item->data2 = data2;
360     item->data3 = data3;
361    
362     X_LOCK (eq_lock);
363    
364     *(eq_head ? &eq_tail->next : &eq_head) = item;
365     eq_tail = item;
366    
367     X_UNLOCK (eq_lock);
368    
369     eq_signal_func (eq_signal_arg, 0);
370     }
371    
372     static eq_item *
373     eq_dec (void)
374     {
375     eq_item *res;
376    
377     X_LOCK (eq_lock);
378    
379     res = eq_head;
380     if (res)
381     eq_head = res->next;
382    
383     X_UNLOCK (eq_lock);
384    
385     return res;
386     }
387    
388     #if 0
389     static void
390     mem_free (pTHX_ void *p)
391     {
392     free (p);
393     }
394     //SAVEDESTRUCTOR_X (mem_free, item);
395     #endif
396    
397     static void
398     eq_poll (void)
399     {
400     eq_item *item;
401    
402     while ((item = eq_dec ()))
403     {
404     ENTER;
405     SAVETMPS;
406    
407     dSP;
408     PUSHMARK (SP);
409     EXTEND (SP, 2);
410    
411     if (item->vtbl->free_cb)
412     sv_2mortal (item->cb);
413    
414     PUTBACK;
415     item->vtbl->push (item->data1, item->data2, item->data3);
416    
417     SV *cb = item->cb;
418     free (item);
419    
420     call_sv (cb, G_DISCARD | G_VOID);
421    
422     FREETMPS;
423     LEAVE;
424     }
425     }
426    
427     static void
428     eq_poll_interrupt (pTHX_ void *c_arg, int value)
429     {
430     eq_poll ();
431     }
432    
433     /* context notify */
434    
435     static void
436     eq_context_push (void *data1, void *data2, void *data3)
437     {
438     dSP;
439     PUSHs (sv_2mortal (newSVpv (data1, 0)));
440     PUSHs (sv_2mortal (newSVpvn (data2, (STRLEN)data3)));
441     PUTBACK;
442     }
443    
444     static eq_vtbl eq_context_vtbl = { 0, eq_context_push };
445    
446     /* build/compile/link notify */
447    
448     static void
449     eq_program_push (void *data1, void *data2, void *data3)
450     {
451     dSP;
452     PUSH_CLOBJ ("OpenCL::Program", data1);
453     PUTBACK;
454     }
455    
456     static eq_vtbl eq_program_vtbl = { 1, eq_program_push };
457    
458     static void CL_CALLBACK
459     eq_program_notify (cl_program program, void *user_data)
460     {
461     eq_enq (&eq_program_vtbl, user_data, (void *)program, 0, 0);
462     }
463    
464     struct build_args
465     {
466     cl_program program;
467     char *options;
468     void *user_data;
469     cl_uint num_devices;
470     };
471    
472     X_THREAD_PROC (build_program_thread)
473     {
474     struct build_args *arg = thr_arg;
475    
476     clBuildProgram (arg->program, arg->num_devices, arg->num_devices ? (void *)(arg + 1) : 0, arg->options, 0, 0);
477    
478     if (arg->user_data)
479     eq_program_notify (arg->program, arg->user_data);
480     else
481     clReleaseProgram (arg->program);
482    
483     free (arg->options);
484     free (arg);
485     }
486    
487     static void
488     build_program_async (cl_program program, cl_uint num_devices, const cl_device_id *device_list, const char *options, void *user_data)
489     {
490     struct build_args *arg = malloc (sizeof (struct build_args) + sizeof (*device_list) * num_devices);
491    
492     arg->program = program;
493     arg->options = strdup (options);
494     arg->user_data = user_data;
495     arg->num_devices = num_devices;
496     memcpy (arg + 1, device_list, sizeof (*device_list) * num_devices);
497    
498     xthread_t id;
499     thread_create (&id, build_program_thread, arg);
500     }
501    
502     /* event objects */
503    
504     static void
505     eq_event_push (void *data1, void *data2, void *data3)
506     {
507     dSP;
508     PUSH_CLOBJ ("OpenCL::Event", data1);
509     PUSHs (sv_2mortal (newSViv ((IV)data2)));
510     PUTBACK;
511     }
512    
513     static eq_vtbl eq_event_vtbl = { 1, eq_event_push };
514    
515     static void CL_CALLBACK
516     eq_event_notify (cl_event event, cl_int event_command_exec_status, void *user_data)
517     {
518     clRetainEvent (event);
519     eq_enq (&eq_event_vtbl, user_data, (void *)event, (void *)event_command_exec_status, 0);
520     }
521    
522     /*****************************************************************************/
523    
524 root 1.11 static size_t
525     img_row_pitch (cl_mem img)
526     {
527     size_t res;
528     clGetImageInfo (img, CL_IMAGE_ROW_PITCH, sizeof (res), &res, 0);
529     return res;
530     }
531    
532 root 1.5 static cl_event *
533 root 1.35 event_list (SV **items, cl_uint *rcount)
534 root 1.5 {
535 root 1.35 cl_uint count = *rcount;
536    
537 root 1.20 if (!count)
538     return 0;
539    
540 root 1.5 cl_event *list = tmpbuf (sizeof (cl_event) * count);
541 root 1.35 int i = 0;
542    
543     do
544     {
545     --count;
546     if (SvOK (items [count]))
547 root 1.51 list [i++] = SvCLOBJ ("clEnqueue", "wait_events", items [count], "OpenCL::Event");
548 root 1.35 }
549     while (count);
550 root 1.5
551 root 1.35 *rcount = i;
552 root 1.5
553 root 1.35 return i ? list : 0;
554 root 1.5 }
555    
556     #define EVENT_LIST(items,count) \
557     cl_uint event_list_count = (count); \
558 root 1.35 cl_event *event_list_ptr = event_list (&ST (items), &event_list_count)
559 root 1.2
560     #define INFO(class) \
561     { \
562 root 1.23 size_t size; \
563     NEED_SUCCESS (Get ## class ## Info, (self, name, 0, 0, &size)); \
564 root 1.10 SV *sv = sv_2mortal (newSV (size)); \
565 root 1.2 SvUPGRADE (sv, SVt_PV); \
566     SvPOK_only (sv); \
567     SvCUR_set (sv, size); \
568 root 1.23 NEED_SUCCESS (Get ## class ## Info, (self, name, size, SvPVX (sv), 0)); \
569 root 1.2 XPUSHs (sv); \
570     }
571    
572 root 1.1 MODULE = OpenCL PACKAGE = OpenCL
573    
574 root 1.2 PROTOTYPES: ENABLE
575    
576 root 1.51 void
577     poll ()
578     CODE:
579     eq_poll ();
580    
581     void
582     _eq_initialise (IV func, IV arg)
583     CODE:
584     eq_signal_func = (void (*)(void *, int))func;
585     eq_signal_arg = (void*)arg;
586    
587 root 1.1 BOOT:
588     {
589 root 1.24 HV *stash = gv_stashpv ("OpenCL", 1);
590     static const ivstr *civ, const_iv[] = {
591     { sizeof (cl_char ), "SIZEOF_CHAR" },
592     { sizeof (cl_uchar ), "SIZEOF_UCHAR" },
593     { sizeof (cl_short ), "SIZEOF_SHORT" },
594     { sizeof (cl_ushort), "SIZEOF_USHORT" },
595     { sizeof (cl_int ), "SIZEOF_INT" },
596     { sizeof (cl_uint ), "SIZEOF_UINT" },
597     { sizeof (cl_long ), "SIZEOF_LONG" },
598     { sizeof (cl_ulong ), "SIZEOF_ULONG" },
599     { sizeof (cl_half ), "SIZEOF_HALF" },
600     { sizeof (cl_float ), "SIZEOF_FLOAT" },
601     { sizeof (cl_double), "SIZEOF_DOUBLE" },
602 root 1.1 #include "constiv.h"
603 root 1.24 };
604 root 1.51
605 root 1.24 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ > const_iv; civ--)
606     newCONSTSUB (stash, (char *)civ[-1].name, newSViv (civ[-1].iv));
607 root 1.51
608     sv_setiv (perl_get_sv ("OpenCL::POLL_FUNC", TRUE), (IV)eq_poll_interrupt);
609 root 1.1 }
610    
611 root 1.5 cl_int
612     errno ()
613     CODE:
614 root 1.37 RETVAL = res;
615     OUTPUT:
616     RETVAL
617 root 1.5
618 root 1.3 const char *
619     err2str (cl_int err)
620    
621     const char *
622     enum2str (cl_uint value)
623    
624 root 1.1 void
625     platforms ()
626     PPCODE:
627     cl_platform_id *list;
628     cl_uint count;
629     int i;
630    
631 root 1.2 NEED_SUCCESS (GetPlatformIDs, (0, 0, &count));
632 root 1.4 list = tmpbuf (sizeof (*list) * count);
633 root 1.2 NEED_SUCCESS (GetPlatformIDs, (count, list, 0));
634 root 1.1
635     EXTEND (SP, count);
636     for (i = 0; i < count; ++i)
637 root 1.51 PUSH_CLOBJ ("OpenCL::Platform", list [i]);
638 root 1.1
639     void
640 root 1.24 context_from_type (cl_context_properties *properties = 0, cl_device_type type = CL_DEVICE_TYPE_DEFAULT, FUTURE notify = 0)
641 root 1.1 PPCODE:
642 root 1.24 NEED_SUCCESS_ARG (cl_context ctx, CreateContextFromType, (properties, type, 0, 0, &res));
643 root 1.51 XPUSH_CLOBJ ("OpenCL::Context", ctx);
644 root 1.1
645 root 1.37 #if 0
646    
647 root 1.8 void
648 root 1.24 context (cl_context_properties *properties = 0, FUTURE devices, FUTURE notify = 0)
649 root 1.8 PPCODE:
650     /* der Gipfel der Kunst */
651 root 1.1
652 root 1.37 #endif
653    
654 root 1.2 void
655     wait_for_events (...)
656     CODE:
657     EVENT_LIST (0, items);
658     NEED_SUCCESS (WaitForEvents, (event_list_count, event_list_ptr));
659    
660     PROTOTYPES: DISABLE
661    
662 root 1.1 MODULE = OpenCL PACKAGE = OpenCL::Platform
663    
664     void
665 root 1.22 info (OpenCL::Platform self, cl_platform_info name)
666 root 1.1 PPCODE:
667 root 1.2 INFO (Platform)
668 root 1.1
669 root 1.47 void
670     unload_compiler (OpenCL::Platform self)
671     CODE:
672     #if CL_VERSION_1_2
673     clUnloadPlatformCompiler (self);
674     #endif
675    
676 root 1.13 #BEGIN:platform
677    
678     void
679 root 1.22 profile (OpenCL::Platform self)
680 root 1.16 ALIAS:
681     profile = CL_PLATFORM_PROFILE
682     version = CL_PLATFORM_VERSION
683     name = CL_PLATFORM_NAME
684     vendor = CL_PLATFORM_VENDOR
685     extensions = CL_PLATFORM_EXTENSIONS
686 root 1.14 PPCODE:
687     size_t size;
688 root 1.22 NEED_SUCCESS (GetPlatformInfo, (self, ix, 0, 0, &size));
689 root 1.14 char *value = tmpbuf (size);
690 root 1.22 NEED_SUCCESS (GetPlatformInfo, (self, ix, size, value, 0));
691 root 1.16 EXTEND (SP, 1);
692     const int i = 0;
693 root 1.14 PUSHs (sv_2mortal (newSVpv (value, 0)));
694 root 1.13
695     #END:platform
696    
697 root 1.1 void
698 root 1.22 devices (OpenCL::Platform self, cl_device_type type = CL_DEVICE_TYPE_ALL)
699 root 1.1 PPCODE:
700     cl_device_id *list;
701     cl_uint count;
702     int i;
703    
704 root 1.22 NEED_SUCCESS (GetDeviceIDs, (self, type, 0, 0, &count));
705 root 1.4 list = tmpbuf (sizeof (*list) * count);
706 root 1.22 NEED_SUCCESS (GetDeviceIDs, (self, type, count, list, 0));
707 root 1.1
708     EXTEND (SP, count);
709     for (i = 0; i < count; ++i)
710     PUSHs (sv_setref_pv (sv_newmortal (), "OpenCL::Device", list [i]));
711    
712     void
713 root 1.51 context (OpenCL::Platform self, cl_context_properties *properties, SV *devices, SV *notify = 0)
714 root 1.8 PPCODE:
715     if (!SvROK (devices) || SvTYPE (SvRV (devices)) != SVt_PVAV)
716 root 1.27 croak ("OpenCL::Platform::context argument 'device' must be an arrayref with device objects, in call");
717 root 1.8
718 root 1.11 AV *av = (AV *)SvRV (devices);
719 root 1.8 cl_uint num_devices = av_len (av) + 1;
720     cl_device_id *device_list = tmpbuf (sizeof (cl_device_id) * num_devices);
721 root 1.51
722 root 1.8 int i;
723     for (i = num_devices; i--; )
724 root 1.51 device_list [i] = SvCLOBJ ("clCreateContext", "devices", *av_fetch (av, i, 0), "OpenCL::Device");
725 root 1.8
726 root 1.51 void (CL_CALLBACK *pfn_notify)(const char *, const void *, size_t, void *) = context_default_notify;
727     void *user_data = 0;
728    
729     NEED_SUCCESS_ARG (cl_context ctx, CreateContext, (properties, num_devices, device_list, pfn_notify, user_data, &res));
730     XPUSH_CLOBJ ("OpenCL::Context", ctx);
731 root 1.8
732     void
733 root 1.24 context_from_type (OpenCL::Platform self, SV *properties = 0, cl_device_type type = CL_DEVICE_TYPE_DEFAULT, FUTURE notify = 0)
734 root 1.1 PPCODE:
735 root 1.24 cl_context_properties extra[] = { CL_CONTEXT_PLATFORM, (cl_context_properties)self };
736     cl_context_properties *props = SvCONTEXTPROPERTIES ("OpenCL::Platform::context_from_type", "properties", properties, extra, 2);
737 root 1.8 NEED_SUCCESS_ARG (cl_context ctx, CreateContextFromType, (props, type, 0, 0, &res));
738 root 1.51 XPUSH_CLOBJ ("OpenCL::Context", ctx);
739 root 1.1
740 root 1.16 MODULE = OpenCL PACKAGE = OpenCL::Device
741 root 1.14
742     void
743 root 1.22 info (OpenCL::Device self, cl_device_info name)
744 root 1.16 PPCODE:
745     INFO (Device)
746 root 1.14
747 root 1.16 #BEGIN:device
748 root 1.14
749     void
750 root 1.22 type (OpenCL::Device self)
751 root 1.14 PPCODE:
752 root 1.16 cl_device_type value [1];
753 root 1.22 NEED_SUCCESS (GetDeviceInfo, (self, CL_DEVICE_TYPE, sizeof (value), value, 0));
754 root 1.16 EXTEND (SP, 1);
755     const int i = 0;
756     PUSHs (sv_2mortal (newSViv (value [i])));
757 root 1.14
758     void
759 root 1.22 vendor_id (OpenCL::Device self)
760 root 1.16 ALIAS:
761     vendor_id = CL_DEVICE_VENDOR_ID
762     max_compute_units = CL_DEVICE_MAX_COMPUTE_UNITS
763     max_work_item_dimensions = CL_DEVICE_MAX_WORK_ITEM_DIMENSIONS
764     preferred_vector_width_char = CL_DEVICE_PREFERRED_VECTOR_WIDTH_CHAR
765     preferred_vector_width_short = CL_DEVICE_PREFERRED_VECTOR_WIDTH_SHORT
766     preferred_vector_width_int = CL_DEVICE_PREFERRED_VECTOR_WIDTH_INT
767     preferred_vector_width_long = CL_DEVICE_PREFERRED_VECTOR_WIDTH_LONG
768     preferred_vector_width_float = CL_DEVICE_PREFERRED_VECTOR_WIDTH_FLOAT
769     preferred_vector_width_double = CL_DEVICE_PREFERRED_VECTOR_WIDTH_DOUBLE
770     max_clock_frequency = CL_DEVICE_MAX_CLOCK_FREQUENCY
771     max_read_image_args = CL_DEVICE_MAX_READ_IMAGE_ARGS
772     max_write_image_args = CL_DEVICE_MAX_WRITE_IMAGE_ARGS
773     image_support = CL_DEVICE_IMAGE_SUPPORT
774     max_samplers = CL_DEVICE_MAX_SAMPLERS
775     mem_base_addr_align = CL_DEVICE_MEM_BASE_ADDR_ALIGN
776     min_data_type_align_size = CL_DEVICE_MIN_DATA_TYPE_ALIGN_SIZE
777     global_mem_cacheline_size = CL_DEVICE_GLOBAL_MEM_CACHELINE_SIZE
778     max_constant_args = CL_DEVICE_MAX_CONSTANT_ARGS
779     preferred_vector_width_half = CL_DEVICE_PREFERRED_VECTOR_WIDTH_HALF
780     native_vector_width_char = CL_DEVICE_NATIVE_VECTOR_WIDTH_CHAR
781     native_vector_width_short = CL_DEVICE_NATIVE_VECTOR_WIDTH_SHORT
782     native_vector_width_int = CL_DEVICE_NATIVE_VECTOR_WIDTH_INT
783     native_vector_width_long = CL_DEVICE_NATIVE_VECTOR_WIDTH_LONG
784     native_vector_width_float = CL_DEVICE_NATIVE_VECTOR_WIDTH_FLOAT
785     native_vector_width_double = CL_DEVICE_NATIVE_VECTOR_WIDTH_DOUBLE
786     native_vector_width_half = CL_DEVICE_NATIVE_VECTOR_WIDTH_HALF
787 root 1.41 reference_count_ext = CL_DEVICE_REFERENCE_COUNT_EXT
788 root 1.14 PPCODE:
789     cl_uint value [1];
790 root 1.22 NEED_SUCCESS (GetDeviceInfo, (self, ix, sizeof (value), value, 0));
791 root 1.14 EXTEND (SP, 1);
792     const int i = 0;
793     PUSHs (sv_2mortal (newSVuv (value [i])));
794    
795     void
796 root 1.22 max_work_group_size (OpenCL::Device self)
797 root 1.16 ALIAS:
798     max_work_group_size = CL_DEVICE_MAX_WORK_GROUP_SIZE
799     image2d_max_width = CL_DEVICE_IMAGE2D_MAX_WIDTH
800     image2d_max_height = CL_DEVICE_IMAGE2D_MAX_HEIGHT
801     image3d_max_width = CL_DEVICE_IMAGE3D_MAX_WIDTH
802     image3d_max_height = CL_DEVICE_IMAGE3D_MAX_HEIGHT
803     image3d_max_depth = CL_DEVICE_IMAGE3D_MAX_DEPTH
804     max_parameter_size = CL_DEVICE_MAX_PARAMETER_SIZE
805     profiling_timer_resolution = CL_DEVICE_PROFILING_TIMER_RESOLUTION
806 root 1.14 PPCODE:
807 root 1.16 size_t value [1];
808 root 1.22 NEED_SUCCESS (GetDeviceInfo, (self, ix, sizeof (value), value, 0));
809 root 1.14 EXTEND (SP, 1);
810     const int i = 0;
811     PUSHs (sv_2mortal (newSVuv (value [i])));
812    
813     void
814 root 1.22 max_work_item_sizes (OpenCL::Device self)
815 root 1.14 PPCODE:
816 root 1.16 size_t size;
817 root 1.22 NEED_SUCCESS (GetDeviceInfo, (self, CL_DEVICE_MAX_WORK_ITEM_SIZES, 0, 0, &size));
818 root 1.16 size_t *value = tmpbuf (size);
819 root 1.22 NEED_SUCCESS (GetDeviceInfo, (self, CL_DEVICE_MAX_WORK_ITEM_SIZES, size, value, 0));
820 root 1.16 int i, n = size / sizeof (*value);
821     EXTEND (SP, n);
822     for (i = 0; i < n; ++i)
823 root 1.14 PUSHs (sv_2mortal (newSVuv (value [i])));
824    
825     void
826 root 1.22 address_bits (OpenCL::Device self)
827 root 1.14 PPCODE:
828 root 1.16 cl_bitfield value [1];
829 root 1.22 NEED_SUCCESS (GetDeviceInfo, (self, CL_DEVICE_ADDRESS_BITS, sizeof (value), value, 0));
830 root 1.14 EXTEND (SP, 1);
831     const int i = 0;
832     PUSHs (sv_2mortal (newSVuv (value [i])));
833    
834     void
835 root 1.22 max_mem_alloc_size (OpenCL::Device self)
836 root 1.16 ALIAS:
837     max_mem_alloc_size = CL_DEVICE_MAX_MEM_ALLOC_SIZE
838     global_mem_cache_size = CL_DEVICE_GLOBAL_MEM_CACHE_SIZE
839     global_mem_size = CL_DEVICE_GLOBAL_MEM_SIZE
840     max_constant_buffer_size = CL_DEVICE_MAX_CONSTANT_BUFFER_SIZE
841     local_mem_size = CL_DEVICE_LOCAL_MEM_SIZE
842 root 1.14 PPCODE:
843 root 1.16 cl_ulong value [1];
844 root 1.22 NEED_SUCCESS (GetDeviceInfo, (self, ix, sizeof (value), value, 0));
845 root 1.14 EXTEND (SP, 1);
846     const int i = 0;
847     PUSHs (sv_2mortal (newSVuv (value [i])));
848    
849     void
850 root 1.22 single_fp_config (OpenCL::Device self)
851 root 1.16 ALIAS:
852     single_fp_config = CL_DEVICE_SINGLE_FP_CONFIG
853     double_fp_config = CL_DEVICE_DOUBLE_FP_CONFIG
854     half_fp_config = CL_DEVICE_HALF_FP_CONFIG
855 root 1.14 PPCODE:
856 root 1.16 cl_device_fp_config value [1];
857 root 1.22 NEED_SUCCESS (GetDeviceInfo, (self, ix, sizeof (value), value, 0));
858 root 1.14 EXTEND (SP, 1);
859     const int i = 0;
860     PUSHs (sv_2mortal (newSVuv (value [i])));
861    
862     void
863 root 1.22 global_mem_cache_type (OpenCL::Device self)
864 root 1.14 PPCODE:
865 root 1.16 cl_device_mem_cache_type value [1];
866 root 1.22 NEED_SUCCESS (GetDeviceInfo, (self, CL_DEVICE_GLOBAL_MEM_CACHE_TYPE, sizeof (value), value, 0));
867 root 1.14 EXTEND (SP, 1);
868     const int i = 0;
869     PUSHs (sv_2mortal (newSVuv (value [i])));
870    
871     void
872 root 1.22 local_mem_type (OpenCL::Device self)
873 root 1.14 PPCODE:
874 root 1.16 cl_device_local_mem_type value [1];
875 root 1.22 NEED_SUCCESS (GetDeviceInfo, (self, CL_DEVICE_LOCAL_MEM_TYPE, sizeof (value), value, 0));
876 root 1.14 EXTEND (SP, 1);
877     const int i = 0;
878     PUSHs (sv_2mortal (newSVuv (value [i])));
879    
880     void
881 root 1.22 error_correction_support (OpenCL::Device self)
882 root 1.16 ALIAS:
883     error_correction_support = CL_DEVICE_ERROR_CORRECTION_SUPPORT
884     endian_little = CL_DEVICE_ENDIAN_LITTLE
885     available = CL_DEVICE_AVAILABLE
886     compiler_available = CL_DEVICE_COMPILER_AVAILABLE
887     host_unified_memory = CL_DEVICE_HOST_UNIFIED_MEMORY
888 root 1.14 PPCODE:
889 root 1.16 cl_bool value [1];
890 root 1.22 NEED_SUCCESS (GetDeviceInfo, (self, ix, sizeof (value), value, 0));
891 root 1.14 EXTEND (SP, 1);
892     const int i = 0;
893 root 1.16 PUSHs (sv_2mortal (value [i] ? &PL_sv_yes : &PL_sv_no));
894 root 1.14
895     void
896 root 1.22 execution_capabilities (OpenCL::Device self)
897 root 1.14 PPCODE:
898 root 1.16 cl_device_exec_capabilities value [1];
899 root 1.22 NEED_SUCCESS (GetDeviceInfo, (self, CL_DEVICE_EXECUTION_CAPABILITIES, sizeof (value), value, 0));
900 root 1.14 EXTEND (SP, 1);
901     const int i = 0;
902     PUSHs (sv_2mortal (newSVuv (value [i])));
903    
904     void
905 root 1.22 properties (OpenCL::Device self)
906 root 1.14 PPCODE:
907 root 1.16 cl_command_queue_properties value [1];
908 root 1.22 NEED_SUCCESS (GetDeviceInfo, (self, CL_DEVICE_QUEUE_PROPERTIES, sizeof (value), value, 0));
909 root 1.14 EXTEND (SP, 1);
910     const int i = 0;
911 root 1.16 PUSHs (sv_2mortal (newSViv (value [i])));
912 root 1.14
913     void
914 root 1.22 platform (OpenCL::Device self)
915 root 1.14 PPCODE:
916 root 1.16 cl_platform_id value [1];
917 root 1.22 NEED_SUCCESS (GetDeviceInfo, (self, CL_DEVICE_PLATFORM, sizeof (value), value, 0));
918 root 1.14 EXTEND (SP, 1);
919     const int i = 0;
920     {
921 root 1.51 PUSH_CLOBJ ("OpenCL::Platform", value [i]);
922 root 1.14 }
923    
924     void
925 root 1.22 name (OpenCL::Device self)
926 root 1.16 ALIAS:
927     name = CL_DEVICE_NAME
928     vendor = CL_DEVICE_VENDOR
929     driver_version = CL_DRIVER_VERSION
930     profile = CL_DEVICE_PROFILE
931     version = CL_DEVICE_VERSION
932     extensions = CL_DEVICE_EXTENSIONS
933 root 1.14 PPCODE:
934     size_t size;
935 root 1.22 NEED_SUCCESS (GetDeviceInfo, (self, ix, 0, 0, &size));
936 root 1.16 char *value = tmpbuf (size);
937 root 1.22 NEED_SUCCESS (GetDeviceInfo, (self, ix, size, value, 0));
938 root 1.16 EXTEND (SP, 1);
939     const int i = 0;
940     PUSHs (sv_2mortal (newSVpv (value, 0)));
941 root 1.14
942     void
943 root 1.22 parent_device_ext (OpenCL::Device self)
944 root 1.14 PPCODE:
945 root 1.16 cl_device_id value [1];
946 root 1.22 NEED_SUCCESS (GetDeviceInfo, (self, CL_DEVICE_PARENT_DEVICE_EXT, sizeof (value), value, 0));
947 root 1.14 EXTEND (SP, 1);
948     const int i = 0;
949 root 1.16 {
950 root 1.51 PUSH_CLOBJ ("OpenCL::Device", value [i]);
951 root 1.16 }
952 root 1.14
953     void
954 root 1.22 partition_types_ext (OpenCL::Device self)
955 root 1.16 ALIAS:
956     partition_types_ext = CL_DEVICE_PARTITION_TYPES_EXT
957     affinity_domains_ext = CL_DEVICE_AFFINITY_DOMAINS_EXT
958     partition_style_ext = CL_DEVICE_PARTITION_STYLE_EXT
959 root 1.14 PPCODE:
960     size_t size;
961 root 1.22 NEED_SUCCESS (GetDeviceInfo, (self, ix, 0, 0, &size));
962 root 1.14 cl_device_partition_property_ext *value = tmpbuf (size);
963 root 1.22 NEED_SUCCESS (GetDeviceInfo, (self, ix, size, value, 0));
964 root 1.15 int i, n = size / sizeof (*value);
965 root 1.14 EXTEND (SP, n);
966     for (i = 0; i < n; ++i)
967     PUSHs (sv_2mortal (newSVuv (value [i])));
968    
969     #END:device
970    
971 root 1.1 MODULE = OpenCL PACKAGE = OpenCL::Context
972    
973     void
974     DESTROY (OpenCL::Context context)
975     CODE:
976     clReleaseContext (context);
977    
978     void
979 root 1.22 info (OpenCL::Context self, cl_context_info name)
980 root 1.1 PPCODE:
981 root 1.2 INFO (Context)
982    
983     void
984 root 1.22 queue (OpenCL::Context self, OpenCL::Device device, cl_command_queue_properties properties = 0)
985 root 1.2 PPCODE:
986 root 1.23 NEED_SUCCESS_ARG (cl_command_queue queue, CreateCommandQueue, (self, device, properties, &res));
987 root 1.51 XPUSH_CLOBJ ("OpenCL::Queue", queue);
988 root 1.2
989     void
990 root 1.22 user_event (OpenCL::Context self)
991 root 1.5 PPCODE:
992 root 1.23 NEED_SUCCESS_ARG (cl_event ev, CreateUserEvent, (self, &res));
993 root 1.51 XPUSH_CLOBJ ("OpenCL::UserEvent", ev);
994 root 1.5
995     void
996 root 1.22 buffer (OpenCL::Context self, cl_mem_flags flags, size_t len)
997 root 1.2 PPCODE:
998 root 1.3 if (flags & (CL_MEM_USE_HOST_PTR | CL_MEM_COPY_HOST_PTR))
999 root 1.27 croak ("OpenCL::Context::buffer: cannot use/copy host ptr when no data is given, use $context->buffer_sv instead?");
1000 root 1.3
1001 root 1.22 NEED_SUCCESS_ARG (cl_mem mem, CreateBuffer, (self, flags, len, 0, &res));
1002 root 1.51 XPUSH_CLOBJ ("OpenCL::BufferObj", mem);
1003 root 1.2
1004     void
1005 root 1.22 buffer_sv (OpenCL::Context self, cl_mem_flags flags, SV *data)
1006 root 1.2 PPCODE:
1007     STRLEN len;
1008 root 1.21 char *ptr = SvOK (data) ? SvPVbyte (data, len) : 0;
1009 root 1.3 if (!(flags & (CL_MEM_USE_HOST_PTR | CL_MEM_COPY_HOST_PTR)))
1010 root 1.27 croak ("OpenCL::Context::buffer_sv: you have to specify use or copy host ptr when buffer data is given, use $context->buffer instead?");
1011 root 1.22 NEED_SUCCESS_ARG (cl_mem mem, CreateBuffer, (self, flags, len, ptr, &res));
1012 root 1.51 XPUSH_CLOBJ ("OpenCL::BufferObj", mem);
1013 root 1.3
1014 root 1.42 #if CL_VERSION_1_2
1015    
1016     void
1017     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, 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)
1018     PPCODE:
1019     STRLEN len;
1020     char *ptr = SvOK (data) ? SvPVbyte (data, len) : 0;
1021     const cl_image_format format = { channel_order, channel_type };
1022     const cl_image_desc desc = {
1023     type,
1024     width, height, depth,
1025     array_size, row_pitch, slice_pitch,
1026     num_mip_level, num_samples,
1027 root 1.51 type == CL_MEM_OBJECT_IMAGE1D_BUFFER ? (cl_mem)SvCLOBJ ("OpenCL::Context::Image", "data", data, "OpenCL::Buffer") : 0
1028 root 1.42 };
1029     NEED_SUCCESS_ARG (cl_mem mem, CreateImage, (self, flags, &format, &desc, ptr, &res));
1030     char *klass = "OpenCL::Image";
1031     switch (type)
1032     {
1033     case CL_MEM_OBJECT_IMAGE1D_BUFFER: klass = "OpenCL::Image1DBuffer"; break;
1034     case CL_MEM_OBJECT_IMAGE1D: klass = "OpenCL::Image1D"; break;
1035     case CL_MEM_OBJECT_IMAGE1D_ARRAY: klass = "OpenCL::Image2DArray"; break;
1036     case CL_MEM_OBJECT_IMAGE2D: klass = "OpenCL::Image2D"; break;
1037     case CL_MEM_OBJECT_IMAGE2D_ARRAY: klass = "OpenCL::Image2DArray"; break;
1038     case CL_MEM_OBJECT_IMAGE3D: klass = "OpenCL::Image3D"; break;
1039     }
1040 root 1.51 XPUSH_CLOBJ (klass, mem);
1041 root 1.42
1042     #endif
1043    
1044 root 1.3 void
1045 root 1.22 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)
1046 root 1.3 PPCODE:
1047     STRLEN len;
1048 root 1.21 char *ptr = SvOK (data) ? SvPVbyte (data, len) : 0;
1049 root 1.3 const cl_image_format format = { channel_order, channel_type };
1050 root 1.44 #if PREFER_1_1
1051     NEED_SUCCESS_ARG (cl_mem mem, CreateImage2D, (self, flags, &format, width, height, row_pitch, ptr, &res));
1052     #else
1053 root 1.42 const cl_image_desc desc = { CL_MEM_OBJECT_IMAGE2D, width, height, 0, 0, row_pitch, 0, 0, 0, 0 };
1054     NEED_SUCCESS_ARG (cl_mem mem, CreateImage, (self, flags, &format, &desc, ptr, &res));
1055     #endif
1056 root 1.51 XPUSH_CLOBJ ("OpenCL::Image2D", mem);
1057 root 1.3
1058     void
1059 root 1.22 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)
1060 root 1.3 PPCODE:
1061     STRLEN len;
1062 root 1.21 char *ptr = SvOK (data) ? SvPVbyte (data, len) : 0;
1063 root 1.3 const cl_image_format format = { channel_order, channel_type };
1064 root 1.44 #if PREFER_1_1
1065     NEED_SUCCESS_ARG (cl_mem mem, CreateImage3D, (self, flags, &format, width, height, depth, row_pitch, slice_pitch, ptr, &res));
1066     #else
1067 root 1.42 const cl_image_desc desc = { CL_MEM_OBJECT_IMAGE3D, width, height, depth, 0, row_pitch, slice_pitch, 0, 0, 0 };
1068     NEED_SUCCESS_ARG (cl_mem mem, CreateImage, (self, flags, &format, &desc, ptr, &res));
1069     #endif
1070 root 1.51 XPUSH_CLOBJ ("OpenCL::Image3D", mem);
1071 root 1.3
1072 root 1.25 #if cl_apple_gl_sharing || cl_khr_gl_sharing
1073    
1074     void
1075     gl_buffer (OpenCL::Context self, cl_mem_flags flags, cl_GLuint bufobj)
1076     PPCODE:
1077     NEED_SUCCESS_ARG (cl_mem mem, CreateFromGLBuffer, (self, flags, bufobj, &res));
1078 root 1.51 XPUSH_CLOBJ ("OpenCL::BufferObj", mem);
1079 root 1.25
1080     void
1081 root 1.40 gl_renderbuffer (OpenCL::Context self, cl_mem_flags flags, cl_GLuint renderbuffer)
1082 root 1.25 PPCODE:
1083 root 1.40 NEED_SUCCESS_ARG (cl_mem mem, CreateFromGLRenderbuffer, (self, flags, renderbuffer, &res));
1084 root 1.51 XPUSH_CLOBJ ("OpenCL::Image2D", mem);
1085 root 1.25
1086 root 1.39 #if CL_VERSION_1_2
1087    
1088     void
1089     gl_texture (OpenCL::Context self, cl_mem_flags flags, cl_GLenum target, cl_GLint miplevel, cl_GLuint texture)
1090 root 1.43 ALIAS:
1091 root 1.39 PPCODE:
1092 root 1.43 NEED_SUCCESS_ARG (cl_mem mem, CreateFromGLTexture, (self, flags, target, miplevel, texture, &res));
1093     cl_gl_object_type type;
1094     NEED_SUCCESS (GetGLObjectInfo, (mem, &type, 0)); // TODO: use target instead?
1095 root 1.39 char *klass = "OpenCL::Memory";
1096 root 1.42 switch (type)
1097 root 1.39 {
1098     case CL_GL_OBJECT_TEXTURE_BUFFER: klass = "OpenCL::Image1DBuffer"; break;
1099     case CL_GL_OBJECT_TEXTURE1D: klass = "OpenCL::Image1D"; break;
1100     case CL_GL_OBJECT_TEXTURE1D_ARRAY: klass = "OpenCL::Image2DArray"; break;
1101     case CL_GL_OBJECT_TEXTURE2D: klass = "OpenCL::Image2D"; break;
1102     case CL_GL_OBJECT_TEXTURE2D_ARRAY: klass = "OpenCL::Image2DArray"; break;
1103     case CL_GL_OBJECT_TEXTURE3D: klass = "OpenCL::Image3D"; break;
1104     }
1105 root 1.51 XPUSH_CLOBJ (klass, mem);
1106 root 1.39
1107 root 1.44 #endif
1108 root 1.40
1109 root 1.25 void
1110 root 1.40 gl_texture2d (OpenCL::Context self, cl_mem_flags flags, cl_GLenum target, cl_GLint miplevel, cl_GLuint texture)
1111 root 1.25 PPCODE:
1112 root 1.44 #if PREFER_1_1
1113 root 1.40 NEED_SUCCESS_ARG (cl_mem mem, CreateFromGLTexture2D, (self, flags, target, miplevel, texture, &res));
1114 root 1.44 #else
1115     NEED_SUCCESS_ARG (cl_mem mem, CreateFromGLTexture , (self, flags, target, miplevel, texture, &res));
1116     #endif
1117 root 1.51 XPUSH_CLOBJ ("OpenCL::Image2D", mem);
1118 root 1.25
1119 root 1.40 void
1120     gl_texture3d (OpenCL::Context self, cl_mem_flags flags, cl_GLenum target, cl_GLint miplevel, cl_GLuint texture)
1121     PPCODE:
1122 root 1.44 #if PREFER_1_1
1123 root 1.40 NEED_SUCCESS_ARG (cl_mem mem, CreateFromGLTexture3D, (self, flags, target, miplevel, texture, &res));
1124 root 1.44 #else
1125     NEED_SUCCESS_ARG (cl_mem mem, CreateFromGLTexture , (self, flags, target, miplevel, texture, &res));
1126     #endif
1127 root 1.51 XPUSH_CLOBJ ("OpenCL::Image3D", mem);
1128 root 1.40
1129     #endif
1130    
1131 root 1.3 void
1132 root 1.22 supported_image_formats (OpenCL::Context self, cl_mem_flags flags, cl_mem_object_type image_type)
1133 root 1.3 PPCODE:
1134     {
1135     cl_uint count;
1136     cl_image_format *list;
1137     int i;
1138    
1139 root 1.23 NEED_SUCCESS (GetSupportedImageFormats, (self, flags, image_type, 0, 0, &count));
1140 root 1.3 Newx (list, count, cl_image_format);
1141 root 1.23 NEED_SUCCESS (GetSupportedImageFormats, (self, flags, image_type, count, list, 0));
1142 root 1.3
1143     EXTEND (SP, count);
1144     for (i = 0; i < count; ++i)
1145     {
1146     AV *av = newAV ();
1147     av_store (av, 1, newSVuv (list [i].image_channel_data_type));
1148     av_store (av, 0, newSVuv (list [i].image_channel_order));
1149     PUSHs (sv_2mortal (newRV_noinc ((SV *)av)));
1150     }
1151 root 1.2 }
1152    
1153     void
1154 root 1.22 sampler (OpenCL::Context self, cl_bool normalized_coords, cl_addressing_mode addressing_mode, cl_filter_mode filter_mode)
1155 root 1.2 PPCODE:
1156 root 1.23 NEED_SUCCESS_ARG (cl_sampler sampler, CreateSampler, (self, normalized_coords, addressing_mode, filter_mode, &res));
1157 root 1.51 XPUSH_CLOBJ ("OpenCL::Sampler", sampler);
1158 root 1.1
1159     void
1160 root 1.22 program_with_source (OpenCL::Context self, SV *program)
1161 root 1.1 PPCODE:
1162 root 1.2 STRLEN len;
1163     size_t len2;
1164     const char *ptr = SvPVbyte (program, len);
1165    
1166     len2 = len;
1167 root 1.23 NEED_SUCCESS_ARG (cl_program prog, CreateProgramWithSource, (self, 1, &ptr, &len2, &res));
1168 root 1.51 XPUSH_CLOBJ ("OpenCL::Program", prog);
1169 root 1.1
1170 root 1.13 #BEGIN:context
1171    
1172 root 1.14 void
1173 root 1.22 reference_count (OpenCL::Context self)
1174 root 1.16 ALIAS:
1175     reference_count = CL_CONTEXT_REFERENCE_COUNT
1176     num_devices = CL_CONTEXT_NUM_DEVICES
1177 root 1.14 PPCODE:
1178     cl_uint value [1];
1179 root 1.22 NEED_SUCCESS (GetContextInfo, (self, ix, sizeof (value), value, 0));
1180 root 1.14 EXTEND (SP, 1);
1181     const int i = 0;
1182     PUSHs (sv_2mortal (newSVuv (value [i])));
1183    
1184     void
1185 root 1.22 devices (OpenCL::Context self)
1186 root 1.14 PPCODE:
1187     size_t size;
1188 root 1.22 NEED_SUCCESS (GetContextInfo, (self, CL_CONTEXT_DEVICES, 0, 0, &size));
1189 root 1.14 cl_device_id *value = tmpbuf (size);
1190 root 1.22 NEED_SUCCESS (GetContextInfo, (self, CL_CONTEXT_DEVICES, size, value, 0));
1191 root 1.15 int i, n = size / sizeof (*value);
1192 root 1.14 EXTEND (SP, n);
1193     for (i = 0; i < n; ++i)
1194     {
1195 root 1.51 PUSH_CLOBJ ("OpenCL::Device", value [i]);
1196 root 1.14 }
1197    
1198     void
1199 root 1.22 properties (OpenCL::Context self)
1200 root 1.14 PPCODE:
1201     size_t size;
1202 root 1.22 NEED_SUCCESS (GetContextInfo, (self, CL_CONTEXT_PROPERTIES, 0, 0, &size));
1203 root 1.14 cl_context_properties *value = tmpbuf (size);
1204 root 1.22 NEED_SUCCESS (GetContextInfo, (self, CL_CONTEXT_PROPERTIES, size, value, 0));
1205 root 1.15 int i, n = size / sizeof (*value);
1206 root 1.14 EXTEND (SP, n);
1207     for (i = 0; i < n; ++i)
1208     PUSHs (sv_2mortal (newSVuv ((UV)value [i])));
1209    
1210 root 1.13 #END:context
1211    
1212 root 1.1 MODULE = OpenCL PACKAGE = OpenCL::Queue
1213    
1214     void
1215 root 1.22 DESTROY (OpenCL::Queue self)
1216 root 1.1 CODE:
1217 root 1.22 clReleaseCommandQueue (self);
1218 root 1.1
1219     void
1220 root 1.22 enqueue_read_buffer (OpenCL::Queue self, OpenCL::Buffer mem, cl_bool blocking, size_t offset, size_t len, SV *data, ...)
1221 root 1.2 PPCODE:
1222     cl_event ev = 0;
1223     EVENT_LIST (6, items - 6);
1224    
1225     SvUPGRADE (data, SVt_PV);
1226     SvGROW (data, len);
1227     SvPOK_only (data);
1228     SvCUR_set (data, len);
1229 root 1.22 NEED_SUCCESS (EnqueueReadBuffer, (self, mem, blocking, offset, len, SvPVX (data), event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0));
1230 root 1.2
1231     if (ev)
1232 root 1.51 XPUSH_CLOBJ ("OpenCL::Event", ev);
1233 root 1.2
1234     void
1235 root 1.22 enqueue_write_buffer (OpenCL::Queue self, OpenCL::Buffer mem, cl_bool blocking, size_t offset, SV *data, ...)
1236 root 1.2 PPCODE:
1237     cl_event ev = 0;
1238     STRLEN len;
1239     char *ptr = SvPVbyte (data, len);
1240     EVENT_LIST (5, items - 5);
1241    
1242 root 1.34 NEED_SUCCESS (EnqueueWriteBuffer, (self, mem, blocking, offset, len, ptr, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0));
1243 root 1.2
1244     if (ev)
1245 root 1.51 XPUSH_CLOBJ ("OpenCL::Event", ev);
1246 root 1.2
1247 root 1.48 #if CL_VERSION_1_2
1248    
1249     void
1250     enqueue_fill_buffer (OpenCL::Queue self, OpenCL::Buffer mem, SV *data, size_t offset, size_t size, ...)
1251     PPCODE:
1252     cl_event ev = 0;
1253     STRLEN len;
1254     char *ptr = SvPVbyte (data, len);
1255     EVENT_LIST (5, items - 5);
1256    
1257     NEED_SUCCESS (EnqueueFillBuffer, (self, mem, ptr, len, offset, size, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0));
1258    
1259     if (ev)
1260 root 1.51 XPUSH_CLOBJ ("OpenCL::Event", ev);
1261 root 1.48
1262     void
1263     enqueue_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, ...)
1264     PPCODE:
1265     cl_event ev = 0;
1266     STRLEN len;
1267     const size_t origin [3] = { x, y, z };
1268     const size_t region [3] = { width, height, depth };
1269     EVENT_LIST (12, items - 12);
1270    
1271     const cl_float c_f [4] = { r, g, b, a };
1272     const cl_uint c_u [4] = { r, g, b, a };
1273     const cl_int c_s [4] = { r, g, b, a };
1274     const void *c_fus [3] = { &c_f, &c_u, &c_s };
1275     static const char fus [] = { 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 1, 1, 1, 0, 0 };
1276     cl_image_format format;
1277 root 1.50 NEED_SUCCESS (GetImageInfo, (img, CL_IMAGE_FORMAT, sizeof (format), &format, 0));
1278 root 1.48 assert (sizeof (fus) == CL_FLOAT + 1 - CL_SNORM_INT8);
1279     if (format.image_channel_data_type < CL_SNORM_INT8 || CL_FLOAT < format.image_channel_data_type)
1280     croak ("enqueue_fill_image: image has unsupported channel type, only opencl 1.2 channel types supported.");
1281    
1282     NEED_SUCCESS (EnqueueFillImage, (self, img, c_fus [fus [format.image_channel_data_type]],
1283     origin, region, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0));
1284    
1285     if (ev)
1286 root 1.51 XPUSH_CLOBJ ("OpenCL::Event", ev);
1287 root 1.48
1288     #endif
1289    
1290 root 1.2 void
1291 root 1.22 enqueue_copy_buffer (OpenCL::Queue self, OpenCL::Buffer src, OpenCL::Buffer dst, size_t src_offset, size_t dst_offset, size_t len, ...)
1292 root 1.2 PPCODE:
1293     cl_event ev = 0;
1294     EVENT_LIST (6, items - 6);
1295    
1296 root 1.22 NEED_SUCCESS (EnqueueCopyBuffer, (self, src, dst, src_offset, dst_offset, len, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0));
1297 root 1.2
1298     if (ev)
1299 root 1.51 XPUSH_CLOBJ ("OpenCL::Event", ev);
1300 root 1.2
1301 root 1.3 void
1302 root 1.22 enqueue_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, ...)
1303 root 1.17 PPCODE:
1304     cl_event ev = 0;
1305     const size_t buf_origin [3] = { buf_x , buf_y , buf_z };
1306     const size_t host_origin[3] = { host_x, host_y, host_z };
1307     const size_t region[3] = { width, height, depth };
1308     EVENT_LIST (17, items - 17);
1309    
1310     if (!buf_row_pitch)
1311     buf_row_pitch = region [0];
1312    
1313     if (!buf_slice_pitch)
1314     buf_slice_pitch = region [1] * buf_row_pitch;
1315    
1316     if (!host_row_pitch)
1317     host_row_pitch = region [0];
1318    
1319     if (!host_slice_pitch)
1320     host_slice_pitch = region [1] * host_row_pitch;
1321    
1322     size_t len = host_row_pitch * host_slice_pitch * region [2];
1323    
1324     SvUPGRADE (data, SVt_PV);
1325     SvGROW (data, len);
1326     SvPOK_only (data);
1327     SvCUR_set (data, len);
1328 root 1.22 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));
1329 root 1.17
1330     if (ev)
1331 root 1.51 XPUSH_CLOBJ ("OpenCL::Event", ev);
1332 root 1.17
1333     void
1334 root 1.22 enqueue_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, ...)
1335 root 1.17 PPCODE:
1336     cl_event ev = 0;
1337     const size_t buf_origin [3] = { buf_x , buf_y , buf_z };
1338     const size_t host_origin[3] = { host_x, host_y, host_z };
1339     const size_t region[3] = { width, height, depth };
1340     STRLEN len;
1341     char *ptr = SvPVbyte (data, len);
1342     EVENT_LIST (17, items - 17);
1343    
1344     if (!buf_row_pitch)
1345     buf_row_pitch = region [0];
1346    
1347     if (!buf_slice_pitch)
1348     buf_slice_pitch = region [1] * buf_row_pitch;
1349    
1350     if (!host_row_pitch)
1351     host_row_pitch = region [0];
1352    
1353     if (!host_slice_pitch)
1354     host_slice_pitch = region [1] * host_row_pitch;
1355    
1356     size_t min_len = host_row_pitch * host_slice_pitch * region [2];
1357    
1358     if (len < min_len)
1359     croak ("clEnqueueWriteImage: data string is shorter than what would be transferred");
1360    
1361 root 1.37 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));
1362 root 1.17
1363     if (ev)
1364 root 1.51 XPUSH_CLOBJ ("OpenCL::Event", ev);
1365 root 1.17
1366     void
1367 root 1.22 enqueue_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, ...)
1368 root 1.18 PPCODE:
1369     cl_event ev = 0;
1370     const size_t src_origin[3] = { src_x, src_y, src_z };
1371     const size_t dst_origin[3] = { dst_x, dst_y, dst_z };
1372     const size_t region[3] = { width, height, depth };
1373     EVENT_LIST (16, items - 16);
1374    
1375 root 1.22 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));
1376 root 1.18
1377     if (ev)
1378 root 1.51 XPUSH_CLOBJ ("OpenCL::Event", ev);
1379 root 1.18
1380     void
1381 root 1.22 enqueue_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, ...)
1382 root 1.3 PPCODE:
1383     cl_event ev = 0;
1384     const size_t src_origin[3] = { src_x, src_y, src_z };
1385     const size_t region[3] = { width, height, depth };
1386 root 1.10 EVENT_LIST (12, items - 12);
1387    
1388 root 1.11 if (!row_pitch)
1389     row_pitch = img_row_pitch (src);
1390    
1391     if (depth > 1 && !slice_pitch)
1392     slice_pitch = row_pitch * height;
1393    
1394     size_t len = slice_pitch ? slice_pitch * depth : row_pitch * height;
1395 root 1.3
1396     SvUPGRADE (data, SVt_PV);
1397     SvGROW (data, len);
1398     SvPOK_only (data);
1399     SvCUR_set (data, len);
1400 root 1.22 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));
1401 root 1.3
1402     if (ev)
1403 root 1.51 XPUSH_CLOBJ ("OpenCL::Event", ev);
1404 root 1.3
1405     void
1406 root 1.22 enqueue_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, ...)
1407 root 1.3 PPCODE:
1408     cl_event ev = 0;
1409     const size_t dst_origin[3] = { dst_x, dst_y, dst_z };
1410     const size_t region[3] = { width, height, depth };
1411     STRLEN len;
1412     char *ptr = SvPVbyte (data, len);
1413 root 1.10 EVENT_LIST (12, items - 12);
1414 root 1.3
1415 root 1.11 if (!row_pitch)
1416     row_pitch = img_row_pitch (dst);
1417    
1418     if (depth > 1 && !slice_pitch)
1419     slice_pitch = row_pitch * height;
1420    
1421     size_t min_len = slice_pitch ? slice_pitch * depth : row_pitch * height;
1422    
1423     if (len < min_len)
1424     croak ("clEnqueueWriteImage: data string is shorter than what would be transferred");
1425    
1426 root 1.37 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));
1427 root 1.3
1428     if (ev)
1429 root 1.51 XPUSH_CLOBJ ("OpenCL::Event", ev);
1430 root 1.3
1431     void
1432 root 1.22 enqueue_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, ...)
1433 root 1.3 PPCODE:
1434     cl_event ev = 0;
1435     const size_t src_origin[3] = { src_x, src_y, src_z };
1436     const size_t dst_origin[3] = { dst_x, dst_y, dst_z };
1437     const size_t region[3] = { width, height, depth };
1438 root 1.18 EVENT_LIST (12, items - 12);
1439 root 1.3
1440 root 1.22 NEED_SUCCESS (EnqueueCopyImage, (self, src, dst, src_origin, dst_origin, region, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0));
1441 root 1.3
1442     if (ev)
1443 root 1.51 XPUSH_CLOBJ ("OpenCL::Event", ev);
1444 root 1.3
1445     void
1446 root 1.22 enqueue_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, ...)
1447 root 1.3 PPCODE:
1448     cl_event ev = 0;
1449 root 1.18 const size_t src_origin[3] = { src_x, src_y, src_z };
1450 root 1.3 const size_t region[3] = { width, height, depth };
1451     EVENT_LIST (10, items - 10);
1452    
1453 root 1.22 NEED_SUCCESS (EnqueueCopyImageToBuffer, (self, src, dst, src_origin, region, dst_offset, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0));
1454 root 1.3
1455     if (ev)
1456 root 1.51 XPUSH_CLOBJ ("OpenCL::Event", ev);
1457 root 1.3
1458     void
1459 root 1.22 enqueue_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, ...)
1460 root 1.3 PPCODE:
1461     cl_event ev = 0;
1462     const size_t dst_origin[3] = { dst_x, dst_y, dst_z };
1463     const size_t region[3] = { width, height, depth };
1464     EVENT_LIST (10, items - 10);
1465    
1466 root 1.22 NEED_SUCCESS (EnqueueCopyBufferToImage, (self, src, dst, src_offset, dst_origin, region, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0));
1467 root 1.3
1468     if (ev)
1469 root 1.51 XPUSH_CLOBJ ("OpenCL::Event", ev);
1470 root 1.3
1471     void
1472 root 1.22 enqueue_task (OpenCL::Queue self, OpenCL::Kernel kernel, ...)
1473 root 1.3 PPCODE:
1474     cl_event ev = 0;
1475     EVENT_LIST (2, items - 2);
1476    
1477 root 1.22 NEED_SUCCESS (EnqueueTask, (self, kernel, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0));
1478 root 1.3
1479     if (ev)
1480 root 1.51 XPUSH_CLOBJ ("OpenCL::Event", ev);
1481 root 1.3
1482 root 1.4 void
1483 root 1.22 enqueue_nd_range_kernel (OpenCL::Queue self, OpenCL::Kernel kernel, SV *global_work_offset, SV *global_work_size, SV *local_work_size = &PL_sv_undef, ...)
1484 root 1.4 PPCODE:
1485     cl_event ev = 0;
1486     size_t *gwo = 0, *gws, *lws = 0;
1487     int gws_len;
1488     size_t *lists;
1489     int i;
1490 root 1.5 EVENT_LIST (5, items - 5);
1491 root 1.4
1492     if (!SvROK (global_work_size) || SvTYPE (SvRV (global_work_size)) != SVt_PVAV)
1493     croak ("clEnqueueNDRangeKernel: global_work_size must be an array reference");
1494    
1495     gws_len = AvFILLp (SvRV (global_work_size)) + 1;
1496    
1497     lists = tmpbuf (sizeof (size_t) * 3 * gws_len);
1498    
1499     gws = lists + gws_len * 0;
1500     for (i = 0; i < gws_len; ++i)
1501     gws [i] = SvIV (AvARRAY (SvRV (global_work_size))[i]);
1502    
1503     if (SvOK (global_work_offset))
1504     {
1505     if (!SvROK (global_work_offset) || SvTYPE (SvRV (global_work_offset)) != SVt_PVAV)
1506     croak ("clEnqueueNDRangeKernel: global_work_offset must be undef or an array reference");
1507    
1508     if (AvFILLp (SvRV (global_work_size)) + 1 != gws_len)
1509     croak ("clEnqueueNDRangeKernel: global_work_offset must be undef or an array of same size as global_work_size");
1510    
1511     gwo = lists + gws_len * 1;
1512     for (i = 0; i < gws_len; ++i)
1513     gwo [i] = SvIV (AvARRAY (SvRV (global_work_offset))[i]);
1514     }
1515    
1516     if (SvOK (local_work_size))
1517     {
1518 root 1.37 if ((SvOK (local_work_size) && !SvROK (local_work_size)) || SvTYPE (SvRV (local_work_size)) != SVt_PVAV)
1519 root 1.4 croak ("clEnqueueNDRangeKernel: global_work_size must be undef or an array reference");
1520    
1521     if (AvFILLp (SvRV (local_work_size)) + 1 != gws_len)
1522     croak ("clEnqueueNDRangeKernel: local_work_local must be undef or an array of same size as global_work_size");
1523    
1524     lws = lists + gws_len * 2;
1525     for (i = 0; i < gws_len; ++i)
1526     lws [i] = SvIV (AvARRAY (SvRV (local_work_size))[i]);
1527     }
1528    
1529 root 1.22 NEED_SUCCESS (EnqueueNDRangeKernel, (self, kernel, gws_len, gwo, gws, lws, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0));
1530 root 1.4
1531     if (ev)
1532 root 1.51 XPUSH_CLOBJ ("OpenCL::Event", ev);
1533 root 1.3
1534 root 1.25 #if cl_apple_gl_sharing || cl_khr_gl_sharing
1535    
1536     void
1537     enqueue_acquire_gl_objects (OpenCL::Queue self, SV *objects, ...)
1538 root 1.27 ALIAS:
1539     enqueue_release_gl_objects = 1
1540 root 1.36 PPCODE:
1541 root 1.27 if (!SvROK (objects) || SvTYPE (SvRV (objects)) != SVt_PVAV)
1542     croak ("OpenCL::Queue::enqueue_acquire/release_gl_objects argument 'objects' must be an arrayref with memory objects, in call");
1543    
1544 root 1.25 cl_event ev = 0;
1545     EVENT_LIST (2, items - 2);
1546 root 1.27 AV *av = (AV *)SvRV (objects);
1547     cl_uint num_objects = av_len (av) + 1;
1548     cl_mem *object_list = tmpbuf (sizeof (cl_mem) * num_objects);
1549     int i;
1550 root 1.25
1551 root 1.27 for (i = num_objects; i--; )
1552 root 1.51 object_list [i] = SvCLOBJ ("OpenCL::Queue::enqueue_acquire/release_gl_objects", "objects", *av_fetch (av, i, 0), "OpenCL::Memory");
1553 root 1.25
1554 root 1.27 if (ix)
1555     NEED_SUCCESS (EnqueueReleaseGLObjects, (self, num_objects, object_list, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0));
1556     else
1557     NEED_SUCCESS (EnqueueAcquireGLObjects, (self, num_objects, object_list, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0));
1558 root 1.25
1559     if (ev)
1560 root 1.51 XPUSH_CLOBJ ("OpenCL::Event", ev);
1561 root 1.25
1562     #endif
1563    
1564 root 1.2 void
1565 root 1.22 enqueue_wait_for_events (OpenCL::Queue self, ...)
1566 root 1.2 CODE:
1567     EVENT_LIST (1, items - 1);
1568 root 1.47 #if PREFER_1_1
1569 root 1.22 NEED_SUCCESS (EnqueueWaitForEvents, (self, event_list_count, event_list_ptr));
1570 root 1.47 #else
1571     NEED_SUCCESS (EnqueueBarrierWithWaitList, (self, event_list_count, event_list_ptr, 0));
1572 root 1.38 #endif
1573    
1574     void
1575     enqueue_marker (OpenCL::Queue self, ...)
1576     PPCODE:
1577     cl_event ev = 0;
1578     EVENT_LIST (1, items - 1);
1579 root 1.45 #if PREFER_1_1
1580 root 1.47 if (!event_list_count)
1581     NEED_SUCCESS (EnqueueMarker, (self, GIMME_V != G_VOID ? &ev : 0));
1582     else
1583 root 1.46 #if CL_VERSION_1_2
1584     NEED_SUCCESS (EnqueueMarkerWithWaitList, (self, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0));
1585     #else
1586 root 1.50 {
1587     NEED_SUCCESS (EnqueueWaitForEvents, (self, event_list_count, event_list_ptr)); // also a barrier
1588     NEED_SUCCESS (EnqueueMarker, (self, GIMME_V != G_VOID ? &ev : 0));
1589     }
1590 root 1.46 #endif
1591 root 1.45 #else
1592     NEED_SUCCESS (EnqueueMarkerWithWaitList, (self, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0));
1593 root 1.38 #endif
1594     if (ev)
1595 root 1.51 XPUSH_CLOBJ ("OpenCL::Event", ev);
1596 root 1.38
1597 root 1.2 void
1598 root 1.38 enqueue_barrier (OpenCL::Queue self, ...)
1599     PPCODE:
1600     cl_event ev = 0;
1601     EVENT_LIST (1, items - 1);
1602 root 1.45 #if PREFER_1_1
1603 root 1.47 if (!event_list_count && GIMME_V == G_VOID)
1604     NEED_SUCCESS (EnqueueBarrier, (self));
1605     else
1606 root 1.46 #if CL_VERSION_1_2
1607     NEED_SUCCESS (EnqueueBarrierWithWaitList, (self, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0));
1608     #else
1609 root 1.47 {
1610     if (event_list_count)
1611 root 1.50 NEED_SUCCESS (EnqueueWaitForEvents, (self, event_list_count, event_list_ptr));
1612 root 1.47
1613     if (GIMME_V != G_VOID)
1614     NEED_SUCCESS (EnqueueMarker, (self, &ev));
1615     }
1616 root 1.46 #endif
1617 root 1.45 #else
1618 root 1.46 NEED_SUCCESS (EnqueueBarrierWithWaitList, (self, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0));
1619 root 1.37 #endif
1620 root 1.38 if (ev)
1621 root 1.51 XPUSH_CLOBJ ("OpenCL::Event", ev);
1622 root 1.37
1623 root 1.3 void
1624 root 1.22 flush (OpenCL::Queue self)
1625 root 1.3 CODE:
1626 root 1.22 NEED_SUCCESS (Flush, (self));
1627 root 1.3
1628     void
1629 root 1.22 finish (OpenCL::Queue self)
1630 root 1.3 CODE:
1631 root 1.22 NEED_SUCCESS (Finish, (self));
1632 root 1.3
1633 root 1.14 void
1634 root 1.22 info (OpenCL::Queue self, cl_command_queue_info name)
1635 root 1.14 PPCODE:
1636     INFO (CommandQueue)
1637    
1638     #BEGIN:command_queue
1639    
1640     void
1641 root 1.22 context (OpenCL::Queue self)
1642 root 1.14 PPCODE:
1643     cl_context value [1];
1644 root 1.22 NEED_SUCCESS (GetCommandQueueInfo, (self, CL_QUEUE_CONTEXT, sizeof (value), value, 0));
1645 root 1.14 EXTEND (SP, 1);
1646     const int i = 0;
1647     {
1648     NEED_SUCCESS (RetainContext, (value [i]));
1649 root 1.51 PUSH_CLOBJ ("OpenCL::Context", value [i]);
1650 root 1.14 }
1651    
1652     void
1653 root 1.22 device (OpenCL::Queue self)
1654 root 1.14 PPCODE:
1655     cl_device_id value [1];
1656 root 1.22 NEED_SUCCESS (GetCommandQueueInfo, (self, CL_QUEUE_DEVICE, sizeof (value), value, 0));
1657 root 1.14 EXTEND (SP, 1);
1658     const int i = 0;
1659     {
1660 root 1.51 PUSH_CLOBJ ("OpenCL::Device", value [i]);
1661 root 1.14 }
1662    
1663     void
1664 root 1.22 reference_count (OpenCL::Queue self)
1665 root 1.14 PPCODE:
1666     cl_uint value [1];
1667 root 1.22 NEED_SUCCESS (GetCommandQueueInfo, (self, CL_QUEUE_REFERENCE_COUNT, sizeof (value), value, 0));
1668 root 1.14 EXTEND (SP, 1);
1669     const int i = 0;
1670     PUSHs (sv_2mortal (newSVuv (value [i])));
1671    
1672     void
1673 root 1.22 properties (OpenCL::Queue self)
1674 root 1.14 PPCODE:
1675     cl_command_queue_properties value [1];
1676 root 1.22 NEED_SUCCESS (GetCommandQueueInfo, (self, CL_QUEUE_PROPERTIES, sizeof (value), value, 0));
1677 root 1.14 EXTEND (SP, 1);
1678     const int i = 0;
1679     PUSHs (sv_2mortal (newSViv (value [i])));
1680    
1681     #END:command_queue
1682    
1683 root 1.2 MODULE = OpenCL PACKAGE = OpenCL::Memory
1684    
1685     void
1686 root 1.22 DESTROY (OpenCL::Memory self)
1687 root 1.2 CODE:
1688 root 1.22 clReleaseMemObject (self);
1689 root 1.2
1690     void
1691 root 1.22 info (OpenCL::Memory self, cl_mem_info name)
1692 root 1.2 PPCODE:
1693     INFO (MemObject)
1694    
1695 root 1.14 #BEGIN:mem
1696    
1697     void
1698 root 1.22 type (OpenCL::Memory self)
1699 root 1.14 PPCODE:
1700     cl_mem_object_type value [1];
1701 root 1.22 NEED_SUCCESS (GetMemObjectInfo, (self, CL_MEM_TYPE, sizeof (value), value, 0));
1702 root 1.14 EXTEND (SP, 1);
1703     const int i = 0;
1704     PUSHs (sv_2mortal (newSViv (value [i])));
1705    
1706     void
1707 root 1.22 flags (OpenCL::Memory self)
1708 root 1.14 PPCODE:
1709     cl_mem_flags value [1];
1710 root 1.22 NEED_SUCCESS (GetMemObjectInfo, (self, CL_MEM_FLAGS, sizeof (value), value, 0));
1711 root 1.14 EXTEND (SP, 1);
1712     const int i = 0;
1713     PUSHs (sv_2mortal (newSViv (value [i])));
1714    
1715     void
1716 root 1.22 size (OpenCL::Memory self)
1717 root 1.16 ALIAS:
1718     size = CL_MEM_SIZE
1719     offset = CL_MEM_OFFSET
1720 root 1.14 PPCODE:
1721     size_t value [1];
1722 root 1.22 NEED_SUCCESS (GetMemObjectInfo, (self, ix, sizeof (value), value, 0));
1723 root 1.14 EXTEND (SP, 1);
1724     const int i = 0;
1725     PUSHs (sv_2mortal (newSVuv (value [i])));
1726    
1727     void
1728 root 1.22 host_ptr (OpenCL::Memory self)
1729 root 1.14 PPCODE:
1730     void * value [1];
1731 root 1.22 NEED_SUCCESS (GetMemObjectInfo, (self, CL_MEM_HOST_PTR, sizeof (value), value, 0));
1732 root 1.14 EXTEND (SP, 1);
1733     const int i = 0;
1734     PUSHs (sv_2mortal (newSVuv ((IV)(intptr_t)value [i])));
1735    
1736     void
1737 root 1.22 map_count (OpenCL::Memory self)
1738 root 1.16 ALIAS:
1739     map_count = CL_MEM_MAP_COUNT
1740     reference_count = CL_MEM_REFERENCE_COUNT
1741 root 1.14 PPCODE:
1742     cl_uint value [1];
1743 root 1.22 NEED_SUCCESS (GetMemObjectInfo, (self, ix, sizeof (value), value, 0));
1744 root 1.14 EXTEND (SP, 1);
1745     const int i = 0;
1746     PUSHs (sv_2mortal (newSVuv (value [i])));
1747    
1748     void
1749 root 1.22 context (OpenCL::Memory self)
1750 root 1.14 PPCODE:
1751     cl_context value [1];
1752 root 1.22 NEED_SUCCESS (GetMemObjectInfo, (self, CL_MEM_CONTEXT, sizeof (value), value, 0));
1753 root 1.14 EXTEND (SP, 1);
1754     const int i = 0;
1755     {
1756     NEED_SUCCESS (RetainContext, (value [i]));
1757 root 1.51 PUSH_CLOBJ ("OpenCL::Context", value [i]);
1758 root 1.14 }
1759    
1760     void
1761 root 1.22 associated_memobject (OpenCL::Memory self)
1762 root 1.14 PPCODE:
1763     cl_mem value [1];
1764 root 1.22 NEED_SUCCESS (GetMemObjectInfo, (self, CL_MEM_ASSOCIATED_MEMOBJECT, sizeof (value), value, 0));
1765 root 1.14 EXTEND (SP, 1);
1766     const int i = 0;
1767     {
1768     NEED_SUCCESS (RetainMemObject, (value [i]));
1769 root 1.51 PUSH_CLOBJ ("OpenCL::Memory", value [i]);
1770 root 1.14 }
1771    
1772     #END:mem
1773    
1774 root 1.26 #if cl_apple_gl_sharing || cl_khr_gl_sharing
1775    
1776     void
1777     gl_object_info (OpenCL::Memory self)
1778     PPCODE:
1779     cl_gl_object_type type;
1780     cl_GLuint name;
1781 root 1.31 NEED_SUCCESS (GetGLObjectInfo, (self, &type, &name));
1782 root 1.26 EXTEND (SP, 2);
1783     PUSHs (sv_2mortal (newSVuv (type)));
1784     PUSHs (sv_2mortal (newSVuv (name)));
1785    
1786     #endif
1787    
1788 root 1.18 MODULE = OpenCL PACKAGE = OpenCL::BufferObj
1789    
1790     void
1791 root 1.22 sub_buffer_region (OpenCL::BufferObj self, cl_mem_flags flags, size_t origin, size_t size)
1792 root 1.18 PPCODE:
1793     if (flags & (CL_MEM_USE_HOST_PTR | CL_MEM_COPY_HOST_PTR | CL_MEM_ALLOC_HOST_PTR))
1794     croak ("clCreateSubBuffer: cannot use/copy/alloc host ptr, doesn't make sense, check your flags!");
1795    
1796     cl_buffer_region crdata = { origin, size };
1797    
1798 root 1.22 NEED_SUCCESS_ARG (cl_mem mem, CreateSubBuffer, (self, flags, CL_BUFFER_CREATE_TYPE_REGION, &crdata, &res));
1799 root 1.51 XPUSH_CLOBJ ("OpenCL::Buffer", mem);
1800 root 1.18
1801 root 1.13 MODULE = OpenCL PACKAGE = OpenCL::Image
1802    
1803     void
1804 root 1.22 image_info (OpenCL::Image self, cl_image_info name)
1805 root 1.13 PPCODE:
1806     INFO (Image)
1807    
1808 root 1.49 void
1809     format (OpenCL::Image self)
1810     PPCODE:
1811     cl_image_format format;
1812     NEED_SUCCESS (GetImageInfo, (self, CL_IMAGE_FORMAT, sizeof (format), &format, 0));
1813     EXTEND (SP, 2);
1814     PUSHs (sv_2mortal (newSVuv (format.image_channel_order)));
1815     PUSHs (sv_2mortal (newSVuv (format.image_channel_data_type)));
1816    
1817 root 1.14 #BEGIN:image
1818    
1819     void
1820 root 1.22 element_size (OpenCL::Image self)
1821 root 1.16 ALIAS:
1822     element_size = CL_IMAGE_ELEMENT_SIZE
1823     row_pitch = CL_IMAGE_ROW_PITCH
1824     slice_pitch = CL_IMAGE_SLICE_PITCH
1825     width = CL_IMAGE_WIDTH
1826     height = CL_IMAGE_HEIGHT
1827     depth = CL_IMAGE_DEPTH
1828 root 1.14 PPCODE:
1829     size_t value [1];
1830 root 1.22 NEED_SUCCESS (GetImageInfo, (self, ix, sizeof (value), value, 0));
1831 root 1.14 EXTEND (SP, 1);
1832     const int i = 0;
1833     PUSHs (sv_2mortal (newSVuv (value [i])));
1834    
1835     #END:image
1836    
1837 root 1.26 #if cl_apple_gl_sharing || cl_khr_gl_sharing
1838    
1839     #BEGIN:gl_texture
1840    
1841     void
1842     target (OpenCL::Image self)
1843     PPCODE:
1844     cl_GLenum value [1];
1845 root 1.31 NEED_SUCCESS (GetGLTextureInfo, (self, CL_GL_TEXTURE_TARGET, sizeof (value), value, 0));
1846 root 1.26 EXTEND (SP, 1);
1847     const int i = 0;
1848     PUSHs (sv_2mortal (newSVuv (value [i])));
1849    
1850     void
1851     gl_mipmap_level (OpenCL::Image self)
1852     PPCODE:
1853     cl_GLint value [1];
1854 root 1.31 NEED_SUCCESS (GetGLTextureInfo, (self, CL_GL_MIPMAP_LEVEL, sizeof (value), value, 0));
1855 root 1.26 EXTEND (SP, 1);
1856     const int i = 0;
1857     PUSHs (sv_2mortal (newSViv (value [i])));
1858    
1859     #END:gl_texture
1860    
1861     #endif
1862    
1863 root 1.2 MODULE = OpenCL PACKAGE = OpenCL::Sampler
1864    
1865     void
1866 root 1.22 DESTROY (OpenCL::Sampler self)
1867 root 1.2 CODE:
1868 root 1.22 clReleaseSampler (self);
1869 root 1.2
1870     void
1871 root 1.22 info (OpenCL::Sampler self, cl_sampler_info name)
1872 root 1.2 PPCODE:
1873     INFO (Sampler)
1874    
1875 root 1.14 #BEGIN:sampler
1876    
1877     void
1878 root 1.22 reference_count (OpenCL::Sampler self)
1879 root 1.14 PPCODE:
1880     cl_uint value [1];
1881 root 1.22 NEED_SUCCESS (GetSamplerInfo, (self, CL_SAMPLER_REFERENCE_COUNT, sizeof (value), value, 0));
1882 root 1.14 EXTEND (SP, 1);
1883     const int i = 0;
1884     PUSHs (sv_2mortal (newSVuv (value [i])));
1885    
1886     void
1887 root 1.22 context (OpenCL::Sampler self)
1888 root 1.14 PPCODE:
1889     cl_context value [1];
1890 root 1.22 NEED_SUCCESS (GetSamplerInfo, (self, CL_SAMPLER_CONTEXT, sizeof (value), value, 0));
1891 root 1.14 EXTEND (SP, 1);
1892     const int i = 0;
1893     {
1894     NEED_SUCCESS (RetainContext, (value [i]));
1895 root 1.51 PUSH_CLOBJ ("OpenCL::Context", value [i]);
1896 root 1.14 }
1897    
1898     void
1899 root 1.22 normalized_coords (OpenCL::Sampler self)
1900 root 1.14 PPCODE:
1901     cl_addressing_mode value [1];
1902 root 1.22 NEED_SUCCESS (GetSamplerInfo, (self, CL_SAMPLER_NORMALIZED_COORDS, sizeof (value), value, 0));
1903 root 1.14 EXTEND (SP, 1);
1904     const int i = 0;
1905     PUSHs (sv_2mortal (newSViv (value [i])));
1906    
1907     void
1908 root 1.22 addressing_mode (OpenCL::Sampler self)
1909 root 1.14 PPCODE:
1910     cl_filter_mode value [1];
1911 root 1.22 NEED_SUCCESS (GetSamplerInfo, (self, CL_SAMPLER_ADDRESSING_MODE, sizeof (value), value, 0));
1912 root 1.14 EXTEND (SP, 1);
1913     const int i = 0;
1914     PUSHs (sv_2mortal (newSViv (value [i])));
1915    
1916     void
1917 root 1.22 filter_mode (OpenCL::Sampler self)
1918 root 1.14 PPCODE:
1919     cl_bool value [1];
1920 root 1.22 NEED_SUCCESS (GetSamplerInfo, (self, CL_SAMPLER_FILTER_MODE, sizeof (value), value, 0));
1921 root 1.14 EXTEND (SP, 1);
1922     const int i = 0;
1923     PUSHs (sv_2mortal (value [i] ? &PL_sv_yes : &PL_sv_no));
1924    
1925     #END:sampler
1926    
1927 root 1.2 MODULE = OpenCL PACKAGE = OpenCL::Program
1928    
1929     void
1930 root 1.22 DESTROY (OpenCL::Program self)
1931 root 1.2 CODE:
1932 root 1.22 clReleaseProgram (self);
1933 root 1.2
1934     void
1935 root 1.51 build (OpenCL::Program self, SV *devices = &PL_sv_undef, SV *options = &PL_sv_undef, SV *notify = &PL_sv_undef)
1936     ALIAS:
1937     build_async = 1
1938 root 1.2 CODE:
1939 root 1.51 void (CL_CALLBACK *pfn_notify)(cl_program program, void *user_data) = 0;
1940     void *user_data = 0;
1941     cl_uint num_devices = 0;
1942     cl_device_id *device_list = 0;
1943    
1944     if (SvOK (devices))
1945     {
1946     if (!SvROK (devices) || SvTYPE (SvRV (devices)) != SVt_PVAV)
1947     croak ("clProgramBuild: devices must be undef or an array of OpenCL::Device objects.");
1948    
1949     AV *av = (AV *)SvRV (devices);
1950     num_devices = av_len (av) + 1;
1951    
1952     if (num_devices)
1953     {
1954     device_list = tmpbuf (sizeof (*device_list) * num_devices);
1955     int count;
1956     for (count = 0; count < num_devices; ++count)
1957     device_list [count] = SvCLOBJ ("clBuildProgram", "devices", *av_fetch (av, count, 1), "OpenCL::Device");
1958     }
1959     }
1960    
1961     if (SvOK (notify))
1962     {
1963     NEED_SUCCESS (RetainProgram, (self));
1964     pfn_notify = eq_program_notify;
1965     user_data = SvREFCNT_inc (s_get_cv (notify));
1966     }
1967    
1968     if (ix)
1969     build_program_async (self, num_devices, device_list, SvPVbyte_nolen (options), user_data);
1970     else
1971     NEED_SUCCESS (BuildProgram, (self, num_devices, device_list, SvPVbyte_nolen (options), pfn_notify, user_data));
1972 root 1.2
1973     void
1974 root 1.22 build_info (OpenCL::Program self, OpenCL::Device device, cl_program_build_info name)
1975 root 1.2 PPCODE:
1976 root 1.23 size_t size;
1977     NEED_SUCCESS (GetProgramBuildInfo, (self, device, name, 0, 0, &size));
1978 root 1.10 SV *sv = sv_2mortal (newSV (size));
1979 root 1.1 SvUPGRADE (sv, SVt_PV);
1980     SvPOK_only (sv);
1981     SvCUR_set (sv, size);
1982 root 1.23 NEED_SUCCESS (GetProgramBuildInfo, (self, device, name, size, SvPVX (sv), 0));
1983 root 1.1 XPUSHs (sv);
1984    
1985 root 1.14 #BEGIN:program_build
1986    
1987     void
1988 root 1.22 build_status (OpenCL::Program self, OpenCL::Device device)
1989 root 1.14 PPCODE:
1990     cl_build_status value [1];
1991 root 1.22 NEED_SUCCESS (GetProgramBuildInfo, (self, device, CL_PROGRAM_BUILD_STATUS, sizeof (value), value, 0));
1992 root 1.14 EXTEND (SP, 1);
1993     const int i = 0;
1994     PUSHs (sv_2mortal (newSViv (value [i])));
1995    
1996     void
1997 root 1.22 build_options (OpenCL::Program self, OpenCL::Device device)
1998 root 1.16 ALIAS:
1999     build_options = CL_PROGRAM_BUILD_OPTIONS
2000     build_log = CL_PROGRAM_BUILD_LOG
2001 root 1.14 PPCODE:
2002     size_t size;
2003 root 1.22 NEED_SUCCESS (GetProgramBuildInfo, (self, device, ix, 0, 0, &size));
2004 root 1.14 char *value = tmpbuf (size);
2005 root 1.22 NEED_SUCCESS (GetProgramBuildInfo, (self, device, ix, size, value, 0));
2006 root 1.16 EXTEND (SP, 1);
2007     const int i = 0;
2008 root 1.14 PUSHs (sv_2mortal (newSVpv (value, 0)));
2009    
2010     #END:program_build
2011    
2012 root 1.2 void
2013     kernel (OpenCL::Program program, SV *function)
2014     PPCODE:
2015 root 1.23 NEED_SUCCESS_ARG (cl_kernel kernel, CreateKernel, (program, SvPVbyte_nolen (function), &res));
2016 root 1.51 XPUSH_CLOBJ ("OpenCL::Kernel", kernel);
2017 root 1.2
2018 root 1.14 void
2019 root 1.47 kernels_in_program (OpenCL::Program program)
2020     PPCODE:
2021     cl_uint num_kernels;
2022     NEED_SUCCESS (CreateKernelsInProgram, (program, 0, 0, &num_kernels));
2023     cl_kernel *kernels = tmpbuf (sizeof (cl_kernel) * num_kernels);
2024     NEED_SUCCESS (CreateKernelsInProgram, (program, num_kernels, kernels, 0));
2025    
2026     int i;
2027     EXTEND (SP, num_kernels);
2028     for (i = 0; i < num_kernels; ++i)
2029 root 1.51 PUSH_CLOBJ ("OpenCL::Kernel", kernels [i]);
2030 root 1.47
2031     void
2032 root 1.22 info (OpenCL::Program self, cl_program_info name)
2033 root 1.14 PPCODE:
2034     INFO (Program)
2035    
2036 root 1.15 void
2037 root 1.22 binaries (OpenCL::Program self)
2038 root 1.15 PPCODE:
2039     cl_uint n, i;
2040     size_t size;
2041    
2042 root 1.22 NEED_SUCCESS (GetProgramInfo, (self, CL_PROGRAM_NUM_DEVICES , sizeof (n) , &n , 0));
2043 root 1.15 if (!n) XSRETURN_EMPTY;
2044    
2045     size_t *sizes = tmpbuf (sizeof (*sizes) * n);
2046 root 1.22 NEED_SUCCESS (GetProgramInfo, (self, CL_PROGRAM_BINARY_SIZES, sizeof (*sizes) * n, sizes, &size));
2047 root 1.15 if (size != sizeof (*sizes) * n) XSRETURN_EMPTY;
2048     unsigned char **ptrs = tmpbuf (sizeof (*ptrs) * n);
2049    
2050     EXTEND (SP, n);
2051     for (i = 0; i < n; ++i)
2052     {
2053     SV *sv = sv_2mortal (newSV (sizes [i]));
2054     SvUPGRADE (sv, SVt_PV);
2055     SvPOK_only (sv);
2056     SvCUR_set (sv, sizes [i]);
2057 root 1.37 ptrs [i] = (void *)SvPVX (sv);
2058 root 1.15 PUSHs (sv);
2059     }
2060    
2061 root 1.22 NEED_SUCCESS (GetProgramInfo, (self, CL_PROGRAM_BINARIES , sizeof (*ptrs ) * n, ptrs , &size));
2062 root 1.15 if (size != sizeof (*ptrs) * n) XSRETURN_EMPTY;
2063    
2064 root 1.14 #BEGIN:program
2065    
2066     void
2067 root 1.22 reference_count (OpenCL::Program self)
2068 root 1.16 ALIAS:
2069     reference_count = CL_PROGRAM_REFERENCE_COUNT
2070     num_devices = CL_PROGRAM_NUM_DEVICES
2071 root 1.14 PPCODE:
2072     cl_uint value [1];
2073 root 1.22 NEED_SUCCESS (GetProgramInfo, (self, ix, sizeof (value), value, 0));
2074 root 1.14 EXTEND (SP, 1);
2075     const int i = 0;
2076     PUSHs (sv_2mortal (newSVuv (value [i])));
2077    
2078     void
2079 root 1.22 context (OpenCL::Program self)
2080 root 1.14 PPCODE:
2081     cl_context value [1];
2082 root 1.22 NEED_SUCCESS (GetProgramInfo, (self, CL_PROGRAM_CONTEXT, sizeof (value), value, 0));
2083 root 1.14 EXTEND (SP, 1);
2084     const int i = 0;
2085     {
2086     NEED_SUCCESS (RetainContext, (value [i]));
2087 root 1.51 PUSH_CLOBJ ("OpenCL::Context", value [i]);
2088 root 1.14 }
2089    
2090     void
2091 root 1.22 devices (OpenCL::Program self)
2092 root 1.14 PPCODE:
2093     size_t size;
2094 root 1.22 NEED_SUCCESS (GetProgramInfo, (self, CL_PROGRAM_DEVICES, 0, 0, &size));
2095 root 1.14 cl_device_id *value = tmpbuf (size);
2096 root 1.22 NEED_SUCCESS (GetProgramInfo, (self, CL_PROGRAM_DEVICES, size, value, 0));
2097 root 1.15 int i, n = size / sizeof (*value);
2098 root 1.14 EXTEND (SP, n);
2099     for (i = 0; i < n; ++i)
2100     {
2101 root 1.51 PUSH_CLOBJ ("OpenCL::Device", value [i]);
2102 root 1.14 }
2103    
2104     void
2105 root 1.22 source (OpenCL::Program self)
2106 root 1.14 PPCODE:
2107     size_t size;
2108 root 1.22 NEED_SUCCESS (GetProgramInfo, (self, CL_PROGRAM_SOURCE, 0, 0, &size));
2109 root 1.14 char *value = tmpbuf (size);
2110 root 1.22 NEED_SUCCESS (GetProgramInfo, (self, CL_PROGRAM_SOURCE, size, value, 0));
2111 root 1.16 EXTEND (SP, 1);
2112     const int i = 0;
2113 root 1.14 PUSHs (sv_2mortal (newSVpv (value, 0)));
2114    
2115     void
2116 root 1.22 binary_sizes (OpenCL::Program self)
2117 root 1.14 PPCODE:
2118     size_t size;
2119 root 1.22 NEED_SUCCESS (GetProgramInfo, (self, CL_PROGRAM_BINARY_SIZES, 0, 0, &size));
2120 root 1.14 size_t *value = tmpbuf (size);
2121 root 1.22 NEED_SUCCESS (GetProgramInfo, (self, CL_PROGRAM_BINARY_SIZES, size, value, 0));
2122 root 1.15 int i, n = size / sizeof (*value);
2123 root 1.14 EXTEND (SP, n);
2124     for (i = 0; i < n; ++i)
2125     PUSHs (sv_2mortal (newSVuv (value [i])));
2126    
2127     #END:program
2128    
2129 root 1.2 MODULE = OpenCL PACKAGE = OpenCL::Kernel
2130    
2131     void
2132 root 1.22 DESTROY (OpenCL::Kernel self)
2133 root 1.2 CODE:
2134 root 1.22 clReleaseKernel (self);
2135 root 1.2
2136     void
2137 root 1.22 set_char (OpenCL::Kernel self, cl_uint idx, cl_char value)
2138 root 1.3 CODE:
2139 root 1.22 clSetKernelArg (self, idx, sizeof (value), &value);
2140 root 1.3
2141     void
2142 root 1.22 set_uchar (OpenCL::Kernel self, cl_uint idx, cl_uchar value)
2143 root 1.3 CODE:
2144 root 1.22 clSetKernelArg (self, idx, sizeof (value), &value);
2145 root 1.3
2146     void
2147 root 1.22 set_short (OpenCL::Kernel self, cl_uint idx, cl_short value)
2148 root 1.3 CODE:
2149 root 1.22 clSetKernelArg (self, idx, sizeof (value), &value);
2150 root 1.3
2151     void
2152 root 1.22 set_ushort (OpenCL::Kernel self, cl_uint idx, cl_ushort value)
2153 root 1.3 CODE:
2154 root 1.22 clSetKernelArg (self, idx, sizeof (value), &value);
2155 root 1.3
2156     void
2157 root 1.22 set_int (OpenCL::Kernel self, cl_uint idx, cl_int value)
2158 root 1.3 CODE:
2159 root 1.22 clSetKernelArg (self, idx, sizeof (value), &value);
2160 root 1.3
2161     void
2162 root 1.22 set_uint (OpenCL::Kernel self, cl_uint idx, cl_uint value)
2163 root 1.3 CODE:
2164 root 1.22 clSetKernelArg (self, idx, sizeof (value), &value);
2165 root 1.3
2166     void
2167 root 1.22 set_long (OpenCL::Kernel self, cl_uint idx, cl_long value)
2168 root 1.3 CODE:
2169 root 1.22 clSetKernelArg (self, idx, sizeof (value), &value);
2170 root 1.3
2171     void
2172 root 1.22 set_ulong (OpenCL::Kernel self, cl_uint idx, cl_ulong value)
2173 root 1.3 CODE:
2174 root 1.22 clSetKernelArg (self, idx, sizeof (value), &value);
2175 root 1.3
2176     void
2177 root 1.22 set_half (OpenCL::Kernel self, cl_uint idx, cl_half value)
2178 root 1.3 CODE:
2179 root 1.22 clSetKernelArg (self, idx, sizeof (value), &value);
2180 root 1.3
2181     void
2182 root 1.22 set_float (OpenCL::Kernel self, cl_uint idx, cl_float value)
2183 root 1.3 CODE:
2184 root 1.22 clSetKernelArg (self, idx, sizeof (value), &value);
2185 root 1.3
2186     void
2187 root 1.22 set_double (OpenCL::Kernel self, cl_uint idx, cl_double value)
2188 root 1.5 CODE:
2189 root 1.22 clSetKernelArg (self, idx, sizeof (value), &value);
2190 root 1.5
2191     void
2192 root 1.22 set_memory (OpenCL::Kernel self, cl_uint idx, OpenCL::Memory_ornull value)
2193 root 1.3 CODE:
2194 root 1.22 clSetKernelArg (self, idx, sizeof (value), &value);
2195 root 1.3
2196     void
2197 root 1.22 set_buffer (OpenCL::Kernel self, cl_uint idx, OpenCL::Buffer_ornull value)
2198 root 1.3 CODE:
2199 root 1.22 clSetKernelArg (self, idx, sizeof (value), &value);
2200 root 1.3
2201     void
2202 root 1.22 set_image2d (OpenCL::Kernel self, cl_uint idx, OpenCL::Image2D_ornull value)
2203 root 1.3 CODE:
2204 root 1.22 clSetKernelArg (self, idx, sizeof (value), &value);
2205 root 1.3
2206     void
2207 root 1.22 set_image3d (OpenCL::Kernel self, cl_uint idx, OpenCL::Image3D_ornull value)
2208 root 1.3 CODE:
2209 root 1.22 clSetKernelArg (self, idx, sizeof (value), &value);
2210 root 1.3
2211     void
2212 root 1.22 set_sampler (OpenCL::Kernel self, cl_uint idx, OpenCL::Sampler value)
2213 root 1.3 CODE:
2214 root 1.22 clSetKernelArg (self, idx, sizeof (value), &value);
2215 root 1.3
2216     void
2217 root 1.33 set_local (OpenCL::Kernel self, cl_uint idx, size_t size)
2218     CODE:
2219     clSetKernelArg (self, idx, size, 0);
2220    
2221     void
2222 root 1.22 set_event (OpenCL::Kernel self, cl_uint idx, OpenCL::Event value)
2223 root 1.2 CODE:
2224 root 1.22 clSetKernelArg (self, idx, sizeof (value), &value);
2225 root 1.2
2226 root 1.14 void
2227 root 1.22 info (OpenCL::Kernel self, cl_kernel_info name)
2228 root 1.14 PPCODE:
2229     INFO (Kernel)
2230    
2231     #BEGIN:kernel
2232    
2233     void
2234 root 1.22 function_name (OpenCL::Kernel self)
2235 root 1.14 PPCODE:
2236     size_t size;
2237 root 1.22 NEED_SUCCESS (GetKernelInfo, (self, CL_KERNEL_FUNCTION_NAME, 0, 0, &size));
2238 root 1.14 char *value = tmpbuf (size);
2239 root 1.22 NEED_SUCCESS (GetKernelInfo, (self, CL_KERNEL_FUNCTION_NAME, size, value, 0));
2240 root 1.16 EXTEND (SP, 1);
2241     const int i = 0;
2242 root 1.14 PUSHs (sv_2mortal (newSVpv (value, 0)));
2243    
2244     void
2245 root 1.22 num_args (OpenCL::Kernel self)
2246 root 1.16 ALIAS:
2247     num_args = CL_KERNEL_NUM_ARGS
2248     reference_count = CL_KERNEL_REFERENCE_COUNT
2249 root 1.14 PPCODE:
2250     cl_uint value [1];
2251 root 1.22 NEED_SUCCESS (GetKernelInfo, (self, ix, sizeof (value), value, 0));
2252 root 1.14 EXTEND (SP, 1);
2253     const int i = 0;
2254     PUSHs (sv_2mortal (newSVuv (value [i])));
2255    
2256     void
2257 root 1.22 context (OpenCL::Kernel self)
2258 root 1.14 PPCODE:
2259     cl_context value [1];
2260 root 1.22 NEED_SUCCESS (GetKernelInfo, (self, CL_KERNEL_CONTEXT, sizeof (value), value, 0));
2261 root 1.14 EXTEND (SP, 1);
2262     const int i = 0;
2263     {
2264     NEED_SUCCESS (RetainContext, (value [i]));
2265 root 1.51 PUSH_CLOBJ ("OpenCL::Context", value [i]);
2266 root 1.14 }
2267    
2268     void
2269 root 1.22 program (OpenCL::Kernel self)
2270 root 1.14 PPCODE:
2271     cl_program value [1];
2272 root 1.22 NEED_SUCCESS (GetKernelInfo, (self, CL_KERNEL_PROGRAM, sizeof (value), value, 0));
2273 root 1.14 EXTEND (SP, 1);
2274     const int i = 0;
2275     {
2276     NEED_SUCCESS (RetainProgram, (value [i]));
2277 root 1.51 PUSH_CLOBJ ("OpenCL::Program", value [i]);
2278 root 1.14 }
2279    
2280     #END:kernel
2281    
2282     void
2283 root 1.22 work_group_info (OpenCL::Kernel self, OpenCL::Device device, cl_kernel_work_group_info name)
2284 root 1.14 PPCODE:
2285 root 1.22 size_t size;
2286     NEED_SUCCESS (GetKernelWorkGroupInfo, (self, device, name, 0, 0, &size));
2287 root 1.14 SV *sv = sv_2mortal (newSV (size));
2288     SvUPGRADE (sv, SVt_PV);
2289     SvPOK_only (sv);
2290     SvCUR_set (sv, size);
2291 root 1.22 NEED_SUCCESS (GetKernelWorkGroupInfo, (self, device, name, size, SvPVX (sv), 0));
2292 root 1.14 XPUSHs (sv);
2293    
2294     #BEGIN:kernel_work_group
2295    
2296     void
2297 root 1.22 work_group_size (OpenCL::Kernel self, OpenCL::Device device)
2298 root 1.16 ALIAS:
2299     work_group_size = CL_KERNEL_WORK_GROUP_SIZE
2300     preferred_work_group_size_multiple = CL_KERNEL_PREFERRED_WORK_GROUP_SIZE_MULTIPLE
2301 root 1.14 PPCODE:
2302     size_t value [1];
2303 root 1.22 NEED_SUCCESS (GetKernelWorkGroupInfo, (self, device, ix, sizeof (value), value, 0));
2304 root 1.14 EXTEND (SP, 1);
2305     const int i = 0;
2306     PUSHs (sv_2mortal (newSVuv (value [i])));
2307    
2308     void
2309 root 1.22 compile_work_group_size (OpenCL::Kernel self, OpenCL::Device device)
2310 root 1.14 PPCODE:
2311     size_t size;
2312 root 1.22 NEED_SUCCESS (GetKernelWorkGroupInfo, (self, device, CL_KERNEL_COMPILE_WORK_GROUP_SIZE, 0, 0, &size));
2313 root 1.14 size_t *value = tmpbuf (size);
2314 root 1.22 NEED_SUCCESS (GetKernelWorkGroupInfo, (self, device, CL_KERNEL_COMPILE_WORK_GROUP_SIZE, size, value, 0));
2315 root 1.15 int i, n = size / sizeof (*value);
2316 root 1.14 EXTEND (SP, n);
2317     for (i = 0; i < n; ++i)
2318     PUSHs (sv_2mortal (newSVuv (value [i])));
2319    
2320     void
2321 root 1.22 local_mem_size (OpenCL::Kernel self, OpenCL::Device device)
2322 root 1.16 ALIAS:
2323     local_mem_size = CL_KERNEL_LOCAL_MEM_SIZE
2324     private_mem_size = CL_KERNEL_PRIVATE_MEM_SIZE
2325 root 1.14 PPCODE:
2326     cl_ulong value [1];
2327 root 1.22 NEED_SUCCESS (GetKernelWorkGroupInfo, (self, device, ix, sizeof (value), value, 0));
2328 root 1.14 EXTEND (SP, 1);
2329     const int i = 0;
2330     PUSHs (sv_2mortal (newSVuv (value [i])));
2331    
2332     #END:kernel_work_group
2333    
2334 root 1.2 MODULE = OpenCL PACKAGE = OpenCL::Event
2335    
2336     void
2337 root 1.22 DESTROY (OpenCL::Event self)
2338 root 1.2 CODE:
2339 root 1.22 clReleaseEvent (self);
2340 root 1.2
2341     void
2342 root 1.22 wait (OpenCL::Event self)
2343 root 1.14 CODE:
2344 root 1.22 clWaitForEvents (1, &self);
2345 root 1.14
2346     void
2347 root 1.51 cb (OpenCL::Event self, cl_int command_exec_callback_type, SV *cb)
2348     CODE:
2349     clSetEventCallback (self, command_exec_callback_type, eq_event_notify, SvREFCNT_inc (s_get_cv (cb)));
2350    
2351     void
2352 root 1.22 info (OpenCL::Event self, cl_event_info name)
2353 root 1.2 PPCODE:
2354     INFO (Event)
2355    
2356 root 1.14 #BEGIN:event
2357    
2358     void
2359 root 1.22 command_queue (OpenCL::Event self)
2360 root 1.14 PPCODE:
2361     cl_command_queue value [1];
2362 root 1.22 NEED_SUCCESS (GetEventInfo, (self, CL_EVENT_COMMAND_QUEUE, sizeof (value), value, 0));
2363 root 1.14 EXTEND (SP, 1);
2364     const int i = 0;
2365     {
2366     NEED_SUCCESS (RetainCommandQueue, (value [i]));
2367 root 1.51 PUSH_CLOBJ ("OpenCL::Queue", value [i]);
2368 root 1.14 }
2369    
2370     void
2371 root 1.22 command_type (OpenCL::Event self)
2372 root 1.14 PPCODE:
2373     cl_command_type value [1];
2374 root 1.22 NEED_SUCCESS (GetEventInfo, (self, CL_EVENT_COMMAND_TYPE, sizeof (value), value, 0));
2375 root 1.14 EXTEND (SP, 1);
2376     const int i = 0;
2377     PUSHs (sv_2mortal (newSVuv (value [i])));
2378    
2379     void
2380 root 1.22 reference_count (OpenCL::Event self)
2381 root 1.16 ALIAS:
2382     reference_count = CL_EVENT_REFERENCE_COUNT
2383     command_execution_status = CL_EVENT_COMMAND_EXECUTION_STATUS
2384 root 1.14 PPCODE:
2385     cl_uint value [1];
2386 root 1.22 NEED_SUCCESS (GetEventInfo, (self, ix, sizeof (value), value, 0));
2387 root 1.14 EXTEND (SP, 1);
2388     const int i = 0;
2389     PUSHs (sv_2mortal (newSVuv (value [i])));
2390    
2391     void
2392 root 1.22 context (OpenCL::Event self)
2393 root 1.14 PPCODE:
2394     cl_context value [1];
2395 root 1.22 NEED_SUCCESS (GetEventInfo, (self, CL_EVENT_CONTEXT, sizeof (value), value, 0));
2396 root 1.14 EXTEND (SP, 1);
2397     const int i = 0;
2398     {
2399     NEED_SUCCESS (RetainContext, (value [i]));
2400 root 1.51 PUSH_CLOBJ ("OpenCL::Context", value [i]);
2401 root 1.14 }
2402    
2403     #END:event
2404    
2405 root 1.2 void
2406 root 1.22 profiling_info (OpenCL::Event self, cl_profiling_info name)
2407 root 1.13 PPCODE:
2408     INFO (EventProfiling)
2409    
2410 root 1.14 #BEGIN:profiling
2411    
2412 root 1.13 void
2413 root 1.22 profiling_command_queued (OpenCL::Event self)
2414 root 1.16 ALIAS:
2415     profiling_command_queued = CL_PROFILING_COMMAND_QUEUED
2416     profiling_command_submit = CL_PROFILING_COMMAND_SUBMIT
2417     profiling_command_start = CL_PROFILING_COMMAND_START
2418     profiling_command_end = CL_PROFILING_COMMAND_END
2419 root 1.14 PPCODE:
2420     cl_ulong value [1];
2421 root 1.22 NEED_SUCCESS (GetEventProfilingInfo, (self, ix, sizeof (value), value, 0));
2422 root 1.14 EXTEND (SP, 1);
2423     const int i = 0;
2424     PUSHs (sv_2mortal (newSVuv (value [i])));
2425    
2426     #END:profiling
2427 root 1.2
2428 root 1.5 MODULE = OpenCL PACKAGE = OpenCL::UserEvent
2429    
2430     void
2431 root 1.22 set_status (OpenCL::UserEvent self, cl_int execution_status)
2432 root 1.5 CODE:
2433 root 1.22 clSetUserEventStatus (self, execution_status);
2434 root 1.5