1 //
2 // Copyright 2021 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6 // CLPlatformCL.cpp: Implements the class methods for CLPlatformCL.
7
8 #include "libANGLE/renderer/cl/CLPlatformCL.h"
9
10 #include "common/angle_version_info.h"
11 #include "libANGLE/CLContext.h"
12 #include "libANGLE/CLDevice.h"
13 #include "libANGLE/CLPlatform.h"
14 #include "libANGLE/cl_utils.h"
15 #include "libANGLE/renderer/cl/CLContextCL.h"
16 #include "libANGLE/renderer/cl/CLDeviceCL.h"
17 #include "libANGLE/renderer/cl/cl_util.h"
18
19 extern "C" {
20 #include "icd.h"
21 } // extern "C"
22
23 namespace rx
24 {
25
26 namespace
27 {
28
GetPlatformString(cl_platform_id platform,cl::PlatformInfo name)29 std::string GetPlatformString(cl_platform_id platform, cl::PlatformInfo name)
30 {
31 size_t size = 0u;
32 if (platform->getDispatch().clGetPlatformInfo(platform, cl::ToCLenum(name), 0u, nullptr,
33 &size) == CL_SUCCESS)
34 {
35 std::vector<char> str(size, '\0');
36 if (platform->getDispatch().clGetPlatformInfo(platform, cl::ToCLenum(name), size,
37 str.data(), nullptr) == CL_SUCCESS)
38 {
39 return std::string(str.data());
40 }
41 }
42 ERR() << "Failed to query CL platform info for " << name;
43 return std::string{};
44 }
45
46 } // namespace
47
48 CLPlatformCL::~CLPlatformCL() = default;
49
createInfo() const50 CLPlatformImpl::Info CLPlatformCL::createInfo() const
51 {
52 // Verify that the platform is valid.
53 // We ignore clGetPlatformIDs for ICD case since the ICD Loader has already verified
54 // clIcdGetPlatformIDsKHR exists and is valid.
55 if (mNative == nullptr || (!mIsIcd && mNative->getDispatch().clGetPlatformIDs == nullptr) ||
56 mNative->getDispatch().clGetPlatformInfo == nullptr ||
57 mNative->getDispatch().clGetDeviceIDs == nullptr ||
58 mNative->getDispatch().clGetDeviceInfo == nullptr ||
59 mNative->getDispatch().clCreateContext == nullptr ||
60 mNative->getDispatch().clCreateContextFromType == nullptr ||
61 mNative->getDispatch().clRetainContext == nullptr ||
62 mNative->getDispatch().clReleaseContext == nullptr ||
63 mNative->getDispatch().clGetContextInfo == nullptr ||
64 mNative->getDispatch().clCreateCommandQueue == nullptr ||
65 mNative->getDispatch().clRetainCommandQueue == nullptr ||
66 mNative->getDispatch().clReleaseCommandQueue == nullptr ||
67 mNative->getDispatch().clGetCommandQueueInfo == nullptr ||
68 mNative->getDispatch().clSetCommandQueueProperty == nullptr ||
69 mNative->getDispatch().clCreateBuffer == nullptr ||
70 mNative->getDispatch().clCreateImage2D == nullptr ||
71 mNative->getDispatch().clCreateImage3D == nullptr ||
72 mNative->getDispatch().clRetainMemObject == nullptr ||
73 mNative->getDispatch().clReleaseMemObject == nullptr ||
74 mNative->getDispatch().clGetSupportedImageFormats == nullptr ||
75 mNative->getDispatch().clGetMemObjectInfo == nullptr ||
76 mNative->getDispatch().clGetImageInfo == nullptr ||
77 mNative->getDispatch().clCreateSampler == nullptr ||
78 mNative->getDispatch().clRetainSampler == nullptr ||
79 mNative->getDispatch().clReleaseSampler == nullptr ||
80 mNative->getDispatch().clGetSamplerInfo == nullptr ||
81 mNative->getDispatch().clCreateProgramWithSource == nullptr ||
82 mNative->getDispatch().clCreateProgramWithBinary == nullptr ||
83 mNative->getDispatch().clRetainProgram == nullptr ||
84 mNative->getDispatch().clReleaseProgram == nullptr ||
85 mNative->getDispatch().clBuildProgram == nullptr ||
86 mNative->getDispatch().clUnloadCompiler == nullptr ||
87 mNative->getDispatch().clGetProgramInfo == nullptr ||
88 mNative->getDispatch().clGetProgramBuildInfo == nullptr ||
89 mNative->getDispatch().clCreateKernel == nullptr ||
90 mNative->getDispatch().clCreateKernelsInProgram == nullptr ||
91 mNative->getDispatch().clRetainKernel == nullptr ||
92 mNative->getDispatch().clReleaseKernel == nullptr ||
93 mNative->getDispatch().clSetKernelArg == nullptr ||
94 mNative->getDispatch().clGetKernelInfo == nullptr ||
95 mNative->getDispatch().clGetKernelWorkGroupInfo == nullptr ||
96 mNative->getDispatch().clWaitForEvents == nullptr ||
97 mNative->getDispatch().clGetEventInfo == nullptr ||
98 mNative->getDispatch().clRetainEvent == nullptr ||
99 mNative->getDispatch().clReleaseEvent == nullptr ||
100 mNative->getDispatch().clGetEventProfilingInfo == nullptr ||
101 mNative->getDispatch().clFlush == nullptr || mNative->getDispatch().clFinish == nullptr ||
102 mNative->getDispatch().clEnqueueReadBuffer == nullptr ||
103 mNative->getDispatch().clEnqueueWriteBuffer == nullptr ||
104 mNative->getDispatch().clEnqueueCopyBuffer == nullptr ||
105 mNative->getDispatch().clEnqueueReadImage == nullptr ||
106 mNative->getDispatch().clEnqueueWriteImage == nullptr ||
107 mNative->getDispatch().clEnqueueCopyImage == nullptr ||
108 mNative->getDispatch().clEnqueueCopyImageToBuffer == nullptr ||
109 mNative->getDispatch().clEnqueueCopyBufferToImage == nullptr ||
110 mNative->getDispatch().clEnqueueMapBuffer == nullptr ||
111 mNative->getDispatch().clEnqueueMapImage == nullptr ||
112 mNative->getDispatch().clEnqueueUnmapMemObject == nullptr ||
113 mNative->getDispatch().clEnqueueNDRangeKernel == nullptr ||
114 mNative->getDispatch().clEnqueueTask == nullptr ||
115 mNative->getDispatch().clEnqueueNativeKernel == nullptr ||
116 mNative->getDispatch().clEnqueueMarker == nullptr ||
117 mNative->getDispatch().clEnqueueWaitForEvents == nullptr ||
118 mNative->getDispatch().clEnqueueBarrier == nullptr ||
119 mNative->getDispatch().clGetExtensionFunctionAddress == nullptr)
120 {
121 ERR() << "Missing entry points for OpenCL 1.0";
122 return Info{};
123 }
124
125 // Fetch common platform info
126 Info info;
127 const std::string vendor = GetPlatformString(mNative, cl::PlatformInfo::Vendor);
128 info.profile = GetPlatformString(mNative, cl::PlatformInfo::Profile);
129 info.versionStr = GetPlatformString(mNative, cl::PlatformInfo::Version);
130 info.name = GetPlatformString(mNative, cl::PlatformInfo::Name);
131 std::string extensionStr = GetPlatformString(mNative, cl::PlatformInfo::Extensions);
132
133 if (vendor.empty() || info.profile.empty() || info.versionStr.empty() || info.name.empty() ||
134 extensionStr.empty())
135 {
136 return Info{};
137 }
138
139 // Skip ANGLE CL implementation to prevent passthrough loop
140 if (vendor.compare(cl::Platform::GetVendor()) == 0)
141 {
142 ERR() << "Tried to create CL pass-through back end for ANGLE library";
143 return Info{};
144 }
145
146 // TODO(jplate) Remove workaround after bug is fixed http://anglebug.com/42264583
147 if (info.versionStr.compare(0u, 15u, "OpenCL 3.0 CUDA", 15u) == 0)
148 {
149 extensionStr.append(" cl_khr_depth_images cl_khr_image2d_from_buffer");
150 }
151
152 const cl_version version = ExtractCLVersion(info.versionStr);
153 if (version == 0u)
154 {
155 return Info{};
156 }
157
158 // Remove unsupported and initialize extensions
159 RemoveUnsupportedCLExtensions(extensionStr);
160 info.initializeExtensions(std::move(extensionStr));
161
162 // Skip platform if it is not ICD compatible
163 if (!info.khrICD)
164 {
165 WARN() << "CL platform is not ICD compatible";
166 return Info{};
167 }
168
169 // Customize version string and name
170 info.versionStr += std::string(" (ANGLE ") + angle::GetANGLEVersionString() + ")";
171 info.name.insert(0u, "ANGLE pass-through -> ");
172
173 if (version >= CL_MAKE_VERSION(2, 1, 0) &&
174 mNative->getDispatch().clGetPlatformInfo(mNative, CL_PLATFORM_HOST_TIMER_RESOLUTION,
175 sizeof(info.hostTimerRes), &info.hostTimerRes,
176 nullptr) != CL_SUCCESS)
177 {
178 ERR() << "Failed to query CL platform info for CL_PLATFORM_HOST_TIMER_RESOLUTION";
179 return Info{};
180 }
181
182 if (version < CL_MAKE_VERSION(3, 0, 0))
183 {
184 info.version = version;
185 }
186 else
187 {
188 if (mNative->getDispatch().clGetPlatformInfo(mNative, CL_PLATFORM_NUMERIC_VERSION,
189 sizeof(info.version), &info.version,
190 nullptr) != CL_SUCCESS)
191 {
192 ERR() << "Failed to query CL platform info for CL_PLATFORM_NUMERIC_VERSION";
193 return Info{};
194 }
195 else if (CL_VERSION_MAJOR(info.version) != CL_VERSION_MAJOR(version) ||
196 CL_VERSION_MINOR(info.version) != CL_VERSION_MINOR(version))
197 {
198 WARN() << "CL_PLATFORM_NUMERIC_VERSION = " << CL_VERSION_MAJOR(info.version) << '.'
199 << CL_VERSION_MINOR(info.version)
200 << " does not match version string: " << info.versionStr;
201 }
202
203 size_t valueSize = 0u;
204 if (mNative->getDispatch().clGetPlatformInfo(mNative, CL_PLATFORM_EXTENSIONS_WITH_VERSION,
205 0u, nullptr, &valueSize) != CL_SUCCESS ||
206 (valueSize % sizeof(decltype(info.extensionsWithVersion)::value_type)) != 0u)
207 {
208 ERR() << "Failed to query CL platform info for CL_PLATFORM_EXTENSIONS_WITH_VERSION";
209 return Info{};
210 }
211 info.extensionsWithVersion.resize(valueSize /
212 sizeof(decltype(info.extensionsWithVersion)::value_type));
213 if (mNative->getDispatch().clGetPlatformInfo(mNative, CL_PLATFORM_EXTENSIONS_WITH_VERSION,
214 valueSize, info.extensionsWithVersion.data(),
215 nullptr) != CL_SUCCESS)
216 {
217 ERR() << "Failed to query CL platform info for CL_PLATFORM_EXTENSIONS_WITH_VERSION";
218 return Info{};
219 }
220 RemoveUnsupportedCLExtensions(info.extensionsWithVersion);
221 }
222
223 if (info.version >= CL_MAKE_VERSION(1, 1, 0) &&
224 (mNative->getDispatch().clSetEventCallback == nullptr ||
225 mNative->getDispatch().clCreateSubBuffer == nullptr ||
226 mNative->getDispatch().clSetMemObjectDestructorCallback == nullptr ||
227 mNative->getDispatch().clCreateUserEvent == nullptr ||
228 mNative->getDispatch().clSetUserEventStatus == nullptr ||
229 mNative->getDispatch().clEnqueueReadBufferRect == nullptr ||
230 mNative->getDispatch().clEnqueueWriteBufferRect == nullptr ||
231 mNative->getDispatch().clEnqueueCopyBufferRect == nullptr))
232 {
233 ERR() << "Missing entry points for OpenCL 1.1";
234 return Info{};
235 }
236
237 if (info.version >= CL_MAKE_VERSION(1, 2, 0) &&
238 (mNative->getDispatch().clCreateSubDevices == nullptr ||
239 mNative->getDispatch().clRetainDevice == nullptr ||
240 mNative->getDispatch().clReleaseDevice == nullptr ||
241 mNative->getDispatch().clCreateImage == nullptr ||
242 mNative->getDispatch().clCreateProgramWithBuiltInKernels == nullptr ||
243 mNative->getDispatch().clCompileProgram == nullptr ||
244 mNative->getDispatch().clLinkProgram == nullptr ||
245 mNative->getDispatch().clUnloadPlatformCompiler == nullptr ||
246 mNative->getDispatch().clGetKernelArgInfo == nullptr ||
247 mNative->getDispatch().clEnqueueFillBuffer == nullptr ||
248 mNative->getDispatch().clEnqueueFillImage == nullptr ||
249 mNative->getDispatch().clEnqueueMigrateMemObjects == nullptr ||
250 mNative->getDispatch().clEnqueueMarkerWithWaitList == nullptr ||
251 mNative->getDispatch().clEnqueueBarrierWithWaitList == nullptr ||
252 mNative->getDispatch().clGetExtensionFunctionAddressForPlatform == nullptr))
253 {
254 ERR() << "Missing entry points for OpenCL 1.2";
255 return Info{};
256 }
257
258 if (info.version >= CL_MAKE_VERSION(2, 0, 0) &&
259 (mNative->getDispatch().clCreateCommandQueueWithProperties == nullptr ||
260 mNative->getDispatch().clCreatePipe == nullptr ||
261 mNative->getDispatch().clGetPipeInfo == nullptr ||
262 mNative->getDispatch().clSVMAlloc == nullptr ||
263 mNative->getDispatch().clSVMFree == nullptr ||
264 mNative->getDispatch().clEnqueueSVMFree == nullptr ||
265 mNative->getDispatch().clEnqueueSVMMemcpy == nullptr ||
266 mNative->getDispatch().clEnqueueSVMMemFill == nullptr ||
267 mNative->getDispatch().clEnqueueSVMMap == nullptr ||
268 mNative->getDispatch().clEnqueueSVMUnmap == nullptr ||
269 mNative->getDispatch().clCreateSamplerWithProperties == nullptr ||
270 mNative->getDispatch().clSetKernelArgSVMPointer == nullptr ||
271 mNative->getDispatch().clSetKernelExecInfo == nullptr))
272 {
273 ERR() << "Missing entry points for OpenCL 2.0";
274 return Info{};
275 }
276
277 if (info.version >= CL_MAKE_VERSION(2, 1, 0) &&
278 (mNative->getDispatch().clCloneKernel == nullptr ||
279 mNative->getDispatch().clCreateProgramWithIL == nullptr ||
280 mNative->getDispatch().clEnqueueSVMMigrateMem == nullptr ||
281 mNative->getDispatch().clGetDeviceAndHostTimer == nullptr ||
282 mNative->getDispatch().clGetHostTimer == nullptr ||
283 mNative->getDispatch().clGetKernelSubGroupInfo == nullptr ||
284 mNative->getDispatch().clSetDefaultDeviceCommandQueue == nullptr))
285 {
286 ERR() << "Missing entry points for OpenCL 2.1";
287 return Info{};
288 }
289
290 if (info.version >= CL_MAKE_VERSION(2, 2, 0) &&
291 (mNative->getDispatch().clSetProgramReleaseCallback == nullptr ||
292 mNative->getDispatch().clSetProgramSpecializationConstant == nullptr))
293 {
294 ERR() << "Missing entry points for OpenCL 2.2";
295 return Info{};
296 }
297
298 if (info.version >= CL_MAKE_VERSION(3, 0, 0) &&
299 (mNative->getDispatch().clCreateBufferWithProperties == nullptr ||
300 mNative->getDispatch().clCreateImageWithProperties == nullptr ||
301 mNative->getDispatch().clSetContextDestructorCallback == nullptr))
302 {
303 ERR() << "Missing entry points for OpenCL 3.0";
304 return Info{};
305 }
306
307 return info;
308 }
309
createDevices() const310 CLDeviceImpl::CreateDatas CLPlatformCL::createDevices() const
311 {
312 CLDeviceImpl::CreateDatas createDatas;
313
314 // Fetch all regular devices. This does not include CL_DEVICE_TYPE_CUSTOM, which are not
315 // supported by the CL pass-through back end because they have no standard feature set.
316 // This makes them unreliable for the purpose of this back end.
317 cl_uint numDevices = 0u;
318 if (mNative->getDispatch().clGetDeviceIDs(mNative, CL_DEVICE_TYPE_ALL, 0u, nullptr,
319 &numDevices) == CL_SUCCESS)
320 {
321 std::vector<cl_device_id> nativeDevices(numDevices, nullptr);
322 if (mNative->getDispatch().clGetDeviceIDs(mNative, CL_DEVICE_TYPE_ALL, numDevices,
323 nativeDevices.data(), nullptr) == CL_SUCCESS)
324 {
325 // Fetch all device types for front end initialization, and find the default device.
326 // If none exists declare first device as default.
327 std::vector<cl::DeviceType> types(nativeDevices.size());
328 size_t defaultIndex = 0u;
329 for (size_t index = 0u; index < nativeDevices.size(); ++index)
330 {
331 if (nativeDevices[index]->getDispatch().clGetDeviceInfo(
332 nativeDevices[index], CL_DEVICE_TYPE, sizeof(cl_device_type), &types[index],
333 nullptr) == CL_SUCCESS)
334 {
335 // If default device found, select it
336 if (types[index].intersects(CL_DEVICE_TYPE_DEFAULT))
337 {
338 defaultIndex = index;
339 }
340 }
341 else
342 {
343 types.clear();
344 nativeDevices.clear();
345 }
346 }
347
348 for (size_t index = 0u; index < nativeDevices.size(); ++index)
349 {
350 // Make sure the default bit is set in exactly one device
351 if (index == defaultIndex)
352 {
353 types[index].set(CL_DEVICE_TYPE_DEFAULT);
354 }
355 else
356 {
357 types[index].clear(CL_DEVICE_TYPE_DEFAULT);
358 }
359
360 cl_device_id nativeDevice = nativeDevices[index];
361 createDatas.emplace_back(types[index], [nativeDevice](const cl::Device &device) {
362 return CLDeviceCL::Ptr(new CLDeviceCL(device, nativeDevice));
363 });
364 }
365 }
366 }
367
368 if (createDatas.empty())
369 {
370 ERR() << "Failed to query CL devices";
371 }
372 return createDatas;
373 }
374
createContext(cl::Context & context,const cl::DevicePtrs & devices,bool userSync,CLContextImpl::Ptr * contextOut)375 angle::Result CLPlatformCL::createContext(cl::Context &context,
376 const cl::DevicePtrs &devices,
377 bool userSync,
378 CLContextImpl::Ptr *contextOut)
379 {
380 cl_int errorCode = CL_SUCCESS;
381 cl_context_properties properties[] = {
382 CL_CONTEXT_PLATFORM, reinterpret_cast<cl_context_properties>(mNative),
383 userSync && mPlatform.isVersionOrNewer(1u, 2u) ? CL_CONTEXT_INTEROP_USER_SYNC : 0, CL_TRUE,
384 0};
385
386 std::vector<cl_device_id> nativeDevices;
387 for (const cl::DevicePtr &device : devices)
388 {
389 nativeDevices.emplace_back(device->getImpl<CLDeviceCL>().getNative());
390 }
391
392 cl_context nativeContext = mNative->getDispatch().clCreateContext(
393 properties, static_cast<cl_uint>(nativeDevices.size()), nativeDevices.data(),
394 cl::Context::ErrorCallback, &context, &errorCode);
395 ANGLE_CL_TRY(errorCode);
396
397 *contextOut = CLContextImpl::Ptr(
398 nativeContext != nullptr ? new CLContextCL(context, nativeContext) : nullptr);
399 return angle::Result::Continue;
400 }
401
createContextFromType(cl::Context & context,cl::DeviceType deviceType,bool userSync,CLContextImpl::Ptr * contextOut)402 angle::Result CLPlatformCL::createContextFromType(cl::Context &context,
403 cl::DeviceType deviceType,
404 bool userSync,
405 CLContextImpl::Ptr *contextOut)
406 {
407 cl_int errorCode = CL_SUCCESS;
408 cl_context_properties properties[] = {
409 CL_CONTEXT_PLATFORM, reinterpret_cast<cl_context_properties>(mNative),
410 userSync && mPlatform.isVersionOrNewer(1u, 2u) ? CL_CONTEXT_INTEROP_USER_SYNC : 0, CL_TRUE,
411 0};
412
413 cl_context nativeContext = mNative->getDispatch().clCreateContextFromType(
414 properties, deviceType.get(), cl::Context::ErrorCallback, &context, &errorCode);
415 ANGLE_CL_TRY(errorCode);
416
417 *contextOut = CLContextImpl::Ptr(
418 nativeContext != nullptr ? new CLContextCL(context, nativeContext) : nullptr);
419 return angle::Result::Continue;
420 }
421
unloadCompiler()422 angle::Result CLPlatformCL::unloadCompiler()
423 {
424 ANGLE_CL_TRY(mNative->getDispatch().clUnloadPlatformCompiler(mNative));
425 return angle::Result::Continue;
426 }
427
Initialize(CreateFuncs & createFuncs,bool isIcd)428 void CLPlatformCL::Initialize(CreateFuncs &createFuncs, bool isIcd)
429 {
430 // Using khrIcdInitialize() of the third party Khronos OpenCL ICD Loader to
431 // enumerate the available OpenCL implementations on the system. They will be
432 // stored in the singly linked list khrIcdVendors of the C struct KHRicdVendor.
433 khrIcdInitialize();
434
435 // The ICD loader will also enumerate ANGLE's OpenCL library if it is registered. Our
436 // OpenCL entry points for the ICD enumeration are reentrant, but at this point of the
437 // initialization there are no platforms available, so our platforms will not be found.
438 // This is intended as this back end should only enumerate non-ANGLE implementations.
439
440 // Iterating through the singly linked list khrIcdVendors to create
441 // an ANGLE CL pass-through platform for each found ICD platform.
442 for (KHRicdVendor *vendorIt = khrIcdVendors; vendorIt != nullptr; vendorIt = vendorIt->next)
443 {
444 cl_platform_id nativePlatform = vendorIt->platform;
445 createFuncs.emplace_back([nativePlatform, isIcd](const cl::Platform &platform) {
446 return Ptr(new CLPlatformCL(platform, nativePlatform, isIcd));
447 });
448 }
449 }
450
CLPlatformCL(const cl::Platform & platform,cl_platform_id native,bool isIcd)451 CLPlatformCL::CLPlatformCL(const cl::Platform &platform, cl_platform_id native, bool isIcd)
452 : CLPlatformImpl(platform), mNative(native), mIsIcd(isIcd)
453 {}
454
455 } // namespace rx
456