GLSL Syntax Highlighting for QTCreator (and Kate)
The syntax highlighter in QTCreator for GLSL shaders is currently far from ideal: It’s basically stuck at OpenGL 2.1. If you write shaders for OpenGL 3 (and later) or OpenGL ES 3, you will see a lot of red marks indicating errors in your code simply because QTCreator doesn’t know the latest keywords. On the other hand, nothing indicates deprecated keywords and it can’t distinguish between the different GLSL variants.
So I was sick of waiting for better support and wrote a solution: The GLSL syntax checker in QTCreator is build-in, so it has to be deactivated first, then a syntax definition XML file in the format also used for Kate (and probably other KDE apps) can be used to define proper and modern GLSL:
- Go to Preferences->Environment->MIME-Types in QTCreator and look for all types associated with GLSL (application/x-glsl). The handler can’t be changed, but the file endings – just remove them. Don’t forget the MIME-Types text/x-glsl-es-frag, text/x-glsl-es-geometry, text/x-glsl-es-vert, text/x-glsl-frag and text/x-glsl-vert!
- Add a new language syntax definition, look at Preferences->Text Editor->Generic Highlighter, you will see a location where QTCreator expects the XML file (~/.config/Nokia/qtcreator/generic-highlighter on OS X and Linux). Place the this glsl.xml file there.
- Restart QTCreator.
The definition has the following features:
- Support for desktop OpenGL up to 4.3 (with all keywords, build-in functions, constants and variables), as well as OpenGL ES 2.0 and OpenGL ES 3.0 (and WebGL as it uses ES 2 shaders).
- Detects the shader version per file based on the #version definition!
- Unsupported keywords, build-in variables and functions are marked as such based on the detected GLSL version (see gif below).
- Marks the ‘special parameters’ inside of layout() declarations based on the GLSL version (those are not keywords according to the specs so they are not marked outside of a layout() declaration).
See this comparison of the same (non functional) dummy code to get an impression:
This highlighting does not perform a full syntax check and assumes core profile code on GLSL 1.30 and later. The detected file endings are: *.glsl; *.vsh; *.vert; *.tcsh; *.tcs; *.tesh; *.tes; *.gsh; *.geo; *.geom; *.fsh; *.frag; *.csh; *.cs, but this list can easily get changed in line 4 of the XML. The highlighting style can get adjusted beginning at line 1884 (always restart QTCreator after changing the XML).
The current limitations are:
- The shader type itself (Compute Shader, Geometry Shader etc.) does not get detected – it would be possible to create six different XML files with slightly different rules to only mark keywords useful/supported per shader stage (in case the file ending is sufficient to make this distinction).
- Version 130 and later assume core and mark compatibility build-ins as unsupported.
- In #version 300 es for OpenGL ES 3, only one space between the ’300′ and the ‘es’ are allowed.
A description of the XML format can be found here.
The glsl.xml version 4.30 (the version number mimics the latest supported OpenGL version).
Update 12/1/12: In case you prefer TextWrangler/BBEdit, there’s a (simpler) solution for that as well.
Update 12/17/12: A few GLSL 4.20 build-in functions were defined as 4.30 functions (e.g. imageStore).
Update: A newer version can be found here.