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

Comparing OpenCL/OpenCL.xs (file contents):
Revision 1.22 by root, Mon Apr 16 09:42:33 2012 UTC vs.
Revision 1.24 by root, Thu Apr 19 12:12:04 2012 UTC

4 4
5#ifdef __APPLE__ 5#ifdef __APPLE__
6 #include <OpenCL/opencl.h> 6 #include <OpenCL/opencl.h>
7#else 7#else
8 #include <CL/opencl.h> 8 #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
9#endif 18#endif
10 19
11typedef cl_platform_id OpenCL__Platform; 20typedef cl_platform_id OpenCL__Platform;
12typedef cl_device_id OpenCL__Device; 21typedef cl_device_id OpenCL__Device;
13typedef cl_context OpenCL__Context; 22typedef cl_context OpenCL__Context;
35 44
36/* up to two temporary buffers */ 45/* up to two temporary buffers */
37static void * 46static void *
38tmpbuf (size_t size) 47tmpbuf (size_t size)
39{ 48{
49 enum { buffers = 3 };
40 static int idx; 50 static int idx;
41 static void *buf [2]; 51 static void *buf [buffers];
42 static size_t len [2]; 52 static size_t len [buffers];
43 53
44 idx ^= 1; 54 idx = ++idx % buffers;
45 55
46 if (len [idx] < size) 56 if (len [idx] < size)
47 { 57 {
48 free (buf [idx]); 58 free (buf [idx]);
49 len [idx] = ((size + 31) & ~4095) + 4096 - 32; 59 len [idx] = ((size + 31) & ~4095) + 4096 - 32;
131 croak ("%s: %s is not of type %s", func, svname, pkg); 141 croak ("%s: %s is not of type %s", func, svname, pkg);
132} 142}
133 143
134/*****************************************************************************/ 144/*****************************************************************************/
135 145
146static cl_context_properties *
147SvCONTEXTPROPERTIES (const char *func, const char *svname, SV *sv, cl_context_properties *extra, int extracount)
148{
149 if (!sv || !SvOK (sv))
150 if (extra)
151 sv = sv_2mortal (newRV_noinc ((SV *)newAV ())); // slow, but rarely used hopefully
152 else
153 return 0;
154
155 if (SvROK (sv) && SvTYPE (SvRV (sv)) == SVt_PVAV)
156 {
157 AV *av = (AV *)SvRV (sv);
158 int i, len = av_len (av);
159 cl_context_properties *p = tmpbuf (sizeof (cl_context_properties) * (len + extracount + 1));
160 cl_context_properties *l = p;
161
162 if (len & 1)
163 croak ("%s: %s is not a property list (must be even number of elements)", func, svname);
164
165 while (extracount--)
166 *l++ = *extra++;
167
168 for (i = 0; i < len; i += 2)
169 {
170 cl_context_properties t = SvIV (*av_fetch (av, i, 0));
171 cl_context_properties v;
172
173 switch (t)
174 {
175 default:
176 /* unknown property, treat as int */
177 v = SvIV (*av_fetch (av, i, 0));
178 break;
179 }
180
181 *l++ = t;
182 *l++ = v;
183 }
184
185 *l = 0;
186
187 return p;
188 }
189
190 croak ("%s: %s is not a property list (either undef or [type => value, ...])", func, svname);
191}
192
193/*****************************************************************************/
194
136static size_t 195static size_t
137img_row_pitch (cl_mem img) 196img_row_pitch (cl_mem img)
138{ 197{
139 size_t res; 198 size_t res;
140 clGetImageInfo (img, CL_IMAGE_ROW_PITCH, sizeof (res), &res, 0); 199 clGetImageInfo (img, CL_IMAGE_ROW_PITCH, sizeof (res), &res, 0);
159 cl_uint event_list_count = (count); \ 218 cl_uint event_list_count = (count); \
160 cl_event *event_list_ptr = event_list (&ST (items), event_list_count) 219 cl_event *event_list_ptr = event_list (&ST (items), event_list_count)
161 220
162#define INFO(class) \ 221#define INFO(class) \
163{ \ 222{ \
164 size_t size; \ 223 size_t size; \
165 NEED_SUCCESS (Get ## class ## Info, (self, name, 0, 0, &size)); \ 224 NEED_SUCCESS (Get ## class ## Info, (self, name, 0, 0, &size)); \
166 SV *sv = sv_2mortal (newSV (size)); \ 225 SV *sv = sv_2mortal (newSV (size)); \
167 SvUPGRADE (sv, SVt_PV); \ 226 SvUPGRADE (sv, SVt_PV); \
168 SvPOK_only (sv); \ 227 SvPOK_only (sv); \
169 SvCUR_set (sv, size); \ 228 SvCUR_set (sv, size); \
170 NEED_SUCCESS (Get ## class ## Info, (self, name, size, SvPVX (sv), 0)); \ 229 NEED_SUCCESS (Get ## class ## Info, (self, name, size, SvPVX (sv), 0)); \
171 XPUSHs (sv); \ 230 XPUSHs (sv); \
172} 231}
173 232
174MODULE = OpenCL PACKAGE = OpenCL 233MODULE = OpenCL PACKAGE = OpenCL
175 234
176PROTOTYPES: ENABLE 235PROTOTYPES: ENABLE
177 236
178BOOT: 237BOOT:
179{ 238{
180 HV *stash = gv_stashpv ("OpenCL", 1); 239 HV *stash = gv_stashpv ("OpenCL", 1);
181 static const ivstr *civ, const_iv[] = { 240 static const ivstr *civ, const_iv[] = {
182 { sizeof (cl_char ), "SIZEOF_CHAR" }, 241 { sizeof (cl_char ), "SIZEOF_CHAR" },
183 { sizeof (cl_uchar ), "SIZEOF_UCHAR" }, 242 { sizeof (cl_uchar ), "SIZEOF_UCHAR" },
184 { sizeof (cl_short ), "SIZEOF_SHORT" }, 243 { sizeof (cl_short ), "SIZEOF_SHORT" },
185 { sizeof (cl_ushort), "SIZEOF_USHORT" }, 244 { sizeof (cl_ushort), "SIZEOF_USHORT" },
186 { sizeof (cl_int ), "SIZEOF_INT" }, 245 { sizeof (cl_int ), "SIZEOF_INT" },
187 { sizeof (cl_uint ), "SIZEOF_UINT" }, 246 { sizeof (cl_uint ), "SIZEOF_UINT" },
188 { sizeof (cl_long ), "SIZEOF_LONG" }, 247 { sizeof (cl_long ), "SIZEOF_LONG" },
189 { sizeof (cl_ulong ), "SIZEOF_ULONG" }, 248 { sizeof (cl_ulong ), "SIZEOF_ULONG" },
190 { sizeof (cl_half ), "SIZEOF_HALF" }, 249 { sizeof (cl_half ), "SIZEOF_HALF" },
191 { sizeof (cl_float ), "SIZEOF_FLOAT" }, 250 { sizeof (cl_float ), "SIZEOF_FLOAT" },
192 { sizeof (cl_double), "SIZEOF_DOUBLE" }, 251 { sizeof (cl_double), "SIZEOF_DOUBLE" },
252 { HAVE_OPENGL , "HAVE_OPENGL" },
193#include "constiv.h" 253#include "constiv.h"
194 }; 254 };
195 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ > const_iv; civ--) 255 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)); 256 newCONSTSUB (stash, (char *)civ[-1].name, newSViv (civ[-1].iv));
197} 257}
198 258
199cl_int 259cl_int
200errno () 260errno ()
201 CODE: 261 CODE:
221 EXTEND (SP, count); 281 EXTEND (SP, count);
222 for (i = 0; i < count; ++i) 282 for (i = 0; i < count; ++i)
223 PUSHs (NEW_MORTAL_OBJ ("OpenCL::Platform", list [i])); 283 PUSHs (NEW_MORTAL_OBJ ("OpenCL::Platform", list [i]));
224 284
225void 285void
226context_from_type (FUTURE properties = 0, cl_device_type type = CL_DEVICE_TYPE_DEFAULT, FUTURE notify = 0) 286context_from_type (cl_context_properties *properties = 0, cl_device_type type = CL_DEVICE_TYPE_DEFAULT, FUTURE notify = 0)
227 PPCODE: 287 PPCODE:
228 NEED_SUCCESS_ARG (cl_context ctx, CreateContextFromType, (0, type, 0, 0, &res)); 288 NEED_SUCCESS_ARG (cl_context ctx, CreateContextFromType, (properties, type, 0, 0, &res));
229 XPUSH_NEW_OBJ ("OpenCL::Context", ctx); 289 XPUSH_NEW_OBJ ("OpenCL::Context", ctx);
230 290
231void 291void
232context (FUTURE properties, FUTURE devices, FUTURE notify = 0) 292context (cl_context_properties *properties = 0, FUTURE devices, FUTURE notify = 0)
233 PPCODE: 293 PPCODE:
234 /* der Gipfel der Kunst */ 294 /* der Gipfel der Kunst */
235 295
236void 296void
237wait_for_events (...) 297wait_for_events (...)
283 EXTEND (SP, count); 343 EXTEND (SP, count);
284 for (i = 0; i < count; ++i) 344 for (i = 0; i < count; ++i)
285 PUSHs (sv_setref_pv (sv_newmortal (), "OpenCL::Device", list [i])); 345 PUSHs (sv_setref_pv (sv_newmortal (), "OpenCL::Device", list [i]));
286 346
287void 347void
288context (OpenCL::Platform self, FUTURE properties, SV *devices, FUTURE notify = 0) 348context (OpenCL::Platform self, cl_context_properties *properties = 0, SV *devices, FUTURE notify = 0)
289 PPCODE: 349 PPCODE:
290 if (!SvROK (devices) || SvTYPE (SvRV (devices)) != SVt_PVAV) 350 if (!SvROK (devices) || SvTYPE (SvRV (devices)) != SVt_PVAV)
291 croak ("OpenCL::Platform argument 'device' must be an arrayref with device objects, in call"); 351 croak ("OpenCL::Platform argument 'device' must be an arrayref with device objects, in call");
292 352
293 AV *av = (AV *)SvRV (devices); 353 AV *av = (AV *)SvRV (devices);
296 int i; 356 int i;
297 357
298 for (i = num_devices; i--; ) 358 for (i = num_devices; i--; )
299 device_list [i] = SvPTROBJ ("clCreateContext", "devices", *av_fetch (av, i, 0), "OpenCL::Device"); 359 device_list [i] = SvPTROBJ ("clCreateContext", "devices", *av_fetch (av, i, 0), "OpenCL::Device");
300 360
301 NEED_SUCCESS_ARG (cl_context ctx, CreateContext, (0, num_devices, device_list, 0, 0, &res)); 361 NEED_SUCCESS_ARG (cl_context ctx, CreateContext, (properties, num_devices, device_list, 0, 0, &res));
302 XPUSH_NEW_OBJ ("OpenCL::Context", ctx); 362 XPUSH_NEW_OBJ ("OpenCL::Context", ctx);
303 363
304void 364void
305context_from_type (OpenCL::Platform self, FUTURE properties = 0, cl_device_type type = CL_DEVICE_TYPE_DEFAULT, FUTURE notify = 0) 365context_from_type (OpenCL::Platform self, SV *properties = 0, cl_device_type type = CL_DEVICE_TYPE_DEFAULT, FUTURE notify = 0)
306 PPCODE: 366 PPCODE:
307 cl_context_properties props[] = { CL_CONTEXT_PLATFORM, (cl_context_properties)self, 0 }; 367 cl_context_properties extra[] = { CL_CONTEXT_PLATFORM, (cl_context_properties)self };
368 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)); 369 NEED_SUCCESS_ARG (cl_context ctx, CreateContextFromType, (props, type, 0, 0, &res));
309 XPUSH_NEW_OBJ ("OpenCL::Context", ctx); 370 XPUSH_NEW_OBJ ("OpenCL::Context", ctx);
310 371
311MODULE = OpenCL PACKAGE = OpenCL::Device 372MODULE = OpenCL PACKAGE = OpenCL::Device
312 373
552 INFO (Context) 613 INFO (Context)
553 614
554void 615void
555queue (OpenCL::Context self, OpenCL::Device device, cl_command_queue_properties properties = 0) 616queue (OpenCL::Context self, OpenCL::Device device, cl_command_queue_properties properties = 0)
556 PPCODE: 617 PPCODE:
557 NEED_SUCCESS_ARG (cl_command_queue queue, CreateCommandQueue, (self, device, properties, &res)); 618 NEED_SUCCESS_ARG (cl_command_queue queue, CreateCommandQueue, (self, device, properties, &res));
558 XPUSH_NEW_OBJ ("OpenCL::Queue", queue); 619 XPUSH_NEW_OBJ ("OpenCL::Queue", queue);
559 620
560void 621void
561user_event (OpenCL::Context self) 622user_event (OpenCL::Context self)
562 PPCODE: 623 PPCODE:
563 NEED_SUCCESS_ARG (cl_event ev, CreateUserEvent, (self, &res)); 624 NEED_SUCCESS_ARG (cl_event ev, CreateUserEvent, (self, &res));
564 XPUSH_NEW_OBJ ("OpenCL::UserEvent", ev); 625 XPUSH_NEW_OBJ ("OpenCL::UserEvent", ev);
565 626
566void 627void
567buffer (OpenCL::Context self, cl_mem_flags flags, size_t len) 628buffer (OpenCL::Context self, cl_mem_flags flags, size_t len)
568 PPCODE: 629 PPCODE:
586image2d (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) 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)
587 PPCODE: 648 PPCODE:
588 STRLEN len; 649 STRLEN len;
589 char *ptr = SvOK (data) ? SvPVbyte (data, len) : 0; 650 char *ptr = SvOK (data) ? SvPVbyte (data, len) : 0;
590 const cl_image_format format = { channel_order, channel_type }; 651 const cl_image_format format = { channel_order, channel_type };
591 NEED_SUCCESS_ARG (cl_mem mem, CreateImage2D, (self, flags, &format, width, height, row_pitch, ptr, &res)); 652 NEED_SUCCESS_ARG (cl_mem mem, CreateImage2D, (self, flags, &format, width, height, row_pitch, ptr, &res));
592 XPUSH_NEW_OBJ ("OpenCL::Image2D", mem); 653 XPUSH_NEW_OBJ ("OpenCL::Image2D", mem);
593 654
594void 655void
595image3d (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) 656image3d (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)
596 PPCODE: 657 PPCODE:
597 STRLEN len; 658 STRLEN len;
598 char *ptr = SvOK (data) ? SvPVbyte (data, len) : 0; 659 char *ptr = SvOK (data) ? SvPVbyte (data, len) : 0;
599 const cl_image_format format = { channel_order, channel_type }; 660 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)); 661 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); 662 XPUSH_NEW_OBJ ("OpenCL::Image3D", mem);
602 663
603void 664void
604supported_image_formats (OpenCL::Context self, cl_mem_flags flags, cl_mem_object_type image_type) 665supported_image_formats (OpenCL::Context self, cl_mem_flags flags, cl_mem_object_type image_type)
605 PPCODE: 666 PPCODE:
606{ 667{
607 cl_uint count; 668 cl_uint count;
608 cl_image_format *list; 669 cl_image_format *list;
609 int i; 670 int i;
610 671
611 NEED_SUCCESS (GetSupportedImageFormats, (self, flags, image_type, 0, 0, &count)); 672 NEED_SUCCESS (GetSupportedImageFormats, (self, flags, image_type, 0, 0, &count));
612 Newx (list, count, cl_image_format); 673 Newx (list, count, cl_image_format);
613 NEED_SUCCESS (GetSupportedImageFormats, (self, flags, image_type, count, list, 0)); 674 NEED_SUCCESS (GetSupportedImageFormats, (self, flags, image_type, count, list, 0));
614 675
615 EXTEND (SP, count); 676 EXTEND (SP, count);
616 for (i = 0; i < count; ++i) 677 for (i = 0; i < count; ++i)
617 { 678 {
618 AV *av = newAV (); 679 AV *av = newAV ();
623} 684}
624 685
625void 686void
626sampler (OpenCL::Context self, cl_bool normalized_coords, cl_addressing_mode addressing_mode, cl_filter_mode filter_mode) 687sampler (OpenCL::Context self, cl_bool normalized_coords, cl_addressing_mode addressing_mode, cl_filter_mode filter_mode)
627 PPCODE: 688 PPCODE:
628 NEED_SUCCESS_ARG (cl_sampler sampler, CreateSampler, (self, normalized_coords, addressing_mode, filter_mode, &res)); 689 NEED_SUCCESS_ARG (cl_sampler sampler, CreateSampler, (self, normalized_coords, addressing_mode, filter_mode, &res));
629 XPUSH_NEW_OBJ ("OpenCL::Sampler", sampler); 690 XPUSH_NEW_OBJ ("OpenCL::Sampler", sampler);
630 691
631void 692void
632program_with_source (OpenCL::Context self, SV *program) 693program_with_source (OpenCL::Context self, SV *program)
633 PPCODE: 694 PPCODE:
634 STRLEN len; 695 STRLEN len;
635 size_t len2; 696 size_t len2;
636 const char *ptr = SvPVbyte (program, len); 697 const char *ptr = SvPVbyte (program, len);
637 698
638 len2 = len; 699 len2 = len;
639 NEED_SUCCESS_ARG (cl_program prog, CreateProgramWithSource, (self, 1, &ptr, &len2, &res)); 700 NEED_SUCCESS_ARG (cl_program prog, CreateProgramWithSource, (self, 1, &ptr, &len2, &res));
640 XPUSH_NEW_OBJ ("OpenCL::Program", prog); 701 XPUSH_NEW_OBJ ("OpenCL::Program", prog);
641 702
642#BEGIN:context 703#BEGIN:context
643 704
644void 705void
1246 NEED_SUCCESS (BuildProgram, (self, 1, &device, SvPVbyte_nolen (options), 0, 0)); 1307 NEED_SUCCESS (BuildProgram, (self, 1, &device, SvPVbyte_nolen (options), 0, 0));
1247 1308
1248void 1309void
1249build_info (OpenCL::Program self, OpenCL::Device device, cl_program_build_info name) 1310build_info (OpenCL::Program self, OpenCL::Device device, cl_program_build_info name)
1250 PPCODE: 1311 PPCODE:
1251 size_t size; 1312 size_t size;
1252 NEED_SUCCESS (GetProgramBuildInfo, (self, device, name, 0, 0, &size)); 1313 NEED_SUCCESS (GetProgramBuildInfo, (self, device, name, 0, 0, &size));
1253 SV *sv = sv_2mortal (newSV (size)); 1314 SV *sv = sv_2mortal (newSV (size));
1254 SvUPGRADE (sv, SVt_PV); 1315 SvUPGRADE (sv, SVt_PV);
1255 SvPOK_only (sv); 1316 SvPOK_only (sv);
1256 SvCUR_set (sv, size); 1317 SvCUR_set (sv, size);
1257 NEED_SUCCESS (GetProgramBuildInfo, (self, device, name, size, SvPVX (sv), 0)); 1318 NEED_SUCCESS (GetProgramBuildInfo, (self, device, name, size, SvPVX (sv), 0));
1258 XPUSHs (sv); 1319 XPUSHs (sv);
1259 1320
1260#BEGIN:program_build 1321#BEGIN:program_build
1261 1322
1262void 1323void
1285#END:program_build 1346#END:program_build
1286 1347
1287void 1348void
1288kernel (OpenCL::Program program, SV *function) 1349kernel (OpenCL::Program program, SV *function)
1289 PPCODE: 1350 PPCODE:
1290 NEED_SUCCESS_ARG (cl_kernel kernel, CreateKernel, (program, SvPVbyte_nolen (function), &res)); 1351 NEED_SUCCESS_ARG (cl_kernel kernel, CreateKernel, (program, SvPVbyte_nolen (function), &res));
1291 XPUSH_NEW_OBJ ("OpenCL::Kernel", kernel); 1352 XPUSH_NEW_OBJ ("OpenCL::Kernel", kernel);
1292 1353
1293void 1354void
1294info (OpenCL::Program self, cl_program_info name) 1355info (OpenCL::Program self, cl_program_info name)
1295 PPCODE: 1356 PPCODE:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines