Skip to main content

NVIDIA GPU+Linux+Unity:解决方案

· 5 min read
Ferdinand Su

具有显示屏的完整机器

根据网上的说法,运行以下几条指令即可:

nvidia-settings -a SyncToVBlank=0
nvidia-settings -a XVideoTextureSyncToVBlank=0
export __GL_SYNC_TO_VBLANK=0

可以把性能显著提升,但是仍然达不到同机器在win下的效果。有人说用Vulkan会好些,但是那会导致闪退

没有显示屏的完整机器

  1. 可以用Xvfb来模拟显示屏:sudo apt install xvfb xserver-xorg x11vnc,其中x11vnc是用来通过vnc检测xvfb窗口情况的,可以不装。
  2. 启动虚拟X Server: Xvfb :1 -screen 0 2560x1440x24 (这条命令会挂起当前终端,可以用&放在后台、^C停止)
  3. 设定环境变量export DISPLAY=:1
  4. 启动程序即可

然而,此时程序非常卡顿,这是因为目前的渲染工作只使用了软件的OpenGL(mesa)完成,不卡才怪。

使用GPU加速:没有显示屏的完整机器

开始前,请确保你理解了Xvfb,并正确配置了机器上的nVidia驱动,可以使用nvidia-smi来确保驱动配置正确。

1. 建立真实使用CPU,但是没有显示屏的3D X Server

文档原文参见此处

gpu_bus_id=`nvidia-xconfig --query-gpu-info |  grep -oP '(?<=(PCI BusID\s:\s)).*'`
sudo nvidia-xconfig -a --allow-empty-initial-configuration --use-display-device=None --virtual=2560x1440 --busid $gpu_bus_id

2. 修改xorg.conf,你也可以手动完成

sed -i '/    Option         "UseDisplayDevice" "None"/a\
Option "HardDPMS" "false"\
Option "AllowGLXWithComposite" "true"\
Option "AddRGBGLXVisuals" "true"\
Option "TripleBuffer" "true"
' /etc/X11/xorg.conf
cat << EOF >> /etc/X11/xorg.conf
Section "Extensions"
Option "Composite" "enable"
EndSection
EOF

3. 安装VirtualGL

文档原文参见此处

  1. 下载VirtualGL软件包,Ubuntu/Debian就下virtualgl_3.1_amd64.deb
  2. 运行dpkg -i virtualgl*.deb
  3. 运行apt update && apt install -f来修复不完整的安装(依赖项没有被打包进deb)
  4. 请确保gpu驱动正常
  5. 关闭显示管理器gdm/sddm/ldm,如:service lightdm stop
  6. 以root权限运行vglserver_config,选择"Configure server for use with VirtualGL (GLX + EGL back ends)", 三个问题全选n,这样会关闭全部的安全防护,而安全防护对于内网服务器是多余的;你当然可以酌情保留
  7. 重启显示管理器gdm/sddm/ldm,如:service lightdm start
  8. 检查:glxinfo -display :0 | grep OpenGL,看到nVidia就算胜利

4. 运行程序

  1. 可以用Xvfb来模拟显示屏:sudo apt install xvfb xserver-xorg x11vnc,其中x11vnc是用来通过vnc检测xvfb窗口情况的,可以不装。
  2. 启动虚拟X Server: Xvfb :1 -screen 0 2560x1440x24 (这条命令会挂起当前终端,可以用&放在后台、^C停止)
  3. 设定环境变量export DISPLAY=:1
  4. 使用vglrun启动程序vglrun [vglrun options] 3D-application-executable-or-script [arguments]

修复奇怪的bug:一段时间后的帧率骤降

在使用vgl运行3d程序,如例程vglrun /opt/VirtualGL/bin/glxspheres64时,可能出现开机一段时间后帧率骤降的现象:

Polygons in scene: 62464 (61 spheres * 1024 polys/spheres)
GLX FB config ID of window: 0xad (8/8/8/0)
Visual ID of window: 0x21
Context is Direct
OpenGL Renderer: NVIDIA GeForce RTX 3060/PCIe/SSE2
1349.869818 frames/sec - 1506.454717 Mpixels/sec
......
1361.546995 frames/sec - 1519.486447 Mpixels/sec
707.108016 frames/sec - 789.132545 Mpixels/sec
0.999221 frames/sec - 1.115131 Mpixels/sec
1.000751 frames/sec - 1.116838 Mpixels/sec
0.998357 frames/sec - 1.114166 Mpixels/sec
......

搜了一下发现:别人也遇到过这个问题:

Hi all,

I ran into the same issue. The FPS drops to 1 after the the screen lock kicks in, i.e. after the number of minutes when the the screen locks. This happens since the update to nvidia 440.31. System is centos 7. GFX card is GeForce GTX 1080 Ti. The effect does not show in identical system with GeForce RTX 2080.

Any help on how to resolve this is very welcome.

猜测是连接了屏幕导致的,遂拔除屏幕,一切恢复正常。这是我调过最离谱的bug了。