]> git.tdb.fi Git - libs/gl.git/commitdiff
Refactor certain variable operations into functions
authorMikko Rasa <tdb@tdb.fi>
Thu, 15 May 2014 09:00:01 +0000 (12:00 +0300)
committerMikko Rasa <tdb@tdb.fi>
Thu, 15 May 2014 09:55:42 +0000 (12:55 +0300)
source/programbuilder.cpp
source/programbuilder.h

index bf89e7db8270ddb552ab8d3eebe49bbd687047aa..28acc5d941f2f24405bc1d5302e795ecb348c8b1 100644 (file)
@@ -116,7 +116,7 @@ const ProgramBuilder::VariableDefinition ProgramBuilder::standard_variables[] =
        { 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 },
 
@@ -325,19 +325,14 @@ string ProgramBuilder::create_source(const list<ShaderVariable *> &variables, Va
 
        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
@@ -352,12 +347,12 @@ string ProgramBuilder::create_source(const list<ShaderVariable *> &variables, Va
                                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";
 
@@ -365,12 +360,11 @@ string ProgramBuilder::create_source(const list<ShaderVariable *> &variables, Va
                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)
@@ -378,7 +372,7 @@ string ProgramBuilder::create_source(const list<ShaderVariable *> &variables, Va
                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);
                }
@@ -678,24 +672,48 @@ bool ProgramBuilder::ShaderVariable::is_referenced_from(VariableScope scope) con
        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;
index 8275e4579556e91aabd3c1b6d39d63698146b34d..821907d5dba46a281323c3364e1b85095426296e 100644 (file)
@@ -84,7 +84,9 @@ private:
                void update_reference(ShaderVariable &, ShaderVariable &);
                void check_inline(bool, bool);
                bool is_referenced_from(VariableScope) const;
-               std::string get_expression() const;
+               std::string create_declaration(char = 0) const;
+               std::string create_replacement(VariableScope) const;
+               std::string create_expression() const;
        };
 
        enum MatchLevel