|
26 | 26 | QLatin1String QgsOpenClUtils::SETTINGS_KEY = QLatin1Literal( "OpenClEnabled" ); |
27 | 27 | QLatin1String QgsOpenClUtils::LOGMESSAGE_TAG = QLatin1Literal( "OpenCL" ); |
28 | 28 | bool QgsOpenClUtils::sAvailable = false; |
29 | | - |
| 29 | +cl::Platform QgsOpenClUtils::sPlatform = cl::Platform(); |
| 30 | +cl::Device QgsOpenClUtils::sDevice = cl::Device(); |
30 | 31 |
|
31 | 32 | void QgsOpenClUtils::init() |
32 | 33 | { |
33 | 34 | static bool initialized = false; |
34 | 35 | if ( ! initialized ) |
35 | 36 | { |
36 | | - std::vector<cl::Platform> platforms; |
37 | | - cl::Platform::get( &platforms ); |
38 | | - cl::Platform plat; |
39 | | - cl::Device dev; |
40 | | - for ( auto &p : platforms ) |
| 37 | + try |
41 | 38 | { |
42 | | - std::string platver = p.getInfo<CL_PLATFORM_VERSION>(); |
43 | | - QgsDebugMsg( QStringLiteral( "Found OpenCL platform %1: %2" ).arg( QString::fromStdString( platver ), QString::fromStdString( p.getInfo<CL_PLATFORM_NAME>() ) ) ); |
44 | | - if ( platver.find( "OpenCL 1." ) != std::string::npos ) |
| 39 | + std::vector<cl::Platform> platforms; |
| 40 | + cl::Platform::get( &platforms ); |
| 41 | + cl::Platform plat; |
| 42 | + cl::Device dev; |
| 43 | + for ( auto &p : platforms ) |
45 | 44 | { |
46 | | - std::vector<cl::Device> devices; |
47 | | - // Check for a GPU device |
48 | | - p.getDevices( CL_DEVICE_TYPE_GPU, &devices ); |
49 | | - if ( devices.size() > 0 ) |
| 45 | + std::string platver = p.getInfo<CL_PLATFORM_VERSION>(); |
| 46 | + QgsDebugMsg( QStringLiteral( "Found OpenCL platform %1: %2" ).arg( QString::fromStdString( platver ), QString::fromStdString( p.getInfo<CL_PLATFORM_NAME>() ) ) ); |
| 47 | + if ( platver.find( "OpenCL 1." ) != std::string::npos ) |
50 | 48 | { |
51 | | - // Got one! |
52 | | - plat = p; |
53 | | - dev = devices[0]; |
54 | | - break; |
| 49 | + std::vector<cl::Device> devices; |
| 50 | + // Check for a GPU device |
| 51 | + try |
| 52 | + { |
| 53 | + p.getDevices( CL_DEVICE_TYPE_GPU, &devices ); |
| 54 | + } |
| 55 | + catch ( cl::Error &e ) |
| 56 | + { |
| 57 | + QgsDebugMsgLevel( QStringLiteral( "Error %1 on platform %3 searching for OpenCL device: %2" ) |
| 58 | + .arg( errorText( e.err() ), |
| 59 | + QString::fromStdString( e.what() ), |
| 60 | + QString::fromStdString( p.getInfo<CL_PLATFORM_NAME>() ) ), 2 ); |
| 61 | + } |
| 62 | + if ( devices.size() > 0 ) |
| 63 | + { |
| 64 | + // Got one! |
| 65 | + plat = p; |
| 66 | + dev = devices[0]; |
| 67 | + break; |
| 68 | + } |
55 | 69 | } |
56 | 70 | } |
57 | | - } |
58 | | - if ( plat() == 0 ) |
59 | | - { |
60 | | - QgsMessageLog::logMessage( QObject::tr( "No OpenCL 1.x platform with GPU found." ), LOGMESSAGE_TAG, Qgis::Warning ); |
61 | | - sAvailable = false; |
62 | | - } |
63 | | - else |
64 | | - { |
65 | | - cl::Platform newP = cl::Platform::setDefault( plat ); |
66 | | - if ( newP != plat ) |
| 71 | + if ( plat() == 0 ) |
67 | 72 | { |
68 | | - QgsMessageLog::logMessage( QObject::tr( "Error setting default platform." ), LOGMESSAGE_TAG, Qgis::Warning ); |
| 73 | + QgsMessageLog::logMessage( QObject::tr( "No OpenCL 1.x platform with GPU found." ), LOGMESSAGE_TAG, Qgis::Warning ); |
69 | 74 | sAvailable = false; |
70 | 75 | } |
71 | 76 | else |
72 | 77 | { |
73 | | - cl::Device::setDefault( dev ); |
74 | | - QgsDebugMsg( QStringLiteral( "Found OpenCL device %1" ).arg( QString::fromStdString( dev.getInfo<CL_DEVICE_NAME>() ) ) ); |
75 | | - sAvailable = true; |
| 78 | + cl::Platform newP = cl::Platform::setDefault( plat ); |
| 79 | + if ( newP != plat ) |
| 80 | + { |
| 81 | + QgsMessageLog::logMessage( QObject::tr( "Error setting default platform." ), LOGMESSAGE_TAG, Qgis::Warning ); |
| 82 | + sAvailable = false; |
| 83 | + } |
| 84 | + else |
| 85 | + { |
| 86 | + cl::Device::setDefault( dev ); |
| 87 | + QgsDebugMsgLevel( QStringLiteral( "Found OpenCL device %1" ) |
| 88 | + .arg( QString::fromStdString( dev.getInfo<CL_DEVICE_NAME>() ) ), 2 ); |
| 89 | + sAvailable = true; |
| 90 | + sDevice = dev; |
| 91 | + sPlatform = plat; |
| 92 | + } |
76 | 93 | } |
77 | 94 | } |
| 95 | + catch ( cl::Error &e ) |
| 96 | + { |
| 97 | + QgsMessageLog::logMessage( QObject::tr( "Error %1 searching for OpenCL device: %2" ) |
| 98 | + .arg( errorText( e.err() ), QString::fromStdString( e.what() ) ), |
| 99 | + LOGMESSAGE_TAG, Qgis::Critical ); |
| 100 | + sAvailable = false; |
| 101 | + } |
78 | 102 | initialized = true; |
79 | 103 | } |
80 | 104 | } |
@@ -220,3 +244,15 @@ QString QgsOpenClUtils::errorText( const int errorCode ) |
220 | 244 | default: return QStringLiteral( "CL_UNKNOWN_ERROR" ); |
221 | 245 | } |
222 | 246 | } |
| 247 | + |
| 248 | +cl::Context QgsOpenClUtils::context() |
| 249 | +{ |
| 250 | + if ( available() && sPlatform() && sDevice() ) |
| 251 | + { |
| 252 | + return cl::Context( sDevice ); |
| 253 | + } |
| 254 | + else |
| 255 | + { |
| 256 | + return cl::Context(); |
| 257 | + } |
| 258 | +} |
0 commit comments