aboutsummaryrefslogtreecommitdiffstats
path: root/emulator/opengl/host/libs/Translator/GLES_V2/ShaderParser.cpp
blob: 940538a7faaf3531118b6fa9458e035f6e75bb48 (plain)
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/*
* Copyright 2011 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "ShaderParser.h"
#include <stdlib.h>
#include <string.h>

ShaderParser::ShaderParser():ObjectData(SHADER_DATA),
                             m_type(0),
                             m_originalSrc(NULL),
                             m_parsedLines(NULL) {
    m_infoLog = new GLchar[1];
    m_infoLog[0] = '\0';
};

ShaderParser::ShaderParser(GLenum type):ObjectData(SHADER_DATA), 
                                        m_type(type),
                                        m_originalSrc(NULL),
                                        m_parsedLines(NULL) {

    m_infoLog = new GLchar[1];
    m_infoLog[0] = '\0';
};

void ShaderParser::setSrc(const Version& ver,GLsizei count,const GLchar** strings,const GLint* length){
    for(int i = 0;i<count;i++){
        m_src.append(strings[i]);
    }
    //store original source
    if (m_originalSrc)
        free(m_originalSrc);
    m_originalSrc = strdup(m_src.c_str());

    clearParsedSrc();

    // parseGLSLversion must be called first since #version should be the
    // first token in the shader source.
    parseGLSLversion();
    parseBuiltinConstants();
    /*
      version 1.30.10 is the first version of GLSL Language containing precision qualifiers
      if the glsl version is less than 1.30.10 than we will use a shader parser which omits
      all precision qualifiers from the shader source , otherwise we will use a shader parser
      which set the default precisions to be the same as the default precisions of GLSL ES
    */
#if 0
    if(ver < Version(1,30,10)){
        parseOmitPrecision();
     } else {
        parseExtendDefaultPrecision();
     }
#else
    //XXX: Until proved otherwise, glsl doesn't know/use those precision macros, so we omit then
    parseOmitPrecision();
#endif
    parseLineNumbers();
    parseOriginalSrc();
}
const GLchar** ShaderParser::parsedLines() {
      m_parsedLines = (GLchar*)m_parsedSrc.c_str();
      return const_cast<const GLchar**> (&m_parsedLines);
};

const char* ShaderParser::getOriginalSrc(){
    return m_originalSrc;
}

void ShaderParser::parseLineNumbers()
{
    m_parsedSrc += "#line 1\n";
}

void ShaderParser::parseOriginalSrc() {
    m_parsedSrc+=m_src;
}

void ShaderParser::parseGLSLversion() {

    //
    // find in shader the #version token if exist.
    // That token should be the first non-comment or blank token
    //
    const char *src = m_src.c_str();
    const int minGLSLVersion = 120;
    int glslVersion = minGLSLVersion;
    enum {
        PARSE_NONE,
        PARSE_IN_C_COMMENT,
        PARSE_IN_LINE_COMMENT
    } parseState = PARSE_NONE;
    const char *c = src;

    while( c && *c != '\0') {
        if (parseState == PARSE_IN_C_COMMENT) {
            if (*c == '*' && *(c+1) == '/') {
                parseState = PARSE_NONE;
                c += 2;
            }
            else c++;
        }
        else if (parseState == PARSE_IN_LINE_COMMENT) {
            if (*c == '\n') {
                parseState = PARSE_NONE;
            }
            c++;
        }
        else if (*c == '/' && *(c+1) == '/') {
            parseState = PARSE_IN_LINE_COMMENT;
            c += 2;
        }
        else if (*c == '/' && *(c+1) == '*') {
            parseState = PARSE_IN_C_COMMENT;
            c += 2;
        }
        else if (*c == ' ' || *c == '\t' || *c == '\r' || *c == '\n') {
            c++;
        }
        else {
            //
            // We have reached the first non-blank character outside
            // a comment, this must be a #version token or else #version
            // token does not exist in this shader source.
            //
            if (!strncmp(c,"#version",8)) {
                int ver;
                if (sscanf(c+8,"%d",&ver) == 1) {
                    //
                    // parsed version string correctly, blank out the
                    // version token from the source, we will add it later at
                    // the begining of the shader.
                    //
                    char *cc = (char *)c;
                    for (int i=0; i<8; i++,cc++) *cc = ' ';
                    while (*cc < '0' || *cc > '9') { *cc = ' '; cc++; }
                    while (*cc >= '0' && *cc <= '9') { *cc = ' '; cc++; }

                    // Use the version from the source but only if
                    // it is larger than our minGLSLVersion
                    if (ver > minGLSLVersion) glslVersion = ver;
                }
            }

            //
            // break the loop, no need to go further on the source.
            break;
        }
    }

    //
    // allow to force GLSL version through environment variable
    //
    const char *forceVersion = getenv("GOOGLE_GLES_FORCE_GLSL_VERSION");
    if (forceVersion) {
        int ver;
        if (sscanf(forceVersion,"%d",&ver) == 1) {
            glslVersion = ver;
        }
    }

    //
    // if glslVersion is defined, add it to the parsed source
    //
    if (glslVersion > 0) {
        char vstr[16];
        sprintf(vstr,"%d",glslVersion);
        m_parsedSrc += std::string("#version ") + 
                       std::string(vstr) + 
                       std::string("\n");
    }
}

void ShaderParser::parseBuiltinConstants()
{
    m_parsedSrc += 
                   "const int _translator_gl_MaxVertexUniformVectors = 256;\n"
                   "const int _translator_gl_MaxFragmentUniformVectors = 256;\n"
                   "const int _translator_gl_MaxVaryingVectors = 15;\n"
                   "#define gl_MaxVertexUniformVectors _translator_gl_MaxVertexUniformVectors\n"
                   "#define gl_MaxFragmentUniformVectors _translator_gl_MaxFragmentUniformVectors\n"
                   "#define gl_MaxVaryingVectors _translator_gl_MaxVaryingVectors\n";

}

void ShaderParser::parseOmitPrecision(){

    //defines we need to add in order to Omit precisions qualifiers
    static const GLchar defines[] = {
                                         "#define GLES 1\n"
                                         "#define lowp \n"
                                         "#define mediump \n"
                                         "#define highp \n"
                                     };
    m_parsedSrc+=defines;

    //
    // parse the source and blank out precision statements
    // which has the following syntax:
    //   precision {qualifier} {type};
    // where {qualifier} is one of lowp,mediump or hightp
    // type is any valid GLES defined type (we do not check that here!)
    // NOTE: This is needed in order to workaround driver bug in
    //       Intel/Linux where the compiler does not get statement like
    //       "float;", otherwise we could just define a macro named
    //       precision to be empty.
    //
    const char *src = m_src.c_str();

    enum { 
        PRECISION,
        QUALIFIER,
        SEMICOLON
    } statementState = PRECISION;
    const char *precision = NULL;

    enum {
        PARSE_NONE,
        PARSE_IN_C_COMMENT,
        PARSE_IN_LINE_COMMENT
    } parseState = PARSE_NONE;
    const char *c = src;
    const char *t = NULL;

    #define IS_DELIMITER(c) ( (c) == ' ' || (c) == '\t' || (c) == '\r' || (c) == '\n' )
    #define IS_TOKEN_START(c) ( ((c) >= 'a' && (c) <='z') || ((c) >= 'A' && (c) <= 'Z') )
    #define IS_TOKEN_DELIMITER(c) ( IS_DELIMITER(c) || (c) == ';' )

    while( c && *c != '\0') {
        if (parseState == PARSE_IN_C_COMMENT) {
            if (*c == '*' && *(c+1) == '/') {
                parseState = PARSE_NONE;
                c += 2;
            }
            else c++;
        }
        else if (parseState == PARSE_IN_LINE_COMMENT) {
            if (*c == '\n') {
                parseState = PARSE_NONE;
            }
            c++;
        }
        else if (*c == '/' && *(c+1) == '/') {
            parseState = PARSE_IN_LINE_COMMENT;
            c += 2;
        }
        else if (*c == '/' && *(c+1) == '*') {
            parseState = PARSE_IN_C_COMMENT;
            c += 2;
        }
        else if (t && IS_TOKEN_DELIMITER(*c)) {
            int tokenLen = c - t;
            switch (statementState) {
            case PRECISION:
                if (tokenLen == 9 && !strncmp(t,"precision",9)) {
                    statementState = QUALIFIER;
                    precision = t;
                }
                break;
            case QUALIFIER:
                if ((tokenLen == 4 && !strncmp(t,"lowp",4)) ||
                    (tokenLen == 7 && !strncmp(t,"mediump",7)) ||
                    (tokenLen == 5 && !strncmp(t,"highp",5))) {
                    statementState = SEMICOLON;
                }
                else {
                    statementState = PRECISION;
                }
                break;
            case SEMICOLON:
                if (*c == ';') {
                    for (char *r = (char *)precision; r<=c ; ++r) {
                        *r = ' '; //blank the character
                    }
                }
                statementState = PRECISION; //search for the next precision line
                break;
            default:
                break;
            }
            c++;
            t = NULL;
        }
        else if (IS_DELIMITER(*c)) {
            c++;
        }
        else {
            if (!t && IS_TOKEN_START(*c)) {
                t = c;
            }
            c++;
        }
    }
}

void ShaderParser::parseExtendDefaultPrecision(){

    //the precision lines which we need to add to the shader
    static const GLchar extend[] = {
                                      "#define GLES 1\n"
                                      "precision lowp sampler2D;\n"
                                      "precision lowp samplerCube;\n"
                                   };

    m_parsedSrc+=extend;
}

void ShaderParser::clearParsedSrc(){
    m_parsedSrc.clear();
}

GLenum ShaderParser::getType() {
    return m_type;
}

void ShaderParser::setInfoLog(GLchar* infoLog)
{
    delete[] m_infoLog;
    m_infoLog = infoLog;
}

GLchar* ShaderParser::getInfoLog()
{   
    return m_infoLog;
}

ShaderParser::~ShaderParser(){
    clearParsedSrc();
    if (m_originalSrc)
        free(m_originalSrc);
    delete[] m_infoLog;
}