Merge branch 'master' into blender2.8
[blender.git] / extern / clew / src / clew.c
1 //////////////////////////////////////////////////////////////////////////
2 //  Copyright (c) 2009 Organic Vectory B.V.
3 //  Written by George van Venrooij
4 //
5 //  Distributed under the Boost Software License, Version 1.0.
6 //  (See accompanying file license.txt)
7 //////////////////////////////////////////////////////////////////////////
8
9 #include "clew.h"
10
11 #ifdef _WIN32
12     #define WIN32_LEAN_AND_MEAN
13     #define VC_EXTRALEAN
14     #include <windows.h>
15
16     typedef HMODULE             CLEW_DYNLIB_HANDLE;
17
18     #define CLEW_DYNLIB_OPEN    LoadLibrary
19     #define CLEW_DYNLIB_CLOSE   FreeLibrary
20     #define CLEW_DYNLIB_IMPORT  GetProcAddress
21 #else
22     #include <dlfcn.h>
23     
24     typedef void*                   CLEW_DYNLIB_HANDLE;
25
26     #define CLEW_DYNLIB_OPEN(path)  dlopen(path, RTLD_NOW | RTLD_GLOBAL)
27     #define CLEW_DYNLIB_CLOSE       dlclose
28     #define CLEW_DYNLIB_IMPORT      dlsym
29 #endif
30
31 #include <stdlib.h>
32
33 //! \brief module handle
34 static CLEW_DYNLIB_HANDLE module = NULL;
35
36 //  Variables holding function entry points
37 PFNCLGETPLATFORMIDS                 __clewGetPlatformIDs                = NULL;
38 PFNCLGETPLATFORMINFO                __clewGetPlatformInfo               = NULL;
39 PFNCLGETDEVICEIDS                   __clewGetDeviceIDs                  = NULL;
40 PFNCLGETDEVICEINFO                  __clewGetDeviceInfo                 = NULL;
41 PFNCLCREATESUBDEVICES               __clewCreateSubDevices              = NULL;
42 PFNCLRETAINDEVICE                   __clewRetainDevice                  = NULL;
43 PFNCLRELEASEDEVICE                  __clewReleaseDevice                 = NULL;
44 PFNCLCREATECONTEXT                  __clewCreateContext                 = NULL;
45 PFNCLCREATECONTEXTFROMTYPE          __clewCreateContextFromType         = NULL;
46 PFNCLRETAINCONTEXT                  __clewRetainContext                 = NULL;
47 PFNCLRELEASECONTEXT                 __clewReleaseContext                = NULL;
48 PFNCLGETCONTEXTINFO                 __clewGetContextInfo                = NULL;
49 PFNCLCREATECOMMANDQUEUE             __clewCreateCommandQueue            = NULL;
50 PFNCLRETAINCOMMANDQUEUE             __clewRetainCommandQueue            = NULL;
51 PFNCLRELEASECOMMANDQUEUE            __clewReleaseCommandQueue           = NULL;
52 PFNCLGETCOMMANDQUEUEINFO            __clewGetCommandQueueInfo           = NULL;
53 #ifdef CL_USE_DEPRECATED_OPENCL_1_0_APIS
54 PFNCLSETCOMMANDQUEUEPROPERTY        __clewSetCommandQueueProperty       = NULL;
55 #endif
56 PFNCLCREATEBUFFER                   __clewCreateBuffer                  = NULL;
57 PFNCLCREATESUBBUFFER                __clewCreateSubBuffer               = NULL;
58 PFNCLCREATEIMAGE                    __clewCreateImage                   = NULL;
59 PFNCLRETAINMEMOBJECT                __clewRetainMemObject               = NULL;
60 PFNCLRELEASEMEMOBJECT               __clewReleaseMemObject              = NULL;
61 PFNCLGETSUPPORTEDIMAGEFORMATS       __clewGetSupportedImageFormats      = NULL;
62 PFNCLGETMEMOBJECTINFO               __clewGetMemObjectInfo              = NULL;
63 PFNCLGETIMAGEINFO                   __clewGetImageInfo                  = NULL;
64 PFNCLSETMEMOBJECTDESTRUCTORCALLBACK __clewSetMemObjectDestructorCallback = NULL;
65 PFNCLCREATESAMPLER                  __clewCreateSampler                 = NULL;
66 PFNCLRETAINSAMPLER                  __clewRetainSampler                 = NULL;
67 PFNCLRELEASESAMPLER                 __clewReleaseSampler                = NULL;
68 PFNCLGETSAMPLERINFO                 __clewGetSamplerInfo                = NULL;
69 PFNCLCREATEPROGRAMWITHSOURCE        __clewCreateProgramWithSource       = NULL;
70 PFNCLCREATEPROGRAMWITHBINARY        __clewCreateProgramWithBinary       = NULL;
71 PFNCLCREATEPROGRAMWITHBUILTINKERNELS __clewCreateProgramWithBuiltInKernels = NULL;
72 PFNCLRETAINPROGRAM                  __clewRetainProgram                 = NULL;
73 PFNCLRELEASEPROGRAM                 __clewReleaseProgram                = NULL;
74 PFNCLBUILDPROGRAM                   __clewBuildProgram                  = NULL;
75 PFNCLGETPROGRAMINFO                 __clewGetProgramInfo                = NULL;
76 PFNCLGETPROGRAMBUILDINFO            __clewGetProgramBuildInfo           = NULL;
77 PFNCLCREATEKERNEL                   __clewCreateKernel                  = NULL;
78 PFNCLCREATEKERNELSINPROGRAM         __clewCreateKernelsInProgram        = NULL;
79 PFNCLRETAINKERNEL                   __clewRetainKernel                  = NULL;
80 PFNCLRELEASEKERNEL                  __clewReleaseKernel                 = NULL;
81 PFNCLSETKERNELARG                   __clewSetKernelArg                  = NULL;
82 PFNCLGETKERNELINFO                  __clewGetKernelInfo                 = NULL;
83 PFNCLGETKERNELWORKGROUPINFO         __clewGetKernelWorkGroupInfo        = NULL;
84 PFNCLWAITFOREVENTS                  __clewWaitForEvents                 = NULL;
85 PFNCLGETEVENTINFO                   __clewGetEventInfo                  = NULL;
86 PFNCLCREATEUSEREVENT                __clewCreateUserEvent               = NULL;
87 PFNCLRETAINEVENT                    __clewRetainEvent                   = NULL;
88 PFNCLRELEASEEVENT                   __clewReleaseEvent                  = NULL;
89 PFNCLSETUSEREVENTSTATUS             __clewSetUserEventStatus            = NULL;
90 PFNCLSETEVENTCALLBACK               __clewSetEventCallback              = NULL;
91 PFNCLGETEVENTPROFILINGINFO          __clewGetEventProfilingInfo         = NULL;
92 PFNCLFLUSH                          __clewFlush                         = NULL;
93 PFNCLFINISH                         __clewFinish                        = NULL;
94 PFNCLENQUEUEREADBUFFER              __clewEnqueueReadBuffer             = NULL;
95 PFNCLENQUEUEREADBUFFERRECT          __clewEnqueueReadBufferRect         = NULL;
96 PFNCLENQUEUEWRITEBUFFER             __clewEnqueueWriteBuffer            = NULL;
97 PFNCLENQUEUEWRITEBUFFERRECT         __clewEnqueueWriteBufferRect        = NULL;
98 PFNCLENQUEUECOPYBUFFER              __clewEnqueueCopyBuffer             = NULL;
99 PFNCLENQUEUEREADIMAGE               __clewEnqueueReadImage              = NULL;
100 PFNCLENQUEUEWRITEIMAGE              __clewEnqueueWriteImage             = NULL;
101 PFNCLENQUEUECOPYIMAGE               __clewEnqueueCopyImage              = NULL;
102 PFNCLENQUEUECOPYBUFFERRECT          __clewEnqueueCopyBufferRect         = NULL;
103 PFNCLENQUEUECOPYIMAGETOBUFFER       __clewEnqueueCopyImageToBuffer      = NULL;
104 PFNCLENQUEUECOPYBUFFERTOIMAGE       __clewEnqueueCopyBufferToImage      = NULL;
105 PFNCLENQUEUEMAPBUFFER               __clewEnqueueMapBuffer              = NULL;
106 PFNCLENQUEUEMAPIMAGE                __clewEnqueueMapImage               = NULL;
107 PFNCLENQUEUEUNMAPMEMOBJECT          __clewEnqueueUnmapMemObject         = NULL;
108 PFNCLENQUEUENDRANGEKERNEL           __clewEnqueueNDRangeKernel          = NULL;
109 PFNCLENQUEUETASK                    __clewEnqueueTask                   = NULL;
110 PFNCLENQUEUENATIVEKERNEL            __clewEnqueueNativeKernel           = NULL;
111
112
113
114 PFNCLGETEXTENSIONFUNCTIONADDRESSFORPLATFORM __clewGetExtensionFunctionAddressForPlatform = NULL;
115
116 #ifdef CL_USE_DEPRECATED_OPENCL_1_1_APIS
117 PFNCLCREATEIMAGE2D                  __clewCreateImage2D                 = NULL;
118 PFNCLCREATEIMAGE3D                  __clewCreateImage3D                 = NULL;
119 PFNCLENQUEUEMARKER                  __clewEnqueueMarker                 = NULL;
120 PFNCLENQUEUEWAITFOREVENTS           __clewEnqueueWaitForEvents          = NULL;
121 PFNCLENQUEUEBARRIER                 __clewEnqueueBarrier                = NULL;
122 PFNCLUNLOADCOMPILER                 __clewUnloadCompiler                = NULL;
123 PFNCLGETEXTENSIONFUNCTIONADDRESS    __clewGetExtensionFunctionAddress   = NULL;
124 #endif
125
126 /* cl_gl */
127 PFNCLCREATEFROMGLBUFFER             __clewCreateFromGLBuffer            = NULL;
128 PFNCLCREATEFROMGLTEXTURE            __clewCreateFromGLTexture           = NULL;
129 PFNCLCREATEFROMGLRENDERBUFFER       __clewCreateFromGLRenderbuffer      = NULL;
130 PFNCLGETGLOBJECTINFO                __clewGetGLObjectInfo               = NULL;
131 PFNCLGETGLTEXTUREINFO               __clewGetGLTextureInfo              = NULL;
132 PFNCLENQUEUEACQUIREGLOBJECTS        __clewEnqueueAcquireGLObjects       = NULL;
133 PFNCLENQUEUERELEASEGLOBJECTS        __clewEnqueueReleaseGLObjects       = NULL;
134 #ifdef CL_USE_DEPRECATED_OPENCL_1_1_APIS
135 PFNCLCREATEFROMGLTEXTURE2D          __clewCreateFromGLTexture2D         = NULL;
136 PFNCLCREATEFROMGLTEXTURE3D          __clewCreateFromGLTexture3D         = NULL;
137 #endif
138 PFNCLGETGLCONTEXTINFOKHR            __clewGetGLContextInfoKHR           = NULL;
139
140 static CLEW_DYNLIB_HANDLE dynamic_library_open_find(const char **paths) {
141   int i = 0;
142   while (paths[i] != NULL) {
143       CLEW_DYNLIB_HANDLE lib = CLEW_DYNLIB_OPEN(paths[i]);
144       if (lib != NULL) {
145         return lib;
146       }
147       ++i;
148   }
149   return NULL;
150 }
151
152 static void clewExit(void)
153 {
154     if (module != NULL)
155     {
156         //  Ignore errors
157         CLEW_DYNLIB_CLOSE(module);
158         module = NULL;
159     }
160 }
161
162 int clewInit()
163 {
164 #ifdef _WIN32
165     const char *paths[] = {"OpenCL.dll", NULL};
166 #elif defined(__APPLE__)
167     const char *paths[] = {"/Library/Frameworks/OpenCL.framework/OpenCL", NULL};
168 #else
169     const char *paths[] = {"libOpenCL.so",
170                            "libOpenCL.so.0",
171                            "libOpenCL.so.1",
172                            "libOpenCL.so.2",
173                            NULL};
174 #endif
175
176     int error = 0;
177
178     //  Check if already initialized
179     if (module != NULL)
180     {
181         return CLEW_SUCCESS;
182     }
183
184     //  Load library
185     module = dynamic_library_open_find(paths);
186
187     //  Check for errors
188     if (module == NULL)
189     {
190         return CLEW_ERROR_OPEN_FAILED;
191     }
192
193     //  Set unloading
194     error = atexit(clewExit);
195
196     if (error)
197     {
198         //  Failure queuing atexit, shutdown with error
199         CLEW_DYNLIB_CLOSE(module);
200         module = NULL;
201
202         return CLEW_ERROR_ATEXIT_FAILED;
203     }
204
205     //  Determine function entry-points
206     __clewGetPlatformIDs                = (PFNCLGETPLATFORMIDS              )CLEW_DYNLIB_IMPORT(module, "clGetPlatformIDs");
207     __clewGetPlatformInfo               = (PFNCLGETPLATFORMINFO             )CLEW_DYNLIB_IMPORT(module, "clGetPlatformInfo");
208     __clewGetDeviceIDs                  = (PFNCLGETDEVICEIDS                )CLEW_DYNLIB_IMPORT(module, "clGetDeviceIDs");
209     __clewGetDeviceInfo                 = (PFNCLGETDEVICEINFO               )CLEW_DYNLIB_IMPORT(module, "clGetDeviceInfo");
210     __clewCreateSubDevices              = (PFNCLCREATESUBDEVICES            )CLEW_DYNLIB_IMPORT(module, "clCreateSubDevices");
211     __clewRetainDevice                  = (PFNCLRETAINDEVICE                )CLEW_DYNLIB_IMPORT(module, "clRetainDevice");
212     __clewReleaseDevice                 = (PFNCLRELEASEDEVICE               )CLEW_DYNLIB_IMPORT(module, "clReleaseDevice");
213     __clewCreateContext                 = (PFNCLCREATECONTEXT               )CLEW_DYNLIB_IMPORT(module, "clCreateContext");
214     __clewCreateContextFromType         = (PFNCLCREATECONTEXTFROMTYPE       )CLEW_DYNLIB_IMPORT(module, "clCreateContextFromType");
215     __clewRetainContext                 = (PFNCLRETAINCONTEXT               )CLEW_DYNLIB_IMPORT(module, "clRetainContext");
216     __clewReleaseContext                = (PFNCLRELEASECONTEXT              )CLEW_DYNLIB_IMPORT(module, "clReleaseContext");
217     __clewGetContextInfo                = (PFNCLGETCONTEXTINFO              )CLEW_DYNLIB_IMPORT(module, "clGetContextInfo");
218     __clewCreateCommandQueue            = (PFNCLCREATECOMMANDQUEUE          )CLEW_DYNLIB_IMPORT(module, "clCreateCommandQueue");
219     __clewRetainCommandQueue            = (PFNCLRETAINCOMMANDQUEUE          )CLEW_DYNLIB_IMPORT(module, "clRetainCommandQueue");
220     __clewReleaseCommandQueue           = (PFNCLRELEASECOMMANDQUEUE         )CLEW_DYNLIB_IMPORT(module, "clReleaseCommandQueue");
221     __clewGetCommandQueueInfo           = (PFNCLGETCOMMANDQUEUEINFO         )CLEW_DYNLIB_IMPORT(module, "clGetCommandQueueInfo");
222 #ifdef CL_USE_DEPRECATED_OPENCL_1_0_APIS
223     __clewSetCommandQueueProperty       = (PFNCLSETCOMMANDQUEUEPROPERTY     )CLEW_DYNLIB_IMPORT(module, "clSetCommandQueueProperty");
224 #endif
225     __clewCreateBuffer                  = (PFNCLCREATEBUFFER                )CLEW_DYNLIB_IMPORT(module, "clCreateBuffer");
226     __clewCreateSubBuffer               = (PFNCLCREATESUBBUFFER             )CLEW_DYNLIB_IMPORT(module, "clCreateBuffer");
227     __clewCreateImage                   = (PFNCLCREATEIMAGE                 )CLEW_DYNLIB_IMPORT(module, "clCreateImage");
228     __clewRetainMemObject               = (PFNCLRETAINMEMOBJECT             )CLEW_DYNLIB_IMPORT(module, "clRetainMemObject");
229     __clewReleaseMemObject              = (PFNCLRELEASEMEMOBJECT            )CLEW_DYNLIB_IMPORT(module, "clReleaseMemObject");
230     __clewGetSupportedImageFormats      = (PFNCLGETSUPPORTEDIMAGEFORMATS    )CLEW_DYNLIB_IMPORT(module, "clGetSupportedImageFormats");
231     __clewGetMemObjectInfo              = (PFNCLGETMEMOBJECTINFO            )CLEW_DYNLIB_IMPORT(module, "clGetMemObjectInfo");
232     __clewGetImageInfo                  = (PFNCLGETIMAGEINFO                )CLEW_DYNLIB_IMPORT(module, "clGetImageInfo");
233     __clewSetMemObjectDestructorCallback = (PFNCLSETMEMOBJECTDESTRUCTORCALLBACK)CLEW_DYNLIB_IMPORT(module, "clSetMemObjectDestructorCallback");
234     __clewCreateSampler                 = (PFNCLCREATESAMPLER               )CLEW_DYNLIB_IMPORT(module, "clCreateSampler");
235     __clewRetainSampler                 = (PFNCLRETAINSAMPLER               )CLEW_DYNLIB_IMPORT(module, "clRetainSampler");
236     __clewReleaseSampler                = (PFNCLRELEASESAMPLER              )CLEW_DYNLIB_IMPORT(module, "clReleaseSampler");
237     __clewGetSamplerInfo                = (PFNCLGETSAMPLERINFO              )CLEW_DYNLIB_IMPORT(module, "clGetSamplerInfo");
238     __clewCreateProgramWithSource       = (PFNCLCREATEPROGRAMWITHSOURCE     )CLEW_DYNLIB_IMPORT(module, "clCreateProgramWithSource");
239     __clewCreateProgramWithBinary       = (PFNCLCREATEPROGRAMWITHBINARY     )CLEW_DYNLIB_IMPORT(module, "clCreateProgramWithBinary");
240     __clewCreateProgramWithBuiltInKernels =(PFNCLCREATEPROGRAMWITHBUILTINKERNELS)CLEW_DYNLIB_IMPORT(module, "clCreateProgramWithBuiltInKernels");
241     __clewRetainProgram                 = (PFNCLRETAINPROGRAM               )CLEW_DYNLIB_IMPORT(module, "clRetainProgram");
242     __clewReleaseProgram                = (PFNCLRELEASEPROGRAM              )CLEW_DYNLIB_IMPORT(module, "clReleaseProgram");
243     __clewBuildProgram                  = (PFNCLBUILDPROGRAM                )CLEW_DYNLIB_IMPORT(module, "clBuildProgram");
244
245     __clewGetProgramInfo                = (PFNCLGETPROGRAMINFO              )CLEW_DYNLIB_IMPORT(module, "clGetProgramInfo");
246     __clewGetProgramBuildInfo           = (PFNCLGETPROGRAMBUILDINFO         )CLEW_DYNLIB_IMPORT(module, "clGetProgramBuildInfo");
247     __clewCreateKernel                  = (PFNCLCREATEKERNEL                )CLEW_DYNLIB_IMPORT(module, "clCreateKernel");
248     __clewCreateKernelsInProgram        = (PFNCLCREATEKERNELSINPROGRAM      )CLEW_DYNLIB_IMPORT(module, "clCreateKernelsInProgram");
249     __clewRetainKernel                  = (PFNCLRETAINKERNEL                )CLEW_DYNLIB_IMPORT(module, "clRetainKernel");
250     __clewReleaseKernel                 = (PFNCLRELEASEKERNEL               )CLEW_DYNLIB_IMPORT(module, "clReleaseKernel");
251     __clewSetKernelArg                  = (PFNCLSETKERNELARG                )CLEW_DYNLIB_IMPORT(module, "clSetKernelArg");
252     __clewGetKernelInfo                 = (PFNCLGETKERNELINFO               )CLEW_DYNLIB_IMPORT(module, "clGetKernelInfo");
253     __clewGetKernelWorkGroupInfo        = (PFNCLGETKERNELWORKGROUPINFO      )CLEW_DYNLIB_IMPORT(module, "clGetKernelWorkGroupInfo");
254     __clewWaitForEvents                 = (PFNCLWAITFOREVENTS               )CLEW_DYNLIB_IMPORT(module, "clWaitForEvents");
255     __clewGetEventInfo                  = (PFNCLGETEVENTINFO                )CLEW_DYNLIB_IMPORT(module, "clGetEventInfo");
256     __clewCreateUserEvent               = (PFNCLCREATEUSEREVENT             )CLEW_DYNLIB_IMPORT(module, "clCreateUserEvent");
257     __clewRetainEvent                   = (PFNCLRETAINEVENT                 )CLEW_DYNLIB_IMPORT(module, "clRetainEvent");
258     __clewReleaseEvent                  = (PFNCLRELEASEEVENT                )CLEW_DYNLIB_IMPORT(module, "clReleaseEvent");
259     __clewSetUserEventStatus            = (PFNCLSETUSEREVENTSTATUS          )CLEW_DYNLIB_IMPORT(module, "clSetUserEventStatus");
260     __clewSetEventCallback              = (PFNCLSETEVENTCALLBACK            )CLEW_DYNLIB_IMPORT(module, "clSetEventCallback");
261     __clewGetEventProfilingInfo         = (PFNCLGETEVENTPROFILINGINFO       )CLEW_DYNLIB_IMPORT(module, "clGetEventProfilingInfo");
262     __clewFlush                         = (PFNCLFLUSH                       )CLEW_DYNLIB_IMPORT(module, "clFlush");
263     __clewFinish                        = (PFNCLFINISH                      )CLEW_DYNLIB_IMPORT(module, "clFinish");
264     __clewEnqueueReadBuffer             = (PFNCLENQUEUEREADBUFFER           )CLEW_DYNLIB_IMPORT(module, "clEnqueueReadBuffer");
265     __clewEnqueueReadBufferRect         = (PFNCLENQUEUEREADBUFFERRECT       )CLEW_DYNLIB_IMPORT(module, "clEnqueueReadBufferRect");
266     __clewEnqueueWriteBuffer            = (PFNCLENQUEUEWRITEBUFFER          )CLEW_DYNLIB_IMPORT(module, "clEnqueueWriteBuffer");
267     __clewEnqueueWriteBufferRect        = (PFNCLENQUEUEWRITEBUFFERRECT      )CLEW_DYNLIB_IMPORT(module, "clEnqueueWriteBufferRect");
268     __clewEnqueueCopyBuffer             = (PFNCLENQUEUECOPYBUFFER           )CLEW_DYNLIB_IMPORT(module, "clEnqueueCopyBuffer");
269     __clewEnqueueCopyBufferRect         = (PFNCLENQUEUECOPYBUFFERRECT       )CLEW_DYNLIB_IMPORT(module, "clEnqueueCopyBufferRect");
270     __clewEnqueueReadImage              = (PFNCLENQUEUEREADIMAGE            )CLEW_DYNLIB_IMPORT(module, "clEnqueueReadImage");
271     __clewEnqueueWriteImage             = (PFNCLENQUEUEWRITEIMAGE           )CLEW_DYNLIB_IMPORT(module, "clEnqueueWriteImage");
272     __clewEnqueueCopyImage              = (PFNCLENQUEUECOPYIMAGE            )CLEW_DYNLIB_IMPORT(module, "clEnqueueCopyImage");
273     __clewEnqueueCopyImageToBuffer      = (PFNCLENQUEUECOPYIMAGETOBUFFER    )CLEW_DYNLIB_IMPORT(module, "clEnqueueCopyImageToBuffer");
274     __clewEnqueueCopyBufferToImage      = (PFNCLENQUEUECOPYBUFFERTOIMAGE    )CLEW_DYNLIB_IMPORT(module, "clEnqueueCopyBufferToImage");
275     __clewEnqueueMapBuffer              = (PFNCLENQUEUEMAPBUFFER            )CLEW_DYNLIB_IMPORT(module, "clEnqueueMapBuffer");
276     __clewEnqueueMapImage               = (PFNCLENQUEUEMAPIMAGE             )CLEW_DYNLIB_IMPORT(module, "clEnqueueMapImage");
277     __clewEnqueueUnmapMemObject         = (PFNCLENQUEUEUNMAPMEMOBJECT       )CLEW_DYNLIB_IMPORT(module, "clEnqueueUnmapMemObject");
278     __clewEnqueueNDRangeKernel          = (PFNCLENQUEUENDRANGEKERNEL        )CLEW_DYNLIB_IMPORT(module, "clEnqueueNDRangeKernel");
279     __clewEnqueueTask                   = (PFNCLENQUEUETASK                 )CLEW_DYNLIB_IMPORT(module, "clEnqueueTask");
280     __clewEnqueueNativeKernel           = (PFNCLENQUEUENATIVEKERNEL         )CLEW_DYNLIB_IMPORT(module, "clEnqueueNativeKernel");
281
282
283     __clewGetExtensionFunctionAddressForPlatform = (PFNCLGETEXTENSIONFUNCTIONADDRESSFORPLATFORM)CLEW_DYNLIB_IMPORT(module, "clGetExtensionFunctionAddressForPlatform");
284 #ifdef CL_USE_DEPRECATED_OPENCL_1_1_APIS
285     __clewCreateImage2D                 = (PFNCLCREATEIMAGE2D               )CLEW_DYNLIB_IMPORT(module, "clCreateImage2D");
286     __clewCreateImage3D                 = (PFNCLCREATEIMAGE3D               )CLEW_DYNLIB_IMPORT(module, "clCreateImage3D");
287     __clewEnqueueMarker                 = (PFNCLENQUEUEMARKER               )CLEW_DYNLIB_IMPORT(module, "clEnqueueMarker");
288     __clewEnqueueWaitForEvents          = (PFNCLENQUEUEWAITFOREVENTS        )CLEW_DYNLIB_IMPORT(module, "clEnqueueWaitForEvents");
289     __clewEnqueueBarrier                = (PFNCLENQUEUEBARRIER              )CLEW_DYNLIB_IMPORT(module, "clEnqueueBarrier");
290     __clewUnloadCompiler                = (PFNCLUNLOADCOMPILER              )CLEW_DYNLIB_IMPORT(module, "clUnloadCompiler");
291     __clewGetExtensionFunctionAddress   = (PFNCLGETEXTENSIONFUNCTIONADDRESS )CLEW_DYNLIB_IMPORT(module, "clGetExtensionFunctionAddress");
292 #endif
293
294
295     /* cl_gl */
296     __clewCreateFromGLBuffer            = (PFNCLCREATEFROMGLBUFFER          )CLEW_DYNLIB_IMPORT(module, "clCreateFromGLBuffer");
297     __clewCreateFromGLTexture           = (PFNCLCREATEFROMGLTEXTURE         )CLEW_DYNLIB_IMPORT(module, "clCreateFromGLTexture");
298     __clewCreateFromGLRenderbuffer      = (PFNCLCREATEFROMGLRENDERBUFFER    )CLEW_DYNLIB_IMPORT(module, "clCreateFromGLRenderbuffer");
299     __clewGetGLObjectInfo               = (PFNCLGETGLOBJECTINFO             )CLEW_DYNLIB_IMPORT(module, "clGetGLObjectInfo");
300     __clewGetGLTextureInfo              = (PFNCLGETGLTEXTUREINFO            )CLEW_DYNLIB_IMPORT(module, "clGetGLTextureInfo");
301     __clewEnqueueAcquireGLObjects       = (PFNCLENQUEUEACQUIREGLOBJECTS     )CLEW_DYNLIB_IMPORT(module, "clEnqueueAcquireGLObjects");
302     __clewEnqueueReleaseGLObjects       = (PFNCLENQUEUERELEASEGLOBJECTS     )CLEW_DYNLIB_IMPORT(module, "clEnqueueReleaseGLObjects");
303     #ifdef CL_USE_DEPRECATED_OPENCL_1_1_APIS
304     __clewCreateFromGLTexture2D         = (PFNCLCREATEFROMGLTEXTURE2D       )CLEW_DYNLIB_IMPORT(module, "clCreateFromGLTexture2D");
305     __clewCreateFromGLTexture3D         = (PFNCLCREATEFROMGLTEXTURE3D       )CLEW_DYNLIB_IMPORT(module, "clCreateFromGLTexture3D");
306     #endif
307     __clewGetGLContextInfoKHR           = (PFNCLGETGLCONTEXTINFOKHR         )CLEW_DYNLIB_IMPORT(module, "clGetGLContextInfoKHR");
308
309
310     if(__clewGetPlatformIDs == NULL) return 0;
311     if(__clewGetPlatformInfo == NULL) return 0;
312     if(__clewGetDeviceIDs == NULL) return 0;
313     if(__clewGetDeviceInfo == NULL) return 0;
314
315     return CLEW_SUCCESS;
316 }
317
318 const char* clewErrorString(cl_int error)
319 {
320     static const char* strings[] =
321     {
322         // Error Codes
323           "CL_SUCCESS"                                  //   0
324         , "CL_DEVICE_NOT_FOUND"                         //  -1
325         , "CL_DEVICE_NOT_AVAILABLE"                     //  -2
326         , "CL_COMPILER_NOT_AVAILABLE"                   //  -3
327         , "CL_MEM_OBJECT_ALLOCATION_FAILURE"            //  -4
328         , "CL_OUT_OF_RESOURCES"                         //  -5
329         , "CL_OUT_OF_HOST_MEMORY"                       //  -6
330         , "CL_PROFILING_INFO_NOT_AVAILABLE"             //  -7
331         , "CL_MEM_COPY_OVERLAP"                         //  -8
332         , "CL_IMAGE_FORMAT_MISMATCH"                    //  -9
333         , "CL_IMAGE_FORMAT_NOT_SUPPORTED"               //  -10
334         , "CL_BUILD_PROGRAM_FAILURE"                    //  -11
335         , "CL_MAP_FAILURE"                              //  -12
336         , "CL_MISALIGNED_SUB_BUFFER_OFFSET"             //  -13
337         , "CL_EXEC_STATUS_ERROR_FOR_EVENTS_IN_WAIT_LIST"//  -14
338         , "CL_COMPILE_PROGRAM_FAILURE"                  //  -15
339         , "CL_LINKER_NOT_AVAILABLE"                     //  -16
340         , "CL_LINK_PROGRAM_FAILURE"                     //  -17
341         , "CL_DEVICE_PARTITION_FAILED"                  //  -18
342         , "CL_KERNEL_ARG_INFO_NOT_AVAILABLE"            //  -19
343
344         , ""    //  -20
345         , ""    //  -21
346         , ""    //  -22
347         , ""    //  -23
348         , ""    //  -24
349         , ""    //  -25
350         , ""    //  -26
351         , ""    //  -27
352         , ""    //  -28
353         , ""    //  -29
354
355         , "CL_INVALID_VALUE"                            //  -30
356         , "CL_INVALID_DEVICE_TYPE"                      //  -31
357         , "CL_INVALID_PLATFORM"                         //  -32
358         , "CL_INVALID_DEVICE"                           //  -33
359         , "CL_INVALID_CONTEXT"                          //  -34
360         , "CL_INVALID_QUEUE_PROPERTIES"                 //  -35
361         , "CL_INVALID_COMMAND_QUEUE"                    //  -36
362         , "CL_INVALID_HOST_PTR"                         //  -37
363         , "CL_INVALID_MEM_OBJECT"                       //  -38
364         , "CL_INVALID_IMAGE_FORMAT_DESCRIPTOR"          //  -39
365         , "CL_INVALID_IMAGE_SIZE"                       //  -40
366         , "CL_INVALID_SAMPLER"                          //  -41
367         , "CL_INVALID_BINARY"                           //  -42
368         , "CL_INVALID_BUILD_OPTIONS"                    //  -43
369         , "CL_INVALID_PROGRAM"                          //  -44
370         , "CL_INVALID_PROGRAM_EXECUTABLE"               //  -45
371         , "CL_INVALID_KERNEL_NAME"                      //  -46
372         , "CL_INVALID_KERNEL_DEFINITION"                //  -47
373         , "CL_INVALID_KERNEL"                           //  -48
374         , "CL_INVALID_ARG_INDEX"                        //  -49
375         , "CL_INVALID_ARG_VALUE"                        //  -50
376         , "CL_INVALID_ARG_SIZE"                         //  -51
377         , "CL_INVALID_KERNEL_ARGS"                      //  -52
378         , "CL_INVALID_WORK_DIMENSION"                   //  -53
379         , "CL_INVALID_WORK_GROUP_SIZE"                  //  -54
380         , "CL_INVALID_WORK_ITEM_SIZE"                   //  -55
381         , "CL_INVALID_GLOBAL_OFFSET"                    //  -56
382         , "CL_INVALID_EVENT_WAIT_LIST"                  //  -57
383         , "CL_INVALID_EVENT"                            //  -58
384         , "CL_INVALID_OPERATION"                        //  -59
385         , "CL_INVALID_GL_OBJECT"                        //  -60
386         , "CL_INVALID_BUFFER_SIZE"                      //  -61
387         , "CL_INVALID_MIP_LEVEL"                        //  -62
388         , "CL_INVALID_GLOBAL_WORK_SIZE"                 //  -63
389         , "CL_INVALID_PROPERTY"                         //  -64
390         , "CL_INVALID_IMAGE_DESCRIPTOR"                 //  -65
391         , "CL_INVALID_COMPILER_OPTIONS"                 //  -66
392         , "CL_INVALID_LINKER_OPTIONS"                   //  -67
393         , "CL_INVALID_DEVICE_PARTITION_COUNT"           //  -68
394     };
395
396     static const int num_errors = sizeof(strings) / sizeof(strings[0]);
397
398     if (error == -1001) {
399         return "CL_PLATFORM_NOT_FOUND_KHR";
400     }
401
402     if (error > 0 || -error >= num_errors) {
403         return "Unknown OpenCL error";
404     }
405
406     return strings[-error];
407 }