]> git.tdb.fi Git - libs/gl.git/blob - source/programbuilder.cpp
Compatibility fixes
[libs/gl.git] / source / programbuilder.cpp
1 #include <algorithm>
2 #include <msp/strings/format.h>
3 #include "program.h"
4 #include "programbuilder.h"
5 #include "shader.h"
6 #include "vertexformat.h"
7
8 using namespace std;
9
10 namespace Msp {
11 namespace GL {
12
13 /*
14 Naming conventions:
15   n_*        Normalized vector
16   l_*        Lighting component
17
18   obj_*      Object space
19   eye_*      Eye space
20   tbn_*      Tangent-Binormal-Normal space
21   shd_*      Shadow space
22   *_dir      Direction vector
23
24   zzz_*      Wildcard space, resolved by the builder
25              All wildcard spaces within an expression must match
26
27   xxx_yyy_*  Matrix that transforms between yyy to xxx
28              The vector is on the side of its designated space, result will be
29              in the other space
30   *_matrix   A matrix (duh)
31   *_rmatrix  A mat4 that works with a row vector
32
33   rgb_*      Color with rgb components only
34   color_*    Color with rgba components
35 */
36
37 /* The array are stored in reverse order, so that variables always come after
38 anything that might need them. */
39 const ProgramBuilder::StandardVariable ProgramBuilder::standard_variables[] =
40 {
41         { FRAGMENT, "gl_FragColor", 0, "color_base", "!t" },
42         { FRAGMENT, "gl_FragColor", 0, "tex_sample", "!l!s!mt" },
43         { FRAGMENT, "gl_FragColor", 0, "tex_sample*color_base", "l|s|mt" },
44         { FRAGMENT, "color_base", "vec4", "vec4(1.0)", "!l!s!m" },
45         { FRAGMENT, "color_base", "vec4", "color", "!l!sm" },
46         { FRAGMENT, "color_base", "vec4", "vec4(vec3(l_shadow), 1.0)", "!ls!m" },
47         { FRAGMENT, "color_base", "vec4", "color*vec4(vec3(l_shadow), 1.0)", "!lsm" },
48         { FRAGMENT, "color_base", "vec4", "vec4(rgb_light_full, 1.0)", "l!m" },
49         { FRAGMENT, "color_base", "vec4", "vec4(rgb_light_full, gl_FrontMaterial.diffuse.a)", "lm" },
50         { FRAGMENT, "rgb_light_full", "vec3", "rgb_light_shadow+gl_FrontLightModelProduct.sceneColor.rgb", "m" },
51         { FRAGMENT, "rgb_light_full", "vec3", "rgb_light_shadow", "!m" },
52         { FRAGMENT, "rgb_light_shadow", "vec3", "rgb_light*l_shadow", "s" },
53         { FRAGMENT, "rgb_light_shadow", "vec3", "rgb_light", "!s" },
54         { FRAGMENT, "rgb_light", "vec3", "vec3(l_diffuse)", "!m!p" },
55         { FRAGMENT, "rgb_light", "vec3", "vec3(l_diffuse+l_specular)", "!mp" },
56         { FRAGMENT, "rgb_light", "vec3", "l_diffuse*gl_FrontLightProduct[0].diffuse.rgb", "m!p" },
57         { FRAGMENT, "rgb_light", "vec3", "l_diffuse*gl_FrontLightProduct[0].diffuse.rgb+l_specular*gl_FrontLightProduct[0].specular.rgb", "mp" },
58         { FRAGMENT, "l_shadow", "float", "mix(1.0, shadow_sample, shadow_darkness)", 0 },
59         { FRAGMENT, "shadow_sample", "float", "shadow2D(shadow, shd_vertex).r", 0 },
60         { FRAGMENT, "l_diffuse", "float", "max(dot(n_zzz_normal, n_zzz_light_dir), 0.0)", 0 },
61         { FRAGMENT, "l_specular", "float", "pow(max(dot(n_zzz_half_vec, n_zzz_normal), 0.0), gl_FrontMaterial.shininess)", 0 },
62         { FRAGMENT, "n_zzz_half_vec", "vec3", "normalize(zzz_light_dir-zzz_incident_dir)", 0 },
63         { FRAGMENT, "n_zzz_light_dir", "vec3", "normalize(zzz_light_dir)", 0 },
64         { FRAGMENT, "n_tbn_normal", "vec3", "texture2D(normalmap, texture_coord).xyz*2.0-1.0", "n" },
65         { FRAGMENT, "n_eye_normal", "vec3", "normalize(eye_normal)", "!n" },
66         { FRAGMENT, "tex_sample", "vec4", "texture2D(texture, texture_coord)", 0 },
67
68         { VERTEX, "gl_Position", 0, "gl_ProjectionMatrix*eye_vertex", 0 },
69         { VERTEX, "shd_vertex", "vec3", "(eye_vertex*eye_shd_rmatrix).xyz", 0 },
70         { VERTEX, "eye_shd_rmatrix", "mat4", "mat4(gl_EyePlaneS[shadow_unit], gl_EyePlaneT[shadow_unit], gl_EyePlaneR[shadow_unit], vec4(0.0, 0.0, 0.0, 1.0))", 0 },
71         { VERTEX, "tbn_light_dir", "vec3", "eye_light_dir*eye_tbn_matrix", 0 },
72         { VERTEX, "eye_light_dir", "vec3", "normalize(gl_LightSource[0].position.xyz-eye_vertex.xyz*gl_LightSource[0].position.w)", 0 },
73         { VERTEX, "tbn_incident_dir", "vec3", "eye_incident_dir*eye_tbn_matrix", 0 },
74         { VERTEX, "eye_incident_dir", "vec3", "normalize(eye_vertex.xyz)", 0 },
75         { VERTEX, "eye_tbn_matrix", "mat3", "mat3(eye_tangent, eye_binormal, eye_normal)", 0 },
76         { VERTEX, "eye_vertex", "vec4", "gl_ModelViewMatrix*gl_Vertex", "!r" },
77         { VERTEX, "eye_vertex", "vec4", "transform_vertex(gl_Vertex)", "r" },
78         { VERTEX, "eye_normal", "vec3", "gl_NormalMatrix*gl_Normal", "!r" },
79         { VERTEX, "eye_normal", "vec3", "transform_normal(gl_Normal)", "r" },
80         { VERTEX, "eye_tangent", "vec3", "gl_NormalMatrix*tangent", "!r" },
81         { VERTEX, "eye_tangent", "vec3", "transform_normal(tangent)", "r" },
82         { VERTEX, "eye_binormal", "vec3", "gl_NormalMatrix*binormal", "!r" },
83         { VERTEX, "eye_binormal", "vec3", "transform_normal(binormal)", "r" },
84         { VERTEX, "color", "vec4", "gl_Color", 0 },
85         { VERTEX, "texture_coord", "vec2", "gl_MultiTexCoord0.xy", 0 },
86
87         { ATTRIBUTE, "tangent", "vec3", 0, 0 },
88         { ATTRIBUTE, "binormal", "vec3", 0, 0 },
89
90         { UNIFORM, "shadow_unit", "int", 0, 0 },
91         { UNIFORM, "texture", "sampler2D", 0, 0 },
92         { UNIFORM, "shadow", "sampler2DShadow", 0, 0 },
93         { UNIFORM, "shadow_darkness", "float", 0, 0 },
94         { UNIFORM, "normalmap", "sampler2D", 0, 0 },
95
96         // Terminator entry
97         { NO_SCOPE,  0, 0, 0, 0 }
98 };
99
100 ProgramBuilder::ProgramBuilder(const StandardFeatures &f):
101         features(f),
102         feature_flags(features.create_flags()),
103         optimize(true)
104 { }
105
106 void ProgramBuilder::set_optimize(bool o)
107 {
108         optimize = o;
109 }
110
111 Program *ProgramBuilder::create_program() const
112 {
113         Program *prog = new Program;
114         add_shaders(*prog);
115         return prog;
116 }
117
118 void ProgramBuilder::add_shaders(Program &prog) const
119 {
120         list<ShaderVariable> variables;
121         list<ShaderVariable *> resolved_vars;
122
123         variables.push_front(ShaderVariable("gl_Position"));
124         variables.push_front(ShaderVariable("gl_FragColor"));
125
126         for(const StandardVariable *i=standard_variables; i->name; ++i)
127         {
128                 // Skip over anything that isn't used with the supplied flags
129                 if(i->flags && !evaluate_flags(i->flags))
130                         continue;
131
132                 // See if this variable can satisfy any unresolved variables
133                 ShaderVariable *last_resolved = 0;
134                 for(list<ShaderVariable>::iterator j=variables.begin(); j!=variables.end(); ++j)
135                 {
136                         if(j->variable)
137                                 continue;
138
139                         if(!name_match(i->name, j->resolved_name.c_str()))
140                                 continue;
141
142                         if(last_resolved)
143                         {
144                                 /* We've already resolved a non-fuzzy variable in this iteration.
145                                 If there are multiple variables that can be resolved, they refer
146                                 to the same variable. */
147                                 j->resolve(*last_resolved);
148                                 continue;
149                         }
150
151                         j->resolve(*i);
152                         resolved_vars.push_front(&*j);
153                         if(!j->fuzzy_space)
154                                 last_resolved = &*j;
155
156                         if(!i->expression)
157                                 continue;
158
159                         vector<string> identifiers = extract_identifiers(i->expression);
160                         for(vector<string>::const_iterator k=identifiers.begin(); k!=identifiers.end(); ++k)
161                         {
162                                 // Use an existing variable if possible, but only if it's not fuzzy
163                                 ShaderVariable *var = 0;
164                                 for(list<ShaderVariable>::iterator l=variables.begin(); (!var && l!=variables.end()); ++l)
165                                         if(!l->fuzzy_space && l->resolved_name==*k)
166                                                 var = &*l;
167
168                                 if(!var)
169                                 {
170                                         variables.push_back(ShaderVariable(*k));
171                                         var = &variables.back();
172                                 }
173                                 j->add_reference(*var);
174                         }
175                 }
176         }
177
178         if(optimize)
179         {
180                 for(list<ShaderVariable *>::const_iterator i=resolved_vars.begin(); i!=resolved_vars.end(); ++i)
181                         (*i)->check_inline();
182         }
183
184         prog.attach_shader_owned(new VertexShader(create_source(resolved_vars, VERTEX)));
185         prog.attach_shader_owned(new FragmentShader(create_source(resolved_vars, FRAGMENT)));
186
187         if(features.normalmap)
188         {
189                 prog.bind_attribute(get_component_type(TANGENT3), "tangent");
190                 prog.bind_attribute(get_component_type(BINORMAL3), "binormal");
191         }
192 }
193
194 string ProgramBuilder::create_source(const list<ShaderVariable *> &variables, VariableScope scope) const
195 {
196         string source;
197
198         for(list<ShaderVariable *>::const_iterator i=variables.begin(); i!=variables.end(); ++i)
199                 if((*i)->variable->scope==UNIFORM && (*i)->is_referenced_from(scope))
200                         source += format("uniform %s %s;\n", (*i)->variable->type, (*i)->resolved_name);
201
202         if(scope==VERTEX)
203         {
204                 for(list<ShaderVariable *>::const_iterator i=variables.begin(); i!=variables.end(); ++i)
205                         if((*i)->variable->scope==ATTRIBUTE)
206                                 source += format("attribute %s %s;\n", (*i)->variable->type, (*i)->resolved_name);
207         }
208
209         /* Any variables defined in vertex scope but referenced from fragment scope
210         should be exported as varyings over the interface. */
211         list<ShaderVariable *> varyings;
212         for(list<ShaderVariable *>::const_iterator i=variables.begin(); i!=variables.end(); ++i)
213                 if((*i)->variable->scope==VERTEX && (*i)->is_referenced_from(FRAGMENT))
214                 {
215                         varyings.push_back(*i);
216                         source += format("varying %s v_%s;\n", (*i)->variable->type, (*i)->resolved_name);
217                 }
218
219         if(scope==VERTEX && features.transform)
220         {
221                 // Add the prototypes here, until I come up with something better
222                 source += "vec4 transform_vertex(vec4);\n";
223                 source += "vec3 transform_normal(vec3);\n";
224         }
225
226         source += "void main()\n{\n";
227
228         for(list<ShaderVariable *>::const_iterator i=variables.begin(); i!=variables.end(); ++i)
229                 if((*i)->variable->scope==scope && !(*i)->inlined)
230                 {
231                         source += '\t';
232                         if((*i)->variable->type)
233                         {
234                                 source += (*i)->variable->type;
235                                 source += ' ';
236                         }
237                         source += format("%s = %s;\n", (*i)->resolved_name, (*i)->get_expression());
238                 }
239
240         if(scope==VERTEX)
241         {
242                 for(list<ShaderVariable *>::const_iterator i=varyings.begin(); i!=varyings.end(); ++i)
243                 {
244                         if((*i)->inlined)
245                                 source += format("\tv_%s = %s;\n", (*i)->resolved_name, (*i)->get_expression());
246                         else
247                                 source += format("\tv_%s = %s;\n", (*i)->resolved_name, (*i)->resolved_name);
248                 }
249         }
250
251         source += '}';
252
253         return source;
254 }
255
256 bool ProgramBuilder::evaluate_flags(const char *flags) const
257 {
258         if(!flags)
259                 return true;
260
261         bool cond = true;
262         char oper = '&';
263         for(const char *i=flags; *i; ++i)
264         {
265                 if(*i>='a' && *i<='z')
266                 {
267                         bool found = (feature_flags.find(*i)!=string::npos);
268                         if(oper=='|')
269                                 cond = (cond || found);
270                         else if(oper=='!')
271                                 cond = (cond && !found);
272                         else if(oper=='&')
273                                 cond = (cond && found);
274                         oper = '&';
275                 }
276                 else
277                         oper = *i;
278         }
279
280         return cond;
281 }
282
283 ProgramBuilder::MatchLevel ProgramBuilder::name_match(const char *n1, const char *n2, const char **space)
284 {
285         int i = 0;
286         int zzz = -1;
287         int zside = 0;
288         while(*n1 && *n2)
289         {
290                 if(*n1==*n2 || *n1=='z' || *n2=='z')
291                 {
292                         if(*n1!=*n2)
293                         {
294                                 int side = (*n1=='z' ? 1 : 2);
295                                 if(zzz<0)
296                                 {
297                                         zzz = i;
298                                         zside = side;
299                                         if(space)
300                                         {
301                                                 if(*n1=='z')
302                                                         *space = n2;
303                                                 else
304                                                         *space = n1;
305                                         }
306                                 }
307                                 else if(i>=zzz+3 || side!=zside)
308                                         return NO_MATCH;
309                         }
310                 }
311                 else
312                         return NO_MATCH;
313                 ++n1;
314                 ++n2;
315                 ++i;
316         }
317         return (!*n1 && !*n2) ? zzz>=0 ? FUZZY : EXACT : NO_MATCH;
318 }
319
320 bool ProgramBuilder::parse_identifier(const char *ptr, unsigned &start, unsigned &length)
321 {
322         bool found = false;
323         bool member = false;
324         for(const char *i=ptr;; ++i)
325         {
326                 if(!found)
327                 {
328                         if(!*i)
329                                 return false;
330                         if(isalpha(*i) || *i=='_')
331                         {
332                                 if(!member)
333                                 {
334                                         start = i-ptr;
335                                         found = true;
336                                 }
337                         }
338                         else if(*i=='.')
339                                 member = true;
340                         else
341                                 member = false;
342                 }
343                 else
344                 {
345                         if(!isalnum(*i) && *i!='_')
346                         {
347                                 length = i-(ptr+start);
348                                 return true;
349                         }
350                 }
351         }
352 }
353
354 vector<string> ProgramBuilder::extract_identifiers(const char *expression)
355 {
356         vector<string> result;
357         const char *ptr = expression;
358         unsigned start, length;
359         while(parse_identifier(ptr, start, length))
360         {
361                 result.push_back(string(ptr+start, length));
362                 ptr += start+length;
363         }
364         return result;
365 }
366
367 string ProgramBuilder::replace_identifiers(const char *expression, const map<string, string> &replace_map)
368 {
369         string result;
370         const char *ptr = expression;
371         unsigned start, length;
372         while(parse_identifier(ptr, start, length))
373         {
374                 result.append(ptr, start);
375                 string identifier(ptr+start, length);
376                 map<string, string>::const_iterator i = replace_map.find(identifier);
377                 if(i!=replace_map.end())
378                         result += i->second;
379                 else
380                         result += identifier;
381                 ptr += start+length;
382         }
383         result += ptr;
384         return result;
385 }
386
387
388 ProgramBuilder::StandardFeatures::StandardFeatures():
389         texture(false),
390         material(false),
391         lighting(false),
392         specular(false),
393         normalmap(false),
394         shadow(false),
395         reflection(false),
396         transform(false)
397 { }
398
399 string ProgramBuilder::StandardFeatures::create_flags() const
400 {
401         string flags;
402         if(texture)
403                 flags += 't';
404         if(material)
405                 flags += 'm';
406         if(lighting)
407         {
408                 flags += 'l';
409                 if(specular)
410                         flags += 'p';
411                 if(normalmap)
412                         flags += 'n';
413         }
414         if(shadow)
415                 flags += 's';
416         if(reflection)
417                 flags += 'e';
418         if(transform)
419                 flags += 'r';
420
421         return flags;
422 }
423
424
425 ProgramBuilder::ShaderVariable::ShaderVariable(const std::string &n):
426         name(n),
427         variable(0),
428         resolved_name(n),
429         fuzzy_space(name.find("zzz")!=string::npos),
430         inlined(false),
431         inline_parens(false)
432 { }
433
434 void ProgramBuilder::ShaderVariable::resolve(const StandardVariable &var)
435 {
436         variable = &var;
437         const char *space = 0;
438         if(name_match(var.name, resolved_name.c_str(), &space)==FUZZY)
439                 resolve_space(string(space, 3));
440 }
441
442 void ProgramBuilder::ShaderVariable::resolve(ShaderVariable &var)
443 {
444         for(list<ShaderVariable *>::iterator i=referenced_by.begin(); i!=referenced_by.end(); ++i)
445                 (*i)->update_reference(*this, var);
446         var.referenced_by.insert(var.referenced_by.end(), referenced_by.begin(), referenced_by.end());
447 }
448
449 void ProgramBuilder::ShaderVariable::resolve_space(const string &space)
450 {
451         if(fuzzy_space)
452         {
453                 resolved_space = space;
454
455                 string::size_type zzz = resolved_name.find("zzz");
456                 resolved_name.replace(zzz, 3, resolved_space);
457                 fuzzy_space = false;
458
459                 // Resolving the space could have affected other variables that use this one
460                 for(list<ShaderVariable *>::iterator i=referenced_by.begin(); i!=referenced_by.end(); ++i)
461                         (*i)->resolve_space(space);
462         }
463
464         for(list<ShaderVariable *>::iterator i=referenced_vars.begin(); i!=referenced_vars.end(); ++i)
465                 if((*i)->fuzzy_space)
466                         (*i)->resolve_space(space);
467 }
468
469 void ProgramBuilder::ShaderVariable::add_reference(ShaderVariable &var)
470 {
471         referenced_vars.push_back(&var);
472         var.referenced_by.push_back(this);
473         if(var.fuzzy_space && !resolved_space.empty())
474                 var.resolve_space(resolved_space);
475 }
476
477 void ProgramBuilder::ShaderVariable::update_reference(ShaderVariable &from, ShaderVariable &to)
478 {
479         replace(referenced_vars.begin(), referenced_vars.end(), &from, &to);
480         replace(referenced_by.begin(), referenced_by.end(), &from, &to);
481         if(from.fuzzy_space && !to.fuzzy_space && !to.resolved_space.empty())
482                 resolve_space(to.resolved_space);
483 }
484
485 void ProgramBuilder::ShaderVariable::check_inline()
486 {
487         if(variable->expression)
488         {
489                 unsigned total_refs = referenced_by.size();
490                 unsigned in_scope_refs = 0;
491                 for(list<ShaderVariable *>::const_iterator i=referenced_by.begin(); i!=referenced_by.end(); ++i)
492                         if((*i)->variable->scope==variable->scope)
493                                 ++in_scope_refs;
494                 if(total_refs==1 || (total_refs>0 && in_scope_refs==0))
495                 {
496                         inlined = true;
497                         unsigned level = 0;
498                         for(const char *c=variable->expression; (!inline_parens && *c); ++c)
499                         {
500                                 if(*c=='(')
501                                         ++level;
502                                 else if(*c==')')
503                                         --level;
504                                 else if(level==0 && !isalnum(*c) && *c!='_' && *c!='.')
505                                         inline_parens = true;
506                         }
507                 }
508         }
509 }
510
511 bool ProgramBuilder::ShaderVariable::is_referenced_from(VariableScope scope) const
512 {
513         for(list<ShaderVariable *>::const_iterator i=referenced_by.begin(); i!=referenced_by.end(); ++i)
514                 if((*i)->variable->scope==scope)
515                         return true;
516         return false;
517 }
518
519 string ProgramBuilder::ShaderVariable::get_expression() const
520 {
521         map<string, string> replace_map;
522         for(list<ShaderVariable *>::const_iterator i=referenced_vars.begin(); i!=referenced_vars.end(); ++i)
523                 if((*i)->variable)
524                 {
525                         string replacement = (*i)->resolved_name;
526                         if(variable->scope==FRAGMENT && (*i)->variable->scope==VERTEX)
527                                 replacement = "v_"+replacement;
528                         else if((*i)->inlined)
529                         {
530                                 replacement = (*i)->get_expression();
531                                 if((*i)->inline_parens)
532                                         replacement = "("+replacement+")";
533                         }
534                         if(replacement!=(*i)->name)
535                                 replace_map[(*i)->name] = replacement;
536                 }
537
538         if(replace_map.empty())
539                 return variable->expression;
540         else
541                 return replace_identifiers(variable->expression, replace_map);
542 }
543
544
545 ProgramBuilder::StandardFeatures::Loader::Loader(StandardFeatures &f):
546         DataFile::ObjectLoader<StandardFeatures>(f)
547 {
548         add("lighting",  &StandardFeatures::lighting);
549         add("material",  &StandardFeatures::material);
550         add("normalmap", &StandardFeatures::normalmap);
551         add("reflection", &StandardFeatures::reflection);
552         add("shadow",    &StandardFeatures::shadow);
553         add("specular",  &StandardFeatures::specular);
554         add("texture",   &StandardFeatures::texture);
555         add("transform", &StandardFeatures::transform);
556 }
557
558 } // namespace GL
559 } // namespace Msp