nir/drawpixels: handle load_color0, load_input, load_interpolated_input
[mesa.git] / src / compiler / glsl / lower_ubo_reference.cpp
index d6269f7cbacc4b5719bd34c54b9ba23d21bc025f..08d4f72efa0a6b5b6e261bc2f47c6f9388e41e2f 100644 (file)
@@ -37,6 +37,7 @@
 #include "ir_builder.h"
 #include "main/macros.h"
 #include "glsl_parser_extras.h"
+#include "main/mtypes.h"
 
 using namespace ir_builder;
 
@@ -44,9 +45,13 @@ namespace {
 class lower_ubo_reference_visitor :
       public lower_buffer_access::lower_buffer_access {
 public:
-   lower_ubo_reference_visitor(struct gl_shader *shader)
-   : shader(shader), struct_field(NULL), variable(NULL)
+   lower_ubo_reference_visitor(struct gl_linked_shader *shader,
+                               bool clamp_block_indices,
+                               bool use_std430_as_default)
+   : shader(shader), clamp_block_indices(clamp_block_indices),
+     struct_field(NULL), variable(NULL)
    {
+      this->use_std430_as_default = use_std430_as_default;
    }
 
    void handle_rvalue(ir_rvalue **rvalue);
@@ -58,8 +63,8 @@ public:
                                 ir_rvalue **offset,
                                 unsigned *const_offset,
                                 bool *row_major,
-                                int *matrix_columns,
-                                unsigned packing);
+                                const glsl_type **matrix_type,
+                                enum glsl_interface_packing packing);
    uint32_t ssbo_access_params();
    ir_expression *ubo_load(void *mem_ctx, const struct glsl_type *type,
                           ir_rvalue *offset);
@@ -97,14 +102,15 @@ public:
    ir_expression *emit_ssbo_get_buffer_size(void *mem_ctx);
 
    unsigned calculate_unsized_array_stride(ir_dereference *deref,
-                                           unsigned packing);
+                                           enum glsl_interface_packing packing);
 
    ir_call *lower_ssbo_atomic_intrinsic(ir_call *ir);
    ir_call *check_for_ssbo_atomic_intrinsic(ir_call *ir);
    ir_visitor_status visit_enter(ir_call *ir);
+   ir_visitor_status visit_enter(ir_texture *ir);
 
-   struct gl_shader *shader;
-   struct gl_uniform_buffer_variable *ubo_var;
+   struct gl_linked_shader *shader;
+   bool clamp_block_indices;
    const struct glsl_struct_field *struct_field;
    ir_variable *variable;
    ir_rvalue *uniform_block;
@@ -242,6 +248,26 @@ interface_field_name(void *mem_ctx, char *base_name, ir_rvalue *d,
    return NULL;
 }
 
+static ir_rvalue *
+clamp_to_array_bounds(void *mem_ctx, ir_rvalue *index, const glsl_type *type)
+{
+   assert(type->is_array());
+
+   const unsigned array_size = type->arrays_of_arrays_size();
+
+   ir_constant *max_index = new(mem_ctx) ir_constant(array_size - 1);
+   max_index->type = index->type;
+
+   ir_constant *zero = new(mem_ctx) ir_constant(0);
+   zero->type = index->type;
+
+   if (index->type->base_type == GLSL_TYPE_INT)
+      index = max2(index, zero);
+   index = min2(index, max_index);
+
+   return index;
+}
+
 void
 lower_ubo_reference_visitor::setup_for_load_or_store(void *mem_ctx,
                                                      ir_variable *var,
@@ -249,8 +275,8 @@ lower_ubo_reference_visitor::setup_for_load_or_store(void *mem_ctx,
                                                      ir_rvalue **offset,
                                                      unsigned *const_offset,
                                                      bool *row_major,
-                                                     int *matrix_columns,
-                                                     unsigned packing)
+                                                     const glsl_type **matrix_type,
+                                                     enum glsl_interface_packing packing)
 {
    /* Determine the name of the interface block */
    ir_rvalue *nonconst_block_index;
@@ -258,15 +284,20 @@ lower_ubo_reference_visitor::setup_for_load_or_store(void *mem_ctx,
       interface_field_name(mem_ctx, (char *) var->get_interface_type()->name,
                            deref, &nonconst_block_index);
 
+   if (nonconst_block_index && clamp_block_indices) {
+      nonconst_block_index =
+         clamp_to_array_bounds(mem_ctx, nonconst_block_index, var->type);
+   }
+
    /* Locate the block by interface name */
    unsigned num_blocks;
    struct gl_uniform_block **blocks;
    if (this->buffer_access_type != ubo_load_access) {
-      num_blocks = shader->NumShaderStorageBlocks;
-      blocks = shader->ShaderStorageBlocks;
+      num_blocks = shader->Program->info.num_ssbos;
+      blocks = shader->Program->sh.ShaderStorageBlocks;
    } else {
-      num_blocks = shader->NumUniformBlocks;
-      blocks = shader->UniformBlocks;
+      num_blocks = shader->Program->info.num_ubos;
+      blocks = shader->Program->sh.UniformBlocks;
    }
    this->uniform_block = NULL;
    for (unsigned i = 0; i < num_blocks; i++) {
@@ -280,8 +311,11 @@ lower_ubo_reference_visitor::setup_for_load_or_store(void *mem_ctx,
             this->uniform_block = index;
          }
 
-         this->ubo_var = var->is_interface_instance()
-            ? &blocks[i]->Uniforms[0] : &blocks[i]->Uniforms[var->data.location];
+         if (var->is_interface_instance()) {
+            *const_offset = 0;
+         } else {
+            *const_offset = blocks[i]->Uniforms[var->data.location].Offset;
+         }
 
          break;
       }
@@ -289,11 +323,9 @@ lower_ubo_reference_visitor::setup_for_load_or_store(void *mem_ctx,
 
    assert(this->uniform_block);
 
-   *const_offset = ubo_var->Offset;
-
    this->struct_field = NULL;
-   setup_buffer_access(mem_ctx, var, deref, offset, const_offset, row_major,
-                       matrix_columns, &this->struct_field, packing);
+   setup_buffer_access(mem_ctx, deref, offset, const_offset, row_major,
+                       matrix_type, &this->struct_field, packing);
 }
 
 void
@@ -315,8 +347,11 @@ lower_ubo_reference_visitor::handle_rvalue(ir_rvalue **rvalue)
    ir_rvalue *offset = NULL;
    unsigned const_offset;
    bool row_major;
-   int matrix_columns;
-   unsigned packing = var->get_interface_type()->interface_packing;
+   const glsl_type *matrix_type;
+
+   enum glsl_interface_packing packing =
+      var->get_interface_type()->
+         get_internal_ifc_packing(use_std430_as_default);
 
    this->buffer_access_type =
       var->is_in_shader_storage_block() ?
@@ -328,7 +363,7 @@ lower_ubo_reference_visitor::handle_rvalue(ir_rvalue **rvalue)
     */
    setup_for_load_or_store(mem_ctx, var, deref,
                            &offset, &const_offset,
-                           &row_major, &matrix_columns,
+                           &row_major, &matrix_type,
                            packing);
    assert(offset);
 
@@ -349,7 +384,7 @@ lower_ubo_reference_visitor::handle_rvalue(ir_rvalue **rvalue)
 
    deref = new(mem_ctx) ir_dereference_variable(load_var);
    emit_access(mem_ctx, false, deref, load_offset, const_offset,
-               row_major, matrix_columns, packing, 0);
+               row_major, matrix_type, packing, 0);
    *rvalue = deref;
 
    progress = true;
@@ -372,7 +407,7 @@ lower_ubo_reference_visitor::ubo_load(void *mem_ctx,
 static bool
 shader_storage_buffer_object(const _mesa_glsl_parse_state *state)
 {
-   return state->ARB_shader_storage_buffer_object_enable;
+   return state->has_shader_storage_buffer_objects();
 }
 
 uint32_t
@@ -383,13 +418,13 @@ lower_ubo_reference_visitor::ssbo_access_params()
    if (variable->is_interface_instance()) {
       assert(struct_field);
 
-      return ((struct_field->image_coherent ? ACCESS_COHERENT : 0) |
-              (struct_field->image_restrict ? ACCESS_RESTRICT : 0) |
-              (struct_field->image_volatile ? ACCESS_VOLATILE : 0));
+      return ((struct_field->memory_coherent ? ACCESS_COHERENT : 0) |
+              (struct_field->memory_restrict ? ACCESS_RESTRICT : 0) |
+              (struct_field->memory_volatile ? ACCESS_VOLATILE : 0));
    } else {
-      return ((variable->data.image_coherent ? ACCESS_COHERENT : 0) |
-              (variable->data.image_restrict ? ACCESS_RESTRICT : 0) |
-              (variable->data.image_volatile ? ACCESS_VOLATILE : 0));
+      return ((variable->data.memory_coherent ? ACCESS_COHERENT : 0) |
+              (variable->data.memory_restrict ? ACCESS_RESTRICT : 0) |
+              (variable->data.memory_volatile ? ACCESS_VOLATILE : 0));
    }
 }
 
@@ -425,7 +460,7 @@ lower_ubo_reference_visitor::ssbo_store(void *mem_ctx,
       ir_function_signature(glsl_type::void_type, shader_storage_buffer_object);
    assert(sig);
    sig->replace_parameters(&sig_params);
-   sig->is_intrinsic = true;
+   sig->intrinsic_id = ir_intrinsic_ssbo_store;
 
    ir_function *f = new(mem_ctx) ir_function("__intrinsic_store_ssbo");
    f->add_signature(sig);
@@ -462,7 +497,7 @@ lower_ubo_reference_visitor::ssbo_load(void *mem_ctx,
       new(mem_ctx) ir_function_signature(type, shader_storage_buffer_object);
    assert(sig);
    sig->replace_parameters(&sig_params);
-   sig->is_intrinsic = true;
+   sig->intrinsic_id = ir_intrinsic_ssbo_load;
 
    ir_function *f = new(mem_ctx) ir_function("__intrinsic_load_ssbo");
    f->add_signature(sig);
@@ -528,8 +563,11 @@ lower_ubo_reference_visitor::write_to_memory(void *mem_ctx,
    ir_rvalue *offset = NULL;
    unsigned const_offset;
    bool row_major;
-   int matrix_columns;
-   unsigned packing = var->get_interface_type()->interface_packing;
+   const glsl_type *matrix_type;
+
+   enum glsl_interface_packing packing =
+      var->get_interface_type()->
+         get_internal_ifc_packing(use_std430_as_default);
 
    this->buffer_access_type = ssbo_store_access;
    this->variable = var;
@@ -539,7 +577,7 @@ lower_ubo_reference_visitor::write_to_memory(void *mem_ctx,
     */
    setup_for_load_or_store(mem_ctx, var, deref,
                            &offset, &const_offset,
-                           &row_major, &matrix_columns,
+                           &row_major, &matrix_type,
                            packing);
    assert(offset);
 
@@ -554,7 +592,7 @@ lower_ubo_reference_visitor::write_to_memory(void *mem_ctx,
 
    deref = new(mem_ctx) ir_dereference_variable(write_var);
    emit_access(mem_ctx, true, deref, write_offset, const_offset,
-               row_major, matrix_columns, packing, write_mask);
+               row_major, matrix_type, packing, write_mask);
 }
 
 ir_visitor_status
@@ -598,7 +636,7 @@ lower_ubo_reference_visitor::check_ssbo_unsized_array_length_expression(ir_expre
          return;
    }
 
-   for (unsigned i = 0; i < ir->get_num_operands(); i++) {
+   for (unsigned i = 0; i < ir->num_operands; i++) {
       if (ir->operands[i]->ir_type != ir_type_expression)
          continue;
       ir_expression *expr = (ir_expression *) ir->operands[i];
@@ -638,7 +676,7 @@ lower_ubo_reference_visitor::emit_ssbo_get_buffer_size(void *mem_ctx)
 
 unsigned
 lower_ubo_reference_visitor::calculate_unsized_array_stride(ir_dereference *deref,
-                                                            unsigned packing)
+                                                            enum glsl_interface_packing packing)
 {
    unsigned array_stride = 0;
 
@@ -707,9 +745,13 @@ lower_ubo_reference_visitor::process_ssbo_unsized_array_length(ir_rvalue **rvalu
    ir_rvalue *base_offset = NULL;
    unsigned const_offset;
    bool row_major;
-   int matrix_columns;
-   unsigned packing = var->get_interface_type()->interface_packing;
-   int unsized_array_stride = calculate_unsized_array_stride(deref, packing);
+   const glsl_type *matrix_type;
+
+   enum glsl_interface_packing packing =
+      var->get_interface_type()->
+         get_internal_ifc_packing(use_std430_as_default);
+   int unsized_array_stride =
+      calculate_unsized_array_stride(deref, packing);
 
    this->buffer_access_type = ssbo_unsized_array_length_access;
    this->variable = var;
@@ -719,7 +761,7 @@ lower_ubo_reference_visitor::process_ssbo_unsized_array_length(ir_rvalue **rvalu
     */
    setup_for_load_or_store(mem_ctx, var, deref,
                            &base_offset, &const_offset,
-                           &row_major, &matrix_columns,
+                           &row_major, &matrix_type,
                            packing);
    /* array.length() =
     *  max((buffer_object_size - offset_of_array) / stride_of_array, 0)
@@ -842,7 +884,7 @@ lower_ubo_reference_visitor::check_for_buffer_struct_copy(ir_assignment *ir)
       return false;
 
    /* LHS and RHS must be records */
-   if (!ir->lhs->type->is_record() || !ir->rhs->type->is_record())
+   if (!ir->lhs->type->is_struct() || !ir->rhs->type->is_struct())
       return false;
 
    /* RHS must be a buffer-backed variable. This is what can cause the problem
@@ -864,7 +906,7 @@ lower_ubo_reference_visitor::check_for_buffer_struct_copy(ir_assignment *ir)
    if (!lhs_deref)
       return false;
 
-   assert(lhs_deref->type->record_compare(rhs_deref->type));
+   assert(lhs_deref->type == rhs_deref->type);
    void *mem_ctx = ralloc_parent(shader->ir);
 
    for (unsigned i = 0; i < lhs_deref->type->length; i++) {
@@ -928,7 +970,8 @@ lower_ubo_reference_visitor::lower_ssbo_atomic_intrinsic(ir_call *ir)
           inst->ir_type == ir_type_swizzle);
 
    ir_rvalue *deref = (ir_rvalue *) inst;
-   assert(deref->type->is_scalar() && deref->type->is_integer());
+   assert(deref->type->is_scalar() &&
+          (deref->type->is_integer_32() || deref->type->is_float()));
 
    ir_variable *var = deref->variable_referenced();
    assert(var);
@@ -941,19 +984,22 @@ lower_ubo_reference_visitor::lower_ssbo_atomic_intrinsic(ir_call *ir)
    ir_rvalue *offset = NULL;
    unsigned const_offset;
    bool row_major;
-   int matrix_columns;
-   unsigned packing = var->get_interface_type()->interface_packing;
+   const glsl_type *matrix_type;
+
+   enum glsl_interface_packing packing =
+      var->get_interface_type()->
+         get_internal_ifc_packing(use_std430_as_default);
 
    this->buffer_access_type = ssbo_atomic_access;
    this->variable = var;
 
    setup_for_load_or_store(mem_ctx, var, deref,
                            &offset, &const_offset,
-                           &row_major, &matrix_columns,
+                           &row_major, &matrix_type,
                            packing);
    assert(offset);
    assert(!row_major);
-   assert(matrix_columns == 1);
+   assert(matrix_type == NULL);
 
    ir_rvalue *deref_offset =
       add(offset, new(mem_ctx) ir_constant(const_offset));
@@ -971,8 +1017,7 @@ lower_ubo_reference_visitor::lower_ssbo_atomic_intrinsic(ir_call *ir)
       ir_variable(glsl_type::uint_type, "offset" , ir_var_function_in);
    sig_params.push_tail(sig_param);
 
-   const glsl_type *type = deref->type->base_type == GLSL_TYPE_INT ?
-      glsl_type::int_type : glsl_type::uint_type;
+   const glsl_type *type = deref->type->get_scalar_type();
    sig_param = new(mem_ctx)
          ir_variable(type, "data1", ir_var_function_in);
    sig_params.push_tail(sig_param);
@@ -988,7 +1033,10 @@ lower_ubo_reference_visitor::lower_ssbo_atomic_intrinsic(ir_call *ir)
                                          shader_storage_buffer_object);
    assert(sig);
    sig->replace_parameters(&sig_params);
-   sig->is_intrinsic = true;
+
+   assert(ir->callee->intrinsic_id >= ir_intrinsic_generic_load);
+   assert(ir->callee->intrinsic_id <= ir_intrinsic_generic_atomic_comp_swap);
+   sig->intrinsic_id = MAP_INTRINSIC_TO_TYPE(ir->callee->intrinsic_id, ssbo);
 
    char func_name[64];
    sprintf(func_name, "%s_ssbo", ir->callee_name());
@@ -1029,15 +1077,15 @@ lower_ubo_reference_visitor::check_for_ssbo_atomic_intrinsic(ir_call *ir)
    if (!var || !var->is_in_shader_storage_block())
       return ir;
 
-   const char *callee = ir->callee_name();
-   if (!strcmp("__intrinsic_atomic_add", callee) ||
-       !strcmp("__intrinsic_atomic_min", callee) ||
-       !strcmp("__intrinsic_atomic_max", callee) ||
-       !strcmp("__intrinsic_atomic_and", callee) ||
-       !strcmp("__intrinsic_atomic_or", callee) ||
-       !strcmp("__intrinsic_atomic_xor", callee) ||
-       !strcmp("__intrinsic_atomic_exchange", callee) ||
-       !strcmp("__intrinsic_atomic_comp_swap", callee)) {
+   const enum ir_intrinsic_id id = ir->callee->intrinsic_id;
+   if (id == ir_intrinsic_generic_atomic_add ||
+       id == ir_intrinsic_generic_atomic_min ||
+       id == ir_intrinsic_generic_atomic_max ||
+       id == ir_intrinsic_generic_atomic_and ||
+       id == ir_intrinsic_generic_atomic_or ||
+       id == ir_intrinsic_generic_atomic_xor ||
+       id == ir_intrinsic_generic_atomic_exchange ||
+       id == ir_intrinsic_generic_atomic_comp_swap) {
       return lower_ssbo_atomic_intrinsic(ir);
    }
 
@@ -1059,12 +1107,28 @@ lower_ubo_reference_visitor::visit_enter(ir_call *ir)
 }
 
 
+ir_visitor_status
+lower_ubo_reference_visitor::visit_enter(ir_texture *ir)
+{
+   ir_dereference *sampler = ir->sampler;
+
+   if (sampler->ir_type == ir_type_dereference_record) {
+      handle_rvalue((ir_rvalue **)&ir->sampler);
+      return visit_continue_with_parent;
+   }
+
+   return rvalue_visit(ir);
+}
+
+
 } /* unnamed namespace */
 
 void
-lower_ubo_reference(struct gl_shader *shader)
+lower_ubo_reference(struct gl_linked_shader *shader,
+                    bool clamp_block_indices, bool use_std430_as_default)
 {
-   lower_ubo_reference_visitor v(shader);
+   lower_ubo_reference_visitor v(shader, clamp_block_indices,
+                                 use_std430_as_default);
 
    /* Loop over the instructions lowering references, because we take
     * a deref of a UBO array using a UBO dereference as the index will