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 |
4 | |
8 | |
5 | #ifdef __APPLE__ |
9 | #ifdef __APPLE__ |
6 | #include <OpenCL/opencl.h> |
10 | #include <OpenCL/opencl.h> |
7 | #else |
11 | #else |
8 | #include <CL/opencl.h> |
12 | #include <CL/opencl.h> |
9 | #endif |
|
|
10 | #if HAVE_OPENGL |
|
|
11 | #ifdef __APPLE__ |
|
|
12 | #include <OpenGL/gl.h> |
|
|
13 | #include <OpenCL/cl_gl.h> |
|
|
14 | #else |
|
|
15 | #include <GL/gl.h> |
|
|
16 | #include <CL/cl_gl.h> |
|
|
17 | #endif |
|
|
18 | #endif |
13 | #endif |
19 | |
14 | |
20 | typedef cl_platform_id OpenCL__Platform; |
15 | typedef cl_platform_id OpenCL__Platform; |
21 | typedef cl_device_id OpenCL__Device; |
16 | typedef cl_device_id OpenCL__Device; |
22 | typedef cl_context OpenCL__Context; |
17 | typedef cl_context OpenCL__Context; |
… | |
… | |
40 | |
35 | |
41 | typedef SV *FUTURE; |
36 | typedef SV *FUTURE; |
42 | |
37 | |
43 | /*****************************************************************************/ |
38 | /*****************************************************************************/ |
44 | |
39 | |
|
|
40 | // name must include a leading underscore |
|
|
41 | // all of this horrors would be unneceesary if somebody wrote a proper OpenGL module |
|
|
42 | // for perl. doh. |
|
|
43 | static void * |
|
|
44 | glsym (const char *name) |
|
|
45 | { |
|
|
46 | void *fun = 0; |
|
|
47 | |
|
|
48 | #if defined I_DLFCN && defined RTLD_DEFAULT |
|
|
49 | fun = dlsym (RTLD_DEFAULT, name + 1); |
|
|
50 | if (!fun) fun = dlsym (RTLD_DEFAULT, name); |
|
|
51 | |
|
|
52 | if (!fun) |
|
|
53 | { |
|
|
54 | static void *libgl; |
|
|
55 | static const char *glso[] = { |
|
|
56 | "libGL.so.1", |
|
|
57 | "libGL.so.3", |
|
|
58 | "libGL.so.4.0", |
|
|
59 | "libGL.so", |
|
|
60 | "/usr/lib/libGL.so", |
|
|
61 | "/usr/X11R6/lib/libGL.1.dylib" |
|
|
62 | }; |
|
|
63 | int i; |
|
|
64 | |
|
|
65 | for (i = 0; !libgl && i < sizeof (glso) / sizeof (glso [0]); ++i) |
|
|
66 | { |
|
|
67 | libgl = dlopen (glso [i], RTLD_LAZY); |
|
|
68 | if (libgl) |
|
|
69 | break; |
|
|
70 | } |
|
|
71 | |
|
|
72 | if (libgl) |
|
|
73 | { |
|
|
74 | fun = dlsym (libgl, name + 1); |
|
|
75 | if (!fun) fun = dlsym (libgl, name); |
|
|
76 | } |
|
|
77 | } |
|
|
78 | #endif |
|
|
79 | |
|
|
80 | return fun; |
|
|
81 | } |
|
|
82 | |
|
|
83 | /*****************************************************************************/ |
|
|
84 | |
45 | /* up to two temporary buffers */ |
85 | /* up to two temporary buffers */ |
46 | static void * |
86 | static void * |
47 | tmpbuf (size_t size) |
87 | tmpbuf (size_t size) |
48 | { |
88 | { |
49 | enum { buffers = 3 }; |
89 | enum { buffers = 3 }; |
… | |
… | |
153 | return 0; |
193 | return 0; |
154 | |
194 | |
155 | if (SvROK (sv) && SvTYPE (SvRV (sv)) == SVt_PVAV) |
195 | if (SvROK (sv) && SvTYPE (SvRV (sv)) == SVt_PVAV) |
156 | { |
196 | { |
157 | AV *av = (AV *)SvRV (sv); |
197 | AV *av = (AV *)SvRV (sv); |
158 | int i, len = av_len (av); |
198 | int i, len = av_len (av) + 1; |
159 | cl_context_properties *p = tmpbuf (sizeof (cl_context_properties) * (len + extracount + 1)); |
199 | cl_context_properties *p = tmpbuf (sizeof (cl_context_properties) * (len + extracount + 1)); |
160 | cl_context_properties *l = p; |
200 | cl_context_properties *l = p; |
161 | |
201 | |
162 | if (len & 1) |
202 | if (len & 1) |
163 | croak ("%s: %s is not a property list (must be even number of elements)", func, svname); |
203 | croak ("%s: %s is not a property list (must be even number of elements)", func, svname); |
… | |
… | |
165 | while (extracount--) |
205 | while (extracount--) |
166 | *l++ = *extra++; |
206 | *l++ = *extra++; |
167 | |
207 | |
168 | for (i = 0; i < len; i += 2) |
208 | for (i = 0; i < len; i += 2) |
169 | { |
209 | { |
170 | cl_context_properties t = SvIV (*av_fetch (av, i, 0)); |
210 | cl_context_properties t = SvIV (*av_fetch (av, i , 0)); |
171 | cl_context_properties v; |
211 | SV *p_sv = *av_fetch (av, i + 1, 0); |
|
|
212 | cl_context_properties v = SvIV (p_sv); // code below can override |
172 | |
213 | |
173 | switch (t) |
214 | switch (t) |
174 | { |
215 | { |
|
|
216 | case CL_GLX_DISPLAY_KHR: |
|
|
217 | if (!SvOK (p_sv)) |
|
|
218 | { |
|
|
219 | void *func = glsym ("_glXGetCurrentDisplay"); |
|
|
220 | if (func) |
|
|
221 | v = (cl_context_properties)((void *(*)(void))func)(); |
|
|
222 | } |
|
|
223 | break; |
|
|
224 | |
|
|
225 | case CL_GL_CONTEXT_KHR: |
|
|
226 | if (!SvOK (p_sv)) |
|
|
227 | { |
|
|
228 | void *func = glsym ("_glXGetCurrentContext"); |
|
|
229 | if (func) |
|
|
230 | v = (cl_context_properties)((void *(*)(void))func)(); |
|
|
231 | } |
|
|
232 | break; |
|
|
233 | |
175 | default: |
234 | default: |
176 | /* unknown property, treat as int */ |
235 | /* unknown property, treat as int */ |
177 | v = SvIV (*av_fetch (av, i, 0)); |
|
|
178 | break; |
236 | break; |
179 | } |
237 | } |
180 | |
238 | |
181 | *l++ = t; |
239 | *l++ = t; |
182 | *l++ = v; |
240 | *l++ = v; |
… | |
… | |
247 | { sizeof (cl_long ), "SIZEOF_LONG" }, |
305 | { sizeof (cl_long ), "SIZEOF_LONG" }, |
248 | { sizeof (cl_ulong ), "SIZEOF_ULONG" }, |
306 | { sizeof (cl_ulong ), "SIZEOF_ULONG" }, |
249 | { sizeof (cl_half ), "SIZEOF_HALF" }, |
307 | { sizeof (cl_half ), "SIZEOF_HALF" }, |
250 | { sizeof (cl_float ), "SIZEOF_FLOAT" }, |
308 | { sizeof (cl_float ), "SIZEOF_FLOAT" }, |
251 | { sizeof (cl_double), "SIZEOF_DOUBLE" }, |
309 | { sizeof (cl_double), "SIZEOF_DOUBLE" }, |
252 | { HAVE_OPENGL , "HAVE_OPENGL" }, |
|
|
253 | #include "constiv.h" |
310 | #include "constiv.h" |
254 | }; |
311 | }; |
255 | for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ > const_iv; civ--) |
312 | for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ > const_iv; civ--) |
256 | newCONSTSUB (stash, (char *)civ[-1].name, newSViv (civ[-1].iv)); |
313 | newCONSTSUB (stash, (char *)civ[-1].name, newSViv (civ[-1].iv)); |
257 | } |
314 | } |
… | |
… | |
346 | |
403 | |
347 | void |
404 | void |
348 | context (OpenCL::Platform self, cl_context_properties *properties = 0, SV *devices, FUTURE notify = 0) |
405 | context (OpenCL::Platform self, cl_context_properties *properties = 0, SV *devices, FUTURE notify = 0) |
349 | PPCODE: |
406 | PPCODE: |
350 | if (!SvROK (devices) || SvTYPE (SvRV (devices)) != SVt_PVAV) |
407 | if (!SvROK (devices) || SvTYPE (SvRV (devices)) != SVt_PVAV) |
351 | croak ("OpenCL::Platform argument 'device' must be an arrayref with device objects, in call"); |
408 | croak ("OpenCL::Platform::context argument 'device' must be an arrayref with device objects, in call"); |
352 | |
409 | |
353 | AV *av = (AV *)SvRV (devices); |
410 | AV *av = (AV *)SvRV (devices); |
354 | cl_uint num_devices = av_len (av) + 1; |
411 | cl_uint num_devices = av_len (av) + 1; |
355 | cl_device_id *device_list = tmpbuf (sizeof (cl_device_id) * num_devices); |
412 | cl_device_id *device_list = tmpbuf (sizeof (cl_device_id) * num_devices); |
356 | int i; |
413 | int i; |
… | |
… | |
626 | |
683 | |
627 | void |
684 | void |
628 | buffer (OpenCL::Context self, cl_mem_flags flags, size_t len) |
685 | buffer (OpenCL::Context self, cl_mem_flags flags, size_t len) |
629 | PPCODE: |
686 | PPCODE: |
630 | if (flags & (CL_MEM_USE_HOST_PTR | CL_MEM_COPY_HOST_PTR)) |
687 | if (flags & (CL_MEM_USE_HOST_PTR | CL_MEM_COPY_HOST_PTR)) |
631 | croak ("clCreateBuffer: cannot use/copy host ptr when no data is given, use $context->buffer_sv instead?"); |
688 | croak ("OpenCL::Context::buffer: cannot use/copy host ptr when no data is given, use $context->buffer_sv instead?"); |
632 | |
689 | |
633 | NEED_SUCCESS_ARG (cl_mem mem, CreateBuffer, (self, flags, len, 0, &res)); |
690 | NEED_SUCCESS_ARG (cl_mem mem, CreateBuffer, (self, flags, len, 0, &res)); |
634 | XPUSH_NEW_OBJ ("OpenCL::BufferObj", mem); |
691 | XPUSH_NEW_OBJ ("OpenCL::BufferObj", mem); |
635 | |
692 | |
636 | void |
693 | void |
637 | buffer_sv (OpenCL::Context self, cl_mem_flags flags, SV *data) |
694 | buffer_sv (OpenCL::Context self, cl_mem_flags flags, SV *data) |
638 | PPCODE: |
695 | PPCODE: |
639 | STRLEN len; |
696 | STRLEN len; |
640 | char *ptr = SvOK (data) ? SvPVbyte (data, len) : 0; |
697 | char *ptr = SvOK (data) ? SvPVbyte (data, len) : 0; |
641 | if (!(flags & (CL_MEM_USE_HOST_PTR | CL_MEM_COPY_HOST_PTR))) |
698 | if (!(flags & (CL_MEM_USE_HOST_PTR | CL_MEM_COPY_HOST_PTR))) |
642 | croak ("clCreateBuffer: have to specify use or copy host ptr when buffer data is given, use $context->buffer instead?"); |
699 | croak ("OpenCL::Context::buffer_sv: you have to specify use or copy host ptr when buffer data is given, use $context->buffer instead?"); |
643 | NEED_SUCCESS_ARG (cl_mem mem, CreateBuffer, (self, flags, len, ptr, &res)); |
700 | NEED_SUCCESS_ARG (cl_mem mem, CreateBuffer, (self, flags, len, ptr, &res)); |
644 | XPUSH_NEW_OBJ ("OpenCL::BufferObj", mem); |
701 | XPUSH_NEW_OBJ ("OpenCL::BufferObj", mem); |
645 | |
702 | |
646 | void |
703 | void |
647 | 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) |
704 | 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) |
… | |
… | |
658 | STRLEN len; |
715 | STRLEN len; |
659 | char *ptr = SvOK (data) ? SvPVbyte (data, len) : 0; |
716 | char *ptr = SvOK (data) ? SvPVbyte (data, len) : 0; |
660 | const cl_image_format format = { channel_order, channel_type }; |
717 | const cl_image_format format = { channel_order, channel_type }; |
661 | NEED_SUCCESS_ARG (cl_mem mem, CreateImage3D, (self, flags, &format, width, height, depth, row_pitch, slice_pitch, ptr, &res)); |
718 | NEED_SUCCESS_ARG (cl_mem mem, CreateImage3D, (self, flags, &format, width, height, depth, row_pitch, slice_pitch, ptr, &res)); |
662 | XPUSH_NEW_OBJ ("OpenCL::Image3D", mem); |
719 | XPUSH_NEW_OBJ ("OpenCL::Image3D", mem); |
|
|
720 | |
|
|
721 | #if cl_apple_gl_sharing || cl_khr_gl_sharing |
|
|
722 | |
|
|
723 | void |
|
|
724 | gl_buffer (OpenCL::Context self, cl_mem_flags flags, cl_GLuint bufobj) |
|
|
725 | PPCODE: |
|
|
726 | NEED_SUCCESS_ARG (cl_mem mem, CreateFromGLBuffer, (self, flags, bufobj, &res)); |
|
|
727 | XPUSH_NEW_OBJ ("OpenCL::BufferObj", mem); |
|
|
728 | |
|
|
729 | void |
|
|
730 | gl_texture2d (OpenCL::Context self, cl_mem_flags flags, cl_GLenum target, cl_GLint miplevel, cl_GLuint texture) |
|
|
731 | PPCODE: |
|
|
732 | NEED_SUCCESS_ARG (cl_mem mem, CreateFromGLTexture2D, (self, flags, target, miplevel, texture, &res)); |
|
|
733 | XPUSH_NEW_OBJ ("OpenCL::Image2D", mem); |
|
|
734 | |
|
|
735 | void |
|
|
736 | gl_texture3d (OpenCL::Context self, cl_mem_flags flags, cl_GLenum target, cl_GLint miplevel, cl_GLuint texture) |
|
|
737 | PPCODE: |
|
|
738 | NEED_SUCCESS_ARG (cl_mem mem, CreateFromGLTexture3D, (self, flags, target, miplevel, texture, &res)); |
|
|
739 | XPUSH_NEW_OBJ ("OpenCL::Image3D", mem); |
|
|
740 | |
|
|
741 | void |
|
|
742 | gl_renderbuffer (OpenCL::Context self, cl_mem_flags flags, cl_GLuint renderbuffer) |
|
|
743 | PPCODE: |
|
|
744 | NEED_SUCCESS_ARG (cl_mem mem, CreateFromGLRenderbuffer, (self, flags, renderbuffer, &res)); |
|
|
745 | XPUSH_NEW_OBJ ("OpenCL::Image2D", mem); |
|
|
746 | |
|
|
747 | #endif |
663 | |
748 | |
664 | void |
749 | void |
665 | supported_image_formats (OpenCL::Context self, cl_mem_flags flags, cl_mem_object_type image_type) |
750 | supported_image_formats (OpenCL::Context self, cl_mem_flags flags, cl_mem_object_type image_type) |
666 | PPCODE: |
751 | PPCODE: |
667 | { |
752 | { |
… | |
… | |
1019 | NEED_SUCCESS (EnqueueNDRangeKernel, (self, kernel, gws_len, gwo, gws, lws, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0)); |
1104 | NEED_SUCCESS (EnqueueNDRangeKernel, (self, kernel, gws_len, gwo, gws, lws, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0)); |
1020 | |
1105 | |
1021 | if (ev) |
1106 | if (ev) |
1022 | XPUSH_NEW_OBJ ("OpenCL::Event", ev); |
1107 | XPUSH_NEW_OBJ ("OpenCL::Event", ev); |
1023 | |
1108 | |
|
|
1109 | #if cl_apple_gl_sharing || cl_khr_gl_sharing |
|
|
1110 | |
|
|
1111 | void |
|
|
1112 | enqueue_acquire_gl_objects (OpenCL::Queue self, SV *objects, ...) |
|
|
1113 | ALIAS: |
|
|
1114 | enqueue_release_gl_objects = 1 |
|
|
1115 | CODE: |
|
|
1116 | if (!SvROK (objects) || SvTYPE (SvRV (objects)) != SVt_PVAV) |
|
|
1117 | croak ("OpenCL::Queue::enqueue_acquire/release_gl_objects argument 'objects' must be an arrayref with memory objects, in call"); |
|
|
1118 | |
|
|
1119 | cl_event ev = 0; |
|
|
1120 | EVENT_LIST (2, items - 2); |
|
|
1121 | AV *av = (AV *)SvRV (objects); |
|
|
1122 | cl_uint num_objects = av_len (av) + 1; |
|
|
1123 | cl_mem *object_list = tmpbuf (sizeof (cl_mem) * num_objects); |
|
|
1124 | int i; |
|
|
1125 | |
|
|
1126 | for (i = num_objects; i--; ) |
|
|
1127 | object_list [i] = SvPTROBJ ("OpenCL::Queue::enqueue_acquire/release_gl_objects", "objects", *av_fetch (av, i, 0), "OpenCL::Memory"); |
|
|
1128 | |
|
|
1129 | if (ix) |
|
|
1130 | NEED_SUCCESS (EnqueueReleaseGLObjects, (self, num_objects, object_list, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0)); |
|
|
1131 | else |
|
|
1132 | NEED_SUCCESS (EnqueueAcquireGLObjects, (self, num_objects, object_list, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0)); |
|
|
1133 | |
|
|
1134 | if (ev) |
|
|
1135 | XPUSH_NEW_OBJ ("OpenCL::Event", ev); |
|
|
1136 | |
|
|
1137 | #endif |
|
|
1138 | |
1024 | void |
1139 | void |
1025 | enqueue_marker (OpenCL::Queue self) |
1140 | enqueue_marker (OpenCL::Queue self) |
1026 | PPCODE: |
1141 | PPCODE: |
1027 | cl_event ev; |
1142 | cl_event ev; |
1028 | NEED_SUCCESS (EnqueueMarker, (self, &ev)); |
1143 | NEED_SUCCESS (EnqueueMarker, (self, &ev)); |
… | |
… | |
1187 | NEED_SUCCESS (RetainMemObject, (value [i])); |
1302 | NEED_SUCCESS (RetainMemObject, (value [i])); |
1188 | PUSHs (NEW_MORTAL_OBJ ("OpenCL::Memory", value [i])); |
1303 | PUSHs (NEW_MORTAL_OBJ ("OpenCL::Memory", value [i])); |
1189 | } |
1304 | } |
1190 | |
1305 | |
1191 | #END:mem |
1306 | #END:mem |
|
|
1307 | |
|
|
1308 | #if cl_apple_gl_sharing || cl_khr_gl_sharing |
|
|
1309 | |
|
|
1310 | void |
|
|
1311 | gl_object_info (OpenCL::Memory self) |
|
|
1312 | PPCODE: |
|
|
1313 | cl_gl_object_type type; |
|
|
1314 | cl_GLuint name; |
|
|
1315 | NEED_SUCCESS (GetGLObjectInfo, (self, &type, &name)); |
|
|
1316 | EXTEND (SP, 2); |
|
|
1317 | PUSHs (sv_2mortal (newSVuv (type))); |
|
|
1318 | PUSHs (sv_2mortal (newSVuv (name))); |
|
|
1319 | |
|
|
1320 | #endif |
1192 | |
1321 | |
1193 | MODULE = OpenCL PACKAGE = OpenCL::BufferObj |
1322 | MODULE = OpenCL PACKAGE = OpenCL::BufferObj |
1194 | |
1323 | |
1195 | void |
1324 | void |
1196 | sub_buffer_region (OpenCL::BufferObj self, cl_mem_flags flags, size_t origin, size_t size) |
1325 | sub_buffer_region (OpenCL::BufferObj self, cl_mem_flags flags, size_t origin, size_t size) |
… | |
… | |
1227 | EXTEND (SP, 1); |
1356 | EXTEND (SP, 1); |
1228 | const int i = 0; |
1357 | const int i = 0; |
1229 | PUSHs (sv_2mortal (newSVuv (value [i]))); |
1358 | PUSHs (sv_2mortal (newSVuv (value [i]))); |
1230 | |
1359 | |
1231 | #END:image |
1360 | #END:image |
|
|
1361 | |
|
|
1362 | #if cl_apple_gl_sharing || cl_khr_gl_sharing |
|
|
1363 | |
|
|
1364 | #BEGIN:gl_texture |
|
|
1365 | |
|
|
1366 | void |
|
|
1367 | target (OpenCL::Image self) |
|
|
1368 | PPCODE: |
|
|
1369 | cl_GLenum value [1]; |
|
|
1370 | NEED_SUCCESS (GetGLTextureInfo, (self, CL_GL_TEXTURE_TARGET, sizeof (value), value, 0)); |
|
|
1371 | EXTEND (SP, 1); |
|
|
1372 | const int i = 0; |
|
|
1373 | PUSHs (sv_2mortal (newSVuv (value [i]))); |
|
|
1374 | |
|
|
1375 | void |
|
|
1376 | gl_mipmap_level (OpenCL::Image self) |
|
|
1377 | PPCODE: |
|
|
1378 | cl_GLint value [1]; |
|
|
1379 | NEED_SUCCESS (GetGLTextureInfo, (self, CL_GL_MIPMAP_LEVEL, sizeof (value), value, 0)); |
|
|
1380 | EXTEND (SP, 1); |
|
|
1381 | const int i = 0; |
|
|
1382 | PUSHs (sv_2mortal (newSViv (value [i]))); |
|
|
1383 | |
|
|
1384 | #END:gl_texture |
|
|
1385 | |
|
|
1386 | #endif |
1232 | |
1387 | |
1233 | MODULE = OpenCL PACKAGE = OpenCL::Sampler |
1388 | MODULE = OpenCL PACKAGE = OpenCL::Sampler |
1234 | |
1389 | |
1235 | void |
1390 | void |
1236 | DESTROY (OpenCL::Sampler self) |
1391 | DESTROY (OpenCL::Sampler self) |
… | |
… | |
1535 | set_sampler (OpenCL::Kernel self, cl_uint idx, OpenCL::Sampler value) |
1690 | set_sampler (OpenCL::Kernel self, cl_uint idx, OpenCL::Sampler value) |
1536 | CODE: |
1691 | CODE: |
1537 | clSetKernelArg (self, idx, sizeof (value), &value); |
1692 | clSetKernelArg (self, idx, sizeof (value), &value); |
1538 | |
1693 | |
1539 | void |
1694 | void |
|
|
1695 | set_local (OpenCL::Kernel self, cl_uint idx, size_t size) |
|
|
1696 | CODE: |
|
|
1697 | clSetKernelArg (self, idx, size, 0); |
|
|
1698 | |
|
|
1699 | void |
1540 | set_event (OpenCL::Kernel self, cl_uint idx, OpenCL::Event value) |
1700 | set_event (OpenCL::Kernel self, cl_uint idx, OpenCL::Event value) |
1541 | CODE: |
1701 | CODE: |
1542 | clSetKernelArg (self, idx, sizeof (value), &value); |
1702 | clSetKernelArg (self, idx, sizeof (value), &value); |
1543 | |
1703 | |
1544 | void |
1704 | void |