Supporting virtual reality displays in Linux
At linux.conf.au (LCA) 2017 in Hobart, Tasmania, Keith Packard talked with kernel graphics maintainer Dave Airlie about how virtual reality devices should be hooked up to Linux. They both thought it would be pretty straightforward to do, so it would "only take a few weeks", but Packard knew "in reality it would take a lot longer". In a talk at LCA 2018 in Sydney, Packard reported back on the progress he has made; most of it is now in the upstream kernel.
Packard has been consulting for Valve, which is a game technology company, to add support for head-mounted displays to Linux. Those displays have an inertial measurement unit (IMU) for position and orientation tracking and a display with some optics. The display is about 2Kx1K pixels in the hardware he is working with; that is split in half for each eye. The displays also have a "bunch of lenses", which makes them "more complicated than you would hope".
The display is meant to block out the real world and to make users believe they inhabit the virtual reality. "It's great if you want to stumble into walls, chairs, and tables." Nearly all of the audience indicated they had used a virtual reality headset, leading Packard to hyperbolically proclaim that he is the last person in the universe to obtain one.
Display
There is a lot of computation that needs to be done in order to display a scene on the headset. The left and right eye buffers must be computed separately by the application. Those buffers must undergo an optical transform to compensate for the distortion introduced by the lenses. In addition, the OLED display has a different intensity for every pixel, which produces highly visible artifacts when moving an image across the display as the head moves. So his hardware has a table that is used to alter the intensities before the buffer is displayed. A regular Linux desktop is not suitable to be shown on a virtual reality headset; these devices should inherently be separate from the set of normal desktop display outputs.
Displaying to one of these head-mounted devices has hard realtime requirements. If the scene does not track the head motion perfectly, users will "get violently ill". Every frame must be displayed perfectly, both from a pixel-perfect perspective and that they must be delivered on time. The slowest frame rate is 90 frames per second, which is 50% faster than the usual desktop frame rate; the application "must hit every frame" or problems will occur. If the device simply presents a static image that does not change with head position, which can happen during debugging, it can cause the wearer to fall down if they are standing or fall out of their chair if they are not.
He and Airlie discussed a few different possibilities for supporting these displays. Most required changes to the desktops, X, the kernel, or some combination of them. All except one would have serious latency problems because X is not a hard realtime environment; that could lead to missed frames or other glitches. X is kind of a "best effort" display manager, Packard said; you give it a frame and it will display it sometime in the future. These displays need more than that.
The idea they came up with was to allow applications to "borrow" a display and get X out of the way—because "it's not helping". It adds latency and the application doesn't need any of the services it provides. By hacking the kernel and X (something that he and Airlie are well qualified to do, Packard said), they could provide a way for X to talk to some displays and for applications to talk to others without the two interacting at all.
Direct rendering manager (DRM) clients already talk directly to the kernel without going through X, he said. Those clients have a file descriptor to use for that communication; either those descriptors could be made more capable or a new kind of file descriptor could be added. Beyond rendering, the applications need to be able to do mode setting, which is a privileged operation that is normally mediated by the window system (e.g. X). What is needed is a way to pull the display away from X temporarily, so that the head-mounted display application has full control of it. But there is also a need for X to be able to recover the display if the application crashes.
Leasing
So they came up with the idea of a "lease". The application would not own the device, but would have limited access for a limited amount of time. The lessor (e.g. X server) promises not to "come in and bug the application while it is busy displaying stuff"; it gives the application free rein on the display. The lessee (application) can set modes, flip buffers, and turn the display on or off for power saving; when the lease terminates (or the lessee does), the lessor can clean up.
When doing new things in the kernel, it often leads to getting sidetracked into a series of "yak-shaving exercises", Packard said. One of those came about when he was trying to change the kernel frame counter from 32-bit to 64-bit. The current vertical blank (VBLANK) API is a mess; there is a single ioctl() command that is used for three separate functions. It only supports a 32-bit frame counter that will wrap in a few years, which means that it cannot be treated as a unique value. A multi-year test-debug cycle is not conducive to finding and fixing any bugs that result from the wrap, so he was hesitant to rely on it. It also only supported microsecond resolution, which was insufficient for his needs.
So he added two new ioctl() commands, one that would retrieve the frame counter and another to queue an event to be delivered on a particular count. Those counts are now 64-bit quantities with nanosecond resolution. He got sidetracked into solving this problem and "it took a surprising amount of time to integrate this into the kernel". It was such a small change to the API and "the specification is really clear and easy to read", so that meant that it got bikeshedded, which was frustrating. He thought it would be quick and easy to do, but "it took like three months". "Welcome to kernel development", he added.
For DRM leasing, he created three patch sets to "tell the story" of the changes he wanted to make. The first chunk simply changed the internal APIs so that the hooks he needed were available; it made no functional change to the kernel. The second was the core of his changes; it added lots of code but didn't touch other parts of the system. That allowed it to be reviewed and merged without actually changing how the kernel functioned. Only with the last chunk, which exposed the new functionality to user space, does the kernel API change. Breaking the patches up this way makes it easier for maintainers to review, Packard said.
Two different kinds of objects can be leased: connectors, which correspond to the physical output connectors, and CRTCs, which are the scan-out engines. Packard noted that CRTC stands for "cathode-ray tube controller" but that few people actually have a cathode-ray tube display any more. In fact, the only person he knows that does still have one plays a competitive video game; the one-frame latency introduced by using an LCD display evidently interferes with optimal game playing. "Wow!"
He also added some new ioctl() commands to allow applications to lease a connector and CRTC. The lessee can only see and manipulate the connector and CRTC resources the lease has been granted for; only the DRM master (e.g. X server) has access to the full set. The application only needs to change in one place: instead of opening the graphics device, it should request a lease. Once the lease is granted, it will only see the resources that it has leased, so nothing else in the application needs to change, he said.
Demo time
He then demonstrated the code using an application he wrote (xlease) that gets a lease from the X server, starts a new X server, and tells the new server to use the leased device for its output. That came up with an xterm, but he could not actually type in that terminal window because the second X server had no input devices. He then ran the venerable x11perf 2D-performance test in a second window. He killed xlease, which killed the second X server; the original X server was able to recover and redisplay his desktop at that point.
The second X server was running as a normal, unprivileged user because it does not require special privileges to open the graphics device as the regular X server does. That could lead to a way to solve the longstanding request for multi-seat X. He has a graphics card with four outputs, for example, and X already handles multiple input devices, so it should be straightforward to set up true multi-seat environments. Each display would have its own X server and input devices simply by changing configuration files, he said.
The master X server can still see all of the resources, including those that have been leased out. It could still mess with the output on the leased devices; it has simply agreed not to do so, there is no enforcement of that by the kernel. But desktops, such as GNOME or KDE/Plasma, would also be able to mess with those displays. So the X server effectively hides the leased resources from other clients; it simply pretends that there is nothing connected while the lease is active.
In an aside, Packard noted that he had seen patches to add leasing to the Wayland protocol the day before. There was no code yet, but "somebody is starting to think about how this might work in a Wayland environment, which is pretty cool".
All of the leasing mechanism sits atop an ability that was added for direct rendering infrastructure (DRI) 3: passing file descriptors from the server to X applications. Passing file descriptors via Unix-domain sockets has been possible for a long time, but was never used in X. DRI 2 had some "hokey mechanism" that was replaced by file-descriptor passing when DRI 3 came about. That required a bunch of changes and infrastructure in the X server that made this leasing work "a piece of cake", he said. The lease is represented as a file descriptor that can be passed to an application; using file descriptors that way is a powerful primitive that he thinks we will be seeing more of for X in the future.
There was a pile of minor fixes that he needed to make in X to support leasing. For example, the cursor needed to be disabled when the CRTC was so that the lessee doesn't end up with a random cursor on the screen. Similarly, the cursor needed to be restored to a known state when the lease terminates.
He has also done some work to provide leasing support for Vulkan. His original plan was to create a Vulkan extension that would be passed the file descriptor for it to use, but that ran aground on the rather heavyweight Vulkan standardization process for extensions. NVIDIA has an extension that does something related and he was able to modify that to handle leasing while still allowing it to be used for its original purpose with the binary-only NVIDIA driver.
He concluded the talk by saying that virtual reality on Linux is "working great and should be coming to your desktop soon". The kernel changes were merged for 4.15, while the X changes will be coming in xserver 1.20. The YouTube video of the talk is available for those interested.
[I would like to thank LWN's travel sponsor, the Linux Foundation, for
travel assistance to Sydney for LCA.]
Index entries for this article | |
---|---|
Conference | linux.conf.au/2018 |
Posted Mar 8, 2018 6:50 UTC (Thu)
by kalvdans (subscriber, #82065)
[Link]
[1] https://community.metavision.com/t/usb-hardware-documenta...
Posted Mar 8, 2018 9:31 UTC (Thu)
by tdz (subscriber, #58733)
[Link] (16 responses)
I'm surprised that this work was done on X. I somehow expected that X was on its way out and that all new features would be implemented on Wayland first.
Posted Mar 8, 2018 15:11 UTC (Thu)
by daniels (subscriber, #16193)
[Link]
Posted Mar 8, 2018 19:56 UTC (Thu)
by keithp (subscriber, #5140)
[Link]
Posted Mar 9, 2018 16:00 UTC (Fri)
by fratti (guest, #105722)
[Link] (13 responses)
Posted Mar 9, 2018 18:32 UTC (Fri)
by rahulsundaram (subscriber, #21946)
[Link]
Depends on how you use your desktop.
Posted Mar 9, 2018 19:44 UTC (Fri)
by zlynx (guest, #2285)
[Link] (6 responses)
Give Gnome on Fedora a try. If you have an AMD or Intel GPU Wayland will work great. I'm not sure where their binary Nvidia support is at, and I haven't tried it with Nouveau.
Posted Mar 15, 2018 4:40 UTC (Thu)
by gwg (guest, #20811)
[Link] (3 responses)
For some peoples value of "very well". For those who need accurate color on the other hand, not so well at all ...
Posted Mar 17, 2018 17:20 UTC (Sat)
by Hi-Angel (guest, #110915)
[Link] (2 responses)
Posted Mar 17, 2018 17:35 UTC (Sat)
by zdzichu (subscriber, #17118)
[Link] (1 responses)
Posted Mar 17, 2018 18:41 UTC (Sat)
by madscientist (subscriber, #16861)
[Link]
After primary/secondary selection, for me the missing parts are per-app (not full screen) remote window display and NVidia support (when I tried to use GNOME on Wayland in Ubuntu 17.10 with my NVidia GeForce 8400GS (I know it's old but all I do is programming, not gaming--it's good enough to run two monitors) the video seemed jerky and I had problems with some apps--of course now I can't remember what they were. Remmina? Videos in Firefox? Something I needed).
I know it wasn't just my imagination because I honestly didn't realize that my desktop had been set to use wayland by default when I upgraded from Ubuntu GNOME 17.04. It wasn't until I noticed these issues and went poking around that I found this setting; changing back to GNOME on X solved my problems.
Posted Mar 20, 2018 12:43 UTC (Tue)
by cortana (subscriber, #24596)
[Link] (1 responses)
Posted Mar 20, 2018 16:43 UTC (Tue)
by zlynx (guest, #2285)
[Link]
Not great for Linux currently, but in a world where the drivers worked for everyone it would be great.
By connecting the high power discrete card to the external port, it removes the copy latency Optimus has and does it in the case where the laptop is most likely plugged in. It also allows the Nvidia card to directly control the port, which means G-Sync is possible. I don't know if they enabled it, but it is possible.
Posted Mar 9, 2018 21:07 UTC (Fri)
by k8to (guest, #15413)
[Link] (2 responses)
Everything worked fine, and I did my job in an officeplace with a variety of applications, though a lot of the stuff was of course browser based, as things are these days. But slack, various IDEs etc all just worked.
What problems remain?
Posted Mar 9, 2018 22:34 UTC (Fri)
by fratti (guest, #105722)
[Link] (1 responses)
Also, the last time I've tried KDE Plasma 5.12 with Wayland, my first experience was that the touchpad behaved all wrong, and the DPI was wrong as well, and the only option to "change" it was so bitmap scale the entire output by 2x which is unacceptable. Then something crashed when I tried to set the scaling back to 1x and change the font sizes instead.
Plus I don't know whether wayland even has an equivalent to xrandr to switch refresh rates during runtime, and as far as I know they still don't inform the clients about when a frame was presented so mpv's video-sync=display-resample doesn't work on it as far as I know.
If I actually used Wayland compositors regularly I'd probably find a lot more wrong with them, but all criticism of Wayland is deflected with the excuse that it's clearly not part of the core protocol and therefore not at all a wayland problem, and if applications such as games wanted title bars they should just relinquish the control over presentation to a toolkit such as Gtk, and that atomic scanout is going to be a thing Real Soon™.
Posted Mar 10, 2018 19:06 UTC (Sat)
by fratti (guest, #105722)
[Link]
Looks like I need to make some corrections to this comment:
Posted Mar 10, 2018 17:09 UTC (Sat)
by tdz (subscriber, #58733)
[Link] (1 responses)
Posted Mar 10, 2018 18:58 UTC (Sat)
by fratti (guest, #105722)
[Link]
Posted Mar 8, 2018 19:21 UTC (Thu)
by mki_n (guest, #122987)
[Link]
Posted Mar 8, 2018 20:22 UTC (Thu)
by CCrob (guest, #113134)
[Link] (1 responses)
Posted Mar 9, 2018 6:32 UTC (Fri)
by Plagman (guest, #98902)
[Link]
Posted Mar 9, 2018 15:44 UTC (Fri)
by excors (subscriber, #95769)
[Link] (6 responses)
Not really relevant to the article but I feel like mentioning it anyway: Good VR headsets don't just rely on the IMU for tracking. Vive requires you to put two 'lighthouse' boxes in high corners of your room, which emit IR pulses and beams that sweep across the room horizontally and vertically. Multiple IR sensors on the headset record the times when the beams reach them. The timing differences between each sensor, combined with the fixed position of each sensor relative to the headset, and some initial calibration, let you determine the absolute position and orientation of the headset. If I remember correctly they say it's accurate to about 1mm at a range of up to 5m, and it updates at 60Hz, which seems pretty decent.
> the one-frame latency introduced by using an LCD display evidently interferes with optimal game playing.
Hmm, I'm not sure about the "evidently" - I think a lot of competitive gamers' hardware choices are based more on superstition than evidence.
Posted Mar 9, 2018 19:48 UTC (Fri)
by mathstuf (subscriber, #69389)
[Link] (5 responses)
While I do tend to agree in general, not all games run at 60 Hz. A one-frame delay may be (more realistically) noticeable on a 24 Hz or maybe even a 30 Hz game.
Posted Mar 9, 2018 22:20 UTC (Fri)
by excors (subscriber, #95769)
[Link] (1 responses)
Posted Mar 12, 2018 19:43 UTC (Mon)
by hkario (subscriber, #94864)
[Link]
Posted Mar 9, 2018 22:27 UTC (Fri)
by flussence (guest, #85566)
[Link] (2 responses)
Posted Mar 9, 2018 22:53 UTC (Fri)
by zlynx (guest, #2285)
[Link] (1 responses)
For one thing, if a CRT wasn't designed for 75 Hz refresh, its phosphors will have too much persistence and it'll smear. On the other hand, with too short persistence and too low a refresh it'll give you a headache from flicker.
And there's the "infinite resolution" myth. Nope. Display masks and dot pitch set very firm limits on where and how many pixels a CRT has.
And the "one frame delay" of LCD and the "super low latency" of CRT thing relies on using the CRT with no vertical sync. Which yes, some gamers did, but it was and still is ugly. And anyway, it only really applied to LCDs with a VGA connection where it had to process the analog signal into pixels. Most LCD gaming displays will set pixels as they come in over DP or HDMI. There really isn't a processing delay at all.
Posted Mar 10, 2018 18:38 UTC (Sat)
by jem (subscriber, #24231)
[Link]
In theory, a color CRT has an uninterrupted phosphor layer just like a monochrome tube. In a color tube the phospor layer alternates between red, green and blue areas, which limits the resolution of the color component. The chrominance resolution is not limited by dot size: the signal can vary within a color dot, limited by the sharpness of the electron beamand the bandwidth of the video signal.
In practice, the dots have borders which affect picure quality, and the vertical resolution is of course limited by the discrete scan lines.
Posted Mar 9, 2018 22:49 UTC (Fri)
by flussence (guest, #85566)
[Link]
Posted Mar 13, 2018 14:33 UTC (Tue)
by JFlorian (guest, #49650)
[Link] (1 responses)
Note to Keith, you're not the last person in the universe; I still don't have a VR display nor have I even ever used one. I wear glasses, primarily for astigmatism so going w/o isn't an option for me (that I know of). I'm still utterly baffled how anybody can focus on something so close to their eyes. Anyone with pointers to interesting reading about how this works would be most welcome.
Oh and for some hi-tech nostalgia, I still have a 20" Hitachi CRT that can do 1600x1200 @ 85 Hz. I had to author my own modelines for the first 3~5 years. It's stunning for what it is, why is why I can't let it go, though it's wasted in my micro-datacenter for KVM use. I'd never even considered that anyone (e.g., gamers) might covet such a thing. I'd hate to see the bill for shipping it though!
Posted Mar 14, 2018 1:35 UTC (Wed)
by excors (subscriber, #95769)
[Link]
(I get minor problems if I pick up a virtual object and hold it close to my face (like <20cm), because the parallax is telling my brain that the object is close and so my eyes should focus nearer, but then the displays are still at effectively ~1m and are no longer in focus. If I close one eye then it removes the conflicting signals and I can focus fine again.)
Posted Mar 16, 2018 6:23 UTC (Fri)
by Kamilion (subscriber, #42576)
[Link] (1 responses)
Finallllllly.
They get it. It took. YEARS.
https://lists.freedesktop.org/archives/wayland-devel/2015...
wacom tablet joystick buttons? yes! Real joysticks and gamepads? NO!
This has been going on for ages, even before I chimed in.
https://lists.freedesktop.org/archives/wayland-devel/2015...
In 2017 it was still a huge problem.
https://www.reddit.com/r/linux_gaming/comments/5xics8/a_b...
As far as I know, it's *still* a problem even outside of VR. And Packard and Arlie are still focused on the display side of things.
Please please please give input some love while you're fixing the kernel up for VR, guys...
This is one of the things you seriously need to get right for gamers, even the most casual 2D platform jumper is unplayable with terrible controls and random latency.
https://www.youtube.com/watch?v=YZ8GuuCwFAw
See how unplayable even something like mario is with poor touch control.
Input is even more important in VR; so the whole pipeline has to do it right.
Posted Mar 16, 2018 7:54 UTC (Fri)
by mpr22 (subscriber, #60784)
[Link]
Open IMUs?
Supporting virtual reality displays in Linux
Supporting virtual reality displays in Linux
Supporting virtual reality displays in Linux
Supporting virtual reality displays in Linux
Supporting virtual reality displays in Linux
Supporting virtual reality displays in Linux
Supporting virtual reality displays in Linux
Supporting virtual reality displays in Linux
Supporting virtual reality displays in Linux
Any real blockers?
Supporting virtual reality displays in Linux
Supporting virtual reality displays in Linux
Supporting virtual reality displays in Linux
Supporting virtual reality displays in Linux
Supporting virtual reality displays in Linux
Supporting virtual reality displays in Linux
Phew, sounds like I need to try wayland again soon.
Supporting virtual reality displays in Linux
Supporting virtual reality displays in Linux
Supporting virtual reality displays in Linux
Supporting virtual reality displays in Linux
Supporting virtual reality displays in Linux
Supporting virtual reality displays in Linux
Supporting virtual reality displays in Linux
Supporting virtual reality displays in Linux
Supporting virtual reality displays in Linux
Supporting virtual reality displays in Linux
Supporting virtual reality displays in Linux
Supporting virtual reality displays in Linux
Supporting virtual reality displays in Linux
Supporting virtual reality displays in Linux
Supporting virtual reality displays in Linux
Supporting virtual reality displays in Linux
As far as I can tell, my wired XBox 360 controller works perfectly with every controller-supporting game I've tried it with on Linux (up to the limits of the control scheme; Skullgirls is really designed for an arcade-style joystick, not a standard console controller). I never got killed by controller latency when playing They Bleed Pixels, for instance. Brain and muscle latency? Sure, TBP is pretty merciless even on the added-after-release easy mode. Controller or display latency? Nope.
Supporting virtual reality displays in Linux