[Mesa-dev] [PATCH 07/10] glsl: ir_deserializer class for the binary shader cache
Tapani Pälli
tapani.palli at intel.com
Wed Jan 29 01:25:02 PST 2014
ir_deserializer fills existing gl_shader structure's ir exec_list from
binary data, this will be used by shader binary cache implementation.
Signed-off-by: Tapani Pälli <tapani.palli at intel.com>
---
src/glsl/Makefile.sources | 1 +
src/glsl/ir_deserializer.cpp | 910 +++++++++++++++++++++++++++++++++++++++++++
src/glsl/ir_deserializer.h | 133 +++++++
3 files changed, 1044 insertions(+)
create mode 100644 src/glsl/ir_deserializer.cpp
create mode 100644 src/glsl/ir_deserializer.h
diff --git a/src/glsl/Makefile.sources b/src/glsl/Makefile.sources
index 24bb70f..3c49a58 100644
--- a/src/glsl/Makefile.sources
+++ b/src/glsl/Makefile.sources
@@ -33,6 +33,7 @@ LIBGLSL_FILES = \
$(GLSL_SRCDIR)/ir_clone.cpp \
$(GLSL_SRCDIR)/ir_constant_expression.cpp \
$(GLSL_SRCDIR)/ir.cpp \
+ $(GLSL_SRCDIR)/ir_deserializer.cpp \
$(GLSL_SRCDIR)/ir_equals.cpp \
$(GLSL_SRCDIR)/ir_expression_flattening.cpp \
$(GLSL_SRCDIR)/ir_function_can_inline.cpp \
diff --git a/src/glsl/ir_deserializer.cpp b/src/glsl/ir_deserializer.cpp
new file mode 100644
index 0000000..c383f61
--- /dev/null
+++ b/src/glsl/ir_deserializer.cpp
@@ -0,0 +1,910 @@
+/* -*- 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_deserializer.h"
+#include "shader_cache_magic.h"
+
+
+/**
+ * Searches for ir_function with matching signature from exec_list
+ */
+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 && strcmp(name, func->name) == 0 &&
+ func->matching_signature(state, parameters))
+ return func;
+ }
+ return NULL;
+}
+
+/**
+ * Helper function to read a list of instructions
+ */
+bool
+ir_deserializer::deserialize_list(exec_list *list)
+{
+ uint32_t list_len = map->read_uint32_t();
+ for (unsigned k = 0; k < list_len; k++)
+ if (!read_instruction(list))
+ return false;
+ return true;
+}
+
+
+const glsl_type *
+ir_deserializer::read_glsl_type()
+{
+ char *name = map->read_string();
+ uint32_t type_size = map->read_uint32_t();
+ const glsl_type *ret_type = glsl_type::error_type;
+
+ const glsl_type *existing_type =
+ state->symbols->get_type(name);
+
+ /* if type exists, move read pointer forward and return type */
+ if (existing_type) {
+ map->ffwd(type_size);
+ return existing_type;
+ }
+
+ /* has this user type been read and stored to hash already? */
+ uint8_t user_type_exists = map->read_uint8_t();
+ uint32_t hash_sum = map->read_uint32_t();
+
+ if (user_type_exists) {
+ hash_entry *entry = _mesa_hash_table_search(type_ht, hash_value,
+ (void*) (uintptr_t) hash_sum);
+
+ /* return already read type from the hash */
+ if (entry && entry->data)
+ return (const glsl_type *) entry->data;
+ else
+ return glsl_type::error_type;
+ }
+
+ uint32_t length = map->read_uint32_t();
+ uint8_t base_type = map->read_uint8_t();
+ uint8_t interface_packing = map->read_uint8_t();
+
+ if (base_type >= GLSL_TYPE_ERROR)
+ return glsl_type::error_type;
+
+ /* array type has additional element_type information */
+ if (base_type == GLSL_TYPE_ARRAY) {
+ const glsl_type *element_type = read_glsl_type();
+ if (!element_type) {
+ CACHE_DEBUG("error reading array element type\n");
+ return NULL;
+ }
+
+ ret_type = glsl_type::get_array_instance(element_type, length);
+ goto return_type;
+ }
+
+ /* structures have fields containing of names and types */
+ else if (base_type == GLSL_TYPE_STRUCT ||
+ base_type == GLSL_TYPE_INTERFACE) {
+ glsl_struct_field *fields = ralloc_array(mem_ctx,
+ glsl_struct_field, length);
+
+ if (!fields)
+ return glsl_type::error_type;
+
+ for (unsigned k = 0; k < length; k++) {
+ uint8_t row_major, interpolation, centroid;
+ int32_t location;
+ char *field_name = map->read_string();
+ fields[k].name = _mesa_strdup(field_name);
+ fields[k].type = read_glsl_type();
+ row_major = map->read_uint8_t();
+ location = map->read_int32_t();
+ interpolation = map->read_uint8_t();
+ centroid = map->read_uint8_t();
+ fields[k].row_major = row_major;
+ fields[k].location = location;
+ fields[k].interpolation = interpolation;
+ fields[k].centroid = centroid;
+
+ /* break out of the loop if read errors occured */
+ if (map->errors())
+ return glsl_type::error_type;
+ }
+
+ if (base_type == GLSL_TYPE_STRUCT)
+ ret_type = glsl_type::get_record_instance(fields, length, name);
+ else if (base_type == GLSL_TYPE_INTERFACE)
+ ret_type = glsl_type::get_interface_instance(fields,
+ length, (glsl_interface_packing) interface_packing, name);
+
+ /* free allocated memory */
+ for (unsigned k = 0; k < length; k++)
+ free((void *)fields[k].name);
+ ralloc_free(fields);
+
+ goto return_type;
+ }
+
+ return glsl_type::error_type;
+
+return_type:
+
+ /* store user type in to hash */
+ _mesa_hash_table_insert(type_ht, hash_value,
+ (void*) (uintptr_t) hash_sum, (void*) ret_type);
+
+ return ret_type;
+}
+
+
+ir_instruction *
+ir_deserializer::read_ir_variable()
+{
+ const glsl_type *type = read_glsl_type();
+
+ char *name = map->read_string();
+ int64_t unique_id = map->read_int64_t();
+ uint8_t mode = map->read_uint8_t();
+
+ ir_variable *var = new(mem_ctx)ir_variable(type,
+ name, (ir_variable_mode) mode);
+
+ if (!var)
+ return NULL;
+
+ map->read(&var->data, sizeof(var->data));
+
+ var->num_state_slots = map->read_uint32_t();
+ uint8_t has_constant_value = map->read_uint8_t();
+ uint8_t has_constant_initializer = map->read_uint8_t();
+
+ var->state_slots = NULL;
+
+ if (var->num_state_slots > 0) {
+
+ /* validate num_state_slots against defined maximum */
+ if (var->num_state_slots > MAX_NUM_STATE_SLOTS)
+ return NULL;
+
+ var->state_slots = ralloc_array(var, ir_state_slot,
+ var->num_state_slots);
+
+ for (unsigned i = 0; i < var->num_state_slots; i++) {
+ var->state_slots[i].swizzle = map->read_int32_t();
+ for (int j = 0; j < 5; j++) {
+ var->state_slots[i].tokens[j] = map->read_int32_t();
+ }
+ }
+ }
+
+ if (has_constant_value)
+ var->constant_value = read_ir_constant();
+
+ if (has_constant_initializer)
+ var->constant_initializer = read_ir_constant();
+
+ uint8_t has_interface_type = map->read_uint8_t();
+
+ if (has_interface_type)
+ var->init_interface_type(read_glsl_type());
+
+ /**
+ * Store address to this variable so that variable
+ * dereference readers can find it later.
+ */
+ _mesa_hash_table_insert(var_ht, hash_value,
+ (void*) (intptr_t) unique_id, var);
+
+ return var;
+}
+
+
+ir_instruction *
+ir_deserializer::read_ir_function(bool prototypes_only)
+{
+ uint8_t is_builtin = 0;
+ uint8_t ir_type;
+ uint32_t len;
+
+ char *name = map->read_string();
+ uint32_t num_signatures = map->read_uint32_t();
+
+ 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 < num_signatures; j++) {
+
+ /* ir_function_signature */
+ ir_type = map->read_uint8_t();
+ len = map->read_uint32_t();
+
+ /* used for debugging */
+ (void) ir_type;
+ (void) len;
+
+ is_builtin = map->read_uint8_t();
+
+ CACHE_DEBUG("%s: [%s] (is_builtin %d)\n", __func__, name, is_builtin);
+
+ const glsl_type *return_type = read_glsl_type();
+
+ if (!return_type) {
+ CACHE_DEBUG("no return type found for [%s]\n", name);
+ return NULL;
+ }
+
+ sig = new(mem_ctx) ir_function_signature(return_type);
+
+ /* fill parameters for function signature */
+ if (!deserialize_list(&sig->parameters))
+ goto read_errors;
+
+ /* fill instructions for the function body */
+ if (!prototypes_only) {
+ uint32_t body_count = map->read_uint32_t();
+ for (unsigned k = 0; k < body_count; k++)
+ if (!read_instruction(&sig->body, 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 NULL;
+ }
+ }
+
+ /* break out of the loop if read errors occured */
+ if (map->errors())
+ goto read_errors;
+
+ } /* for each function signature */
+
+ CACHE_DEBUG("added %s function [%s]\n",
+ is_builtin ? "builtin" : "user", name);
+
+ return f;
+
+read_errors:
+ CACHE_DEBUG("%s: read errors with [%s]\n", __func__, name);
+ if (sig)
+ ralloc_free(sig);
+ return NULL;
+
+}
+
+
+/* reads in type and package length, compares type to expected type */
+#define VALIDATE_RVALUE(node_type)\
+ uint8_t ir_type = map->read_uint8_t();\
+ uint32_t len = map->read_uint32_t();\
+ (void) len;\
+ CACHE_DEBUG("%s: ir_type %d len %d\n", __func__, ir_type, len);\
+ if (ir_type != node_type)\
+ return NULL;\
+
+
+ir_dereference_array *
+ir_deserializer::read_ir_dereference_array()
+{
+ VALIDATE_RVALUE(ir_type_dereference_array);
+
+ ir_rvalue *array_rval = read_ir_rvalue();
+ ir_rvalue *index_rval = read_ir_rvalue();
+
+ 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_deserializer::read_ir_dereference_record()
+{
+ VALIDATE_RVALUE(ir_type_dereference_record);
+
+ char *name = map->read_string();
+
+ ir_rvalue *rval = read_ir_rvalue();
+
+ 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_deserializer::read_ir_dereference_variable()
+{
+ VALIDATE_RVALUE(ir_type_dereference_variable);
+
+ int64_t unique_id = map->read_int64_t();
+
+ hash_entry *entry = _mesa_hash_table_search(var_ht, hash_value,
+ (void*) (intptr_t) unique_id);
+
+ if (!entry) {
+ CACHE_DEBUG("%s: could not find variable\n", __func__);
+ return NULL;
+ }
+
+ CACHE_DEBUG("%s: found addr %p\n", __func__, entry->data);
+
+ return new(mem_ctx) ir_dereference_variable((ir_variable*) entry->data);
+}
+
+
+ir_constant *
+ir_deserializer::read_ir_constant()
+{
+ VALIDATE_RVALUE(ir_type_constant);
+
+ ir_constant *con = NULL;
+ const glsl_type *constant_type = read_glsl_type();
+
+ /* data structure */
+ ir_constant_data data;
+ map->read(&data, sizeof(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();
+
+
+ /* break out of the loop if read errors occured */
+ if (map->errors()) {
+ ralloc_free(con);
+ return NULL;
+ }
+ }
+
+ } else if (constant_type->base_type == GLSL_TYPE_STRUCT) {
+ if (!deserialize_list(&con->components)) {
+ ralloc_free(con);
+ return NULL;
+ }
+ }
+
+ return con;
+}
+
+
+ir_swizzle *
+ir_deserializer::read_ir_swizzle()
+{
+ VALIDATE_RVALUE(ir_type_swizzle);
+
+ /* swizzle mask, rvalue */
+ struct ir_swizzle_mask mask;
+ map->read(&mask, sizeof(ir_swizzle_mask));
+
+ ir_rvalue *rval = read_ir_rvalue();
+
+ if (rval)
+ return new(mem_ctx) ir_swizzle(rval,
+ mask.x, mask.y, mask.z, mask.w, mask.num_components);
+
+ CACHE_DEBUG("error, could not handle rvalue for swizzle\n");
+ return NULL;
+}
+
+
+ir_texture *
+ir_deserializer::read_ir_texture()
+{
+ VALIDATE_RVALUE(ir_type_texture);
+
+ int32_t op = map->read_int32_t();
+
+ ir_texture *new_tex = new(mem_ctx) ir_texture((ir_texture_opcode)op);
+
+ const glsl_type * type;
+ ir_dereference * sampler;
+
+ if (!new_tex)
+ goto errors;
+
+ type = read_glsl_type();
+ sampler = (ir_dereference *) read_ir_rvalue();
+
+ if (!sampler)
+ goto errors;
+
+ new_tex->set_sampler(sampler, type);
+
+ new_tex->coordinate = read_ir_rvalue();
+ new_tex->projector = read_ir_rvalue();
+ new_tex->shadow_comparitor = read_ir_rvalue();
+ new_tex->offset = read_ir_rvalue();
+
+ /* lod_info structure */
+ memset(&new_tex->lod_info, 0, sizeof(ir_texture::lod_info));
+
+ new_tex->lod_info.lod = read_ir_rvalue();
+ new_tex->lod_info.bias = read_ir_rvalue();
+ new_tex->lod_info.sample_index = read_ir_rvalue();
+ new_tex->lod_info.component = read_ir_rvalue();
+ new_tex->lod_info.grad.dPdx = read_ir_rvalue();
+ new_tex->lod_info.grad.dPdy = read_ir_rvalue();
+
+ return new_tex;
+
+errors:
+ CACHE_DEBUG("error, could not read ir_texture\n");
+ return NULL;
+}
+
+
+ir_expression *
+ir_deserializer::read_ir_expression()
+{
+ VALIDATE_RVALUE(ir_type_expression);
+
+ /* glsl_type resulted from operation */
+ const glsl_type *rval_type = read_glsl_type();
+
+ /* read operation type + all operands for creating ir_expression */
+ uint32_t operation = map->read_uint32_t();
+ uint32_t operands = map->read_int32_t();
+
+ CACHE_DEBUG("%s : operation %d, operands %d\n",
+ __func__, operation, operands);
+
+ ir_rvalue *ir_rvalue_table[4] = { NULL };
+ for (unsigned k = 0; k < operands; k++) {
+ ir_rvalue *val = read_ir_rvalue();
+
+ 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_deserializer::read_ir_rvalue()
+{
+ uint8_t ir_type = map->read_uint8_t();
+
+ CACHE_DEBUG("%s: ir_value %d\n", __func__, ir_type);
+
+ switch(ir_type) {
+ case ir_type_constant:
+ return read_ir_constant();
+ case ir_type_dereference_variable:
+ return read_ir_dereference_variable();
+ case ir_type_dereference_record:
+ return read_ir_dereference_record();
+ case ir_type_dereference_array:
+ return read_ir_dereference_array();
+ case ir_type_expression:
+ return read_ir_expression();
+ case ir_type_swizzle:
+ return read_ir_swizzle();
+ case ir_type_texture:
+ return read_ir_texture();
+ /* type is ir_type_unset ir rvalue is set to NULL */
+ case ir_type_unset:
+ return NULL;
+ default:
+ CACHE_DEBUG("%s: error, unhandled type %d\n",
+ __func__, ir_type);
+ break;
+ }
+ return NULL;
+}
+
+
+ir_instruction *
+ir_deserializer::read_ir_assignment()
+{
+ ir_dereference *lhs_deref = NULL;
+ ir_rvalue *cond = NULL;
+
+ uint32_t write_mask = map->read_uint8_t();
+
+ lhs_deref = (ir_dereference *) read_ir_rvalue();
+
+ if (!lhs_deref) {
+ CACHE_DEBUG("could not find lhs variable, bailing out\n");
+ return NULL;
+ }
+
+ cond = read_ir_rvalue();
+
+ /* rvalue for assignment */
+ ir_rvalue *rval = read_ir_rvalue();
+
+ /* if we managed to parse rvalue, then we can construct assignment */
+ if (rval)
+ return new(mem_ctx) ir_assignment(lhs_deref, rval, cond, write_mask);
+
+ CACHE_DEBUG("error reading assignment rhs\n");
+ return NULL;
+}
+
+
+ir_instruction *
+ir_deserializer::read_ir_if()
+{
+ CACHE_DEBUG("%s\n", __func__);
+
+ ir_rvalue *cond = read_ir_rvalue();
+
+ if (!cond) {
+ CACHE_DEBUG("%s: error reading condition\n", __func__);
+ return NULL;
+ }
+
+ ir_if *irif = new(mem_ctx) ir_if(cond);
+
+ if (!deserialize_list(&irif->then_instructions))
+ goto read_errors;
+ if (!deserialize_list(&irif->else_instructions))
+ goto read_errors;
+
+ return irif;
+
+read_errors:
+ CACHE_DEBUG("%s: read errors", __func__);
+ ralloc_free(irif);
+ return NULL;
+}
+
+
+ir_instruction *
+ir_deserializer::read_ir_return()
+{
+ CACHE_DEBUG("%s\n", __func__);
+ ir_rvalue *rval = read_ir_rvalue();
+ return new(mem_ctx) ir_return(rval);
+}
+
+
+/**
+ * Read a call to ir_function, finds the correct function
+ * signature from prototypes list and creates the call
+ */
+ir_instruction *
+ir_deserializer::read_ir_call()
+{
+ struct exec_list parameters;
+ ir_dereference_variable *return_deref = NULL;
+
+ char *name = map->read_string();
+
+ return_deref = read_ir_dereference_variable();
+
+ uint8_t list_len = map->read_uint8_t();
+
+ 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();
+ if (rval) {
+ parameters.push_tail(rval);
+ } else {
+ CACHE_DEBUG("%s: error reading rvalue\n", __func__);
+ return NULL;
+ }
+ }
+
+ uint8_t use_builtin = map->read_uint8_t();
+
+ if (use_builtin) {
+ ir_function_signature *builtin_sig =
+ _mesa_glsl_find_builtin_function(state, name, ¶meters);
+
+ 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 NULL;
+ }
+
+ ir_call *call = new(mem_ctx) ir_call(callee, return_deref,
+ ¶meters);
+
+ call->use_builtin = true;
+
+ return call;
+ }
+ }
+
+ /* find the function from the prototypes */
+ ir_function *func = search_func(state, prototypes, name, ¶meters);
+
+ 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,
+ ¶meters);
+
+ /**
+ * This is a workaround for a call to empty user defined function, that
+ * happens with glb2.7 if dumping unlinked shaders, linking would fail
+ * if we would create a call, empty functions get removed only after
+ * linking .. this may look a bit strange thing todo but we just ignore
+ * the call here
+ */
+ if (!callee->is_defined)
+ return NULL;
+
+ return new(mem_ctx) ir_call(callee, return_deref, ¶meters);
+ }
+
+ CACHE_DEBUG("%s:function %s not found for ir_call ...\n",
+ __func__, name);
+ return NULL;
+}
+
+
+ir_instruction *
+ir_deserializer::read_ir_discard()
+{
+ CACHE_DEBUG("%s\n", __func__);
+ ir_rvalue *condition = read_ir_rvalue();
+ return new(mem_ctx) ir_discard(condition);
+}
+
+
+ir_instruction *
+ir_deserializer::read_ir_loop()
+{
+ ir_loop *loop = new(mem_ctx) ir_loop;
+
+ if (!deserialize_list(&loop->body_instructions)) {
+ CACHE_DEBUG("%s: read errors\n", __func__);
+ if (loop)
+ ralloc_free(loop);
+ return NULL;
+ }
+
+ return loop;
+}
+
+
+ir_instruction *
+ir_deserializer::read_ir_loop_jump()
+{
+ uint32_t mode = map->read_uint32_t();
+ return new(mem_ctx) ir_loop_jump((ir_loop_jump::jump_mode)mode);
+}
+
+
+ir_instruction *
+ir_deserializer::read_emit_vertex()
+{
+ return new(mem_ctx) ir_emit_vertex;
+}
+
+
+ir_instruction *
+ir_deserializer::read_end_primitive()
+{
+ return new(mem_ctx) ir_end_primitive;
+}
+
+
+bool
+ir_deserializer::read_instruction(struct exec_list *list, bool ignore)
+{
+ uint8_t ir_type = map->read_uint8_t();
+ uint32_t inst_dumpsize = map->read_uint32_t();
+
+ /* reader wants to jump over this instruction */
+ if (ignore) {
+ map->ffwd(inst_dumpsize);
+ return true;
+ }
+
+ ir_instruction *ir;
+
+ switch(ir_type) {
+ case ir_type_variable:
+ ir = read_ir_variable();
+ break;
+ case ir_type_assignment:
+ ir = read_ir_assignment();
+ break;
+ case ir_type_constant:
+ ir = read_ir_constant();
+ break;
+ case ir_type_function:
+ ir = read_ir_function();
+ break;
+ case ir_type_if:
+ ir = read_ir_if();
+ break;
+ case ir_type_return:
+ ir = read_ir_return();
+ break;
+ case ir_type_call:
+ ir = read_ir_call();
+ break;
+ case ir_type_discard:
+ ir = read_ir_discard();
+ break;
+ case ir_type_loop:
+ ir = read_ir_loop();
+ break;
+ case ir_type_loop_jump:
+ ir = read_ir_loop_jump();
+ break;
+ case ir_type_emit_vertex:
+ ir = read_emit_vertex();
+ break;
+ case ir_type_end_primitive:
+ ir = read_end_primitive();
+ break;
+ default:
+ CACHE_DEBUG("%s cannot read type %d, todo...\n",
+ __func__, ir_type);
+ return false;
+ }
+
+ if (!ir || map->errors()) {
+ CACHE_DEBUG("%s, failed with %d\n", __func__, ir_type);
+ return false;
+ }
+
+ list->push_tail(ir);
+ return true;
+}
+
+
+/**
+ * Go through the blob and read prototypes for the functions
+ */
+bool
+ir_deserializer::read_prototypes(unsigned list_len)
+{
+ uint32_t ir_start = map->position();
+
+ for (unsigned k = 0; k < list_len; k++) {
+ /* check type of next instruction */
+ uint8_t ir_type = map->read_uint8_t();
+ uint32_t len = map->read_uint32_t();
+
+ /* ignore if not ir_function */
+ if (ir_type != ir_type_function) {
+ map->ffwd(len);
+ continue;
+ }
+
+ ir_instruction *func = read_ir_function(true);
+ if (!func)
+ return false;
+
+ prototypes->push_tail(func);
+
+ /* break out of the loop if read errors occured */
+ if (map->errors())
+ return false;
+ }
+
+ /* go back to beginning */
+ map->jump(ir_start);
+ return true;
+}
+
+
+bool
+ir_deserializer::deserialize(void *mem_ctx, gl_shader *shader,
+ memory_map *map, uint32_t shader_size)
+{
+ bool success = false;
+ uint32_t exec_list_len;
+ GET_CURRENT_CONTEXT(ctx);
+
+ this->map = map;
+
+ /**
+ * parse state is used to find builtin functions and
+ * existing types during reading
+ */
+ state = new(mem_ctx) _mesa_glsl_parse_state(ctx,
+ _mesa_shader_enum_to_shader_stage(shader->Type), shader);
+
+ state->uses_builtin_functions = true;
+ _mesa_glsl_initialize_builtin_functions();
+ _mesa_glsl_initialize_types(state);
+
+ /* allocations during reading */
+ this->mem_ctx = mem_ctx;
+
+ prototypes = new(mem_ctx) exec_list;
+ shader->ir = new(shader) exec_list;
+
+ exec_list_len = map->read_uint32_t();
+
+ success = read_prototypes(exec_list_len);
+
+ CACHE_DEBUG("reading %d IR instructions\n", exec_list_len);
+
+ /* top level exec_list read loop, constructs a new list */
+ while(map->position() < shader_size && success == true)
+ success = read_instruction(shader->ir);
+
+ ralloc_free(prototypes);
+ ralloc_free(state);
+
+ if (!success)
+ goto error_deserialize;
+
+ shader->CompileStatus = GL_TRUE;
+
+ /* allocates glsl_symbol_table internally */
+ populate_symbol_table(shader);
+
+ validate_ir_tree(shader->ir);
+
+ CACHE_DEBUG("shader from cache\n");
+
+error_deserialize:
+ return success;
+}
diff --git a/src/glsl/ir_deserializer.h b/src/glsl/ir_deserializer.h
new file mode 100644
index 0000000..ec3dcef
--- /dev/null
+++ b/src/glsl/ir_deserializer.h
@@ -0,0 +1,133 @@
+/* -*- 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_DESERIALIZER_H
+#define IR_CACHE_DESERIALIZER_H
+
+#include "program/prog_parameter.h"
+#include "program/program.h"
+#include "glsl_parser_extras.h"
+#include "main/hash_table.h"
+#include "main/imports.h"
+#include "main/shaderobj.h"
+#include "linker.h"
+#include "memory_map.h"
+
+#ifdef SHADER_CACHE_DEBUG
+#define CACHE_DEBUG(fmt, ...) printf(fmt, ## __VA_ARGS__)
+#else
+#define CACHE_DEBUG(fmt, ...) do {} while (0)
+#endif
+
+#ifdef __cplusplus
+
+/**
+ * Class to deserialize gl_shader IR from a binary data blob
+ *
+ * Deserialization is done with a help of memory_map class that takes care
+ * of actual data reading. Class fills existing gl_shader's ir exec_list
+ * with IR instructions from the binary blob.
+ */
+class ir_deserializer
+{
+public:
+ ir_deserializer() :
+ state(NULL),
+ mem_ctx(NULL),
+ map(NULL)
+ {
+ var_ht = _mesa_hash_table_create(0, int_equal);
+ type_ht = _mesa_hash_table_create(0, int_equal);
+ hash_value = _mesa_hash_data(this, sizeof(ir_deserializer));
+ }
+
+ ~ir_deserializer()
+ {
+ _mesa_hash_table_destroy(var_ht, NULL);
+ _mesa_hash_table_destroy(type_ht, NULL);
+ }
+
+ /* deserialize IR to gl_shader from mapped memory */
+ bool deserialize(void *mem_ctx, gl_shader *shader, memory_map *map,
+ uint32_t shader_size);
+
+private:
+
+ struct _mesa_glsl_parse_state *state;
+ void *mem_ctx;
+ memory_map *map;
+
+ /* pointer to list which contains prototypes of functions */
+ struct exec_list *prototypes;
+
+ bool read_prototypes(unsigned list_len);
+ bool read_instruction(struct exec_list *list, bool ignore = false);
+ bool deserialize_list(struct exec_list *list);
+
+ const glsl_type *read_glsl_type();
+
+ ir_instruction *read_ir_variable();
+ ir_instruction *read_ir_assignment();
+ ir_instruction *read_ir_function(bool prototypes_only = false);
+ ir_instruction *read_ir_if();
+ ir_instruction *read_ir_return();
+ ir_instruction *read_ir_call();
+ ir_instruction *read_ir_discard();
+ ir_instruction *read_ir_loop();
+ ir_instruction *read_ir_loop_jump();
+ ir_instruction *read_emit_vertex();
+ ir_instruction *read_end_primitive();
+
+ /* rvalue readers */
+ ir_rvalue *read_ir_rvalue();
+ ir_constant *read_ir_constant();
+ ir_swizzle *read_ir_swizzle();
+ ir_texture *read_ir_texture();
+ ir_expression *read_ir_expression();
+ ir_dereference_array *read_ir_dereference_array();
+ ir_dereference_record *read_ir_dereference_record();
+ ir_dereference_variable *read_ir_dereference_variable();
+
+ /**
+ * 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;
+ uint32_t hash_value;
+
+ /* to read user types only once */
+ struct hash_table *type_ht;
+
+ static bool int_equal(const void *a, const void *b)
+ {
+ return a == b;
+ }
+
+};
+
+
+#endif /* ifdef __cplusplus */
+
+#endif /* IR_CACHE_DESERIALIZER_H */
--
1.8.5.3
More information about the mesa-dev
mailing list