… | |
… | |
3 | #include "XSUB.h" |
3 | #include "XSUB.h" |
4 | |
4 | |
5 | #ifdef I_DLFCN |
5 | #ifdef I_DLFCN |
6 | #include <dlfcn.h> |
6 | #include <dlfcn.h> |
7 | #endif |
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 */ |
8 | |
18 | |
9 | #ifdef __APPLE__ |
19 | #ifdef __APPLE__ |
10 | #include <OpenCL/opencl.h> |
20 | #include <OpenCL/opencl.h> |
11 | #else |
21 | #else |
12 | #include <CL/opencl.h> |
22 | #include <CL/opencl.h> |
… | |
… | |
89 | enum { buffers = 3 }; |
99 | enum { buffers = 3 }; |
90 | static int idx; |
100 | static int idx; |
91 | static void *buf [buffers]; |
101 | static void *buf [buffers]; |
92 | static size_t len [buffers]; |
102 | static size_t len [buffers]; |
93 | |
103 | |
94 | idx = ++idx % buffers; |
104 | idx = (idx + 1) % buffers; |
95 | |
105 | |
96 | if (len [idx] < size) |
106 | if (len [idx] < size) |
97 | { |
107 | { |
98 | free (buf [idx]); |
108 | free (buf [idx]); |
99 | len [idx] = ((size + 31) & ~4095) + 4096 - 32; |
109 | len [idx] = ((size + 31) & ~4095) + 4096 - 32; |
… | |
… | |
257 | clGetImageInfo (img, CL_IMAGE_ROW_PITCH, sizeof (res), &res, 0); |
267 | clGetImageInfo (img, CL_IMAGE_ROW_PITCH, sizeof (res), &res, 0); |
258 | return res; |
268 | return res; |
259 | } |
269 | } |
260 | |
270 | |
261 | static cl_event * |
271 | static cl_event * |
262 | event_list (SV **items, int count) |
272 | event_list (SV **items, cl_uint *rcount) |
263 | { |
273 | { |
|
|
274 | cl_uint count = *rcount; |
|
|
275 | |
264 | if (!count) |
276 | if (!count) |
265 | return 0; |
277 | return 0; |
266 | |
278 | |
267 | cl_event *list = tmpbuf (sizeof (cl_event) * count); |
279 | cl_event *list = tmpbuf (sizeof (cl_event) * count); |
|
|
280 | int i = 0; |
268 | |
281 | |
269 | while (count--) |
282 | do |
|
|
283 | { |
|
|
284 | --count; |
|
|
285 | if (SvOK (items [count])) |
270 | list [count] = SvPTROBJ ("clEnqueue", "wait_events", items [count], "OpenCL::Event"); |
286 | list [i++] = SvPTROBJ ("clEnqueue", "wait_events", items [count], "OpenCL::Event"); |
|
|
287 | } |
|
|
288 | while (count); |
271 | |
289 | |
|
|
290 | *rcount = i; |
|
|
291 | |
272 | return list; |
292 | return i ? list : 0; |
273 | } |
293 | } |
274 | |
294 | |
275 | #define EVENT_LIST(items,count) \ |
295 | #define EVENT_LIST(items,count) \ |
276 | cl_uint event_list_count = (count); \ |
296 | cl_uint event_list_count = (count); \ |
277 | cl_event *event_list_ptr = event_list (&ST (items), event_list_count) |
297 | cl_event *event_list_ptr = event_list (&ST (items), &event_list_count) |
278 | |
298 | |
279 | #define INFO(class) \ |
299 | #define INFO(class) \ |
280 | { \ |
300 | { \ |
281 | size_t size; \ |
301 | size_t size; \ |
282 | NEED_SUCCESS (Get ## class ## Info, (self, name, 0, 0, &size)); \ |
302 | NEED_SUCCESS (Get ## class ## Info, (self, name, 0, 0, &size)); \ |
… | |
… | |
314 | } |
334 | } |
315 | |
335 | |
316 | cl_int |
336 | cl_int |
317 | errno () |
337 | errno () |
318 | CODE: |
338 | CODE: |
319 | errno = res; |
339 | RETVAL = res; |
|
|
340 | OUTPUT: |
|
|
341 | RETVAL |
320 | |
342 | |
321 | const char * |
343 | const char * |
322 | err2str (cl_int err) |
344 | err2str (cl_int err) |
323 | |
345 | |
324 | const char * |
346 | const char * |
… | |
… | |
343 | context_from_type (cl_context_properties *properties = 0, cl_device_type type = CL_DEVICE_TYPE_DEFAULT, FUTURE notify = 0) |
365 | context_from_type (cl_context_properties *properties = 0, cl_device_type type = CL_DEVICE_TYPE_DEFAULT, FUTURE notify = 0) |
344 | PPCODE: |
366 | PPCODE: |
345 | NEED_SUCCESS_ARG (cl_context ctx, CreateContextFromType, (properties, type, 0, 0, &res)); |
367 | NEED_SUCCESS_ARG (cl_context ctx, CreateContextFromType, (properties, type, 0, 0, &res)); |
346 | XPUSH_NEW_OBJ ("OpenCL::Context", ctx); |
368 | XPUSH_NEW_OBJ ("OpenCL::Context", ctx); |
347 | |
369 | |
|
|
370 | #if 0 |
|
|
371 | |
348 | void |
372 | void |
349 | context (cl_context_properties *properties = 0, FUTURE devices, FUTURE notify = 0) |
373 | context (cl_context_properties *properties = 0, FUTURE devices, FUTURE notify = 0) |
350 | PPCODE: |
374 | PPCODE: |
351 | /* der Gipfel der Kunst */ |
375 | /* der Gipfel der Kunst */ |
|
|
376 | |
|
|
377 | #endif |
352 | |
378 | |
353 | void |
379 | void |
354 | wait_for_events (...) |
380 | wait_for_events (...) |
355 | CODE: |
381 | CODE: |
356 | EVENT_LIST (0, items); |
382 | EVENT_LIST (0, items); |
… | |
… | |
698 | if (!(flags & (CL_MEM_USE_HOST_PTR | CL_MEM_COPY_HOST_PTR))) |
724 | if (!(flags & (CL_MEM_USE_HOST_PTR | CL_MEM_COPY_HOST_PTR))) |
699 | croak ("OpenCL::Context::buffer_sv: you have to specify use or copy host ptr when buffer data is given, use $context->buffer instead?"); |
725 | croak ("OpenCL::Context::buffer_sv: you have to specify use or copy host ptr when buffer data is given, use $context->buffer instead?"); |
700 | NEED_SUCCESS_ARG (cl_mem mem, CreateBuffer, (self, flags, len, ptr, &res)); |
726 | NEED_SUCCESS_ARG (cl_mem mem, CreateBuffer, (self, flags, len, ptr, &res)); |
701 | XPUSH_NEW_OBJ ("OpenCL::BufferObj", mem); |
727 | XPUSH_NEW_OBJ ("OpenCL::BufferObj", mem); |
702 | |
728 | |
|
|
729 | #if !defined CL_VERSION_1_2 || defined CL_USE_DEPRECATED_OPENCL_1_1_APIS |
|
|
730 | |
703 | void |
731 | void |
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) |
732 | 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) |
705 | PPCODE: |
733 | PPCODE: |
706 | STRLEN len; |
734 | STRLEN len; |
707 | char *ptr = SvOK (data) ? SvPVbyte (data, len) : 0; |
735 | char *ptr = SvOK (data) ? SvPVbyte (data, len) : 0; |
… | |
… | |
716 | char *ptr = SvOK (data) ? SvPVbyte (data, len) : 0; |
744 | char *ptr = SvOK (data) ? SvPVbyte (data, len) : 0; |
717 | const cl_image_format format = { channel_order, channel_type }; |
745 | const cl_image_format format = { channel_order, channel_type }; |
718 | NEED_SUCCESS_ARG (cl_mem mem, CreateImage3D, (self, flags, &format, width, height, depth, row_pitch, slice_pitch, ptr, &res)); |
746 | NEED_SUCCESS_ARG (cl_mem mem, CreateImage3D, (self, flags, &format, width, height, depth, row_pitch, slice_pitch, ptr, &res)); |
719 | XPUSH_NEW_OBJ ("OpenCL::Image3D", mem); |
747 | XPUSH_NEW_OBJ ("OpenCL::Image3D", mem); |
720 | |
748 | |
|
|
749 | #endif |
|
|
750 | |
721 | #if cl_apple_gl_sharing || cl_khr_gl_sharing |
751 | #if cl_apple_gl_sharing || cl_khr_gl_sharing |
722 | |
752 | |
723 | void |
753 | void |
724 | gl_buffer (OpenCL::Context self, cl_mem_flags flags, cl_GLuint bufobj) |
754 | gl_buffer (OpenCL::Context self, cl_mem_flags flags, cl_GLuint bufobj) |
725 | PPCODE: |
755 | PPCODE: |
726 | NEED_SUCCESS_ARG (cl_mem mem, CreateFromGLBuffer, (self, flags, bufobj, &res)); |
756 | NEED_SUCCESS_ARG (cl_mem mem, CreateFromGLBuffer, (self, flags, bufobj, &res)); |
727 | XPUSH_NEW_OBJ ("OpenCL::BufferObj", mem); |
757 | XPUSH_NEW_OBJ ("OpenCL::BufferObj", mem); |
728 | |
758 | |
|
|
759 | #if !defined CL_VERSION_1_2 || defined CL_USE_DEPRECATED_OPENCL_1_1_APIS |
|
|
760 | |
729 | void |
761 | void |
730 | gl_texture2d (OpenCL::Context self, cl_mem_flags flags, cl_GLenum target, cl_GLint miplevel, cl_GLuint texture) |
762 | gl_texture2d (OpenCL::Context self, cl_mem_flags flags, cl_GLenum target, cl_GLint miplevel, cl_GLuint texture) |
731 | PPCODE: |
763 | PPCODE: |
732 | NEED_SUCCESS_ARG (cl_mem mem, CreateFromGLTexture2D, (self, flags, target, miplevel, texture, &res)); |
764 | NEED_SUCCESS_ARG (cl_mem mem, CreateFromGLTexture2D, (self, flags, target, miplevel, texture, &res)); |
733 | XPUSH_NEW_OBJ ("OpenCL::Image2D", mem); |
765 | XPUSH_NEW_OBJ ("OpenCL::Image2D", mem); |
… | |
… | |
735 | void |
767 | void |
736 | gl_texture3d (OpenCL::Context self, cl_mem_flags flags, cl_GLenum target, cl_GLint miplevel, cl_GLuint texture) |
768 | gl_texture3d (OpenCL::Context self, cl_mem_flags flags, cl_GLenum target, cl_GLint miplevel, cl_GLuint texture) |
737 | PPCODE: |
769 | PPCODE: |
738 | NEED_SUCCESS_ARG (cl_mem mem, CreateFromGLTexture3D, (self, flags, target, miplevel, texture, &res)); |
770 | NEED_SUCCESS_ARG (cl_mem mem, CreateFromGLTexture3D, (self, flags, target, miplevel, texture, &res)); |
739 | XPUSH_NEW_OBJ ("OpenCL::Image3D", mem); |
771 | XPUSH_NEW_OBJ ("OpenCL::Image3D", mem); |
|
|
772 | |
|
|
773 | #endif |
740 | |
774 | |
741 | void |
775 | void |
742 | gl_renderbuffer (OpenCL::Context self, cl_mem_flags flags, cl_GLuint renderbuffer) |
776 | gl_renderbuffer (OpenCL::Context self, cl_mem_flags flags, cl_GLuint renderbuffer) |
743 | PPCODE: |
777 | PPCODE: |
744 | NEED_SUCCESS_ARG (cl_mem mem, CreateFromGLRenderbuffer, (self, flags, renderbuffer, &res)); |
778 | NEED_SUCCESS_ARG (cl_mem mem, CreateFromGLRenderbuffer, (self, flags, renderbuffer, &res)); |
… | |
… | |
931 | size_t min_len = host_row_pitch * host_slice_pitch * region [2]; |
965 | size_t min_len = host_row_pitch * host_slice_pitch * region [2]; |
932 | |
966 | |
933 | if (len < min_len) |
967 | if (len < min_len) |
934 | croak ("clEnqueueWriteImage: data string is shorter than what would be transferred"); |
968 | croak ("clEnqueueWriteImage: data string is shorter than what would be transferred"); |
935 | |
969 | |
936 | 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)); |
970 | 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)); |
937 | |
971 | |
938 | if (ev) |
972 | if (ev) |
939 | XPUSH_NEW_OBJ ("OpenCL::Event", ev); |
973 | XPUSH_NEW_OBJ ("OpenCL::Event", ev); |
940 | |
974 | |
941 | void |
975 | void |
… | |
… | |
996 | size_t min_len = slice_pitch ? slice_pitch * depth : row_pitch * height; |
1030 | size_t min_len = slice_pitch ? slice_pitch * depth : row_pitch * height; |
997 | |
1031 | |
998 | if (len < min_len) |
1032 | if (len < min_len) |
999 | croak ("clEnqueueWriteImage: data string is shorter than what would be transferred"); |
1033 | croak ("clEnqueueWriteImage: data string is shorter than what would be transferred"); |
1000 | |
1034 | |
1001 | 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)); |
1035 | 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)); |
1002 | |
1036 | |
1003 | if (ev) |
1037 | if (ev) |
1004 | XPUSH_NEW_OBJ ("OpenCL::Event", ev); |
1038 | XPUSH_NEW_OBJ ("OpenCL::Event", ev); |
1005 | |
1039 | |
1006 | void |
1040 | void |
… | |
… | |
1088 | gwo [i] = SvIV (AvARRAY (SvRV (global_work_offset))[i]); |
1122 | gwo [i] = SvIV (AvARRAY (SvRV (global_work_offset))[i]); |
1089 | } |
1123 | } |
1090 | |
1124 | |
1091 | if (SvOK (local_work_size)) |
1125 | if (SvOK (local_work_size)) |
1092 | { |
1126 | { |
1093 | if (SvOK (local_work_size) && !SvROK (local_work_size) || SvTYPE (SvRV (local_work_size)) != SVt_PVAV) |
1127 | if ((SvOK (local_work_size) && !SvROK (local_work_size)) || SvTYPE (SvRV (local_work_size)) != SVt_PVAV) |
1094 | croak ("clEnqueueNDRangeKernel: global_work_size must be undef or an array reference"); |
1128 | croak ("clEnqueueNDRangeKernel: global_work_size must be undef or an array reference"); |
1095 | |
1129 | |
1096 | if (AvFILLp (SvRV (local_work_size)) + 1 != gws_len) |
1130 | if (AvFILLp (SvRV (local_work_size)) + 1 != gws_len) |
1097 | croak ("clEnqueueNDRangeKernel: local_work_local must be undef or an array of same size as global_work_size"); |
1131 | croak ("clEnqueueNDRangeKernel: local_work_local must be undef or an array of same size as global_work_size"); |
1098 | |
1132 | |
… | |
… | |
1110 | |
1144 | |
1111 | void |
1145 | void |
1112 | enqueue_acquire_gl_objects (OpenCL::Queue self, SV *objects, ...) |
1146 | enqueue_acquire_gl_objects (OpenCL::Queue self, SV *objects, ...) |
1113 | ALIAS: |
1147 | ALIAS: |
1114 | enqueue_release_gl_objects = 1 |
1148 | enqueue_release_gl_objects = 1 |
1115 | CODE: |
1149 | PPCODE: |
1116 | if (!SvROK (objects) || SvTYPE (SvRV (objects)) != SVt_PVAV) |
1150 | 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"); |
1151 | croak ("OpenCL::Queue::enqueue_acquire/release_gl_objects argument 'objects' must be an arrayref with memory objects, in call"); |
1118 | |
1152 | |
1119 | cl_event ev = 0; |
1153 | cl_event ev = 0; |
1120 | EVENT_LIST (2, items - 2); |
1154 | EVENT_LIST (2, items - 2); |
… | |
… | |
1134 | if (ev) |
1168 | if (ev) |
1135 | XPUSH_NEW_OBJ ("OpenCL::Event", ev); |
1169 | XPUSH_NEW_OBJ ("OpenCL::Event", ev); |
1136 | |
1170 | |
1137 | #endif |
1171 | #endif |
1138 | |
1172 | |
|
|
1173 | #if !defined CL_VERSION_1_2 || defined CL_USE_DEPRECATED_OPENCL_1_1_APIS |
|
|
1174 | |
1139 | void |
1175 | void |
1140 | enqueue_marker (OpenCL::Queue self) |
1176 | enqueue_marker (OpenCL::Queue self) |
1141 | PPCODE: |
1177 | PPCODE: |
1142 | cl_event ev; |
1178 | cl_event ev; |
1143 | NEED_SUCCESS (EnqueueMarker, (self, &ev)); |
1179 | NEED_SUCCESS (EnqueueMarker, (self, &ev)); |
… | |
… | |
1151 | |
1187 | |
1152 | void |
1188 | void |
1153 | enqueue_barrier (OpenCL::Queue self) |
1189 | enqueue_barrier (OpenCL::Queue self) |
1154 | CODE: |
1190 | CODE: |
1155 | NEED_SUCCESS (EnqueueBarrier, (self)); |
1191 | NEED_SUCCESS (EnqueueBarrier, (self)); |
|
|
1192 | |
|
|
1193 | #endif |
1156 | |
1194 | |
1157 | void |
1195 | void |
1158 | flush (OpenCL::Queue self) |
1196 | flush (OpenCL::Queue self) |
1159 | CODE: |
1197 | CODE: |
1160 | NEED_SUCCESS (Flush, (self)); |
1198 | NEED_SUCCESS (Flush, (self)); |
… | |
… | |
1530 | { |
1568 | { |
1531 | SV *sv = sv_2mortal (newSV (sizes [i])); |
1569 | SV *sv = sv_2mortal (newSV (sizes [i])); |
1532 | SvUPGRADE (sv, SVt_PV); |
1570 | SvUPGRADE (sv, SVt_PV); |
1533 | SvPOK_only (sv); |
1571 | SvPOK_only (sv); |
1534 | SvCUR_set (sv, sizes [i]); |
1572 | SvCUR_set (sv, sizes [i]); |
1535 | ptrs [i] = SvPVX (sv); |
1573 | ptrs [i] = (void *)SvPVX (sv); |
1536 | PUSHs (sv); |
1574 | PUSHs (sv); |
1537 | } |
1575 | } |
1538 | |
1576 | |
1539 | NEED_SUCCESS (GetProgramInfo, (self, CL_PROGRAM_BINARIES , sizeof (*ptrs ) * n, ptrs , &size)); |
1577 | NEED_SUCCESS (GetProgramInfo, (self, CL_PROGRAM_BINARIES , sizeof (*ptrs ) * n, ptrs , &size)); |
1540 | if (size != sizeof (*ptrs) * n) XSRETURN_EMPTY; |
1578 | if (size != sizeof (*ptrs) * n) XSRETURN_EMPTY; |