nir/spirv: Move CF emit code into vtn_cfg.c
[mesa.git] / src / glsl / ast_array_index.cpp
index 752d86f72fd8e86e2177cf89be762f2daef1c13b..ca7a9a10c36730f2818f5a1eb6551836ec06b817 100644 (file)
 void
 ast_array_specifier::print(void) const
 {
-   if (this->is_unsized_array) {
-      printf("[ ] ");
-   }
-
    foreach_list_typed (ast_node, array_dimension, link, &this->array_dimensions) {
       printf("[ ");
-      array_dimension->print();
+      if (((ast_expression*)array_dimension)->oper != ast_unsized_array_dim)
+         array_dimension->print();
       printf("] ");
    }
 }
@@ -64,21 +61,29 @@ update_max_array_access(ir_rvalue *ir, int idx, YYLTYPE *loc,
       }
    } else if (ir_dereference_record *deref_record =
               ir->as_dereference_record()) {
-      /* There are two possibilities we need to consider:
+      /* There are three possibilities we need to consider:
        *
        * - Accessing an element of an array that is a member of a named
        *   interface block (e.g. ifc.foo[i])
        *
        * - Accessing an element of an array that is a member of a named
        *   interface block array (e.g. ifc[j].foo[i]).
+       *
+       * - Accessing an element of an array that is a member of a named
+       *   interface block array of arrays (e.g. ifc[j][k].foo[i]).
        */
       ir_dereference_variable *deref_var =
          deref_record->record->as_dereference_variable();
       if (deref_var == NULL) {
-         if (ir_dereference_array *deref_array =
-             deref_record->record->as_dereference_array()) {
-            deref_var = deref_array->array->as_dereference_variable();
+         ir_dereference_array *deref_array =
+            deref_record->record->as_dereference_array();
+         ir_dereference_array *deref_array_prev = NULL;
+         while (deref_array != NULL) {
+            deref_array_prev = deref_array;
+            deref_array = deref_array->array->as_dereference_array();
          }
+         if (deref_array_prev != NULL)
+            deref_var = deref_array_prev->array->as_dereference_variable();
       }
 
       if (deref_var != NULL) {
@@ -107,6 +112,33 @@ update_max_array_access(ir_rvalue *ir, int idx, YYLTYPE *loc,
 }
 
 
+static int
+get_implicit_array_size(struct _mesa_glsl_parse_state *state,
+                        ir_rvalue *array)
+{
+   ir_variable *var = array->variable_referenced();
+
+   /* Inputs in control shader are implicitly sized
+    * to the maximum patch size.
+    */
+   if (state->stage == MESA_SHADER_TESS_CTRL &&
+       var->data.mode == ir_var_shader_in) {
+      return state->Const.MaxPatchVertices;
+   }
+
+   /* Non-patch inputs in evaluation shader are implicitly sized
+    * to the maximum patch size.
+    */
+   if (state->stage == MESA_SHADER_TESS_EVAL &&
+       var->data.mode == ir_var_shader_in &&
+       !var->data.patch) {
+      return state->Const.MaxPatchVertices;
+   }
+
+   return 0;
+}
+
+
 ir_rvalue *
 _mesa_ast_array_index_to_hir(void *mem_ctx,
                             struct _mesa_glsl_parse_state *state,
@@ -183,17 +215,38 @@ _mesa_ast_array_index_to_hir(void *mem_ctx,
          update_max_array_access(array, idx, &loc, state);
    } else if (const_index == NULL && array->type->is_array()) {
       if (array->type->is_unsized_array()) {
-        _mesa_glsl_error(&loc, state, "unsized array index must be constant");
-      } else if (array->type->fields.array->is_interface()
-                 && array->variable_referenced()->data.mode == ir_var_uniform
+         int implicit_size = get_implicit_array_size(state, array);
+         if (implicit_size) {
+            ir_variable *v = array->whole_variable_referenced();
+            if (v != NULL)
+               v->data.max_array_access = implicit_size - 1;
+         }
+         else if (state->stage == MESA_SHADER_TESS_CTRL &&
+                  array->variable_referenced()->data.mode == ir_var_shader_out &&
+                  !array->variable_referenced()->data.patch) {
+            /* Tessellation control shader output non-patch arrays are
+             * initially unsized. Despite that, they are allowed to be
+             * indexed with a non-constant expression (typically
+             * "gl_InvocationID"). The array size will be determined
+             * by the linker.
+             */
+         }
+         else if (array->variable_referenced()->data.mode !=
+                  ir_var_shader_storage) {
+            _mesa_glsl_error(&loc, state, "unsized array index must be constant");
+         }
+      } else if (array->type->without_array()->is_interface()
+                 && (array->variable_referenced()->data.mode == ir_var_uniform ||
+                     array->variable_referenced()->data.mode == ir_var_shader_storage)
                  && !state->is_version(400, 0) && !state->ARB_gpu_shader5_enable) {
-        /* Page 46 in section 4.3.7 of the OpenGL ES 3.00 spec says:
+        /* Page 50 in section 4.3.9 of the OpenGL ES 3.10 spec says:
          *
-         *     "All indexes used to index a uniform block array must be
-         *     constant integral expressions."
+         *     "All indices used to index a uniform or shader storage block
+         *     array must be constant integral expressions."
          */
-        _mesa_glsl_error(&loc, state,
-                         "uniform block array index must be constant");
+        _mesa_glsl_error(&loc, state, "%s block array index must be constant",
+                          array->variable_referenced()->data.mode
+                          == ir_var_uniform ? "uniform" : "shader storage");
       } else {
         /* whole_variable_referenced can return NULL if the array is a
          * member of a structure.  In this case it is safe to not update
@@ -226,24 +279,39 @@ _mesa_ast_array_index_to_hir(void *mem_ctx,
        * dynamically uniform expression is undefined.
        */
       if (array->type->without_array()->is_sampler()) {
-        if (!state->is_version(130, 100)) {
-           if (state->es_shader) {
-              _mesa_glsl_warning(&loc, state,
-                                 "sampler arrays indexed with non-constant "
-                                 "expressions is optional in %s",
-                                 state->get_version_string());
-           } else {
-              _mesa_glsl_warning(&loc, state,
-                                 "sampler arrays indexed with non-constant "
-                                 "expressions will be forbidden in GLSL 1.30 "
-                                 "and later");
-           }
-        } else if (!state->is_version(400, 0) && !state->ARB_gpu_shader5_enable) {
-           _mesa_glsl_error(&loc, state,
-                            "sampler arrays indexed with non-constant "
-                            "expressions is forbidden in GLSL 1.30 and "
-                            "later");
-        }
+         if (!state->is_version(400, 0) && !state->ARB_gpu_shader5_enable) {
+            if (state->is_version(130, 300))
+               _mesa_glsl_error(&loc, state,
+                                "sampler arrays indexed with non-constant "
+                                "expressions are forbidden in GLSL %s "
+                                "and later",
+                                state->es_shader ? "ES 3.00" : "1.30");
+            else if (state->es_shader)
+               _mesa_glsl_warning(&loc, state,
+                                  "sampler arrays indexed with non-constant "
+                                  "expressions will be forbidden in GLSL "
+                                  "3.00 and later");
+            else
+               _mesa_glsl_warning(&loc, state,
+                                  "sampler arrays indexed with non-constant "
+                                  "expressions will be forbidden in GLSL "
+                                  "1.30 and later");
+         }
+      }
+
+      /* From page 27 of the GLSL ES 3.1 specification:
+       *
+       * "When aggregated into arrays within a shader, images can only be
+       *  indexed with a constant integral expression."
+       *
+       * On the other hand the desktop GL specification extension allows
+       * non-constant indexing of image arrays, but behavior is left undefined
+       * in cases where the indexing expression is not dynamically uniform.
+       */
+      if (state->es_shader && array->type->without_array()->is_image()) {
+         _mesa_glsl_error(&loc, state,
+                          "image arrays indexed with non-constant "
+                          "expressions are forbidden in GLSL ES.");
       }
    }
 
@@ -251,10 +319,9 @@ _mesa_ast_array_index_to_hir(void *mem_ctx,
     * expression.
     */
    if (array->type->is_array()
-       || array->type->is_matrix()) {
+       || array->type->is_matrix()
+       || array->type->is_vector()) {
       return new(mem_ctx) ir_dereference_array(array, idx);
-   } else if (array->type->is_vector()) {
-      return new(mem_ctx) ir_expression(ir_binop_vector_extract, array, idx);
    } else if (array->type->is_error()) {
       return array;
    } else {