]> git.tdb.fi Git - libs/gl.git/blob - source/programbuilder.cpp
Add support for array and multiple lights in generated shaders
[libs/gl.git] / source / programbuilder.cpp
1 #include <algorithm>
2 #include <cstring>
3 #include <msp/strings/format.h>
4 #include <msp/strings/utils.h>
5 #include "extension.h"
6 #include "program.h"
7 #include "programbuilder.h"
8 #include "shader.h"
9 #include "vertexformat.h"
10
11 using namespace std;
12
13 namespace Msp {
14 namespace GL {
15
16 /*
17 Naming conventions:
18   n_*        Normalized vector
19   l_*        Lighting component
20
21   obj_*      Object space
22   eye_*      Eye space
23   tbn_*      Tangent-Binormal-Normal space
24   shd_*      Shadow space
25   env_*      Environment space
26   *_dir      Direction vector
27
28   zzz_*      Wildcard space, resolved by the builder
29              All wildcard spaces within an expression must match
30
31   xxx_yyy_*  Matrix that transforms between yyy to xxx
32              The vector is on the side of its designated space, result will be
33              in the other space
34   *_matrix   A matrix (duh)
35   *_rmatrix  A mat4 that works with a row vector
36
37   rgb_*      Color with rgb components only
38   color_*    Color with rgba components
39 */
40
41 /* The array are stored in reverse order, so that variables always come after
42 anything that might need them. */
43 const ProgramBuilder::VariableDefinition ProgramBuilder::standard_variables[] =
44 {
45         { FRAGMENT, "gl_FragColor", "vec4", "frag_color", "g" },
46         { FRAGMENT, "frag_color", "vec4", "color_base", "!t" },
47         { FRAGMENT, "frag_color", "vec4", "tex_sample", "!l!s!mt" },
48         { FRAGMENT, "frag_color", "vec4", "tex_sample*color_base", "l|s|mt" },
49         { FRAGMENT, "color_base", "vec4", "vec4(1.0)", "!l!s!m" },
50         { FRAGMENT, "color_base", "vec4", "color", "!l!sm" },
51         { FRAGMENT, "color_base", "vec4", "vec4(vec3(l_shadow), 1.0)", "!ls!m" },
52         { FRAGMENT, "color_base", "vec4", "color*vec4(vec3(l_shadow), 1.0)", "!lsm" },
53         { FRAGMENT, "color_base", "vec4", "vec4(rgb_light_env, 1.0)", "l!m" },
54         { FRAGMENT, "color_base", "vec4", "vec4(rgb_light_env, material.diffuse.a)", "lm" },
55         { FRAGMENT, "rgb_light_env", "vec3", "rgb_light_full+reflect_sample.rgb*reflectivity", "e" },
56         { FRAGMENT, "rgb_light_env", "vec3", "rgb_light_full", "!e" },
57         { FRAGMENT, "rgb_light_full", "vec3", "rgb_light_shadow+ambient_product_diffuse.rgb", "m" },
58         { FRAGMENT, "rgb_light_full", "vec3", "rgb_light_shadow", "!m" },
59         { FRAGMENT, "ambient_product_diffuse", "vec4", "gl_FrontLightModelProduct.sceneColor", "g" },
60         { FRAGMENT, "ambient_product_diffuse", "vec4", "ambient_color*material.diffuse", "!g" },
61         { FRAGMENT, "rgb_light_shadow", "vec3", "rgb_light*l_shadow", "s" },
62         { FRAGMENT, "rgb_light_shadow", "vec3", "rgb_light", "!s" },
63         { FRAGMENT, "rgb_light[i]", "vec3", "vec3(l_diffuse[i])", "!m!p" },
64         { FRAGMENT, "rgb_light[i]", "vec3", "vec3(l_diffuse[i]+l_specular[i])", "!mp" },
65         { FRAGMENT, "rgb_light[i]", "vec3", "l_diffuse[i]*light_product_diffuse[i].rgb", "m!p" },
66         { FRAGMENT, "rgb_light[i]", "vec3", "l_diffuse[i]*light_product_diffuse[i].rgb+l_specular[i]*light_product_specular[i].rgb", "mp" },
67         { FRAGMENT, "light_product_diffuse[i]", "vec4", "gl_FrontLightProduct[i].diffuse", "g" },
68         { FRAGMENT, "light_product_diffuse[i]", "vec4", "light_sources[i].diffuse*material.diffuse", "!g" },
69         { FRAGMENT, "light_product_specular[i]", "vec4", "gl_FrontLightProduct[i].specular", "g" },
70         { FRAGMENT, "light_product_specular[i]", "vec4", "light_sources[i].specular*material.specular", "!g" },
71         { FRAGMENT, "reflect_sample", "vec4", "textureCube(environment, env_reflect_dir)", 0 },
72         { FRAGMENT, "env_reflect_dir", "vec3", "env_eye_matrix*eye_reflect_dir", 0 },
73         { FRAGMENT, "eye_reflect_dir", "vec3", "eye_tbn_matrix*tbn_reflect_dir", "n" },
74         { FRAGMENT, "zzz_reflect_dir", "vec3", "reflect(zzz_incident_dir, n_zzz_normal)", 0 },
75         { FRAGMENT, "l_shadow", "float", "mix(1.0, shadow_sample, shadow_darkness)", 0 },
76         { FRAGMENT, "shadow_sample", "float", "shadow2D(shadow, shd_vertex).r", 0 },
77         { FRAGMENT, "l_diffuse[i]", "float", "max(dot(n_zzz_normal, n_zzz_light_dir[i]), 0.0)", 0 },
78         { FRAGMENT, "l_specular[i]", "float", "pow(max(dot(n_zzz_half_vec[i], n_zzz_normal), 0.0), material.shininess)", 0 },
79         { FRAGMENT, "n_zzz_half_vec[i]", "vec3", "normalize(zzz_light_dir[i]-zzz_incident_dir)", 0 },
80         { FRAGMENT, "n_zzz_light_dir[i]", "vec3", "normalize(zzz_light_dir[i])", 0 },
81         { FRAGMENT, "n_tbn_normal", "vec3", "normal_sample*2.0-1.0", "n" },
82         { FRAGMENT, "n_eye_normal", "vec3", "normalize(eye_normal)", "!n" },
83         { FRAGMENT, "normal_sample", "vec3", "texture2D(normalmap, texture_coord).xyz", 0 },
84         { FRAGMENT, "tex_sample", "vec4", "texture2D(texture, texture_coord)", 0 },
85
86         { VERTEX, "gl_Position", "vec4", "projection_matrix*eye_vertex", 0 },
87         { VERTEX, "shd_vertex", "vec3", "vec3(dot(eye_vertex, gl_EyePlaneS[shadow_unit]), dot(eye_vertex, gl_EyePlaneT[shadow_unit]), dot(eye_vertex, gl_EyePlaneR[shadow_unit]))", "g" },
88         { VERTEX, "shd_vertex", "vec3", "(shd_eye_matrix*eye_vertex).xyz", "!g" },
89         { VERTEX, "tbn_light_dir[i]", "vec3", "eye_light_dir[i]*eye_tbn_matrix", 0 },
90         { VERTEX, "eye_light_dir[i]", "vec3", "normalize(eye_light_position[i].xyz-eye_vertex.xyz*eye_light_position[i].w)", 0 },
91         { VERTEX, "eye_light_position[i]", "vec4", "light_sources[i].position", 0 },
92         { VERTEX, "tbn_incident_dir", "vec3", "eye_incident_dir*eye_tbn_matrix", 0 },
93         { VERTEX, "eye_incident_dir", "vec3", "normalize(eye_vertex.xyz)", 0 },
94         { VERTEX, "eye_tbn_matrix", "mat3", "mat3(eye_tangent, eye_binormal, eye_normal)", 0 },
95         { VERTEX, "eye_vertex", "vec4", "eye_obj_matrix*vertex", 0 },
96         { VERTEX, "eye_normal", "vec3", "eye_obj_normal_matrix*normal", 0 },
97         { VERTEX, "eye_tangent", "vec3", "eye_obj_normal_matrix*tangent", 0 },
98         { VERTEX, "eye_binormal", "vec3", "eye_obj_normal_matrix*binormal", 0 },
99         { VERTEX, "texture_coord", "vec2", "texcoord.xy", 0 },
100
101         { ATTRIBUTE, "vertex", "vec4", "gl_Vertex", 0 },
102         { ATTRIBUTE, "texcoord", "vec4", "gl_MultiTexCoord0", 0 },
103         { ATTRIBUTE, "color", "vec4", "gl_Color", 0 },
104         { ATTRIBUTE, "normal", "vec3", "gl_Normal", 0 },
105         { ATTRIBUTE, "tangent", "vec3", 0, 0 },
106         { ATTRIBUTE, "binormal", "vec3", 0, 0 },
107
108         { UNIFORM, "shadow_unit", "int", 0, 0 },
109         { UNIFORM, "texture", "sampler2D", 0, 0 },
110         { UNIFORM, "shadow", "sampler2DShadow", 0, 0 },
111         { UNIFORM, "shadow_darkness", "float", 0, 0 },
112         { UNIFORM, "normalmap", "sampler2D", 0, 0 },
113         { UNIFORM, "environment", "samplerCube", 0, 0 },
114         { UNIFORM, "env_eye_matrix", "mat3", 0, 0 },
115         { UNIFORM, "reflectivity", "float", 0, 0 },
116         { UNIFORM, "eye_obj_matrix", "mat4", "gl_ModelViewMatrix", 0 },
117         { UNIFORM, "eye_obj_normal_matrix", "mat3", "gl_NormalMatrix", 0 },
118         { UNIFORM, "projection_matrix", "mat4", "gl_ProjectionMatrix", 0 },
119         { UNIFORM, "shd_eye_matrix", "mat4", 0, 0 },
120         { UNIFORM, "light_sources", "struct { vec4 position; vec4 diffuse; vec4 specular; }[MAX_LIGHTS]", "gl_LightSource[i]", 0 },
121         { UNIFORM, "ambient_color", "vec4", 0, 0 },
122         { UNIFORM, "material", "struct { vec4 ambient; vec4 diffuse; vec4 specular; float shininess; }", "gl_FrontMaterial", 0 },
123
124         // Terminator entry
125         { NO_SCOPE, 0, 0, 0, 0 }
126 };
127
128 const char ProgramBuilder::interfaces[] = { 0, 0, 0, 'v', 0 };
129
130 ProgramBuilder::ProgramBuilder(const StandardFeatures &f):
131         features(f),
132         feature_flags(features.create_flags()),
133         optimize(true)
134 {
135         if(!features.custom.empty())
136         {
137                 const char *whitespace = " \t\n";
138                 string::size_type start = 0;
139                 while(1)
140                 {
141                         start = features.custom.find_first_not_of(whitespace, start);
142                         if(start==string::npos)
143                                 break;
144
145                         string::size_type semicolon = features.custom.find(';', start);
146                         if(semicolon==start)
147                         {
148                                 ++start;
149                                 continue;
150                         }
151                         else if(semicolon==string::npos)
152                                 throw invalid_variable_definition(features.custom.substr(start));
153
154                         string::size_type equals = features.custom.find('=', start);
155                         if(equals>semicolon)
156                                 equals = string::npos;
157
158                         VariableDefinition var;
159                         string::size_type decl_end = min(equals, semicolon);
160                         for(unsigned i=0;; ++i)
161                         {
162                                 string::size_type word_end = features.custom.find_first_of(whitespace, start);
163                                 word_end = min(word_end, decl_end);
164                                 features.custom[word_end] = 0;
165
166                                 const char *word = &features.custom[start];
167                                 if(i==0)
168                                 {
169                                         if(!strcmp(word, "uniform"))
170                                                 var.scope = UNIFORM;
171                                         else if(!strcmp(word, "attribute"))
172                                                 var.scope = ATTRIBUTE;
173                                         else if(!strcmp(word, "vertex"))
174                                                 var.scope = VERTEX;
175                                         else if(!strcmp(word, "fragment"))
176                                                 var.scope = FRAGMENT;
177                                         else
178                                                 throw invalid_variable_definition(word);
179                                 }
180                                 else if(i==1)
181                                         var.type = word;
182                                 else if(i==2)
183                                         var.name = word;
184
185                                 start = features.custom.find_first_not_of(whitespace, word_end+1);
186                                 if(start>=decl_end)
187                                         break;
188                         }
189
190                         if(equals!=string::npos)
191                         {
192                                 start = features.custom.find_first_not_of(whitespace, equals+1);
193                                 if(start>=semicolon)
194                                         throw invalid_variable_definition("no expression");
195                                 features.custom[semicolon] = 0;
196                                 var.expression = &features.custom[start];
197                         }
198                         else
199                                 var.expression = 0;
200
201                         var.flags = 0;
202                         for(const VariableDefinition *j=standard_variables; j->name; ++j)
203                                 if(!strcmp(var.name, j->name))
204                                         var.flags = "o";
205
206                         custom_variables.push_front(var);
207
208                         start = semicolon+1;
209                 }
210         }
211 }
212
213 void ProgramBuilder::set_optimize(bool o)
214 {
215         optimize = o;
216 }
217
218 Program *ProgramBuilder::create_program() const
219 {
220         Program *prog = new Program;
221         add_shaders(*prog);
222         return prog;
223 }
224
225 void ProgramBuilder::add_shaders(Program &prog) const
226 {
227         list<ShaderVariable> variables;
228         list<ShaderVariable *> resolved_vars;
229
230         variables.push_front(ShaderVariable("gl_Position"));
231         variables.push_front(ShaderVariable(features.legacy ? "gl_FragColor" : "frag_color"));
232
233         list<VariableDefinition>::const_iterator next_custom = custom_variables.begin();
234         for(const VariableDefinition *i=standard_variables; i->name; )
235         {
236                 const VariableDefinition *def = 0;
237                 if(next_custom!=custom_variables.end() && (!strcmp(next_custom->name, i->name) || !next_custom->flags))
238                 {
239                         def = &*next_custom;
240                         ++next_custom;
241                 }
242                 else
243                 {
244                         def = i;
245                         ++i;
246
247                         // Skip over anything that isn't used with the supplied flags
248                         if(def->flags && !evaluate_flags(def->flags))
249                                 continue;
250                 }
251
252                 // See if this variable can satisfy any unresolved variables
253                 ShaderVariable *last_resolved = 0;
254                 for(list<ShaderVariable>::iterator j=variables.begin(); j!=variables.end(); ++j)
255                 {
256                         if(j->variable)
257                                 continue;
258
259                         if(!name_match(def->name, j->resolved_name.c_str()))
260                                 continue;
261
262                         if(last_resolved)
263                         {
264                                 /* We've already resolved a non-fuzzy variable in this iteration.
265                                 If there are multiple variables that can be resolved, they refer
266                                 to the same variable. */
267                                 j->resolve(*last_resolved);
268                                 continue;
269                         }
270
271                         j->resolve(*def);
272                         resolved_vars.push_front(&*j);
273                         if(!j->fuzzy_space)
274                                 last_resolved = &*j;
275
276                         if(!def->expression)
277                                 continue;
278
279                         vector<string> identifiers = extract_identifiers(def->expression);
280                         for(vector<string>::const_iterator k=identifiers.begin(); k!=identifiers.end(); ++k)
281                         {
282                                 // Use an existing variable if possible, but only if it's not fuzzy
283                                 ShaderVariable *var = 0;
284                                 for(list<ShaderVariable>::iterator l=variables.begin(); (!var && l!=variables.end()); ++l)
285                                         if(!l->fuzzy_space && l->resolved_name==*k)
286                                                 var = &*l;
287
288                                 if(!var)
289                                 {
290                                         variables.push_back(ShaderVariable(*k));
291                                         var = &variables.back();
292                                 }
293                                 j->add_reference(*var);
294                         }
295                 }
296         }
297
298         // Array sizes need to be resolved for inline processing
299         for(list<ShaderVariable>::iterator i=variables.end(); i!=variables.begin(); )
300                 (--i)->resolve_array(features);
301
302         for(list<ShaderVariable *>::const_iterator i=resolved_vars.begin(); i!=resolved_vars.end(); ++i)
303                 (*i)->check_inline(features.legacy, !optimize);
304
305         prog.attach_shader_owned(new VertexShader(create_source(resolved_vars, VERTEX)));
306         prog.attach_shader_owned(new FragmentShader(create_source(resolved_vars, FRAGMENT)));
307
308         if(!features.legacy)
309         {
310                 prog.bind_fragment_data(0, "frag_color");
311                 prog.bind_attribute(VERTEX4, "vertex");
312                 if(features.lighting)
313                         prog.bind_attribute(NORMAL3, "normal");
314                 else if(features.material)
315                         prog.bind_attribute(COLOR4_FLOAT, "color");
316                 if(features.texture || features.normalmap)
317                         prog.bind_attribute(TEXCOORD4, "texcoord");
318         }
319         if(features.normalmap)
320         {
321                 prog.bind_attribute(get_component_type(TANGENT3), "tangent");
322                 prog.bind_attribute(get_component_type(BINORMAL3), "binormal");
323         }
324 }
325
326 string ProgramBuilder::create_source(const list<ShaderVariable *> &variables, VariableScope scope) const
327 {
328         string source;
329
330         if(!features.legacy)
331                 source += "#version 130\n";
332
333         for(list<ShaderVariable *>::const_iterator i=variables.begin(); i!=variables.end(); ++i)
334                 if((*i)->variable->scope==UNIFORM && (*i)->is_referenced_from(scope) && !(*i)->inlined)
335                         source += format("uniform %s;\n", (*i)->create_declaration());
336
337         /* Interface variables need to have global declarations. */
338         for(list<ShaderVariable *>::const_iterator i=variables.begin(); i!=variables.end(); ++i)
339         {
340                 if(!(*i)->resolved_name.compare(0, 3, "gl_"))
341                         continue;
342
343                 InterfaceFlags interface = (*i)->get_interface_flags(scope);
344
345                 if(interface&INPUT)
346                 {
347                         const char *qualifier = (features.legacy ? scope==VERTEX ? "attribute" : "varying" : "in");
348                         source += format("%s %s;\n", qualifier, (*i)->create_declaration(interfaces[scope-1]));
349                 }
350
351                 if(interface&OUTPUT)
352                 {
353                         const char *qualifier = (features.legacy ? "varying" : "out");
354                         source += format("%s %s;\n", qualifier, (*i)->create_declaration(interfaces[scope]));
355                 }
356         }
357
358         source += "void main()\n{\n";
359
360         list<ShaderVariable *> loop_vars;
361         unsigned loop_size = 0;
362         for(list<ShaderVariable *>::const_iterator i=variables.begin(); i!=variables.end(); ++i)
363         {
364                 if(!loop_vars.empty() && !loop_vars.back()->in_loop && (*i)->array_size!=loop_size)
365                 {
366                         /* Declare all variables that need to be visible outside the loop.
367                         Output variables are already declared. */
368                         for(list<ShaderVariable *>::const_iterator j=loop_vars.begin(); j!=loop_vars.end(); ++j)
369                         {
370                                 InterfaceFlags interface = (*j)->get_interface_flags(scope);
371                                 if(!(*j)->in_loop && !(interface&OUTPUT))
372                                         source += format("\t%s;\n", (*j)->create_declaration());
373                         }
374
375                         source += format("\tfor(int i=0; i<%d; ++i)\n\t{\n", loop_size);
376                         for(list<ShaderVariable *>::const_iterator j=loop_vars.begin(); j!=loop_vars.end(); ++j)
377                         {
378                                 if((*j)->variable->scope==scope && !(*j)->inlined)
379                                 {
380                                         string decl;
381                                         if((*j)->in_loop)
382                                                 decl = (*j)->create_declaration(0, true);
383                                         else
384                                         {
385                                                 decl = (*j)->resolved_name;
386                                                 if(!(*j)->array_sum)
387                                                         decl += "[i]";
388                                         }
389                                         const char *oper = ((*j)->array_sum ? "+=" : "=");
390                                         source += format("\t\t%s %s %s;\n", decl, oper, (*j)->create_expression("i"));
391                                 }
392
393                                 InterfaceFlags interface = (*j)->get_interface_flags(scope);
394                                 if(interface&OUTPUT)
395                                 {
396                                         string expr = ((*j)->inlined ? (*j)->create_expression("i") : (*j)->resolved_name+"[i]");
397                                         source += format("\t\t%c_%s[i] = %s;\n", interfaces[scope], (*j)->resolved_name, expr);
398                                 }
399                         }
400                         source += "\t}\n";
401
402                         loop_vars.clear();
403                 }
404
405                 InterfaceFlags interface = (*i)->get_interface_flags(scope);
406
407                 if((*i)->array_size>1)
408                 {
409                         if((*i)->variable->scope==scope || (interface&OUTPUT))
410                         {
411                                 loop_size = (*i)->array_size;
412                                 loop_vars.push_back(*i);
413                         }
414                         continue;
415                 }
416
417                 if((*i)->variable->scope==scope && !(*i)->inlined)
418                 {
419                         string decl = ((interface&GOAL) ? (*i)->resolved_name : (*i)->create_declaration());
420                         source += format("\t%s = %s;\n", decl, (*i)->create_expression());
421                 }
422
423                 if((interface&(OUTPUT|GOAL))==OUTPUT)
424                 {
425                         string expr = ((*i)->inlined ? (*i)->create_expression() : (*i)->resolved_name);
426                         source += format("\t%c_%s = %s;\n", interfaces[scope], (*i)->resolved_name, expr);
427                 }
428         }
429
430         source += '}';
431
432         return source;
433 }
434
435 bool ProgramBuilder::evaluate_flags(const char *flags) const
436 {
437         if(!flags)
438                 return true;
439
440         bool cond = true;
441         char oper = '&';
442         for(const char *i=flags; *i; ++i)
443         {
444                 if(*i>='a' && *i<='z')
445                 {
446                         bool found = (feature_flags.find(*i)!=string::npos);
447                         if(oper=='|')
448                                 cond = (cond || found);
449                         else if(oper=='!')
450                                 cond = (cond && !found);
451                         else if(oper=='&')
452                                 cond = (cond && found);
453                         oper = '&';
454                 }
455                 else
456                         oper = *i;
457         }
458
459         return cond;
460 }
461
462 ProgramBuilder::MatchType ProgramBuilder::name_match(const char *n1, const char *n2, const char **space)
463 {
464         int i = 0;
465         int zzz = -1;
466         int zside = 0;
467         while(*n1 && *n2)
468         {
469                 if(*n1==*n2 || *n1=='z' || *n2=='z')
470                 {
471                         if(*n1!=*n2)
472                         {
473                                 int side = (*n1=='z' ? 1 : 2);
474                                 if(zzz<0)
475                                 {
476                                         zzz = i;
477                                         zside = side;
478                                         if(space)
479                                         {
480                                                 if(*n1=='z')
481                                                         *space = n2;
482                                                 else
483                                                         *space = n1;
484                                         }
485                                 }
486                                 else if(i>=zzz+3 || side!=zside)
487                                         return NO_MATCH;
488                         }
489                 }
490                 else
491                         return NO_MATCH;
492                 ++n1;
493                 ++n2;
494                 ++i;
495         }
496         return (!*n1 && !*n2) ? (zzz>=0 ? FUZZY : EXACT) : ((*n1=='[' || *n2=='[') ? ARRAY : NO_MATCH);
497 }
498
499 bool ProgramBuilder::parse_identifier(const char *ptr, unsigned &start, unsigned &length)
500 {
501         bool found = false;
502         bool member = false;
503         bool subscript = false;
504         for(const char *i=ptr;; ++i)
505         {
506                 if(!found)
507                 {
508                         if(!*i)
509                                 return false;
510                         if(isalpha(*i) || *i=='_')
511                         {
512                                 if(!member)
513                                 {
514                                         start = i-ptr;
515                                         found = true;
516                                 }
517                         }
518                         else if(*i=='.')
519                                 member = true;
520                         else
521                                 member = false;
522                 }
523                 else
524                 {
525                         if(subscript)
526                         {
527                                 if(*i==']')
528                                 {
529                                         length = i+1-(ptr+start);
530                                         return true;
531                                 }
532                                 else if(!isalpha(*i) || i>ptr+start+length+1)
533                                         return true;
534                         }
535                         else if(!isalnum(*i) && *i!='_')
536                         {
537                                 length = i-(ptr+start);
538                                 if(*i=='[')
539                                         subscript = true;
540                                 else
541                                         return true;
542                         }
543                 }
544         }
545 }
546
547 vector<string> ProgramBuilder::extract_identifiers(const char *expression)
548 {
549         vector<string> result;
550         const char *ptr = expression;
551         unsigned start = 0;
552         unsigned length = 0;
553         while(parse_identifier(ptr, start, length))
554         {
555                 result.push_back(string(ptr+start, length));
556                 ptr += start+length;
557         }
558         return result;
559 }
560
561 string ProgramBuilder::replace_identifiers(const char *expression, const map<string, string> &replace_map)
562 {
563         string result;
564         const char *ptr = expression;
565         unsigned start = 0;
566         unsigned length = 0;
567         while(parse_identifier(ptr, start, length))
568         {
569                 result.append(ptr, start);
570                 string identifier(ptr+start, length);
571                 map<string, string>::const_iterator i = replace_map.find(identifier);
572                 if(i!=replace_map.end())
573                         result += i->second;
574                 else
575                         result += identifier;
576                 ptr += start+length;
577         }
578         result += ptr;
579         return result;
580 }
581
582
583 ProgramBuilder::StandardFeatures::StandardFeatures():
584         texture(false),
585         material(false),
586         lighting(false),
587         max_lights(1),
588         specular(false),
589         normalmap(false),
590         shadow(false),
591         reflection(false),
592         legacy(!(get_glsl_version()>=Version(1, 30)))
593 { }
594
595 string ProgramBuilder::StandardFeatures::create_flags() const
596 {
597         string flags;
598         if(texture)
599                 flags += 't';
600         if(material)
601                 flags += 'm';
602         if(lighting)
603         {
604                 flags += 'l';
605                 if(specular)
606                         flags += 'p';
607                 if(normalmap)
608                         flags += 'n';
609         }
610         if(shadow)
611                 flags += 's';
612         if(reflection)
613                 flags += 'e';
614         if(legacy)
615                 flags += 'g';
616
617         return flags;
618 }
619
620
621 ProgramBuilder::ShaderVariable::ShaderVariable(const std::string &n):
622         name(n),
623         variable(0),
624         resolved_name(n),
625         fuzzy_space(name.find("zzz")!=string::npos),
626         array_sum(false),
627         array_size(0),
628         inlined(false),
629         inline_parens(false),
630         in_loop(false)
631 {
632         string::size_type bracket = name.find('[');
633         if(bracket!=string::npos)
634                 array_subscript = name.substr(bracket+1, name.size()-bracket-2);
635 }
636
637 void ProgramBuilder::ShaderVariable::resolve(const VariableDefinition &var)
638 {
639         variable = &var;
640         const char *space = 0;
641         MatchType match = name_match(var.name, resolved_name.c_str(), &space);
642
643         if(match==FUZZY)
644                 resolve_space(string(space, 3));
645         else if(match==ARRAY)
646         {
647                 if(array_subscript.empty())
648                         array_sum = true;
649                 else if(var.scope==UNIFORM)
650                 {
651                         const char *bracket = strrchr(variable->type, '[');
652                         if(bracket)
653                                 array_subscript = string(bracket+1, strlen(bracket)-2);
654                 }
655         }
656 }
657
658 void ProgramBuilder::ShaderVariable::resolve(ShaderVariable &var)
659 {
660         for(list<ShaderVariable *>::iterator i=referenced_by.begin(); i!=referenced_by.end(); ++i)
661                 (*i)->update_reference(*this, var);
662         var.referenced_by.insert(var.referenced_by.end(), referenced_by.begin(), referenced_by.end());
663 }
664
665 void ProgramBuilder::ShaderVariable::resolve_space(const string &space)
666 {
667         if(fuzzy_space)
668         {
669                 resolved_space = space;
670
671                 string::size_type zzz = resolved_name.find("zzz");
672                 resolved_name.replace(zzz, 3, resolved_space);
673                 fuzzy_space = false;
674
675                 // Resolving the space could have affected other variables that use this one
676                 for(list<ShaderVariable *>::iterator i=referenced_by.begin(); i!=referenced_by.end(); ++i)
677                         (*i)->resolve_space(space);
678         }
679
680         for(list<ShaderVariable *>::iterator i=referenced_vars.begin(); i!=referenced_vars.end(); ++i)
681                 if((*i)->fuzzy_space)
682                         (*i)->resolve_space(space);
683 }
684
685 void ProgramBuilder::ShaderVariable::resolve_array(const StandardFeatures &features, unsigned size_hint)
686 {
687         if(array_size)
688                 return;
689         if(!array_sum && array_subscript.empty())
690                 return;
691
692         if(!array_subscript.empty())
693         {
694                 string::size_type bracket = resolved_name.find('[');
695                 if(bracket!=string::npos)
696                         resolved_name = resolved_name.substr(0, bracket);
697         }
698
699         if(variable && variable->scope==UNIFORM)
700         {
701                 if(array_subscript=="MAX_LIGHTS")
702                         array_size = features.max_lights;
703                 else if(isnumrc(array_subscript))
704                         array_size = lexical_cast<unsigned>(array_subscript);
705                 else
706                         throw invalid_variable_definition("invalid array size");
707         }
708
709         if(!array_size)
710         {
711                 for(list<ShaderVariable *>::const_iterator i=referenced_vars.begin(); i!=referenced_vars.end(); ++i)
712                         if((*i)->array_size)
713                         {
714                                 array_size = (*i)->array_size;
715                                 break;
716                         }
717         }
718
719         if(!array_size && size_hint)
720                 array_size = size_hint;
721
722         if(array_size)
723         {
724                 for(list<ShaderVariable *>::const_iterator i=referenced_vars.begin(); i!=referenced_vars.end(); ++i)
725                         if(!(*i)->array_subscript.empty() && !(*i)->array_size)
726                                 (*i)->resolve_array(features, array_size);
727         }
728 }
729
730 void ProgramBuilder::ShaderVariable::add_reference(ShaderVariable &var)
731 {
732         referenced_vars.push_back(&var);
733         var.referenced_by.push_back(this);
734         if(var.fuzzy_space && !resolved_space.empty())
735                 var.resolve_space(resolved_space);
736 }
737
738 void ProgramBuilder::ShaderVariable::update_reference(ShaderVariable &from, ShaderVariable &to)
739 {
740         replace(referenced_vars.begin(), referenced_vars.end(), &from, &to);
741         replace(referenced_by.begin(), referenced_by.end(), &from, &to);
742         if(from.fuzzy_space && !to.fuzzy_space && !to.resolved_space.empty())
743                 resolve_space(to.resolved_space);
744 }
745
746 void ProgramBuilder::ShaderVariable::check_inline(bool allow_legacy, bool trivial_only)
747 {
748         if(variable->expression)
749         {
750                 if(array_sum && array_size>1)
751                         return;
752                 if(!allow_legacy && !strncmp(variable->expression, "gl_", 3))
753                         return;
754
755                 // Never inline goal variables
756                 if(referenced_by.empty())
757                         return;
758
759                 // Inline an expression consisting of a single identifier
760                 unsigned start, length;
761                 if(parse_identifier(variable->expression, start, length))
762                         if(start==0 && variable->expression[length]==0)
763                         {
764                                 inlined = true;
765                                 return;
766                         }
767
768                 if(trivial_only)
769                         return;
770
771                 /* If all references to the variable come from arrays in the same scope
772                 and of the same size, the variable can be embedded in the loop. */
773                 in_loop = (array_size>1 && !array_sum);
774                 for(list<ShaderVariable *>::const_iterator i=referenced_by.begin(); i!=referenced_by.end(); ++i)
775                         if((*i)->variable->scope!=variable->scope || (*i)->array_size!=array_size)
776                                 in_loop = false;
777                 
778                 /* Count all refs to this variable.  Refs from array variables count once
779                 per loop iteration. */
780                 unsigned total_refs = 0;
781                 unsigned in_scope_refs = 0;
782                 for(list<ShaderVariable *>::const_iterator i=referenced_by.begin(); i!=referenced_by.end(); ++i)
783                 {
784                         unsigned count = max((*i)->array_size*!in_loop, 1U);
785                         total_refs += count;
786                         if((*i)->variable->scope==variable->scope)
787                                 in_scope_refs += count;
788                 }
789
790                 /* Inline if there's only one ref, or if all refs are in other scopes.
791                 In the latter case, the actual inlining will happen in the interface
792                 variable assignment. */
793                 if(total_refs==1 || in_scope_refs==0)
794                 {
795                         inlined = true;
796                         unsigned level = 0;
797                         for(const char *c=variable->expression; (!inline_parens && *c); ++c)
798                         {
799                                 if(*c=='(')
800                                         ++level;
801                                 else if(*c==')')
802                                         --level;
803                                 else if(level==0 && !isalnum(*c) && *c!='_' && *c!='.')
804                                         inline_parens = true;
805                         }
806                 }
807         }
808 }
809
810 bool ProgramBuilder::ShaderVariable::is_referenced_from(VariableScope scope) const
811 {
812         for(list<ShaderVariable *>::const_iterator i=referenced_by.begin(); i!=referenced_by.end(); ++i)
813                 if((*i)->variable->scope==scope)
814                         return true;
815         return false;
816 }
817
818 ProgramBuilder::InterfaceFlags ProgramBuilder::ShaderVariable::get_interface_flags(VariableScope scope) const
819 {
820         /* Uniforms are available to all stages and are not passed through
821         interfaces */
822         if(variable->scope==UNIFORM)
823                 return NO_INTERFACE;
824
825         int flags = NO_INTERFACE;
826
827         for(list<ShaderVariable *>::const_iterator i=referenced_by.begin(); i!=referenced_by.end(); ++i)
828         {
829                 /* Variables used in a later scope than they are declared in need to go
830                 through the interface */
831                 if((*i)->variable->scope>scope && variable->scope<=scope)
832                         flags |= OUTPUT;
833                 if((*i)->variable->scope>=scope && variable->scope<scope)
834                         if(!inlined || variable->scope!=ATTRIBUTE || scope!=VERTEX)
835                                 flags |= INPUT;
836         }
837
838         // Variables without any references are goals and also outputs.
839         if(referenced_by.empty() && variable->scope==scope)
840                 flags |= OUTPUT|GOAL;
841
842         return static_cast<InterfaceFlags>(flags);
843 }
844
845 string ProgramBuilder::ShaderVariable::create_declaration(char interface, bool loop) const
846 {
847         if(variable->scope==UNIFORM && !array_subscript.empty())
848         {
849                 const char *bracket = strrchr(variable->type, '[');
850                 if(bracket)
851                         return format("%s %s[%d]", string(variable->type, bracket), resolved_name, array_size);
852         }
853
854         string array;
855         if(!array_sum && array_size>1 && !loop)
856                 array = format("[%d]", array_size);
857
858         if(interface)
859                 return format("%s %c_%s%s", variable->type, interface, resolved_name, array);
860         else
861                 return format("%s %s%s", variable->type, resolved_name, array);
862 }
863
864 string ProgramBuilder::ShaderVariable::create_replacement(VariableScope from_scope, const char *loop) const
865 {
866         string replacement = resolved_name;
867         InterfaceFlags interface = NO_INTERFACE;
868         if(variable)
869         {
870                 interface = get_interface_flags(from_scope);
871                 if((interface&INPUT) && interfaces[from_scope-1])
872                         replacement = format("%c_%s", interfaces[from_scope-1], replacement);
873                 else if(inlined)
874                 {
875                         replacement = create_expression(loop);
876                         if(inline_parens)
877                                 replacement = "("+replacement+")";
878                         return replacement;
879                 }
880         }
881
882         // Add an array subscript, unless the variable is embedded in a loop
883         if(!array_subscript.empty() && !in_loop)
884         {
885                 if(loop)
886                         return format("%s[%s]", replacement, loop);
887                 else if(!variable || variable->scope==UNIFORM)
888                         return replacement+"[0]";
889         }
890
891         return replacement;
892 }
893
894 string ProgramBuilder::ShaderVariable::create_expression(const char *loop) const
895 {
896         map<string, string> replace_map;
897         for(list<ShaderVariable *>::const_iterator i=referenced_vars.begin(); i!=referenced_vars.end(); ++i)
898         {
899                 string replacement = (*i)->create_replacement(variable->scope, loop);
900                 if(replacement!=(*i)->name)
901                         replace_map[(*i)->name] = replacement;
902         }
903
904         if(replace_map.empty())
905                 return variable->expression;
906         else
907                 return replace_identifiers(variable->expression, replace_map);
908 }
909
910
911 ProgramBuilder::StandardFeatures::Loader::Loader(StandardFeatures &f):
912         DataFile::ObjectLoader<StandardFeatures>(f)
913 {
914         add("custom",    &StandardFeatures::custom);
915         add("lighting",  &StandardFeatures::lighting);
916         add("material",  &StandardFeatures::material);
917         add("max_lights", &StandardFeatures::max_lights);
918         add("normalmap", &StandardFeatures::normalmap);
919         add("reflection", &StandardFeatures::reflection);
920         add("shadow",    &StandardFeatures::shadow);
921         add("specular",  &StandardFeatures::specular);
922         add("texture",   &StandardFeatures::texture);
923 }
924
925 } // namespace GL
926 } // namespace Msp