How to fix Nvidia vsync on Linux with proprietary drivers

Tearings and lags in any Desktop Environment are the main problem when it comes to Linux Desktop and holds it back from growing. In this tutorial, Installation, Configuration to enable VSync and fixing tearings in any Linux Distro will be shown with details.

Table Of Contents:
1. Installing the nvidia driver
2. Enabling “Force Full Composition Pipeline” With nvidia-settings
3. Disabling no-ubb
4. Disabling Allow Flipping and Sync to VBlank with Command Line or GUI
5. Fixing Performance decrease and lagging in games.

1. Installing the Nvidia Driver

I used different Debian distributions and this is the common solution to almost every common Linux distro.
Most Linux distributions come with the xserver-xorg-video-nouveau package pre installed. It’s basically the reverse engineered version on Nvidia’s proprietary(closed source) driver. nouveau is opensource but has problems when it comes to compositing and doing heavy tasks.

There are two ways to install the drivers: Ubuntu repositories or Nvidia’s website. Here’s how to install the driver from nvidia’s websiteThe latter one is recommended the most; but for the sake of this tutorial, the first one is used :

The first step is to add the Nvidia proprietary drivers’ repository with

sudo add-apt-repository ppa:graphics-drivers

and then updating the package manager with

sudo apt-get update

And finally installing it.
Right now, as of 19th of September 2018, the latest driver is the nvidia-driver-396 package. it can be installed with the following command:

sudo apt-get install nvidia-driver-396.

Or you can download and install the driver

*IMPORTANT*

If you have problems installing the driver regarding blacklisting the nouveau kernel module; do the following:

sudo bash -c 'echo -e "blacklist nouveau \n options nouveau modeset=0" > /etc/modprobe.d/blacklist-nouveau.conf'

Then update the “initial ram file system” file for the blacklisting to take place:

sudo update-initramfs -u

then reboot for it to take effect:

sudo reboot

A reboot is a must after the installation.

2. Enabling “Force Full Composition Pipeline” With nvidia-settings

After the reboot, a program called “NVIDIA X Server Settings” is installed with a simple GUI, the command line is sudo nvidia-settings(Because you can’t save to /etc/X11/xorg.conf without root permissions). For the sake of startup process and ease of use, I’ll use the GUI.
First, head over to the ‘X Server Display Configuration’ section, there are a few things which need to be altered; First, choose your resolution then press on the Advanced button in the bottom and check the "Force Full Composition Pipeline" option.
Then click the "Save to X Configuration File" button to make this permanent.
what you'll face in nvidia xserver settings
This option is also available from the command line using

nvidia-settings --assign CurrentMetaMode="nvidia-auto-select +0+0 { ForceFullCompositionPipeline = On }"

You have to make this an startup command using a bash script, i prefer using the GUI and saving the ‘X Configuration File’ in the /etc/X11/xorg.conf file so the config will start with startup. Do this with the Save to X Configuration File option in the above picture.

If you don’t have access to the nvidia-settings software; You can just edit the /etc/X11/xorg.conf file and add the metamodes line to the Screen Section.

*IMPORTANT*
If you’re getting errors while trying to Save to X Configuration File; execute nvidia-settings as root:

sudo nvidia-settings

Now click that button and save the settings.

3. Disabling no-ubb

And another thing is to disable UBB as root:

sudo nvidia-xconfig --no-ubb

This will automatically edit the /etc/X11/xorg.conf file with the no UBB option.

OVERALL, the configuration file should look like(Only add the red line to the “Screen” Section):

Section "Screen"
    Identifier     "Screen0"
    Device         "Device0"
    Monitor        "Monitor0"
    DefaultDepth    24
    Option         "AllowEmptyInitialConfiguration" "True"
    Option         "Coolbits" "28"
    Option         "Stereo" "0"
    Option         "nvidiaXineramaInfoOrder" "DFP-4"
    Option         "metamodes" "1920x1080_60 +0+0 {ForceCompositionPipeline=On}"
    Option         "SLI" "Off"
    Option         "MultiGPU" "Off"
    Option         "BaseMosaic" "off"
    Option         "UBB" "False"
    SubSection     "Display"
        Depth       24
    EndSubSection
EndSection

 

3. Disabling Allow Flipping and Sync to VBlank with Command Line or GUI

I myself had problems with some default settings that Nvidia set for OpenGL; they even document this issue but there never seems to be a patch to fix this out of the box. To do so, go into OpenGL Settings and disable Allow Flipping and Sync to VBlank:
Uncheck OpenGL flipping and SynctoVBlank

To set this with A command line for startup purposes, etc. :

/usr/bin/nvidia-settings -a 'SyncToVBlank=0'
/usr/bin/nvidia-settings -a 'AllowFlipping=0'

 

*IMPORTANT*

Now that’s taken care of, we have to enable compositing through the compizconfig application from the store or with. This is only if you’re using ubuntu with Gnome.

sudo apt-get install compizconfig-settings-manager

command and then entering its GUI. We only have to enable ‘Compositing’ and set the refresh rate to what you saved in nvidia x server settings.
options you must change in compizconfig
remember to uncheck the "Detect Refresh Rate" option and set it manually.

After the "Force Full Composition PipeLine" option is it, one thing is lacking; Games don’t have any tearing even with their VSync options turned off. Which yields input lag and FPS capped at the monitor’s refresh rate. To fix this, the "Force Full Composition PipeLine" needs to be turned off while in game. To do this automatically and not having to turn it on and off ourselves. I wrote a script to help with this:

5. Fixing Performance decrease and lagging in games.

PROGS=('csgo_linux64' 'hl2_linux' 'hl_linux' 'RocketLeague' 'rust' 'GTA5.exe')
while sleep 1; do  
       for p in ${PROGS[@]}; do 
               if [[ -n $(pgrep $p) ]]; then 
                       nvidia-settings --assign CurrentMetaMode="nvidia-auto-select +0+0 { ForceFullCompositionPipeline = Off }" 
                       while [[ -n $(pgrep $p) ]]; do 
                               sleep 5 
                       done 
                       nvidia-settings --assign CurrentMetaMode="nvidia-auto-select +0+0 { ForceFullCompositionPipeline = On }" 
               fi 
       done 
done

All this script does is to check if the processes in the PROGS array are running; If any of them are running; turns the "Force Full Composition PipeLine" option off; then while it’s running, sleep for 5 seconds  to keep the if condition alive and prevent overloading the cpu; Then after the process is no longer running, turns the "Force Full Composition PipeLine" option on to make the desktop tear-free; this will go on and on forever and is fault tolerant. You can add this to a startup job using systemd or /etc/rc.local. Google can help on that.
You can add other processes in the array with any other process name you don’t want "Force Full Composition PipeLine" to be run with it.

Now good luck playing any game you want!

4
Leave a Reply

avatar
2 Comment threads
2 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
4 Comment authors
shayanthethieffrupshayanthethiefJimmy Recent comment authors
  Subscribe  
Notify of
Jimmy
Guest

Ahaa, its good discussion on the topic of this piece of writing here at this web site, I have read all that,
so now me also commenting here. Does your blog have a contact
page? I’m having problems locating it but, I’d like to shoot you an email.

I’ve got some suggestions for your blog you might be
interested in hearing. Either way, great site and I look forward to seeing it expand over time.
I couldn’t refrain from commenting. Very well written! http://cspan.org/

frup
Guest
frup

You know you can just set an Application Profile in Nvidia X Server Settings to enable and disable things per applications, there’s no need to write a script for it. Create a Profile, and then add your Rule and select the Application to execute for.