OSDN Git Service

glsl: Refactor handling of ast_array_index to a separate function
[android-x86/external-mesa.git] / src / 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 "glsl_types.h"
26 #include "ir.h"
27
28 ir_rvalue *
29 _mesa_ast_array_index_to_hir(void *mem_ctx,
30                              struct _mesa_glsl_parse_state *state,
31                              ir_rvalue *array, ir_rvalue *idx,
32                              YYLTYPE &loc, YYLTYPE &idx_loc,
33                              bool error_emitted)
34 {
35    ir_rvalue *result = new(mem_ctx) ir_dereference_array(array, idx);
36
37    if (error_emitted)
38       return result;
39
40    if (!array->type->is_array()
41        && !array->type->is_matrix()
42        && !array->type->is_vector()) {
43       _mesa_glsl_error(& idx_loc, state,
44                        "cannot dereference non-array / non-matrix / "
45                        "non-vector");
46       error_emitted = true;
47    }
48
49    if (!idx->type->is_integer()) {
50       _mesa_glsl_error(& idx_loc, state,
51                        "array index must be integer type");
52       error_emitted = true;
53    } else if (!idx->type->is_scalar()) {
54       _mesa_glsl_error(& idx_loc, state,
55                        "array index must be scalar");
56       error_emitted = true;
57    }
58
59    /* If the array index is a constant expression and the array has a
60     * declared size, ensure that the access is in-bounds.  If the array
61     * index is not a constant expression, ensure that the array has a
62     * declared size.
63     */
64    ir_constant *const const_index = idx->constant_expression_value();
65    if (const_index != NULL) {
66       const int idx = const_index->value.i[0];
67       const char *type_name;
68       unsigned bound = 0;
69
70       if (array->type->is_matrix()) {
71          type_name = "matrix";
72       } else if (array->type->is_vector()) {
73          type_name = "vector";
74       } else {
75          type_name = "array";
76       }
77
78       /* From page 24 (page 30 of the PDF) of the GLSL 1.50 spec:
79        *
80        *    "It is illegal to declare an array with a size, and then
81        *    later (in the same shader) index the same array with an
82        *    integral constant expression greater than or equal to the
83        *    declared size. It is also illegal to index an array with a
84        *    negative constant expression."
85        */
86       if (array->type->is_matrix()) {
87          if (array->type->row_type()->vector_elements <= idx) {
88             bound = array->type->row_type()->vector_elements;
89          }
90       } else if (array->type->is_vector()) {
91          if (array->type->vector_elements <= idx) {
92             bound = array->type->vector_elements;
93          }
94       } else {
95          if ((array->type->array_size() > 0)
96              && (array->type->array_size() <= idx)) {
97             bound = array->type->array_size();
98          }
99       }
100
101       if (bound > 0) {
102          _mesa_glsl_error(& loc, state, "%s index must be < %u",
103                           type_name, bound);
104          error_emitted = true;
105       } else if (idx < 0) {
106          _mesa_glsl_error(& loc, state, "%s index must be >= 0",
107                           type_name);
108          error_emitted = true;
109       }
110
111       if (array->type->is_array()) {
112          /* If the array is a variable dereference, it dereferences the
113           * whole array, by definition.  Use this to get the variable.
114           *
115           * FINISHME: Should some methods for getting / setting / testing
116           * FINISHME: array access limits be added to ir_dereference?
117           */
118          ir_variable *const v = array->whole_variable_referenced();
119          if ((v != NULL) && (unsigned(idx) > v->max_array_access)) {
120             v->max_array_access = idx;
121
122             /* Check whether this access will, as a side effect, implicitly
123              * cause the size of a built-in array to be too large.
124              */
125             if (check_builtin_array_max_size(v->name, idx+1, loc, state))
126                error_emitted = true;
127          }
128       }
129    } else if (array->type->array_size() == 0) {
130       _mesa_glsl_error(&loc, state, "unsized array index must be constant");
131    } else if (array->type->is_array()
132               && array->type->fields.array->is_interface()) {
133       /* Page 46 in section 4.3.7 of the OpenGL ES 3.00 spec says:
134        *
135        *     "All indexes used to index a uniform block array must be
136        *     constant integral expressions."
137        */
138       _mesa_glsl_error(&loc, state,
139                        "uniform block array index must be constant");
140    } else {
141       if (array->type->is_array()) {
142          /* whole_variable_referenced can return NULL if the array is a
143           * member of a structure.  In this case it is safe to not update
144           * the max_array_access field because it is never used for fields
145           * of structures.
146           */
147          ir_variable *v = array->whole_variable_referenced();
148          if (v != NULL)
149             v->max_array_access = array->type->array_size() - 1;
150       }
151    }
152
153    /* From page 23 (29 of the PDF) of the GLSL 1.30 spec:
154     *
155     *    "Samplers aggregated into arrays within a shader (using square
156     *    brackets [ ]) can only be indexed with integral constant
157     *    expressions [...]."
158     *
159     * This restriction was added in GLSL 1.30.  Shaders using earlier version
160     * of the language should not be rejected by the compiler front-end for
161     * using this construct.  This allows useful things such as using a loop
162     * counter as the index to an array of samplers.  If the loop in unrolled,
163     * the code should compile correctly.  Instead, emit a warning.
164     */
165    if (array->type->is_array() &&
166        array->type->element_type()->is_sampler() &&
167        const_index == NULL) {
168
169       if (!state->is_version(130, 100)) {
170          if (state->es_shader) {
171             _mesa_glsl_warning(&loc, state,
172                                "sampler arrays indexed with non-constant "
173                                "expressions is optional in %s",
174                                state->get_version_string());
175          } else {
176             _mesa_glsl_warning(&loc, state,
177                                "sampler arrays indexed with non-constant "
178                                "expressions will be forbidden in GLSL 1.30 and "
179                                "later");
180          }
181       } else {
182          _mesa_glsl_error(&loc, state,
183                           "sampler arrays indexed with non-constant "
184                           "expressions is forbidden in GLSL 1.30 and "
185                           "later");
186          error_emitted = true;
187       }
188    }
189
190    if (error_emitted)
191       result->type = glsl_type::error_type;
192
193    return result;
194 }