Skip to content

Instantly share code, notes, and snippets.

@gschintgen
Last active June 28, 2024 10:30
Show Gist options
  • Save gschintgen/ea148962f024b1c75e3c9ad5a114afa5 to your computer and use it in GitHub Desktop.
Save gschintgen/ea148962f024b1c75e3c9ad5a114afa5 to your computer and use it in GitHub Desktop.
AMD: Patching Mesa on Ubuntu for low-latency VA-API encoding

AMD VA-API encoding latency

As of this writing (2024-05), VA-API has no support for AMD's low-latency encoding mode. This is problematic for ultra low-latency applications like realtime game-streaming as implemented by Sunshine.

As a temporary fix, Mesa can be patched to unconditionally use the low-latency optimizations. This entails a higher power consumption and may not be suitable for all applications.

Patching Mesa for Ubuntu

I'm providing a basic shell script to re-build Ubuntu's Mesa from the official source .debs with the necessary patch. It has only seen some light testing (22.04 only) and is provided without support. For my own usage I'm executing this script in a throw-away Incus container based on Ubuntu 22.04. The script does not clean up after itself (i.e. no purging of build dependencies etc.). Make sure to validate the scripts' contents before executing it.

In a clean container/VM, execute the script:

./build-patched-mesa.sh

The script will build a number of .deb files in mesa-lowlat.

Installation

The package that needs to be updated in order to fix Sunshine's encoding latency is libgl1-mesa-dri. Copy the relevant file (currently libgl1-mesa-dri_23.2.1-1ubuntu3.1~22.04.2_amd64.deb) from your build environment to the host system.

Unfortunately the newly built .deb can't just be installed using dpkg -i since there's a conflict with the Changelog file of the version that is already installed on the system. Hence the dangerous --force-overwrite option. Proceed at your own risk.

sudo dpkg -i --force-overwrite libgl1-mesa-dri_23.2.1-1ubuntu3.1~22.04.2_amd64.deb

The package can be marked hold in order to prevent accidental changes to it by the system's package manager.

sudo apt-mark hold libgl1-mesa-dri

Reverting

Reverting to the distribution's official version of the packages is done via:

sudo apt install --mark-auto --reinstall -o DPkg::options::="--force-overwrite" libgl1-mesa-dri
#!/bin/bash
set -e
mkdir mesa-lowlat
cd mesa-lowlat
# Enabling deb-src repositories
sudo sed -i -e 's/# \(deb-src.*ubuntu.com\/ubuntu.*\)/\1/' /etc/apt/sources.list
sudo apt update
sudo apt install wget build-essential fakeroot devscripts -y
sudo apt build-dep libgl1-mesa-dri -y
apt source libgl1-mesa-dri
wget https://gitlab.freedesktop.org/drm/amd/uploads/0981011557dd34e70b61f91e012a8bae/0001-low-latency.patch
MESADIR=$(ls -d */)
cd $MESADIR
patch -p1 < ../0001-low-latency.patch
debuild -us -uc -i -I
cd ..
ls -lh libgl1-mesa-dri*.deb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment