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/Program.cpp b/src/libANGLE/Program.cpp
index 5a15847..b5b8b77 100644
--- a/src/libANGLE/Program.cpp
+++ b/src/libANGLE/Program.cpp
@@ -2799,32 +2799,40 @@
 template <typename VarT>
 void Program::defineUniformBlockMembers(const std::vector<VarT> &fields,
                                         const std::string &prefix,
+                                        const std::string &mappedPrefix,
                                         int blockIndex)
 {
     for (const VarT &field : fields)
     {
         const std::string &fullName = (prefix.empty() ? field.name : prefix + "." + field.name);
 
+        const std::string &fullMappedName =
+            (mappedPrefix.empty() ? field.mappedName : mappedPrefix + "." + field.mappedName);
+
         if (field.isStruct())
         {
             for (unsigned int arrayElement = 0; arrayElement < field.elementCount(); arrayElement++)
             {
                 const std::string uniformElementName =
                     fullName + (field.isArray() ? ArrayString(arrayElement) : "");
-                defineUniformBlockMembers(field.fields, uniformElementName, blockIndex);
+                const std::string uniformElementMappedName =
+                    fullMappedName + (field.isArray() ? ArrayString(arrayElement) : "");
+                defineUniformBlockMembers(field.fields, uniformElementName,
+                                          uniformElementMappedName, blockIndex);
             }
         }
         else
         {
             // If getBlockMemberInfo returns false, the uniform is optimized out.
             sh::BlockMemberInfo memberInfo;
-            if (!mProgram->getUniformBlockMemberInfo(fullName, &memberInfo))
+            if (!mProgram->getUniformBlockMemberInfo(fullName, fullMappedName, &memberInfo))
             {
                 continue;
             }
 
             LinkedUniform newUniform(field.type, field.precision, fullName, field.arraySize, -1, -1,
                                      -1, blockIndex, memberInfo);
+            newUniform.mappedName = fullMappedName;
 
             // Since block uniforms have no location, we don't need to store them in the uniform
             // locations list.
@@ -2841,7 +2849,8 @@
     // Track the first and last uniform index to determine the range of active uniforms in the
     // block.
     size_t firstBlockUniformIndex = mState.mUniforms.size();
-    defineUniformBlockMembers(interfaceBlock.fields, interfaceBlock.fieldPrefix(), blockIndex);
+    defineUniformBlockMembers(interfaceBlock.fields, interfaceBlock.fieldPrefix(),
+                              interfaceBlock.fieldMappedPrefix(), blockIndex);
     size_t lastBlockUniformIndex = mState.mUniforms.size();
 
     std::vector<unsigned int> blockUniformIndexes;
@@ -2859,12 +2868,13 @@
         for (unsigned int arrayElement = 0; arrayElement < interfaceBlock.arraySize; ++arrayElement)
         {
             // Don't define this block at all if it's not active in the implementation.
-            if (!mProgram->getUniformBlockSize(interfaceBlock.name + ArrayString(arrayElement),
-                                               &blockSize))
+            if (!mProgram->getUniformBlockSize(
+                    interfaceBlock.name + ArrayString(arrayElement),
+                    interfaceBlock.mappedName + ArrayString(arrayElement), &blockSize))
             {
                 continue;
             }
-            UniformBlock block(interfaceBlock.name, true, arrayElement,
+            UniformBlock block(interfaceBlock.name, interfaceBlock.mappedName, true, arrayElement,
                                blockBinding + arrayElement);
             block.memberIndexes = blockUniformIndexes;
 
@@ -2898,11 +2908,12 @@
     }
     else
     {
-        if (!mProgram->getUniformBlockSize(interfaceBlock.name, &blockSize))
+        if (!mProgram->getUniformBlockSize(interfaceBlock.name, interfaceBlock.mappedName,
+                                           &blockSize))
         {
             return;
         }
-        UniformBlock block(interfaceBlock.name, false, 0, blockBinding);
+        UniformBlock block(interfaceBlock.name, interfaceBlock.mappedName, false, 0, blockBinding);
         block.memberIndexes = blockUniformIndexes;
 
         switch (shaderType)