anything that might need them. */
const ProgramBuilder::VariableDefinition ProgramBuilder::standard_variables[] =
{
- { FRAGMENT, "gl_FragColor", 0, "frag_color", "g" },
- { FRAGMENT, "frag_color", 0, "color_base", "!t" },
- { FRAGMENT, "frag_color", 0, "tex_sample", "!l!s!mt" },
- { FRAGMENT, "frag_color", 0, "tex_sample*color_base", "l|s|mt" },
+ { FRAGMENT, "gl_FragColor", "vec4", "frag_color", "g" },
+ { FRAGMENT, "frag_color", "vec4", "color_base", "!t" },
+ { FRAGMENT, "frag_color", "vec4", "tex_sample", "!l!s!mt" },
+ { FRAGMENT, "frag_color", "vec4", "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, "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+ambient_product_diffuse.rgb", "m" },
+ { FRAGMENT, "rgb_light_full", "vec3", "rgb_light_shadow", "!m" },
{ FRAGMENT, "ambient_product_diffuse", "vec4", "gl_FrontLightModelProduct.sceneColor", "g" },
{ FRAGMENT, "ambient_product_diffuse", "vec4", "ambient_color*material.diffuse", "!g" },
- { 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, "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, "gl_Position", "vec4", "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, "shd_vertex", "vec3", "(shd_eye_matrix*eye_vertex).xyz", "!g" },
{ VERTEX, "tbn_light_dir", "vec3", "eye_light_dir*eye_tbn_matrix", 0 },
{ UNIFORM, "eye_obj_matrix", "mat4", "gl_ModelViewMatrix", 0 },
{ UNIFORM, "eye_obj_normal_matrix", "mat3", "gl_NormalMatrix", 0 },
{ UNIFORM, "projection_matrix", "mat4", "gl_ProjectionMatrix", 0 },
- { UNIFORM, "shd_eye_matrix", "mat4", 0, "!g" },
- { UNIFORM, "light_sources", "struct { vec4 position; vec4 diffuse; vec4 specular; } %s[2]", "gl_LightSource", 0 },
+ { UNIFORM, "shd_eye_matrix", "mat4", 0, 0 },
+ { UNIFORM, "light_sources", "struct { vec4 position; vec4 diffuse; vec4 specular; }[2]", "gl_LightSource", 0 },
{ UNIFORM, "ambient_color", "vec4", 0, 0 },
{ UNIFORM, "material", "struct { vec4 ambient; vec4 diffuse; vec4 specular; float shininess; }", "gl_FrontMaterial", 0 },
// Terminator entry
- { NO_SCOPE, 0, 0, 0, 0 }
+ { NO_SCOPE, 0, 0, 0, 0 }
};
+const char ProgramBuilder::interfaces[] = { 0, 0, 0, 'v', 0 };
+
ProgramBuilder::ProgramBuilder(const StandardFeatures &f):
features(f),
feature_flags(features.create_flags()),
}
}
- if(optimize)
- {
- for(list<ShaderVariable *>::const_iterator i=resolved_vars.begin(); i!=resolved_vars.end(); ++i)
- (*i)->check_inline(features.legacy);
- }
+ for(list<ShaderVariable *>::const_iterator i=resolved_vars.begin(); i!=resolved_vars.end(); ++i)
+ (*i)->check_inline(features.legacy, !optimize);
prog.attach_shader_owned(new VertexShader(create_source(resolved_vars, VERTEX)));
prog.attach_shader_owned(new FragmentShader(create_source(resolved_vars, FRAGMENT)));
for(list<ShaderVariable *>::const_iterator i=variables.begin(); i!=variables.end(); ++i)
if((*i)->variable->scope==UNIFORM && (*i)->is_referenced_from(scope) && !(*i)->inlined)
- {
- if(strchr((*i)->variable->type, '%'))
- source += format("uniform %s;\n", format((*i)->variable->type, (*i)->resolved_name));
- else
- source += format("uniform %s %s;\n", (*i)->variable->type, (*i)->resolved_name);
- }
+ source += format("uniform %s;\n", (*i)->create_declaration());
- if(scope==VERTEX)
+ /* Interface variables need to have global declarations. */
+ for(list<ShaderVariable *>::const_iterator i=variables.begin(); i!=variables.end(); ++i)
{
- const char *qualifier = (features.legacy ? "attribute" : "in");
- for(list<ShaderVariable *>::const_iterator i=variables.begin(); i!=variables.end(); ++i)
- if((*i)->variable->scope==ATTRIBUTE && !(*i)->inlined)
- source += format("%s %s %s;\n", qualifier, (*i)->variable->type, (*i)->resolved_name);
- }
+ if(!(*i)->resolved_name.compare(0, 3, "gl_"))
+ continue;
- /* 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)->variable->scope==ATTRIBUTE) && (*i)->is_referenced_from(FRAGMENT))
+ InterfaceFlags interface = (*i)->get_interface_flags(scope);
+
+ if(interface&INPUT)
{
- varyings.push_back(*i);
- const char *qualifier;
- if(!features.legacy)
- qualifier = (scope==VERTEX ? "out" : "in");
- else
- qualifier = "varying";
- source += format("%s %s v_%s;\n", qualifier, (*i)->variable->type, (*i)->resolved_name);
+ const char *qualifier = (features.legacy ? scope==VERTEX ? "attribute" : "varying" : "in");
+ source += format("%s %s;\n", qualifier, (*i)->create_declaration(interfaces[scope-1]));
}
- if(scope==FRAGMENT && !features.legacy)
- source += "out vec4 frag_color;\n";
+ if(interface&OUTPUT)
+ {
+ const char *qualifier = (features.legacy ? "varying" : "out");
+ source += format("%s %s;\n", qualifier, (*i)->create_declaration(interfaces[scope]));
+ }
+ }
source += "void main()\n{\n";
for(list<ShaderVariable *>::const_iterator i=variables.begin(); i!=variables.end(); ++i)
+ {
+ InterfaceFlags interface = (*i)->get_interface_flags(scope);
+
if((*i)->variable->scope==scope && !(*i)->inlined)
{
- source += '\t';
- if((*i)->variable->type)
- {
- source += (*i)->variable->type;
- source += ' ';
- }
- source += format("%s = %s;\n", (*i)->resolved_name, (*i)->get_expression());
+ string decl = ((interface&GOAL) ? (*i)->resolved_name : (*i)->create_declaration());
+ source += format("\t%s = %s;\n", decl, (*i)->create_expression());
}
- if(scope==VERTEX)
- {
- for(list<ShaderVariable *>::const_iterator i=varyings.begin(); i!=varyings.end(); ++i)
+ if((interface&(OUTPUT|GOAL))==OUTPUT)
{
- 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);
+ string expr = ((*i)->inlined ? (*i)->create_expression() : (*i)->resolved_name);
+ source += format("\t%c_%s = %s;\n", interfaces[scope], (*i)->resolved_name, expr);
}
}
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)
resolve_space(to.resolved_space);
}
-void ProgramBuilder::ShaderVariable::check_inline(bool allow_legacy)
+void ProgramBuilder::ShaderVariable::check_inline(bool allow_legacy, bool trivial_only)
{
if(variable->expression)
{
if(!allow_legacy && !strncmp(variable->expression, "gl_", 3))
return;
+ // Never inline goal variables
unsigned total_refs = referenced_by.size();
+ if(!total_refs)
+ return;
+
+ // Inline an expression consisting of a single identifier
+ unsigned start, length;
+ if(parse_identifier(variable->expression, start, length))
+ if(start==0 && variable->expression[length]==0)
+ {
+ inlined = true;
+ return;
+ }
+
+ if(trivial_only)
+ return;
+
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))
+
+ /* Inline if there's only one ref, or if all refs are in other scopes.
+ In the latter case, the actual inlining will happen in the interface
+ variable assignment. */
+ if(total_refs==1 || in_scope_refs==0)
{
inlined = true;
unsigned level = 0;
return false;
}
-string ProgramBuilder::ShaderVariable::get_expression() const
+ProgramBuilder::InterfaceFlags ProgramBuilder::ShaderVariable::get_interface_flags(VariableScope scope) const
{
- map<string, string> replace_map;
- for(list<ShaderVariable *>::const_iterator i=referenced_vars.begin(); i!=referenced_vars.end(); ++i)
- if((*i)->variable)
+ /* Uniforms are available to all stages and are not passed through
+ interfaces */
+ if(variable->scope==UNIFORM)
+ return NO_INTERFACE;
+
+ int flags = NO_INTERFACE;
+
+ for(list<ShaderVariable *>::const_iterator i=referenced_by.begin(); i!=referenced_by.end(); ++i)
+ {
+ /* Variables used in a later scope than they are declared in need to go
+ through the interface */
+ if((*i)->variable->scope>scope && variable->scope<=scope)
+ flags |= OUTPUT;
+ if((*i)->variable->scope>=scope && variable->scope<scope)
+ if(!inlined || variable->scope!=ATTRIBUTE || scope!=VERTEX)
+ flags |= INPUT;
+ }
+
+ // Variables without any references are goals and also outputs.
+ if(referenced_by.empty() && variable->scope==scope)
+ flags |= OUTPUT|GOAL;
+
+ return static_cast<InterfaceFlags>(flags);
+}
+
+string ProgramBuilder::ShaderVariable::create_declaration(char interface) const
+{
+ if(variable->scope==UNIFORM)
+ {
+ const char *bracket = strrchr(variable->type, '[');
+ if(bracket)
+ return format("%s %s%s", string(variable->type, bracket), resolved_name, bracket);
+ }
+
+ if(interface)
+ return format("%s %c_%s", variable->type, interface, resolved_name);
+ else
+ return format("%s %s", variable->type, resolved_name);
+}
+
+string ProgramBuilder::ShaderVariable::create_replacement(VariableScope from_scope) const
+{
+ string replacement = resolved_name;
+ if(variable)
+ {
+ InterfaceFlags interface = get_interface_flags(from_scope);
+ if((interface&INPUT) && interfaces[from_scope-1])
+ replacement = format("%c_%s", interfaces[from_scope-1], replacement);
+ else if(inlined)
{
- string replacement = (*i)->resolved_name;
- if(variable->scope==FRAGMENT && ((*i)->variable->scope==VERTEX || (*i)->variable->scope==ATTRIBUTE))
- 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;
+ replacement = create_expression();
+ if(inline_parens)
+ replacement = "("+replacement+")";
}
+ }
+
+ return replacement;
+}
+
+string ProgramBuilder::ShaderVariable::create_expression() const
+{
+ map<string, string> replace_map;
+ for(list<ShaderVariable *>::const_iterator i=referenced_vars.begin(); i!=referenced_vars.end(); ++i)
+ {
+ string replacement = (*i)->create_replacement(variable->scope);
+ if(replacement!=(*i)->name)
+ replace_map[(*i)->name] = replacement;
+ }
if(replace_map.empty())
return variable->expression;