Ejemplo n.º 1
0
 /// Returns the address of the \p function_name extension
 /// function. Returns \c 0 if \p function_name is invalid.
 void* get_extension_function_address(const char *function_name) const
 {
     #ifdef BOOST_COMPUTE_CL_VERSION_1_2
     return clGetExtensionFunctionAddressForPlatform(m_platform,
                                                     function_name);
     #else
     return clGetExtensionFunctionAddress(function_name);
     #endif
 }
Ejemplo n.º 2
0
void * WINAPI wine_clGetExtensionFunctionAddress(const char * func_name)
{
    void * ret = 0;
    TRACE("(%s)\n",func_name);
#if 0
    ret = clGetExtensionFunctionAddress(func_name);
#else
    FIXME("extensions not implemented\n");
#endif
    TRACE("(%s)=%p\n",func_name, ret);
    return ret;
}
Ejemplo n.º 3
0
void *
CLDevice::get_extension_function (const char *func_name)
{
    XCAM_ASSERT (func_name);
    void *ext_func = NULL;

#if defined (CL_VERSION_1_2) && (CL_VERSION_1_2 == 1)
    ext_func = (void *) clGetExtensionFunctionAddressForPlatform (_platform_id, func_name);
#else
    ext_func = (void *) clGetExtensionFunctionAddress (func_name);
#endif
    if (!ext_func)
        XCAM_LOG_ERROR ("ocl driver get extension function (%s) failed", func_name);

    return ext_func;
}
int OCL_Environment::OCL_LoadLibrary()
{

	// EXTENSIONS
	// OCL D3D10 interop functions
	clGetDeviceIDsFromD3D10KHRFunc = (clGetDeviceIDsFromD3D10KHRFuncType) clGetExtensionFunctionAddress("clGetDeviceIDsFromD3D10KHR");
	clCreateFromD3D10BufferKHRFunc = (clCreateFromD3D10BufferKHRFuncType) clGetExtensionFunctionAddress("clCreateFromD3D10BufferKHR");
	clCreateFromD3D10Texture2DKHRFunc = (clCreateFromD3D10Texture2DKHRFuncType) clGetExtensionFunctionAddress("clCreateFromD3D10Texture2DKHR");
	clCreateFromD3D10Texture3DKHRFunc = (clCreateFromD3D10Texture3DKHRFuncType) clGetExtensionFunctionAddress("clCreateFromD3D10Texture3DKHR");
	clEnqueueAcquireD3D10ObjectsKHRFunc = (clEnqueueAcquireD3D10ObjectsKHRFuncType) clGetExtensionFunctionAddress("clEnqueueAcquireD3D10ObjectsKHR");
	clEnqueueReleaseD3D10ObjectsKHRFunc = (clEnqueueReleaseD3D10ObjectsKHRFuncType) clGetExtensionFunctionAddress("clEnqueueReleaseD3D10ObjectsKHR");

	return 0;
}
Ejemplo n.º 5
0
void initCL(cl::Context& _context, std::vector<cl::Device>& _devices, cl::CommandQueue& _queue)
{
	cl_int err = CL_SUCCESS;

	std::vector<cl::Platform> platforms;
	cl::Platform::get(&platforms);
	if (platforms.size() == 0)
	{
		throw std::exception("No OpenCL platform found.");
	}

	HGLRC glCtx = wglGetCurrentContext();

	cl_context_properties properties[] = {
		CL_CONTEXT_PLATFORM, (cl_context_properties) platforms[0](),
		CL_WGL_HDC_KHR, (cl_context_properties)wglGetCurrentDC(),
		CL_GL_CONTEXT_KHR, (cl_context_properties)glCtx,
		0
	};

	static clGetGLContextInfoKHR_fn clGetGLContextInfoKHR;
	if (!clGetGLContextInfoKHR)
	{
		clGetGLContextInfoKHR = (clGetGLContextInfoKHR_fn) clGetExtensionFunctionAddress("clGetGLContextInfoKHR");
		if (!clGetGLContextInfoKHR)
		{
			throw std::exception("Failed to query proc address for clGetGLContextInfoKHR.");
		}
	}

	cl_device_id interopDevice;
	cl_int status = clGetGLContextInfoKHR(properties, CL_CURRENT_DEVICE_FOR_GL_CONTEXT_KHR, sizeof(cl_device_id), &interopDevice, nullptr);
	cl::Device dev(interopDevice);

	_devices.push_back(dev);

	_context = cl::Context(_devices, properties);

	_queue = cl::CommandQueue(_context, dev, CL_QUEUE_PROFILING_ENABLE, &err);
}
Ejemplo n.º 6
0
CL::CL() {
	cl_int err;

	std::vector<cl::Platform> platforms;
	if(cl::Platform::get(&platforms) == CL_INVALID_VALUE) {
		fprintf(stderr, "[OpenCL] No platforms available\n");
		util_abort();
	}

	platform_ = platforms[0]; //Just select the first platform

	std::string name, version, extensions;

	platform_.getInfo(CL_PLATFORM_NAME, &name);
	platform_.getInfo(CL_PLATFORM_VERSION, &version);
	platform_.getInfo(CL_PLATFORM_EXTENSIONS, &extensions);

	fprintf(verbose, "[OpenCL] Platform: %s %s\n"
	        "  Extensions: %s\n", name.c_str(), version.c_str() ,extensions.c_str());

#if defined (__APPLE__) || defined(MACOSX)
	CGLContextObj kCGLContext = CGLGetCurrentContext();
	CGLShareGroupObj kCGLShareGroup = CGLGetShareGroup(kCGLContext);
	cl_context_properties properties[] =
	{
		CL_CONTEXT_PROPERTY_USE_CGL_SHAREGROUP_APPLE, (cl_context_properties)kCGLShareGroup,
		0
	};
#elif defined WIN32
	HGLRC current_context = wglGetCurrentContext();
	HDC current_dc = wglGetCurrentDC();
	if(current_dc == NULL || current_context == NULL) {
		fprintf(stderr,"[OpenCL] No OpenGL context active\n");
		util_abort();
	}

	cl_context_properties properties[] = {
		CL_CONTEXT_PLATFORM, (cl_context_properties)platform_(),
		CL_WGL_HDC_KHR, (intptr_t) current_dc,
		CL_GL_CONTEXT_KHR, (intptr_t) current_context,
		0
	};

#else
	if(glXGetCurrentContext() == NULL) {
		fprintf(stderr, "[OpenCL] glXGetCurrentContex() return NULL. Make sure to create OpenGL context before create the CL-context\n");
		util_abort();
	}
	cl_context_properties properties[] =
	{
		CL_GL_CONTEXT_KHR, (cl_context_properties)glXGetCurrentContext(),
		CL_GLX_DISPLAY_KHR, (cl_context_properties)glXGetCurrentDisplay(),
		CL_CONTEXT_PLATFORM, (cl_context_properties)(platform_)(),
		0
	};
#endif

	
	static CL_API_ENTRY cl_int (CL_API_CALL
	*clGetGLContextInfoKHR)(const cl_context_properties *properties,
														cl_gl_context_info param_name,
														size_t param_value_size,
														void *param_value,
														size_t *param_value_size_ret)=NULL;

	clGetGLContextInfoKHR = (clGetGLContextInfoKHR_fn) clGetExtensionFunctionAddress("clGetGLContextInfoKHR");

	cl_device_id devices[32];
	size_t deviceSize = 0;
	err = clGetGLContextInfoKHR(properties, 
									CL_DEVICES_FOR_GL_CONTEXT_KHR,
									32 * sizeof(cl_device_id), 
									devices, 
									&deviceSize);

	if(deviceSize == 0) {
		fprintf(stderr, "[OpenCL] Interop not possible\n");
		util_abort();
	}

	
	
	cl_bool image_support, available;
	size_t max_width, max_height;
	cl_uint num_cores, frequency;
	cl_device_type _type;
	std::string type;

	fprintf(verbose, "[OpenCL] Available devices: \n");

	for(int i=0; i< (deviceSize / sizeof(cl_device_id)); ++i) {
		cl::Device device(devices[i]);
		device.getInfo(CL_DEVICE_VENDOR, &name);
		device.getInfo(CL_DEVICE_VERSION, &version);
		device.getInfo(CL_DEVICE_EXTENSIONS, &extensions);
		device.getInfo(CL_DEVICE_AVAILABLE, &available);
		device.getInfo(CL_DEVICE_IMAGE_SUPPORT, &image_support);
		device.getInfo(CL_DEVICE_IMAGE2D_MAX_WIDTH, &max_width);
		device.getInfo(CL_DEVICE_IMAGE2D_MAX_HEIGHT, &max_height);
		device.getInfo( CL_DEVICE_MAX_COMPUTE_UNITS , &num_cores);
		device.getInfo(CL_DEVICE_MAX_CLOCK_FREQUENCY, &frequency);
		device.getInfo(CL_DEVICE_TYPE, &_type);

		switch(_type) {
			case CL_DEVICE_TYPE_GPU:
				type = "GPU";
				break;
			case CL_DEVICE_TYPE_CPU:
				type = "CPU";
				break;
			case CL_DEVICE_TYPE_ACCELERATOR:
				type =  "Accelerator";
				break;
		}

		fprintf(verbose, "[OpenCL] Device (%p): %s %s (%s)\n"
				"		Cores: %u, Frequency: %u MHz, Available: %s,"
				"		Image support: %s, max size: %lux%lu\n"
				"		Extensions: %s\n --- \n", (device)(),  name.c_str(), version.c_str(), type.c_str(), num_cores, frequency,available?"YES":"NO",image_support?"YES":"NO", max_width, max_height, extensions.c_str());
		devices_.push_back(device);
	}
	
	fprintf(verbose, "\n-------------------\n");

	cl_device_id device_id;


	context_ = cl::Context(devices_, properties, &CL::cl_error_callback, nullptr, &err);


	if(err != CL_SUCCESS) {
		fprintf(stderr, "[OpenCL] Failed to create context: %s\n", errorString(err));
		util_abort();
	}



	err = clGetGLContextInfoKHR(properties, CL_CURRENT_DEVICE_FOR_GL_CONTEXT_KHR, sizeof(device_id), &device_id, NULL);
	if(err != CL_SUCCESS) {
		fprintf(stderr, "[OpenCL] Failed to get current device for context: %s\n", errorString(err));
		util_abort();
	}

	context_device_ = cl::Device(device_id);

	
	context_device_.getInfo(CL_DEVICE_VENDOR, &name);
	context_device_.getInfo(CL_DEVICE_VERSION, &version);
		fprintf(verbose, "[OpenCL] Context Device (%p): %s %s\n",(context_device_)(),  name.c_str(), version.c_str());

	queue_ = cl::CommandQueue(context_, context_device_, 0, &err);

	if(err != CL_SUCCESS) {
		fprintf(stderr, "[OpenCL] Failed to create a command queue: %s\n", errorString(err));
		util_abort();
	}
}
Ejemplo n.º 7
0
    CLContext::CLContext() {
#if defined(__APPLE__)
        CGLContextObj kCGLContext = CGLGetCurrentContext();              
        CGLShareGroupObj kCGLShareGroup = CGLGetShareGroup(kCGLContext);
        
        cl_context_properties properties[] = { 
            CL_CONTEXT_PROPERTY_USE_CGL_SHAREGROUP_APPLE, 
            (cl_context_properties)kCGLShareGroup, 0 
        };
        
        context_ = clCreateContext(properties, 0, 0, clLogMessagesToStdoutAPPLE, 0, 0);
        if (!context_)
            crash("failed to create context");
        
        uint device_count;
        cl_device_id device_ids[16];
        size_t returned_size;
        
        int err = clGetContextInfo(context_, CL_CONTEXT_DEVICES, sizeof(device_ids), device_ids, &returned_size);
        if (err != CL_SUCCESS)
            crash("failed to get devices");
		
        device_count = (int)returned_size / sizeof(cl_device_id);
        
        int i = 0;
        bool device_found = false;
        cl_device_type device_type;	
        for (i = 0; i < device_count; i++) 
        {
            clGetDeviceInfo(device_ids[i], CL_DEVICE_TYPE, sizeof(cl_device_type), &device_type, NULL);
            if (device_type == CL_DEVICE_TYPE_GPU)
            {
                device_id_ = device_ids[i];
                device_found = true;
                break;
            }
        }
        
        if (!device_found)
            crash("no GPU device found");
#elif defined(WIN32)
		HGLRC hrc = wglGetCurrentContext();
		HDC hdc = wglGetCurrentDC();

		if (!hrc || !hdc)
			crash("failed to get HDC or HGLRC");

		cl_platform_id platform_ids[16];
		size_t platform_count;

		int err = clGetPlatformIDs(sizeof(platform_ids) / sizeof(platform_ids[0]), platform_ids, &platform_count);
		if (err != CL_SUCCESS || !platform_count)
			crash("failed to get platforms");

		for (uint i = 0; i < platform_count; ++i) {
			char platform_name[100];
			char platform_version[100];
			char platform_vendor[100];
			char platform_extensions[1000];
            err = clGetPlatformInfo(platform_ids[i],
                                    CL_PLATFORM_NAME,
                                    sizeof(platform_name),
                                    platform_name,
                                    NULL);
			if (err != CL_SUCCESS)
				crash("failed to get platform name");
            err = clGetPlatformInfo(platform_ids[i],
                                    CL_PLATFORM_VERSION,
                                    sizeof(platform_version),
                                    platform_version,
                                    NULL);
			if (err != CL_SUCCESS)
				crash("failed to get platform version");
            err = clGetPlatformInfo(platform_ids[i],
                                    CL_PLATFORM_VENDOR,
                                    sizeof(platform_vendor),
                                    platform_vendor,
                                    NULL);
			if (err != CL_SUCCESS)
				crash("failed to get platform vendor");
            err = clGetPlatformInfo(platform_ids[i],
                                    CL_PLATFORM_EXTENSIONS,
                                    sizeof(platform_extensions),
                                    platform_extensions,
                                    NULL);
			if (err != CL_SUCCESS)
				crash("failed to get platform extensions");

			cout << "platform name: " << platform_name << endl;
			cout << "platform version: " << platform_version << endl;
			cout << "platform vendor: " << platform_vendor << endl;
			cout << "platform extensions: " << platform_extensions << endl;
			cout << endl;
		}

		cl_platform_id platform_id = platform_ids[0];

		typedef CL_API_ENTRY cl_int (CL_API_CALL *clGetGLContextInfoKHR_fn)(
			const cl_context_properties *properties,
			cl_gl_context_info param_name,
			size_t param_value_size,
			void *param_value,
			size_t *param_value_size_ret);

		clGetGLContextInfoKHR_fn clGetGLContextInfoKHR;
		clGetGLContextInfoKHR = (clGetGLContextInfoKHR_fn) clGetExtensionFunctionAddress("clGetGLContextInfoKHR");
		if (!clGetGLContextInfoKHR)
			crash("failed to query proc address for clGetGLContextInfoKHR");

#ifndef NO_OPENGL_SHARING

		cl_context_properties properties[] = 
        {
			CL_CONTEXT_PLATFORM, (cl_context_properties) platform_id,
			CL_GL_CONTEXT_KHR,   (cl_context_properties) hrc,
			CL_WGL_HDC_KHR,      (cl_context_properties) hdc,
			0
        };

		size_t device_count;
		err = clGetGLContextInfoKHR(properties, 
		                            CL_CURRENT_DEVICE_FOR_GL_CONTEXT_KHR,
		                            sizeof(device_id_), 
		                            &device_id_, 
		                            &device_count);
		if (err != CL_SUCCESS || !device_count)
			crash("failed to get CL device from GL context");

		char device_name[100];
		char device_version[100];
		cl_device_type device_type;
		cl_bool device_image_support;
		err = clGetDeviceInfo(device_id_, CL_DEVICE_NAME, sizeof(device_name), device_name, NULL);
		if (err != CL_SUCCESS)
			crash("failed to get device name");
		err = clGetDeviceInfo(device_id_, CL_DEVICE_VERSION, sizeof(device_version), device_version, NULL);
		if (err != CL_SUCCESS)
			crash("failed to get device version");
		err = clGetDeviceInfo(device_id_, CL_DEVICE_TYPE, sizeof(device_type), &device_type, NULL);
		if (err != CL_SUCCESS)
			crash("failed to get device type");
		err = clGetDeviceInfo(device_id_, CL_DEVICE_IMAGE_SUPPORT, sizeof(device_image_support), &device_image_support, NULL);
		if (err != CL_SUCCESS)
			crash("failed to get device image support");

		cout << "device name: " << device_name << endl;
		cout << "device version: " << device_version << endl;
		cout << "device type: " << ((device_type & CL_DEVICE_TYPE_GPU) ? "GPU" : "not GPU") << endl;
		cout << "device image support: " << (device_image_support ? "yes" : "no") << endl;
		cout << endl;

		context_ = clCreateContext(properties,
		                           1,
		                           &device_id_,
		                           0,
		                           0,
		                           &err);

#else

		cl_int error = 0;
		cl_platform_id platform;
		
		// Platform
		error = clGetPlatformIDs(1, &platform, NULL);
		if (error != CL_SUCCESS) {
		   cout << "Error getting platform id: " << error << endl;
		   exit(error);
		}
		// Device
		error = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, 1, &device_id_, NULL);
		if (err != CL_SUCCESS) {
		   cout << "Error getting device ids: " << error << endl;
		   exit(error);
		}
		// Context
		context_ = clCreateContext(0, 1, &device_id_, NULL, NULL, &error);
		if (error != CL_SUCCESS) {
		   cout << "Error creating context: " << error << endl;
		   exit(error);
		}

#endif

#endif

		
        cl_command_queue_properties queue_properties = 0;

		queue_properties |= CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE;

#ifdef PROFILING_ENABLED
		queue_properties |= CL_QUEUE_PROFILING_ENABLE;
#endif

        queue_ = clCreateCommandQueue(context_, device_id_, queue_properties, NULL);
        if (!queue_)
            crash("failed to create command queue");
    }
Ejemplo n.º 8
0
Archivo: ocl.cpp Proyecto: jcxz/DIP
bool selectGLDeviceAndPlatform(cl_device_id *device, cl_platform_id *platform)
{
  assert(device != nullptr);
  assert(platform != nullptr);

  /* first try to get the necessary extension */
  clGetGLContextInfoKHR_fn clGetGLContextInfoKHR = (clGetGLContextInfoKHR_fn) clGetExtensionFunctionAddress("clGetGLContextInfoKHR");
  if (clGetGLContextInfoKHR == nullptr)
  {
    ERRORM("clGetGLContextInfoKHR extension function not supproted.");
    return false;
  }

#if defined(FLUIDSIM_OS_MAC)
  cl_context_properties props[] = {
    CL_CONTEXT_PROPERTY_USE_CGL_SHAREGROUP_APPLE,
    (cl_context_properties) CGLGetShareGroup(CGLGetCurrentContext()),
    0
  };

  cl_int err = clGetGLContextInfoKHR(props,                                  // the OpenGL context
                                     CL_CURRENT_DEVICE_FOR_GL_CONTEXT_KHR,   // get the id of the device currently executing OpenGL
                                     sizeof(*device),
                                     device,
                                     nullptr);
  if (err != CL_SUCCESS)
  {
    ERRORM("Failed to retrieve the OpenCL id of the device executing OpenGL: " << errorToStr(err));
    return false;
  }

  /* get the platform associated with the device */
  err = clGetDeviceInfo(*device, CL_DEVICE_PLATFORM, sizeof(*platform), platform, nullptr);
  if (err != CL_SUCCESS)
  {
    ERRORM("Failed to retirieve platform id for the device executing OpenGL: " << errorToStr(err));
    return false;
  }
#else
  cl_context_properties props[] = {
#if defined(FLUIDSIM_OS_UNIX)
    CL_GL_CONTEXT_KHR, (cl_context_properties) glXGetCurrentContext(),
    CL_GLX_DISPLAY_KHR, (cl_context_properties) glXGetCurrentDisplay(),
    CL_CONTEXT_PLATFORM, (cl_context_properties) nullptr,
#elif defined(FLUIDSIM_OS_WIN)
    CL_GL_CONTEXT_KHR, (cl_context_properties) wglGetCurrentContext(),
    CL_WGL_HDC_KHR, (cl_context_properties) wglGetCurrentDC(),
    CL_CONTEXT_PLATFORM, (cl_context_properties) nullptr,
#else
# error "Unsupported OS platform"
#endif
    0
  };

  std::vector<boost::compute::platform> platform_list = boost::compute::system::platforms();

  for (const boost::compute::platform & p : platform_list)
  {
    WARNM("platform: " << p.name());
    props[5] = (cl_context_properties) p.id();
    cl_int err = clGetGLContextInfoKHR(props,                                 // the OpenGL context
                                       CL_CURRENT_DEVICE_FOR_GL_CONTEXT_KHR,  // get the id of the device currently executing OpenGL
                                       sizeof(*device),
                                       device,
                                       nullptr);
    if ((err == CL_SUCCESS) && (boost::compute::device(*device).type() == CL_DEVICE_TYPE_GPU))
    {
      *platform = (cl_platform_id) props[5];
      return true;
    }
    else
    {
      WARNM("clGetGLContextInfoKHR: " << errorToStr(err));
    }
  }
#endif

  return false;
}
Ejemplo n.º 9
0
/*!
    Creates an OpenCL context that is compatible with the current
    QGLContext and \a platform.  Returns false if there is no OpenGL
    context current or the OpenCL context could not be created for
    some reason.

    This function will first try to create a QCLDevice::GPU device,
    and will then fall back to QCLDevice::Default if a GPU is not found.

    If \a platform is null, then the first platform that has a GPU
    will be used.  If there is no GPU, then the first platform with a
    default device will be used.

    \sa supportsObjectSharing()
*/
bool QCLContextGL::create(const QCLPlatform &platform)
{
    Q_D(QCLContextGL);

    // Bail out if the context already exists.
    if (isCreated())
        return true;

    // Bail out if we don't have an OpenGL context.
    if (!QGLContext::currentContext()) {
        qWarning() << "QCLContextGL::create: needs a current GL context";
        setLastError(CL_INVALID_CONTEXT);
        return false;
    }

    // Find the first gpu device.
    QList<QCLDevice> devices;
    cl_device_type deviceType = CL_DEVICE_TYPE_GPU;
    devices = QCLDevice::devices(QCLDevice::GPU, platform);
    if (devices.isEmpty()) {
        // Find the first default device.
        devices = QCLDevice::devices(QCLDevice::Default, platform);
        deviceType = CL_DEVICE_TYPE_DEFAULT;
    }
    if (devices.isEmpty()) {
        qWarning() << "QCLContextGL::create: no gpu devices found";
        setLastError(CL_DEVICE_NOT_FOUND);
        return false;
    }
    QCLDevice gpu = devices[0];
    QVarLengthArray<cl_device_id> devs;
    foreach (QCLDevice dev, devices)
        devs.append(dev.deviceId());

    // Add the platform identifier to the properties.
    QVarLengthArray<cl_context_properties> properties;
    properties.append(CL_CONTEXT_PLATFORM);
    properties.append(cl_context_properties(gpu.platform().platformId()));

    bool hasSharing = false;
#ifndef QT_NO_CL_OPENGL
    // Determine what kind of OpenCL-OpenGL sharing we have and enable it.
#if defined(__APPLE__) || defined(__MACOSX)
    bool appleSharing = gpu.hasExtension("cl_apple_gl_sharing");
    if (appleSharing) {
        CGLContextObj cglContext = CGLGetCurrentContext();
        CGLShareGroupObj cglShareGroup = CGLGetShareGroup(cglContext);
        properties.append(CL_CGL_SHAREGROUP_KHR);
        properties.append(cl_context_properties(cglShareGroup));
        hasSharing = true;
    }
#else
    bool khrSharing = gpu.hasExtension("cl_khr_gl_sharing");
#if defined(QT_OPENGL_ES_2) || defined(QT_OPENGL_ES)
    if (khrSharing) {
        properties.append(CL_EGL_DISPLAY_KHR);
        properties.append(cl_context_properties(eglGetCurrentDisplay()));
#ifdef EGL_OPENGL_ES_API
        eglBindAPI(EGL_OPENGL_ES_API);
#endif
        properties.append(CL_GL_CONTEXT_KHR);
        properties.append(cl_context_properties(eglGetCurrentContext()));
        hasSharing = true;
    }
#elif defined(Q_WS_X11)
    if (khrSharing) {
        properties.append(CL_GLX_DISPLAY_KHR);
        properties.append(cl_context_properties(glXGetCurrentDisplay()));
        properties.append(CL_GL_CONTEXT_KHR);
        properties.append(cl_context_properties(glXGetCurrentContext()));
        hasSharing = true;
    }
#else
    // Needs to be ported to other platforms.
    if (khrSharing)
        qWarning() << "QCLContextGL::create: do not know how to enable sharing";
#endif
#endif
#endif // !QT_NO_CL_OPENGL
    properties.append(0);

#ifndef QT_NO_CL_OPENGL
    // Query the actual OpenCL devices we should use with the OpenGL context.
    typedef cl_int (*q_PFNCLGETGLCONTEXTINFOKHR)
        (const cl_context_properties *, cl_uint, size_t, void *, size_t *);
    q_PFNCLGETGLCONTEXTINFOKHR getGLContextInfo =
        (q_PFNCLGETGLCONTEXTINFOKHR)clGetExtensionFunctionAddress
            ("clGetGLContextInfoKHR");
    if (getGLContextInfo && hasSharing) {
        size_t size;
        cl_device_id currentDev;
        if(getGLContextInfo(properties.data(),
                            CL_DEVICES_FOR_GL_CONTEXT_KHR,
                            0, 0, &size) == CL_SUCCESS && size > 0) {
            QVarLengthArray<cl_device_id> buf(size / sizeof(cl_device_id));
            getGLContextInfo(properties.data(),
                             CL_DEVICES_FOR_GL_CONTEXT_KHR,
                             size, buf.data(), 0);
            devs = buf;
            gpu = QCLDevice(devs[0]);
        }
        if (getGLContextInfo(properties.data(),
                             CL_CURRENT_DEVICE_FOR_GL_CONTEXT_KHR,
                             sizeof(currentDev), &currentDev, 0)
                == CL_SUCCESS) {
            gpu = QCLDevice(currentDev);
        }
    }
#endif

    // Create the OpenCL context.
    cl_context id;
    cl_int error;
    id = clCreateContext
        (properties.data(), devs.size(), devs.data(),
         qt_clgl_context_notify, 0, &error);
    if (!id && hasSharing) {
        // Try again without the sharing parameters.
        properties.resize(2);
        properties.append(0);
        hasSharing = false;
        id = clCreateContext
            (properties.data(), devs.size(), devs.data(),
             qt_clgl_context_notify, 0, &error);
    }
    setLastError(error);
    if (id == 0) {
        qWarning() << "QCLContextGL::create:" << errorName(error);
        d->supportsSharing = false;
    } else {
        setContextId(id);
        clReleaseContext(id);   // setContextId() adds an extra reference.
        setDefaultDevice(gpu);
        d->supportsSharing = hasSharing;
    }
    return id != 0;
}
Ejemplo n.º 10
0
oclContext* oclContext::create(const char* iVendor, int iDeviceType)
{
    /*
    cl_int status = CL_SUCCESS;

    cl_device_type dType = CL_DEVICE_TYPE_GPU;

    cl_uint numPlatforms;
    cl_platform_id platform = NULL;
    status = clGetPlatformIDs(0, NULL, &numPlatforms);

    if (0 < numPlatforms) 
    {
        cl_platform_id* platforms = new cl_platform_id[numPlatforms];
        status = clGetPlatformIDs(numPlatforms, platforms, NULL);
        for (unsigned i = 0; i < numPlatforms; ++i) 
        {
            char pbuf[100];
            status = clGetPlatformInfo(platforms[i],
                                       CL_PLATFORM_VENDOR,
                                       sizeof(pbuf),
                                       pbuf,
                                       NULL);
            platform = platforms[i];
            if (!strcmp(pbuf, "Advanced Micro Devices, Inc.")) 
            {
                break;
            }
        }
        delete[] platforms;
    }

#ifdef _WIN32
    HGLRC glCtx = wglGetCurrentContext();
#else //!_WIN32
    GLXContext glCtx = glXGetCurrentContext();
#endif //!_WIN32
    
    cl_context_properties cpsGL[] = { CL_CONTEXT_PLATFORM, (cl_context_properties)platform,
#ifdef _WIN32
                                      CL_WGL_HDC_KHR, (intptr_t) wglGetCurrentDC(),
#else //!_WIN32
                                      CL_GLX_DISPLAY_KHR, (intptr_t) glXGetCurrentDisplay(),
#endif //!_WIN32
                                      CL_GL_CONTEXT_KHR, (intptr_t) glCtx, 0};


    cl_context context = clCreateContextFromType(
        cpsGL,
        dType,
        NULL,
        NULL,
        &status);
    return  new oclContext(context, "Advanced Micro Devices, Inc.");
*/

    
    cl_uint lPlatformCount = 0;
    sStatusCL = clGetPlatformIDs(0, NULL, &lPlatformCount);
    oclSuccess("clGetPlatformIDs");

    clGetGLContextInfoKHR_fn _clGetGLContextInfoKHR = (clGetGLContextInfoKHR_fn)clGetExtensionFunctionAddress("clGetGLContextInfoKHR");

    cl_platform_id lPlatform[100];
    sStatusCL = clGetPlatformIDs(lPlatformCount, lPlatform, NULL);
    oclSuccess("clGetPlatformIDs");

    char lBuffer[200];
    for (cl_uint i=0; i < lPlatformCount; i++) 
    {
        sStatusCL = clGetPlatformInfo(lPlatform[i],
                                       CL_PLATFORM_VENDOR,
                                       sizeof(lBuffer),
                                       lBuffer,
                                       NULL);
        oclSuccess("clGetPlatformInfo");

        cl_context_properties GL_PROPS[] = 
        {
#ifdef WIN32
            CL_GL_CONTEXT_KHR, (cl_context_properties)wglGetCurrentContext(),
            CL_WGL_HDC_KHR, (cl_context_properties)wglGetCurrentDC(),
#else
            CL_GL_CONTEXT_KHR, (cl_context_properties)glXGetCurrentContext(),
            CL_GLX_DISPLAY_KHR, (intptr_t) glXGetCurrentDisplay(),
#endif
            CL_CONTEXT_PLATFORM, (cl_context_properties)lPlatform[i], 
            0
        };

        cl_context_properties CL_PROPS[] = 
        {
            CL_CONTEXT_PLATFORM, (cl_context_properties)lPlatform[i], 
            0
        };

        if (!strncmp(lBuffer, iVendor, 5))  // compare only first 5 letters -- Intel starts with "Intel" and "Intel(R)"
        {
            switch (iDeviceType)
            {
            case CL_DEVICE_TYPE_GPU:
                // gpu context
                cl_device_id lDevices[100];
                cl_uint lDeviceCount;
                sStatusCL = clGetDeviceIDs(lPlatform[i], CL_DEVICE_TYPE_GPU, 100, lDevices, &lDeviceCount);
                if (!oclSuccess("clGetDeviceIDs"))
                {
                    continue;
                }

                if (lDeviceCount)
                {
                    size_t lDeviceGLCount = 0;
                    cl_device_id lDeviceGL; 
                    sStatusCL = _clGetGLContextInfoKHR(GL_PROPS, CL_CURRENT_DEVICE_FOR_GL_CONTEXT_KHR, sizeof(cl_device_id), &lDeviceGL, &lDeviceGLCount);
                    if (!oclSuccess("clGetDeviceIDs"))
                    {
                        //continue; AMD drivers produce an error here if not OpenGL context is present
                    }

                    if (lDeviceGLCount)
                    {
                        // gpu context with sharing enabled
                        cl_context lContextCL = clCreateContext(GL_PROPS, lDeviceCount, lDevices, NULL, NULL, &sStatusCL);
                        if (!oclSuccess("clCreateContext"))
                        {
                            continue;
                        }
                        return new oclContext(lContextCL, lPlatform[i], lBuffer);
                    }
                    else
                    {
                        // gpu context without sharing
                        cl_context lContextCL = clCreateContext(CL_PROPS, lDeviceCount, lDevices, NULL, NULL, &sStatusCL);
                        if (!oclSuccess("clCreateContext"))
                        {
                            continue;
                        }
                        return new oclContext(lContextCL, lPlatform[i], lBuffer);
                    }
                }
                break;

            case CL_DEVICE_TYPE_CPU:
                // cpu context
                cl_context lContextCL = clCreateContextFromType(CL_PROPS, CL_DEVICE_TYPE_CPU, NULL, NULL, &sStatusCL);
                if (!oclSuccess("clCreateContextFromType"))
                {
                    continue;
                }
                return new oclContext(lContextCL, lPlatform[i], lBuffer);
                break;
            }
        }
    }
    
    return 0;
}