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