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