From 739769e449eac552813b01f8dbec89a579443715 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Tue, 13 May 2014 12:21:28 +0300 Subject: [PATCH] Tweak goal variable handling in ProgramBuilder 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 | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/source/programbuilder.cpp b/source/programbuilder.cpp index 97f27a85..0f29675f 100644 --- a/source/programbuilder.cpp +++ b/source/programbuilder.cpp @@ -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 &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::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 &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) -- 2.43.0