1
votes

I want to pass a OpenGL texture to OpenCL in Python Therefore I create a texture in OpenGL using a frambuffer:

        fbo =glGenFramebuffersEXT(1))
        glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo)
        depthbuffer = glGenRenderbuffersEXT(1)
        glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, depthbuffer)
        glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT, width, height)
        glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, depthbuffer)       

        texture = glGenTextures(1))
        glBindTexture(GL_TEXTURE_2D, texture)
        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, GL_NEAREST)
        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_NEAREST)
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8,  width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, None);
        glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT,  GL_TEXTURE_2D,texture, 0);
        glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo);
        RENDERSCENE()
        glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);

After that I create the OpenCL context and try to pass the texture from OpenGL to OpenCL like this

        self.context = pyopencl.create_some_context()
        self.queue = pyopencl.CommandQueue(self.context)
        if pyopencl.have_gl():
              self.image = pyopencl.GLTexture(self.context, pyopencl.mem_flags.READ_WRITE,GL_TEXTURE_2D, 0, texture, 2)

The texture works fine, if I use it in OpenGL but whehn I tried to use it with OpenCL it crashes:

Traceback (most recent call last):
  File "/home/jul*/*/*er/FBOtest2.py", line 250, in <module>
    fbo.mainGameLoop()
  File "/home/jul*/*/*er/FBOtest2.py", line 223, in mainGameLoop
    self.transformTexturesForOpenCL()  
  File "/home/jul*/*/*er/FBOtest2.py", line 63, in transformTexturesForOpenCL
    self.image = pyopencl.GLTexture(self.context, pyopencl.mem_flags.READ_WRITE,GL_TEXTURE_2D, 0, texture, 2)
pyopencl.LogicError: clCreateFromGLTexture2D failed: invalid context
1

1 Answers

3
votes

I'm not sure you can create "some context". You will need to create the OpenCL context with knowledge of the OpenGL context. Look here for more information on CL-GL interop.