glsl: Fix incorrect hard-coded location of the gl_SecondaryFragColorEXT built-in.
[mesa.git] / src / compiler / glsl / ast_array_index.cpp
1 /*
2 * Copyright © 2010 Intel Corporation
3 *
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:
10 *
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
13 * Software.
14 *
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.
22 */
23
24 #include "ast.h"
25 #include "compiler/glsl_types.h"
26 #include "ir.h"
27
28 void
29 ast_array_specifier::print(void) const
30 {
31 foreach_list_typed (ast_node, array_dimension, link, &this->array_dimensions) {
32 printf("[ ");
33 if (((ast_expression*)array_dimension)->oper != ast_unsized_array_dim)
34 array_dimension->print();
35 printf("] ");
36 }
37 }
38
39 /**
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.
43 *
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.
47 */
48 static void
49 update_max_array_access(ir_rvalue *ir, int idx, YYLTYPE *loc,
50 struct _mesa_glsl_parse_state *state)
51 {
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;
56
57 /* Check whether this access will, as a side effect, implicitly cause
58 * the size of a built-in array to be too large.
59 */
60 check_builtin_array_max_size(var->name, idx+1, *loc, state);
61 }
62 } else if (ir_dereference_record *deref_record =
63 ir->as_dereference_record()) {
64 /* There are three possibilities we need to consider:
65 *
66 * - Accessing an element of an array that is a member of a named
67 * interface block (e.g. ifc.foo[i])
68 *
69 * - Accessing an element of an array that is a member of a named
70 * interface block array (e.g. ifc[j].foo[i]).
71 *
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]).
74 */
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();
84 }
85 if (deref_array_prev != NULL)
86 deref_var = deref_array_prev->array->as_dereference_variable();
87 }
88
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);
94
95 int *const max_ifc_array_access =
96 deref_var->var->get_max_ifc_array_access();
97
98 assert(max_ifc_array_access != NULL);
99
100 if (idx > max_ifc_array_access[field_index]) {
101 max_ifc_array_access[field_index] = idx;
102
103 /* Check whether this access will, as a side effect, implicitly
104 * cause the size of a built-in array to be too large.
105 */
106 check_builtin_array_max_size(deref_record->field, idx+1, *loc,
107 state);
108 }
109 }
110 }
111 }
112 }
113
114
115 static int
116 get_implicit_array_size(struct _mesa_glsl_parse_state *state,
117 ir_rvalue *array)
118 {
119 ir_variable *var = array->variable_referenced();
120
121 /* Inputs in control shader are implicitly sized
122 * to the maximum patch size.
123 */
124 if (state->stage == MESA_SHADER_TESS_CTRL &&
125 var->data.mode == ir_var_shader_in) {
126 return state->Const.MaxPatchVertices;
127 }
128
129 /* Non-patch inputs in evaluation shader are implicitly sized
130 * to the maximum patch size.
131 */
132 if (state->stage == MESA_SHADER_TESS_EVAL &&
133 var->data.mode == ir_var_shader_in &&
134 !var->data.patch) {
135 return state->Const.MaxPatchVertices;
136 }
137
138 return 0;
139 }
140
141
142 ir_rvalue *
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)
147 {
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 / "
154 "non-vector");
155 }
156
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");
162 }
163 }
164
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
168 * declared size.
169 */
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";
174 unsigned bound = 0;
175
176 /* From page 24 (page 30 of the PDF) of the GLSL 1.50 spec:
177 *
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."
183 */
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;
188 }
189 } else if (array->type->is_vector()) {
190 if (array->type->vector_elements <= idx) {
191 type_name = "vector";
192 bound = array->type->vector_elements;
193 }
194 } else {
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.
198 */
199 if ((array->type->array_size() > 0)
200 && (array->type->array_size() <= idx)) {
201 type_name = "array";
202 bound = array->type->array_size();
203 }
204 }
205
206 if (bound > 0) {
207 _mesa_glsl_error(& loc, state, "%s index must be < %u",
208 type_name, bound);
209 } else if (idx < 0) {
210 _mesa_glsl_error(& loc, state, "%s index must be >= 0",
211 type_name);
212 }
213
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);
219 if (implicit_size) {
220 ir_variable *v = array->whole_variable_referenced();
221 if (v != NULL)
222 v->data.max_array_access = implicit_size - 1;
223 }
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
231 * by the linker.
232 */
233 }
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");
237 }
238 } else if (array->type->without_array()->is_interface()
239 && ((array->variable_referenced()->data.mode == ir_var_uniform
240 && !state->is_version(400, 320)
241 && !state->ARB_gpu_shader5_enable
242 && !state->EXT_gpu_shader5_enable
243 && !state->OES_gpu_shader5_enable) ||
244 (array->variable_referenced()->data.mode == ir_var_shader_storage
245 && !state->is_version(400, 0)
246 && !state->ARB_gpu_shader5_enable))) {
247 /* Page 50 in section 4.3.9 of the OpenGL ES 3.10 spec says:
248 *
249 * "All indices used to index a uniform or shader storage block
250 * array must be constant integral expressions."
251 *
252 * But OES_gpu_shader5 (and ESSL 3.20) relax this to allow indexing
253 * on uniform blocks but not shader storage blocks.
254 *
255 */
256 _mesa_glsl_error(&loc, state, "%s block array index must be constant",
257 array->variable_referenced()->data.mode
258 == ir_var_uniform ? "uniform" : "shader storage");
259 } else {
260 /* whole_variable_referenced can return NULL if the array is a
261 * member of a structure. In this case it is safe to not update
262 * the max_array_access field because it is never used for fields
263 * of structures.
264 */
265 ir_variable *v = array->whole_variable_referenced();
266 if (v != NULL)
267 v->data.max_array_access = array->type->array_size() - 1;
268 }
269
270 /* From page 23 (29 of the PDF) of the GLSL 1.30 spec:
271 *
272 * "Samplers aggregated into arrays within a shader (using square
273 * brackets [ ]) can only be indexed with integral constant
274 * expressions [...]."
275 *
276 * This restriction was added in GLSL 1.30. Shaders using earlier
277 * version of the language should not be rejected by the compiler
278 * front-end for using this construct. This allows useful things such
279 * as using a loop counter as the index to an array of samplers. If the
280 * loop in unrolled, the code should compile correctly. Instead, emit a
281 * warning.
282 *
283 * In GLSL 4.00 / ARB_gpu_shader5, this requirement is relaxed again to allow
284 * indexing with dynamically uniform expressions. Note that these are not
285 * required to be uniforms or expressions based on them, but merely that the
286 * values must not diverge between shader invocations run together. If the
287 * values *do* diverge, then the behavior of the operation requiring a
288 * dynamically uniform expression is undefined.
289 */
290 if (array->type->without_array()->is_sampler()) {
291 if (!state->is_version(400, 320) &&
292 !state->ARB_gpu_shader5_enable &&
293 !state->EXT_gpu_shader5_enable &&
294 !state->OES_gpu_shader5_enable) {
295 if (state->is_version(130, 300))
296 _mesa_glsl_error(&loc, state,
297 "sampler arrays indexed with non-constant "
298 "expressions are forbidden in GLSL %s "
299 "and later",
300 state->es_shader ? "ES 3.00" : "1.30");
301 else if (state->es_shader)
302 _mesa_glsl_warning(&loc, state,
303 "sampler arrays indexed with non-constant "
304 "expressions will be forbidden in GLSL "
305 "3.00 and later");
306 else
307 _mesa_glsl_warning(&loc, state,
308 "sampler arrays indexed with non-constant "
309 "expressions will be forbidden in GLSL "
310 "1.30 and later");
311 }
312 }
313
314 /* From page 27 of the GLSL ES 3.1 specification:
315 *
316 * "When aggregated into arrays within a shader, images can only be
317 * indexed with a constant integral expression."
318 *
319 * On the other hand the desktop GL specification extension allows
320 * non-constant indexing of image arrays, but behavior is left undefined
321 * in cases where the indexing expression is not dynamically uniform.
322 */
323 if (state->es_shader && array->type->without_array()->is_image()) {
324 _mesa_glsl_error(&loc, state,
325 "image arrays indexed with non-constant "
326 "expressions are forbidden in GLSL ES.");
327 }
328 }
329
330 /* After performing all of the error checking, generate the IR for the
331 * expression.
332 */
333 if (array->type->is_array()
334 || array->type->is_matrix()
335 || array->type->is_vector()) {
336 return new(mem_ctx) ir_dereference_array(array, idx);
337 } else if (array->type->is_error()) {
338 return array;
339 } else {
340 ir_rvalue *result = new(mem_ctx) ir_dereference_array(array, idx);
341 result->type = glsl_type::error_type;
342
343 return result;
344 }
345 }