troubles with CL/GL texture interoperability clCreateFromGLTextureXD fail but clCreateFromGLBuffer w

Hi

I am facing a weird problem:

i can correctly share buffer objects between CL and GL, but if i try the same with texture2D or 3D, i keep getting error -60 (CL_INVALID_GL_OBJECT)

This on W764, latest drivers (260, CL 1.0, it happened also with previous drivers 259), gtx280

so my code is the following:

void createCLBuffer() {

	if (!m_vecDataCL) {

		m_clOrigin[0] = m_clOrigin[1] =m_clOrigin[2] =0;

		m_clRegion[0] = m_uWidth; m_clRegion[1] = m_uHeight; m_clRegion[2] = m_uDepth;

		

		switch(m_GLStorageMode) {

		case GL_STORAGE_BUFFER:

			createGLBufferStorage();

			m_vecDataCL = clCreateFromGLBuffer(*m_clContext, CL_MEM_READ_WRITE, m_glItem, &clErr);

			m_oclContext->checkforCLerrors(clErr,"BufferCL",__LINE__);

			break;

		case GL_STORAGE_TEX3D:		

				createGLTexture3DStorage();

				//m_vecDataCL = clCreateFromGLBuffer(*m_clContext, CL_MEM_READ_WRITE, m_glItem, &clErr);

				glBindTexture(GL_TEXTURE_3D,m_glItem); m_oclContext->checkforGLerrors("BufferCL()",__LINE__);

				m_vecDataCL = clCreateFromGLTexture3D (*m_clContext, CL_MEM_READ_ONLY, GL_TEXTURE_3D, 0, m_glItem, &clErr);

				m_oclContext->checkforCLerrors(clErr,"BufferCL",__LINE__);

				glBindTexture(GL_TEXTURE_3D,0); m_oclContext->checkforGLerrors("BufferCL()",__LINE__);

				GLenum glTarget;

				GLint glLevel;

				clErr = clGetGLTextureInfo ( m_vecDataCL,CL_GL_TEXTURE_TARGET, sizeof(GLenum), &glTarget, NULL); m_oclContext->checkforCLerrors(clErr,"BufferCL",__LINE__);

				clErr = clGetGLTextureInfo ( m_vecDataCL,CL_GL_MIPMAP_LEVEL, sizeof(GLint), &glLevel, NULL); m_oclContext->checkforCLerrors(clErr,"BufferCL",__LINE__);

				break;	

		case GL_STORAGE_TEX2D:		

				createGLTexture2DStorage();

				//m_vecDataCL = clCreateFromGLBuffer(*m_clContext, CL_MEM_READ_WRITE, m_glItem, &clErr);

				m_vecDataCL = clCreateFromGLTexture2D (*m_clContext, CL_MEM_READ_WRITE, GL_TEXTURE_2D, 0, m_glItem, &clErr);

				m_oclContext->checkforCLerrors(clErr,"BufferCL",__LINE__);

				break;	

		}

		clEnqueueAcquireGLObjects(*m_clCommandQueue, 1, &m_vecDataCL, 0,0,0);

	}

};

void createGLBufferStorage() {

	glGenBuffers(1, &m_glItem);

	glBindBuffer(GL_TEXTURE_BUFFER, m_glItem);

	glBufferData(GL_TEXTURE_BUFFER, sizeof(Type) * m_uWidth*m_uHeight*m_uDepth*m_uTimesteps*m_uChannels, 0,

		GL_DYNAMIC_COPY);

	m_oclContext->checkforGLerrors("BufferCL()",__LINE__);

	glBindBuffer(GL_TEXTURE_BUFFER, 0);

}

void createGLTexture2DStorage() {

	glGenTextures(1, &m_glItem);

	glBindTexture(GL_TEXTURE_2D,m_glItem); m_oclContext->checkforGLerrors("BufferCL()",__LINE__);

	glPixelStorei(GL_UNPACK_ALIGNMENT,1); m_oclContext->checkforGLerrors("BufferCL()",__LINE__);

	glTexImage2D(GL_TEXTURE_2D, 

		0,

		GL_ALPHA,

		m_uWidth,

		m_uHeight,

		0,

		GL_ALPHA,

		GL_UNSIGNED_BYTE,

		NULL

		); m_oclContext->checkforGLerrors("BufferCL()",__LINE__);

	glBindTexture(GL_TEXTURE_2D,0); m_oclContext->checkforGLerrors("BufferCL()",__LINE__);

}

void createGLTexture3DStorage(){

	glGenTextures(1, &m_glItem);

	glBindTexture(GL_TEXTURE_3D,m_glItem); m_oclContext->checkforGLerrors("BufferCL()",__LINE__);

	glPixelStorei(GL_UNPACK_ALIGNMENT,1); m_oclContext->checkforGLerrors("BufferCL()",__LINE__);

	glTexImage3D(GL_TEXTURE_3D, 

					0,

					GL_ALPHA,

					m_uWidth,

					m_uHeight,

					m_uDepth,

					0,

					GL_ALPHA,

					GL_UNSIGNED_BYTE,

					NULL

	); m_oclContext->checkforGLerrors("BufferCL()",__LINE__);

	glBindTexture(GL_TEXTURE_3D,0); m_oclContext->checkforGLerrors("BufferCL()",__LINE__);

}

finally here is how i initialize the shared context:

		void init()

		{

			//Get an OpenCL platform

			printf("clGetPlatformID...\n");  ciErr1 = clGetPlatformIDs(1, &clPlatform, NULL); if (ciErr1 != CL_SUCCESS) printf("Error in clGetPlatformID, Line %u in file %s !!!\n\n", __LINE__, __FILE__);

			//Get the devices

			printf("clGetDeviceIDs...\n"); ciErr1 = clGetDeviceIDs(clPlatform, CL_DEVICE_TYPE_GPU, 1, &m_clDeviceReal, NULL); if (ciErr1 != CL_SUCCESS) printf("Error in clGetDeviceIDs, Line %u in file %s !!!\n\n", __LINE__, __FILE__);

			//Create the context

			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)clPlatform, 

				0

			};

			printf("clCreateContext...\n"); m_clContextReal = clCreateContext(props, 1, &m_clDeviceReal, NULL, NULL, &ciErr1); if (ciErr1 != CL_SUCCESS)  printf("Error in clCreateContext, Line %u in file %s !!!\n\n", __LINE__, __FILE__);

			// Create a command-queue

			printf("clCreateCommandQueue...\n"); m_clCommandQueueReal = clCreateCommandQueue(m_clContextReal, m_clDeviceReal, 0, &ciErr1);  if (ciErr1 != CL_SUCCESS) printf("Error in clCreateCommandQueue, Line %u in file %s !!!\n\n", __LINE__, __FILE__);

                }

Hi

I am facing a weird problem:

i can correctly share buffer objects between CL and GL, but if i try the same with texture2D or 3D, i keep getting error -60 (CL_INVALID_GL_OBJECT)

This on W764, latest drivers (260, CL 1.0, it happened also with previous drivers 259), gtx280

so my code is the following:

void createCLBuffer() {

	if (!m_vecDataCL) {

		m_clOrigin[0] = m_clOrigin[1] =m_clOrigin[2] =0;

		m_clRegion[0] = m_uWidth; m_clRegion[1] = m_uHeight; m_clRegion[2] = m_uDepth;

		

		switch(m_GLStorageMode) {

		case GL_STORAGE_BUFFER:

			createGLBufferStorage();

			m_vecDataCL = clCreateFromGLBuffer(*m_clContext, CL_MEM_READ_WRITE, m_glItem, &clErr);

			m_oclContext->checkforCLerrors(clErr,"BufferCL",__LINE__);

			break;

		case GL_STORAGE_TEX3D:		

				createGLTexture3DStorage();

				//m_vecDataCL = clCreateFromGLBuffer(*m_clContext, CL_MEM_READ_WRITE, m_glItem, &clErr);

				glBindTexture(GL_TEXTURE_3D,m_glItem); m_oclContext->checkforGLerrors("BufferCL()",__LINE__);

				m_vecDataCL = clCreateFromGLTexture3D (*m_clContext, CL_MEM_READ_ONLY, GL_TEXTURE_3D, 0, m_glItem, &clErr);

				m_oclContext->checkforCLerrors(clErr,"BufferCL",__LINE__);

				glBindTexture(GL_TEXTURE_3D,0); m_oclContext->checkforGLerrors("BufferCL()",__LINE__);

				GLenum glTarget;

				GLint glLevel;

				clErr = clGetGLTextureInfo ( m_vecDataCL,CL_GL_TEXTURE_TARGET, sizeof(GLenum), &glTarget, NULL); m_oclContext->checkforCLerrors(clErr,"BufferCL",__LINE__);

				clErr = clGetGLTextureInfo ( m_vecDataCL,CL_GL_MIPMAP_LEVEL, sizeof(GLint), &glLevel, NULL); m_oclContext->checkforCLerrors(clErr,"BufferCL",__LINE__);

				break;	

		case GL_STORAGE_TEX2D:		

				createGLTexture2DStorage();

				//m_vecDataCL = clCreateFromGLBuffer(*m_clContext, CL_MEM_READ_WRITE, m_glItem, &clErr);

				m_vecDataCL = clCreateFromGLTexture2D (*m_clContext, CL_MEM_READ_WRITE, GL_TEXTURE_2D, 0, m_glItem, &clErr);

				m_oclContext->checkforCLerrors(clErr,"BufferCL",__LINE__);

				break;	

		}

		clEnqueueAcquireGLObjects(*m_clCommandQueue, 1, &m_vecDataCL, 0,0,0);

	}

};

void createGLBufferStorage() {

	glGenBuffers(1, &m_glItem);

	glBindBuffer(GL_TEXTURE_BUFFER, m_glItem);

	glBufferData(GL_TEXTURE_BUFFER, sizeof(Type) * m_uWidth*m_uHeight*m_uDepth*m_uTimesteps*m_uChannels, 0,

		GL_DYNAMIC_COPY);

	m_oclContext->checkforGLerrors("BufferCL()",__LINE__);

	glBindBuffer(GL_TEXTURE_BUFFER, 0);

}

void createGLTexture2DStorage() {

	glGenTextures(1, &m_glItem);

	glBindTexture(GL_TEXTURE_2D,m_glItem); m_oclContext->checkforGLerrors("BufferCL()",__LINE__);

	glPixelStorei(GL_UNPACK_ALIGNMENT,1); m_oclContext->checkforGLerrors("BufferCL()",__LINE__);

	glTexImage2D(GL_TEXTURE_2D, 

		0,

		GL_ALPHA,

		m_uWidth,

		m_uHeight,

		0,

		GL_ALPHA,

		GL_UNSIGNED_BYTE,

		NULL

		); m_oclContext->checkforGLerrors("BufferCL()",__LINE__);

	glBindTexture(GL_TEXTURE_2D,0); m_oclContext->checkforGLerrors("BufferCL()",__LINE__);

}

void createGLTexture3DStorage(){

	glGenTextures(1, &m_glItem);

	glBindTexture(GL_TEXTURE_3D,m_glItem); m_oclContext->checkforGLerrors("BufferCL()",__LINE__);

	glPixelStorei(GL_UNPACK_ALIGNMENT,1); m_oclContext->checkforGLerrors("BufferCL()",__LINE__);

	glTexImage3D(GL_TEXTURE_3D, 

					0,

					GL_ALPHA,

					m_uWidth,

					m_uHeight,

					m_uDepth,

					0,

					GL_ALPHA,

					GL_UNSIGNED_BYTE,

					NULL

	); m_oclContext->checkforGLerrors("BufferCL()",__LINE__);

	glBindTexture(GL_TEXTURE_3D,0); m_oclContext->checkforGLerrors("BufferCL()",__LINE__);

}

finally here is how i initialize the shared context:

		void init()

		{

			//Get an OpenCL platform

			printf("clGetPlatformID...\n");  ciErr1 = clGetPlatformIDs(1, &clPlatform, NULL); if (ciErr1 != CL_SUCCESS) printf("Error in clGetPlatformID, Line %u in file %s !!!\n\n", __LINE__, __FILE__);

			//Get the devices

			printf("clGetDeviceIDs...\n"); ciErr1 = clGetDeviceIDs(clPlatform, CL_DEVICE_TYPE_GPU, 1, &m_clDeviceReal, NULL); if (ciErr1 != CL_SUCCESS) printf("Error in clGetDeviceIDs, Line %u in file %s !!!\n\n", __LINE__, __FILE__);

			//Create the context

			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)clPlatform, 

				0

			};

			printf("clCreateContext...\n"); m_clContextReal = clCreateContext(props, 1, &m_clDeviceReal, NULL, NULL, &ciErr1); if (ciErr1 != CL_SUCCESS)  printf("Error in clCreateContext, Line %u in file %s !!!\n\n", __LINE__, __FILE__);

			// Create a command-queue

			printf("clCreateCommandQueue...\n"); m_clCommandQueueReal = clCreateCommandQueue(m_clContextReal, m_clDeviceReal, 0, &ciErr1);  if (ciErr1 != CL_SUCCESS) printf("Error in clCreateCommandQueue, Line %u in file %s !!!\n\n", __LINE__, __FILE__);

                }

I want to answer myself saying that i discovered the problem: it’s the use of GL_ALPHA in the internal format.

I would like to point the attention here:
among my supported image format i have:
20 CL_A CL_UNSIGNED_INT8

Now, since the correspondence between GL internal formats and CL internal formats is documented NOWHERE , i lost 2 days trying to figure out this problem.
I think it’s a shame, for a company like nVIDIA that claims to have the best OpenCL support, that this happens and developers have to “guess” what works and what
does not.

(not to mention the lack of the extension for 3d image writing, but that’s another thing)

I want to answer myself saying that i discovered the problem: it’s the use of GL_ALPHA in the internal format.

I would like to point the attention here:
among my supported image format i have:
20 CL_A CL_UNSIGNED_INT8

Now, since the correspondence between GL internal formats and CL internal formats is documented NOWHERE , i lost 2 days trying to figure out this problem.
I think it’s a shame, for a company like nVIDIA that claims to have the best OpenCL support, that this happens and developers have to “guess” what works and what
does not.

(not to mention the lack of the extension for 3d image writing, but that’s another thing)