A simple pixel shader viewer

In a previous post, I wrote about Vkrunner, and how I used it to play with fragment shaders. While I was writing the shaders for it, I had to save them, generate a PPM image and display it to see the changes. This render to image/display repetition gave me the idea to write a minimal tool that automatically displays my changes every time I save the file with the shader code and use it when the complexity of the scene is increasing. And so, I’ve written sdrviewer, the minimal OpenGL viewer for pixel shaders of the video below:

Usage:

 
The filename can either be a file containing the GLSL code of a pixel shader, or a vkrunner/piglit *.shader_test that contains a [fragment shader] section from which the pixel shader code will be extracted. If sdrviewer doesn’t find the [fragment shader] section in the input file, it will assume that the whole file is a valid fragment shader written in GLSL. (I didn’t want to spend much time with the parsing and do something more complex here :p).

After the shader code is loaded, sdrviewer will compile and link it and use the generated program to display a 2D quad.

I’ve added some extra options that help me debug the shaders:

  • Pressing b enables and disables the busy loop: Normally, I only render a frame on the screen when there is either an event coming from the xserver or when the file with the shader code is saved. But in order to count the frame rate, I would need to draw every frame, and so I’ve added the option to switch between the two modes.
  • Pressing = or – (the +/- buttons of the us layout), increases or decreases the camera motion sensitivity respectively.
  • Moving the mouse with the left button pressed rotates the camera.
  • Moving the mouse with the right button pressed allows zoom in and zoom out.

I used libresman to watch the text file with the pixel shader code. The library seems to take into account that some editors don’t save the text directly to the file but in a backup that then use to override the original. Nevertheless, sdrviewer sometimes didn’t update the shader when the backup mode was enabled on Vim. The problem was solved by disabling it:
:set nowritebackup before running sdrviewer. 🙂

The code is here: https://github.com/hikiko/sdrviewer

Leave a Reply

Your email address will not be published. Required fields are marked *