Perform constant folding on array indices.
[mesa.git] / ir_variable.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 "glsl_parser_extras.h"
25 #include "glsl_symbol_table.h"
26 #include "ir.h"
27 #include "builtin_variables.h"
28
29 #ifndef Elements
30 #define Elements(x) (sizeof(x)/sizeof(*(x)))
31 #endif
32
33 static void
34 add_variable(const char *name, enum ir_variable_mode mode,
35 const glsl_type *type, exec_list *instructions,
36 glsl_symbol_table *symtab)
37 {
38 ir_variable *var = new ir_variable(type, name);
39
40 var->mode = mode;
41 if (var->mode != ir_var_out)
42 var->read_only = true;
43
44
45 /* Once the variable is created an initialized, add it to the symbol table
46 * and add the declaration to the IR stream.
47 */
48 instructions->push_tail(var);
49
50 symtab->add_variable(var->name, var);
51 }
52
53
54 static void
55 add_builtin_variable(const builtin_variable *proto, exec_list *instructions,
56 glsl_symbol_table *symtab)
57 {
58 /* Create a new variable declaration from the description supplied by
59 * the caller.
60 */
61 const glsl_type *const type = symtab->get_type(proto->type);
62
63 assert(type != NULL);
64
65 add_variable(proto->name, proto->mode, type, instructions, symtab);
66 }
67
68
69 static void
70 generate_110_uniforms(exec_list *instructions,
71 glsl_symbol_table *symtab)
72 {
73 for (unsigned i = 0
74 ; i < Elements(builtin_110_deprecated_uniforms)
75 ; i++) {
76 add_builtin_variable(& builtin_110_deprecated_uniforms[i],
77 instructions, symtab);
78 }
79
80 /* FINISHME: Add support for gl_TextureMatrix[]. The size of this array is
81 * FINISHME: implementation dependent based on the value of
82 * FINISHME: GL_MAX_TEXTURE_COORDS.
83 */
84
85 /* FINISHME: Add support for gl_DepthRangeParameters */
86 /* FINISHME: Add support for gl_ClipPlane[] */
87 /* FINISHME: Add support for gl_PointParameters */
88
89 /* FINISHME: Add support for gl_MaterialParameters
90 * FINISHME: (glFrontMaterial, glBackMaterial)
91 */
92
93 /* FINISHME: Add support for gl_LightSource[] */
94 /* FINISHME: Add support for gl_LightModel */
95 /* FINISHME: Add support for gl_FrontLightProduct[], gl_BackLightProduct[] */
96 /* FINISHME: Add support for gl_TextureEnvColor[] */
97 /* FINISHME: Add support for gl_ObjectPlane*[], gl_EyePlane*[] */
98 /* FINISHME: Add support for gl_Fog */
99 }
100
101 static void
102 generate_110_vs_variables(exec_list *instructions,
103 glsl_symbol_table *symtab)
104 {
105 for (unsigned i = 0; i < Elements(builtin_core_vs_variables); i++) {
106 add_builtin_variable(& builtin_core_vs_variables[i],
107 instructions, symtab);
108 }
109
110 for (unsigned i = 0
111 ; i < Elements(builtin_110_deprecated_vs_variables)
112 ; i++) {
113 add_builtin_variable(& builtin_110_deprecated_vs_variables[i],
114 instructions, symtab);
115 }
116 generate_110_uniforms(instructions, symtab);
117
118 /* FINISHME: The size of this array is implementation dependent based on the
119 * FINISHME: value of GL_MAX_TEXTURE_COORDS. GL_MAX_TEXTURE_COORDS must be
120 * FINISHME: at least 2, so hard-code 2 for now.
121 */
122 const glsl_type *const vec4_type =
123 glsl_type::get_instance(GLSL_TYPE_FLOAT, 4, 0);
124 const glsl_type *const vec4_array_type =
125 glsl_type::get_array_instance(vec4_type, 2);
126
127 add_variable("gl_TexCoord", ir_var_out, vec4_array_type, instructions,
128 symtab);
129 }
130
131
132 static void
133 generate_120_vs_variables(exec_list *instructions,
134 glsl_symbol_table *symtab)
135 {
136 /* GLSL version 1.20 did not add any built-in variables in the vertex
137 * shader.
138 */
139 generate_110_vs_variables(instructions, symtab);
140 }
141
142
143 static void
144 generate_130_vs_variables(exec_list *instructions,
145 glsl_symbol_table *symtab)
146 {
147 generate_120_vs_variables(instructions, symtab);
148
149 for (unsigned i = 0; i < Elements(builtin_130_vs_variables); i++) {
150 add_builtin_variable(& builtin_130_vs_variables[i],
151 instructions, symtab);
152 }
153
154 /* FINISHME: The size of this array is implementation dependent based on
155 * FINISHME: the value of GL_MAX_CLIP_DISTANCES.
156 */
157 const glsl_type *const clip_distance_array_type =
158 glsl_type::get_array_instance(glsl_type::float_type, 8);
159 add_variable("gl_ClipDistance", ir_var_out, clip_distance_array_type,
160 instructions, symtab);
161
162 }
163
164
165 static void
166 initialize_vs_variables(exec_list *instructions,
167 struct _mesa_glsl_parse_state *state)
168 {
169
170 switch (state->language_version) {
171 case 110:
172 generate_110_vs_variables(instructions, state->symbols);
173 break;
174 case 120:
175 generate_120_vs_variables(instructions, state->symbols);
176 break;
177 case 130:
178 generate_130_vs_variables(instructions, state->symbols);
179 break;
180 }
181 }
182
183 static void
184 generate_110_fs_variables(exec_list *instructions,
185 glsl_symbol_table *symtab)
186 {
187 for (unsigned i = 0; i < Elements(builtin_core_fs_variables); i++) {
188 add_builtin_variable(& builtin_core_fs_variables[i],
189 instructions, symtab);
190 }
191
192 for (unsigned i = 0
193 ; i < Elements(builtin_110_deprecated_fs_variables)
194 ; i++) {
195 add_builtin_variable(& builtin_110_deprecated_fs_variables[i],
196 instructions, symtab);
197 }
198 generate_110_uniforms(instructions, symtab);
199
200 /* FINISHME: Add support for gl_FragData[GL_MAX_DRAW_BUFFERS]. */
201
202 /* FINISHME: The size of this array is implementation dependent based on the
203 * FINISHME: value of GL_MAX_TEXTURE_COORDS. GL_MAX_TEXTURE_COORDS must be
204 * FINISHME: at least 2, so hard-code 2 for now.
205 */
206 const glsl_type *const vec4_type =
207 glsl_type::get_instance(GLSL_TYPE_FLOAT, 4, 0);
208 const glsl_type *const vec4_array_type =
209 glsl_type::get_array_instance(vec4_type, 2);
210
211 add_variable("gl_TexCoord", ir_var_in, vec4_array_type, instructions,
212 symtab);
213 }
214
215 static void
216 generate_120_fs_variables(exec_list *instructions,
217 glsl_symbol_table *symtab)
218 {
219 /* GLSL version 1.20 did not add any built-in variables in the fragment
220 * shader.
221 */
222 generate_110_fs_variables(instructions, symtab);
223 }
224
225 static void
226 generate_130_fs_variables(exec_list *instructions,
227 glsl_symbol_table *symtab)
228 {
229 generate_120_fs_variables(instructions, symtab);
230
231 /* FINISHME: Add support fo gl_ClipDistance. The size of this array is
232 * FINISHME: implementation dependent based on the value of
233 * FINISHME: GL_MAX_CLIP_DISTANCES.
234 */
235 }
236
237 static void
238 initialize_fs_variables(exec_list *instructions,
239 struct _mesa_glsl_parse_state *state)
240 {
241
242 switch (state->language_version) {
243 case 110:
244 generate_110_fs_variables(instructions, state->symbols);
245 break;
246 case 120:
247 generate_120_fs_variables(instructions, state->symbols);
248 break;
249 case 130:
250 generate_130_fs_variables(instructions, state->symbols);
251 break;
252 }
253 }
254
255 void
256 _mesa_glsl_initialize_variables(exec_list *instructions,
257 struct _mesa_glsl_parse_state *state)
258 {
259 switch (state->target) {
260 case vertex_shader:
261 initialize_vs_variables(instructions, state);
262 break;
263 case geometry_shader:
264 break;
265 case fragment_shader:
266 initialize_fs_variables(instructions, state);
267 break;
268 }
269 }