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 "compiler/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_idx
= deref_record
->field_idx
;
92 assert(field_idx
< deref_var
->var
->get_interface_type()->length
);
94 int *const max_ifc_array_access
=
95 deref_var
->var
->get_max_ifc_array_access();
97 assert(max_ifc_array_access
!= NULL
);
99 if (idx
> max_ifc_array_access
[field_idx
]) {
100 max_ifc_array_access
[field_idx
] = idx
;
102 /* Check whether this access will, as a side effect, implicitly
103 * cause the size of a built-in array to be too large.
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
);
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_32()) {
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(mem_ctx
);
171 if (const_index
!= NULL
&& idx
->type
->is_integer_32()) {
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", type_name
);
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
);
219 ir_variable
*v
= array
->whole_variable_referenced();
221 v
->data
.max_array_access
= implicit_size
- 1;
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
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");
237 /* Unsized array non-constant indexing on SSBO is allowed only for
238 * the last member of the SSBO definition.
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 "
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:
262 * "All indices used to index a uniform or shader storage block
263 * array must be constant integral expressions."
265 * But OES_gpu_shader5 (and ESSL 3.20) relax this to allow indexing
266 * on uniform blocks but not shader storage blocks.
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");
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
278 ir_variable
*v
= array
->whole_variable_referenced();
280 v
->data
.max_array_access
= array
->type
->array_size() - 1;
283 /* From page 23 (29 of the PDF) of the GLSL 1.30 spec:
285 * "Samplers aggregated into arrays within a shader (using square
286 * brackets [ ]) can only be indexed with integral constant
287 * expressions [...]."
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
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.
303 * From section 4.1.7 of the ARB_bindless_texture spec:
305 * "Samplers aggregated into arrays within a shader (using square
306 * brackets []) can be indexed with arbitrary integer expressions."
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 "
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 "
326 _mesa_glsl_warning(&loc
, state
,
327 "sampler arrays indexed with non-constant "
328 "expressions will be forbidden in GLSL "
333 /* From page 27 of the GLSL ES 3.1 specification:
335 * "When aggregated into arrays within a shader, images can only be
336 * indexed with a constant integral expression."
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.
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.");
349 /* After performing all of the error checking, generate the IR for the
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()) {
359 ir_rvalue
*result
= new(mem_ctx
) ir_dereference_array(array
, idx
);
360 result
->type
= glsl_type::error_type
;