1 /*
2 Bullet Continuous Collision Detection and Physics Library, http://bulletphysics.org
3 Copyright (C) 2006 - 2011 Sony Computer Entertainment Inc.
4 
5 This software is provided 'as-is', without any express or implied warranty.
6 In no event will the authors be held liable for any damages arising from the use of this software.
7 Permission is granted to anyone to use this software for any purpose,
8 including commercial applications, and to alter it and redistribute it freely,
9 subject to the following restrictions:
10 
11 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
12 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
13 3. This notice may not be removed or altered from any source distribution.
14 */
15 
16 //original author: Roman Ponomarev
17 //cleanup by Erwin Coumans
18 
19 #ifndef B3_OPENCL_UTILS_H
20 #define B3_OPENCL_UTILS_H
21 
22 #include "b3OpenCLInclude.h"
23 
24 #ifdef __cplusplus
25 extern "C"
26 {
27 #endif
28 
29 	///C API for OpenCL utilities: convenience functions, see below for C++ API
30 
31 	/// CL Context optionally takes a GL context. This is a generic type because we don't really want this code
32 	/// to have to understand GL types. It is a HGLRC in _WIN32 or a GLXContext otherwise.
33 	cl_context b3OpenCLUtils_createContextFromType(cl_device_type deviceType, cl_int* pErrNum, void* pGLCtx, void* pGLDC, int preferredDeviceIndex, int preferredPlatformIndex, cl_platform_id* platformId);
34 
35 	int b3OpenCLUtils_getNumDevices(cl_context cxMainContext);
36 
37 	cl_device_id b3OpenCLUtils_getDevice(cl_context cxMainContext, int nr);
38 
39 	void b3OpenCLUtils_printDeviceInfo(cl_device_id device);
40 
41 	cl_kernel b3OpenCLUtils_compileCLKernelFromString(cl_context clContext, cl_device_id device, const char* kernelSource, const char* kernelName, cl_int* pErrNum, cl_program prog, const char* additionalMacros);
42 
43 	//optional
44 	cl_program b3OpenCLUtils_compileCLProgramFromString(cl_context clContext, cl_device_id device, const char* kernelSource, cl_int* pErrNum, const char* additionalMacros, const char* srcFileNameForCaching, bool disableBinaryCaching);
45 
46 	//the following optional APIs provide access using specific platform information
47 	int b3OpenCLUtils_getNumPlatforms(cl_int* pErrNum);
48 
49 	///get the nr'th platform, where nr is in the range [0..getNumPlatforms)
50 	cl_platform_id b3OpenCLUtils_getPlatform(int nr, cl_int* pErrNum);
51 
52 	void b3OpenCLUtils_printPlatformInfo(cl_platform_id platform);
53 
54 	const char* b3OpenCLUtils_getSdkVendorName();
55 
56 	///set the path (directory/folder) where the compiled OpenCL kernel are stored
57 	void b3OpenCLUtils_setCachePath(const char* path);
58 
59 	cl_context b3OpenCLUtils_createContextFromPlatform(cl_platform_id platform, cl_device_type deviceType, cl_int* pErrNum, void* pGLCtx, void* pGLDC, int preferredDeviceIndex, int preferredPlatformIndex);
60 
61 #ifdef __cplusplus
62 }
63 
64 #define B3_MAX_STRING_LENGTH 1024
65 
66 typedef struct
67 {
68 	char m_deviceName[B3_MAX_STRING_LENGTH];
69 	char m_deviceVendor[B3_MAX_STRING_LENGTH];
70 	char m_driverVersion[B3_MAX_STRING_LENGTH];
71 	char m_deviceExtensions[B3_MAX_STRING_LENGTH];
72 
73 	cl_device_type m_deviceType;
74 	cl_uint m_computeUnits;
75 	size_t m_workitemDims;
76 	size_t m_workItemSize[3];
77 	size_t m_image2dMaxWidth;
78 	size_t m_image2dMaxHeight;
79 	size_t m_image3dMaxWidth;
80 	size_t m_image3dMaxHeight;
81 	size_t m_image3dMaxDepth;
82 	size_t m_workgroupSize;
83 	cl_uint m_clockFrequency;
84 	cl_ulong m_constantBufferSize;
85 	cl_ulong m_localMemSize;
86 	cl_ulong m_globalMemSize;
87 	cl_bool m_errorCorrectionSupport;
88 	cl_device_local_mem_type m_localMemType;
89 	cl_uint m_maxReadImageArgs;
90 	cl_uint m_maxWriteImageArgs;
91 
92 	cl_uint m_addressBits;
93 	cl_ulong m_maxMemAllocSize;
94 	cl_command_queue_properties m_queueProperties;
95 	cl_bool m_imageSupport;
96 	cl_uint m_vecWidthChar;
97 	cl_uint m_vecWidthShort;
98 	cl_uint m_vecWidthInt;
99 	cl_uint m_vecWidthLong;
100 	cl_uint m_vecWidthFloat;
101 	cl_uint m_vecWidthDouble;
102 
103 } b3OpenCLDeviceInfo;
104 
105 struct b3OpenCLPlatformInfo
106 {
107 	char m_platformVendor[B3_MAX_STRING_LENGTH];
108 	char m_platformName[B3_MAX_STRING_LENGTH];
109 	char m_platformVersion[B3_MAX_STRING_LENGTH];
110 
b3OpenCLPlatformInfob3OpenCLPlatformInfo111 	b3OpenCLPlatformInfo()
112 	{
113 		m_platformVendor[0] = 0;
114 		m_platformName[0] = 0;
115 		m_platformVersion[0] = 0;
116 	}
117 };
118 
119 ///C++ API for OpenCL utilities: convenience functions
120 struct b3OpenCLUtils
121 {
122 	/// CL Context optionally takes a GL context. This is a generic type because we don't really want this code
123 	/// to have to understand GL types. It is a HGLRC in _WIN32 or a GLXContext otherwise.
124 	static inline cl_context createContextFromType(cl_device_type deviceType, cl_int* pErrNum, void* pGLCtx = 0, void* pGLDC = 0, int preferredDeviceIndex = -1, int preferredPlatformIndex = -1, cl_platform_id* platformId = 0)
125 	{
126 		return b3OpenCLUtils_createContextFromType(deviceType, pErrNum, pGLCtx, pGLDC, preferredDeviceIndex, preferredPlatformIndex, platformId);
127 	}
128 
getNumDevicesb3OpenCLUtils129 	static inline int getNumDevices(cl_context cxMainContext)
130 	{
131 		return b3OpenCLUtils_getNumDevices(cxMainContext);
132 	}
getDeviceb3OpenCLUtils133 	static inline cl_device_id getDevice(cl_context cxMainContext, int nr)
134 	{
135 		return b3OpenCLUtils_getDevice(cxMainContext, nr);
136 	}
137 
138 	static void getDeviceInfo(cl_device_id device, b3OpenCLDeviceInfo* info);
139 
printDeviceInfob3OpenCLUtils140 	static inline void printDeviceInfo(cl_device_id device)
141 	{
142 		b3OpenCLUtils_printDeviceInfo(device);
143 	}
144 
145 	static inline cl_kernel compileCLKernelFromString(cl_context clContext, cl_device_id device, const char* kernelSource, const char* kernelName, cl_int* pErrNum = 0, cl_program prog = 0, const char* additionalMacros = "")
146 	{
147 		return b3OpenCLUtils_compileCLKernelFromString(clContext, device, kernelSource, kernelName, pErrNum, prog, additionalMacros);
148 	}
149 
150 	//optional
151 	static inline cl_program compileCLProgramFromString(cl_context clContext, cl_device_id device, const char* kernelSource, cl_int* pErrNum = 0, const char* additionalMacros = "", const char* srcFileNameForCaching = 0, bool disableBinaryCaching = false)
152 	{
153 		return b3OpenCLUtils_compileCLProgramFromString(clContext, device, kernelSource, pErrNum, additionalMacros, srcFileNameForCaching, disableBinaryCaching);
154 	}
155 
156 	//the following optional APIs provide access using specific platform information
157 	static inline int getNumPlatforms(cl_int* pErrNum = 0)
158 	{
159 		return b3OpenCLUtils_getNumPlatforms(pErrNum);
160 	}
161 	///get the nr'th platform, where nr is in the range [0..getNumPlatforms)
162 	static inline cl_platform_id getPlatform(int nr, cl_int* pErrNum = 0)
163 	{
164 		return b3OpenCLUtils_getPlatform(nr, pErrNum);
165 	}
166 
167 	static void getPlatformInfo(cl_platform_id platform, b3OpenCLPlatformInfo* platformInfo);
168 
printPlatformInfob3OpenCLUtils169 	static inline void printPlatformInfo(cl_platform_id platform)
170 	{
171 		b3OpenCLUtils_printPlatformInfo(platform);
172 	}
173 
getSdkVendorNameb3OpenCLUtils174 	static inline const char* getSdkVendorName()
175 	{
176 		return b3OpenCLUtils_getSdkVendorName();
177 	}
178 	static inline cl_context createContextFromPlatform(cl_platform_id platform, cl_device_type deviceType, cl_int* pErrNum, void* pGLCtx = 0, void* pGLDC = 0, int preferredDeviceIndex = -1, int preferredPlatformIndex = -1)
179 	{
180 		return b3OpenCLUtils_createContextFromPlatform(platform, deviceType, pErrNum, pGLCtx, pGLDC, preferredDeviceIndex, preferredPlatformIndex);
181 	}
setCachePathb3OpenCLUtils182 	static void setCachePath(const char* path)
183 	{
184 		b3OpenCLUtils_setCachePath(path);
185 	}
186 };
187 
188 #endif  //__cplusplus
189 
190 #endif  // B3_OPENCL_UTILS_H
191