Ubuntu下Intel集显无法使用OpenGL 3.0以上版本的问题解决方法

把系统从 Ubuntu 16.04 换到了最新 Ubuntu 19.10 后在编写 OpenGL 程序后发生了闪退,但是把程序放在 16.04 上跑就跑得好好的。

找 BUG 时发现可能是没有获取到正确的版本号,使得程序认为找不到相应的 OpenGL 版本。百思不得其解,不应该啊,换了新系统程序就不能用了?在两个系统间来回切换对比后发现了问题的原因。

原来是我在 19.10 上没有安装独显的驱动,系统只运行了自带的集显驱动,虽然集显的驱动支持OpenGL4.5,但可能是为了兼容性的原因,默认只允许使用3.0及以下的版本。

程序在运行时在加载 OpenGL 时首先会查看 OpenGL version string ,如果该字符串中所表示的版本号小于所需的版本则会直接报错退出。

执行glxinfo | grep OpenGL命令查看系统中的OpenGL信息时,显示以下信息:

OpenGL vendor string: Intel Open Source Technology Center
OpenGL renderer string: Mesa DRI Intel(R) HD Graphics 5500 (Broadwell GT2) 
OpenGL core profile version string: 4.5 (Core Profile) Mesa 19.2.8
OpenGL core profile shading language version string: 4.50
OpenGL core profile context flags: (none)
OpenGL core profile profile mask: core profile
OpenGL core profile extensions:
OpenGL version string: 3.0 Mesa 19.2.8
OpenGL shading language version string: 1.30
OpenGL context flags: (none)
OpenGL extensions:
OpenGL ES profile version string: OpenGL ES 3.1 Mesa 19.2.8
OpenGL ES profile shading language version string: OpenGL ES GLSL ES 3.10
OpenGL ES profile extensions:

根据得到的信息可以看到 OpenGL core profile version string4.5 说明显卡驱动是支持 OpenGL 4.5 的,但是可能是因为兼容性原因无法同时使用核心模式和兼容模式故而 OpenGL version string 默认返回 3.0

Google 搜了一圈后解决方案是在执行程序前强制指定使用特定的版本:

MESA_GL_VERSION_OVERRIDE=4.5 command

命令中的MESA_GL_VERSION_OVERRIDE=4.5就是使用版本号4.5来覆盖系统提供的版本,后面的command就是需要运行的程序名。

例如下列命令执行后

MESA_GL_VERSION_OVERRIDE=4.5 glxinfo | grep OpenGL

输出的结果中的 OpenGL version string 就从3.0变为了4.5

OpenGL version string: 4.5 (Compatibility Profile) Mesa 19.2.8

其实MESA_GL_VERSION_OVERRIDE就是一个环境变量,如果觉得麻烦可以在~/.bashrc中添加一行export MESA_GL_VERSION_OVERRIDE=4.5就可以一劳永逸地解决这个问题了。

参考资料:

https://askubuntu.com/questions/850900/why-is-my-opengl-version-stuck-at-3-0-despite-new-hardware-software

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章