[Mesa-dev] [PATCH 27/74] glsl: Lower shader storage buffer object writes to ir_ssbo_store

Iago Toral itoral at igalia.com
Mon May 18 23:15:06 PDT 2015


On Mon, 2015-05-18 at 15:58 -0700, Jordan Justen wrote:
> On 2015-05-14 07:06:30, Iago Toral Quiroga wrote:
> > Extend the existing lower_ubo_reference pass to also detect SSBO writes
> > and lower them to ir_ssbo_store nodes.
> > ---
> >  src/glsl/lower_ubo_reference.cpp | 412 +++++++++++++++++++++++++++++++--------
> >  1 file changed, 336 insertions(+), 76 deletions(-)
> > 
> > diff --git a/src/glsl/lower_ubo_reference.cpp b/src/glsl/lower_ubo_reference.cpp
> > index fb8a890..8cb28f5 100644
> > --- a/src/glsl/lower_ubo_reference.cpp
> > +++ b/src/glsl/lower_ubo_reference.cpp
> > @@ -139,12 +139,34 @@ public:
> >     }
> >  
> >     void handle_rvalue(ir_rvalue **rvalue);
> > +   ir_visitor_status visit_enter(ir_assignment *ir);
> > +
> > +   void setup_for_load_or_write(ir_variable *var,
> > +                                ir_dereference *deref,
> > +                                ir_rvalue **offset,
> > +                                unsigned *const_offset,
> > +                                bool *row_major,
> > +                                int *matrix_columns,
> > +                                bool *is_buffer);
> > +
> >     void emit_ubo_loads(ir_dereference *deref, ir_variable *base_offset,
> >                         unsigned int deref_offset, bool row_major,
> >                         int matrix_columns);
> >     ir_expression *ubo_load(const struct glsl_type *type,
> >                            ir_rvalue *offset);
> >  
> > +
> > +   void check_for_ssbo_write(ir_assignment *ir);
> > +   void write_to_memory(ir_dereference *deref,
> > +                        ir_variable *var,
> > +                        ir_variable *write_var,
> > +                        unsigned write_mask);
> > +   void emit_ssbo_writes(ir_dereference *deref, ir_variable *base_offset,
> > +                         unsigned int deref_offset, bool row_major,
> > +                         int matrix_columns, unsigned write_mask);
> > +   ir_ssbo_store *ssbo_write(ir_rvalue *deref, ir_rvalue *offset,
> > +                             unsigned write_mask);
> > +
> >     void *mem_ctx;
> >     struct gl_shader *shader;
> >     struct gl_uniform_buffer_variable *ubo_var;
> > @@ -219,26 +241,21 @@ interface_field_name(void *mem_ctx, char *base_name, ir_dereference *d,
> >  }
> >  
> >  void
> > -lower_ubo_reference_visitor::handle_rvalue(ir_rvalue **rvalue)
> > +lower_ubo_reference_visitor::setup_for_load_or_write(ir_variable *var,
> > +                                                     ir_dereference *deref,
> > +                                                     ir_rvalue **offset,
> > +                                                     unsigned *const_offset,
> > +                                                     bool *row_major,
> > +                                                     int *matrix_columns,
> > +                                                     bool *is_buffer)
> >  {
> > -   if (!*rvalue)
> > -      return;
> > -
> > -   ir_dereference *deref = (*rvalue)->as_dereference();
> > -   if (!deref)
> > -      return;
> > -
> > -   ir_variable *var = deref->variable_referenced();
> > -   if (!var || !var->is_in_uniform_block())
> > -      return;
> > -
> > -   mem_ctx = ralloc_parent(*rvalue);
> > -
> > +   /* Fix out the name of the interface block*/
> >     ir_rvalue *nonconst_block_index;
> >     const char *const field_name =
> >        interface_field_name(mem_ctx, (char *) var->get_interface_type()->name,
> >                             deref, &nonconst_block_index);
> >  
> > +   /* Locate the ubo block by interface name */
> >     this->uniform_block = NULL;
> >     for (unsigned i = 0; i < shader->NumUniformBlocks; i++) {
> >        if (strcmp(field_name, shader->UniformBlocks[i].Name) == 0) {
> > @@ -253,10 +270,7 @@ lower_ubo_reference_visitor::handle_rvalue(ir_rvalue **rvalue)
> >              this->uniform_block = index;
> >           }
> >  
> > -         if (shader->UniformBlocks[i].IsBuffer)
> > -            this->opcode = ir_binop_ssbo_load;
> > -         else
> > -            this->opcode = ir_binop_ubo_load;
> > +         *is_buffer = shader->UniformBlocks[i].IsBuffer;
> >  
> >           struct gl_uniform_block *block = &shader->UniformBlocks[i];
> >  
> > @@ -269,10 +283,10 @@ lower_ubo_reference_visitor::handle_rvalue(ir_rvalue **rvalue)
> >  
> >     assert(this->uniform_block);
> >  
> > -   ir_rvalue *offset = new(mem_ctx) ir_constant(0u);
> > -   unsigned const_offset = 0;
> > -   bool row_major = is_dereferenced_thing_row_major(deref);
> > -   int matrix_columns = 1;
> > +   *offset = new(mem_ctx) ir_constant(0u);
> > +   *const_offset = 0;
> > +   *row_major = is_dereferenced_thing_row_major(deref);
> > +   *matrix_columns = 1;
> >  
> >     /* Calculate the offset to the start of the region of the UBO
> >      * dereferenced by *rvalue.  This may be a variable offset if an
> > @@ -281,76 +295,76 @@ lower_ubo_reference_visitor::handle_rvalue(ir_rvalue **rvalue)
> >     while (deref) {
> >        switch (deref->ir_type) {
> >        case ir_type_dereference_variable: {
> > -        const_offset += ubo_var->Offset;
> > -        deref = NULL;
> > -        break;
> > +         *const_offset += ubo_var->Offset;
> > +         deref = NULL;
> > +         break;
> >        }
> >  
> >        case ir_type_dereference_array: {
> > -        ir_dereference_array *deref_array = (ir_dereference_array *)deref;
> > -        unsigned array_stride;
> > -        if (deref_array->array->type->is_matrix() && row_major) {
> > -           /* When loading a vector out of a row major matrix, the
> > -            * step between the columns (vectors) is the size of a
> > -            * float, while the step between the rows (elements of a
> > -            * vector) is handled below in emit_ubo_loads.
> > -            */
> > -           array_stride = 4;
> > +         ir_dereference_array *deref_array = (ir_dereference_array *) deref;
> > +         unsigned array_stride;
> > +         if (deref_array->array->type->is_matrix() && *row_major) {
> > +            /* When loading a vector out of a row major matrix, the
> > +             * step between the columns (vectors) is the size of a
> > +             * float, while the step between the rows (elements of a
> > +             * vector) is handled below in emit_ubo_loads.
> > +             */
> > +            array_stride = 4;
> >              if (deref_array->array->type->is_double())
> >                 array_stride *= 2;
> > -            matrix_columns = deref_array->array->type->matrix_columns;
> > +            *matrix_columns = deref_array->array->type->matrix_columns;
> >           } else if (deref_array->type->is_interface()) {
> >              /* We're processing an array dereference of an interface instance
> > -            * array.  The thing being dereferenced *must* be a variable
> > -            * dereference because intefaces cannot be embedded an other
> > -            * types.  In terms of calculating the offsets for the lowering
> > -            * pass, we don't care about the array index.  All elements of an
> > -            * interface instance array will have the same offsets relative to
> > -            * the base of the block that backs them.
> > +             * array. The thing being dereferenced *must* be a variable
> > +             * dereference because interfaces cannot be embedded in other
> > +             * types. In terms of calculating the offsets for the lowering
> > +             * pass, we don't care about the array index. All elements of an
> > +             * interface instance array will have the same offsets relative to
> > +             * the base of the block that backs them.
> >               */
> >              assert(deref_array->array->as_dereference_variable());
> >              deref = deref_array->array->as_dereference();
> >              break;
> > -        } else {
> > +         } else {
> >              /* Whether or not the field is row-major (because it might be a
> > -             * bvec2 or something) does not affect the array itself.  We need
> > +             * bvec2 or something) does not affect the array itself. We need
> >               * to know whether an array element in its entirety is row-major.
> >               */
> >              const bool array_row_major =
> >                 is_dereferenced_thing_row_major(deref_array);
> >  
> > -           array_stride = deref_array->type->std140_size(array_row_major);
> > -           array_stride = glsl_align(array_stride, 16);
> > -        }
> > +            array_stride = deref_array->type->std140_size(array_row_major);
> > +            array_stride = glsl_align(array_stride, 16);
> > +         }
> >  
> >           ir_rvalue *array_index = deref_array->array_index;
> >           if (array_index->type->base_type == GLSL_TYPE_INT)
> >              array_index = i2u(array_index);
> >  
> > -        ir_constant *const_index =
> > +         ir_constant *const_index =
> >              array_index->constant_expression_value(NULL);
> > -        if (const_index) {
> > -           const_offset += array_stride * const_index->value.u[0];
> > -        } else {
> > -           offset = add(offset,
> > -                        mul(array_index,
> > -                            new(mem_ctx) ir_constant(array_stride)));
> > -        }
> > -        deref = deref_array->array->as_dereference();
> > -        break;
> > +         if (const_index) {
> > +            *const_offset += array_stride * const_index->value.u[0];
> > +         } else {
> > +            *offset = add(*offset,
> > +                          mul(array_index,
> > +                              new(mem_ctx) ir_constant(array_stride)));
> > +         }
> > +         deref = deref_array->array->as_dereference();
> > +         break;
> >        }
> >  
> >        case ir_type_dereference_record: {
> > -        ir_dereference_record *deref_record = (ir_dereference_record *)deref;
> > -        const glsl_type *struct_type = deref_record->record->type;
> > -        unsigned intra_struct_offset = 0;
> > +         ir_dereference_record *deref_record = (ir_dereference_record *) deref;
> > +         const glsl_type *struct_type = deref_record->record->type;
> > +         unsigned intra_struct_offset = 0;
> >  
> > -        for (unsigned int i = 0; i < struct_type->length; i++) {
> > -           const glsl_type *type = struct_type->fields.structure[i].type;
> > +         for (unsigned int i = 0; i < struct_type->length; i++) {
> > +            const glsl_type *type = struct_type->fields.structure[i].type;
> >  
> > -            ir_dereference_record *field_deref =
> > -               new(mem_ctx) ir_dereference_record(deref_record->record,
> > -                                                  struct_type->fields.structure[i].name);
> > +            ir_dereference_record *field_deref = new(mem_ctx)
> > +               ir_dereference_record(deref_record->record,
> > +                                     struct_type->fields.structure[i].name);
> >              const bool field_row_major =
> >                 is_dereferenced_thing_row_major(field_deref);
> >  
> > @@ -358,11 +372,12 @@ lower_ubo_reference_visitor::handle_rvalue(ir_rvalue **rvalue)
> >  
> >              unsigned field_align = type->std140_base_alignment(field_row_major);
> >  
> > -           intra_struct_offset = glsl_align(intra_struct_offset, field_align);
> > +            intra_struct_offset = glsl_align(intra_struct_offset, field_align);
> > +
> > +            if (strcmp(struct_type->fields.structure[i].name,
> > +                       deref_record->field) == 0)
> > +               break;
> >  
> > -           if (strcmp(struct_type->fields.structure[i].name,
> > -                      deref_record->field) == 0)
> > -              break;
> >              intra_struct_offset += type->std140_size(field_row_major);
> >  
> >              /* If the field just examined was itself a structure, apply rule
> > @@ -377,19 +392,56 @@ lower_ubo_reference_visitor::handle_rvalue(ir_rvalue **rvalue)
> >                                                  field_align);
> >  
> >              }
> > -        }
> > -
> > -        const_offset += intra_struct_offset;
> > +         }
> >  
> > -        deref = deref_record->record->as_dereference();
> > -        break;
> > +         *const_offset += intra_struct_offset;
> > +         deref = deref_record->record->as_dereference();
> > +         break;
> >        }
> > +
> >        default:
> > -        assert(!"not reached");
> > -        deref = NULL;
> > -        break;
> > +         assert(!"not reached");
> > +         deref = NULL;
> > +         break;
> >        }
> >     }
> > +}
> > +
> > +void
> > +lower_ubo_reference_visitor::handle_rvalue(ir_rvalue **rvalue)
> > +{
> > +   if (!*rvalue)
> > +      return;
> > +
> > +   ir_dereference *deref = (*rvalue)->as_dereference();
> > +   if (!deref)
> > +      return;
> > +
> > +   ir_variable *var = deref->variable_referenced();
> > +   if (!var || !var->is_in_uniform_block())
> > +      return;
> > +
> > +   mem_ctx = ralloc_parent(*rvalue);
> > +
> > +   ir_rvalue *offset = NULL;
> > +   unsigned const_offset;
> > +   bool row_major;
> > +   int matrix_columns;
> > +   bool is_buffer;
> > +
> > +   /* Compute the offset to the start if the dereference as well as other
> > +    * information we need to configure the write
> > +    */
> > +   setup_for_load_or_write(var, deref,
> > +                           &offset, &const_offset,
> > +                           &row_major, &matrix_columns,
> > +                           &is_buffer);
> > +   assert(offset);
> > +
> > +   if (is_buffer)
> > +      this->opcode = ir_binop_ssbo_load;
> > +   else
> > +      this->opcode = ir_binop_ubo_load;
> >  
> >     /* Now that we've calculated the offset to the start of the
> >      * dereference, walk over the type and emit loads into a temporary.
> > @@ -549,6 +601,214 @@ lower_ubo_reference_visitor::emit_ubo_loads(ir_dereference *deref,
> >     }
> >  }
> >  
> > +ir_ssbo_store *
> > +lower_ubo_reference_visitor::ssbo_write(ir_rvalue *deref,
> > +                                        ir_rvalue *offset,
> > +                                        unsigned write_mask)
> > +{
> > +   ir_rvalue *block_ref = this->uniform_block->clone(mem_ctx, NULL);
> > +   ir_rvalue *val_ref = deref->clone(mem_ctx, NULL);
> > +   return new(mem_ctx) ir_ssbo_store(block_ref, offset, val_ref, write_mask);
> > +}
> > +
> > +static inline int
> > +writemask_for_size(unsigned n)
> > +{
> > +   return ((1 << n) - 1);
> > +}
> > +
> > +void
> > +lower_ubo_reference_visitor::emit_ssbo_writes(ir_dereference *deref,
> > +                                              ir_variable *base_offset,
> > +                                              unsigned int deref_offset,
> > +                                              bool row_major,
> > +                                              int matrix_columns,
> > +                                              unsigned write_mask)
> > +{
> 
> This code seems to be mostly a duplicate of the load case. Wouldn't
> this leave us with two mostly similar complex code paths?
> 
> Maybe the low level emit could switch based on the opcode?
> 
> -Jordan

Yeah, good point. I'll do that.

Iago

> > +   if (deref->type->is_record()) {
> > +      unsigned int field_offset = 0;
> > +
> > +      for (unsigned i = 0; i < deref->type->length; i++) {
> > +         const struct glsl_struct_field *field =
> > +            &deref->type->fields.structure[i];
> > +         ir_dereference *field_deref =
> > +            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));
> > +
> > +         emit_ssbo_writes(field_deref, base_offset,
> > +                          deref_offset + field_offset,
> > +                          row_major, 1,
> > +                          writemask_for_size(field_deref->type->vector_elements));
> > +
> > +             field_offset += field->type->std140_size(row_major);
> > +      }
> > +      return;
> > +   }
> > +
> > +   if (deref->type->is_array()) {
> > +      unsigned array_stride =
> > +         glsl_align(deref->type->fields.array->std140_size(row_major), 16);
> > +
> > +      for (unsigned i = 0; i < deref->type->length; i++) {
> > +         ir_constant *element = new(mem_ctx) ir_constant(i);
> > +         ir_dereference *element_deref =
> > +            new(mem_ctx) ir_dereference_array(deref->clone(mem_ctx, NULL),
> > +                                              element);
> > +         emit_ssbo_writes(element_deref, base_offset,
> > +                          deref_offset + i * array_stride,
> > +                          row_major, 1,
> > +                          writemask_for_size(element_deref->type->vector_elements));
> > +      }
> > +      return;
> > +   }
> > +
> > +   if (deref->type->is_matrix()) {
> > +      for (unsigned i = 0; i < deref->type->matrix_columns; i++) {
> > +         ir_constant *col = new(mem_ctx) ir_constant(i);
> > +         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_double() ? 8 : 4;
> > +            emit_ssbo_writes(col_deref, base_offset,
> > +                             deref_offset + i * size_mul,
> > +                             row_major, deref->type->matrix_columns,
> > +                             writemask_for_size(col_deref->type->vector_elements));
> > +         } 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.
> > +             */
> > +            int size_mul = (deref->type->is_double() &&
> > +                            deref->type->vector_elements > 2) ? 32 : 16;
> > +            emit_ssbo_writes(col_deref, base_offset,
> > +                             deref_offset + i * size_mul,
> > +                             row_major, deref->type->matrix_columns,
> > +                             writemask_for_size(col_deref->type->vector_elements));
> > +         }
> > +      }
> > +      return;
> > +   }
> > +
> > +   assert(deref->type->is_scalar() || deref->type->is_vector());
> > +
> > +   if (!row_major) {
> > +      ir_rvalue *offset =
> > +         add(base_offset, new(mem_ctx) ir_constant(deref_offset));
> > +      base_ir->insert_after(ssbo_write(deref, offset, write_mask));
> > +   } else {
> > +      unsigned N = deref->type->is_double() ? 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 = glsl_align(matrix_columns * N, 16);
> > +
> > +      for (unsigned i = 0; i < deref->type->vector_elements; i++) {
> > +         ir_rvalue *chan_offset =
> > +            add(base_offset,
> > +                new(mem_ctx) ir_constant(deref_offset + i * matrix_stride));
> > +
> > +         base_ir->insert_after(ssbo_write(swizzle(deref, i, 1), chan_offset, 1));
> > +      }
> > +   }
> > +}
> > +
> > +void
> > +lower_ubo_reference_visitor::write_to_memory(ir_dereference *deref,
> > +                                             ir_variable *var,
> > +                                             ir_variable *write_var,
> > +                                             unsigned write_mask)
> > +{
> > +   ir_rvalue *offset = NULL;
> > +   unsigned const_offset;
> > +   bool row_major;
> > +   int matrix_columns;
> > +   bool is_buffer;
> > +
> > +   /* Compute the offset to the start if the dereference as well as other
> > +    * information we need to configure the write
> > +    */
> > +   setup_for_load_or_write(var, deref,
> > +                           &offset, &const_offset,
> > +                           &row_major, &matrix_columns,
> > +                           &is_buffer);
> > +   assert(offset);
> > +   assert(is_buffer);
> > +
> > +   /* Now emit writes from the temporary to memory */
> > +   ir_variable *write_offset =
> > +      new(mem_ctx) ir_variable(glsl_type::uint_type,
> > +                               "ssbo_write_temp_offset",
> > +                               ir_var_temporary);
> > +
> > +   base_ir->insert_before(write_offset);
> > +   base_ir->insert_before(assign(write_offset, offset));
> > +
> > +   deref = new(mem_ctx) ir_dereference_variable(write_var);
> > +   emit_ssbo_writes(deref, write_offset, const_offset,
> > +                    row_major, matrix_columns, write_mask);
> > +}
> > +
> > +void
> > +lower_ubo_reference_visitor::check_for_ssbo_write(ir_assignment *ir)
> > +{
> > +   if (!ir || !ir->lhs)
> > +      return;
> > +
> > +   ir_rvalue *rvalue = ir->lhs->as_rvalue();
> > +   if (!rvalue)
> > +      return;
> > +
> > +   ir_dereference *deref = ir->lhs->as_dereference();
> > +   if (!deref)
> > +      return;
> > +
> > +   ir_variable *var = ir->lhs->variable_referenced();
> > +   if (!var || !var->is_in_uniform_block())
> > +      return;
> > +
> > +   /* We have a write to a buffer variable, so declare a temporary and rewrite
> > +    * the assignment so that the temporary is the LHS.
> > +    */
> > +   mem_ctx = ralloc_parent(rvalue);
> > +
> > +   const glsl_type *type = rvalue->type;
> > +   ir_variable *write_var = new(mem_ctx) ir_variable(type,
> > +                                                     "ssbo_write_temp",
> > +                                                     ir_var_temporary);
> > +   base_ir->insert_before(write_var);
> > +   ir->lhs = new(mem_ctx) ir_dereference_variable(write_var);
> > +
> > +   /* Now we have to write the value assigned to the temporary back to memory */
> > +   write_to_memory(deref, var, write_var, ir->write_mask);
> > +   progress = true;
> > +}
> > +
> > +
> > +ir_visitor_status
> > +lower_ubo_reference_visitor::visit_enter(ir_assignment *ir)
> > +{
> > +   check_for_ssbo_write(ir);
> > +   return rvalue_visit(ir);
> > +}
> > +
> >  } /* unnamed namespace */
> >  
> >  void
> > -- 
> > 1.9.1
> > 
> > _______________________________________________
> > mesa-dev mailing list
> > mesa-dev at lists.freedesktop.org
> > http://lists.freedesktop.org/mailman/listinfo/mesa-dev
> 




More information about the mesa-dev mailing list