Hello everybody,
I’m currently experimenting a little bit with OpenCL and OpenGL interoperation capabilities.
I wrote a simple raytracer for OpenCL which works without problems. Now I’m trying to modify this, so that the output is written directly to an OpenGL texture.
However, I already fail at trying to give the opengl texture to opencl via clCreateFromGLTexture2D. This methods does not succeed but instead gives an “INVALID_VALUE” error code back. I can’t figure out, so I will post the relevant code parts here in hope, that someone can give me a hint.
The code is written in C#. I use the opencl.net Wrapper for OpenCL and TAO as the OpenGL Wrapper. However, I hope that the problems I experience have nothing to do with the wrapper implementations.
I initialize the OpenCL context like this:
IntPtr curDC = wglGetCurrentDC();
IntPtr curContext = wglGetCurrentContext();
IntPtr[] platforms = new IntPtr[1];
uint numPlatforms;
OpenCL.GetPlatformIDs(1, platforms, out numPlatforms);
Platform platform = new Platform(platforms[0]);
var contextProperties = new IntPtr[] { (IntPtr) ContextProperties.GL_CONTEXT_KHR, curContext,
(IntPtr) ContextProperties.WGL_HDC_KHR, curDC,
(IntPtr) ContextProperties.PLATFORM, platform };
OCLMan.CreateContextFromType(platform, DeviceType.GPU, contextProperties, null, IntPtr.Zero);
The created opencl context works without problems. I can use it to compile my opencl code and also run it.
But then there is the part, where I try to use CreateFromGLTexture2D:
int texture;
Gl.glGenTextures(1, out texture);
Gl.glBindTexture(Gl.GL_TEXTURE_2D, texture);
Gl.glTexEnvf(Gl.GL_TEXTURE_ENV, Gl.GL_TEXTURE_ENV_MODE, Gl.GL_MODULATE);
Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MIN_FILTER, Gl.GL_NEAREST);
Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MAG_FILTER, Gl.GL_LINEAR);
Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_WRAP_S, Gl.GL_CLAMP_TO_EDGE);
Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_WRAP_T, Gl.GL_CLAMP_TO_EDGE);
byte[] textureData = new byte[256*256*4];
textureData = textureData.Select(x => (byte)50).ToArray();
Gl.glTexImage2D(Gl.GL_TEXTURE_2D, 0, Gl.GL_RGBA, 256, 256, 0, Gl.GL_LUMINANCE, Gl.GL_UNSIGNED_BYTE, textureData);
ErrorCode errcode_ret;
IntPtr b = OpenCL.CreateFromGLTexture2D(OCLMan.Context, (ulong) (MemFlags.WRITE_ONLY | MemFlags.ALLOC_HOST_PTR), (int) CLGLObjectType.TEXTURE2D, 0, (uint) texture, out errcode_ret);
if (errcode_ret != ErrorCode.SUCCESS)
MessageBox.Show("gl -> cl error!");
This never works. I already tried to experiment with all parameters, but always the same error.
You can download the whole sourcecode here:
http://dl.dropbox.com/u/10560525/GPUTracer.zip
You only need Visual Studio 2010 to compile this, everything else is included in it.
I really hope someone can help me with this.
By the way: I only tested this with several NVidia GPUs (with the newest dev drivers). Is it maybe possible, that clCreateFromGLTexture2D simply doesn’t work with NVidia OpenCL implementations?