QEmu on Windows with OpenGL

posted on 2024-03-03

OpenGL and GTK clipboard sharing are disabled by dafault in the QEmu Windows binary distibution.

The clipboard sharing may actually hang the VM: https://gitlab.com/qemu-project/qemu/-/issues/1455 , so be warned.

I wanted both of those features so I have compiled QEMU from sources using MSYS2. Having virglrenderer package present during compilation enables OpenGL.

Below are build instructions assuming you have a MSYS2 enviroment installed already.

Change compile flag -march to native for possibly slightly better optimization in the resulting binaries:

nano /etc/makepkg.conf

Fing CFLAGS=“-march=…” line and change march to “native”

CFLAGS="-march=native  ..."

Install base-devel and git package:

pacman -S base-devel git

Install virglrenderer package (for OpenGL support in QEMU’s virtio-gpu device):

pacman -S ucrt64/mingw-w64-ucrt-x86_64-virglrenderer

Close MSYS2 console window and run new MSYS UCRT64 console window (that’s important).

Clone MSYS2’s MINGW-packages tree where the qemu package have its definition:

git clone https://github.com/msys2/MINGW-packages
cd MINGW-packages/mingw-w64-qemu/

Rebuild package enabling opengl, virglrenderer and gtk-clipboard support:

CONFIGURE_OPTS="--enable-gtk-clipboard --enable-opengl --enable-virglrenderer" makepkg -sCLf --skippgpcheck --nocheck

Install built packages (you may have to change the version numbers):

pacman -U mingw-w64-ucrt-x86_64-qemu-8.2.1-1-any.pkg.tar.zst mingw-w64-ucrt-x86_64-qemu-common-8.2.1-1-any.pkg.tar.zst mingw-w64-ucrt-x86_64-qemu-image-util-8.2.1-1-any.pkg.tar.zst

After installation you may remove the cloned MINGW-packages directory to save some space.

The command I use for the QEMU:

(left out the binary name and –drive flag, the “^” at the end just tell cmd.exe/powershell to continue the command on the next line - an equivalent of “ \” at the end of the line in linux shells)

-smp 4,sockets=1,cores=4,threads=1 ^
-m 5G ^
-L Bios ^
-rtc base=utc,clock=host ^
-parallel none ^
-name Gentoo ^
-no-reboot ^
-accel whpx,kernel-irqchip=off ^
-machine q35 ^
-cpu qemu64,+invtsc,vmware-cpuid-freq=on,+sse,+sse2,+ssse3,+sse4.2,+popcnt,+avx,+aes,+xsave,+xsaveopt ^
-device nec-usb-xhci,id=xhci ^
-device usb-tablet ^
-device usb-kbd ^
-audio driver=dsound,model=virtio ^
-nic user,model=virtio-net-pci ^
-display gtk,gl=on,grab-on-hover=on ^
-device virtio-vga-gl,max_outputs=1,id=gpu0,xres=1920,yres=1080 ^
-vga none ^
-device virtio-serial,packed=on,ioeventfd=on ^
-device virtserialport,name=com.redhat.spice.0,chardev=vdagent0 ^
-chardev qemu-vdagent,id=vdagent0,name=vdagent,clipboard=on,mouse=off

the important lines are the "-device virtio-vga-gl ...", "-vga none" and "display gtk,gl=on...".

I won’t cover virtual machine image creation and installation of the operating system - lots of tutorials are available on the Internet and I haven’t done it myself as I just converted my previous VirtualBox image to QEMU’s qcow2 format.

What you’ll need inside the guest system are:

I won’t cover them here as most of this is distro dependent.

Hope that helps you getting QEmu running with OpenGL (and clipboard sharing) on a Windows host.

Happy hacking!