我想在 Python 中将 OpenGL 纹理传递给 OpenCL 因此我使用帧缓冲区在 OpenGL 中创建纹理:
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);
之后,我创建 OpenCL 上下文并尝试像这样将纹理从 OpenGL 传递到 OpenCL
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)
纹理工作正常,如果我在 OpenGL 中使用它,但是当我尝试将它与 OpenCL 一起使用时,它会崩溃:
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