]> git.tdb.fi Git - libs/gl.git/blob - source/programbuilder.cpp
Generalize shader interface handling
[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::VariableDefinition ProgramBuilder::standard_variables[] =
43 {
44         { FRAGMENT, "gl_FragColor", "vec4", "frag_color", "g" },
45         { FRAGMENT, "frag_color", "vec4", "color_base", "!t" },
46         { FRAGMENT, "frag_color", "vec4", "tex_sample", "!l!s!mt" },
47         { FRAGMENT, "frag_color", "vec4", "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, "rgb_light_full", "vec3", "rgb_light_shadow", "!m" },
58         { FRAGMENT, "ambient_product_diffuse", "vec4", "gl_FrontLightModelProduct.sceneColor", "g" },
59         { FRAGMENT, "ambient_product_diffuse", "vec4", "ambient_color*material.diffuse", "!g" },
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", 0 },
83         { FRAGMENT, "tex_sample", "vec4", "texture2D(texture, texture_coord)", 0 },
84
85         { VERTEX, "gl_Position", "vec4", "projection_matrix*eye_vertex", 0 },
86         { 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" },
87         { VERTEX, "shd_vertex", "vec3", "(shd_eye_matrix*eye_vertex).xyz", "!g" },
88         { VERTEX, "tbn_light_dir", "vec3", "eye_light_dir*eye_tbn_matrix", 0 },
89         { VERTEX, "eye_light_dir", "vec3", "normalize(eye_light_position.xyz-eye_vertex.xyz*eye_light_position.w)", 0 },
90         { VERTEX, "eye_light_position", "vec4", "light_sources[0].position", 0 },
91         { VERTEX, "tbn_incident_dir", "vec3", "eye_incident_dir*eye_tbn_matrix", 0 },
92         { VERTEX, "eye_incident_dir", "vec3", "normalize(eye_vertex.xyz)", 0 },
93         { VERTEX, "eye_tbn_matrix", "mat3", "mat3(eye_tangent, eye_binormal, eye_normal)", 0 },
94         { VERTEX, "eye_vertex", "vec4", "eye_obj_matrix*vertex", 0 },
95         { VERTEX, "eye_normal", "vec3", "eye_obj_normal_matrix*normal", 0 },
96         { VERTEX, "eye_tangent", "vec3", "eye_obj_normal_matrix*tangent", 0 },
97         { VERTEX, "eye_binormal", "vec3", "eye_obj_normal_matrix*binormal", 0 },
98         { VERTEX, "texture_coord", "vec2", "texcoord.xy", 0 },
99
100         { ATTRIBUTE, "vertex", "vec4", "gl_Vertex", 0 },
101         { ATTRIBUTE, "texcoord", "vec4", "gl_MultiTexCoord0", 0 },
102         { ATTRIBUTE, "color", "vec4", "gl_Color", 0 },
103         { ATTRIBUTE, "normal", "vec3", "gl_Normal", 0 },
104         { ATTRIBUTE, "tangent", "vec3", 0, 0 },
105         { ATTRIBUTE, "binormal", "vec3", 0, 0 },
106
107         { UNIFORM, "shadow_unit", "int", 0, 0 },
108         { UNIFORM, "texture", "sampler2D", 0, 0 },
109         { UNIFORM, "shadow", "sampler2DShadow", 0, 0 },
110         { UNIFORM, "shadow_darkness", "float", 0, 0 },
111         { UNIFORM, "normalmap", "sampler2D", 0, 0 },
112         { UNIFORM, "environment", "samplerCube", 0, 0 },
113         { UNIFORM, "env_eye_matrix", "mat3", 0, 0 },
114         { UNIFORM, "reflectivity", "float", 0, 0 },
115         { UNIFORM, "eye_obj_matrix", "mat4", "gl_ModelViewMatrix", 0 },
116         { UNIFORM, "eye_obj_normal_matrix", "mat3", "gl_NormalMatrix", 0 },
117         { UNIFORM, "projection_matrix", "mat4", "gl_ProjectionMatrix", 0 },
118         { UNIFORM, "shd_eye_matrix", "mat4", 0, 0 },
119         { UNIFORM, "light_sources", "struct { vec4 position; vec4 diffuse; vec4 specular; }[2]", "gl_LightSource", 0 },
120         { UNIFORM, "ambient_color", "vec4", 0, 0 },
121         { UNIFORM, "material", "struct { vec4 ambient; vec4 diffuse; vec4 specular; float shininess; }", "gl_FrontMaterial", 0 },
122
123         // Terminator entry
124         { NO_SCOPE, 0, 0, 0, 0 }
125 };
126
127 const char ProgramBuilder::interfaces[] = { 0, 0, 0, 'v', 0 };
128
129 ProgramBuilder::ProgramBuilder(const StandardFeatures &f):
130         features(f),
131         feature_flags(features.create_flags()),
132         optimize(true)
133 {
134         if(!features.custom.empty())
135         {
136                 const char *whitespace = " \t\n";
137                 string::size_type start = 0;
138                 while(1)
139                 {
140                         start = features.custom.find_first_not_of(whitespace, start);
141                         if(start==string::npos)
142                                 break;
143
144                         string::size_type semicolon = features.custom.find(';', start);
145                         if(semicolon==start)
146                         {
147                                 ++start;
148                                 continue;
149                         }
150                         else if(semicolon==string::npos)
151                                 throw invalid_variable_definition(features.custom.substr(start));
152
153                         string::size_type equals = features.custom.find('=', start);
154                         if(equals>semicolon)
155                                 equals = string::npos;
156
157                         VariableDefinition var;
158                         string::size_type decl_end = min(equals, semicolon);
159                         for(unsigned i=0;; ++i)
160                         {
161                                 string::size_type word_end = features.custom.find_first_of(whitespace, start);
162                                 word_end = min(word_end, decl_end);
163                                 features.custom[word_end] = 0;
164
165                                 const char *word = &features.custom[start];
166                                 if(i==0)
167                                 {
168                                         if(!strcmp(word, "uniform"))
169                                                 var.scope = UNIFORM;
170                                         else if(!strcmp(word, "attribute"))
171                                                 var.scope = ATTRIBUTE;
172                                         else if(!strcmp(word, "vertex"))
173                                                 var.scope = VERTEX;
174                                         else if(!strcmp(word, "fragment"))
175                                                 var.scope = FRAGMENT;
176                                         else
177                                                 throw invalid_variable_definition(word);
178                                 }
179                                 else if(i==1)
180                                         var.type = word;
181                                 else if(i==2)
182                                         var.name = word;
183
184                                 start = features.custom.find_first_not_of(whitespace, word_end+1);
185                                 if(start>=decl_end)
186                                         break;
187                         }
188
189                         if(equals!=string::npos)
190                         {
191                                 start = features.custom.find_first_not_of(whitespace, equals+1);
192                                 if(start>=semicolon)
193                                         throw invalid_variable_definition("no expression");
194                                 features.custom[semicolon] = 0;
195                                 var.expression = &features.custom[start];
196                         }
197                         else
198                                 var.expression = 0;
199
200                         var.flags = 0;
201                         for(const VariableDefinition *j=standard_variables; j->name; ++j)
202                                 if(!strcmp(var.name, j->name))
203                                         var.flags = "o";
204
205                         custom_variables.push_front(var);
206
207                         start = semicolon+1;
208                 }
209         }
210 }
211
212 void ProgramBuilder::set_optimize(bool o)
213 {
214         optimize = o;
215 }
216
217 Program *ProgramBuilder::create_program() const
218 {
219         Program *prog = new Program;
220         add_shaders(*prog);
221         return prog;
222 }
223
224 void ProgramBuilder::add_shaders(Program &prog) const
225 {
226         list<ShaderVariable> variables;
227         list<ShaderVariable *> resolved_vars;
228
229         variables.push_front(ShaderVariable("gl_Position"));
230         variables.push_front(ShaderVariable(features.legacy ? "gl_FragColor" : "frag_color"));
231
232         list<VariableDefinition>::const_iterator next_custom = custom_variables.begin();
233         for(const VariableDefinition *i=standard_variables; i->name; )
234         {
235                 const VariableDefinition *def = 0;
236                 if(next_custom!=custom_variables.end() && (!strcmp(next_custom->name, i->name) || !next_custom->flags))
237                 {
238                         def = &*next_custom;
239                         ++next_custom;
240                 }
241                 else
242                 {
243                         def = i;
244                         ++i;
245
246                         // Skip over anything that isn't used with the supplied flags
247                         if(def->flags && !evaluate_flags(def->flags))
248                                 continue;
249                 }
250
251                 // See if this variable can satisfy any unresolved variables
252                 ShaderVariable *last_resolved = 0;
253                 for(list<ShaderVariable>::iterator j=variables.begin(); j!=variables.end(); ++j)
254                 {
255                         if(j->variable)
256                                 continue;
257
258                         if(!name_match(def->name, j->resolved_name.c_str()))
259                                 continue;
260
261                         if(last_resolved)
262                         {
263                                 /* We've already resolved a non-fuzzy variable in this iteration.
264                                 If there are multiple variables that can be resolved, they refer
265                                 to the same variable. */
266                                 j->resolve(*last_resolved);
267                                 continue;
268                         }
269
270                         j->resolve(*def);
271                         resolved_vars.push_front(&*j);
272                         if(!j->fuzzy_space)
273                                 last_resolved = &*j;
274
275                         if(!def->expression)
276                                 continue;
277
278                         vector<string> identifiers = extract_identifiers(def->expression);
279                         for(vector<string>::const_iterator k=identifiers.begin(); k!=identifiers.end(); ++k)
280                         {
281                                 // Use an existing variable if possible, but only if it's not fuzzy
282                                 ShaderVariable *var = 0;
283                                 for(list<ShaderVariable>::iterator l=variables.begin(); (!var && l!=variables.end()); ++l)
284                                         if(!l->fuzzy_space && l->resolved_name==*k)
285                                                 var = &*l;
286
287                                 if(!var)
288                                 {
289                                         variables.push_back(ShaderVariable(*k));
290                                         var = &variables.back();
291                                 }
292                                 j->add_reference(*var);
293                         }
294                 }
295         }
296
297         for(list<ShaderVariable *>::const_iterator i=resolved_vars.begin(); i!=resolved_vars.end(); ++i)
298                 (*i)->check_inline(features.legacy, !optimize);
299
300         prog.attach_shader_owned(new VertexShader(create_source(resolved_vars, VERTEX)));
301         prog.attach_shader_owned(new FragmentShader(create_source(resolved_vars, FRAGMENT)));
302
303         if(!features.legacy)
304         {
305                 prog.bind_fragment_data(0, "frag_color");
306                 prog.bind_attribute(VERTEX4, "vertex");
307                 if(features.lighting)
308                         prog.bind_attribute(NORMAL3, "normal");
309                 else if(features.material)
310                         prog.bind_attribute(COLOR4_FLOAT, "color");
311                 if(features.texture || features.normalmap)
312                         prog.bind_attribute(TEXCOORD4, "texcoord");
313         }
314         if(features.normalmap)
315         {
316                 prog.bind_attribute(get_component_type(TANGENT3), "tangent");
317                 prog.bind_attribute(get_component_type(BINORMAL3), "binormal");
318         }
319 }
320
321 string ProgramBuilder::create_source(const list<ShaderVariable *> &variables, VariableScope scope) const
322 {
323         string source;
324
325         if(!features.legacy)
326                 source += "#version 130\n";
327
328         for(list<ShaderVariable *>::const_iterator i=variables.begin(); i!=variables.end(); ++i)
329                 if((*i)->variable->scope==UNIFORM && (*i)->is_referenced_from(scope) && !(*i)->inlined)
330                         source += format("uniform %s;\n", (*i)->create_declaration());
331
332         /* Interface variables need to have global declarations. */
333         for(list<ShaderVariable *>::const_iterator i=variables.begin(); i!=variables.end(); ++i)
334         {
335                 if(!(*i)->resolved_name.compare(0, 3, "gl_"))
336                         continue;
337
338                 InterfaceFlags interface = (*i)->get_interface_flags(scope);
339
340                 if(interface&INPUT)
341                 {
342                         const char *qualifier = (features.legacy ? scope==VERTEX ? "attribute" : "varying" : "in");
343                         source += format("%s %s;\n", qualifier, (*i)->create_declaration(interfaces[scope-1]));
344                 }
345
346                 if(interface&OUTPUT)
347                 {
348                         const char *qualifier = (features.legacy ? "varying" : "out");
349                         source += format("%s %s;\n", qualifier, (*i)->create_declaration(interfaces[scope]));
350                 }
351         }
352
353         source += "void main()\n{\n";
354
355         for(list<ShaderVariable *>::const_iterator i=variables.begin(); i!=variables.end(); ++i)
356         {
357                 InterfaceFlags interface = (*i)->get_interface_flags(scope);
358
359                 if((*i)->variable->scope==scope && !(*i)->inlined)
360                 {
361                         string decl = ((interface&GOAL) ? (*i)->resolved_name : (*i)->create_declaration());
362                         source += format("\t%s = %s;\n", decl, (*i)->create_expression());
363                 }
364
365                 if((interface&(OUTPUT|GOAL))==OUTPUT)
366                 {
367                         string expr = ((*i)->inlined ? (*i)->create_expression() : (*i)->resolved_name);
368                         source += format("\t%c_%s = %s;\n", interfaces[scope], (*i)->resolved_name, expr);
369                 }
370         }
371
372         source += '}';
373
374         return source;
375 }
376
377 bool ProgramBuilder::evaluate_flags(const char *flags) const
378 {
379         if(!flags)
380                 return true;
381
382         bool cond = true;
383         char oper = '&';
384         for(const char *i=flags; *i; ++i)
385         {
386                 if(*i>='a' && *i<='z')
387                 {
388                         bool found = (feature_flags.find(*i)!=string::npos);
389                         if(oper=='|')
390                                 cond = (cond || found);
391                         else if(oper=='!')
392                                 cond = (cond && !found);
393                         else if(oper=='&')
394                                 cond = (cond && found);
395                         oper = '&';
396                 }
397                 else
398                         oper = *i;
399         }
400
401         return cond;
402 }
403
404 ProgramBuilder::MatchLevel ProgramBuilder::name_match(const char *n1, const char *n2, const char **space)
405 {
406         int i = 0;
407         int zzz = -1;
408         int zside = 0;
409         while(*n1 && *n2)
410         {
411                 if(*n1==*n2 || *n1=='z' || *n2=='z')
412                 {
413                         if(*n1!=*n2)
414                         {
415                                 int side = (*n1=='z' ? 1 : 2);
416                                 if(zzz<0)
417                                 {
418                                         zzz = i;
419                                         zside = side;
420                                         if(space)
421                                         {
422                                                 if(*n1=='z')
423                                                         *space = n2;
424                                                 else
425                                                         *space = n1;
426                                         }
427                                 }
428                                 else if(i>=zzz+3 || side!=zside)
429                                         return NO_MATCH;
430                         }
431                 }
432                 else
433                         return NO_MATCH;
434                 ++n1;
435                 ++n2;
436                 ++i;
437         }
438         return (!*n1 && !*n2) ? zzz>=0 ? FUZZY : EXACT : NO_MATCH;
439 }
440
441 bool ProgramBuilder::parse_identifier(const char *ptr, unsigned &start, unsigned &length)
442 {
443         bool found = false;
444         bool member = false;
445         for(const char *i=ptr;; ++i)
446         {
447                 if(!found)
448                 {
449                         if(!*i)
450                                 return false;
451                         if(isalpha(*i) || *i=='_')
452                         {
453                                 if(!member)
454                                 {
455                                         start = i-ptr;
456                                         found = true;
457                                 }
458                         }
459                         else if(*i=='.')
460                                 member = true;
461                         else
462                                 member = false;
463                 }
464                 else
465                 {
466                         if(!isalnum(*i) && *i!='_')
467                         {
468                                 length = i-(ptr+start);
469                                 return true;
470                         }
471                 }
472         }
473 }
474
475 vector<string> ProgramBuilder::extract_identifiers(const char *expression)
476 {
477         vector<string> result;
478         const char *ptr = expression;
479         unsigned start = 0;
480         unsigned length = 0;
481         while(parse_identifier(ptr, start, length))
482         {
483                 result.push_back(string(ptr+start, length));
484                 ptr += start+length;
485         }
486         return result;
487 }
488
489 string ProgramBuilder::replace_identifiers(const char *expression, const map<string, string> &replace_map)
490 {
491         string result;
492         const char *ptr = expression;
493         unsigned start = 0;
494         unsigned length = 0;
495         while(parse_identifier(ptr, start, length))
496         {
497                 result.append(ptr, start);
498                 string identifier(ptr+start, length);
499                 map<string, string>::const_iterator i = replace_map.find(identifier);
500                 if(i!=replace_map.end())
501                         result += i->second;
502                 else
503                         result += identifier;
504                 ptr += start+length;
505         }
506         result += ptr;
507         return result;
508 }
509
510
511 ProgramBuilder::StandardFeatures::StandardFeatures():
512         texture(false),
513         material(false),
514         lighting(false),
515         specular(false),
516         normalmap(false),
517         shadow(false),
518         reflection(false),
519         legacy(!(get_glsl_version()>=Version(1, 30)))
520 { }
521
522 string ProgramBuilder::StandardFeatures::create_flags() const
523 {
524         string flags;
525         if(texture)
526                 flags += 't';
527         if(material)
528                 flags += 'm';
529         if(lighting)
530         {
531                 flags += 'l';
532                 if(specular)
533                         flags += 'p';
534                 if(normalmap)
535                         flags += 'n';
536         }
537         if(shadow)
538                 flags += 's';
539         if(reflection)
540                 flags += 'e';
541         if(legacy)
542                 flags += 'g';
543
544         return flags;
545 }
546
547
548 ProgramBuilder::ShaderVariable::ShaderVariable(const std::string &n):
549         name(n),
550         variable(0),
551         resolved_name(n),
552         fuzzy_space(name.find("zzz")!=string::npos),
553         inlined(false),
554         inline_parens(false)
555 { }
556
557 void ProgramBuilder::ShaderVariable::resolve(const VariableDefinition &var)
558 {
559         variable = &var;
560         const char *space = 0;
561         if(name_match(var.name, resolved_name.c_str(), &space)==FUZZY)
562                 resolve_space(string(space, 3));
563
564 }
565
566 void ProgramBuilder::ShaderVariable::resolve(ShaderVariable &var)
567 {
568         for(list<ShaderVariable *>::iterator i=referenced_by.begin(); i!=referenced_by.end(); ++i)
569                 (*i)->update_reference(*this, var);
570         var.referenced_by.insert(var.referenced_by.end(), referenced_by.begin(), referenced_by.end());
571 }
572
573 void ProgramBuilder::ShaderVariable::resolve_space(const string &space)
574 {
575         if(fuzzy_space)
576         {
577                 resolved_space = space;
578
579                 string::size_type zzz = resolved_name.find("zzz");
580                 resolved_name.replace(zzz, 3, resolved_space);
581                 fuzzy_space = false;
582
583                 // Resolving the space could have affected other variables that use this one
584                 for(list<ShaderVariable *>::iterator i=referenced_by.begin(); i!=referenced_by.end(); ++i)
585                         (*i)->resolve_space(space);
586         }
587
588         for(list<ShaderVariable *>::iterator i=referenced_vars.begin(); i!=referenced_vars.end(); ++i)
589                 if((*i)->fuzzy_space)
590                         (*i)->resolve_space(space);
591 }
592
593 void ProgramBuilder::ShaderVariable::add_reference(ShaderVariable &var)
594 {
595         referenced_vars.push_back(&var);
596         var.referenced_by.push_back(this);
597         if(var.fuzzy_space && !resolved_space.empty())
598                 var.resolve_space(resolved_space);
599 }
600
601 void ProgramBuilder::ShaderVariable::update_reference(ShaderVariable &from, ShaderVariable &to)
602 {
603         replace(referenced_vars.begin(), referenced_vars.end(), &from, &to);
604         replace(referenced_by.begin(), referenced_by.end(), &from, &to);
605         if(from.fuzzy_space && !to.fuzzy_space && !to.resolved_space.empty())
606                 resolve_space(to.resolved_space);
607 }
608
609 void ProgramBuilder::ShaderVariable::check_inline(bool allow_legacy, bool trivial_only)
610 {
611         if(variable->expression)
612         {
613                 if(!allow_legacy && !strncmp(variable->expression, "gl_", 3))
614                         return;
615
616                 // Never inline goal variables
617                 unsigned total_refs = referenced_by.size();
618                 if(!total_refs)
619                         return;
620
621                 // Inline an expression consisting of a single identifier
622                 unsigned start, length;
623                 if(parse_identifier(variable->expression, start, length))
624                         if(start==0 && variable->expression[length]==0)
625                         {
626                                 inlined = true;
627                                 return;
628                         }
629
630                 if(trivial_only)
631                         return;
632
633                 unsigned in_scope_refs = 0;
634                 for(list<ShaderVariable *>::const_iterator i=referenced_by.begin(); i!=referenced_by.end(); ++i)
635                         if((*i)->variable->scope==variable->scope)
636                                 ++in_scope_refs;
637
638                 /* Inline if there's only one ref, or if all refs are in other scopes.
639                 In the latter case, the actual inlining will happen in the interface
640                 variable assignment. */
641                 if(total_refs==1 || in_scope_refs==0)
642                 {
643                         inlined = true;
644                         unsigned level = 0;
645                         for(const char *c=variable->expression; (!inline_parens && *c); ++c)
646                         {
647                                 if(*c=='(')
648                                         ++level;
649                                 else if(*c==')')
650                                         --level;
651                                 else if(level==0 && !isalnum(*c) && *c!='_' && *c!='.')
652                                         inline_parens = true;
653                         }
654                 }
655         }
656 }
657
658 bool ProgramBuilder::ShaderVariable::is_referenced_from(VariableScope scope) const
659 {
660         for(list<ShaderVariable *>::const_iterator i=referenced_by.begin(); i!=referenced_by.end(); ++i)
661                 if((*i)->variable->scope==scope)
662                         return true;
663         return false;
664 }
665
666 ProgramBuilder::InterfaceFlags ProgramBuilder::ShaderVariable::get_interface_flags(VariableScope scope) const
667 {
668         /* Uniforms are available to all stages and are not passed through
669         interfaces */
670         if(variable->scope==UNIFORM)
671                 return NO_INTERFACE;
672
673         int flags = NO_INTERFACE;
674
675         for(list<ShaderVariable *>::const_iterator i=referenced_by.begin(); i!=referenced_by.end(); ++i)
676         {
677                 /* Variables used in a later scope than they are declared in need to go
678                 through the interface */
679                 if((*i)->variable->scope>scope && variable->scope<=scope)
680                         flags |= OUTPUT;
681                 if((*i)->variable->scope>=scope && variable->scope<scope)
682                         if(!inlined || variable->scope!=ATTRIBUTE || scope!=VERTEX)
683                                 flags |= INPUT;
684         }
685
686         // Variables without any references are goals and also outputs.
687         if(referenced_by.empty() && variable->scope==scope)
688                 flags |= OUTPUT|GOAL;
689
690         return static_cast<InterfaceFlags>(flags);
691 }
692
693 string ProgramBuilder::ShaderVariable::create_declaration(char interface) const
694 {
695         if(variable->scope==UNIFORM)
696         {
697                 const char *bracket = strrchr(variable->type, '[');
698                 if(bracket)
699                         return format("%s %s%s", string(variable->type, bracket), resolved_name, bracket);
700         }
701
702         if(interface)
703                 return format("%s %c_%s", variable->type, interface, resolved_name);
704         else
705                 return format("%s %s", variable->type, resolved_name);
706 }
707
708 string ProgramBuilder::ShaderVariable::create_replacement(VariableScope from_scope) const
709 {
710         string replacement = resolved_name;
711         if(variable)
712         {
713                 InterfaceFlags interface = get_interface_flags(from_scope);
714                 if((interface&INPUT) && interfaces[from_scope-1])
715                         replacement = format("%c_%s", interfaces[from_scope-1], replacement);
716                 else if(inlined)
717                 {
718                         replacement = create_expression();
719                         if(inline_parens)
720                                 replacement = "("+replacement+")";
721                 }
722         }
723
724         return replacement;
725 }
726
727 string ProgramBuilder::ShaderVariable::create_expression() const
728 {
729         map<string, string> replace_map;
730         for(list<ShaderVariable *>::const_iterator i=referenced_vars.begin(); i!=referenced_vars.end(); ++i)
731         {
732                 string replacement = (*i)->create_replacement(variable->scope);
733                 if(replacement!=(*i)->name)
734                         replace_map[(*i)->name] = replacement;
735         }
736
737         if(replace_map.empty())
738                 return variable->expression;
739         else
740                 return replace_identifiers(variable->expression, replace_map);
741 }
742
743
744 ProgramBuilder::StandardFeatures::Loader::Loader(StandardFeatures &f):
745         DataFile::ObjectLoader<StandardFeatures>(f)
746 {
747         add("custom",    &StandardFeatures::custom);
748         add("lighting",  &StandardFeatures::lighting);
749         add("material",  &StandardFeatures::material);
750         add("normalmap", &StandardFeatures::normalmap);
751         add("reflection", &StandardFeatures::reflection);
752         add("shadow",    &StandardFeatures::shadow);
753         add("specular",  &StandardFeatures::specular);
754         add("texture",   &StandardFeatures::texture);
755 }
756
757 } // namespace GL
758 } // namespace Msp