1 | #include "EXTERN.h" |
1 | #include "EXTERN.h" |
2 | #include "perl.h" |
2 | #include "perl.h" |
3 | #include "XSUB.h" |
3 | #include "XSUB.h" |
|
|
4 | |
|
|
5 | #ifdef I_DLFCN |
|
|
6 | #include <dlfcn.h> |
|
|
7 | #endif |
|
|
8 | |
|
|
9 | // how stupid is that, the 1.2 header files define CL_VERSION_1_1, |
|
|
10 | // but then fail to define the api functions unless you ALSO define |
|
|
11 | // this. This breaks 100% of the opencl 1.1 apps, for what reason? |
|
|
12 | // after all, the functions are deprecated, not removed. |
|
|
13 | // in addition, you cannot test for this in any future-proof way. |
|
|
14 | // each time a new opencl version comes out, you need to make a new |
|
|
15 | // release. |
|
|
16 | #define CL_USE_DEPRECATED_OPENCL_1_1_APIS |
|
|
17 | #define CL_USE_DEPRECATED_OPENCL_1_2_APIS /* just guessing, you stupid idiots */ |
4 | |
18 | |
5 | #ifdef __APPLE__ |
19 | #ifdef __APPLE__ |
6 | #include <OpenCL/opencl.h> |
20 | #include <OpenCL/opencl.h> |
7 | #else |
21 | #else |
8 | #include <CL/opencl.h> |
22 | #include <CL/opencl.h> |
|
|
23 | #endif |
|
|
24 | |
|
|
25 | #if 0 // testing |
|
|
26 | #undef CL_USE_DEPRECATED_OPENCL_1_1_APIS |
|
|
27 | #undef CL_VERSION_1_2 |
9 | #endif |
28 | #endif |
10 | |
29 | |
11 | typedef cl_platform_id OpenCL__Platform; |
30 | typedef cl_platform_id OpenCL__Platform; |
12 | typedef cl_device_id OpenCL__Device; |
31 | typedef cl_device_id OpenCL__Device; |
13 | typedef cl_context OpenCL__Context; |
32 | typedef cl_context OpenCL__Context; |
… | |
… | |
31 | |
50 | |
32 | typedef SV *FUTURE; |
51 | typedef SV *FUTURE; |
33 | |
52 | |
34 | /*****************************************************************************/ |
53 | /*****************************************************************************/ |
35 | |
54 | |
|
|
55 | // name must include a leading underscore |
|
|
56 | // all of this horrors would be unneceesary if somebody wrote a proper OpenGL module |
|
|
57 | // for perl. doh. |
|
|
58 | static void * |
|
|
59 | glsym (const char *name) |
|
|
60 | { |
|
|
61 | void *fun = 0; |
|
|
62 | |
|
|
63 | #if defined I_DLFCN && defined RTLD_DEFAULT |
|
|
64 | fun = dlsym (RTLD_DEFAULT, name + 1); |
|
|
65 | if (!fun) fun = dlsym (RTLD_DEFAULT, name); |
|
|
66 | |
|
|
67 | if (!fun) |
|
|
68 | { |
|
|
69 | static void *libgl; |
|
|
70 | static const char *glso[] = { |
|
|
71 | "libGL.so.1", |
|
|
72 | "libGL.so.3", |
|
|
73 | "libGL.so.4.0", |
|
|
74 | "libGL.so", |
|
|
75 | "/usr/lib/libGL.so", |
|
|
76 | "/usr/X11R6/lib/libGL.1.dylib" |
|
|
77 | }; |
|
|
78 | int i; |
|
|
79 | |
|
|
80 | for (i = 0; !libgl && i < sizeof (glso) / sizeof (glso [0]); ++i) |
|
|
81 | { |
|
|
82 | libgl = dlopen (glso [i], RTLD_LAZY); |
|
|
83 | if (libgl) |
|
|
84 | break; |
|
|
85 | } |
|
|
86 | |
|
|
87 | if (libgl) |
|
|
88 | { |
|
|
89 | fun = dlsym (libgl, name + 1); |
|
|
90 | if (!fun) fun = dlsym (libgl, name); |
|
|
91 | } |
|
|
92 | } |
|
|
93 | #endif |
|
|
94 | |
|
|
95 | return fun; |
|
|
96 | } |
|
|
97 | |
|
|
98 | /*****************************************************************************/ |
|
|
99 | |
36 | /* up to two temporary buffers */ |
100 | /* up to two temporary buffers */ |
37 | static void * |
101 | static void * |
38 | tmpbuf (size_t size) |
102 | tmpbuf (size_t size) |
39 | { |
103 | { |
|
|
104 | enum { buffers = 3 }; |
40 | static int idx; |
105 | static int idx; |
41 | static void *buf [2]; |
106 | static void *buf [buffers]; |
42 | static size_t len [2]; |
107 | static size_t len [buffers]; |
43 | |
108 | |
44 | idx ^= 1; |
109 | idx = (idx + 1) % buffers; |
45 | |
110 | |
46 | if (len [idx] < size) |
111 | if (len [idx] < size) |
47 | { |
112 | { |
48 | free (buf [idx]); |
113 | free (buf [idx]); |
49 | len [idx] = ((size + 31) & ~4095) + 4096 - 32; |
114 | len [idx] = ((size + 31) & ~4095) + 4096 - 32; |
… | |
… | |
131 | croak ("%s: %s is not of type %s", func, svname, pkg); |
196 | croak ("%s: %s is not of type %s", func, svname, pkg); |
132 | } |
197 | } |
133 | |
198 | |
134 | /*****************************************************************************/ |
199 | /*****************************************************************************/ |
135 | |
200 | |
|
|
201 | static cl_context_properties * |
|
|
202 | SvCONTEXTPROPERTIES (const char *func, const char *svname, SV *sv, cl_context_properties *extra, int extracount) |
|
|
203 | { |
|
|
204 | if (!sv || !SvOK (sv)) |
|
|
205 | if (extra) |
|
|
206 | sv = sv_2mortal (newRV_noinc ((SV *)newAV ())); // slow, but rarely used hopefully |
|
|
207 | else |
|
|
208 | return 0; |
|
|
209 | |
|
|
210 | if (SvROK (sv) && SvTYPE (SvRV (sv)) == SVt_PVAV) |
|
|
211 | { |
|
|
212 | AV *av = (AV *)SvRV (sv); |
|
|
213 | int i, len = av_len (av) + 1; |
|
|
214 | cl_context_properties *p = tmpbuf (sizeof (cl_context_properties) * (len + extracount + 1)); |
|
|
215 | cl_context_properties *l = p; |
|
|
216 | |
|
|
217 | if (len & 1) |
|
|
218 | croak ("%s: %s is not a property list (must be even number of elements)", func, svname); |
|
|
219 | |
|
|
220 | while (extracount--) |
|
|
221 | *l++ = *extra++; |
|
|
222 | |
|
|
223 | for (i = 0; i < len; i += 2) |
|
|
224 | { |
|
|
225 | cl_context_properties t = SvIV (*av_fetch (av, i , 0)); |
|
|
226 | SV *p_sv = *av_fetch (av, i + 1, 0); |
|
|
227 | cl_context_properties v = SvIV (p_sv); // code below can override |
|
|
228 | |
|
|
229 | switch (t) |
|
|
230 | { |
|
|
231 | case CL_GLX_DISPLAY_KHR: |
|
|
232 | if (!SvOK (p_sv)) |
|
|
233 | { |
|
|
234 | void *func = glsym ("_glXGetCurrentDisplay"); |
|
|
235 | if (func) |
|
|
236 | v = (cl_context_properties)((void *(*)(void))func)(); |
|
|
237 | } |
|
|
238 | break; |
|
|
239 | |
|
|
240 | case CL_GL_CONTEXT_KHR: |
|
|
241 | if (!SvOK (p_sv)) |
|
|
242 | { |
|
|
243 | void *func = glsym ("_glXGetCurrentContext"); |
|
|
244 | if (func) |
|
|
245 | v = (cl_context_properties)((void *(*)(void))func)(); |
|
|
246 | } |
|
|
247 | break; |
|
|
248 | |
|
|
249 | default: |
|
|
250 | /* unknown property, treat as int */ |
|
|
251 | break; |
|
|
252 | } |
|
|
253 | |
|
|
254 | *l++ = t; |
|
|
255 | *l++ = v; |
|
|
256 | } |
|
|
257 | |
|
|
258 | *l = 0; |
|
|
259 | |
|
|
260 | return p; |
|
|
261 | } |
|
|
262 | |
|
|
263 | croak ("%s: %s is not a property list (either undef or [type => value, ...])", func, svname); |
|
|
264 | } |
|
|
265 | |
|
|
266 | /*****************************************************************************/ |
|
|
267 | |
136 | static size_t |
268 | static size_t |
137 | img_row_pitch (cl_mem img) |
269 | img_row_pitch (cl_mem img) |
138 | { |
270 | { |
139 | size_t res; |
271 | size_t res; |
140 | clGetImageInfo (img, CL_IMAGE_ROW_PITCH, sizeof (res), &res, 0); |
272 | clGetImageInfo (img, CL_IMAGE_ROW_PITCH, sizeof (res), &res, 0); |
141 | return res; |
273 | return res; |
142 | } |
274 | } |
143 | |
275 | |
144 | static cl_event * |
276 | static cl_event * |
145 | event_list (SV **items, int count) |
277 | event_list (SV **items, cl_uint *rcount) |
146 | { |
278 | { |
|
|
279 | cl_uint count = *rcount; |
|
|
280 | |
147 | if (!count) |
281 | if (!count) |
148 | return 0; |
282 | return 0; |
149 | |
283 | |
150 | cl_event *list = tmpbuf (sizeof (cl_event) * count); |
284 | cl_event *list = tmpbuf (sizeof (cl_event) * count); |
|
|
285 | int i = 0; |
151 | |
286 | |
152 | while (count--) |
287 | do |
|
|
288 | { |
|
|
289 | --count; |
|
|
290 | if (SvOK (items [count])) |
153 | list [count] = SvPTROBJ ("clEnqueue", "wait_events", items [count], "OpenCL::Event"); |
291 | list [i++] = SvPTROBJ ("clEnqueue", "wait_events", items [count], "OpenCL::Event"); |
|
|
292 | } |
|
|
293 | while (count); |
154 | |
294 | |
|
|
295 | *rcount = i; |
|
|
296 | |
155 | return list; |
297 | return i ? list : 0; |
156 | } |
298 | } |
157 | |
299 | |
158 | #define EVENT_LIST(items,count) \ |
300 | #define EVENT_LIST(items,count) \ |
159 | cl_uint event_list_count = (count); \ |
301 | cl_uint event_list_count = (count); \ |
160 | cl_event *event_list_ptr = event_list (&ST (items), event_list_count) |
302 | cl_event *event_list_ptr = event_list (&ST (items), &event_list_count) |
161 | |
303 | |
162 | #define INFO(class) \ |
304 | #define INFO(class) \ |
163 | { \ |
305 | { \ |
164 | size_t size; \ |
306 | size_t size; \ |
165 | NEED_SUCCESS (Get ## class ## Info, (self, name, 0, 0, &size)); \ |
307 | NEED_SUCCESS (Get ## class ## Info, (self, name, 0, 0, &size)); \ |
… | |
… | |
175 | |
317 | |
176 | PROTOTYPES: ENABLE |
318 | PROTOTYPES: ENABLE |
177 | |
319 | |
178 | BOOT: |
320 | BOOT: |
179 | { |
321 | { |
180 | HV *stash = gv_stashpv ("OpenCL", 1); |
322 | HV *stash = gv_stashpv ("OpenCL", 1); |
181 | static const ivstr *civ, const_iv[] = { |
323 | static const ivstr *civ, const_iv[] = { |
182 | { sizeof (cl_char ), "SIZEOF_CHAR" }, |
324 | { sizeof (cl_char ), "SIZEOF_CHAR" }, |
183 | { sizeof (cl_uchar ), "SIZEOF_UCHAR" }, |
325 | { sizeof (cl_uchar ), "SIZEOF_UCHAR" }, |
184 | { sizeof (cl_short ), "SIZEOF_SHORT" }, |
326 | { sizeof (cl_short ), "SIZEOF_SHORT" }, |
185 | { sizeof (cl_ushort), "SIZEOF_USHORT" }, |
327 | { sizeof (cl_ushort), "SIZEOF_USHORT" }, |
186 | { sizeof (cl_int ), "SIZEOF_INT" }, |
328 | { sizeof (cl_int ), "SIZEOF_INT" }, |
187 | { sizeof (cl_uint ), "SIZEOF_UINT" }, |
329 | { sizeof (cl_uint ), "SIZEOF_UINT" }, |
188 | { sizeof (cl_long ), "SIZEOF_LONG" }, |
330 | { sizeof (cl_long ), "SIZEOF_LONG" }, |
189 | { sizeof (cl_ulong ), "SIZEOF_ULONG" }, |
331 | { sizeof (cl_ulong ), "SIZEOF_ULONG" }, |
190 | { sizeof (cl_half ), "SIZEOF_HALF" }, |
332 | { sizeof (cl_half ), "SIZEOF_HALF" }, |
191 | { sizeof (cl_float ), "SIZEOF_FLOAT" }, |
333 | { sizeof (cl_float ), "SIZEOF_FLOAT" }, |
192 | { sizeof (cl_double), "SIZEOF_DOUBLE" }, |
334 | { sizeof (cl_double), "SIZEOF_DOUBLE" }, |
193 | #include "constiv.h" |
335 | #include "constiv.h" |
194 | }; |
336 | }; |
195 | for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ > const_iv; civ--) |
337 | for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ > const_iv; civ--) |
196 | newCONSTSUB (stash, (char *)civ[-1].name, newSViv (civ[-1].iv)); |
338 | newCONSTSUB (stash, (char *)civ[-1].name, newSViv (civ[-1].iv)); |
197 | } |
339 | } |
198 | |
340 | |
199 | cl_int |
341 | cl_int |
200 | errno () |
342 | errno () |
201 | CODE: |
343 | CODE: |
202 | errno = res; |
344 | RETVAL = res; |
|
|
345 | OUTPUT: |
|
|
346 | RETVAL |
203 | |
347 | |
204 | const char * |
348 | const char * |
205 | err2str (cl_int err) |
349 | err2str (cl_int err) |
206 | |
350 | |
207 | const char * |
351 | const char * |
… | |
… | |
221 | EXTEND (SP, count); |
365 | EXTEND (SP, count); |
222 | for (i = 0; i < count; ++i) |
366 | for (i = 0; i < count; ++i) |
223 | PUSHs (NEW_MORTAL_OBJ ("OpenCL::Platform", list [i])); |
367 | PUSHs (NEW_MORTAL_OBJ ("OpenCL::Platform", list [i])); |
224 | |
368 | |
225 | void |
369 | void |
226 | context_from_type (FUTURE properties = 0, cl_device_type type = CL_DEVICE_TYPE_DEFAULT, FUTURE notify = 0) |
370 | context_from_type (cl_context_properties *properties = 0, cl_device_type type = CL_DEVICE_TYPE_DEFAULT, FUTURE notify = 0) |
227 | PPCODE: |
371 | PPCODE: |
228 | NEED_SUCCESS_ARG (cl_context ctx, CreateContextFromType, (0, type, 0, 0, &res)); |
372 | NEED_SUCCESS_ARG (cl_context ctx, CreateContextFromType, (properties, type, 0, 0, &res)); |
229 | XPUSH_NEW_OBJ ("OpenCL::Context", ctx); |
373 | XPUSH_NEW_OBJ ("OpenCL::Context", ctx); |
230 | |
374 | |
|
|
375 | #if 0 |
|
|
376 | |
231 | void |
377 | void |
232 | context (FUTURE properties, FUTURE devices, FUTURE notify = 0) |
378 | context (cl_context_properties *properties = 0, FUTURE devices, FUTURE notify = 0) |
233 | PPCODE: |
379 | PPCODE: |
234 | /* der Gipfel der Kunst */ |
380 | /* der Gipfel der Kunst */ |
|
|
381 | |
|
|
382 | #endif |
235 | |
383 | |
236 | void |
384 | void |
237 | wait_for_events (...) |
385 | wait_for_events (...) |
238 | CODE: |
386 | CODE: |
239 | EVENT_LIST (0, items); |
387 | EVENT_LIST (0, items); |
… | |
… | |
283 | EXTEND (SP, count); |
431 | EXTEND (SP, count); |
284 | for (i = 0; i < count; ++i) |
432 | for (i = 0; i < count; ++i) |
285 | PUSHs (sv_setref_pv (sv_newmortal (), "OpenCL::Device", list [i])); |
433 | PUSHs (sv_setref_pv (sv_newmortal (), "OpenCL::Device", list [i])); |
286 | |
434 | |
287 | void |
435 | void |
288 | context (OpenCL::Platform self, FUTURE properties, SV *devices, FUTURE notify = 0) |
436 | context (OpenCL::Platform self, cl_context_properties *properties = 0, SV *devices, FUTURE notify = 0) |
289 | PPCODE: |
437 | PPCODE: |
290 | if (!SvROK (devices) || SvTYPE (SvRV (devices)) != SVt_PVAV) |
438 | if (!SvROK (devices) || SvTYPE (SvRV (devices)) != SVt_PVAV) |
291 | croak ("OpenCL::Platform argument 'device' must be an arrayref with device objects, in call"); |
439 | croak ("OpenCL::Platform::context argument 'device' must be an arrayref with device objects, in call"); |
292 | |
440 | |
293 | AV *av = (AV *)SvRV (devices); |
441 | AV *av = (AV *)SvRV (devices); |
294 | cl_uint num_devices = av_len (av) + 1; |
442 | cl_uint num_devices = av_len (av) + 1; |
295 | cl_device_id *device_list = tmpbuf (sizeof (cl_device_id) * num_devices); |
443 | cl_device_id *device_list = tmpbuf (sizeof (cl_device_id) * num_devices); |
296 | int i; |
444 | int i; |
297 | |
445 | |
298 | for (i = num_devices; i--; ) |
446 | for (i = num_devices; i--; ) |
299 | device_list [i] = SvPTROBJ ("clCreateContext", "devices", *av_fetch (av, i, 0), "OpenCL::Device"); |
447 | device_list [i] = SvPTROBJ ("clCreateContext", "devices", *av_fetch (av, i, 0), "OpenCL::Device"); |
300 | |
448 | |
301 | NEED_SUCCESS_ARG (cl_context ctx, CreateContext, (0, num_devices, device_list, 0, 0, &res)); |
449 | NEED_SUCCESS_ARG (cl_context ctx, CreateContext, (properties, num_devices, device_list, 0, 0, &res)); |
302 | XPUSH_NEW_OBJ ("OpenCL::Context", ctx); |
450 | XPUSH_NEW_OBJ ("OpenCL::Context", ctx); |
303 | |
451 | |
304 | void |
452 | void |
305 | context_from_type (OpenCL::Platform self, FUTURE properties = 0, cl_device_type type = CL_DEVICE_TYPE_DEFAULT, FUTURE notify = 0) |
453 | context_from_type (OpenCL::Platform self, SV *properties = 0, cl_device_type type = CL_DEVICE_TYPE_DEFAULT, FUTURE notify = 0) |
306 | PPCODE: |
454 | PPCODE: |
307 | cl_context_properties props[] = { CL_CONTEXT_PLATFORM, (cl_context_properties)self, 0 }; |
455 | cl_context_properties extra[] = { CL_CONTEXT_PLATFORM, (cl_context_properties)self }; |
|
|
456 | cl_context_properties *props = SvCONTEXTPROPERTIES ("OpenCL::Platform::context_from_type", "properties", properties, extra, 2); |
308 | NEED_SUCCESS_ARG (cl_context ctx, CreateContextFromType, (props, type, 0, 0, &res)); |
457 | NEED_SUCCESS_ARG (cl_context ctx, CreateContextFromType, (props, type, 0, 0, &res)); |
309 | XPUSH_NEW_OBJ ("OpenCL::Context", ctx); |
458 | XPUSH_NEW_OBJ ("OpenCL::Context", ctx); |
310 | |
459 | |
311 | MODULE = OpenCL PACKAGE = OpenCL::Device |
460 | MODULE = OpenCL PACKAGE = OpenCL::Device |
312 | |
461 | |
… | |
… | |
565 | |
714 | |
566 | void |
715 | void |
567 | buffer (OpenCL::Context self, cl_mem_flags flags, size_t len) |
716 | buffer (OpenCL::Context self, cl_mem_flags flags, size_t len) |
568 | PPCODE: |
717 | PPCODE: |
569 | if (flags & (CL_MEM_USE_HOST_PTR | CL_MEM_COPY_HOST_PTR)) |
718 | if (flags & (CL_MEM_USE_HOST_PTR | CL_MEM_COPY_HOST_PTR)) |
570 | croak ("clCreateBuffer: cannot use/copy host ptr when no data is given, use $context->buffer_sv instead?"); |
719 | croak ("OpenCL::Context::buffer: cannot use/copy host ptr when no data is given, use $context->buffer_sv instead?"); |
571 | |
720 | |
572 | NEED_SUCCESS_ARG (cl_mem mem, CreateBuffer, (self, flags, len, 0, &res)); |
721 | NEED_SUCCESS_ARG (cl_mem mem, CreateBuffer, (self, flags, len, 0, &res)); |
573 | XPUSH_NEW_OBJ ("OpenCL::BufferObj", mem); |
722 | XPUSH_NEW_OBJ ("OpenCL::BufferObj", mem); |
574 | |
723 | |
575 | void |
724 | void |
576 | buffer_sv (OpenCL::Context self, cl_mem_flags flags, SV *data) |
725 | buffer_sv (OpenCL::Context self, cl_mem_flags flags, SV *data) |
577 | PPCODE: |
726 | PPCODE: |
578 | STRLEN len; |
727 | STRLEN len; |
579 | char *ptr = SvOK (data) ? SvPVbyte (data, len) : 0; |
728 | char *ptr = SvOK (data) ? SvPVbyte (data, len) : 0; |
580 | if (!(flags & (CL_MEM_USE_HOST_PTR | CL_MEM_COPY_HOST_PTR))) |
729 | if (!(flags & (CL_MEM_USE_HOST_PTR | CL_MEM_COPY_HOST_PTR))) |
581 | croak ("clCreateBuffer: have to specify use or copy host ptr when buffer data is given, use $context->buffer instead?"); |
730 | croak ("OpenCL::Context::buffer_sv: you have to specify use or copy host ptr when buffer data is given, use $context->buffer instead?"); |
582 | NEED_SUCCESS_ARG (cl_mem mem, CreateBuffer, (self, flags, len, ptr, &res)); |
731 | NEED_SUCCESS_ARG (cl_mem mem, CreateBuffer, (self, flags, len, ptr, &res)); |
583 | XPUSH_NEW_OBJ ("OpenCL::BufferObj", mem); |
732 | XPUSH_NEW_OBJ ("OpenCL::BufferObj", mem); |
|
|
733 | |
|
|
734 | #if !CL_VERSION_1_2 || defined CL_USE_DEPRECATED_OPENCL_1_1_APIS |
584 | |
735 | |
585 | void |
736 | void |
586 | 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) |
737 | 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) |
587 | PPCODE: |
738 | PPCODE: |
588 | STRLEN len; |
739 | STRLEN len; |
… | |
… | |
597 | STRLEN len; |
748 | STRLEN len; |
598 | char *ptr = SvOK (data) ? SvPVbyte (data, len) : 0; |
749 | char *ptr = SvOK (data) ? SvPVbyte (data, len) : 0; |
599 | const cl_image_format format = { channel_order, channel_type }; |
750 | const cl_image_format format = { channel_order, channel_type }; |
600 | NEED_SUCCESS_ARG (cl_mem mem, CreateImage3D, (self, flags, &format, width, height, depth, row_pitch, slice_pitch, ptr, &res)); |
751 | NEED_SUCCESS_ARG (cl_mem mem, CreateImage3D, (self, flags, &format, width, height, depth, row_pitch, slice_pitch, ptr, &res)); |
601 | XPUSH_NEW_OBJ ("OpenCL::Image3D", mem); |
752 | XPUSH_NEW_OBJ ("OpenCL::Image3D", mem); |
|
|
753 | |
|
|
754 | #endif |
|
|
755 | |
|
|
756 | #if cl_apple_gl_sharing || cl_khr_gl_sharing |
|
|
757 | |
|
|
758 | void |
|
|
759 | gl_buffer (OpenCL::Context self, cl_mem_flags flags, cl_GLuint bufobj) |
|
|
760 | PPCODE: |
|
|
761 | NEED_SUCCESS_ARG (cl_mem mem, CreateFromGLBuffer, (self, flags, bufobj, &res)); |
|
|
762 | XPUSH_NEW_OBJ ("OpenCL::BufferObj", mem); |
|
|
763 | |
|
|
764 | #if !CL_VERSION_1_2 || defined CL_USE_DEPRECATED_OPENCL_1_1_APIS |
|
|
765 | |
|
|
766 | void |
|
|
767 | gl_texture2d (OpenCL::Context self, cl_mem_flags flags, cl_GLenum target, cl_GLint miplevel, cl_GLuint texture) |
|
|
768 | PPCODE: |
|
|
769 | NEED_SUCCESS_ARG (cl_mem mem, CreateFromGLTexture2D, (self, flags, target, miplevel, texture, &res)); |
|
|
770 | XPUSH_NEW_OBJ ("OpenCL::Image2D", mem); |
|
|
771 | |
|
|
772 | void |
|
|
773 | gl_texture3d (OpenCL::Context self, cl_mem_flags flags, cl_GLenum target, cl_GLint miplevel, cl_GLuint texture) |
|
|
774 | PPCODE: |
|
|
775 | NEED_SUCCESS_ARG (cl_mem mem, CreateFromGLTexture3D, (self, flags, target, miplevel, texture, &res)); |
|
|
776 | XPUSH_NEW_OBJ ("OpenCL::Image3D", mem); |
|
|
777 | |
|
|
778 | #endif |
|
|
779 | |
|
|
780 | #if CL_VERSION_1_2 |
|
|
781 | |
|
|
782 | void |
|
|
783 | gl_texture (OpenCL::Context self, cl_mem_flags flags, cl_GLenum target, cl_GLint miplevel, cl_GLuint texture) |
|
|
784 | PPCODE: |
|
|
785 | char *klass = "OpenCL::Memory"; |
|
|
786 | cl_gl_object_type t; |
|
|
787 | NEED_SUCCESS_ARG (cl_mem mem, CreateFromGLTexture2D, (self, flags, target, miplevel, texture, &res)); |
|
|
788 | NEED_SUCCESS (GetGLObjectInfo, (mem, &t, 0)); |
|
|
789 | switch (t) |
|
|
790 | { |
|
|
791 | case CL_GL_OBJECT_TEXTURE_BUFFER: klass = "OpenCL::Image1DBuffer"; break; |
|
|
792 | case CL_GL_OBJECT_TEXTURE1D: klass = "OpenCL::Image1D"; break; |
|
|
793 | case CL_GL_OBJECT_TEXTURE1D_ARRAY: klass = "OpenCL::Image2DArray"; break; |
|
|
794 | case CL_GL_OBJECT_TEXTURE2D: klass = "OpenCL::Image2D"; break; |
|
|
795 | case CL_GL_OBJECT_TEXTURE2D_ARRAY: klass = "OpenCL::Image2DArray"; break; |
|
|
796 | case CL_GL_OBJECT_TEXTURE3D: klass = "OpenCL::Image3D"; break; |
|
|
797 | } |
|
|
798 | XPUSH_NEW_OBJ (klass, mem); |
|
|
799 | |
|
|
800 | #endif |
|
|
801 | |
|
|
802 | void |
|
|
803 | gl_renderbuffer (OpenCL::Context self, cl_mem_flags flags, cl_GLuint renderbuffer) |
|
|
804 | PPCODE: |
|
|
805 | NEED_SUCCESS_ARG (cl_mem mem, CreateFromGLRenderbuffer, (self, flags, renderbuffer, &res)); |
|
|
806 | XPUSH_NEW_OBJ ("OpenCL::Image2D", mem); |
|
|
807 | |
|
|
808 | #endif |
602 | |
809 | |
603 | void |
810 | void |
604 | supported_image_formats (OpenCL::Context self, cl_mem_flags flags, cl_mem_object_type image_type) |
811 | supported_image_formats (OpenCL::Context self, cl_mem_flags flags, cl_mem_object_type image_type) |
605 | PPCODE: |
812 | PPCODE: |
606 | { |
813 | { |
… | |
… | |
709 | cl_event ev = 0; |
916 | cl_event ev = 0; |
710 | STRLEN len; |
917 | STRLEN len; |
711 | char *ptr = SvPVbyte (data, len); |
918 | char *ptr = SvPVbyte (data, len); |
712 | EVENT_LIST (5, items - 5); |
919 | EVENT_LIST (5, items - 5); |
713 | |
920 | |
714 | NEED_SUCCESS (EnqueueReadBuffer, (self, mem, blocking, offset, len, ptr, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0)); |
921 | NEED_SUCCESS (EnqueueWriteBuffer, (self, mem, blocking, offset, len, ptr, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0)); |
715 | |
922 | |
716 | if (ev) |
923 | if (ev) |
717 | XPUSH_NEW_OBJ ("OpenCL::Event", ev); |
924 | XPUSH_NEW_OBJ ("OpenCL::Event", ev); |
718 | |
925 | |
719 | void |
926 | void |
… | |
… | |
785 | size_t min_len = host_row_pitch * host_slice_pitch * region [2]; |
992 | size_t min_len = host_row_pitch * host_slice_pitch * region [2]; |
786 | |
993 | |
787 | if (len < min_len) |
994 | if (len < min_len) |
788 | croak ("clEnqueueWriteImage: data string is shorter than what would be transferred"); |
995 | croak ("clEnqueueWriteImage: data string is shorter than what would be transferred"); |
789 | |
996 | |
790 | NEED_SUCCESS (EnqueueWriteBufferRect, (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)); |
997 | 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)); |
791 | |
998 | |
792 | if (ev) |
999 | if (ev) |
793 | XPUSH_NEW_OBJ ("OpenCL::Event", ev); |
1000 | XPUSH_NEW_OBJ ("OpenCL::Event", ev); |
794 | |
1001 | |
795 | void |
1002 | void |
… | |
… | |
850 | size_t min_len = slice_pitch ? slice_pitch * depth : row_pitch * height; |
1057 | size_t min_len = slice_pitch ? slice_pitch * depth : row_pitch * height; |
851 | |
1058 | |
852 | if (len < min_len) |
1059 | if (len < min_len) |
853 | croak ("clEnqueueWriteImage: data string is shorter than what would be transferred"); |
1060 | croak ("clEnqueueWriteImage: data string is shorter than what would be transferred"); |
854 | |
1061 | |
855 | NEED_SUCCESS (EnqueueWriteImage, (self, dst, blocking, dst_origin, region, row_pitch, slice_pitch, SvPVX (data), event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0)); |
1062 | 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)); |
856 | |
1063 | |
857 | if (ev) |
1064 | if (ev) |
858 | XPUSH_NEW_OBJ ("OpenCL::Event", ev); |
1065 | XPUSH_NEW_OBJ ("OpenCL::Event", ev); |
859 | |
1066 | |
860 | void |
1067 | void |
… | |
… | |
942 | gwo [i] = SvIV (AvARRAY (SvRV (global_work_offset))[i]); |
1149 | gwo [i] = SvIV (AvARRAY (SvRV (global_work_offset))[i]); |
943 | } |
1150 | } |
944 | |
1151 | |
945 | if (SvOK (local_work_size)) |
1152 | if (SvOK (local_work_size)) |
946 | { |
1153 | { |
947 | if (SvOK (local_work_size) && !SvROK (local_work_size) || SvTYPE (SvRV (local_work_size)) != SVt_PVAV) |
1154 | if ((SvOK (local_work_size) && !SvROK (local_work_size)) || SvTYPE (SvRV (local_work_size)) != SVt_PVAV) |
948 | croak ("clEnqueueNDRangeKernel: global_work_size must be undef or an array reference"); |
1155 | croak ("clEnqueueNDRangeKernel: global_work_size must be undef or an array reference"); |
949 | |
1156 | |
950 | if (AvFILLp (SvRV (local_work_size)) + 1 != gws_len) |
1157 | if (AvFILLp (SvRV (local_work_size)) + 1 != gws_len) |
951 | croak ("clEnqueueNDRangeKernel: local_work_local must be undef or an array of same size as global_work_size"); |
1158 | croak ("clEnqueueNDRangeKernel: local_work_local must be undef or an array of same size as global_work_size"); |
952 | |
1159 | |
… | |
… | |
958 | NEED_SUCCESS (EnqueueNDRangeKernel, (self, kernel, gws_len, gwo, gws, lws, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0)); |
1165 | NEED_SUCCESS (EnqueueNDRangeKernel, (self, kernel, gws_len, gwo, gws, lws, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0)); |
959 | |
1166 | |
960 | if (ev) |
1167 | if (ev) |
961 | XPUSH_NEW_OBJ ("OpenCL::Event", ev); |
1168 | XPUSH_NEW_OBJ ("OpenCL::Event", ev); |
962 | |
1169 | |
|
|
1170 | #if cl_apple_gl_sharing || cl_khr_gl_sharing |
|
|
1171 | |
963 | void |
1172 | void |
964 | enqueue_marker (OpenCL::Queue self) |
1173 | enqueue_acquire_gl_objects (OpenCL::Queue self, SV *objects, ...) |
|
|
1174 | ALIAS: |
|
|
1175 | enqueue_release_gl_objects = 1 |
965 | PPCODE: |
1176 | PPCODE: |
|
|
1177 | if (!SvROK (objects) || SvTYPE (SvRV (objects)) != SVt_PVAV) |
|
|
1178 | croak ("OpenCL::Queue::enqueue_acquire/release_gl_objects argument 'objects' must be an arrayref with memory objects, in call"); |
|
|
1179 | |
966 | cl_event ev; |
1180 | cl_event ev = 0; |
967 | NEED_SUCCESS (EnqueueMarker, (self, &ev)); |
1181 | EVENT_LIST (2, items - 2); |
|
|
1182 | AV *av = (AV *)SvRV (objects); |
|
|
1183 | cl_uint num_objects = av_len (av) + 1; |
|
|
1184 | cl_mem *object_list = tmpbuf (sizeof (cl_mem) * num_objects); |
|
|
1185 | int i; |
|
|
1186 | |
|
|
1187 | for (i = num_objects; i--; ) |
|
|
1188 | object_list [i] = SvPTROBJ ("OpenCL::Queue::enqueue_acquire/release_gl_objects", "objects", *av_fetch (av, i, 0), "OpenCL::Memory"); |
|
|
1189 | |
|
|
1190 | if (ix) |
|
|
1191 | NEED_SUCCESS (EnqueueReleaseGLObjects, (self, num_objects, object_list, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0)); |
|
|
1192 | else |
|
|
1193 | NEED_SUCCESS (EnqueueAcquireGLObjects, (self, num_objects, object_list, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0)); |
|
|
1194 | |
|
|
1195 | if (ev) |
968 | XPUSH_NEW_OBJ ("OpenCL::Event", ev); |
1196 | XPUSH_NEW_OBJ ("OpenCL::Event", ev); |
|
|
1197 | |
|
|
1198 | #endif |
|
|
1199 | |
|
|
1200 | #if !CL_VERSION_1_2 || defined CL_USE_DEPRECATED_OPENCL_1_1_APIS |
969 | |
1201 | |
970 | void |
1202 | void |
971 | enqueue_wait_for_events (OpenCL::Queue self, ...) |
1203 | enqueue_wait_for_events (OpenCL::Queue self, ...) |
972 | CODE: |
1204 | CODE: |
973 | EVENT_LIST (1, items - 1); |
1205 | EVENT_LIST (1, items - 1); |
974 | NEED_SUCCESS (EnqueueWaitForEvents, (self, event_list_count, event_list_ptr)); |
1206 | NEED_SUCCESS (EnqueueWaitForEvents, (self, event_list_count, event_list_ptr)); |
975 | |
1207 | |
|
|
1208 | #endif |
|
|
1209 | |
976 | void |
1210 | void |
|
|
1211 | enqueue_marker (OpenCL::Queue self, ...) |
|
|
1212 | PPCODE: |
|
|
1213 | cl_event ev = 0; |
|
|
1214 | EVENT_LIST (1, items - 1); |
|
|
1215 | #if CL_VERSION_1_2 |
|
|
1216 | NEED_SUCCESS (EnqueueMarkerWithWaitList, (self, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0)); |
|
|
1217 | #else |
|
|
1218 | if (event_list_count) |
|
|
1219 | croak ("OpenCL::Queue->enqueue_marker does not support a wait list in OpenCL 1.1 - upgrade to 1.2"); |
|
|
1220 | NEED_SUCCESS (EnqueueMarker, (self, GIMME_V != G_VOID ? &ev : 0)); |
|
|
1221 | #endif |
|
|
1222 | if (ev) |
|
|
1223 | XPUSH_NEW_OBJ ("OpenCL::Event", ev); |
|
|
1224 | |
|
|
1225 | void |
977 | enqueue_barrier (OpenCL::Queue self) |
1226 | enqueue_barrier (OpenCL::Queue self, ...) |
978 | CODE: |
1227 | PPCODE: |
|
|
1228 | cl_event ev = 0; |
|
|
1229 | EVENT_LIST (1, items - 1); |
|
|
1230 | #if CL_VERSION_1_2 |
|
|
1231 | NEED_SUCCESS (EnqueueBarrierWithWaitList, (self, event_list_count, event_list_ptr, &ev)); |
|
|
1232 | #else |
|
|
1233 | if (event_list_count) |
|
|
1234 | croak ("OpenCL::Queue->enqueue_barrier does not support a wait list in OpenCL 1.1 - upgrade to 1.2"); |
|
|
1235 | if (GIMME_V != G_VOID) |
|
|
1236 | croak ("OpenCL::Queue->enqueue_barrier does not return an event object in OpenCL 1.1 - upgrade to 1.2"); |
979 | NEED_SUCCESS (EnqueueBarrier, (self)); |
1237 | NEED_SUCCESS (EnqueueBarrier, (self)); |
|
|
1238 | #endif |
|
|
1239 | if (ev) |
|
|
1240 | XPUSH_NEW_OBJ ("OpenCL::Event", ev); |
980 | |
1241 | |
981 | void |
1242 | void |
982 | flush (OpenCL::Queue self) |
1243 | flush (OpenCL::Queue self) |
983 | CODE: |
1244 | CODE: |
984 | NEED_SUCCESS (Flush, (self)); |
1245 | NEED_SUCCESS (Flush, (self)); |
… | |
… | |
1126 | NEED_SUCCESS (RetainMemObject, (value [i])); |
1387 | NEED_SUCCESS (RetainMemObject, (value [i])); |
1127 | PUSHs (NEW_MORTAL_OBJ ("OpenCL::Memory", value [i])); |
1388 | PUSHs (NEW_MORTAL_OBJ ("OpenCL::Memory", value [i])); |
1128 | } |
1389 | } |
1129 | |
1390 | |
1130 | #END:mem |
1391 | #END:mem |
|
|
1392 | |
|
|
1393 | #if cl_apple_gl_sharing || cl_khr_gl_sharing |
|
|
1394 | |
|
|
1395 | void |
|
|
1396 | gl_object_info (OpenCL::Memory self) |
|
|
1397 | PPCODE: |
|
|
1398 | cl_gl_object_type type; |
|
|
1399 | cl_GLuint name; |
|
|
1400 | NEED_SUCCESS (GetGLObjectInfo, (self, &type, &name)); |
|
|
1401 | EXTEND (SP, 2); |
|
|
1402 | PUSHs (sv_2mortal (newSVuv (type))); |
|
|
1403 | PUSHs (sv_2mortal (newSVuv (name))); |
|
|
1404 | |
|
|
1405 | #endif |
1131 | |
1406 | |
1132 | MODULE = OpenCL PACKAGE = OpenCL::BufferObj |
1407 | MODULE = OpenCL PACKAGE = OpenCL::BufferObj |
1133 | |
1408 | |
1134 | void |
1409 | void |
1135 | sub_buffer_region (OpenCL::BufferObj self, cl_mem_flags flags, size_t origin, size_t size) |
1410 | sub_buffer_region (OpenCL::BufferObj self, cl_mem_flags flags, size_t origin, size_t size) |
… | |
… | |
1167 | const int i = 0; |
1442 | const int i = 0; |
1168 | PUSHs (sv_2mortal (newSVuv (value [i]))); |
1443 | PUSHs (sv_2mortal (newSVuv (value [i]))); |
1169 | |
1444 | |
1170 | #END:image |
1445 | #END:image |
1171 | |
1446 | |
|
|
1447 | #if cl_apple_gl_sharing || cl_khr_gl_sharing |
|
|
1448 | |
|
|
1449 | #BEGIN:gl_texture |
|
|
1450 | |
|
|
1451 | void |
|
|
1452 | target (OpenCL::Image self) |
|
|
1453 | PPCODE: |
|
|
1454 | cl_GLenum value [1]; |
|
|
1455 | NEED_SUCCESS (GetGLTextureInfo, (self, CL_GL_TEXTURE_TARGET, sizeof (value), value, 0)); |
|
|
1456 | EXTEND (SP, 1); |
|
|
1457 | const int i = 0; |
|
|
1458 | PUSHs (sv_2mortal (newSVuv (value [i]))); |
|
|
1459 | |
|
|
1460 | void |
|
|
1461 | gl_mipmap_level (OpenCL::Image self) |
|
|
1462 | PPCODE: |
|
|
1463 | cl_GLint value [1]; |
|
|
1464 | NEED_SUCCESS (GetGLTextureInfo, (self, CL_GL_MIPMAP_LEVEL, sizeof (value), value, 0)); |
|
|
1465 | EXTEND (SP, 1); |
|
|
1466 | const int i = 0; |
|
|
1467 | PUSHs (sv_2mortal (newSViv (value [i]))); |
|
|
1468 | |
|
|
1469 | #END:gl_texture |
|
|
1470 | |
|
|
1471 | #endif |
|
|
1472 | |
1172 | MODULE = OpenCL PACKAGE = OpenCL::Sampler |
1473 | MODULE = OpenCL PACKAGE = OpenCL::Sampler |
1173 | |
1474 | |
1174 | void |
1475 | void |
1175 | DESTROY (OpenCL::Sampler self) |
1476 | DESTROY (OpenCL::Sampler self) |
1176 | CODE: |
1477 | CODE: |
… | |
… | |
1314 | { |
1615 | { |
1315 | SV *sv = sv_2mortal (newSV (sizes [i])); |
1616 | SV *sv = sv_2mortal (newSV (sizes [i])); |
1316 | SvUPGRADE (sv, SVt_PV); |
1617 | SvUPGRADE (sv, SVt_PV); |
1317 | SvPOK_only (sv); |
1618 | SvPOK_only (sv); |
1318 | SvCUR_set (sv, sizes [i]); |
1619 | SvCUR_set (sv, sizes [i]); |
1319 | ptrs [i] = SvPVX (sv); |
1620 | ptrs [i] = (void *)SvPVX (sv); |
1320 | PUSHs (sv); |
1621 | PUSHs (sv); |
1321 | } |
1622 | } |
1322 | |
1623 | |
1323 | NEED_SUCCESS (GetProgramInfo, (self, CL_PROGRAM_BINARIES , sizeof (*ptrs ) * n, ptrs , &size)); |
1624 | NEED_SUCCESS (GetProgramInfo, (self, CL_PROGRAM_BINARIES , sizeof (*ptrs ) * n, ptrs , &size)); |
1324 | if (size != sizeof (*ptrs) * n) XSRETURN_EMPTY; |
1625 | if (size != sizeof (*ptrs) * n) XSRETURN_EMPTY; |
… | |
… | |
1474 | set_sampler (OpenCL::Kernel self, cl_uint idx, OpenCL::Sampler value) |
1775 | set_sampler (OpenCL::Kernel self, cl_uint idx, OpenCL::Sampler value) |
1475 | CODE: |
1776 | CODE: |
1476 | clSetKernelArg (self, idx, sizeof (value), &value); |
1777 | clSetKernelArg (self, idx, sizeof (value), &value); |
1477 | |
1778 | |
1478 | void |
1779 | void |
|
|
1780 | set_local (OpenCL::Kernel self, cl_uint idx, size_t size) |
|
|
1781 | CODE: |
|
|
1782 | clSetKernelArg (self, idx, size, 0); |
|
|
1783 | |
|
|
1784 | void |
1479 | set_event (OpenCL::Kernel self, cl_uint idx, OpenCL::Event value) |
1785 | set_event (OpenCL::Kernel self, cl_uint idx, OpenCL::Event value) |
1480 | CODE: |
1786 | CODE: |
1481 | clSetKernelArg (self, idx, sizeof (value), &value); |
1787 | clSetKernelArg (self, idx, sizeof (value), &value); |
1482 | |
1788 | |
1483 | void |
1789 | void |