ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/OpenCL/OpenCL.xs
Revision: 1.80
Committed: Sat May 5 16:44:02 2012 UTC (12 years ago) by root
Branch: MAIN
CVS Tags: rel-1_0
Changes since 1.79: +100 -18 lines
Log Message:
*** empty log message ***

File Contents

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