+#include <algorithm>
+#include <msp/strings/format.h>
+#include "program.h"
+#include "programbuilder.h"
+#include "shader.h"
+
+using namespace std;
+
+namespace Msp {
+namespace GL {
+
+/*
+Naming conventions:
+ n_* Normalized vector
+ l_* Lighting component
+
+ obj_* Object space
+ eye_* Eye space
+ tbn_* Tangent-Binormal-Normal space
+ shd_* Shadow space
+ *_dir Direction vector
+
+ zzz_* Wildcard space, resolved by the builder
+ All wildcard spaces within an expression must match
+
+ xxx_yyy_* Matrix that transforms between yyy to xxx
+ The vector is on the side of its designated space, result will be
+ in the other space
+ *_matrix A matrix (duh)
+ *_rmatrix A mat4 that works with a row vector
+
+ rgb_* Color with rgb components only
+ color_* Color with rgba components
+*/
+
+/* The array are stored in reverse order, so that variables always come after
+anything that might need them. */
+const ProgramBuilder::StandardVariable ProgramBuilder::standard_variables[] =
+{
+ { FRAGMENT, "gl_FragColor", 0, "color_base", "!t" },
+ { FRAGMENT, "gl_FragColor", 0, "tex_sample*color_base", "t" },
+ { FRAGMENT, "color_base", "vec4", "color_unlit", "!l!s" },
+ { FRAGMENT, "color_base", "vec4", "color_unlit*vec4(vec3(l_shadow), 1.0)", "!ls" },
+ { FRAGMENT, "color_base", "vec4", "vec4(rgb_light_full, 1.0)", "l!m" },
+ { FRAGMENT, "color_base", "vec4", "vec4(rgb_light_full, gl_FrontMaterial.diffuse.a)", "lm" },
+ { FRAGMENT, "color_unlit", "vec4", "vec4(1.0)", "!m" },
+ { FRAGMENT, "color_unlit", "vec4", "color", "m" },
+ { FRAGMENT, "rgb_light_full", "vec3", "rgb_light_shadow+gl_FrontLightModelProduct.sceneColor.rgb", "m" },
+ { FRAGMENT, "rgb_light_full", "vec3", "rgb_light_shadow", "!m" },
+ { FRAGMENT, "rgb_light_shadow", "vec3", "(rgb_light)*l_shadow", "s" },
+ { FRAGMENT, "rgb_light_shadow", "vec3", "rgb_light", "!s" },
+ { FRAGMENT, "rgb_light", "vec3", "vec3(l_diffuse)", "!m!p" },
+ { FRAGMENT, "rgb_light", "vec3", "vec3(l_diffuse+l_specular)", "!mp" },
+ { FRAGMENT, "rgb_light", "vec3", "l_diffuse*gl_FrontLightProduct[0].diffuse.rgb", "m!p" },
+ { FRAGMENT, "rgb_light", "vec3", "l_diffuse*gl_FrontLightProduct[0].diffuse.rgb+l_specular*gl_FrontLightProduct[0].specular.rgb", "mp" },
+ { FRAGMENT, "l_shadow", "float", "shadow2D(shadow, shd_vertex).r", 0 },
+ { FRAGMENT, "l_diffuse", "float", "max(dot(n_zzz_normal, n_zzz_light_dir), 0.0)", 0 },
+ { FRAGMENT, "l_specular", "float", "pow(max(dot(n_zzz_half_vec, n_zzz_normal), 0.0), gl_FrontMaterial.shininess)", 0 },
+ { FRAGMENT, "n_zzz_half_vec", "vec3", "normalize(zzz_light_dir-zzz_incident_dir)", 0 },
+ { FRAGMENT, "n_zzz_light_dir", "vec3", "normalize(zzz_light_dir)", 0 },
+ { FRAGMENT, "n_tbn_normal", "vec3", "texture2D(normalmap, texture_coord).xyz*2.0-1.0", "n" },
+ { FRAGMENT, "n_eye_normal", "vec3", "normalize(eye_normal)", "!n" },
+ { FRAGMENT, "tex_sample", "vec4", "texture2D(texture, texture_coord)", 0 },
+
+ { VERTEX, "gl_Position", 0, "gl_ProjectionMatrix*eye_vertex", 0 },
+ { VERTEX, "shd_vertex", "vec3", "eye_vertex*eye_shd_rmatrix", 0 },
+ { 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 },
+ { VERTEX, "tbn_light_dir", "vec3", "eye_light_dir*eye_tbn_matrix", 0 },
+ { VERTEX, "eye_light_dir", "vec3", "normalize(gl_LightSource[0].position.xyz-eye_vertex.xyz*gl_LightSource[0].position.w)", 0 },
+ { VERTEX, "tbn_incident_dir", "vec3", "eye_incident_dir*eye_tbn_matrix", 0 },
+ { VERTEX, "eye_incident_dir", "vec3", "normalize(eye_vertex.xyz)", 0 },
+ { VERTEX, "eye_tbn_matrix", "mat3", "mat3(eye_tangent, eye_binormal, eye_normal)", 0 },
+ { VERTEX, "eye_vertex", "vec4", "gl_ModelViewMatrix*gl_Vertex", "!r" },
+ { VERTEX, "eye_vertex", "vec4", "transform_vertex(gl_Vertex)", "r" },
+ { VERTEX, "eye_normal", "vec3", "gl_NormalMatrix*gl_Normal", "!r" },
+ { VERTEX, "eye_normal", "vec3", "transform_normal(gl_Normal)", "r" },
+ { VERTEX, "eye_tangent", "vec3", "gl_NormalMatrix*tangent", "!r" },
+ { VERTEX, "eye_tangent", "vec3", "transform_normal(tangent)", "r" },
+ { VERTEX, "eye_binormal", "vec3", "gl_NormalMatrix*binormal", "!r" },
+ { VERTEX, "eye_binormal", "vec3", "transform_normal(binormal)", "r" },
+ { VERTEX, "color", "vec4", "gl_Color", 0 },
+ { VERTEX, "texture_coord", "vec2", "gl_MultiTexCoord0", 0 },
+
+ { ATTRIBUTE, "tangent", "vec3", 0, 0 },
+ { ATTRIBUTE, "binormal", "vec3", 0, 0 },
+
+ { UNIFORM, "shadow_unit", "int", 0, 0 },
+ { UNIFORM, "texture", "sampler2D", 0, 0 },
+ { UNIFORM, "shadow", "sampler2DShadow", 0, 0 },
+ { UNIFORM, "normalmap", "sampler2D", 0, 0 },
+
+ // Terminator entry
+ { NO_SCOPE, 0, 0, 0, 0 }
+};
+
+ProgramBuilder::ProgramBuilder(const StandardFeatures &f):
+ features(f),
+ feature_flags(features.create_flags())
+{ }
+
+Program *ProgramBuilder::create_program() const
+{
+ Program *prog = new Program;
+ add_shaders(*prog);
+ return prog;
+}
+
+void ProgramBuilder::add_shaders(Program &prog) const
+{
+ list<ShaderVariable> variables;
+ list<ShaderVariable *> resolved_vars;
+
+ variables.push_front(ShaderVariable("gl_Position"));
+ variables.push_front(ShaderVariable("gl_FragColor"));
+
+ for(const StandardVariable *i=standard_variables; i->name; ++i)
+ {
+ // Skip over anything that isn't used with the supplied flags
+ if(i->flags && !evaluate_flags(i->flags))
+ continue;
+
+ // See if this variable can satisfy any unresolved variables
+ ShaderVariable *last_resolved = 0;
+ for(list<ShaderVariable>::iterator j=variables.begin(); j!=variables.end(); ++j)
+ {
+ if(j->variable)
+ continue;
+
+ if(!name_match(i->name, j->resolved_name.c_str()))
+ continue;
+
+ if(last_resolved)
+ {
+ /* We've already resolved a non-fuzzy variable in this iteration.
+ If there are multiple variables that can be resolved, they refer
+ to the same variable. */
+ j->resolve(*last_resolved);
+ continue;
+ }
+
+ j->resolve(*i);
+ resolved_vars.push_front(&*j);
+ if(!j->fuzzy_space)
+ last_resolved = &*j;
+
+ if(!i->expression)
+ continue;
+
+ vector<string> identifiers = extract_identifiers(i->expression);
+ for(vector<string>::const_iterator k=identifiers.begin(); k!=identifiers.end(); ++k)
+ {
+ // Use an existing variable if possible, but only if it's not fuzzy
+ ShaderVariable *var = 0;
+ for(list<ShaderVariable>::iterator l=variables.begin(); (!var && l!=variables.end()); ++l)
+ if(!l->fuzzy_space && l->resolved_name==*k)
+ var = &*l;
+
+ if(!var)
+ {
+ variables.push_back(ShaderVariable(*k));
+ var = &variables.back();
+ }
+ j->add_reference(*var);
+ }
+ }
+ }
+
+ prog.attach_shader_owned(new VertexShader(create_source(resolved_vars, VERTEX)));
+ prog.attach_shader_owned(new FragmentShader(create_source(resolved_vars, FRAGMENT)));
+}
+
+string ProgramBuilder::create_source(const list<ShaderVariable *> &variables, VariableScope scope) const
+{
+ string source;
+
+ for(list<ShaderVariable *>::const_iterator i=variables.begin(); i!=variables.end(); ++i)
+ if((*i)->variable->scope==UNIFORM && (*i)->is_referenced_from(scope))
+ source += format("uniform %s %s;\n", (*i)->variable->type, (*i)->resolved_name);
+
+ if(scope==VERTEX)
+ {
+ for(list<ShaderVariable *>::const_iterator i=variables.begin(); i!=variables.end(); ++i)
+ if((*i)->variable->scope==ATTRIBUTE)
+ source += format("attribute %s %s;\n", (*i)->variable->type, (*i)->resolved_name);
+ }
+
+ /* Any variables defined in vertex scope but referenced from fragment scope
+ should be exported as varyings over the interface. */
+ list<ShaderVariable *> varyings;
+ for(list<ShaderVariable *>::const_iterator i=variables.begin(); i!=variables.end(); ++i)
+ if((*i)->variable->scope==VERTEX && (*i)->is_referenced_from(FRAGMENT))
+ {
+ varyings.push_back(*i);
+ source += format("varying %s v_%s;\n", (*i)->variable->type, (*i)->resolved_name);
+ }
+
+ if(scope==VERTEX && features.transform)
+ {
+ // Add the prototypes here, until I come up with something better
+ source += "vec4 transform_vertex(vec4);\n";
+ source += "vec3 transform_normal(vec3);\n";
+ }
+
+ source += "void main()\n{\n";
+
+ for(list<ShaderVariable *>::const_iterator i=variables.begin(); i!=variables.end(); ++i)
+ if((*i)->variable->scope==scope)
+ {
+ source += '\t';
+ if((*i)->variable->type)
+ {
+ source += (*i)->variable->type;
+ source += ' ';
+ }
+ source += format("%s = %s;\n", (*i)->resolved_name, (*i)->get_expression());
+ }
+
+ if(scope==VERTEX)
+ {
+ for(list<ShaderVariable *>::const_iterator i=varyings.begin(); i!=varyings.end(); ++i)
+ source += format("\tv_%s = %s;\n", (*i)->resolved_name, (*i)->resolved_name);
+ }
+
+ source += '}';
+
+ return source;
+}
+
+bool ProgramBuilder::evaluate_flags(const char *flags) const
+{
+ if(!flags)
+ return true;
+
+ bool cond = true;
+ char oper = '&';
+ for(const char *i=flags; *i; ++i)
+ {
+ if(*i>='a' && *i<='z')
+ {
+ bool found = (feature_flags.find(*i)!=string::npos);
+ if(oper=='|')
+ cond = (cond || found);
+ else if(oper=='!')
+ cond = (cond && !found);
+ else if(oper=='&')
+ cond = (cond && found);
+ oper = '&';
+ }
+ else
+ oper = *i;
+ }
+
+ return cond;
+}
+
+ProgramBuilder::MatchLevel ProgramBuilder::name_match(const char *n1, const char *n2, const char **space)
+{
+ int i = 0;
+ int zzz = -1;
+ int zside = 0;
+ while(*n1 && *n2)
+ {
+ if(*n1==*n2 || *n1=='z' || *n2=='z')
+ {
+ if(*n1!=*n2)
+ {
+ int side = (*n1=='z' ? 1 : 2);
+ if(zzz<0)
+ {
+ zzz = i;
+ zside = side;
+ if(space)
+ {
+ if(*n1=='z')
+ *space = n2;
+ else
+ *space = n1;
+ }
+ }
+ else if(i>=zzz+3 || side!=zside)
+ return NO_MATCH;
+ }
+ }
+ else
+ return NO_MATCH;
+ ++n1;
+ ++n2;
+ ++i;
+ }
+ return (!*n1 && !*n2) ? zzz>=0 ? FUZZY : EXACT : NO_MATCH;
+}
+
+bool ProgramBuilder::parse_identifier(const char *ptr, unsigned &start, unsigned &length)
+{
+ bool found = false;
+ bool member = false;
+ for(const char *i=ptr;; ++i)
+ {
+ if(!found)
+ {
+ if(!*i)
+ return false;
+ if(isalpha(*i) || *i=='_')
+ {
+ if(!member)
+ {
+ start = i-ptr;
+ found = true;
+ }
+ }
+ else if(*i=='.')
+ member = true;
+ else
+ member = false;
+ }
+ else
+ {
+ if(!isalnum(*i) && *i!='_')
+ {
+ length = i-(ptr+start);
+ return true;
+ }
+ }
+ }
+}
+
+vector<string> ProgramBuilder::extract_identifiers(const char *expression)
+{
+ vector<string> result;
+ const char *ptr = expression;
+ unsigned start, length;
+ while(parse_identifier(ptr, start, length))
+ {
+ result.push_back(string(ptr+start, length));
+ ptr += start+length;
+ }
+ return result;
+}
+
+string ProgramBuilder::replace_identifiers(const char *expression, const map<string, string> &replace_map)
+{
+ string result;
+ const char *ptr = expression;
+ unsigned start, length;
+ while(parse_identifier(ptr, start, length))
+ {
+ result.append(ptr, start);
+ string identifier(ptr+start, length);
+ map<string, string>::const_iterator i = replace_map.find(identifier);
+ if(i!=replace_map.end())
+ result += i->second;
+ else
+ result += identifier;
+ ptr += start+length;
+ }
+ result += ptr;
+ return result;
+}
+
+
+ProgramBuilder::StandardFeatures::StandardFeatures():
+ texture(false),
+ material(false),
+ lighting(false),
+ specular(false),
+ normalmap(false),
+ shadow(false),
+ reflection(false),
+ transform(false)
+{ }
+
+string ProgramBuilder::StandardFeatures::create_flags() const
+{
+ string flags;
+ if(texture)
+ flags += 't';
+ if(material)
+ flags += 'm';
+ if(lighting)
+ {
+ flags += 'l';
+ if(specular)
+ flags += 'p';
+ if(normalmap)
+ flags += 'n';
+ }
+ if(shadow)
+ flags += 's';
+ if(reflection)
+ flags += 'e';
+ if(transform)
+ flags += 'r';
+
+ return flags;
+}
+
+
+ProgramBuilder::ShaderVariable::ShaderVariable(const std::string &n):
+ name(n),
+ variable(0),
+ resolved_name(n),
+ fuzzy_space(name.find("zzz")!=string::npos)
+{ }
+
+void ProgramBuilder::ShaderVariable::resolve(const StandardVariable &var)
+{
+ variable = &var;
+ const char *space = 0;
+ if(name_match(var.name, resolved_name.c_str(), &space)==FUZZY)
+ resolve_space(string(space, 3));
+}
+
+void ProgramBuilder::ShaderVariable::resolve(ShaderVariable &var)
+{
+ for(list<ShaderVariable *>::iterator i=referenced_by.begin(); i!=referenced_by.end(); ++i)
+ (*i)->update_reference(*this, var);
+}
+
+void ProgramBuilder::ShaderVariable::resolve_space(const string &space)
+{
+ if(fuzzy_space)
+ {
+ resolved_space = space;
+
+ string::size_type zzz = resolved_name.find("zzz");
+ resolved_name.replace(zzz, 3, resolved_space);
+ fuzzy_space = false;
+
+ // Resolving the space could have affected other variables that use this one
+ for(list<ShaderVariable *>::iterator i=referenced_by.begin(); i!=referenced_by.end(); ++i)
+ (*i)->resolve_space(space);
+ }
+
+ for(list<ShaderVariable *>::iterator i=referenced_vars.begin(); i!=referenced_vars.end(); ++i)
+ if((*i)->fuzzy_space)
+ (*i)->resolve_space(space);
+}
+
+void ProgramBuilder::ShaderVariable::add_reference(ShaderVariable &var)
+{
+ referenced_vars.push_back(&var);
+ var.referenced_by.push_back(this);
+ if(var.fuzzy_space && !resolved_space.empty())
+ var.resolve_space(resolved_space);
+}
+
+void ProgramBuilder::ShaderVariable::update_reference(ShaderVariable &from, ShaderVariable &to)
+{
+ replace(referenced_vars.begin(), referenced_vars.end(), &from, &to);
+ replace(referenced_by.begin(), referenced_by.end(), &from, &to);
+ if(from.fuzzy_space && !to.fuzzy_space && !to.resolved_space.empty())
+ resolve_space(to.resolved_space);
+}
+
+bool ProgramBuilder::ShaderVariable::is_referenced_from(VariableScope scope) const
+{
+ for(list<ShaderVariable *>::const_iterator i=referenced_by.begin(); i!=referenced_by.end(); ++i)
+ if((*i)->variable->scope==scope)
+ return true;
+ return false;
+}
+
+string ProgramBuilder::ShaderVariable::get_expression() const
+{
+ map<string, string> replace_map;
+ for(list<ShaderVariable *>::const_iterator i=referenced_vars.begin(); i!=referenced_vars.end(); ++i)
+ if((*i)->variable)
+ {
+ string var_name = (*i)->resolved_name;
+ if(variable->scope==FRAGMENT && (*i)->variable->scope==VERTEX)
+ var_name = "v_"+var_name;
+ if(var_name!=(*i)->name)
+ replace_map[(*i)->name] = var_name;
+ }
+
+ if(replace_map.empty())
+ return variable->expression;
+ else
+ return replace_identifiers(variable->expression, replace_map);
+}
+
+
+ProgramBuilder::StandardFeatures::Loader::Loader(StandardFeatures &f):
+ DataFile::ObjectLoader<StandardFeatures>(f)
+{
+ add("lighting", &StandardFeatures::lighting);
+ add("material", &StandardFeatures::material);
+ add("normalmap", &StandardFeatures::normalmap);
+ add("reflection", &StandardFeatures::reflection);
+ add("shadow", &StandardFeatures::shadow);
+ add("specular", &StandardFeatures::specular);
+ add("texture", &StandardFeatures::texture);
+ add("transform", &StandardFeatures::transform);
+}
+
+} // namespace GL
+} // namespace Msp