1

I can write data to cl::BufferGL variables (on GPU) and use it to draw, but I got wrong values when reading back to host. I have ran a couple examples from AMD SDK, including GL_CL interop, and is all ok. I can read and write cl::Buffer, i.e., copy from host to device and copy from device to host.

The source code is hosted at link.

To create the cl::Buffer I do steps below (in order, and after get a cl_gl context):

  1. glGenBuffer
  2. glBindBuffer
  3. glBufferData
  4. clCreateFromGLBuffer
  5. glBindBuffer (bind to 0, or unbind)

The program is written using:

  • C++
  • Ubuntu 12.06 64 bits, kernel 3.2.0-29-generic
  • OpenCL 1.2
  • Driver 1124.2 (Binary driver from AMD, Catalyst 13.4)
  • AMD APPSDK v2.8
  • OpenGL Version: 4.2.12217
  • Glew
  • Freeglut

EDIT: I've created a code block to reproduce the error

// Terminates OpenCL commands
cl_int status = 0;
status =  clFinish( (cq)() );
if ( status != CL_SUCCESS ) { printf("File: %s:%d\n", __FILE__, __LINE__);  }

// Create Vertex buffer object
GLint  number = 5;
GLuint buffer;
CHk_ERR( glGenBuffers( 1, &buffer ));
CHk_ERR( glBindBuffer( GL_ARRAY_BUFFER, buffer ) );

// initialize buffer object
GLsizeiptr     size = static_cast<GLsizeiptr>(sizeof(number));
const GLvoid * data = static_cast<const GLvoid *>(&number);
CHk_ERR( glBufferData( GL_ARRAY_BUFFER, size, data, GL_DYNAMIC_DRAW) );
CHk_ERR( glBindBuffer(GL_ARRAY_BUFFER, 0) );
glFinish();

if ( glGetError() != GL_NO_ERROR ) { printf("File: %s:%d\n", __FILE__, __LINE__);  }

// create OpenCL buffer from GL VBO
cl_mem mem = clCreateFromGLBuffer( (context)(), CL_MEM_READ_WRITE, buffer, &status );
if ( status != CL_SUCCESS ) { printf("File: %s:%d\n", __FILE__, __LINE__);  }

// Acquire OpenCL memory objects that have been created from OpenGL objects
if ( clEnqueueAcquireGLObjects( (cq)(), 1, &(mem), 0, NULL, NULL ) != CL_SUCCESS ){
    std::cout << "ERROR!";
}

// Read buffer
GLint value;
cq.enqueueReadBuffer( mem, CL_TRUE, 0, size, &value );
std::cout << "Value: " << value << std::endl;

Any help is appreciated!!

4

0 回答 0