[Mesa-dev] [RFC 02/20] glsl: glsl_type serialization

Tapani Pälli tapani.palli at intel.com
Mon Jun 2 05:05:43 PDT 2014


Will be utilized by IR serialization to serialize user defined types.

Signed-off-by: Tapani Pälli <tapani.palli at intel.com>
Reviewed-by: Paul Berry <stereotype441 at gmail.com> (v1)
---
 src/glsl/glsl_types.cpp | 58 +++++++++++++++++++++++++++++++++++++++++++++++++
 src/glsl/glsl_types.h   |  6 +++++
 2 files changed, 64 insertions(+)

diff --git a/src/glsl/glsl_types.cpp b/src/glsl/glsl_types.cpp
index 849a79a..212d533 100644
--- a/src/glsl/glsl_types.cpp
+++ b/src/glsl/glsl_types.cpp
@@ -143,6 +143,64 @@ glsl_type::glsl_type(const glsl_struct_field *fields, unsigned num_fields,
 }
 
 
+void
+glsl_type::serialize(memory_writer &mem) const
+{
+   uint32_t data_len = 0;
+
+   mem.write_string(name);
+
+   unsigned start_pos = mem.position();
+   mem.write_uint32_t(data_len);
+
+   /**
+    * Used to notify reader if a user defined type
+    * has been serialized before.
+    */
+   uint8_t user_type_exists = 0;
+
+   /* Serialize only user defined types. */
+   switch (base_type) {
+   case GLSL_TYPE_ARRAY:
+   case GLSL_TYPE_STRUCT:
+   case GLSL_TYPE_INTERFACE:
+      break;
+   default:
+      goto serilization_epilogue;
+   }
+
+   uint32_t type_id;
+   user_type_exists = mem.make_unique_id(this, &type_id);
+
+   mem.write_uint8_t(user_type_exists);
+   mem.write_uint32_t(type_id);
+
+   /* No need to write again. */
+   if (user_type_exists)
+      goto serilization_epilogue;
+
+   mem.write_uint32_t((uint32_t)length);
+   mem.write_uint8_t((uint8_t)base_type);
+   mem.write_uint8_t((uint8_t)interface_packing);
+
+   if (base_type == GLSL_TYPE_ARRAY) {
+      element_type()->serialize(mem);
+   } else {
+      glsl_struct_field *field = fields.structure;
+      for (unsigned i = 0; i < length; i++, field++) {
+         mem.write(field, sizeof(glsl_struct_field));
+         mem.write_string(field->name);
+         field->type->serialize(mem);
+      }
+   }
+
+serilization_epilogue:
+   /* Update the length of written data in 'start_pos'. */
+   data_len = mem.position() - start_pos - sizeof(data_len);
+   mem.overwrite(&data_len, sizeof(data_len), start_pos);
+}
+
+
 bool
 glsl_type::contains_sampler() const
 {
diff --git a/src/glsl/glsl_types.h b/src/glsl/glsl_types.h
index dca5492..bc2dffe 100644
--- a/src/glsl/glsl_types.h
+++ b/src/glsl/glsl_types.h
@@ -82,6 +82,7 @@ enum glsl_interface_packing {
 #ifdef __cplusplus
 #include "GL/gl.h"
 #include "ralloc.h"
+#include "memory_writer.h"
 
 struct glsl_type {
    GLenum gl_type;
@@ -122,6 +123,11 @@ struct glsl_type {
    }
 
    /**
+    * Serialization functionality used by binary shaders.
+    */
+   void serialize(memory_writer &mem) const;
+
+   /**
     * \name Vector and matrix element counts
     *
     * For scalars, each of these values will be 1.  For non-numeric types
-- 
1.8.3.1



More information about the mesa-dev mailing list