3 # This script validates shaders (if successfully compiled) using spirv-val.
4 # It is not meant to preclude the possible addition of the validator to
7 declare -r EXE='../build/install/bin/glslangValidator'
9 # search common locations for spirv-tools: keep first one
10 for toolsdir in '../External/spirv-tools/build/tools' '../../SPIRV-Tools/build/tools/bin' '/usr/local/bin'; do
11 [[ -z "$VAL" && -x "${toolsdir}/spirv-val" ]] && declare -r VAL="${toolsdir}/spirv-val"
12 [[ -z "$DIS" && -x "${toolsdir}/spirv-dis" ]] && declare -r DIS="${toolsdir}/spirv-dis"
15 declare -r gtests='../gtests/Hlsl.FromFile.cpp ../gtests/Spv.FromFile.cpp'
17 declare -r targetenv='vulkan1.0'
19 function fatal() { echo "ERROR: $@"; exit 5; }
24 echo "Usage: $(basename $0) [options...] shaders..."
26 echo " Validates shaders (if successfully compiled) through spirv-val."
28 echo "General options:"
29 echo " --help prints this text"
30 echo " --no-color disables output colorization"
31 echo " --dump-asm dumps all successfully compiled shader assemblies"
32 echo " --dump-val dumps all validation results"
33 echo " --dump-comp dumps all compilation logs"
34 echo "Spam reduction options:"
35 echo " --no-summary disables result summaries"
36 echo " --skip-ok do not print successful validations"
37 echo " --skip-comperr do not print compilation errors"
38 echo " --skip-valerr do not print validation errors"
39 echo " --quiet synonym for --skip-ok --skip-comperr --skip-valerr --no-summary"
40 echo " --terse print terse single line progress summary"
41 echo "Disassembly options:"
42 echo " --raw-id uses raw ids for disassembly"
44 echo "Usage examples. Note most non-hlsl tests fail to compile for expected reasons."
45 echo " Exercise all hlsl.* files:"
46 echo " $(basename $0) hlsl.*"
47 echo " Exercise all hlsl.* files, tersely:"
48 echo " $(basename $0) --terse hlsl.*"
49 echo " Print validator output for myfile.frag:"
50 echo " $(basename $0) --quiet --dump-val myfile.frag"
51 echo " Exercise hlsl.* files, only printing validation errors:"
52 echo " $(basename $0) --skip-ok --skip-comperr hlsl.*"
59 printf "%-40s: %b\n" "$1" "$2"
62 # make sure we can find glslang
63 [[ -x "$EXE" ]] || fatal "Unable to locate $(basename "$EXE") executable"
64 [[ -x "$VAL" ]] || fatal "Unable to locate spirv-val executable"
65 [[ -x "$DIS" ]] || fatal "Unable to locate spirv-dis executable"
67 for gtest in $gtests; do
68 [[ -r "$gtest" ]] || fatal "Unable to locate source file: $(basename $gtest)"
72 declare -r spvfile='out.spv' \
78 declare opt_vallog=false \
82 opt_stat_comperr=true \
84 opt_stat_valerr=true \
91 trap "rm -f ${spvfile} ${complog} ${vallog} ${dislog}" EXIT
93 # Language guesser: there is no fixed mapping from filenames to language,
94 # so this examines the file and return one of:
99 # This is easier WRT future expansion than a big explicit list.
100 function FindLanguage()
104 # If it starts with hlsl, assume it's hlsl.
105 if [[ "$test" == *hlsl.* ]]; then
110 if [[ "$test" == *.spv ]]; then
115 # If it doesn't start with spv., assume it's GLSL.
116 if [[ ! "$test" == spv.* && ! "$test" == remap.* ]]; then
121 # Otherwise, attempt to guess from shader contents, since there's no
122 # fixed mapping of filenames to languages.
123 local contents="$(cat "$test")"
125 if [[ "$contents" == *#version* ]]; then
130 if [[ "$contents" == *SamplerState* ||
131 "$contents" == *cbuffer* ||
132 "$contents" == *SV_* ]]; then
140 # Attempt to discover entry point
141 function FindEntryPoint()
145 # if it's not hlsl, always use main
146 if [[ "$language" != 'hlsl' ]]; then
151 # Try to find it in test sources
152 awk -F '[ (){",]+' -e "\$2 == \"${test}\" { print \$3; found=1; } END { if (found==0) print \"main\"; } " $gtests
155 # command line options
159 # -c) glslang="$2"; shift 2;;
161 --no-color) opt_color=false; shift;;
162 --no-summary) opt_summary=false; shift;;
163 --skip-ok) opt_stat_ok=false; shift;;
164 --skip-comperr) opt_stat_comperr=false; shift;;
165 --skip-valerr) opt_stat_valerr=false; shift;;
166 --dump-asm) opt_dislog=true; shift;;
167 --dump-val) opt_vallog=true; shift;;
168 --dump-comp) opt_complog=true; shift;;
169 --raw-id) opt_raw_id=true; shift;;
170 --quiet) opt_quiet=true; shift;;
171 --terse) opt_quiet=true
174 --*) fatal "Unknown command line option: $1";;
179 # this is what quiet means
182 opt_stat_comperr=false
183 opt_stat_valerr=false
184 $opt_terse || opt_summary=false
188 declare -r white="\e[1;37m" cyan="\e[1;36m" red="\e[0;31m" no_color="\e[0m"
190 declare -r white="" cyan="" red="" no_color=""
194 declare -i count_ok=0 count_err=0 count_nocomp=0 count_total=0
196 declare -r dashsep='------------------------------------------------------------------------'
199 # if no shaders given, look for everything in current directory
200 [[ ${#testfiles[*]} == 0 ]] && testfiles=(*.frag *.vert *.tesc *.tese *.geom *.comp)
202 $opt_summary && printf "\nValidating: ${#testfiles[*]} shaders\n\n"
204 # Loop through the shaders we were given, compiling them if we can.
205 for test in ${testfiles[*]}
207 if [[ ! -r "$test" ]]; then
208 $opt_quiet || status "$test" "${red}FILE NOT FOUND${no_color}"
214 $opt_terse && printf "\r[%-3d/%-3d : ${white}comperr=%-3d ${red}valerr=%-3d ${cyan}ok=%-3d${no_color}]" \
215 ${count_total} ${#testfiles[*]} ${count_nocomp} ${count_err} ${count_ok}
217 language="$(FindLanguage $test)"
218 entry="$(FindEntryPoint $test)"
222 hlsl) langops='-D --hlsl-iomap --hlsl-offsets';;
224 bin) continue;; # skip binaries
225 *) $opt_quiet || status "$test" "${red}UNKNOWN LANGUAGE${no_color}"; continue;;
228 # compile the test file
229 if compout=$("$EXE" -e "$entry" $langops -V -o "$spvfile" "$test" 2>&1)
231 # successful compilation: validate
232 if valout=$("$VAL" --target-env ${targetenv} "$spvfile" 2>&1)
235 $opt_stat_ok && status "$test" "${cyan}OK${no_color}"
239 $opt_stat_valerr && status "$test" "${red}VAL ERROR${no_color}"
240 printf "%s\n%s:\n%s\n" "$dashsep" "$test" "$valout" >> "$vallog"
245 printf "%s\n%s:\n" "$dashsep" "$test" >> "$dislog"
246 $opt_raw_id && id_opt=--raw-id
247 "$DIS" ${id_opt} "$spvfile" >> "$dislog"
251 $opt_stat_comperr && status "$test" "${white}COMP ERROR${no_color}"
252 printf "%s\n%s\n" "$dashsep" "$compout" >> "$complog"
260 $opt_summary && printf "\nSummary: ${white}${count_nocomp} compile errors${no_color}, ${red}${count_err} validation errors${no_color}, ${cyan}${count_ok} successes${no_color}\n"
263 $opt_vallog && [[ -r $vallog ]] && cat "$vallog"
264 $opt_complog && [[ -r $complog ]] && cat "$complog"
265 $opt_dislog && [[ -r $dislog ]] && cat "$dislog"
268 [[ ${count_err} -gt 0 ]] && exit 1