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