Updated: October 2, 2020
Virtualization is a clever thing. A computer within a computer, if you will, an endless series of possibilities and usecase scenarios. Except one. Gaming. Ask any nerd who runs virtual machines, for whatever reason, and they will most likely point out one big deficiency in all and every virtualization software - insufficiently developed or powerful enough graphics stack.
Indeed, quite often, when you run guest operating systems inside virtual machines, you are often limited by the graphics acceleration - 2D and 3D functionality that is behind the native capabilities of your host. So if you think virtualization can solve many of your issues - gaming is not one of them. Virgil 3d is a project that aims to bring 3D wonders to virtualization software, with the goal of one day offering parity to host performance. An implementation of this solution is QEMU Virgil, which allows you to have SDL2 and Virgil 3d enabled in QEMU/KVM-based virtual machines. Sounds interesting. So let's have a look.
Setup
The one big downside with this kind of effort is that can be quite complicated, and it takes time to get everything configured and running. However, with Virgil 3d, if you want to try the existing snap package, it dispenses with all gory bits of manual compilation and whatnot. Hint: As one must always share, I be affiliated with the snap effort, so if you smell bias, run away!
sudo snap install qemu-virgil
sudo snap connect qemu-virgil:kvm
Running a virtual machine
Let's have a look at a (sample) command for how you can start a 3D-accelerated virtual machine. If you feel a little overwhelmed, I'd suggest you take a look at my Introduction to KVM guide from a few years ago. Most of the stuff detailed there still applies.
qemu-virgil -enable-kvm -m 4096 -device virtio-vga,virgl=on -display sdl,gl=on -netdev user,id=ethernet.0,hostfwd=tcp::10022-:22 -device rtl8139,netdev=ethernet.0 -soundhw ac97 kubuntu.img -boot d -cdrom kubuntu.iso
What do we have here?
Let's take a look at all these colorful options:
- -enable-kvm - We use the full KVM virtualization support. Needless to say, your hardware should have the necessary extensions (present in the CPU and enabled in BIOS).
- -m 4096 - The VM memory is set to 4GB.
- -device ... - This option adds device drivers. Specifically, we use virgl.
- -display ... - This option enables the use of SDL and OpenGL rendering for our display.
- -netdev ... - This option configures a network interface that has TCP port forwarding, so you can SSH into your virtual machine (using the host port 10022). The network device will be an RTL8139 virtual adapter. Now, this adapter is limited to 100 Mbps, but you can also use e1000. The official KVM networking page has more details on this topic.
- -soundhw - We configure the sound card.
- kubuntu.img - This is the name of the virtual hard disk. We've not created one yet - and you can do this either by creating a disk image with dd (either a full or a sparse one), or you can use the qemu-img command.
- -boot d - This option specifies the boot order; d means CD-ROM first.
- -cdrom kubuntu.iso - Here, we have the kubuntu.iso as the virtual CD image. We will boot into the live session first, install the operating system, and then, on subsequent uses, you can simply remove the boot and cdrom options from the command.
There are many other options you could try. Like the number of virtual CPUs, different CPU models, snapshots, disk caching mode, USB functionality, various memory management feature, and more. But we're not here to master KVM, we're here to have some 3D fun.
Create hard disk
So, you can do something like:
qemu-img create -f qcow2 kubuntu.img 20g
Start virtual machine & 3D graphics
Press Enter. Magic ensues. The virtual machine will boot, and you will soon see the desktop of whichever guest operating system you've chosen. For my test, I've selected Kubuntu 20.04, running on top of Fedora 32. But you can use anything you like for your guest, including Windows.
You can check the 3D acceleration enablement with:
glxinfo | grep "direct rendering"
Now, this does not mean that every single 3D rendering function has been implemented, but it sure if a good start. Indeed, my Kubuntu VM had all the nice bits and pieces, and it was fairly sprightly, including solid 3D performance. I even ran the PassMarkburn-in test to see what gives. Cushty.
Conclusion
Virgil looks like a really nice thing. Because it's a relatively simple and inexpensive solution. Now, I know people who have multi-GPU computers, and they have clever VM configurations, where different GPU cards are used separately by the host and the guest virtual machine, using PCI bus identifiers and whatnot, offering near-native performance in their guest systems. But that's often beyond the skill and budget of an ordinary nerd. Because let's be realistic, really ordinary people won't use Linux, or virtualization, or anything like that.
In between zero and everything 3D, if you have a need for some fancy graphics in your virtual machines, then perhaps Virgil can provide you with the right level of functionality. It's maybe niche, it's still in development, but it works reasonably well, and from my testing, the performance is adequate. We're still not at a stage where virtualization can fully and completely replace physical hardware (if ever), but Virgil is a right step in the right direction. Have a go, nerd away. We're done here.
Cheers.