]> git.tdb.fi Git - libs/gl.git/blob - source/programbuilder.cpp
Add an overload of bind_attribute that takes a VertexComponent
[libs/gl.git] / source / programbuilder.cpp
1 #include <algorithm>
2 #include <cstring>
3 #include <msp/strings/format.h>
4 #include "extension.h"
5 #include "program.h"
6 #include "programbuilder.h"
7 #include "shader.h"
8 #include "vertexformat.h"
9
10 using namespace std;
11
12 namespace Msp {
13 namespace GL {
14
15 /*
16 Naming conventions:
17   n_*        Normalized vector
18   l_*        Lighting component
19
20   obj_*      Object space
21   eye_*      Eye space
22   tbn_*      Tangent-Binormal-Normal space
23   shd_*      Shadow space
24   env_*      Environment space
25   *_dir      Direction vector
26
27   zzz_*      Wildcard space, resolved by the builder
28              All wildcard spaces within an expression must match
29
30   xxx_yyy_*  Matrix that transforms between yyy to xxx
31              The vector is on the side of its designated space, result will be
32              in the other space
33   *_matrix   A matrix (duh)
34   *_rmatrix  A mat4 that works with a row vector
35
36   rgb_*      Color with rgb components only
37   color_*    Color with rgba components
38 */
39
40 /* The array are stored in reverse order, so that variables always come after
41 anything that might need them. */
42 const ProgramBuilder::StandardVariable ProgramBuilder::standard_variables[] =
43 {
44         { FRAGMENT, "gl_FragColor", 0, "frag_color", "g" },
45         { FRAGMENT, "frag_color", 0, "color_base", "!t" },
46         { FRAGMENT, "frag_color", 0, "tex_sample", "!l!s!mt" },
47         { FRAGMENT, "frag_color", 0, "tex_sample*color_base", "l|s|mt" },
48         { FRAGMENT, "color_base", "vec4", "vec4(1.0)", "!l!s!m" },
49         { FRAGMENT, "color_base", "vec4", "color", "!l!sm" },
50         { FRAGMENT, "color_base", "vec4", "vec4(vec3(l_shadow), 1.0)", "!ls!m" },
51         { FRAGMENT, "color_base", "vec4", "color*vec4(vec3(l_shadow), 1.0)", "!lsm" },
52         { FRAGMENT, "color_base", "vec4", "vec4(rgb_light_env, 1.0)", "l!m" },
53         { FRAGMENT, "color_base", "vec4", "vec4(rgb_light_env, material.diffuse.a)", "lm" },
54         { FRAGMENT, "rgb_light_env", "vec3", "rgb_light_full+reflect_sample.rgb*reflectivity", "e" },
55         { FRAGMENT, "rgb_light_env", "vec3", "rgb_light_full", "!e" },
56         { FRAGMENT, "rgb_light_full", "vec3", "rgb_light_shadow+ambient_product_diffuse.rgb", "m" },
57         { FRAGMENT, "ambient_product_diffuse", "vec4", "gl_FrontLightModelProduct.sceneColor", "g" },
58         { FRAGMENT, "ambient_product_diffuse", "vec4", "ambient_color*material.diffuse", "!g" },
59         { FRAGMENT, "rgb_light_full", "vec3", "rgb_light_shadow", "!m" },
60         { FRAGMENT, "rgb_light_shadow", "vec3", "rgb_light*l_shadow", "s" },
61         { FRAGMENT, "rgb_light_shadow", "vec3", "rgb_light", "!s" },
62         { FRAGMENT, "rgb_light", "vec3", "vec3(l_diffuse)", "!m!p" },
63         { FRAGMENT, "rgb_light", "vec3", "vec3(l_diffuse+l_specular)", "!mp" },
64         { FRAGMENT, "rgb_light", "vec3", "l_diffuse*light_product_diffuse.rgb", "m!p" },
65         { FRAGMENT, "rgb_light", "vec3", "l_diffuse*light_product_diffuse.rgb+l_specular*light_product_specular.rgb", "mp" },
66         { FRAGMENT, "light_product_diffuse", "vec4", "gl_FrontLightProduct[0].diffuse", "g" },
67         { FRAGMENT, "light_product_diffuse", "vec4", "light_sources[0].diffuse*material.diffuse", "!g" },
68         { FRAGMENT, "light_product_specular", "vec4", "gl_FrontLightProduct[0].specular", "g" },
69         { FRAGMENT, "light_product_specular", "vec4", "light_sources[0].specular*material.specular", "!g" },
70         { FRAGMENT, "reflect_sample", "vec4", "textureCube(environment, env_reflect_dir)", 0 },
71         { FRAGMENT, "env_reflect_dir", "vec3", "env_eye_matrix*eye_reflect_dir", 0 },
72         { FRAGMENT, "eye_reflect_dir", "vec3", "eye_tbn_matrix*tbn_reflect_dir", "n" },
73         { FRAGMENT, "zzz_reflect_dir", "vec3", "reflect(zzz_incident_dir, n_zzz_normal)", 0 },
74         { FRAGMENT, "l_shadow", "float", "mix(1.0, shadow_sample, shadow_darkness)", 0 },
75         { FRAGMENT, "shadow_sample", "float", "shadow2D(shadow, shd_vertex).r", 0 },
76         { FRAGMENT, "l_diffuse", "float", "max(dot(n_zzz_normal, n_zzz_light_dir), 0.0)", 0 },
77         { FRAGMENT, "l_specular", "float", "pow(max(dot(n_zzz_half_vec, n_zzz_normal), 0.0), material.shininess)", 0 },
78         { FRAGMENT, "n_zzz_half_vec", "vec3", "normalize(zzz_light_dir-zzz_incident_dir)", 0 },
79         { FRAGMENT, "n_zzz_light_dir", "vec3", "normalize(zzz_light_dir)", 0 },
80         { FRAGMENT, "n_tbn_normal", "vec3", "normal_sample*2.0-1.0", "n" },
81         { FRAGMENT, "n_eye_normal", "vec3", "normalize(eye_normal)", "!n" },
82         { FRAGMENT, "normal_sample", "vec3", "texture2D(normalmap, texture_coord).xyz", "!c" },
83         { FRAGMENT, "normal_sample", "vec3", "sample_normalmap(texture_coord)", "c" },
84         { FRAGMENT, "tex_sample", "vec4", "texture2D(texture, texture_coord)", "!c" },
85         { FRAGMENT, "tex_sample", "vec4", "sample_texture(texture_coord)", "c" },
86
87         { VERTEX, "gl_Position", 0, "projection_matrix*eye_vertex", 0 },
88         { 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" },
89         { VERTEX, "shd_vertex", "vec3", "(shd_eye_matrix*eye_vertex).xyz", "!g" },
90         { VERTEX, "tbn_light_dir", "vec3", "eye_light_dir*eye_tbn_matrix", 0 },
91         { VERTEX, "eye_light_dir", "vec3", "normalize(eye_light_position.xyz-eye_vertex.xyz*eye_light_position.w)", 0 },
92         { VERTEX, "eye_light_position", "vec4", "light_sources[0].position", 0 },
93         { VERTEX, "tbn_incident_dir", "vec3", "eye_incident_dir*eye_tbn_matrix", 0 },
94         { VERTEX, "eye_incident_dir", "vec3", "normalize(eye_vertex.xyz)", 0 },
95         { VERTEX, "eye_tbn_matrix", "mat3", "mat3(eye_tangent, eye_binormal, eye_normal)", 0 },
96         { VERTEX, "eye_vertex", "vec4", "eye_obj_matrix*vertex", "!r" },
97         { VERTEX, "eye_vertex", "vec4", "transform_vertex(vertex)", "r" },
98         { VERTEX, "eye_normal", "vec3", "eye_obj_normal_matrix*normal", "!r" },
99         { VERTEX, "eye_normal", "vec3", "transform_normal(normal)", "r" },
100         { VERTEX, "eye_tangent", "vec3", "eye_obj_normal_matrix*tangent", "!r" },
101         { VERTEX, "eye_tangent", "vec3", "transform_normal(tangent)", "r" },
102         { VERTEX, "eye_binormal", "vec3", "eye_obj_normal_matrix*binormal", "!r" },
103         { VERTEX, "eye_binormal", "vec3", "transform_normal(binormal)", "r" },
104         { VERTEX, "texture_coord", "vec2", "texcoord.xy", 0 },
105
106         { ATTRIBUTE, "vertex", "vec4", "gl_Vertex", 0 },
107         { ATTRIBUTE, "texcoord", "vec4", "gl_MultiTexCoord0", 0 },
108         { ATTRIBUTE, "color", "vec4", "gl_Color", 0 },
109         { ATTRIBUTE, "normal", "vec3", "gl_Normal", 0 },
110         { ATTRIBUTE, "tangent", "vec3", 0, 0 },
111         { ATTRIBUTE, "binormal", "vec3", 0, 0 },
112
113         { UNIFORM, "shadow_unit", "int", 0, 0 },
114         { UNIFORM, "texture", "sampler2D", 0, 0 },
115         { UNIFORM, "shadow", "sampler2DShadow", 0, 0 },
116         { UNIFORM, "shadow_darkness", "float", 0, 0 },
117         { UNIFORM, "normalmap", "sampler2D", 0, 0 },
118         { UNIFORM, "environment", "samplerCube", 0, 0 },
119         { UNIFORM, "env_eye_matrix", "mat3", 0, 0 },
120         { UNIFORM, "reflectivity", "float", 0, 0 },
121         { UNIFORM, "eye_obj_matrix", "mat4", "gl_ModelViewMatrix", 0 },
122         { UNIFORM, "eye_obj_normal_matrix", "mat3", "gl_NormalMatrix", 0 },
123         { UNIFORM, "projection_matrix", "mat4", "gl_ProjectionMatrix", 0 },
124         { UNIFORM, "shd_eye_matrix", "mat4", 0, "!g" },
125         { UNIFORM, "light_sources", "struct { vec4 position; vec4 diffuse; vec4 specular; } %s[2]", "gl_LightSource", 0 },
126         { UNIFORM, "ambient_color", "vec4", 0, 0 },
127         { UNIFORM, "material", "struct { vec4 ambient; vec4 diffuse; vec4 specular; float shininess; }", "gl_FrontMaterial", 0 },
128
129         // Terminator entry
130         { NO_SCOPE,  0, 0, 0, 0 }
131 };
132
133 ProgramBuilder::ProgramBuilder(const StandardFeatures &f):
134         features(f),
135         feature_flags(features.create_flags()),
136         optimize(true)
137 { }
138
139 void ProgramBuilder::set_optimize(bool o)
140 {
141         optimize = o;
142 }
143
144 Program *ProgramBuilder::create_program() const
145 {
146         Program *prog = new Program;
147         add_shaders(*prog);
148         return prog;
149 }
150
151 void ProgramBuilder::add_shaders(Program &prog) const
152 {
153         list<ShaderVariable> variables;
154         list<ShaderVariable *> resolved_vars;
155
156         variables.push_front(ShaderVariable("gl_Position"));
157         variables.push_front(ShaderVariable(features.legacy ? "gl_FragColor" : "frag_color"));
158
159         for(const StandardVariable *i=standard_variables; i->name; ++i)
160         {
161                 // Skip over anything that isn't used with the supplied flags
162                 if(i->flags && !evaluate_flags(i->flags))
163                         continue;
164
165                 // See if this variable can satisfy any unresolved variables
166                 ShaderVariable *last_resolved = 0;
167                 for(list<ShaderVariable>::iterator j=variables.begin(); j!=variables.end(); ++j)
168                 {
169                         if(j->variable)
170                                 continue;
171
172                         if(!name_match(i->name, j->resolved_name.c_str()))
173                                 continue;
174
175                         if(last_resolved)
176                         {
177                                 /* We've already resolved a non-fuzzy variable in this iteration.
178                                 If there are multiple variables that can be resolved, they refer
179                                 to the same variable. */
180                                 j->resolve(*last_resolved);
181                                 continue;
182                         }
183
184                         j->resolve(*i);
185                         resolved_vars.push_front(&*j);
186                         if(!j->fuzzy_space)
187                                 last_resolved = &*j;
188
189                         if(!i->expression)
190                                 continue;
191
192                         vector<string> identifiers = extract_identifiers(i->expression);
193                         for(vector<string>::const_iterator k=identifiers.begin(); k!=identifiers.end(); ++k)
194                         {
195                                 // Use an existing variable if possible, but only if it's not fuzzy
196                                 ShaderVariable *var = 0;
197                                 for(list<ShaderVariable>::iterator l=variables.begin(); (!var && l!=variables.end()); ++l)
198                                         if(!l->fuzzy_space && l->resolved_name==*k)
199                                                 var = &*l;
200
201                                 if(!var)
202                                 {
203                                         variables.push_back(ShaderVariable(*k));
204                                         var = &variables.back();
205                                 }
206                                 j->add_reference(*var);
207                         }
208                 }
209         }
210
211         if(optimize)
212         {
213                 for(list<ShaderVariable *>::const_iterator i=resolved_vars.begin(); i!=resolved_vars.end(); ++i)
214                         (*i)->check_inline(features.legacy);
215         }
216
217         prog.attach_shader_owned(new VertexShader(create_source(resolved_vars, VERTEX)));
218         prog.attach_shader_owned(new FragmentShader(create_source(resolved_vars, FRAGMENT)));
219
220         if(!features.legacy)
221         {
222                 prog.bind_fragment_data(0, "frag_color");
223                 prog.bind_attribute(VERTEX4, "vertex");
224                 if(features.lighting)
225                         prog.bind_attribute(NORMAL3, "normal");
226                 else if(features.material)
227                         prog.bind_attribute(COLOR4_FLOAT, "color");
228                 if(features.texture || features.normalmap)
229                         prog.bind_attribute(TEXCOORD4, "texcoord");
230         }
231         if(features.normalmap)
232         {
233                 prog.bind_attribute(get_component_type(TANGENT3), "tangent");
234                 prog.bind_attribute(get_component_type(BINORMAL3), "binormal");
235         }
236 }
237
238 string ProgramBuilder::create_source(const list<ShaderVariable *> &variables, VariableScope scope) const
239 {
240         string source;
241
242         if(!features.legacy)
243                 source += "#version 130\n";
244
245         for(list<ShaderVariable *>::const_iterator i=variables.begin(); i!=variables.end(); ++i)
246                 if((*i)->variable->scope==UNIFORM && (*i)->is_referenced_from(scope) && !(*i)->inlined)
247                 {
248                         if(strchr((*i)->variable->type, '%'))
249                                 source += format("uniform %s;\n", format((*i)->variable->type, (*i)->resolved_name));
250                         else
251                                 source += format("uniform %s %s;\n", (*i)->variable->type, (*i)->resolved_name);
252                 }
253
254         if(scope==VERTEX)
255         {
256                 const char *qualifier = (features.legacy ? "attribute" : "in");
257                 for(list<ShaderVariable *>::const_iterator i=variables.begin(); i!=variables.end(); ++i)
258                         if((*i)->variable->scope==ATTRIBUTE && !(*i)->inlined)
259                                 source += format("%s %s %s;\n", qualifier, (*i)->variable->type, (*i)->resolved_name);
260         }
261
262         /* Any variables defined in vertex scope but referenced from fragment scope
263         should be exported as varyings over the interface. */
264         list<ShaderVariable *> varyings;
265         for(list<ShaderVariable *>::const_iterator i=variables.begin(); i!=variables.end(); ++i)
266                 if(((*i)->variable->scope==VERTEX || (*i)->variable->scope==ATTRIBUTE) && (*i)->is_referenced_from(FRAGMENT))
267                 {
268                         varyings.push_back(*i);
269                         const char *qualifier;
270                         if(!features.legacy)
271                                 qualifier = (scope==VERTEX ? "out" : "in");
272                         else
273                                 qualifier = "varying";
274                         source += format("%s %s v_%s;\n", qualifier, (*i)->variable->type, (*i)->resolved_name);
275                 }
276
277         if(scope==FRAGMENT && !features.legacy)
278                 source += "out vec4 frag_color;\n";
279
280         if(scope==VERTEX && features.transform)
281         {
282                 // Add the prototypes here, until I come up with something better
283                 source += "vec4 transform_vertex(vec4);\n";
284                 source += "vec3 transform_normal(vec3);\n";
285         }
286         else if(scope==FRAGMENT && features.colorify)
287         {
288                 if(features.texture)
289                         source += "vec4 sample_texture(vec2);\n";
290                 if(features.normalmap)
291                         source += "vec3 sample_normalmap(vec2);\n";
292         }
293
294         source += "void main()\n{\n";
295
296         for(list<ShaderVariable *>::const_iterator i=variables.begin(); i!=variables.end(); ++i)
297                 if((*i)->variable->scope==scope && !(*i)->inlined)
298                 {
299                         source += '\t';
300                         if((*i)->variable->type)
301                         {
302                                 source += (*i)->variable->type;
303                                 source += ' ';
304                         }
305                         source += format("%s = %s;\n", (*i)->resolved_name, (*i)->get_expression());
306                 }
307
308         if(scope==VERTEX)
309         {
310                 for(list<ShaderVariable *>::const_iterator i=varyings.begin(); i!=varyings.end(); ++i)
311                 {
312                         if((*i)->inlined)
313                                 source += format("\tv_%s = %s;\n", (*i)->resolved_name, (*i)->get_expression());
314                         else
315                                 source += format("\tv_%s = %s;\n", (*i)->resolved_name, (*i)->resolved_name);
316                 }
317         }
318
319         source += '}';
320
321         return source;
322 }
323
324 bool ProgramBuilder::evaluate_flags(const char *flags) const
325 {
326         if(!flags)
327                 return true;
328
329         bool cond = true;
330         char oper = '&';
331         for(const char *i=flags; *i; ++i)
332         {
333                 if(*i>='a' && *i<='z')
334                 {
335                         bool found = (feature_flags.find(*i)!=string::npos);
336                         if(oper=='|')
337                                 cond = (cond || found);
338                         else if(oper=='!')
339                                 cond = (cond && !found);
340                         else if(oper=='&')
341                                 cond = (cond && found);
342                         oper = '&';
343                 }
344                 else
345                         oper = *i;
346         }
347
348         return cond;
349 }
350
351 ProgramBuilder::MatchLevel ProgramBuilder::name_match(const char *n1, const char *n2, const char **space)
352 {
353         int i = 0;
354         int zzz = -1;
355         int zside = 0;
356         while(*n1 && *n2)
357         {
358                 if(*n1==*n2 || *n1=='z' || *n2=='z')
359                 {
360                         if(*n1!=*n2)
361                         {
362                                 int side = (*n1=='z' ? 1 : 2);
363                                 if(zzz<0)
364                                 {
365                                         zzz = i;
366                                         zside = side;
367                                         if(space)
368                                         {
369                                                 if(*n1=='z')
370                                                         *space = n2;
371                                                 else
372                                                         *space = n1;
373                                         }
374                                 }
375                                 else if(i>=zzz+3 || side!=zside)
376                                         return NO_MATCH;
377                         }
378                 }
379                 else
380                         return NO_MATCH;
381                 ++n1;
382                 ++n2;
383                 ++i;
384         }
385         return (!*n1 && !*n2) ? zzz>=0 ? FUZZY : EXACT : NO_MATCH;
386 }
387
388 bool ProgramBuilder::parse_identifier(const char *ptr, unsigned &start, unsigned &length)
389 {
390         bool found = false;
391         bool member = false;
392         for(const char *i=ptr;; ++i)
393         {
394                 if(!found)
395                 {
396                         if(!*i)
397                                 return false;
398                         if(isalpha(*i) || *i=='_')
399                         {
400                                 if(!member)
401                                 {
402                                         start = i-ptr;
403                                         found = true;
404                                 }
405                         }
406                         else if(*i=='.')
407                                 member = true;
408                         else
409                                 member = false;
410                 }
411                 else
412                 {
413                         if(!isalnum(*i) && *i!='_')
414                         {
415                                 length = i-(ptr+start);
416                                 return true;
417                         }
418                 }
419         }
420 }
421
422 vector<string> ProgramBuilder::extract_identifiers(const char *expression)
423 {
424         vector<string> result;
425         const char *ptr = expression;
426         unsigned start = 0;
427         unsigned length = 0;
428         while(parse_identifier(ptr, start, length))
429         {
430                 result.push_back(string(ptr+start, length));
431                 ptr += start+length;
432         }
433         return result;
434 }
435
436 string ProgramBuilder::replace_identifiers(const char *expression, const map<string, string> &replace_map)
437 {
438         string result;
439         const char *ptr = expression;
440         unsigned start = 0;
441         unsigned length = 0;
442         while(parse_identifier(ptr, start, length))
443         {
444                 result.append(ptr, start);
445                 string identifier(ptr+start, length);
446                 map<string, string>::const_iterator i = replace_map.find(identifier);
447                 if(i!=replace_map.end())
448                         result += i->second;
449                 else
450                         result += identifier;
451                 ptr += start+length;
452         }
453         result += ptr;
454         return result;
455 }
456
457
458 ProgramBuilder::StandardFeatures::StandardFeatures():
459         texture(false),
460         material(false),
461         lighting(false),
462         specular(false),
463         normalmap(false),
464         shadow(false),
465         reflection(false),
466         transform(false),
467         colorify(false),
468         legacy(!(get_glsl_version()>=Version(1, 30)))
469 { }
470
471 string ProgramBuilder::StandardFeatures::create_flags() const
472 {
473         string flags;
474         if(texture)
475                 flags += 't';
476         if(material)
477                 flags += 'm';
478         if(lighting)
479         {
480                 flags += 'l';
481                 if(specular)
482                         flags += 'p';
483                 if(normalmap)
484                         flags += 'n';
485         }
486         if(shadow)
487                 flags += 's';
488         if(reflection)
489                 flags += 'e';
490         if(transform)
491                 flags += 'r';
492         if(colorify)
493                 flags += 'c';
494         if(legacy)
495                 flags += 'g';
496
497         return flags;
498 }
499
500
501 ProgramBuilder::ShaderVariable::ShaderVariable(const std::string &n):
502         name(n),
503         variable(0),
504         resolved_name(n),
505         fuzzy_space(name.find("zzz")!=string::npos),
506         inlined(false),
507         inline_parens(false)
508 { }
509
510 void ProgramBuilder::ShaderVariable::resolve(const StandardVariable &var)
511 {
512         variable = &var;
513         const char *space = 0;
514         if(name_match(var.name, resolved_name.c_str(), &space)==FUZZY)
515                 resolve_space(string(space, 3));
516 }
517
518 void ProgramBuilder::ShaderVariable::resolve(ShaderVariable &var)
519 {
520         for(list<ShaderVariable *>::iterator i=referenced_by.begin(); i!=referenced_by.end(); ++i)
521                 (*i)->update_reference(*this, var);
522         var.referenced_by.insert(var.referenced_by.end(), referenced_by.begin(), referenced_by.end());
523 }
524
525 void ProgramBuilder::ShaderVariable::resolve_space(const string &space)
526 {
527         if(fuzzy_space)
528         {
529                 resolved_space = space;
530
531                 string::size_type zzz = resolved_name.find("zzz");
532                 resolved_name.replace(zzz, 3, resolved_space);
533                 fuzzy_space = false;
534
535                 // Resolving the space could have affected other variables that use this one
536                 for(list<ShaderVariable *>::iterator i=referenced_by.begin(); i!=referenced_by.end(); ++i)
537                         (*i)->resolve_space(space);
538         }
539
540         for(list<ShaderVariable *>::iterator i=referenced_vars.begin(); i!=referenced_vars.end(); ++i)
541                 if((*i)->fuzzy_space)
542                         (*i)->resolve_space(space);
543 }
544
545 void ProgramBuilder::ShaderVariable::add_reference(ShaderVariable &var)
546 {
547         referenced_vars.push_back(&var);
548         var.referenced_by.push_back(this);
549         if(var.fuzzy_space && !resolved_space.empty())
550                 var.resolve_space(resolved_space);
551 }
552
553 void ProgramBuilder::ShaderVariable::update_reference(ShaderVariable &from, ShaderVariable &to)
554 {
555         replace(referenced_vars.begin(), referenced_vars.end(), &from, &to);
556         replace(referenced_by.begin(), referenced_by.end(), &from, &to);
557         if(from.fuzzy_space && !to.fuzzy_space && !to.resolved_space.empty())
558                 resolve_space(to.resolved_space);
559 }
560
561 void ProgramBuilder::ShaderVariable::check_inline(bool allow_legacy)
562 {
563         if(variable->expression)
564         {
565                 if(!allow_legacy && !strncmp(variable->expression, "gl_", 3))
566                         return;
567
568                 unsigned total_refs = referenced_by.size();
569                 unsigned in_scope_refs = 0;
570                 for(list<ShaderVariable *>::const_iterator i=referenced_by.begin(); i!=referenced_by.end(); ++i)
571                         if((*i)->variable->scope==variable->scope)
572                                 ++in_scope_refs;
573                 if(total_refs==1 || (total_refs>0 && in_scope_refs==0))
574                 {
575                         inlined = true;
576                         unsigned level = 0;
577                         for(const char *c=variable->expression; (!inline_parens && *c); ++c)
578                         {
579                                 if(*c=='(')
580                                         ++level;
581                                 else if(*c==')')
582                                         --level;
583                                 else if(level==0 && !isalnum(*c) && *c!='_' && *c!='.')
584                                         inline_parens = true;
585                         }
586                 }
587         }
588 }
589
590 bool ProgramBuilder::ShaderVariable::is_referenced_from(VariableScope scope) const
591 {
592         for(list<ShaderVariable *>::const_iterator i=referenced_by.begin(); i!=referenced_by.end(); ++i)
593                 if((*i)->variable->scope==scope)
594                         return true;
595         return false;
596 }
597
598 string ProgramBuilder::ShaderVariable::get_expression() const
599 {
600         map<string, string> replace_map;
601         for(list<ShaderVariable *>::const_iterator i=referenced_vars.begin(); i!=referenced_vars.end(); ++i)
602                 if((*i)->variable)
603                 {
604                         string replacement = (*i)->resolved_name;
605                         if(variable->scope==FRAGMENT && ((*i)->variable->scope==VERTEX || (*i)->variable->scope==ATTRIBUTE))
606                                 replacement = "v_"+replacement;
607                         else if((*i)->inlined)
608                         {
609                                 replacement = (*i)->get_expression();
610                                 if((*i)->inline_parens)
611                                         replacement = "("+replacement+")";
612                         }
613                         if(replacement!=(*i)->name)
614                                 replace_map[(*i)->name] = replacement;
615                 }
616
617         if(replace_map.empty())
618                 return variable->expression;
619         else
620                 return replace_identifiers(variable->expression, replace_map);
621 }
622
623
624 ProgramBuilder::StandardFeatures::Loader::Loader(StandardFeatures &f):
625         DataFile::ObjectLoader<StandardFeatures>(f)
626 {
627         add("colorify",  &StandardFeatures::colorify);
628         add("lighting",  &StandardFeatures::lighting);
629         add("material",  &StandardFeatures::material);
630         add("normalmap", &StandardFeatures::normalmap);
631         add("reflection", &StandardFeatures::reflection);
632         add("shadow",    &StandardFeatures::shadow);
633         add("specular",  &StandardFeatures::specular);
634         add("texture",   &StandardFeatures::texture);
635         add("transform", &StandardFeatures::transform);
636 }
637
638 } // namespace GL
639 } // namespace Msp