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 foreach_list_typed (ast_node
, array_dimension
, link
, &this->array_dimensions
) {
33 if (((ast_expression
*)array_dimension
)->oper
!= ast_unsized_array_dim
)
34 array_dimension
->print();
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.
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.
49 update_max_array_access(ir_rvalue
*ir
, int idx
, YYLTYPE
*loc
,
50 struct _mesa_glsl_parse_state
*state
)
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
;
57 /* Check whether this access will, as a side effect, implicitly cause
58 * the size of a built-in array to be too large.
60 check_builtin_array_max_size(var
->name
, idx
+1, *loc
, state
);
62 } else if (ir_dereference_record
*deref_record
=
63 ir
->as_dereference_record()) {
64 /* There are three possibilities we need to consider:
66 * - Accessing an element of an array that is a member of a named
67 * interface block (e.g. ifc.foo[i])
69 * - Accessing an element of an array that is a member of a named
70 * interface block array (e.g. ifc[j].foo[i]).
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]).
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();
85 if (deref_array_prev
!= NULL
)
86 deref_var
= deref_array_prev
->array
->as_dereference_variable();
89 if (deref_var
!= NULL
) {
90 if (deref_var
->var
->is_interface_instance()) {
91 unsigned field_index
=
92 deref_record
->record
->type
->field_index(deref_record
->field
);
93 assert(field_index
< deref_var
->var
->get_interface_type()->length
);
95 unsigned *const max_ifc_array_access
=
96 deref_var
->var
->get_max_ifc_array_access();
98 assert(max_ifc_array_access
!= NULL
);
100 if (idx
> (int)max_ifc_array_access
[field_index
]) {
101 max_ifc_array_access
[field_index
] = idx
;
103 /* Check whether this access will, as a side effect, implicitly
104 * cause the size of a built-in array to be too large.
106 check_builtin_array_max_size(deref_record
->field
, idx
+1, *loc
,
116 get_implicit_array_size(struct _mesa_glsl_parse_state
*state
,
119 ir_variable
*var
= array
->variable_referenced();
121 /* Inputs in control shader are implicitly sized
122 * to the maximum patch size.
124 if (state
->stage
== MESA_SHADER_TESS_CTRL
&&
125 var
->data
.mode
== ir_var_shader_in
) {
126 return state
->Const
.MaxPatchVertices
;
129 /* Non-patch inputs in evaluation shader are implicitly sized
130 * to the maximum patch size.
132 if (state
->stage
== MESA_SHADER_TESS_EVAL
&&
133 var
->data
.mode
== ir_var_shader_in
&&
135 return state
->Const
.MaxPatchVertices
;
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
)
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 / "
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");
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
170 ir_constant
*const const_index
= idx
->constant_expression_value();
171 if (const_index
!= NULL
&& idx
->type
->is_integer()) {
172 const int idx
= const_index
->value
.i
[0];
173 const char *type_name
= "error";
176 /* From page 24 (page 30 of the PDF) of the GLSL 1.50 spec:
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."
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
;
189 } else if (array
->type
->is_vector()) {
190 if (array
->type
->vector_elements
<= idx
) {
191 type_name
= "vector";
192 bound
= array
->type
->vector_elements
;
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.
199 if ((array
->type
->array_size() > 0)
200 && (array
->type
->array_size() <= idx
)) {
202 bound
= array
->type
->array_size();
207 _mesa_glsl_error(& loc
, state
, "%s index must be < %u",
209 } else if (idx
< 0) {
210 _mesa_glsl_error(& loc
, state
, "%s index must be >= 0",
214 if (array
->type
->is_array())
215 update_max_array_access(array
, idx
, &loc
, state
);
216 } else if (const_index
== NULL
&& array
->type
->is_array()) {
217 if (array
->type
->is_unsized_array()) {
218 int implicit_size
= get_implicit_array_size(state
, array
);
220 ir_variable
*v
= array
->whole_variable_referenced();
222 v
->data
.max_array_access
= implicit_size
- 1;
224 else if (state
->stage
== MESA_SHADER_TESS_CTRL
&&
225 array
->variable_referenced()->data
.mode
== ir_var_shader_out
&&
226 !array
->variable_referenced()->data
.patch
) {
227 /* Tessellation control shader output non-patch arrays are
228 * initially unsized. Despite that, they are allowed to be
229 * indexed with a non-constant expression (typically
230 * "gl_InvocationID"). The array size will be determined
234 else if (array
->variable_referenced()->data
.mode
!=
235 ir_var_shader_storage
) {
236 _mesa_glsl_error(&loc
, state
, "unsized array index must be constant");
238 } else if (array
->type
->without_array()->is_interface()
239 && (array
->variable_referenced()->data
.mode
== ir_var_uniform
||
240 array
->variable_referenced()->data
.mode
== ir_var_shader_storage
)
241 && !state
->is_version(400, 0) && !state
->ARB_gpu_shader5_enable
) {
242 /* Page 50 in section 4.3.9 of the OpenGL ES 3.10 spec says:
244 * "All indices used to index a uniform or shader storage block
245 * array must be constant integral expressions."
247 _mesa_glsl_error(&loc
, state
, "%s block array index must be constant",
248 array
->variable_referenced()->data
.mode
249 == ir_var_uniform
? "uniform" : "shader storage");
251 /* whole_variable_referenced can return NULL if the array is a
252 * member of a structure. In this case it is safe to not update
253 * the max_array_access field because it is never used for fields
256 ir_variable
*v
= array
->whole_variable_referenced();
258 v
->data
.max_array_access
= array
->type
->array_size() - 1;
261 /* From page 23 (29 of the PDF) of the GLSL 1.30 spec:
263 * "Samplers aggregated into arrays within a shader (using square
264 * brackets [ ]) can only be indexed with integral constant
265 * expressions [...]."
267 * This restriction was added in GLSL 1.30. Shaders using earlier
268 * version of the language should not be rejected by the compiler
269 * front-end for using this construct. This allows useful things such
270 * as using a loop counter as the index to an array of samplers. If the
271 * loop in unrolled, the code should compile correctly. Instead, emit a
274 * In GLSL 4.00 / ARB_gpu_shader5, this requirement is relaxed again to allow
275 * indexing with dynamically uniform expressions. Note that these are not
276 * required to be uniforms or expressions based on them, but merely that the
277 * values must not diverge between shader invocations run together. If the
278 * values *do* diverge, then the behavior of the operation requiring a
279 * dynamically uniform expression is undefined.
281 if (array
->type
->without_array()->is_sampler()) {
282 if (!state
->is_version(400, 0) && !state
->ARB_gpu_shader5_enable
) {
283 if (state
->is_version(130, 300))
284 _mesa_glsl_error(&loc
, state
,
285 "sampler arrays indexed with non-constant "
286 "expressions are forbidden in GLSL %s "
288 state
->es_shader
? "ES 3.00" : "1.30");
289 else if (state
->es_shader
)
290 _mesa_glsl_warning(&loc
, state
,
291 "sampler arrays indexed with non-constant "
292 "expressions will be forbidden in GLSL "
295 _mesa_glsl_warning(&loc
, state
,
296 "sampler arrays indexed with non-constant "
297 "expressions will be forbidden in GLSL "
302 /* From page 27 of the GLSL ES 3.1 specification:
304 * "When aggregated into arrays within a shader, images can only be
305 * indexed with a constant integral expression."
307 * On the other hand the desktop GL specification extension allows
308 * non-constant indexing of image arrays, but behavior is left undefined
309 * in cases where the indexing expression is not dynamically uniform.
311 if (state
->es_shader
&& array
->type
->without_array()->is_image()) {
312 _mesa_glsl_error(&loc
, state
,
313 "image arrays indexed with non-constant "
314 "expressions are forbidden in GLSL ES.");
318 /* After performing all of the error checking, generate the IR for the
321 if (array
->type
->is_array()
322 || array
->type
->is_matrix()) {
323 return new(mem_ctx
) ir_dereference_array(array
, idx
);
324 } else if (array
->type
->is_vector()) {
325 return new(mem_ctx
) ir_expression(ir_binop_vector_extract
, array
, idx
);
326 } else if (array
->type
->is_error()) {
329 ir_rvalue
*result
= new(mem_ctx
) ir_dereference_array(array
, idx
);
330 result
->type
= glsl_type::error_type
;