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