Blender V2.61 - r43446
|
00001 00002 // Copyright (c) 2009 Organic Vectory B.V. 00003 // Written by George van Venrooij 00004 // 00005 // Distributed under the Boost Software License, Version 1.0. 00006 // (See accompanying file doc/license/Boost.txt) 00007 // Extracted from the CLCC project - http://clcc.sourceforge.net/ 00009 00010 #include <stdlib.h> 00011 00012 #include "util_opencl.h" 00013 00014 #ifndef CLCC_GENERATE_DOCUMENTATION 00015 #ifdef _WIN32 00016 #define WIN32_LEAN_AND_MEAN 00017 #define VC_EXTRALEAN 00018 #include <windows.h> 00019 00020 typedef HMODULE CLCC_DYNLIB_HANDLE; 00021 00022 #define CLCC_DYNLIB_OPEN LoadLibrary 00023 #define CLCC_DYNLIB_CLOSE FreeLibrary 00024 #define CLCC_DYNLIB_IMPORT GetProcAddress 00025 #else 00026 #include <dlfcn.h> 00027 00028 typedef void* CLCC_DYNLIB_HANDLE; 00029 00030 #define CLCC_DYNLIB_OPEN(path) dlopen(path, RTLD_NOW | RTLD_GLOBAL) 00031 #define CLCC_DYNLIB_CLOSE dlclose 00032 #define CLCC_DYNLIB_IMPORT dlsym 00033 #endif 00034 #else 00035 //typedef implementation_defined CLCC_DYNLIB_HANDLE; 00036 //#define CLCC_DYNLIB_OPEN(path) implementation_defined 00037 //#define CLCC_DYNLIB_CLOSE implementation_defined 00038 //#define CLCC_DYNLIB_IMPORT implementation_defined 00039 #endif 00040 00041 CCL_NAMESPACE_BEGIN 00042 00044 static CLCC_DYNLIB_HANDLE module = NULL; 00045 00046 // Variables holding function entry points 00047 #ifndef CLCC_GENERATE_DOCUMENTATION 00048 PFNCLGETPLATFORMIDS __clewGetPlatformIDs = NULL; 00049 PFNCLGETPLATFORMINFO __clewGetPlatformInfo = NULL; 00050 PFNCLGETDEVICEIDS __clewGetDeviceIDs = NULL; 00051 PFNCLGETDEVICEINFO __clewGetDeviceInfo = NULL; 00052 PFNCLCREATECONTEXT __clewCreateContext = NULL; 00053 PFNCLCREATECONTEXTFROMTYPE __clewCreateContextFromType = NULL; 00054 PFNCLRETAINCONTEXT __clewRetainContext = NULL; 00055 PFNCLRELEASECONTEXT __clewReleaseContext = NULL; 00056 PFNCLGETCONTEXTINFO __clewGetContextInfo = NULL; 00057 PFNCLCREATECOMMANDQUEUE __clewCreateCommandQueue = NULL; 00058 PFNCLRETAINCOMMANDQUEUE __clewRetainCommandQueue = NULL; 00059 PFNCLRELEASECOMMANDQUEUE __clewReleaseCommandQueue = NULL; 00060 PFNCLGETCOMMANDQUEUEINFO __clewGetCommandQueueInfo = NULL; 00061 PFNCLSETCOMMANDQUEUEPROPERTY __clewSetCommandQueueProperty = NULL; 00062 PFNCLCREATEBUFFER __clewCreateBuffer = NULL; 00063 PFNCLCREATEIMAGE2D __clewCreateImage2D = NULL; 00064 PFNCLCREATEIMAGE3D __clewCreateImage3D = NULL; 00065 PFNCLRETAINMEMOBJECT __clewRetainMemObject = NULL; 00066 PFNCLRELEASEMEMOBJECT __clewReleaseMemObject = NULL; 00067 PFNCLGETSUPPORTEDIMAGEFORMATS __clewGetSupportedImageFormats = NULL; 00068 PFNCLGETMEMOBJECTINFO __clewGetMemObjectInfo = NULL; 00069 PFNCLGETIMAGEINFO __clewGetImageInfo = NULL; 00070 PFNCLCREATESAMPLER __clewCreateSampler = NULL; 00071 PFNCLRETAINSAMPLER __clewRetainSampler = NULL; 00072 PFNCLRELEASESAMPLER __clewReleaseSampler = NULL; 00073 PFNCLGETSAMPLERINFO __clewGetSamplerInfo = NULL; 00074 PFNCLCREATEPROGRAMWITHSOURCE __clewCreateProgramWithSource = NULL; 00075 PFNCLCREATEPROGRAMWITHBINARY __clewCreateProgramWithBinary = NULL; 00076 PFNCLRETAINPROGRAM __clewRetainProgram = NULL; 00077 PFNCLRELEASEPROGRAM __clewReleaseProgram = NULL; 00078 PFNCLBUILDPROGRAM __clewBuildProgram = NULL; 00079 PFNCLUNLOADCOMPILER __clewUnloadCompiler = NULL; 00080 PFNCLGETPROGRAMINFO __clewGetProgramInfo = NULL; 00081 PFNCLGETPROGRAMBUILDINFO __clewGetProgramBuildInfo = NULL; 00082 PFNCLCREATEKERNEL __clewCreateKernel = NULL; 00083 PFNCLCREATEKERNELSINPROGRAM __clewCreateKernelsInProgram = NULL; 00084 PFNCLRETAINKERNEL __clewRetainKernel = NULL; 00085 PFNCLRELEASEKERNEL __clewReleaseKernel = NULL; 00086 PFNCLSETKERNELARG __clewSetKernelArg = NULL; 00087 PFNCLGETKERNELINFO __clewGetKernelInfo = NULL; 00088 PFNCLGETKERNELWORKGROUPINFO __clewGetKernelWorkGroupInfo = NULL; 00089 PFNCLWAITFOREVENTS __clewWaitForEvents = NULL; 00090 PFNCLGETEVENTINFO __clewGetEventInfo = NULL; 00091 PFNCLRETAINEVENT __clewRetainEvent = NULL; 00092 PFNCLRELEASEEVENT __clewReleaseEvent = NULL; 00093 PFNCLGETEVENTPROFILINGINFO __clewGetEventProfilingInfo = NULL; 00094 PFNCLFLUSH __clewFlush = NULL; 00095 PFNCLFINISH __clewFinish = NULL; 00096 PFNCLENQUEUEREADBUFFER __clewEnqueueReadBuffer = NULL; 00097 PFNCLENQUEUEWRITEBUFFER __clewEnqueueWriteBuffer = NULL; 00098 PFNCLENQUEUECOPYBUFFER __clewEnqueueCopyBuffer = NULL; 00099 PFNCLENQUEUEREADIMAGE __clewEnqueueReadImage = NULL; 00100 PFNCLENQUEUEWRITEIMAGE __clewEnqueueWriteImage = NULL; 00101 PFNCLENQUEUECOPYIMAGE __clewEnqueueCopyImage = NULL; 00102 PFNCLENQUEUECOPYIMAGETOBUFFER __clewEnqueueCopyImageToBuffer = NULL; 00103 PFNCLENQUEUECOPYBUFFERTOIMAGE __clewEnqueueCopyBufferToImage = NULL; 00104 PFNCLENQUEUEMAPBUFFER __clewEnqueueMapBuffer = NULL; 00105 PFNCLENQUEUEMAPIMAGE __clewEnqueueMapImage = NULL; 00106 PFNCLENQUEUEUNMAPMEMOBJECT __clewEnqueueUnmapMemObject = NULL; 00107 PFNCLENQUEUENDRANGEKERNEL __clewEnqueueNDRangeKernel = NULL; 00108 PFNCLENQUEUETASK __clewEnqueueTask = NULL; 00109 PFNCLENQUEUENATIVEKERNEL __clewEnqueueNativeKernel = NULL; 00110 PFNCLENQUEUEMARKER __clewEnqueueMarker = NULL; 00111 PFNCLENQUEUEWAITFOREVENTS __clewEnqueueWaitForEvents = NULL; 00112 PFNCLENQUEUEBARRIER __clewEnqueueBarrier = NULL; 00113 PFNCLGETEXTENSIONFUNCTIONADDRESS __clewGetExtensionFunctionAddress = NULL; 00114 #endif // CLCC_GENERATE_DOCUMENTATION 00115 00116 00118 static void clewExit(void) 00119 { 00120 if (module != NULL) 00121 { 00122 // Ignore errors 00123 CLCC_DYNLIB_CLOSE(module); 00124 module = NULL; 00125 } 00126 } 00127 00132 int clLibraryInit() 00133 { 00134 #ifdef _WIN32 00135 const char *path = "OpenCL.dll"; 00136 #elif defined(__APPLE__) 00137 const char *path = "/Library/Frameworks/OpenCL.framework/OpenCL"; 00138 #else 00139 const char *path = "libOpenCL.so"; 00140 #endif 00141 int error = 0; 00142 00143 // Check if already initialized 00144 if (module != NULL) 00145 { 00146 return 1; 00147 } 00148 00149 // Load library 00150 module = CLCC_DYNLIB_OPEN(path); 00151 00152 // Check for errors 00153 if (module == NULL) 00154 { 00155 return 0; 00156 } 00157 00158 // Set unloading 00159 error = atexit(clewExit); 00160 00161 if (error) 00162 { 00163 // Failure queing atexit, shutdown with error 00164 CLCC_DYNLIB_CLOSE(module); 00165 module = NULL; 00166 00167 return 0; 00168 } 00169 00170 // Determine function entry-points 00171 __clewGetPlatformIDs = (PFNCLGETPLATFORMIDS )CLCC_DYNLIB_IMPORT(module, "clGetPlatformIDs"); 00172 __clewGetPlatformInfo = (PFNCLGETPLATFORMINFO )CLCC_DYNLIB_IMPORT(module, "clGetPlatformInfo"); 00173 __clewGetDeviceIDs = (PFNCLGETDEVICEIDS )CLCC_DYNLIB_IMPORT(module, "clGetDeviceIDs"); 00174 __clewGetDeviceInfo = (PFNCLGETDEVICEINFO )CLCC_DYNLIB_IMPORT(module, "clGetDeviceInfo"); 00175 __clewCreateContext = (PFNCLCREATECONTEXT )CLCC_DYNLIB_IMPORT(module, "clCreateContext"); 00176 __clewCreateContextFromType = (PFNCLCREATECONTEXTFROMTYPE )CLCC_DYNLIB_IMPORT(module, "clCreateContextFromType"); 00177 __clewRetainContext = (PFNCLRETAINCONTEXT )CLCC_DYNLIB_IMPORT(module, "clRetainContext"); 00178 __clewReleaseContext = (PFNCLRELEASECONTEXT )CLCC_DYNLIB_IMPORT(module, "clReleaseContext"); 00179 __clewGetContextInfo = (PFNCLGETCONTEXTINFO )CLCC_DYNLIB_IMPORT(module, "clGetContextInfo"); 00180 __clewCreateCommandQueue = (PFNCLCREATECOMMANDQUEUE )CLCC_DYNLIB_IMPORT(module, "clCreateCommandQueue"); 00181 __clewRetainCommandQueue = (PFNCLRETAINCOMMANDQUEUE )CLCC_DYNLIB_IMPORT(module, "clRetainCommandQueue"); 00182 __clewReleaseCommandQueue = (PFNCLRELEASECOMMANDQUEUE )CLCC_DYNLIB_IMPORT(module, "clReleaseCommandQueue"); 00183 __clewGetCommandQueueInfo = (PFNCLGETCOMMANDQUEUEINFO )CLCC_DYNLIB_IMPORT(module, "clGetCommandQueueInfo"); 00184 __clewSetCommandQueueProperty = (PFNCLSETCOMMANDQUEUEPROPERTY )CLCC_DYNLIB_IMPORT(module, "clSetCommandQueueProperty"); 00185 __clewCreateBuffer = (PFNCLCREATEBUFFER )CLCC_DYNLIB_IMPORT(module, "clCreateBuffer"); 00186 __clewCreateImage2D = (PFNCLCREATEIMAGE2D )CLCC_DYNLIB_IMPORT(module, "clCreateImage2D"); 00187 __clewCreateImage3D = (PFNCLCREATEIMAGE3D )CLCC_DYNLIB_IMPORT(module, "clCreateImage3D"); 00188 __clewRetainMemObject = (PFNCLRETAINMEMOBJECT )CLCC_DYNLIB_IMPORT(module, "clRetainMemObject"); 00189 __clewReleaseMemObject = (PFNCLRELEASEMEMOBJECT )CLCC_DYNLIB_IMPORT(module, "clReleaseMemObject"); 00190 __clewGetSupportedImageFormats = (PFNCLGETSUPPORTEDIMAGEFORMATS )CLCC_DYNLIB_IMPORT(module, "clGetSupportedImageFormats"); 00191 __clewGetMemObjectInfo = (PFNCLGETMEMOBJECTINFO )CLCC_DYNLIB_IMPORT(module, "clGetMemObjectInfo"); 00192 __clewGetImageInfo = (PFNCLGETIMAGEINFO )CLCC_DYNLIB_IMPORT(module, "clGetImageInfo"); 00193 __clewCreateSampler = (PFNCLCREATESAMPLER )CLCC_DYNLIB_IMPORT(module, "clCreateSampler"); 00194 __clewRetainSampler = (PFNCLRETAINSAMPLER )CLCC_DYNLIB_IMPORT(module, "clRetainSampler"); 00195 __clewReleaseSampler = (PFNCLRELEASESAMPLER )CLCC_DYNLIB_IMPORT(module, "clReleaseSampler"); 00196 __clewGetSamplerInfo = (PFNCLGETSAMPLERINFO )CLCC_DYNLIB_IMPORT(module, "clGetSamplerInfo"); 00197 __clewCreateProgramWithSource = (PFNCLCREATEPROGRAMWITHSOURCE )CLCC_DYNLIB_IMPORT(module, "clCreateProgramWithSource"); 00198 __clewCreateProgramWithBinary = (PFNCLCREATEPROGRAMWITHBINARY )CLCC_DYNLIB_IMPORT(module, "clCreateProgramWithBinary"); 00199 __clewRetainProgram = (PFNCLRETAINPROGRAM )CLCC_DYNLIB_IMPORT(module, "clRetainProgram"); 00200 __clewReleaseProgram = (PFNCLRELEASEPROGRAM )CLCC_DYNLIB_IMPORT(module, "clReleaseProgram"); 00201 __clewBuildProgram = (PFNCLBUILDPROGRAM )CLCC_DYNLIB_IMPORT(module, "clBuildProgram"); 00202 __clewUnloadCompiler = (PFNCLUNLOADCOMPILER )CLCC_DYNLIB_IMPORT(module, "clUnloadCompiler"); 00203 __clewGetProgramInfo = (PFNCLGETPROGRAMINFO )CLCC_DYNLIB_IMPORT(module, "clGetProgramInfo"); 00204 __clewGetProgramBuildInfo = (PFNCLGETPROGRAMBUILDINFO )CLCC_DYNLIB_IMPORT(module, "clGetProgramBuildInfo"); 00205 __clewCreateKernel = (PFNCLCREATEKERNEL )CLCC_DYNLIB_IMPORT(module, "clCreateKernel"); 00206 __clewCreateKernelsInProgram = (PFNCLCREATEKERNELSINPROGRAM )CLCC_DYNLIB_IMPORT(module, "clCreateKernelsInProgram"); 00207 __clewRetainKernel = (PFNCLRETAINKERNEL )CLCC_DYNLIB_IMPORT(module, "clRetainKernel"); 00208 __clewReleaseKernel = (PFNCLRELEASEKERNEL )CLCC_DYNLIB_IMPORT(module, "clReleaseKernel"); 00209 __clewSetKernelArg = (PFNCLSETKERNELARG )CLCC_DYNLIB_IMPORT(module, "clSetKernelArg"); 00210 __clewGetKernelInfo = (PFNCLGETKERNELINFO )CLCC_DYNLIB_IMPORT(module, "clGetKernelInfo"); 00211 __clewGetKernelWorkGroupInfo = (PFNCLGETKERNELWORKGROUPINFO )CLCC_DYNLIB_IMPORT(module, "clGetKernelWorkGroupInfo"); 00212 __clewWaitForEvents = (PFNCLWAITFOREVENTS )CLCC_DYNLIB_IMPORT(module, "clWaitForEvents"); 00213 __clewGetEventInfo = (PFNCLGETEVENTINFO )CLCC_DYNLIB_IMPORT(module, "clGetEventInfo"); 00214 __clewRetainEvent = (PFNCLRETAINEVENT )CLCC_DYNLIB_IMPORT(module, "clRetainEvent"); 00215 __clewReleaseEvent = (PFNCLRELEASEEVENT )CLCC_DYNLIB_IMPORT(module, "clReleaseEvent"); 00216 __clewGetEventProfilingInfo = (PFNCLGETEVENTPROFILINGINFO )CLCC_DYNLIB_IMPORT(module, "clGetEventProfilingInfo"); 00217 __clewFlush = (PFNCLFLUSH )CLCC_DYNLIB_IMPORT(module, "clFlush"); 00218 __clewFinish = (PFNCLFINISH )CLCC_DYNLIB_IMPORT(module, "clFinish"); 00219 __clewEnqueueReadBuffer = (PFNCLENQUEUEREADBUFFER )CLCC_DYNLIB_IMPORT(module, "clEnqueueReadBuffer"); 00220 __clewEnqueueWriteBuffer = (PFNCLENQUEUEWRITEBUFFER )CLCC_DYNLIB_IMPORT(module, "clEnqueueWriteBuffer"); 00221 __clewEnqueueCopyBuffer = (PFNCLENQUEUECOPYBUFFER )CLCC_DYNLIB_IMPORT(module, "clEnqueueCopyBuffer"); 00222 __clewEnqueueReadImage = (PFNCLENQUEUEREADIMAGE )CLCC_DYNLIB_IMPORT(module, "clEnqueueReadImage"); 00223 __clewEnqueueWriteImage = (PFNCLENQUEUEWRITEIMAGE )CLCC_DYNLIB_IMPORT(module, "clEnqueueWriteImage"); 00224 __clewEnqueueCopyImage = (PFNCLENQUEUECOPYIMAGE )CLCC_DYNLIB_IMPORT(module, "clEnqueueCopyImage"); 00225 __clewEnqueueCopyImageToBuffer = (PFNCLENQUEUECOPYIMAGETOBUFFER )CLCC_DYNLIB_IMPORT(module, "clEnqueueCopyImageToBuffer"); 00226 __clewEnqueueCopyBufferToImage = (PFNCLENQUEUECOPYBUFFERTOIMAGE )CLCC_DYNLIB_IMPORT(module, "clEnqueueCopyBufferToImage"); 00227 __clewEnqueueMapBuffer = (PFNCLENQUEUEMAPBUFFER )CLCC_DYNLIB_IMPORT(module, "clEnqueueMapBuffer"); 00228 __clewEnqueueMapImage = (PFNCLENQUEUEMAPIMAGE )CLCC_DYNLIB_IMPORT(module, "clEnqueueMapImage"); 00229 __clewEnqueueUnmapMemObject = (PFNCLENQUEUEUNMAPMEMOBJECT )CLCC_DYNLIB_IMPORT(module, "clEnqueueUnmapMemObject"); 00230 __clewEnqueueNDRangeKernel = (PFNCLENQUEUENDRANGEKERNEL )CLCC_DYNLIB_IMPORT(module, "clEnqueueNDRangeKernel"); 00231 __clewEnqueueTask = (PFNCLENQUEUETASK )CLCC_DYNLIB_IMPORT(module, "clEnqueueTask"); 00232 __clewEnqueueNativeKernel = (PFNCLENQUEUENATIVEKERNEL )CLCC_DYNLIB_IMPORT(module, "clEnqueueNativeKernel"); 00233 __clewEnqueueMarker = (PFNCLENQUEUEMARKER )CLCC_DYNLIB_IMPORT(module, "clEnqueueMarker"); 00234 __clewEnqueueWaitForEvents = (PFNCLENQUEUEWAITFOREVENTS )CLCC_DYNLIB_IMPORT(module, "clEnqueueWaitForEvents"); 00235 __clewEnqueueBarrier = (PFNCLENQUEUEBARRIER )CLCC_DYNLIB_IMPORT(module, "clEnqueueBarrier"); 00236 __clewGetExtensionFunctionAddress = (PFNCLGETEXTENSIONFUNCTIONADDRESS )CLCC_DYNLIB_IMPORT(module, "clGetExtensionFunctionAddress"); 00237 00238 return 1; 00239 } 00240 00243 const char *clErrorString(cl_int error) 00244 { 00245 static const char* strings[] = 00246 { 00247 // Error Codes 00248 "CL_SUCCESS" // 0 00249 , "CL_DEVICE_NOT_FOUND" // -1 00250 , "CL_DEVICE_NOT_AVAILABLE" // -2 00251 , "CL_COMPILER_NOT_AVAILABLE" // -3 00252 , "CL_MEM_OBJECT_ALLOCATION_FAILURE" // -4 00253 , "CL_OUT_OF_RESOURCES" // -5 00254 , "CL_OUT_OF_HOST_MEMORY" // -6 00255 , "CL_PROFILING_INFO_NOT_AVAILABLE" // -7 00256 , "CL_MEM_COPY_OVERLAP" // -8 00257 , "CL_IMAGE_FORMAT_MISMATCH" // -9 00258 , "CL_IMAGE_FORMAT_NOT_SUPPORTED" // -10 00259 , "CL_BUILD_PROGRAM_FAILURE" // -11 00260 , "CL_MAP_FAILURE" // -12 00261 00262 , "" // -13 00263 , "" // -14 00264 , "" // -15 00265 , "" // -16 00266 , "" // -17 00267 , "" // -18 00268 , "" // -19 00269 00270 , "" // -20 00271 , "" // -21 00272 , "" // -22 00273 , "" // -23 00274 , "" // -24 00275 , "" // -25 00276 , "" // -26 00277 , "" // -27 00278 , "" // -28 00279 , "" // -29 00280 00281 , "CL_INVALID_VALUE" // -30 00282 , "CL_INVALID_DEVICE_TYPE" // -31 00283 , "CL_INVALID_PLATFORM" // -32 00284 , "CL_INVALID_DEVICE" // -33 00285 , "CL_INVALID_CONTEXT" // -34 00286 , "CL_INVALID_QUEUE_PROPERTIES" // -35 00287 , "CL_INVALID_COMMAND_QUEUE" // -36 00288 , "CL_INVALID_HOST_PTR" // -37 00289 , "CL_INVALID_MEM_OBJECT" // -38 00290 , "CL_INVALID_IMAGE_FORMAT_DESCRIPTOR" // -39 00291 , "CL_INVALID_IMAGE_SIZE" // -40 00292 , "CL_INVALID_SAMPLER" // -41 00293 , "CL_INVALID_BINARY" // -42 00294 , "CL_INVALID_BUILD_OPTIONS" // -43 00295 , "CL_INVALID_PROGRAM" // -44 00296 , "CL_INVALID_PROGRAM_EXECUTABLE" // -45 00297 , "CL_INVALID_KERNEL_NAME" // -46 00298 , "CL_INVALID_KERNEL_DEFINITION" // -47 00299 , "CL_INVALID_KERNEL" // -48 00300 , "CL_INVALID_ARG_INDEX" // -49 00301 , "CL_INVALID_ARG_VALUE" // -50 00302 , "CL_INVALID_ARG_SIZE" // -51 00303 , "CL_INVALID_KERNEL_ARGS" // -52 00304 , "CL_INVALID_WORK_DIMENSION" // -53 00305 , "CL_INVALID_WORK_GROUP_SIZE" // -54 00306 , "CL_INVALID_WORK_ITEM_SIZE" // -55 00307 , "CL_INVALID_GLOBAL_OFFSET" // -56 00308 , "CL_INVALID_EVENT_WAIT_LIST" // -57 00309 , "CL_INVALID_EVENT" // -58 00310 , "CL_INVALID_OPERATION" // -59 00311 , "CL_INVALID_GL_OBJECT" // -60 00312 , "CL_INVALID_BUFFER_SIZE" // -61 00313 , "CL_INVALID_MIP_LEVEL" // -62 00314 , "CL_INVALID_GLOBAL_WORK_SIZE" // -63 00315 }; 00316 00317 return strings[-error]; 00318 } 00319 00320 CCL_NAMESPACE_END 00321