[Mesa-dev] [v3 1/4] glsl: add ir_cache class for IR serialization (WIP)

Tapani Pälli tapani.palli at intel.com
Sun Sep 29 23:14:56 PDT 2013


Patch introduces ir_cache class that can serialize a gl_shader
to memory with help of memory_writer class and also unserialize
it back with help of memory_map class.

This can be used by the shader compiler to cache individual shaders
and skip lexing, parsing, type checking, AST->IR generation and
optimization rounds that happen during compilation.

v2: fix builtin function handling in read_ir_function(), now
generated IR is identical with original

v3: piglit crash fixes, dump more data

Known issues / bugs / missing implementation:

- constant structures not supported
- interfaces not supported
- no support for geometry shader emit_vertex, emit_primitive
- write/read file i/o likely not portable to some architectures

TODO

- try to use tpl or other library for serialization
- think of ways to reorganize ir_* structure contents for
  faster data dumping/loading
- save also process name if possible, this would help when
  filing bugs and debugging

Signed-off-by: Tapani Pälli <tapani.palli at intel.com>
---
 src/glsl/Makefile.sources         |    2 +
 src/glsl/ir_cache.h               |  627 ++++++++++++++++++++++
 src/glsl/ir_cache_serialize.cpp   |  679 ++++++++++++++++++++++++
 src/glsl/ir_cache_unserialize.cpp | 1054 +++++++++++++++++++++++++++++++++++++
 4 files changed, 2362 insertions(+)
 create mode 100644 src/glsl/ir_cache.h
 create mode 100644 src/glsl/ir_cache_serialize.cpp
 create mode 100644 src/glsl/ir_cache_unserialize.cpp

diff --git a/src/glsl/Makefile.sources b/src/glsl/Makefile.sources
index 2f7bfa1..1a3e72e 100644
--- a/src/glsl/Makefile.sources
+++ b/src/glsl/Makefile.sources
@@ -30,6 +30,8 @@ LIBGLSL_FILES = \
 	$(GLSL_SRCDIR)/hir_field_selection.cpp \
 	$(GLSL_SRCDIR)/ir_basic_block.cpp \
 	$(GLSL_SRCDIR)/ir_builder.cpp \
+	$(GLSL_SRCDIR)/ir_cache_serialize.cpp \
+	$(GLSL_SRCDIR)/ir_cache_unserialize.cpp \
 	$(GLSL_SRCDIR)/ir_clone.cpp \
 	$(GLSL_SRCDIR)/ir_constant_expression.cpp \
 	$(GLSL_SRCDIR)/ir.cpp \
diff --git a/src/glsl/ir_cache.h b/src/glsl/ir_cache.h
new file mode 100644
index 0000000..f579195
--- /dev/null
+++ b/src/glsl/ir_cache.h
@@ -0,0 +1,627 @@
+/* -*- c++ -*- */
+/*
+ * Copyright © 2013 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.
+ */
+
+#pragma once
+#ifndef IR_CACHE_H
+#define IR_CACHE_H
+
+#include <fcntl.h>
+#include <unistd.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+
+#include "ir.h"
+#include "main/imports.h"
+#include "glsl_parser_extras.h"
+#include "program/hash_table.h"
+
+extern "C" {
+#include "program/symbol_table.h"
+}
+
+//#define SHADER_CACHE_DEBUG
+#ifdef SHADER_CACHE_DEBUG
+#define CACHE_DEBUG(fmt, args...) printf(fmt, ## args)
+#else
+#define CACHE_DEBUG(fmt, args...) do {} while (0)
+#endif
+
+
+
+/**
+ * helper class for writing data to memory
+ */
+struct memory_writer
+{
+public:
+   memory_writer() :
+      memory(NULL),
+      memory_p(NULL),
+      curr_size(0),
+      pos(0)
+   { }
+
+   /* NOTE - there is no dtor to free memory, caller is responsible */
+   void free_memory()
+   {
+      if (memory)
+         free(memory);
+   }
+
+   /* realloc more memory */
+   int grow(int32_t size)
+   {
+      char *more_mem = (char *) realloc(memory, curr_size + size);
+      if (more_mem == NULL) {
+         return -1;
+      } else {
+         memory = more_mem;
+         memory_p = memory + pos;
+         curr_size += size;
+         return 0;
+      }
+   }
+
+   /* convenience function for the write() below */
+   template <typename T>
+   int write(T *val, int32_t offset = -1)
+   {
+      return write(val, sizeof(*val), 1, offset);
+   }
+
+   int write(const void *data, int32_t size, int32_t nmemb,
+      int32_t offset = -1)
+   {
+      int32_t amount = size * nmemb;
+      int32_t extra = 8192;
+
+      /* reallocate more if does not fix to current memory */
+      if (!memory || pos > (int32_t)(curr_size - amount))
+         if (grow(amount + extra))
+            return -1;
+
+      /**
+       * by default data is written to pos memory_p, however
+       * if an offset value >= 0 is passed then that value is
+       * used as offset from start of the memory blob
+       */
+      char *dst = memory_p;
+
+      if (offset > -1)
+         dst = memory + offset;
+
+      memcpy(dst, data, amount);
+
+      /* if no offset given, forward the pointer */
+      if (offset == -1) {
+         memory_p += amount;
+         pos += amount;
+      }
+      return 0;
+   }
+
+   inline int32_t position() { return pos; }
+   inline char *mem() { return memory; }
+
+private:
+   char *memory;
+   char *memory_p;
+   int32_t curr_size;
+   int32_t pos;
+};
+
+
+/**
+ * structures used for instruction serialization/creation
+ */
+struct string_data
+{
+public:
+   string_data() : len(0), data(NULL) {}
+   string_data(const char *cstr) :
+      data(NULL)
+   {
+      if (cstr) {
+         len = strlen(cstr);
+         data = _mesa_strdup(cstr);
+      }
+   }
+
+   ~string_data() {
+      if (data) {
+         free(data);
+         data = NULL;
+      }
+   }
+
+   int serialize(memory_writer &blob)
+   {
+      if (!data)
+         return -1;
+
+      blob.write(&len);
+      blob.write(data, 1, len);
+      return 0;
+   }
+
+   void set(const char *cstr)
+   {
+      if (data)
+         free(data);
+      data = _mesa_strdup(cstr);
+      len = strlen(cstr);
+   }
+
+   uint32_t len;
+   char *data;
+
+};
+
+
+/**
+ * data required to serialize glsl_type
+ */
+struct glsl_type_data
+{
+public:
+   glsl_type_data() :
+      name(NULL),
+      element_type(NULL),
+      field_names(NULL),
+      field_types(NULL) {}
+
+   glsl_type_data(const glsl_type *t) :
+      base_type(t->base_type),
+      length(t->length),
+      vector_elms(t->vector_elements),
+      matrix_cols(t->matrix_columns),
+      sampler_dimensionality(t->sampler_dimensionality),
+      sampler_shadow(t->sampler_shadow),
+      sampler_array(t->sampler_array),
+      sampler_type(t->sampler_type),
+      interface_packing(t->interface_packing),
+      element_type(NULL),
+      field_names(NULL),
+      field_types(NULL)
+   {
+      name = new string_data(t->name);
+
+      /* for array, save element type information */
+      if (t->base_type == GLSL_TYPE_ARRAY)
+         element_type =
+            new glsl_type_data(t->element_type());
+
+      /* with structs, copy each struct field name + type */
+      else if (t->base_type == GLSL_TYPE_STRUCT) {
+
+         field_names = new string_data[t->length];
+         field_types = new glsl_type_data*[t->length];
+
+         glsl_struct_field *field = t->fields.structure;
+         glsl_type_data **field_t = field_types;
+         for (unsigned k = 0; k < t->length; k++, field++, field_t++) {
+            field_names[k].set(field->name);
+            *field_t = new glsl_type_data(field->type);
+         }
+      }
+
+      else if (t->base_type == GLSL_TYPE_SAMPLER) {
+         /* TODO - figure out if we need to dump more data than right now */
+      }
+
+   }
+
+   ~glsl_type_data() {
+      delete name;
+      delete element_type;
+      delete [] field_names;
+      if (field_types) {
+          struct glsl_type_data **data = field_types;
+          for (int k = 0; k < length; k++, data++)
+             delete *data;
+          delete [] field_types;
+      }
+   }
+
+   int serialize(memory_writer &blob)
+   {
+      long ir_len = 666;
+      blob.write(&name->len);
+      blob.write(name->data, 1, name->len);
+
+      int32_t start_pos = blob.position();
+      blob.write(&ir_len);
+
+      blob.write(this, sizeof(*this), 1);
+
+      if (base_type == GLSL_TYPE_ARRAY)
+         element_type->serialize(blob);
+      else if (base_type == GLSL_TYPE_STRUCT) {
+         struct string_data *data = field_names;
+         glsl_type_data **field_t = field_types;
+         for (int k = 0; k < length; k++, data++, field_t++) {
+            blob.write(&data->len);
+            blob.write(data->data, 1, data->len);
+            (*field_t)->serialize(blob);
+         }
+      }
+
+      ir_len = blob.position() - start_pos - sizeof(ir_len);
+      blob.write(&ir_len, start_pos);
+      return 0;
+   }
+
+   int32_t base_type;
+   int32_t length;
+   int32_t vector_elms;
+   int32_t matrix_cols;
+
+   uint32_t sampler_dimensionality;
+   uint32_t sampler_shadow;
+   uint32_t sampler_array;
+   uint32_t sampler_type;
+
+   uint32_t interface_packing;
+
+   struct string_data *name;
+
+   /* array element type */
+   struct glsl_type_data *element_type;
+
+   /* structure fields */
+   struct string_data *field_names;
+   struct glsl_type_data **field_types;
+};
+
+
+/* helper to create a unique id from a ir_variable address */
+static uint32_t _unique_id(ir_variable *var)
+{
+   char buffer[256];
+   _mesa_snprintf(buffer, 256, "%s_%p", var->name, var);
+   return _mesa_str_checksum(buffer);
+}
+
+
+struct ir_variable_data
+{
+public:
+   ir_variable_data() :
+      type(NULL),
+      name(NULL),
+      unique_name(NULL),
+      state_slots(NULL) {}
+
+   ir_variable_data(ir_variable *ir) :
+      unique_id(_unique_id(ir)),
+      max_array_access(ir->max_array_access),
+      ir_type(ir->ir_type),
+      mode(ir->mode),
+      location(ir->location),
+      read_only(ir->read_only),
+      centroid(ir->centroid),
+      invariant(ir->invariant),
+      interpolation(ir->interpolation),
+      origin_upper_left(ir->origin_upper_left),
+      pixel_center_integer(ir->pixel_center_integer),
+      explicit_location(ir->explicit_location),
+      explicit_index(ir->explicit_index),
+      explicit_binding(ir->explicit_binding),
+      depth_layout(ir->depth_layout),
+      num_state_slots(ir->num_state_slots),
+      has_constant_value(ir->constant_value ? 1 : 0),
+      has_constant_initializer(ir->constant_initializer ? 1 : 0)
+   {
+      name = new string_data(ir->name);
+
+      /* name can be NULL, see ir_print_visitor for explanation */
+      if (!ir->name)
+         name->set("parameter");
+
+      char uniq[256];
+      _mesa_snprintf(uniq, 256, "%s_%d", name->data, unique_id);
+      unique_name = new string_data(uniq);
+      type = new glsl_type_data(ir->type);
+
+      state_slots = (ir_state_slot *) malloc
+         (ir->num_state_slots * sizeof(ir->state_slots[0]));
+      memcpy(state_slots, ir->state_slots,
+         ir->num_state_slots * sizeof(ir->state_slots[0]));
+   }
+
+   ~ir_variable_data()
+   {
+      delete name;
+      delete unique_name;
+      delete type;
+
+      if (state_slots)
+         free(state_slots);
+   }
+
+   int serialize(memory_writer &blob)
+   {
+      type->serialize(blob);
+      name->serialize(blob);
+      unique_name->serialize(blob);
+      blob.write(this, sizeof(*this), 1);
+
+      for (unsigned i = 0; i < num_state_slots; i++) {
+         blob.write(&state_slots[i].swizzle);
+         for (unsigned j = 0; j < 5; j++) {
+            blob.write(&state_slots[i].tokens[j]);
+         }
+      }
+      return 0;
+   }
+
+   struct glsl_type_data *type;
+   struct string_data *name;
+   struct string_data *unique_name;
+
+   uint32_t unique_id;
+
+   uint32_t max_array_access;
+   int32_t ir_type;
+   int32_t mode;
+   int32_t location;
+   uint32_t read_only;
+   uint32_t centroid;
+   uint32_t invariant;
+   uint32_t interpolation;
+   uint32_t origin_upper_left;
+   uint32_t pixel_center_integer;
+   uint32_t explicit_location;
+   uint32_t explicit_index;
+   uint32_t explicit_binding;
+   int32_t depth_layout;
+
+   uint32_t num_state_slots;
+   struct ir_state_slot *state_slots;
+
+   uint32_t has_constant_value;
+   uint32_t has_constant_initializer;
+};
+
+
+/**
+ * helper class to read instructions
+ */
+struct memory_map
+{
+public:
+   memory_map() :
+      fd(0),
+      cache_size(0),
+      cache_mmap(NULL),
+      cache_mmap_p(NULL) { }
+
+   int map(const char *path)
+   {
+      struct stat stat_info;
+      if (stat(path, &stat_info) != 0)
+         return -1;
+
+      cache_size = stat_info.st_size;
+
+      fd = open(path, O_RDONLY);
+      if (fd) {
+         cache_mmap_p = cache_mmap = (char *)
+            mmap(NULL, cache_size, PROT_READ, MAP_PRIVATE, fd, 0);
+         return (cache_mmap == MAP_FAILED) ? -1 : 0;
+      }
+      return -1;
+   }
+
+   ~memory_map() {
+      if (cache_mmap) {
+         munmap(cache_mmap, cache_size);
+         close(fd);
+      }
+   }
+
+   /* move read pointer forward */
+   inline void ffwd(int len)
+   {
+      cache_mmap_p += len;
+   }
+
+   /* have we reached the end of mapping? */
+   inline bool end()
+   {
+      return (!((cache_mmap_p - cache_mmap) < cache_size));
+   }
+
+   /* template avoids use of typeof() cast */
+   template <typename T>
+   inline void read_value(T *val)
+   {
+      *val = *(T *)cache_mmap_p;
+      ffwd(sizeof(T));
+   }
+
+   template <typename T>
+   inline void read_nval(T *val, uint32_t amount)
+   {
+      memcpy(val, cache_mmap_p, amount * sizeof(val[0]));
+      ffwd(amount * sizeof(val[0]));
+   }
+
+   inline void read_string(char *str)
+   {
+      uint32_t len;
+      read_value(&len);
+      memcpy(str, cache_mmap_p, len);
+      str[len] = '\0';
+      ffwd(len);
+   }
+
+   template <typename T>
+   inline void read_struct(T *s)
+   {
+      memcpy(s, cache_mmap_p, sizeof(*s));
+      ffwd(sizeof(*s));
+   }
+
+private:
+
+   int32_t fd;
+   int32_t cache_size;
+   char *cache_mmap;
+   char *cache_mmap_p;
+};
+
+
+struct ir_cache
+{
+public:
+   ir_cache(bool prototypes = false) :
+      prototypes_only(prototypes)
+   {
+      var_ht = hash_table_ctor(0, hash_table_string_hash,
+         hash_table_string_compare);
+   }
+
+   ~ir_cache()
+   {
+      hash_table_call_foreach(this->var_ht, delete_key, NULL);
+      hash_table_dtor(this->var_ht);
+   }
+
+   /* serialize gl_shader to memory  */
+   char *serialize(struct gl_shader *shader,
+      struct _mesa_glsl_parse_state *state,
+      const char *mesa_sha, size_t *size);
+
+   /* unserialize gl_shader from mapped memory */
+   struct gl_shader *unserialize(void *mem_ctx, memory_map &map,
+      struct _mesa_glsl_parse_state *state,
+      const char *mesa_sha,
+      int *error_code);
+
+   enum cache_error {
+      GENERAL_READ_ERROR = -1,
+      DIFFERENT_MESA_SHA = -2,
+   };
+
+private:
+
+   /* variables and methods required for serialization */
+
+   memory_writer blob;
+
+   bool prototypes_only;
+
+   /**
+    * writes ir_type and instruction dump size as a 'header'
+    * for each instruction before calling save_ir
+    */
+   int save(ir_instruction *ir);
+
+   int save_ir(ir_variable *ir);
+   int save_ir(ir_assignment *ir);
+   int save_ir(ir_call *ir);
+   int save_ir(ir_constant *ir);
+   int save_ir(ir_dereference_array *ir);
+   int save_ir(ir_dereference_record *ir);
+   int save_ir(ir_dereference_variable *ir);
+   int save_ir(ir_discard *ir);
+   int save_ir(ir_expression *ir);
+   int save_ir(ir_function *ir);
+   int save_ir(ir_function_signature *ir);
+   int save_ir(ir_if *ir);
+   int save_ir(ir_loop *ir);
+   int save_ir(ir_loop_jump *ir);
+   int save_ir(ir_return *ir);
+   int save_ir(ir_swizzle *ir);
+   int save_ir(ir_emit_vertex *ir);
+   int save_ir(ir_end_primitive *ir);
+
+
+   /* variables and methods required for unserialization */
+
+   struct _mesa_glsl_parse_state *state;
+   void *mem_ctx;
+
+   struct exec_list *top_level;
+   struct exec_list *prototypes;
+   struct exec_list *current_function;
+
+   int read_header(struct gl_shader *shader, memory_map &map,
+      const char *mesa_sha);
+   int read_prototypes(memory_map &map);
+
+   int read_instruction(struct exec_list *list, memory_map &map,
+      bool ignore = false);
+
+   int read_ir_variable(struct exec_list *list, memory_map &map);
+   int read_ir_assignment(struct exec_list *list, memory_map &map);
+   int read_ir_function(struct exec_list *list, memory_map &map);
+   int read_ir_if(struct exec_list *list, memory_map &map);
+   int read_ir_return(struct exec_list *list, memory_map &map);
+   int read_ir_call(struct exec_list *list, memory_map &map);
+   int read_ir_discard(struct exec_list *list, memory_map &map);
+   int read_ir_loop(struct exec_list *list, memory_map &map);
+   int read_ir_loop_jump(struct exec_list *list, memory_map &map);
+
+   /* rvalue readers */
+   ir_rvalue *read_ir_rvalue(memory_map &map);
+   ir_constant *read_ir_constant(memory_map &map);
+   ir_swizzle *read_ir_swizzle(memory_map &map);
+   ir_expression *read_ir_expression(memory_map &map);
+   ir_dereference_array *read_ir_dereference_array(memory_map &map);
+   ir_dereference_record *read_ir_dereference_record(memory_map &map);
+   ir_dereference_variable *read_ir_dereference_variable(memory_map &map);
+
+   const glsl_type *read_glsl_type(memory_map &map);
+
+   /**
+    * var_ht is used to store created ir_variables with a unique_key for
+    * each so that ir_dereference_variable creation can find the variable
+    */
+   struct hash_table *var_ht;
+
+   /**
+    * these 2 functions are ~copy-pasta from string_to_uint_map,
+    * we need a hash here with a string key and pointer value
+    */
+   void hash_store(void * value, const char *key)
+   {
+      char *dup_key = strdup(key);
+      bool result = hash_table_replace(this->var_ht, value, dup_key);
+      if (result)
+         free(dup_key);
+   }
+
+   static void delete_key(const void *key, void *data, void *closure)
+   {
+      (void) data;
+      (void) closure;
+      free((char *)key);
+   }
+
+};
+
+#endif /* IR_CACHE_H */
diff --git a/src/glsl/ir_cache_serialize.cpp b/src/glsl/ir_cache_serialize.cpp
new file mode 100644
index 0000000..b060f6f
--- /dev/null
+++ b/src/glsl/ir_cache_serialize.cpp
@@ -0,0 +1,679 @@
+/* -*- c++ -*- */
+/*
+ * Copyright © 2013 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 "ir_cache.h"
+
+
+inline static int _write_string(const char *str, memory_writer &blob)
+{
+   uint32_t len = strlen(str);
+
+   blob.write(&len);
+   blob.write(str, 1, len);
+
+   return 0;
+}
+
+
+int ir_cache::save_ir(ir_variable *ir)
+{
+   ir_variable_data *data = new ir_variable_data(ir);
+
+   data->serialize(blob);
+
+   CACHE_DEBUG("save ir_variable [%s] id %d\n",
+      data->name->data, data->unique_id);
+
+   delete data;
+
+   if (ir->is_interface_instance()) {
+      CACHE_DEBUG("sorry, interfaces not supported\n");
+      return -1;
+   }
+
+   if (ir->constant_value)
+      if (save(ir->constant_value))
+         return -1;
+
+   if (ir->constant_initializer)
+      if (save(ir->constant_initializer))
+         return -1;
+
+   return 0;
+}
+
+
+int ir_cache::save_ir(ir_dereference_array *ir)
+{
+   blob.write(&ir->array->ir_type);
+
+   save(ir->array);
+
+   blob.write(&ir->array_index->ir_type);
+
+   return save(ir->array_index);
+}
+
+
+int ir_cache::save_ir(ir_dereference_record *ir)
+{
+   _write_string(ir->field, blob);
+
+   blob.write(&ir->record->ir_type);
+
+   return save(ir->record);
+}
+
+
+int ir_cache::save_ir(ir_dereference_variable *ir)
+{
+   _write_string(ir->var->name, blob);
+   uint32_t unique_id = _unique_id(ir->var);
+   blob.write(&unique_id);
+
+   CACHE_DEBUG("save ir_dereference_variable [%s] id %d\n",
+      ir->var->name, unique_id);
+
+   return 0;
+}
+
+
+int ir_cache::save_ir(ir_constant *ir)
+{
+   glsl_type_data *data = new glsl_type_data(ir->type);
+   data->serialize(blob);
+
+   blob.write(&ir->value, sizeof(ir_constant_data), 1);
+
+   delete data;
+
+   if (ir->array_elements) {
+      for (unsigned i = 0; i < ir->type->length; i++)
+         if (save(ir->array_elements[i]))
+            return -1;
+   }
+
+   /* FIXME, support missing for structs */
+   if (!ir->components.is_empty()) {
+      CACHE_DEBUG("unsupported ir_constant type (struct)\n");
+      return -1;
+   }
+   return 0;
+}
+
+
+int ir_cache::save_ir(ir_expression *ir)
+{
+   glsl_type_data *data = new glsl_type_data(ir->type);
+   int32_t num_operands = ir->get_num_operands();
+
+   data->serialize(blob);
+   delete data;
+
+   blob.write(&ir->operation);
+   blob.write(&num_operands);
+
+   /* operand ir_type below is written to make parsing easier */
+   for (unsigned i = 0; i < ir->get_num_operands(); i++) {
+      blob.write(&ir->operands[i]->ir_type);
+      if (save(ir->operands[i]))
+         return -1;
+   }
+   return 0;
+}
+
+
+int ir_cache::save_ir(ir_function *ir)
+{
+   uint32_t sig_amount = 0;
+
+   foreach_iter(exec_list_iterator, iter, *ir)
+      sig_amount++;
+
+   _write_string(ir->name, blob);
+   blob.write(&sig_amount);
+
+   CACHE_DEBUG("save ir_function [%s], %d sigs\n", ir->name, sig_amount);
+
+   foreach_iter(exec_list_iterator, iter, *ir) {
+      ir_function_signature *const sig = (ir_function_signature *) iter.get();
+      if (save(sig))
+         return -1;
+   }
+
+   return 0;
+}
+
+
+int ir_cache::save_ir(ir_function_signature *ir)
+{
+   int32_t par_count = 0;
+   int32_t body_size = 0;
+   uint32_t is_builtin = ir->is_builtin();
+
+   foreach_iter(exec_list_iterator, iter, ir->parameters)
+      par_count++;
+
+   foreach_iter(exec_list_iterator, iter, ir->body)
+      body_size++;
+
+   CACHE_DEBUG("signature (%s), returns %d, params %d size %d (builtin %d)\n",
+      ir->function_name(), ir->return_type->base_type, par_count, body_size,
+      is_builtin);
+
+   blob.write(&par_count);
+   blob.write(&body_size);
+   blob.write(&is_builtin);
+
+   /* dump the return type of function */
+   glsl_type_data *data = new glsl_type_data(ir->return_type);
+   data->serialize(blob);
+   delete data;
+
+   /* function parameters */
+   foreach_iter(exec_list_iterator, iter, ir->parameters) {
+      ir_variable *const inst = (ir_variable *) iter.get();
+      CACHE_DEBUG("   parameter %s\n", inst->name);
+      if (save(inst))
+         return -1;
+   }
+
+   if (prototypes_only)
+      return 0;
+
+   /* function body */
+   foreach_iter(exec_list_iterator, iter, ir->body) {
+      ir_instruction *const inst = (ir_instruction *) iter.get();
+      CACHE_DEBUG("   body instruction node type %d\n", inst->ir_type);
+      if (save(inst))
+         return -1;
+   }
+
+   return 0;
+}
+
+
+int ir_cache::save_ir(ir_assignment *ir)
+{
+   uint32_t write_mask = ir->write_mask;
+
+   blob.write(&write_mask);
+
+   /* lhs (ir_deference_*) */
+   blob.write(&ir->lhs->ir_type);
+
+   _write_string(ir->lhs->variable_referenced()->name, blob);
+
+   if (save(ir->lhs))
+      return -1;
+
+   if (ir->condition) {
+      CACHE_DEBUG("%s: assignment has condition, not supported", __func__);
+   }
+
+    /* rhs (constant, expression ...) */
+   blob.write(&ir->rhs->ir_type);
+
+   if (save(ir->rhs))
+      return -1;
+
+   return 0;
+}
+
+
+int ir_cache::save_ir(ir_return *ir)
+{
+   ir_rvalue *const value = ir->get_value();
+   uint32_t has_rvalue = value ? 1 : 0;
+
+   blob.write(&has_rvalue);
+
+   if (has_rvalue) {
+      blob.write(&value->ir_type);
+      return save(value);
+   }
+   return 0;
+}
+
+
+int ir_cache::save_ir(ir_swizzle *ir)
+{
+   uint32_t components = ir->mask.num_components;
+   const uint32_t mask[4] = {
+      ir->mask.x,
+      ir->mask.y,
+      ir->mask.z,
+      ir->mask.w
+   };
+
+   blob.write(&components);
+   blob.write(&mask, sizeof(mask[0]), 4);
+   blob.write(&ir->val->ir_type);
+
+   return save(ir->val);
+}
+
+
+int ir_cache::save_ir(ir_discard *ir)
+{
+   unsigned has_condition = ir->condition ? 1 : 0;
+   blob.write(&has_condition);
+
+   /* TODO - figure out what condition on discard means */
+   if (ir->condition != NULL) {
+      CACHE_DEBUG("%s: error, there is no cond support here yet...\n",
+         __func__);
+      if (save(ir->condition))
+         return -1;
+   }
+
+   return 0;
+}
+
+
+int ir_cache::save_ir(ir_call *ir)
+{
+   _write_string(ir->callee_name(), blob);
+
+   uint32_t has_return_deref = ir->return_deref ? 1 : 0;
+   uint32_t list_len = 0;
+   uint32_t use_builtin = ir->use_builtin;
+
+   blob.write(&has_return_deref);
+
+   if (ir->return_deref)
+      if (save(ir->return_deref))
+         return -1;
+
+   /* call parameter list */
+   foreach_iter(exec_list_iterator, iter, *ir)
+      list_len++;
+
+   blob.write(&list_len);
+
+   foreach_iter(exec_list_iterator, iter, *ir) {
+      ir_instruction *const inst = (ir_instruction *) iter.get();
+
+      int32_t ir_type = inst->ir_type;
+      blob.write(&ir_type);
+
+      if (save(inst))
+         return -1;
+   }
+
+   blob.write(&use_builtin);
+
+   return 0;
+}
+
+
+int ir_cache::save_ir(ir_if *ir)
+{
+   uint32_t then_len = 0, else_len = 0;
+
+   /* then and else branch lengths */
+   foreach_iter(exec_list_iterator, iter, ir->then_instructions)
+      then_len++;
+   foreach_iter(exec_list_iterator, iter, ir->else_instructions)
+      else_len++;
+
+   blob.write(&then_len);
+   blob.write(&else_len);
+   blob.write(&ir->condition->ir_type);
+
+   CACHE_DEBUG("dump ir_if (then %d else %d), condition ir_type %d\n",
+      then_len, else_len, ir->condition->ir_type);
+
+   save(ir->condition);
+
+   /* dump branch instruction lists */
+   foreach_iter(exec_list_iterator, iter, ir->then_instructions) {
+      ir_instruction *const inst = (ir_instruction *) iter.get();
+      CACHE_DEBUG("   ir_if then instruction node type %d\n", inst->ir_type);
+      if (save(inst))
+         return -1;
+   }
+
+   foreach_iter(exec_list_iterator, iter, ir->else_instructions) {
+      ir_instruction *const inst = (ir_instruction *) iter.get();
+      CACHE_DEBUG("   ir_if else instruction node type %d\n", inst->ir_type);
+      if (save(inst))
+         return -1;
+   }
+
+   return 0;
+}
+
+
+int ir_cache::save_ir(ir_loop *ir)
+{
+   uint32_t has_counter = ir->counter ? 1 : 0;
+   uint32_t has_from = ir->from ? 1 : 0;
+   uint32_t has_to = ir->to ? 1 : 0;
+   uint32_t has_incr = ir->increment ? 1 : 0;
+   uint32_t body_size = 0;
+
+   foreach_iter(exec_list_iterator, iter, ir->body_instructions)
+      body_size++;
+
+   blob.write(&has_from);
+   blob.write(&has_to);
+   blob.write(&has_incr);
+   blob.write(&has_counter);
+   blob.write(&ir->cmp);
+   blob.write(&body_size);
+
+   if (has_from) {
+      blob.write(&ir->from->ir_type);
+      if (save(ir->from))
+         return -1;
+   }
+
+   if (has_to) {
+      blob.write(&ir->to->ir_type);
+      if (save(ir->to))
+         return -1;
+   }
+
+   if (has_incr) {
+      blob.write(&ir->increment->ir_type);
+      if (save(ir->increment))
+         return -1;
+   }
+
+   if (has_counter) {
+     _write_string(ir->counter->name, blob);
+     if (save(ir->counter))
+        return -1;
+   }
+
+   foreach_iter(exec_list_iterator, iter, ir->body_instructions) {
+      ir_instruction *const inst = (ir_instruction *) iter.get();
+      CACHE_DEBUG("save loop instruction type %d\n", inst->ir_type);
+      if (save(inst))
+         return -1;
+   }
+
+   return 0;
+}
+
+
+int ir_cache::save_ir(ir_loop_jump *ir)
+{
+   return blob.write(&ir->mode);
+}
+
+
+int ir_cache::save_ir(ir_emit_vertex *ir)
+{
+   return -1;
+}
+
+
+int ir_cache::save_ir(ir_end_primitive *ir)
+{
+   return -1;
+}
+
+
+/**
+ * writes instruction type, packet size and calls
+ * save function for the instruction to save the data
+ */
+int ir_cache::save(ir_instruction *ir)
+{
+   long ir_len = 666;
+
+   blob.write(&ir->ir_type);
+
+   int32_t start_pos = blob.position();
+
+   blob.write(&ir_len);
+
+#define SAVE_IR(type)\
+   if (save_ir(static_cast<type *>(ir))) goto write_errors;
+
+   switch(ir->ir_type) {
+
+   case ir_type_variable:
+      SAVE_IR(ir_variable);
+      break;
+   case ir_type_call:
+      SAVE_IR(ir_call);
+      break;
+   case ir_type_constant:
+      SAVE_IR(ir_constant);
+      break;
+   case ir_type_discard:
+      SAVE_IR(ir_discard);
+      break;
+   case ir_type_expression:
+      SAVE_IR(ir_expression);
+      break;
+   case ir_type_dereference_array:
+      SAVE_IR(ir_dereference_array);
+      break;
+   case ir_type_dereference_record:
+      SAVE_IR(ir_dereference_record);
+      break;
+   case ir_type_dereference_variable:
+      SAVE_IR(ir_dereference_variable);
+      break;
+   case ir_type_function:
+      SAVE_IR(ir_function);
+      break;
+   case ir_type_function_signature:
+      SAVE_IR(ir_function_signature);
+      break;
+   case ir_type_swizzle:
+      SAVE_IR(ir_swizzle);
+      break;
+   case ir_type_assignment:
+      SAVE_IR(ir_assignment);
+      break;
+   case ir_type_if:
+      SAVE_IR(ir_if);
+      break;
+   case ir_type_loop:
+      SAVE_IR(ir_loop);
+      break;
+   case ir_type_loop_jump:
+      SAVE_IR(ir_loop_jump);
+      break;
+   case ir_type_return:
+      SAVE_IR(ir_return);
+      break;
+   case ir_type_emit_vertex:
+      SAVE_IR(ir_emit_vertex);
+      break;
+   case ir_type_end_primitive:
+      SAVE_IR(ir_end_primitive);
+      break;
+
+   default:
+      CACHE_DEBUG("%s: error, type %d not implemented\n",
+         __func__, ir->ir_type);
+      return -1;
+   }
+
+   ir_len = blob.position() - start_pos - sizeof(ir_len);
+
+   blob.write(&ir_len, start_pos);
+
+   return 0;
+
+write_errors:
+   CACHE_DEBUG("%s: write errors (ir type %d)\n", __func__, ir->ir_type);
+   return -1;
+}
+
+
+static void _write_header(gl_shader *shader, const char *mesa_sha,
+   memory_writer &blob)
+{
+   _write_string(mesa_sha, blob);
+   blob.write(&shader->Version);
+   blob.write(&shader->Type);
+   blob.write(&shader->IsES);
+}
+
+
+static void _dump_bool(bool value, memory_writer &blob)
+{
+   uint32_t val = value;
+   blob.write(&val);
+}
+
+/**
+ * some of the state such as extension bits is required from
+ * the preprocessing stage
+ */
+static void _write_state(struct _mesa_glsl_parse_state *state,
+   memory_writer &blob)
+{
+   /* FIXME - currently the set of extensions is hard to dump */
+   _dump_bool(state->ARB_draw_buffers_enable, blob);
+   _dump_bool(state->ARB_draw_buffers_warn, blob);
+   _dump_bool(state->ARB_draw_instanced_enable, blob);
+   _dump_bool(state->ARB_draw_instanced_warn, blob);
+   _dump_bool(state->ARB_explicit_attrib_location_enable, blob);
+   _dump_bool(state->ARB_explicit_attrib_location_warn, blob);
+   _dump_bool(state->ARB_fragment_coord_conventions_enable, blob);
+   _dump_bool(state->ARB_fragment_coord_conventions_warn, blob);
+   _dump_bool(state->ARB_texture_rectangle_enable, blob);
+   _dump_bool(state->ARB_texture_rectangle_warn, blob);
+   _dump_bool(state->EXT_texture_array_enable, blob);
+   _dump_bool(state->EXT_texture_array_warn, blob);
+   _dump_bool(state->ARB_shader_texture_lod_enable, blob);
+   _dump_bool(state->ARB_shader_texture_lod_warn, blob);
+   _dump_bool(state->ARB_shader_stencil_export_enable, blob);
+   _dump_bool(state->ARB_shader_stencil_export_warn, blob);
+   _dump_bool(state->AMD_conservative_depth_enable, blob);
+   _dump_bool(state->AMD_conservative_depth_warn, blob);
+   _dump_bool(state->ARB_conservative_depth_enable, blob);
+   _dump_bool(state->ARB_conservative_depth_warn, blob);
+   _dump_bool(state->AMD_shader_stencil_export_enable, blob);
+   _dump_bool(state->AMD_shader_stencil_export_warn, blob);
+   _dump_bool(state->OES_texture_3D_enable, blob);
+   _dump_bool(state->OES_texture_3D_warn, blob);
+   _dump_bool(state->OES_EGL_image_external_enable, blob);
+   _dump_bool(state->OES_EGL_image_external_warn, blob);
+   _dump_bool(state->ARB_shader_bit_encoding_enable, blob);
+   _dump_bool(state->ARB_shader_bit_encoding_warn, blob);
+   _dump_bool(state->ARB_uniform_buffer_object_enable, blob);
+   _dump_bool(state->ARB_uniform_buffer_object_warn, blob);
+   _dump_bool(state->OES_standard_derivatives_enable, blob);
+   _dump_bool(state->OES_standard_derivatives_warn, blob);
+   _dump_bool(state->ARB_texture_cube_map_array_enable, blob);
+   _dump_bool(state->ARB_texture_cube_map_array_warn, blob);
+   _dump_bool(state->ARB_shading_language_packing_enable, blob);
+   _dump_bool(state->ARB_shading_language_packing_warn, blob);
+   _dump_bool(state->ARB_texture_multisample_enable, blob);
+   _dump_bool(state->ARB_texture_multisample_warn, blob);
+   _dump_bool(state->ARB_texture_query_lod_enable, blob);
+   _dump_bool(state->ARB_texture_query_lod_warn, blob);
+   _dump_bool(state->ARB_gpu_shader5_enable, blob);
+   _dump_bool(state->ARB_gpu_shader5_warn, blob);
+   _dump_bool(state->AMD_vertex_shader_layer_enable, blob);
+   _dump_bool(state->AMD_vertex_shader_layer_warn, blob);
+   _dump_bool(state->ARB_shading_language_420pack_enable, blob);
+   _dump_bool(state->ARB_shading_language_420pack_warn, blob);
+   _dump_bool(state->EXT_shader_integer_mix_enable, blob);
+   _dump_bool(state->EXT_shader_integer_mix_warn, blob);
+}
+
+
+/**
+ * serializes a single gl_shader, writes shader header
+ * information and exec_list of instructions
+ */
+char *ir_cache::serialize(struct gl_shader *shader,
+   struct _mesa_glsl_parse_state *state,
+   const char *mesa_sha, size_t *size)
+{
+   uint32_t total = 0;
+
+   prototypes_only = true;
+
+   *size = 0;
+
+   int32_t start_pos = blob.position();
+   long shader_data_len = 666;
+   blob.write(&shader_data_len);
+
+   _write_header(shader, mesa_sha, blob);
+
+   _write_state(state, blob);
+
+   /* count variables + functions and dump prototypes */
+   foreach_list_const(node, shader->ir) {
+      if (((ir_instruction *) node)->as_variable())
+         total++;
+      if (((ir_instruction *) node)->as_function())
+         total++;
+   }
+
+   blob.write(&total);
+
+   CACHE_DEBUG("write %d prototypes\n", total);
+
+   foreach_list_const(node, shader->ir) {
+      ir_instruction *const inst = (ir_instruction *) node;
+      if (inst->as_variable())
+         if (save(inst))
+            goto write_errors;
+   }
+
+   foreach_list_const(node, shader->ir) {
+      ir_instruction *const inst = (ir_instruction *) node;
+      if (inst->as_function())
+         if (save(inst))
+            goto write_errors;
+   }
+
+   /* all shader instructions */
+   prototypes_only = false;
+   foreach_list_const(node, shader->ir) {
+      ir_instruction *instruction = (ir_instruction *) node;
+      if (save(instruction))
+         goto write_errors;
+   }
+
+   CACHE_DEBUG("cached a shader\n");
+
+   /* how much has been written */
+   *size = blob.position();
+
+   shader_data_len = blob.position() -
+      start_pos - sizeof(shader_data_len);
+   blob.write(&shader_data_len, start_pos);
+
+   return blob.mem();
+
+write_errors:
+
+   blob.free_memory();
+   return NULL;
+}
+
diff --git a/src/glsl/ir_cache_unserialize.cpp b/src/glsl/ir_cache_unserialize.cpp
new file mode 100644
index 0000000..2dae886
--- /dev/null
+++ b/src/glsl/ir_cache_unserialize.cpp
@@ -0,0 +1,1054 @@
+/* -*- c++ -*- */
+/*
+ * Copyright © 2013 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 "ir_cache.h"
+
+
+static ir_variable *search_var(struct exec_list *list,
+   const char *name, bool debug_print = false)
+{
+   foreach_list_safe(node, list) {
+      ir_variable *var = ((ir_instruction *) node)->as_variable();
+      if (var && debug_print) printf("[%s vs %s]\n", name, var->name);
+      if (var && strstr(name, var->name))
+         return var;
+   }
+   return NULL;
+}
+
+
+static ir_function *search_func(struct _mesa_glsl_parse_state *state,
+struct exec_list *list, const char *name, struct exec_list *parameters)
+{
+   foreach_list_safe(node, list) {
+      ir_function *func = ((ir_instruction *) node)->as_function();
+      if (func && strstr(name, func->name) &&
+         func->matching_signature(state, parameters))
+         return func;
+   }
+   return NULL;
+}
+
+
+/**
+ * main purpose of the header is that it validates that cached
+ * shader was produced with the same Mesa drivers
+ */
+int ir_cache::read_header(struct gl_shader *shader, memory_map &map,
+   const char *mesa_sha)
+{
+   char cache_mesa_sha[256];
+
+   map.read_string(cache_mesa_sha);
+
+   map.read_value(&shader->Version);
+   map.read_value(&shader->Type);
+   map.read_value(&shader->IsES);
+
+   CACHE_DEBUG("%s: version %d, type 0x%x, %s (mesa %s)\n",
+      __func__,  shader->Version, shader->Type,
+      (shader->IsES) ? "glsl es" : "desktop glsl",
+      cache_mesa_sha);
+
+   return memcmp(cache_mesa_sha, mesa_sha, strlen(mesa_sha));
+}
+
+
+const glsl_type *ir_cache::read_glsl_type(memory_map &map)
+{
+   char name[256];
+   long type_size;
+
+   map.read_string(name);
+   map.read_value(&type_size);
+
+   const glsl_type *type_exists = state->symbols->get_type(name);
+
+   /* if type exists, move read pointer forward and return type */
+   if (type_exists) {
+      map.ffwd(type_size);
+      return type_exists;
+   }
+
+   glsl_type_data data;
+
+   map.read_struct(&data);
+
+   data.name = NULL;
+   data.element_type = NULL;
+   data.field_names = NULL;
+   data.field_types = NULL;
+
+   if (data.base_type == GLSL_TYPE_SAMPLER) {
+
+      switch(data.sampler_dimensionality) {
+      case 0:
+         return glsl_type::sampler1D_type;
+      case 1:
+         return glsl_type::sampler2D_type;
+      case 2:
+         return glsl_type::sampler3D_type;
+      case 3:
+         return glsl_type::samplerCube_type;
+      default:
+         CACHE_DEBUG("%s: unknown sampler type (dim %d)\n",
+            __func__, data.sampler_dimensionality);
+      }
+   }
+
+   /* array type has additional element_type information */
+   if (data.base_type == GLSL_TYPE_ARRAY) {
+      const glsl_type *element_type = read_glsl_type(map);
+      if (!element_type) {
+         CACHE_DEBUG("error reading array element type\n");
+         return NULL;
+      }
+      return glsl_type::get_array_instance(element_type, data.length);
+   }
+
+   /* structures have fields containing of names and types */
+   else if (data.base_type == GLSL_TYPE_STRUCT) {
+      glsl_struct_field *fields = ralloc_array(mem_ctx,
+         glsl_struct_field, data.length);
+      for (int k = 0; k < data.length; k++) {
+         char field_name[256];
+         map.read_string(field_name);
+         fields[k].name = _mesa_strdup(field_name);
+         fields[k].type = read_glsl_type(map);
+      }
+      return glsl_type::get_record_instance(fields, data.length, name);
+   }
+
+   return glsl_type::get_instance(data.base_type,
+      data.vector_elms, data.matrix_cols);
+}
+
+
+int ir_cache::read_ir_variable(struct exec_list *list, memory_map &map)
+{
+   char name[256];
+   char unique_name[256];
+   glsl_type_data type_data;
+   ir_variable_data data;
+
+   const glsl_type *type = read_glsl_type(map);
+
+   map.read_string(name);
+   map.read_string(unique_name);
+   map.read_struct(&data);
+   data.name = NULL;
+   data.unique_name = NULL;
+   data.type = NULL;
+   data.state_slots = NULL;
+
+   ir_variable *var = new(mem_ctx)ir_variable(type,
+      name, (ir_variable_mode) data.mode);
+
+   if (!var)
+      return -1;
+
+   var->explicit_location = data.explicit_location;
+   var->explicit_index = data.explicit_index;
+   var->explicit_binding = data.explicit_binding;
+
+   var->max_array_access = data.max_array_access;
+   var->location = data.location;
+   var->read_only = data.read_only;
+   var->centroid = data.centroid;
+   var->invariant = data.invariant;
+   var->interpolation = data.interpolation;
+   var->origin_upper_left = data.origin_upper_left;
+   var->pixel_center_integer = data.pixel_center_integer;
+   var->depth_layout = (ir_depth_layout) data.depth_layout;
+   var->num_state_slots = data.num_state_slots;
+
+   var->state_slots = NULL;
+
+   if (var->num_state_slots > 0) {
+      var->state_slots = ralloc_array(var, ir_state_slot,
+         var->num_state_slots);
+
+      for (int i = 0; i < var->num_state_slots; i++) {
+         map.read_value(&var->state_slots[i].swizzle);
+         for (int j = 0; j < 5; j++) {
+            map.read_value(&var->state_slots[i].tokens[j]);
+         }
+      }
+   }
+
+   var->location_frac = 0;
+
+   if (data.has_constant_value)
+      var->constant_value = read_ir_constant(map);
+
+   if (data.has_constant_initializer)
+      var->constant_initializer = read_ir_constant(map);
+
+   /* store address to this variable */
+   hash_store(var, unique_name);
+
+   list->push_tail(var);
+
+   return 0;
+}
+
+
+int ir_cache::read_ir_function(struct exec_list *list, memory_map &map)
+{
+   char name[256];
+   int32_t par_count = 0;
+   int32_t body_count = 0;
+   uint32_t is_builtin = 0;
+   int ir_type;
+   long len;
+   uint32_t sig_amount;
+
+   map.read_string(name);
+   map.read_value(&sig_amount);
+
+   ir_function *f = new(mem_ctx) ir_function(name);
+   ir_function_signature *sig = NULL;
+
+   /* add all signatures to the function */
+   for (unsigned j = 0; j < sig_amount; j++) {
+
+      /* ir_function_signature */
+      map.read_value(&ir_type);
+      map.read_value(&len);
+
+      if (ir_type != ir_type_function_signature) {
+         CACHE_DEBUG("cache format error with function %s\n", name);
+         return -1;
+      }
+
+      map.read_value(&par_count);
+      map.read_value(&body_count);
+      map.read_value(&is_builtin);
+
+
+      CACHE_DEBUG("%s: [%s] %d parameters, body size %d (is_builtin %d)\n",
+         __func__, name, par_count, body_count, is_builtin);
+
+      const glsl_type *return_type = read_glsl_type(map);
+
+      if (!return_type) {
+         CACHE_DEBUG("no return type found for [%s]\n", name);
+         return -1;
+      }
+
+      sig = new(mem_ctx) ir_function_signature(return_type);
+
+      /* fill parameters for function signature */
+      for (int k = 0; k < par_count; k++)
+         if (read_instruction(&sig->parameters, map))
+            goto read_errors;
+
+      /* insert function parameter variables to prototypes list ... */
+      foreach_list_const(node, &sig->parameters) {
+         ir_variable *var = ((ir_instruction *) node)->as_variable();
+         if (var)
+            prototypes->push_tail(var->clone(mem_ctx, NULL));
+      }
+
+      current_function = &sig->body;
+
+      /* fill instructions for the function body */
+      if (!prototypes_only)
+         for (int k = 0; k < body_count; k++)
+            if (read_instruction(&sig->body, map, is_builtin ? true : false))
+               goto read_errors;
+
+      sig->is_defined = body_count ? 1 : 0;
+
+      if (!is_builtin) {
+         f->add_signature(sig);
+      } else {
+         ir_function_signature *builtin_sig =
+            _mesa_glsl_find_builtin_function(state, name, &sig->parameters);
+
+         if (builtin_sig) {
+            CACHE_DEBUG("found builtin signature for [%s]\n", name);
+            f->add_signature(sig);
+         } else {
+            CACHE_DEBUG("cannot find builtin, function [%s]\n", name);
+            return -1;
+         }
+      }
+
+   } /* for each function signature */
+
+   CACHE_DEBUG("added user function [%s]\n", name);
+
+   /* push ready function to the IR exec_list */
+   list->push_tail(f);
+
+   return 0;
+
+read_errors:
+   CACHE_DEBUG("%s: read errors with [%s]\n", __func__, name);
+   if (sig)
+      ralloc_free(sig);
+   return -1;
+
+}
+
+
+ir_dereference_array *ir_cache::read_ir_dereference_array(memory_map &map)
+{
+   int ir_type;
+   long len;
+
+   CACHE_DEBUG("%s\n", __func__);
+
+   map.read_value(&ir_type);
+   map.read_value(&len);
+
+   ir_rvalue *array_rval = read_ir_rvalue(map);
+   ir_rvalue *index_rval = read_ir_rvalue(map);
+
+   if (array_rval && index_rval)
+      return new(mem_ctx) ir_dereference_array(array_rval, index_rval);
+
+   CACHE_DEBUG("%s: could not get rvalues", __func__);
+   return NULL;
+}
+
+
+ir_dereference_record *ir_cache::read_ir_dereference_record(memory_map &map)
+{
+   int ir_type;
+   long len;
+   char name[256];
+
+   CACHE_DEBUG("%s\n", __func__);
+
+   map.read_value(&ir_type);
+   map.read_value(&len);
+   map.read_string(name);
+
+   ir_rvalue *rval = read_ir_rvalue(map);
+
+   if (rval)
+      return new(mem_ctx) ir_dereference_record(rval, name);
+
+   CACHE_DEBUG("%s: could not get rvalue", __func__);
+   return NULL;
+}
+
+
+/**
+ * Reads in a variable deref, seeks variable address
+ * from a map with it's unique_name
+ */
+ir_dereference_variable *ir_cache::read_ir_dereference_variable(memory_map &map)
+{
+   int ir_type;
+   long len;
+   char name[256];
+   char unique_name[256];
+   uint32_t unique_id;
+
+   map.read_value(&ir_type);
+   map.read_value(&len);
+   map.read_string(name);
+   map.read_value(&unique_id);
+
+   _mesa_snprintf(unique_name, 256, "%s_%d", name, unique_id);
+   const void *addr = hash_table_find(var_ht, (const void *) unique_name);
+
+   CACHE_DEBUG("found addr %p with name %s\n", addr, unique_name);
+
+   if (addr != 0) {
+      ir_variable *var = (ir_variable*) addr;
+      return new(mem_ctx) ir_dereference_variable(var);
+   }
+
+   CACHE_DEBUG("%s: could not find [%s]\n", __func__, name);
+   return NULL;
+}
+
+
+ir_constant *ir_cache::read_ir_constant(memory_map &map)
+{
+   ir_constant *con = NULL;
+   int ir_type;
+   long size;
+
+   map.read_value(&ir_type);
+   map.read_value(&size);
+
+   const glsl_type *constant_type = read_glsl_type(map);
+
+   /* data structure */
+   ir_constant_data data;
+   map.read_struct(&data);
+
+   con = new(mem_ctx) ir_constant(constant_type, &data);
+
+   /* constant with array of constants */
+   if (constant_type->base_type == GLSL_TYPE_ARRAY) {
+      con->array_elements = ralloc_array(mem_ctx, ir_constant *,
+         constant_type->length);
+
+      for (unsigned i = 0; i < constant_type->length; i++)
+         con->array_elements[i] = read_ir_constant(map);
+
+      return con;
+   }
+
+   /* FIXME, not supported yet */
+   else if (constant_type->base_type == GLSL_TYPE_STRUCT)
+      return NULL;
+
+   return con;
+}
+
+
+ir_swizzle *ir_cache::read_ir_swizzle(memory_map &map)
+{
+   unsigned swiz[4] = { 0 };
+   unsigned count;
+   int ir_type;
+   long size;
+
+   CACHE_DEBUG("%s\n", __func__);
+
+   map.read_value(&ir_type);
+   map.read_value(&size);
+
+   /* num of components + swizzle mask, rvalue */
+   map.read_value(&count);
+   map.read_nval(swiz, 4);
+
+   ir_rvalue *rval = read_ir_rvalue(map);
+
+   if (rval)
+      return new(mem_ctx) ir_swizzle(rval, swiz, count);
+
+   CACHE_DEBUG("error, could not handle rvalue for swizzle\n");
+   return NULL;
+}
+
+
+ir_expression *ir_cache::read_ir_expression(memory_map &map)
+{
+   ir_expression_operation operation;
+   ir_rvalue *ir_rvalue_table[4] = { NULL };
+   int operands;
+   int ir_type;
+   long r_size;
+
+   CACHE_DEBUG("%s\n", __func__);
+
+   map.read_value(&ir_type);
+   map.read_value(&r_size);
+
+   /* glsl_type resulted from operation */
+   const glsl_type *rval_type = read_glsl_type(map);
+
+   /* read operation type + all operands for creating ir_expression */
+   map.read_value(&operation);
+   map.read_value(&operands);
+
+   CACHE_DEBUG("%s : operation %d, operands %d\n",
+      __func__, operation, operands);
+
+   for (int k = 0; k < operands; k++) {
+      ir_rvalue *val = read_ir_rvalue(map);
+
+      if (!val)
+         return NULL;
+
+      ir_rvalue_table[k] = val;
+   }
+
+   return new(mem_ctx) ir_expression(operation,
+      rval_type,
+      ir_rvalue_table[0],
+      ir_rvalue_table[1],
+      ir_rvalue_table[2],
+      ir_rvalue_table[3]);
+}
+
+
+ir_rvalue *ir_cache::read_ir_rvalue(memory_map &map)
+{
+   int32_t ir_type = ir_type_unset;
+
+   map.read_value(&ir_type);
+
+   CACHE_DEBUG("%s: ir_value %d\n", __func__, ir_type);
+
+   switch(ir_type) {
+   case ir_type_constant:
+      return read_ir_constant(map);
+   case ir_type_dereference_variable:
+      return read_ir_dereference_variable(map);
+   case ir_type_dereference_record:
+      return read_ir_dereference_record(map);
+   case ir_type_dereference_array:
+      return read_ir_dereference_array(map);
+   case ir_type_expression:
+      return read_ir_expression(map);
+   case ir_type_swizzle:
+      return read_ir_swizzle(map);
+   default:
+      CACHE_DEBUG("%s: error, unhandled type %d\n",
+      __func__, ir_type);
+      break;
+   }
+   return NULL;
+}
+
+
+/**
+ * read assignment instruction, mask + rvalue
+ */
+int ir_cache::read_ir_assignment(struct exec_list *list, memory_map &map)
+{
+   unsigned write_mask = 0;
+   int lhs_type = 0;
+   char lhs_name[256];
+
+   ir_assignment *assign = NULL;
+   ir_dereference *lhs_deref = NULL;
+
+   map.read_value(&write_mask);
+   map.read_value(&lhs_type);
+
+   CACHE_DEBUG("%s: mask %d lhs_type %d\n", __func__, write_mask, lhs_type);
+
+   map.read_string(lhs_name);
+
+   CACHE_DEBUG("%s : lhs name [%s]\n", __func__, lhs_name);
+
+   switch (lhs_type) {
+   case ir_type_dereference_variable:
+      lhs_deref = read_ir_dereference_variable(map);
+      break;
+   case ir_type_dereference_record:
+      lhs_deref = read_ir_dereference_record(map);
+      break;
+   case ir_type_dereference_array:
+      lhs_deref = read_ir_dereference_array(map);
+      break;
+   default:
+      CACHE_DEBUG("%s: error, unhandled lhs_type %d\n",
+         __func__, lhs_type);
+   }
+
+   if (!lhs_deref) {
+      CACHE_DEBUG("could not find lhs variable, bailing out\n");
+      return -1;
+   }
+
+   /* rvalue for assignment */
+   ir_rvalue *rval = read_ir_rvalue(map);
+
+   /* if we managed to parse rvalue, then we can construct assignment */
+   if (rval) {
+
+      CACHE_DEBUG("%s: lhs type %d\n", __func__, lhs_type);
+
+      assign = new(mem_ctx) ir_assignment(lhs_deref, rval, NULL, write_mask);
+      list->push_tail(assign);
+      return 0;
+   }
+
+   CACHE_DEBUG("error reading assignment rhs\n");
+   return -1;
+}
+
+
+/**
+ * if with condition + then and else branches
+ */
+int ir_cache::read_ir_if(struct exec_list *list, memory_map &map)
+{
+   unsigned then_len, else_len;
+
+   CACHE_DEBUG("%s\n", __func__);
+
+   map.read_value(&then_len);
+   map.read_value(&else_len);
+
+   ir_rvalue *cond = read_ir_rvalue(map);
+
+   if (!cond) {
+      CACHE_DEBUG("%s: error reading condition\n", __func__);
+      return -1;
+   }
+
+   ir_if *irif = new(mem_ctx) ir_if(cond);
+
+   for (unsigned k = 0; k < then_len; k++)
+      if(read_instruction(&irif->then_instructions, map))
+         goto read_errors;
+
+   for (unsigned k = 0; k < else_len; k++)
+      if (read_instruction(&irif->else_instructions, map))
+         goto read_errors;
+
+   list->push_tail(irif);
+   return 0;
+
+read_errors:
+   CACHE_DEBUG("%s: read errors(then %d else %d)\n",
+      __func__, then_len, else_len);
+   ralloc_free(irif);
+   return -1;
+}
+
+
+int ir_cache::read_ir_return(struct exec_list *list, memory_map &map)
+{
+   uint32_t has_rvalue = 0;
+   map.read_value(&has_rvalue);
+
+   CACHE_DEBUG("%s\n", __func__);
+
+   ir_rvalue *rval = NULL;
+   if (has_rvalue)
+      rval = read_ir_rvalue(map);
+
+   ir_return *ret = new(mem_ctx) ir_return(rval);
+   list->push_tail(ret);
+
+   return 0;
+}
+
+
+/**
+ * read a call to a ir_function, finds the correct function
+ * signature from prototypes list and creates the call
+ */
+int ir_cache::read_ir_call(struct exec_list *list, memory_map &map)
+{
+   unsigned has_return_deref = 0;
+   unsigned list_len = 0;
+   unsigned use_builtin = 0;
+   struct exec_list parameters;
+   char name[256];
+   ir_dereference_variable *return_deref = NULL;
+
+   map.read_string(name);
+
+   map.read_value(&has_return_deref);
+
+   if (has_return_deref)
+      return_deref = read_ir_dereference_variable(map);
+
+   map.read_value(&list_len);
+
+   CACHE_DEBUG("call to function %s, %d parameters (ret deref %p)\n",
+      name, list_len, return_deref);
+
+   /* read call parameters */
+   for(unsigned k = 0; k < list_len; k++) {
+
+      ir_rvalue *rval = read_ir_rvalue(map);
+      if (rval) {
+         parameters.push_tail(rval);
+      } else {
+         CACHE_DEBUG("%s: error reading rvalue\n", __func__);
+         return -1;
+      }
+   }
+
+   map.read_value(&use_builtin);
+
+   if (use_builtin) {
+      ir_function_signature *builtin_sig =
+         _mesa_glsl_find_builtin_function(state, name, &parameters);
+
+      if (builtin_sig) {
+         CACHE_DEBUG("%s: found function %s from builtins\n", __func__, name);
+
+         ir_function_signature *callee = builtin_sig;
+
+         CACHE_DEBUG("function signature for builtin %s : %p\n", name, callee);
+         if (!callee) {
+            CACHE_DEBUG("sorry, cannot find signature for builtin ..\n");
+            return -1;
+         }
+
+         ir_call *call = new(mem_ctx) ir_call(callee, return_deref,
+            &parameters);
+
+         call->use_builtin = true;
+
+         list->push_tail(call);
+         return 0;
+      }
+   }
+
+   /* find the function from the prototypes */
+   ir_function *func = search_func(state, prototypes, name, &parameters);
+
+   if (func) {
+      CACHE_DEBUG("found function with name %s (has user sig %d)\n",
+         name, func->has_user_signature());
+
+      ir_function_signature *callee = func->matching_signature(state,
+         &parameters);
+
+      /**
+       * FIXME - this is a workaround for a call to empty user defined function
+       * (happens with glb2.7), linking would fail if we would create a call,
+       * empty functions get removed only afer linking .. this is not a safe
+       * thing todo here though :/
+       */
+      if (!callee->is_defined)
+         return 0;
+
+      ir_call *call = new(mem_ctx) ir_call(callee, return_deref, &parameters);
+      list->push_tail(call);
+      return 0;
+   }
+
+   CACHE_DEBUG("%s:function %s not found for ir_call ...\n",
+      __func__, name);
+   return -1;
+}
+
+
+int ir_cache::read_ir_discard(struct exec_list *list, memory_map &map)
+{
+   uint32_t has_condition;
+   map.read_value(&has_condition);
+
+   CACHE_DEBUG("%s\n", __func__);
+
+   list->push_tail(new(mem_ctx) ir_discard);
+   return 0;
+}
+
+
+/**
+ * read in ir_loop
+ */
+int ir_cache::read_ir_loop(struct exec_list *list, memory_map &map)
+{
+   unsigned has_counter, has_from, has_to, has_increment, body_size;
+   int cmp;
+   char counter_name[256];
+   ir_loop *loop = NULL;
+
+   loop = new(mem_ctx) ir_loop;
+
+   map.read_value(&has_from);
+   map.read_value(&has_to);
+   map.read_value(&has_increment);
+   map.read_value(&has_counter);
+   map.read_value(&cmp);
+   map.read_value(&body_size);
+
+   /* comparison operation for loop termination */
+   loop->cmp = cmp;
+
+   /* ir_rvalues: from, to, increment + one ir_variable counter */
+   if (has_from) {
+      loop->from = read_ir_rvalue(map);
+      if (!loop->from)
+         return -1;
+   }
+
+   if (has_to) {
+      loop->to = read_ir_rvalue(map);
+      if (!loop->to)
+         return -1;
+   }
+
+   if (has_increment) {
+      loop->increment = read_ir_rvalue(map);
+      if (!loop->increment)
+         return -1;
+   }
+
+  /* read ir_variable to prototypes list and search from there */
+  if (has_counter) {
+      map.read_string(counter_name);
+      if (read_instruction(prototypes, map))
+         return -1;
+      loop->counter = search_var(prototypes, counter_name);
+      if (!loop->counter)
+         return -1;
+  }
+
+   CACHE_DEBUG("%s: from %p to %p increment %p counter %p size %d\n", __func__,
+      loop->from, loop->to, loop->increment, loop->counter, body_size);
+
+   for (unsigned k = 0; k < body_size; k++) {
+      if(read_instruction(&loop->body_instructions, map))
+         goto read_errors;
+   }
+
+   list->push_tail(loop);
+   return 0;
+
+read_errors:
+   CACHE_DEBUG("%s: read errors\n", __func__);
+   if (loop)
+      ralloc_free(loop);
+   return -1;
+}
+
+
+int ir_cache::read_ir_loop_jump(struct exec_list *list, memory_map &map)
+{
+   int32_t mode;
+   map.read_value(&mode);
+   list->push_tail(new(mem_ctx) ir_loop_jump((ir_loop_jump::jump_mode)mode));
+   return 0;
+}
+
+
+int ir_cache::read_instruction(struct exec_list *list, memory_map &map,
+   bool ignore)
+{
+   int ir_type = ir_type_unset;
+   long inst_dumpsize = 0;
+
+   map.read_value(&ir_type);
+   map.read_value(&inst_dumpsize);
+
+   /* reader wants to jump over this instruction */
+   if (ignore) {
+      map.ffwd(inst_dumpsize);
+      return 0;
+   }
+
+   switch(ir_type) {
+   case ir_type_variable:
+      return read_ir_variable(list, map);
+   case ir_type_assignment:
+      return read_ir_assignment(list, map);
+   case ir_type_function:
+      return read_ir_function(list, map);
+   case ir_type_if:
+      return read_ir_if(list, map);
+   case ir_type_return:
+      return read_ir_return(list, map);
+   case ir_type_call:
+      return read_ir_call(list, map);
+   case ir_type_discard:
+      return read_ir_discard(list, map);
+   case ir_type_loop:
+      return read_ir_loop(list, map);
+   case ir_type_loop_jump:
+      return read_ir_loop_jump(list, map);
+   default:
+      CACHE_DEBUG("%s cannot read type %d, todo...\n",
+         __func__, ir_type);
+   }
+
+   return -1;
+}
+
+
+/**
+ * reads prototypes section of the dump, consists
+ * of variables and functions
+ */
+int ir_cache::read_prototypes(memory_map &map)
+{
+   uint32_t total;
+   int ir_type;
+   long inst_dumpsize;
+
+   map.read_value(&total);
+
+   prototypes_only = true;
+
+   for (unsigned k = 0; k < total; k++) {
+
+      map.read_value(&ir_type);
+      map.read_value(&inst_dumpsize);
+
+      switch (ir_type) {
+      case ir_type_variable:
+         if (read_ir_variable(prototypes, map))
+            return -1;
+         break;
+      case ir_type_function:
+         if (read_ir_function(prototypes, map))
+            return -1;
+         break;
+      default:
+         CACHE_DEBUG("%s: error in cache data (ir %d)\n",
+            __func__, ir_type);
+         return -1;
+      }
+   }
+
+   prototypes_only = false;
+
+   CACHE_DEBUG("%s: done\n", __func__);
+   return 0;
+}
+
+static uint32_t read_bool(memory_map &map)
+{
+   uint32_t value = 0;
+   map.read_value(&value);
+   return value;
+}
+
+
+static void _read_state(struct _mesa_glsl_parse_state *state, memory_map &map)
+{
+   /* FIXME - needs rewrite */
+   state->ARB_draw_buffers_enable = read_bool(map);
+   state->ARB_draw_buffers_warn = read_bool(map);
+   state->ARB_draw_instanced_enable = read_bool(map);
+   state->ARB_draw_instanced_warn = read_bool(map);
+   state->ARB_explicit_attrib_location_enable = read_bool(map);
+   state->ARB_explicit_attrib_location_warn = read_bool(map);
+   state->ARB_fragment_coord_conventions_enable = read_bool(map);
+   state->ARB_fragment_coord_conventions_warn = read_bool(map);
+   state->ARB_texture_rectangle_enable = read_bool(map);
+   state->ARB_texture_rectangle_warn = read_bool(map);
+   state->EXT_texture_array_enable = read_bool(map);
+   state->EXT_texture_array_warn = read_bool(map);
+   state->ARB_shader_texture_lod_enable = read_bool(map);
+   state->ARB_shader_texture_lod_warn = read_bool(map);
+   state->ARB_shader_stencil_export_enable = read_bool(map);
+   state->ARB_shader_stencil_export_warn = read_bool(map);
+   state->AMD_conservative_depth_enable = read_bool(map);
+   state->AMD_conservative_depth_warn = read_bool(map);
+   state->ARB_conservative_depth_enable = read_bool(map);
+   state->ARB_conservative_depth_warn = read_bool(map);
+   state->AMD_shader_stencil_export_enable = read_bool(map);
+   state->AMD_shader_stencil_export_warn = read_bool(map);
+   state->OES_texture_3D_enable = read_bool(map);
+   state->OES_texture_3D_warn = read_bool(map);
+   state->OES_EGL_image_external_enable = read_bool(map);
+   state->OES_EGL_image_external_warn = read_bool(map);
+   state->ARB_shader_bit_encoding_enable = read_bool(map);
+   state->ARB_shader_bit_encoding_warn = read_bool(map);
+   state->ARB_uniform_buffer_object_enable = read_bool(map);
+   state->ARB_uniform_buffer_object_warn = read_bool(map);
+   state->OES_standard_derivatives_enable = read_bool(map);
+   state->OES_standard_derivatives_warn = read_bool(map);
+   state->ARB_texture_cube_map_array_enable = read_bool(map);
+   state->ARB_texture_cube_map_array_warn = read_bool(map);
+   state->ARB_shading_language_packing_enable = read_bool(map);
+   state->ARB_shading_language_packing_warn = read_bool(map);
+   state->ARB_texture_multisample_enable = read_bool(map);
+   state->ARB_texture_multisample_warn = read_bool(map);
+   state->ARB_texture_query_lod_enable = read_bool(map);
+   state->ARB_texture_query_lod_warn = read_bool(map);
+   state->ARB_gpu_shader5_enable = read_bool(map);
+   state->ARB_gpu_shader5_warn = read_bool(map);
+   state->AMD_vertex_shader_layer_enable = read_bool(map);
+   state->AMD_vertex_shader_layer_warn = read_bool(map);
+   state->ARB_shading_language_420pack_enable = read_bool(map);
+   state->ARB_shading_language_420pack_warn = read_bool(map);
+   state->EXT_shader_integer_mix_enable = read_bool(map);
+   state->EXT_shader_integer_mix_warn = read_bool(map);
+}
+
+
+struct gl_shader *ir_cache::unserialize(void *mem_ctx,
+   memory_map &map,
+   struct _mesa_glsl_parse_state *state,
+   const char *mesa_sha,
+   int *error_code)
+{
+   int error = 0;
+   *error_code = ir_cache::GENERAL_READ_ERROR;
+
+   struct gl_shader *shader =
+      (struct gl_shader *) ralloc (mem_ctx, struct gl_shader);
+
+   if (!shader)
+      return NULL;
+
+   shader->ir = new(shader) exec_list;
+
+   top_level = shader->ir;
+
+   if (read_header(shader, map, mesa_sha)) {
+      *error_code = ir_cache::DIFFERENT_MESA_SHA;
+      goto error_unserialize;
+   }
+
+   _read_state(state, map);
+
+   /* fill parse state from shader header information */
+   switch (shader->Type) {
+      case GL_VERTEX_SHADER:
+         state->target = vertex_shader;
+         break;
+      case GL_FRAGMENT_SHADER:
+         state->target = fragment_shader;
+         break;
+      case GL_GEOMETRY_SHADER_ARB:
+         state->target = geometry_shader;
+         break;
+      break;
+   }
+
+   state->language_version = shader->Version;
+   state->es_shader = shader->IsES ? 1 : 0;
+   state->num_builtins_to_link = 0;
+
+   _mesa_glsl_initialize_builtin_functions();
+   _mesa_glsl_initialize_types(state);
+
+   /**
+    * parser state is used to find builtin functions and
+    * existing types during reading
+    */
+   this->state = state;
+
+   /* allocations during reading */
+   this->mem_ctx = mem_ctx;
+
+   prototypes = new(mem_ctx) exec_list;
+
+   error = read_prototypes(map);
+
+   /* top level exec_list read loop, constructs a new list */
+   while(!map.end() && error == 0)
+      error = read_instruction(shader->ir, map);
+
+   ralloc_free(prototypes);
+
+   if (error)
+      goto error_unserialize;
+
+   *error_code = 0;
+
+   CACHE_DEBUG("shader from cache\n");
+
+   return shader;
+
+error_unserialize:
+
+   ralloc_free(shader->ir);
+   ralloc_free(shader);
+   return NULL;
+}
+
-- 
1.8.1.4



More information about the mesa-dev mailing list