]> git.tdb.fi Git - libs/gl.git/commitdiff
Tweak goal variable handling in ProgramBuilder
authorMikko Rasa <tdb@tdb.fi>
Tue, 13 May 2014 09:21:28 +0000 (12:21 +0300)
committerMikko Rasa <tdb@tdb.fi>
Tue, 13 May 2014 16:26:00 +0000 (19:26 +0300)
All variables now have types, and goal variables are recognized by the
lack of references to them.  This solves a problem where non-optimized
legacy shaders would emit an assignment to frag_color without declaring
it first.

source/programbuilder.cpp

index 97f27a8596f703b383b94121ff134e9e4deaec8d..0f29675f19dd3e45e4a63633747c6d31093f5680 100644 (file)
@@ -41,10 +41,10 @@ Naming conventions:
 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" },
@@ -82,7 +82,7 @@ const ProgramBuilder::VariableDefinition ProgramBuilder::standard_variables[] =
        { 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 },
@@ -358,8 +358,9 @@ string ProgramBuilder::create_source(const list<ShaderVariable *> &variables, Va
                        source += format("%s %s v_%s;\n", qualifier, (*i)->variable->type, (*i)->resolved_name);
                }
 
-       if(scope==FRAGMENT && !features.legacy)
-               source += "out vec4 frag_color;\n";
+       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 += "void main()\n{\n";
 
@@ -367,7 +368,7 @@ string ProgramBuilder::create_source(const list<ShaderVariable *> &variables, Va
                if((*i)->variable->scope==scope && !(*i)->inlined)
                {
                        source += '\t';
-                       if((*i)->variable->type)
+                       if(!(*i)->referenced_by.empty())
                        {
                                source += (*i)->variable->type;
                                source += ' ';
@@ -577,6 +578,7 @@ void ProgramBuilder::ShaderVariable::resolve(const VariableDefinition &var)
        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)