cancel
Showing results for 
Search instead for 
Did you mean: 

Archives Discussions

nalkar
Adept I

OpenCL / OpenGL context sharing breaks the OpenGL context

I was trying to modify some successfully used OpenGL/OpenCL sharing from a few months ago and was supervised when the output window from the application was all white. In trying to determine what was going on i created a trivial OpenGL Application that simply displaces a single texture mapped quad in a window.  The texture is initialized in software to all red. This produced the expected results, but when i added OpenCL initialization, i once again got an all white window.

I started commenting out section by section the parts of the OpenCL init. This revealed that, as soon as the shared context creation was removed, the texture was once again displayed. I tried creating a non shared context and once again texture instead of white window.

At this point upgraded my APP SDK, my drivers where already up to date. But after doing so, the problem still remains. Does anyone have any idea what is going on and how to fix this?  Relevant code below. It is worth noting that i don't actually have to do anything with OpenCL for this to happen, beyond making a shared context. Also the call to get the shared texture returns success although it clearly doesn't work. All of this worked about 3 months ago. Any help would be appreciated.

Donald J.

MIsc Info

Card     HD6950

APP SDK v2.7

Cat v 12.4     (Tested Version From SDK)

OpenGL Version    6.14.10.11631

Windows 7

// glut display function

void display()

{

    static bool first = true;

    if ( first )

    {

        make_rgba_32_texture(data.buffer_t,1024,1024,red);

        load_texture(data.buffer_t);

        init_cl();

        first = false;

    }   

    glMatrixMode(GL_PROJECTION);

    glLoadIdentity();

    gluOrtho2D(-1.0,1.0,-1.0,1.0);

    glMatrixMode(GL_MODELVIEW);

    glLoadIdentity();

    glTranslated(0.0,0.0,-0.5);

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glFinish();

    glEnable(GL_TEXTURE_2D);

    glBindTexture( GL_TEXTURE_2D, data.buffer_t.gl_id );

    glBegin(GL_QUADS);

        glTexCoord2f(0.0,1.0);

        glVertex3f(-1.0,-1.0,0);

        glTexCoord2f(1.0,1.0);

        glVertex3f(1.0,-1.0,0);

        glTexCoord2f(1.0,0.0);

        glVertex3f(1.0,1.0,0);

        glTexCoord2f(0.0,0.0);

        glVertex3f(-1.0,1.0,0);

    glEnd();

    glutSwapBuffers();

}

// Open CL init

void init_cl()

{

    // Get platform and device information

    cl_platform_id platform_id = NULL;

    cl_uint ret_num_devices;

    cl_uint ret_num_platforms;

    // get avaliable platforms

    cldata.ret = clGetPlatformIDs(1, &platform_id, &ret_num_platforms);

    if ( cldata.ret != CL_SUCCESS )

    {

        fprintf(stderr,"Could not find OpenCL Platforms\n");

        exit(1);

    }

    else

    {

        fprintf(stderr,"Found OpenCL Platforms\n");

    }

    // get avaliable devices

    cldata.ret = clGetDeviceIDs( platform_id, CL_DEVICE_TYPE_GPU, 1,

            &cldata.device, &ret_num_devices);

    if ( cldata.ret != CL_SUCCESS )

    {

        fprintf(stderr,"Could not find GPU device \n");

        exit(1);

    }

    else

    {

        fprintf(stderr,"Found GPU Device\n");

    }

    size_t extension_size;

    cldata.ret = clGetDeviceInfo(cldata.device,CL_DEVICE_EXTENSIONS,0,NULL,&extension_size);

    if ( cldata.ret != CL_SUCCESS )

    {

        fprintf(stderr,"Could not get extension info for GPU\n");

        exit(1);

    }

    // see if we have the sharing extension

    char* extensions = new char[extension_size];

    cldata.ret = clGetDeviceInfo(cldata.device,CL_DEVICE_EXTENSIONS,extension_size,extensions,&extension_size);

    std::string extensionList(extensions);

    delete extensions;

    extensions = NULL;

    size_t pos1 = 0;

    size_t pos2 = extensionList.find(' ',pos1);

    // search for sharing extension

    while ( pos1 != extensionList.npos )

    {

        std::string s = extensionList.substr(pos1,pos2-pos1);

        if ( s == "cl_khr_gl_sharing" )

        {

            cldata.cl_sharing = true;

            break;

        }

        pos1 = pos2 + 1;

        pos2 = extensionList.find(' ',pos1);

    }

    if ( cldata.cl_sharing == true )

    {

        // this is platform dependant

        #ifdef WIN32

        cl_context_properties props[] =

        {

            CL_GL_CONTEXT_KHR, reinterpret_cast<cl_context_properties>( wglGetCurrentContext() ),

            CL_WGL_HDC_KHR, reinterpret_cast<cl_context_properties>( wglGetCurrentDC() ),

            CL_CONTEXT_PLATFORM, (cl_context_properties) platform_id,

            0

        };

        //cldata.context = clCreateContext( NULL, 1, &cldata.device, NULL, NULL, &cldata.ret);  // testing to see if none shared context broke gl but it doesnt

        cldata.context = clCreateContext( props, 1, &cldata.device, NULL, NULL, &cldata.ret);   

        #elif __APPLE__

        cl_context_properties props[] =

        {

            CL_CONTEXT_PROPERTY_USE_CGL_SHAREGROUP, reinterpret_cast<cl_context_properties>( kCGLShareGroup() ),

            0

        };

        #elif LINUX

        cl_context_properties props[] =

        {

            CL_GL_CONTEXT_KHR, reinterpret_cast<cl_context_properties>(glxGetCurrentContext() ),

            CL_GLX_DISPLAY_KHR, reinterpret_cast<cl_context_properties>( glxGetCurrentDisplay() ),

            0

        };

        #endif

    }

    else

    {

        // context without  sharing

        // Create an OpenCL context

        cldata.context = clCreateContext( NULL, 1, &cldata.device, NULL, NULL, &cldata.ret);

    }

    if ( cldata.ret != CL_SUCCESS || cldata.context == NULL )

    {

        fprintf(stderr,"Could not create a context\n");

        exit(1);

    }

    else

    {

        fprintf(stderr,"Created context\n");

    }

     // NOTHING PAST HERE MATTERS I JUST UNCOMMENTED THIS SECTION TO TRY THE clFinish

    // Create a command queue

    cldata.queue = clCreateCommandQueue(cldata.context, cldata.device, 0, &cldata.ret);

    if ( cldata.ret != CL_SUCCESS || cldata.queue == NULL )

    {

        fprintf(stderr,"Could not create a command queue\n");

        exit(1);

    }

    else

    {

        fprintf(stderr,"Created Command Queue\n");

    }

   // tried a finish just in case it would help but doesnt

    // clFinish(cldata.queue);

    /*

    // create buffers to hold the mask and data

    cl_load_program_and_kernels("mhm.cl");

    cl_uint num;

    cldata.ret = clGetSupportedImageFormats(cldata.context,CL_MEM_READ_WRITE,CL_MEM_OBJECT_IMAGE2D,0,NULL,&num);

    cl_image_format* fmt = new cl_image_format[num];

    cldata.ret = clGetSupportedImageFormats(cldata.context,CL_MEM_READ_WRITE,CL_MEM_OBJECT_IMAGE2D,num,fmt,&num);

    bool f1 = false;

    bool f2 = false;

    for( size_t i = 0; i < num; ++i )

    {

        if( fmt.image_channel_data_type == CL_FLOAT )

        {

            if ( fmt.image_channel_order == CL_A )

            {

                printf("float alpha \n");

            }

            else if ( fmt.image_channel_order == CL_R )

            {

                printf("float red \n");

            }

            else if ( fmt.image_channel_order == CL_INTENSITY )

            {

                printf("float intensity\n");

            }

            else if ( fmt.image_channel_order == CL_LUMINANCE )

            {

                printf("float luminance\n");

                f1 = true;

            }

        }

        else if( fmt.image_channel_data_type == CL_UNORM_INT8 )

        {

            if ( fmt.image_channel_order == CL_A )

            {

                printf("unorm int8 alpha \n");

            }

            else if ( fmt.image_channel_order == CL_R )

            {

                printf("unorm int8 red \n");

            }

            else if ( fmt.image_channel_order == CL_INTENSITY )

            {

                printf("unorm int8 intensity\n");

            }

            else if ( fmt.image_channel_order == CL_LUMINANCE )

            {

                printf("unorm int8 luminance\n");

                f2 = true;

            }

        }

    }

    delete[] fmt;

    if ( f1 && f2)

    {

        printf("found required image formats\n");

    }

    else

    {

        printf("missing required image formats\n");

        exit(1);

    }

    // load the data bufers

    cldata.data_b[0] = normalize_and_load_to_texture(data.data_t[0]);

    cldata.data_b[1] = normalize_and_load_to_texture(data.data_t[1]);

    cldata.data_b[2] = normalize_and_load_to_texture(data.data_t[2]);

    cldata.data_b[3] = normalize_and_load_to_texture(data.data_t[3]);

    cldata.data_b[4] = normalize_and_load_to_texture(data.data_t[4]);

    cldata.data_b[5] = normalize_and_load_to_texture(data.data_t[5]);

    // get open gl texture as image

    cldata.image = clCreateFromGLTexture2D(cldata.context, CL_MEM_READ_WRITE, GL_TEXTURE_2D, 0, data.buffer_t.gl_id, &cldata.ret);

    if ( cldata.ret != CL_SUCCESS )

    {

        fprintf( stderr, "could not refernce shared texture\n");

        exit(1);

    }

    */

}

0 Likes
1 Solution
nalkar
Adept I

complete uninstall of all AMDdrivers, followed by DriverSweeper, reinstall of drivers, uninstall of driver (CCC) wouldn't load, another reinstall of drivers, and finally reinstalling the APP SDK appears to fix this issue. This was using 12.8 drivers and 2.7 APP SDK. I can only guess that some part of the system had gotten corrupted.

View solution in original post

0 Likes
10 Replies
binying
Challenger

Can you run the openGL sample in the SDK?

0 Likes

The samples that use just OpenCL work. Those that use OpenGL / OpenCL Sharing display a black screen. Those that use OpenCL with directx work.

0 Likes

Those that use OpenGL / OpenCL Sharing display a black screen. --So you may need to fix this first...

0 Likes

That is what i am trying to fix. The samples are showing the same error as my code. Creating the shared context kills the opengl context.

0 Likes
peakitde
Journeyman III

Nice Thanx to share this.

0 Likes

Some files (head files?)  need to be downloaded in order to run openGL code correctly, which are not there by default after installing the SDK.

0 Likes

What header files? So far as i can tell OpenGL works correctly until i try to making a shared context.

0 Likes

Does your device support cl_khr_gl_sharing? Have you enabled this extension?

0 Likes

Yes it supports the extension. In addition the init code test for the presence of that extension before trying to make a shared context. If the extension string is not found the code would exit. As stated before this code used to work fine, now both it and the SDK sample on sharing a gl context fail in the same fashion.

0 Likes
nalkar
Adept I

complete uninstall of all AMDdrivers, followed by DriverSweeper, reinstall of drivers, uninstall of driver (CCC) wouldn't load, another reinstall of drivers, and finally reinstalling the APP SDK appears to fix this issue. This was using 12.8 drivers and 2.7 APP SDK. I can only guess that some part of the system had gotten corrupted.

0 Likes