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