Prefix user-defined names in GLSL output

Now user-defined names are prefixed by _u in GLSL output in case name
hashing is not on. Internal names such as names of temporary variables
created in AST transformations are written out as such.

This makes handling of internal function names and internal variable
names consistent. It also removes the possibility of name conflicts
between user-defined names and internal names in case name hashing is
not on. In the same vein, it makes it safe to use GLSL reserved words
that are not reserved in ESSL as variable names in case name hashing
is not on.

This also makes the GLSL output more consistent with how names are
handled in HLSL output. Name hashing code is shared between
VariableInfo and OutputGLSLBase to ensure names are handled
consistently in both. The name that's used in the shader source for a
given interface variable is written out to ShaderVariable::mappedName.

An exception needs to be made for identifiers close to the length
limit, since adding any prefix would take them over the limit. But
they can be just written out as such, since we don't have any builtins
or ANGLE internal variables that have as long names and could create a
conflict.

BUG=angleproject:2139
BUG=angleproject:2038
TEST=angle_unittests, angle_end2end_tests, WebGL conformance tests

Change-Id: Id6ed052c4fab2d091227dc9a3668083053b67a38
Reviewed-on: https://chromium-review.googlesource.com/507647
Commit-Queue: Olli Etuaho <oetuaho@nvidia.com>
Reviewed-by: Jamie Madill <jmadill@chromium.org>
diff --git a/src/libANGLE/renderer/gl/ProgramGL.cpp b/src/libANGLE/renderer/gl/ProgramGL.cpp
index 2e3c64b..95770a2 100644
--- a/src/libANGLE/renderer/gl/ProgramGL.cpp
+++ b/src/libANGLE/renderer/gl/ProgramGL.cpp
@@ -144,13 +144,16 @@
     else
     {
         // Set the transform feedback state
-        std::vector<const GLchar *> transformFeedbackVaryings;
+        std::vector<std::string> transformFeedbackVaryingMappedNames;
         for (const auto &tfVarying : mState.getTransformFeedbackVaryingNames())
         {
-            transformFeedbackVaryings.push_back(tfVarying.c_str());
+            std::string tfVaryingMappedName =
+                mState.getAttachedVertexShader()->getTransformFeedbackVaryingMappedName(tfVarying,
+                                                                                        context);
+            transformFeedbackVaryingMappedNames.push_back(tfVaryingMappedName);
         }
 
-        if (transformFeedbackVaryings.empty())
+        if (transformFeedbackVaryingMappedNames.empty())
         {
             if (mFunctions->transformFeedbackVaryings)
             {
@@ -161,8 +164,13 @@
         else
         {
             ASSERT(mFunctions->transformFeedbackVaryings);
+            std::vector<const GLchar *> transformFeedbackVaryings;
+            for (const auto &varying : transformFeedbackVaryingMappedNames)
+            {
+                transformFeedbackVaryings.push_back(varying.c_str());
+            }
             mFunctions->transformFeedbackVaryings(
-                mProgramID, static_cast<GLsizei>(transformFeedbackVaryings.size()),
+                mProgramID, static_cast<GLsizei>(transformFeedbackVaryingMappedNames.size()),
                 &transformFeedbackVaryings[0], mState.getTransformFeedbackBufferMode());
         }
 
@@ -181,7 +189,8 @@
                 continue;
             }
 
-            mFunctions->bindAttribLocation(mProgramID, attribute.location, attribute.name.c_str());
+            mFunctions->bindAttribLocation(mProgramID, attribute.location,
+                                           attribute.mappedName.c_str());
         }
 
         // Link and verify
@@ -501,8 +510,9 @@
         mUniformBlockRealLocationMap.reserve(mState.getUniformBlocks().size());
         for (const gl::UniformBlock &uniformBlock : mState.getUniformBlocks())
         {
-            const std::string &nameWithIndex = uniformBlock.nameWithArrayIndex();
-            GLuint blockIndex = mFunctions->getUniformBlockIndex(mProgramID, nameWithIndex.c_str());
+            const std::string &mappedNameWithIndex = uniformBlock.mappedNameWithArrayIndex();
+            GLuint blockIndex =
+                mFunctions->getUniformBlockIndex(mProgramID, mappedNameWithIndex.c_str());
             mUniformBlockRealLocationMap.push_back(blockIndex);
         }
     }
@@ -519,11 +529,13 @@
     return mProgramID;
 }
 
-bool ProgramGL::getUniformBlockSize(const std::string &blockName, size_t *sizeOut) const
+bool ProgramGL::getUniformBlockSize(const std::string & /* blockName */,
+                                    const std::string &blockMappedName,
+                                    size_t *sizeOut) const
 {
     ASSERT(mProgramID != 0u);
 
-    GLuint blockIndex = mFunctions->getUniformBlockIndex(mProgramID, blockName.c_str());
+    GLuint blockIndex = mFunctions->getUniformBlockIndex(mProgramID, blockMappedName.c_str());
     if (blockIndex == GL_INVALID_INDEX)
     {
         *sizeOut = 0;
@@ -537,11 +549,12 @@
     return true;
 }
 
-bool ProgramGL::getUniformBlockMemberInfo(const std::string &memberUniformName,
+bool ProgramGL::getUniformBlockMemberInfo(const std::string & /* memberUniformName */,
+                                          const std::string &memberUniformMappedName,
                                           sh::BlockMemberInfo *memberInfoOut) const
 {
     GLuint uniformIndex;
-    const GLchar *memberNameGLStr = memberUniformName.c_str();
+    const GLchar *memberNameGLStr = memberUniformMappedName.c_str();
     mFunctions->getUniformIndices(mProgramID, 1, &memberNameGLStr, &uniformIndex);
 
     if (uniformIndex == GL_INVALID_INDEX)
@@ -574,7 +587,7 @@
 
     for (const auto &input : mPathRenderingFragmentInputs)
     {
-        if (input.name == inputName)
+        if (input.mappedName == inputName)
         {
             mFunctions->programPathFragmentInputGenNV(mProgramID, input.location, genMode,
                                                       components, coeffs);
@@ -650,7 +663,7 @@
         // "Locations for sequential array indices are not required to be sequential."
         const gl::LinkedUniform &uniform = uniforms[entry.index];
         std::stringstream fullNameStr;
-        fullNameStr << uniform.name;
+        fullNameStr << uniform.mappedName;
         if (uniform.isArray())
         {
             fullNameStr << "[" << entry.element << "]";
@@ -664,7 +677,7 @@
     if (mState.usesMultiview())
     {
         mMultiviewBaseViewLayerIndexUniformLocation =
-            mFunctions->getUniformLocation(mProgramID, "webgl_angle_multiviewBaseViewLayerIndex");
+            mFunctions->getUniformLocation(mProgramID, "multiviewBaseViewLayerIndex");
         ASSERT(mMultiviewBaseViewLayerIndexUniformLocation != -1);
     }
 
@@ -685,16 +698,16 @@
 
     for (GLint i = 0; i < numFragmentInputs; ++i)
     {
-        std::string name;
-        name.resize(maxNameLength);
+        std::string mappedName;
+        mappedName.resize(maxNameLength);
 
         GLsizei nameLen = 0;
         mFunctions->getProgramResourceName(mProgramID, GL_FRAGMENT_INPUT_NV, i, maxNameLength,
-                                           &nameLen, &name[0]);
-        name.resize(nameLen);
+                                           &nameLen, &mappedName[0]);
+        mappedName.resize(nameLen);
 
         // Ignore built-ins
-        if (angle::BeginsWith(name, "gl_"))
+        if (angle::BeginsWith(mappedName, "gl_"))
             continue;
 
         const GLenum kQueryProperties[] = {GL_LOCATION, GL_ARRAY_SIZE};
@@ -709,16 +722,16 @@
         ASSERT(queryLength == static_cast<GLsizei>(ArraySize(kQueryProperties)));
 
         PathRenderingFragmentInput baseElementInput;
-        baseElementInput.name     = name;
+        baseElementInput.mappedName = mappedName;
         baseElementInput.location = queryResults[0];
         mPathRenderingFragmentInputs.push_back(std::move(baseElementInput));
 
         // If the input is an array it's denoted by [0] suffix on the variable
         // name. We'll then create an entry per each array index where index > 0
-        if (angle::EndsWith(name, "[0]"))
+        if (angle::EndsWith(mappedName, "[0]"))
         {
             // drop the suffix
-            name.resize(name.size() - 3);
+            mappedName.resize(mappedName.size() - 3);
 
             const auto arraySize    = queryResults[1];
             const auto baseLocation = queryResults[0];
@@ -726,7 +739,7 @@
             for (GLint arrayIndex = 1; arrayIndex < arraySize; ++arrayIndex)
             {
                 PathRenderingFragmentInput arrayElementInput;
-                arrayElementInput.name     = name + "[" + ToString(arrayIndex) + "]";
+                arrayElementInput.mappedName = mappedName + "[" + ToString(arrayIndex) + "]";
                 arrayElementInput.location = baseLocation + arrayIndex;
                 mPathRenderingFragmentInputs.push_back(std::move(arrayElementInput));
             }