glsl: Refactor AST-to-HIR code handling variable initializers
[mesa.git] / src / glsl / 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 "ir.h"
25 #include "glsl_parser_extras.h"
26 #include "glsl_symbol_table.h"
27 #include "builtin_variables.h"
28
29 static void generate_ARB_draw_buffers_variables(exec_list *,
30 struct _mesa_glsl_parse_state *,
31 bool, _mesa_glsl_parser_targets);
32
33 static void
34 generate_ARB_draw_instanced_variables(exec_list *,
35 struct _mesa_glsl_parse_state *,
36 bool, _mesa_glsl_parser_targets);
37
38 static ir_variable *
39 add_variable(const char *name, enum ir_variable_mode mode, int slot,
40 const glsl_type *type, exec_list *instructions,
41 glsl_symbol_table *symtab)
42 {
43 ir_variable *var = new(symtab) ir_variable(type, name, mode);
44
45 switch (var->mode) {
46 case ir_var_auto:
47 case ir_var_in:
48 case ir_var_const_in:
49 case ir_var_uniform:
50 case ir_var_system_value:
51 var->read_only = true;
52 break;
53 case ir_var_inout:
54 case ir_var_out:
55 break;
56 default:
57 assert(0);
58 break;
59 }
60
61 var->location = slot;
62 var->explicit_location = (slot >= 0);
63
64 /* Once the variable is created an initialized, add it to the symbol table
65 * and add the declaration to the IR stream.
66 */
67 instructions->push_tail(var);
68
69 symtab->add_variable(var);
70 return var;
71 }
72
73 static ir_variable *
74 add_uniform(exec_list *instructions,
75 struct _mesa_glsl_parse_state *state,
76 const char *name, const glsl_type *type)
77 {
78 return add_variable(name, ir_var_uniform, -1, type, instructions,
79 state->symbols);
80 }
81
82 static void
83 add_builtin_variable(const builtin_variable *proto, exec_list *instructions,
84 glsl_symbol_table *symtab)
85 {
86 /* Create a new variable declaration from the description supplied by
87 * the caller.
88 */
89 const glsl_type *const type = symtab->get_type(proto->type);
90
91 assert(type != NULL);
92
93 add_variable(proto->name, proto->mode, proto->slot, type, instructions,
94 symtab);
95 }
96
97 static void
98 add_builtin_constant(exec_list *instructions,
99 struct _mesa_glsl_parse_state *state,
100 const char *name, int value)
101 {
102 ir_variable *const var = add_variable(name, ir_var_auto,
103 -1, glsl_type::int_type,
104 instructions, state->symbols);
105 var->constant_value = new(var) ir_constant(value);
106 }
107
108 /* Several constants in GLSL ES have different names than normal desktop GLSL.
109 * Therefore, this function should only be called on the ES path.
110 */
111 static void
112 generate_100ES_uniforms(exec_list *instructions,
113 struct _mesa_glsl_parse_state *state)
114 {
115 add_builtin_constant(instructions, state, "gl_MaxVertexAttribs",
116 state->Const.MaxVertexAttribs);
117 add_builtin_constant(instructions, state, "gl_MaxVertexUniformVectors",
118 state->Const.MaxVertexUniformComponents);
119 add_builtin_constant(instructions, state, "gl_MaxVaryingVectors",
120 state->Const.MaxVaryingFloats / 4);
121 add_builtin_constant(instructions, state, "gl_MaxVertexTextureImageUnits",
122 state->Const.MaxVertexTextureImageUnits);
123 add_builtin_constant(instructions, state, "gl_MaxCombinedTextureImageUnits",
124 state->Const.MaxCombinedTextureImageUnits);
125 add_builtin_constant(instructions, state, "gl_MaxTextureImageUnits",
126 state->Const.MaxTextureImageUnits);
127 add_builtin_constant(instructions, state, "gl_MaxFragmentUniformVectors",
128 state->Const.MaxFragmentUniformComponents);
129
130 add_uniform(instructions, state, "gl_DepthRange",
131 state->symbols->get_type("gl_DepthRangeParameters"));
132 }
133
134 static void
135 generate_110_uniforms(exec_list *instructions,
136 struct _mesa_glsl_parse_state *state)
137 {
138 for (unsigned i = 0
139 ; i < Elements(builtin_110_deprecated_uniforms)
140 ; i++) {
141 add_builtin_variable(& builtin_110_deprecated_uniforms[i],
142 instructions, state->symbols);
143 }
144
145 add_builtin_constant(instructions, state, "gl_MaxLights",
146 state->Const.MaxLights);
147 add_builtin_constant(instructions, state, "gl_MaxClipPlanes",
148 state->Const.MaxClipPlanes);
149 add_builtin_constant(instructions, state, "gl_MaxTextureUnits",
150 state->Const.MaxTextureUnits);
151 add_builtin_constant(instructions, state, "gl_MaxTextureCoords",
152 state->Const.MaxTextureCoords);
153 add_builtin_constant(instructions, state, "gl_MaxVertexAttribs",
154 state->Const.MaxVertexAttribs);
155 add_builtin_constant(instructions, state, "gl_MaxVertexUniformComponents",
156 state->Const.MaxVertexUniformComponents);
157 add_builtin_constant(instructions, state, "gl_MaxVaryingFloats",
158 state->Const.MaxVaryingFloats);
159 add_builtin_constant(instructions, state, "gl_MaxVertexTextureImageUnits",
160 state->Const.MaxVertexTextureImageUnits);
161 add_builtin_constant(instructions, state, "gl_MaxCombinedTextureImageUnits",
162 state->Const.MaxCombinedTextureImageUnits);
163 add_builtin_constant(instructions, state, "gl_MaxTextureImageUnits",
164 state->Const.MaxTextureImageUnits);
165 add_builtin_constant(instructions, state, "gl_MaxFragmentUniformComponents",
166 state->Const.MaxFragmentUniformComponents);
167
168 const glsl_type *const mat4_array_type =
169 glsl_type::get_array_instance(glsl_type::mat4_type,
170 state->Const.MaxTextureCoords);
171
172 add_uniform(instructions, state, "gl_TextureMatrix", mat4_array_type);
173 add_uniform(instructions, state, "gl_TextureMatrixInverse", mat4_array_type);
174 add_uniform(instructions, state, "gl_TextureMatrixTranspose", mat4_array_type);
175 add_uniform(instructions, state, "gl_TextureMatrixInverseTranspose", mat4_array_type);
176
177 add_uniform(instructions, state, "gl_DepthRange",
178 state->symbols->get_type("gl_DepthRangeParameters"));
179
180 add_uniform(instructions, state, "gl_ClipPlane",
181 glsl_type::get_array_instance(glsl_type::vec4_type,
182 state->Const.MaxClipPlanes));
183 add_uniform(instructions, state, "gl_Point",
184 state->symbols->get_type("gl_PointParameters"));
185
186 const glsl_type *const material_parameters_type =
187 state->symbols->get_type("gl_MaterialParameters");
188 add_uniform(instructions, state, "gl_FrontMaterial", material_parameters_type);
189 add_uniform(instructions, state, "gl_BackMaterial", material_parameters_type);
190
191 const glsl_type *const light_source_array_type =
192 glsl_type::get_array_instance(state->symbols->get_type("gl_LightSourceParameters"), state->Const.MaxLights);
193
194 add_uniform(instructions, state, "gl_LightSource", light_source_array_type);
195
196 const glsl_type *const light_model_products_type =
197 state->symbols->get_type("gl_LightModelProducts");
198 add_uniform(instructions, state, "gl_FrontLightModelProduct",
199 light_model_products_type);
200 add_uniform(instructions, state, "gl_BackLightModelProduct",
201 light_model_products_type);
202
203 const glsl_type *const light_products_type =
204 glsl_type::get_array_instance(state->symbols->get_type("gl_LightProducts"),
205 state->Const.MaxLights);
206 add_uniform(instructions, state, "gl_FrontLightProduct", light_products_type);
207 add_uniform(instructions, state, "gl_BackLightProduct", light_products_type);
208
209 add_uniform(instructions, state, "gl_TextureEnvColor",
210 glsl_type::get_array_instance(glsl_type::vec4_type,
211 state->Const.MaxTextureUnits));
212
213 const glsl_type *const texcoords_vec4 =
214 glsl_type::get_array_instance(glsl_type::vec4_type,
215 state->Const.MaxTextureCoords);
216 add_uniform(instructions, state, "gl_EyePlaneS", texcoords_vec4);
217 add_uniform(instructions, state, "gl_EyePlaneT", texcoords_vec4);
218 add_uniform(instructions, state, "gl_EyePlaneR", texcoords_vec4);
219 add_uniform(instructions, state, "gl_EyePlaneQ", texcoords_vec4);
220 add_uniform(instructions, state, "gl_ObjectPlaneS", texcoords_vec4);
221 add_uniform(instructions, state, "gl_ObjectPlaneT", texcoords_vec4);
222 add_uniform(instructions, state, "gl_ObjectPlaneR", texcoords_vec4);
223 add_uniform(instructions, state, "gl_ObjectPlaneQ", texcoords_vec4);
224
225 add_uniform(instructions, state, "gl_Fog",
226 state->symbols->get_type("gl_FogParameters"));
227 }
228
229 /* This function should only be called for ES, not desktop GL. */
230 static void
231 generate_100ES_vs_variables(exec_list *instructions,
232 struct _mesa_glsl_parse_state *state)
233 {
234 for (unsigned i = 0; i < Elements(builtin_core_vs_variables); i++) {
235 add_builtin_variable(& builtin_core_vs_variables[i],
236 instructions, state->symbols);
237 }
238
239 generate_100ES_uniforms(instructions, state);
240
241 generate_ARB_draw_buffers_variables(instructions, state, false,
242 vertex_shader);
243 }
244
245
246 static void
247 generate_110_vs_variables(exec_list *instructions,
248 struct _mesa_glsl_parse_state *state)
249 {
250 for (unsigned i = 0; i < Elements(builtin_core_vs_variables); i++) {
251 add_builtin_variable(& builtin_core_vs_variables[i],
252 instructions, state->symbols);
253 }
254
255 for (unsigned i = 0
256 ; i < Elements(builtin_110_deprecated_vs_variables)
257 ; i++) {
258 add_builtin_variable(& builtin_110_deprecated_vs_variables[i],
259 instructions, state->symbols);
260 }
261 generate_110_uniforms(instructions, state);
262
263 /* From page 54 (page 60 of the PDF) of the GLSL 1.20 spec:
264 *
265 * "As with all arrays, indices used to subscript gl_TexCoord must
266 * either be an integral constant expressions, or this array must be
267 * re-declared by the shader with a size. The size can be at most
268 * gl_MaxTextureCoords. Using indexes close to 0 may aid the
269 * implementation in preserving varying resources."
270 */
271 const glsl_type *const vec4_array_type =
272 glsl_type::get_array_instance(glsl_type::vec4_type, 0);
273
274 add_variable("gl_TexCoord", ir_var_out, VERT_RESULT_TEX0, vec4_array_type,
275 instructions, state->symbols);
276
277 generate_ARB_draw_buffers_variables(instructions, state, false,
278 vertex_shader);
279 }
280
281
282 static void
283 generate_120_vs_variables(exec_list *instructions,
284 struct _mesa_glsl_parse_state *state)
285 {
286 /* GLSL version 1.20 did not add any built-in variables in the vertex
287 * shader.
288 */
289 generate_110_vs_variables(instructions, state);
290 }
291
292
293 static void
294 generate_130_vs_variables(exec_list *instructions,
295 struct _mesa_glsl_parse_state *state)
296 {
297 generate_120_vs_variables(instructions, state);
298
299 for (unsigned i = 0; i < Elements(builtin_130_vs_variables); i++) {
300 add_builtin_variable(& builtin_130_vs_variables[i],
301 instructions, state->symbols);
302 }
303
304 const glsl_type *const clip_distance_array_type =
305 glsl_type::get_array_instance(glsl_type::float_type,
306 state->Const.MaxClipPlanes);
307
308 /* FINISHME: gl_ClipDistance needs a real location assigned. */
309 add_variable("gl_ClipDistance", ir_var_out, -1, clip_distance_array_type,
310 instructions, state->symbols);
311
312 }
313
314
315 static void
316 initialize_vs_variables(exec_list *instructions,
317 struct _mesa_glsl_parse_state *state)
318 {
319
320 switch (state->language_version) {
321 case 100:
322 generate_100ES_vs_variables(instructions, state);
323 break;
324 case 110:
325 generate_110_vs_variables(instructions, state);
326 break;
327 case 120:
328 generate_120_vs_variables(instructions, state);
329 break;
330 case 130:
331 generate_130_vs_variables(instructions, state);
332 break;
333 }
334
335 if (state->ARB_draw_instanced_enable)
336 generate_ARB_draw_instanced_variables(instructions, state, false,
337 vertex_shader);
338 }
339
340
341 /* This function should only be called for ES, not desktop GL. */
342 static void
343 generate_100ES_fs_variables(exec_list *instructions,
344 struct _mesa_glsl_parse_state *state)
345 {
346 for (unsigned i = 0; i < Elements(builtin_core_fs_variables); i++) {
347 add_builtin_variable(& builtin_core_fs_variables[i],
348 instructions, state->symbols);
349 }
350
351 for (unsigned i = 0; i < Elements(builtin_100ES_fs_variables); i++) {
352 add_builtin_variable(& builtin_100ES_fs_variables[i],
353 instructions, state->symbols);
354 }
355
356 generate_100ES_uniforms(instructions, state);
357
358 generate_ARB_draw_buffers_variables(instructions, state, false,
359 fragment_shader);
360 }
361
362 static void
363 generate_110_fs_variables(exec_list *instructions,
364 struct _mesa_glsl_parse_state *state)
365 {
366 for (unsigned i = 0; i < Elements(builtin_core_fs_variables); i++) {
367 add_builtin_variable(& builtin_core_fs_variables[i],
368 instructions, state->symbols);
369 }
370
371 for (unsigned i = 0; i < Elements(builtin_110_fs_variables); i++) {
372 add_builtin_variable(& builtin_110_fs_variables[i],
373 instructions, state->symbols);
374 }
375
376 for (unsigned i = 0
377 ; i < Elements(builtin_110_deprecated_fs_variables)
378 ; i++) {
379 add_builtin_variable(& builtin_110_deprecated_fs_variables[i],
380 instructions, state->symbols);
381 }
382 generate_110_uniforms(instructions, state);
383
384 /* From page 54 (page 60 of the PDF) of the GLSL 1.20 spec:
385 *
386 * "As with all arrays, indices used to subscript gl_TexCoord must
387 * either be an integral constant expressions, or this array must be
388 * re-declared by the shader with a size. The size can be at most
389 * gl_MaxTextureCoords. Using indexes close to 0 may aid the
390 * implementation in preserving varying resources."
391 */
392 const glsl_type *const vec4_array_type =
393 glsl_type::get_array_instance(glsl_type::vec4_type, 0);
394
395 add_variable("gl_TexCoord", ir_var_in, FRAG_ATTRIB_TEX0, vec4_array_type,
396 instructions, state->symbols);
397
398 generate_ARB_draw_buffers_variables(instructions, state, false,
399 fragment_shader);
400 }
401
402
403 static void
404 generate_ARB_draw_buffers_variables(exec_list *instructions,
405 struct _mesa_glsl_parse_state *state,
406 bool warn, _mesa_glsl_parser_targets target)
407 {
408 /* gl_MaxDrawBuffers is available in all shader stages.
409 */
410 ir_variable *const mdb =
411 add_variable("gl_MaxDrawBuffers", ir_var_auto, -1,
412 glsl_type::int_type, instructions, state->symbols);
413
414 if (warn)
415 mdb->warn_extension = "GL_ARB_draw_buffers";
416
417 mdb->constant_value = new(mdb)
418 ir_constant(int(state->Const.MaxDrawBuffers));
419
420
421 /* gl_FragData is only available in the fragment shader.
422 */
423 if (target == fragment_shader) {
424 const glsl_type *const vec4_array_type =
425 glsl_type::get_array_instance(glsl_type::vec4_type,
426 state->Const.MaxDrawBuffers);
427
428 ir_variable *const fd =
429 add_variable("gl_FragData", ir_var_out, FRAG_RESULT_DATA0,
430 vec4_array_type, instructions, state->symbols);
431
432 if (warn)
433 fd->warn_extension = "GL_ARB_draw_buffers";
434 }
435 }
436
437
438 static void
439 generate_ARB_draw_instanced_variables(exec_list *instructions,
440 struct _mesa_glsl_parse_state *state,
441 bool warn,
442 _mesa_glsl_parser_targets target)
443 {
444 /* gl_InstanceIDARB is only available in the vertex shader.
445 */
446 if (target == vertex_shader) {
447 ir_variable *const inst =
448 add_variable("gl_InstanceIDARB", ir_var_system_value,
449 SYSTEM_VALUE_INSTANCE_ID,
450 glsl_type::int_type, instructions, state->symbols);
451
452 if (warn)
453 inst->warn_extension = "GL_ARB_draw_instanced";
454 }
455 }
456
457
458 static void
459 generate_ARB_shader_stencil_export_variables(exec_list *instructions,
460 struct _mesa_glsl_parse_state *state,
461 bool warn)
462 {
463 /* gl_FragStencilRefARB is only available in the fragment shader.
464 */
465 ir_variable *const fd =
466 add_variable("gl_FragStencilRefARB", ir_var_out, FRAG_RESULT_STENCIL,
467 glsl_type::int_type, instructions, state->symbols);
468
469 if (warn)
470 fd->warn_extension = "GL_ARB_shader_stencil_export";
471 }
472
473 static void
474 generate_120_fs_variables(exec_list *instructions,
475 struct _mesa_glsl_parse_state *state)
476 {
477 generate_110_fs_variables(instructions, state);
478
479 for (unsigned i = 0
480 ; i < Elements(builtin_120_fs_variables)
481 ; i++) {
482 add_builtin_variable(& builtin_120_fs_variables[i],
483 instructions, state->symbols);
484 }
485 }
486
487 static void
488 generate_130_fs_variables(exec_list *instructions,
489 struct _mesa_glsl_parse_state *state)
490 {
491 generate_120_fs_variables(instructions, state);
492
493 const glsl_type *const clip_distance_array_type =
494 glsl_type::get_array_instance(glsl_type::float_type,
495 state->Const.MaxClipPlanes);
496
497 /* FINISHME: gl_ClipDistance needs a real location assigned. */
498 add_variable("gl_ClipDistance", ir_var_in, -1, clip_distance_array_type,
499 instructions, state->symbols);
500 }
501
502 static void
503 initialize_fs_variables(exec_list *instructions,
504 struct _mesa_glsl_parse_state *state)
505 {
506
507 switch (state->language_version) {
508 case 100:
509 generate_100ES_fs_variables(instructions, state);
510 break;
511 case 110:
512 generate_110_fs_variables(instructions, state);
513 break;
514 case 120:
515 generate_120_fs_variables(instructions, state);
516 break;
517 case 130:
518 generate_130_fs_variables(instructions, state);
519 break;
520 }
521
522 if (state->ARB_shader_stencil_export_enable)
523 generate_ARB_shader_stencil_export_variables(instructions, state,
524 state->ARB_shader_stencil_export_warn);
525 }
526
527 void
528 _mesa_glsl_initialize_variables(exec_list *instructions,
529 struct _mesa_glsl_parse_state *state)
530 {
531 switch (state->target) {
532 case vertex_shader:
533 initialize_vs_variables(instructions, state);
534 break;
535 case geometry_shader:
536 break;
537 case fragment_shader:
538 initialize_fs_variables(instructions, state);
539 break;
540 }
541 }