Welcome to mirror list, hosted at ThFree Co, Russian Federation.

opensubdiv_device_context_opencl.cc « internal « opensubdiv « intern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 00f58af894a4ace8a1e0de47e85b07de6c1c4e79 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
// Adopted from OpenSubdiv with the following license:
//
//   Copyright 2015 Pixar
//
//   Licensed under the Apache License, Version 2.0 (the "Apache License")
//   with the following modification; you may not use this file except in
//   compliance with the Apache License and the following modification to it:
//   Section 6. Trademarks. is deleted and replaced with:
//
//   6. Trademarks. This License does not grant permission to use the trade
//      names, trademarks, service marks, or product names of the Licensor
//      and its affiliates, except as required to comply with Section 4(c) of
//      the License and to reproduce the content of the NOTICE file.
//
//   You may obtain a copy of the Apache License at
//
//       http://www.apache.org/licenses/LICENSE-2.0
//
//   Unless required by applicable law or agreed to in writing, software
//   distributed under the Apache License with the above modification is
//   distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
//   KIND, either express or implied. See the Apache License for the specific
//   language governing permissions and limitations under the Apache License.

#include "opensubdiv_device_context_opencl.h"

#ifdef OPENSUBDIV_HAS_OPENCL

#if defined(_WIN32)
#  include <windows.h>
#elif defined(__APPLE__)
#  include <OpenGL/OpenGL.h>
#else
#  include <GL/glx.h>
#endif

#include <cstdio>
#include <cstring>
#include <string>
#include <vector>

#define message(...)  // fprintf(stderr, __VA_ARGS__)
#define error(...) fprintf(stderr, __VA_ARGS__)

namespace {

// Returns the first found platform.
cl_platform_id findPlatform() {
  cl_uint num_platforms;
  cl_int ci_error_number = clGetPlatformIDs(0, NULL, &num_platforms);
  if (ci_error_number != CL_SUCCESS) {
    error("Error %d in clGetPlatformIDs call.\n", ci_error_number);
    return NULL;
  }
  if (num_platforms == 0) {
    error("No OpenCL platform found.\n");
    return NULL;
  }
  std::vector<cl_platform_id> cl_platform_ids(num_platforms);
  ci_error_number = clGetPlatformIDs(num_platforms, &cl_platform_ids[0], NULL);
  char ch_buffer[1024];
  for (cl_uint i = 0; i < num_platforms; ++i) {
    ci_error_number = clGetPlatformInfo(cl_platform_ids[i],
                                        CL_PLATFORM_NAME,
                                        sizeof(ch_buffer),
                                        ch_buffer,
                                        NULL);
    if (ci_error_number == CL_SUCCESS) {
      cl_platform_id platform_id = cl_platform_ids[i];
      return platform_id;
    }
  }
  return NULL;
}

// Return the device in cl_devices which supports the extension.
int findExtensionSupportedDevice(cl_device_id* cl_devices,
                                 int num_devices,
                                 const char* extension_name) {
  // Find a device that supports sharing with GL/D3D11
  // (SLI / X-fire configurations)
  cl_int cl_error_number;
  for (int i = 0; i < num_devices; ++i) {
    // Get extensions string size.
    size_t extensions_size;
    cl_error_number = clGetDeviceInfo(cl_devices[i],
                                      CL_DEVICE_EXTENSIONS,
                                      0,
                                      NULL,
                                      &extensions_size);
    if (cl_error_number != CL_SUCCESS) {
      error("Error %d in clGetDeviceInfo\n", cl_error_number);
      return -1;
    }
    if (extensions_size > 0) {
      // Get extensions string.
      std::string extensions('\0', extensions_size);
      cl_error_number = clGetDeviceInfo(cl_devices[i],
                                        CL_DEVICE_EXTENSIONS,
                                        extensions_size,
                                        &extensions[0],
                                        &extensions_size);
      if (cl_error_number != CL_SUCCESS) {
        error("Error %d in clGetDeviceInfo\n", cl_error_number);
        continue;
      }
      // Parse string. This is bit deficient since the extentions
      // is space separated.
      //
      // The actual string would be "cl_khr_d3d11_sharing"
      //                         or "cl_nv_d3d11_sharing"
      if (extensions.find(extension_name) != std::string::npos) {
        return i;
      }
    }
  }
  return -1;
}

}  // namespace

CLDeviceContext::CLDeviceContext()
    : cl_context_(NULL),
      cl_command_queue_(NULL) {
}

CLDeviceContext::~CLDeviceContext() {
  if (cl_command_queue_) {
    clReleaseCommandQueue(cl_command_queue_);
  }
  if (cl_context_) {
    clReleaseContext(cl_context_);
  }
}

bool CLDeviceContext::HAS_CL_VERSION_1_1() {
#ifdef OPENSUBDIV_HAS_CLEW
  static bool clew_initialized = false;
  static bool clew_load_success;
  if (!clew_initialized) {
    clew_initialized = true;
    clew_load_success = clewInit() == CLEW_SUCCESS;
    if (!clew_load_success) {
      error("Loading OpenCL failed.\n");
    }
  }
  return clew_load_success;
#endif
  return true;
}

bool CLDeviceContext::Initialize() {
#ifdef OPENSUBDIV_HAS_CLEW
  if (!clGetPlatformIDs) {
    error("Error clGetPlatformIDs function not bound.\n");
    return false;
  }
#endif
  cl_int cl_error_number;
  cl_platform_id cp_platform = findPlatform();

#if defined(_WIN32)
  cl_context_properties props[] = {
      CL_GL_CONTEXT_KHR,
      (cl_context_properties)wglGetCurrentContext(),
      CL_WGL_HDC_KHR,
      (cl_context_properties)wglGetCurrentDC(),
      CL_CONTEXT_PLATFORM,
      (cl_context_properties)cp_platform,
      0};
#elif defined(__APPLE__)
  CGLContextObj kCGLContext = CGLGetCurrentContext();
  CGLShareGroupObj kCGLShareGroup = CGLGetShareGroup(kCGLContext);
  cl_context_properties props[] = {CL_CONTEXT_PROPERTY_USE_CGL_SHAREGROUP_APPLE,
                                   (cl_context_properties)kCGLShareGroup,
                                   0};
#else
  cl_context_properties props[] = {
      CL_GL_CONTEXT_KHR,
      (cl_context_properties)glXGetCurrentContext(),
      CL_GLX_DISPLAY_KHR,
      (cl_context_properties)glXGetCurrentDisplay(),
      CL_CONTEXT_PLATFORM,
      (cl_context_properties)cp_platform,
      0};
#endif

#if defined(__APPLE__)
  _clContext = clCreateContext(props, 0, NULL, clLogMessagesToStdoutAPPLE, NULL,
                               &cl_error_number);
  if (cl_error_number != CL_SUCCESS) {
    error("Error %d in clCreateContext\n", cl_error_number);
    return false;
  }
  size_t devices_size = 0;
  clGetGLContextInfoAPPLE(_clContext, kCGLContext,
                          CL_CGL_DEVICES_FOR_SUPPORTED_VIRTUAL_SCREENS_APPLE,
                          0,
                          NULL,
                          &devices_size);
  const int num_devices = devices_size / sizeof(cl_device_id);
  if (num_devices == 0) {
    error("No sharable devices.\n");
    return false;
  }
  std::vector<cl_device_id> cl_devices(num_devices);
  clGetGLContextInfoAPPLE(_clContext, kCGLContext,
                          CL_CGL_DEVICES_FOR_SUPPORTED_VIRTUAL_SCREENS_APPLE,
                          num_devices * sizeof(cl_device_id),
                          &cl_devices[0],
                          NULL);
  int cl_device_used = 0;
#else   // not __APPLE__
  // Get the number of GPU devices available to the platform.
  cl_uint num_devices = 0;
  clGetDeviceIDs(cp_platform, CL_DEVICE_TYPE_GPU, 0, NULL, &num_devices);
  if (num_devices == 0) {
    error("No CL GPU device found.\n");
    return false;
  }
  // Create the device list.
  std::vector<cl_device_id> cl_devices(num_devices);
  clGetDeviceIDs(cp_platform,
                 CL_DEVICE_TYPE_GPU,
                 num_devices,
                 &cl_devices[0],
                 NULL);
  const char* extension = "cl_khr_gl_sharing";
  int cl_device_used = findExtensionSupportedDevice(&cl_devices[0],
                                                    num_devices,
                                                    extension);
  if (cl_device_used < 0) {
    error("No device found that supports CL/GL context sharing\n");
    return false;
  }
  cl_context_ = clCreateContext(props,
                                1,
                                &cl_devices[cl_device_used],
                                NULL, NULL,
                                &cl_error_number);
#endif  // not __APPLE__
  if (cl_error_number != CL_SUCCESS) {
    error("Error %d in clCreateContext\n", cl_error_number);
    return false;
  }
  cl_command_queue_ = clCreateCommandQueue(cl_context_,
                                           cl_devices[cl_device_used],
                                           0,
                                           &cl_error_number);
  if (cl_error_number != CL_SUCCESS) {
    error("Error %d in clCreateCommandQueue\n", cl_error_number);
    return false;
  }
  return true;
}

bool CLDeviceContext::IsInitialized() const {
  return (cl_context_ != NULL);
}

cl_context CLDeviceContext::GetContext() const {
  return cl_context_;
}

cl_command_queue CLDeviceContext::GetCommandQueue() const {
  return cl_command_queue_;
}

#endif  // OPENSUBDIV_HAS_OPENCL