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