a5f23206acfa37cf9cba73df7bbddb86995e6a5d
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"
30 * If \c ir is a reference to an array for which we are tracking the max array
31 * element accessed, track that the given element has been accessed.
32 * Otherwise do nothing.
34 * This function also checks whether the array is a built-in array whose
35 * maximum size is too small to accommodate the given index, and if so uses
36 * loc and state to report the error.
39 update_max_array_access(ir_rvalue
*ir
, unsigned idx
, YYLTYPE
*loc
,
40 struct _mesa_glsl_parse_state
*state
)
42 if (ir_dereference_variable
*deref_var
= ir
->as_dereference_variable()) {
43 ir_variable
*var
= deref_var
->var
;
44 if (idx
> var
->data
.max_array_access
) {
45 var
->data
.max_array_access
= idx
;
47 /* Check whether this access will, as a side effect, implicitly cause
48 * the size of a built-in array to be too large.
50 check_builtin_array_max_size(var
->name
, idx
+1, *loc
, state
);
52 } else if (ir_dereference_record
*deref_record
=
53 ir
->as_dereference_record()) {
54 /* There are two possibilities we need to consider:
56 * - Accessing an element of an array that is a member of a named
57 * interface block (e.g. ifc.foo[i])
59 * - Accessing an element of an array that is a member of a named
60 * interface block array (e.g. ifc[j].foo[i]).
62 ir_dereference_variable
*deref_var
=
63 deref_record
->record
->as_dereference_variable();
64 if (deref_var
== NULL
) {
65 if (ir_dereference_array
*deref_array
=
66 deref_record
->record
->as_dereference_array()) {
67 deref_var
= deref_array
->array
->as_dereference_variable();
71 if (deref_var
!= NULL
) {
72 if (deref_var
->var
->is_interface_instance()) {
73 const glsl_type
*interface_type
=
74 deref_var
->var
->get_interface_type();
75 unsigned field_index
=
76 deref_record
->record
->type
->field_index(deref_record
->field
);
77 assert(field_index
< interface_type
->length
);
78 if (idx
> deref_var
->var
->max_ifc_array_access
[field_index
]) {
79 deref_var
->var
->max_ifc_array_access
[field_index
] = idx
;
81 /* Check whether this access will, as a side effect, implicitly
82 * cause the size of a built-in array to be too large.
84 check_builtin_array_max_size(deref_record
->field
, idx
+1, *loc
,
94 _mesa_ast_array_index_to_hir(void *mem_ctx
,
95 struct _mesa_glsl_parse_state
*state
,
96 ir_rvalue
*array
, ir_rvalue
*idx
,
97 YYLTYPE
&loc
, YYLTYPE
&idx_loc
)
99 if (!array
->type
->is_error()
100 && !array
->type
->is_array()
101 && !array
->type
->is_matrix()
102 && !array
->type
->is_vector()) {
103 _mesa_glsl_error(& idx_loc
, state
,
104 "cannot dereference non-array / non-matrix / "
108 if (!idx
->type
->is_error()) {
109 if (!idx
->type
->is_integer()) {
110 _mesa_glsl_error(& idx_loc
, state
, "array index must be integer type");
111 } else if (!idx
->type
->is_scalar()) {
112 _mesa_glsl_error(& idx_loc
, state
, "array index must be scalar");
116 /* If the array index is a constant expression and the array has a
117 * declared size, ensure that the access is in-bounds. If the array
118 * index is not a constant expression, ensure that the array has a
121 ir_constant
*const const_index
= idx
->constant_expression_value();
122 if (const_index
!= NULL
&& idx
->type
->is_integer()) {
123 const int idx
= const_index
->value
.i
[0];
124 const char *type_name
= "error";
127 /* From page 24 (page 30 of the PDF) of the GLSL 1.50 spec:
129 * "It is illegal to declare an array with a size, and then
130 * later (in the same shader) index the same array with an
131 * integral constant expression greater than or equal to the
132 * declared size. It is also illegal to index an array with a
133 * negative constant expression."
135 if (array
->type
->is_matrix()) {
136 if (array
->type
->row_type()->vector_elements
<= idx
) {
137 type_name
= "matrix";
138 bound
= array
->type
->row_type()->vector_elements
;
140 } else if (array
->type
->is_vector()) {
141 if (array
->type
->vector_elements
<= idx
) {
142 type_name
= "vector";
143 bound
= array
->type
->vector_elements
;
146 /* glsl_type::array_size() returns -1 for non-array types. This means
147 * that we don't need to verify that the type is an array before
148 * doing the bounds checking.
150 if ((array
->type
->array_size() > 0)
151 && (array
->type
->array_size() <= idx
)) {
153 bound
= array
->type
->array_size();
158 _mesa_glsl_error(& loc
, state
, "%s index must be < %u",
160 } else if (idx
< 0) {
161 _mesa_glsl_error(& loc
, state
, "%s index must be >= 0",
165 if (array
->type
->is_array())
166 update_max_array_access(array
, idx
, &loc
, state
);
167 } else if (const_index
== NULL
&& array
->type
->is_array()) {
168 if (array
->type
->is_unsized_array()) {
169 _mesa_glsl_error(&loc
, state
, "unsized array index must be constant");
170 } else if (array
->type
->fields
.array
->is_interface()
171 && array
->variable_referenced()->data
.mode
== ir_var_uniform
) {
172 /* Page 46 in section 4.3.7 of the OpenGL ES 3.00 spec says:
174 * "All indexes used to index a uniform block array must be
175 * constant integral expressions."
177 _mesa_glsl_error(&loc
, state
,
178 "uniform block array index must be constant");
180 /* whole_variable_referenced can return NULL if the array is a
181 * member of a structure. In this case it is safe to not update
182 * the max_array_access field because it is never used for fields
185 ir_variable
*v
= array
->whole_variable_referenced();
187 v
->data
.max_array_access
= array
->type
->array_size() - 1;
190 /* From page 23 (29 of the PDF) of the GLSL 1.30 spec:
192 * "Samplers aggregated into arrays within a shader (using square
193 * brackets [ ]) can only be indexed with integral constant
194 * expressions [...]."
196 * This restriction was added in GLSL 1.30. Shaders using earlier
197 * version of the language should not be rejected by the compiler
198 * front-end for using this construct. This allows useful things such
199 * as using a loop counter as the index to an array of samplers. If the
200 * loop in unrolled, the code should compile correctly. Instead, emit a
203 if (array
->type
->element_type()->is_sampler()) {
204 if (!state
->is_version(130, 100)) {
205 if (state
->es_shader
) {
206 _mesa_glsl_warning(&loc
, state
,
207 "sampler arrays indexed with non-constant "
208 "expressions is optional in %s",
209 state
->get_version_string());
211 _mesa_glsl_warning(&loc
, state
,
212 "sampler arrays indexed with non-constant "
213 "expressions will be forbidden in GLSL 1.30 "
217 _mesa_glsl_error(&loc
, state
,
218 "sampler arrays indexed with non-constant "
219 "expressions is forbidden in GLSL 1.30 and "
225 /* After performing all of the error checking, generate the IR for the
228 if (array
->type
->is_array()
229 || array
->type
->is_matrix()) {
230 return new(mem_ctx
) ir_dereference_array(array
, idx
);
231 } else if (array
->type
->is_vector()) {
232 return new(mem_ctx
) ir_expression(ir_binop_vector_extract
, array
, idx
);
233 } else if (array
->type
->is_error()) {
236 ir_rvalue
*result
= new(mem_ctx
) ir_dereference_array(array
, idx
);
237 result
->type
= glsl_type::error_type
;