• Portfolio and blog of sydneyzh.

# Using Flycheck with glslangValidator for syntax checking and SPIR-V compilation from GLSL source code

In the not so long time that I have been working with Vulkan, I adopted the method of scheduling shader compilation right before starting building the program. Syntax checking therefore happened at a rather inconveniently late stage. On the other hand, I haven't successfully spotted a Visual Studio plugin that does GLSL syntax highlight and checking as well as one would expect (at least not for VS2017).

Then I thought why not just use the good old Emacs with Flycheck. A simple Flycheck configuration has been there. At first I thought it would only provide syntax checking. But in order to allow GLSL for Vulkan, one has to add a command option "-V" for the checker,

(flycheck-define-checker glslang-validator)
:command ("glslangValidator" "-V" source "-o" (eval (concat (buffer-file-name) ".spv")))
:error-patterns(...)
:modes glsl-mode)


which will go ahead and output the "spv" file in the same(or specified) directory if the compilation does not encounter any error. Now some build time can gladly be saved.

If it seems too much to check on every edit(Flycheck default behaviour), the following hook can be added so that it only happens when flycheck-mode is enabled and the local buffer is saved:

(add-hook 'glsl-mode-hook (lambda() (setq-local flycheck-check-syntax-automatically '(mode-enabled save))))