Jump to content
  • Advertisement
Alex Garcin

OpenCL retrieve opencl platform that match the opengl one

Recommended Posts

Hello !

I have two gpu on my computer, one from my cpu and another one from my graphic card.

I am trying to use the opengl/opencl interop capabilities. But I am stuck at the creation of the opencl context, I don't know how to identify which platform / device of the two one is used by opengl.

In the above code, which fonction should I use in the test "DEVICE MATCHING OPENGL ONE" to test if the device is the one used by OpenGL, or what should I do to test if the platform_id is the good one ? 

sf::ContextSettings settings;
settings.depthBits = 24;
settings.stencilBits = 8;
settings.antialiasingLevel = 2;
sf::Window window(sf::VideoMode(2048, 1024), "GAME",
        sf::Style::Fullscreen, settings);

glewInit();

cl_platform_id platform_ids[16] = { NULL };
cl_device_id device_id = NULL;
cl_uint ret_num_devices;
cl_uint ret_num_platforms;
    
cl_platform_id platform_id = 0;
cl_int ret = clGetPlatformIDs(_countof(platform_ids), platform_ids,
            &ret_num_platforms);

size_t n = 0;

cl_context_properties props[] = { CL_GL_CONTEXT_KHR,
    (cl_context_properties) wglGetCurrentContext(), CL_WGL_HDC_KHR,
    (cl_context_properties) wglGetCurrentDC(), CL_CONTEXT_PLATFORM,
    (cl_context_properties) platform_id, 0 };

for (size_t i = 0; i < ret_num_platforms; ++i) {
    platform_id = platform_ids[i];
    cl_device_id curDevices_id[16];

        ret = clGetDeviceIDs(platform_id, CL_DEVICE_TYPE_GPU,
                _countof(curDevices_id), curDevices_id, &ret_num_devices);

    for (cl_uint nDevices = 0; nDevices < ret_num_devices; ++nDevices) {
        cl_device_id curDevice_id = curDevices_id[nDevices];

        clGetGLContextInfoKHR_fn clGetGLContextInfo =
                reinterpret_cast<clGetGLContextInfoKHR_fn> 
                   (clGetExtensionFunctionAddressForPlatform(
                            platform_id, "clGetGLContextInfoKHR"));

        if (clGetGLContextInfo) {
            cl_device_id clGLDevice = 0;
            props[5] =
                    reinterpret_cast<cl_context_properties>(platform_id);
            clGetGLContextInfo(props,
                    CL_CURRENT_DEVICE_FOR_GL_CONTEXT_KHR,
                    sizeof(clGLDevice), &clGLDevice, &n);

            if (DEVICE MATCHING OPENGL ONE) {
                device_id = clGLDevice;
            }
        }
    }
    if (device_id) {
        break;
    }
}

cl_context context = clCreateContext(props, 1, &device_id, NULL, NULL,
        &ret);

Thanks for your future help!

Share this post


Link to post
Share on other sites
Advertisement

Hi, @Alex Garcin

I have already answered you:

if (clGLDevice == curDevice_id) {
    device_id = clGLDevice;
    break;
}

does't it code work ?

Quote

or what should I do to test if the platform_id is the good one ?

You should choice the platform that supports GPU and can interop with OpenGL.

Share this post


Link to post
Share on other sites
clGLDevice == curDevice_id

This assertion is true for both the two platform. 

Each of the platform support GPU so how to get the one that is used by opengl ?

Share this post


Link to post
Share on other sites

Hi @Alex Garcin,

14 hours ago, Alex Garcin said:

clGLDevice == curDevice_id

This assertion is true for both the two platform. 

Each of the platform support GPU so how to get the one that is used by opengl ?

How many OpenGL Contexts do you have ? I don't know how to create OpenGL context on GPU that is not connected to Monitor. How do you have more than one context on you system with single monitor ?

I have already answered, did you try it ? 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!