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