glsl: Silence unused parameter warning
[mesa.git] / src / compiler / glsl / lower_ubo_reference.cpp
index a172054bac8279f399e2b00867e8b12642d29f86..1ed281c488aaa1f837bdcb480040a486f6b79783 100644 (file)
@@ -44,8 +44,10 @@ namespace {
 class lower_ubo_reference_visitor :
       public lower_buffer_access::lower_buffer_access {
 public:
-   lower_ubo_reference_visitor(struct gl_shader *shader)
-   : shader(shader)
+   lower_ubo_reference_visitor(struct gl_linked_shader *shader,
+                               bool clamp_block_indices)
+   : shader(shader), clamp_block_indices(clamp_block_indices),
+     struct_field(NULL), variable(NULL)
    {
    }
 
@@ -59,7 +61,8 @@ public:
                                 unsigned *const_offset,
                                 bool *row_major,
                                 int *matrix_columns,
-                                unsigned packing);
+                                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);
    ir_call *ssbo_load(void *mem_ctx, const struct glsl_type *type,
@@ -96,14 +99,17 @@ 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);
 
-   struct gl_shader *shader;
+   struct gl_linked_shader *shader;
+   bool clamp_block_indices;
    struct gl_uniform_buffer_variable *ubo_var;
+   const struct glsl_struct_field *struct_field;
+   ir_variable *variable;
    ir_rvalue *uniform_block;
    bool progress;
 };
@@ -239,6 +245,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,
@@ -247,7 +273,7 @@ lower_ubo_reference_visitor::setup_for_load_or_store(void *mem_ctx,
                                                      unsigned *const_offset,
                                                      bool *row_major,
                                                      int *matrix_columns,
-                                                     unsigned packing)
+                                                     enum glsl_interface_packing packing)
 {
    /* Determine the name of the interface block */
    ir_rvalue *nonconst_block_index;
@@ -255,6 +281,11 @@ 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;
@@ -288,8 +319,9 @@ lower_ubo_reference_visitor::setup_for_load_or_store(void *mem_ctx,
 
    *const_offset = ubo_var->Offset;
 
-   setup_buffer_access(mem_ctx, var, deref, offset, const_offset, row_major,
-                       matrix_columns, packing);
+   this->struct_field = NULL;
+   setup_buffer_access(mem_ctx, deref, offset, const_offset, row_major,
+                       matrix_columns, &this->struct_field, packing);
 }
 
 void
@@ -312,11 +344,12 @@ lower_ubo_reference_visitor::handle_rvalue(ir_rvalue **rvalue)
    unsigned const_offset;
    bool row_major;
    int matrix_columns;
-   unsigned packing = var->get_interface_type()->interface_packing;
+   enum glsl_interface_packing packing = var->get_interface_type_packing();
 
    this->buffer_access_type =
       var->is_in_shader_storage_block() ?
       ssbo_load_access : ubo_load_access;
+   this->variable = var;
 
    /* Compute the offset to the start if the dereference as well as other
     * information we need to configure the write
@@ -367,7 +400,25 @@ 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
+lower_ubo_reference_visitor::ssbo_access_params()
+{
+   assert(variable);
+
+   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));
+   } else {
+      return ((variable->data.image_coherent ? ACCESS_COHERENT : 0) |
+              (variable->data.image_restrict ? ACCESS_RESTRICT : 0) |
+              (variable->data.image_volatile ? ACCESS_VOLATILE : 0));
+   }
 }
 
 ir_call *
@@ -394,6 +445,10 @@ lower_ubo_reference_visitor::ssbo_store(void *mem_ctx,
       ir_variable(glsl_type::uint_type, "write_mask" , ir_var_function_in);
    sig_params.push_tail(writemask_ref);
 
+   ir_variable *access_ref = new(mem_ctx)
+      ir_variable(glsl_type::uint_type, "access" , ir_var_function_in);
+   sig_params.push_tail(access_ref);
+
    ir_function_signature *sig = new(mem_ctx)
       ir_function_signature(glsl_type::void_type, shader_storage_buffer_object);
    assert(sig);
@@ -408,6 +463,7 @@ lower_ubo_reference_visitor::ssbo_store(void *mem_ctx,
    call_params.push_tail(offset->clone(mem_ctx, NULL));
    call_params.push_tail(deref->clone(mem_ctx, NULL));
    call_params.push_tail(new(mem_ctx) ir_constant(write_mask));
+   call_params.push_tail(new(mem_ctx) ir_constant(ssbo_access_params()));
    return new(mem_ctx) ir_call(sig, NULL, &call_params);
 }
 
@@ -426,6 +482,10 @@ lower_ubo_reference_visitor::ssbo_load(void *mem_ctx,
       ir_variable(glsl_type::uint_type, "offset_ref" , ir_var_function_in);
    sig_params.push_tail(offset_ref);
 
+   ir_variable *access_ref = new(mem_ctx)
+      ir_variable(glsl_type::uint_type, "access" , ir_var_function_in);
+   sig_params.push_tail(access_ref);
+
    ir_function_signature *sig =
       new(mem_ctx) ir_function_signature(type, shader_storage_buffer_object);
    assert(sig);
@@ -444,6 +504,7 @@ lower_ubo_reference_visitor::ssbo_load(void *mem_ctx,
    exec_list call_params;
    call_params.push_tail(this->uniform_block->clone(mem_ctx, NULL));
    call_params.push_tail(offset->clone(mem_ctx, NULL));
+   call_params.push_tail(new(mem_ctx) ir_constant(ssbo_access_params()));
 
    return new(mem_ctx) ir_call(sig, deref_result, &call_params);
 }
@@ -496,9 +557,10 @@ lower_ubo_reference_visitor::write_to_memory(void *mem_ctx,
    unsigned const_offset;
    bool row_major;
    int matrix_columns;
-   unsigned packing = var->get_interface_type()->interface_packing;
+   enum glsl_interface_packing packing = var->get_interface_type_packing();
 
    this->buffer_access_type = ssbo_store_access;
+   this->variable = var;
 
    /* Compute the offset to the start if the dereference as well as other
     * information we need to configure the write
@@ -604,7 +666,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;
 
@@ -674,10 +736,11 @@ lower_ubo_reference_visitor::process_ssbo_unsized_array_length(ir_rvalue **rvalu
    unsigned const_offset;
    bool row_major;
    int matrix_columns;
-   unsigned packing = var->get_interface_type()->interface_packing;
+   enum glsl_interface_packing packing = var->get_interface_type_packing();
    int unsized_array_stride = calculate_unsized_array_stride(deref, packing);
 
    this->buffer_access_type = ssbo_unsized_array_length_access;
+   this->variable = var;
 
    /* Compute the offset to the start if the dereference as well as other
     * information we need to calculate the length.
@@ -907,9 +970,10 @@ lower_ubo_reference_visitor::lower_ssbo_atomic_intrinsic(ir_call *ir)
    unsigned const_offset;
    bool row_major;
    int matrix_columns;
-   unsigned packing = var->get_interface_type()->interface_packing;
+   enum glsl_interface_packing packing = var->get_interface_type_packing();
 
    this->buffer_access_type = ssbo_atomic_access;
+   this->variable = var;
 
    setup_for_load_or_store(mem_ctx, var, deref,
                            &offset, &const_offset,
@@ -1026,9 +1090,9 @@ lower_ubo_reference_visitor::visit_enter(ir_call *ir)
 } /* unnamed namespace */
 
 void
-lower_ubo_reference(struct gl_shader *shader)
+lower_ubo_reference(struct gl_linked_shader *shader, bool clamp_block_indices)
 {
-   lower_ubo_reference_visitor v(shader);
+   lower_ubo_reference_visitor v(shader, clamp_block_indices);
 
    /* Loop over the instructions lowering references, because we take
     * a deref of a UBO array using a UBO dereference as the index will