//
// Copyright (c) 2002-2013 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//

//
// Create strings that declare built-in definitions, add built-ins that
// cannot be expressed in the files, and establish mappings between 
// built-in functions and operators.
//

#include "compiler/Initialize.h"

#include "compiler/intermediate.h"

//============================================================================
//
// Prototypes for built-in functions seen by both vertex and fragment shaders.
//
//============================================================================
static TString BuiltInFunctionsCommon()
{
    TString s;

    //
    // Angle and Trigonometric Functions.
    //
    s.append(TString("float radians(float degrees);"));
    s.append(TString("vec2  radians(vec2  degrees);"));
    s.append(TString("vec3  radians(vec3  degrees);"));
    s.append(TString("vec4  radians(vec4  degrees);"));

    s.append(TString("float degrees(float radians);"));
    s.append(TString("vec2  degrees(vec2  radians);"));
    s.append(TString("vec3  degrees(vec3  radians);"));
    s.append(TString("vec4  degrees(vec4  radians);"));

    s.append(TString("float sin(float angle);"));
    s.append(TString("vec2  sin(vec2  angle);"));
    s.append(TString("vec3  sin(vec3  angle);"));
    s.append(TString("vec4  sin(vec4  angle);"));

    s.append(TString("float cos(float angle);"));
    s.append(TString("vec2  cos(vec2  angle);"));
    s.append(TString("vec3  cos(vec3  angle);"));
    s.append(TString("vec4  cos(vec4  angle);"));

    s.append(TString("float tan(float angle);"));
    s.append(TString("vec2  tan(vec2  angle);"));
    s.append(TString("vec3  tan(vec3  angle);"));
    s.append(TString("vec4  tan(vec4  angle);"));

    s.append(TString("float asin(float x);"));
    s.append(TString("vec2  asin(vec2  x);"));
    s.append(TString("vec3  asin(vec3  x);"));
    s.append(TString("vec4  asin(vec4  x);"));

    s.append(TString("float acos(float x);"));
    s.append(TString("vec2  acos(vec2  x);"));
    s.append(TString("vec3  acos(vec3  x);"));
    s.append(TString("vec4  acos(vec4  x);"));

    s.append(TString("float atan(float y, float x);"));
    s.append(TString("vec2  atan(vec2  y, vec2  x);"));
    s.append(TString("vec3  atan(vec3  y, vec3  x);"));
    s.append(TString("vec4  atan(vec4  y, vec4  x);"));

    s.append(TString("float atan(float y_over_x);"));
    s.append(TString("vec2  atan(vec2  y_over_x);"));
    s.append(TString("vec3  atan(vec3  y_over_x);"));
    s.append(TString("vec4  atan(vec4  y_over_x);"));

    //
    // Exponential Functions.
    //
    s.append(TString("float pow(float x, float y);"));
    s.append(TString("vec2  pow(vec2  x, vec2  y);"));
    s.append(TString("vec3  pow(vec3  x, vec3  y);"));
    s.append(TString("vec4  pow(vec4  x, vec4  y);"));

    s.append(TString("float exp(float x);"));
    s.append(TString("vec2  exp(vec2  x);"));
    s.append(TString("vec3  exp(vec3  x);"));
    s.append(TString("vec4  exp(vec4  x);"));

    s.append(TString("float log(float x);"));
    s.append(TString("vec2  log(vec2  x);"));
    s.append(TString("vec3  log(vec3  x);"));
    s.append(TString("vec4  log(vec4  x);"));

    s.append(TString("float exp2(float x);"));
    s.append(TString("vec2  exp2(vec2  x);"));
    s.append(TString("vec3  exp2(vec3  x);"));
    s.append(TString("vec4  exp2(vec4  x);"));

    s.append(TString("float log2(float x);"));
    s.append(TString("vec2  log2(vec2  x);"));
    s.append(TString("vec3  log2(vec3  x);"));
    s.append(TString("vec4  log2(vec4  x);"));

    s.append(TString("float sqrt(float x);"));
    s.append(TString("vec2  sqrt(vec2  x);"));
    s.append(TString("vec3  sqrt(vec3  x);"));
    s.append(TString("vec4  sqrt(vec4  x);"));

    s.append(TString("float inversesqrt(float x);"));
    s.append(TString("vec2  inversesqrt(vec2  x);"));
    s.append(TString("vec3  inversesqrt(vec3  x);"));
    s.append(TString("vec4  inversesqrt(vec4  x);"));

    //
    // Common Functions.
    //
    s.append(TString("float abs(float x);"));
    s.append(TString("vec2  abs(vec2  x);"));
    s.append(TString("vec3  abs(vec3  x);"));
    s.append(TString("vec4  abs(vec4  x);"));

    s.append(TString("float sign(float x);"));
    s.append(TString("vec2  sign(vec2  x);"));
    s.append(TString("vec3  sign(vec3  x);"));
    s.append(TString("vec4  sign(vec4  x);"));

    s.append(TString("float floor(float x);"));
    s.append(TString("vec2  floor(vec2  x);"));
    s.append(TString("vec3  floor(vec3  x);"));
    s.append(TString("vec4  floor(vec4  x);"));

    s.append(TString("float ceil(float x);"));
    s.append(TString("vec2  ceil(vec2  x);"));
    s.append(TString("vec3  ceil(vec3  x);"));
    s.append(TString("vec4  ceil(vec4  x);"));

    s.append(TString("float fract(float x);"));
    s.append(TString("vec2  fract(vec2  x);"));
    s.append(TString("vec3  fract(vec3  x);"));
    s.append(TString("vec4  fract(vec4  x);"));

    s.append(TString("float mod(float x, float y);"));
    s.append(TString("vec2  mod(vec2  x, float y);"));
    s.append(TString("vec3  mod(vec3  x, float y);"));
    s.append(TString("vec4  mod(vec4  x, float y);"));
    s.append(TString("vec2  mod(vec2  x, vec2  y);"));
    s.append(TString("vec3  mod(vec3  x, vec3  y);"));
    s.append(TString("vec4  mod(vec4  x, vec4  y);"));

    s.append(TString("float min(float x, float y);"));
    s.append(TString("vec2  min(vec2  x, float y);"));
    s.append(TString("vec3  min(vec3  x, float y);"));
    s.append(TString("vec4  min(vec4  x, float y);"));
    s.append(TString("vec2  min(vec2  x, vec2  y);"));
    s.append(TString("vec3  min(vec3  x, vec3  y);"));
    s.append(TString("vec4  min(vec4  x, vec4  y);"));

    s.append(TString("float max(float x, float y);"));
    s.append(TString("vec2  max(vec2  x, float y);"));
    s.append(TString("vec3  max(vec3  x, float y);"));
    s.append(TString("vec4  max(vec4  x, float y);"));
    s.append(TString("vec2  max(vec2  x, vec2  y);"));
    s.append(TString("vec3  max(vec3  x, vec3  y);"));
    s.append(TString("vec4  max(vec4  x, vec4  y);"));

    s.append(TString("float clamp(float x, float minVal, float maxVal);"));
    s.append(TString("vec2  clamp(vec2  x, float minVal, float maxVal);"));
    s.append(TString("vec3  clamp(vec3  x, float minVal, float maxVal);"));
    s.append(TString("vec4  clamp(vec4  x, float minVal, float maxVal);"));
    s.append(TString("vec2  clamp(vec2  x, vec2  minVal, vec2  maxVal);"));
    s.append(TString("vec3  clamp(vec3  x, vec3  minVal, vec3  maxVal);"));
    s.append(TString("vec4  clamp(vec4  x, vec4  minVal, vec4  maxVal);"));

    s.append(TString("float mix(float x, float y, float a);"));
    s.append(TString("vec2  mix(vec2  x, vec2  y, float a);"));
    s.append(TString("vec3  mix(vec3  x, vec3  y, float a);"));
    s.append(TString("vec4  mix(vec4  x, vec4  y, float a);"));
    s.append(TString("vec2  mix(vec2  x, vec2  y, vec2  a);"));
    s.append(TString("vec3  mix(vec3  x, vec3  y, vec3  a);"));
    s.append(TString("vec4  mix(vec4  x, vec4  y, vec4  a);"));

    s.append(TString("float step(float edge, float x);"));
    s.append(TString("vec2  step(vec2  edge, vec2  x);"));
    s.append(TString("vec3  step(vec3  edge, vec3  x);"));
    s.append(TString("vec4  step(vec4  edge, vec4  x);"));
    s.append(TString("vec2  step(float edge, vec2  x);"));
    s.append(TString("vec3  step(float edge, vec3  x);"));
    s.append(TString("vec4  step(float edge, vec4  x);"));

    s.append(TString("float smoothstep(float edge0, float edge1, float x);"));
    s.append(TString("vec2  smoothstep(vec2  edge0, vec2  edge1, vec2  x);"));
    s.append(TString("vec3  smoothstep(vec3  edge0, vec3  edge1, vec3  x);"));
    s.append(TString("vec4  smoothstep(vec4  edge0, vec4  edge1, vec4  x);"));
    s.append(TString("vec2  smoothstep(float edge0, float edge1, vec2  x);"));
    s.append(TString("vec3  smoothstep(float edge0, float edge1, vec3  x);"));
    s.append(TString("vec4  smoothstep(float edge0, float edge1, vec4  x);"));

    //
    // Geometric Functions.
    //
    s.append(TString("float length(float x);"));
    s.append(TString("float length(vec2  x);"));
    s.append(TString("float length(vec3  x);"));
    s.append(TString("float length(vec4  x);"));

    s.append(TString("float distance(float p0, float p1);"));
    s.append(TString("float distance(vec2  p0, vec2  p1);"));
    s.append(TString("float distance(vec3  p0, vec3  p1);"));
    s.append(TString("float distance(vec4  p0, vec4  p1);"));

    s.append(TString("float dot(float x, float y);"));
    s.append(TString("float dot(vec2  x, vec2  y);"));
    s.append(TString("float dot(vec3  x, vec3  y);"));
    s.append(TString("float dot(vec4  x, vec4  y);"));

    s.append(TString("vec3 cross(vec3 x, vec3 y);"));
    s.append(TString("float normalize(float x);"));
    s.append(TString("vec2  normalize(vec2  x);"));
    s.append(TString("vec3  normalize(vec3  x);"));
    s.append(TString("vec4  normalize(vec4  x);"));

    s.append(TString("float faceforward(float N, float I, float Nref);"));
    s.append(TString("vec2  faceforward(vec2  N, vec2  I, vec2  Nref);"));
    s.append(TString("vec3  faceforward(vec3  N, vec3  I, vec3  Nref);"));
    s.append(TString("vec4  faceforward(vec4  N, vec4  I, vec4  Nref);"));

    s.append(TString("float reflect(float I, float N);"));
    s.append(TString("vec2  reflect(vec2  I, vec2  N);"));
    s.append(TString("vec3  reflect(vec3  I, vec3  N);"));
    s.append(TString("vec4  reflect(vec4  I, vec4  N);"));

    s.append(TString("float refract(float I, float N, float eta);"));
    s.append(TString("vec2  refract(vec2  I, vec2  N, float eta);"));
    s.append(TString("vec3  refract(vec3  I, vec3  N, float eta);"));
    s.append(TString("vec4  refract(vec4  I, vec4  N, float eta);"));

    //
    // Matrix Functions.
    //
    s.append(TString("mat2 matrixCompMult(mat2 x, mat2 y);"));
    s.append(TString("mat3 matrixCompMult(mat3 x, mat3 y);"));
    s.append(TString("mat4 matrixCompMult(mat4 x, mat4 y);"));

    //
    // Vector relational functions.
    //
    s.append(TString("bvec2 lessThan(vec2 x, vec2 y);"));
    s.append(TString("bvec3 lessThan(vec3 x, vec3 y);"));
    s.append(TString("bvec4 lessThan(vec4 x, vec4 y);"));

    s.append(TString("bvec2 lessThan(ivec2 x, ivec2 y);"));
    s.append(TString("bvec3 lessThan(ivec3 x, ivec3 y);"));
    s.append(TString("bvec4 lessThan(ivec4 x, ivec4 y);"));

    s.append(TString("bvec2 lessThanEqual(vec2 x, vec2 y);"));
    s.append(TString("bvec3 lessThanEqual(vec3 x, vec3 y);"));
    s.append(TString("bvec4 lessThanEqual(vec4 x, vec4 y);"));

    s.append(TString("bvec2 lessThanEqual(ivec2 x, ivec2 y);"));
    s.append(TString("bvec3 lessThanEqual(ivec3 x, ivec3 y);"));
    s.append(TString("bvec4 lessThanEqual(ivec4 x, ivec4 y);"));

    s.append(TString("bvec2 greaterThan(vec2 x, vec2 y);"));
    s.append(TString("bvec3 greaterThan(vec3 x, vec3 y);"));
    s.append(TString("bvec4 greaterThan(vec4 x, vec4 y);"));

    s.append(TString("bvec2 greaterThan(ivec2 x, ivec2 y);"));
    s.append(TString("bvec3 greaterThan(ivec3 x, ivec3 y);"));
    s.append(TString("bvec4 greaterThan(ivec4 x, ivec4 y);"));

    s.append(TString("bvec2 greaterThanEqual(vec2 x, vec2 y);"));
    s.append(TString("bvec3 greaterThanEqual(vec3 x, vec3 y);"));
    s.append(TString("bvec4 greaterThanEqual(vec4 x, vec4 y);"));

    s.append(TString("bvec2 greaterThanEqual(ivec2 x, ivec2 y);"));
    s.append(TString("bvec3 greaterThanEqual(ivec3 x, ivec3 y);"));
    s.append(TString("bvec4 greaterThanEqual(ivec4 x, ivec4 y);"));

    s.append(TString("bvec2 equal(vec2 x, vec2 y);"));
    s.append(TString("bvec3 equal(vec3 x, vec3 y);"));
    s.append(TString("bvec4 equal(vec4 x, vec4 y);"));

    s.append(TString("bvec2 equal(ivec2 x, ivec2 y);"));
    s.append(TString("bvec3 equal(ivec3 x, ivec3 y);"));
    s.append(TString("bvec4 equal(ivec4 x, ivec4 y);"));

    s.append(TString("bvec2 equal(bvec2 x, bvec2 y);"));
    s.append(TString("bvec3 equal(bvec3 x, bvec3 y);"));
    s.append(TString("bvec4 equal(bvec4 x, bvec4 y);"));

    s.append(TString("bvec2 notEqual(vec2 x, vec2 y);"));
    s.append(TString("bvec3 notEqual(vec3 x, vec3 y);"));
    s.append(TString("bvec4 notEqual(vec4 x, vec4 y);"));

    s.append(TString("bvec2 notEqual(ivec2 x, ivec2 y);"));
    s.append(TString("bvec3 notEqual(ivec3 x, ivec3 y);"));
    s.append(TString("bvec4 notEqual(ivec4 x, ivec4 y);"));

    s.append(TString("bvec2 notEqual(bvec2 x, bvec2 y);"));
    s.append(TString("bvec3 notEqual(bvec3 x, bvec3 y);"));
    s.append(TString("bvec4 notEqual(bvec4 x, bvec4 y);"));

    s.append(TString("bool any(bvec2 x);"));
    s.append(TString("bool any(bvec3 x);"));
    s.append(TString("bool any(bvec4 x);"));

    s.append(TString("bool all(bvec2 x);"));
    s.append(TString("bool all(bvec3 x);"));
    s.append(TString("bool all(bvec4 x);"));

    s.append(TString("bvec2 not(bvec2 x);"));
    s.append(TString("bvec3 not(bvec3 x);"));
    s.append(TString("bvec4 not(bvec4 x);"));

    //
    // Noise functions.
    //
    //s.append(TString("float noise1(float x);"));
    //s.append(TString("float noise1(vec2  x);"));
    //s.append(TString("float noise1(vec3  x);"));
    //s.append(TString("float noise1(vec4  x);"));

    //s.append(TString("vec2 noise2(float x);"));
    //s.append(TString("vec2 noise2(vec2  x);"));
    //s.append(TString("vec2 noise2(vec3  x);"));
    //s.append(TString("vec2 noise2(vec4  x);"));

    //s.append(TString("vec3 noise3(float x);"));
    //s.append(TString("vec3 noise3(vec2  x);"));
    //s.append(TString("vec3 noise3(vec3  x);"));
    //s.append(TString("vec3 noise3(vec4  x);"));

    //s.append(TString("vec4 noise4(float x);"));
    //s.append(TString("vec4 noise4(vec2  x);"));
    //s.append(TString("vec4 noise4(vec3  x);"));
    //s.append(TString("vec4 noise4(vec4  x);"));

    return s;
}

static TString BuiltInFunctionsCommonTexture1_0(const ShBuiltInResources& resources)
{
    TString s;

    //
    // Texture Functions for GLSL ES 1.0
    //
    s.append(TString("vec4 texture2D(sampler2D sampler, vec2 coord);"));
    s.append(TString("vec4 texture2DProj(sampler2D sampler, vec3 coord);"));
    s.append(TString("vec4 texture2DProj(sampler2D sampler, vec4 coord);"));
    s.append(TString("vec4 textureCube(samplerCube sampler, vec3 coord);"));

    if (resources.OES_EGL_image_external) {
        s.append(TString("vec4 texture2D(samplerExternalOES sampler, vec2 coord);"));
        s.append(TString("vec4 texture2DProj(samplerExternalOES sampler, vec3 coord);"));
        s.append(TString("vec4 texture2DProj(samplerExternalOES sampler, vec4 coord);"));
    }

    if (resources.ARB_texture_rectangle) {
        s.append(TString("vec4 texture2DRect(sampler2DRect sampler, vec2 coord);"));
        s.append(TString("vec4 texture2DRectProj(sampler2DRect sampler, vec3 coord);"));
        s.append(TString("vec4 texture2DRectProj(sampler2DRect sampler, vec4 coord);"));
    }

    return s;
}

static TString BuiltInFunctionsCommonTexture3_0()
{
    TString s;

    //
    // Texture Functions for GLSL ES 3.0
    //
    s.append(TString("vec4 texture(sampler2D sampler, vec2 coord);"));
    s.append(TString("vec4 texture(samplerCube sampler, vec3 coord);"));
    s.append(TString("vec4 textureProj(sampler2D sampler, vec3 coord);"));
    s.append(TString("vec4 textureProj(sampler2D sampler, vec4 coord);"));

    return s;
}

static TString BuiltInFunctionsFragmentGradient3_0()
{
    TString s;

    s.append(TString("float dFdx(float p);"));
    s.append(TString("vec2  dFdx(vec2  p);"));
    s.append(TString("vec3  dFdx(vec3  p);"));
    s.append(TString("vec4  dFdx(vec4  p);"));

    s.append(TString("float dFdy(float p);"));
    s.append(TString("vec2  dFdy(vec2  p);"));
    s.append(TString("vec3  dFdy(vec3  p);"));
    s.append(TString("vec4  dFdy(vec4  p);"));

    s.append(TString("float fwidth(float p);"));
    s.append(TString("vec2  fwidth(vec2  p);"));
    s.append(TString("vec3  fwidth(vec3  p);"));
    s.append(TString("vec4  fwidth(vec4  p);"));

    return s;
}

//============================================================================
//
// Prototypes for built-in functions seen by vertex shaders only.
//
//============================================================================
static TString BuiltInFunctionsVertexTexture1_0()
{
    TString s;

    //
    // Geometric Functions.
    //
    //s.append(TString("vec4 ftransform();"));

    //
    // Texture Functions.
    //
    s.append(TString("vec4 texture2DLod(sampler2D sampler, vec2 coord, float lod);"));
    s.append(TString("vec4 texture2DProjLod(sampler2D sampler, vec3 coord, float lod);"));
    s.append(TString("vec4 texture2DProjLod(sampler2D sampler, vec4 coord, float lod);"));
    s.append(TString("vec4 textureCubeLod(samplerCube sampler, vec3 coord, float lod);"));

    return s;
}

//============================================================================
//
// Prototypes for built-in functions seen by fragment shaders only.
//
//============================================================================
static TString BuiltInFunctionsFragmentTexture1_0(const ShBuiltInResources& resources)
{
    TString s;

    //
    // Texture Functions.
    //
    s.append(TString("vec4 texture2D(sampler2D sampler, vec2 coord, float bias);"));
    s.append(TString("vec4 texture2DProj(sampler2D sampler, vec3 coord, float bias);"));
    s.append(TString("vec4 texture2DProj(sampler2D sampler, vec4 coord, float bias);"));
    s.append(TString("vec4 textureCube(samplerCube sampler, vec3 coord, float bias);"));

    if (resources.OES_standard_derivatives) {
        s.append(TString("float dFdx(float p);"));
        s.append(TString("vec2  dFdx(vec2  p);"));
        s.append(TString("vec3  dFdx(vec3  p);"));
        s.append(TString("vec4  dFdx(vec4  p);"));

        s.append(TString("float dFdy(float p);"));
        s.append(TString("vec2  dFdy(vec2  p);"));
        s.append(TString("vec3  dFdy(vec3  p);"));
        s.append(TString("vec4  dFdy(vec4  p);"));

        s.append(TString("float fwidth(float p);"));
        s.append(TString("vec2  fwidth(vec2  p);"));
        s.append(TString("vec3  fwidth(vec3  p);"));
        s.append(TString("vec4  fwidth(vec4  p);"));
    }

    return s;
}

//============================================================================
//
// Standard uniforms.
//
//============================================================================
static TString StandardUniforms()
{
    TString s;

    //
    // Depth range in window coordinates
    //
    s.append(TString("struct gl_DepthRangeParameters {"));
    s.append(TString("    highp float near;"));        // n
    s.append(TString("    highp float far;"));         // f
    s.append(TString("    highp float diff;"));        // f - n
    s.append(TString("};"));
    s.append(TString("uniform gl_DepthRangeParameters gl_DepthRange;"));

    return s;
}

//============================================================================
//
// Default precision for vertex shaders.
//
//============================================================================
static TString DefaultPrecisionVertex()
{
    TString s;

    s.append(TString("precision highp int;"));
    s.append(TString("precision highp float;"));

    return s;
}

//============================================================================
//
// Default precision for fragment shaders.
//
//============================================================================
static TString DefaultPrecisionFragment()
{
    TString s;

    s.append(TString("precision mediump int;"));
    // No default precision for float in fragment shaders

    return s;
}

//============================================================================
//
// Implementation dependent built-in constants.
//
//============================================================================
static TString BuiltInConstants(const ShBuiltInResources &resources)
{
    TStringStream s;

    s << "const mediump int gl_MaxVertexAttribs = " << resources.MaxVertexAttribs << ";";
    s << "const mediump int gl_MaxVertexUniformVectors = " << resources.MaxVertexUniformVectors << ";";

    s << "const mediump int gl_MaxVertexTextureImageUnits = " << resources.MaxVertexTextureImageUnits << ";";
    s << "const mediump int gl_MaxCombinedTextureImageUnits = " << resources.MaxCombinedTextureImageUnits << ";";
    s << "const mediump int gl_MaxTextureImageUnits = " << resources.MaxTextureImageUnits << ";";
    s << "const mediump int gl_MaxFragmentUniformVectors = " << resources.MaxFragmentUniformVectors << ";";
    
    return s.str();
}

static TString BuiltInConstants1_0(ShShaderSpec spec, const ShBuiltInResources &resources, const TExtensionBehavior& extensionBehavior)
{
    TStringStream s;

    s << "const mediump int gl_MaxVaryingVectors = " << resources.MaxVaryingVectors << ";";

    if (spec != SH_CSS_SHADERS_SPEC)
    {
        TExtensionBehavior::const_iterator iter = extensionBehavior.find("GL_EXT_draw_buffers");
        const bool usingMRTExtension = (iter != extensionBehavior.end() && (iter->second == EBhEnable || iter->second == EBhRequire));
        const int maxDrawBuffers = (usingMRTExtension ? resources.MaxDrawBuffers : 1);

        s << "const mediump int gl_MaxDrawBuffers = " << maxDrawBuffers << ";";
    }

    return s.str();
}

static TString BuiltInConstants3_0(const ShBuiltInResources &resources)
{
    TStringStream s;

    s << "const mediump int gl_MaxVertexOutputVectors = " << resources.MaxVertexOutputVectors << ";";
    s << "const mediump int gl_MaxFragmentInputVectors = " << resources.MaxFragmentInputVectors << ";";
    s << "const mediump int gl_MinProgramTexelOffset = " << resources.MinProgramTexelOffset << ";";
    s << "const mediump int gl_MaxProgramTexelOffset = " << resources.MaxProgramTexelOffset << ";";

    return s.str();
}

void TBuiltIns::initialize(ShShaderType type, ShShaderSpec spec,
                           const ShBuiltInResources& resources, 
                           const TExtensionBehavior& extensionBehavior)
{
    switch (type) {
    case SH_FRAGMENT_SHADER:
        commonBuiltIns.push_back(DefaultPrecisionFragment());
        commonBuiltIns.push_back(BuiltInFunctionsCommon());
        essl1BuiltIns.push_back(BuiltInFunctionsCommonTexture1_0(resources));
        essl1BuiltIns.push_back(BuiltInFunctionsFragmentTexture1_0(resources));
        essl3BuiltIns.push_back(BuiltInFunctionsCommonTexture3_0());
        essl3BuiltIns.push_back(BuiltInFunctionsFragmentGradient3_0());
        commonBuiltIns.push_back(StandardUniforms());
        break;

    case SH_VERTEX_SHADER:
        commonBuiltIns.push_back(DefaultPrecisionVertex());
        commonBuiltIns.push_back(BuiltInFunctionsCommon());
        essl1BuiltIns.push_back(BuiltInFunctionsCommonTexture1_0(resources));
        essl1BuiltIns.push_back(BuiltInFunctionsVertexTexture1_0());
        essl3BuiltIns.push_back(BuiltInFunctionsCommonTexture3_0());
        commonBuiltIns.push_back(StandardUniforms());
        break;

    default: assert(false && "Language not supported");
    }

    commonBuiltIns.push_back(BuiltInConstants(resources));
    essl1BuiltIns.push_back(BuiltInConstants1_0(spec, resources, extensionBehavior));
    essl3BuiltIns.push_back(BuiltInConstants3_0(resources));
}

void IdentifyBuiltIns(ShShaderType type, ShShaderSpec spec,
                      const ShBuiltInResources& resources,
                      TSymbolTable& symbolTable)
{
    //
    // First, insert some special built-in variables that are not in 
    // the built-in header files.
    //
    switch(type) {
    case SH_FRAGMENT_SHADER:
        symbolTable.insert(COMMON_BUILTINS, *new TVariable(NewPoolTString("gl_FragCoord"), TType(EbtFloat, EbpMedium, EvqFragCoord,   4)));
        symbolTable.insert(COMMON_BUILTINS, *new TVariable(NewPoolTString("gl_FrontFacing"), TType(EbtBool,  EbpUndefined, EvqFrontFacing, 1)));
        symbolTable.insert(COMMON_BUILTINS, *new TVariable(NewPoolTString("gl_PointCoord"), TType(EbtFloat, EbpMedium, EvqPointCoord,  2)));

        //
        // In CSS Shaders, gl_FragColor, gl_FragData, and gl_MaxDrawBuffers are not available.
        // Instead, css_MixColor and css_ColorMatrix are available.
        //
        if (spec != SH_CSS_SHADERS_SPEC) {
            symbolTable.insert(ESSL1_BUILTINS, *new TVariable(NewPoolTString("gl_FragColor"), TType(EbtFloat, EbpMedium, EvqFragColor,   4)));
            symbolTable.insert(ESSL1_BUILTINS, *new TVariable(NewPoolTString("gl_FragData[gl_MaxDrawBuffers]"), TType(EbtFloat, EbpMedium, EvqFragData,    4)));
        } else {
            symbolTable.insert(ESSL1_BUILTINS, *new TVariable(NewPoolTString("css_MixColor"), TType(EbtFloat, EbpMedium, EvqGlobal,      4)));
            symbolTable.insert(ESSL1_BUILTINS, *new TVariable(NewPoolTString("css_ColorMatrix"), TType(EbtFloat, EbpMedium, EvqGlobal,      4, 4)));
        }

        break;

    case SH_VERTEX_SHADER:
        symbolTable.insert(COMMON_BUILTINS, *new TVariable(NewPoolTString("gl_Position"), TType(EbtFloat, EbpHigh, EvqPosition,    4)));
        symbolTable.insert(COMMON_BUILTINS, *new TVariable(NewPoolTString("gl_PointSize"), TType(EbtFloat, EbpMedium, EvqPointSize,   1)));
        break;

    default: assert(false && "Language not supported");
    }

    //
    // Next, identify which built-ins from the already loaded headers have
    // a mapping to an operator.  Those that are not identified as such are
    // expected to be resolved through a library of functions, versus as
    // operations.
    //
    symbolTable.relateToOperator(COMMON_BUILTINS, "matrixCompMult",   EOpMul);

    symbolTable.relateToOperator(COMMON_BUILTINS, "equal",            EOpVectorEqual);
    symbolTable.relateToOperator(COMMON_BUILTINS, "notEqual",         EOpVectorNotEqual);
    symbolTable.relateToOperator(COMMON_BUILTINS, "lessThan",         EOpLessThan);
    symbolTable.relateToOperator(COMMON_BUILTINS, "greaterThan",      EOpGreaterThan);
    symbolTable.relateToOperator(COMMON_BUILTINS, "lessThanEqual",    EOpLessThanEqual);
    symbolTable.relateToOperator(COMMON_BUILTINS, "greaterThanEqual", EOpGreaterThanEqual);
    
    symbolTable.relateToOperator(COMMON_BUILTINS, "radians",      EOpRadians);
    symbolTable.relateToOperator(COMMON_BUILTINS, "degrees",      EOpDegrees);
    symbolTable.relateToOperator(COMMON_BUILTINS, "sin",          EOpSin);
    symbolTable.relateToOperator(COMMON_BUILTINS, "cos",          EOpCos);
    symbolTable.relateToOperator(COMMON_BUILTINS, "tan",          EOpTan);
    symbolTable.relateToOperator(COMMON_BUILTINS, "asin",         EOpAsin);
    symbolTable.relateToOperator(COMMON_BUILTINS, "acos",         EOpAcos);
    symbolTable.relateToOperator(COMMON_BUILTINS, "atan",         EOpAtan);

    symbolTable.relateToOperator(COMMON_BUILTINS, "pow",          EOpPow);
    symbolTable.relateToOperator(COMMON_BUILTINS, "exp2",         EOpExp2);
    symbolTable.relateToOperator(COMMON_BUILTINS, "log",          EOpLog);
    symbolTable.relateToOperator(COMMON_BUILTINS, "exp",          EOpExp);
    symbolTable.relateToOperator(COMMON_BUILTINS, "log2",         EOpLog2);
    symbolTable.relateToOperator(COMMON_BUILTINS, "sqrt",         EOpSqrt);
    symbolTable.relateToOperator(COMMON_BUILTINS, "inversesqrt",  EOpInverseSqrt);

    symbolTable.relateToOperator(COMMON_BUILTINS, "abs",          EOpAbs);
    symbolTable.relateToOperator(COMMON_BUILTINS, "sign",         EOpSign);
    symbolTable.relateToOperator(COMMON_BUILTINS, "floor",        EOpFloor);
    symbolTable.relateToOperator(COMMON_BUILTINS, "ceil",         EOpCeil);
    symbolTable.relateToOperator(COMMON_BUILTINS, "fract",        EOpFract);
    symbolTable.relateToOperator(COMMON_BUILTINS, "mod",          EOpMod);
    symbolTable.relateToOperator(COMMON_BUILTINS, "min",          EOpMin);
    symbolTable.relateToOperator(COMMON_BUILTINS, "max",          EOpMax);
    symbolTable.relateToOperator(COMMON_BUILTINS, "clamp",        EOpClamp);
    symbolTable.relateToOperator(COMMON_BUILTINS, "mix",          EOpMix);
    symbolTable.relateToOperator(COMMON_BUILTINS, "step",         EOpStep);
    symbolTable.relateToOperator(COMMON_BUILTINS, "smoothstep",   EOpSmoothStep);

    symbolTable.relateToOperator(COMMON_BUILTINS, "length",       EOpLength);
    symbolTable.relateToOperator(COMMON_BUILTINS, "distance",     EOpDistance);
    symbolTable.relateToOperator(COMMON_BUILTINS, "dot",          EOpDot);
    symbolTable.relateToOperator(COMMON_BUILTINS, "cross",        EOpCross);
    symbolTable.relateToOperator(COMMON_BUILTINS, "normalize",    EOpNormalize);
    symbolTable.relateToOperator(COMMON_BUILTINS, "faceforward",  EOpFaceForward);
    symbolTable.relateToOperator(COMMON_BUILTINS, "reflect",      EOpReflect);
    symbolTable.relateToOperator(COMMON_BUILTINS, "refract",      EOpRefract);
    
    symbolTable.relateToOperator(COMMON_BUILTINS, "any",          EOpAny);
    symbolTable.relateToOperator(COMMON_BUILTINS, "all",          EOpAll);
    symbolTable.relateToOperator(COMMON_BUILTINS, "not",          EOpVectorLogicalNot);

    // Map language-specific operators.
    switch(type) {
    case SH_VERTEX_SHADER:
        break;
    case SH_FRAGMENT_SHADER:
        if (resources.OES_standard_derivatives) {
            symbolTable.relateToOperator(ESSL1_BUILTINS, "dFdx",   EOpDFdx);
            symbolTable.relateToOperator(ESSL1_BUILTINS, "dFdy",   EOpDFdy);
            symbolTable.relateToOperator(ESSL1_BUILTINS, "fwidth", EOpFwidth);

            symbolTable.relateToExtension(ESSL1_BUILTINS, "dFdx", "GL_OES_standard_derivatives");
            symbolTable.relateToExtension(ESSL1_BUILTINS, "dFdy", "GL_OES_standard_derivatives");
            symbolTable.relateToExtension(ESSL1_BUILTINS, "fwidth", "GL_OES_standard_derivatives");

            symbolTable.relateToOperator(ESSL3_BUILTINS, "dFdx",   EOpDFdx);
            symbolTable.relateToOperator(ESSL3_BUILTINS, "dFdy",   EOpDFdy);
            symbolTable.relateToOperator(ESSL3_BUILTINS, "fwidth", EOpFwidth);
        }
        break;
    default: break;
    }

    // Finally add resource-specific variables.
    switch(type) {
    case SH_FRAGMENT_SHADER:
        if (spec != SH_CSS_SHADERS_SPEC) {
            // Set up gl_FragData.  The array size.
            TType fragData(EbtFloat, EbpMedium, EvqFragData, 4, 1, true);
            fragData.setArraySize(resources.MaxDrawBuffers);
            symbolTable.insert(ESSL1_BUILTINS, *new TVariable(NewPoolTString("gl_FragData"), fragData));
        }
        break;
    default: break;
    }
}

void InitExtensionBehavior(const ShBuiltInResources& resources,
                           TExtensionBehavior& extBehavior)
{
    if (resources.OES_standard_derivatives)
        extBehavior["GL_OES_standard_derivatives"] = EBhUndefined;
    if (resources.OES_EGL_image_external)
        extBehavior["GL_OES_EGL_image_external"] = EBhUndefined;
    if (resources.ARB_texture_rectangle)
        extBehavior["GL_ARB_texture_rectangle"] = EBhUndefined;
    if (resources.EXT_draw_buffers)
        extBehavior["GL_EXT_draw_buffers"] = EBhUndefined;
}
