Home | History | Annotate | Download | only in compiler
      1 //
      2 // Copyright (c) 2002-2010 The ANGLE Project Authors. All rights reserved.
      3 // Use of this source code is governed by a BSD-style license that can be
      4 // found in the LICENSE file.
      5 //
      6 
      7 //
      8 // Create strings that declare built-in definitions, add built-ins that
      9 // cannot be expressed in the files, and establish mappings between
     10 // built-in functions and operators.
     11 //
     12 
     13 #include "compiler/Initialize.h"
     14 
     15 #include "compiler/intermediate.h"
     16 
     17 //============================================================================
     18 //
     19 // Prototypes for built-in functions seen by both vertex and fragment shaders.
     20 //
     21 //============================================================================
     22 static TString BuiltInFunctionsCommon()
     23 {
     24     TString s;
     25 
     26     //
     27     // Angle and Trigonometric Functions.
     28     //
     29     s.append(TString("float radians(float degrees);"));
     30     s.append(TString("vec2  radians(vec2  degrees);"));
     31     s.append(TString("vec3  radians(vec3  degrees);"));
     32     s.append(TString("vec4  radians(vec4  degrees);"));
     33 
     34     s.append(TString("float degrees(float radians);"));
     35     s.append(TString("vec2  degrees(vec2  radians);"));
     36     s.append(TString("vec3  degrees(vec3  radians);"));
     37     s.append(TString("vec4  degrees(vec4  radians);"));
     38 
     39     s.append(TString("float sin(float angle);"));
     40     s.append(TString("vec2  sin(vec2  angle);"));
     41     s.append(TString("vec3  sin(vec3  angle);"));
     42     s.append(TString("vec4  sin(vec4  angle);"));
     43 
     44     s.append(TString("float cos(float angle);"));
     45     s.append(TString("vec2  cos(vec2  angle);"));
     46     s.append(TString("vec3  cos(vec3  angle);"));
     47     s.append(TString("vec4  cos(vec4  angle);"));
     48 
     49     s.append(TString("float tan(float angle);"));
     50     s.append(TString("vec2  tan(vec2  angle);"));
     51     s.append(TString("vec3  tan(vec3  angle);"));
     52     s.append(TString("vec4  tan(vec4  angle);"));
     53 
     54     s.append(TString("float asin(float x);"));
     55     s.append(TString("vec2  asin(vec2  x);"));
     56     s.append(TString("vec3  asin(vec3  x);"));
     57     s.append(TString("vec4  asin(vec4  x);"));
     58 
     59     s.append(TString("float acos(float x);"));
     60     s.append(TString("vec2  acos(vec2  x);"));
     61     s.append(TString("vec3  acos(vec3  x);"));
     62     s.append(TString("vec4  acos(vec4  x);"));
     63 
     64     s.append(TString("float atan(float y, float x);"));
     65     s.append(TString("vec2  atan(vec2  y, vec2  x);"));
     66     s.append(TString("vec3  atan(vec3  y, vec3  x);"));
     67     s.append(TString("vec4  atan(vec4  y, vec4  x);"));
     68 
     69     s.append(TString("float atan(float y_over_x);"));
     70     s.append(TString("vec2  atan(vec2  y_over_x);"));
     71     s.append(TString("vec3  atan(vec3  y_over_x);"));
     72     s.append(TString("vec4  atan(vec4  y_over_x);"));
     73 
     74     //
     75     // Exponential Functions.
     76     //
     77     s.append(TString("float pow(float x, float y);"));
     78     s.append(TString("vec2  pow(vec2  x, vec2  y);"));
     79     s.append(TString("vec3  pow(vec3  x, vec3  y);"));
     80     s.append(TString("vec4  pow(vec4  x, vec4  y);"));
     81 
     82     s.append(TString("float exp(float x);"));
     83     s.append(TString("vec2  exp(vec2  x);"));
     84     s.append(TString("vec3  exp(vec3  x);"));
     85     s.append(TString("vec4  exp(vec4  x);"));
     86 
     87     s.append(TString("float log(float x);"));
     88     s.append(TString("vec2  log(vec2  x);"));
     89     s.append(TString("vec3  log(vec3  x);"));
     90     s.append(TString("vec4  log(vec4  x);"));
     91 
     92     s.append(TString("float exp2(float x);"));
     93     s.append(TString("vec2  exp2(vec2  x);"));
     94     s.append(TString("vec3  exp2(vec3  x);"));
     95     s.append(TString("vec4  exp2(vec4  x);"));
     96 
     97     s.append(TString("float log2(float x);"));
     98     s.append(TString("vec2  log2(vec2  x);"));
     99     s.append(TString("vec3  log2(vec3  x);"));
    100     s.append(TString("vec4  log2(vec4  x);"));
    101 
    102     s.append(TString("float sqrt(float x);"));
    103     s.append(TString("vec2  sqrt(vec2  x);"));
    104     s.append(TString("vec3  sqrt(vec3  x);"));
    105     s.append(TString("vec4  sqrt(vec4  x);"));
    106 
    107     s.append(TString("float inversesqrt(float x);"));
    108     s.append(TString("vec2  inversesqrt(vec2  x);"));
    109     s.append(TString("vec3  inversesqrt(vec3  x);"));
    110     s.append(TString("vec4  inversesqrt(vec4  x);"));
    111 
    112     //
    113     // Common Functions.
    114     //
    115     s.append(TString("float abs(float x);"));
    116     s.append(TString("vec2  abs(vec2  x);"));
    117     s.append(TString("vec3  abs(vec3  x);"));
    118     s.append(TString("vec4  abs(vec4  x);"));
    119 
    120     s.append(TString("float sign(float x);"));
    121     s.append(TString("vec2  sign(vec2  x);"));
    122     s.append(TString("vec3  sign(vec3  x);"));
    123     s.append(TString("vec4  sign(vec4  x);"));
    124 
    125     s.append(TString("float floor(float x);"));
    126     s.append(TString("vec2  floor(vec2  x);"));
    127     s.append(TString("vec3  floor(vec3  x);"));
    128     s.append(TString("vec4  floor(vec4  x);"));
    129 
    130     s.append(TString("float ceil(float x);"));
    131     s.append(TString("vec2  ceil(vec2  x);"));
    132     s.append(TString("vec3  ceil(vec3  x);"));
    133     s.append(TString("vec4  ceil(vec4  x);"));
    134 
    135     s.append(TString("float fract(float x);"));
    136     s.append(TString("vec2  fract(vec2  x);"));
    137     s.append(TString("vec3  fract(vec3  x);"));
    138     s.append(TString("vec4  fract(vec4  x);"));
    139 
    140     s.append(TString("float mod(float x, float y);"));
    141     s.append(TString("vec2  mod(vec2  x, float y);"));
    142     s.append(TString("vec3  mod(vec3  x, float y);"));
    143     s.append(TString("vec4  mod(vec4  x, float y);"));
    144     s.append(TString("vec2  mod(vec2  x, vec2  y);"));
    145     s.append(TString("vec3  mod(vec3  x, vec3  y);"));
    146     s.append(TString("vec4  mod(vec4  x, vec4  y);"));
    147 
    148     s.append(TString("float min(float x, float y);"));
    149     s.append(TString("vec2  min(vec2  x, float y);"));
    150     s.append(TString("vec3  min(vec3  x, float y);"));
    151     s.append(TString("vec4  min(vec4  x, float y);"));
    152     s.append(TString("vec2  min(vec2  x, vec2  y);"));
    153     s.append(TString("vec3  min(vec3  x, vec3  y);"));
    154     s.append(TString("vec4  min(vec4  x, vec4  y);"));
    155 
    156     s.append(TString("float max(float x, float y);"));
    157     s.append(TString("vec2  max(vec2  x, float y);"));
    158     s.append(TString("vec3  max(vec3  x, float y);"));
    159     s.append(TString("vec4  max(vec4  x, float y);"));
    160     s.append(TString("vec2  max(vec2  x, vec2  y);"));
    161     s.append(TString("vec3  max(vec3  x, vec3  y);"));
    162     s.append(TString("vec4  max(vec4  x, vec4  y);"));
    163 
    164     s.append(TString("float clamp(float x, float minVal, float maxVal);"));
    165     s.append(TString("vec2  clamp(vec2  x, float minVal, float maxVal);"));
    166     s.append(TString("vec3  clamp(vec3  x, float minVal, float maxVal);"));
    167     s.append(TString("vec4  clamp(vec4  x, float minVal, float maxVal);"));
    168     s.append(TString("vec2  clamp(vec2  x, vec2  minVal, vec2  maxVal);"));
    169     s.append(TString("vec3  clamp(vec3  x, vec3  minVal, vec3  maxVal);"));
    170     s.append(TString("vec4  clamp(vec4  x, vec4  minVal, vec4  maxVal);"));
    171 
    172     s.append(TString("float mix(float x, float y, float a);"));
    173     s.append(TString("vec2  mix(vec2  x, vec2  y, float a);"));
    174     s.append(TString("vec3  mix(vec3  x, vec3  y, float a);"));
    175     s.append(TString("vec4  mix(vec4  x, vec4  y, float a);"));
    176     s.append(TString("vec2  mix(vec2  x, vec2  y, vec2  a);"));
    177     s.append(TString("vec3  mix(vec3  x, vec3  y, vec3  a);"));
    178     s.append(TString("vec4  mix(vec4  x, vec4  y, vec4  a);"));
    179 
    180     s.append(TString("float step(float edge, float x);"));
    181     s.append(TString("vec2  step(vec2  edge, vec2  x);"));
    182     s.append(TString("vec3  step(vec3  edge, vec3  x);"));
    183     s.append(TString("vec4  step(vec4  edge, vec4  x);"));
    184     s.append(TString("vec2  step(float edge, vec2  x);"));
    185     s.append(TString("vec3  step(float edge, vec3  x);"));
    186     s.append(TString("vec4  step(float edge, vec4  x);"));
    187 
    188     s.append(TString("float smoothstep(float edge0, float edge1, float x);"));
    189     s.append(TString("vec2  smoothstep(vec2  edge0, vec2  edge1, vec2  x);"));
    190     s.append(TString("vec3  smoothstep(vec3  edge0, vec3  edge1, vec3  x);"));
    191     s.append(TString("vec4  smoothstep(vec4  edge0, vec4  edge1, vec4  x);"));
    192     s.append(TString("vec2  smoothstep(float edge0, float edge1, vec2  x);"));
    193     s.append(TString("vec3  smoothstep(float edge0, float edge1, vec3  x);"));
    194     s.append(TString("vec4  smoothstep(float edge0, float edge1, vec4  x);"));
    195 
    196     //
    197     // Geometric Functions.
    198     //
    199     s.append(TString("float length(float x);"));
    200     s.append(TString("float length(vec2  x);"));
    201     s.append(TString("float length(vec3  x);"));
    202     s.append(TString("float length(vec4  x);"));
    203 
    204     s.append(TString("float distance(float p0, float p1);"));
    205     s.append(TString("float distance(vec2  p0, vec2  p1);"));
    206     s.append(TString("float distance(vec3  p0, vec3  p1);"));
    207     s.append(TString("float distance(vec4  p0, vec4  p1);"));
    208 
    209     s.append(TString("float dot(float x, float y);"));
    210     s.append(TString("float dot(vec2  x, vec2  y);"));
    211     s.append(TString("float dot(vec3  x, vec3  y);"));
    212     s.append(TString("float dot(vec4  x, vec4  y);"));
    213 
    214     s.append(TString("vec3 cross(vec3 x, vec3 y);"));
    215     s.append(TString("float normalize(float x);"));
    216     s.append(TString("vec2  normalize(vec2  x);"));
    217     s.append(TString("vec3  normalize(vec3  x);"));
    218     s.append(TString("vec4  normalize(vec4  x);"));
    219 
    220     s.append(TString("float faceforward(float N, float I, float Nref);"));
    221     s.append(TString("vec2  faceforward(vec2  N, vec2  I, vec2  Nref);"));
    222     s.append(TString("vec3  faceforward(vec3  N, vec3  I, vec3  Nref);"));
    223     s.append(TString("vec4  faceforward(vec4  N, vec4  I, vec4  Nref);"));
    224 
    225     s.append(TString("float reflect(float I, float N);"));
    226     s.append(TString("vec2  reflect(vec2  I, vec2  N);"));
    227     s.append(TString("vec3  reflect(vec3  I, vec3  N);"));
    228     s.append(TString("vec4  reflect(vec4  I, vec4  N);"));
    229 
    230     s.append(TString("float refract(float I, float N, float eta);"));
    231     s.append(TString("vec2  refract(vec2  I, vec2  N, float eta);"));
    232     s.append(TString("vec3  refract(vec3  I, vec3  N, float eta);"));
    233     s.append(TString("vec4  refract(vec4  I, vec4  N, float eta);"));
    234 
    235     //
    236     // Matrix Functions.
    237     //
    238     s.append(TString("mat2 matrixCompMult(mat2 x, mat2 y);"));
    239     s.append(TString("mat3 matrixCompMult(mat3 x, mat3 y);"));
    240     s.append(TString("mat4 matrixCompMult(mat4 x, mat4 y);"));
    241 
    242     //
    243     // Vector relational functions.
    244     //
    245     s.append(TString("bvec2 lessThan(vec2 x, vec2 y);"));
    246     s.append(TString("bvec3 lessThan(vec3 x, vec3 y);"));
    247     s.append(TString("bvec4 lessThan(vec4 x, vec4 y);"));
    248 
    249     s.append(TString("bvec2 lessThan(ivec2 x, ivec2 y);"));
    250     s.append(TString("bvec3 lessThan(ivec3 x, ivec3 y);"));
    251     s.append(TString("bvec4 lessThan(ivec4 x, ivec4 y);"));
    252 
    253     s.append(TString("bvec2 lessThanEqual(vec2 x, vec2 y);"));
    254     s.append(TString("bvec3 lessThanEqual(vec3 x, vec3 y);"));
    255     s.append(TString("bvec4 lessThanEqual(vec4 x, vec4 y);"));
    256 
    257     s.append(TString("bvec2 lessThanEqual(ivec2 x, ivec2 y);"));
    258     s.append(TString("bvec3 lessThanEqual(ivec3 x, ivec3 y);"));
    259     s.append(TString("bvec4 lessThanEqual(ivec4 x, ivec4 y);"));
    260 
    261     s.append(TString("bvec2 greaterThan(vec2 x, vec2 y);"));
    262     s.append(TString("bvec3 greaterThan(vec3 x, vec3 y);"));
    263     s.append(TString("bvec4 greaterThan(vec4 x, vec4 y);"));
    264 
    265     s.append(TString("bvec2 greaterThan(ivec2 x, ivec2 y);"));
    266     s.append(TString("bvec3 greaterThan(ivec3 x, ivec3 y);"));
    267     s.append(TString("bvec4 greaterThan(ivec4 x, ivec4 y);"));
    268 
    269     s.append(TString("bvec2 greaterThanEqual(vec2 x, vec2 y);"));
    270     s.append(TString("bvec3 greaterThanEqual(vec3 x, vec3 y);"));
    271     s.append(TString("bvec4 greaterThanEqual(vec4 x, vec4 y);"));
    272 
    273     s.append(TString("bvec2 greaterThanEqual(ivec2 x, ivec2 y);"));
    274     s.append(TString("bvec3 greaterThanEqual(ivec3 x, ivec3 y);"));
    275     s.append(TString("bvec4 greaterThanEqual(ivec4 x, ivec4 y);"));
    276 
    277     s.append(TString("bvec2 equal(vec2 x, vec2 y);"));
    278     s.append(TString("bvec3 equal(vec3 x, vec3 y);"));
    279     s.append(TString("bvec4 equal(vec4 x, vec4 y);"));
    280 
    281     s.append(TString("bvec2 equal(ivec2 x, ivec2 y);"));
    282     s.append(TString("bvec3 equal(ivec3 x, ivec3 y);"));
    283     s.append(TString("bvec4 equal(ivec4 x, ivec4 y);"));
    284 
    285     s.append(TString("bvec2 equal(bvec2 x, bvec2 y);"));
    286     s.append(TString("bvec3 equal(bvec3 x, bvec3 y);"));
    287     s.append(TString("bvec4 equal(bvec4 x, bvec4 y);"));
    288 
    289     s.append(TString("bvec2 notEqual(vec2 x, vec2 y);"));
    290     s.append(TString("bvec3 notEqual(vec3 x, vec3 y);"));
    291     s.append(TString("bvec4 notEqual(vec4 x, vec4 y);"));
    292 
    293     s.append(TString("bvec2 notEqual(ivec2 x, ivec2 y);"));
    294     s.append(TString("bvec3 notEqual(ivec3 x, ivec3 y);"));
    295     s.append(TString("bvec4 notEqual(ivec4 x, ivec4 y);"));
    296 
    297     s.append(TString("bvec2 notEqual(bvec2 x, bvec2 y);"));
    298     s.append(TString("bvec3 notEqual(bvec3 x, bvec3 y);"));
    299     s.append(TString("bvec4 notEqual(bvec4 x, bvec4 y);"));
    300 
    301     s.append(TString("bool any(bvec2 x);"));
    302     s.append(TString("bool any(bvec3 x);"));
    303     s.append(TString("bool any(bvec4 x);"));
    304 
    305     s.append(TString("bool all(bvec2 x);"));
    306     s.append(TString("bool all(bvec3 x);"));
    307     s.append(TString("bool all(bvec4 x);"));
    308 
    309     s.append(TString("bvec2 not(bvec2 x);"));
    310     s.append(TString("bvec3 not(bvec3 x);"));
    311     s.append(TString("bvec4 not(bvec4 x);"));
    312 
    313     //
    314     // Noise functions.
    315     //
    316     //s.append(TString("float noise1(float x);"));
    317     //s.append(TString("float noise1(vec2  x);"));
    318     //s.append(TString("float noise1(vec3  x);"));
    319     //s.append(TString("float noise1(vec4  x);"));
    320 
    321     //s.append(TString("vec2 noise2(float x);"));
    322     //s.append(TString("vec2 noise2(vec2  x);"));
    323     //s.append(TString("vec2 noise2(vec3  x);"));
    324     //s.append(TString("vec2 noise2(vec4  x);"));
    325 
    326     //s.append(TString("vec3 noise3(float x);"));
    327     //s.append(TString("vec3 noise3(vec2  x);"));
    328     //s.append(TString("vec3 noise3(vec3  x);"));
    329     //s.append(TString("vec3 noise3(vec4  x);"));
    330 
    331     //s.append(TString("vec4 noise4(float x);"));
    332     //s.append(TString("vec4 noise4(vec2  x);"));
    333     //s.append(TString("vec4 noise4(vec3  x);"));
    334     //s.append(TString("vec4 noise4(vec4  x);"));
    335 
    336     return s;
    337 }
    338 
    339 //============================================================================
    340 //
    341 // Prototypes for built-in functions seen by vertex shaders only.
    342 //
    343 //============================================================================
    344 static TString BuiltInFunctionsVertex(const ShBuiltInResources& resources)
    345 {
    346     TString s;
    347 
    348     //
    349     // Geometric Functions.
    350     //
    351     //s.append(TString("vec4 ftransform();"));
    352 
    353     //
    354     // Texture Functions.
    355     //
    356     if (resources.MaxVertexTextureImageUnits > 0) {
    357         s.append(TString("vec4 texture2D(sampler2D sampler, vec2 coord);"));
    358         s.append(TString("vec4 texture2DProj(sampler2D sampler, vec3 coord);"));
    359         s.append(TString("vec4 texture2DProj(sampler2D sampler, vec4 coord);"));
    360         s.append(TString("vec4 textureCube(samplerCube sampler, vec3 coord);"));
    361 
    362         s.append(TString("vec4 texture2DLod(sampler2D sampler, vec2 coord, float lod);"));
    363         s.append(TString("vec4 texture2DProjLod(sampler2D sampler, vec3 coord, float lod);"));
    364         s.append(TString("vec4 texture2DProjLod(sampler2D sampler, vec4 coord, float lod);"));
    365         s.append(TString("vec4 textureCubeLod(samplerCube sampler, vec3 coord, float lod);"));
    366     }
    367 
    368     return s;
    369 }
    370 
    371 //============================================================================
    372 //
    373 // Prototypes for built-in functions seen by fragment shaders only.
    374 //
    375 //============================================================================
    376 static TString BuiltInFunctionsFragment(const ShBuiltInResources& resources)
    377 {
    378     TString s;
    379 
    380     //
    381     // Texture Functions.
    382     //
    383     s.append(TString("vec4 texture2D(sampler2D sampler, vec2 coord);"));
    384     s.append(TString("vec4 texture2DProj(sampler2D sampler, vec3 coord);"));
    385     s.append(TString("vec4 texture2DProj(sampler2D sampler, vec4 coord);"));
    386     s.append(TString("vec4 textureCube(samplerCube sampler, vec3 coord);"));
    387 
    388     s.append(TString("vec4 texture2D(sampler2D sampler, vec2 coord, float bias);"));
    389     s.append(TString("vec4 texture2DProj(sampler2D sampler, vec3 coord, float bias);"));
    390     s.append(TString("vec4 texture2DProj(sampler2D sampler, vec4 coord, float bias);"));
    391     s.append(TString("vec4 textureCube(samplerCube sampler, vec3 coord, float bias);"));
    392 
    393     if (resources.OES_standard_derivatives) {
    394         s.append(TString("float dFdx(float p);"));
    395         s.append(TString("vec2  dFdx(vec2  p);"));
    396         s.append(TString("vec3  dFdx(vec3  p);"));
    397         s.append(TString("vec4  dFdx(vec4  p);"));
    398 
    399         s.append(TString("float dFdy(float p);"));
    400         s.append(TString("vec2  dFdy(vec2  p);"));
    401         s.append(TString("vec3  dFdy(vec3  p);"));
    402         s.append(TString("vec4  dFdy(vec4  p);"));
    403 
    404         s.append(TString("float fwidth(float p);"));
    405         s.append(TString("vec2  fwidth(vec2  p);"));
    406         s.append(TString("vec3  fwidth(vec3  p);"));
    407         s.append(TString("vec4  fwidth(vec4  p);"));
    408     }
    409 
    410     return s;
    411 }
    412 
    413 //============================================================================
    414 //
    415 // Standard uniforms.
    416 //
    417 //============================================================================
    418 static TString StandardUniforms()
    419 {
    420     TString s;
    421 
    422     //
    423     // Depth range in window coordinates
    424     //
    425     s.append(TString("struct gl_DepthRangeParameters {"));
    426     s.append(TString("    highp float near;"));        // n
    427     s.append(TString("    highp float far;"));         // f
    428     s.append(TString("    highp float diff;"));        // f - n
    429     s.append(TString("};"));
    430     s.append(TString("uniform gl_DepthRangeParameters gl_DepthRange;"));
    431 
    432     return s;
    433 }
    434 
    435 //============================================================================
    436 //
    437 // Default precision for vertex shaders.
    438 //
    439 //============================================================================
    440 static TString DefaultPrecisionVertex()
    441 {
    442     TString s;
    443 
    444     s.append(TString("precision highp int;"));
    445     s.append(TString("precision highp float;"));
    446 
    447     return s;
    448 }
    449 
    450 //============================================================================
    451 //
    452 // Default precision for fragment shaders.
    453 //
    454 //============================================================================
    455 static TString DefaultPrecisionFragment()
    456 {
    457     TString s;
    458 
    459     s.append(TString("precision mediump int;"));
    460     // No default precision for float in fragment shaders
    461 
    462     return s;
    463 }
    464 
    465 //============================================================================
    466 //
    467 // Implementation dependent built-in constants.
    468 //
    469 //============================================================================
    470 static TString BuiltInConstants(const ShBuiltInResources &resources)
    471 {
    472     TStringStream s;
    473 
    474     s << "const int gl_MaxVertexAttribs = " << resources.MaxVertexAttribs << ";";
    475     s << "const int gl_MaxVertexUniformVectors = " << resources.MaxVertexUniformVectors << ";";
    476 
    477     s << "const int gl_MaxVaryingVectors = " << resources.MaxVaryingVectors << ";";
    478     s << "const int gl_MaxVertexTextureImageUnits = " << resources.MaxVertexTextureImageUnits << ";";
    479     s << "const int gl_MaxCombinedTextureImageUnits = " << resources.MaxCombinedTextureImageUnits << ";";
    480     s << "const int gl_MaxTextureImageUnits = " << resources.MaxTextureImageUnits << ";";
    481     s << "const int gl_MaxFragmentUniformVectors = " << resources.MaxFragmentUniformVectors << ";";
    482     s << "const int gl_MaxDrawBuffers = " << resources.MaxDrawBuffers << ";";
    483 
    484     return s.str();
    485 }
    486 
    487 void TBuiltIns::initialize(ShShaderType type, ShShaderSpec spec,
    488                            const ShBuiltInResources& resources)
    489 {
    490     switch (type) {
    491     case SH_FRAGMENT_SHADER:
    492         builtInStrings.push_back(DefaultPrecisionFragment());
    493         builtInStrings.push_back(BuiltInFunctionsCommon());
    494         builtInStrings.push_back(BuiltInFunctionsFragment(resources));
    495         builtInStrings.push_back(StandardUniforms());
    496         break;
    497 
    498     case SH_VERTEX_SHADER:
    499         builtInStrings.push_back(DefaultPrecisionVertex());
    500         builtInStrings.push_back(BuiltInFunctionsCommon());
    501         builtInStrings.push_back(BuiltInFunctionsVertex(resources));
    502         builtInStrings.push_back(StandardUniforms());
    503         break;
    504 
    505     default: assert(false && "Language not supported");
    506     }
    507 
    508     builtInStrings.push_back(BuiltInConstants(resources));
    509 }
    510 
    511 void IdentifyBuiltIns(ShShaderType type, ShShaderSpec spec,
    512                       const ShBuiltInResources& resources,
    513                       TSymbolTable& symbolTable)
    514 {
    515     //
    516     // First, insert some special built-in variables that are not in
    517     // the built-in header files.
    518     //
    519     switch(type) {
    520     case SH_FRAGMENT_SHADER:
    521         symbolTable.insert(*new TVariable(NewPoolTString("gl_FragCoord"),                   TType(EbtFloat, EbpMedium, EvqFragCoord,   4)));
    522         symbolTable.insert(*new TVariable(NewPoolTString("gl_FrontFacing"),                 TType(EbtBool,  EbpUndefined, EvqFrontFacing, 1)));
    523         symbolTable.insert(*new TVariable(NewPoolTString("gl_FragColor"),                   TType(EbtFloat, EbpMedium, EvqFragColor,   4)));
    524         symbolTable.insert(*new TVariable(NewPoolTString("gl_FragData[gl_MaxDrawBuffers]"), TType(EbtFloat, EbpMedium, EvqFragData,    4)));
    525         symbolTable.insert(*new TVariable(NewPoolTString("gl_PointCoord"),                  TType(EbtFloat, EbpMedium, EvqPointCoord,  2)));
    526         break;
    527 
    528     case SH_VERTEX_SHADER:
    529         symbolTable.insert(*new TVariable(NewPoolTString("gl_Position"),    TType(EbtFloat, EbpHigh, EvqPosition,    4)));
    530         symbolTable.insert(*new TVariable(NewPoolTString("gl_PointSize"),   TType(EbtFloat, EbpMedium, EvqPointSize,   1)));
    531         break;
    532 
    533     default: assert(false && "Language not supported");
    534     }
    535 
    536     //
    537     // Next, identify which built-ins from the already loaded headers have
    538     // a mapping to an operator.  Those that are not identified as such are
    539     // expected to be resolved through a library of functions, versus as
    540     // operations.
    541     //
    542     symbolTable.relateToOperator("not",              EOpVectorLogicalNot);
    543 
    544     symbolTable.relateToOperator("matrixCompMult",   EOpMul);
    545 
    546     symbolTable.relateToOperator("equal",            EOpVectorEqual);
    547     symbolTable.relateToOperator("notEqual",         EOpVectorNotEqual);
    548     symbolTable.relateToOperator("lessThan",         EOpLessThan);
    549     symbolTable.relateToOperator("greaterThan",      EOpGreaterThan);
    550     symbolTable.relateToOperator("lessThanEqual",    EOpLessThanEqual);
    551     symbolTable.relateToOperator("greaterThanEqual", EOpGreaterThanEqual);
    552 
    553     symbolTable.relateToOperator("radians",      EOpRadians);
    554     symbolTable.relateToOperator("degrees",      EOpDegrees);
    555     symbolTable.relateToOperator("sin",          EOpSin);
    556     symbolTable.relateToOperator("cos",          EOpCos);
    557     symbolTable.relateToOperator("tan",          EOpTan);
    558     symbolTable.relateToOperator("asin",         EOpAsin);
    559     symbolTable.relateToOperator("acos",         EOpAcos);
    560     symbolTable.relateToOperator("atan",         EOpAtan);
    561 
    562     symbolTable.relateToOperator("pow",          EOpPow);
    563     symbolTable.relateToOperator("exp2",         EOpExp2);
    564     symbolTable.relateToOperator("log",          EOpLog);
    565     symbolTable.relateToOperator("exp",          EOpExp);
    566     symbolTable.relateToOperator("log2",         EOpLog2);
    567     symbolTable.relateToOperator("sqrt",         EOpSqrt);
    568     symbolTable.relateToOperator("inversesqrt",  EOpInverseSqrt);
    569 
    570     symbolTable.relateToOperator("abs",          EOpAbs);
    571     symbolTable.relateToOperator("sign",         EOpSign);
    572     symbolTable.relateToOperator("floor",        EOpFloor);
    573     symbolTable.relateToOperator("ceil",         EOpCeil);
    574     symbolTable.relateToOperator("fract",        EOpFract);
    575     symbolTable.relateToOperator("mod",          EOpMod);
    576     symbolTable.relateToOperator("min",          EOpMin);
    577     symbolTable.relateToOperator("max",          EOpMax);
    578     symbolTable.relateToOperator("clamp",        EOpClamp);
    579     symbolTable.relateToOperator("mix",          EOpMix);
    580     symbolTable.relateToOperator("step",         EOpStep);
    581     symbolTable.relateToOperator("smoothstep",   EOpSmoothStep);
    582 
    583     symbolTable.relateToOperator("length",       EOpLength);
    584     symbolTable.relateToOperator("distance",     EOpDistance);
    585     symbolTable.relateToOperator("dot",          EOpDot);
    586     symbolTable.relateToOperator("cross",        EOpCross);
    587     symbolTable.relateToOperator("normalize",    EOpNormalize);
    588     symbolTable.relateToOperator("faceforward",  EOpFaceForward);
    589     symbolTable.relateToOperator("reflect",      EOpReflect);
    590     symbolTable.relateToOperator("refract",      EOpRefract);
    591 
    592     symbolTable.relateToOperator("any",          EOpAny);
    593     symbolTable.relateToOperator("all",          EOpAll);
    594 
    595     // Map language-specific operators.
    596     switch(type) {
    597     case SH_VERTEX_SHADER:
    598         break;
    599     case SH_FRAGMENT_SHADER:
    600         if (resources.OES_standard_derivatives) {
    601             symbolTable.relateToOperator("dFdx",   EOpDFdx);
    602             symbolTable.relateToOperator("dFdy",   EOpDFdy);
    603             symbolTable.relateToOperator("fwidth", EOpFwidth);
    604 
    605             symbolTable.relateToExtension("dFdx", "GL_OES_standard_derivatives");
    606             symbolTable.relateToExtension("dFdy", "GL_OES_standard_derivatives");
    607             symbolTable.relateToExtension("fwidth", "GL_OES_standard_derivatives");
    608         }
    609         break;
    610     default: break;
    611     }
    612 
    613     // Finally add resource-specific variables.
    614     switch(type) {
    615     case SH_FRAGMENT_SHADER: {
    616             // Set up gl_FragData.  The array size.
    617             TType fragData(EbtFloat, EbpMedium, EvqFragColor,   4, false, true);
    618             fragData.setArraySize(resources.MaxDrawBuffers);
    619             symbolTable.insert(*new TVariable(NewPoolTString("gl_FragData"),    fragData));
    620         }
    621         break;
    622     default: break;
    623     }
    624 }
    625 
    626 void InitExtensionBehavior(const ShBuiltInResources& resources,
    627                            TExtensionBehavior& extBehavior)
    628 {
    629     if (resources.OES_standard_derivatives)
    630         extBehavior["GL_OES_standard_derivatives"] = EBhDisable;
    631 }
    632