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

Comparing OpenCL/OpenCL.xs (file contents):
Revision 1.28 by root, Thu Apr 19 13:49:33 2012 UTC vs.
Revision 1.36 by root, Sat Apr 21 17:56:21 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>
31 35
32typedef SV *FUTURE; 36typedef SV *FUTURE;
33 37
34/*****************************************************************************/ 38/*****************************************************************************/
35 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.
43static void *
44glsym (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
36/* up to two temporary buffers */ 85/* up to two temporary buffers */
37static void * 86static void *
38tmpbuf (size_t size) 87tmpbuf (size_t size)
39{ 88{
40 enum { buffers = 3 }; 89 enum { buffers = 3 };
154 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);
155 204
156 while (extracount--) 205 while (extracount--)
157 *l++ = *extra++; 206 *l++ = *extra++;
158 207
159 for (i = 0; i < len; ++i) 208 for (i = 0; i < len; i += 2)
160 { 209 {
161 cl_context_properties t = SvIV (*av_fetch (av, i, 0)); 210 cl_context_properties t = SvIV (*av_fetch (av, i , 0));
162 cl_context_properties v; 211 SV *p_sv = *av_fetch (av, i + 1, 0);
163 212 cl_context_properties v = SvIV (p_sv); // code below can override
164 ++i;
165 213
166 switch (t) 214 switch (t)
167 { 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
168 default: 234 default:
169 /* unknown property, treat as int */ 235 /* unknown property, treat as int */
170 v = SvIV (*av_fetch (av, i, 0));
171 break; 236 break;
172 } 237 }
173 238
174 *l++ = t; 239 *l++ = t;
175 *l++ = v; 240 *l++ = v;
192 clGetImageInfo (img, CL_IMAGE_ROW_PITCH, sizeof (res), &res, 0); 257 clGetImageInfo (img, CL_IMAGE_ROW_PITCH, sizeof (res), &res, 0);
193 return res; 258 return res;
194} 259}
195 260
196static cl_event * 261static cl_event *
197event_list (SV **items, int count) 262event_list (SV **items, cl_uint *rcount)
198{ 263{
264 cl_uint count = *rcount;
265
199 if (!count) 266 if (!count)
200 return 0; 267 return 0;
201 268
202 cl_event *list = tmpbuf (sizeof (cl_event) * count); 269 cl_event *list = tmpbuf (sizeof (cl_event) * count);
270 int i = 0;
203 271
204 while (count--) 272 do
273 {
274 --count;
275 if (SvOK (items [count]))
205 list [count] = SvPTROBJ ("clEnqueue", "wait_events", items [count], "OpenCL::Event"); 276 list [i++] = SvPTROBJ ("clEnqueue", "wait_events", items [count], "OpenCL::Event");
277 }
278 while (count);
206 279
280 *rcount = i;
281
207 return list; 282 return i ? list : 0;
208} 283}
209 284
210#define EVENT_LIST(items,count) \ 285#define EVENT_LIST(items,count) \
211 cl_uint event_list_count = (count); \ 286 cl_uint event_list_count = (count); \
212 cl_event *event_list_ptr = event_list (&ST (items), event_list_count) 287 cl_event *event_list_ptr = event_list (&ST (items), &event_list_count)
213 288
214#define INFO(class) \ 289#define INFO(class) \
215{ \ 290{ \
216 size_t size; \ 291 size_t size; \
217 NEED_SUCCESS (Get ## class ## Info, (self, name, 0, 0, &size)); \ 292 NEED_SUCCESS (Get ## class ## Info, (self, name, 0, 0, &size)); \
790 cl_event ev = 0; 865 cl_event ev = 0;
791 STRLEN len; 866 STRLEN len;
792 char *ptr = SvPVbyte (data, len); 867 char *ptr = SvPVbyte (data, len);
793 EVENT_LIST (5, items - 5); 868 EVENT_LIST (5, items - 5);
794 869
795 NEED_SUCCESS (EnqueueReadBuffer, (self, mem, blocking, offset, len, ptr, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0)); 870 NEED_SUCCESS (EnqueueWriteBuffer, (self, mem, blocking, offset, len, ptr, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0));
796 871
797 if (ev) 872 if (ev)
798 XPUSH_NEW_OBJ ("OpenCL::Event", ev); 873 XPUSH_NEW_OBJ ("OpenCL::Event", ev);
799 874
800void 875void
1045 1120
1046void 1121void
1047enqueue_acquire_gl_objects (OpenCL::Queue self, SV *objects, ...) 1122enqueue_acquire_gl_objects (OpenCL::Queue self, SV *objects, ...)
1048 ALIAS: 1123 ALIAS:
1049 enqueue_release_gl_objects = 1 1124 enqueue_release_gl_objects = 1
1050 CODE: 1125 PPCODE:
1051 if (!SvROK (objects) || SvTYPE (SvRV (objects)) != SVt_PVAV) 1126 if (!SvROK (objects) || SvTYPE (SvRV (objects)) != SVt_PVAV)
1052 croak ("OpenCL::Queue::enqueue_acquire/release_gl_objects argument 'objects' must be an arrayref with memory objects, in call"); 1127 croak ("OpenCL::Queue::enqueue_acquire/release_gl_objects argument 'objects' must be an arrayref with memory objects, in call");
1053 1128
1054 cl_event ev = 0; 1129 cl_event ev = 0;
1055 EVENT_LIST (2, items - 2); 1130 EVENT_LIST (2, items - 2);
1245void 1320void
1246gl_object_info (OpenCL::Memory self) 1321gl_object_info (OpenCL::Memory self)
1247 PPCODE: 1322 PPCODE:
1248 cl_gl_object_type type; 1323 cl_gl_object_type type;
1249 cl_GLuint name; 1324 cl_GLuint name;
1250 NEED_SUCCESS (clGetGLObjectInfo, (self, &type, &name)); 1325 NEED_SUCCESS (GetGLObjectInfo, (self, &type, &name));
1251 EXTEND (SP, 2); 1326 EXTEND (SP, 2);
1252 PUSHs (sv_2mortal (newSVuv (type))); 1327 PUSHs (sv_2mortal (newSVuv (type)));
1253 PUSHs (sv_2mortal (newSVuv (name))); 1328 PUSHs (sv_2mortal (newSVuv (name)));
1254 1329
1255#endif 1330#endif
1300 1375
1301void 1376void
1302target (OpenCL::Image self) 1377target (OpenCL::Image self)
1303 PPCODE: 1378 PPCODE:
1304 cl_GLenum value [1]; 1379 cl_GLenum value [1];
1305 NEED_SUCCESS (GetGlTextureInfo, (self, CL_GL_TEXTURE_TARGET, sizeof (value), value, 0)); 1380 NEED_SUCCESS (GetGLTextureInfo, (self, CL_GL_TEXTURE_TARGET, sizeof (value), value, 0));
1306 EXTEND (SP, 1); 1381 EXTEND (SP, 1);
1307 const int i = 0; 1382 const int i = 0;
1308 PUSHs (sv_2mortal (newSVuv (value [i]))); 1383 PUSHs (sv_2mortal (newSVuv (value [i])));
1309 1384
1310void 1385void
1311gl_mipmap_level (OpenCL::Image self) 1386gl_mipmap_level (OpenCL::Image self)
1312 PPCODE: 1387 PPCODE:
1313 cl_GLint value [1]; 1388 cl_GLint value [1];
1314 NEED_SUCCESS (GetGlTextureInfo, (self, CL_GL_MIPMAP_LEVEL, sizeof (value), value, 0)); 1389 NEED_SUCCESS (GetGLTextureInfo, (self, CL_GL_MIPMAP_LEVEL, sizeof (value), value, 0));
1315 EXTEND (SP, 1); 1390 EXTEND (SP, 1);
1316 const int i = 0; 1391 const int i = 0;
1317 PUSHs (sv_2mortal (newSViv (value [i]))); 1392 PUSHs (sv_2mortal (newSViv (value [i])));
1318 1393
1319#END:gl_texture 1394#END:gl_texture
1625set_sampler (OpenCL::Kernel self, cl_uint idx, OpenCL::Sampler value) 1700set_sampler (OpenCL::Kernel self, cl_uint idx, OpenCL::Sampler value)
1626 CODE: 1701 CODE:
1627 clSetKernelArg (self, idx, sizeof (value), &value); 1702 clSetKernelArg (self, idx, sizeof (value), &value);
1628 1703
1629void 1704void
1705set_local (OpenCL::Kernel self, cl_uint idx, size_t size)
1706 CODE:
1707 clSetKernelArg (self, idx, size, 0);
1708
1709void
1630set_event (OpenCL::Kernel self, cl_uint idx, OpenCL::Event value) 1710set_event (OpenCL::Kernel self, cl_uint idx, OpenCL::Event value)
1631 CODE: 1711 CODE:
1632 clSetKernelArg (self, idx, sizeof (value), &value); 1712 clSetKernelArg (self, idx, sizeof (value), &value);
1633 1713
1634void 1714void

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines