# Zink+Venus status report

These are my current findings regarding using Zink+Venus as an alternative to VirGL.

## Section 0: Setup

The following tests were done on two different computers:

- Setup #1: A Volteer Chromebook
- Setup #2: A desktop with a Ryzen 5 1600AF CPU, RX6600 XT GPU and 32GB of RAM

The following games were tested:
- Portal 2
- XCOM 2
- Tomb Raider

## Section 1: Rendering issues

- Portal 2
  - Almost renders correctly, but quite a few meshes are untextured and default to a single color. This appears to be a Zink bug, I opened an issue for it: https://gitlab.freedesktop.org/mesa/mesa/-/issues/6240
- XCOM 2
  - Renders correctly.
- Tomb Raider
  - Constantly flickers between the correct frame and an incorrect one with heavy artifacts. I can't reproduce this bug outside borealis.

## Section 2: Performance comparison

To measure performance, the gpu clock frequency was locked in both machines:

- Chromebook: `intel_gpu_frequency -m`
- Desktop: `echo "profile_peak" > /sys/class/drm/card0/device/power_dpm_force_performance_level`

### Test 1: apitraces

I mostly used apitraces from the three games to compare performance between VirGL and Zink+Venus.
Vsync was disabled with `vblank_mode=0` and `MESA_VK_WSI_PRESENT_MODE=immediate` for GL and Vulkan, respectively.

#### Pros and cons

There's one big downside to this approach of using traces to measure performance, which is that apitrace does a basic `frames_rendered/time_elapsed` calculation to return the FPS metric for a particular trace. The problem is that these traces usually spend quite a bit of time on the menus and loading screens before entering the game, which can (and often does) decrease the reported framerate. For example, the XCOM2 trace on top of virgl is reported as having a framerate of ~31, but in reality the gameplay framerate is closer to 50 FPS, but the loading screens decrease the reported value.

With that said, the benefit of using this approach is that one can be sure that the exact same API calls are being made in all the tests. Because of this, comparing the FPS metric given between VirGL and Zink+Venus is very meaningful. Just keep in mind that the number does not necessarily reflect the gameplay experience.

#### Results

The following table shows the FPS metric reported by apitrace when running the traces with each mesa backend.

```
|-----------------------------------|----------|--------|------------|
|                       FPS per game per setup                       |
|-----------------------------------|----------|--------|------------|
|                                   | Portal 2 | XCOM 2 | TombRaider |
|-----------------------------------|----------|--------|------------|
| Chromebook with VirGL             |    120   |   31   |     43     |
| Chromebook with Zink+Venus        |    72    |    5   |     27     |
| Desktop with VirGL (vtest)        |    48    |    8   |      -     |
| Desktop with Zink+Venus (vtest)   |    62    |    4   |      -     |
| Desktop with Zink+RADV            |    80    |   26   |      -     |
| Desktop native GL                 |    139   |   29   |      -     |
|-----------------------------------|----------|--------|------------|
```

These results almost all point to Zink+Venus giving significantly worse performance than VirGL, with the exception of Portal 2 on vtest, which has a better performance with Zink+Venus already.

### Test 2: glbench

I also used the glbench tool from https://chromium.googlesource.com/chromiumos/platform/glbench/.
Overall it seems that VirGL does better than Zink+Venus here, and Zink logs many memory errors. 

  Zink+Venus: https://gitlab.collabora.com/-/snippets/171

  VirGL: https://gitlab.collabora.com/-/snippets/172

## Section 3: Profiling

### Perfetto

Profiling with perfetto I didn't find anything immediately obvious, but it's tricky because perfetto has a memory limit for the traces otherwise the browser tab memory explodes, so we have to pick and choose which events we want to trace.

- Here are some perfetto traces:
  - Portal 2 on Zink+Venus: https://drive.google.com/file/d/1mAnVLvgkNw24e1KNJnyY3RM0fV-Ejb_C/view?usp=sharing
  - Portal 2 on VirGL: https://drive.google.com/file/d/1ibFJsXpjJTEMqM4Ft1jaVH98rTf1-xB8/view?usp=sharing

### Perf

On the other hand, profiling with perf I noticed that Venus spends a lot of time idle, suggesting that most of the work is on the guest side, likely on Zink. Flamegraphing also suggests some major differences between the two paths:

- XCOM2 on Zink+Venus: https://gitlab.freedesktop.org/italove/virglrenderer/-/raw/3158823cae53edaff706e9a0cbdb1fea081cda2c/virgl_perf.svg
- XCOM2 on VirGL: https://gitlab.freedesktop.org/italove/virglrenderer/-/raw/3158823cae53edaff706e9a0cbdb1fea081cda2c/zink_venus_perf.svg

In particular, Zink seems to spend a huge chunk of its time on `_mesa_InvalidateBufferData`, which may be a hint to what's causing the performance difference.

## Section 4: Final considerations

From what I've seen so far it seems we still have some work to do before zink+venus can substitute VirGL, but in at least one of the tests it gives better performance, which might indicate that with the bugs fixed it can in the future give us better performance than VirGL with a more maintainable codebase.