from geometry to pixels

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.

TL;DR: QT & Kate:  – TextWrangler & BBEdit:


Here’s a short intro to the syntax files and hot to install them copied together from my previous posts:

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:

  1. 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!
  2. 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.
  3. 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:

GLSL Syntax Highlighting for modern OpenGL

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).

OpenGL GLSL Syntax Highlighting for TextWrangler

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.


Happy coding!

, , , ,

3 thoughts on “GLSL Syntax Highlighting for OpenGL 4.4
  • James Gangur says:

    Works great, although explicit support for GL_ARB_shading_language_420pack (I’m using that + v330 would be nice. I’ll see if I can add that, and then post it here. Anyway, even without that, it’s still way better than default.

  • I updated the Qt glsl.xml with a few fixes:

    I noticed that identifiers like “s1” would have the “1” in it highlighted, which looked strange to me. I fixed this by adding rules for identifiers.

    I also noticed that the number “0” would not appear highlighted in layout declarations, as opposed to other numbers. I also noticed that hex and octal constants in layouts were not handled, even if it seems like GLSL accepts them. That was fixed by adding rules for octal/hex constants in layout contexts (somehow this fixed the “0” bug too.)

  • Jeremy says:

    Thanks a lot Robert and Nicolas!

    A note: there’s another catch if your shaders use conditional compilation (#if etc.):

    Qt Creator will auto-detect the shader as C++ source because of the “magic header” setting of the “text/x-c++hdr” MIME type.

    Very frustrating (that and the fact that user-defined syntax highlighting should take precedence, not be ignored silently)…

Leave a Reply

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