ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/OpenCL/OpenCL.xs
(Generate patch)

Comparing OpenCL/OpenCL.xs (file contents):
Revision 1.24 by root, Thu Apr 19 12:12:04 2012 UTC vs.
Revision 1.30 by root, Thu Apr 19 14:34:56 2012 UTC

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
20typedef cl_platform_id OpenCL__Platform; 15typedef cl_platform_id OpenCL__Platform;
21typedef cl_device_id OpenCL__Device; 16typedef cl_device_id OpenCL__Device;
22typedef cl_context OpenCL__Context; 17typedef cl_context OpenCL__Context;
40 35
41typedef SV *FUTURE; 36typedef SV *FUTURE;
42 37
43/*****************************************************************************/ 38/*****************************************************************************/
44 39
40// name must include a leading underscore
41static void *
42getsym (const char *name)
43{
44 #if defined I_DLFCN && defined RTLD_DEFAULT
45 #if !DLSYM_NEEDS_UNDERSCORE
46 ++name; // skip _
47 #endif
48 return dlsym (RTLD_DEFAULT, name);
49 #else
50 return 0;
51 #endif
52}
53
54/*****************************************************************************/
55
45/* up to two temporary buffers */ 56/* up to two temporary buffers */
46static void * 57static void *
47tmpbuf (size_t size) 58tmpbuf (size_t size)
48{ 59{
49 enum { buffers = 3 }; 60 enum { buffers = 3 };
153 return 0; 164 return 0;
154 165
155 if (SvROK (sv) && SvTYPE (SvRV (sv)) == SVt_PVAV) 166 if (SvROK (sv) && SvTYPE (SvRV (sv)) == SVt_PVAV)
156 { 167 {
157 AV *av = (AV *)SvRV (sv); 168 AV *av = (AV *)SvRV (sv);
158 int i, len = av_len (av); 169 int i, len = av_len (av) + 1;
159 cl_context_properties *p = tmpbuf (sizeof (cl_context_properties) * (len + extracount + 1)); 170 cl_context_properties *p = tmpbuf (sizeof (cl_context_properties) * (len + extracount + 1));
160 cl_context_properties *l = p; 171 cl_context_properties *l = p;
161 172
162 if (len & 1) 173 if (len & 1)
163 croak ("%s: %s is not a property list (must be even number of elements)", func, svname); 174 croak ("%s: %s is not a property list (must be even number of elements)", func, svname);
165 while (extracount--) 176 while (extracount--)
166 *l++ = *extra++; 177 *l++ = *extra++;
167 178
168 for (i = 0; i < len; i += 2) 179 for (i = 0; i < len; i += 2)
169 { 180 {
170 cl_context_properties t = SvIV (*av_fetch (av, i, 0)); 181 cl_context_properties t = SvIV (*av_fetch (av, i , 0));
182 SV *p_sv = *av_fetch (av, i + 1, 0);
171 cl_context_properties v; 183 cl_context_properties v;
172 184
173 switch (t) 185 switch (t)
174 { 186 {
175 default: 187 default:
176 /* unknown property, treat as int */ 188 /* unknown property, treat as int */
177 v = SvIV (*av_fetch (av, i, 0)); 189 v = SvIV (p_sv);
178 break; 190 break;
179 } 191 }
180 192
181 *l++ = t; 193 *l++ = t;
182 *l++ = v; 194 *l++ = v;
247 { sizeof (cl_long ), "SIZEOF_LONG" }, 259 { sizeof (cl_long ), "SIZEOF_LONG" },
248 { sizeof (cl_ulong ), "SIZEOF_ULONG" }, 260 { sizeof (cl_ulong ), "SIZEOF_ULONG" },
249 { sizeof (cl_half ), "SIZEOF_HALF" }, 261 { sizeof (cl_half ), "SIZEOF_HALF" },
250 { sizeof (cl_float ), "SIZEOF_FLOAT" }, 262 { sizeof (cl_float ), "SIZEOF_FLOAT" },
251 { sizeof (cl_double), "SIZEOF_DOUBLE" }, 263 { sizeof (cl_double), "SIZEOF_DOUBLE" },
252 { HAVE_OPENGL , "HAVE_OPENGL" },
253#include "constiv.h" 264#include "constiv.h"
254 }; 265 };
255 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ > const_iv; civ--) 266 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)); 267 newCONSTSUB (stash, (char *)civ[-1].name, newSViv (civ[-1].iv));
257} 268}
346 357
347void 358void
348context (OpenCL::Platform self, cl_context_properties *properties = 0, SV *devices, FUTURE notify = 0) 359context (OpenCL::Platform self, cl_context_properties *properties = 0, SV *devices, FUTURE notify = 0)
349 PPCODE: 360 PPCODE:
350 if (!SvROK (devices) || SvTYPE (SvRV (devices)) != SVt_PVAV) 361 if (!SvROK (devices) || SvTYPE (SvRV (devices)) != SVt_PVAV)
351 croak ("OpenCL::Platform argument 'device' must be an arrayref with device objects, in call"); 362 croak ("OpenCL::Platform::context argument 'device' must be an arrayref with device objects, in call");
352 363
353 AV *av = (AV *)SvRV (devices); 364 AV *av = (AV *)SvRV (devices);
354 cl_uint num_devices = av_len (av) + 1; 365 cl_uint num_devices = av_len (av) + 1;
355 cl_device_id *device_list = tmpbuf (sizeof (cl_device_id) * num_devices); 366 cl_device_id *device_list = tmpbuf (sizeof (cl_device_id) * num_devices);
356 int i; 367 int i;
626 637
627void 638void
628buffer (OpenCL::Context self, cl_mem_flags flags, size_t len) 639buffer (OpenCL::Context self, cl_mem_flags flags, size_t len)
629 PPCODE: 640 PPCODE:
630 if (flags & (CL_MEM_USE_HOST_PTR | CL_MEM_COPY_HOST_PTR)) 641 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?"); 642 croak ("OpenCL::Context::buffer: cannot use/copy host ptr when no data is given, use $context->buffer_sv instead?");
632 643
633 NEED_SUCCESS_ARG (cl_mem mem, CreateBuffer, (self, flags, len, 0, &res)); 644 NEED_SUCCESS_ARG (cl_mem mem, CreateBuffer, (self, flags, len, 0, &res));
634 XPUSH_NEW_OBJ ("OpenCL::BufferObj", mem); 645 XPUSH_NEW_OBJ ("OpenCL::BufferObj", mem);
635 646
636void 647void
637buffer_sv (OpenCL::Context self, cl_mem_flags flags, SV *data) 648buffer_sv (OpenCL::Context self, cl_mem_flags flags, SV *data)
638 PPCODE: 649 PPCODE:
639 STRLEN len; 650 STRLEN len;
640 char *ptr = SvOK (data) ? SvPVbyte (data, len) : 0; 651 char *ptr = SvOK (data) ? SvPVbyte (data, len) : 0;
641 if (!(flags & (CL_MEM_USE_HOST_PTR | CL_MEM_COPY_HOST_PTR))) 652 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?"); 653 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)); 654 NEED_SUCCESS_ARG (cl_mem mem, CreateBuffer, (self, flags, len, ptr, &res));
644 XPUSH_NEW_OBJ ("OpenCL::BufferObj", mem); 655 XPUSH_NEW_OBJ ("OpenCL::BufferObj", mem);
645 656
646void 657void
647image2d (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) 658image2d (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; 669 STRLEN len;
659 char *ptr = SvOK (data) ? SvPVbyte (data, len) : 0; 670 char *ptr = SvOK (data) ? SvPVbyte (data, len) : 0;
660 const cl_image_format format = { channel_order, channel_type }; 671 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)); 672 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); 673 XPUSH_NEW_OBJ ("OpenCL::Image3D", mem);
674
675#if cl_apple_gl_sharing || cl_khr_gl_sharing
676
677void
678gl_buffer (OpenCL::Context self, cl_mem_flags flags, cl_GLuint bufobj)
679 PPCODE:
680 NEED_SUCCESS_ARG (cl_mem mem, CreateFromGLBuffer, (self, flags, bufobj, &res));
681 XPUSH_NEW_OBJ ("OpenCL::BufferObj", mem);
682
683void
684gl_texture2d (OpenCL::Context self, cl_mem_flags flags, cl_GLenum target, cl_GLint miplevel, cl_GLuint texture)
685 PPCODE:
686 NEED_SUCCESS_ARG (cl_mem mem, CreateFromGLTexture2D, (self, flags, target, miplevel, texture, &res));
687 XPUSH_NEW_OBJ ("OpenCL::Image2D", mem);
688
689void
690gl_texture3d (OpenCL::Context self, cl_mem_flags flags, cl_GLenum target, cl_GLint miplevel, cl_GLuint texture)
691 PPCODE:
692 NEED_SUCCESS_ARG (cl_mem mem, CreateFromGLTexture3D, (self, flags, target, miplevel, texture, &res));
693 XPUSH_NEW_OBJ ("OpenCL::Image3D", mem);
694
695void
696gl_renderbuffer (OpenCL::Context self, cl_mem_flags flags, cl_GLuint renderbuffer)
697 PPCODE:
698 NEED_SUCCESS_ARG (cl_mem mem, CreateFromGLRenderbuffer, (self, flags, renderbuffer, &res));
699 XPUSH_NEW_OBJ ("OpenCL::Image2D", mem);
700
701#endif
663 702
664void 703void
665supported_image_formats (OpenCL::Context self, cl_mem_flags flags, cl_mem_object_type image_type) 704supported_image_formats (OpenCL::Context self, cl_mem_flags flags, cl_mem_object_type image_type)
666 PPCODE: 705 PPCODE:
667{ 706{
1019 NEED_SUCCESS (EnqueueNDRangeKernel, (self, kernel, gws_len, gwo, gws, lws, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0)); 1058 NEED_SUCCESS (EnqueueNDRangeKernel, (self, kernel, gws_len, gwo, gws, lws, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0));
1020 1059
1021 if (ev) 1060 if (ev)
1022 XPUSH_NEW_OBJ ("OpenCL::Event", ev); 1061 XPUSH_NEW_OBJ ("OpenCL::Event", ev);
1023 1062
1063#if cl_apple_gl_sharing || cl_khr_gl_sharing
1064
1065void
1066enqueue_acquire_gl_objects (OpenCL::Queue self, SV *objects, ...)
1067 ALIAS:
1068 enqueue_release_gl_objects = 1
1069 CODE:
1070 if (!SvROK (objects) || SvTYPE (SvRV (objects)) != SVt_PVAV)
1071 croak ("OpenCL::Queue::enqueue_acquire/release_gl_objects argument 'objects' must be an arrayref with memory objects, in call");
1072
1073 cl_event ev = 0;
1074 EVENT_LIST (2, items - 2);
1075 AV *av = (AV *)SvRV (objects);
1076 cl_uint num_objects = av_len (av) + 1;
1077 cl_mem *object_list = tmpbuf (sizeof (cl_mem) * num_objects);
1078 int i;
1079
1080 for (i = num_objects; i--; )
1081 object_list [i] = SvPTROBJ ("OpenCL::Queue::enqueue_acquire/release_gl_objects", "objects", *av_fetch (av, i, 0), "OpenCL::Memory");
1082
1083 if (ix)
1084 NEED_SUCCESS (EnqueueReleaseGLObjects, (self, num_objects, object_list, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0));
1085 else
1086 NEED_SUCCESS (EnqueueAcquireGLObjects, (self, num_objects, object_list, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0));
1087
1088 if (ev)
1089 XPUSH_NEW_OBJ ("OpenCL::Event", ev);
1090
1091#endif
1092
1024void 1093void
1025enqueue_marker (OpenCL::Queue self) 1094enqueue_marker (OpenCL::Queue self)
1026 PPCODE: 1095 PPCODE:
1027 cl_event ev; 1096 cl_event ev;
1028 NEED_SUCCESS (EnqueueMarker, (self, &ev)); 1097 NEED_SUCCESS (EnqueueMarker, (self, &ev));
1187 NEED_SUCCESS (RetainMemObject, (value [i])); 1256 NEED_SUCCESS (RetainMemObject, (value [i]));
1188 PUSHs (NEW_MORTAL_OBJ ("OpenCL::Memory", value [i])); 1257 PUSHs (NEW_MORTAL_OBJ ("OpenCL::Memory", value [i]));
1189 } 1258 }
1190 1259
1191#END:mem 1260#END:mem
1261
1262#if cl_apple_gl_sharing || cl_khr_gl_sharing
1263
1264void
1265gl_object_info (OpenCL::Memory self)
1266 PPCODE:
1267 cl_gl_object_type type;
1268 cl_GLuint name;
1269 NEED_SUCCESS (clGetGLObjectInfo, (self, &type, &name));
1270 EXTEND (SP, 2);
1271 PUSHs (sv_2mortal (newSVuv (type)));
1272 PUSHs (sv_2mortal (newSVuv (name)));
1273
1274#endif
1192 1275
1193MODULE = OpenCL PACKAGE = OpenCL::BufferObj 1276MODULE = OpenCL PACKAGE = OpenCL::BufferObj
1194 1277
1195void 1278void
1196sub_buffer_region (OpenCL::BufferObj self, cl_mem_flags flags, size_t origin, size_t size) 1279sub_buffer_region (OpenCL::BufferObj self, cl_mem_flags flags, size_t origin, size_t size)
1227 EXTEND (SP, 1); 1310 EXTEND (SP, 1);
1228 const int i = 0; 1311 const int i = 0;
1229 PUSHs (sv_2mortal (newSVuv (value [i]))); 1312 PUSHs (sv_2mortal (newSVuv (value [i])));
1230 1313
1231#END:image 1314#END:image
1315
1316#if cl_apple_gl_sharing || cl_khr_gl_sharing
1317
1318#BEGIN:gl_texture
1319
1320void
1321target (OpenCL::Image self)
1322 PPCODE:
1323 cl_GLenum value [1];
1324 NEED_SUCCESS (GetGlTextureInfo, (self, CL_GL_TEXTURE_TARGET, sizeof (value), value, 0));
1325 EXTEND (SP, 1);
1326 const int i = 0;
1327 PUSHs (sv_2mortal (newSVuv (value [i])));
1328
1329void
1330gl_mipmap_level (OpenCL::Image self)
1331 PPCODE:
1332 cl_GLint value [1];
1333 NEED_SUCCESS (GetGlTextureInfo, (self, CL_GL_MIPMAP_LEVEL, sizeof (value), value, 0));
1334 EXTEND (SP, 1);
1335 const int i = 0;
1336 PUSHs (sv_2mortal (newSViv (value [i])));
1337
1338#END:gl_texture
1339
1340#endif
1232 1341
1233MODULE = OpenCL PACKAGE = OpenCL::Sampler 1342MODULE = OpenCL PACKAGE = OpenCL::Sampler
1234 1343
1235void 1344void
1236DESTROY (OpenCL::Sampler self) 1345DESTROY (OpenCL::Sampler self)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines