Merge remote-tracking branch 'mesa-public/master' into vulkan
authorJason Ekstrand <jason.ekstrand@intel.com>
Mon, 28 Dec 2015 07:23:05 +0000 (23:23 -0800)
committerJason Ekstrand <jason.ekstrand@intel.com>
Mon, 28 Dec 2015 07:23:05 +0000 (23:23 -0800)
This pulls in tessellation and the store_var changes that go with it.

32 files changed:
1  2 
configure.ac
src/glsl/ast_to_hir.cpp
src/glsl/nir/glsl_to_nir.cpp
src/glsl/nir/glsl_types.cpp
src/glsl/nir/glsl_types.h
src/glsl/nir/nir.h
src/glsl/nir/nir_builder.h
src/glsl/nir/nir_intrinsics.h
src/glsl/nir/nir_lower_io.c
src/glsl/nir/nir_lower_returns.c
src/glsl/nir/nir_print.c
src/glsl/nir/nir_types.cpp
src/glsl/nir/nir_types.h
src/glsl/nir/spirv_to_nir.c
src/mesa/drivers/dri/i965/Makefile.sources
src/mesa/drivers/dri/i965/brw_compiler.h
src/mesa/drivers/dri/i965/brw_defines.h
src/mesa/drivers/dri/i965/brw_fs.cpp
src/mesa/drivers/dri/i965/brw_fs.h
src/mesa/drivers/dri/i965/brw_fs_nir.cpp
src/mesa/drivers/dri/i965/brw_fs_visitor.cpp
src/mesa/drivers/dri/i965/brw_shader.cpp
src/mesa/drivers/dri/i965/brw_shader.h
src/mesa/drivers/dri/i965/brw_surface_formats.c
src/mesa/drivers/dri/i965/brw_vec4.h
src/mesa/drivers/dri/i965/brw_vec4_generator.cpp
src/mesa/drivers/dri/i965/brw_vec4_nir.cpp
src/mesa/main/mtypes.h
src/mesa/program/ir_to_mesa.cpp
src/mesa/program/prog_to_nir.c
src/vulkan/anv_meta.c
src/vulkan/anv_pipeline.c

diff --cc configure.ac
Simple merge
Simple merge
Simple merge
index bc8677ba6fc16c63f66a42dc9380f1de84bb3d51,44d30565e4dd78787f15c60cb18c5123c89a2187..d86609718ea6a53c14fdf082a0c6d113467898b1
@@@ -1872,9 -1689,8 +1880,9 @@@ glsl_type::count_attribute_slots(bool v
     }
  
     case GLSL_TYPE_ARRAY:
-       return this->length * this->fields.array->count_attribute_slots();
+       return this->length * this->fields.array->count_attribute_slots(vertex_input_slots);
  
 +   case GLSL_TYPE_FUNCTION:
     case GLSL_TYPE_SAMPLER:
     case GLSL_TYPE_IMAGE:
     case GLSL_TYPE_ATOMIC_UINT:
Simple merge
index 904e444487a972ba16c0a6a74a9052bec1c5b3ed,9dbda448dd61ec58f5bed5bb046bae1c6cf13078..c05df101f29cbde0ef3fd95ee22bcab11450f86a
@@@ -366,32 -352,6 +357,34 @@@ typedef struct 
  #define nir_foreach_variable(var, var_list) \
     foreach_list_typed(nir_variable, var, node, var_list)
  
-    unsigned slots = glsl_count_attribute_slots(var_type);
 +/**
 + * Returns the bits in the inputs_read, outputs_written, or
 + * system_values_read bitfield corresponding to this variable.
 + */
 +static inline uint64_t
 +nir_variable_get_io_mask(nir_variable *var, gl_shader_stage stage)
 +{
 +   assert(var->data.mode == nir_var_shader_in ||
 +          var->data.mode == nir_var_shader_out ||
 +          var->data.mode == nir_var_system_value);
 +   assert(var->data.location >= 0);
 +
 +   const struct glsl_type *var_type = var->type;
 +   if (stage == MESA_SHADER_GEOMETRY && var->data.mode == nir_var_shader_in) {
 +      /* Most geometry shader inputs are per-vertex arrays */
 +      if (var->data.location >= VARYING_SLOT_VAR0)
 +         assert(glsl_type_is_array(var_type));
 +
 +      if (glsl_type_is_array(var_type))
 +         var_type = glsl_get_array_element(var_type);
 +   }
 +
++   bool is_vertex_input = (var->data.mode == nir_var_shader_in &&
++                           stage == MESA_SHADER_VERTEX);
++   unsigned slots = glsl_count_attribute_slots(var_type, is_vertex_input);
 +   return ((1ull << slots) - 1) << var->data.location;
 +}
 +
  typedef struct {
     struct exec_node node;
  
Simple merge
Simple merge
Simple merge
index ce0512c770aa3246bb4d097daab445e99b834933,0000000000000000000000000000000000000000..f1e8b143840acae8a9f6b65c15806c20a3935660
mode 100644,000000..100644
--- /dev/null
@@@ -1,245 -1,0 +1,245 @@@
-    nir_store_var(b, state->return_flag, nir_imm_int(b, NIR_TRUE));
 +/*
 + * 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.
 + */
 +
 +#include "nir.h"
 +#include "nir_builder.h"
 +#include "nir_control_flow.h"
 +
 +struct lower_returns_state {
 +   nir_builder builder;
 +   struct exec_list *cf_list;
 +   nir_loop *loop;
 +   nir_variable *return_flag;
 +};
 +
 +static bool lower_returns_in_cf_list(struct exec_list *cf_list,
 +                                     struct lower_returns_state *state);
 +
 +static void
 +predicate_following(nir_cf_node *node, struct lower_returns_state *state)
 +{
 +   nir_builder *b = &state->builder;
 +   b->cursor = nir_after_cf_node_and_phis(node);
 +
 +   if (nir_cursors_equal(b->cursor, nir_after_cf_list(state->cf_list)))
 +      return; /* Nothing to predicate */
 +
 +   assert(state->return_flag);
 +
 +   nir_if *if_stmt = nir_if_create(b->shader);
 +   if_stmt->condition = nir_src_for_ssa(nir_load_var(b, state->return_flag));
 +   nir_cf_node_insert(b->cursor, &if_stmt->cf_node);
 +
 +   if (state->loop) {
 +      /* If we're inside of a loop, then all we need to do is insert a
 +       * conditional break.
 +       */
 +      nir_jump_instr *brk =
 +         nir_jump_instr_create(state->builder.shader, nir_jump_break);
 +      nir_instr_insert(nir_before_cf_list(&if_stmt->then_list), &brk->instr);
 +   } else {
 +      /* Otherwise, we need to actually move everything into the else case
 +       * of the if statement.
 +       */
 +      nir_cf_list list;
 +      nir_cf_extract(&list, nir_after_cf_node(&if_stmt->cf_node),
 +                            nir_after_cf_list(state->cf_list));
 +      assert(!exec_list_is_empty(&list.list));
 +      nir_cf_reinsert(&list, nir_before_cf_list(&if_stmt->else_list));
 +   }
 +}
 +
 +static bool
 +lower_returns_in_loop(nir_loop *loop, struct lower_returns_state *state)
 +{
 +   nir_loop *parent = state->loop;
 +   state->loop = loop;
 +   bool progress = lower_returns_in_cf_list(&loop->body, state);
 +   state->loop = parent;
 +
 +   /* If the recursive call made progress, then there were returns inside
 +    * of the loop.  These would have been lowered to breaks with the return
 +    * flag set to true.  We need to predicate everything following the loop
 +    * on the return flag.
 +    */
 +   if (progress)
 +      predicate_following(&loop->cf_node, state);
 +
 +   return progress;
 +}
 +
 +static bool
 +lower_returns_in_if(nir_if *if_stmt, struct lower_returns_state *state)
 +{
 +   bool progress;
 +
 +   progress = lower_returns_in_cf_list(&if_stmt->then_list, state);
 +   progress = lower_returns_in_cf_list(&if_stmt->else_list, state) || progress;
 +
 +   /* If either of the recursive calls made progress, then there were
 +    * returns inside of the body of the if.  If we're in a loop, then these
 +    * were lowered to breaks which automatically skip to the end of the
 +    * loop so we don't have to do anything.  If we're not in a loop, then
 +    * all we know is that the return flag is set appropreately and that the
 +    * recursive calls ensured that nothing gets executed *inside* the if
 +    * after a return.  In order to ensure nothing outside gets executed
 +    * after a return, we need to predicate everything following on the
 +    * return flag.
 +    */
 +   if (progress && !state->loop)
 +      predicate_following(&if_stmt->cf_node, state);
 +
 +   return progress;
 +}
 +
 +static bool
 +lower_returns_in_block(nir_block *block, struct lower_returns_state *state)
 +{
 +   if (block->predecessors->entries == 0 &&
 +       block != nir_start_block(state->builder.impl)) {
 +      /* This block is unreachable.  Delete it and everything after it. */
 +      nir_cf_list list;
 +      nir_cf_extract(&list, nir_before_cf_node(&block->cf_node),
 +                            nir_after_cf_list(state->cf_list));
 +
 +      if (exec_list_is_empty(&list.list)) {
 +         /* There's nothing here, which also means there's nothing in this
 +          * block so we have nothing to do.
 +          */
 +         return false;
 +      } else {
 +         nir_cf_delete(&list);
 +         return true;
 +      }
 +   }
 +
 +   nir_instr *last_instr = nir_block_last_instr(block);
 +   if (last_instr == NULL)
 +      return false;
 +
 +   if (last_instr->type != nir_instr_type_jump)
 +      return false;
 +
 +   nir_jump_instr *jump = nir_instr_as_jump(last_instr);
 +   if (jump->type != nir_jump_return)
 +      return false;
 +
 +   nir_builder *b = &state->builder;
 +   b->cursor = nir_before_instr(&jump->instr);
 +
 +   /* Set the return flag */
 +   if (state->return_flag == NULL) {
 +      state->return_flag =
 +         nir_local_variable_create(b->impl, glsl_bool_type(), "return");
 +
 +      /* Set a default value of false */
 +      state->return_flag->constant_initializer =
 +         rzalloc(state->return_flag, nir_constant);
 +   }
++   nir_store_var(b, state->return_flag, nir_imm_int(b, NIR_TRUE), 1);
 +
 +   if (state->loop) {
 +      /* We're in a loop.  Make the return a break. */
 +      jump->type = nir_jump_return;
 +   } else {
 +      /* Not in a loop.  Just delete the return; we'll deal with
 +       * predicating later.
 +       */
 +      assert(nir_cf_node_next(&block->cf_node) == NULL);
 +      nir_instr_remove(&jump->instr);
 +   }
 +
 +   return true;
 +}
 +
 +static bool
 +lower_returns_in_cf_list(struct exec_list *cf_list,
 +                         struct lower_returns_state *state)
 +{
 +   bool progress = false;
 +
 +   struct exec_list *parent_list = state->cf_list;
 +   state->cf_list = cf_list;
 +
 +   /* We iterate over the list backwards because any given lower call may
 +    * take everything following the given CF node and predicate it.  In
 +    * order to avoid recursion/iteration problems, we want everything after
 +    * a given node to already be lowered before this happens.
 +    */
 +   foreach_list_typed_reverse_safe(nir_cf_node, node, node, cf_list) {
 +      switch (node->type) {
 +      case nir_cf_node_block:
 +         if (lower_returns_in_block(nir_cf_node_as_block(node), state))
 +            progress = true;
 +         break;
 +
 +      case nir_cf_node_if:
 +         if (lower_returns_in_if(nir_cf_node_as_if(node), state))
 +            progress = true;
 +         break;
 +
 +      case nir_cf_node_loop:
 +         if (lower_returns_in_loop(nir_cf_node_as_loop(node), state))
 +            progress = true;
 +         break;
 +
 +      default:
 +         unreachable("Invalid inner CF node type");
 +      }
 +   }
 +
 +   state->cf_list = parent_list;
 +
 +   return progress;
 +}
 +
 +bool
 +nir_lower_returns_impl(nir_function_impl *impl)
 +{
 +   struct lower_returns_state state;
 +
 +   state.cf_list = &impl->body;
 +   state.loop = NULL;
 +   state.return_flag = NULL;
 +   nir_builder_init(&state.builder, impl);
 +
 +   bool progress = lower_returns_in_cf_list(&impl->body, &state);
 +
 +   if (progress)
 +      nir_metadata_preserve(impl, nir_metadata_none);
 +
 +   return progress;
 +}
 +
 +bool
 +nir_lower_returns(nir_shader *shader)
 +{
 +   bool progress = false;
 +
 +   nir_foreach_overload(shader, overload) {
 +      if (overload->impl)
 +         progress = nir_lower_returns_impl(overload->impl) || progress;
 +   }
 +
 +   return progress;
 +}
index 26b1cbb467de459c862ac62cba0515649dcd7cdf,56e570504d7f42a711b9350f741193a722f32048..2691cbdf2135d89df59f99ca1441e7521402d12c
@@@ -219,35 -219,53 +219,82 @@@ print_alu_instr(nir_alu_instr *instr, p
     }
  }
  
 +static const char *
 +get_var_name(nir_variable *var, print_state *state)
 +{
 +   if (state->ht == NULL)
 +      return var->name;
 +
 +   assert(state->syms);
 +
 +   struct hash_entry *entry = _mesa_hash_table_search(state->ht, var);
 +   if (entry)
 +      return entry->data;
 +
 +   char *name;
 +
 +   struct set_entry *set_entry = _mesa_set_search(state->syms, var->name);
 +   if (set_entry != NULL) {
 +      /* we have a collision with another name, append an @ + a unique index */
 +      name = ralloc_asprintf(state->syms, "%s@%u", var->name, state->index++);
 +   } else {
 +      /* Mark this one as seen */
 +      _mesa_set_add(state->syms, var->name);
 +      name = var->name;
 +   }
 +
 +   _mesa_hash_table_insert(state->ht, var, name);
 +
 +   return name;
 +}
 +
+ static void
+ print_constant(nir_constant *c, const struct glsl_type *type, print_state *state)
+ {
+    FILE *fp = state->fp;
+    unsigned total_elems = glsl_get_components(type);
+    unsigned i;
+    switch (glsl_get_base_type(type)) {
+    case GLSL_TYPE_UINT:
+    case GLSL_TYPE_INT:
+    case GLSL_TYPE_BOOL:
+       for (i = 0; i < total_elems; i++) {
+          if (i > 0) fprintf(fp, ", ");
+          fprintf(fp, "0x%08x", c->value.u[i]);
+       }
+       break;
+    case GLSL_TYPE_FLOAT:
+       for (i = 0; i < total_elems; i++) {
+          if (i > 0) fprintf(fp, ", ");
+          fprintf(fp, "%f", c->value.f[i]);
+       }
+       break;
+    case GLSL_TYPE_STRUCT:
+       for (i = 0; i < c->num_elements; i++) {
+          if (i > 0) fprintf(fp, ", ");
+          fprintf(fp, "{ ");
+          print_constant(c->elements[i], glsl_get_struct_field(type, i), state);
+          fprintf(fp, " }");
+       }
+       break;
+    case GLSL_TYPE_ARRAY:
+       for (i = 0; i < c->num_elements; i++) {
+          if (i > 0) fprintf(fp, ", ");
+          fprintf(fp, "{ ");
+          print_constant(c->elements[i], glsl_get_array_element(type), state);
+          fprintf(fp, " }");
+       }
+       break;
+    default:
+       unreachable("not reached");
+    }
+ }
  static void
  print_var_decl(nir_variable *var, print_state *state)
  {
        fprintf(fp, " (%s, %u)", loc, var->data.driver_location);
     }
  
+    if (var->constant_initializer) {
+       fprintf(fp, " = { ");
+       print_constant(var->constant_initializer, var->type, state);
+       fprintf(fp, " }");
+    }
     fprintf(fp, "\n");
 -
 -   if (state->syms) {
 -      _mesa_set_add(state->syms, name);
 -      _mesa_hash_table_insert(state->ht, var, name);
 -   }
  }
  
  static void
index 54751cbcb5f011ee7d40df7a470d82cbaf0b487e,41ac54673d9a67d37c59510d8826f5700d6c0ec4..86f8508b85906e7a8fa09d56e4f99ecbb344af28
@@@ -124,12 -112,6 +124,13 @@@ glsl_get_aoa_size(const struct glsl_typ
     return type->arrays_of_arrays_size();
  }
  
- glsl_count_attribute_slots(const struct glsl_type *type)
 +unsigned
-    return type->count_attribute_slots();
++glsl_count_attribute_slots(const struct glsl_type *type,
++                           bool vertex_input_slots)
 +{
++   return type->count_attribute_slots(vertex_input_slots);
 +}
 +
  const char *
  glsl_get_struct_elem_name(const struct glsl_type *type, unsigned index)
  {
@@@ -237,50 -167,24 +238,56 @@@ glsl_float_type(void
     return glsl_type::float_type;
  }
  
+ const glsl_type *
+ glsl_vec_type(unsigned n)
+ {
+    return glsl_type::vec(n);
+ }
+ const glsl_type *
+ glsl_vec4_type(void)
+ {
+    return glsl_type::vec4_type;
+ }
 +const glsl_type *
 +glsl_int_type(void)
 +{
 +   return glsl_type::int_type;
 +}
 +
  const glsl_type *
  glsl_uint_type(void)
  {
     return glsl_type::uint_type;
  }
  
- const glsl_type *
- glsl_vec4_type(void)
- {
-    return glsl_type::vec4_type;
- }
 +const glsl_type *
 +glsl_bool_type(void)
 +{
 +   return glsl_type::bool_type;
 +}
 +
 +const glsl_type *
 +glsl_scalar_type(enum glsl_base_type base_type)
 +{
 +   return glsl_type::get_instance(base_type, 1, 1);
 +}
 +
 +const glsl_type *
 +glsl_vector_type(enum glsl_base_type base_type, unsigned components)
 +{
 +   assert(components > 1 && components <= 4);
 +   return glsl_type::get_instance(base_type, components, 1);
 +}
 +
 +const glsl_type *
 +glsl_matrix_type(enum glsl_base_type base_type, unsigned rows, unsigned columns)
 +{
 +   assert(rows > 1 && rows <= 4 && columns >= 1 && columns <= 4);
 +   return glsl_type::get_instance(base_type, rows, columns);
 +}
 +
  const glsl_type *
  glsl_array_type(const glsl_type *base, unsigned elements)
  {
index 1bae84a356edfc038a82ca05830f115b51fa32a9,64a75f607d580cac80abc0064031975c8eff30e5..535d36373de2b7992c31d9a873c8262df05d8e28
@@@ -67,8 -61,6 +67,9 @@@ unsigned glsl_get_length(const struct g
  
  unsigned glsl_get_aoa_size(const struct glsl_type *type);
  
- unsigned glsl_count_attribute_slots(const struct glsl_type *type);
++unsigned glsl_count_attribute_slots(const struct glsl_type *type,
++                                    bool vertex_input_slots);
 +
  const char *glsl_get_struct_elem_name(const struct glsl_type *type,
                                        unsigned index);
  
@@@ -92,31 -75,11 +93,32 @@@ bool glsl_sampler_type_is_array(const s
  
  const struct glsl_type *glsl_void_type(void);
  const struct glsl_type *glsl_float_type(void);
+ const struct glsl_type *glsl_vec_type(unsigned n);
+ const struct glsl_type *glsl_vec4_type(void);
 +const struct glsl_type *glsl_int_type(void);
  const struct glsl_type *glsl_uint_type(void);
- const struct glsl_type *glsl_vec4_type(void);
 +const struct glsl_type *glsl_bool_type(void);
 +
 +const struct glsl_type *glsl_scalar_type(enum glsl_base_type base_type);
 +const struct glsl_type *glsl_vector_type(enum glsl_base_type base_type,
 +                                         unsigned components);
 +const struct glsl_type *glsl_matrix_type(enum glsl_base_type base_type,
 +                                         unsigned rows, unsigned columns);
  const struct glsl_type *glsl_array_type(const struct glsl_type *base,
                                          unsigned elements);
 +const struct glsl_type *glsl_struct_type(const struct glsl_struct_field *fields,
 +                                         unsigned num_fields, const char *name);
 +const struct glsl_type *glsl_sampler_type(enum glsl_sampler_dim dim,
 +                                          bool is_shadow, bool is_array,
 +                                          enum glsl_base_type base_type);
 +const struct glsl_type *glsl_image_type(enum glsl_sampler_dim dim,
 +                                        bool is_array,
 +                                        enum glsl_base_type base_type);
 +const struct glsl_type * glsl_function_type(const struct glsl_type *return_type,
 +                                            const struct glsl_function_param *params,
 +                                            unsigned num_params);
 +
 +const struct glsl_type *glsl_transposed_type(const struct glsl_type *type);
  
  #ifdef __cplusplus
  }
index 48960b7037316ba37c6d0e9db1d5f2dd6d14f537,0000000000000000000000000000000000000000..91710ba45f9f78920a8f754e709e6afd59c7991c
mode 100644,000000..100644
--- /dev/null
@@@ -1,3953 -1,0 +1,3964 @@@
-       switch (src[0]->num_components) {
-       case 1:  op = nir_op_imov;    break;
-       case 2:  op = nir_op_bany2;   break;
-       case 3:  op = nir_op_bany3;   break;
-       case 4:  op = nir_op_bany4;   break;
 +/*
 + * 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)
 + *
 + */
 +
 +#include "spirv_to_nir_private.h"
 +#include "nir_vla.h"
 +#include "nir_control_flow.h"
 +
 +static struct vtn_ssa_value *
 +vtn_undef_ssa_value(struct vtn_builder *b, const struct glsl_type *type)
 +{
 +   struct vtn_ssa_value *val = rzalloc(b, struct vtn_ssa_value);
 +   val->type = type;
 +
 +   if (glsl_type_is_vector_or_scalar(type)) {
 +      unsigned num_components = glsl_get_vector_elements(val->type);
 +      nir_ssa_undef_instr *undef =
 +         nir_ssa_undef_instr_create(b->shader, num_components);
 +
 +      nir_instr_insert_before_cf_list(&b->impl->body, &undef->instr);
 +      val->def = &undef->def;
 +   } else {
 +      unsigned elems = glsl_get_length(val->type);
 +      val->elems = ralloc_array(b, struct vtn_ssa_value *, elems);
 +      if (glsl_type_is_matrix(type)) {
 +         const struct glsl_type *elem_type =
 +            glsl_vector_type(glsl_get_base_type(type),
 +                             glsl_get_vector_elements(type));
 +
 +         for (unsigned i = 0; i < elems; i++)
 +            val->elems[i] = vtn_undef_ssa_value(b, elem_type);
 +      } else if (glsl_type_is_array(type)) {
 +         const struct glsl_type *elem_type = glsl_get_array_element(type);
 +         for (unsigned i = 0; i < elems; i++)
 +            val->elems[i] = vtn_undef_ssa_value(b, elem_type);
 +      } else {
 +         for (unsigned i = 0; i < elems; i++) {
 +            const struct glsl_type *elem_type = glsl_get_struct_field(type, i);
 +            val->elems[i] = vtn_undef_ssa_value(b, elem_type);
 +         }
 +      }
 +   }
 +
 +   return val;
 +}
 +
 +static struct vtn_ssa_value *
 +vtn_const_ssa_value(struct vtn_builder *b, nir_constant *constant,
 +                    const struct glsl_type *type)
 +{
 +   struct hash_entry *entry = _mesa_hash_table_search(b->const_table, constant);
 +
 +   if (entry)
 +      return entry->data;
 +
 +   struct vtn_ssa_value *val = rzalloc(b, struct vtn_ssa_value);
 +   val->type = type;
 +
 +   switch (glsl_get_base_type(type)) {
 +   case GLSL_TYPE_INT:
 +   case GLSL_TYPE_UINT:
 +   case GLSL_TYPE_BOOL:
 +   case GLSL_TYPE_FLOAT:
 +   case GLSL_TYPE_DOUBLE:
 +      if (glsl_type_is_vector_or_scalar(type)) {
 +         unsigned num_components = glsl_get_vector_elements(val->type);
 +         nir_load_const_instr *load =
 +            nir_load_const_instr_create(b->shader, num_components);
 +
 +         for (unsigned i = 0; i < num_components; i++)
 +            load->value.u[i] = constant->value.u[i];
 +
 +         nir_instr_insert_before_cf_list(&b->impl->body, &load->instr);
 +         val->def = &load->def;
 +      } else {
 +         assert(glsl_type_is_matrix(type));
 +         unsigned rows = glsl_get_vector_elements(val->type);
 +         unsigned columns = glsl_get_matrix_columns(val->type);
 +         val->elems = ralloc_array(b, struct vtn_ssa_value *, columns);
 +
 +         for (unsigned i = 0; i < columns; i++) {
 +            struct vtn_ssa_value *col_val = rzalloc(b, struct vtn_ssa_value);
 +            col_val->type = glsl_get_column_type(val->type);
 +            nir_load_const_instr *load =
 +               nir_load_const_instr_create(b->shader, rows);
 +
 +            for (unsigned j = 0; j < rows; j++)
 +               load->value.u[j] = constant->value.u[rows * i + j];
 +
 +            nir_instr_insert_before_cf_list(&b->impl->body, &load->instr);
 +            col_val->def = &load->def;
 +
 +            val->elems[i] = col_val;
 +         }
 +      }
 +      break;
 +
 +   case GLSL_TYPE_ARRAY: {
 +      unsigned elems = glsl_get_length(val->type);
 +      val->elems = ralloc_array(b, struct vtn_ssa_value *, elems);
 +      const struct glsl_type *elem_type = glsl_get_array_element(val->type);
 +      for (unsigned i = 0; i < elems; i++)
 +         val->elems[i] = vtn_const_ssa_value(b, constant->elements[i],
 +                                             elem_type);
 +      break;
 +   }
 +
 +   case GLSL_TYPE_STRUCT: {
 +      unsigned elems = glsl_get_length(val->type);
 +      val->elems = ralloc_array(b, struct vtn_ssa_value *, elems);
 +      for (unsigned i = 0; i < elems; i++) {
 +         const struct glsl_type *elem_type =
 +            glsl_get_struct_field(val->type, i);
 +         val->elems[i] = vtn_const_ssa_value(b, constant->elements[i],
 +                                             elem_type);
 +      }
 +      break;
 +   }
 +
 +   default:
 +      unreachable("bad constant type");
 +   }
 +
 +   return val;
 +}
 +
 +static struct vtn_ssa_value *
 +vtn_variable_load(struct vtn_builder *b, nir_deref_var *src,
 +                  struct vtn_type *src_type);
 +
 +struct vtn_ssa_value *
 +vtn_ssa_value(struct vtn_builder *b, uint32_t value_id)
 +{
 +   struct vtn_value *val = vtn_untyped_value(b, value_id);
 +   switch (val->value_type) {
 +   case vtn_value_type_undef:
 +      return vtn_undef_ssa_value(b, val->type->type);
 +
 +   case vtn_value_type_constant:
 +      return vtn_const_ssa_value(b, val->constant, val->const_type);
 +
 +   case vtn_value_type_ssa:
 +      return val->ssa;
 +
 +   case vtn_value_type_deref:
 +      /* This is needed for function parameters */
 +      return vtn_variable_load(b, val->deref, val->deref_type);
 +
 +   default:
 +      unreachable("Invalid type for an SSA value");
 +   }
 +}
 +
 +static char *
 +vtn_string_literal(struct vtn_builder *b, const uint32_t *words,
 +                   unsigned word_count)
 +{
 +   return ralloc_strndup(b, (char *)words, word_count * sizeof(*words));
 +}
 +
 +static const uint32_t *
 +vtn_foreach_instruction(struct vtn_builder *b, const uint32_t *start,
 +                        const uint32_t *end, vtn_instruction_handler handler)
 +{
 +   const uint32_t *w = start;
 +   while (w < end) {
 +      SpvOp opcode = w[0] & SpvOpCodeMask;
 +      unsigned count = w[0] >> SpvWordCountShift;
 +      assert(count >= 1 && w + count <= end);
 +
 +      if (opcode == SpvOpNop) {
 +         w++;
 +         continue;
 +      }
 +
 +      if (!handler(b, opcode, w, count))
 +         return w;
 +
 +      w += count;
 +   }
 +   assert(w == end);
 +   return w;
 +}
 +
 +static void
 +vtn_handle_extension(struct vtn_builder *b, SpvOp opcode,
 +                     const uint32_t *w, unsigned count)
 +{
 +   switch (opcode) {
 +   case SpvOpExtInstImport: {
 +      struct vtn_value *val = vtn_push_value(b, w[1], vtn_value_type_extension);
 +      if (strcmp((const char *)&w[2], "GLSL.std.450") == 0) {
 +         val->ext_handler = vtn_handle_glsl450_instruction;
 +      } else {
 +         assert(!"Unsupported extension");
 +      }
 +      break;
 +   }
 +
 +   case SpvOpExtInst: {
 +      struct vtn_value *val = vtn_value(b, w[3], vtn_value_type_extension);
 +      bool handled = val->ext_handler(b, w[4], w, count);
 +      (void)handled;
 +      assert(handled);
 +      break;
 +   }
 +
 +   default:
 +      unreachable("Unhandled opcode");
 +   }
 +}
 +
 +static void
 +_foreach_decoration_helper(struct vtn_builder *b,
 +                           struct vtn_value *base_value,
 +                           int parent_member,
 +                           struct vtn_value *value,
 +                           vtn_decoration_foreach_cb cb, void *data)
 +{
 +   for (struct vtn_decoration *dec = value->decoration; dec; dec = dec->next) {
 +      int member;
 +      if (dec->member < 0) {
 +         member = parent_member;
 +      } else {
 +         assert(parent_member == -1);
 +         member = dec->member;
 +      }
 +
 +      if (dec->group) {
 +         assert(dec->group->value_type == vtn_value_type_decoration_group);
 +         _foreach_decoration_helper(b, base_value, member, dec->group,
 +                                    cb, data);
 +      } else {
 +         cb(b, base_value, member, dec, data);
 +      }
 +   }
 +}
 +
 +/** Iterates (recursively if needed) over all of the decorations on a value
 + *
 + * This function iterates over all of the decorations applied to a given
 + * value.  If it encounters a decoration group, it recurses into the group
 + * and iterates over all of those decorations as well.
 + */
 +void
 +vtn_foreach_decoration(struct vtn_builder *b, struct vtn_value *value,
 +                       vtn_decoration_foreach_cb cb, void *data)
 +{
 +   _foreach_decoration_helper(b, value, -1, value, cb, data);
 +}
 +
 +static void
 +vtn_handle_decoration(struct vtn_builder *b, SpvOp opcode,
 +                      const uint32_t *w, unsigned count)
 +{
 +   const uint32_t *w_end = w + count;
 +   const uint32_t target = w[1];
 +   w += 2;
 +
 +   int member = -1;
 +   switch (opcode) {
 +   case SpvOpDecorationGroup:
 +      vtn_push_value(b, target, vtn_value_type_undef);
 +      break;
 +
 +   case SpvOpMemberDecorate:
 +      member = *(w++);
 +      /* fallthrough */
 +   case SpvOpDecorate: {
 +      struct vtn_value *val = &b->values[target];
 +
 +      struct vtn_decoration *dec = rzalloc(b, struct vtn_decoration);
 +      dec->member = member;
 +      dec->decoration = *(w++);
 +      dec->literals = w;
 +
 +      /* Link into the list */
 +      dec->next = val->decoration;
 +      val->decoration = dec;
 +      break;
 +   }
 +
 +   case SpvOpGroupMemberDecorate:
 +      member = *(w++);
 +      /* fallthrough */
 +   case SpvOpGroupDecorate: {
 +      struct vtn_value *group = &b->values[target];
 +      assert(group->value_type == vtn_value_type_decoration_group);
 +
 +      for (; w < w_end; w++) {
 +         struct vtn_value *val = &b->values[*w];
 +         struct vtn_decoration *dec = rzalloc(b, struct vtn_decoration);
 +         dec->member = member;
 +         dec->group = group;
 +
 +         /* Link into the list */
 +         dec->next = val->decoration;
 +         val->decoration = dec;
 +      }
 +      break;
 +   }
 +
 +   default:
 +      unreachable("Unhandled opcode");
 +   }
 +}
 +
 +struct member_decoration_ctx {
 +   struct glsl_struct_field *fields;
 +   struct vtn_type *type;
 +};
 +
 +/* does a shallow copy of a vtn_type */
 +
 +static struct vtn_type *
 +vtn_type_copy(struct vtn_builder *b, struct vtn_type *src)
 +{
 +   struct vtn_type *dest = ralloc(b, struct vtn_type);
 +   dest->type = src->type;
 +   dest->is_builtin = src->is_builtin;
 +   if (src->is_builtin)
 +      dest->builtin = src->builtin;
 +
 +   if (!glsl_type_is_vector_or_scalar(src->type)) {
 +      switch (glsl_get_base_type(src->type)) {
 +      case GLSL_TYPE_ARRAY:
 +         dest->array_element = src->array_element;
 +         dest->stride = src->stride;
 +         break;
 +
 +      case GLSL_TYPE_INT:
 +      case GLSL_TYPE_UINT:
 +      case GLSL_TYPE_BOOL:
 +      case GLSL_TYPE_FLOAT:
 +      case GLSL_TYPE_DOUBLE:
 +         /* matrices */
 +         dest->row_major = src->row_major;
 +         dest->stride = src->stride;
 +         break;
 +
 +      case GLSL_TYPE_STRUCT: {
 +         unsigned elems = glsl_get_length(src->type);
 +
 +         dest->members = ralloc_array(b, struct vtn_type *, elems);
 +         memcpy(dest->members, src->members, elems * sizeof(struct vtn_type *));
 +
 +         dest->offsets = ralloc_array(b, unsigned, elems);
 +         memcpy(dest->offsets, src->offsets, elems * sizeof(unsigned));
 +         break;
 +      }
 +
 +      default:
 +         unreachable("unhandled type");
 +      }
 +   }
 +
 +   return dest;
 +}
 +
 +static void
 +struct_member_decoration_cb(struct vtn_builder *b,
 +                            struct vtn_value *val, int member,
 +                            const struct vtn_decoration *dec, void *void_ctx)
 +{
 +   struct member_decoration_ctx *ctx = void_ctx;
 +
 +   if (member < 0)
 +      return;
 +
 +   switch (dec->decoration) {
 +   case SpvDecorationRelaxedPrecision:
 +      break; /* FIXME: Do nothing with this for now. */
 +   case SpvDecorationNoPerspective:
 +      ctx->fields[member].interpolation = INTERP_QUALIFIER_NOPERSPECTIVE;
 +      break;
 +   case SpvDecorationFlat:
 +      ctx->fields[member].interpolation = INTERP_QUALIFIER_FLAT;
 +      break;
 +   case SpvDecorationCentroid:
 +      ctx->fields[member].centroid = true;
 +      break;
 +   case SpvDecorationSample:
 +      ctx->fields[member].sample = true;
 +      break;
 +   case SpvDecorationLocation:
 +      ctx->fields[member].location = dec->literals[0];
 +      break;
 +   case SpvDecorationBuiltIn:
 +      ctx->type->members[member] = vtn_type_copy(b,
 +                                                 ctx->type->members[member]);
 +      ctx->type->members[member]->is_builtin = true;
 +      ctx->type->members[member]->builtin = dec->literals[0];
 +      ctx->type->builtin_block = true;
 +      break;
 +   case SpvDecorationOffset:
 +      ctx->type->offsets[member] = dec->literals[0];
 +      break;
 +   case SpvDecorationMatrixStride:
 +      ctx->type->members[member]->stride = dec->literals[0];
 +      break;
 +   case SpvDecorationColMajor:
 +      break; /* Nothing to do here.  Column-major is the default. */
 +   default:
 +      unreachable("Unhandled member decoration");
 +   }
 +}
 +
 +static void
 +type_decoration_cb(struct vtn_builder *b,
 +                   struct vtn_value *val, int member,
 +                    const struct vtn_decoration *dec, void *ctx)
 +{
 +   struct vtn_type *type = val->type;
 +
 +   if (member != -1)
 +      return;
 +
 +   switch (dec->decoration) {
 +   case SpvDecorationArrayStride:
 +      type->stride = dec->literals[0];
 +      break;
 +   case SpvDecorationBlock:
 +      type->block = true;
 +      break;
 +   case SpvDecorationBufferBlock:
 +      type->buffer_block = true;
 +      break;
 +   case SpvDecorationGLSLShared:
 +   case SpvDecorationGLSLPacked:
 +      /* Ignore these, since we get explicit offsets anyways */
 +      break;
 +
 +   case SpvDecorationStream:
 +      assert(dec->literals[0] == 0);
 +      break;
 +
 +   default:
 +      unreachable("Unhandled type decoration");
 +   }
 +}
 +
 +static unsigned
 +translate_image_format(SpvImageFormat format)
 +{
 +   switch (format) {
 +   case SpvImageFormatUnknown:      return 0;      /* GL_NONE */
 +   case SpvImageFormatRgba32f:      return 0x8814; /* GL_RGBA32F */
 +   case SpvImageFormatRgba16f:      return 0x881A; /* GL_RGBA16F */
 +   case SpvImageFormatR32f:         return 0x822E; /* GL_R32F */
 +   case SpvImageFormatRgba8:        return 0x8058; /* GL_RGBA8 */
 +   case SpvImageFormatRgba8Snorm:   return 0x8F97; /* GL_RGBA8_SNORM */
 +   case SpvImageFormatRg32f:        return 0x8230; /* GL_RG32F */
 +   case SpvImageFormatRg16f:        return 0x822F; /* GL_RG16F */
 +   case SpvImageFormatR11fG11fB10f: return 0x8C3A; /* GL_R11F_G11F_B10F */
 +   case SpvImageFormatR16f:         return 0x822D; /* GL_R16F */
 +   case SpvImageFormatRgba16:       return 0x805B; /* GL_RGBA16 */
 +   case SpvImageFormatRgb10A2:      return 0x8059; /* GL_RGB10_A2 */
 +   case SpvImageFormatRg16:         return 0x822C; /* GL_RG16 */
 +   case SpvImageFormatRg8:          return 0x822B; /* GL_RG8 */
 +   case SpvImageFormatR16:          return 0x822A; /* GL_R16 */
 +   case SpvImageFormatR8:           return 0x8229; /* GL_R8 */
 +   case SpvImageFormatRgba16Snorm:  return 0x8F9B; /* GL_RGBA16_SNORM */
 +   case SpvImageFormatRg16Snorm:    return 0x8F99; /* GL_RG16_SNORM */
 +   case SpvImageFormatRg8Snorm:     return 0x8F95; /* GL_RG8_SNORM */
 +   case SpvImageFormatR16Snorm:     return 0x8F98; /* GL_R16_SNORM */
 +   case SpvImageFormatR8Snorm:      return 0x8F94; /* GL_R8_SNORM */
 +   case SpvImageFormatRgba32i:      return 0x8D82; /* GL_RGBA32I */
 +   case SpvImageFormatRgba16i:      return 0x8D88; /* GL_RGBA16I */
 +   case SpvImageFormatRgba8i:       return 0x8D8E; /* GL_RGBA8I */
 +   case SpvImageFormatR32i:         return 0x8235; /* GL_R32I */
 +   case SpvImageFormatRg32i:        return 0x823B; /* GL_RG32I */
 +   case SpvImageFormatRg16i:        return 0x8239; /* GL_RG16I */
 +   case SpvImageFormatRg8i:         return 0x8237; /* GL_RG8I */
 +   case SpvImageFormatR16i:         return 0x8233; /* GL_R16I */
 +   case SpvImageFormatR8i:          return 0x8231; /* GL_R8I */
 +   case SpvImageFormatRgba32ui:     return 0x8D70; /* GL_RGBA32UI */
 +   case SpvImageFormatRgba16ui:     return 0x8D76; /* GL_RGBA16UI */
 +   case SpvImageFormatRgba8ui:      return 0x8D7C; /* GL_RGBA8UI */
 +   case SpvImageFormatR32ui:        return 0x8236; /* GL_R32UI */
 +   case SpvImageFormatRgb10a2ui:    return 0x906F; /* GL_RGB10_A2UI */
 +   case SpvImageFormatRg32ui:       return 0x823C; /* GL_RG32UI */
 +   case SpvImageFormatRg16ui:       return 0x823A; /* GL_RG16UI */
 +   case SpvImageFormatRg8ui:        return 0x8238; /* GL_RG8UI */
 +   case SpvImageFormatR16ui:        return 0x823A; /* GL_RG16UI */
 +   case SpvImageFormatR8ui:         return 0x8232; /* GL_R8UI */
 +   default:
 +      assert(!"Invalid image format");
 +      return 0;
 +   }
 +}
 +
 +static void
 +vtn_handle_type(struct vtn_builder *b, SpvOp opcode,
 +                const uint32_t *w, unsigned count)
 +{
 +   struct vtn_value *val = vtn_push_value(b, w[1], vtn_value_type_type);
 +
 +   val->type = rzalloc(b, struct vtn_type);
 +   val->type->is_builtin = false;
 +
 +   switch (opcode) {
 +   case SpvOpTypeVoid:
 +      val->type->type = glsl_void_type();
 +      break;
 +   case SpvOpTypeBool:
 +      val->type->type = glsl_bool_type();
 +      break;
 +   case SpvOpTypeInt:
 +      val->type->type = glsl_int_type();
 +      break;
 +   case SpvOpTypeFloat:
 +      val->type->type = glsl_float_type();
 +      break;
 +
 +   case SpvOpTypeVector: {
 +      const struct glsl_type *base =
 +         vtn_value(b, w[2], vtn_value_type_type)->type->type;
 +      unsigned elems = w[3];
 +
 +      assert(glsl_type_is_scalar(base));
 +      val->type->type = glsl_vector_type(glsl_get_base_type(base), elems);
 +      break;
 +   }
 +
 +   case SpvOpTypeMatrix: {
 +      struct vtn_type *base =
 +         vtn_value(b, w[2], vtn_value_type_type)->type;
 +      unsigned columns = w[3];
 +
 +      assert(glsl_type_is_vector(base->type));
 +      val->type->type = glsl_matrix_type(glsl_get_base_type(base->type),
 +                                         glsl_get_vector_elements(base->type),
 +                                         columns);
 +      val->type->array_element = base;
 +      val->type->row_major = false;
 +      val->type->stride = 0;
 +      break;
 +   }
 +
 +   case SpvOpTypeRuntimeArray:
 +   case SpvOpTypeArray: {
 +      struct vtn_type *array_element =
 +         vtn_value(b, w[2], vtn_value_type_type)->type;
 +
 +      unsigned length;
 +      if (opcode == SpvOpTypeRuntimeArray) {
 +         /* A length of 0 is used to denote unsized arrays */
 +         length = 0;
 +      } else {
 +         length =
 +            vtn_value(b, w[3], vtn_value_type_constant)->constant->value.u[0];
 +      }
 +
 +      val->type->type = glsl_array_type(array_element->type, length);
 +      val->type->array_element = array_element;
 +      val->type->stride = 0;
 +      break;
 +   }
 +
 +   case SpvOpTypeStruct: {
 +      unsigned num_fields = count - 2;
 +      val->type->members = ralloc_array(b, struct vtn_type *, num_fields);
 +      val->type->offsets = ralloc_array(b, unsigned, num_fields);
 +
 +      NIR_VLA(struct glsl_struct_field, fields, count);
 +      for (unsigned i = 0; i < num_fields; i++) {
 +         /* TODO: Handle decorators */
 +         val->type->members[i] =
 +            vtn_value(b, w[i + 2], vtn_value_type_type)->type;
 +         fields[i].type = val->type->members[i]->type;
 +         fields[i].name = ralloc_asprintf(b, "field%d", i);
 +         fields[i].location = -1;
 +         fields[i].interpolation = 0;
 +         fields[i].centroid = 0;
 +         fields[i].sample = 0;
 +         fields[i].matrix_layout = 2;
 +      }
 +
 +      struct member_decoration_ctx ctx = {
 +         .fields = fields,
 +         .type = val->type
 +      };
 +
 +      vtn_foreach_decoration(b, val, struct_member_decoration_cb, &ctx);
 +
 +      const char *name = val->name ? val->name : "struct";
 +
 +      val->type->type = glsl_struct_type(fields, num_fields, name);
 +      break;
 +   }
 +
 +   case SpvOpTypeFunction: {
 +      const struct glsl_type *return_type =
 +         vtn_value(b, w[2], vtn_value_type_type)->type->type;
 +      NIR_VLA(struct glsl_function_param, params, count - 3);
 +      for (unsigned i = 0; i < count - 3; i++) {
 +         params[i].type = vtn_value(b, w[i + 3], vtn_value_type_type)->type->type;
 +
 +         /* FIXME: */
 +         params[i].in = true;
 +         params[i].out = true;
 +      }
 +      val->type->type = glsl_function_type(return_type, params, count - 3);
 +      break;
 +   }
 +
 +   case SpvOpTypePointer:
 +      /* FIXME:  For now, we'll just do the really lame thing and return
 +       * the same type.  The validator should ensure that the proper number
 +       * of dereferences happen
 +       */
 +      val->type = vtn_value(b, w[3], vtn_value_type_type)->type;
 +      break;
 +
 +   case SpvOpTypeImage: {
 +      const struct glsl_type *sampled_type =
 +         vtn_value(b, w[2], vtn_value_type_type)->type->type;
 +
 +      assert(glsl_type_is_vector_or_scalar(sampled_type));
 +
 +      enum glsl_sampler_dim dim;
 +      switch ((SpvDim)w[3]) {
 +      case SpvDim1D:       dim = GLSL_SAMPLER_DIM_1D;    break;
 +      case SpvDim2D:       dim = GLSL_SAMPLER_DIM_2D;    break;
 +      case SpvDim3D:       dim = GLSL_SAMPLER_DIM_3D;    break;
 +      case SpvDimCube:     dim = GLSL_SAMPLER_DIM_CUBE;  break;
 +      case SpvDimRect:     dim = GLSL_SAMPLER_DIM_RECT;  break;
 +      case SpvDimBuffer:   dim = GLSL_SAMPLER_DIM_BUF;   break;
 +      default:
 +         unreachable("Invalid SPIR-V Sampler dimension");
 +      }
 +
 +      bool is_shadow = w[4];
 +      bool is_array = w[5];
 +      bool multisampled = w[6];
 +      unsigned sampled = w[7];
 +      SpvImageFormat format = w[8];
 +
 +      assert(!multisampled && "FIXME: Handl multi-sampled textures");
 +
 +      val->type->image_format = translate_image_format(format);
 +
 +      if (sampled == 1) {
 +         val->type->type = glsl_sampler_type(dim, is_shadow, is_array,
 +                                             glsl_get_base_type(sampled_type));
 +      } else if (sampled == 2) {
 +         assert(format);
 +         assert(!is_shadow);
 +         val->type->type = glsl_image_type(dim, is_array,
 +                                           glsl_get_base_type(sampled_type));
 +      } else {
 +         assert(!"We need to know if the image will be sampled");
 +      }
 +      break;
 +   }
 +
 +   case SpvOpTypeSampledImage:
 +      val->type = vtn_value(b, w[2], vtn_value_type_type)->type;
 +      break;
 +
 +   case SpvOpTypeSampler:
 +      /* The actual sampler type here doesn't really matter.  It gets
 +       * thrown away the moment you combine it with an image.  What really
 +       * matters is that it's a sampler type as opposed to an integer type
 +       * so the backend knows what to do.
 +       *
 +       * TODO: Eventually we should consider adding a "bare sampler" type
 +       * to glsl_types.
 +       */
 +      val->type->type = glsl_sampler_type(GLSL_SAMPLER_DIM_2D, false, false,
 +                                          GLSL_TYPE_FLOAT);
 +      break;
 +
 +   case SpvOpTypeOpaque:
 +   case SpvOpTypeEvent:
 +   case SpvOpTypeDeviceEvent:
 +   case SpvOpTypeReserveId:
 +   case SpvOpTypeQueue:
 +   case SpvOpTypePipe:
 +   default:
 +      unreachable("Unhandled opcode");
 +   }
 +
 +   vtn_foreach_decoration(b, val, type_decoration_cb, NULL);
 +}
 +
 +static void
 +vtn_handle_constant(struct vtn_builder *b, SpvOp opcode,
 +                    const uint32_t *w, unsigned count)
 +{
 +   struct vtn_value *val = vtn_push_value(b, w[2], vtn_value_type_constant);
 +   val->const_type = vtn_value(b, w[1], vtn_value_type_type)->type->type;
 +   val->constant = ralloc(b, nir_constant);
 +   switch (opcode) {
 +   case SpvOpConstantTrue:
 +      assert(val->const_type == glsl_bool_type());
 +      val->constant->value.u[0] = NIR_TRUE;
 +      break;
 +   case SpvOpConstantFalse:
 +      assert(val->const_type == glsl_bool_type());
 +      val->constant->value.u[0] = NIR_FALSE;
 +      break;
 +   case SpvOpConstant:
 +      assert(glsl_type_is_scalar(val->const_type));
 +      val->constant->value.u[0] = w[3];
 +      break;
 +   case SpvOpConstantComposite: {
 +      unsigned elem_count = count - 3;
 +      nir_constant **elems = ralloc_array(b, nir_constant *, elem_count);
 +      for (unsigned i = 0; i < elem_count; i++)
 +         elems[i] = vtn_value(b, w[i + 3], vtn_value_type_constant)->constant;
 +
 +      switch (glsl_get_base_type(val->const_type)) {
 +      case GLSL_TYPE_UINT:
 +      case GLSL_TYPE_INT:
 +      case GLSL_TYPE_FLOAT:
 +      case GLSL_TYPE_BOOL:
 +         if (glsl_type_is_matrix(val->const_type)) {
 +            unsigned rows = glsl_get_vector_elements(val->const_type);
 +            assert(glsl_get_matrix_columns(val->const_type) == elem_count);
 +            for (unsigned i = 0; i < elem_count; i++)
 +               for (unsigned j = 0; j < rows; j++)
 +                  val->constant->value.u[rows * i + j] = elems[i]->value.u[j];
 +         } else {
 +            assert(glsl_type_is_vector(val->const_type));
 +            assert(glsl_get_vector_elements(val->const_type) == elem_count);
 +            for (unsigned i = 0; i < elem_count; i++)
 +               val->constant->value.u[i] = elems[i]->value.u[0];
 +         }
 +         ralloc_free(elems);
 +         break;
 +
 +      case GLSL_TYPE_STRUCT:
 +      case GLSL_TYPE_ARRAY:
 +         ralloc_steal(val->constant, elems);
 +         val->constant->elements = elems;
 +         break;
 +
 +      default:
 +         unreachable("Unsupported type for constants");
 +      }
 +      break;
 +   }
 +
 +   default:
 +      unreachable("Unhandled opcode");
 +   }
 +}
 +
 +static void
 +set_mode_system_value(nir_variable_mode *mode)
 +{
 +   assert(*mode == nir_var_system_value || *mode == nir_var_shader_in);
 +   *mode = nir_var_system_value;
 +}
 +
 +static void
 +validate_per_vertex_mode(struct vtn_builder *b, nir_variable_mode mode)
 +{
 +   switch (b->shader->stage) {
 +   case MESA_SHADER_VERTEX:
 +      assert(mode == nir_var_shader_out);
 +      break;
 +   case MESA_SHADER_GEOMETRY:
 +      assert(mode == nir_var_shader_out || mode == nir_var_shader_in);
 +      break;
 +   default:
 +      assert(!"Invalid shader stage");
 +   }
 +}
 +
 +static void
 +vtn_get_builtin_location(struct vtn_builder *b,
 +                         SpvBuiltIn builtin, int *location,
 +                         nir_variable_mode *mode)
 +{
 +   switch (builtin) {
 +   case SpvBuiltInPosition:
 +      *location = VARYING_SLOT_POS;
 +      validate_per_vertex_mode(b, *mode);
 +      break;
 +   case SpvBuiltInPointSize:
 +      *location = VARYING_SLOT_PSIZ;
 +      validate_per_vertex_mode(b, *mode);
 +      break;
 +   case SpvBuiltInClipDistance:
 +      *location = VARYING_SLOT_CLIP_DIST0; /* XXX CLIP_DIST1? */
 +      validate_per_vertex_mode(b, *mode);
 +      break;
 +   case SpvBuiltInCullDistance:
 +      /* XXX figure this out */
 +      unreachable("unhandled builtin");
 +   case SpvBuiltInVertexId:
 +      /* Vulkan defines VertexID to be zero-based and reserves the new
 +       * builtin keyword VertexIndex to indicate the non-zero-based value.
 +       */
 +      *location = SYSTEM_VALUE_VERTEX_ID_ZERO_BASE;
 +      set_mode_system_value(mode);
 +      break;
 +   case SpvBuiltInInstanceId:
 +      *location = SYSTEM_VALUE_INSTANCE_ID;
 +      set_mode_system_value(mode);
 +      break;
 +   case SpvBuiltInPrimitiveId:
 +      *location = VARYING_SLOT_PRIMITIVE_ID;
 +      *mode = nir_var_shader_out;
 +      break;
 +   case SpvBuiltInInvocationId:
 +      *location = SYSTEM_VALUE_INVOCATION_ID;
 +      set_mode_system_value(mode);
 +      break;
 +   case SpvBuiltInLayer:
 +      *location = VARYING_SLOT_LAYER;
 +      *mode = nir_var_shader_out;
 +      break;
 +   case SpvBuiltInTessLevelOuter:
 +   case SpvBuiltInTessLevelInner:
 +   case SpvBuiltInTessCoord:
 +   case SpvBuiltInPatchVertices:
 +      unreachable("no tessellation support");
 +   case SpvBuiltInFragCoord:
 +      *location = VARYING_SLOT_POS;
 +      assert(b->shader->stage == MESA_SHADER_FRAGMENT);
 +      assert(*mode == nir_var_shader_in);
 +      break;
 +   case SpvBuiltInPointCoord:
 +      *location = VARYING_SLOT_PNTC;
 +      assert(b->shader->stage == MESA_SHADER_FRAGMENT);
 +      assert(*mode == nir_var_shader_in);
 +      break;
 +   case SpvBuiltInFrontFacing:
 +      *location = VARYING_SLOT_FACE;
 +      assert(b->shader->stage == MESA_SHADER_FRAGMENT);
 +      assert(*mode == nir_var_shader_in);
 +      break;
 +   case SpvBuiltInSampleId:
 +      *location = SYSTEM_VALUE_SAMPLE_ID;
 +      set_mode_system_value(mode);
 +      break;
 +   case SpvBuiltInSamplePosition:
 +      *location = SYSTEM_VALUE_SAMPLE_POS;
 +      set_mode_system_value(mode);
 +      break;
 +   case SpvBuiltInSampleMask:
 +      *location = SYSTEM_VALUE_SAMPLE_MASK_IN; /* XXX out? */
 +      set_mode_system_value(mode);
 +      break;
 +   case SpvBuiltInFragDepth:
 +      *location = FRAG_RESULT_DEPTH;
 +      assert(b->shader->stage == MESA_SHADER_FRAGMENT);
 +      assert(*mode == nir_var_shader_out);
 +      break;
 +   case SpvBuiltInNumWorkgroups:
 +      *location = SYSTEM_VALUE_NUM_WORK_GROUPS;
 +      set_mode_system_value(mode);
 +      break;
 +   case SpvBuiltInWorkgroupSize:
 +      /* This should already be handled */
 +      unreachable("unsupported builtin");
 +      break;
 +   case SpvBuiltInWorkgroupId:
 +      *location = SYSTEM_VALUE_WORK_GROUP_ID;
 +      set_mode_system_value(mode);
 +      break;
 +   case SpvBuiltInLocalInvocationId:
 +      *location = SYSTEM_VALUE_LOCAL_INVOCATION_ID;
 +      set_mode_system_value(mode);
 +      break;
 +   case SpvBuiltInLocalInvocationIndex:
 +      *location = SYSTEM_VALUE_LOCAL_INVOCATION_INDEX;
 +      set_mode_system_value(mode);
 +      break;
 +   case SpvBuiltInGlobalInvocationId:
 +      *location = SYSTEM_VALUE_GLOBAL_INVOCATION_ID;
 +      set_mode_system_value(mode);
 +      break;
 +   case SpvBuiltInHelperInvocation:
 +   default:
 +      unreachable("unsupported builtin");
 +   }
 +}
 +
 +static void
 +var_decoration_cb(struct vtn_builder *b, struct vtn_value *val, int member,
 +                  const struct vtn_decoration *dec, void *void_var)
 +{
 +   assert(val->value_type == vtn_value_type_deref);
 +   assert(val->deref->deref.child == NULL);
 +   assert(val->deref->var == void_var);
 +
 +   nir_variable *var = void_var;
 +   switch (dec->decoration) {
 +   case SpvDecorationRelaxedPrecision:
 +      break; /* FIXME: Do nothing with this for now. */
 +   case SpvDecorationNoPerspective:
 +      var->data.interpolation = INTERP_QUALIFIER_NOPERSPECTIVE;
 +      break;
 +   case SpvDecorationFlat:
 +      var->data.interpolation = INTERP_QUALIFIER_FLAT;
 +      break;
 +   case SpvDecorationCentroid:
 +      var->data.centroid = true;
 +      break;
 +   case SpvDecorationSample:
 +      var->data.sample = true;
 +      break;
 +   case SpvDecorationInvariant:
 +      var->data.invariant = true;
 +      break;
 +   case SpvDecorationConstant:
 +      assert(var->constant_initializer != NULL);
 +      var->data.read_only = true;
 +      break;
 +   case SpvDecorationNonWritable:
 +      var->data.read_only = true;
 +      break;
 +   case SpvDecorationLocation:
 +      var->data.location = dec->literals[0];
 +      break;
 +   case SpvDecorationComponent:
 +      var->data.location_frac = dec->literals[0];
 +      break;
 +   case SpvDecorationIndex:
 +      var->data.explicit_index = true;
 +      var->data.index = dec->literals[0];
 +      break;
 +   case SpvDecorationBinding:
 +      var->data.explicit_binding = true;
 +      var->data.binding = dec->literals[0];
 +      break;
 +   case SpvDecorationDescriptorSet:
 +      var->data.descriptor_set = dec->literals[0];
 +      break;
 +   case SpvDecorationBuiltIn: {
 +      SpvBuiltIn builtin = dec->literals[0];
 +
 +      if (builtin == SpvBuiltInWorkgroupSize) {
 +         /* This shouldn't be a builtin.  It's actually a constant. */
 +         var->data.mode = nir_var_global;
 +         var->data.read_only = true;
 +
 +         nir_constant *val = ralloc(var, nir_constant);
 +         val->value.u[0] = b->shader->info.cs.local_size[0];
 +         val->value.u[1] = b->shader->info.cs.local_size[1];
 +         val->value.u[2] = b->shader->info.cs.local_size[2];
 +         var->constant_initializer = val;
 +         break;
 +      }
 +
 +      nir_variable_mode mode = var->data.mode;
 +      vtn_get_builtin_location(b, builtin, &var->data.location, &mode);
 +      var->data.explicit_location = true;
 +      var->data.mode = mode;
 +      if (mode == nir_var_shader_in || mode == nir_var_system_value)
 +         var->data.read_only = true;
 +
 +      if (builtin == SpvBuiltInFragCoord || builtin == SpvBuiltInSamplePosition)
 +         var->data.origin_upper_left = b->origin_upper_left;
 +
 +      if (mode == nir_var_shader_out)
 +         b->builtins[dec->literals[0]].out = var;
 +      else
 +         b->builtins[dec->literals[0]].in = var;
 +      break;
 +   }
 +   case SpvDecorationRowMajor:
 +   case SpvDecorationColMajor:
 +   case SpvDecorationGLSLShared:
 +   case SpvDecorationPatch:
 +   case SpvDecorationRestrict:
 +   case SpvDecorationAliased:
 +   case SpvDecorationVolatile:
 +   case SpvDecorationCoherent:
 +   case SpvDecorationNonReadable:
 +   case SpvDecorationUniform:
 +      /* This is really nice but we have no use for it right now. */
 +   case SpvDecorationCPacked:
 +   case SpvDecorationSaturatedConversion:
 +   case SpvDecorationStream:
 +   case SpvDecorationOffset:
 +   case SpvDecorationXfbBuffer:
 +   case SpvDecorationFuncParamAttr:
 +   case SpvDecorationFPRoundingMode:
 +   case SpvDecorationFPFastMathMode:
 +   case SpvDecorationLinkageAttributes:
 +   case SpvDecorationSpecId:
 +      break;
 +   default:
 +      unreachable("Unhandled variable decoration");
 +   }
 +}
 +
 +static nir_variable *
 +get_builtin_variable(struct vtn_builder *b,
 +                     nir_variable_mode mode,
 +                     const struct glsl_type *type,
 +                     SpvBuiltIn builtin)
 +{
 +   nir_variable *var;
 +   if (mode == nir_var_shader_out)
 +      var = b->builtins[builtin].out;
 +   else
 +      var = b->builtins[builtin].in;
 +
 +   if (!var) {
 +      int location;
 +      vtn_get_builtin_location(b, builtin, &location, &mode);
 +
 +      var = nir_variable_create(b->shader, mode, type, "builtin");
 +
 +      var->data.location = location;
 +      var->data.explicit_location = true;
 +
 +      if (builtin == SpvBuiltInFragCoord || builtin == SpvBuiltInSamplePosition)
 +         var->data.origin_upper_left = b->origin_upper_left;
 +
 +      if (mode == nir_var_shader_out)
 +         b->builtins[builtin].out = var;
 +      else
 +         b->builtins[builtin].in = var;
 +   }
 +
 +   return var;
 +}
 +
 +static struct vtn_ssa_value *
 +_vtn_variable_load(struct vtn_builder *b,
 +                   nir_deref_var *src_deref, nir_deref *src_deref_tail)
 +{
 +   struct vtn_ssa_value *val = rzalloc(b, struct vtn_ssa_value);
 +   val->type = src_deref_tail->type;
 +
 +   /* The deref tail may contain a deref to select a component of a vector (in
 +    * other words, it might not be an actual tail) so we have to save it away
 +    * here since we overwrite it later.
 +    */
 +   nir_deref *old_child = src_deref_tail->child;
 +
 +   if (glsl_type_is_vector_or_scalar(val->type)) {
 +      /* Terminate the deref chain in case there is one more link to pick
 +       * off a component of the vector.
 +       */
 +      src_deref_tail->child = NULL;
 +
 +      nir_intrinsic_instr *load =
 +         nir_intrinsic_instr_create(b->shader, nir_intrinsic_load_var);
 +      load->variables[0] =
 +         nir_deref_as_var(nir_copy_deref(load, &src_deref->deref));
 +      load->num_components = glsl_get_vector_elements(val->type);
 +      nir_ssa_dest_init(&load->instr, &load->dest, load->num_components, NULL);
 +
 +      nir_builder_instr_insert(&b->nb, &load->instr);
 +
 +      if (src_deref->var->data.mode == nir_var_uniform &&
 +          glsl_get_base_type(val->type) == GLSL_TYPE_BOOL) {
 +         /* Uniform boolean loads need to be fixed up since they're defined
 +          * to be zero/nonzero rather than NIR_FALSE/NIR_TRUE.
 +          */
 +         val->def = nir_ine(&b->nb, &load->dest.ssa, nir_imm_int(&b->nb, 0));
 +      } else {
 +         val->def = &load->dest.ssa;
 +      }
 +   } else if (glsl_get_base_type(val->type) == GLSL_TYPE_ARRAY ||
 +              glsl_type_is_matrix(val->type)) {
 +      unsigned elems = glsl_get_length(val->type);
 +      val->elems = ralloc_array(b, struct vtn_ssa_value *, elems);
 +
 +      nir_deref_array *deref = nir_deref_array_create(b);
 +      deref->deref_array_type = nir_deref_array_type_direct;
 +      deref->deref.type = glsl_get_array_element(val->type);
 +      src_deref_tail->child = &deref->deref;
 +      for (unsigned i = 0; i < elems; i++) {
 +         deref->base_offset = i;
 +         val->elems[i] = _vtn_variable_load(b, src_deref, &deref->deref);
 +      }
 +   } else {
 +      assert(glsl_get_base_type(val->type) == GLSL_TYPE_STRUCT);
 +      unsigned elems = glsl_get_length(val->type);
 +      val->elems = ralloc_array(b, struct vtn_ssa_value *, elems);
 +
 +      nir_deref_struct *deref = nir_deref_struct_create(b, 0);
 +      src_deref_tail->child = &deref->deref;
 +      for (unsigned i = 0; i < elems; i++) {
 +         deref->index = i;
 +         deref->deref.type = glsl_get_struct_field(val->type, i);
 +         val->elems[i] = _vtn_variable_load(b, src_deref, &deref->deref);
 +      }
 +   }
 +
 +   src_deref_tail->child = old_child;
 +
 +   return val;
 +}
 +
 +static void
 +_vtn_variable_store(struct vtn_builder *b,
 +                    nir_deref_var *dest_deref, nir_deref *dest_deref_tail,
 +                    struct vtn_ssa_value *src)
 +{
 +   nir_deref *old_child = dest_deref_tail->child;
 +
 +   if (glsl_type_is_vector_or_scalar(src->type)) {
 +      /* Terminate the deref chain in case there is one more link to pick
 +       * off a component of the vector.
 +       */
 +      dest_deref_tail->child = NULL;
 +
 +      nir_intrinsic_instr *store =
 +         nir_intrinsic_instr_create(b->shader, nir_intrinsic_store_var);
 +      store->variables[0] =
 +         nir_deref_as_var(nir_copy_deref(store, &dest_deref->deref));
 +      store->num_components = glsl_get_vector_elements(src->type);
++      store->const_index[0] = (1 << store->num_components) - 1;
 +      store->src[0] = nir_src_for_ssa(src->def);
 +
 +      nir_builder_instr_insert(&b->nb, &store->instr);
 +   } else if (glsl_get_base_type(src->type) == GLSL_TYPE_ARRAY ||
 +              glsl_type_is_matrix(src->type)) {
 +      unsigned elems = glsl_get_length(src->type);
 +
 +      nir_deref_array *deref = nir_deref_array_create(b);
 +      deref->deref_array_type = nir_deref_array_type_direct;
 +      deref->deref.type = glsl_get_array_element(src->type);
 +      dest_deref_tail->child = &deref->deref;
 +      for (unsigned i = 0; i < elems; i++) {
 +         deref->base_offset = i;
 +         _vtn_variable_store(b, dest_deref, &deref->deref, src->elems[i]);
 +      }
 +   } else {
 +      assert(glsl_get_base_type(src->type) == GLSL_TYPE_STRUCT);
 +      unsigned elems = glsl_get_length(src->type);
 +
 +      nir_deref_struct *deref = nir_deref_struct_create(b, 0);
 +      dest_deref_tail->child = &deref->deref;
 +      for (unsigned i = 0; i < elems; i++) {
 +         deref->index = i;
 +         deref->deref.type = glsl_get_struct_field(src->type, i);
 +         _vtn_variable_store(b, dest_deref, &deref->deref, src->elems[i]);
 +      }
 +   }
 +
 +   dest_deref_tail->child = old_child;
 +}
 +
 +static nir_ssa_def *
 +nir_vulkan_resource_index(nir_builder *b, unsigned set, unsigned binding,
 +                          nir_variable_mode mode, nir_ssa_def *array_index)
 +{
 +   if (array_index == NULL)
 +      array_index = nir_imm_int(b, 0);
 +
 +   nir_intrinsic_instr *instr =
 +      nir_intrinsic_instr_create(b->shader,
 +                                 nir_intrinsic_vulkan_resource_index);
 +   instr->src[0] = nir_src_for_ssa(array_index);
 +   instr->const_index[0] = set;
 +   instr->const_index[1] = binding;
 +   instr->const_index[2] = mode;
 +
 +   nir_ssa_dest_init(&instr->instr, &instr->dest, 1, NULL);
 +   nir_builder_instr_insert(b, &instr->instr);
 +
 +   return &instr->dest.ssa;
 +}
 +
 +static struct vtn_ssa_value *
 +_vtn_block_load(struct vtn_builder *b, nir_intrinsic_op op,
 +                unsigned set, unsigned binding, nir_variable_mode mode,
 +                nir_ssa_def *index, nir_ssa_def *offset, struct vtn_type *type)
 +{
 +   struct vtn_ssa_value *val = ralloc(b, struct vtn_ssa_value);
 +   val->type = type->type;
 +   val->transposed = NULL;
 +   if (glsl_type_is_vector_or_scalar(type->type)) {
 +      nir_intrinsic_instr *load = nir_intrinsic_instr_create(b->shader, op);
 +      load->num_components = glsl_get_vector_elements(type->type);
 +
 +      switch (op) {
 +      case nir_intrinsic_load_ubo:
 +      case nir_intrinsic_load_ssbo: {
 +         nir_ssa_def *res_index = nir_vulkan_resource_index(&b->nb,
 +                                                            set, binding,
 +                                                            mode, index);
 +         load->src[0] = nir_src_for_ssa(res_index);
 +         load->src[1] = nir_src_for_ssa(offset);
 +         break;
 +      }
 +
 +      case nir_intrinsic_load_push_constant:
 +         load->src[0] = nir_src_for_ssa(offset);
 +         break;
 +
 +      default:
 +         unreachable("Invalid block load intrinsic");
 +      }
 +
 +      nir_ssa_dest_init(&load->instr, &load->dest, load->num_components, NULL);
 +      nir_builder_instr_insert(&b->nb, &load->instr);
 +
 +      if (glsl_get_base_type(type->type) == GLSL_TYPE_BOOL) {
 +         /* Loads of booleans from externally visible memory need to be
 +          * fixed up since they're defined to be zero/nonzero rather than
 +          * NIR_FALSE/NIR_TRUE.
 +          */
 +         val->def = nir_ine(&b->nb, &load->dest.ssa, nir_imm_int(&b->nb, 0));
 +      } else {
 +         val->def = &load->dest.ssa;
 +      }
 +   } else {
 +      unsigned elems = glsl_get_length(type->type);
 +      val->elems = ralloc_array(b, struct vtn_ssa_value *, elems);
 +      if (glsl_type_is_struct(type->type)) {
 +         for (unsigned i = 0; i < elems; i++) {
 +            nir_ssa_def *child_offset =
 +               nir_iadd(&b->nb, offset, nir_imm_int(&b->nb, type->offsets[i]));
 +            val->elems[i] = _vtn_block_load(b, op, set, binding, mode, index,
 +                                            child_offset, type->members[i]);
 +         }
 +      } else {
 +         for (unsigned i = 0; i < elems; i++) {
 +            nir_ssa_def *child_offset =
 +               nir_iadd(&b->nb, offset, nir_imm_int(&b->nb, i * type->stride));
 +            val->elems[i] = _vtn_block_load(b, op, set, binding, mode, index,
 +                                            child_offset,type->array_element);
 +         }
 +      }
 +   }
 +
 +   return val;
 +}
 +
 +static void
 +vtn_block_get_offset(struct vtn_builder *b, nir_deref_var *src,
 +                     struct vtn_type **type, nir_deref *src_tail,
 +                     nir_ssa_def **index, nir_ssa_def **offset)
 +{
 +   nir_deref *deref = &src->deref;
 +
 +   if (deref->child->deref_type == nir_deref_type_array) {
 +      deref = deref->child;
 +      *type = (*type)->array_element;
 +      nir_deref_array *deref_array = nir_deref_as_array(deref);
 +      *index = nir_imm_int(&b->nb, deref_array->base_offset);
 +
 +      if (deref_array->deref_array_type == nir_deref_array_type_indirect)
 +         *index = nir_iadd(&b->nb, *index, deref_array->indirect.ssa);
 +   } else {
 +      *index = nir_imm_int(&b->nb, 0);
 +   }
 +
 +   *offset = nir_imm_int(&b->nb, 0);
 +   while (deref != src_tail) {
 +      deref = deref->child;
 +      switch (deref->deref_type) {
 +      case nir_deref_type_array: {
 +         nir_deref_array *deref_array = nir_deref_as_array(deref);
 +         nir_ssa_def *off = nir_imm_int(&b->nb, deref_array->base_offset);
 +
 +         if (deref_array->deref_array_type == nir_deref_array_type_indirect)
 +            off = nir_iadd(&b->nb, off, deref_array->indirect.ssa);
 +
 +         off = nir_imul(&b->nb, off, nir_imm_int(&b->nb, (*type)->stride));
 +         *offset = nir_iadd(&b->nb, *offset, off);
 +
 +         *type = (*type)->array_element;
 +         break;
 +      }
 +
 +      case nir_deref_type_struct: {
 +         nir_deref_struct *deref_struct = nir_deref_as_struct(deref);
 +
 +         unsigned elem_off = (*type)->offsets[deref_struct->index];
 +         *offset = nir_iadd(&b->nb, *offset, nir_imm_int(&b->nb, elem_off));
 +
 +         *type = (*type)->members[deref_struct->index];
 +         break;
 +      }
 +
 +      default:
 +         unreachable("unknown deref type");
 +      }
 +   }
 +}
 +
 +static struct vtn_ssa_value *
 +vtn_block_load(struct vtn_builder *b, nir_deref_var *src,
 +               struct vtn_type *type, nir_deref *src_tail)
 +{
 +   nir_ssa_def *index;
 +   nir_ssa_def *offset;
 +   vtn_block_get_offset(b, src, &type, src_tail, &index, &offset);
 +
 +   nir_intrinsic_op op;
 +   if (src->var->data.mode == nir_var_uniform) {
 +      if (src->var->data.descriptor_set >= 0) {
 +         /* UBO load */
 +         assert(src->var->data.binding >= 0);
 +
 +         op = nir_intrinsic_load_ubo;
 +      } else {
 +         /* Push constant load */
 +         assert(src->var->data.descriptor_set == -1 &&
 +                src->var->data.binding == -1);
 +
 +         op = nir_intrinsic_load_push_constant;
 +      }
 +   } else {
 +      assert(src->var->data.mode == nir_var_shader_storage);
 +      op = nir_intrinsic_load_ssbo;
 +   }
 +
 +   return _vtn_block_load(b, op, src->var->data.descriptor_set,
 +                          src->var->data.binding, src->var->data.mode,
 +                          index, offset, type);
 +}
 +
 +/*
 + * Gets the NIR-level deref tail, which may have as a child an array deref
 + * selecting which component due to OpAccessChain supporting per-component
 + * indexing in SPIR-V.
 + */
 +
 +static nir_deref *
 +get_deref_tail(nir_deref_var *deref)
 +{
 +   nir_deref *cur = &deref->deref;
 +   while (!glsl_type_is_vector_or_scalar(cur->type) && cur->child)
 +      cur = cur->child;
 +
 +   return cur;
 +}
 +
 +static nir_ssa_def *vtn_vector_extract(struct vtn_builder *b,
 +                                       nir_ssa_def *src, unsigned index);
 +
 +static nir_ssa_def *vtn_vector_extract_dynamic(struct vtn_builder *b,
 +                                               nir_ssa_def *src,
 +                                               nir_ssa_def *index);
 +
 +static bool
 +variable_is_external_block(nir_variable *var)
 +{
 +   return var->interface_type &&
 +          glsl_type_is_struct(var->interface_type) &&
 +          (var->data.mode == nir_var_uniform ||
 +           var->data.mode == nir_var_shader_storage);
 +}
 +
 +static struct vtn_ssa_value *
 +vtn_variable_load(struct vtn_builder *b, nir_deref_var *src,
 +                  struct vtn_type *src_type)
 +{
 +   nir_deref *src_tail = get_deref_tail(src);
 +
 +   struct vtn_ssa_value *val;
 +   if (variable_is_external_block(src->var))
 +      val = vtn_block_load(b, src, src_type, src_tail);
 +   else
 +      val = _vtn_variable_load(b, src, src_tail);
 +
 +   if (src_tail->child) {
 +      nir_deref_array *vec_deref = nir_deref_as_array(src_tail->child);
 +      assert(vec_deref->deref.child == NULL);
 +      val->type = vec_deref->deref.type;
 +      if (vec_deref->deref_array_type == nir_deref_array_type_direct)
 +         val->def = vtn_vector_extract(b, val->def, vec_deref->base_offset);
 +      else
 +         val->def = vtn_vector_extract_dynamic(b, val->def,
 +                                               vec_deref->indirect.ssa);
 +   }
 +
 +   return val;
 +}
 +
 +static void
 +_vtn_block_store(struct vtn_builder *b, nir_intrinsic_op op,
 +                 struct vtn_ssa_value *src, unsigned set, unsigned binding,
 +                 nir_variable_mode mode, nir_ssa_def *index,
 +                 nir_ssa_def *offset, struct vtn_type *type)
 +{
 +   assert(src->type == type->type);
 +   if (glsl_type_is_vector_or_scalar(type->type)) {
 +      nir_intrinsic_instr *store = nir_intrinsic_instr_create(b->shader, op);
 +      store->num_components = glsl_get_vector_elements(type->type);
 +      store->const_index[0] = (1 << store->num_components) - 1;
 +      store->src[0] = nir_src_for_ssa(src->def);
 +
 +      nir_ssa_def *res_index = nir_vulkan_resource_index(&b->nb,
 +                                                         set, binding,
 +                                                         mode, index);
 +      store->src[1] = nir_src_for_ssa(res_index);
 +      store->src[2] = nir_src_for_ssa(offset);
 +
 +      nir_builder_instr_insert(&b->nb, &store->instr);
 +   } else {
 +      unsigned elems = glsl_get_length(type->type);
 +      if (glsl_type_is_struct(type->type)) {
 +         for (unsigned i = 0; i < elems; i++) {
 +            nir_ssa_def *child_offset =
 +               nir_iadd(&b->nb, offset, nir_imm_int(&b->nb, type->offsets[i]));
 +            _vtn_block_store(b, op, src->elems[i], set, binding, mode,
 +                             index, child_offset, type->members[i]);
 +         }
 +      } else {
 +         for (unsigned i = 0; i < elems; i++) {
 +            nir_ssa_def *child_offset =
 +               nir_iadd(&b->nb, offset, nir_imm_int(&b->nb, i * type->stride));
 +            _vtn_block_store(b, op, src->elems[i], set, binding, mode,
 +                             index, child_offset, type->array_element);
 +         }
 +      }
 +   }
 +}
 +
 +static void
 +vtn_block_store(struct vtn_builder *b, struct vtn_ssa_value *src,
 +                nir_deref_var *dest, struct vtn_type *type,
 +                nir_deref *dest_tail)
 +{
 +   nir_ssa_def *index;
 +   nir_ssa_def *offset;
 +   vtn_block_get_offset(b, dest, &type, dest_tail, &index, &offset);
 +
 +   nir_intrinsic_op op = nir_intrinsic_store_ssbo;
 +
 +   return _vtn_block_store(b, op, src, dest->var->data.descriptor_set,
 +                           dest->var->data.binding, dest->var->data.mode,
 +                           index, offset, type);
 +}
 +
 +static nir_ssa_def * vtn_vector_insert(struct vtn_builder *b,
 +                                       nir_ssa_def *src, nir_ssa_def *insert,
 +                                       unsigned index);
 +
 +static nir_ssa_def * vtn_vector_insert_dynamic(struct vtn_builder *b,
 +                                               nir_ssa_def *src,
 +                                               nir_ssa_def *insert,
 +                                               nir_ssa_def *index);
 +static void
 +vtn_variable_store(struct vtn_builder *b, struct vtn_ssa_value *src,
 +                   nir_deref_var *dest, struct vtn_type *dest_type)
 +{
 +   nir_deref *dest_tail = get_deref_tail(dest);
 +   if (variable_is_external_block(dest->var)) {
 +      assert(dest->var->data.mode == nir_var_shader_storage);
 +      vtn_block_store(b, src, dest, dest_type, dest_tail);
 +   } else {
 +      if (dest_tail->child) {
 +         struct vtn_ssa_value *val = _vtn_variable_load(b, dest, dest_tail);
 +         nir_deref_array *deref = nir_deref_as_array(dest_tail->child);
 +         assert(deref->deref.child == NULL);
 +         if (deref->deref_array_type == nir_deref_array_type_direct)
 +            val->def = vtn_vector_insert(b, val->def, src->def,
 +                                         deref->base_offset);
 +         else
 +            val->def = vtn_vector_insert_dynamic(b, val->def, src->def,
 +                                                 deref->indirect.ssa);
 +         _vtn_variable_store(b, dest, dest_tail, val);
 +      } else {
 +         _vtn_variable_store(b, dest, dest_tail, src);
 +      }
 +   }
 +}
 +
 +static void
 +vtn_variable_copy(struct vtn_builder *b, nir_deref_var *src,
 +                  nir_deref_var *dest, struct vtn_type *type)
 +{
 +   nir_deref *src_tail = get_deref_tail(src);
 +
 +   if (src_tail->child || src->var->interface_type) {
 +      assert(get_deref_tail(dest)->child);
 +      struct vtn_ssa_value *val = vtn_variable_load(b, src, type);
 +      vtn_variable_store(b, val, dest, type);
 +   } else {
 +      nir_intrinsic_instr *copy =
 +         nir_intrinsic_instr_create(b->shader, nir_intrinsic_copy_var);
 +      copy->variables[0] = nir_deref_as_var(nir_copy_deref(copy, &dest->deref));
 +      copy->variables[1] = nir_deref_as_var(nir_copy_deref(copy, &src->deref));
 +
 +      nir_builder_instr_insert(&b->nb, &copy->instr);
 +   }
 +}
 +
 +/* Tries to compute the size of an interface block based on the strides and
 + * offsets that are provided to us in the SPIR-V source.
 + */
 +static unsigned
 +vtn_type_block_size(struct vtn_type *type)
 +{
 +   enum glsl_base_type base_type = glsl_get_base_type(type->type);
 +   switch (base_type) {
 +   case GLSL_TYPE_UINT:
 +   case GLSL_TYPE_INT:
 +   case GLSL_TYPE_FLOAT:
 +   case GLSL_TYPE_BOOL:
 +   case GLSL_TYPE_DOUBLE: {
 +      unsigned cols = type->row_major ? glsl_get_vector_elements(type->type) :
 +                                        glsl_get_matrix_columns(type->type);
 +      if (cols > 1) {
 +         assert(type->stride > 0);
 +         return type->stride * cols;
 +      } else if (base_type == GLSL_TYPE_DOUBLE) {
 +         return glsl_get_vector_elements(type->type) * 8;
 +      } else {
 +         return glsl_get_vector_elements(type->type) * 4;
 +      }
 +   }
 +
 +   case GLSL_TYPE_STRUCT:
 +   case GLSL_TYPE_INTERFACE: {
 +      unsigned size = 0;
 +      unsigned num_fields = glsl_get_length(type->type);
 +      for (unsigned f = 0; f < num_fields; f++) {
 +         unsigned field_end = type->offsets[f] +
 +                              vtn_type_block_size(type->members[f]);
 +         size = MAX2(size, field_end);
 +      }
 +      return size;
 +   }
 +
 +   case GLSL_TYPE_ARRAY:
 +      assert(type->stride > 0);
 +      assert(glsl_get_length(type->type) > 0);
 +      return type->stride * glsl_get_length(type->type);
 +
 +   default:
 +      assert(!"Invalid block type");
 +      return 0;
 +   }
 +}
 +
 +static bool
 +is_interface_type(struct vtn_type *type)
 +{
 +   return type->block || type->buffer_block ||
 +          glsl_type_is_sampler(type->type) ||
 +          glsl_type_is_image(type->type);
 +}
 +
 +static void
 +vtn_handle_variables(struct vtn_builder *b, SpvOp opcode,
 +                     const uint32_t *w, unsigned count)
 +{
 +   switch (opcode) {
 +   case SpvOpVariable: {
 +      struct vtn_type *type =
 +         vtn_value(b, w[1], vtn_value_type_type)->type;
 +      struct vtn_value *val = vtn_push_value(b, w[2], vtn_value_type_deref);
 +
 +      nir_variable *var = rzalloc(b->shader, nir_variable);
 +
 +      var->type = type->type;
 +      var->name = ralloc_strdup(var, val->name);
 +
 +      struct vtn_type *interface_type;
 +      if (is_interface_type(type)) {
 +         interface_type = type;
 +      } else if (glsl_type_is_array(type->type) &&
 +                 is_interface_type(type->array_element)) {
 +         interface_type = type->array_element;
 +      } else {
 +         interface_type = NULL;
 +      }
 +
 +      if (interface_type)
 +         var->interface_type = interface_type->type;
 +
 +      switch ((SpvStorageClass)w[3]) {
 +      case SpvStorageClassUniform:
 +      case SpvStorageClassUniformConstant:
 +         if (interface_type && interface_type->buffer_block) {
 +            var->data.mode = nir_var_shader_storage;
 +            b->shader->info.num_ssbos++;
 +         } else {
 +            /* UBO's and samplers */
 +            var->data.mode = nir_var_uniform;
 +            var->data.read_only = true;
 +            if (interface_type) {
 +               if (glsl_type_is_image(interface_type->type)) {
 +                  b->shader->info.num_images++;
 +                  var->data.image.format = interface_type->image_format;
 +               } else if (glsl_type_is_sampler(interface_type->type)) {
 +                  b->shader->info.num_textures++;
 +               } else {
 +                  assert(glsl_type_is_struct(interface_type->type));
 +                  b->shader->info.num_ubos++;
 +               }
 +            }
 +         }
 +         break;
 +      case SpvStorageClassPushConstant:
 +         assert(interface_type && interface_type->block);
 +         var->data.mode = nir_var_uniform;
 +         var->data.read_only = true;
 +         var->data.descriptor_set = -1;
 +         var->data.binding = -1;
 +
 +         /* We have exactly one push constant block */
 +         assert(b->shader->num_uniforms == 0);
 +         b->shader->num_uniforms = vtn_type_block_size(type) * 4;
 +         break;
 +      case SpvStorageClassInput:
 +         var->data.mode = nir_var_shader_in;
 +         var->data.read_only = true;
 +         break;
 +      case SpvStorageClassOutput:
 +         var->data.mode = nir_var_shader_out;
 +         break;
 +      case SpvStorageClassPrivate:
 +         var->data.mode = nir_var_global;
 +         break;
 +      case SpvStorageClassFunction:
 +         var->data.mode = nir_var_local;
 +         break;
 +      case SpvStorageClassWorkgroup:
 +      case SpvStorageClassCrossWorkgroup:
 +      case SpvStorageClassGeneric:
 +      case SpvStorageClassAtomicCounter:
 +      default:
 +         unreachable("Unhandled variable storage class");
 +      }
 +
 +      if (count > 4) {
 +         assert(count == 5);
 +         var->constant_initializer =
 +            vtn_value(b, w[4], vtn_value_type_constant)->constant;
 +      }
 +
 +      val->deref = nir_deref_var_create(b, var);
 +      val->deref_type = type;
 +
 +      /* We handle decorations first because decorations might give us
 +       * location information.  We use the data.explicit_location field to
 +       * note that the location provided is the "final" location.  If
 +       * data.explicit_location == false, this means that it's relative to
 +       * whatever the base location is.
 +       */
 +      vtn_foreach_decoration(b, val, var_decoration_cb, var);
 +
 +      if (!var->data.explicit_location) {
 +         if (b->execution_model == SpvExecutionModelFragment &&
 +             var->data.mode == nir_var_shader_out) {
 +            var->data.location += FRAG_RESULT_DATA0;
 +         } else if (b->execution_model == SpvExecutionModelVertex &&
 +                    var->data.mode == nir_var_shader_in) {
 +            var->data.location += VERT_ATTRIB_GENERIC0;
 +         } else if (var->data.mode == nir_var_shader_in ||
 +                    var->data.mode == nir_var_shader_out) {
 +            var->data.location += VARYING_SLOT_VAR0;
 +         }
 +      }
 +
 +      /* Interface block variables aren't actually going to be referenced
 +       * by the generated NIR, so we don't put them in the list
 +       */
 +      if (interface_type && glsl_type_is_struct(interface_type->type))
 +         break;
 +
 +      if (var->data.mode == nir_var_local) {
 +         nir_function_impl_add_variable(b->impl, var);
 +      } else {
 +         nir_shader_add_variable(b->shader, var);
 +      }
 +
 +      break;
 +   }
 +
 +   case SpvOpAccessChain:
 +   case SpvOpInBoundsAccessChain: {
 +      nir_deref_var *base;
 +      struct vtn_value *base_val = vtn_untyped_value(b, w[3]);
 +      if (base_val->value_type == vtn_value_type_sampled_image) {
 +         /* This is rather insane.  SPIR-V allows you to use OpSampledImage
 +          * to combine an array of images with a single sampler to get an
 +          * array of sampled images that all share the same sampler.
 +          * Fortunately, this means that we can more-or-less ignore the
 +          * sampler when crawling the access chain, but it does leave us
 +          * with this rather awkward little special-case.
 +          */
 +         base = base_val->sampled_image->image;
 +      } else {
 +         assert(base_val->value_type == vtn_value_type_deref);
 +         base = base_val->deref;
 +      }
 +
 +      nir_deref_var *deref = nir_deref_as_var(nir_copy_deref(b, &base->deref));
 +      struct vtn_type *deref_type = vtn_value(b, w[3], vtn_value_type_deref)->deref_type;
 +
 +      nir_deref *tail = &deref->deref;
 +      while (tail->child)
 +         tail = tail->child;
 +
 +      for (unsigned i = 0; i < count - 4; i++) {
 +         assert(w[i + 4] < b->value_id_bound);
 +         struct vtn_value *idx_val = &b->values[w[i + 4]];
 +
 +         enum glsl_base_type base_type = glsl_get_base_type(tail->type);
 +         switch (base_type) {
 +         case GLSL_TYPE_UINT:
 +         case GLSL_TYPE_INT:
 +         case GLSL_TYPE_FLOAT:
 +         case GLSL_TYPE_DOUBLE:
 +         case GLSL_TYPE_BOOL:
 +         case GLSL_TYPE_ARRAY: {
 +            nir_deref_array *deref_arr = nir_deref_array_create(b);
 +            if (base_type == GLSL_TYPE_ARRAY ||
 +                glsl_type_is_matrix(tail->type)) {
 +               deref_type = deref_type->array_element;
 +            } else {
 +               assert(glsl_type_is_vector(tail->type));
 +               deref_type = ralloc(b, struct vtn_type);
 +               deref_type->type = glsl_scalar_type(base_type);
 +            }
 +
 +            deref_arr->deref.type = deref_type->type;
 +
 +            if (idx_val->value_type == vtn_value_type_constant) {
 +               unsigned idx = idx_val->constant->value.u[0];
 +               deref_arr->deref_array_type = nir_deref_array_type_direct;
 +               deref_arr->base_offset = idx;
 +            } else {
 +               assert(idx_val->value_type == vtn_value_type_ssa);
 +               assert(glsl_type_is_scalar(idx_val->ssa->type));
 +               deref_arr->deref_array_type = nir_deref_array_type_indirect;
 +               deref_arr->base_offset = 0;
 +               deref_arr->indirect = nir_src_for_ssa(idx_val->ssa->def);
 +            }
 +            tail->child = &deref_arr->deref;
 +            break;
 +         }
 +
 +         case GLSL_TYPE_STRUCT: {
 +            assert(idx_val->value_type == vtn_value_type_constant);
 +            unsigned idx = idx_val->constant->value.u[0];
 +            deref_type = deref_type->members[idx];
 +            nir_deref_struct *deref_struct = nir_deref_struct_create(b, idx);
 +            deref_struct->deref.type = deref_type->type;
 +            tail->child = &deref_struct->deref;
 +            break;
 +         }
 +         default:
 +            unreachable("Invalid type for deref");
 +         }
 +
 +         if (deref_type->is_builtin) {
 +            /* If we encounter a builtin, we throw away the ress of the
 +             * access chain, jump to the builtin, and keep building.
 +             */
 +            const struct glsl_type *builtin_type = deref_type->type;
 +
 +            nir_deref_array *per_vertex_deref = NULL;
 +            if (glsl_type_is_array(base->var->type)) {
 +               /* This builtin is a per-vertex builtin */
 +               assert(b->shader->stage == MESA_SHADER_GEOMETRY);
 +               assert(base->var->data.mode == nir_var_shader_in);
 +               builtin_type = glsl_array_type(builtin_type,
 +                                              b->shader->info.gs.vertices_in);
 +
 +               /* The first non-var deref should be an array deref. */
 +               assert(deref->deref.child->deref_type ==
 +                      nir_deref_type_array);
 +               per_vertex_deref = nir_deref_as_array(deref->deref.child);
 +            }
 +
 +            nir_variable *builtin = get_builtin_variable(b,
 +                                                         base->var->data.mode,
 +                                                         builtin_type,
 +                                                         deref_type->builtin);
 +            deref = nir_deref_var_create(b, builtin);
 +
 +            if (per_vertex_deref) {
 +               /* Since deref chains start at the variable, we can just
 +                * steal that link and use it.
 +                */
 +               deref->deref.child = &per_vertex_deref->deref;
 +               per_vertex_deref->deref.child = NULL;
 +               per_vertex_deref->deref.type =
 +                  glsl_get_array_element(builtin_type);
 +
 +               tail = &per_vertex_deref->deref;
 +            } else {
 +               tail = &deref->deref;
 +            }
 +         } else {
 +            tail = tail->child;
 +         }
 +      }
 +
 +      /* For uniform blocks, we don't resolve the access chain until we
 +       * actually access the variable, so we need to keep around the original
 +       * type of the variable.
 +       */
 +      if (variable_is_external_block(base->var))
 +         deref_type = vtn_value(b, w[3], vtn_value_type_deref)->deref_type;
 +
 +      if (base_val->value_type == vtn_value_type_sampled_image) {
 +         struct vtn_value *val =
 +            vtn_push_value(b, w[2], vtn_value_type_sampled_image);
 +         val->sampled_image = ralloc(b, struct vtn_sampled_image);
 +         val->sampled_image->image = deref;
 +         val->sampled_image->sampler = base_val->sampled_image->sampler;
 +      } else {
 +         struct vtn_value *val = vtn_push_value(b, w[2], vtn_value_type_deref);
 +         val->deref = deref;
 +         val->deref_type = deref_type;
 +      }
 +
 +      break;
 +   }
 +
 +   case SpvOpCopyMemory: {
 +      nir_deref_var *dest = vtn_value(b, w[1], vtn_value_type_deref)->deref;
 +      nir_deref_var *src = vtn_value(b, w[2], vtn_value_type_deref)->deref;
 +      struct vtn_type *type =
 +         vtn_value(b, w[1], vtn_value_type_deref)->deref_type;
 +
 +      vtn_variable_copy(b, src, dest, type);
 +      break;
 +   }
 +
 +   case SpvOpLoad: {
 +      nir_deref_var *src = vtn_value(b, w[3], vtn_value_type_deref)->deref;
 +      struct vtn_type *src_type =
 +         vtn_value(b, w[3], vtn_value_type_deref)->deref_type;
 +
 +      if (src->var->interface_type &&
 +          (glsl_type_is_sampler(src->var->interface_type) ||
 +           glsl_type_is_image(src->var->interface_type))) {
 +         vtn_push_value(b, w[2], vtn_value_type_deref)->deref = src;
 +         return;
 +      }
 +
 +      struct vtn_value *val = vtn_push_value(b, w[2], vtn_value_type_ssa);
 +      val->ssa = vtn_variable_load(b, src, src_type);
 +      break;
 +   }
 +
 +   case SpvOpStore: {
 +      nir_deref_var *dest = vtn_value(b, w[1], vtn_value_type_deref)->deref;
 +      struct vtn_type *dest_type =
 +         vtn_value(b, w[1], vtn_value_type_deref)->deref_type;
 +      struct vtn_ssa_value *src = vtn_ssa_value(b, w[2]);
 +      vtn_variable_store(b, src, dest, dest_type);
 +      break;
 +   }
 +
 +   case SpvOpCopyMemorySized:
 +   case SpvOpArrayLength:
 +   default:
 +      unreachable("Unhandled opcode");
 +   }
 +}
 +
 +static void
 +vtn_handle_function_call(struct vtn_builder *b, SpvOp opcode,
 +                         const uint32_t *w, unsigned count)
 +{
 +   struct nir_function_overload *overload =
 +      vtn_value(b, w[3], vtn_value_type_function)->func->impl->overload;
 +
 +   nir_call_instr *call = nir_call_instr_create(b->nb.shader, overload);
 +   for (unsigned i = 0; i < call->num_params; i++) {
 +      unsigned arg_id = w[4 + i];
 +      struct vtn_value *arg = vtn_untyped_value(b, arg_id);
 +      if (arg->value_type == vtn_value_type_deref) {
 +         call->params[i] =
 +            nir_deref_as_var(nir_copy_deref(call, &arg->deref->deref));
 +      } else {
 +         struct vtn_ssa_value *arg_ssa = vtn_ssa_value(b, arg_id);
 +
 +         /* Make a temporary to store the argument in */
 +         nir_variable *tmp =
 +            nir_local_variable_create(b->impl, arg_ssa->type, "arg_tmp");
 +         call->params[i] = nir_deref_var_create(call, tmp);
 +
 +         vtn_variable_store(b, arg_ssa, call->params[i], arg->type);
 +      }
 +   }
 +
 +   nir_variable *out_tmp = NULL;
 +   if (!glsl_type_is_void(overload->return_type)) {
 +      out_tmp = nir_local_variable_create(b->impl, overload->return_type,
 +                                          "out_tmp");
 +      call->return_deref = nir_deref_var_create(call, out_tmp);
 +   }
 +
 +   nir_builder_instr_insert(&b->nb, &call->instr);
 +
 +   if (glsl_type_is_void(overload->return_type)) {
 +      vtn_push_value(b, w[2], vtn_value_type_undef);
 +   } else {
 +      struct vtn_type *rettype = vtn_value(b, w[1], vtn_value_type_type)->type;
 +      struct vtn_value *retval = vtn_push_value(b, w[2], vtn_value_type_ssa);
 +      retval->ssa = vtn_variable_load(b, call->return_deref, rettype);
 +   }
 +}
 +
 +static struct vtn_ssa_value *
 +vtn_create_ssa_value(struct vtn_builder *b, const struct glsl_type *type)
 +{
 +   struct vtn_ssa_value *val = rzalloc(b, struct vtn_ssa_value);
 +   val->type = type;
 +
 +   if (!glsl_type_is_vector_or_scalar(type)) {
 +      unsigned elems = glsl_get_length(type);
 +      val->elems = ralloc_array(b, struct vtn_ssa_value *, elems);
 +      for (unsigned i = 0; i < elems; i++) {
 +         const struct glsl_type *child_type;
 +
 +         switch (glsl_get_base_type(type)) {
 +         case GLSL_TYPE_INT:
 +         case GLSL_TYPE_UINT:
 +         case GLSL_TYPE_BOOL:
 +         case GLSL_TYPE_FLOAT:
 +         case GLSL_TYPE_DOUBLE:
 +            child_type = glsl_get_column_type(type);
 +            break;
 +         case GLSL_TYPE_ARRAY:
 +            child_type = glsl_get_array_element(type);
 +            break;
 +         case GLSL_TYPE_STRUCT:
 +            child_type = glsl_get_struct_field(type, i);
 +            break;
 +         default:
 +            unreachable("unkown base type");
 +         }
 +
 +         val->elems[i] = vtn_create_ssa_value(b, child_type);
 +      }
 +   }
 +
 +   return val;
 +}
 +
 +static nir_tex_src
 +vtn_tex_src(struct vtn_builder *b, unsigned index, nir_tex_src_type type)
 +{
 +   nir_tex_src src;
 +   src.src = nir_src_for_ssa(vtn_ssa_value(b, index)->def);
 +   src.src_type = type;
 +   return src;
 +}
 +
 +static void
 +vtn_handle_texture(struct vtn_builder *b, SpvOp opcode,
 +                   const uint32_t *w, unsigned count)
 +{
 +   if (opcode == SpvOpSampledImage) {
 +      struct vtn_value *val =
 +         vtn_push_value(b, w[2], vtn_value_type_sampled_image);
 +      val->sampled_image = ralloc(b, struct vtn_sampled_image);
 +      val->sampled_image->image =
 +         vtn_value(b, w[3], vtn_value_type_deref)->deref;
 +      val->sampled_image->sampler =
 +         vtn_value(b, w[4], vtn_value_type_deref)->deref;
 +      return;
 +   }
 +
 +   struct vtn_value *val = vtn_push_value(b, w[2], vtn_value_type_ssa);
 +
 +   struct vtn_sampled_image sampled;
 +   struct vtn_value *sampled_val = vtn_untyped_value(b, w[3]);
 +   if (sampled_val->value_type == vtn_value_type_sampled_image) {
 +      sampled = *sampled_val->sampled_image;
 +   } else {
 +      assert(sampled_val->value_type == vtn_value_type_deref);
 +      sampled.image = NULL;
 +      sampled.sampler = sampled_val->deref;
 +   }
 +
 +   nir_tex_src srcs[8]; /* 8 should be enough */
 +   nir_tex_src *p = srcs;
 +
 +   unsigned idx = 4;
 +
 +   unsigned coord_components = 0;
 +   switch (opcode) {
 +   case SpvOpImageSampleImplicitLod:
 +   case SpvOpImageSampleExplicitLod:
 +   case SpvOpImageSampleDrefImplicitLod:
 +   case SpvOpImageSampleDrefExplicitLod:
 +   case SpvOpImageSampleProjImplicitLod:
 +   case SpvOpImageSampleProjExplicitLod:
 +   case SpvOpImageSampleProjDrefImplicitLod:
 +   case SpvOpImageSampleProjDrefExplicitLod:
 +   case SpvOpImageFetch:
 +   case SpvOpImageGather:
 +   case SpvOpImageDrefGather:
 +   case SpvOpImageQueryLod: {
 +      /* All these types have the coordinate as their first real argument */
 +      struct vtn_ssa_value *coord = vtn_ssa_value(b, w[idx++]);
 +      coord_components = glsl_get_vector_elements(coord->type);
 +      p->src = nir_src_for_ssa(coord->def);
 +      p->src_type = nir_tex_src_coord;
 +      p++;
 +      break;
 +   }
 +
 +   default:
 +      break;
 +   }
 +
 +   /* These all have an explicit depth value as their next source */
 +   switch (opcode) {
 +   case SpvOpImageSampleDrefImplicitLod:
 +   case SpvOpImageSampleDrefExplicitLod:
 +   case SpvOpImageSampleProjDrefImplicitLod:
 +   case SpvOpImageSampleProjDrefExplicitLod:
 +      (*p++) = vtn_tex_src(b, w[idx++], nir_tex_src_comparitor);
 +      break;
 +   default:
 +      break;
 +   }
 +
 +   /* Figure out the base texture operation */
 +   nir_texop texop;
 +   switch (opcode) {
 +   case SpvOpImageSampleImplicitLod:
 +   case SpvOpImageSampleDrefImplicitLod:
 +   case SpvOpImageSampleProjImplicitLod:
 +   case SpvOpImageSampleProjDrefImplicitLod:
 +      texop = nir_texop_tex;
 +      break;
 +
 +   case SpvOpImageSampleExplicitLod:
 +   case SpvOpImageSampleDrefExplicitLod:
 +   case SpvOpImageSampleProjExplicitLod:
 +   case SpvOpImageSampleProjDrefExplicitLod:
 +      texop = nir_texop_txl;
 +      break;
 +
 +   case SpvOpImageFetch:
 +      texop = nir_texop_txf;
 +      break;
 +
 +   case SpvOpImageGather:
 +   case SpvOpImageDrefGather:
 +      texop = nir_texop_tg4;
 +      break;
 +
 +   case SpvOpImageQuerySizeLod:
 +   case SpvOpImageQuerySize:
 +      texop = nir_texop_txs;
 +      break;
 +
 +   case SpvOpImageQueryLod:
 +      texop = nir_texop_lod;
 +      break;
 +
 +   case SpvOpImageQueryLevels:
 +      texop = nir_texop_query_levels;
 +      break;
 +
 +   case SpvOpImageQuerySamples:
 +   default:
 +      unreachable("Unhandled opcode");
 +   }
 +
 +   /* Now we need to handle some number of optional arguments */
 +   if (idx < count) {
 +      uint32_t operands = w[idx++];
 +
 +      if (operands & SpvImageOperandsBiasMask) {
 +         assert(texop == nir_texop_tex);
 +         texop = nir_texop_txb;
 +         (*p++) = vtn_tex_src(b, w[idx++], nir_tex_src_bias);
 +      }
 +
 +      if (operands & SpvImageOperandsLodMask) {
 +         assert(texop == nir_texop_txl || texop == nir_texop_txf ||
 +                texop == nir_texop_txs);
 +         (*p++) = vtn_tex_src(b, w[idx++], nir_tex_src_lod);
 +      }
 +
 +      if (operands & SpvImageOperandsGradMask) {
 +         assert(texop == nir_texop_tex);
 +         texop = nir_texop_txd;
 +         (*p++) = vtn_tex_src(b, w[idx++], nir_tex_src_ddx);
 +         (*p++) = vtn_tex_src(b, w[idx++], nir_tex_src_ddy);
 +      }
 +
 +      if (operands & SpvImageOperandsOffsetMask ||
 +          operands & SpvImageOperandsConstOffsetMask)
 +         (*p++) = vtn_tex_src(b, w[idx++], nir_tex_src_offset);
 +
 +      if (operands & SpvImageOperandsConstOffsetsMask)
 +         assert(!"Constant offsets to texture gather not yet implemented");
 +
 +      if (operands & SpvImageOperandsSampleMask) {
 +         assert(texop == nir_texop_txf);
 +         texop = nir_texop_txf_ms;
 +         (*p++) = vtn_tex_src(b, w[idx++], nir_tex_src_ms_index);
 +      }
 +   }
 +   /* We should have now consumed exactly all of the arguments */
 +   assert(idx == count);
 +
 +   nir_tex_instr *instr = nir_tex_instr_create(b->shader, p - srcs);
 +
 +   const struct glsl_type *sampler_type =
 +      nir_deref_tail(&sampled.sampler->deref)->type;
 +   instr->sampler_dim = glsl_get_sampler_dim(sampler_type);
 +
 +   switch (glsl_get_sampler_result_type(sampler_type)) {
 +   case GLSL_TYPE_FLOAT:   instr->dest_type = nir_type_float;     break;
 +   case GLSL_TYPE_INT:     instr->dest_type = nir_type_int;       break;
 +   case GLSL_TYPE_UINT:    instr->dest_type = nir_type_uint;  break;
 +   case GLSL_TYPE_BOOL:    instr->dest_type = nir_type_bool;      break;
 +   default:
 +      unreachable("Invalid base type for sampler result");
 +   }
 +
 +   instr->op = texop;
 +   memcpy(instr->src, srcs, instr->num_srcs * sizeof(*instr->src));
 +   instr->coord_components = coord_components;
 +   instr->is_array = glsl_sampler_type_is_array(sampler_type);
 +   instr->is_shadow = glsl_sampler_type_is_shadow(sampler_type);
 +
 +   instr->sampler =
 +      nir_deref_as_var(nir_copy_deref(instr, &sampled.sampler->deref));
 +   if (sampled.image) {
 +      instr->texture =
 +         nir_deref_as_var(nir_copy_deref(instr, &sampled.image->deref));
 +   } else {
 +      instr->texture = NULL;
 +   }
 +
 +   nir_ssa_dest_init(&instr->instr, &instr->dest, 4, NULL);
 +   val->ssa = vtn_create_ssa_value(b, glsl_vector_type(GLSL_TYPE_FLOAT, 4));
 +   val->ssa->def = &instr->dest.ssa;
 +
 +   nir_builder_instr_insert(&b->nb, &instr->instr);
 +}
 +
 +static nir_ssa_def *
 +get_image_coord(struct vtn_builder *b, uint32_t value)
 +{
 +   struct vtn_ssa_value *coord = vtn_ssa_value(b, value);
 +
 +   /* The image_load_store intrinsics assume a 4-dim coordinate */
 +   unsigned dim = glsl_get_vector_elements(coord->type);
 +   unsigned swizzle[4];
 +   for (unsigned i = 0; i < 4; i++)
 +      swizzle[i] = MIN2(i, dim - 1);
 +
 +   return nir_swizzle(&b->nb, coord->def, swizzle, 4, false);
 +}
 +
 +static void
 +vtn_handle_image(struct vtn_builder *b, SpvOp opcode,
 +                 const uint32_t *w, unsigned count)
 +{
 +   /* Just get this one out of the way */
 +   if (opcode == SpvOpImageTexelPointer) {
 +      struct vtn_value *val =
 +         vtn_push_value(b, w[2], vtn_value_type_image_pointer);
 +      val->image = ralloc(b, struct vtn_image_pointer);
 +
 +      val->image->deref = vtn_value(b, w[3], vtn_value_type_deref)->deref;
 +      val->image->coord = get_image_coord(b, w[4]);
 +      val->image->sample = vtn_ssa_value(b, w[5])->def;
 +      return;
 +   }
 +
 +   struct vtn_image_pointer image;
 +
 +   switch (opcode) {
 +   case SpvOpAtomicExchange:
 +   case SpvOpAtomicCompareExchange:
 +   case SpvOpAtomicCompareExchangeWeak:
 +   case SpvOpAtomicIIncrement:
 +   case SpvOpAtomicIDecrement:
 +   case SpvOpAtomicIAdd:
 +   case SpvOpAtomicISub:
 +   case SpvOpAtomicSMin:
 +   case SpvOpAtomicUMin:
 +   case SpvOpAtomicSMax:
 +   case SpvOpAtomicUMax:
 +   case SpvOpAtomicAnd:
 +   case SpvOpAtomicOr:
 +   case SpvOpAtomicXor:
 +      image = *vtn_value(b, w[3], vtn_value_type_image_pointer)->image;
 +      break;
 +
 +   case SpvOpImageRead:
 +      image.deref = vtn_value(b, w[3], vtn_value_type_deref)->deref;
 +      image.coord = get_image_coord(b, w[4]);
 +
 +      if (count > 5 && (w[5] & SpvImageOperandsSampleMask)) {
 +         assert(w[5] == SpvImageOperandsSampleMask);
 +         image.sample = vtn_ssa_value(b, w[6])->def;
 +      } else {
 +         image.sample = nir_ssa_undef(&b->nb, 1);
 +      }
 +      break;
 +
 +   case SpvOpImageWrite:
 +      image.deref = vtn_value(b, w[1], vtn_value_type_deref)->deref;
 +      image.coord = get_image_coord(b, w[2]);
 +
 +      /* texel = w[3] */
 +
 +      if (count > 4 && (w[4] & SpvImageOperandsSampleMask)) {
 +         assert(w[4] == SpvImageOperandsSampleMask);
 +         image.sample = vtn_ssa_value(b, w[5])->def;
 +      } else {
 +         image.sample = nir_ssa_undef(&b->nb, 1);
 +      }
 +
 +   default:
 +      unreachable("Invalid image opcode");
 +   }
 +
 +   nir_intrinsic_op op;
 +   switch (opcode) {
 +#define OP(S, N) case SpvOp##S: op = nir_intrinsic_image_##N; break;
 +   OP(ImageRead,              load)
 +   OP(ImageWrite,             store)
 +   OP(AtomicExchange,         atomic_exchange)
 +   OP(AtomicCompareExchange,  atomic_comp_swap)
 +   OP(AtomicIIncrement,       atomic_add)
 +   OP(AtomicIDecrement,       atomic_add)
 +   OP(AtomicIAdd,             atomic_add)
 +   OP(AtomicISub,             atomic_add)
 +   OP(AtomicSMin,             atomic_min)
 +   OP(AtomicUMin,             atomic_min)
 +   OP(AtomicSMax,             atomic_max)
 +   OP(AtomicUMax,             atomic_max)
 +   OP(AtomicAnd,              atomic_and)
 +   OP(AtomicOr,               atomic_or)
 +   OP(AtomicXor,              atomic_xor)
 +#undef OP
 +   default:
 +      unreachable("Invalid image opcode");
 +   }
 +
 +   nir_intrinsic_instr *intrin = nir_intrinsic_instr_create(b->shader, op);
 +   intrin->variables[0] =
 +      nir_deref_as_var(nir_copy_deref(&intrin->instr, &image.deref->deref));
 +   intrin->src[0] = nir_src_for_ssa(image.coord);
 +   intrin->src[1] = nir_src_for_ssa(image.sample);
 +
 +   switch (opcode) {
 +   case SpvOpImageRead:
 +      break;
 +   case SpvOpImageWrite:
 +      intrin->src[2] = nir_src_for_ssa(vtn_ssa_value(b, w[3])->def);
 +      break;
 +   case SpvOpAtomicIIncrement:
 +      intrin->src[2] = nir_src_for_ssa(nir_imm_int(&b->nb, 1));
 +      break;
 +   case SpvOpAtomicIDecrement:
 +      intrin->src[2] = nir_src_for_ssa(nir_imm_int(&b->nb, -1));
 +      break;
 +
 +   case SpvOpAtomicExchange:
 +   case SpvOpAtomicIAdd:
 +   case SpvOpAtomicSMin:
 +   case SpvOpAtomicUMin:
 +   case SpvOpAtomicSMax:
 +   case SpvOpAtomicUMax:
 +   case SpvOpAtomicAnd:
 +   case SpvOpAtomicOr:
 +   case SpvOpAtomicXor:
 +      intrin->src[2] = nir_src_for_ssa(vtn_ssa_value(b, w[6])->def);
 +      break;
 +
 +   case SpvOpAtomicCompareExchange:
 +      intrin->src[2] = nir_src_for_ssa(vtn_ssa_value(b, w[7])->def);
 +      intrin->src[3] = nir_src_for_ssa(vtn_ssa_value(b, w[6])->def);
 +      break;
 +
 +   case SpvOpAtomicISub:
 +      intrin->src[2] = nir_src_for_ssa(nir_ineg(&b->nb, vtn_ssa_value(b, w[6])->def));
 +      break;
 +
 +   default:
 +      unreachable("Invalid image opcode");
 +   }
 +
 +   if (opcode != SpvOpImageWrite) {
 +      struct vtn_value *val = vtn_push_value(b, w[2], vtn_value_type_ssa);
 +      struct vtn_type *type = vtn_value(b, w[1], vtn_value_type_type)->type;
 +      nir_ssa_dest_init(&intrin->instr, &intrin->dest,
 +                        glsl_get_vector_elements(type->type), NULL);
 +      val->ssa = vtn_create_ssa_value(b, type->type);
 +      val->ssa->def = &intrin->dest.ssa;
 +   }
 +
 +   nir_builder_instr_insert(&b->nb, &intrin->instr);
 +}
 +
 +static nir_alu_instr *
 +create_vec(nir_shader *shader, unsigned num_components)
 +{
 +   nir_op op;
 +   switch (num_components) {
 +   case 1: op = nir_op_fmov; break;
 +   case 2: op = nir_op_vec2; break;
 +   case 3: op = nir_op_vec3; break;
 +   case 4: op = nir_op_vec4; break;
 +   default: unreachable("bad vector size");
 +   }
 +
 +   nir_alu_instr *vec = nir_alu_instr_create(shader, op);
 +   nir_ssa_dest_init(&vec->instr, &vec->dest.dest, num_components, NULL);
 +   vec->dest.write_mask = (1 << num_components) - 1;
 +
 +   return vec;
 +}
 +
 +static struct vtn_ssa_value *
 +vtn_transpose(struct vtn_builder *b, struct vtn_ssa_value *src)
 +{
 +   if (src->transposed)
 +      return src->transposed;
 +
 +   struct vtn_ssa_value *dest =
 +      vtn_create_ssa_value(b, glsl_transposed_type(src->type));
 +
 +   for (unsigned i = 0; i < glsl_get_matrix_columns(dest->type); i++) {
 +      nir_alu_instr *vec = create_vec(b->shader,
 +                                      glsl_get_matrix_columns(src->type));
 +      if (glsl_type_is_vector_or_scalar(src->type)) {
 +          vec->src[0].src = nir_src_for_ssa(src->def);
 +          vec->src[0].swizzle[0] = i;
 +      } else {
 +         for (unsigned j = 0; j < glsl_get_matrix_columns(src->type); j++) {
 +            vec->src[j].src = nir_src_for_ssa(src->elems[j]->def);
 +            vec->src[j].swizzle[0] = i;
 +         }
 +      }
 +      nir_builder_instr_insert(&b->nb, &vec->instr);
 +      dest->elems[i]->def = &vec->dest.dest.ssa;
 +   }
 +
 +   dest->transposed = src;
 +
 +   return dest;
 +}
 +
 +/*
 + * Normally, column vectors in SPIR-V correspond to a single NIR SSA
 + * definition. But for matrix multiplies, we want to do one routine for
 + * multiplying a matrix by a matrix and then pretend that vectors are matrices
 + * with one column. So we "wrap" these things, and unwrap the result before we
 + * send it off.
 + */
 +
 +static struct vtn_ssa_value *
 +vtn_wrap_matrix(struct vtn_builder *b, struct vtn_ssa_value *val)
 +{
 +   if (val == NULL)
 +      return NULL;
 +
 +   if (glsl_type_is_matrix(val->type))
 +      return val;
 +
 +   struct vtn_ssa_value *dest = rzalloc(b, struct vtn_ssa_value);
 +   dest->type = val->type;
 +   dest->elems = ralloc_array(b, struct vtn_ssa_value *, 1);
 +   dest->elems[0] = val;
 +
 +   return dest;
 +}
 +
 +static struct vtn_ssa_value *
 +vtn_unwrap_matrix(struct vtn_ssa_value *val)
 +{
 +   if (glsl_type_is_matrix(val->type))
 +         return val;
 +
 +   return val->elems[0];
 +}
 +
 +static struct vtn_ssa_value *
 +vtn_matrix_multiply(struct vtn_builder *b,
 +                    struct vtn_ssa_value *_src0, struct vtn_ssa_value *_src1)
 +{
 +
 +   struct vtn_ssa_value *src0 = vtn_wrap_matrix(b, _src0);
 +   struct vtn_ssa_value *src1 = vtn_wrap_matrix(b, _src1);
 +   struct vtn_ssa_value *src0_transpose = vtn_wrap_matrix(b, _src0->transposed);
 +   struct vtn_ssa_value *src1_transpose = vtn_wrap_matrix(b, _src1->transposed);
 +
 +   unsigned src0_rows = glsl_get_vector_elements(src0->type);
 +   unsigned src0_columns = glsl_get_matrix_columns(src0->type);
 +   unsigned src1_columns = glsl_get_matrix_columns(src1->type);
 +
 +   const struct glsl_type *dest_type;
 +   if (src1_columns > 1) {
 +      dest_type = glsl_matrix_type(glsl_get_base_type(src0->type),
 +                                   src0_rows, src1_columns);
 +   } else {
 +      dest_type = glsl_vector_type(glsl_get_base_type(src0->type), src0_rows);
 +   }
 +   struct vtn_ssa_value *dest = vtn_create_ssa_value(b, dest_type);
 +
 +   dest = vtn_wrap_matrix(b, dest);
 +
 +   bool transpose_result = false;
 +   if (src0_transpose && src1_transpose) {
 +      /* transpose(A) * transpose(B) = transpose(B * A) */
 +      src1 = src0_transpose;
 +      src0 = src1_transpose;
 +      src0_transpose = NULL;
 +      src1_transpose = NULL;
 +      transpose_result = true;
 +   }
 +
 +   if (src0_transpose && !src1_transpose &&
 +       glsl_get_base_type(src0->type) == GLSL_TYPE_FLOAT) {
 +      /* We already have the rows of src0 and the columns of src1 available,
 +       * so we can just take the dot product of each row with each column to
 +       * get the result.
 +       */
 +
 +      for (unsigned i = 0; i < src1_columns; i++) {
 +         nir_alu_instr *vec = create_vec(b->shader, src0_rows);
 +         for (unsigned j = 0; j < src0_rows; j++) {
 +            vec->src[j].src =
 +               nir_src_for_ssa(nir_fdot(&b->nb, src0_transpose->elems[j]->def,
 +                                        src1->elems[i]->def));
 +         }
 +
 +         nir_builder_instr_insert(&b->nb, &vec->instr);
 +         dest->elems[i]->def = &vec->dest.dest.ssa;
 +      }
 +   } else {
 +      /* We don't handle the case where src1 is transposed but not src0, since
 +       * the general case only uses individual components of src1 so the
 +       * optimizer should chew through the transpose we emitted for src1.
 +       */
 +
 +      for (unsigned i = 0; i < src1_columns; i++) {
 +         /* dest[i] = sum(src0[j] * src1[i][j] for all j) */
 +         dest->elems[i]->def =
 +            nir_fmul(&b->nb, src0->elems[0]->def,
 +                     vtn_vector_extract(b, src1->elems[i]->def, 0));
 +         for (unsigned j = 1; j < src0_columns; j++) {
 +            dest->elems[i]->def =
 +               nir_fadd(&b->nb, dest->elems[i]->def,
 +                        nir_fmul(&b->nb, src0->elems[j]->def,
 +                                 vtn_vector_extract(b,
 +                                                    src1->elems[i]->def, j)));
 +         }
 +      }
 +   }
 +
 +   dest = vtn_unwrap_matrix(dest);
 +
 +   if (transpose_result)
 +      dest = vtn_transpose(b, dest);
 +
 +   return dest;
 +}
 +
 +static struct vtn_ssa_value *
 +vtn_mat_times_scalar(struct vtn_builder *b,
 +                     struct vtn_ssa_value *mat,
 +                     nir_ssa_def *scalar)
 +{
 +   struct vtn_ssa_value *dest = vtn_create_ssa_value(b, mat->type);
 +   for (unsigned i = 0; i < glsl_get_matrix_columns(mat->type); i++) {
 +      if (glsl_get_base_type(mat->type) == GLSL_TYPE_FLOAT)
 +         dest->elems[i]->def = nir_fmul(&b->nb, mat->elems[i]->def, scalar);
 +      else
 +         dest->elems[i]->def = nir_imul(&b->nb, mat->elems[i]->def, scalar);
 +   }
 +
 +   return dest;
 +}
 +
 +static void
 +vtn_handle_matrix_alu(struct vtn_builder *b, SpvOp opcode,
 +                      const uint32_t *w, unsigned count)
 +{
 +   struct vtn_value *val = vtn_push_value(b, w[2], vtn_value_type_ssa);
 +
 +   switch (opcode) {
 +   case SpvOpTranspose: {
 +      struct vtn_ssa_value *src = vtn_ssa_value(b, w[3]);
 +      val->ssa = vtn_transpose(b, src);
 +      break;
 +   }
 +
 +   case SpvOpOuterProduct: {
 +      struct vtn_ssa_value *src0 = vtn_ssa_value(b, w[3]);
 +      struct vtn_ssa_value *src1 = vtn_ssa_value(b, w[4]);
 +
 +      val->ssa = vtn_matrix_multiply(b, src0, vtn_transpose(b, src1));
 +      break;
 +   }
 +
 +   case SpvOpMatrixTimesScalar: {
 +      struct vtn_ssa_value *mat = vtn_ssa_value(b, w[3]);
 +      struct vtn_ssa_value *scalar = vtn_ssa_value(b, w[4]);
 +
 +      if (mat->transposed) {
 +         val->ssa = vtn_transpose(b, vtn_mat_times_scalar(b, mat->transposed,
 +                                                          scalar->def));
 +      } else {
 +         val->ssa = vtn_mat_times_scalar(b, mat, scalar->def);
 +      }
 +      break;
 +   }
 +
 +   case SpvOpVectorTimesMatrix:
 +   case SpvOpMatrixTimesVector:
 +   case SpvOpMatrixTimesMatrix: {
 +      struct vtn_ssa_value *src0 = vtn_ssa_value(b, w[3]);
 +      struct vtn_ssa_value *src1 = vtn_ssa_value(b, w[4]);
 +
 +      if (opcode == SpvOpVectorTimesMatrix) {
 +         val->ssa = vtn_matrix_multiply(b, vtn_transpose(b, src1), src0);
 +      } else {
 +         val->ssa = vtn_matrix_multiply(b, src0, src1);
 +      }
 +      break;
 +   }
 +
 +   default: unreachable("unknown matrix opcode");
 +   }
 +}
 +
 +static void
 +vtn_handle_alu(struct vtn_builder *b, SpvOp opcode,
 +               const uint32_t *w, unsigned count)
 +{
 +   struct vtn_value *val = vtn_push_value(b, w[2], vtn_value_type_ssa);
 +   const struct glsl_type *type =
 +      vtn_value(b, w[1], vtn_value_type_type)->type->type;
 +   val->ssa = vtn_create_ssa_value(b, type);
 +
 +   /* Collect the various SSA sources */
 +   unsigned num_inputs = count - 3;
 +   nir_ssa_def *src[4];
 +   for (unsigned i = 0; i < num_inputs; i++)
 +      src[i] = vtn_ssa_value(b, w[i + 3])->def;
 +
 +   /* Indicates that the first two arguments should be swapped.  This is
 +    * used for implementing greater-than and less-than-or-equal.
 +    */
 +   bool swap = false;
 +
 +   nir_op op;
 +   switch (opcode) {
 +   /* Basic ALU operations */
 +   case SpvOpSNegate:               op = nir_op_ineg;    break;
 +   case SpvOpFNegate:               op = nir_op_fneg;    break;
 +   case SpvOpNot:                   op = nir_op_inot;    break;
 +
 +   case SpvOpAny:
-       switch (src[0]->num_components) {
-       case 1:  op = nir_op_imov;    break;
-       case 2:  op = nir_op_ball2;   break;
-       case 3:  op = nir_op_ball3;   break;
-       case 4:  op = nir_op_ball4;   break;
++      if (src[0]->num_components == 1) {
++         op = nir_op_imov;
++      } else {
++         switch (src[0]->num_components) {
++         case 2:  op = nir_op_bany_inequal2; break;
++         case 3:  op = nir_op_bany_inequal3; break;
++         case 4:  op = nir_op_bany_inequal4; break;
++         }
++         num_inputs = 2;
++         src[1] = nir_imm_int(&b->nb, NIR_FALSE);
 +      }
 +      break;
 +
 +   case SpvOpAll:
++      if (src[0]->num_components == 1) {
++         op = nir_op_imov;
++      } else {
++         switch (src[0]->num_components) {
++         case 2:  op = nir_op_ball_iequal2;  break;
++         case 3:  op = nir_op_ball_iequal3;  break;
++         case 4:  op = nir_op_ball_iequal4;  break;
++         }
++         num_inputs = 2;
++         src[1] = nir_imm_int(&b->nb, NIR_TRUE);
 +      }
 +      break;
 +
 +   case SpvOpIAdd:                  op = nir_op_iadd;    break;
 +   case SpvOpFAdd:                  op = nir_op_fadd;    break;
 +   case SpvOpISub:                  op = nir_op_isub;    break;
 +   case SpvOpFSub:                  op = nir_op_fsub;    break;
 +   case SpvOpIMul:                  op = nir_op_imul;    break;
 +   case SpvOpFMul:                  op = nir_op_fmul;    break;
 +   case SpvOpUDiv:                  op = nir_op_udiv;    break;
 +   case SpvOpSDiv:                  op = nir_op_idiv;    break;
 +   case SpvOpFDiv:                  op = nir_op_fdiv;    break;
 +   case SpvOpUMod:                  op = nir_op_umod;    break;
 +   case SpvOpSMod:                  op = nir_op_umod;    break; /* FIXME? */
 +   case SpvOpFMod:                  op = nir_op_fmod;    break;
 +
 +   case SpvOpDot:
 +      assert(src[0]->num_components == src[1]->num_components);
 +      switch (src[0]->num_components) {
 +      case 1:  op = nir_op_fmul;    break;
 +      case 2:  op = nir_op_fdot2;   break;
 +      case 3:  op = nir_op_fdot3;   break;
 +      case 4:  op = nir_op_fdot4;   break;
 +      }
 +      break;
 +
 +   case SpvOpShiftRightLogical:     op = nir_op_ushr;    break;
 +   case SpvOpShiftRightArithmetic:  op = nir_op_ishr;    break;
 +   case SpvOpShiftLeftLogical:      op = nir_op_ishl;    break;
 +   case SpvOpLogicalOr:             op = nir_op_ior;     break;
 +   case SpvOpLogicalEqual:          op = nir_op_ieq;     break;
 +   case SpvOpLogicalNotEqual:       op = nir_op_ine;     break;
 +   case SpvOpLogicalAnd:            op = nir_op_iand;    break;
 +   case SpvOpLogicalNot:            op = nir_op_inot;    break;
 +   case SpvOpBitwiseOr:             op = nir_op_ior;     break;
 +   case SpvOpBitwiseXor:            op = nir_op_ixor;    break;
 +   case SpvOpBitwiseAnd:            op = nir_op_iand;    break;
 +   case SpvOpSelect:                op = nir_op_bcsel;   break;
 +   case SpvOpIEqual:                op = nir_op_ieq;     break;
 +
 +   /* Comparisons: (TODO: How do we want to handled ordered/unordered?) */
 +   case SpvOpFOrdEqual:             op = nir_op_feq;     break;
 +   case SpvOpFUnordEqual:           op = nir_op_feq;     break;
 +   case SpvOpINotEqual:             op = nir_op_ine;     break;
 +   case SpvOpFOrdNotEqual:          op = nir_op_fne;     break;
 +   case SpvOpFUnordNotEqual:        op = nir_op_fne;     break;
 +   case SpvOpULessThan:             op = nir_op_ult;     break;
 +   case SpvOpSLessThan:             op = nir_op_ilt;     break;
 +   case SpvOpFOrdLessThan:          op = nir_op_flt;     break;
 +   case SpvOpFUnordLessThan:        op = nir_op_flt;     break;
 +   case SpvOpUGreaterThan:          op = nir_op_ult;  swap = true;   break;
 +   case SpvOpSGreaterThan:          op = nir_op_ilt;  swap = true;   break;
 +   case SpvOpFOrdGreaterThan:       op = nir_op_flt;  swap = true;   break;
 +   case SpvOpFUnordGreaterThan:     op = nir_op_flt;  swap = true;   break;
 +   case SpvOpULessThanEqual:        op = nir_op_uge;  swap = true;   break;
 +   case SpvOpSLessThanEqual:        op = nir_op_ige;  swap = true;   break;
 +   case SpvOpFOrdLessThanEqual:     op = nir_op_fge;  swap = true;   break;
 +   case SpvOpFUnordLessThanEqual:   op = nir_op_fge;  swap = true;   break;
 +   case SpvOpUGreaterThanEqual:     op = nir_op_uge;     break;
 +   case SpvOpSGreaterThanEqual:     op = nir_op_ige;     break;
 +   case SpvOpFOrdGreaterThanEqual:  op = nir_op_fge;     break;
 +   case SpvOpFUnordGreaterThanEqual:op = nir_op_fge;     break;
 +
 +   /* Conversions: */
 +   case SpvOpConvertFToU:           op = nir_op_f2u;     break;
 +   case SpvOpConvertFToS:           op = nir_op_f2i;     break;
 +   case SpvOpConvertSToF:           op = nir_op_i2f;     break;
 +   case SpvOpConvertUToF:           op = nir_op_u2f;     break;
 +   case SpvOpBitcast:               op = nir_op_imov;    break;
 +   case SpvOpUConvert:
 +   case SpvOpSConvert:
 +      op = nir_op_imov; /* TODO: NIR is 32-bit only; these are no-ops. */
 +      break;
 +   case SpvOpFConvert:
 +      op = nir_op_fmov;
 +      break;
 +
 +   /* Derivatives: */
 +   case SpvOpDPdx:         op = nir_op_fddx;          break;
 +   case SpvOpDPdy:         op = nir_op_fddy;          break;
 +   case SpvOpDPdxFine:     op = nir_op_fddx_fine;     break;
 +   case SpvOpDPdyFine:     op = nir_op_fddy_fine;     break;
 +   case SpvOpDPdxCoarse:   op = nir_op_fddx_coarse;   break;
 +   case SpvOpDPdyCoarse:   op = nir_op_fddy_coarse;   break;
 +   case SpvOpFwidth:
 +      val->ssa->def = nir_fadd(&b->nb,
 +                               nir_fabs(&b->nb, nir_fddx(&b->nb, src[0])),
 +                               nir_fabs(&b->nb, nir_fddx(&b->nb, src[1])));
 +      return;
 +   case SpvOpFwidthFine:
 +      val->ssa->def = nir_fadd(&b->nb,
 +                               nir_fabs(&b->nb, nir_fddx_fine(&b->nb, src[0])),
 +                               nir_fabs(&b->nb, nir_fddx_fine(&b->nb, src[1])));
 +      return;
 +   case SpvOpFwidthCoarse:
 +      val->ssa->def = nir_fadd(&b->nb,
 +                               nir_fabs(&b->nb, nir_fddx_coarse(&b->nb, src[0])),
 +                               nir_fabs(&b->nb, nir_fddx_coarse(&b->nb, src[1])));
 +      return;
 +
 +   case SpvOpVectorTimesScalar:
 +      /* The builder will take care of splatting for us. */
 +      val->ssa->def = nir_fmul(&b->nb, src[0], src[1]);
 +      return;
 +
 +   case SpvOpSRem:
 +   case SpvOpFRem:
 +      unreachable("No NIR equivalent");
 +
 +   case SpvOpIsNan:
 +   case SpvOpIsInf:
 +   case SpvOpIsFinite:
 +   case SpvOpIsNormal:
 +   case SpvOpSignBitSet:
 +   case SpvOpLessOrGreater:
 +   case SpvOpOrdered:
 +   case SpvOpUnordered:
 +   default:
 +      unreachable("Unhandled opcode");
 +   }
 +
 +   if (swap) {
 +      nir_ssa_def *tmp = src[0];
 +      src[0] = src[1];
 +      src[1] = tmp;
 +   }
 +
 +   nir_alu_instr *instr = nir_alu_instr_create(b->shader, op);
 +   nir_ssa_dest_init(&instr->instr, &instr->dest.dest,
 +                     glsl_get_vector_elements(type), val->name);
 +   instr->dest.write_mask = (1 << glsl_get_vector_elements(type)) - 1;
 +   val->ssa->def = &instr->dest.dest.ssa;
 +
 +   for (unsigned i = 0; i < nir_op_infos[op].num_inputs; i++)
 +      instr->src[i].src = nir_src_for_ssa(src[i]);
 +
 +   nir_builder_instr_insert(&b->nb, &instr->instr);
 +}
 +
 +static nir_ssa_def *
 +vtn_vector_extract(struct vtn_builder *b, nir_ssa_def *src, unsigned index)
 +{
 +   unsigned swiz[4] = { index };
 +   return nir_swizzle(&b->nb, src, swiz, 1, true);
 +}
 +
 +
 +static nir_ssa_def *
 +vtn_vector_insert(struct vtn_builder *b, nir_ssa_def *src, nir_ssa_def *insert,
 +                  unsigned index)
 +{
 +   nir_alu_instr *vec = create_vec(b->shader, src->num_components);
 +
 +   for (unsigned i = 0; i < src->num_components; i++) {
 +      if (i == index) {
 +         vec->src[i].src = nir_src_for_ssa(insert);
 +      } else {
 +         vec->src[i].src = nir_src_for_ssa(src);
 +         vec->src[i].swizzle[0] = i;
 +      }
 +   }
 +
 +   nir_builder_instr_insert(&b->nb, &vec->instr);
 +
 +   return &vec->dest.dest.ssa;
 +}
 +
 +static nir_ssa_def *
 +vtn_vector_extract_dynamic(struct vtn_builder *b, nir_ssa_def *src,
 +                           nir_ssa_def *index)
 +{
 +   nir_ssa_def *dest = vtn_vector_extract(b, src, 0);
 +   for (unsigned i = 1; i < src->num_components; i++)
 +      dest = nir_bcsel(&b->nb, nir_ieq(&b->nb, index, nir_imm_int(&b->nb, i)),
 +                       vtn_vector_extract(b, src, i), dest);
 +
 +   return dest;
 +}
 +
 +static nir_ssa_def *
 +vtn_vector_insert_dynamic(struct vtn_builder *b, nir_ssa_def *src,
 +                          nir_ssa_def *insert, nir_ssa_def *index)
 +{
 +   nir_ssa_def *dest = vtn_vector_insert(b, src, insert, 0);
 +   for (unsigned i = 1; i < src->num_components; i++)
 +      dest = nir_bcsel(&b->nb, nir_ieq(&b->nb, index, nir_imm_int(&b->nb, i)),
 +                       vtn_vector_insert(b, src, insert, i), dest);
 +
 +   return dest;
 +}
 +
 +static nir_ssa_def *
 +vtn_vector_shuffle(struct vtn_builder *b, unsigned num_components,
 +                   nir_ssa_def *src0, nir_ssa_def *src1,
 +                   const uint32_t *indices)
 +{
 +   nir_alu_instr *vec = create_vec(b->shader, num_components);
 +
 +   nir_ssa_undef_instr *undef = nir_ssa_undef_instr_create(b->shader, 1);
 +   nir_builder_instr_insert(&b->nb, &undef->instr);
 +
 +   for (unsigned i = 0; i < num_components; i++) {
 +      uint32_t index = indices[i];
 +      if (index == 0xffffffff) {
 +         vec->src[i].src = nir_src_for_ssa(&undef->def);
 +      } else if (index < src0->num_components) {
 +         vec->src[i].src = nir_src_for_ssa(src0);
 +         vec->src[i].swizzle[0] = index;
 +      } else {
 +         vec->src[i].src = nir_src_for_ssa(src1);
 +         vec->src[i].swizzle[0] = index - src0->num_components;
 +      }
 +   }
 +
 +   nir_builder_instr_insert(&b->nb, &vec->instr);
 +
 +   return &vec->dest.dest.ssa;
 +}
 +
 +/*
 + * Concatentates a number of vectors/scalars together to produce a vector
 + */
 +static nir_ssa_def *
 +vtn_vector_construct(struct vtn_builder *b, unsigned num_components,
 +                     unsigned num_srcs, nir_ssa_def **srcs)
 +{
 +   nir_alu_instr *vec = create_vec(b->shader, num_components);
 +
 +   unsigned dest_idx = 0;
 +   for (unsigned i = 0; i < num_srcs; i++) {
 +      nir_ssa_def *src = srcs[i];
 +      for (unsigned j = 0; j < src->num_components; j++) {
 +         vec->src[dest_idx].src = nir_src_for_ssa(src);
 +         vec->src[dest_idx].swizzle[0] = j;
 +         dest_idx++;
 +      }
 +   }
 +
 +   nir_builder_instr_insert(&b->nb, &vec->instr);
 +
 +   return &vec->dest.dest.ssa;
 +}
 +
 +static struct vtn_ssa_value *
 +vtn_composite_copy(void *mem_ctx, struct vtn_ssa_value *src)
 +{
 +   struct vtn_ssa_value *dest = rzalloc(mem_ctx, struct vtn_ssa_value);
 +   dest->type = src->type;
 +
 +   if (glsl_type_is_vector_or_scalar(src->type)) {
 +      dest->def = src->def;
 +   } else {
 +      unsigned elems = glsl_get_length(src->type);
 +
 +      dest->elems = ralloc_array(mem_ctx, struct vtn_ssa_value *, elems);
 +      for (unsigned i = 0; i < elems; i++)
 +         dest->elems[i] = vtn_composite_copy(mem_ctx, src->elems[i]);
 +   }
 +
 +   return dest;
 +}
 +
 +static struct vtn_ssa_value *
 +vtn_composite_insert(struct vtn_builder *b, struct vtn_ssa_value *src,
 +                     struct vtn_ssa_value *insert, const uint32_t *indices,
 +                     unsigned num_indices)
 +{
 +   struct vtn_ssa_value *dest = vtn_composite_copy(b, src);
 +
 +   struct vtn_ssa_value *cur = dest;
 +   unsigned i;
 +   for (i = 0; i < num_indices - 1; i++) {
 +      cur = cur->elems[indices[i]];
 +   }
 +
 +   if (glsl_type_is_vector_or_scalar(cur->type)) {
 +      /* According to the SPIR-V spec, OpCompositeInsert may work down to
 +       * the component granularity. In that case, the last index will be
 +       * the index to insert the scalar into the vector.
 +       */
 +
 +      cur->def = vtn_vector_insert(b, cur->def, insert->def, indices[i]);
 +   } else {
 +      cur->elems[indices[i]] = insert;
 +   }
 +
 +   return dest;
 +}
 +
 +static struct vtn_ssa_value *
 +vtn_composite_extract(struct vtn_builder *b, struct vtn_ssa_value *src,
 +                      const uint32_t *indices, unsigned num_indices)
 +{
 +   struct vtn_ssa_value *cur = src;
 +   for (unsigned i = 0; i < num_indices; i++) {
 +      if (glsl_type_is_vector_or_scalar(cur->type)) {
 +         assert(i == num_indices - 1);
 +         /* According to the SPIR-V spec, OpCompositeExtract may work down to
 +          * the component granularity. The last index will be the index of the
 +          * vector to extract.
 +          */
 +
 +         struct vtn_ssa_value *ret = rzalloc(b, struct vtn_ssa_value);
 +         ret->type = glsl_scalar_type(glsl_get_base_type(cur->type));
 +         ret->def = vtn_vector_extract(b, cur->def, indices[i]);
 +         return ret;
 +      } else {
 +         cur = cur->elems[indices[i]];
 +      }
 +   }
 +
 +   return cur;
 +}
 +
 +static void
 +vtn_handle_composite(struct vtn_builder *b, SpvOp opcode,
 +                     const uint32_t *w, unsigned count)
 +{
 +   struct vtn_value *val = vtn_push_value(b, w[2], vtn_value_type_ssa);
 +   const struct glsl_type *type =
 +      vtn_value(b, w[1], vtn_value_type_type)->type->type;
 +   val->ssa = vtn_create_ssa_value(b, type);
 +
 +   switch (opcode) {
 +   case SpvOpVectorExtractDynamic:
 +      val->ssa->def = vtn_vector_extract_dynamic(b, vtn_ssa_value(b, w[3])->def,
 +                                                 vtn_ssa_value(b, w[4])->def);
 +      break;
 +
 +   case SpvOpVectorInsertDynamic:
 +      val->ssa->def = vtn_vector_insert_dynamic(b, vtn_ssa_value(b, w[3])->def,
 +                                                vtn_ssa_value(b, w[4])->def,
 +                                                vtn_ssa_value(b, w[5])->def);
 +      break;
 +
 +   case SpvOpVectorShuffle:
 +      val->ssa->def = vtn_vector_shuffle(b, glsl_get_vector_elements(type),
 +                                         vtn_ssa_value(b, w[3])->def,
 +                                         vtn_ssa_value(b, w[4])->def,
 +                                         w + 5);
 +      break;
 +
 +   case SpvOpCompositeConstruct: {
 +      unsigned elems = count - 3;
 +      if (glsl_type_is_vector_or_scalar(type)) {
 +         nir_ssa_def *srcs[4];
 +         for (unsigned i = 0; i < elems; i++)
 +            srcs[i] = vtn_ssa_value(b, w[3 + i])->def;
 +         val->ssa->def =
 +            vtn_vector_construct(b, glsl_get_vector_elements(type),
 +                                 elems, srcs);
 +      } else {
 +         val->ssa->elems = ralloc_array(b, struct vtn_ssa_value *, elems);
 +         for (unsigned i = 0; i < elems; i++)
 +            val->ssa->elems[i] = vtn_ssa_value(b, w[3 + i]);
 +      }
 +      break;
 +   }
 +   case SpvOpCompositeExtract:
 +      val->ssa = vtn_composite_extract(b, vtn_ssa_value(b, w[3]),
 +                                       w + 4, count - 4);
 +      break;
 +
 +   case SpvOpCompositeInsert:
 +      val->ssa = vtn_composite_insert(b, vtn_ssa_value(b, w[4]),
 +                                      vtn_ssa_value(b, w[3]),
 +                                      w + 5, count - 5);
 +      break;
 +
 +   case SpvOpCopyObject:
 +      val->ssa = vtn_composite_copy(b, vtn_ssa_value(b, w[3]));
 +      break;
 +
 +   default:
 +      unreachable("unknown composite operation");
 +   }
 +}
 +
 +static void
 +vtn_handle_barrier(struct vtn_builder *b, SpvOp opcode,
 +                   const uint32_t *w, unsigned count)
 +{
 +   nir_intrinsic_op intrinsic_op;
 +   switch (opcode) {
 +   case SpvOpEmitVertex:
 +   case SpvOpEmitStreamVertex:
 +      intrinsic_op = nir_intrinsic_emit_vertex;
 +      break;
 +   case SpvOpEndPrimitive:
 +   case SpvOpEndStreamPrimitive:
 +      intrinsic_op = nir_intrinsic_end_primitive;
 +      break;
 +   case SpvOpMemoryBarrier:
 +      intrinsic_op = nir_intrinsic_memory_barrier;
 +      break;
 +   case SpvOpControlBarrier:
 +   default:
 +      unreachable("unknown barrier instruction");
 +   }
 +
 +   nir_intrinsic_instr *intrin =
 +      nir_intrinsic_instr_create(b->shader, intrinsic_op);
 +
 +   if (opcode == SpvOpEmitStreamVertex || opcode == SpvOpEndStreamPrimitive)
 +      intrin->const_index[0] = w[1];
 +
 +   nir_builder_instr_insert(&b->nb, &intrin->instr);
 +}
 +
 +static void
 +vtn_phi_node_init(struct vtn_builder *b, struct vtn_ssa_value *val)
 +{
 +   if (glsl_type_is_vector_or_scalar(val->type)) {
 +      nir_phi_instr *phi = nir_phi_instr_create(b->shader);
 +      nir_ssa_dest_init(&phi->instr, &phi->dest,
 +                        glsl_get_vector_elements(val->type), NULL);
 +      exec_list_make_empty(&phi->srcs);
 +      nir_builder_instr_insert(&b->nb, &phi->instr);
 +      val->def = &phi->dest.ssa;
 +   } else {
 +      unsigned elems = glsl_get_length(val->type);
 +      for (unsigned i = 0; i < elems; i++)
 +         vtn_phi_node_init(b, val->elems[i]);
 +   }
 +}
 +
 +static struct vtn_ssa_value *
 +vtn_phi_node_create(struct vtn_builder *b, const struct glsl_type *type)
 +{
 +   struct vtn_ssa_value *val = vtn_create_ssa_value(b, type);
 +   vtn_phi_node_init(b, val);
 +   return val;
 +}
 +
 +static void
 +vtn_handle_phi_first_pass(struct vtn_builder *b, const uint32_t *w)
 +{
 +   struct vtn_value *val = vtn_push_value(b, w[2], vtn_value_type_ssa);
 +   const struct glsl_type *type =
 +      vtn_value(b, w[1], vtn_value_type_type)->type->type;
 +   val->ssa = vtn_phi_node_create(b, type);
 +}
 +
 +static void
 +vtn_phi_node_add_src(struct vtn_ssa_value *phi, const nir_block *pred,
 +                     struct vtn_ssa_value *val)
 +{
 +   assert(phi->type == val->type);
 +   if (glsl_type_is_vector_or_scalar(phi->type)) {
 +      nir_phi_instr *phi_instr = nir_instr_as_phi(phi->def->parent_instr);
 +      nir_phi_src *src = ralloc(phi_instr, nir_phi_src);
 +      src->pred = (nir_block *) pred;
 +      src->src = nir_src_for_ssa(val->def);
 +      exec_list_push_tail(&phi_instr->srcs, &src->node);
 +   } else {
 +      unsigned elems = glsl_get_length(phi->type);
 +      for (unsigned i = 0; i < elems; i++)
 +         vtn_phi_node_add_src(phi->elems[i], pred, val->elems[i]);
 +   }
 +}
 +
 +static struct vtn_ssa_value *
 +vtn_get_phi_node_src(struct vtn_builder *b, nir_block *block,
 +                     const struct glsl_type *type, const uint32_t *w,
 +                     unsigned count)
 +{
 +   struct hash_entry *entry = _mesa_hash_table_search(b->block_table, block);
 +   if (entry) {
 +      struct vtn_block *spv_block = entry->data;
 +      for (unsigned off = 4; off < count; off += 2) {
 +         if (spv_block == vtn_value(b, w[off], vtn_value_type_block)->block) {
 +            return vtn_ssa_value(b, w[off - 1]);
 +         }
 +      }
 +   }
 +
 +   b->nb.cursor = nir_before_block(block);
 +   struct vtn_ssa_value *phi = vtn_phi_node_create(b, type);
 +
 +   struct set_entry *entry2;
 +   set_foreach(block->predecessors, entry2) {
 +      nir_block *pred = (nir_block *) entry2->key;
 +      struct vtn_ssa_value *val = vtn_get_phi_node_src(b, pred, type, w,
 +                                                       count);
 +      vtn_phi_node_add_src(phi, pred, val);
 +   }
 +
 +   return phi;
 +}
 +
 +static bool
 +vtn_handle_phi_second_pass(struct vtn_builder *b, SpvOp opcode,
 +                           const uint32_t *w, unsigned count)
 +{
 +   if (opcode == SpvOpLabel) {
 +      b->block = vtn_value(b, w[1], vtn_value_type_block)->block;
 +      return true;
 +   }
 +
 +   if (opcode != SpvOpPhi)
 +      return true;
 +
 +   struct vtn_ssa_value *phi = vtn_value(b, w[2], vtn_value_type_ssa)->ssa;
 +
 +   struct set_entry *entry;
 +   set_foreach(b->block->block->predecessors, entry) {
 +      nir_block *pred = (nir_block *) entry->key;
 +
 +      struct vtn_ssa_value *val = vtn_get_phi_node_src(b, pred, phi->type, w,
 +                                                       count);
 +      vtn_phi_node_add_src(phi, pred, val);
 +   }
 +
 +   return true;
 +}
 +
 +static unsigned
 +gl_primitive_from_spv_execution_mode(SpvExecutionMode mode)
 +{
 +   switch (mode) {
 +   case SpvExecutionModeInputPoints:
 +   case SpvExecutionModeOutputPoints:
 +      return 0; /* GL_POINTS */
 +   case SpvExecutionModeInputLines:
 +      return 1; /* GL_LINES */
 +   case SpvExecutionModeInputLinesAdjacency:
 +      return 0x000A; /* GL_LINE_STRIP_ADJACENCY_ARB */
 +   case SpvExecutionModeTriangles:
 +      return 4; /* GL_TRIANGLES */
 +   case SpvExecutionModeInputTrianglesAdjacency:
 +      return 0x000C; /* GL_TRIANGLES_ADJACENCY_ARB */
 +   case SpvExecutionModeQuads:
 +      return 7; /* GL_QUADS */
 +   case SpvExecutionModeIsolines:
 +      return 0x8E7A; /* GL_ISOLINES */
 +   case SpvExecutionModeOutputLineStrip:
 +      return 3; /* GL_LINE_STRIP */
 +   case SpvExecutionModeOutputTriangleStrip:
 +      return 5; /* GL_TRIANGLE_STRIP */
 +   default:
 +      assert(!"Invalid primitive type");
 +      return 4;
 +   }
 +}
 +
 +static unsigned
 +vertices_in_from_spv_execution_mode(SpvExecutionMode mode)
 +{
 +   switch (mode) {
 +   case SpvExecutionModeInputPoints:
 +      return 1;
 +   case SpvExecutionModeInputLines:
 +      return 2;
 +   case SpvExecutionModeInputLinesAdjacency:
 +      return 4;
 +   case SpvExecutionModeTriangles:
 +      return 3;
 +   case SpvExecutionModeInputTrianglesAdjacency:
 +      return 6;
 +   default:
 +      assert(!"Invalid GS input mode");
 +      return 0;
 +   }
 +}
 +
 +static bool
 +vtn_handle_preamble_instruction(struct vtn_builder *b, SpvOp opcode,
 +                                const uint32_t *w, unsigned count)
 +{
 +   switch (opcode) {
 +   case SpvOpSource:
 +   case SpvOpSourceExtension:
 +   case SpvOpExtension:
 +      /* Unhandled, but these are for debug so that's ok. */
 +      break;
 +
 +   case SpvOpCapability:
 +      switch ((SpvCapability)w[1]) {
 +      case SpvCapabilityMatrix:
 +      case SpvCapabilityShader:
 +         /* All shaders support these */
 +         break;
 +      case SpvCapabilityGeometry:
 +         assert(b->shader->stage == MESA_SHADER_GEOMETRY);
 +         break;
 +      default:
 +         assert(!"Unsupported capability");
 +      }
 +      break;
 +
 +   case SpvOpExtInstImport:
 +      vtn_handle_extension(b, opcode, w, count);
 +      break;
 +
 +   case SpvOpMemoryModel:
 +      assert(w[1] == SpvAddressingModelLogical);
 +      assert(w[2] == SpvMemoryModelGLSL450);
 +      break;
 +
 +   case SpvOpEntryPoint:
 +      assert(b->entry_point == NULL);
 +      b->entry_point = &b->values[w[2]];
 +      b->execution_model = w[1];
 +      break;
 +
 +   case SpvOpExecutionMode:
 +      assert(b->entry_point == &b->values[w[1]]);
 +
 +      SpvExecutionMode mode = w[2];
 +      switch(mode) {
 +      case SpvExecutionModeOriginUpperLeft:
 +      case SpvExecutionModeOriginLowerLeft:
 +         b->origin_upper_left = (mode == SpvExecutionModeOriginUpperLeft);
 +         break;
 +
 +      case SpvExecutionModeEarlyFragmentTests:
 +         assert(b->shader->stage == MESA_SHADER_FRAGMENT);
 +         b->shader->info.fs.early_fragment_tests = true;
 +         break;
 +
 +      case SpvExecutionModeInvocations:
 +         assert(b->shader->stage == MESA_SHADER_GEOMETRY);
 +         b->shader->info.gs.invocations = MAX2(1, w[3]);
 +         break;
 +
 +      case SpvExecutionModeDepthReplacing:
 +         assert(b->shader->stage == MESA_SHADER_FRAGMENT);
 +         b->shader->info.fs.depth_layout = FRAG_DEPTH_LAYOUT_ANY;
 +         break;
 +      case SpvExecutionModeDepthGreater:
 +         assert(b->shader->stage == MESA_SHADER_FRAGMENT);
 +         b->shader->info.fs.depth_layout = FRAG_DEPTH_LAYOUT_GREATER;
 +         break;
 +      case SpvExecutionModeDepthLess:
 +         assert(b->shader->stage == MESA_SHADER_FRAGMENT);
 +         b->shader->info.fs.depth_layout = FRAG_DEPTH_LAYOUT_LESS;
 +         break;
 +      case SpvExecutionModeDepthUnchanged:
 +         assert(b->shader->stage == MESA_SHADER_FRAGMENT);
 +         b->shader->info.fs.depth_layout = FRAG_DEPTH_LAYOUT_UNCHANGED;
 +         break;
 +
 +      case SpvExecutionModeLocalSize:
 +         assert(b->shader->stage == MESA_SHADER_COMPUTE);
 +         b->shader->info.cs.local_size[0] = w[3];
 +         b->shader->info.cs.local_size[1] = w[4];
 +         b->shader->info.cs.local_size[2] = w[5];
 +         break;
 +      case SpvExecutionModeLocalSizeHint:
 +         break; /* Nothing do do with this */
 +
 +      case SpvExecutionModeOutputVertices:
 +         assert(b->shader->stage == MESA_SHADER_GEOMETRY);
 +         b->shader->info.gs.vertices_out = w[3];
 +         break;
 +
 +      case SpvExecutionModeInputPoints:
 +      case SpvExecutionModeInputLines:
 +      case SpvExecutionModeInputLinesAdjacency:
 +      case SpvExecutionModeTriangles:
 +      case SpvExecutionModeInputTrianglesAdjacency:
 +      case SpvExecutionModeQuads:
 +      case SpvExecutionModeIsolines:
 +         if (b->shader->stage == MESA_SHADER_GEOMETRY) {
 +            b->shader->info.gs.vertices_in =
 +               vertices_in_from_spv_execution_mode(mode);
 +         } else {
 +            assert(!"Tesselation shaders not yet supported");
 +         }
 +         break;
 +
 +      case SpvExecutionModeOutputPoints:
 +      case SpvExecutionModeOutputLineStrip:
 +      case SpvExecutionModeOutputTriangleStrip:
 +         assert(b->shader->stage == MESA_SHADER_GEOMETRY);
 +         b->shader->info.gs.output_primitive =
 +            gl_primitive_from_spv_execution_mode(mode);
 +         break;
 +
 +      case SpvExecutionModeSpacingEqual:
 +      case SpvExecutionModeSpacingFractionalEven:
 +      case SpvExecutionModeSpacingFractionalOdd:
 +      case SpvExecutionModeVertexOrderCw:
 +      case SpvExecutionModeVertexOrderCcw:
 +      case SpvExecutionModePointMode:
 +         assert(!"TODO: Add tessellation metadata");
 +         break;
 +
 +      case SpvExecutionModePixelCenterInteger:
 +      case SpvExecutionModeXfb:
 +         assert(!"Unhandled execution mode");
 +         break;
 +
 +      case SpvExecutionModeVecTypeHint:
 +      case SpvExecutionModeContractionOff:
 +         break; /* OpenCL */
 +      }
 +      break;
 +
 +   case SpvOpString:
 +      vtn_push_value(b, w[1], vtn_value_type_string)->str =
 +         vtn_string_literal(b, &w[2], count - 2);
 +      break;
 +
 +   case SpvOpName:
 +      b->values[w[1]].name = vtn_string_literal(b, &w[2], count - 2);
 +      break;
 +
 +   case SpvOpMemberName:
 +      /* TODO */
 +      break;
 +
 +   case SpvOpLine:
 +      break; /* Ignored for now */
 +
 +   case SpvOpDecorationGroup:
 +   case SpvOpDecorate:
 +   case SpvOpMemberDecorate:
 +   case SpvOpGroupDecorate:
 +   case SpvOpGroupMemberDecorate:
 +      vtn_handle_decoration(b, opcode, w, count);
 +      break;
 +
 +   case SpvOpTypeVoid:
 +   case SpvOpTypeBool:
 +   case SpvOpTypeInt:
 +   case SpvOpTypeFloat:
 +   case SpvOpTypeVector:
 +   case SpvOpTypeMatrix:
 +   case SpvOpTypeImage:
 +   case SpvOpTypeSampler:
 +   case SpvOpTypeSampledImage:
 +   case SpvOpTypeArray:
 +   case SpvOpTypeRuntimeArray:
 +   case SpvOpTypeStruct:
 +   case SpvOpTypeOpaque:
 +   case SpvOpTypePointer:
 +   case SpvOpTypeFunction:
 +   case SpvOpTypeEvent:
 +   case SpvOpTypeDeviceEvent:
 +   case SpvOpTypeReserveId:
 +   case SpvOpTypeQueue:
 +   case SpvOpTypePipe:
 +      vtn_handle_type(b, opcode, w, count);
 +      break;
 +
 +   case SpvOpConstantTrue:
 +   case SpvOpConstantFalse:
 +   case SpvOpConstant:
 +   case SpvOpConstantComposite:
 +   case SpvOpConstantSampler:
 +   case SpvOpSpecConstantTrue:
 +   case SpvOpSpecConstantFalse:
 +   case SpvOpSpecConstant:
 +   case SpvOpSpecConstantComposite:
 +      vtn_handle_constant(b, opcode, w, count);
 +      break;
 +
 +   case SpvOpVariable:
 +      vtn_handle_variables(b, opcode, w, count);
 +      break;
 +
 +   default:
 +      return false; /* End of preamble */
 +   }
 +
 +   return true;
 +}
 +
 +static bool
 +vtn_handle_first_cfg_pass_instruction(struct vtn_builder *b, SpvOp opcode,
 +                                      const uint32_t *w, unsigned count)
 +{
 +   switch (opcode) {
 +   case SpvOpFunction: {
 +      assert(b->func == NULL);
 +      b->func = rzalloc(b, struct vtn_function);
 +
 +      const struct glsl_type *result_type =
 +         vtn_value(b, w[1], vtn_value_type_type)->type->type;
 +      struct vtn_value *val = vtn_push_value(b, w[2], vtn_value_type_function);
 +      val->func = b->func;
 +
 +      const struct glsl_type *func_type =
 +         vtn_value(b, w[4], vtn_value_type_type)->type->type;
 +
 +      assert(glsl_get_function_return_type(func_type) == result_type);
 +
 +      nir_function *func =
 +         nir_function_create(b->shader, ralloc_strdup(b->shader, val->name));
 +
 +      nir_function_overload *overload = nir_function_overload_create(func);
 +      overload->num_params = glsl_get_length(func_type);
 +      overload->params = ralloc_array(overload, nir_parameter,
 +                                      overload->num_params);
 +      for (unsigned i = 0; i < overload->num_params; i++) {
 +         const struct glsl_function_param *param =
 +            glsl_get_function_param(func_type, i);
 +         overload->params[i].type = param->type;
 +         if (param->in) {
 +            if (param->out) {
 +               overload->params[i].param_type = nir_parameter_inout;
 +            } else {
 +               overload->params[i].param_type = nir_parameter_in;
 +            }
 +         } else {
 +            if (param->out) {
 +               overload->params[i].param_type = nir_parameter_out;
 +            } else {
 +               assert(!"Parameter is neither in nor out");
 +            }
 +         }
 +      }
 +
 +      overload->return_type = glsl_get_function_return_type(func_type);
 +
 +      b->func->impl = nir_function_impl_create(overload);
 +      if (!glsl_type_is_void(overload->return_type)) {
 +         b->func->impl->return_var =
 +            nir_local_variable_create(b->func->impl,
 +                                      overload->return_type, "retval");
 +      }
 +
 +      b->func_param_idx = 0;
 +      break;
 +   }
 +
 +   case SpvOpFunctionEnd:
 +      b->func->end = w;
 +      b->func = NULL;
 +      break;
 +
 +   case SpvOpFunctionParameter: {
 +      struct vtn_value *val = vtn_push_value(b, w[2], vtn_value_type_deref);
 +
 +      assert(b->func_param_idx < b->func->impl->num_params);
 +      unsigned idx = b->func_param_idx++;
 +
 +      nir_variable *param =
 +         nir_local_variable_create(b->func->impl,
 +                                   b->func->impl->overload->params[idx].type,
 +                                   val->name);
 +
 +      b->func->impl->params[idx] = param;
 +      val->deref = nir_deref_var_create(b, param);
 +      val->deref_type = vtn_value(b, w[1], vtn_value_type_type)->type;
 +      break;
 +   }
 +
 +   case SpvOpLabel: {
 +      assert(b->block == NULL);
 +      b->block = rzalloc(b, struct vtn_block);
 +      b->block->label = w;
 +      vtn_push_value(b, w[1], vtn_value_type_block)->block = b->block;
 +
 +      if (b->func->start_block == NULL) {
 +         /* This is the first block encountered for this function.  In this
 +          * case, we set the start block and add it to the list of
 +          * implemented functions that we'll walk later.
 +          */
 +         b->func->start_block = b->block;
 +         exec_list_push_tail(&b->functions, &b->func->node);
 +      }
 +      break;
 +   }
 +
 +   case SpvOpBranch:
 +   case SpvOpBranchConditional:
 +   case SpvOpSwitch:
 +   case SpvOpKill:
 +   case SpvOpReturn:
 +   case SpvOpReturnValue:
 +   case SpvOpUnreachable:
 +      assert(b->block);
 +      b->block->branch = w;
 +      b->block = NULL;
 +      break;
 +
 +   case SpvOpSelectionMerge:
 +   case SpvOpLoopMerge:
 +      assert(b->block && b->block->merge_op == SpvOpNop);
 +      b->block->merge_op = opcode;
 +      b->block->merge_block_id = w[1];
 +      break;
 +
 +   default:
 +      /* Continue on as per normal */
 +      return true;
 +   }
 +
 +   return true;
 +}
 +
 +static bool
 +vtn_handle_body_instruction(struct vtn_builder *b, SpvOp opcode,
 +                            const uint32_t *w, unsigned count)
 +{
 +   switch (opcode) {
 +   case SpvOpLabel: {
 +      struct vtn_block *block = vtn_value(b, w[1], vtn_value_type_block)->block;
 +      assert(block->block == NULL);
 +
 +      block->block = nir_cursor_current_block(b->nb.cursor);
 +      break;
 +   }
 +
 +   case SpvOpLoopMerge:
 +   case SpvOpSelectionMerge:
 +      /* This is handled by cfg pre-pass and walk_blocks */
 +      break;
 +
 +   case SpvOpUndef: {
 +      struct vtn_value *val = vtn_push_value(b, w[2], vtn_value_type_undef);
 +      val->type = vtn_value(b, w[1], vtn_value_type_type)->type;
 +      break;
 +   }
 +
 +   case SpvOpExtInst:
 +      vtn_handle_extension(b, opcode, w, count);
 +      break;
 +
 +   case SpvOpVariable:
 +   case SpvOpLoad:
 +   case SpvOpStore:
 +   case SpvOpCopyMemory:
 +   case SpvOpCopyMemorySized:
 +   case SpvOpAccessChain:
 +   case SpvOpInBoundsAccessChain:
 +   case SpvOpArrayLength:
 +      vtn_handle_variables(b, opcode, w, count);
 +      break;
 +
 +   case SpvOpFunctionCall:
 +      vtn_handle_function_call(b, opcode, w, count);
 +      break;
 +
 +   case SpvOpSampledImage:
 +   case SpvOpImageSampleImplicitLod:
 +   case SpvOpImageSampleExplicitLod:
 +   case SpvOpImageSampleDrefImplicitLod:
 +   case SpvOpImageSampleDrefExplicitLod:
 +   case SpvOpImageSampleProjImplicitLod:
 +   case SpvOpImageSampleProjExplicitLod:
 +   case SpvOpImageSampleProjDrefImplicitLod:
 +   case SpvOpImageSampleProjDrefExplicitLod:
 +   case SpvOpImageFetch:
 +   case SpvOpImageGather:
 +   case SpvOpImageDrefGather:
 +   case SpvOpImageQuerySizeLod:
 +   case SpvOpImageQuerySize:
 +   case SpvOpImageQueryLod:
 +   case SpvOpImageQueryLevels:
 +   case SpvOpImageQuerySamples:
 +      vtn_handle_texture(b, opcode, w, count);
 +      break;
 +
 +   case SpvOpImageRead:
 +   case SpvOpImageWrite:
 +   case SpvOpImageTexelPointer:
 +      vtn_handle_image(b, opcode, w, count);
 +      break;
 +
 +   case SpvOpAtomicExchange:
 +   case SpvOpAtomicCompareExchange:
 +   case SpvOpAtomicCompareExchangeWeak:
 +   case SpvOpAtomicIIncrement:
 +   case SpvOpAtomicIDecrement:
 +   case SpvOpAtomicIAdd:
 +   case SpvOpAtomicISub:
 +   case SpvOpAtomicSMin:
 +   case SpvOpAtomicUMin:
 +   case SpvOpAtomicSMax:
 +   case SpvOpAtomicUMax:
 +   case SpvOpAtomicAnd:
 +   case SpvOpAtomicOr:
 +   case SpvOpAtomicXor: {
 +      struct vtn_value *pointer = vtn_untyped_value(b, w[3]);
 +      if (pointer->value_type == vtn_value_type_image_pointer) {
 +         vtn_handle_image(b, opcode, w, count);
 +      } else {
 +         assert(!"Atomic buffers not yet implemented");
 +      }
 +   }
 +
 +   case SpvOpSNegate:
 +   case SpvOpFNegate:
 +   case SpvOpNot:
 +   case SpvOpAny:
 +   case SpvOpAll:
 +   case SpvOpConvertFToU:
 +   case SpvOpConvertFToS:
 +   case SpvOpConvertSToF:
 +   case SpvOpConvertUToF:
 +   case SpvOpUConvert:
 +   case SpvOpSConvert:
 +   case SpvOpFConvert:
 +   case SpvOpConvertPtrToU:
 +   case SpvOpConvertUToPtr:
 +   case SpvOpPtrCastToGeneric:
 +   case SpvOpGenericCastToPtr:
 +   case SpvOpBitcast:
 +   case SpvOpIsNan:
 +   case SpvOpIsInf:
 +   case SpvOpIsFinite:
 +   case SpvOpIsNormal:
 +   case SpvOpSignBitSet:
 +   case SpvOpLessOrGreater:
 +   case SpvOpOrdered:
 +   case SpvOpUnordered:
 +   case SpvOpIAdd:
 +   case SpvOpFAdd:
 +   case SpvOpISub:
 +   case SpvOpFSub:
 +   case SpvOpIMul:
 +   case SpvOpFMul:
 +   case SpvOpUDiv:
 +   case SpvOpSDiv:
 +   case SpvOpFDiv:
 +   case SpvOpUMod:
 +   case SpvOpSRem:
 +   case SpvOpSMod:
 +   case SpvOpFRem:
 +   case SpvOpFMod:
 +   case SpvOpVectorTimesScalar:
 +   case SpvOpDot:
 +   case SpvOpShiftRightLogical:
 +   case SpvOpShiftRightArithmetic:
 +   case SpvOpShiftLeftLogical:
 +   case SpvOpLogicalEqual:
 +   case SpvOpLogicalNotEqual:
 +   case SpvOpLogicalOr:
 +   case SpvOpLogicalAnd:
 +   case SpvOpLogicalNot:
 +   case SpvOpBitwiseOr:
 +   case SpvOpBitwiseXor:
 +   case SpvOpBitwiseAnd:
 +   case SpvOpSelect:
 +   case SpvOpIEqual:
 +   case SpvOpFOrdEqual:
 +   case SpvOpFUnordEqual:
 +   case SpvOpINotEqual:
 +   case SpvOpFOrdNotEqual:
 +   case SpvOpFUnordNotEqual:
 +   case SpvOpULessThan:
 +   case SpvOpSLessThan:
 +   case SpvOpFOrdLessThan:
 +   case SpvOpFUnordLessThan:
 +   case SpvOpUGreaterThan:
 +   case SpvOpSGreaterThan:
 +   case SpvOpFOrdGreaterThan:
 +   case SpvOpFUnordGreaterThan:
 +   case SpvOpULessThanEqual:
 +   case SpvOpSLessThanEqual:
 +   case SpvOpFOrdLessThanEqual:
 +   case SpvOpFUnordLessThanEqual:
 +   case SpvOpUGreaterThanEqual:
 +   case SpvOpSGreaterThanEqual:
 +   case SpvOpFOrdGreaterThanEqual:
 +   case SpvOpFUnordGreaterThanEqual:
 +   case SpvOpDPdx:
 +   case SpvOpDPdy:
 +   case SpvOpFwidth:
 +   case SpvOpDPdxFine:
 +   case SpvOpDPdyFine:
 +   case SpvOpFwidthFine:
 +   case SpvOpDPdxCoarse:
 +   case SpvOpDPdyCoarse:
 +   case SpvOpFwidthCoarse:
 +      vtn_handle_alu(b, opcode, w, count);
 +      break;
 +
 +   case SpvOpTranspose:
 +   case SpvOpOuterProduct:
 +   case SpvOpMatrixTimesScalar:
 +   case SpvOpVectorTimesMatrix:
 +   case SpvOpMatrixTimesVector:
 +   case SpvOpMatrixTimesMatrix:
 +      vtn_handle_matrix_alu(b, opcode, w, count);
 +      break;
 +
 +   case SpvOpVectorExtractDynamic:
 +   case SpvOpVectorInsertDynamic:
 +   case SpvOpVectorShuffle:
 +   case SpvOpCompositeConstruct:
 +   case SpvOpCompositeExtract:
 +   case SpvOpCompositeInsert:
 +   case SpvOpCopyObject:
 +      vtn_handle_composite(b, opcode, w, count);
 +      break;
 +
 +   case SpvOpPhi:
 +      vtn_handle_phi_first_pass(b, w);
 +      break;
 +
 +   case SpvOpEmitVertex:
 +   case SpvOpEndPrimitive:
 +   case SpvOpEmitStreamVertex:
 +   case SpvOpEndStreamPrimitive:
 +   case SpvOpControlBarrier:
 +   case SpvOpMemoryBarrier:
 +      vtn_handle_barrier(b, opcode, w, count);
 +      break;
 +
 +   default:
 +      unreachable("Unhandled opcode");
 +   }
 +
 +   return true;
 +}
 +
 +static void
 +vtn_walk_blocks(struct vtn_builder *b, struct vtn_block *start,
 +                struct vtn_block *break_block, struct vtn_block *cont_block,
 +                struct vtn_block *end_block)
 +{
 +   struct vtn_block *block = start;
 +   while (block != end_block) {
 +      if (block->merge_op == SpvOpLoopMerge) {
 +         /* This is the jump into a loop. */
 +         struct vtn_block *new_cont_block = block;
 +         struct vtn_block *new_break_block =
 +            vtn_value(b, block->merge_block_id, vtn_value_type_block)->block;
 +
 +         nir_loop *loop = nir_loop_create(b->shader);
 +         nir_cf_node_insert(b->nb.cursor, &loop->cf_node);
 +
 +         /* Reset the merge_op to prerevent infinite recursion */
 +         block->merge_op = SpvOpNop;
 +
 +         b->nb.cursor = nir_after_cf_list(&loop->body);
 +         vtn_walk_blocks(b, block, new_break_block, new_cont_block, NULL);
 +
 +         b->nb.cursor = nir_after_cf_node(&loop->cf_node);
 +         block = new_break_block;
 +         continue;
 +      }
 +
 +      const uint32_t *w = block->branch;
 +      SpvOp branch_op = w[0] & SpvOpCodeMask;
 +
 +      b->block = block;
 +      vtn_foreach_instruction(b, block->label, block->branch,
 +                              vtn_handle_body_instruction);
 +
 +      nir_block *cur_block = nir_cursor_current_block(b->nb.cursor);
 +      assert(cur_block == block->block);
 +      _mesa_hash_table_insert(b->block_table, cur_block, block);
 +
 +      switch (branch_op) {
 +      case SpvOpBranch: {
 +         struct vtn_block *branch_block =
 +            vtn_value(b, w[1], vtn_value_type_block)->block;
 +
 +         if (branch_block == break_block) {
 +            nir_jump_instr *jump = nir_jump_instr_create(b->shader,
 +                                                         nir_jump_break);
 +            nir_builder_instr_insert(&b->nb, &jump->instr);
 +
 +            return;
 +         } else if (branch_block == cont_block) {
 +            nir_jump_instr *jump = nir_jump_instr_create(b->shader,
 +                                                         nir_jump_continue);
 +            nir_builder_instr_insert(&b->nb, &jump->instr);
 +
 +            return;
 +         } else if (branch_block == end_block) {
 +            /* We're branching to the merge block of an if, since for loops
 +             * and functions end_block == NULL, so we're done here.
 +             */
 +            return;
 +         } else {
 +            /* We're branching to another block, and according to the rules,
 +             * we can only branch to another block with one predecessor (so
 +             * we're the only one jumping to it) so we can just process it
 +             * next.
 +             */
 +            block = branch_block;
 +            continue;
 +         }
 +      }
 +
 +      case SpvOpBranchConditional: {
 +         /* Gather up the branch blocks */
 +         struct vtn_block *then_block =
 +            vtn_value(b, w[2], vtn_value_type_block)->block;
 +         struct vtn_block *else_block =
 +            vtn_value(b, w[3], vtn_value_type_block)->block;
 +
 +         nir_if *if_stmt = nir_if_create(b->shader);
 +         if_stmt->condition = nir_src_for_ssa(vtn_ssa_value(b, w[1])->def);
 +         nir_cf_node_insert(b->nb.cursor, &if_stmt->cf_node);
 +
 +         if (then_block == break_block) {
 +            nir_jump_instr *jump = nir_jump_instr_create(b->shader,
 +                                                         nir_jump_break);
 +            nir_instr_insert_after_cf_list(&if_stmt->then_list,
 +                                           &jump->instr);
 +            block = else_block;
 +         } else if (else_block == break_block) {
 +            nir_jump_instr *jump = nir_jump_instr_create(b->shader,
 +                                                         nir_jump_break);
 +            nir_instr_insert_after_cf_list(&if_stmt->else_list,
 +                                           &jump->instr);
 +            block = then_block;
 +         } else if (then_block == cont_block) {
 +            nir_jump_instr *jump = nir_jump_instr_create(b->shader,
 +                                                         nir_jump_continue);
 +            nir_instr_insert_after_cf_list(&if_stmt->then_list,
 +                                           &jump->instr);
 +            block = else_block;
 +         } else if (else_block == cont_block) {
 +            nir_jump_instr *jump = nir_jump_instr_create(b->shader,
 +                                                         nir_jump_continue);
 +            nir_instr_insert_after_cf_list(&if_stmt->else_list,
 +                                           &jump->instr);
 +            block = then_block;
 +         } else {
 +            /* According to the rules we're branching to two blocks that don't
 +             * have any other predecessors, so we can handle this as a
 +             * conventional if.
 +             */
 +            assert(block->merge_op == SpvOpSelectionMerge);
 +            struct vtn_block *merge_block =
 +               vtn_value(b, block->merge_block_id, vtn_value_type_block)->block;
 +
 +            b->nb.cursor = nir_after_cf_list(&if_stmt->then_list);
 +            vtn_walk_blocks(b, then_block, break_block, cont_block, merge_block);
 +
 +            b->nb.cursor = nir_after_cf_list(&if_stmt->else_list);
 +            vtn_walk_blocks(b, else_block, break_block, cont_block, merge_block);
 +
 +            b->nb.cursor = nir_after_cf_node(&if_stmt->cf_node);
 +            block = merge_block;
 +            continue;
 +         }
 +
 +         /* If we got here then we inserted a predicated break or continue
 +          * above and we need to handle the other case.  We already set
 +          * `block` above to indicate what block to visit after the
 +          * predicated break.
 +          */
 +
 +         /* It's possible that the other branch is also a break/continue.
 +          * If it is, we handle that here.
 +          */
 +         if (block == break_block) {
 +            nir_jump_instr *jump = nir_jump_instr_create(b->shader,
 +                                                         nir_jump_break);
 +            nir_builder_instr_insert(&b->nb, &jump->instr);
 +
 +            return;
 +         } else if (block == cont_block) {
 +            nir_jump_instr *jump = nir_jump_instr_create(b->shader,
 +                                                         nir_jump_continue);
 +            nir_builder_instr_insert(&b->nb, &jump->instr);
 +
 +            return;
 +         }
 +
 +         /* If we got here then there was a predicated break/continue but
 +          * the other half of the if has stuff in it.  `block` was already
 +          * set above so there is nothing left for us to do.
 +          */
 +         continue;
 +      }
 +
 +      case SpvOpReturn: {
 +         nir_jump_instr *jump = nir_jump_instr_create(b->shader,
 +                                                      nir_jump_return);
 +         nir_builder_instr_insert(&b->nb, &jump->instr);
 +         return;
 +      }
 +
 +      case SpvOpReturnValue: {
 +         struct vtn_ssa_value *src = vtn_ssa_value(b, w[1]);
 +         vtn_variable_store(b, src,
 +                            nir_deref_var_create(b, b->impl->return_var),
 +                            NULL);
 +
 +         nir_jump_instr *jump = nir_jump_instr_create(b->shader,
 +                                                      nir_jump_return);
 +         nir_builder_instr_insert(&b->nb, &jump->instr);
 +         return;
 +      }
 +
 +      case SpvOpKill: {
 +         nir_intrinsic_instr *discard =
 +            nir_intrinsic_instr_create(b->shader, nir_intrinsic_discard);
 +         nir_builder_instr_insert(&b->nb, &discard->instr);
 +         return;
 +      }
 +
 +      case SpvOpSwitch:
 +      case SpvOpUnreachable:
 +      default:
 +         unreachable("Unhandled opcode");
 +      }
 +   }
 +}
 +
 +nir_shader *
 +spirv_to_nir(const uint32_t *words, size_t word_count,
 +             gl_shader_stage stage,
 +             const nir_shader_compiler_options *options)
 +{
 +   const uint32_t *word_end = words + word_count;
 +
 +   /* Handle the SPIR-V header (first 4 dwords)  */
 +   assert(word_count > 5);
 +
 +   assert(words[0] == SpvMagicNumber);
 +   assert(words[1] >= 0x10000);
 +   /* words[2] == generator magic */
 +   unsigned value_id_bound = words[3];
 +   assert(words[4] == 0);
 +
 +   words+= 5;
 +
 +   nir_shader *shader = nir_shader_create(NULL, stage, options);
 +
 +   /* Initialize the stn_builder object */
 +   struct vtn_builder *b = rzalloc(NULL, struct vtn_builder);
 +   b->shader = shader;
 +   b->value_id_bound = value_id_bound;
 +   b->values = rzalloc_array(b, struct vtn_value, value_id_bound);
 +   exec_list_make_empty(&b->functions);
 +
 +   /* XXX: We shouldn't need these defaults */
 +   if (b->shader->stage == MESA_SHADER_GEOMETRY) {
 +      b->shader->info.gs.vertices_in = 3;
 +      b->shader->info.gs.output_primitive = 4; /* GL_TRIANGLES */
 +   }
 +
 +   /* Handle all the preamble instructions */
 +   words = vtn_foreach_instruction(b, words, word_end,
 +                                   vtn_handle_preamble_instruction);
 +
 +   /* Do a very quick CFG analysis pass */
 +   vtn_foreach_instruction(b, words, word_end,
 +                           vtn_handle_first_cfg_pass_instruction);
 +
 +   foreach_list_typed(struct vtn_function, func, node, &b->functions) {
 +      b->impl = func->impl;
 +      b->const_table = _mesa_hash_table_create(b, _mesa_hash_pointer,
 +                                               _mesa_key_pointer_equal);
 +      b->block_table = _mesa_hash_table_create(b, _mesa_hash_pointer,
 +                                               _mesa_key_pointer_equal);
 +      nir_builder_init(&b->nb, b->impl);
 +      b->nb.cursor = nir_after_cf_list(&b->impl->body);
 +      vtn_walk_blocks(b, func->start_block, NULL, NULL, NULL);
 +      vtn_foreach_instruction(b, func->start_block->label, func->end,
 +                              vtn_handle_phi_second_pass);
 +   }
 +
 +   /* Because we can still have output reads in NIR, we need to lower
 +    * outputs to temporaries before we are truely finished.
 +    */
 +   nir_lower_outputs_to_temporaries(shader);
 +
 +   ralloc_free(b);
 +
 +   return shader;
 +}
Simple merge
Simple merge
Simple merge
Simple merge
Simple merge
Simple merge
index 63976cdfe4238aa4b535f9294db19c96636e0e8c,0000000000000000000000000000000000000000..cf6678d852f892b10d19eda97dd0398c7444a601
mode 100644,000000..100644
--- /dev/null
@@@ -1,1298 -1,0 +1,1298 @@@
-    nir_store_var(&b, color_out, &tex->dest.ssa);
 +/*
 + * 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.
 + */
 +
 +#include <assert.h>
 +#include <stdbool.h>
 +#include <string.h>
 +#include <unistd.h>
 +#include <fcntl.h>
 +
 +#include "anv_meta.h"
 +#include "anv_meta_clear.h"
 +#include "anv_private.h"
 +#include "anv_nir_builder.h"
 +
 +struct anv_render_pass anv_meta_dummy_renderpass = {0};
 +
 +static nir_shader *
 +build_nir_vertex_shader(bool attr_flat)
 +{
 +   nir_builder b;
 +
 +   const struct glsl_type *vertex_type = glsl_vec4_type();
 +
 +   nir_builder_init_simple_shader(&b, MESA_SHADER_VERTEX);
 +
 +   nir_variable *pos_in = nir_variable_create(b.shader, nir_var_shader_in,
 +                                              vertex_type, "a_pos");
 +   pos_in->data.location = VERT_ATTRIB_GENERIC0;
 +   nir_variable *pos_out = nir_variable_create(b.shader, nir_var_shader_out,
 +                                               vertex_type, "gl_Position");
 +   pos_in->data.location = VARYING_SLOT_POS;
 +   nir_copy_var(&b, pos_out, pos_in);
 +
 +   /* Add one more pass-through attribute.  For clear shaders, this is used
 +    * to store the color and for blit shaders it's the texture coordinate.
 +    */
 +   const struct glsl_type *attr_type = glsl_vec4_type();
 +   nir_variable *attr_in = nir_variable_create(b.shader, nir_var_shader_in,
 +                                               attr_type, "a_attr");
 +   attr_in->data.location = VERT_ATTRIB_GENERIC1;
 +   nir_variable *attr_out = nir_variable_create(b.shader, nir_var_shader_out,
 +                                                attr_type, "v_attr");
 +   attr_out->data.location = VARYING_SLOT_VAR0;
 +   attr_out->data.interpolation = attr_flat ? INTERP_QUALIFIER_FLAT :
 +                                              INTERP_QUALIFIER_SMOOTH;
 +   nir_copy_var(&b, attr_out, attr_in);
 +
 +   return b.shader;
 +}
 +
 +static nir_shader *
 +build_nir_copy_fragment_shader(enum glsl_sampler_dim tex_dim)
 +{
 +   nir_builder b;
 +
 +   nir_builder_init_simple_shader(&b, MESA_SHADER_FRAGMENT);
 +
 +   const struct glsl_type *color_type = glsl_vec4_type();
 +
 +   nir_variable *tex_pos_in = nir_variable_create(b.shader, nir_var_shader_in,
 +                                                  glsl_vec4_type(), "v_attr");
 +   tex_pos_in->data.location = VARYING_SLOT_VAR0;
 +
 +   const struct glsl_type *sampler_type =
 +      glsl_sampler_type(tex_dim, false, false, glsl_get_base_type(color_type));
 +   nir_variable *sampler = nir_variable_create(b.shader, nir_var_uniform,
 +                                               sampler_type, "s_tex");
 +   sampler->data.descriptor_set = 0;
 +   sampler->data.binding = 0;
 +
 +   nir_tex_instr *tex = nir_tex_instr_create(b.shader, 1);
 +   tex->sampler_dim = tex_dim;
 +   tex->op = nir_texop_tex;
 +   tex->src[0].src_type = nir_tex_src_coord;
 +   tex->src[0].src = nir_src_for_ssa(nir_load_var(&b, tex_pos_in));
 +   tex->dest_type = nir_type_float; /* TODO */
 +
 +   if (tex_dim == GLSL_SAMPLER_DIM_2D)
 +      tex->is_array = true;
 +   tex->coord_components = 3;
 +
 +   tex->sampler = nir_deref_var_create(tex, sampler);
 +
 +   nir_ssa_dest_init(&tex->instr, &tex->dest, 4, "tex");
 +   nir_builder_instr_insert(&b, &tex->instr);
 +
 +   nir_variable *color_out = nir_variable_create(b.shader, nir_var_shader_out,
 +                                                 color_type, "f_color");
 +   color_out->data.location = FRAG_RESULT_DATA0;
++   nir_store_var(&b, color_out, &tex->dest.ssa, 4);
 +
 +   return b.shader;
 +}
 +
 +void
 +anv_meta_save(struct anv_meta_saved_state *state,
 +              const struct anv_cmd_buffer *cmd_buffer,
 +              uint32_t dynamic_mask)
 +{
 +   state->old_pipeline = cmd_buffer->state.pipeline;
 +   state->old_descriptor_set0 = cmd_buffer->state.descriptors[0];
 +   memcpy(state->old_vertex_bindings, cmd_buffer->state.vertex_bindings,
 +          sizeof(state->old_vertex_bindings));
 +
 +   state->dynamic_mask = dynamic_mask;
 +   anv_dynamic_state_copy(&state->dynamic, &cmd_buffer->state.dynamic,
 +                          dynamic_mask);
 +}
 +
 +void
 +anv_meta_restore(const struct anv_meta_saved_state *state,
 +                 struct anv_cmd_buffer *cmd_buffer)
 +{
 +   cmd_buffer->state.pipeline = state->old_pipeline;
 +   cmd_buffer->state.descriptors[0] = state->old_descriptor_set0;
 +   memcpy(cmd_buffer->state.vertex_bindings, state->old_vertex_bindings,
 +          sizeof(state->old_vertex_bindings));
 +
 +   cmd_buffer->state.vb_dirty |= (1 << ANV_META_VERTEX_BINDING_COUNT) - 1;
 +   cmd_buffer->state.dirty |= ANV_CMD_DIRTY_PIPELINE;
 +   cmd_buffer->state.descriptors_dirty |= VK_SHADER_STAGE_VERTEX_BIT;
 +
 +   anv_dynamic_state_copy(&cmd_buffer->state.dynamic, &state->dynamic,
 +                          state->dynamic_mask);
 +   cmd_buffer->state.dirty |= state->dynamic_mask;
 +}
 +
 +VkImageViewType
 +anv_meta_get_view_type(const struct anv_image *image)
 +{
 +   switch (image->type) {
 +   case VK_IMAGE_TYPE_1D: return VK_IMAGE_VIEW_TYPE_1D;
 +   case VK_IMAGE_TYPE_2D: return VK_IMAGE_VIEW_TYPE_2D;
 +   case VK_IMAGE_TYPE_3D: return VK_IMAGE_VIEW_TYPE_3D;
 +   default:
 +      unreachable("bad VkImageViewType");
 +   }
 +}
 +
 +static uint32_t
 +meta_blit_get_dest_view_base_array_slice(const struct anv_image *dest_image,
 +                                         const VkImageSubresourceLayers *dest_subresource,
 +                                         const VkOffset3D *dest_offset)
 +{
 +   switch (dest_image->type) {
 +   case VK_IMAGE_TYPE_1D:
 +   case VK_IMAGE_TYPE_2D:
 +      return dest_subresource->baseArrayLayer;
 +   case VK_IMAGE_TYPE_3D:
 +      /* HACK: Vulkan does not allow attaching a 3D image to a framebuffer,
 +       * but meta does it anyway. When doing so, we translate the
 +       * destination's z offset into an array offset.
 +       */
 +      return dest_offset->z;
 +   default:
 +      assert(!"bad VkImageType");
 +      return 0;
 +   }
 +}
 +
 +static void
 +anv_device_init_meta_blit_state(struct anv_device *device)
 +{
 +   anv_CreateRenderPass(anv_device_to_handle(device),
 +      &(VkRenderPassCreateInfo) {
 +         .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
 +         .attachmentCount = 1,
 +         .pAttachments = &(VkAttachmentDescription) {
 +            .format = VK_FORMAT_UNDEFINED, /* Our shaders don't care */
 +            .loadOp = VK_ATTACHMENT_LOAD_OP_LOAD,
 +            .storeOp = VK_ATTACHMENT_STORE_OP_STORE,
 +            .initialLayout = VK_IMAGE_LAYOUT_GENERAL,
 +            .finalLayout = VK_IMAGE_LAYOUT_GENERAL,
 +         },
 +         .subpassCount = 1,
 +         .pSubpasses = &(VkSubpassDescription) {
 +            .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
 +            .inputAttachmentCount = 0,
 +            .colorAttachmentCount = 1,
 +            .pColorAttachments = &(VkAttachmentReference) {
 +               .attachment = 0,
 +               .layout = VK_IMAGE_LAYOUT_GENERAL,
 +            },
 +            .pResolveAttachments = NULL,
 +            .pDepthStencilAttachment = &(VkAttachmentReference) {
 +               .attachment = VK_ATTACHMENT_UNUSED,
 +               .layout = VK_IMAGE_LAYOUT_GENERAL,
 +            },
 +            .preserveAttachmentCount = 1,
 +            .pPreserveAttachments = &(VkAttachmentReference) {
 +               .attachment = 0,
 +               .layout = VK_IMAGE_LAYOUT_GENERAL,
 +            },
 +         },
 +         .dependencyCount = 0,
 +      }, NULL, &device->meta_state.blit.render_pass);
 +
 +   /* We don't use a vertex shader for clearing, but instead build and pass
 +    * the VUEs directly to the rasterization backend.  However, we do need
 +    * to provide GLSL source for the vertex shader so that the compiler
 +    * does not dead-code our inputs.
 +    */
 +   struct anv_shader_module vs = {
 +      .nir = build_nir_vertex_shader(false),
 +   };
 +
 +   struct anv_shader_module fs_2d = {
 +      .nir = build_nir_copy_fragment_shader(GLSL_SAMPLER_DIM_2D),
 +   };
 +
 +   struct anv_shader_module fs_3d = {
 +      .nir = build_nir_copy_fragment_shader(GLSL_SAMPLER_DIM_3D),
 +   };
 +
 +   VkPipelineVertexInputStateCreateInfo vi_create_info = {
 +      .sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO,
 +      .vertexBindingDescriptionCount = 2,
 +      .pVertexBindingDescriptions = (VkVertexInputBindingDescription[]) {
 +         {
 +            .binding = 0,
 +            .stride = 0,
 +            .inputRate = VK_VERTEX_INPUT_RATE_VERTEX
 +         },
 +         {
 +            .binding = 1,
 +            .stride = 5 * sizeof(float),
 +            .inputRate = VK_VERTEX_INPUT_RATE_VERTEX
 +         },
 +      },
 +      .vertexAttributeDescriptionCount = 3,
 +      .pVertexAttributeDescriptions = (VkVertexInputAttributeDescription[]) {
 +         {
 +            /* VUE Header */
 +            .location = 0,
 +            .binding = 0,
 +            .format = VK_FORMAT_R32G32B32A32_UINT,
 +            .offset = 0
 +         },
 +         {
 +            /* Position */
 +            .location = 1,
 +            .binding = 1,
 +            .format = VK_FORMAT_R32G32_SFLOAT,
 +            .offset = 0
 +         },
 +         {
 +            /* Texture Coordinate */
 +            .location = 2,
 +            .binding = 1,
 +            .format = VK_FORMAT_R32G32B32_SFLOAT,
 +            .offset = 8
 +         }
 +      }
 +   };
 +
 +   VkDescriptorSetLayoutCreateInfo ds_layout_info = {
 +      .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
 +      .bindingCount = 1,
 +      .pBinding = (VkDescriptorSetLayoutBinding[]) {
 +         {
 +            .binding = 0,
 +            .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
 +            .descriptorCount = 1,
 +            .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
 +            .pImmutableSamplers = NULL
 +         },
 +      }
 +   };
 +   anv_CreateDescriptorSetLayout(anv_device_to_handle(device), &ds_layout_info,
 +                                 NULL, &device->meta_state.blit.ds_layout);
 +
 +   anv_CreatePipelineLayout(anv_device_to_handle(device),
 +      &(VkPipelineLayoutCreateInfo) {
 +         .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
 +         .setLayoutCount = 1,
 +         .pSetLayouts = &device->meta_state.blit.ds_layout,
 +      },
 +      NULL, &device->meta_state.blit.pipeline_layout);
 +
 +   VkPipelineShaderStageCreateInfo pipeline_shader_stages[] = {
 +      {
 +         .sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
 +         .stage = VK_SHADER_STAGE_VERTEX_BIT,
 +         .module = anv_shader_module_to_handle(&vs),
 +         .pName = "main",
 +         .pSpecializationInfo = NULL
 +      }, {
 +         .sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
 +         .stage = VK_SHADER_STAGE_FRAGMENT_BIT,
 +         .module = VK_NULL_HANDLE, /* TEMPLATE VALUE! FILL ME IN! */
 +         .pName = "main",
 +         .pSpecializationInfo = NULL
 +      },
 +   };
 +
 +   const VkGraphicsPipelineCreateInfo vk_pipeline_info = {
 +      .sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO,
 +      .stageCount = ARRAY_SIZE(pipeline_shader_stages),
 +      .pStages = pipeline_shader_stages,
 +      .pVertexInputState = &vi_create_info,
 +      .pInputAssemblyState = &(VkPipelineInputAssemblyStateCreateInfo) {
 +         .sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO,
 +         .topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP,
 +         .primitiveRestartEnable = false,
 +      },
 +      .pViewportState = &(VkPipelineViewportStateCreateInfo) {
 +         .sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO,
 +         .viewportCount = 1,
 +         .scissorCount = 1,
 +      },
 +      .pRasterizationState = &(VkPipelineRasterizationStateCreateInfo) {
 +         .sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO,
 +         .rasterizerDiscardEnable = false,
 +         .polygonMode = VK_POLYGON_MODE_FILL,
 +         .cullMode = VK_CULL_MODE_NONE,
 +         .frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE
 +      },
 +      .pMultisampleState = &(VkPipelineMultisampleStateCreateInfo) {
 +         .sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO,
 +         .rasterizationSamples = 1,
 +         .sampleShadingEnable = false,
 +         .pSampleMask = (VkSampleMask[]) { UINT32_MAX },
 +      },
 +      .pColorBlendState = &(VkPipelineColorBlendStateCreateInfo) {
 +         .sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO,
 +         .attachmentCount = 1,
 +         .pAttachments = (VkPipelineColorBlendAttachmentState []) {
 +            { .colorWriteMask =
 +                 VK_COLOR_COMPONENT_A_BIT |
 +                 VK_COLOR_COMPONENT_R_BIT |
 +                 VK_COLOR_COMPONENT_G_BIT |
 +                 VK_COLOR_COMPONENT_B_BIT },
 +         }
 +      },
 +      .pDynamicState = &(VkPipelineDynamicStateCreateInfo) {
 +         .sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO,
 +         .dynamicStateCount = 9,
 +         .pDynamicStates = (VkDynamicState[]) {
 +            VK_DYNAMIC_STATE_VIEWPORT,
 +            VK_DYNAMIC_STATE_SCISSOR,
 +            VK_DYNAMIC_STATE_LINE_WIDTH,
 +            VK_DYNAMIC_STATE_DEPTH_BIAS,
 +            VK_DYNAMIC_STATE_BLEND_CONSTANTS,
 +            VK_DYNAMIC_STATE_DEPTH_BOUNDS,
 +            VK_DYNAMIC_STATE_STENCIL_COMPARE_MASK,
 +            VK_DYNAMIC_STATE_STENCIL_WRITE_MASK,
 +            VK_DYNAMIC_STATE_STENCIL_REFERENCE,
 +         },
 +      },
 +      .flags = 0,
 +      .layout = device->meta_state.blit.pipeline_layout,
 +      .renderPass = device->meta_state.blit.render_pass,
 +      .subpass = 0,
 +   };
 +
 +   const struct anv_graphics_pipeline_create_info anv_pipeline_info = {
 +      .use_repclear = false,
 +      .disable_viewport = true,
 +      .disable_scissor = true,
 +      .disable_vs = true,
 +      .use_rectlist = true
 +   };
 +
 +   pipeline_shader_stages[1].module = anv_shader_module_to_handle(&fs_2d);
 +   anv_graphics_pipeline_create(anv_device_to_handle(device),
 +      &vk_pipeline_info, &anv_pipeline_info,
 +      NULL, &device->meta_state.blit.pipeline_2d_src);
 +
 +   pipeline_shader_stages[1].module = anv_shader_module_to_handle(&fs_3d);
 +   anv_graphics_pipeline_create(anv_device_to_handle(device),
 +      &vk_pipeline_info, &anv_pipeline_info,
 +      NULL, &device->meta_state.blit.pipeline_3d_src);
 +
 +   ralloc_free(vs.nir);
 +   ralloc_free(fs_2d.nir);
 +   ralloc_free(fs_3d.nir);
 +}
 +
 +static void
 +meta_prepare_blit(struct anv_cmd_buffer *cmd_buffer,
 +                  struct anv_meta_saved_state *saved_state)
 +{
 +   anv_meta_save(saved_state, cmd_buffer,
 +                 (1 << VK_DYNAMIC_STATE_VIEWPORT));
 +}
 +
 +struct blit_region {
 +   VkOffset3D src_offset;
 +   VkExtent3D src_extent;
 +   VkOffset3D dest_offset;
 +   VkExtent3D dest_extent;
 +};
 +
 +static void
 +meta_emit_blit(struct anv_cmd_buffer *cmd_buffer,
 +               struct anv_image *src_image,
 +               struct anv_image_view *src_iview,
 +               VkOffset3D src_offset,
 +               VkExtent3D src_extent,
 +               struct anv_image *dest_image,
 +               struct anv_image_view *dest_iview,
 +               VkOffset3D dest_offset,
 +               VkExtent3D dest_extent,
 +               VkFilter blit_filter)
 +{
 +   struct anv_device *device = cmd_buffer->device;
 +   VkDescriptorPool dummy_desc_pool = (VkDescriptorPool)1;
 +
 +   struct blit_vb_data {
 +      float pos[2];
 +      float tex_coord[3];
 +   } *vb_data;
 +
 +   unsigned vb_size = sizeof(struct anv_vue_header) + 3 * sizeof(*vb_data);
 +
 +   struct anv_state vb_state =
 +      anv_cmd_buffer_alloc_dynamic_state(cmd_buffer, vb_size, 16);
 +   memset(vb_state.map, 0, sizeof(struct anv_vue_header));
 +   vb_data = vb_state.map + sizeof(struct anv_vue_header);
 +
 +   vb_data[0] = (struct blit_vb_data) {
 +      .pos = {
 +         dest_offset.x + dest_extent.width,
 +         dest_offset.y + dest_extent.height,
 +      },
 +      .tex_coord = {
 +         (float)(src_offset.x + src_extent.width) / (float)src_iview->extent.width,
 +         (float)(src_offset.y + src_extent.height) / (float)src_iview->extent.height,
 +         (float)src_offset.z / (float)src_iview->extent.depth,
 +      },
 +   };
 +
 +   vb_data[1] = (struct blit_vb_data) {
 +      .pos = {
 +         dest_offset.x,
 +         dest_offset.y + dest_extent.height,
 +      },
 +      .tex_coord = {
 +         (float)src_offset.x / (float)src_iview->extent.width,
 +         (float)(src_offset.y + src_extent.height) / (float)src_iview->extent.height,
 +         (float)src_offset.z / (float)src_iview->extent.depth,
 +      },
 +   };
 +
 +   vb_data[2] = (struct blit_vb_data) {
 +      .pos = {
 +         dest_offset.x,
 +         dest_offset.y,
 +      },
 +      .tex_coord = {
 +         (float)src_offset.x / (float)src_iview->extent.width,
 +         (float)src_offset.y / (float)src_iview->extent.height,
 +         (float)src_offset.z / (float)src_iview->extent.depth,
 +      },
 +   };
 +
 +   anv_state_clflush(vb_state);
 +
 +   struct anv_buffer vertex_buffer = {
 +      .device = device,
 +      .size = vb_size,
 +      .bo = &device->dynamic_state_block_pool.bo,
 +      .offset = vb_state.offset,
 +   };
 +
 +   anv_CmdBindVertexBuffers(anv_cmd_buffer_to_handle(cmd_buffer), 0, 2,
 +      (VkBuffer[]) {
 +         anv_buffer_to_handle(&vertex_buffer),
 +         anv_buffer_to_handle(&vertex_buffer)
 +      },
 +      (VkDeviceSize[]) {
 +         0,
 +         sizeof(struct anv_vue_header),
 +      });
 +
 +   VkSampler sampler;
 +   ANV_CALL(CreateSampler)(anv_device_to_handle(device),
 +      &(VkSamplerCreateInfo) {
 +         .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
 +         .magFilter = blit_filter,
 +         .minFilter = blit_filter,
 +      }, &cmd_buffer->pool->alloc, &sampler);
 +
 +   VkDescriptorSet set;
 +   anv_AllocateDescriptorSets(anv_device_to_handle(device),
 +      &(VkDescriptorSetAllocateInfo) {
 +         .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
 +         .descriptorPool = dummy_desc_pool,
 +         .setLayoutCount = 1,
 +         .pSetLayouts = &device->meta_state.blit.ds_layout
 +      }, &set);
 +   anv_UpdateDescriptorSets(anv_device_to_handle(device),
 +      1, /* writeCount */
 +      (VkWriteDescriptorSet[]) {
 +         {
 +            .sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
 +            .dstSet = set,
 +            .dstBinding = 0,
 +            .dstArrayElement = 0,
 +            .descriptorCount = 1,
 +            .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
 +            .pImageInfo = (VkDescriptorImageInfo[]) {
 +               {
 +                  .sampler = sampler,
 +                  .imageView = anv_image_view_to_handle(src_iview),
 +                  .imageLayout = VK_IMAGE_LAYOUT_GENERAL,
 +               },
 +            }
 +         }
 +      }, 0, NULL);
 +
 +   VkFramebuffer fb;
 +   anv_CreateFramebuffer(anv_device_to_handle(device),
 +      &(VkFramebufferCreateInfo) {
 +         .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
 +         .attachmentCount = 1,
 +         .pAttachments = (VkImageView[]) {
 +            anv_image_view_to_handle(dest_iview),
 +         },
 +         .width = dest_iview->extent.width,
 +         .height = dest_iview->extent.height,
 +         .layers = 1
 +      }, &cmd_buffer->pool->alloc, &fb);
 +
 +   ANV_CALL(CmdBeginRenderPass)(anv_cmd_buffer_to_handle(cmd_buffer),
 +      &(VkRenderPassBeginInfo) {
 +         .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
 +         .renderPass = device->meta_state.blit.render_pass,
 +         .framebuffer = fb,
 +         .renderArea = {
 +            .offset = { dest_offset.x, dest_offset.y },
 +            .extent = { dest_extent.width, dest_extent.height },
 +         },
 +         .clearValueCount = 0,
 +         .pClearValues = NULL,
 +      }, VK_SUBPASS_CONTENTS_INLINE);
 +
 +   VkPipeline pipeline;
 +
 +   switch (src_image->type) {
 +   case VK_IMAGE_TYPE_1D:
 +      anv_finishme("VK_IMAGE_TYPE_1D");
 +      pipeline = device->meta_state.blit.pipeline_2d_src;
 +      break;
 +   case VK_IMAGE_TYPE_2D:
 +      pipeline = device->meta_state.blit.pipeline_2d_src;
 +      break;
 +   case VK_IMAGE_TYPE_3D:
 +      pipeline = device->meta_state.blit.pipeline_3d_src;
 +      break;
 +   default:
 +      unreachable(!"bad VkImageType");
 +   }
 +
 +   if (cmd_buffer->state.pipeline != anv_pipeline_from_handle(pipeline)) {
 +      anv_CmdBindPipeline(anv_cmd_buffer_to_handle(cmd_buffer),
 +                          VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline);
 +   }
 +
 +   anv_CmdSetViewport(anv_cmd_buffer_to_handle(cmd_buffer), 1,
 +                      &(VkViewport) {
 +                        .x = 0.0f,
 +                        .y = 0.0f,
 +                        .width = dest_iview->extent.width,
 +                        .height = dest_iview->extent.height,
 +                        .minDepth = 0.0f,
 +                        .maxDepth = 1.0f,
 +                      });
 +
 +   anv_CmdBindDescriptorSets(anv_cmd_buffer_to_handle(cmd_buffer),
 +                             VK_PIPELINE_BIND_POINT_GRAPHICS,
 +                             device->meta_state.blit.pipeline_layout, 0, 1,
 +                             &set, 0, NULL);
 +
 +   ANV_CALL(CmdDraw)(anv_cmd_buffer_to_handle(cmd_buffer), 3, 1, 0, 0);
 +
 +   ANV_CALL(CmdEndRenderPass)(anv_cmd_buffer_to_handle(cmd_buffer));
 +
 +   /* At the point where we emit the draw call, all data from the
 +    * descriptor sets, etc. has been used.  We are free to delete it.
 +    */
 +   anv_descriptor_set_destroy(device, anv_descriptor_set_from_handle(set));
 +   anv_DestroySampler(anv_device_to_handle(device), sampler,
 +                      &cmd_buffer->pool->alloc);
 +   anv_DestroyFramebuffer(anv_device_to_handle(device), fb,
 +                          &cmd_buffer->pool->alloc);
 +}
 +
 +static void
 +meta_finish_blit(struct anv_cmd_buffer *cmd_buffer,
 +                 const struct anv_meta_saved_state *saved_state)
 +{
 +   anv_meta_restore(saved_state, cmd_buffer);
 +}
 +
 +static VkFormat
 +vk_format_for_size(int bs)
 +{
 +   switch (bs) {
 +   case 1: return VK_FORMAT_R8_UINT;
 +   case 2: return VK_FORMAT_R8G8_UINT;
 +   case 3: return VK_FORMAT_R8G8B8_UINT;
 +   case 4: return VK_FORMAT_R8G8B8A8_UINT;
 +   case 6: return VK_FORMAT_R16G16B16_UINT;
 +   case 8: return VK_FORMAT_R16G16B16A16_UINT;
 +   case 12: return VK_FORMAT_R32G32B32_UINT;
 +   case 16: return VK_FORMAT_R32G32B32A32_UINT;
 +   default:
 +      unreachable("Invalid format block size");
 +   }
 +}
 +
 +static void
 +do_buffer_copy(struct anv_cmd_buffer *cmd_buffer,
 +               struct anv_bo *src, uint64_t src_offset,
 +               struct anv_bo *dest, uint64_t dest_offset,
 +               int width, int height, VkFormat copy_format)
 +{
 +   VkDevice vk_device = anv_device_to_handle(cmd_buffer->device);
 +
 +   VkImageCreateInfo image_info = {
 +      .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
 +      .imageType = VK_IMAGE_TYPE_2D,
 +      .format = copy_format,
 +      .extent = {
 +         .width = width,
 +         .height = height,
 +         .depth = 1,
 +      },
 +      .mipLevels = 1,
 +      .arrayLayers = 1,
 +      .samples = 1,
 +      .tiling = VK_IMAGE_TILING_LINEAR,
 +      .usage = 0,
 +      .flags = 0,
 +   };
 +
 +   VkImage src_image;
 +   image_info.usage = VK_IMAGE_USAGE_SAMPLED_BIT;
 +   anv_CreateImage(vk_device, &image_info,
 +                   &cmd_buffer->pool->alloc, &src_image);
 +
 +   VkImage dest_image;
 +   image_info.usage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
 +   anv_CreateImage(vk_device, &image_info,
 +                   &cmd_buffer->pool->alloc, &dest_image);
 +
 +   /* We could use a vk call to bind memory, but that would require
 +    * creating a dummy memory object etc. so there's really no point.
 +    */
 +   anv_image_from_handle(src_image)->bo = src;
 +   anv_image_from_handle(src_image)->offset = src_offset;
 +   anv_image_from_handle(dest_image)->bo = dest;
 +   anv_image_from_handle(dest_image)->offset = dest_offset;
 +
 +   struct anv_image_view src_iview;
 +   anv_image_view_init(&src_iview, cmd_buffer->device,
 +      &(VkImageViewCreateInfo) {
 +         .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
 +         .image = src_image,
 +         .viewType = VK_IMAGE_VIEW_TYPE_2D,
 +         .format = copy_format,
 +         .subresourceRange = {
 +            .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
 +            .baseMipLevel = 0,
 +            .levelCount = 1,
 +            .baseArrayLayer = 0,
 +            .layerCount = 1
 +         },
 +      },
 +      cmd_buffer);
 +
 +   struct anv_image_view dest_iview;
 +   anv_image_view_init(&dest_iview, cmd_buffer->device,
 +      &(VkImageViewCreateInfo) {
 +         .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
 +         .image = dest_image,
 +         .viewType = VK_IMAGE_VIEW_TYPE_2D,
 +         .format = copy_format,
 +         .subresourceRange = {
 +            .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
 +            .baseMipLevel = 0,
 +            .levelCount = 1,
 +            .baseArrayLayer = 0,
 +            .layerCount = 1,
 +         },
 +      },
 +      cmd_buffer);
 +
 +   meta_emit_blit(cmd_buffer,
 +                  anv_image_from_handle(src_image),
 +                  &src_iview,
 +                  (VkOffset3D) { 0, 0, 0 },
 +                  (VkExtent3D) { width, height, 1 },
 +                  anv_image_from_handle(dest_image),
 +                  &dest_iview,
 +                  (VkOffset3D) { 0, 0, 0 },
 +                  (VkExtent3D) { width, height, 1 },
 +                  VK_FILTER_NEAREST);
 +
 +   anv_DestroyImage(vk_device, src_image, &cmd_buffer->pool->alloc);
 +   anv_DestroyImage(vk_device, dest_image, &cmd_buffer->pool->alloc);
 +}
 +
 +void anv_CmdCopyBuffer(
 +    VkCommandBuffer                             commandBuffer,
 +    VkBuffer                                    srcBuffer,
 +    VkBuffer                                    destBuffer,
 +    uint32_t                                    regionCount,
 +    const VkBufferCopy*                         pRegions)
 +{
 +   ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
 +   ANV_FROM_HANDLE(anv_buffer, src_buffer, srcBuffer);
 +   ANV_FROM_HANDLE(anv_buffer, dest_buffer, destBuffer);
 +
 +   struct anv_meta_saved_state saved_state;
 +
 +   meta_prepare_blit(cmd_buffer, &saved_state);
 +
 +   for (unsigned r = 0; r < regionCount; r++) {
 +      uint64_t src_offset = src_buffer->offset + pRegions[r].srcOffset;
 +      uint64_t dest_offset = dest_buffer->offset + pRegions[r].dstOffset;
 +      uint64_t copy_size = pRegions[r].size;
 +
 +      /* First, we compute the biggest format that can be used with the
 +       * given offsets and size.
 +       */
 +      int bs = 16;
 +
 +      int fs = ffs(src_offset) - 1;
 +      if (fs != -1)
 +         bs = MIN2(bs, 1 << fs);
 +      assert(src_offset % bs == 0);
 +
 +      fs = ffs(dest_offset) - 1;
 +      if (fs != -1)
 +         bs = MIN2(bs, 1 << fs);
 +      assert(dest_offset % bs == 0);
 +
 +      fs = ffs(pRegions[r].size) - 1;
 +      if (fs != -1)
 +         bs = MIN2(bs, 1 << fs);
 +      assert(pRegions[r].size % bs == 0);
 +
 +      VkFormat copy_format = vk_format_for_size(bs);
 +
 +      /* This is maximum possible width/height our HW can handle */
 +      uint64_t max_surface_dim = 1 << 14;
 +
 +      /* First, we make a bunch of max-sized copies */
 +      uint64_t max_copy_size = max_surface_dim * max_surface_dim * bs;
 +      while (copy_size > max_copy_size) {
 +         do_buffer_copy(cmd_buffer, src_buffer->bo, src_offset,
 +                        dest_buffer->bo, dest_offset,
 +                        max_surface_dim, max_surface_dim, copy_format);
 +         copy_size -= max_copy_size;
 +         src_offset += max_copy_size;
 +         dest_offset += max_copy_size;
 +      }
 +
 +      uint64_t height = copy_size / (max_surface_dim * bs);
 +      assert(height < max_surface_dim);
 +      if (height != 0) {
 +         uint64_t rect_copy_size = height * max_surface_dim * bs;
 +         do_buffer_copy(cmd_buffer, src_buffer->bo, src_offset,
 +                        dest_buffer->bo, dest_offset,
 +                        max_surface_dim, height, copy_format);
 +         copy_size -= rect_copy_size;
 +         src_offset += rect_copy_size;
 +         dest_offset += rect_copy_size;
 +      }
 +
 +      if (copy_size != 0) {
 +         do_buffer_copy(cmd_buffer, src_buffer->bo, src_offset,
 +                        dest_buffer->bo, dest_offset,
 +                        copy_size / bs, 1, copy_format);
 +      }
 +   }
 +
 +   meta_finish_blit(cmd_buffer, &saved_state);
 +}
 +
 +void anv_CmdCopyImage(
 +    VkCommandBuffer                             commandBuffer,
 +    VkImage                                     srcImage,
 +    VkImageLayout                               srcImageLayout,
 +    VkImage                                     destImage,
 +    VkImageLayout                               destImageLayout,
 +    uint32_t                                    regionCount,
 +    const VkImageCopy*                          pRegions)
 +{
 +   ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
 +   ANV_FROM_HANDLE(anv_image, src_image, srcImage);
 +   ANV_FROM_HANDLE(anv_image, dest_image, destImage);
 +
 +   struct anv_meta_saved_state saved_state;
 +
 +   meta_prepare_blit(cmd_buffer, &saved_state);
 +
 +   for (unsigned r = 0; r < regionCount; r++) {
 +      struct anv_image_view src_iview;
 +      anv_image_view_init(&src_iview, cmd_buffer->device,
 +         &(VkImageViewCreateInfo) {
 +            .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
 +            .image = srcImage,
 +            .viewType = anv_meta_get_view_type(src_image),
 +            .format = src_image->format->vk_format,
 +            .subresourceRange = {
 +               .aspectMask = pRegions[r].srcSubresource.aspectMask,
 +               .baseMipLevel = pRegions[r].srcSubresource.mipLevel,
 +               .levelCount = 1,
 +               .baseArrayLayer = pRegions[r].srcSubresource.baseArrayLayer,
 +               .layerCount = pRegions[r].dstSubresource.layerCount,
 +            },
 +         },
 +         cmd_buffer);
 +
 +      const VkOffset3D dest_offset = {
 +         .x = pRegions[r].dstOffset.x,
 +         .y = pRegions[r].dstOffset.y,
 +         .z = 0,
 +      };
 +
 +      unsigned num_slices;
 +      if (src_image->type == VK_IMAGE_TYPE_3D) {
 +         assert(pRegions[r].srcSubresource.layerCount == 1 &&
 +                pRegions[r].dstSubresource.layerCount == 1);
 +         num_slices = pRegions[r].extent.depth;
 +      } else {
 +         assert(pRegions[r].srcSubresource.layerCount ==
 +                pRegions[r].dstSubresource.layerCount);
 +         assert(pRegions[r].extent.depth == 1);
 +         num_slices = pRegions[r].dstSubresource.layerCount;
 +      }
 +
 +      const uint32_t dest_base_array_slice =
 +         meta_blit_get_dest_view_base_array_slice(dest_image,
 +                                                  &pRegions[r].dstSubresource,
 +                                                  &pRegions[r].dstOffset);
 +
 +      for (unsigned slice = 0; slice < num_slices; slice++) {
 +         VkOffset3D src_offset = pRegions[r].srcOffset;
 +         src_offset.z += slice;
 +
 +         struct anv_image_view dest_iview;
 +         anv_image_view_init(&dest_iview, cmd_buffer->device,
 +            &(VkImageViewCreateInfo) {
 +               .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
 +               .image = destImage,
 +               .viewType = anv_meta_get_view_type(dest_image),
 +               .format = dest_image->format->vk_format,
 +               .subresourceRange = {
 +                  .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
 +                  .baseMipLevel = pRegions[r].dstSubresource.mipLevel,
 +                  .levelCount = 1,
 +                  .baseArrayLayer = dest_base_array_slice + slice,
 +                  .layerCount = 1
 +               },
 +            },
 +            cmd_buffer);
 +
 +         meta_emit_blit(cmd_buffer,
 +                        src_image, &src_iview,
 +                        src_offset,
 +                        pRegions[r].extent,
 +                        dest_image, &dest_iview,
 +                        dest_offset,
 +                        pRegions[r].extent,
 +                        VK_FILTER_NEAREST);
 +      }
 +   }
 +
 +   meta_finish_blit(cmd_buffer, &saved_state);
 +}
 +
 +void anv_CmdBlitImage(
 +    VkCommandBuffer                             commandBuffer,
 +    VkImage                                     srcImage,
 +    VkImageLayout                               srcImageLayout,
 +    VkImage                                     destImage,
 +    VkImageLayout                               destImageLayout,
 +    uint32_t                                    regionCount,
 +    const VkImageBlit*                          pRegions,
 +    VkFilter                                    filter)
 +
 +{
 +   ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
 +   ANV_FROM_HANDLE(anv_image, src_image, srcImage);
 +   ANV_FROM_HANDLE(anv_image, dest_image, destImage);
 +
 +   struct anv_meta_saved_state saved_state;
 +
 +   anv_finishme("respect VkFilter");
 +
 +   meta_prepare_blit(cmd_buffer, &saved_state);
 +
 +   for (unsigned r = 0; r < regionCount; r++) {
 +      struct anv_image_view src_iview;
 +      anv_image_view_init(&src_iview, cmd_buffer->device,
 +         &(VkImageViewCreateInfo) {
 +            .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
 +            .image = srcImage,
 +            .viewType = anv_meta_get_view_type(src_image),
 +            .format = src_image->format->vk_format,
 +            .subresourceRange = {
 +               .aspectMask = pRegions[r].srcSubresource.aspectMask,
 +               .baseMipLevel = pRegions[r].srcSubresource.mipLevel,
 +               .levelCount = 1,
 +               .baseArrayLayer = pRegions[r].srcSubresource.baseArrayLayer,
 +               .layerCount = 1
 +            },
 +         },
 +         cmd_buffer);
 +
 +      const VkOffset3D dest_offset = {
 +         .x = pRegions[r].dstOffset.x,
 +         .y = pRegions[r].dstOffset.y,
 +         .z = 0,
 +      };
 +
 +      const uint32_t dest_array_slice =
 +         meta_blit_get_dest_view_base_array_slice(dest_image,
 +                                                  &pRegions[r].dstSubresource,
 +                                                  &pRegions[r].dstOffset);
 +
 +      if (pRegions[r].srcSubresource.layerCount > 1)
 +         anv_finishme("FINISHME: copy multiple array layers");
 +
 +      if (pRegions[r].dstExtent.depth > 1)
 +         anv_finishme("FINISHME: copy multiple depth layers");
 +
 +      struct anv_image_view dest_iview;
 +      anv_image_view_init(&dest_iview, cmd_buffer->device,
 +         &(VkImageViewCreateInfo) {
 +            .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
 +            .image = destImage,
 +            .viewType = anv_meta_get_view_type(dest_image),
 +            .format = dest_image->format->vk_format,
 +            .subresourceRange = {
 +               .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
 +               .baseMipLevel = pRegions[r].dstSubresource.mipLevel,
 +               .levelCount = 1,
 +               .baseArrayLayer = dest_array_slice,
 +               .layerCount = 1
 +            },
 +         },
 +         cmd_buffer);
 +
 +      meta_emit_blit(cmd_buffer,
 +                     src_image, &src_iview,
 +                     pRegions[r].srcOffset,
 +                     pRegions[r].srcExtent,
 +                     dest_image, &dest_iview,
 +                     dest_offset,
 +                     pRegions[r].dstExtent,
 +                     filter);
 +   }
 +
 +   meta_finish_blit(cmd_buffer, &saved_state);
 +}
 +
 +static struct anv_image *
 +make_image_for_buffer(VkDevice vk_device, VkBuffer vk_buffer, VkFormat format,
 +                      VkImageUsageFlags usage,
 +                      VkImageType image_type,
 +                      const VkAllocationCallbacks *alloc,
 +                      const VkBufferImageCopy *copy)
 +{
 +   ANV_FROM_HANDLE(anv_buffer, buffer, vk_buffer);
 +
 +   VkExtent3D extent = copy->imageExtent;
 +   if (copy->bufferRowLength)
 +      extent.width = copy->bufferRowLength;
 +   if (copy->bufferImageHeight)
 +      extent.height = copy->bufferImageHeight;
 +   extent.depth = 1;
 +
 +   VkImage vk_image;
 +   VkResult result = anv_CreateImage(vk_device,
 +      &(VkImageCreateInfo) {
 +         .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
 +         .imageType = VK_IMAGE_TYPE_2D,
 +         .format = format,
 +         .extent = extent,
 +         .mipLevels = 1,
 +         .arrayLayers = 1,
 +         .samples = 1,
 +         .tiling = VK_IMAGE_TILING_LINEAR,
 +         .usage = usage,
 +         .flags = 0,
 +      }, alloc, &vk_image);
 +   assert(result == VK_SUCCESS);
 +
 +   ANV_FROM_HANDLE(anv_image, image, vk_image);
 +
 +   /* We could use a vk call to bind memory, but that would require
 +    * creating a dummy memory object etc. so there's really no point.
 +    */
 +   image->bo = buffer->bo;
 +   image->offset = buffer->offset + copy->bufferOffset;
 +
 +   return image;
 +}
 +
 +void anv_CmdCopyBufferToImage(
 +    VkCommandBuffer                             commandBuffer,
 +    VkBuffer                                    srcBuffer,
 +    VkImage                                     destImage,
 +    VkImageLayout                               destImageLayout,
 +    uint32_t                                    regionCount,
 +    const VkBufferImageCopy*                    pRegions)
 +{
 +   ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
 +   ANV_FROM_HANDLE(anv_image, dest_image, destImage);
 +   VkDevice vk_device = anv_device_to_handle(cmd_buffer->device);
 +   const VkFormat orig_format = dest_image->format->vk_format;
 +   struct anv_meta_saved_state saved_state;
 +
 +   meta_prepare_blit(cmd_buffer, &saved_state);
 +
 +   for (unsigned r = 0; r < regionCount; r++) {
 +      VkFormat proxy_format = orig_format;
 +      VkImageAspectFlags proxy_aspect = pRegions[r].imageSubresource.aspectMask;
 +
 +      if (orig_format == VK_FORMAT_S8_UINT) {
 +         proxy_format = VK_FORMAT_R8_UINT;
 +         proxy_aspect = VK_IMAGE_ASPECT_COLOR_BIT;
 +      }
 +
 +      struct anv_image *src_image =
 +         make_image_for_buffer(vk_device, srcBuffer, proxy_format,
 +                               VK_IMAGE_USAGE_SAMPLED_BIT,
 +                               dest_image->type, &cmd_buffer->pool->alloc,
 +                               &pRegions[r]);
 +
 +      const uint32_t dest_base_array_slice =
 +         meta_blit_get_dest_view_base_array_slice(dest_image,
 +                                                  &pRegions[r].imageSubresource,
 +                                                  &pRegions[r].imageOffset);
 +
 +      unsigned num_slices;
 +      if (dest_image->type == VK_IMAGE_TYPE_3D) {
 +         assert(pRegions[r].imageSubresource.layerCount == 1);
 +         num_slices = pRegions[r].imageExtent.depth;
 +      } else {
 +         assert(pRegions[r].imageExtent.depth == 1);
 +         num_slices = pRegions[r].imageSubresource.layerCount;
 +      }
 +
 +      for (unsigned slice = 0; slice < num_slices; slice++) {
 +         struct anv_image_view src_iview;
 +         anv_image_view_init(&src_iview, cmd_buffer->device,
 +            &(VkImageViewCreateInfo) {
 +               .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
 +               .image = anv_image_to_handle(src_image),
 +               .viewType = VK_IMAGE_VIEW_TYPE_2D,
 +               .format = proxy_format,
 +               .subresourceRange = {
 +                  .aspectMask = proxy_aspect,
 +                  .baseMipLevel = 0,
 +                  .levelCount = 1,
 +                  .baseArrayLayer = 0,
 +                  .layerCount = 1,
 +               },
 +            },
 +            cmd_buffer);
 +
 +         struct anv_image_view dest_iview;
 +         anv_image_view_init(&dest_iview, cmd_buffer->device,
 +            &(VkImageViewCreateInfo) {
 +               .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
 +               .image = anv_image_to_handle(dest_image),
 +               .viewType = anv_meta_get_view_type(dest_image),
 +               .format = proxy_format,
 +               .subresourceRange = {
 +                  .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
 +                  .baseMipLevel = pRegions[r].imageSubresource.mipLevel,
 +                  .levelCount = 1,
 +                  .baseArrayLayer = dest_base_array_slice + slice,
 +                  .layerCount = 1
 +               },
 +            },
 +            cmd_buffer);
 +
 +         VkOffset3D src_offset = { 0, 0, slice };
 +
 +         const VkOffset3D dest_offset = {
 +            .x = pRegions[r].imageOffset.x,
 +            .y = pRegions[r].imageOffset.y,
 +            .z = 0,
 +         };
 +
 +         meta_emit_blit(cmd_buffer,
 +                        src_image,
 +                        &src_iview,
 +                        src_offset,
 +                        pRegions[r].imageExtent,
 +                        dest_image,
 +                        &dest_iview,
 +                        dest_offset,
 +                        pRegions[r].imageExtent,
 +                        VK_FILTER_NEAREST);
 +
 +         /* Once we've done the blit, all of the actual information about
 +          * the image is embedded in the command buffer so we can just
 +          * increment the offset directly in the image effectively
 +          * re-binding it to different backing memory.
 +          */
 +         /* XXX: Insert a real CPP */
 +         src_image->offset += src_image->extent.width *
 +                              src_image->extent.height * 4;
 +      }
 +
 +      anv_DestroyImage(vk_device, anv_image_to_handle(src_image),
 +                       &cmd_buffer->pool->alloc);
 +   }
 +
 +   meta_finish_blit(cmd_buffer, &saved_state);
 +}
 +
 +void anv_CmdCopyImageToBuffer(
 +    VkCommandBuffer                             commandBuffer,
 +    VkImage                                     srcImage,
 +    VkImageLayout                               srcImageLayout,
 +    VkBuffer                                    destBuffer,
 +    uint32_t                                    regionCount,
 +    const VkBufferImageCopy*                    pRegions)
 +{
 +   ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
 +   ANV_FROM_HANDLE(anv_image, src_image, srcImage);
 +   VkDevice vk_device = anv_device_to_handle(cmd_buffer->device);
 +   struct anv_meta_saved_state saved_state;
 +
 +   meta_prepare_blit(cmd_buffer, &saved_state);
 +
 +   for (unsigned r = 0; r < regionCount; r++) {
 +      struct anv_image_view src_iview;
 +      anv_image_view_init(&src_iview, cmd_buffer->device,
 +         &(VkImageViewCreateInfo) {
 +            .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
 +            .image = srcImage,
 +            .viewType = anv_meta_get_view_type(src_image),
 +            .format = src_image->format->vk_format,
 +            .subresourceRange = {
 +               .aspectMask = pRegions[r].imageSubresource.aspectMask,
 +               .baseMipLevel = pRegions[r].imageSubresource.mipLevel,
 +               .levelCount = 1,
 +               .baseArrayLayer = pRegions[r].imageSubresource.baseArrayLayer,
 +               .layerCount = pRegions[r].imageSubresource.layerCount,
 +            },
 +         },
 +         cmd_buffer);
 +
 +      VkFormat dest_format = src_image->format->vk_format;
 +      if (dest_format == VK_FORMAT_S8_UINT) {
 +         dest_format = VK_FORMAT_R8_UINT;
 +      }
 +
 +      struct anv_image *dest_image =
 +         make_image_for_buffer(vk_device, destBuffer, dest_format,
 +                               VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
 +                               src_image->type, &cmd_buffer->pool->alloc,
 +                               &pRegions[r]);
 +
 +      unsigned num_slices;
 +      if (src_image->type == VK_IMAGE_TYPE_3D) {
 +         assert(pRegions[r].imageSubresource.layerCount == 1);
 +         num_slices = pRegions[r].imageExtent.depth;
 +      } else {
 +         assert(pRegions[r].imageExtent.depth == 1);
 +         num_slices = pRegions[r].imageSubresource.layerCount;
 +      }
 +
 +      for (unsigned slice = 0; slice < num_slices; slice++) {
 +         VkOffset3D src_offset = pRegions[r].imageOffset;
 +         src_offset.z += slice;
 +
 +         struct anv_image_view dest_iview;
 +         anv_image_view_init(&dest_iview, cmd_buffer->device,
 +            &(VkImageViewCreateInfo) {
 +               .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
 +               .image = anv_image_to_handle(dest_image),
 +               .viewType = VK_IMAGE_VIEW_TYPE_2D,
 +               .format = dest_format,
 +               .subresourceRange = {
 +                  .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
 +                  .baseMipLevel = 0,
 +                  .levelCount = 1,
 +                  .baseArrayLayer = 0,
 +                  .layerCount = 1
 +               },
 +            },
 +            cmd_buffer);
 +
 +         meta_emit_blit(cmd_buffer,
 +                        anv_image_from_handle(srcImage),
 +                        &src_iview,
 +                        src_offset,
 +                        pRegions[r].imageExtent,
 +                        dest_image,
 +                        &dest_iview,
 +                        (VkOffset3D) { 0, 0, 0 },
 +                        pRegions[r].imageExtent,
 +                        VK_FILTER_NEAREST);
 +
 +         /* Once we've done the blit, all of the actual information about
 +          * the image is embedded in the command buffer so we can just
 +          * increment the offset directly in the image effectively
 +          * re-binding it to different backing memory.
 +          */
 +         /* XXX: Insert a real CPP */
 +         dest_image->offset += dest_image->extent.width *
 +                               dest_image->extent.height * 4;
 +      }
 +
 +      anv_DestroyImage(vk_device, anv_image_to_handle(dest_image),
 +                       &cmd_buffer->pool->alloc);
 +   }
 +
 +   meta_finish_blit(cmd_buffer, &saved_state);
 +}
 +
 +void anv_CmdUpdateBuffer(
 +    VkCommandBuffer                             commandBuffer,
 +    VkBuffer                                    destBuffer,
 +    VkDeviceSize                                destOffset,
 +    VkDeviceSize                                dataSize,
 +    const uint32_t*                             pData)
 +{
 +   stub();
 +}
 +
 +void anv_CmdFillBuffer(
 +    VkCommandBuffer                             commandBuffer,
 +    VkBuffer                                    destBuffer,
 +    VkDeviceSize                                destOffset,
 +    VkDeviceSize                                fillSize,
 +    uint32_t                                    data)
 +{
 +   stub();
 +}
 +
 +void anv_CmdResolveImage(
 +    VkCommandBuffer                             commandBuffer,
 +    VkImage                                     srcImage,
 +    VkImageLayout                               srcImageLayout,
 +    VkImage                                     destImage,
 +    VkImageLayout                               destImageLayout,
 +    uint32_t                                    regionCount,
 +    const VkImageResolve*                       pRegions)
 +{
 +   stub();
 +}
 +
 +void
 +anv_device_init_meta(struct anv_device *device)
 +{
 +   anv_device_init_meta_clear_state(device);
 +   anv_device_init_meta_blit_state(device);
 +}
 +
 +void
 +anv_device_finish_meta(struct anv_device *device)
 +{
 +   anv_device_finish_meta_clear_state(device);
 +
 +   /* Blit */
 +   anv_DestroyRenderPass(anv_device_to_handle(device),
 +                         device->meta_state.blit.render_pass, NULL);
 +   anv_DestroyPipeline(anv_device_to_handle(device),
 +                       device->meta_state.blit.pipeline_2d_src, NULL);
 +   anv_DestroyPipeline(anv_device_to_handle(device),
 +                       device->meta_state.blit.pipeline_3d_src, NULL);
 +   anv_DestroyPipelineLayout(anv_device_to_handle(device),
 +                             device->meta_state.blit.pipeline_layout, NULL);
 +   anv_DestroyDescriptorSetLayout(anv_device_to_handle(device),
 +                                  device->meta_state.blit.ds_layout, NULL);
 +}
index 02be5a81984434656999fd36e8ccee108b1eb8a5,0000000000000000000000000000000000000000..c2070be10ec7de6b6fddf77d3a52f5e17352ad7c
mode 100644,000000..100644
--- /dev/null
@@@ -1,1159 -1,0 +1,1162 @@@
-    nir = brw_lower_nir(nir, &pipeline->device->info, NULL,
-                        compiler->scalar_stage[stage]);
 +/*
 + * 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.
 + */
 +
 +#include <assert.h>
 +#include <stdbool.h>
 +#include <string.h>
 +#include <unistd.h>
 +#include <fcntl.h>
 +
 +#include "anv_private.h"
 +#include "brw_nir.h"
 +#include "anv_nir.h"
 +#include "glsl/nir/nir_spirv.h"
 +
 +/* Needed for SWIZZLE macros */
 +#include "program/prog_instruction.h"
 +
 +// Shader functions
 +
 +VkResult anv_CreateShaderModule(
 +    VkDevice                                    _device,
 +    const VkShaderModuleCreateInfo*             pCreateInfo,
 +    const VkAllocationCallbacks*                pAllocator,
 +    VkShaderModule*                             pShaderModule)
 +{
 +   ANV_FROM_HANDLE(anv_device, device, _device);
 +   struct anv_shader_module *module;
 +
 +   assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO);
 +   assert(pCreateInfo->flags == 0);
 +
 +   module = anv_alloc2(&device->alloc, pAllocator,
 +                       sizeof(*module) + pCreateInfo->codeSize, 8,
 +                       VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
 +   if (module == NULL)
 +      return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
 +
 +   module->nir = NULL;
 +   module->size = pCreateInfo->codeSize;
 +   memcpy(module->data, pCreateInfo->pCode, module->size);
 +
 +   *pShaderModule = anv_shader_module_to_handle(module);
 +
 +   return VK_SUCCESS;
 +}
 +
 +void anv_DestroyShaderModule(
 +    VkDevice                                    _device,
 +    VkShaderModule                              _module,
 +    const VkAllocationCallbacks*                pAllocator)
 +{
 +   ANV_FROM_HANDLE(anv_device, device, _device);
 +   ANV_FROM_HANDLE(anv_shader_module, module, _module);
 +
 +   anv_free2(&device->alloc, pAllocator, module);
 +}
 +
 +#define SPIR_V_MAGIC_NUMBER 0x07230203
 +
 +/* Eventually, this will become part of anv_CreateShader.  Unfortunately,
 + * we can't do that yet because we don't have the ability to copy nir.
 + */
 +static nir_shader *
 +anv_shader_compile_to_nir(struct anv_device *device,
 +                          struct anv_shader_module *module,
 +                          const char *entrypoint_name,
 +                          gl_shader_stage stage)
 +{
 +   if (strcmp(entrypoint_name, "main") != 0) {
 +      anv_finishme("Multiple shaders per module not really supported");
 +   }
 +
 +   const struct brw_compiler *compiler =
 +      device->instance->physicalDevice.compiler;
 +   const nir_shader_compiler_options *nir_options =
 +      compiler->glsl_compiler_options[stage].NirOptions;
 +
 +   nir_shader *nir;
 +   if (module->nir) {
 +      /* Some things such as our meta clear/blit code will give us a NIR
 +       * shader directly.  In that case, we just ignore the SPIR-V entirely
 +       * and just use the NIR shader */
 +      nir = module->nir;
 +      nir->options = nir_options;
 +      nir_validate_shader(nir);
 +   } else {
 +      uint32_t *spirv = (uint32_t *) module->data;
 +      assert(spirv[0] == SPIR_V_MAGIC_NUMBER);
 +      assert(module->size % 4 == 0);
 +
 +      nir = spirv_to_nir(spirv, module->size / 4, stage, nir_options);
 +      nir_validate_shader(nir);
 +
 +      nir_lower_returns(nir);
 +      nir_validate_shader(nir);
 +
 +      nir_inline_functions(nir);
 +      nir_validate_shader(nir);
++
++      nir_lower_system_values(nir);
++      nir_validate_shader(nir);
 +   }
 +
 +   /* Vulkan uses the separate-shader linking model */
 +   nir->info.separate_shader = true;
 +
 +   /* Pick off the single entrypoint that we want */
 +   nir_function_impl *entrypoint = NULL;
 +   foreach_list_typed_safe(nir_function, func, node, &nir->functions) {
 +      if (strcmp(entrypoint_name, func->name) != 0) {
 +         /* Not our function, get rid of it */
 +         exec_node_remove(&func->node);
 +         continue;
 +      }
 +
 +      assert(exec_list_length(&func->overload_list) == 1);
 +      foreach_list_typed(nir_function_overload, overload, node,
 +                         &func->overload_list) {
 +         assert(overload->impl);
 +         entrypoint = overload->impl;
 +      }
 +   }
 +   assert(entrypoint != NULL);
 +
 +   nir = brw_preprocess_nir(nir, compiler->scalar_stage[stage]);
 +
 +   nir_shader_gather_info(nir, entrypoint);
 +
 +   return nir;
 +}
 +
 +VkResult anv_CreatePipelineCache(
 +    VkDevice                                    device,
 +    const VkPipelineCacheCreateInfo*            pCreateInfo,
 +    const VkAllocationCallbacks*                pAllocator,
 +    VkPipelineCache*                            pPipelineCache)
 +{
 +   *pPipelineCache = (VkPipelineCache)1;
 +
 +   stub_return(VK_SUCCESS);
 +}
 +
 +void anv_DestroyPipelineCache(
 +    VkDevice                                    _device,
 +    VkPipelineCache                             _cache,
 +    const VkAllocationCallbacks*                pAllocator)
 +{
 +}
 +
 +VkResult anv_GetPipelineCacheData(
 +    VkDevice                                    device,
 +    VkPipelineCache                             pipelineCache,
 +    size_t*                                     pDataSize,
 +    void*                                       pData)
 +{
 +   *pDataSize = 0;
 +   stub_return(VK_SUCCESS);
 +}
 +
 +VkResult anv_MergePipelineCaches(
 +    VkDevice                                    device,
 +    VkPipelineCache                             destCache,
 +    uint32_t                                    srcCacheCount,
 +    const VkPipelineCache*                      pSrcCaches)
 +{
 +   stub_return(VK_SUCCESS);
 +}
 +
 +void anv_DestroyPipeline(
 +    VkDevice                                    _device,
 +    VkPipeline                                  _pipeline,
 +    const VkAllocationCallbacks*                pAllocator)
 +{
 +   ANV_FROM_HANDLE(anv_device, device, _device);
 +   ANV_FROM_HANDLE(anv_pipeline, pipeline, _pipeline);
 +
 +   anv_reloc_list_finish(&pipeline->batch_relocs,
 +                         pAllocator ? pAllocator : &device->alloc);
 +   anv_state_stream_finish(&pipeline->program_stream);
 +   if (pipeline->blend_state.map)
 +      anv_state_pool_free(&device->dynamic_state_pool, pipeline->blend_state);
 +   anv_free2(&device->alloc, pAllocator, pipeline);
 +}
 +
 +static const uint32_t vk_to_gen_primitive_type[] = {
 +   [VK_PRIMITIVE_TOPOLOGY_POINT_LIST]                    = _3DPRIM_POINTLIST,
 +   [VK_PRIMITIVE_TOPOLOGY_LINE_LIST]                     = _3DPRIM_LINELIST,
 +   [VK_PRIMITIVE_TOPOLOGY_LINE_STRIP]                    = _3DPRIM_LINESTRIP,
 +   [VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST]                 = _3DPRIM_TRILIST,
 +   [VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP]                = _3DPRIM_TRISTRIP,
 +   [VK_PRIMITIVE_TOPOLOGY_TRIANGLE_FAN]                  = _3DPRIM_TRIFAN,
 +   [VK_PRIMITIVE_TOPOLOGY_LINE_LIST_WITH_ADJACENCY]      = _3DPRIM_LINELIST_ADJ,
 +   [VK_PRIMITIVE_TOPOLOGY_LINE_STRIP_WITH_ADJACENCY]     = _3DPRIM_LINESTRIP_ADJ,
 +   [VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST_WITH_ADJACENCY]  = _3DPRIM_TRILIST_ADJ,
 +   [VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP_WITH_ADJACENCY] = _3DPRIM_TRISTRIP_ADJ,
 +/*   [VK_PRIMITIVE_TOPOLOGY_PATCH_LIST]                = _3DPRIM_PATCHLIST_1 */
 +};
 +
 +static void
 +populate_sampler_prog_key(const struct brw_device_info *devinfo,
 +                          struct brw_sampler_prog_key_data *key)
 +{
 +   /* XXX: Handle texture swizzle on HSW- */
 +   for (int i = 0; i < MAX_SAMPLERS; i++) {
 +      /* Assume color sampler, no swizzling. (Works for BDW+) */
 +      key->swizzles[i] = SWIZZLE_XYZW;
 +   }
 +}
 +
 +static void
 +populate_vs_prog_key(const struct brw_device_info *devinfo,
 +                     struct brw_vs_prog_key *key)
 +{
 +   memset(key, 0, sizeof(*key));
 +
 +   populate_sampler_prog_key(devinfo, &key->tex);
 +
 +   /* XXX: Handle vertex input work-arounds */
 +
 +   /* XXX: Handle sampler_prog_key */
 +}
 +
 +static void
 +populate_gs_prog_key(const struct brw_device_info *devinfo,
 +                     struct brw_gs_prog_key *key)
 +{
 +   memset(key, 0, sizeof(*key));
 +
 +   populate_sampler_prog_key(devinfo, &key->tex);
 +}
 +
 +static void
 +populate_wm_prog_key(const struct brw_device_info *devinfo,
 +                     const VkGraphicsPipelineCreateInfo *info,
 +                     struct brw_wm_prog_key *key)
 +{
 +   ANV_FROM_HANDLE(anv_render_pass, render_pass, info->renderPass);
 +
 +   memset(key, 0, sizeof(*key));
 +
 +   populate_sampler_prog_key(devinfo, &key->tex);
 +
 +   /* TODO: Fill out key->input_slots_valid */
 +
 +   /* Vulkan doesn't specify a default */
 +   key->high_quality_derivatives = false;
 +
 +   /* XXX Vulkan doesn't appear to specify */
 +   key->clamp_fragment_color = false;
 +
 +   /* Vulkan always specifies upper-left coordinates */
 +   key->drawable_height = 0;
 +   key->render_to_fbo = false;
 +
 +   key->nr_color_regions = render_pass->subpasses[info->subpass].color_count;
 +
 +   key->replicate_alpha = key->nr_color_regions > 1 &&
 +                          info->pMultisampleState &&
 +                          info->pMultisampleState->alphaToCoverageEnable;
 +
 +   if (info->pMultisampleState && info->pMultisampleState->rasterizationSamples > 1) {
 +      /* We should probably pull this out of the shader, but it's fairly
 +       * harmless to compute it and then let dead-code take care of it.
 +       */
 +      key->persample_shading = info->pMultisampleState->sampleShadingEnable;
 +      if (key->persample_shading)
 +         key->persample_2x = info->pMultisampleState->rasterizationSamples == 2;
 +
 +      key->compute_pos_offset = info->pMultisampleState->sampleShadingEnable;
 +      key->compute_sample_id = info->pMultisampleState->sampleShadingEnable;
 +   }
 +}
 +
 +static void
 +populate_cs_prog_key(const struct brw_device_info *devinfo,
 +                     struct brw_cs_prog_key *key)
 +{
 +   memset(key, 0, sizeof(*key));
 +
 +   populate_sampler_prog_key(devinfo, &key->tex);
 +}
 +
 +static nir_shader *
 +anv_pipeline_compile(struct anv_pipeline *pipeline,
 +                     struct anv_shader_module *module,
 +                     const char *entrypoint,
 +                     gl_shader_stage stage,
 +                     struct brw_stage_prog_data *prog_data)
 +{
 +   const struct brw_compiler *compiler =
 +      pipeline->device->instance->physicalDevice.compiler;
 +
 +   nir_shader *nir = anv_shader_compile_to_nir(pipeline->device,
 +                                               module, entrypoint, stage);
 +   if (nir == NULL)
 +      return NULL;
 +
 +   anv_nir_lower_push_constants(nir, compiler->scalar_stage[stage]);
 +
 +   /* Figure out the number of parameters */
 +   prog_data->nr_params = 0;
 +
 +   if (nir->num_uniforms > 0) {
 +      /* If the shader uses any push constants at all, we'll just give
 +       * them the maximum possible number
 +       */
 +      prog_data->nr_params += MAX_PUSH_CONSTANTS_SIZE / sizeof(float);
 +   }
 +
 +   if (pipeline->layout && pipeline->layout->stage[stage].has_dynamic_offsets)
 +      prog_data->nr_params += MAX_DYNAMIC_BUFFERS * 2;
 +
 +   if (pipeline->layout && pipeline->layout->stage[stage].image_count > 0)
 +      prog_data->nr_params += pipeline->layout->stage[stage].image_count *
 +                              BRW_IMAGE_PARAM_SIZE;
 +
 +   if (prog_data->nr_params > 0) {
 +      /* XXX: I think we're leaking this */
 +      prog_data->param = (const union gl_constant_value **)
 +         malloc(prog_data->nr_params * sizeof(union gl_constant_value *));
 +
 +      /* We now set the param values to be offsets into a
 +       * anv_push_constant_data structure.  Since the compiler doesn't
 +       * actually dereference any of the gl_constant_value pointers in the
 +       * params array, it doesn't really matter what we put here.
 +       */
 +      struct anv_push_constants *null_data = NULL;
 +      if (nir->num_uniforms > 0) {
 +         /* Fill out the push constants section of the param array */
 +         for (unsigned i = 0; i < MAX_PUSH_CONSTANTS_SIZE / sizeof(float); i++)
 +            prog_data->param[i] = (const union gl_constant_value *)
 +               &null_data->client_data[i * sizeof(float)];
 +      }
 +   }
 +
 +   /* Set up dynamic offsets */
 +   anv_nir_apply_dynamic_offsets(pipeline, nir, prog_data);
 +
 +   /* Apply the actual pipeline layout to UBOs, SSBOs, and textures */
 +   if (pipeline->layout)
 +      anv_nir_apply_pipeline_layout(nir, prog_data, pipeline->layout);
 +
 +   /* All binding table offsets provided by apply_pipeline_layout() are
 +    * relative to the start of the bindint table (plus MAX_RTS for VS).
 +    */
 +   unsigned bias;
 +   switch (stage) {
 +   case MESA_SHADER_FRAGMENT:
 +      bias = MAX_RTS;
 +      break;
 +   case MESA_SHADER_COMPUTE:
 +      bias = 1;
 +      break;
 +   default:
 +      bias = 0;
 +      break;
 +   }
 +   prog_data->binding_table.size_bytes = 0;
 +   prog_data->binding_table.texture_start = bias;
 +   prog_data->binding_table.ubo_start = bias;
 +   prog_data->binding_table.ssbo_start = bias;
 +   prog_data->binding_table.image_start = bias;
 +
 +   /* Finish the optimization and compilation process */
++   nir = brw_nir_lower_io(nir, &pipeline->device->info,
++                          compiler->scalar_stage[stage]);
 +
 +   /* nir_lower_io will only handle the push constants; we need to set this
 +    * to the full number of possible uniforms.
 +    */
 +   nir->num_uniforms = prog_data->nr_params * 4;
 +
 +   return nir;
 +}
 +
 +static uint32_t
 +anv_pipeline_upload_kernel(struct anv_pipeline *pipeline,
 +                           const void *data, size_t size)
 +{
 +   struct anv_state state =
 +      anv_state_stream_alloc(&pipeline->program_stream, size, 64);
 +
 +   assert(size < pipeline->program_stream.block_pool->block_size);
 +
 +   memcpy(state.map, data, size);
 +
 +   if (!pipeline->device->info.has_llc)
 +      anv_state_clflush(state);
 +
 +   return state.offset;
 +}
 +
 +static void
 +anv_pipeline_add_compiled_stage(struct anv_pipeline *pipeline,
 +                                gl_shader_stage stage,
 +                                struct brw_stage_prog_data *prog_data)
 +{
 +   struct brw_device_info *devinfo = &pipeline->device->info;
 +   uint32_t max_threads[] = {
 +      [MESA_SHADER_VERTEX]                  = devinfo->max_vs_threads,
 +      [MESA_SHADER_TESS_CTRL]               = 0,
 +      [MESA_SHADER_TESS_EVAL]               = 0,
 +      [MESA_SHADER_GEOMETRY]                = devinfo->max_gs_threads,
 +      [MESA_SHADER_FRAGMENT]                = devinfo->max_wm_threads,
 +      [MESA_SHADER_COMPUTE]                 = devinfo->max_cs_threads,
 +   };
 +
 +   pipeline->prog_data[stage] = prog_data;
 +   pipeline->active_stages |= mesa_to_vk_shader_stage(stage);
 +   pipeline->scratch_start[stage] = pipeline->total_scratch;
 +   pipeline->total_scratch =
 +      align_u32(pipeline->total_scratch, 1024) +
 +      prog_data->total_scratch * max_threads[stage];
 +}
 +
 +static VkResult
 +anv_pipeline_compile_vs(struct anv_pipeline *pipeline,
 +                        const VkGraphicsPipelineCreateInfo *info,
 +                        struct anv_shader_module *module,
 +                        const char *entrypoint)
 +{
 +   const struct brw_compiler *compiler =
 +      pipeline->device->instance->physicalDevice.compiler;
 +   struct brw_vs_prog_data *prog_data = &pipeline->vs_prog_data;
 +   struct brw_vs_prog_key key;
 +
 +   populate_vs_prog_key(&pipeline->device->info, &key);
 +
 +   /* TODO: Look up shader in cache */
 +
 +   memset(prog_data, 0, sizeof(*prog_data));
 +
 +   nir_shader *nir = anv_pipeline_compile(pipeline, module, entrypoint,
 +                                          MESA_SHADER_VERTEX,
 +                                          &prog_data->base.base);
 +   if (nir == NULL)
 +      return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
 +
 +   void *mem_ctx = ralloc_context(NULL);
 +
 +   if (module->nir == NULL)
 +      ralloc_steal(mem_ctx, nir);
 +
 +   prog_data->inputs_read = nir->info.inputs_read;
 +   pipeline->writes_point_size = nir->info.outputs_written & VARYING_SLOT_PSIZ;
 +
 +   brw_compute_vue_map(&pipeline->device->info,
 +                       &prog_data->base.vue_map,
 +                       nir->info.outputs_written,
 +                       nir->info.separate_shader);
 +
 +   unsigned code_size;
 +   const unsigned *shader_code =
 +      brw_compile_vs(compiler, NULL, mem_ctx, &key, prog_data, nir,
 +                     NULL, false, -1, &code_size, NULL);
 +   if (shader_code == NULL) {
 +      ralloc_free(mem_ctx);
 +      return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
 +   }
 +
 +   const uint32_t offset =
 +      anv_pipeline_upload_kernel(pipeline, shader_code, code_size);
 +   if (prog_data->base.dispatch_mode == DISPATCH_MODE_SIMD8) {
 +      pipeline->vs_simd8 = offset;
 +      pipeline->vs_vec4 = NO_KERNEL;
 +   } else {
 +      pipeline->vs_simd8 = NO_KERNEL;
 +      pipeline->vs_vec4 = offset;
 +   }
 +
 +   ralloc_free(mem_ctx);
 +
 +   anv_pipeline_add_compiled_stage(pipeline, MESA_SHADER_VERTEX,
 +                                   &prog_data->base.base);
 +
 +   return VK_SUCCESS;
 +}
 +
 +static VkResult
 +anv_pipeline_compile_gs(struct anv_pipeline *pipeline,
 +                        const VkGraphicsPipelineCreateInfo *info,
 +                        struct anv_shader_module *module,
 +                        const char *entrypoint)
 +{
 +   const struct brw_compiler *compiler =
 +      pipeline->device->instance->physicalDevice.compiler;
 +   struct brw_gs_prog_data *prog_data = &pipeline->gs_prog_data;
 +   struct brw_gs_prog_key key;
 +
 +   populate_gs_prog_key(&pipeline->device->info, &key);
 +
 +   /* TODO: Look up shader in cache */
 +
 +   memset(prog_data, 0, sizeof(*prog_data));
 +
 +   nir_shader *nir = anv_pipeline_compile(pipeline, module, entrypoint,
 +                                          MESA_SHADER_GEOMETRY,
 +                                          &prog_data->base.base);
 +   if (nir == NULL)
 +      return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
 +
 +   void *mem_ctx = ralloc_context(NULL);
 +
 +   if (module->nir == NULL)
 +      ralloc_steal(mem_ctx, nir);
 +
 +   brw_compute_vue_map(&pipeline->device->info,
 +                       &prog_data->base.vue_map,
 +                       nir->info.outputs_written,
 +                       nir->info.separate_shader);
 +
 +   unsigned code_size;
 +   const unsigned *shader_code =
 +      brw_compile_gs(compiler, NULL, mem_ctx, &key, prog_data, nir,
 +                     NULL, -1, &code_size, NULL);
 +   if (shader_code == NULL) {
 +      ralloc_free(mem_ctx);
 +      return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
 +   }
 +
 +   /* TODO: SIMD8 GS */
 +   pipeline->gs_kernel =
 +      anv_pipeline_upload_kernel(pipeline, shader_code, code_size);
 +   pipeline->gs_vertex_count = nir->info.gs.vertices_in;
 +
 +   ralloc_free(mem_ctx);
 +
 +   anv_pipeline_add_compiled_stage(pipeline, MESA_SHADER_GEOMETRY,
 +                                   &prog_data->base.base);
 +
 +   return VK_SUCCESS;
 +}
 +
 +static VkResult
 +anv_pipeline_compile_fs(struct anv_pipeline *pipeline,
 +                        const VkGraphicsPipelineCreateInfo *info,
 +                        struct anv_shader_module *module,
 +                        const char *entrypoint)
 +{
 +   const struct brw_compiler *compiler =
 +      pipeline->device->instance->physicalDevice.compiler;
 +   struct brw_wm_prog_data *prog_data = &pipeline->wm_prog_data;
 +   struct brw_wm_prog_key key;
 +
 +   populate_wm_prog_key(&pipeline->device->info, info, &key);
 +
 +   if (pipeline->use_repclear)
 +      key.nr_color_regions = 1;
 +
 +   /* TODO: Look up shader in cache */
 +
 +   memset(prog_data, 0, sizeof(*prog_data));
 +
 +   prog_data->binding_table.render_target_start = 0;
 +
 +   nir_shader *nir = anv_pipeline_compile(pipeline, module, entrypoint,
 +                                          MESA_SHADER_FRAGMENT,
 +                                          &prog_data->base);
 +   if (nir == NULL)
 +      return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
 +
 +   void *mem_ctx = ralloc_context(NULL);
 +
 +   if (module->nir == NULL)
 +      ralloc_steal(mem_ctx, nir);
 +
 +   unsigned code_size;
 +   const unsigned *shader_code =
 +      brw_compile_fs(compiler, NULL, mem_ctx, &key, prog_data, nir,
 +                     NULL, -1, -1, pipeline->use_repclear, &code_size, NULL);
 +   if (shader_code == NULL) {
 +      ralloc_free(mem_ctx);
 +      return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
 +   }
 +
 +   uint32_t offset = anv_pipeline_upload_kernel(pipeline,
 +                                                shader_code, code_size);
 +   if (prog_data->no_8)
 +      pipeline->ps_simd8 = NO_KERNEL;
 +   else
 +      pipeline->ps_simd8 = offset;
 +
 +   if (prog_data->no_8 || prog_data->prog_offset_16) {
 +      pipeline->ps_simd16 = offset + prog_data->prog_offset_16;
 +   } else {
 +      pipeline->ps_simd16 = NO_KERNEL;
 +   }
 +
 +   pipeline->ps_ksp2 = 0;
 +   pipeline->ps_grf_start2 = 0;
 +   if (pipeline->ps_simd8 != NO_KERNEL) {
 +      pipeline->ps_ksp0 = pipeline->ps_simd8;
 +      pipeline->ps_grf_start0 = prog_data->base.dispatch_grf_start_reg;
 +      if (pipeline->ps_simd16 != NO_KERNEL) {
 +         pipeline->ps_ksp2 = pipeline->ps_simd16;
 +         pipeline->ps_grf_start2 = prog_data->dispatch_grf_start_reg_16;
 +      }
 +   } else if (pipeline->ps_simd16 != NO_KERNEL) {
 +      pipeline->ps_ksp0 = pipeline->ps_simd16;
 +      pipeline->ps_grf_start0 = prog_data->dispatch_grf_start_reg_16;
 +   }
 +
 +   ralloc_free(mem_ctx);
 +
 +   anv_pipeline_add_compiled_stage(pipeline, MESA_SHADER_FRAGMENT,
 +                                   &prog_data->base);
 +
 +   return VK_SUCCESS;
 +}
 +
 +VkResult
 +anv_pipeline_compile_cs(struct anv_pipeline *pipeline,
 +                        const VkComputePipelineCreateInfo *info,
 +                        struct anv_shader_module *module,
 +                        const char *entrypoint)
 +{
 +   const struct brw_compiler *compiler =
 +      pipeline->device->instance->physicalDevice.compiler;
 +   struct brw_cs_prog_data *prog_data = &pipeline->cs_prog_data;
 +   struct brw_cs_prog_key key;
 +
 +   populate_cs_prog_key(&pipeline->device->info, &key);
 +
 +   /* TODO: Look up shader in cache */
 +
 +   memset(prog_data, 0, sizeof(*prog_data));
 +
 +   prog_data->binding_table.work_groups_start = 0;
 +
 +   nir_shader *nir = anv_pipeline_compile(pipeline, module, entrypoint,
 +                                          MESA_SHADER_COMPUTE,
 +                                          &prog_data->base);
 +   if (nir == NULL)
 +      return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
 +
 +   void *mem_ctx = ralloc_context(NULL);
 +
 +   if (module->nir == NULL)
 +      ralloc_steal(mem_ctx, nir);
 +
 +   unsigned code_size;
 +   const unsigned *shader_code =
 +      brw_compile_cs(compiler, NULL, mem_ctx, &key, prog_data, nir,
 +                     -1, &code_size, NULL);
 +   if (shader_code == NULL) {
 +      ralloc_free(mem_ctx);
 +      return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
 +   }
 +
 +   pipeline->cs_simd = anv_pipeline_upload_kernel(pipeline,
 +                                                  shader_code, code_size);
 +   ralloc_free(mem_ctx);
 +
 +   anv_pipeline_add_compiled_stage(pipeline, MESA_SHADER_COMPUTE,
 +                                   &prog_data->base);
 +
 +   return VK_SUCCESS;
 +}
 +
 +static const int gen8_push_size = 32 * 1024;
 +
 +static void
 +gen7_compute_urb_partition(struct anv_pipeline *pipeline)
 +{
 +   const struct brw_device_info *devinfo = &pipeline->device->info;
 +   bool vs_present = pipeline->active_stages & VK_SHADER_STAGE_VERTEX_BIT;
 +   unsigned vs_size = vs_present ? pipeline->vs_prog_data.base.urb_entry_size : 1;
 +   unsigned vs_entry_size_bytes = vs_size * 64;
 +   bool gs_present = pipeline->active_stages & VK_SHADER_STAGE_GEOMETRY_BIT;
 +   unsigned gs_size = gs_present ? pipeline->gs_prog_data.base.urb_entry_size : 1;
 +   unsigned gs_entry_size_bytes = gs_size * 64;
 +
 +   /* From p35 of the Ivy Bridge PRM (section 1.7.1: 3DSTATE_URB_GS):
 +    *
 +    *     VS Number of URB Entries must be divisible by 8 if the VS URB Entry
 +    *     Allocation Size is less than 9 512-bit URB entries.
 +    *
 +    * Similar text exists for GS.
 +    */
 +   unsigned vs_granularity = (vs_size < 9) ? 8 : 1;
 +   unsigned gs_granularity = (gs_size < 9) ? 8 : 1;
 +
 +   /* URB allocations must be done in 8k chunks. */
 +   unsigned chunk_size_bytes = 8192;
 +
 +   /* Determine the size of the URB in chunks. */
 +   unsigned urb_chunks = devinfo->urb.size * 1024 / chunk_size_bytes;
 +
 +   /* Reserve space for push constants */
 +   unsigned push_constant_bytes = gen8_push_size;
 +   unsigned push_constant_chunks =
 +      push_constant_bytes / chunk_size_bytes;
 +
 +   /* Initially, assign each stage the minimum amount of URB space it needs,
 +    * and make a note of how much additional space it "wants" (the amount of
 +    * additional space it could actually make use of).
 +    */
 +
 +   /* VS has a lower limit on the number of URB entries */
 +   unsigned vs_chunks =
 +      ALIGN(devinfo->urb.min_vs_entries * vs_entry_size_bytes,
 +            chunk_size_bytes) / chunk_size_bytes;
 +   unsigned vs_wants =
 +      ALIGN(devinfo->urb.max_vs_entries * vs_entry_size_bytes,
 +            chunk_size_bytes) / chunk_size_bytes - vs_chunks;
 +
 +   unsigned gs_chunks = 0;
 +   unsigned gs_wants = 0;
 +   if (gs_present) {
 +      /* There are two constraints on the minimum amount of URB space we can
 +       * allocate:
 +       *
 +       * (1) We need room for at least 2 URB entries, since we always operate
 +       * the GS in DUAL_OBJECT mode.
 +       *
 +       * (2) We can't allocate less than nr_gs_entries_granularity.
 +       */
 +      gs_chunks = ALIGN(MAX2(gs_granularity, 2) * gs_entry_size_bytes,
 +                        chunk_size_bytes) / chunk_size_bytes;
 +      gs_wants =
 +         ALIGN(devinfo->urb.max_gs_entries * gs_entry_size_bytes,
 +               chunk_size_bytes) / chunk_size_bytes - gs_chunks;
 +   }
 +
 +   /* There should always be enough URB space to satisfy the minimum
 +    * requirements of each stage.
 +    */
 +   unsigned total_needs = push_constant_chunks + vs_chunks + gs_chunks;
 +   assert(total_needs <= urb_chunks);
 +
 +   /* Mete out remaining space (if any) in proportion to "wants". */
 +   unsigned total_wants = vs_wants + gs_wants;
 +   unsigned remaining_space = urb_chunks - total_needs;
 +   if (remaining_space > total_wants)
 +      remaining_space = total_wants;
 +   if (remaining_space > 0) {
 +      unsigned vs_additional = (unsigned)
 +         round(vs_wants * (((double) remaining_space) / total_wants));
 +      vs_chunks += vs_additional;
 +      remaining_space -= vs_additional;
 +      gs_chunks += remaining_space;
 +   }
 +
 +   /* Sanity check that we haven't over-allocated. */
 +   assert(push_constant_chunks + vs_chunks + gs_chunks <= urb_chunks);
 +
 +   /* Finally, compute the number of entries that can fit in the space
 +    * allocated to each stage.
 +    */
 +   unsigned nr_vs_entries = vs_chunks * chunk_size_bytes / vs_entry_size_bytes;
 +   unsigned nr_gs_entries = gs_chunks * chunk_size_bytes / gs_entry_size_bytes;
 +
 +   /* Since we rounded up when computing *_wants, this may be slightly more
 +    * than the maximum allowed amount, so correct for that.
 +    */
 +   nr_vs_entries = MIN2(nr_vs_entries, devinfo->urb.max_vs_entries);
 +   nr_gs_entries = MIN2(nr_gs_entries, devinfo->urb.max_gs_entries);
 +
 +   /* Ensure that we program a multiple of the granularity. */
 +   nr_vs_entries = ROUND_DOWN_TO(nr_vs_entries, vs_granularity);
 +   nr_gs_entries = ROUND_DOWN_TO(nr_gs_entries, gs_granularity);
 +
 +   /* Finally, sanity check to make sure we have at least the minimum number
 +    * of entries needed for each stage.
 +    */
 +   assert(nr_vs_entries >= devinfo->urb.min_vs_entries);
 +   if (gs_present)
 +      assert(nr_gs_entries >= 2);
 +
 +   /* Lay out the URB in the following order:
 +    * - push constants
 +    * - VS
 +    * - GS
 +    */
 +   pipeline->urb.vs_start = push_constant_chunks;
 +   pipeline->urb.vs_size = vs_size;
 +   pipeline->urb.nr_vs_entries = nr_vs_entries;
 +
 +   pipeline->urb.gs_start = push_constant_chunks + vs_chunks;
 +   pipeline->urb.gs_size = gs_size;
 +   pipeline->urb.nr_gs_entries = nr_gs_entries;
 +}
 +
 +static void
 +anv_pipeline_init_dynamic_state(struct anv_pipeline *pipeline,
 +                                const VkGraphicsPipelineCreateInfo *pCreateInfo)
 +{
 +   anv_cmd_dirty_mask_t states = ANV_CMD_DIRTY_DYNAMIC_ALL;
 +   ANV_FROM_HANDLE(anv_render_pass, pass, pCreateInfo->renderPass);
 +   struct anv_subpass *subpass = &pass->subpasses[pCreateInfo->subpass];
 +
 +   pipeline->dynamic_state = default_dynamic_state;
 +
 +   if (pCreateInfo->pDynamicState) {
 +      /* Remove all of the states that are marked as dynamic */
 +      uint32_t count = pCreateInfo->pDynamicState->dynamicStateCount;
 +      for (uint32_t s = 0; s < count; s++)
 +         states &= ~(1 << pCreateInfo->pDynamicState->pDynamicStates[s]);
 +   }
 +
 +   struct anv_dynamic_state *dynamic = &pipeline->dynamic_state;
 +
 +   dynamic->viewport.count = pCreateInfo->pViewportState->viewportCount;
 +   if (states & (1 << VK_DYNAMIC_STATE_VIEWPORT)) {
 +      typed_memcpy(dynamic->viewport.viewports,
 +                   pCreateInfo->pViewportState->pViewports,
 +                   pCreateInfo->pViewportState->viewportCount);
 +   }
 +
 +   dynamic->scissor.count = pCreateInfo->pViewportState->scissorCount;
 +   if (states & (1 << VK_DYNAMIC_STATE_SCISSOR)) {
 +      typed_memcpy(dynamic->scissor.scissors,
 +                   pCreateInfo->pViewportState->pScissors,
 +                   pCreateInfo->pViewportState->scissorCount);
 +   }
 +
 +   if (states & (1 << VK_DYNAMIC_STATE_LINE_WIDTH)) {
 +      assert(pCreateInfo->pRasterizationState);
 +      dynamic->line_width = pCreateInfo->pRasterizationState->lineWidth;
 +   }
 +
 +   if (states & (1 << VK_DYNAMIC_STATE_DEPTH_BIAS)) {
 +      assert(pCreateInfo->pRasterizationState);
 +      dynamic->depth_bias.bias =
 +         pCreateInfo->pRasterizationState->depthBiasConstantFactor;
 +      dynamic->depth_bias.clamp =
 +         pCreateInfo->pRasterizationState->depthBiasClamp;
 +      dynamic->depth_bias.slope =
 +         pCreateInfo->pRasterizationState->depthBiasSlopeFactor;
 +   }
 +
 +   if (states & (1 << VK_DYNAMIC_STATE_BLEND_CONSTANTS)) {
 +      assert(pCreateInfo->pColorBlendState);
 +      typed_memcpy(dynamic->blend_constants,
 +                   pCreateInfo->pColorBlendState->blendConstants, 4);
 +   }
 +
 +   /* If there is no depthstencil attachment, then don't read
 +    * pDepthStencilState. The Vulkan spec states that pDepthStencilState may
 +    * be NULL in this case. Even if pDepthStencilState is non-NULL, there is
 +    * no need to override the depthstencil defaults in
 +    * anv_pipeline::dynamic_state when there is no depthstencil attachment.
 +    *
 +    * From the Vulkan spec (20 Oct 2015, git-aa308cb):
 +    *
 +    *    pDepthStencilState [...] may only be NULL if renderPass and subpass
 +    *    specify a subpass that has no depth/stencil attachment.
 +    */
 +   if (subpass->depth_stencil_attachment != VK_ATTACHMENT_UNUSED) {
 +      if (states & (1 << VK_DYNAMIC_STATE_DEPTH_BOUNDS)) {
 +         assert(pCreateInfo->pDepthStencilState);
 +         dynamic->depth_bounds.min =
 +            pCreateInfo->pDepthStencilState->minDepthBounds;
 +         dynamic->depth_bounds.max =
 +            pCreateInfo->pDepthStencilState->maxDepthBounds;
 +      }
 +
 +      if (states & (1 << VK_DYNAMIC_STATE_STENCIL_COMPARE_MASK)) {
 +         assert(pCreateInfo->pDepthStencilState);
 +         dynamic->stencil_compare_mask.front =
 +            pCreateInfo->pDepthStencilState->front.compareMask;
 +         dynamic->stencil_compare_mask.back =
 +            pCreateInfo->pDepthStencilState->back.compareMask;
 +      }
 +
 +      if (states & (1 << VK_DYNAMIC_STATE_STENCIL_WRITE_MASK)) {
 +         assert(pCreateInfo->pDepthStencilState);
 +         dynamic->stencil_write_mask.front =
 +            pCreateInfo->pDepthStencilState->front.writeMask;
 +         dynamic->stencil_write_mask.back =
 +            pCreateInfo->pDepthStencilState->back.writeMask;
 +      }
 +
 +      if (states & (1 << VK_DYNAMIC_STATE_STENCIL_REFERENCE)) {
 +         assert(pCreateInfo->pDepthStencilState);
 +         dynamic->stencil_reference.front =
 +            pCreateInfo->pDepthStencilState->front.reference;
 +         dynamic->stencil_reference.back =
 +            pCreateInfo->pDepthStencilState->back.reference;
 +      }
 +   }
 +
 +   pipeline->dynamic_state_mask = states;
 +}
 +
 +static void
 +anv_pipeline_validate_create_info(const VkGraphicsPipelineCreateInfo *info)
 +{
 +   struct anv_render_pass *renderpass = NULL;
 +   struct anv_subpass *subpass = NULL;
 +
 +   /* Assert that all required members of VkGraphicsPipelineCreateInfo are
 +    * present, as explained by the Vulkan (20 Oct 2015, git-aa308cb), Section
 +    * 4.2 Graphics Pipeline.
 +    */
 +   assert(info->sType == VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO);
 +
 +   renderpass = anv_render_pass_from_handle(info->renderPass);
 +   assert(renderpass);
 +
 +   if (renderpass != &anv_meta_dummy_renderpass) {
 +      assert(info->subpass < renderpass->subpass_count);
 +      subpass = &renderpass->subpasses[info->subpass];
 +   }
 +
 +   assert(info->stageCount >= 1);
 +   assert(info->pVertexInputState);
 +   assert(info->pInputAssemblyState);
 +   assert(info->pViewportState);
 +   assert(info->pRasterizationState);
 +
 +   if (subpass && subpass->depth_stencil_attachment != VK_ATTACHMENT_UNUSED)
 +      assert(info->pDepthStencilState);
 +
 +   if (subpass && subpass->color_count > 0)
 +      assert(info->pColorBlendState);
 +
 +   for (uint32_t i = 0; i < info->stageCount; ++i) {
 +      switch (info->pStages[i].stage) {
 +      case VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT:
 +      case VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT:
 +         assert(info->pTessellationState);
 +         break;
 +      default:
 +         break;
 +      }
 +   }
 +}
 +
 +VkResult
 +anv_pipeline_init(struct anv_pipeline *pipeline, struct anv_device *device,
 +                  const VkGraphicsPipelineCreateInfo *pCreateInfo,
 +                  const struct anv_graphics_pipeline_create_info *extra,
 +                  const VkAllocationCallbacks *alloc)
 +{
 +   anv_validate {
 +      anv_pipeline_validate_create_info(pCreateInfo);
 +   }
 +
 +   if (alloc == NULL)
 +      alloc = &device->alloc;
 +
 +   pipeline->device = device;
 +   pipeline->layout = anv_pipeline_layout_from_handle(pCreateInfo->layout);
 +
 +   anv_reloc_list_init(&pipeline->batch_relocs, alloc);
 +   /* TODO: Handle allocation fail */
 +
 +   pipeline->batch.alloc = alloc;
 +   pipeline->batch.next = pipeline->batch.start = pipeline->batch_data;
 +   pipeline->batch.end = pipeline->batch.start + sizeof(pipeline->batch_data);
 +   pipeline->batch.relocs = &pipeline->batch_relocs;
 +
 +   anv_state_stream_init(&pipeline->program_stream,
 +                         &device->instruction_block_pool);
 +
 +   anv_pipeline_init_dynamic_state(pipeline, pCreateInfo);
 +
 +   if (pCreateInfo->pTessellationState)
 +      anv_finishme("VK_STRUCTURE_TYPE_PIPELINE_TESSELLATION_STATE_CREATE_INFO");
 +   if (pCreateInfo->pMultisampleState &&
 +       pCreateInfo->pMultisampleState->rasterizationSamples > 1)
 +      anv_finishme("VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO");
 +
 +   pipeline->use_repclear = extra && extra->use_repclear;
 +   pipeline->writes_point_size = false;
 +
 +   /* When we free the pipeline, we detect stages based on the NULL status
 +    * of various prog_data pointers.  Make them NULL by default.
 +    */
 +   memset(pipeline->prog_data, 0, sizeof(pipeline->prog_data));
 +   memset(pipeline->scratch_start, 0, sizeof(pipeline->scratch_start));
 +
 +   pipeline->vs_simd8 = NO_KERNEL;
 +   pipeline->vs_vec4 = NO_KERNEL;
 +   pipeline->gs_kernel = NO_KERNEL;
 +
 +   pipeline->active_stages = 0;
 +   pipeline->total_scratch = 0;
 +
 +   for (uint32_t i = 0; i < pCreateInfo->stageCount; i++) {
 +      ANV_FROM_HANDLE(anv_shader_module, module,
 +                      pCreateInfo->pStages[i].module);
 +      const char *entrypoint = pCreateInfo->pStages[i].pName;
 +
 +      switch (pCreateInfo->pStages[i].stage) {
 +      case VK_SHADER_STAGE_VERTEX_BIT:
 +         anv_pipeline_compile_vs(pipeline, pCreateInfo, module, entrypoint);
 +         break;
 +      case VK_SHADER_STAGE_GEOMETRY_BIT:
 +         anv_pipeline_compile_gs(pipeline, pCreateInfo, module, entrypoint);
 +         break;
 +      case VK_SHADER_STAGE_FRAGMENT_BIT:
 +         anv_pipeline_compile_fs(pipeline, pCreateInfo, module, entrypoint);
 +         break;
 +      default:
 +         anv_finishme("Unsupported shader stage");
 +      }
 +   }
 +
 +   if (!(pipeline->active_stages & VK_SHADER_STAGE_VERTEX_BIT)) {
 +      /* Vertex is only optional if disable_vs is set */
 +      assert(extra->disable_vs);
 +      memset(&pipeline->vs_prog_data, 0, sizeof(pipeline->vs_prog_data));
 +   }
 +
 +   gen7_compute_urb_partition(pipeline);
 +
 +   const VkPipelineVertexInputStateCreateInfo *vi_info =
 +      pCreateInfo->pVertexInputState;
 +   pipeline->vb_used = 0;
 +   for (uint32_t i = 0; i < vi_info->vertexBindingDescriptionCount; i++) {
 +      const VkVertexInputBindingDescription *desc =
 +         &vi_info->pVertexBindingDescriptions[i];
 +
 +      pipeline->vb_used |= 1 << desc->binding;
 +      pipeline->binding_stride[desc->binding] = desc->stride;
 +
 +      /* Step rate is programmed per vertex element (attribute), not
 +       * binding. Set up a map of which bindings step per instance, for
 +       * reference by vertex element setup. */
 +      switch (desc->inputRate) {
 +      default:
 +      case VK_VERTEX_INPUT_RATE_VERTEX:
 +         pipeline->instancing_enable[desc->binding] = false;
 +         break;
 +      case VK_VERTEX_INPUT_RATE_INSTANCE:
 +         pipeline->instancing_enable[desc->binding] = true;
 +         break;
 +      }
 +   }
 +
 +   const VkPipelineInputAssemblyStateCreateInfo *ia_info =
 +      pCreateInfo->pInputAssemblyState;
 +   pipeline->primitive_restart = ia_info->primitiveRestartEnable;
 +   pipeline->topology = vk_to_gen_primitive_type[ia_info->topology];
 +
 +   if (extra && extra->use_rectlist)
 +      pipeline->topology = _3DPRIM_RECTLIST;
 +
 +   return VK_SUCCESS;
 +}
 +
 +VkResult
 +anv_graphics_pipeline_create(
 +   VkDevice _device,
 +   const VkGraphicsPipelineCreateInfo *pCreateInfo,
 +   const struct anv_graphics_pipeline_create_info *extra,
 +   const VkAllocationCallbacks *pAllocator,
 +   VkPipeline *pPipeline)
 +{
 +   ANV_FROM_HANDLE(anv_device, device, _device);
 +
 +   switch (device->info.gen) {
 +   case 7:
 +      if (device->info.is_haswell)
 +         return gen75_graphics_pipeline_create(_device, pCreateInfo, extra, pAllocator, pPipeline);
 +      else
 +         return gen7_graphics_pipeline_create(_device, pCreateInfo, extra, pAllocator, pPipeline);
 +   case 8:
 +      return gen8_graphics_pipeline_create(_device, pCreateInfo, extra, pAllocator, pPipeline);
 +   case 9:
 +      return gen9_graphics_pipeline_create(_device, pCreateInfo, extra, pAllocator, pPipeline);
 +   default:
 +      unreachable("unsupported gen\n");
 +   }
 +}
 +
 +VkResult anv_CreateGraphicsPipelines(
 +    VkDevice                                    _device,
 +    VkPipelineCache                             pipelineCache,
 +    uint32_t                                    count,
 +    const VkGraphicsPipelineCreateInfo*         pCreateInfos,
 +    const VkAllocationCallbacks*                pAllocator,
 +    VkPipeline*                                 pPipelines)
 +{
 +   VkResult result = VK_SUCCESS;
 +
 +   unsigned i = 0;
 +   for (; i < count; i++) {
 +      result = anv_graphics_pipeline_create(_device, &pCreateInfos[i],
 +                                            NULL, pAllocator, &pPipelines[i]);
 +      if (result != VK_SUCCESS) {
 +         for (unsigned j = 0; j < i; j++) {
 +            anv_DestroyPipeline(_device, pPipelines[j], pAllocator);
 +         }
 +
 +         return result;
 +      }
 +   }
 +
 +   return VK_SUCCESS;
 +}
 +
 +static VkResult anv_compute_pipeline_create(
 +    VkDevice                                    _device,
 +    const VkComputePipelineCreateInfo*          pCreateInfo,
 +    const VkAllocationCallbacks*                pAllocator,
 +    VkPipeline*                                 pPipeline)
 +{
 +   ANV_FROM_HANDLE(anv_device, device, _device);
 +
 +   switch (device->info.gen) {
 +   case 7:
 +      if (device->info.is_haswell)
 +         return gen75_compute_pipeline_create(_device, pCreateInfo, pAllocator, pPipeline);
 +      else
 +         return gen7_compute_pipeline_create(_device, pCreateInfo, pAllocator, pPipeline);
 +   case 8:
 +      return gen8_compute_pipeline_create(_device, pCreateInfo, pAllocator, pPipeline);
 +   case 9:
 +      return gen9_compute_pipeline_create(_device, pCreateInfo, pAllocator, pPipeline);
 +   default:
 +      unreachable("unsupported gen\n");
 +   }
 +}
 +
 +VkResult anv_CreateComputePipelines(
 +    VkDevice                                    _device,
 +    VkPipelineCache                             pipelineCache,
 +    uint32_t                                    count,
 +    const VkComputePipelineCreateInfo*          pCreateInfos,
 +    const VkAllocationCallbacks*                pAllocator,
 +    VkPipeline*                                 pPipelines)
 +{
 +   VkResult result = VK_SUCCESS;
 +
 +   unsigned i = 0;
 +   for (; i < count; i++) {
 +      result = anv_compute_pipeline_create(_device, &pCreateInfos[i],
 +                                           pAllocator, &pPipelines[i]);
 +      if (result != VK_SUCCESS) {
 +         for (unsigned j = 0; j < i; j++) {
 +            anv_DestroyPipeline(_device, pPipelines[j], pAllocator);
 +         }
 +
 +         return result;
 +      }
 +   }
 +
 +   return VK_SUCCESS;
 +}