File: clDeviceContext.cpp

package info (click to toggle)
opensubdiv 3.6.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 75,132 kB
  • sloc: cpp: 137,820; python: 1,069; objc: 412; javascript: 361; lisp: 216; ansic: 170; makefile: 24
file content (351 lines) | stat: -rw-r--r-- 11,165 bytes parent folder | download | duplicates (3)
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
//
//   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.
//

#define CL_USE_DEPRECATED_OPENCL_2_0_APIS

#include "clDeviceContext.h"

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

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

#if defined(OPENSUBDIV_HAS_DX11SDK)
#include <D3D11.h>

#if defined(OPENSUBDIV_HAS_CL_D3D11_H)
#include <CL/cl_d3d11.h>
#elif defined(OPENSUBDIV_HAS_CL_D3D11_EXT_H)
#include <CL/cl_d3d11_ext.h>
#endif

#endif



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

// returns the first found platform.
//
static cl_platform_id
findPlatform() {
    cl_uint numPlatforms;
    cl_int ciErrNum = clGetPlatformIDs(0, NULL, &numPlatforms);

    if (ciErrNum != CL_SUCCESS) {
        error("Error %d in clGetPlatformIDs call.\n", ciErrNum);
        return NULL;
    }
    if (numPlatforms == 0) {
        error("No OpenCL platform found.\n");
        return NULL;
    }

    cl_platform_id *clPlatformIDs = new cl_platform_id[numPlatforms];
    ciErrNum = clGetPlatformIDs(numPlatforms, clPlatformIDs, NULL);
    char chBuffer[1024];
    for (cl_uint i = 0; i < numPlatforms; ++i) {
        ciErrNum = clGetPlatformInfo(clPlatformIDs[i], CL_PLATFORM_NAME,
                                     1024, chBuffer,NULL);
        if (ciErrNum == CL_SUCCESS) {
            cl_platform_id platformId = clPlatformIDs[i];
            delete[] clPlatformIDs;
            return platformId;
        }
    }
    delete[] clPlatformIDs;
    return NULL;
}


// returns the device in clDevices which supports the extension.
//
static int
findExtensionSupportedDevice(cl_device_id *clDevices,
                             int numDevices,
                             const char *extensionName) {
    // find a device that supports sharing with GL/D3D11
    // (SLI / X-fire configurations)
    cl_int ciErrNum;

    for (int i = 0; i < numDevices; ++i) {
        // get extensions string size
        size_t extensionSize;
        ciErrNum = clGetDeviceInfo(clDevices[i],
                                   CL_DEVICE_EXTENSIONS, 0, NULL,
                                   &extensionSize );

        if (ciErrNum != CL_SUCCESS) {
            error("Error %d in clGetDeviceInfo\n", ciErrNum);
            return -1;
        }

        if (extensionSize>0) {
            // get extensions string
            char *extensions = new char[extensionSize];
            ciErrNum = clGetDeviceInfo(clDevices[i], CL_DEVICE_EXTENSIONS,
                                       extensionSize, extensions,
                                       &extensionSize);
            if (ciErrNum != CL_SUCCESS) {
                error("Error %d in clGetDeviceInfo\n", ciErrNum);
                delete[] extensions;
                continue;
            }
            std::string extString(extensions);
            delete[] extensions;

            // parse string. This is a bit deficient since the extensions
            // string is space separated.
            //
            // The actual string would be "cl_khr_d3d11_sharing"
            //                         or "cl_nv_d3d11_sharing"
            if (extString.find(extensionName) != std::string::npos) {
                return i;
            }
        }
    }
    return -1;
}

// --------------------------------------------------------------------------

CLDeviceContext::CLDeviceContext() :
    _clContext(NULL), _clCommandQueue(NULL) {
}

CLDeviceContext::~CLDeviceContext() {

    if (_clCommandQueue)
        clReleaseCommandQueue(_clCommandQueue);
    if (_clContext)
        clReleaseContext(_clContext);
}

/*static*/
bool
CLDeviceContext::HAS_CL_VERSION_1_1 () {

#ifdef OPENSUBDIV_HAS_CLEW
    static bool clewInitialized = false;
    static bool clewLoadSuccess;
    if (!clewInitialized) {
        clewInitialized = true;
        clewLoadSuccess = clewInit() == CLEW_SUCCESS;
        if (!clewLoadSuccess) {
            error("Loading OpenCL failed.\n");
        }
    }
    return clewLoadSuccess;
#endif
    return true;
}

bool
CLDeviceContext::Initialize() {

#ifdef OPENSUBDIV_HAS_CLEW
    if (!clGetPlatformIDs) {
        error("Error clGetPlatformIDs function not bound.\n");
        return false;
    }
#endif

    cl_int ciErrNum;
    cl_platform_id cpPlatform = 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)cpPlatform,
        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)cpPlatform,
        0
    };
#endif

#if defined(__APPLE__)
    _clContext = clCreateContext(props, 0, NULL, clLogMessagesToStdoutAPPLE,
                                 NULL, &ciErrNum);
    if (ciErrNum != CL_SUCCESS) {
        error("Error %d in clCreateContext\n", ciErrNum);
        return false;
    }

    size_t devicesSize = 0;
    clGetGLContextInfoAPPLE(_clContext, kCGLContext,
                            CL_CGL_DEVICES_FOR_SUPPORTED_VIRTUAL_SCREENS_APPLE,
                            0, NULL, &devicesSize);
    int numDevices = int(devicesSize / sizeof(cl_device_id));
    if (numDevices == 0) {
        error("No sharable devices.\n");
        return false;
    }
    cl_device_id *clDevices = new cl_device_id[numDevices];
    clGetGLContextInfoAPPLE(_clContext, kCGLContext,
                            CL_CGL_DEVICES_FOR_SUPPORTED_VIRTUAL_SCREENS_APPLE,
                            numDevices * sizeof(cl_device_id), clDevices, NULL);
    int clDeviceUsed = 0;

#else   // not __APPLE__

    // get the number of GPU devices available to the platform
    cl_uint numDevices = 0;
    clGetDeviceIDs(cpPlatform, CL_DEVICE_TYPE_GPU, 0, NULL, &numDevices);
    if (numDevices == 0) {
        error("No CL GPU device found.\n");
        return false;
    }

    // create the device list
    cl_device_id *clDevices = new cl_device_id[numDevices];
    clGetDeviceIDs(cpPlatform, CL_DEVICE_TYPE_GPU, numDevices, clDevices, NULL);

    const char *extension = "cl_khr_gl_sharing";
    int clDeviceUsed = findExtensionSupportedDevice(clDevices, numDevices,
                                                    extension);

    if (clDeviceUsed < 0) {
        error("No device found that supports CL/GL context sharing\n");
        delete[] clDevices;
        return false;
    }

    _clContext = clCreateContext(props, 1, &clDevices[clDeviceUsed],
                                 NULL, NULL, &ciErrNum);

#endif   // not __APPLE__

    if (ciErrNum != CL_SUCCESS) {
        error("Error %d in clCreateContext\n", ciErrNum);
        delete[] clDevices;
        return false;
    }

    _clCommandQueue = clCreateCommandQueue(_clContext, clDevices[clDeviceUsed],
                                    0, &ciErrNum);
    delete[] clDevices;
    if (ciErrNum != CL_SUCCESS) {
        error("Error %d in clCreateCommandQueue\n", ciErrNum);
        return false;
    }
    return true;
}

// ---------------------------------------------------------------------------

bool
CLD3D11DeviceContext::Initialize(ID3D11DeviceContext *d3dDeviceContext) {

#if defined(OPENSUBDIV_HAS_DX11SDK) && \
    (defined(OPENSUBDIV_HAS_CL_D3D11_H) || defined(OPENSUBDIV_HAS_CL_D3D11_EXT_H))

    _d3dDeviceContext = d3dDeviceContext;

    cl_int ciErrNum;
    cl_platform_id cpPlatform = findPlatform();

    ID3D11Device *device;
    d3dDeviceContext->GetDevice(&device);

#if defined(OPENSUBDIV_HAS_CL_D3D11_H)
    cl_context_properties props[] = {
        CL_CONTEXT_D3D11_DEVICE_KHR, (cl_context_properties)device,
        CL_CONTEXT_PLATFORM, (cl_context_properties)cpPlatform,
        0
    };
#elif defined(OPENSUBDIV_HAS_CL_D3D11_EXT_H)
    cl_context_properties props[] = {
        CL_CONTEXT_D3D11_DEVICE_NV, (cl_context_properties)device,
        CL_CONTEXT_PLATFORM, (cl_context_properties)cpPlatform,
        0
    };
#endif

    // get the number of GPU devices available to the platform
    cl_uint numDevices = 0;
    clGetDeviceIDs(cpPlatform, CL_DEVICE_TYPE_GPU, 0, NULL, &numDevices);
    if (numDevices == 0) {
        error("No CL GPU device found.\n");
        return false;
    }

    // create the device list
    cl_device_id *clDevices = new cl_device_id[numDevices];
    clGetDeviceIDs(cpPlatform, CL_DEVICE_TYPE_GPU, numDevices, clDevices, NULL);

    // we're cheating a little bit.
    // try to find both cl_khr_d3d11_sharing and cl_nv_d3d11_sharing.
    const char *extension = "_d3d11_sharing";
    int clDeviceUsed = findExtensionSupportedDevice(clDevices, numDevices,
                                                    extension);

    if (clDeviceUsed < 0) {
        error("No device found that supports CL/D3D11 context sharing\n");
        delete[] clDevices;
        return false;
    }

    _clContext = clCreateContext(props, 1, &clDevices[clDeviceUsed],
                                 NULL, NULL, &ciErrNum);
    if (ciErrNum != CL_SUCCESS) {
        error("Error %d in clCreateContext\n", ciErrNum);
        delete[] clDevices;
        return false;
    }

    _clCommandQueue = clCreateCommandQueue(_clContext, clDevices[clDeviceUsed],
                                    0, &ciErrNum);
    delete[] clDevices;
    if (ciErrNum != CL_SUCCESS) {
        error("Error %d in clCreateCommandQueue\n", ciErrNum);
        return false;
    }
    return true;
#else
    (void)d3dDeviceContext;  // unused
    return false;
#endif
}