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