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 | |
32 | typedef SV *FUTURE; |
36 | typedef SV *FUTURE; |
33 | |
37 | |
34 | /*****************************************************************************/ |
38 | /*****************************************************************************/ |
35 | |
39 | |
|
|
40 | // name must include a leading underscore |
|
|
41 | static void * |
|
|
42 | getsym (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 | |
36 | /* up to two temporary buffers */ |
56 | /* up to two temporary buffers */ |
37 | static void * |
57 | static void * |
38 | tmpbuf (size_t size) |
58 | tmpbuf (size_t size) |
39 | { |
59 | { |
40 | enum { buffers = 3 }; |
60 | enum { buffers = 3 }; |
… | |
… | |
144 | return 0; |
164 | return 0; |
145 | |
165 | |
146 | if (SvROK (sv) && SvTYPE (SvRV (sv)) == SVt_PVAV) |
166 | if (SvROK (sv) && SvTYPE (SvRV (sv)) == SVt_PVAV) |
147 | { |
167 | { |
148 | AV *av = (AV *)SvRV (sv); |
168 | AV *av = (AV *)SvRV (sv); |
149 | int i, len = av_len (av); |
169 | int i, len = av_len (av) + 1; |
150 | 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)); |
151 | cl_context_properties *l = p; |
171 | cl_context_properties *l = p; |
152 | |
172 | |
153 | if (len & 1) |
173 | if (len & 1) |
154 | 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); |
… | |
… | |
156 | while (extracount--) |
176 | while (extracount--) |
157 | *l++ = *extra++; |
177 | *l++ = *extra++; |
158 | |
178 | |
159 | for (i = 0; i < len; i += 2) |
179 | for (i = 0; i < len; i += 2) |
160 | { |
180 | { |
161 | 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); |
162 | cl_context_properties v; |
183 | cl_context_properties v; |
163 | |
184 | |
164 | switch (t) |
185 | switch (t) |
165 | { |
186 | { |
166 | default: |
187 | default: |
167 | /* unknown property, treat as int */ |
188 | /* unknown property, treat as int */ |
168 | v = SvIV (*av_fetch (av, i, 0)); |
189 | v = SvIV (p_sv); |
169 | break; |
190 | break; |
170 | } |
191 | } |
171 | |
192 | |
172 | *l++ = t; |
193 | *l++ = t; |
173 | *l++ = v; |
194 | *l++ = v; |
… | |
… | |
336 | |
357 | |
337 | void |
358 | void |
338 | context (OpenCL::Platform self, cl_context_properties *properties = 0, SV *devices, FUTURE notify = 0) |
359 | context (OpenCL::Platform self, cl_context_properties *properties = 0, SV *devices, FUTURE notify = 0) |
339 | PPCODE: |
360 | PPCODE: |
340 | if (!SvROK (devices) || SvTYPE (SvRV (devices)) != SVt_PVAV) |
361 | if (!SvROK (devices) || SvTYPE (SvRV (devices)) != SVt_PVAV) |
341 | 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"); |
342 | |
363 | |
343 | AV *av = (AV *)SvRV (devices); |
364 | AV *av = (AV *)SvRV (devices); |
344 | cl_uint num_devices = av_len (av) + 1; |
365 | cl_uint num_devices = av_len (av) + 1; |
345 | 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); |
346 | int i; |
367 | int i; |
… | |
… | |
616 | |
637 | |
617 | void |
638 | void |
618 | buffer (OpenCL::Context self, cl_mem_flags flags, size_t len) |
639 | buffer (OpenCL::Context self, cl_mem_flags flags, size_t len) |
619 | PPCODE: |
640 | PPCODE: |
620 | 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)) |
621 | 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?"); |
622 | |
643 | |
623 | 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)); |
624 | XPUSH_NEW_OBJ ("OpenCL::BufferObj", mem); |
645 | XPUSH_NEW_OBJ ("OpenCL::BufferObj", mem); |
625 | |
646 | |
626 | void |
647 | void |
627 | buffer_sv (OpenCL::Context self, cl_mem_flags flags, SV *data) |
648 | buffer_sv (OpenCL::Context self, cl_mem_flags flags, SV *data) |
628 | PPCODE: |
649 | PPCODE: |
629 | STRLEN len; |
650 | STRLEN len; |
630 | char *ptr = SvOK (data) ? SvPVbyte (data, len) : 0; |
651 | char *ptr = SvOK (data) ? SvPVbyte (data, len) : 0; |
631 | 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))) |
632 | 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?"); |
633 | 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)); |
634 | XPUSH_NEW_OBJ ("OpenCL::BufferObj", mem); |
655 | XPUSH_NEW_OBJ ("OpenCL::BufferObj", mem); |
635 | |
656 | |
636 | void |
657 | void |
637 | 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 | 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) |
… | |
… | |
672 | XPUSH_NEW_OBJ ("OpenCL::Image3D", mem); |
693 | XPUSH_NEW_OBJ ("OpenCL::Image3D", mem); |
673 | |
694 | |
674 | void |
695 | void |
675 | gl_renderbuffer (OpenCL::Context self, cl_mem_flags flags, cl_GLuint renderbuffer) |
696 | gl_renderbuffer (OpenCL::Context self, cl_mem_flags flags, cl_GLuint renderbuffer) |
676 | PPCODE: |
697 | PPCODE: |
677 | NEED_SUCCESS_ARG (cl_mem mem, CreateFromGLRenderBuffer, (self, flags, renderbuffer, &res)); |
698 | NEED_SUCCESS_ARG (cl_mem mem, CreateFromGLRenderbuffer, (self, flags, renderbuffer, &res)); |
678 | XPUSH_NEW_OBJ ("OpenCL::Image2D", mem); |
699 | XPUSH_NEW_OBJ ("OpenCL::Image2D", mem); |
679 | |
700 | |
680 | #endif |
701 | #endif |
681 | |
702 | |
682 | void |
703 | void |
… | |
… | |
1037 | 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)); |
1038 | |
1059 | |
1039 | if (ev) |
1060 | if (ev) |
1040 | XPUSH_NEW_OBJ ("OpenCL::Event", ev); |
1061 | XPUSH_NEW_OBJ ("OpenCL::Event", ev); |
1041 | |
1062 | |
1042 | #if 0 |
|
|
1043 | #if cl_apple_gl_sharing || cl_khr_gl_sharing |
1063 | #if cl_apple_gl_sharing || cl_khr_gl_sharing |
1044 | |
1064 | |
1045 | void |
1065 | void |
1046 | enqueue_acquire_gl_objects (OpenCL::Queue self, SV *objects, ...) |
1066 | enqueue_acquire_gl_objects (OpenCL::Queue self, SV *objects, ...) |
|
|
1067 | ALIAS: |
|
|
1068 | enqueue_release_gl_objects = 1 |
1047 | CODE: |
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 | |
1048 | cl_event ev = 0; |
1073 | cl_event ev = 0; |
1049 | EVENT_LIST (2, items - 2); |
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; |
1050 | |
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 |
1051 | NEED_SUCCESS (EnqueueAcquireGLObjects, (self, ..., event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0)); |
1086 | NEED_SUCCESS (EnqueueAcquireGLObjects, (self, num_objects, object_list, event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0)); |
1052 | |
1087 | |
1053 | if (ev) |
1088 | if (ev) |
1054 | XPUSH_NEW_OBJ ("OpenCL::Event", ev); |
1089 | XPUSH_NEW_OBJ ("OpenCL::Event", ev); |
1055 | |
1090 | |
1056 | void |
|
|
1057 | enqueue_release_gl_objects (OpenCL::Queue self, SV *objects, ...) |
|
|
1058 | CODE: |
|
|
1059 | cl_event ev = 0; |
|
|
1060 | EVENT_LIST (2, items - 2); |
|
|
1061 | |
|
|
1062 | NEED_SUCCESS (EnqueueAcquireGLObjects, (self, ..., event_list_count, event_list_ptr, GIMME_V != G_VOID ? &ev : 0)); |
|
|
1063 | |
|
|
1064 | if (ev) |
|
|
1065 | XPUSH_NEW_OBJ ("OpenCL::Event", ev); |
|
|
1066 | |
|
|
1067 | #endif |
|
|
1068 | #endif |
1091 | #endif |
1069 | |
1092 | |
1070 | void |
1093 | void |
1071 | enqueue_marker (OpenCL::Queue self) |
1094 | enqueue_marker (OpenCL::Queue self) |
1072 | PPCODE: |
1095 | PPCODE: |
… | |
… | |
1233 | NEED_SUCCESS (RetainMemObject, (value [i])); |
1256 | NEED_SUCCESS (RetainMemObject, (value [i])); |
1234 | PUSHs (NEW_MORTAL_OBJ ("OpenCL::Memory", value [i])); |
1257 | PUSHs (NEW_MORTAL_OBJ ("OpenCL::Memory", value [i])); |
1235 | } |
1258 | } |
1236 | |
1259 | |
1237 | #END:mem |
1260 | #END:mem |
|
|
1261 | |
|
|
1262 | #if cl_apple_gl_sharing || cl_khr_gl_sharing |
|
|
1263 | |
|
|
1264 | void |
|
|
1265 | gl_object_info (OpenCL::Memory self) |
|
|
1266 | PPCODE: |
|
|
1267 | cl_gl_object_type type; |
|
|
1268 | cl_GLuint name; |
|
|
1269 | NEED_SUCCESS (GetGLObjectInfo, (self, &type, &name)); |
|
|
1270 | EXTEND (SP, 2); |
|
|
1271 | PUSHs (sv_2mortal (newSVuv (type))); |
|
|
1272 | PUSHs (sv_2mortal (newSVuv (name))); |
|
|
1273 | |
|
|
1274 | #endif |
1238 | |
1275 | |
1239 | MODULE = OpenCL PACKAGE = OpenCL::BufferObj |
1276 | MODULE = OpenCL PACKAGE = OpenCL::BufferObj |
1240 | |
1277 | |
1241 | void |
1278 | void |
1242 | sub_buffer_region (OpenCL::BufferObj self, cl_mem_flags flags, size_t origin, size_t size) |
1279 | sub_buffer_region (OpenCL::BufferObj self, cl_mem_flags flags, size_t origin, size_t size) |
… | |
… | |
1273 | EXTEND (SP, 1); |
1310 | EXTEND (SP, 1); |
1274 | const int i = 0; |
1311 | const int i = 0; |
1275 | PUSHs (sv_2mortal (newSVuv (value [i]))); |
1312 | PUSHs (sv_2mortal (newSVuv (value [i]))); |
1276 | |
1313 | |
1277 | #END:image |
1314 | #END:image |
|
|
1315 | |
|
|
1316 | #if cl_apple_gl_sharing || cl_khr_gl_sharing |
|
|
1317 | |
|
|
1318 | #BEGIN:gl_texture |
|
|
1319 | |
|
|
1320 | void |
|
|
1321 | target (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 | |
|
|
1329 | void |
|
|
1330 | gl_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 |
1278 | |
1341 | |
1279 | MODULE = OpenCL PACKAGE = OpenCL::Sampler |
1342 | MODULE = OpenCL PACKAGE = OpenCL::Sampler |
1280 | |
1343 | |
1281 | void |
1344 | void |
1282 | DESTROY (OpenCL::Sampler self) |
1345 | DESTROY (OpenCL::Sampler self) |