mesa: remove support for GL_APPLE_client_storage extension
[mesa.git] / src / glsl / ir_variable.cpp
index 15a4a92f628897a277d1296b7575fc937259d921..e0b6f38f1c910def256831837a548dd42ddbf998 100644 (file)
 #include "glsl_parser_extras.h"
 #include "glsl_symbol_table.h"
 #include "builtin_variables.h"
+#include "main/uniforms.h"
+#include "program/prog_parameter.h"
+#include "program/prog_statevars.h"
+#include "program/prog_instruction.h"
 
-#ifndef Elements
-#define Elements(x) (sizeof(x)/sizeof(*(x)))
-#endif
+static void generate_ARB_draw_buffers_variables(exec_list *,
+                                               struct _mesa_glsl_parse_state *,
+                                               bool, _mesa_glsl_parser_targets);
+
+static void
+generate_ARB_draw_instanced_variables(exec_list *,
+                                      struct _mesa_glsl_parse_state *,
+                                      bool, _mesa_glsl_parser_targets);
+
+static struct gl_builtin_uniform_element gl_DepthRange_elements[] = {
+   {"near", {STATE_DEPTH_RANGE, 0, 0}, SWIZZLE_XXXX},
+   {"far", {STATE_DEPTH_RANGE, 0, 0}, SWIZZLE_YYYY},
+   {"diff", {STATE_DEPTH_RANGE, 0, 0}, SWIZZLE_ZZZZ},
+};
+
+static struct gl_builtin_uniform_element gl_ClipPlane_elements[] = {
+   {NULL, {STATE_CLIPPLANE, 0, 0}, SWIZZLE_XYZW}
+};
+
+static struct gl_builtin_uniform_element gl_Point_elements[] = {
+   {"size", {STATE_POINT_SIZE}, SWIZZLE_XXXX},
+   {"sizeMin", {STATE_POINT_SIZE}, SWIZZLE_YYYY},
+   {"sizeMax", {STATE_POINT_SIZE}, SWIZZLE_ZZZZ},
+   {"fadeThresholdSize", {STATE_POINT_SIZE}, SWIZZLE_WWWW},
+   {"distanceConstantAttenuation", {STATE_POINT_ATTENUATION}, SWIZZLE_XXXX},
+   {"distanceLinearAttenuation", {STATE_POINT_ATTENUATION}, SWIZZLE_YYYY},
+   {"distanceQuadraticAttenuation", {STATE_POINT_ATTENUATION}, SWIZZLE_ZZZZ},
+};
+
+static struct gl_builtin_uniform_element gl_FrontMaterial_elements[] = {
+   {"emission", {STATE_MATERIAL, 0, STATE_EMISSION}, SWIZZLE_XYZW},
+   {"ambient", {STATE_MATERIAL, 0, STATE_AMBIENT}, SWIZZLE_XYZW},
+   {"diffuse", {STATE_MATERIAL, 0, STATE_DIFFUSE}, SWIZZLE_XYZW},
+   {"specular", {STATE_MATERIAL, 0, STATE_SPECULAR}, SWIZZLE_XYZW},
+   {"shininess", {STATE_MATERIAL, 0, STATE_SHININESS}, SWIZZLE_XXXX},
+};
+
+static struct gl_builtin_uniform_element gl_BackMaterial_elements[] = {
+   {"emission", {STATE_MATERIAL, 1, STATE_EMISSION}, SWIZZLE_XYZW},
+   {"ambient", {STATE_MATERIAL, 1, STATE_AMBIENT}, SWIZZLE_XYZW},
+   {"diffuse", {STATE_MATERIAL, 1, STATE_DIFFUSE}, SWIZZLE_XYZW},
+   {"specular", {STATE_MATERIAL, 1, STATE_SPECULAR}, SWIZZLE_XYZW},
+   {"shininess", {STATE_MATERIAL, 1, STATE_SHININESS}, SWIZZLE_XXXX},
+};
+
+static struct gl_builtin_uniform_element gl_LightSource_elements[] = {
+   {"ambient", {STATE_LIGHT, 0, STATE_AMBIENT}, SWIZZLE_XYZW},
+   {"diffuse", {STATE_LIGHT, 0, STATE_DIFFUSE}, SWIZZLE_XYZW},
+   {"specular", {STATE_LIGHT, 0, STATE_SPECULAR}, SWIZZLE_XYZW},
+   {"position", {STATE_LIGHT, 0, STATE_POSITION}, SWIZZLE_XYZW},
+   {"halfVector", {STATE_LIGHT, 0, STATE_HALF_VECTOR}, SWIZZLE_XYZW},
+   {"spotDirection", {STATE_LIGHT, 0, STATE_SPOT_DIRECTION},
+    MAKE_SWIZZLE4(SWIZZLE_X,
+                 SWIZZLE_Y,
+                 SWIZZLE_Z,
+                 SWIZZLE_Z)},
+   {"spotCosCutoff", {STATE_LIGHT, 0, STATE_SPOT_DIRECTION}, SWIZZLE_WWWW},
+   {"spotCutoff", {STATE_LIGHT, 0, STATE_SPOT_CUTOFF}, SWIZZLE_XXXX},
+   {"spotExponent", {STATE_LIGHT, 0, STATE_ATTENUATION}, SWIZZLE_WWWW},
+   {"constantAttenuation", {STATE_LIGHT, 0, STATE_ATTENUATION}, SWIZZLE_XXXX},
+   {"linearAttenuation", {STATE_LIGHT, 0, STATE_ATTENUATION}, SWIZZLE_YYYY},
+   {"quadraticAttenuation", {STATE_LIGHT, 0, STATE_ATTENUATION}, SWIZZLE_ZZZZ},
+};
+
+static struct gl_builtin_uniform_element gl_LightModel_elements[] = {
+   {"ambient", {STATE_LIGHTMODEL_AMBIENT, 0}, SWIZZLE_XYZW},
+};
+
+static struct gl_builtin_uniform_element gl_FrontLightModelProduct_elements[] = {
+   {"sceneColor", {STATE_LIGHTMODEL_SCENECOLOR, 0}, SWIZZLE_XYZW},
+};
+
+static struct gl_builtin_uniform_element gl_BackLightModelProduct_elements[] = {
+   {"sceneColor", {STATE_LIGHTMODEL_SCENECOLOR, 1}, SWIZZLE_XYZW},
+};
+
+static struct gl_builtin_uniform_element gl_FrontLightProduct_elements[] = {
+   {"ambient", {STATE_LIGHTPROD, 0, 0, STATE_AMBIENT}, SWIZZLE_XYZW},
+   {"diffuse", {STATE_LIGHTPROD, 0, 0, STATE_DIFFUSE}, SWIZZLE_XYZW},
+   {"specular", {STATE_LIGHTPROD, 0, 0, STATE_SPECULAR}, SWIZZLE_XYZW},
+};
+
+static struct gl_builtin_uniform_element gl_BackLightProduct_elements[] = {
+   {"ambient", {STATE_LIGHTPROD, 0, 1, STATE_AMBIENT}, SWIZZLE_XYZW},
+   {"diffuse", {STATE_LIGHTPROD, 0, 1, STATE_DIFFUSE}, SWIZZLE_XYZW},
+   {"specular", {STATE_LIGHTPROD, 0, 1, STATE_SPECULAR}, SWIZZLE_XYZW},
+};
+
+static struct gl_builtin_uniform_element gl_TextureEnvColor_elements[] = {
+   {NULL, {STATE_TEXENV_COLOR, 0}, SWIZZLE_XYZW},
+};
+
+static struct gl_builtin_uniform_element gl_EyePlaneS_elements[] = {
+   {NULL, {STATE_TEXGEN, 0, STATE_TEXGEN_EYE_S}, SWIZZLE_XYZW},
+};
+
+static struct gl_builtin_uniform_element gl_EyePlaneT_elements[] = {
+   {NULL, {STATE_TEXGEN, 0, STATE_TEXGEN_EYE_T}, SWIZZLE_XYZW},
+};
+
+static struct gl_builtin_uniform_element gl_EyePlaneR_elements[] = {
+   {NULL, {STATE_TEXGEN, 0, STATE_TEXGEN_EYE_R}, SWIZZLE_XYZW},
+};
+
+static struct gl_builtin_uniform_element gl_EyePlaneQ_elements[] = {
+   {NULL, {STATE_TEXGEN, 0, STATE_TEXGEN_EYE_Q}, SWIZZLE_XYZW},
+};
+
+static struct gl_builtin_uniform_element gl_ObjectPlaneS_elements[] = {
+   {NULL, {STATE_TEXGEN, 0, STATE_TEXGEN_OBJECT_S}, SWIZZLE_XYZW},
+};
+
+static struct gl_builtin_uniform_element gl_ObjectPlaneT_elements[] = {
+   {NULL, {STATE_TEXGEN, 0, STATE_TEXGEN_OBJECT_T}, SWIZZLE_XYZW},
+};
+
+static struct gl_builtin_uniform_element gl_ObjectPlaneR_elements[] = {
+   {NULL, {STATE_TEXGEN, 0, STATE_TEXGEN_OBJECT_R}, SWIZZLE_XYZW},
+};
+
+static struct gl_builtin_uniform_element gl_ObjectPlaneQ_elements[] = {
+   {NULL, {STATE_TEXGEN, 0, STATE_TEXGEN_OBJECT_Q}, SWIZZLE_XYZW},
+};
+
+static struct gl_builtin_uniform_element gl_Fog_elements[] = {
+   {"color", {STATE_FOG_COLOR}, SWIZZLE_XYZW},
+   {"density", {STATE_FOG_PARAMS}, SWIZZLE_XXXX},
+   {"start", {STATE_FOG_PARAMS}, SWIZZLE_YYYY},
+   {"end", {STATE_FOG_PARAMS}, SWIZZLE_ZZZZ},
+   {"scale", {STATE_FOG_PARAMS}, SWIZZLE_WWWW},
+};
+
+static struct gl_builtin_uniform_element gl_NormalScale_elements[] = {
+   {NULL, {STATE_NORMAL_SCALE}, SWIZZLE_XXXX},
+};
+
+static struct gl_builtin_uniform_element gl_MESABumpRotMatrix0_elements[] = {
+   {NULL, {STATE_INTERNAL, STATE_ROT_MATRIX_0}, SWIZZLE_XYZW},
+};
+
+static struct gl_builtin_uniform_element gl_MESABumpRotMatrix1_elements[] = {
+   {NULL, {STATE_INTERNAL, STATE_ROT_MATRIX_1}, SWIZZLE_XYZW},
+};
+
+static struct gl_builtin_uniform_element gl_MESAFogParamsOptimized_elements[] = {
+   {NULL, {STATE_INTERNAL, STATE_FOG_PARAMS_OPTIMIZED}, SWIZZLE_XYZW},
+};
+
+#define MATRIX(name, statevar, modifier)                               \
+   static struct gl_builtin_uniform_element name ## _elements[] = {    \
+      { NULL, { statevar, 0, 0, 0, modifier}, SWIZZLE_XYZW },          \
+      { NULL, { statevar, 0, 1, 1, modifier}, SWIZZLE_XYZW },          \
+      { NULL, { statevar, 0, 2, 2, modifier}, SWIZZLE_XYZW },          \
+      { NULL, { statevar, 0, 3, 3, modifier}, SWIZZLE_XYZW },          \
+   }
+
+MATRIX(gl_ModelViewMatrix,
+       STATE_MODELVIEW_MATRIX, STATE_MATRIX_TRANSPOSE);
+MATRIX(gl_ModelViewMatrixInverse,
+       STATE_MODELVIEW_MATRIX, STATE_MATRIX_INVTRANS);
+MATRIX(gl_ModelViewMatrixTranspose,
+       STATE_MODELVIEW_MATRIX, 0);
+MATRIX(gl_ModelViewMatrixInverseTranspose,
+       STATE_MODELVIEW_MATRIX, STATE_MATRIX_INVERSE);
+
+MATRIX(gl_ProjectionMatrix,
+       STATE_PROJECTION_MATRIX, STATE_MATRIX_TRANSPOSE);
+MATRIX(gl_ProjectionMatrixInverse,
+       STATE_PROJECTION_MATRIX, STATE_MATRIX_INVTRANS);
+MATRIX(gl_ProjectionMatrixTranspose,
+       STATE_PROJECTION_MATRIX, 0);
+MATRIX(gl_ProjectionMatrixInverseTranspose,
+       STATE_PROJECTION_MATRIX, STATE_MATRIX_INVERSE);
+
+MATRIX(gl_ModelViewProjectionMatrix,
+       STATE_MVP_MATRIX, STATE_MATRIX_TRANSPOSE);
+MATRIX(gl_ModelViewProjectionMatrixInverse,
+       STATE_MVP_MATRIX, STATE_MATRIX_INVTRANS);
+MATRIX(gl_ModelViewProjectionMatrixTranspose,
+       STATE_MVP_MATRIX, 0);
+MATRIX(gl_ModelViewProjectionMatrixInverseTranspose,
+       STATE_MVP_MATRIX, STATE_MATRIX_INVERSE);
+
+MATRIX(gl_TextureMatrix,
+       STATE_TEXTURE_MATRIX, STATE_MATRIX_TRANSPOSE);
+MATRIX(gl_TextureMatrixInverse,
+       STATE_TEXTURE_MATRIX, STATE_MATRIX_INVTRANS);
+MATRIX(gl_TextureMatrixTranspose,
+       STATE_TEXTURE_MATRIX, 0);
+MATRIX(gl_TextureMatrixInverseTranspose,
+       STATE_TEXTURE_MATRIX, STATE_MATRIX_INVERSE);
+
+static struct gl_builtin_uniform_element gl_NormalMatrix_elements[] = {
+   { NULL, { STATE_MODELVIEW_MATRIX, 0, 0, 0, STATE_MATRIX_INVERSE},
+     SWIZZLE_XYZW },
+   { NULL, { STATE_MODELVIEW_MATRIX, 0, 1, 1, STATE_MATRIX_INVERSE},
+     SWIZZLE_XYZW },
+   { NULL, { STATE_MODELVIEW_MATRIX, 0, 2, 2, STATE_MATRIX_INVERSE},
+     SWIZZLE_XYZW },
+};
+
+#undef MATRIX
+
+#define STATEVAR(name) {#name, name ## _elements, Elements(name ## _elements)}
+
+const struct gl_builtin_uniform_desc _mesa_builtin_uniform_desc[] = {
+   STATEVAR(gl_DepthRange),
+   STATEVAR(gl_ClipPlane),
+   STATEVAR(gl_Point),
+   STATEVAR(gl_FrontMaterial),
+   STATEVAR(gl_BackMaterial),
+   STATEVAR(gl_LightSource),
+   STATEVAR(gl_LightModel),
+   STATEVAR(gl_FrontLightModelProduct),
+   STATEVAR(gl_BackLightModelProduct),
+   STATEVAR(gl_FrontLightProduct),
+   STATEVAR(gl_BackLightProduct),
+   STATEVAR(gl_TextureEnvColor),
+   STATEVAR(gl_EyePlaneS),
+   STATEVAR(gl_EyePlaneT),
+   STATEVAR(gl_EyePlaneR),
+   STATEVAR(gl_EyePlaneQ),
+   STATEVAR(gl_ObjectPlaneS),
+   STATEVAR(gl_ObjectPlaneT),
+   STATEVAR(gl_ObjectPlaneR),
+   STATEVAR(gl_ObjectPlaneQ),
+   STATEVAR(gl_Fog),
+
+   STATEVAR(gl_ModelViewMatrix),
+   STATEVAR(gl_ModelViewMatrixInverse),
+   STATEVAR(gl_ModelViewMatrixTranspose),
+   STATEVAR(gl_ModelViewMatrixInverseTranspose),
+
+   STATEVAR(gl_ProjectionMatrix),
+   STATEVAR(gl_ProjectionMatrixInverse),
+   STATEVAR(gl_ProjectionMatrixTranspose),
+   STATEVAR(gl_ProjectionMatrixInverseTranspose),
+
+   STATEVAR(gl_ModelViewProjectionMatrix),
+   STATEVAR(gl_ModelViewProjectionMatrixInverse),
+   STATEVAR(gl_ModelViewProjectionMatrixTranspose),
+   STATEVAR(gl_ModelViewProjectionMatrixInverseTranspose),
+
+   STATEVAR(gl_TextureMatrix),
+   STATEVAR(gl_TextureMatrixInverse),
+   STATEVAR(gl_TextureMatrixTranspose),
+   STATEVAR(gl_TextureMatrixInverseTranspose),
+
+   STATEVAR(gl_NormalMatrix),
+   STATEVAR(gl_NormalScale),
+
+   STATEVAR(gl_MESABumpRotMatrix0),
+   STATEVAR(gl_MESABumpRotMatrix1),
+   STATEVAR(gl_MESAFogParamsOptimized),
+
+   {NULL, NULL, 0}
+};
 
 static ir_variable *
-add_variable(const char *name, enum ir_variable_mode mode, int slot,
-            const glsl_type *type, exec_list *instructions,
-                    glsl_symbol_table *symtab)
+add_variable(exec_list *instructions, glsl_symbol_table *symtab,
+            const char *name, const glsl_type *type,
+            enum ir_variable_mode mode, int slot)
 {
-   ir_variable *var = new(symtab) ir_variable(type, name);
+   ir_variable *var = new(symtab) ir_variable(type, name, mode);
 
-   var->mode = mode;
    switch (var->mode) {
+   case ir_var_auto:
    case ir_var_in:
-      var->shader_in = true;
+   case ir_var_const_in:
+   case ir_var_uniform:
+   case ir_var_system_value:
       var->read_only = true;
       break;
    case ir_var_inout:
-      var->shader_in = true;
-      var->shader_out = true;
-      break;
    case ir_var_out:
-      var->shader_out = true;
-      break;
-   case ir_var_uniform:
-      var->shader_in = true;
-      var->read_only = true;
       break;
    default:
       assert(0);
@@ -60,20 +312,63 @@ add_variable(const char *name, enum ir_variable_mode mode, int slot,
    }
 
    var->location = slot;
+   var->explicit_location = (slot >= 0);
 
    /* Once the variable is created an initialized, add it to the symbol table
     * and add the declaration to the IR stream.
     */
    instructions->push_tail(var);
 
-   symtab->add_variable(var->name, var);
+   symtab->add_variable(var);
    return var;
 }
 
+static ir_variable *
+add_uniform(exec_list *instructions, glsl_symbol_table *symtab,
+           const char *name, const glsl_type *type)
+{
+   ir_variable *const uni =
+      add_variable(instructions, symtab, name, type, ir_var_uniform, -1);
+
+   unsigned i;
+   for (i = 0; _mesa_builtin_uniform_desc[i].name != NULL; i++) {
+      if (strcmp(_mesa_builtin_uniform_desc[i].name, name) == 0) {
+        break;
+      }
+   }
+
+   assert(_mesa_builtin_uniform_desc[i].name != NULL);
+   const struct gl_builtin_uniform_desc* const statevar =
+      &_mesa_builtin_uniform_desc[i];
+
+   const unsigned array_count = type->is_array() ? type->length : 1;
+   uni->num_state_slots = array_count * statevar->num_elements;
+
+   ir_state_slot *slots =
+      ralloc_array(uni, ir_state_slot, uni->num_state_slots);
+
+   uni->state_slots = slots;
+
+   for (unsigned a = 0; a < array_count; a++) {
+      for (unsigned j = 0; j < statevar->num_elements; j++) {
+        struct gl_builtin_uniform_element *element = &statevar->elements[j];
+
+        memcpy(slots->tokens, element->tokens, sizeof(element->tokens));
+        if (type->is_array()) {
+           slots->tokens[1] = a;
+        }
+
+        slots->swizzle = element->swizzle;
+        slots++;
+      }
+   }
+
+   return uni;
+}
 
 static void
-add_builtin_variable(const builtin_variable *proto, exec_list *instructions,
-                    glsl_symbol_table *symtab)
+add_builtin_variable(exec_list *instructions, glsl_symbol_table *symtab,
+                    const builtin_variable *proto)
 {
    /* Create a new variable declaration from the description supplied by
     * the caller.
@@ -82,121 +377,253 @@ add_builtin_variable(const builtin_variable *proto, exec_list *instructions,
 
    assert(type != NULL);
 
-   add_variable(proto->name, proto->mode, proto->slot, type, instructions,
-               symtab);
+   if (proto->mode == ir_var_uniform) {
+      add_uniform(instructions, symtab, proto->name, type);
+   } else {
+      add_variable(instructions, symtab, proto->name, type, proto->mode,
+                  proto->slot);
+   }
 }
 
+static void
+add_builtin_constant(exec_list *instructions, glsl_symbol_table *symtab,
+                    const char *name, int value)
+{
+   ir_variable *const var = add_variable(instructions, symtab,
+                                        name, glsl_type::int_type,
+                                        ir_var_auto, -1);
+   var->constant_value = new(var) ir_constant(value);
+}
+
+/* Several constants in GLSL ES have different names than normal desktop GLSL.
+ * Therefore, this function should only be called on the ES path.
+ */
+static void
+generate_100ES_uniforms(exec_list *instructions,
+                    struct _mesa_glsl_parse_state *state)
+{
+   glsl_symbol_table *const symtab = state->symbols;
+
+   add_builtin_constant(instructions, symtab, "gl_MaxVertexAttribs",
+                       state->Const.MaxVertexAttribs);
+   add_builtin_constant(instructions, symtab, "gl_MaxVertexUniformVectors",
+                       state->Const.MaxVertexUniformComponents);
+   add_builtin_constant(instructions, symtab, "gl_MaxVaryingVectors",
+                       state->Const.MaxVaryingFloats / 4);
+   add_builtin_constant(instructions, symtab, "gl_MaxVertexTextureImageUnits",
+                       state->Const.MaxVertexTextureImageUnits);
+   add_builtin_constant(instructions, symtab, "gl_MaxCombinedTextureImageUnits",
+                       state->Const.MaxCombinedTextureImageUnits);
+   add_builtin_constant(instructions, symtab, "gl_MaxTextureImageUnits",
+                       state->Const.MaxTextureImageUnits);
+   add_builtin_constant(instructions, symtab, "gl_MaxFragmentUniformVectors",
+                       state->Const.MaxFragmentUniformComponents);
+
+   add_uniform(instructions, symtab, "gl_DepthRange",
+              state->symbols->get_type("gl_DepthRangeParameters"));
+}
 
 static void
 generate_110_uniforms(exec_list *instructions,
-                     glsl_symbol_table *symtab)
+                     struct _mesa_glsl_parse_state *state)
 {
+   glsl_symbol_table *const symtab = state->symbols;
+
    for (unsigned i = 0
           ; i < Elements(builtin_110_deprecated_uniforms)
           ; i++) {
-      add_builtin_variable(& builtin_110_deprecated_uniforms[i],
-                          instructions, symtab);
+      add_builtin_variable(instructions, symtab,
+                          & builtin_110_deprecated_uniforms[i]);
    }
 
-   /* FINISHME: The size of this array is implementation dependent based on the
-    * FINISHME: value of GL_MAX_TEXTURE_COORDS.  Every platform that supports
-    * FINISHME: GLSL sets GL_MAX_TEXTURE_COORDS to at least 4, so hard-code 4
-    * FINISHME: for now.
-    */
+   add_builtin_constant(instructions, symtab, "gl_MaxLights",
+                       state->Const.MaxLights);
+   add_builtin_constant(instructions, symtab, "gl_MaxClipPlanes",
+                       state->Const.MaxClipPlanes);
+   add_builtin_constant(instructions, symtab, "gl_MaxTextureUnits",
+                       state->Const.MaxTextureUnits);
+   add_builtin_constant(instructions, symtab, "gl_MaxTextureCoords",
+                       state->Const.MaxTextureCoords);
+   add_builtin_constant(instructions, symtab, "gl_MaxVertexAttribs",
+                       state->Const.MaxVertexAttribs);
+   add_builtin_constant(instructions, symtab, "gl_MaxVertexUniformComponents",
+                       state->Const.MaxVertexUniformComponents);
+   add_builtin_constant(instructions, symtab, "gl_MaxVaryingFloats",
+                       state->Const.MaxVaryingFloats);
+   add_builtin_constant(instructions, symtab, "gl_MaxVertexTextureImageUnits",
+                       state->Const.MaxVertexTextureImageUnits);
+   add_builtin_constant(instructions, symtab, "gl_MaxCombinedTextureImageUnits",
+                       state->Const.MaxCombinedTextureImageUnits);
+   add_builtin_constant(instructions, symtab, "gl_MaxTextureImageUnits",
+                       state->Const.MaxTextureImageUnits);
+   add_builtin_constant(instructions, symtab, "gl_MaxFragmentUniformComponents",
+                       state->Const.MaxFragmentUniformComponents);
+
    const glsl_type *const mat4_array_type =
-      glsl_type::get_array_instance(symtab, glsl_type::mat4_type, 4);
+      glsl_type::get_array_instance(glsl_type::mat4_type,
+                                   state->Const.MaxTextureCoords);
 
-   add_variable("gl_TextureMatrix", ir_var_uniform, -1, mat4_array_type,
-               instructions, symtab);
+   add_uniform(instructions, symtab, "gl_TextureMatrix", mat4_array_type);
+   add_uniform(instructions, symtab, "gl_TextureMatrixInverse", mat4_array_type);
+   add_uniform(instructions, symtab, "gl_TextureMatrixTranspose", mat4_array_type);
+   add_uniform(instructions, symtab, "gl_TextureMatrixInverseTranspose", mat4_array_type);
 
-   /* FINISHME: Add support for gl_DepthRangeParameters */
-   /* FINISHME: Add support for gl_ClipPlane[] */
-   /* FINISHME: Add support for gl_PointParameters */
+   add_uniform(instructions, symtab, "gl_DepthRange",
+               symtab->get_type("gl_DepthRangeParameters"));
 
-   /* FINISHME: Add support for gl_MaterialParameters
-    * FINISHME: (glFrontMaterial, glBackMaterial)
-    */
+   add_uniform(instructions, symtab, "gl_ClipPlane",
+              glsl_type::get_array_instance(glsl_type::vec4_type,
+                                            state->Const.MaxClipPlanes));
+   add_uniform(instructions, symtab, "gl_Point",
+              symtab->get_type("gl_PointParameters"));
+
+   const glsl_type *const material_parameters_type =
+      symtab->get_type("gl_MaterialParameters");
+   add_uniform(instructions, symtab, "gl_FrontMaterial", material_parameters_type);
+   add_uniform(instructions, symtab, "gl_BackMaterial", material_parameters_type);
 
-   /* FINISHME: The size of this array is implementation dependent based on the
-    * FINISHME: value of GL_MAX_TEXTURE_LIGHTS.  GL_MAX_TEXTURE_LIGHTS must be
-    * FINISHME: at least 8, so hard-code 8 for now.
-    */
    const glsl_type *const light_source_array_type =
-      glsl_type::get_array_instance(symtab,
-                                   symtab->get_type("gl_LightSourceParameters"), 8);
+      glsl_type::get_array_instance(symtab->get_type("gl_LightSourceParameters"), state->Const.MaxLights);
+
+   add_uniform(instructions, symtab, "gl_LightSource", light_source_array_type);
+
+   const glsl_type *const light_model_products_type =
+      symtab->get_type("gl_LightModelProducts");
+   add_uniform(instructions, symtab, "gl_FrontLightModelProduct",
+              light_model_products_type);
+   add_uniform(instructions, symtab, "gl_BackLightModelProduct",
+              light_model_products_type);
+
+   const glsl_type *const light_products_type =
+      glsl_type::get_array_instance(symtab->get_type("gl_LightProducts"),
+                                   state->Const.MaxLights);
+   add_uniform(instructions, symtab, "gl_FrontLightProduct", light_products_type);
+   add_uniform(instructions, symtab, "gl_BackLightProduct", light_products_type);
+
+   add_uniform(instructions, symtab, "gl_TextureEnvColor",
+              glsl_type::get_array_instance(glsl_type::vec4_type,
+                                            state->Const.MaxTextureUnits));
+
+   const glsl_type *const texcoords_vec4 =
+      glsl_type::get_array_instance(glsl_type::vec4_type,
+                                   state->Const.MaxTextureCoords);
+   add_uniform(instructions, symtab, "gl_EyePlaneS", texcoords_vec4);
+   add_uniform(instructions, symtab, "gl_EyePlaneT", texcoords_vec4);
+   add_uniform(instructions, symtab, "gl_EyePlaneR", texcoords_vec4);
+   add_uniform(instructions, symtab, "gl_EyePlaneQ", texcoords_vec4);
+   add_uniform(instructions, symtab, "gl_ObjectPlaneS", texcoords_vec4);
+   add_uniform(instructions, symtab, "gl_ObjectPlaneT", texcoords_vec4);
+   add_uniform(instructions, symtab, "gl_ObjectPlaneR", texcoords_vec4);
+   add_uniform(instructions, symtab, "gl_ObjectPlaneQ", texcoords_vec4);
+
+   add_uniform(instructions, symtab, "gl_Fog",
+              symtab->get_type("gl_FogParameters"));
+}
 
-   add_variable("gl_LightSource", ir_var_uniform, -1, light_source_array_type,
-               instructions, symtab);
+/* This function should only be called for ES, not desktop GL. */
+static void
+generate_100ES_vs_variables(exec_list *instructions,
+                         struct _mesa_glsl_parse_state *state)
+{
+   for (unsigned i = 0; i < Elements(builtin_core_vs_variables); i++) {
+      add_builtin_variable(instructions, state->symbols,
+                          & builtin_core_vs_variables[i]);
+   }
+
+   generate_100ES_uniforms(instructions, state);
 
-   /* FINISHME: Add support for gl_LightModel */
-   /* FINISHME: Add support for gl_FrontLightProduct[], gl_BackLightProduct[] */
-   /* FINISHME: Add support for gl_TextureEnvColor[] */
-   /* FINISHME: Add support for gl_ObjectPlane*[], gl_EyePlane*[] */
-   /* FINISHME: Add support for gl_Fog */
+   generate_ARB_draw_buffers_variables(instructions, state, false,
+                                      vertex_shader);
 }
 
+
 static void
 generate_110_vs_variables(exec_list *instructions,
-                         glsl_symbol_table *symtab)
+                         struct _mesa_glsl_parse_state *state)
 {
    for (unsigned i = 0; i < Elements(builtin_core_vs_variables); i++) {
-      add_builtin_variable(& builtin_core_vs_variables[i],
-                          instructions, symtab);
+      add_builtin_variable(instructions, state->symbols,
+                          & builtin_core_vs_variables[i]);
    }
 
    for (unsigned i = 0
           ; i < Elements(builtin_110_deprecated_vs_variables)
           ; i++) {
-      add_builtin_variable(& builtin_110_deprecated_vs_variables[i],
-                          instructions, symtab);
+      add_builtin_variable(instructions, state->symbols,
+                          & builtin_110_deprecated_vs_variables[i]);
    }
-   generate_110_uniforms(instructions, symtab);
-
-   /* FINISHME: The size of this array is implementation dependent based on the
-    * FINISHME: value of GL_MAX_TEXTURE_COORDS.  Every platform that supports
-    * FINISHME: GLSL sets GL_MAX_TEXTURE_COORDS to at least 4, so hard-code 4
-    * FINISHME: for now.
+   generate_110_uniforms(instructions, state);
+
+   /* From page 54 (page 60 of the PDF) of the GLSL 1.20 spec:
+    *
+    *     "As with all arrays, indices used to subscript gl_TexCoord must
+    *     either be an integral constant expressions, or this array must be
+    *     re-declared by the shader with a size. The size can be at most
+    *     gl_MaxTextureCoords. Using indexes close to 0 may aid the
+    *     implementation in preserving varying resources."
     */
    const glsl_type *const vec4_array_type =
-      glsl_type::get_array_instance(symtab, glsl_type::vec4_type, 4);
+      glsl_type::get_array_instance(glsl_type::vec4_type, 0);
+
+   add_variable(instructions, state->symbols,
+               "gl_TexCoord", vec4_array_type, ir_var_out, VERT_RESULT_TEX0);
 
-   add_variable("gl_TexCoord", ir_var_out, VERT_RESULT_TEX0, vec4_array_type,
-               instructions, symtab);
+   generate_ARB_draw_buffers_variables(instructions, state, false,
+                                      vertex_shader);
 }
 
 
 static void
 generate_120_vs_variables(exec_list *instructions,
-                         glsl_symbol_table *symtab)
+                         struct _mesa_glsl_parse_state *state)
 {
    /* GLSL version 1.20 did not add any built-in variables in the vertex
     * shader.
     */
-   generate_110_vs_variables(instructions, symtab);
+   generate_110_vs_variables(instructions, state);
+}
+
+
+static void
+generate_130_uniforms(exec_list *instructions,
+                     struct _mesa_glsl_parse_state *state)
+{
+   glsl_symbol_table *const symtab = state->symbols;
+
+   add_builtin_constant(instructions, symtab, "gl_MaxClipDistances",
+                        state->Const.MaxClipPlanes);
 }
 
 
 static void
 generate_130_vs_variables(exec_list *instructions,
-                         glsl_symbol_table *symtab)
+                         struct _mesa_glsl_parse_state *state)
 {
-   void *ctx = symtab;
-   generate_120_vs_variables(instructions, symtab);
+   generate_120_vs_variables(instructions, state);
 
    for (unsigned i = 0; i < Elements(builtin_130_vs_variables); i++) {
-      add_builtin_variable(& builtin_130_vs_variables[i],
-                          instructions, symtab);
+      add_builtin_variable(instructions, state->symbols,
+                          & builtin_130_vs_variables[i]);
    }
 
-   /* FINISHME: The size of this array is implementation dependent based on
-    * FINISHME: the value of GL_MAX_CLIP_DISTANCES.
+   generate_130_uniforms(instructions, state);
+
+   /* From the GLSL 1.30 spec, section 7.1 (Vertex Shader Special
+    * Variables):
+    *
+    *   The gl_ClipDistance array is predeclared as unsized and must
+    *   be sized by the shader either redeclaring it with a size or
+    *   indexing it only with integral constant expressions.
+    *
+    * We represent this in Mesa by initially declaring the array as
+    * size 0.
     */
    const glsl_type *const clip_distance_array_type =
-      glsl_type::get_array_instance(ctx, glsl_type::float_type, 8);
+      glsl_type::get_array_instance(glsl_type::float_type, 0);
 
    /* FINISHME: gl_ClipDistance needs a real location assigned. */
-   add_variable("gl_ClipDistance", ir_var_out, -1, clip_distance_array_type,
-               instructions, symtab);
+   add_variable(instructions, state->symbols,
+               "gl_ClipDistance", clip_distance_array_type, ir_var_out, -1);
 
 }
 
@@ -207,92 +634,216 @@ initialize_vs_variables(exec_list *instructions,
 {
 
    switch (state->language_version) {
+   case 100:
+      generate_100ES_vs_variables(instructions, state);
+      break;
    case 110:
-      generate_110_vs_variables(instructions, state->symbols);
+      generate_110_vs_variables(instructions, state);
       break;
    case 120:
-      generate_120_vs_variables(instructions, state->symbols);
+      generate_120_vs_variables(instructions, state);
       break;
    case 130:
-      generate_130_vs_variables(instructions, state->symbols);
+      generate_130_vs_variables(instructions, state);
       break;
    }
+
+   if (state->ARB_draw_instanced_enable)
+      generate_ARB_draw_instanced_variables(instructions, state, false,
+                                            vertex_shader);
+}
+
+
+/* This function should only be called for ES, not desktop GL. */
+static void
+generate_100ES_fs_variables(exec_list *instructions,
+                         struct _mesa_glsl_parse_state *state)
+{
+   for (unsigned i = 0; i < Elements(builtin_core_fs_variables); i++) {
+      add_builtin_variable(instructions, state->symbols,
+                          & builtin_core_fs_variables[i]);
+   }
+
+   for (unsigned i = 0; i < Elements(builtin_100ES_fs_variables); i++) {
+      add_builtin_variable(instructions, state->symbols,
+                          & builtin_100ES_fs_variables[i]);
+   }
+
+   generate_100ES_uniforms(instructions, state);
+
+   generate_ARB_draw_buffers_variables(instructions, state, false,
+                                      fragment_shader);
 }
 
 static void
 generate_110_fs_variables(exec_list *instructions,
-                         glsl_symbol_table *symtab)
+                         struct _mesa_glsl_parse_state *state)
 {
    for (unsigned i = 0; i < Elements(builtin_core_fs_variables); i++) {
-      add_builtin_variable(& builtin_core_fs_variables[i],
-                          instructions, symtab);
+      add_builtin_variable(instructions, state->symbols,
+                          & builtin_core_fs_variables[i]);
+   }
+
+   for (unsigned i = 0; i < Elements(builtin_110_fs_variables); i++) {
+      add_builtin_variable(instructions, state->symbols,
+                          & builtin_110_fs_variables[i]);
    }
 
    for (unsigned i = 0
           ; i < Elements(builtin_110_deprecated_fs_variables)
           ; i++) {
-      add_builtin_variable(& builtin_110_deprecated_fs_variables[i],
-                          instructions, symtab);
+      add_builtin_variable(instructions, state->symbols,
+                          & builtin_110_deprecated_fs_variables[i]);
    }
-   generate_110_uniforms(instructions, symtab);
-
-   /* FINISHME: The size of this array is implementation dependent based on the
-    * FINISHME: value of GL_MAX_TEXTURE_COORDS.  Every platform that supports
-    * FINISHME: GLSL sets GL_MAX_TEXTURE_COORDS to at least 4, so hard-code 4
-    * FINISHME: for now.
+   generate_110_uniforms(instructions, state);
+
+   /* From page 54 (page 60 of the PDF) of the GLSL 1.20 spec:
+    *
+    *     "As with all arrays, indices used to subscript gl_TexCoord must
+    *     either be an integral constant expressions, or this array must be
+    *     re-declared by the shader with a size. The size can be at most
+    *     gl_MaxTextureCoords. Using indexes close to 0 may aid the
+    *     implementation in preserving varying resources."
     */
    const glsl_type *const vec4_array_type =
-      glsl_type::get_array_instance(symtab, glsl_type::vec4_type, 4);
+      glsl_type::get_array_instance(glsl_type::vec4_type, 0);
 
-   add_variable("gl_TexCoord", ir_var_in, FRAG_ATTRIB_TEX0, vec4_array_type,
-               instructions, symtab);
+   add_variable(instructions, state->symbols,
+               "gl_TexCoord", vec4_array_type, ir_var_in, FRAG_ATTRIB_TEX0);
+
+   generate_ARB_draw_buffers_variables(instructions, state, false,
+                                      fragment_shader);
 }
 
 
 static void
-generate_ARB_draw_buffers_fs_variables(exec_list *instructions,
-                                      glsl_symbol_table *symtab, bool warn)
+generate_ARB_draw_buffers_variables(exec_list *instructions,
+                                   struct _mesa_glsl_parse_state *state,
+                                   bool warn, _mesa_glsl_parser_targets target)
 {
-   /* FINISHME: The size of this array is implementation dependent based on the
-    * FINISHME: value of GL_MAX_DRAW_BUFFERS.  GL_MAX_DRAW_BUFFERS must be
-    * FINISHME: at least 1, so hard-code 1 for now.
+   /* gl_MaxDrawBuffers is available in all shader stages.
     */
-   const glsl_type *const vec4_array_type =
-      glsl_type::get_array_instance(symtab, glsl_type::vec4_type, 1);
+   ir_variable *const mdb =
+      add_variable(instructions, state->symbols,
+                  "gl_MaxDrawBuffers", glsl_type::int_type, ir_var_auto, -1);
+
+   if (warn)
+      mdb->warn_extension = "GL_ARB_draw_buffers";
+
+   mdb->constant_value = new(mdb)
+      ir_constant(int(state->Const.MaxDrawBuffers));
+
+
+   /* gl_FragData is only available in the fragment shader.
+    */
+   if (target == fragment_shader) {
+      const glsl_type *const vec4_array_type =
+        glsl_type::get_array_instance(glsl_type::vec4_type,
+                                      state->Const.MaxDrawBuffers);
+
+      ir_variable *const fd =
+        add_variable(instructions, state->symbols,
+                     "gl_FragData", vec4_array_type,
+                     ir_var_out, FRAG_RESULT_DATA0);
+
+      if (warn)
+        fd->warn_extension = "GL_ARB_draw_buffers";
+   }
+}
+
 
+static void
+generate_ARB_draw_instanced_variables(exec_list *instructions,
+                                      struct _mesa_glsl_parse_state *state,
+                                      bool warn,
+                                      _mesa_glsl_parser_targets target)
+{
+   /* gl_InstanceIDARB is only available in the vertex shader.
+    */
+   if (target == vertex_shader) {
+      ir_variable *const inst =
+         add_variable(instructions, state->symbols,
+                     "gl_InstanceIDARB", glsl_type::int_type,
+                     ir_var_system_value, SYSTEM_VALUE_INSTANCE_ID);
+
+      if (warn)
+         inst->warn_extension = "GL_ARB_draw_instanced";
+   }
+}
+
+
+static void
+generate_ARB_shader_stencil_export_variables(exec_list *instructions,
+                                            struct _mesa_glsl_parse_state *state,
+                                            bool warn)
+{
+   /* gl_FragStencilRefARB is only available in the fragment shader.
+    */
    ir_variable *const fd =
-      add_variable("gl_FragData", ir_var_out, FRAG_RESULT_DATA0,
-                  vec4_array_type, instructions, symtab);
+      add_variable(instructions, state->symbols,
+                  "gl_FragStencilRefARB", glsl_type::int_type,
+                  ir_var_out, FRAG_RESULT_STENCIL);
 
    if (warn)
-      fd->warn_extension = "GL_ARB_draw_buffers";
+      fd->warn_extension = "GL_ARB_shader_stencil_export";
 }
 
+static void
+generate_AMD_shader_stencil_export_variables(exec_list *instructions,
+                                            struct _mesa_glsl_parse_state *state,
+                                            bool warn)
+{
+   /* gl_FragStencilRefAMD is only available in the fragment shader.
+    */
+   ir_variable *const fd =
+      add_variable(instructions, state->symbols,
+                  "gl_FragStencilRefAMD", glsl_type::int_type,
+                  ir_var_out, FRAG_RESULT_STENCIL);
+
+   if (warn)
+      fd->warn_extension = "GL_AMD_shader_stencil_export";
+}
 
 static void
 generate_120_fs_variables(exec_list *instructions,
-                         glsl_symbol_table *symtab)
+                         struct _mesa_glsl_parse_state *state)
 {
-   generate_110_fs_variables(instructions, symtab);
-   generate_ARB_draw_buffers_fs_variables(instructions, symtab, false);
+   generate_110_fs_variables(instructions, state);
+
+   for (unsigned i = 0
+          ; i < Elements(builtin_120_fs_variables)
+          ; i++) {
+      add_builtin_variable(instructions, state->symbols,
+                          & builtin_120_fs_variables[i]);
+   }
 }
 
 static void
 generate_130_fs_variables(exec_list *instructions,
-                         glsl_symbol_table *symtab)
+                         struct _mesa_glsl_parse_state *state)
 {
-   void *ctx = symtab;
-   generate_120_fs_variables(instructions, symtab);
-
-   /* FINISHME: The size of this array is implementation dependent based on
-    * FINISHME: the value of GL_MAX_CLIP_DISTANCES.
+   generate_120_fs_variables(instructions, state);
+
+   generate_130_uniforms(instructions, state);
+
+   /* From the GLSL 1.30 spec, section 7.2 (Fragment Shader Special
+    * Variables):
+    *
+    *   The built-in input variable gl_ClipDistance array contains linearly
+    *   interpolated values for the vertex values written by the vertex shader
+    *   to the gl_ClipDistance vertex output variable. This array must be
+    *   sized in the fragment shader either implicitly or explicitly to be the
+    *   same size as it was sized in the vertex shader.
+    *
+    * In other words, the array must be pre-declared as implicitly sized.  We
+    * represent this in Mesa by initially declaring the array as size 0.
     */
    const glsl_type *const clip_distance_array_type =
-      glsl_type::get_array_instance(ctx, glsl_type::float_type, 8);
+      glsl_type::get_array_instance(glsl_type::float_type, 0);
 
    /* FINISHME: gl_ClipDistance needs a real location assigned. */
-   add_variable("gl_ClipDistance", ir_var_in, -1, clip_distance_array_type,
-               instructions, symtab);
+   add_variable(instructions, state->symbols,
+               "gl_ClipDistance", clip_distance_array_type, ir_var_in, -1);
 }
 
 static void
@@ -301,27 +852,27 @@ initialize_fs_variables(exec_list *instructions,
 {
 
    switch (state->language_version) {
+   case 100:
+      generate_100ES_fs_variables(instructions, state);
+      break;
    case 110:
-      generate_110_fs_variables(instructions, state->symbols);
+      generate_110_fs_variables(instructions, state);
       break;
    case 120:
-      generate_120_fs_variables(instructions, state->symbols);
+      generate_120_fs_variables(instructions, state);
       break;
    case 130:
-      generate_130_fs_variables(instructions, state->symbols);
+      generate_130_fs_variables(instructions, state);
       break;
    }
 
+   if (state->ARB_shader_stencil_export_enable)
+      generate_ARB_shader_stencil_export_variables(instructions, state,
+                                                  state->ARB_shader_stencil_export_warn);
 
-   /* Since GL_ARB_draw_buffers is included in GLSL 1.20 and later, we
-    * can basically ignore any extension settings for it.
-    */
-   if (state->language_version < 120) {
-      if (state->ARB_draw_buffers_enable) {
-        generate_ARB_draw_buffers_fs_variables(instructions, state->symbols,
-                                               state->ARB_draw_buffers_warn);
-      }
-   }
+   if (state->AMD_shader_stencil_export_enable)
+      generate_AMD_shader_stencil_export_variables(instructions, state,
+                                                  state->AMD_shader_stencil_export_warn);
 }
 
 void
@@ -337,9 +888,5 @@ _mesa_glsl_initialize_variables(exec_list *instructions,
    case fragment_shader:
       initialize_fs_variables(instructions, state);
       break;
-   case ir_shader:
-      fprintf(stderr, "ir reader has no builtin variables");
-      exit(1);
-      break;
    }
 }