28890ae3e9cd135fd22bc8af1f57870f93673938
[xonotic/xonotic-maps.pk3dir.git] / scripts / shader-cpp.sh
1 #!/bin/sh
2
3 case "$0" in
4         */*)
5                 cd "${0%/*}"
6                 ;;
7 esac
8
9 . ./shader-parser.subr
10
11 LF="
12 "
13 TAB="   "
14
15 parse_shader_pre()
16 {
17         shadertext=
18         shaderconds=
19 }
20 parse_conditional()
21 {
22         shaderconds="$shaderconds$LF$*"
23 }
24 parse_shader_line()
25 {
26         case "$1" in
27                 '#if')
28                         parse_conditional $2 $3
29                         ;;
30         esac
31         shadertext="$shadertext$LF$TAB$*"
32 }
33 parse_shaderstage_pre()
34 {
35         shadertext="$shadertext$LF$TAB{"
36 }
37 parse_shaderstage_line()
38 {
39         case "$1" in
40                 '#if')
41                         parse_conditional $2 $3
42                         ;;
43         esac
44         shadertext="$shadertext$LF$TAB$TAB$*"
45 }
46 parse_shaderstage_post()
47 {
48         shadertext="$shadertext$LF$TAB}"
49 }
50 # note: the conds are what is FALSE, not TRUE
51 shaderkill()
52 {
53         parseALLtheconds=$conds
54         while :; do
55                 case "$parseALLtheconds" in
56                         '')
57                                 break
58                                 ;;
59                         *$LF*)
60                                 thiscond=${parseALLtheconds%%$LF*}
61                                 parseALLtheconds=${parseALLtheconds#*$LF}
62                                 ;;
63                         *)
64                                 thiscond=$parseALLtheconds
65                                 parseALLtheconds=
66                                 ;;
67                 esac
68                 [ -n "$thiscond" ] || continue
69                 echo "$TAB""dpshaderkillifcvar $thiscond"
70         done
71 }
72 preprocess()
73 {
74         condstack=
75         echo "$shadertext" | while IFS= read -r L; do
76                 [ -n "$L" ] || continue
77                 set -- $L
78                 k=$1
79                 shift
80                 case "$k" in
81                         '#if')
82                                 case "$LF$conds$LF" in
83                                         *"$LF$*$LF"*)
84                                                 condstack=0$condstack
85                                                 ;;
86                                         *)
87                                                 condstack=1$condstack
88                                                 ;;
89                                 esac
90                                 ;;
91                         '#else')
92                                 case "$condstack" in
93                                         0*)
94                                                 condstack=1${condstack#0}
95                                                 ;;
96                                         1*)
97                                                 condstack=0${condstack#1}
98                                                 ;;
99                                 esac
100                                 ;;
101                         '#endif')
102                                 condstack=${condstack#?}
103                                 ;;
104                         *)
105                                 case "$condstack" in
106                                         *0*)
107                                                 ;;
108                                         *)
109                                                 echo "${L% }"
110                                                 ;;
111                                 esac
112                                 ;;
113                 esac
114         done
115 }
116 conditionalize()
117 {
118         case "$1" in
119                 '')
120                         # no conditions
121                         echo "$parsing_shader"
122                         echo "{"
123                         shaderkill
124                         preprocess
125                         echo "}"
126                         return
127                         ;;
128                 *$LF*)
129                         # many conditions
130                         firstcond="${1%%$LF*}"
131                         restcond="${1#*$LF}"
132                         ;;
133                 *)
134                         firstcond=$1
135                         restcond=
136                         ;;
137         esac
138         (
139                 conds="$conds$LF$firstcond"
140                 conditionalize "$restcond"
141         )
142         (
143                 set -- $firstcond
144                 case "$2" in
145                         '==') op='!=' ;;
146                         '!=') op='==' ;;
147                         '>') op='<=' ;;
148                         '<') op='>=' ;;
149                         '>=') op='<' ;;
150                         '<=') op='>' ;;
151                 esac
152                 set -- "$1" "$op" "$3"
153                 conds="$conds$LF$*"
154                 conditionalize "$restcond"
155         )
156 }
157 parse_shader_post()
158 {
159         allconds=`echo "$shaderconds" | sort -u | grep .`
160         conds=
161         conditionalize "$allconds"
162 }
163
164 parse_shaders "$@"