Fixing Linux OpenGL tearing on NVidia GTX 580

25 December 2011
I originally found the solution by watching part of a YouTube video, but as with a lot of Linux how-to material, it was not quite up to date. I also hate resorting to watching videos for technical documentation, as sometimes I need to refer to them at work where I don't have audio output (since the company banned listening of music, I no longer keep headphones there). If I find solutions for other chip-sets (likely as I am currently experimenting with Intel H67's), I might update this article..

Symptoms

Tearing is most noticeable when the scene is moving sideways on systems with a very high FPS, and it looks like the upper part of the screen has shifted whereas the lower part has not. What is happening is that some scan lines have been updated whereas others have not, because the graphics card has decided to update the video signal halfway-through drawing it. In my case it was very noticeable because Doomsday Engine (Doom2 updated to use OpenGL basically) on a quad-core i7 equipped with a GTX 580 was quite likely chalking up an FPS somewhere in the several hundreds, whereas the display refresh rate was circa 60Hz. As a result I was seeing several tear-lines. With more intense games (such as Doom 3) there were far fewer tear-lines, but they were still there.

Solution overview

What you need to do is enable Sync to VBlank, which basically means that the scene is updated between screen refresh cycles rather than during them (VBlank is a slightly archaic term from CRT days when a small delay was required to allow the electron beam to travel from the bottom-right to top-left corners of the display) the problem is that there are usually multiple such settings, and it is often not clear which one takes precedence. What caught me out is that the driver's setting was taking precedence, and more importantly, this setting is maintained separately got OpenGL applications.

NVidia GTX 580 under Linux

I happened to be using Ubuntu 10.04, but since even the "propriety" Ubuntu drivers did not work, I was using the distribution-independent drivers available off Nvidia's website. As a result these instructions should be distribution-independent. Run nvidia-setting (for some reason, it did not need root permissions on my system), and look under the following tree-list items:
X Screen 0
X Server XVideo Settings
Under Video Texure Adapter, make sure Sync to VBlank is set.
OpenGL Settings
Under Performance, make sure Sync to VBlank is set. I am not sure if Allow Flipping has any effect, so I left it set.
I happened to be using v1.27 of nvidia-setting (aka NVIDIA X Server Settings), but from what I recall the interface changes very little between versions. If you have multiple monitors, there may be other entries such as X Screen 1. Since at time of writing I only have one physical display available, I can only take guesses of whether Sync to VBlank needs to be set individually for all displays.