• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright © 2010 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  */
23 
24 #include "ast.h"
25 #include "compiler/glsl_types.h"
26 #include "ir.h"
27 
28 void
print(void) const29 ast_array_specifier::print(void) const
30 {
31    foreach_list_typed (ast_node, array_dimension, link, &this->array_dimensions) {
32       printf("[ ");
33       if (((ast_expression*)array_dimension)->oper != ast_unsized_array_dim)
34          array_dimension->print();
35       printf("] ");
36    }
37 }
38 
39 /**
40  * If \c ir is a reference to an array for which we are tracking the max array
41  * element accessed, track that the given element has been accessed.
42  * Otherwise do nothing.
43  *
44  * This function also checks whether the array is a built-in array whose
45  * maximum size is too small to accommodate the given index, and if so uses
46  * loc and state to report the error.
47  */
48 static void
update_max_array_access(ir_rvalue * ir,int idx,YYLTYPE * loc,struct _mesa_glsl_parse_state * state)49 update_max_array_access(ir_rvalue *ir, int idx, YYLTYPE *loc,
50                         struct _mesa_glsl_parse_state *state)
51 {
52    if (ir_dereference_variable *deref_var = ir->as_dereference_variable()) {
53       ir_variable *var = deref_var->var;
54       if (idx > (int)var->data.max_array_access) {
55          var->data.max_array_access = idx;
56 
57          /* Check whether this access will, as a side effect, implicitly cause
58           * the size of a built-in array to be too large.
59           */
60          check_builtin_array_max_size(var->name, idx+1, *loc, state);
61       }
62    } else if (ir_dereference_record *deref_record =
63               ir->as_dereference_record()) {
64       /* There are three possibilities we need to consider:
65        *
66        * - Accessing an element of an array that is a member of a named
67        *   interface block (e.g. ifc.foo[i])
68        *
69        * - Accessing an element of an array that is a member of a named
70        *   interface block array (e.g. ifc[j].foo[i]).
71        *
72        * - Accessing an element of an array that is a member of a named
73        *   interface block array of arrays (e.g. ifc[j][k].foo[i]).
74        */
75       ir_dereference_variable *deref_var =
76          deref_record->record->as_dereference_variable();
77       if (deref_var == NULL) {
78          ir_dereference_array *deref_array =
79             deref_record->record->as_dereference_array();
80          ir_dereference_array *deref_array_prev = NULL;
81          while (deref_array != NULL) {
82             deref_array_prev = deref_array;
83             deref_array = deref_array->array->as_dereference_array();
84          }
85          if (deref_array_prev != NULL)
86             deref_var = deref_array_prev->array->as_dereference_variable();
87       }
88 
89       if (deref_var != NULL) {
90          if (deref_var->var->is_interface_instance()) {
91             unsigned field_idx = deref_record->field_idx;
92             assert(field_idx < deref_var->var->get_interface_type()->length);
93 
94             int *const max_ifc_array_access =
95                deref_var->var->get_max_ifc_array_access();
96 
97             assert(max_ifc_array_access != NULL);
98 
99             if (idx > max_ifc_array_access[field_idx]) {
100                max_ifc_array_access[field_idx] = idx;
101 
102                /* Check whether this access will, as a side effect, implicitly
103                 * cause the size of a built-in array to be too large.
104                 */
105                const char *field_name =
106                   deref_record->record->type->fields.structure[field_idx].name;
107                check_builtin_array_max_size(field_name, idx+1, *loc, state);
108             }
109          }
110       }
111    }
112 }
113 
114 
115 static int
get_implicit_array_size(struct _mesa_glsl_parse_state * state,ir_rvalue * array)116 get_implicit_array_size(struct _mesa_glsl_parse_state *state,
117                         ir_rvalue *array)
118 {
119    ir_variable *var = array->variable_referenced();
120 
121    /* Inputs in control shader are implicitly sized
122     * to the maximum patch size.
123     */
124    if (state->stage == MESA_SHADER_TESS_CTRL &&
125        var->data.mode == ir_var_shader_in) {
126       return state->Const.MaxPatchVertices;
127    }
128 
129    /* Non-patch inputs in evaluation shader are implicitly sized
130     * to the maximum patch size.
131     */
132    if (state->stage == MESA_SHADER_TESS_EVAL &&
133        var->data.mode == ir_var_shader_in &&
134        !var->data.patch) {
135       return state->Const.MaxPatchVertices;
136    }
137 
138    return 0;
139 }
140 
141 
142 ir_rvalue *
_mesa_ast_array_index_to_hir(void * mem_ctx,struct _mesa_glsl_parse_state * state,ir_rvalue * array,ir_rvalue * idx,YYLTYPE & loc,YYLTYPE & idx_loc)143 _mesa_ast_array_index_to_hir(void *mem_ctx,
144                              struct _mesa_glsl_parse_state *state,
145                              ir_rvalue *array, ir_rvalue *idx,
146                              YYLTYPE &loc, YYLTYPE &idx_loc)
147 {
148    if (!array->type->is_error()
149        && !array->type->is_array()
150        && !array->type->is_matrix()
151        && !array->type->is_vector()) {
152       _mesa_glsl_error(& idx_loc, state,
153                        "cannot dereference non-array / non-matrix / "
154                        "non-vector");
155    }
156 
157    if (!idx->type->is_error()) {
158       if (!idx->type->is_integer()) {
159          _mesa_glsl_error(& idx_loc, state, "array index must be integer type");
160       } else if (!idx->type->is_scalar()) {
161          _mesa_glsl_error(& idx_loc, state, "array index must be scalar");
162       }
163    }
164 
165    /* If the array index is a constant expression and the array has a
166     * declared size, ensure that the access is in-bounds.  If the array
167     * index is not a constant expression, ensure that the array has a
168     * declared size.
169     */
170    ir_constant *const const_index = idx->constant_expression_value(mem_ctx);
171    if (const_index != NULL && idx->type->is_integer()) {
172       const int idx = const_index->value.i[0];
173       const char *type_name = "error";
174       unsigned bound = 0;
175 
176       /* From page 24 (page 30 of the PDF) of the GLSL 1.50 spec:
177        *
178        *    "It is illegal to declare an array with a size, and then
179        *    later (in the same shader) index the same array with an
180        *    integral constant expression greater than or equal to the
181        *    declared size. It is also illegal to index an array with a
182        *    negative constant expression."
183        */
184       if (array->type->is_matrix()) {
185          if (array->type->row_type()->vector_elements <= idx) {
186             type_name = "matrix";
187             bound = array->type->row_type()->vector_elements;
188          }
189       } else if (array->type->is_vector()) {
190          if (array->type->vector_elements <= idx) {
191             type_name = "vector";
192             bound = array->type->vector_elements;
193          }
194       } else {
195          /* glsl_type::array_size() returns -1 for non-array types.  This means
196           * that we don't need to verify that the type is an array before
197           * doing the bounds checking.
198           */
199          if ((array->type->array_size() > 0)
200              && (array->type->array_size() <= idx)) {
201             type_name = "array";
202             bound = array->type->array_size();
203          }
204       }
205 
206       if (bound > 0) {
207          _mesa_glsl_error(& loc, state, "%s index must be < %u",
208                           type_name, bound);
209       } else if (idx < 0) {
210          _mesa_glsl_error(& loc, state, "%s index must be >= 0", type_name);
211       }
212 
213       if (array->type->is_array())
214          update_max_array_access(array, idx, &loc, state);
215    } else if (const_index == NULL && array->type->is_array()) {
216       if (array->type->is_unsized_array()) {
217          int implicit_size = get_implicit_array_size(state, array);
218          if (implicit_size) {
219             ir_variable *v = array->whole_variable_referenced();
220             if (v != NULL)
221                v->data.max_array_access = implicit_size - 1;
222          }
223          else if (state->stage == MESA_SHADER_TESS_CTRL &&
224                   array->variable_referenced()->data.mode == ir_var_shader_out &&
225                   !array->variable_referenced()->data.patch) {
226             /* Tessellation control shader output non-patch arrays are
227              * initially unsized. Despite that, they are allowed to be
228              * indexed with a non-constant expression (typically
229              * "gl_InvocationID"). The array size will be determined
230              * by the linker.
231              */
232          }
233          else if (array->variable_referenced()->data.mode !=
234                   ir_var_shader_storage) {
235             _mesa_glsl_error(&loc, state, "unsized array index must be constant");
236          } else {
237             /* Unsized array non-constant indexing on SSBO is allowed only for
238              * the last member of the SSBO definition.
239              */
240             ir_variable *var = array->variable_referenced();
241             const glsl_type *iface_type = var->get_interface_type();
242             int field_index = iface_type->field_index(var->name);
243             /* Field index can be < 0 for instance arrays */
244             if (field_index >= 0 &&
245                 field_index != (int) iface_type->length - 1) {
246                _mesa_glsl_error(&loc, state, "Indirect access on unsized "
247                                 "array is limited to the last member of "
248                                 "SSBO.");
249             }
250          }
251       } else if (array->type->without_array()->is_interface()
252                  && ((array->variable_referenced()->data.mode == ir_var_uniform
253                       && !state->is_version(400, 320)
254                       && !state->ARB_gpu_shader5_enable
255                       && !state->EXT_gpu_shader5_enable
256                       && !state->OES_gpu_shader5_enable) ||
257                      (array->variable_referenced()->data.mode == ir_var_shader_storage
258                       && !state->is_version(400, 0)
259                       && !state->ARB_gpu_shader5_enable))) {
260          /* Page 50 in section 4.3.9 of the OpenGL ES 3.10 spec says:
261           *
262           *     "All indices used to index a uniform or shader storage block
263           *     array must be constant integral expressions."
264           *
265           * But OES_gpu_shader5 (and ESSL 3.20) relax this to allow indexing
266           * on uniform blocks but not shader storage blocks.
267           *
268           */
269          _mesa_glsl_error(&loc, state, "%s block array index must be constant",
270                           array->variable_referenced()->data.mode
271                           == ir_var_uniform ? "uniform" : "shader storage");
272       } else {
273          /* whole_variable_referenced can return NULL if the array is a
274           * member of a structure.  In this case it is safe to not update
275           * the max_array_access field because it is never used for fields
276           * of structures.
277           */
278          ir_variable *v = array->whole_variable_referenced();
279          if (v != NULL)
280             v->data.max_array_access = array->type->array_size() - 1;
281       }
282 
283       /* From page 23 (29 of the PDF) of the GLSL 1.30 spec:
284        *
285        *    "Samplers aggregated into arrays within a shader (using square
286        *    brackets [ ]) can only be indexed with integral constant
287        *    expressions [...]."
288        *
289        * This restriction was added in GLSL 1.30.  Shaders using earlier
290        * version of the language should not be rejected by the compiler
291        * front-end for using this construct.  This allows useful things such
292        * as using a loop counter as the index to an array of samplers.  If the
293        * loop in unrolled, the code should compile correctly.  Instead, emit a
294        * warning.
295        *
296        * In GLSL 4.00 / ARB_gpu_shader5, this requirement is relaxed again to allow
297        * indexing with dynamically uniform expressions. Note that these are not
298        * required to be uniforms or expressions based on them, but merely that the
299        * values must not diverge between shader invocations run together. If the
300        * values *do* diverge, then the behavior of the operation requiring a
301        * dynamically uniform expression is undefined.
302        *
303        * From section 4.1.7 of the ARB_bindless_texture spec:
304        *
305        *    "Samplers aggregated into arrays within a shader (using square
306        *    brackets []) can be indexed with arbitrary integer expressions."
307        */
308       if (array->type->without_array()->is_sampler()) {
309          if (!state->is_version(400, 320) &&
310              !state->ARB_gpu_shader5_enable &&
311              !state->EXT_gpu_shader5_enable &&
312              !state->OES_gpu_shader5_enable &&
313              !state->has_bindless()) {
314             if (state->is_version(130, 300))
315                _mesa_glsl_error(&loc, state,
316                                 "sampler arrays indexed with non-constant "
317                                 "expressions are forbidden in GLSL %s "
318                                 "and later",
319                                 state->es_shader ? "ES 3.00" : "1.30");
320             else if (state->es_shader)
321                _mesa_glsl_warning(&loc, state,
322                                   "sampler arrays indexed with non-constant "
323                                   "expressions will be forbidden in GLSL "
324                                   "3.00 and later");
325             else
326                _mesa_glsl_warning(&loc, state,
327                                   "sampler arrays indexed with non-constant "
328                                   "expressions will be forbidden in GLSL "
329                                   "1.30 and later");
330          }
331       }
332 
333       /* From page 27 of the GLSL ES 3.1 specification:
334        *
335        * "When aggregated into arrays within a shader, images can only be
336        *  indexed with a constant integral expression."
337        *
338        * On the other hand the desktop GL specification extension allows
339        * non-constant indexing of image arrays, but behavior is left undefined
340        * in cases where the indexing expression is not dynamically uniform.
341        */
342       if (state->es_shader && array->type->without_array()->is_image()) {
343          _mesa_glsl_error(&loc, state,
344                           "image arrays indexed with non-constant "
345                           "expressions are forbidden in GLSL ES.");
346       }
347    }
348 
349    /* After performing all of the error checking, generate the IR for the
350     * expression.
351     */
352    if (array->type->is_array()
353        || array->type->is_matrix()
354        || array->type->is_vector()) {
355       return new(mem_ctx) ir_dereference_array(array, idx);
356    } else if (array->type->is_error()) {
357       return array;
358    } else {
359       ir_rvalue *result = new(mem_ctx) ir_dereference_array(array, idx);
360       result->type = glsl_type::error_type;
361 
362       return result;
363    }
364 }
365