#include "program.h"
#include "programbuilder.h"
#include "shader.h"
+#include "vertexformat.h"
using namespace std;
eye_* Eye space
tbn_* Tangent-Binormal-Normal space
shd_* Shadow space
+ env_* Environment space
*_dir Direction vector
zzz_* Wildcard space, resolved by the builder
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, "gl_FragColor", 0, "tex_sample", "!l!s!mt" },
+ { FRAGMENT, "gl_FragColor", 0, "tex_sample*color_base", "l|s|mt" },
+ { FRAGMENT, "color_base", "vec4", "vec4(1.0)", "!l!s!m" },
+ { FRAGMENT, "color_base", "vec4", "color", "!l!sm" },
+ { FRAGMENT, "color_base", "vec4", "vec4(vec3(l_shadow), 1.0)", "!ls!m" },
+ { FRAGMENT, "color_base", "vec4", "color*vec4(vec3(l_shadow), 1.0)", "!lsm" },
+ { FRAGMENT, "color_base", "vec4", "vec4(rgb_light_env, 1.0)", "l!m" },
+ { FRAGMENT, "color_base", "vec4", "vec4(rgb_light_env, gl_FrontMaterial.diffuse.a)", "lm" },
+ { FRAGMENT, "rgb_light_env", "vec3", "rgb_light_full+reflect_sample.rgb*reflectivity", "e" },
+ { FRAGMENT, "rgb_light_env", "vec3", "rgb_light_full", "!e" },
{ 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*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, "reflect_sample", "vec4", "textureCube(environment, env_reflect_dir)", 0 },
+ { FRAGMENT, "env_reflect_dir", "vec3", "env_eye_matrix*eye_reflect_dir", 0 },
+ { FRAGMENT, "eye_reflect_dir", "vec3", "eye_tbn_matrix*tbn_reflect_dir", "n" },
+ { FRAGMENT, "zzz_reflect_dir", "vec3", "reflect(zzz_incident_dir, n_zzz_normal)", 0 },
{ FRAGMENT, "l_shadow", "float", "mix(1.0, shadow_sample, shadow_darkness)", 0 },
{ FRAGMENT, "shadow_sample", "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_tbn_normal", "vec3", "normal_sample*2.0-1.0", "n" },
{ FRAGMENT, "n_eye_normal", "vec3", "normalize(eye_normal)", "!n" },
- { FRAGMENT, "tex_sample", "vec4", "texture2D(texture, texture_coord)", 0 },
+ { 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" },
{ VERTEX, "gl_Position", 0, "gl_ProjectionMatrix*eye_vertex", 0 },
- { VERTEX, "shd_vertex", "vec3", "eye_vertex*eye_shd_rmatrix", 0 },
+ { VERTEX, "shd_vertex", "vec3", "(eye_vertex*eye_shd_rmatrix).xyz", 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, "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 },
+ { VERTEX, "texture_coord", "vec2", "gl_MultiTexCoord0.xy", 0 },
{ ATTRIBUTE, "tangent", "vec3", 0, 0 },
{ ATTRIBUTE, "binormal", "vec3", 0, 0 },
{ UNIFORM, "shadow", "sampler2DShadow", 0, 0 },
{ UNIFORM, "shadow_darkness", "float", 0, 0 },
{ UNIFORM, "normalmap", "sampler2D", 0, 0 },
+ { UNIFORM, "environment", "samplerCube", 0, 0 },
+ { UNIFORM, "env_eye_matrix", "mat3", 0, 0 },
+ { UNIFORM, "reflectivity", "float", 0, 0 },
// Terminator entry
{ NO_SCOPE, 0, 0, 0, 0 }
ProgramBuilder::ProgramBuilder(const StandardFeatures &f):
features(f),
- feature_flags(features.create_flags())
+ feature_flags(features.create_flags()),
+ optimize(true)
{ }
+void ProgramBuilder::set_optimize(bool o)
+{
+ optimize = o;
+}
+
Program *ProgramBuilder::create_program() const
{
Program *prog = new Program;
}
}
+ if(optimize)
+ {
+ for(list<ShaderVariable *>::const_iterator i=resolved_vars.begin(); i!=resolved_vars.end(); ++i)
+ (*i)->check_inline();
+ }
+
prog.attach_shader_owned(new VertexShader(create_source(resolved_vars, VERTEX)));
prog.attach_shader_owned(new FragmentShader(create_source(resolved_vars, FRAGMENT)));
+
+ if(features.normalmap)
+ {
+ prog.bind_attribute(get_component_type(TANGENT3), "tangent");
+ prog.bind_attribute(get_component_type(BINORMAL3), "binormal");
+ }
}
string ProgramBuilder::create_source(const list<ShaderVariable *> &variables, VariableScope scope) const
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)
- if((*i)->variable->scope==scope)
+ if((*i)->variable->scope==scope && !(*i)->inlined)
{
source += '\t';
if((*i)->variable->type)
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);
+ {
+ if((*i)->inlined)
+ source += format("\tv_%s = %s;\n", (*i)->resolved_name, (*i)->get_expression());
+ else
+ source += format("\tv_%s = %s;\n", (*i)->resolved_name, (*i)->resolved_name);
+ }
}
source += '}';
{
vector<string> result;
const char *ptr = expression;
- unsigned start, length;
+ unsigned start = 0;
+ unsigned length = 0;
while(parse_identifier(ptr, start, length))
{
result.push_back(string(ptr+start, length));
{
string result;
const char *ptr = expression;
- unsigned start, length;
+ unsigned start = 0;
+ unsigned length = 0;
while(parse_identifier(ptr, start, length))
{
result.append(ptr, start);
normalmap(false),
shadow(false),
reflection(false),
- transform(false)
+ transform(false),
+ colorify(false)
{ }
string ProgramBuilder::StandardFeatures::create_flags() const
flags += 'e';
if(transform)
flags += 'r';
+ if(colorify)
+ flags += 'c';
return flags;
}
name(n),
variable(0),
resolved_name(n),
- fuzzy_space(name.find("zzz")!=string::npos)
+ fuzzy_space(name.find("zzz")!=string::npos),
+ inlined(false),
+ inline_parens(false)
{ }
void ProgramBuilder::ShaderVariable::resolve(const StandardVariable &var)
{
for(list<ShaderVariable *>::iterator i=referenced_by.begin(); i!=referenced_by.end(); ++i)
(*i)->update_reference(*this, var);
+ var.referenced_by.insert(var.referenced_by.end(), referenced_by.begin(), referenced_by.end());
}
void ProgramBuilder::ShaderVariable::resolve_space(const string &space)
resolve_space(to.resolved_space);
}
+void ProgramBuilder::ShaderVariable::check_inline()
+{
+ if(variable->expression)
+ {
+ unsigned total_refs = referenced_by.size();
+ unsigned in_scope_refs = 0;
+ for(list<ShaderVariable *>::const_iterator i=referenced_by.begin(); i!=referenced_by.end(); ++i)
+ if((*i)->variable->scope==variable->scope)
+ ++in_scope_refs;
+ if(total_refs==1 || (total_refs>0 && in_scope_refs==0))
+ {
+ inlined = true;
+ unsigned level = 0;
+ for(const char *c=variable->expression; (!inline_parens && *c); ++c)
+ {
+ if(*c=='(')
+ ++level;
+ else if(*c==')')
+ --level;
+ else if(level==0 && !isalnum(*c) && *c!='_' && *c!='.')
+ inline_parens = true;
+ }
+ }
+ }
+}
+
bool ProgramBuilder::ShaderVariable::is_referenced_from(VariableScope scope) const
{
for(list<ShaderVariable *>::const_iterator i=referenced_by.begin(); i!=referenced_by.end(); ++i)
for(list<ShaderVariable *>::const_iterator i=referenced_vars.begin(); i!=referenced_vars.end(); ++i)
if((*i)->variable)
{
- string var_name = (*i)->resolved_name;
+ string replacement = (*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;
+ replacement = "v_"+replacement;
+ else if((*i)->inlined)
+ {
+ replacement = (*i)->get_expression();
+ if((*i)->inline_parens)
+ replacement = "("+replacement+")";
+ }
+ if(replacement!=(*i)->name)
+ replace_map[(*i)->name] = replacement;
}
if(replace_map.empty())
ProgramBuilder::StandardFeatures::Loader::Loader(StandardFeatures &f):
DataFile::ObjectLoader<StandardFeatures>(f)
{
+ add("colorify", &StandardFeatures::colorify);
add("lighting", &StandardFeatures::lighting);
add("material", &StandardFeatures::material);
add("normalmap", &StandardFeatures::normalmap);