GLSL validation Script

![](/content/images/2014/Jun/opengl.jpg)

I've had to write some GSL recently. Quite enjoyable but the I'd set up a pretty awful development loop. Edit the shader, run the game and then deal with any syntax errors the in game compilation phase come up with.

It's a slow way of doing things. So I spent a -little- bit of effort to compile and check errors for this stuff during my build stage. Details on how to do it below.

It's made things a lot quicker and best of all picks up errors that I was currently missing and would likely not work on all OGL drivers.

Here We Go

A bit specific to my project BUT it's been useful for me in this project so thought it may be worth sharing.

Takes a few GLSL files, concatenates them and then uses the excellent glslangValidator to check for any errors. Any errors are translated into a vim friendly output format.

Caveats

  • You need to specify the type of shader you're validing
    • -t vert or frag
    • Geometry, tessellation and other shader types probably work
    • But I haven't tried them :D
  • Forces #version 150 to the top of all files
  • Assumes path of glslangValidator
  • But that's all easy to fix if you want to
  • It's my first bash script
  • So it's probably shit :)

Might add this kind of functionality to glslangValidator, it seems like something that would be generally and genuinely useful.

#!/bin/sh

__ScriptVersion="0.1"

function usage ()
{
		cat <<- EOT

Usage :  $0 [options] [--]

Options:
-t|type [frag|vert]
-h|help       Display this message
-v|version    Display script version

		EOT
}

typ="vert"

while getopts ":h:v:t:" opt
do
case $opt in
	t)
		typ=${OPTARG}
		;;

	h)
		usage; exit 0
		;;

	v) 
		echo "$0 -- Version $__ScriptVersion"; exit 0   ;;

	\?)
		echo -e "\n  Option does not exist : $OPTARG\n"
		usage; exit 1   ;;
esac    # --- end of case ---
done

shift $(($OPTIND-1))


if [ -z "${typ}" ]; then
	echo "You specify a type of either frag or vert"
	exit 10
fi

tmp_file="build/temp.${typ}"

file_num=0

echo "#version 150" > $tmp_file

for f in $@
do
	echo "#line 0 $file_num" >> $tmp_file
	cat $f >> $tmp_file
	((file_num++))
done

err=$(./bin/macosx/glslangValidator $tmp_file)

if [ "$?" = "0" ]; then
	exit 0
else
	file_num=0
	for f in $@
	do
		err=$(echo "$err" | sed -e "s|ERROR: $file_num:|$f:|g")
		err=$(echo "$err" | sed -e "s|WARNING: $file_num:|$f:|g")
		((file_num++))
	done

	echo "$err"
	exit 10
fi