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