AnsweredAssumed Answered

OpenCL / OpenGL context sharing breaks the OpenGL context

Question asked by nalkar on Sep 21, 2012
Latest reply on Oct 9, 2012 by nalkar

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[i].image_channel_data_type == CL_FLOAT )

        {

            if ( fmt[i].image_channel_order == CL_A )

            {

                printf("float alpha \n");

            }

            else if ( fmt[i].image_channel_order == CL_R )

            {

                printf("float red \n");

            }

            else if ( fmt[i].image_channel_order == CL_INTENSITY )

            {

                printf("float intensity\n");

            }

            else if ( fmt[i].image_channel_order == CL_LUMINANCE )

            {

                printf("float luminance\n");

                f1 = true;

            }

 

        }

        else if( fmt[i].image_channel_data_type == CL_UNORM_INT8 )

        {

            if ( fmt[i].image_channel_order == CL_A )

            {

                printf("unorm int8 alpha \n");

            }

            else if ( fmt[i].image_channel_order == CL_R )

            {

                printf("unorm int8 red \n");

            }

            else if ( fmt[i].image_channel_order == CL_INTENSITY )

            {

                printf("unorm int8 intensity\n");

            }

            else if ( fmt[i].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);

    }

    */

}

Outcomes