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