This is the 12th day of my participation in the August Challenge

Use the Nsight graphics debugger

Debugging GLSL shader code is notoriously difficult. Unlike programming in typical languages such as C++ or Java, it is often unclear where a shader fails. Typically, a shader error appears as a blank screen, giving no clue as to the nature of the error. Even more frustrating, there is no way to print out the value of a shader variable at run time, as one usually does when tracking an elusive bug. The lack of a simple ability to display shader variables is a serious handicap, and for this reason, graphics card manufacturers sometimes provide capabilities in their hardware to extract information from shaders at run time, and have built tools to access that information in the form of graphical debuggers. Each manufacturer’s debugging tool can only work in the presence of that manufacturer’s graphics card. NVIDIA’s graphics debugger is part of a larger tool suite called Nsight, and AMD has a similar tool suite called CodeXL.

About Nvidia Nsight

Nsight is a set of NVIDIA tools that includes a graphics debugger that allows you to view various stages of the OpenGL graphics pipeline, including shaders, while the program is running. No code needs to be changed or added. Simply run existing programs with Nsight enabled. Nsight allows you to examine shaders at run time, such as viewing the current contents of the shader’s uniform variables. Some versions even allow shader code to be changed at run time.

There are versions of Nsight for Windows and Linux/MacOS that interact with Microsoft’s Visual Studio (VS) and Eclipse IDE. We will only talk about Windows and Visual Studio.

Nsight is only compatible with NVIDIA graphics cards; It doesn’t work with Intel or AMD graphics cards. A complete list of support cards can be found on the NVIDIA website

Nsight is changing rapidly and there are likely to be more exciting changes and developments in Nsight

Set the Nsight

There are several ways to set up Nsight and integrate it into Visual Studio. Depending on what version you have installed, the Nsight menu will be added to the top VS menu or as a submenu of the “Extensions” menu. Here’s how we installed Nsight:

1. Install NVIDIA Nsight Graphics, url for developer.nvidia.com/nsight-grap… We installed version 2020.4.

2. In Visual Studio, use Extensions►Manage Extensions to search for and install Nsight integration. The “Nsight” menu should appear in the top menu bar or submenu under the extension.

Run C++ / OpenGL applications in Nsight

  1. Load the program you want to run. From the “Nsight” menu, select “Frame Debugger” or “Start Graphics Debugging” (depending on your version and installation) to run your program, as shown below:

  1. A window may pop up asking if you want “Secure Connection free?” If so, then click “Connection is not secure”. A window may also appear showing the connection to Nsight and asking if you want to start the frame debugger. If so, click “Launch Frame Debugger”, as shown below:

  1. You should then execute the C++ /OpenGL graphics program. Depending on your installation, various Windows may appear next to the program you are running. Nsight can also superimpose information on an application you’re running, like this:

  1. After the program runs, interact with it in any areas you wish to examine. At this point, you need to pause execution. In some installations this is done by selecting “Pause and capture Frames” from the Nsight menu, while in other versions this is done by pressing CTRL+Z in the run window and then clicking the “Pause for Live Analysis” button.

  1. There should be a Frame debugger screen, along with a HUD toolbar and a horizontal selection tool called “ScRubber.” Your program may freeze at this point. In the center of the debugger screen is a left bar with buttons for each shader stage. For example, you can highlight “VS” for “Vertex Shader,” and in the larger center box to the right, you can scroll down and see the contents of the uniform variable (assuming you select “API Inspector” on it). In the image below, the small box to the right of “proj_matrix” has been opened to show the contents of the 4×4 projection matrix

  1. Another interesting window appears similar to the running program. There is a timeline at the bottom of the window that you can click to see the sequence of items drawn on the frame. Here’s an example. Notice that the cursor has clicked on the left side of the timeline and it will display those items to that point

For more information on how to get the most out of Nsight tools, see the Nsight documentation.