1 //////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2009 Organic Vectory B.V.
3 // Written by George van Venrooij
5 // Distributed under the Boost Software License, Version 1.0.
6 // (See accompanying file license.txt)
7 //////////////////////////////////////////////////////////////////////////
12 #define WIN32_LEAN_AND_MEAN
16 typedef HMODULE CLEW_DYNLIB_HANDLE;
18 #define CLEW_DYNLIB_OPEN LoadLibraryA
19 #define CLEW_DYNLIB_CLOSE FreeLibrary
20 #define CLEW_DYNLIB_IMPORT GetProcAddress
24 typedef void* CLEW_DYNLIB_HANDLE;
26 #define CLEW_DYNLIB_OPEN(path) dlopen(path, RTLD_NOW | RTLD_GLOBAL)
27 #define CLEW_DYNLIB_CLOSE dlclose
28 #define CLEW_DYNLIB_IMPORT dlsym
33 //! \brief module handle
34 static CLEW_DYNLIB_HANDLE module = NULL;
36 // Variables holding function entry points
37 PFNCLGETPLATFORMIDS __clewGetPlatformIDs = NULL;
38 PFNCLGETPLATFORMINFO __clewGetPlatformInfo = NULL;
39 PFNCLGETDEVICEIDS __clewGetDeviceIDs = NULL;
40 PFNCLGETDEVICEINFO __clewGetDeviceInfo = NULL;
41 PFNCLCREATECONTEXT __clewCreateContext = NULL;
42 PFNCLCREATECONTEXTFROMTYPE __clewCreateContextFromType = NULL;
43 PFNCLRETAINCONTEXT __clewRetainContext = NULL;
44 PFNCLRELEASECONTEXT __clewReleaseContext = NULL;
45 PFNCLGETCONTEXTINFO __clewGetContextInfo = NULL;
46 PFNCLCREATECOMMANDQUEUE __clewCreateCommandQueue = NULL;
47 PFNCLRETAINCOMMANDQUEUE __clewRetainCommandQueue = NULL;
48 PFNCLRELEASECOMMANDQUEUE __clewReleaseCommandQueue = NULL;
49 PFNCLGETCOMMANDQUEUEINFO __clewGetCommandQueueInfo = NULL;
50 #ifdef CL_USE_DEPRECATED_OPENCL_1_0_APIS
51 PFNCLSETCOMMANDQUEUEPROPERTY __clewSetCommandQueueProperty = NULL;
53 PFNCLCREATEBUFFER __clewCreateBuffer = NULL;
54 PFNCLCREATESUBBUFFER __clewCreateSubBuffer = NULL;
55 PFNCLCREATEIMAGE2D __clewCreateImage2D = NULL;
56 PFNCLCREATEIMAGE3D __clewCreateImage3D = NULL;
57 PFNCLRETAINMEMOBJECT __clewRetainMemObject = NULL;
58 PFNCLRELEASEMEMOBJECT __clewReleaseMemObject = NULL;
59 PFNCLGETSUPPORTEDIMAGEFORMATS __clewGetSupportedImageFormats = NULL;
60 PFNCLGETMEMOBJECTINFO __clewGetMemObjectInfo = NULL;
61 PFNCLGETIMAGEINFO __clewGetImageInfo = NULL;
62 PFNCLSETMEMOBJECTDESTRUCTORCALLBACK __clewSetMemObjectDestructorCallback = NULL;
63 PFNCLCREATESAMPLER __clewCreateSampler = NULL;
64 PFNCLRETAINSAMPLER __clewRetainSampler = NULL;
65 PFNCLRELEASESAMPLER __clewReleaseSampler = NULL;
66 PFNCLGETSAMPLERINFO __clewGetSamplerInfo = NULL;
67 PFNCLCREATEPROGRAMWITHSOURCE __clewCreateProgramWithSource = NULL;
68 PFNCLCREATEPROGRAMWITHBINARY __clewCreateProgramWithBinary = NULL;
69 PFNCLRETAINPROGRAM __clewRetainProgram = NULL;
70 PFNCLRELEASEPROGRAM __clewReleaseProgram = NULL;
71 PFNCLBUILDPROGRAM __clewBuildProgram = NULL;
72 PFNCLUNLOADCOMPILER __clewUnloadCompiler = NULL;
73 PFNCLGETPROGRAMINFO __clewGetProgramInfo = NULL;
74 PFNCLGETPROGRAMBUILDINFO __clewGetProgramBuildInfo = NULL;
75 PFNCLCREATEKERNEL __clewCreateKernel = NULL;
76 PFNCLCREATEKERNELSINPROGRAM __clewCreateKernelsInProgram = NULL;
77 PFNCLRETAINKERNEL __clewRetainKernel = NULL;
78 PFNCLRELEASEKERNEL __clewReleaseKernel = NULL;
79 PFNCLSETKERNELARG __clewSetKernelArg = NULL;
80 PFNCLGETKERNELINFO __clewGetKernelInfo = NULL;
81 PFNCLGETKERNELWORKGROUPINFO __clewGetKernelWorkGroupInfo = NULL;
82 PFNCLWAITFOREVENTS __clewWaitForEvents = NULL;
83 PFNCLGETEVENTINFO __clewGetEventInfo = NULL;
84 PFNCLCREATEUSEREVENT __clewCreateUserEvent = NULL;
85 PFNCLRETAINEVENT __clewRetainEvent = NULL;
86 PFNCLRELEASEEVENT __clewReleaseEvent = NULL;
87 PFNCLSETUSEREVENTSTATUS __clewSetUserEventStatus = NULL;
88 PFNCLSETEVENTCALLBACK __clewSetEventCallback = NULL;
89 PFNCLGETEVENTPROFILINGINFO __clewGetEventProfilingInfo = NULL;
90 PFNCLFLUSH __clewFlush = NULL;
91 PFNCLFINISH __clewFinish = NULL;
92 PFNCLENQUEUEREADBUFFER __clewEnqueueReadBuffer = NULL;
93 PFNCLENQUEUEREADBUFFERRECT __clewEnqueueReadBufferRect = NULL;
94 PFNCLENQUEUEWRITEBUFFER __clewEnqueueWriteBuffer = NULL;
95 PFNCLENQUEUEWRITEBUFFERRECT __clewEnqueueWriteBufferRect = NULL;
96 PFNCLENQUEUECOPYBUFFER __clewEnqueueCopyBuffer = NULL;
97 PFNCLENQUEUEREADIMAGE __clewEnqueueReadImage = NULL;
98 PFNCLENQUEUEWRITEIMAGE __clewEnqueueWriteImage = NULL;
99 PFNCLENQUEUECOPYIMAGE __clewEnqueueCopyImage = NULL;
100 PFNCLENQUEUECOPYBUFFERRECT __clewEnqueueCopyBufferRect = NULL;
101 PFNCLENQUEUECOPYIMAGETOBUFFER __clewEnqueueCopyImageToBuffer = NULL;
102 PFNCLENQUEUECOPYBUFFERTOIMAGE __clewEnqueueCopyBufferToImage = NULL;
103 PFNCLENQUEUEMAPBUFFER __clewEnqueueMapBuffer = NULL;
104 PFNCLENQUEUEMAPIMAGE __clewEnqueueMapImage = NULL;
105 PFNCLENQUEUEUNMAPMEMOBJECT __clewEnqueueUnmapMemObject = NULL;
106 PFNCLENQUEUENDRANGEKERNEL __clewEnqueueNDRangeKernel = NULL;
107 PFNCLENQUEUETASK __clewEnqueueTask = NULL;
108 PFNCLENQUEUENATIVEKERNEL __clewEnqueueNativeKernel = NULL;
109 PFNCLENQUEUEMARKER __clewEnqueueMarker = NULL;
110 PFNCLENQUEUEWAITFOREVENTS __clewEnqueueWaitForEvents = NULL;
111 PFNCLENQUEUEBARRIER __clewEnqueueBarrier = NULL;
112 PFNCLGETEXTENSIONFUNCTIONADDRESS __clewGetExtensionFunctionAddress = NULL;
119 CLEW_DYNLIB_CLOSE(module);
124 int clewInit(const char* path)
128 // Check if already initialized
135 module = CLEW_DYNLIB_OPEN(path);
140 return CLEW_ERROR_OPEN_FAILED;
144 error = atexit(clewExit);
148 // Failure queuing atexit, shutdown with error
149 CLEW_DYNLIB_CLOSE(module);
152 return CLEW_ERROR_ATEXIT_FAILED;
155 // Determine function entry-points
156 __clewGetPlatformIDs = (PFNCLGETPLATFORMIDS)CLEW_DYNLIB_IMPORT(module, "clGetPlatformIDs");
157 __clewGetPlatformInfo = (PFNCLGETPLATFORMINFO)CLEW_DYNLIB_IMPORT(module, "clGetPlatformInfo");
158 __clewGetDeviceIDs = (PFNCLGETDEVICEIDS)CLEW_DYNLIB_IMPORT(module, "clGetDeviceIDs");
159 __clewGetDeviceInfo = (PFNCLGETDEVICEINFO)CLEW_DYNLIB_IMPORT(module, "clGetDeviceInfo");
160 __clewCreateContext = (PFNCLCREATECONTEXT)CLEW_DYNLIB_IMPORT(module, "clCreateContext");
161 __clewCreateContextFromType = (PFNCLCREATECONTEXTFROMTYPE)CLEW_DYNLIB_IMPORT(module, "clCreateContextFromType");
162 __clewRetainContext = (PFNCLRETAINCONTEXT)CLEW_DYNLIB_IMPORT(module, "clRetainContext");
163 __clewReleaseContext = (PFNCLRELEASECONTEXT)CLEW_DYNLIB_IMPORT(module, "clReleaseContext");
164 __clewGetContextInfo = (PFNCLGETCONTEXTINFO)CLEW_DYNLIB_IMPORT(module, "clGetContextInfo");
165 __clewCreateCommandQueue = (PFNCLCREATECOMMANDQUEUE)CLEW_DYNLIB_IMPORT(module, "clCreateCommandQueue");
166 __clewRetainCommandQueue = (PFNCLRETAINCOMMANDQUEUE)CLEW_DYNLIB_IMPORT(module, "clRetainCommandQueue");
167 __clewReleaseCommandQueue = (PFNCLRELEASECOMMANDQUEUE)CLEW_DYNLIB_IMPORT(module, "clReleaseCommandQueue");
168 __clewGetCommandQueueInfo = (PFNCLGETCOMMANDQUEUEINFO)CLEW_DYNLIB_IMPORT(module, "clGetCommandQueueInfo");
169 #ifdef CL_USE_DEPRECATED_OPENCL_1_0_APIS
170 __clewSetCommandQueueProperty = (PFNCLSETCOMMANDQUEUEPROPERTY)CLEW_DYNLIB_IMPORT(module, "clSetCommandQueueProperty");
172 __clewCreateBuffer = (PFNCLCREATEBUFFER)CLEW_DYNLIB_IMPORT(module, "clCreateBuffer");
173 __clewCreateSubBuffer = (PFNCLCREATESUBBUFFER)CLEW_DYNLIB_IMPORT(module, "clCreateBuffer");
174 __clewCreateImage2D = (PFNCLCREATEIMAGE2D)CLEW_DYNLIB_IMPORT(module, "clCreateImage2D");
175 __clewCreateImage3D = (PFNCLCREATEIMAGE3D)CLEW_DYNLIB_IMPORT(module, "clCreateImage3D");
176 __clewRetainMemObject = (PFNCLRETAINMEMOBJECT)CLEW_DYNLIB_IMPORT(module, "clRetainMemObject");
177 __clewReleaseMemObject = (PFNCLRELEASEMEMOBJECT)CLEW_DYNLIB_IMPORT(module, "clReleaseMemObject");
178 __clewGetSupportedImageFormats = (PFNCLGETSUPPORTEDIMAGEFORMATS)CLEW_DYNLIB_IMPORT(module, "clGetSupportedImageFormats");
179 __clewGetMemObjectInfo = (PFNCLGETMEMOBJECTINFO)CLEW_DYNLIB_IMPORT(module, "clGetMemObjectInfo");
180 __clewGetImageInfo = (PFNCLGETIMAGEINFO)CLEW_DYNLIB_IMPORT(module, "clGetImageInfo");
181 __clewSetMemObjectDestructorCallback = (PFNCLSETMEMOBJECTDESTRUCTORCALLBACK)CLEW_DYNLIB_IMPORT(module, "clSetMemObjectDestructorCallback");
182 __clewCreateSampler = (PFNCLCREATESAMPLER)CLEW_DYNLIB_IMPORT(module, "clCreateSampler");
183 __clewRetainSampler = (PFNCLRETAINSAMPLER)CLEW_DYNLIB_IMPORT(module, "clRetainSampler");
184 __clewReleaseSampler = (PFNCLRELEASESAMPLER)CLEW_DYNLIB_IMPORT(module, "clReleaseSampler");
185 __clewGetSamplerInfo = (PFNCLGETSAMPLERINFO)CLEW_DYNLIB_IMPORT(module, "clGetSamplerInfo");
186 __clewCreateProgramWithSource = (PFNCLCREATEPROGRAMWITHSOURCE)CLEW_DYNLIB_IMPORT(module, "clCreateProgramWithSource");
187 __clewCreateProgramWithBinary = (PFNCLCREATEPROGRAMWITHBINARY)CLEW_DYNLIB_IMPORT(module, "clCreateProgramWithBinary");
188 __clewRetainProgram = (PFNCLRETAINPROGRAM)CLEW_DYNLIB_IMPORT(module, "clRetainProgram");
189 __clewReleaseProgram = (PFNCLRELEASEPROGRAM)CLEW_DYNLIB_IMPORT(module, "clReleaseProgram");
190 __clewBuildProgram = (PFNCLBUILDPROGRAM)CLEW_DYNLIB_IMPORT(module, "clBuildProgram");
191 __clewUnloadCompiler = (PFNCLUNLOADCOMPILER)CLEW_DYNLIB_IMPORT(module, "clUnloadCompiler");
192 __clewGetProgramInfo = (PFNCLGETPROGRAMINFO)CLEW_DYNLIB_IMPORT(module, "clGetProgramInfo");
193 __clewGetProgramBuildInfo = (PFNCLGETPROGRAMBUILDINFO)CLEW_DYNLIB_IMPORT(module, "clGetProgramBuildInfo");
194 __clewCreateKernel = (PFNCLCREATEKERNEL)CLEW_DYNLIB_IMPORT(module, "clCreateKernel");
195 __clewCreateKernelsInProgram = (PFNCLCREATEKERNELSINPROGRAM)CLEW_DYNLIB_IMPORT(module, "clCreateKernelsInProgram");
196 __clewRetainKernel = (PFNCLRETAINKERNEL)CLEW_DYNLIB_IMPORT(module, "clRetainKernel");
197 __clewReleaseKernel = (PFNCLRELEASEKERNEL)CLEW_DYNLIB_IMPORT(module, "clReleaseKernel");
198 __clewSetKernelArg = (PFNCLSETKERNELARG)CLEW_DYNLIB_IMPORT(module, "clSetKernelArg");
199 __clewGetKernelInfo = (PFNCLGETKERNELINFO)CLEW_DYNLIB_IMPORT(module, "clGetKernelInfo");
200 __clewGetKernelWorkGroupInfo = (PFNCLGETKERNELWORKGROUPINFO)CLEW_DYNLIB_IMPORT(module, "clGetKernelWorkGroupInfo");
201 __clewWaitForEvents = (PFNCLWAITFOREVENTS)CLEW_DYNLIB_IMPORT(module, "clWaitForEvents");
202 __clewGetEventInfo = (PFNCLGETEVENTINFO)CLEW_DYNLIB_IMPORT(module, "clGetEventInfo");
203 __clewCreateUserEvent = (PFNCLCREATEUSEREVENT)CLEW_DYNLIB_IMPORT(module, "clCreateUserEvent");
204 __clewRetainEvent = (PFNCLRETAINEVENT)CLEW_DYNLIB_IMPORT(module, "clRetainEvent");
205 __clewReleaseEvent = (PFNCLRELEASEEVENT)CLEW_DYNLIB_IMPORT(module, "clReleaseEvent");
206 __clewSetUserEventStatus = (PFNCLSETUSEREVENTSTATUS)CLEW_DYNLIB_IMPORT(module, "clSetUserEventStatus");
207 __clewSetEventCallback = (PFNCLSETEVENTCALLBACK)CLEW_DYNLIB_IMPORT(module, "clSetEventCallback");
208 __clewGetEventProfilingInfo = (PFNCLGETEVENTPROFILINGINFO)CLEW_DYNLIB_IMPORT(module, "clGetEventProfilingInfo");
209 __clewFlush = (PFNCLFLUSH)CLEW_DYNLIB_IMPORT(module, "clFlush");
210 __clewFinish = (PFNCLFINISH)CLEW_DYNLIB_IMPORT(module, "clFinish");
211 __clewEnqueueReadBuffer = (PFNCLENQUEUEREADBUFFER)CLEW_DYNLIB_IMPORT(module, "clEnqueueReadBuffer");
212 __clewEnqueueReadBufferRect = (PFNCLENQUEUEREADBUFFERRECT)CLEW_DYNLIB_IMPORT(module, "clEnqueueReadBufferRect");
213 __clewEnqueueWriteBuffer = (PFNCLENQUEUEWRITEBUFFER)CLEW_DYNLIB_IMPORT(module, "clEnqueueWriteBuffer");
214 __clewEnqueueWriteBufferRect = (PFNCLENQUEUEWRITEBUFFERRECT)CLEW_DYNLIB_IMPORT(module, "clEnqueueWriteBufferRect");
215 __clewEnqueueCopyBuffer = (PFNCLENQUEUECOPYBUFFER)CLEW_DYNLIB_IMPORT(module, "clEnqueueCopyBuffer");
216 __clewEnqueueCopyBufferRect = (PFNCLENQUEUECOPYBUFFERRECT)CLEW_DYNLIB_IMPORT(module, "clEnqueueCopyBufferRect");
217 __clewEnqueueReadImage = (PFNCLENQUEUEREADIMAGE)CLEW_DYNLIB_IMPORT(module, "clEnqueueReadImage");
218 __clewEnqueueWriteImage = (PFNCLENQUEUEWRITEIMAGE)CLEW_DYNLIB_IMPORT(module, "clEnqueueWriteImage");
219 __clewEnqueueCopyImage = (PFNCLENQUEUECOPYIMAGE)CLEW_DYNLIB_IMPORT(module, "clEnqueueCopyImage");
220 __clewEnqueueCopyImageToBuffer = (PFNCLENQUEUECOPYIMAGETOBUFFER)CLEW_DYNLIB_IMPORT(module, "clEnqueueCopyImageToBuffer");
221 __clewEnqueueCopyBufferToImage = (PFNCLENQUEUECOPYBUFFERTOIMAGE)CLEW_DYNLIB_IMPORT(module, "clEnqueueCopyBufferToImage");
222 __clewEnqueueMapBuffer = (PFNCLENQUEUEMAPBUFFER)CLEW_DYNLIB_IMPORT(module, "clEnqueueMapBuffer");
223 __clewEnqueueMapImage = (PFNCLENQUEUEMAPIMAGE)CLEW_DYNLIB_IMPORT(module, "clEnqueueMapImage");
224 __clewEnqueueUnmapMemObject = (PFNCLENQUEUEUNMAPMEMOBJECT)CLEW_DYNLIB_IMPORT(module, "clEnqueueUnmapMemObject");
225 __clewEnqueueNDRangeKernel = (PFNCLENQUEUENDRANGEKERNEL)CLEW_DYNLIB_IMPORT(module, "clEnqueueNDRangeKernel");
226 __clewEnqueueTask = (PFNCLENQUEUETASK)CLEW_DYNLIB_IMPORT(module, "clEnqueueTask");
227 __clewEnqueueNativeKernel = (PFNCLENQUEUENATIVEKERNEL)CLEW_DYNLIB_IMPORT(module, "clEnqueueNativeKernel");
228 __clewEnqueueMarker = (PFNCLENQUEUEMARKER)CLEW_DYNLIB_IMPORT(module, "clEnqueueMarker");
229 __clewEnqueueWaitForEvents = (PFNCLENQUEUEWAITFOREVENTS)CLEW_DYNLIB_IMPORT(module, "clEnqueueWaitForEvents");
230 __clewEnqueueBarrier = (PFNCLENQUEUEBARRIER)CLEW_DYNLIB_IMPORT(module, "clEnqueueBarrier");
231 __clewGetExtensionFunctionAddress = (PFNCLGETEXTENSIONFUNCTIONADDRESS)CLEW_DYNLIB_IMPORT(module, "clGetExtensionFunctionAddress");
236 const char* clewErrorString(cl_int error)
238 static const char* strings[] =
243 "CL_DEVICE_NOT_FOUND" // -1
245 "CL_DEVICE_NOT_AVAILABLE" // -2
247 "CL_COMPILER_NOT_AVAILABLE" // -3
249 "CL_MEM_OBJECT_ALLOCATION_FAILURE" // -4
251 "CL_OUT_OF_RESOURCES" // -5
253 "CL_OUT_OF_HOST_MEMORY" // -6
255 "CL_PROFILING_INFO_NOT_AVAILABLE" // -7
257 "CL_MEM_COPY_OVERLAP" // -8
259 "CL_IMAGE_FORMAT_MISMATCH" // -9
261 "CL_IMAGE_FORMAT_NOT_SUPPORTED" // -10
263 "CL_BUILD_PROGRAM_FAILURE" // -11
265 "CL_MAP_FAILURE" // -12
304 "CL_INVALID_VALUE" // -30
306 "CL_INVALID_DEVICE_TYPE" // -31
308 "CL_INVALID_PLATFORM" // -32
310 "CL_INVALID_DEVICE" // -33
312 "CL_INVALID_CONTEXT" // -34
314 "CL_INVALID_QUEUE_PROPERTIES" // -35
316 "CL_INVALID_COMMAND_QUEUE" // -36
318 "CL_INVALID_HOST_PTR" // -37
320 "CL_INVALID_MEM_OBJECT" // -38
322 "CL_INVALID_IMAGE_FORMAT_DESCRIPTOR" // -39
324 "CL_INVALID_IMAGE_SIZE" // -40
326 "CL_INVALID_SAMPLER" // -41
328 "CL_INVALID_BINARY" // -42
330 "CL_INVALID_BUILD_OPTIONS" // -43
332 "CL_INVALID_PROGRAM" // -44
334 "CL_INVALID_PROGRAM_EXECUTABLE" // -45
336 "CL_INVALID_KERNEL_NAME" // -46
338 "CL_INVALID_KERNEL_DEFINITION" // -47
340 "CL_INVALID_KERNEL" // -48
342 "CL_INVALID_ARG_INDEX" // -49
344 "CL_INVALID_ARG_VALUE" // -50
346 "CL_INVALID_ARG_SIZE" // -51
348 "CL_INVALID_KERNEL_ARGS" // -52
350 "CL_INVALID_WORK_DIMENSION" // -53
352 "CL_INVALID_WORK_GROUP_SIZE" // -54
354 "CL_INVALID_WORK_ITEM_SIZE" // -55
356 "CL_INVALID_GLOBAL_OFFSET" // -56
358 "CL_INVALID_EVENT_WAIT_LIST" // -57
360 "CL_INVALID_EVENT" // -58
362 "CL_INVALID_OPERATION" // -59
364 "CL_INVALID_GL_OBJECT" // -60
366 "CL_INVALID_BUFFER_SIZE" // -61
368 "CL_INVALID_MIP_LEVEL" // -62
370 "CL_INVALID_GLOBAL_WORK_SIZE" // -63
373 return strings[-error];