[Mesa-dev] [PATCH 11/16] glsl: Track matrix layout of structure fields using two bits

Ian Romanick idr at freedesktop.org
Mon Jul 21 14:04:27 PDT 2014


From: Ian Romanick <ian.d.romanick at intel.com>

Signed-off-by: Ian Romanick <ian.d.romanick at intel.com>
---
 src/glsl/ast_to_hir.cpp        |  8 +++--
 src/glsl/builtin_types.cpp     | 74 +++++++++++++++++++++---------------------
 src/glsl/builtin_variables.cpp |  2 +-
 src/glsl/glsl_types.cpp        | 32 ++++++++++++++----
 src/glsl/glsl_types.h          | 12 ++++++-
 src/glsl/link_uniforms.cpp     |  3 +-
 6 files changed, 81 insertions(+), 50 deletions(-)

diff --git a/src/glsl/ast_to_hir.cpp b/src/glsl/ast_to_hir.cpp
index b11b0e6..1fb4274 100644
--- a/src/glsl/ast_to_hir.cpp
+++ b/src/glsl/ast_to_hir.cpp
@@ -5204,11 +5204,13 @@ ast_process_structure_or_interface_block(exec_list *instructions,
          }
 
          if (field_type->is_matrix_or_array_of()) {
-            fields[i].row_major = block_row_major;
+            fields[i].matrix_layout = block_row_major
+               ? GLSL_MATRIX_LAYOUT_ROW_MAJOR
+               : GLSL_MATRIX_LAYOUT_COLUMN_MAJOR;
             if (qual->flags.q.row_major)
-               fields[i].row_major = true;
+               fields[i].matrix_layout = GLSL_MATRIX_LAYOUT_ROW_MAJOR;
             else if (qual->flags.q.column_major)
-               fields[i].row_major = false;
+               fields[i].matrix_layout = GLSL_MATRIX_LAYOUT_COLUMN_MAJOR;
          }
 
          i++;
diff --git a/src/glsl/builtin_types.cpp b/src/glsl/builtin_types.cpp
index 0a0fa8c..54a589f 100644
--- a/src/glsl/builtin_types.cpp
+++ b/src/glsl/builtin_types.cpp
@@ -53,64 +53,64 @@
       &glsl_type::_struct_##NAME##_type;
 
 static const struct glsl_struct_field gl_DepthRangeParameters_fields[] = {
-   { glsl_type::float_type, "near", false, -1 },
-   { glsl_type::float_type, "far",  false, -1 },
-   { glsl_type::float_type, "diff", false, -1 },
+   { glsl_type::float_type, "near", -1, 0, 0, 0, GLSL_MATRIX_LAYOUT_DEFAULT, 0 },
+   { glsl_type::float_type, "far",  -1, 0, 0, 0, GLSL_MATRIX_LAYOUT_DEFAULT, 0 },
+   { glsl_type::float_type, "diff", -1, 0, 0, 0, GLSL_MATRIX_LAYOUT_DEFAULT, 0 },
 };
 
 static const struct glsl_struct_field gl_PointParameters_fields[] = {
-   { glsl_type::float_type, "size", false, -1 },
-   { glsl_type::float_type, "sizeMin", false, -1 },
-   { glsl_type::float_type, "sizeMax", false, -1 },
-   { glsl_type::float_type, "fadeThresholdSize", false, -1 },
-   { glsl_type::float_type, "distanceConstantAttenuation", false, -1 },
-   { glsl_type::float_type, "distanceLinearAttenuation", false, -1 },
-   { glsl_type::float_type, "distanceQuadraticAttenuation", false, -1 },
+   { glsl_type::float_type, "size", -1, 0, 0, 0, GLSL_MATRIX_LAYOUT_DEFAULT, 0 },
+   { glsl_type::float_type, "sizeMin", -1, 0, 0, 0, GLSL_MATRIX_LAYOUT_DEFAULT, 0 },
+   { glsl_type::float_type, "sizeMax", -1, 0, 0, 0, GLSL_MATRIX_LAYOUT_DEFAULT, 0 },
+   { glsl_type::float_type, "fadeThresholdSize", -1, 0, 0, 0, GLSL_MATRIX_LAYOUT_DEFAULT, 0 },
+   { glsl_type::float_type, "distanceConstantAttenuation", -1, 0, 0, 0, GLSL_MATRIX_LAYOUT_DEFAULT, 0 },
+   { glsl_type::float_type, "distanceLinearAttenuation", -1, 0, 0, 0, GLSL_MATRIX_LAYOUT_DEFAULT, 0 },
+   { glsl_type::float_type, "distanceQuadraticAttenuation", -1, 0, 0, 0, GLSL_MATRIX_LAYOUT_DEFAULT, 0 },
 };
 
 static const struct glsl_struct_field gl_MaterialParameters_fields[] = {
-   { glsl_type::vec4_type, "emission", false, -1 },
-   { glsl_type::vec4_type, "ambient", false, -1 },
-   { glsl_type::vec4_type, "diffuse", false, -1 },
-   { glsl_type::vec4_type, "specular", false, -1 },
-   { glsl_type::float_type, "shininess", false, -1 },
+   { glsl_type::vec4_type, "emission", -1, 0, 0, 0, GLSL_MATRIX_LAYOUT_DEFAULT, 0 },
+   { glsl_type::vec4_type, "ambient", -1, 0, 0, 0, GLSL_MATRIX_LAYOUT_DEFAULT, 0 },
+   { glsl_type::vec4_type, "diffuse", -1, 0, 0, 0, GLSL_MATRIX_LAYOUT_DEFAULT, 0 },
+   { glsl_type::vec4_type, "specular", -1, 0, 0, 0, GLSL_MATRIX_LAYOUT_DEFAULT, 0 },
+   { glsl_type::float_type, "shininess", -1, 0, 0, 0, GLSL_MATRIX_LAYOUT_DEFAULT, 0 },
 };
 
 static const struct glsl_struct_field gl_LightSourceParameters_fields[] = {
-   { glsl_type::vec4_type, "ambient", false, -1 },
-   { glsl_type::vec4_type, "diffuse", false, -1 },
-   { glsl_type::vec4_type, "specular", false, -1 },
-   { glsl_type::vec4_type, "position", false, -1 },
-   { glsl_type::vec4_type, "halfVector", false, -1 },
-   { glsl_type::vec3_type, "spotDirection", false, -1 },
-   { glsl_type::float_type, "spotExponent", false, -1 },
-   { glsl_type::float_type, "spotCutoff", false, -1 },
-   { glsl_type::float_type, "spotCosCutoff", false, -1 },
-   { glsl_type::float_type, "constantAttenuation", false, -1 },
-   { glsl_type::float_type, "linearAttenuation", false, -1 },
-   { glsl_type::float_type, "quadraticAttenuation", false, -1 },
+   { glsl_type::vec4_type, "ambient", -1, 0, 0, 0, GLSL_MATRIX_LAYOUT_DEFAULT, 0 },
+   { glsl_type::vec4_type, "diffuse", -1, 0, 0, 0, GLSL_MATRIX_LAYOUT_DEFAULT, 0 },
+   { glsl_type::vec4_type, "specular", -1, 0, 0, 0, GLSL_MATRIX_LAYOUT_DEFAULT, 0 },
+   { glsl_type::vec4_type, "position", -1, 0, 0, 0, GLSL_MATRIX_LAYOUT_DEFAULT, 0 },
+   { glsl_type::vec4_type, "halfVector", -1, 0, 0, 0, GLSL_MATRIX_LAYOUT_DEFAULT, 0 },
+   { glsl_type::vec3_type, "spotDirection", -1, 0, 0, 0, GLSL_MATRIX_LAYOUT_DEFAULT, 0 },
+   { glsl_type::float_type, "spotExponent", -1, 0, 0, 0, GLSL_MATRIX_LAYOUT_DEFAULT, 0 },
+   { glsl_type::float_type, "spotCutoff", -1, 0, 0, 0, GLSL_MATRIX_LAYOUT_DEFAULT, 0 },
+   { glsl_type::float_type, "spotCosCutoff", -1, 0, 0, 0, GLSL_MATRIX_LAYOUT_DEFAULT, 0 },
+   { glsl_type::float_type, "constantAttenuation", -1, 0, 0, 0, GLSL_MATRIX_LAYOUT_DEFAULT, 0 },
+   { glsl_type::float_type, "linearAttenuation", -1, 0, 0, 0, GLSL_MATRIX_LAYOUT_DEFAULT, 0 },
+   { glsl_type::float_type, "quadraticAttenuation", -1, 0, 0, 0, GLSL_MATRIX_LAYOUT_DEFAULT, 0 },
 };
 
 static const struct glsl_struct_field gl_LightModelParameters_fields[] = {
-   { glsl_type::vec4_type, "ambient", false, -1 },
+   { glsl_type::vec4_type, "ambient", -1, 0, 0, 0, GLSL_MATRIX_LAYOUT_DEFAULT, 0 },
 };
 
 static const struct glsl_struct_field gl_LightModelProducts_fields[] = {
-   { glsl_type::vec4_type, "sceneColor", false, -1 },
+   { glsl_type::vec4_type, "sceneColor", -1, 0, 0, 0, GLSL_MATRIX_LAYOUT_DEFAULT, 0 },
 };
 
 static const struct glsl_struct_field gl_LightProducts_fields[] = {
-   { glsl_type::vec4_type, "ambient", false, -1 },
-   { glsl_type::vec4_type, "diffuse", false, -1 },
-   { glsl_type::vec4_type, "specular", false, -1 },
+   { glsl_type::vec4_type, "ambient", -1, 0, 0, 0, GLSL_MATRIX_LAYOUT_DEFAULT, 0 },
+   { glsl_type::vec4_type, "diffuse", -1, 0, 0, 0, GLSL_MATRIX_LAYOUT_DEFAULT, 0 },
+   { glsl_type::vec4_type, "specular", -1, 0, 0, 0, GLSL_MATRIX_LAYOUT_DEFAULT, 0 },
 };
 
 static const struct glsl_struct_field gl_FogParameters_fields[] = {
-   { glsl_type::vec4_type, "color", false, -1 },
-   { glsl_type::float_type, "density", false, -1 },
-   { glsl_type::float_type, "start", false, -1 },
-   { glsl_type::float_type, "end", false, -1 },
-   { glsl_type::float_type, "scale", false, -1 },
+   { glsl_type::vec4_type, "color", -1, 0, 0, 0, GLSL_MATRIX_LAYOUT_DEFAULT, 0 },
+   { glsl_type::float_type, "density", -1, 0, 0, 0, GLSL_MATRIX_LAYOUT_DEFAULT, 0 },
+   { glsl_type::float_type, "start", -1, 0, 0, 0, GLSL_MATRIX_LAYOUT_DEFAULT, 0 },
+   { glsl_type::float_type, "end", -1, 0, 0, 0, GLSL_MATRIX_LAYOUT_DEFAULT, 0 },
+   { glsl_type::float_type, "scale", -1, 0, 0, 0, GLSL_MATRIX_LAYOUT_DEFAULT, 0 },
 };
 
 #include "builtin_type_macros.h"
diff --git a/src/glsl/builtin_variables.cpp b/src/glsl/builtin_variables.cpp
index 3497302..93cd059 100644
--- a/src/glsl/builtin_variables.cpp
+++ b/src/glsl/builtin_variables.cpp
@@ -317,7 +317,7 @@ per_vertex_accumulator::add_field(int slot, const glsl_type *type,
    assert(this->num_fields < ARRAY_SIZE(this->fields));
    this->fields[this->num_fields].type = type;
    this->fields[this->num_fields].name = name;
-   this->fields[this->num_fields].row_major = false;
+   this->fields[this->num_fields].matrix_layout = GLSL_MATRIX_LAYOUT_DEFAULT;
    this->fields[this->num_fields].location = slot;
    this->fields[this->num_fields].interpolation = INTERP_QUALIFIER_NONE;
    this->fields[this->num_fields].centroid = 0;
diff --git a/src/glsl/glsl_types.cpp b/src/glsl/glsl_types.cpp
index 01a84b1..1719cfe 100644
--- a/src/glsl/glsl_types.cpp
+++ b/src/glsl/glsl_types.cpp
@@ -108,7 +108,7 @@ glsl_type::glsl_type(const glsl_struct_field *fields, unsigned num_fields,
       this->fields.structure[i].interpolation = fields[i].interpolation;
       this->fields.structure[i].centroid = fields[i].centroid;
       this->fields.structure[i].sample = fields[i].sample;
-      this->fields.structure[i].row_major = fields[i].row_major;
+      this->fields.structure[i].matrix_layout = fields[i].matrix_layout;
    }
 }
 
@@ -136,7 +136,7 @@ glsl_type::glsl_type(const glsl_struct_field *fields, unsigned num_fields,
       this->fields.structure[i].interpolation = fields[i].interpolation;
       this->fields.structure[i].centroid = fields[i].centroid;
       this->fields.structure[i].sample = fields[i].sample;
-      this->fields.structure[i].row_major = fields[i].row_major;
+      this->fields.structure[i].matrix_layout = fields[i].matrix_layout;
    }
 }
 
@@ -496,8 +496,8 @@ glsl_type::record_compare(const glsl_type *b) const
       if (strcmp(this->fields.structure[i].name,
 		 b->fields.structure[i].name) != 0)
 	 return false;
-      if (this->fields.structure[i].row_major
-         != b->fields.structure[i].row_major)
+      if (this->fields.structure[i].matrix_layout
+         != b->fields.structure[i].matrix_layout)
         return false;
       if (this->fields.structure[i].location
           != b->fields.structure[i].location)
@@ -826,9 +826,18 @@ glsl_type::std140_base_alignment(bool row_major) const
    if (this->is_record()) {
       unsigned base_alignment = 16;
       for (unsigned i = 0; i < this->length; i++) {
+         bool field_row_major = row_major;
+         const enum glsl_matrix_layout matrix_layout =
+            glsl_matrix_layout(this->fields.structure[i].matrix_layout);
+         if (matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR) {
+            field_row_major = true;
+         } else if (matrix_layout == GLSL_MATRIX_LAYOUT_COLUMN_MAJOR) {
+            field_row_major = false;
+         }
+
 	 const struct glsl_type *field_type = this->fields.structure[i].type;
 	 base_alignment = MAX2(base_alignment,
-			       field_type->std140_base_alignment(row_major));
+			       field_type->std140_base_alignment(field_row_major));
       }
       return base_alignment;
    }
@@ -937,10 +946,19 @@ glsl_type::std140_size(bool row_major) const
       unsigned max_align = 0;
 
       for (unsigned i = 0; i < this->length; i++) {
+         bool field_row_major = row_major;
+         const enum glsl_matrix_layout matrix_layout =
+            glsl_matrix_layout(this->fields.structure[i].matrix_layout);
+         if (matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR) {
+            field_row_major = true;
+         } else if (matrix_layout == GLSL_MATRIX_LAYOUT_COLUMN_MAJOR) {
+            field_row_major = false;
+         }
+
 	 const struct glsl_type *field_type = this->fields.structure[i].type;
-	 unsigned align = field_type->std140_base_alignment(row_major);
+	 unsigned align = field_type->std140_base_alignment(field_row_major);
 	 size = glsl_align(size, align);
-	 size += field_type->std140_size(row_major);
+	 size += field_type->std140_size(field_row_major);
 
          max_align = MAX2(align, max_align);
 
diff --git a/src/glsl/glsl_types.h b/src/glsl/glsl_types.h
index 2dfa8dd..7981f55 100644
--- a/src/glsl/glsl_types.h
+++ b/src/glsl/glsl_types.h
@@ -79,6 +79,12 @@ enum glsl_interface_packing {
    GLSL_INTERFACE_PACKING_PACKED
 };
 
+enum glsl_matrix_layout {
+   GLSL_MATRIX_LAYOUT_DEFAULT,
+   GLSL_MATRIX_LAYOUT_COLUMN_MAJOR,
+   GLSL_MATRIX_LAYOUT_ROW_MAJOR
+};
+
 #ifdef __cplusplus
 #include "GL/gl.h"
 #include "ralloc.h"
@@ -667,7 +673,6 @@ private:
 struct glsl_struct_field {
    const struct glsl_type *type;
    const char *name;
-   bool row_major;
 
    /**
     * For interface blocks, gl_varying_slot corresponding to the input/output
@@ -697,6 +702,11 @@ struct glsl_struct_field {
    unsigned sample:1;
 
    /**
+    * Layout of the matrix.  Uses glsl_matrix_layout values.
+    */
+   unsigned matrix_layout:2;
+
+   /**
     * For interface blocks, it has a value if this variable uses multiple vertex
     * streams (as in ir_variable::stream). -1 otherwise.
     */
diff --git a/src/glsl/link_uniforms.cpp b/src/glsl/link_uniforms.cpp
index c88ecbd..68553f1 100644
--- a/src/glsl/link_uniforms.cpp
+++ b/src/glsl/link_uniforms.cpp
@@ -181,7 +181,8 @@ program_resource_visitor::recursion(const glsl_type *t, char **name,
          }
 
          recursion(t->fields.structure[i].type, name, new_length,
-                   t->fields.structure[i].row_major, record_type,
+                   t->fields.structure[i].matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR,
+                   record_type,
                    (i + 1) == t->length);
 
          /* Only the first leaf-field of the record gets called with the
-- 
1.8.1.4



More information about the mesa-dev mailing list