4 Replies Latest reply on Jun 7, 2010 10:33 AM by Pillard

    Lag when reading CL buffers from GL using clCreateFromGLBuffer ?

      I'm trying to use OpenCL along with openGL. I get a huge lag (almost 1sec) every 3-4 seconds. What am I doing wrong ???

      Hello all !

      I'm trying to get openCL to read buffers outputted from openGL (the Z buffer actually). It works pretty well, except that I get a huge slow down of approx. 1 sec long every 3-4 seconds....

      Here is the code I use to retrieve the GL Z buffer, does someone see something very, very wrong in the code below ?? (this code gets executed at every frame). It doesn't do anything, as I removed everything I could in order to isolate the instruction creating the lag (apparently it is caused by "clCreateFromGLBuffer"). Note: the GL buffers are created earlier during init().

      EDIT: nop, I was wrong: clCreateFromGLBuffer works fine, the lag comes from clEnqueueAcquireGLObjects()... maybe a synchronisation issue ?

      glBindBuffer(GL_PIXEL_PACK_BUFFER, GLBuffers[0]); unsigned int size = resoX*resoY*2*sizeof(cl_float)*maxLocComplexity*2; if (!initDone) depths = (cl_float*)malloc(size); memset(depths, 0, size); glBufferData(GL_PIXEL_PACK_BUFFER, size, (GLvoid*)depths, GL_STATIC_READ); glReadPixels(0, 0, resoX*2*maxLocComplexity, resoY*2, GL_DEPTH_COMPONENT, GL_FLOAT, 0); depthBuf = clCreateFromGLBuffer(context, CL_MEM_READ_ONLY, GLBuffers[0], &status); if(!sampleCommon->checkVal( status,CL_SUCCESS, "createFromGL failed. (GLBuffers[0])")) return SDK_FAILURE; status = clEnqueueAcquireGLObjects(commandQueue, 1, &depthBuf, 0, 0, NULL); status = clEnqueueReleaseGLObjects(commandQueue, 1, &depthBuf, 0, 0, 0); glBindBuffer(GL_PIXEL_PACK_BUFFER, 0); clReleaseMemObject(depthBuf); return 0;

        • Lag when reading CL buffers from GL using clCreateFromGLBuffer ?

          1. why do you load zero block of memory into GLbuffer if you fill it with other data anyway. just call glBufferData() with NULL pointer and it allocate empty buffer with desired size.

          2. creating cl_mem object every frame is IMHO bad idea. move it into init() or something.

            • Lag when reading CL buffers from GL using clCreateFromGLBuffer ?

              Hi Nou, and thanks for your reply !

              1. well, I guess because I don't fully understand the memory handling between CPU and GPU yet (I'm pretty new to openCL and fancy openGL ops)...


              2. yep, this was the issue: I changed the code so that clCreateFromGLBuffer  gets called only once during init, and my lag is gone ! Thanks a lot !

              Btw, what is the difference between clCreateFromGLBuffer and and clEnqueueAcquireGLObjects? I thought that clEnqueueAcquireGLObjects would actually retrieve the memory contained in the GL buffer of interest, but now I ain't so sure any longer...

              Thanks again !

                • Lag when reading CL buffers from GL using clCreateFromGLBuffer ?

                  clEnqueueAcquireGLObjects / clEnqueueReleaseGLObjects has nothing to do with allocating or releasing a buffer, its like getting a lock on that object so that it can be used in the compute mode.

                  When you are done with CL calculations on buffer, you should call clEnqueueReleaseGLObjects so that it can be used in GL mode.

                    • Lag when reading CL buffers from GL using clCreateFromGLBuffer ?

                      Alright, I see... This makes sense actually Thank you N0thing !

                      However, when I call clCreateFromGLBuffer only at startup, my buffer gets updated only once.... (i.e. it retrieves content of only the first frame, afterward nothing... at all... unless I call clCreateFromGLBuffer again)....

                      Any idea about this ?

                      EDIT: and if I call clCreateFromGLBuffer at every frame, the data always corresponds to the first frame... I guess I'm missing something else somewhere....

                      EDIT2: well, looks  like I should call both glBufferData AND clCreateFromGLBuffer only once at startup. This gives me appropriate data. sorry for the trouble guys and thanks again for the help