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 two 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 ir_dereference_variable
*deref_var
=
73 deref_record
->record
->as_dereference_variable();
74 if (deref_var
== NULL
) {
75 if (ir_dereference_array
*deref_array
=
76 deref_record
->record
->as_dereference_array()) {
77 deref_var
= deref_array
->array
->as_dereference_variable();
81 if (deref_var
!= NULL
) {
82 if (deref_var
->var
->is_interface_instance()) {
83 unsigned field_index
=
84 deref_record
->record
->type
->field_index(deref_record
->field
);
85 assert(field_index
< deref_var
->var
->get_interface_type()->length
);
87 unsigned *const max_ifc_array_access
=
88 deref_var
->var
->get_max_ifc_array_access();
90 assert(max_ifc_array_access
!= NULL
);
92 if (idx
> (int)max_ifc_array_access
[field_index
]) {
93 max_ifc_array_access
[field_index
] = idx
;
95 /* Check whether this access will, as a side effect, implicitly
96 * cause the size of a built-in array to be too large.
98 check_builtin_array_max_size(deref_record
->field
, idx
+1, *loc
,
108 get_implicit_array_size(struct _mesa_glsl_parse_state
*state
,
111 ir_variable
*var
= array
->variable_referenced();
113 /* Inputs in control shader are implicitly sized
114 * to the maximum patch size.
116 if (state
->stage
== MESA_SHADER_TESS_CTRL
&&
117 var
->data
.mode
== ir_var_shader_in
) {
118 return state
->Const
.MaxPatchVertices
;
121 /* Non-patch inputs in evaluation shader are implicitly sized
122 * to the maximum patch size.
124 if (state
->stage
== MESA_SHADER_TESS_EVAL
&&
125 var
->data
.mode
== ir_var_shader_in
&&
127 return state
->Const
.MaxPatchVertices
;
135 _mesa_ast_array_index_to_hir(void *mem_ctx
,
136 struct _mesa_glsl_parse_state
*state
,
137 ir_rvalue
*array
, ir_rvalue
*idx
,
138 YYLTYPE
&loc
, YYLTYPE
&idx_loc
)
140 if (!array
->type
->is_error()
141 && !array
->type
->is_array()
142 && !array
->type
->is_matrix()
143 && !array
->type
->is_vector()) {
144 _mesa_glsl_error(& idx_loc
, state
,
145 "cannot dereference non-array / non-matrix / "
149 if (!idx
->type
->is_error()) {
150 if (!idx
->type
->is_integer()) {
151 _mesa_glsl_error(& idx_loc
, state
, "array index must be integer type");
152 } else if (!idx
->type
->is_scalar()) {
153 _mesa_glsl_error(& idx_loc
, state
, "array index must be scalar");
157 /* If the array index is a constant expression and the array has a
158 * declared size, ensure that the access is in-bounds. If the array
159 * index is not a constant expression, ensure that the array has a
162 ir_constant
*const const_index
= idx
->constant_expression_value();
163 if (const_index
!= NULL
&& idx
->type
->is_integer()) {
164 const int idx
= const_index
->value
.i
[0];
165 const char *type_name
= "error";
168 /* From page 24 (page 30 of the PDF) of the GLSL 1.50 spec:
170 * "It is illegal to declare an array with a size, and then
171 * later (in the same shader) index the same array with an
172 * integral constant expression greater than or equal to the
173 * declared size. It is also illegal to index an array with a
174 * negative constant expression."
176 if (array
->type
->is_matrix()) {
177 if (array
->type
->row_type()->vector_elements
<= idx
) {
178 type_name
= "matrix";
179 bound
= array
->type
->row_type()->vector_elements
;
181 } else if (array
->type
->is_vector()) {
182 if (array
->type
->vector_elements
<= idx
) {
183 type_name
= "vector";
184 bound
= array
->type
->vector_elements
;
187 /* glsl_type::array_size() returns -1 for non-array types. This means
188 * that we don't need to verify that the type is an array before
189 * doing the bounds checking.
191 if ((array
->type
->array_size() > 0)
192 && (array
->type
->array_size() <= idx
)) {
194 bound
= array
->type
->array_size();
199 _mesa_glsl_error(& loc
, state
, "%s index must be < %u",
201 } else if (idx
< 0) {
202 _mesa_glsl_error(& loc
, state
, "%s index must be >= 0",
206 if (array
->type
->is_array())
207 update_max_array_access(array
, idx
, &loc
, state
);
208 } else if (const_index
== NULL
&& array
->type
->is_array()) {
209 if (array
->type
->is_unsized_array()) {
210 int implicit_size
= get_implicit_array_size(state
, array
);
212 ir_variable
*v
= array
->whole_variable_referenced();
214 v
->data
.max_array_access
= implicit_size
- 1;
216 else if (state
->stage
== MESA_SHADER_TESS_CTRL
&&
217 array
->variable_referenced()->data
.mode
== ir_var_shader_out
&&
218 !array
->variable_referenced()->data
.patch
) {
219 /* Tessellation control shader output non-patch arrays are
220 * initially unsized. Despite that, they are allowed to be
221 * indexed with a non-constant expression (typically
222 * "gl_InvocationID"). The array size will be determined
226 else if (array
->variable_referenced()->data
.mode
!=
227 ir_var_shader_storage
) {
228 _mesa_glsl_error(&loc
, state
, "unsized array index must be constant");
230 } else if (array
->type
->fields
.array
->is_interface()
231 && (array
->variable_referenced()->data
.mode
== ir_var_uniform
||
232 array
->variable_referenced()->data
.mode
== ir_var_shader_storage
)
233 && !state
->is_version(400, 0) && !state
->ARB_gpu_shader5_enable
) {
234 /* Page 50 in section 4.3.9 of the OpenGL ES 3.10 spec says:
236 * "All indices used to index a uniform or shader storage block
237 * array must be constant integral expressions."
239 _mesa_glsl_error(&loc
, state
, "%s block array index must be constant",
240 array
->variable_referenced()->data
.mode
241 == ir_var_uniform
? "uniform" : "shader storage");
243 /* whole_variable_referenced can return NULL if the array is a
244 * member of a structure. In this case it is safe to not update
245 * the max_array_access field because it is never used for fields
248 ir_variable
*v
= array
->whole_variable_referenced();
250 v
->data
.max_array_access
= array
->type
->array_size() - 1;
253 /* From page 23 (29 of the PDF) of the GLSL 1.30 spec:
255 * "Samplers aggregated into arrays within a shader (using square
256 * brackets [ ]) can only be indexed with integral constant
257 * expressions [...]."
259 * This restriction was added in GLSL 1.30. Shaders using earlier
260 * version of the language should not be rejected by the compiler
261 * front-end for using this construct. This allows useful things such
262 * as using a loop counter as the index to an array of samplers. If the
263 * loop in unrolled, the code should compile correctly. Instead, emit a
266 * In GLSL 4.00 / ARB_gpu_shader5, this requirement is relaxed again to allow
267 * indexing with dynamically uniform expressions. Note that these are not
268 * required to be uniforms or expressions based on them, but merely that the
269 * values must not diverge between shader invocations run together. If the
270 * values *do* diverge, then the behavior of the operation requiring a
271 * dynamically uniform expression is undefined.
273 if (array
->type
->without_array()->is_sampler()) {
274 if (!state
->is_version(400, 0) && !state
->ARB_gpu_shader5_enable
) {
275 if (state
->is_version(130, 300))
276 _mesa_glsl_error(&loc
, state
,
277 "sampler arrays indexed with non-constant "
278 "expressions are forbidden in GLSL %s "
280 state
->es_shader
? "ES 3.00" : "1.30");
281 else if (state
->es_shader
)
282 _mesa_glsl_warning(&loc
, state
,
283 "sampler arrays indexed with non-constant "
284 "expressions will be forbidden in GLSL "
287 _mesa_glsl_warning(&loc
, state
,
288 "sampler arrays indexed with non-constant "
289 "expressions will be forbidden in GLSL "
294 /* From page 27 of the GLSL ES 3.1 specification:
296 * "When aggregated into arrays within a shader, images can only be
297 * indexed with a constant integral expression."
299 * On the other hand the desktop GL specification extension allows
300 * non-constant indexing of image arrays, but behavior is left undefined
301 * in cases where the indexing expression is not dynamically uniform.
303 if (state
->es_shader
&& array
->type
->without_array()->is_image()) {
304 _mesa_glsl_error(&loc
, state
,
305 "image arrays indexed with non-constant "
306 "expressions are forbidden in GLSL ES.");
310 /* After performing all of the error checking, generate the IR for the
313 if (array
->type
->is_array()
314 || array
->type
->is_matrix()) {
315 return new(mem_ctx
) ir_dereference_array(array
, idx
);
316 } else if (array
->type
->is_vector()) {
317 return new(mem_ctx
) ir_expression(ir_binop_vector_extract
, array
, idx
);
318 } else if (array
->type
->is_error()) {
321 ir_rvalue
*result
= new(mem_ctx
) ir_dereference_array(array
, idx
);
322 result
->type
= glsl_type::error_type
;