Transform feedback varyings no longer linked when unused

In OpenGL ES 3.0, varyings declared in the vertex shader may be
used by transform feedback, regardless of whether or not they are
also declared in the fragment shader. In OpenGL ES 2.0, these
unmatched varyings are usually discarded. In order to preserve
the OpenGL ES 2.0 behavior and to not burden the Open GL ES 3.0
implementation with new unused varyings, only the unmatched
varyings which may be used as transform feedback inputs will be
added to the vertex shader, instead of always adding all of them.

Change-Id: I35f37b6ee77181b5d3a47605ef246c7d1ecf904d
Reviewed-on: https://swiftshader-review.googlesource.com/9808
Tested-by: Alexis Hétu <sugoi@google.com>
Reviewed-by: Nicolas Capens <capn@google.com>
diff --git a/src/OpenGL/libGLESv2/Program.cpp b/src/OpenGL/libGLESv2/Program.cpp
index 4fcadea..400da5d 100644
--- a/src/OpenGL/libGLESv2/Program.cpp
+++ b/src/OpenGL/libGLESv2/Program.cpp
@@ -1353,24 +1353,33 @@
 				}
 			}
 
-			// For openGL ES 3.0, we need to still add the vertex shader outputs for unmatched varyings, for transform feedback.
-			if(!matched && (egl::getClientVersion() >= 3))
+			if(!matched)
 			{
-				int out = output->reg;
-				int components = VariableRegisterSize(output->type);
-				int registers = VariableRegisterCount(output->type) * output->size();
-
-				if(out >= 0)
+				// For openGL ES 3.0, we need to still add the vertex shader outputs for unmatched varyings, for transform feedback.
+				for(const std::string &indexedTfVaryingName : transformFeedbackVaryings)
 				{
-					if(out + registers > MAX_VARYING_VECTORS)
-					{
-						appendToInfoLog("Too many varyings");
-						return false;
-					}
+					std::string tfVaryingName = es2::ParseUniformName(indexedTfVaryingName, nullptr);
 
-					for(int i = 0; i < registers; i++)
+					if(tfVaryingName == output->name)
 					{
-						vertexBinary->setOutput(out + i, components, sw::Shader::Semantic(sw::Shader::USAGE_COLOR));
+						int out = output->reg;
+						int components = VariableRegisterSize(output->type);
+						int registers = VariableRegisterCount(output->type) * output->size();
+
+						if(out >= 0)
+						{
+							if(out + registers > MAX_VARYING_VECTORS)
+							{
+								appendToInfoLog("Too many varyings");
+								return false;
+							}
+
+							for(int i = 0; i < registers; i++)
+							{
+								vertexBinary->setOutput(out + i, components, sw::Shader::Semantic(sw::Shader::USAGE_COLOR));
+							}
+						}
+						break;
 					}
 				}
 			}