aboutsummaryrefslogtreecommitdiffstats
path: root/src/modules/evas/engines/gl_common/shader/gen_shaders.sh
blob: db1cad4fbce0ad151219e577d72a9a1d99553528 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#!/bin/bash

set -e

ENDIAN="$1"
DIR=`dirname $0`
OUTPUT=${DIR}/evas_gl_shaders"$1".x
OUTPUT_ENUM=${DIR}/evas_gl_enum.x
CPP="cpp -P"

VTX_HEADER="#ifdef GL_ES
precision highp float;
#endif
"

FGM_HEADER="#ifdef GL_ES
#ifdef GL_FRAGMENT_PRECISION_HIGH
precision highp float;
#else
precision mediump float;
#endif
#endif
"

# Hack
FGM_HEADER_OES="#ifdef GL_ES
# extension GL_OES_EGL_image_external : require
# ifdef GL_FRAGMENT_PRECISION_HIGH
precision highp float;
# else
precision mediump float;
# endif
# define SAMPLER_EXTERNAL_OES samplerExternalOES
#else
# define SAMPLER_EXTERNAL_OES sampler2D
#endif
"

# Skip generation if there is no diff (or no git)
if ! git rev-parse 2>> /dev/null >> /dev/null ; then exit 0 ; fi
if git diff --quiet --exit-code -- "$DIR"
then
  touch "${OUTPUT}" "${OUTPUT_ENUM}"
  exit 0
fi

function upper() {
  echo $@ |tr '[:lower:]' '[:upper:]'
}

function run() {
  [ "$V" = "1" ] && echo "$@"
  "$@"
}

# Urgh
OIFS=$IFS
IFS=$'\n' SHADERS=(`cat ${DIR}/shaders.txt`)
IFS=$OIFS

# Write header
printf "/* DO NOT MODIFY THIS FILE AS IT IS AUTO-GENERATED */\n" > ${OUTPUT}
printf "/* IF IT IS CHANGED PLEASE COMMIT THE CHANGES */\n\n" >> ${OUTPUT}

# Including private for hilights and stuff :)
printf "#include \"../evas_gl_private.h\"\n\n" >> ${OUTPUT}

# Prepare list of shaders:
shaders_source=""
shaders_enum=""
shaders_type=(frag vert)
shaders_textures=""

# Generate SHD files
LIST=""
for (( i = 0; i < ${#SHADERS[@]} ; i++ )) ; do
  s=${SHADERS[$i]}
  if [[ $s == \#* ]] ; then continue ; fi

  name=`echo $s |cut -d ':' -f 1`
  opts=`echo $s |cut -d ':' -f 2 |tr ',' '\n'`

  echo "  SHADER   $name"
  UNAME=`echo ${name} |tr "[:lower:]" "[:upper:]"`
  OPTS=""
  if [[ "$1" == "_bigendian" ]] ; then OPTS="-DBIGENDIAN"; fi
  FGM=${FGM_HEADER}
  TYPE=`echo ${name} |cut -d '_' -f 1 |tr '[:lower:]' '[:upper:]'`
  bgra=0
  mask=0
  nomul=0
  afill=0
  sam="SAM11"
  masksam="SAM11"
  tex=""

  # Urgh. Some fixups
  case $TYPE in
   IMG) TYPE="IMAGE";;
   RGB) TYPE="RGB_A_PAIR";;
   TEX) TYPE="TEX_EXTERNAL";;
  esac

  for opt in $opts ; do
    UP=`upper $opt`
    OPTS="$OPTS -DSHD_${UP}"
    case $opt in
     tex) tex="${tex}tex ";;
     mask) tex="${tex}texm " ; mask=1;;
     texa) tex="${tex}texa ";;
     yuv) tex="${tex}texu texv ";;
     nv12) tex="${tex}texuv ";;
     yuy2) tex="${tex}texuv ";;
     bgra) bgra=1;;
     nomul) nomul=1;;
     afill) afill=1;;
     external) FGM=${FGM_HEADER_OES};;
     sam12) sam="SAM12";;
     sam21) sam="SAM21";;
     sam22) sam="SAM22";;
     masksam12) masksam="SAM12";;
     masksam21) masksam="SAM21";;
     masksam22) masksam="SAM22";;
    esac
  done

  # vertex shader
  filev=${DIR}/${name}_vert.shd
  printf "${VTX_HEADER}" >| ${filev}
  run $CPP $OPTS ${DIR}/vertex.glsl >> ${filev}

  # fragment shader
  filef=${DIR}/${name}_frag.shd
  printf "${FGM}" >| ${filef}
  run $CPP $OPTS ${DIR}/fragment.glsl >> ${filef}

  k=0
  for shd in ${filef} ${filev} ; do
    shdname=${name}_${shaders_type[$k]}
    k=$(($k + 1))

    OIFS=$IFS
    IFS=$'\n'
    printf "static const char ${shdname}_glsl[] =" >> ${OUTPUT}
    for line in `cat ${shd}` ; do
      printf "\n   \"${line}\\\n\"" >> ${OUTPUT}
    done
    printf ";\n" >> ${OUTPUT}
    IFS=${OIFS}

    printf "Evas_GL_Program_Source shader_${shdname}_src =\n{\n   ${shdname}_glsl,\n   NULL, 0\n};\n\n" >> ${OUTPUT}
  done

  shaders_source="${shaders_source}   { SHADER_${UNAME}, &(shader_${name}_vert_src), &(shader_${name}_frag_src), \"${name}\", SHD_${TYPE}, SHD_${sam}, SHD_${masksam}, ${bgra}, ${mask}, ${nomul}, ${afill} },\n"
  shaders_enum="${shaders_enum}   SHADER_${UNAME},\n"

  # Bind textures to the programs. Only if there is more than 1 texture.
  textures=(${tex})
  if [ ${#textures[@]} -ge 2 ] ; then
    for tname in ${tex} ; do
      shaders_textures="${shaders_textures}   { SHADER_${UNAME}, \"${tname}\" },\n"
    done
  fi
done

printf "
static const struct {
   Evas_GL_Shader id;
   Evas_GL_Program_Source *vert;
   Evas_GL_Program_Source *frag;
   const char *name;
   Shader_Type type;
   Shader_Sampling sam;
   Shader_Sampling masksam;
   Eina_Bool bgra : 1;
   Eina_Bool mask : 1;
   Eina_Bool nomul : 1;
   Eina_Bool afill : 1;
} _shaders_source[] = {\n" >> ${OUTPUT}

printf "${shaders_source}};\n\n" >> ${OUTPUT}

printf "/* DO NOT MODIFY THIS FILE AS IT IS AUTO-GENERATED */
/* IF IT IS CHANGED PLEASE COMMIT THE CHANGES */

typedef enum {
${shaders_enum}   SHADER_LAST
} Evas_GL_Shader;

#ifdef _EVAS_GL_CONTEXT_C

static struct {
   Evas_GL_Shader id;
   const char *tname;
} _shaders_textures[] = {
${shaders_textures}   { SHADER_LAST, NULL }
};

#endif // _EVAS_GL_CONTEXT_C
" >| ${OUTPUT_ENUM}

# You can remove the files now
#rm -f ${LIST}