Add default compiler options to WebGL shaders.

Enforces default compiler options when compiling WebGL compatible shaders.

BUG=angleproject:1616

Change-Id: I18490db68b29981fab4817bdd61727752cf50997
Reviewed-on: https://chromium-review.googlesource.com/409016
Commit-Queue: Bryan Bernhart <bryan.bernhart@intel.com>
Commit-Queue: Jamie Madill <jmadill@chromium.org>
Reviewed-by: Geoff Lang <geofflang@chromium.org>
diff --git a/src/libANGLE/Shader.cpp b/src/libANGLE/Shader.cpp
index e65eb8e..a4c6895 100644
--- a/src/libANGLE/Shader.cpp
+++ b/src/libANGLE/Shader.cpp
@@ -20,6 +20,7 @@
 #include "libANGLE/renderer/GLImplFactory.h"
 #include "libANGLE/renderer/ShaderImpl.h"
 #include "libANGLE/ResourceManager.h"
+#include "libANGLE/Context.h"
 
 namespace gl
 {
@@ -227,7 +228,7 @@
     getSourceImpl(debugInfo, bufSize, length, buffer);
 }
 
-void Shader::compile(Compiler *compiler)
+void Shader::compile(const Context *context)
 {
     mState.mTranslatedSource.clear();
     mInfoLog.clear();
@@ -238,6 +239,7 @@
     mState.mActiveAttributes.clear();
     mState.mActiveOutputVariables.clear();
 
+    Compiler *compiler = context->getCompiler();
     ShHandle compilerHandle = compiler->getCompilerHandle(mState.mShaderType);
 
     std::stringstream sourceStream;
@@ -247,6 +249,14 @@
         mImplementation->prepareSourceAndReturnOptions(&sourceStream, &sourcePath);
     ShCompileOptions compileOptions = (SH_OBJECT_CODE | SH_VARIABLES | additionalOptions);
 
+    // Add default options to WebGL shaders to prevent unexpected behavior during compilation.
+    if (context->getExtensions().webglCompatibility)
+    {
+        compileOptions |= SH_LIMIT_CALL_STACK_DEPTH;
+        compileOptions |= SH_LIMIT_EXPRESSION_COMPLEXITY;
+        compileOptions |= SH_ENFORCE_PACKING_RESTRICTIONS;
+    }
+
     // Some targets (eg D3D11 Feature Level 9_3 and below) do not support non-constant loop indexes
     // in fragment shaders. Shader compilation will fail. To provide a better error message we can
     // instruct the compiler to pre-validate.