ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/OpenCL/OpenCL.xs
Revision: 1.79
Committed: Sat May 5 15:43:02 2012 UTC (12 years ago) by root
Branch: MAIN
Changes since 1.78: +64 -186 lines
Log Message:
*** empty log message ***

File Contents

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