ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/OpenCL/OpenCL.xs
Revision: 1.64
Committed: Thu May 3 23:30:08 2012 UTC (12 years ago) by root
Branch: MAIN
CVS Tags: rel-0_99
Changes since 1.63: +114 -24 lines
Log Message:
*** empty log message ***

File Contents

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