Put static pointers to vec[234]_types along with the static float_type.
[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 ir_variable *
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 switch (var->mode) {
42 case ir_var_in:
43 var->shader_in = true;
44 var->read_only = true;
45 break;
46 case ir_var_inout:
47 var->shader_in = true;
48 var->shader_out = true;
49 break;
50 case ir_var_out:
51 var->shader_out = true;
52 break;
53 case ir_var_uniform:
54 var->shader_in = true;
55 var->read_only = true;
56 break;
57 default:
58 assert(0);
59 break;
60 }
61
62 /* Once the variable is created an initialized, add it to the symbol table
63 * and add the declaration to the IR stream.
64 */
65 instructions->push_tail(var);
66
67 symtab->add_variable(var->name, var);
68 return var;
69 }
70
71
72 static void
73 add_builtin_variable(const builtin_variable *proto, exec_list *instructions,
74 glsl_symbol_table *symtab)
75 {
76 /* Create a new variable declaration from the description supplied by
77 * the caller.
78 */
79 const glsl_type *const type = symtab->get_type(proto->type);
80
81 assert(type != NULL);
82
83 add_variable(proto->name, proto->mode, type, instructions, symtab);
84 }
85
86
87 static void
88 generate_110_uniforms(exec_list *instructions,
89 glsl_symbol_table *symtab)
90 {
91 for (unsigned i = 0
92 ; i < Elements(builtin_110_deprecated_uniforms)
93 ; i++) {
94 add_builtin_variable(& builtin_110_deprecated_uniforms[i],
95 instructions, symtab);
96 }
97
98 /* FINISHME: Add support for gl_TextureMatrix[]. The size of this array is
99 * FINISHME: implementation dependent based on the value of
100 * FINISHME: GL_MAX_TEXTURE_COORDS.
101 */
102
103 /* FINISHME: Add support for gl_DepthRangeParameters */
104 /* FINISHME: Add support for gl_ClipPlane[] */
105 /* FINISHME: Add support for gl_PointParameters */
106
107 /* FINISHME: Add support for gl_MaterialParameters
108 * FINISHME: (glFrontMaterial, glBackMaterial)
109 */
110
111 /* FINISHME: Add support for gl_LightSource[] */
112 /* FINISHME: Add support for gl_LightModel */
113 /* FINISHME: Add support for gl_FrontLightProduct[], gl_BackLightProduct[] */
114 /* FINISHME: Add support for gl_TextureEnvColor[] */
115 /* FINISHME: Add support for gl_ObjectPlane*[], gl_EyePlane*[] */
116 /* FINISHME: Add support for gl_Fog */
117 }
118
119 static void
120 generate_110_vs_variables(exec_list *instructions,
121 glsl_symbol_table *symtab)
122 {
123 for (unsigned i = 0; i < Elements(builtin_core_vs_variables); i++) {
124 add_builtin_variable(& builtin_core_vs_variables[i],
125 instructions, symtab);
126 }
127
128 for (unsigned i = 0
129 ; i < Elements(builtin_110_deprecated_vs_variables)
130 ; i++) {
131 add_builtin_variable(& builtin_110_deprecated_vs_variables[i],
132 instructions, symtab);
133 }
134 generate_110_uniforms(instructions, symtab);
135
136 /* FINISHME: The size of this array is implementation dependent based on the
137 * FINISHME: value of GL_MAX_TEXTURE_COORDS. GL_MAX_TEXTURE_COORDS must be
138 * FINISHME: at least 2, so hard-code 2 for now.
139 */
140 const glsl_type *const vec4_array_type =
141 glsl_type::get_array_instance(glsl_type::vec4_type, 2);
142
143 add_variable("gl_TexCoord", ir_var_out, vec4_array_type, instructions,
144 symtab);
145 }
146
147
148 static void
149 generate_120_vs_variables(exec_list *instructions,
150 glsl_symbol_table *symtab)
151 {
152 /* GLSL version 1.20 did not add any built-in variables in the vertex
153 * shader.
154 */
155 generate_110_vs_variables(instructions, symtab);
156 }
157
158
159 static void
160 generate_130_vs_variables(exec_list *instructions,
161 glsl_symbol_table *symtab)
162 {
163 generate_120_vs_variables(instructions, symtab);
164
165 for (unsigned i = 0; i < Elements(builtin_130_vs_variables); i++) {
166 add_builtin_variable(& builtin_130_vs_variables[i],
167 instructions, symtab);
168 }
169
170 /* FINISHME: The size of this array is implementation dependent based on
171 * FINISHME: the value of GL_MAX_CLIP_DISTANCES.
172 */
173 const glsl_type *const clip_distance_array_type =
174 glsl_type::get_array_instance(glsl_type::float_type, 8);
175 add_variable("gl_ClipDistance", ir_var_out, clip_distance_array_type,
176 instructions, symtab);
177
178 }
179
180
181 static void
182 initialize_vs_variables(exec_list *instructions,
183 struct _mesa_glsl_parse_state *state)
184 {
185
186 switch (state->language_version) {
187 case 110:
188 generate_110_vs_variables(instructions, state->symbols);
189 break;
190 case 120:
191 generate_120_vs_variables(instructions, state->symbols);
192 break;
193 case 130:
194 generate_130_vs_variables(instructions, state->symbols);
195 break;
196 }
197 }
198
199 static void
200 generate_110_fs_variables(exec_list *instructions,
201 glsl_symbol_table *symtab)
202 {
203 for (unsigned i = 0; i < Elements(builtin_core_fs_variables); i++) {
204 add_builtin_variable(& builtin_core_fs_variables[i],
205 instructions, symtab);
206 }
207
208 for (unsigned i = 0
209 ; i < Elements(builtin_110_deprecated_fs_variables)
210 ; i++) {
211 add_builtin_variable(& builtin_110_deprecated_fs_variables[i],
212 instructions, symtab);
213 }
214 generate_110_uniforms(instructions, symtab);
215
216 /* FINISHME: The size of this array is implementation dependent based on the
217 * FINISHME: value of GL_MAX_TEXTURE_COORDS. GL_MAX_TEXTURE_COORDS must be
218 * FINISHME: at least 2, so hard-code 2 for now.
219 */
220 const glsl_type *const vec4_array_type =
221 glsl_type::get_array_instance(glsl_type::vec4_type, 2);
222
223 add_variable("gl_TexCoord", ir_var_in, vec4_array_type, instructions,
224 symtab);
225 }
226
227
228 static void
229 generate_ARB_draw_buffers_fs_variables(exec_list *instructions,
230 glsl_symbol_table *symtab, bool warn)
231 {
232 /* FINISHME: The size of this array is implementation dependent based on the
233 * FINISHME: value of GL_MAX_DRAW_BUFFERS. GL_MAX_DRAW_BUFFERS must be
234 * FINISHME: at least 1, so hard-code 1 for now.
235 */
236 const glsl_type *const vec4_array_type =
237 glsl_type::get_array_instance(glsl_type::vec4_type, 1);
238
239 ir_variable *const fd =
240 add_variable("gl_FragData", ir_var_out, vec4_array_type, instructions,
241 symtab);
242
243 if (warn)
244 fd->warn_extension = "GL_ARB_draw_buffers";
245 }
246
247
248 static void
249 generate_120_fs_variables(exec_list *instructions,
250 glsl_symbol_table *symtab)
251 {
252 generate_110_fs_variables(instructions, symtab);
253 generate_ARB_draw_buffers_fs_variables(instructions, symtab, false);
254 }
255
256 static void
257 generate_130_fs_variables(exec_list *instructions,
258 glsl_symbol_table *symtab)
259 {
260 generate_120_fs_variables(instructions, symtab);
261
262 /* FINISHME: The size of this array is implementation dependent based on
263 * FINISHME: the value of GL_MAX_CLIP_DISTANCES.
264 */
265 const glsl_type *const clip_distance_array_type =
266 glsl_type::get_array_instance(glsl_type::float_type, 8);
267 add_variable("gl_ClipDistance", ir_var_in, clip_distance_array_type,
268 instructions, symtab);
269 }
270
271 static void
272 initialize_fs_variables(exec_list *instructions,
273 struct _mesa_glsl_parse_state *state)
274 {
275
276 switch (state->language_version) {
277 case 110:
278 generate_110_fs_variables(instructions, state->symbols);
279 break;
280 case 120:
281 generate_120_fs_variables(instructions, state->symbols);
282 break;
283 case 130:
284 generate_130_fs_variables(instructions, state->symbols);
285 break;
286 }
287
288
289 /* Since GL_ARB_draw_buffers is included in GLSL 1.20 and later, we
290 * can basically ignore any extension settings for it.
291 */
292 if (state->language_version < 120) {
293 if (state->ARB_draw_buffers_enable) {
294 generate_ARB_draw_buffers_fs_variables(instructions, state->symbols,
295 state->ARB_draw_buffers_warn);
296 }
297 }
298 }
299
300 void
301 _mesa_glsl_initialize_variables(exec_list *instructions,
302 struct _mesa_glsl_parse_state *state)
303 {
304 switch (state->target) {
305 case vertex_shader:
306 initialize_vs_variables(instructions, state);
307 break;
308 case geometry_shader:
309 break;
310 case fragment_shader:
311 initialize_fs_variables(instructions, state);
312 break;
313 }
314 }