GLSL Syntax Highlighting for OpenGL 4.4
The syntax highlighting definitions for QT Creator, KDE Kate, TextWrangler and BBEdit have been updated for OpenGL 4.4 (GLSL 440). The update includes a fix of one typo and highlight the noise functions in QT Creator as not available ( e.g.
noise4(); ) as those are defined as deprecated beginning with GLSL 440 and are now defined to always return 0 – so don’t even try to use them anymore ;-) If you need a noise function in GLSL, take a look at webgl-noise from ashima or upload a texture with precalculated noise.
QTCreator (also works for Kate):
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 this (unzipped) glsl.xml file there.
- Restart QTCreator.
The definition has the following features:
- Support for desktop OpenGL up to 4.4 (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.
TextWrangler and BBEdit:
It is much simpler than the Kate/QTCreator highlighting and only highlights GLSL version 440 (core profile) but it’s better than nothing (In case you are writing shaders for OpenGL ES 3.0, or OpenGL 3.0 – 4.3 it just means that reserved keywords are highlighted as well. In case your shaders are WebGL, OpenGL ES 2.0 or OpenGL 2.x, some (now deprecated) keywords won’t get highlighted).
To install it, download the glsl440.plist (unzip it) and drop it into your ~/Library/Application Support/TextWrangler/Language Modules folder. After restarting TextWrangler, all .glsl .fsh .vsh .gsh .tcsh .tesh .csh .frag .vert and .geo files should get the correct highlighting. In case you prefer other extensions, you can add or remove extensions from the beginning of the plist (just edit it in a text editor) or change the settings in TextWrangler under Preferences->Languages->Custom Extension Mappings.