把系統從 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 string 爲 4.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
就可以一勞永逸地解決這個問題了。
參考資料: