101e04c3fSmrg/*
201e04c3fSmrg * Copyright (c) 2015 Intel Corporation
301e04c3fSmrg *
401e04c3fSmrg * Permission is hereby granted, free of charge, to any person obtaining a
501e04c3fSmrg * copy of this software and associated documentation files (the "Software"),
601e04c3fSmrg * to deal in the Software without restriction, including without limitation
701e04c3fSmrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
801e04c3fSmrg * and/or sell copies of the Software, and to permit persons to whom the
901e04c3fSmrg * Software is furnished to do so, subject to the following conditions:
1001e04c3fSmrg *
1101e04c3fSmrg * The above copyright notice and this permission notice (including the next
1201e04c3fSmrg * paragraph) shall be included in all copies or substantial portions of the
1301e04c3fSmrg * Software.
1401e04c3fSmrg *
1501e04c3fSmrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1601e04c3fSmrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1701e04c3fSmrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
1801e04c3fSmrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1901e04c3fSmrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
2001e04c3fSmrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
2101e04c3fSmrg * DEALINGS IN THE SOFTWARE.
2201e04c3fSmrg */
2301e04c3fSmrg
2401e04c3fSmrg/**
2501e04c3fSmrg * \file lower_buffer_access.cpp
2601e04c3fSmrg *
2701e04c3fSmrg * Helper for IR lowering pass to replace dereferences of buffer object based
2801e04c3fSmrg * shader variables with intrinsic function calls.
2901e04c3fSmrg *
3001e04c3fSmrg * This helper is used by lowering passes for UBOs, SSBOs and compute shader
3101e04c3fSmrg * shared variables.
3201e04c3fSmrg */
3301e04c3fSmrg
3401e04c3fSmrg#include "lower_buffer_access.h"
3501e04c3fSmrg#include "ir_builder.h"
3601e04c3fSmrg#include "main/macros.h"
3701e04c3fSmrg#include "util/list.h"
3801e04c3fSmrg#include "glsl_parser_extras.h"
3901e04c3fSmrg#include "linker.h"
4001e04c3fSmrg
4101e04c3fSmrgusing namespace ir_builder;
4201e04c3fSmrg
4301e04c3fSmrgnamespace lower_buffer_access {
4401e04c3fSmrg
4501e04c3fSmrgstatic inline int
4601e04c3fSmrgwritemask_for_size(unsigned n)
4701e04c3fSmrg{
4801e04c3fSmrg   return ((1 << n) - 1);
4901e04c3fSmrg}
5001e04c3fSmrg
5101e04c3fSmrg/**
5201e04c3fSmrg * Takes a deref and recursively calls itself to break the deref down to the
5301e04c3fSmrg * point that the reads or writes generated are contiguous scalars or vectors.
5401e04c3fSmrg */
5501e04c3fSmrgvoid
5601e04c3fSmrglower_buffer_access::emit_access(void *mem_ctx,
5701e04c3fSmrg                                 bool is_write,
5801e04c3fSmrg                                 ir_dereference *deref,
5901e04c3fSmrg                                 ir_variable *base_offset,
6001e04c3fSmrg                                 unsigned int deref_offset,
6101e04c3fSmrg                                 bool row_major,
6201e04c3fSmrg                                 const glsl_type *matrix_type,
6301e04c3fSmrg                                 enum glsl_interface_packing packing,
6401e04c3fSmrg                                 unsigned int write_mask)
6501e04c3fSmrg{
667e102996Smaya   if (deref->type->is_struct()) {
6701e04c3fSmrg      unsigned int field_offset = 0;
6801e04c3fSmrg
6901e04c3fSmrg      for (unsigned i = 0; i < deref->type->length; i++) {
7001e04c3fSmrg         const struct glsl_struct_field *field =
7101e04c3fSmrg            &deref->type->fields.structure[i];
7201e04c3fSmrg         ir_dereference *field_deref =
7301e04c3fSmrg            new(mem_ctx) ir_dereference_record(deref->clone(mem_ctx, NULL),
7401e04c3fSmrg                                               field->name);
7501e04c3fSmrg
7601e04c3fSmrg         unsigned field_align;
7701e04c3fSmrg         if (packing == GLSL_INTERFACE_PACKING_STD430)
7801e04c3fSmrg            field_align = field->type->std430_base_alignment(row_major);
7901e04c3fSmrg         else
8001e04c3fSmrg            field_align = field->type->std140_base_alignment(row_major);
8101e04c3fSmrg         field_offset = glsl_align(field_offset, field_align);
8201e04c3fSmrg
8301e04c3fSmrg         emit_access(mem_ctx, is_write, field_deref, base_offset,
8401e04c3fSmrg                     deref_offset + field_offset,
8501e04c3fSmrg                     row_major, NULL, packing,
8601e04c3fSmrg                     writemask_for_size(field_deref->type->vector_elements));
8701e04c3fSmrg
8801e04c3fSmrg         if (packing == GLSL_INTERFACE_PACKING_STD430)
8901e04c3fSmrg            field_offset += field->type->std430_size(row_major);
9001e04c3fSmrg         else
9101e04c3fSmrg            field_offset += field->type->std140_size(row_major);
9201e04c3fSmrg      }
9301e04c3fSmrg      return;
9401e04c3fSmrg   }
9501e04c3fSmrg
9601e04c3fSmrg   if (deref->type->is_array()) {
9701e04c3fSmrg      unsigned array_stride = packing == GLSL_INTERFACE_PACKING_STD430 ?
9801e04c3fSmrg         deref->type->fields.array->std430_array_stride(row_major) :
9901e04c3fSmrg         glsl_align(deref->type->fields.array->std140_size(row_major), 16);
10001e04c3fSmrg
10101e04c3fSmrg      for (unsigned i = 0; i < deref->type->length; i++) {
10201e04c3fSmrg         ir_constant *element = new(mem_ctx) ir_constant(i);
10301e04c3fSmrg         ir_dereference *element_deref =
10401e04c3fSmrg            new(mem_ctx) ir_dereference_array(deref->clone(mem_ctx, NULL),
10501e04c3fSmrg                                              element);
10601e04c3fSmrg         emit_access(mem_ctx, is_write, element_deref, base_offset,
10701e04c3fSmrg                     deref_offset + i * array_stride,
10801e04c3fSmrg                     row_major, NULL, packing,
10901e04c3fSmrg                     writemask_for_size(element_deref->type->vector_elements));
11001e04c3fSmrg      }
11101e04c3fSmrg      return;
11201e04c3fSmrg   }
11301e04c3fSmrg
11401e04c3fSmrg   if (deref->type->is_matrix()) {
11501e04c3fSmrg      for (unsigned i = 0; i < deref->type->matrix_columns; i++) {
11601e04c3fSmrg         ir_constant *col = new(mem_ctx) ir_constant(i);
11701e04c3fSmrg         ir_dereference *col_deref =
11801e04c3fSmrg            new(mem_ctx) ir_dereference_array(deref->clone(mem_ctx, NULL), col);
11901e04c3fSmrg
12001e04c3fSmrg         /* For a row-major matrix, the next column starts at the next
12101e04c3fSmrg          * element.  Otherwise it is offset by the matrix stride.
12201e04c3fSmrg          */
12301e04c3fSmrg         const unsigned size_mul = row_major
12401e04c3fSmrg            ? (deref->type->is_double() ? 8 : 4)
12501e04c3fSmrg            : link_calculate_matrix_stride(deref->type, row_major, packing);
12601e04c3fSmrg
12701e04c3fSmrg         emit_access(mem_ctx, is_write, col_deref, base_offset,
12801e04c3fSmrg                     deref_offset + i * size_mul,
12901e04c3fSmrg                     row_major, deref->type, packing,
13001e04c3fSmrg                     writemask_for_size(col_deref->type->vector_elements));
13101e04c3fSmrg      }
13201e04c3fSmrg      return;
13301e04c3fSmrg   }
13401e04c3fSmrg
13501e04c3fSmrg   assert(deref->type->is_scalar() || deref->type->is_vector());
13601e04c3fSmrg
13701e04c3fSmrg   if (!row_major) {
13801e04c3fSmrg      ir_rvalue *offset =
13901e04c3fSmrg         add(base_offset, new(mem_ctx) ir_constant(deref_offset));
14001e04c3fSmrg      unsigned mask =
14101e04c3fSmrg         is_write ? write_mask : (1 << deref->type->vector_elements) - 1;
14201e04c3fSmrg      insert_buffer_access(mem_ctx, deref, deref->type, offset, mask, -1);
14301e04c3fSmrg   } else {
14401e04c3fSmrg      /* We're dereffing a column out of a row-major matrix, so we
14501e04c3fSmrg       * gather the vector from each stored row.
14601e04c3fSmrg       */
14701e04c3fSmrg      assert(deref->type->is_float() || deref->type->is_double());
14801e04c3fSmrg      assert(matrix_type != NULL);
14901e04c3fSmrg
15001e04c3fSmrg      const unsigned matrix_stride =
15101e04c3fSmrg         link_calculate_matrix_stride(matrix_type, row_major, packing);
15201e04c3fSmrg
15301e04c3fSmrg      const glsl_type *deref_type = deref->type->get_scalar_type();
15401e04c3fSmrg
15501e04c3fSmrg      for (unsigned i = 0; i < deref->type->vector_elements; i++) {
15601e04c3fSmrg         ir_rvalue *chan_offset =
15701e04c3fSmrg            add(base_offset,
15801e04c3fSmrg                new(mem_ctx) ir_constant(deref_offset + i * matrix_stride));
15901e04c3fSmrg         if (!is_write || ((1U << i) & write_mask))
16001e04c3fSmrg            insert_buffer_access(mem_ctx, deref, deref_type, chan_offset,
16101e04c3fSmrg                                 (1U << i), i);
16201e04c3fSmrg      }
16301e04c3fSmrg   }
16401e04c3fSmrg}
16501e04c3fSmrg
16601e04c3fSmrg/**
16701e04c3fSmrg * Determine if a thing being dereferenced is row-major
16801e04c3fSmrg *
16901e04c3fSmrg * There is some trickery here.
17001e04c3fSmrg *
17101e04c3fSmrg * If the thing being dereferenced is a member of uniform block \b without an
17201e04c3fSmrg * instance name, then the name of the \c ir_variable is the field name of an
17301e04c3fSmrg * interface type.  If this field is row-major, then the thing referenced is
17401e04c3fSmrg * row-major.
17501e04c3fSmrg *
17601e04c3fSmrg * If the thing being dereferenced is a member of uniform block \b with an
17701e04c3fSmrg * instance name, then the last dereference in the tree will be an
17801e04c3fSmrg * \c ir_dereference_record.  If that record field is row-major, then the
17901e04c3fSmrg * thing referenced is row-major.
18001e04c3fSmrg */
18101e04c3fSmrgbool
18201e04c3fSmrglower_buffer_access::is_dereferenced_thing_row_major(const ir_rvalue *deref)
18301e04c3fSmrg{
18401e04c3fSmrg   bool matrix = false;
18501e04c3fSmrg   const ir_rvalue *ir = deref;
18601e04c3fSmrg
18701e04c3fSmrg   while (true) {
18801e04c3fSmrg      matrix = matrix || ir->type->without_array()->is_matrix();
18901e04c3fSmrg
19001e04c3fSmrg      switch (ir->ir_type) {
19101e04c3fSmrg      case ir_type_dereference_array: {
19201e04c3fSmrg         const ir_dereference_array *const array_deref =
19301e04c3fSmrg            (const ir_dereference_array *) ir;
19401e04c3fSmrg
19501e04c3fSmrg         ir = array_deref->array;
19601e04c3fSmrg         break;
19701e04c3fSmrg      }
19801e04c3fSmrg
19901e04c3fSmrg      case ir_type_dereference_record: {
20001e04c3fSmrg         const ir_dereference_record *const record_deref =
20101e04c3fSmrg            (const ir_dereference_record *) ir;
20201e04c3fSmrg
20301e04c3fSmrg         ir = record_deref->record;
20401e04c3fSmrg
20501e04c3fSmrg         const int idx = record_deref->field_idx;
20601e04c3fSmrg         assert(idx >= 0);
20701e04c3fSmrg
20801e04c3fSmrg         const enum glsl_matrix_layout matrix_layout =
20901e04c3fSmrg            glsl_matrix_layout(ir->type->fields.structure[idx].matrix_layout);
21001e04c3fSmrg
21101e04c3fSmrg         switch (matrix_layout) {
21201e04c3fSmrg         case GLSL_MATRIX_LAYOUT_INHERITED:
21301e04c3fSmrg            break;
21401e04c3fSmrg         case GLSL_MATRIX_LAYOUT_COLUMN_MAJOR:
21501e04c3fSmrg            return false;
21601e04c3fSmrg         case GLSL_MATRIX_LAYOUT_ROW_MAJOR:
2177e102996Smaya            return matrix || deref->type->without_array()->is_struct();
21801e04c3fSmrg         }
21901e04c3fSmrg
22001e04c3fSmrg         break;
22101e04c3fSmrg      }
22201e04c3fSmrg
22301e04c3fSmrg      case ir_type_dereference_variable: {
22401e04c3fSmrg         const ir_dereference_variable *const var_deref =
22501e04c3fSmrg            (const ir_dereference_variable *) ir;
22601e04c3fSmrg
22701e04c3fSmrg         const enum glsl_matrix_layout matrix_layout =
22801e04c3fSmrg            glsl_matrix_layout(var_deref->var->data.matrix_layout);
22901e04c3fSmrg
23001e04c3fSmrg         switch (matrix_layout) {
23101e04c3fSmrg         case GLSL_MATRIX_LAYOUT_INHERITED: {
23201e04c3fSmrg            /* For interface block matrix variables we handle inherited
23301e04c3fSmrg             * layouts at HIR generation time, but we don't do that for shared
23401e04c3fSmrg             * variables, which are always column-major
23501e04c3fSmrg             */
2367ec681f3Smrg            ASSERTED ir_variable *var = deref->variable_referenced();
23701e04c3fSmrg            assert((var->is_in_buffer_block() && !matrix) ||
23801e04c3fSmrg                   var->data.mode == ir_var_shader_shared);
23901e04c3fSmrg            return false;
24001e04c3fSmrg         }
24101e04c3fSmrg         case GLSL_MATRIX_LAYOUT_COLUMN_MAJOR:
24201e04c3fSmrg            return false;
24301e04c3fSmrg         case GLSL_MATRIX_LAYOUT_ROW_MAJOR:
2447e102996Smaya            return matrix || deref->type->without_array()->is_struct();
24501e04c3fSmrg         }
24601e04c3fSmrg
24701e04c3fSmrg         unreachable("invalid matrix layout");
24801e04c3fSmrg         break;
24901e04c3fSmrg      }
25001e04c3fSmrg
25101e04c3fSmrg      default:
25201e04c3fSmrg         return false;
25301e04c3fSmrg      }
25401e04c3fSmrg   }
25501e04c3fSmrg
25601e04c3fSmrg   /* The tree must have ended with a dereference that wasn't an
25701e04c3fSmrg    * ir_dereference_variable.  That is invalid, and it should be impossible.
25801e04c3fSmrg    */
25901e04c3fSmrg   unreachable("invalid dereference tree");
26001e04c3fSmrg   return false;
26101e04c3fSmrg}
26201e04c3fSmrg
26301e04c3fSmrg/**
26401e04c3fSmrg * This function initializes various values that will be used later by
26501e04c3fSmrg * emit_access when actually emitting loads or stores.
26601e04c3fSmrg *
26701e04c3fSmrg * Note: const_offset is an input as well as an output, clients must
26801e04c3fSmrg * initialize it to the offset of the variable in the underlying block, and
26901e04c3fSmrg * this function will adjust it by adding the constant offset of the member
27001e04c3fSmrg * being accessed into that variable.
27101e04c3fSmrg */
27201e04c3fSmrgvoid
27301e04c3fSmrglower_buffer_access::setup_buffer_access(void *mem_ctx,
27401e04c3fSmrg                                         ir_rvalue *deref,
27501e04c3fSmrg                                         ir_rvalue **offset,
27601e04c3fSmrg                                         unsigned *const_offset,
27701e04c3fSmrg                                         bool *row_major,
27801e04c3fSmrg                                         const glsl_type **matrix_type,
27901e04c3fSmrg                                         const glsl_struct_field **struct_field,
28001e04c3fSmrg                                         enum glsl_interface_packing packing)
28101e04c3fSmrg{
28201e04c3fSmrg   *offset = new(mem_ctx) ir_constant(0u);
28301e04c3fSmrg   *row_major = is_dereferenced_thing_row_major(deref);
28401e04c3fSmrg   *matrix_type = NULL;
28501e04c3fSmrg
28601e04c3fSmrg   /* Calculate the offset to the start of the region of the UBO
28701e04c3fSmrg    * dereferenced by *rvalue.  This may be a variable offset if an
28801e04c3fSmrg    * array dereference has a variable index.
28901e04c3fSmrg    */
29001e04c3fSmrg   while (deref) {
29101e04c3fSmrg      switch (deref->ir_type) {
29201e04c3fSmrg      case ir_type_dereference_variable: {
29301e04c3fSmrg         deref = NULL;
29401e04c3fSmrg         break;
29501e04c3fSmrg      }
29601e04c3fSmrg
29701e04c3fSmrg      case ir_type_dereference_array: {
29801e04c3fSmrg         ir_dereference_array *deref_array = (ir_dereference_array *) deref;
29901e04c3fSmrg         unsigned array_stride;
30001e04c3fSmrg         if (deref_array->array->type->is_vector()) {
30101e04c3fSmrg            /* We get this when storing or loading a component out of a vector
30201e04c3fSmrg             * with a non-constant index. This happens for v[i] = f where v is
30301e04c3fSmrg             * a vector (or m[i][j] = f where m is a matrix). If we don't
30401e04c3fSmrg             * lower that here, it gets turned into v = vector_insert(v, i,
30501e04c3fSmrg             * f), which loads the entire vector, modifies one component and
30601e04c3fSmrg             * then write the entire thing back.  That breaks if another
30701e04c3fSmrg             * thread or SIMD channel is modifying the same vector.
30801e04c3fSmrg             */
30901e04c3fSmrg            array_stride = 4;
31001e04c3fSmrg            if (deref_array->array->type->is_64bit())
31101e04c3fSmrg               array_stride *= 2;
31201e04c3fSmrg         } else if (deref_array->array->type->is_matrix() && *row_major) {
31301e04c3fSmrg            /* When loading a vector out of a row major matrix, the
31401e04c3fSmrg             * step between the columns (vectors) is the size of a
31501e04c3fSmrg             * float, while the step between the rows (elements of a
31601e04c3fSmrg             * vector) is handled below in emit_ubo_loads.
31701e04c3fSmrg             */
31801e04c3fSmrg            array_stride = 4;
31901e04c3fSmrg            if (deref_array->array->type->is_64bit())
32001e04c3fSmrg               array_stride *= 2;
32101e04c3fSmrg            *matrix_type = deref_array->array->type;
32201e04c3fSmrg         } else if (deref_array->type->without_array()->is_interface()) {
32301e04c3fSmrg            /* We're processing an array dereference of an interface instance
32401e04c3fSmrg             * array. The thing being dereferenced *must* be a variable
32501e04c3fSmrg             * dereference because interfaces cannot be embedded in other
32601e04c3fSmrg             * types. In terms of calculating the offsets for the lowering
32701e04c3fSmrg             * pass, we don't care about the array index. All elements of an
32801e04c3fSmrg             * interface instance array will have the same offsets relative to
32901e04c3fSmrg             * the base of the block that backs them.
33001e04c3fSmrg             */
33101e04c3fSmrg            deref = deref_array->array->as_dereference();
33201e04c3fSmrg            break;
33301e04c3fSmrg         } else {
33401e04c3fSmrg            /* Whether or not the field is row-major (because it might be a
33501e04c3fSmrg             * bvec2 or something) does not affect the array itself. We need
33601e04c3fSmrg             * to know whether an array element in its entirety is row-major.
33701e04c3fSmrg             */
33801e04c3fSmrg            const bool array_row_major =
33901e04c3fSmrg               is_dereferenced_thing_row_major(deref_array);
34001e04c3fSmrg
34101e04c3fSmrg            /* The array type will give the correct interface packing
34201e04c3fSmrg             * information
34301e04c3fSmrg             */
34401e04c3fSmrg            if (packing == GLSL_INTERFACE_PACKING_STD430) {
34501e04c3fSmrg               array_stride = deref_array->type->std430_array_stride(array_row_major);
34601e04c3fSmrg            } else {
34701e04c3fSmrg               array_stride = deref_array->type->std140_size(array_row_major);
34801e04c3fSmrg               array_stride = glsl_align(array_stride, 16);
34901e04c3fSmrg            }
35001e04c3fSmrg         }
35101e04c3fSmrg
35201e04c3fSmrg         ir_rvalue *array_index = deref_array->array_index;
35301e04c3fSmrg         if (array_index->type->base_type == GLSL_TYPE_INT)
35401e04c3fSmrg            array_index = i2u(array_index);
35501e04c3fSmrg
35601e04c3fSmrg         ir_constant *const_index =
35701e04c3fSmrg            array_index->constant_expression_value(mem_ctx, NULL);
35801e04c3fSmrg         if (const_index) {
35901e04c3fSmrg            *const_offset += array_stride * const_index->value.u[0];
36001e04c3fSmrg         } else {
36101e04c3fSmrg            *offset = add(*offset,
36201e04c3fSmrg                          mul(array_index,
36301e04c3fSmrg                              new(mem_ctx) ir_constant(array_stride)));
36401e04c3fSmrg         }
36501e04c3fSmrg         deref = deref_array->array->as_dereference();
36601e04c3fSmrg         break;
36701e04c3fSmrg      }
36801e04c3fSmrg
36901e04c3fSmrg      case ir_type_dereference_record: {
37001e04c3fSmrg         ir_dereference_record *deref_record = (ir_dereference_record *) deref;
37101e04c3fSmrg         const glsl_type *struct_type = deref_record->record->type;
37201e04c3fSmrg         unsigned intra_struct_offset = 0;
37301e04c3fSmrg
37401e04c3fSmrg         for (unsigned int i = 0; i < struct_type->length; i++) {
37501e04c3fSmrg            const glsl_type *type = struct_type->fields.structure[i].type;
37601e04c3fSmrg
37701e04c3fSmrg            ir_dereference_record *field_deref = new(mem_ctx)
37801e04c3fSmrg               ir_dereference_record(deref_record->record,
37901e04c3fSmrg                                     struct_type->fields.structure[i].name);
38001e04c3fSmrg            const bool field_row_major =
38101e04c3fSmrg               is_dereferenced_thing_row_major(field_deref);
38201e04c3fSmrg
38301e04c3fSmrg            ralloc_free(field_deref);
38401e04c3fSmrg
38501e04c3fSmrg            unsigned field_align = 0;
38601e04c3fSmrg
38701e04c3fSmrg            if (packing == GLSL_INTERFACE_PACKING_STD430)
38801e04c3fSmrg               field_align = type->std430_base_alignment(field_row_major);
38901e04c3fSmrg            else
39001e04c3fSmrg               field_align = type->std140_base_alignment(field_row_major);
39101e04c3fSmrg
39201e04c3fSmrg            if (struct_type->fields.structure[i].offset != -1) {
39301e04c3fSmrg               intra_struct_offset = struct_type->fields.structure[i].offset;
39401e04c3fSmrg            }
39501e04c3fSmrg
39601e04c3fSmrg            intra_struct_offset = glsl_align(intra_struct_offset, field_align);
39701e04c3fSmrg
39801e04c3fSmrg            assert(deref_record->field_idx >= 0);
39901e04c3fSmrg            if (i == (unsigned) deref_record->field_idx) {
40001e04c3fSmrg               if (struct_field)
40101e04c3fSmrg                  *struct_field = &struct_type->fields.structure[i];
40201e04c3fSmrg               break;
40301e04c3fSmrg            }
40401e04c3fSmrg
40501e04c3fSmrg            if (packing == GLSL_INTERFACE_PACKING_STD430)
40601e04c3fSmrg               intra_struct_offset += type->std430_size(field_row_major);
40701e04c3fSmrg            else
40801e04c3fSmrg               intra_struct_offset += type->std140_size(field_row_major);
40901e04c3fSmrg
41001e04c3fSmrg            /* If the field just examined was itself a structure, apply rule
41101e04c3fSmrg             * #9:
41201e04c3fSmrg             *
41301e04c3fSmrg             *     "The structure may have padding at the end; the base offset
41401e04c3fSmrg             *     of the member following the sub-structure is rounded up to
41501e04c3fSmrg             *     the next multiple of the base alignment of the structure."
41601e04c3fSmrg             */
4177e102996Smaya            if (type->without_array()->is_struct()) {
41801e04c3fSmrg               intra_struct_offset = glsl_align(intra_struct_offset,
41901e04c3fSmrg                                                field_align);
42001e04c3fSmrg
42101e04c3fSmrg            }
42201e04c3fSmrg         }
42301e04c3fSmrg
42401e04c3fSmrg         *const_offset += intra_struct_offset;
42501e04c3fSmrg         deref = deref_record->record->as_dereference();
42601e04c3fSmrg         break;
42701e04c3fSmrg      }
42801e04c3fSmrg
42901e04c3fSmrg      case ir_type_swizzle: {
43001e04c3fSmrg         ir_swizzle *deref_swizzle = (ir_swizzle *) deref;
43101e04c3fSmrg
43201e04c3fSmrg         assert(deref_swizzle->mask.num_components == 1);
43301e04c3fSmrg
43401e04c3fSmrg         *const_offset += deref_swizzle->mask.x * sizeof(int);
43501e04c3fSmrg         deref = deref_swizzle->val->as_dereference();
43601e04c3fSmrg         break;
43701e04c3fSmrg      }
43801e04c3fSmrg
43901e04c3fSmrg      default:
44001e04c3fSmrg         assert(!"not reached");
44101e04c3fSmrg         deref = NULL;
44201e04c3fSmrg         break;
44301e04c3fSmrg      }
44401e04c3fSmrg   }
44501e04c3fSmrg}
44601e04c3fSmrg
44701e04c3fSmrg} /* namespace lower_buffer_access */
448