blog.polynom.me/how-i-play-games/index.html

150 lines
10 KiB
HTML
Raw Permalink Normal View History

<!doctype html>
<html lang="en-gb">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link href="https://blog.polynom.me/css/index.css" rel="stylesheet" integrity="sha384-EJX4ZZbYMJeuoLRp93IbM/RYSzZmxw42TK7sgSRBEMChbBFK4NYUQEfsz3nBJQm8" crossorigin="anonymous" />
<link rel="alternate" type="application/rss+xml" title="blog.polynom.me Atom feed" href="https://blog.polynom.me/atom.xml">
<meta property="og:description" content="" />
<meta property="og:title" content="How I Play Games on My Linux PC" />
<title>How I Play Games on My Linux PC</title>
</head>
<body>
<div class="flex flex-col p-2 md:p-8 items-start md:w-4/5 mx-auto">
<!-- Header -->
<div class="flex flex-row self-center">
<img class="w-12 h-12 md:w-24 md:h-24 rounded-lg" src="https://blog.polynom.me/img/avatar.jpg" integrity="sha384-uiNteVXosQ2+o/izp41L1G9VwuwYDYCOPxzFWks058DMUhW7KfQXcipM7WqgSgEZ" alt="Profile picture" crossorigin="anonymous"/>
<div class="ml-4 self-center">
<a class="self-center text-2xl font-bold" href="/">PapaTutuWawa's Blog</a>
<ul class="list-none">
<li class="inline mr-8"><a href="/">Posts</a></li>
<li class="inline mr-8"><a href="https://blog.polynom.me/atom.xml">RSS</a></li>
<li class="inline mr-8"><a href="https://polynom.me">About</a></li>
</ul>
</div>
</div>
<!-- Container for posts -->
<div class="mx-auto mt-4 w-full md:max-w-prose">
<h1 class="text-indigo-400 text-3xl">How I Play Games on My Linux PC</h1>
<span class="text-md mt-2">Posted on 2019-06-08</span>
<!-- Actual article -->
<article class="prose lg:prose-lg text-white mt-4">
<p>I love Linux. In fact, I love it so much that it runs on every computer I use, except for my phone but that
can be changed. It always amazes me how much control Linux gives me about my computer and how easy it is
to create a script that just does everything that I was doing manually before.</p>
<span id="continue-reading"></span>
<p>Since Septemper of 2018, I decided to stop dual booting Windows and Linux and only use Linux. I mean, I could
play my most played games under Linux: <em>CS:GO, Split/Second Velocity (Wine), NieR: Automata (Wine).</em> But there
were still some games that I could not play as either have no Linux port or refuse to run with Wine. I love
playing <em>Tom Clancy's The Division</em> and <em>The Division 2</em>. I really enjoyed playing <em>Tom Clancy's Rainbow Six Siege</em> and
<em>Wildlands</em> was much fun. Except for <em>The Division</em>, none of these games runs under Wine. So what do?</p>
<h1 id="gpu-passthrough">GPU Passthrough</h1>
<p>Before even having the thought of switching to Linux &quot;full-time&quot;, I stumbled across <a href="https://invidio.us/watch?v=16dbAUrtMX4">this video</a> by Level1Linux.
It introduced me to the concept of hardware passthrough and I wanted to do it ever since. Now that my mainboard
has an IOMMU and my CPU supports all needed virtualization extensions, I was ready.</p>
<p>At that time I was using a AMD Ryzen 2400G and a Nvidia Geforce GTX 1060. I chose this particular CPU
as it contains an iGPU, allowing me to have video output of my host even when I pass the 1060 through
to my VM.</p>
<!-- There are many great tutorials out there that teach you to do this thing but I was amazed at how well -->
<!-- the games run. It should have come to no suprise but it still did. -->
<p>The only thing that I did not like was the fact that the Nvidia driver refuses to run in a Virtual Machine, so
I had to configure my VM via libvirt in a way that hides the fact that the driver is run inside a VM.</p>
<h1 id="dynamic-gpu-passthrough">Dynamic GPU Passthrough</h1>
<p>While this allowed me to play <em>The Division</em>, it was tedious to reboot to not have the GPU bound to the
vfio-pci module so that I could use it on my host. Most guides expect you to have a second powerful GPU
so that you don't have to worry about the unavailable GPU but to me it seemed like a waste.</p>
<p>So I wrote myself a script which...</p>
<ul>
<li>unloaded all Nvidia kernel modules;</li>
<li>started libvirt and loaded the vfio-pci module;</li>
<li>bound the GPU to the vfio-pci module;</li>
<li>started the VM.</li>
</ul>
<p>The only problem with this was that the Nvidia modules kept being loaded by the X server. This was annoying
since I had to blacklist the modules, which prevented me from using the GPU on my host. The solution, albeit
very hacky, was a custom package which installed the kernel modules into a new folder from where the modules
were manually inserted using <code>insmod</code> by another script.</p>
<p>My host's video output comes from my Ryzen's iGPU. It is not powerful enough to run games like <em>Split/Second Velocity</em>
or <em>CS:GO</em> at an acceptable framerate, so what do?</p>
<p>Since the Nvidia driver for Linux is proprietary <a href="https://wiki.archlinux.org/index.php/PRIME#PRIME_GPU_offloading">PRIME offloading</a> was not an option. I, however, discovered
a library which allowed the offloading of an application's rendering - if it uses GLX - onto another GPU: <a href="https://github.com/amonakov/primus">primus</a>.</p>
<p>It worked well enough for games that used OpenGL, like <em>CS:GO</em>. But when I tried launching <em>Split/Second Velocity</em>
using Wine, it crashed. Vulkan offloading was not possible with primus, but with <a href="https://github.com/felixdoerre/primus_vk">primus_vk</a>. This library I never got to work so I cannot say anything about it.</p>
<p>The only solution to that, from my point-of-view, was to create another script with launched a second X server
on the Nvidia GPU, start Openbox as a WM on that X server and create a seamless transition from my iGPU- to my
Nvidia-X-server using <a href="https://github.com/debauchee/barrier">barrier</a>. I then could start applications like
Steam on the Nvidia X server and use the GPU's full potential.</p>
<p>Since I was using barrier for the second X server I tried doing the same with barrier inside my VM and all I can
say is that it works very well. It made the entire &quot;workflow&quot; with the VM much less painful as I could just take
control of the host if I ever needed to without the need for a second keyboard.</p>
<h1 id="gpu-changes">GPU Changes</h1>
<p>Today, my PC runs the same AMD CPU. However, the Nvidia GPU got replaced with an AMD RX 590. This allowed me to
use the opensource amdgpu driver, which was and still is a huge plus for me. It complicated some things for me
though.</p>
<p>While I can now use PRIME offloading on any application I want, I cannot simply unbind the RX 590 from the amdgpu
driver while in X for use in my VM. While the driver exposes this functionality, it crashes the kernel as soon
as I try to suspend or shutdown my computer.</p>
<p>The only solution for this is to blacklist the amdgpu module when starting the kernel, bind the GPU to the vfio-pci
driver and pass it through. Then I can load the amdgpu module again and have it attach itself to my iGPU. When I am
done with using the VM, I can re-attach the GPU to the amdgpu driver and use it there.</p>
<p>There are some issues with this entire setup though:</p>
<ul>
<li>sometimes after re-attaching, the GPU does not run with full speed. While I can normally play <em>CS:GO</em> with ~80 FPS, it can be as low as ~55 FPS after re-attachment.</li>
<li>the GPU cannot be reset by the Linux kernel. This means that the GPU has to be disabled inside Windows before shutting down the VM. Otherwise, the amdgpu module cannot bind to the GPU which even crashed my kernel.</li>
</ul>
<h1 id="some-freezes">Some Freezes</h1>
<p>Ignoring the GPU issue, since around Linux kernel 4.1x I experienced another issue: My computer would sometimes freeze
up when opening <em>Steam</em>. In even newer versions, it even freezed by PC when I gave my VM 10GB of RAM, but did not when
I gave my VM only 8GB.</p>
<p>By running htop with a really small refresh interval I was lucky to observe the probable cause of these freezes: The
kernel tried to swap as much as he could, thus making everything grind to a halt. The solution to this, even though
it <em>feels</em> hacky, is to just tell the kernel to swap less aggressively by setting <code>vm.swappiness</code> to either a much
lower value to swap later to to 0 to stop swapping.</p>
<h1 id="audio">Audio</h1>
<p>QEMU, which I used as libvirt's backend, allows you to &quot;pass through&quot; audio from inside the VM to your PulseAudio socket
on the host. This worked okay-ish at first, but now - presumably because something got updated inside QEMU - it
works well enough to play games. I get the occasional crackling but it is not distracting at all.</p>
<p>I also tried a software called <a href="https://github.com/duncanthrax/scream">scream</a> which streamed the audio from a
virtual audio device inside the VM to the network. As the only network interface attached to my VM was going directly
to my host, I just set up the receiver application to listen only on this specific interface. This worked remarkebly
well as I never heard any crackling.</p>
<p>The only issue that I had with scream was that, for some reason, <em>Tom Clancy's The Division 2</em> would crash every 5
minutes when I was using scream. Without it, <em>The Division 2</em> never crashed.</p>
<h1 id="conclusion">Conclusion</h1>
<p>My solutions are probably not the most elegant or the most practical but</p>
<p><img src="/img/as-long-as-it-works.jpg" alt="" /></p>
</article>
<!-- Common post footer -->
<div class="mt-6">
<span class="prose lg:prose-lg text-md text-white">
If you have any questions or comments, then feel free to send me an email (Preferably with GPG encryption)
to papatutuwawa [at] polynom.me or reach out to me on the Fediverse at <a href="https://social.polynom.me&#x2F;papatutuwawa">@papatutuwawa@social.polynom.me</a>.
</span>
</div>
</div>
</div>
</body>
</html>