opensubdiv_device_context_opencl.cc 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. // Adopted from OpenSubdiv with the following license:
  2. //
  3. // Copyright 2015 Pixar
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "Apache License")
  6. // with the following modification; you may not use this file except in
  7. // compliance with the Apache License and the following modification to it:
  8. // Section 6. Trademarks. is deleted and replaced with:
  9. //
  10. // 6. Trademarks. This License does not grant permission to use the trade
  11. // names, trademarks, service marks, or product names of the Licensor
  12. // and its affiliates, except as required to comply with Section 4(c) of
  13. // the License and to reproduce the content of the NOTICE file.
  14. //
  15. // You may obtain a copy of the Apache License at
  16. //
  17. // http://www.apache.org/licenses/LICENSE-2.0
  18. //
  19. // Unless required by applicable law or agreed to in writing, software
  20. // distributed under the Apache License with the above modification is
  21. // distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  22. // KIND, either express or implied. See the Apache License for the specific
  23. // language governing permissions and limitations under the Apache License.
  24. #include "opensubdiv_device_context_opencl.h"
  25. #ifdef OPENSUBDIV_HAS_OPENCL
  26. # if defined(_WIN32)
  27. # include <windows.h>
  28. # elif defined(__APPLE__)
  29. # include <OpenGL/OpenGL.h>
  30. # else
  31. # include <GL/glx.h>
  32. # endif
  33. # include <cstdio>
  34. # include <cstring>
  35. # include "internal/opensubdiv_util.h"
  36. # define message(...) // fprintf(stderr, __VA_ARGS__)
  37. # define error(...) fprintf(stderr, __VA_ARGS__)
  38. namespace {
  39. // Returns the first found platform.
  40. cl_platform_id findPlatform()
  41. {
  42. cl_uint num_platforms;
  43. cl_int ci_error_number = clGetPlatformIDs(0, NULL, &num_platforms);
  44. if (ci_error_number != CL_SUCCESS) {
  45. error("Error %d in clGetPlatformIDs call.\n", ci_error_number);
  46. return NULL;
  47. }
  48. if (num_platforms == 0) {
  49. error("No OpenCL platform found.\n");
  50. return NULL;
  51. }
  52. vector<cl_platform_id> cl_platform_ids(num_platforms);
  53. ci_error_number = clGetPlatformIDs(num_platforms, &cl_platform_ids[0], NULL);
  54. char ch_buffer[1024];
  55. for (cl_uint i = 0; i < num_platforms; ++i) {
  56. ci_error_number = clGetPlatformInfo(
  57. cl_platform_ids[i], CL_PLATFORM_NAME, sizeof(ch_buffer), ch_buffer, NULL);
  58. if (ci_error_number == CL_SUCCESS) {
  59. cl_platform_id platform_id = cl_platform_ids[i];
  60. return platform_id;
  61. }
  62. }
  63. return NULL;
  64. }
  65. // Return the device in cl_devices which supports the extension.
  66. int findExtensionSupportedDevice(cl_device_id *cl_devices,
  67. int num_devices,
  68. const char *extension_name)
  69. {
  70. // Find a device that supports sharing with GL/D3D11
  71. // (SLI / X-fire configurations)
  72. cl_int cl_error_number;
  73. for (int i = 0; i < num_devices; ++i) {
  74. // Get extensions string size.
  75. size_t extensions_size;
  76. cl_error_number = clGetDeviceInfo(
  77. cl_devices[i], CL_DEVICE_EXTENSIONS, 0, NULL, &extensions_size);
  78. if (cl_error_number != CL_SUCCESS) {
  79. error("Error %d in clGetDeviceInfo\n", cl_error_number);
  80. return -1;
  81. }
  82. if (extensions_size > 0) {
  83. // Get extensions string.
  84. string extensions('\0', extensions_size);
  85. cl_error_number = clGetDeviceInfo(
  86. cl_devices[i], CL_DEVICE_EXTENSIONS, extensions_size, &extensions[0], &extensions_size);
  87. if (cl_error_number != CL_SUCCESS) {
  88. error("Error %d in clGetDeviceInfo\n", cl_error_number);
  89. continue;
  90. }
  91. // Parse string. This is bit deficient since the extentions
  92. // is space separated.
  93. //
  94. // The actual string would be "cl_khr_d3d11_sharing"
  95. // or "cl_nv_d3d11_sharing"
  96. if (extensions.find(extension_name) != string::npos) {
  97. return i;
  98. }
  99. }
  100. }
  101. return -1;
  102. }
  103. } // namespace
  104. CLDeviceContext::CLDeviceContext() : cl_context_(NULL), cl_command_queue_(NULL)
  105. {
  106. }
  107. CLDeviceContext::~CLDeviceContext()
  108. {
  109. if (cl_command_queue_) {
  110. clReleaseCommandQueue(cl_command_queue_);
  111. }
  112. if (cl_context_) {
  113. clReleaseContext(cl_context_);
  114. }
  115. }
  116. bool CLDeviceContext::HAS_CL_VERSION_1_1()
  117. {
  118. # ifdef OPENSUBDIV_HAS_CLEW
  119. static bool clew_initialized = false;
  120. static bool clew_load_success;
  121. if (!clew_initialized) {
  122. clew_initialized = true;
  123. clew_load_success = clewInit() == CLEW_SUCCESS;
  124. if (!clew_load_success) {
  125. error("Loading OpenCL failed.\n");
  126. }
  127. }
  128. return clew_load_success;
  129. # endif
  130. return true;
  131. }
  132. bool CLDeviceContext::Initialize()
  133. {
  134. # ifdef OPENSUBDIV_HAS_CLEW
  135. if (!clGetPlatformIDs) {
  136. error("Error clGetPlatformIDs function not bound.\n");
  137. return false;
  138. }
  139. # endif
  140. cl_int cl_error_number;
  141. cl_platform_id cp_platform = findPlatform();
  142. # if defined(_WIN32)
  143. cl_context_properties props[] = {
  144. CL_GL_CONTEXT_KHR,
  145. (cl_context_properties)wglGetCurrentContext(),
  146. CL_WGL_HDC_KHR,
  147. (cl_context_properties)wglGetCurrentDC(),
  148. CL_CONTEXT_PLATFORM,
  149. (cl_context_properties)cp_platform,
  150. 0,
  151. };
  152. # elif defined(__APPLE__)
  153. CGLContextObj kCGLContext = CGLGetCurrentContext();
  154. CGLShareGroupObj kCGLShareGroup = CGLGetShareGroup(kCGLContext);
  155. cl_context_properties props[] = {
  156. CL_CONTEXT_PROPERTY_USE_CGL_SHAREGROUP_APPLE, (cl_context_properties)kCGLShareGroup, 0};
  157. # else
  158. cl_context_properties props[] = {
  159. CL_GL_CONTEXT_KHR,
  160. (cl_context_properties)glXGetCurrentContext(),
  161. CL_GLX_DISPLAY_KHR,
  162. (cl_context_properties)glXGetCurrentDisplay(),
  163. CL_CONTEXT_PLATFORM,
  164. (cl_context_properties)cp_platform,
  165. 0,
  166. };
  167. # endif
  168. # if defined(__APPLE__)
  169. _clContext = clCreateContext(props, 0, NULL, clLogMessagesToStdoutAPPLE, NULL, &cl_error_number);
  170. if (cl_error_number != CL_SUCCESS) {
  171. error("Error %d in clCreateContext\n", cl_error_number);
  172. return false;
  173. }
  174. size_t devices_size = 0;
  175. clGetGLContextInfoAPPLE(_clContext,
  176. kCGLContext,
  177. CL_CGL_DEVICES_FOR_SUPPORTED_VIRTUAL_SCREENS_APPLE,
  178. 0,
  179. NULL,
  180. &devices_size);
  181. const int num_devices = devices_size / sizeof(cl_device_id);
  182. if (num_devices == 0) {
  183. error("No sharable devices.\n");
  184. return false;
  185. }
  186. vector<cl_device_id> cl_devices(num_devices);
  187. clGetGLContextInfoAPPLE(_clContext,
  188. kCGLContext,
  189. CL_CGL_DEVICES_FOR_SUPPORTED_VIRTUAL_SCREENS_APPLE,
  190. num_devices * sizeof(cl_device_id),
  191. &cl_devices[0],
  192. NULL);
  193. int cl_device_used = 0;
  194. # else // not __APPLE__
  195. // Get the number of GPU devices available to the platform.
  196. cl_uint num_devices = 0;
  197. clGetDeviceIDs(cp_platform, CL_DEVICE_TYPE_GPU, 0, NULL, &num_devices);
  198. if (num_devices == 0) {
  199. error("No CL GPU device found.\n");
  200. return false;
  201. }
  202. // Create the device list.
  203. vector<cl_device_id> cl_devices(num_devices);
  204. clGetDeviceIDs(cp_platform, CL_DEVICE_TYPE_GPU, num_devices, &cl_devices[0], NULL);
  205. const char *extension = "cl_khr_gl_sharing";
  206. int cl_device_used = findExtensionSupportedDevice(&cl_devices[0], num_devices, extension);
  207. if (cl_device_used < 0) {
  208. error("No device found that supports CL/GL context sharing\n");
  209. return false;
  210. }
  211. cl_context_ = clCreateContext(
  212. props, 1, &cl_devices[cl_device_used], NULL, NULL, &cl_error_number);
  213. # endif // not __APPLE__
  214. if (cl_error_number != CL_SUCCESS) {
  215. error("Error %d in clCreateContext\n", cl_error_number);
  216. return false;
  217. }
  218. cl_command_queue_ = clCreateCommandQueue(
  219. cl_context_, cl_devices[cl_device_used], 0, &cl_error_number);
  220. if (cl_error_number != CL_SUCCESS) {
  221. error("Error %d in clCreateCommandQueue\n", cl_error_number);
  222. return false;
  223. }
  224. return true;
  225. }
  226. bool CLDeviceContext::IsInitialized() const
  227. {
  228. return (cl_context_ != NULL);
  229. }
  230. cl_context CLDeviceContext::GetContext() const
  231. {
  232. return cl_context_;
  233. }
  234. cl_command_queue CLDeviceContext::GetCommandQueue() const
  235. {
  236. return cl_command_queue_;
  237. }
  238. #endif // OPENSUBDIV_HAS_OPENCL