{ UNIFORM, "eye_obj_normal_matrix", "mat3", "gl_NormalMatrix", 0 },
{ UNIFORM, "projection_matrix", "mat4", "gl_ProjectionMatrix", 0 },
{ UNIFORM, "shd_eye_matrix", "mat4", 0, 0 },
- { UNIFORM, "light_sources", "struct { vec4 position; vec4 diffuse; vec4 specular; } %s[2]", "gl_LightSource", 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 },
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)
{
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);
+ source += format("%s %s;\n", qualifier, (*i)->create_declaration());
}
/* Any variables defined in vertex scope but referenced from fragment scope
qualifier = (scope==VERTEX ? "out" : "in");
else
qualifier = "varying";
- source += format("%s %s v_%s;\n", qualifier, (*i)->variable->type, (*i)->resolved_name);
+ source += format("%s %s;\n", qualifier, (*i)->create_declaration('v'));
}
for(list<ShaderVariable *>::const_iterator i=variables.begin(); i!=variables.end(); ++i)
if((*i)->referenced_by.empty() && (*i)->resolved_name.compare(0, 3, "gl_"))
- source += format("out %s %s;\n", (*i)->variable->type, (*i)->resolved_name);
+ source += format("out %s;\n", (*i)->create_declaration());
source += "void main()\n{\n";
if((*i)->variable->scope==scope && !(*i)->inlined)
{
source += '\t';
- if(!(*i)->referenced_by.empty())
- {
- source += (*i)->variable->type;
- source += ' ';
- }
- source += format("%s = %s;\n", (*i)->resolved_name, (*i)->get_expression());
+ if((*i)->referenced_by.empty())
+ source += (*i)->resolved_name;
+ else
+ source += (*i)->create_declaration();
+ source += format(" = %s;\n", (*i)->create_expression());
}
if(scope==VERTEX)
for(list<ShaderVariable *>::const_iterator i=varyings.begin(); i!=varyings.end(); ++i)
{
if((*i)->inlined)
- source += format("\tv_%s = %s;\n", (*i)->resolved_name, (*i)->get_expression());
+ source += format("\tv_%s = %s;\n", (*i)->resolved_name, (*i)->create_expression());
else
source += format("\tv_%s = %s;\n", (*i)->resolved_name, (*i)->resolved_name);
}
return false;
}
-string ProgramBuilder::ShaderVariable::get_expression() const
+string ProgramBuilder::ShaderVariable::create_declaration(char interface) const
{
- map<string, string> replace_map;
- for(list<ShaderVariable *>::const_iterator i=referenced_vars.begin(); i!=referenced_vars.end(); ++i)
- if((*i)->variable)
+ 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)
+ {
+ if(from_scope==FRAGMENT && (variable->scope==VERTEX || variable->scope==ATTRIBUTE))
+ replacement = "v_"+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;