2 * Copyright © 2010 Intel Corporation
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:
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
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.
25 #include "glsl_types.h"
29 ast_array_specifier::print(void) const
31 if (this->is_unsized_array
) {
35 foreach_list_typed (ast_node
, array_dimension
, link
, &this->array_dimensions
) {
37 array_dimension
->print();
43 * If \c ir is a reference to an array for which we are tracking the max array
44 * element accessed, track that the given element has been accessed.
45 * Otherwise do nothing.
47 * This function also checks whether the array is a built-in array whose
48 * maximum size is too small to accommodate the given index, and if so uses
49 * loc and state to report the error.
52 update_max_array_access(ir_rvalue
*ir
, int idx
, YYLTYPE
*loc
,
53 struct _mesa_glsl_parse_state
*state
)
55 if (ir_dereference_variable
*deref_var
= ir
->as_dereference_variable()) {
56 ir_variable
*var
= deref_var
->var
;
57 if (idx
> (int)var
->data
.max_array_access
) {
58 var
->data
.max_array_access
= idx
;
60 /* Check whether this access will, as a side effect, implicitly cause
61 * the size of a built-in array to be too large.
63 check_builtin_array_max_size(var
->name
, idx
+1, *loc
, state
);
65 } else if (ir_dereference_record
*deref_record
=
66 ir
->as_dereference_record()) {
67 /* There are two possibilities we need to consider:
69 * - Accessing an element of an array that is a member of a named
70 * interface block (e.g. ifc.foo[i])
72 * - Accessing an element of an array that is a member of a named
73 * interface block array (e.g. ifc[j].foo[i]).
75 ir_dereference_variable
*deref_var
=
76 deref_record
->record
->as_dereference_variable();
77 if (deref_var
== NULL
) {
78 if (ir_dereference_array
*deref_array
=
79 deref_record
->record
->as_dereference_array()) {
80 deref_var
= deref_array
->array
->as_dereference_variable();
84 if (deref_var
!= NULL
) {
85 if (deref_var
->var
->is_interface_instance()) {
86 const glsl_type
*interface_type
=
87 deref_var
->var
->get_interface_type();
88 unsigned field_index
=
89 deref_record
->record
->type
->field_index(deref_record
->field
);
90 assert(field_index
< interface_type
->length
);
92 unsigned *const max_ifc_array_access
=
93 deref_var
->var
->get_max_ifc_array_access();
95 assert(max_ifc_array_access
!= NULL
);
97 if (idx
> (int)max_ifc_array_access
[field_index
]) {
98 max_ifc_array_access
[field_index
] = idx
;
100 /* Check whether this access will, as a side effect, implicitly
101 * cause the size of a built-in array to be too large.
103 check_builtin_array_max_size(deref_record
->field
, idx
+1, *loc
,
113 _mesa_ast_array_index_to_hir(void *mem_ctx
,
114 struct _mesa_glsl_parse_state
*state
,
115 ir_rvalue
*array
, ir_rvalue
*idx
,
116 YYLTYPE
&loc
, YYLTYPE
&idx_loc
)
118 if (!array
->type
->is_error()
119 && !array
->type
->is_array()
120 && !array
->type
->is_matrix()
121 && !array
->type
->is_vector()) {
122 _mesa_glsl_error(& idx_loc
, state
,
123 "cannot dereference non-array / non-matrix / "
127 if (!idx
->type
->is_error()) {
128 if (!idx
->type
->is_integer()) {
129 _mesa_glsl_error(& idx_loc
, state
, "array index must be integer type");
130 } else if (!idx
->type
->is_scalar()) {
131 _mesa_glsl_error(& idx_loc
, state
, "array index must be scalar");
135 /* If the array index is a constant expression and the array has a
136 * declared size, ensure that the access is in-bounds. If the array
137 * index is not a constant expression, ensure that the array has a
140 ir_constant
*const const_index
= idx
->constant_expression_value();
141 if (const_index
!= NULL
&& idx
->type
->is_integer()) {
142 const int idx
= const_index
->value
.i
[0];
143 const char *type_name
= "error";
146 /* From page 24 (page 30 of the PDF) of the GLSL 1.50 spec:
148 * "It is illegal to declare an array with a size, and then
149 * later (in the same shader) index the same array with an
150 * integral constant expression greater than or equal to the
151 * declared size. It is also illegal to index an array with a
152 * negative constant expression."
154 if (array
->type
->is_matrix()) {
155 if (array
->type
->row_type()->vector_elements
<= idx
) {
156 type_name
= "matrix";
157 bound
= array
->type
->row_type()->vector_elements
;
159 } else if (array
->type
->is_vector()) {
160 if (array
->type
->vector_elements
<= idx
) {
161 type_name
= "vector";
162 bound
= array
->type
->vector_elements
;
165 /* glsl_type::array_size() returns -1 for non-array types. This means
166 * that we don't need to verify that the type is an array before
167 * doing the bounds checking.
169 if ((array
->type
->array_size() > 0)
170 && (array
->type
->array_size() <= idx
)) {
172 bound
= array
->type
->array_size();
177 _mesa_glsl_error(& loc
, state
, "%s index must be < %u",
179 } else if (idx
< 0) {
180 _mesa_glsl_error(& loc
, state
, "%s index must be >= 0",
184 if (array
->type
->is_array())
185 update_max_array_access(array
, idx
, &loc
, state
);
186 } else if (const_index
== NULL
&& array
->type
->is_array()) {
187 if (array
->type
->is_unsized_array()) {
188 _mesa_glsl_error(&loc
, state
, "unsized array index must be constant");
189 } else if (array
->type
->fields
.array
->is_interface()
190 && array
->variable_referenced()->data
.mode
== ir_var_uniform
191 && !state
->is_version(400, 0) && !state
->ARB_gpu_shader5_enable
) {
192 /* Page 46 in section 4.3.7 of the OpenGL ES 3.00 spec says:
194 * "All indexes used to index a uniform block array must be
195 * constant integral expressions."
197 _mesa_glsl_error(&loc
, state
,
198 "uniform block array index must be constant");
200 /* whole_variable_referenced can return NULL if the array is a
201 * member of a structure. In this case it is safe to not update
202 * the max_array_access field because it is never used for fields
205 ir_variable
*v
= array
->whole_variable_referenced();
207 v
->data
.max_array_access
= array
->type
->array_size() - 1;
210 /* From page 23 (29 of the PDF) of the GLSL 1.30 spec:
212 * "Samplers aggregated into arrays within a shader (using square
213 * brackets [ ]) can only be indexed with integral constant
214 * expressions [...]."
216 * This restriction was added in GLSL 1.30. Shaders using earlier
217 * version of the language should not be rejected by the compiler
218 * front-end for using this construct. This allows useful things such
219 * as using a loop counter as the index to an array of samplers. If the
220 * loop in unrolled, the code should compile correctly. Instead, emit a
223 * In GLSL 4.00 / ARB_gpu_shader5, this requirement is relaxed again to allow
224 * indexing with dynamically uniform expressions. Note that these are not
225 * required to be uniforms or expressions based on them, but merely that the
226 * values must not diverge between shader invocations run together. If the
227 * values *do* diverge, then the behavior of the operation requiring a
228 * dynamically uniform expression is undefined.
230 if (array
->type
->element_type()->is_sampler()) {
231 if (!state
->is_version(130, 100)) {
232 if (state
->es_shader
) {
233 _mesa_glsl_warning(&loc
, state
,
234 "sampler arrays indexed with non-constant "
235 "expressions is optional in %s",
236 state
->get_version_string());
238 _mesa_glsl_warning(&loc
, state
,
239 "sampler arrays indexed with non-constant "
240 "expressions will be forbidden in GLSL 1.30 "
243 } else if (!state
->is_version(400, 0) && !state
->ARB_gpu_shader5_enable
) {
244 _mesa_glsl_error(&loc
, state
,
245 "sampler arrays indexed with non-constant "
246 "expressions is forbidden in GLSL 1.30 and "
252 /* After performing all of the error checking, generate the IR for the
255 if (array
->type
->is_array()
256 || array
->type
->is_matrix()) {
257 return new(mem_ctx
) ir_dereference_array(array
, idx
);
258 } else if (array
->type
->is_vector()) {
259 return new(mem_ctx
) ir_expression(ir_binop_vector_extract
, array
, idx
);
260 } else if (array
->type
->is_error()) {
263 ir_rvalue
*result
= new(mem_ctx
) ir_dereference_array(array
, idx
);
264 result
->type
= glsl_type::error_type
;