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