/*! \brief Attempts to Initializes an OpenCL context given a specific device type. \returns True if a matching device and context could be constructed. */ inline bool initOpenCLContextType(cl_device_type devType) { std::vector<cl::Platform> platforms; cl::Platform::get(&platforms); //Now cycle through the platforms trying to get a context with a GPU for (auto& platform : platforms) { std::cout << "GL-Context " << _context << ": Trying OpenCL platform - " << platform.getInfo<CL_PLATFORM_VENDOR>() << " - " << platform.getInfo<CL_PLATFORM_NAME>() << " - " << platform.getInfo<CL_PLATFORM_VERSION>() << std::endl; std::vector<cl::Device> devices; try { platform.getDevices(devType, &devices); } catch (...) { continue; } for (auto& device : devices) { std::cout << "GL-Context " << _context << ": Trying Device - " << device.getInfo<CL_DEVICE_NAME>() << " - " << device.getInfo<CL_DRIVER_VERSION>() << std::endl; if (!getCLGLContext(platform, device)) continue; std::cout << "GL-Context " << _context << ": Success" << std::endl; //Success! now set the platform+device and return! _clplatform = platform; _cldevice = device; return true; } } return false; }
inline void initContext() { std::vector<cl::Platform> platforms; cl::Platform::get(&platforms); //Now cycle through the platforms trying to get a context with a GPU for (std::vector<cl::Platform>::const_iterator iPtr = platforms.begin(); iPtr != platforms.end(); ++iPtr) { std::vector<cl::Device> devices; try { iPtr->getDevices(CL_DEVICE_TYPE_GPU, &devices); } catch (...) { continue; } for (std::vector<cl::Device>::const_iterator devPtr = devices.begin(); devPtr != devices.end(); ++devPtr) { if (!getCLGLContext(*iPtr, *devPtr)) continue; //Success! now set the platform+device and return! _platform = *iPtr; cl::GLBuffer::hostTransfers() = false; _device = *devPtr; return; } } //Try and see if there's another OpenCL-OpenGL platform available for (std::vector<cl::Platform>::const_iterator iPtr = platforms.begin(); iPtr != platforms.end(); ++iPtr) { std::vector<cl::Device> devices; try { iPtr->getDevices(CL_DEVICE_TYPE_ALL, &devices); } catch (...) { continue; } for (std::vector<cl::Device>::const_iterator devPtr = devices.begin(); devPtr != devices.end(); ++devPtr) { if (!getCLGLContext(*iPtr, *devPtr)) continue; //Success! now set the platform+device and return! _platform = *iPtr; cl::GLBuffer::hostTransfers() = false; _device = *devPtr; return; } } //No CLGL platform was found so just give the first platform _platform = platforms.front(); //Make sure to set host transfers on! cl::GLBuffer::hostTransfers() = true; cl_context_properties cpsFallBack[] = {CL_CONTEXT_PLATFORM, (cl_context_properties)_platform(), 0}; try { _context = cl::Context(CL_DEVICE_TYPE_ALL, cpsFallBack, NULL, NULL); _device = _context.getInfo<CL_CONTEXT_DEVICES>().front(); } catch (...) { throw std::runtime_error("Failed to create a normal OpenCL context!"); } }