ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/OpenCL/OpenCL.xs
Revision: 1.63
Committed: Tue May 1 22:25:13 2012 UTC (12 years ago) by root
Branch: MAIN
Changes since 1.62: +11 -0 lines
Log Message:
*** empty log message ***

File Contents

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