/* The array are stored in reverse order, so that variables always come after
anything that might need them. */
-const ProgramBuilder::StandardVariable ProgramBuilder::standard_variables[] =
+const ProgramBuilder::VariableDefinition ProgramBuilder::standard_variables[] =
{
{ FRAGMENT, "gl_FragColor", 0, "frag_color", "g" },
{ FRAGMENT, "frag_color", 0, "color_base", "!t" },
{ FRAGMENT, "n_zzz_light_dir", "vec3", "normalize(zzz_light_dir)", 0 },
{ FRAGMENT, "n_tbn_normal", "vec3", "normal_sample*2.0-1.0", "n" },
{ FRAGMENT, "n_eye_normal", "vec3", "normalize(eye_normal)", "!n" },
- { FRAGMENT, "normal_sample", "vec3", "texture2D(normalmap, texture_coord).xyz", "!c" },
- { FRAGMENT, "normal_sample", "vec3", "sample_normalmap(texture_coord)", "c" },
- { FRAGMENT, "tex_sample", "vec4", "texture2D(texture, texture_coord)", "!c" },
- { FRAGMENT, "tex_sample", "vec4", "sample_texture(texture_coord)", "c" },
+ { FRAGMENT, "normal_sample", "vec3", "texture2D(normalmap, texture_coord).xyz", 0 },
+ { FRAGMENT, "tex_sample", "vec4", "texture2D(texture, texture_coord)", 0 },
{ VERTEX, "gl_Position", 0, "projection_matrix*eye_vertex", 0 },
{ VERTEX, "shd_vertex", "vec3", "vec3(dot(eye_vertex, gl_EyePlaneS[shadow_unit]), dot(eye_vertex, gl_EyePlaneT[shadow_unit]), dot(eye_vertex, gl_EyePlaneR[shadow_unit]))", "g" },
{ 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", "eye_obj_matrix*vertex", "!r" },
- { VERTEX, "eye_vertex", "vec4", "transform_vertex(vertex)", "r" },
- { VERTEX, "eye_normal", "vec3", "eye_obj_normal_matrix*normal", "!r" },
- { VERTEX, "eye_normal", "vec3", "transform_normal(normal)", "r" },
- { VERTEX, "eye_tangent", "vec3", "eye_obj_normal_matrix*tangent", "!r" },
- { VERTEX, "eye_tangent", "vec3", "transform_normal(tangent)", "r" },
- { VERTEX, "eye_binormal", "vec3", "eye_obj_normal_matrix*binormal", "!r" },
- { VERTEX, "eye_binormal", "vec3", "transform_normal(binormal)", "r" },
+ { VERTEX, "eye_vertex", "vec4", "eye_obj_matrix*vertex", 0 },
+ { VERTEX, "eye_normal", "vec3", "eye_obj_normal_matrix*normal", 0 },
+ { VERTEX, "eye_tangent", "vec3", "eye_obj_normal_matrix*tangent", 0 },
+ { VERTEX, "eye_binormal", "vec3", "eye_obj_normal_matrix*binormal", 0 },
{ VERTEX, "texture_coord", "vec2", "texcoord.xy", 0 },
{ ATTRIBUTE, "vertex", "vec4", "gl_Vertex", 0 },
features(f),
feature_flags(features.create_flags()),
optimize(true)
-{ }
+{
+ if(!features.custom.empty())
+ {
+ const char *whitespace = " \t\n";
+ string::size_type start = 0;
+ while(1)
+ {
+ start = features.custom.find_first_not_of(whitespace, start);
+ if(start==string::npos)
+ break;
+
+ string::size_type semicolon = features.custom.find(';', start);
+ if(semicolon==start)
+ {
+ ++start;
+ continue;
+ }
+ else if(semicolon==string::npos)
+ throw invalid_variable_definition(features.custom.substr(start));
+
+ string::size_type equals = features.custom.find('=', start);
+ if(equals>semicolon)
+ equals = string::npos;
+
+ VariableDefinition var;
+ string::size_type decl_end = min(equals, semicolon);
+ for(unsigned i=0;; ++i)
+ {
+ string::size_type word_end = features.custom.find_first_of(whitespace, start);
+ word_end = min(word_end, decl_end);
+ features.custom[word_end] = 0;
+
+ const char *word = &features.custom[start];
+ if(i==0)
+ {
+ if(!strcmp(word, "uniform"))
+ var.scope = UNIFORM;
+ else if(!strcmp(word, "attribute"))
+ var.scope = ATTRIBUTE;
+ else if(!strcmp(word, "vertex"))
+ var.scope = VERTEX;
+ else if(!strcmp(word, "fragment"))
+ var.scope = FRAGMENT;
+ else
+ throw invalid_variable_definition(word);
+ }
+ else if(i==1)
+ var.type = word;
+ else if(i==2)
+ var.name = word;
+
+ start = features.custom.find_first_not_of(whitespace, word_end+1);
+ if(start>=decl_end)
+ break;
+ }
+
+ if(equals!=string::npos)
+ {
+ start = features.custom.find_first_not_of(whitespace, equals+1);
+ if(start>=semicolon)
+ throw invalid_variable_definition("no expression");
+ features.custom[semicolon] = 0;
+ var.expression = &features.custom[start];
+ }
+ else
+ var.expression = 0;
+
+ var.flags = 0;
+ for(const VariableDefinition *j=standard_variables; j->name; ++j)
+ if(!strcmp(var.name, j->name))
+ var.flags = "o";
+
+ custom_variables.push_front(var);
+
+ start = semicolon+1;
+ }
+ }
+}
void ProgramBuilder::set_optimize(bool o)
{
variables.push_front(ShaderVariable("gl_Position"));
variables.push_front(ShaderVariable(features.legacy ? "gl_FragColor" : "frag_color"));
- for(const StandardVariable *i=standard_variables; i->name; ++i)
+ list<VariableDefinition>::const_iterator next_custom = custom_variables.begin();
+ for(const VariableDefinition *i=standard_variables; i->name; )
{
- // Skip over anything that isn't used with the supplied flags
- if(i->flags && !evaluate_flags(i->flags))
- continue;
+ const VariableDefinition *def = 0;
+ if(next_custom!=custom_variables.end() && (!strcmp(next_custom->name, i->name) || !next_custom->flags))
+ {
+ def = &*next_custom;
+ ++next_custom;
+ }
+ else
+ {
+ def = i;
+ ++i;
+
+ // Skip over anything that isn't used with the supplied flags
+ if(def->flags && !evaluate_flags(def->flags))
+ continue;
+ }
// See if this variable can satisfy any unresolved variables
ShaderVariable *last_resolved = 0;
if(j->variable)
continue;
- if(!name_match(i->name, j->resolved_name.c_str()))
+ if(!name_match(def->name, j->resolved_name.c_str()))
continue;
if(last_resolved)
continue;
}
- j->resolve(*i);
+ j->resolve(*def);
resolved_vars.push_front(&*j);
if(!j->fuzzy_space)
last_resolved = &*j;
- if(!i->expression)
+ if(!def->expression)
continue;
- vector<string> identifiers = extract_identifiers(i->expression);
+ vector<string> identifiers = extract_identifiers(def->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
if(scope==FRAGMENT && !features.legacy)
source += "out vec4 frag_color;\n";
- 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";
- }
- else if(scope==FRAGMENT && features.colorify)
- {
- if(features.texture)
- source += "vec4 sample_texture(vec2);\n";
- if(features.normalmap)
- source += "vec3 sample_normalmap(vec2);\n";
- }
-
source += "void main()\n{\n";
for(list<ShaderVariable *>::const_iterator i=variables.begin(); i!=variables.end(); ++i)
normalmap(false),
shadow(false),
reflection(false),
- transform(false),
- colorify(false),
legacy(!(get_glsl_version()>=Version(1, 30)))
{ }
flags += 's';
if(reflection)
flags += 'e';
- if(transform)
- flags += 'r';
- if(colorify)
- flags += 'c';
if(legacy)
flags += 'g';
inline_parens(false)
{ }
-void ProgramBuilder::ShaderVariable::resolve(const StandardVariable &var)
+void ProgramBuilder::ShaderVariable::resolve(const VariableDefinition &var)
{
variable = &var;
const char *space = 0;
ProgramBuilder::StandardFeatures::Loader::Loader(StandardFeatures &f):
DataFile::ObjectLoader<StandardFeatures>(f)
{
- add("colorify", &StandardFeatures::colorify);
+ add("custom", &StandardFeatures::custom);
add("lighting", &StandardFeatures::lighting);
add("material", &StandardFeatures::material);
add("normalmap", &StandardFeatures::normalmap);
add("shadow", &StandardFeatures::shadow);
add("specular", &StandardFeatures::specular);
add("texture", &StandardFeatures::texture);
- add("transform", &StandardFeatures::transform);
}
} // namespace GL