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