PyOpenGL cannot compile shader

血红的双手。 提交于 2019-12-13 14:28:53

问题


I'm using Python3, Qt4 and PyOpenGL on Debian testing with the python3-pyside packages. This the minimized example code:

#!/bin/env python3

from OpenGL.GL import shaders, GL_VERTEX_SHADER
from PySide import QtGui
from PySide.QtOpenGL import QGLWidget

class MyGLWidget(QGLWidget):
    def initializeGL(self):
        self.vertex_shader = shaders.compileShader("""
        #version 330
        layout(location = 0) in vec4 position;
        void main()
        {
            gl_Position = position;
        }
        """, GL_VERTEX_SHADER)

if __name__ == '__main__':
    app = QtGui.QApplication(["shader fail"])
    widget = MyGLWidget()
    widget.show()
    app.exec_()

This is the error:

$ python3 fail.py 
Traceback (most recent call last):
  File "fail.py", line 16, in initializeGL
    """, GL_VERTEX_SHADER)
  File "/usr/lib/python3/dist-packages/OpenGL/GL/shaders.py", line 231, in compileShader
    shaderType,
RuntimeError: ("Shader compile failure (0): b'0:1(13): preprocessor error: syntax error, unexpected HASH_TOKEN\\n'", [b'\n        #version 330\n        layout(location = 0) in vec4 position;\n        void main()\n        {\n            gl_Position = position;\n        }\n        '], GL_VERTEX_SHADER)

This is the most simple shader I can think of and the error is not very descriptive. Can you please help me?

EDIT: If in initializeGL I first do print(self.context().format()), the output is

<PySide.QtOpenGL.QGLFormat(options QFlags(0x1|0x2|0x4|0x8|0x20|0x80|0x400) , plane  0 , depthBufferSize  24 , accumBufferSize  -1 , stencilBufferSize  8 , redBufferSize  8 , greenBufferSize  8 , blueBufferSize  8 , alphaBufferSize  8 , samples  -1 , swapInterval  0 , majorVersion  1 , minorVersion  0 , profile  0 )   at 0x7f66bc804508>

Which means I get an OpenGL 1.0 context. So I guess I have to request a higher version context somehow.

As for my graphics hardware: I use the integrated graphics in my Intel i7-4710MQ with the Debian default driver. It identifies to lspci as

00:02.0 VGA compatible controller: Intel Corporation 4th Gen Core Processor Integrated Graphics Controller (rev 06)

More interestingly:

$ glxinfo | grep -i version
server glx version string: 1.4
client glx version string: 1.4
GLX version: 1.4
OpenGL core profile version string: 3.3 (Core Profile) Mesa 10.4.2
OpenGL core profile shading language version string: 3.30
OpenGL version string: 3.0 Mesa 10.4.2
OpenGL shading language version string: 1.30
OpenGL ES profile version string: OpenGL ES 3.0 Mesa 10.4.2
OpenGL ES profile shading language version string: OpenGL ES GLSL ES 3.0

So the hardware+driver should be able to do the trick.

Update:

print(int(QGLFormat.openGLVersionFlags()))

gives 4223 which includes versions up to 3.0 but not 3.1 or higher, which is weird because up to 3.3 should be available.

If I try requesting a higher version context like this:

fmt = QGLFormat(QGLFormat.OpenGL_Version_3_3 | QGLFormat.CompatibilityProfile)
widget = MyGLWidget(fmt)

or try it with Version_3_0 or Version_2_0 with QGLFormat.NoProfile I still get a context of version 1.0

来源:https://stackoverflow.com/questions/29993306/pyopengl-cannot-compile-shader

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!