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