ClangFormat: apply to source, most of intern
[blender.git] / intern / opensubdiv / internal / opensubdiv_device_context_opencl.cc
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
25 #include "opensubdiv_device_context_opencl.h"
26
27 #ifdef OPENSUBDIV_HAS_OPENCL
28
29 #  if defined(_WIN32)
30 #    include <windows.h>
31 #  elif defined(__APPLE__)
32 #    include <OpenGL/OpenGL.h>
33 #  else
34 #    include <GL/glx.h>
35 #  endif
36
37 #  include <cstdio>
38 #  include <cstring>
39
40 #  include "internal/opensubdiv_util.h"
41
42 #  define message(...)  // fprintf(stderr, __VA_ARGS__)
43 #  define error(...) fprintf(stderr, __VA_ARGS__)
44
45 namespace {
46
47 // Returns the first found platform.
48 cl_platform_id findPlatform()
49 {
50   cl_uint num_platforms;
51   cl_int ci_error_number = clGetPlatformIDs(0, NULL, &num_platforms);
52   if (ci_error_number != CL_SUCCESS) {
53     error("Error %d in clGetPlatformIDs call.\n", ci_error_number);
54     return NULL;
55   }
56   if (num_platforms == 0) {
57     error("No OpenCL platform found.\n");
58     return NULL;
59   }
60   vector<cl_platform_id> cl_platform_ids(num_platforms);
61   ci_error_number = clGetPlatformIDs(num_platforms, &cl_platform_ids[0], NULL);
62   char ch_buffer[1024];
63   for (cl_uint i = 0; i < num_platforms; ++i) {
64     ci_error_number = clGetPlatformInfo(
65         cl_platform_ids[i], CL_PLATFORM_NAME, sizeof(ch_buffer), ch_buffer, NULL);
66     if (ci_error_number == CL_SUCCESS) {
67       cl_platform_id platform_id = cl_platform_ids[i];
68       return platform_id;
69     }
70   }
71   return NULL;
72 }
73
74 // Return the device in cl_devices which supports the extension.
75 int findExtensionSupportedDevice(cl_device_id *cl_devices,
76                                  int num_devices,
77                                  const char *extension_name)
78 {
79   // Find a device that supports sharing with GL/D3D11
80   // (SLI / X-fire configurations)
81   cl_int cl_error_number;
82   for (int i = 0; i < num_devices; ++i) {
83     // Get extensions string size.
84     size_t extensions_size;
85     cl_error_number = clGetDeviceInfo(
86         cl_devices[i], CL_DEVICE_EXTENSIONS, 0, NULL, &extensions_size);
87     if (cl_error_number != CL_SUCCESS) {
88       error("Error %d in clGetDeviceInfo\n", cl_error_number);
89       return -1;
90     }
91     if (extensions_size > 0) {
92       // Get extensions string.
93       string extensions('\0', extensions_size);
94       cl_error_number = clGetDeviceInfo(
95           cl_devices[i], CL_DEVICE_EXTENSIONS, extensions_size, &extensions[0], &extensions_size);
96       if (cl_error_number != CL_SUCCESS) {
97         error("Error %d in clGetDeviceInfo\n", cl_error_number);
98         continue;
99       }
100       // Parse string. This is bit deficient since the extentions
101       // is space separated.
102       //
103       // The actual string would be "cl_khr_d3d11_sharing"
104       //                         or "cl_nv_d3d11_sharing"
105       if (extensions.find(extension_name) != string::npos) {
106         return i;
107       }
108     }
109   }
110   return -1;
111 }
112
113 }  // namespace
114
115 CLDeviceContext::CLDeviceContext() : cl_context_(NULL), cl_command_queue_(NULL)
116 {
117 }
118
119 CLDeviceContext::~CLDeviceContext()
120 {
121   if (cl_command_queue_) {
122     clReleaseCommandQueue(cl_command_queue_);
123   }
124   if (cl_context_) {
125     clReleaseContext(cl_context_);
126   }
127 }
128
129 bool CLDeviceContext::HAS_CL_VERSION_1_1()
130 {
131 #  ifdef OPENSUBDIV_HAS_CLEW
132   static bool clew_initialized = false;
133   static bool clew_load_success;
134   if (!clew_initialized) {
135     clew_initialized = true;
136     clew_load_success = clewInit() == CLEW_SUCCESS;
137     if (!clew_load_success) {
138       error("Loading OpenCL failed.\n");
139     }
140   }
141   return clew_load_success;
142 #  endif
143   return true;
144 }
145
146 bool CLDeviceContext::Initialize()
147 {
148 #  ifdef OPENSUBDIV_HAS_CLEW
149   if (!clGetPlatformIDs) {
150     error("Error clGetPlatformIDs function not bound.\n");
151     return false;
152   }
153 #  endif
154   cl_int cl_error_number;
155   cl_platform_id cp_platform = findPlatform();
156
157 #  if defined(_WIN32)
158   cl_context_properties props[] = {CL_GL_CONTEXT_KHR,
159                                    (cl_context_properties)wglGetCurrentContext(),
160                                    CL_WGL_HDC_KHR,
161                                    (cl_context_properties)wglGetCurrentDC(),
162                                    CL_CONTEXT_PLATFORM,
163                                    (cl_context_properties)cp_platform,
164                                    0};
165 #  elif defined(__APPLE__)
166   CGLContextObj kCGLContext = CGLGetCurrentContext();
167   CGLShareGroupObj kCGLShareGroup = CGLGetShareGroup(kCGLContext);
168   cl_context_properties props[] = {
169       CL_CONTEXT_PROPERTY_USE_CGL_SHAREGROUP_APPLE, (cl_context_properties)kCGLShareGroup, 0};
170 #  else
171   cl_context_properties props[] = {CL_GL_CONTEXT_KHR,
172                                    (cl_context_properties)glXGetCurrentContext(),
173                                    CL_GLX_DISPLAY_KHR,
174                                    (cl_context_properties)glXGetCurrentDisplay(),
175                                    CL_CONTEXT_PLATFORM,
176                                    (cl_context_properties)cp_platform,
177                                    0};
178 #  endif
179
180 #  if defined(__APPLE__)
181   _clContext = clCreateContext(props, 0, NULL, clLogMessagesToStdoutAPPLE, NULL, &cl_error_number);
182   if (cl_error_number != CL_SUCCESS) {
183     error("Error %d in clCreateContext\n", cl_error_number);
184     return false;
185   }
186   size_t devices_size = 0;
187   clGetGLContextInfoAPPLE(_clContext,
188                           kCGLContext,
189                           CL_CGL_DEVICES_FOR_SUPPORTED_VIRTUAL_SCREENS_APPLE,
190                           0,
191                           NULL,
192                           &devices_size);
193   const int num_devices = devices_size / sizeof(cl_device_id);
194   if (num_devices == 0) {
195     error("No sharable devices.\n");
196     return false;
197   }
198   vector<cl_device_id> cl_devices(num_devices);
199   clGetGLContextInfoAPPLE(_clContext,
200                           kCGLContext,
201                           CL_CGL_DEVICES_FOR_SUPPORTED_VIRTUAL_SCREENS_APPLE,
202                           num_devices * sizeof(cl_device_id),
203                           &cl_devices[0],
204                           NULL);
205   int cl_device_used = 0;
206 #  else   // not __APPLE__
207   // Get the number of GPU devices available to the platform.
208   cl_uint num_devices = 0;
209   clGetDeviceIDs(cp_platform, CL_DEVICE_TYPE_GPU, 0, NULL, &num_devices);
210   if (num_devices == 0) {
211     error("No CL GPU device found.\n");
212     return false;
213   }
214   // Create the device list.
215   vector<cl_device_id> cl_devices(num_devices);
216   clGetDeviceIDs(cp_platform, CL_DEVICE_TYPE_GPU, num_devices, &cl_devices[0], NULL);
217   const char *extension = "cl_khr_gl_sharing";
218   int cl_device_used = findExtensionSupportedDevice(&cl_devices[0], num_devices, extension);
219   if (cl_device_used < 0) {
220     error("No device found that supports CL/GL context sharing\n");
221     return false;
222   }
223   cl_context_ = clCreateContext(
224       props, 1, &cl_devices[cl_device_used], NULL, NULL, &cl_error_number);
225 #  endif  // not __APPLE__
226   if (cl_error_number != CL_SUCCESS) {
227     error("Error %d in clCreateContext\n", cl_error_number);
228     return false;
229   }
230   cl_command_queue_ = clCreateCommandQueue(
231       cl_context_, cl_devices[cl_device_used], 0, &cl_error_number);
232   if (cl_error_number != CL_SUCCESS) {
233     error("Error %d in clCreateCommandQueue\n", cl_error_number);
234     return false;
235   }
236   return true;
237 }
238
239 bool CLDeviceContext::IsInitialized() const
240 {
241   return (cl_context_ != NULL);
242 }
243
244 cl_context CLDeviceContext::GetContext() const
245 {
246   return cl_context_;
247 }
248
249 cl_command_queue CLDeviceContext::GetCommandQueue() const
250 {
251   return cl_command_queue_;
252 }
253
254 #endif  // OPENSUBDIV_HAS_OPENCL