Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
5564 | serge | 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 "glsl_types.h" |
||
26 | #include "ir.h" |
||
27 | |||
28 | void |
||
29 | ast_array_specifier::print(void) const |
||
30 | { |
||
31 | if (this->is_unsized_array) { |
||
32 | printf("[ ] "); |
||
33 | } |
||
34 | |||
35 | foreach_list_typed (ast_node, array_dimension, link, &this->array_dimensions) { |
||
36 | printf("[ "); |
||
37 | array_dimension->print(); |
||
38 | printf("] "); |
||
39 | } |
||
40 | } |
||
41 | |||
42 | /** |
||
43 | * If \c ir is a reference to an array for which we are tracking the max array |
||
44 | * element accessed, track that the given element has been accessed. |
||
45 | * Otherwise do nothing. |
||
46 | * |
||
47 | * This function also checks whether the array is a built-in array whose |
||
48 | * maximum size is too small to accommodate the given index, and if so uses |
||
49 | * loc and state to report the error. |
||
50 | */ |
||
51 | static void |
||
52 | update_max_array_access(ir_rvalue *ir, int idx, YYLTYPE *loc, |
||
53 | struct _mesa_glsl_parse_state *state) |
||
54 | { |
||
55 | if (ir_dereference_variable *deref_var = ir->as_dereference_variable()) { |
||
56 | ir_variable *var = deref_var->var; |
||
57 | if (idx > (int)var->data.max_array_access) { |
||
58 | var->data.max_array_access = idx; |
||
59 | |||
60 | /* Check whether this access will, as a side effect, implicitly cause |
||
61 | * the size of a built-in array to be too large. |
||
62 | */ |
||
63 | check_builtin_array_max_size(var->name, idx+1, *loc, state); |
||
64 | } |
||
65 | } else if (ir_dereference_record *deref_record = |
||
66 | ir->as_dereference_record()) { |
||
67 | /* There are two possibilities we need to consider: |
||
68 | * |
||
69 | * - Accessing an element of an array that is a member of a named |
||
70 | * interface block (e.g. ifc.foo[i]) |
||
71 | * |
||
72 | * - Accessing an element of an array that is a member of a named |
||
73 | * interface block array (e.g. ifc[j].foo[i]). |
||
74 | */ |
||
75 | ir_dereference_variable *deref_var = |
||
76 | deref_record->record->as_dereference_variable(); |
||
77 | if (deref_var == NULL) { |
||
78 | if (ir_dereference_array *deref_array = |
||
79 | deref_record->record->as_dereference_array()) { |
||
80 | deref_var = deref_array->array->as_dereference_variable(); |
||
81 | } |
||
82 | } |
||
83 | |||
84 | if (deref_var != NULL) { |
||
85 | if (deref_var->var->is_interface_instance()) { |
||
86 | unsigned field_index = |
||
87 | deref_record->record->type->field_index(deref_record->field); |
||
88 | assert(field_index < deref_var->var->get_interface_type()->length); |
||
89 | |||
90 | unsigned *const max_ifc_array_access = |
||
91 | deref_var->var->get_max_ifc_array_access(); |
||
92 | |||
93 | assert(max_ifc_array_access != NULL); |
||
94 | |||
95 | if (idx > (int)max_ifc_array_access[field_index]) { |
||
96 | max_ifc_array_access[field_index] = idx; |
||
97 | |||
98 | /* Check whether this access will, as a side effect, implicitly |
||
99 | * cause the size of a built-in array to be too large. |
||
100 | */ |
||
101 | check_builtin_array_max_size(deref_record->field, idx+1, *loc, |
||
102 | state); |
||
103 | } |
||
104 | } |
||
105 | } |
||
106 | } |
||
107 | } |
||
108 | |||
109 | |||
110 | ir_rvalue * |
||
111 | _mesa_ast_array_index_to_hir(void *mem_ctx, |
||
112 | struct _mesa_glsl_parse_state *state, |
||
113 | ir_rvalue *array, ir_rvalue *idx, |
||
114 | YYLTYPE &loc, YYLTYPE &idx_loc) |
||
115 | { |
||
116 | if (!array->type->is_error() |
||
117 | && !array->type->is_array() |
||
118 | && !array->type->is_matrix() |
||
119 | && !array->type->is_vector()) { |
||
120 | _mesa_glsl_error(& idx_loc, state, |
||
121 | "cannot dereference non-array / non-matrix / " |
||
122 | "non-vector"); |
||
123 | } |
||
124 | |||
125 | if (!idx->type->is_error()) { |
||
126 | if (!idx->type->is_integer()) { |
||
127 | _mesa_glsl_error(& idx_loc, state, "array index must be integer type"); |
||
128 | } else if (!idx->type->is_scalar()) { |
||
129 | _mesa_glsl_error(& idx_loc, state, "array index must be scalar"); |
||
130 | } |
||
131 | } |
||
132 | |||
133 | /* If the array index is a constant expression and the array has a |
||
134 | * declared size, ensure that the access is in-bounds. If the array |
||
135 | * index is not a constant expression, ensure that the array has a |
||
136 | * declared size. |
||
137 | */ |
||
138 | ir_constant *const const_index = idx->constant_expression_value(); |
||
139 | if (const_index != NULL && idx->type->is_integer()) { |
||
140 | const int idx = const_index->value.i[0]; |
||
141 | const char *type_name = "error"; |
||
142 | unsigned bound = 0; |
||
143 | |||
144 | /* From page 24 (page 30 of the PDF) of the GLSL 1.50 spec: |
||
145 | * |
||
146 | * "It is illegal to declare an array with a size, and then |
||
147 | * later (in the same shader) index the same array with an |
||
148 | * integral constant expression greater than or equal to the |
||
149 | * declared size. It is also illegal to index an array with a |
||
150 | * negative constant expression." |
||
151 | */ |
||
152 | if (array->type->is_matrix()) { |
||
153 | if (array->type->row_type()->vector_elements <= idx) { |
||
154 | type_name = "matrix"; |
||
155 | bound = array->type->row_type()->vector_elements; |
||
156 | } |
||
157 | } else if (array->type->is_vector()) { |
||
158 | if (array->type->vector_elements <= idx) { |
||
159 | type_name = "vector"; |
||
160 | bound = array->type->vector_elements; |
||
161 | } |
||
162 | } else { |
||
163 | /* glsl_type::array_size() returns -1 for non-array types. This means |
||
164 | * that we don't need to verify that the type is an array before |
||
165 | * doing the bounds checking. |
||
166 | */ |
||
167 | if ((array->type->array_size() > 0) |
||
168 | && (array->type->array_size() <= idx)) { |
||
169 | type_name = "array"; |
||
170 | bound = array->type->array_size(); |
||
171 | } |
||
172 | } |
||
173 | |||
174 | if (bound > 0) { |
||
175 | _mesa_glsl_error(& loc, state, "%s index must be < %u", |
||
176 | type_name, bound); |
||
177 | } else if (idx < 0) { |
||
178 | _mesa_glsl_error(& loc, state, "%s index must be >= 0", |
||
179 | type_name); |
||
180 | } |
||
181 | |||
182 | if (array->type->is_array()) |
||
183 | update_max_array_access(array, idx, &loc, state); |
||
184 | } else if (const_index == NULL && array->type->is_array()) { |
||
185 | if (array->type->is_unsized_array()) { |
||
186 | _mesa_glsl_error(&loc, state, "unsized array index must be constant"); |
||
187 | } else if (array->type->fields.array->is_interface() |
||
188 | && array->variable_referenced()->data.mode == ir_var_uniform |
||
189 | && !state->is_version(400, 0) && !state->ARB_gpu_shader5_enable) { |
||
190 | /* Page 46 in section 4.3.7 of the OpenGL ES 3.00 spec says: |
||
191 | * |
||
192 | * "All indexes used to index a uniform block array must be |
||
193 | * constant integral expressions." |
||
194 | */ |
||
195 | _mesa_glsl_error(&loc, state, |
||
196 | "uniform block array index must be constant"); |
||
197 | } else { |
||
198 | /* whole_variable_referenced can return NULL if the array is a |
||
199 | * member of a structure. In this case it is safe to not update |
||
200 | * the max_array_access field because it is never used for fields |
||
201 | * of structures. |
||
202 | */ |
||
203 | ir_variable *v = array->whole_variable_referenced(); |
||
204 | if (v != NULL) |
||
205 | v->data.max_array_access = array->type->array_size() - 1; |
||
206 | } |
||
207 | |||
208 | /* From page 23 (29 of the PDF) of the GLSL 1.30 spec: |
||
209 | * |
||
210 | * "Samplers aggregated into arrays within a shader (using square |
||
211 | * brackets [ ]) can only be indexed with integral constant |
||
212 | * expressions [...]." |
||
213 | * |
||
214 | * This restriction was added in GLSL 1.30. Shaders using earlier |
||
215 | * version of the language should not be rejected by the compiler |
||
216 | * front-end for using this construct. This allows useful things such |
||
217 | * as using a loop counter as the index to an array of samplers. If the |
||
218 | * loop in unrolled, the code should compile correctly. Instead, emit a |
||
219 | * warning. |
||
220 | * |
||
221 | * In GLSL 4.00 / ARB_gpu_shader5, this requirement is relaxed again to allow |
||
222 | * indexing with dynamically uniform expressions. Note that these are not |
||
223 | * required to be uniforms or expressions based on them, but merely that the |
||
224 | * values must not diverge between shader invocations run together. If the |
||
225 | * values *do* diverge, then the behavior of the operation requiring a |
||
226 | * dynamically uniform expression is undefined. |
||
227 | */ |
||
228 | if (array->type->element_type()->is_sampler()) { |
||
229 | if (!state->is_version(130, 100)) { |
||
230 | if (state->es_shader) { |
||
231 | _mesa_glsl_warning(&loc, state, |
||
232 | "sampler arrays indexed with non-constant " |
||
233 | "expressions is optional in %s", |
||
234 | state->get_version_string()); |
||
235 | } else { |
||
236 | _mesa_glsl_warning(&loc, state, |
||
237 | "sampler arrays indexed with non-constant " |
||
238 | "expressions will be forbidden in GLSL 1.30 " |
||
239 | "and later"); |
||
240 | } |
||
241 | } else if (!state->is_version(400, 0) && !state->ARB_gpu_shader5_enable) { |
||
242 | _mesa_glsl_error(&loc, state, |
||
243 | "sampler arrays indexed with non-constant " |
||
244 | "expressions is forbidden in GLSL 1.30 and " |
||
245 | "later"); |
||
246 | } |
||
247 | } |
||
248 | } |
||
249 | |||
250 | /* After performing all of the error checking, generate the IR for the |
||
251 | * expression. |
||
252 | */ |
||
253 | if (array->type->is_array() |
||
254 | || array->type->is_matrix()) { |
||
255 | return new(mem_ctx) ir_dereference_array(array, idx); |
||
256 | } else if (array->type->is_vector()) { |
||
257 | return new(mem_ctx) ir_expression(ir_binop_vector_extract, array, idx); |
||
258 | } else if (array->type->is_error()) { |
||
259 | return array; |
||
260 | } else { |
||
261 | ir_rvalue *result = new(mem_ctx) ir_dereference_array(array, idx); |
||
262 | result->type = glsl_type::error_type; |
||
263 | |||
264 | return result; |
||
265 | } |
||
266 | }>>=>=>=>> |