forked from KhronosGroup/OpenCL-ICD-Loader
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathicd_dispatch.c
More file actions
349 lines (306 loc) · 11.1 KB
/
Copy pathicd_dispatch.c
File metadata and controls
349 lines (306 loc) · 11.1 KB
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
/*
* Copyright (c) 2012-2020 The Khronos Group Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* OpenCL is a trademark of Apple Inc. used under license by Khronos.
*/
#include "icd.h"
#include "icd_dispatch.h"
#include "icd_version.h"
#include <stdlib.h>
#include <string.h>
static clGetICDLoaderInfoOCLICD_t clGetICDLoaderInfoOCLICD;
cl_int CL_API_CALL
clGetICDLoaderInfoOCLICD(
cl_icdl_info param_name,
size_t param_value_size,
void * param_value,
size_t * param_value_size_ret)
{
static const char cl_icdl_OCL_VERSION[] = OPENCL_ICD_LOADER_OCL_VERSION_STRING;
static const char cl_icdl_VERSION[] = OPENCL_ICD_LOADER_VERSION_STRING;
static const char cl_icdl_NAME[] = OPENCL_ICD_LOADER_NAME_STRING;
static const char cl_icdl_VENDOR[] = OPENCL_ICD_LOADER_VENDOR_STRING;
size_t pvs;
void * pv;
#define KHR_ICD_CASE_STRING_PARAM_NAME(name) \
case CL_ICDL_ ## name: \
pvs = strlen(cl_icdl_ ## name) + 1; \
pv = (void *)cl_icdl_ ## name; \
break
switch (param_name) {
KHR_ICD_CASE_STRING_PARAM_NAME(OCL_VERSION);
KHR_ICD_CASE_STRING_PARAM_NAME(VERSION);
KHR_ICD_CASE_STRING_PARAM_NAME(NAME);
KHR_ICD_CASE_STRING_PARAM_NAME(VENDOR);
default:
return CL_INVALID_VALUE;
}
#undef KHR_ICD_CASE_PARAM_NAME
if (param_value) {
if (param_value_size < pvs)
return CL_INVALID_VALUE;
memcpy(param_value, pv, pvs);
}
if (param_value_size_ret != NULL)
*param_value_size_ret = pvs;
return CL_SUCCESS;
}
// !!! TODO: Switch this to use the function typedef when it's in the headers!
static cl_int CL_API_CALL clShutdownOCLICD(void)
{
khrIcdShutdown();
return CL_SUCCESS;
}
static void* khrIcdGetExtensionFunctionAddress(const char* function_name)
{
// Most extensions, including multi-vendor KHR and EXT extensions,
// do not need to be ICD-aware and do not require any ICD loader
// modifications. The KHR and EXT extensions below were added for
// backwards compatibility only.
#define KHR_ICD_CHECK_EXTENSION_FUNCTION(name) \
do \
{ \
if (!strcmp(function_name, #name)) \
{ \
return (void*)(size_t)&name; \
} \
} while (0)
// Functions supporting the creation of OpenCL Memory Objects
// from OpenGL Objects (cl_apple_gl_sharing, cl_khr_gl_sharing)
KHR_ICD_CHECK_EXTENSION_FUNCTION(clCreateFromGLBuffer);
KHR_ICD_CHECK_EXTENSION_FUNCTION(clCreateFromGLTexture);
KHR_ICD_CHECK_EXTENSION_FUNCTION(clCreateFromGLTexture2D);
KHR_ICD_CHECK_EXTENSION_FUNCTION(clCreateFromGLTexture3D);
KHR_ICD_CHECK_EXTENSION_FUNCTION(clCreateFromGLRenderbuffer);
KHR_ICD_CHECK_EXTENSION_FUNCTION(clGetGLObjectInfo);
KHR_ICD_CHECK_EXTENSION_FUNCTION(clGetGLTextureInfo);
KHR_ICD_CHECK_EXTENSION_FUNCTION(clEnqueueAcquireGLObjects);
KHR_ICD_CHECK_EXTENSION_FUNCTION(clEnqueueReleaseGLObjects);
// cl_khr_gl_sharing
KHR_ICD_CHECK_EXTENSION_FUNCTION(clGetGLContextInfoKHR);
// cl_khr_gl_event
KHR_ICD_CHECK_EXTENSION_FUNCTION(clCreateEventFromGLsyncKHR);
#if defined(_WIN32)
// cl_khr_d3d10_sharing
KHR_ICD_CHECK_EXTENSION_FUNCTION(clGetDeviceIDsFromD3D10KHR);
KHR_ICD_CHECK_EXTENSION_FUNCTION(clCreateFromD3D10BufferKHR);
KHR_ICD_CHECK_EXTENSION_FUNCTION(clCreateFromD3D10Texture2DKHR);
KHR_ICD_CHECK_EXTENSION_FUNCTION(clCreateFromD3D10Texture3DKHR);
KHR_ICD_CHECK_EXTENSION_FUNCTION(clEnqueueAcquireD3D10ObjectsKHR);
KHR_ICD_CHECK_EXTENSION_FUNCTION(clEnqueueReleaseD3D10ObjectsKHR);
// cl_khr_d3d11_sharing
KHR_ICD_CHECK_EXTENSION_FUNCTION(clGetDeviceIDsFromD3D11KHR);
KHR_ICD_CHECK_EXTENSION_FUNCTION(clCreateFromD3D11BufferKHR);
KHR_ICD_CHECK_EXTENSION_FUNCTION(clCreateFromD3D11Texture2DKHR);
KHR_ICD_CHECK_EXTENSION_FUNCTION(clCreateFromD3D11Texture3DKHR);
KHR_ICD_CHECK_EXTENSION_FUNCTION(clEnqueueAcquireD3D11ObjectsKHR);
KHR_ICD_CHECK_EXTENSION_FUNCTION(clEnqueueReleaseD3D11ObjectsKHR);
// cl_khr_dx9_media_sharing
KHR_ICD_CHECK_EXTENSION_FUNCTION(clGetDeviceIDsFromDX9MediaAdapterKHR);
KHR_ICD_CHECK_EXTENSION_FUNCTION(clCreateFromDX9MediaSurfaceKHR);
KHR_ICD_CHECK_EXTENSION_FUNCTION(clEnqueueAcquireDX9MediaSurfacesKHR);
KHR_ICD_CHECK_EXTENSION_FUNCTION(clEnqueueReleaseDX9MediaSurfacesKHR);
#endif
// cl_ext_device_fission
KHR_ICD_CHECK_EXTENSION_FUNCTION(clCreateSubDevicesEXT);
KHR_ICD_CHECK_EXTENSION_FUNCTION(clRetainDeviceEXT);
KHR_ICD_CHECK_EXTENSION_FUNCTION(clReleaseDeviceEXT);
// cl_khr_egl_image
KHR_ICD_CHECK_EXTENSION_FUNCTION(clCreateFromEGLImageKHR);
KHR_ICD_CHECK_EXTENSION_FUNCTION(clEnqueueAcquireEGLObjectsKHR);
KHR_ICD_CHECK_EXTENSION_FUNCTION(clEnqueueReleaseEGLObjectsKHR);
// cl_khr_egl_event
KHR_ICD_CHECK_EXTENSION_FUNCTION(clCreateEventFromEGLSyncKHR);
// cl_khr_sub_groups
KHR_ICD_CHECK_EXTENSION_FUNCTION(clGetKernelSubGroupInfoKHR);
// cl_icdl
KHR_ICD_CHECK_EXTENSION_FUNCTION(clGetICDLoaderInfoOCLICD);
// cl_icd_shutdown
KHR_ICD_CHECK_EXTENSION_FUNCTION(clShutdownOCLICD);
#undef KHR_ICD_CHECK_EXTENSION_FUNCTION
return NULL;
}
#ifdef __cplusplus
extern "C" {
#endif
static inline cl_int clGetPlatformIDs_body(
cl_uint num_entries,
cl_platform_id* platforms,
cl_uint* num_platforms)
{
KHRicdVendor* vendor = NULL;
cl_uint i;
if (!num_entries && platforms)
{
return CL_INVALID_VALUE;
}
if (!platforms && !num_platforms)
{
return CL_INVALID_VALUE;
}
// set num_platforms to 0 and set all platform pointers to NULL
if (num_platforms)
{
*num_platforms = 0;
}
for (i = 0; i < num_entries && platforms; ++i)
{
platforms[i] = NULL;
}
// return error if we have no platforms
if (!khrIcdVendors)
{
return CL_PLATFORM_NOT_FOUND_KHR;
}
// otherwise enumerate all platforms
for (vendor = khrIcdVendors; vendor; vendor = vendor->next)
{
if (num_entries && platforms)
{
*(platforms++) = vendor->platform;
--num_entries;
}
if (num_platforms)
{
++(*num_platforms);
}
}
return CL_SUCCESS;
}
cl_int CL_API_CALL clGetPlatformIDs_disp(
cl_uint num_entries,
cl_platform_id* platforms,
cl_uint* num_platforms)
{
return clGetPlatformIDs_body(
num_entries,
platforms,
num_platforms);
}
CL_API_ENTRY cl_int CL_API_CALL clGetPlatformIDs(
cl_uint num_entries,
cl_platform_id* platforms,
cl_uint* num_platforms)
{
// initialize the platforms (in case they have not been already)
khrIcdInitialize();
#if defined(CL_ENABLE_LAYERS)
if (khrFirstLayer)
return khrFirstLayer->dispatch.clGetPlatformIDs(
num_entries,
platforms,
num_platforms);
#endif // defined(CL_ENABLE_LAYERS)
return clGetPlatformIDs_body(
num_entries,
platforms,
num_platforms);
}
static inline void* clGetExtensionFunctionAddress_body(
const char* function_name)
{
void* function_address = NULL;
size_t function_name_length = 0;
KHRicdVendor* vendor = NULL;
KHR_ICD_VALIDATE_HANDLE_RETURN_ERROR(function_name, NULL);
// check if this is an ICD-aware extension
function_address = khrIcdGetExtensionFunctionAddress(function_name);
if (function_address)
{
return function_address;
}
// fall back to vendor extension detection
function_name_length = strlen(function_name);
for (vendor = khrIcdVendors; vendor; vendor = vendor->next)
{
size_t vendor_suffix_length = strlen(vendor->suffix);
if (vendor_suffix_length <= function_name_length &&
vendor_suffix_length > 0)
{
const char* function_suffix =
function_name + function_name_length - vendor_suffix_length;
if (!strcmp(function_suffix, vendor->suffix))
{
return vendor->clGetExtensionFunctionAddress(function_name);
}
}
}
return NULL;
}
void* CL_API_CALL clGetExtensionFunctionAddress_disp(
const char* function_name)
{
return clGetExtensionFunctionAddress_body(
function_name);
}
CL_API_ENTRY void* CL_API_CALL clGetExtensionFunctionAddress(
const char* function_name)
{
// make sure the ICD is initialized
khrIcdInitialize();
#if defined(CL_ENABLE_LAYERS)
if (khrFirstLayer)
return khrFirstLayer->dispatch.clGetExtensionFunctionAddress(
function_name);
#endif // defined(CL_ENABLE_LAYERS)
return clGetExtensionFunctionAddress_body(
function_name);
}
static inline void* clGetExtensionFunctionAddressForPlatform_body(
cl_platform_id platform,
const char* function_name)
{
void* function_address = NULL;
KHR_ICD_VALIDATE_HANDLE_RETURN_ERROR(function_name, NULL);
// check if this is an ICD-aware extension
function_address = khrIcdGetExtensionFunctionAddress(function_name);
if (function_address)
{
return function_address;
}
// This is not an ICD-aware extension, so call into the implementation
// to get the extension function address.
KHR_ICD_VALIDATE_HANDLE_RETURN_ERROR(platform, NULL);
return platform->dispatch->clGetExtensionFunctionAddressForPlatform(
platform,
function_name);
}
void* CL_API_CALL clGetExtensionFunctionAddressForPlatform_disp(
cl_platform_id platform,
const char* function_name)
{
return clGetExtensionFunctionAddressForPlatform_body(
platform,
function_name);
}
CL_API_ENTRY void* CL_API_CALL clGetExtensionFunctionAddressForPlatform(
cl_platform_id platform,
const char* function_name)
{
// make sure the ICD is initialized
khrIcdInitialize();
#if defined(CL_ENABLE_LAYERS)
if (khrFirstLayer)
return khrFirstLayer->dispatch.clGetExtensionFunctionAddressForPlatform(
platform,
function_name);
#endif // defined(CL_ENABLE_LAYERS)
return clGetExtensionFunctionAddressForPlatform_body(
platform,
function_name);
}
#ifdef __cplusplus
}
#endif