some shader fixes again
[xonotic/xonotic-maps.pk3dir.git] / scripts / shader-audit.sh
1 #!/bin/sh
2
3 case "$0" in
4         */*)
5                 cd "${0%/*}"
6                 ;;
7 esac
8
9 pid=$$
10 status=true
11 trap 'status=false' USR1
12 seterror()
13 {
14         kill -USR1 "$pid"
15 }
16
17 LF="
18 "
19
20 normalize()
21 {
22         echo "$1" | sed 's/\.\(tga\|jpg\|png\)$//'
23 }
24
25 allowed_prefixes=
26 forbidden_prefixes=
27
28 getstats_e()
29 {
30         identify -verbose -depth 8 -alpha set -alpha extract "$1" | {
31                 pix=0
32                 while read -r L V R; do
33                         case "$L" in
34                                 Geometry:)
35                                         V=${V%%[-+]*}
36                                         pix=$(( (${V%%x*} * ${V#*x}) / 2 ))
37                                         ;;
38                                 min:)
39                                         min=$V
40                                         ;;
41                                 max:)
42                                         max=$V
43                                         ;;
44                                 [0-9]*:)
45                                         pix=$(( $pix - ${L%:} ))
46                                         if [ $pix -le 0 ]; then
47                                                 median=`echo "$V $R" | cut -d , -f 1 | tr -cd 0-9`
48                                                 break
49                                         fi
50                         esac
51                 done
52                 cat >/dev/null
53                 echo "min=$min"
54                 echo "max=$max"
55                 echo "median=$median"
56                 echo "error=false"
57         }
58 }
59 getstats()
60 {
61         min=255
62         max=255
63         median=255
64         error=true
65         [ -f "$1" ] || return 1
66         eval `getstats_e "$1"`
67 }
68
69 textures_used=
70 # $1 = shader
71 # $2 = texture
72 # $3 = shader | map | animmap | editorimage | sky
73 use_texture()
74 {
75         # does texture exist?
76         if \
77                 [ -f "../$2.tga" ] || \
78                 [ -f "../$2.jpg" ] || \
79                 [ -f "../$2.png" ]; then
80                 :
81         else
82                 if [ "$3" = "shader" ]; then
83                         return
84                 else
85                         echo "(EE) shader $1 uses non-existing texture $2"; seterror
86                 fi
87         fi
88         textures_used="$textures_used$LF$2"
89
90         if [ x"$3" = x"map" ]; then
91                 lasttex=$2
92                 if [ -n "$AUDIT_ALPHACHANNELS" ]; then
93                         if [ -f "../${2}_norm.tga" ] || [ -f "../${2}_norm.png" ] || [ -f "../${2}_norm.jpg" ]; then
94                                 case "$offsetmapping_match8" in
95                                         '') # no dpoffsetmapping keyword
96                                                 getstats "../${2}_norm.tga" || getstats "../${2}_norm.png" || getstats "../${2}_norm.jpg"
97                                                 if [ "$min" -eq "$max" ]; then
98                                                         echo "(WW) shader $1 uses broken normalmap ${2}_norm.tga (add dpoffsetmapping none)"
99                                                 else
100                                                         echo "(EE) shader $1 uses ${2}_norm.tga but lacks median (add dpoffsetmapping - 1 match8 $median)"; seterror
101                                                 fi
102                                                 ;;
103                                         none) # offsetmapping turned off explicitly
104                                                 ;;
105                                         default) # offsetmapping keyword without bias
106                                                 getstats "../${2}_norm.tga"
107                                                 if [ "$min" -eq "$max" ]; then
108                                                         echo "(EE) shader $1 uses broken normalmap ${2}_norm.tga, maybe use dpoffsetmapping none?";