From a71d524ecad48837e0124a03124bc05f59a48be7 Mon Sep 17 00:00:00 2001
From: marha <marha@users.sourceforge.net>
Date: Thu, 30 Apr 2015 23:24:30 +0200
Subject: fontconfig pixman libX11 mesa git update 30 Apr 2015

libX11           commit d3415d1f052530760b4617db45affcb984cfe35c
pixman           commit e0c0153d8e5d42c08c2b9bd2cf2123bff2c48d75
fontconfig       commit 4a6f5efd5f6a468e1872d58e589bcf30ba88e2fd
mesa             commit 1ac7db07b363207e8ded9259f84bbcaa084b8667
---
 mesalib/src/gallium/SConscript                   |   5 +
 mesalib/src/gallium/auxiliary/Android.mk         |   5 +-
 mesalib/src/gallium/auxiliary/hud/hud_context.c  | 163 ++++++++++++++++++++++-
 mesalib/src/gallium/auxiliary/hud/hud_private.h  |   4 +
 mesalib/src/gallium/auxiliary/util/u_tile.c      |   4 +-
 mesalib/src/gallium/auxiliary/util/u_video.h     |  36 +++++
 mesalib/src/glsl/Android.gen.mk                  |  66 ++++++++-
 mesalib/src/glsl/Android.mk                      |  13 +-
 mesalib/src/glsl/ast_function.cpp                |   2 +-
 mesalib/src/glsl/ast_to_hir.cpp                  |   4 +-
 mesalib/src/glsl/glcpp/glcpp-parse.y             |   2 +-
 mesalib/src/glsl/glsl_types.cpp                  |  23 ++--
 mesalib/src/glsl/glsl_types.h                    |   7 +-
 mesalib/src/glsl/link_uniforms.cpp               |   2 +-
 mesalib/src/glsl/linker.cpp                      |   3 +-
 mesalib/src/glsl/lower_packed_varyings.cpp       |   8 +-
 mesalib/src/glsl/lower_packing_builtins.cpp      |   8 +-
 mesalib/src/glsl/nir/nir.c                       | 112 ++++++++++++----
 mesalib/src/glsl/nir/nir.h                       |  17 ++-
 mesalib/src/glsl/nir/nir_array.h                 |  96 +++++++++++++
 mesalib/src/glsl/nir/nir_from_ssa.c              |   2 +-
 mesalib/src/glsl/nir/nir_lower_locals_to_regs.c  | 148 +++++++++++++++++---
 mesalib/src/glsl/nir/nir_lower_to_source_mods.c  |   7 +
 mesalib/src/glsl/nir/nir_lower_var_copies.c      |  24 +---
 mesalib/src/glsl/nir/nir_lower_vars_to_ssa.c     | 120 +++--------------
 mesalib/src/glsl/nir/nir_opt_algebraic.py        |   1 +
 mesalib/src/glsl/nir/nir_print.c                 |   2 +
 mesalib/src/glsl/nir/nir_types.cpp               |   2 +-
 mesalib/src/glsl/opt_algebraic.cpp               |  20 +++
 mesalib/src/glsl/opt_dead_builtin_varyings.cpp   |   8 +-
 mesalib/src/glsl/opt_function_inlining.cpp       |   2 +-
 mesalib/src/glsl/standalone_scaffolding.cpp      |   3 -
 mesalib/src/loader/Android.mk                    |   2 +-
 mesalib/src/loader/loader.h                      |   2 +-
 mesalib/src/mapi/Android.mk                      |   2 +-
 mesalib/src/mapi/glapi/gen/glX_proto_send.py     |   2 +-
 mesalib/src/mapi/glapi/gen/glX_proto_size.py     |   2 +-
 mesalib/src/mapi/glapi/gen/gl_x86-64_asm.py      |   2 +-
 mesalib/src/mapi/glapi/glapi_entrypoint.c        |   2 +
 mesalib/src/mesa/Android.gen.mk                  |  23 +++-
 mesalib/src/mesa/Android.libmesa_dricore.mk      |  15 +--
 mesalib/src/mesa/Android.libmesa_glsl_utils.mk   |  10 +-
 mesalib/src/mesa/Android.libmesa_st_mesa.mk      |   9 +-
 mesalib/src/mesa/Android.mesa_gen_matypes.mk     |   1 -
 mesalib/src/mesa/drivers/common/meta.c           |   1 -
 mesalib/src/mesa/drivers/dri/Android.mk          |  11 +-
 mesalib/src/mesa/drivers/dri/common/Android.mk   |  38 ++++--
 mesalib/src/mesa/drivers/dri/common/drirc        |   4 +
 mesalib/src/mesa/main/compiler.h                 |   5 -
 mesalib/src/mesa/main/context.c                  |   6 +-
 mesalib/src/mesa/main/extensions.c               |   2 +
 mesalib/src/mesa/main/fbobject.c                 |   9 +-
 mesalib/src/mesa/main/ffvertex_prog.c            |   2 +-
 mesalib/src/mesa/main/formats.c                  |   2 +-
 mesalib/src/mesa/main/macros.h                   |   5 +-
 mesalib/src/mesa/main/mtypes.h                   |  13 --
 mesalib/src/mesa/main/shader_query.cpp           |  56 ++++----
 mesalib/src/mesa/main/shaderapi.c                |   9 --
 mesalib/src/mesa/main/texgetimage.c              |  54 +++++++-
 mesalib/src/mesa/main/teximage.c                 |   7 +-
 mesalib/src/mesa/main/texobj.c                   |   1 -
 mesalib/src/mesa/main/texturebarrier.c           |   6 +
 mesalib/src/mesa/main/uniform_query.cpp          |  20 ++-
 mesalib/src/mesa/main/version.c                  |  63 ++++++++-
 mesalib/src/mesa/math/m_matrix.c                 |   2 +-
 mesalib/src/mesa/program/Android.mk              |  14 +-
 mesalib/src/mesa/program/hash_table.h            |   4 +-
 mesalib/src/mesa/program/prog_cache.c            |   2 +-
 mesalib/src/mesa/program/prog_to_nir.c           |  25 +++-
 mesalib/src/mesa/state_tracker/st_atom_texture.c |  14 +-
 mesalib/src/mesa/state_tracker/st_cb_flush.c     |   2 +-
 mesalib/src/mesa/state_tracker/st_cb_texture.c   |   2 +-
 mesalib/src/mesa/state_tracker/st_extensions.c   |   9 +-
 mesalib/src/mesa/state_tracker/st_format.c       |  19 ++-
 mesalib/src/mesa/swrast/s_texcombine.c           |   2 +-
 mesalib/src/mesa/tnl/t_vertex.h                  |   2 +-
 mesalib/src/mesa/vbo/vbo_exec_api.c              |   2 +-
 mesalib/src/mesa/vbo/vbo_save_draw.c             |   2 +-
 mesalib/src/util/Android.mk                      |  42 +++---
 mesalib/src/util/macros.h                        |   2 +
 80 files changed, 1034 insertions(+), 416 deletions(-)
 create mode 100644 mesalib/src/glsl/nir/nir_array.h

(limited to 'mesalib/src')

diff --git a/mesalib/src/gallium/SConscript b/mesalib/src/gallium/SConscript
index 680ad925f..eeb1c780f 100644
--- a/mesalib/src/gallium/SConscript
+++ b/mesalib/src/gallium/SConscript
@@ -60,6 +60,11 @@ SConscript([
 ])
 
 if not env['embedded']:
+    SConscript([
+        'state_trackers/osmesa/SConscript',
+        'targets/osmesa/SConscript',
+    ])
+
     if env['x11']:
         SConscript([
             'state_trackers/glx/xlib/SConscript',
diff --git a/mesalib/src/gallium/auxiliary/Android.mk b/mesalib/src/gallium/auxiliary/Android.mk
index 0bc183170..96a2125de 100644
--- a/mesalib/src/gallium/auxiliary/Android.mk
+++ b/mesalib/src/gallium/auxiliary/Android.mk
@@ -33,14 +33,13 @@ LOCAL_SRC_FILES := \
 	$(VL_STUB_SOURCES)
 
 LOCAL_C_INCLUDES := \
-	$(GALLIUM_TOP)/auxiliary/util \
-	$(MESA_TOP)/src
+	$(GALLIUM_TOP)/auxiliary/util
 
 LOCAL_MODULE := libmesa_gallium
 
 # generate sources
 LOCAL_MODULE_CLASS := STATIC_LIBRARIES
-intermediates := $(call local-intermediates-dir)
+intermediates := $(call local-generated-sources-dir)
 LOCAL_GENERATED_SOURCES := $(addprefix $(intermediates)/, $(GENERATED_SOURCES))
 
 $(LOCAL_GENERATED_SOURCES): PRIVATE_PYTHON := $(MESA_PYTHON2)
diff --git a/mesalib/src/gallium/auxiliary/hud/hud_context.c b/mesalib/src/gallium/auxiliary/hud/hud_context.c
index e46c68cdd..00ec20589 100644
--- a/mesalib/src/gallium/auxiliary/hud/hud_context.c
+++ b/mesalib/src/gallium/auxiliary/hud/hud_context.c
@@ -569,9 +569,36 @@ hud_pane_set_max_value(struct hud_pane *pane, uint64_t value)
    pane->yscale = -(int)pane->inner_height / (float)pane->max_value;
 }
 
+static void
+hud_pane_update_dyn_ceiling(struct hud_graph *gr, struct hud_pane *pane)
+{
+   unsigned i;
+   float tmp = 0.0f;
+
+   if (pane->dyn_ceil_last_ran != gr->index) {
+      LIST_FOR_EACH_ENTRY(gr, &pane->graph_list, head) {
+         for (i = 0; i <  gr->num_vertices; ++i) {
+            tmp = gr->vertices[i * 2 + 1] > tmp ?
+                  gr->vertices[i * 2 + 1] : tmp;
+         }
+      }
+
+      /* Avoid setting it lower than the initial starting height. */
+      tmp = tmp > pane->initial_max_value ? tmp : pane->initial_max_value;
+      hud_pane_set_max_value(pane, tmp);
+   }
+
+   /*
+    * Mark this adjustment run so we could avoid repeating a full update
+    * again needlessly in case the pane has more than one graph.
+    */
+   pane->dyn_ceil_last_ran = gr->index;
+}
+
 static struct hud_pane *
 hud_pane_create(unsigned x1, unsigned y1, unsigned x2, unsigned y2,
-                unsigned period, uint64_t max_value)
+                unsigned period, uint64_t max_value, uint64_t ceiling,
+                boolean dyn_ceiling)
 {
    struct hud_pane *pane = CALLOC_STRUCT(hud_pane);
 
@@ -590,6 +617,10 @@ hud_pane_create(unsigned x1, unsigned y1, unsigned x2, unsigned y2,
    pane->inner_height = pane->inner_y2 - pane->inner_y1;
    pane->period = period;
    pane->max_num_vertices = (x2 - x1 + 2) / 2;
+   pane->ceiling = ceiling;
+   pane->dyn_ceiling = dyn_ceiling;
+   pane->dyn_ceil_last_ran = 0;
+   pane->initial_max_value = max_value;
    hud_pane_set_max_value(pane, max_value);
    LIST_INITHEAD(&pane->graph_list);
    return pane;
@@ -633,6 +664,9 @@ hud_pane_add_graph(struct hud_pane *pane, struct hud_graph *gr)
 void
 hud_graph_add_value(struct hud_graph *gr, uint64_t value)
 {
+   gr->current_value = value;
+   value = value > gr->pane->ceiling ? gr->pane->ceiling : value;
+
    if (gr->index == gr->pane->max_num_vertices) {
       gr->vertices[0] = 0;
       gr->vertices[1] = gr->vertices[(gr->index-1)*2+1];
@@ -646,7 +680,9 @@ hud_graph_add_value(struct hud_graph *gr, uint64_t value)
       gr->num_vertices++;
    }
 
-   gr->current_value = value;
+   if (gr->pane->dyn_ceiling == true) {
+      hud_pane_update_dyn_ceiling(gr, gr->pane);
+   }
    if (value > gr->pane->max_value) {
       hud_pane_set_max_value(gr->pane, value);
    }
@@ -683,6 +719,69 @@ parse_string(const char *s, char *out)
    return i;
 }
 
+static char *
+read_pane_settings(char *str, unsigned * const x, unsigned * const y,
+               unsigned * const width, unsigned * const height,
+               uint64_t * const ceiling, boolean * const dyn_ceiling)
+{
+   char *ret = str;
+   unsigned tmp;
+
+   while (*str == '.') {
+      ++str;
+      switch (*str) {
+      case 'x':
+         ++str;
+         *x = strtoul(str, &ret, 10);
+         str = ret;
+         break;
+
+      case 'y':
+         ++str;
+         *y = strtoul(str, &ret, 10);
+         str = ret;
+         break;
+
+      case 'w':
+         ++str;
+         tmp = strtoul(str, &ret, 10);
+         *width = tmp > 80 ? tmp : 80; /* 80 is chosen arbitrarily */
+         str = ret;
+         break;
+
+      /*
+       * Prevent setting height to less than 50. If the height is set to less,
+       * the text of the Y axis labels on the graph will start overlapping.
+       */
+      case 'h':
+         ++str;
+         tmp = strtoul(str, &ret, 10);
+         *height = tmp > 50 ? tmp : 50;
+         str = ret;
+         break;
+
+      case 'c':
+         ++str;
+         tmp = strtoul(str, &ret, 10);
+         *ceiling = tmp > 10 ? tmp : 10;
+         str = ret;
+         break;
+
+      case 'd':
+         ++str;
+         ret = str;
+         *dyn_ceiling = true;
+         break;
+
+      default:
+         fprintf(stderr, "gallium_hud: syntax error: unexpected '%c'\n", *str);
+      }
+
+   }
+
+   return ret;
+}
+
 static boolean
 has_occlusion_query(struct pipe_screen *screen)
 {
@@ -705,11 +804,15 @@ static void
 hud_parse_env_var(struct hud_context *hud, const char *env)
 {
    unsigned num, i;
-   char name[256], s[256];
+   char name_a[256], s[256];
+   char *name;
    struct hud_pane *pane = NULL;
    unsigned x = 10, y = 10;
    unsigned width = 251, height = 100;
    unsigned period = 500 * 1000;  /* default period (1/2 second) */
+   uint64_t ceiling = UINT64_MAX;
+   unsigned column_width = 251;
+   boolean dyn_ceiling = false;
    const char *period_env;
 
    /*
@@ -725,11 +828,23 @@ hud_parse_env_var(struct hud_context *hud, const char *env)
       }
    }
 
-   while ((num = parse_string(env, name)) != 0) {
+   while ((num = parse_string(env, name_a)) != 0) {
       env += num;
 
+      /* check for explicit location, size and etc. settings */
+      name = read_pane_settings(name_a, &x, &y, &width, &height, &ceiling,
+                         &dyn_ceiling);
+
+     /*
+      * Keep track of overall column width to avoid pane overlapping in case
+      * later we create a new column while the bottom pane in the current
+      * column is less wide than the rest of the panes in it.
+      */
+     column_width = width > column_width ? width : column_width;
+
       if (!pane) {
-         pane = hud_pane_create(x, y, x + width, y + height, period, 10);
+         pane = hud_pane_create(x, y, x + width, y + height, period, 10,
+                         ceiling, dyn_ceiling);
          if (!pane)
             return;
       }
@@ -807,6 +922,7 @@ hud_parse_env_var(struct hud_context *hud, const char *env)
 
          if (num && sscanf(s, "%u", &i) == 1) {
             hud_pane_set_max_value(pane, i);
+            pane->initial_max_value = i;
          }
          else {
             fprintf(stderr, "gallium_hud: syntax error: unexpected '%c' (%i) "
@@ -826,6 +942,7 @@ hud_parse_env_var(struct hud_context *hud, const char *env)
       case ',':
          env++;
          y += height + hud->font.glyph_height * (pane->num_graphs + 2);
+         height = 100;
 
          if (pane && pane->num_graphs) {
             LIST_ADDTAIL(&pane->head, &hud->pane_list);
@@ -836,17 +953,27 @@ hud_parse_env_var(struct hud_context *hud, const char *env)
       case ';':
          env++;
          y = 10;
-         x += width + hud->font.glyph_width * 7;
+         x += column_width + hud->font.glyph_width * 7;
+         height = 100;
 
          if (pane && pane->num_graphs) {
             LIST_ADDTAIL(&pane->head, &hud->pane_list);
             pane = NULL;
          }
+
+         /* Starting a new column; reset column width. */
+         column_width = 251;
          break;
 
       default:
          fprintf(stderr, "gallium_hud: syntax error: unexpected '%c'\n", *env);
       }
+
+      /* Reset to defaults for the next pane in case these were modified. */
+      width = 251;
+      ceiling = UINT64_MAX;
+      dyn_ceiling = false;
+
    }
 
    if (pane) {
@@ -878,6 +1005,30 @@ print_help(struct pipe_screen *screen)
    puts("");
    puts("  Example: GALLIUM_HUD=\"cpu,fps;primitives-generated\"");
    puts("");
+   puts("  Additionally, by prepending '.[identifier][value]' modifiers to");
+   puts("  a name, it is possible to explicitly set the location and size");
+   puts("  of a pane, along with limiting overall maximum value of the");
+   puts("  Y axis and activating dynamic readjustment of the Y axis.");
+   puts("  Several modifiers may be applied to the same pane simultaneously.");
+   puts("");
+   puts("  'x[value]' sets the location of the pane on the x axis relative");
+   puts("             to the upper-left corner of the viewport, in pixels.");
+   puts("  'y[value]' sets the location of the pane on the y axis relative");
+   puts("             to the upper-left corner of the viewport, in pixels.");
+   puts("  'w[value]' sets width of the graph pixels.");
+   puts("  'h[value]' sets height of the graph in pixels.");
+   puts("  'c[value]' sets the ceiling of the value of the Y axis.");
+   puts("             If the graph needs to draw values higher than");
+   puts("             the ceiling allows, the value is clamped.");
+   puts("  'd' activates dynamic Y axis readjustment to set the value of");
+   puts("      the Y axis to match the highest value still visible in the graph.");
+   puts("");
+   puts("  If 'c' and 'd' modifiers are used simultaneously, both are in effect:");
+   puts("  the Y axis does not go above the restriction imposed by 'c' while");
+   puts("  still adjusting the value of the Y axis down when appropriate.");
+   puts("");
+   puts("  Example: GALLIUM_HUD=\".w256.h64.x1600.y520.d.c1000fps+cpu,.datom-count\"");
+   puts("");
    puts("  Available names:");
    puts("    fps");
    puts("    cpu");
diff --git a/mesalib/src/gallium/auxiliary/hud/hud_private.h b/mesalib/src/gallium/auxiliary/hud/hud_private.h
index 1606ada4a..230f02664 100644
--- a/mesalib/src/gallium/auxiliary/hud/hud_private.h
+++ b/mesalib/src/gallium/auxiliary/hud/hud_private.h
@@ -62,6 +62,10 @@ struct hud_pane {
    float yscale;
    unsigned max_num_vertices;
    uint64_t max_value;
+   uint64_t initial_max_value;
+   uint64_t ceiling;
+   unsigned dyn_ceil_last_ran;
+   boolean dyn_ceiling;
    boolean uses_byte_units;
    uint64_t period; /* in microseconds */
 
diff --git a/mesalib/src/gallium/auxiliary/util/u_tile.c b/mesalib/src/gallium/auxiliary/util/u_tile.c
index 8e199200f..b91bb413a 100644
--- a/mesalib/src/gallium/auxiliary/util/u_tile.c
+++ b/mesalib/src/gallium/auxiliary/util/u_tile.c
@@ -341,13 +341,13 @@ x32_s8_get_tile_rgba(const unsigned *src,
    unsigned i, j;
 
    for (i = 0; i < h; i++) {
-      float *pRow = p;
+      uint32_t *pRow = (uint32_t *)p;
       for (j = 0; j < w; j++, pRow += 4) {
          src++;
          pRow[0] =
          pRow[1] =
          pRow[2] =
-         pRow[3] = (float)(*src++ & 0xff);
+         pRow[3] = (*src++ & 0xff);
       }
       p += dst_stride;
    }
diff --git a/mesalib/src/gallium/auxiliary/util/u_video.h b/mesalib/src/gallium/auxiliary/util/u_video.h
index 45b2d6e76..b4743d13f 100644
--- a/mesalib/src/gallium/auxiliary/util/u_video.h
+++ b/mesalib/src/gallium/auxiliary/util/u_video.h
@@ -38,6 +38,7 @@ extern "C" {
 /* u_reduce_video_profile() needs these */
 #include "pipe/p_compiler.h"
 #include "util/u_debug.h"
+#include "util/u_math.h"
 
 static INLINE enum pipe_video_format
 u_reduce_video_profile(enum pipe_video_profile profile)
@@ -146,6 +147,41 @@ u_copy_swap422_packed(void *const *destination_data,
    }
 }
 
+static INLINE uint32_t
+u_get_h264_level(uint32_t width, uint32_t height, uint32_t *max_reference)
+{
+   uint32_t max_dpb_mbs;
+
+   width = align(width, 16);
+   height = align(height, 16);
+
+   /* Max references will be used for caculation of number of DPB buffers
+      in the UVD driver, limitation of max references is 16. Some client
+      like mpv application for VA-API, it requires references more than that,
+      so we have to set max of references to 16 here. */
+   *max_reference = MIN2(*max_reference, 16);
+   max_dpb_mbs = (width / 16) * (height / 16) * *max_reference;
+
+   /* The calculation is based on "Decoded picture buffering" section
+      from http://en.wikipedia.org/wiki/H.264/MPEG-4_AVC */
+   if (max_dpb_mbs <= 8100)
+      return 30;
+   else if (max_dpb_mbs <= 18000)
+      return 31;
+   else if (max_dpb_mbs <= 20480)
+      return 32;
+   else if (max_dpb_mbs <= 32768)
+      return 41;
+   else if (max_dpb_mbs <= 34816)
+      return 42;
+   else if (max_dpb_mbs <= 110400)
+      return 50;
+   else if (max_dpb_mbs <= 184320)
+      return 51;
+   else
+      return 52;
+}
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/mesalib/src/glsl/Android.gen.mk b/mesalib/src/glsl/Android.gen.mk
index 7ec56d4f2..0835871f9 100644
--- a/mesalib/src/glsl/Android.gen.mk
+++ b/mesalib/src/glsl/Android.gen.mk
@@ -27,17 +27,29 @@ ifeq ($(LOCAL_MODULE_CLASS),)
 LOCAL_MODULE_CLASS := STATIC_LIBRARIES
 endif
 
-intermediates := $(call local-intermediates-dir)
+intermediates := $(call local-generated-sources-dir)
 
 sources := \
 	glsl_lexer.cpp \
 	glsl_parser.cpp \
 	glcpp/glcpp-lex.c \
-	glcpp/glcpp-parse.c
+	glcpp/glcpp-parse.c \
+	nir/nir_builder_opcodes.h \
+	nir/nir_constant_expressions.c \
+	nir/nir_opcodes.c \
+	nir/nir_opcodes.h \
+	nir/nir_opt_algebraic.c
 
 LOCAL_SRC_FILES := $(filter-out $(sources), $(LOCAL_SRC_FILES))
 
-LOCAL_C_INCLUDES += $(intermediates) $(intermediates)/glcpp $(MESA_TOP)/src/glsl/glcpp
+LOCAL_C_INCLUDES += \
+	$(intermediates)/glcpp \
+	$(intermediates)/nir \
+	$(MESA_TOP)/src/glsl/glcpp \
+	$(MESA_TOP)/src/glsl/nir
+
+LOCAL_EXPORT_C_INCLUDE_DIRS += \
+	$(intermediates)/nir
 
 sources := $(addprefix $(intermediates)/, $(sources))
 LOCAL_GENERATED_SOURCES += $(sources)
@@ -77,3 +89,51 @@ $(intermediates)/glcpp/glcpp-lex.c: $(LOCAL_PATH)/glcpp/glcpp-lex.l
 
 $(intermediates)/glcpp/glcpp-parse.c: $(LOCAL_PATH)/glcpp/glcpp-parse.y
 	$(call glsl_local-y-to-c-and-h)
+
+nir_builder_opcodes_gen := $(LOCAL_PATH)/nir/nir_builder_opcodes_h.py
+nir_builder_opcodes_deps := \
+	$(LOCAL_PATH)/nir/nir_opcodes.py \
+	$(LOCAL_PATH)/nir/nir_builder_opcodes_h.py
+
+$(intermediates)/nir/nir_builder_opcodes.h: $(nir_builder_opcodes_deps)
+	@mkdir -p $(dir $@)
+	$(hide) $(MESA_PYTHON2) $(nir_builder_opcodes_gen) $< > $@
+
+nir_constant_expressions_gen := $(LOCAL_PATH)/nir/nir_constant_expressions.py
+nir_constant_expressions_deps := \
+	$(LOCAL_PATH)/nir/nir_opcodes.py \
+	$(LOCAL_PATH)/nir/nir_constant_expressions.py \
+	$(LOCAL_PATH)/nir/nir_constant_expressions.h
+
+$(intermediates)/nir/nir_constant_expressions.c: $(nir_constant_expressions_deps)
+	@mkdir -p $(dir $@)
+	$(hide) $(MESA_PYTHON2) $(nir_constant_expressions_gen) $< > $@
+
+nir_opcodes_h_gen := $(LOCAL_PATH)/nir/nir_opcodes_h.py
+nir_opcodes_h_deps := \
+	$(LOCAL_PATH)/nir/nir_opcodes.py \
+	$(LOCAL_PATH)/nir/nir_opcodes_h.py
+
+$(intermediates)/nir/nir_opcodes.h: $(nir_opcodes_h_deps)
+	@mkdir -p $(dir $@)
+	$(hide) $(MESA_PYTHON2) $(nir_opcodes_h_gen) $< > $@
+
+$(LOCAL_PATH)/nir/nir.h: $(intermediates)/nir/nir_opcodes.h
+
+nir_opcodes_c_gen := $(LOCAL_PATH)/nir/nir_opcodes_c.py
+nir_opcodes_c_deps := \
+	$(LOCAL_PATH)/nir/nir_opcodes.py \
+	$(LOCAL_PATH)/nir/nir_opcodes_c.py
+
+$(intermediates)/nir/nir_opcodes.c: $(nir_opcodes_c_deps)
+	@mkdir -p $(dir $@)
+	$(hide) $(MESA_PYTHON2) $(nir_opcodes_c_gen) $< > $@
+
+nir_opt_algebraic_gen := $(LOCAL_PATH)/nir/nir_opt_algebraic.py
+nir_opt_algebraic_deps := \
+	$(LOCAL_PATH)/nir/nir_opt_algebraic.py \
+	$(LOCAL_PATH)/nir/nir_algebraic.py
+
+$(intermediates)/nir/nir_opt_algebraic.c: $(nir_opt_algebraic_deps)
+	@mkdir -p $(dir $@)
+	$(hide) $(MESA_PYTHON2) $(nir_opt_algebraic_gen) $< > $@
diff --git a/mesalib/src/glsl/Android.mk b/mesalib/src/glsl/Android.mk
index 38c2087a4..f20741e0d 100644
--- a/mesalib/src/glsl/Android.mk
+++ b/mesalib/src/glsl/Android.mk
@@ -35,12 +35,14 @@ include $(CLEAR_VARS)
 
 LOCAL_SRC_FILES := \
 	$(LIBGLCPP_FILES) \
-	$(LIBGLSL_FILES)
+	$(LIBGLSL_FILES) \
+	$(NIR_FILES)
 
 LOCAL_C_INCLUDES := \
-	$(MESA_TOP)/src \
 	$(MESA_TOP)/src/mapi \
-	$(MESA_TOP)/src/mesa
+	$(MESA_TOP)/src/mesa \
+	$(MESA_TOP)/src/gallium/include \
+	$(MESA_TOP)/src/gallium/auxiliary
 
 LOCAL_MODULE := libmesa_glsl
 
@@ -59,9 +61,10 @@ LOCAL_SRC_FILES := \
 	$(GLSL_COMPILER_CXX_FILES)
 
 LOCAL_C_INCLUDES := \
-	$(MESA_TOP)/src \
 	$(MESA_TOP)/src/mapi \
-	$(MESA_TOP)/src/mesa
+	$(MESA_TOP)/src/mesa \
+	$(MESA_TOP)/src/gallium/include \
+	$(MESA_TOP)/src/gallium/auxiliary
 
 LOCAL_STATIC_LIBRARIES := libmesa_glsl libmesa_glsl_utils libmesa_util
 
diff --git a/mesalib/src/glsl/ast_function.cpp b/mesalib/src/glsl/ast_function.cpp
index 87df93e68..758361324 100644
--- a/mesalib/src/glsl/ast_function.cpp
+++ b/mesalib/src/glsl/ast_function.cpp
@@ -1191,7 +1191,7 @@ emit_inline_matrix_constructor(const glsl_type *type,
     *
     *  - Construct a matrix from an arbirary combination of vectors and
     *    scalars.  The components of the constructor parameters are assigned
-    *    to the matrix in colum-major order until the matrix is full.
+    *    to the matrix in column-major order until the matrix is full.
     *
     *  - Construct a matrix from a single matrix.  The source matrix is copied
     *    to the upper left portion of the constructed matrix, and the remaining
diff --git a/mesalib/src/glsl/ast_to_hir.cpp b/mesalib/src/glsl/ast_to_hir.cpp
index 78369360f..18b82e3be 100644
--- a/mesalib/src/glsl/ast_to_hir.cpp
+++ b/mesalib/src/glsl/ast_to_hir.cpp
@@ -3532,9 +3532,7 @@ ast_declarator_list::hir(exec_list *instructions,
              *    vectors. Vertex shader inputs cannot be arrays or
              *    structures."
              */
-            const glsl_type *check_type = var->type;
-            while (check_type->is_array())
-               check_type = check_type->element_type();
+            const glsl_type *check_type = var->type->without_array();
 
             switch (check_type->base_type) {
             case GLSL_TYPE_FLOAT:
diff --git a/mesalib/src/glsl/glcpp/glcpp-parse.y b/mesalib/src/glsl/glcpp/glcpp-parse.y
index c2f5223a9..cfceca66b 100644
--- a/mesalib/src/glsl/glcpp/glcpp-parse.y
+++ b/mesalib/src/glsl/glcpp/glcpp-parse.y
@@ -1463,7 +1463,7 @@ _arguments_parse (argument_list_t *arguments,
 		else {
 			if (argument->head == NULL) {
 				/* Don't treat initial whitespace as
-				 * part of the arguement. */
+				 * part of the argument. */
 				if (node->token->type == SPACE)
 					continue;
 			}
diff --git a/mesalib/src/glsl/glsl_types.cpp b/mesalib/src/glsl/glsl_types.cpp
index 4aa36a794..9c9b7efcb 100644
--- a/mesalib/src/glsl/glsl_types.cpp
+++ b/mesalib/src/glsl/glsl_types.cpp
@@ -738,24 +738,27 @@ glsl_type::record_key_compare(const void *a, const void *b)
 }
 
 
+/**
+ * Generate an integer hash value for a glsl_type structure type.
+ */
 unsigned
 glsl_type::record_key_hash(const void *a)
 {
    const glsl_type *const key = (glsl_type *) a;
-   char hash_key[128];
-   unsigned size = 0;
-
-   size = snprintf(hash_key, sizeof(hash_key), "%08x", key->length);
+   uintptr_t hash = key->length;
+   unsigned retval;
 
    for (unsigned i = 0; i < key->length; i++) {
-      if (size >= sizeof(hash_key))
-	 break;
-
-      size += snprintf(& hash_key[size], sizeof(hash_key) - size,
-		       "%p", (void *) key->fields.structure[i].type);
+      /* casting pointer to uintptr_t */
+      hash = (hash * 13 ) + (uintptr_t) key->fields.structure[i].type;
    }
 
-   return hash_table_string_hash(& hash_key);
+   if (sizeof(hash) == 8)
+      retval = (hash & 0xffffffff) ^ ((uint64_t) hash >> 32);
+   else
+      retval = hash;
+
+   return retval;
 }
 
 
diff --git a/mesalib/src/glsl/glsl_types.h b/mesalib/src/glsl/glsl_types.h
index d383dd5be..5645dcd50 100644
--- a/mesalib/src/glsl/glsl_types.h
+++ b/mesalib/src/glsl/glsl_types.h
@@ -540,7 +540,12 @@ struct glsl_type {
     */
    const glsl_type *without_array() const
    {
-      return this->is_array() ? this->fields.array : this;
+      const glsl_type *t = this;
+
+      while (t->is_array())
+         t = t->fields.array;
+
+      return t;
    }
 
    /**
diff --git a/mesalib/src/glsl/link_uniforms.cpp b/mesalib/src/glsl/link_uniforms.cpp
index 59adc298b..d5ca23a38 100644
--- a/mesalib/src/glsl/link_uniforms.cpp
+++ b/mesalib/src/glsl/link_uniforms.cpp
@@ -274,7 +274,7 @@ namespace {
  *
  * As uniforms are added to the active set the number of active uniforms and
  * the storage requirements for those uniforms are accumulated.  The active
- * uniforms are added the the hash table supplied to the constructor.
+ * uniforms are added to the hash table supplied to the constructor.
  *
  * If the same uniform is added multiple times (i.e., once for each shader
  * target), it will only be accounted once.
diff --git a/mesalib/src/glsl/linker.cpp b/mesalib/src/glsl/linker.cpp
index b6baa5d36..21fde9444 100644
--- a/mesalib/src/glsl/linker.cpp
+++ b/mesalib/src/glsl/linker.cpp
@@ -2570,7 +2570,8 @@ add_interface_variables(struct gl_shader_program *shProg,
          if (var->data.location != SYSTEM_VALUE_VERTEX_ID &&
              var->data.location != SYSTEM_VALUE_VERTEX_ID_ZERO_BASE &&
              var->data.location != SYSTEM_VALUE_INSTANCE_ID)
-         continue;
+            continue;
+         /* FALLTHROUGH */
       case ir_var_shader_in:
          if (programInterface != GL_PROGRAM_INPUT)
             continue;
diff --git a/mesalib/src/glsl/lower_packed_varyings.cpp b/mesalib/src/glsl/lower_packed_varyings.cpp
index 2c9a1c495..d8bebb522 100644
--- a/mesalib/src/glsl/lower_packed_varyings.cpp
+++ b/mesalib/src/glsl/lower_packed_varyings.cpp
@@ -645,13 +645,7 @@ lower_packed_varyings_visitor::needs_lowering(ir_variable *var)
    if (var->data.explicit_location)
       return false;
 
-   const glsl_type *type = var->type;
-   if (this->gs_input_vertices != 0) {
-      assert(type->is_array());
-      type = type->element_type();
-   }
-   if (type->is_array())
-      type = type->fields.array;
+   const glsl_type *type = var->type->without_array();
    if (type->vector_elements == 4 && !type->is_double())
       return false;
    return true;
diff --git a/mesalib/src/glsl/lower_packing_builtins.cpp b/mesalib/src/glsl/lower_packing_builtins.cpp
index db73c7b0f..a6fb8a883 100644
--- a/mesalib/src/glsl/lower_packing_builtins.cpp
+++ b/mesalib/src/glsl/lower_packing_builtins.cpp
@@ -569,8 +569,8 @@ private:
        *     return pack_uvec2_to_uint(uvec2(
        *                round(clamp(VEC2_RVALUE, 0.0f, 1.0f) * 65535.0f)));
        *
-       * Here it is safe to directly convert the vec2 to uvec2 because the the
-       * vec2 has been clamped to a non-negative range.
+       * Here it is safe to directly convert the vec2 to uvec2 because the vec2
+       * has been clamped to a non-negative range.
        */
 
       assert(vec2_rval->type == glsl_type::vec2_type);
@@ -613,8 +613,8 @@ private:
        *     return pack_uvec4_to_uint(uvec4(
        *                round(clamp(VEC2_RVALUE, 0.0f, 1.0f) * 255.0f)));
        *
-       * Here it is safe to directly convert the vec4 to uvec4 because the the
-       * vec4 has been clamped to a non-negative range.
+       * Here it is safe to directly convert the vec4 to uvec4 because the vec4
+       * has been clamped to a non-negative range.
        */
 
       assert(vec4_rval->type == glsl_type::vec4_type);
diff --git a/mesalib/src/glsl/nir/nir.c b/mesalib/src/glsl/nir/nir.c
index c6e53612b..4cc074b80 100644
--- a/mesalib/src/glsl/nir/nir.c
+++ b/mesalib/src/glsl/nir/nir.c
@@ -589,6 +589,66 @@ nir_copy_deref(void *mem_ctx, nir_deref *deref)
    return NULL;
 }
 
+/* Returns a load_const instruction that represents the constant
+ * initializer for the given deref chain.  The caller is responsible for
+ * ensuring that there actually is a constant initializer.
+ */
+nir_load_const_instr *
+nir_deref_get_const_initializer_load(nir_shader *shader, nir_deref_var *deref)
+{
+   nir_constant *constant = deref->var->constant_initializer;
+   assert(constant);
+
+   const nir_deref *tail = &deref->deref;
+   unsigned matrix_offset = 0;
+   while (tail->child) {
+      switch (tail->child->deref_type) {
+      case nir_deref_type_array: {
+         nir_deref_array *arr = nir_deref_as_array(tail->child);
+         assert(arr->deref_array_type == nir_deref_array_type_direct);
+         if (glsl_type_is_matrix(tail->type)) {
+            assert(arr->deref.child == NULL);
+            matrix_offset = arr->base_offset;
+         } else {
+            constant = constant->elements[arr->base_offset];
+         }
+         break;
+      }
+
+      case nir_deref_type_struct: {
+         constant = constant->elements[nir_deref_as_struct(tail->child)->index];
+         break;
+      }
+
+      default:
+         unreachable("Invalid deref child type");
+      }
+
+      tail = tail->child;
+   }
+
+   nir_load_const_instr *load =
+      nir_load_const_instr_create(shader, glsl_get_vector_elements(tail->type));
+
+   matrix_offset *= load->def.num_components;
+   for (unsigned i = 0; i < load->def.num_components; i++) {
+      switch (glsl_get_base_type(tail->type)) {
+      case GLSL_TYPE_FLOAT:
+      case GLSL_TYPE_INT:
+      case GLSL_TYPE_UINT:
+         load->value.u[i] = constant->value.u[matrix_offset + i];
+         break;
+      case GLSL_TYPE_BOOL:
+         load->value.u[i] = constant->value.b[matrix_offset + i] ?
+                             NIR_TRUE : NIR_FALSE;
+         break;
+      default:
+         unreachable("Invalid immediate type");
+      }
+   }
+
+   return load;
+}
 
 /**
  * \name Control flow modification
@@ -1800,33 +1860,37 @@ src_does_not_use_reg(nir_src *src, void *void_reg)
 void
 nir_instr_rewrite_src(nir_instr *instr, nir_src *src, nir_src new_src)
 {
-   if (src->is_ssa) {
-      nir_ssa_def *old_ssa = src->ssa;
-      *src = new_src;
-      if (old_ssa && nir_foreach_src(instr, src_does_not_use_def, old_ssa)) {
-         struct set_entry *entry = _mesa_set_search(old_ssa->uses, instr);
-         assert(entry);
-         _mesa_set_remove(old_ssa->uses, entry);
-      }
-   } else {
-      if (src->reg.indirect)
-         nir_instr_rewrite_src(instr, src->reg.indirect, new_src);
-
-      nir_register *old_reg = src->reg.reg;
-      *src = new_src;
-      if (old_reg && nir_foreach_src(instr, src_does_not_use_reg, old_reg)) {
-         struct set_entry *entry = _mesa_set_search(old_reg->uses, instr);
-         assert(entry);
-         _mesa_set_remove(old_reg->uses, entry);
+   nir_src old_src = *src;
+   *src = new_src;
+
+   for (nir_src *iter_src = &old_src; iter_src;
+        iter_src = iter_src->is_ssa ? NULL : iter_src->reg.indirect) {
+      if (iter_src->is_ssa) {
+         nir_ssa_def *ssa = iter_src->ssa;
+         if (ssa && nir_foreach_src(instr, src_does_not_use_def, ssa)) {
+            struct set_entry *entry = _mesa_set_search(ssa->uses, instr);
+            assert(entry);
+            _mesa_set_remove(ssa->uses, entry);
+         }
+      } else {
+         nir_register *reg = iter_src->reg.reg;
+         if (reg && nir_foreach_src(instr, src_does_not_use_reg, reg)) {
+            struct set_entry *entry = _mesa_set_search(reg->uses, instr);
+            assert(entry);
+            _mesa_set_remove(reg->uses, entry);
+         }
       }
    }
 
-   if (new_src.is_ssa) {
-      if (new_src.ssa)
-         _mesa_set_add(new_src.ssa->uses, instr);
-   } else {
-      if (new_src.reg.reg)
-         _mesa_set_add(new_src.reg.reg->uses, instr);
+   for (nir_src *iter_src = &new_src; iter_src;
+        iter_src = iter_src->is_ssa ? NULL : iter_src->reg.indirect) {
+      if (iter_src->is_ssa) {
+         if (iter_src->ssa)
+            _mesa_set_add(iter_src->ssa->uses, instr);
+      } else {
+         if (iter_src->reg.reg)
+            _mesa_set_add(iter_src->reg.reg->uses, instr);
+      }
    }
 }
 
diff --git a/mesalib/src/glsl/nir/nir.h b/mesalib/src/glsl/nir/nir.h
index 74772c798..98b0ec328 100644
--- a/mesalib/src/glsl/nir/nir.h
+++ b/mesalib/src/glsl/nir/nir.h
@@ -960,7 +960,8 @@ typedef struct {
 static inline unsigned
 nir_tex_instr_dest_size(nir_tex_instr *instr)
 {
-   if (instr->op == nir_texop_txs) {
+   switch (instr->op) {
+   case nir_texop_txs: {
       unsigned ret;
       switch (instr->sampler_dim) {
          case GLSL_SAMPLER_DIM_1D:
@@ -985,13 +986,18 @@ nir_tex_instr_dest_size(nir_tex_instr *instr)
       return ret;
    }
 
-   if (instr->op == nir_texop_query_levels)
+   case nir_texop_lod:
       return 2;
 
-   if (instr->is_shadow && instr->is_new_style_shadow)
+   case nir_texop_query_levels:
       return 1;
 
-   return 4;
+   default:
+      if (instr->is_shadow && instr->is_new_style_shadow)
+         return 1;
+
+      return 4;
+   }
 }
 
 static inline unsigned
@@ -1514,6 +1520,9 @@ nir_deref_struct *nir_deref_struct_create(void *mem_ctx, unsigned field_index);
 
 nir_deref *nir_copy_deref(void *mem_ctx, nir_deref *deref);
 
+nir_load_const_instr *
+nir_deref_get_const_initializer_load(nir_shader *shader, nir_deref_var *deref);
+
 void nir_instr_insert_before(nir_instr *instr, nir_instr *before);
 void nir_instr_insert_after(nir_instr *instr, nir_instr *after);
 
diff --git a/mesalib/src/glsl/nir/nir_array.h b/mesalib/src/glsl/nir/nir_array.h
new file mode 100644
index 000000000..1db4e8cea
--- /dev/null
+++ b/mesalib/src/glsl/nir/nir_array.h
@@ -0,0 +1,96 @@
+/*
+ * Copyright © 2015 Intel Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ *
+ * Authors:
+ *    Jason Ekstrand (jason@jlekstrand.net)
+ *
+ */
+
+#pragma once
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+typedef struct {
+   void *mem_ctx;
+   size_t size;
+   size_t alloc;
+   void *data;
+} nir_array;
+
+static inline void
+nir_array_init(nir_array *arr, void *mem_ctx)
+{
+   arr->mem_ctx = mem_ctx;
+   arr->size = 0;
+   arr->alloc = 0;
+   arr->data = NULL;
+}
+
+static inline void
+nir_array_fini(nir_array *arr)
+{
+   if (arr->mem_ctx)
+      ralloc_free(arr->data);
+   else
+      free(arr->data);
+}
+
+#define NIR_ARRAY_INITIAL_SIZE 64
+
+/* Increments the size of the array by the given ammount and returns a
+ * pointer to the beginning of the newly added space.
+ */
+static inline void *
+nir_array_grow(nir_array *arr, size_t additional)
+{
+   size_t new_size = arr->size + additional;
+   if (new_size > arr->alloc) {
+      if (arr->alloc == 0)
+         arr->alloc = NIR_ARRAY_INITIAL_SIZE;
+
+      while (new_size > arr->alloc)
+         arr->alloc *= 2;
+
+      if (arr->mem_ctx)
+         arr->data = reralloc_size(arr->mem_ctx, arr->data, arr->alloc);
+      else
+         arr->data = realloc(arr->data, arr->alloc);
+   }
+
+   void *ptr = (void *)((char *)arr->data + arr->size);
+   arr->size = new_size;
+
+   return ptr;
+}
+
+#define nir_array_add(arr, type, elem) \
+   *(type *)nir_array_grow(arr, sizeof(type)) = (elem)
+
+#define nir_array_foreach(arr, type, elem) \
+   for (type *elem = (type *)(arr)->data; \
+        elem < (type *)((char *)(arr)->data + (arr)->size); elem++)
+
+#ifdef __cplusplus
+} /* extern "C" */
+#endif
diff --git a/mesalib/src/glsl/nir/nir_from_ssa.c b/mesalib/src/glsl/nir/nir_from_ssa.c
index 184698abd..6a3b141bd 100644
--- a/mesalib/src/glsl/nir/nir_from_ssa.c
+++ b/mesalib/src/glsl/nir/nir_from_ssa.c
@@ -642,7 +642,7 @@ emit_copy(nir_parallel_copy_instr *pcopy, nir_src src, nir_src dest_src,
  *
  * The algorithm works by playing this little shell game with the values.
  * We start by recording where every source value is and which source value
- * each destination value should recieve.  We then grab any copy whose
+ * each destination value should receive.  We then grab any copy whose
  * destination is "empty", i.e. not used as a source, and do the following:
  *  - Find where its source value currently lives
  *  - Emit the move instruction
diff --git a/mesalib/src/glsl/nir/nir_lower_locals_to_regs.c b/mesalib/src/glsl/nir/nir_lower_locals_to_regs.c
index 8c5df7be6..bc6a3d320 100644
--- a/mesalib/src/glsl/nir/nir_lower_locals_to_regs.c
+++ b/mesalib/src/glsl/nir/nir_lower_locals_to_regs.c
@@ -26,13 +26,20 @@
  */
 
 #include "nir.h"
+#include "nir_array.h"
 
 struct locals_to_regs_state {
-   void *mem_ctx;
+   nir_shader *shader;
    nir_function_impl *impl;
 
    /* A hash table mapping derefs to registers */
    struct hash_table *regs_table;
+
+   /* A growing array of derefs that we have encountered.  There is exactly
+    * one element of this array per element in the hash table.  This is
+    * used to make adding register initialization code deterministic.
+    */
+   nir_array derefs_array;
 };
 
 /* The following two functions implement a hash and equality check for
@@ -100,15 +107,8 @@ get_reg_for_deref(nir_deref_var *deref, struct locals_to_regs_state *state)
    unsigned array_size = 1;
    nir_deref *tail = &deref->deref;
    while (tail->child) {
-      if (tail->child->deref_type == nir_deref_type_array) {
-         /* Multiply by the parent's type. */
-         if (glsl_type_is_matrix(tail->type)) {
-            array_size *= glsl_get_matrix_columns(tail->type);
-         } else {
-            assert(glsl_get_length(tail->type) > 0);
-            array_size *= glsl_get_length(tail->type);
-         }
-      }
+      if (tail->child->deref_type == nir_deref_type_array)
+         array_size *= glsl_get_length(tail->type);
       tail = tail->child;
    }
 
@@ -119,6 +119,7 @@ get_reg_for_deref(nir_deref_var *deref, struct locals_to_regs_state *state)
    reg->num_array_elems = array_size > 1 ? array_size : 0;
 
    _mesa_hash_table_insert_pre_hashed(state->regs_table, hash, deref, reg);
+   nir_array_add(&state->derefs_array, nir_deref_var *, deref);
 
    return reg;
 }
@@ -134,6 +135,14 @@ get_deref_reg_src(nir_deref_var *deref, nir_instr *instr,
    src.reg.base_offset = 0;
    src.reg.indirect = NULL;
 
+   /* It is possible for a user to create a shader that has an array with a
+    * single element and then proceed to access it indirectly.  Indirectly
+    * accessing a non-array register is not allowed in NIR.  In order to
+    * handle this case we just convert it to a direct reference.
+    */
+   if (src.reg.reg->num_array_elems == 0)
+      return src;
+
    nir_deref *tail = &deref->deref;
    while (tail->child != NULL) {
       const struct glsl_type *parent_type = tail->type;
@@ -149,11 +158,11 @@ get_deref_reg_src(nir_deref_var *deref, nir_instr *instr,
 
       if (src.reg.indirect) {
          nir_load_const_instr *load_const =
-            nir_load_const_instr_create(state->mem_ctx, 1);
+            nir_load_const_instr_create(state->shader, 1);
          load_const->value.u[0] = glsl_get_length(parent_type);
          nir_instr_insert_before(instr, &load_const->instr);
 
-         nir_alu_instr *mul = nir_alu_instr_create(state->mem_ctx, nir_op_imul);
+         nir_alu_instr *mul = nir_alu_instr_create(state->shader, nir_op_imul);
          mul->src[0].src = *src.reg.indirect;
          mul->src[1].src.is_ssa = true;
          mul->src[1].src.ssa = &load_const->def;
@@ -167,15 +176,15 @@ get_deref_reg_src(nir_deref_var *deref, nir_instr *instr,
 
       if (deref_array->deref_array_type == nir_deref_array_type_indirect) {
          if (src.reg.indirect == NULL) {
-            src.reg.indirect = ralloc(state->mem_ctx, nir_src);
+            src.reg.indirect = ralloc(state->shader, nir_src);
             nir_src_copy(src.reg.indirect, &deref_array->indirect,
-                         state->mem_ctx);
+                         state->shader);
          } else {
-            nir_alu_instr *add = nir_alu_instr_create(state->mem_ctx,
+            nir_alu_instr *add = nir_alu_instr_create(state->shader,
                                                       nir_op_iadd);
             add->src[0].src = *src.reg.indirect;
             nir_src_copy(&add->src[1].src, &deref_array->indirect,
-                         state->mem_ctx);
+                         state->shader);
             add->dest.write_mask = 1;
             nir_ssa_dest_init(&add->instr, &add->dest.dest, 1, NULL);
             nir_instr_insert_before(instr, &add->instr);
@@ -205,7 +214,7 @@ lower_locals_to_regs_block(nir_block *block, void *void_state)
          if (intrin->variables[0]->var->data.mode != nir_var_local)
             continue;
 
-         nir_alu_instr *mov = nir_alu_instr_create(state->mem_ctx, nir_op_imov);
+         nir_alu_instr *mov = nir_alu_instr_create(state->shader, nir_op_imov);
          mov->src[0].src = get_deref_reg_src(intrin->variables[0],
                                              &intrin->instr, state);
          mov->dest.write_mask = (1 << intrin->num_components) - 1;
@@ -214,9 +223,9 @@ lower_locals_to_regs_block(nir_block *block, void *void_state)
                               intrin->num_components, NULL);
             nir_ssa_def_rewrite_uses(&intrin->dest.ssa,
                                      nir_src_for_ssa(&mov->dest.dest.ssa),
-                                     state->mem_ctx);
+                                     state->shader);
          } else {
-            nir_dest_copy(&mov->dest.dest, &intrin->dest, state->mem_ctx);
+            nir_dest_copy(&mov->dest.dest, &intrin->dest, state->shader);
          }
          nir_instr_insert_before(&intrin->instr, &mov->instr);
 
@@ -231,8 +240,8 @@ lower_locals_to_regs_block(nir_block *block, void *void_state)
          nir_src reg_src = get_deref_reg_src(intrin->variables[0],
                                              &intrin->instr, state);
 
-         nir_alu_instr *mov = nir_alu_instr_create(state->mem_ctx, nir_op_imov);
-         nir_src_copy(&mov->src[0].src, &intrin->src[0], state->mem_ctx);
+         nir_alu_instr *mov = nir_alu_instr_create(state->shader, nir_op_imov);
+         nir_src_copy(&mov->src[0].src, &intrin->src[0], state->shader);
          mov->dest.write_mask = (1 << intrin->num_components) - 1;
          mov->dest.dest.is_ssa = false;
          mov->dest.dest.reg.reg = reg_src.reg.reg;
@@ -257,20 +266,115 @@ lower_locals_to_regs_block(nir_block *block, void *void_state)
    return true;
 }
 
+static nir_block *
+compute_reg_usedef_lca(nir_register *reg)
+{
+   struct set_entry *entry;
+   nir_block *lca = NULL;
+
+   set_foreach(reg->defs, entry)
+      lca = nir_dominance_lca(lca, ((nir_instr *)entry->key)->block);
+
+   set_foreach(reg->uses, entry)
+      lca = nir_dominance_lca(lca, ((nir_instr *)entry->key)->block);
+
+   set_foreach(reg->if_uses, entry) {
+      nir_if *if_stmt = (nir_if *)entry->key;
+      nir_cf_node *prev_node = nir_cf_node_prev(&if_stmt->cf_node);
+      assert(prev_node->type == nir_cf_node_block);
+      lca = nir_dominance_lca(lca, nir_cf_node_as_block(prev_node));
+   }
+
+   return lca;
+}
+
+static void
+insert_constant_initializer(nir_deref_var *deref_head, nir_deref *deref_tail,
+                            nir_block *block,
+                            struct locals_to_regs_state *state)
+{
+   if (deref_tail->child) {
+      switch (deref_tail->child->deref_type) {
+      case nir_deref_type_array: {
+         unsigned array_elems = glsl_get_length(deref_tail->type);
+
+         nir_deref_array arr_deref;
+         arr_deref.deref = *deref_tail->child;
+         arr_deref.deref_array_type = nir_deref_array_type_direct;
+
+         nir_deref *old_child = deref_tail->child;
+         deref_tail->child = &arr_deref.deref;
+         for (unsigned i = 0; i < array_elems; i++) {
+            arr_deref.base_offset = i;
+            insert_constant_initializer(deref_head, &arr_deref.deref,
+                                        block, state);
+         }
+         deref_tail->child = old_child;
+         return;
+      }
+
+      case nir_deref_type_struct:
+         insert_constant_initializer(deref_head, deref_tail->child,
+                                     block, state);
+         return;
+
+      default:
+         unreachable("Invalid deref child type");
+      }
+   }
+
+   assert(deref_tail->child == NULL);
+
+   nir_load_const_instr *load =
+      nir_deref_get_const_initializer_load(state->shader, deref_head);
+   nir_instr_insert_before_block(block, &load->instr);
+
+   nir_src reg_src = get_deref_reg_src(deref_head, &load->instr, state);
+
+   nir_alu_instr *mov = nir_alu_instr_create(state->shader, nir_op_imov);
+   mov->src[0].src = nir_src_for_ssa(&load->def);
+   mov->dest.write_mask = (1 << load->def.num_components) - 1;
+   mov->dest.dest.is_ssa = false;
+   mov->dest.dest.reg.reg = reg_src.reg.reg;
+   mov->dest.dest.reg.base_offset = reg_src.reg.base_offset;
+   mov->dest.dest.reg.indirect = reg_src.reg.indirect;
+
+   nir_instr_insert_after(&load->instr, &mov->instr);
+}
+
 static void
 nir_lower_locals_to_regs_impl(nir_function_impl *impl)
 {
    struct locals_to_regs_state state;
 
-   state.mem_ctx = ralloc_parent(impl);
+   state.shader = impl->overload->function->shader;
    state.impl = impl;
    state.regs_table = _mesa_hash_table_create(NULL, hash_deref, derefs_equal);
+   nir_array_init(&state.derefs_array, NULL);
+
+   nir_metadata_require(impl, nir_metadata_dominance);
 
    nir_foreach_block(impl, lower_locals_to_regs_block, &state);
 
+   nir_array_foreach(&state.derefs_array, nir_deref_var *, deref_ptr) {
+      nir_deref_var *deref = *deref_ptr;
+      struct hash_entry *deref_entry =
+         _mesa_hash_table_search(state.regs_table, deref);
+      assert(deref_entry && deref_entry->key == deref);
+      nir_register *reg = (nir_register *)deref_entry->data;
+
+      if (deref->var->constant_initializer == NULL)
+         continue;
+
+      nir_block *usedef_lca = compute_reg_usedef_lca(reg);
+
+      insert_constant_initializer(deref, &deref->deref, usedef_lca, &state);
+   }
+
    nir_metadata_preserve(impl, nir_metadata_block_index |
                                nir_metadata_dominance);
 
+   nir_array_fini(&state.derefs_array);
    _mesa_hash_table_destroy(state.regs_table, NULL);
 }
 
diff --git a/mesalib/src/glsl/nir/nir_lower_to_source_mods.c b/mesalib/src/glsl/nir/nir_lower_to_source_mods.c
index d6bf77f17..7b4a0f657 100644
--- a/mesalib/src/glsl/nir/nir_lower_to_source_mods.c
+++ b/mesalib/src/glsl/nir/nir_lower_to_source_mods.c
@@ -67,6 +67,13 @@ nir_lower_to_source_mods_block(nir_block *block, void *state)
             continue;
          }
 
+         /* We can only do a rewrite if the source we are copying is SSA.
+          * Otherwise, moving the read might invalidly reorder reads/writes
+          * on a register.
+          */
+         if (!parent->src[0].src.is_ssa)
+            continue;
+
          nir_instr_rewrite_src(instr, &alu->src[i].src, parent->src[0].src);
          if (alu->src[i].abs) {
             /* abs trumps both neg and abs, do nothing */
diff --git a/mesalib/src/glsl/nir/nir_lower_var_copies.c b/mesalib/src/glsl/nir/nir_lower_var_copies.c
index 58389a7c7..21672901f 100644
--- a/mesalib/src/glsl/nir/nir_lower_var_copies.c
+++ b/mesalib/src/glsl/nir/nir_lower_var_copies.c
@@ -64,26 +64,6 @@ get_deref_tail(nir_deref *deref)
    return deref;
 }
 
-static int
-type_get_length(const struct glsl_type *type)
-{
-   switch (glsl_get_base_type(type)) {
-   case GLSL_TYPE_STRUCT:
-   case GLSL_TYPE_ARRAY:
-      return glsl_get_length(type);
-   case GLSL_TYPE_FLOAT:
-   case GLSL_TYPE_INT:
-   case GLSL_TYPE_UINT:
-   case GLSL_TYPE_BOOL:
-      if (glsl_type_is_matrix(type))
-         return glsl_get_matrix_columns(type);
-      else
-         return glsl_get_vector_elements(type);
-   default:
-      unreachable("Invalid deref base type");
-   }
-}
-
 /* This function recursively walks the given deref chain and replaces the
  * given copy instruction with an equivalent sequence load/store
  * operations.
@@ -121,9 +101,9 @@ emit_copy_load_store(nir_intrinsic_instr *copy_instr,
       nir_deref_array *src_arr = nir_deref_as_array(src_arr_parent->child);
       nir_deref_array *dest_arr = nir_deref_as_array(dest_arr_parent->child);
 
-      unsigned length = type_get_length(src_arr_parent->type);
+      unsigned length = glsl_get_length(src_arr_parent->type);
       /* The wildcards should represent the same number of elements */
-      assert(length == type_get_length(dest_arr_parent->type));
+      assert(length == glsl_get_length(dest_arr_parent->type));
       assert(length > 0);
 
       /* Walk over all of the elements that this wildcard refers to and
diff --git a/mesalib/src/glsl/nir/nir_lower_vars_to_ssa.c b/mesalib/src/glsl/nir/nir_lower_vars_to_ssa.c
index 2ca74d71b..bb60f4601 100644
--- a/mesalib/src/glsl/nir/nir_lower_vars_to_ssa.c
+++ b/mesalib/src/glsl/nir/nir_lower_vars_to_ssa.c
@@ -55,7 +55,7 @@ struct deref_node {
 };
 
 struct lower_variables_state {
-   void *mem_ctx;
+   nir_shader *shader;
    void *dead_ctx;
    nir_function_impl *impl;
 
@@ -90,34 +90,14 @@ struct lower_variables_state {
    struct hash_table *phi_table;
 };
 
-static int
-type_get_length(const struct glsl_type *type)
-{
-   switch (glsl_get_base_type(type)) {
-   case GLSL_TYPE_STRUCT:
-   case GLSL_TYPE_ARRAY:
-      return glsl_get_length(type);
-   case GLSL_TYPE_FLOAT:
-   case GLSL_TYPE_INT:
-   case GLSL_TYPE_UINT:
-   case GLSL_TYPE_BOOL:
-      if (glsl_type_is_matrix(type))
-         return glsl_get_matrix_columns(type);
-      else
-         return glsl_get_vector_elements(type);
-   default:
-      unreachable("Invalid deref base type");
-   }
-}
-
 static struct deref_node *
 deref_node_create(struct deref_node *parent,
-                  const struct glsl_type *type, void *mem_ctx)
+                  const struct glsl_type *type, nir_shader *shader)
 {
    size_t size = sizeof(struct deref_node) +
-                 type_get_length(type) * sizeof(struct deref_node *);
+                 glsl_get_length(type) * sizeof(struct deref_node *);
 
-   struct deref_node *node = rzalloc_size(mem_ctx, size);
+   struct deref_node *node = rzalloc_size(shader, size);
    node->type = type;
    node->parent = parent;
    node->deref = NULL;
@@ -165,7 +145,7 @@ get_deref_node(nir_deref_var *deref, struct lower_variables_state *state)
       case nir_deref_type_struct: {
          nir_deref_struct *deref_struct = nir_deref_as_struct(tail);
 
-         assert(deref_struct->index < type_get_length(node->type));
+         assert(deref_struct->index < glsl_get_length(node->type));
 
          if (node->children[deref_struct->index] == NULL)
             node->children[deref_struct->index] =
@@ -184,7 +164,7 @@ get_deref_node(nir_deref_var *deref, struct lower_variables_state *state)
              * out-of-bounds offset.  We need to handle this at least
              * somewhat gracefully.
              */
-            if (arr->base_offset >= type_get_length(node->type))
+            if (arr->base_offset >= glsl_get_length(node->type))
                return NULL;
 
             if (node->children[arr->base_offset] == NULL)
@@ -317,6 +297,10 @@ deref_may_be_aliased_node(struct deref_node *node, nir_deref *deref,
          if (arr->deref_array_type == nir_deref_array_type_indirect)
             return true;
 
+         /* If there is an indirect at this level, we're aliased. */
+         if (node->indirect)
+            return true;
+
          assert(arr->deref_array_type == nir_deref_array_type_direct);
 
          if (node->children[arr->base_offset] &&
@@ -465,7 +449,7 @@ lower_copies_to_load_store(struct deref_node *node,
    set_foreach(node->copies, copy_entry) {
       nir_intrinsic_instr *copy = (void *)copy_entry->key;
 
-      nir_lower_var_copy_instr(copy, state->mem_ctx);
+      nir_lower_var_copy_instr(copy, state->shader);
 
       for (unsigned i = 0; i < 2; ++i) {
          struct deref_node *arg_node =
@@ -485,67 +469,6 @@ lower_copies_to_load_store(struct deref_node *node,
    return true;
 }
 
-/* Returns a load_const instruction that represents the constant
- * initializer for the given deref chain.  The caller is responsible for
- * ensuring that there actually is a constant initializer.
- */
-static nir_load_const_instr *
-get_const_initializer_load(const nir_deref_var *deref,
-                           struct lower_variables_state *state)
-{
-   nir_constant *constant = deref->var->constant_initializer;
-   const nir_deref *tail = &deref->deref;
-   unsigned matrix_offset = 0;
-   while (tail->child) {
-      switch (tail->child->deref_type) {
-      case nir_deref_type_array: {
-         nir_deref_array *arr = nir_deref_as_array(tail->child);
-         assert(arr->deref_array_type == nir_deref_array_type_direct);
-         if (glsl_type_is_matrix(tail->type)) {
-            assert(arr->deref.child == NULL);
-            matrix_offset = arr->base_offset;
-         } else {
-            constant = constant->elements[arr->base_offset];
-         }
-         break;
-      }
-
-      case nir_deref_type_struct: {
-         constant = constant->elements[nir_deref_as_struct(tail->child)->index];
-         break;
-      }
-
-      default:
-         unreachable("Invalid deref child type");
-      }
-
-      tail = tail->child;
-   }
-
-   nir_load_const_instr *load =
-      nir_load_const_instr_create(state->mem_ctx,
-                                  glsl_get_vector_elements(tail->type));
-
-   matrix_offset *= load->def.num_components;
-   for (unsigned i = 0; i < load->def.num_components; i++) {
-      switch (glsl_get_base_type(tail->type)) {
-      case GLSL_TYPE_FLOAT:
-      case GLSL_TYPE_INT:
-      case GLSL_TYPE_UINT:
-         load->value.u[i] = constant->value.u[matrix_offset + i];
-         break;
-      case GLSL_TYPE_BOOL:
-         load->value.u[i] = constant->value.b[matrix_offset + i] ?
-                             NIR_TRUE : NIR_FALSE;
-         break;
-      default:
-         unreachable("Invalid immediate type");
-      }
-   }
-
-   return load;
-}
-
 /** Pushes an SSA def onto the def stack for the given node
  *
  * Each node is potentially associated with a stack of SSA definitions.
@@ -614,7 +537,7 @@ get_ssa_def_for_block(struct deref_node *node, nir_block *block,
     * given block.  This means that we need to add an undef and use that.
     */
    nir_ssa_undef_instr *undef =
-      nir_ssa_undef_instr_create(state->mem_ctx,
+      nir_ssa_undef_instr_create(state->shader,
                                  glsl_get_vector_elements(node->type));
    nir_instr_insert_before_cf_list(&state->impl->body, &undef->instr);
    def_stack_push(node, &undef->def, state);
@@ -694,7 +617,7 @@ rename_variables_block(nir_block *block, struct lower_variables_state *state)
                 * should result in an undefined value.
                 */
                nir_ssa_undef_instr *undef =
-                  nir_ssa_undef_instr_create(state->mem_ctx,
+                  nir_ssa_undef_instr_create(state->shader,
                                              intrin->num_components);
 
                nir_instr_insert_before(&intrin->instr, &undef->instr);
@@ -702,14 +625,14 @@ rename_variables_block(nir_block *block, struct lower_variables_state *state)
 
                nir_ssa_def_rewrite_uses(&intrin->dest.ssa,
                                         nir_src_for_ssa(&undef->def),
-                                        state->mem_ctx);
+                                        state->shader);
                continue;
             }
 
             if (!node->lower_to_ssa)
                continue;
 
-            nir_alu_instr *mov = nir_alu_instr_create(state->mem_ctx,
+            nir_alu_instr *mov = nir_alu_instr_create(state->shader,
                                                       nir_op_imov);
             mov->src[0].src.is_ssa = true;
             mov->src[0].src.ssa = get_ssa_def_for_block(node, block, state);
@@ -727,7 +650,7 @@ rename_variables_block(nir_block *block, struct lower_variables_state *state)
 
             nir_ssa_def_rewrite_uses(&intrin->dest.ssa,
                                      nir_src_for_ssa(&mov->dest.dest.ssa),
-                                     state->mem_ctx);
+                                     state->shader);
             break;
          }
 
@@ -750,7 +673,7 @@ rename_variables_block(nir_block *block, struct lower_variables_state *state)
 
             assert(intrin->src[0].is_ssa);
 
-            nir_alu_instr *mov = nir_alu_instr_create(state->mem_ctx,
+            nir_alu_instr *mov = nir_alu_instr_create(state->shader,
                                                       nir_op_imov);
             mov->src[0].src.is_ssa = true;
             mov->src[0].src.ssa = intrin->src[0].ssa;
@@ -887,7 +810,7 @@ insert_phi_nodes(struct lower_variables_state *state)
                continue;
 
             if (has_already[next->index] < iter_count) {
-               nir_phi_instr *phi = nir_phi_instr_create(state->mem_ctx);
+               nir_phi_instr *phi = nir_phi_instr_create(state->shader);
                nir_ssa_dest_init(&phi->instr, &phi->dest,
                                  glsl_get_vector_elements(node->type), NULL);
                nir_instr_insert_before_block(next, &phi->instr);
@@ -938,8 +861,8 @@ nir_lower_vars_to_ssa_impl(nir_function_impl *impl)
 {
    struct lower_variables_state state;
 
-   state.mem_ctx = ralloc_parent(impl);
-   state.dead_ctx = ralloc_context(state.mem_ctx);
+   state.shader = impl->overload->function->shader;
+   state.dead_ctx = ralloc_context(state.shader);
    state.impl = impl;
 
    state.deref_var_nodes = _mesa_hash_table_create(state.dead_ctx,
@@ -983,7 +906,8 @@ nir_lower_vars_to_ssa_impl(nir_function_impl *impl)
       progress = true;
 
       if (deref->var->constant_initializer) {
-         nir_load_const_instr *load = get_const_initializer_load(deref, &state);
+         nir_load_const_instr *load =
+            nir_deref_get_const_initializer_load(state.shader, deref);
          nir_ssa_def_init(&load->instr, &load->def,
                           glsl_get_vector_elements(node->type), NULL);
          nir_instr_insert_before_cf_list(&impl->body, &load->instr);
diff --git a/mesalib/src/glsl/nir/nir_opt_algebraic.py b/mesalib/src/glsl/nir/nir_opt_algebraic.py
index cdb19241c..2a2b9561e 100644
--- a/mesalib/src/glsl/nir/nir_opt_algebraic.py
+++ b/mesalib/src/glsl/nir/nir_opt_algebraic.py
@@ -144,6 +144,7 @@ optimizations = [
    (('fexp',  ('fmul', ('flog', a), b)),  ('fpow', a, b), '!options->lower_fpow'), # e^(ln(a)*b) = a^b
    (('fpow', a, 1.0), a),
    (('fpow', a, 2.0), ('fmul', a, a)),
+   (('fpow', a, 4.0), ('fmul', ('fmul', a, a), ('fmul', a, a))),
    (('fpow', 2.0, a), ('fexp2', a)),
    (('fsqrt', ('fexp2', a)), ('fexp2', ('fmul', 0.5, a))),
    (('fsqrt', ('fexp', a)), ('fexp', ('fmul', 0.5, a))),
diff --git a/mesalib/src/glsl/nir/nir_print.c b/mesalib/src/glsl/nir/nir_print.c
index fb8c9344c..eb4045cec 100644
--- a/mesalib/src/glsl/nir/nir_print.c
+++ b/mesalib/src/glsl/nir/nir_print.c
@@ -533,6 +533,8 @@ print_load_const_instr(nir_load_const_instr *instr, unsigned tabs, FILE *fp)
 
       fprintf(fp, "0x%08x /* %f */", instr->value.u[i], instr->value.f[i]);
    }
+
+   fprintf(fp, ")");
 }
 
 static void
diff --git a/mesalib/src/glsl/nir/nir_types.cpp b/mesalib/src/glsl/nir/nir_types.cpp
index f0d0b46d2..62176f508 100644
--- a/mesalib/src/glsl/nir/nir_types.cpp
+++ b/mesalib/src/glsl/nir/nir_types.cpp
@@ -103,7 +103,7 @@ glsl_get_matrix_columns(const struct glsl_type *type)
 unsigned
 glsl_get_length(const struct glsl_type *type)
 {
-   return type->length;
+   return type->is_matrix() ? type->matrix_columns : type->length;
 }
 
 const char *
diff --git a/mesalib/src/glsl/opt_algebraic.cpp b/mesalib/src/glsl/opt_algebraic.cpp
index 3d2f2ca0b..fa5db70f2 100644
--- a/mesalib/src/glsl/opt_algebraic.cpp
+++ b/mesalib/src/glsl/opt_algebraic.cpp
@@ -98,6 +98,12 @@ is_vec_two(ir_constant *ir)
    return (ir == NULL) ? false : ir->is_value(2.0, 2);
 }
 
+static inline bool
+is_vec_four(ir_constant *ir)
+{
+   return (ir == NULL) ? false : ir->is_value(4.0, 4);
+}
+
 static inline bool
 is_vec_negative_one(ir_constant *ir)
 {
@@ -774,6 +780,20 @@ ir_algebraic_visitor::handle_expression(ir_expression *ir)
          return mul(x, x);
       }
 
+      if (is_vec_four(op_const[1])) {
+         ir_variable *x = new(ir) ir_variable(ir->operands[1]->type, "x",
+                                              ir_var_temporary);
+         base_ir->insert_before(x);
+         base_ir->insert_before(assign(x, ir->operands[0]));
+
+         ir_variable *squared = new(ir) ir_variable(ir->operands[1]->type,
+                                                    "squared",
+                                                    ir_var_temporary);
+         base_ir->insert_before(squared);
+         base_ir->insert_before(assign(squared, mul(x, x)));
+         return mul(squared, squared);
+      }
+
       break;
 
    case ir_binop_min:
diff --git a/mesalib/src/glsl/opt_dead_builtin_varyings.cpp b/mesalib/src/glsl/opt_dead_builtin_varyings.cpp
index 50c8aa763..92f20c71d 100644
--- a/mesalib/src/glsl/opt_dead_builtin_varyings.cpp
+++ b/mesalib/src/glsl/opt_dead_builtin_varyings.cpp
@@ -35,7 +35,7 @@
  * the built-in varyings have pre-assigned locations. Also, the elimination
  * of unused gl_TexCoord elements requires its own lowering pass anyway.
  *
- * It's implemented by replacing all occurences of dead varyings with
+ * It's implemented by replacing all occurrences of dead varyings with
  * temporary variables, which creates dead code. It is recommended to run
  * a dead-code elimination pass after this.
  *
@@ -280,7 +280,7 @@ public:
        *
        * We're going to break down the gl_TexCoord array into separate
        * variables. First, add declarations of the new variables all
-       * occurences of gl_TexCoord will be replaced with.
+       * occurrences of gl_TexCoord will be replaced with.
        */
       if (info->lower_texcoord_array) {
          prepare_array(ir, this->new_texcoord, ARRAY_SIZE(this->new_texcoord),
@@ -411,7 +411,7 @@ public:
        * variable dereference representing gl_TexCoord[i].
        */
       if (this->info->lower_texcoord_array) {
-         /* gl_TexCoord[i] occurence */
+         /* gl_TexCoord[i] occurrence */
          ir_dereference_array *const da = (*rvalue)->as_dereference_array();
 
          if (da && da->variable_referenced() ==
@@ -425,7 +425,7 @@ public:
 
       /* Same for gl_FragData. */
       if (this->info->lower_fragdata_array) {
-         /* gl_FragData[i] occurence */
+         /* gl_FragData[i] occurrence */
          ir_dereference_array *const da = (*rvalue)->as_dereference_array();
 
          if (da && da->variable_referenced() == this->info->fragdata_array) {
diff --git a/mesalib/src/glsl/opt_function_inlining.cpp b/mesalib/src/glsl/opt_function_inlining.cpp
index 64b4907ba..84a9e4fa0 100644
--- a/mesalib/src/glsl/opt_function_inlining.cpp
+++ b/mesalib/src/glsl/opt_function_inlining.cpp
@@ -130,7 +130,7 @@ ir_call::generate_inline(ir_instruction *next_ir)
 	 parameters[i] = sig_param->clone(ctx, ht);
 	 parameters[i]->data.mode = ir_var_auto;
 
-	 /* Remove the read-only decoration becuase we're going to write
+	 /* Remove the read-only decoration because we're going to write
 	  * directly to this variable.  If the cloned variable is left
 	  * read-only and the inlined function is inside a loop, the loop
 	  * analysis code will get confused.
diff --git a/mesalib/src/glsl/standalone_scaffolding.cpp b/mesalib/src/glsl/standalone_scaffolding.cpp
index 6f5a27f85..a109c4e92 100644
--- a/mesalib/src/glsl/standalone_scaffolding.cpp
+++ b/mesalib/src/glsl/standalone_scaffolding.cpp
@@ -189,9 +189,6 @@ void initialize_context_to_defaults(struct gl_context *ctx, gl_api api)
    options.MaxUnrollIterations = 32;
    options.MaxIfDepth = UINT_MAX;
 
-   /* Default pragma settings */
-   options.DefaultPragmas.Optimize = true;
-
    for (int sh = 0; sh < MESA_SHADER_STAGES; ++sh)
       memcpy(&ctx->Const.ShaderCompilerOptions[sh], &options, sizeof(options));
 }
diff --git a/mesalib/src/loader/Android.mk b/mesalib/src/loader/Android.mk
index 8daeedf5d..8e215de3c 100644
--- a/mesalib/src/loader/Android.mk
+++ b/mesalib/src/loader/Android.mk
@@ -37,7 +37,7 @@ LOCAL_SRC_FILES := \
 ifeq ($(MESA_GPU_DRIVERS),swrast)
 LOCAL_CFLAGS += -D__NOT_HAVE_DRM_H
 else
-LOCAL_C_INCLUDES += $(TARGET_OUT_HEADERS)/libdrm
+LOCAL_SHARED_LIBRARIES := libdrm
 endif
 
 LOCAL_MODULE := libmesa_loader
diff --git a/mesalib/src/loader/loader.h b/mesalib/src/loader/loader.h
index fa57950de..810e7da7f 100644
--- a/mesalib/src/loader/loader.h
+++ b/mesalib/src/loader/loader.h
@@ -43,7 +43,7 @@ loader_get_device_name_for_fd(int fd);
 
 /* Function to get a different device than the one we are to use by default,
  * if the user requests so and it is possible. The initial fd will be closed
- * if neccessary. The returned fd is potentially a render-node.
+ * if necessary. The returned fd is potentially a render-node.
  */
 
 int
diff --git a/mesalib/src/mapi/Android.mk b/mesalib/src/mapi/Android.mk
index f10437874..44452181e 100644
--- a/mesalib/src/mapi/Android.mk
+++ b/mesalib/src/mapi/Android.mk
@@ -53,7 +53,7 @@ LOCAL_C_INCLUDES := \
 LOCAL_MODULE := libglapi
 
 LOCAL_MODULE_CLASS := SHARED_LIBRARIES
-intermediates := $(call local-intermediates-dir)
+intermediates := $(call local-generated-sources-dir)
 abi_header := $(intermediates)/$(abi_header)
 LOCAL_GENERATED_SOURCES := $(abi_header)
 
diff --git a/mesalib/src/mapi/glapi/gen/glX_proto_send.py b/mesalib/src/mapi/glapi/gen/glX_proto_send.py
index 1419a7cc1..b93989f75 100644
--- a/mesalib/src/mapi/glapi/gen/glX_proto_send.py
+++ b/mesalib/src/mapi/glapi/gen/glX_proto_send.py
@@ -574,7 +574,7 @@ generic_%u_byte( GLint rop, const void * ptr )
         print '    struct glx_context * const gc = __glXGetCurrentContext();'
 
         # The only reason that single and vendor private commands need
-        # a variable called 'dpy' is becuase they use the SyncHandle
+        # a variable called 'dpy' is because they use the SyncHandle
         # macro.  For whatever brain-dead reason, that macro is hard-
         # coded to use a variable called 'dpy' instead of taking a
         # parameter.
diff --git a/mesalib/src/mapi/glapi/gen/glX_proto_size.py b/mesalib/src/mapi/glapi/gen/glX_proto_size.py
index fdb355d4d..4737fbf71 100644
--- a/mesalib/src/mapi/glapi/gen/glX_proto_size.py
+++ b/mesalib/src/mapi/glapi/gen/glX_proto_size.py
@@ -57,7 +57,7 @@ class glx_enum_function(object):
         # Fill self.count and self.enums using the dictionary of enums
         # that was passed in.  The generic Get functions (e.g.,
         # GetBooleanv and friends) are handled specially here.  In
-        # the data the generic Get functions are refered to as "Get".
+        # the data the generic Get functions are referred to as "Get".
 
         if func_name in ["GetIntegerv", "GetBooleanv", "GetFloatv", "GetDoublev"]:
             match_name = "Get"
diff --git a/mesalib/src/mapi/glapi/gen/gl_x86-64_asm.py b/mesalib/src/mapi/glapi/gen/gl_x86-64_asm.py
index 5a69e0343..7afc2b108 100644
--- a/mesalib/src/mapi/glapi/gen/gl_x86-64_asm.py
+++ b/mesalib/src/mapi/glapi/gen/gl_x86-64_asm.py
@@ -187,7 +187,7 @@ class PrintGenericStubs(gl_XML.gl_print_base):
 
         # The x86-64 ABI divides function parameters into a couple
         # classes.  For the OpenGL interface, the only ones that are
-        # relevent are INTEGER and SSE.  Basically, the first 8
+        # relevant are INTEGER and SSE.  Basically, the first 8
         # GLfloat or GLdouble parameters are placed in %xmm0 - %xmm7,
         # the first 6 non-GLfloat / non-GLdouble parameters are placed
         # in registers listed in int_parameters.
diff --git a/mesalib/src/mapi/glapi/glapi_entrypoint.c b/mesalib/src/mapi/glapi/glapi_entrypoint.c
index 7facd8aca..f1f7f5e59 100644
--- a/mesalib/src/mapi/glapi/glapi_entrypoint.c
+++ b/mesalib/src/mapi/glapi/glapi_entrypoint.c
@@ -29,6 +29,8 @@
  */
 
 
+#include <string.h>
+
 #include "c11/threads.h"
 #include "glapi/glapi_priv.h"
 #include "u_execmem.h"
diff --git a/mesalib/src/mesa/Android.gen.mk b/mesalib/src/mesa/Android.gen.mk
index caae2c107..cc979547e 100644
--- a/mesalib/src/mesa/Android.gen.mk
+++ b/mesalib/src/mesa/Android.gen.mk
@@ -27,13 +27,16 @@ ifeq ($(LOCAL_MODULE_CLASS),)
 LOCAL_MODULE_CLASS := STATIC_LIBRARIES
 endif
 
-intermediates := $(call local-intermediates-dir)
+intermediates := $(call local-generated-sources-dir)
 
 # This is the list of auto-generated files: sources and headers
 sources := \
 	main/enums.c \
 	main/api_exec.c \
 	main/dispatch.h \
+	main/format_pack.c \
+	main/format_unpack.c \
+	main/format_info.h \
 	main/remap_helper.h \
 	main/get_hash.h
 
@@ -124,3 +127,21 @@ format_info_deps := \
 
 $(intermediates)/main/format_info.h: $(format_info_deps)
 	@$(MESA_PYTHON2) $(FORMAT_INFO) $< > $@
+
+FORMAT_PACK := $(LOCAL_PATH)/main/format_pack.py
+format_pack_deps := \
+	$(LOCAL_PATH)/main/formats.csv \
+	$(LOCAL_PATH)/main/format_parser.py \
+	$(FORMAT_PACK)
+
+$(intermediates)/main/format_pack.c: $(format_pack_deps)
+	$(hide) $(MESA_PYTHON2) $(FORMAT_PACK) $< > $@
+
+FORMAT_UNPACK := $(LOCAL_PATH)/main/format_unpack.py
+format_unpack_deps := \
+	$(LOCAL_PATH)/main/formats.csv \
+	$(LOCAL_PATH)/main/format_parser.py \
+	$(FORMAT_UNPACK)
+
+$(intermediates)/main/format_unpack.c: $(format_unpack_deps)
+	$(hide) $(MESA_PYTHON2) $(FORMAT_UNPACK) $< > $@
diff --git a/mesalib/src/mesa/Android.libmesa_dricore.mk b/mesalib/src/mesa/Android.libmesa_dricore.mk
index e4a52677c..2e308b837 100644
--- a/mesalib/src/mesa/Android.libmesa_dricore.mk
+++ b/mesalib/src/mesa/Android.libmesa_dricore.mk
@@ -49,21 +49,18 @@ endif # MESA_ENABLE_ASM
 
 ifeq ($(ARCH_X86_HAVE_SSE4_1),true)
 LOCAL_SRC_FILES += \
-	$(SRCDIR)main/streaming-load-memcpy.c \
-	$(SRCDIR)main/sse_minmax.c
-LOCAL_CFLAGS := -msse4.1
-endif
-
-ifeq ($(ARCH_X86_HAVE_SSE4_1),true)
-LOCAL_CFLAGS += \
+	main/streaming-load-memcpy.c \
+	mesa/main/sse_minmax.c
+LOCAL_CFLAGS := \
+	-msse4.1 \
        -DUSE_SSE41
 endif
 
 LOCAL_C_INCLUDES := \
-	$(call intermediates-dir-for STATIC_LIBRARIES,libmesa_program,,) \
-	$(MESA_TOP)/src \
 	$(MESA_TOP)/src/mapi \
+	$(MESA_TOP)/src/mesa/main \
 	$(MESA_TOP)/src/glsl \
+	$(MESA_TOP)/src/gallium/include \
 	$(MESA_TOP)/src/gallium/auxiliary
 
 LOCAL_WHOLE_STATIC_LIBRARIES := \
diff --git a/mesalib/src/mesa/Android.libmesa_glsl_utils.mk b/mesalib/src/mesa/Android.libmesa_glsl_utils.mk
index a9f6ff59a..3497377af 100644
--- a/mesalib/src/mesa/Android.libmesa_glsl_utils.mk
+++ b/mesalib/src/mesa/Android.libmesa_glsl_utils.mk
@@ -36,9 +36,10 @@ include $(CLEAR_VARS)
 LOCAL_MODULE := libmesa_glsl_utils
 
 LOCAL_C_INCLUDES := \
-	$(MESA_TOP)/src \
 	$(MESA_TOP)/src/glsl \
-	$(MESA_TOP)/src/mapi
+	$(MESA_TOP)/src/mapi \
+	$(MESA_TOP)/src/gallium/include \
+	$(MESA_TOP)/src/gallium/auxiliary
 
 LOCAL_SRC_FILES := \
 	main/imports.c \
@@ -59,9 +60,10 @@ LOCAL_IS_HOST_MODULE := true
 LOCAL_CFLAGS := -D_POSIX_C_SOURCE=199309L
 
 LOCAL_C_INCLUDES := \
-	$(MESA_TOP)/src \
 	$(MESA_TOP)/src/glsl \
-	$(MESA_TOP)/src/mapi
+	$(MESA_TOP)/src/mapi \
+	$(MESA_TOP)/src/gallium/include \
+	$(MESA_TOP)/src/gallium/auxiliary
 
 LOCAL_SRC_FILES := \
 	main/imports.c \
diff --git a/mesalib/src/mesa/Android.libmesa_st_mesa.mk b/mesalib/src/mesa/Android.libmesa_st_mesa.mk
index a08366d53..b4b7fd977 100644
--- a/mesalib/src/mesa/Android.libmesa_st_mesa.mk
+++ b/mesalib/src/mesa/Android.libmesa_st_mesa.mk
@@ -52,12 +52,11 @@ LOCAL_CFLAGS := \
 endif
 
 LOCAL_C_INCLUDES := \
-	$(call intermediates-dir-for STATIC_LIBRARIES,libmesa_program,,) \
-	$(MESA_TOP)/src/gallium/auxiliary \
-	$(MESA_TOP)/src/gallium/include \
-	$(MESA_TOP)/src \
+	$(MESA_TOP)/src/mapi \
+	$(MESA_TOP)/src/mesa/main \
 	$(MESA_TOP)/src/glsl \
-	$(MESA_TOP)/src/mapi
+	$(MESA_TOP)/src/gallium/auxiliary \
+	$(MESA_TOP)/src/gallium/include
 
 LOCAL_WHOLE_STATIC_LIBRARIES := \
 	libmesa_program
diff --git a/mesalib/src/mesa/Android.mesa_gen_matypes.mk b/mesalib/src/mesa/Android.mesa_gen_matypes.mk
index 5521087c4..6e301f904 100644
--- a/mesalib/src/mesa/Android.mesa_gen_matypes.mk
+++ b/mesalib/src/mesa/Android.mesa_gen_matypes.mk
@@ -33,7 +33,6 @@ include $(CLEAR_VARS)
 
 LOCAL_MODULE := mesa_gen_matypes
 LOCAL_IS_HOST_MODULE := true
-LOCAL_CFLAGS := -D_POSIX_C_SOURCE=199309L
 
 LOCAL_C_INCLUDES := \
 	$(MESA_TOP)/src/mapi \
diff --git a/mesalib/src/mesa/drivers/common/meta.c b/mesalib/src/mesa/drivers/common/meta.c
index cf99d9563..d2ab7b8de 100644
--- a/mesalib/src/mesa/drivers/common/meta.c
+++ b/mesalib/src/mesa/drivers/common/meta.c
@@ -41,7 +41,6 @@
 #include "main/bufferobj.h"
 #include "main/buffers.h"
 #include "main/clear.h"
-#include "main/colortab.h"
 #include "main/condrender.h"
 #include "main/depth.h"
 #include "main/enable.h"
diff --git a/mesalib/src/mesa/drivers/dri/Android.mk b/mesalib/src/mesa/drivers/dri/Android.mk
index 935722c53..42bfd77d4 100644
--- a/mesalib/src/mesa/drivers/dri/Android.mk
+++ b/mesalib/src/mesa/drivers/dri/Android.mk
@@ -35,17 +35,17 @@ MESA_DRI_CFLAGS := \
 	-DHAVE_ANDROID_PLATFORM
 
 MESA_DRI_C_INCLUDES := \
-	$(MESA_TOP)/src \
-	$(call intermediates-dir-for,STATIC_LIBRARIES,libmesa_dri_common) \
 	$(addprefix $(MESA_TOP)/, $(mesa_dri_common_INCLUDES)) \
-	$(TARGET_OUT_HEADERS)/libdrm \
+	$(MESA_TOP)/src/gallium/include \
+	$(MESA_TOP)/src/gallium/auxiliary \
 	external/expat/lib
 
 MESA_DRI_WHOLE_STATIC_LIBRARIES := \
 	libmesa_glsl \
 	libmesa_megadriver_stub \
 	libmesa_dri_common \
-	libmesa_dricore
+	libmesa_dricore \
+	libmesa_util
 
 MESA_DRI_SHARED_LIBRARIES := \
 	libcutils \
@@ -55,9 +55,6 @@ MESA_DRI_SHARED_LIBRARIES := \
 	libglapi \
 	liblog
 
-# All DRI modules must add this to LOCAL_GENERATED_SOURCES.
-MESA_DRI_OPTIONS_H := $(call intermediates-dir-for,STATIC_LIBRARIES,libmesa_dri_common)/xmlpool/options.h
-
 #-----------------------------------------------
 # Build drivers and libmesa_dri_common
 
diff --git a/mesalib/src/mesa/drivers/dri/common/Android.mk b/mesalib/src/mesa/drivers/dri/common/Android.mk
index b95feb679..a7fcd6d57 100644
--- a/mesalib/src/mesa/drivers/dri/common/Android.mk
+++ b/mesalib/src/mesa/drivers/dri/common/Android.mk
@@ -34,16 +34,24 @@ include $(LOCAL_PATH)/Makefile.sources
 LOCAL_MODULE := libmesa_dri_common
 LOCAL_MODULE_CLASS := STATIC_LIBRARIES
 
-intermediates := $(call local-intermediates-dir)
+intermediates := $(call local-generated-sources-dir)
 
 LOCAL_C_INCLUDES := \
-    $(intermediates) \
     $(MESA_DRI_C_INCLUDES)
 
+LOCAL_EXPORT_C_INCLUDE_DIRS := $(intermediates)
+
+# swrast only
+ifeq ($(MESA_GPU_DRIVERS),swrast)
+LOCAL_CFLAGS := -D__NOT_HAVE_DRM_H
+else
+LOCAL_SHARED_LIBRARIES := libdrm
+endif
+
 LOCAL_SRC_FILES := $(DRI_COMMON_FILES)
 
-LOCAL_GENERATED_SOURCES := \
-    $(intermediates)/xmlpool/options.h
+MESA_DRI_OPTIONS_H := $(intermediates)/xmlpool/options.h
+LOCAL_GENERATED_SOURCES := $(MESA_DRI_OPTIONS_H)
 
 #
 # Generate options.h from gettext translations.
@@ -73,15 +81,14 @@ $(intermediates)/xmlpool/%/LC_MESSAGES/options.mo: $(intermediates)/xmlpool/%.po
 	mkdir -p $(dir $@)
 	msgfmt -o $@ $<
 
-$(intermediates)/xmlpool/options.h: PRIVATE_SCRIPT := $(LOCAL_PATH)/xmlpool/gen_xmlpool.py
-$(intermediates)/xmlpool/options.h: PRIVATE_LOCALEDIR := $(intermediates)/xmlpool
-$(intermediates)/xmlpool/options.h: PRIVATE_TEMPLATE_HEADER := $(LOCAL_PATH)/xmlpool/t_options.h
-$(intermediates)/xmlpool/options.h: PRIVATE_MO_FILES := $(MESA_DRI_OPTIONS_LANGS:%=$(intermediates)/xmlpool/%/LC_MESSAGES/options.mo)
+$(MESA_DRI_OPTIONS_H): PRIVATE_SCRIPT := $(LOCAL_PATH)/xmlpool/gen_xmlpool.py
+$(MESA_DRI_OPTIONS_H): PRIVATE_LOCALEDIR := $(intermediates)/xmlpool
+$(MESA_DRI_OPTIONS_H): PRIVATE_TEMPLATE_HEADER := $(LOCAL_PATH)/xmlpool/t_options.h
+$(MESA_DRI_OPTIONS_H): PRIVATE_MO_FILES := $(MESA_DRI_OPTIONS_LANGS:%=$(intermediates)/xmlpool/%/LC_MESSAGES/options.mo)
 .SECONDEXPANSION:
-$(intermediates)/xmlpool/options.h: $$(PRIVATE_SCRIPT) $$(PRIVATE_TEMPLATE_HEADER) $$(PRIVATE_MO_FILES)
-	mkdir -p $(dir $@)
-	mkdir -p $(PRIVATE_LOCALEDIR)
-	$(MESA_PYTHON2) $(PRIVATE_SCRIPT) $(PRIVATE_TEMPLATE_HEADER) \
+$(MESA_DRI_OPTIONS_H): $$(PRIVATE_SCRIPT) $$(PRIVATE_TEMPLATE_HEADER) $$(PRIVATE_MO_FILES)
+	@mkdir -p $(dir $@)
+	$(hide) $(MESA_PYTHON2) $(PRIVATE_SCRIPT) $(PRIVATE_TEMPLATE_HEADER) \
 		$(PRIVATE_LOCALEDIR) $(MESA_DRI_OPTIONS_LANGS) > $@
 
 include $(MESA_COMMON_MK)
@@ -99,6 +106,13 @@ LOCAL_MODULE_CLASS := STATIC_LIBRARIES
 LOCAL_C_INCLUDES := \
     $(MESA_DRI_C_INCLUDES)
 
+# swrast only
+ifeq ($(MESA_GPU_DRIVERS),swrast)
+LOCAL_CFLAGS := -D__NOT_HAVE_DRM_H
+else
+LOCAL_SHARED_LIBRARIES := libdrm
+endif
+
 LOCAL_SRC_FILES := $(megadriver_stub_FILES)
 
 include $(MESA_COMMON_MK)
diff --git a/mesalib/src/mesa/drivers/dri/common/drirc b/mesalib/src/mesa/drivers/dri/common/drirc
index cecd6a953..145e707a6 100644
--- a/mesalib/src/mesa/drivers/dri/common/drirc
+++ b/mesalib/src/mesa/drivers/dri/common/drirc
@@ -91,5 +91,9 @@ TODO: document the other workarounds.
         <application name="Dead Island" executable="DeadIslandGame">
             <option name="allow_glsl_extension_directive_midshader" value="true" />
         </application>
+
+        <application name="Second Life" executable="do-not-directly-run-secondlife-bin">
+            <option name="allow_glsl_extension_directive_midshader" value="true" />
+        </application>
     </device>
 </driconf>
diff --git a/mesalib/src/mesa/main/compiler.h b/mesalib/src/mesa/main/compiler.h
index 55152fdef..93b4e6f12 100644
--- a/mesalib/src/mesa/main/compiler.h
+++ b/mesalib/src/mesa/main/compiler.h
@@ -60,11 +60,6 @@ extern "C" {
 #endif
 
 
-/* XXX: Use standard `__func__` instead */
-#ifndef __FUNCTION__
-#  define __FUNCTION__ __func__
-#endif
-
 /**
  * Either define MESA_BIG_ENDIAN or MESA_LITTLE_ENDIAN, and CPU_TO_LE32.
  * Do not use these unless absolutely necessary!
diff --git a/mesalib/src/mesa/main/context.c b/mesalib/src/mesa/main/context.c
index adf64976e..4aaf8b1af 100644
--- a/mesalib/src/mesa/main/context.c
+++ b/mesalib/src/mesa/main/context.c
@@ -894,14 +894,14 @@ update_default_objects(struct gl_context *ctx)
  * If there's no current OpenGL context for the calling thread, we can
  * print a message to stderr.
  *
- * \param name  the name of the OpenGL function, without the "gl" prefix
+ * \param name  the name of the OpenGL function
  */
 static void
 nop_handler(const char *name)
 {
    GET_CURRENT_CONTEXT(ctx);
    if (ctx) {
-      _mesa_error(ctx, GL_INVALID_OPERATION, "gl%s(invalid call)", name);
+      _mesa_error(ctx, GL_INVALID_OPERATION, "%s(invalid call)", name);
    }
 #if defined(DEBUG)
    else if (getenv("MESA_DEBUG") || getenv("LIBGL_DEBUG")) {
@@ -936,7 +936,7 @@ alloc_dispatch_table(void)
 {
    /* Find the larger of Mesa's dispatch table and libGL's dispatch table.
     * In practice, this'll be the same for stand-alone Mesa.  But for DRI
-    * Mesa we do this to accomodate different versions of libGL and various
+    * Mesa we do this to accommodate different versions of libGL and various
     * DRI drivers.
     */
    GLint numEntries = MAX2(_glapi_get_dispatch_table_size(), _gloffset_COUNT);
diff --git a/mesalib/src/mesa/main/extensions.c b/mesalib/src/mesa/main/extensions.c
index 861b15006..9be8993b0 100644
--- a/mesalib/src/mesa/main/extensions.c
+++ b/mesalib/src/mesa/main/extensions.c
@@ -186,6 +186,7 @@ static const struct extension extension_table[] = {
    { "GL_ARB_texture_rectangle",                   o(NV_texture_rectangle),                    GL,             2004 },
    { "GL_ARB_texture_rgb10_a2ui",                  o(ARB_texture_rgb10_a2ui),                  GL,             2009 },
    { "GL_ARB_texture_rg",                          o(ARB_texture_rg),                          GL,             2008 },
+   { "GL_ARB_texture_stencil8",                    o(ARB_texture_stencil8),                    GL,             2013 },
    { "GL_ARB_texture_storage",                     o(dummy_true),                              GL,             2011 },
    { "GL_ARB_texture_storage_multisample",         o(ARB_texture_multisample),                 GL,             2012 },
    { "GL_ARB_texture_view",                        o(ARB_texture_view),                        GL,             2012 },
@@ -297,6 +298,7 @@ static const struct extension extension_table[] = {
    { "GL_OES_depth_texture",                       o(ARB_depth_texture),                                  ES2, 2006 },
    { "GL_OES_depth_texture_cube_map",              o(OES_depth_texture_cube_map),                         ES2, 2012 },
    { "GL_OES_draw_texture",                        o(OES_draw_texture),                             ES1,       2004 },
+   { "GL_OES_EGL_sync",                            o(dummy_true),                                   ES1 | ES2, 2010 },
    /*  FIXME: Mesa expects GL_OES_EGL_image to be available in OpenGL contexts. */
    { "GL_OES_EGL_image",                           o(OES_EGL_image),                           GL | ES1 | ES2, 2006 },
    { "GL_OES_EGL_image_external",                  o(OES_EGL_image_external),                       ES1 | ES2, 2010 },
diff --git a/mesalib/src/mesa/main/fbobject.c b/mesalib/src/mesa/main/fbobject.c
index 8032585ab..27cf97f17 100644
--- a/mesalib/src/mesa/main/fbobject.c
+++ b/mesalib/src/mesa/main/fbobject.c
@@ -813,8 +813,10 @@ test_attachment_completeness(const struct gl_context *ctx, GLenum format,
          if (ctx->Extensions.ARB_depth_texture &&
              baseFormat == GL_DEPTH_STENCIL) {
             /* OK */
-         }
-         else {
+         } else if (ctx->Extensions.ARB_texture_stencil8 &&
+                    baseFormat == GL_STENCIL_INDEX) {
+            /* OK */
+         } else {
             /* no such thing as stencil-only textures */
             att_incomplete("illegal stencil texture");
             att->Complete = GL_FALSE;
@@ -978,7 +980,8 @@ _mesa_test_framebuffer_completeness(struct gl_context *ctx,
 
          if (!is_format_color_renderable(ctx, attFormat,
                                          texImg->InternalFormat) &&
-             !is_legal_depth_format(ctx, f)) {
+             !is_legal_depth_format(ctx, f) &&
+             f != GL_STENCIL_INDEX) {
             fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
             fbo_incomplete(ctx, "texture attachment incomplete", -1);
             return;
diff --git a/mesalib/src/mesa/main/ffvertex_prog.c b/mesalib/src/mesa/main/ffvertex_prog.c
index edf7e3390..7fdd9bab6 100644
--- a/mesalib/src/mesa/main/ffvertex_prog.c
+++ b/mesalib/src/mesa/main/ffvertex_prog.c
@@ -1657,7 +1657,7 @@ _mesa_get_fixed_func_vertex_program(struct gl_context *ctx)
    struct gl_vertex_program *prog;
    struct state_key key;
 
-   /* Grab all the relevent state and put it in a single structure:
+   /* Grab all the relevant state and put it in a single structure:
     */
    make_state_key(ctx, &key);
 
diff --git a/mesalib/src/mesa/main/formats.c b/mesalib/src/mesa/main/formats.c
index 2bc8bcad9..8af44e905 100644
--- a/mesalib/src/mesa/main/formats.c
+++ b/mesalib/src/mesa/main/formats.c
@@ -811,7 +811,7 @@ _mesa_format_image_size(mesa_format format, GLsizei width,
 
 /**
  * Same as _mesa_format_image_size() but returns a 64-bit value to
- * accomodate very large textures.
+ * accommodate very large textures.
  */
 uint64_t
 _mesa_format_image_size64(mesa_format format, GLsizei width,
diff --git a/mesalib/src/mesa/main/macros.h b/mesalib/src/mesa/main/macros.h
index 3344ec841..0608650ae 100644
--- a/mesalib/src/mesa/main/macros.h
+++ b/mesalib/src/mesa/main/macros.h
@@ -31,6 +31,7 @@
 #ifndef MACROS_H
 #define MACROS_H
 
+#include "util/macros.h"
 #include "util/u_math.h"
 #include "imports.h"
 
@@ -800,10 +801,6 @@ DIFFERENT_SIGNS(GLfloat x, GLfloat y)
 }
 
 
-/** Compute ceiling of integer quotient of A divided by B. */
-#define DIV_ROUND_UP( A, B )  ( (A) % (B) == 0 ? (A)/(B) : (A)/(B)+1 )
-
-
 /** casts to silence warnings with some compilers */
 #define ENUM_TO_INT(E)     ((GLint)(E))
 #define ENUM_TO_FLOAT(E)   ((GLfloat)(GLint)(E))
diff --git a/mesalib/src/mesa/main/mtypes.h b/mesalib/src/mesa/main/mtypes.h
index 1c751cfff..fb4143086 100644
--- a/mesalib/src/mesa/main/mtypes.h
+++ b/mesalib/src/mesa/main/mtypes.h
@@ -2365,16 +2365,6 @@ struct gl_ati_fragment_shader_state
 };
 
 
-/** Set by #pragma directives */
-struct gl_sl_pragmas
-{
-   GLboolean IgnoreOptimize;  /**< ignore #pragma optimize(on/off) ? */
-   GLboolean IgnoreDebug;     /**< ignore #pragma debug(on/off) ? */
-   GLboolean Optimize;  /**< defaults on */
-   GLboolean Debug;     /**< defaults off */
-};
-
-
 /**
  * A GLSL vertex or fragment shader object.
  */
@@ -2397,7 +2387,6 @@ struct gl_shader
 
    struct gl_program *Program;  /**< Post-compile assembly code */
    GLchar *InfoLog;
-   struct gl_sl_pragmas Pragmas;
 
    unsigned Version;       /**< GLSL version used for linking */
 
@@ -2903,8 +2892,6 @@ struct gl_shader_compiler_options
     */
    GLboolean OptimizeForAOS;
 
-   struct gl_sl_pragmas DefaultPragmas; /**< Default #pragma settings */
-
    const struct nir_shader_compiler_options *NirOptions;
 };
 
diff --git a/mesalib/src/mesa/main/shader_query.cpp b/mesalib/src/mesa/main/shader_query.cpp
index b5f1d082c..d2ca49b43 100644
--- a/mesalib/src/mesa/main/shader_query.cpp
+++ b/mesalib/src/mesa/main/shader_query.cpp
@@ -291,7 +291,6 @@ _mesa_GetAttribLocation(GLhandleARB program, const GLcharARB * name)
    return (loc >= 0) ? loc : -1;
 }
 
-
 unsigned
 _mesa_count_active_attribs(struct gl_shader_program *shProg)
 {
@@ -300,19 +299,15 @@ _mesa_count_active_attribs(struct gl_shader_program *shProg)
       return 0;
    }
 
-   exec_list *const ir = shProg->_LinkedShaders[MESA_SHADER_VERTEX]->ir;
-   unsigned i = 0;
-
-   foreach_in_list(ir_instruction, node, ir) {
-      const ir_variable *const var = node->as_variable();
-
-      if (!is_active_attrib(var))
-         continue;
-
-      i++;
+   struct gl_program_resource *res = shProg->ProgramResourceList;
+   unsigned count = 0;
+   for (unsigned j = 0; j < shProg->NumProgramResourceList; j++, res++) {
+      if (res->Type == GL_PROGRAM_INPUT &&
+          res->StageReferences & (1 << MESA_SHADER_VERTEX) &&
+          is_active_attrib(RESOURCE_VAR(res)))
+         count++;
    }
-
-   return i;
+   return count;
 }
 
 
@@ -324,20 +319,16 @@ _mesa_longest_attribute_name_length(struct gl_shader_program *shProg)
       return 0;
    }
 
-   exec_list *const ir = shProg->_LinkedShaders[MESA_SHADER_VERTEX]->ir;
+   struct gl_program_resource *res = shProg->ProgramResourceList;
    size_t longest = 0;
+   for (unsigned j = 0; j < shProg->NumProgramResourceList; j++, res++) {
+      if (res->Type == GL_PROGRAM_INPUT &&
+          res->StageReferences & (1 << MESA_SHADER_VERTEX)) {
 
-   foreach_in_list(ir_instruction, node, ir) {
-      const ir_variable *const var = node->as_variable();
-
-      if (var == NULL
-	  || var->data.mode != ir_var_shader_in
-	  || var->data.location == -1)
-	 continue;
-
-      const size_t len = strlen(var->name);
-      if (len >= longest)
-	 longest = len + 1;
+          const size_t length = strlen(RESOURCE_VAR(res)->name);
+          if (length >= longest)
+             longest = length + 1;
+      }
    }
 
    return longest;
@@ -537,6 +528,7 @@ array_index_of_resource(struct gl_program_resource *res,
       return get_matching_index(RESOURCE_VAR(res), name);
    default:
       assert(!"support for resource type not implemented");
+      return -1;
    }
 }
 
@@ -634,7 +626,7 @@ _mesa_program_resource_find_index(struct gl_shader_program *shProg,
       case GL_ATOMIC_COUNTER_BUFFER:
          if (_mesa_program_resource_index(shProg, res) == index)
             return res;
-
+         break;
       case GL_TRANSFORM_FEEDBACK_VARYING:
       case GL_PROGRAM_INPUT:
       case GL_PROGRAM_OUTPUT:
@@ -860,13 +852,23 @@ get_buffer_property(struct gl_shader_program *shProg,
          *val = RESOURCE_UBO(res)->UniformBufferSize;
          return 1;
       case GL_NUM_ACTIVE_VARIABLES:
-         *val = RESOURCE_UBO(res)->NumUniforms;
+         *val = 0;
+         for (unsigned i = 0; i < RESOURCE_UBO(res)->NumUniforms; i++) {
+            const char *iname = RESOURCE_UBO(res)->Uniforms[i].IndexName;
+            struct gl_program_resource *uni =
+               _mesa_program_resource_find_name(shProg, GL_UNIFORM, iname);
+            if (!uni)
+               continue;
+            (*val)++;
+         }
          return 1;
       case GL_ACTIVE_VARIABLES:
          for (unsigned i = 0; i < RESOURCE_UBO(res)->NumUniforms; i++) {
             const char *iname = RESOURCE_UBO(res)->Uniforms[i].IndexName;
             struct gl_program_resource *uni =
                _mesa_program_resource_find_name(shProg, GL_UNIFORM, iname);
+            if (!uni)
+               continue;
             *val++ =
                _mesa_program_resource_index(shProg, uni);
          }
diff --git a/mesalib/src/mesa/main/shaderapi.c b/mesalib/src/mesa/main/shaderapi.c
index 77e2b8745..a04b28711 100644
--- a/mesalib/src/mesa/main/shaderapi.c
+++ b/mesalib/src/mesa/main/shaderapi.c
@@ -115,9 +115,6 @@ _mesa_init_shader_state(struct gl_context *ctx)
    options.MaxUnrollIterations = 32;
    options.MaxIfDepth = UINT_MAX;
 
-   /* Default pragma settings */
-   options.DefaultPragmas.Optimize = GL_TRUE;
-
    for (sh = 0; sh < MESA_SHADER_STAGES; ++sh)
       memcpy(&ctx->Const.ShaderCompilerOptions[sh], &options, sizeof(options));
 
@@ -864,17 +861,11 @@ static void
 compile_shader(struct gl_context *ctx, GLuint shaderObj)
 {
    struct gl_shader *sh;
-   struct gl_shader_compiler_options *options;
 
    sh = _mesa_lookup_shader_err(ctx, shaderObj, "glCompileShader");
    if (!sh)
       return;
 
-   options = &ctx->Const.ShaderCompilerOptions[sh->Stage];
-
-   /* set default pragma state for shader */
-   sh->Pragmas = options->DefaultPragmas;
-
    if (!sh->Source) {
       /* If the user called glCompileShader without first calling
        * glShaderSource, we should fail to compile, but not raise a GL_ERROR.
diff --git a/mesalib/src/mesa/main/texgetimage.c b/mesalib/src/mesa/main/texgetimage.c
index 255d36559..92b4d6795 100644
--- a/mesalib/src/mesa/main/texgetimage.c
+++ b/mesalib/src/mesa/main/texgetimage.c
@@ -175,6 +175,51 @@ get_tex_depth_stencil(struct gl_context *ctx, GLuint dimensions,
    }
 }
 
+/**
+ * glGetTexImage for stencil pixels.
+ */
+static void
+get_tex_stencil(struct gl_context *ctx, GLuint dimensions,
+                GLenum format, GLenum type, GLvoid *pixels,
+                struct gl_texture_image *texImage)
+{
+   const GLint width = texImage->Width;
+   const GLint height = texImage->Height;
+   const GLint depth = texImage->Depth;
+   GLint img, row;
+
+   assert(format == GL_STENCIL_INDEX);
+
+   for (img = 0; img < depth; img++) {
+      GLubyte *srcMap;
+      GLint rowstride;
+
+      /* map src texture buffer */
+      ctx->Driver.MapTextureImage(ctx, texImage, img,
+                                  0, 0, width, height, GL_MAP_READ_BIT,
+                                  &srcMap, &rowstride);
+
+      if (srcMap) {
+         for (row = 0; row < height; row++) {
+            const GLubyte *src = srcMap + row * rowstride;
+            void *dest = _mesa_image_address(dimensions, &ctx->Pack, pixels,
+                                             width, height, format, type,
+                                             img, row, 0);
+            _mesa_unpack_ubyte_stencil_row(texImage->TexFormat,
+                                           width,
+                                           (const GLuint *) src,
+                                           dest);
+         }
+
+         ctx->Driver.UnmapTextureImage(ctx, texImage, img);
+      }
+      else {
+         _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetTexImage");
+         break;
+      }
+   }
+}
+
 
 /**
  * glGetTexImage for YCbCr pixels.
@@ -285,7 +330,7 @@ get_tex_rgba_compressed(struct gl_context *ctx, GLuint dimensions,
    }
 
    /* Depending on the base format involved we may need to apply a rebase
-    * tranaform (for example: if we download to a Luminance format we want
+    * transform (for example: if we download to a Luminance format we want
     * G=0 and B=0).
     */
    if (baseFormat == GL_LUMINANCE ||
@@ -388,7 +433,7 @@ get_tex_rgba_uncompressed(struct gl_context *ctx, GLuint dimensions,
    }
 
    /* Depending on the base format involved we may need to apply a rebase
-    * tranaform (for example: if we download to a Luminance format we want
+    * transform (for example: if we download to a Luminance format we want
     * G=0 and B=0).
     */
    if (texImage->_BaseFormat == GL_LUMINANCE ||
@@ -684,6 +729,9 @@ _mesa_GetTexImage_sw(struct gl_context *ctx,
    else if (format == GL_DEPTH_STENCIL_EXT) {
       get_tex_depth_stencil(ctx, dimensions, format, type, pixels, texImage);
    }
+   else if (format == GL_STENCIL_INDEX) {
+      get_tex_stencil(ctx, dimensions, format, type, pixels, texImage);
+   }
    else if (format == GL_YCBCR_MESA) {
       get_tex_ycbcr(ctx, dimensions, format, type, pixels, texImage);
    }
@@ -879,7 +927,7 @@ getteximage_error_check(struct gl_context *ctx,
                   "glGetTex%sImage(format mismatch)", suffix);
       return GL_TRUE;
    }
-   else if (_mesa_is_enum_format_integer(format) !=
+   else if (!_mesa_is_stencil_format(format) && _mesa_is_enum_format_integer(format) !=
             _mesa_is_format_integer(texImage->TexFormat)) {
       _mesa_error(ctx, GL_INVALID_OPERATION,
                   "glGetTex%sImage(format mismatch)", suffix);
diff --git a/mesalib/src/mesa/main/teximage.c b/mesalib/src/mesa/main/teximage.c
index 8d9d7cfc1..7bc1da7f8 100644
--- a/mesalib/src/mesa/main/teximage.c
+++ b/mesalib/src/mesa/main/teximage.c
@@ -1997,7 +1997,8 @@ _mesa_legal_texture_base_format_for_target(struct gl_context *ctx,
                                            const char *caller)
 {
    if (_mesa_base_tex_format(ctx, internalFormat) == GL_DEPTH_COMPONENT
-       || _mesa_base_tex_format(ctx, internalFormat) == GL_DEPTH_STENCIL) {
+       || _mesa_base_tex_format(ctx, internalFormat) == GL_DEPTH_STENCIL
+       || _mesa_base_tex_format(ctx, internalFormat) == GL_STENCIL_INDEX) {
       /* Section 3.8.3 (Texture Image Specification) of the OpenGL 3.3 Core
        * Profile spec says:
        *
@@ -3319,7 +3320,9 @@ teximage(struct gl_context *ctx, GLboolean compressed, GLuint dims,
 
       if (!sizeOK) {
          _mesa_error(ctx, GL_OUT_OF_MEMORY,
-                     "glTexImage%uD(image too large)", dims);
+                     "glTexImage%uD(image too large: %d x %d x %d, %s format)",
+                     dims, width, height, depth,
+                     _mesa_lookup_enum_by_nr(internalFormat));
          return;
       }
 
diff --git a/mesalib/src/mesa/main/texobj.c b/mesalib/src/mesa/main/texobj.c
index e018ab910..c563f1e74 100644
--- a/mesalib/src/mesa/main/texobj.c
+++ b/mesalib/src/mesa/main/texobj.c
@@ -30,7 +30,6 @@
 
 #include <stdio.h>
 #include "bufferobj.h"
-#include "colortab.h"
 #include "context.h"
 #include "enums.h"
 #include "fbobject.h"
diff --git a/mesalib/src/mesa/main/texturebarrier.c b/mesalib/src/mesa/main/texturebarrier.c
index 08ff56150..d879eed57 100644
--- a/mesalib/src/mesa/main/texturebarrier.c
+++ b/mesalib/src/mesa/main/texturebarrier.c
@@ -49,5 +49,11 @@ _mesa_TextureBarrierNV(void)
 {
    GET_CURRENT_CONTEXT(ctx);
 
+   if (!ctx->Extensions.NV_texture_barrier) {
+      _mesa_error(ctx, GL_INVALID_OPERATION,
+                  "glTextureBarrier(not supported)");
+      return;
+   }
+
    ctx->Driver.TextureBarrier(ctx);
 }
diff --git a/mesalib/src/mesa/main/uniform_query.cpp b/mesalib/src/mesa/main/uniform_query.cpp
index 4e77b3284..3e857ed10 100644
--- a/mesalib/src/mesa/main/uniform_query.cpp
+++ b/mesalib/src/mesa/main/uniform_query.cpp
@@ -129,14 +129,26 @@ _mesa_GetActiveUniformsiv(GLuint program,
 
    res_prop = resource_prop_from_uniform_prop(pname);
 
+   /* We need to first verify that each entry exists as active uniform. If
+    * not, generate error and do not cause any other side effects.
+    *
+    * In the case of and error condition, Page 16 (section 2.3.1 Errors)
+    * of the OpenGL 4.5 spec says:
+    *
+    *     "If the generating command modifies values through a pointer argu-
+    *     ment, no change is made to these values."
+    */
    for (int i = 0; i < uniformCount; i++) {
-      res = _mesa_program_resource_find_index(shProg, GL_UNIFORM,
-                                              uniformIndices[i]);
-      if (!res) {
+      if (!_mesa_program_resource_find_index(shProg, GL_UNIFORM,
+                                              uniformIndices[i])) {
          _mesa_error(ctx, GL_INVALID_VALUE, "glGetActiveUniformsiv(index)");
-         break;
+         return;
       }
+   }
 
+   for (int i = 0; i < uniformCount; i++) {
+      res = _mesa_program_resource_find_index(shProg, GL_UNIFORM,
+                                              uniformIndices[i]);
       if (!_mesa_program_resource_prop(shProg, res, uniformIndices[i],
                                        res_prop, &params[i],
                                        "glGetActiveUniformsiv"))
diff --git a/mesalib/src/mesa/main/version.c b/mesalib/src/mesa/main/version.c
index 7c6d99418..5b8ac0a5c 100644
--- a/mesalib/src/mesa/main/version.c
+++ b/mesalib/src/mesa/main/version.c
@@ -295,7 +295,51 @@ compute_version(const struct gl_extensions *extensions,
                               extensions->EXT_texture_swizzle);
                               /* ARB_sampler_objects is always enabled in mesa */
 
-   if (ver_3_3) {
+   const GLboolean ver_4_0 = (ver_3_3 &&
+                              consts->GLSLVersion >= 400 &&
+                              extensions->ARB_draw_buffers_blend &&
+                              extensions->ARB_draw_indirect &&
+                              extensions->ARB_gpu_shader5 &&
+                              extensions->ARB_gpu_shader_fp64 &&
+                              extensions->ARB_sample_shading &&
+                              0/*extensions->ARB_shader_subroutine*/ &&
+                              extensions->ARB_tessellation_shader &&
+                              extensions->ARB_texture_buffer_object_rgb32 &&
+                              extensions->ARB_texture_cube_map_array &&
+                              extensions->ARB_texture_query_lod &&
+                              extensions->ARB_transform_feedback2 &&
+                              extensions->ARB_transform_feedback3);
+   const GLboolean ver_4_1 = (ver_4_0 &&
+                              consts->GLSLVersion >= 410 &&
+                              extensions->ARB_ES2_compatibility &&
+                              extensions->ARB_shader_precision &&
+                              0/*extensions->ARB_vertex_attrib_64bit*/ &&
+                              extensions->ARB_viewport_array);
+   const GLboolean ver_4_2 = (ver_4_1 &&
+                              consts->GLSLVersion >= 420 &&
+                              extensions->ARB_base_instance &&
+                              extensions->ARB_conservative_depth &&
+                              extensions->ARB_internalformat_query &&
+                              extensions->ARB_shader_atomic_counters &&
+                              extensions->ARB_shader_image_load_store &&
+                              extensions->ARB_shading_language_420pack &&
+                              extensions->ARB_shading_language_packing &&
+                              extensions->ARB_texture_compression_bptc &&
+                              extensions->ARB_transform_feedback_instanced);
+
+   if (ver_4_2) {
+      major = 4;
+      minor = 2;
+   }
+   else if (ver_4_1) {
+      major = 4;
+      minor = 1;
+   }
+   else if (ver_4_0) {
+      major = 4;
+      minor = 0;
+   }
+   else if (ver_3_3) {
       major = 3;
       minor = 3;
    }
@@ -439,6 +483,23 @@ _mesa_compute_version(struct gl_context *ctx)
 
    ctx->Version = _mesa_get_version(&ctx->Extensions, &ctx->Const, ctx->API);
 
+   /* Make sure that the GLSL version lines up with the GL version. In some
+    * cases it can be too high, e.g. if an extension is missing.
+    */
+   if (ctx->API == API_OPENGL_CORE) {
+      switch (ctx->Version) {
+      case 31:
+         ctx->Const.GLSLVersion = 140;
+         break;
+      case 32:
+         ctx->Const.GLSLVersion = 150;
+         break;
+      default:
+         ctx->Const.GLSLVersion = ctx->Version * 10;
+         break;
+      }
+   }
+
    switch (ctx->API) {
    case API_OPENGL_COMPAT:
    case API_OPENGL_CORE:
diff --git a/mesalib/src/mesa/math/m_matrix.c b/mesalib/src/mesa/math/m_matrix.c
index 0475a7ab2..ecf564c00 100644
--- a/mesalib/src/mesa/math/m_matrix.c
+++ b/mesalib/src/mesa/math/m_matrix.c
@@ -50,7 +50,7 @@
 /*@{*/
 #define MAT_FLAG_IDENTITY       0     /**< is an identity matrix flag.
                                        *   (Not actually used - the identity
-                                       *   matrix is identified by the absense
+                                       *   matrix is identified by the absence
                                        *   of all other flags.)
                                        */
 #define MAT_FLAG_GENERAL        0x1   /**< is a general matrix flag */
diff --git a/mesalib/src/mesa/program/Android.mk b/mesalib/src/mesa/program/Android.mk
index a237b65bc..ccb0fa5f3 100644
--- a/mesalib/src/mesa/program/Android.mk
+++ b/mesalib/src/mesa/program/Android.mk
@@ -44,8 +44,9 @@ include $(CLEAR_VARS)
 
 LOCAL_MODULE := libmesa_program
 LOCAL_MODULE_CLASS := STATIC_LIBRARIES
+LOCAL_STATIC_LIBRARIES := libmesa_glsl
 
-intermediates := $(call local-intermediates-dir)
+intermediates := $(call local-generated-sources-dir)
 
 # TODO(chadv): In Makefile.sources, move these vars to a different list so we can
 # remove this kludge.
@@ -55,7 +56,8 @@ generated_sources_basenames := \
 	program_parse.tab.h
 
 LOCAL_SRC_FILES := \
-	$(filter-out $(generated_sources_basenames),$(subst program/,,$(PROGRAM_FILES)))
+	$(filter-out $(generated_sources_basenames),$(subst program/,,$(PROGRAM_FILES))) \
+	$(subst program/,,$(PROGRAM_NIR_FILES))
 
 LOCAL_GENERATED_SOURCES := \
 	$(addprefix $(intermediates)/program/,$(generated_sources_basenames))
@@ -70,11 +72,13 @@ $(intermediates)/program/lex.yy.c: $(LOCAL_PATH)/program_lexer.l
 	$(local-l-to-c)
 
 LOCAL_C_INCLUDES := \
-	$(intermediates) \
-	$(MESA_TOP)/src \
 	$(MESA_TOP)/src/mapi \
 	$(MESA_TOP)/src/mesa \
-	$(MESA_TOP)/src/glsl
+	$(MESA_TOP)/src/glsl \
+	$(MESA_TOP)/src/gallium/auxiliary \
+	$(MESA_TOP)/src/gallium/include
+
+LOCAL_EXPORT_C_INCLUDE_DIRS := $(intermediates)
 
 include $(MESA_COMMON_MK)
 include $(BUILD_STATIC_LIBRARY)
diff --git a/mesalib/src/mesa/program/hash_table.h b/mesalib/src/mesa/program/hash_table.h
index eed2e55dc..e85a836a0 100644
--- a/mesalib/src/mesa/program/hash_table.h
+++ b/mesalib/src/mesa/program/hash_table.h
@@ -116,8 +116,8 @@ extern void hash_table_insert(struct hash_table *ht, void *data,
  * Add an element to a hash table with replacement
  *
  * \return
- * 1 if it did replace the the value (in which case the old key is kept), 0 if
- * it did not replace the value (in which case the new key is kept).
+ * 1 if it did replace the value (in which case the old key is kept), 0 if it
+ * did not replace the value (in which case the new key is kept).
  *
  * \warning
  * If \c key is already in the hash table, \c data will \b replace the most
diff --git a/mesalib/src/mesa/program/prog_cache.c b/mesalib/src/mesa/program/prog_cache.c
index 34609f056..ed93af7f1 100644
--- a/mesalib/src/mesa/program/prog_cache.c
+++ b/mesalib/src/mesa/program/prog_cache.c
@@ -77,7 +77,7 @@ hash_key(const void *key, GLuint key_size)
 
 
 /**
- * Rebuild/expand the hash table to accomodate more entries
+ * Rebuild/expand the hash table to accommodate more entries
  */
 static void
 rehash(struct gl_program_cache *cache)
diff --git a/mesalib/src/mesa/program/prog_to_nir.c b/mesalib/src/mesa/program/prog_to_nir.c
index c738f5073..ff3d9f3be 100644
--- a/mesalib/src/mesa/program/prog_to_nir.c
+++ b/mesalib/src/mesa/program/prog_to_nir.c
@@ -222,12 +222,23 @@ ptn_get_src(struct ptn_compile *c, const struct prog_src_register *prog_src)
    }
 
    nir_ssa_def *def;
-   if (!HAS_EXTENDED_SWIZZLE(prog_src->Swizzle)) {
+   if (!HAS_EXTENDED_SWIZZLE(prog_src->Swizzle) &&
+       (prog_src->Negate == NEGATE_NONE || prog_src->Negate == NEGATE_XYZW)) {
+      /* The simple non-SWZ case. */
       for (int i = 0; i < 4; i++)
          src.swizzle[i] = GET_SWZ(prog_src->Swizzle, i);
 
       def = nir_fmov_alu(b, src, 4);
+
+      if (prog_src->Abs)
+         def = nir_fabs(b, def);
+
+      if (prog_src->Negate)
+         def = nir_fneg(b, def);
    } else {
+      /* The SWZ instruction allows per-component zero/one swizzles, and also
+       * per-component negation.
+       */
       nir_ssa_def *chans[4];
       for (int i = 0; i < 4; i++) {
          int swizzle = GET_SWZ(prog_src->Swizzle, i);
@@ -246,16 +257,16 @@ ptn_get_src(struct ptn_compile *c, const struct prog_src_register *prog_src)
 
             chans[i] = &mov->dest.dest.ssa;
          }
+
+         if (prog_src->Abs)
+            chans[i] = nir_fabs(b, chans[i]);
+
+         if (prog_src->Negate & (1 << i))
+            chans[i] = nir_fneg(b, chans[i]);
       }
       def = nir_vec4(b, chans[0], chans[1], chans[2], chans[3]);
    }
 
-   if (prog_src->Abs)
-      def = nir_fabs(b, def);
-
-   if (prog_src->Negate)
-      def = nir_fneg(b, def);
-
    return def;
 }
 
diff --git a/mesalib/src/mesa/state_tracker/st_atom_texture.c b/mesalib/src/mesa/state_tracker/st_atom_texture.c
index eff28fc6f..04ba86448 100644
--- a/mesalib/src/mesa/state_tracker/st_atom_texture.c
+++ b/mesalib/src/mesa/state_tracker/st_atom_texture.c
@@ -287,16 +287,22 @@ st_get_texture_sampler_view_from_stobj(struct st_context *st,
 				       enum pipe_format format)
 {
    struct pipe_sampler_view **sv;
-
+   const struct st_texture_image *firstImage;
    if (!stObj || !stObj->pt) {
       return NULL;
    }
 
    sv = st_texture_get_sampler_view(st, stObj);
 
-   if (stObj->base.StencilSampling &&
-       util_format_is_depth_and_stencil(format))
-      format = util_format_stencil_only(format);
+   if (util_format_is_depth_and_stencil(format)) {
+      if (stObj->base.StencilSampling)
+         format = util_format_stencil_only(format);
+      else {
+         firstImage = st_texture_image_const(_mesa_base_tex_image(&stObj->base));
+         if (firstImage->base._BaseFormat == GL_STENCIL_INDEX)
+            format = util_format_stencil_only(format);
+      }
+   }
 
    /* if sampler view has changed dereference it */
    if (*sv) {
diff --git a/mesalib/src/mesa/state_tracker/st_cb_flush.c b/mesalib/src/mesa/state_tracker/st_cb_flush.c
index 6df430049..ca51eeee3 100644
--- a/mesalib/src/mesa/state_tracker/st_cb_flush.c
+++ b/mesalib/src/mesa/state_tracker/st_cb_flush.c
@@ -152,7 +152,7 @@ void st_init_flush_functions(struct dd_function_table *functions)
     * Finish() is identical to Flush() in all cases - no differences in
     * rendering or ReadPixels are visible if we opt not to wait here.
     *
-    * Only set this up on windows to avoid suprise elsewhere.
+    * Only set this up on Windows to avoid surprise elsewhere.
     */
 #ifdef PIPE_OS_WINDOWS
    functions->Finish = st_glFlush;
diff --git a/mesalib/src/mesa/state_tracker/st_cb_texture.c b/mesalib/src/mesa/state_tracker/st_cb_texture.c
index bdf236e82..7ea3846ff 100644
--- a/mesalib/src/mesa/state_tracker/st_cb_texture.c
+++ b/mesalib/src/mesa/state_tracker/st_cb_texture.c
@@ -950,7 +950,7 @@ st_GetTexImage(struct gl_context * ctx,
 
    /* XXX Fallback to _mesa_GetTexImage_sw for depth-stencil formats
     * due to an incomplete stencil blit implementation in some drivers. */
-   if (format == GL_DEPTH_STENCIL) {
+   if (format == GL_DEPTH_STENCIL || format == GL_STENCIL_INDEX) {
       goto fallback;
    }
 
diff --git a/mesalib/src/mesa/state_tracker/st_extensions.c b/mesalib/src/mesa/state_tracker/st_extensions.c
index bc20f7323..82e4a3009 100644
--- a/mesalib/src/mesa/state_tracker/st_extensions.c
+++ b/mesalib/src/mesa/state_tracker/st_extensions.c
@@ -650,12 +650,15 @@ void st_init_extensions(struct pipe_screen *screen,
                           ARRAY_SIZE(vertex_mapping), PIPE_BUFFER,
                           PIPE_BIND_VERTEX_BUFFER);
 
+   if (extensions->ARB_stencil_texturing)
+      extensions->ARB_texture_stencil8 = GL_TRUE;
+
    /* Figure out GLSL support. */
    glsl_feature_level = screen->get_param(screen, PIPE_CAP_GLSL_FEATURE_LEVEL);
 
    consts->GLSLVersion = glsl_feature_level;
-   if (glsl_feature_level >= 330)
-      consts->GLSLVersion = 330;
+   if (glsl_feature_level >= 410)
+      consts->GLSLVersion = 410;
 
    _mesa_override_glsl_version(consts);
 
@@ -666,6 +669,8 @@ void st_init_extensions(struct pipe_screen *screen,
 
    if (glsl_feature_level >= 400)
       extensions->ARB_gpu_shader5 = GL_TRUE;
+   if (glsl_feature_level >= 410)
+      extensions->ARB_shader_precision = GL_TRUE;
 
    /* This extension needs full OpenGL 3.2, but we don't know if that's
     * supported at this point. Only check the GLSL version. */
diff --git a/mesalib/src/mesa/state_tracker/st_format.c b/mesalib/src/mesa/state_tracker/st_format.c
index 72dbf3bd4..181465dd8 100644
--- a/mesalib/src/mesa/state_tracker/st_format.c
+++ b/mesalib/src/mesa/state_tracker/st_format.c
@@ -1942,11 +1942,6 @@ st_ChooseTextureFormat(struct gl_context *ctx, GLenum target,
                        GLint internalFormat,
                        GLenum format, GLenum type)
 {
-   const boolean want_renderable =
-      internalFormat == 3 || internalFormat == 4 ||
-      internalFormat == GL_RGB || internalFormat == GL_RGBA ||
-      internalFormat == GL_RGB8 || internalFormat == GL_RGBA8 ||
-      internalFormat == GL_BGRA;
    struct st_context *st = st_context(ctx);
    enum pipe_format pFormat;
    unsigned bindings;
@@ -1962,15 +1957,17 @@ st_ChooseTextureFormat(struct gl_context *ctx, GLenum target,
    }
 
    /* GL textures may wind up being render targets, but we don't know
-    * that in advance.  Specify potential render target flags now.
+    * that in advance.  Specify potential render target flags now for formats
+    * that we know should always be renderable.
     */
    bindings = PIPE_BIND_SAMPLER_VIEW;
-   if (want_renderable) {
-      if (_mesa_is_depth_or_stencil_format(internalFormat))
-	 bindings |= PIPE_BIND_DEPTH_STENCIL;
-      else
+   if (_mesa_is_depth_or_stencil_format(internalFormat))
+      bindings |= PIPE_BIND_DEPTH_STENCIL;
+   else if (internalFormat == 3 || internalFormat == 4 ||
+            internalFormat == GL_RGB || internalFormat == GL_RGBA ||
+            internalFormat == GL_RGB8 || internalFormat == GL_RGBA8 ||
+            internalFormat == GL_BGRA)
 	 bindings |= PIPE_BIND_RENDER_TARGET;
-   }
 
    /* GLES allows the driver to choose any format which matches
     * the format+type combo, because GLES only supports unsized internal
diff --git a/mesalib/src/mesa/swrast/s_texcombine.c b/mesalib/src/mesa/swrast/s_texcombine.c
index 0adb8e5ba..453bd3636 100644
--- a/mesalib/src/mesa/swrast/s_texcombine.c
+++ b/mesalib/src/mesa/swrast/s_texcombine.c
@@ -645,7 +645,7 @@ _swrast_texture_span( struct gl_context *ctx, SWspan *span )
 
    /*
     * Must do all texture sampling before combining in order to
-    * accomodate GL_ARB_texture_env_crossbar.
+    * accommodate GL_ARB_texture_env_crossbar.
     */
    for (unit = 0; unit < ctx->Const.MaxTextureUnits; unit++) {
       const struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit];
diff --git a/mesalib/src/mesa/tnl/t_vertex.h b/mesalib/src/mesa/tnl/t_vertex.h
index 36d8a969e..a84b299e5 100644
--- a/mesalib/src/mesa/tnl/t_vertex.h
+++ b/mesalib/src/mesa/tnl/t_vertex.h
@@ -40,7 +40,7 @@ struct tnl_clipspace;
 
 
 /* It will probably be necessary to allow drivers to specify new
- * emit-styles to cover all the wierd and wacky things out there.
+ * emit-styles to cover all the weird and wacky things out there.
  */
 enum tnl_attr_format {
    EMIT_1F,
diff --git a/mesalib/src/mesa/vbo/vbo_exec_api.c b/mesalib/src/mesa/vbo/vbo_exec_api.c
index 859078f12..48680ec88 100644
--- a/mesalib/src/mesa/vbo/vbo_exec_api.c
+++ b/mesalib/src/mesa/vbo/vbo_exec_api.c
@@ -685,7 +685,7 @@ static void GLAPIENTRY vbo_exec_Begin( GLenum mode )
       return;
    }
 
-   /* Heuristic: attempt to isolate attributes occuring outside
+   /* Heuristic: attempt to isolate attributes occurring outside
     * begin/end pairs.
     */
    if (exec->vtx.vertex_size && !exec->vtx.attrsz[0])
diff --git a/mesalib/src/mesa/vbo/vbo_save_draw.c b/mesalib/src/mesa/vbo/vbo_save_draw.c
index 74e266d19..de744e0c7 100644
--- a/mesalib/src/mesa/vbo/vbo_save_draw.c
+++ b/mesalib/src/mesa/vbo/vbo_save_draw.c
@@ -278,7 +278,7 @@ vbo_save_playback_vertex_list(struct gl_context *ctx, void *data)
          goto end;
       }
       else if (save->replay_flags) {
-	 /* Various degnerate cases: translate into immediate mode
+	 /* Various degenerate cases: translate into immediate mode
 	  * calls rather than trying to execute in place.
 	  */
 	 vbo_save_loopback_vertex_list( ctx, node );
diff --git a/mesalib/src/util/Android.mk b/mesalib/src/util/Android.mk
index 6b38cf9af..a39185ae4 100644
--- a/mesalib/src/util/Android.mk
+++ b/mesalib/src/util/Android.mk
@@ -36,27 +36,22 @@ LOCAL_SRC_FILES := \
 LOCAL_C_INCLUDES := \
 	$(MESA_TOP)/src/mesa \
 	$(MESA_TOP)/src/mapi \
-	$(MESA_TOP)/src
+	$(MESA_TOP)/src/gallium/include \
+	$(MESA_TOP)/src/gallium/auxiliary
 
 LOCAL_MODULE := libmesa_util
 
 # Generated sources
 
-ifeq ($(LOCAL_MODULE_CLASS),)
 LOCAL_MODULE_CLASS := STATIC_LIBRARIES
-endif
 
-intermediates := $(call local-intermediates-dir)
+intermediates := $(call local-generated-sources-dir)
+LOCAL_GENERATED_SOURCES := $(addprefix $(intermediates)/,$(MESA_UTIL_GENERATED_FILES))
 
-# This is the list of auto-generated files: sources and headers
-sources := $(addprefix $(intermediates)/, $(MESA_UTIL_GENERATED_FILES))
-
-LOCAL_GENERATED_SOURCES += $(sources)
-
-FORMAT_SRGB := $(LOCAL_PATH)/format_srgb.py
-
-$(intermediates)/format_srgb.c: $(FORMAT_SRGB)
-	@$(MESA_PYTHON2) $(FORMAT_SRGB) $< > $@
+$(LOCAL_GENERATED_SOURCES): PRIVATE_PYTHON := $(MESA_PYTHON2)
+$(LOCAL_GENERATED_SOURCES): PRIVATE_CUSTOM_TOOL = $(PRIVATE_PYTHON) $^ > $@
+$(LOCAL_GENERATED_SOURCES): $(intermediates)/%.c: $(LOCAL_PATH)/%.py
+	$(transform-generated-source)
 
 include $(MESA_COMMON_MK)
 include $(BUILD_STATIC_LIBRARY)
@@ -76,27 +71,22 @@ LOCAL_SRC_FILES := \
 LOCAL_C_INCLUDES := \
 	$(MESA_TOP)/src/mesa \
 	$(MESA_TOP)/src/mapi \
-	$(MESA_TOP)/src
+	$(MESA_TOP)/src/gallium/include \
+	$(MESA_TOP)/src/gallium/auxiliary
 
 LOCAL_MODULE := libmesa_util
 
 # Generated sources
 
-ifeq ($(LOCAL_MODULE_CLASS),)
 LOCAL_MODULE_CLASS := STATIC_LIBRARIES
-endif
-
-intermediates := $(call local-intermediates-dir)
-
-# This is the list of auto-generated files: sources and headers
-sources := $(addprefix $(intermediates)/, $(MESA_UTIL_GENERATED_FILES))
-
-LOCAL_GENERATED_SOURCES += $(sources)
 
-FORMAT_SRGB := $(LOCAL_PATH)/format_srgb.py
+intermediates := $(call local-generated-sources-dir)
+LOCAL_GENERATED_SOURCES := $(addprefix $(intermediates)/,$(MESA_UTIL_GENERATED_FILES))
 
-$(intermediates)/format_srgb.c: $(FORMAT_SRGB)
-	@$(MESA_PYTHON2) $(FORMAT_SRGB) $< > $@
+$(LOCAL_GENERATED_SOURCES): PRIVATE_PYTHON := $(MESA_PYTHON2)
+$(LOCAL_GENERATED_SOURCES): PRIVATE_CUSTOM_TOOL = $(PRIVATE_PYTHON) $^ > $@
+$(LOCAL_GENERATED_SOURCES): $(intermediates)/%.c: $(LOCAL_PATH)/%.py
+	$(transform-generated-source)
 
 include $(MESA_COMMON_MK)
 include $(BUILD_HOST_STATIC_LIBRARY)
diff --git a/mesalib/src/util/macros.h b/mesalib/src/util/macros.h
index 6c7bda7ae..3b708ed6a 100644
--- a/mesalib/src/util/macros.h
+++ b/mesalib/src/util/macros.h
@@ -182,5 +182,7 @@ do {                       \
 #define UNUSED
 #endif
 
+/** Compute ceiling of integer quotient of A divided by B. */
+#define DIV_ROUND_UP( A, B )  ( (A) % (B) == 0 ? (A)/(B) : (A)/(B)+1 )
 
 #endif /* UTIL_MACROS_H */
-- 
cgit v1.2.3