mesa: Add glsl_type::get_scalar_type() function.
authorPaul Berry <stereotype441@gmail.com>
Tue, 25 Oct 2011 23:24:03 +0000 (16:24 -0700)
committerPaul Berry <stereotype441@gmail.com>
Mon, 31 Oct 2011 18:29:14 +0000 (11:29 -0700)
This function is similar to get_base_type(), but when called on
arrays, it returns the scalar type composing the array.  For example,
glsl_type(vec4[]) => float_type.

Acked-by: Kenneth Graunke <kenneth@whitecape.org>
src/glsl/glsl_types.cpp
src/glsl/glsl_types.h

index c94aec0d2da8352772aecc284b9d308eab3b5174..03e99879e8ebfa4c40228bfb42b92345dae825bb 100644 (file)
@@ -258,6 +258,29 @@ const glsl_type *glsl_type::get_base_type() const
 }
 
 
+const glsl_type *glsl_type::get_scalar_type() const
+{
+   const glsl_type *type = this;
+
+   /* Handle arrays */
+   while (type->base_type == GLSL_TYPE_ARRAY)
+      type = type->fields.array;
+
+   /* Handle vectors and matrices */
+   switch (type->base_type) {
+   case GLSL_TYPE_UINT:
+      return uint_type;
+   case GLSL_TYPE_INT:
+      return int_type;
+   case GLSL_TYPE_FLOAT:
+      return float_type;
+   default:
+      /* Handle everything else */
+      return type;
+   }
+}
+
+
 void
 _mesa_glsl_release_types(void)
 {
index 048696693be7fe18b27bf2ae4456de9d532aa0a4..2f849afba709b76c0b40c0b99b29d750155b215a 100644 (file)
@@ -177,6 +177,17 @@ struct glsl_type {
     */
    const glsl_type *get_base_type() const;
 
+   /**
+    * Get the basic scalar type which this type aggregates.
+    *
+    * If the type is a numeric or boolean scalar, vector, or matrix, or an
+    * array of any of those, this function gets the scalar type of the
+    * individual components.  For structs and arrays of structs, this function
+    * returns the struct type.  For samplers and arrays of samplers, this
+    * function returns the sampler type.
+    */
+   const glsl_type *get_scalar_type() const;
+
    /**
     * Query the type of elements in an array
     *