Ok, so let’s go for some code :
My initialisation first (in case of…)
// Get the number of plateforms
cl_uint numPlatforms;
errcode = clGetPlatformIDs(0, NULL, &numPlatforms);
clErrorCodes(errcode);
// The code will run on the same machine that does the compilation, I'm sure there will always be a single GPU
// Get plateforms info
platforms = new cl_platform_id[numPlatforms];
errcode = clGetPlatformIDs(numPlatforms, platforms, NULL);
clErrorCodes(errcode);
if( NULL == (*platforms))
{
std::cout << "Failure!" << std::endl;
}
// Create a context to run OpenCL on our CUDA-enabled NVIDIA GPU
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)(*platforms),
0
};
theContext = clCreateContextFromType(props,targetDevice,NULL, NULL, &errcode);
clErrorCodes(errcode);
// Get the list of GPU devices associated with this context
size_t ParmDataBytes;
clGetContextInfo(theContext, CL_CONTEXT_DEVICES, 0, NULL, &ParmDataBytes);
theDevice = new cl_device_id[ParmDataBytes];
clGetContextInfo(theContext, CL_CONTEXT_DEVICES, ParmDataBytes, theDevice, NULL);
// Create a command-queue on the first GPU device
theCmdQueue = clCreateCommandQueue(theContext, theDevice[0], 0, &errcode);
clErrorCodes(errcode);
Then the code I’m using to read a valid OpenGL texture :
// prepare it first : (where glObject->getID() return the correct GLuint ID)
clCreateFromGLTexture2D(HdlOCL::context(), CL_MEM_READ_WRITE, GL_TEXTURE_2D, 0, glObject->getID(), HdlOCL::errorFB());
// returns CL_SUCCESS
//grab the texture
glFlush();
HdlOCL::errorFB( clEnqueueAcquireGLObjects( HdlOCL::queue(), 1, &buffer, 0, 0, 0) );
// returns CL_SUCCESS
//Try to read it or write it
HdlOCL::errorFB( clEnqueueReadBuffer( HdlOCL::queue(), buffer, CL_FALSE, 0, 1920*1080*3, ptr, 0, NULL, NULL) );
//or HdlOCL::errorFB(clEnqueueWriteBuffer( HdlOCL::queue(), buffer, CL_FALSE, 0, 1920*1080*3, ptr, 0, NULL, NULL) );
/* or
size_t origin[] = {0, 0, 0};
size_t region[] = {1920, 1080, 1};
HdlOCL::errorFB( clEnqueueReadImage(HdlOCL::queue(), buffer, CL_TRUE, origin, region, 0, 0, ptr, 0, 0, 0) );
*/
/* or
size_t origin[] = {0, 0, 0};
size_t region[] = {1920, 1080, 1};
HdlOCL::errorFB( clEnqueueWriteImage(HdlOCL::queue(), buffer, CL_TRUE, origin, region, 0, 0, ptr, 0, 0, 0) );
*/
// each returns CL_INVALID_VALUE
// Release it
HdlOCL::errorFB( clEnqueueReleaseGLObjects( HdlOCL::queue(), 1, &buffer, 0, 0, 0) );
clFinish(HdlOCL::queue());
//returns CL_SUCCESS
I also verified the information with the CL queries : clGetGLTextureInfo, clGetGLObjectInfo and I get the right values…
Thanks for the help!