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