glsl: fix the type of ir_constant_data::u16
[mesa.git] / src / compiler / glsl / lower_buffer_access.cpp
index 69dc82bf26110b2808d27e0badf7ad8d63eb92b7..a6e2f741ebe66e925a55d359d00cc5df206b8061 100644 (file)
@@ -36,6 +36,7 @@
 #include "main/macros.h"
 #include "util/list.h"
 #include "glsl_parser_extras.h"
+#include "linker.h"
 
 using namespace ir_builder;
 
@@ -58,11 +59,11 @@ lower_buffer_access::emit_access(void *mem_ctx,
                                  ir_variable *base_offset,
                                  unsigned int deref_offset,
                                  bool row_major,
-                                 int matrix_columns,
-                                 unsigned int packing,
+                                 const glsl_type *matrix_type,
+                                 enum glsl_interface_packing packing,
                                  unsigned int write_mask)
 {
-   if (deref->type->is_record()) {
+   if (deref->type->is_struct()) {
       unsigned int field_offset = 0;
 
       for (unsigned i = 0; i < deref->type->length; i++) {
@@ -72,16 +73,22 @@ lower_buffer_access::emit_access(void *mem_ctx,
             new(mem_ctx) ir_dereference_record(deref->clone(mem_ctx, NULL),
                                                field->name);
 
-         field_offset =
-            glsl_align(field_offset,
-                       field->type->std140_base_alignment(row_major));
+         unsigned field_align;
+         if (packing == GLSL_INTERFACE_PACKING_STD430)
+            field_align = field->type->std430_base_alignment(row_major);
+         else
+            field_align = field->type->std140_base_alignment(row_major);
+         field_offset = glsl_align(field_offset, field_align);
 
          emit_access(mem_ctx, is_write, field_deref, base_offset,
                      deref_offset + field_offset,
-                     row_major, 1, packing,
+                     row_major, NULL, packing,
                      writemask_for_size(field_deref->type->vector_elements));
 
-         field_offset += field->type->std140_size(row_major);
+         if (packing == GLSL_INTERFACE_PACKING_STD430)
+            field_offset += field->type->std430_size(row_major);
+         else
+            field_offset += field->type->std140_size(row_major);
       }
       return;
    }
@@ -98,7 +105,7 @@ lower_buffer_access::emit_access(void *mem_ctx,
                                               element);
          emit_access(mem_ctx, is_write, element_deref, base_offset,
                      deref_offset + i * array_stride,
-                     row_major, 1, packing,
+                     row_major, NULL, packing,
                      writemask_for_size(element_deref->type->vector_elements));
       }
       return;
@@ -110,42 +117,17 @@ lower_buffer_access::emit_access(void *mem_ctx,
          ir_dereference *col_deref =
             new(mem_ctx) ir_dereference_array(deref->clone(mem_ctx, NULL), col);
 
-         if (row_major) {
-            /* For a row-major matrix, the next column starts at the next
-             * element.
-             */
-            int size_mul = deref->type->is_64bit() ? 8 : 4;
-            emit_access(mem_ctx, is_write, col_deref, base_offset,
-                        deref_offset + i * size_mul,
-                        row_major, deref->type->matrix_columns, packing,
-                        writemask_for_size(col_deref->type->vector_elements));
-         } else {
-            int size_mul;
-
-            /* std430 doesn't round up vec2 size to a vec4 size */
-            if (packing == GLSL_INTERFACE_PACKING_STD430 &&
-                deref->type->vector_elements == 2 &&
-                !deref->type->is_64bit()) {
-               size_mul = 8;
-            } else {
-               /* std140 always rounds the stride of arrays (and matrices) to a
-                * vec4, so matrices are always 16 between columns/rows. With
-                * doubles, they will be 32 apart when there are more than 2 rows.
-                *
-                * For both std140 and std430, if the member is a
-                * three-'component vector with components consuming N basic
-                * machine units, the base alignment is 4N. For vec4, base
-                * alignment is 4N.
-                */
-               size_mul = (deref->type->is_64bit() &&
-                           deref->type->vector_elements > 2) ? 32 : 16;
-            }
-
-            emit_access(mem_ctx, is_write, col_deref, base_offset,
-                        deref_offset + i * size_mul,
-                        row_major, deref->type->matrix_columns, packing,
-                        writemask_for_size(col_deref->type->vector_elements));
-         }
+         /* For a row-major matrix, the next column starts at the next
+          * element.  Otherwise it is offset by the matrix stride.
+          */
+         const unsigned size_mul = row_major
+            ? (deref->type->is_double() ? 8 : 4)
+            : link_calculate_matrix_stride(deref->type, row_major, packing);
+
+         emit_access(mem_ctx, is_write, col_deref, base_offset,
+                     deref_offset + i * size_mul,
+                     row_major, deref->type, packing,
+                     writemask_for_size(col_deref->type->vector_elements));
       }
       return;
    }
@@ -159,48 +141,16 @@ lower_buffer_access::emit_access(void *mem_ctx,
          is_write ? write_mask : (1 << deref->type->vector_elements) - 1;
       insert_buffer_access(mem_ctx, deref, deref->type, offset, mask, -1);
    } else {
-      unsigned N = deref->type->is_64bit() ? 8 : 4;
-
       /* We're dereffing a column out of a row-major matrix, so we
        * gather the vector from each stored row.
-      */
-      assert(deref->type->base_type == GLSL_TYPE_FLOAT ||
-             deref->type->base_type == GLSL_TYPE_DOUBLE);
-      /* Matrices, row_major or not, are stored as if they were
-       * arrays of vectors of the appropriate size in std140.
-       * Arrays have their strides rounded up to a vec4, so the
-       * matrix stride is always 16. However a double matrix may either be 16
-       * or 32 depending on the number of columns.
        */
-      assert(matrix_columns <= 4);
-      unsigned matrix_stride = 0;
-      /* Matrix stride for std430 mat2xY matrices are not rounded up to
-       * vec4 size. From OpenGL 4.3 spec, section 7.6.2.2 "Standard Uniform
-       * Block Layout":
-       *
-       * "2. If the member is a two- or four-component vector with components
-       * consuming N basic machine units, the base alignment is 2N or 4N,
-       * respectively." [...]
-       * "4. If the member is an array of scalars or vectors, the base alignment
-       * and array stride are set to match the base alignment of a single array
-       * element, according to rules (1), (2), and (3), and rounded up to the
-       * base alignment of a vec4." [...]
-       * "7. If the member is a row-major matrix with C columns and R rows, the
-       * matrix is stored identically to an array of R row vectors with C
-       * components each, according to rule (4)." [...]
-       * "When using the std430 storage layout, shader storage blocks will be
-       * laid out in buffer storage identically to uniform and shader storage
-       * blocks using the std140 layout, except that the base alignment and
-       * stride of arrays of scalars and vectors in rule 4 and of structures in
-       * rule 9 are not rounded up a multiple of the base alignment of a vec4."
-       */
-      if (packing == GLSL_INTERFACE_PACKING_STD430 && matrix_columns == 2)
-         matrix_stride = 2 * N;
-      else
-         matrix_stride = glsl_align(matrix_columns * N, 16);
+      assert(deref->type->is_float() || deref->type->is_double());
+      assert(matrix_type != NULL);
+
+      const unsigned matrix_stride =
+         link_calculate_matrix_stride(matrix_type, row_major, packing);
 
-      const glsl_type *deref_type = deref->type->base_type == GLSL_TYPE_FLOAT ?
-         glsl_type::float_type : glsl_type::double_type;
+      const glsl_type *deref_type = deref->type->get_scalar_type();
 
       for (unsigned i = 0; i < deref->type->vector_elements; i++) {
          ir_rvalue *chan_offset =
@@ -252,7 +202,7 @@ lower_buffer_access::is_dereferenced_thing_row_major(const ir_rvalue *deref)
 
          ir = record_deref->record;
 
-         const int idx = ir->type->field_index(record_deref->field);
+         const int idx = record_deref->field_idx;
          assert(idx >= 0);
 
          const enum glsl_matrix_layout matrix_layout =
@@ -264,7 +214,7 @@ lower_buffer_access::is_dereferenced_thing_row_major(const ir_rvalue *deref)
          case GLSL_MATRIX_LAYOUT_COLUMN_MAJOR:
             return false;
          case GLSL_MATRIX_LAYOUT_ROW_MAJOR:
-            return matrix || deref->type->without_array()->is_record();
+            return matrix || deref->type->without_array()->is_struct();
          }
 
          break;
@@ -283,7 +233,7 @@ lower_buffer_access::is_dereferenced_thing_row_major(const ir_rvalue *deref)
              * layouts at HIR generation time, but we don't do that for shared
              * variables, which are always column-major
              */
-            MAYBE_UNUSED ir_variable *var = deref->variable_referenced();
+            ASSERTED ir_variable *var = deref->variable_referenced();
             assert((var->is_in_buffer_block() && !matrix) ||
                    var->data.mode == ir_var_shader_shared);
             return false;
@@ -291,7 +241,7 @@ lower_buffer_access::is_dereferenced_thing_row_major(const ir_rvalue *deref)
          case GLSL_MATRIX_LAYOUT_COLUMN_MAJOR:
             return false;
          case GLSL_MATRIX_LAYOUT_ROW_MAJOR:
-            return matrix || deref->type->without_array()->is_record();
+            return matrix || deref->type->without_array()->is_struct();
          }
 
          unreachable("invalid matrix layout");
@@ -321,18 +271,17 @@ lower_buffer_access::is_dereferenced_thing_row_major(const ir_rvalue *deref)
  */
 void
 lower_buffer_access::setup_buffer_access(void *mem_ctx,
-                                         ir_variable *var,
                                          ir_rvalue *deref,
                                          ir_rvalue **offset,
                                          unsigned *const_offset,
                                          bool *row_major,
-                                         int *matrix_columns,
+                                         const glsl_type **matrix_type,
                                          const glsl_struct_field **struct_field,
                                          enum glsl_interface_packing packing)
 {
    *offset = new(mem_ctx) ir_constant(0u);
    *row_major = is_dereferenced_thing_row_major(deref);
-   *matrix_columns = 1;
+   *matrix_type = NULL;
 
    /* Calculate the offset to the start of the region of the UBO
     * dereferenced by *rvalue.  This may be a variable offset if an
@@ -369,7 +318,7 @@ lower_buffer_access::setup_buffer_access(void *mem_ctx,
             array_stride = 4;
             if (deref_array->array->type->is_64bit())
                array_stride *= 2;
-            *matrix_columns = deref_array->array->type->matrix_columns;
+            *matrix_type = deref_array->array->type;
          } else if (deref_array->type->without_array()->is_interface()) {
             /* We're processing an array dereference of an interface instance
              * array. The thing being dereferenced *must* be a variable
@@ -405,7 +354,7 @@ lower_buffer_access::setup_buffer_access(void *mem_ctx,
             array_index = i2u(array_index);
 
          ir_constant *const_index =
-            array_index->constant_expression_value(NULL);
+            array_index->constant_expression_value(mem_ctx, NULL);
          if (const_index) {
             *const_offset += array_stride * const_index->value.u[0];
          } else {
@@ -446,8 +395,8 @@ lower_buffer_access::setup_buffer_access(void *mem_ctx,
 
             intra_struct_offset = glsl_align(intra_struct_offset, field_align);
 
-            if (strcmp(struct_type->fields.structure[i].name,
-                       deref_record->field) == 0) {
+            assert(deref_record->field_idx >= 0);
+            if (i == (unsigned) deref_record->field_idx) {
                if (struct_field)
                   *struct_field = &struct_type->fields.structure[i];
                break;
@@ -465,7 +414,7 @@ lower_buffer_access::setup_buffer_access(void *mem_ctx,
              *     of the member following the sub-structure is rounded up to
              *     the next multiple of the base alignment of the structure."
              */
-            if (type->without_array()->is_record()) {
+            if (type->without_array()->is_struct()) {
                intra_struct_offset = glsl_align(intra_struct_offset,
                                                 field_align);